Skip to contentSkip to conversation

icount: Take iothread lock when running QEMU timers

PATCH In review Peter Maydell 3 participants 3 messages Aug 1, 2022 – Aug 2, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 icount: Take iothread lock when running QEMU timers

The function icount_prepare_for_run() is called with the iothread unlocked, but it can call icount_notify_aio_contexts() which will run qemu timer handlers. Those are supposed to be run only with the iothread lock held, so take the lock while we do that.

Since icount mode runs everything on a single thread anyway, not holding the lock is likely mostly not going to introduce races, but it can cause us to trip over assertions that we do hold the lock, such as the one reported in issue 1130.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1130

---
 accel/tcg/tcg-accel-ops-icount.c | 6 ++++++
 1 file changed, 6 insertions(+)
accel/tcg/tcg-accel-ops-icount.c +6 -0
index 8f1dda4344c..84cc7421be8 100644
@@ -109,7 +109,13 @@ void icount_prepare_for_run(CPUState *cpu)
     replay_mutex_lock();
 
     if (cpu->icount_budget == 0) {
+        /*
+         * We're called without the iothread lock, so must take it while
+         * we're calling timer handlers.
+         */
+        qemu_mutex_lock_iothread();
         icount_notify_aio_contexts();
+        qemu_mutex_unlock_iothread();
     }
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Peter Maydell

The function icount_prepare_for_run() is called with the iothread unlocked, but it can call icount_notify_aio_contexts() which will run qemu timer handlers. Those are supposed to be run only with the iothread lock held, so take the lock while we do that.

Since icount mode runs everything on a single thread anyway, not holding the lock is likely mostly not going to introduce races, but it can cause us to trip over assertions that we do hold the lock, such as the one reported in issue 1130.

Quoted 5 lines

Resolves:https://gitlab.com/qemu-project/qemu/-/issues/1130

---
  accel/tcg/tcg-accel-ops-icount.c | 6 ++++++
  1 file changed, 6 insertions(+)

r~

Tested-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 34 lines · Peter Maydell

The function icount_prepare_for_run() is called with the iothread unlocked, but it can call icount_notify_aio_contexts() which will run qemu timer handlers. Those are supposed to be run only with the iothread lock held, so take the lock while we do that.

Since icount mode runs everything on a single thread anyway, not holding the lock is likely mostly not going to introduce races, but it can cause us to trip over assertions that we do hold the lock, such as the one reported in issue 1130.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1130

---
  accel/tcg/tcg-accel-ops-icount.c | 6 ++++++
  1 file changed, 6 insertions(+)
accel/tcg/tcg-accel-ops-icount.c +6 -0
index 8f1dda4344c..84cc7421be8 100644
@@ -109,7 +109,13 @@ void icount_prepare_for_run(CPUState *cpu)
      replay_mutex_lock();
 
      if (cpu->icount_budget == 0) {
+        /*
+         * We're called without the iothread lock, so must take it while
+         * we're calling timer handlers.
+         */
+        qemu_mutex_lock_iothread();
          icount_notify_aio_contexts();
+        qemu_mutex_unlock_iothread();
      }
  }