preempt.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #ifndef __LINUX_PREEMPT_H
  2. #define __LINUX_PREEMPT_H
  3. /*
  4. * include/linux/preempt.h - macros for accessing and manipulating
  5. * preempt_count (used for kernel preemption, interrupt count, etc.)
  6. */
  7. #include <linux/thread_info.h>
  8. #include <linux/linkage.h>
  9. #include <linux/list.h>
  10. /*
  11. * We use the MSB mostly because its available; see <linux/preempt_mask.h> for
  12. * the other bits -- can't include that header due to inclusion hell.
  13. */
  14. #define PREEMPT_NEED_RESCHED 0x80000000
  15. /*
  16. * We mask the PREEMPT_NEED_RESCHED bit so as not to confuse all current users
  17. * that think a non-zero value indicates we cannot preempt.
  18. */
  19. static __always_inline int preempt_count(void)
  20. {
  21. return current_thread_info()->preempt_count & ~PREEMPT_NEED_RESCHED;
  22. }
  23. static __always_inline int *preempt_count_ptr(void)
  24. {
  25. return &current_thread_info()->preempt_count;
  26. }
  27. /*
  28. * We now loose PREEMPT_NEED_RESCHED and cause an extra reschedule; however the
  29. * alternative is loosing a reschedule. Better schedule too often -- also this
  30. * should be a very rare operation.
  31. */
  32. static __always_inline void preempt_count_set(int pc)
  33. {
  34. *preempt_count_ptr() = pc;
  35. }
  36. /*
  37. * We fold the NEED_RESCHED bit into the preempt count such that
  38. * preempt_enable() can decrement and test for needing to reschedule with a
  39. * single instruction.
  40. *
  41. * We invert the actual bit, so that when the decrement hits 0 we know we both
  42. * need to resched (the bit is cleared) and can resched (no preempt count).
  43. */
  44. static __always_inline void set_preempt_need_resched(void)
  45. {
  46. *preempt_count_ptr() &= ~PREEMPT_NEED_RESCHED;
  47. }
  48. static __always_inline void clear_preempt_need_resched(void)
  49. {
  50. *preempt_count_ptr() |= PREEMPT_NEED_RESCHED;
  51. }
  52. static __always_inline bool test_preempt_need_resched(void)
  53. {
  54. return !(*preempt_count_ptr() & PREEMPT_NEED_RESCHED);
  55. }
  56. #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
  57. extern void add_preempt_count(int val);
  58. extern void sub_preempt_count(int val);
  59. #else
  60. # define add_preempt_count(val) do { *preempt_count_ptr() += (val); } while (0)
  61. # define sub_preempt_count(val) do { *preempt_count_ptr() -= (val); } while (0)
  62. #endif
  63. #define inc_preempt_count() add_preempt_count(1)
  64. #define dec_preempt_count() sub_preempt_count(1)
  65. #ifdef CONFIG_PREEMPT
  66. asmlinkage void preempt_schedule(void);
  67. #define preempt_check_resched() \
  68. do { \
  69. if (unlikely(!*preempt_count_ptr())) \
  70. preempt_schedule(); \
  71. } while (0)
  72. #ifdef CONFIG_CONTEXT_TRACKING
  73. void preempt_schedule_context(void);
  74. #define preempt_check_resched_context() \
  75. do { \
  76. if (unlikely(!*preempt_count_ptr())) \
  77. preempt_schedule_context(); \
  78. } while (0)
  79. #else
  80. #define preempt_check_resched_context() preempt_check_resched()
  81. #endif /* CONFIG_CONTEXT_TRACKING */
  82. #else /* !CONFIG_PREEMPT */
  83. #define preempt_check_resched() do { } while (0)
  84. #define preempt_check_resched_context() do { } while (0)
  85. #endif /* CONFIG_PREEMPT */
  86. #ifdef CONFIG_PREEMPT_COUNT
  87. #define preempt_disable() \
  88. do { \
  89. inc_preempt_count(); \
  90. barrier(); \
  91. } while (0)
  92. #define sched_preempt_enable_no_resched() \
  93. do { \
  94. barrier(); \
  95. dec_preempt_count(); \
  96. } while (0)
  97. #define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  98. #define preempt_enable() \
  99. do { \
  100. preempt_enable_no_resched(); \
  101. preempt_check_resched(); \
  102. } while (0)
  103. /* For debugging and tracer internals only! */
  104. #define add_preempt_count_notrace(val) \
  105. do { *preempt_count_ptr() += (val); } while (0)
  106. #define sub_preempt_count_notrace(val) \
  107. do { *preempt_count_ptr() -= (val); } while (0)
  108. #define inc_preempt_count_notrace() add_preempt_count_notrace(1)
  109. #define dec_preempt_count_notrace() sub_preempt_count_notrace(1)
  110. #define preempt_disable_notrace() \
  111. do { \
  112. inc_preempt_count_notrace(); \
  113. barrier(); \
  114. } while (0)
  115. #define preempt_enable_no_resched_notrace() \
  116. do { \
  117. barrier(); \
  118. dec_preempt_count_notrace(); \
  119. } while (0)
  120. /* preempt_check_resched is OK to trace */
  121. #define preempt_enable_notrace() \
  122. do { \
  123. preempt_enable_no_resched_notrace(); \
  124. preempt_check_resched_context(); \
  125. } while (0)
  126. #else /* !CONFIG_PREEMPT_COUNT */
  127. /*
  128. * Even if we don't have any preemption, we need preempt disable/enable
  129. * to be barriers, so that we don't have things like get_user/put_user
  130. * that can cause faults and scheduling migrate into our preempt-protected
  131. * region.
  132. */
  133. #define preempt_disable() barrier()
  134. #define sched_preempt_enable_no_resched() barrier()
  135. #define preempt_enable_no_resched() barrier()
  136. #define preempt_enable() barrier()
  137. #define preempt_disable_notrace() barrier()
  138. #define preempt_enable_no_resched_notrace() barrier()
  139. #define preempt_enable_notrace() barrier()
  140. #endif /* CONFIG_PREEMPT_COUNT */
  141. #ifdef CONFIG_PREEMPT_NOTIFIERS
  142. struct preempt_notifier;
  143. /**
  144. * preempt_ops - notifiers called when a task is preempted and rescheduled
  145. * @sched_in: we're about to be rescheduled:
  146. * notifier: struct preempt_notifier for the task being scheduled
  147. * cpu: cpu we're scheduled on
  148. * @sched_out: we've just been preempted
  149. * notifier: struct preempt_notifier for the task being preempted
  150. * next: the task that's kicking us out
  151. *
  152. * Please note that sched_in and out are called under different
  153. * contexts. sched_out is called with rq lock held and irq disabled
  154. * while sched_in is called without rq lock and irq enabled. This
  155. * difference is intentional and depended upon by its users.
  156. */
  157. struct preempt_ops {
  158. void (*sched_in)(struct preempt_notifier *notifier, int cpu);
  159. void (*sched_out)(struct preempt_notifier *notifier,
  160. struct task_struct *next);
  161. };
  162. /**
  163. * preempt_notifier - key for installing preemption notifiers
  164. * @link: internal use
  165. * @ops: defines the notifier functions to be called
  166. *
  167. * Usually used in conjunction with container_of().
  168. */
  169. struct preempt_notifier {
  170. struct hlist_node link;
  171. struct preempt_ops *ops;
  172. };
  173. void preempt_notifier_register(struct preempt_notifier *notifier);
  174. void preempt_notifier_unregister(struct preempt_notifier *notifier);
  175. static inline void preempt_notifier_init(struct preempt_notifier *notifier,
  176. struct preempt_ops *ops)
  177. {
  178. INIT_HLIST_NODE(&notifier->link);
  179. notifier->ops = ops;
  180. }
  181. #endif
  182. #endif /* __LINUX_PREEMPT_H */