1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __LINUX_PREEMPT_H 3 #define __LINUX_PREEMPT_H 4 5 /* 6 * include/linux/preempt.h - macros for accessing and manipulating 7 * preempt_count (used for kernel preemption, interrupt count, etc.) 8 */ 9 10 #include <linux/linkage.h> 11 #include <linux/list.h> 12 13 /* 14 * We put the hardirq and softirq counter into the preemption 15 * counter. The bitmask has the following meaning: 16 * 17 * - bits 0-7 are the preemption count (max preemption depth: 256) 18 * - bits 8-15 are the softirq count (max # of softirqs: 256) 19 * 20 * The hardirq count could in theory be the same as the number of 21 * interrupts in the system, but we run all interrupt handlers with 22 * interrupts disabled, so we cannot have nesting interrupts. Though 23 * there are a few palaeontologic drivers which reenable interrupts in 24 * the handler, so we need more than one bit here. 25 * 26 * PREEMPT_MASK: 0x000000ff 27 * SOFTIRQ_MASK: 0x0000ff00 28 * HARDIRQ_MASK: 0x000f0000 29 * NMI_MASK: 0x00f00000 30 * PREEMPT_NEED_RESCHED: 0x80000000 31 */ 32 #define PREEMPT_BITS 8 33 #define SOFTIRQ_BITS 8 34 #define HARDIRQ_BITS 4 35 #define NMI_BITS 4 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_DISABLED (PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED) 57 58 /* 59 * Disable preemption until the scheduler is running -- use an unconditional 60 * value so that it also works on !PREEMPT_COUNT kernels. 61 * 62 * Reset by start_kernel()->sched_init()->init_idle()->init_idle_preempt_count(). 63 */ 64 #define INIT_PREEMPT_COUNT PREEMPT_OFFSET 65 66 /* 67 * Initial preempt_count value; reflects the preempt_count schedule invariant 68 * which states that during context switches: 69 * 70 * preempt_count() == 2*PREEMPT_DISABLE_OFFSET 71 * 72 * Note: PREEMPT_DISABLE_OFFSET is 0 for !PREEMPT_COUNT kernels. 73 * Note: See finish_task_switch(). 74 */ 75 #define FORK_PREEMPT_COUNT (2*PREEMPT_DISABLE_OFFSET + PREEMPT_ENABLED) 76 77 /* preempt_count() and related functions, depends on PREEMPT_NEED_RESCHED */ 78 #include <asm/preempt.h> 79 80 /** 81 * interrupt_context_level - return interrupt context level 82 * 83 * Returns the current interrupt context level. 84 * 0 - normal context 85 * 1 - softirq context 86 * 2 - hardirq context 87 * 3 - NMI context 88 */ interrupt_context_level(void)89 static __always_inline unsigned char interrupt_context_level(void) 90 { 91 unsigned long pc = preempt_count(); 92 unsigned char level = 0; 93 94 level += !!(pc & (NMI_MASK)); 95 level += !!(pc & (NMI_MASK | HARDIRQ_MASK)); 96 level += !!(pc & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)); 97 98 return level; 99 } 100 101 /* 102 * These macro definitions avoid redundant invocations of preempt_count() 103 * because such invocations would result in redundant loads given that 104 * preempt_count() is commonly implemented with READ_ONCE(). 105 */ 106 107 #define nmi_count() (preempt_count() & NMI_MASK) 108 #define hardirq_count() (preempt_count() & HARDIRQ_MASK) 109 #ifdef CONFIG_PREEMPT_RT 110 # define softirq_count() (current->softirq_disable_cnt & SOFTIRQ_MASK) 111 # define irq_count() ((preempt_count() & (NMI_MASK | HARDIRQ_MASK)) | softirq_count()) 112 #else 113 # define softirq_count() (preempt_count() & SOFTIRQ_MASK) 114 # define irq_count() (preempt_count() & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_MASK)) 115 #endif 116 117 /* 118 * Macros to retrieve the current execution context: 119 * 120 * in_nmi() - We're in NMI context 121 * in_hardirq() - We're in hard IRQ context 122 * in_serving_softirq() - We're in softirq context 123 * in_task() - We're in task context 124 */ 125 #define in_nmi() (nmi_count()) 126 #define in_hardirq() (hardirq_count()) 127 #define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET) 128 #ifdef CONFIG_PREEMPT_RT 129 # define in_task() (!((preempt_count() & (NMI_MASK | HARDIRQ_MASK)) | in_serving_softirq())) 130 #else 131 # define in_task() (!(preempt_count() & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET))) 132 #endif 133 134 /* 135 * The following macros are deprecated and should not be used in new code: 136 * in_irq() - Obsolete version of in_hardirq() 137 * in_softirq() - We have BH disabled, or are processing softirqs 138 * in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled 139 */ 140 #define in_irq() (hardirq_count()) 141 #define in_softirq() (softirq_count()) 142 #define in_interrupt() (irq_count()) 143 144 /* 145 * The preempt_count offset after preempt_disable(); 146 */ 147 #if defined(CONFIG_PREEMPT_COUNT) 148 # define PREEMPT_DISABLE_OFFSET PREEMPT_OFFSET 149 #else 150 # define PREEMPT_DISABLE_OFFSET 0 151 #endif 152 153 /* 154 * The preempt_count offset after spin_lock() 155 */ 156 #if !defined(CONFIG_PREEMPT_RT) 157 #define PREEMPT_LOCK_OFFSET PREEMPT_DISABLE_OFFSET 158 #else 159 #define PREEMPT_LOCK_OFFSET 0 160 #endif 161 162 /* 163 * The preempt_count offset needed for things like: 164 * 165 * spin_lock_bh() 166 * 167 * Which need to disable both preemption (CONFIG_PREEMPT_COUNT) and 168 * softirqs, such that unlock sequences of: 169 * 170 * spin_unlock(); 171 * local_bh_enable(); 172 * 173 * Work as expected. 174 */ 175 #define SOFTIRQ_LOCK_OFFSET (SOFTIRQ_DISABLE_OFFSET + PREEMPT_LOCK_OFFSET) 176 177 /* 178 * Are we running in atomic context? WARNING: this macro cannot 179 * always detect atomic context; in particular, it cannot know about 180 * held spinlocks in non-preemptible kernels. Thus it should not be 181 * used in the general case to determine whether sleeping is possible. 182 * Do not use in_atomic() in driver code. 183 */ 184 #define in_atomic() (preempt_count() != 0) 185 186 /* 187 * Check whether we were atomic before we did preempt_disable(): 188 * (used by the scheduler) 189 */ 190 #define in_atomic_preempt_off() (preempt_count() != PREEMPT_DISABLE_OFFSET) 191 192 #if defined(CONFIG_DEBUG_PREEMPT) || defined(CONFIG_TRACE_PREEMPT_TOGGLE) 193 extern void preempt_count_add(int val); 194 extern void preempt_count_sub(int val); 195 #define preempt_count_dec_and_test() \ 196 ({ preempt_count_sub(1); should_resched(0); }) 197 #else 198 #define preempt_count_add(val) __preempt_count_add(val) 199 #define preempt_count_sub(val) __preempt_count_sub(val) 200 #define preempt_count_dec_and_test() __preempt_count_dec_and_test() 201 #endif 202 203 #define __preempt_count_inc() __preempt_count_add(1) 204 #define __preempt_count_dec() __preempt_count_sub(1) 205 206 #define preempt_count_inc() preempt_count_add(1) 207 #define preempt_count_dec() preempt_count_sub(1) 208 209 #ifdef CONFIG_PREEMPT_COUNT 210 211 #define preempt_disable() \ 212 do { \ 213 preempt_count_inc(); \ 214 barrier(); \ 215 } while (0) 216 217 #define sched_preempt_enable_no_resched() \ 218 do { \ 219 barrier(); \ 220 preempt_count_dec(); \ 221 } while (0) 222 223 #define preempt_enable_no_resched() sched_preempt_enable_no_resched() 224 225 #define preemptible() (preempt_count() == 0 && !irqs_disabled()) 226 227 #ifdef CONFIG_PREEMPTION 228 #define preempt_enable() \ 229 do { \ 230 barrier(); \ 231 if (unlikely(preempt_count_dec_and_test())) \ 232 __preempt_schedule(); \ 233 } while (0) 234 235 #define preempt_enable_notrace() \ 236 do { \ 237 barrier(); \ 238 if (unlikely(__preempt_count_dec_and_test())) \ 239 __preempt_schedule_notrace(); \ 240 } while (0) 241 242 #define preempt_check_resched() \ 243 do { \ 244 if (should_resched(0)) \ 245 __preempt_schedule(); \ 246 } while (0) 247 248 #else /* !CONFIG_PREEMPTION */ 249 #define preempt_enable() \ 250 do { \ 251 barrier(); \ 252 preempt_count_dec(); \ 253 } while (0) 254 255 #define preempt_enable_notrace() \ 256 do { \ 257 barrier(); \ 258 __preempt_count_dec(); \ 259 } while (0) 260 261 #define preempt_check_resched() do { } while (0) 262 #endif /* CONFIG_PREEMPTION */ 263 264 #define preempt_disable_notrace() \ 265 do { \ 266 __preempt_count_inc(); \ 267 barrier(); \ 268 } while (0) 269 270 #define preempt_enable_no_resched_notrace() \ 271 do { \ 272 barrier(); \ 273 __preempt_count_dec(); \ 274 } while (0) 275 276 #else /* !CONFIG_PREEMPT_COUNT */ 277 278 /* 279 * Even if we don't have any preemption, we need preempt disable/enable 280 * to be barriers, so that we don't have things like get_user/put_user 281 * that can cause faults and scheduling migrate into our preempt-protected 282 * region. 283 */ 284 #define preempt_disable() barrier() 285 #define sched_preempt_enable_no_resched() barrier() 286 #define preempt_enable_no_resched() barrier() 287 #define preempt_enable() barrier() 288 #define preempt_check_resched() do { } while (0) 289 290 #define preempt_disable_notrace() barrier() 291 #define preempt_enable_no_resched_notrace() barrier() 292 #define preempt_enable_notrace() barrier() 293 #define preemptible() 0 294 295 #endif /* CONFIG_PREEMPT_COUNT */ 296 297 #ifdef MODULE 298 /* 299 * Modules have no business playing preemption tricks. 300 */ 301 #undef sched_preempt_enable_no_resched 302 #undef preempt_enable_no_resched 303 #undef preempt_enable_no_resched_notrace 304 #undef preempt_check_resched 305 #endif 306 307 #define preempt_set_need_resched() \ 308 do { \ 309 set_preempt_need_resched(); \ 310 } while (0) 311 #define preempt_fold_need_resched() \ 312 do { \ 313 if (tif_need_resched()) \ 314 set_preempt_need_resched(); \ 315 } while (0) 316 317 #ifdef CONFIG_PREEMPT_NOTIFIERS 318 319 struct preempt_notifier; 320 321 /** 322 * preempt_ops - notifiers called when a task is preempted and rescheduled 323 * @sched_in: we're about to be rescheduled: 324 * notifier: struct preempt_notifier for the task being scheduled 325 * cpu: cpu we're scheduled on 326 * @sched_out: we've just been preempted 327 * notifier: struct preempt_notifier for the task being preempted 328 * next: the task that's kicking us out 329 * 330 * Please note that sched_in and out are called under different 331 * contexts. sched_out is called with rq lock held and irq disabled 332 * while sched_in is called without rq lock and irq enabled. This 333 * difference is intentional and depended upon by its users. 334 */ 335 struct preempt_ops { 336 void (*sched_in)(struct preempt_notifier *notifier, int cpu); 337 void (*sched_out)(struct preempt_notifier *notifier, 338 struct task_struct *next); 339 }; 340 341 /** 342 * preempt_notifier - key for installing preemption notifiers 343 * @link: internal use 344 * @ops: defines the notifier functions to be called 345 * 346 * Usually used in conjunction with container_of(). 347 */ 348 struct preempt_notifier { 349 struct hlist_node link; 350 struct preempt_ops *ops; 351 }; 352 353 void preempt_notifier_inc(void); 354 void preempt_notifier_dec(void); 355 void preempt_notifier_register(struct preempt_notifier *notifier); 356 void preempt_notifier_unregister(struct preempt_notifier *notifier); 357 preempt_notifier_init(struct preempt_notifier * notifier,struct preempt_ops * ops)358 static inline void preempt_notifier_init(struct preempt_notifier *notifier, 359 struct preempt_ops *ops) 360 { 361 INIT_HLIST_NODE(¬ifier->link); 362 notifier->ops = ops; 363 } 364 365 #endif 366 367 #ifdef CONFIG_SMP 368 369 /* 370 * Migrate-Disable and why it is undesired. 371 * 372 * When a preempted task becomes elegible to run under the ideal model (IOW it 373 * becomes one of the M highest priority tasks), it might still have to wait 374 * for the preemptee's migrate_disable() section to complete. Thereby suffering 375 * a reduction in bandwidth in the exact duration of the migrate_disable() 376 * section. 377 * 378 * Per this argument, the change from preempt_disable() to migrate_disable() 379 * gets us: 380 * 381 * - a higher priority tasks gains reduced wake-up latency; with preempt_disable() 382 * it would have had to wait for the lower priority task. 383 * 384 * - a lower priority tasks; which under preempt_disable() could've instantly 385 * migrated away when another CPU becomes available, is now constrained 386 * by the ability to push the higher priority task away, which might itself be 387 * in a migrate_disable() section, reducing it's available bandwidth. 388 * 389 * IOW it trades latency / moves the interference term, but it stays in the 390 * system, and as long as it remains unbounded, the system is not fully 391 * deterministic. 392 * 393 * 394 * The reason we have it anyway. 395 * 396 * PREEMPT_RT breaks a number of assumptions traditionally held. By forcing a 397 * number of primitives into becoming preemptible, they would also allow 398 * migration. This turns out to break a bunch of per-cpu usage. To this end, 399 * all these primitives employ migirate_disable() to restore this implicit 400 * assumption. 401 * 402 * This is a 'temporary' work-around at best. The correct solution is getting 403 * rid of the above assumptions and reworking the code to employ explicit 404 * per-cpu locking or short preempt-disable regions. 405 * 406 * The end goal must be to get rid of migrate_disable(), alternatively we need 407 * a schedulability theory that does not depend on abritrary migration. 408 * 409 * 410 * Notes on the implementation. 411 * 412 * The implementation is particularly tricky since existing code patterns 413 * dictate neither migrate_disable() nor migrate_enable() is allowed to block. 414 * This means that it cannot use cpus_read_lock() to serialize against hotplug, 415 * nor can it easily migrate itself into a pending affinity mask change on 416 * migrate_enable(). 417 * 418 * 419 * Note: even non-work-conserving schedulers like semi-partitioned depends on 420 * migration, so migrate_disable() is not only a problem for 421 * work-conserving schedulers. 422 * 423 */ 424 extern void migrate_disable(void); 425 extern void migrate_enable(void); 426 427 #else 428 migrate_disable(void)429 static inline void migrate_disable(void) { } migrate_enable(void)430 static inline void migrate_enable(void) { } 431 432 #endif /* CONFIG_SMP */ 433 434 #endif /* __LINUX_PREEMPT_H */ 435