Skip to contentSkip to conversation

lockdep: Fix subclass resource leak in lockdep_unregister_key()

PATCH Superseded Nilay Shroff 2 participants 2 messages Jul 19 Locking primitives

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lockdep: Fix subclass resource leak in lockdep_unregister_key()

When a dynamic lock key is unregistered using lockdep_unregister_key(), lockdep routes the cleanup sequence through __lockdep_free_key_range() with a hardcoded size of 1.

This layout model assumes that a dynamic key only occupies its base registration address (subclass 0). However, when a lock utilizes nested acquisitions—either implicitly via networking paths (e.g., bh_lock_sock_nested() passing SINGLE_DEPTH_NESTING) or explicitly via mutex_lock_nested()—lockdep calculates virtual class tracking nodes using pointer math offsets:

Virtual Class Pointer = Base Address of Key + Subclass Index

Because the unregister range size is strictly constrained to 1, lockdep completely skips evaluating adjacent virtual subclass slots (key + 1 through key + 7). Consequently, when dynamic structures (such as per- queue network sockets) are repeatedly created, teardowned, and allocated at fresh memory blocks, the subclass /1 structures and their historical dependency chains are permanently orphaned in the global graph. Over prolonged runtime and frequent reconnect loops, this asymmetry leads to the absolute exhaustion of MAX_LOCKDEP_CHAIN_HLOCKS.

Fix this by instructing lockdep_unregister_key() to look ahead across the entire valid subclass allocation block range (MAX_LOCKDEP_SUBCLASSES), ensuring that all related subclass matrix definitions are completely zapped alongside the primary key.

---
 kernel/locking/lockdep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/lockdep.c +1 -1
index 2d4c5bab5af8..490904bee63e 100644
@@ -6606,7 +6606,7 @@ void lockdep_unregister_key(struct lock_class_key *key)
 	WARN_ON_ONCE(!found && debug_locks);
 	if (found) {
 		pf = get_pending_free();
-		__lockdep_free_key_range(pf, key, 1);
+		__lockdep_free_key_range(pf, key, MAX_LOCKDEP_SUBCLASSES);
 		need_callback = prepare_call_rcu_zapped(pf);
 		nr_dynamic_keys--;
 	}
Signature · 1 line
2.53.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 40 lines · Nilay Shroff

When a dynamic lock key is unregistered using lockdep_unregister_key(), lockdep routes the cleanup sequence through __lockdep_free_key_range() with a hardcoded size of 1.

This layout model assumes that a dynamic key only occupies its base registration address (subclass 0). However, when a lock utilizes nested acquisitions—either implicitly via networking paths (e.g., bh_lock_sock_nested() passing SINGLE_DEPTH_NESTING) or explicitly via mutex_lock_nested()—lockdep calculates virtual class tracking nodes using pointer math offsets:

Virtual Class Pointer = Base Address of Key + Subclass Index

Because the unregister range size is strictly constrained to 1, lockdep completely skips evaluating adjacent virtual subclass slots (key + 1 through key + 7). Consequently, when dynamic structures (such as per- queue network sockets) are repeatedly created, teardowned, and allocated at fresh memory blocks, the subclass /1 structures and their historical dependency chains are permanently orphaned in the global graph. Over prolonged runtime and frequent reconnect loops, this asymmetry leads to the absolute exhaustion of MAX_LOCKDEP_CHAIN_HLOCKS.

Fix this by instructing lockdep_unregister_key() to look ahead across the entire valid subclass allocation block range (MAX_LOCKDEP_SUBCLASSES), ensuring that all related subclass matrix definitions are completely zapped alongside the primary key.

---
  kernel/locking/lockdep.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/locking/lockdep.c +0 -0
index 2d4c5bab5af8..490904bee63e 100644

kernel/locking/lockdep.c void lockdep_unregister_key(struct lock_class_key *key) · line 6612

Quoted 8 lines
kernel/locking/lockdep.c +1 -1
@@ -6606,7 +6606,7 @@ void lockdep_unregister_key(struct lock_class_key *key)
  	WARN_ON_ONCE(!found && debug_locks);
  	if (found) {
  		pf = get_pending_free();
-		__lockdep_free_key_range(pf, key, 1);
+		__lockdep_free_key_range(pf, key, MAX_LOCKDEP_SUBCLASSES);
  		need_callback = prepare_call_rcu_zapped(pf);
  		nr_dynamic_keys--;
  	}

I think you should add

Fixes 108c14858b9e ("locking/lockdep: Add support for dynamic keys")

Other than that,