arm64: Set kernel stack size from cmdline
Subsystems (10)
cover arm64: Set kernel stack size from cmdline
Summary
=======
This patch series adds the ability to configure the kernel stack size for arm64 from the kernel command line.
On some systems (specifically, Android), this provides a mechanism to reduce the kernel memory consumption, as the default kernel stack size of 16kB (with 4kB pages) can result in ~100MB of allocated memory, despite the fact that most threads do not come close to exhausting their allocation [1].
There have been multiple alternative attempts to improve this situation, including for x86 and cloud workloads. However, these have typically focussed on more dynamic behaviours such as allocating kernel stack pages lazily (based on faults) [2] or reclaiming unused stack pages from blocked tasks [1], whereas this series focusses on making the kernel stack size configurable without changing the way in which it is allocated.
It also permits a 12kB stack size, which is currently not supported by arm64's stack overflow checking logic.
This series re-uses the first part of the dynamic stack patches which makes it possible to partially back the VA space of the stack (THREAD_SIZE) with memory, but never attempts to dynamically grow the stack.
The size of the stack (<= THREAD_SIZE) is determined from the kernel command line and is not further configurable at runtime, all threads on the system (except the init_task, see below) will use the size set from the command line.
Patches
=======
The patches have dependency on the ongoing arm re-work[3] to move the overflow stack to sp_el1 allowing the early kernel exception to use it without clobbering any registers.
- Patches 01-09: have no functional change, they abstract the code dealing with the stack to avoid hardcoding the stack size
- Patches 10-13: Introduce the new ARCH_HAS_VARIABLE_STACK_SIZE and make the kernel deal with stack sizes set in the run time.
- Patches 14-15: arm64 selecting ARCH_HAS_VARIABLE_STACK_SIZE and setting the kernel stack size from the command line.
KASAN
======
The KASAN stack helpers keep unpoisoning the whole THREAD_SIZE area. As they only write shadow memory which is populated for the whole vmap area.
init_task
=========init_task is the only kernel thread that has a full stack allocation as it is allocated statically. However, the additional stack pages are not usable because the overflow check on exception entry will continue to check against the configured stack size.
Future work
===========
There are multiple paths that can build on this
- Per task stack size (either via an in-kernel API for kthreads or potentially a prctl() for userspace to configure)
- It is still possible to build on top of this on the fault path to add dynamic stacks if the challenges raised in [2] can be solved.
Will Deacon will host a discussion at LPC next week [4]
Testing
=======
I tested on Lenovo Mini-x gen 10 (Qualcomm X1 CPU).
With configs KMEMLEAK, DEBUG_STACK_USAGE, SCHED_STACK_END_CHECK.
1) 4kB kernel - 16kB stack (default)
2) 4kB kernel - 8kB stack
3) 4kB kernel - 12kB stack
4) 64kB kernel - 64kB stack
With running VMs with KVM, stress-ng and LKDTM.
16kB tests done on Qemu.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lpc.events/event/20/contributions/2419/
David Stevens (3):
fork: Don't assume fully populated stack during reuse
fork: Move vm_stack to the beginning of the stack
fork: Move vmap stack freeing to work queue
Mostafa Saleh (9):
sched/task_stack: Add helpers for stack high/low
exit: Don't assume the kernel stack size
usercopy: Don't assume the kernel stack size
mm: kmemleak: Don't assume the kernel stack size
arm64: Don't assume the kernel stack size sched/task_stack: Introduce ARCH_HAS_VARIABLE_STACK_SIZE
fork: Implement partial VMAP stack allocation
arm64: mm: Relax kernel stack alignment
arm64: mm: Set stack size from the kernel command line
Pasha Tatashin (3):
fork: Remove assumption that vm_area->nr_pages equals to THREAD_SIZE
fork: Separate vmap stack allocation and free calls mm/vmalloc: Add a get_vm_area_node()
.../admin-guide/kernel-parameters.txt | 7 + arch/Kconfig | 10 ++ arch/arm64/Kconfig | 1 + arch/arm64/include/asm/memory.h | 19 ++- arch/arm64/include/asm/stacktrace.h | 4 +- arch/arm64/kernel/entry.S | 91 ++++++++++-- arch/arm64/kernel/setup.c | 24 ++++ arch/arm64/kernel/traps.c | 5 +- include/linux/sched/task_stack.h | 61 +++++++- include/linux/thread_info.h | 8 ++ include/linux/vmalloc.h | 3 + kernel/exit.c | 2 +- kernel/fork.c | 134 +++++++++++++++--- mm/kmemleak.c | 2 +- mm/usercopy.c | 4 +- mm/vmalloc.c | 24 ++++ 16 files changed, 348 insertions(+), 51 deletions(-)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 1/15 fork: Remove assumption that vm_area->nr_pages equals to THREAD_SIZE
From: Pasha Tatashin <[email protected]>
In many places number of pages in the stack is determined via (THREAD_SIZE / PAGE_SIZE). There is also a BUG_ON() that ensures that (THREAD_SIZE / PAGE_SIZE) is indeed equals to vm_area->nr_pages.
In the next patches the kernel stack size will not be a compile time constant and would be less than or equal THREAD_SIZE based on a command line arg. Therefore, use vm_area->nr_pages to determine the actual number of pages allocated in stack.
--- kernel/fork.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
kernel/fork.c +3 -5
@@ -315,9 +315,7 @@ static int memcg_charge_kernel_stack(struct vm_struct *vm_area)int ret;int nr_charged =0 ; -BUG_ON (vm_area->nr_pages !=THREAD_SIZE /PAGE_SIZE ); - -for (i =0 ; i <THREAD_SIZE /PAGE_SIZE ; i++) { +for (i =0 ; i < vm_area->nr_pages; i++) { ret =memcg_kmem_charge_page (vm_area->pages[i],GFP_KERNEL ,0 );if (ret) goto err;
@@ -488,7 +486,7 @@ static void account_kernel_stack(struct task_struct *tsk, int account)struct vm_struct *vm_area =task_stack_vm_area (tsk);int i; -for (i =0 ; i <THREAD_SIZE /PAGE_SIZE ; i++) +for (i =0 ; i < vm_area->nr_pages; i++)mod_lruvec_page_state (vm_area->pages[i],NR_KERNEL_STACK_KB , account * (PAGE_SIZE /1024 )); }else {
@@ -509,7 +507,7 @@ void exit_task_stack_account(struct task_struct *tsk)int i; vm_area =task_stack_vm_area (tsk); -for (i =0 ; i <THREAD_SIZE /PAGE_SIZE ; i++) +for (i =0 ; i < vm_area->nr_pages; i++)memcg_kmem_uncharge_page (vm_area->pages[i],0 ); } }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 2/15 fork: Don't assume fully populated stack during reuse
In reply to Mostafa SalehFrom: David Stevens <[email protected]>
In preparation for partially populated kernel stacks, don't assume that vm_area->nr_pages matches THREAD_SIZE when clearing a stack for reuse.
Note that the VA range is still THREAD_SIZE in that case but it might not be fully allocated.
--- kernel/fork.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/fork.c +6 -1
@@ -335,11 +335,16 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node) vm_area =alloc_thread_stack_node_from_cache (tsk, node);if (vm_area) { +unsigned long offset =0 ; +if (memcg_charge_kernel_stack (vm_area)) {vfree (vm_area->addr);return -ENOMEM ; } +if (!IS_ENABLED (CONFIG_STACK_GROWSUP )) + offset =THREAD_SIZE - vm_area->nr_pages *PAGE_SIZE ; +/* Reset stack metadata. */ if (!kasan_hw_tags_enabled ())kasan_unpoison_range (vm_area->addr,THREAD_SIZE );
@@ -347,7 +352,7 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node) stack =kasan_reset_tag (vm_area->addr);/* Clear stale pointers from reused stack. */ -clear_pages (vm_area->addr, vm_area->nr_pages); +clear_pages (vm_area->addr + offset, vm_area->nr_pages); tsk->stack_vm_area = vm_area; tsk->stack = stack;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 3/15 fork: Move vm_stack to the beginning of the stack
In reply to Mostafa SalehFrom: David Stevens <[email protected]>
The vm_stack struct used to free stacks via an RCU callback is stored directly in the stack being freed. Make sure it's stored at the beginning of the stack regardless of stack growth direction, to avoid faults on partially allocated stacks.
--- kernel/fork.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/fork.c +6 -1
@@ -285,7 +285,12 @@ static void thread_stack_free_rcu(struct rcu_head *rh)static void thread_stack_delayed_free (struct task_struct *tsk) { -struct vm_stack *vm_stack = tsk->stack; +struct vm_stack *vm_stack; + +if (IS_ENABLED (CONFIG_STACK_GROWSUP )) + vm_stack = tsk->stack; +else + vm_stack = tsk->stack +THREAD_SIZE -sizeof (*vm_stack); vm_stack->stack_vm_area = tsk->stack_vm_area;call_rcu (&vm_stack->rcu, thread_stack_free_rcu);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 4/15 fork: Separate vmap stack allocation and free calls
In reply to Mostafa SalehFrom: Pasha Tatashin <[email protected]>
In preparation for the partially populated stacks, separate out the __vmalloc_node and vfree calls from the vmap based stack allocations.
--- kernel/fork.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-)
kernel/fork.c +23 -12
@@ -272,6 +272,21 @@ static bool try_release_thread_stack_to_cache(struct vm_struct *vm_area)return false; } +static inline struct vm_struct *alloc_vmap_stack (int node) +{ +void *stack; + + stack =__vmalloc_node (THREAD_SIZE ,THREAD_ALIGN ,GFP_VMAP_STACK , + node,__builtin_return_address (0 )); + +return stack ?find_vm_area (stack) :NULL ; +} + +static inline void free_vmap_stack (struct vm_struct *vm_area) +{ +vfree (vm_area->addr); +} +static void thread_stack_free_rcu (struct rcu_head *rh) { struct vm_stack *vm_stack = container_of(rh, struct vm_stack, rcu);
@@ -280,7 +295,7 @@ static void thread_stack_free_rcu(struct rcu_head *rh)if (try_release_thread_stack_to_cache (vm_stack->stack_vm_area))return ; -vfree (vm_area->addr); +free_vmap_stack (vm_area); }static void thread_stack_delayed_free (struct task_struct *tsk)
@@ -307,7 +322,7 @@ static int free_vm_stack_cache(unsigned int cpu)if (!vm_area)continue ; -vfree (vm_area->addr); +free_vmap_stack (vm_area); cached_vm_stack_areas[i] =NULL ; }
@@ -336,14 +351,14 @@ static int memcg_charge_kernel_stack(struct vm_struct *vm_area)static int alloc_thread_stack_node (struct task_struct *tsk,int node) {struct vm_struct *vm_area; -void *stack; vm_area =alloc_thread_stack_node_from_cache (tsk, node);if (vm_area) {unsigned long offset =0 ; +void *stack;if (memcg_charge_kernel_stack (vm_area)) { -vfree (vm_area->addr); +free_vmap_stack (vm_area);return -ENOMEM ; }
@@ -364,15 +379,12 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)return 0 ; } - stack =__vmalloc_node (THREAD_SIZE ,THREAD_ALIGN , -GFP_VMAP_STACK , - node,__builtin_return_address (0 )); -if (!stack) + vm_area =alloc_vmap_stack (node); +if (!vm_area)return -ENOMEM ; - vm_area =find_vm_area (stack);if (memcg_charge_kernel_stack (vm_area)) { -vfree (stack); +free_vmap_stack (vm_area);return -ENOMEM ; }/*
@@ -381,8 +393,7 @@ static int alloc_thread_stack_node(struct task_struct *tsk, int node)* so cache the vm_struct. */ tsk->stack_vm_area = vm_area; - stack =kasan_reset_tag (stack); - tsk->stack = stack; + tsk->stack =kasan_reset_tag (vm_area->addr);return 0 ; }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 5/15 sched/task_stack: Add helpers for stack high/low
In reply to Mostafa SalehSoon, memory mapped for a kernel stack can be less than THREAD_SIZE. However, many parts of the kernel assume it is.
Add new helpers for the kernel stack that can be called regardless of CONFIG_STACK_GROWSUP or the kernel stack size:
- task_stack_low(): Lowest usable address on the stack.
- task_stack_high(): Highest usable address on the stack.
- task_stack_size(): The size of a task kernel stack, it is still THREAD_SIZE at the moment but that will change soon.
--- include/linux/sched/task_stack.h | 56 ++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-)
include/linux/sched/task_stack.h +53 -3
@@ -13,6 +13,8 @@#ifdef CONFIG_THREAD_INFO_IN_TASK +static __always_inline void *task_stack_low(const struct task_struct *task); +/* * When accessing the stack of a non-current task that might exit, use * try_get_task_stack() instead. task_stack_page will return a pointer
@@ -30,7 +32,7 @@ static __always_inline unsigned long *end_of_stack(const struct task_struct *tas#ifdef CONFIG_STACK_GROWSUP return (unsigned long *)((unsigned long )task->stack +THREAD_SIZE ) -1 ;#else -return task->stack; +return task_stack_low (task);#endif }
@@ -64,6 +66,53 @@ static inline unsigned long *end_of_stack(const struct task_struct *p)#endif +/* +* Kernel stack layout, higher addresses at the top. task_stack_page() is +* the start of the THREAD_SIZE area, task_stack_low() => task_stack_high() +* is the usable stack. +* +* Case 1) Stack grows down: Only the top task_stack_size() bytes are mapped: +* +* +-------------+ <- task_stack_high() = task_stack_page() + THREAD_SIZE +* | stack | +* | | | task_stack_size() bytes +* | v | +* +-------------+ <- task_stack_low() = end_of_stack() (*) +* | may be | +* | unmapped | THREAD_SIZE - task_stack_size() bytes +* +-------------+ <- task_stack_page() +* +* (*) Depending on CONFIG_THREAD_INFO_IN_TASK, end_of_stack() might be just +* above it. +* +* Case 2) Stack grows up (CONFIG_STACK_GROWSUP): The whole area is used. +* No support for partially mapped stacks. +* +* +-------------+ <- task_stack_high() = task_stack_page() + THREAD_SIZE +* | ^ | end_of_stack() is the last unsigned long below it +* | | | +* | stack | THREAD_SIZE bytes +* +-------------+ <- task_stack_low() = task_stack_page() +*/ +static __always_inline unsigned long task_stack_size (const struct task_struct *task) +{ +return THREAD_SIZE ; +} + +static __always_inline void *task_stack_low(const struct task_struct *task) +{ +#if defined(CONFIG_STACK_GROWSUP ) +return task_stack_page (task); +#else +return task_stack_page (task) +THREAD_SIZE -task_stack_size (task); +#endif +} + +static __always_inline void *task_stack_high(const struct task_struct *task) +{ +return task_stack_low (task) +task_stack_size (task); +} +#ifdef CONFIG_THREAD_INFO_IN_TASK static inline void *try_get_task_stack (struct task_struct *tsk) {
@@ -88,10 +137,11 @@ void exit_task_stack_account(struct task_struct *tsk);static inline int object_is_on_stack (const void *obj) { -void *stack =task_stack_page (current); +void *stack_base =task_stack_low (current); +void *stack_end =task_stack_high (current); obj =kasan_reset_tag (obj); -return (obj >= stack) && (obj < (stack +THREAD_SIZE )); +return (obj >= stack_base) && (obj < stack_end); }extern void thread_stack_cache_init (void );
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 6/15 exit: Don't assume the kernel stack size
In reply to Mostafa Salehcheck_stack_usage() assumes that the kernel stack size is THREAD_SIZE. The allocated kernel stack size may not be THREAD_SIZE anymore, rely on the newly introduced helpers task_stack_size()
--- kernel/exit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/exit.c +1 -1
@@ -883,7 +883,7 @@ static void check_stack_usage(void)unsigned long free; free =stack_not_used (current); -kstack_histogram (THREAD_SIZE - free); +kstack_histogram (task_stack_size (current) - free);if (free >= lowest_to_date)return ;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 7/15 usercopy: Don't assume the kernel stack size
In reply to Mostafa Salehcheck_stack_object() assumes that the kernel stack size is THREAD_SIZE.
The allocated kernel stack size may not be THREAD_SIZE anymore, rely on the newly introduced helper task_stack_{low, high}()
--- mm/usercopy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/usercopy.c +2 -2
@@ -36,8 +36,8 @@*/ static noinline int check_stack_object(const void *obj, unsigned long len) { -const void *const stack =task_stack_page (current); -const void *const stackend = stack +THREAD_SIZE ; +const void *const stack =task_stack_low (current); +const void *const stackend =task_stack_high (current);int ret;/* Object is not on the stack at all. */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 8/15 mm: kmemleak: Don't assume the kernel stack size
In reply to Mostafa Salehkmemleak_scan_task_stacks() assumes that the kernel stack size is THREAD_SIZE.
The allocated kernel stack size may not be THREAD_SIZE anymore, rely on the newly introduced helpers task_stack_{low, high}()
--- mm/kmemleak.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/kmemleak.c +1 -1
@@ -1749,7 +1749,7 @@ static void kmemleak_scan_task_stacks(void)void *stack =try_get_task_stack (p);if (stack) { - stop =scan_block (stack, stack +THREAD_SIZE ,NULL ); + stop =scan_block (task_stack_low (p),task_stack_high (p),NULL );put_task_stack (p); }put_task_struct (p);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 9/15 arm64: Don't assume the kernel stack size
In reply to Mostafa Salehstacktrace and panic functions assume that the kernel stack size is THREAD_SIZE.
The allocated kernel stack size may not be THREAD_SIZE anymore, rely on the newly introduced helpers task_stack_{low, high}()
--- arch/arm64/include/asm/stacktrace.h | 4 ++-- arch/arm64/kernel/traps.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/arm64/include/asm/stacktrace.h +2 -2
@@ -41,8 +41,8 @@ static inline bool on_irq_stack(unsigned long sp, unsigned long size)static inline struct stack_info stackinfo_get_task (const struct task_struct *tsk) { -unsigned long low = (unsigned long )task_stack_page (tsk); -unsigned long high = low +THREAD_SIZE ; +unsigned long low = (unsigned long )task_stack_low (tsk); +unsigned long high = (unsigned long )task_stack_high (tsk);return (struct stack_info ) { .low = low,
arch/arm64/kernel/traps.c +3 -2
@@ -902,7 +902,8 @@ DEFINE_PER_CPU(unsigned long [KERNEL_EXC_STACK_SIZE/sizeof(long)],void __noreturn panic_bad_stack (struct pt_regs *regs,unsigned long esr,unsigned long far) { -unsigned long tsk_stk = (unsigned long )current->stack; +unsigned long tsk_stk = (unsigned long )task_stack_low (current); +unsigned long tsk_stk_end = (unsigned long )task_stack_high (current);unsigned long irq_stk = (unsigned long )this_cpu_read (irq_stack_ptr);unsigned long exc_stk = (unsigned long )this_cpu_ptr (kernel_exception_stack);
@@ -913,7 +914,7 @@ void __noreturn panic_bad_stack(struct pt_regs *regs, unsigned long esr, unsignepr_emerg ("FAR: 0x%016lx\n" , far);pr_emerg ("Task stack: [0x%016lx..0x%016lx]\n" , - tsk_stk, tsk_stk +THREAD_SIZE ); + tsk_stk, tsk_stk_end);pr_emerg ("IRQ stack: [0x%016lx..0x%016lx]\n" , irq_stk, irq_stk +IRQ_STACK_SIZE ); pr_emerg("Exception stack: [0x%016lx..0x%016lx]\n",
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 10/15 mm/vmalloc: Add a get_vm_area_node()
In reply to Mostafa SalehFrom: Pasha Tatashin <[email protected]>
get_vm_area_node()
Unlike the other public get_vm_area_* variants, this one accepts node from which to allocate data structure, and also the align, which allows to create vm area with a specific alignment.
This call is going to be used by partially populated stacks in order to allocate the VA space for the kernel stacks that might be larger than the memory mapped behind it.
[vmap_pages_range() already in the header, and remove the export as it
was not needed, update commit msg]
--- include/linux/vmalloc.h | 3 +++ mm/vmalloc.c | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
include/linux/vmalloc.h +3 -0
@@ -250,6 +250,9 @@ extern struct vm_struct *__get_vm_area_caller(unsigned long size,unsigned long flags, unsigned long start, unsigned long end, const void *caller); +struct vm_struct *get_vm_area_node (unsigned long size,unsigned long align, +unsigned long flags,int node,gfp_t gfp, +const void *caller);void free_vm_area (struct vm_struct *area);extern struct vm_struct *remove_vm_area (const void *addr);extern struct vm_struct *find_vm_area (const void *addr);
mm/vmalloc.c +24 -0
@@ -3301,6 +3301,30 @@ struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags, NUMA_NO_NODE, GFP_KERNEL, caller); } +/** +* get_vm_area_node - reserve a contiguous and aligned kernel virtual area +* @size: size of the area +* @align: alignment of the start address of the area +* @flags: %VM_IOREMAP for I/O mappings +* @node: NUMA node from which to allocate the area data structure +* @gfp: Flags to pass to the allocator +* @caller: Caller to be stored in the vm area data structure +* +* Search for an area of @size/align in the kernel virtual mapping area and +* reserve it for our purposes. Returns the area descriptor on success or %NULL +* on failure. +* +* Return: the area descriptor on success or %NULL on failure. +*/ +struct vm_struct *get_vm_area_node (unsigned long size,unsigned long align, +unsigned long flags,int node,gfp_t gfp, +const void *caller) +{ +return __get_vm_area_node (size, align,PAGE_SHIFT , flags, +VMALLOC_START ,VMALLOC_END , + node, gfp, caller); +} +/** * find_vm_area - find a continuous kernel virtual area * @addr: base address
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 11/15 fork: Move vmap stack freeing to work queue
In reply to Mostafa SalehFrom: David Stevens <[email protected]>
For vmap stacks not immediately released into the stack cache, free them in a workqueue instead of via call_rcu(). In an RCU context, vfree already schedules the actual freeing on the per-cpu system workqueue, so this change only affects when exactly the second attempt to put the stack into the stack cache occurs.
Moving freeing to a workqueue will allow for freeing partially populated stacks in a sleepable context (for remove_vm_area), rather than relying on vfree dispatching to a workqueue via vfree_atomic.
[Use system_percpu_wq instead of system_wq as it is deprecated and
update commit msg]
--- kernel/fork.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
kernel/fork.c +5 -4
@@ -208,7 +208,7 @@ static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]);#define GFP_VMAP_STACK (GFP_KERNEL | __GFP_ZERO | __GFP_SKIP_KASAN)struct vm_stack { -struct rcu_head rcu; +struct rcu_work work;struct vm_struct *stack_vm_area; };
@@ -287,9 +287,9 @@ static inline void free_vmap_stack(struct vm_struct *vm_area)vfree (vm_area->addr); } -static void thread_stack_free_rcu (struct rcu_head *rh) +static void thread_stack_free_work (struct work_struct *work) { - struct vm_stack *vm_stack = container_of(rh, struct vm_stack, rcu); + struct vm_stack *vm_stack = container_of(to_rcu_work(work), struct vm_stack, work);struct vm_struct *vm_area = vm_stack->stack_vm_area;if (try_release_thread_stack_to_cache (vm_stack->stack_vm_area))
@@ -308,7 +308,8 @@ static void thread_stack_delayed_free(struct task_struct *tsk) vm_stack = tsk->stack +THREAD_SIZE -sizeof (*vm_stack); vm_stack->stack_vm_area = tsk->stack_vm_area; -call_rcu (&vm_stack->rcu, thread_stack_free_rcu); +INIT_RCU_WORK (&vm_stack->work, thread_stack_free_work); +queue_rcu_work (system_percpu_wq, &vm_stack->work); }static int free_vm_stack_cache (unsigned int cpu)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 12/15 sched/task_stack: Introduce ARCH_HAS_VARIABLE_STACK_SIZE
In reply to Mostafa SalehWhen 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.
--- arch/Kconfig | 10 ++++++++++ include/linux/sched/task_stack.h | 5 +++++ 2 files changed, 15 insertions(+)
arch/Kconfig +10 -0
@@ -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
include/linux/sched/task_stack.h +5 -0
@@ -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 ; }
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 13/15 fork: Implement partial VMAP stack allocation
In reply to Mostafa SalehWhen CONFIG_ARCH_HAS_VARIABLE_STACK_SIZE is enabled, architectures can allocate a VMAP stack that is smaller than the full THREAD_SIZE.
This introduces arch abstraction arch_vm_stack_pages() which defaults to THREAD_SIZE / PAGE_SIZE.
During stack allocation, the kernel will map only the requested number of pages at the top of the THREAD_SIZE virtual area (as the stack grows down), leaving the bottom unmapped. This saves memory while retaining the same virtual alignment and guard page overflow detection characteristics.
[Fix memory leak in error path of alloc_vmap_stack()] and use
VM_UNINITIALIZED before installing the pages]
--- include/linux/thread_info.h | 8 +++++ kernel/fork.c | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+)
include/linux/thread_info.h +8 -0
@@ -92,6 +92,14 @@ static inline long set_restart_fn(struct restart_block *restart,#define THREAD_ALIGN THREAD_SIZE#endif +/* +* Arch selecting CONFIG_ARCH_HAS_VARIABLE_STACK_SIZE should override this +* with kernel stack size. +*/ +#ifndef arch_vm_stack_pages +#define arch_vm_stack_pages () (THREAD_SIZE / PAGE_SIZE) +#endif +#define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_SKIP_KASAN)/*
kernel/fork.c +68 -0
86 lines not on this page · raw message
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 14/15 arm64: mm: Relax kernel stack alignment
In reply to Mostafa SalehOn the kernel entry it was not possible to use the stack until it was checked for overflow which was done by a clever trick relying on aligning the kernel stack to double it's size, so if the bit at THREAD_SHIFT was set it means that the stack pointer has overflowed.
Now, we can easily switch the sp to sp_el1 which is the overflow stack, push some registers and execute more complex flow.
Rework the kernel entry code to eliminate the tbnz check and the alignment requirement.
--- arch/arm64/include/asm/memory.h | 7 +-- arch/arm64/kernel/entry.S | 87 ++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 17 deletions(-)
arch/arm64/include/asm/memory.h +1 -6
@@ -130,12 +130,7 @@#define THREAD_SIZE (UL(1) << THREAD_SHIFT) -/* -* By aligning VMAP'd stacks to 2 * THREAD_SIZE, we can detect overflow by -* checking sp & (1 << THREAD_SHIFT), which we can do cheaply in the entry -* assembly. -*/ -#define THREAD_ALIGN (2 * THREAD_SIZE) +#define THREAD_ALIGN THREAD_SIZE#define IRQ_STACK_SIZE THREAD_SIZE
arch/arm64/kernel/entry.S +76 -11
104 lines not on this page · raw message
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
RFC PATCH 15/15 arm64: mm: Set stack size from the kernel command line
In reply to Mostafa SalehSelect CONFIG_ARCH_HAS_VARIABLE_STACK_SIZE for arm64 and override arch_vm_stack_pages() to a value set from the command line, this value have the following properties:
- Aligned to page size (doesn't need to be a power of 2), so it can be 12kB for example
- Min value is chosen to be 8kB
- Max value will be the default stack size on arm64 which is used to allocate the VA range for the kernel stack.
--- .../admin-guide/kernel-parameters.txt | 7 ++++++ arch/arm64/Kconfig | 1 + arch/arm64/include/asm/memory.h | 12 ++++++++++ arch/arm64/kernel/entry.S | 10 +++++--- arch/arm64/kernel/setup.c | 24 +++++++++++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +7 -0
@@ -2920,6 +2920,13 @@ Kernel parameters keepinitrd [HW,ARM] See retain_initrd. + kernel_stack_pages= + [ARM64,EARLY] + Format: <integer> + Number of pages mapped for each task kernel stack. + The virtual area stays THREAD_SIZE and only its top + pages are mapped. + kernelcore= [KNL,X86,PPC,EARLY] Format: nn[KMGTPE] | nn% | "mirror" This parameter specifies the amount of memory usable by
arch/arm64/Kconfig +1 -0
@@ -58,6 +58,7 @@ config ARM64 select ARCH_HAS_SYSCALL_WRAPPER select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_ZONE_DMA_SET if EXPERT + select ARCH_HAS_VARIABLE_STACK_SIZE select ARCH_HAVE_ELF_PROT select ARCH_HAVE_NMI_SAFE_CMPXCHG select ARCH_HAVE_TRACE_MMIO_ACCESS
arch/arm64/include/asm/memory.h +12 -0
@@ -113,6 +113,13 @@#define DIRECT_MAP_PHYSMEM_END __pa(PAGE_END - 1)#define MIN_THREAD_SHIFT (14 + KASAN_THREAD_SHIFT) +#define MIN_KERNEL_STACK_SHIFT (13 + KASAN_THREAD_SHIFT) + +#if (MIN_KERNEL_STACK_SHIFT <PAGE_SHIFT ) +#define MIN_KERNEL_STACK_SIZE PAGE_SIZE +#else +#define MIN_KERNEL_STACK_SIZE (UL(1) << MIN_KERNEL_STACK_SHIFT) +#endif /* * VMAP'd stacks are allocated at page granularity, so we must ensure that such
@@ -130,6 +137,11 @@#define THREAD_SIZE (UL(1) << THREAD_SHIFT) +#ifndef __ASSEMBLY__ +extern unsigned long kernel_stack_pages; +#define arch_vm_stack_pages () (kernel_stack_pages) +#endif +#define THREAD_ALIGN THREAD_SIZE#define IRQ_STACK_SIZE THREAD_SIZE
arch/arm64/kernel/entry.S +7 -3
@@ -76,9 +76,13 @@ msr spsel, #0 mov x1, sp msr spsel, #1 - sub x1, x1, x0 - cmp x1, #THREAD_SIZE - b.lo .Lstack_ok\@ + + add x0, x0, #THREAD_SIZE + sub x1, x0, x1 + adr_l x0, kernel_stack_pages + ldr x0, [x0] + cmp x1, x0, lsl #PAGE_SHIFT + b.ls .Lstack_ok\@ /* Check whether we are on the IRQ, SDEI or EFI stacks. */ stp x2, x30, [sp, #-16]!
arch/arm64/kernel/setup.c +24 -0
@@ -278,6 +278,30 @@ u64 cpu_logical_map(unsigned int cpu)return __cpu_logical_map[cpu]; } +unsigned long kernel_stack_pages__ro_after_init =THREAD_SIZE /PAGE_SIZE ; + +static int __init setup_kernel_stack_pages (char *str) +{ +unsigned long min =MIN_KERNEL_STACK_SIZE /PAGE_SIZE ; +unsigned long max =THREAD_SIZE /PAGE_SIZE ; +unsigned long val; + +if (!str) +return -EINVAL ; + +if (kstrtoul (str,10 , &val) || val < min || val > max) { +pr_err ("Invalid kernel_stack_pages=%s, should be between %lu and %lu\n" , + str, min, max); +return 0 ; + } + + kernel_stack_pages = val; +pr_info ("Kernel stack size set to %lu KiB\n" , (val *PAGE_SIZE ) /SZ_1K ); + +return 0 ; +} +early_param ("kernel_stack_pages" , setup_kernel_stack_pages); + void __init __no_sanitize_address setup_arch(char **cmdline_p) {setup_initial_init_mm (_text, _etext, _edata, _end);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Randy Dunlap
reviewerRe: [RFC PATCH 12/15] sched/task_stack: Introduce ARCH_HAS_VARIABLE_STACK_SIZE
In reply to Mostafa SalehQuoted 24 lines · Mostafa Saleh
When
CONFIG_ARCH_HAS_VARIABLE_STACK_SIZEis enabled, the kernel stack may not be fully populated with theTHREAD_SIZEvirtual 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 likestack_not_used()andset_task_stack_end_magic()to use the lowest valid stack address instead ofTHREAD_SIZEwhich 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(+)arch/Kconfig +0
-0@@ -431,6 +431,16 @@ config HAVE_ASM_MODVERSIONS <asm/asm-prototypes.h> to support the module versioning for symbols exported from assembly code.
arch/Kconfig config HAVE_ASM_MODVERSIONS · line 442
Quoted 10 lines
arch/Kconfig +9
-0+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.
can be set from the command line.
?
Quoted 27 lines
arch/Kconfig +1
-0+ config HAVE_REGS_AND_STACK_ACCESS_API bool helpinclude/linux/sched/task_stack.h +5
-0@@ -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 ; }
Signature · 1 line
~Randy
Re: [RFC PATCH 12/15] sched/task_stack: Introduce ARCH_HAS_VARIABLE_STACK_SIZE
Quoted 28 lines · Randy Dunlap
Quoted 25 lines · Mostafa Saleh
When
CONFIG_ARCH_HAS_VARIABLE_STACK_SIZEis enabled, the kernel stack may not be fully populated with theTHREAD_SIZEvirtual 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 likestack_not_used()andset_task_stack_end_magic()to use the lowest valid stack address instead ofTHREAD_SIZEwhich 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(+)arch/Kconfig +0
-0@@ -431,6 +431,16 @@ config HAVE_ASM_MODVERSIONS <asm/asm-prototypes.h> to support the module versioning for symbols exported from assembly code.
Quoted 13 lines
arch/Kconfig config HAVE_ASM_MODVERSIONS · line 442
Quoted 9 lines
arch/Kconfig +9
-0+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.can be set from the command line.
?
I will fix it in v2.
Thanks,
Mostafa
Quoted 31 lines
Quoted 27 lines
arch/Kconfig +1
-0+ config HAVE_REGS_AND_STACK_ACCESS_API bool helpinclude/linux/sched/task_stack.h +5
-0@@ -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 ; }Signature · 1 line
~Randy