Skip to contentSkip to conversation

xen-block: Fix double qlist remove

PATCH Superseded Anthony PERARD 3 participants 6 messages Apr 2, 2020 – Apr 7, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen-block: Fix double qlist remove

Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on remove") revealed that a request was removed twice from a list, once in xen_block_finish_request() and a second time in xen_block_release_request() when both function are called from xen_block_complete_aio(). But also, the `requests_inflight' counter is decreased twice, and thus became negative.

This is a bug that was introduced in bfd0d6366043, where a `finished' list was removed.

This patch simply re-add the `finish' parameter of xen_block_release_request() so that we can distinguish when we need to remove a request from the inflight list and when not.

Fixes bfd0d6366043 ("xen-block: improve response latency")
---
 hw/block/dataplane/xen-block.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
hw/block/dataplane/xen-block.c +9 -5
index 288a87a814ad..6cc089fc561f 100644
@@ -123,15 +123,19 @@ static void xen_block_finish_request(XenBlockRequest *request)
     dataplane->requests_inflight--;
 }
 
-static void xen_block_release_request(XenBlockRequest *request)
+static void xen_block_release_request(XenBlockRequest *request, bool finish)
 {
     XenBlockDataPlane *dataplane = request->dataplane;
 
-    QLIST_REMOVE(request, list);
+    if (!finish) {
+        QLIST_REMOVE(request, list);
+    }
     reset_request(request);
     request->dataplane = dataplane;
     QLIST_INSERT_HEAD(&dataplane->freelist, request, list);
-    dataplane->requests_inflight--;
+    if (!finish) {
+        dataplane->requests_inflight--;
+    }
 }
 
 /*
@@ -316,7 +320,7 @@ static void xen_block_complete_aio(void *opaque, int ret)
             error_report_err(local_err);
         }
     }
-    xen_block_release_request(request);
+    xen_block_release_request(request, true);
 
     if (dataplane->more_work) {
         qemu_bh_schedule(dataplane->bh);
@@ -585,7 +589,7 @@ static bool xen_block_handle_requests(XenBlockDataPlane *dataplane)
                     error_report_err(local_err);
                 }
             }
-            xen_block_release_request(request);
+            xen_block_release_request(request, false);
             continue;
         }
 
Signature · 1 line
Anthony PERARD
#1 lore raw
Quoted 20 lines

-----Original Message-----
From: Anthony PERARD <[email protected]>
Sent: 02 April 2020 14:08
To: [email protected]
Cc: [email protected]; Anthony PERARD <[email protected]>; Stefano Stabellini <[email protected]>; Paul Durrant <[email protected]>; Stefan Hajnoczi <[email protected]>; Kevin Wolf <[email protected]>; Max Reitz <[email protected]>; [email protected]; qemu- [email protected]
Subject: [PATCH for-5.0] xen-block: Fix double qlist remove

Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on remove") revealed that a request was removed twice from a list, once in xen_block_finish_request() and a second time in xen_block_release_request() when both function are called from xen_block_complete_aio(). But also, the `requests_inflight' counter is decreased twice, and thus became negative.

This is a bug that was introduced in bfd0d6366043, where a `finished' list was removed.

Quoted 6 lines

This patch simply re-add the `finish' parameter of xen_block_release_request() so that we can distinguish when we need to remove a request from the inflight list and when not.

Fixes bfd0d6366043 ("xen-block: improve response latency")

It looks to me like it would just be more straightforward to simply drop the QLIST_REMOVE and requests_inflight-- from xen_block_release_request() and simply insist that xen_block_finish_request() is called in all cases (which I think means adding one extra call to it in xen_block_handle_requests()).

Paul

Quoted 51 lines
---
 hw/block/dataplane/xen-block.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
hw/block/dataplane/xen-block.c +9 -6
index 288a87a814ad..6cc089fc561f 100644
@@ -123,15 +123,19 @@ static void xen_block_finish_request(XenBlockRequest *request)
     dataplane->requests_inflight--;
 }
 
-static void xen_block_release_request(XenBlockRequest *request)
+static void xen_block_release_request(XenBlockRequest *request, bool finish)
 {
     XenBlockDataPlane *dataplane = request->dataplane;
 
-    QLIST_REMOVE(request, list);
+    if (!finish) {
+        QLIST_REMOVE(request, list);
+    }
     reset_request(request);
     request->dataplane = dataplane;
     QLIST_INSERT_HEAD(&dataplane->freelist, request, list);
-    dataplane->requests_inflight--;
+    if (!finish) {
+        dataplane->requests_inflight--;
+    }
 }
 
 /*
@@ -316,7 +320,7 @@ static void xen_block_complete_aio(void *opaque, int ret)
             error_report_err(local_err);
         }
     }
-    xen_block_release_request(request);
+    xen_block_release_request(request, true);
 
     if (dataplane->more_work) {
         qemu_bh_schedule(dataplane->bh);
@@ -585,7 +589,7 @@ static bool xen_block_handle_requests(XenBlockDataPlane *dataplane)
                     error_report_err(local_err);
                 }
             }
-            xen_block_release_request(request);
+            xen_block_release_request(request, false);
             continue;
         }
 
--

Anthony PERARD

#2 lore raw
Quoted 24 lines · Paul Durrant
Quoted 24 lines

-----Original Message-----
From: Anthony PERARD <[email protected]>
Sent: 02 April 2020 14:08
To: [email protected]
Cc: [email protected]; Anthony PERARD <[email protected]>; Stefano Stabellini <[email protected]>; Paul Durrant <[email protected]>; Stefan Hajnoczi <[email protected]>; Kevin Wolf <[email protected]>; Max Reitz <[email protected]>; [email protected]; qemu- [email protected]
Subject: [PATCH for-5.0] xen-block: Fix double qlist remove

Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on remove") revealed that a request was removed twice from a list, once in xen_block_finish_request() and a second time in xen_block_release_request() when both function are called from xen_block_complete_aio(). But also, the `requests_inflight' counter is decreased twice, and thus became negative.

This is a bug that was introduced in bfd0d6366043, where a `finished' list was removed.

This patch simply re-add the `finish' parameter of xen_block_release_request() so that we can distinguish when we need to remove a request from the inflight list and when not.

Quoted 6 lines
Quoted 2 lines
Fixes bfd0d6366043 ("xen-block: improve response latency")

It looks to me like it would just be more straightforward to simply drop the QLIST_REMOVE and requests_inflight-- from xen_block_release_request() and simply insist that xen_block_finish_request() is called in all cases (which I think means adding one extra call to it in xen_block_handle_requests()).

I'm thinking of going further than that. I've notice another bug, in case of error in xen_block_do_aio(), xen_block_finish_request() is called without ever calling send_response() or release_request(). I think that mean a leak of request.

So, I'm thinking of creating a function that would do finish_request(), send_response(), release_request(), has I believe those operations needs to be done together anyway.

I'll rework the patch.

Signature · 1 line
Anthony PERARD
#3 lore raw

PATCH v2 1/1 xen-block: Fix double qlist remove and request leak

Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on remove") revealed that a request was removed twice from a list, once in xen_block_finish_request() and a second time in xen_block_release_request() when both function are called from xen_block_complete_aio(). But also, the `requests_inflight' counter is decreased twice, and thus became negative.

This is a bug that was introduced in bfd0d6366043, where a `finished' list was removed.

That commit also introduced a leak of request in xen_block_do_aio(). That function calls xen_block_finish_request() but the request is never released after that.

To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let now the result to the other end, thus we should call xen_block_send_response() before releasing a request.

The first change fix the double QLIST_REMOVE() as we remove the extra call. The second change makes the leak go away because if we want to call finish_request(), we need to call a function that do all of finish, send response, and release.

Fixes bfd0d6366043 ("xen-block: improve response latency")
---
 hw/block/dataplane/xen-block.c | 48 ++++++++++++----------------------
 1 file changed, 16 insertions(+), 32 deletions(-)
hw/block/dataplane/xen-block.c +16 -32
index 288a87a814ad..5f8f15778ba5 100644
@@ -64,6 +64,8 @@ struct XenBlockDataPlane {
     AioContext *ctx;
 };
 
+static int xen_block_send_response(XenBlockRequest *request);
+
 static void reset_request(XenBlockRequest *request)
 {
     memset(&request->req, 0, sizeof(request->req));
@@ -115,23 +117,26 @@ static XenBlockRequest *xen_block_start_request(XenBlockDataPlane *dataplane)
     return request;
 }
 
-static void xen_block_finish_request(XenBlockRequest *request)
+static void xen_block_complete_request(XenBlockRequest *request)
 {
     XenBlockDataPlane *dataplane = request->dataplane;
 
-    QLIST_REMOVE(request, list);
-    dataplane->requests_inflight--;
-}
+    if (xen_block_send_response(request)) {
+        Error *local_err = NULL;
 
-static void xen_block_release_request(XenBlockRequest *request)
-{
-    XenBlockDataPlane *dataplane = request->dataplane;
+        xen_device_notify_event_channel(dataplane->xendev,
+                                        dataplane->event_channel,
+                                        &local_err);
+        if (local_err) {
+            error_report_err(local_err);
+        }
+    }
 
     QLIST_REMOVE(request, list);
+    dataplane->requests_inflight--;
     reset_request(request);
     request->dataplane = dataplane;
     QLIST_INSERT_HEAD(&dataplane->freelist, request, list);
-    dataplane->requests_inflight--;
 }
 
 /*
@@ -246,7 +251,6 @@ static int xen_block_copy_request(XenBlockRequest *request)
 }
 
 static int xen_block_do_aio(XenBlockRequest *request);
-static int xen_block_send_response(XenBlockRequest *request);
 
 static void xen_block_complete_aio(void *opaque, int ret)
 {
@@ -286,7 +290,6 @@ static void xen_block_complete_aio(void *opaque, int ret)
     }
 
     request->status = request->aio_errors ? BLKIF_RSP_ERROR : BLKIF_RSP_OKAY;
-    xen_block_finish_request(request);
 
     switch (request->req.operation) {
     case BLKIF_OP_WRITE:
@@ -306,17 +309,8 @@ static void xen_block_complete_aio(void *opaque, int ret)
     default:
         break;
     }
-    if (xen_block_send_response(request)) {
-        Error *local_err = NULL;
 
-        xen_device_notify_event_channel(dataplane->xendev,
-                                        dataplane->event_channel,
-                                        &local_err);
-        if (local_err) {
-            error_report_err(local_err);
-        }
-    }
-    xen_block_release_request(request);
+    xen_block_complete_request(request);
 
     if (dataplane->more_work) {
         qemu_bh_schedule(dataplane->bh);
@@ -420,8 +414,8 @@ static int xen_block_do_aio(XenBlockRequest *request)
     return 0;
 
 err:
-    xen_block_finish_request(request);
     request->status = BLKIF_RSP_ERROR;
+    xen_block_complete_request(request);
     return -1;
 }
 
@@ -575,17 +569,7 @@ static bool xen_block_handle_requests(XenBlockDataPlane *dataplane)
                 break;
             };
 
-            if (xen_block_send_response(request)) {
-                Error *local_err = NULL;
-
-                xen_device_notify_event_channel(dataplane->xendev,
-                                                dataplane->event_channel,
-                                                &local_err);
-                if (local_err) {
-                    error_report_err(local_err);
-                }
-            }
-            xen_block_release_request(request);
+            xen_block_complete_request(request);
             continue;
         }
 
Signature · 1 line
Anthony PERARD
Reviewed-by on v2 1/1
#4 lore raw

RE: [PATCH v2 for-5.0] xen-block: Fix double qlist remove and request leak

Quoted 10 lines

-----Original Message-----
From: Anthony PERARD <[email protected]>
Sent: 06 April 2020 15:02
To: [email protected]
Cc: [email protected]; Anthony PERARD <[email protected]>; Stefano Stabellini <[email protected]>; Paul Durrant <[email protected]>; Stefan Hajnoczi <[email protected]>; Kevin Wolf <[email protected]>; Max Reitz <[email protected]>; [email protected]; qemu- [email protected]
Subject: [PATCH v2 for-5.0] xen-block: Fix double qlist remove and request leak

Quoted 8 lines

Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on remove") revealed that a request was removed twice from a list, once in xen_block_finish_request() and a second time in xen_block_release_request() when both function are called from xen_block_complete_aio(). But also, the `requests_inflight' counter is decreased twice, and thus became negative.

This is a bug that was introduced in bfd0d6366043

NIT: I guess you should quote the patch title here as well.

Quoted 7 lines

, where a `finished'
list was removed.

That commit also introduced a leak of request in xen_block_do_aio(). That function calls xen_block_finish_request() but the request is never released after that.

Quoted 5 lines

To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let now the result to the other end,

"we need to let the other end know the result"

Quoted 4 lines

thus we should call xen_block_send_response() before
releasing a request.

The first change fix the double QLIST_REMOVE() as we remove the extra

s/fix/fixes

Quoted 2 lines

call. The second change makes the leak go away because if we want to call finish_request(), we need to call a function that do all of

s/do/does

Quoted 4 lines

finish, send response, and release.

Fixes bfd0d6366043 ("xen-block: improve response latency")

The code looks ok, so with the cosmetic fixes...

#5 lore raw

Re: [PATCH v2 for-5.0] xen-block: Fix double qlist remove and request leak

In reply to Anthony PERARD
Quoted 26 lines · Anthony PERARD

Commit a31ca6801c02 ("qemu/queue.h: clear linked list pointers on remove") revealed that a request was removed twice from a list, once in xen_block_finish_request() and a second time in xen_block_release_request() when both function are called from xen_block_complete_aio(). But also, the `requests_inflight' counter is decreased twice, and thus became negative.

This is a bug that was introduced in bfd0d6366043, where a `finished' list was removed.

That commit also introduced a leak of request in xen_block_do_aio(). That function calls xen_block_finish_request() but the request is never released after that.

To fix both issue, we do two changes:
- we squash finish_request() and release_request() together as we want to remove a request from 'inflight' list to add it to 'freelist'.
- before releasing a request, we need to let now the result to the other end, thus we should call xen_block_send_response() before releasing a request.

The first change fix the double QLIST_REMOVE() as we remove the extra call. The second change makes the leak go away because if we want to call finish_request(), we need to call a function that do all of finish, send response, and release.

Quoted 5 lines
Fixes bfd0d6366043 ("xen-block: improve response latency")
---
 hw/block/dataplane/xen-block.c | 48 ++++++++++++----------------------
 1 file changed, 16 insertions(+), 32 deletions(-)

I’m going to send a pull request today anyway, so I hope you won’t mind and let me take this patch to my branch (with Paul’s suggestions incorporated):

https://git.xanclic.moe/XanClic/qemu/commits/branch/block

Max