Skip to contentSkip to conversation

Subject: [PATCH v2 0/2] x86/kstack_erase: Faster stack poisoning, plus a startup build fix

PATCH New Usama Arif 1 participant 3 messages Nov 23 x86

lore
Share & tools
lore

Usama Arif

author
#0 lore raw

v2 cover Subject: [PATCH v2 0/2] x86/kstack_erase: Faster stack poisoning, plus a startup build fix

With KSTACK_ERASE, every return to user space poisons the part of the task stack used since the previous return, one 8-byte store at a time. On Meta hosts running many small VMs, that loop costs about 1.5% of non-idle host kernel cycles. Most of it is on the way out of futex, epoll, procfs and socket syscalls that go 1-2 KiB deep.

Patch 2 makes x86-64 use memset64() for spans above 512 bytes, through the arch hook that s390 already uses. Shorter spans keep the loop, and the range and value written do not change.

Patch 1 is an independent build fix I found while testing patch 2 with a small KSTACK_ERASE_TRACK_MIN_SIZE. The startup code still uses the pre-rename DISABLE_STACKLEAK_PLUGIN, so with Clang, AMD memory encryption and a tracking minimum below about 90 bytes, vmlinux fails to link. It can be applied on its own.

Zen 5c KVM guest, one vCPU, before -> after:

  stackleak_erase() per return to user       68 ns -> 47 ns
  futex wake/wait ping-pong                  6% faster
  Unix socket sendmsg()/recvmsg() ping-pong  6% faster
  open+read+close of /proc/self/stat         8% faster
  getppid() (spans below 512 bytes)          within 1%

v1 -> v2: https://lore.kernel.org/r/[email protected]
- Patch 1: add Nick's Reviewed-by.
- Patch 2: use memset64() instead of inline asm and make it __always_inline (Nick, Peter). KMSAN kernels keep the generic loop, as memset64() is out of line there.
- Patch 2: keep the word loop up to 512 bytes; this is the v2.1 variant from the v1 thread. Unrolled loops, Duff's device, a 64-byte aligned REP STOSQ and REP STOSQ for all lengths (Peter, David) all came within 1 ns per erase of it. Most of the time left is the search for the poison, not the fill. This current version is the cleanest one IMO.

Usama Arif (2):
x86/boot/startup: Disable kernel stack depth tracking x86/kstack_erase: Use memset64() for large stack fills

 arch/x86/boot/startup/Makefile   |  2 +-
 arch/x86/include/asm/processor.h | 21 +++++++++++++++++++++
 arch/x86/include/asm/string_64.h |  2 +-
 3 files changed, 23 insertions(+), 2 deletions(-)
Signature · 1 line
2.53.1-Meta
author
#1 lore raw

PATCH v2 1/2 x86/boot/startup: Disable kernel stack depth tracking

Commit 57fbad15c2ee ("stackleak: Rename STACKLEAK to KSTACK_ERASE") renamed DISABLE_STACKLEAK_PLUGIN to DISABLE_KSTACK_ERASE everywhere except in arch/x86/boot/startup/Makefile. The old name now expands to nothing, so the startup code is built with stack depth tracking enabled.

The startup code is linked with every symbol prefixed by __pi_, so that it cannot call into the rest of the kernel by accident. It has no alias for the tracking callback, and tracking stack depth there is of no use.

At the default KSTACK_ERASE_TRACK_MIN_SIZE of 100 no startup function is large enough to be tracked, so this went unnoticed. With Clang, CONFIG_AMD_MEM_ENCRYPT=y and a minimum below about 90 bytes, pvalidate_4k_page(), snp_cpuid() and do_vc_no_ghcb() get a call to __sanitizer_cov_stack_depth() and vmlinux fails to link:

ld.lld: error: undefined symbol: __pi___sanitizer_cov_stack_depth

Use DISABLE_KSTACK_ERASE, which disables tracking with either compiler.

Fixes 57fbad15c2ee ("stackleak: Rename STACKLEAK to KSTACK_ERASE")
---
 arch/x86/boot/startup/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/boot/startup/Makefile +1 -1
index 5e499cfb29b5c0..0815105e7205c7 100644
@@ -3,7 +3,7 @@
 KBUILD_AFLAGS		+= -D__DISABLE_EXPORTS
 KBUILD_CFLAGS		+= -D__DISABLE_EXPORTS -mcmodel=small -fPIC \
 			   -Os -DDISABLE_BRANCH_PROFILING \
-			   $(DISABLE_STACKLEAK_PLUGIN) \
+			   $(DISABLE_KSTACK_ERASE) \
 			   $(DISABLE_LATENT_ENTROPY_PLUGIN) \
 			   -fno-stack-protector -D__NO_FORTIFY \
 			   -fno-jump-tables \
Signature · 1 line
2.53.1-Meta
author
#2 lore raw

PATCH v2 2/2 x86/kstack_erase: Use memset64() for large stack fills

In reply to Usama Arif

With KSTACK_ERASE, every return to user space poisons the part of the task stack that was used since the previous return. The generic __stackleak_poison() does that one word per loop iteration, so a syscall that went 1-2 KiB deep pays for 128-256 separate stores. On Meta hosts running many small VMs, stackleak_erase() accounts for about 1.5% of non-idle host kernel cycles.

Override __stackleak_poison() on x86-64, as s390 already does, through the hook added by commit 491a78663e03 ("stackleak: allow to specify arch specific stackleak poison function"). Fill spans larger than 512 bytes with memset64(), which is a single REP STOSQ, and keep the word loop for shorter ones, where the startup cost of REP STOSQ does not pay off. REP STOSQ starts to win at about 320 bytes on Cooper Lake and 430 bytes on Zen 5c, so on both no span gets slower.

stackleak_erase() is noinstr, so make memset64() __always_inline. With KMSAN, x86 does not provide memset64() and the out-of-line lib/string.c version would be called, so KMSAN kernels keep the generic loop.

In a Zen 5c KVM guest, stackleak_erase() got 32% cheaper per return to user space (68 ns to 47 ns, scan included) over a futex, epoll, procfs and Unix socket syscall mix. Same-vCPU futex wake/wait and Unix socket sendmsg()/recvmsg() ping-pong round trips got about 6% faster.

---
 arch/x86/include/asm/processor.h | 21 +++++++++++++++++++++
 arch/x86/include/asm/string_64.h |  2 +-
 2 files changed, 22 insertions(+), 1 deletion(-)
arch/x86/include/asm/processor.h +21 -0
index 77488ff5eb8c7c..3d205ae8566114 100644
@@ -28,6 +28,7 @@ struct vm86;
 #include <asm/vmxfeatures.h>
 #include <asm/vdso/processor.h>
 #include <asm/shstk.h>
+#include <asm/string.h>
 
 #include <linux/personality.h>
 #include <linux/cache.h>
@@ -580,6 +581,26 @@ static __always_inline bool on_thread_stack(void)
 			       current_stack_pointer) < THREAD_SIZE;
 }
 
+#if defined(CONFIG_X86_64) && !defined(CONFIG_KMSAN)
+#define __stackleak_poison __stackleak_poison
+static __always_inline void __stackleak_poison(unsigned long erase_low,
+					       unsigned long erase_high,
+					       unsigned long poison)
+{
+	/* Short spans do not amortize the startup cost of REP STOSQ. */
+	if (erase_high <= erase_low + 512) {
+		while (erase_low < erase_high) {
+			*(unsigned long *)erase_low = poison;
+			erase_low += sizeof(unsigned long);
+		}
+		return;
+	}
+
+	memset64((u64 *)erase_low, poison,
+		 (erase_high - erase_low) / sizeof(u64));
+}
+#endif
+
 #ifdef CONFIG_PARAVIRT_XXL
 #include <asm/paravirt.h>
 #else
arch/x86/include/asm/string_64.h +1 -1
index 2dae70c2b01465..73cc91254b1381 100644
@@ -54,7 +54,7 @@ static inline void *memset32(uint32_t *s, uint32_t v, size_t n)
 }
 
 #define __HAVE_ARCH_MEMSET64
-static inline void *memset64(uint64_t *s, uint64_t v, size_t n)
+static __always_inline void *memset64(uint64_t *s, uint64_t v, size_t n)
 {
 	const auto s0 = s;
 	asm volatile (
Signature · 1 line
2.53.1-Meta