All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: david.laight.linux@gmail.com
Cc: Alexander Lobakin <aleksander.lobakin@intel.com>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] Fortify: Use C arithmetic not FIELD_xxx() in FORTIFY_REASON defines
Date: Mon, 12 Jan 2026 14:18:56 -0800	[thread overview]
Message-ID: <202601121415.CEB3C024@keescook> (raw)
In-Reply-To: <20251214125857.3308-1-david.laight.linux@gmail.com>

On Sun, Dec 14, 2025 at 12:58:57PM +0000, david.laight.linux@gmail.com wrote:
> From: David Laight <david.laight.linux@gmail.com>
> 
> 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>
> ---
> 
> Note that changing 'const u8 reason' to 'const unsigned int reason' generates
> better code - in this case removing 2 instructions (one in each of the called
> functions).
> 
>  include/linux/fortify-string.h | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
> 
> diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
> index b3b53f8c1b28..171982e53c9a 100644
> --- a/include/linux/fortify-string.h
> +++ b/include/linux/fortify-string.h
> @@ -2,7 +2,6 @@
>  #ifndef _LINUX_FORTIFY_STRING_H_
>  #define _LINUX_FORTIFY_STRING_H_
>  
> -#include <linux/bitfield.h>
>  #include <linux/bug.h>
>  #include <linux/const.h>
>  #include <linux/limits.h>
> @@ -10,10 +9,9 @@
>  #define __FORTIFY_INLINE extern __always_inline __gnu_inline __overloadable
>  #define __RENAME(x) __asm__(#x)
>  
> -#define FORTIFY_REASON_DIR(r)		FIELD_GET(BIT(0), r)
> -#define FORTIFY_REASON_FUNC(r)		FIELD_GET(GENMASK(7, 1), r)
> -#define FORTIFY_REASON(func, write)	(FIELD_PREP(BIT(0), write) | \
> -					 FIELD_PREP(GENMASK(7, 1), func))
> +#define FORTIFY_REASON_DIR(r)		((r) & 1)
> +#define FORTIFY_REASON_FUNC(r)		((r) >> 1)

Sure, we can do this. I agree, the preprocessor gunk is huge currently.
For the above, how about keeping with the original logic and use:

#define FORTIFY_REASON_FUNC(r)			(((r) & 0xF) >> 1)

> +#define FORTIFY_REASON(func, write)	((func) << 1 | (write))

and:

> +#define FORTIFY_REASON(func, write)	((func) << 1 | (write))

#define FORTIFY_REASON(func, write)	(((func) << 1 | ((write) & 1)) & 0xF)

so we're always getting processing a u8?

-Kees

-- 
Kees Cook

  reply	other threads:[~2026-01-12 22:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-14 12:58 [PATCH 1/1] Fortify: Use C arithmetic not FIELD_xxx() in FORTIFY_REASON defines david.laight.linux
2026-01-12 22:18 ` Kees Cook [this message]
2026-01-12 23:03   ` David Laight
2026-01-13 20:03 ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202601121415.CEB3C024@keescook \
    --to=kees@kernel.org \
    --cc=aleksander.lobakin@intel.com \
    --cc=david.laight.linux@gmail.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.