Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1428121AbdDXUTk (ORCPT ); Mon, 24 Apr 2017 16:19:40 -0400 Received: from mail-io0-f169.google.com ([209.85.223.169]:36003 "EHLO mail-io0-f169.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1174252AbdDXUTc (ORCPT ); Mon, 24 Apr 2017 16:19:32 -0400 MIME-Version: 1.0 In-Reply-To: <20170424104539.qa2jva2jytwboeam@hirez.programming.kicks-ass.net> References: <20170421220939.GA65363@beast> <20170424104539.qa2jva2jytwboeam@hirez.programming.kicks-ass.net> From: Kees Cook Date: Mon, 24 Apr 2017 13:19:24 -0700 X-Google-Sender-Auth: WU6fhw0OffFdfQrZ8wd6jW2SJMM Message-ID: Subject: Re: [PATCH] x86/refcount: Implement fast refcount_t handling To: Peter Zijlstra Cc: LKML , Eric Biggers , Christoph Hellwig , "axboe@kernel.dk" , James Bottomley , Elena Reshetova , Hans Liljestrand , David Windsor , "x86@kernel.org" , Ingo Molnar , Arnd Bergmann , Greg Kroah-Hartman , Jann Horn , "David S. Miller" , linux-arch , "kernel-hardening@lists.openwall.com" , PaX Team Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 873 Lines: 34 On Mon, Apr 24, 2017 at 3:45 AM, Peter Zijlstra wrote: > On Fri, Apr 21, 2017 at 03:09:39PM -0700, Kees Cook wrote: >> +static __always_inline __must_check bool refcount_inc_not_zero(refcount_t *r) >> +{ >> + const int a = 1; >> + const int u = 0; >> + int c, old; >> + >> + c = atomic_read(&(r->refs)); >> + for (;;) { >> + if (unlikely(c == (u))) >> + break; >> + old = atomic_cmpxchg(&(r->refs), c, c + (a)); > > Please use atomic_try_cmpxchg(), that generates saner code. Ah-ha, thanks. I actually copied this directly out of the existing atomic_t function, so we should probably update it there too. -Kees > >> + if (likely(old == c)) >> + break; >> + c = old; >> + } >> + return c != u; >> +} -- Kees Cook Pixel Security