xref: /linux-6.15/include/linux/thread_info.h (revision 3136b93c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* thread_info.h: common low-level thread information accessors
3  *
4  * Copyright (C) 2002  David Howells ([email protected])
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/bug.h>
13 #include <linux/restart_block.h>
14 
15 #ifdef CONFIG_THREAD_INFO_IN_TASK
16 /*
17  * For CONFIG_THREAD_INFO_IN_TASK kernels we need <asm/current.h> for the
18  * definition of current, but for !CONFIG_THREAD_INFO_IN_TASK kernels,
19  * including <asm/current.h> can cause a circular dependency on some platforms.
20  */
21 #include <asm/current.h>
22 #define current_thread_info() ((struct thread_info *)current)
23 #endif
24 
25 #include <linux/bitops.h>
26 
27 /*
28  * For per-arch arch_within_stack_frames() implementations, defined in
29  * asm/thread_info.h.
30  */
31 enum {
32 	BAD_STACK = -1,
33 	NOT_STACK = 0,
34 	GOOD_FRAME,
35 	GOOD_STACK,
36 };
37 
38 #include <asm/thread_info.h>
39 
40 #ifdef __KERNEL__
41 
42 #ifndef THREAD_ALIGN
43 #define THREAD_ALIGN	THREAD_SIZE
44 #endif
45 
46 #define THREADINFO_GFP		(GFP_KERNEL_ACCOUNT | __GFP_ZERO)
47 
48 /*
49  * flag set/clear/test wrappers
50  * - pass TIF_xxxx constants to these functions
51  */
52 
53 static inline void set_ti_thread_flag(struct thread_info *ti, int flag)
54 {
55 	set_bit(flag, (unsigned long *)&ti->flags);
56 }
57 
58 static inline void clear_ti_thread_flag(struct thread_info *ti, int flag)
59 {
60 	clear_bit(flag, (unsigned long *)&ti->flags);
61 }
62 
63 static inline void update_ti_thread_flag(struct thread_info *ti, int flag,
64 					 bool value)
65 {
66 	if (value)
67 		set_ti_thread_flag(ti, flag);
68 	else
69 		clear_ti_thread_flag(ti, flag);
70 }
71 
72 static inline int test_and_set_ti_thread_flag(struct thread_info *ti, int flag)
73 {
74 	return test_and_set_bit(flag, (unsigned long *)&ti->flags);
75 }
76 
77 static inline int test_and_clear_ti_thread_flag(struct thread_info *ti, int flag)
78 {
79 	return test_and_clear_bit(flag, (unsigned long *)&ti->flags);
80 }
81 
82 static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
83 {
84 	return test_bit(flag, (unsigned long *)&ti->flags);
85 }
86 
87 #define set_thread_flag(flag) \
88 	set_ti_thread_flag(current_thread_info(), flag)
89 #define clear_thread_flag(flag) \
90 	clear_ti_thread_flag(current_thread_info(), flag)
91 #define update_thread_flag(flag, value) \
92 	update_ti_thread_flag(current_thread_info(), flag, value)
93 #define test_and_set_thread_flag(flag) \
94 	test_and_set_ti_thread_flag(current_thread_info(), flag)
95 #define test_and_clear_thread_flag(flag) \
96 	test_and_clear_ti_thread_flag(current_thread_info(), flag)
97 #define test_thread_flag(flag) \
98 	test_ti_thread_flag(current_thread_info(), flag)
99 
100 #ifdef CONFIG_GENERIC_ENTRY
101 #define set_syscall_work(fl) \
102 	set_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
103 #define test_syscall_work(fl) \
104 	test_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
105 #define clear_syscall_work(fl) \
106 	clear_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
107 
108 #define set_task_syscall_work(t, fl) \
109 	set_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
110 #define test_task_syscall_work(t, fl) \
111 	test_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
112 #define clear_task_syscall_work(t, fl) \
113 	clear_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
114 
115 #else /* CONFIG_GENERIC_ENTRY */
116 
117 #define set_syscall_work(fl)						\
118 	set_ti_thread_flag(current_thread_info(), SYSCALL_WORK_##fl)
119 #define test_syscall_work(fl) \
120 	test_ti_thread_flag(current_thread_info(), SYSCALL_WORK_##fl)
121 #define clear_syscall_work(fl) \
122 	clear_ti_thread_flag(current_thread_info(), SYSCALL_WORK_##fl)
123 
124 #define set_task_syscall_work(t, fl) \
125 	set_ti_thread_flag(task_thread_info(t), TIF_##fl)
126 #define test_task_syscall_work(t, fl) \
127 	test_ti_thread_flag(task_thread_info(t), TIF_##fl)
128 #define clear_task_syscall_work(t, fl) \
129 	clear_ti_thread_flag(task_thread_info(t), TIF_##fl)
130 #endif /* !CONFIG_GENERIC_ENTRY */
131 
132 #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
133 
134 #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
135 static inline int arch_within_stack_frames(const void * const stack,
136 					   const void * const stackend,
137 					   const void *obj, unsigned long len)
138 {
139 	return 0;
140 }
141 #endif
142 
143 #ifdef CONFIG_HARDENED_USERCOPY
144 extern void __check_object_size(const void *ptr, unsigned long n,
145 					bool to_user);
146 
147 static __always_inline void check_object_size(const void *ptr, unsigned long n,
148 					      bool to_user)
149 {
150 	if (!__builtin_constant_p(n))
151 		__check_object_size(ptr, n, to_user);
152 }
153 #else
154 static inline void check_object_size(const void *ptr, unsigned long n,
155 				     bool to_user)
156 { }
157 #endif /* CONFIG_HARDENED_USERCOPY */
158 
159 extern void __compiletime_error("copy source size is too small")
160 __bad_copy_from(void);
161 extern void __compiletime_error("copy destination size is too small")
162 __bad_copy_to(void);
163 
164 static inline void copy_overflow(int size, unsigned long count)
165 {
166 	WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
167 }
168 
169 static __always_inline __must_check bool
170 check_copy_size(const void *addr, size_t bytes, bool is_source)
171 {
172 	int sz = __compiletime_object_size(addr);
173 	if (unlikely(sz >= 0 && sz < bytes)) {
174 		if (!__builtin_constant_p(bytes))
175 			copy_overflow(sz, bytes);
176 		else if (is_source)
177 			__bad_copy_from();
178 		else
179 			__bad_copy_to();
180 		return false;
181 	}
182 	if (WARN_ON_ONCE(bytes > INT_MAX))
183 		return false;
184 	check_object_size(addr, bytes, is_source);
185 	return true;
186 }
187 
188 #ifndef arch_setup_new_exec
189 static inline void arch_setup_new_exec(void) { }
190 #endif
191 
192 #endif	/* __KERNEL__ */
193 
194 #endif /* _LINUX_THREAD_INFO_H */
195