Skip to contentSkip to conversation

vhost: keep vring addresses stable while the backend is attached

PATCH In review Jia Jia 3 participants 4 messages Sep 19 – Sep 20

Subsystems (5)
lore
Share & tools
lore

Jia Jia

author
#0 lore raw

PATCH 1/1 vhost: keep vring addresses stable while the backend is attached

vhost-scsi keeps the response and bounce-buffer iovecs until the backend completes the command. If userspace changes the vring addresses while the command is in flight, the completion data is copied through the old iovecs while vhost_add_used() updates the new used ring. The guest then no longer sees the completion.

Reject changes to the vring addresses while a backend is attached to the queue. Keep accepting the current addresses so userspace can update VHOST_VRING_F_LOG or log_guest_addr without stopping the backend. Queues that have not been attached retain the existing setup behavior; userspace must detach the backend before changing their addresses.

Fixes 057cbf49a1f0 ("tcm_vhost: Initial merge for vhost level target fabric driver")
---
 drivers/vhost/vhost.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/vhost/vhost.c +11 -0
index 44cac11b68d2..074590838de5 100644
@@ -2150,6 +2150,17 @@ static long vhost_vring_set_addr(struct vhost_dev *d,
 				a.flags & (0x1 << VHOST_VRING_F_LOG),
 				a.log_guest_addr))
 			return -EINVAL;
+
+		/*
+		 * Commands may retain iovecs derived from the current ring
+		 * addresses.  Keep the addresses unchanged while the backend
+		 * is attached.
+		 */
+		if ((vq->desc || vq->avail || vq->used) &&
+		    (vq->desc != (void __user *)(unsigned long)a.desc_user_addr ||
+		     vq->avail != (void __user *)(unsigned long)a.avail_user_addr ||
+		     vq->used != (void __user *)(unsigned long)a.used_user_addr))
+			return -EBUSY;
 	}
 
 	vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

vhost-scsi keeps the response and bounce-buffer iovecs until the backend completes the command. If userspace changes the vring addresses while the command is in flight, the completion data is copied through the old iovecs while vhost_add_used() updates the new used ring. The guest then no longer sees the completion.

Quoted 8 lines

Reject changes to the vring addresses while a backend is attached to the queue. Keep accepting the current addresses so userspace can update VHOST_VRING_F_LOG or log_guest_addr without stopping the backend. Queues that have not been attached retain the existing setup behavior; userspace must detach the backend before changing their addresses.

Fixes 057cbf49a1f0 ("tcm_vhost: Initial merge for vhost level target fabric driver")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Jia Jia
Quoted 5 lines · Jia Jia

vhost-scsi keeps the response and bounce-buffer iovecs until the backend completes the command. If userspace changes the vring addresses while the command is in flight, the completion data is copied through the old iovecs while vhost_add_used() updates the new used ring. The guest then no longer sees the completion.

I do not get what all this is trying to say. if you break it you get to keep both pieces. when is this a real problem?

Quoted 35 lines

Reject changes to the vring addresses while a backend is attached to the queue. Keep accepting the current addresses so userspace can update VHOST_VRING_F_LOG or log_guest_addr without stopping the backend. Queues that have not been attached retain the existing setup behavior; userspace must detach the backend before changing their addresses.

Fixes 057cbf49a1f0 ("tcm_vhost: Initial merge for vhost level target fabric driver")
---
 drivers/vhost/vhost.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/vhost/vhost.c +11 -0
index 44cac11b68d2..074590838de5 100644
@@ -2150,6 +2150,17 @@ static long vhost_vring_set_addr(struct vhost_dev *d,
 				a.flags & (0x1 << VHOST_VRING_F_LOG),
 				a.log_guest_addr))
 			return -EINVAL;
+
+		/*
+		 * Commands may retain iovecs derived from the current ring
+		 * addresses.  Keep the addresses unchanged while the backend
+		 * is attached.
+		 */
+		if ((vq->desc || vq->avail || vq->used) &&
+		    (vq->desc != (void __user *)(unsigned long)a.desc_user_addr ||
+		     vq->avail != (void __user *)(unsigned long)a.avail_user_addr ||
+		     vq->used != (void __user *)(unsigned long)a.used_user_addr))
+			return -EBUSY;
 	}
 
 	vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));
author
#3 lore raw
Quoted 11 lines · Jia Jia
Quoted 5 lines

vhost-scsi keeps the response and bounce-buffer iovecs until the backend completes the command. If userspace changes the vring addresses while the command is in flight, the completion data is copied through the old iovecs while vhost_add_used() updates the new used ring. The guest then no longer sees the completion.

I do not get what all this is trying to say. if you break it you get to keep both pieces. when is this a real problem?

Before running the test, I checked the virtio spec and vhost UAPI and did not find an explicit rule
prohibiting changes to vring addresses while a backend is attached. That is why I ran the directed test.
I see what you mean now. I'll drop this patch.

Quoted 36 lines
Quoted 35 lines

Reject changes to the vring addresses while a backend is attached to the queue. Keep accepting the current addresses so userspace can update VHOST_VRING_F_LOG or log_guest_addr without stopping the backend. Queues that have not been attached retain the existing setup behavior; userspace must detach the backend before changing their addresses.

Fixes 057cbf49a1f0 ("tcm_vhost: Initial merge for vhost level target fabric driver")
---
 drivers/vhost/vhost.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/vhost/vhost.c +11 -0
index 44cac11b68d2..074590838de5 100644
@@ -2150,6 +2150,17 @@ static long vhost_vring_set_addr(struct vhost_dev *d,
                              a.flags & (0x1 << VHOST_VRING_F_LOG),
                              a.log_guest_addr))
                      return -EINVAL;
+
+             /*
+              * Commands may retain iovecs derived from the current ring
+              * addresses.  Keep the addresses unchanged while the backend
+              * is attached.
+              */
+             if ((vq->desc || vq->avail || vq->used) &&
+                 (vq->desc != (void __user *)(unsigned long)a.desc_user_addr ||
+                  vq->avail != (void __user *)(unsigned long)a.avail_user_addr ||
+                  vq->used != (void __user *)(unsigned long)a.used_user_addr))
+                     return -EBUSY;
      }
 
      vq->log_used = !!(a.flags & (0x1 << VHOST_VRING_F_LOG));