From nobody Thu May 16 09:17:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1672870595; cv=none; d=zohomail.com; s=zohoarc; b=Y4z/1ADzTYC/I/DpLH9mpHnH0BfbOB0DS2jNF0gKFLdjzhvemL5iKj1nNSbao9x6+lCqx/azjRlKUXyhMtbxbmKsnKc0NyJgl8Xfy9xoH4fmmPRbQpUGUzN0Ew2SpO8v9i189bizEGpAJ5W4xzqSsTfVeYtqaz2WXJwu3SeMPfk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672870595; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cIZIt4G1FMzjSRThlfCgoXrC+ua/8mdsvSyxDF/ESn0=; b=GF69q6wsPfsY/uQ6+2blRu1oN68jITv25w089AMgYTEy/t4TATQLg5GHgP4XrymC86KgHfFBmRLHpIbZ4PsFidtlwB7+ej+8u0vJvCJeuwHWFp0bbLiFrQiV6JtKa77loxxo9cuyaBiCSjlfsovP5x7ApzWpRKqIfBeKfsJeG2c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1672870595749648.3987553906715; Wed, 4 Jan 2023 14:16:35 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDBuF-0001h2-Qb; Wed, 04 Jan 2023 17:06:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDBuD-0001bm-W0 for qemu-devel@nongnu.org; Wed, 04 Jan 2023 17:06:42 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDBuC-0006Cj-4V for qemu-devel@nongnu.org; Wed, 04 Jan 2023 17:06:41 -0500 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-323-5Il1ltHzMVWPIeRAB2S3Lg-1; Wed, 04 Jan 2023 17:06:35 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6ABEE2999B2E; Wed, 4 Jan 2023 22:06:35 +0000 (UTC) Received: from localhost (unknown [10.39.192.21]) by smtp.corp.redhat.com (Postfix) with ESMTP id D60152026D4B; Wed, 4 Jan 2023 22:06:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672869999; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cIZIt4G1FMzjSRThlfCgoXrC+ua/8mdsvSyxDF/ESn0=; b=ahYZ0DXWhsIt1G0RNZvB1GMqtQ0VAmhP0iV8V/g6yIr8McPB/7bbOH9MZg6edDnU/3SBpJ S4MYHLSpGCcTJeov92WCzJTOeNBDsJwu15Vi4y71JWcaeoVebqEpjUzXQ8ZuJItr0Ptc+2 uoJWP5iYkYiYbFOkpCJ8pY2WL0b6RD4= X-MC-Unique: 5Il1ltHzMVWPIeRAB2S3Lg-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , Stefan Hajnoczi Subject: [PATCH 1/3] block: add BdrvChildClass->pre_detach() and ->post_attach() Date: Wed, 4 Jan 2023 17:06:29 -0500 Message-Id: <20230104220631.110321-2-stefanha@redhat.com> In-Reply-To: <20230104220631.110321-1-stefanha@redhat.com> References: <20230104220631.110321-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1672870598077100003 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Stefan Hajnoczi --- include/block/block_int-common.h | 8 ++++++++ block.c | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/block/block_int-common.h b/include/block/block_int-com= mon.h index c34c525fa6..6cefde5d23 100644 --- a/include/block/block_int-common.h +++ b/include/block/block_int-common.h @@ -899,6 +899,14 @@ struct BdrvChildClass { void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child); void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child); =20 + /* + * 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 th= at it diff --git a/block.c b/block.c index 9c2ac757e4..e472a4b105 100644 --- a/block.c +++ b/block.c @@ -1402,6 +1402,30 @@ static void bdrv_inherited_options(BdrvChildRole rol= e, bool parent_is_format, *child_flags =3D flags; } =20 +static void bdrv_child_cb_pre_detach(BdrvChild *child, BdrvChild *to_detac= h) +{ + BlockDriverState *parent_bs =3D 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 *attache= d) +{ + BlockDriverState *parent_bs =3D 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 =3D child->opaque; @@ -1488,6 +1512,8 @@ const BdrvChildClass child_of_bds =3D { .drained_end =3D bdrv_child_cb_drained_end, .attach =3D bdrv_child_cb_attach, .detach =3D bdrv_child_cb_detach, + .pre_detach =3D bdrv_child_cb_pre_detach, + .post_attach =3D bdrv_child_cb_post_attach, .inactivate =3D bdrv_child_cb_inactivate, .change_aio_ctx =3D bdrv_child_cb_change_aio_ctx, .update_filename =3D bdrv_child_cb_update_filename, @@ -2873,6 +2899,10 @@ static void bdrv_replace_child_noperm(BdrvChild *chi= ld, assert(bdrv_get_aio_context(old_bs) =3D=3D bdrv_get_aio_context(ne= w_bs)); } =20 + 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 *chi= ld, } bdrv_graph_wrunlock(); =20 + if (child->klass->post_attach) { + child->klass->post_attach(child, child); + } + /* * If the parent was drained through this BdrvChild previously, but ne= w_bs * is not drained, allow requests to come in only after the new node h= as --=20 2.39.0 From nobody Thu May 16 09:17:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1672870056; cv=none; d=zohomail.com; s=zohoarc; b=g1uqqGSvJvAJc+IM5AJp/gCzlJzHQ7qEvfTG5RYDR1kugKBC3o2MW4fz8Ikn4jGM4aqIJbUsqh/LOs0oqeHP/K3rExYQt0ApxAdlsavswdQ/T/rJI9u2TtflLwWHi7fR7sQ6ysq+Q5d94saeMd/s2LvDiu7fingDLTg1o4/cf1U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672870056; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ryUcQzn7G+fFA92ZvAcmDgJCaRRZJzx8a6/DZd99lrg=; b=eF+kDM3LP2XX3f5cx64oVsks90h6kxDLYTAg+PutWwhTG8vm8SoZ1fvvO2VGlMQi/UJUbxTcGbkJjz6XeIzCYTfRhqEwNqu0zdr6iAPXblqSeNQJsEboJTidxt7ZZ+q393vSZunjB93fBaC9KOalztxRYL1cn6pHjS9UcchQPS8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1672870056766681.8375980507758; Wed, 4 Jan 2023 14:07:36 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDBuY-0001io-W1; Wed, 04 Jan 2023 17:07:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDBuF-0001gv-FF for qemu-devel@nongnu.org; Wed, 04 Jan 2023 17:06:43 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDBuD-0006Hm-M0 for qemu-devel@nongnu.org; Wed, 04 Jan 2023 17:06:43 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-42-P634D3NbNZes5Amu3k-QlQ-1; Wed, 04 Jan 2023 17:06:38 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AF7CF181E3EE; Wed, 4 Jan 2023 22:06:37 +0000 (UTC) Received: from localhost (unknown [10.39.192.21]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2EC562166B31; Wed, 4 Jan 2023 22:06:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672870001; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ryUcQzn7G+fFA92ZvAcmDgJCaRRZJzx8a6/DZd99lrg=; b=bL++uNQXRfgcGtMne20+DeO2VATfwyzlPxr2lX+cVS0rz4PsBim3iTijP2BmA5qqN3aBBg /vAoPlKZvla2lDWXxnoQuRnZLhuoCXbtaRCPPzeChWuQ5QN0xLAY9sSNNObrrDBxS6sS6f E1NtQpBnjBW0xB3ehVs5P0p7fR8fZ50= X-MC-Unique: P634D3NbNZes5Amu3k-QlQ-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , Stefan Hajnoczi Subject: [PATCH 2/3] block-backend: add graph change notifier API Date: Wed, 4 Jan 2023 17:06:30 -0500 Message-Id: <20230104220631.110321-3-stefanha@redhat.com> In-Reply-To: <20230104220631.110321-1-stefanha@redhat.com> References: <20230104220631.110321-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1672870057180100001 Content-Type: text/plain; charset="utf-8" The 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. Signed-off-by: Stefan Hajnoczi --- include/sysemu/block-backend-global-state.h | 10 +++ block/block-backend.c | 79 +++++++++++++++++++++ block/trace-events | 2 + 3 files changed, 91 insertions(+) diff --git a/include/sysemu/block-backend-global-state.h b/include/sysemu/b= lock-backend-global-state.h index 6858e39cb6..736df34bf0 100644 --- a/include/sysemu/block-backend-global-state.h +++ b/include/sysemu/block-backend-global-state.h @@ -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 *, voi= d *), + 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); diff --git a/block/block-backend.c b/block/block-backend.c index ba7bf1d6bc..40d958ffad 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -42,6 +42,13 @@ typedef struct BlockBackendAioNotifier { QLIST_ENTRY(BlockBackendAioNotifier) list; } BlockBackendAioNotifier; =20 +typedef struct BlockBackendGraphChangeNotifier { + void (*pre_detach)(BlockBackend *blk, void *opaque); + void (*post_attach)(BlockBackend *blk, void *opaque); + void *opaque; + QLIST_ENTRY(BlockBackendGraphChangeNotifier) list; +} BlockBackendGraphChangeNotifier; + struct BlockBackend { char *name; int refcnt; @@ -79,6 +86,7 @@ struct BlockBackend { /* Protected by BQL */ NotifierList remove_bs_notifiers, insert_bs_notifiers; QLIST_HEAD(, BlockBackendAioNotifier) aio_notifiers; + QLIST_HEAD(, BlockBackendGraphChangeNotifier) graph_change_notifiers; =20 int quiesce_counter; CoQueue queued_requests; @@ -308,6 +316,36 @@ static void blk_root_detach(BdrvChild *child) } } =20 +static void blk_root_pre_detach(BdrvChild *child, BdrvChild *to_detach) +{ + BlockBackend *blk =3D child->opaque; + BlockBackendGraphChangeNotifier *notifier; + BlockBackendGraphChangeNotifier *next; + + trace_blk_root_pre_detach(child, blk, to_detach, to_detach->bs); + + QLIST_FOREACH_SAFE(notifier, &blk->graph_change_notifiers, list, next)= { + if (notifier->pre_detach) { + notifier->pre_detach(blk, notifier->opaque); + } + } +} + +static void blk_root_post_attach(BdrvChild *child, BdrvChild *attached) +{ + BlockBackend *blk =3D child->opaque; + BlockBackendGraphChangeNotifier *notifier; + BlockBackendGraphChangeNotifier *next; + + trace_blk_root_post_attach(child, blk, attached, attached->bs); + + QLIST_FOREACH_SAFE(notifier, &blk->graph_change_notifiers, list, next)= { + if (notifier->post_attach) { + notifier->post_attach(blk, notifier->opaque); + } + } +} + static AioContext *blk_root_get_parent_aio_context(BdrvChild *c) { BlockBackend *blk =3D c->opaque; @@ -334,6 +372,9 @@ static const BdrvChildClass child_root =3D { .attach =3D blk_root_attach, .detach =3D blk_root_detach, =20 + .pre_detach =3D blk_root_pre_detach, + .post_attach =3D blk_root_post_attach, + .change_aio_ctx =3D blk_root_change_aio_ctx, =20 .get_parent_aio_context =3D blk_root_get_parent_aio_context, @@ -372,6 +413,7 @@ BlockBackend *blk_new(AioContext *ctx, uint64_t perm, u= int64_t shared_perm) notifier_list_init(&blk->remove_bs_notifiers); notifier_list_init(&blk->insert_bs_notifiers); QLIST_INIT(&blk->aio_notifiers); + QLIST_INIT(&blk->graph_change_notifiers); =20 QTAILQ_INSERT_TAIL(&block_backends, blk, link); return blk; @@ -485,6 +527,7 @@ static void blk_delete(BlockBackend *blk) assert(QLIST_EMPTY(&blk->remove_bs_notifiers.notifiers)); assert(QLIST_EMPTY(&blk->insert_bs_notifiers.notifiers)); assert(QLIST_EMPTY(&blk->aio_notifiers)); + assert(QLIST_EMPTY(&blk->graph_change_notifiers)); QTAILQ_REMOVE(&block_backends, blk, link); drive_info_del(blk->legacy_dinfo); block_acct_cleanup(&blk->stats); @@ -2315,6 +2358,42 @@ void blk_add_insert_bs_notifier(BlockBackend *blk, N= otifier *notify) notifier_list_add(&blk->insert_bs_notifiers, notify); } =20 +void blk_add_graph_change_notifier(BlockBackend *blk, + void (*pre_detach)(BlockBackend *blk, void *), + void (*post_attach)(BlockBackend *blk, void *), + void *opaque) +{ + BlockBackendGraphChangeNotifier *notifier; + GLOBAL_STATE_CODE(); + + notifier =3D g_new(BlockBackendGraphChangeNotifier, 1); + notifier->pre_detach =3D pre_detach; + notifier->post_attach =3D post_attach; + notifier->opaque =3D opaque; + QLIST_INSERT_HEAD(&blk->graph_change_notifiers, notifier, list); +} + +void blk_remove_graph_change_notifier(BlockBackend *blk, + void (*pre_detach)(BlockBackend *blk, void *), + void (*post_attach)(BlockBackend *blk, void *), + void *opaque) +{ + BlockBackendGraphChangeNotifier *notifier; + GLOBAL_STATE_CODE(); + + QLIST_FOREACH(notifier, &blk->graph_change_notifiers, list) { + if (notifier->pre_detach =3D=3D pre_detach && + notifier->post_attach =3D=3D post_attach && + notifier->opaque =3D=3D opaque) { + QLIST_REMOVE(notifier, list); + g_free(notifier); + return; + } + } + + abort(); +} + void blk_io_plug(BlockBackend *blk) { BlockDriverState *bs =3D blk_bs(blk); diff --git a/block/trace-events b/block/trace-events index 48dbf10c66..74f05b50b9 100644 --- a/block/trace-events +++ b/block/trace-events @@ -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 fla= gs) "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_deta= ch_bs) "child %p blk %p to_detach %p to_detach_bs %p" +blk_root_post_attach(void *child, void *blk, void *attached, void *attache= d_bs) "child %p blk %p attached %p attached_bs %p" =20 # 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" --=20 2.39.0 From nobody Thu May 16 09:17:56 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1672871047; cv=none; d=zohomail.com; s=zohoarc; b=R3MAY0tn2kpNLqsDSuQhh4IYwf4ioJIN/muSNs6uaTNByBvRJz8ZCEoqnFgeRSAwYCJgmxWiDagxYLORAHjC+jDW3RWETpLINog5SzT6GZ8tgRpygA55EHrVAk6ttoF4OJKXeMUZ+lYbfZ20YQiCLGHBo6pvcmknzXDEVEre0qQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1672871047; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=iakRaPjlZHRAhoFV1mLNcawbsCOENmNXEShIiIe3GoI=; b=OJnQ+EdAmcUsqx00iuWcE5Y5TogoZDStpnE3VrMto9684/a67vR09j3FoKkDW1PcgsKa0fs8NZcijQ71vvDsbQlQN4JsWKubFa0RO0cQm1GFYOeduju6/06zgiV/X7QAiZ/qIkuohbGolGmDylaLrvpVQSX5t0XbDG/Y/zFnCr8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1672871047237125.87199283813095; Wed, 4 Jan 2023 14:24:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pDBug-0001uO-Qf; Wed, 04 Jan 2023 17:07:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDBuG-0001ht-CK for qemu-devel@nongnu.org; Wed, 04 Jan 2023 17:06:44 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pDBuE-0006IS-Ox for qemu-devel@nongnu.org; Wed, 04 Jan 2023 17:06:44 -0500 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-623-PlNzUGmTMkKPnwiocxq8Ow-1; Wed, 04 Jan 2023 17:06:40 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 21E0E885620; Wed, 4 Jan 2023 22:06:40 +0000 (UTC) Received: from localhost (unknown [10.39.192.21]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9DFD62166B30; Wed, 4 Jan 2023 22:06:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672870002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iakRaPjlZHRAhoFV1mLNcawbsCOENmNXEShIiIe3GoI=; b=KZHvSkpLanZv7HnlhZXLIiHcjW2OuyIm0cFjkm+K8V/42XElGLJJ3ciy53d18BP9xN7lOd WuX4DmYTWrNNREcjXyhmbyl1NhPXKMhV5ECzDzdwjruRL/9xs9PCLMGRRqpSzyB4RplCMv lReabBGZRItt4ajeZspfwSZjVpjBDFk= X-MC-Unique: PlNzUGmTMkKPnwiocxq8Ow-1 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, Hanna Reitz , Kevin Wolf , Stefan Hajnoczi Subject: [PATCH 3/3] block-ram-registrar: reset when the graph changes Date: Wed, 4 Jan 2023 17:06:31 -0500 Message-Id: <20230104220631.110321-4-stefanha@redhat.com> In-Reply-To: <20230104220631.110321-1-stefanha@redhat.com> References: <20230104220631.110321-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1672871048228100003 Content-Type: text/plain; charset="utf-8" The 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 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. Signed-off-by: Stefan Hajnoczi --- block/block-ram-registrar.c | 40 ++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/block/block-ram-registrar.c b/block/block-ram-registrar.c index 25dbafa789..1a111ac124 100644 --- a/block/block-ram-registrar.c +++ b/block/block-ram-registrar.c @@ -33,6 +33,35 @@ static void ram_block_removed(RAMBlockNotifier *n, void = *host, size_t size, blk_unregister_buf(r->blk, host, max_size); } =20 +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 =3D 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 =3D opaque; + + /* Unregisters all buffers from all BDSes */ + remove_ram_block_notifier(r); +} + +static void graph_change_post_attach(BlockBackend *blk, void *opaque) +{ + BlockRAMRegistrar *r =3D opaque; + + /* Re-registers all buffers with all BDSes */ + add_ram_block_notifier(r); +} + void blk_ram_registrar_init(BlockRAMRegistrar *r, BlockBackend *blk) { r->blk =3D blk; @@ -45,14 +74,15 @@ void blk_ram_registrar_init(BlockRAMRegistrar *r, Block= Backend *blk) * value that does not change across resize. */ }; - r->ok =3D true; =20 - 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); } =20 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); } --=20 2.39.0