Skip to contentSkip to conversation

minmax: reduce compilation time

PATCH Superseded David Laight 8 participants 58 messages Jul 24, 2024 – Jul 30, 2024

Subsystems (12)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover minmax: reduce compilation time

The changes to minmax.h that changed the type check to a signedness check significantly increased the length of the expansion. In some cases it has also significantly increased compile type. This is particularly noticeable for nested expansions.

These changes reduce the expansions somewhat.
The biggest change is the last patch that directly implements min3() and max3() rather than using a nested expansion.

Further significant improvements can be made by removing the requirement that min(1,2) be 'constant enough' for an array size. Instead supporting MIN() and MAX() for constants only with a result that is valid for a static initialiser.
However that needs an initial change to the few files that have local versions of MIN() or MAX().

David Laight (7):
minmax: Put all the clamp() definitions together
minmax: Use _Static_assert() instead of static_assert() compiler.h: Add __if_constexpr(expr, if_const, if_not_const)
minmax: Simplify signedness check
minmax: Factor out the zero-extension logic from umin/umax.
minmax: Optimise _Static_assert() check in clamp().
minmax: minmax: Add __types_ok3() and optimise defines with 3
arguments

 include/linux/compiler.h |  65 +++++----------
 include/linux/minmax.h   | 176 ++++++++++++++++++++-------------------
 2 files changed, 113 insertions(+), 128 deletions(-)
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#1 lore raw

PATCH 1/7 minmax: Put all the clamp() definitions together

The defines for clamp() have got separated, move togther for readability. Update description of signedness check.

---
 include/linux/minmax.h | 120 +++++++++++++++++++----------------------
 1 file changed, 56 insertions(+), 64 deletions(-)
include/linux/minmax.h +56 -64
index 2ec559284a9f..63c45865b48a 100644
@@ -57,26 +57,6 @@
 		__cmp(op, x, y),				\
 		__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);						\
-	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))); })
-
 /**
  * min - return minimum of two values of the same or compatible types
  * @x: first value
@@ -124,6 +104,22 @@
  */
 #define max3(x, y, z) max((typeof(x))max(x, y), 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)	__careful_cmp(min, (type)(x), (type)(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)	__careful_cmp(max, (type)(x), (type)(y))
+
 /**
  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
  * @x: value1
@@ -134,39 +130,60 @@
 	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, unique_val, unique_lo, unique_hi) ({		\
+	typeof(val) unique_val = (val);						\
+	typeof(lo) unique_lo = (lo);						\
+	typeof(hi) unique_hi = (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))); })
+
 /**
  * clamp - return a value clamped to a given range with strict 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 that @val, @lo and @hi have the same signedness.
  */
 #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)	__careful_cmp(min, (type)(x), (type)(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)	__careful_cmp(max, (type)(x), (type)(y))
+#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
 
 /*
  * Do not check the array parameter using __must_be_array().
@@ -211,31 +228,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;
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#2 lore raw

PATCH 2/7 minmax: Use _Static_assert() instead of static_assert()

In reply to David Laight

The static_assert() wrapper provides the text of the expression as the error message, this isn't needed here as an explicit message is provided. If there is an error (quite likely for min/max) the wrapper also adds two more lines of error output that just make it harder to read.

Since it gives no benefit and actually makes things worse directly using _Static_assert() is much better.

---
 include/linux/minmax.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/minmax.h +4 -4
index 63c45865b48a..900eec7a28e5 100644
@@ -48,7 +48,7 @@
 #define __cmp_once(op, x, y, unique_x, unique_y) ({	\
 	typeof(x) unique_x = (x);			\
 	typeof(y) unique_y = (y);			\
-	static_assert(__types_ok(x, y),			\
+	_Static_assert(__types_ok(x, y),			\
 		#op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \
 	__cmp(op, unique_x, unique_y); })
 
@@ -137,11 +137,11 @@
 	typeof(val) unique_val = (val);						\
 	typeof(lo) unique_lo = (lo);						\
 	typeof(hi) unique_hi = (hi);						\
-	static_assert(__builtin_choose_expr(__is_constexpr((lo) > (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");	\
+	_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) ({					\
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#3 lore raw

PATCH 3/7 compiler.h: Add __if_constexpr(expr, if_const, if_not_const)

In reply to David Laight

__if_constexpr(expr, if_const, if_not_const) returns 'if_const' if 'expr' is a 'constant integer expression' otherwise 'if_not_const'. The two values may have different types.

Redefine __is_constextpr(expr) as __if_constexpr(expr, 1, 0).

Implemented using _Generic() for portibility.

Add proper kerndoc comments.

---
 include/linux/compiler.h | 65 +++++++++++++---------------------------
 1 file changed, 21 insertions(+), 44 deletions(-)
include/linux/compiler.h +21 -44
index 2594553bb30b..7d559e390011 100644
@@ -242,52 +242,29 @@ static inline void *offset_to_ptr(const int *off)
 /* &a[0] degrades to a pointer: a different type from an array */
 #define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
 
-/*
- * This returns a constant expression while determining if an argument is
- * a constant expression, most importantly without evaluating the argument.
- * Glory to Martin Uecker <[email protected]>
+/**
+ * __if_constexpr - Check whether an expression is an 'integer
+ *		constant expression' 
+ * @expr: Expression to test, not evaluated, can be a pointer
+ * @if_const: return value if constant
+ * @if_not_const: return value if not constant
+ *
+ * The return values @if_const and @if_not_const can have different types.
  *
- * Details:
- * - sizeof() return an integer constant expression, and does not evaluate
- *   the value of its operand; it only examines the type of its operand.
- * - The results of comparing two integer constant expressions is also
- *   an integer constant expression.
- * - The first literal "8" isn't important. It could be any literal value.
- * - The second literal "8" is to avoid warnings about unaligned pointers;
- *   this could otherwise just be "1".
- * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit
- *   architectures.
- * - The C Standard defines "null pointer constant", "(void *)0", as
- *   distinct from other void pointers.
- * - If (x) is an integer constant expression, then the "* 0l" resolves
- *   it into an integer constant expression of value 0. Since it is cast to
- *   "void *", this makes the second operand a null pointer constant.
- * - If (x) is not an integer constant expression, then the second operand
- *   resolves to a void pointer (but not a null pointer constant: the value
- *   is not an integer constant 0).
- * - The conditional operator's third operand, "(int *)8", is an object
- *   pointer (to type "int").
- * - The behavior (including the return type) of the conditional operator
- *   ("operand1 ? operand2 : operand3") depends on the kind of expressions
- *   given for the second and third operands. This is the central mechanism
- *   of the macro:
- *   - When one operand is a null pointer constant (i.e. when x is an integer
- *     constant expression) and the other is an object pointer (i.e. our
- *     third operand), the conditional operator returns the type of the
- *     object pointer operand (i.e. "int *"). Here, within the sizeof(), we
- *     would then get:
- *       sizeof(*((int *)(...))  == sizeof(int)  == 4
- *   - When one operand is a void pointer (i.e. when x is not an integer
- *     constant expression) and the other is an object pointer (i.e. our
- *     third operand), the conditional operator returns a "void *" type.
- *     Here, within the sizeof(), we would then get:
- *       sizeof(*((void *)(...)) == sizeof(void) == 1
- * - The equality comparison to "sizeof(int)" therefore depends on (x):
- *     sizeof(int) == sizeof(int)     (x) was a constant expression
- *     sizeof(int) != sizeof(void)    (x) was not a constant expression
+ * Relies on typeof(x ? NULL : ptr_type) being ptr_type and
+ * typeof(x ? (void *)y : ptr_type) being 'void *'.
+ */
+#define __if_constexpr(expr, if_const, if_not_const)		\
+	_Generic(0 ? ((void *)((long)(expr) * 0l)) : (char *)0,	\
+		char *: (if_const),				\
+		void *: (if_not_const))
+
+/**
+ * __is_constexpr - Return 1 for an 'integer constant expression'
+ *		0 otherwise.
+ * @expr: expression to check, not evaluated
  */
-#define __is_constexpr(x) \
-	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+#define __is_constexpr(expr) __if_constexpr((expr), 1, 0)
 
 /*
  * Whether 'type' is a signed type or an unsigned type. Supports scalar types,
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#4 lore raw

Re: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const)

include/linux/compiler.h static inline void *offset_to_ptr(const int *off) · line 267

Quoted 8 lines · David Laight
include/linux/compiler.h +6 -2
+#define __if_constexpr(expr, if_const, if_not_const)		\
+	_Generic(0 ? ((void *)((long)(expr) * 0l)) : (char *)0,	\
+		char *: (if_const),				\
+		void *: (if_not_const))
+
-#define __is_constexpr(x) \
-	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+#define __is_constexpr(expr) __if_constexpr((expr), 1, 0)

I don't immediately see anything wrong with this, but I'm still scared of any change to it, especially if this is meant to go straight into mainline.

Would it be possible to do patch 4/7 without the new __if_constexpr() and instead still using __builtin_choose_expr()?

Arnd

#5 lore raw

RE: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const)

From: Arnd Bergmann

Quoted 4 lines

Sent: 24 July 2024 18:32

On Wed, Jul 24, 2024, at 16:29, David Laight wrote:

Quoted 12 lines

include/linux/compiler.h static inline void *offset_to_ptr(const int *off) · line 267

Quoted 8 lines
include/linux/compiler.h +6 -2
+#define __if_constexpr(expr, if_const, if_not_const)		\
+	_Generic(0 ? ((void *)((long)(expr) * 0l)) : (char *)0,	\
+		char *: (if_const),				\
+		void *: (if_not_const))
+
-#define __is_constexpr(x) \
-	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+#define __is_constexpr(expr) __if_constexpr((expr), 1, 0)

I don't immediately see anything wrong with this, but I'm still scared of any change to it, especially if this is meant to go straight into mainline.

Well it would be -rc1 (or maybe -rc2).

Quoted 2 lines

Would it be possible to do patch 4/7 without the new __if_constexpr() and instead still using __builtin_choose_expr()?

The safer option would be to add __if_constexpr() but leave the change to __is_constexpr() for 'next'.

David

Quoted 2 lines

Arnd

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#6 lore raw

Re: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const)

In reply to David Laight
Quoted 2 lines · David Laight

Implemented using _Generic() for portibility.

I do think this is ultimately the clearer implementation, if only because the underlying trick it uses does very much depend on a very special oddity in the C type system.

So then using the type comparison with _Generic() is rather natural, and allows for picking other things than 0/1 as a result.

However, there is NO WAY this is a "portability" enhancement.

The original trick basically works on all ANSI C versions, while "_Generic()" is quite a modern thing (added in C11? Something like that)

Now, in the kernel we obviously already moved to -std=gnu11 a couple of years ago, so doing this looks reasonable.

But what does not look reasonable to me is claiming that it's a portability thing. Quite the opposite. It still requires the same ternary operator type trick, now it just requires _Generic() support in _addition_ to it.

Linus

#7 lore raw

RE: [PATCH 3/7] compiler.h: Add __if_constexpr(expr, if_const, if_not_const)

From: Linus Torvalds

Quoted 6 lines

Sent: 24 July 2024 20:49

Quoted 2 lines · David Laight

Implemented using _Generic() for portibility.

Quoted 8 lines

I do think this is ultimately the clearer implementation, if only because the underlying trick it uses does very much depend on a very special oddity in the C type system.

So then using the type comparison with _Generic() is rather natural, and allows for picking other things than 0/1 as a result.

However, there is NO WAY this is a "portability" enhancement.

The old code relied on sizeof(void) which is invalid C.

So this version can be used when that gcc extension isn't enabled. While the linux kernel rather assumes it other builds (eg nolibc) don't.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#8 lore raw

PATCH 4/7 minmax: Simplify signedness check

In reply to David Laight

It is enough to check that both 'x' and 'y' are valid for either a signed compare or an unsigned compare.
For unsigned they must be an unsigned type or a positive constant. For signed they must be signed after unsigned char/short are promoted.

Order the expressions to avoid warnings about comparisons that are always true.

---
 include/linux/minmax.h | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
include/linux/minmax.h +10 -12
index 900eec7a28e5..d3ac65c1add7 100644
@@ -8,7 +8,7 @@
 #include <linux/types.h>
 
 /*
- * 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.
@@ -26,19 +26,17 @@
 #define __typecheck(x, y) \
 	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
 
-/* is_signed_type() isn't a constexpr for pointer types */
-#define __is_signed(x) 								\
-	__builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))),	\
-		is_signed_type(typeof(x)), 0)
+/* Allow unsigned compares against non-negative signed constants. */
+#define __is_ok_unsigned(x) \
+	((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, (x) + 0, -1)) >= 0)
 
-/* True for a non-negative signed int constant */
-#define __is_noneg_int(x)	\
-	(__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0)
+/* Check for signed after promoting unsigned char/short to int */
+#define __is_ok_signed(x) is_signed_type(typeof((x) + 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))
+/* Allow if both x and y are valid for either signed or unsigned compares. */
+#define __types_ok(x, y)				\
+	((__is_ok_signed(x) && __is_ok_signed(y)) ||	\
+	 (__is_ok_unsigned(x) && __is_ok_unsigned(y)))
 
 #define __cmp_op_min <
 #define __cmp_op_max >
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#9 lore raw

Re: [PATCH 4/7] minmax: Simplify signedness check

Quoted 9 lines · David Laight

It is enough to check that both 'x' and 'y' are valid for either a signed compare or an unsigned compare.
For unsigned they must be an unsigned type or a positive constant. For signed they must be signed after unsigned char/short are promoted.

Order the expressions to avoid warnings about comparisons that are always true.

This patch gives me a 10x speedup on compiling arch/x86/xen/setup.c, taking it from 15 seconds to 1.5 seconds for a defconfig+CONFIG_XEN build.

include/linux/minmax.h line 31

Quoted 3 lines
include/linux/minmax.h +3 -0
+/* Allow unsigned compares against non-negative signed constants. */
+#define __is_ok_unsigned(x) \
+	((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, (x) + 0, -1)) >= 0)

I don't understand why this return '0' for unsigned types, shouldn't this be

((is_unsigned_type(typeof(x)) ? 1 : __if_constexpr(x, (x) + 0, -1)) >= 0)

?

Arnd

#10 lore raw

Re: [PATCH 4/7] minmax: Simplify signedness check

Quoted 5 lines · Arnd Bergmann

I don't understand why this return '0' for unsigned types, shouldn't this be

((is_unsigned_type(typeof(x)) ? 1 : __if_constexpr(x, (x) + 0, -1)) >= 0)

Yes, that looks more logical.

Plus why do that "__if_constexpr(x, (x) + 0, -1)) >= 0)" when it would appear to be more logical to move the comparison inside, ie

__if_constexpr(x, (x) >= 0, 0)

but I also don't see why that "+ 0" existed in the original. So there's presumably something I'm missing.

I do get the feeling that the problem came from us being much too clever with out min/max macros, and now this series is doubling down instead of saying "it wasn't really worth it".

Linus

#11 lore raw

RE: [PATCH 4/7] minmax: Simplify signedness check

From: Linus Torvalds

Quoted 10 lines

Sent: 24 July 2024 21:03

Quoted 5 lines · Arnd Bergmann

I don't understand why this return '0' for unsigned types, shouldn't this be

((is_unsigned_type(typeof(x)) ? 1 : __if_constexpr(x, (x) + 0, -1)) >= 0)

Yes, that looks more logical.

The condition is '>= 0' so it doesn't matter if it is '1' or '0'.

Quoted 4 lines

Plus why do that "__if_constexpr(x, (x) + 0, -1)) >= 0)" when it would appear to be more logical to move the comparison inside, ie

__if_constexpr(x, (x) >= 0, 0)

That gives a 'comparison of unsigned type against 0 is always true' warning. (The compiler generates that for code in the unused branches of both __builtin_choose_expr() and _Generic().)
Moving the comparison to the outer level stops all such compiler warnings.

Quoted 2 lines

but I also don't see why that "+ 0" existed in the original. So there's presumably something I'm missing.

IIRC it was there to convert a 'bool' to 'int'. Somewhere the is a max(bool,bool) that could just be |. If may not be needed now the expansion is '(cond ? 0 : bool) >= 0' since the 'bool' picks up an (int) cast from the result of ?:.

David

Quoted 5 lines

I do get the feeling that the problem came from us being much too clever with out min/max macros, and now this series is doubling down instead of saying "it wasn't really worth it".

Linus

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#12 lore raw

Re: [PATCH 4/7] minmax: Simplify signedness check

Quoted 2 lines · David Laight

The condition is '>= 0' so it doesn't matter if it is '1' or '0'.

Yes, but that's because the whole conditional is so inexplicably complex.

But the explanation is:

Quoted 4 lines

That gives a 'comparison of unsigned type against 0 is always true' warning. (The compiler generates that for code in the unused branches of both __builtin_choose_expr() and _Generic().)
Moving the comparison to the outer level stops all such compiler warnings.

Christ. This whole series is a nightmare of "add complexity to deal with stupid issues".

But the kernel test robot clearly found even more issues.

I think we need to just go back to the old code. It was stupid and limited and caused us to have to be more careful about types than was strictly necessary.

But it was also about a million times simpler, and didn't cause build time regressions.

Linus

#13 lore raw

Re: [PATCH 4/7] minmax: Simplify signedness check

Quoted 13 lines · Linus Torvalds
Quoted 2 lines · David Laight

The condition is '>= 0' so it doesn't matter if it is '1' or '0'.

Yes, but that's because the whole conditional is so inexplicably complex.

But the explanation is:

Quoted 4 lines

That gives a 'comparison of unsigned type against 0 is always true' warning. (The compiler generates that for code in the unused branches of both __builtin_choose_expr() and _Generic().)
Moving the comparison to the outer level stops all such compiler warnings.

Quoted 8 lines

Christ. This whole series is a nightmare of "add complexity to deal with stupid issues".

But the kernel test robot clearly found even more issues.

I think we need to just go back to the old code. It was stupid and limited and caused us to have to be more careful about types than was strictly necessary.

The problem is simply reverting reveals that seemingly a _ton_ of code has come to rely on the relaxed conditions.

When I went to gather the numbers for my initial report I had to manually fix up every case which was rather painful, and that was just a defconfig + a few extra options. allmodconfig is likely to be a hellscape.

I've not dug deep into the ins + outs of this, so forgive me for being vague (Arnd has a far clearer understanding) - but it seems that the majority of the complexity comes from having to absolutely ensure all this works for compile-time constant values.

Arnd had a look through and determined there weren't _too_ many cases where we need this (for instance array sizes).

So I wonder whether we can't just vastly simplify this stuff (and reducing the macro expansion hell) for the usual case, and implement something like cmin()/cmax() or whatever for the true-constant cases?

Quoted 4 lines

But it was also about a million times simpler, and didn't cause build time regressions.

:)

Quoted 1 line

Linus

#14 lore raw

RE: [PATCH 4/7] minmax: Simplify signedness check

From: Lorenzo Stoakes

Quoted 28 lines

Sent: 26 July 2024 10:44

Quoted 13 lines · Linus Torvalds
Quoted 2 lines · David Laight

The condition is '>= 0' so it doesn't matter if it is '1' or '0'.

Yes, but that's because the whole conditional is so inexplicably complex.

But the explanation is:

Quoted 4 lines

That gives a 'comparison of unsigned type against 0 is always true' warning. (The compiler generates that for code in the unused branches of both __builtin_choose_expr() and _Generic().)
Moving the comparison to the outer level stops all such compiler warnings.

Quoted 8 lines

Christ. This whole series is a nightmare of "add complexity to deal with stupid issues".

But the kernel test robot clearly found even more issues.

I think we need to just go back to the old code. It was stupid and limited and caused us to have to be more careful about types than was strictly necessary.

The problem is simply reverting reveals that seemingly a _ton_ of code has come to rely on the relaxed conditions.

Quoted 8 lines

When I went to gather the numbers for my initial report I had to manually fix up every case which was rather painful, and that was just a defconfig + a few extra options. allmodconfig is likely to be a hellscape.

I've not dug deep into the ins + outs of this, so forgive me for being vague (Arnd has a far clearer understanding) - but it seems that the majority of the complexity comes from having to absolutely ensure all this works for compile-time constant values.

The problems arise due to some odd uses, not just the requirement for compile-time constants for on-stack array sizes.

The test robot report is for a test between pointers.
I thought there was one in the build I do - and it doesn't usually generate a warning. This one is related to the different between a 'compile time constant' and a 'constant integer expression'.
This is due to is_unsigned_type(t) being (t)-1 > 0 but (type *)x not being 'constant enough' unless 'x' is zero.
Fixable by something like:

	(__if_constexpr((t)-1, (t)-1, 1) > 0)

But that requires two expansions of the type.
Now the type could be that of the unique temporary - but that would make it all more complicated.

There are fewer min/max of pointers than when constants are needed. So requiring them be min_ptr() wouldn't be a massive change.

Quoted 6 lines

Arnd had a look through and determined there weren't _too_ many cases where we need this (for instance array sizes).

So I wonder whether we can't just vastly simplify this stuff (and reducing the macro expansion hell) for the usual case, and implement something like cmin()/cmax() or whatever for the true-constant cases?

I did do that in a patch set from much earlier in the year. But Linus said they'd need to be MIN() and MAX() and that requires changes to a few places where those are already defined.

Quoted 2 lines
Quoted 2 lines

But it was also about a million times simpler, and didn't cause build time regressions.

Just bugs because people did min_t(short, 65536, 128) and didn't expect zero.

It has to be said that the chances of a min(negative_value, unsigned_constant) appearing are pretty slim.
All these tests are there to trap that case.

There is always the option of disabling the tests for 'normal' build, but leaving them there for (say) the W=1 builds.
Then it won't matter as much if the tests slow down the build a little.

I think I have tried a W=1 build - but there are too many warnings/errors from other places to get anywhere.
A lot are for 'unsigned_var >= 0' in paths that get optimised away. The compiler doesn't help!

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#15 lore raw

Re: [PATCH 4/7] minmax: Simplify signedness check

Quoted 4 lines · David Laight

From: Lorenzo Stoakes

Quoted 3 lines

Sent: 26 July 2024 10:44

On Thu, Jul 25, 2024 at 10:02:45AM GMT, Linus Torvalds wrote:

[snip]

Quoted 16 lines
Quoted 16 lines
Quoted 8 lines

Christ. This whole series is a nightmare of "add complexity to deal with stupid issues".

But the kernel test robot clearly found even more issues.

I think we need to just go back to the old code. It was stupid and limited and caused us to have to be more careful about types than was strictly necessary.

The problem is simply reverting reveals that seemingly a _ton_ of code has come to rely on the relaxed conditions.

When I went to gather the numbers for my initial report I had to manually fix up every case which was rather painful, and that was just a defconfig + a few extra options. allmodconfig is likely to be a hellscape.

Quoted 7 lines
Quoted 4 lines

I've not dug deep into the ins + outs of this, so forgive me for being vague (Arnd has a far clearer understanding) - but it seems that the majority of the complexity comes from having to absolutely ensure all this works for compile-time constant values.

The problems arise due to some odd uses, not just the requirement for compile-time constants for on-stack array sizes.

Odd implies not many, same argument applies.

[snip]

Quoted 11 lines
Quoted 7 lines

Arnd had a look through and determined there weren't _too_ many cases where we need this (for instance array sizes).

So I wonder whether we can't just vastly simplify this stuff (and reducing the macro expansion hell) for the usual case, and implement something like cmin()/cmax() or whatever for the true-constant cases?

I did do that in a patch set from much earlier in the year. But Linus said they'd need to be MIN() and MAX() and that requires changes to a few places where those are already defined.

OK, so what's stopping you from doing that?

In order to implement a MIN()/MAX() you'd need to change call sites only (should be a managable amount), so we can change this too?

I'm concerned that a solution is being proposed here and then handwaved away...

Unfortunately a revert is no longer possible (I had to fix up 33 call sites manually just for my defconfig build to compare perf before/after), so if the intent is to eliminate the complexity, then we need a practical way forward.

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

But it was also about a million times simpler, and didn't cause build time regressions.

Just bugs because people did min_t(short, 65536, 128) and didn't expect zero.

Quoted 7 lines

It has to be said that the chances of a min(negative_value, unsigned_constant) appearing are pretty slim.
All these tests are there to trap that case.

There is always the option of disabling the tests for 'normal' build, but leaving them there for (say) the W=1 builds.
Then it won't matter as much if the tests slow down the build a little.

Very much NAK disabling tests as a solution! Also leaving them for a build that's apparently broken... yeah not a fan.

Quoted 12 lines

I think I have tried a W=1 build - but there are too many warnings/errors from other places to get anywhere.
A lot are for 'unsigned_var >= 0' in paths that get optimised away. The compiler doesn't help!

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#16 lore raw

Re: [PATCH 4/7] minmax: Simplify signedness check

In reply to David Laight
kernel test robot noticed the following build errors:

Hi David,

kernel test robot noticed the following build errors:

[auto build test ERROR on linux/master]
[also build test ERROR on linus/master v6.10 next-20240725] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Laight/minmax-Put-all-the-clamp-definitions-together/20240724-224832
base:   linux/master
patch link:    https://lore.kernel.org/r/03601661326c4efba4e618ead15fa0e2%40AcuMS.aculab.com
patch subject: [PATCH 4/7] minmax: Simplify signedness check
config: mips-loongson1b_defconfig (https://download.01.org/0day-ci/archive/20240725/[email protected]/config)
compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project 8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240725/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> crypto/skcipher.c:83:9: error: static assertion expression is not an integral constant expression
           return max(start, end_page);
                  ^~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:70:19: note: expanded from macro 'max'
   #define max(x, y)       __careful_cmp(max, x, y)
                           ^~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:56:3: note: expanded from macro '__careful_cmp'
                   __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:49:17: note: expanded from macro '__cmp_once'
           _Static_assert(__types_ok(x, y),                        \
                          ^~~~~~~~~~~~~~~~
   include/linux/minmax.h:38:2: note: expanded from macro '__types_ok'
           ((__is_ok_signed(x) && __is_ok_signed(y)) ||    \
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   crypto/skcipher.c:83:9: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
   include/linux/minmax.h:70:19: note: expanded from macro 'max'
   #define max(x, y)       __careful_cmp(max, x, y)
                           ^
   include/linux/minmax.h:56:3: note: expanded from macro '__careful_cmp'
                   __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
                   ^
   include/linux/minmax.h:49:17: note: expanded from macro '__cmp_once'
           _Static_assert(__types_ok(x, y),                        \
                          ^
   include/linux/minmax.h:38:4: note: expanded from macro '__types_ok'
           ((__is_ok_signed(x) && __is_ok_signed(y)) ||    \
             ^
   include/linux/minmax.h:34:27: note: expanded from macro '__is_ok_signed'
   #define __is_ok_signed(x) is_signed_type(typeof((x) + 0))
                             ^
   include/linux/compiler.h:273:32: note: expanded from macro 'is_signed_type'
   #define is_signed_type(type) (((type)(-1)) < (__force type)1)
                                  ^
   1 error generated.
--
>> lib/lzo/lzo1x_compress.c:53:33: error: static assertion expression is not an integral constant expression
                           const unsigned char *limit = min(ip_end, ip + MAX_ZERO_RUN_LENGTH + 1);
                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:63:19: note: expanded from macro 'min'
   #define min(x, y)       __careful_cmp(min, x, y)
                           ^~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:56:3: note: expanded from macro '__careful_cmp'
                   __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:49:17: note: expanded from macro '__cmp_once'
           _Static_assert(__types_ok(x, y),                        \
                          ^~~~~~~~~~~~~~~~
   include/linux/minmax.h:38:2: note: expanded from macro '__types_ok'
           ((__is_ok_signed(x) && __is_ok_signed(y)) ||    \
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   lib/lzo/lzo1x_compress.c:53:33: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression
   include/linux/minmax.h:63:19: note: expanded from macro 'min'
   #define min(x, y)       __careful_cmp(min, x, y)
                           ^
   include/linux/minmax.h:56:3: note: expanded from macro '__careful_cmp'
                   __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
                   ^
   include/linux/minmax.h:49:17: note: expanded from macro '__cmp_once'
           _Static_assert(__types_ok(x, y),                        \
                          ^
   include/linux/minmax.h:38:4: note: expanded from macro '__types_ok'
           ((__is_ok_signed(x) && __is_ok_signed(y)) ||    \
             ^
   include/linux/minmax.h:34:27: note: expanded from macro '__is_ok_signed'
   #define __is_ok_signed(x) is_signed_type(typeof((x) + 0))
                             ^
   include/linux/compiler.h:273:32: note: expanded from macro 'is_signed_type'
   #define is_signed_type(type) (((type)(-1)) < (__force type)1)
                                  ^

1 error generated.

vim +83 crypto/skcipher.c

b286d8b1a690667 Herbert Xu 2016-11-22  75  
b286d8b1a690667 Herbert Xu 2016-11-22  76  /* Get a spot of the specified length that does not straddle a page.
b286d8b1a690667 Herbert Xu 2016-11-22  77   * The caller needs to ensure that there is enough space for this operation.
b286d8b1a690667 Herbert Xu 2016-11-22  78   */
b286d8b1a690667 Herbert Xu 2016-11-22  79  static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
b286d8b1a690667 Herbert Xu 2016-11-22  80  {
b286d8b1a690667 Herbert Xu 2016-11-22  81  	u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
b286d8b1a690667 Herbert Xu 2016-11-22  82  

b286d8b1a690667 Herbert Xu 2016-11-22 @83 return max(start, end_page);
b286d8b1a690667 Herbert Xu 2016-11-22 84 }
b286d8b1a690667 Herbert Xu 2016-11-22 85

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#17 lore raw

RE: [PATCH 4/7] minmax: Simplify signedness check

From: kernel test robot <[email protected]>

Quoted 30 lines

Sent: 25 July 2024 14:24

Hi David,

kernel test robot noticed the following build errors:

[auto build test ERROR on linux/master]
[also build test ERROR on linus/master v6.10 next-20240725] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url: https://github.com/intel-lab-lkp/linux/commits/David-Laight/minmax-Put-all-the-clamp-
definitions-together/20240724-224832

base:   linux/master
patch link:    https://lore.kernel.org/r/03601661326c4efba4e618ead15fa0e2%40AcuMS.aculab.com
patch subject: [PATCH 4/7] minmax: Simplify signedness check
config: mips-loongson1b_defconfig (https://download.01.org/0day-

ci/archive/20240725/[email protected]/config)
compiler: clang version 15.0.7 (https://github.com/llvm/llvm-project
8dfdcc7b7bf66834a761bd8de445840ef68e4d1a)
reproduce (this is a W=1 build): (https://download.01.org/0day-
ci/archive/20240725/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

...

Quoted 11 lines
b286d8b1a690667 Herbert Xu 2016-11-22  75
b286d8b1a690667 Herbert Xu 2016-11-22  76  /* Get a spot of the specified length that does not straddle a page.
b286d8b1a690667 Herbert Xu 2016-11-22  77   * The caller needs to ensure that there is enough space for this operation.
b286d8b1a690667 Herbert Xu 2016-11-22  78   */
b286d8b1a690667 Herbert Xu 2016-11-22  79  static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
b286d8b1a690667 Herbert Xu 2016-11-22  80  {
b286d8b1a690667 Herbert Xu 2016-11-22  81  	u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
b286d8b1a690667 Herbert Xu 2016-11-22  82

b286d8b1a690667 Herbert Xu 2016-11-22 @83 return max(start, end_page);
b286d8b1a690667 Herbert Xu 2016-11-22 84 }
b286d8b1a690667 Herbert Xu 2016-11-22 85

I thought this version supported that :-(
Certainly I've hit and fixed this before.
Using min/max() on pointers just makes it all more horrid.
The problem is that is_signed_type() isn't 'constant enough' for pointers.

Supporting pointers may require yet another (bloating) _is_constexpr() call. Or add a simpler min_ptr() that includes a 'pointer' check.

But this might only be reported by clang with W=1.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#18 lore raw

PATCH 5/7 minmax: Factor out the zero-extension logic from umin/umax

In reply to David Laight

The '+ 0u + 0ul + 0ull' to zero extend to 64bit on both 32bit and 64bit systems was replicated 4 times.
Factor out and then join up some 'not overlong' lines.

---
 include/linux/minmax.h | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
include/linux/minmax.h +10 -6
index d3ac65c1add7..e250957036a1 100644
@@ -28,7 +28,7 @@
 
 /* Allow unsigned compares against non-negative signed constants. */
 #define __is_ok_unsigned(x) \
-	((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, (x) + 0, -1)) >= 0)
+	((is_unsigned_type(typeof(x)) ? 0 : __if_constexpr(x, x, -1)) >= 0)
 
 /* Check for signed after promoting unsigned char/short to int */
 #define __is_ok_signed(x) is_signed_type(typeof((x) + 0))
@@ -69,22 +69,26 @@
  */
 #define max(x, y)	__careful_cmp(max, x, y)
 
+/*
+ * Zero extend a non-negative value to 64bits.
+ * Undefined for negative values.
+ * The extension to 64 bits is often optimised away.
+ */
+#define __zero_extend(x) ((x) + 0u + 0ul + 0ull)
+
 /**
  * umin - return minimum of two non-negative values
- *   Signed types are zero extended to match a larger unsigned type.
  * @x: first value
  * @y: second value
  */
-#define umin(x, y)	\
-	__careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
+#define umin(x, y)	__careful_cmp(min, __zero_extend(x), __zero_extend(y))
 
 /**
  * umax - return maximum of two non-negative values
  * @x: first value
  * @y: second value
  */
-#define umax(x, y)	\
-	__careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
+#define umax(x, y)	__careful_cmp(max, __zero_extend(x), __zero_extend(y))
 
 /**
  * min3 - return minimum of three values
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#19 lore raw

PATCH 6/7 minmax: Optimise _Static_assert() check in clamp()

In reply to David Laight

Use __if_constexpr() instead of __builtin_choose_expr(__is_constexpr()).

---
 include/linux/minmax.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
include/linux/minmax.h +1 -2
index e250957036a1..2fb63efbeb0e 100644
@@ -139,8 +139,7 @@
 	typeof(val) unique_val = (val);						\
 	typeof(lo) unique_lo = (lo);						\
 	typeof(hi) unique_hi = (hi);						\
-	_Static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)),	\
-			(lo) <= (hi), true),					\
+	_Static_assert(__if_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");	\
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#20 lore raw

PATCH 7/7 minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments

In reply to David Laight

min3() and max3() were added to optimise nested min(x, min(y, z)) sequences, bit only moved where the expansion was requiested.

Add a separate implementation for 3 argument calls. These are never required to generate constant expressiions to remove that logic.

---
 include/linux/minmax.h | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
include/linux/minmax.h +20 -5
index 2fb63efbeb0e..4bbc82c589cf 100644
@@ -38,6 +38,11 @@
 	((__is_ok_signed(x) && __is_ok_signed(y)) ||	\
 	 (__is_ok_unsigned(x) && __is_ok_unsigned(y)))
 
+/* Check three values for min3(), max3() and clamp() */
+#define __types_ok3(x, y, z)							\
+	((__is_ok_signed(x) && __is_ok_signed(y) && __is_ok_signed(z)) ||	\
+	 (__is_ok_unsigned(x) && __is_ok_unsigned(y) && __is_ok_unsigned(z)))
+
 #define __cmp_op_min <
 #define __cmp_op_max >
 
@@ -90,13 +95,24 @@
  */
 #define umax(x, y)	__careful_cmp(max, __zero_extend(x), __zero_extend(y))
 
+#define __cmp_once3(op, x, y, z, uniq) ({	\
+	typeof(x) __x_##uniq = (x);		\
+	typeof(x) __y_##uniq = (y);		\
+	typeof(x) __z_##uniq = (z);		\
+	__cmp(op, __cmp(op, __x_##uniq, __y_##uniq), __z_##uniq); })
+
+#define __careful_cmp3(op, x, y, z, uniq) ({				\
+	static_assert(__types_ok3(x, y, z),				\
+		#op "3(" #x ", " #y ", " #z ") signedness error");	\
+	__cmp_once3(op, x, y, z, uniq); })
+
 /**
  * min3 - return minimum of three values
  * @x: first value
  * @y: second value
  * @z: third value
  */
-#define min3(x, y, z) min((typeof(x))min(x, y), z)
+#define min3(x, y, z) __careful_cmp3(min, x, y, z, __COUNTER__)
 
 /**
  * max3 - return maximum of three values
@@ -104,7 +120,7 @@
  * @y: second value
  * @z: third value
  */
-#define max3(x, y, z) max((typeof(x))max(x, y), z)
+#define max3(x, y, z) __careful_cmp3(max, x, y, z, __COUNTER__)
 
 /**
  * min_t - return minimum of two values, using the specified type
@@ -139,10 +155,9 @@
 	typeof(val) unique_val = (val);						\
 	typeof(lo) unique_lo = (lo);						\
 	typeof(hi) unique_hi = (hi);						\
-	_Static_assert(__if_constexpr((lo) <= (hi), (lo) <= (hi), true),		\
+	_Static_assert(__if_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");	\
+	_Static_assert(__types_ok3(val, lo, hi), "clamp() signedness error");	\
 	__clamp(unique_val, unique_lo, unique_hi); })
 
 #define __careful_clamp(val, lo, hi) ({					\
Signature · 5 lines
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
#21 lore raw

Re: [PATCH 7/7] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments

Quoted 8 lines · David Laight

min3() and max3() were added to optimise nested min(x, min(y, z)) sequences, bit only moved where the expansion was requiested.

Add a separate implementation for 3 argument calls. These are never required to generate constant expressiions to remove that logic.

This brings another 3x improvement in the size of the expansion and build speed.

include/linux/minmax.h line 102

Quoted 5 lines
include/linux/minmax.h +5 -0
+#define __cmp_once3(op, x, y, z, uniq) ({	\
+	typeof(x) __x_##uniq = (x);		\
+	typeof(x) __y_##uniq = (y);		\
+	typeof(x) __z_##uniq = (z);		\
+	__cmp(op, __cmp(op, __x_##uniq, __y_##uniq), __z_##uniq); })

This still has a nested call to __cmp(), which makes the resulting expression bigger than necessary.

The three typeof(x) should be x/y/z, right? Using __auto_type would avoid the bug and also remove one more variable expansion.

Using another temporary variable, plus the use of __auto_type brings the example line from xen/setup.c down 750KB to 530KB, and the compile speed from 0.5s to 0.34s.

 #define __cmp_once3(op, x, y, z, uniq) ({      \
       __auto_type __x_##uniq = (x);           \
       __auto_type __y_##uniq = (y);           \
       __auto_type __z_##uniq = (z);           \
       __auto_type __xy##uniq = __cmp(op, __x_##uniq, __y_##uniq); \
       __cmp(op, __xy_##uniq, __z_##uniq); })

The __auto_type change can also be applied to the other typeof() in this file.

Arnd

#22 lore raw

RE: [PATCH 7/7] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments

From: Arnd Bergmann

Quoted 24 lines

Sent: 24 July 2024 18:04

Quoted 8 lines · David Laight

min3() and max3() were added to optimise nested min(x, min(y, z)) sequences, bit only moved where the expansion was requiested.

Add a separate implementation for 3 argument calls. These are never required to generate constant expressiions to remove that logic.

This brings another 3x improvement in the size of the expansion and build speed.

include/linux/minmax.h line 102

Quoted 5 lines
include/linux/minmax.h +5 -0
+#define __cmp_once3(op, x, y, z, uniq) ({	\
+	typeof(x) __x_##uniq = (x);		\
+	typeof(x) __y_##uniq = (y);		\
+	typeof(x) __z_##uniq = (z);		\
+	__cmp(op, __cmp(op, __x_##uniq, __y_##uniq), __z_##uniq); })

This still has a nested call to __cmp(), which makes the resulting expression bigger than necessary.

Quoted 1 line

The three typeof(x) should be x/y/z, right?

Ooops...

Quoted 2 lines

Using __auto_type
would avoid the bug and also remove one more variable expansion.

I'd thought that as well.
But hadn't looked up the syntax.

Quoted 11 lines

Using another temporary variable, plus the use of __auto_type brings the example line from xen/setup.c down 750KB to 530KB, and the compile speed from 0.5s to 0.34s.

 #define __cmp_once3(op, x, y, z, uniq) ({      \
       __auto_type __x_##uniq = (x);           \
       __auto_type __y_##uniq = (y);           \
       __auto_type __z_##uniq = (z);           \
       __auto_type __xy##uniq = __cmp(op, __x_##uniq, __y_##uniq); \
       __cmp(op, __xy_##uniq, __z_##uniq); })
Quoted 2 lines

The __auto_type change can also be applied to the other typeof() in this file.

True.

David

Quoted 2 lines

Arnd

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#23 lore raw
In reply to David Laight
Quoted 31 lines · David Laight

The changes to minmax.h that changed the type check to a signedness check significantly increased the length of the expansion. In some cases it has also significantly increased compile type. This is particularly noticeable for nested expansions.

These changes reduce the expansions somewhat.
The biggest change is the last patch that directly implements min3() and max3() rather than using a nested expansion.

Further significant improvements can be made by removing the requirement that min(1,2) be 'constant enough' for an array size. Instead supporting MIN() and MAX() for constants only with a result that is valid for a static initialiser.
However that needs an initial change to the few files that have local versions of MIN() or MAX().

David Laight (7):
minmax: Put all the clamp() definitions together
minmax: Use _Static_assert() instead of static_assert() compiler.h: Add __if_constexpr(expr, if_const, if_not_const)
minmax: Simplify signedness check
minmax: Factor out the zero-extension logic from umin/umax.
minmax: Optimise _Static_assert() check in clamp().
minmax: minmax: Add __types_ok3() and optimise defines with 3
arguments

 include/linux/compiler.h |  65 +++++----------
 include/linux/minmax.h   | 176 ++++++++++++++++++++-------------------
 2 files changed, 113 insertions(+), 128 deletions(-)
Quoted 7 lines

--
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

FWIW, I measured ~26.7s user time improvement with CONFIG_XEN set, leaving another ~15.9s vs. reverted case.

It reduces the time taken on the egregious arch/x86/xen/setup.c down to .7s which is comparable to the reverted case.

If CONFIG_XEN is not set, we see a ~32.5s user time improvement, leaving another ~15.8s vs. reverted case.

So this series definitely improves things, but there is still more that could be done.

I share Arnd's concerns about the need to be _very_ careful about very sensitive and complicated logic in these macros so we need to be very sure we're not breaking anything here before we proceed.

Be good to really kick the tyres if we can.

My rough numbers below, on a 32-core intel i9-14900KF box using defconfig + a small number of debug flags I use for compiler development:

## CONFIG_XEN enabled with original min()/max() impl

make 1639.84s user 95.25s system 2358% cpu 1:13.58 total make 1638.79s user 96.40s system 2366% cpu 1:13.33 total make 1635.00s user 96.44s system 2359% cpu 1:13.37 total

## CONFIG_XEN enabled with this patch series

make 1609.13s user 92.71s system 2321% cpu 1:13.30 total make 1611.94s user 92.45s system 2366% cpu 1:12.01 total make 1612.51s user 92.35s system 2368% cpu 1:11.99 total

## CONFIG_XEN enabled with original min()/max() changes reverted

make 1588.46s user 92.33s system 2430% cpu 1:09.16 total make 1598.57s user 93.49s system 2419% cpu 1:09.94 total make 1598.99s user 92.49s system 2419% cpu 1:09.91 total

## No CONFIG_XEN with original min/max() impl

make 1570.76s user 91.62s system 2365% cpu 1:10.28 total make 1573.93s user 92.74s system 2367% cpu 1:10.40 total make 1576.97s user 92.33s system 2363% cpu 1:10.62 total

## No CONFIG_XEN with this patch series

make 1539.13s user 88.16s system 2347% cpu 1:09.31 total make 1541.55s user 88.84s system 2355% cpu 1:09.22 total make 1543.44s user 89.76s system 2355% cpu 1:09.34 total

## No CONFIG_XEN with original min()/max() changes reverted

make 1524.97s user 89.84s system 2399% cpu 1:07.31 total make 1521.01s user 88.99s system 2391% cpu 1:07.32 total make 1530.75s user 89.65s system 2389% cpu 1:07.83 total

#24 lore raw
Quoted 3 lines · Lorenzo Stoakes

My rough numbers below, on a 32-core intel i9-14900KF box using defconfig + a small number of debug flags I use for compiler development:

Side note: I think better than "time it" would be to simply generate all the pre-processor files, and look at the size.

Because to a very close approximation, all the overhead from these things comes from the deep macro expansions.

And unlike timings, the pre-processor size is going to be (a) entirely repeatable, and (b) will also perhaps point out other worst-case scenarios. IOW, maybe there is something else that looks like that "min3()" horror, that can be more surgically fixed.

Linus

#25 lore raw
Quoted 12 lines · Linus Torvalds
Quoted 3 lines · Lorenzo Stoakes

My rough numbers below, on a 32-core intel i9-14900KF box using defconfig + a small number of debug flags I use for compiler development:

Side note: I think better than "time it" would be to simply generate all the pre-processor files, and look at the size.

Because to a very close approximation, all the overhead from these things comes from the deep macro expansions.

Quoted 6 lines

And unlike timings, the pre-processor size is going to be (a) entirely repeatable, and (b) will also perhaps point out other worst-case scenarios. IOW, maybe there is something else that looks like that "min3()" horror, that can be more surgically fixed.

Linus

Agreed, have generated these numbers:

## CONFIG_XEN not set

### Not Reverted

8,413,999,318 bytes

### With Laight's patch series applied

8,237,750,156 bytes (~2.1% improvement)

### Reverted

8,157,201,494 bytes (~3.1% improvement)

## CONFIG_XEN set

### Not Reverted

9,006,361,868 bytes

51,407,944 ./arch/x86/xen/setup.o.pre

### With Laight's patch series applied

8,781,003,140 bytes (~2.5% improvement)

4,819,999 ./arch/x86/xen/setup.o.pre (!)

### Reverted

8,699,892,262 bytes (~3.4% improvement)

5,447,539 ./arch/x86/xen/setup.o.pre

#26 lore raw
Quoted 2 lines · Lorenzo Stoakes

5,447,539 ./arch/x86/xen/setup.o.pre

Can you perhaps do some kind of "max expansion" on all the preprocessor files (you seem to have done it by changing the ".c.o" rule to just spit it out as "o.pre", which sounds fine).

For example, this trivial patch seems to fix the setup.c expansion by about an order of magnitude (ie 50M -> 5M).

Entirely untested, but looks ObviouslyCorrect(tm) to me.

Linus

Linus

 arch/x86/xen/setup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/xen/setup.c +3 -2
index a0c3e77e3d5b..806ddb2391d9 100644
@@ -690,6 +690,7 @@ char * __init xen_memory_setup(void)
 	struct xen_memory_map memmap;
 	unsigned long max_pages;
 	unsigned long extra_pages = 0;
+	unsigned long maxmem_pages;
 	int i;
 	int op;
 
@@ -761,8 +762,8 @@ char * __init xen_memory_setup(void)
 	 * Make sure we have no memory above max_pages, as this area
 	 * isn't handled by the p2m management.
 	 */
-	extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
-			   extra_pages, max_pages - max_pfn);
+	maxmem_pages = EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM));
+	extra_pages = min3(maxmem_pages, extra_pages, max_pages - max_pfn);
 	i = 0;
 	addr = xen_e820_table.entries[0].addr;
 	size = xen_e820_table.entries[0].size;
Reviewed-by
#27 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 2 lines · Lorenzo Stoakes

5,447,539 ./arch/x86/xen/setup.o.pre

Can you perhaps do some kind of "max expansion" on all the preprocessor files (you seem to have done it by changing the ".c.o" rule to just spit it out as "o.pre", which sounds fine).

Yeah I simply hacked in a gcc -E, ugly but effective, e.g. prepending cmd_cc_o_c in scripts/Makefile.build with:

cmd_cc_o_c = $(CC) $(c_flags) -E $< > [email protected]; \

(with credit to Arnd, who came up with a timing patch using a similar technique, I just super trivially adapted to do this instead).

I therefore generate a bunch of .pre files, + run this to get total numbers:

find . -iname '*.pre' -exec du -cb {} + | grep total$ | cut -f1 | paste -sd+ - | bc

So I have the numbers for all of the files. Did you want me to post the numbers somewhere, or were you after someting cleverer, not sure what you mean by "max expansion"?

Quoted 9 lines

For example, this trivial patch seems to fix the setup.c expansion by about an order of magnitude (ie 50M -> 5M).

Entirely untested, but looks ObviouslyCorrect(tm) to me.

Linus

Linus

Yeah this has a big impact, with just this patch we go from 51,407,944 to 4,351,747 locally for me, so match your results.

Patch looks obviously correct to me, I wonder if we should just apply that now and take out one of the worst offenders while we figure out how to fix the broader problem?

#28 lore raw
Quoted 37 lines · Lorenzo Stoakes
Quoted 8 lines · Linus Torvalds
Quoted 2 lines · Lorenzo Stoakes

5,447,539 ./arch/x86/xen/setup.o.pre

Can you perhaps do some kind of "max expansion" on all the preprocessor files (you seem to have done it by changing the ".c.o" rule to just spit it out as "o.pre", which sounds fine).

Yeah I simply hacked in a gcc -E, ugly but effective, e.g. prepending cmd_cc_o_c in scripts/Makefile.build with:

cmd_cc_o_c = $(CC) $(c_flags) -E $< > [email protected]; \

(with credit to Arnd, who came up with a timing patch using a similar technique, I just super trivially adapted to do this instead).

I therefore generate a bunch of .pre files, + run this to get total numbers:

find . -iname '*.pre' -exec du -cb {} + | grep total$ | cut -f1 | paste -sd+ - | bc

So I have the numbers for all of the files. Did you want me to post the numbers somewhere, or were you after someting cleverer, not sure what you mean by "max expansion"?

Quoted 9 lines

For example, this trivial patch seems to fix the setup.c expansion by about an order of magnitude (ie 50M -> 5M).

Entirely untested, but looks ObviouslyCorrect(tm) to me.

Linus

Linus

Quoted 8 lines

Yeah this has a big impact, with just this patch we go from 51,407,944 to 4,351,747 locally for me, so match your results.

Patch looks obviously correct to me, I wonder if we should just apply that now and take out one of the worst offenders while we figure out how to fix the broader problem?

A simple comparison pre-revert vs. post-revert gives some ideas for other low-hanging fruit:

389180./drivers/block/loop.o.pre 409741./net/ipv4/tcp_output.o.pre 462230./net/ipv4/tcp_input.o.pre 470714./mm/page_alloc.o.pre 570698./block/bio.o.pre 600206./net/ipv6/proc.o.pre 761333./arch/x86/kernel/cpu/cacheinfo.o.pre 1315265./drivers/gpu/drm/i915/display/intel_pmdemand.o.pre 1334256./mm/compaction.o.pre 45960405./arch/x86/xen/setup.o.pre

I might try an allmodconfig with David's patches to see if that pulls out any other really obvious cases.

#29 lore raw
Quoted 5 lines · Lorenzo Stoakes

A simple comparison pre-revert vs. post-revert gives some ideas for other low-hanging fruit:

1334256./mm/compaction.o.pre

LOL. At least some of that is because of 'pageblock_order', which expands to 2.5kB of text just because of this:

/*
* Huge pages are a constant size, but don't exceed the maximum allocation
* granularity.
*/
#define pageblock_order min_t(unsigned int, HUGETLB_PAGE_ORDER,
MAX_PAGE_ORDER)

I think the two arguments to "min_t" are literally "(21 - 12)" and "10", and it expands to 2.5kB.

So it _looks_ like "pageblock_order", and it *acts* like a simple compile-time constant, but our complex type-checking min() macro ends up making it horrible.

But no, that's not nearly the longest expansion. Writing a little script, and I get

Longest line is 85061 (253kB)

so we have a single expansion that is 253kB in size. And it comes from this:

                case ISOLATE_SUCCESS:
                        update_cached = false;
                        last_migrated_pfn = max(cc->zone->zone_start_pfn,
                                pageblock_start_pfn(cc->migrate_pfn - 1));

where that "max()" ends up interacting with "pageblock_start_pfn()", and that pageblock_start_pfn() thing is

  #define pageblock_nr_pages      (1UL << pageblock_order)
  #define pageblock_start_pfn(pfn)  ALIGN_DOWN((pfn), pageblock_nr_pages)

so once again it's "pageblock_order", it's just that it's now mixed in with "max()".

Now, fixing that, and you end up with

Longest line is 61861 (82kB)

so it's now "only" 82kB in size, and that actually comes from <linux/bio.h>, which has this:

   static inline unsigned bio_segments(struct bio *bio)
   {
   ...
        bio_for_each_segment(bv, bio, iter)
                segs++;

which looks very tame indeed, but it turns out that "bio_for_each_segment()" expands to 82kB of code.

Jens? Maybe time to look into this?

Linus

 arch/x86/xen/setup.c            | 5 +++--
 include/linux/minmax.h          | 7 +++++++
 include/linux/pageblock-flags.h | 4 ++--
 3 files changed, 12 insertions(+), 4 deletions(-)
arch/x86/xen/setup.c +3 -2
index a0c3e77e3d5b..806ddb2391d9 100644
@@ -690,6 +690,7 @@ char * __init xen_memory_setup(void)
 	struct xen_memory_map memmap;
 	unsigned long max_pages;
 	unsigned long extra_pages = 0;
+	unsigned long maxmem_pages;
 	int i;
 	int op;
 
@@ -761,8 +762,8 @@ char * __init xen_memory_setup(void)
 	 * Make sure we have no memory above max_pages, as this area
 	 * isn't handled by the p2m management.
 	 */
-	extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
-			   extra_pages, max_pages - max_pfn);
+	maxmem_pages = EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM));
+	extra_pages = min3(maxmem_pages, extra_pages, max_pages - max_pfn);
 	i = 0;
 	addr = xen_e820_table.entries[0].addr;
 	size = xen_e820_table.entries[0].size;
include/linux/minmax.h +7 -0
index 2ec559284a9f..5e0c02a87d08 100644
@@ -7,6 +7,13 @@
 #include <linux/const.h>
 #include <linux/types.h>
 
+/*
+ * Use these carefully: no type checking, and uses the arguments
+ * multiple times. Use for obvious constants only.
+ */
+#define CONST_MIN(a,b) ((a)<(b)?(a):(b))
+#define CONST_MAX(a,b) ((a)>(b)?(a):(b))
+
 /*
  * min()/max()/clamp() macros must accomplish three things:
  *
include/linux/pageblock-flags.h +2 -2
index 547e82cdc89a..ce1b7c4d57e2 100644
@@ -41,13 +41,13 @@ extern unsigned int pageblock_order;
  * Huge pages are a constant size, but don't exceed the maximum allocation
  * granularity.
  */
-#define pageblock_order		min_t(unsigned int, HUGETLB_PAGE_ORDER, MAX_PAGE_ORDER)
+#define pageblock_order	((unsigned int)CONST_MIN(HUGETLB_PAGE_ORDER, MAX_PAGE_ORDER))
 
 #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
 
 #elif defined(CONFIG_TRANSPARENT_HUGEPAGE)
 
-#define pageblock_order		min_t(unsigned int, HPAGE_PMD_ORDER, MAX_PAGE_ORDER)
+#define pageblock_order ((unsigned int)CONST_MIN(HPAGE_PMD_ORDER, MAX_PAGE_ORDER))
 
 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>

static void die(const char *reason)

{
	fprintf(stderr, "Fatal: %s\n", reason);
	exit(1);
}

static unsigned long find_line(const char *line, unsigned long len)

{
	unsigned long res = 0;
	do {
		res++;
		if (*line == '\n')
			break;
		line++;
	} while (--len);
	return res;
}

int main(int argc, char **argv)

{
	struct stat st;
	char *buf;
	unsigned long len;
	long longest = 0;
	char *line = "";
	int lineno = 0, i;

	if (fstat(0, &st)) die("stat");
	if (!S_ISREG(st.st_mode)) die("Not a regularfile");
	len = st.st_size;
	buf = malloc(len);
	if (!buf) die("malloc failed");
	if (read(0, buf, len) != len) die("read failed");

	for (i = 1; len; i++) {
		unsigned long linelen;
		linelen = find_line(buf, len);
		if (linelen > longest) {
			longest = linelen;
			line = buf;
			lineno = i;
		}
		buf += linelen;
		len -= linelen;
	}

	printf("Longest line is %d (%lukB)\n", lineno, (longest+512) / 1024);
	printf("   '%.*s'\n", (int) longest-1, line);
	return 0;
}
#30 lore raw
Quoted 13 lines · Linus Torvalds

Now, fixing that, and you end up with

Longest line is 61861 (82kB)

so it's now "only" 82kB in size, and that actually comes from <linux/bio.h>, which has this:

   static inline unsigned bio_segments(struct bio *bio)
   {
   ...
        bio_for_each_segment(bv, bio, iter)
                segs++;
Quoted 4 lines

which looks very tame indeed, but it turns out that "bio_for_each_segment()" expands to 82kB of code.

Jens? Maybe time to look into this?

Eek yes, that looks horrible. I'll take a look.

Signature · 1 line
Jens Axboe
#31 lore raw
In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

LOL. At least some of that is because of 'pageblock_order', which expands to 2.5kB of text [..]

Ok, I pushed out my two fixes - one for the xen/setup.c case, and another for the ridiculous 'pageblock_order' expansion.

They both looked obvious to me, and they definitely make for a notably smaller preprocessor noise load, but maybe I messed something up.

I didn't even look at what the issue was with the bio_for_each_segment() expansion, in the hope that Jens will make that one look better.

There are probably many other cases where we have these kinds of "tens of kB of macro expansion", I only looked at the xen case and the mm/compaction.c case based on Lorenzo's list of top offenders.

Linus

#32 lore raw
Quoted 3 lines · Linus Torvalds

I didn't even look at what the issue was with the bio_for_each_segment() expansion, in the hope that Jens will make that one look better.

I did take a quick look, pretty obviously bvec_iter_bvec() which makes it horrible, which came from Kent's immutable work quite a while ago. Not sure yet what to do about it, will spend some time on this next week.

Signature · 1 line
Jens Axboe
#33 lore raw
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Linus Torvalds

I didn't even look at what the issue was with the bio_for_each_segment() expansion, in the hope that Jens will make that one look better.

I did take a quick look, pretty obviously bvec_iter_bvec() which makes it horrible, which came from Kent's immutable work quite a while ago. Not sure yet what to do about it, will spend some time on this next week.

Maybe something like this, totally untested...

include/linux/bvec.h +9 -6
index f41c7f0ef91e..9ccccddadde2 100644
@@ -130,12 +130,15 @@ struct bvec_iter_all {
 	(mp_bvec_iter_page((bvec), (iter)) +			\
 	 mp_bvec_iter_page_idx((bvec), (iter)))
 
-#define bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec bvec_iter_bvec(struct bio_vec *bv,
+					    struct bvec_iter iter)
+{
+	return (struct bio_vec) {
+		.bv_page	= bvec_iter_page(bv, iter),
+		.bv_len		= bvec_iter_len(bv, iter),
+		.bv_offset	= bvec_iter_offset(bv, iter)
+	};
+}
 
 static inline bool bvec_iter_advance(const struct bio_vec *bv,
 		struct bvec_iter *iter, unsigned bytes)
Signature · 1 line
Jens Axboe
#34 lore raw
Quoted 41 lines · Jens Axboe
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Linus Torvalds

I didn't even look at what the issue was with the bio_for_each_segment() expansion, in the hope that Jens will make that one look better.

I did take a quick look, pretty obviously bvec_iter_bvec() which makes it horrible, which came from Kent's immutable work quite a while ago. Not sure yet what to do about it, will spend some time on this next week.

Maybe something like this, totally untested...

include/linux/bvec.h +9 -7
index f41c7f0ef91e..9ccccddadde2 100644
@@ -130,12 +130,15 @@ struct bvec_iter_all {
 	(mp_bvec_iter_page((bvec), (iter)) +			\
 	 mp_bvec_iter_page_idx((bvec), (iter)))
 
-#define bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec bvec_iter_bvec(struct bio_vec *bv,
+					    struct bvec_iter iter)
+{
+	return (struct bio_vec) {
+		.bv_page	= bvec_iter_page(bv, iter),
+		.bv_len		= bvec_iter_len(bv, iter),
+		.bv_offset	= bvec_iter_offset(bv, iter)
+	};
+}
 
 static inline bool bvec_iter_advance(const struct bio_vec *bv,
 		struct bvec_iter *iter, unsigned bytes)
 
--
Quoted 2 lines

Jens Axboe

I tried this patch, doesn't seem to make a huge difference, going from 3,958,564 bytes with longest line of 82 kB to 3,943,824 bytes with a longest line of 77kB.

It seems that the .bv_len = ... expansion is what's doing it, so I tried patching mp_bvec_iter_len() as well to do a silly ?: thing (sorry), which takes us down to 3,880,309 with longest line of 20kB.

This is starting to feel like whack-a-mole isn't it? I looked at the next longest line, which originates from include/linux/pid_namespace.h believe it or not where some compiler cleverness + a loop is resulting in _another_ combinatorial explosion.

Whacking that gets us down to 3,860,780 with longest line of 16kB and (credit to Willy for telling me about the make <file basename>.i thing):

awk '{print length}' mm/compaction.i | sort -n

Tells me we have... 18 lines >10k remaining in that file.

Obviously dropping from 82 kB to 16 kB max size is handy but I think all this points to the need for a fundamental fix for this (I vote for fundamental simplification of core min()/max() macros, keep something complicated around for true const cases, e.g. MIN()/MAX()).

Patch attached including Jens's change + mine.

----8<----
From 4303ddf7bd7c892f89c37d9b2cc4102860d8ef97 Mon Sep 17 00:00:00 2001
From: Lorenzo Stoakes <[email protected]>
Date: Sat, 27 Jul 2024 17:25:03 +0100
Subject: [PATCH] Various attempts at whack-a-mole.

---
 include/linux/bvec.h          | 26 +++++++++++++++++---------
 include/linux/pid_namespace.h |  8 ++++++--
 2 files changed, 23 insertions(+), 11 deletions(-)
include/linux/bvec.h +17 -9
index f41c7f0ef91e..567522aec2f9 100644
@@ -101,9 +101,14 @@ struct bvec_iter_all {
 #define mp_bvec_iter_page(bvec, iter)				\
 	(__bvec_iter_bvec((bvec), (iter))->bv_page)
 
-#define mp_bvec_iter_len(bvec, iter)				\
-	min((iter).bi_size,					\
-	    __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
+static inline unsigned mp_bvec_iter_len(const struct bio_vec *bvec,
+					struct bvec_iter iter)
+{
+	unsigned remains = __bvec_iter_bvec(bvec, iter)->bv_len -
+		iter.bi_bvec_done;
+
+	return remains < iter.bi_size ? remains : iter.bi_size;
+}
 
 #define mp_bvec_iter_offset(bvec, iter)				\
 	(__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
@@ -130,12 +135,15 @@ struct bvec_iter_all {
 	(mp_bvec_iter_page((bvec), (iter)) +			\
 	 mp_bvec_iter_page_idx((bvec), (iter)))
 
-#define bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec bvec_iter_bvec(struct bio_vec *bv,
+                                           struct bvec_iter iter)
+{
+       return (struct bio_vec) {
+               .bv_page        = bvec_iter_page(bv, iter),
+               .bv_len         = bvec_iter_len(bv, iter),
+               .bv_offset      = bvec_iter_offset(bv, iter)
+       };
+}
 
 static inline bool bvec_iter_advance(const struct bio_vec *bv,
 		struct bvec_iter *iter, unsigned bytes)
include/linux/pid_namespace.h +6 -2
index f9f9931e02d6..0c9642acc670 100644
@@ -60,8 +60,12 @@ static inline int pidns_memfd_noexec_scope(struct pid_namespace *ns)
 {
 	int scope = MEMFD_NOEXEC_SCOPE_EXEC;
 
-	for (; ns; ns = ns->parent)
-		scope = max(scope, READ_ONCE(ns->memfd_noexec_scope));
+	for (; ns; ns = ns->parent) {
+		int ns_scope = READ_ONCE(ns->memfd_noexec_scope);
+
+		if (ns_scope > scope)
+			scope = ns_scope;
+	}
 
 	return scope;
 }

--
2.45.2

#35 lore raw
Quoted 49 lines · Lorenzo Stoakes
Quoted 41 lines · Jens Axboe
Quoted 9 lines · Jens Axboe
Quoted 3 lines · Linus Torvalds

I didn't even look at what the issue was with the bio_for_each_segment() expansion, in the hope that Jens will make that one look better.

I did take a quick look, pretty obviously bvec_iter_bvec() which makes it horrible, which came from Kent's immutable work quite a while ago. Not sure yet what to do about it, will spend some time on this next week.

Maybe something like this, totally untested...

include/linux/bvec.h +9 -7
index f41c7f0ef91e..9ccccddadde2 100644
@@ -130,12 +130,15 @@ struct bvec_iter_all {
 	(mp_bvec_iter_page((bvec), (iter)) +			\
 	 mp_bvec_iter_page_idx((bvec), (iter)))
 
-#define bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec bvec_iter_bvec(struct bio_vec *bv,
+					    struct bvec_iter iter)
+{
+	return (struct bio_vec) {
+		.bv_page	= bvec_iter_page(bv, iter),
+		.bv_len		= bvec_iter_len(bv, iter),
+		.bv_offset	= bvec_iter_offset(bv, iter)
+	};
+}
 
 static inline bool bvec_iter_advance(const struct bio_vec *bv,
 		struct bvec_iter *iter, unsigned bytes)
 
--
Quoted 2 lines

Jens Axboe

I tried this patch, doesn't seem to make a huge difference, going from 3,958,564 bytes with longest line of 82 kB to 3,943,824 bytes with a longest line of 77kB.

Quoted 3 lines

It seems that the .bv_len = ... expansion is what's doing it, so I tried patching mp_bvec_iter_len() as well to do a silly ?: thing (sorry), which takes us down to 3,880,309 with longest line of 20kB.

Right, I did compile it after the fact and applied the same thing to mp_bvec_iter_len().

Quoted 4 lines

This is starting to feel like whack-a-mole isn't it? I looked at the next longest line, which originates from include/linux/pid_namespace.h believe it or not where some compiler cleverness + a loop is resulting in _another_ combinatorial explosion.

Oh it's certainly whack-a-mole, doesn't mean it's not worth doing for the low hanging stuff :-)

Quoted 1 line

Patch attached including Jens's change + mine.

bvec side matches what I have here, fwiw, except I also did mp_bvec_iter_len(). Didn't see big expansion there, but might as well keep them consistent.

Signature · 1 line
Jens Axboe
#36 lore raw
Quoted 1 line · Jens Axboe

On 7/27/24 10:31 AM, Lorenzo Stoakes wrote:

[snip]

Quoted 10 lines
Quoted 7 lines

I tried this patch, doesn't seem to make a huge difference, going from 3,958,564 bytes with longest line of 82 kB to 3,943,824 bytes with a longest line of 77kB.

It seems that the .bv_len = ... expansion is what's doing it, so I tried patching mp_bvec_iter_len() as well to do a silly ?: thing (sorry), which takes us down to 3,880,309 with longest line of 20kB.

Right, I did compile it after the fact and applied the same thing to mp_bvec_iter_len().

Ah cool :)

Quoted 8 lines
Quoted 5 lines

This is starting to feel like whack-a-mole isn't it? I looked at the next longest line, which originates from include/linux/pid_namespace.h believe it or not where some compiler cleverness + a loop is resulting in _another_ combinatorial explosion.

Oh it's certainly whack-a-mole, doesn't mean it's not worth doing for the low hanging stuff :-)

Yeah I'd say getting down to 20 or 16kB is probably worth it, hardly difficult.

I guess I resent the fact we're being forced to do this because of insanity in the macros.. but if it's obvious and not going to cause regressions in existing code then no harm.

Quoted 6 lines
Quoted 2 lines

Patch attached including Jens's change + mine.

bvec side matches what I have here, fwiw, except I also did mp_bvec_iter_len(). Didn't see big expansion there, but might as well keep them consistent.

Makes sense, am happy to give R-b tag for this patch if you want to put it forward? Not sure if Linus is minded to just pull something for this now?

If we're whack-a-moleing may as well sort the drivers I found in my allmodconfig too obviously as some hilariously low-hanging fruit there...

Quoted 4 lines

--
Jens Axboe

#37 lore raw
Quoted 8 lines · Lorenzo Stoakes
Quoted 5 lines
Quoted 1 line

Patch attached including Jens's change + mine.

bvec side matches what I have here, fwiw, except I also did mp_bvec_iter_len(). Didn't see big expansion there, but might as well keep them consistent.

Makes sense, am happy to give R-b tag for this patch if you want to put it forward? Not sure if Linus is minded to just pull something for this now?

Here's what I built (allmodconfig, gcc-14 and clang-20) and did a basic boot of. Was just going to shove it into the 6.12 queue once posted, but can go either way with this. Or Linus can just grab it and run with it, not super particular ;-)

Quoted 2 lines

If we're whack-a-moleing may as well sort the drivers I found in my allmodconfig too obviously as some hilariously low-hanging fruit there...

Oh I'm not, I got plenty of other things to tend to. Just got sucked into the block side as that looked nuts, wanted to make that wasn't insane.

include/linux/bvec.h +26 -15
index f41c7f0ef91e..1cb76a829b83 100644
@@ -101,9 +101,14 @@ struct bvec_iter_all {
 #define mp_bvec_iter_page(bvec, iter)				\
 	(__bvec_iter_bvec((bvec), (iter))->bv_page)
 
-#define mp_bvec_iter_len(bvec, iter)				\
-	min((iter).bi_size,					\
-	    __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
+static inline unsigned mp_bvec_iter_len(const struct bio_vec *bvec,
+					struct bvec_iter iter)
+{
+	unsigned remains = __bvec_iter_bvec(bvec, iter)->bv_len -
+				iter.bi_bvec_done;
+
+	return remains < iter.bi_size ? remains : iter.bi_size;
+}
 
 #define mp_bvec_iter_offset(bvec, iter)				\
 	(__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
@@ -111,12 +116,15 @@ struct bvec_iter_all {
 #define mp_bvec_iter_page_idx(bvec, iter)			\
 	(mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
 
-#define mp_bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= mp_bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= mp_bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= mp_bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec mp_bvec_iter_bvec(const struct bio_vec *bv,
+					       struct bvec_iter iter)
+{
+	return (struct bio_vec) {
+		.bv_page	= mp_bvec_iter_page(bv, iter),
+		.bv_len		= mp_bvec_iter_len(bv, iter),
+		.bv_offset	= mp_bvec_iter_offset(bv, iter)
+	};
+}
 
 /* For building single-page bvec in flight */
  #define bvec_iter_offset(bvec, iter)				\
@@ -130,12 +138,15 @@ struct bvec_iter_all {
 	(mp_bvec_iter_page((bvec), (iter)) +			\
 	 mp_bvec_iter_page_idx((bvec), (iter)))
 
-#define bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec bvec_iter_bvec(const struct bio_vec *bv,
+					    struct bvec_iter iter)
+{
+	return (struct bio_vec) {
+		.bv_page	= bvec_iter_page(bv, iter),
+		.bv_len		= bvec_iter_len(bv, iter),
+		.bv_offset	= bvec_iter_offset(bv, iter)
+	};
+}
 
 static inline bool bvec_iter_advance(const struct bio_vec *bv,
 		struct bvec_iter *iter, unsigned bytes)
Signature · 1 line
Jens Axboe
Reviewed-by
#38 lore raw
Quoted 7 lines · Jens Axboe
Quoted 6 lines · Lorenzo Stoakes
Quoted 5 lines
Quoted 1 line

Patch attached including Jens's change + mine.

bvec side matches what I have here, fwiw, except I also did mp_bvec_iter_len(). Didn't see big expansion there, but might as well keep them consistent.

Quoted 7 lines
Quoted 2 lines

Makes sense, am happy to give R-b tag for this patch if you want to put it forward? Not sure if Linus is minded to just pull something for this now?

Here's what I built (allmodconfig, gcc-14 and clang-20) and did a basic boot of. Was just going to shove it into the 6.12 queue once posted, but can go either way with this. Or Linus can just grab it and run with it, not super particular ;-)

Yeah looks good to me, either way have:

Quoted 8 lines
Quoted 3 lines

If we're whack-a-moleing may as well sort the drivers I found in my allmodconfig too obviously as some hilariously low-hanging fruit there...

Oh I'm not, I got plenty of other things to tend to. Just got sucked into the block side as that looked nuts, wanted to make that wasn't insane.

Fully understandable, and it is the weekend after all :)

As I am clearly a bit bored this w/e, I might take a look at the obvious bits there myself if Linus hasn't already patched...

Have an allmodconfig build going now.

Quoted 72 lines
include/linux/bvec.h +26 -16
index f41c7f0ef91e..1cb76a829b83 100644
@@ -101,9 +101,14 @@ struct bvec_iter_all {
 #define mp_bvec_iter_page(bvec, iter)				\
 	(__bvec_iter_bvec((bvec), (iter))->bv_page)
 
-#define mp_bvec_iter_len(bvec, iter)				\
-	min((iter).bi_size,					\
-	    __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
+static inline unsigned mp_bvec_iter_len(const struct bio_vec *bvec,
+					struct bvec_iter iter)
+{
+	unsigned remains = __bvec_iter_bvec(bvec, iter)->bv_len -
+				iter.bi_bvec_done;
+
+	return remains < iter.bi_size ? remains : iter.bi_size;
+}
 
 #define mp_bvec_iter_offset(bvec, iter)				\
 	(__bvec_iter_bvec((bvec), (iter))->bv_offset + (iter).bi_bvec_done)
@@ -111,12 +116,15 @@ struct bvec_iter_all {
 #define mp_bvec_iter_page_idx(bvec, iter)			\
 	(mp_bvec_iter_offset((bvec), (iter)) / PAGE_SIZE)
 
-#define mp_bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= mp_bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= mp_bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= mp_bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec mp_bvec_iter_bvec(const struct bio_vec *bv,
+					       struct bvec_iter iter)
+{
+	return (struct bio_vec) {
+		.bv_page	= mp_bvec_iter_page(bv, iter),
+		.bv_len		= mp_bvec_iter_len(bv, iter),
+		.bv_offset	= mp_bvec_iter_offset(bv, iter)
+	};
+}
 
 /* For building single-page bvec in flight */
  #define bvec_iter_offset(bvec, iter)				\
@@ -130,12 +138,15 @@ struct bvec_iter_all {
 	(mp_bvec_iter_page((bvec), (iter)) +			\
 	 mp_bvec_iter_page_idx((bvec), (iter)))
 
-#define bvec_iter_bvec(bvec, iter)				\
-((struct bio_vec) {						\
-	.bv_page	= bvec_iter_page((bvec), (iter)),	\
-	.bv_len		= bvec_iter_len((bvec), (iter)),	\
-	.bv_offset	= bvec_iter_offset((bvec), (iter)),	\
-})
+static inline struct bio_vec bvec_iter_bvec(const struct bio_vec *bv,
+					    struct bvec_iter iter)
+{
+	return (struct bio_vec) {
+		.bv_page	= bvec_iter_page(bv, iter),
+		.bv_len		= bvec_iter_len(bv, iter),
+		.bv_offset	= bvec_iter_offset(bv, iter)
+	};
+}
 
 static inline bool bvec_iter_advance(const struct bio_vec *bv,
 		struct bvec_iter *iter, unsigned bytes)
 
--

Jens Axboe

#39 lore raw
In reply to Lorenzo Stoakes

From: Lorenzo Stoakes

Quoted 2 lines

Sent: 27 July 2024 17:31

...

Quoted 8 lines

I tried this patch, doesn't seem to make a huge difference, going from 3,958,564 bytes with longest line of 82 kB to 3,943,824 bytes with a longest line of 77kB.

It seems that the .bv_len = ... expansion is what's doing it, so I tried patching mp_bvec_iter_len() as well to do a silly ?: thing (sorry), which takes us down to 3,880,309 with longest line of 20kB.

...

Quoted 9 lines
include/linux/bvec.h +0 -1
index f41c7f0ef91e..567522aec2f9 100644
@@ -101,9 +101,14 @@ struct bvec_iter_all {
 #define mp_bvec_iter_page(bvec, iter)				\
 	(__bvec_iter_bvec((bvec), (iter))->bv_page)
 
-#define mp_bvec_iter_len(bvec, iter)				\

include/linux/bvec.h struct bvec_iter_all { · line 111

Quoted 10 lines
include/linux/bvec.h +8 -2
-	min((iter).bi_size,					\
-	    __bvec_iter_bvec((bvec), (iter))->bv_len - (iter).bi_bvec_done)
+static inline unsigned mp_bvec_iter_len(const struct bio_vec *bvec,
+					struct bvec_iter iter)
+{
+	unsigned remains = __bvec_iter_bvec(bvec, iter)->bv_len -
+		iter.bi_bvec_done;
+
+	return remains < iter.bi_size ? remains : iter.bi_size;
+}

That can still be a #define and still use min().
The important thing is to assign the result of __bvec_iter_bvec() to a local variable.

So maybe something like:

#define mp_bvec_iter_len(bvec, iter) ({ \
	__auto_type _remains = __bvec_iter_bvec((bvec), \
		(iter))->bv_len - (iter).bi_bvec_done); \
	min((iter).bi_size, _remains); \
})

Bloat is always going to happen if you pass one complex #define to another.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#40 lore raw
In reply to Linus Torvalds
Quoted 5 lines · Linus Torvalds

Now, fixing that, and you end up with

Longest line is 61861 (82kB)

Quoted 8 lines

so it's now "only" 82kB in size, and that actually comes from <linux/bio.h>, which has this:

   static inline unsigned bio_segments(struct bio *bio)
   {
   ...
        bio_for_each_segment(bv, bio, iter)
                segs++;

Ok, so I was playing around with this some more, and got really fed up with manually matching up where the longest line actually came from in the preprocessor output, so I updated my stupid "longest-line.c" program to just track the original file and line number as it was walking through the preprocessor file.

And yes, I realize that nobody sane would do this in C, and this all should be done in some sane language that is actually meant for string parsing, and me writing scripts in C shows that there's something seriously wrong with me.

I'm aware.

But anyway, it works. At least to some degree.

So I can do things like this:

  $ make drivers/net/ethernet/marvell/mvpp2/mvpp2_main.i
  $ ~/longest-line drivers/net/ethernet/marvell/mvpp2/mvpp2_main.i

and it will spit out

Longest line is drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:1136 (2346kB)

     '   ((((((pkt_size) + __builtin_choose_expr((sizeof(int) ==

sizeof(*(8 ? ((void *)((long)((__builtin_...'

to tell me that we have that insane 2.2 *megabyte* line due to the MVPP2_SKB_HEADROOM thing, and I should apply this patch:

-#define MVPP2_SKB_HEADROOM min(max(XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)
+#define MVPP2_SKB_HEADROOM MIN_T(int,MAX_T(int,XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)

to fix it.

At which point the above incantation starts giving Jens the evil eye again:

Longest line is include/linux/bio.h:194 (82kB)

     ' for (iter = ((bio)->bi_iter); (iter).bi_size && ((bv = ((struct

bio_vec) { .bv_page = (((&((((((bio...'

but while that is certainly an impressive 82kB line, we have some good company in code VM header files, and I've also seen

Longest line is include/linux/page-flags.h:507 (27kB)

     'static inline __attribute__((__gnu_inline__))

__attribute__((__unused__)) __attribute__((no_instrume...'

because the expansion from

__PAGEFLAG(Locked, locked, PF_NO_TAIL)

does indeed generate some impressive stuff. It's all the functions for the locked bit handling generated from one line.

But my C scripting may be buggy.

Anyway, I'm throwing this out in the hopes that somebody will use this to go "look, file XYZ generates a ridiculous X-megabyte line".

I found that 2.2MB expansion in mvpp2_main.c by basically just randomly grepping for nested min/max use. There may be worse things hiding.

Linus

#41 lore raw
Quoted 3 lines · Linus Torvalds

Anyway, I'm throwing this out in the hopes that somebody will use this to go "look, file XYZ generates a ridiculous X-megabyte line".

Oh, and by "throwing this out" I meant to actually include the updated "longest-line.c".

And somebody sane can write it in two lines of perl instead.

Linus

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

static void die(const char *reason)

{
	fprintf(stderr, "Fatal: %s\n", reason);
	exit(1);
}

static unsigned long find_line(const char *line, unsigned long len)

{
	unsigned long res = 0;
	do {
		res++;
		if (*line == '\n')
			break;
		line++;
	} while (--len);
	return res;
}

/*
* By this time, we know the line length, and it has been
* NUL-terminated. We can change it and use the underlying
* data if we find a preprocessor linemarker.
*/
void track_cpp_line_number(char *line, unsigned long len,
int *lineno, const char **filename)

{
	unsigned long num;
	char *end;

	if (line[0] != '#')
		return;
	if (line[1] != ' ')
		return;

	line += 2;
	len -= 2;

	num = strtoul(line, &end, 10);
	if (!num)
		return;
	if (end == line)
		return;

	len -= end - line;
	line = end;

	// Remove flag at the end
	if (len > 2) {
		if (line[len-2] == ' ' && isdigit(line[len-1])) {
			line[len-2] = 0;
			len -= 2;
		}			
	}

	// Remove spaces from end
	while (len > 1 && line[len-1] == ' ')
		line[--len] = 0;

	// We're done with 'len' now, don't bother */
	while (*line == ' ')
		line++;

	if (*line == '"') {
		end = strchr(++line, '"');
		if (end)
			*end = 0;
	}
	while (!strncmp(line, "./", 2))
		line += 2;

	*lineno = num-1;
	if (*line)
		*filename = line;
}

int main(int argc, char **argv)

{
	struct stat st;
	char *buf;
	unsigned long size;
	long longest_len = 0;
	char *longest_line = "";
	int longest_lineno = 0;
	const char *longest_file = "";
	const char *file;
	const char *unit;
	int lineno = 0, fd;

	fd = 0;
	file = "stdin";
	if (argc > 1) {
		file = argv[1];
		fd = open(file, O_RDONLY);
		if (fd < 0) die("open");
	}

	if (fstat(fd, &st)) die("stat");
	if (!S_ISREG(st.st_mode)) die("Not a regularfile");
	size = st.st_size;
	buf = malloc(size+1);
	if (!buf) die("malloc failed");
	if (read(fd, buf, size) != size) die("read failed");

	for (lineno = 1; size; lineno++) {
		unsigned long len;
		char *line = buf;

		/* Find the line, NUL-terminate it */
		len = find_line(buf, size);
		buf[len-1] = 0;

		buf += len;
		size -= len;

		/* Track preprocessor info */
		track_cpp_line_number(line, len-1, &lineno, &file);

		if (len <= longest_len)
			continue;

		longest_len = len;
		longest_line = line;
		longest_file = file;
		longest_lineno = lineno;
	}

	/* Limit printout of longest line arbitrarily */
	if (longest_len > 104)
		memcpy(longest_line + 100, "...", 4);

	unit = " bytes";
	if (longest_len > 10*1024) {
		unit = "kB";
		longest_len = (longest_len+512) / 1024;
	}

	printf("Longest line is %s:%d (%lu%s)\n",
		longest_file,
		longest_lineno,
		longest_len, unit);
	printf("   '%s'\n", longest_line);
	return 0;
}
#42 lore raw
In reply to Linus Torvalds

...

Quoted 13 lines

and it will spit out

Longest line is drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:1136 (2346kB)

     '   ((((((pkt_size) + __builtin_choose_expr((sizeof(int) ==

sizeof(*(8 ? ((void *)((long)((__builtin_...'

to tell me that we have that insane 2.2 *megabyte* line due to the MVPP2_SKB_HEADROOM thing, and I should apply this patch:

(quoted diff) +1 -1
-#define MVPP2_SKB_HEADROOM     min(max(XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)
+#define MVPP2_SKB_HEADROOM

MIN_T(int,MAX_T(int,XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)

Quoted 1 line

to fix it.

Or (if I've got is right):
#define MVPP2_SKB_HEADROOM clamp(XDP_PACKET_HEADROOM, NET_SKB_PAD, 224)

Hmmm...
I've found:
#define XDP_PACKET_HEADROOM 256
#define NET_SKB_PAD max(32, L1_CACHE_BYTES)
I'd bet that some architecture even has a non-constant L1_CACHE_BYTES.

But the 256 means the headroom is always 224 (whatever that limit is related to).

It is definitely worth freeing up MIN() and MAX() for:

#define MIN(x, y) ( \
	BUILD_BUG_ON_ZERO(__is_constexpr((x) + (y)) + ((x) < (y) ? (x) : (y)))

which is then usable for static initialisers.
Just assuming that no one is silly enough to get a negative constant compared to an unsigned value.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#43 lore raw
Quoted 7 lines · David Laight

...

Quoted 6 lines

and it will spit out

Longest line is drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:1136 (2346kB)

     '   ((((((pkt_size) + __builtin_choose_expr((sizeof(int) ==

sizeof(*(8 ? ((void *)((long)((__builtin_...'

Quoted 8 lines
Quoted 8 lines

to tell me that we have that insane 2.2 *megabyte* line due to the MVPP2_SKB_HEADROOM thing, and I should apply this patch:

(quoted diff) +1 -1
-#define MVPP2_SKB_HEADROOM     min(max(XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)
+#define MVPP2_SKB_HEADROOM

MIN_T(int,MAX_T(int,XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)

to fix it.

Yeah sorry just saw you had already addresed this Linus... I just went with a clamp()_t in my patch.

Quoted 4 lines

Or (if I've got is right):
#define MVPP2_SKB_HEADROOM clamp(XDP_PACKET_HEADROOM, NET_SKB_PAD, 224)

I'm pretty sure you can clamp_t(int, ...) here safely based on usage.

Part of the expansion is because of NET_SKB_PAD as you mention below...

Quoted 5 lines

Hmmm...
I've found:
#define XDP_PACKET_HEADROOM 256
#define NET_SKB_PAD max(32, L1_CACHE_BYTES)
I'd bet that some architecture even has a non-constant L1_CACHE_BYTES.

I checked and unless I missed something, nope.

You'll see in my proposed patch that I jut replace this with a dumb #if as a result.

Quoted 15 lines

But the 256 means the headroom is always 224 (whatever that limit is related to).

It is definitely worth freeing up MIN() and MAX() for:

#define MIN(x, y) ( \
	BUILD_BUG_ON_ZERO(__is_constexpr((x) + (y)) + ((x) < (y) ? (x) : (y)))

which is then usable for static initialisers.
Just assuming that no one is silly enough to get a negative constant compared to an unsigned value.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#44 lore raw
Quoted 7 lines · Lorenzo Stoakes
Quoted 4 lines
Quoted 4 lines
(quoted diff) +1 -1
-#define MVPP2_SKB_HEADROOM     min(max(XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)
+#define MVPP2_SKB_HEADROOM

MIN_T(int,MAX_T(int,XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)

Yeah sorry just saw you had already addresed this Linus... I just went with a clamp()_t in my patch.

I think your patch is better. I threw away my disgusting MIN_T() thing here. It made more sense in pageblock-flags.h where it replaced "min_t()".

Linus

#45 lore raw
In reply to Lorenzo Stoakes

From: Lorenzo Stoakes

Quoted 23 lines

Sent: 27 July 2024 19:59

Quoted 7 lines · David Laight

...

Quoted 6 lines

and it will spit out

Longest line is drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:1136 (2346kB)

     '   ((((((pkt_size) + __builtin_choose_expr((sizeof(int) ==

sizeof(*(8 ? ((void *)((long)((__builtin_...'

Quoted 8 lines
Quoted 8 lines

to tell me that we have that insane 2.2 *megabyte* line due to the MVPP2_SKB_HEADROOM thing, and I should apply this patch:

(quoted diff) +1 -1
-#define MVPP2_SKB_HEADROOM     min(max(XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)
+#define MVPP2_SKB_HEADROOM

MIN_T(int,MAX_T(int,XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)

to fix it.

Yeah sorry just saw you had already addresed this Linus... I just went with a clamp()_t in my patch.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

Or (if I've got is right):
#define MVPP2_SKB_HEADROOM clamp(XDP_PACKET_HEADROOM, NET_SKB_PAD, 224)

I'm pretty sure you can clamp_t(int, ...) here safely based on usage.

Why doesn't a plain clamp() work?
The xxx_t() variants really shouldn't be used very often.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#46 lore raw
Quoted 24 lines · David Laight

From: Lorenzo Stoakes

Quoted 23 lines

Sent: 27 July 2024 19:59

Quoted 7 lines · David Laight

...

Quoted 6 lines

and it will spit out

Longest line is drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:1136 (2346kB)

     '   ((((((pkt_size) + __builtin_choose_expr((sizeof(int) ==

sizeof(*(8 ? ((void *)((long)((__builtin_...'

Quoted 8 lines
Quoted 8 lines

to tell me that we have that insane 2.2 *megabyte* line due to the MVPP2_SKB_HEADROOM thing, and I should apply this patch:

(quoted diff) +1 -1
-#define MVPP2_SKB_HEADROOM     min(max(XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)
+#define MVPP2_SKB_HEADROOM

MIN_T(int,MAX_T(int,XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)

to fix it.

Yeah sorry just saw you had already addresed this Linus... I just went with a clamp()_t in my patch.

Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Or (if I've got is right):
#define MVPP2_SKB_HEADROOM clamp(XDP_PACKET_HEADROOM, NET_SKB_PAD, 224)

I'm pretty sure you can clamp_t(int, ...) here safely based on usage.

Why doesn't a plain clamp() work?

It was on the assumption that it'd cause less of a combinatorial explosion due to the various clever checks your macros have in them.

However I stand corrected after checking - it's actually a little smaller if we use clamp(), so I am fine with us just using clamp() here.

Quoted 1 line

The xxx_t() variants really shouldn't be used very often.

I disagree, but for the sakes of civility (+ sanity + thread length) I'm not going to go on about it :)

I might (politely!) suggest, however, that under the circumstances it might be best to refrain from suggesting what should/shouldn't be used when it comes to these macros.

Quoted 7 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#47 lore raw
In reply to Linus Torvalds
Quoted 5 lines · Linus Torvalds

but while that is certainly an impressive 82kB line, we have some good company in code VM header files, and I've also seen

Longest line is include/linux/page-flags.h:507 (27kB)

     'static inline __attribute__((__gnu_inline__))
Quoted 8 lines

__attribute__((__unused__)) __attribute__((no_instrume...'

because the expansion from

__PAGEFLAG(Locked, locked, PF_NO_TAIL)

does indeed generate some impressive stuff. It's all the functions for the locked bit handling generated from one line.

In the specific case of PageLocked, that can hopefully go away fairly soon. We only have 24 instances left in tree and five of those are comments/docs. The ones in fs (btrfs, crypto, f2fs, ocfs2 and pipe) should be converted to folio soon.

Mostly they're just detritus. We could probably remove the PageLocked definition in the next merge window if we actually care.

But I have been wondering whether the way we define all the functions around page/folio flags is sensible. Every file which includes page-flags.h (... which is most of them ...) regenerates the macros. You can't grep for the definition of folio_test_locked(). There's nowhere to put kernel-doc for folio_test_locked().

Would we be better off generating page-flags-generated.h from a more compact definition file somewhere, rather than using the C preprocessor?

#48 lore raw
Quoted 5 lines · Matthew Wilcox

In the specific case of PageLocked, that can hopefully go away fairly soon. We only have 24 instances left in tree and five of those are comments/docs. The ones in fs (btrfs, crypto, f2fs, ocfs2 and pipe) should be converted to folio soon.

So unlike the min/max mess, at least in this case there's some _reason_ for the long lines (ie it's not some long line because of just mindless expansion, it's a long line because it defines several helper functions intentionally in one go).

End result: I don't think these are really in the same class as some of the min/max expansion issues, but:

Quoted 5 lines

But I have been wondering whether the way we define all the functions around page/folio flags is sensible. Every file which includes page-flags.h (... which is most of them ...) regenerates the macros. You can't grep for the definition of folio_test_locked(). There's nowhere to put kernel-doc for folio_test_locked().

Right, these things do have their own issues, and the different flag handling helper functions _used_ to be simpler than they are now. It is indeed a pain to grep for, for example, and yes, it gets included for a lot of people who simply don't need it or want it.

I'm not convinced having it in a generated file would help the greppability - I certainly don't use "grep -R" any more, I use "git grep", which by definition will never see any generated files any more than it sees pre-processor output.

But whether those functions should be in one core header file at all, that's clearly questionable. I wonder how inlined they need to be either, outside of the trivial cases.

(And when I say "those functions", I don't mean just the pageflag ones. The folio ones largely have all the same issues - except the "page" ones have a *TON* of debugging code in them, so they expand to be much bigger)

Side note: one very simple thing to do might be to short-circuit the "constant bit number" test in the bitops that the PG_##flags cases use. That's a lot of noise to select between "simple constant bit number" and "generic variable one" when we know they are all constant.

Linus

#49 lore raw
In reply to Lorenzo Stoakes

On Fri, Jul 26, 2024 at 08:21:31PM GMT, Lorenzo Stoakes wrote:
[snip]

Quoted 2 lines

I might try an allmodconfig with David's patches to see if that pulls out any other really obvious cases.

I did this last night but didn't want to spam too much. This shows the delta on pre-processed file size before/after David's patch set which should give a good indication for min/max combinatorial explosion.

There's a lot of noise from staging drivers but also a bunch from other places. Might be fun to run Linus's longest line thing on these and see what pops out too...

I enclose the full list. Interestingly we see an _increase_ in some file sizes with the patches. But not too many (they show negative delta).

...
1768108./fs/xfs/libxfs/xfs_trans_resv.o.pre 2354450./drivers/staging/media/atomisp/pci/isp/kernels/gc/gc_1.0/ia_css_gc.host.o.pre 2354694./drivers/net/ethernet/intel/iavf/iavf_main.o.pre 2419455./drivers/net/ethernet/marvell/mvneta.o.pre 3119381./drivers/staging/media/atomisp/pci/sh_css_params.o.pre 3476319./drivers/staging/media/atomisp/pci/isp/kernels/tnr/tnr_1.0/ia_css_tnr.host.o.pre 3490399./drivers/staging/media/atomisp/pci/isp/kernels/bh/bh_2/ia_css_bh.host.o.pre 3494239./drivers/staging/media/atomisp/pci/isp/kernels/de/de_1.0/ia_css_de.host.o.pre 4621996./drivers/staging/media/atomisp/pci/isp/kernels/bnr/bnr_1.0/ia_css_bnr.host.o.pre 4623276./drivers/staging/media/atomisp/pci/isp/kernels/ob/ob2/ia_css_ob2.host.o.pre 5445036./drivers/staging/media/atomisp/pci/isp/kernels/wb/wb_1.0/ia_css_wb.host.o.pre 8503041./drivers/staging/media/atomisp/pci/isp/kernels/s3a/s3a_1.0/ia_css_s3a.host.o.pre 11322234./drivers/staging/media/atomisp/pci/isp/kernels/dp/dp_1.0/ia_css_dp.host.o.pre 21096020./drivers/net/ethernet/marvell/mvpp2/mvpp2_main.o.pre 45254333./arch/x86/xen/setup.o.pre 62603617./drivers/staging/media/atomisp/pci/isp/kernels/ynr/ynr_1.0/ia_css_ynr.host.o.pre

24,408 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#50 lore raw
Quoted 2 lines · Lorenzo Stoakes

62603617./drivers/staging/media/atomisp/pci/isp/kernels/ynr/ynr_1.0/ia_css_ynr.host.o.pre

Heh.

Longest line is drivers/.../ia_css_ynr.host.c:71 (27785kB)

yeah, that's a single line that expands to 27MB in size.

And yes, that line is one single min(...) expression with arguments that are then in turn macros with other nested min/max arguments.

See also drivers/staging/media/atomisp/pci/sh_css_frac.h.

On my fairly beefy (admittedly more cores than single-thread) machine, just generating the preprocessor file takes just under 20s.

Building the object file is actually faster at "only" 8.5s for that one file, because it uses the built-in preprocessor and never writes it out, and most of the actual preprocessing result is trivial stuff that gets thrown away immediately.

Linus

#51 lore raw
Quoted 19 lines · Linus Torvalds
Quoted 2 lines · Lorenzo Stoakes

62603617./drivers/staging/media/atomisp/pci/isp/kernels/ynr/ynr_1.0/ia_css_ynr.host.o.pre

Heh.

Longest line is drivers/.../ia_css_ynr.host.c:71 (27785kB)

yeah, that's a single line that expands to 27MB in size.

And yes, that line is one single min(...) expression with arguments that are then in turn macros with other nested min/max arguments.

See also drivers/staging/media/atomisp/pci/sh_css_frac.h.

On my fairly beefy (admittedly more cores than single-thread) machine, just generating the preprocessor file takes just under 20s.

Quoted 6 lines

Building the object file is actually faster at "only" 8.5s for that one file, because it uses the built-in preprocessor and never writes it out, and most of the actual preprocessing result is trivial stuff that gets thrown away immediately.

Linus

I attach a patch which addresses some of the worst culprits here including that staging monstrosity. Changing the sDIGIT_FITTING() and uDIGIT_FITTING() macros affects a ton of other related drivers so has an outsized impact.

Another big one I tackled is the NET_SKB_PAD define causing slightly hidden nesting, we can just replace that with a dumb #if and get rid of that.

I also moved MVPP2_SKB_HEADROOM to a clamp_t().

I noticed a bunch of xfs stuff that's slow too, but tracked that down to <linux/bio.h> which I see you're covering in another thread with Willy.

There are other bits and pieces, but this seems to cover the most egregious cases.

This patch reduces preprocessor-generated output for allmodconfig from 102,966,525,841 bytes (!) to 102,764,954,617 on my system, thus saves ~200MB of generated output.

----8<----
From 02f844f0a623645134732aeb96f635558050d104 Mon Sep 17 00:00:00 2001
From: Lorenzo Stoakes <[email protected]>
Date: Sat, 27 Jul 2024 19:10:01 +0100
Subject: [PATCH] minmax: fixup call sites generating egregious macro
expansions

Adjust code that results in a combinatorial explosion of min()/max() macro expansion, resulting in significant build performance degradation.

Simplify by using constructs that do not result in the preprocessor doing this.

This code should have no functional impact.

---
 drivers/net/ethernet/marvell/mvpp2/mvpp2.h    |  2 +-
 .../staging/media/atomisp/pci/sh_css_frac.h   | 26 ++++++++++++++-----
 include/linux/skbuff.h                        |  6 ++++-
 3 files changed, 25 insertions(+), 9 deletions(-)
drivers/net/ethernet/marvell/mvpp2/mvpp2.h +1 -1
index e809f91c08fb..8b431f90efc3 100644
@@ -23,7 +23,7 @@
 /* The PacketOffset field is measured in units of 32 bytes and is 3 bits wide,
  * so the maximum offset is 7 * 32 = 224
  */
-#define MVPP2_SKB_HEADROOM	min(max(XDP_PACKET_HEADROOM, NET_SKB_PAD), 224)
+#define MVPP2_SKB_HEADROOM	clamp_t(int, XDP_PACKET_HEADROOM, NET_SKB_PAD, 224)
 
 #define MVPP2_XDP_PASS		0
 #define MVPP2_XDP_DROPPED	BIT(0)
drivers/staging/media/atomisp/pci/sh_css_frac.h +19 -7
index b90b5b330dfa..ec6cc818f3c6 100644
@@ -32,12 +32,24 @@
 #define uISP_VAL_MAX		      ((unsigned int)((1 << uISP_REG_BIT) - 1))
 
 /* a:fraction bits for 16bit precision, b:fraction bits for ISP precision */
-#define sDIGIT_FITTING(v, a, b) \
-	min_t(int, max_t(int, (((v) >> sSHIFT) >> max(sFRACTION_BITS_FITTING(a) - (b), 0)), \
-	  sISP_VAL_MIN), sISP_VAL_MAX)
-#define uDIGIT_FITTING(v, a, b) \
-	min((unsigned int)max((unsigned)(((v) >> uSHIFT) \
-	>> max((int)(uFRACTION_BITS_FITTING(a) - (b)), 0)), \
-	  uISP_VAL_MIN), uISP_VAL_MAX)
+static inline int sDIGIT_FITTING(short v, int a, int b)
+{
+	int fit_shift = sFRACTION_BITS_FITTING(a) - b;
+
+	v >>= sSHIFT;
+	v >>= fit_shift > 0 ? fit_shift : 0;
+
+	return clamp_t(int, v, sISP_VAL_MIN, sISP_VAL_MAX);
+}
+
+static inline unsigned uDIGIT_FITTING(unsigned v, int a, int b)
+{
+	int fit_shift = uFRACTION_BITS_FITTING(a) - b;
+
+	v >>= uSHIFT;
+	v >>= fit_shift > 0 ? fit_shift : 0;
+
+	return clamp_t(unsigned, v, uISP_VAL_MIN, uISP_VAL_MAX);
+}
 
 #endif /* __SH_CSS_FRAC_H */
include/linux/skbuff.h +5 -1
index 29c3ea5b6e93..d53b296df504 100644
@@ -3164,7 +3164,11 @@ static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
  * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
  */
 #ifndef NET_SKB_PAD
-#define NET_SKB_PAD	max(32, L1_CACHE_BYTES)
+#if L1_CACHE_BYTES < 32
+#define NET_SKB_PAD	32
+#else
+#define NET_SKB_PAD	L1_CACHE_BYTES
+#endif
 #endif
 
 int ___pskb_trim(struct sk_buff *skb, unsigned int len);

--
2.45.2

#52 lore raw
In reply to Linus Torvalds
Quoted 2 lines · Linus Torvalds

Longest line is drivers/.../ia_css_ynr.host.c:71 (27785kB)

So that 27MB shrank to 23MB and then to "only" 1.4MB with the patches that are now in mainline.

Now with the patch I just posted at

https://lore.kernel.org/all/CAHk-=wgRDupSBzUX_N_Qo_eaYyDfOH=VTihhikN36cGxCc+jvg@mail.gmail.com/

that nasty line expands to "just" 82kB, and the file builds quickly.

Still not exactly small, but hey, two and a half orders of magnitude isn't bad.

And yes, that one driver is disgusting and Lorenzo had a nice patch to change some macros to inline functions which just made the whole problem go away, but as a test-case for horrid macro expansion of min()/max() it was lovely.

Of course, I never did any build timings. I'm not sure how much it actually shows up on that level. A couple of bad cases won't be *that* noticeable when most cases aren't too horrid.

Linus

#53 lore raw
Quoted 4 lines · Linus Torvalds

Of course, I never did any build timings. I'm not sure how much it actually shows up on that level. A couple of bad cases won't be *that* noticeable when most cases aren't too horrid.

I did a little bit of build timing last week, which confirms exactly what you expect here: On individual files, the difference can easily be 1000x faster, like on that arch/x86/xen/setup.c file, but overall the difference in build speed for an allmodconfig or defconfig build was between 1% and 3%, depending on how many of the extreme cases are included.

Of course, it would be easy to take one of the extreme cases and add another expansion that then drives the memory usage and/or compile time up to a point where the build fails.

Arnd

#54 lore raw
In reply to Lorenzo Stoakes

Hi Lorenzo,

Quoted 6 lines · Lorenzo Stoakes
Quoted 5 lines · Linus Torvalds
Quoted 2 lines · Lorenzo Stoakes

5,447,539 ./arch/x86/xen/setup.o.pre

Quoted 8 lines
Quoted 3 lines

Can you perhaps do some kind of "max expansion" on all the preprocessor files (you seem to have done it by changing the ".c.o" rule to just spit it out as "o.pre", which sounds fine).

Yeah I simply hacked in a gcc -E, ugly but effective, e.g. prepending cmd_cc_o_c in scripts/Makefile.build with:

cmd_cc_o_c = $(CC) $(c_flags) -E $< > [email protected]; \

Kbuild already knows how to build a preprocessed file (.i), so making any .o depend on its .i should work, too?

Or am I missing something?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#55 lore raw
Quoted 17 lines · Geert Uytterhoeven

Hi Lorenzo,

Quoted 13 lines · Lorenzo Stoakes
Quoted 8 lines · Linus Torvalds
Quoted 2 lines · Lorenzo Stoakes

5,447,539 ./arch/x86/xen/setup.o.pre

Can you perhaps do some kind of "max expansion" on all the preprocessor files (you seem to have done it by changing the ".c.o" rule to just spit it out as "o.pre", which sounds fine).

Yeah I simply hacked in a gcc -E, ugly but effective, e.g. prepending cmd_cc_o_c in scripts/Makefile.build with:

Quoted 6 lines
Quoted 1 line

cmd_cc_o_c = $(CC) $(c_flags) -E $< > [email protected]; \

Kbuild already knows how to build a preprocessed file (.i), so making any .o depend on its .i should work, too?

Or am I missing something?

Yup I'm sure putting something in like that would work, I wasn't aware of the whole .i thing when I did this and I just hacked something in quick + dirty to get the data.

Quoted 11 lines

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#56 lore raw
In reply to Linus Torvalds

From: Linus Torvalds

Quoted 7 lines

Sent: 26 July 2024 19:24

Quoted 2 lines · Lorenzo Stoakes

5,447,539 ./arch/x86/xen/setup.o.pre

Quoted 8 lines

Can you perhaps do some kind of "max expansion" on all the preprocessor files (you seem to have done it by changing the ".c.o" rule to just spit it out as "o.pre", which sounds fine).

For example, this trivial patch seems to fix the setup.c expansion by about an order of magnitude (ie 50M -> 5M).

Entirely untested, but looks ObviouslyCorrect(tm) to me.

- extra_pages = min3(EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)),
- extra_pages, max_pages - max_pfn);
+ maxmem_pages = EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM));
+ extra_pages = min3(maxmem_pages, extra_pages, max_pages - max_pfn);

I suspect just reordering to:

	extra_pages = min3(extra_pages, max_pages - max_pfn,
		EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)));

will have a similar effect.

I'll try to do a v2 of the patchset tomorrow.
- I'll leave __is_constexpr() with its current define (for now).
- Fix the issue the build robot fount.
- Fix the typo in min3().
- use auto_type for min/max functions.

Once that lot is in I'll do some further changes for 'next'.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#57 lore raw
Quoted 5 lines · David Laight

I suspect just reordering to:
extra_pages = min3(extra_pages, max_pages - max_pfn,
EXTRA_MEM_RATIO * min(max_pfn, PFN_DOWN(MAXMEM)));
will have a similar effect.

That's disgusting.

No, we're not adding random "the ordering of min3() matters subtly for expansion size" crap.

We're simplifying the sub-expressions, and fixing the problem that way.

Linus