thread_info.h 4.1 KB

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