]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/preempt.h
Merge tag 'printk-for-5.13-fixup' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-jammy-kernel.git] / include / linux / preempt.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#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
1da177e4 10#include <linux/linkage.h>
e107be36 11#include <linux/list.h>
1da177e4 12
92cf2118
FW
13/*
14 * 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 *
2e10e71c
FW
26 * PREEMPT_MASK: 0x000000ff
27 * SOFTIRQ_MASK: 0x0000ff00
28 * HARDIRQ_MASK: 0x000f0000
69ea03b5 29 * NMI_MASK: 0x00f00000
2e10e71c 30 * PREEMPT_NEED_RESCHED: 0x80000000
92cf2118
FW
31 */
32#define PREEMPT_BITS 8
33#define SOFTIRQ_BITS 8
34#define HARDIRQ_BITS 4
69ea03b5 35#define NMI_BITS 4
92cf2118
FW
36
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
d04b0ad3
IM
56#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
2e10e71c
FW
77/* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
78#include <asm/preempt.h>
79
15115830 80#define nmi_count() (preempt_count() & NMI_MASK)
92cf2118 81#define hardirq_count() (preempt_count() & HARDIRQ_MASK)
728b478d
TG
82#ifdef CONFIG_PREEMPT_RT
83# define softirq_count() (current->softirq_disable_cnt & SOFTIRQ_MASK)
84#else
85# define softirq_count() (preempt_count() & SOFTIRQ_MASK)
86#endif
15115830 87#define irq_count() (nmi_count() | hardirq_count() | softirq_count())
92cf2118
FW
88
89/*
15115830 90 * Macros to retrieve the current execution context:
7c478895 91 *
15115830
TG
92 * in_nmi() - We're in NMI context
93 * in_hardirq() - We're in hard IRQ context
94 * in_serving_softirq() - We're in softirq context
95 * in_task() - We're in task context
96 */
97#define in_nmi() (nmi_count())
98#define in_hardirq() (hardirq_count())
99#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
100#define in_task() (!(in_nmi() | in_hardirq() | in_serving_softirq()))
101
102/*
103 * The following macros are deprecated and should not be used in new code:
104 * in_irq() - Obsolete version of in_hardirq()
7c478895
PZ
105 * in_softirq() - We have BH disabled, or are processing softirqs
106 * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled
92cf2118
FW
107 */
108#define in_irq() (hardirq_count())
109#define in_softirq() (softirq_count())
110#define in_interrupt() (irq_count())
92cf2118 111
fe32d3cd
KK
112/*
113 * The preempt_count offset after preempt_disable();
114 */
92cf2118 115#if defined(CONFIG_PREEMPT_COUNT)
fe32d3cd 116# define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
92cf2118 117#else
fe32d3cd 118# define PREEMPT_DISABLE_OFFSET 0
92cf2118
FW
119#endif
120
fe32d3cd
KK
121/*
122 * The preempt_count offset after spin_lock()
123 */
124#define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
125
92cf2118
FW
126/*
127 * The preempt_count offset needed for things like:
128 *
129 * spin_lock_bh()
130 *
131 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
132 * softirqs, such that unlock sequences of:
133 *
134 * spin_unlock();
135 * local_bh_enable();
136 *
137 * Work as expected.
138 */
fe32d3cd 139#define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
92cf2118
FW
140
141/*
142 * Are we running in atomic context? WARNING: this macro cannot
143 * always detect atomic context; in particular, it cannot know about
144 * held spinlocks in non-preemptible kernels. Thus it should not be
145 * used in the general case to determine whether sleeping is possible.
146 * Do not use in_atomic() in driver code.
147 */
3e51f3c4 148#define in_atomic() (preempt_count() != 0)
92cf2118
FW
149
150/*
151 * Check whether we were atomic before we did preempt_disable():
e017cf21 152 * (used by the scheduler)
92cf2118 153 */
da7142e2 154#define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
92cf2118 155
c3bc8fd6 156#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE)
bdb43806
PZ
157extern void preempt_count_add(int val);
158extern void preempt_count_sub(int val);
fe32d3cd
KK
159#define preempt_count_dec_and_test() \
160 ({ preempt_count_sub(1); should_resched(0); })
1da177e4 161#else
bdb43806
PZ
162#define preempt_count_add(val) __preempt_count_add(val)
163#define preempt_count_sub(val) __preempt_count_sub(val)
164#define preempt_count_dec_and_test() __preempt_count_dec_and_test()
1da177e4
LT
165#endif
166
bdb43806
PZ
167#define __preempt_count_inc() __preempt_count_add(1)
168#define __preempt_count_dec() __preempt_count_sub(1)
bdd4e85d 169
bdb43806
PZ
170#define preempt_count_inc() preempt_count_add(1)
171#define preempt_count_dec() preempt_count_sub(1)
bdd4e85d
FW
172
173#ifdef CONFIG_PREEMPT_COUNT
174
1da177e4
LT
175#define preempt_disable() \
176do { \
bdb43806 177 preempt_count_inc(); \
1da177e4
LT
178 barrier(); \
179} while (0)
180
ba74c144 181#define sched_preempt_enable_no_resched() \
1da177e4
LT
182do { \
183 barrier(); \
bdb43806 184 preempt_count_dec(); \
1da177e4
LT
185} while (0)
186
bdb43806 187#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
ba74c144 188
2e10e71c
FW
189#define preemptible() (preempt_count() == 0 && !irqs_disabled())
190
c1a280b6 191#ifdef CONFIG_PREEMPTION
1da177e4
LT
192#define preempt_enable() \
193do { \
bdb43806
PZ
194 barrier(); \
195 if (unlikely(preempt_count_dec_and_test())) \
1a338ac3 196 __preempt_schedule(); \
1da177e4
LT
197} while (0)
198
9a92e3dc
FW
199#define preempt_enable_notrace() \
200do { \
201 barrier(); \
202 if (unlikely(__preempt_count_dec_and_test())) \
203 __preempt_schedule_notrace(); \
204} while (0)
205
bdb43806
PZ
206#define preempt_check_resched() \
207do { \
fe32d3cd 208 if (should_resched(0)) \
1a338ac3 209 __preempt_schedule(); \
bdb43806
PZ
210} while (0)
211
c1a280b6 212#else /* !CONFIG_PREEMPTION */
62b94a08
PZ
213#define preempt_enable() \
214do { \
215 barrier(); \
216 preempt_count_dec(); \
217} while (0)
50282528 218
9a92e3dc 219#define preempt_enable_notrace() \
50282528
SR
220do { \
221 barrier(); \
bdb43806 222 __preempt_count_dec(); \
50282528
SR
223} while (0)
224
9a92e3dc 225#define preempt_check_resched() do { } while (0)
c1a280b6 226#endif /* CONFIG_PREEMPTION */
bdb43806 227
9a92e3dc 228#define preempt_disable_notrace() \
50282528 229do { \
9a92e3dc 230 __preempt_count_inc(); \
bdb43806 231 barrier(); \
50282528 232} while (0)
9a92e3dc
FW
233
234#define preempt_enable_no_resched_notrace() \
62b94a08
PZ
235do { \
236 barrier(); \
237 __preempt_count_dec(); \
238} while (0)
50282528 239
bdd4e85d 240#else /* !CONFIG_PREEMPT_COUNT */
1da177e4 241
386afc91
LT
242/*
243 * Even if we don't have any preemption, we need preempt disable/enable
244 * to be barriers, so that we don't have things like get_user/put_user
245 * that can cause faults and scheduling migrate into our preempt-protected
246 * region.
247 */
bdb43806 248#define preempt_disable() barrier()
386afc91 249#define sched_preempt_enable_no_resched() barrier()
bdb43806
PZ
250#define preempt_enable_no_resched() barrier()
251#define preempt_enable() barrier()
252#define preempt_check_resched() do { } while (0)
386afc91
LT
253
254#define preempt_disable_notrace() barrier()
255#define preempt_enable_no_resched_notrace() barrier()
256#define preempt_enable_notrace() barrier()
2e10e71c 257#define preemptible() 0
50282528 258
bdd4e85d 259#endif /* CONFIG_PREEMPT_COUNT */
1da177e4 260
62b94a08
PZ
261#ifdef MODULE
262/*
263 * Modules have no business playing preemption tricks.
264 */
265#undef sched_preempt_enable_no_resched
266#undef preempt_enable_no_resched
267#undef preempt_enable_no_resched_notrace
268#undef preempt_check_resched
269#endif
270
8cb75e0c
PZ
271#define preempt_set_need_resched() \
272do { \
273 set_preempt_need_resched(); \
274} while (0)
275#define preempt_fold_need_resched() \
276do { \
277 if (tif_need_resched()) \
278 set_preempt_need_resched(); \
279} while (0)
8cb75e0c 280
e107be36
AK
281#ifdef CONFIG_PREEMPT_NOTIFIERS
282
283struct preempt_notifier;
284
285/**
286 * preempt_ops - notifiers called when a task is preempted and rescheduled
287 * @sched_in: we're about to be rescheduled:
288 * notifier: struct preempt_notifier for the task being scheduled
289 * cpu: cpu we're scheduled on
290 * @sched_out: we've just been preempted
291 * notifier: struct preempt_notifier for the task being preempted
292 * next: the task that's kicking us out
8592e648
TH
293 *
294 * Please note that sched_in and out are called under different
295 * contexts. sched_out is called with rq lock held and irq disabled
296 * while sched_in is called without rq lock and irq enabled. This
297 * difference is intentional and depended upon by its users.
e107be36
AK
298 */
299struct preempt_ops {
300 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
301 void (*sched_out)(struct preempt_notifier *notifier,
302 struct task_struct *next);
303};
304
305/**
306 * preempt_notifier - key for installing preemption notifiers
307 * @link: internal use
308 * @ops: defines the notifier functions to be called
309 *
310 * Usually used in conjunction with container_of().
311 */
312struct preempt_notifier {
313 struct hlist_node link;
314 struct preempt_ops *ops;
315};
316
2ecd9d29
PZ
317void preempt_notifier_inc(void);
318void preempt_notifier_dec(void);
e107be36
AK
319void preempt_notifier_register(struct preempt_notifier *notifier);
320void preempt_notifier_unregister(struct preempt_notifier *notifier);
321
322static inline void preempt_notifier_init(struct preempt_notifier *notifier,
323 struct preempt_ops *ops)
324{
325 INIT_HLIST_NODE(&notifier->link);
326 notifier->ops = ops;
327}
328
329#endif
330
74d862b6 331#ifdef CONFIG_SMP
af449901
PZ
332
333/*
a7c81556 334 * Migrate-Disable and why it is undesired.
af449901 335 *
a7c81556
PZ
336 * When a preempted task becomes elegible to run under the ideal model (IOW it
337 * becomes one of the M highest priority tasks), it might still have to wait
338 * for the preemptee's migrate_disable() section to complete. Thereby suffering
339 * a reduction in bandwidth in the exact duration of the migrate_disable()
340 * section.
af449901 341 *
a7c81556
PZ
342 * Per this argument, the change from preempt_disable() to migrate_disable()
343 * gets us:
af449901 344 *
a7c81556
PZ
345 * - a higher priority tasks gains reduced wake-up latency; with preempt_disable()
346 * it would have had to wait for the lower priority task.
347 *
348 * - a lower priority tasks; which under preempt_disable() could've instantly
349 * migrated away when another CPU becomes available, is now constrained
350 * by the ability to push the higher priority task away, which might itself be
351 * in a migrate_disable() section, reducing it's available bandwidth.
352 *
353 * IOW it trades latency / moves the interference term, but it stays in the
354 * system, and as long as it remains unbounded, the system is not fully
355 * deterministic.
af449901
PZ
356 *
357 *
358 * The reason we have it anyway.
359 *
360 * PREEMPT_RT breaks a number of assumptions traditionally held. By forcing a
361 * number of primitives into becoming preemptible, they would also allow
362 * migration. This turns out to break a bunch of per-cpu usage. To this end,
363 * all these primitives employ migirate_disable() to restore this implicit
364 * assumption.
365 *
366 * This is a 'temporary' work-around at best. The correct solution is getting
367 * rid of the above assumptions and reworking the code to employ explicit
368 * per-cpu locking or short preempt-disable regions.
369 *
370 * The end goal must be to get rid of migrate_disable(), alternatively we need
371 * a schedulability theory that does not depend on abritrary migration.
372 *
373 *
374 * Notes on the implementation.
375 *
376 * The implementation is particularly tricky since existing code patterns
377 * dictate neither migrate_disable() nor migrate_enable() is allowed to block.
378 * This means that it cannot use cpus_read_lock() to serialize against hotplug,
379 * nor can it easily migrate itself into a pending affinity mask change on
380 * migrate_enable().
381 *
382 *
383 * Note: even non-work-conserving schedulers like semi-partitioned depends on
384 * migration, so migrate_disable() is not only a problem for
385 * work-conserving schedulers.
386 *
387 */
388extern void migrate_disable(void);
389extern void migrate_enable(void);
390
74d862b6 391#else
af449901
PZ
392
393static inline void migrate_disable(void) { }
394static inline void migrate_enable(void) { }
395
74d862b6 396#endif /* CONFIG_SMP */
af449901 397
1da177e4 398#endif /* __LINUX_PREEMPT_H */