Skip to contentSkip to conversation

util/qemu-coroutine: skip global pool lock when pool is empty

PATCH In review Bin Guo 3 participants 3 messages May 29

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 1/1 util/qemu-coroutine: skip global pool lock when pool is empty

coroutine_pool_refill_local() acquires global_pool_lock on every call even when the global pool is empty (global_pool_size == 0). Under high I/O concurrency many threads simultaneously attempt to refill from an empty global pool, causing unnecessary mutex contention.

Add a fast-path check: read global_pool_size with qatomic_read() before acquiring the mutex. If zero, the global pool is empty and we return immediately. This is a racy read but correctness is
preserved: the only consequence of a stale read is a missed refill opportunity, which will be retried on the next coroutine allocation.

---
 util/qemu-coroutine.c | 9 +++++++++
 1 file changed, 9 insertions(+)
util/qemu-coroutine.c +9 -0
index d17135f585..0f8c3a23eb 100644
@@ -138,6 +138,15 @@ static void coroutine_pool_refill_local(void)
     CoroutinePool *local_pool = get_ptr_local_pool();
     CoroutinePoolBatch *batch = NULL;
 
+    /*
+     * Fast path: skip the lock when the global pool is obviously empty.
+     * The read is racy but harmless -- worst case we miss a concurrent
+     * put and retry on the next allocation.
+     */
+    if (qatomic_read(&global_pool_size) == 0) {
+        return;
+    }
+
     WITH_QEMU_LOCK_GUARD(&global_pool_lock) {
         batch = QSLIST_FIRST(&global_pool);
 
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw
Quoted 23 lines · Bin Guo

coroutine_pool_refill_local() acquires global_pool_lock on every call even when the global pool is empty (global_pool_size == 0). Under high I/O concurrency many threads simultaneously attempt to refill from an empty global pool, causing unnecessary mutex contention.

Add a fast-path check: read global_pool_size with qatomic_read() before acquiring the mutex. If zero, the global pool is empty and we return immediately. This is a racy read but correctness is
preserved: the only consequence of a stale read is a missed refill opportunity, which will be retried on the next coroutine allocation.

---
  util/qemu-coroutine.c | 9 +++++++++
  1 file changed, 9 insertions(+)
util/qemu-coroutine.c +0 -0
index d17135f585..0f8c3a23eb 100644
@@ -138,6 +138,15 @@ static void coroutine_pool_refill_local(void)
      CoroutinePool *local_pool = get_ptr_local_pool();

util/qemu-coroutine.c static void coroutine_pool_refill_local(void) · line 148

Quoted 10 lines
util/qemu-coroutine.c +8 -0
      CoroutinePoolBatch *batch = NULL;
 
+    /*
+     * Fast path: skip the lock when the global pool is obviously empty.
+     * The read is racy but harmless -- worst case we miss a concurrent
+     * put and retry on the next allocation.
+     */
+    if (qatomic_read(&global_pool_size) == 0) {
+        return;
+    }

Reads are fine within the lock, but you still need to use qatomic_set for writes now, i.e.

- global_pool_size -= batch->size;
+ qatomic_set(&global_pool_size,
+ global_pool_size - batch->size);

- global_pool_size += batch->size;
+ qatomic_set(&global_pool_size,
+ global_pool_size + batch->size);

Thanks,

Paolo

Quoted 4 lines
util/qemu-coroutine.c +1 -0
+
      WITH_QEMU_LOCK_GUARD(&global_pool_lock) {
          batch = QSLIST_FIRST(&global_pool);
 
#2 lore raw
In reply to Bin Guo

Am 29.05.2026 um 03:54 hat Bin Guo geschrieben:

Quoted 6 lines

coroutine_pool_refill_local() acquires global_pool_lock on every call even when the global pool is empty (global_pool_size == 0). Under high I/O concurrency many threads simultaneously attempt to refill from an empty global pool, causing unnecessary mutex contention.

Quoted 7 lines

Add a fast-path check: read global_pool_size with qatomic_read() before acquiring the mutex. If zero, the global pool is empty and we return immediately. This is a racy read but correctness is
preserved: the only consequence of a stale read is a missed refill opportunity, which will be retried on the next coroutine allocation.

If you have any benchmark results, putting them in the commit message would be a good idea. (If you don't have them, you should probably get them for a patch that is motivated by performance.)

Kevin