Skip to contentSkip to conversation

vsock: improve tap delivery accuracy

PATCH In review Cong Wang 5 participants 8 messages May 2, 2023 – May 3, 2023

Subsystems (6)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

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.

Fixes 82dfb540aeb2 ("VSOCK: Add virtio vsock vsockmon hooks")
Cc Stefan Hajnoczi <[email protected]>
Cc Stefano Garzarella <[email protected]>
Cc Bobby Eshleman <[email protected]>
---
 net/vmw_vsock/virtio_transport.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +2 -3
index e95df847176b..055678628c07 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw
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 move virtio_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]>
maintainer
#2 lore raw
In reply to Cong Wang
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 move virtio_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]>
---
 net/vmw_vsock/virtio_transport.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +0 -3
index e95df847176b..055678628c07 100644
@@ -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

#3 lore raw
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 move virtio_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]>
---
 net/vmw_vsock/virtio_transport.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +0 -3
index e95df847176b..055678628c07 100644
@@ -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

#4 lore raw
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 move virtio_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]>
---
 net/vmw_vsock/virtio_transport.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +0 -3
index e95df847176b..055678628c07 100644
@@ -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-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)

This is true, but both send_pkt_work and tx_work hold tx_lock, so can they really go in parallel?

Thanks,
Stefano

#5 lore raw
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 move virtio_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]>
---
 net/vmw_vsock/virtio_transport.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +0 -3
index e95df847176b..055678628c07 100644
@@ -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_work and tx_work hold tx_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

maintainer
#6 lore raw
In reply to Bobby Eshleman
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 move virtio_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]>
---
 net/vmw_vsock/virtio_transport.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +0 -3
index e95df847176b..055678628c07 100644
@@ -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-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)

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

#7 lore raw
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 move virtio_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]>
---
 net/vmw_vsock/virtio_transport.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
net/vmw_vsock/virtio_transport.c +0 -3
index e95df847176b..055678628c07 100644
@@ -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-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)

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() before deliver_tap_pkt() returns, so I can't say whether it's safe or not.

I see, thanks for the clarification.

Best,
Bobby