preempt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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: 0x00f00000
  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 4
  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_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
  48. /*
  49. * Disable preemption until the scheduler is running -- use an unconditional
  50. * value so that it also works on !PREEMPT_COUNT kernels.
  51. *
  52. * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
  53. */
  54. #define INIT_PREEMPT_COUNT PREEMPT_OFFSET
  55. /*
  56. * Initial preempt_count value; reflects the preempt_count schedule invariant
  57. * which states that during context switches:
  58. *
  59. * preempt_count() == 2*PREEMPT_DISABLE_OFFSET
  60. *
  61. * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
  62. * Note: See finish_task_switch().
  63. */
  64. #define FORK_PREEMPT_COUNT (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
  65. /* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
  66. #include <asm/preempt.h>
  67. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  68. #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  69. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  70. | NMI_MASK))
  71. /*
  72. * Are we doing bottom half or hardware interrupt processing?
  73. *
  74. * in_irq() - We're in (hard) IRQ context
  75. * in_softirq() - We have BH disabled, or are processing softirqs
  76. * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled
  77. * in_serving_softirq() - We're in softirq context
  78. * in_nmi() - We're in NMI context
  79. * in_task() - We're in task context
  80. *
  81. * Note: due to the BH disabled confusion: in_softirq(),in_interrupt() really
  82. * should not be used in new code.
  83. */
  84. #define in_irq() (hardirq_count())
  85. #define in_softirq() (softirq_count())
  86. #define in_interrupt() (irq_count())
  87. #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  88. #define in_nmi() (preempt_count() & NMI_MASK)
  89. #define in_task() (!(preempt_count() & \
  90. (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
  91. /*
  92. * The preempt_count offset after preempt_disable();
  93. */
  94. #if defined(CONFIG_PREEMPT_COUNT)
  95. # define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
  96. #else
  97. # define PREEMPT_DISABLE_OFFSET 0
  98. #endif
  99. /*
  100. * The preempt_count offset after spin_lock()
  101. */
  102. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  103. /*
  104. * The preempt_count offset needed for things like:
  105. *
  106. * spin_lock_bh()
  107. *
  108. * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
  109. * softirqs, such that unlock sequences of:
  110. *
  111. * spin_unlock();
  112. * local_bh_enable();
  113. *
  114. * Work as expected.
  115. */
  116. #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
  117. /*
  118. * Are we running in atomic context? WARNING: this macro cannot
  119. * always detect atomic context; in particular, it cannot know about
  120. * held spinlocks in non-preemptible kernels. Thus it should not be
  121. * used in the general case to determine whether sleeping is possible.
  122. * Do not use in_atomic() in driver code.
  123. */
  124. #define in_atomic() (preempt_count() != 0)
  125. /*
  126. * Check whether we were atomic before we did preempt_disable():
  127. * (used by the scheduler)
  128. */
  129. #define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
  130. #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE)
  131. extern void preempt_count_add(int val);
  132. extern void preempt_count_sub(int val);
  133. #define preempt_count_dec_and_test() \
  134. ({ preempt_count_sub(1); should_resched(0); })
  135. #else
  136. #define preempt_count_add(val) __preempt_count_add(val)
  137. #define preempt_count_sub(val) __preempt_count_sub(val)
  138. #define preempt_count_dec_and_test() __preempt_count_dec_and_test()
  139. #endif
  140. #define __preempt_count_inc() __preempt_count_add(1)
  141. #define __preempt_count_dec() __preempt_count_sub(1)
  142. #define preempt_count_inc() preempt_count_add(1)
  143. #define preempt_count_dec() preempt_count_sub(1)
  144. #ifdef CONFIG_PREEMPT_COUNT
  145. #define preempt_disable() \
  146. do { \
  147. preempt_count_inc(); \
  148. barrier(); \
  149. } while (0)
  150. #define sched_preempt_enable_no_resched() \
  151. do { \
  152. barrier(); \
  153. preempt_count_dec(); \
  154. } while (0)
  155. #define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  156. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  157. #ifdef CONFIG_PREEMPTION
  158. #define preempt_enable() \
  159. do { \
  160. barrier(); \
  161. if (unlikely(preempt_count_dec_and_test())) \
  162. __preempt_schedule(); \
  163. } while (0)
  164. #define preempt_enable_notrace() \
  165. do { \
  166. barrier(); \
  167. if (unlikely(__preempt_count_dec_and_test())) \
  168. __preempt_schedule_notrace(); \
  169. } while (0)
  170. #define preempt_check_resched() \
  171. do { \
  172. if (should_resched(0)) \
  173. __preempt_schedule(); \
  174. } while (0)
  175. #else /* !CONFIG_PREEMPTION */
  176. #define preempt_enable() \
  177. do { \
  178. barrier(); \
  179. preempt_count_dec(); \
  180. } while (0)
  181. #define preempt_enable_notrace() \
  182. do { \
  183. barrier(); \
  184. __preempt_count_dec(); \
  185. } while (0)
  186. #define preempt_check_resched() do { } while (0)
  187. #endif /* CONFIG_PREEMPTION */
  188. #define preempt_disable_notrace() \
  189. do { \
  190. __preempt_count_inc(); \
  191. barrier(); \
  192. } while (0)
  193. #define preempt_enable_no_resched_notrace() \
  194. do { \
  195. barrier(); \
  196. __preempt_count_dec(); \
  197. } while (0)
  198. #else /* !CONFIG_PREEMPT_COUNT */
  199. /*
  200. * Even if we don't have any preemption, we need preempt disable/enable
  201. * to be barriers, so that we don't have things like get_user/put_user
  202. * that can cause faults and scheduling migrate into our preempt-protected
  203. * region.
  204. */
  205. #define preempt_disable() barrier()
  206. #define sched_preempt_enable_no_resched() barrier()
  207. #define preempt_enable_no_resched() barrier()
  208. #define preempt_enable() barrier()
  209. #define preempt_check_resched() do { } while (0)
  210. #define preempt_disable_notrace() barrier()
  211. #define preempt_enable_no_resched_notrace() barrier()
  212. #define preempt_enable_notrace() barrier()
  213. #define preemptible() 0
  214. #endif /* CONFIG_PREEMPT_COUNT */
  215. #ifdef MODULE
  216. /*
  217. * Modules have no business playing preemption tricks.
  218. */
  219. #undef sched_preempt_enable_no_resched
  220. #undef preempt_enable_no_resched
  221. #undef preempt_enable_no_resched_notrace
  222. #undef preempt_check_resched
  223. #endif
  224. #define preempt_set_need_resched() \
  225. do { \
  226. set_preempt_need_resched(); \
  227. } while (0)
  228. #define preempt_fold_need_resched() \
  229. do { \
  230. if (tif_need_resched()) \
  231. set_preempt_need_resched(); \
  232. } while (0)
  233. #ifdef CONFIG_PREEMPT_NOTIFIERS
  234. struct preempt_notifier;
  235. /**
  236. * preempt_ops - notifiers called when a task is preempted and rescheduled
  237. * @sched_in: we're about to be rescheduled:
  238. * notifier: struct preempt_notifier for the task being scheduled
  239. * cpu: cpu we're scheduled on
  240. * @sched_out: we've just been preempted
  241. * notifier: struct preempt_notifier for the task being preempted
  242. * next: the task that's kicking us out
  243. *
  244. * Please note that sched_in and out are called under different
  245. * contexts. sched_out is called with rq lock held and irq disabled
  246. * while sched_in is called without rq lock and irq enabled. This
  247. * difference is intentional and depended upon by its users.
  248. */
  249. struct preempt_ops {
  250. void (*sched_in)(struct preempt_notifier *notifier, int cpu);
  251. void (*sched_out)(struct preempt_notifier *notifier,
  252. struct task_struct *next);
  253. };
  254. /**
  255. * preempt_notifier - key for installing preemption notifiers
  256. * @link: internal use
  257. * @ops: defines the notifier functions to be called
  258. *
  259. * Usually used in conjunction with container_of().
  260. */
  261. struct preempt_notifier {
  262. struct hlist_node link;
  263. struct preempt_ops *ops;
  264. };
  265. void preempt_notifier_inc(void);
  266. void preempt_notifier_dec(void);
  267. void preempt_notifier_register(struct preempt_notifier *notifier);
  268. void preempt_notifier_unregister(struct preempt_notifier *notifier);
  269. static inline void preempt_notifier_init(struct preempt_notifier *notifier,
  270. struct preempt_ops *ops)
  271. {
  272. INIT_HLIST_NODE(&notifier->link);
  273. notifier->ops = ops;
  274. }
  275. #endif
  276. /**
  277. * migrate_disable - Prevent migration of the current task
  278. *
  279. * Maps to preempt_disable() which also disables preemption. Use
  280. * migrate_disable() to annotate that the intent is to prevent migration,
  281. * but not necessarily preemption.
  282. *
  283. * Can be invoked nested like preempt_disable() and needs the corresponding
  284. * number of migrate_enable() invocations.
  285. */
  286. static __always_inline void migrate_disable(void)
  287. {
  288. preempt_disable();
  289. }
  290. /**
  291. * migrate_enable - Allow migration of the current task
  292. *
  293. * Counterpart to migrate_disable().
  294. *
  295. * As migrate_disable() can be invoked nested, only the outermost invocation
  296. * reenables migration.
  297. *
  298. * Currently mapped to preempt_enable().
  299. */
  300. static __always_inline void migrate_enable(void)
  301. {
  302. preempt_enable();
  303. }
  304. #endif /* __LINUX_PREEMPT_H */