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_ACTIVE: 0x00200000
30 * PREEMPT_NEED_RESCHED: 0x80000000
32 #define PREEMPT_BITS 8
33 #define SOFTIRQ_BITS 8
34 #define HARDIRQ_BITS 4
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)
42 #define __IRQ_MASK(x) ((1UL << (x))-1)
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)
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)
54 #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
56 #define PREEMPT_ACTIVE_BITS 1
57 #define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
58 #define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
60 /* We use the MSB mostly because its available */
61 #define PREEMPT_NEED_RESCHED 0x80000000
63 /* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
64 #include <asm/preempt.h>
66 #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
67 #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
68 #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
72 * Are we doing bottom half or hardware interrupt processing?
73 * Are we in a softirq context? Interrupt context?
74 * in_softirq - Are we currently processing softirq or have bh disabled?
75 * in_serving_softirq - Are we currently processing softirq?
77 #define in_irq() (hardirq_count())
78 #define in_softirq() (softirq_count())
79 #define in_interrupt() (irq_count())
80 #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
83 * Are we in NMI context?
85 #define in_nmi() (preempt_count() & NMI_MASK)
87 #if defined(CONFIG_PREEMPT_COUNT)
88 # define PREEMPT_DISABLE_OFFSET 1
90 # define PREEMPT_DISABLE_OFFSET 0
94 * The preempt_count offset needed for things like:
98 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
99 * softirqs, such that unlock sequences of:
106 #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_DISABLE_OFFSET)
109 * Are we running in atomic context? WARNING: this macro cannot
110 * always detect atomic context; in particular, it cannot know about
111 * held spinlocks in non-preemptible kernels. Thus it should not be
112 * used in the general case to determine whether sleeping is possible.
113 * Do not use in_atomic() in driver code.
115 #define in_atomic() (preempt_count() != 0)
118 * Check whether we were atomic before we did preempt_disable():
119 * (used by the scheduler)
121 #define in_atomic_preempt_off() \
122 ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_DISABLE_OFFSET)
124 #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
125 extern void preempt_count_add(int val);
126 extern void preempt_count_sub(int val);
127 #define preempt_count_dec_and_test() ({ preempt_count_sub(1); should_resched(); })
129 #define preempt_count_add(val) __preempt_count_add(val)
130 #define preempt_count_sub(val) __preempt_count_sub(val)
131 #define preempt_count_dec_and_test() __preempt_count_dec_and_test()
134 #define __preempt_count_inc() __preempt_count_add(1)
135 #define __preempt_count_dec() __preempt_count_sub(1)
137 #define preempt_count_inc() preempt_count_add(1)
138 #define preempt_count_dec() preempt_count_sub(1)
140 #define preempt_active_enter() \
142 preempt_count_add(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET); \
146 #define preempt_active_exit() \
149 preempt_count_sub(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET); \
152 #ifdef CONFIG_PREEMPT_COUNT
154 #define preempt_disable() \
156 preempt_count_inc(); \
160 #define sched_preempt_enable_no_resched() \
163 preempt_count_dec(); \
166 #define preempt_enable_no_resched() sched_preempt_enable_no_resched()
168 #define preemptible() (preempt_count() == 0 && !irqs_disabled())
170 #ifdef CONFIG_PREEMPT
171 #define preempt_enable() \
174 if (unlikely(preempt_count_dec_and_test())) \
175 __preempt_schedule(); \
178 #define preempt_enable_notrace() \
181 if (unlikely(__preempt_count_dec_and_test())) \
182 __preempt_schedule_notrace(); \
185 #define preempt_check_resched() \
187 if (should_resched()) \
188 __preempt_schedule(); \
191 #else /* !CONFIG_PREEMPT */
192 #define preempt_enable() \
195 preempt_count_dec(); \
198 #define preempt_enable_notrace() \
201 __preempt_count_dec(); \
204 #define preempt_check_resched() do { } while (0)
205 #endif /* CONFIG_PREEMPT */
207 #define preempt_disable_notrace() \
209 __preempt_count_inc(); \
213 #define preempt_enable_no_resched_notrace() \
216 __preempt_count_dec(); \
219 #else /* !CONFIG_PREEMPT_COUNT */
222 * Even if we don't have any preemption, we need preempt disable/enable
223 * to be barriers, so that we don't have things like get_user/put_user
224 * that can cause faults and scheduling migrate into our preempt-protected
227 #define preempt_disable() barrier()
228 #define sched_preempt_enable_no_resched() barrier()
229 #define preempt_enable_no_resched() barrier()
230 #define preempt_enable() barrier()
231 #define preempt_check_resched() do { } while (0)
233 #define preempt_disable_notrace() barrier()
234 #define preempt_enable_no_resched_notrace() barrier()
235 #define preempt_enable_notrace() barrier()
236 #define preemptible() 0
238 #endif /* CONFIG_PREEMPT_COUNT */
242 * Modules have no business playing preemption tricks.
244 #undef sched_preempt_enable_no_resched
245 #undef preempt_enable_no_resched
246 #undef preempt_enable_no_resched_notrace
247 #undef preempt_check_resched
250 #define preempt_set_need_resched() \
252 set_preempt_need_resched(); \
254 #define preempt_fold_need_resched() \
256 if (tif_need_resched()) \
257 set_preempt_need_resched(); \
260 #ifdef CONFIG_PREEMPT_NOTIFIERS
262 struct preempt_notifier;
265 * preempt_ops - notifiers called when a task is preempted and rescheduled
266 * @sched_in: we're about to be rescheduled:
267 * notifier: struct preempt_notifier for the task being scheduled
268 * cpu: cpu we're scheduled on
269 * @sched_out: we've just been preempted
270 * notifier: struct preempt_notifier for the task being preempted
271 * next: the task that's kicking us out
273 * Please note that sched_in and out are called under different
274 * contexts. sched_out is called with rq lock held and irq disabled
275 * while sched_in is called without rq lock and irq enabled. This
276 * difference is intentional and depended upon by its users.
279 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
280 void (*sched_out)(struct preempt_notifier *notifier,
281 struct task_struct *next);
285 * preempt_notifier - key for installing preemption notifiers
286 * @link: internal use
287 * @ops: defines the notifier functions to be called
289 * Usually used in conjunction with container_of().
291 struct preempt_notifier {
292 struct hlist_node link;
293 struct preempt_ops *ops;
296 void preempt_notifier_inc(void);
297 void preempt_notifier_dec(void);
298 void preempt_notifier_register(struct preempt_notifier *notifier);
299 void preempt_notifier_unregister(struct preempt_notifier *notifier);
301 static inline void preempt_notifier_init(struct preempt_notifier *notifier,
302 struct preempt_ops *ops)
304 INIT_HLIST_NODE(¬ifier->link);
310 #endif /* __LINUX_PREEMPT_H */