Skip to contentSkip to conversation

blkdebug: ignore invalid rules in non-coroutine context

PATCH In review Paolo Bonzini 2 participants 5 messages Oct 13, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 blkdebug: ignore invalid rules in non-coroutine context

blkdebug events can be called from either non-coroutine or coroutine contexts. However, some actions (specifically suspend actions and errors reported with immediately=off) only make sense from within a coroutine.

Currently, using those action would lead to an abort() in qemu_coroutine_yield() ("Co-routine is yielding to no one"). Catch them and print an error instead.

---
 block/blkdebug.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
block/blkdebug.c +14 -3
index bbf2948703..bf0aedb17d 100644
@@ -31,6 +31,7 @@
 #include "block/qdict.h"
 #include "qemu/module.h"
 #include "qemu/option.h"
+#include "qemu/error-report.h"
 #include "qapi/qapi-visit-block-core.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qlist.h"
@@ -623,8 +624,13 @@ static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 
     qemu_mutex_unlock(&s->lock);
     if (!immediately) {
-        aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
-        qemu_coroutine_yield();
+        if (qemu_in_coroutine()) {
+            aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
+            qemu_coroutine_yield();
+        } else {
+            error_report("Non-coroutine event %s needs immediately = off\n",
+                         BlkdebugEvent_lookup.array[rule->event]);
+        }
     }
 
     return -error;
@@ -858,7 +864,12 @@ static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
     }
 
     while (actions_count[ACTION_SUSPEND] > 0) {
-        qemu_coroutine_yield();
+        if (qemu_in_coroutine()) {
+            qemu_coroutine_yield();
+        } else {
+            error_report("Non-coroutine event %s cannot suspend\n",
+                         BlkdebugEvent_lookup.array[event]);
+        }
         actions_count[ACTION_SUSPEND]--;
     }
 }
Signature · 1 line
2.37.3
#1 lore raw
Quoted 28 lines · Paolo Bonzini

blkdebug events can be called from either non-coroutine or coroutine contexts. However, some actions (specifically suspend actions and errors reported with immediately=off) only make sense from within a coroutine.

Currently, using those action would lead to an abort() in qemu_coroutine_yield() ("Co-routine is yielding to no one"). Catch them and print an error instead.

---
 block/blkdebug.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
block/blkdebug.c +1 -0
index bbf2948703..bf0aedb17d 100644
@@ -31,6 +31,7 @@
 #include "block/qdict.h"
 #include "qemu/module.h"
 #include "qemu/option.h"
+#include "qemu/error-report.h"
 #include "qapi/qapi-visit-block-core.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qlist.h"
@@ -623,8 +624,13 @@ static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 

block/blkdebug.c static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes, · line 632

Quoted 10 lines
block/blkdebug.c +6 -2
     qemu_mutex_unlock(&s->lock);
     if (!immediately) {
-        aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
-        qemu_coroutine_yield();
+        if (qemu_in_coroutine()) {
+            aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
+            qemu_coroutine_yield();
+        } else {
+            error_report("Non-coroutine event %s needs immediately = off\n",
+                         BlkdebugEvent_lookup.array[rule->event]);

rule_check() is called from blkdebug_co_preadv(), blkdebug_co_pwritev(), blkdebug_co_pwrite_zeroes(), blkdebug_co_pdiscard(), blkdebug_co_block_status() (all marked coroutine_fn), and blkdebug_co_flush() (which looks like it should be marked coroutine_fn).

Ignorant question: how could it be called outside coroutine context?

Also, code smell: reporting an error without taking an error path. But let's worry about that only after I understand the problem you're trying to fix.

Quoted 18 lines
block/blkdebug.c +7 -1
+        }
     }
 
     return -error;
@@ -858,7 +864,12 @@ static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
     }
 
     while (actions_count[ACTION_SUSPEND] > 0) {
-        qemu_coroutine_yield();
+        if (qemu_in_coroutine()) {
+            qemu_coroutine_yield();
+        } else {
+            error_report("Non-coroutine event %s cannot suspend\n",
+                         BlkdebugEvent_lookup.array[event]);
+        }
         actions_count[ACTION_SUSPEND]--;
     }
 }
#2 lore raw
Quoted 4 lines · Markus Armbruster

rule_check() is called from blkdebug_co_preadv(), blkdebug_co_pwritev(), blkdebug_co_pwrite_zeroes(), blkdebug_co_pdiscard(), blkdebug_co_block_status() (all marked coroutine_fn), and blkdebug_co_flush() (which looks like it should be marked coroutine_fn).

Yes (separate patch sent, https://lore.kernel.org/qemu-devel/[email protected]/T/#u).

Quoted 1 line

Ignorant question: how could it be called outside coroutine context?

You're right, only blkdebug_debug_event() can be called outside coroutine context. I confused process_rule() (called by blkdebug_debug_event(), both inside and outside coroutine context) with rule_check() (called in coroutine context).

Quoted 3 lines

Also, code smell: reporting an error without taking an error path. But let's worry about that only after I understand the problem you're trying to fix.

Unfortunately there's no way to know in advance if an event will be called inside vs. outside a coroutine. I can keep the abort() if you think it's preferrable, so what you get is still a crash but with a nicer error message. Since this is debugging code either solution has pros and cons.

Paolo

#3 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 4 lines · Markus Armbruster

rule_check() is called from blkdebug_co_preadv(), blkdebug_co_pwritev(), blkdebug_co_pwrite_zeroes(), blkdebug_co_pdiscard(), blkdebug_co_block_status() (all marked coroutine_fn), and blkdebug_co_flush() (which looks like it should be marked coroutine_fn).

Quoted 6 lines

Yes (separate patch sent, https://lore.kernel.org/qemu-devel/[email protected]/T/#u).

Quoted 1 line

Ignorant question: how could it be called outside coroutine context?

You're right, only blkdebug_debug_event() can be called outside coroutine context. I confused process_rule() (called by blkdebug_debug_event(), both inside and outside coroutine context) with rule_check() (called in coroutine context).

Let's drop the rule_check() hunk then.

Quoted 7 lines
Quoted 3 lines

Also, code smell: reporting an error without taking an error path. But let's worry about that only after I understand the problem you're trying to fix.

Unfortunately there's no way to know in advance if an event will be called inside vs. outside a coroutine. I can keep the abort() if you think it's preferrable, so what you get is still a crash but with a nicer error message. Since this is debugging code either solution has pros and cons.

Let's have another look at the remaining patch hunk:

@@ -858,7 +864,12 @@ static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
}

while (actions_count[ACTION_SUSPEND] > 0) {
- qemu_coroutine_yield();
+ if (qemu_in_coroutine()) {
+ qemu_coroutine_yield();
+ } else {
+ error_report("Non-coroutine event %s cannot suspend\n",
+ BlkdebugEvent_lookup.array[event]);
+ }
actions_count[ACTION_SUSPEND]--;
}
}

If I understand this correctly, the user asked us to suspend, but it now turns out suspend doesn't make sense, so we ignore the request. Correct?

warn_report()? info_report()?

#4 lore raw
Quoted 17 lines · Markus Armbruster

Let's have another look at the remaining patch hunk:

@@ -858,7 +864,12 @@ static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
}

while (actions_count[ACTION_SUSPEND] > 0) {
- qemu_coroutine_yield();
+ if (qemu_in_coroutine()) {
+ qemu_coroutine_yield();
+ } else {
+ error_report("Non-coroutine event %s cannot suspend\n",
+ BlkdebugEvent_lookup.array[event]);
+ }
actions_count[ACTION_SUSPEND]--;
}
}

Quoted 3 lines

If I understand this correctly, the user asked us to suspend, but it now turns out suspend doesn't make sense, so we ignore the request. Correct?

Yes.

Quoted 1 line

warn_report()? info_report()?

Sure, warn_report() can work too.

Paolo