Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1#ifndef __LINUX_PREEMPT_H
2#define __LINUX_PREEMPT_H
3
4/*
5 * include/linux/preempt.h - macros for accessing and manipulating
6 * preempt_count (used for kernel preemption, interrupt count, etc.)
7 */
8
9#include <linux/linkage.h>
10#include <linux/list.h>
11
12/*
13 * We put the hardirq and softirq counter into the preemption
14 * counter. The bitmask has the following meaning:
15 *
16 * - bits 0-7 are the preemption count (max preemption depth: 256)
17 * - bits 8-15 are the softirq count (max # of softirqs: 256)
18 *
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.
24 *
25 * PREEMPT_MASK: 0x000000ff
26 * SOFTIRQ_MASK: 0x0000ff00
27 * HARDIRQ_MASK: 0x000f0000
28 * NMI_MASK: 0x00100000
29 * PREEMPT_NEED_RESCHED: 0x80000000
30 */
31#define PREEMPT_BITS 8
32#define SOFTIRQ_BITS 8
33#define HARDIRQ_BITS 4
34#define NMI_BITS 1
35
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)
40
41#define __IRQ_MASK(x) ((1UL << (x))-1)
42
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)
47
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)
52
53#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
54
55/* We use the MSB mostly because its available */
56#define PREEMPT_NEED_RESCHED 0x80000000
57
58/* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */
59#include <asm/preempt.h>
60
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 \
64 | NMI_MASK))
65
66/*
67 * Are we doing bottom half or hardware interrupt processing?
68 *
69 * in_irq() - We're in (hard) IRQ context
70 * in_softirq() - We have BH disabled, or are processing softirqs
71 * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled
72 * in_serving_softirq() - We're in softirq context
73 * in_nmi() - We're in NMI context
74 * in_task() - We're in task context
75 *
76 * Note: due to the BH disabled confusion: in_softirq(),in_interrupt() really
77 * should not be used in new code.
78 */
79#define in_irq() (hardirq_count())
80#define in_softirq() (softirq_count())
81#define in_interrupt() (irq_count())
82#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
83#define in_nmi() (preempt_count() & NMI_MASK)
84#define in_task() (!(preempt_count() & \
85 (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
86
87/*
88 * The preempt_count offset after preempt_disable();
89 */
90#if defined(CONFIG_PREEMPT_COUNT)
91# define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
92#else
93# define PREEMPT_DISABLE_OFFSET 0
94#endif
95
96/*
97 * The preempt_count offset after spin_lock()
98 */
99#define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
100
101/*
102 * The preempt_count offset needed for things like:
103 *
104 * spin_lock_bh()
105 *
106 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
107 * softirqs, such that unlock sequences of:
108 *
109 * spin_unlock();
110 * local_bh_enable();
111 *
112 * Work as expected.
113 */
114#define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
115
116/*
117 * Are we running in atomic context? WARNING: this macro cannot
118 * always detect atomic context; in particular, it cannot know about
119 * held spinlocks in non-preemptible kernels. Thus it should not be
120 * used in the general case to determine whether sleeping is possible.
121 * Do not use in_atomic() in driver code.
122 */
123#define in_atomic() (preempt_count() != 0)
124
125/*
126 * Check whether we were atomic before we did preempt_disable():
127 * (used by the scheduler)
128 */
129#define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
130
131#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
132extern void preempt_count_add(int val);
133extern void preempt_count_sub(int val);
134#define preempt_count_dec_and_test() \
135 ({ preempt_count_sub(1); should_resched(0); })
136#else
137#define preempt_count_add(val) __preempt_count_add(val)
138#define preempt_count_sub(val) __preempt_count_sub(val)
139#define preempt_count_dec_and_test() __preempt_count_dec_and_test()
140#endif
141
142#define __preempt_count_inc() __preempt_count_add(1)
143#define __preempt_count_dec() __preempt_count_sub(1)
144
145#define preempt_count_inc() preempt_count_add(1)
146#define preempt_count_dec() preempt_count_sub(1)
147
148#ifdef CONFIG_PREEMPT_COUNT
149
150#define preempt_disable() \
151do { \
152 preempt_count_inc(); \
153 barrier(); \
154} while (0)
155
156#define sched_preempt_enable_no_resched() \
157do { \
158 barrier(); \
159 preempt_count_dec(); \
160} while (0)
161
162#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
163
164#define preemptible() (preempt_count() == 0 && !irqs_disabled())
165
166#ifdef CONFIG_PREEMPT
167#define preempt_enable() \
168do { \
169 barrier(); \
170 if (unlikely(preempt_count_dec_and_test())) \
171 __preempt_schedule(); \
172} while (0)
173
174#define preempt_enable_notrace() \
175do { \
176 barrier(); \
177 if (unlikely(__preempt_count_dec_and_test())) \
178 __preempt_schedule_notrace(); \
179} while (0)
180
181#define preempt_check_resched() \
182do { \
183 if (should_resched(0)) \
184 __preempt_schedule(); \
185} while (0)
186
187#else /* !CONFIG_PREEMPT */
188#define preempt_enable() \
189do { \
190 barrier(); \
191 preempt_count_dec(); \
192} while (0)
193
194#define preempt_enable_notrace() \
195do { \
196 barrier(); \
197 __preempt_count_dec(); \
198} while (0)
199
200#define preempt_check_resched() do { } while (0)
201#endif /* CONFIG_PREEMPT */
202
203#define preempt_disable_notrace() \
204do { \
205 __preempt_count_inc(); \
206 barrier(); \
207} while (0)
208
209#define preempt_enable_no_resched_notrace() \
210do { \
211 barrier(); \
212 __preempt_count_dec(); \
213} while (0)
214
215#else /* !CONFIG_PREEMPT_COUNT */
216
217/*
218 * Even if we don't have any preemption, we need preempt disable/enable
219 * to be barriers, so that we don't have things like get_user/put_user
220 * that can cause faults and scheduling migrate into our preempt-protected
221 * region.
222 */
223#define preempt_disable() barrier()
224#define sched_preempt_enable_no_resched() barrier()
225#define preempt_enable_no_resched() barrier()
226#define preempt_enable() barrier()
227#define preempt_check_resched() do { } while (0)
228
229#define preempt_disable_notrace() barrier()
230#define preempt_enable_no_resched_notrace() barrier()
231#define preempt_enable_notrace() barrier()
232#define preemptible() 0
233
234#endif /* CONFIG_PREEMPT_COUNT */
235
236#ifdef MODULE
237/*
238 * Modules have no business playing preemption tricks.
239 */
240#undef sched_preempt_enable_no_resched
241#undef preempt_enable_no_resched
242#undef preempt_enable_no_resched_notrace
243#undef preempt_check_resched
244#endif
245
246#define preempt_set_need_resched() \
247do { \
248 set_preempt_need_resched(); \
249} while (0)
250#define preempt_fold_need_resched() \
251do { \
252 if (tif_need_resched()) \
253 set_preempt_need_resched(); \
254} while (0)
255
256#ifdef CONFIG_PREEMPT_NOTIFIERS
257
258struct preempt_notifier;
259
260/**
261 * preempt_ops - notifiers called when a task is preempted and rescheduled
262 * @sched_in: we're about to be rescheduled:
263 * notifier: struct preempt_notifier for the task being scheduled
264 * cpu: cpu we're scheduled on
265 * @sched_out: we've just been preempted
266 * notifier: struct preempt_notifier for the task being preempted
267 * next: the task that's kicking us out
268 *
269 * Please note that sched_in and out are called under different
270 * contexts. sched_out is called with rq lock held and irq disabled
271 * while sched_in is called without rq lock and irq enabled. This
272 * difference is intentional and depended upon by its users.
273 */
274struct preempt_ops {
275 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
276 void (*sched_out)(struct preempt_notifier *notifier,
277 struct task_struct *next);
278};
279
280/**
281 * preempt_notifier - key for installing preemption notifiers
282 * @link: internal use
283 * @ops: defines the notifier functions to be called
284 *
285 * Usually used in conjunction with container_of().
286 */
287struct preempt_notifier {
288 struct hlist_node link;
289 struct preempt_ops *ops;
290};
291
292void preempt_notifier_inc(void);
293void preempt_notifier_dec(void);
294void preempt_notifier_register(struct preempt_notifier *notifier);
295void preempt_notifier_unregister(struct preempt_notifier *notifier);
296
297static inline void preempt_notifier_init(struct preempt_notifier *notifier,
298 struct preempt_ops *ops)
299{
300 INIT_HLIST_NODE(¬ifier->link);
301 notifier->ops = ops;
302}
303
304#endif
305
306#endif /* __LINUX_PREEMPT_H */