Skip to contentSkip to conversation

lockdep: Fix subclass resource leak in lockdep_unregister_key()

PATCH In review Nilay Shroff 2 participants 4 messages Jul 27 – Sep 25 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

Cc Shin'ichiro Kawasaki <[email protected]>
Fixes 108c14858b9e ("locking/lockdep: Add support for dynamic keys")

---
Changes from v1:
- added Fixes and RB tags

---
 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
Reviewed-by on v2 1/1
#1 lore raw
Quoted 4 lines · Nilay Shroff

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.

#2 lore raw

Hi,

A gentle ping on this one...It's been pending since quite some time now. Does this look okay?

Thanks,
--Nilay

Quoted 8 lines · Bart Van Assche
Quoted 4 lines · Nilay Shroff

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.

#3 lore raw
Quoted 2 lines · Nilay Shroff

A gentle ping on this one...It's been pending since quite some time now. Does this look okay?

This patch looks OK to me and works fine on my test setups so I would appreciate it if this patch would be queued.

Thanks,

Bart.