xref: /linux/include/linux/thread_info.h (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* thread_info.h: common low-level thread information accessors
3  *
4  * Copyright (C) 2002  David Howells (dhowells@redhat.com)
5  * - Incorporating suggestions made by Linus Torvalds
6  */
7 
8 #ifndef _LINUX_THREAD_INFO_H
9 #define _LINUX_THREAD_INFO_H
10 
11 #include <linux/types.h>
12 #include <linux/limits.h>
13 #include <linux/bug.h>
14 #include <linux/restart_block.h>
15 #include <linux/errno.h>
16 
17 #ifdef CONFIG_THREAD_INFO_IN_TASK
18 /*
19  * For CONFIG_THREAD_INFO_IN_TASK kernels we need <asm/current.h> for the
20  * definition of current, but for !CONFIG_THREAD_INFO_IN_TASK kernels,
21  * including <asm/current.h> can cause a circular dependency on some platforms.
22  */
23 #include <asm/current.h>
24 #define current_thread_info() ((struct thread_info *)current)
25 #endif
26 
27 #include <linux/bitops.h>
28 
29 /*
30  * For per-arch arch_within_stack_frames() implementations, defined in
31  * asm/thread_info.h.
32  */
33 enum {
34 	BAD_STACK = -1,
35 	NOT_STACK = 0,
36 	GOOD_FRAME,
37 	GOOD_STACK,
38 };
39 
40 #ifdef CONFIG_GENERIC_ENTRY
41 enum syscall_work_bit {
42 	SYSCALL_WORK_BIT_SECCOMP,
43 	SYSCALL_WORK_BIT_SYSCALL_TRACEPOINT,
44 	SYSCALL_WORK_BIT_SYSCALL_TRACE,
45 	SYSCALL_WORK_BIT_SYSCALL_EMU,
46 	SYSCALL_WORK_BIT_SYSCALL_AUDIT,
47 	SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
48 	SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
49 };
50 
51 #define SYSCALL_WORK_SECCOMP		BIT(SYSCALL_WORK_BIT_SECCOMP)
52 #define SYSCALL_WORK_SYSCALL_TRACEPOINT	BIT(SYSCALL_WORK_BIT_SYSCALL_TRACEPOINT)
53 #define SYSCALL_WORK_SYSCALL_TRACE	BIT(SYSCALL_WORK_BIT_SYSCALL_TRACE)
54 #define SYSCALL_WORK_SYSCALL_EMU	BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)
55 #define SYSCALL_WORK_SYSCALL_AUDIT	BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT)
56 #define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
57 #define SYSCALL_WORK_SYSCALL_EXIT_TRAP	BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
58 #endif
59 
60 #include <asm/thread_info.h>
61 
62 #ifndef TIF_NEED_RESCHED_LAZY
63 #ifdef CONFIG_ARCH_HAS_PREEMPT_LAZY
64 #error Inconsistent PREEMPT_LAZY
65 #endif
66 #define TIF_NEED_RESCHED_LAZY TIF_NEED_RESCHED
67 #define _TIF_NEED_RESCHED_LAZY _TIF_NEED_RESCHED
68 #endif
69 
70 #ifndef TIF_RSEQ
71 # define TIF_RSEQ	TIF_NOTIFY_RESUME
72 # define _TIF_RSEQ	_TIF_NOTIFY_RESUME
73 #endif
74 
75 #ifdef __KERNEL__
76 
77 #ifndef arch_set_restart_data
78 #define arch_set_restart_data(restart) do { } while (0)
79 #endif
80 
81 static inline long set_restart_fn(struct restart_block *restart,
82 					long (*fn)(struct restart_block *))
83 {
84 	restart->fn = fn;
85 	arch_set_restart_data(restart);
86 	return -ERESTART_RESTARTBLOCK;
87 }
88 
89 #ifndef THREAD_ALIGN
90 #define THREAD_ALIGN	THREAD_SIZE
91 #endif
92 
93 #define THREADINFO_GFP		(GFP_KERNEL_ACCOUNT | __GFP_ZERO)
94 
95 /*
96  * flag set/clear/test wrappers
97  * - pass TIF_xxxx constants to these functions
98  */
99 
100 static inline void set_ti_thread_flag(struct thread_info *ti, int flag)
101 {
102 	set_bit(flag, (unsigned long *)&ti->flags);
103 }
104 
105 static inline void clear_ti_thread_flag(struct thread_info *ti, int flag)
106 {
107 	clear_bit(flag, (unsigned long *)&ti->flags);
108 }
109 
110 static inline void update_ti_thread_flag(struct thread_info *ti, int flag,
111 					 bool value)
112 {
113 	if (value)
114 		set_ti_thread_flag(ti, flag);
115 	else
116 		clear_ti_thread_flag(ti, flag);
117 }
118 
119 static inline int test_and_set_ti_thread_flag(struct thread_info *ti, int flag)
120 {
121 	return test_and_set_bit(flag, (unsigned long *)&ti->flags);
122 }
123 
124 static inline int test_and_clear_ti_thread_flag(struct thread_info *ti, int flag)
125 {
126 	return test_and_clear_bit(flag, (unsigned long *)&ti->flags);
127 }
128 
129 static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
130 {
131 	return test_bit(flag, (unsigned long *)&ti->flags);
132 }
133 
134 /*
135  * This may be used in noinstr code, and needs to be __always_inline to prevent
136  * inadvertent instrumentation.
137  */
138 static __always_inline unsigned long read_ti_thread_flags(struct thread_info *ti)
139 {
140 	return READ_ONCE(ti->flags);
141 }
142 
143 #define set_thread_flag(flag) \
144 	set_ti_thread_flag(current_thread_info(), flag)
145 #define clear_thread_flag(flag) \
146 	clear_ti_thread_flag(current_thread_info(), flag)
147 #define update_thread_flag(flag, value) \
148 	update_ti_thread_flag(current_thread_info(), flag, value)
149 #define test_and_set_thread_flag(flag) \
150 	test_and_set_ti_thread_flag(current_thread_info(), flag)
151 #define test_and_clear_thread_flag(flag) \
152 	test_and_clear_ti_thread_flag(current_thread_info(), flag)
153 #define test_thread_flag(flag) \
154 	test_ti_thread_flag(current_thread_info(), flag)
155 #define read_thread_flags() \
156 	read_ti_thread_flags(current_thread_info())
157 
158 #define read_task_thread_flags(t) \
159 	read_ti_thread_flags(task_thread_info(t))
160 
161 #ifdef CONFIG_GENERIC_ENTRY
162 #define set_syscall_work(fl) \
163 	set_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
164 #define test_syscall_work(fl) \
165 	test_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
166 #define clear_syscall_work(fl) \
167 	clear_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
168 
169 #define set_task_syscall_work(t, fl) \
170 	set_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
171 #define test_task_syscall_work(t, fl) \
172 	test_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
173 #define clear_task_syscall_work(t, fl) \
174 	clear_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
175 
176 #else /* CONFIG_GENERIC_ENTRY */
177 
178 #define set_syscall_work(fl)						\
179 	set_ti_thread_flag(current_thread_info(), TIF_##fl)
180 #define test_syscall_work(fl) \
181 	test_ti_thread_flag(current_thread_info(), TIF_##fl)
182 #define clear_syscall_work(fl) \
183 	clear_ti_thread_flag(current_thread_info(), TIF_##fl)
184 
185 #define set_task_syscall_work(t, fl) \
186 	set_ti_thread_flag(task_thread_info(t), TIF_##fl)
187 #define test_task_syscall_work(t, fl) \
188 	test_ti_thread_flag(task_thread_info(t), TIF_##fl)
189 #define clear_task_syscall_work(t, fl) \
190 	clear_ti_thread_flag(task_thread_info(t), TIF_##fl)
191 #endif /* !CONFIG_GENERIC_ENTRY */
192 
193 #ifdef _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H
194 
195 static __always_inline bool tif_test_bit(int bit)
196 {
197 	return arch_test_bit(bit,
198 			     (unsigned long *)(&current_thread_info()->flags));
199 }
200 
201 #else
202 
203 static __always_inline bool tif_test_bit(int bit)
204 {
205 	return test_bit(bit,
206 			(unsigned long *)(&current_thread_info()->flags));
207 }
208 
209 #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H */
210 
211 static __always_inline bool tif_need_resched(void)
212 {
213 	return tif_test_bit(TIF_NEED_RESCHED);
214 }
215 
216 #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
217 static inline int arch_within_stack_frames(const void * const stack,
218 					   const void * const stackend,
219 					   const void *obj, unsigned long len)
220 {
221 	return 0;
222 }
223 #endif
224 
225 #ifndef arch_setup_new_exec
226 static inline void arch_setup_new_exec(void) { }
227 #endif
228 
229 void arch_task_cache_init(void); /* for CONFIG_SH */
230 void arch_release_task_struct(struct task_struct *tsk);
231 int arch_dup_task_struct(struct task_struct *dst,
232 				struct task_struct *src);
233 
234 #endif	/* __KERNEL__ */
235 
236 #endif /* _LINUX_THREAD_INFO_H */
237