Re: [PATCH v2] kfence: don't allow static keys mode on x86

From: Marco Elver

Date: Sun Nov 29 2026 - 06:37:39 EST


On Sat, 28 Nov 2026 at 19:22, Chris Down <chris@xxxxxxxxxxxxxx> wrote:
>
> KFENCE can gate allocations by toggling a static key for each sample, or
> by leaving the key enabled and checking an allocation gate. Static keys
> mode saves a load and compare per allocation, but sends IPIs to all CPUs
> each time it toggles the key, which is twice per sample interval.
>
> On x86 the saving is too small to be worth that. In an 8 CPU x86 VM, a
> kmem_cache_alloc()/kmem_cache_free() pair takes 8.0ns in static keys
> mode, which is the same as with KFENCE off. With the dynamic branch it
> takes 8.4ns. A kmalloc()/kfree() pair takes 13.3ns in all three.
>
> So static keys mode saves about half a nanosecond per allocation at
> best. At a million allocations a second on a CPU, that accounts for
> 0.05% of it. Toggling the key, on the other hand, takes milliseconds on
> machines with hundreds of CPUs and can hurt tail latencies, which is why
> commit 4f612ed3f748 ("kfence: default to dynamic branch instead of
> static keys mode") made the dynamic branch the default.
>
> Given this, let's stop offering CONFIG_KFENCE_STATIC_KEYS on x86. x86
> configs that have it set will get the dynamic branch from now on. Other
> architectures keep the option for now, because we don't have numbers for
> them yet.
>
> Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>

Can you remind me what concrete problem you encountered that prompted all this?

> ---
> v1 at https://lore.kernel.org/r/auA7FdubFEDVPB7v@xxxxxxxxxxxxxx
>
> v2: Drop the boot parameter and stop offering CONFIG_KFENCE_STATIC_KEYS
> on x86 instead (Marco)
>
> Documentation/dev-tools/kfence.rst | 3 ++-
> lib/Kconfig.kfence | 1 +
> 2 files changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/Documentation/dev-tools/kfence.rst b/Documentation/dev-tools/kfence.rst
> index b03d1201ddae..88acca8a00e6 100644
> --- a/Documentation/dev-tools/kfence.rst
> +++ b/Documentation/dev-tools/kfence.rst
> @@ -264,7 +264,8 @@ through the main allocator's fast-path by relying on static branches via the
> static keys infrastructure. The static branch is toggled to redirect the
> allocation to KFENCE. Depending on sample interval, target workloads, and
> system architecture, this may perform better than the simple dynamic branch.
> -Careful benchmarking is recommended.
> +Careful benchmarking is recommended. This option is not available on x86, where
> +it saves too little to be worth the IPIs that toggling the static branch sends.
>
> KFENCE objects each reside on a dedicated page, at either the left or right
> page boundaries selected at random. The pages to the left and right of the
> diff --git a/lib/Kconfig.kfence b/lib/Kconfig.kfence
> index 6fbbebec683a..0b11010934b6 100644
> --- a/lib/Kconfig.kfence
> +++ b/lib/Kconfig.kfence
> @@ -60,6 +60,7 @@ config KFENCE_DEFERRABLE
> config KFENCE_STATIC_KEYS
> bool "Use static keys to set up allocations" if EXPERT
> depends on JUMP_LABEL
> + depends on !X86 # Saves too little to be worth the IPI broadcasts

One thing of note: this option is already "if EXPERT", which implies
DEBUG_KERNEL, so who are we trying to save from making a stupid
choice? If an "expert" sysadmin decides this is what they want, why
stop them?
For production kernels, the general notion of selecting EXPERT or
DEBUG_KERNEL has always been a little odd, so that begs the question:
why not leave it just EXPERT-selectable?
What are we gaining by restricting X86 if it's already constrained with !EXPERT?

As I mentioned in the other thread, I favor ensuring this is not
selectable on X86, but maybe it's already sufficiently constrained?

The main problem is that x86 systems that are used for testing are now
artificially constrained from covering these codepaths, to the
detriment of other architectures where this option is still valid.

Having pondered this a bit longer, I'm quite split on what to do. One
option is to leave as-is, but does knowing it's constrained by EXPERT
cover the case you were worried about?