* [PATCH v2] kfence: don't allow static keys mode on x86
@ 2026-11-28 18:22 Chris Down
2026-11-29 11:36 ` Marco Elver
0 siblings, 1 reply; 3+ messages in thread
From: Chris Down @ 2026-11-28 18:22 UTC (permalink / raw)
To: Alexander Potapenko, Marco Elver
Cc: Dmitry Vyukov, Andrew Morton, Jonathan Corbet, Shuah Khan,
Randy Dunlap, kasan-dev, linux-mm, linux-kernel, linux-doc,
kernel-team
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@chrisdown.name>
---
v1 at https://lore.kernel.org/r/auA7FdubFEDVPB7v@chrisdown.name
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
help
Use static keys (static branches) to set up KFENCE allocations. This
option is only recommended when using very large sample intervals, or
base-commit: 23cd682622886800b912219e307cf46a99f69c10
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] kfence: don't allow static keys mode on x86
2026-11-28 18:22 [PATCH v2] kfence: don't allow static keys mode on x86 Chris Down
@ 2026-11-29 11:36 ` Marco Elver
2026-11-30 8:16 ` Peter Zijlstra
0 siblings, 1 reply; 3+ messages in thread
From: Marco Elver @ 2026-11-29 11:36 UTC (permalink / raw)
To: Chris Down
Cc: Alexander Potapenko, Dmitry Vyukov, Andrew Morton,
Jonathan Corbet, Shuah Khan, Randy Dunlap, kasan-dev, linux-mm,
linux-kernel, linux-doc, kernel-team, the arch/x86 maintainers
On Sat, 28 Nov 2026 at 19:22, Chris Down <chris@chrisdown.name> 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@chrisdown.name>
Can you remind me what concrete problem you encountered that prompted all this?
> ---
> v1 at https://lore.kernel.org/r/auA7FdubFEDVPB7v@chrisdown.name
>
> 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?
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] kfence: don't allow static keys mode on x86
2026-11-29 11:36 ` Marco Elver
@ 2026-11-30 8:16 ` Peter Zijlstra
0 siblings, 0 replies; 3+ messages in thread
From: Peter Zijlstra @ 2026-11-30 8:16 UTC (permalink / raw)
To: Marco Elver
Cc: Chris Down, Alexander Potapenko, Dmitry Vyukov, Andrew Morton,
Jonathan Corbet, Shuah Khan, Randy Dunlap, kasan-dev, linux-mm,
linux-kernel, linux-doc, kernel-team, the arch/x86 maintainers
On Sun, Nov 29, 2026 at 12:36:35PM +0100, Marco Elver wrote:
> 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.
Yeah, this is bad.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 8:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 18:22 [PATCH v2] kfence: don't allow static keys mode on x86 Chris Down
2026-11-29 11:36 ` Marco Elver
2026-11-30 8:16 ` Peter Zijlstra
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox