From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 51132C433EF for ; Mon, 13 Dec 2021 11:09:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234376AbhLMLJL (ORCPT ); Mon, 13 Dec 2021 06:09:11 -0500 Received: from foss.arm.com ([217.140.110.172]:51818 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234158AbhLMLJI (ORCPT ); Mon, 13 Dec 2021 06:09:08 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 05ACD6D; Mon, 13 Dec 2021 03:09:08 -0800 (PST) Received: from FVFF77S0Q05N (unknown [10.57.67.68]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 639543F793; Mon, 13 Dec 2021 03:09:06 -0800 (PST) Date: Mon, 13 Dec 2021 11:09:03 +0000 From: Mark Rutland To: Peter Zijlstra Cc: will@kernel.org, boqun.feng@gmail.com, linux-kernel@vger.kernel.org, x86@kernel.org, elver@google.com, keescook@chromium.org, hch@infradead.org, torvalds@linux-foundation.org, axboe@kernel.dk Subject: Re: [PATCH v2 3/9] atomic: Introduce atomic_{inc,dec,dec_and_test}_overflow() Message-ID: References: <20211210161618.645249719@infradead.org> <20211210162313.464256797@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 13, 2021 at 11:59:48AM +0100, Peter Zijlstra wrote: > On Mon, Dec 13, 2021 at 10:06:01AM +0000, Mark Rutland wrote: > > On Fri, Dec 10, 2021 at 05:16:21PM +0100, Peter Zijlstra wrote: > > > > +#ifndef arch_atomic_inc_overflow > > > +#define arch_atomic_inc_overflow(_v, _label) \ > > > +do { \ > > > + int __old = arch_atomic_fetch_inc(_v); \ > > > + if (unlikely(__old <= 0)) \ > > > + goto _label; \ > > > +} while (0) > > > +#endif > > > + > > > +#ifndef arch_atomic_dec_overflow > > > +#define arch_atomic_dec_overflow(_v, _label) \ > > > +do { \ > > > + int __new = arch_atomic_dec_return(_v); \ > > > + if (unlikely(__new <= 0)) \ > > > + goto _label; \ > > > +} while (0) > > > +#endif > > > + > > > +#ifndef arch_atomic_dec_and_test_overflow > > > +#define arch_atomic_dec_and_test_overflow(_v, _label) \ > > > +({ \ > > > + bool __ret = false; \ > > > + int __new = arch_atomic_dec_return(_v); \ > > > + if (unlikely(__new < 0)) \ > > > + goto _label; \ > > > + if (unlikely(__new == 0)) \ > > > + __ret = true; \ > > > + __ret; \ > > > +}) > > > +#endif > > > > I had wanted to move at least part of this to a function to ensure > > single-evaluation and avoid accidental symbol aliasing, but (as we discussed > > over IRC) I couldn't find any good way to do so, and given this is sufficiently > > specialise I think we should be ok with this as-is. It's certainly no worse > > than the existing stuff for xchg/cmpxchg. > > Right, as you know I tried the same :-) Anyway, the above macros should > be free of multi-evaluation issues, both _v and _label are only used the > once. Aliassing is always a possibility but minimized by __ prefixing > the local variables. Agreed! I just wanted something archived in the thread mentioning that this was the only practical option. I agree that as this stands we're not doing anything sufficiently complicated enough to worry about multi-evaluation, and that the underscores should be sufficient to avoid aliasing in practice. Mark.