virtio-net: disable delayed refill when pausing rx
Subsystems (6)
v2 cover virtio-net: disable delayed refill when pausing rx
Hi everyone,
This series tries to fix a deadlock in virtio-net when binding/unbinding XDP program, XDP socket or resizing the rx queue.
When pausing rx (e.g. set up xdp, xsk pool, rx resize), we call napi_disable() on the receive queue's napi. In delayed refill_work, it also calls napi_disable() on the receive queue's napi. When napi_disable() is called on an already disabled napi, it will sleep in napi_disable_locked while still holding the netdev_lock. As a result, later napi_enable gets stuck too as it cannot acquire the netdev_lock. This leads to refill_work and the pause-then-resume tx are stuck altogether.
This scenario can be reproducible by binding a XDP socket to virtio-net interface without setting up the fill ring. As a result, try_fill_recv will fail until the fill ring is set up and refill_work is scheduled.
This fix adds virtnet_rx_(pause/resume)_all helpers and fixes up the virtnet_rx_resume to disable future and cancel all inflights delayed refill_work before calling napi_disable() to pause the rx.
Version 2 changes:
- Add selftest for deadlock scenario
Thanks,
Quang Minh.
Bui Quang Minh (3):
virtio-net: disable delayed refill when pausing rx
selftests: net: move xdp_helper to net/lib
selftests: net: add a virtio_net deadlock selftest
drivers/net/virtio_net.c | 60 ++++++++++++++++---
tools/testing/selftests/Makefile | 2 +-
tools/testing/selftests/drivers/net/Makefile | 2 -
tools/testing/selftests/drivers/net/queues.py | 4 +-
.../selftests/drivers/net/virtio_net/Makefile | 2 +
.../selftests/drivers/net/virtio_net/config | 1 +
.../drivers/net/virtio_net/lib/py/__init__.py | 16 +++++
.../drivers/net/virtio_net/xsk_pool.py | 52 ++++++++++++++++
tools/testing/selftests/net/lib/.gitignore | 1 +
tools/testing/selftests/net/lib/Makefile | 1 +
.../{drivers/net => net/lib}/xdp_helper.c | 0
11 files changed, 127 insertions(+), 14 deletions(-)
create mode 100644 tools/testing/selftests/drivers/net/virtio_net/lib/py/__init__.py
create mode 100755 tools/testing/selftests/drivers/net/virtio_net/xsk_pool.py
rename tools/testing/selftests/{drivers/net => net/lib}/xdp_helper.c (100%)Signature · 1 line
2.43.0
PATCH v2 1/3 virtio-net: disable delayed refill when pausing rx
When pausing rx (e.g. set up xdp, xsk pool, rx resize), we call napi_disable() on the receive queue's napi. In delayed refill_work, it also calls napi_disable() on the receive queue's napi. When napi_disable() is called on an already disabled napi, it will sleep in napi_disable_locked while still holding the netdev_lock. As a result, later napi_enable gets stuck too as it cannot acquire the netdev_lock. This leads to refill_work and the pause-then-resume tx are stuck altogether.
This scenario can be reproducible by binding a XDP socket to virtio-net interface without setting up the fill ring. As a result, try_fill_recv will fail until the fill ring is set up and refill_work is scheduled.
This commit adds virtnet_rx_(pause/resume)_all helpers and fixes up the virtnet_rx_resume to disable future and cancel all inflights delayed refill_work before calling napi_disable() to pause the rx.
--- drivers/net/virtio_net.c | 60 ++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 9 deletions(-)
drivers/net/virtio_net.c +51 -9
@@ -3342,10 +3342,53 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)return NETDEV_TX_OK ; } +static void virtnet_rx_pause_all (struct virtnet_info *vi) +{ +bool running =netif_running (vi->dev); + +/* +* Make sure refill_work does not run concurrently to +* avoid napi_disable race which leads to deadlock. +*/ +disable_delayed_refill (vi); +cancel_delayed_work_sync (&vi->refill); +if (running) { +int i; + +for (i =0 ; i < vi->max_queue_pairs; i++) { +virtnet_napi_disable (&vi->rq[i]); +virtnet_cancel_dim (vi, &vi->rq[i].dim); + } + } +} + +static void virtnet_rx_resume_all (struct virtnet_info *vi) +{ +bool running =netif_running (vi->dev); +int i; + +enable_delayed_refill (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) { +if (i < vi->curr_queue_pairs) { +if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL )) +schedule_delayed_work (&vi->refill,0 ); + } + +if (running) +virtnet_napi_enable (&vi->rq[i]); + } +} +static void virtnet_rx_pause (struct virtnet_info *vi,struct receive_queue *rq) {bool running =netif_running (vi->dev); +/* +* Make sure refill_work does not run concurrently to +* avoid napi_disable race which leads to deadlock. +*/ +disable_delayed_refill (vi); +cancel_delayed_work_sync (&vi->refill);if (running) {virtnet_napi_disable (rq);virtnet_cancel_dim (vi, &rq->dim);
@@ -3356,6 +3399,7 @@ static void virtnet_rx_resume(struct virtnet_info *vi, struct receive_queue *rq) {bool running =netif_running (vi->dev); +enable_delayed_refill (vi);if (!try_fill_recv (vi, rq,GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 );
@@ -5959,12 +6003,12 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,if (prog)bpf_prog_add (prog, vi->max_queue_pairs -1 ); +virtnet_rx_pause_all (vi); +/* Make sure NAPI is not using any XDP TX queues for RX. */ if (netif_running (dev)) { -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_disable (&vi->rq[i]); +for (i =0 ; i < vi->max_queue_pairs; i++)virtnet_napi_tx_disable (&vi->sq[i]); - } }if (!prog) {
@@ -5996,13 +6040,12 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, vi->xdp_enabled = false; } +virtnet_rx_resume_all (vi);for (i =0 ; i < vi->max_queue_pairs; i++) {if (old_prog)bpf_prog_put (old_prog); -if (netif_running (dev)) { -virtnet_napi_enable (&vi->rq[i]); +if (netif_running (dev))virtnet_napi_tx_enable (&vi->sq[i]); - } }return 0 ;
@@ -6014,11 +6057,10 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,rcu_assign_pointer (vi->rq[i].xdp_prog, old_prog); } +virtnet_rx_resume_all (vi);if (netif_running (dev)) { -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_enable (&vi->rq[i]); +for (i =0 ; i < vi->max_queue_pairs; i++)virtnet_napi_tx_enable (&vi->sq[i]); - } }if (prog)bpf_prog_sub (prog, vi->max_queue_pairs -1 );
Signature · 1 line
2.43.0
Michael S. Tsirkin
maintainerQuoted 17 lines · Bui Quang Minh
When pausing rx (e.g. set up xdp, xsk pool, rx resize), we call
napi_disable()on the receive queue's napi. In delayedrefill_work, it also callsnapi_disable()on the receive queue's napi. Whennapi_disable()is called on an already disabled napi, it will sleep innapi_disable_lockedwhile still holding thenetdev_lock. As a result, laternapi_enablegets stuck too as it cannot acquire thenetdev_lock. This leads torefill_workand the pause-then-resume tx are stuck altogether.This scenario can be reproducible by binding a XDP socket to virtio-net interface without setting up the fill ring. As a result,
try_fill_recvwill fail until the fill ring is set up andrefill_workis scheduled.This commit adds virtnet_rx_(pause/resume)_all helpers and fixes up the
virtnet_rx_resumeto disable future and cancel all inflights delayedrefill_workbefore callingnapi_disable()to pause the rx.
Quoted 5 lines
Fixes 413f0271f396 ("net: protect NAPI enablement with netdev_lock()")Signed-off-by Bui Quang Minh <[email protected]>--- drivers/net/virtio_net.c | 60 ++++++++++++++++++++++++++++++++++------ 1 file changed, 51 insertions(+), 9 deletions(-)
Thans for the patch! Yes something to improve:
Quoted 14 lines
drivers/net/virtio_net.c +6
-0@@ -3342,10 +3342,53 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)return NETDEV_TX_OK ; } +static void virtnet_rx_pause_all (struct virtnet_info *vi) +{ +bool running =netif_running (vi->dev); + +/* +* Make sure refill_work does not run concurrently to
drivers/net/virtio_net.c static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) · line 3360
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+* avoid napi_disable race which leads to deadlock. +*/ +disable_delayed_refill (vi); +cancel_delayed_work_sync (&vi->refill); +if (running) { +int i; + +for (i =0 ; i < vi->max_queue_pairs; i++) { +virtnet_napi_disable (&vi->rq[i]); +virtnet_cancel_dim (vi, &vi->rq[i].dim);
duplicates a bit of code from virtnet_rx_pause_all.
Quoted 21 lines
drivers/net/virtio_net.c +21
-0+ } + } +} + +static void virtnet_rx_resume_all (struct virtnet_info *vi) +{ +bool running =netif_running (vi->dev); +int i; + +enable_delayed_refill (vi); +for (i =0 ; i < vi->max_queue_pairs; i++) { +if (i < vi->curr_queue_pairs) { +if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL )) +schedule_delayed_work (&vi->refill,0 ); + } + +if (running) +virtnet_napi_enable (&vi->rq[i]); + } +} +
drivers/net/virtio_net.c static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) · line 3391
Quoted 10 lines
drivers/net/virtio_net.c +6
-0static void virtnet_rx_pause (struct virtnet_info *vi,struct receive_queue *rq) {bool running =netif_running (vi->dev); +/* +* Make sure refill_work does not run concurrently to +* avoid napi_disable race which leads to deadlock. +*/ +disable_delayed_refill (vi); +cancel_delayed_work_sync (&vi->refill);
Maybe rename this e.g. __virtnet_rx_pause ?
Quoted 59 lines
drivers/net/virtio_net.c +8
-9if (running) {virtnet_napi_disable (rq);virtnet_cancel_dim (vi, &rq->dim);@@ -3356,6 +3399,7 @@ static void virtnet_rx_resume(struct virtnet_info *vi, struct receive_queue *rq) {bool running =netif_running (vi->dev); +enable_delayed_refill (vi);if (!try_fill_recv (vi, rq,GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 );@@ -5959,12 +6003,12 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,if (prog)bpf_prog_add (prog, vi->max_queue_pairs -1 ); +virtnet_rx_pause_all (vi); +/* Make sure NAPI is not using any XDP TX queues for RX. */ if (netif_running (dev)) { -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_disable (&vi->rq[i]); +for (i =0 ; i < vi->max_queue_pairs; i++)virtnet_napi_tx_disable (&vi->sq[i]); - } }if (!prog) {@@ -5996,13 +6040,12 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog, vi->xdp_enabled = false; } +virtnet_rx_resume_all (vi);for (i =0 ; i < vi->max_queue_pairs; i++) {if (old_prog)bpf_prog_put (old_prog); -if (netif_running (dev)) { -virtnet_napi_enable (&vi->rq[i]); +if (netif_running (dev))virtnet_napi_tx_enable (&vi->sq[i]); - } }return 0 ;@@ -6014,11 +6057,10 @@ static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,rcu_assign_pointer (vi->rq[i].xdp_prog, old_prog); } +virtnet_rx_resume_all (vi);if (netif_running (dev)) { -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_enable (&vi->rq[i]); +for (i =0 ; i < vi->max_queue_pairs; i++)virtnet_napi_tx_enable (&vi->sq[i]); - } }if (prog)bpf_prog_sub (prog, vi->max_queue_pairs -1 );Signature · 1 line
2.43.0
PATCH v2 2/3 selftests: net: move xdp_helper to net/lib
In reply to Bui Quang MinhMove xdp_helper to net/lib to make it easier for other selftests to use the helper.
---
tools/testing/selftests/drivers/net/Makefile | 2 --
tools/testing/selftests/drivers/net/queues.py | 4 ++--
tools/testing/selftests/net/lib/.gitignore | 1 +
tools/testing/selftests/net/lib/Makefile | 1 +
tools/testing/selftests/{drivers/net => net/lib}/xdp_helper.c | 0
5 files changed, 4 insertions(+), 4 deletions(-)
rename tools/testing/selftests/{drivers/net => net/lib}/xdp_helper.c (100%)tools/testing/selftests/drivers/net/Makefile +0 -2
@@ -6,8 +6,6 @@ TEST_INCLUDES := $(wildcard lib/py/*.py) \ ../../net/net_helper.sh \ ../../net/lib.sh \ -TEST_GEN_FILES := xdp_helper - TEST_PROGS := \ netcons_basic.sh \ netcons_fragmented_msg.sh \
tools/testing/selftests/drivers/net/queues.py +2 -2
@@ -26,13 +26,13 @@ def nl_get_queues(cfg, nl, qtype='rx'): def check_xsk(cfg, nl, xdp_queue_id=0) -> None: # Probe for support - xdp = cmd(f'{cfg.test_dir / "xdp_helper"} - -', fail=False) + xdp = cmd(f'{cfg.net_lib_dir / "xdp_helper"} - -', fail=False) if xdp.ret == 255: raise KsftSkipEx('AF_XDP unsupported') elif xdp.ret > 0: raise KsftFailEx('unable to create AF_XDP socket') - with bkg(f'{cfg.test_dir / "xdp_helper"} {cfg.ifindex} {xdp_queue_id}', + with bkg(f'{cfg.net_lib_dir / "xdp_helper"} {cfg.ifindex} {xdp_queue_id}', ksft_wait=3): rx = tx = False
tools/testing/selftests/net/lib/.gitignore +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only csum +xdp_helper
tools/testing/selftests/net/lib/Makefile +1 -0
@@ -10,6 +10,7 @@ TEST_FILES += ../../../../net/ynl TEST_GEN_FILES += csum TEST_GEN_FILES += $(patsubst %.c,%.o,$(wildcard *.bpf.c)) +TEST_GEN_FILES += xdp_helper TEST_INCLUDES := $(wildcard py/*.py sh/*.sh)
tools/testing/selftests/net/lib/xdp_helper.c renamed from tools/testing/selftests/drivers/net/xdp_helper.c +0 -0
Signature · 1 line
2.43.0
PATCH v2 3/3 selftests: net: add a virtio_net deadlock selftest
In reply to Bui Quang MinhThe selftest reproduces the deadlock scenario when binding/unbinding XDP program, XDP socket, rx ring resize on virtio_net interface.
--- tools/testing/selftests/Makefile | 2 +- .../selftests/drivers/net/virtio_net/Makefile | 2 + .../selftests/drivers/net/virtio_net/config | 1 + .../drivers/net/virtio_net/lib/py/__init__.py | 16 ++++++ .../drivers/net/virtio_net/xsk_pool.py | 52 +++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/drivers/net/virtio_net/lib/py/__init__.py create mode 100755 tools/testing/selftests/drivers/net/virtio_net/xsk_pool.py
tools/testing/selftests/Makefile +1 -1
@@ -130,7 +130,7 @@ TARGETS_HOTPLUG = cpu-hotplug TARGETS_HOTPLUG += memory-hotplug # Networking tests want the net/lib target, include it automatically -ifneq ($(filter net drivers/net drivers/net/hw,$(TARGETS)),) +ifneq ($(filter net drivers/net drivers/net/hw drivers/net/virtio_net,$(TARGETS)),) ifeq ($(filter net/lib,$(TARGETS)),) INSTALL_DEP_TARGETS := net/lib endif
tools/testing/selftests/drivers/net/virtio_net/Makefile +2 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0+ OR MIT TEST_PROGS = basic_features.sh \ + xsk_pool.py \ # TEST_FILES = \
@@ -8,6 +9,7 @@ TEST_FILES = \ # TEST_INCLUDES = \ + $(wildcard lib/py/*.py ../lib/py/*.py) \ ../../../net/forwarding/lib.sh \ ../../../net/lib.sh \ #
tools/testing/selftests/drivers/net/virtio_net/config +1 -0
@@ -6,3 +6,4 @@ CONFIG_NET_L3_MASTER_DEV=y CONFIG_NET_VRF=m CONFIG_VIRTIO_DEBUG=y CONFIG_VIRTIO_NET=y +CONFIG_XDP_SOCKETS=y
tools/testing/selftests/drivers/net/virtio_net/lib/py/__init__.py new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0 + +import sys +from pathlib import Path + +KSFT_DIR = (Path(__file__).parent / "../../../../..").resolve() + +try: + sys.path.append(KSFT_DIR.as_posix()) + from net.lib.py import * + from drivers.net.lib.py import * +except ModuleNotFoundError as e: + ksft_pr("Failed importing `net` library from kernel sources") + ksft_pr(str(e)) + ktap_result(True, comment="SKIP") + sys.exit(4)
tools/testing/selftests/drivers/net/virtio_net/xsk_pool.py new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.43.0