net/sched: don't wait for RCU when destroying qdisc lockdep keys
Subsystems (4)
cover net/sched: don't wait for RCU when destroying qdisc lockdep keys
From: Vlad Poenaru <[email protected]>
On kernels with CONFIG_PROVE_LOCKING, every qdisc except ingress, clsact and noqueue has a dynamic lockdep key, and destroying the qdisc calls lockdep_unregister_key(), which waits for an expedited RCU grace period. Replacing an mq root destroys one qdisc per TX queue under RTNL, so a single "tc qdisc replace dev eth0 root mq" waits for that many grace periods. Commit 7a3cedafccf8 ("lockdep: Speed up lockdep_unregister_key() with expedited RCU synchronization") made each wait shorter as a temporary workaround until hazard pointers are available.
The wait is redundant for qdiscs: the qdisc is freed with call_rcu() after its key is unregistered, so the key's memory already outlives an RCU grace period. Patch 1 revives Bart Van Assche's lockdep_unregister_key_nosync() [1], with the grace-period requirement spelled out, and patch 2 uses it for qdisc keys, as Eric suggested [2].
Boqun was concerned that a nosync variant is hard to enforce and review, since a later refactor could move the free [3]. For qdiscs the RCU-deferred free is not optional: datapath readers find the qdisc under RCU, so freeing it synchronously would already be a use-after-free regardless of lockdep. Patch 2 also documents the requirement at qdisc_lock_uninit(). The hazptr-based replacement for the wait is still under discussion [4]; if it lands, the nosync variant can stay as the no-wait path or be dropped.
On an x86_64 KASAN + PROVE_LOCKING kernel (64 vCPU KVM guest, 64-queue netdevsim device, 214K lock chains) this removes 5.3 ms of grace-period wait from each of the 65 qdisc destroys in "tc qdisc replace dev eth0 root handle 1: mq".
A separate lockdep patch, sent to tip, makes the zap_class() part of lockdep_unregister_key() cheaper [5]; the two are independent. Nilay Shroff's pending "lockdep: Fix subclass resource leak in lockdep_unregister_key()" [6] touches the same function as patch 1; whichever lands second needs a trivial rebase.
Peter, Boqun, Waiman: if patch 1 looks fine, an Ack for taking it through net-next with its user would be appreciated.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CANn89iLvu_ijSV+FnrZ6qei_Ty2c9V-RjwLMk24+oOS91bNjaQ@mail.gmail.com/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/
Bart Van Assche (1):
lockdep: Introduce lockdep_unregister_key_nosync()
Vlad Poenaru (1):
net/sched: Don't wait for RCU when unregistering the qdisc lockdep key
include/linux/lockdep.h | 5 +++++ include/net/pkt_sched.h | 3 ++- kernel/locking/lockdep.c | 36 +++++++++++++++++++++++++----------- net/sched/sch_generic.c | 2 +- 4 files changed, 33 insertions(+), 13 deletions(-)
Signature · 1 line
2.53.0-Meta
PATCH 1/2 lockdep: Introduce lockdep_unregister_key_nosync()
From: Bart Van Assche <[email protected]>
Add a variant of lockdep_unregister_key() that neither sleeps nor waits for an RCU grace period. Instead the caller must make sure that a grace period elapses after the call before the memory holding the key is freed or reused.
That lets callers that already free the key's memory with call_rcu() or kfree_rcu() avoid waiting for a second grace period. The qdisc code is such a caller: all qdiscs except ingress, clsact and noqueue embed a dynamic lockdep key and are freed with call_rcu(), and replacing an mq root on a multi-queue NIC destroys one qdisc per TX queue under RTNL, each currently waiting for its own expedited grace period.
[vlad: rebase onto synchronize_rcu_expedited(), spell out the grace
period requirement in the comments and changelog, describe the qdisc use case]
--- include/linux/lockdep.h | 5 +++++ kernel/locking/lockdep.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 11 deletions(-)
include/linux/lockdep.h +5 -0
@@ -117,6 +117,7 @@ do { \ }while (0 ) extern void lockdep_register_key(struct lock_class_key *key); +extern void lockdep_unregister_key_nosync (struct lock_class_key *key);extern void lockdep_unregister_key (struct lock_class_key *key);/*
@@ -375,6 +376,10 @@ static inline void lockdep_register_key(struct lock_class_key *key) { } +static inline void lockdep_unregister_key_nosync (struct lock_class_key *key) +{ +} +static inline void lockdep_unregister_key (struct lock_class_key *key) { }
kernel/locking/lockdep.c +25 -11
@@ -6669,13 +6669,19 @@ void lockdep_reset_lock(struct lockdep_map *lock) }/* -* Unregister a dynamically allocated key. +* Unregister a dynamically allocated key without waiting for RCU readers. * * Unlike lockdep_register_key(), a search is always done to find a matching * key irrespective of debug_locks to avoid potential invalid access to freed * memory in lock_class entry. +* +* is_dynamic_key() may still be walking past @key under RCU when this +* returns. The memory holding @key must not be freed or reused, including by +* registering @key again, until an RCU grace period that starts after this +* call has elapsed, e.g. by freeing it with call_rcu() or kfree_rcu() queued +* after this call. Does not sleep. */ -void lockdep_unregister_key (struct lock_class_key *key) +void lockdep_unregister_key_nosync (struct lock_class_key *key) {struct hlist_head *hash_head =keyhashentry (key);struct lock_class_key *k;
@@ -6684,8 +6690,6 @@ void lockdep_unregister_key(struct lock_class_key *key)bool found = false;bool need_callback = false; -might_sleep (); -if (WARN_ON_ONCE (static_obj (key)))return ;
@@ -6711,15 +6715,25 @@ void lockdep_unregister_key(struct lock_class_key *key)if (need_callback)call_rcu (&delayed_free.rcu_head, free_zapped_rcu); +} +EXPORT_SYMBOL_GPL (lockdep_unregister_key_nosync); + +/* +* Unregister a dynamically allocated key and wait for is_dynamic_key() +* readers, so that the memory holding @key may be freed as soon as this +* returns. May sleep. +*/ +void lockdep_unregister_key (struct lock_class_key *key) +{ +might_sleep (); + +lockdep_unregister_key_nosync (key);/* -* Wait until is_dynamic_key() has finished accessing k->hash_entry. -* -* Some operations like __qdisc_destroy() will call this in a debug -* kernel, and the network traffic is disabled while waiting, hence -* the delay of the wait matters in debugging cases. Currently use a -* synchronize_rcu_expedited() to speed up the wait at the cost of -* system IPIs. TODO: Replace RCU with hazptr for this. +* Expedited to keep the wait short, at the cost of IPIs. Callers that +* free @key after a grace period anyway should use +* lockdep_unregister_key_nosync() instead. +* TODO: Replace RCU with hazptr for this. */ synchronize_rcu_expedited (); }
Signature · 1 line
2.53.0-Meta
Re: [PATCH net-next 1/2] lockdep: Introduce lockdep_unregister_key_nosync()
Quoted 22 lines · Vlad Poenaru
From: Bart Van Assche <[email protected]>
Add a variant of
lockdep_unregister_key()that neither sleeps nor waits for an RCU grace period. Instead the caller must make sure that a grace period elapses after the call before the memory holding the key is freed or reused.That lets callers that already free the key's memory with
call_rcu()orkfree_rcu()avoid waiting for a second grace period. The qdisc code is such a caller: all qdiscs except ingress, clsact and noqueue embed a dynamic lockdep key and are freed withcall_rcu(), and replacing an mq root on a multi-queue NIC destroys one qdisc per TX queue under RTNL, each currently waiting for its own expedited grace period.Cc Peter Zijlstra <[email protected]>Cc Ingo Molnar <[email protected]>Cc Will Deacon <[email protected]>Cc Waiman Long <[email protected]>Cc Boqun Feng <[email protected]>Signed-off-by Bart Van Assche <[email protected]>
Quoted 4 lines
[vlad: rebase onto
synchronize_rcu_expedited(), spell out the grace
period requirement in the comments and changelog, describe the qdisc use case]Signed-off-by Vlad Poenaru <[email protected]>
Thanks.
PATCH 2/2 net/sched: Don't wait for RCU when unregistering the qdisc lockdep key
In reply to Vlad PoenaruSince commit af0cb3fa3f9e ("net/sched: fix false lockdep warning on qdisc root lock") every qdisc except ingress, clsact and noqueue registers a dynamic lockdep key, and destroying it calls lockdep_unregister_key(), which waits for an expedited RCU grace period. Replacing an mq root destroys one qdisc per TX queue under RTNL, so a single "tc qdisc replace dev eth0 root mq" waits for that many grace periods, each of which IPIs every CPU that is not idle.
The wait is not needed here: __qdisc_destroy() and qdisc_create() free the qdisc with qdisc_free_rcu() after unregistering, which already keeps the key's memory valid for a grace period. Use lockdep_unregister_key_nosync(), and free with kfree_rcu() in the one qdisc_alloc() error path that used kfree().
On an x86_64 KASAN + PROVE_LOCKING kernel (64 vCPU KVM guest, 64-queue netdevsim device, 214K lock chains) this removes 5.3 ms of grace-period wait from each of the 65 qdisc destroys in "tc qdisc replace dev eth0 root handle 1: mq".
--- include/net/pkt_sched.h | 3 ++- net/sched/sch_generic.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)
include/net/pkt_sched.h +2 -1
@@ -323,6 +323,7 @@ static inline void qdisc_lock_init(struct Qdisc *sch,lockdep_set_class (&sch->q.lock, &sch->root_lock_key); } +/* Free @sch only after an RCU grace period, e.g. with qdisc_free_rcu(). */ static inline void qdisc_lock_uninit (struct Qdisc *sch,const struct Qdisc_ops *ops) {
@@ -330,7 +331,7 @@ static inline void qdisc_lock_uninit(struct Qdisc *sch, ops == &noqueue_qdisc_ops)return ; - lockdep_unregister_key(&sch->root_lock_key); + lockdep_unregister_key_nosync(&sch->root_lock_key); }#endif
net/sched/sch_generic.c +1 -1
@@ -1033,7 +1033,7 @@ struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,return sch; errout1:qdisc_lock_uninit (sch, ops); -kfree (sch); +kfree_rcu (sch, rcu); errout:return ERR_PTR (err); }
Signature · 1 line
2.53.0-Meta
Re: [PATCH net-next 2/2] net/sched: Don't wait for RCU when unregistering the qdisc lockdep key
Quoted 15 lines · Vlad Poenaru
Since commit
af0cb3fa3f9e("net/sched: fix false lockdep warning on qdisc root lock") every qdisc except ingress, clsact and noqueue registers a dynamic lockdep key, and destroying it callslockdep_unregister_key(), which waits for an expedited RCU grace period. Replacing an mq root destroys one qdisc per TX queue under RTNL, so a single "tc qdisc replace dev eth0 root mq" waits for that many grace periods, each of which IPIs every CPU that is not idle.The wait is not needed here:
__qdisc_destroy()andqdisc_create()free the qdisc withqdisc_free_rcu()after unregistering, which already keeps the key's memory valid for a grace period. Uselockdep_unregister_key_nosync(), and free withkfree_rcu()in the oneqdisc_alloc()error path that usedkfree().
Quoted 8 lines
On an
x86_64KASAN +PROVE_LOCKINGkernel (64 vCPU KVM guest, 64-queue netdevsim device, 214K lock chains) this removes 5.3 ms of grace-period wait from each of the 65 qdisc destroys in "tc qdisc replace dev eth0 root handle 1: mq".
Note the cover letter has 'From: Vlad Poenaru <[email protected]>', so this SoB is a bit confusing.
Thanks.
Boqun Feng
maintainerQuoted 17 lines · Vlad Poenaru
From: Vlad Poenaru <[email protected]>
On kernels with
CONFIG_PROVE_LOCKING, every qdisc except ingress, clsact and noqueue has a dynamic lockdep key, and destroying the qdisc callslockdep_unregister_key(), which waits for an expedited RCU grace period. Replacing an mq root destroys one qdisc per TX queue under RTNL, so a single "tc qdisc replace dev eth0 root mq" waits for that many grace periods. Commit7a3cedafccf8("lockdep: Speed uplockdep_unregister_key()with expedited RCU synchronization") made each wait shorter as a temporary workaround until hazard pointers are available.The wait is redundant for qdiscs: the qdisc is freed with
call_rcu()after its key is unregistered, so the key's memory already outlives an RCU grace period. Patch 1 revives Bart Van Assche'slockdep_unregister_key_nosync()[1], with the grace-period requirement spelled out, and patch 2 uses it for qdisc keys, as Eric suggested [2].
Quoted 5 lines
Boqun was concerned that a nosync variant is hard to enforce and review, since a later refactor could move the free [3]. For qdiscs the RCU-deferred free is not optional: datapath readers find the qdisc under RCU, so freeing it synchronously would already be a use-after-free regardless of lockdep. Patch 2 also documents the requirement at
qdisc_lock_uninit().
I think my concern is still valid: for lockdep PoV, lockdep_unregister_key_nosync() is an API that users have to use it carefully, because it leaves the unregistration process in a stage that you need to guarantee that the lockdep key reclamation is delayed by an RCU grace period. Or in other words, do you have a way to prevent a user from doing lockdep_unregister_key_nosync() but kfree(key), or some sort of debugging to find a mis-use?
Regards,
Boqun
Quoted 40 lines
The hazptr-based replacement for the wait is still under discussion [4]; if it lands, the nosync variant can stay as the no-wait path or be dropped.
On an
x86_64KASAN +PROVE_LOCKINGkernel (64 vCPU KVM guest, 64-queue netdevsim device, 214K lock chains) this removes 5.3 ms of grace-period wait from each of the 65 qdisc destroys in "tc qdisc replace dev eth0 root handle 1: mq".A separate lockdep patch, sent to tip, makes the
zap_class()part oflockdep_unregister_key()cheaper [5]; the two are independent. Nilay Shroff's pending "lockdep: Fix subclass resource leak inlockdep_unregister_key()" [6] touches the same function as patch 1; whichever lands second needs a trivial rebase.Peter, Boqun, Waiman: if patch 1 looks fine, an Ack for taking it through net-next with its user would be appreciated.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/CANn89iLvu_ijSV+FnrZ6qei_Ty2c9V-RjwLMk24+oOS91bNjaQ@mail.gmail.com/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/Bart Van Assche (1):
lockdep: Introducelockdep_unregister_key_nosync()Vlad Poenaru (1):
net/sched: Don't wait for RCU when unregistering the qdisc lockdep keyinclude/linux/lockdep.h | 5 +++++ include/net/pkt_sched.h | 3 ++- kernel/locking/lockdep.c | 36 +++++++++++++++++++++++++----------- net/sched/sch_generic.c | 2 +- 4 files changed, 33 insertions(+), 13 deletions(-)Signature · 1 line
2.53.0-Meta
Quoted 7 lines · Boqun Feng
I think my concern is still valid: for lockdep PoV,
lockdep_unregister_key_nosync()is an API that users have to use it carefully, because it leaves the unregistration process in a stage that you need to guarantee that the lockdep key reclamation is delayed by an RCU grace period. Or in other words, do you have a way to prevent a user from doinglockdep_unregister_key_nosync()but kfree(key), or some sort of debugging to find a mis-use?
Not in this version, but lockdep could catch it. Every slab, page and vmalloc free already goes through debug_check_no_locks_freed(), so:
- lockdep_unregister_key_nosync() records the key together with get_state_synchronize_rcu() in a small fixed-size table;
- debug_check_no_locks_freed() warns if the freed range contains a recorded key whose grace period has not elapsed yet (poll_state_synchronize_rcu()), and retires entries whose grace period has;
- lockdep_register_key() warns the same way if a recorded key is reused too early.
The table is empty nearly all the time, so frees only pay for it during bursts of unregisters. A correct kfree_rcu() or call_rcu() free runs after the grace period and never warns; lockdep_unregister_key_nosync() followed by kfree() warns on that kfree().
Would that address your concern? If so I'll add it to v2.
The other way would be for lockdep to keep the dynamic-key hash nodes in its own storage, freed via RCU like zapped classes, so is_dynamic_key() never dereferences caller memory and lockdep_unregister_key() needs no wait at all. That removes the nosync API, but it is a bigger change.
Separately, two corrections to the cover letter: please ignore the stray "From: Vlad Poenaru <[email protected]>" line (Eric, sorry for the confusion; the series is from and signed off by [email protected]), and [5] should be:
[5] https://lore.kernel.org/all/[email protected]/
Thanks,
Vlad
Boqun Feng
maintainerQuoted 21 lines · Vlad Poenaru
Quoted 7 lines · Boqun Feng
I think my concern is still valid: for lockdep PoV,
lockdep_unregister_key_nosync()is an API that users have to use it carefully, because it leaves the unregistration process in a stage that you need to guarantee that the lockdep key reclamation is delayed by an RCU grace period. Or in other words, do you have a way to prevent a user from doinglockdep_unregister_key_nosync()but kfree(key), or some sort of debugging to find a mis-use?Not in this version, but lockdep could catch it. Every slab, page and vmalloc free already goes through
debug_check_no_locks_freed(), so:-
lockdep_unregister_key_nosync()records the key together withget_state_synchronize_rcu()in a small fixed-size table;
-debug_check_no_locks_freed()warns if the freed range contains a recorded key whose grace period has not elapsed yet (poll_state_synchronize_rcu()), and retires entries whose grace period has;
-lockdep_register_key()warns the same way if a recorded key is reused too early.
Quoted 7 lines
The table is empty nearly all the time, so frees only pay for it during bursts of unregisters. A correct
kfree_rcu()orcall_rcu()free runs after the grace period and never warns;lockdep_unregister_key_nosync()followed bykfree()warns on thatkfree().Would that address your concern? If so I'll add it to v2.
Sounds like a solid plan to me. You can make it behind CONFIG_LOCKDEP_DEBUG.
Quoted 2 lines
The other way would be for lockdep to keep the dynamic-key hash nodes in its own storage, freed via RCU like zapped classes, so
is_dynamic_key()
But this means lockdep needs to do its own allocation for the hash nodes? Where does the memory come from, a slab or something? If so, I think we should probably avoid that. But I like this idea more since it won't introduce another API. So please do explore this idea as well, thank you!
Regards,
Boqun
Quoted 12 lines
never dereferences caller memory and
lockdep_unregister_key()needs no wait at all. That removes the nosync API, but it is a bigger change.Separately, two corrections to the cover letter: please ignore the stray "From: Vlad Poenaru <[email protected]>" line (Eric, sorry for the confusion; the series is from and signed off by [email protected]), and [5] should be:
[5] https://lore.kernel.org/all/[email protected]/
Thanks,
Vlad
Quoted 2 lines · Boqun Feng
Sounds like a solid plan to me. You can make it behind
CONFIG_LOCKDEP_DEBUG.
Done, under CONFIG_DEBUG_LOCKDEP, as a fallback.
Quoted 5 lines
But this means lockdep needs to do its own allocation for the hash nodes? Where does the memory come from, a slab or something? If so, I think we should probably avoid that. But I like this idea more since it won't introduce another API. So please do explore this idea as well, thank you!
No slab. The nodes live in a static array with an in-use bitmap, like lockdep's classes, list entries and chains, sized by a new CONFIG_LOCKDEP_DYNAMIC_KEYS_BITS. Unregistering hands the node to the existing delayed_free machinery, so a slot is only reused after a grace period, and lockdep_unregister_key() no longer waits at all. is_dynamic_key() only compares key addresses stored in lockdep's own memory. I posted it as an RFC against tip/locking/core:
https://lore.kernel.org/all/[email protected]/
With that, this series is not needed. Given Jakub's note it is on hold anyway; I will only come back to it if the RFC approach does not work out.
Thanks,
Vlad
Quoted 7 lines · Boqun Feng
I think my concern is still valid: for lockdep PoV,
lockdep_unregister_key_nosync()is an API that users have to use it carefully, because it leaves the unregistration process in a stage that you need to guarantee that the lockdep key reclamation is delayed by an RCU grace period. Or in other words, do you have a way to prevent a user from doinglockdep_unregister_key_nosync()but kfree(key), or some sort of debugging to find a mis-use?
Kernel developers are assumed to know what they are doing. A clear comment above lockdep_unregister_key_nosync() should be sufficient.
Thanks,
Bart.
Boqun Feng
maintainerQuoted 10 lines · Bart Van Assche
Quoted 7 lines · Boqun Feng
I think my concern is still valid: for lockdep PoV,
lockdep_unregister_key_nosync()is an API that users have to use it carefully, because it leaves the unregistration process in a stage that you need to guarantee that the lockdep key reclamation is delayed by an RCU grace period. Or in other words, do you have a way to prevent a user from doinglockdep_unregister_key_nosync()but kfree(key), or some sort of debugging to find a mis-use?Kernel developers are assumed to know what they are doing. A clear comment
Well, if that's true, why do you need lockdep :) If developers know what they are doing, they should write no deadlock or prove that in a comment.
Is there any context that I'm missing that you want to shut up this discussion and rush the change in?
Regards,
Boqun
Quoted 5 lines
above
lockdep_unregister_key_nosync()should be sufficient.Thanks,
Bart.
Quoted 2 lines · Boqun Feng
Is there any context that I'm missing that you want to shut up this discussion and rush the change in?
I'm not trying to shut up anything or rush in anything. All I did is to share my opinion.
Bart.
Boqun Feng
maintainerQuoted 7 lines · Bart Van Assche
Quoted 2 lines · Boqun Feng
Is there any context that I'm missing that you want to shut up this discussion and rush the change in?
I'm not trying to shut up anything or rush in anything. All I did is to share my opinion.
Phew. I value your input but note the Vlad already offered some options to resolve my concern [1]. It'll be valuable to examine these first in my opinion.
[1]: https://lore.kernel.org/lkml/[email protected]/
Regards,
Boqun
Quoted 1 line
Bart.
Jakub Kicinski
maintainerQuoted 8 lines · On Mon, 19 Oct 2026 10:02:23 -0700 Vlad Poenaru wrote:
On kernels with
CONFIG_PROVE_LOCKING, every qdisc except ingress, clsact and noqueue has a dynamic lockdep key, and destroying the qdisc callslockdep_unregister_key(), which waits for an expedited RCU grace period. Replacing an mq root destroys one qdisc per TX queue under RTNL, so a single "tc qdisc replace dev eth0 root mq" waits for that many grace periods. Commit7a3cedafccf8("lockdep: Speed uplockdep_unregister_key()with expedited RCU synchronization") made each wait shorter as a temporary workaround until hazard pointers are available.
## Form letter - net-next-closed
The merge window for Linux v7.4 has begun, and the net-next tree is now closed for new drivers, features, code refactoring and optimizations. We are currently accepting bug fixes only.
Please repost when net-next reopens. There is some uncertainty about how long this merge window will last, so please check
https://netdev.bots.linux.dev/net-next.html
before reposting. net-next _may_ reopen on Nov 2nd.
See: https://www.kernel.org/doc/html/next/process/maintainer-netdev.html#development-cycle
Signature · 2 lines
pw-bot: defer pv-bot: closed