Skip to contentSkip to conversation

vhost: clean up outstanding buffers before setting vring

PATCH In review Shirley Ma 2 participants 5 messages Jul 19, 2011 – Jul 20, 2011

Subsystems (3)
lore
Share & tools
lore

Shirley Ma

author
#0 lore raw

PATCH 1/1 vhost: clean up outstanding buffers before setting vring

The outstanding DMA buffers need to be clean up before setting vring in vhost. Otherwise the vring would be out of sync.

---

 drivers/vhost/vhost.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
drivers/vhost/vhost.c +9 -2
index c14c42b..d6315b4 100644
@@ -445,8 +445,10 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 			vhost_poll_flush(&dev->vqs[i].poll);
 		}
 		/* Wait for all lower device DMAs done. */
-		if (dev->vqs[i].ubufs)
+		if (dev->vqs[i].ubufs) {
 			vhost_ubuf_put_and_wait(dev->vqs[i].ubufs);
+			kfree(dev->vqs[i].ubufs);
+		}
 
 		/* Signal guest as appropriate. */
 		vhost_zerocopy_signal_used(&dev->vqs[i]);
@@ -651,6 +653,12 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
 	vq = d->vqs + idx;
 
 	mutex_lock(&vq->mutex);
+	/* Wait for all lower device DMAs done. */
+	if (vq->ubufs)
+		vhost_ubuf_put_and_wait(vq->ubufs);
+
+	/* Signal guest as appropriate. */
+	vhost_zerocopy_signal_used(vq);
 
 	switch (ioctl) {
 	case VHOST_SET_VRING_NUM:
@@ -1592,7 +1600,6 @@ void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
 {
 	kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
 	wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
-	kfree(ubufs);
 }
 
 void vhost_zerocopy_callback(void *arg)
#1 lore raw
Quoted 4 lines · Shirley Ma

The outstanding DMA buffers need to be clean up before setting vring in vhost. Otherwise the vring would be out of sync.

I suspect what is missing is calling
vhost_zerocopy_signal_used then?

If yes we probably should do it after
changing the backend, not on vring set.

Quoted 22 lines

---

 drivers/vhost/vhost.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
drivers/vhost/vhost.c +3 -1
index c14c42b..d6315b4 100644
@@ -445,8 +445,10 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 			vhost_poll_flush(&dev->vqs[i].poll);
 		}
 		/* Wait for all lower device DMAs done. */
-		if (dev->vqs[i].ubufs)
+		if (dev->vqs[i].ubufs) {
 			vhost_ubuf_put_and_wait(dev->vqs[i].ubufs);
+			kfree(dev->vqs[i].ubufs);
+		}
 
 		/* Signal guest as appropriate. */
 		vhost_zerocopy_signal_used(&dev->vqs[i]);

drivers/vhost/vhost.c static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp) · line 658

Quoted 6 lines
drivers/vhost/vhost.c +3 -0
@@ -651,6 +653,12 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
 	vq = d->vqs + idx;
 
 	mutex_lock(&vq->mutex);
+	/* Wait for all lower device DMAs done. */
+	if (vq->ubufs)
+		vhost_ubuf_put_and_wait(vq->ubufs);

Could you elaborate on the problem you observe please? At least in theory, existing code flushes outstanding requests when backend is changed.
And since vring set verifies no backend is active, we should be fine?

drivers/vhost/vhost.c void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs) · line 1595

Quoted 11 lines
drivers/vhost/vhost.c +3 -1
+
+	/* Signal guest as appropriate. */
+	vhost_zerocopy_signal_used(vq);
 
 	switch (ioctl) {
 	case VHOST_SET_VRING_NUM:
@@ -1592,7 +1600,6 @@ void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs)
 {
 	kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
 	wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
-	kfree(ubufs);

Won't this leak memory when ubufs are switched in vhost_net_set_backend?

Quoted 6 lines
drivers/vhost/vhost.c +0 -0
 }
 
 void vhost_zerocopy_callback(void *arg)
author
#2 lore raw
Quoted 5 lines · Michael S. Tsirkin
Quoted 1 line · Shirley Ma

The outstanding DMA buffers need to be clean up before setting vring

in

Quoted 2 lines

vhost. Otherwise the vring would be out of sync.

Quoted 7 lines
Quoted 1 line

I suspect what is missing is calling
vhost_zerocopy_signal_used then?

If yes we probably should do it after
changing the backend, not on vring set.

I think vhost_zerocopy_signal_used might not be sufficient. But we can test it out by remove/reloading the guest virtio_net module.

Quoted 30 lines

drivers/vhost/vhost.c static long vhost_set_vring(struct vhost_dev

Quoted 22 lines

---

 drivers/vhost/vhost.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
drivers/vhost/vhost.c +3 -1
index c14c42b..d6315b4 100644
@@ -445,8 +445,10 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
                      vhost_poll_flush(&dev->vqs[i].poll);
              }
              /* Wait for all lower device DMAs done. */
-             if (dev->vqs[i].ubufs)
+             if (dev->vqs[i].ubufs) {
                      vhost_ubuf_put_and_wait(dev->vqs[i].ubufs);
+                     kfree(dev->vqs[i].ubufs);
+             }
 
              /* Signal guest as appropriate. */
              vhost_zerocopy_signal_used(&dev->vqs[i]);
@@ -651,6 +653,12 @@ static long vhost_set_vring(struct vhost_dev

*d, int ioctl, void __user *argp)

drivers/vhost/vhost.c static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp) · line 658

Quoted 6 lines
drivers/vhost/vhost.c +3 -0
      vq = d->vqs + idx;
 
      mutex_lock(&vq->mutex);
+     /* Wait for all lower device DMAs done. */
+     if (vq->ubufs)
+             vhost_ubuf_put_and_wait(vq->ubufs);
Quoted 5 lines

Could you elaborate on the problem you observe please? At least in theory, existing code flushes outstanding requests when backend is changed.
And since vring set verifies no backend is active, we should be fine?

The problem encounters when guest rmmod virtio_net module, then reload the module, and configure the interface, it complains about some ring id is not a head. With this patch, the problem is solved.

Quoted 9 lines

drivers/vhost/vhost.c void vhost_ubuf_put_and_wait(struct

Quoted 8 lines
drivers/vhost/vhost.c +3 -0
+
+     /* Signal guest as appropriate. */
+     vhost_zerocopy_signal_used(vq);
 
      switch (ioctl) {
      case VHOST_SET_VRING_NUM:
@@ -1592,7 +1600,6 @@ void vhost_ubuf_put_and_wait(struct

vhost_ubuf_ref *ubufs)

Quoted 7 lines

drivers/vhost/vhost.c void vhost_ubuf_put_and_wait(struct vhost_ubuf_ref *ubufs) · line 1595

Quoted 4 lines
drivers/vhost/vhost.c +0 -1
 {
      kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
      wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
-     kfree(ubufs);

Won't this leak memory when ubufs are switched in vhost_net_set_backend?

Right, I forgot to check net.c, whenever it calls vhot_ubuf_put_and_wait, it should call kfree(ubufs).

#3 lore raw
Quoted 8 lines · Shirley Ma
Quoted 7 lines · Michael S. Tsirkin
Quoted 1 line · Shirley Ma

The outstanding DMA buffers need to be clean up before setting vring

in

Quoted 3 lines

vhost. Otherwise the vring would be out of sync.

Quoted 7 lines
Quoted 5 lines

I suspect what is missing is calling
vhost_zerocopy_signal_used then?

If yes we probably should do it after
changing the backend, not on vring set.

I think vhost_zerocopy_signal_used might not be sufficient.

If not, I'd like to understand what the root cause of the problem is.

Quoted 2 lines

But we can
test it out by remove/reloading the guest virtio_net module.

Well, try out something like the below patch then.

Quoted 4 lines

The problem encounters when guest rmmod virtio_net module, then reload the module, and configure the interface, it complains about some ring id is not a head.

OK, good, such a problem decription belongs in the patch commit log.

Quoted 1 line

With this patch, the problem is solved.

Additional info you want to put in the commit log is what in the code triggers the problem and how your patch fixes it.

Quoted 14 lines
Quoted 14 lines

Review void vhost_ubuf_put_and_wait(struct

Quoted 8 lines
(quoted diff) +3 -0
+
+     /* Signal guest as appropriate. */
+     vhost_zerocopy_signal_used(vq);
 
      switch (ioctl) {
      case VHOST_SET_VRING_NUM:
@@ -1592,7 +1600,6 @@ void vhost_ubuf_put_and_wait(struct

vhost_ubuf_ref *ubufs)

Quoted 4 lines
(quoted diff) +0 -1
 {
      kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
      wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
-     kfree(ubufs);
Quoted 6 lines
Quoted 2 lines

Won't this leak memory when ubufs are switched in
vhost_net_set_backend?

Right, I forgot to check net.c, whenever it calls vhot_ubuf_put_and_wait, it should call kfree(ubufs).

-->

vhost-net: update used ring on backend change

On backend change, we flushed out outstanding skbs but forgot to update the used ring. Do that to avoid losing heads.

--

drivers/vhost/net.c +5 -1
index 70ac604..248b250 100644
@@ -711,8 +711,12 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
 
 	mutex_unlock(&vq->mutex);
 
-	if (oldubufs)
+	if (oldubufs) {
 		vhost_ubuf_put_and_wait(oldubufs);
+		mutex_lock(&vq->mutex);
+		vhost_zerocopy_signal_used(vq);
+		mutex_unlock(&vq->mutex);
+	}
 
 	if (oldsock) {
 		vhost_net_flush_vq(n, index);
Signature · 1 line
MST
author
#4 lore raw
Quoted 21 lines · Michael S. Tsirkin
Quoted 3 lines · Shirley Ma
Quoted 2 lines · Michael S. Tsirkin
Quoted 1 line · Shirley Ma

The outstanding DMA buffers need to be clean up before setting

vring

Quoted 5 lines
Quoted 5 lines

in

Quoted 3 lines

vhost. Otherwise the vring would be out of sync.

Quoted 7 lines
Quoted 5 lines

I suspect what is missing is calling
vhost_zerocopy_signal_used then?

If yes we probably should do it after
changing the backend, not on vring set.

I think vhost_zerocopy_signal_used might not be sufficient.

If not, I'd like to understand what the root cause of the problem is.

Quoted 6 lines
Quoted 2 lines

But we can
test it out by remove/reloading the guest virtio_net module.

Well, try out something like the below patch then.

From the test results, below patch solves the problem. You can check in

this.

Quoted 8 lines
Quoted 2 lines

The problem encounters when guest rmmod virtio_net module, then

reload

Quoted 1 line

the module, and configure the interface, it complains about some

ring id

Quoted 1 line

is not a head.

OK, good, such a problem decription belongs in the patch commit log.

You can add this description in the change log. When removing and reloading KVM guest virtio_net module, it complains vring data is NULL. The vring is out of sync between vhost and virtio_net.

Quoted 60 lines
Quoted 1 line

With this patch, the problem is solved.

Additional info you want to put in the commit log is what in the code triggers the problem and how your patch fixes it.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
(quoted diff) +3 -0
+
+     /* Signal guest as appropriate. */
+     vhost_zerocopy_signal_used(vq);
Quoted 7 lines
Quoted 7 lines

Review void vhost_ubuf_put_and_wait(struct

Quoted 3 lines
      switch (ioctl) {
      case VHOST_SET_VRING_NUM:
(quoted diff) +0 -0
@@ -1592,7 +1600,6 @@ void vhost_ubuf_put_and_wait(struct

vhost_ubuf_ref *ubufs)

Quoted 3 lines
(quoted diff) +0 -0
 {
      kref_put(&ubufs->kref, vhost_zerocopy_done_signal);
      wait_event(ubufs->wait, !

atomic_read(&ubufs->kref.refcount));

Quoted 8 lines
Quoted 4 lines

Review void vhost_ubuf_put_and_wait(struct · line 1595

Quoted 1 line
(quoted diff) +0 -1
-     kfree(ubufs);

Won't this leak memory when ubufs are switched in
vhost_net_set_backend?

Right, I forgot to check net.c, whenever it calls vhot_ubuf_put_and_wait, it should call kfree(ubufs).

-->

vhost-net: update used ring on backend change

On backend change, we flushed out outstanding skbs but forgot to update the used ring. Do that to avoid losing heads.

--

drivers/vhost/net.c +5 -1
index 70ac604..248b250 100644
@@ -711,8 +711,12 @@ static long vhost_net_set_backend(struct
 vhost_net *n, unsigned index, int fd)
 
        mutex_unlock(&vq->mutex);
 
-       if (oldubufs)
+       if (oldubufs) {
                vhost_ubuf_put_and_wait(oldubufs);
+               mutex_lock(&vq->mutex);
+               vhost_zerocopy_signal_used(vq);
+               mutex_unlock(&vq->mutex);
+       }
 
        if (oldsock) {
                vhost_net_flush_vq(n, index);