When CONFIG_ARCH_HAS_VARIABLE_STACK_SIZE is enabled, the kernel stack
may not be fully populated with the THREAD_SIZE virtual area.

Teach task_stack_size() to return the size of the vm area and not a
fixed size.

This propagates to end_of_stack() which makes functions like
stack_not_used() and set_task_stack_end_magic() to use the lowest
valid stack address instead of THREAD_SIZE which would cause data
aborts.

Signed-off-by: Mostafa Saleh <[email protected]>
---
 arch/Kconfig                     | 10 ++++++++++
 include/linux/sched/task_stack.h |  5 +++++
 2 files changed, 15 insertions(+)

diff --git a/arch/Kconfig b/arch/Kconfig
index 45c657772362..d6fa2ccb0599 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -431,6 +431,16 @@ config HAVE_ASM_MODVERSIONS
          <asm/asm-prototypes.h> to support the module versioning for symbols
          exported from assembly code.
 
+config ARCH_HAS_VARIABLE_STACK_SIZE
+       bool
+       depends on VMAP_STACK
+       depends on !STACK_GROWSUP
+       depends on THREAD_INFO_IN_TASK
+       help
+         An arch should select this if it supports dynamically configuring the
+         kernel stack size.
+         For example the kernel stack size can be from the command line.
+
 config HAVE_REGS_AND_STACK_ACCESS_API
        bool
        help
diff --git a/include/linux/sched/task_stack.h b/include/linux/sched/task_stack.h
index 4a808f9c4631..07d73adaecd1 100644
--- a/include/linux/sched/task_stack.h
+++ b/include/linux/sched/task_stack.h
@@ -10,6 +10,7 @@
 #include <linux/magic.h>
 #include <linux/refcount.h>
 #include <linux/kasan.h>
+#include <linux/vmalloc.h>
 
 #ifdef CONFIG_THREAD_INFO_IN_TASK
 
@@ -96,6 +97,10 @@ static inline unsigned long *end_of_stack(const struct 
task_struct *p)
  */
 static __always_inline unsigned long task_stack_size(const struct task_struct 
*task)
 {
+#ifdef CONFIG_ARCH_HAS_VARIABLE_STACK_SIZE
+       if (task->stack_vm_area)
+               return task->stack_vm_area->nr_pages * PAGE_SIZE;
+#endif
        return THREAD_SIZE;
 }
 
-- 
2.56.0.rc1.315.gc6ed9934b7-goog


Reply via email to