preempt.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. /**
  68. * interrupt_context_level - return interrupt context level
  69. *
  70. * Returns the current interrupt context level.
  71. * 0 - normal context
  72. * 1 - softirq context
  73. * 2 - hardirq context
  74. * 3 - NMI context
  75. */
  76. static __always_inline unsigned char interrupt_context_level(void)
  77. {
  78. unsigned long pc = preempt_count();
  79. unsigned char level = 0;
  80. level += !!(pc & (NMI_MASK));
  81. level += !!(pc & (NMI_MASK | HARDIRQ_MASK));
  82. level += !!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET));
  83. return level;
  84. }
  85. /*
  86. * These macro definitions avoid redundant invocations of preempt_count()
  87. * because such invocations would result in redundant loads given that
  88. * preempt_count() is commonly implemented with READ_ONCE().
  89. */
  90. #define nmi_count() (preempt_count() & NMI_MASK)
  91. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  92. #ifdef CONFIG_PREEMPT_RT
  93. # define softirq_count() (current->softirq_disable_cnt & SOFTIRQ_MASK)
  94. # define irq_count() ((preempt_count() & (NMI_MASK | HARDIRQ_MASK)) | softirq_count())
  95. #else
  96. # define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  97. # define irq_count() (preempt_count() & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_MASK))
  98. #endif
  99. /*
  100. * Macros to retrieve the current execution context:
  101. *
  102. * in_nmi() - We're in NMI context
  103. * in_hardirq() - We're in hard IRQ context
  104. * in_serving_softirq() - We're in softirq context
  105. * in_task() - We're in task context
  106. */
  107. #define in_nmi() (nmi_count())
  108. #define in_hardirq() (hardirq_count())
  109. #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
  110. #ifdef CONFIG_PREEMPT_RT
  111. # define in_task() (!((preempt_count() & (NMI_MASK | HARDIRQ_MASK)) | in_serving_softirq()))
  112. #else
  113. # define in_task() (!(preempt_count() & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
  114. #endif
  115. /*
  116. * The following macros are deprecated and should not be used in new code:
  117. * in_irq() - Obsolete version of in_hardirq()
  118. * in_softirq() - We have BH disabled, or are processing softirqs
  119. * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled
  120. */
  121. #define in_irq() (hardirq_count())
  122. #define in_softirq() (softirq_count())
  123. #define in_interrupt() (irq_count())
  124. /*
  125. * The preempt_count offset after preempt_disable();
  126. */
  127. #if defined(CONFIG_PREEMPT_COUNT)
  128. # define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
  129. #else
  130. # define PREEMPT_DISABLE_OFFSET 0
  131. #endif
  132. /*
  133. * The preempt_count offset after spin_lock()
  134. */
  135. #if !defined(CONFIG_PREEMPT_RT)
  136. #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
  137. #else
  138. /* Locks on RT do not disable preemption */
  139. #define PREEMPT_LOCK_OFFSET 0
  140. #endif
  141. /*
  142. * The preempt_count offset needed for things like:
  143. *
  144. * spin_lock_bh()
  145. *
  146. * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
  147. * softirqs, such that unlock sequences of:
  148. *
  149. * spin_unlock();
  150. * local_bh_enable();
  151. *
  152. * Work as expected.
  153. */
  154. #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
  155. /*
  156. * Are we running in atomic context? WARNING: this macro cannot
  157. * always detect atomic context; in particular, it cannot know about
  158. * held spinlocks in non-preemptible kernels. Thus it should not be
  159. * used in the general case to determine whether sleeping is possible.
  160. * Do not use in_atomic() in driver code.
  161. */
  162. #define in_atomic() (preempt_count() != 0)
  163. /*
  164. * Check whether we were atomic before we did preempt_disable():
  165. * (used by the scheduler)
  166. */
  167. #define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
  168. #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE)
  169. extern void preempt_count_add(int val);
  170. extern void preempt_count_sub(int val);
  171. #define preempt_count_dec_and_test() \
  172. ({ preempt_count_sub(1); should_resched(0); })
  173. #else
  174. #define preempt_count_add(val) __preempt_count_add(val)
  175. #define preempt_count_sub(val) __preempt_count_sub(val)
  176. #define preempt_count_dec_and_test() __preempt_count_dec_and_test()
  177. #endif
  178. #define __preempt_count_inc() __preempt_count_add(1)
  179. #define __preempt_count_dec() __preempt_count_sub(1)
  180. #define preempt_count_inc() preempt_count_add(1)
  181. #define preempt_count_dec() preempt_count_sub(1)
  182. #ifdef CONFIG_PREEMPT_COUNT
  183. #define preempt_disable() \
  184. do { \
  185. preempt_count_inc(); \
  186. barrier(); \
  187. } while (0)
  188. #define sched_preempt_enable_no_resched() \
  189. do { \
  190. barrier(); \
  191. preempt_count_dec(); \
  192. } while (0)
  193. #define preempt_enable_no_resched() sched_preempt_enable_no_resched()
  194. #define preemptible() (preempt_count() == 0 && !irqs_disabled())
  195. #ifdef CONFIG_PREEMPTION
  196. #define preempt_enable() \
  197. do { \
  198. barrier(); \
  199. if (unlikely(preempt_count_dec_and_test())) \
  200. __preempt_schedule(); \
  201. } while (0)
  202. #define preempt_enable_notrace() \
  203. do { \
  204. barrier(); \
  205. if (unlikely(__preempt_count_dec_and_test())) \
  206. __preempt_schedule_notrace(); \
  207. } while (0)
  208. #define preempt_check_resched() \
  209. do { \
  210. if (should_resched(0)) \
  211. __preempt_schedule(); \
  212. } while (0)
  213. #else /* !CONFIG_PREEMPTION */
  214. #define preempt_enable() \
  215. do { \
  216. barrier(); \
  217. preempt_count_dec(); \
  218. } while (0)
  219. #define preempt_enable_notrace() \
  220. do { \
  221. barrier(); \
  222. __preempt_count_dec(); \
  223. } while (0)
  224. #define preempt_check_resched() do { } while (0)
  225. #endif /* CONFIG_PREEMPTION */
  226. #define preempt_disable_notrace() \
  227. do { \
  228. __preempt_count_inc(); \
  229. barrier(); \
  230. } while (0)
  231. #define preempt_enable_no_resched_notrace() \
  232. do { \
  233. barrier(); \
  234. __preempt_count_dec(); \
  235. } while (0)
  236. #else /* !CONFIG_PREEMPT_COUNT */
  237. /*
  238. * Even if we don't have any preemption, we need preempt disable/enable
  239. * to be barriers, so that we don't have things like get_user/put_user
  240. * that can cause faults and scheduling migrate into our preempt-protected
  241. * region.
  242. */
  243. #define preempt_disable() barrier()
  244. #define sched_preempt_enable_no_resched() barrier()
  245. #define preempt_enable_no_resched() barrier()
  246. #define preempt_enable() barrier()
  247. #define preempt_check_resched() do { } while (0)
  248. #define preempt_disable_notrace() barrier()
  249. #define preempt_enable_no_resched_notrace() barrier()
  250. #define preempt_enable_notrace() barrier()
  251. #define preemptible() 0
  252. #endif /* CONFIG_PREEMPT_COUNT */
  253. #ifdef MODULE
  254. /*
  255. * Modules have no business playing preemption tricks.
  256. */
  257. #undef sched_preempt_enable_no_resched
  258. #undef preempt_enable_no_resched
  259. #undef preempt_enable_no_resched_notrace
  260. #undef preempt_check_resched
  261. #endif
  262. #define preempt_set_need_resched() \
  263. do { \
  264. set_preempt_need_resched(); \
  265. } while (0)
  266. #define preempt_fold_need_resched() \
  267. do { \
  268. if (tif_need_resched()) \
  269. set_preempt_need_resched(); \
  270. } while (0)
  271. #ifdef CONFIG_PREEMPT_NOTIFIERS
  272. struct preempt_notifier;
  273. /**
  274. * preempt_ops - notifiers called when a task is preempted and rescheduled
  275. * @sched_in: we're about to be rescheduled:
  276. * notifier: struct preempt_notifier for the task being scheduled
  277. * cpu: cpu we're scheduled on
  278. * @sched_out: we've just been preempted
  279. * notifier: struct preempt_notifier for the task being preempted
  280. * next: the task that's kicking us out
  281. *
  282. * Please note that sched_in and out are called under different
  283. * contexts. sched_out is called with rq lock held and irq disabled
  284. * while sched_in is called without rq lock and irq enabled. This
  285. * difference is intentional and depended upon by its users.
  286. */
  287. struct preempt_ops {
  288. void (*sched_in)(struct preempt_notifier *notifier, int cpu);
  289. void (*sched_out)(struct preempt_notifier *notifier,
  290. struct task_struct *next);
  291. };
  292. /**
  293. * preempt_notifier - key for installing preemption notifiers
  294. * @link: internal use
  295. * @ops: defines the notifier functions to be called
  296. *
  297. * Usually used in conjunction with container_of().
  298. */
  299. struct preempt_notifier {
  300. struct hlist_node link;
  301. struct preempt_ops *ops;
  302. };
  303. void preempt_notifier_inc(void);
  304. void preempt_notifier_dec(void);
  305. void preempt_notifier_register(struct preempt_notifier *notifier);
  306. void preempt_notifier_unregister(struct preempt_notifier *notifier);
  307. static inline void preempt_notifier_init(struct preempt_notifier *notifier,
  308. struct preempt_ops *ops)
  309. {
  310. INIT_HLIST_NODE(&notifier->link);
  311. notifier->ops = ops;
  312. }
  313. #endif
  314. #ifdef CONFIG_SMP
  315. /*
  316. * Migrate-Disable and why it is undesired.
  317. *
  318. * When a preempted task becomes elegible to run under the ideal model (IOW it
  319. * becomes one of the M highest priority tasks), it might still have to wait
  320. * for the preemptee's migrate_disable() section to complete. Thereby suffering
  321. * a reduction in bandwidth in the exact duration of the migrate_disable()
  322. * section.
  323. *
  324. * Per this argument, the change from preempt_disable() to migrate_disable()
  325. * gets us:
  326. *
  327. * - a higher priority tasks gains reduced wake-up latency; with preempt_disable()
  328. * it would have had to wait for the lower priority task.
  329. *
  330. * - a lower priority tasks; which under preempt_disable() could've instantly
  331. * migrated away when another CPU becomes available, is now constrained
  332. * by the ability to push the higher priority task away, which might itself be
  333. * in a migrate_disable() section, reducing it's available bandwidth.
  334. *
  335. * IOW it trades latency / moves the interference term, but it stays in the
  336. * system, and as long as it remains unbounded, the system is not fully
  337. * deterministic.
  338. *
  339. *
  340. * The reason we have it anyway.
  341. *
  342. * PREEMPT_RT breaks a number of assumptions traditionally held. By forcing a
  343. * number of primitives into becoming preemptible, they would also allow
  344. * migration. This turns out to break a bunch of per-cpu usage. To this end,
  345. * all these primitives employ migirate_disable() to restore this implicit
  346. * assumption.
  347. *
  348. * This is a 'temporary' work-around at best. The correct solution is getting
  349. * rid of the above assumptions and reworking the code to employ explicit
  350. * per-cpu locking or short preempt-disable regions.
  351. *
  352. * The end goal must be to get rid of migrate_disable(), alternatively we need
  353. * a schedulability theory that does not depend on abritrary migration.
  354. *
  355. *
  356. * Notes on the implementation.
  357. *
  358. * The implementation is particularly tricky since existing code patterns
  359. * dictate neither migrate_disable() nor migrate_enable() is allowed to block.
  360. * This means that it cannot use cpus_read_lock() to serialize against hotplug,
  361. * nor can it easily migrate itself into a pending affinity mask change on
  362. * migrate_enable().
  363. *
  364. *
  365. * Note: even non-work-conserving schedulers like semi-partitioned depends on
  366. * migration, so migrate_disable() is not only a problem for
  367. * work-conserving schedulers.
  368. *
  369. */
  370. extern void migrate_disable(void);
  371. extern void migrate_enable(void);
  372. #else
  373. static inline void migrate_disable(void) { }
  374. static inline void migrate_enable(void) { }
  375. #endif /* CONFIG_SMP */
  376. /**
  377. * preempt_disable_nested - Disable preemption inside a normally preempt disabled section
  378. *
  379. * Use for code which requires preemption protection inside a critical
  380. * section which has preemption disabled implicitly on non-PREEMPT_RT
  381. * enabled kernels, by e.g.:
  382. * - holding a spinlock/rwlock
  383. * - soft interrupt context
  384. * - regular interrupt handlers
  385. *
  386. * On PREEMPT_RT enabled kernels spinlock/rwlock held sections, soft
  387. * interrupt context and regular interrupt handlers are preemptible and
  388. * only prevent migration. preempt_disable_nested() ensures that preemption
  389. * is disabled for cases which require CPU local serialization even on
  390. * PREEMPT_RT. For non-PREEMPT_RT kernels this is a NOP.
  391. *
  392. * The use cases are code sequences which are not serialized by a
  393. * particular lock instance, e.g.:
  394. * - seqcount write side critical sections where the seqcount is not
  395. * associated to a particular lock and therefore the automatic
  396. * protection mechanism does not work. This prevents a live lock
  397. * against a preempting high priority reader.
  398. * - RMW per CPU variable updates like vmstat.
  399. */
  400. /* Macro to avoid header recursion hell vs. lockdep */
  401. #define preempt_disable_nested() \
  402. do { \
  403. if (IS_ENABLED(CONFIG_PREEMPT_RT)) \
  404. preempt_disable(); \
  405. else \
  406. lockdep_assert_preemption_disabled(); \
  407. } while (0)
  408. /**
  409. * preempt_enable_nested - Undo the effect of preempt_disable_nested()
  410. */
  411. static __always_inline void preempt_enable_nested(void)
  412. {
  413. if (IS_ENABLED(CONFIG_PREEMPT_RT))
  414. preempt_enable();
  415. }
  416. #endif /* __LINUX_PREEMPT_H */