Skip to content

Commit 4d99f66

Browse files
avagindavem330
authored andcommitted
net: allow to call netif_reset_xps_queues() under cpus_read_lock
The definition of static_key_slow_inc() has cpus_read_lock in place. In the virtio_net driver, XPS queues are initialized after setting the queue:cpu affinity in virtnet_set_affinity() which is already protected within cpus_read_lock. Lockdep prints a warning when we are trying to acquire cpus_read_lock when it is already held. This patch adds an ability to call __netif_set_xps_queue under cpus_read_lock(). Acked-by: Jason Wang <[email protected]> ============================================ WARNING: possible recursive locking detected 4.18.0-rc3-next-20180703+ #1 Not tainted -------------------------------------------- swapper/0/1 is trying to acquire lock: 00000000cf973d46 (cpu_hotplug_lock.rw_sem){++++}, at: static_key_slow_inc+0xe/0x20 but task is already holding lock: 00000000cf973d46 (cpu_hotplug_lock.rw_sem){++++}, at: init_vqs+0x513/0x5a0 other info that might help us debug this: Possible unsafe locking scenario: CPU0 ---- lock(cpu_hotplug_lock.rw_sem); lock(cpu_hotplug_lock.rw_sem); *** DEADLOCK *** May be due to missing lock nesting notation 3 locks held by swapper/0/1: #0: 00000000244bc7da (&dev->mutex){....}, at: __driver_attach+0x5a/0x110 #1: 00000000cf973d46 (cpu_hotplug_lock.rw_sem){++++}, at: init_vqs+0x513/0x5a0 #2: 000000005cd8463f (xps_map_mutex){+.+.}, at: __netif_set_xps_queue+0x8d/0xc60 v2: move cpus_read_lock() out of __netif_set_xps_queue() Cc: "Nambiar, Amritha" <[email protected]> Cc: "Michael S. Tsirkin" <[email protected]> Cc: Jason Wang <[email protected]> Fixes: 8af2c06 ("net-sysfs: Add interface for Rx queue(s) map per Tx queue") Signed-off-by: Andrei Vagin <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4005a7c commit 4d99f66

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

drivers/net/virtio_net.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1903,9 +1903,11 @@ static void virtnet_set_affinity(struct virtnet_info *vi)
19031903

19041904
i = 0;
19051905
for_each_online_cpu(cpu) {
1906+
const unsigned long *mask = cpumask_bits(cpumask_of(cpu));
1907+
19061908
virtqueue_set_affinity(vi->rq[i].vq, cpu);
19071909
virtqueue_set_affinity(vi->sq[i].vq, cpu);
1908-
netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
1910+
__netif_set_xps_queue(vi->dev, mask, i, false);
19091911
i++;
19101912
}
19111913

net/core/dev.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,7 @@ static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
21762176
if (!static_key_false(&xps_needed))
21772177
return;
21782178

2179+
cpus_read_lock();
21792180
mutex_lock(&xps_map_mutex);
21802181

21812182
if (static_key_false(&xps_rxqs_needed)) {
@@ -2199,10 +2200,11 @@ static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
21992200

22002201
out_no_maps:
22012202
if (static_key_enabled(&xps_rxqs_needed))
2202-
static_key_slow_dec(&xps_rxqs_needed);
2203+
static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
22032204

2204-
static_key_slow_dec(&xps_needed);
2205+
static_key_slow_dec_cpuslocked(&xps_needed);
22052206
mutex_unlock(&xps_map_mutex);
2207+
cpus_read_unlock();
22062208
}
22072209

22082210
static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
@@ -2250,6 +2252,7 @@ static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
22502252
return new_map;
22512253
}
22522254

2255+
/* Must be called under cpus_read_lock */
22532256
int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
22542257
u16 index, bool is_rxqs_map)
22552258
{
@@ -2317,9 +2320,9 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
23172320
if (!new_dev_maps)
23182321
goto out_no_new_maps;
23192322

2320-
static_key_slow_inc(&xps_needed);
2323+
static_key_slow_inc_cpuslocked(&xps_needed);
23212324
if (is_rxqs_map)
2322-
static_key_slow_inc(&xps_rxqs_needed);
2325+
static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
23232326

23242327
for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
23252328
j < nr_ids;) {
@@ -2448,11 +2451,18 @@ int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
24482451
kfree(new_dev_maps);
24492452
return -ENOMEM;
24502453
}
2454+
EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
24512455

24522456
int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
24532457
u16 index)
24542458
{
2455-
return __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2459+
int ret;
2460+
2461+
cpus_read_lock();
2462+
ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2463+
cpus_read_unlock();
2464+
2465+
return ret;
24562466
}
24572467
EXPORT_SYMBOL(netif_set_xps_queue);
24582468

net/core/net-sysfs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/pm_runtime.h>
2727
#include <linux/of.h>
2828
#include <linux/of_net.h>
29+
#include <linux/cpu.h>
2930

3031
#include "net-sysfs.h"
3132

@@ -1400,7 +1401,10 @@ static ssize_t xps_rxqs_store(struct netdev_queue *queue, const char *buf,
14001401
return err;
14011402
}
14021403

1404+
cpus_read_lock();
14031405
err = __netif_set_xps_queue(dev, mask, index, true);
1406+
cpus_read_unlock();
1407+
14041408
kfree(mask);
14051409
return err ? : len;
14061410
}

0 commit comments

Comments
 (0)