virtio: 9p: Order used ring updates before notifications
PATCH 1/1 virtio: 9p: Order used ring updates before notifications
virtio_p9_do_io() signals the guest immediately after adding each request to the used ring. Unlike the block and net backends, it bypasses virtio_queue__should_signal(), which provides the full memory barrier needed between publishing used->idx and injecting the interrupt.
On weakly ordered architectures, the guest can therefore handle the interrupt before the updated used->idx becomes visible. If it observes the old index and goes back to sleep, the completed request can remain stuck indefinitely because no further interrupt is generated.
This was observed on RISC-V with two 9p RPCs blocked while the host used->idx was two entries ahead of the guest's last_used_idx.
Process all available requests first, then use virtio_queue__should_signal() before notifying the guest. Besides providing the required ordering, this also honors the driver's notification suppression request.
--- virtio/9p.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
virtio/9p.c +5 -1
@@ -1372,11 +1372,15 @@ static void virtio_p9_do_io(struct kvm *kvm, void *param)struct p9_dev_job *job = (struct p9_dev_job *)param;struct p9_dev *p9dev = job->p9dev;struct virt_queue *vq = job->vq; +bool completed = false;while (virt_queue__available (vq)) {virtio_p9_do_io_request (kvm, job); - p9dev->vdev.ops->signal_vq (kvm, &p9dev->vdev, vq - p9dev->vqs); + completed = true; } + +if (completed &&virtio_queue__should_signal (vq)) + p9dev->vdev.ops->signal_vq (kvm, &p9dev->vdev, vq - p9dev->vqs); }static u8 *get_config (struct kvm *kvm,void *dev)
Signature · 1 line
2.17.1
Quoted 32 lines · Xie Bo
virtio_p9_do_io()signals the guest immediately after adding each request to the used ring. Unlike the block and net backends, it bypassesvirtio_queue__should_signal(), which provides the full memory barrier needed between publishingused->idxand injecting the interrupt.On weakly ordered architectures, the guest can therefore handle the interrupt before the updated
used->idxbecomes visible. If it observes the old index and goes back to sleep, the completed request can remain stuck indefinitely because no further interrupt is generated.This was observed on RISC-V with two 9p RPCs blocked while the host
used->idxwas two entries ahead of the guest'slast_used_idx.Process all available requests first, then use
virtio_queue__should_signal()before notifying the guest. Besides providing the required ordering, this also honors the driver's notification suppression request.Fixes 1c7850f95903 ("kvm tools: Add virtio-9p")Signed-off-by Xie Bo <[email protected]>--- virtio/9p.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)virtio/9p.c +0
-0@@ -1372,11 +1372,15 @@ static void virtio_p9_do_io(struct kvm *kvm, void *param)struct p9_dev_job *job = (struct p9_dev_job *)param;struct p9_dev *p9dev = job->p9dev;struct virt_queue *vq = job->vq;
virtio/9p.c static void virtio_p9_do_io(struct kvm *kvm, void *param) · line 1383
Quoted 10 lines
virtio/9p.c +5
-1+bool completed = false;while (virt_queue__available (vq)) {virtio_p9_do_io_request (kvm, job); - p9dev->vdev.ops->signal_vq (kvm, &p9dev->vdev, vq - p9dev->vqs); + completed = true; } + +if (completed &&virtio_queue__should_signal (vq)) + p9dev->vdev.ops->signal_vq (kvm, &p9dev->vdev, vq - p9dev->vqs);
Why do you need the 'completed' bool here? Can't we just rely on virtio_queue__should_signal() instead?
Will
PATCH v2 1/1 virtio: 9p: Order used ring updates before notifications
virtio_p9_do_io() signals the guest immediately after adding each request to the used ring. Unlike the block and net backends, it bypasses virtio_queue__should_signal(), which provides the full memory barrier needed between publishing used->idx and injecting the interrupt.
On weakly ordered architectures, the guest can therefore handle the interrupt before the updated used->idx becomes visible. If it observes the old index and goes back to sleep, the completed request can remain stuck indefinitely because no further interrupt is generated.
This was observed on RISC-V with two 9p RPCs blocked while the host used->idx was two entries ahead of the guest's last_used_idx.
Process all available requests first, then use virtio_queue__should_signal() before notifying the guest. Besides providing the required ordering, this also honors the driver's notification suppression request.
---
Changes in v2:
- Drop the completed flag and rely directly on virtio_queue__should_signal(), as suggested by Will Deacon.
virtio/9p.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
virtio/9p.c +3 -1
@@ -1375,8 +1375,10 @@ static void virtio_p9_do_io(struct kvm *kvm, void *param)while (virt_queue__available (vq)) {virtio_p9_do_io_request (kvm, job); - p9dev->vdev.ops->signal_vq (kvm, &p9dev->vdev, vq - p9dev->vqs); } + +if (virtio_queue__should_signal (vq)) + p9dev->vdev.ops->signal_vq (kvm, &p9dev->vdev, vq - p9dev->vqs); }static u8 *get_config (struct kvm *kvm,void *dev)
Signature · 1 line
2.54.0
Will Deacon
AppliedQuoted 11 lines · Xie Bo
virtio_p9_do_io()signals the guest immediately after adding each request to the used ring. Unlike the block and net backends, it bypassesvirtio_queue__should_signal(), which provides the full memory barrier needed between publishingused->idxand injecting the interrupt.On weakly ordered architectures, the guest can therefore handle the interrupt before the updated
used->idxbecomes visible. If it observes the old index and goes back to sleep, the completed request can remain stuck indefinitely because no further interrupt is generated.[...]
Applied to kvmtool (master), thanks!
[1/1] virtio: 9p: Order used ring updates before notifications
https://git.kernel.org/will/kvmtool/c/59ccded8ade3
Cheers,
Signature · 5 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev