blob: 8cc0338a5e9a5252af1ab48e4d5d6df34943f0cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/linkage.h>
Avi Kivitye107be32007-07-26 13:40:43 +020010#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Peter Zijlstraf27dde82013-08-14 14:55:31 +020012/*
13 * We use the MSB mostly because its available; see <linux/preempt_mask.h> for
14 * the other bits -- can't include that header due to inclusion hell.
15 */
16#define PREEMPT_NEED_RESCHED 0x80000000
17
Peter Zijlstraa7878702013-08-14 14:55:40 +020018#include <asm/preempt.h>
Peter Zijlstraf27dde82013-08-14 14:55:31 +020019
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020020/*
21 * We put the hardirq and softirq counter into the preemption
22 * counter. The bitmask has the following meaning:
23 *
24 * - bits 0-7 are the preemption count (max preemption depth: 256)
25 * - bits 8-15 are the softirq count (max # of softirqs: 256)
26 *
27 * The hardirq count could in theory be the same as the number of
28 * interrupts in the system, but we run all interrupt handlers with
29 * interrupts disabled, so we cannot have nesting interrupts. Though
30 * there are a few palaeontologic drivers which reenable interrupts in
31 * the handler, so we need more than one bit here.
32 *
33 * PREEMPT_MASK: 0x000000ff
34 * SOFTIRQ_MASK: 0x0000ff00
35 * HARDIRQ_MASK: 0x000f0000
36 * NMI_MASK: 0x00100000
37 * PREEMPT_ACTIVE: 0x00200000
38 */
39#define PREEMPT_BITS 8
40#define SOFTIRQ_BITS 8
41#define HARDIRQ_BITS 4
42#define NMI_BITS 1
43
44#define PREEMPT_SHIFT 0
45#define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
46#define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
47#define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
48
49#define __IRQ_MASK(x) ((1UL << (x))-1)
50
51#define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
52#define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
53#define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
54#define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
55
56#define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
57#define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
58#define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
59#define NMI_OFFSET (1UL << NMI_SHIFT)
60
61#define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET)
62
63#define PREEMPT_ACTIVE_BITS 1
64#define PREEMPT_ACTIVE_SHIFT (NMI_SHIFT + NMI_BITS)
65#define PREEMPT_ACTIVE (__IRQ_MASK(PREEMPT_ACTIVE_BITS) << PREEMPT_ACTIVE_SHIFT)
66
67#define hardirq_count() (preempt_count() & HARDIRQ_MASK)
68#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
69#define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
70 | NMI_MASK))
71
72/*
73 * Are we doing bottom half or hardware interrupt processing?
74 * Are we in a softirq context? Interrupt context?
75 * in_softirq - Are we currently processing softirq or have bh disabled?
76 * in_serving_softirq - Are we currently processing softirq?
77 */
78#define in_irq() (hardirq_count())
79#define in_softirq() (softirq_count())
80#define in_interrupt() (irq_count())
81#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
82
83/*
84 * Are we in NMI context?
85 */
86#define in_nmi() (preempt_count() & NMI_MASK)
87
88#if defined(CONFIG_PREEMPT_COUNT)
89# define PREEMPT_CHECK_OFFSET 1
90#else
91# define PREEMPT_CHECK_OFFSET 0
92#endif
93
94/*
95 * The preempt_count offset needed for things like:
96 *
97 * spin_lock_bh()
98 *
99 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
100 * softirqs, such that unlock sequences of:
101 *
102 * spin_unlock();
103 * local_bh_enable();
104 *
105 * Work as expected.
106 */
107#define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_CHECK_OFFSET)
108
109/*
110 * Are we running in atomic context? WARNING: this macro cannot
111 * always detect atomic context; in particular, it cannot know about
112 * held spinlocks in non-preemptible kernels. Thus it should not be
113 * used in the general case to determine whether sleeping is possible.
114 * Do not use in_atomic() in driver code.
115 */
116#define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0)
117
118/*
119 * Check whether we were atomic before we did preempt_disable():
120 * (used by the scheduler, *after* releasing the kernel lock)
121 */
122#define in_atomic_preempt_off() \
123 ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_CHECK_OFFSET)
124
125#ifdef CONFIG_PREEMPT_COUNT
126# define preemptible() (preempt_count() == 0 && !irqs_disabled())
127#else
128# define preemptible() 0
129#endif
130
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200131#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200132extern void preempt_count_add(int val);
133extern void preempt_count_sub(int val);
134#define preempt_count_dec_and_test() ({ preempt_count_sub(1); should_resched(); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#else
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200136#define preempt_count_add(val) __preempt_count_add(val)
137#define preempt_count_sub(val) __preempt_count_sub(val)
138#define preempt_count_dec_and_test() __preempt_count_dec_and_test()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#endif
140
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200141#define __preempt_count_inc() __preempt_count_add(1)
142#define __preempt_count_dec() __preempt_count_sub(1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200144#define preempt_count_inc() preempt_count_add(1)
145#define preempt_count_dec() preempt_count_sub(1)
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200146
147#ifdef CONFIG_PREEMPT_COUNT
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#define preempt_disable() \
150do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200151 preempt_count_inc(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 barrier(); \
153} while (0)
154
Thomas Gleixnerba74c142011-03-21 13:32:17 +0100155#define sched_preempt_enable_no_resched() \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156do { \
157 barrier(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200158 preempt_count_dec(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159} while (0)
160
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200161#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
Thomas Gleixnerba74c142011-03-21 13:32:17 +0100162
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200163#ifdef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define preempt_enable() \
165do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200166 barrier(); \
167 if (unlikely(preempt_count_dec_and_test())) \
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200168 __preempt_schedule(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169} while (0)
170
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200171#define preempt_check_resched() \
172do { \
173 if (should_resched()) \
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200174 __preempt_schedule(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200175} while (0)
176
177#else
Peter Zijlstra62b94a02013-11-20 16:52:19 +0100178#define preempt_enable() \
179do { \
180 barrier(); \
181 preempt_count_dec(); \
182} while (0)
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200183#define preempt_check_resched() do { } while (0)
184#endif
Steven Rostedt50282522008-05-12 21:20:41 +0200185
186#define preempt_disable_notrace() \
187do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200188 __preempt_count_inc(); \
Steven Rostedt50282522008-05-12 21:20:41 +0200189 barrier(); \
190} while (0)
191
192#define preempt_enable_no_resched_notrace() \
193do { \
194 barrier(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200195 __preempt_count_dec(); \
Steven Rostedt50282522008-05-12 21:20:41 +0200196} while (0)
197
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200198#ifdef CONFIG_PREEMPT
199
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200200#ifndef CONFIG_CONTEXT_TRACKING
201#define __preempt_schedule_context() __preempt_schedule()
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200202#endif
203
Steven Rostedt50282522008-05-12 21:20:41 +0200204#define preempt_enable_notrace() \
205do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200206 barrier(); \
207 if (unlikely(__preempt_count_dec_and_test())) \
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200208 __preempt_schedule_context(); \
Steven Rostedt50282522008-05-12 21:20:41 +0200209} while (0)
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200210#else
Peter Zijlstra62b94a02013-11-20 16:52:19 +0100211#define preempt_enable_notrace() \
212do { \
213 barrier(); \
214 __preempt_count_dec(); \
215} while (0)
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200216#endif
Steven Rostedt50282522008-05-12 21:20:41 +0200217
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200218#else /* !CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds386afc92013-04-09 10:48:33 -0700220/*
221 * Even if we don't have any preemption, we need preempt disable/enable
222 * to be barriers, so that we don't have things like get_user/put_user
223 * that can cause faults and scheduling migrate into our preempt-protected
224 * region.
225 */
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200226#define preempt_disable() barrier()
Linus Torvalds386afc92013-04-09 10:48:33 -0700227#define sched_preempt_enable_no_resched() barrier()
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200228#define preempt_enable_no_resched() barrier()
229#define preempt_enable() barrier()
230#define preempt_check_resched() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Linus Torvalds386afc92013-04-09 10:48:33 -0700232#define preempt_disable_notrace() barrier()
233#define preempt_enable_no_resched_notrace() barrier()
234#define preempt_enable_notrace() barrier()
Steven Rostedt50282522008-05-12 21:20:41 +0200235
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200236#endif /* CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Peter Zijlstra62b94a02013-11-20 16:52:19 +0100238#ifdef MODULE
239/*
240 * Modules have no business playing preemption tricks.
241 */
242#undef sched_preempt_enable_no_resched
243#undef preempt_enable_no_resched
244#undef preempt_enable_no_resched_notrace
245#undef preempt_check_resched
246#endif
247
Peter Zijlstra8cb75e02013-11-20 12:22:37 +0100248#define preempt_set_need_resched() \
249do { \
250 set_preempt_need_resched(); \
251} while (0)
252#define preempt_fold_need_resched() \
253do { \
254 if (tif_need_resched()) \
255 set_preempt_need_resched(); \
256} while (0)
Peter Zijlstra8cb75e02013-11-20 12:22:37 +0100257
Avi Kivitye107be32007-07-26 13:40:43 +0200258#ifdef CONFIG_PREEMPT_NOTIFIERS
259
260struct preempt_notifier;
261
262/**
263 * preempt_ops - notifiers called when a task is preempted and rescheduled
264 * @sched_in: we're about to be rescheduled:
265 * notifier: struct preempt_notifier for the task being scheduled
266 * cpu: cpu we're scheduled on
267 * @sched_out: we've just been preempted
268 * notifier: struct preempt_notifier for the task being preempted
269 * next: the task that's kicking us out
Tejun Heo8592e642009-12-02 12:56:46 +0900270 *
271 * Please note that sched_in and out are called under different
272 * contexts. sched_out is called with rq lock held and irq disabled
273 * while sched_in is called without rq lock and irq enabled. This
274 * difference is intentional and depended upon by its users.
Avi Kivitye107be32007-07-26 13:40:43 +0200275 */
276struct preempt_ops {
277 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
278 void (*sched_out)(struct preempt_notifier *notifier,
279 struct task_struct *next);
280};
281
282/**
283 * preempt_notifier - key for installing preemption notifiers
284 * @link: internal use
285 * @ops: defines the notifier functions to be called
286 *
287 * Usually used in conjunction with container_of().
288 */
289struct preempt_notifier {
290 struct hlist_node link;
291 struct preempt_ops *ops;
292};
293
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(&notifier->link);
301 notifier->ops = ops;
302}
303
304#endif
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#endif /* __LINUX_PREEMPT_H */