preempt.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. static __always_inline int preempt_count(void)
  11. {
  12. return current_thread_info()->preempt_count;
  13. }
  14. static __always_inline int *preempt_count_ptr(void)
  15. {
  16. return &current_thread_info()->preempt_count;
  17. }
  18. static __always_inline void preempt_count_set(int pc)
  19. {
  20. *preempt_count_ptr() = pc;
  21. }
  22. #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
  23. extern void add_preempt_count(int val);
  24. extern void sub_preempt_count(int val);
  25. #else
  26. # define add_preempt_count(val) do { *preempt_count_ptr() += (val); } while (0)
  27. # define sub_preempt_count(val) do { *preempt_count_ptr() -= (val); } while (0)
  28. #endif
  29. #define inc_preempt_count() add_preempt_count(1)
  30. #define dec_preempt_count() sub_preempt_count(1)
  31. #ifdef CONFIG_PREEMPT
  32. asmlinkage void preempt_schedule(void);
  33. #define preempt_check_resched() \
  34. do { \
  35. if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) \
  36. preempt_schedule(); \
  37. } while (0)
  38. #ifdef CONFIG_CONTEXT_TRACKING
  39. void preempt_schedule_context(void);
  40. #define preempt_check_resched_context() \
  41. do { \
  42. if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) \
  43. preempt_schedule_context(); \
  44. } while (0)
  45. #else
  46. #define preempt_check_resched_context() preempt_check_resched()
  47. #endif /* CONFIG_CONTEXT_TRACKING */
  48. #else /* !CONFIG_PREEMPT */
  49. #define preempt_check_resched() do { } while (0)
  50. #define preempt_check_resched_context() do { } while (0)
  51. #endif /* CONFIG_PREEMPT */
  52. #ifdef CONFIG_PREEMPT_COUNT
  53. #define preempt_disable() \
  54. do { \
  55. inc_preempt_count(); \
  56. barrier(); \
  57. } while (0)
  58. #define sched_preempt_enable_no_resched() \
  59. do { \
  60. barrier(); \
  61. dec_preempt_count(); \
  62. } while (0)
  63. #define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  64. #define preempt_enable() \
  65. do { \
  66. preempt_enable_no_resched(); \
  67. barrier(); \
  68. preempt_check_resched(); \
  69. } while (0)
  70. /* For debugging and tracer internals only! */
  71. #define add_preempt_count_notrace(val) \
  72. do { *preempt_count_ptr() += (val); } while (0)
  73. #define sub_preempt_count_notrace(val) \
  74. do { *preempt_count_ptr() -= (val); } while (0)
  75. #define inc_preempt_count_notrace() add_preempt_count_notrace(1)
  76. #define dec_preempt_count_notrace() sub_preempt_count_notrace(1)
  77. #define preempt_disable_notrace() \
  78. do { \
  79. inc_preempt_count_notrace(); \
  80. barrier(); \
  81. } while (0)
  82. #define preempt_enable_no_resched_notrace() \
  83. do { \
  84. barrier(); \
  85. dec_preempt_count_notrace(); \
  86. } while (0)
  87. /* preempt_check_resched is OK to trace */
  88. #define preempt_enable_notrace() \
  89. do { \
  90. preempt_enable_no_resched_notrace(); \
  91. barrier(); \
  92. preempt_check_resched_context(); \
  93. } while (0)
  94. #else /* !CONFIG_PREEMPT_COUNT */
  95. /*
  96. * Even if we don't have any preemption, we need preempt disable/enable
  97. * to be barriers, so that we don't have things like get_user/put_user
  98. * that can cause faults and scheduling migrate into our preempt-protected
  99. * region.
  100. */
  101. #define preempt_disable() barrier()
  102. #define sched_preempt_enable_no_resched() barrier()
  103. #define preempt_enable_no_resched() barrier()
  104. #define preempt_enable() barrier()
  105. #define preempt_disable_notrace() barrier()
  106. #define preempt_enable_no_resched_notrace() barrier()
  107. #define preempt_enable_notrace() barrier()
  108. #endif /* CONFIG_PREEMPT_COUNT */
  109. #ifdef CONFIG_PREEMPT_NOTIFIERS
  110. struct preempt_notifier;
  111. /**
  112. * preempt_ops - notifiers called when a task is preempted and rescheduled
  113. * @sched_in: we're about to be rescheduled:
  114. * notifier: struct preempt_notifier for the task being scheduled
  115. * cpu: cpu we're scheduled on
  116. * @sched_out: we've just been preempted
  117. * notifier: struct preempt_notifier for the task being preempted
  118. * next: the task that's kicking us out
  119. *
  120. * Please note that sched_in and out are called under different
  121. * contexts. sched_out is called with rq lock held and irq disabled
  122. * while sched_in is called without rq lock and irq enabled. This
  123. * difference is intentional and depended upon by its users.
  124. */
  125. struct preempt_ops {
  126. void (*sched_in)(struct preempt_notifier *notifier, int cpu);
  127. void (*sched_out)(struct preempt_notifier *notifier,
  128. struct task_struct *next);
  129. };
  130. /**
  131. * preempt_notifier - key for installing preemption notifiers
  132. * @link: internal use
  133. * @ops: defines the notifier functions to be called
  134. *
  135. * Usually used in conjunction with container_of().
  136. */
  137. struct preempt_notifier {
  138. struct hlist_node link;
  139. struct preempt_ops *ops;
  140. };
  141. void preempt_notifier_register(struct preempt_notifier *notifier);
  142. void preempt_notifier_unregister(struct preempt_notifier *notifier);
  143. static inline void preempt_notifier_init(struct preempt_notifier *notifier,
  144. struct preempt_ops *ops)
  145. {
  146. INIT_HLIST_NODE(&notifier->link);
  147. notifier->ops = ops;
  148. }
  149. #endif
  150. #endif /* __LINUX_PREEMPT_H */