preempt.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/linkage.h>
  8. #include <linux/list.h>
  9. /*
  10. * We put the hardirq and softirq counter into the preemption
  11. * counter. The bitmask has the following meaning:
  12. *
  13. * - bits 0-7 are the preemption count (max preemption depth: 256)
  14. * - bits 8-15 are the softirq count (max # of softirqs: 256)
  15. *
  16. * The hardirq count could in theory be the same as the number of
  17. * interrupts in the system, but we run all interrupt handlers with
  18. * interrupts disabled, so we cannot have nesting interrupts. Though
  19. * there are a few palaeontologic drivers which reenable interrupts in
  20. * the handler, so we need more than one bit here.
  21. *
  22. * PREEMPT_MASK: 0x000000ff
  23. * SOFTIRQ_MASK: 0x0000ff00
  24. * HARDIRQ_MASK: 0x000f0000
  25. * NMI_MASK: 0x00100000
  26. * PREEMPT_ACTIVE: 0x00200000
  27. * PREEMPT_NEED_RESCHED: 0x80000000
  28. */
  29. #define PREEMPT_BITS 8
  30. #define SOFTIRQ_BITS 8
  31. #define HARDIRQ_BITS 4
  32. #define NMI_BITS 1
  33. #define PREEMPT_SHIFT 0
  34. #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
  35. #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
  36. #define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
  37. #define __IRQ_MASK(x) ((1UL << (x))-1)
  38. #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
  39. #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
  40. #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
  41. #define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
  42. #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
  43. #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
  44. #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
  45. #define NMI_OFFSET (1UL << NMI_SHIFT)
  46. #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
  47. #define PREEMPT_ACTIVE_BITS 1
  48. #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
  49. #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
  50. /* We use the MSB mostly because its available */
  51. #define PREEMPT_NEED_RESCHED 0x80000000
  52. /* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
  53. #include <asm/preempt.h>
  54. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  55. #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  56. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  57. | NMI_MASK))
  58. /*
  59. * Are we doing bottom half or hardware interrupt processing?
  60. * Are we in a softirq context? Interrupt context?
  61. * in_softirq - Are we currently processing softirq or have bh disabled?
  62. * in_serving_softirq - Are we currently processing softirq?
  63. */
  64. #define in_irq() (hardirq_count())
  65. #define in_softirq() (softirq_count())
  66. #define in_interrupt() (irq_count())
  67. #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  68. /*
  69. * Are we in NMI context?
  70. */
  71. #define in_nmi() (preempt_count() & NMI_MASK)
  72. #if defined(CONFIG_PREEMPT_COUNT)
  73. # define PREEMPT_DISABLE_OFFSET 1
  74. #else
  75. # define PREEMPT_DISABLE_OFFSET 0
  76. #endif
  77. /*
  78. * The preempt_count offset needed for things like:
  79. *
  80. * spin_lock_bh()
  81. *
  82. * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
  83. * softirqs, such that unlock sequences of:
  84. *
  85. * spin_unlock();
  86. * local_bh_enable();
  87. *
  88. * Work as expected.
  89. */
  90. #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_DISABLE_OFFSET)
  91. /*
  92. * Are we running in atomic context? WARNING: this macro cannot
  93. * always detect atomic context; in particular, it cannot know about
  94. * held spinlocks in non-preemptible kernels. Thus it should not be
  95. * used in the general case to determine whether sleeping is possible.
  96. * Do not use in_atomic() in driver code.
  97. */
  98. #define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
  99. /*
  100. * Check whether we were atomic before we did preempt_disable():
  101. * (used by the scheduler, *after* releasing the kernel lock)
  102. */
  103. #define in_atomic_preempt_off() \
  104. ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_DISABLE_OFFSET)
  105. #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
  106. extern void preempt_count_add(int val);
  107. extern void preempt_count_sub(int val);
  108. #define preempt_count_dec_and_test() ({ preempt_count_sub(1); should_resched(); })
  109. #else
  110. #define preempt_count_add(val) __preempt_count_add(val)
  111. #define preempt_count_sub(val) __preempt_count_sub(val)
  112. #define preempt_count_dec_and_test() __preempt_count_dec_and_test()
  113. #endif
  114. #define __preempt_count_inc() __preempt_count_add(1)
  115. #define __preempt_count_dec() __preempt_count_sub(1)
  116. #define preempt_count_inc() preempt_count_add(1)
  117. #define preempt_count_dec() preempt_count_sub(1)
  118. #ifdef CONFIG_PREEMPT_COUNT
  119. #define preempt_disable() \
  120. do { \
  121. preempt_count_inc(); \
  122. barrier(); \
  123. } while (0)
  124. #define sched_preempt_enable_no_resched() \
  125. do { \
  126. barrier(); \
  127. preempt_count_dec(); \
  128. } while (0)
  129. #define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  130. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  131. #ifdef CONFIG_PREEMPT
  132. #define preempt_enable() \
  133. do { \
  134. barrier(); \
  135. if (unlikely(preempt_count_dec_and_test())) \
  136. __preempt_schedule(); \
  137. } while (0)
  138. #define preempt_check_resched() \
  139. do { \
  140. if (should_resched()) \
  141. __preempt_schedule(); \
  142. } while (0)
  143. #else
  144. #define preempt_enable() \
  145. do { \
  146. barrier(); \
  147. preempt_count_dec(); \
  148. } while (0)
  149. #define preempt_check_resched() do { } while (0)
  150. #endif
  151. #define preempt_disable_notrace() \
  152. do { \
  153. __preempt_count_inc(); \
  154. barrier(); \
  155. } while (0)
  156. #define preempt_enable_no_resched_notrace() \
  157. do { \
  158. barrier(); \
  159. __preempt_count_dec(); \
  160. } while (0)
  161. #ifdef CONFIG_PREEMPT
  162. #ifndef CONFIG_CONTEXT_TRACKING
  163. #define __preempt_schedule_context() __preempt_schedule()
  164. #endif
  165. #define preempt_enable_notrace() \
  166. do { \
  167. barrier(); \
  168. if (unlikely(__preempt_count_dec_and_test())) \
  169. __preempt_schedule_context(); \
  170. } while (0)
  171. #else
  172. #define preempt_enable_notrace() \
  173. do { \
  174. barrier(); \
  175. __preempt_count_dec(); \
  176. } while (0)
  177. #endif
  178. #else /* !CONFIG_PREEMPT_COUNT */
  179. /*
  180. * Even if we don't have any preemption, we need preempt disable/enable
  181. * to be barriers, so that we don't have things like get_user/put_user
  182. * that can cause faults and scheduling migrate into our preempt-protected
  183. * region.
  184. */
  185. #define preempt_disable() barrier()
  186. #define sched_preempt_enable_no_resched() barrier()
  187. #define preempt_enable_no_resched() barrier()
  188. #define preempt_enable() barrier()
  189. #define preempt_check_resched() do { } while (0)
  190. #define preempt_disable_notrace() barrier()
  191. #define preempt_enable_no_resched_notrace() barrier()
  192. #define preempt_enable_notrace() barrier()
  193. #define preemptible() 0
  194. #endif /* CONFIG_PREEMPT_COUNT */
  195. #ifdef MODULE
  196. /*
  197. * Modules have no business playing preemption tricks.
  198. */
  199. #undef sched_preempt_enable_no_resched
  200. #undef preempt_enable_no_resched
  201. #undef preempt_enable_no_resched_notrace
  202. #undef preempt_check_resched
  203. #endif
  204. #define preempt_set_need_resched() \
  205. do { \
  206. set_preempt_need_resched(); \
  207. } while (0)
  208. #define preempt_fold_need_resched() \
  209. do { \
  210. if (tif_need_resched()) \
  211. set_preempt_need_resched(); \
  212. } while (0)
  213. #ifdef CONFIG_PREEMPT_NOTIFIERS
  214. struct preempt_notifier;
  215. /**
  216. * preempt_ops - notifiers called when a task is preempted and rescheduled
  217. * @sched_in: we're about to be rescheduled:
  218. * notifier: struct preempt_notifier for the task being scheduled
  219. * cpu: cpu we're scheduled on
  220. * @sched_out: we've just been preempted
  221. * notifier: struct preempt_notifier for the task being preempted
  222. * next: the task that's kicking us out
  223. *
  224. * Please note that sched_in and out are called under different
  225. * contexts. sched_out is called with rq lock held and irq disabled
  226. * while sched_in is called without rq lock and irq enabled. This
  227. * difference is intentional and depended upon by its users.
  228. */
  229. struct preempt_ops {
  230. void (*sched_in)(struct preempt_notifier *notifier, int cpu);
  231. void (*sched_out)(struct preempt_notifier *notifier,
  232. struct task_struct *next);
  233. };
  234. /**
  235. * preempt_notifier - key for installing preemption notifiers
  236. * @link: internal use
  237. * @ops: defines the notifier functions to be called
  238. *
  239. * Usually used in conjunction with container_of().
  240. */
  241. struct preempt_notifier {
  242. struct hlist_node link;
  243. struct preempt_ops *ops;
  244. };
  245. void preempt_notifier_register(struct preempt_notifier *notifier);
  246. void preempt_notifier_unregister(struct preempt_notifier *notifier);
  247. static inline void preempt_notifier_init(struct preempt_notifier *notifier,
  248. struct preempt_ops *ops)
  249. {
  250. INIT_HLIST_NODE(&notifier->link);
  251. notifier->ops = ops;
  252. }
  253. #endif
  254. #endif /* __LINUX_PREEMPT_H */