Skip to contentSkip to conversation

block/rbd: Do not use BDS's AioContext

PATCH In review Hanna Czenczek 3 participants 4 messages Feb 12, 2025 – Feb 18, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/rbd: Do not use BDS's AioContext

RBD schedules the request completion code (qemu_rbd_finish_bh()) to run in the BDS's AioContext. The intent seems to be to run it in the same context that the original request coroutine ran in, i.e. the thread on whose stack the RBDTask object exists (see qemu_rbd_start_co()).

However, with multiqueue, that thread is not necessarily the same as the BDS's AioContext. Instead, we need to remember the actual AioContext and schedule the completion BH there.

---
I think I could also drop RBDTask.ctx and just use qemu_coroutine_get_aio_context(RBDTask.co) instead, but this is the version of the patch that was tested and confirmed to fix the issue (I don't have a local reproducer), so I thought I'll post this first.

---
 block/rbd.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
block/rbd.c +6 -4
index af984fb7db..9d4e0817e0 100644
@@ -102,7 +102,7 @@ typedef struct BDRVRBDState {
 } BDRVRBDState;
 
 typedef struct RBDTask {
-    BlockDriverState *bs;
+    AioContext *ctx;
     Coroutine *co;
     bool complete;
     int64_t ret;
@@ -1269,8 +1269,7 @@ static void qemu_rbd_completion_cb(rbd_completion_t c, RBDTask *task)
 {
     task->ret = rbd_aio_get_return_value(c);
     rbd_aio_release(c);
-    aio_bh_schedule_oneshot(bdrv_get_aio_context(task->bs),
-                            qemu_rbd_finish_bh, task);
+    aio_bh_schedule_oneshot(task->ctx, qemu_rbd_finish_bh, task);
 }
 
 static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
@@ -1281,7 +1280,10 @@ static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
                                           RBDAIOCmd cmd)
 {
     BDRVRBDState *s = bs->opaque;
-    RBDTask task = { .bs = bs, .co = qemu_coroutine_self() };
+    RBDTask task = {
+        .ctx = qemu_get_current_aio_context(),
+        .co = qemu_coroutine_self(),
+    };
     rbd_completion_t c;
     int r;
 
Signature · 1 line
2.48.1
#1 lore raw

Am 12.02.2025 um 10:32 hat Hanna Czenczek geschrieben:

Quoted 10 lines

RBD schedules the request completion code (qemu_rbd_finish_bh()) to run in the BDS's AioContext. The intent seems to be to run it in the same context that the original request coroutine ran in, i.e. the thread on whose stack the RBDTask object exists (see qemu_rbd_start_co()).

However, with multiqueue, that thread is not necessarily the same as the BDS's AioContext. Instead, we need to remember the actual AioContext and schedule the completion BH there.

Please add a short summary of what actually happens to the commit message. I had to check the link to remember what the symptoms are.

Quoted 7 lines

---
I think I could also drop RBDTask.ctx and just use qemu_coroutine_get_aio_context(RBDTask.co) instead, but this is the version of the patch that was tested and confirmed to fix the issue (I don't have a local reproducer), so I thought I'll post this first.

Did you figure out why it even makes a difference in which thread qemu_rbd_finish_bh() runs? For context:

    static void qemu_rbd_finish_bh(void *opaque)
    {
        RBDTask *task = opaque;
        task->complete = true;
        aio_co_wake(task->co);
    }

This looks as if it should be working in any thread, except maybe for a missing barrier after updating task->complete - but I think the failure mode for that would be a hang in qemu_rbd_start_co().

Quoted 27 lines
 block/rbd.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
block/rbd.c +2 -3
index af984fb7db..9d4e0817e0 100644
@@ -102,7 +102,7 @@ typedef struct BDRVRBDState {
 } BDRVRBDState;
 
 typedef struct RBDTask {
-    BlockDriverState *bs;
+    AioContext *ctx;
     Coroutine *co;
     bool complete;
     int64_t ret;
@@ -1269,8 +1269,7 @@ static void qemu_rbd_completion_cb(rbd_completion_t c, RBDTask *task)
 {
     task->ret = rbd_aio_get_return_value(c);
     rbd_aio_release(c);
-    aio_bh_schedule_oneshot(bdrv_get_aio_context(task->bs),
-                            qemu_rbd_finish_bh, task);
+    aio_bh_schedule_oneshot(task->ctx, qemu_rbd_finish_bh, task);
 }
 
 static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,

block/rbd.c static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs, · line 1288

Quoted 10 lines
block/rbd.c +4 -1
@@ -1281,7 +1280,10 @@ static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
                                           RBDAIOCmd cmd)
 {
     BDRVRBDState *s = bs->opaque;
-    RBDTask task = { .bs = bs, .co = qemu_coroutine_self() };
+    RBDTask task = {
+        .ctx = qemu_get_current_aio_context(),
+        .co = qemu_coroutine_self(),
+    };
     rbd_completion_t c;
     int r;

Nothing wrong I can see about the change, but I don't understand why it fixes the problem.

Kevin

#2 lore raw
Quoted 6 lines · Kevin Wolf

Am 12.02.2025 um 10:32 hat Hanna Czenczek geschrieben:

Quoted 5 lines

RBD schedules the request completion code (qemu_rbd_finish_bh()) to run in the BDS's AioContext. The intent seems to be to run it in the same context that the original request coroutine ran in, i.e. the thread on whose stack the RBDTask object exists (see qemu_rbd_start_co()).

Quoted 7 lines
Quoted 5 lines

However, with multiqueue, that thread is not necessarily the same as the BDS's AioContext. Instead, we need to remember the actual AioContext and schedule the completion BH there.

Please add a short summary of what actually happens to the commit message. I had to check the link to remember what the symptoms are.

Sure.  The problem is, I don’t know exactly what’s going wrong (looked like a coroutine being rescheduled after it was already done), and I don’t know how this fixes it.

Quoted 17 lines
Quoted 7 lines

---
I think I could also drop RBDTask.ctx and just use qemu_coroutine_get_aio_context(RBDTask.co) instead, but this is the version of the patch that was tested and confirmed to fix the issue (I don't have a local reproducer), so I thought I'll post this first.

Did you figure out why it even makes a difference in which thread qemu_rbd_finish_bh() runs? For context:

     static void qemu_rbd_finish_bh(void *opaque)
     {
         RBDTask *task = opaque;
         task->complete = true;
         aio_co_wake(task->co);
     }
Quoted 3 lines

This looks as if it should be working in any thread, except maybe for a missing barrier after updating task->complete - but I think the failure mode for that would be a hang in qemu_rbd_start_co().

Yes, I thought the same thing.  All I could imagine was that maybe reading task->co returns the wrong result, but given how long ago that must have been set, it seems quite unlikely (to say the least).  In addition, qemu_rbd_completion_cb() already reads the object from a different thread, and that seems to work fine.

Really, all I know is that the notion of a BDS’s AioContext no longer makes sense in a multiqueue I/O path, so this should be scheduled in the I/O’s AioContext (just conceptually speaking), and that this seems to fix the bug.

Quoted 25 lines
Quoted 25 lines
  block/rbd.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
block/rbd.c +2 -3
index af984fb7db..9d4e0817e0 100644
@@ -102,7 +102,7 @@ typedef struct BDRVRBDState {
  } BDRVRBDState;
 
  typedef struct RBDTask {
-    BlockDriverState *bs;
+    AioContext *ctx;
      Coroutine *co;
      bool complete;
      int64_t ret;
@@ -1269,8 +1269,7 @@ static void qemu_rbd_completion_cb(rbd_completion_t c, RBDTask *task)
  {
      task->ret = rbd_aio_get_return_value(c);
      rbd_aio_release(c);
-    aio_bh_schedule_oneshot(bdrv_get_aio_context(task->bs),
-                            qemu_rbd_finish_bh, task);
+    aio_bh_schedule_oneshot(task->ctx, qemu_rbd_finish_bh, task);
  }
 
Quoted 14 lines

block/rbd.c static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs, · line 1288

Quoted 12 lines
block/rbd.c +4 -1
  static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
@@ -1281,7 +1280,10 @@ static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
                                            RBDAIOCmd cmd)
  {
      BDRVRBDState *s = bs->opaque;
-    RBDTask task = { .bs = bs, .co = qemu_coroutine_self() };
+    RBDTask task = {
+        .ctx = qemu_get_current_aio_context(),
+        .co = qemu_coroutine_self(),
+    };
      rbd_completion_t c;
      int r;

Nothing wrong I can see about the change, but I don't understand why it fixes the problem.

Me neither.  But if this patch had been part of one of the original multiqueue series (without pointing out the linked bug), would there have been any argument against it?

Indeed it is a problem that I don’t understand what’s happening. But even more honestly, I’ll have to admit I can’t ever claim to understand what’s happening in a multi-threaded asynchronous C environment; even more so when the reproducer is installing Windows on RBD.

Hanna

#3 lore raw
Quoted 46 lines · Hanna Czenczek
Quoted 6 lines · Kevin Wolf

Am 12.02.2025 um 10:32 hat Hanna Czenczek geschrieben:

Quoted 5 lines

RBD schedules the request completion code (qemu_rbd_finish_bh()) to run in the BDS's AioContext. The intent seems to be to run it in the same context that the original request coroutine ran in, i.e. the thread on whose stack the RBDTask object exists (see qemu_rbd_start_co()).

Quoted 7 lines
Quoted 5 lines

However, with multiqueue, that thread is not necessarily the same as the BDS's AioContext. Instead, we need to remember the actual AioContext and schedule the completion BH there.

Please add a short summary of what actually happens to the commit message. I had to check the link to remember what the symptoms are.

Sure.  The problem is, I don’t know exactly what’s going wrong (looked like a coroutine being rescheduled after it was already done), and I don’t know how this fixes it.

Quoted 17 lines
Quoted 7 lines

---
I think I could also drop RBDTask.ctx and just use qemu_coroutine_get_aio_context(RBDTask.co) instead, but this is the version of the patch that was tested and confirmed to fix the issue (I don't have a local reproducer), so I thought I'll post this first.

Did you figure out why it even makes a difference in which thread qemu_rbd_finish_bh() runs? For context:

     static void qemu_rbd_finish_bh(void *opaque)
     {
         RBDTask *task = opaque;
         task->complete = true;
         aio_co_wake(task->co);
     }
Quoted 3 lines

This looks as if it should be working in any thread, except maybe for a missing barrier after updating task->complete - but I think the failure mode for that would be a hang in qemu_rbd_start_co().

Yes, I thought the same thing.  All I could imagine was that maybe reading task->co returns the wrong result, but given how long ago that must have been set, it seems quite unlikely (to say the least).  In addition, qemu_rbd_completion_cb() already reads the object from a different thread, and that seems to work fine.

Quoted 3 lines

Really, all I know is that the notion of a BDS’s AioContext no longer makes sense in a multiqueue I/O path, so this should be scheduled in the I/O’s AioContext (just conceptually speaking), and that this seems to fix the bug.

Scheduling the BH directly in the task's AioContext should help performance, so I think this patch makes sense irrespective of the bug. Performance results would be needed to justify that though.

With regards to the bug, I'm also not sure where the issue lies. The root cause needs to be understood so that we can be sure that this patch is really a fix and not just papering over a bug that is still reachable in other ways.

Quoted 49 lines
Quoted 30 lines
Quoted 30 lines
  block/rbd.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
block/rbd.c +2 -4
index af984fb7db..9d4e0817e0 100644
@@ -102,7 +102,7 @@ typedef struct BDRVRBDState {
  } BDRVRBDState;
  typedef struct RBDTask {
-    BlockDriverState *bs;
+    AioContext *ctx;
      Coroutine *co;
      bool complete;
      int64_t ret;
@@ -1269,8 +1269,7 @@ static void qemu_rbd_completion_cb(rbd_completion_t c, RBDTask *task)
  {
      task->ret = rbd_aio_get_return_value(c);
      rbd_aio_release(c);
-    aio_bh_schedule_oneshot(bdrv_get_aio_context(task->bs),
-                            qemu_rbd_finish_bh, task);
+    aio_bh_schedule_oneshot(task->ctx, qemu_rbd_finish_bh, task);
  }
  static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
@@ -1281,7 +1280,10 @@ static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
                                            RBDAIOCmd cmd)
  {
      BDRVRBDState *s = bs->opaque;
-    RBDTask task = { .bs = bs, .co = qemu_coroutine_self() };
Quoted 8 lines

block/rbd.c static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs, · line 1288

Quoted 6 lines
block/rbd.c +4 -0
+    RBDTask task = {
+        .ctx = qemu_get_current_aio_context(),
+        .co = qemu_coroutine_self(),
+    };
      rbd_completion_t c;
      int r;

Nothing wrong I can see about the change, but I don't understand why it fixes the problem.

Me neither.  But if this patch had been part of one of the original multiqueue series (without pointing out the linked bug), would there have been any argument against it?

Indeed it is a problem that I don’t understand what’s happening. But even more honestly, I’ll have to admit I can’t ever claim to understand what’s happening in a multi-threaded asynchronous C environment; even more so when the reproducer is installing Windows on RBD.

Hanna