Skip to contentSkip to conversation

overflow: Convert size_add() to take variadic arguments

RFC PATCH In review Andy Shevchenko 4 participants 17 messages Jun 17 – Jun 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover overflow: Convert size_add() to take variadic arguments

This is an RFC!

We have already users that want add sizes of up to 5 arguments and I know about at least one that also wants 3 or 4.

This is brave move to make size_add() to take variadic arguments. The second patch is an example of use.

The implementation includes a case with a single argument on a purpose. In the future it might be extended to take an array as an argument, something like

	int sizes[21];
	size_add(sizes);

where the first element is amount of entries in the array (the same format as used in get_options() call) or other possible variants. This can be distinguished by _Generic().

But it may be dropped and we require always two arguments at minimum.

The RFC just to collect opinions and perception. Note, array3*(), min3()/max3() and all like that also can use similar approach.

Andy Shevchenko (2):
overflow: Allow to sum a few arguments at once
wifi: nl80211: Call size_add() only once

 include/linux/overflow.h | 37 ++++++++++++++++++++++++++-----------
 net/wireless/nl80211.c   | 11 ++++-------
 2 files changed, 30 insertions(+), 18 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/2 overflow: Allow to sum a few arguments at once

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

---
 include/linux/overflow.h | 37 ++++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)
include/linux/overflow.h +26 -11
index a8cb6319b4fb..a8b0325e73f3 100644
@@ -2,9 +2,10 @@
 #ifndef __LINUX_OVERFLOW_H
 #define __LINUX_OVERFLOW_H
 
+#include <linux/args.h>
 #include <linux/compiler.h>
-#include <linux/limits.h>
 #include <linux/const.h>
+#include <linux/limits.h>
 
 /*
  * We need to compute the minimum and maximum values representable in a given
@@ -337,16 +338,7 @@ static __always_inline size_t __must_check size_mul(size_t factor1, size_t facto
 	return bytes;
 }
 
-/**
- * size_add() - Calculate size_t addition with saturation at SIZE_MAX
- * @addend1: first addend
- * @addend2: second addend
- *
- * Returns: calculate @addend1 + @addend2, both promoted to size_t,
- * with any overflow causing the return value to be SIZE_MAX. The
- * lvalue must be size_t to avoid implicit type conversion.
- */
-static __always_inline size_t __must_check size_add(size_t addend1, size_t addend2)
+static __always_inline size_t __must_check __size_add(size_t addend1, size_t addend2)
 {
 	size_t bytes;
 
@@ -356,6 +348,29 @@ static __always_inline size_t __must_check size_add(size_t addend1, size_t adden
 	return bytes;
 }
 
+#define __size_add0(addend1, ...)						\
+	__size_add(addend1, 0)
+#define __size_add1(addend1, addend2, ...)					\
+	__size_add(addend1,  addend2)
+#define __size_add2(addend1, addend2, addend3, ...)				\
+	__size_add(__size_add(addend1,  addend2), addend3)
+#define __size_add3(addend1, addend2, addend3, addend4, ...)			\
+	__size_add(__size_add2(addend1,  addend2, addend3), addend4)
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)
+
+/**
+ * size_add() - Calculate size_t addition with saturation at SIZE_MAX
+ * @addend1: first addend
+ * @...: more to add (optional)
+ *
+ * Returns: calculate @addend1 + @addend2, both promoted to size_t,
+ * with any overflow causing the return value to be SIZE_MAX. The
+ * lvalue must be size_t to avoid implicit type conversion.
+ */
+#define size_add(addend1, ...)	\
+	CONCATENATE(__size_add, COUNT_ARGS(__VA_ARGS__))(addend1, __VA_ARGS__)
+
 /**
  * size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX
  * @minuend: value to subtract from
Signature · 1 line
2.50.1
maintainer
#2 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 7 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

include/linux/overflow.h +4 -0
+#define __size_add3(addend1, addend2, addend3, addend4, ...)			\
+	__size_add(__size_add2(addend1,  addend2, addend3), addend4)
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)

I guess it's not going to really matter, but it would generate fewer calls to have something more like

#define __size_add3(a1, a2, a3, a4) \
	size_add(size_add(a1, a2), size_add(a3, a4))
#define __size_add4(a1, a2, a3, a4, a5) \
	size_add(size_add(a1, a2), size_add(a3, a4, a5))

as a binary tree, rather than only cutting one off every time. Not sure that results in hugely different code though - maybe fewer overflow checks?

Although your version make it really completely equivalent to the nl80211.c code, clearly it doesn't matter if all the values are "good", and I believe the overflow behaviour means it doesn't matter for the overflow case either?

johannes

#3 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 12 lines · On Wed, 17 Jun 2026 14:56:09 +0200 Johannes Berg <[email protected]>…

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 7 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

include/linux/overflow.h +4 -0
+#define __size_add3(addend1, addend2, addend3, addend4, ...)			\
+	__size_add(__size_add2(addend1,  addend2, addend3), addend4)
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)  

I guess it's not going to really matter, but it would generate fewer calls to have something more like

Quoted 8 lines
#define __size_add3(a1, a2, a3, a4) \
	size_add(size_add(a1, a2), size_add(a3, a4))
#define __size_add4(a1, a2, a3, a4, a5) \
	size_add(size_add(a1, a2), size_add(a3, a4, a5))

as a binary tree, rather than only cutting one off every time. Not sure that results in hugely different code though - maybe fewer overflow checks?

The binary tree stands a chance of executing less slowly because the leaf adds can be executed in parallel.
Excluding the saturation checks (wtf is it called size_add() not saturating_add() ?) (a + b) + (c + d) will usually execute faster than ((a + b) + c) + d because the (a + b) and (c + d) can execute at the same time; unfortunately gcc will always generate the latter.

David

Quoted 8 lines

Although your version make it really completely equivalent to the nl80211.c code, clearly it doesn't matter if all the values are "good", and I believe the overflow behaviour means it doesn't matter for the overflow case either?

johannes

#4 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 14 lines · David Laight
Quoted 12 lines · On Wed, 17 Jun 2026 14:56:09 +0200 Johannes Berg <[email protected]>…

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 7 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

include/linux/overflow.h +4 -0
+#define __size_add3(addend1, addend2, addend3, addend4, ...)			\
+	__size_add(__size_add2(addend1,  addend2, addend3), addend4)
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)  

I guess it's not going to really matter, but it would generate fewer calls to have something more like

Quoted 8 lines
Quoted 8 lines
#define __size_add3(a1, a2, a3, a4) \
	size_add(size_add(a1, a2), size_add(a3, a4))
#define __size_add4(a1, a2, a3, a4, a5) \
	size_add(size_add(a1, a2), size_add(a3, a4, a5))

as a binary tree, rather than only cutting one off every time. Not sure that results in hugely different code though - maybe fewer overflow checks?

Good question. I'm also thinking that one-by-one may expand in too much of preprocessor code (haven't checked myself).

Quoted 6 lines

The binary tree stands a chance of executing less slowly because the leaf adds can be executed in parallel.
Excluding the saturation checks (wtf is it called size_add() not saturating_add() ?) (a + b) + (c + d) will usually execute faster than ((a + b) + c) + d because the (a + b) and (c + d) can execute at the same time; unfortunately gcc will always generate the latter.

I'm confused. "unfortunately... the latter"? You meant "the former"?

Quoted 4 lines
Quoted 4 lines

Although your version make it really completely equivalent to the nl80211.c code, clearly it doesn't matter if all the values are "good", and I believe the overflow behaviour means it doesn't matter for the overflow case either?

Indeed. Whenever the value is saturated, the rest is just matter of sequential unlikely branches taken.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

(hah, just found this window open from this morning ...)

Quoted 20 lines · Andy Shevchenko
Quoted 19 lines · David Laight
Quoted 17 lines · On Wed, 17 Jun 2026 14:56:09 +0200 Johannes Berg <[email protected]>…

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 7 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

include/linux/overflow.h +4 -0
+#define __size_add3(addend1, addend2, addend3, addend4, ...)			\
+	__size_add(__size_add2(addend1,  addend2, addend3), addend4)
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)  

I guess it's not going to really matter, but it would generate fewer calls to have something more like

#define __size_add3(a1, a2, a3, a4) \
	size_add(size_add(a1, a2), size_add(a3, a4))
#define __size_add4(a1, a2, a3, a4, a5) \
	size_add(size_add(a1, a2), size_add(a3, a4, a5))
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

as a binary tree, rather than only cutting one off every time. Not sure that results in hugely different code though - maybe fewer overflow checks?

Good question. I'm also thinking that one-by-one may expand in too much of preprocessor code (haven't checked myself).

No. I was confused, and managed to confuse you too perhaps, sorry!

We have to have the same number of operations (__size_add calls) regardless, since you have to add it all up: 1 + 2 + 3 + 4 + 5 has a fixed number of + signs regardless of how you parenthesise it.

I guess actual CPU execution would have a better data dependency tree if we balance it, but ... if our hotpath depends on size_add() we've lost already.

johannes

#6 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 30 lines · On Thu, 18 Jun 2026 20:53:37 +0200 Johannes Berg <[email protected]>…

(hah, just found this window open from this morning ...)

Quoted 20 lines · Andy Shevchenko
Quoted 19 lines · David Laight
Quoted 17 lines · On Wed, 17 Jun 2026 14:56:09 +0200 Johannes Berg <[email protected]>…

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 7 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

include/linux/overflow.h +4 -0
+#define __size_add3(addend1, addend2, addend3, addend4, ...)			\
+	__size_add(__size_add2(addend1,  addend2, addend3), addend4)
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)    

I guess it's not going to really matter, but it would generate fewer calls to have something more like

#define __size_add3(a1, a2, a3, a4) \
	size_add(size_add(a1, a2), size_add(a3, a4))
#define __size_add4(a1, a2, a3, a4, a5) \
	size_add(size_add(a1, a2), size_add(a3, a4, a5))
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

as a binary tree, rather than only cutting one off every time. Not sure that results in hugely different code though - maybe fewer overflow checks?

Good question. I'm also thinking that one-by-one may expand in too much of preprocessor code (haven't checked myself).

Quoted 8 lines

No. I was confused, and managed to confuse you too perhaps, sorry!

We have to have the same number of operations (__size_add calls) regardless, since you have to add it all up: 1 + 2 + 3 + 4 + 5 has a fixed number of + signs regardless of how you parenthesise it.

I guess actual CPU execution would have a better data dependency tree if we balance it,

Absolutely.
Intel Haswell onwards and zen1-4 can execute 4 independent add/sub/and/or (etc) every clock.

zen5 wins with 6 arithmetic ops or 4 cmov (and 2 alu) per clock.

Quoted 1 line

but ... if our hotpath depends on size_add() we've lost already.

I've no idea what the compiler generates, but a cmovc to copy in ~0 when the add sets carry stands a good chance of being pretty near the best. What you don't want is a conditional jump.
The add, cmov pair will take two clocks, but the pairs are independent of each other (the carry flag isn't a limitation).
The cpu should be able to execute two add and two cmov every clock. So with 4 values the 'tree' version is 4 clocks
The other problem with ((a + b) + c) + d is that execution can't start until both a and b are available; with (a + b) + (c + d) it is much more likely that one of the adds can be executed early.

Trying to guess the performance of modern cpu is non-trivial.

David

Quoted 2 lines

johannes

maintainer
#7 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

Oh, this is fun. I like it. :)

Quoted 44 lines
---
 include/linux/overflow.h | 37 ++++++++++++++++++++++++++-----------
 1 file changed, 26 insertions(+), 11 deletions(-)
include/linux/overflow.h +3 -11
index a8cb6319b4fb..a8b0325e73f3 100644
@@ -2,9 +2,10 @@
 #ifndef __LINUX_OVERFLOW_H
 #define __LINUX_OVERFLOW_H
 
+#include <linux/args.h>
 #include <linux/compiler.h>
-#include <linux/limits.h>
 #include <linux/const.h>
+#include <linux/limits.h>
 
 /*
  * We need to compute the minimum and maximum values representable in a given
@@ -337,16 +338,7 @@ static __always_inline size_t __must_check size_mul(size_t factor1, size_t facto
 	return bytes;
 }
 
-/**
- * size_add() - Calculate size_t addition with saturation at SIZE_MAX
- * @addend1: first addend
- * @addend2: second addend
- *
- * Returns: calculate @addend1 + @addend2, both promoted to size_t,
- * with any overflow causing the return value to be SIZE_MAX. The
- * lvalue must be size_t to avoid implicit type conversion.
- */
-static __always_inline size_t __must_check size_add(size_t addend1, size_t addend2)
+static __always_inline size_t __must_check __size_add(size_t addend1, size_t addend2)
 {
 	size_t bytes;
 
@@ -356,6 +348,29 @@ static __always_inline size_t __must_check size_add(size_t addend1, size_t adden
 	return bytes;
 }
 

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 10 lines
include/linux/overflow.h +10 -0
+#define __size_add0(addend1, ...)						\
+	__size_add(addend1, 0)
+#define __size_add1(addend1, addend2, ...)					\
+	__size_add(addend1,  addend2)
+#define __size_add2(addend1, addend2, addend3, ...)				\
+	__size_add(__size_add(addend1,  addend2), addend3)
+#define __size_add3(addend1, addend2, addend3, addend4, ...)			\
+	__size_add(__size_add2(addend1,  addend2, addend3), addend4)
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)

Is 4 the max seen in practice?

Signature · 1 line
Kees Cook
#8 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 5 lines · Kees Cook
Quoted 2 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

Oh, this is fun. I like it. :)

He-he :-)

...

Quoted 4 lines

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 2 lines
include/linux/overflow.h +2 -0
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)

Is 4 the max seen in practice?

In patch 2 there are 5! In cover letter I also mentioned a new user (there are actually three). And I remember seeing 3 somewhere else.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer Acked-by
#9 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 11 lines · Andy Shevchenko
Quoted 5 lines · Kees Cook
Quoted 2 lines · Andy Shevchenko

Convert size_add() to take variadic argument, so we can simplify users with using a macro only once.

Oh, this is fun. I like it. :)

He-he :-)

...

Quoted 7 lines
Quoted 4 lines

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 2 lines
include/linux/overflow.h +2 -0
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)

Is 4 the max seen in practice?

In patch 2 there are 5! In cover letter I also mentioned a new user (there are actually three). And I remember seeing 3 somewhere else.

I feel like we should allow size_mul() to work this way too, though I don't find any users that would need it, so I guess just size_add()?

I'm fine for this to go via whatever tree is first to use it:

Signature · 1 line
Kees Cook
#10 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 3 lines · Kees Cook
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Kees Cook

On Wed, Jun 17, 2026 at 01:12:36PM +0200, Andy Shevchenko wrote:

...

Quoted 10 lines
Quoted 7 lines
Quoted 4 lines

include/linux/overflow.h static __always_inline size_t __must_check size_add(size_t addend1, size_t adden · line 360

Quoted 2 lines
include/linux/overflow.h +2 -0
+#define __size_add4(addend1, addend2, addend3, addend4, addend5, ...)		\
+	__size_add(__size_add3(addend1,  addend2, addend3, addend4), addend5)

Is 4 the max seen in practice?

In patch 2 there are 5! In cover letter I also mentioned a new user (there are actually three). And I remember seeing 3 somewhere else.

I feel like we should allow size_mul() to work this way too, though I don't find any users that would need it, so I guess just size_add()?

For now, yes. The array_size() can be modified as we have already two of them, but I don't remember seeing more than 3, so it might look like an unneeded churn.

Quoted 3 lines

I'm fine for this to go via whatever tree is first to use it:

Thanks!

Johannes, are you okay to take a new version (I assume the wish is to have the balanced additions)? If so, I will prepare one.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#11 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 2 lines · Andy Shevchenko

Johannes, are you okay to take a new version (I assume the wish is to have the balanced additions)? If so, I will prepare one.

Sure, I can take the patches, sounds good to me.

johannes

#12 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 5 lines · Johannes Berg
Quoted 2 lines · Andy Shevchenko

Johannes, are you okay to take a new version (I assume the wish is to have the balanced additions)? If so, I will prepare one.

Sure, I can take the patches, sounds good to me.

Will send soon, for the record the *.i file is 61 character less with the patch.

Was

size = __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), sizeof(*(request)) + __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})))), __size_add(sizeof(*(request)), __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");}))))));
- size = __size_add(size, size_mul(sizeof(*request->ssids), n_ssids));
- size = __size_add(size, size_mul(sizeof(*request->match_sets), n_match_sets));
- size = __size_add(size, size_mul(sizeof(*request->scan_plans), n_plans));
- size = __size_add(size, ie_len);

Now

size = __size_add(__size_add(__size_add(__builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), sizeof(*(request)) + __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})))), __size_add(sizeof(*(request)), __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})))))), ie_len), size_mul(sizeof(*request->ssids), n_ssids)), __size_add(size_mul(sizeof(*request->match_sets), n_match_sets), size_mul(sizeof(*request->scan_plans), n_plans)));

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 17 lines · On Tue, 23 Jun 2026 17:09:40 +0300 Andy Shevchenko…
Quoted 5 lines · Johannes Berg
Quoted 2 lines · Andy Shevchenko

Johannes, are you okay to take a new version (I assume the wish is to have the balanced additions)? If so, I will prepare one.

Sure, I can take the patches, sounds good to me.

Will send soon, for the record the *.i file is 61 character less with the patch.

Was

size = __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), sizeof(*(request)) + __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})))), __size_add(sizeof(*(request)), __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");}))))));
- size = __size_add(size, size_mul(sizeof(*request->ssids), n_ssids));
- size = __size_add(size, size_mul(sizeof(*request->match_sets), n_match_sets));
- size = __size_add(size, size_mul(sizeof(*request->scan_plans), n_plans));
- size = __size_add(size, ie_len);

Quoted 4 lines

Now

size = __size_add(__size_add(__size_add(__builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), sizeof(*(request)) + __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})))), __size_add(sizeof(*(request)), __builtin_choose_expr((sizeof(int) == sizeof(*(8 ? ((void *)((long)(n_channels) * 0l)) : (int *)8))), (n_channels) * sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})), size_mul(n_channels, sizeof(*(request)->channels) + ((int)sizeof(struct {_Static_assert(!(!(!__builtin_types_compatible_p(typeof(((request)->channels)), typeof(&((request)->channels)[0])))), "must be array");})))))), ie_len), size_mul(sizeof(*request->ssids), n_ssids)), __size_add(size_mul(sizeof(*request->match_sets), n_match_sets), size_mul(sizeof(*request->scan_plans), n_plans)));

Both those seem to contain multiple expansions of is_constexpr(). I'd be surprised if __builtin_constant_p() wasn't good enough.

I also wonder what kind of mess the object code looks like.

I'd also worry whether the saturating maths in any way performs the correct check. I suspect the (non-saturated) value gets used for a kmalloc(),
but that has much smaller valid (and sane) limits than can come out of the above.

David

maintainer
#14 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

Quoted 5 lines · David Laight

Both those seem to contain multiple expansions of is_constexpr(). I'd be surprised if __builtin_constant_p() wasn't good enough.

I also wonder what kind of mess the object code looks like.

You should definitely wonder less out loud and only comment when you have actual hard facts...

Quoted 3 lines

I'd also worry whether the saturating maths in any way performs the correct check. I suspect the (non-saturated) value gets used for a kmalloc(),
but that has much smaller valid (and sane) limits than can come out of the above.

That's, frankly, ridiculous.

johannes

#15 lore raw

Re: [PATCH v1 1/2] overflow: Allow to sum a few arguments at once

In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 5 lines · Johannes Berg
Quoted 2 lines · Andy Shevchenko

Johannes, are you okay to take a new version (I assume the wish is to have the balanced additions)? If so, I will prepare one.

Sure, I can take the patches, sounds good to me.

Will send soon, for the record the *.i file is 61 character less with the patch.

After looking closer at the potential users, I see less benefit of this change than before. The other case in one of wireless driver can actually benefit from separate size_add() calls and temporary variables which will be reused later in the code. The similar approach even can be used for nl80211.c case. So for now I abandon this until more users come. (Of course it might be that I'm missing something and if somebody knows what, I would like to learn.)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw

PATCH 2/2 wifi: nl80211: Call size_add() only once

In reply to Andy Shevchenko

Since size_add() may take a few arguments at once, call it only once.

---
 net/wireless/nl80211.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
net/wireless/nl80211.c +4 -7
index 53b4b3f76697..98f92c268944 100644
@@ -11560,13 +11560,10 @@ nl80211_parse_sched_scan(struct wiphy *wiphy, struct wireless_dev *wdev,
 	     attrs[NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST]))
 		return ERR_PTR(-EINVAL);
 
-	size = struct_size(request, channels, n_channels);
-	size = size_add(size, array_size(sizeof(*request->ssids), n_ssids));
-	size = size_add(size, array_size(sizeof(*request->match_sets),
-					 n_match_sets));
-	size = size_add(size, array_size(sizeof(*request->scan_plans),
-					 n_plans));
-	size = size_add(size, ie_len);
+	size = size_add(struct_size(request, channels, n_channels), ie_len,
+			array_size(sizeof(*request->ssids), n_ssids),
+			array_size(sizeof(*request->match_sets), n_match_sets),
+			array_size(sizeof(*request->scan_plans), n_plans));
 	request = kzalloc(size, GFP_KERNEL);
 	if (!request)
 		return ERR_PTR(-ENOMEM);
Signature · 1 line
2.50.1