All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] util/qemu-coroutine: skip global pool lock when pool is empty
@ 2026-05-29  1:54 Bin Guo
  2026-05-29  8:34 ` Paolo Bonzini
  2026-05-29 13:39 ` Kevin Wolf
  0 siblings, 2 replies; 3+ messages in thread
From: Bin Guo @ 2026-05-29  1:54 UTC (permalink / raw)
  To: qemu-devel; +Cc: stefanha, kwolf

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.

Signed-off-by: Bin Guo <guobin@linux.alibaba.com>
---
 util/qemu-coroutine.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c
index d17135f585..0f8c3a23eb 100644
--- a/util/qemu-coroutine.c
+++ b/util/qemu-coroutine.c
@@ -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);
 
-- 
2.50.1 (Apple Git-155)



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] util/qemu-coroutine: skip global pool lock when pool is empty
  2026-05-29  1:54 [PATCH] util/qemu-coroutine: skip global pool lock when pool is empty Bin Guo
@ 2026-05-29  8:34 ` Paolo Bonzini
  2026-05-29 13:39 ` Kevin Wolf
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2026-05-29  8:34 UTC (permalink / raw)
  To: Bin Guo, qemu-devel; +Cc: stefanha, kwolf

On 5/29/26 03:54, Bin Guo wrote:
> 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.
> 
> Signed-off-by: Bin Guo <guobin@linux.alibaba.com>
> ---
>   util/qemu-coroutine.c | 9 +++++++++
>   1 file changed, 9 insertions(+)
> 
> diff --git a/util/qemu-coroutine.c b/util/qemu-coroutine.c
> index d17135f585..0f8c3a23eb 100644
> --- a/util/qemu-coroutine.c
> +++ b/util/qemu-coroutine.c
> @@ -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;
> +    }

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

> +
>       WITH_QEMU_LOCK_GUARD(&global_pool_lock) {
>           batch = QSLIST_FIRST(&global_pool);
>   



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] util/qemu-coroutine: skip global pool lock when pool is empty
  2026-05-29  1:54 [PATCH] util/qemu-coroutine: skip global pool lock when pool is empty Bin Guo
  2026-05-29  8:34 ` Paolo Bonzini
@ 2026-05-29 13:39 ` Kevin Wolf
  1 sibling, 0 replies; 3+ messages in thread
From: Kevin Wolf @ 2026-05-29 13:39 UTC (permalink / raw)
  To: Bin Guo; +Cc: qemu-devel, stefanha

Am 29.05.2026 um 03:54 hat Bin Guo geschrieben:
> 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.
> 
> Signed-off-by: Bin Guo <guobin@linux.alibaba.com>

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-05-29 13:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-29  1:54 [PATCH] util/qemu-coroutine: skip global pool lock when pool is empty Bin Guo
2026-05-29  8:34 ` Paolo Bonzini
2026-05-29 13:39 ` Kevin Wolf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.