Skip to content

Commit 8fd8ad5

Browse files
a-darwishPeter Zijlstra
authored andcommitted
lockdep: Add preemption enabled/disabled assertion APIs
Asserting that preemption is enabled or disabled is a critical sanity check. Developers are usually reluctant to add such a check in a fastpath as reading the preemption count can be costly. Extend the lockdep API with macros asserting that preemption is disabled or enabled. If lockdep is disabled, or if the underlying architecture does not support kernel preemption, this assert has no runtime overhead. References: f54bb2e ("locking/lockdep: Add IRQs disabled/enabled assertion APIs: ...") Signed-off-by: Ahmed S. Darwish <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 932e463 commit 8fd8ad5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

include/linux/lockdep.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,22 @@ do { \
549549
WARN_ON_ONCE(debug_locks && !this_cpu_read(hardirq_context)); \
550550
} while (0)
551551

552+
#define lockdep_assert_preemption_enabled() \
553+
do { \
554+
WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \
555+
debug_locks && \
556+
(preempt_count() != 0 || \
557+
!this_cpu_read(hardirqs_enabled))); \
558+
} while (0)
559+
560+
#define lockdep_assert_preemption_disabled() \
561+
do { \
562+
WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT) && \
563+
debug_locks && \
564+
(preempt_count() == 0 && \
565+
this_cpu_read(hardirqs_enabled))); \
566+
} while (0)
567+
552568
#else
553569
# define might_lock(lock) do { } while (0)
554570
# define might_lock_read(lock) do { } while (0)
@@ -557,6 +573,9 @@ do { \
557573
# define lockdep_assert_irqs_enabled() do { } while (0)
558574
# define lockdep_assert_irqs_disabled() do { } while (0)
559575
# define lockdep_assert_in_irq() do { } while (0)
576+
577+
# define lockdep_assert_preemption_enabled() do { } while (0)
578+
# define lockdep_assert_preemption_disabled() do { } while (0)
560579
#endif
561580

562581
#ifdef CONFIG_PROVE_RAW_LOCK_NESTING

lib/Kconfig.debug

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@ config PROVE_LOCKING
11171117
select DEBUG_RWSEMS
11181118
select DEBUG_WW_MUTEX_SLOWPATH
11191119
select DEBUG_LOCK_ALLOC
1120+
select PREEMPT_COUNT if !ARCH_NO_PREEMPT
11201121
select TRACE_IRQFLAGS
11211122
default n
11221123
help

0 commit comments

Comments
 (0)