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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1A057E77188 for ; Sun, 12 Jan 2025 17:56:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F9F86B0088; Sun, 12 Jan 2025 12:56:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AAF16B0089; Sun, 12 Jan 2025 12:56:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5716C6B008A; Sun, 12 Jan 2025 12:56:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 36FB36B0088 for ; Sun, 12 Jan 2025 12:56:19 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A435012181F for ; Sun, 12 Jan 2025 17:56:18 +0000 (UTC) X-FDA: 82999554036.11.DA3E0D0 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id DC6051C0009 for ; Sun, 12 Jan 2025 17:56:16 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="IU/bnvgj"; spf=pass (imf20.hostedemail.com: domain of "SRS0=Kc2N=UE=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org" designates 139.178.84.217 as permitted sender) smtp.mailfrom="SRS0=Kc2N=UE=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org"; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736704577; a=rsa-sha256; cv=none; b=aa6iOanXw9wWlhwa3aLxHULIvKcBPzYqHFrltiOSyLZIYmvJ4gXFGb5UDWVzQ/ajZ3Pn+F 3FQn0YcnKKLGXCOVb7EClgSUiMCXQk8hZlpn0LJz3ffClziEI5Eg6lAa9VWBny9wpA9fUR m0+sNpRyhw+sC23Aprlvp3bKWjFig7g= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="IU/bnvgj"; spf=pass (imf20.hostedemail.com: domain of "SRS0=Kc2N=UE=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org" designates 139.178.84.217 as permitted sender) smtp.mailfrom="SRS0=Kc2N=UE=paulmck-ThinkPad-P17-Gen-1.home=paulmck@kernel.org"; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736704577; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dp1n90ObwZQmRXzEg1FOGP2hcdm3IAFQPm3RSkz8YU4=; b=sRzGXCVCankcVSTJwhVziGVCgVIllICzrb0qe2mNXxX6z3AIj/mJB26kHgEWS1qyAjf8wZ NeTiPG/krG/+BJldrhrHh/uXzXweyRxc4Ik6j9eejc1jowLdXAeSSZr+sVZn+qp8qzzTQ7 Is7PXfGp/3cWJkWtuQ4qXIgaxuix4IM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9ADA75C4131; Sun, 12 Jan 2025 17:55:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25D6AC4CEDF; Sun, 12 Jan 2025 17:56:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1736704575; bh=4Dz9u9aeNhKXzT0Cx/t1lQGKVf4xK23UX6FfJoAGeug=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=IU/bnvgjJglVXdbx0F2fiktJLGwA8ePZ1EmB5ywQa06phBCabL1ZVdCFXVDF4Cgyv n7l7fyePGyql26nttBCgXtLVBy6NCnU/ouk2C0Az8zdfCsXss3qJepAYO1reVRcxSs NTf3BDw/Z/Va50j/Ib7CZOKmkehezT9tm8Xmee3Rx/pcyCOcH30qob7eMYlExVICm9 wQl6g19c4Sg57DEyGxZbrWykfW6EZZ+9ogBgah3hiwKlGt3mKoBfPhqwKHy9+BKEDv 1SF+HuVAQixY9O0e09z4MXlwF0iiuLYKXmkKJIRE/1o8Vd5HrmPkwoYGcokOlAvI5U B7IdCE+ijYAsw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id C8974CE37BE; Sun, 12 Jan 2025 09:56:14 -0800 (PST) Date: Sun, 12 Jan 2025 09:56:14 -0800 From: "Paul E. McKenney" To: David Laight Cc: Suren Baghdasaryan , akpm@linux-foundation.org, peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mjguzik@gmail.com, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, richard.weiyang@gmail.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, nathan@kernel.org Subject: Re: [PATCH v9 10/17] refcount: introduce __refcount_{add|inc}_not_zero_limited - clang 17.0.1 bug Message-ID: <52b5b501-7b98-4a9a-94e4-f3eeaaeadfad@paulmck-laptop> Reply-To: paulmck@kernel.org References: <20250111042604.3230628-1-surenb@google.com> <20250111042604.3230628-11-surenb@google.com> <20250111123900.643e4b49@pumpkin> <9086dca2-3d53-4147-abc8-bf2d1cca0b5a@paulmck-laptop> <20250111221939.3a36908d@pumpkin> <20250111225016.660c5784@pumpkin> <20250112113756.3b8a5d0a@pumpkin> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250112113756.3b8a5d0a@pumpkin> X-Rspamd-Queue-Id: DC6051C0009 X-Stat-Signature: xucj36f5fz3xssnrds588bxbquufytf8 X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1736704576-124024 X-HE-Meta: U2FsdGVkX1/CsneTUEW4/+KcHXvjIhW6Df5jAOOLjj4Wjc5z3BXlHAD6yoxFngYgggiSTd3T78viMMBE0piGsK2UeowmPDQUTIim8ctRocLQ63q3FTjhHWWGWiM/nsXV8fE3f/vz72McH2OTawYm1tQjRTEeo6QsUylLN0pEK/MOCeVwB0irhN+vL2G8g/BIPYpCvZX9BCAIVH4FDpGqq9Bntc4Q5puon+JKcfEr5f5AZuRCW3Es3/EYniDrEEQSmB6ua6oVaLO95PtKD5/pZa9InAe0GX/fXpSK2KGA7yTauD+yhAAJkxtuh5hdReS4qaVj8yva0V/w8+t/ZfmeeCZm6Inmx9PF3C27over6pqUXZfA/p3DlT/wm3wlVD2mtiPdlGi90lA0bdwJGkpzGAkF0pLxPlaMEPdNyipajC6+3UtbM6Fb1j5MfpSKStkaq2GnwCebH283c0EIhpgrchgnMLqd3bj3+12rH9zvBEDNJUjHojWkqkTInGC9qv8z5zlaZMosm2yn2GVcgD4YZ942Nsoub7bvrlDnOvwIOk43+uCtXqIYhFBDI7mVVVXs4SqOsyt3uTfGq1lIV4It/RTQZN30JzVnuK3i1Lcstba0eFGuJ9bbz0ab/SyQgoxwgqEHtdjZyLSf9oawTj5iL4lemqMKeMmpOaLJuQjtKsP0n6JUBcRDMd903VyQ0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sprgG5d4IHny1d0aK0bfGQmkrTDeG4yyMX9ARsI0/hr3Ln16LfslPQ21wW3kbN0vcylFHbjwY3hrR X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Sun, Jan 12, 2025 at 11:37:56AM +0000, David Laight wrote: > On Sat, 11 Jan 2025 22:50:16 +0000 > David Laight wrote: > > > On Sat, 11 Jan 2025 22:19:39 +0000 > > David Laight wrote: > > > > > On Sat, 11 Jan 2025 10:30:40 -0800 > > > "Paul E. McKenney" wrote: > > > > > > > On Sat, Jan 11, 2025 at 12:39:00PM +0000, David Laight wrote: > > > > > On Fri, 10 Jan 2025 20:25:57 -0800 > > > > > Suren Baghdasaryan wrote: > > > > > > > > > > > Introduce functions to increase refcount but with a top limit above which > > > > > > they will fail to increase (the limit is inclusive). Setting the limit to > > > > > > INT_MAX indicates no limit. > > > > > > > > > > This function has never worked as expected! > > > > > I've removed the update and added in the rest of the code. > > > > > > > > > > > diff --git a/include/linux/refcount.h b/include/linux/refcount.h > > > > > > index 35f039ecb272..5072ba99f05e 100644 > > > > > > --- a/include/linux/refcount.h > > > > > > +++ b/include/linux/refcount.h > > > > > > @@ -137,13 +137,23 @@ static inline unsigned int refcount_read(const refcount_t *r) > > > > > > } > > > > > > > > > > > > static inline __must_check __signed_wrap > > > > > > -bool __refcount_add_not_zero(int i, refcount_t *r, int *oldp) > > > > > > { > > > > > > int old = refcount_read(r); > > > > > > > > > > > > do { > > > > > > if (!old) > > > > > > break; > > > > > > > > > > > > } while (!atomic_try_cmpxchg_relaxed(&r->refs, &old, old + i)); > > > > > > > > > > > > if (oldp) > > > > > > *oldp = old; > > > > > ? > > > > > > if (unlikely(old < 0 || old + i < 0)) > > > > > > refcount_warn_saturate(r, REFCOUNT_ADD_NOT_ZERO_OVF); > > > > > > > > > > > > return old; > > > > > > } > > > > > > > > > > The saturate test just doesn't work as expected. > > > > > In C signed integer overflow is undefined (probably so that cpu that saturate/trap > > > > > signed overflow can be conformant) and gcc uses that to optimise code. > > > > > > > > > > So if you compile (https://www.godbolt.org/z/WYWo84Weq): > > > > > int inc_wraps(int i) > > > > > { > > > > > return i < 0 || i + 1 < 0; > > > > > } > > > > > the second test is optimised away. > > > > > I don't think the kernel compiles disable this optimisation. > > > > > > > > Last I checked, my kernel compiles specified -fno-strict-overflow. > > > > What happens if you try that in godbolt? > > > > > > That does make gcc generated the wanted object code. > > > I know that compilation option has come up before, but I couldn't remember the > > > name or whether it was disabled :-( > > > > > > You do get much better object code from return (i | i + 1) < 0; > > > And that is likely to be much better still if you need a conditional jump. > > > > I've just checked some more cases (see https://www.godbolt.org/z/YoM9odTbe). > > gcc 11 onwards generates the same code code for the two expressions. > > > > Rather more worryingly clang 17.0.1 is getting this one wrong: > > return i < 0 || i + 1 < 0 ? foo(i) : bar(i); > > It ignores the 'i + 1' test even with -fno-strict-overflow. > > That is more representative of the actual code. > > > > What have I missed now? > > A different optimisation :-( So the Linux kernel is good with signed integer overflow, right? (Give or take compiler bugs, of course...) Thanx, Paul