From owner-freebsd-arch@FreeBSD.ORG Mon Oct 27 16:29:23 2014 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id DA659DDF for ; Mon, 27 Oct 2014 16:29:23 +0000 (UTC) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [IPv6:2001:470:1f11:75::1]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id B3044E84 for ; Mon, 27 Oct 2014 16:29:23 +0000 (UTC) Received: from ralph.baldwin.cx (pool-173-70-85-31.nwrknj.fios.verizon.net [173.70.85.31]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id A575FB941; Mon, 27 Oct 2014 12:29:22 -0400 (EDT) From: John Baldwin To: freebsd-arch@freebsd.org Subject: Re: refcount_release_take_##lock Date: Mon, 27 Oct 2014 11:27:45 -0400 Message-ID: <2629048.tOq3sNXcCP@ralph.baldwin.cx> User-Agent: KMail/4.14.2 (FreeBSD/10.1-PRERELEASE; KDE/4.14.2; amd64; ; ) In-Reply-To: <20141025190407.GU82214@funkthat.com> References: <20141025184448.GA19066@dft-labs.eu> <20141025190407.GU82214@funkthat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Mon, 27 Oct 2014 12:29:22 -0400 (EDT) Cc: John-Mark Gurney , Mateusz Guzik X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Oct 2014 16:29:23 -0000 On Saturday, October 25, 2014 12:04:07 PM John-Mark Gurney wrote: > Mateusz Guzik wrote this message on Sat, Oct 25, 2014 at 20:44 +0200: > > The following idiom is used here and there: > > > > int old; > > old = obj->ref; > > if (old > 1 && atomic_cmpset_int(&obj->ref, old, old -1)) > > > > return; > > > > lock(&something); > > if (refcount_release(&obj->ref) == 0) { > > > > unlock(&something); > > return; > > > > } > > free up > > unlock(&something); > > > > ========== > > Couldn't this be better written as: > if (__predict_false(refcount_release(&obj->ref) == 0)) { > lock(&something); > if (__predict_true(!obj->ref)) { > free up > } > unlock(&something); > } > > The reason I'm asking is that I changed how IPsec SA ref counting was > handled, and used something similar... No, this has a race as others have noted. Please go fix the IPsec code. :) > My code gets rid of a branch, and is better in that it uses refcount > API properly, instead of using atomic_cmpset_int... He is extending the refcount() API (which uses atomic_* internally). The API implementation _should_ use atomic_* directly. Mateusz, Please keep the refcount_*() prefix so it matches the rest of the API. I would just declare the functions directly in refcount.h rather than requiring a macro to be invoked in each C file. We can also just implement the needed lock types for now instead of all of them. You could maybe replace 'take' with 'lock', but either name is fine. -- John Baldwin