virtio_console: fix replug of virtio console port
Subsystems (3)
v3 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 makes use of 'virtqueue_detach_unused_buf' function to detach the unused buffers during port hotunplug time.
Patch 2 updates the next avail index for packed ring code. Tested the packed ring code with the qemu virtio 1.1 device code posted here [1].
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
Pankaj Gupta (2):
virtio_console: free unused buffers with port delete
virtio: decrement avail idx with buffer detach for packed ring
char/virtio_console.c | 14 +++++++++++--- virtio/virtio_ring.c | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
PATCH v3 1/2 virtio_console: free unused buffers with port delete
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 removes the unused buffers in vq's when we unplug the port. This is the best we can do as we cannot call device_reset because virtio device is still active.
--- drivers/char/virtio_console.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/char/virtio_console.c +11 -3
@@ -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
PATCH v3 2/2 virtio: decrement avail idx with buffer detach for packed ring
In reply to Pankaj GuptaThis 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
@@ -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
PATCH v3 1/2 virtio_console: free unused buffers with port delete
In reply to Pankaj GuptaThe 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 removes the unused buffers in vq's when we unplug the port. This is the best we can do as we cannot call device_reset because virtio device is still active.
--- drivers/char/virtio_console.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/char/virtio_console.c +11 -3
@@ -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
Michael S. Tsirkin
maintainerRe: [PATCH v3 2/2] virtio: decrement avail idx with buffer detach for packed ring
In reply to Pankaj GuptaQuoted 7 lines · 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 invirtqueue_detach_unused_buf_splitfunction. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.Signed-off-by Pankaj Gupta <[email protected]>
I would make this patch 1, not patch 2, otherwise
patch 1 corrupts the ring.
Quoted 23 lines
--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/virtio/virtio_ring.c +6
-0@@ -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
Michael S. Tsirkin
maintainerRe: [PATCH v3 1/2] virtio_console: free unused buffers with port delete
In reply to Pankaj GuptaQuoted 6 lines · 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.
So why not reuse the buffers that are already there in this case? Seems quite possible.
Quoted 8 lines
This patch removes the unused buffers in vq's when we unplug the port. This is the best we can do as we cannot call
device_resetbecause virtio device is still active.Reported-by Xiaohui Li <[email protected]>Fixes a7a69ec0d8e4 ("virtio_console: free buffers after reset")Signed-off-by Pankaj Gupta <[email protected]>
This is really a revert of a7a69ec0d8e4, just tagged confusingly.
And the original is also supposed to be a bugfix.
So how will the original bug be fixed?
"this is the best we can do" is rarely the case.
I am not necessarily against the revert. But if we go that way then what we need to do is specify the behaviour in the spec, since strict spec compliance is exactly what the original patch was addressing.
In particular, we'd document that console has a special property that when port is detached virtqueue is considered stopped, device must not use any buffers, and it is legal to take buffers out of the device.
Quoted 49 lines
--- drivers/char/virtio_console.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/char/virtio_console.c +11
-3@@ -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
Re: [PATCH v3 2/2] virtio: decrement avail idx with buffer detach for packed ring
In reply to Pankaj GuptaQuoted 18 lines · 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 invirtqueue_detach_unused_buf_splitfunction. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.Signed-off-by Pankaj Gupta <[email protected]>--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/virtio/virtio_ring.c +0
-0@@ -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;
drivers/virtio/virtio_ring.c static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) · line 1548
Quoted 10 lines
drivers/virtio/virtio_ring.c +6
-0detach_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; }
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Re: [PATCH v3 2/2] virtio: decrement avail idx with buffer detach for packed ring
In reply to Pankaj GuptaQuoted 18 lines · 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 invirtqueue_detach_unused_buf_splitfunction. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.Signed-off-by Pankaj Gupta <[email protected]>--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/virtio/virtio_ring.c +0
-0@@ -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;
drivers/virtio/virtio_ring.c static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) · line 1548
Quoted 10 lines
drivers/virtio/virtio_ring.c +6
-0detach_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; }
Re: [PATCH v3 2/2] virtio: decrement avail idx with buffer detach for packed ring
In reply to Michael S. TsirkinQuoted 8 lines · Pankaj Gupta
Quoted 6 lines
This patch decrements '
next_avail_idx' count when detaching a buffer from vq for packed ring code. Split ring code already does this invirtqueue_detach_unused_buf_splitfunction. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.
Quoted 4 lines
Quoted 1 line
Signed-off-by Pankaj Gupta <[email protected]>I would make this patch 1, not patch 2, otherwise
patch 1 corrupts the ring.
Sure. Will post this patch as patch 1 in next version.
Thanks,
Pankaj
Quoted 27 lines
Quoted 26 lines
--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/virtio/virtio_ring.c +6
-0@@ -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;}
--
2.20.1
Re: [PATCH v3 2/2] virtio: decrement avail idx with buffer detach for packed ring
In reply to Michael S. TsirkinQuoted 8 lines · Pankaj Gupta
Quoted 6 lines
This patch decrements '
next_avail_idx' count when detaching a buffer from vq for packed ring code. Split ring code already does this invirtqueue_detach_unused_buf_splitfunction. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.
Quoted 4 lines
Quoted 1 line
Signed-off-by Pankaj Gupta <[email protected]>I would make this patch 1, not patch 2, otherwise
patch 1 corrupts the ring.
Sure. Will post this patch as patch 1 in next version.
Thanks,
Pankaj
Quoted 27 lines
Quoted 26 lines
--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/virtio/virtio_ring.c +6
-0@@ -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;}
--
2.20.1
Re: [PATCH v3 1/2] virtio_console: free unused buffers with port delete
In reply to Michael S. TsirkinQuoted 10 lines · Pankaj Gupta
Quoted 6 lines
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.
Quoted 2 lines
So why not reuse the buffers that are already there in this case? Seems quite possible.
I took this approach because reusing the buffers will involve tweaking the existing core functionality like managing the the virt queue indexes. Compared to that deleting the buffers while hot-unplugging port is simple and was working fine before. It seems logically correct as well.
I agree we need a spec change for this.
Quoted 10 lines
Quoted 9 lines
This patch removes the unused buffers in vq's when we unplug the port. This is the best we can do as we cannot call
device_resetbecause virtio device is still active.Reported-by Xiaohui Li <[email protected]>Fixes a7a69ec0d8e4 ("virtio_console: free buffers after reset")Signed-off-by Pankaj Gupta <[email protected]>
Quoted 4 lines
This is really a revert of
a7a69ec0d8e4, just tagged confusingly.And the original is also supposed to be a bugfix.
So how will the original bug be fixed?
Yes, Even I was confused while adding this tag.
I will remove remove 'fixes' tag completely for this patch? because its a revert to original behavior which also is a bugfix.
Quoted 6 lines
"this is the best we can do" is rarely the case.
I am not necessarily against the revert. But if we go that way then what we need to do is specify the behaviour in the spec, since strict spec compliance is exactly what the original patch was addressing.
Agree.
Quoted 4 lines
In particular, we'd document that console has a special property that when port is detached virtqueue is considered stopped, device must not use any buffers, and it is legal to take buffers out of the device.
Yes. This documents the exact scenario. Thanks.
You want me to send a patch for the spec change?
Best regards,
Pankaj
Quoted 53 lines
Quoted 52 lines
--- drivers/char/virtio_console.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/char/virtio_console.c +11
-4@@ -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); --2.21.0
Re: [PATCH v3 2/2] virtio: decrement avail idx with buffer detach for packed ring
In reply to Jason WangQuoted 27 lines · Pankaj Gupta
Quoted 24 lines
This patch decrements '
next_avail_idx' count when detaching a buffer from vq for packed ring code. Split ring code already does this invirtqueue_detach_unused_buf_splitfunction. This updates the 'next_avail_idx' to the previous correct index after an unused buffer is detatched from the vq.Signed-off-by Pankaj Gupta <[email protected]>--- drivers/virtio/virtio_ring.c | 6 ++++++ 1 file changed, 6 insertions(+)drivers/virtio/virtio_ring.c +4
-0@@ -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 ;
Quoted 8 lines
Quoted 5 lines
drivers/virtio/virtio_ring.c +2
-0+ } +END_USE (vq);return buf;}Acked-by Jason Wang <[email protected]>
Thank you, Jason.
Best regards,
Pankaj
Quoted 3 lines
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Michael S. Tsirkin
maintainerRe: [PATCH v3 1/2] virtio_console: free unused buffers with port delete
In reply to Pankaj GuptaQuoted 10 lines · Pankaj Gupta
Quoted 10 lines · Pankaj Gupta
Quoted 6 lines
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.
Quoted 5 lines
Quoted 2 lines
So why not reuse the buffers that are already there in this case? Seems quite possible.
I took this approach because reusing the buffers will involve tweaking the existing core functionality like managing the the virt queue indexes.
I don't see why frankly, if you keep a list of outstanding buffers on plug you can assume they have been added.
Quoted 38 lines
Compared to that deleting the buffers while hot-unplugging port is simple and was working fine before. It seems logically correct as well.
I agree we need a spec change for this.
Quoted 10 lines
Quoted 9 lines
This patch removes the unused buffers in vq's when we unplug the port. This is the best we can do as we cannot call
device_resetbecause virtio device is still active.Reported-by Xiaohui Li <[email protected]>Fixes a7a69ec0d8e4 ("virtio_console: free buffers after reset")Signed-off-by Pankaj Gupta <[email protected]>Quoted 4 lines
This is really a revert of
a7a69ec0d8e4, just tagged confusingly.And the original is also supposed to be a bugfix.
So how will the original bug be fixed?Yes, Even I was confused while adding this tag.
I will remove remove 'fixes' tag completely for this patch? because its a revert to original behavior which also is a bugfix.Quoted 6 lines
"this is the best we can do" is rarely the case.
I am not necessarily against the revert. But if we go that way then what we need to do is specify the behaviour in the spec, since strict spec compliance is exactly what the original patch was addressing.
Agree.
Quoted 4 lines
In particular, we'd document that console has a special property that when port is detached virtqueue is considered stopped, device must not use any buffers, and it is legal to take buffers out of the device.
Quoted 5 lines
Yes. This documents the exact scenario. Thanks.
You want me to send a patch for the spec change?Best regards,
Pankaj
Go ahead.
Quoted 53 lines
Quoted 53 lines
Quoted 52 lines
--- drivers/char/virtio_console.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)drivers/char/virtio_console.c +11
-4@@ -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); --2.21.0