preempt.h 9.3 KB

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