blob: 7d9c1c0e149c0d429d5ecbf9e015848fa1fdb8a4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __LINUX_PREEMPT_H
3#define __LINUX_PREEMPT_H
4
5/*
6 * include/linux/preempt.h - macros for accessing and manipulating
7 * preempt_count (used for kernel preemption, interrupt count, etc.)
8 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/linkage.h>
Avi Kivitye107be32007-07-26 13:40:43 +020011#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Peter Zijlstraf27dde82013-08-14 14:55:31 +020013/*
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020014 * We put the hardirq and softirq counter into the preemption
15 * counter. The bitmask has the following meaning:
16 *
17 * - bits 0-7 are the preemption count (max preemption depth: 256)
18 * - bits 8-15 are the softirq count (max # of softirqs: 256)
19 *
20 * The hardirq count could in theory be the same as the number of
21 * interrupts in the system, but we run all interrupt handlers with
22 * interrupts disabled, so we cannot have nesting interrupts. Though
23 * there are a few palaeontologic drivers which reenable interrupts in
24 * the handler, so we need more than one bit here.
25 *
Frederic Weisbecker2e10e712015-05-12 16:41:47 +020026 * PREEMPT_MASK: 0x000000ff
27 * SOFTIRQ_MASK: 0x0000ff00
28 * HARDIRQ_MASK: 0x000f0000
Peter Zijlstra69ea03b2020-02-19 09:46:47 +010029 * NMI_MASK: 0x00f00000
Frederic Weisbecker2e10e712015-05-12 16:41:47 +020030 * PREEMPT_NEED_RESCHED: 0x80000000
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020031 */
32#define PREEMPT_BITS 8
33#define SOFTIRQ_BITS 8
34#define HARDIRQ_BITS 4
Peter Zijlstra69ea03b2020-02-19 09:46:47 +010035#define NMI_BITS 4
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020036
37#define PREEMPT_SHIFT 0
38#define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
39#define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
40#define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
41
42#define __IRQ_MASK(x) ((1UL << (x))-1)
43
44#define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
45#define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
46#define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
47#define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
48
49#define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
50#define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
51#define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
52#define NMI_OFFSET (1UL << NMI_SHIFT)
53
54#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
55
Ingo Molnard04b0ad2017-02-03 22:07:57 +010056#define PREEMPT_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
57
58/*
59 * Disable preemption until the scheduler is running -- use an unconditional
60 * value so that it also works on !PREEMPT_COUNT kernels.
61 *
62 * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count().
63 */
64#define INIT_PREEMPT_COUNT PREEMPT_OFFSET
65
66/*
67 * Initial preempt_count value; reflects the preempt_count schedule invariant
68 * which states that during context switches:
69 *
70 * preempt_count() == 2*PREEMPT_DISABLE_OFFSET
71 *
72 * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels.
73 * Note: See finish_task_switch().
74 */
75#define FORK_PREEMPT_COUNT (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED)
76
Frederic Weisbecker2e10e712015-05-12 16:41:47 +020077/* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
78#include <asm/preempt.h>
79
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020080#define hardirq_count() (preempt_count() & HARDIRQ_MASK)
81#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
82#define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
83 | NMI_MASK))
84
85/*
86 * Are we doing bottom half or hardware interrupt processing?
Peter Zijlstra7c478892016-11-22 10:57:15 +010087 *
88 * in_irq() - We're in (hard) IRQ context
89 * in_softirq() - We have BH disabled, or are processing softirqs
90 * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled
91 * in_serving_softirq() - We're in softirq context
92 * in_nmi() - We're in NMI context
93 * in_task() - We're in task context
94 *
95 * Note: due to the BH disabled confusion: in_softirq(),in_interrupt() really
96 * should not be used in new code.
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020097 */
98#define in_irq() (hardirq_count())
99#define in_softirq() (softirq_count())
100#define in_interrupt() (irq_count())
101#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
Peter Zijlstra7c478892016-11-22 10:57:15 +0100102#define in_nmi() (preempt_count() & NMI_MASK)
103#define in_task() (!(preempt_count() & \
104 (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200105
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300106/*
107 * The preempt_count offset after preempt_disable();
108 */
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200109#if defined(CONFIG_PREEMPT_COUNT)
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300110# define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200111#else
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300112# define PREEMPT_DISABLE_OFFSET 0
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200113#endif
114
115/*
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300116 * The preempt_count offset after spin_lock()
117 */
118#define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
119
120/*
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200121 * The preempt_count offset needed for things like:
122 *
123 * spin_lock_bh()
124 *
125 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
126 * softirqs, such that unlock sequences of:
127 *
128 * spin_unlock();
129 * local_bh_enable();
130 *
131 * Work as expected.
132 */
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300133#define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200134
135/*
136 * Are we running in atomic context? WARNING: this macro cannot
137 * always detect atomic context; in particular, it cannot know about
138 * held spinlocks in non-preemptible kernels. Thus it should not be
139 * used in the general case to determine whether sleeping is possible.
140 * Do not use in_atomic() in driver code.
141 */
Frederic Weisbecker3e51f3c2015-05-12 16:41:51 +0200142#define in_atomic() (preempt_count() != 0)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200143
144/*
145 * Check whether we were atomic before we did preempt_disable():
Frederic Weisbeckere017cf22015-05-12 16:41:50 +0200146 * (used by the scheduler)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200147 */
Peter Zijlstrada7142e2015-09-28 18:11:45 +0200148#define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200149
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -0700150#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE)
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200151extern void preempt_count_add(int val);
152extern void preempt_count_sub(int val);
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300153#define preempt_count_dec_and_test() \
154 ({ preempt_count_sub(1); should_resched(0); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#else
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200156#define preempt_count_add(val) __preempt_count_add(val)
157#define preempt_count_sub(val) __preempt_count_sub(val)
158#define preempt_count_dec_and_test() __preempt_count_dec_and_test()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#endif
160
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200161#define __preempt_count_inc() __preempt_count_add(1)
162#define __preempt_count_dec() __preempt_count_sub(1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200164#define preempt_count_inc() preempt_count_add(1)
165#define preempt_count_dec() preempt_count_sub(1)
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200166
167#ifdef CONFIG_PREEMPT_COUNT
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#define preempt_disable() \
170do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200171 preempt_count_inc(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 barrier(); \
173} while (0)
174
Thomas Gleixnerba74c142011-03-21 13:32:17 +0100175#define sched_preempt_enable_no_resched() \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176do { \
177 barrier(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200178 preempt_count_dec(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179} while (0)
180
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200181#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
Thomas Gleixnerba74c142011-03-21 13:32:17 +0100182
Frederic Weisbecker2e10e712015-05-12 16:41:47 +0200183#define preemptible() (preempt_count() == 0 && !irqs_disabled())
184
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +0200185#ifdef CONFIG_PREEMPTION
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define preempt_enable() \
187do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200188 barrier(); \
189 if (unlikely(preempt_count_dec_and_test())) \
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200190 __preempt_schedule(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191} while (0)
192
Frederic Weisbecker9a92e3d2015-06-04 17:39:09 +0200193#define preempt_enable_notrace() \
194do { \
195 barrier(); \
196 if (unlikely(__preempt_count_dec_and_test())) \
197 __preempt_schedule_notrace(); \
198} while (0)
199
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200200#define preempt_check_resched() \
201do { \
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300202 if (should_resched(0)) \
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200203 __preempt_schedule(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200204} while (0)
205
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +0200206#else /* !CONFIG_PREEMPTION */
Peter Zijlstra62b94a02013-11-20 16:52:19 +0100207#define preempt_enable() \
208do { \
209 barrier(); \
210 preempt_count_dec(); \
211} while (0)
Frederic Weisbecker9a92e3d2015-06-04 17:39:09 +0200212
213#define preempt_enable_notrace() \
214do { \
215 barrier(); \
216 __preempt_count_dec(); \
217} while (0)
218
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200219#define preempt_check_resched() do { } while (0)
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +0200220#endif /* CONFIG_PREEMPTION */
Steven Rostedt50282522008-05-12 21:20:41 +0200221
222#define preempt_disable_notrace() \
223do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200224 __preempt_count_inc(); \
Steven Rostedt50282522008-05-12 21:20:41 +0200225 barrier(); \
226} while (0)
227
228#define preempt_enable_no_resched_notrace() \
229do { \
230 barrier(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200231 __preempt_count_dec(); \
Steven Rostedt50282522008-05-12 21:20:41 +0200232} while (0)
233
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200234#else /* !CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvalds386afc92013-04-09 10:48:33 -0700236/*
237 * Even if we don't have any preemption, we need preempt disable/enable
238 * to be barriers, so that we don't have things like get_user/put_user
239 * that can cause faults and scheduling migrate into our preempt-protected
240 * region.
241 */
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200242#define preempt_disable() barrier()
Linus Torvalds386afc92013-04-09 10:48:33 -0700243#define sched_preempt_enable_no_resched() barrier()
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200244#define preempt_enable_no_resched() barrier()
245#define preempt_enable() barrier()
246#define preempt_check_resched() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds386afc92013-04-09 10:48:33 -0700248#define preempt_disable_notrace() barrier()
249#define preempt_enable_no_resched_notrace() barrier()
250#define preempt_enable_notrace() barrier()
Frederic Weisbecker2e10e712015-05-12 16:41:47 +0200251#define preemptible() 0
Steven Rostedt50282522008-05-12 21:20:41 +0200252
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200253#endif /* CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Peter Zijlstra62b94a02013-11-20 16:52:19 +0100255#ifdef MODULE
256/*
257 * Modules have no business playing preemption tricks.
258 */
259#undef sched_preempt_enable_no_resched
260#undef preempt_enable_no_resched
261#undef preempt_enable_no_resched_notrace
262#undef preempt_check_resched
263#endif
264
Peter Zijlstra8cb75e02013-11-20 12:22:37 +0100265#define preempt_set_need_resched() \
266do { \
267 set_preempt_need_resched(); \
268} while (0)
269#define preempt_fold_need_resched() \
270do { \
271 if (tif_need_resched()) \
272 set_preempt_need_resched(); \
273} while (0)
Peter Zijlstra8cb75e02013-11-20 12:22:37 +0100274
Avi Kivitye107be32007-07-26 13:40:43 +0200275#ifdef CONFIG_PREEMPT_NOTIFIERS
276
277struct preempt_notifier;
278
279/**
280 * preempt_ops - notifiers called when a task is preempted and rescheduled
281 * @sched_in: we're about to be rescheduled:
282 * notifier: struct preempt_notifier for the task being scheduled
283 * cpu: cpu we're scheduled on
284 * @sched_out: we've just been preempted
285 * notifier: struct preempt_notifier for the task being preempted
286 * next: the task that's kicking us out
Tejun Heo8592e642009-12-02 12:56:46 +0900287 *
288 * Please note that sched_in and out are called under different
289 * contexts. sched_out is called with rq lock held and irq disabled
290 * while sched_in is called without rq lock and irq enabled. This
291 * difference is intentional and depended upon by its users.
Avi Kivitye107be32007-07-26 13:40:43 +0200292 */
293struct preempt_ops {
294 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
295 void (*sched_out)(struct preempt_notifier *notifier,
296 struct task_struct *next);
297};
298
299/**
300 * preempt_notifier - key for installing preemption notifiers
301 * @link: internal use
302 * @ops: defines the notifier functions to be called
303 *
304 * Usually used in conjunction with container_of().
305 */
306struct preempt_notifier {
307 struct hlist_node link;
308 struct preempt_ops *ops;
309};
310
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200311void preempt_notifier_inc(void);
312void preempt_notifier_dec(void);
Avi Kivitye107be32007-07-26 13:40:43 +0200313void preempt_notifier_register(struct preempt_notifier *notifier);
314void preempt_notifier_unregister(struct preempt_notifier *notifier);
315
316static inline void preempt_notifier_init(struct preempt_notifier *notifier,
317 struct preempt_ops *ops)
318{
319 INIT_HLIST_NODE(&notifier->link);
320 notifier->ops = ops;
321}
322
323#endif
324
Thomas Gleixner66630052020-02-08 20:48:29 +0100325/**
326 * migrate_disable - Prevent migration of the current task
327 *
328 * Maps to preempt_disable() which also disables preemption. Use
329 * migrate_disable() to annotate that the intent is to prevent migration,
330 * but not necessarily preemption.
331 *
332 * Can be invoked nested like preempt_disable() and needs the corresponding
333 * number of migrate_enable() invocations.
334 */
335static __always_inline void migrate_disable(void)
336{
337 preempt_disable();
338}
339
340/**
341 * migrate_enable - Allow migration of the current task
342 *
343 * Counterpart to migrate_disable().
344 *
345 * As migrate_disable() can be invoked nested, only the outermost invocation
346 * reenables migration.
347 *
348 * Currently mapped to preempt_enable().
349 */
350static __always_inline void migrate_enable(void)
351{
352 preempt_enable();
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355#endif /* __LINUX_PREEMPT_H */