util/async: use qemu_aio_coroutine_enter in co_schedule_bh_cb
PATCH 1/1 util/async: use qemu_aio_coroutine_enter in co_schedule_bh_cb
AIO Coroutines shouldn't by managed by an AioContext different than the one assigned when they are created. aio_co_enter avoids entering a coroutine from a different AioContext, calling aio_co_schedule instead.
Scheduled coroutines are then entered by co_schedule_bh_cb using qemu_coroutine_enter, which just calls qemu_aio_coroutine_enter with the current AioContext obtained with qemu_get_current_aio_context. Eventually, co->ctx will be set to the AioContext passed as an argument to qemu_aio_coroutine_enter.
This means that, if an IO Thread's AioConext is being processed by the Main Thread (due to aio_poll being called with a BDS AioContext, as it happens in AIO_WAIT_WHILE among other places), the AioContext from some coroutines may be wrongly replaced with the one from the Main Thread.
This is the root cause behind some crashes, mainly triggered by the drain code at block/io.c. The most common are these abort and failed assertion:
util/async.c:aio_co_schedule
456 if (scheduled) {
457 fprintf(stderr,
458 "%s: Co-routine was already scheduled in '%s'\n",
459 __func__, scheduled);
460 abort();
461 }
util/qemu-coroutine-lock.c:
286 assert(mutex->holder == self);
But it's also known to cause random errors at different locations, and even SIGSEGV with broken coroutine backtraces.
By using qemu_aio_coroutine_enter directly in co_schedule_bh_cb, we can pass the correct AioContext as an argument, making sure co->ctx is not wrongly altered.
--- util/async.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
util/async.c +1 -1
@@ -400,7 +400,7 @@ static void co_schedule_bh_cb(void *opaque)/* Protected by write barrier in qemu_aio_coroutine_enter */ atomic_set (&co->scheduled,NULL ); -qemu_coroutine_enter (co); +qemu_aio_coroutine_enter (ctx, co);aio_context_release (ctx); } }
Signature · 1 line
2.17.0
Quoted 58 lines · Sergio Lopez
AIO Coroutines shouldn't by managed by an AioContext different than the one assigned when they are created.
aio_co_enteravoids entering a coroutine from a different AioContext, callingaio_co_scheduleinstead.Scheduled coroutines are then entered by
co_schedule_bh_cbusingqemu_coroutine_enter, which just callsqemu_aio_coroutine_enterwith the current AioContext obtained withqemu_get_current_aio_context. Eventually,co->ctxwill be set to the AioContext passed as an argument toqemu_aio_coroutine_enter.This means that, if an IO Thread's AioConext is being processed by the Main Thread (due to
aio_pollbeing called with a BDS AioContext, as it happens inAIO_WAIT_WHILEamong other places), the AioContext from some coroutines may be wrongly replaced with the one from the Main Thread.This is the root cause behind some crashes, mainly triggered by the drain code at
block/io.c. The most common are these abort and failed assertion:util/async.c:aio_co_schedule
456 if (scheduled) {
457 fprintf(stderr,
458 "%s: Co-routine was already scheduled in '%s'\n",
459__func__, scheduled);
460abort();
461 }
util/qemu-coroutine-lock.c:
286assert(mutex->holder == self);But it's also known to cause random errors at different locations, and even SIGSEGV with broken coroutine backtraces.
By using
qemu_aio_coroutine_enterdirectly inco_schedule_bh_cb, we can pass the correct AioContext as an argument, making sureco->ctxis not wrongly altered.Signed-off-by Sergio Lopez <[email protected]>--- util/async.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/async.c +1
-1@@ -400,7 +400,7 @@ static void co_schedule_bh_cb(void *opaque)/* Protected by write barrier in qemu_aio_coroutine_enter */ atomic_set (&co->scheduled,NULL ); -qemu_coroutine_enter (co); +qemu_aio_coroutine_enter (ctx, co);aio_context_release (ctx); } }Signature · 1 line
2.17.0
Quoted 47 lines · Sergio Lopez
AIO Coroutines shouldn't by managed by an AioContext different than the one assigned when they are created.
aio_co_enteravoids entering a coroutine from a different AioContext, callingaio_co_scheduleinstead.Scheduled coroutines are then entered by
co_schedule_bh_cbusingqemu_coroutine_enter, which just callsqemu_aio_coroutine_enterwith the current AioContext obtained withqemu_get_current_aio_context. Eventually,co->ctxwill be set to the AioContext passed as an argument toqemu_aio_coroutine_enter.This means that, if an IO Thread's AioConext is being processed by the Main Thread (due to
aio_pollbeing called with a BDS AioContext, as it happens inAIO_WAIT_WHILEamong other places), the AioContext from some coroutines may be wrongly replaced with the one from the Main Thread.This is the root cause behind some crashes, mainly triggered by the drain code at
block/io.c. The most common are these abort and failed assertion:util/async.c:aio_co_schedule
456 if (scheduled) {
457 fprintf(stderr,
458 "%s: Co-routine was already scheduled in '%s'\n",
459__func__, scheduled);
460abort();
461 }
util/qemu-coroutine-lock.c:
286assert(mutex->holder == self);But it's also known to cause random errors at different locations, and even SIGSEGV with broken coroutine backtraces.
By using
qemu_aio_coroutine_enterdirectly inco_schedule_bh_cb, we can pass the correct AioContext as an argument, making sureco->ctxis not wrongly altered.Signed-off-by Sergio Lopez <[email protected]>--- util/async.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/async.c +0
-0
util/async.c static void co_schedule_bh_cb(void *opaque) · line 406
Quoted 8 lines
util/async.c +1
-1@@ -400,7 +400,7 @@ static void co_schedule_bh_cb(void *opaque)/* Protected by write barrier in qemu_aio_coroutine_enter */ atomic_set (&co->scheduled,NULL ); -qemu_coroutine_enter (co); +qemu_aio_coroutine_enter (ctx, co);aio_context_release (ctx); } }
Kevin, could you test this patch together with your next version of the drain fix series? Since they are related, it's better if you could include it in your series or even apply it yourself. Peter is not processing pull requests, so scattering fixes in various trees will do no good.
Fam
Am 12.09.2018 um 09:41 hat Fam Zheng geschrieben:
Quoted 49 lines · Sergio Lopez
Quoted 48 lines
AIO Coroutines shouldn't by managed by an AioContext different than the one assigned when they are created.
aio_co_enteravoids entering a coroutine from a different AioContext, callingaio_co_scheduleinstead.Scheduled coroutines are then entered by
co_schedule_bh_cbusingqemu_coroutine_enter, which just callsqemu_aio_coroutine_enterwith the current AioContext obtained withqemu_get_current_aio_context. Eventually,co->ctxwill be set to the AioContext passed as an argument toqemu_aio_coroutine_enter.This means that, if an IO Thread's AioConext is being processed by the Main Thread (due to
aio_pollbeing called with a BDS AioContext, as it happens inAIO_WAIT_WHILEamong other places), the AioContext from some coroutines may be wrongly replaced with the one from the Main Thread.This is the root cause behind some crashes, mainly triggered by the drain code at
block/io.c. The most common are these abort and failed assertion:util/async.c:aio_co_schedule
456 if (scheduled) {
457 fprintf(stderr,
458 "%s: Co-routine was already scheduled in '%s'\n",
459__func__, scheduled);
460abort();
461 }
util/qemu-coroutine-lock.c:
286assert(mutex->holder == self);But it's also known to cause random errors at different locations, and even SIGSEGV with broken coroutine backtraces.
By using
qemu_aio_coroutine_enterdirectly inco_schedule_bh_cb, we can pass the correct AioContext as an argument, making sureco->ctxis not wrongly altered.Signed-off-by Sergio Lopez <[email protected]>--- util/async.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/async.c +0
-0@@ -400,7 +400,7 @@ static void co_schedule_bh_cb(void *opaque)
Quoted 12 lines
util/async.c static void co_schedule_bh_cb(void *opaque) · line 406
Quoted 7 lines
util/async.c +1
-1/* Protected by write barrier in qemu_aio_coroutine_enter */ atomic_set (&co->scheduled,NULL ); -qemu_coroutine_enter (co); +qemu_aio_coroutine_enter (ctx, co);aio_context_release (ctx); } }Kevin, could you test this patch together with your next version of the drain fix series? Since they are related, it's better if you could include it in your series or even apply it yourself. Peter is not processing pull requests, so scattering fixes in various trees will do no good.
Apparently I forgot to send an email, but I already applied this to my block branch.
Kevin
Quoted 48 lines · Sergio Lopez
AIO Coroutines shouldn't by managed by an AioContext different than the one assigned when they are created.
aio_co_enteravoids entering a coroutine from a different AioContext, callingaio_co_scheduleinstead.Scheduled coroutines are then entered by
co_schedule_bh_cbusingqemu_coroutine_enter, which just callsqemu_aio_coroutine_enterwith the current AioContext obtained withqemu_get_current_aio_context. Eventually,co->ctxwill be set to the AioContext passed as an argument toqemu_aio_coroutine_enter.This means that, if an IO Thread's AioConext is being processed by the Main Thread (due to
aio_pollbeing called with a BDS AioContext, as it happens inAIO_WAIT_WHILEamong other places), the AioContext from some coroutines may be wrongly replaced with the one from the Main Thread.This is the root cause behind some crashes, mainly triggered by the drain code at
block/io.c. The most common are these abort and failed assertion:util/async.c:aio_co_schedule
456 if (scheduled) {
457 fprintf(stderr,
458 "%s: Co-routine was already scheduled in '%s'\n",
459__func__, scheduled);
460abort();
461 }
util/qemu-coroutine-lock.c:
286assert(mutex->holder == self);But it's also known to cause random errors at different locations, and even SIGSEGV with broken coroutine backtraces.
By using
qemu_aio_coroutine_enterdirectly inco_schedule_bh_cb, we can pass the correct AioContext as an argument, making sureco->ctxis not wrongly altered.Signed-off-by Sergio Lopez <[email protected]>--- util/async.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)util/async.c +0
-0
util/async.c static void co_schedule_bh_cb(void *opaque) · line 406
Quoted 8 lines
util/async.c +1
-1@@ -400,7 +400,7 @@ static void co_schedule_bh_cb(void *opaque)/* Protected by write barrier in qemu_aio_coroutine_enter */ atomic_set (&co->scheduled,NULL ); -qemu_coroutine_enter (co); +qemu_aio_coroutine_enter (ctx, co);aio_context_release (ctx); } }
Ouch.
Paolo