block-ram-registrar: reset when the graph changes
cover block-ram-registrar: reset when the graph changes
Kevin pointed out that block-ram-registrar doesn't handle graph changes. New nodes must have all of guest RAM registered before they can process I/O requests with BDRV_REQ_REGISTERED_BUF.
These patches add a mechanism for monitoring graph changes and use it to refresh registered buffers in block-ram-registrar.
Stefan Hajnoczi (3):
block: add BdrvChildClass->pre_detach() and ->post_attach()
block-backend: add graph change notifier API
block-ram-registrar: reset when the graph changes
include/block/block_int-common.h | 8 +++ include/sysemu/block-backend-global-state.h | 10 +++ block.c | 34 +++++++++ block/block-backend.c | 79 +++++++++++++++++++++ block/block-ram-registrar.c | 40 +++++++++-- block/trace-events | 2 + 6 files changed, 168 insertions(+), 5 deletions(-)
Signature · 1 line
2.39.0
PATCH 1/3 block: add BdrvChildClass->pre_detach() and ->post_attach()
Add callbacks for graph changes and propagate them to the root via child_of_bds. The purpose is to respond to graph changes.
This commit doesn't do anything on its own since the callbacks aren't used by BlockBackend's child_root yet. That will be done in the next commit.
--- include/block/block_int-common.h | 8 ++++++++ block.c | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+)
include/block/block_int-common.h +8 -0
@@ -899,6 +899,14 @@ struct BdrvChildClass { void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child); void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child); +/* +* Notifies the parent that an immediate child or deeper descendant is +* about to be detached or has been attached. Use this to monitor graph +* changes. to_detach->bs and attached->bs can be NULL. +*/ +void (*pre_detach)(BdrvChild *child,BdrvChild *to_detach); +void (*post_attach)(BdrvChild *child,BdrvChild *attached); +/* * Notifies the parent that the filename of its child has changed (e.g. * because the direct child was removed from the backing chain), so that it
block.c +34 -0
@@ -1402,6 +1402,30 @@ static void bdrv_inherited_options(BdrvChildRole role, bool parent_is_format, *child_flags = flags; } +static void bdrv_child_cb_pre_detach (BdrvChild *child,BdrvChild *to_detach) +{ +BlockDriverState *parent_bs = child->opaque; +BdrvChild *grandparent; + + QLIST_FOREACH(grandparent, &parent_bs->parents, next_parent) { +if (grandparent->klass->pre_detach) { + grandparent->klass->pre_detach (grandparent, to_detach); + } + } +} + +static void bdrv_child_cb_post_attach (BdrvChild *child,BdrvChild *attached) +{ +BlockDriverState *parent_bs = child->opaque; +BdrvChild *grandparent; + + QLIST_FOREACH(grandparent, &parent_bs->parents, next_parent) { +if (grandparent->klass->post_attach) { + grandparent->klass->post_attach (grandparent, attached); + } + } +} + static void GRAPH_WRLOCK bdrv_child_cb_attach(BdrvChild *child) {BlockDriverState *bs = child->opaque;
@@ -1488,6 +1512,8 @@ const BdrvChildClass child_of_bds = { .drained_end = bdrv_child_cb_drained_end, .attach = bdrv_child_cb_attach, .detach = bdrv_child_cb_detach, + .pre_detach = bdrv_child_cb_pre_detach, + .post_attach = bdrv_child_cb_post_attach, .inactivate = bdrv_child_cb_inactivate, .change_aio_ctx = bdrv_child_cb_change_aio_ctx, .update_filename = bdrv_child_cb_update_filename,
@@ -2873,6 +2899,10 @@ static void bdrv_replace_child_noperm(BdrvChild *child,assert (bdrv_get_aio_context (old_bs) ==bdrv_get_aio_context (new_bs)); } +if (child->klass->pre_detach) { + child->klass->pre_detach (child, child); + } +/* TODO Pull this up into the callers to avoid polling here */ bdrv_graph_wrlock ();if (old_bs) {
@@ -2892,6 +2922,10 @@ static void bdrv_replace_child_noperm(BdrvChild *child, }bdrv_graph_wrunlock (); +if (child->klass->post_attach) { + child->klass->post_attach (child, child); + } +/* * If the parent was drained through this BdrvChild previously, but new_bs * is not drained, allow requests to come in only after the new node has
Signature · 1 line
2.39.0
PATCH 2/3 block-backend: add graph change notifier API
In reply to Stefan HajnocziThe existing blk_add_insert_bs_notifier() and blk_add_remove_bs_notifier() APIs only monitor changes to the BlockBackend's root node. There is no way to find out if other nodes are inserted or removed.
Add an API to monitor changes to the full graph. The next commit will use this to reset blk_ram_registrar so that blk_register_buf()/blk_unregister_buf() is called as nodes are inserted/removed anywhere in the graph.
--- include/sysemu/block-backend-global-state.h | 10 +++ block/block-backend.c | 79 +++++++++++++++++++++ block/trace-events | 2 + 3 files changed, 91 insertions(+)
include/sysemu/block-backend-global-state.h +10 -0
@@ -89,6 +89,16 @@ void blk_remove_aio_context_notifier(BlockBackend *blk, void *opaque);void blk_add_remove_bs_notifier (BlockBackend *blk,Notifier *notify);void blk_add_insert_bs_notifier (BlockBackend *blk,Notifier *notify); +void blk_add_graph_change_notifier (BlockBackend *blk, +void (*pre_detach)(BlockBackend *,void *), +void (*post_attach)(BlockBackend *,void *), +void *opaque); +void blk_remove_graph_change_notifier (BlockBackend *blk, +void (*pre_detach)(BlockBackend *, +void *), +void (*post_attach)(BlockBackend *, +void *), +void *opaque);BlockBackendRootState *blk_get_root_state (BlockBackend *blk);void blk_update_root_state (BlockBackend *blk);bool blk_get_detect_zeroes_from_root_state (BlockBackend *blk);
block/block-backend.c +79 -0
121 lines not on this page · raw message
block/trace-events +2 -0
@@ -9,6 +9,8 @@ blk_co_preadv(void *blk, void *bs, int64_t offset, int64_t bytes, int flags) "bl blk_co_pwritev(void *blk, void *bs, int64_t offset, int64_t bytes, int flags) "blk %p bs %p offset %"PRId64" bytes %" PRId64 " flags 0x%x" blk_root_attach(void *child, void *blk, void *bs) "child %p blk %p bs %p" blk_root_detach(void *child, void *blk, void *bs) "child %p blk %p bs %p" +blk_root_pre_detach(void *child, void *blk, void *to_detach, void *to_detach_bs) "child %p blk %p to_detach %p to_detach_bs %p" +blk_root_post_attach(void *child, void *blk, void *attached, void *attached_bs) "child %p blk %p attached %p attached_bs %p" # io.c bdrv_co_preadv_part(void *bs, int64_t offset, int64_t bytes, unsigned int flags) "bs %p offset %" PRId64 " bytes %" PRId64 " flags 0x%x"
Signature · 1 line
2.39.0
PATCH 3/3 block-ram-registrar: reset when the graph changes
In reply to Stefan HajnocziThe BDRV_REQ_REGISTERED_BUF flag can be set on I/O requests to indicate that I/O buffers were previously registered using bdrv_register_buf(). block-ram-registrar automatically registers guest RAM so that emulated storage controllers can set BDRV_REQ_REGISTERED_BUF without having to worry about registering buffers themselves.
Kevin Wolf <[email protected]> pointed out that block-ram-registrar does not handle graph changes. This means buffers aren't unregistered when a node is removed from the graph and buffer registrations aren't replayed when a new node is inserted in the graph. Therefore BDRV_REQ_REGISTERED_BUF may be set but the buffer wasn't previously registered with the new node.
Fix this by monitoring graph changes and replaying buffer registration. Note that the ram_block_notifier_add() API enumerates all RAM blocks so we don't need to manually replay them.
--- block/block-ram-registrar.c | 40 ++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-)
block/block-ram-registrar.c +35 -5
@@ -33,6 +33,35 @@ static void ram_block_removed(RAMBlockNotifier *n, void *host, size_t size,blk_unregister_buf (r->blk, host, max_size); } +static void remove_ram_block_notifier (BlockRAMRegistrar *r) +{ +if (r->ok) { +ram_block_notifier_remove (&r->notifier); + } +} + +static void add_ram_block_notifier (BlockRAMRegistrar *r) +{ + r->ok = true;/* reset flag in case it was false */ +ram_block_notifier_add (&r->notifier); +} + +static void graph_change_pre_detach (BlockBackend *blk,void *opaque) +{ +BlockRAMRegistrar *r = opaque; + +/* Unregisters all buffers from all BDSes */ +remove_ram_block_notifier (r); +} + +static void graph_change_post_attach (BlockBackend *blk,void *opaque) +{ +BlockRAMRegistrar *r = opaque; + +/* Re-registers all buffers with all BDSes */ +add_ram_block_notifier (r); +} +void blk_ram_registrar_init (BlockRAMRegistrar *r,BlockBackend *blk) { r->blk = blk;
@@ -45,14 +74,15 @@ void blk_ram_registrar_init(BlockRAMRegistrar *r, BlockBackend *blk)* value that does not change across resize. */ }; - r->ok = true; -ram_block_notifier_add (&r->notifier); +blk_add_graph_change_notifier (blk, graph_change_pre_detach, + graph_change_post_attach, r); +add_ram_block_notifier (r); }void blk_ram_registrar_destroy (BlockRAMRegistrar *r) { -if (r->ok) { -ram_block_notifier_remove (&r->notifier); - } +remove_ram_block_notifier (r); +blk_remove_graph_change_notifier (r->blk, graph_change_pre_detach, + graph_change_post_attach, r); }
Signature · 1 line
2.39.0