Skip to contentSkip to conversation

virtio_console: fix replug of virtio console port

PATCH New Pankaj Gupta 1 participant 5 messages Aug 13, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover virtio_console: fix replug of virtio console port

This patch series fixes the issue with unplug/replug of a port in virtio console driver which fails with an error "Error allocating inbufs\n".

Patch 1 updates the next avail index for packed ring code. Patch 2 makes use of 'virtqueue_detach_unused_buf' function to detach the unused buffers during port hotunplug time.

Tested the packed ring code with the qemu virtio 1.1 device code posted here [1]. Also, sent a patch to document the behavior in virtio spec as suggested by Michael.

Changes from v3
- Swap patch 1 with patch 2 - [Michael]
- Added acked-by tag by Jason in patch 1
- Add reference to spec change
Changes from v2
- Better change log in patch2 - [Greg]
Changes from v1[2]
- Make virtio packed ring code compatible with split ring - [Michael]

[1]  https://marc.info/?l=qemu-devel&m=156471883703948&w=2
[2]  https://lkml.org/lkml/2019/3/4/517
[3]  https://lists.oasis-open.org/archives/virtio-dev/201908/msg00055.html

Pankaj Gupta (2):
virtio: decrement avail idx with buffer detach for packed ring
virtio_console: free unused buffers with port delete

 char/virtio_console.c |   14 +++++++++++---
 virtio/virtio_ring.c  |    6 ++++++
 2 files changed, 17 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v4 1/2 virtio: decrement avail idx with buffer detach for packed ring

This patch decrements 'next_avail_idx' count when detaching a buffer from vq for packed ring code. Split ring code already does this in virtqueue_detach_unused_buf_split function. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.

---
 drivers/virtio/virtio_ring.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/virtio/virtio_ring.c +6 -0
index c8be1c4f5b55..7c69181113e2 100644
@@ -1537,6 +1537,12 @@ static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
 		/* detach_buf clears data, so grab it now. */
 		buf = vq->packed.desc_state[i].data;
 		detach_buf_packed(vq, i, NULL);
+		vq->packed.next_avail_idx--;
+		if (vq->packed.next_avail_idx < 0) {
+			vq->packed.next_avail_idx = vq->packed.vring.num - 1;
+			vq->packed.avail_wrap_counter ^= 1;
+		}
+
 		END_USE(vq);
 		return buf;
 	}
Signature · 1 line
2.20.1
#3 lore raw

PATCH v4 2/2 virtio_console: free unused buffers with port delete

In reply to Pankaj Gupta

The commit a7a69ec0d8e4 ("virtio_console: free buffers after reset") deferred detaching of unused buffer to virtio device unplug time. This causes unplug/replug of single port in virtio device with an error "Error allocating inbufs\n". As we don't free the unused buffers attached with the port. Re-plug the same port tries to allocate new buffers in virtqueue and results in this error if queue is full. This patch reverts this commit by removing the unused buffers in vq's when we unplug the port.

---
 drivers/char/virtio_console.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/char/virtio_console.c +11 -3
index 7270e7b69262..e8be82f1bae9 100644
@@ -1494,15 +1494,25 @@ static void remove_port(struct kref *kref)
 	kfree(port);
 }
 
+static void remove_unused_bufs(struct virtqueue *vq)
+{
+	struct port_buffer *buf;
+
+	while ((buf = virtqueue_detach_unused_buf(vq)))
+		free_buf(buf, true);
+}
+
 static void remove_port_data(struct port *port)
 {
 	spin_lock_irq(&port->inbuf_lock);
 	/* Remove unused data this port might have received. */
 	discard_port_data(port);
+	remove_unused_bufs(port->in_vq);
 	spin_unlock_irq(&port->inbuf_lock);
 
 	spin_lock_irq(&port->outvq_lock);
 	reclaim_consumed_buffers(port);
+	remove_unused_bufs(port->out_vq);
 	spin_unlock_irq(&port->outvq_lock);
 }
 
@@ -1938,11 +1948,9 @@ static void remove_vqs(struct ports_device *portdev)
 	struct virtqueue *vq;
 
 	virtio_device_for_each_vq(portdev->vdev, vq) {
-		struct port_buffer *buf;
 
 		flush_bufs(vq, true);
-		while ((buf = virtqueue_detach_unused_buf(vq)))
-			free_buf(buf, true);
+		remove_unused_bufs(vq);
 	}
 	portdev->vdev->config->del_vqs(portdev->vdev);
 	kfree(portdev->in_vqs);
Signature · 1 line
2.21.0
#2 lore raw

PATCH v4 1/2 virtio: decrement avail idx with buffer detach for packed ring

In reply to Pankaj Gupta

This patch decrements 'next_avail_idx' count when detaching a buffer from vq for packed ring code. Split ring code already does this in virtqueue_detach_unused_buf_split function. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.

---
 drivers/virtio/virtio_ring.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/virtio/virtio_ring.c +6 -0
index c8be1c4f5b55..7c69181113e2 100644
@@ -1537,6 +1537,12 @@ static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq)
 		/* detach_buf clears data, so grab it now. */
 		buf = vq->packed.desc_state[i].data;
 		detach_buf_packed(vq, i, NULL);
+		vq->packed.next_avail_idx--;
+		if (vq->packed.next_avail_idx < 0) {
+			vq->packed.next_avail_idx = vq->packed.vring.num - 1;
+			vq->packed.avail_wrap_counter ^= 1;
+		}
+
 		END_USE(vq);
 		return buf;
 	}
Signature · 1 line
2.20.1
#4 lore raw

PATCH v4 2/2 virtio_console: free unused buffers with port delete

In reply to Pankaj Gupta

The commit a7a69ec0d8e4 ("virtio_console: free buffers after reset") deferred detaching of unused buffer to virtio device unplug time. This causes unplug/replug of single port in virtio device with an error "Error allocating inbufs\n". As we don't free the unused buffers attached with the port. Re-plug the same port tries to allocate new buffers in virtqueue and results in this error if queue is full. This patch reverts this commit by removing the unused buffers in vq's when we unplug the port.

---
 drivers/char/virtio_console.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/char/virtio_console.c +11 -3
index 7270e7b69262..e8be82f1bae9 100644
@@ -1494,15 +1494,25 @@ static void remove_port(struct kref *kref)
 	kfree(port);
 }
 
+static void remove_unused_bufs(struct virtqueue *vq)
+{
+	struct port_buffer *buf;
+
+	while ((buf = virtqueue_detach_unused_buf(vq)))
+		free_buf(buf, true);
+}
+
 static void remove_port_data(struct port *port)
 {
 	spin_lock_irq(&port->inbuf_lock);
 	/* Remove unused data this port might have received. */
 	discard_port_data(port);
+	remove_unused_bufs(port->in_vq);
 	spin_unlock_irq(&port->inbuf_lock);
 
 	spin_lock_irq(&port->outvq_lock);
 	reclaim_consumed_buffers(port);
+	remove_unused_bufs(port->out_vq);
 	spin_unlock_irq(&port->outvq_lock);
 }
 
@@ -1938,11 +1948,9 @@ static void remove_vqs(struct ports_device *portdev)
 	struct virtqueue *vq;
 
 	virtio_device_for_each_vq(portdev->vdev, vq) {
-		struct port_buffer *buf;
 
 		flush_bufs(vq, true);
-		while ((buf = virtqueue_detach_unused_buf(vq)))
-			free_buf(buf, true);
+		remove_unused_bufs(vq);
 	}
 	portdev->vdev->config->del_vqs(portdev->vdev);
 	kfree(portdev->in_vqs);
Signature · 1 line
2.21.0