[PATCH v2] net: use exponential backoff in netdev_wait_allrefs

From: Francesco Ruggeri
Date: Thu Sep 17 2020 - 18:19:27 EST

The combination of aca_free_rcu, introduced in commit 2384d02520ff
("net/ipv6: Add anycast addresses to a global hashtable"), and
fib6_info_destroy_rcu, introduced in commit 9b0a8da8c4c6 ("net/ipv6:
respect rcu grace period before freeing fib6_info"), can result in
an extra rcu grace period being needed when deleting an interface,
with the result that netdev_wait_allrefs ends up hitting the msleep(250),
which is considerably longer than the required grace period.
This can result in long delays when deleting a large number of interfaces,
and it can be observed with this script:


ip netns add $ns
ip netns exec $ns ip link set lo up
ip netns exec $ns sysctl net.ipv6.conf.default.disable_ipv6=0
ip netns exec $ns sysctl net.ipv6.conf.default.forwarding=1

for ((i=0; i<$NIFS; i++))
ip netns exec $ns ip link add $if type dummy
ip netns exec $ns ip link set $if up
ip netns exec $ns ip -6 addr add 2021:$i::1/120 dev $if

for ((i=0; i<$NIFS; i++))
ip netns exec $ns ip link del $if

ip netns del $ns

This patch uses exponential backoff instead of the fixed msleep(250)
to get out of the loop faster.

Time with this patch on a 5.4 kernel:

real 0m8.199s
user 0m0.402s
sys 0m1.213s

Time without this patch:

real 0m31.522s
user 0m0.438s
sys 0m1.156s

v2: use exponential backoff instead of trying to wake up

Signed-off-by: Francesco Ruggeri <fruggeri@xxxxxxxxxx>

net/core/dev.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index 4086d335978c..69f549780b8e 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -9986,10 +9986,13 @@ EXPORT_SYMBOL(netdev_refcnt_read);
* We can get stuck here if buggy protocols don't correctly
* call dev_put.
+#define MIN_MSLEEP ((unsigned int)16)
+#define MAX_MSLEEP ((unsigned int)250)
static void netdev_wait_allrefs(struct net_device *dev)
unsigned long rebroadcast_time, warning_time;
int refcnt;
+ unsigned int wait = MIN_MSLEEP;


@@ -10023,7 +10026,8 @@ static void netdev_wait_allrefs(struct net_device *dev)
rebroadcast_time = jiffies;

- msleep(250);
+ msleep(wait);
+ wait = min(wait << 1, MAX_MSLEEP);

refcnt = netdev_refcnt_read(dev);