summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorArd Biesheuvel <ard.biesheuvel@linaro.org>2018-08-23 19:58:10 -0700
committerSascha Hauer <s.hauer@pengutronix.de>2018-08-24 10:23:30 +0200
commitfd6bdb1c87588b4619e82bedb98a389a70dc3cd3 (patch)
tree9bcc88d6fc17ff518974acaabe0540528d4a5139 /include
parent079f92099a694544a04e619a3748da3defe4f4b4 (diff)
downloadbarebox-fd6bdb1c87588b4619e82bedb98a389a70dc3cd3.tar.gz
barebox-fd6bdb1c87588b4619e82bedb98a389a70dc3cd3.tar.xz
log2: make order_base_2() behave correctly on const input value zero
The function order_base_2() is defined (according to the comment block) as returning zero on input zero, but subsequently passes the input into roundup_pow_of_two(), which is explicitly undefined for input zero. This has gone unnoticed until now, but optimization passes in GCC 7 may produce constant folded function instances where a constant value of zero is passed into order_base_2(), resulting in link errors against the deliberately undefined '____ilog2_NaN'. So update order_base_2() to adhere to its own documented interface. [ See http://marc.info/?l=linux-kernel&m=147672952517795&w=2 and follow-up discussion for more background. The gcc "optimization pass" is really just broken, but now the GCC trunk problem seems to have escaped out of just specially built daily images, so we need to work around it in mainline. - Linus ] Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> [andrew.smirnov@gmail.com: Applied kernel patch to Barebox as-is] Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'include')
-rw-r--r--include/linux/log2.h13
1 files changed, 12 insertions, 1 deletions
diff --git a/include/linux/log2.h b/include/linux/log2.h
index 36519e3aa3..da4f60f948 100644
--- a/include/linux/log2.h
+++ b/include/linux/log2.h
@@ -203,6 +203,17 @@ unsigned long __rounddown_pow_of_two(unsigned long n)
* ... and so on.
*/
-#define order_base_2(n) ilog2(roundup_pow_of_two(n))
+static inline __attribute_const__
+int __order_base_2(unsigned long n)
+{
+ return n > 1 ? ilog2(n - 1) + 1 : 0;
+}
+#define order_base_2(n) \
+( \
+ __builtin_constant_p(n) ? ( \
+ ((n) == 0 || (n) == 1) ? 0 : \
+ ilog2((n) - 1) + 1) : \
+ __order_base_2(n) \
+)
#endif /* _LINUX_LOG2_H */