block: fix blk_aio_*() segfault when blk->root == NULL
cover block: fix blk_aio_*() segfault when blk->root == NULL
Using bdrv_inc_in_flight(blk_bs(blk)) doesn't work since BlockBackend->root may be NULL.
This patch series solves the issue by adding an BlockBackend->in_flight counter so requests can be tracked even when there is no BlockDriverState.
This should fix the IDE and virtio-blk segfaults that have been encountered when there is no BlockDriverState.
The patch is based on work by Kevin Wolf.
Kevin Wolf (1):
block: test blk_aio_flush() with blk->root == NULL
Stefan Hajnoczi (2):
block: add BlockBackend->in_flight counter
Revert "IDE: Do not flush empty CDROM drives"
tests/Makefile.include | 2 ++ block.c | 2 +- block/block-backend.c | 59 +++++++++++++++++++++++++++++---- hw/ide/core.c | 10 +----- tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+), 17 deletions(-) create mode 100644 tests/test-block-backend.c
Signature · 1 line
2.14.3
PATCH 1/3 block: add BlockBackend->in_flight counter
BlockBackend currently relies on BlockDriverState->in_flight to track requests for blk_drain(). There is a corner case where BlockDriverState->in_flight cannot be used though: blk->root can be NULL when there is no medium. This results in a segfault when the NULL pointer is dereferenced.
Introduce a BlockBackend->in_flight counter for aio requests so it works even when blk->root == NULL.
Based on a patch by Kevin Wolf <[email protected]>.
--- block.c | 2 +- block/block-backend.c | 59 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 8 deletions(-)
block.c +1 -1
@@ -4716,7 +4716,7 @@ out:AioContext *bdrv_get_aio_context (BlockDriverState *bs) { -return bs->aio_context; +return bs ? bs->aio_context :qemu_get_aio_context (); }void bdrv_coroutine_enter (BlockDriverState *bs,Coroutine *co)
block/block-backend.c +52 -7
104 lines not on this page · raw message
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 1/3] block: add BlockBackend->in_flight counter
Quoted 9 lines · Stefan Hajnoczi
BlockBackend currently relies on
BlockDriverState->in_flightto track requests forblk_drain(). There is a corner case whereBlockDriverState->in_flightcannot be used though:blk->rootcan be NULL when there is no medium. This results in a segfault when the NULL pointer is dereferenced.Introduce a
BlockBackend->in_flightcounter for aio requests so it works even whenblk->root== NULL.
Quoted 9 lines
Based on a patch by Kevin Wolf <[email protected]>.
Signed-off-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- block.c | 2 +- block/block-backend.c | 59 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 8 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH 1/3] block: add BlockBackend->in_flight counter
In reply to Stefan Hajnocziblock/block-backend.c int blk_flush(BlockBackend *blk) · line 1551
Quoted 13 lines · Stefan Hajnoczi
block/block-backend.c +13
-0+BlockDriverState *bs =blk_bs (blk); + +if (bs) { +bdrv_drained_begin (bs); + } + +/* We may have aio requests like -ENOMEDIUM in flight */ +while (atomic_mb_read (&blk->in_flight) >0 ) { +aio_poll (blk_get_aio_context (blk), true); + } + +if (bs) { +bdrv_drained_end (bs);
This only works if you are in the default AioContext, otherwise you can run handlers for one AioContexts in two threads.
bdrv_dec_in_flight uses bdrv_wakeup and BDRV_POLL_WHILE to ensure that this doesn't happen, so there would be more code that you have to copy into block-backend.c.
Paolo
Re: [Qemu-devel] [PATCH 1/3] block: add BlockBackend->in_flight counter
Am 09.02.2018 um 17:28 hat Paolo Bonzini geschrieben:
Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines
block/block-backend.c +8
-0+BlockDriverState *bs =blk_bs (blk); + +if (bs) { +bdrv_drained_begin (bs); + } + +/* We may have aio requests like -ENOMEDIUM in flight */ +while (atomic_mb_read (&blk->in_flight) >0 ) {
Quoted 8 lines
block/block-backend.c int blk_flush(BlockBackend *blk) · line 1551
Quoted 5 lines
block/block-backend.c +5
-0+aio_poll (blk_get_aio_context (blk), true); + } + +if (bs) { +bdrv_drained_end (bs);This only works if you are in the default AioContext, otherwise you can run handlers for one AioContexts in two threads.
Should aio_poll() assert that it's called in the right thread to make sure we obey the rules?
Quoted 3 lines
bdrv_dec_in_flightusesbdrv_wakeupandBDRV_POLL_WHILEto ensure that this doesn't happen, so there would be more code that you have to copy intoblock-backend.c.
Instead of copying, can't we generalise it into a POLL_WHILE(ctx, wakeup, cond) and make BDRV_POLL_WHILE() a wrapper for that?
Kevin
Re: [Qemu-devel] [PATCH 1/3] block: add BlockBackend->in_flight counter
Quoted 22 lines · Kevin Wolf
Am 09.02.2018 um 17:28 hat Paolo Bonzini geschrieben:
Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines
block/block-backend.c +8
-0+BlockDriverState *bs =blk_bs (blk); + +if (bs) { +bdrv_drained_begin (bs); + } + +/* We may have aio requests like -ENOMEDIUM in flight */ +while (atomic_mb_read (&blk->in_flight) >0 ) {Quoted 8 lines
block/block-backend.c int blk_flush(BlockBackend *blk) · line 1551
Quoted 5 lines
block/block-backend.c +5
-0+aio_poll (blk_get_aio_context (blk), true); + } + +if (bs) { +bdrv_drained_end (bs);This only works if you are in the default AioContext, otherwise you can run handlers for one AioContexts in two threads.
Should
aio_poll()assert that it's called in the right thread to make sure we obey the rules?
Quoted 6 lines
Quoted 3 lines
bdrv_dec_in_flightusesbdrv_wakeupandBDRV_POLL_WHILEto ensure that this doesn't happen, so there would be more code that you have to copy intoblock-backend.c.Instead of copying, can't we generalise it into a
POLL_WHILE(ctx, wakeup, cond)and makeBDRV_POLL_WHILE()a wrapper for that?
Yes, or even move bdrv_wakeup to AioContext would do. We already have block layer-specific fields such as the linux-aio state(*).
(*) though now that linux-aio has been improved to do something
like epoll, there may be a better reason to place linux-aio state in AioContext.
Paolo
Re: [Qemu-devel] [PATCH 1/3] block: add BlockBackend->in_flight counter
Quoted 30 lines · Paolo Bonzini
Quoted 22 lines · Kevin Wolf
Am 09.02.2018 um 17:28 hat Paolo Bonzini geschrieben:
Quoted 9 lines · Stefan Hajnoczi
Quoted 8 lines
block/block-backend.c +8
-0+BlockDriverState *bs =blk_bs (blk); + +if (bs) { +bdrv_drained_begin (bs); + } + +/* We may have aio requests like -ENOMEDIUM in flight */ +while (atomic_mb_read (&blk->in_flight) >0 ) {Quoted 8 lines
block/block-backend.c int blk_flush(BlockBackend *blk) · line 1551
Quoted 5 lines
block/block-backend.c +5
-0+aio_poll (blk_get_aio_context (blk), true); + } + +if (bs) { +bdrv_drained_end (bs);This only works if you are in the default AioContext, otherwise you can run handlers for one AioContexts in two threads.
Should
aio_poll()assert that it's called in the right thread to make sure we obey the rules?Quoted 6 lines
Quoted 3 lines
bdrv_dec_in_flightusesbdrv_wakeupandBDRV_POLL_WHILEto ensure that this doesn't happen, so there would be more code that you have to copy intoblock-backend.c.Instead of copying, can't we generalise it into a
POLL_WHILE(ctx, wakeup, cond)and makeBDRV_POLL_WHILE()a wrapper for that?
Quoted 6 lines
Yes, or even move
bdrv_wakeupto AioContext would do. We already have block layer-specific fields such as the linux-aiostate(*).(*) though now that linux-aio has been improved to do something
like epoll, there may be a better reason to place linux-aio state in AioContext.
Thanks for the ideas, will fix in v2.
Stefan
PATCH 2/3 block: test blk_aio_flush() with blk->root == NULL
In reply to Stefan HajnocziFrom: Kevin Wolf <[email protected]>
This patch adds test cases for the scenario where blk_aio_flush() is called on a BlockBackend with no root. Calling drain afterwards should complete the requests with -ENOMEDIUM.
--- tests/Makefile.include | 2 ++ tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 tests/test-block-backend.c
tests/Makefile.include +2 -0
@@ -83,6 +83,7 @@ gcov-files-test-hbitmap-y = blockjob.c check-unit-y += tests/test-bdrv-drain$(EXESUF) check-unit-y += tests/test-blockjob$(EXESUF) check-unit-y += tests/test-blockjob-txn$(EXESUF) +check-unit-y += tests/test-block-backend$(EXESUF) check-unit-y += tests/test-x86-cpuid$(EXESUF) # all code tested by test-x86-cpuid is inside topology.h gcov-files-test-x86-cpuid-y =
@@ -609,6 +610,7 @@ tests/test-throttle$(EXESUF): tests/test-throttle.o $(test-block-obj-y) tests/test-bdrv-drain$(EXESUF): tests/test-bdrv-drain.o $(test-block-obj-y) $(test-util-obj-y) tests/test-blockjob$(EXESUF): tests/test-blockjob.o $(test-block-obj-y) $(test-util-obj-y) tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) $(test-util-obj-y) +tests/test-block-backend$(EXESUF): tests/test-block-backend.o $(test-block-obj-y) $(test-util-obj-y) tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y) tests/test-iov$(EXESUF): tests/test-iov.o $(test-util-obj-y) tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o $(test-util-obj-y) $(test-crypto-obj-y)
tests/test-block-backend.c new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 2/3] block: test blk_aio_flush() with blk->root == NULL
Quoted 17 lines · Stefan Hajnoczi
From: Kevin Wolf <[email protected]>
This patch adds test cases for the scenario where
blk_aio_flush()is called on a BlockBackend with no root. Calling drain afterwards should complete the requests with-ENOMEDIUM.Signed-off-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- tests/Makefile.include | 2 ++ tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)create mode 100644
tests/test-block-backend.c+++
b/tests/test-block-backend.c
tests/test-block-backend.c line 4
Quoted 4 lines
tests/test-block-backend.c new file +4
-0@@ -0,0 +1,82 @@ +/* +* BlockBackend tests +* +* Copyright (c) 2017 Kevin Wolf <[email protected]>
Want to include 2018? Using a redhat.com address but a personal copyright? (Then again, Red Hat has some pretty cool policies on what it permits for code written on an employee's own time).
As long as Kevin is happy with the final copyright line that goes into the commit:
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH 2/3] block: test blk_aio_flush() with blk->root == NULL
Quoted 27 lines · Eric Blake
Quoted 17 lines · Stefan Hajnoczi
From: Kevin Wolf <[email protected]>
This patch adds test cases for the scenario where
blk_aio_flush()is called on a BlockBackend with no root. Calling drain afterwards should complete the requests with-ENOMEDIUM.Signed-off-by Kevin Wolf <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- tests/Makefile.include | 2 ++ tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+)create mode 100644
tests/test-block-backend.c+++
b/tests/test-block-backend.ctests/test-block-backend.c line 4
Quoted 4 lines
tests/test-block-backend.c new file +4
-0@@ -0,0 +1,82 @@ +/* +* BlockBackend tests +* +* Copyright (c) 2017 Kevin Wolf <[email protected]> Want to include 2018? Using a redhat.com address but a personal copyright? (Then again, Red Hat has some pretty cool policies on what it permits for code written on an employee's own time).
Quoted 4 lines
As long as Kevin is happy with the final copyright line that goes into the commit:
Reviewed-by Eric Blake <[email protected]>
This is how I got the test case from Kevin, I didn't change the copyright line.
Stefan
PATCH 3/3 Revert "IDE: Do not flush empty CDROM drives"
In reply to Stefan HajnocziThis reverts commit 4da97120d51a4383aa96d741a2b837f8c4bbcd0b.
blk_aio_flush() now handles the blk->root == NULL case, so we no longer need this workaround.
--- hw/ide/core.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
hw/ide/core.c +1 -9
@@ -1085,15 +1085,7 @@ static void ide_flush_cache(IDEState *s) s->status |=BUSY_STAT ;ide_set_retry (s);block_acct_start (blk_get_stats (s->blk), &s->acct,0 ,BLOCK_ACCT_FLUSH ); - -if (blk_bs (s->blk)) { - s->pio_aiocb =blk_aio_flush (s->blk, ide_flush_cb, s); - }else { -/* XXX blk_aio_flush() crashes when blk_bs(blk) is NULL, remove this -* temporary workaround when blk_aio_*() functions handle NULL blk_bs. -*/ -ide_flush_cb (s,0 ); - } + s->pio_aiocb =blk_aio_flush (s->blk, ide_flush_cb, s); }static void ide_cfata_metadata_inquiry (IDEState *s)
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 3/3] Revert "IDE: Do not flush empty CDROM drives"
Quoted 11 lines · Stefan Hajnoczi
This reverts commit
4da97120d51a4383aa96d741a2b837f8c4bbcd0b.
blk_aio_flush()now handles theblk->root== NULL case, so we no longer need this workaround.Cc John Snow <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- hw/ide/core.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Quoted 9 lines · Stefan Hajnoczi
Using bdrv_inc_in_flight(blk_bs(blk)) doesn't work since
BlockBackend->rootmay be NULL.This patch series solves the issue by adding an
BlockBackend->in_flightcounter so requests can be tracked even when there is no BlockDriverState.This should fix the IDE and virtio-blk segfaults that have been encountered when there is no BlockDriverState.
Quoted 8 lines
The patch is based on work by Kevin Wolf.
Kevin Wolf (1):
block: test blk_aio_flush() with blk->root == NULLStefan Hajnoczi (2):
block: addBlockBackend->in_flightcounter
Revert "IDE: Do not flush empty CDROM drives"
Tested by applying the series out of order (2, 3, 1) - the new test fails after 2 (so even our workaround was not robust), fails after 3 (expected there, because we revert the workaround), then finally passes after 1 (the correct fix with no workaround needed). So for the series, you can add:
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org