virtio: fallback from irqfd to non-irqfd notify
PATCH 1/1 virtio: fallback from irqfd to non-irqfd notify
The commits 03de2f527 "virtio-blk: do not use vring in dataplane" and 9ffe337c08 "virtio-blk: always use dataplane path if ioeventfd is active" changed how notifications are done for virtio-blk substantially. Due to a race condition interrupts are lost when irqfd is torn down after notify_guest_bh was scheduled but before it actually runs. Furthermore virtio_notify_irqfd ignores the value returned by event_notifier_set which correctly indicates that notification has failed due to bad file descriptor.
Let's fix this by making virtio_notify_irqfd fall back to the non-irqfd notification mechanism if event_notifier_set fails.
---
This is probably not the only way to fix this: suggestions welcome. I did not use a fixes tag because I'm not sure yet where exactly things got broken. Maybe guys more familiar with dataplane an coroutines can help (Paolo, Stefan).
--- hw/virtio/virtio.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
hw/virtio/virtio.c +3 -1
@@ -1581,7 +1581,9 @@ void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)* to an atomic operation. */ virtio_set_isr (vq->vdev,0x1 ); -event_notifier_set (&vq->guest_notifier); +if (event_notifier_set (&vq->guest_notifier)) { +virtio_notify_vector (vdev, vq->vector); + } }static void virtio_irq (VirtQueue *vq)
Signature · 1 line
2.8.4
Quoted 28 lines · Halil Pasic
The commits
03de2f527"virtio-blk: do not use vring in dataplane" and9ffe337c08"virtio-blk: always use dataplane path if ioeventfd is active" changed how notifications are done for virtio-blk substantially. Due to a race condition interrupts are lost when irqfd is torn down afternotify_guest_bhwas scheduled but before it actually runs. Furthermorevirtio_notify_irqfdignores the value returned byevent_notifier_setwhich correctly indicates that notification has failed due to bad file descriptor.Let's fix this by making
virtio_notify_irqfdfall back to the non-irqfd notification mechanism ifevent_notifier_setfails.Signed-off-by Halil Pasic <[email protected]>---
This is probably not the only way to fix this: suggestions welcome. I did not use a fixes tag because I'm not sure yet where exactly things got broken. Maybe guys more familiar with dataplane an coroutines can help (Paolo, Stefan).
--- hw/virtio/virtio.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/virtio/virtio.c +0
-0
hw/virtio/virtio.c void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq) · line 1586
Quoted 7 lines
hw/virtio/virtio.c +3
-1@@ -1581,7 +1581,9 @@ void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)* to an atomic operation. */ virtio_set_isr (vq->vdev,0x1 ); -event_notifier_set (&vq->guest_notifier); +if (event_notifier_set (&vq->guest_notifier)) { +virtio_notify_vector (vdev, vq->vector); + }
Does this fail because the underlying fd got closed? Then there's a problem: trying to write to a closed
fd might corrupt an unrelated fd.
If you want to use this way we need to set fds to -1 when we close.
Quoted 5 lines
hw/virtio/virtio.c +0
-0}static void virtio_irq (VirtQueue *vq)Signature · 1 line
2.8.4
Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Halil Pasic
The commits
03de2f527"virtio-blk: do not use vring in dataplane" and9ffe337c08"virtio-blk: always use dataplane path if ioeventfd is active" changed how notifications are done for virtio-blk substantially. Due to a race condition interrupts are lost when irqfd is torn down afternotify_guest_bhwas scheduled but before it actually runs. Furthermorevirtio_notify_irqfdignores the value returned byevent_notifier_setwhich correctly indicates that notification has failed due to bad file descriptor.Let's fix this by making
virtio_notify_irqfdfall back to the non-irqfd notification mechanism ifevent_notifier_setfails.Signed-off-by Halil Pasic <[email protected]>---
This is probably not the only way to fix this: suggestions welcome. I did not use a fixes tag because I'm not sure yet where exactly things got broken. Maybe guys more familiar with dataplane an coroutines can help (Paolo, Stefan).
--- hw/virtio/virtio.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 14 lines
hw/virtio/virtio.c void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq) · line 1586
Quoted 12 lines
hw/virtio/virtio.c +3
-1@@ -1581,7 +1581,9 @@ void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)* to an atomic operation. */ virtio_set_isr (vq->vdev,0x1 ); -event_notifier_set (&vq->guest_notifier); +if (event_notifier_set (&vq->guest_notifier)) { +virtio_notify_vector (vdev, vq->vector); + }Does this fail because the underlying fd got closed?
Yes. Its data_plane_stop()->virtio_ccw_set_guest_notifier->event_notifier_cleanup(). The function event_notifier_cleanup() does not set fds to -1 :(.
Seems to me, it would be safer to do so. Should I make a patch?
Quoted 3 lines
Then there's a problem: trying to write to a closed
fd might corrupt an unrelated fd.
If you want to use this way we need to set fds to -1 when we close.
Sorry, did not check for that. OTOH Paolo says my approach is fundamentally flawed because virtio_notify_vector is not thread safe. I suggest we continue the discussion there.
Regards,
Halil
Quoted 7 lines
Quoted 6 lines
hw/virtio/virtio.c +0
-0}static void virtio_irq (VirtQueue *vq)Signature · 1 line
2.8.4
Quoted 5 lines · Halil Pasic
The commits
03de2f527"virtio-blk: do not use vring in dataplane" and9ffe337c08"virtio-blk: always use dataplane path if ioeventfd is active" changed how notifications are done for virtio-blk substantially. Due to a race condition interrupts are lost when irqfd is torn down afternotify_guest_bhwas scheduled but before it actually runs.
I don't think the non-irqfd notification mechanism is thread safe, and that would be a problem for this patch.
What is the path that causes the irqfd to be torn down? Only something like a reset should cause it (the only call in virtio-blk is from virtio_blk_data_plane_stop), and then the guest doesn't care anymore about interrupts.
That path also does a qemu_bh_delete, so the notify_guest_bh should not be invoked at all.
Paolo
Quoted 35 lines
Furthermore
virtio_notify_irqfdignores the value returned byevent_notifier_setwhich correctly indicates that notification has failed due to bad file descriptor.Let's fix this by making
virtio_notify_irqfdfall back to the non-irqfd notification mechanism ifevent_notifier_setfails.Signed-off-by Halil Pasic <[email protected]>---
This is probably not the only way to fix this: suggestions welcome. I did not use a fixes tag because I'm not sure yet where exactly things got broken. Maybe guys more familiar with dataplane an coroutines can help (Paolo, Stefan).
--- hw/virtio/virtio.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/virtio/virtio.c +3
-1@@ -1581,7 +1581,9 @@ void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq)* to an atomic operation. */ virtio_set_isr (vq->vdev,0x1 ); -event_notifier_set (&vq->guest_notifier); +if (event_notifier_set (&vq->guest_notifier)) { +virtio_notify_vector (vdev, vq->vector); + } }static void virtio_irq (VirtQueue *vq)
Quoted 11 lines · Paolo Bonzini
Quoted 5 lines · Halil Pasic
The commits
03de2f527"virtio-blk: do not use vring in dataplane" and9ffe337c08"virtio-blk: always use dataplane path if ioeventfd is active" changed how notifications are done for virtio-blk substantially. Due to a race condition interrupts are lost when irqfd is torn down afternotify_guest_bhwas scheduled but before it actually runs.I don't think the non-irqfd notification mechanism is thread safe, and that would be a problem for this patch.
Sorry, haven't looked into that thoroughly (and speculated people with more understanding will jump in).
Quoted 2 lines
What is the path that causes the irqfd to be torn down? Only something
Here a trace:
[email protected]:virtio_blk_req_complete req 0x2aa6b117e10 status 0 [email protected]:virtio_notify_irqfd vdev 0x2aa6b0e19d8 vq 0x2aa6b4c0870 [email protected]:virtio_blk_req_complete req 0x2aa6b118980 status 0 [email protected]:virtio_blk_req_complete req 0x2aa6b119260 status 0 [email protected]:virtio_notify_irqfd vdev 0x2aa6b0e19d8 vq 0x2aa6b4c0870 [email protected]:virtio_blk_req_complete req 0x2aa6b118980 status 0 [email protected]:virtio_notify_irqfd vdev 0x2aa6b0e19d8 vq 0x2aa6b4c0870 [email protected]:virtio_guest_notifier_read vdev 0x2aa6b0e61c8 vq 0x2aa6b4de880 [email protected]:virtio_guest_notifier_read vdev 0x2aa6b0e61c8 vq 0x2aa6b4de8f8 [email protected]:virtio_blk_data_plane_stop dataplane 0x2aa6b0e5540
^== DATAPLANE STOP
[email protected]:virtio_blk_req_complete req 0x2aa6b117e10 status 0 [email protected]:virtio_guest_notifier_read vdev 0x2aa6b0e19d8 vq 0x2aa6b4c0870
^== comes from k->set_guest_notifiers(qbus->parent, nvqs, false);
in virtio_blk_data_plane_stop and done immediately after irqfd is cleaned up by the transport
[email protected]:virtio_notify_irqfd vdev 0x2aa6b0e19d8 vq 0x2aa6b4c0870
halil: error in event_notifier_set: Bad file descriptor
^== here we have the problem
If you want a stacktrace that can be arranged to.
Quoted 3 lines
like a reset should cause it (the only call in virtio-blk is from
virtio_blk_data_plane_stop), and then the guest doesn't care anymore about interrupts.
I do not understand this with 'doesn't care anymore about interrupts'. I was debugging a virtio-blk device being stuck waiting for a host notification (interrupt) after migration.
Quoted 4 lines
That path also does a
qemu_bh_delete, so thenotify_guest_bhshould not be invoked at all.
That's only for destroy. I'm migrating.
Seems I tried to fix this is the wrong way. Was not too confident about it in the first place. Suggestions welcome!
Cheers,
Halil
Quoted 22 lines · Halil Pasic
Here a trace:
[email protected]:virtio_blk_req_complete req
0x2aa6b117e10status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_blk_req_complete req0x2aa6b118980status 0 [email protected]:virtio_blk_req_complete req0x2aa6b119260status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_blk_req_complete req0x2aa6b118980status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_guest_notifier_read vdev0x2aa6b0e61c8vq0x2aa6b4de880[email protected]:virtio_guest_notifier_read vdev0x2aa6b0e61c8vq0x2aa6b4de8f8[email protected]:virtio_blk_data_plane_stop dataplane0x2aa6b0e5540^== DATAPLANE STOP[email protected]:virtio_blk_req_complete req
0x2aa6b117e10status 0 [email protected]:virtio_guest_notifier_read vdev0x2aa6b0e19d8vq0x2aa6b4c0870
^== comes fromk->set_guest_notifiers(qbus->parent, nvqs, false);
invirtio_blk_data_plane_stopand done immediately after irqfd is cleaned up by the transport
[email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870
halil: error inevent_notifier_set: Bad file descriptor^== here we have the problem
Quoted 8 lines
If you want a stacktrace that can be arranged to.
Quoted 3 lines
like a reset should cause it (the only call in virtio-blk is from
virtio_blk_data_plane_stop), and then the guest doesn't care anymore about interrupts.I do not understand this with 'doesn't care anymore about interrupts'. I was debugging a virtio-blk device being stuck waiting for a host notification (interrupt) after migration.
Ok, this explains it better then. The issue is that virtio_blk_data_plane_stop doesn't flush the bottom half, which you want to do when the caller is, for example, virtio_ccw_vmstate_change.
Does it work if you call to qemu_bh_cancel(s->bh) and notify_guest_bh(s) after
blk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());
?
Paolo
Quoted 7 lines
Quoted 3 lines
That path also does a
qemu_bh_delete, so thenotify_guest_bhshould not be invoked at all.That's only for destroy. I'm migrating.
Seems I tried to fix this is the wrong way. Was not too confident about it in the first place. Suggestions welcome!
Quoted 38 lines · Paolo Bonzini
Quoted 22 lines · Halil Pasic
Here a trace:
[email protected]:virtio_blk_req_complete req
0x2aa6b117e10status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_blk_req_complete req0x2aa6b118980status 0 [email protected]:virtio_blk_req_complete req0x2aa6b119260status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_blk_req_complete req0x2aa6b118980status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_guest_notifier_read vdev0x2aa6b0e61c8vq0x2aa6b4de880[email protected]:virtio_guest_notifier_read vdev0x2aa6b0e61c8vq0x2aa6b4de8f8[email protected]:virtio_blk_data_plane_stop dataplane0x2aa6b0e5540^== DATAPLANE STOP[email protected]:virtio_blk_req_complete req
0x2aa6b117e10status 0 [email protected]:virtio_guest_notifier_read vdev0x2aa6b0e19d8vq0x2aa6b4c0870
^== comes fromk->set_guest_notifiers(qbus->parent, nvqs, false);
invirtio_blk_data_plane_stopand done immediately after irqfd is cleaned up by the transport
[email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870
halil: error inevent_notifier_set: Bad file descriptor^== here we have the problemQuoted 8 lines
If you want a stacktrace that can be arranged to.
Quoted 3 lines
like a reset should cause it (the only call in virtio-blk is from
virtio_blk_data_plane_stop), and then the guest doesn't care anymore about interrupts.I do not understand this with 'doesn't care anymore about interrupts'. I was debugging a virtio-blk device being stuck waiting for a host notification (interrupt) after migration.
Ok, this explains it better then. The issue is that
virtio_blk_data_plane_stopdoesn't flush the bottom half, which you want to do when the caller is, for example,virtio_ccw_vmstate_change.
Quoted 7 lines
Does it work if you call to
qemu_bh_cancel(s->bh)andnotify_guest_bh(s)afterblk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());?
With
hw/block/dataplane/virtio-blk.c +2 -0
@@ -260,6 +260,8 @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev)/* Drain and switch bs back to the QEMU main loop */ blk_set_aio_context (s->conf->conf.blk,qemu_get_aio_context ()); +qemu_bh_cancel (s->bh); +notify_guest_bh (s);
applied I do not see the problem any more. I will most likely turn this into a patch tomorrow. I would like to give it some more testing and thinking (see questions below) until tomorrow.
I should probably cc stable, or?
I would also like to do some diagnostic stuff if virtio_notify_irqfd fails. Maybe assert success for event_notifier_set. Would that be OK with you?
I have a couple of questions about the ways of the dataplane code. If you are too busy, feel free to not answer -- I will keep thinking myself.
Q1. For this to work correctly, it seems to me, we need to be sure that virtio_blk_req_complete can not be happen between the newly added notify_guest_bh(s);
andvblk->dataplane_started = false;
becomes visible. How is this ensured?
Q2. The virtio_blk_data_plane_stop should be from the thread/context associated with the main event loop, and with that vblk->dataplane_started = false too. But I think dataplane_started may end up being used form a different thread (e.g. req_complete). How does the sequencing work there and/or is it even important?
Regards,
Halil
Quoted 64 lines · Halil Pasic
Quoted 38 lines · Paolo Bonzini
Quoted 22 lines · Halil Pasic
Here a trace:
[email protected]:virtio_blk_req_complete req
0x2aa6b117e10status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_blk_req_complete req0x2aa6b118980status 0 [email protected]:virtio_blk_req_complete req0x2aa6b119260status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_blk_req_complete req0x2aa6b118980status 0 [email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870[email protected]:virtio_guest_notifier_read vdev0x2aa6b0e61c8vq0x2aa6b4de880[email protected]:virtio_guest_notifier_read vdev0x2aa6b0e61c8vq0x2aa6b4de8f8[email protected]:virtio_blk_data_plane_stop dataplane0x2aa6b0e5540^== DATAPLANE STOP[email protected]:virtio_blk_req_complete req
0x2aa6b117e10status 0 [email protected]:virtio_guest_notifier_read vdev0x2aa6b0e19d8vq0x2aa6b4c0870
^== comes fromk->set_guest_notifiers(qbus->parent, nvqs, false);
invirtio_blk_data_plane_stopand done immediately after irqfd is cleaned up by the transport
[email protected]:virtio_notify_irqfd vdev0x2aa6b0e19d8vq0x2aa6b4c0870
halil: error inevent_notifier_set: Bad file descriptor^== here we have the problemQuoted 8 lines
If you want a stacktrace that can be arranged to.
Quoted 3 lines
like a reset should cause it (the only call in virtio-blk is from
virtio_blk_data_plane_stop), and then the guest doesn't care anymore about interrupts.I do not understand this with 'doesn't care anymore about interrupts'. I was debugging a virtio-blk device being stuck waiting for a host notification (interrupt) after migration.
Ok, this explains it better then. The issue is that
virtio_blk_data_plane_stopdoesn't flush the bottom half, which you want to do when the caller is, for example,virtio_ccw_vmstate_change.Quoted 7 lines
Does it work if you call to
qemu_bh_cancel(s->bh)andnotify_guest_bh(s)afterblk_set_aio_context(s->conf->conf.blk, qemu_get_aio_context());?
With
hw/block/dataplane/virtio-blk.c +2
-0@@ -260,6 +260,8 @@ void virtio_blk_data_plane_stop(VirtIODevice *vdev)/* Drain and switch bs back to the QEMU main loop */ blk_set_aio_context (s->conf->conf.blk,qemu_get_aio_context ()); +qemu_bh_cancel (s->bh); +notify_guest_bh (s);applied I do not see the problem any more. I will most likely turn this into a patch tomorrow. I would like to give it some more testing and thinking (see questions below) until tomorrow.
Quoted 4 lines
I should probably cc stable, or?
I would also like to do some diagnostic stuff if
virtio_notify_irqfdfails. Maybe assert success forevent_notifier_set. Would that be OK with you?
Main reason it can't fail is because we don't close the fd. Given no callers check the return status, I'd be inclined to go further and stick that assert into event_notifier_set, convert that function to int.
Quoted 17 lines
I have a couple of questions about the ways of the dataplane code. If you are too busy, feel free to not answer -- I will keep thinking myself.
Q1. For this to work correctly, it seems to me, we need to be sure that
virtio_blk_req_completecan not be happen between the newly addednotify_guest_bh(s);
andvblk->dataplane_started= false;
becomes visible. How is this ensured?
Q2. Thevirtio_blk_data_plane_stopshould be from the thread/context associated with the main event loop, and with thatvblk->dataplane_started= false too. But I thinkdataplane_startedmay end up being used form a different thread (e.g.req_complete). How does the sequencing work there and/or is it even important?Regards,
Halil
Quoted 5 lines · Halil Pasic
applied I do not see the problem any more. I will most likely turn this into a patch tomorrow. I would like to give it some more testing and thinking (see questions below) until tomorrow.
I should probably cc stable, or?
Yes, please do!
Quoted 7 lines
Q1. For this to work correctly, it seems to me, we need to be sure that
virtio_blk_req_completecan not be happen between the newly addednotify_guest_bh(s);
andvblk->dataplane_started= false;
becomes visible. How is this ensured?
blk_set_aio_context drains the block device, and the event notifiers are not active anymore so draining the block device coincides with the last call to virtio_blk_req_complete.
Please add a comment - it's a good observation.
Quoted 4 lines
Q2. The
virtio_blk_data_plane_stopshould be from the thread/context associated with the main event loop, and with thatvblk->dataplane_started= false too. But I thinkdataplane_startedmay end up being used form a different thread (e.g.req_complete).
1) virtio_queue_aio_set_host_notifier_handler stops the event notifiers
2) virtio_bus_set_host_notifier invokes them one last time before exiting
Note that this could call again virtio_queue_notify_vq and hence virtio_device_start_ioeventfd, but dataplane won't be reactivated because vblk->dataplane_started is still true.
Quoted 1 line
How does the sequencing work there and/or is it even important?
It is important and not really easy to get right---as shown by the bug you found, in fact.
Thanks,
Paolo
Quoted 34 lines · Paolo Bonzini
Quoted 5 lines · Halil Pasic
applied I do not see the problem any more. I will most likely turn this into a patch tomorrow. I would like to give it some more testing and thinking (see questions below) until tomorrow.
I should probably cc stable, or?
Yes, please do!
Quoted 7 lines
Q1. For this to work correctly, it seems to me, we need to be sure that
virtio_blk_req_completecan not be happen between the newly addednotify_guest_bh(s);
andvblk->dataplane_started= false;
becomes visible. How is this ensured?
blk_set_aio_contextdrains the block device, and the event notifiers are not active anymore so draining the block device coincides with the last call tovirtio_blk_req_complete.Please add a comment - it's a good observation.
Quoted 4 lines
Q2. The
virtio_blk_data_plane_stopshould be from the thread/context associated with the main event loop, and with thatvblk->dataplane_started= false too. But I thinkdataplane_startedmay end up being used form a different thread (e.g.req_complete).1)
virtio_queue_aio_set_host_notifier_handlerstops the event notifiers2)
virtio_bus_set_host_notifierinvokes them one last time before exiting
Quoted 9 lines
Note that this could call again
virtio_queue_notify_vqand hencevirtio_device_start_ioeventfd, but dataplane won't be reactivated becausevblk->dataplane_startedis still true.Quoted 1 line
How does the sequencing work there and/or is it even important?
It is important and not really easy to get right---as shown by the bug you found, in fact.
Thank you very much for the explanations. I have just sent a patch based on what we discussed here. I think I roughly understand now, how this is supposed to work regarding concurrency, but I guess I will have to just trust you to some extent.