[tip: core/entry] randomize_kstack: Provide add_random_kstack_offset_irqsoff()

tip-bot2 for Thomas Gleixner posted 1 patch 1 week, 6 days ago
include/linux/randomize_kstack.h | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
[tip: core/entry] randomize_kstack: Provide add_random_kstack_offset_irqsoff()
Posted by tip-bot2 for Thomas Gleixner 1 week, 6 days ago
The following commit has been merged into the core/entry branch of tip:

Commit-ID:     0b9a3057530cc1c3f87ce2ab9d1278164e81990f
Gitweb:        https://git.kernel.org/tip/0b9a3057530cc1c3f87ce2ab9d1278164e81990f
Author:        Thomas Gleixner <tglx@kernel.org>
AuthorDate:    Tue, 07 Jul 2026 21:06:02 +02:00
Committer:     Thomas Gleixner <tglx@kernel.org>
CommitterDate: Sun, 12 Jul 2026 12:38:00 +02:00

randomize_kstack: Provide add_random_kstack_offset_irqsoff()

add_random_kstack_offset() uses get/put_cpu_var() which is pointless
overhead when it is invoked from low level entry code with interrupts
disabled.

Provide a irqsoff() variant, which avoids that.

Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Reviewed-by: Radu Rendec <radu@rendec.net>
Reviewed-by: Kees Cook <kees@kernel.org>
Reviewed-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@gmail.com>
Link: https://patch.msgid.link/20260707190253.768842729@kernel.org
---
 include/linux/randomize_kstack.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/include/linux/randomize_kstack.h b/include/linux/randomize_kstack.h
index 024fc20..d8b939f 100644
--- a/include/linux/randomize_kstack.h
+++ b/include/linux/randomize_kstack.h
@@ -77,8 +77,27 @@ static __always_inline u32 get_kstack_offset(void)
 	}								\
 } while (0)
 
+/**
+ * add_random_kstack_offset_irqsoff - Increase stack utilization by a random offset.
+ *
+ * This should be used in the syscall entry path after user registers have been
+ * stored to the stack. Interrupts must be still disabled.
+ */
+#define add_random_kstack_offset_irqsoff()					\
+do {										\
+	lockdep_assert_irqs_disabled();						\
+	if (static_branch_maybe(CONFIG_RANDOMIZE_KSTACK_OFFSET_DEFAULT,		\
+				&randomize_kstack_offset)) {			\
+		u32 offset = prandom_u32_state(raw_cpu_ptr(&kstack_rnd_state));	\
+		u8 *ptr = __kstack_alloca(KSTACK_OFFSET_MAX(offset));		\
+		/* Keep allocation even after "ptr" loses scope. */		\
+		asm volatile("" :: "r"(ptr) : "memory");			\
+	}									\
+} while (0)
+
 #else /* CONFIG_RANDOMIZE_KSTACK_OFFSET */
 #define add_random_kstack_offset()		do { } while (0)
+#define add_random_kstack_offset_irqsoff()	do { } while (0)
 #endif /* CONFIG_RANDOMIZE_KSTACK_OFFSET */
 
 #endif