virtio_net: Fix error unwinding of XDP initialization
Subsystems (5)
PATCH v4 1/1 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.
---
v3 -> v4
feedbacks from Jiri Pirko
- Add symmetric helper function virtnet_enable_qp to enable queues.
- Error handle: cleanup current queue pair in virtnet_enable_qp, and complete the reset queue pairs cleanup in virtnet_open.
- Fix coding style.
feedbacks from Parav Pandit
- Remove redundant debug message and white space.
v2 -> v3
feedbacks from Michael S. Tsirkin
- Remove redundant comment.
v1 -> v2
feedbacks from Michael S. Tsirkin
- squash two patches together.
--- drivers/net/virtio_net.c | 58 ++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-)
drivers/net/virtio_net.c +41 -17
@@ -1868,6 +1868,38 @@ static int virtnet_poll(struct napi_struct *napi, int budget)return received; } +static void virtnet_disable_qp (struct virtnet_info *vi,int qp_index) +{ +virtnet_napi_tx_disable (&vi->sq[qp_index].napi); +napi_disable (&vi->rq[qp_index].napi); +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq); +} + +static int virtnet_enable_qp (struct virtnet_info *vi,int qp_index) +{ +struct net_device *dev = vi->dev; +int err; + + err =xdp_rxq_info_reg (&vi->rq[qp_index].xdp_rxq, dev, qp_index, + vi->rq[qp_index].napi.napi_id); +if (err <0 ) +return err; + + err =xdp_rxq_info_reg_mem_model (&vi->rq[qp_index].xdp_rxq, +MEM_TYPE_PAGE_SHARED ,NULL ); +if (err <0 ) + goto err_xdp_reg_mem_model; + +virtnet_napi_enable (vi->rq[qp_index].vq, &vi->rq[qp_index].napi); +virtnet_napi_tx_enable (vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); + +return 0 ; + +err_xdp_reg_mem_model: +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq); +return err; +} +static int virtnet_open (struct net_device *dev) {struct virtnet_info *vi =netdev_priv (dev);
@@ -1881,22 +1913,17 @@ static int virtnet_open(struct net_device *dev)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 ); - err =xdp_rxq_info_reg (&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); + err =virtnet_enable_qp (vi, i);if (err <0 ) -return err; - - err =xdp_rxq_info_reg_mem_model (&vi->rq[i].xdp_rxq, -MEM_TYPE_PAGE_SHARED ,NULL ); -if (err <0 ) { -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); -return err; - } - -virtnet_napi_enable (vi->rq[i].vq, &vi->rq[i].napi); -virtnet_napi_tx_enable (vi, vi->sq[i].vq, &vi->sq[i].napi); + goto err_enable_qp; }return 0 ; + +err_enable_qp: +for (i--; i >=0 ; i--) +virtnet_disable_qp (vi, i); +return err; }static int virtnet_poll_tx (struct napi_struct *napi,int budget)
@@ -2305,11 +2332,8 @@ static int virtnet_close(struct net_device *dev)/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill); -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_tx_disable (&vi->sq[i].napi); -napi_disable (&vi->rq[i].napi); -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); - } +for (i =0 ; i < vi->max_queue_pairs; i++) +virtnet_disable_qp (vi, i);return 0 ; }
Signature · 1 line
2.37.1 (Apple Git-137.1)
Michael S. Tsirkin
maintainerQuoted 41 lines · Feng Liu
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 invirtnet_open.Fixes 754b8a21a96d ("virtio_net: setup xdp_rxq_info")Signed-off-by Feng Liu <[email protected]>Reviewed-by Jiri Pirko <[email protected]>---
v3 -> v4
feedbacks from Jiri Pirko
- Add symmetric helper functionvirtnet_enable_qpto enable queues.
- Error handle: cleanup current queue pair invirtnet_enable_qp, and complete the reset queue pairs cleanup invirtnet_open.
- Fix coding style.
feedbacks from Parav Pandit
- Remove redundant debug message and white space.v2 -> v3
feedbacks from Michael S. Tsirkin
- Remove redundant comment.v1 -> v2
feedbacks from Michael S. Tsirkin
- squash two patches together.--- drivers/net/virtio_net.c | 58 ++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-)drivers/net/virtio_net.c +0
-0
drivers/net/virtio_net.c static int virtnet_poll(struct napi_struct *napi, int budget) · line 1871
Quoted 4 lines
drivers/net/virtio_net.c +1
-0@@ -1868,6 +1868,38 @@ static int virtnet_poll(struct napi_struct *napi, int budget)return received; } +static void virtnet_disable_qp (struct virtnet_info *vi,int qp_index)
I am guessing _qp stands for queue pair? Let's call it virtnet_disable_queue_pair please, consistently with max_queue_pairs.
drivers/net/virtio_net.c static int virtnet_poll(struct napi_struct *napi, int budget) · line 1878
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+{ +virtnet_napi_tx_disable (&vi->sq[qp_index].napi); +napi_disable (&vi->rq[qp_index].napi); +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq); +} + +static int virtnet_enable_qp (struct virtnet_info *vi,int qp_index)
Similarly, virtnet_enable_queue_pair
Quoted 73 lines
drivers/net/virtio_net.c +33
-17+{ +struct net_device *dev = vi->dev; +int err; + + err =xdp_rxq_info_reg (&vi->rq[qp_index].xdp_rxq, dev, qp_index, + vi->rq[qp_index].napi.napi_id); +if (err <0 ) +return err; + + err =xdp_rxq_info_reg_mem_model (&vi->rq[qp_index].xdp_rxq, +MEM_TYPE_PAGE_SHARED ,NULL ); +if (err <0 ) + goto err_xdp_reg_mem_model; + +virtnet_napi_enable (vi->rq[qp_index].vq, &vi->rq[qp_index].napi); +virtnet_napi_tx_enable (vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); + +return 0 ; + +err_xdp_reg_mem_model: +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq); +return err; +} +static int virtnet_open (struct net_device *dev) {struct virtnet_info *vi =netdev_priv (dev);@@ -1881,22 +1913,17 @@ static int virtnet_open(struct net_device *dev)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 ); - err =xdp_rxq_info_reg (&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); + err =virtnet_enable_qp (vi, i);if (err <0 ) -return err; - - err =xdp_rxq_info_reg_mem_model (&vi->rq[i].xdp_rxq, -MEM_TYPE_PAGE_SHARED ,NULL ); -if (err <0 ) { -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); -return err; - } - -virtnet_napi_enable (vi->rq[i].vq, &vi->rq[i].napi); -virtnet_napi_tx_enable (vi, vi->sq[i].vq, &vi->sq[i].napi); + goto err_enable_qp; }return 0 ; + +err_enable_qp: +for (i--; i >=0 ; i--) +virtnet_disable_qp (vi, i); +return err; }static int virtnet_poll_tx (struct napi_struct *napi,int budget)@@ -2305,11 +2332,8 @@ static int virtnet_close(struct net_device *dev)/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill); -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_tx_disable (&vi->sq[i].napi); -napi_disable (&vi->rq[i].napi); -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); - } +for (i =0 ; i < vi->max_queue_pairs; i++) +virtnet_disable_qp (vi, i);return 0 ; }Signature · 1 line
2.37.1 (Apple Git-137.1)
Quoted 48 lines · Michael S. Tsirkin
External email: Use caution opening links or attachments
Quoted 44 lines · Feng Liu
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 invirtnet_open.Fixes 754b8a21a96d ("virtio_net: setup xdp_rxq_info")Signed-off-by Feng Liu <[email protected]>Reviewed-by Jiri Pirko <[email protected]>---
v3 -> v4
feedbacks from Jiri Pirko
- Add symmetric helper functionvirtnet_enable_qpto enable queues.
- Error handle: cleanup current queue pair invirtnet_enable_qp, and complete the reset queue pairs cleanup invirtnet_open.
- Fix coding style.
feedbacks from Parav Pandit
- Remove redundant debug message and white space.v2 -> v3
feedbacks from Michael S. Tsirkin
- Remove redundant comment.v1 -> v2
feedbacks from Michael S. Tsirkin
- squash two patches together.--- drivers/net/virtio_net.c | 58 ++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-)drivers/net/virtio_net.c +0
-0@@ -1868,6 +1868,38 @@ static int virtnet_poll(struct napi_struct *napi, int budget)return received; }
Quoted 6 lines
drivers/net/virtio_net.c static int virtnet_poll(struct napi_struct *napi, int budget) · line 1871
Quoted 1 line
drivers/net/virtio_net.c +1
-0+static void virtnet_disable_qp (struct virtnet_info *vi,int qp_index)I am guessing
_qpstands for queue pair? Let's call itvirtnet_disable_queue_pairplease, consistently withmax_queue_pairs.
Yes, qp stands for queue pair
will do, thanks
Quoted 10 lines
drivers/net/virtio_net.c static int virtnet_poll(struct napi_struct *napi, int budget) · line 1878
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+{ +virtnet_napi_tx_disable (&vi->sq[qp_index].napi); +napi_disable (&vi->rq[qp_index].napi); +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq); +} + +static int virtnet_enable_qp (struct virtnet_info *vi,int qp_index)Similarly,
virtnet_enable_queue_pair
will do, thanks
Quoted 74 lines
Quoted 73 lines
drivers/net/virtio_net.c +33
-18+{ +struct net_device *dev = vi->dev; +int err; + + err =xdp_rxq_info_reg (&vi->rq[qp_index].xdp_rxq, dev, qp_index, + vi->rq[qp_index].napi.napi_id); +if (err <0 ) +return err; + + err =xdp_rxq_info_reg_mem_model (&vi->rq[qp_index].xdp_rxq, +MEM_TYPE_PAGE_SHARED ,NULL ); +if (err <0 ) + goto err_xdp_reg_mem_model; + +virtnet_napi_enable (vi->rq[qp_index].vq, &vi->rq[qp_index].napi); +virtnet_napi_tx_enable (vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); + +return 0 ; + +err_xdp_reg_mem_model: +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq); +return err; +} +static int virtnet_open (struct net_device *dev) {struct virtnet_info *vi =netdev_priv (dev);@@ -1881,22 +1913,17 @@ static int virtnet_open(struct net_device *dev)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 ); - err =xdp_rxq_info_reg (&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); + err =virtnet_enable_qp (vi, i);if (err <0 ) -return err; - - err =xdp_rxq_info_reg_mem_model (&vi->rq[i].xdp_rxq, -MEM_TYPE_PAGE_SHARED ,NULL ); -if (err <0 ) { -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); -return err; - } - -virtnet_napi_enable (vi->rq[i].vq, &vi->rq[i].napi); -virtnet_napi_tx_enable (vi, vi->sq[i].vq, &vi->sq[i].napi); + goto err_enable_qp; }return 0 ; + +err_enable_qp: +for (i--; i >=0 ; i--) +virtnet_disable_qp (vi, i); +return err; }static int virtnet_poll_tx (struct napi_struct *napi,int budget)@@ -2305,11 +2332,8 @@ static int virtnet_close(struct net_device *dev)/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill); -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_tx_disable (&vi->sq[i].napi); -napi_disable (&vi->rq[i].napi); -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); - } +for (i =0 ; i < vi->max_queue_pairs; i++) +virtnet_disable_qp (vi, i);return 0 ; } --2.37.1 (Apple Git-137.1)
Quoted 63 lines · Feng Liu
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 invirtnet_open.Fixes 754b8a21a96d ("virtio_net: setup xdp_rxq_info")Signed-off-by Feng Liu <[email protected]>Reviewed-by Jiri Pirko <[email protected]>---
v3 -> v4
feedbacks from Jiri Pirko
- Add symmetric helper functionvirtnet_enable_qpto enable queues.
- Error handle: cleanup current queue pair invirtnet_enable_qp, and complete the reset queue pairs cleanup invirtnet_open.
- Fix coding style.
feedbacks from Parav Pandit
- Remove redundant debug message and white space.v2 -> v3
feedbacks from Michael S. Tsirkin
- Remove redundant comment.v1 -> v2
feedbacks from Michael S. Tsirkin
- squash two patches together.--- drivers/net/virtio_net.c | 58 ++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-)drivers/net/virtio_net.c +19
-0@@ -1868,6 +1868,38 @@ static int virtnet_poll(struct napi_struct *napi, int budget)return received; } +static void virtnet_disable_qp (struct virtnet_info *vi,int qp_index) +{ +virtnet_napi_tx_disable (&vi->sq[qp_index].napi); +napi_disable (&vi->rq[qp_index].napi); +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq); +} + +static int virtnet_enable_qp (struct virtnet_info *vi,int qp_index) +{ +struct net_device *dev = vi->dev; +int err; + + err =xdp_rxq_info_reg (&vi->rq[qp_index].xdp_rxq, dev, qp_index, + vi->rq[qp_index].napi.napi_id); +if (err <0 ) +return err; + + err =xdp_rxq_info_reg_mem_model (&vi->rq[qp_index].xdp_rxq, +MEM_TYPE_PAGE_SHARED ,NULL );
drivers/net/virtio_net.c static int virtnet_poll(struct napi_struct *napi, int budget) · line 1899
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+if (err <0 ) + goto err_xdp_reg_mem_model; + +virtnet_napi_enable (vi->rq[qp_index].vq, &vi->rq[qp_index].napi); +virtnet_napi_tx_enable (vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi); + +return 0 ; + +err_xdp_reg_mem_model: +xdp_rxq_info_unreg (&vi->rq[qp_index].xdp_rxq);
Is it really necessary to call 'xdp_rxq_info_unreg()' from here?
It seems there is a risk of calling that function twice if 'xdp_rxq_info_reg_mem_model()" fails.
For example in the following scenario:
1. We call 'virtnet_enable_qp()' from 'virtnet_open()'
2. 'xdp_rxq_info_reg()' succeeds.
3. 'xdp_rxq_info_reg_mem_model()' fails, so we go to the label "err_xdp_info_reg_mem_model".
4. 'xdp_rxq_info_unreg()' is called.
5. Register state of 'xdp_rxq' changes to 'REG_STATE_UNREGISTERED'.
6. 'virtnet_enable_qp()' returns an error.
7. In 'virtnet_open()' we go to the "err_enable_qp" label.
8. 'virtnet_disable_qp()' is called.
9. 'xdp_rxq_info_unreg()' is called for the second time on the xdp_rxq which is already in state 'REG_STATE_UNREGISTERED'.
10. The following warning from 'xdp_rxq_info_unreg_mem_model' can be displayed:
if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
WARN(1, "Missing register, driver bug"); return;
}
I think the 'xdp_rxq_info_unreg()' can be called only once for the same 'xdp_rxq'. I believe we should either:
- remove that call from 'virtnet_enable_qp()',
- or use the following API in 'virtnet_disable_qp()':
'xdp_rxq_info_is_reg()'
to check if the xdp_rxq is actually registered.
Thanks,
Michal
Quoted 54 lines
drivers/net/virtio_net.c +12
-17+return err; +} +static int virtnet_open (struct net_device *dev) {struct virtnet_info *vi =netdev_priv (dev);@@ -1881,22 +1913,17 @@ static int virtnet_open(struct net_device *dev)if (!try_fill_recv (vi, &vi->rq[i],GFP_KERNEL ))schedule_delayed_work (&vi->refill,0 ); - err =xdp_rxq_info_reg (&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id); + err =virtnet_enable_qp (vi, i);if (err <0 ) -return err; - - err =xdp_rxq_info_reg_mem_model (&vi->rq[i].xdp_rxq, -MEM_TYPE_PAGE_SHARED ,NULL ); -if (err <0 ) { -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); -return err; - } - -virtnet_napi_enable (vi->rq[i].vq, &vi->rq[i].napi); -virtnet_napi_tx_enable (vi, vi->sq[i].vq, &vi->sq[i].napi); + goto err_enable_qp; }return 0 ; + +err_enable_qp: +for (i--; i >=0 ; i--) +virtnet_disable_qp (vi, i); +return err; }static int virtnet_poll_tx (struct napi_struct *napi,int budget)@@ -2305,11 +2332,8 @@ static int virtnet_close(struct net_device *dev)/* Make sure refill_work doesn't re-enable napi! */ cancel_delayed_work_sync (&vi->refill); -for (i =0 ; i < vi->max_queue_pairs; i++) { -virtnet_napi_tx_disable (&vi->sq[i].napi); -napi_disable (&vi->rq[i].napi); -xdp_rxq_info_unreg (&vi->rq[i].xdp_rxq); - } +for (i =0 ; i < vi->max_queue_pairs; i++) +virtnet_disable_qp (vi, i);return 0 ; }Signature · 1 line
2.37.1 (Apple Git-137.1)