thread_info.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #ifndef _LINUX_THREAD_INFO_H
  8. #define _LINUX_THREAD_INFO_H
  9. #include <linux/types.h>
  10. #include <linux/bug.h>
  11. #include <linux/restart_block.h>
  12. #include <linux/errno.h>
  13. #ifdef CONFIG_THREAD_INFO_IN_TASK
  14. /*
  15. * For CONFIG_THREAD_INFO_IN_TASK kernels we need <asm/current.h> for the
  16. * definition of current, but for !CONFIG_THREAD_INFO_IN_TASK kernels,
  17. * including <asm/current.h> can cause a circular dependency on some platforms.
  18. */
  19. #include <asm/current.h>
  20. #define current_thread_info() ((struct thread_info *)current)
  21. #endif
  22. #include <linux/bitops.h>
  23. /*
  24. * For per-arch arch_within_stack_frames() implementations, defined in
  25. * asm/thread_info.h.
  26. */
  27. enum {
  28. BAD_STACK = -1,
  29. NOT_STACK = 0,
  30. GOOD_FRAME,
  31. GOOD_STACK,
  32. };
  33. #include <asm/thread_info.h>
  34. #ifdef __KERNEL__
  35. #ifndef arch_set_restart_data
  36. #define arch_set_restart_data(restart) do { } while (0)
  37. #endif
  38. static inline long set_restart_fn(struct restart_block *restart,
  39. long (*fn)(struct restart_block *))
  40. {
  41. restart->fn = fn;
  42. arch_set_restart_data(restart);
  43. return -ERESTART_RESTARTBLOCK;
  44. }
  45. #ifndef THREAD_ALIGN
  46. #define THREAD_ALIGN THREAD_SIZE
  47. #endif
  48. #define THREADINFO_GFP (GFP_KERNEL_ACCOUNT | __GFP_ZERO)
  49. /*
  50. * flag set/clear/test wrappers
  51. * - pass TIF_xxxx constants to these functions
  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. static inline void clear_ti_thread_flag(struct thread_info *ti, int flag)
  58. {
  59. clear_bit(flag, (unsigned long *)&ti->flags);
  60. }
  61. static inline void update_ti_thread_flag(struct thread_info *ti, int flag,
  62. bool value)
  63. {
  64. if (value)
  65. set_ti_thread_flag(ti, flag);
  66. else
  67. clear_ti_thread_flag(ti, flag);
  68. }
  69. static inline int test_and_set_ti_thread_flag(struct thread_info *ti, int flag)
  70. {
  71. return test_and_set_bit(flag, (unsigned long *)&ti->flags);
  72. }
  73. static inline int test_and_clear_ti_thread_flag(struct thread_info *ti, int flag)
  74. {
  75. return test_and_clear_bit(flag, (unsigned long *)&ti->flags);
  76. }
  77. static inline int test_ti_thread_flag(struct thread_info *ti, int flag)
  78. {
  79. return test_bit(flag, (unsigned long *)&ti->flags);
  80. }
  81. #define set_thread_flag(flag) \
  82. set_ti_thread_flag(current_thread_info(), flag)
  83. #define clear_thread_flag(flag) \
  84. clear_ti_thread_flag(current_thread_info(), flag)
  85. #define update_thread_flag(flag, value) \
  86. update_ti_thread_flag(current_thread_info(), flag, value)
  87. #define test_and_set_thread_flag(flag) \
  88. test_and_set_ti_thread_flag(current_thread_info(), flag)
  89. #define test_and_clear_thread_flag(flag) \
  90. test_and_clear_ti_thread_flag(current_thread_info(), flag)
  91. #define test_thread_flag(flag) \
  92. test_ti_thread_flag(current_thread_info(), flag)
  93. #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED)
  94. #ifndef CONFIG_HAVE_ARCH_WITHIN_STACK_FRAMES
  95. static inline int arch_within_stack_frames(const void * const stack,
  96. const void * const stackend,
  97. const void *obj, unsigned long len)
  98. {
  99. return 0;
  100. }
  101. #endif
  102. #ifdef CONFIG_HARDENED_USERCOPY
  103. extern void __check_object_size(const void *ptr, unsigned long n,
  104. bool to_user);
  105. static __always_inline void check_object_size(const void *ptr, unsigned long n,
  106. bool to_user)
  107. {
  108. if (!__builtin_constant_p(n))
  109. __check_object_size(ptr, n, to_user);
  110. }
  111. #else
  112. static inline void check_object_size(const void *ptr, unsigned long n,
  113. bool to_user)
  114. { }
  115. #endif /* CONFIG_HARDENED_USERCOPY */
  116. extern void __compiletime_error("copy source size is too small")
  117. __bad_copy_from(void);
  118. extern void __compiletime_error("copy destination size is too small")
  119. __bad_copy_to(void);
  120. static inline void copy_overflow(int size, unsigned long count)
  121. {
  122. WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
  123. }
  124. static __always_inline __must_check bool
  125. check_copy_size(const void *addr, size_t bytes, bool is_source)
  126. {
  127. int sz = __compiletime_object_size(addr);
  128. if (unlikely(sz >= 0 && sz < bytes)) {
  129. if (!__builtin_constant_p(bytes))
  130. copy_overflow(sz, bytes);
  131. else if (is_source)
  132. __bad_copy_from();
  133. else
  134. __bad_copy_to();
  135. return false;
  136. }
  137. if (WARN_ON_ONCE(bytes > INT_MAX))
  138. return false;
  139. check_object_size(addr, bytes, is_source);
  140. return true;
  141. }
  142. #ifndef arch_setup_new_exec
  143. static inline void arch_setup_new_exec(void) { }
  144. #endif
  145. #endif /* __KERNEL__ */
  146. #endif /* _LINUX_THREAD_INFO_H */