From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F06BC3C0602 for ; Wed, 22 Apr 2026 13:11:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776863467; cv=none; b=G2QFIc432t2JkvAaNWVre8CM1sobPwVaW2Gk25PV/Fxzlq2xa98g3u3IfJPcdTuC/s8c/vU9tX/KSUf60Y5f9H0SjFrQKqcD2u0Yrl6UaW72ogtfAdV6VtUr1xVb+sABBHR82CWMchwsDJP/rW2d1Evty4MfaTEAIN2LX9PHPm4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776863467; c=relaxed/simple; bh=03lkltZM33ly/Y1bi8+MQwlUOAdudcrWYo3uPRrj2Pw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e/oSCHclRmR1EGITQ6k34MRg5I3ikw2gAuMPOYnFj+o9c70AtVj4Oexmp86eUmppAZL1PHMpQ2rSVbtby221ooIizxkGoxadx1rkE/nEFLN2gbMTTsAXOLxCJWdZrgfFdi5LnaLKRKUY+KWZ82mtfKZUqlOEKaWYiAgQhMWp6j0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=e1ltMMUg; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="e1ltMMUg" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-2ab39b111b9so22154995ad.1 for ; Wed, 22 Apr 2026 06:11:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776863465; x=1777468265; darn=vger.kernel.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=BfTqY76zyVDHAOhHLjOsF+fg1eNIaQE5Yd8/TQ9BvSE=; b=e1ltMMUgdd4koMcD7kyKi1Cehd75/OtSBi9XOYmXQyY4BCS7omMt9CAj+TcGOYtiP2 PHlEC+D9YBCqDDlPoa0tJVAV048SUFG5RFMnXl2+HmyM9DMIEBqTuL2Hl25kSfzz3ifx Efd/R9yXYUrLhWrME6owHfeSU6rzxpD9gdt08qz08hg5O0CHMhT8kd3s2BMYkjoQXT4P tqcx3fyDtoTygW6hKZzEPyP+QDSmRHZeM6E6pIJX0bftdwfFt8nzQsiAom+nv9WS7Fje EwXdb3ktHmJdfsZpgXoAgIkE8TxDZhRSzlBISJF7ngXv5z/p6miXBTei0CWPZHbPiu9e iLfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776863465; x=1777468265; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=BfTqY76zyVDHAOhHLjOsF+fg1eNIaQE5Yd8/TQ9BvSE=; b=PWDATKcMcci66WZX4HfS8oL0ZWUu1W6/SQDYIO5Eb9G09VrJL3/7nSZn3IFsdxfIn1 V7amOpXeMIpeKnpJyXEWKlI7C/aGExhpqBIbupc1NktowFnRZ5a+hHcMom6fNHNDnr8e iZ2neSskvzSypYvBqtALNcF7uEb4b87RO5JDsQeRzbWSCD9mHY8zZlD4vQaoYzEwLn5f mwox+q1CN2ACbxR/mTtbIGv7zfDQ+9x9XZtoi5Q7DAfM8db9rVd4ztxuIkBNcDlhhR6q 68+GmLWEMzwnpmS3DhtdC9zQWMoKRF1ILv4GfiDThuY+c4EvXgAEq+3kZcdjjuL/D3fj 7FSw== X-Forwarded-Encrypted: i=1; AFNElJ91r4Gp8dFmFcdG5qE4QW2xXEn6MCjYzHYYY6j1//UIR3sTkx5EB0gIy5fS9PAhRTjIhfcl8GW2RhHpM/8=@vger.kernel.org X-Gm-Message-State: AOJu0YyTYPTd7qWQUKyZtD83evucJXaik7nGCxV40n4EcbY+wlxAkgns DM1lTu+eQ9Ug4XhtYZRTa9AlwAMjHY5Y//8MF8dBWqjqJEpUVcxOLXwB X-Gm-Gg: AeBDieuwX9XSAVaEvovdHR3FHqH52VIXec2FdBSrar4l18LmhcZEUKlIEOkVh1oSifO Lb2u5dG/F4ozW/4+Hb725pRPPaUTCmjGPWUcmfNi02/0fFe/LtQN8UT8ogAB7p21wAQgw3V9gz0 CIUR+dBHG/jXcsSCsHFcJBdj428PnNW7JRryf0syCpmD5OP2dqqrFyqT8PCvT98AkSG32SVdc55 PRbvDwAfv7P4KczysnCyU0bo0n6j2wUZV/49W5xSw+iLvXFkegrSYPRNlbZ/yutpTYQsOURUpsV OSKG1sieQv3UMNojVDU15onH7mNDDJzDESIRuUQkSHn7vl21hMbvBRbx8qkSoySVQJGQ92suoJ5 CGT2adNv1mnzi9u+VdtAtYKE+0jabtSP7/Z0lfUqKknb1UjYbfrBolmXGOWSotTX2usQxqLeC5g iVaq3jk0of2XJAXIccKfotm1uZQsAvgZUrqScA4Fy1eUZLpROAm8clUbQArfjcdLj1rg3X5aNeR M12WHNlIgcO0dWJKVdXdhVxL5ycMJpoVJCahA== X-Received: by 2002:a17:903:246:b0:2b6:309:9f72 with SMTP id d9443c01a7336-2b603099feamr202064555ad.21.1776863465116; Wed, 22 Apr 2026 06:11:05 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b606ce9891sm127892235ad.83.2026.04.22.06.11.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Apr 2026 06:11:04 -0700 (PDT) From: Sungho Bae To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Sungho Bae Subject: [RFC PATCH v3 3/4] virtio: add noirq system sleep PM infrastructure Date: Wed, 22 Apr 2026 22:10:13 +0900 Message-Id: <20260422131014.956-4-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260422131014.956-1-baver.bae@gmail.com> References: <20260422131014.956-1-baver.bae@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Sungho Bae Some virtio-mmio devices, such as virtio-clock or virtio-regulator, must become operational before the regular PM restore callback runs because other devices may depend on them. Add the core infrastructure needed to support noirq system-sleep PM callbacks for virtio transports: - virtio_add_status_noirq(): status helper without might_sleep(). - virtio_features_ok_noirq(): feature negotiation without might_sleep(). - virtio_reset_device_noirq(): device reset that skips virtio_synchronize_cbs() (IRQ handlers are already quiesced in the noirq phase). - virtio_device_reinit_noirq(): full noirq bring-up sequence using the above helpers. - virtio_config_core_enable_noirq(): config enable with irqsave locking. - virtio_device_ready_noirq(): marks DRIVER_OK without virtio_synchronize_cbs(). Add freeze_noirq/restore_noirq callbacks to struct virtio_driver and provide matching helper wrappers in the virtio core: - virtio_device_freeze_noirq(): forwards to drv->freeze_noirq(). - virtio_device_restore_noirq(): runs the noirq bring-up sequence, resets existing vrings via the new config_ops->reset_vqs() hook, then calls drv->restore_noirq(). Modify virtio_device_restore() so that when a driver provides restore_noirq, the normal-phase restore skips the re-initialization that was already done in the noirq phase. Signed-off-by: Sungho Bae --- drivers/virtio/virtio.c | 193 ++++++++++++++++++++++++++++++++-- include/linux/virtio.h | 10 ++ include/linux/virtio_config.h | 29 +++++ 3 files changed, 226 insertions(+), 6 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 98f1875f8df1..124ada693f5f 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -193,6 +193,17 @@ static void virtio_config_core_enable(struct virtio_device *dev) spin_unlock_irq(&dev->config_lock); } +static void virtio_config_core_enable_noirq(struct virtio_device *dev) +{ + unsigned long flags; + + spin_lock_irqsave(&dev->config_lock, flags); + dev->config_core_enabled = true; + if (dev->config_change_pending) + __virtio_config_changed(dev); + spin_unlock_irqrestore(&dev->config_lock, flags); +} + void virtio_add_status(struct virtio_device *dev, unsigned int status) { might_sleep(); @@ -200,6 +211,20 @@ void virtio_add_status(struct virtio_device *dev, unsigned int status) } EXPORT_SYMBOL_GPL(virtio_add_status); +/* + * Same as virtio_add_status() but without the might_sleep() assertion, + * so it is safe to call from noirq context. + * + * This assumes that the device's get_status and set_status operations are + * also noirq-safe. Therefore, the device must garantee that get_status and + * set_status can be called from noirq context. + */ +void virtio_add_status_noirq(struct virtio_device *dev, unsigned int status) +{ + dev->config->set_status(dev, dev->config->get_status(dev) | status); +} +EXPORT_SYMBOL_GPL(virtio_add_status_noirq); + /* Do some validation, then set FEATURES_OK */ static int virtio_features_ok(struct virtio_device *dev) { @@ -234,6 +259,38 @@ static int virtio_features_ok(struct virtio_device *dev) return 0; } +/* noirq-safe variant: no might_sleep(), uses virtio_add_status_noirq() */ +static int virtio_features_ok_noirq(struct virtio_device *dev) +{ + unsigned int status; + + if (virtio_check_mem_acc_cb(dev)) { + if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) { + dev_warn(&dev->dev, + "device must provide VIRTIO_F_VERSION_1\n"); + return -ENODEV; + } + + if (!virtio_has_feature(dev, VIRTIO_F_ACCESS_PLATFORM)) { + dev_warn(&dev->dev, + "device must provide VIRTIO_F_ACCESS_PLATFORM\n"); + return -ENODEV; + } + } + + if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) + return 0; + + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_FEATURES_OK); + status = dev->config->get_status(dev); + if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) { + dev_err(&dev->dev, "virtio: device refuses features: %x\n", + status); + return -ENODEV; + } + return 0; +} + /** * virtio_reset_device - quiesce device for removal * @dev: the device to reset @@ -267,6 +324,24 @@ void virtio_reset_device(struct virtio_device *dev) } EXPORT_SYMBOL_GPL(virtio_reset_device); +/** + * virtio_reset_device_noirq - noirq-safe variant of virtio_reset_device() + * @dev: the device to reset + */ +void virtio_reset_device_noirq(struct virtio_device *dev) +{ +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION + /* + * The noirq stage runs with device IRQ handlers disabled, so + * virtio_synchronize_cbs() must not be called here. + */ + virtio_break_device(dev); +#endif + + dev->config->reset(dev); +} +EXPORT_SYMBOL_GPL(virtio_reset_device_noirq); + static int virtio_dev_probe(struct device *_d) { int err, i; @@ -539,6 +614,7 @@ int register_virtio_device(struct virtio_device *dev) dev->config_driver_disabled = false; dev->config_core_enabled = false; dev->config_change_pending = false; + dev->noirq_restore_done = false; INIT_LIST_HEAD(&dev->vqs); spin_lock_init(&dev->vqs_list_lock); @@ -618,6 +694,41 @@ static int virtio_device_reinit(struct virtio_device *dev) return virtio_features_ok(dev); } +/* noirq-safe variant of virtio_device_reinit() */ +static int virtio_device_reinit_noirq(struct virtio_device *dev) +{ + struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); + int ret; + + /* + * We always start by resetting the device, in case a previous + * driver messed it up. + */ + virtio_reset_device_noirq(dev); + + /* Acknowledge that we've seen the device. */ + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); + + /* + * Maybe driver failed before freeze. + * Restore the failed status, for debugging. + */ + if (dev->failed) + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_FAILED); + + if (!drv) + return 0; + + /* We have a driver! */ + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_DRIVER); + + ret = dev->config->finalize_features(dev); + if (ret) + return ret; + + return virtio_features_ok_noirq(dev); +} + #ifdef CONFIG_PM_SLEEP int virtio_device_freeze(struct virtio_device *dev) { @@ -627,6 +738,7 @@ int virtio_device_freeze(struct virtio_device *dev) virtio_config_core_disable(dev); dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED; + dev->noirq_restore_done = false; if (drv && drv->freeze) { ret = drv->freeze(dev); @@ -645,12 +757,17 @@ int virtio_device_restore(struct virtio_device *dev) struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); int ret; - ret = virtio_device_reinit(dev); - if (ret) - goto err; - - if (!drv) - return 0; + /* + * If this device was already brought up in the noirq phase, + * skip the re-initialization here. + */ + if (!drv || !dev->noirq_restore_done) { + ret = virtio_device_reinit(dev); + if (ret) + goto err; + if (!drv) + return 0; + } if (drv->restore) { ret = drv->restore(dev); @@ -671,6 +788,70 @@ int virtio_device_restore(struct virtio_device *dev) return ret; } EXPORT_SYMBOL_GPL(virtio_device_restore); + +int virtio_device_freeze_noirq(struct virtio_device *dev) +{ + struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); + + if (drv && drv->freeze_noirq) { + /* + * If the driver provides restore_noirq and has active vqs, + * the transport must support reset_vqs to restore them. + * Fail here so the PM core can abort the transition + * gracefully, rather than hitting -EOPNOTSUPP on resume. + */ + if (drv->restore_noirq && !list_empty(&dev->vqs) && + !dev->config->reset_vqs) + return -EOPNOTSUPP; + + return drv->freeze_noirq(dev); + } + + return 0; +} +EXPORT_SYMBOL_GPL(virtio_device_freeze_noirq); + +int virtio_device_restore_noirq(struct virtio_device *dev) +{ + struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); + int ret; + + if (!drv || !drv->restore_noirq) + return 0; + + ret = virtio_device_reinit_noirq(dev); + if (ret) + goto err; + + if (!list_empty(&dev->vqs)) { + if (!dev->config->reset_vqs) { + ret = -EOPNOTSUPP; + goto err; + } + + ret = dev->config->reset_vqs(dev); + if (ret) + goto err; + } + + ret = drv->restore_noirq(dev); + if (ret) + goto err; + + /* Mark that noirq restore has completed. */ + dev->noirq_restore_done = true; + + /* If restore_noirq set DRIVER_OK, enable config now. */ + if (dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK) + virtio_config_core_enable_noirq(dev); + + return 0; + +err: + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_FAILED); + return ret; +} +EXPORT_SYMBOL_GPL(virtio_device_restore_noirq); #endif int virtio_device_reset_prepare(struct virtio_device *dev) diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 3bbc4cb6a672..ab66a3799310 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -151,6 +151,7 @@ struct virtio_admin_cmd { * @config_driver_disabled: configuration change reporting disabled by * a driver * @config_change_pending: configuration change reported while disabled + * @noirq_restore_done: set if the noirq restore phase completed successfully * @config_lock: protects configuration change reporting * @vqs_list_lock: protects @vqs. * @dev: underlying device. @@ -171,6 +172,7 @@ struct virtio_device { bool config_core_enabled; bool config_driver_disabled; bool config_change_pending; + bool noirq_restore_done; spinlock_t config_lock; spinlock_t vqs_list_lock; struct device dev; @@ -209,8 +211,12 @@ void virtio_config_driver_enable(struct virtio_device *dev); #ifdef CONFIG_PM_SLEEP int virtio_device_freeze(struct virtio_device *dev); int virtio_device_restore(struct virtio_device *dev); +int virtio_device_freeze_noirq(struct virtio_device *dev); +int virtio_device_restore_noirq(struct virtio_device *dev); #endif void virtio_reset_device(struct virtio_device *dev); +void virtio_reset_device_noirq(struct virtio_device *dev); +void virtio_add_status_noirq(struct virtio_device *dev, unsigned int status); int virtio_device_reset_prepare(struct virtio_device *dev); int virtio_device_reset_done(struct virtio_device *dev); @@ -237,6 +243,8 @@ size_t virtio_max_dma_size(const struct virtio_device *vdev); * changes; may be called in interrupt context. * @freeze: optional function to call during suspend/hibernation. * @restore: optional function to call on resume. + * @freeze_noirq: optional function to call during noirq suspend/hibernation. + * @restore_noirq: optional function to call on noirq resume. * @reset_prepare: optional function to call when a transport specific reset * occurs. * @reset_done: optional function to call after transport specific reset @@ -258,6 +266,8 @@ struct virtio_driver { void (*config_changed)(struct virtio_device *dev); int (*freeze)(struct virtio_device *dev); int (*restore)(struct virtio_device *dev); + int (*freeze_noirq)(struct virtio_device *dev); + int (*restore_noirq)(struct virtio_device *dev); int (*reset_prepare)(struct virtio_device *dev); int (*reset_done)(struct virtio_device *dev); void (*shutdown)(struct virtio_device *dev); diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index 69f84ea85d71..496897bc417e 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -70,6 +70,9 @@ struct virtqueue_info { * vqs_info: array of virtqueue info structures * Returns 0 on success or error status * @del_vqs: free virtqueues found by find_vqs(). + * @reset_vqs: reinitialize existing virtqueues without allocating or + * freeing them (optional). Used during noirq restore. + * Returns 0 on success or error status. * @synchronize_cbs: synchronize with the virtqueue callbacks (optional) * The function guarantees that all memory operations on the * queue before it are visible to the vring_interrupt() that is @@ -123,6 +126,7 @@ struct virtio_config_ops { struct virtqueue_info vqs_info[], struct irq_affinity *desc); void (*del_vqs)(struct virtio_device *); + int (*reset_vqs)(struct virtio_device *vdev); void (*synchronize_cbs)(struct virtio_device *); u64 (*get_features)(struct virtio_device *vdev); void (*get_extended_features)(struct virtio_device *vdev, @@ -371,6 +375,31 @@ void virtio_device_ready(struct virtio_device *dev) dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK); } +/** + * virtio_device_ready_noirq - noirq-safe variant of virtio_device_ready() + * @dev: the virtio device + * + * This assumes that the device's get_status and set_status operations are + * noirq-safe. + */ +static inline +void virtio_device_ready_noirq(struct virtio_device *dev) +{ + unsigned int status = dev->config->get_status(dev); + + WARN_ON(status & VIRTIO_CONFIG_S_DRIVER_OK); + +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION + /* + * The noirq stage runs with device IRQ handlers disabled, so + * virtio_synchronize_cbs() must not be called here. + */ + __virtio_unbreak_device(dev); +#endif + + dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK); +} + static inline const char *virtio_bus_name(struct virtio_device *vdev) { -- 2.43.0