From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751136AbdEaLJu (ORCPT ); Wed, 31 May 2017 07:09:50 -0400 Received: from merlin.infradead.org ([205.233.59.134]:37698 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751025AbdEaLJs (ORCPT ); Wed, 31 May 2017 07:09:48 -0400 Date: Wed, 31 May 2017 13:09:18 +0200 From: Peter Zijlstra To: "Reshetova, Elena" Cc: Kees Cook , "linux-kernel@vger.kernel.org" , Christoph Hellwig , "Eric W. Biederman" , Andrew Morton , Josh Poimboeuf , PaX Team , Jann Horn , Eric Biggers , Hans Liljestrand , David Windsor , Greg KH , Ingo Molnar , Alexey Dobriyan , "Serge E. Hallyn" , "arozansk@redhat.com" , Davidlohr Bueso , Manfred Spraul , "axboe@kernel.dk" , James Bottomley , "x86@kernel.org" , Ingo Molnar , Arnd Bergmann , "David S. Miller" , Rik van Riel , linux-arch , "kernel-hardening@lists.openwall.com" Subject: Re: [PATCH v5 1/3] refcount: Create unchecked atomic_t implementation Message-ID: <20170531110918.tgbppvxgkkhoaclk@hirez.programming.kicks-ass.net> References: <1496180392-98718-1-git-send-email-keescook@chromium.org> <1496180392-98718-2-git-send-email-keescook@chromium.org> <2236FBA76BA1254E88B949DDB74E612B6B215E2E@IRSMSX102.ger.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2236FBA76BA1254E88B949DDB74E612B6B215E2E@IRSMSX102.ger.corp.intel.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 31, 2017 at 10:45:09AM +0000, Reshetova, Elena wrote: > > +static inline __must_check bool refcount_add_not_zero(unsigned int i, > > + > > refcount_t *r) > > +{ > > + return atomic_add_return(i, &r->refs) != 0; > > +} > > Maybe atomic_add_unless(&r->refs, i, 0) in order to be consistent with the below inc_not_zero implementation? Yes, atomic_add_return() is strictly incorrect here since the add is unconditional. > > +static inline __must_check bool refcount_sub_and_test(unsigned int i, > > + > > refcount_t *r) > > +{ > > + return atomic_sub_return(i, &r->refs) == 0; > > +} > > Any reason for not using atomic_sub_and_test() here? > > +static inline __must_check bool refcount_dec_and_test(refcount_t *r) > > +{ > > + return atomic_dec_return(&r->refs) == 0; > > +} > > Same here: atomic_dec_and_test()? Both those are better because they return condition codes generated from the operand itself.