net-shapers: implement shaper cleanup on queue deletion
authorPaolo Abeni <pabeni@redhat.com>
Wed, 9 Oct 2024 08:09:53 +0000 (10:09 +0200)
committerJakub Kicinski <kuba@kernel.org>
Thu, 10 Oct 2024 15:30:22 +0000 (08:30 -0700)
hook into netif_set_real_num_tx_queues() to cleanup any shaper
configured on top of the to-be-destroyed TX queues.

Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Reviewed-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
Link: https://patch.msgid.link/6da4ee03cae2b2a757d7b59e88baf09cc94c5ef1.1728460186.git.pabeni@redhat.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/core/dev.c
net/core/dev.h
net/shaper/shaper.c

index 6e727c49a6f7e1408978fe79a4691b8ac9f25953..b590eefce3b41379d86edcfa8315ee316dbe8fce 100644 (file)
@@ -2949,6 +2949,8 @@ int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
                if (dev->num_tc)
                        netif_setup_tc(dev, txq);
 
+               net_shaper_set_real_num_tx_queues(dev, txq);
+
                dev_qdisc_change_real_num_tx(dev, txq);
 
                dev->real_num_tx_queues = txq;
index 13c558874af3432d48d39e460e24a10be3bfd22c..d3ea92949ff38d4a1e6df4e6e1c27fbd79ecc35a 100644 (file)
@@ -37,8 +37,12 @@ void dev_addr_check(struct net_device *dev);
 
 #if IS_ENABLED(CONFIG_NET_SHAPER)
 void net_shaper_flush_netdev(struct net_device *dev);
+void net_shaper_set_real_num_tx_queues(struct net_device *dev,
+                                      unsigned int txq);
 #else
 static inline void net_shaper_flush_netdev(struct net_device *dev) {}
+static inline void net_shaper_set_real_num_tx_queues(struct net_device *dev,
+                                                    unsigned int txq) {}
 #endif
 
 /* sysctls not referred to from outside net/core/ */
index ddd1999b3f2794b68a461dbfcd3eec04b1a2d556..85ad172833fce8363a00242aa7060e0573d26abf 100644 (file)
@@ -1157,6 +1157,54 @@ void net_shaper_flush_netdev(struct net_device *dev)
        net_shaper_flush(&binding);
 }
 
+void net_shaper_set_real_num_tx_queues(struct net_device *dev,
+                                      unsigned int txq)
+{
+       struct net_shaper_hierarchy *hierarchy;
+       struct net_shaper_binding binding;
+       int i;
+
+       binding.type = NET_SHAPER_BINDING_TYPE_NETDEV;
+       binding.netdev = dev;
+       hierarchy = net_shaper_hierarchy(&binding);
+       if (!hierarchy)
+               return;
+
+       /* Only drivers implementing shapers support ensure
+        * the lock is acquired in advance.
+        */
+       lockdep_assert_held(&dev->lock);
+
+       /* Take action only when decreasing the tx queue number. */
+       for (i = txq; i < dev->real_num_tx_queues; ++i) {
+               struct net_shaper_handle handle, parent_handle;
+               struct net_shaper *shaper;
+               u32 index;
+
+               handle.scope = NET_SHAPER_SCOPE_QUEUE;
+               handle.id = i;
+               shaper = net_shaper_lookup(&binding, &handle);
+               if (!shaper)
+                       continue;
+
+               /* Don't touch the H/W for the queue shaper, the drivers already
+                * deleted the queue and related resources.
+                */
+               parent_handle = shaper->parent;
+               index = net_shaper_handle_to_index(&handle);
+               xa_erase(&hierarchy->shapers, index);
+               kfree_rcu(shaper, rcu);
+
+               /* The recursion on parent does the full job. */
+               if (parent_handle.scope != NET_SHAPER_SCOPE_NODE)
+                       continue;
+
+               shaper = net_shaper_lookup(&binding, &parent_handle);
+               if (shaper && !--shaper->leaves)
+                       __net_shaper_delete(&binding, shaper, NULL);
+       }
+}
+
 static int __init shaper_init(void)
 {
        return genl_register_family(&net_shaper_nl_family);