From nobody Fri Jun 19 09:25:50 2026 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 4E90520A5F3 for ; Fri, 24 Apr 2026 12:30:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033824; cv=none; b=G7EbF/mlprTowakp6KHy5FGJx+lS/0arKEYhdsU/vm/J6nSKvxilvJQIOppLnCd1cneaUwC4rRqNJNHOaGAqiSZkdDJSBEqwLJq10m+FlJNj5H3fdqap6u5JTpIGxFbumb9gaclIeK0YzS/Bg/TnhVaLyg4sxnDX98YU/4AU4Q4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033824; c=relaxed/simple; bh=ElyyymLN3BzEU2n3paseyXgS1oFdDA7FSr0KdZ24I/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oVLMeIEUlRKnJSGHiUq2UFFR7whGTnq3aBNEFVUbfqsHm8C2zh7SMCSv5M5RwbknXe8z0z4GyIMzgPo3KieOo+ozwZhTlCWx73fznhquQVJkexR2MOOly4Zln9Yt4x+u5neF92CFAPby7i0D/Tl7a9kpjiX1ODlAEMZMXUhtFTo= 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=ODpg/rZn; arc=none smtp.client-ip=209.85.214.176 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="ODpg/rZn" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-2b24fdac394so75542135ad.3 for ; Fri, 24 Apr 2026 05:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777033823; x=1777638623; 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=ODpg/rZnA2eB1B8v9lbUf6fBPCcngxYkXHRgd92LHENxk0lvHL3gSQY3CnbJqLCwjs VSLrD2hkliMMookq+xb70o1JqliHpjLku1LVAGEAbSDtIY9Ww3e8FP2KSpu1dNFcpKnR 1l2CmAYhw4bKlElwvN6qziXsHHxvYkLxAZ/9tu5FVeLS9SIcaJ8XJXRP9irX8MfrYKTm fL10r3U80N+ghQnvPsRs8ULqbsFSW4bRNfiYp9ynHYnsKn1WyL773C9n8hqB0EATmaQj fbVsnC57pQYCQY5H3aKj3BJQdKuOH/aANYag9+l+lUrpwQZMRhPAUeVCvddKq4SqfTkI hqCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777033823; x=1777638623; 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=jDcCaF5+N3ivMu2/8uygyZpej3sYJ13HqoMuNLNdmPlgKDg28iZMi6wL8qMrOZ7Obx smsnAuNWvd73w/HW/15CcXowMC9RzRvhZdQZZSOow3IGgMEbMfI4+6+fTPqexuiAcP+c u13aYEfI4l4w5dr0orht4F/es76Eg2/IzOOni1wOdav0gj/OMl+imwOAn6Qt1JSzFFZp 4eN4eF3FpnP0U2eMr/PT/l9KWa5niZxqwLT5GX4oLf4kG6N+TeVuVJTFh+c2SNHdBtqM WyeppOqnpon9Gy9X+w93wfA58c6bIANwc5fa8L52LP3nVB6muX/+fQBAxEQKTy3+I9PG 4zTA== X-Forwarded-Encrypted: i=1; AFNElJ99+es/l88L1U/WvbwFINbFcN+zh8dF5oYY7rmqVtpLmt99OF/VlLk6GFJ/lfotG4q2EU1lo26VelpexM0=@vger.kernel.org X-Gm-Message-State: AOJu0Yw33TOHUL02gu0nGmoU+3NJ0eoO6cD610c2EOZCeAEU6X8rSTRu twnev24UoOajFnerbSTKCbxYxCvjWJXJ6ekIoz7iND4M+1XrSNn2uBOv X-Gm-Gg: AeBDiettLq1vCNcoRsKqdV6Hh53NFToO97EffGiZWcXK3xfYBbTIMfLTg+M8MulpNy7 KebyKF3zCo7omnFt5OOPAwCexqd7p7cK2ItH7BNbiYmJizwY2hPvAYlqIwGCBszkxasyNmfN3GZ ENbi6wC+xexiKGzG64hC3ZPUYVUmKksAIyNgT6BYrA7JstZJBEBQ/Wsg0/KSl4SZ7+riR9nQakT +sQkzNTyvso82y4z9QAokJLJbjDU8YLmPPZYYBQHle+Fmj3rk8svS7dz5unMZp2A7aBk3Zutgaq VkjIl91l9tN8WqSgwC3O7qyf12tPSLABAVh7khXojWfZESZGiRmB4fU3n/CMolSNAlE24ByI91y Nv82et2kFA9N29+gqpceMQxf6V4otSEKdci1PqRJRQY3DHOUR9/tqzl81LxyPcNdhOvB7u8sqmf ooXjTjk7G1QZnV9xN8TS8x2UkpU1xkGVrkOz3aFBhhNlJmHZWdrjgroRPmxtZZNED4HSXeC5RG/ GuNwGdUG4nHmwk5XZxdd2qjik02BKxV50LcgA== X-Received: by 2002:a17:903:3c24:b0:2b7:a3bf:b2a0 with SMTP id d9443c01a7336-2b7a3bfb415mr82719095ad.5.1777033817771; Fri, 24 Apr 2026 05:30:17 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff734sm218006415ad.10.2026.04.24.05.30.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 05:30:17 -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 v5 1/4] virtio: separate PM restore and reset_done paths Date: Fri, 24 Apr 2026 21:29:51 +0900 Message-Id: <20260424122954.273-2-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260424122954.273-1-baver.bae@gmail.com> References: <20260424122954.273-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 Fri Jun 19 09:25:50 2026 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.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 7268C1B4244 for ; Fri, 24 Apr 2026 12:30:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033822; cv=none; b=BV7B+1baLTUeLuli6XAJKWOS5pSjsiaJTkeEpV6MGzgM9FGLkx8es4nE6LljQ8uwMgetwDn9HFFOlC5CoLdNiU74Sqdo7sp7s7RyHzqQTsROciUjhYpCsXD4f7QPQlf2QFDVt2s5KZa7gyf239Z8gWUhNDOXSmqOciburNaZRfo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033822; c=relaxed/simple; bh=Wp+rXTYV6cCR7GQckednAxBMcKVMT6SIRlSrHu1tg4E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=J431ZPi+GRW60bfaHS3O52xGXw84grgghDB6I9k9sD7+KghoEbsBTmQbuY5SyKgHJMihYW5jRKIKc8xxLy4dMufZlxj+/4IYMoVSXPgCIEWeCLGUNqPnWJzCc+rc/15/UmDj95p7OexJTP+dDJuq0eGXDJXYje2ql9AqBfhgZ9Y= 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=ByerhANY; arc=none smtp.client-ip=209.85.214.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="ByerhANY" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2ad9516a653so39622275ad.0 for ; Fri, 24 Apr 2026 05:30:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777033821; x=1777638621; 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=zfrLDAY7+No90zlR6HffZqbGM74lFTvO1IbltHEt5HY=; b=ByerhANY1Qi3USY/+lZi1d47rmLBbDWV1G55/DxlVAl9mC8Ns4rOlO/noDlKoPTob0 EZ6Bz+Z+etIcWZngT59GdMfT4mDiln6Cca/XRlK82CzuSPbuR1liW7P7lxiK+E4XpGcx qS6nIfup/FDdTYkNmn05T/0L80x/eh8CFD6VshuqaUv72J6WTG/u7uGBHwKLeEzk5asj GgBTgqmccrLO2EROEh6ollyXz+vCOb+7b7rolpDLT0a0OnwwEvvO1zQsDfyc/x9mYgc9 Gwt11gKzE6uywNlIY7hGpMAWTXf5KtQE8DhQUoGb/8pgZKRyvMtiXcsFdPCgZhM0HkqD KeNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777033821; x=1777638621; 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=zfrLDAY7+No90zlR6HffZqbGM74lFTvO1IbltHEt5HY=; b=Ceufn6FcWE3Mkhbnf+nzmpUX0IGX1xOToVJAHBBMLXapFCdFH6EFiWy5uNzo8dZRkG z/J//tX1agT6+6FQvFQZT6DI2M1wEP89iPu8Z8lT/UO2F59t6mqAiRzTBNuU4S6u96tB rU/8d8hci982iAVouf49HL9j49XPtE48ENC7ePNyrBAaJjwleB/q40HUcohG/ui5QGAc Qa+B2nlvQEEF24bOul16UWvdhp+MBMKD4MkWwN2EcxjWSWR29TDex9f2WjhNyqim7DoH XqrlTElNyEZpgtsZxh262VNab5kAgKfrv3Se8ZzEjNpTp+r7jSr//mz060HitFBa8Vzj IWLA== X-Forwarded-Encrypted: i=1; AFNElJ/+NJogdus/tmMYJvNskZlf/MgF4WDgTaXztDJQwbOyxwsWM+kQFS8dZB9XaNDr8Bx1cLYnbpCDEtlu2y8=@vger.kernel.org X-Gm-Message-State: AOJu0Yxnml6PTvHImYQmzRPkV0+V2VeLDd/MqUXH3PouFgyQZF2cXyhZ 6CyuqlDq/UHqCv2FlvUryFBrJVmp/8TouofImguezRVvfPbiUDC6KWTy X-Gm-Gg: AeBDieuU23e61dJbDQiX2rdy/HXgNX6w1HUCea2+dFf9gnnqenp8TXKm/EjDfDBoU9f gXEcg8195/kzd0vvcLZmIkus3SsUuLdzWqTxUEMO6ehE3pMGQ7AoVCFmqTOwc5W78jSngWrggje 0ZsI0Nm0qNDdmstwIqgxeg8BQI9/7xM53xDZjpqE7JLgr+wqXa+8kqnbu/4mbILLFkv92lsGjhX yaiQErs9cnV1YOb1nB8pm6Kxq0uL7RqbHZ/yCILBv8yFfjWIiSO7pTUxUr9sVCYlvQZTIzQKl0n 81t5bAgYlZS3N9eRSI1DPk8wQaDEvggb+D3ICae4WPECDiWFKL9j49Uq81LCxKXSNgWzjt2AtcQ cSfLsi2QJH2QAgcEheiensca6RoOnK5BL9Uwda+3nL7NqfRC90wWH5BRKauMJm00KysHvPGZaE6 AKM9QNVumUkqrqoi5LCzHxXFdY7zAXI2o4nhjWUdRq0E4mTBrAEe5MvXS5gcpPTi8+kkqwxTCNA LlYbI3kv7INdS3qwnDrPWyRTOQGPXaqcJIQvfN4Nv/iNInC X-Received: by 2002:a17:902:8f90:b0:2b4:5f96:184d with SMTP id d9443c01a7336-2b5f9e5da53mr238303975ad.5.1777033820417; Fri, 24 Apr 2026 05:30:20 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff734sm218006415ad.10.2026.04.24.05.30.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 05:30:19 -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 v5 2/4] virtio_ring: export virtqueue_reinit_vring() for noirq restore Date: Fri, 24 Apr 2026 21:29:52 +0900 Message-Id: <20260424122954.273-3-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260424122954.273-1-baver.bae@gmail.com> References: <20260424122954.273-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 | 51 ++++++++++++++++++++++++++++++++++++ include/linux/virtio_ring.h | 3 +++ 2 files changed, 54 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index fbca7ce1c6bf..6631c30cb706 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,47 @@ 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. + */ +void 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 */ + WARN_ON(vq->vq.num_free !=3D num); + + 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. + */ + WARN_ON(vq->split.avail_idx_shadow !=3D + virtio16_to_cpu(vq->vq.vdev, + vq->split.vring.avail->idx)); + + virtqueue_reset_split(vq); + } +} +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..26c7c9d0a151 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) */ +void 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 Fri Jun 19 09:25:50 2026 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 A23401D6195 for ; Fri, 24 Apr 2026 12:30:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033827; cv=none; b=EsdgZQvpzeN7m8Z8+ChhAPBf4lG4SOXAJ+82htIId38qazfOKfnSQZnRYxVWsmINGvdhmEXlANk9Mn8gDRF3lJL39E80eGzF7Fs9SSOQS2Am+8HVEh/2yl/D5Yb14HENzA7KC4LzyEeR/hgZdtCQOztHG1heuqdFfgoTmSDwiIY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033827; c=relaxed/simple; bh=2fmyWu4kxuCuoaJAAeGKpm1IEMx3xSqtJfULeN6+mbE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GY+UtX/lo7s9ck0iIHy2DvrWNhB/1pHuatbx+tlVWww3krQ4hoignLxXn1vgnc2OTLrkZ+zmIJMEijfF/uDysMlOQqF8FmHbPp8uAu4EFDCiEc1ZrFrufiekD+CXHxggvStPTsqyUgJ4tfZCRVBsQFbZQ52idWFQtvyyE3vOpmw= 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=Qt/rn2If; arc=none smtp.client-ip=209.85.214.172 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="Qt/rn2If" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2ab232cc803so38025215ad.3 for ; Fri, 24 Apr 2026 05:30:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777033825; x=1777638625; 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=QLbRBPMmDQ3hSBzjklmsq8CDAdRCFQB8lJl3whr2kLg=; b=Qt/rn2IfybsBy87/bEwPOUn1Y83E4wKHbkeyeda11j6VYK3Xi1NyEFdGTdGYsbxJWe 14KRfQxcyH5FGc06XCl8PWK74vbkCmvh+QOSo2hfZrR1J70bxSDExdUXEBiLwZF9VTGz hQNVTznEn+XYkKq1+4q0xjkTuAr1ZBEJzdYD9vuVOICX70avutrAisSFwYt6b0uwTiN1 0Ko2hHN9vv4Fd4m36Plvwqliab069P8oGZDsBsbg7mbCt6/ONDbFC+t18C7Z6jr+dflI +X6QbEzcWmd7XBLKVBcecQ0tXJk9qq2M/X0MZAHdxE0w5/3nEh0InA07xXwleI01YDdo jOTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777033825; x=1777638625; 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=QLbRBPMmDQ3hSBzjklmsq8CDAdRCFQB8lJl3whr2kLg=; b=hNyt+fsztVCKuTenl2611cZff/yBSKks+FtwpQLopHteg4qFP2TOtNEmb5/v6Sob1c i6brVCn9Ji3vPHfHgnmS00p0V+PVRN3bzCs7KwNg+lHbxGdqSYvH0F3rMxU3BhYMS+zZ vcb6GmbaRdTRQayg4SjTc/1opig7Fav7snLbS6lDRs+/5f2BYfeq96fWF05D0qVr+I0K dcpr1Wc1SnMmBLGHMviNKCHxnLWP2aZQ2+AeqGdnU/VFsWlaNtDr1EkPpBJBWrJvpiEB KQNb/1ePL0agXSSsgH7SGzMdiIPym9OUAqgeNgHfz8krXNWqykRde163FtBdhYIB2eqA 3s9w== X-Forwarded-Encrypted: i=1; AFNElJ8cph34jj4GieD8hjSo9RCBKJBEsCJXOHm14xce4QaRqCkW5/miTq9b+bWabpYlkq9s7kdcNTFVOQgPpVg=@vger.kernel.org X-Gm-Message-State: AOJu0YwR0qgVNrjUd8XLnXzj9aWM0wDMvy+VC6rz6TTTeUFkryvtFakZ er+EsNi7f9OPZWu6IDnLpXQPC1dmkAwzu/2cB27CjpLJFHaMnDKuJaoX X-Gm-Gg: AeBDievmn9VcArUks4iMchF+0mIM8fJ5mon8P7vlIznXY1y36lcjyo1kb1AnWN7Bskp OxEj8Ngq837JG3t9fY7rHe5AgbmZvSEKozNg7mWuSzUGIQE2Z+nqkQjHBkW1w/S7qlS6mK8iyP1 b6BSxytisZbGO0j6eEGWINLS9uNTj4x8jhCS6Sq60L8iXFUucVsBNoIE1F2fHXdn1iuV/rrlhJv em4SDTmnuvQ6pJhq0KUA69M0z/kpVJAufUcY+uc+7T5VShU5Gao7T8sFzk+5Es6T9P3Pek7NhEy b2CrJ1jN8KU+XXp4Qp0vtR0F6WlWWT3QBk8ZHa2QHp03hSWZ7ooaLD8EaGAMiCqMb9c/rHMK/Rs kuVME2Zq5NHkV4h64byohcAgMxt1S17xhYAoqRehE8JrH6MZwNlhSxptD3qqzRnLAcEOCMgo3tj 4SF9y9jwP1uMgxUg5eS3otlBHFeGSSzTodJ3sxbCGnEZFMN4k8XPRN3NoNieU8kzPlrZ+/b6/DR Xowr59VDLYDtRd4JU/lTLsV/ftc8uE/9DXuJABjOzQh+JMY X-Received: by 2002:a17:902:d505:b0:2ae:825b:49a5 with SMTP id d9443c01a7336-2b5f9d67569mr294552005ad.0.1777033822925; Fri, 24 Apr 2026 05:30:22 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff734sm218006415ad.10.2026.04.24.05.30.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 05:30:22 -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 v5 3/4] virtio: add noirq system sleep PM infrastructure Date: Fri, 24 Apr 2026 21:29:53 +0900 Message-Id: <20260424122954.273-4-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260424122954.273-1-baver.bae@gmail.com> References: <20260424122954.273-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 | 239 +++++++++++++++++++++++++++++++++- include/linux/virtio.h | 17 +++ include/linux/virtio_config.h | 39 ++++++ 3 files changed, 289 insertions(+), 6 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 98f1875f8df1..b1d2cba7d59a 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,7 @@ 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; =20 if (drv && drv->freeze) { ret =3D drv->freeze(dev); @@ -645,12 +768,22 @@ 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) - goto err; - - if (!drv) - return 0; + /* + * If this device was already brought up in the noirq phase, + * skip the re-initialization here. + * + * Note: this normal restore path does not call reset_vqs(). + * Drivers that implement restore_noirq and preserve virtqueues + * must make queue/device state consistent in ->restore() when + * noirq restore did not complete. + */ + 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 +804,100 @@ 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. */ + 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: + /* Record that noirq restore failed so FAILED status persists across rese= t/reinit. */ + dev->failed =3D true; + 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..c4f85f1ebffa 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 successfu= lly * @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,15 @@ 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. + * If noirq resume was skipped or failed, core may have reset the device + * before calling this callback. Drivers that preserve virtqueues for + * @restore_noirq must make queue/device state consistent here before + * returning success. + * @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 fall back to @restore. The fallb= ack + * normal resume path does not implicitly perform transport queue reset= for + * preserved virtqueues; drivers must handle that in @restore if needed. * @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 +273,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..81af2ad6a7c3 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 Fri Jun 19 09:25:50 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.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 594BE35B64A for ; Fri, 24 Apr 2026 12:30:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033828; cv=none; b=IJMiq/s+liicKgwRsWmErraSxkg206DvNqXMUe3UxzghNpDLMJuvoj3bvH842RgmcnY+0LyqfR3BMMVzbq2HHbM/gH1NXSoD09mEAKL9zdOyQwB2fym6+VwjwDjTgoZ3E57GUf9dVQ0islJd+7OtcE1WVo/aPK0iwRr5PUeUnkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777033828; c=relaxed/simple; bh=wLKpK3tMbIqIN8Ol6qX8hkN7d3LwE17kRaqy9JLPUw8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JQ54YhMkc74qnNkpFmkyNwrdEWpn7tvllcLFrzx+nSJMCwvbNV03e0ZZuPAI4E7F6ev9x0i5th3cUAVxLmuHCt5XTcGFs+YaQQDY4yP/NuBmxyNUNhhXqFKLdjib6Zx+sI+4BJoR0I5uGGTiUQ/FtVH/H91WUGVDmo9bC3rnZYc= 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=b4qFfM82; arc=none smtp.client-ip=209.85.214.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="b4qFfM82" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2aaf59c4f7cso36885965ad.1 for ; Fri, 24 Apr 2026 05:30:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777033826; x=1777638626; 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=jhejpkHi44NTY/EhjfX/VPO4q3I84QLwQ+oZB4nOOiw=; b=b4qFfM82kvvTYjKMLPLs4XY1qIhMivPx46DdeJEydlPiXLXj8+uieC+RCODQnCP0xj QAk2LfC1/BiWZRo8EydmapbuAYV5Eb17ST6YeV8tQi6Vw2PbVud2UwVjHFSX0ku4+zUw dKhS1hyOmBgDuWDa/5zKUSCCteXsQTzqbfNCl/orJDA0AaKqB61aj4jhKmOyHac1ncnr iRwRCeM19gD4EjoVSo7hmDfCc/dWR4lp8CLXPR2S6Se3Ku9Xx6aJrKDtzglgkLph+otK CZfvw1EV0vnpWBl3ISqbwSy7H6DMdWzLAT7LXWyd5AMbVNaAHKUBFzq+VXtxx5ug1l4T rZDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777033826; x=1777638626; 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=jhejpkHi44NTY/EhjfX/VPO4q3I84QLwQ+oZB4nOOiw=; b=F0xIzD5bx4nChOJt6VbEhke/8oPPirDQpjqDxJIE3SBI2F1zTRXMXvLNBlHIvM8JrE d2wmz5tYtNymsVKJ0N5vl4eZeXhB36YYzl6xJcof4tkYRkQETZ9A5QQhblJm0AhYPGBC nB84rADRNto8fF0Jr1Oj7URNgbTmZeOXpA3s439Q7ytpd38syMiNli/661CMD5/83Jkj dqD3gqLe79EfO6mMpU3JltXkCDBg+gEoS3HZiH8WnyVoBOPW4NRYvS7zFJFonkRU0HfC r9J42ftXgbbZpJFYt4IzCUPIYFOhG9Fs0sLgUzJyPkfcioLdufVcHNwmVc78Uk+7xOuw zJYw== X-Forwarded-Encrypted: i=1; AFNElJ/tttdA9lbwtGmbdtTVvg3tOZx6Rv/hOynqvaTyMIaT/vrV3HmMT5RjY8jtH8PMVjf8JQRhFw9DMZnkf6Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yzb3EZVxl2Vn2+L0Bcs6reiL9NVLygi86sd9xGL0IK8wJDT4jkm soO0ag+t6Dm7r8D/8X9zHSBMx57b2IUi6KnQfZQVGNYgng1WLBkqWs5V X-Gm-Gg: AeBDievIJ5QoBjmLI0qpWsXGMn73z4oj2AgjS3huKIuEiSD+MGQ59M1vnunwYBr0FE8 04j9y+qXDihynWXIsr5wYvputS0xw2F+zebmFbS69mtttG6tVXTChL7fi5s2ielLWjBaikLg7nJ Rh7R8rWZI2lSW41cLw57VEV59A+lfVYDUKMOHmn8af37kVCHXO1yVn8oTgDU9vfKBqSYIzgwOqn 3XtOQXKEkiZT7wFhlfBj3xe7Q6ZwaSYdaBKHQcXMCKEcbvpcklaPWrH4U2vALIFMzjN6MH8Vv6u PXaESHtaA+aYwhoH8sleoQAxykZiTK9yLknC2npq447s8Gg3awxiqAj5PxfQqJshdqjQn2ewN3j 2WOXIWe+bqkATSA/XEnEt8wHGANFW+dTs5rNTGXJSa2xiL60JZdC8/cdvimT2KrC4kGjq3juX7F aVl7x8clh8jUKG+mNg8QMg+qivoHxOi37lGhyWxzepLmZ5vcyDxRiTBN270/UkR4nuJeEBTjMzK kNMWsQGzIkkBTDr2surBMbuNHz0yg/jfxfYHQ== X-Received: by 2002:a17:903:28f:b0:2b4:5309:2c14 with SMTP id d9443c01a7336-2b5f9f3ad64mr348042135ad.31.1777033825376; Fri, 24 Apr 2026 05:30:25 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b5fa9ff734sm218006415ad.10.2026.04.24.05.30.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2026 05:30:24 -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 v5 4/4] virtio-mmio: wire up noirq system sleep PM callbacks Date: Fri, 24 Apr 2026 21:29:54 +0900 Message-Id: <20260424122954.273-5-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260424122954.273-1-baver.bae@gmail.com> References: <20260424122954.273-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 | 134 ++++++++++++++++++++++++----------- 1 file changed, 94 insertions(+), 40 deletions(-) diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c index 595c2274fbb5..1cd262f9f8b6 100644 --- a/drivers/virtio/virtio_mmio.c +++ b/drivers/virtio/virtio_mmio.c @@ -336,6 +336,75 @@ 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 */ + virtqueue_reinit_vring(vq); + + /* 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 +457,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 +561,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 +582,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