Skip to contentSkip to conversation

minmax: reduce egregious min/max macro expansion

PATCH In review Lorenzo Stoakes 4 participants 7 messages Sep 11, 2024

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

v2 cover minmax: reduce egregious min/max macro expansion

Avoid nested min()/max() which results in egregious macro expansion.

This issue was introduced by commit 867046cc7027 ("minmax: relax check to allow comparison between unsigned arguments and signed constants") [0].

Work has been done to address the issue of egregious min()/max() macro expansion in commit 22f546873149 ("minmax: improve macro expansion and type checking") and related, however it appears that some issues remain on more tightly constrained systems.

Adjust a few known-bad cases of deeply nested macros to avoid doing so to mitigate this. Porting the patch first proposed in [1] to Linus's tree.

Note that we use clamp_t() rather than clamp() when we otherwise could use the latter because it isn't certain than the clamp() won't expand into a more egregiously huge form, certainly prior to the recent fixes.

Running an allmodconfig build using the methodology described in [2] we observe a 35 MiB reduction in generated code.

The difference is much more significant prior to recent minmax fixes which were not backported. As per [1] prior these the reduction is more like 200 MiB.

This resolves an issue with slackware 15.0 32-bit compilation as reported by Richard Narron.

Presumably the min/max fixups would be difficult to backport, this series should be easier and fix's Richard's problem in 5.15.

[0]:https://lore.kernel.org/all/[email protected]/ [1]:https://lore.kernel.org/lkml/[email protected]/ [2]:https://lore.kernel.org/linux-mm/[email protected]/

v2:
* Split into separate patches to make backporting easier.
* Corrected type in sDIGIT_FITTING() argument.
* Added reviewed-by tags.

v1:
https://lore.kernel.org/all/[email protected]/

Lorenzo Stoakes (3):
minmax: reduce min/max macro expansion in mvpp2 driver
minmax: reduce min/max macro expansion in skbuff
minmax: reduce min/max macro expansion in atomisp driver

 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(-)

--
2.46.0

#1 lore raw

PATCH v2 1/3 minmax: reduce min/max macro expansion in mvpp2 driver

Avoid unnecessary nested min()/max() which reults in egregious macro expansion. Use clamp_t() as this introduces the least possible expansion.

Not doing so results in an impact on build times.

Fixes 867046cc7027 ("minmax: relax check to allow comparison between unsigned arguments and signed constants")
---
 drivers/net/ethernet/marvell/mvpp2/mvpp2.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
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)

--
2.46.0

#2 lore raw

PATCH v2 2/3 minmax: reduce min/max macro expansion in skbuff

In reply to Lorenzo Stoakes

Avoid unnecessary nested min()/max() which reults in egregious macro expansion.

The nesting occurs when NET_SKB_PAD is used in a min() or max() invocation, for instance, various ethernet drivers wrap it in a max().

Not doing so results in an impact on build times.

Fixes 867046cc7027 ("minmax: relax check to allow comparison between unsigned arguments and signed constants")
---
 include/linux/skbuff.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
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.46.0

#3 lore raw

Re: [PATCH hotfix 6.11 v2 2/3] minmax: reduce min/max macro expansion in skbuff

FYI: kernel test robot notices the stable kernel rule is not satisfied.

Hi,

Thanks for your patch.

FYI: kernel test robot notices the stable kernel rule is not satisfied.

The check is based on https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html#option-3

Rule: The upstream commit ID must be specified with a separate line above the commit text.
Subject: [PATCH hotfix 6.11 v2 2/3] minmax: reduce min/max macro expansion in skbuff

Please ignore this mail if the patch is not relevant for upstream.

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

PATCH v2 3/3 minmax: reduce min/max macro expansion in atomisp driver

In reply to Lorenzo Stoakes

Avoid unnecessary nested min()/max() which results in egregious macro expansion. Use clamp_t() as this introduces the least possible expansion.

Not doing so results in an impact on build times.

This resolves an issue with slackware 15.0 32-bit compilation as reported by Richard Narron.

Presumably the min/max fixups would be difficult to backport, this patch should be easier and fix's Richard's problem in 5.15.

Fixes 867046cc7027 ("minmax: relax check to allow comparison between unsigned arguments and signed constants")
---
 .../staging/media/atomisp/pci/sh_css_frac.h   | 26 ++++++++++++++-----
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/staging/media/atomisp/pci/sh_css_frac.h +19 -7
index b90b5b330dfa..8ba65161f7a9 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(int 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 int uDIGIT_FITTING(unsigned int 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 int, v, uISP_VAL_MIN, uISP_VAL_MAX);
+}
 
 #endif /* __SH_CSS_FRAC_H */

--
2.46.0

Reviewed-by on v2 3/3
#5 lore raw

Re: [PATCH hotfix 6.11 v2 3/3] minmax: reduce min/max macro expansion in atomisp driver

Hi,

Quoted 8 lines · Lorenzo Stoakes

Avoid unnecessary nested min()/max() which results in egregious macro expansion. Use clamp_t() as this introduces the least possible expansion.

Not doing so results in an impact on build times.

This resolves an issue with slackware 15.0 32-bit compilation as reported by Richard Narron.

Quoted 8 lines

Presumably the min/max fixups would be difficult to backport, this patch should be easier and fix's Richard's problem in 5.15.

Fixes 867046cc7027 ("minmax: relax check to allow comparison between unsigned arguments and signed constants")

Thanks, patch looks good to me:

Regards,

Hans

Quoted 43 lines
---
 .../staging/media/atomisp/pci/sh_css_frac.h   | 26 ++++++++++++++-----
 1 file changed, 19 insertions(+), 7 deletions(-)
drivers/staging/media/atomisp/pci/sh_css_frac.h +19 -8
index b90b5b330dfa..8ba65161f7a9 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(int 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 int uDIGIT_FITTING(unsigned int 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 int, v, uISP_VAL_MIN, uISP_VAL_MAX);
+}
 
 #endif /* __SH_CSS_FRAC_H */
--

2.46.0

#6 lore raw

Re: [PATCH hotfix 6.11 v2 3/3] minmax: reduce min/max macro expansion in atomisp driver

In reply to Lorenzo Stoakes
Quoted 3 lines · Lorenzo Stoakes

Avoid unnecessary nested min()/max() which results in egregious macro expansion. Use clamp_t() as this introduces the least possible expansion.

I took this (single) patch directly, since that's the one that actually causes build problems in limited environments (admittedly not in current git with the more invasive min/max cleanups, but in order to be back-ported).

Plus it cleans up the code with more legible inline functions, rather than just doing some minimal syntactic changes. I expanded on the commit message to say that.

The two others I'll leave for now and see what maintainers of their respective areas think.

Linus