From nobody Fri Apr 4 05:15:29 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 From nobody Fri Apr 4 05:15:29 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=1741947438; cv=none; d=zohomail.com; s=zohoarc; b=fkJIqmOoLIP1IB7Kb7miEZGG8HkqIG3HdT+7UMA3WfYsgg00kZ0C3zay+jUzRT9J60dwvOna9fIXCT8hBz7uYHp2pj2sQ2ge8fPlHCky1d5qzmlgcYs1EhfpMxZnpmqcnajfT2JYzWVcdpwncTWNh6gIuydIzptKBhJFeGdxCSg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741947438; 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=Eh6xZmod1XJzdJDqa+dorzqnDF3twPcYJbdIGQ8yqlo=; b=jhYz/n9Ct8lO8QYqM06Qnz9gkH4Db69YcdDi9a8LJg4na2vDHgcVQZiS5bxhrcXrZr84bMyXvbuoRh2pK8ZY/3lvuGUPQuJXFRNnx2nJ9g5U/ZuWgyrffF92aucwny5bBZmgbX3ZRE8X4buXco3GN11bPdnUHGgUvSV9x/8efQU= 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 17419474388211006.5684178448713; Fri, 14 Mar 2025 03:17:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tt25h-00063t-Ec; Fri, 14 Mar 2025 06:16:33 -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 1tt25E-0005oe-PB for qemu-devel@nongnu.org; Fri, 14 Mar 2025 06:16:06 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tt25A-000356-9L for qemu-devel@nongnu.org; Fri, 14 Mar 2025 06:16:03 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2239aa5da08so37674415ad.3 for ; Fri, 14 Mar 2025 03:15:59 -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.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 03:15:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1741947357; x=1742552157; 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=Eh6xZmod1XJzdJDqa+dorzqnDF3twPcYJbdIGQ8yqlo=; b=kZYZGZBzqVujGHqanFRetPGum5+sGR+1Grh5XCqARcGGYKlRkjSAs7ff+lEovnnSJ/ nWlkleYQb2W+kAG1QaZtWQLHQERfyEpZ1+6yTPgBx/nRkwxBexEKHRh8ouXaVfmGoNRy nuosdtQ9CJmCFyF9ZwQ/gFk2GSCWZ22Y3MOaMbInAshvNkVIoZxIMDm21NZpsOUHwfnO 5GZRGAVw/iIRZtmJOEnmD7IzkKlRizZjKz+yuRt5LxW1JBDbGbl05juUXOnvPMIxjzSU u3d9PJvotFBjp5e95DxFvDZoTyclfGDJAcem5eAG4SlFIgkRR1r+x60bMBoF8Rh87wRi +Exg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741947357; x=1742552157; 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=Eh6xZmod1XJzdJDqa+dorzqnDF3twPcYJbdIGQ8yqlo=; b=TWm//psX1TM1lxUrXLBZS8yFybPNKUrP1Bkf2vLFfRv/dtGLVd1xo+uOSLyrLEtXHA e3UrHInrI03K+QS+41U5d1AzSrkxuDJNiCuAIONWmXrsHwHKB/qhIDEHCFOdw0abVt74 SXuDRl8Sb14M2bjpM7MLpYCXXBVzArxHRKoYaDvnA/kN0rbpPCkzl7D4hZI4MgXT6T1N If96GhZXkpOgVcoZbV6saM2q3tzpgS0V8sDbVfSsewQhEV99lg8p3lxerVUuvNen1s9R FjFAt4VysRpPmw00lGDkMeuF4eDGMjPrvLSvTvT6WM4Qxa3HNchSt9jTMeWjgBabKUht YZPA== X-Gm-Message-State: AOJu0Yw4D0ro3gpJjPJm0QxeNdTQXu/xDzm4M/w5HoFSHbHoom/rYSYZ hd8UJjfLBR9rlKXj4iFnBScoaZ5qLKupWvuw0iwBsI+ZPcxIOrH2gAq53WtOukvDWVNnLbquXwx V4YNwIn4Xymo= X-Gm-Gg: ASbGncvpi4ymkPB+BpGOaXRAC2QxtuqC2EoJrUQwYt2gq6U7xknTFKHVVvsmxYXl8T9 XW8Rlpmhwg1bW0ZFRGo12o9HgjjEfyjAiP94DljwuyLmZIoVy0tev5ldFxOUN2mo0Rlk9jbXoA8 j4sOsb4lYctPWVncCnpNSQoxOyAwItCc29FEQCHRKt5/Z757SSpo3y4FkxOP03GUYQLAsCtBGAY YzT3mSz0liWUxWLqsU7/nzjprFinypN4lmawQXah0pOfKSscSUnGnGCpf6g1dQ86gFrAGKFHnPf DqjI0ziBWqrhyFlDFQxojRMGLJ1Xf0IP2tVCVXfbiFZQJ95wPJoM X-Google-Smtp-Source: AGHT+IELjadHM3CDZrgwr1snAkeKV/1wkKjaE7JoNZxBM7Ewnps9JWQB+8uU43QbznIX0azeh46AuA== X-Received: by 2002:a05:6a00:2e85:b0:736:7960:981f with SMTP id d2e1a72fcca58-73722354755mr3047213b3a.8.1741947357100; Fri, 14 Mar 2025 03:15:57 -0700 (PDT) From: Haoqian He To: qemu-devel@nongnu.org Cc: fengli@smartx.com, yuhua@smartx.com, Raphael Norwitz , "Michael S. Tsirkin" , Stefano Garzarella , 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 2/3] vhost: return failure if stop virtqueue failed in vhost_dev_stop Date: Fri, 14 Mar 2025 06:15:33 -0400 Message-ID: <20250314101535.1059308-3-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::62e; envelope-from=haoqian.he@smartx.com; helo=mail-pl1-x62e.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: 1741947440997019000 Content-Type: text/plain; charset="utf-8" The backend maybe crash when vhost_dev_stop and GET_VRING_BASE would fail, we can return failure to indicate the connection with the backend is broken. Signed-off-by: Haoqian He --- hw/virtio/vhost.c | 27 +++++++++++++++------------ include/hw/virtio/vhost.h | 8 +++++--- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 6aa72fd434..c82bbbe4cc 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -1368,23 +1368,23 @@ fail_alloc_desc: return r; } =20 -void vhost_virtqueue_stop(struct vhost_dev *dev, - struct VirtIODevice *vdev, - struct vhost_virtqueue *vq, - unsigned idx) +int vhost_virtqueue_stop(struct vhost_dev *dev, + struct VirtIODevice *vdev, + struct vhost_virtqueue *vq, + unsigned idx) { int vhost_vq_index =3D dev->vhost_ops->vhost_get_vq_index(dev, idx); struct vhost_vring_state state =3D { .index =3D vhost_vq_index, }; - int r; + int r =3D 0; =20 if (virtio_queue_get_desc_addr(vdev, idx) =3D=3D 0) { /* Don't stop the virtqueue which might have not been started */ - return; + return 0; } =20 - r =3D dev->vhost_ops->vhost_get_vring_base(dev, &state); + r |=3D dev->vhost_ops->vhost_get_vring_base(dev, &state); if (r < 0) { VHOST_OPS_DEBUG(r, "vhost VQ %u ring restore failed: %d", idx, r); /* Connection to the backend is broken, so let's sync internal @@ -1412,6 +1412,7 @@ void vhost_virtqueue_stop(struct vhost_dev *dev, 0, virtio_queue_get_avail_size(vdev, idx)); vhost_memory_unmap(dev, vq->desc, virtio_queue_get_desc_size(vdev, idx= ), 0, virtio_queue_get_desc_size(vdev, idx)); + return r; } =20 static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev, @@ -2136,9 +2137,10 @@ fail_features: } =20 /* Host notifiers must be enabled at this point. */ -void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vring= s) +int vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings) { int i; + int rc =3D 0; =20 /* should only be called after backend is connected */ assert(hdev->vhost_ops); @@ -2157,10 +2159,10 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIOD= evice *vdev, bool vrings) vhost_dev_set_vring_enable(hdev, false); } for (i =3D 0; i < hdev->nvqs; ++i) { - vhost_virtqueue_stop(hdev, - vdev, - hdev->vqs + i, - hdev->vq_index + i); + rc |=3D vhost_virtqueue_stop(hdev, + vdev, + hdev->vqs + i, + hdev->vq_index + i); } if (hdev->vhost_ops->vhost_reset_status) { hdev->vhost_ops->vhost_reset_status(hdev); @@ -2177,6 +2179,7 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODev= ice *vdev, bool vrings) hdev->started =3D false; vdev->vhost_started =3D false; hdev->vdev =3D NULL; + return rc; } =20 int vhost_net_set_backend(struct vhost_dev *hdev, diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index a9469d50bc..fd96ec9c39 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -232,8 +232,10 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevi= ce *vdev, bool vrings); * Stop the vhost device. After the device is stopped the notifiers * can be disabled (@vhost_dev_disable_notifiers) and the device can * be torn down (@vhost_dev_cleanup). + * + * Return: 0 on success, !=3D 0 on error when stopping dev. */ -void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vring= s); +int vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings= ); =20 /** * DOC: vhost device configuration handling @@ -333,8 +335,8 @@ int vhost_device_iotlb_miss(struct vhost_dev *dev, uint= 64_t iova, int write); =20 int vhost_virtqueue_start(struct vhost_dev *dev, struct VirtIODevice *vdev, struct vhost_virtqueue *vq, unsigned idx); -void vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev, - struct vhost_virtqueue *vq, unsigned idx); +int vhost_virtqueue_stop(struct vhost_dev *dev, struct VirtIODevice *vdev, + struct vhost_virtqueue *vq, unsigned idx); =20 void vhost_dev_reset_inflight(struct vhost_inflight *inflight); void vhost_dev_free_inflight(struct vhost_inflight *inflight); --=20 2.48.1 From nobody Fri Apr 4 05:15:29 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=1741947435; cv=none; d=zohomail.com; s=zohoarc; b=BJs/BRbnxKc1ANpRSlVZlXOj2A7cdQeb9Lh+nWaBrAGYgQlxvBLmTOYRf+px5ZWIAF3moOLFjD6IpleFbHP2FB3R7DFqUFWYIKwaYElt6WNZvyChOebapYOti2AREc0NBf+KqnbBHr3RWRNBuwQH5HhuTw4rcpwbH2EY+2ChXZM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741947435; 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=xsdw8pAjBTeMk/gQWIVoL0cHkZcubeblaIvJp2xm1+I=; b=T65r7JaaRWF+J8lJyeqg2KnPnIbuch9iN+TzWvHMku8mmWKaYeCBw8rHAE1ITnCFpb26h4xeW06eBtoibgwDNBkTlJIx9weBspmitie0PSmqIjl2qloGPStJFVjA5J9jOBVAYY6CA3vrvvIbyhvyHXMiDZXCUFw7gGu+61jP+9k= 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 1741947435397191.37935097208697; Fri, 14 Mar 2025 03:17:15 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tt25h-00064F-DS; Fri, 14 Mar 2025 06:16:33 -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 1tt25L-0005p5-3g for qemu-devel@nongnu.org; Fri, 14 Mar 2025 06:16:13 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tt25F-000372-IE for qemu-devel@nongnu.org; Fri, 14 Mar 2025 06:16:10 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-22403cbb47fso36657705ad.0 for ; Fri, 14 Mar 2025 03:16:05 -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.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Mar 2025 03:16:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1741947363; x=1742552163; 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=xsdw8pAjBTeMk/gQWIVoL0cHkZcubeblaIvJp2xm1+I=; b=JKXi9AWj/ft8Ps0hdiGeHE93CdxEyzZ8W5tzZE2lBOCZ6A99F8QekWz+z3Ygt6UBG0 J+g7TBJr7Eiv41b/3oNPx90DXfJcsAd7YjB2cDJRt2IrtWB2xsnAaRSWZ1OqR+shseeX v+L2EKjqwylYEHC9IcfUXAhTVrTbvtniA1D2Lu/ldRXjmAhsmfsEbwRQuHkMZ7LBk1Us JOx1I/l49mh1wt4b31owRBSWGraHQvbSH8umQ0wwT2grOryDv6LOiaeakB0Rs0+UyTdj C6gXIMOJgbtvzX76AFKbzywtgXHhlFTYSFOGbZUHzoUage1mrQmuUF3B4Vu9gfvtfU8Z yj1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741947363; x=1742552163; 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=xsdw8pAjBTeMk/gQWIVoL0cHkZcubeblaIvJp2xm1+I=; b=tGyvC6qCp8Avqike/cA0C+kI2EWLXGOTXfsWmQGJPNaquQ6076+UypQHvLlPWgO+Pu rRkaCGJe3YqhsKp9xHxFcPmJUd44XoiaSO3/8fd0bgc3fPzm7mhqPKq+X5jyRn+G9Ykm SFQbXX3l+833MBBvYMGWMFE8NjZccLZDkmXncQfEEAeLSV199v6iZc47px9gQDjmyG3g Xr3qNhKVJvhyfxJGo3ina7P5uuNnCyrSg8+hG26R37yDcYBo7LX9a2ES6AspVjXwxlDA zdsEICPibWx98M2V9R9oYjGNCKr2erk8Y9LEwbDV4aUFXgYnSu8z7G/fq1mMKOw3/ZX7 3W8w== X-Gm-Message-State: AOJu0Ywrnm21R3RJSnykOCYv6GfNkNcpigAyUml08yCLleV4N4+lt8dc RERfPaGYEF+xunGwDxa5P6nf/qCkbRXTcQL3eFsNP8ub6RyI+fmWM2QXqy42tbqVQk9LuuuF5MR f4QUPpiwUC/Q= X-Gm-Gg: ASbGncvbZoEgPiiL3N/tp08j1ryz0EkQfm0JCgvs8u0bH/iqWxQqsth95jrmRRWeA6u 8dImpDvMDXrGmg4E00L1Ru9gbN+XW3eK6z9BkhpmzZrOGOMFILHO65I1TC0MTMHfaTjCKW75vRj jDET/StJ9L+eD+X1pPnpnFX0sviDi+kciUvujukaalsmYE0bmFCJ8oErqyE9vmjeoIxJ9pAtkDi yfrTmV9hYJuDWWjndAQTujQbnfrmaBFVjaxcLC7j9iKM5t/8sSWZeI1dHYn0DpBMea6RAWNvUub Q+nF3nMiGbGq3R5CdkhxQEYdeLZe5W3jLtKYdRhg7GHPMH79oK+nohA/TeLGDM8= X-Google-Smtp-Source: AGHT+IEmFiqPKYkLM2urxD6vyggJ0nEowwzKUU0S7nrQ+r1BQZfHB2kks+ASZ91x6n7uxYpbWhs6ug== X-Received: by 2002:a05:6a00:b95:b0:736:3d7c:2368 with SMTP id d2e1a72fcca58-73722353269mr2864981b3a.7.1741947363469; Fri, 14 Mar 2025 03:16:03 -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 3/3] vhost-user: return failure if backend crash when live migration Date: Fri, 14 Mar 2025 06:15:34 -0400 Message-ID: <20250314101535.1059308-4-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::634; envelope-from=haoqian.he@smartx.com; helo=mail-pl1-x634.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_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: 1741947437720019100 Content-Type: text/plain; charset="utf-8" Live migration should be terminated if the backend crashes before the migration completes. Since the vhost device will be stopped when VM is stopped before the end of the live migration, current implementation if vhost backend died, vhost device's set_status() will not return failure, live migration won't perceive the disconnection between qemu and vhost backend, inflight io would be submitted in migration target host, leading to IO error. To fix this issue: 1. Add set_status_ext() which has return value for VirtioDeviceClass and vhost-user-blk/scsi use the _ext version. 2. In set_status_ext(), return failure if the flag `connected` is false or vhost_dev_stop return failure, which means qemu lost connection with backend. Hence migration_completion() will process failure, terminate migration and restore VM. Signed-off-by: Haoqian He --- hw/block/vhost-user-blk.c | 29 +++++++++++++++------------ hw/scsi/vhost-scsi-common.c | 13 ++++++------ hw/scsi/vhost-user-scsi.c | 20 ++++++++++-------- hw/virtio/virtio.c | 20 +++++++++++++----- include/hw/virtio/vhost-scsi-common.h | 2 +- include/hw/virtio/virtio.h | 1 + 6 files changed, 52 insertions(+), 33 deletions(-) diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c index ae42327cf8..4865786c54 100644 --- a/hw/block/vhost-user-blk.c +++ b/hw/block/vhost-user-blk.c @@ -204,7 +204,7 @@ err_host_notifiers: return ret; } =20 -static void vhost_user_blk_stop(VirtIODevice *vdev) +static int vhost_user_blk_stop(VirtIODevice *vdev) { VHostUserBlk *s =3D VHOST_USER_BLK(vdev); BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); @@ -212,26 +212,26 @@ static void vhost_user_blk_stop(VirtIODevice *vdev) int ret; =20 if (!s->started_vu) { - return; + return 0; } s->started_vu =3D false; =20 if (!k->set_guest_notifiers) { - return; + return 0; } =20 - vhost_dev_stop(&s->dev, vdev, true); + ret =3D vhost_dev_stop(&s->dev, vdev, true); =20 - ret =3D k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); - if (ret < 0) { + if (k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false) < 0) { error_report("vhost guest notifier cleanup failed: %d", ret); - return; + return -1; } =20 vhost_dev_disable_notifiers(&s->dev, vdev); + return ret; } =20 -static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) +static int vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) { VHostUserBlk *s =3D VHOST_USER_BLK(vdev); bool should_start =3D virtio_device_should_start(vdev, status); @@ -239,11 +239,11 @@ static void vhost_user_blk_set_status(VirtIODevice *v= dev, uint8_t status) int ret; =20 if (!s->connected) { - return; + return -1; } =20 if (vhost_dev_is_started(&s->dev) =3D=3D should_start) { - return; + return 0; } =20 if (should_start) { @@ -253,9 +253,12 @@ static void vhost_user_blk_set_status(VirtIODevice *vd= ev, uint8_t status) qemu_chr_fe_disconnect(&s->chardev); } } else { - vhost_user_blk_stop(vdev); + ret =3D vhost_user_blk_stop(vdev); + if (ret < 0) { + return ret; + } } - + return 0; } =20 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev, @@ -597,7 +600,7 @@ static void vhost_user_blk_class_init(ObjectClass *klas= s, void *data) vdc->get_config =3D vhost_user_blk_update_config; vdc->set_config =3D vhost_user_blk_set_config; vdc->get_features =3D vhost_user_blk_get_features; - vdc->set_status =3D vhost_user_blk_set_status; + vdc->set_status_ext =3D vhost_user_blk_set_status; vdc->reset =3D vhost_user_blk_reset; vdc->get_vhost =3D vhost_user_blk_get_vhost; } diff --git a/hw/scsi/vhost-scsi-common.c b/hw/scsi/vhost-scsi-common.c index 4c8637045d..43525ba46d 100644 --- a/hw/scsi/vhost-scsi-common.c +++ b/hw/scsi/vhost-scsi-common.c @@ -101,24 +101,25 @@ err_host_notifiers: return ret; } =20 -void vhost_scsi_common_stop(VHostSCSICommon *vsc) +int vhost_scsi_common_stop(VHostSCSICommon *vsc) { VirtIODevice *vdev =3D VIRTIO_DEVICE(vsc); BusState *qbus =3D BUS(qdev_get_parent_bus(DEVICE(vdev))); VirtioBusClass *k =3D VIRTIO_BUS_GET_CLASS(qbus); int ret =3D 0; =20 - vhost_dev_stop(&vsc->dev, vdev, true); + ret =3D vhost_dev_stop(&vsc->dev, vdev, true); =20 if (k->set_guest_notifiers) { - ret =3D k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); - if (ret < 0) { - error_report("vhost guest notifier cleanup failed: %d", re= t); + int r =3D k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, fals= e); + if (r < 0) { + error_report("vhost guest notifier cleanup failed: %d", ret); + return r; } } - assert(ret >=3D 0); =20 vhost_dev_disable_notifiers(&vsc->dev, vdev); + return ret; } =20 uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t featu= res, diff --git a/hw/scsi/vhost-user-scsi.c b/hw/scsi/vhost-user-scsi.c index adb41b9816..8e7efc38f2 100644 --- a/hw/scsi/vhost-user-scsi.c +++ b/hw/scsi/vhost-user-scsi.c @@ -52,19 +52,19 @@ static int vhost_user_scsi_start(VHostUserSCSI *s, Erro= r **errp) return ret; } =20 -static void vhost_user_scsi_stop(VHostUserSCSI *s) +static int vhost_user_scsi_stop(VHostUserSCSI *s) { VHostSCSICommon *vsc =3D VHOST_SCSI_COMMON(s); =20 if (!s->started_vu) { - return; + return 0; } s->started_vu =3D false; =20 - vhost_scsi_common_stop(vsc); + return vhost_scsi_common_stop(vsc); } =20 -static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status) +static int vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status) { VHostUserSCSI *s =3D (VHostUserSCSI *)vdev; DeviceState *dev =3D DEVICE(vdev); @@ -75,11 +75,11 @@ static void vhost_user_scsi_set_status(VirtIODevice *vd= ev, uint8_t status) int ret; =20 if (!s->connected) { - return; + return -1; } =20 if (vhost_dev_is_started(&vsc->dev) =3D=3D should_start) { - return; + return 0; } =20 if (should_start) { @@ -91,8 +91,12 @@ static void vhost_user_scsi_set_status(VirtIODevice *vde= v, uint8_t status) qemu_chr_fe_disconnect(&vs->conf.chardev); } } else { - vhost_user_scsi_stop(s); + ret =3D vhost_user_scsi_stop(s); + if (ret) { + return ret; + } } + return 0; } =20 static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *v= q) @@ -399,7 +403,7 @@ static void vhost_user_scsi_class_init(ObjectClass *kla= ss, void *data) vdc->unrealize =3D vhost_user_scsi_unrealize; vdc->get_features =3D vhost_scsi_common_get_features; vdc->set_config =3D vhost_scsi_common_set_config; - vdc->set_status =3D vhost_user_scsi_set_status; + vdc->set_status_ext =3D vhost_user_scsi_set_status; fwc->get_dev_path =3D vhost_scsi_common_get_fw_dev_path; vdc->reset =3D vhost_user_scsi_reset; vdc->get_vhost =3D vhost_user_scsi_get_vhost; diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 5e8d4cab53..fff7cdb35d 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -2221,12 +2221,12 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t v= al) { VirtioDeviceClass *k =3D VIRTIO_DEVICE_GET_CLASS(vdev); trace_virtio_set_status(vdev, val); + int ret =3D 0; =20 if (virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1)) { if (!(vdev->status & VIRTIO_CONFIG_S_FEATURES_OK) && val & VIRTIO_CONFIG_S_FEATURES_OK) { - int ret =3D virtio_validate_features(vdev); - + ret =3D virtio_validate_features(vdev); if (ret) { return ret; } @@ -2238,12 +2238,18 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t v= al) virtio_set_started(vdev, val & VIRTIO_CONFIG_S_DRIVER_OK); } =20 - if (k->set_status) { + if (k->set_status_ext) { + ret =3D k->set_status_ext(vdev, val); + if (ret) { + qemu_log("set %s status to %d failed, old status: %d\n", + vdev->name, val, vdev->status); + } + } else if (k->set_status) { k->set_status(vdev, val); } vdev->status =3D val; =20 - return 0; + return ret; } =20 static enum virtio_device_endian virtio_default_endian(void) @@ -3436,7 +3442,11 @@ static int virtio_vmstate_change(void *opaque, bool = running, RunState state) } =20 if (!backend_run) { - virtio_set_status(vdev, vdev->status); + // the return value was used for stopping VM during migration + int ret =3D virtio_set_status(vdev, vdev->status); + if (ret) { + return ret; + } } return 0; } diff --git a/include/hw/virtio/vhost-scsi-common.h b/include/hw/virtio/vhos= t-scsi-common.h index c5d2c09455..d54d9c916f 100644 --- a/include/hw/virtio/vhost-scsi-common.h +++ b/include/hw/virtio/vhost-scsi-common.h @@ -40,7 +40,7 @@ struct VHostSCSICommon { }; =20 int vhost_scsi_common_start(VHostSCSICommon *vsc, Error **errp); -void vhost_scsi_common_stop(VHostSCSICommon *vsc); +int vhost_scsi_common_stop(VHostSCSICommon *vsc); char *vhost_scsi_common_get_fw_dev_path(FWPathProvider *p, BusState *bus, DeviceState *dev); void vhost_scsi_common_set_config(VirtIODevice *vdev, const uint8_t *confi= g); diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 6386910280..c99d56f519 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -187,6 +187,7 @@ struct VirtioDeviceClass { void (*set_config)(VirtIODevice *vdev, const uint8_t *config); void (*reset)(VirtIODevice *vdev); void (*set_status)(VirtIODevice *vdev, uint8_t val); + int (*set_status_ext)(VirtIODevice *vdev, uint8_t val); /* Device must validate queue_index. */ void (*queue_reset)(VirtIODevice *vdev, uint32_t queue_index); /* Device must validate queue_index. */ --=20 2.48.1