1 #ifndef __LINUX_PREEMPT_H
2 #define __LINUX_PREEMPT_H
5 * include/linux/preempt.h - macros for accessing and manipulating
6 * preempt_count (used for kernel preemption, interrupt count, etc.)
9 #include <linux/linkage.h>
10 #include <linux/list.h>
13 * We put the hardirq and softirq counter into the preemption
14 * counter. The bitmask has the following meaning:
16 * - bits 0-7 are the preemption count (max preemption depth: 256)
17 * - bits 8-15 are the softirq count (max # of softirqs: 256)
19 * The hardirq count could in theory be the same as the number of
20 * interrupts in the system, but we run all interrupt handlers with
21 * interrupts disabled, so we cannot have nesting interrupts. Though
22 * there are a few palaeontologic drivers which reenable interrupts in
23 * the handler, so we need more than one bit here.
25 * PREEMPT_MASK: 0x000000ff
26 * SOFTIRQ_MASK: 0x0000ff00
27 * HARDIRQ_MASK: 0x000f0000
28 * NMI_MASK: 0x00100000
29 * PREEMPT_NEED_RESCHED: 0x80000000
31 #define PREEMPT_BITS 8
32 #define SOFTIRQ_BITS 8
33 #define HARDIRQ_BITS 4
36 #define PREEMPT_SHIFT 0
37 #define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
38 #define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
39 #define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
41 #define __IRQ_MASK(x) ((1UL << (x))-1)
43 #define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
44 #define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
45 #define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
46 #define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
48 #define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
49 #define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
50 #define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
51 #define NMI_OFFSET (1UL << NMI_SHIFT)
53 #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
55 /* We use the MSB mostly because its available */
56 #define PREEMPT_NEED_RESCHED 0x80000000
58 /* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
59 #include <asm/preempt.h>
61 #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
62 #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
63 #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
67 * Are we doing bottom half or hardware interrupt processing?
68 * Are we in a softirq context? Interrupt context?
69 * in_softirq - Are we currently processing softirq or have bh disabled?
70 * in_serving_softirq - Are we currently processing softirq?
72 #define in_irq() (hardirq_count())
73 #define in_softirq() (softirq_count())
74 #define in_interrupt() (irq_count())
75 #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
78 * Are we in NMI context?
80 #define in_nmi() (preempt_count() & NMI_MASK)
83 * The preempt_count offset after preempt_disable();
85 #if defined(CONFIG_PREEMPT_COUNT)
86 # define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
88 # define PREEMPT_DISABLE_OFFSET 0
92 * The preempt_count offset after spin_lock()
94 #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
97 * The preempt_count offset needed for things like:
101 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
102 * softirqs, such that unlock sequences of:
109 #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
112 * Are we running in atomic context? WARNING: this macro cannot
113 * always detect atomic context; in particular, it cannot know about
114 * held spinlocks in non-preemptible kernels. Thus it should not be
115 * used in the general case to determine whether sleeping is possible.
116 * Do not use in_atomic() in driver code.
118 #define in_atomic() (preempt_count() != 0)
121 * Check whether we were atomic before we did preempt_disable():
122 * (used by the scheduler)
124 #define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
126 #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
127 extern void preempt_count_add(int val);
128 extern void preempt_count_sub(int val);
129 #define preempt_count_dec_and_test() \
130 ({ preempt_count_sub(1); should_resched(0); })
132 #define preempt_count_add(val) __preempt_count_add(val)
133 #define preempt_count_sub(val) __preempt_count_sub(val)
134 #define preempt_count_dec_and_test() __preempt_count_dec_and_test()
137 #define __preempt_count_inc() __preempt_count_add(1)
138 #define __preempt_count_dec() __preempt_count_sub(1)
140 #define preempt_count_inc() preempt_count_add(1)
141 #define preempt_count_dec() preempt_count_sub(1)
143 #ifdef CONFIG_PREEMPT_COUNT
145 #define preempt_disable() \
147 preempt_count_inc(); \
151 #define sched_preempt_enable_no_resched() \
154 preempt_count_dec(); \
157 #define preempt_enable_no_resched() sched_preempt_enable_no_resched()
159 #define preemptible() (preempt_count() == 0 && !irqs_disabled())
161 #ifdef CONFIG_PREEMPT
162 #define preempt_enable() \
165 if (unlikely(preempt_count_dec_and_test())) \
166 __preempt_schedule(); \
169 #define preempt_enable_notrace() \
172 if (unlikely(__preempt_count_dec_and_test())) \
173 __preempt_schedule_notrace(); \
176 #define preempt_check_resched() \
178 if (should_resched(0)) \
179 __preempt_schedule(); \
182 #else /* !CONFIG_PREEMPT */
183 #define preempt_enable() \
186 preempt_count_dec(); \
189 #define preempt_enable_notrace() \
192 __preempt_count_dec(); \
195 #define preempt_check_resched() do { } while (0)
196 #endif /* CONFIG_PREEMPT */
198 #define preempt_disable_notrace() \
200 __preempt_count_inc(); \
204 #define preempt_enable_no_resched_notrace() \
207 __preempt_count_dec(); \
210 #else /* !CONFIG_PREEMPT_COUNT */
213 * Even if we don't have any preemption, we need preempt disable/enable
214 * to be barriers, so that we don't have things like get_user/put_user
215 * that can cause faults and scheduling migrate into our preempt-protected
218 #define preempt_disable() barrier()
219 #define sched_preempt_enable_no_resched() barrier()
220 #define preempt_enable_no_resched() barrier()
221 #define preempt_enable() barrier()
222 #define preempt_check_resched() do { } while (0)
224 #define preempt_disable_notrace() barrier()
225 #define preempt_enable_no_resched_notrace() barrier()
226 #define preempt_enable_notrace() barrier()
227 #define preemptible() 0
229 #endif /* CONFIG_PREEMPT_COUNT */
233 * Modules have no business playing preemption tricks.
235 #undef sched_preempt_enable_no_resched
236 #undef preempt_enable_no_resched
237 #undef preempt_enable_no_resched_notrace
238 #undef preempt_check_resched
241 #define preempt_set_need_resched() \
243 set_preempt_need_resched(); \
245 #define preempt_fold_need_resched() \
247 if (tif_need_resched()) \
248 set_preempt_need_resched(); \
251 #ifdef CONFIG_PREEMPT_NOTIFIERS
253 struct preempt_notifier;
256 * preempt_ops - notifiers called when a task is preempted and rescheduled
257 * @sched_in: we're about to be rescheduled:
258 * notifier: struct preempt_notifier for the task being scheduled
259 * cpu: cpu we're scheduled on
260 * @sched_out: we've just been preempted
261 * notifier: struct preempt_notifier for the task being preempted
262 * next: the task that's kicking us out
264 * Please note that sched_in and out are called under different
265 * contexts. sched_out is called with rq lock held and irq disabled
266 * while sched_in is called without rq lock and irq enabled. This
267 * difference is intentional and depended upon by its users.
270 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
271 void (*sched_out)(struct preempt_notifier *notifier,
272 struct task_struct *next);
276 * preempt_notifier - key for installing preemption notifiers
277 * @link: internal use
278 * @ops: defines the notifier functions to be called
280 * Usually used in conjunction with container_of().
282 struct preempt_notifier {
283 struct hlist_node link;
284 struct preempt_ops *ops;
287 void preempt_notifier_inc(void);
288 void preempt_notifier_dec(void);
289 void preempt_notifier_register(struct preempt_notifier *notifier);
290 void preempt_notifier_unregister(struct preempt_notifier *notifier);
292 static inline void preempt_notifier_init(struct preempt_notifier *notifier,
293 struct preempt_ops *ops)
295 INIT_HLIST_NODE(¬ifier->link);
301 #endif /* __LINUX_PREEMPT_H */