monitor: fix abort when a rate-limited event races with its timer
PATCH 1/1 monitor: fix abort when a rate-limited event races with its timer
From: Denis V. Lunev <[email protected]>
Rate-limited events can be raised from iothreads, outside the BQL. QUORUM_REPORT_BAD always could be. Since 2155d2dd7f73 BLOCK_IO_ERROR is rate limited too, and any disk on an iothread raises it.
The rate-limit timer runs in the main loop. timerlist_run_timers() takes it off the active list before it calls the handler, and only then does the handler take monitor_lock. An iothread raising the same event for the same device in between finds the rate-limit entry with a timer that is no longer pending, and QEMU aborts with:
Assertion `!evstate || timer_pending(evstate->timer)' failed.
This hits virtio-blk on an iothread with an error policy other than stop when the storage goes away and every guest request fails.
The state is fine, the assert is wrong. Once the timer has fired, the handler is committed to run: it takes the lock as soon as the iothread drops it, sends the stored event and re-arms the timer, exactly as for an event stored while the timer was pending. The event goes out at the same moment it would have had the iothread come in after the handler. The entry is only created, re-armed and freed under monitor_lock, so it cannot go away under the iothread. Drop the assert.
--- monitor/monitor.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
monitor/monitor.c +4 -3
@@ -360,13 +360,14 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)MonitorQAPIEventState key = { .event = event, .data = data }; evstate =g_hash_table_lookup (monitor_qapi_event_state, &key); -assert (!evstate ||timer_pending (evstate->timer));if (evstate) {/* * Timer is pending for (at least) evconf->rate ns after -* last send. Store event for sending when timer fires, -* replacing a prior stored event if any. +* last send, or it has expired and its handler has not +* taken monitor_lock yet. Either way the handler will +* send what is stored here. Store event for sending +* then, replacing a prior stored event if any. */ qobject_unref (evstate->qdict); evstate->qdict =qobject_ref (qdict);
base-commit: 8f66cb79044c370e3813c56ceb7a75a5ca2d0a0e
Signature · 1 line
2.55.0
Quoted 44 lines · Denis V. Lunev
From: Denis V. Lunev <[email protected]>
Rate-limited events can be raised from iothreads, outside the BQL.
QUORUM_REPORT_BADalways could be. Since2155d2dd7f73BLOCK_IO_ERRORis rate limited too, and any disk on an iothread raises it.The rate-limit timer runs in the main loop.
timerlist_run_timers()takes it off the active list before it calls the handler, and only then does the handler takemonitor_lock. An iothread raising the same event for the same device in between finds the rate-limit entry with a timer that is no longer pending, and QEMU aborts with:Assertion `!evstate || timer_pending(evstate->timer)' failed.This hits virtio-blk on an iothread with an error policy other than stop when the storage goes away and every guest request fails.
The state is fine, the assert is wrong. Once the timer has fired, the handler is committed to run: it takes the lock as soon as the iothread drops it, sends the stored event and re-arms the timer, exactly as for an event stored while the timer was pending. The event goes out at the same moment it would have had the iothread come in after the handler. The entry is only created, re-armed and freed under
monitor_lock, so it cannot go away under the iothread. Drop the assert.Fixes a24712af5425 ("monitor: Turn monitor_qapi_event_state[] into a hash table")Fixes 2155d2dd7f73 ("block-backend: per-device throttling of BLOCK_IO_ERROR reports")Signed-off-by Denis V. Lunev <[email protected]>Cc Markus Armbruster <[email protected]>Cc Dr. David Alan Gilbert <[email protected]>--- monitor/monitor.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)monitor/monitor.c +0
-1@@ -360,13 +360,14 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)MonitorQAPIEventState key = { .event = event, .data = data }; evstate =g_hash_table_lookup (monitor_qapi_event_state, &key); -assert (!evstate ||timer_pending (evstate->timer));
monitor/monitor.c monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) · line 370
Quoted 10 lines
monitor/monitor.c +4
-2if (evstate) {/* * Timer is pending for (at least) evconf->rate ns after -* last send. Store event for sending when timer fires, -* replacing a prior stored event if any. +* last send, or it has expired and its handler has not +* taken monitor_lock yet. Either way the handler will +* send what is stored here. Store event for sending +* then, replacing a prior stored event if any.
How about:
+ * last send, or its handler has not taken monitor_lock + * yet (the timer may have fired, or not been armed yet). + * Either way the handler will send what is stored here. + * Store event for sending then, replacing a prior stored + * event if any.
?
Quoted 5 lines
monitor/monitor.c +0
-0*/ qobject_unref (evstate->qdict); evstate->qdict =qobject_ref (qdict);base-commit:
8f66cb79044c370e3813c56ceb7a75a5ca2d0a0e
Quoted 51 lines · Philippe Mathieu-Daudé
Quoted 37 lines · Denis V. Lunev
From: Denis V. Lunev <[email protected]>
Rate-limited events can be raised from iothreads, outside the BQL.QUORUM_REPORT_BADalways could be. Since2155d2dd7f73BLOCK_IO_ERRORis rate limited too, and any disk on an iothread raises it.
The rate-limit timer runs in the main loop.timerlist_run_timers()takes it off the active list before it calls the handler, and only then does the handler takemonitor_lock. An iothread raising the same event for the same device in between finds the rate-limit entry with a timer that is no longer pending, and QEMU aborts with:Assertion `!evstate || timer_pending(evstate->timer)' failed.This hits virtio-blk on an iothread with an error policy other than stop when the storage goes away and every guest request fails. The state is fine, the assert is wrong. Once the timer has fired, the handler is committed to run: it takes the lock as soon as the iothread drops it, sends the stored event and re-arms the timer, exactly as for an event stored while the timer was pending. The event goes out at the same moment it would have had the iothread come in after the handler. The entry is only created, re-armed and freed under
monitor_lock, so it cannot go away under the iothread. Drop the assert.Fixes a24712af5425 ("monitor: Turn monitor_qapi_event_state[] into a hash table")Fixes 2155d2dd7f73 ("block-backend: per-device throttling of BLOCK_IO_ERROR reports")Signed-off-by Denis V. Lunev <[email protected]>Cc Markus Armbruster <[email protected]>Cc Dr. David Alan Gilbert <[email protected]>--- monitor/monitor.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)monitor/monitor.c +0
-1@@ -360,13 +360,14 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)MonitorQAPIEventState key = { .event = event, .data = data }; evstate =g_hash_table_lookup (monitor_qapi_event_state, &key); -assert (!evstate ||timer_pending (evstate->timer));monitor/monitor.c monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) · line 370
Quoted 10 lines
monitor/monitor.c +4
-2if (evstate) {/* * Timer is pending for (at least) evconf->rate ns after -* last send. Store event for sending when timer fires, -* replacing a prior stored event if any. +* last send, or it has expired and its handler has not +* taken monitor_lock yet. Either way the handler will +* send what is stored here. Store event for sending +* then, replacing a prior stored event if any. How about:
Quoted 7 lines
+ * last send, or its handler has not taken monitor_lock + * yet (the timer may have fired, or not been armed yet). + * Either way the handler will send what is stored here. + * Store event for sending then, replacing a prior stored + * event if any.?
I find this version harder to understand. What scenario exactly is missing from Denis's version? And is it important here?
Quoted 6 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 4 lines
monitor/monitor.c +0
-0*/ qobject_unref (evstate->qdict); evstate->qdict =qobject_ref (qdict);base-commit:
8f66cb79044c370e3813c56ceb7a75a5ca2d0a0e
Quoted 59 lines · Markus Armbruster
Quoted 57 lines · Philippe Mathieu-Daudé
Quoted 37 lines · Denis V. Lunev
From: Denis V. Lunev <[email protected]>
Rate-limited events can be raised from iothreads, outside the BQL.QUORUM_REPORT_BADalways could be. Since2155d2dd7f73BLOCK_IO_ERRORis rate limited too, and any disk on an iothread raises it.
The rate-limit timer runs in the main loop.timerlist_run_timers()takes it off the active list before it calls the handler, and only then does the handler takemonitor_lock. An iothread raising the same event for the same device in between finds the rate-limit entry with a timer that is no longer pending, and QEMU aborts with:Assertion `!evstate || timer_pending(evstate->timer)' failed.This hits virtio-blk on an iothread with an error policy other than stop when the storage goes away and every guest request fails. The state is fine, the assert is wrong. Once the timer has fired, the handler is committed to run: it takes the lock as soon as the iothread drops it, sends the stored event and re-arms the timer, exactly as for an event stored while the timer was pending. The event goes out at the same moment it would have had the iothread come in after the handler. The entry is only created, re-armed and freed under
monitor_lock, so it cannot go away under the iothread. Drop the assert.Fixes a24712af5425 ("monitor: Turn monitor_qapi_event_state[] into a hash table")Fixes 2155d2dd7f73 ("block-backend: per-device throttling of BLOCK_IO_ERROR reports")Signed-off-by Denis V. Lunev <[email protected]>Cc Markus Armbruster <[email protected]>Cc Dr. David Alan Gilbert <[email protected]>--- monitor/monitor.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)monitor/monitor.c +0
-1@@ -360,13 +360,14 @@ monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict)MonitorQAPIEventState key = { .event = event, .data = data }; evstate =g_hash_table_lookup (monitor_qapi_event_state, &key); -assert (!evstate ||timer_pending (evstate->timer));monitor/monitor.c monitor_qapi_event_queue_no_reenter(QAPIEvent event, QDict *qdict) · line 370
Quoted 10 lines
monitor/monitor.c +4
-2if (evstate) {/* * Timer is pending for (at least) evconf->rate ns after -* last send. Store event for sending when timer fires, -* replacing a prior stored event if any. +* last send, or it has expired and its handler has not +* taken monitor_lock yet. Either way the handler will +* send what is stored here. Store event for sending +* then, replacing a prior stored event if any. How about:
+ * last send, or its handler has not taken monitor_lock + * yet (the timer may have fired, or not been armed yet). + * Either way the handler will send what is stored here. + * Store event for sending then, replacing a prior stored + * event if any.
Quoted 4 lines
Quoted 1 line
?
I find this version harder to understand. What scenario exactly is missing from Denis's version? And is it important here?
The issue Denis fixed also exists in the gap before the first timer_mod_ns() under monitor_lock and between timer_mod_ns() calls after later sends, now described as "or has not been armed yet":
"the timer has fired [, or has not been armed yet], and its handler has not taken monitor_lock."
Quoted 8 lines
Quoted 7 lines
Reviewed-by Philippe Mathieu-Daudé <[email protected]>Quoted 4 lines
monitor/monitor.c +0
-0*/ qobject_unref (evstate->qdict); evstate->qdict =qobject_ref (qdict);base-commit:
8f66cb79044c370e3813c56ceb7a75a5ca2d0a0e