[PATCH 1/2] block: minimize bs->reqs_lock section in tracked_request_end()

Stefan Hajnoczi posted 2 patches 2 years, 6 months ago
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Fam Zheng <fam@euphon.net>
[PATCH 1/2] block: minimize bs->reqs_lock section in tracked_request_end()
Posted by Stefan Hajnoczi 2 years, 6 months ago
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 block/io.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/block/io.c b/block/io.c
index 055fcf7438..85d5176256 100644
--- a/block/io.c
+++ b/block/io.c
@@ -593,8 +593,14 @@ static void coroutine_fn tracked_request_end(BdrvTrackedRequest *req)
 
     qemu_co_mutex_lock(&req->bs->reqs_lock);
     QLIST_REMOVE(req, list);
+    qemu_co_mutex_unlock(&req->bs->reqs_lock);
+
+    /*
+     * At this point qemu_co_queue_wait(&req->wait_queue, ...) won't be called
+     * anymore because the request has been removed from the list, so it's safe
+     * to restart the queue outside reqs_lock to minimize the critical section.
+     */
     qemu_co_queue_restart_all(&req->wait_queue);
-    qemu_co_mutex_unlock(&req->bs->reqs_lock);
 }
 
 /**
-- 
2.41.0
Re: [PATCH 1/2] block: minimize bs->reqs_lock section in tracked_request_end()
Posted by Eric Blake 2 years, 6 months ago
On Tue, Aug 08, 2023 at 11:58:51AM -0400, Stefan Hajnoczi wrote:
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  block/io.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)

Reviewed-by: Eric Blake <eblake@redhat.com>

> 
> diff --git a/block/io.c b/block/io.c
> index 055fcf7438..85d5176256 100644
> --- a/block/io.c
> +++ b/block/io.c
> @@ -593,8 +593,14 @@ static void coroutine_fn tracked_request_end(BdrvTrackedRequest *req)
>  
>      qemu_co_mutex_lock(&req->bs->reqs_lock);
>      QLIST_REMOVE(req, list);
> +    qemu_co_mutex_unlock(&req->bs->reqs_lock);
> +
> +    /*
> +     * At this point qemu_co_queue_wait(&req->wait_queue, ...) won't be called
> +     * anymore because the request has been removed from the list, so it's safe
> +     * to restart the queue outside reqs_lock to minimize the critical section.
> +     */
>      qemu_co_queue_restart_all(&req->wait_queue);
> -    qemu_co_mutex_unlock(&req->bs->reqs_lock);
>  }
>  
>  /**
> -- 
> 2.41.0
> 
> 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org