Skip to contentSkip to conversation

locking/lockdep: make chain-hlocks average depth configurable

PATCH Superseded Muhammad Bilal 2 participants 3 messages Aug 3 – Aug 4

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 locking/lockdep: make chain-hlocks average depth configurable

The static chain_hlocks[] pool is sized as:

MAX_LOCKDEP_CHAIN_HLOCKS = MAX_LOCKDEP_CHAINS * AVG_LOCKDEP_CHAIN_DEPTH

MAX_LOCKDEP_CHAINS is already tunable via CONFIG_LOCKDEP_CHAINS_BITS, but AVG_LOCKDEP_CHAIN_DEPTH is hardcoded to 5 and has no Kconfig knob. Workloads that build unusually deep individual lock chains -- rather than simply a large number of distinct chains -- can exhaust chain_hlocks[] and trip:

  BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!

well before MAX_LOCKDEP_CHAINS itself is anywhere near full, which silently disables lock debugging for the rest of the boot (debug_locks_off_graph_unlock()). Bumping LOCKDEP_CHAINS_BITS alone does not help in that case since the bottleneck is chain depth, not chain count.

Observed on a PREEMPT_DYNAMIC + RCU lockdep + KASAN debug build, reproducing at every boot from add_chain_cache() failing to allocate out of the static pool, hit from sched wakeup, hrtimer, and btrfs flush-workqueue paths (deep IRQ/softirq nesting stacked on top of deep filesystem/scheduler call chains).

Add CONFIG_LOCKDEP_CHAIN_DEPTH so this can be tuned like LOCKDEP_BITS/LOCKDEP_CHAINS_BITS, defaulting to 5 to preserve current behavior for everyone who isn't hitting this.

---
 kernel/locking/lockdep_internals.h |  2 +-
 lib/Kconfig.debug                  | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
kernel/locking/lockdep_internals.h +1 -1
index 0e5e6ffe91a3..ed566681f3c8 100644
@@ -121,7 +121,7 @@ enum {
 
 #define MAX_LOCKDEP_CHAINS	(1UL << MAX_LOCKDEP_CHAINS_BITS)
 
-#define AVG_LOCKDEP_CHAIN_DEPTH		5
+#define AVG_LOCKDEP_CHAIN_DEPTH		CONFIG_LOCKDEP_CHAIN_DEPTH
 #define MAX_LOCKDEP_CHAIN_HLOCKS (MAX_LOCKDEP_CHAINS * AVG_LOCKDEP_CHAIN_DEPTH)
 
 extern struct lock_chain lock_chains[];
lib/Kconfig.debug +17 -0
index 1244dcac2294..90b7f990611d 100644
@@ -1614,6 +1614,23 @@ config LOCKDEP_CHAINS_BITS
 	help
 	  Try increasing this value if you hit "BUG: MAX_LOCKDEP_CHAINS too low!" message.
 
+config LOCKDEP_CHAIN_DEPTH
+	int "Average depth for MAX_LOCKDEP_CHAIN_HLOCKS"
+	depends on LOCKDEP
+	range 3 20
+	default 5
+	help
+	  Average per-chain depth used to size the static chain_hlocks[]
+	  pool: MAX_LOCKDEP_CHAIN_HLOCKS = MAX_LOCKDEP_CHAINS *
+	  LOCKDEP_CHAIN_DEPTH.
+
+	  Workloads that build unusually deep lock chains (heavy irq/softirq
+	  nesting stacked on top of deep filesystem or scheduler call chains)
+	  can exhaust this pool and trip "BUG: MAX_LOCKDEP_CHAIN_HLOCKS too
+	  low!" well before MAX_LOCKDEP_CHAINS itself is exhausted, silently
+	  disabling lock debugging. Increase this value if you hit that
+	  message and LOCKDEP_CHAINS_BITS increases alone don't help.
+
 config LOCKDEP_STACK_TRACE_BITS
 	int "Size for MAX_STACK_TRACE_ENTRIES (as Nth power of 2)"
 	depends on LOCKDEP && !LOCKDEP_SMALL
Signature · 1 line
2.55.0
#1 lore raw

From: Zhan Xusheng <[email protected]>

Quoted 1 line · Muhammad Bilal
lib/Kconfig.debug +1 -0
+	range 3 20

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+	   Increase this value if you hit that message and LOCKDEP_CHAINS_BITS
+	   increases alone don't help.

I might be missing something, but doesn't the top of this range interact with LOCKDEP_CHAINS_BITS in a way that can fail the build?

MAX_LOCKDEP_CHAIN_HLOCKS is (1 << LOCKDEP_CHAINS_BITS) * LOCKDEP_CHAIN_DEPTH, and add_chain_cache() has:

	BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));

since lock_chain.base is a 24-bit index into chain_hlocks[]. So the product has to stay below 2^24.

LOCKDEP_CHAINS_BITS ranges up to 21, so with the new knob:

	CHAINS_BITS=21, DEPTH=8  ->  2^21 * 8 == 2^24  ->  BUILD_BUG_ON fires

i.e. at the maximum CHAINS_BITS anything above DEPTH=7 stops building. Today, with the fixed depth of 5, 2^21 * 5 stays under the limit, so this is a new combination the patch makes reachable.

What I find a little awkward is that this is exactly the path the help text points people at: someone who already raised LOCKDEP_CHAINS_BITS to 21 chasing the "too low" message and then bumps LOCKDEP_CHAIN_DEPTH as suggested gets a (fairly opaque) BUILD_BUG_ON rather than a larger pool.

Kconfig can't easily express the (1 << CHAINS_BITS) * DEPTH < 2^24 product, so would it make sense to at least spell the constraint out in the help text and note that the build fails otherwise? Capping the range for the worst case (CHAINS_BITS=21 => DEPTH <= 7) would be safe but would needlessly restrict the common CHAINS_BITS=16 case, where much larger depths are fine.

Thanks,
Zhan Xusheng

#2 lore raw

Good catch! Just send v2 with fix.

Thanks,
Muhammad Bilal

Quoted 41 lines · Zhan Xusheng

From: Zhan Xusheng <[email protected]>

Quoted 1 line · Muhammad Bilal

+ range 3 20

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+        Increase this value if you hit that message and LOCKDEP_CHAINS_BITS
+        increases alone don't help.

I might be missing something, but doesn't the top of this range interact with LOCKDEP_CHAINS_BITS in a way that can fail the build?

MAX_LOCKDEP_CHAIN_HLOCKS is (1 << LOCKDEP_CHAINS_BITS) * LOCKDEP_CHAIN_DEPTH, and add_chain_cache() has:

        BUILD_BUG_ON((1UL << 24) <= ARRAY_SIZE(chain_hlocks));

since lock_chain.base is a 24-bit index into chain_hlocks[]. So the product has to stay below 2^24.

LOCKDEP_CHAINS_BITS ranges up to 21, so with the new knob:

        CHAINS_BITS=21, DEPTH=8  ->  2^21 * 8 == 2^24  ->  BUILD_BUG_ON fires

i.e. at the maximum CHAINS_BITS anything above DEPTH=7 stops building. Today, with the fixed depth of 5, 2^21 * 5 stays under the limit, so this is a new combination the patch makes reachable.

What I find a little awkward is that this is exactly the path the help text points people at: someone who already raised LOCKDEP_CHAINS_BITS to 21 chasing the "too low" message and then bumps LOCKDEP_CHAIN_DEPTH as suggested gets a (fairly opaque) BUILD_BUG_ON rather than a larger pool.

Kconfig can't easily express the (1 << CHAINS_BITS) * DEPTH < 2^24 product, so would it make sense to at least spell the constraint out in the help text and note that the build fails otherwise? Capping the range for the worst case (CHAINS_BITS=21 => DEPTH <= 7) would be safe but would needlessly restrict the common CHAINS_BITS=16 case, where much larger depths are fine.

Thanks,
Zhan Xusheng