From 245d29a01127e9192c6c1fe8fe4159fd14fbe506 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Mon, 22 Sep 2025 14:15:55 +0200 Subject: [PATCH] 6.12-stable patches added patches: minmax.h-add-whitespace-around-operators-and-after-commas.patch minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch minmax.h-remove-some-defines-that-are-only-expanded-once.patch minmax.h-simplify-the-variants-of-clamp.patch minmax.h-update-some-comments.patch minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch --- ...ce-around-operators-and-after-commas.patch | 118 +++++++++++ ...p-definitions-after-the-min-max-ones.patch | 183 ++++++++++++++++++ ...efine-expansion-of-min-max-and-clamp.patch | 100 ++++++++++ ...-defines-that-are-only-expanded-once.patch | 82 ++++++++ ...max.h-simplify-the-variants-of-clamp.patch | 97 ++++++++++ .../minmax.h-update-some-comments.patch | 128 ++++++++++++ ...g_on_msg-for-the-lo-hi-test-in-clamp.patch | 47 +++++ queue-6.12/series | 7 + 8 files changed, 762 insertions(+) create mode 100644 queue-6.12/minmax.h-add-whitespace-around-operators-and-after-commas.patch create mode 100644 queue-6.12/minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch create mode 100644 queue-6.12/minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch create mode 100644 queue-6.12/minmax.h-remove-some-defines-that-are-only-expanded-once.patch create mode 100644 queue-6.12/minmax.h-simplify-the-variants-of-clamp.patch create mode 100644 queue-6.12/minmax.h-update-some-comments.patch create mode 100644 queue-6.12/minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch diff --git a/queue-6.12/minmax.h-add-whitespace-around-operators-and-after-commas.patch b/queue-6.12/minmax.h-add-whitespace-around-operators-and-after-commas.patch new file mode 100644 index 0000000000..af27dab43f --- /dev/null +++ b/queue-6.12/minmax.h-add-whitespace-around-operators-and-after-commas.patch @@ -0,0 +1,118 @@ +From stable+bounces-180916-greg=kroah.com@vger.kernel.org Mon Sep 22 12:32:02 2025 +From: Eliav Farber +Date: Mon, 22 Sep 2025 10:31:17 +0000 +Subject: minmax.h: add whitespace around operators and after commas +To: , , , , , +Cc: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato +Message-ID: <20250922103123.14538-2-farbere@amazon.com> + +From: David Laight + +[ Upstream commit 71ee9b16251ea4bf7c1fe222517c82bdb3220acc ] + +Patch series "minmax.h: Cleanups and minor optimisations". + +Some tidyups and minor changes to minmax.h. + +This patch (of 7): + +Link: https://lkml.kernel.org/r/c50365d214e04f9ba256d417c8bebbc0@AcuMS.aculab.com +Link: https://lkml.kernel.org/r/f04b2e1310244f62826267346fde0553@AcuMS.aculab.com +Signed-off-by: David Laight +Cc: Andy Shevchenko +Cc: Arnd Bergmann +Cc: Christoph Hellwig +Cc: Dan Carpenter +Cc: Jason A. Donenfeld +Cc: Jens Axboe +Cc: Lorenzo Stoakes +Cc: Mateusz Guzik +Cc: Matthew Wilcox +Cc: Pedro Falcato +Signed-off-by: Andrew Morton +Signed-off-by: Eliav Farber +Signed-off-by: Greg Kroah-Hartman +--- + include/linux/minmax.h | 34 +++++++++++++++++----------------- + 1 file changed, 17 insertions(+), 17 deletions(-) + +--- a/include/linux/minmax.h ++++ b/include/linux/minmax.h +@@ -51,10 +51,10 @@ + * only need to be careful to not cause warnings for + * pointer use. + */ +-#define __signed_type_use(x,ux) (2+__is_nonneg(x,ux)) +-#define __unsigned_type_use(x,ux) (1+2*(sizeof(ux)<4)) +-#define __sign_use(x,ux) (is_signed_type(typeof(ux))? \ +- __signed_type_use(x,ux):__unsigned_type_use(x,ux)) ++#define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux)) ++#define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4)) ++#define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \ ++ __signed_type_use(x, ux) : __unsigned_type_use(x, ux)) + + /* + * To avoid warnings about casting pointers to integers +@@ -74,15 +74,15 @@ + #ifdef CONFIG_64BIT + #define __signed_type(ux) long + #else +- #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux)>4,1LL,1L)) ++ #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)) + #endif +-#define __is_nonneg(x,ux) statically_true((__signed_type(ux))(x)>=0) ++#define __is_nonneg(x, ux) statically_true((__signed_type(ux))(x) >= 0) + +-#define __types_ok(x,y,ux,uy) \ +- (__sign_use(x,ux) & __sign_use(y,uy)) ++#define __types_ok(x, y, ux, uy) \ ++ (__sign_use(x, ux) & __sign_use(y, uy)) + +-#define __types_ok3(x,y,z,ux,uy,uz) \ +- (__sign_use(x,ux) & __sign_use(y,uy) & __sign_use(z,uz)) ++#define __types_ok3(x, y, z, ux, uy, uz) \ ++ (__sign_use(x, ux) & __sign_use(y, uy) & __sign_use(z, uz)) + + #define __cmp_op_min < + #define __cmp_op_max > +@@ -97,7 +97,7 @@ + + #define __careful_cmp_once(op, x, y, ux, uy) ({ \ + __auto_type ux = (x); __auto_type uy = (y); \ +- BUILD_BUG_ON_MSG(!__types_ok(x,y,ux,uy), \ ++ BUILD_BUG_ON_MSG(!__types_ok(x, y, ux, uy), \ + #op"("#x", "#y") signedness error"); \ + __cmp(op, ux, uy); }) + +@@ -114,7 +114,7 @@ + static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ + (lo) <= (hi), true), \ + "clamp() low limit " #lo " greater than high limit " #hi); \ +- BUILD_BUG_ON_MSG(!__types_ok3(val,lo,hi,uval,ulo,uhi), \ ++ BUILD_BUG_ON_MSG(!__types_ok3(val, lo, hi, uval, ulo, uhi), \ + "clamp("#val", "#lo", "#hi") signedness error"); \ + __clamp(uval, ulo, uhi); }) + +@@ -154,7 +154,7 @@ + + #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ + __auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\ +- BUILD_BUG_ON_MSG(!__types_ok3(x,y,z,ux,uy,uz), \ ++ BUILD_BUG_ON_MSG(!__types_ok3(x, y, z, ux, uy, uz), \ + #op"3("#x", "#y", "#z") signedness error"); \ + __cmp(op, ux, __cmp(op, uy, uz)); }) + +@@ -326,9 +326,9 @@ static inline bool in_range32(u32 val, u + * Use these carefully: no type checking, and uses the arguments + * multiple times. Use for obvious constants only. + */ +-#define MIN(a,b) __cmp(min,a,b) +-#define MAX(a,b) __cmp(max,a,b) +-#define MIN_T(type,a,b) __cmp(min,(type)(a),(type)(b)) +-#define MAX_T(type,a,b) __cmp(max,(type)(a),(type)(b)) ++#define MIN(a, b) __cmp(min, a, b) ++#define MAX(a, b) __cmp(max, a, b) ++#define MIN_T(type, a, b) __cmp(min, (type)(a), (type)(b)) ++#define MAX_T(type, a, b) __cmp(max, (type)(a), (type)(b)) + + #endif /* _LINUX_MINMAX_H */ diff --git a/queue-6.12/minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch b/queue-6.12/minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch new file mode 100644 index 0000000000..8016dbc8cd --- /dev/null +++ b/queue-6.12/minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch @@ -0,0 +1,183 @@ +From c3939872ee4a6b8bdcd0e813c66823b31e6e26f7 Mon Sep 17 00:00:00 2001 +From: David Laight +Date: Mon, 18 Nov 2024 19:14:19 +0000 +Subject: minmax.h: move all the clamp() definitions after the min/max() ones + +From: David Laight + +commit c3939872ee4a6b8bdcd0e813c66823b31e6e26f7 upstream. + +At some point the definitions for clamp() got added in the middle of the +ones for min() and max(). Re-order the definitions so they are more +sensibly grouped. + +Link: https://lkml.kernel.org/r/8bb285818e4846469121c8abc3dfb6e2@AcuMS.aculab.com +Signed-off-by: David Laight +Cc: Andy Shevchenko +Cc: Arnd Bergmann +Cc: Christoph Hellwig +Cc: Dan Carpenter +Cc: Jason A. Donenfeld +Cc: Jens Axboe +Cc: Lorenzo Stoakes +Cc: Mateusz Guzik +Cc: Matthew Wilcox +Cc: Pedro Falcato +Signed-off-by: Andrew Morton +Signed-off-by: Eliav Farber +Signed-off-by: Greg Kroah-Hartman +--- + include/linux/minmax.h | 109 ++++++++++++++++++++++--------------------------- + 1 file changed, 51 insertions(+), 58 deletions(-) + +--- a/include/linux/minmax.h ++++ b/include/linux/minmax.h +@@ -99,22 +99,6 @@ + #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, uval, ulo, uhi) ({ \ +- __auto_type uval = (val); \ +- __auto_type ulo = (lo); \ +- __auto_type uhi = (hi); \ +- BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ +- "clamp() low limit " #lo " greater than high limit " #hi); \ +- BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ +- "clamp("#val", "#lo", "#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 + * @x: first value +@@ -171,6 +155,22 @@ + __careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_)) + + /** ++ * min_t - return minimum of two values, using the specified type ++ * @type: data type to use ++ * @x: first value ++ * @y: second value ++ */ ++#define min_t(type, x, y) __cmp_once(min, type, x, y) ++ ++/** ++ * max_t - return maximum of two values, using the specified type ++ * @type: data type to use ++ * @x: first value ++ * @y: second value ++ */ ++#define max_t(type, x, y) __cmp_once(max, type, x, y) ++ ++/** + * min_not_zero - return the minimum that is _not_ zero, unless both are zero + * @x: value1 + * @y: value2 +@@ -180,6 +180,22 @@ + typeof(y) __y = (y); \ + __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) + ++#define __clamp(val, lo, hi) \ ++ ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) ++ ++#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ ++ __auto_type uval = (val); \ ++ __auto_type ulo = (lo); \ ++ __auto_type uhi = (hi); \ ++ BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ ++ "clamp() low limit " #lo " greater than high limit " #hi); \ ++ BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ ++ "clamp("#val", "#lo", "#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_)) ++ + /** + * clamp - return a value clamped to a given range with strict typechecking + * @val: current value +@@ -191,28 +207,30 @@ + */ + #define clamp(val, lo, hi) __careful_clamp(val, lo, hi) + +-/* +- * ..and if you can't take the strict +- * types, you can specify one yourself. +- * +- * Or not use min/max/clamp at all, of course. +- */ +- + /** +- * min_t - return minimum of two values, using the specified type +- * @type: data type to use +- * @x: first value +- * @y: second value ++ * clamp_t - return a value clamped to a given range using a given type ++ * @type: the type of variable to use ++ * @val: current value ++ * @lo: minimum allowable value ++ * @hi: maximum allowable value ++ * ++ * This macro does no typechecking and uses temporary variables of type ++ * @type to make all the comparisons. + */ +-#define min_t(type, x, y) __cmp_once(min, type, x, y) ++#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi)) + + /** +- * max_t - return maximum of two values, using the specified type +- * @type: data type to use +- * @x: first value +- * @y: second value ++ * clamp_val - return a value clamped to a given range using val's type ++ * @val: current value ++ * @lo: minimum allowable value ++ * @hi: maximum allowable value ++ * ++ * This macro does no typechecking and uses temporary variables of whatever ++ * type the input argument @val is. This is useful when @val is an unsigned ++ * type and @lo and @hi are literals that will otherwise be assigned a signed ++ * integer type. + */ +-#define max_t(type, x, y) __cmp_once(max, type, x, y) ++#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) + + /* + * Do not check the array parameter using __must_be_array(). +@@ -257,31 +275,6 @@ + */ + #define max_array(array, len) __minmax_array(max, array, len) + +-/** +- * clamp_t - return a value clamped to a given range using a given type +- * @type: the type of variable to use +- * @val: current value +- * @lo: minimum allowable value +- * @hi: maximum allowable value +- * +- * This macro does no typechecking and uses temporary variables of type +- * @type to make all the comparisons. +- */ +-#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 +- * @val: current value +- * @lo: minimum allowable value +- * @hi: maximum allowable value +- * +- * This macro does no typechecking and uses temporary variables of whatever +- * type the input argument @val is. This is useful when @val is an unsigned +- * type and @lo and @hi are literals that will otherwise be assigned a signed +- * integer type. +- */ +-#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) +- + static inline bool in_range64(u64 val, u64 start, u64 len) + { + return (val - start) < len; diff --git a/queue-6.12/minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch b/queue-6.12/minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch new file mode 100644 index 0000000000..f465a72653 --- /dev/null +++ b/queue-6.12/minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch @@ -0,0 +1,100 @@ +From stable+bounces-180918-greg=kroah.com@vger.kernel.org Mon Sep 22 12:32:22 2025 +From: Eliav Farber +Date: Mon, 22 Sep 2025 10:31:19 +0000 +Subject: minmax.h: reduce the #define expansion of min(), max() and clamp() +To: , , , , , +Cc: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato +Message-ID: <20250922103123.14538-4-farbere@amazon.com> + +From: David Laight + +[ Upstream commit b280bb27a9f7c91ddab730e1ad91a9c18a051f41 ] + +Since the test for signed values being non-negative only relies on +__builtion_constant_p() (not is_constexpr()) it can use the 'ux' variable +instead of the caller supplied expression. This means that the #define +parameters are only expanded twice. Once in the code and once quoted in +the error message. + +Link: https://lkml.kernel.org/r/051afc171806425da991908ed8688a98@AcuMS.aculab.com +Signed-off-by: David Laight +Cc: Andy Shevchenko +Cc: Arnd Bergmann +Cc: Christoph Hellwig +Cc: Dan Carpenter +Cc: Jason A. Donenfeld +Cc: Jens Axboe +Cc: Lorenzo Stoakes +Cc: Mateusz Guzik +Cc: Matthew Wilcox +Cc: Pedro Falcato +Signed-off-by: Andrew Morton +Signed-off-by: Eliav Farber +Signed-off-by: Greg Kroah-Hartman +--- + include/linux/minmax.h | 24 ++++++++++++------------ + 1 file changed, 12 insertions(+), 12 deletions(-) + +--- a/include/linux/minmax.h ++++ b/include/linux/minmax.h +@@ -46,10 +46,10 @@ + * comparison, and these expressions only need to be careful to not cause + * warnings for pointer use. + */ +-#define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux)) +-#define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4)) +-#define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \ +- __signed_type_use(x, ux) : __unsigned_type_use(x, ux)) ++#define __signed_type_use(ux) (2 + __is_nonneg(ux)) ++#define __unsigned_type_use(ux) (1 + 2 * (sizeof(ux) < 4)) ++#define __sign_use(ux) (is_signed_type(typeof(ux)) ? \ ++ __signed_type_use(ux) : __unsigned_type_use(ux)) + + /* + * Check whether a signed value is always non-negative. +@@ -71,13 +71,13 @@ + #else + #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)) + #endif +-#define __is_nonneg(x, ux) statically_true((__signed_type(ux))(x) >= 0) ++#define __is_nonneg(ux) statically_true((__signed_type(ux))(ux) >= 0) + +-#define __types_ok(x, y, ux, uy) \ +- (__sign_use(x, ux) & __sign_use(y, uy)) ++#define __types_ok(ux, uy) \ ++ (__sign_use(ux) & __sign_use(uy)) + +-#define __types_ok3(x, y, z, ux, uy, uz) \ +- (__sign_use(x, ux) & __sign_use(y, uy) & __sign_use(z, uz)) ++#define __types_ok3(ux, uy, uz) \ ++ (__sign_use(ux) & __sign_use(uy) & __sign_use(uz)) + + #define __cmp_op_min < + #define __cmp_op_max > +@@ -92,7 +92,7 @@ + + #define __careful_cmp_once(op, x, y, ux, uy) ({ \ + __auto_type ux = (x); __auto_type uy = (y); \ +- BUILD_BUG_ON_MSG(!__types_ok(x, y, ux, uy), \ ++ BUILD_BUG_ON_MSG(!__types_ok(ux, uy), \ + #op"("#x", "#y") signedness error"); \ + __cmp(op, ux, uy); }) + +@@ -109,7 +109,7 @@ + static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ + (lo) <= (hi), true), \ + "clamp() low limit " #lo " greater than high limit " #hi); \ +- BUILD_BUG_ON_MSG(!__types_ok3(val, lo, hi, uval, ulo, uhi), \ ++ BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ + "clamp("#val", "#lo", "#hi") signedness error"); \ + __clamp(uval, ulo, uhi); }) + +@@ -149,7 +149,7 @@ + + #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ + __auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\ +- BUILD_BUG_ON_MSG(!__types_ok3(x, y, z, ux, uy, uz), \ ++ BUILD_BUG_ON_MSG(!__types_ok3(ux, uy, uz), \ + #op"3("#x", "#y", "#z") signedness error"); \ + __cmp(op, ux, __cmp(op, uy, uz)); }) + diff --git a/queue-6.12/minmax.h-remove-some-defines-that-are-only-expanded-once.patch b/queue-6.12/minmax.h-remove-some-defines-that-are-only-expanded-once.patch new file mode 100644 index 0000000000..445ff93232 --- /dev/null +++ b/queue-6.12/minmax.h-remove-some-defines-that-are-only-expanded-once.patch @@ -0,0 +1,82 @@ +From stable+bounces-180922-greg=kroah.com@vger.kernel.org Mon Sep 22 12:33:29 2025 +From: Eliav Farber +Date: Mon, 22 Sep 2025 10:31:23 +0000 +Subject: minmax.h: remove some #defines that are only expanded once +To: , , , , , +Cc: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato +Message-ID: <20250922103123.14538-8-farbere@amazon.com> + +From: David Laight + +[ Upstream commit 2b97aaf74ed534fb838d09867d09a3ca5d795208 ] + +The bodies of __signed_type_use() and __unsigned_type_use() are much the +same size as their names - so put the bodies in the only line that expands +them. + +Similarly __signed_type() is defined separately for 64bit and then used +exactly once just below. + +Change the test for __signed_type from CONFIG_64BIT to one based on gcc +defined macros so that the code is valid if it gets used outside of a +kernel build. + +Link: https://lkml.kernel.org/r/9386d1ebb8974fbabbed2635160c3975@AcuMS.aculab.com +Signed-off-by: David Laight +Cc: Andy Shevchenko +Cc: Arnd Bergmann +Cc: Christoph Hellwig +Cc: Dan Carpenter +Cc: Jason A. Donenfeld +Cc: Jens Axboe +Cc: Lorenzo Stoakes +Cc: Mateusz Guzik +Cc: Matthew Wilcox +Cc: Pedro Falcato +Signed-off-by: Andrew Morton +Signed-off-by: Eliav Farber +Signed-off-by: Greg Kroah-Hartman +--- + include/linux/minmax.h | 14 ++++++-------- + 1 file changed, 6 insertions(+), 8 deletions(-) + +--- a/include/linux/minmax.h ++++ b/include/linux/minmax.h +@@ -46,10 +46,8 @@ + * comparison, and these expressions only need to be careful to not cause + * warnings for pointer use. + */ +-#define __signed_type_use(ux) (2 + __is_nonneg(ux)) +-#define __unsigned_type_use(ux) (1 + 2 * (sizeof(ux) < 4)) + #define __sign_use(ux) (is_signed_type(typeof(ux)) ? \ +- __signed_type_use(ux) : __unsigned_type_use(ux)) ++ (2 + __is_nonneg(ux)) : (1 + 2 * (sizeof(ux) < 4))) + + /* + * Check whether a signed value is always non-negative. +@@ -57,7 +55,7 @@ + * A cast is needed to avoid any warnings from values that aren't signed + * integer types (in which case the result doesn't matter). + * +- * On 64-bit any integer or pointer type can safely be cast to 'long'. ++ * On 64-bit any integer or pointer type can safely be cast to 'long long'. + * But on 32-bit we need to avoid warnings about casting pointers to integers + * of different sizes without truncating 64-bit values so 'long' or 'long long' + * must be used depending on the size of the value. +@@ -66,12 +64,12 @@ + * them, but we do not use s128 types in the kernel (we do use 'u128', + * but they are handled by the !is_signed_type() case). + */ +-#ifdef CONFIG_64BIT +- #define __signed_type(ux) long ++#if __SIZEOF_POINTER__ == __SIZEOF_LONG_LONG__ ++#define __is_nonneg(ux) statically_true((long long)(ux) >= 0) + #else +- #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)) ++#define __is_nonneg(ux) statically_true( \ ++ (typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)))(ux) >= 0) + #endif +-#define __is_nonneg(ux) statically_true((__signed_type(ux))(ux) >= 0) + + #define __types_ok(ux, uy) \ + (__sign_use(ux) & __sign_use(uy)) diff --git a/queue-6.12/minmax.h-simplify-the-variants-of-clamp.patch b/queue-6.12/minmax.h-simplify-the-variants-of-clamp.patch new file mode 100644 index 0000000000..aac18daf2b --- /dev/null +++ b/queue-6.12/minmax.h-simplify-the-variants-of-clamp.patch @@ -0,0 +1,97 @@ +From stable+bounces-180920-greg=kroah.com@vger.kernel.org Mon Sep 22 12:32:59 2025 +From: Eliav Farber +Date: Mon, 22 Sep 2025 10:31:22 +0000 +Subject: minmax.h: simplify the variants of clamp() +To: , , , , , +Cc: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato +Message-ID: <20250922103123.14538-7-farbere@amazon.com> + +From: David Laight + +[ Upstream commit 495bba17cdf95e9703af1b8ef773c55ef0dfe703 ] + +Always pass a 'type' through to __clamp_once(), pass '__auto_type' from +clamp() itself. + +The expansion of __types_ok3() is reasonable so it isn't worth the added +complexity of avoiding it when a fixed type is used for all three values. + +Link: https://lkml.kernel.org/r/8f69f4deac014f558bab186444bac2e8@AcuMS.aculab.com +Signed-off-by: David Laight +Cc: Andy Shevchenko +Cc: Arnd Bergmann +Cc: Christoph Hellwig +Cc: Dan Carpenter +Cc: Jason A. Donenfeld +Cc: Jens Axboe +Cc: Lorenzo Stoakes +Cc: Mateusz Guzik +Cc: Matthew Wilcox +Cc: Pedro Falcato +Signed-off-by: Andrew Morton +Signed-off-by: Eliav Farber +Signed-off-by: Greg Kroah-Hartman +--- + include/linux/minmax.h | 24 ++++++++++++------------ + 1 file changed, 12 insertions(+), 12 deletions(-) + +--- a/include/linux/minmax.h ++++ b/include/linux/minmax.h +@@ -183,29 +183,29 @@ + #define __clamp(val, lo, hi) \ + ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val))) + +-#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ +- __auto_type uval = (val); \ +- __auto_type ulo = (lo); \ +- __auto_type uhi = (hi); \ ++#define __clamp_once(type, val, lo, hi, uval, ulo, uhi) ({ \ ++ type uval = (val); \ ++ type ulo = (lo); \ ++ type uhi = (hi); \ + BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ + "clamp() low limit " #lo " greater than high limit " #hi); \ + BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ + "clamp("#val", "#lo", "#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_)) ++#define __careful_clamp(type, val, lo, hi) \ ++ __clamp_once(type, val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_)) + + /** +- * clamp - return a value clamped to a given range with strict typechecking ++ * clamp - return a value clamped to a given range with typechecking + * @val: current value + * @lo: lowest allowable value + * @hi: highest allowable value + * +- * This macro does strict typechecking of @lo/@hi to make sure they are of the +- * same type as @val. See the unnecessary pointer comparisons. ++ * This macro checks @val/@lo/@hi to make sure they have compatible ++ * signedness. + */ +-#define clamp(val, lo, hi) __careful_clamp(val, lo, hi) ++#define clamp(val, lo, hi) __careful_clamp(__auto_type, val, lo, hi) + + /** + * clamp_t - return a value clamped to a given range using a given type +@@ -217,7 +217,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) __careful_clamp((type)(val), (type)(lo), (type)(hi)) ++#define clamp_t(type, val, lo, hi) __careful_clamp(type, val, lo, hi) + + /** + * clamp_val - return a value clamped to a given range using val's type +@@ -230,7 +230,7 @@ + * type and @lo and @hi are literals that will otherwise be assigned a signed + * integer type. + */ +-#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) ++#define clamp_val(val, lo, hi) __careful_clamp(typeof(val), val, lo, hi) + + /* + * Do not check the array parameter using __must_be_array(). diff --git a/queue-6.12/minmax.h-update-some-comments.patch b/queue-6.12/minmax.h-update-some-comments.patch new file mode 100644 index 0000000000..ad4bd5ca2a --- /dev/null +++ b/queue-6.12/minmax.h-update-some-comments.patch @@ -0,0 +1,128 @@ +From stable+bounces-180917-greg=kroah.com@vger.kernel.org Mon Sep 22 12:32:17 2025 +From: Eliav Farber +Date: Mon, 22 Sep 2025 10:31:18 +0000 +Subject: minmax.h: update some comments +To: , , , , , +Cc: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato +Message-ID: <20250922103123.14538-3-farbere@amazon.com> + +From: David Laight + +[ Upstream commit 10666e99204818ef45c702469488353b5bb09ec7 ] + +- Change three to several. +- Remove the comment about retaining constant expressions, no longer true. +- Realign to nearer 80 columns and break on major punctiation. +- Add a leading comment to the block before __signed_type() and __is_nonneg() + Otherwise the block explaining the cast is a bit 'floating'. + Reword the rest of that comment to improve readability. + +Link: https://lkml.kernel.org/r/85b050c81c1d4076aeb91a6cded45fee@AcuMS.aculab.com +Signed-off-by: David Laight +Cc: Andy Shevchenko +Cc: Arnd Bergmann +Cc: Christoph Hellwig +Cc: Dan Carpenter +Cc: Jason A. Donenfeld +Cc: Jens Axboe +Cc: Lorenzo Stoakes +Cc: Mateusz Guzik +Cc: Matthew Wilcox +Cc: Pedro Falcato +Signed-off-by: Andrew Morton +Signed-off-by: Eliav Farber +Signed-off-by: Greg Kroah-Hartman +--- + include/linux/minmax.h | 61 ++++++++++++++++++++++--------------------------- + 1 file changed, 28 insertions(+), 33 deletions(-) + +--- a/include/linux/minmax.h ++++ b/include/linux/minmax.h +@@ -8,13 +8,10 @@ + #include + + /* +- * min()/max()/clamp() macros must accomplish three things: ++ * min()/max()/clamp() macros must accomplish several things: + * + * - Avoid multiple evaluations of the arguments (so side-effects like + * "x++" happen only once) when non-constant. +- * - Retain result as a constant expressions when called with only +- * constant expressions (to avoid tripping VLA warnings in stack +- * allocation usage). + * - Perform signed v unsigned type-checking (to generate compile + * errors instead of nasty runtime surprises). + * - Unsigned char/short are always promoted to signed int and can be +@@ -31,25 +28,23 @@ + * bit #0 set if ok for unsigned comparisons + * bit #1 set if ok for signed comparisons + * +- * In particular, statically non-negative signed integer +- * expressions are ok for both. ++ * In particular, statically non-negative signed integer expressions ++ * are ok for both. + * +- * NOTE! Unsigned types smaller than 'int' are implicitly +- * converted to 'int' in expressions, and are accepted for +- * signed conversions for now. This is debatable. +- * +- * Note that 'x' is the original expression, and 'ux' is +- * the unique variable that contains the value. +- * +- * We use 'ux' for pure type checking, and 'x' for when +- * we need to look at the value (but without evaluating +- * it for side effects! Careful to only ever evaluate it +- * with sizeof() or __builtin_constant_p() etc). +- * +- * Pointers end up being checked by the normal C type +- * rules at the actual comparison, and these expressions +- * only need to be careful to not cause warnings for +- * pointer use. ++ * NOTE! Unsigned types smaller than 'int' are implicitly converted to 'int' ++ * in expressions, and are accepted for signed conversions for now. ++ * This is debatable. ++ * ++ * Note that 'x' is the original expression, and 'ux' is the unique variable ++ * that contains the value. ++ * ++ * We use 'ux' for pure type checking, and 'x' for when we need to look at the ++ * value (but without evaluating it for side effects! ++ * Careful to only ever evaluate it with sizeof() or __builtin_constant_p() etc). ++ * ++ * Pointers end up being checked by the normal C type rules at the actual ++ * comparison, and these expressions only need to be careful to not cause ++ * warnings for pointer use. + */ + #define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux)) + #define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4)) +@@ -57,19 +52,19 @@ + __signed_type_use(x, ux) : __unsigned_type_use(x, ux)) + + /* +- * To avoid warnings about casting pointers to integers +- * of different sizes, we need that special sign type. ++ * Check whether a signed value is always non-negative. + * +- * On 64-bit we can just always use 'long', since any +- * integer or pointer type can just be cast to that. ++ * A cast is needed to avoid any warnings from values that aren't signed ++ * integer types (in which case the result doesn't matter). + * +- * This does not work for 128-bit signed integers since +- * the cast would truncate them, but we do not use s128 +- * types in the kernel (we do use 'u128', but they will +- * be handled by the !is_signed_type() case). +- * +- * NOTE! The cast is there only to avoid any warnings +- * from when values that aren't signed integer types. ++ * On 64-bit any integer or pointer type can safely be cast to 'long'. ++ * But on 32-bit we need to avoid warnings about casting pointers to integers ++ * of different sizes without truncating 64-bit values so 'long' or 'long long' ++ * must be used depending on the size of the value. ++ * ++ * This does not work for 128-bit signed integers since the cast would truncate ++ * them, but we do not use s128 types in the kernel (we do use 'u128', ++ * but they are handled by the !is_signed_type() case). + */ + #ifdef CONFIG_64BIT + #define __signed_type(ux) long diff --git a/queue-6.12/minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch b/queue-6.12/minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch new file mode 100644 index 0000000000..70b8dc2319 --- /dev/null +++ b/queue-6.12/minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch @@ -0,0 +1,47 @@ +From stable+bounces-180921-greg=kroah.com@vger.kernel.org Mon Sep 22 12:33:01 2025 +From: Eliav Farber +Date: Mon, 22 Sep 2025 10:31:20 +0000 +Subject: minmax.h: use BUILD_BUG_ON_MSG() for the lo < hi test in clamp() +To: , , , , , +Cc: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato +Message-ID: <20250922103123.14538-5-farbere@amazon.com> + +From: David Laight + +[ Upstream commit a5743f32baec4728711bbc01d6ac2b33d4c67040 ] + +Use BUILD_BUG_ON_MSG(statically_true(ulo > uhi), ...) for the sanity check +of the bounds in clamp(). Gives better error coverage and one less +expansion of the arguments. + +Link: https://lkml.kernel.org/r/34d53778977747f19cce2abb287bb3e6@AcuMS.aculab.com +Signed-off-by: David Laight +Cc: Andy Shevchenko +Cc: Arnd Bergmann +Cc: Christoph Hellwig +Cc: Dan Carpenter +Cc: Jason A. Donenfeld +Cc: Jens Axboe +Cc: Lorenzo Stoakes +Cc: Mateusz Guzik +Cc: Matthew Wilcox +Cc: Pedro Falcato +Signed-off-by: Andrew Morton +Signed-off-by: Eliav Farber +Signed-off-by: Greg Kroah-Hartman +--- + include/linux/minmax.h | 3 +-- + 1 file changed, 1 insertion(+), 2 deletions(-) + +--- a/include/linux/minmax.h ++++ b/include/linux/minmax.h +@@ -106,8 +106,7 @@ + __auto_type uval = (val); \ + __auto_type ulo = (lo); \ + __auto_type uhi = (hi); \ +- static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ +- (lo) <= (hi), true), \ ++ BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ + "clamp() low limit " #lo " greater than high limit " #hi); \ + BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ + "clamp("#val", "#lo", "#hi") signedness error"); \ diff --git a/queue-6.12/series b/queue-6.12/series index d3e54130d6..4809da4e73 100644 --- a/queue-6.12/series +++ b/queue-6.12/series @@ -97,3 +97,10 @@ xhci-dbc-decouple-endpoint-allocation-from-initialization.patch xhci-dbc-fix-full-dbc-transfer-ring-after-several-reconnects.patch rtc-pcf2127-fix-spi-command-byte-for-pcf2131-backport.patch x86-sev-guard-sev_evict_cache-with-config_amd_mem_encrypt.patch +minmax.h-add-whitespace-around-operators-and-after-commas.patch +minmax.h-update-some-comments.patch +minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch +minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch +minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch +minmax.h-simplify-the-variants-of-clamp.patch +minmax.h-remove-some-defines-that-are-only-expanded-once.patch -- 2.47.3