thread_info.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #ifndef _LINUX_THREAD_INFO_H
  8. #define _LINUX_THREAD_INFO_H
  9. #include <linux/types.h>
  10. #include <linux/limits.h>
  11. #include <linux/bug.h>
  12. #include <linux/restart_block.h>
  13. #include <linux/errno.h>
  14. #ifdef CONFIG_THREAD_INFO_IN_TASK
  15. /*
  16. * For CONFIG_THREAD_INFO_IN_TASK kernels we need <asm/current.h> for the
  17. * definition of current, but for !CONFIG_THREAD_INFO_IN_TASK kernels,
  18. * including <asm/current.h> can cause a circular dependency on some platforms.
  19. */
  20. #include <asm/current.h>
  21. #define current_thread_info() ((struct thread_info *)current)
  22. #endif
  23. #include <linux/bitops.h>
  24. /*
  25. * For per-arch arch_within_stack_frames() implementations, defined in
  26. * asm/thread_info.h.
  27. */
  28. enum {
  29. BAD_STACK = -1,
  30. NOT_STACK = 0,
  31. GOOD_FRAME,
  32. GOOD_STACK,
  33. };
  34. #ifdef CONFIG_GENERIC_ENTRY
  35. enum syscall_work_bit {
  36. SYSCALL_WORK_BIT_SECCOMP,
  37. SYSCALL_WORK_BIT_SYSCALL_TRACEPOINT,
  38. SYSCALL_WORK_BIT_SYSCALL_TRACE,
  39. SYSCALL_WORK_BIT_SYSCALL_EMU,
  40. SYSCALL_WORK_BIT_SYSCALL_AUDIT,
  41. SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH,
  42. SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP,
  43. };
  44. #define SYSCALL_WORK_SECCOMP BIT(SYSCALL_WORK_BIT_SECCOMP)
  45. #define SYSCALL_WORK_SYSCALL_TRACEPOINT BIT(SYSCALL_WORK_BIT_SYSCALL_TRACEPOINT)
  46. #define SYSCALL_WORK_SYSCALL_TRACE BIT(SYSCALL_WORK_BIT_SYSCALL_TRACE)
  47. #define SYSCALL_WORK_SYSCALL_EMU BIT(SYSCALL_WORK_BIT_SYSCALL_EMU)
  48. #define SYSCALL_WORK_SYSCALL_AUDIT BIT(SYSCALL_WORK_BIT_SYSCALL_AUDIT)
  49. #define SYSCALL_WORK_SYSCALL_USER_DISPATCH BIT(SYSCALL_WORK_BIT_SYSCALL_USER_DISPATCH)
  50. #define SYSCALL_WORK_SYSCALL_EXIT_TRAP BIT(SYSCALL_WORK_BIT_SYSCALL_EXIT_TRAP)
  51. #endif
  52. #include <asm/thread_info.h>
  53. #ifdef __KERNEL__
  54. #ifndef arch_set_restart_data
  55. #define arch_set_restart_data(restart) do { } while (0)
  56. #endif
  57. static inline long set_restart_fn(struct restart_block *restart,
  58. long (*fn)(struct restart_block *))
  59. {
  60. restart->fn = fn;
  61. arch_set_restart_data(restart);
  62. return -ERESTART_RESTARTBLOCK;
  63. }
  64. #ifndef THREAD_ALIGN
  65. #define THREAD_ALIGN THREAD_SIZE
  66. #endif
  67. #define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_ZERO)
  68. /*
  69. * flag set/clear/test wrappers
  70. * - pass TIF_xxxx constants to these functions
  71. */
  72. static inline void set_ti_thread_flag(struct thread_info *ti, int flag)
  73. {
  74. set_bit(flag, (unsigned long *)&ti->flags);
  75. }
  76. static inline void clear_ti_thread_flag(struct thread_info *ti, int flag)
  77. {
  78. clear_bit(flag, (unsigned long *)&ti->flags);
  79. }
  80. static inline void update_ti_thread_flag(struct thread_info *ti, int flag,
  81. bool value)
  82. {
  83. if (value)
  84. set_ti_thread_flag(ti, flag);
  85. else
  86. clear_ti_thread_flag(ti, flag);
  87. }
  88. static inline int test_and_set_ti_thread_flag(struct thread_info *ti, int flag)
  89. {
  90. return test_and_set_bit(flag, (unsigned long *)&ti->flags);
  91. }
  92. static inline int test_and_clear_ti_thread_flag(struct thread_info *ti, int flag)
  93. {
  94. return test_and_clear_bit(flag, (unsigned long *)&ti->flags);
  95. }
  96. static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
  97. {
  98. return test_bit(flag, (unsigned long *)&ti->flags);
  99. }
  100. /*
  101. * This may be used in noinstr code, and needs to be __always_inline to prevent
  102. * inadvertent instrumentation.
  103. */
  104. static __always_inline unsigned long read_ti_thread_flags(struct thread_info *ti)
  105. {
  106. return READ_ONCE(ti->flags);
  107. }
  108. #define set_thread_flag(flag) \
  109. set_ti_thread_flag(current_thread_info(), flag)
  110. #define clear_thread_flag(flag) \
  111. clear_ti_thread_flag(current_thread_info(), flag)
  112. #define update_thread_flag(flag, value) \
  113. update_ti_thread_flag(current_thread_info(), flag, value)
  114. #define test_and_set_thread_flag(flag) \
  115. test_and_set_ti_thread_flag(current_thread_info(), flag)
  116. #define test_and_clear_thread_flag(flag) \
  117. test_and_clear_ti_thread_flag(current_thread_info(), flag)
  118. #define test_thread_flag(flag) \
  119. test_ti_thread_flag(current_thread_info(), flag)
  120. #define read_thread_flags() \
  121. read_ti_thread_flags(current_thread_info())
  122. #define read_task_thread_flags(t) \
  123. read_ti_thread_flags(task_thread_info(t))
  124. #ifdef CONFIG_GENERIC_ENTRY
  125. #define set_syscall_work(fl) \
  126. set_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
  127. #define test_syscall_work(fl) \
  128. test_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
  129. #define clear_syscall_work(fl) \
  130. clear_bit(SYSCALL_WORK_BIT_##fl, &current_thread_info()->syscall_work)
  131. #define set_task_syscall_work(t, fl) \
  132. set_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
  133. #define test_task_syscall_work(t, fl) \
  134. test_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
  135. #define clear_task_syscall_work(t, fl) \
  136. clear_bit(SYSCALL_WORK_BIT_##fl, &task_thread_info(t)->syscall_work)
  137. #else /* CONFIG_GENERIC_ENTRY */
  138. #define set_syscall_work(fl) \
  139. set_ti_thread_flag(current_thread_info(), TIF_##fl)
  140. #define test_syscall_work(fl) \
  141. test_ti_thread_flag(current_thread_info(), TIF_##fl)
  142. #define clear_syscall_work(fl) \
  143. clear_ti_thread_flag(current_thread_info(), TIF_##fl)
  144. #define set_task_syscall_work(t, fl) \
  145. set_ti_thread_flag(task_thread_info(t), TIF_##fl)
  146. #define test_task_syscall_work(t, fl) \
  147. test_ti_thread_flag(task_thread_info(t), TIF_##fl)
  148. #define clear_task_syscall_work(t, fl) \
  149. clear_ti_thread_flag(task_thread_info(t), TIF_##fl)
  150. #endif /* !CONFIG_GENERIC_ENTRY */
  151. #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  152. #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
  153. static inline int arch_within_stack_frames(const void * const stack,
  154. const void * const stackend,
  155. const void *obj, unsigned long len)
  156. {
  157. return 0;
  158. }
  159. #endif
  160. #ifdef CONFIG_HARDENED_USERCOPY
  161. extern void __check_object_size(const void *ptr, unsigned long n,
  162. bool to_user);
  163. static __always_inline void check_object_size(const void *ptr, unsigned long n,
  164. bool to_user)
  165. {
  166. if (!__builtin_constant_p(n))
  167. __check_object_size(ptr, n, to_user);
  168. }
  169. #else
  170. static inline void check_object_size(const void *ptr, unsigned long n,
  171. bool to_user)
  172. { }
  173. #endif /* CONFIG_HARDENED_USERCOPY */
  174. extern void __compiletime_error("copy source size is too small")
  175. __bad_copy_from(void);
  176. extern void __compiletime_error("copy destination size is too small")
  177. __bad_copy_to(void);
  178. void __copy_overflow(int size, unsigned long count);
  179. static inline void copy_overflow(int size, unsigned long count)
  180. {
  181. if (IS_ENABLED(CONFIG_BUG))
  182. __copy_overflow(size, count);
  183. }
  184. static __always_inline __must_check bool
  185. check_copy_size(const void *addr, size_t bytes, bool is_source)
  186. {
  187. int sz = __builtin_object_size(addr, 0);
  188. if (unlikely(sz >= 0 && sz < bytes)) {
  189. if (!__builtin_constant_p(bytes))
  190. copy_overflow(sz, bytes);
  191. else if (is_source)
  192. __bad_copy_from();
  193. else
  194. __bad_copy_to();
  195. return false;
  196. }
  197. if (WARN_ON_ONCE(bytes > INT_MAX))
  198. return false;
  199. check_object_size(addr, bytes, is_source);
  200. return true;
  201. }
  202. #ifndef arch_setup_new_exec
  203. static inline void arch_setup_new_exec(void) { }
  204. #endif
  205. #endif /* __KERNEL__ */
  206. #endif /* _LINUX_THREAD_INFO_H */