Skip to contentSkip to conversation

lockdep: Fix wait context check on softirq for PREEMPT_RT

PATCH Superseded Ryo Takakura 2 participants 3 messages Dec 27, 2024 – Jan 7, 2025 Real-time Linux (PREEMPT_RT)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lockdep: Fix wait context check on softirq for PREEMPT_RT

Since commit 0c1d7a2c2d32 ("lockdep: Remove softirq accounting on PREEMPT_RT."), the wait context test for mutex usage within "in softirq context" fails as it references @softirq_context.

[    0.184549]   | wait context tests |
[    0.184549]   --------------------------------------------------------------------------
[    0.184549]                                  | rcu  | raw  | spin |mutex |
[    0.184549]   --------------------------------------------------------------------------
[    0.184550]                in hardirq context:  ok  |  ok  |  ok  |  ok  |
[    0.185083] in hardirq context (not threaded):  ok  |  ok  |  ok  |  ok  |
[    0.185606]                in softirq context:  ok  |  ok  |  ok  |FAILED|

As a fix, add lockdep map for BH disabled section. This fixes the issue by letting us catch cases when local_bh_disable() gets called with preemption disabled where local_lock doesn't get acquired. In the case of "in softirq context" selftest, local_bh_disable() was being called with preemption disable as it's early in the boot.

---

Hi!

This version fixes the issue by adding lockdep map for BH disabled section. Thanks Boqun for the suggestion!

Changes since v1:
https://lore.kernel.org/lkml/[email protected]/

- Fix the issue by adding lockdep map for BH disabled section. This let us catch mutex usage within "in softirq context" selftest as it disables BH section.

Also, may I add Boqun as <suggested-by> tag?

---
 include/linux/bottom_half.h |  8 ++++++++
 kernel/softirq.c            | 12 ++++++++++++
 2 files changed, 20 insertions(+)
include/linux/bottom_half.h +8 -0
index fc53e0ad5..f77dc746c 100644
@@ -4,6 +4,7 @@
 
 #include <linux/instruction_pointer.h>
 #include <linux/preempt.h>
+#include <linux/lockdep.h>
 
 #if defined(CONFIG_PREEMPT_RT) || defined(CONFIG_TRACE_IRQFLAGS)
 extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
@@ -15,8 +16,13 @@ static __always_inline void __local_bh_disable_ip(unsigned long ip, unsigned int
 }
 #endif
 
+#ifdef CONFIG_LOCKDEP
+extern struct lockdep_map bh_lock_map;
+#endif
+
 static inline void local_bh_disable(void)
 {
+	lock_map_acquire_read(&bh_lock_map);
 	__local_bh_disable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
 }
 
@@ -25,11 +31,13 @@ extern void __local_bh_enable_ip(unsigned long ip, unsigned int cnt);
 
 static inline void local_bh_enable_ip(unsigned long ip)
 {
+	lock_map_release(&bh_lock_map);
 	__local_bh_enable_ip(ip, SOFTIRQ_DISABLE_OFFSET);
 }
 
 static inline void local_bh_enable(void)
 {
+	lock_map_release(&bh_lock_map);
 	__local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
 }
 
kernel/softirq.c +12 -0
index 4dae6ac2e..a8211ba9a 100644
@@ -1073,3 +1073,15 @@ unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
 {
 	return from;
 }
+
+#ifdef CONFIG_LOCKDEP
+static struct lock_class_key bh_lock_key;
+struct lockdep_map bh_lock_map = {
+	.name = "local_bh",
+	.key = &bh_lock_key,
+	.wait_type_outer = LD_WAIT_FREE,
+	.wait_type_inner = LD_WAIT_CONFIG, /* PREEMPT_RT makes BH preemptible. */
+	.lock_type = LD_LOCK_PERCPU,
+};
+EXPORT_SYMBOL_GPL(bh_lock_map);
+#endif
Signature · 1 line
2.34.1
#1 lore raw
Quoted 90 lines · Ryo Takakura

Since commit 0c1d7a2c2d32 ("lockdep: Remove softirq accounting on PREEMPT_RT."), the wait context test for mutex usage within "in softirq context" fails as it references @softirq_context.

[    0.184549]   | wait context tests |
[    0.184549]   --------------------------------------------------------------------------
[    0.184549]                                  | rcu  | raw  | spin |mutex |
[    0.184549]   --------------------------------------------------------------------------
[    0.184550]                in hardirq context:  ok  |  ok  |  ok  |  ok  |
[    0.185083] in hardirq context (not threaded):  ok  |  ok  |  ok  |  ok  |
[    0.185606]                in softirq context:  ok  |  ok  |  ok  |FAILED|

As a fix, add lockdep map for BH disabled section. This fixes the issue by letting us catch cases when local_bh_disable() gets called with preemption disabled where local_lock doesn't get acquired. In the case of "in softirq context" selftest, local_bh_disable() was being called with preemption disable as it's early in the boot.

---

Hi!

This version fixes the issue by adding lockdep map for BH disabled section. Thanks Boqun for the suggestion!

Changes since v1:
https://lore.kernel.org/lkml/[email protected]/

- Fix the issue by adding lockdep map for BH disabled section. This let us catch mutex usage within "in softirq context" selftest as it disables BH section.

Also, may I add Boqun as <suggested-by> tag?

---
  include/linux/bottom_half.h |  8 ++++++++
  kernel/softirq.c            | 12 ++++++++++++
  2 files changed, 20 insertions(+)
include/linux/bottom_half.h +8 -0
index fc53e0ad5..f77dc746c 100644
@@ -4,6 +4,7 @@
 
  #include <linux/instruction_pointer.h>
  #include <linux/preempt.h>
+#include <linux/lockdep.h>
 
  #if defined(CONFIG_PREEMPT_RT) || defined(CONFIG_TRACE_IRQFLAGS)
  extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
@@ -15,8 +16,13 @@ static __always_inline void __local_bh_disable_ip(unsigned long ip, unsigned int
  }
  #endif
 
+#ifdef CONFIG_LOCKDEP
+extern struct lockdep_map bh_lock_map;
+#endif
+
  static inline void local_bh_disable(void)
  {
+	lock_map_acquire_read(&bh_lock_map);
  	__local_bh_disable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  }
 
@@ -25,11 +31,13 @@ extern void __local_bh_enable_ip(unsigned long ip, unsigned int cnt);
 
  static inline void local_bh_enable_ip(unsigned long ip)
  {
+	lock_map_release(&bh_lock_map);
  	__local_bh_enable_ip(ip, SOFTIRQ_DISABLE_OFFSET);
  }
 
  static inline void local_bh_enable(void)
  {
+	lock_map_release(&bh_lock_map);
  	__local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  }
 
kernel/softirq.c +2 -0
index 4dae6ac2e..a8211ba9a 100644
@@ -1073,3 +1073,15 @@ unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
  {
  	return from;
  }
+
+#ifdef CONFIG_LOCKDEP

kernel/softirq.c unsigned int __weak arch_dynirq_lower_bound(unsigned int from) · line 1087

Quoted 10 lines
kernel/softirq.c +10 -0
+static struct lock_class_key bh_lock_key;
+struct lockdep_map bh_lock_map = {
+	.name = "local_bh",
+	.key = &bh_lock_key,
+	.wait_type_outer = LD_WAIT_FREE,
+	.wait_type_inner = LD_WAIT_CONFIG, /* PREEMPT_RT makes BH preemptible. */
+	.lock_type = LD_LOCK_PERCPU,
+};
+EXPORT_SYMBOL_GPL(bh_lock_map);
+#endif

All the locking code use CONFIG_DEBUG_LOCK_ALLOC to determine if lockdep_map should be used. Should you also use CONFIG_DEBUG_LOCK_ALLOC instead of CONFIG_LOCKDEP to be consistent with the others?

Cheers,
Longman

#2 lore raw

Hi Long, and happy new year to everyone!

Quoted 61 lines · Waiman Long
Quoted 60 lines · Ryo Takakura
  include/linux/bottom_half.h |  8 ++++++++
  kernel/softirq.c            | 12 ++++++++++++
  2 files changed, 20 insertions(+)
include/linux/bottom_half.h +8 -0
index fc53e0ad5..f77dc746c 100644
@@ -4,6 +4,7 @@
 
  #include <linux/instruction_pointer.h>
  #include <linux/preempt.h>
+#include <linux/lockdep.h>
 
  #if defined(CONFIG_PREEMPT_RT) || defined(CONFIG_TRACE_IRQFLAGS)
  extern void __local_bh_disable_ip(unsigned long ip, unsigned int cnt);
@@ -15,8 +16,13 @@ static __always_inline void __local_bh_disable_ip(unsigned long ip, unsigned int
  }
  #endif
 
+#ifdef CONFIG_LOCKDEP
+extern struct lockdep_map bh_lock_map;
+#endif
+
  static inline void local_bh_disable(void)
  {
+	lock_map_acquire_read(&bh_lock_map);
  	__local_bh_disable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  }
 
@@ -25,11 +31,13 @@ extern void __local_bh_enable_ip(unsigned long ip, unsigned int cnt);
 
  static inline void local_bh_enable_ip(unsigned long ip)
  {
+	lock_map_release(&bh_lock_map);
  	__local_bh_enable_ip(ip, SOFTIRQ_DISABLE_OFFSET);
  }
 
  static inline void local_bh_enable(void)
  {
+	lock_map_release(&bh_lock_map);
  	__local_bh_enable_ip(_THIS_IP_, SOFTIRQ_DISABLE_OFFSET);
  }
 
kernel/softirq.c +8 -0
index 4dae6ac2e..a8211ba9a 100644
@@ -1073,3 +1073,15 @@ unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
  {
  	return from;
  }
+
+#ifdef CONFIG_LOCKDEP
+static struct lock_class_key bh_lock_key;
+struct lockdep_map bh_lock_map = {
+	.name = "local_bh",
+	.key = &bh_lock_key,
+	.wait_type_outer = LD_WAIT_FREE,
+	.wait_type_inner = LD_WAIT_CONFIG, /* PREEMPT_RT makes BH preemptible. */
Quoted 8 lines

kernel/softirq.c unsigned int __weak arch_dynirq_lower_bound(unsigned int from) · line 1087

Quoted 4 lines
kernel/softirq.c +4 -0
+	.lock_type = LD_LOCK_PERCPU,
+};
+EXPORT_SYMBOL_GPL(bh_lock_map);
+#endif

All the locking code use CONFIG_DEBUG_LOCK_ALLOC to determine if lockdep_map should be used. Should you also use CONFIG_DEBUG_LOCK_ALLOC instead of CONFIG_LOCKDEP to be consistent with the others?

I see, I wasn't sure which CONFIG should be use here. Thanks for pointing it out!
I'll use CONFIG_DEBUG_LOCK_ALLOC for the next version.

Quoted 2 lines

Cheers,
Longman

Sincerely,
Ryo Takakura