block: fix blk_aio_*() segfault when blk->root == NULL
v3 cover block: fix blk_aio_*() segfault when blk->root == NULL
v3:
* Add Patch 1 to rename aio_context_in_iothread() to in_aio_context_home_thread() [Eric]
v2:
* Introduce AIO_WAIT_WHILE() since aio_poll(ctx, true) is not allowed [Paolo]
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 (4):
aio: rename aio_context_in_iothread() to in_aio_context_home_thread()
block: extract AIO_WAIT_WHILE() from BlockDriverState
block: add BlockBackend->in_flight counter
Revert "IDE: Do not flush empty CDROM drives"
tests/Makefile.include | 2 + util/Makefile.objs | 2 +- include/block/aio-wait.h | 116 +++++++++++++++++++++++++++++++++++++++++++++ include/block/aio.h | 7 ++- include/block/block.h | 40 +++------------- include/block/block_int.h | 7 ++- block.c | 7 ++- block/block-backend.c | 60 ++++++++++++++++++++--- block/io.c | 10 +--- hw/ide/core.c | 10 +--- tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++ util/aio-wait.c | 40 ++++++++++++++++ 12 files changed, 318 insertions(+), 65 deletions(-) create mode 100644 include/block/aio-wait.h create mode 100644 tests/test-block-backend.c create mode 100644 util/aio-wait.c
Signature · 1 line
2.14.3
PATCH v3 1/5 aio: rename aio_context_in_iothread() to in_aio_context_home_thread()
The name aio_context_in_iothread() is misleading because it also return true when called on the main AioContext from the main loop thread, which is not an IOThread.
This patch renames it to in_aio_context_home_thread() and expands the doc comment to make the semantics clearer.
--- include/block/aio.h | 7 +++++-- include/block/block.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-)
include/block/aio.h +5 -2
@@ -534,11 +534,14 @@ void aio_co_enter(AioContext *ctx, struct Coroutine *co);AioContext *qemu_get_current_aio_context (void );/** +* in_aio_context_home_thread: * @ctx: the aio context * -* Return whether we are running in the I/O thread that manages @ctx. +* Return whether we are running in the thread that normally runs @ctx. Note +* that acquiring/releasing ctx does not affect the outcome, each AioContext +* still only has a one home thread that is responsible for running it. */ -static inline bool aio_context_in_iothread (AioContext *ctx) +static inline bool in_aio_context_home_thread (AioContext *ctx) {return ctx ==qemu_get_current_aio_context (); }
include/block/block.h +1 -1
@@ -372,7 +372,7 @@ void bdrv_drain_all(void);bool busy_ = true; \BlockDriverState *bs_ = (bs); \AioContext *ctx_ =bdrv_get_aio_context (bs_); \ -if (aio_context_in_iothread (ctx_)) { \ +if (in_aio_context_home_thread (ctx_)) { \while ((cond) || busy_) { \ busy_ =aio_poll (ctx_, (cond)); \ waited_ |= !!(cond) | busy_; \
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH v3 1/5] aio: rename aio_context_in_iothread() to in_aio_context_home_thread()
Quoted 1 line · Stefan Hajnoczi
The name
aio_context_in_iothread()is misleading because it also return
s/return/returns/
Quoted 13 lines
true when called on the main AioContext from the main loop thread, which is not an IOThread.
This patch renames it to
in_aio_context_home_thread()and expands the doc comment to make the semantics clearer.Signed-off-by Stefan Hajnoczi <[email protected]>--- include/block/aio.h | 7 +++++-- include/block/block.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-)
Quoted 8 lines
/**
+ *in_aio_context_home_thread:
* @ctx: the aio context
*
- * Return whether we are running in the I/O thread that manages @ctx.
+ * Return whether we are running in the thread that normally runs @ctx. Note
+ * that acquiring/releasing ctx does not affect the outcome, each AioContext
+ * still only has a one home thread that is responsible for running it.
s/a one/one/
Typo fixes are trivial, so
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v3 2/5 block: extract AIO_WAIT_WHILE() from BlockDriverState
In reply to Stefan HajnocziBlockDriverState has the BDRV_POLL_WHILE() macro to wait on event loop activity while a condition evaluates to true. This is used to implement synchronous operations where it acts as a condvar between the IOThread running the operation and the main loop waiting for the operation. It can also be called from the thread that owns the AioContext and in that case it's just a nested event loop.
BlockBackend needs this behavior but doesn't always have a BlockDriverState it can use. This patch extracts BDRV_POLL_WHILE() into the AioWait abstraction, which can be used with AioContext and isn't tied to BlockDriverState anymore.
This feature could be built directly into AioContext but then all users would kick the event loop even if they signal different conditions. Imagine an AioContext with many BlockDriverStates, each time a request completes any waiter would wake up and re-check their condition. It's nicer to keep a separate AioWait object for each condition instead.
Please see "block/aio-wait.h" for details on the API.
The name AIO_WAIT_WHILE() avoids the confusion between AIO_POLL_WHILE() and AioContext polling.
--- util/Makefile.objs | 2 +- include/block/aio-wait.h | 116 ++++++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 40 +++------------- include/block/block_int.h | 7 ++- block.c | 5 ++ block/io.c | 10 +--- util/aio-wait.c | 40 ++++++++++++++++ 7 files changed, 174 insertions(+), 46 deletions(-) create mode 100644 include/block/aio-wait.h create mode 100644 util/aio-wait.c
util/Makefile.objs +1 -1
@@ -1,7 +1,7 @@ util-obj-y = osdep.o cutils.o unicode.o qemu-timer-common.o util-obj-y += bufferiszero.o util-obj-y += lockcnt.o -util-obj-y += aiocb.o async.o thread-pool.o qemu-timer.o +util-obj-y += aiocb.o async.o aio-wait.o thread-pool.o qemu-timer.o util-obj-y += main-loop.o iohandler.o util-obj-$(CONFIG_POSIX) += aio-posix.o util-obj-$(CONFIG_POSIX) += compatfd.o
include/block/aio-wait.h new file +116 -0
116 lines not on this page · raw message
include/block/block.h +7 -33
@@ -2,6 +2,7 @@#define BLOCK_H #include "block/aio.h" +#include "block/aio-wait.h" #include "qapi-types.h" #include "qemu/iov.h" #include "qemu/coroutine.h"
@@ -367,41 +368,14 @@ void bdrv_drain_all_begin(void);void bdrv_drain_all_end (void );void bdrv_drain_all (void ); +/* Returns NULL when bs == NULL */ +AioWait *bdrv_get_aio_wait (BlockDriverState *bs); +#define BDRV_POLL_WHILE (bs, cond) ({ \ - bool waited_ = false; \ - bool busy_ = true; \ BlockDriverState *bs_ = (bs); \ - AioContext *ctx_ = bdrv_get_aio_context(bs_); \ - if (in_aio_context_home_thread(ctx_)) { \ - while ((cond) || busy_) { \ - busy_ = aio_poll(ctx_, (cond)); \ - waited_ |= !!(cond) | busy_; \ - } \ - } else { \ - assert(qemu_get_current_aio_context() == \ - qemu_get_aio_context()); \ -/* Ask bdrv_dec_in_flight to wake up the main \ -* QEMU AioContext. Extra I/O threads never take \ -* other I/O threads' AioContexts (see for example \ -* block_job_defer_to_main_loop for how to do it). \ -*/ \ -assert (!bs_->wakeup); \ -/* Set bs->wakeup before evaluating cond. */ \ -atomic_mb_set (&bs_->wakeup, true); \ -while (busy_) { \ -if ((cond)) { \ - waited_ = busy_ = true; \ -aio_context_release (ctx_); \ -aio_poll (qemu_get_aio_context (), true); \ -aio_context_acquire (ctx_); \ - }else { \ - busy_ =aio_poll (ctx_, false); \ - waited_ |= busy_; \ - } \ - } \ -atomic_set (&bs_->wakeup, false); \ - } \ - waited_; }) + AIO_WAIT_WHILE(bdrv_get_aio_wait(bs_), \ + bdrv_get_aio_context(bs_), \ + cond); })int bdrv_pdiscard (BlockDriverState *bs,int64_t offset,int bytes);int bdrv_co_pdiscard (BlockDriverState *bs,int64_t offset,int bytes);
include/block/block_int.h +3 -4
@@ -26,6 +26,7 @@#include "block/accounting.h" #include "block/block.h" +#include "block/aio-wait.h" #include "qemu/queue.h" #include "qemu/coroutine.h" #include "qemu/stats64.h"
@@ -709,10 +710,8 @@ struct BlockDriverState {unsigned int in_flight;unsigned int serialising_in_flight; -/* Internal to BDRV_POLL_WHILE and bdrv_wakeup. Accessed with atomic -* ops. -*/ -bool wakeup; +/* Kicked to signal main loop when a request completes. */ +AioWait wait;/* counter for nested bdrv_io_plug. * Accessed with atomic ops.
block.c +5 -0
@@ -4716,6 +4716,11 @@ AioContext *bdrv_get_aio_context(BlockDriverState *bs)return bs->aio_context; } +AioWait *bdrv_get_aio_wait (BlockDriverState *bs) +{ +return bs ? &bs->wait :NULL ; +} +void bdrv_coroutine_enter (BlockDriverState *bs,Coroutine *co) {aio_co_enter (bdrv_get_aio_context (bs), co);
block/io.c +2 -8
@@ -25,6 +25,7 @@#include "qemu/osdep.h" #include "trace.h" #include "sysemu/block-backend.h" +#include "block/aio-wait.h" #include "block/blockjob.h" #include "block/blockjob_int.h" #include "block/block_int.h"
@@ -587,16 +588,9 @@ void bdrv_inc_in_flight(BlockDriverState *bs)atomic_inc (&bs->in_flight); } -static void dummy_bh_cb (void *opaque) -{ -} -void bdrv_wakeup (BlockDriverState *bs) { -/* The barrier (or an atomic op) is in the caller. */ -if (atomic_read (&bs->wakeup)) { -aio_bh_schedule_oneshot (qemu_get_aio_context (), dummy_bh_cb,NULL ); - } +aio_wait_kick (bdrv_get_aio_wait (bs)); }void bdrv_dec_in_flight (BlockDriverState *bs)
util/aio-wait.c new file +40 -0
@@ -0,0 +1,40 @@ +/* +* AioContext wait support +* +* Copyright (C) 2018 Red Hat, Inc. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qemu/main-loop.h" +#include "block/aio-wait.h" + +static void dummy_bh_cb (void *opaque) +{ +/* The point is to make AIO_WAIT_WHILE()'s aio_poll() return */ +} + +void aio_wait_kick (AioWait *wait) +{ +/* The barrier (or an atomic op) is in the caller. */ +if (atomic_read (&wait->need_kick)) { +aio_bh_schedule_oneshot (qemu_get_aio_context (), dummy_bh_cb,NULL ); + } +}
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH v3 2/5] block: extract AIO_WAIT_WHILE() from BlockDriverState
Quoted 18 lines · Stefan Hajnoczi
BlockDriverState has the
BDRV_POLL_WHILE()macro to wait on event loop activity while a condition evaluates to true. This is used to implement synchronous operations where it acts as a condvar between the IOThread running the operation and the main loop waiting for the operation. It can also be called from the thread that owns the AioContext and in that case it's just a nested event loop.BlockBackend needs this behavior but doesn't always have a BlockDriverState it can use. This patch extracts
BDRV_POLL_WHILE()into the AioWait abstraction, which can be used with AioContext and isn't tied to BlockDriverState anymore.This feature could be built directly into AioContext but then all users would kick the event loop even if they signal different conditions. Imagine an AioContext with many BlockDriverStates, each time a request completes any waiter would wake up and re-check their condition. It's nicer to keep a separate AioWait object for each condition instead.
Quoted 7 lines
Please see "
block/aio-wait.h" for details on the API.The name
AIO_WAIT_WHILE()avoids the confusion betweenAIO_POLL_WHILE()and AioContext polling.Signed-off-by Stefan Hajnoczi <[email protected]>---
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
PATCH v3 3/5 block: add BlockBackend->in_flight counter
In reply to Stefan HajnocziBlockBackend 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 | 60 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 54 insertions(+), 8 deletions(-)
block.c +1 -1
@@ -4713,7 +4713,7 @@ out:AioContext *bdrv_get_aio_context (BlockDriverState *bs) { -return bs->aio_context; +return bs ? bs->aio_context :qemu_get_aio_context (); }AioWait *bdrv_get_aio_wait (BlockDriverState *bs)
block/block-backend.c +53 -7
105 lines not on this page · raw message
Signature · 1 line
2.14.3
PATCH v3 4/5 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 =
@@ -612,6 +613,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
PATCH v3 5/5 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
@@ -1087,15 +1087,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
Quoted 26 lines · Stefan Hajnoczi
v3:
* Add Patch 1 to renameaio_context_in_iothread()toin_aio_context_home_thread()[Eric]
v2:
* IntroduceAIO_WAIT_WHILE()sinceaio_poll(ctx, true)is not allowed [Paolo]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.
The patch is based on work by Kevin Wolf.
Kevin Wolf (1):
block: testblk_aio_flush()withblk->root== NULLStefan Hajnoczi (4):
aio: renameaio_context_in_iothread()toin_aio_context_home_thread()
block: extractAIO_WAIT_WHILE()from BlockDriverState
block: addBlockBackend->in_flightcounter
Revert "IDE: Do not flush empty CDROM drives"
Quoted 16 lines
tests/Makefile.include | 2 + util/Makefile.objs | 2 +- include/block/aio-wait.h | 116 +++++++++++++++++++++++++++++++++++++++++++++ include/block/aio.h | 7 ++- include/block/block.h | 40 +++------------- include/block/block_int.h | 7 ++- block.c | 7 ++- block/block-backend.c | 60 ++++++++++++++++++++--- block/io.c | 10 +--- hw/ide/core.c | 10 +--- tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++ util/aio-wait.c | 40 ++++++++++++++++ 12 files changed, 318 insertions(+), 65 deletions(-) create mode 100644 include/block/aio-wait.h create mode 100644 tests/test-block-backend.c create mode 100644 util/aio-wait.c
Any more comments?
Quoted 44 lines · Stefan Hajnoczi
Quoted 26 lines · Stefan Hajnoczi
v3:
* Add Patch 1 to renameaio_context_in_iothread()toin_aio_context_home_thread()[Eric]
v2:
* IntroduceAIO_WAIT_WHILE()sinceaio_poll(ctx, true)is not allowed [Paolo]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.
The patch is based on work by Kevin Wolf.
Kevin Wolf (1):
block: testblk_aio_flush()withblk->root== NULLStefan Hajnoczi (4):
aio: renameaio_context_in_iothread()toin_aio_context_home_thread()
block: extractAIO_WAIT_WHILE()from BlockDriverState
block: addBlockBackend->in_flightcounter
Revert "IDE: Do not flush empty CDROM drives"Quoted 16 lines
tests/Makefile.include | 2 + util/Makefile.objs | 2 +- include/block/aio-wait.h | 116 +++++++++++++++++++++++++++++++++++++++++++++ include/block/aio.h | 7 ++- include/block/block.h | 40 +++------------- include/block/block_int.h | 7 ++- block.c | 7 ++- block/block-backend.c | 60 ++++++++++++++++++++--- block/io.c | 10 +--- hw/ide/core.c | 10 +--- tests/test-block-backend.c | 82 ++++++++++++++++++++++++++++++++ util/aio-wait.c | 40 ++++++++++++++++ 12 files changed, 318 insertions(+), 65 deletions(-) create mode 100644 include/block/aio-wait.h create mode 100644 tests/test-block-backend.c create mode 100644 util/aio-wait.c
Quoted 2 lines
Any more comments?
Not from me. :)
Paolo
Kevin Wolf
AppliedAm 16.02.2018 um 17:50 hat Stefan Hajnoczi geschrieben:
Quoted 9 lines
v3:
* Add Patch 1 to renameaio_context_in_iothread()toin_aio_context_home_thread()[Eric]
v2:
* IntroduceAIO_WAIT_WHILE()sinceaio_poll(ctx, true)is not allowed [Paolo]Using bdrv_inc_in_flight(blk_bs(blk)) doesn't work since
BlockBackend->rootmay be NULL.
Quoted 7 lines
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.
The patch is based on work by Kevin Wolf.
Thanks, applied to the block branch.
Kevin