# Re: \[PATCH net-next v10 04/14\] net: hold netdev instance lock during qdisc ndo\_setup\_tc

- From: Cosmin Ratiu \<hidden\>
- Date: 2025-05-05 13:41:43 
- List: netdev
- Page: https://ratatoskr.run/netdev/2025/05/11326458
- Thread: https://ratatoskr.run/netdev/2025/03/11321361/t.md

```text
On Wed, 2025-03-05 at 08:37 -0800, Stanislav Fomichev wrote:
> Qdisc operations that can lead to ndo_setup_tc might need
> to have an instance lock. Add netdev_lock_ops/netdev_unlock_ops
> invocations for all psched_rtnl_msg_handlers operations.

Sorry for resurrecting this thread, but it seems like a good place to
ask a related question.

If qdisc operations that lead to .ndo_setup_tc() need to hold an
instance lock, shouldn't all such callers acquire it?

In my testing, I found out that e.g., when unloading a device, there's
this call path which ends up calling .ndo_setup_tc() unlocked:

devlink_reload -...-> device_del -...-> unregister_netdev -...->
unregister_netdevice_many_notify -> dev_shutdown -> qdisc_put ->
__qdisc_destroy -> mqprio_disable_offload -> .ndo_setup_tc

Many other qdiscs (other than mqprio) call .ndo_setup_tc() and would be
in a similar situation.

Does it make sense to extend the netdev lock for dev_shutdown like in
the patch below? After some basic testing, it seems safe but I haven't
looked too deep into all possibilities.

Cosmin.

From e8b613dfd2b241a6c19bb89a829d598d6640b6f9 Mon Sep 17 00:00:00 2001
From: Cosmin Ratiu <redacted>
Date: Mon, 5 May 2025 15:34:53 +0300
Subject: [PATCH 01/20] net/sched: Lock netdevices during dev_shutdown

Various qdiscs can end up calling into .ndo_setup_tc() and as such,
might need the netdev instance lock.

Signed-off-by: Cosmin Ratiu <redacted>
---
 net/core/dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index d1a8cad0c99c..134ceddf7fa5 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -12020,9 +12020,9 @@ void unregister_netdevice_many_notify(struct
list_head *head,
                struct sk_buff *skb = NULL;
 
                /* Shutdown queueing discipline. */
+               netdev_lock_ops(dev);
                dev_shutdown(dev);
                dev_tcx_uninstall(dev);
-               netdev_lock_ops(dev);
                dev_xdp_uninstall(dev);
                dev_memory_provider_uninstall(dev);
                netdev_unlock_ops(dev);
@@ -12218,7 +12218,9 @@ int __dev_change_net_namespace(struct
net_device *dev, struct net *net,
        synchronize_net();
 
        /* Shutdown queueing discipline. */
+       netdev_lock_ops(dev);
        dev_shutdown(dev);
+       netdev_unlock_ops(dev);
 
        /* Notify protocols, that we are about to destroy
         * this device. They should clean all the things.
-- 
2.45.0

```
