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