| 1 | /* SPDX-License-Identifier: GPL-2.0 */ | 
|---|
| 2 | #ifndef _LINUX_SCHED_TASK_STACK_H | 
|---|
| 3 | #define _LINUX_SCHED_TASK_STACK_H | 
|---|
| 4 |  | 
|---|
| 5 | /* | 
|---|
| 6 | * task->stack (kernel stack) handling interfaces: | 
|---|
| 7 | */ | 
|---|
| 8 |  | 
|---|
| 9 | #include <linux/sched.h> | 
|---|
| 10 | #include <linux/magic.h> | 
|---|
| 11 | #include <linux/refcount.h> | 
|---|
| 12 | #include <linux/kasan.h> | 
|---|
| 13 |  | 
|---|
| 14 | #ifdef CONFIG_THREAD_INFO_IN_TASK | 
|---|
| 15 |  | 
|---|
| 16 | /* | 
|---|
| 17 | * When accessing the stack of a non-current task that might exit, use | 
|---|
| 18 | * try_get_task_stack() instead.  task_stack_page will return a pointer | 
|---|
| 19 | * that could get freed out from under you. | 
|---|
| 20 | */ | 
|---|
| 21 | static __always_inline void *task_stack_page(const struct task_struct *task) | 
|---|
| 22 | { | 
|---|
| 23 | return task->stack; | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 | #define setup_thread_stack(new,old)	do { } while(0) | 
|---|
| 27 |  | 
|---|
| 28 | static __always_inline unsigned long *end_of_stack(const struct task_struct *task) | 
|---|
| 29 | { | 
|---|
| 30 | #ifdef CONFIG_STACK_GROWSUP | 
|---|
| 31 | return (unsigned long *)((unsigned long)task->stack + THREAD_SIZE) - 1; | 
|---|
| 32 | #else | 
|---|
| 33 | return task->stack; | 
|---|
| 34 | #endif | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | #else | 
|---|
| 38 |  | 
|---|
| 39 | #define task_stack_page(task)	((void *)(task)->stack) | 
|---|
| 40 |  | 
|---|
| 41 | static inline void setup_thread_stack(struct task_struct *p, struct task_struct *org) | 
|---|
| 42 | { | 
|---|
| 43 | *task_thread_info(p) = *task_thread_info(org); | 
|---|
| 44 | task_thread_info(p)->task = p; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | /* | 
|---|
| 48 | * Return the address of the last usable long on the stack. | 
|---|
| 49 | * | 
|---|
| 50 | * When the stack grows down, this is just above the thread | 
|---|
| 51 | * info struct. Going any lower will corrupt the threadinfo. | 
|---|
| 52 | * | 
|---|
| 53 | * When the stack grows up, this is the highest address. | 
|---|
| 54 | * Beyond that position, we corrupt data on the next page. | 
|---|
| 55 | */ | 
|---|
| 56 | static inline unsigned long *end_of_stack(const struct task_struct *p) | 
|---|
| 57 | { | 
|---|
| 58 | #ifdef CONFIG_STACK_GROWSUP | 
|---|
| 59 | return (unsigned long *)((unsigned long)task_thread_info(p) + THREAD_SIZE) - 1; | 
|---|
| 60 | #else | 
|---|
| 61 | return (unsigned long *)(task_thread_info(p) + 1); | 
|---|
| 62 | #endif | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | #endif | 
|---|
| 66 |  | 
|---|
| 67 | #ifdef CONFIG_THREAD_INFO_IN_TASK | 
|---|
| 68 | static inline void *try_get_task_stack(struct task_struct *tsk) | 
|---|
| 69 | { | 
|---|
| 70 | return refcount_inc_not_zero(r: &tsk->stack_refcount) ? | 
|---|
| 71 | task_stack_page(task: tsk) : NULL; | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | extern void put_task_stack(struct task_struct *tsk); | 
|---|
| 75 | #else | 
|---|
| 76 | static inline void *try_get_task_stack(struct task_struct *tsk) | 
|---|
| 77 | { | 
|---|
| 78 | return task_stack_page(tsk); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | static inline void put_task_stack(struct task_struct *tsk) {} | 
|---|
| 82 | #endif | 
|---|
| 83 |  | 
|---|
| 84 | void exit_task_stack_account(struct task_struct *tsk); | 
|---|
| 85 |  | 
|---|
| 86 | #define task_stack_end_corrupted(task) \ | 
|---|
| 87 | (*(end_of_stack(task)) != STACK_END_MAGIC) | 
|---|
| 88 |  | 
|---|
| 89 | static inline int object_is_on_stack(const void *obj) | 
|---|
| 90 | { | 
|---|
| 91 | void *stack = task_stack_page(current); | 
|---|
| 92 |  | 
|---|
| 93 | obj = kasan_reset_tag(addr: obj); | 
|---|
| 94 | return (obj >= stack) && (obj < (stack + THREAD_SIZE)); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | extern void thread_stack_cache_init(void); | 
|---|
| 98 |  | 
|---|
| 99 | #ifdef CONFIG_DEBUG_STACK_USAGE | 
|---|
| 100 | unsigned long stack_not_used(struct task_struct *p); | 
|---|
| 101 | #else | 
|---|
| 102 | static inline unsigned long stack_not_used(struct task_struct *p) | 
|---|
| 103 | { | 
|---|
| 104 | return 0; | 
|---|
| 105 | } | 
|---|
| 106 | #endif | 
|---|
| 107 | extern void set_task_stack_end_magic(struct task_struct *tsk); | 
|---|
| 108 |  | 
|---|
| 109 | static inline int kstack_end(void *addr) | 
|---|
| 110 | { | 
|---|
| 111 | /* Reliable end of stack detection: | 
|---|
| 112 | * Some APM bios versions misalign the stack | 
|---|
| 113 | */ | 
|---|
| 114 | return !(((unsigned long)addr+sizeof(void*)-1) & (THREAD_SIZE-sizeof(void*))); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | #endif /* _LINUX_SCHED_TASK_STACK_H */ | 
|---|
| 118 |  | 
|---|