Skip to content

Commit 5306623

Browse files
Feng Liudavem330
authored andcommitted
virtio_net: Fix error unwinding of XDP initialization
When initializing XDP in virtnet_open(), some rq xdp initialization may hit an error causing net device open failed. However, previous rqs have already initialized XDP and enabled NAPI, which is not the expected behavior. Need to roll back the previous rq initialization to avoid leaks in error unwinding of init code. Also extract helper functions of disable and enable queue pairs. Use newly introduced disable helper function in error unwinding and virtnet_close. Use enable helper function in virtnet_open. Fixes: 754b8a2 ("virtio_net: setup xdp_rxq_info") Signed-off-by: Feng Liu <feliu@nvidia.com> Reviewed-by: Jiri Pirko <jiri@nvidia.com> Reviewed-by: William Tu <witu@nvidia.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com> Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6ead9c9 commit 5306623

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

drivers/net/virtio_net.c

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,38 @@ static int virtnet_poll(struct napi_struct *napi, int budget)
18681868
return received;
18691869
}
18701870

1871+
static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
1872+
{
1873+
virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
1874+
napi_disable(&vi->rq[qp_index].napi);
1875+
xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
1876+
}
1877+
1878+
static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
1879+
{
1880+
struct net_device *dev = vi->dev;
1881+
int err;
1882+
1883+
err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
1884+
vi->rq[qp_index].napi.napi_id);
1885+
if (err < 0)
1886+
return err;
1887+
1888+
err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
1889+
MEM_TYPE_PAGE_SHARED, NULL);
1890+
if (err < 0)
1891+
goto err_xdp_reg_mem_model;
1892+
1893+
virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
1894+
virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
1895+
1896+
return 0;
1897+
1898+
err_xdp_reg_mem_model:
1899+
xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
1900+
return err;
1901+
}
1902+
18711903
static int virtnet_open(struct net_device *dev)
18721904
{
18731905
struct virtnet_info *vi = netdev_priv(dev);
@@ -1881,22 +1913,20 @@ static int virtnet_open(struct net_device *dev)
18811913
if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
18821914
schedule_delayed_work(&vi->refill, 0);
18831915

1884-
err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1916+
err = virtnet_enable_queue_pair(vi, i);
18851917
if (err < 0)
1886-
return err;
1887-
1888-
err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
1889-
MEM_TYPE_PAGE_SHARED, NULL);
1890-
if (err < 0) {
1891-
xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
1892-
return err;
1893-
}
1894-
1895-
virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1896-
virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1918+
goto err_enable_qp;
18971919
}
18981920

18991921
return 0;
1922+
1923+
err_enable_qp:
1924+
disable_delayed_refill(vi);
1925+
cancel_delayed_work_sync(&vi->refill);
1926+
1927+
for (i--; i >= 0; i--)
1928+
virtnet_disable_queue_pair(vi, i);
1929+
return err;
19001930
}
19011931

19021932
static int virtnet_poll_tx(struct napi_struct *napi, int budget)
@@ -2305,11 +2335,8 @@ static int virtnet_close(struct net_device *dev)
23052335
/* Make sure refill_work doesn't re-enable napi! */
23062336
cancel_delayed_work_sync(&vi->refill);
23072337

2308-
for (i = 0; i < vi->max_queue_pairs; i++) {
2309-
virtnet_napi_tx_disable(&vi->sq[i].napi);
2310-
napi_disable(&vi->rq[i].napi);
2311-
xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
2312-
}
2338+
for (i = 0; i < vi->max_queue_pairs; i++)
2339+
virtnet_disable_queue_pair(vi, i);
23132340

23142341
return 0;
23152342
}

0 commit comments

Comments
 (0)