Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/linkage.h> |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 10 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 12 | /* |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 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 | * |
Frederic Weisbecker | 2e10e71 | 2015-05-12 16:41:47 +0200 | [diff] [blame] | 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 |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 31 | */ |
| 32 | #define PREEMPT_BITS 8 |
| 33 | #define SOFTIRQ_BITS 8 |
| 34 | #define HARDIRQ_BITS 4 |
| 35 | #define NMI_BITS 1 |
| 36 | |
| 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) |
| 41 | |
| 42 | #define __IRQ_MASK(x) ((1UL << (x))-1) |
| 43 | |
| 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) |
| 48 | |
| 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) |
| 53 | |
| 54 | #define SOFTIRQ_DISABLE_OFFSET (2 * SOFTIRQ_OFFSET) |
| 55 | |
| 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) |
| 59 | |
Frederic Weisbecker | 2e10e71 | 2015-05-12 16:41:47 +0200 | [diff] [blame] | 60 | /* We use the MSB mostly because its available */ |
| 61 | #define PREEMPT_NEED_RESCHED 0x80000000 |
| 62 | |
| 63 | /* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */ |
| 64 | #include <asm/preempt.h> |
| 65 | |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 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 \ |
| 69 | | NMI_MASK)) |
| 70 | |
| 71 | /* |
| 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? |
| 76 | */ |
| 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) |
| 81 | |
| 82 | /* |
| 83 | * Are we in NMI context? |
| 84 | */ |
| 85 | #define in_nmi() (preempt_count() & NMI_MASK) |
| 86 | |
| 87 | #if defined(CONFIG_PREEMPT_COUNT) |
Frederic Weisbecker | 90b62b5 | 2015-05-12 16:41:48 +0200 | [diff] [blame^] | 88 | # define PREEMPT_DISABLE_OFFSET 1 |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 89 | #else |
Frederic Weisbecker | 90b62b5 | 2015-05-12 16:41:48 +0200 | [diff] [blame^] | 90 | # define PREEMPT_DISABLE_OFFSET 0 |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | /* |
| 94 | * The preempt_count offset needed for things like: |
| 95 | * |
| 96 | * spin_lock_bh() |
| 97 | * |
| 98 | * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and |
| 99 | * softirqs, such that unlock sequences of: |
| 100 | * |
| 101 | * spin_unlock(); |
| 102 | * local_bh_enable(); |
| 103 | * |
| 104 | * Work as expected. |
| 105 | */ |
Frederic Weisbecker | 90b62b5 | 2015-05-12 16:41:48 +0200 | [diff] [blame^] | 106 | #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_DISABLE_OFFSET) |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 107 | |
| 108 | /* |
| 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. |
| 114 | */ |
| 115 | #define in_atomic() ((preempt_count() & ~PREEMPT_ACTIVE) != 0) |
| 116 | |
| 117 | /* |
| 118 | * Check whether we were atomic before we did preempt_disable(): |
| 119 | * (used by the scheduler, *after* releasing the kernel lock) |
| 120 | */ |
| 121 | #define in_atomic_preempt_off() \ |
Frederic Weisbecker | 90b62b5 | 2015-05-12 16:41:48 +0200 | [diff] [blame^] | 122 | ((preempt_count() & ~PREEMPT_ACTIVE) != PREEMPT_DISABLE_OFFSET) |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 123 | |
Steven Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 124 | #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 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(); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | #else |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 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() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | #endif |
| 133 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 134 | #define __preempt_count_inc() __preempt_count_add(1) |
| 135 | #define __preempt_count_dec() __preempt_count_sub(1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 137 | #define preempt_count_inc() preempt_count_add(1) |
| 138 | #define preempt_count_dec() preempt_count_sub(1) |
Frederic Weisbecker | bdd4e85 | 2011-06-08 01:13:27 +0200 | [diff] [blame] | 139 | |
| 140 | #ifdef CONFIG_PREEMPT_COUNT |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | #define preempt_disable() \ |
| 143 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 144 | preempt_count_inc(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | barrier(); \ |
| 146 | } while (0) |
| 147 | |
Thomas Gleixner | ba74c14 | 2011-03-21 13:32:17 +0100 | [diff] [blame] | 148 | #define sched_preempt_enable_no_resched() \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | do { \ |
| 150 | barrier(); \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 151 | preempt_count_dec(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } while (0) |
| 153 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 154 | #define preempt_enable_no_resched() sched_preempt_enable_no_resched() |
Thomas Gleixner | ba74c14 | 2011-03-21 13:32:17 +0100 | [diff] [blame] | 155 | |
Frederic Weisbecker | 2e10e71 | 2015-05-12 16:41:47 +0200 | [diff] [blame] | 156 | #define preemptible() (preempt_count() == 0 && !irqs_disabled()) |
| 157 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 158 | #ifdef CONFIG_PREEMPT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | #define preempt_enable() \ |
| 160 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 161 | barrier(); \ |
| 162 | if (unlikely(preempt_count_dec_and_test())) \ |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 163 | __preempt_schedule(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } while (0) |
| 165 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 166 | #define preempt_check_resched() \ |
| 167 | do { \ |
| 168 | if (should_resched()) \ |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 169 | __preempt_schedule(); \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 170 | } while (0) |
| 171 | |
| 172 | #else |
Peter Zijlstra | 62b94a0 | 2013-11-20 16:52:19 +0100 | [diff] [blame] | 173 | #define preempt_enable() \ |
| 174 | do { \ |
| 175 | barrier(); \ |
| 176 | preempt_count_dec(); \ |
| 177 | } while (0) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 178 | #define preempt_check_resched() do { } while (0) |
| 179 | #endif |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 180 | |
| 181 | #define preempt_disable_notrace() \ |
| 182 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 183 | __preempt_count_inc(); \ |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 184 | barrier(); \ |
| 185 | } while (0) |
| 186 | |
| 187 | #define preempt_enable_no_resched_notrace() \ |
| 188 | do { \ |
| 189 | barrier(); \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 190 | __preempt_count_dec(); \ |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 191 | } while (0) |
| 192 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 193 | #ifdef CONFIG_PREEMPT |
| 194 | |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 195 | #ifndef CONFIG_CONTEXT_TRACKING |
| 196 | #define __preempt_schedule_context() __preempt_schedule() |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 197 | #endif |
| 198 | |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 199 | #define preempt_enable_notrace() \ |
| 200 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 201 | barrier(); \ |
| 202 | if (unlikely(__preempt_count_dec_and_test())) \ |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 203 | __preempt_schedule_context(); \ |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 204 | } while (0) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 205 | #else |
Peter Zijlstra | 62b94a0 | 2013-11-20 16:52:19 +0100 | [diff] [blame] | 206 | #define preempt_enable_notrace() \ |
| 207 | do { \ |
| 208 | barrier(); \ |
| 209 | __preempt_count_dec(); \ |
| 210 | } while (0) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 211 | #endif |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 212 | |
Frederic Weisbecker | bdd4e85 | 2011-06-08 01:13:27 +0200 | [diff] [blame] | 213 | #else /* !CONFIG_PREEMPT_COUNT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 215 | /* |
| 216 | * Even if we don't have any preemption, we need preempt disable/enable |
| 217 | * to be barriers, so that we don't have things like get_user/put_user |
| 218 | * that can cause faults and scheduling migrate into our preempt-protected |
| 219 | * region. |
| 220 | */ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 221 | #define preempt_disable() barrier() |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 222 | #define sched_preempt_enable_no_resched() barrier() |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 223 | #define preempt_enable_no_resched() barrier() |
| 224 | #define preempt_enable() barrier() |
| 225 | #define preempt_check_resched() do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 227 | #define preempt_disable_notrace() barrier() |
| 228 | #define preempt_enable_no_resched_notrace() barrier() |
| 229 | #define preempt_enable_notrace() barrier() |
Frederic Weisbecker | 2e10e71 | 2015-05-12 16:41:47 +0200 | [diff] [blame] | 230 | #define preemptible() 0 |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 231 | |
Frederic Weisbecker | bdd4e85 | 2011-06-08 01:13:27 +0200 | [diff] [blame] | 232 | #endif /* CONFIG_PREEMPT_COUNT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Peter Zijlstra | 62b94a0 | 2013-11-20 16:52:19 +0100 | [diff] [blame] | 234 | #ifdef MODULE |
| 235 | /* |
| 236 | * Modules have no business playing preemption tricks. |
| 237 | */ |
| 238 | #undef sched_preempt_enable_no_resched |
| 239 | #undef preempt_enable_no_resched |
| 240 | #undef preempt_enable_no_resched_notrace |
| 241 | #undef preempt_check_resched |
| 242 | #endif |
| 243 | |
Peter Zijlstra | 8cb75e0 | 2013-11-20 12:22:37 +0100 | [diff] [blame] | 244 | #define preempt_set_need_resched() \ |
| 245 | do { \ |
| 246 | set_preempt_need_resched(); \ |
| 247 | } while (0) |
| 248 | #define preempt_fold_need_resched() \ |
| 249 | do { \ |
| 250 | if (tif_need_resched()) \ |
| 251 | set_preempt_need_resched(); \ |
| 252 | } while (0) |
Peter Zijlstra | 8cb75e0 | 2013-11-20 12:22:37 +0100 | [diff] [blame] | 253 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 254 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 255 | |
| 256 | struct preempt_notifier; |
| 257 | |
| 258 | /** |
| 259 | * preempt_ops - notifiers called when a task is preempted and rescheduled |
| 260 | * @sched_in: we're about to be rescheduled: |
| 261 | * notifier: struct preempt_notifier for the task being scheduled |
| 262 | * cpu: cpu we're scheduled on |
| 263 | * @sched_out: we've just been preempted |
| 264 | * notifier: struct preempt_notifier for the task being preempted |
| 265 | * next: the task that's kicking us out |
Tejun Heo | 8592e64 | 2009-12-02 12:56:46 +0900 | [diff] [blame] | 266 | * |
| 267 | * Please note that sched_in and out are called under different |
| 268 | * contexts. sched_out is called with rq lock held and irq disabled |
| 269 | * while sched_in is called without rq lock and irq enabled. This |
| 270 | * difference is intentional and depended upon by its users. |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 271 | */ |
| 272 | struct preempt_ops { |
| 273 | void (*sched_in)(struct preempt_notifier *notifier, int cpu); |
| 274 | void (*sched_out)(struct preempt_notifier *notifier, |
| 275 | struct task_struct *next); |
| 276 | }; |
| 277 | |
| 278 | /** |
| 279 | * preempt_notifier - key for installing preemption notifiers |
| 280 | * @link: internal use |
| 281 | * @ops: defines the notifier functions to be called |
| 282 | * |
| 283 | * Usually used in conjunction with container_of(). |
| 284 | */ |
| 285 | struct preempt_notifier { |
| 286 | struct hlist_node link; |
| 287 | struct preempt_ops *ops; |
| 288 | }; |
| 289 | |
| 290 | void preempt_notifier_register(struct preempt_notifier *notifier); |
| 291 | void preempt_notifier_unregister(struct preempt_notifier *notifier); |
| 292 | |
| 293 | static inline void preempt_notifier_init(struct preempt_notifier *notifier, |
| 294 | struct preempt_ops *ops) |
| 295 | { |
| 296 | INIT_HLIST_NODE(¬ifier->link); |
| 297 | notifier->ops = ops; |
| 298 | } |
| 299 | |
| 300 | #endif |
| 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | #endif /* __LINUX_PREEMPT_H */ |