From patchwork Sun Mar 9 09:07:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haoqian He X-Patchwork-Id: 14008267 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2C646C28B28 for ; Sun, 9 Mar 2025 09:08:27 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1trCdV-0004fv-FK; Sun, 09 Mar 2025 05:07:53 -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 1trCd9-0004Z7-SL for qemu-devel@nongnu.org; Sun, 09 Mar 2025 05:07:36 -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 1trCd6-0002WW-LL for qemu-devel@nongnu.org; Sun, 09 Mar 2025 05:07:31 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2234e4b079cso56328725ad.1 for ; Sun, 09 Mar 2025 01:07:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1741511247; x=1742116047; 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=jJuyh3JjTvuxYUGjDGeDYwF67SEbhykfSBmn8PrwmG4=; b=j/9gWcOcsh0AUte7rVCW044GGnfBpnNT4Z5hl18pJ456ydjtaw9eikL5jBVmfCwlFZ IrzUfg0femEIWIx0e5GjPeIEAKsrsAQ+CHtWbdQzLq/zVVcuAip2AKlTc7gkwzY7IcDC 2Ix9+2rE+f0Avib7wAEX8YNV0CdsGgvsxClsjWUUIz4mufeybcV5BZySS6LNEHU7gEFX POwIRqLnX/dCfzdX9HGBhOB0YO/p/IKuycQV62abdsge4Tq59wzLud1peOCAJi+aGf/v xYiZ+DGtK549GAdILZIc67422d437zDr7SaYx6Czv8YBk6KXGvEgPZpSFNqtUDciYdfF faew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741511247; x=1742116047; 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=jJuyh3JjTvuxYUGjDGeDYwF67SEbhykfSBmn8PrwmG4=; b=S9q0vzpd4AK48A/gBCGTnKHHjV3J/FI2o2QSaBQv5DxTGR8ugrpluoj/miQDdBUQe9 N8aaWcnVdKZDCoxgGvJ6sy62GoQNvJGqeKTsnbUoIHnntc5mP4E8ZkMk4ng3b3pp2QC5 nXvpBvEiUSdSqVaoMgE30kfiHc+wrtrn1VVfz3d+Ox7u0gC2wAG1JMm0RG5cB833/rfN 5U3qXk3CJBctugsznK60VgGnoJ5qbmyzNBdufjnRbOmIKithfkyY74vLLAA2fztWzB7Y 16EVH5iiIPZ6IXWq6fG3SGj4dbCl5POxVSKX6C4xLjoY43uVoLpqJUWbhx4hWxSp1nCb dMug== X-Gm-Message-State: AOJu0Yxm+liPaZvJnMo8CF2H+3gj9kIR9QZLkfD+VrAHgzj3KF0DnTIF 8McBnFANmt1/qP9k6h3XzXSmQ0WudBd83zBOAX6vzpfejjIRtKvCAZ8wfzSZyIxOHIVVsAzFXth /WMCdpuIU X-Gm-Gg: ASbGncujHpT96E7PwJ+z+h47HXedDXZFVzKuAQj5mjiLhJCBAVnAsBIPvHGUNbPpz9L S4ZWRrZN0CfgIpu4FJRwmpHN9g2r2GbtWQavW241hPALeFPnjP5nICdWcFNR0L60zcAvz3g9eeU NCfS5XLt5SeLLImDDNVMKE97xV81tyPZaKFFQUIi2f7LyCbcZeBx/cJ2wv7OJQUYUSpRGPt6oZ8 gUezbqva80h5XnKDh9vtX088cOfxHs/wlBBpje71gViX4w9sbJjc6vsdKYYyszuklcyHY/31ygx IQtRfTKHAwdNxr9L1qQzkaot27v68vZ4tSXXqQ3HxPdctllWinuctK4b9w== X-Google-Smtp-Source: AGHT+IHybcbujbLli1Bipp+FLDz4PAsWzrNbfccr6WO2udHgxTk7geWOFCwZoebWCGhssbj9Oa3nSw== X-Received: by 2002:a05:6a21:497:b0:1ee:ced0:f0a4 with SMTP id adf61e73a8af0-1f544c377b2mr19003198637.34.1741511246742; Sun, 09 Mar 2025 01:07:26 -0800 (PST) Received: from 9950x.taila029d.ts.net ([1.202.18.10]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-736b82f2663sm3435654b3a.130.2025.03.09.01.07.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Mar 2025 01:07:26 -0800 (PST) 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_Le_Goat?= =?utf-8?q?er?= , Richard Henderson , qemu-block@nongnu.org (open list:Block layer core) Subject: [PATCH 1/3] virtio: add VM state change cb with return value Date: Sun, 9 Mar 2025 17:07:05 +0800 Message-ID: <20250309090708.3928953-2-haoqian.he@smartx.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250309090708.3928953-1-haoqian.he@smartx.com> References: <20250309090708.3928953-1-haoqian.he@smartx.com> MIME-Version: 1.0 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: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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 a1829e3abd..4d6c3c9a54 100644 --- a/hw/block/virtio-blk.c +++ b/hw/block/virtio-blk.c @@ -1918,7 +1918,7 @@ static void virtio_blk_device_realize(DeviceState *dev, Error **errp) * called after ->start_ioeventfd() has already set blk's AioContext. */ s->change = - 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, NULL, s); 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-handler.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 callbacks @@ -54,21 +55,22 @@ static int qdev_get_dev_tree_depth(DeviceState *dev) */ VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, VMChangeStateHandler *cb, + VMChangeStateHandlerExt *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, opaque); } /* * 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 *prepare_cb, + VMChangeStateHandlerExt *cb_ext, void *opaque) { int depth = qdev_get_dev_tree_depth(dev); - return qemu_add_vm_change_state_handler_prio_full(cb, prepare_cb, opaque, - depth); + return qemu_add_vm_change_state_handler_prio_full(cb, prepare_cb, cb_ext, + 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 = qdev_add_vm_change_state_handler(DEVICE(dev), - scsi_dma_restart_cb, dev); + scsi_dma_restart_cb, NULL, dev); } static void scsi_qdev_unrealize(DeviceState *qdev) diff --git a/hw/vfio/migration.c b/hw/vfio/migration.c index adfa752db5..52bb9a3845 100644 --- a/hw/vfio/migration.c +++ b/hw/vfio/migration.c @@ -976,7 +976,7 @@ static int vfio_migration_init(VFIODevice *vbasedev) vfio_vmstate_change_prepare : NULL; migration->vm_state = 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); 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); } -static void virtio_vmstate_change(void *opaque, bool running, RunState state) +static int virtio_vmstate_change(void *opaque, bool running, RunState state) { VirtIODevice *vdev = opaque; BusState *qbus = 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; } 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 = NULL; } vdev->vmstate = qdev_add_vm_change_state_handler(DEVICE(vdev), - virtio_vmstate_change, vdev); + NULL, virtio_vmstate_change, vdev); vdev->device_endian = virtio_default_endian(); vdev->use_guest_notifier_mask = 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); typedef void VMChangeStateHandler(void *opaque, bool running, RunState state); +typedef int VMChangeStateHandlerExt(void *opaque, bool running, RunState state); VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, void *opaque); @@ -20,21 +21,25 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( VMChangeStateEntry * qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, VMChangeStateHandler *prepare_cb, + VMChangeStateHandlerExt *cb_ext, void *opaque, int priority); VMChangeStateEntry *qdev_add_vm_change_state_handler(DeviceState *dev, VMChangeStateHandler *cb, + VMChangeStateHandlerExt *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 *prepare_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); static inline bool shutdown_caused_by_guest(ShutdownCause cause) { diff --git a/system/cpus.c b/system/cpus.c index 37e5892c24..f07f72621e 100644 --- a/system/cpus.c +++ b/system/cpus.c @@ -298,14 +298,14 @@ static int do_vm_stop(RunState state, bool send_stop) if (oldstate == RUN_STATE_RUNNING) { pause_all_vcpus(); } - vm_state_notify(0, state); + ret = vm_state_notify(0, state); if (send_stop) { qapi_event_send_stop(); } } bdrv_drain_all(); - ret = bdrv_flush_all(); + ret |= bdrv_flush_all(); trace_vm_stop_flush_all(ret); 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_state_head = 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); } /** * 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 reverse is * true when the vm stops @@ -344,6 +346,7 @@ VMChangeStateEntry *qemu_add_vm_change_state_handler_prio( VMChangeStateEntry * qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, VMChangeStateHandler *prepare_cb, + VMChangeStateHandlerExt *cb_ext, void *opaque, int priority) { VMChangeStateEntry *e; @@ -352,6 +355,7 @@ qemu_add_vm_change_state_handler_prio_full(VMChangeStateHandler *cb, e = g_malloc0(sizeof(*e)); e->cb = cb; e->prepare_cb = prepare_cb; + e->cb_ext = cb_ext; e->opaque = opaque; e->priority = priority; @@ -379,9 +383,10 @@ void qemu_del_vm_change_state_handler(VMChangeStateEntry *e) g_free(e); } -void vm_state_notify(bool running, RunState state) +int vm_state_notify(bool running, RunState state) { VMChangeStateEntry *e, *next; + int ret = 0; trace_vm_state_notify(running, state, RunState_str(state)); @@ -393,7 +398,12 @@ void vm_state_notify(bool running, RunState state) } 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, next) { @@ -403,9 +413,14 @@ void vm_state_notify(bool running, RunState state) } QTAILQ_FOREACH_REVERSE_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) { + ret |= e->cb_ext(e->opaque, running, state); + } } } + return ret; } static ShutdownCause reset_requested; From patchwork Sun Mar 9 09:40:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haoqian He X-Patchwork-Id: 14008276 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6ED00C282D1 for ; Sun, 9 Mar 2025 09:41:22 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1trD9e-0000CO-8D; Sun, 09 Mar 2025 05:41:06 -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 1trD9a-00005r-1s for qemu-devel@nongnu.org; Sun, 09 Mar 2025 05:41:02 -0400 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1trD9X-0004Ao-DB for qemu-devel@nongnu.org; Sun, 09 Mar 2025 05:41:01 -0400 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2232aead377so67734005ad.0 for ; Sun, 09 Mar 2025 01:40:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1741513258; x=1742118058; 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=VFsKcWzhSWSgAHaM8U54/pTx6Nzh2t0MccjEcz+0ttfnzo61UOe6iea57nq1nu+ckx tec+826oPokkXDb0S7s1bP059LRKMl3QZyRw2JBeJWvac4wqxP8ENJ2BV3EeQILcvclH RTNdE6IsBwIhtnmSd3I6HqKvImSd+n7NgVPByGXcXqvwhGTamsWhbOAht190cuVObgxk IAtDZZBrKTT3Q99ju1cw/XLRInN7eCQKifTV0K/zRwucMe8/dNh7y4xVE7unfmuQm989 VYzarw+iCXnDF+QlE0f6fCkQPN/2U80EPJ5PnAWlVmYvTTa6gbK2SS+u5U2lJq73OLP4 K9Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741513258; x=1742118058; 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=O6LZdvim6DCfNbjPx8F9hqnaLpv9fGg/Aec0xn24N43llLM5OlhiY8tO0yQBh5wmCw pUCQtBXBWQFn6l+gJw+k1fSrv1mypyryrY0S7yb7sCmGKC6WYruMt1yEKTkpuzOJLPHs qt8vcZMcXED3+SenSici9RgGirlSR902dJJe+N95e1+TaUQ36iOOFRcZx85qRC29WPvL ehuyTSiQSdGI1ApsGccRpxzU54KbXGeXbmVCsaWKilzBV3zmVb00eKCsKEfRzUj82zol ihq/oxdHIZf9xz5XZekCIKEb2/Bj9CqOyjx0e/KC52DEyEExHvBetlPAbLKuFhtBvddT IdsA== X-Gm-Message-State: AOJu0YwtRsuIYJwuL6dEiawGdhxNG8JBJP3WDCeje7wm3n3yUBfIiW0+ 3/Rpye6TngP899Cz3j5W3LnKCyS4MsyurcaxPuR7Hy6FWbyVeJCcae1xvQFFrQJhfgKy8JZbgxq bZOL66A== X-Gm-Gg: ASbGnctyJriiF7Qw0z1ROxpPZ2ygrIXQzjhAARGmr5VKiKCyGR42xiVmMAt4rLKwkKZ 77niE7mXQz2gzWp67SXZuxsZgM4VIVDEYuCo68OENTkWakBVNtutWlQuxaL9lFFD+7DiFVR8aqy rAmUQvkA49abtaQ/Ww+zATqMM7SLMbyXRAVLPSf7LCwkE0H4yXJXYAG6F4DYOWcdU/NfytNEeC/ x1TjLs9VA82l5GyvfBvKuIUKvMMN+Kxvtd4WH8cKiV0TYPVwsVkUlGwtzO1Obt2t44PWXeKBLSb m2gdD4s2iCq0p6ZgLOdUGyWzFhmvniTGd3yzpISwzdCLKB+HLH5b X-Google-Smtp-Source: AGHT+IFd+dUTlIZVThVJQpaxksgShVwmhZ3DuojwHwy13tAbGh3o917KVQ5nV+6J5rP6kyWftCoAZQ== X-Received: by 2002:a17:902:cf07:b0:224:192a:9154 with SMTP id d9443c01a7336-22428a8cf90mr160892165ad.26.1741513257693; Sun, 09 Mar 2025 01:40:57 -0800 (PST) Received: from fedora.smartx.com ([103.85.74.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410ac0794sm57716185ad.259.2025.03.09.01.40.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Mar 2025 01:40:57 -0800 (PST) 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 , Paolo Bonzini , Fam Zheng , qemu-block@nongnu.org (open list:Block layer core) Subject: [PATCH 2/3] vhost: return failure if stop virtqueue failed in vhost_dev_stop Date: Sun, 9 Mar 2025 05:40:50 -0400 Message-ID: <20250309094051.3655292-1-haoqian.he@smartx.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250309090708.3928953-1-haoqian.he@smartx.com> References: <20250309090708.3928953-1-haoqian.he@smartx.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::629; envelope-from=haoqian.he@smartx.com; helo=mail-pl1-x629.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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; } -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 = dev->vhost_ops->vhost_get_vq_index(dev, idx); struct vhost_vring_state state = { .index = vhost_vq_index, }; - int r; + int r = 0; if (virtio_queue_get_desc_addr(vdev, idx) == 0) { /* Don't stop the virtqueue which might have not been started */ - return; + return 0; } - r = dev->vhost_ops->vhost_get_vring_base(dev, &state); + r |= 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; } static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev, @@ -2136,9 +2137,10 @@ fail_features: } /* Host notifiers must be enabled at this point. */ -void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings) +int vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings) { int i; + int rc = 0; /* should only be called after backend is connected */ assert(hdev->vhost_ops); @@ -2157,10 +2159,10 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings) vhost_dev_set_vring_enable(hdev, false); } for (i = 0; i < hdev->nvqs; ++i) { - vhost_virtqueue_stop(hdev, - vdev, - hdev->vqs + i, - hdev->vq_index + i); + rc |= 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, VirtIODevice *vdev, bool vrings) hdev->started = false; vdev->vhost_started = false; hdev->vdev = NULL; + return rc; } 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, VirtIODevice *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, != 0 on error when stopping dev. */ -void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); +int vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings); /** * DOC: vhost device configuration handling @@ -333,8 +335,8 @@ int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write); 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); void vhost_dev_reset_inflight(struct vhost_inflight *inflight); void vhost_dev_free_inflight(struct vhost_inflight *inflight); From patchwork Sun Mar 9 09:40:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haoqian He X-Patchwork-Id: 14008277 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B89A6C282D1 for ; Sun, 9 Mar 2025 09:41:25 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1trD9g-0000I9-UH; Sun, 09 Mar 2025 05:41:08 -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 1trD9f-0000Hg-Cj for qemu-devel@nongnu.org; Sun, 09 Mar 2025 05:41:07 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1trD9d-0004Bf-Gj for qemu-devel@nongnu.org; Sun, 09 Mar 2025 05:41:07 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-225477548e1so13116715ad.0 for ; Sun, 09 Mar 2025 01:41:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1741513264; x=1742118064; 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=1ySNXBRXbnlWGaKRtbvqCGzEZMz6zc9dLcyulHwbJso=; b=ppacD6+Qxpdihy14q4GdWYMAteNuQMxVuYot53eHupqO/GVYDxSVLIFAYQ2MoqHijE SbWETWNCmMQIQfYqCMnViymPXksIXjqbXHEltZmxFuwA83C4LWiuhkFu03gCe+2L4no2 HBPw3mrssyQ9JJqQmt8pPOfSssItG+DBJENurCzMlL9zj4G3VyVkKGNnHN3V/GFvlqjT YNJOQH1N54JKenc/jPZaeK56Cz6OEwjdnDqtgqtjSOh/hIUdp8vKH8Zlid1h/rWCtkdY JfUKeFUFuR/RLfZ/OI0QVWH2OVB/FjWRyxrGLVzTCJHkEyxGSTcP3hqN3jgn09gGkZXV /Lrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741513264; x=1742118064; 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=1ySNXBRXbnlWGaKRtbvqCGzEZMz6zc9dLcyulHwbJso=; b=VWUu8UfigePTi85Qvl8QLyluzPb1GzAAUwEL4jb8i/LJg46vw3i40qHx3UL3XNJmn9 cHJ/q24coiwST9xPzhMaHz4yS3v4i87v7/3sZ5t+yWjPhbDwMi0WFDgK5HKfoz6HuzAe vM0G/PsEKvjPwHQetRWheClsgbG8XYt1O8fVu7+v9VUgKsf4TqqgFGhG3Wdap1ctJTP6 kXRd6mqygUhitJdbByKPe9mnHOfkjrJiGTUfm4btyZ1OfomUz7K9RVmdhvuaAfwL+X10 U7x+BbIHcgKFkLpdWxJ4uDipwPhIu2rmWYyB1G7OIs5OgZ9jiLHUauaTjlqh0YGKQOul G/wA== X-Gm-Message-State: AOJu0YyC79Rq9ZRq2uYDWhfntNnLJRzYA1jRzBeaXl+/3eDDkexFlExv LmhXOb91Nk/SLV0HenpvZGqk5fUZLkls9ot+yE+To2WubG6qjp7moTsU4z47CMZCPNOsGULgk55 UmncyCmYp72BmbOw5 X-Gm-Gg: ASbGncvKiB4WxnW9p9b7kodpWk0rveVfGmtaynum1+ddBKD4NhoRslmOd3WDeI+iaaL uR4bo/++NMvXAbakRfBzQar+AdtvBhXG3oZXeQ1FviJrdnauZaqnhz8bWUai70eV3uwvXluWxYk mbKBVfJRADwvnI2sL5r9zzIDRr+6mBayq9JVGBgqMSOaxRocjtNKhNuPfLhXOR7m0bzXJ2WMe25 sHRTjAp7n8saScf0/pZ4UdIFLKdfAxfB2B9FFiee5u5VD4yAj8BhLb7qYMHBG3HD7NmEMPFygqg Zfhmuwwa13ri44fIfPWdi7WjYR+4hnSbczCbZoAayjdGCLmJN9EJ X-Google-Smtp-Source: AGHT+IFgP/MEs8k8gKEAPDloLiStbhcsdlBmOxR/6l/e6npgwJxqsqh6OC1p6JmveTHSqoluTOVtwg== X-Received: by 2002:a17:903:2445:b0:21f:988d:5758 with SMTP id d9443c01a7336-22428c075ccmr158438235ad.35.1741513263769; Sun, 09 Mar 2025 01:41:03 -0800 (PST) Received: from fedora.smartx.com ([103.85.74.92]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22410ac0794sm57716185ad.259.2025.03.09.01.41.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Mar 2025 01:41:03 -0800 (PST) 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 , Paolo Bonzini , Fam Zheng , qemu-block@nongnu.org (open list:Block layer core) Subject: [PATCH 3/3] vhost-user: return failure if backend crash when live migration Date: Sun, 9 Mar 2025 05:40:51 -0400 Message-ID: <20250309094051.3655292-2-haoqian.he@smartx.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250309094051.3655292-1-haoqian.he@smartx.com> References: <20250309090708.3928953-1-haoqian.he@smartx.com> <20250309094051.3655292-1-haoqian.he@smartx.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=haoqian.he@smartx.com; helo=mail-pl1-x636.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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Live migration should be terminated if backend crash. 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 | 11 +++++----- 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, 51 insertions(+), 32 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; } -static void vhost_user_blk_stop(VirtIODevice *vdev) +static int vhost_user_blk_stop(VirtIODevice *vdev) { VHostUserBlk *s = VHOST_USER_BLK(vdev); BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); @@ -212,26 +212,26 @@ static void vhost_user_blk_stop(VirtIODevice *vdev) int ret; if (!s->started_vu) { - return; + return 0; } s->started_vu = false; if (!k->set_guest_notifiers) { - return; + return 0; } - vhost_dev_stop(&s->dev, vdev, true); + ret = vhost_dev_stop(&s->dev, vdev, true); - ret = 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; } vhost_dev_disable_notifiers(&s->dev, vdev); + return ret; } -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 = VHOST_USER_BLK(vdev); bool should_start = virtio_device_should_start(vdev, status); @@ -239,11 +239,11 @@ static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) int ret; if (!s->connected) { - return; + return -1; } if (vhost_dev_is_started(&s->dev) == should_start) { - return; + return 0; } if (should_start) { @@ -253,9 +253,12 @@ static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) qemu_chr_fe_disconnect(&s->chardev); } } else { - vhost_user_blk_stop(vdev); + ret = vhost_user_blk_stop(vdev); + if (ret < 0) { + return ret; + } } - + return 0; } static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev, @@ -597,7 +600,7 @@ static void vhost_user_blk_class_init(ObjectClass *klass, void *data) vdc->get_config = vhost_user_blk_update_config; vdc->set_config = vhost_user_blk_set_config; vdc->get_features = vhost_user_blk_get_features; - vdc->set_status = vhost_user_blk_set_status; + vdc->set_status_ext = vhost_user_blk_set_status; vdc->reset = vhost_user_blk_reset; vdc->get_vhost = vhost_user_blk_get_vhost; } diff --git a/hw/scsi/vhost-scsi-common.c b/hw/scsi/vhost-scsi-common.c index 4c8637045d..bfeed0cb1b 100644 --- a/hw/scsi/vhost-scsi-common.c +++ b/hw/scsi/vhost-scsi-common.c @@ -101,24 +101,25 @@ err_host_notifiers: return ret; } -void vhost_scsi_common_stop(VHostSCSICommon *vsc) +int vhost_scsi_common_stop(VHostSCSICommon *vsc) { VirtIODevice *vdev = VIRTIO_DEVICE(vsc); BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); int ret = 0; - vhost_dev_stop(&vsc->dev, vdev, true); + ret = vhost_dev_stop(&vsc->dev, vdev, true); if (k->set_guest_notifiers) { - ret = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); - if (ret < 0) { + int r = k->set_guest_notifiers(qbus->parent, vsc->dev.nvqs, false); + if (r < 0) { error_report("vhost guest notifier cleanup failed: %d", ret); } + assert(r >= 0); } - assert(ret >= 0); vhost_dev_disable_notifiers(&vsc->dev, vdev); + return ret; } uint64_t vhost_scsi_common_get_features(VirtIODevice *vdev, uint64_t features, 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, Error **errp) return ret; } -static void vhost_user_scsi_stop(VHostUserSCSI *s) +static int vhost_user_scsi_stop(VHostUserSCSI *s) { VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s); if (!s->started_vu) { - return; + return 0; } s->started_vu = false; - vhost_scsi_common_stop(vsc); + return vhost_scsi_common_stop(vsc); } -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 = (VHostUserSCSI *)vdev; DeviceState *dev = DEVICE(vdev); @@ -75,11 +75,11 @@ static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status) int ret; if (!s->connected) { - return; + return -1; } if (vhost_dev_is_started(&vsc->dev) == should_start) { - return; + return 0; } if (should_start) { @@ -91,8 +91,12 @@ static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status) qemu_chr_fe_disconnect(&vs->conf.chardev); } } else { - vhost_user_scsi_stop(s); + ret = vhost_user_scsi_stop(s); + if (ret) { + return ret; + } } + return 0; } static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *vq) @@ -399,7 +403,7 @@ static void vhost_user_scsi_class_init(ObjectClass *klass, void *data) vdc->unrealize = vhost_user_scsi_unrealize; vdc->get_features = vhost_scsi_common_get_features; vdc->set_config = vhost_scsi_common_set_config; - vdc->set_status = vhost_user_scsi_set_status; + vdc->set_status_ext = vhost_user_scsi_set_status; fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path; vdc->reset = vhost_user_scsi_reset; vdc->get_vhost = 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 val) { VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); trace_virtio_set_status(vdev, val); + int ret = 0; 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 = virtio_validate_features(vdev); - + ret = virtio_validate_features(vdev); if (ret) { return ret; } @@ -2238,12 +2238,18 @@ int virtio_set_status(VirtIODevice *vdev, uint8_t val) virtio_set_started(vdev, val & VIRTIO_CONFIG_S_DRIVER_OK); } - if (k->set_status) { + if (k->set_status_ext) { + ret = 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 = val; - return 0; + return ret; } static enum virtio_device_endian virtio_default_endian(void) @@ -3436,7 +3442,11 @@ static int virtio_vmstate_change(void *opaque, bool running, RunState state) } if (!backend_run) { - virtio_set_status(vdev, vdev->status); + // the return value was used for stopping VM during migration + int ret = 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/vhost-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 { }; 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 *config); 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. */