1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_TASK_STACK_H
3 #define _LINUX_SCHED_TASK_STACK_H
6 * task->stack (kernel stack) handling interfaces:
9 #include <linux/sched.h>
10 #include <linux/magic.h>
11 #include <linux/refcount.h>
13 #ifdef CONFIG_THREAD_INFO_IN_TASK
16 * When accessing the stack of a non-current task that might exit, use
17 * try_get_task_stack() instead. task_stack_page will return a pointer
18 * that could get freed out from under you.
20 static __always_inline void *task_stack_page(const struct task_struct *task)
25 #define setup_thread_stack(new,old) do { } while(0)
27 static __always_inline unsigned long *end_of_stack(const struct task_struct *task)
29 #ifdef CONFIG_STACK_GROWSUP
30 return (unsigned long *)((unsigned long)task->stack + THREAD_SIZE) - 1;
36 #elif !defined(__HAVE_THREAD_FUNCTIONS)
38 #define task_stack_page(task) ((void *)(task)->stack)
40 static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org)
42 *task_thread_info(p) = *task_thread_info(org);
43 task_thread_info(p)->task = p;
47 * Return the address of the last usable long on the stack.
49 * When the stack grows down, this is just above the thread
50 * info struct. Going any lower will corrupt the threadinfo.
52 * When the stack grows up, this is the highest address.
53 * Beyond that position, we corrupt data on the next page.
55 static inline unsigned long *end_of_stack(struct task_struct *p)
57 #ifdef CONFIG_STACK_GROWSUP
58 return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1;
60 return (unsigned long *)(task_thread_info(p) + 1);
66 #ifdef CONFIG_THREAD_INFO_IN_TASK
67 static inline void *try_get_task_stack(struct task_struct *tsk)
69 return refcount_inc_not_zero(&tsk->stack_refcount) ?
70 task_stack_page(tsk) : NULL;
73 extern void put_task_stack(struct task_struct *tsk);
75 static inline void *try_get_task_stack(struct task_struct *tsk)
77 return task_stack_page(tsk);
80 static inline void put_task_stack(struct task_struct *tsk) {}
83 void exit_task_stack_account(struct task_struct *tsk);
85 #define task_stack_end_corrupted(task) \
86 (*(end_of_stack(task)) != STACK_END_MAGIC)
88 static inline int object_is_on_stack(const void *obj)
90 void *stack = task_stack_page(current);
92 return (obj >= stack) && (obj < (stack + THREAD_SIZE));
95 extern void thread_stack_cache_init(void);
97 #ifdef CONFIG_DEBUG_STACK_USAGE
98 static inline unsigned long stack_not_used(struct task_struct *p)
100 unsigned long *n = end_of_stack(p);
102 do { /* Skip over canary */
103 # ifdef CONFIG_STACK_GROWSUP
110 # ifdef CONFIG_STACK_GROWSUP
111 return (unsigned long)end_of_stack(p) - (unsigned long)n;
113 return (unsigned long)n - (unsigned long)end_of_stack(p);
117 extern void set_task_stack_end_magic(struct task_struct *tsk);
119 #ifndef __HAVE_ARCH_KSTACK_END
120 static inline int kstack_end(void *addr)
122 /* Reliable end of stack detection:
123 * Some APM bios versions misalign the stack
125 return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*)));
129 #endif /* _LINUX_SCHED_TASK_STACK_H */