Skip to content

Commit 555bb4c

Browse files
committed
preempt: Provide preempt_[dis|en]able_nested()
On PREEMPT_RT enabled kernels, spinlocks and rwlocks are neither disabling preemption nor interrupts. Though there are a few places which depend on the implicit preemption/interrupt disable of those locks, e.g. seqcount write sections, per CPU statistics updates etc. To avoid sprinkling CONFIG_PREEMPT_RT conditionals all over the place, add preempt_disable_nested() and preempt_enable_nested() which should be descriptive enough. Add a lockdep assertion for the !PREEMPT_RT case to catch callers which do not have preemption disabled. Suggested-by: Linus Torvalds <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Acked-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 521a547 commit 555bb4c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

include/linux/preempt.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,46 @@ static inline void migrate_enable(void) { }
421421

422422
#endif /* CONFIG_SMP */
423423

424+
/**
425+
* preempt_disable_nested - Disable preemption inside a normally preempt disabled section
426+
*
427+
* Use for code which requires preemption protection inside a critical
428+
* section which has preemption disabled implicitly on non-PREEMPT_RT
429+
* enabled kernels, by e.g.:
430+
* - holding a spinlock/rwlock
431+
* - soft interrupt context
432+
* - regular interrupt handlers
433+
*
434+
* On PREEMPT_RT enabled kernels spinlock/rwlock held sections, soft
435+
* interrupt context and regular interrupt handlers are preemptible and
436+
* only prevent migration. preempt_disable_nested() ensures that preemption
437+
* is disabled for cases which require CPU local serialization even on
438+
* PREEMPT_RT. For non-PREEMPT_RT kernels this is a NOP.
439+
*
440+
* The use cases are code sequences which are not serialized by a
441+
* particular lock instance, e.g.:
442+
* - seqcount write side critical sections where the seqcount is not
443+
* associated to a particular lock and therefore the automatic
444+
* protection mechanism does not work. This prevents a live lock
445+
* against a preempting high priority reader.
446+
* - RMW per CPU variable updates like vmstat.
447+
*/
448+
/* Macro to avoid header recursion hell vs. lockdep */
449+
#define preempt_disable_nested() \
450+
do { \
451+
if (IS_ENABLED(CONFIG_PREEMPT_RT)) \
452+
preempt_disable(); \
453+
else \
454+
lockdep_assert_preemption_disabled(); \
455+
} while (0)
456+
457+
/**
458+
* preempt_enable_nested - Undo the effect of preempt_disable_nested()
459+
*/
460+
static __always_inline void preempt_enable_nested(void)
461+
{
462+
if (IS_ENABLED(CONFIG_PREEMPT_RT))
463+
preempt_enable();
464+
}
465+
424466
#endif /* __LINUX_PREEMPT_H */

0 commit comments

Comments
 (0)