vsock: improve tap delivery accuracy
Subsystems (6)
PATCH 1/1 vsock: improve tap delivery accuracy
From: Cong Wang <[email protected]>
When virtqueue_add_sgs() fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to move virtio_transport_deliver_tap_pkt() down after all possible failures.
--- net/vmw_vsock/virtio_transport.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +2 -3
@@ -109,9 +109,6 @@ virtio_transport_send_pkt_work(struct work_struct *work)if (!skb)break ; -virtio_transport_deliver_tap_pkt (skb); - reply =virtio_vsock_skb_reply (skb); -sg_init_one (&hdr,virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb))); sgs[out_sg++] = &hdr;if (skb->len >0 ) {
@@ -128,6 +125,8 @@ virtio_transport_send_pkt_work(struct work_struct *work)break ; } +virtio_transport_deliver_tap_pkt (skb); + reply =virtio_vsock_skb_reply (skb);if (reply) {struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX ];int val;
Signature · 6 lines
2.34.1 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 7 lines · Cong Wang
From: Cong Wang <[email protected]>
When
virtqueue_add_sgs()fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to movevirtio_transport_deliver_tap_pkt()down after all possible failures.
Quoted 5 lines
Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")Cc Stefan Hajnoczi <[email protected]>Cc Stefano Garzarella <[email protected]>Cc Bobby Eshleman <[email protected]>Signed-off-by Cong Wang <[email protected]>
Stefan Hajnoczi
maintainerQuoted 31 lines · Cong Wang
From: Cong Wang <[email protected]>
When
virtqueue_add_sgs()fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to movevirtio_transport_deliver_tap_pkt()down after all possible failures.Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")Cc Stefan Hajnoczi <[email protected]>Cc Stefano Garzarella <[email protected]>Cc Bobby Eshleman <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/vmw_vsock/virtio_transport.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport.c +0
-3@@ -109,9 +109,6 @@ virtio_transport_send_pkt_work(struct work_struct *work)if (!skb)break ; -virtio_transport_deliver_tap_pkt (skb); - reply =virtio_vsock_skb_reply (skb); -sg_init_one (&hdr,virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb))); sgs[out_sg++] = &hdr;if (skb->len >0 ) {
net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 129
Quoted 5 lines
net/vmw_vsock/virtio_transport.c +2
-0@@ -128,6 +125,8 @@ virtio_transport_send_pkt_work(struct work_struct *work)break ; } +virtio_transport_deliver_tap_pkt (skb); + reply =virtio_vsock_skb_reply (skb);
I don't remember the reason for the ordering, but I'm pretty sure it was deliberate. Probably because the payload buffers could be freed as soon as virtqueue_add_sgs() is called.
If that's no longer true with Bobby's skbuff code, then maybe it's safe to monitor packets after they have been sent.
Stefan
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 42 lines · Stefan Hajnoczi
Quoted 31 lines · Cong Wang
From: Cong Wang <[email protected]>
When
virtqueue_add_sgs()fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to movevirtio_transport_deliver_tap_pkt()down after all possible failures.Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")Cc Stefan Hajnoczi <[email protected]>Cc Stefano Garzarella <[email protected]>Cc Bobby Eshleman <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/vmw_vsock/virtio_transport.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport.c +0
-3@@ -109,9 +109,6 @@ virtio_transport_send_pkt_work(struct work_struct *work)if (!skb)break ; -virtio_transport_deliver_tap_pkt (skb); - reply =virtio_vsock_skb_reply (skb); -sg_init_one (&hdr,virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb))); sgs[out_sg++] = &hdr;if (skb->len >0 ) {net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 129
Quoted 5 lines
net/vmw_vsock/virtio_transport.c +2
-0@@ -128,6 +125,8 @@ virtio_transport_send_pkt_work(struct work_struct *work)break ; } +virtio_transport_deliver_tap_pkt (skb); + reply =virtio_vsock_skb_reply (skb);I don't remember the reason for the ordering, but I'm pretty sure it was deliberate. Probably because the payload buffers could be freed as soon as
virtqueue_add_sgs()is called.
Quoted 4 lines
If that's no longer true with Bobby's skbuff code, then maybe it's safe to monitor packets after they have been sent.
Stefan
Hey Stefan,
Unfortunately, skbuff doesn't change that behavior.
If I understand correctly, the problem flow you are describing would be something like this:
Thread 0 Thread 1
guest:virtqueue_add_sgs()[@send_pkt_work]
host:vhost_vq_get_desc()[@handle_tx_kick] host:vhost_add_used() host:vhost_signal() guest:virtqueue_get_buf()[@tx_work] guest:consume_skb()
guest:deliver_tap_pkt()[@send_pkt_work]
^ use-after-free
Which I guess is possible because the receiver can consume the new scatterlist during the processing kicked off for a previous batch? (doesn't have to wait for the subsequent kick)
Best,
Bobby
Stefano Garzarella
maintainerQuoted 36 lines · Bobby Eshleman
Quoted 35 lines · Stefan Hajnoczi
Quoted 34 lines · Cong Wang
From: Cong Wang <[email protected]>
When
virtqueue_add_sgs()fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to movevirtio_transport_deliver_tap_pkt()down after all possible failures.Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")Cc Stefan Hajnoczi <[email protected]>Cc Stefano Garzarella <[email protected]>Cc Bobby Eshleman <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/vmw_vsock/virtio_transport.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport.c +0
-3@@ -109,9 +109,6 @@ virtio_transport_send_pkt_work(struct work_struct *work)if (!skb)break ; -virtio_transport_deliver_tap_pkt (skb); - reply =virtio_vsock_skb_reply (skb); -sg_init_one (&hdr,virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb))); sgs[out_sg++] = &hdr;if (skb->len >0 ) {@@ -128,6 +125,8 @@ virtio_transport_send_pkt_work(struct work_struct *work)break ; }
Quoted 1 line
Quoted 1 line
net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 128
Quoted 1 line
net/vmw_vsock/virtio_transport.c +1
-0+virtio_transport_deliver_tap_pkt (skb);
I would move only the virtio_transport_deliver_tap_pkt(), virtio_vsock_skb_reply() is not related.
Quoted 27 lines
Quoted 10 lines
net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 129
Quoted 1 line
net/vmw_vsock/virtio_transport.c +1
-0+ reply =virtio_vsock_skb_reply (skb);I don't remember the reason for the ordering, but I'm pretty sure it was deliberate. Probably because the payload buffers could be freed as soon as
virtqueue_add_sgs()is called.If that's no longer true with Bobby's skbuff code, then maybe it's safe to monitor packets after they have been sent.
Stefan
Hey Stefan,
Unfortunately, skbuff doesn't change that behavior.
If I understand correctly, the problem flow you are describing would be something like this:
Thread 0 Thread 1
guest:virtqueue_add_sgs()[@send_pkt_work]host:vhost_vq_get_desc()[@handle_tx_kick] host:vhost_add_used() host:vhost_signal() guest:virtqueue_get_buf()[@tx_work] guest:consume_skb()
Quoted 6 lines
guest:deliver_tap_pkt()[@send_pkt_work]
^ use-after-freeWhich I guess is possible because the receiver can consume the new scatterlist during the processing kicked off for a previous batch? (doesn't have to wait for the subsequent kick)
This is true, but both send_pkt_work and tx_work hold tx_lock, so can they really go in parallel?
Thanks,
Stefano
Quoted 72 lines · Stefano Garzarella
Quoted 36 lines · Bobby Eshleman
Quoted 35 lines · Stefan Hajnoczi
Quoted 34 lines · Cong Wang
From: Cong Wang <[email protected]>
When
virtqueue_add_sgs()fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to movevirtio_transport_deliver_tap_pkt()down after all possible failures.Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")Cc Stefan Hajnoczi <[email protected]>Cc Stefano Garzarella <[email protected]>Cc Bobby Eshleman <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/vmw_vsock/virtio_transport.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport.c +0
-3@@ -109,9 +109,6 @@ virtio_transport_send_pkt_work(struct work_struct *work)if (!skb)break ; -virtio_transport_deliver_tap_pkt (skb); - reply =virtio_vsock_skb_reply (skb); -sg_init_one (&hdr,virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb))); sgs[out_sg++] = &hdr;if (skb->len >0 ) {@@ -128,6 +125,8 @@ virtio_transport_send_pkt_work(struct work_struct *work)break ; }Quoted 1 line
Quoted 1 line
net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 128
Quoted 1 line
net/vmw_vsock/virtio_transport.c +1
-0+virtio_transport_deliver_tap_pkt (skb);I would move only the
virtio_transport_deliver_tap_pkt(),virtio_vsock_skb_reply()is not related.Quoted 30 lines
Quoted 10 lines
net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 129
Quoted 1 line
net/vmw_vsock/virtio_transport.c +1
-0+ reply =virtio_vsock_skb_reply (skb);I don't remember the reason for the ordering, but I'm pretty sure it was deliberate. Probably because the payload buffers could be freed as soon as
virtqueue_add_sgs()is called.If that's no longer true with Bobby's skbuff code, then maybe it's safe to monitor packets after they have been sent.
Stefan
Hey Stefan,
Unfortunately, skbuff doesn't change that behavior.
If I understand correctly, the problem flow you are describing would be something like this:
Thread 0 Thread 1
guest:virtqueue_add_sgs()[@send_pkt_work]host:vhost_vq_get_desc()[@handle_tx_kick] host:vhost_add_used() host:vhost_signal() guest:virtqueue_get_buf()[@tx_work] guest:consume_skb()guest:deliver_tap_pkt()[@send_pkt_work]
^ use-after-free
Quoted 7 lines
Quoted 3 lines
Which I guess is possible because the receiver can consume the new scatterlist during the processing kicked off for a previous batch? (doesn't have to wait for the subsequent kick)
This is true, but both
send_pkt_workandtx_workholdtx_lock, so can they really go in parallel?
Oh good point, the tx_lock synchronizes it:
Thread 0 Thread 1
guest:virtqueue_add_sgs()[@send_pkt_work]
host:vhost_vq_get_desc()[@handle_tx_kick] host:vhost_add_used() host:vhost_signal() guest:mutex_lock()[@tx_work] guest:deliver_tap_pkt()[@send_pkt_work] guest:mutex_unlock() guest:virtqueue_get_buf()[@tx_work] guest:consume_skb()
I'm pretty sure this should be safe.
Best,
Bobby
Stefan Hajnoczi
maintainerQuoted 64 lines · Bobby Eshleman
Quoted 42 lines · Stefan Hajnoczi
Quoted 31 lines · Cong Wang
From: Cong Wang <[email protected]>
When
virtqueue_add_sgs()fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to movevirtio_transport_deliver_tap_pkt()down after all possible failures.Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")Cc Stefan Hajnoczi <[email protected]>Cc Stefano Garzarella <[email protected]>Cc Bobby Eshleman <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/vmw_vsock/virtio_transport.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport.c +0
-3@@ -109,9 +109,6 @@ virtio_transport_send_pkt_work(struct work_struct *work)if (!skb)break ; -virtio_transport_deliver_tap_pkt (skb); - reply =virtio_vsock_skb_reply (skb); -sg_init_one (&hdr,virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb))); sgs[out_sg++] = &hdr;if (skb->len >0 ) {net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 129
Quoted 5 lines
net/vmw_vsock/virtio_transport.c +2
-0@@ -128,6 +125,8 @@ virtio_transport_send_pkt_work(struct work_struct *work)break ; } +virtio_transport_deliver_tap_pkt (skb); + reply =virtio_vsock_skb_reply (skb);I don't remember the reason for the ordering, but I'm pretty sure it was deliberate. Probably because the payload buffers could be freed as soon as
virtqueue_add_sgs()is called.Quoted 4 lines
If that's no longer true with Bobby's skbuff code, then maybe it's safe to monitor packets after they have been sent.
Stefan
Hey Stefan,
Unfortunately, skbuff doesn't change that behavior.
If I understand correctly, the problem flow you are describing would be something like this:
Thread 0 Thread 1
guest:virtqueue_add_sgs()[@send_pkt_work]host:vhost_vq_get_desc()[@handle_tx_kick] host:vhost_add_used() host:vhost_signal() guest:virtqueue_get_buf()[@tx_work] guest:consume_skb()
Quoted 6 lines
guest:deliver_tap_pkt()[@send_pkt_work]
^ use-after-freeWhich I guess is possible because the receiver can consume the new scatterlist during the processing kicked off for a previous batch? (doesn't have to wait for the subsequent kick)
Yes, drivers must assume that the device completes request before virtqueue_add_sgs() returns. For example, the device is allowed to poll the virtqueue memory and may see the new descriptors immediately.
I haven't audited the current vsock code path to determine whether it's possible to reach consume_skb() before deliver_tap_pkt() returns, so I can't say whether it's safe or not.
Stefan
Quoted 72 lines · Stefan Hajnoczi
Quoted 64 lines · Bobby Eshleman
Quoted 42 lines · Stefan Hajnoczi
Quoted 31 lines · Cong Wang
From: Cong Wang <[email protected]>
When
virtqueue_add_sgs()fails, the skb is put back to send queue, we should not deliver the copy to tap device in this case. So we need to movevirtio_transport_deliver_tap_pkt()down after all possible failures.Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")Cc Stefan Hajnoczi <[email protected]>Cc Stefano Garzarella <[email protected]>Cc Bobby Eshleman <[email protected]>Signed-off-by Cong Wang <[email protected]>--- net/vmw_vsock/virtio_transport.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)net/vmw_vsock/virtio_transport.c +0
-3@@ -109,9 +109,6 @@ virtio_transport_send_pkt_work(struct work_struct *work)if (!skb)break ; -virtio_transport_deliver_tap_pkt (skb); - reply =virtio_vsock_skb_reply (skb); -sg_init_one (&hdr,virtio_vsock_hdr (skb),sizeof (*virtio_vsock_hdr (skb))); sgs[out_sg++] = &hdr;if (skb->len >0 ) {net/vmw_vsock/virtio_transport.c virtio_transport_send_pkt_work(struct work_struct *work) · line 129
Quoted 5 lines
net/vmw_vsock/virtio_transport.c +2
-0@@ -128,6 +125,8 @@ virtio_transport_send_pkt_work(struct work_struct *work)break ; } +virtio_transport_deliver_tap_pkt (skb); + reply =virtio_vsock_skb_reply (skb);I don't remember the reason for the ordering, but I'm pretty sure it was deliberate. Probably because the payload buffers could be freed as soon as
virtqueue_add_sgs()is called.Quoted 4 lines
If that's no longer true with Bobby's skbuff code, then maybe it's safe to monitor packets after they have been sent.
Stefan
Hey Stefan,
Unfortunately, skbuff doesn't change that behavior.
If I understand correctly, the problem flow you are describing would be something like this:
Thread 0 Thread 1
guest:virtqueue_add_sgs()[@send_pkt_work]host:vhost_vq_get_desc()[@handle_tx_kick] host:vhost_add_used() host:vhost_signal() guest:virtqueue_get_buf()[@tx_work] guest:consume_skb()Quoted 6 lines
guest:deliver_tap_pkt()[@send_pkt_work]
^ use-after-freeWhich I guess is possible because the receiver can consume the new scatterlist during the processing kicked off for a previous batch? (doesn't have to wait for the subsequent kick)
Quoted 8 lines
Yes, drivers must assume that the device completes request before
virtqueue_add_sgs()returns. For example, the device is allowed to poll the virtqueue memory and may see the new descriptors immediately.I haven't audited the current vsock code path to determine whether it's possible to reach
consume_skb()beforedeliver_tap_pkt()returns, so I can't say whether it's safe or not.
I see, thanks for the clarification.
Best,
Bobby