From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8848D3064AA; Mon, 22 Sep 2025 12:27:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758544052; cv=none; b=DASq3Z1YOMdClSsdS96ntW/Zk8iVC50+JOS7AVl8lQ5RBhnOWPdppgdQ1MDXvlp7QfJAo5v3RdIKZtzH0b6w+xNG4Gy0Ni0gHtUhwFih5l/rQJYG2oz7PNl/Z/ZEHhAbDRpxWP1YiJ/ayMJ2f15AhlYgQ5jewkiSWG90zw3K+dE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758544052; c=relaxed/simple; bh=rRfLqX/zR5w+ifwG90F2/vKyrmETA1lsBOrPDyyqAEM=; h=Subject:To:Cc:From:Date:In-Reply-To:Message-ID:MIME-Version: Content-Type; b=AkMPg28B00srF2lrvvGOOZwW50Zljj0JI9k4K9VF6tPN54sVwo201MjA869Zxv1x/bkrEvmt9gXn2BLrf8g82ecTzv8Li3eE4BY/h+mOnOpR+5D12E9pN3M2T9mYPnAgrDMm9yUHqIxOH7Guza/sTaJ6KYNjEthREYPiy1+3/zc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=vR10fA4F; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="vR10fA4F" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D4CDC113D0; Mon, 22 Sep 2025 12:27:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1758544052; bh=rRfLqX/zR5w+ifwG90F2/vKyrmETA1lsBOrPDyyqAEM=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=vR10fA4FmZ4v7KaHCZIyLcLXELsAbPUAD5lYraSzHLR8SUfcT7fvuaVAfns1d40qo ntIYtwcGsPGn6qaY9unY9gkWp6CxSXcCwRRsXp2js5R/wunwsZ0apFhkBqd5cHpQeH 1Is19V8BQwQKhQvN6kPToNslukgQ2UAJ+ojqTKAY= Subject: Patch "minmax: simplify min()/max()/clamp() implementation" has been added to the 6.6-stable tree To: David.Laight@ACULAB.COM,David.Laight@aculab.com,Rodrigo.Siqueira@amd.com,Xinhui.Pan@amd.com,agk@redhat.com,airlied@gmail.com,akpm@linux-foundation.org,alexander.deucher@amd.com,alexandre.torgue@foss.st.com,amd-gfx@lists.freedesktop.org,andriy.shevchenko@linux.intel.com,anton.ivanov@cambridgegreys.com,arnd@kernel.org,artur.paszkiewicz@intel.com,bp@alien8.de,christian.koenig@amd.com,clm@fb.com,daniel@ffwll.ch,dave.hansen@linux.intel.com,davem@davemloft.net,dm-devel@lists.linux.dev,dmitry.torokhov@gmail.com,dri-devel@lists.freedesktop.org,dsahern@kernel.org,dsterba@suse.com,edumazet@google.com,evan.quan@amd.com,farbere@amazon.com,gregkh@linuxfoundation.org,harry.wentland@amd.com,hdegoede@redhat.com,hpa@zytor.com,ilpo.jarvinen@linux.intel.com,james.morse@arm.com,jdelvare@suse.com,jejb@linux.ibm.com,jernej.skrabec@gmail.com,joabreu@synopsys.com,johannes@sipsolutions.net,josef@toxicpanda.com,keescook@chromium.org,krzysztof.kozlowski@linaro.org,kuba@kernel.org,linus.walleij@linaro.org,linux-arm-kernel@lists.infradead.org,linux-mm@kvack.org,linux-staging@lists.linux.dev,linux-stm32@st-md-mailman.stormreply.com,linux-sunxi@lists.linux.dev,linux-um@lists.infradead.org,linux@rasmusvillemoes.dk,linux@roeck-us.net,lorenzo.stoakes@oracle.com,luc.vanoostenryck@gmail.com,luto@kernel.org,maarten.lankhorst@linux.intel.com,mailhol.vincent@wanadoo.fr,malattia@linux.it,markgross@kernel.org,martin.petersen@oracle.com,mchehab@kernel.org,mcoquelin.stm32@gmail.com,mhiramat@kernel.org,minchan@kernel.org,mingo@redhat.com,mkl@pengutronix.de,mripard@kernel.org,pabeni@redhat.com,peterz@infradead.org,pmladek@suse.com,qiuxu.zhuo@intel.com,richard@nod.at,rostedt@goodmis.org,rric@kernel.org,sakari.ailus@linux.intel.com,samuel@sholland.org,senozhatsky@chromium.org,shuah@kernel.org,snitzer@kernel.org,sunpeng.li@amd.com,tglx@linutronix.de,tony.luck@intel.com,torvalds@linux-foundation.org,tzimmermann@suse.de,wad@chromium.org,wens@csie.org,wg@grandegger.com,x86@kernel.org Cc: From: Date: Mon, 22 Sep 2025 14:27:00 +0200 In-Reply-To: <20250922103241.16213-6-farbere@amazon.com> Message-ID: <2025092200-bullfrog-resolved-b3bd@gregkh> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore This is a note to let you know that I've just added the patch titled minmax: simplify min()/max()/clamp() implementation to the 6.6-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-simplify-min-max-clamp-implementation.patch and it can be found in the queue-6.6 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From stable+bounces-180928-greg=kroah.com@vger.kernel.org Mon Sep 22 12:37:49 2025 From: Eliav Farber Date: Mon, 22 Sep 2025 10:32:31 +0000 Subject: minmax: simplify min()/max()/clamp() implementation To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Cc: Linus Torvalds , David Laight , Lorenzo Stoakes Message-ID: <20250922103241.16213-6-farbere@amazon.com> From: Linus Torvalds [ Upstream commit dc1c8034e31b14a2e5e212104ec508aec44ce1b9 ] Now that we no longer have any C constant expression contexts (ie array size declarations or static initializers) that use min() or max(), we can simpify the implementation by not having to worry about the result staying as a C constant expression. So now we can unconditionally just use temporary variables of the right type, and get rid of the excessive expansion that used to come from the use of __builtin_choose_expr(__is_constexpr(...), .. to pick the specialized code for constant expressions. Another expansion simplification is to pass the temporary variables (in addition to the original expression) to our __types_ok() macro. That may superficially look like it complicates the macro, but when we only want the type of the expression, expanding the temporary variable names is much simpler and smaller than expanding the potentially complicated original expression. As a result, on my machine, doing a $ time make drivers/staging/media/atomisp/pci/isp/kernels/ynr/ynr_1.0/ia_css_ynr.host.i goes from real 0m16.621s user 0m15.360s sys 0m1.221s to real 0m2.532s user 0m2.091s sys 0m0.452s because the token expansion goes down dramatically. In particular, the longest line expansion (which was line 71 of that 'ia_css_ynr.host.c' file) shrinks from 23,338kB (yes, 23MB for one single line) to "just" 1,444kB (now "only" 1.4MB). And yes, that line is still the line from hell, because it's doing multiple levels of "min()/max()" expansion thanks to some of them being hidden inside the uDIGIT_FITTING() macro. Lorenzo has a nice cleanup patch that makes that driver use inline functions instead of macros for sDIGIT_FITTING() and uDIGIT_FITTING(), which will fix that line once and for all, but the 16-fold reduction in this case does show why we need to simplify these helpers. Cc: David Laight Cc: Lorenzo Stoakes Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber Signed-off-by: Greg Kroah-Hartman --- include/linux/minmax.h | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -35,10 +35,10 @@ #define __is_noneg_int(x) \ (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0) -#define __types_ok(x, y) \ - (__is_signed(x) == __is_signed(y) || \ - __is_signed((x) + 0) == __is_signed((y) + 0) || \ - __is_noneg_int(x) || __is_noneg_int(y)) +#define __types_ok(x, y, ux, uy) \ + (__is_signed(ux) == __is_signed(uy) || \ + __is_signed((ux) + 0) == __is_signed((uy) + 0) || \ + __is_noneg_int(x) || __is_noneg_int(y)) #define __cmp_op_min < #define __cmp_op_max > @@ -51,34 +51,31 @@ #define __cmp_once(op, type, x, y) \ __cmp_once_unique(op, type, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) -#define __careful_cmp_once(op, x, y) ({ \ - static_assert(__types_ok(x, y), \ +#define __careful_cmp_once(op, x, y, ux, uy) ({ \ + __auto_type ux = (x); __auto_type uy = (y); \ + static_assert(__types_ok(x, y, ux, uy), \ #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \ - __cmp_once(op, __auto_type, x, y); }) + __cmp(op, ux, uy); }) -#define __careful_cmp(op, x, y) \ - __builtin_choose_expr(__is_constexpr((x) - (y)), \ - __cmp(op, x, y), __careful_cmp_once(op, x, y)) +#define __careful_cmp(op, x, y) \ + __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) #define __clamp(val, lo, hi) \ ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) -#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); \ +#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ + __auto_type uval = (val); \ + __auto_type ulo = (lo); \ + __auto_type uhi = (hi); \ static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <= (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ - static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ - __clamp(unique_val, unique_lo, unique_hi); }) - -#define __careful_clamp(val, lo, hi) ({ \ - __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ - __clamp(val, lo, hi), \ - __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ - __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + static_assert(__types_ok(uval, lo, uval, ulo), "clamp() 'lo' signedness error"); \ + static_assert(__types_ok(uval, hi, uval, uhi), "clamp() 'hi' signedness error"); \ + __clamp(uval, ulo, uhi); }) + +#define __careful_clamp(val, lo, hi) \ + __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_)) /** * min - return minimum of two values of the same or compatible types Patches currently in stable-queue which might be from farbere@amazon.com are queue-6.6/minmax-don-t-use-max-in-situations-that-want-a-c-constant-expression.patch queue-6.6/minmax-make-generic-min-and-max-macros-available-everywhere.patch queue-6.6/minmax-fix-up-min3-and-max3-too.patch queue-6.6/minmax-add-a-few-more-min_t-max_t-users.patch queue-6.6/minmax-improve-macro-expansion-and-type-checking.patch queue-6.6/minmax-avoid-overly-complicated-constant-expressions-in-vm-code.patch queue-6.6/minmax-simplify-min-max-clamp-implementation.patch queue-6.6/minmax-simplify-and-clarify-min_t-max_t-implementation.patch