mac80211: correct legacy rates check in ieee80211_calc_rx_airtime
[platform/kernel/linux-rpi.git] / include / linux / preempt.h
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 #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
87 #define irq_count()     (nmi_count() | hardirq_count() | softirq_count())
88
89 /*
90  * Macros to retrieve the current execution context:
91  *
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()
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
107  */
108 #define in_irq()                (hardirq_count())
109 #define in_softirq()            (softirq_count())
110 #define in_interrupt()          (irq_count())
111
112 /*
113  * The preempt_count offset after preempt_disable();
114  */
115 #if defined(CONFIG_PREEMPT_COUNT)
116 # define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
117 #else
118 # define PREEMPT_DISABLE_OFFSET 0
119 #endif
120
121 /*
122  * The preempt_count offset after spin_lock()
123  */
124 #if !defined(CONFIG_PREEMPT_RT)
125 #define PREEMPT_LOCK_OFFSET             PREEMPT_DISABLE_OFFSET
126 #else
127 /* Locks on RT do not disable preemption */
128 #define PREEMPT_LOCK_OFFSET             0
129 #endif
130
131 /*
132  * The preempt_count offset needed for things like:
133  *
134  *  spin_lock_bh()
135  *
136  * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
137  * softirqs, such that unlock sequences of:
138  *
139  *  spin_unlock();
140  *  local_bh_enable();
141  *
142  * Work as expected.
143  */
144 #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
145
146 /*
147  * Are we running in atomic context?  WARNING: this macro cannot
148  * always detect atomic context; in particular, it cannot know about
149  * held spinlocks in non-preemptible kernels.  Thus it should not be
150  * used in the general case to determine whether sleeping is possible.
151  * Do not use in_atomic() in driver code.
152  */
153 #define in_atomic()     (preempt_count() != 0)
154
155 /*
156  * Check whether we were atomic before we did preempt_disable():
157  * (used by the scheduler)
158  */
159 #define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
160
161 #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE)
162 extern void preempt_count_add(int val);
163 extern void preempt_count_sub(int val);
164 #define preempt_count_dec_and_test() \
165         ({ preempt_count_sub(1); should_resched(0); })
166 #else
167 #define preempt_count_add(val)  __preempt_count_add(val)
168 #define preempt_count_sub(val)  __preempt_count_sub(val)
169 #define preempt_count_dec_and_test() __preempt_count_dec_and_test()
170 #endif
171
172 #define __preempt_count_inc() __preempt_count_add(1)
173 #define __preempt_count_dec() __preempt_count_sub(1)
174
175 #define preempt_count_inc() preempt_count_add(1)
176 #define preempt_count_dec() preempt_count_sub(1)
177
178 #ifdef CONFIG_PREEMPT_LAZY
179 #define add_preempt_lazy_count(val)     do { preempt_lazy_count() += (val); } while (0)
180 #define sub_preempt_lazy_count(val)     do { preempt_lazy_count() -= (val); } while (0)
181 #define inc_preempt_lazy_count()        add_preempt_lazy_count(1)
182 #define dec_preempt_lazy_count()        sub_preempt_lazy_count(1)
183 #define preempt_lazy_count()            (current_thread_info()->preempt_lazy_count)
184 #else
185 #define add_preempt_lazy_count(val)     do { } while (0)
186 #define sub_preempt_lazy_count(val)     do { } while (0)
187 #define inc_preempt_lazy_count()        do { } while (0)
188 #define dec_preempt_lazy_count()        do { } while (0)
189 #define preempt_lazy_count()            (0)
190 #endif
191
192 #ifdef CONFIG_PREEMPT_COUNT
193
194 #define preempt_disable() \
195 do { \
196         preempt_count_inc(); \
197         barrier(); \
198 } while (0)
199
200 #define preempt_lazy_disable() \
201 do { \
202         inc_preempt_lazy_count(); \
203         barrier(); \
204 } while (0)
205
206 #define sched_preempt_enable_no_resched() \
207 do { \
208         barrier(); \
209         preempt_count_dec(); \
210 } while (0)
211
212 #ifndef CONFIG_PREEMPT_RT
213 # define preempt_enable_no_resched() sched_preempt_enable_no_resched()
214 # define preempt_check_resched_rt() barrier();
215 #else
216 # define preempt_enable_no_resched() preempt_enable()
217 # define preempt_check_resched_rt() preempt_check_resched()
218 #endif
219
220 #define preemptible()   (preempt_count() == 0 && !irqs_disabled())
221
222 #ifdef CONFIG_PREEMPTION
223 #define preempt_enable() \
224 do { \
225         barrier(); \
226         if (unlikely(preempt_count_dec_and_test())) \
227                 __preempt_schedule(); \
228 } while (0)
229
230 #define preempt_enable_notrace() \
231 do { \
232         barrier(); \
233         if (unlikely(__preempt_count_dec_and_test())) \
234                 __preempt_schedule_notrace(); \
235 } while (0)
236
237 #define preempt_check_resched() \
238 do { \
239         if (should_resched(0)) \
240                 __preempt_schedule(); \
241 } while (0)
242
243 /*
244  * open code preempt_check_resched() because it is not exported to modules and
245  * used by local_unlock() or bpf_enable_instrumentation().
246  */
247 #define preempt_lazy_enable() \
248 do { \
249         dec_preempt_lazy_count(); \
250         barrier(); \
251         if (should_resched(0)) \
252                 __preempt_schedule(); \
253 } while (0)
254
255 #else /* !CONFIG_PREEMPTION */
256 #define preempt_enable() \
257 do { \
258         barrier(); \
259         preempt_count_dec(); \
260 } while (0)
261
262 #define preempt_lazy_enable() \
263 do { \
264         dec_preempt_lazy_count(); \
265         barrier(); \
266 } while (0)
267
268 #define preempt_enable_notrace() \
269 do { \
270         barrier(); \
271         __preempt_count_dec(); \
272 } while (0)
273
274 #define preempt_check_resched() do { } while (0)
275 #endif /* CONFIG_PREEMPTION */
276
277 #define preempt_disable_notrace() \
278 do { \
279         __preempt_count_inc(); \
280         barrier(); \
281 } while (0)
282
283 #define preempt_enable_no_resched_notrace() \
284 do { \
285         barrier(); \
286         __preempt_count_dec(); \
287 } while (0)
288
289 #else /* !CONFIG_PREEMPT_COUNT */
290
291 /*
292  * Even if we don't have any preemption, we need preempt disable/enable
293  * to be barriers, so that we don't have things like get_user/put_user
294  * that can cause faults and scheduling migrate into our preempt-protected
295  * region.
296  */
297 #define preempt_disable()                       barrier()
298 #define sched_preempt_enable_no_resched()       barrier()
299 #define preempt_enable_no_resched()             barrier()
300 #define preempt_enable()                        barrier()
301 #define preempt_check_resched()                 do { } while (0)
302
303 #define preempt_disable_notrace()               barrier()
304 #define preempt_enable_no_resched_notrace()     barrier()
305 #define preempt_enable_notrace()                barrier()
306 #define preempt_check_resched_rt()              barrier()
307 #define preemptible()                           0
308
309 #define preempt_lazy_disable()                  barrier()
310 #define preempt_lazy_enable()                   barrier()
311
312 #endif /* CONFIG_PREEMPT_COUNT */
313
314 #ifdef MODULE
315 /*
316  * Modules have no business playing preemption tricks.
317  */
318 #undef sched_preempt_enable_no_resched
319 #undef preempt_enable_no_resched
320 #undef preempt_enable_no_resched_notrace
321 #undef preempt_check_resched
322 #endif
323
324 #define preempt_set_need_resched() \
325 do { \
326         set_preempt_need_resched(); \
327 } while (0)
328 #define preempt_fold_need_resched() \
329 do { \
330         if (tif_need_resched_now()) \
331                 set_preempt_need_resched(); \
332 } while (0)
333
334 #ifdef CONFIG_PREEMPT_NOTIFIERS
335
336 struct preempt_notifier;
337
338 /**
339  * preempt_ops - notifiers called when a task is preempted and rescheduled
340  * @sched_in: we're about to be rescheduled:
341  *    notifier: struct preempt_notifier for the task being scheduled
342  *    cpu:  cpu we're scheduled on
343  * @sched_out: we've just been preempted
344  *    notifier: struct preempt_notifier for the task being preempted
345  *    next: the task that's kicking us out
346  *
347  * Please note that sched_in and out are called under different
348  * contexts.  sched_out is called with rq lock held and irq disabled
349  * while sched_in is called without rq lock and irq enabled.  This
350  * difference is intentional and depended upon by its users.
351  */
352 struct preempt_ops {
353         void (*sched_in)(struct preempt_notifier *notifier, int cpu);
354         void (*sched_out)(struct preempt_notifier *notifier,
355                           struct task_struct *next);
356 };
357
358 /**
359  * preempt_notifier - key for installing preemption notifiers
360  * @link: internal use
361  * @ops: defines the notifier functions to be called
362  *
363  * Usually used in conjunction with container_of().
364  */
365 struct preempt_notifier {
366         struct hlist_node link;
367         struct preempt_ops *ops;
368 };
369
370 void preempt_notifier_inc(void);
371 void preempt_notifier_dec(void);
372 void preempt_notifier_register(struct preempt_notifier *notifier);
373 void preempt_notifier_unregister(struct preempt_notifier *notifier);
374
375 static inline void preempt_notifier_init(struct preempt_notifier *notifier,
376                                      struct preempt_ops *ops)
377 {
378         INIT_HLIST_NODE(&notifier->link);
379         notifier->ops = ops;
380 }
381
382 #endif
383
384 #ifdef CONFIG_SMP
385
386 /*
387  * Migrate-Disable and why it is undesired.
388  *
389  * When a preempted task becomes elegible to run under the ideal model (IOW it
390  * becomes one of the M highest priority tasks), it might still have to wait
391  * for the preemptee's migrate_disable() section to complete. Thereby suffering
392  * a reduction in bandwidth in the exact duration of the migrate_disable()
393  * section.
394  *
395  * Per this argument, the change from preempt_disable() to migrate_disable()
396  * gets us:
397  *
398  * - a higher priority tasks gains reduced wake-up latency; with preempt_disable()
399  *   it would have had to wait for the lower priority task.
400  *
401  * - a lower priority tasks; which under preempt_disable() could've instantly
402  *   migrated away when another CPU becomes available, is now constrained
403  *   by the ability to push the higher priority task away, which might itself be
404  *   in a migrate_disable() section, reducing it's available bandwidth.
405  *
406  * IOW it trades latency / moves the interference term, but it stays in the
407  * system, and as long as it remains unbounded, the system is not fully
408  * deterministic.
409  *
410  *
411  * The reason we have it anyway.
412  *
413  * PREEMPT_RT breaks a number of assumptions traditionally held. By forcing a
414  * number of primitives into becoming preemptible, they would also allow
415  * migration. This turns out to break a bunch of per-cpu usage. To this end,
416  * all these primitives employ migirate_disable() to restore this implicit
417  * assumption.
418  *
419  * This is a 'temporary' work-around at best. The correct solution is getting
420  * rid of the above assumptions and reworking the code to employ explicit
421  * per-cpu locking or short preempt-disable regions.
422  *
423  * The end goal must be to get rid of migrate_disable(), alternatively we need
424  * a schedulability theory that does not depend on abritrary migration.
425  *
426  *
427  * Notes on the implementation.
428  *
429  * The implementation is particularly tricky since existing code patterns
430  * dictate neither migrate_disable() nor migrate_enable() is allowed to block.
431  * This means that it cannot use cpus_read_lock() to serialize against hotplug,
432  * nor can it easily migrate itself into a pending affinity mask change on
433  * migrate_enable().
434  *
435  *
436  * Note: even non-work-conserving schedulers like semi-partitioned depends on
437  *       migration, so migrate_disable() is not only a problem for
438  *       work-conserving schedulers.
439  *
440  */
441 extern void migrate_disable(void);
442 extern void migrate_enable(void);
443
444 #else
445
446 static inline void migrate_disable(void)
447 {
448         preempt_lazy_disable();
449 }
450
451 static inline void migrate_enable(void)
452 {
453         preempt_lazy_enable();
454 }
455
456 #endif /* CONFIG_SMP */
457
458 #endif /* __LINUX_PREEMPT_H */