From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783167569735.5073256539805; Fri, 23 Oct 2026 12:19:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlN-00032a-Tn; Fri, 23 Oct 2026 15:17:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKky-0002wy-Td for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:16:54 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKkw-0001zu-55 for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:16:48 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKkk-00000007BEQ-3sMo; Fri, 23 Oct 2026 21:16:34 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 01/13] runstate: Rename VM change state handlers "priority" to "depth" Date: Fri, 23 Oct 2026 21:16:12 +0200 Message-ID: <1eac2289128bda1df0e8fde970c11c7231b1d052.1792758435.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783167917158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" This frees the term "priority" for implementing an explicit multi-level priority system of these handlers instead of the current two-level implicit prepare_cb() vs cb() system. Signed-off-by: Maciej S. Szmigiero --- include/system/runstate.h | 12 ++++++------ system/runstate.c | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/system/runstate.h b/include/system/runstate.h index 929379adae41..cceb88d2e3b0 100644 --- a/include/system/runstate.h +++ b/include/system/runstate.h @@ -30,8 +30,8 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMCh= angeStateHandler *cb, * qemu_add_vm_change_state_handler_prio: * @cb: the callback to invoke * @opaque: user data passed to the callback - * @priority: low priorities execute first when the vm runs and the revers= e is - * true when the vm stops + * @depth: low depths execute first when the vm runs and the reverse is tr= ue + * when the vm stops * * Register a callback function that is invoked when the vm starts or stops * running. @@ -39,7 +39,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMCh= angeStateHandler *cb, * Returns: an entry to be freed using qemu_del_vm_change_state_handler() */ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( - VMChangeStateHandler *cb, void *opaque, int priority); + VMChangeStateHandler *cb, void *opaque, int depth); VMChangeStateEntry * /** * qemu_add_vm_change_state_handler_prio_full: @@ -47,8 +47,8 @@ VMChangeStateEntry * * @prepare_cb: a callback to invoke before the main callback * @cb_ret: the main callback to invoke with return value * @opaque: user data passed to the callbacks - * @priority: low priorities execute first when the vm runs and the revers= e is - * true when the vm stops + * @depth: low depths execute first when the vm runs and the reverse is tr= ue + * when the vm stops * * Register a main callback function and an optional prepare callback func= tion * that are invoked when the vm starts or stops running. The main callback= and @@ -62,7 +62,7 @@ VMChangeStateEntry * qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, VMChangeStateHandler *prepare_c= b, VMChangeStateHandlerWithRet *cb= _ret, - void *opaque, int priority); + void *opaque, int depth); VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, VMChangeStateHandler = *cb, VMChangeStateHandlerW= ithRet *cb_ret, diff --git a/system/runstate.c b/system/runstate.c index 56bfc5b07da9..60eba9a63e93 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -304,24 +304,24 @@ struct VMChangeStateEntry { VMChangeStateHandlerWithRet *cb_ret; void *opaque; QTAILQ_ENTRY(VMChangeStateEntry) entries; - int priority; + int depth; }; =20 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =3D QTAILQ_HEAD_INITIALIZER(vm_change_state_head); =20 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( - VMChangeStateHandler *cb, void *opaque, int priority) + VMChangeStateHandler *cb, void *opaque, int depth) { return qemu_add_vm_change_state_handler_prio_full(cb, NULL, NULL, - opaque, priority); + opaque, depth); } =20 VMChangeStateEntry * qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, VMChangeStateHandler *prepare_c= b, VMChangeStateHandlerWithRet *cb= _ret, - void *opaque, int priority) + void *opaque, int depth) { VMChangeStateEntry *e; VMChangeStateEntry *other; @@ -331,11 +331,11 @@ qemu_add_vm_change_state_handler_prio_full(VMChangeSt= ateHandler *cb, e->prepare_cb =3D prepare_cb; e->cb_ret =3D cb_ret; e->opaque =3D opaque; - e->priority =3D priority; + e->depth =3D depth; =20 - /* Keep list sorted in ascending priority order */ + /* Keep list sorted in ascending depth order */ QTAILQ_FOREACH(other, &vm_change_state_head, entries) { - if (priority < other->priority) { + if (depth < other->depth) { QTAILQ_INSERT_BEFORE(other, e, entries); return e; } From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783168722407.6708736867175; Fri, 23 Oct 2026 12:19:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlI-00030A-9V; Fri, 23 Oct 2026 15:17:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKkx-0002wW-0D for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:16:50 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKku-00020H-3u for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:16:46 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKkq-00000007BEb-0BRN; Fri, 23 Oct 2026 21:16:40 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 02/13] runstate: Introduce VM change state handlers explicit priority levels Date: Fri, 23 Oct 2026 21:16:13 +0200 Message-ID: X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783170033158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" Replace the current two-level implicit priority system of prepare_cb() and cb() with explicit multi-level priority system. The old prepare_cb() maps to VM_CHANGE_STATE_PRI_HIGH in this new system, while the old cb() maps to VM_CHANGE_STATE_PRI_NORMAL. Signed-off-by: Maciej S. Szmigiero --- hw/core/vm-change-state-handler.c | 16 +++-- hw/vfio/migration.c | 14 ++-- hw/vfio/vfio-migration-internal.h | 1 + include/system/runstate.h | 38 ++++++----- system/runstate.c | 106 ++++++++++++++++-------------- 5 files changed, 98 insertions(+), 77 deletions(-) diff --git a/hw/core/vm-change-state-handler.c b/hw/core/vm-change-state-ha= ndler.c index 2c111350298d..3800f148af68 100644 --- a/hw/core/vm-change-state-handler.c +++ b/hw/core/vm-change-state-handler.c @@ -61,20 +61,22 @@ VMChangeStateEntry *qdev_add_vm_change_state_handler(De= viceState *dev, void *opaque) { assert(!cb || !cb_ret); - return qdev_add_vm_change_state_handler_full(dev, cb, NULL, cb_ret, op= aque); + return qdev_add_vm_change_state_handler_full( + dev, cb, cb_ret, opaque, VM_CHANGE_STATE_PRI_NORMAL); } =20 /* - * Exactly like qdev_add_vm_change_state_handler() but passes a prepare_cb - * and the cb_ret arguments too. + * Exactly like qdev_add_vm_change_state_handler() but allows selecting the + * notification priority. */ VMChangeStateEntry *qdev_add_vm_change_state_handler_full( - DeviceState *dev, VMChangeStateHandler *cb, VMChangeStateHandler *prep= are_cb, - VMChangeStateHandlerWithRet *cb_ret, void *opaque) + DeviceState *dev, VMChangeStateHandler *cb, + VMChangeStateHandlerWithRet *cb_ret, void *opaque, + VMChangeStateHandlerPri priority) { int depth =3D qdev_get_dev_tree_depth(dev); =20 assert(!cb || !cb_ret); - return qemu_add_vm_change_state_handler_prio_full(cb, prepare_cb, cb_r= et, - opaque, depth); + return qemu_add_vm_change_state_handler_prio_full(cb, cb_ret, opaque, + priority, depth); } diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 73c49d8c24f2..76ede0efbf02 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -1149,7 +1149,6 @@ static bool vfio_migration_init(VFIODevice *vbasedev,= Error **errp) g_autofree char *path =3D NULL, *oid =3D NULL; uint64_t mig_flags =3D 0; bool precopy_info_v2_used =3D false; - VMChangeStateHandler *prepare_cb; =20 if (!vbasedev->ops->vfio_get_object) { error_setg(errp, "no vfio_get_object handler"); @@ -1210,11 +1209,13 @@ static bool vfio_migration_init(VFIODevice *vbasede= v, Error **errp) register_savevm_live(id, VMSTATE_INSTANCE_ID_ANY, 1, &savevm_vfio_hand= lers, vbasedev); =20 - prepare_cb =3D migration->mig_flags & VFIO_MIGRATION_P2P ? - vfio_vmstate_change_prepare : - NULL; - migration->vm_state =3D qdev_add_vm_change_state_handler_full( - vbasedev->dev, vfio_vmstate_change, prepare_cb, NULL, vbasedev); + if (migration->mig_flags & VFIO_MIGRATION_P2P) { + migration->vm_state_prepare =3D qdev_add_vm_change_state_handler_f= ull( + vbasedev->dev, vfio_vmstate_change_prepare, NULL, vbasedev, + VM_CHANGE_STATE_PRI_HIGH); + } + migration->vm_state =3D qdev_add_vm_change_state_handler( + vbasedev->dev, vfio_vmstate_change, NULL, vbasedev); migration_add_notifier(&migration->migration_state, vfio_migration_state_notifier); =20 @@ -1289,6 +1290,7 @@ static void vfio_migration_deinit(VFIODevice *vbasede= v) VFIOMigration *migration =3D vbasedev->migration; =20 migration_remove_notifier(&migration->migration_state); + qemu_del_vm_change_state_handler(migration->vm_state_prepare); qemu_del_vm_change_state_handler(migration->vm_state); unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev); vfio_migration_free(vbasedev); diff --git a/hw/vfio/vfio-migration-internal.h b/hw/vfio/vfio-migration-int= ernal.h index a1c58b112685..b3c2f86cae78 100644 --- a/hw/vfio/vfio-migration-internal.h +++ b/hw/vfio/vfio-migration-internal.h @@ -38,6 +38,7 @@ typedef struct VFIOMultifd VFIOMultifd; =20 typedef struct VFIOMigration { struct VFIODevice *vbasedev; + VMChangeStateEntry *vm_state_prepare; VMChangeStateEntry *vm_state; NotifierWithReturn migration_state; uint32_t device_state; diff --git a/include/system/runstate.h b/include/system/runstate.h index cceb88d2e3b0..e28261ec4a20 100644 --- a/include/system/runstate.h +++ b/include/system/runstate.h @@ -14,6 +14,12 @@ void runstate_replay_enable(void); typedef void VMChangeStateHandler(void *opaque, bool running, RunState sta= te); typedef int VMChangeStateHandlerWithRet(void *opaque, bool running, RunSta= te state); =20 +typedef enum VMChangeStateHandlerPri { + VM_CHANGE_STATE_PRI_HIGH, + VM_CHANGE_STATE_PRI_NORMAL, + VM_CHANGE_STATE_PRI_MAX, +} VMChangeStateHandlerPri; + /** * qemu_add_vm_change_state_handler: * @cb: the callback to invoke @@ -30,8 +36,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMCh= angeStateHandler *cb, * qemu_add_vm_change_state_handler_prio: * @cb: the callback to invoke * @opaque: user data passed to the callback - * @depth: low depths execute first when the vm runs and the reverse is tr= ue - * when the vm stops + * @priority: priority at which to invoke the callback * * Register a callback function that is invoked when the vm starts or stops * running. @@ -39,37 +44,38 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VM= ChangeStateHandler *cb, * Returns: an entry to be freed using qemu_del_vm_change_state_handler() */ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( - VMChangeStateHandler *cb, void *opaque, int depth); + VMChangeStateHandler *cb, void *opaque, + VMChangeStateHandlerPri priority); VMChangeStateEntry * /** * qemu_add_vm_change_state_handler_prio_full: - * @cb: the main callback to invoke - * @prepare_cb: a callback to invoke before the main callback - * @cb_ret: the main callback to invoke with return value + * @cb: the callback to invoke + * @cb_ret: the callback to invoke with return value * @opaque: user data passed to the callbacks + * @priority: priority at which to invoke the callback * @depth: low depths execute first when the vm runs and the reverse is tr= ue * when the vm stops * - * Register a main callback function and an optional prepare callback func= tion - * that are invoked when the vm starts or stops running. The main callback= and - * the prepare callback are called in two separate phases: First all prepa= re - * callbacks are called and only then all main callbacks are called. As its - * name suggests, the prepare callback can be used to do some preparatory = work - * before invoking the main callback. + * Register a callback function that is invoked when the vm starts or stops + * running. All higher priority notifications are completed before calling= any + * lower priority ones. In addition to that, depth ordering is maintained = within + * each priority level. * * Returns: an entry to be freed using qemu_del_vm_change_state_handler() */ qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, - VMChangeStateHandler *prepare_c= b, VMChangeStateHandlerWithRet *cb= _ret, - void *opaque, int depth); + void *opaque, + VMChangeStateHandlerPri priorit= y, + int depth); VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, VMChangeStateHandler = *cb, VMChangeStateHandlerW= ithRet *cb_ret, void *opaque); VMChangeStateEntry *qdev_add_vm_change_state_handler_full( - DeviceState *dev, VMChangeStateHandler *cb, VMChangeStateHandler *prep= are_cb, - VMChangeStateHandlerWithRet *cb_ret, void *opaque); + DeviceState *dev, VMChangeStateHandler *cb, + VMChangeStateHandlerWithRet *cb_ret, void *opaque, + VMChangeStateHandlerPri priority); void qemu_del_vm_change_state_handler(VMChangeStateEntry *e); /** * vm_state_notify: Notify the state of the VM diff --git a/system/runstate.c b/system/runstate.c index 60eba9a63e93..5f16f09507ed 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -300,55 +300,71 @@ void qemu_system_vmstop_request(RunState state) } struct VMChangeStateEntry { VMChangeStateHandler *cb; - VMChangeStateHandler *prepare_cb; VMChangeStateHandlerWithRet *cb_ret; void *opaque; QTAILQ_ENTRY(VMChangeStateEntry) entries; + VMChangeStateHandlerPri priority; int depth; }; =20 -static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head =3D - QTAILQ_HEAD_INITIALIZER(vm_change_state_head); +typedef QTAILQ_HEAD(VMChangeStateHead, VMChangeStateEntry) VMChangeStateHe= ad; + +static VMChangeStateHead vm_change_state_head[VM_CHANGE_STATE_PRI_MAX] =3D= { + [VM_CHANGE_STATE_PRI_HIGH] =3D + QTAILQ_HEAD_INITIALIZER( + vm_change_state_head[VM_CHANGE_STATE_PRI_HIGH]), + [VM_CHANGE_STATE_PRI_NORMAL] =3D + QTAILQ_HEAD_INITIALIZER( + vm_change_state_head[VM_CHANGE_STATE_PRI_NORMAL]), +}; =20 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( - VMChangeStateHandler *cb, void *opaque, int depth) + VMChangeStateHandler *cb, void *opaque, + VMChangeStateHandlerPri priority) { - return qemu_add_vm_change_state_handler_prio_full(cb, NULL, NULL, - opaque, depth); + return qemu_add_vm_change_state_handler_prio_full( + cb, NULL, opaque, priority, 0); } =20 VMChangeStateEntry * qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, - VMChangeStateHandler *prepare_c= b, VMChangeStateHandlerWithRet *cb= _ret, - void *opaque, int depth) + void *opaque, + VMChangeStateHandlerPri priorit= y, + int depth) { VMChangeStateEntry *e; VMChangeStateEntry *other; + VMChangeStateHead *head; + + assert(priority >=3D 0 && priority < VM_CHANGE_STATE_PRI_MAX); + assert(!cb || !cb_ret); =20 e =3D g_malloc0(sizeof(*e)); e->cb =3D cb; - e->prepare_cb =3D prepare_cb; e->cb_ret =3D cb_ret; e->opaque =3D opaque; + e->priority =3D priority; e->depth =3D depth; =20 + head =3D &vm_change_state_head[priority]; /* Keep list sorted in ascending depth order */ - QTAILQ_FOREACH(other, &vm_change_state_head, entries) { + QTAILQ_FOREACH(other, head, entries) { if (depth < other->depth) { QTAILQ_INSERT_BEFORE(other, e, entries); return e; } } =20 - QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries); + QTAILQ_INSERT_TAIL(head, e, entries); return e; } =20 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler = *cb, void *opaque) { - return qemu_add_vm_change_state_handler_prio(cb, opaque, 0); + return qemu_add_vm_change_state_handler_prio( + cb, opaque, VM_CHANGE_STATE_PRI_NORMAL); } =20 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) @@ -356,54 +372,48 @@ void qemu_del_vm_change_state_handler(VMChangeStateEn= try *e) if (!e) { return; } - QTAILQ_REMOVE(&vm_change_state_head, e, entries); + QTAILQ_REMOVE(&vm_change_state_head[e->priority], e, entries); g_free(e); } =20 int vm_state_notify(bool running, RunState state) { VMChangeStateEntry *e, *next; + int pri; int ret =3D 0; =20 trace_vm_state_notify(running, state, RunState_str(state)); =20 - if (running) { - QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { - if (e->prepare_cb) { - e->prepare_cb(e->opaque, running, state); - } - } + for (pri =3D VM_CHANGE_STATE_PRI_HIGH; + pri < VM_CHANGE_STATE_PRI_MAX; pri++) { + VMChangeStateHead *head =3D &vm_change_state_head[pri]; =20 - QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { - if (e->cb) { - e->cb(e->opaque, running, state); - } else if (e->cb_ret) { - /* - * Here ignore the return value of cb_ret because - * we only care about the stopping the device during - * the VM live migration to indicate whether the - * connection between qemu and backend is normal. - */ - e->cb_ret(e->opaque, running, state); + if (running) { + QTAILQ_FOREACH_SAFE(e, head, entries, next) { + if (e->cb) { + e->cb(e->opaque, running, state); + } else if (e->cb_ret) { + /* + * Here ignore the return value of cb_ret because + * we only care about stopping the device during + * the VM live migration to indicate whether the + * connection between qemu and backend is normal. + */ + e->cb_ret(e->opaque, running, state); + } } - } - } else { - QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, nex= t) { - if (e->prepare_cb) { - e->prepare_cb(e->opaque, running, state); - } - } - - QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, nex= t) { - if (e->cb) { - e->cb(e->opaque, running, state); - } else if (e->cb_ret) { - /* - * We should execute all registered callbacks even if - * one of them returns failure, otherwise, some cleanup - * work of the device will be skipped. - */ - ret |=3D e->cb_ret(e->opaque, running, state); + } else { + QTAILQ_FOREACH_REVERSE_SAFE(e, head, entries, next) { + if (e->cb) { + e->cb(e->opaque, running, state); + } else if (e->cb_ret) { + /* + * We should execute all registered callbacks even if + * one of them returns failure, otherwise, some cleanup + * work of the device will be skipped. + */ + ret |=3D e->cb_ret(e->opaque, running, state); + } } } } From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783150079785.1131379101627; Fri, 23 Oct 2026 12:19:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlN-00032T-Dq; Fri, 23 Oct 2026 15:17:13 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKl4-0002xp-3p for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:16:58 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKkz-00020w-E6 for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:16:50 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKkv-00000007BEq-0j2T; Fri, 23 Oct 2026 21:16:45 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 03/13] runstate: Add an intermediate VM change state handlers SYNC priority Date: Fri, 23 Oct 2026 21:16:14 +0200 Message-ID: <2f8d2c6eccb373a2a6d19245386889b2ebaf5cba.1792758435.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783151578158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" VFIO code needs to register a VM change state handler between NORMAL and HIGH priorities as a synchronization point/barrier between the first state transition of its devices and their second state transition. Add an intermediate SYNC priority of VM change state handlers for that. Signed-off-by: Maciej S. Szmigiero --- include/system/runstate.h | 2 +- system/runstate.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/system/runstate.h b/include/system/runstate.h index e28261ec4a20..c10d9dc8b751 100644 --- a/include/system/runstate.h +++ b/include/system/runstate.h @@ -16,6 +16,7 @@ typedef int VMChangeStateHandlerWithRet(void *opaque, boo= l running, RunState sta =20 typedef enum VMChangeStateHandlerPri { VM_CHANGE_STATE_PRI_HIGH, + VM_CHANGE_STATE_PRI_SYNC, VM_CHANGE_STATE_PRI_NORMAL, VM_CHANGE_STATE_PRI_MAX, } VMChangeStateHandlerPri; @@ -163,4 +164,3 @@ void qemu_system_guest_pvshutdown(void); bool qemu_system_dump_in_progress(void); =20 #endif - diff --git a/system/runstate.c b/system/runstate.c index 5f16f09507ed..6039bb81ecfd 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -313,6 +313,9 @@ static VMChangeStateHead vm_change_state_head[VM_CHANGE= _STATE_PRI_MAX] =3D { [VM_CHANGE_STATE_PRI_HIGH] =3D QTAILQ_HEAD_INITIALIZER( vm_change_state_head[VM_CHANGE_STATE_PRI_HIGH]), + [VM_CHANGE_STATE_PRI_SYNC] =3D + QTAILQ_HEAD_INITIALIZER( + vm_change_state_head[VM_CHANGE_STATE_PRI_SYNC]), [VM_CHANGE_STATE_PRI_NORMAL] =3D QTAILQ_HEAD_INITIALIZER( vm_change_state_head[VM_CHANGE_STATE_PRI_NORMAL]), From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783107726228.18189204466364; Fri, 23 Oct 2026 12:18:27 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlK-00030q-8A; Fri, 23 Oct 2026 15:17:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKl5-0002yK-Vm for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:01 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKl4-00021U-0A for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:16:55 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKl0-00000007BF1-1CmC; Fri, 23 Oct 2026 21:16:50 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 04/13] hw/vfio: Introduce VFIOPCIDeviceClass Date: Fri, 23 Oct 2026 21:16:15 +0200 Message-ID: X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783111142158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" It will soon be needed to store a pointer to data shared between all VFIOPCIDevice instances. Signed-off-by: Maciej S. Szmigiero --- hw/vfio/pci.c | 1 + hw/vfio/pci.h | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 428ab2f06983..31699422f238 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -3853,6 +3853,7 @@ static const TypeInfo vfio_pci_device_info =3D { .name =3D TYPE_VFIO_PCI_DEVICE, .parent =3D TYPE_PCI_DEVICE, .instance_size =3D sizeof(VFIOPCIDevice), + .class_size =3D sizeof(VFIOPCIDeviceClass), .abstract =3D true, .class_init =3D vfio_pci_device_class_init, .interfaces =3D (const InterfaceInfo[]) { diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index c9ab9498708e..70c46add8fe4 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -120,7 +120,11 @@ typedef struct VFIOMSIXInfo { MemoryRegion *pba_region; } VFIOMSIXInfo; =20 -OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI_DEVICE) +OBJECT_DECLARE_TYPE(VFIOPCIDevice, VFIOPCIDeviceClass, VFIO_PCI_DEVICE) + +struct VFIOPCIDeviceClass { + PCIDeviceClass parent_class; +}; =20 struct VFIOPCIDevice { PCIDevice parent_obj; From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783057821193.46546884073223; Fri, 23 Oct 2026 12:17:37 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlQ-00033d-JN; Fri, 23 Oct 2026 15:17:16 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlD-0002zs-Hu for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:03 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKl9-00022U-HT for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:03 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKl5-00000007BFB-1gvj; Fri, 23 Oct 2026 21:16:55 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 05/13] vfio/migration: Parallelize device state transitions Date: Fri, 23 Oct 2026 21:16:16 +0200 Message-ID: <192e01ee9a2d89825c8455e48cdfa7ee893173cb.1792758435.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783063668158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" When multiple VFIO devices are present in a VM the fact that their state transitions on migration happen sequentially has a visible impact on migration downtime. This is because both PRE_COPY -> PRE_COPY_P2P -> STOP_COPY transitions on the source and RESUMING -> RUNNING_P2P -> RUNNING transitions on the target happen during the switchover phase. During this phase the VM is stopped so the downtime is ticking. These device state transitions are performed by VM state change handlers registered by the VFIO device migration code. Instead of performing these state transitions synchronously launch a per-device thread performing them at VM_CHANGE_STATE_PRI_HIGH. Since no VFIO device should start the second state transition until all VFIO devices have completed the first transition also register a single VM_CHANGE_STATE_PRI_SYNC handler common for all VFIO devices that serves as a synchronization point/barrier between these transitions. This synchronization point also serves to fulfill similar requirement with respect to non-VFIO devices that require particular sequencing with respect to running their VM state change handlers versus VFIO devices. In particular, this requirement applies to DMA-generating devices and interrupt controllers. These VFIO device state change threads are joined (waited for) from a per-device VM_CHANGE_STATE_PRI_NORMAL handler. This way these state transitions can happen in parallel not only with respect to other VFIO device instances but also ordinary (serialized) handlers for other devices at other qdev tree depths - as long as these handlers at other qdev tree depths would be previously ordered earlier than the synchronous VFIO device state change handler. Data used by common VM_CHANGE_STATE_PRI_SYNC handler is shared between all TYPE_VFIO_PCI_DEVICE instances, so it is kept in its VFIOPCIDeviceClass struct as new "vm_state_change_sync" field. This data is allocated on demand during initialization of the migration part of the first migratable VFIO device. Since derived classes (like TYPE_VFIO_PCI and TYPE_VFIO_PCI_NOHOTPLUG) inherit this class field as a shallow copy, one extra level of pointer indirection is necessary for this data. Enable the feature by default since it has no impact on the migration bit stream protocol - it shouldn't need disabling for anything else but debugging scenarios. Signed-off-by: Maciej S. Szmigiero --- hw/vfio/migration.c | 228 +++++++++++++++++++++++++++++- hw/vfio/pci.c | 6 + hw/vfio/pci.h | 4 + hw/vfio/trace-events | 2 + hw/vfio/vfio-migration-internal.h | 2 + include/hw/vfio/vfio-device.h | 1 + 6 files changed, 237 insertions(+), 6 deletions(-) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 76ede0efbf02..2300a6e3ec16 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -1053,6 +1053,202 @@ static void vfio_vmstate_change(void *opaque, bool = running, RunState state) mig_state_to_str(new_state)); } =20 +typedef struct VMStateChangeThreadData { + VFIODevice *vbasedev; + VFIOVMStateChangeSync *sync; + bool running; + RunState state; +} VMStateChangeThreadData; + +struct VFIOVMStateChangeSync { + VMChangeStateEntry *vm_state; + + /* Only counts devices with parallel state change ON */ + unsigned int device_count; + unsigned int thread_count; + + unsigned int p2p_done_count; + QemuEvent p2p_all_done; + QemuEvent final_transition_start; +}; + +static void vfio_vmstate_change_sync_handler(void *opaque, bool running, + RunState state) +{ + VFIOVMStateChangeSync *sync =3D opaque; + + assert(qatomic_read(&sync->thread_count) =3D=3D + qatomic_read(&sync->device_count)); + + trace_vfio_vmstate_change_sync_wait(); + qemu_event_wait(&sync->p2p_all_done); + + trace_vfio_vmstate_change_sync_done(); + qemu_event_set(&sync->final_transition_start); +} + +static VFIOVMStateChangeSync ** +vfio_vmstate_change_sync_ptr(VFIODevice *vbasedev) +{ + VFIOPCIDevice *vdev =3D vfio_pci_from_vfio_device(vbasedev); + VFIOPCIDeviceClass *vpc; + + assert(vdev); + vpc =3D VFIO_PCI_DEVICE_GET_CLASS(vdev); + assert(vpc); + assert(vpc->vm_state_change_sync); + + return vpc->vm_state_change_sync; +} + +static VFIOVMStateChangeSync * +vfio_vmstate_change_sync_ref(VFIODevice *vbasedev) +{ + VFIOVMStateChangeSync **syncp =3D vfio_vmstate_change_sync_ptr(vbasede= v); + VFIOVMStateChangeSync *sync =3D *syncp; + + if (!sync) { + sync =3D g_new0(VFIOVMStateChangeSync, 1); + qemu_event_init(&sync->p2p_all_done, false); + qemu_event_init(&sync->final_transition_start, false); + + sync->vm_state =3D qemu_add_vm_change_state_handler_prio( + vfio_vmstate_change_sync_handler, sync, + VM_CHANGE_STATE_PRI_SYNC); + + *syncp =3D sync; + } + + qatomic_fetch_inc(&sync->device_count); + + return sync; +} + +static void vfio_vmstate_change_sync_unref(VFIODevice *vbasedev) +{ + VFIOVMStateChangeSync **syncp =3D vfio_vmstate_change_sync_ptr(vbasede= v); + VFIOVMStateChangeSync *sync =3D *syncp; + unsigned int device_count; + + assert(sync); + + device_count =3D qatomic_fetch_dec(&sync->device_count); + assert(device_count > 0); + + if (device_count !=3D 1) { + return; + } + + assert(qatomic_read(&sync->thread_count) =3D=3D 0); + + qemu_del_vm_change_state_handler(sync->vm_state); + qemu_event_destroy(&sync->final_transition_start); + qemu_event_destroy(&sync->p2p_all_done); + + g_clear_pointer(syncp, g_free); +} + +static VFIOVMStateChangeSync * +vfio_vmstate_change_sync_get(VFIODevice *vbasedev) +{ + VFIOVMStateChangeSync *sync =3D *vfio_vmstate_change_sync_ptr(vbasedev= ); + + assert(sync); + + return sync; +} + +static void +vfio_vmstate_change_sync_thread_pre_start(VFIOVMStateChangeSync *sync) +{ + if (qatomic_fetch_inc(&sync->thread_count) =3D=3D 0) { + /* + * Reset the sync data at the first thread launch time since + * the whole struct survives VM start/stop sequence. + */ + qatomic_set(&sync->p2p_done_count, 0); + qemu_event_reset(&sync->p2p_all_done); + qemu_event_reset(&sync->final_transition_start); + } +} + +static void +vfio_vmstate_change_sync_p2p_done(VFIOVMStateChangeSync *sync) +{ + unsigned int done; + + done =3D qatomic_fetch_inc(&sync->p2p_done_count) + 1; + if (done =3D=3D qatomic_read(&sync->device_count)) { + qemu_event_set(&sync->p2p_all_done); + } + + qemu_event_wait(&sync->final_transition_start); +} + +static void +vfio_vmstate_change_sync_thread_finished(VFIOVMStateChangeSync *sync) +{ + unsigned int thread_count; + + thread_count =3D qatomic_fetch_dec(&sync->thread_count); + assert(thread_count > 0); +} + +static void *vfio_vmstate_change_thread(void *opaque) +{ + g_autofree VMStateChangeThreadData *data =3D opaque; + VFIOMigration *migration =3D data->vbasedev->migration; + + if (migration->mig_flags & VFIO_MIGRATION_P2P) { + vfio_vmstate_change_prepare(data->vbasedev, data->running, data->s= tate); + } + + vfio_vmstate_change_sync_p2p_done(data->sync); + + vfio_vmstate_change(data->vbasedev, data->running, data->state); + + return NULL; +} + +static void vfio_vmstate_change_thread_launch(void *opaque, bool running, + RunState state) +{ + VFIODevice *vbasedev =3D opaque; + VFIOMigration *migration =3D vbasedev->migration; + VMStateChangeThreadData *data =3D g_new(VMStateChangeThreadData, 1); + VFIOVMStateChangeSync *sync =3D vfio_vmstate_change_sync_get(vbasedev); + + data->vbasedev =3D vbasedev; + data->sync =3D sync; + data->running =3D running; + data->state =3D state; + + assert(!migration->vm_state_thread_running); + migration->vm_state_thread_running =3D true; + + vfio_vmstate_change_sync_thread_pre_start(sync); + + qemu_thread_create(&migration->vm_state_thread, + "vfio_vmstate_change", + vfio_vmstate_change_thread, data, + QEMU_THREAD_JOINABLE); +} + +static void vfio_vmstate_change_thread_join(void *opaque, bool running, + RunState state) +{ + VFIODevice *vbasedev =3D opaque; + VFIOMigration *migration =3D vbasedev->migration; + VFIOVMStateChangeSync *sync =3D vfio_vmstate_change_sync_get(vbasedev); + + assert(migration->vm_state_thread_running); + + qemu_thread_join(&migration->vm_state_thread); + migration->vm_state_thread_running =3D false; + + vfio_vmstate_change_sync_thread_finished(sync); +} + static int vfio_migration_state_notifier(NotifierWithReturn *notifier, MigrationEvent *e, Error **errp) { @@ -1069,6 +1265,8 @@ static int vfio_migration_state_notifier(NotifierWith= Return *notifier, * MigrationNotifyFunc may not return an error code and an Error * object for MIG_EVENT_FAILED. Hence, report the error * locally and ignore the errp argument. + * This state change is not parallelized as it is not expected to = be + * performance critical. */ ret =3D vfio_migration_set_state_or_reset(vbasedev, VFIO_DEVICE_STATE_RUNNING, @@ -1209,13 +1407,25 @@ static bool vfio_migration_init(VFIODevice *vbasede= v, Error **errp) register_savevm_live(id, VMSTATE_INSTANCE_ID_ANY, 1, &savevm_vfio_hand= lers, vbasedev); =20 - if (migration->mig_flags & VFIO_MIGRATION_P2P) { - migration->vm_state_prepare =3D qdev_add_vm_change_state_handler_f= ull( - vbasedev->dev, vfio_vmstate_change_prepare, NULL, vbasedev, - VM_CHANGE_STATE_PRI_HIGH); + if (vbasedev->migration_parallel_states) { + vfio_vmstate_change_sync_ref(vbasedev); + migration->vm_state_prepare =3D + qdev_add_vm_change_state_handler_full( + vbasedev->dev, vfio_vmstate_change_thread_launch, NULL, + vbasedev, VM_CHANGE_STATE_PRI_HIGH); + migration->vm_state =3D qdev_add_vm_change_state_handler( + vbasedev->dev, vfio_vmstate_change_thread_join, NULL, vbasedev= ); + } else { + if (migration->mig_flags & VFIO_MIGRATION_P2P) { + migration->vm_state_prepare =3D + qdev_add_vm_change_state_handler_full( + vbasedev->dev, vfio_vmstate_change_prepare, NULL, + vbasedev, VM_CHANGE_STATE_PRI_HIGH); + } + migration->vm_state =3D qdev_add_vm_change_state_handler( + vbasedev->dev, vfio_vmstate_change, NULL, vbasedev); } - migration->vm_state =3D qdev_add_vm_change_state_handler( - vbasedev->dev, vfio_vmstate_change, NULL, vbasedev); + migration_add_notifier(&migration->migration_state, vfio_migration_state_notifier); =20 @@ -1292,6 +1502,12 @@ static void vfio_migration_deinit(VFIODevice *vbased= ev) migration_remove_notifier(&migration->migration_state); qemu_del_vm_change_state_handler(migration->vm_state_prepare); qemu_del_vm_change_state_handler(migration->vm_state); + + if (vbasedev->migration_parallel_states) { + assert(!migration->vm_state_thread_running); + vfio_vmstate_change_sync_unref(vbasedev); + } + unregister_savevm(VMSTATE_IF(vbasedev->dev), "vfio", vbasedev); vfio_migration_free(vbasedev); vfio_unblock_multiple_devices_migration(); diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index 31699422f238..e9794a5964aa 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -3839,9 +3839,13 @@ static void vfio_pci_init(Object *obj) =20 static void vfio_pci_device_class_init(ObjectClass *klass, const void *dat= a) { + VFIOPCIDeviceClass *vpc =3D VFIO_PCI_DEVICE_CLASS(klass); DeviceClass *dc =3D DEVICE_CLASS(klass); PCIDeviceClass *pdc =3D PCI_DEVICE_CLASS(klass); =20 + /* Derived classes inherit this field as a copy */ + vpc->vm_state_change_sync =3D g_new0(VFIOVMStateChangeSync *, 1); + dc->desc =3D "VFIO PCI base device"; set_bit(DEVICE_CATEGORY_MISC, dc->categories); pdc->exit =3D vfio_exitfn; @@ -3902,6 +3906,8 @@ static const Property vfio_pci_properties[] =3D { ON_OFF_AUTO_AUTO), DEFINE_PROP_SIZE("x-migration-max-queued-buffers-size", VFIOPCIDevice, vbasedev.migration_max_queued_buffers_size, UINT64_MA= X), + DEFINE_PROP_BOOL("x-migration-parallel-states", VFIOPCIDevice, + vbasedev.migration_parallel_states, true), DEFINE_PROP_BOOL("migration-events", VFIOPCIDevice, vbasedev.migration_events, false), DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false), diff --git a/hw/vfio/pci.h b/hw/vfio/pci.h index 70c46add8fe4..598472050f59 100644 --- a/hw/vfio/pci.h +++ b/hw/vfio/pci.h @@ -120,10 +120,14 @@ typedef struct VFIOMSIXInfo { MemoryRegion *pba_region; } VFIOMSIXInfo; =20 +typedef struct VFIOVMStateChangeSync VFIOVMStateChangeSync; + OBJECT_DECLARE_TYPE(VFIOPCIDevice, VFIOPCIDeviceClass, VFIO_PCI_DEVICE) =20 struct VFIOPCIDeviceClass { PCIDeviceClass parent_class; + + VFIOVMStateChangeSync **vm_state_change_sync; }; =20 struct VFIOPCIDevice { diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index bfdaf229e42b..6225d7661c67 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -183,6 +183,8 @@ vfio_state_pending(const char *name, uint64_t stopcopy_= size, uint64_t precopy_in vfio_send_init_data_flag(const char *name) " (%s)" vfio_vmstate_change(const char *name, int running, const char *reason, con= st char *dev_state) " (%s) running %d reason %s device state %s" vfio_vmstate_change_prepare(const char *name, int running, const char *rea= son, const char *dev_state) " (%s) running %d reason %s device state %s" +vfio_vmstate_change_sync_wait(void) "" +vfio_vmstate_change_sync_done(void) "" =20 #iommufd.c =20 diff --git a/hw/vfio/vfio-migration-internal.h b/hw/vfio/vfio-migration-int= ernal.h index b3c2f86cae78..5be321527a42 100644 --- a/hw/vfio/vfio-migration-internal.h +++ b/hw/vfio/vfio-migration-internal.h @@ -40,6 +40,8 @@ typedef struct VFIOMigration { struct VFIODevice *vbasedev; VMChangeStateEntry *vm_state_prepare; VMChangeStateEntry *vm_state; + QemuThread vm_state_thread; + bool vm_state_thread_running; NotifierWithReturn migration_state; uint32_t device_state; int data_fd; diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 8472420d3fac..5a96aab20700 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -69,6 +69,7 @@ typedef struct VFIODevice { OnOffAuto migration_multifd_transfer; OnOffAuto migration_load_config_after_iter; uint64_t migration_max_queued_buffers_size; + bool migration_parallel_states; bool migration_events; bool use_region_fds; VFIODeviceOps *ops; From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179278308528031.905240408456734; Fri, 23 Oct 2026 12:18:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlS-00034e-AQ; Fri, 23 Oct 2026 15:17:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlG-00030R-6A for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:08 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlE-00023e-PW for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:05 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKlA-00000007BFM-2CUU; Fri, 23 Oct 2026 21:17:00 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , Nicholas Piggin , Harsh Prateek Bora , Amit Machhiwal , Glenn Miles , Gautam Menghani , qemu-devel@nongnu.org Subject: [PATCH v2 06/13] spapr/xive: Split VM state change handler into start and stop parts Date: Fri, 23 Oct 2026 21:16:17 +0200 Message-ID: <8945320039882c06fa33682db6fc0b097e11d7e5.1792758436.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783088613158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" This will allow moving the VM start part of this handler into VM_CHANGE_STATE_PRI_HIGH. Signed-off-by: Maciej S. Szmigiero --- hw/intc/spapr_xive_kvm.c | 74 ++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/hw/intc/spapr_xive_kvm.c b/hw/intc/spapr_xive_kvm.c index 26d30b41c15c..e1a4f0b640cb 100644 --- a/hw/intc/spapr_xive_kvm.c +++ b/hw/intc/spapr_xive_kvm.c @@ -498,39 +498,51 @@ static int kvmppc_xive_get_queues(SpaprXive *xive, Er= ror **errp) * runs again. If an interrupt was queued while the VM was stopped, * simply generate a trigger. */ -static void kvmppc_xive_change_state_handler(void *opaque, bool running, - RunState state) +static void kvmppc_xive_change_state_start(void *opaque, bool running, + RunState state) +{ + SpaprXive *xive =3D opaque; + XiveSource *xsrc =3D &xive->source; + int i; + + if (!running) { + return; + } + + /* + * Restore the sources to their initial state. This is called when + * the VM resumes after a stop or a migration. + */ + for (i =3D 0; i < xsrc->nr_irqs; i++) { + uint8_t pq; + uint8_t old_pq; + + if (!xive_eas_is_valid(&xive->eat[i])) { + continue; + } + + pq =3D xive_source_esb_get(xsrc, i); + old_pq =3D xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_00 + (pq << 8)); + + /* + * An interrupt was queued while the VM was stopped, + * generate a trigger. + */ + if (pq =3D=3D XIVE_ESB_RESET && old_pq =3D=3D XIVE_ESB_QUEUED) { + kvmppc_xive_esb_trigger(xsrc, i); + } + } +} + +static void kvmppc_xive_change_state_stop(void *opaque, bool running, + RunState state) { SpaprXive *xive =3D opaque; XiveSource *xsrc =3D &xive->source; Error *local_err =3D NULL; int i; =20 - /* - * Restore the sources to their initial state. This is called when - * the VM resumes after a stop or a migration. - */ if (running) { - for (i =3D 0; i < xsrc->nr_irqs; i++) { - uint8_t pq; - uint8_t old_pq; - - if (!xive_eas_is_valid(&xive->eat[i])) { - continue; - } - - pq =3D xive_source_esb_get(xsrc, i); - old_pq =3D xive_esb_read(xsrc, i, XIVE_ESB_SET_PQ_00 + (pq << = 8)); - - /* - * An interrupt was queued while the VM was stopped, - * generate a trigger. - */ - if (pq =3D=3D XIVE_ESB_RESET && old_pq =3D=3D XIVE_ESB_QUEUED)= { - kvmppc_xive_esb_trigger(xsrc, i); - } - } - return; } =20 @@ -573,6 +585,16 @@ static void kvmppc_xive_change_state_handler(void *opa= que, bool running, } } =20 +static void kvmppc_xive_change_state_handler(void *opaque, bool running, + RunState state) +{ + if (running) { + kvmppc_xive_change_state_start(opaque, running, state); + } else { + kvmppc_xive_change_state_stop(opaque, running, state); + } +} + void kvmppc_xive_synchronize_state(SpaprXive *xive, Error **errp) { assert(xive->fd !=3D -1); From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783150645711.3768447808175; Fri, 23 Oct 2026 12:19:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlV-00036E-TQ; Fri, 23 Oct 2026 15:17:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlP-00033N-3S; Fri, 23 Oct 2026 15:17:15 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlM-00025P-LR; Fri, 23 Oct 2026 15:17:14 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKlF-00000007BFY-2heh; Fri, 23 Oct 2026 21:17:05 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , Peter Maydell , Glenn Miles , Gautam Menghani , Nicholas Piggin , Harsh Prateek Bora , Amit Machhiwal , qemu-arm@nongnu.org, qemu-ppc@nongnu.org, qemu-devel@nongnu.org Subject: [PATCH v2 07/13] runstate: Start/stop interrupt controllers before/after ordinary devices Date: Fri, 23 Oct 2026 21:16:18 +0200 Message-ID: X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783151627158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" The VFIO spec allows VFIO devices to generate interrupts already after their first state transition at VM start (done at VM_CHANGE_STATE_PRI_HIGH) until the second (last) state transition at VM stop (done at VM_CHANGE_STATE_PRI_NORMAL). In principle, the same may also apply to other/emulated devices. That means in order to avoid potentially lost interrupts any interrupt controllers with VM state change handlers need to be started before VFIO devices at VM_CHANGE_STATE_PRI_HIGH on VM start and stopped only after VFIO devices at VM_CHANGE_STATE_PRI_NORMAL. To implement that ordering and to make sure that it is not lost in a future code refactor, introduce an explicit VMChangeStateHandlerDepth - much like migration has explicit MigrationPriority values. Since normal qdev tree depths are non-negative these earlier-on-start / later-on-stop depths need to be increasingly negative. Preserve the old ITS vs GIC ordering. This happens to also match the MigrationPriority of these controllers. In terms of implementation, the kvm-tpr-opt VAPIC VM state change handler is already NOP on VM stop, while ITS and GIC handlers are NOPs on VM start. So for these devices the change amounts to trivial adjustment of their handlers priorities and/or depths. For the remaining sPAPR XIVE interrupt controller make use of the start/stop split of its VM state change handler introduced by the previous commit. Signed-off-by: Maciej S. Szmigiero --- hw/i386/vapic.c | 5 +++-- hw/intc/arm_gicv3_its_kvm.c | 4 +++- hw/intc/arm_gicv3_kvm.c | 4 +++- hw/intc/spapr_xive_kvm.c | 26 ++++++++++---------------- include/hw/ppc/spapr_xive.h | 3 ++- include/system/runstate.h | 11 +++++++++++ system/runstate.c | 2 +- 7 files changed, 33 insertions(+), 22 deletions(-) diff --git a/hw/i386/vapic.c b/hw/i386/vapic.c index 5c3911cf9c83..3d8f7a64b9cb 100644 --- a/hw/i386/vapic.c +++ b/hw/i386/vapic.c @@ -822,8 +822,9 @@ static int vapic_post_load(void *opaque, int version_id) } =20 if (!s->vmsentry) { - s->vmsentry =3D - qemu_add_vm_change_state_handler(vapic_vm_state_change, s); + s->vmsentry =3D qemu_add_vm_change_state_handler_prio_full( + vapic_vm_state_change, NULL, s, VM_CHANGE_STATE_PRI_HIGH, + VM_CHANGE_STATE_DEPTH_APIC); } return 0; } diff --git a/hw/intc/arm_gicv3_its_kvm.c b/hw/intc/arm_gicv3_its_kvm.c index a8d6d4fb540a..ea3681b8d62f 100644 --- a/hw/intc/arm_gicv3_its_kvm.c +++ b/hw/intc/arm_gicv3_its_kvm.c @@ -119,7 +119,9 @@ static void kvm_arm_its_realize(DeviceState *dev, Error= **errp) return; } } else { - qemu_add_vm_change_state_handler(vm_change_state_handler, s); + qemu_add_vm_change_state_handler_prio_full( + vm_change_state_handler, NULL, s, VM_CHANGE_STATE_PRI_NORMAL, + VM_CHANGE_STATE_DEPTH_GICV3_ITS); } =20 kvm_msi_use_devid =3D true; diff --git a/hw/intc/arm_gicv3_kvm.c b/hw/intc/arm_gicv3_kvm.c index fddeefa26f30..fd4a4527c46f 100644 --- a/hw/intc/arm_gicv3_kvm.c +++ b/hw/intc/arm_gicv3_kvm.c @@ -925,7 +925,9 @@ static void kvm_arm_gicv3_realize(DeviceState *dev, Err= or **errp) } if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL, KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES)) { - qemu_add_vm_change_state_handler(vm_change_state_handler, s); + qemu_add_vm_change_state_handler_prio_full( + vm_change_state_handler, NULL, s, VM_CHANGE_STATE_PRI_NORMAL, + VM_CHANGE_STATE_DEPTH_GICV3); migration_add_notifier_mode(&s->cpr_notifier, kvm_arm_gicv3_notifier, MIG_MODE_CPR_TRANSFER); diff --git a/hw/intc/spapr_xive_kvm.c b/hw/intc/spapr_xive_kvm.c index e1a4f0b640cb..483f9ffcb607 100644 --- a/hw/intc/spapr_xive_kvm.c +++ b/hw/intc/spapr_xive_kvm.c @@ -585,16 +585,6 @@ static void kvmppc_xive_change_state_stop(void *opaque= , bool running, } } =20 -static void kvmppc_xive_change_state_handler(void *opaque, bool running, - RunState state) -{ - if (running) { - kvmppc_xive_change_state_start(opaque, running, state); - } else { - kvmppc_xive_change_state_stop(opaque, running, state); - } -} - void kvmppc_xive_synchronize_state(SpaprXive *xive, Error **errp) { assert(xive->fd !=3D -1); @@ -813,8 +803,12 @@ int kvmppc_xive_connect(SpaprInterruptController *intc= , uint32_t nr_servers, memory_region_add_subregion_overlap(&xive->tm_mmio, 0, &xive->tm_mmio_kvm, 1); =20 - xive->change =3D qemu_add_vm_change_state_handler( - kvmppc_xive_change_state_handler, xive); + xive->change_start =3D qemu_add_vm_change_state_handler_prio_full( + kvmppc_xive_change_state_start, NULL, xive, + VM_CHANGE_STATE_PRI_HIGH, VM_CHANGE_STATE_DEPTH_XIVE); + xive->change_stop =3D qemu_add_vm_change_state_handler_prio_full( + kvmppc_xive_change_state_stop, NULL, xive, + VM_CHANGE_STATE_PRI_NORMAL, VM_CHANGE_STATE_DEPTH_XIVE); =20 /* Connect the presenters to the initial VCPUs of the machine */ CPU_FOREACH(cs) { @@ -884,8 +878,8 @@ void kvmppc_xive_disconnect(SpaprInterruptController *i= ntc) kvm_cpu_disable_all(); =20 /* VM Change state handler is not needed anymore */ - if (xive->change) { - qemu_del_vm_change_state_handler(xive->change); - xive->change =3D NULL; - } + qemu_del_vm_change_state_handler(xive->change_start); + xive->change_start =3D NULL; + qemu_del_vm_change_state_handler(xive->change_stop); + xive->change_stop =3D NULL; } diff --git a/include/hw/ppc/spapr_xive.h b/include/hw/ppc/spapr_xive.h index b282960ad90d..4fe9af8c6c7a 100644 --- a/include/hw/ppc/spapr_xive.h +++ b/include/hw/ppc/spapr_xive.h @@ -48,7 +48,8 @@ typedef struct SpaprXive { int fd; void *tm_mmap; MemoryRegion tm_mmio_kvm; - VMChangeStateEntry *change; + VMChangeStateEntry *change_start; + VMChangeStateEntry *change_stop; =20 uint8_t hv_prio; } SpaprXive; diff --git a/include/system/runstate.h b/include/system/runstate.h index c10d9dc8b751..523d267ea2fc 100644 --- a/include/system/runstate.h +++ b/include/system/runstate.h @@ -21,6 +21,17 @@ typedef enum VMChangeStateHandlerPri { VM_CHANGE_STATE_PRI_MAX, } VMChangeStateHandlerPri; =20 +typedef enum VMChangeStateHandlerDepth { + VM_CHANGE_STATE_DEPTH_GICV3 =3D -3, + VM_CHANGE_STATE_DEPTH_GICV3_ITS =3D -2, + + VM_CHANGE_STATE_DEPTH_APIC =3D -1, + VM_CHANGE_STATE_DEPTH_XIVE =3D -1, + + /* qdev tree depths start here */ + VM_CHANGE_STATE_DEPTH_DEFAULT =3D 0, +} VMChangeStateHandlerDepth; + /** * qemu_add_vm_change_state_handler: * @cb: the callback to invoke diff --git a/system/runstate.c b/system/runstate.c index 6039bb81ecfd..45469f997fdf 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -326,7 +326,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler_pr= io( VMChangeStateHandlerPri priority) { return qemu_add_vm_change_state_handler_prio_full( - cb, NULL, opaque, priority, 0); + cb, NULL, opaque, priority, VM_CHANGE_STATE_DEPTH_DEFAULT); } =20 VMChangeStateEntry * From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783108903537.2301019660467; Fri, 23 Oct 2026 12:18:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlS-00034h-TC; Fri, 23 Oct 2026 15:17:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlP-00033i-OC for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:15 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlO-00025Y-6H for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:15 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKlK-00000007BFj-3Doy; Fri, 23 Oct 2026 21:17:10 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 08/13] audio: Split VM state change handler into start and stop parts Date: Fri, 23 Oct 2026 21:16:19 +0200 Message-ID: X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783111066158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" This will allow moving the VM stop part of this handler into VM_CHANGE_STATE_PRI_HIGH. Signed-off-by: Maciej S. Szmigiero --- audio/audio-mixeng-be.c | 42 +++++++++++++++++++++++++++++++++-------- audio/audio_int.h | 3 ++- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/audio/audio-mixeng-be.c b/audio/audio-mixeng-be.c index f7d73d85608c..070c03c553e3 100644 --- a/audio/audio-mixeng-be.c +++ b/audio/audio-mixeng-be.c @@ -1381,10 +1381,8 @@ static bool audio_mixeng_backend_realize(AudioBacken= d *abe, return true; } =20 -static void audio_vm_change_state_handler (void *opaque, bool running, - RunState state) +static void audio_vm_change_state(AudioMixengBackend *s, bool running) { - AudioMixengBackend *s =3D opaque; AudioMixengBackendClass *k =3D AUDIO_MIXENG_BACKEND_GET_CLASS(s); HWVoiceOut *hwo =3D NULL; HWVoiceIn *hwi =3D NULL; @@ -1403,6 +1401,26 @@ static void audio_vm_change_state_handler (void *opa= que, bool running, audio_reset_timer (s); } =20 +static void audio_vm_change_state_start(void *opaque, bool running, + RunState state) +{ + if (!running) { + return; + } + + audio_vm_change_state(opaque, true); +} + +static void audio_vm_change_state_stop(void *opaque, bool running, + RunState state) +{ + if (running) { + return; + } + + audio_vm_change_state(opaque, false); +} + static const VMStateDescription vmstate_audio; =20 static const char *audio_mixeng_backend_get_id(AudioBackend *be) @@ -1459,8 +1477,12 @@ static void audio_mixeng_backend_init(Object *obj) s->ts =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s); s->run_timer =3D g_timer_new(); =20 - s->vmse =3D qemu_add_vm_change_state_handler(audio_vm_change_state_han= dler, s); - assert(s->vmse !=3D NULL); + s->vmse_start =3D qemu_add_vm_change_state_handler( + audio_vm_change_state_start, s); + assert(s->vmse_start !=3D NULL); + s->vmse_stop =3D qemu_add_vm_change_state_handler( + audio_vm_change_state_stop, s); + assert(s->vmse_stop !=3D NULL); =20 vmstate_register_any(NULL, &vmstate_audio, s); } @@ -1511,9 +1533,13 @@ static void audio_mixeng_backend_finalize(Object *ob= j) =20 g_clear_pointer(&s->run_timer, g_timer_destroy); =20 - if (s->vmse) { - qemu_del_vm_change_state_handler(s->vmse); - s->vmse =3D NULL; + if (s->vmse_start) { + qemu_del_vm_change_state_handler(s->vmse_start); + s->vmse_start =3D NULL; + } + if (s->vmse_stop) { + qemu_del_vm_change_state_handler(s->vmse_stop); + s->vmse_stop =3D NULL; } =20 vmstate_unregister(NULL, &vmstate_audio, s); diff --git a/audio/audio_int.h b/audio/audio_int.h index 290cf373b49f..5bf6b8133e4a 100644 --- a/audio/audio_int.h +++ b/audio/audio_int.h @@ -229,7 +229,8 @@ struct AudioMixengBackend { =20 bool timer_running; uint64_t timer_last; - VMChangeStateEntry *vmse; + VMChangeStateEntry *vmse_start; + VMChangeStateEntry *vmse_stop; }; =20 extern const struct mixeng_volume nominal_volume; From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783188888408.66386846414116; Fri, 23 Oct 2026 12:19:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlW-00036K-32; Fri, 23 Oct 2026 15:17:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlU-00035z-Vt for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:21 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlT-00025s-Ca for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:20 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKlP-00000007BFx-3iaA; Fri, 23 Oct 2026 21:17:15 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 09/13] audio: Make sure to stop audio before VFIO gets non-DMA-capable Date: Fri, 23 Oct 2026 21:16:20 +0200 Message-ID: X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783190303158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" Although a VM state change normally happens under BQL some state change handlers may internally temporarily drop BQL. On the migration source, the VM state change happens in the migration thread so this potentially allows main loop thread to acquire BQL and dispatch audio backend handlers. In turn, these audio backend handlers may call audio_run() which then may call into emulated sound card to do DMA. Since VFIO devices start their transition into a state where they no longer can accept incoming DMA at VM_CHANGE_STATE_PRI_SYNC if audio is stopped at VM_CHANGE_STATE_PRI_NORMAL in an unlikely but possible case that the aforementioned sound card DMA targets a VFIO device BAR it may reach a VFIO device that has already switched into a non-DMA-capable state. Fix this by making sure that at VM stop audio is stopped at HIGH priority level, so before VFIO devices start reaching states not capable of accepting incoming DMA. Signed-off-by: Maciej S. Szmigiero --- audio/audio-mixeng-be.c | 4 ++-- tests/audio/audio-stubs.c | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/audio/audio-mixeng-be.c b/audio/audio-mixeng-be.c index 070c03c553e3..338e7fea560c 100644 --- a/audio/audio-mixeng-be.c +++ b/audio/audio-mixeng-be.c @@ -1480,8 +1480,8 @@ static void audio_mixeng_backend_init(Object *obj) s->vmse_start =3D qemu_add_vm_change_state_handler( audio_vm_change_state_start, s); assert(s->vmse_start !=3D NULL); - s->vmse_stop =3D qemu_add_vm_change_state_handler( - audio_vm_change_state_stop, s); + s->vmse_stop =3D qemu_add_vm_change_state_handler_prio( + audio_vm_change_state_stop, s, VM_CHANGE_STATE_PRI_HIGH); assert(s->vmse_stop !=3D NULL); =20 vmstate_register_any(NULL, &vmstate_audio, s); diff --git a/tests/audio/audio-stubs.c b/tests/audio/audio-stubs.c index 2c19dfa7d6bb..2cc1027c205d 100644 --- a/tests/audio/audio-stubs.c +++ b/tests/audio/audio-stubs.c @@ -59,6 +59,13 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler(VMC= hangeStateHandler *cb, return (VMChangeStateEntry *)&dummy_vmse; } =20 +VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( + VMChangeStateHandler *cb, void *opaque, + VMChangeStateHandlerPri priority) +{ + return qemu_add_vm_change_state_handler(cb, opaque); +} + void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) { } From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783144526575.232397992783; Fri, 23 Oct 2026 12:19:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlh-00037v-KB; Fri, 23 Oct 2026 15:17:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKla-00037E-25 for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:26 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlY-000267-5R for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:25 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKlV-00000007BGA-00ii; Fri, 23 Oct 2026 21:17:21 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 10/13] net: Split VM state change handler into start and stop parts Date: Fri, 23 Oct 2026 21:16:21 +0200 Message-ID: <6cd059494816e5553620b3bee99b6a422b47d098.1792758436.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783145681158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" This will allow moving the VM stop part of this handler into VM_CHANGE_STATE_PRI_HIGH. Signed-off-by: Maciej S. Szmigiero --- net/net.c | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/net/net.c b/net/net.c index d7fa637ce51a..9fc62e19b36e 100644 --- a/net/net.c +++ b/net/net.c @@ -64,7 +64,8 @@ # define CONFIG_NET_BRIDGE #endif =20 -static VMChangeStateEntry *net_change_state_entry; +static VMChangeStateEntry *net_change_state_start_entry; +static VMChangeStateEntry *net_change_state_stop_entry; NetClientStateList net_clients; =20 typedef struct NetdevQueueEntry { @@ -1729,27 +1730,43 @@ void qmp_set_link(const char *name, bool up, Error = **errp) net_client_set_link(ncs, queues, up); } =20 -static void net_vm_change_state_handler(void *opaque, bool running, - RunState state) +static void net_vm_change_state_start(void *opaque, bool running, + RunState state) { NetClientState *nc; NetClientState *tmp; =20 + if (!running) { + return; + } + QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { - if (running) { - /* Flush queued packets and wake up backends. */ - if (nc->peer && qemu_can_send_packet(nc)) { - qemu_flush_queued_packets(nc->peer); - } - } else { - /* Complete all queued packets, to guarantee we don't modify - * state later when VM is not running. - */ - qemu_flush_or_purge_queued_packets(nc, true); + /* Flush queued packets and wake up backends. */ + if (nc->peer && qemu_can_send_packet(nc)) { + qemu_flush_queued_packets(nc->peer); } } } =20 +static void net_vm_change_state_stop(void *opaque, bool running, + RunState state) +{ + NetClientState *nc; + NetClientState *tmp; + + if (running) { + return; + } + + QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) { + /* + * Complete all queued packets, to guarantee we don't modify + * state later when VM is not running. + */ + qemu_flush_or_purge_queued_packets(nc, true); + } +} + void net_cleanup(void) { NetClientState *nc, **p =3D &QTAILQ_FIRST(&net_clients); @@ -1799,7 +1816,8 @@ void net_cleanup(void) } } =20 - qemu_del_vm_change_state_handler(net_change_state_entry); + qemu_del_vm_change_state_handler(net_change_state_start_entry); + qemu_del_vm_change_state_handler(net_change_state_stop_entry); } =20 void net_check_clients(void) @@ -1957,8 +1975,10 @@ static void netdev_init_modern(void) =20 void net_init_clients(void) { - net_change_state_entry =3D - qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL= ); + net_change_state_start_entry =3D + qemu_add_vm_change_state_handler(net_vm_change_state_start, NULL); + net_change_state_stop_entry =3D + qemu_add_vm_change_state_handler(net_vm_change_state_stop, NULL); =20 QTAILQ_INIT(&net_clients); From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783180344534.576395075633; Fri, 23 Oct 2026 12:19:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKlm-000390-VH; Fri, 23 Oct 2026 15:17:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlf-00037z-IA for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:31 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKld-00026S-Sr for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:31 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKla-00000007BGK-0X1S; Fri, 23 Oct 2026 21:17:26 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 11/13] net: Make sure to stop net before VFIO gets non-DMA-capable Date: Fri, 23 Oct 2026 21:16:22 +0200 Message-ID: <76d7499b02395dfa6d50575b9858b9470f526a43.1792758436.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783182142158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" Although a VM state change normally happens under BQL some state change handlers may internally temporarily drop BQL. On the migration source, the VM state change happens in the migration thread so this potentially allows main loop thread to acquire BQL and dispatch network backend handlers, like (at least) TAP. In turn, these network backend handlers may call into emulated NIC TX path to do DMA. Since VFIO devices start their transition into a state where they no longer can accept incoming DMA at VM_CHANGE_STATE_PRI_SYNC if net is stopped at VM_CHANGE_STATE_PRI_NORMAL in an unlikely but possible case that the aforementioned NIC DMA targets a VFIO device BAR it may reach a VFIO device that has already switched into a non-DMA-capable state. Fix this by making sure that at VM stop network is stopped at HIGH priority level, so before VFIO devices start reaching states not capable of accepting incoming DMA. Signed-off-by: Maciej S. Szmigiero --- net/net.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net/net.c b/net/net.c index 9fc62e19b36e..4f498e60790a 100644 --- a/net/net.c +++ b/net/net.c @@ -1977,8 +1977,8 @@ void net_init_clients(void) { net_change_state_start_entry =3D qemu_add_vm_change_state_handler(net_vm_change_state_start, NULL); - net_change_state_stop_entry =3D - qemu_add_vm_change_state_handler(net_vm_change_state_stop, NULL); + net_change_state_stop_entry =3D qemu_add_vm_change_state_handler_prio( + net_vm_change_state_stop, NULL, VM_CHANGE_STATE_PRI_HIGH); =20 QTAILQ_INIT(&net_clients); From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792783123516812.0562884898485; Fri, 23 Oct 2026 12:18:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKmQ-0003dJ-Gz; Fri, 23 Oct 2026 15:18:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKll-0003Aj-DC for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:38 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKli-00026g-Jz for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:36 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKlf-00000007BGU-116M; Fri, 23 Oct 2026 21:17:31 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 12/13] virtio: Split VM state change handler into start and stop parts Date: Fri, 23 Oct 2026 21:16:23 +0200 Message-ID: <0fac083d9ef687849bddde691aebc2cafe182d79.1792758436.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783125049158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" This will allow moving the VM stop part of this handler into VM_CHANGE_STATE_PRI_HIGH. Signed-off-by: Maciej S. Szmigiero --- hw/virtio/virtio.c | 32 +++++++++++++++++++++++++++----- include/hw/virtio/virtio.h | 3 ++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index c9f9e5b51168..9eefb2e2e670 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3733,12 +3733,12 @@ virtio_load(VirtIODevice *vdev, QEMUFile *f, int ve= rsion_id) =20 void virtio_cleanup(VirtIODevice *vdev) { - qemu_del_vm_change_state_handler(vdev->vmstate); + qemu_del_vm_change_state_handler(vdev->vmstate_start); + qemu_del_vm_change_state_handler(vdev->vmstate_stop); } =20 -static int virtio_vmstate_change(void *opaque, bool running, RunState stat= e) +static int virtio_vmstate_change(VirtIODevice *vdev, bool running) { - VirtIODevice *vdev =3D opaque; BusState *qbus =3D qdev_get_parent_bus(DEVICE(vdev)); VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); bool backend_run =3D running && virtio_device_started(vdev, vdev->stat= us); @@ -3761,6 +3761,26 @@ static int virtio_vmstate_change(void *opaque, bool = running, RunState state) return 0; } =20 +static int virtio_vmstate_change_start(void *opaque, bool running, + RunState state) +{ + if (!running) { + return 0; + } + + return virtio_vmstate_change(opaque, true); +} + +static int virtio_vmstate_change_stop(void *opaque, bool running, + RunState state) +{ + if (running) { + return 0; + } + + return virtio_vmstate_change(opaque, false); +} + void virtio_instance_init_common(Object *proxy_obj, void *data, size_t vdev_size, const char *vdev_name) { @@ -3809,8 +3829,10 @@ void virtio_init(VirtIODevice *vdev, uint16_t device= _id, size_t config_size) } else { vdev->config =3D NULL; } - vdev->vmstate =3D qdev_add_vm_change_state_handler(DEVICE(vdev), - NULL, virtio_vmstate_change, vdev); + vdev->vmstate_start =3D qdev_add_vm_change_state_handler(DEVICE(vdev), + NULL, virtio_vmstate_change_start, vdev); + vdev->vmstate_stop =3D qdev_add_vm_change_state_handler(DEVICE(vdev), + NULL, virtio_vmstate_change_stop, vdev); vdev->device_endian =3D virtio_default_endian(); vdev->use_guest_notifier_mask =3D true; QSIMPLEQ_INIT(&vdev->shmem_list); diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 27c5fe3a6b48..ab7fdd6e0e42 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -191,7 +191,8 @@ struct VirtIODevice bool start_on_kick; /* when virtio 1.0 feature has not been negotiated= */ bool disable_legacy_check; bool vhost_started; - VMChangeStateEntry *vmstate; + VMChangeStateEntry *vmstate_start; + VMChangeStateEntry *vmstate_stop; char *bus_name; uint8_t device_endian; /** From nobody Mon Nov 23 04:34:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; 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 Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 179278321276079.29054224587821; Fri, 23 Oct 2026 12:20:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xKKmT-0003er-RE; Fri, 23 Oct 2026 15:18:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKlp-0003D3-6B for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:42 -0400 Received: from vps-ovh.mhejs.net ([145.239.82.108]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xKKln-000278-Ix for qemu-devel@nongnu.org; Fri, 23 Oct 2026 15:17:40 -0400 Received: from MUA by vps-ovh.mhejs.net with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.99.1) (envelope-from ) id 1xKKlk-00000007BGe-1VFz; Fri, 23 Oct 2026 21:17:36 +0200 From: "Maciej S. Szmigiero" To: Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= Cc: Peter Xu , Fabiano Rosas , Paolo Bonzini , Avihai Horon , qemu-devel@nongnu.org Subject: [PATCH v2 13/13] virtio: Make sure to stop virtio before VFIO gets non-DMA-capable Date: Fri, 23 Oct 2026 21:16:24 +0200 Message-ID: X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=lists1p.gnu.org; Received-SPF: pass client-ip=145.239.82.108; envelope-from=mhej@vps-ovh.mhejs.net; helo=vps-ovh.mhejs.net X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development 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-ZM-MESSAGEID: 1792783214915158500 Content-Type: text/plain; charset="utf-8" From: "Maciej S. Szmigiero" On a VM stop various virtio backends stop their requests from functions ultimately called from the generic virtio VM state change handler. For example, virtio-blk flushes all of its in-flight I/O and waits for virtio_blk_dma_restart_bh() to complete from its stop_ioeventfd method. virtio-scsi-dataplane has similar in-flight I/O flushing code in its stop_ioeventfd method. That stop_ioeventfd method is called from (for example) virtio_pci_vmstate_change(), which in turn is called from the generic virtio VM state change handler. Since VFIO devices start their transition into a state where they no longer can accept incoming DMA at VM_CHANGE_STATE_PRI_SYNC if these backends are stopped at VM_CHANGE_STATE_PRI_NORMAL in an unlikely but possible case that DMA from these virtio devices target a VFIO device BAR it may reach a VFIO device that has already switched into a non-DMA-capable state. Fix this by making sure that at VM stop virtio is stopped at HIGH priority level, so before VFIO devices start reaching states not capable of accepting incoming DMA. Signed-off-by: Maciej S. Szmigiero --- hw/virtio/virtio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 9eefb2e2e670..47c1a5e25c17 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3831,8 +3831,8 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_= id, size_t config_size) } vdev->vmstate_start =3D qdev_add_vm_change_state_handler(DEVICE(vdev), NULL, virtio_vmstate_change_start, vdev); - vdev->vmstate_stop =3D qdev_add_vm_change_state_handler(DEVICE(vdev), - NULL, virtio_vmstate_change_stop, vdev); + vdev->vmstate_stop =3D qdev_add_vm_change_state_handler_full(DEVICE(vd= ev), + NULL, virtio_vmstate_change_stop, vdev, VM_CHANGE_STATE_PRI_HI= GH); vdev->device_endian =3D virtio_default_endian(); vdev->use_guest_notifier_mask =3D true; QSIMPLEQ_INIT(&vdev->shmem_list);