From nobody Fri Apr 4 22:50:31 2025 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=fail(p=none dis=none) header.from=smartx.com ARC-Seal: i=1; a=rsa-sha256; t=1741947411; cv=none; d=zohomail.com; s=zohoarc; b=AkEcbDil+C6ZIa7URJfVDcwB5dlLBXq6ddJB5SJlTVqeYZEph6SHTZdVVhEslmBIaxL4AG14IT9a46B4jcbz3hQaAh3+Yom8A2veztYrl91HVgnnpxznOkkyLDcdrRWMaO6Jjxjgyc0rW7nOmQieWQKqIRmZ9u977SNAdBRTpcE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741947411; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4KdS/98hGZswd1uYVRRtaOB5eJ4bk9Cy+EJDEqdJonE=; b=BtUmGH93assGGftAh5gZQXc85OVEMR+VLSykNjc4eLOvDZFwKlGrLZDQwSyr79iySrPHW5VDe+MYEkCwRewtC/3I7Pq70g/5jphaaasMsEFq/+X2ewwSiVO+5MC9B0All4olhmqnRHkjnN1SQNrT97XcHZC0xJTq7lM03nVi7JA= 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=fail 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 174194741143010.934478652488451; Fri, 14 Mar 2025 03:16:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tt25D-0005o0-5Z; Fri, 14 Mar 2025 06:16:03 -0400 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 1tt258-0005nI-Iq for qemu-devel@nongnu.org; Fri, 14 Mar 2025 06:16:00 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tt254-00032R-53 for qemu-devel@nongnu.org; Fri, 14 Mar 2025 06:15:56 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-22435603572so32695115ad.1 for ; Fri, 14 Mar 2025 03:15:53 -0700 (PDT) Received: from fedora.smartx.com ([103.85.74.92]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7371169549fsm2698898b3a.131.2025.03.14.03.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 03:15:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1741947351; x=1742552151; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4KdS/98hGZswd1uYVRRtaOB5eJ4bk9Cy+EJDEqdJonE=; b=tRrUbHHJ4MMGOWbso/irG6LNYzr410M6aMlPh2WnxcR6daQ2ACQRZ309DYiixsxow9 UlNMk3VXjZFcCYwZab+qSm3iaRzTxjsEvZwW0LWcnHjXrbholc0GqyRLYHanGPgSwie4 zzpZ0F0W2Zvyw7QYJJZ2mWS69iCjhvu9eJg8Ych2+Zyp3kcB3/qBfj821SJ+hKb6l6tB MSIkSKvJ8BsnONR0oz5iyLZL0fZqXHYkhdvL7pd3tuLpSDu7HMbeVNdp41rdww+INYrG EnUkxYm9Iy5cJQyy6XXMf6z8IvusW/f0McdrLMnwmFbA8WbBQeErkd2XkzBymFb3iq1c N8cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741947351; x=1742552151; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4KdS/98hGZswd1uYVRRtaOB5eJ4bk9Cy+EJDEqdJonE=; b=YgTqJYjpIIenA3Dmn3XEWLb2iAZSs17CNf53njqe2SYwDLNXrtaL543tiQcXWCreO/ mXt6y5ou0dSaNmJ5AEo5pIvCaHBo1rY/SWVCDksrMG1OjvA+6hQqj7HEzw7G7Uo+gMqi 1YgeLWrBnp/WoBnsyYkn2qrBXbJWm6aScWuZZP+9OdCpM8M+zY8wHfguU4tK3zO+9Pvh NoGY4o+Sso1aUkknYciyRHg96YBZeWHuuA8dv7R8CuNvAS2JfWmWC50ejpd1X0/Y+dlQ AgwiczvAcz6RksWYVVpLZ8kSph9PX1ogolsYXbwW4sSx9m8YNx9bVcBvJeKv187DR0XR SbFw== X-Gm-Message-State: AOJu0YxuAIClRBam7JNFt7HVQ9n4Rul3Akg0+yrQXop0n0VvcUy9GtVw AbTI6JVZxVfxKkC4ysX5Xm3caYiW0W2SpcNI60VMr7ufve1dl4mTZOQxl9rs0qF6jfcVTZe/tg+ m5fHr9Fzumgc= X-Gm-Gg: ASbGnct7KRXnmlr4WtbVPCFMVgNYGUYrds/v5GowEG7gYBuomMDLxDpRWMUTbQ+TnnS ORZ3Dr7AEGLKDyvVuL+/KV5Y3Buvsjq65Zmm/ucZ5J+xyOW/OaV0lqCcY8Fyxq2V2EUmqNUOKjo gLPxO+ZH+zW58UCzfUuVRHRXYeyAveGvLznqkkIswDpH7VIWmhDtJuOigLSjo9d8T7MaSqpW/0Q c37nZ/6dKjIRj5sSUvP4+RGWmODhpl6ck3DZNU/kaHT1etbr1Eky7HR8/Ix5yZ4oKBwWKmZDgpS iKKFo1Ap2aIHCoF7DgM9EGXK8WaPgq59zUf32wqk4bjBae+yQQTw X-Google-Smtp-Source: AGHT+IGmepXfcZ4nBrBOjmHvRGh4wMau5kDJjK76XFCNPlfuW73sOOjOxqX0EGcS3QnIOEar2NyQvA== X-Received: by 2002:a05:6a00:3cc3:b0:736:ab21:6f37 with SMTP id d2e1a72fcca58-7372223f0bcmr2534560b3a.0.1741947351362; Fri, 14 Mar 2025 03:15:51 -0700 (PDT) From: Haoqian He To: qemu-devel@nongnu.org Cc: fengli@smartx.com, yuhua@smartx.com, "Michael S. Tsirkin" , Stefano Garzarella , Raphael Norwitz , Kevin Wolf , Hanna Reitz , Stefan Hajnoczi , Paolo Bonzini , Fam Zheng , Alex Williamson , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , Richard Henderson , qemu-block@nongnu.org (open list:Block layer core) Subject: [PATCH v2 1/3] virtio: add VM state change cb with return value Date: Fri, 14 Mar 2025 06:15:32 -0400 Message-ID: <20250314101535.1059308-2-haoqian.he@smartx.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250314101535.1059308-1-haoqian.he@smartx.com> References: <20250309090708.3928953-1-haoqian.he@smartx.com> <20250314101535.1059308-1-haoqian.he@smartx.com> 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=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=haoqian.he@smartx.com; helo=mail-pl1-x62b.google.com X-Spam_score_int: 14 X-Spam_score: 1.4 X-Spam_bar: + X-Spam_report: (1.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=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: 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 @smartx-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1741947413057019000 Content-Type: text/plain; charset="utf-8" This patch contains two changes: 1. Add VM state change cb type VMChangeStateHandlerExt which has return value for virtio devices VMChangeStateEntry. When VM state changes, virtio device will call the _Ext version. 2. Add return value for vm_state_notify(). Signed-off-by: Haoqian He --- hw/block/virtio-blk.c | 2 +- hw/core/vm-change-state-handler.c | 14 ++++++++------ hw/scsi/scsi-bus.c | 2 +- hw/vfio/migration.c | 2 +- hw/virtio/virtio.c | 5 +++-- include/system/runstate.h | 11 ++++++++--- system/cpus.c | 4 ++-- system/runstate.c | 25 ++++++++++++++++++++----- 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/hw/block/virtio-blk.c b/hw/block/virtio-blk.c index 5135b4d8f1..4a48a16790 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -1928,7 +1928,7 @@ static void virtio_blk_device_realize(DeviceState *de= v, Error **errp) * called after ->start_ioeventfd() has already set blk's AioContext. */ s->change =3D - qdev_add_vm_change_state_handler(dev, virtio_blk_dma_restart_cb, s= ); + qdev_add_vm_change_state_handler(dev, virtio_blk_dma_restart_cb, N= ULL, s); =20 blk_ram_registrar_init(&s->blk_ram_registrar, s->blk); blk_set_dev_ops(s->blk, &virtio_block_ops, s); diff --git a/hw/core/vm-change-state-handler.c b/hw/core/vm-change-state-ha= ndler.c index 7064995578..d5045b17c1 100644 --- a/hw/core/vm-change-state-handler.c +++ b/hw/core/vm-change-state-handler.c @@ -40,6 +40,7 @@ static int qdev_get_dev_tree_depth(DeviceState *dev) * qdev_add_vm_change_state_handler: * @dev: the device that owns this handler * @cb: the callback function to be invoked + * @cb_ext: the callback function with return value to be invoked * @opaque: user data passed to the callback function * * This function works like qemu_add_vm_change_state_handler() except call= backs @@ -54,21 +55,22 @@ static int qdev_get_dev_tree_depth(DeviceState *dev) */ VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, VMChangeStateHandler = *cb, + VMChangeStateHandlerE= xt *cb_ext, void *opaque) { - return qdev_add_vm_change_state_handler_full(dev, cb, NULL, opaque); + return qdev_add_vm_change_state_handler_full(dev, cb, NULL, cb_ext, op= aque); } =20 /* * Exactly like qdev_add_vm_change_state_handler() but passes a prepare_cb - * argument too. + * and the cb_ext arguments too. */ VMChangeStateEntry *qdev_add_vm_change_state_handler_full( - DeviceState *dev, VMChangeStateHandler *cb, - VMChangeStateHandler *prepare_cb, void *opaque) + DeviceState *dev, VMChangeStateHandler *cb, VMChangeStateHandler *prep= are_cb, + VMChangeStateHandlerExt *cb_ext, void *opaque) { int depth =3D qdev_get_dev_tree_depth(dev); =20 - return qemu_add_vm_change_state_handler_prio_full(cb, prepare_cb, opaq= ue, - depth); + return qemu_add_vm_change_state_handler_prio_full(cb, prepare_cb, cb_e= xt, + opaque, depth); } diff --git a/hw/scsi/scsi-bus.c b/hw/scsi/scsi-bus.c index 7d4546800f..ec098f5f0a 100644 --- a/hw/scsi/scsi-bus.c +++ b/hw/scsi/scsi-bus.c @@ -356,7 +356,7 @@ static void scsi_qdev_realize(DeviceState *qdev, Error = **errp) return; } dev->vmsentry =3D qdev_add_vm_change_state_handler(DEVICE(dev), - scsi_dma_restart_cb, dev); + scsi_dma_restart_cb, NULL, dev); } =20 static void scsi_qdev_unrealize(DeviceState *qdev) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index 416643ddd6..f531db83ea 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -1015,7 +1015,7 @@ static int vfio_migration_init(VFIODevice *vbasedev) vfio_vmstate_change_prepare : NULL; migration->vm_state =3D qdev_add_vm_change_state_handler_full( - vbasedev->dev, vfio_vmstate_change, prepare_cb, vbasedev); + vbasedev->dev, vfio_vmstate_change, prepare_cb, NULL, vbasedev); migration_add_notifier(&migration->migration_state, vfio_migration_state_notifier); =20 diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 85110bce37..5e8d4cab53 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3419,7 +3419,7 @@ void virtio_cleanup(VirtIODevice *vdev) qemu_del_vm_change_state_handler(vdev->vmstate); } =20 -static void virtio_vmstate_change(void *opaque, bool running, RunState sta= te) +static int virtio_vmstate_change(void *opaque, bool running, RunState stat= e) { VirtIODevice *vdev =3D opaque; BusState *qbus =3D qdev_get_parent_bus(DEVICE(vdev)); @@ -3438,6 +3438,7 @@ static void virtio_vmstate_change(void *opaque, bool = running, RunState state) if (!backend_run) { virtio_set_status(vdev, vdev->status); } + return 0; } =20 void virtio_instance_init_common(Object *proxy_obj, void *data, @@ -3489,7 +3490,7 @@ void virtio_init(VirtIODevice *vdev, uint16_t device_= id, size_t config_size) vdev->config =3D NULL; } vdev->vmstate =3D qdev_add_vm_change_state_handler(DEVICE(vdev), - virtio_vmstate_change, vdev); + NULL, virtio_vmstate_change, vdev); vdev->device_endian =3D virtio_default_endian(); vdev->use_guest_notifier_mask =3D true; } diff --git a/include/system/runstate.h b/include/system/runstate.h index bffc3719d4..af33ea92b6 100644 --- a/include/system/runstate.h +++ b/include/system/runstate.h @@ -12,6 +12,7 @@ bool runstate_needs_reset(void); void runstate_replay_enable(void); =20 typedef void VMChangeStateHandler(void *opaque, bool running, RunState sta= te); +typedef int VMChangeStateHandlerExt(void *opaque, bool running, RunState s= tate); =20 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler = *cb, void *opaque); @@ -20,21 +21,25 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler_pr= io( VMChangeStateEntry * qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, VMChangeStateHandler *prepare_c= b, + VMChangeStateHandlerExt *cb_ext, void *opaque, int priority); VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, VMChangeStateHandler = *cb, + VMChangeStateHandlerE= xt *cb_ext, void *opaque); VMChangeStateEntry *qdev_add_vm_change_state_handler_full( - DeviceState *dev, VMChangeStateHandler *cb, - VMChangeStateHandler *prepare_cb, void *opaque); + DeviceState *dev, VMChangeStateHandler *cb, VMChangeStateHandler *prep= are_cb, + VMChangeStateHandlerExt *cb_ext, void *opaque); void qemu_del_vm_change_state_handler(VMChangeStateEntry *e); /** * vm_state_notify: Notify the state of the VM * * @running: whether the VM is running or not. * @state: the #RunState of the VM. + * + * Return the result of the callback which has return value. */ -void vm_state_notify(bool running, RunState state); +int vm_state_notify(bool running, RunState state); =20 static inline bool shutdown_caused_by_guest(ShutdownCause cause) { diff --git a/system/cpus.c b/system/cpus.c index 2cc5f887ab..6e1cf5720f 100644 --- a/system/cpus.c +++ b/system/cpus.c @@ -299,14 +299,14 @@ static int do_vm_stop(RunState state, bool send_stop) if (oldstate =3D=3D RUN_STATE_RUNNING) { pause_all_vcpus(); } - vm_state_notify(0, state); + ret =3D vm_state_notify(0, state); if (send_stop) { qapi_event_send_stop(); } } =20 bdrv_drain_all(); - ret =3D bdrv_flush_all(); + ret |=3D bdrv_flush_all(); trace_vm_stop_flush_all(ret); =20 return ret; diff --git a/system/runstate.c b/system/runstate.c index 272801d307..2219cec409 100644 --- a/system/runstate.c +++ b/system/runstate.c @@ -297,6 +297,7 @@ void qemu_system_vmstop_request(RunState state) struct VMChangeStateEntry { VMChangeStateHandler *cb; VMChangeStateHandler *prepare_cb; + VMChangeStateHandlerExt *cb_ext; void *opaque; QTAILQ_ENTRY(VMChangeStateEntry) entries; int priority; @@ -320,14 +321,15 @@ static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_st= ate_head =3D VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( VMChangeStateHandler *cb, void *opaque, int priority) { - return qemu_add_vm_change_state_handler_prio_full(cb, NULL, opaque, - priority); + return qemu_add_vm_change_state_handler_prio_full(cb, NULL, NULL, + opaque, priority); } =20 /** * 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_ext: 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 @@ -344,6 +346,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler_pr= io( VMChangeStateEntry * qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, VMChangeStateHandler *prepare_c= b, + VMChangeStateHandlerExt *cb_ext, void *opaque, int priority) { VMChangeStateEntry *e; @@ -352,6 +355,7 @@ qemu_add_vm_change_state_handler_prio_full(VMChangeStat= eHandler *cb, e =3D g_malloc0(sizeof(*e)); e->cb =3D cb; e->prepare_cb =3D prepare_cb; + e->cb_ext =3D cb_ext; e->opaque =3D opaque; e->priority =3D priority; =20 @@ -379,9 +383,10 @@ void qemu_del_vm_change_state_handler(VMChangeStateEnt= ry *e) g_free(e); } =20 -void vm_state_notify(bool running, RunState state) +int vm_state_notify(bool running, RunState state) { VMChangeStateEntry *e, *next; + int ret =3D 0; =20 trace_vm_state_notify(running, state, RunState_str(state)); =20 @@ -393,7 +398,12 @@ void vm_state_notify(bool running, RunState state) } =20 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) { - e->cb(e->opaque, running, state); + if (e->cb) { + e->cb(e->opaque, running, state); + } else if (e->cb_ext) { + // no need to process the result when starting VM + e->cb_ext(e->opaque, running, state); + } } } else { QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, nex= t) { @@ -403,9 +413,14 @@ void vm_state_notify(bool running, RunState state) } =20 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, nex= t) { - e->cb(e->opaque, running, state); + if (e->cb) { + e->cb(e->opaque, running, state); + } else if (e->cb_ext) { + ret |=3D e->cb_ext(e->opaque, running, state); + } } } + return ret; } =20 static ShutdownCause reset_requested; --=20 2.48.1