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 | /* |
| 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 Zijlstra | a787870 | 2013-08-14 14:55:40 +0200 | [diff] [blame] | 18 | #include <asm/preempt.h> |
Peter Zijlstra | f27dde8 | 2013-08-14 14:55:31 +0200 | [diff] [blame] | 19 | |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame^] | 20 | /* |
| 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 Rostedt | 6cd8a4b | 2008-05-12 21:20:42 +0200 | [diff] [blame] | 131 | #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_PREEMPT_TRACER) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 132 | extern void preempt_count_add(int val); |
| 133 | extern void preempt_count_sub(int val); |
| 134 | #define preempt_count_dec_and_test() ({ preempt_count_sub(1); should_resched(); }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | #else |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 136 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | #endif |
| 140 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 141 | #define __preempt_count_inc() __preempt_count_add(1) |
| 142 | #define __preempt_count_dec() __preempt_count_sub(1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 144 | #define preempt_count_inc() preempt_count_add(1) |
| 145 | #define preempt_count_dec() preempt_count_sub(1) |
Frederic Weisbecker | bdd4e85 | 2011-06-08 01:13:27 +0200 | [diff] [blame] | 146 | |
| 147 | #ifdef CONFIG_PREEMPT_COUNT |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | #define preempt_disable() \ |
| 150 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 151 | preempt_count_inc(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | barrier(); \ |
| 153 | } while (0) |
| 154 | |
Thomas Gleixner | ba74c14 | 2011-03-21 13:32:17 +0100 | [diff] [blame] | 155 | #define sched_preempt_enable_no_resched() \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | do { \ |
| 157 | barrier(); \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 158 | preempt_count_dec(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } while (0) |
| 160 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 161 | #define preempt_enable_no_resched() sched_preempt_enable_no_resched() |
Thomas Gleixner | ba74c14 | 2011-03-21 13:32:17 +0100 | [diff] [blame] | 162 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 163 | #ifdef CONFIG_PREEMPT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | #define preempt_enable() \ |
| 165 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 166 | barrier(); \ |
| 167 | if (unlikely(preempt_count_dec_and_test())) \ |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 168 | __preempt_schedule(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } while (0) |
| 170 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 171 | #define preempt_check_resched() \ |
| 172 | do { \ |
| 173 | if (should_resched()) \ |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 174 | __preempt_schedule(); \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 175 | } while (0) |
| 176 | |
| 177 | #else |
Peter Zijlstra | 62b94a0 | 2013-11-20 16:52:19 +0100 | [diff] [blame] | 178 | #define preempt_enable() \ |
| 179 | do { \ |
| 180 | barrier(); \ |
| 181 | preempt_count_dec(); \ |
| 182 | } while (0) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 183 | #define preempt_check_resched() do { } while (0) |
| 184 | #endif |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 185 | |
| 186 | #define preempt_disable_notrace() \ |
| 187 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 188 | __preempt_count_inc(); \ |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 189 | barrier(); \ |
| 190 | } while (0) |
| 191 | |
| 192 | #define preempt_enable_no_resched_notrace() \ |
| 193 | do { \ |
| 194 | barrier(); \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 195 | __preempt_count_dec(); \ |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 196 | } while (0) |
| 197 | |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 198 | #ifdef CONFIG_PREEMPT |
| 199 | |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 200 | #ifndef CONFIG_CONTEXT_TRACKING |
| 201 | #define __preempt_schedule_context() __preempt_schedule() |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 202 | #endif |
| 203 | |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 204 | #define preempt_enable_notrace() \ |
| 205 | do { \ |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 206 | barrier(); \ |
| 207 | if (unlikely(__preempt_count_dec_and_test())) \ |
Peter Zijlstra | 1a338ac | 2013-08-14 14:51:00 +0200 | [diff] [blame] | 208 | __preempt_schedule_context(); \ |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 209 | } while (0) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 210 | #else |
Peter Zijlstra | 62b94a0 | 2013-11-20 16:52:19 +0100 | [diff] [blame] | 211 | #define preempt_enable_notrace() \ |
| 212 | do { \ |
| 213 | barrier(); \ |
| 214 | __preempt_count_dec(); \ |
| 215 | } while (0) |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 216 | #endif |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 217 | |
Frederic Weisbecker | bdd4e85 | 2011-06-08 01:13:27 +0200 | [diff] [blame] | 218 | #else /* !CONFIG_PREEMPT_COUNT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 220 | /* |
| 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 Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 226 | #define preempt_disable() barrier() |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 227 | #define sched_preempt_enable_no_resched() barrier() |
Peter Zijlstra | bdb4380 | 2013-09-10 12:15:23 +0200 | [diff] [blame] | 228 | #define preempt_enable_no_resched() barrier() |
| 229 | #define preempt_enable() barrier() |
| 230 | #define preempt_check_resched() do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Linus Torvalds | 386afc9 | 2013-04-09 10:48:33 -0700 | [diff] [blame] | 232 | #define preempt_disable_notrace() barrier() |
| 233 | #define preempt_enable_no_resched_notrace() barrier() |
| 234 | #define preempt_enable_notrace() barrier() |
Steven Rostedt | 5028252 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 235 | |
Frederic Weisbecker | bdd4e85 | 2011-06-08 01:13:27 +0200 | [diff] [blame] | 236 | #endif /* CONFIG_PREEMPT_COUNT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Peter Zijlstra | 62b94a0 | 2013-11-20 16:52:19 +0100 | [diff] [blame] | 238 | #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 Zijlstra | 8cb75e0 | 2013-11-20 12:22:37 +0100 | [diff] [blame] | 248 | #define preempt_set_need_resched() \ |
| 249 | do { \ |
| 250 | set_preempt_need_resched(); \ |
| 251 | } while (0) |
| 252 | #define preempt_fold_need_resched() \ |
| 253 | do { \ |
| 254 | if (tif_need_resched()) \ |
| 255 | set_preempt_need_resched(); \ |
| 256 | } while (0) |
Peter Zijlstra | 8cb75e0 | 2013-11-20 12:22:37 +0100 | [diff] [blame] | 257 | |
Avi Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 258 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
| 259 | |
| 260 | struct 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 Heo | 8592e64 | 2009-12-02 12:56:46 +0900 | [diff] [blame] | 270 | * |
| 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 Kivity | e107be3 | 2007-07-26 13:40:43 +0200 | [diff] [blame] | 275 | */ |
| 276 | struct 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 | */ |
| 289 | struct preempt_notifier { |
| 290 | struct hlist_node link; |
| 291 | struct preempt_ops *ops; |
| 292 | }; |
| 293 | |
| 294 | void preempt_notifier_register(struct preempt_notifier *notifier); |
| 295 | void preempt_notifier_unregister(struct preempt_notifier *notifier); |
| 296 | |
| 297 | static 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | #endif /* __LINUX_PREEMPT_H */ |