From nobody Wed Jun 17 06:02:52 2026 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.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 820A13E3C67 for ; Mon, 27 Apr 2026 17:32:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311126; cv=none; b=mGAGFxhEJhFd94q8NKF5SdntHAiHihxstITQj/j1H2T6o9fAFbONFiM5algwYtko4U3oMC/XYw3rnvJ1lDzjiWZgjuVBp+zBJFIjWK94Y5eMMfkBcT+EIrqv6NXS5dDuzquvlMZ/XTJXcA5pdfRKrAJUw8etDxVsg2KmqzmNy2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311126; c=relaxed/simple; bh=ElyyymLN3BzEU2n3paseyXgS1oFdDA7FSr0KdZ24I/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d8hg422GqSpSewyLBlOD3i/2vH9EbQn7KgJWlC1z3dAc2x4d9XIZ+PV8S2xySWS3xviBpHQLLZn7QgHdRpc4mfFRUWZ5AKFsLM1GMCJycE6dQxXMpIK1yGb5p+oSKRmXAR08/ugPpFSb6pGw8yFm8swwuHvuV91CnqBZWHu0+UA= 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=SZQ+Nt95; arc=none smtp.client-ip=209.85.215.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="SZQ+Nt95" Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-c76bde70ec9so4201225a12.2 for ; Mon, 27 Apr 2026 10:32:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777311125; x=1777915925; 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=lmg393/U6YNbXTTx1LhzYWJYt4NvUfYyO+r0SFXM9Jg=; b=SZQ+Nt95FIgu+FEZNhv2peqKJQh+YPQdf5ks/e64h5vI3ooPHl5TLSL/W4G32+UcnF A3tdQ3XJvZ2+5GuWjQmx5ZVD3x2I40vfTKLd90GoLDpaRv2MDlOz344k3qClNOhTQLbu 7vF5tFJbEkU9+jpTg+82wll00HUFa+L3SzddQQkzlr87QvmQ/YkBC+JoAGYSmWoBf/Nr Lwfl++8FH0dMm8tkohxZmY2F6+JwcRnpG6AhQVS7Y8TPGJ0dkmZEi9n/nWmIUPDN+pDY 9ex5eWGLTDPZIxZXCLAt7DCE9yefkRQvrtXd1lo3A7VKhWnrSUhoInOa7/9mRQvLsa7O oUUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777311125; x=1777915925; 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=lmg393/U6YNbXTTx1LhzYWJYt4NvUfYyO+r0SFXM9Jg=; b=WEAt9v7LV6jc1Ktw4WaRqghrGZ1Chc5UMmuSFz7HyVqploUI1Bi6liuy1/PoK6Tpoq I1RUd+mD6BiTZJ+pyt88MizzCwAZY7jzILSrpc95uVP1qR/8Uqt1XLVf3mT04kF0Is9p uFMSibCHa7HzUiCbZ/d6xHIxKjzbwjxkUNPTw5jDkhxV5ZaigvwLJQnCbvTudN8X3rKM 0wRjZRhWdePtqYdeO1Ro3ij23P30Q5gAU/7Q7HzUu3L1zXArlbD58o1L28f1Qj2AaXYZ wsZfME0LzQQJBOB+BDxskoz6a6qZUNd9QzMCJlBuNWFHuiQpPw+3aMlM7rGniKtjSdMA 9v7w== X-Forwarded-Encrypted: i=1; AFNElJ/14optIFcGqKzBDZ+da50dXtMCDarEyhTAOyYA8GbRwidl51Nv4NURsaF87IWxBiECwdF4xnlAkbppKzA=@vger.kernel.org X-Gm-Message-State: AOJu0Yyy/lWDB3SgqXgT5/pI7L+qqEhHlSXIcyXlqpXsnTTv6Qwe6/pF j0I7jBw6A6/tGx3/gwiA3VL3dEGS+TkxdRJO5m9yZZ7Z5vZ2e53FNBx+ktlYl6ye X-Gm-Gg: AeBDievW9P0Sr1lpYO65Ffp0Zh8YnloHb6xTW2VSQEAMQDsJxslyVQRXuIYb9INFKyZ Dpo0hsER3xG6OI+IghgxZV+Fvmf8jRjJBlFUJYrYlMFZCmGEcKf4VyDp5XqME7gObi3tpSroUg0 heXlzjlikXEbSNpByz+kKKYMYsIy7yfpRF8+R+kv6exs4fLDLeMhQIu8fGcrURJi0T6z1GjmGUs teDJ+FzDyyqiuazcYGY4Fdw+6UHM1lABY4anyvrDZITih0HhPYvQ4aqC+GgCBtf5h/jxN1razPf d2sEvxo6MgeCh3HUnzoHRVG/Kgol2IzVzsYH7F5Apv/kU7ucxiO0PeMfDxu6j2ZdhVb5/TT9XS+ nR/N/AhqEXudMLHBuadZtXu463aFffbKy1x2LQr/kIwe8HEqbTzizPqxkgSSCKMKVGiIJTu5RQ3 bVtETSCEfL9CP2M/WhdR9aX0gnYA1sMKUfJxOMA8l3SYmC0XltcjdIMt/lF0Ubv+vGhPFEEmBt5 rus8jRHPk2qF3gxDglOBZ1XGlKBeZfgcqs3ug== X-Received: by 2002:a05:6300:2713:10b0:3a3:1f85:645f with SMTP id adf61e73a8af0-3a398c968f4mr251771637.18.1777311124796; Mon, 27 Apr 2026 10:32:04 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7977031729sm26246246a12.25.2026.04.27.10.32.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 10:32: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 v6 1/4] virtio: separate PM restore and reset_done paths Date: Tue, 28 Apr 2026 02:30:42 +0900 Message-Id: <20260427173045.28652-2-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260427173045.28652-1-baver.bae@gmail.com> References: <20260427173045.28652-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sungho Bae Refactor virtio_device_restore_priv() by extracting the common device re-initialization sequence into virtio_device_reinit(). This helper performs the full bring-up sequence: reset, status acknowledgment, feature finalization, and feature negotiation. virtio_device_restore() and virtio_device_reset_done() now each call virtio_device_reinit() directly instead of going through a boolean- dispatched wrapper. This makes each path independently readable and extensible without further complicating the dispatch logic. A follow-up series will add noirq PM callbacks that only affect the restore path; having the two paths separated avoids adding more conditionals to a shared function. No functional change. Signed-off-by: Sungho Bae --- drivers/virtio/virtio.c | 81 +++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 5bdc6b82b30b..98f1875f8df1 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -588,7 +588,7 @@ void unregister_virtio_device(struct virtio_device *dev) } EXPORT_SYMBOL_GPL(unregister_virtio_device); =20 -static int virtio_device_restore_priv(struct virtio_device *dev, bool rest= ore) +static int virtio_device_reinit(struct virtio_device *dev) { struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); int ret; @@ -613,35 +613,9 @@ static int virtio_device_restore_priv(struct virtio_de= vice *dev, bool restore) =20 ret =3D dev->config->finalize_features(dev); if (ret) - goto err; - - ret =3D virtio_features_ok(dev); - if (ret) - goto err; - - if (restore) { - if (drv->restore) { - ret =3D drv->restore(dev); - if (ret) - goto err; - } - } else { - ret =3D drv->reset_done(dev); - if (ret) - goto err; - } - - /* If restore didn't do it, mark device DRIVER_OK ourselves. */ - if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) - virtio_device_ready(dev); - - virtio_config_core_enable(dev); - - return 0; + return ret; =20 -err: - virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); - return ret; + return virtio_features_ok(dev); } =20 #ifdef CONFIG_PM_SLEEP @@ -668,7 +642,33 @@ EXPORT_SYMBOL_GPL(virtio_device_freeze); =20 int virtio_device_restore(struct virtio_device *dev) { - return virtio_device_restore_priv(dev, true); + struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); + int ret; + + ret =3D virtio_device_reinit(dev); + if (ret) + goto err; + + if (!drv) + return 0; + + if (drv->restore) { + ret =3D drv->restore(dev); + if (ret) + goto err; + } + + /* If restore didn't do it, mark device DRIVER_OK ourselves. */ + if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) + virtio_device_ready(dev); + + virtio_config_core_enable(dev); + + return 0; + +err: + virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); + return ret; } EXPORT_SYMBOL_GPL(virtio_device_restore); #endif @@ -698,11 +698,30 @@ EXPORT_SYMBOL_GPL(virtio_device_reset_prepare); int virtio_device_reset_done(struct virtio_device *dev) { struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); + int ret; =20 if (!drv || !drv->reset_done) return -EOPNOTSUPP; =20 - return virtio_device_restore_priv(dev, false); + ret =3D virtio_device_reinit(dev); + if (ret) + goto err; + + ret =3D drv->reset_done(dev); + if (ret) + goto err; + + /* If reset_done didn't do it, mark device DRIVER_OK ourselves. */ + if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) + virtio_device_ready(dev); + + virtio_config_core_enable(dev); + + return 0; + +err: + virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); + return ret; } EXPORT_SYMBOL_GPL(virtio_device_reset_done); =20 --=20 2.43.0 From nobody Wed Jun 17 06:02:52 2026 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.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 580133E4C89 for ; Mon, 27 Apr 2026 17:32:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311129; cv=none; b=B0H7/4CN7sxTiB8OQfu/I2v7SX0E6cTBdrGrJC7quNU1g8BBwdLq5k5aCFSjjiKGvDJ2Z3YLV4YTJiSMGsFh1iqDKhH483zFL8mlf6MohieJSW+UoQSlnTO/q6YAFDvTlHERftbQmN4R/ttT5NvSnE4qsxpK6gTTXhzAVu9UXS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311129; c=relaxed/simple; bh=MWBv1DHfHJ5MORln2uC6zteVz62qSKn6Mmg1BPVPwwk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e9mGkCcYHYSi/t5hLECcafsQLretcjnR+dna+iJfl/0VDAfa74u7cuRbV8mXLaSKJi0lvmMRGL2fP9NtMSd338uIOU5xhcBe7YukoNbxLpF5+OqxM5znGj5Hfv9OV5liYAqwVAedXv/U1LZKz/vttSUmwRZ5XOLx6cC4gaFgaIU= 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=axMaK+Ik; arc=none smtp.client-ip=209.85.215.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="axMaK+Ik" Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-c70ea5e9e9dso4673490a12.1 for ; Mon, 27 Apr 2026 10:32:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777311128; x=1777915928; 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=r0D2OjrENWJxNTLidObtJYsE1ofQWJtzpBHNBO0DSMM=; b=axMaK+IkBwxPK9YTGutORrC6oxBwFwzW4opBQXK2fQ8k7wyM3E0P4+CkXc1Nnr2JvN F4ewFOY3IVEoXEomuEd0c618teqQFT/Z2KmE/R0f2yKlLT08wUX8xB+NiBcJCA3Ers/6 5kcYxWKtiV0y0ehHRoPDCEsSbOp3FXjrj5xG4pBVwyHQpso85FIhF57dRh0HRfAeUmJ9 KlTzRmJifWDLjyaM9lvYVB7q32sAiuCWoF5ZbRXLkTUnr6+1llXIuBLKo6zUu+ORq9Zh 9aWueXwlDV68Po9QjGmrH8lmWncG9xaH5JPeGqksmr1Gdfz4BQy4p0uepY9eGO3j2mAv 0L2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777311128; x=1777915928; 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=r0D2OjrENWJxNTLidObtJYsE1ofQWJtzpBHNBO0DSMM=; b=O2VLJveM6dgTswnlYeJRDoKRaaYqw5V+7wFin9/eu9ZtnuryD4Sazp40nuHIsp4JP+ n37vlF2FGemehoe+M+yFn7AsffWQTjOut5VazzPSLlgbr7aEkhPgf15wFUFTIEX0CzdP qUZdchb2+uKCGW7SbpoRbdPmqZF78p3ZZ8KwlwumJPcU/yLdVc4xi3e1/gVhFZqz3ClK 5l0dbOodIOjLlgP8nIGl+vkQuM4AvUQYFGL5ycOp42Yq2UNuEis1LGOKbsnUkTe6eJPs ykDpQh3n+4TB1skH7H1S2MR1UndeiuchgnHV4Tl2X+neX7hinmWc7ifu2pLTuSfhBRox 1+IA== X-Forwarded-Encrypted: i=1; AFNElJ8dww9LbrUfvQfgHj5YOZ07eRm6v33Sak7TKyMS/ySlZfbFucpJBCx56VfOjFSTWSHWu3BPNE/vfPPiqmg=@vger.kernel.org X-Gm-Message-State: AOJu0YxnJDHGzKQ2FKzkKE6koynwQWeBZjvZjGYqlSumLu9HzhOaal6m ey7ZQZNlZHa2ETlVXQ6sxf1Us4TPvBNdpui+kKewAXnZt6ntBS9sjFIz X-Gm-Gg: AeBDietSqIFpu7K/lvBY4cS6BiNlTZobWyhCjVOD+Yr4PwKxSpMFF0NAok21PQS4Ycx UeBKeL1wwQAsvKrip1BYSRisLvjB6d7Wx7ZlSql8B3tEC2Ih0764+Y1yTnd/uc0IzSO9XxNamtw FSEv0hWvLGJbqQt22q1KIjhwzNNudGbYMBCZgfT/Ujix526QFuKSMFhdyxX2hEBHqwKyF60ZJyy 4pRfGWW5u9BhdcObDrGFNOOpRuRaIZ8DvNq0m5pcoAuRV6uWWGZHXSIn6iB18ANlNRhihZFkfu9 HUBsWYaEHejRnVZIcVD5JIUatTIZl1zessvtC4JUTjDDGnG2TpTJi0A9nep5B5fy3L2sLuhYXqe crBhzo5stKDGafajvFVAlt60DSy5ZtIUUHvWe4aW6lrOWwlY0GXYK5NLJ7YG/g7h1plkvrmbW9v fMdhwa8aDxIPbZ7TFbLsUzbSyWUrmLrB+btE0wvt3+tTvY8ink1Vr/DKuW+T/Q+pPfYEXfoUMyZ edm8uYmWNEgt9X+DvpaCvqbiUQSbny9B4CIIA== X-Received: by 2002:a05:6a20:394a:b0:3a2:d68d:9e7e with SMTP id adf61e73a8af0-3a398e542acmr321376637.41.1777311127480; Mon, 27 Apr 2026 10:32:07 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7977031729sm26246246a12.25.2026.04.27.10.32.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 10:32:06 -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 v6 2/4] virtio_ring: export virtqueue_reinit_vring() for noirq restore Date: Tue, 28 Apr 2026 02:30:43 +0900 Message-Id: <20260427173045.28652-3-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260427173045.28652-1-baver.bae@gmail.com> References: <20260427173045.28652-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sungho Bae After a device reset in noirq context the existing vrings must be re-initialized without any memory allocation, because GFP_KERNEL is not available. The internal helpers virtqueue_reset_split() and virtqueue_reset_packed() already reset vring indices and descriptor state in place. Add a thin exported wrapper, virtqueue_reinit_vring(), that dispatches to the appropriate helper based on the ring layout. This will be used by a subsequent patch that adds noirq system-sleep PM callbacks for virtio-mmio. Signed-off-by: Sungho Bae --- drivers/virtio/virtio_ring.c | 58 ++++++++++++++++++++++++++++++++++++ include/linux/virtio_ring.h | 3 ++ 2 files changed, 61 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index fbca7ce1c6bf..d3339b820f6b 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -506,6 +506,15 @@ static void virtqueue_init(struct vring_virtqueue *vq,= u32 num) vq->event_triggered =3D false; vq->num_added =3D 0; =20 + /* + * Keep IN_ORDER state aligned with a freshly initialized/reset queue. + * For packed IN_ORDER, free_head is unused but harmlessly reset. + */ + if (virtqueue_is_in_order(vq)) { + vq->free_head =3D 0; + vq->batch_last.id =3D UINT_MAX; + } + #ifdef DEBUG vq->in_use =3D false; vq->last_add_time_valid =3D false; @@ -3936,5 +3945,54 @@ void virtqueue_map_sync_single_range_for_device(cons= t struct virtqueue *_vq, } EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_device); =20 +/** + * virtqueue_reinit_vring - reinitialize vring state without reallocation + * @_vq: the virtqueue + * + * Reset the avail/used indices and descriptor state of an existing + * virtqueue so it can be reused after a device reset. No memory is + * allocated or freed, making this safe for use in noirq context. + * + * Preconditions for callers: + * 1) The vq must be fully quiesced (no concurrent add/get/kick/IRQ callba= ck). + * 2) Transport/device side must already have stopped/reset this queue. + * 3) All in-flight buffers must already be completed or detached. + * + * If called with outstanding descriptors, free-list state can be corrupte= d: + * num_free is restored to full capacity while desc_extra next-chain/free_= head + * may still represent a partially consumed list. + * + * Return: + * 0 on success, or -EBUSY if preconditions are not met. + */ +int virtqueue_reinit_vring(struct virtqueue *_vq) +{ + struct vring_virtqueue *vq =3D to_vvq(_vq); + unsigned int num =3D virtqueue_is_packed(vq) ? + vq->packed.vring.num : vq->split.vring.num; + + /* All in-flight descriptors must be completed or detached */ + if (WARN_ON(vq->vq.num_free !=3D num)) + return -EBUSY; + + if (virtqueue_is_packed(vq)) { + virtqueue_reset_packed(vq); + } else { + /* + * Split queue shadow index should match the visible avail + * index when the queue is fully quiesced. + */ + if (WARN_ON(vq->split.avail_idx_shadow !=3D + virtio16_to_cpu(vq->vq.vdev, + vq->split.vring.avail->idx))) + return -EBUSY; + + virtqueue_reset_split(vq); + } + + return 0; +} +EXPORT_SYMBOL_GPL(virtqueue_reinit_vring); + MODULE_DESCRIPTION("Virtio ring implementation"); MODULE_LICENSE("GPL"); diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h index c97a12c1cda3..8b421fef4fef 100644 --- a/include/linux/virtio_ring.h +++ b/include/linux/virtio_ring.h @@ -118,6 +118,9 @@ void vring_del_virtqueue(struct virtqueue *vq); /* Filter out transport-specific feature bits. */ void vring_transport_features(struct virtio_device *vdev); =20 +/* Reinitialize a virtqueue without reallocation (safe in noirq context) */ +int virtqueue_reinit_vring(struct virtqueue *_vq); + irqreturn_t vring_interrupt(int irq, void *_vq); =20 u32 vring_notification_data(struct virtqueue *_vq); --=20 2.43.0 From nobody Wed Jun 17 06:02:52 2026 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 29A923E51FD for ; Mon, 27 Apr 2026 17:32:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311133; cv=none; b=HOaDer7LtZe7US15sFvUwQe+R7CSkJw2t/8zX4lkC7SSewe6lTgWzzh7QB0MhNm25yS1R1A5k2iZKBx0CqxXJZdquf9HAMsL9ENJ1I2iOaIlncv2zrw7RWwIU9DI9HeNcoPFMmfPLXzhuM4gWKbrZOjSkabp1roghtjjDUPCyg8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311133; c=relaxed/simple; bh=7DwoUM5kTDdK9tr7tbXsFj6Vb/kFoAfgi3ROFWKPDto=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hmfm8dSFDloCaC7jfKvtHcqh1XBQhrdlTScAlzWt9CWVooCOeMf6uTVlZ0gcjrQALWz4j5i8I6RclRGt2JgQDFAnnd4AiQb2mun1sBdnVtBZOTaH3Rcc94FCZa+FkKEZBYy6PJhfW91muVCFT/yHaLCfnVTFm9iH1RD+aORNxCw= 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=WURdV6D6; arc=none smtp.client-ip=209.85.215.179 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="WURdV6D6" Received: by mail-pg1-f179.google.com with SMTP id 41be03b00d2f7-c70fb6aa323so3674865a12.3 for ; Mon, 27 Apr 2026 10:32:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777311130; x=1777915930; 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=eW2qm0EjzH5rw13kLgPY7G7m54t0QudkxtFnSZkOqm0=; b=WURdV6D6DNWtvt2oKPeBpXeaEiuifx9uwgLxdNM8IEheU7DRN+3Vy1J9Lc5k62QOEu 5OlSBC1mQg7IUKkL3Et/yGBCip09NaO3f7bIUoHFcXddXvFPpY4W9h2ylCNV44850gel bjKeJfURDPSMH5j14Pk9oVxBf+Y0fmH5EnLhsI63dSD37fC7UcTZXFcnA/07pIhB/+w8 fs9lW3gmJAdb2CSp0bn/yuZe/pGR0WQSiDgQbGex2iFBdDqbwuP7mQXDIDd5cWHzGxGq Srm0CNl9t4LUCRANbQvWi+MUYdwhDa9eK8RvZ07yPGxS0GyNgJH2ktXT0PkCHC3tbDmY aJ/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777311130; x=1777915930; 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=eW2qm0EjzH5rw13kLgPY7G7m54t0QudkxtFnSZkOqm0=; b=B0C/iNP+Li93yxnTxuK6Zon7jC3SxOQEv2I9DmJLBv28EHjc6F0pBVm/YHobMKz4D8 396D8P/lxkxylVsCo7kFf6cB4hqo/9DxZNuqaQisWmfFGj6npfHFfw7g1b+C7LMK2tm0 6DNLSWuiG0bUF4CJ9D8Iec7JplFch6b3EnYLomqmhOVOpREIPr2VS0t/7qoLEsQRc6wD ONE7goZgmrLdjzVWrmU5KtvR0FllmUDE4WbWx9qN38thHv4SPB6/vg2/4rM131P1Kpgu tgSOL74P7Q4tsc8AbbfZadW0aWsQG31xA7e6VZLlzcWx+jq4qD4q8UHmHEJghMpNvjQO 8VOg== X-Forwarded-Encrypted: i=1; AFNElJ+Wdzi85c4DFJjGCWgl0fyuXESYIhm5Tk+SVixoGqjANsJRTFfwpi+4O980RJGhAFtXLev1FG7+6VIV2ao=@vger.kernel.org X-Gm-Message-State: AOJu0YyOYakfD38aRQGQm/LC65w3TnEvPH0K/q+1yy52/XCbinTaLUzl gvQpNcz8ewNtFAS0DFePDsYXymhrz3srpee+rYH6CTRu6noy4eBwaXmQ X-Gm-Gg: AeBDieskWPB+XdC26CaLIUESN36+kgjL9BeVc9n6TtNazqUW7ynVEsZNBTHAwrmjKlK SmkhUBNajV6mR1jxIk1iMrfoe9FC4DiaKq/vVoQyfli82sZCWIT3YdtIqJ6eLtU91vPtXbbVagg 8Mx6hW3IY5uy91uxWTIP1b6//ln87mL1fIRhd373Q4F/bunOKPl+OCN0ODWbJ1KyLDB7AwEBWXx NJEE/OpxURiZkJdjTgtzziiGi+sAKkoSgC3NjeSA67j3fvnQ7QqMA2BPHznV/EEA5WESppcC+nO KQLL1zPmLp4tndcBbyOG8W/7K9mhWC9CVf2EiX5sOPTfVAn5J8eHST4YHuuJQssBMJiSpsC+p3Q yXx4LYhLghGqnAoI2Kno4QFAvnYrhFU4et3wKQC0vm99Jes8UK1mnG/1Gta5Cpix71zPovqXIL+ NJt7l7ESF0dbJ/ZTmPWCDQhB9keOpQ1Q2yDhm9/QFBhuI+SWJB4VtQ27NznOYnDbQzIQwb4qjw9 fZBJCLARknqddpCtJ0cEfn23KbMHwXiQC0YBQ== X-Received: by 2002:a05:6a20:914a:b0:3a2:bd1d:d68c with SMTP id adf61e73a8af0-3a398f22f6dmr315701637.50.1777311130023; Mon, 27 Apr 2026 10:32:10 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7977031729sm26246246a12.25.2026.04.27.10.32.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 10:32:09 -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 v6 3/4] virtio: add noirq system sleep PM infrastructure Date: Tue, 28 Apr 2026 02:30:44 +0900 Message-Id: <20260427173045.28652-4-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260427173045.28652-1-baver.bae@gmail.com> References: <20260427173045.28652-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: quoted-printable Content-Type: text/plain; charset="utf-8" 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(). Not all transports can safely call reset, get_status, set_status, or finalize_features during the noirq phase: transports like virtio-ccw issue channel commands and wait for a completion interrupt, which will never be delivered because device interrupts are masked at the interrupt controller during noirq suspend/resume. To address this, introduce a boolean field noirq_safe in struct virtio_config_ops. Transports that implement the above operations via simple MMIO reads/writes (e.g. virtio-mmio) set this flag; all others leave it at the default false. The noirq helpers assert noirq_safe via WARN_ON at runtime. virtio_device_freeze_noirq() enforces the contract at freeze time, returning -EOPNOTSUPP early if the driver provides restore_noirq but the transport does not meet the requirements, to prevent a deadlock on resume. virtio_device_restore_noirq() performs a second check as a safety net in case freeze_noirq was not called. Add freeze_noirq/restore_noirq callbacks to struct virtio_driver and provide matching helper wrappers in the virtio core: - virtio_device_freeze_noirq(): validates noirq_safe and reset_vqs requirements, then forwards to drv->freeze_noirq(). - virtio_device_restore_noirq(): guards against unsafe transports, 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 | 255 +++++++++++++++++++++++++++++++++- include/linux/virtio.h | 24 ++++ include/linux/virtio_config.h | 39 ++++++ 3 files changed, 314 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 98f1875f8df1..9a2a9439f1f4 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -193,6 +193,17 @@ static void virtio_config_core_enable(struct virtio_de= vice *dev) spin_unlock_irq(&dev->config_lock); } =20 +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 =3D 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,21 @@ void virtio_add_status(struct virtio_device *dev, unsi= gned int status) } EXPORT_SYMBOL_GPL(virtio_add_status); =20 +/* + * Same as virtio_add_status() but without the might_sleep() assertion, + * so it is safe to call from noirq context. + * + * Requires the transport to have set config_ops->noirq_safe, which declar= es + * that reset, get_status, and set_status do not wait for a completion + * interrupt and are therefore safe during the noirq PM phase. + */ +void virtio_add_status_noirq(struct virtio_device *dev, unsigned int statu= s) +{ + WARN_ON(!dev->config->noirq_safe); + 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 +260,38 @@ static int virtio_features_ok(struct virtio_device *de= v) return 0; } =20 +/* 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 =3D 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 +325,28 @@ void virtio_reset_device(struct virtio_device *dev) } EXPORT_SYMBOL_GPL(virtio_reset_device); =20 +/** + * virtio_reset_device_noirq - noirq-safe variant of virtio_reset_device() + * @dev: the device to reset + * + * Requires the transport to have set config_ops->noirq_safe. + */ +void virtio_reset_device_noirq(struct virtio_device *dev) +{ + WARN_ON(!dev->config->noirq_safe); + +#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 +619,7 @@ int register_virtio_device(struct virtio_device *dev) dev->config_driver_disabled =3D false; dev->config_core_enabled =3D false; dev->config_change_pending =3D false; + dev->noirq_restore_done =3D false; =20 INIT_LIST_HEAD(&dev->vqs); spin_lock_init(&dev->vqs_list_lock); @@ -618,6 +699,47 @@ static int virtio_device_reinit(struct virtio_device *= dev) return virtio_features_ok(dev); } =20 +/* + * noirq-safe variant of virtio_device_reinit(). + * + * Requires the transport to declare config_ops->noirq_safe, which means + * reset, get_status, set_status, and finalize_features are safe to call + * during the noirq PM phase. + */ +static int virtio_device_reinit_noirq(struct virtio_device *dev) +{ + struct virtio_driver *drv =3D 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 =3D 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 +749,20 @@ int virtio_device_freeze(struct virtio_device *dev) virtio_config_core_disable(dev); =20 dev->failed =3D dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED; + dev->noirq_restore_done =3D false; + + /* + * If the driver provides restore_noirq, verify that the transport + * supports noirq PM. It will fail early so the PM core can abort + * the transition gracefully, rather than silently skipping noirq + * restore and then failing in the normal restore path. + */ + if (drv && drv->restore_noirq && !dev->config->noirq_safe) { + dev_warn(&dev->dev, + "transport does not support noirq PM\n"); + virtio_config_core_enable(dev); + return -EOPNOTSUPP; + } =20 if (drv && drv->freeze) { ret =3D drv->freeze(dev); @@ -645,12 +781,31 @@ int virtio_device_restore(struct virtio_device *dev) struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); int ret; =20 - ret =3D virtio_device_reinit(dev); - if (ret) + /* + * If the driver implements restore_noirq but it did not complete + * successfully, the noirq phase must have failed. PM core may + * continue later resume phases for global recovery, but virtio + * does not use the normal restore path as an implicit same-device + * fallback. + */ + if (drv && drv->restore_noirq && !dev->noirq_restore_done) { + ret =3D -EIO; goto err; + } =20 - if (!drv) - return 0; + /* + * Re-initialization is only needed when noirq restore was not + * attempted. If noirq restore succeeded, the transport was already + * reset, features renegotiated, and virtqueues restored, so only + * the driver-level restore() callback (if any) still needs to run. + */ + if (!drv || !dev->noirq_restore_done) { + ret =3D virtio_device_reinit(dev); + if (ret) + goto err; + if (!drv) + return 0; + } =20 if (drv->restore) { ret =3D drv->restore(dev); @@ -671,6 +826,98 @@ 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 =3D drv_to_virtio(dev->dev.driver); + + if (!drv) + return 0; + + /* + * restore_noirq requires that the transport's config ops + * (reset, get_status, set_status) are safe to call during the noirq + * PM phase. Catch the mismatch early at freeze time so the PM core + * can abort cleanly rather than deadlocking on resume. + */ + if (drv->restore_noirq && !dev->config->noirq_safe) { + dev_warn(&dev->dev, + "transport does not support noirq PM\n"); + return -EOPNOTSUPP; + } + + /* + * 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) { + dev_warn(&dev->dev, + "transport does not support noirq PM restore with active vqs (missing = reset_vqs)\n"); + return -EOPNOTSUPP; + } + + if (drv->freeze_noirq) + 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 =3D drv_to_virtio(dev->dev.driver); + int ret; + + if (!drv || !drv->restore_noirq) + return 0; + + /* + * All transport ops called below (reset, get_status, set_status) must + * be noirq-safe. Return early if not - this should normally have + * been caught at freeze_noirq time. + */ + if (!dev->config->noirq_safe) { + dev_warn(&dev->dev, + "transport does not support noirq PM; skipping restore\n"); + return -EOPNOTSUPP; + } + + ret =3D virtio_device_reinit_noirq(dev); + if (ret) + goto err; + + if (!list_empty(&dev->vqs)) { + if (!dev->config->reset_vqs) { + ret =3D -EOPNOTSUPP; + goto err; + } + + ret =3D dev->config->reset_vqs(dev); + if (ret) + goto err; + } + + ret =3D drv->restore_noirq(dev); + if (ret) + goto err; + + /* Mark that noirq restore has completed successfully. */ + dev->noirq_restore_done =3D 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 =20 int virtio_device_reset_prepare(struct virtio_device *dev) diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 3bbc4cb6a672..9a33a1d26c51 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 when noirq restore 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 statu= s); int virtio_device_reset_prepare(struct virtio_device *dev); int virtio_device_reset_done(struct virtio_device *dev); =20 @@ -237,6 +243,22 @@ 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. + * When @restore_noirq is not implemented, core resets and reinitializes + * the device before calling this. When @restore_noirq succeeded, core + * skips reinitialization; drivers should avoid calling virtio_device_r= eady() + * if DRIVER_OK was already set in the noirq phase. + * When @restore_noirq failed, this callback is not invoked for same-de= vice + * recovery; the saved noirq error is propagated instead. + * @freeze_noirq: optional function to call during noirq suspend/hibernati= on. + * @restore_noirq: optional function to call on noirq resume. + * If this callback fails, PM core may still continue later resume phas= es + * for global system recovery. Virtio does not treat @restore as an + * implicit same-device fallback for @restore_noirq failure; drivers sh= ould + * only implement @restore_noirq when noirq resume is their required + * recovery point. + * A noirq restore failure is detected by the normal restore path + * (restore_noirq implemented but noirq_restore_done is false) and + * returns -EIO instead of attempting same-device recovery. * @reset_prepare: optional function to call when a transport specific res= et * occurs. * @reset_done: optional function to call after transport specific reset @@ -258,6 +280,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..0110b091f634 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 @@ -108,6 +111,14 @@ struct virtqueue_info { * Returns 0 on success or error status * If disable_vq_and_reset is set, then enable_vq_after_reset must also be * set. + * @noirq_safe: set to true if @reset, @get_status, @set_status, and + * @finalize_features are safe to call during the noirq phase of system + * suspend/resume. Transports that implement these operations via simple + * MMIO reads/writes (e.g. virtio-mmio) can set this flag. Transports + * that issue channel commands and wait for a completion interrupt (e.g. + * virtio-ccw) must NOT set it, because device interrupts are masked at + * the interrupt controller during the noirq phase, which would cause the + * wait to hang. */ struct virtio_config_ops { void (*get)(struct virtio_device *vdev, unsigned offset, @@ -123,6 +134,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, @@ -137,6 +149,7 @@ struct virtio_config_ops { struct virtio_shm_region *region, u8 id); int (*disable_vq_and_reset)(struct virtqueue *vq); int (*enable_vq_after_reset)(struct virtqueue *vq); + bool noirq_safe; }; =20 /** @@ -371,6 +384,32 @@ void virtio_device_ready(struct virtio_device *dev) dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK); } =20 +/** + * virtio_device_ready_noirq - noirq-safe variant of virtio_device_ready() + * @dev: the virtio device + * + * Requires the transport to have set config_ops->noirq_safe, which declar= es + * that get_status and set_status do not wait for a completion interrupt. + */ +static inline +void virtio_device_ready_noirq(struct virtio_device *dev) +{ + unsigned int status =3D dev->config->get_status(dev); + + WARN_ON(!dev->config->noirq_safe); + 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) { --=20 2.43.0 From nobody Wed Jun 17 06:02:52 2026 Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 BC5943E4C61 for ; Mon, 27 Apr 2026 17:32:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311135; cv=none; b=JBWtMppXqwnm34DKcR2akWf3wRD5wdQzjLwywN3Gbq+1X2er8V1DVvvFifi3mPMLH5ZG5hGOafRDZlxfduGmHFcyGl7L+Y9oIfk3in7sCenyPFVEAgDRaW9EcwcVoJbDTMNbH3VR9WSGls8/UD2J82kr+1UGGGcZRxV/Thq9K8M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311135; c=relaxed/simple; bh=NcY3/HYuL0HhUfal/SPd7W7TNW+k4WOElEB6PI8qU9k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NnsDFBP/4d4Zr1VliFPTg77CrlPcSeBBe/hyGvomRPZzRJ0mXZt+sG22wqG0sSBI1lFmAVw7sU8jb7VkbLVeT32Mjnuu7UO+maM2DNKSSgxvacmraw0g+QFDWcutyrhG+erABSt84ZNYAE7WxKML7AHao1yGbs1B17tAHeglUu8= 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=KoCp6MI7; arc=none smtp.client-ip=209.85.215.177 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="KoCp6MI7" Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-c6e2355739dso4178496a12.2 for ; Mon, 27 Apr 2026 10:32:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777311133; x=1777915933; 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=cgov5pKPFmVmTdXq6mInFjiLm1BGL5Qb5uWZW3rhPvs=; b=KoCp6MI7ihQ1bwIDS2DeLHV+yWIY4JZ/rqwzpvmZDjqm4t5Rpq3WepKiM5nEMUKOCc Gvon44fm1rt09i57qnvNTZYS00uXn5yNfGNvUUcMhemj+vz9VsA52LF9iMjZSMQTisS+ 0YydOI7aRr0+kmsxqpw5ZE//PxmcP7orWoDDtxSKCG4zc2jpDo2XX59uzSJur8qfTkAr pT3arB77BN45ee5ulbMNWWQCt+Wshh7rBgozcKycp84ANZRuOFt/sq09bd2kuuMGbY8I 3GcCgbyXoKjmx8OpmHkh5C2KTx/OwFiAlTpRBUDdDpYeENMLNsW45JGX3ROHOPawmR4M 0Ecw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777311133; x=1777915933; 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=cgov5pKPFmVmTdXq6mInFjiLm1BGL5Qb5uWZW3rhPvs=; b=RuMpyMVaE5/Mu+jwfFSX/z3kRQryASCPoM3uxAWhvHLC3MUL0KUj8N4eHziTpaD3aR F//nCXPY5sCMcQP/1f/gh/xX8ZEwW9Gpk3zHY9HOxox6sz3CztNxdyrnay4qbCM4Hfty c5yJYL75e60DNj20wNsh8m2ZAxAW3PHe92BtBknGpmfNWxj4RwDSFPVOKOkSGFfrxZXE jtqaGiXV4Jk7n7Rx2lL1++7h/8crzCM50yOu2t8z+VROOV4JN23wMTF7/mCt3rxc48oj VKtLI/UTl9VQ8q8DaoOAzy5vGQADs4O/aUNsxyR0F1QbzM2VukHaKfOfLbZy+q5EvmR9 10Yw== X-Forwarded-Encrypted: i=1; AFNElJ8FlpGF/68j6Q3/ipPqe4jyFzAIMFlO+wbqMYjaIdvZ370DS3z1uq8+KJ3xNn176KBoMolWoeKWpzUfEYA=@vger.kernel.org X-Gm-Message-State: AOJu0Yw7KzSPSSYm/V/iPebK2LnVV/lcrJsChMJfmqdSayPLjVbzz/YU vvMqkF7TyBv8C68Q/2M8xZFQ9PaRXRA7T8l6X5lN87dHx74HdmORh6P0 X-Gm-Gg: AeBDiet0vJNgR+pQm5nFdL2Gw6LbZrSQ7t485r+NHjC8bbYFXCVRvuRwuM7h5Q+8ihF Oy8Dk1u+T3/5zhW/uCdu/nPm/wYGUiUWtjVa79elsm/USpA+3IKWZV30+qiktfVlICYQSlKIBp1 Nf0QgDL2v+Q3Y/r0BsxYlLi3gg+LRGa+EMPyhr62QSHChUUP6hPvQ83X4YxbyzonfIZVuSQx6Li ucRPMkgCbE5oGxMpaZdWY6TIbw+KI1Ll8p0+2iGglRS+ZIcR0O7t/ZoNY2KluHokzkTtDliz3E9 7fphDCFHohnI26Y+4Uy2bX3RWLsM1v5xul8dez4VWMb5cVzGF0He1sL2eHDqjYp2Sb0iq4iKaRd RexFYOPCJv9sJeHLzDMAG95zXWQ+VaxPVm/lkJiRHYZjA6NbNvPpsNkQwc5cjk45ccit2DUql+E MBLKHQXez2+M5q8UfM0Ce2iy15DjiJB2YwiCICMZwQ2KEShjx6ToQuzHpC7kU2S6xJUTPD60bw8 dUmzBpIBxcvdlXpw3EGDvJZrv6suOHVryzZTw== X-Received: by 2002:a05:6a21:3393:b0:3a2:bd1d:d684 with SMTP id adf61e73a8af0-3a398e05ec1mr359533637.33.1777311132902; Mon, 27 Apr 2026 10:32:12 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7977031729sm26246246a12.25.2026.04.27.10.32.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 10:32:12 -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 v6 4/4] virtio-mmio: wire up noirq system sleep PM callbacks Date: Tue, 28 Apr 2026 02:30:45 +0900 Message-Id: <20260427173045.28652-5-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260427173045.28652-1-baver.bae@gmail.com> References: <20260427173045.28652-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: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sungho Bae Add noirq system-sleep PM support to the virtio-mmio transport. This change wires noirq freeze/restore callbacks into virtio-mmio and hooks queue reset/reactivation into the transport config ops so virtqueues can be reinitialized and reused across suspend/resume. For legacy (v1) devices, keep GUEST_PAGE_SIZE programming aligned with the noirq restore path while avoiding duplicate programming in normal restore. This enables virtio-mmio based devices to participate safely in the noirq PM phase, which is required for early-restore users. Signed-off-by: Sungho Bae --- drivers/virtio/virtio_mmio.c | 136 ++++++++++++++++++++++++----------- 1 file changed, 96 insertions(+), 40 deletions(-) diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c index 595c2274fbb5..71d13cb57c43 100644 --- a/drivers/virtio/virtio_mmio.c +++ b/drivers/virtio/virtio_mmio.c @@ -336,6 +336,77 @@ static void vm_del_vqs(struct virtio_device *vdev) free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev); } =20 +static int vm_active_vq(struct virtio_device *vdev, struct virtqueue *vq) +{ + struct virtio_mmio_device *vm_dev =3D to_virtio_mmio_device(vdev); + int q_num =3D virtqueue_get_vring_size(vq); + + writel(q_num, vm_dev->base + VIRTIO_MMIO_QUEUE_NUM); + if (vm_dev->version =3D=3D 1) { + u64 q_pfn =3D virtqueue_get_desc_addr(vq) >> PAGE_SHIFT; + + /* + * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something + * that doesn't fit in 32bit, fail the setup rather than + * pretending to be successful. + */ + if (q_pfn >> 32) { + dev_err(&vdev->dev, + "platform bug: legacy virtio-mmio must not be used with RAM above 0x%l= lxGB\n", + 0x1ULL << (32 + PAGE_SHIFT - 30)); + return -E2BIG; + } + + writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN); + writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); + } else { + u64 addr; + + addr =3D virtqueue_get_desc_addr(vq); + writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW); + writel((u32)(addr >> 32), + vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH); + + addr =3D virtqueue_get_avail_addr(vq); + writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW); + writel((u32)(addr >> 32), + vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH); + + addr =3D virtqueue_get_used_addr(vq); + writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW); + writel((u32)(addr >> 32), + vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH); + + writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); + } + + return 0; +} + +static int vm_reset_vqs(struct virtio_device *vdev) +{ + struct virtio_mmio_device *vm_dev =3D to_virtio_mmio_device(vdev); + struct virtqueue *vq; + int err; + + virtio_device_for_each_vq(vdev, vq) { + /* Re-initialize vring state */ + err =3D virtqueue_reinit_vring(vq); + if (err < 0) + return err; + + /* Select the queue we're interested in */ + writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL); + + /* Activate the queue */ + err =3D vm_active_vq(vdev, vq); + if (err < 0) + return err; + } + + return 0; +} + static void vm_synchronize_cbs(struct virtio_device *vdev) { struct virtio_mmio_device *vm_dev =3D to_virtio_mmio_device(vdev); @@ -388,45 +459,9 @@ static struct virtqueue *vm_setup_vq(struct virtio_dev= ice *vdev, unsigned int in vq->num_max =3D num; =20 /* Activate the queue */ - writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM= ); - if (vm_dev->version =3D=3D 1) { - u64 q_pfn =3D virtqueue_get_desc_addr(vq) >> PAGE_SHIFT; - - /* - * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something - * that doesn't fit in 32bit, fail the setup rather than - * pretending to be successful. - */ - if (q_pfn >> 32) { - dev_err(&vdev->dev, - "platform bug: legacy virtio-mmio must not be used with RAM above 0x%l= lxGB\n", - 0x1ULL << (32 + PAGE_SHIFT - 30)); - err =3D -E2BIG; - goto error_bad_pfn; - } - - writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN); - writel(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN); - } else { - u64 addr; - - addr =3D virtqueue_get_desc_addr(vq); - writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW); - writel((u32)(addr >> 32), - vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH); - - addr =3D virtqueue_get_avail_addr(vq); - writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW); - writel((u32)(addr >> 32), - vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH); - - addr =3D virtqueue_get_used_addr(vq); - writel((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW); - writel((u32)(addr >> 32), - vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH); - - writel(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY); - } + err =3D vm_active_vq(vdev, vq); + if (err < 0) + goto error_bad_pfn; =20 return vq; =20 @@ -528,11 +563,13 @@ static const struct virtio_config_ops virtio_mmio_con= fig_ops =3D { .reset =3D vm_reset, .find_vqs =3D vm_find_vqs, .del_vqs =3D vm_del_vqs, + .reset_vqs =3D vm_reset_vqs, .get_features =3D vm_get_features, .finalize_features =3D vm_finalize_features, .bus_name =3D vm_bus_name, .get_shm_region =3D vm_get_shm_region, .synchronize_cbs =3D vm_synchronize_cbs, + .noirq_safe =3D true, }; =20 #ifdef CONFIG_PM_SLEEP @@ -547,14 +584,33 @@ static int virtio_mmio_restore(struct device *dev) { struct virtio_mmio_device *vm_dev =3D dev_get_drvdata(dev); =20 - if (vm_dev->version =3D=3D 1) + if (vm_dev->version =3D=3D 1 && !vm_dev->vdev.noirq_restore_done) writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); =20 return virtio_device_restore(&vm_dev->vdev); } =20 +static int virtio_mmio_freeze_noirq(struct device *dev) +{ + struct virtio_mmio_device *vm_dev =3D dev_get_drvdata(dev); + + return virtio_device_freeze_noirq(&vm_dev->vdev); +} + +static int virtio_mmio_restore_noirq(struct device *dev) +{ + struct virtio_mmio_device *vm_dev =3D dev_get_drvdata(dev); + + if (vm_dev->version =3D=3D 1) + writel(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE); + + return virtio_device_restore_noirq(&vm_dev->vdev); +} + static const struct dev_pm_ops virtio_mmio_pm_ops =3D { SET_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze, virtio_mmio_restore) + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(virtio_mmio_freeze_noirq, + virtio_mmio_restore_noirq) }; #endif =20 --=20 2.43.0