Skip to contentSkip to conversation

block/throttle-groups: fix deadlock with iolimits and muliple iothreads

PATCH RESEND In review Dmitry Guryanov 4 participants 6 messages Dec 8, 2025 – Mar 8 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 block/throttle-groups: fix deadlock with iolimits and muliple iothreads

Details: https://gitlab.com/qemu-project/qemu/-/issues/3144

The function schedule_next_request is called with tg->lock held and it may call throttle_group_co_restart_queue, which takes tgm->throttled_reqs_lock, qemu_co_mutex_lock may leave current coroutine if other iothread has taken the lock. If the next coroutine will call throttle_group_co_io_limits_intercept - it will try to take the mutex tg->lock which will never be released.

Here is the backtrace of the iothread:
Thread 30 (Thread 0x7f8aad1fd6c0 (LWP 24240) "IO iothread2"):

 #0  futex_wait (futex_word=0x5611adb7d828, expected=2, private=0) at ../sysdeps/nptl/futex-internal.h:146
 #1  __GI___lll_lock_wait (futex=futex@entry=0x5611adb7d828, private=0) at lowlevellock.c:49
 #2  0x00007f8ab5a97501 in lll_mutex_lock_optimized (mutex=0x5611adb7d828) at pthread_mutex_lock.c:48
 #3  ___pthread_mutex_lock (mutex=0x5611adb7d828) at pthread_mutex_lock.c:93
 #4  0x00005611823f5482 in qemu_mutex_lock_impl (mutex=0x5611adb7d828, file=0x56118289daca "../block/throttle-groups.c", line=372) at ../util/qemu-thread-posix.c:94
 #5  0x00005611822b0b39 in throttle_group_co_io_limits_intercept (tgm=0x5611af1bb4d8, bytes=4096, direction=THROTTLE_READ) at ../block/throttle-groups.c:372
 #6  0x00005611822473b1 in blk_co_do_preadv_part (blk=0x5611af1bb490, offset=15972311040, bytes=4096, qiov=0x7f8aa4000f98, qiov_offset=0, flags=BDRV_REQ_REGISTERED_BUF) at ../block/block-backend.c:1354
 #7  0x0000561182247fa0 in blk_aio_read_entry (opaque=0x7f8aa4005910) at ../block/block-backend.c:1619
 #8  0x000056118241952e in coroutine_trampoline (i0=-1543497424, i1=32650) at ../util/coroutine-ucontext.c:175
 #9  0x00007f8ab5a56f70 in ?? () at ../sysdeps/unix/sysv/linux/x86_64/__start_context.S:66 from target:/lib64/libc.so.6

#10 0x00007f8aad1ef190 in ?? ()
#11 0x0000000000000000 in ?? ()

The lock is taken in line 386:
(gdb) p tg.lock
$1 = {lock = {__data = {__lock = 2, __count = 0, __owner = 24240, __nusers = 1, __kind = 0, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}},

    __size = "\002\000\000\000\000\000\000\000\260^\000\000\001", '\000' <repeats 26 times>, __align = 2}, file = 0x56118289daca "../block/throttle-groups.c",

line = 386, initialized = true}

The solution is to use tg->lock to protect both ThreadGroup fields and ThrottleGroupMember.throttled_reqs. It doesn't seem to be possible to use separate locks because we need to first manipulate ThrottleGroup fields, then schedule next coroutine using throttled_reqs and after than update token field from ThrottleGroup depending on the throttled_reqs state.

---
 block/throttle-groups.c         | 21 ++++++---------------
 include/block/throttle-groups.h |  1 -
 2 files changed, 6 insertions(+), 16 deletions(-)
block/throttle-groups.c +6 -15
index 66fdce9a90..bb01e52516 100644
@@ -295,19 +295,15 @@ static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm,
 /* Start the next pending I/O request for a ThrottleGroupMember. Return whether
  * any request was actually pending.
  *
+ * This assumes that tg->lock is held.
+ *
  * @tgm:       the current ThrottleGroupMember
  * @direction: the ThrottleDirection
  */
 static bool coroutine_fn throttle_group_co_restart_queue(ThrottleGroupMember *tgm,
                                                          ThrottleDirection direction)
 {
-    bool ret;
-
-    qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
-    ret = qemu_co_queue_next(&tgm->throttled_reqs[direction]);
-    qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
-
-    return ret;
+    return qemu_co_queue_next(&tgm->throttled_reqs[direction]);;
 }
 
 /* Look for the next pending I/O request and schedule it.
@@ -378,12 +374,8 @@ void coroutine_fn throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm
     /* Wait if there's a timer set or queued requests of this type */
     if (must_wait || tgm->pending_reqs[direction]) {
         tgm->pending_reqs[direction]++;
-        qemu_mutex_unlock(&tg->lock);
-        qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
         qemu_co_queue_wait(&tgm->throttled_reqs[direction],
-                           &tgm->throttled_reqs_lock);
-        qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
-        qemu_mutex_lock(&tg->lock);
+                           &tg->lock);
         tgm->pending_reqs[direction]--;
     }
 
@@ -410,15 +402,15 @@ static void coroutine_fn throttle_group_restart_queue_entry(void *opaque)
     ThrottleDirection direction = data->direction;
     bool empty_queue;
 
+    qemu_mutex_lock(&tg->lock);
     empty_queue = !throttle_group_co_restart_queue(tgm, direction);
 
     /* If the request queue was empty then we have to take care of
      * scheduling the next one */
     if (empty_queue) {
-        qemu_mutex_lock(&tg->lock);
         schedule_next_request(tgm, direction);
-        qemu_mutex_unlock(&tg->lock);
     }
+    qemu_mutex_unlock(&tg->lock);
 
     g_free(data);
 
@@ -569,7 +561,6 @@ void throttle_group_register_tgm(ThrottleGroupMember *tgm,
                          read_timer_cb,
                          write_timer_cb,
                          tgm);
-    qemu_co_mutex_init(&tgm->throttled_reqs_lock);
 }
 
 /* Unregister a ThrottleGroupMember from its group, removing it from the list,
include/block/throttle-groups.h +0 -1
index 2355e8d9de..374c7c0215 100644
@@ -36,7 +36,6 @@
 typedef struct ThrottleGroupMember {
     AioContext   *aio_context;
     /* throttled_reqs_lock protects the CoQueues for throttled requests.  */
-    CoMutex      throttled_reqs_lock;
     CoQueue      throttled_reqs[THROTTLE_MAX];
 
     /* Nonzero if the I/O limits are currently being ignored; generally
Signature · 1 line
2.51.1
#1 lore raw

Hi! Could, someone, please, comment? The issue in gitlab is still open.

Quoted 154 lines · Dmitry Guryanov

Details: https://gitlab.com/qemu-project/qemu/-/issues/3144

The function schedule_next_request is called with tg->lock held and it may call throttle_group_co_restart_queue, which takes tgm->throttled_reqs_lock, qemu_co_mutex_lock may leave current coroutine if other iothread has taken the lock. If the next coroutine will call throttle_group_co_io_limits_intercept - it will try to take the mutex tg->lock which will never be released.

Here is the backtrace of the iothread:
Thread 30 (Thread 0x7f8aad1fd6c0 (LWP 24240) "IO iothread2"):
#0 futex_wait (futex_word=0x5611adb7d828, expected=2, private=0) at
../sysdeps/nptl/futex-internal.h:146
#1 __GI___lll_lock_wait (futex=futex@entry=0x5611adb7d828, private=0)
at lowlevellock.c:49
#2 0x00007f8ab5a97501 in lll_mutex_lock_optimized (mutex=0x5611adb7d828)
at pthread_mutex_lock.c:48
#3 ___pthread_mutex_lock (mutex=0x5611adb7d828) at
pthread_mutex_lock.c:93
#4 0x00005611823f5482 in qemu_mutex_lock_impl (mutex=0x5611adb7d828,
file=0x56118289daca "../block/throttle-groups.c", line=372) at ../util/qemu-thread-posix.c:94
#5 0x00005611822b0b39 in throttle_group_co_io_limits_intercept
(tgm=0x5611af1bb4d8, bytes=4096, direction=THROTTLE_READ) at ../block/throttle-groups.c:372
#6 0x00005611822473b1 in blk_co_do_preadv_part (blk=0x5611af1bb490,
offset=15972311040, bytes=4096, qiov=0x7f8aa4000f98, qiov_offset=0, flags=BDRV_REQ_REGISTERED_BUF) at ../block/block-backend.c:1354
#7 0x0000561182247fa0 in blk_aio_read_entry (opaque=0x7f8aa4005910) at
../block/block-backend.c:1619
#8 0x000056118241952e in coroutine_trampoline (i0=-1543497424, i1=32650)

at ../util/coroutine-ucontext.c:175
 #9  0x00007f8ab5a56f70 in ?? () at

../sysdeps/unix/sysv/linux/x86_64/__start_context.S:66 from target:/lib64/libc.so.6
#10 0x00007f8aad1ef190 in ?? ()
#11 0x0000000000000000 in ?? ()

The lock is taken in line 386:
(gdb) p tg.lock
$1 = {lock = {__data = {__lock = 2, __count = 0, __owner = 24240, __nusers = 1, __kind = 0, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}},

    __size = "\002\000\000\000\000\000\000\000\260^\000\000\001", '\000'

<repeats 26 times>, __align = 2}, file = 0x56118289daca "../block/throttle-groups.c",
line = 386, initialized = true}

The solution is to use tg->lock to protect both ThreadGroup fields and ThrottleGroupMember.throttled_reqs. It doesn't seem to be possible to use separate locks because we need to first manipulate ThrottleGroup fields, then schedule next coroutine using throttled_reqs and after than update token field from ThrottleGroup depending on the throttled_reqs state.

---
 block/throttle-groups.c         | 21 ++++++---------------
 include/block/throttle-groups.h |  1 -
 2 files changed, 6 insertions(+), 16 deletions(-)
block/throttle-groups.c +0 -0
index 66fdce9a90..bb01e52516 100644
@@ -295,19 +295,15 @@ static bool
 throttle_group_schedule_timer(ThrottleGroupMember *tgm,
 /* Start the next pending I/O request for a ThrottleGroupMember. Return

whether
* any request was actually pending.
*
+ * This assumes that tg->lock is held.
+ *
* @tgm: the current ThrottleGroupMember
* @direction: the ThrottleDirection
*/
static bool coroutine_fn
throttle_group_co_restart_queue(ThrottleGroupMember *tgm,

ThrottleDirection direction)
{

block/throttle-groups.c +1 -7
-    bool ret;
-
-    qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
-    ret = qemu_co_queue_next(&tgm->throttled_reqs[direction]);
-    qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
-
-    return ret;
+    return qemu_co_queue_next(&tgm->throttled_reqs[direction]);;
 }
 
 /* Look for the next pending I/O request and schedule it.
@@ -378,12 +374,8 @@ void coroutine_fn

throttle_group_co_io_limits_intercept(ThrottleGroupMember *tgm

     /* Wait if there's a timer set or queued requests of this type */
     if (must_wait || tgm->pending_reqs[direction]) {
         tgm->pending_reqs[direction]++;
block/throttle-groups.c +3 -8
-        qemu_mutex_unlock(&tg->lock);
-        qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
         qemu_co_queue_wait(&tgm->throttled_reqs[direction],
-                           &tgm->throttled_reqs_lock);
-        qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
-        qemu_mutex_lock(&tg->lock);
+                           &tg->lock);
         tgm->pending_reqs[direction]--;
     }
@@ -410,15 +402,15 @@ static void coroutine_fn
 throttle_group_restart_queue_entry(void *opaque)
     ThrottleDirection direction = data->direction;
     bool empty_queue;
 
+    qemu_mutex_lock(&tg->lock);
     empty_queue = !throttle_group_co_restart_queue(tgm, direction);
 
     /* If the request queue was empty then we have to take care of
      * scheduling the next one */
     if (empty_queue) {
-        qemu_mutex_lock(&tg->lock);
         schedule_next_request(tgm, direction);
-        qemu_mutex_unlock(&tg->lock);
     }
+    qemu_mutex_unlock(&tg->lock);
 
     g_free(data);
@@ -569,7 +561,6 @@ void throttle_group_register_tgm(ThrottleGroupMember
 *tgm,
                          read_timer_cb,
                          write_timer_cb,
                          tgm);
-    qemu_co_mutex_init(&tgm->throttled_reqs_lock);
 }
 

/* Unregister a ThrottleGroupMember from its group, removing it from the
list,

include/block/throttle-groups.h +0 -1
index 2355e8d9de..374c7c0215 100644
@@ -36,7 +36,6 @@
 typedef struct ThrottleGroupMember {
     AioContext   *aio_context;
     /* throttled_reqs_lock protects the CoQueues for throttled requests.
 */
-    CoMutex      throttled_reqs_lock;
     CoQueue      throttled_reqs[THROTTLE_MAX];
 
     /* Nonzero if the I/O limits are currently being ignored; generally

--
2.51.1

Signature · 1 line
Dmitry Guryanov
Reviewed-by on v1 1/1
#2 lore raw
In reply to Dmitry Guryanov
Quoted 57 lines · Dmitry Guryanov

Details: https://gitlab.com/qemu-project/qemu/-/issues/3144

The function schedule_next_request is called with tg->lock held and it may call throttle_group_co_restart_queue, which takes tgm->throttled_reqs_lock, qemu_co_mutex_lock may leave current coroutine if other iothread has taken the lock. If the next coroutine will call throttle_group_co_io_limits_intercept - it will try to take the mutex tg->lock which will never be released.

Here is the backtrace of the iothread:
Thread 30 (Thread 0x7f8aad1fd6c0 (LWP 24240) "IO iothread2"):

  #0  futex_wait (futex_word=0x5611adb7d828, expected=2, private=0) at ../sysdeps/nptl/futex-internal.h:146
  #1  __GI___lll_lock_wait (futex=futex@entry=0x5611adb7d828, private=0) at lowlevellock.c:49
  #2  0x00007f8ab5a97501 in lll_mutex_lock_optimized (mutex=0x5611adb7d828) at pthread_mutex_lock.c:48
  #3  ___pthread_mutex_lock (mutex=0x5611adb7d828) at pthread_mutex_lock.c:93
  #4  0x00005611823f5482 in qemu_mutex_lock_impl (mutex=0x5611adb7d828, file=0x56118289daca "../block/throttle-groups.c", line=372) at ../util/qemu-thread-posix.c:94
  #5  0x00005611822b0b39 in throttle_group_co_io_limits_intercept (tgm=0x5611af1bb4d8, bytes=4096, direction=THROTTLE_READ) at ../block/throttle-groups.c:372
  #6  0x00005611822473b1 in blk_co_do_preadv_part (blk=0x5611af1bb490, offset=15972311040, bytes=4096, qiov=0x7f8aa4000f98, qiov_offset=0, flags=BDRV_REQ_REGISTERED_BUF) at ../block/block-backend.c:1354
  #7  0x0000561182247fa0 in blk_aio_read_entry (opaque=0x7f8aa4005910) at ../block/block-backend.c:1619
  #8  0x000056118241952e in coroutine_trampoline (i0=-1543497424, i1=32650) at ../util/coroutine-ucontext.c:175
  #9  0x00007f8ab5a56f70 in ?? () at ../sysdeps/unix/sysv/linux/x86_64/__start_context.S:66 from target:/lib64/libc.so.6

#10 0x00007f8aad1ef190 in ?? ()
#11 0x0000000000000000 in ?? ()

The lock is taken in line 386:
(gdb) p tg.lock
$1 = {lock = {__data = {__lock = 2, __count = 0, __owner = 24240, __nusers = 1, __kind = 0, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}},

     __size = "\002\000\000\000\000\000\000\000\260^\000\000\001", '\000' <repeats 26 times>, __align = 2}, file = 0x56118289daca "../block/throttle-groups.c",

line = 386, initialized = true}

The solution is to use tg->lock to protect both ThreadGroup fields and ThrottleGroupMember.throttled_reqs. It doesn't seem to be possible to use separate locks because we need to first manipulate ThrottleGroup fields, then schedule next coroutine using throttled_reqs and after than update token field from ThrottleGroup depending on the throttled_reqs state.

---
  block/throttle-groups.c         | 21 ++++++---------------
  include/block/throttle-groups.h |  1 -
  2 files changed, 6 insertions(+), 16 deletions(-)
block/throttle-groups.c +2 -0
index 66fdce9a90..bb01e52516 100644
@@ -295,19 +295,15 @@ static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm,
  /* Start the next pending I/O request for a ThrottleGroupMember. Return whether
   * any request was actually pending.
   *
+ * This assumes that tg->lock is held.
+ *
   * @tgm:       the current ThrottleGroupMember
   * @direction: the ThrottleDirection
   */
  static bool coroutine_fn throttle_group_co_restart_queue(ThrottleGroupMember *tgm,

block/throttle-groups.c static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm, · line 306

Quoted 10 lines
block/throttle-groups.c +1 -7
                                                           ThrottleDirection direction)
  {
-    bool ret;
-
-    qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
-    ret = qemu_co_queue_next(&tgm->throttled_reqs[direction]);
-    qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
-
-    return ret;
+    return qemu_co_queue_next(&tgm->throttled_reqs[direction]);;

Double semicolon here, should just be one.

Quoted 3 lines
block/throttle-groups.c +0 -0
  }
 
  /* Look for the next pending I/O request and schedule it.

[...]

include/block/throttle-groups.h line 38

Quoted 8 lines
include/block/throttle-groups.h +0 -0
index 2355e8d9de..374c7c0215 100644
@@ -36,7 +36,6 @@
  typedef struct ThrottleGroupMember {
      AioContext   *aio_context;
      /* throttled_reqs_lock protects the CoQueues for throttled requests.  */

This comment should be updated to something like "Protected by ThrottleGroup.lock".

With both of these fixed:

Quoted 4 lines
include/block/throttle-groups.h +0 -1
-    CoMutex      throttled_reqs_lock;
      CoQueue      throttled_reqs[THROTTLE_MAX];
 
      /* Nonzero if the I/O limits are currently being ignored; generally
#3 lore raw

Am 26.02.2026 um 15:37 hat Hanna Czenczek geschrieben:

Quoted 88 lines · Dmitry Guryanov
Quoted 57 lines

Details: https://gitlab.com/qemu-project/qemu/-/issues/3144

The function schedule_next_request is called with tg->lock held and it may call throttle_group_co_restart_queue, which takes tgm->throttled_reqs_lock, qemu_co_mutex_lock may leave current coroutine if other iothread has taken the lock. If the next coroutine will call throttle_group_co_io_limits_intercept - it will try to take the mutex tg->lock which will never be released.

Here is the backtrace of the iothread:
Thread 30 (Thread 0x7f8aad1fd6c0 (LWP 24240) "IO iothread2"):

  #0  futex_wait (futex_word=0x5611adb7d828, expected=2, private=0) at ../sysdeps/nptl/futex-internal.h:146
  #1  __GI___lll_lock_wait (futex=futex@entry=0x5611adb7d828, private=0) at lowlevellock.c:49
  #2  0x00007f8ab5a97501 in lll_mutex_lock_optimized (mutex=0x5611adb7d828) at pthread_mutex_lock.c:48
  #3  ___pthread_mutex_lock (mutex=0x5611adb7d828) at pthread_mutex_lock.c:93
  #4  0x00005611823f5482 in qemu_mutex_lock_impl (mutex=0x5611adb7d828, file=0x56118289daca "../block/throttle-groups.c", line=372) at ../util/qemu-thread-posix.c:94
  #5  0x00005611822b0b39 in throttle_group_co_io_limits_intercept (tgm=0x5611af1bb4d8, bytes=4096, direction=THROTTLE_READ) at ../block/throttle-groups.c:372
  #6  0x00005611822473b1 in blk_co_do_preadv_part (blk=0x5611af1bb490, offset=15972311040, bytes=4096, qiov=0x7f8aa4000f98, qiov_offset=0, flags=BDRV_REQ_REGISTERED_BUF) at ../block/block-backend.c:1354
  #7  0x0000561182247fa0 in blk_aio_read_entry (opaque=0x7f8aa4005910) at ../block/block-backend.c:1619
  #8  0x000056118241952e in coroutine_trampoline (i0=-1543497424, i1=32650) at ../util/coroutine-ucontext.c:175
  #9  0x00007f8ab5a56f70 in ?? () at ../sysdeps/unix/sysv/linux/x86_64/__start_context.S:66 from target:/lib64/libc.so.6

#10 0x00007f8aad1ef190 in ?? ()
#11 0x0000000000000000 in ?? ()

The lock is taken in line 386:
(gdb) p tg.lock
$1 = {lock = {__data = {__lock = 2, __count = 0, __owner = 24240, __nusers = 1, __kind = 0, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}},

     __size = "\002\000\000\000\000\000\000\000\260^\000\000\001", '\000' <repeats 26 times>, __align = 2}, file = 0x56118289daca "../block/throttle-groups.c",

line = 386, initialized = true}

The solution is to use tg->lock to protect both ThreadGroup fields and ThrottleGroupMember.throttled_reqs. It doesn't seem to be possible to use separate locks because we need to first manipulate ThrottleGroup fields, then schedule next coroutine using throttled_reqs and after than update token field from ThrottleGroup depending on the throttled_reqs state.

---
  block/throttle-groups.c         | 21 ++++++---------------
  include/block/throttle-groups.h |  1 -
  2 files changed, 6 insertions(+), 16 deletions(-)
block/throttle-groups.c +2 -0
index 66fdce9a90..bb01e52516 100644
@@ -295,19 +295,15 @@ static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm,
  /* Start the next pending I/O request for a ThrottleGroupMember. Return whether
   * any request was actually pending.
   *
+ * This assumes that tg->lock is held.
+ *
   * @tgm:       the current ThrottleGroupMember
   * @direction: the ThrottleDirection
   */
  static bool coroutine_fn throttle_group_co_restart_queue(ThrottleGroupMember *tgm,

block/throttle-groups.c static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm, · line 306

Quoted 10 lines
block/throttle-groups.c +1 -7
                                                           ThrottleDirection direction)
  {
-    bool ret;
-
-    qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
-    ret = qemu_co_queue_next(&tgm->throttled_reqs[direction]);
-    qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
-
-    return ret;
+    return qemu_co_queue_next(&tgm->throttled_reqs[direction]);;

Double semicolon here, should just be one.

Quoted 2 lines
block/throttle-groups.c +0 -0
  }
  /* Look for the next pending I/O request and schedule it.

[...]

include/block/throttle-groups.h line 38

Quoted 8 lines
include/block/throttle-groups.h +0 -0
index 2355e8d9de..374c7c0215 100644
@@ -36,7 +36,6 @@
  typedef struct ThrottleGroupMember {
      AioContext   *aio_context;
      /* throttled_reqs_lock protects the CoQueues for throttled requests.  */

This comment should be updated to something like "Protected by ThrottleGroup.lock".

Quoted 3 lines

With both of these fixed:

Thanks, I applied the series based on your review and made the changes you suggested.

Kevin

#4 lore raw

Thanks!

Quoted 124 lines · Kevin Wolf

Am 26.02.2026 um 15:37 hat Hanna Czenczek geschrieben:

Quoted 10 lines · Dmitry Guryanov
Quoted 9 lines

Details: https://gitlab.com/qemu-project/qemu/-/issues/3144

The function schedule_next_request is called with tg->lock held and it may call throttle_group_co_restart_queue, which takes tgm->throttled_reqs_lock, qemu_co_mutex_lock may leave current coroutine if other iothread has taken the lock. If the next coroutine will call throttle_group_co_io_limits_intercept - it will try to take the mutex tg->lock which will never be released.

Quoted 3 lines
Quoted 3 lines

Here is the backtrace of the iothread:
Thread 30 (Thread 0x7f8aad1fd6c0 (LWP 24240) "IO iothread2"):
#0 futex_wait (futex_word=0x5611adb7d828, expected=2, private=0) at

../sysdeps/nptl/futex-internal.h:146

Quoted 1 line
Quoted 1 line

#1 __GI___lll_lock_wait (futex=futex@entry=0x5611adb7d828,

private=0) at lowlevellock.c:49

Quoted 1 line
Quoted 1 line

#2 0x00007f8ab5a97501 in lll_mutex_lock_optimized

(mutex=0x5611adb7d828) at pthread_mutex_lock.c:48

Quoted 1 line
Quoted 1 line

#3 ___pthread_mutex_lock (mutex=0x5611adb7d828) at

pthread_mutex_lock.c:93

Quoted 1 line
Quoted 1 line

#4 0x00005611823f5482 in qemu_mutex_lock_impl

(mutex=0x5611adb7d828, file=0x56118289daca "../block/throttle-groups.c", line=372) at ../util/qemu-thread-posix.c:94

Quoted 1 line
Quoted 1 line

#5 0x00005611822b0b39 in throttle_group_co_io_limits_intercept

(tgm=0x5611af1bb4d8, bytes=4096, direction=THROTTLE_READ) at ../block/throttle-groups.c:372

Quoted 1 line
Quoted 1 line

#6 0x00005611822473b1 in blk_co_do_preadv_part (blk=0x5611af1bb490,

offset=15972311040, bytes=4096, qiov=0x7f8aa4000f98, qiov_offset=0, flags=BDRV_REQ_REGISTERED_BUF) at ../block/block-backend.c:1354

Quoted 1 line
Quoted 1 line

#7 0x0000561182247fa0 in blk_aio_read_entry (opaque=0x7f8aa4005910)

at ../block/block-backend.c:1619

Quoted 1 line
Quoted 1 line

#8 0x000056118241952e in coroutine_trampoline (i0=-1543497424,

i1=32650) at ../util/coroutine-ucontext.c:175

Quoted 1 line
Quoted 1 line

#9 0x00007f8ab5a56f70 in ?? () at

../sysdeps/unix/sysv/linux/x86_64/__start_context.S:66 from target:/lib64/libc.so.6

Quoted 6 lines
Quoted 6 lines

#10 0x00007f8aad1ef190 in ?? ()
#11 0x0000000000000000 in ?? ()

The lock is taken in line 386:
(gdb) p tg.lock
$1 = {lock = {__data = {__lock = 2, __count = 0, __owner = 24240,

__nusers = 1, __kind = 0, __spins = 0, __elision = 0, __list = {__prev = 0x0, __next = 0x0}},

Quoted 1 line
Quoted 1 line
     __size = "\002\000\000\000\000\000\000\000\260^\000\000\001",

'\000' <repeats 26 times>, __align = 2}, file = 0x56118289daca "../block/throttle-groups.c",

Quoted 6 lines
Quoted 6 lines
   line = 386, initialized = true}

The solution is to use tg->lock to protect both ThreadGroup fields and ThrottleGroupMember.throttled_reqs. It doesn't seem to be possible to use separate locks because we need to first manipulate ThrottleGroup fields, then schedule next coroutine using throttled_reqs and after

than

Quoted 9 lines
Quoted 9 lines

update token field from ThrottleGroup depending on the throttled_reqs state.

---
  block/throttle-groups.c         | 21 ++++++---------------
  include/block/throttle-groups.h |  1 -
  2 files changed, 6 insertions(+), 16 deletions(-)
Quoted 5 lines

block/throttle-groups.c static bool

Quoted 5 lines
block/throttle-groups.c +0 -0
index 66fdce9a90..bb01e52516 100644
@@ -295,19 +295,15 @@ static bool

throttle_group_schedule_timer(ThrottleGroupMember *tgm,

Quoted 1 line

block/throttle-groups.c static bool · line 295

Quoted 1 line
block/throttle-groups.c +0 -0
  /* Start the next pending I/O request for a ThrottleGroupMember.

Return whether

Quoted 8 lines

block/throttle-groups.c static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm,

Quoted 8 lines
block/throttle-groups.c +2 -0
   * any request was actually pending.
   *
+ * This assumes that tg->lock is held.
+ *
   * @tgm:       the current ThrottleGroupMember
   * @direction: the ThrottleDirection
   */
  static bool coroutine_fn

throttle_group_co_restart_queue(ThrottleGroupMember *tgm,

Quoted 1 line
Quoted 1 line

ThrottleDirection direction)

Quoted 12 lines

block/throttle-groups.c static bool throttle_group_schedule_timer(ThrottleGroupMember *tgm, · line 306

Quoted 9 lines
  {
block/throttle-groups.c +1 -7
-    bool ret;
-
-    qemu_co_mutex_lock(&tgm->throttled_reqs_lock);
-    ret = qemu_co_queue_next(&tgm->throttled_reqs[direction]);
-    qemu_co_mutex_unlock(&tgm->throttled_reqs_lock);
-
-    return ret;
+    return qemu_co_queue_next(&tgm->throttled_reqs[direction]);;

Double semicolon here, should just be one.

Quoted 6 lines
Quoted 2 lines

}
/* Look for the next pending I/O request and schedule it.

[...]

Review

Quoted 1 line
(quoted diff) +0 -0

b/include/block/throttle-groups.h

Quoted 7 lines

include/block/throttle-groups.h line 38

Quoted 7 lines

index 2355e8d9de..374c7c0215 100644

include/block/throttle-groups.h +0 -0
@@ -36,7 +36,6 @@
  typedef struct ThrottleGroupMember {
      AioContext   *aio_context;
      /* throttled_reqs_lock protects the CoQueues for throttled

requests. */

Quoted 7 lines

This comment should be updated to something like "Protected by ThrottleGroup.lock".

With both of these fixed:

Thanks, I applied the series based on your review and made the changes you suggested.

Kevin

Signature · 1 line
Dmitry Guryanov
#5 lore raw
In reply to Kevin Wolf
Quoted 10 lines · Kevin Wolf

Am 26.02.2026 um 15:37 hat Hanna Czenczek geschrieben:

Quoted 9 lines · Dmitry Guryanov
Quoted 8 lines

Details: https://gitlab.com/qemu-project/qemu/-/issues/3144

The function schedule_next_request is called with tg->lock held and it may call throttle_group_co_restart_queue, which takes tgm->throttled_reqs_lock, qemu_co_mutex_lock may leave current coroutine if other iothread has taken the lock. If the next coroutine will call throttle_group_co_io_limits_intercept - it will try to take the mutex tg->lock which will never be released.

...

This smells like a qemu-stable material (10.0.x, 10.1.x, 10.2.x).

Please let me know if it isn't.

Thanks,

/mjt