Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fortify: Use C arithmetic not FIELD_xxx() in FORTIFY_REASON defines

FIELD_GET() and FIELD_PREP() are mainly useful for hardware register
accesses, but here they are being used for some very simple oprations.

This wouldn't matter much, but they contain a lot of compile-time
checks (that really aren't needed here) that bloat the expansion
of FIELD_GET(GENMASK(7, 1), func) to over 18KB.
Even with the 'bloat reduced' FIELD_GET/PREP they are still hundreds of
characters.

Replace FIELD_GET(BIT(0), r) with ((r) & 1), FIELD_GET(GENMASK(7, 1), r) with
(r) >> 1), and (FIELD_PREP(BIT(0), write) | FIELD_PREP(GENMASK(7, 1), func))
with ((func) << 1 | (write)).

The generated code is the same, but it makes the .c file less obfuctaced,
the .i file much easier to read, and should marginally decrease compilation
time.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
Link: https://patch.msgid.link/20251214125857.3308-1-david.laight.linux@gmail.com
Signed-off-by: Kees Cook <kees@kernel.org>

authored by

David Laight and committed by
Kees Cook
995ddc58 cc34c669

+3 -5
+3 -5
include/linux/fortify-string.h
··· 2 2 #ifndef _LINUX_FORTIFY_STRING_H_ 3 3 #define _LINUX_FORTIFY_STRING_H_ 4 4 5 - #include <linux/bitfield.h> 6 5 #include <linux/bug.h> 7 6 #include <linux/const.h> 8 7 #include <linux/limits.h> ··· 9 10 #define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable 10 11 #define __RENAME(x) __asm__(#x) 11 12 12 - #define FORTIFY_REASON_DIR(r) FIELD_GET(BIT(0), r) 13 - #define FORTIFY_REASON_FUNC(r) FIELD_GET(GENMASK(7, 1), r) 14 - #define FORTIFY_REASON(func, write) (FIELD_PREP(BIT(0), write) | \ 15 - FIELD_PREP(GENMASK(7, 1), func)) 13 + #define FORTIFY_REASON_DIR(r) ((r) & 1) 14 + #define FORTIFY_REASON_FUNC(r) ((r) >> 1) 15 + #define FORTIFY_REASON(func, write) ((func) << 1 | (write)) 16 16 17 17 /* Overridden by KUnit tests. */ 18 18 #ifndef fortify_panic