Commit be9a2277 authored by Sebastian Andrzej Siewior's avatar Sebastian Andrzej Siewior Committed by Thomas Gleixner

fork: Redo ifdefs around task stack handling

The use of ifdef CONFIG_VMAP_STACK is confusing in terms what is
actually happenning and what can happen.

For instance from reading free_thread_stack() it appears that in the
CONFIG_VMAP_STACK case it may receive a non-NULL vm pointer but it may also
be NULL in which case __free_pages() is used to free the stack.  This is
however not the case because in the VMAP case a non-NULL pointer is always
returned here.  Since it looks like this might happen, the compiler creates
the correct dead code with the invocation to __free_pages() and everything
around it. Twice.

Add spaces between the ifdef and the identifer to recognize the ifdef
level which is currently in scope.

Add the current identifer as a comment behind #else and #endif.
Move the code within free_thread_stack() and alloc_thread_stack_node()
into the relevant ifdef blocks.
Signed-off-by: default avatarSebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Acked-by: default avatarAndy Lutomirski <luto@kernel.org>
Link: https://lore.kernel.org/r/20220217102406.3697941-2-bigeasy@linutronix.de
parent cfb92440
...@@ -185,7 +185,7 @@ static inline void free_task_struct(struct task_struct *tsk) ...@@ -185,7 +185,7 @@ static inline void free_task_struct(struct task_struct *tsk)
*/ */
# if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)
#ifdef CONFIG_VMAP_STACK # ifdef CONFIG_VMAP_STACK
/* /*
* vmalloc() is a bit slow, and calling vfree() enough times will force a TLB * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB
* flush. Try to minimize the number of calls by caching stacks. * flush. Try to minimize the number of calls by caching stacks.
...@@ -210,11 +210,9 @@ static int free_vm_stack_cache(unsigned int cpu) ...@@ -210,11 +210,9 @@ static int free_vm_stack_cache(unsigned int cpu)
return 0; return 0;
} }
#endif
static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
{ {
#ifdef CONFIG_VMAP_STACK
void *stack; void *stack;
int i; int i;
...@@ -258,45 +256,53 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) ...@@ -258,45 +256,53 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
tsk->stack = stack; tsk->stack = stack;
} }
return stack; return stack;
#else
struct page *page = alloc_pages_node(node, THREADINFO_GFP,
THREAD_SIZE_ORDER);
if (likely(page)) {
tsk->stack = kasan_reset_tag(page_address(page));
return tsk->stack;
}
return NULL;
#endif
} }
static inline void free_thread_stack(struct task_struct *tsk) static void free_thread_stack(struct task_struct *tsk)
{ {
#ifdef CONFIG_VMAP_STACK
struct vm_struct *vm = task_stack_vm_area(tsk); struct vm_struct *vm = task_stack_vm_area(tsk);
int i;
if (vm) { for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++)
int i; memcg_kmem_uncharge_page(vm->pages[i], 0);
for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) for (i = 0; i < NR_CACHED_STACKS; i++) {
memcg_kmem_uncharge_page(vm->pages[i], 0); if (this_cpu_cmpxchg(cached_stacks[i], NULL,
tsk->stack_vm_area) != NULL)
continue;
for (i = 0; i < NR_CACHED_STACKS; i++) { tsk->stack = NULL;
if (this_cpu_cmpxchg(cached_stacks[i], tsk->stack_vm_area = NULL;
NULL, tsk->stack_vm_area) != NULL) return;
continue; }
vfree_atomic(tsk->stack);
tsk->stack = NULL;
tsk->stack_vm_area = NULL;
}
return; # else /* !CONFIG_VMAP_STACK */
}
vfree_atomic(tsk->stack); static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
return; {
struct page *page = alloc_pages_node(node, THREADINFO_GFP,
THREAD_SIZE_ORDER);
if (likely(page)) {
tsk->stack = kasan_reset_tag(page_address(page));
return tsk->stack;
} }
#endif return NULL;
}
static void free_thread_stack(struct task_struct *tsk)
{
__free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER);
tsk->stack = NULL;
} }
# else
# endif /* CONFIG_VMAP_STACK */
# else /* !(THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK)) */
static struct kmem_cache *thread_stack_cache; static struct kmem_cache *thread_stack_cache;
static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
...@@ -312,6 +318,7 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, ...@@ -312,6 +318,7 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk,
static void free_thread_stack(struct task_struct *tsk) static void free_thread_stack(struct task_struct *tsk)
{ {
kmem_cache_free(thread_stack_cache, tsk->stack); kmem_cache_free(thread_stack_cache, tsk->stack);
tsk->stack = NULL;
} }
void thread_stack_cache_init(void) void thread_stack_cache_init(void)
...@@ -321,8 +328,9 @@ void thread_stack_cache_init(void) ...@@ -321,8 +328,9 @@ void thread_stack_cache_init(void)
THREAD_SIZE, NULL); THREAD_SIZE, NULL);
BUG_ON(thread_stack_cache == NULL); BUG_ON(thread_stack_cache == NULL);
} }
# endif
#endif # endif /* THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) */
#endif /* !CONFIG_ARCH_THREAD_STACK_ALLOCATOR */
/* SLAB cache for signal_struct structures (tsk->signal) */ /* SLAB cache for signal_struct structures (tsk->signal) */
static struct kmem_cache *signal_cachep; static struct kmem_cache *signal_cachep;
...@@ -432,10 +440,6 @@ static void release_task_stack(struct task_struct *tsk) ...@@ -432,10 +440,6 @@ static void release_task_stack(struct task_struct *tsk)
account_kernel_stack(tsk, -1); account_kernel_stack(tsk, -1);
free_thread_stack(tsk); free_thread_stack(tsk);
tsk->stack = NULL;
#ifdef CONFIG_VMAP_STACK
tsk->stack_vm_area = NULL;
#endif
} }
#ifdef CONFIG_THREAD_INFO_IN_TASK #ifdef CONFIG_THREAD_INFO_IN_TASK
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment