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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7170CCCD199 for ; Fri, 17 Oct 2025 13:49:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6B828E008E; Fri, 17 Oct 2025 09:49:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C1B948E0006; Fri, 17 Oct 2025 09:49:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B0B0C8E008E; Fri, 17 Oct 2025 09:49:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9BCBC8E0006 for ; Fri, 17 Oct 2025 09:49:35 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 350B8855D5 for ; Fri, 17 Oct 2025 13:49:35 +0000 (UTC) X-FDA: 84007738710.02.E54F1A3 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf27.hostedemail.com (Postfix) with ESMTP id 5D0BA4000E for ; Fri, 17 Oct 2025 13:49:33 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=fail ("body hash did not verify") header.d=linuxfoundation.org header.s=korg header.b="k7BKk/J+"; spf=pass (imf27.hostedemail.com: domain of gregkh@linuxfoundation.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1760708973; 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:dkim-signature; bh=T95hHO/pOCUOF/uEdxSIqx1eDt1shT08jSSXk8Aky8M=; b=cQDHbU8chWBCy7XFnHNOQaxseSe9YonA8IbYYQAS6iewXHFaxo8BQCRz4iv9Qr5gU45N7j KsHrwaQJ3SFj1o7fINkCXZ/ueUqM/flgK053b+PbfHmP1mQ2vK6wXYv2YtmSIPAO7j0BnK WV6ASG+5NScP8232EJKM/6QiHvReXGQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=fail ("body hash did not verify") header.d=linuxfoundation.org header.s=korg header.b="k7BKk/J+"; spf=pass (imf27.hostedemail.com: domain of gregkh@linuxfoundation.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1760708973; a=rsa-sha256; cv=none; b=ibq52Vgi+41VgqP5GGGku4gVOSn1jJmWPIwIMxXD69l5nZmOdW+HC4hiuOzFpmZ5m4/pt4 vwp9klg2L8SkI+Pxwwhzj+o9Wz/5mWrntkytcl8XKSvqn3XRK/UmI1REFHJJFD9HVGhCR7 LFhtqtRAtxzvbXGgXWeXZWnGnNOUYGE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 6A1994B43F; Fri, 17 Oct 2025 13:49:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD758C4CEFE; Fri, 17 Oct 2025 13:49:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1760708972; bh=C4SzgmKRE06gjdbVP9S3zeoEfakcGE72FYSgstJTWko=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=k7BKk/J+YheiXrEDuIn++bF+Qm4flIGkIrK7ul7+IuO5OZW88JhojCPovXU8GBaq9 oOLQtT3qi9nM6hefhAf8QLy9HVPd7SocYic8+sZXqMXLRlfqdSjf+Buc8uzEoO986B bFHefq3pARjF1CNIScIHqzf1El9QhWpsLu9083hE= Subject: Patch "minmax: sanity check constant bounds when clamping" has been added to the 5.10-stable tree To: David.Laight@ACULAB.COM, Jason@zx2c4.com, adilger.kernel@dilger.ca, agk@redhat.com, airlied@linux.ie, akpm@linux-foundation.org, alexander.deucher@amd.com, alexandre.torgue@st.com, amd-gfx@lists.freedesktop.org, andriy.shevchenko@linux.intel.com, anton.ivanov@cambridgegreys.com, artur.paszkiewicz@intel.com, bp@alien8.de, brian.starkey@arm.com, bvanassche@acm.org, chao@kernel.org, christian.koenig@amd.com, clm@fb.com, coreteam@netfilter.org, daniel@ffwll.ch, dave.hansen@linux.intel.com, davem@davemloft.net, dm-devel@redhat.com, dmitry.torokhov@gmail.com, dri-devel@lists.freedesktop.org, dsterba@suse.com, dushistov@mail.ru, evan.quan@amd.com, farbere@amazon.com, fery@cypress.com, freedreno@lists.freedesktop.org, fw@strlen.de, gregkh@linuxfoundation.org, harry.wentland@amd.com, hdegoede@redhat.com, herve.codina@bootlin.com, hpa@zytor.com, intel-linux-scu@intel.com, jack@suse.com, james.morse@arm.com, james.qian.wang@arm.com, jdelvare@suse.com, jdike@addtoit.com, jejb@linux.ibm.com, jmaloy@redhat.com, joabreu@synopsys.com, josef@toxicpanda.com, kadlec@netfilter.org, kbusch@kernel.org, keescook@chromium.org, kuba@kernel.org, kuznet@ms2.inr.ac.ru, linux-arm-kernel@lists.infradead.org, linux-erofs@lists.ozlabs.org, linux-mm@kvack.org, linux-staging@lists.linux.dev, linux-stm32@st-md-mailman.stormreply.com, linux-um@lists.infradead.org, linux@armlinux.org.uk, linux@rasmusvillemoes.dk, linux@roeck-us.net, liviu.dudau@arm.com, luc.vanoostenryck@gmail.com, luto@kernel.org, maarten.lankhorst@linux.intel.com, malattia@linux.it, martin.petersen@oracle.com, mchehab@kernel.org, mcoquelin.stm32@gmail.com, mgross@linux.intel.com, mihail.atanassov@arm.com, minchan@kernel.org, mingo@redhat.com, mripard@kernel.org, nathan@kernel.org, ndesaulniers@google.com, ngupta@vflare.org, pablo@netfilter.org, peppe.cavallaro@st.com, peterz@infradead.org, pmladek@suse.com, qiuxu.zhuo@intel.com, rajur@chelsio.com, richard@nod.at, robdclark@gmail.com, rostedt@goodmis.org, rric@kernel.org, ruanjinjie@huawei.com, sakari.ailus@linux.intel.com, sashal@kernel.org, sean@poorly.run, serge@kvack.org, y.senozhatsky@gmail.com, snitzer@redhat.com, sunpeng.li@amd.com, tglx@linutronix.de, tipc-discussion@lists.sourceforge.net, tony.luck@intel.com, tytso@mit.edu, tzimmermann@suse.de, willy@infradead.org, x86@kernel.org, xiang@kernel.org, ying.xue@windriver.com, yoshfuji@linux-ipv6.org Cc: From: Date: Fri, 17 Oct 2025 15:48:31 +0200 In-Reply-To: <20251017090519.46992-4-farbere@amazon.com> Message-ID: <2025101731-unholy-bulginess-e51e@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore X-Rspamd-Queue-Id: 5D0BA4000E X-Rspamd-Server: rspam11 X-Rspam-User: X-Stat-Signature: 5z93ojkag15ysdz4dw8oqg7sip1r9sik X-HE-Tag: 1760708973-643390 X-HE-Meta: U2FsdGVkX1+MDq4SxGPmM+u4fhztf5GrAwi/maIDgwa3iDk6at6X+RG6dgcE+Q2Kk6MW3aF0l7gzLk4lhL+uM//mVKsl4m1lGP8PbcCYVIXmp2Phvsw86hq9Ur/rnwcCHmaRvjDzBkOkeCvolM0jjVjBs6ZItDg57elsHc/hMTNnePizSOD/dtH8TRNo1lSuHZYeiMQ3cFQK1rOKMRr8XxiLuQhAPyBMUlT0YqmDRJxPIN0EkrDf39EJfocDVQ+C64B5o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GkleL3RozgTpCc7q8YLehxnOqX5PrmeGJ0YgwvDOhp7WwqvL/s7vQpg0/qe9HGppnkERYyoiLxZ8NEBMFzgT5JzWI9xC2HWy1aeSQIk2XZOVJhfViPRJixrQf8I2Cc1o4pj/kq9GcyKjajWNFntaxJEiQP8GNWRMdTHwhrt+F3p15aaWacxAtIpPQnI2Namyem8wJnnHJBI8IkMdkNtmBkewCkJi4kE0vRy1eWFb/AbDqDlp0s+n8A6AYdw5a5pcDQjOJQLQIwISad9bU/4vcIQzn1LKYcNp+ujQtx2rfiCD9x7TIfIJmbFQ4fCytgjPXD4hja1vQIjnI1qXarloa9+sEUzLDqNvMn2r/sNm2ycE0cSFg/GzDvw+KXGthqnokUGLAB+j08xL7++da0uIY9cuVpxGdgUUPeZkHjVVVX/LBfrpO67QPzVlnPWUrc0bTkovkktV2ACsPamlquTJX+z22fAvmkSPbUIvaCgw+W+cGWnyTyufVx+IMxmRfNLpHb/VJD2OQy0grgsBoCrcGC6UM9R2xEhD0XD8dUiU7fvmIkMw HexHak2X 2qdVwwB8JTurcSFd34TyvSeUblXiVwLU+f9XzwDScKnF42a7w== 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: This is a note to let you know that I've just added the patch titled minmax: sanity check constant bounds when clamping to the 5.10-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: minmax-sanity-check-constant-bounds-when-clamping.patch and it can be found in the queue-5.10 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From linux-staging+bounces-34941-greg=kroah.com@lists.linux.dev Fri Oct 17 11:08:22 2025 From: Eliav Farber Date: Fri, 17 Oct 2025 09:04:55 +0000 Subject: minmax: sanity check constant bounds when clamping To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Message-ID: <20251017090519.46992-4-farbere@amazon.com> From: "Jason A. Donenfeld" [ Upstream commit 5efcecd9a3b18078d3398b359a84c83f549e22cf ] The clamp family of functions only makes sense if hi>=lo. If hi and lo are compile-time constants, then raise a build error. Doing so has already caught buggy code. This also introduces the infrastructure to improve the clamping function in subsequent commits. [akpm@linux-foundation.org: coding-style cleanups] [akpm@linux-foundation.org: s@&&\@&& \@] Link: https://lkml.kernel.org/r/20220926133435.1333846-1-Jason@zx2c4.com Signed-off-by: Jason A. Donenfeld Reviewed-by: Andy Shevchenko Cc: Kees Cook Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber Signed-off-by: Greg Kroah-Hartman --- include/linux/minmax.h | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -37,6 +37,28 @@ __cmp(x, y, op), \ __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) +#define __clamp(val, lo, hi) \ + __cmp(__cmp(val, lo, >), hi, <) + +#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ + typeof(val) unique_val = (val); \ + typeof(lo) unique_lo = (lo); \ + typeof(hi) unique_hi = (hi); \ + __clamp(unique_val, unique_lo, unique_hi); }) + +#define __clamp_input_check(lo, hi) \ + (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ + __is_constexpr((lo) > (hi)), (lo) > (hi), false))) + +#define __careful_clamp(val, lo, hi) ({ \ + __clamp_input_check(lo, hi) + \ + __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \ + __typecheck(hi, lo) && __is_constexpr(val) && \ + __is_constexpr(lo) && __is_constexpr(hi), \ + __clamp(val, lo, hi), \ + __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ + __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + /** * min - return minimum of two values of the same or compatible types * @x: first value @@ -103,7 +125,7 @@ * This macro does strict typechecking of @lo/@hi to make sure they are of the * same type as @val. See the unnecessary pointer comparisons. */ -#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) +#define clamp(val, lo, hi) __careful_clamp(val, lo, hi) /* * ..and if you can't take the strict @@ -138,7 +160,7 @@ * This macro does no typechecking and uses temporary variables of type * @type to make all the comparisons. */ -#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi)) /** * clamp_val - return a value clamped to a given range using val's type Patches currently in stable-queue which might be from farbere@amazon.com are queue-5.10/minmax-allow-comparisons-of-int-against-unsigned-char-short.patch queue-5.10/minmax-add-a-few-more-min_t-max_t-users.patch queue-5.10/minmax-improve-macro-expansion-and-type-checking.patch queue-5.10/minmax-fix-indentation-of-__cmp_once-and-__clamp_once.patch queue-5.10/minmax.h-simplify-the-variants-of-clamp.patch queue-5.10/minmax-add-in_range-macro.patch queue-5.10/minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch queue-5.10/minmax-allow-min-max-clamp-if-the-arguments-have-the-same-signedness.patch queue-5.10/minmax-don-t-use-max-in-situations-that-want-a-c-constant-expression.patch queue-5.10/minmax.h-remove-some-defines-that-are-only-expanded-once.patch queue-5.10/minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch queue-5.10/minmax-simplify-min-max-clamp-implementation.patch queue-5.10/minmax-deduplicate-__unconst_integer_typeof.patch queue-5.10/minmax-simplify-and-clarify-min_t-max_t-implementation.patch queue-5.10/minmax.h-add-whitespace-around-operators-and-after-commas.patch queue-5.10/minmax-sanity-check-constant-bounds-when-clamping.patch queue-5.10/minmax-avoid-overly-complicated-constant-expressions-in-vm-code.patch queue-5.10/minmax-make-generic-min-and-max-macros-available-everywhere.patch queue-5.10/minmax-fix-up-min3-and-max3-too.patch queue-5.10/minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch queue-5.10/minmax-fix-header-inclusions.patch queue-5.10/minmax-introduce-min-max-_array.patch queue-5.10/btrfs-remove-duplicated-in_range-macro.patch queue-5.10/overflow-tracing-define-the-is_signed_type-macro-once.patch queue-5.10/minmax-relax-check-to-allow-comparison-between-unsigned-arguments-and-signed-constants.patch queue-5.10/minmax-clamp-more-efficiently-by-avoiding-extra-comparison.patch queue-5.10/minmax.h-update-some-comments.patch