[RFC][PATCH 22/24] kernel/task: Add a helper to locate the top of the shadow stack

From: Dave Hansen

Date: Wed Oct 21 2026 - 15:57:12 EST



Just like the normal RSP stack, the shadow stack starts at the top of the
stack and grows down. Add a helper to calculate the top (exclusive)
address of the shadow stack.

Note that this is generic code without #ifdefs. The shadow_stack member
is present and 0 bytes long on configs without shadow stacks.

---

b/include/linux/sched/task_stack.h | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff -puN include/linux/sched/task_stack.h~generic-shstk-top-helper include/linux/sched/task_stack.h
--- a/include/linux/sched/task_stack.h~generic-shstk-top-helper 2026-10-21 12:36:43.603891930 -0700
+++ b/include/linux/sched/task_stack.h 2026-10-21 12:36:43.606891936 -0700
@@ -23,11 +23,6 @@ static __always_inline void *task_stack_
return task->stack;
}

-static inline union thread_union *task_thread_union(struct task_struct *tsk)
-{
- return (union thread_union *)task_stack_page(tsk);
-}
-
#define setup_thread_stack(new,old) do { } while(0)

static __always_inline unsigned long *end_of_stack(const struct task_struct *task)
@@ -119,4 +114,22 @@ static inline int kstack_end(void *addr)
return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
}

+static inline union thread_union *task_thread_union(struct task_struct *tsk)
+{
+ return (union thread_union *)task_stack_page(tsk);
+}
+
+/*
+ * Return the address of the top of the shadow stack for a task.
+ *
+ * Note that this is the address just above the last shadow stack
+ * slot. It is an invalid address and needs to be decremented by
+ * something before actual use.
+ */
+static inline void *task_shstk_top(struct task_struct *tsk)
+{
+ return task_thread_union(tsk)->stacks.shadow_stack +
+ sizeof(task_thread_union(tsk)->stacks.shadow_stack);
+}
+
#endif /* _LINUX_SCHED_TASK_STACK_H */
_