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