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 1357FE7719D for ; Sun, 12 Jan 2025 11:38:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 671EC6B0095; Sun, 12 Jan 2025 06:38:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 621D16B0098; Sun, 12 Jan 2025 06:38:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E8CF6B0099; Sun, 12 Jan 2025 06:38:03 -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 323066B0095 for ; Sun, 12 Jan 2025 06:38:03 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 805401A17D4 for ; Sun, 12 Jan 2025 11:38:02 +0000 (UTC) X-FDA: 82998600804.16.C0ED692 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by imf30.hostedemail.com (Postfix) with ESMTP id 8C5398001B for ; Sun, 12 Jan 2025 11:38:00 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fz9KH5tQ; spf=pass (imf30.hostedemail.com: domain of david.laight.linux@gmail.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=david.laight.linux@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736681880; h=from:from:sender: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=O66R6Ait/z6GS/vRVPqZx0IxMgTAQOgliTkMHnf2X+4=; b=f2cfrN+llZJ3rjl2ISgv92oRtBhyNUKM7pWKkw3WMTrvddpGE5qHl+4dU86ct2OCTerFqU g/Rq/boaZh6uOGk8dTkJGsCx0M8lFZP4imH42zlPtNxd0oRMgT0j9p1kgljgH5c3evp838 WGekOxTGfTfCAr8jJH4zx1aECTob4ds= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fz9KH5tQ; spf=pass (imf30.hostedemail.com: domain of david.laight.linux@gmail.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=david.laight.linux@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736681880; a=rsa-sha256; cv=none; b=AQCNLCZLTkjMezRvQwGULM6af0X8yVuQCMZaJALfkOpw8VB7mrEI5NmU1mSslH8c0B0SpG KgB3fs3ZWdT+VuA7npIaTBTLka4G5irW/AL+WZWq4dVGJ2Uzo8jNyj6/ZVFnSJwJKjYp+v nvx1QgxJ9b0QQ71vyDDg6/H5qHkynkA= Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-38633b5dbcfso3479098f8f.2 for ; Sun, 12 Jan 2025 03:38:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736681879; x=1737286679; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=O66R6Ait/z6GS/vRVPqZx0IxMgTAQOgliTkMHnf2X+4=; b=fz9KH5tQEmWPxH1lngPmuxfaIaUbr31TYhQs/nVcdX3O3KWpEC+sxAUdygNJH3YQ5d 3LQnAB9sECJbqQJj+nre33+d9xTLAp6VkxKK1+/E0pHvKPXhvPoQY9vVYaS5cWrNpdqt Et84yVKyqHwslp0kIVX4XDUbXdUVtBbYMB7PQbu8xhlTOBwLcc+Z+frKIXkovkQsqwxn hrcAyl3DhZSwMKb1AaEHyPauyYfAo3FUEXZQJxqYigDNaCy9N66KUsnd6ing8ZesnSKn eHinVgs6uCPMpYPqg42P4B7aVSmpQuIC0H4IymJiV6+VhSc/sLwyK+cKIbhVYQFx49QA 71RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736681879; x=1737286679; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O66R6Ait/z6GS/vRVPqZx0IxMgTAQOgliTkMHnf2X+4=; b=aqiyGcttBU4KqYRuo4ODx15AdiwRDYBQRWN7cM5kwiFVFV7q9Dp5rDFDgmuchhHyNi 7o/brhPdQVKA2Y4U9tdRzty1FGKeiTMKEcCFEDdt7l0PJDMJY9TtimFiiWH83y53xzMW QSYxu7yx4km3EiVPZbhgk0BIGJP/s8AEKmwyUzveT/ko8KNt+roTAUiP0F03mU07kfCh 9El7TzJNxZbXknv6FG0Ba7Wb38NLMVXK4MVAf08q1pBgKJJiYe/qLeUQ+Fju8RlDcjHb KaFdOmmmga3qcREv+MaAzJLw+YoP2PXZj1bvof00s/cgIddKi/HF50Pjnw/ehF3FKLEd Up1w== X-Forwarded-Encrypted: i=1; AJvYcCUGmC6F4VD0JMMHH85io4BPB5Lqdj+LDcI5grLzYANvMhXMg43LWvz6s9Iq7rvqen4+lvaZHSfEFA==@kvack.org X-Gm-Message-State: AOJu0YxW6BQvDzo2WJNtXFUBE6SRYlIPhduO7MRd2PywyKaZZ4fajF/7 84fdiFyjnN93DprR8XZtOZVDN8MRqEWXdW8w3zTJI9WW5iKXYstp X-Gm-Gg: ASbGncu9zm5IY9lwaaBpmCDQVYNE524wTuSQE1ss/sgnitLZGu9HawvmhwqwcwRRKas 0w7PoWXjGfTVcjEPmmCy5s9ReispBOwM+Hj07krjRNvOpczFnw6eHyqFG2gyd0DhA7PijpSU2Sv Hm5wHbFxPMgMB54X/uFJ+ZYRFi60kbW+J6zWUMrB2cuO3/VjgU/Mu0p9iHjY9cIPkWsGMX66wAX UOvGymfdqVth2kBqm37qpYohi4zIBVDc3UTF8VwO1JuBdYJ45Q+TXPvfx1D2opt5BITD0ZeO4PQ 40YO7/RdnZjaIUvyLXI= X-Google-Smtp-Source: AGHT+IGPNn5bLQdWXbCvylb6sk9BQyy0TzH6peYQPW4c4/F3yQ1S+AfAr34weGZHVZxJioeqfZ/cLg== X-Received: by 2002:a05:6000:154c:b0:38a:a043:eac4 with SMTP id ffacd0b85a97d-38aa043ee77mr4599702f8f.44.1736681878732; Sun, 12 Jan 2025 03:37:58 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-436e9dc8826sm108996695e9.11.2025.01.12.03.37.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Jan 2025 03:37:58 -0800 (PST) Date: Sun, 12 Jan 2025 11:37:56 +0000 From: David Laight To: "Paul E. McKenney" 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: <20250112113756.3b8a5d0a@pumpkin> In-Reply-To: <20250111225016.660c5784@pumpkin> 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> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 8C5398001B X-Stat-Signature: 79e4w5o6aersg1oedcmw9n9u5u8iqgkz X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1736681880-500258 X-HE-Meta: U2FsdGVkX1+9IowllqQklD2ys9VFfH8/i2uq9AdlWp/8LE4QJ8TgSObdAxDtNxuB9c/0kbNk/TsmaHKn/+lEmESFWgoSk5/pHZcl5etgUGCcEPWpLniBUirPSy/cjSilrvOUPEagR8K1bqCOMfKVrP/+qhxRnXv9MVPVRdE7B4Dr8jdMNodHvKhLWaBiYs5qxsOX9zKAsJMrgzY/eBDDGAR7aPbxC1fy2QOvJJLsyZRkyhrl9ZKKSoyq54JqUlGI8/6h2GGWUnxUFMbxHQy9X25Ftz/a+tpfL4xjVW4b2IHDRBELdo5ofzMSwQWjsrduM72KL0v9z+HF1LRq6VBgmpnykOueyoKDdUv87n/RsGvCeLcXS62RQ2zKyoGYbMKZnO92VFs5BZvYThEPa8hOoKFv+VC/dGgYOSaGeKZd36NN9WMpWfV3EiSMF0GWCEWQMTc4Q1qVt5s+nBukD7lmO7LgvDy/RludOnBYvFlzOm0KTIft56RF52mTiifu8d8HRfSxyvjMrGl7/OojTg72U0UulISkap1PGkAmJo3B2dOiXpD5zd14+2B68db3OqY2D8gWy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kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 :-( > > David >