blob: 75e4e30677f1254fb2c9f076f71fc4e2df1fb9be [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/*
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020013 * 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 *
Frederic Weisbecker2e10e712015-05-12 16:41:47 +020025 * PREEMPT_MASK: 0x000000ff
26 * SOFTIRQ_MASK: 0x0000ff00
27 * HARDIRQ_MASK: 0x000f0000
28 * NMI_MASK: 0x00100000
Frederic Weisbecker2e10e712015-05-12 16:41:47 +020029 * PREEMPT_NEED_RESCHED: 0x80000000
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020030 */
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
Frederic Weisbecker2e10e712015-05-12 16:41:47 +020055/* 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
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020061#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 * 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?
71 */
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)
76
77/*
78 * Are we in NMI context?
79 */
80#define in_nmi() (preempt_count() & NMI_MASK)
81
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +030082/*
83 * The preempt_count offset after preempt_disable();
84 */
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020085#if defined(CONFIG_PREEMPT_COUNT)
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +030086# define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020087#else
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +030088# define PREEMPT_DISABLE_OFFSET 0
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020089#endif
90
91/*
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +030092 * The preempt_count offset after spin_lock()
93 */
94#define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET
95
96/*
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020097 * The preempt_count offset needed for things like:
98 *
99 * spin_lock_bh()
100 *
101 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and
102 * softirqs, such that unlock sequences of:
103 *
104 * spin_unlock();
105 * local_bh_enable();
106 *
107 * Work as expected.
108 */
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300109#define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200110
111/*
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.
117 */
Frederic Weisbecker3e51f3c2015-05-12 16:41:51 +0200118#define in_atomic() (preempt_count() != 0)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200119
120/*
121 * Check whether we were atomic before we did preempt_disable():
Frederic Weisbeckere017cf22015-05-12 16:41:50 +0200122 * (used by the scheduler)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200123 */
Peter Zijlstrada7142e2015-09-28 18:11:45 +0200124#define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET)
Frederic Weisbecker92cf2112015-05-12 16:41:46 +0200125
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +0200126#if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER)
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200127extern void preempt_count_add(int val);
128extern void preempt_count_sub(int val);
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300129#define preempt_count_dec_and_test() \
130 ({ preempt_count_sub(1); should_resched(0); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#else
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200132#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()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135#endif
136
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200137#define __preempt_count_inc() __preempt_count_add(1)
138#define __preempt_count_dec() __preempt_count_sub(1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200140#define preempt_count_inc() preempt_count_add(1)
141#define preempt_count_dec() preempt_count_sub(1)
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200142
143#ifdef CONFIG_PREEMPT_COUNT
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#define preempt_disable() \
146do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200147 preempt_count_inc(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 barrier(); \
149} while (0)
150
Thomas Gleixnerba74c142011-03-21 13:32:17 +0100151#define sched_preempt_enable_no_resched() \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152do { \
153 barrier(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200154 preempt_count_dec(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155} while (0)
156
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200157#define preempt_enable_no_resched() sched_preempt_enable_no_resched()
Thomas Gleixnerba74c142011-03-21 13:32:17 +0100158
Frederic Weisbecker2e10e712015-05-12 16:41:47 +0200159#define preemptible() (preempt_count() == 0 && !irqs_disabled())
160
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200161#ifdef CONFIG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define preempt_enable() \
163do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200164 barrier(); \
165 if (unlikely(preempt_count_dec_and_test())) \
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200166 __preempt_schedule(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167} while (0)
168
Frederic Weisbecker9a92e3d2015-06-04 17:39:09 +0200169#define preempt_enable_notrace() \
170do { \
171 barrier(); \
172 if (unlikely(__preempt_count_dec_and_test())) \
173 __preempt_schedule_notrace(); \
174} while (0)
175
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200176#define preempt_check_resched() \
177do { \
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300178 if (should_resched(0)) \
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200179 __preempt_schedule(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200180} while (0)
181
Frederic Weisbecker9a92e3d2015-06-04 17:39:09 +0200182#else /* !CONFIG_PREEMPT */
Peter Zijlstra62b94a02013-11-20 16:52:19 +0100183#define preempt_enable() \
184do { \
185 barrier(); \
186 preempt_count_dec(); \
187} while (0)
Frederic Weisbecker9a92e3d2015-06-04 17:39:09 +0200188
189#define preempt_enable_notrace() \
190do { \
191 barrier(); \
192 __preempt_count_dec(); \
193} while (0)
194
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200195#define preempt_check_resched() do { } while (0)
Frederic Weisbecker9a92e3d2015-06-04 17:39:09 +0200196#endif /* CONFIG_PREEMPT */
Steven Rostedt50282522008-05-12 21:20:41 +0200197
198#define preempt_disable_notrace() \
199do { \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200200 __preempt_count_inc(); \
Steven Rostedt50282522008-05-12 21:20:41 +0200201 barrier(); \
202} while (0)
203
204#define preempt_enable_no_resched_notrace() \
205do { \
206 barrier(); \
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200207 __preempt_count_dec(); \
Steven Rostedt50282522008-05-12 21:20:41 +0200208} while (0)
209
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200210#else /* !CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds386afc92013-04-09 10:48:33 -0700212/*
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
216 * region.
217 */
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200218#define preempt_disable() barrier()
Linus Torvalds386afc92013-04-09 10:48:33 -0700219#define sched_preempt_enable_no_resched() barrier()
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200220#define preempt_enable_no_resched() barrier()
221#define preempt_enable() barrier()
222#define preempt_check_resched() do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds386afc92013-04-09 10:48:33 -0700224#define preempt_disable_notrace() barrier()
225#define preempt_enable_no_resched_notrace() barrier()
226#define preempt_enable_notrace() barrier()
Frederic Weisbecker2e10e712015-05-12 16:41:47 +0200227#define preemptible() 0
Steven Rostedt50282522008-05-12 21:20:41 +0200228
Frederic Weisbeckerbdd4e852011-06-08 01:13:27 +0200229#endif /* CONFIG_PREEMPT_COUNT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Peter Zijlstra62b94a02013-11-20 16:52:19 +0100231#ifdef MODULE
232/*
233 * Modules have no business playing preemption tricks.
234 */
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
239#endif
240
Peter Zijlstra8cb75e02013-11-20 12:22:37 +0100241#define preempt_set_need_resched() \
242do { \
243 set_preempt_need_resched(); \
244} while (0)
245#define preempt_fold_need_resched() \
246do { \
247 if (tif_need_resched()) \
248 set_preempt_need_resched(); \
249} while (0)
Peter Zijlstra8cb75e02013-11-20 12:22:37 +0100250
Avi Kivitye107be32007-07-26 13:40:43 +0200251#ifdef CONFIG_PREEMPT_NOTIFIERS
252
253struct preempt_notifier;
254
255/**
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
Tejun Heo8592e642009-12-02 12:56:46 +0900263 *
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.
Avi Kivitye107be32007-07-26 13:40:43 +0200268 */
269struct preempt_ops {
270 void (*sched_in)(struct preempt_notifier *notifier, int cpu);
271 void (*sched_out)(struct preempt_notifier *notifier,
272 struct task_struct *next);
273};
274
275/**
276 * preempt_notifier - key for installing preemption notifiers
277 * @link: internal use
278 * @ops: defines the notifier functions to be called
279 *
280 * Usually used in conjunction with container_of().
281 */
282struct preempt_notifier {
283 struct hlist_node link;
284 struct preempt_ops *ops;
285};
286
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200287void preempt_notifier_inc(void);
288void preempt_notifier_dec(void);
Avi Kivitye107be32007-07-26 13:40:43 +0200289void preempt_notifier_register(struct preempt_notifier *notifier);
290void preempt_notifier_unregister(struct preempt_notifier *notifier);
291
292static inline void preempt_notifier_init(struct preempt_notifier *notifier,
293 struct preempt_ops *ops)
294{
295 INIT_HLIST_NODE(&notifier->link);
296 notifier->ops = ops;
297}
298
299#endif
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#endif /* __LINUX_PREEMPT_H */