From nobody Sat Jun 13 14:47:56 2026 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.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 39D3547ECE3 for ; Wed, 6 May 2026 16:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084656; cv=none; b=uymS9Jg4/1OQfBWTRTsWKAw+qOhqDkye31r5HD8cO3lJj7pF8yWdOGNImB6u9O8gJFP/mSGs2+K1nl4XpG0CtBW+7PtOaMvo6fQA31o/joAaOzsWump7rTJfKmrELGC1iWYrL/S8Cnp1Zi4mGdjqYlDiJIBdGiNUrJZPT8+oraA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084656; c=relaxed/simple; bh=ElyyymLN3BzEU2n3paseyXgS1oFdDA7FSr0KdZ24I/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LDYiLSy9rnbhRUuGiXM4gUGtUb7XKLV+yvMrGzFAnkxW0L2X7of3WtsT7I2IFKa36POZfMtreX0o2lqJjBe2ILgHgZdPKU39kHNcSFbbobKWtOIi8W/JvoQGoTnX0UlenZ9nR25JggxHOvEXzuc0YCtMs9wxhHM+2dJp9HyBuJs= 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=KG4ZVS6M; arc=none smtp.client-ip=209.85.210.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="KG4ZVS6M" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-836ebdeb969so1693966b3a.3 for ; Wed, 06 May 2026 09:24:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778084654; x=1778689454; 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=KG4ZVS6MZbPlAza67aao799w0L7npBwOUwVFWlepZ9pnBRARkec/izSQBx76Q+lOhv B+BxahZs4moJOGLVxHmbBO+UuXMGgy7ibE/np56FURnvml9Mg2L8u4cHmlTzXRgEZmLE mV0MDWJinjHorLRcLvZCW0mXgql1hylSWJwOl92Z7TNf5wvHQJPTtxMHYqXV2rh4cJ97 CMNmgH4KEcFl3Ki2h5+V+RCokFlz+lRR4DfFsh4cG2Icgida7E8Ft2eWZ+TmfSogj791 H0BH04/dINgNlIoopy7TSFWaQ/LO1b8BBivpOTXNF+giorG+Y99u8TqQuvWpNf/oy9tH shww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778084654; x=1778689454; 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=cucgMb2Ham5yvV6M8Sz0GZWO8CfMHTanWALFkO24itKnakpgKePYljPX8lhRRzz/8b ttcBYtnvPBvTB76dOdp8OZZsYCs4Zgb7BrqmMBDxjCZUvQxPScREOZJ5H5qGJa34JVKy hCRWq+T5+wyFBASRXU3sZpk8S3hOGT3ZXZIemYz0qKXT575hwnFHimtF2PG4MA4ueRQs vGanYgH5qR3NBy/TllZK27fIZo7Lrx3kBX7s8B+liRox8hunCeeMENjY2RP2srlv9Gdw hDfAj8TUpt4E7Li8xD47NhjS4S/8tLc2UVElVYR8CJ4CNMHgS5Va9/+S57rHKgqXqD3I vhQg== X-Forwarded-Encrypted: i=1; AFNElJ8mVesrhrRD0rmLPWdoR2U/Nw2WX+eI57e0Mtch3Yr5GOjEecChHrQOpGJKkZBAnj9ZGA9VDW/bDCY0c9s=@vger.kernel.org X-Gm-Message-State: AOJu0YyAa5DNt64uBvAGVeXaftdhPzSj4FmjjboH929PVDpSaAnO/obg A6vKPCFv9ElxPVFkC0qJbM8T1/DRLPazmljxEH8D14/2FXjS9zLhUg8x X-Gm-Gg: AeBDievY459ET6uHYAZaZnX90B6g1OoFK/SQ2V7z0y9I7NgSXAKnVXdq3ClvmzgpGra cBM7ipdmv2B4HTxKpV1/hOK86ckZjoq0HiBFpiRHGTqBBNrAqfCilhFGtaFOYRLC5iBEs7kVy1q 733DstmQ8HDCxIATGr5E+3ZPyTmv5d0oLUUA0d74GTc6FD8hce+MBV8mTu7ni/u3A3jKYr6Cqc8 6s6dSkP87tSDJ8e08bPr+/4pw8mch9b8aKrwHMPw1FQmE3l3euCTLKqgDXv0r9HAk0yLkYbHWfo UWqAZHO+0BHIqMtMha3NIG+3+1mn/v2OMgGAI7Ta2OYXqZPR3TA/ZQ+m2WRMiSTC1HtozfTjNY8 H8Jvsf1VjZsgi03aqJXTAgS8ueq4qNiolqoJ/kCd+1w1BDYdQOyAt53AZJxVXNVeWi+Z3Sqk4E5 yP4wm/+wJ+2dE0HKz9XFULI4D+lb/XVSfcQk+v60rM9TmBrzrBd+9PKju9DOrlq0W1abjm2PLWm PqObYh3A7ftPnwqj0UH/8/trXvy0uHa1bKw1Q== X-Received: by 2002:a05:6a00:4616:b0:824:3bd9:aac6 with SMTP id d2e1a72fcca58-83a5b9d824bmr3948577b3a.16.1778084654372; Wed, 06 May 2026 09:24:14 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8629sm5980337b3a.36.2026.05.06.09.24.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 09:24:14 -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 v8 1/4] virtio: separate PM restore and reset_done paths Date: Thu, 7 May 2026 01:22:51 +0900 Message-Id: <20260506162254.25576-2-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260506162254.25576-1-baver.bae@gmail.com> References: <20260506162254.25576-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 Sat Jun 13 14:47:56 2026 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 E2EF8481FB0 for ; Wed, 6 May 2026 16:24:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084659; cv=none; b=F94FtXqWoO3OP4k8W4mWzAzOXnQcJJGAjXbVPX3zVouyHpzd4trMA1G45w4Cy4UWgxMMYh4hpAJRfGh8pDcsvAKxHR4atsgg7Hwbk4WCiRt7oa/g/HgtJyQaLU2hF0gRXPH6ljD5oRl0B71gWfufkK/kPWNhnKEkK8ObPrO/ESU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084659; c=relaxed/simple; bh=MWBv1DHfHJ5MORln2uC6zteVz62qSKn6Mmg1BPVPwwk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AhUaufwwYrYXo9WQlNM9aq1FcYcDtUsEerr5iVZYRIxsKGl5pwr2J/By4jawc3lITtmyXqSwUZi7x1pbYe4NKjPKaXLjp/lmavK3ddGWIIWBNVswv9FPCTGQHENdpinOjZngukxEPNpXeW1Nk75lY0MHz/dS5P5wU0EMhJGIWj8= 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=SCiZuhjt; arc=none smtp.client-ip=209.85.210.180 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="SCiZuhjt" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-8353ca0f1f1so577567b3a.1 for ; Wed, 06 May 2026 09:24:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778084657; x=1778689457; 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=SCiZuhjt5gYEeVPkwVoxLB3NC1cgqBEYffF3yM5SmNQrM48aOmZkkF3IJrz5Aypj0G eEwJy4P/yTjqN9xB3aBqVOTef307TlHEj7641qwwB3+Fu/Ks3bXn9PN0c+Q9kSxPkYzY XoGLV3bN9rMgRcbIfNmKXSDkgsG+0SzuSy4gKhymTApmJ20yAAGA021BRXR9wNk3HOfH WKY0uaeVZuHla5TqbmjJohLOcBgFkQ/T8PSMTSp4ns/LDll0aqi9XVO0Ck40WX81Yuyg srYC9szaFJMniqNlEBX1891/B/xea0w2+X1UTJXBEiWT8at5IzFMAoqIpEx91lae2Oq5 ChFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778084657; x=1778689457; 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=fXD2Hk8xMGipxh8ch54ERgY6rl9hFE245+vxMo9BqOYQ43w5qX/3oN81a7SZ977FDn EwY1y1KT9z519t8l/li1N+eJeUQNG/nBZaE/1hgRGEEBPP7RKgoVpYMh2TciTOFWH6gI yJm3vzwu8MYoAFC9ZLYxUUXXOtA1lPe7efaibOPGYXtTQXR3ZOQszNonOnYX6RGx1TMJ LvDPUegMQGhW7wbP3G9AY4w0r7ZN8JUoUkTfhyTr5dYZri3W1UQfnusrCPwQd7QIKzg+ 2+1Qb7TcVVK1LaIS5DZRCigDErqz+EUbXXgyycDLexO2M3wP6CxVvLOcLgY3uoZkLPLI fPHA== X-Forwarded-Encrypted: i=1; AFNElJ+7ZFx3Tys00cP+sJuv4jw8mpBjuEYpZ+ilaZb/Dx5KMpMh9/5cCpe+rkiTula2heGenQ0N/RmU3MsZhdU=@vger.kernel.org X-Gm-Message-State: AOJu0Yxwemv/gWSWHcPSfvFHwtGDivHb4fioHMajbDiho2Z/3QmZKgtK dDQhHQa890x9klEefSRyCZ+3aGK0eufGdsmI7iYDe4l5p3bmNsyDLS18 X-Gm-Gg: AeBDieuT+pV/EychZpQpXn8hNGsX+F9LLBRwKKOLck9yZy+3tEvgCKIEgLQXJMVejxk 98T45lgRi3hJTKrYze9+wT9momfsqJHX+a3VyA/Rl0Sw6m/pyVbWP+j/aow7hLMuz3UPwoJDjP/ CE0BBepoIpYJeqeQnUlXmTWuPymPtcmWkqU7p8Bu1C4WFHQ5ejH+Q4cHdzbBcS20ROc0wr773Yz JW75PejeQ8HHVy5f9vBmLo8x19UkJpblSGX4rCkKdkpGm5P/iylZxOrvcXx+1KXLu5YXm70aciV UaouNs3bdcE74htN2KEZdgiF5T86AXia69XTwY3bDj0fMhXfWfVP1pd0mRcc3Xz8A1GQLDaGqdz rSCPnBTslRY+/DfOf7Sq6RNd5KlVx1tLuj2cBg7nnOXPdHh98up4bC02LZHZn7J1fDntjdRNVBZ H7Op5QDV6yWrRQTkd/xSOiMGoBzQLajDIrY/D+6NhhxvGtuZo28tfCpxGiuzhlaBoN7WJanKwDH 6TCbNBTL9S/cbDTrNrq9X5ZCys98sagscF1gQ== X-Received: by 2002:a05:6a00:3d08:b0:81f:5acb:55fc with SMTP id d2e1a72fcca58-83a54de4c38mr3284338b3a.10.1778084656982; Wed, 06 May 2026 09:24:16 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8629sm5980337b3a.36.2026.05.06.09.24.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 09:24:16 -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 v8 2/4] virtio_ring: export virtqueue_reinit_vring() for noirq restore Date: Thu, 7 May 2026 01:22:52 +0900 Message-Id: <20260506162254.25576-3-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260506162254.25576-1-baver.bae@gmail.com> References: <20260506162254.25576-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 Sat Jun 13 14:47:56 2026 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) (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 93B6F492529 for ; Wed, 6 May 2026 16:24:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084663; cv=none; b=Z4AIh5SsLCSsSbT4LbY9KwIHTMdjlzgNTaXtDFOzruSl3R8mIL1LdDDKKKoVqYGsJr1x9J3d8QTwf3enqgieL5Db9HaCHWiFCjGMwGxi2JECG9X23iwYg72FVHn0NCvSmfUNgGj4zoo2pFWZe+ALAsvhvzS+jg9sTvZl2Mn+M2U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084663; c=relaxed/simple; bh=akbSaJuDyI+9w9xWGv+PNgi3DkfYHIta1e3lwiG7ftU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZNl1zqrDmLyBj2Rd/a/EOK7iPcQs/OLHmo4Q+W0n/3/S9I5hcup7rcU1Qcc9ksr9Bqpwid5fmrT6NesdcDGjCk05MW4k0eJlYZUGDxN/eC57SSFR2pj1RyJuBg2CdDOhlmq6zTh3cKRVxNWD6HgsqfTzhVLHgd4jDGHnELJAhig= 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=PrLuzA3k; arc=none smtp.client-ip=209.85.210.169 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="PrLuzA3k" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-839dc688d6cso798848b3a.2 for ; Wed, 06 May 2026 09:24:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778084660; x=1778689460; 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=93OQr3bmhiR/HStReHr/b9EA87qTqN9YZsE+Bz35FIY=; b=PrLuzA3kFrEn6bLVplcyDmR15cCt/BCt0Tlb4P3C5IeQncfRj5vgn8JThVUYYNgHBb lNvaOqKNwm+OAUPKXY2D+W7NMCoLtSdUDeNXsFCBEmQdLJd7+vsNxKBydBwRKlLYFqFZ jmbF6Xv5qURlNkzg090xf6DLyCivejq1JaYTkM5Kx74/GsNjKJwfoWLQq5d0gxYEShSQ 5gvkbkj+goq4IWPdk/gArFThUPvShOPbjLgz6SzqSfG6HX1wk2u0iMcIsHOgW6+ruRzo 0Qfvl0jRcwj7OySz38huszljizGxGHwaruYW8C58IVdJ11mMHL11u6I9ZqB4rRgYKrFa Zm/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778084660; x=1778689460; 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=93OQr3bmhiR/HStReHr/b9EA87qTqN9YZsE+Bz35FIY=; b=sXKG88jT8lERBtnP/EbNY0UxMWeDxNUk2KlPSaz7d6b0fEY4KMHha2nugExHq6HBVI dVWzHqEcBj9XDAt88sdwFEHAdJzR/4ON4yNYJ0TjwVWufQbEYfqZMvvWo5GF67gW0kMN wsvOLruDgR8ilYJZDaKRG0Bx0zIGNX2RDuOgfhqARw5S+jt9j9jL7ZHRZk5Ed3PwShPZ afaVwq/eKpiNiByqv7PlAWSGdMP+Cc67VAPgH2usDk+cxew6Nzf8o2H8M9MDhanhNuQI fzOzMUeIOFIth2iUR8He/McL4c11j5Ci8FBk1TAOjrfEwiGZ9NqNfmtrWNVX6PQY340s Rl0g== X-Forwarded-Encrypted: i=1; AFNElJ+ZCut7aPqrnoIbT0XSh1Ek5JWSH2yPWnE1wkQZTdA+zUO9MiMmBMK7ffpQCRPNbXJ8cd368ktIZAE+Uic=@vger.kernel.org X-Gm-Message-State: AOJu0Yyzi3m2kELuvC1Y0hHrFCv7O2J2yHWGbYJPy9iPpAfWya6cyKpz 6DWcINDmW0vaFxXhNlejjGb3sIJcu05qu1Z2XuRVikfUMy5d7p0kdFGI X-Gm-Gg: AeBDiev/WvIa50XdksKMZDYVGeyMUgQXe6JitNgwsJudZKOma3exQ8jdzZfxve6HHk9 jKS/BmXORw9RmMXQeOUoWXjsH7Au1KBZTquxQW3tCLaQz9yCJecIUCkKY+OTWu9d3Ji43ajXx/y Vy820CH5jTx53WTUfim9hKkefYnQoSszfz781d9mXqyfau8tYD2EVa5Nn+nH0WYTcviP3SFGmNq zzklXEsTX2xiKdLJDd2gkQL99zz3nGHvXNbTxUJ0UiO0PDNRUTI6qpcr0m3eUzW4Jt825PNYwEF aIm4iLOEURvXUn9FU/yeAr4KM7z20Prifk5/cd0zwB18GySOeadp0XKceA5+S1yUat//5i0Hpaq sbso7KwVqVXIU/7PP2IcaKSBxMoBvIfdR3U5HQNpQHLGpUYAjZ9a3Ivq5gKeDaRKRty35zOgTU+ gw6YzARUWjVPKf9YVdjDP/6ENdr1A0SkTl74T5Sl5+nPBj97d7Zd0rOmHu8QrUF9cFCQAj8z+DQ ynNAp1moCBrc5KYsppNK7AYW3Oz1OAUHXB9Ow== X-Received: by 2002:a05:6a00:2e98:b0:82c:e9cc:f61d with SMTP id d2e1a72fcca58-83a5b0dbb65mr3856991b3a.9.1778084659637; Wed, 06 May 2026 09:24:19 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8629sm5980337b3a.36.2026.05.06.09.24.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 09:24: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 v8 3/4] virtio: add noirq system sleep PM infrastructure Date: Thu, 7 May 2026 01:22:53 +0900 Message-Id: <20260506162254.25576-4-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260506162254.25576-1-baver.bae@gmail.com> References: <20260506162254.25576-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() 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_freeze_noirq() and virtio_device_restore_noirq() perform secondary checks as safety nets. 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 | 305 +++++++++++++++++++++++++++++++++- include/linux/virtio.h | 42 +++++ include/linux/virtio_config.h | 39 +++++ 3 files changed, 382 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 98f1875f8df1..97a3ed5c2985 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,32 @@ 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; + + /* + * Skip virtio_check_mem_acc_cb() here: it may sleep (e.g. Xen's + * xen_virtio_restricted_mem_acc() calls devm_kzalloc with GFP_KERNEL). + * The check was already performed during probe in virtio_features_ok(); + * features cannot change across suspend/resume so the constraint is + * still satisfied. + */ + + 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 +319,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 +613,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_state =3D VIRTIO_NOIRQ_NONE; =20 INIT_LIST_HEAD(&dev->vqs); spin_lock_init(&dev->vqs_list_lock); @@ -618,7 +693,63 @@ 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 +static inline bool virtio_has_valid_pm_cbs(struct virtio_driver *drv) +{ + /* Each callback pair must be fully implemented or fully absent. */ + bool has_freeze =3D drv->freeze; + bool has_restore =3D drv->restore; + bool has_freeze_noirq =3D drv->freeze_noirq; + bool has_restore_noirq =3D drv->restore_noirq; + + if (has_freeze !=3D has_restore) + return false; + if (has_freeze_noirq !=3D has_restore_noirq) + return false; + return true; +} + int virtio_device_freeze(struct virtio_device *dev) { struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); @@ -627,6 +758,34 @@ 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_state =3D VIRTIO_NOIRQ_NONE; + + /* + * freeze_noirq and restore_noirq must be implemented as a pair. + * freeze_noirq performs full device teardown that only + * restore_noirq knows how to undo, and restore_noirq assumes + * freeze_noirq prepared the device for re-initialization. + */ + if (drv && !virtio_has_valid_pm_cbs(drv)) { + dev_warn(&dev->dev, + "freeze/restore and freeze_noirq/restore_noirq must each be paired\n"); + virtio_config_core_enable(dev); + return -EINVAL; + } + + /* + * If the driver provides noirq callbacks, verify that the + * transport supports noirq PM. The driver's freeze_noirq or + * restore_noirq may call transport ops (reset, get_status, + * set_status) that could wait for an interrupt that will never + * arrive if the transport is not noirq-safe. + */ + 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 +804,42 @@ 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 and the noirq phase was + * actually entered (freeze_noirq ran), but restore_noirq 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_state =3D=3D VIRTIO_NOIRQ_ENTERED) { + ret =3D -EIO; goto err; + } =20 - if (!drv) - return 0; + /* + * Re-initialization is needed only for drivers that do not + * implement restore_noirq. When restore_noirq exists, either: + * - NOIRQ_NONE: noirq phase was never entered, so no noirq-specific + * teardown occurred and the device is still live. + * - NOIRQ_RESTORED: noirq phase already performed reinit. + * (NOIRQ_ENTERED is caught above as -EIO.) + * + * Note: when a driver implements restore_noirq, freeze() must NOT + * perform full device teardown (e.g., must not destroy virtqueues). + * Full teardown is deferred to freeze_noirq(). If suspend is aborted + * before the noirq phase, restore() is called to undo only the + * partial quiesce performed by freeze(), with the device still live + * and in DRIVER_OK state. + */ + if (!drv || !drv->restore_noirq) { + 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 +860,114 @@ 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; + + /* + * Pairing is validated in virtio_device_freeze(); since both + * callbacks must co-exist, checking one implies the other. + * + * Verify the transport supports noirq PM. This should normally + * have been caught at freeze time, but guard here as well. + */ + if (drv->freeze_noirq && !dev->config->noirq_safe) { + dev_warn(&dev->dev, + "transport does not support noirq PM\n"); + return -EOPNOTSUPP; + } + + /* + * If the driver provides noirq callbacks 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->freeze_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; + } + + /* + * Invoke the driver's freeze_noirq callback and mark noirq + * phase entered on success. Pairing is enforced in + * virtio_device_freeze(), so restore_noirq also exists. + * + * If freeze_noirq fails, the driver must have rolled back to + * the pre-call state (per kernel PM convention), so + * noirq_state remains NONE to allow restore() to proceed. + */ + if (drv->freeze_noirq) { + int ret =3D drv->freeze_noirq(dev); + + if (!ret) + dev->noirq_state =3D VIRTIO_NOIRQ_ENTERED; + + return ret; + } + + 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_state =3D VIRTIO_NOIRQ_RESTORED; + + /* 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..937bc3c56bb8 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -143,6 +143,18 @@ struct virtio_admin_cmd { int ret; }; =20 +/** + * enum virtio_noirq_state - tracks noirq PM phase progress + * @VIRTIO_NOIRQ_NONE: noirq phase was not entered (only freeze ran) + * @VIRTIO_NOIRQ_ENTERED: freeze_noirq ran; restore_noirq is expected + * @VIRTIO_NOIRQ_RESTORED: restore_noirq completed successfully + */ +enum virtio_noirq_state { + VIRTIO_NOIRQ_NONE, + VIRTIO_NOIRQ_ENTERED, + VIRTIO_NOIRQ_RESTORED, +}; + /** * struct virtio_device - representation of a device using virtio * @index: unique position on the virtio bus @@ -151,6 +163,7 @@ struct virtio_admin_cmd { * @config_driver_disabled: configuration change reporting disabled by * a driver * @config_change_pending: configuration change reported while disabled + * @noirq_state: tracks noirq PM phase progress for restore coordination * @config_lock: protects configuration change reporting * @vqs_list_lock: protects @vqs. * @dev: underlying device. @@ -171,6 +184,7 @@ struct virtio_device { bool config_core_enabled; bool config_driver_disabled; bool config_change_pending; + enum virtio_noirq_state noirq_state; spinlock_t config_lock; spinlock_t vqs_list_lock; struct device dev; @@ -209,8 +223,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 +255,28 @@ 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. + * When the noirq phase was entirely skipped (e.g. suspend aborted befo= re + * suspend_noirq), core skips reinitialization for drivers that impleme= nt + * @restore_noirq and calls @restore (if provided) to undo the freeze() + * quiesce. Drivers without @restore_noirq follow the normal reinit + + * restore path. + * @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 + * (noirq_state =3D=3D VIRTIO_NOIRQ_ENTERED, meaning freeze_noirq ran b= ut + * restore_noirq did not complete) and returns -EIO instead of attempti= ng + * 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 +298,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 Sat Jun 13 14:47:56 2026 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.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 3368548C8A5 for ; Wed, 6 May 2026 16:24:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084664; cv=none; b=E3a0c46Dr48DI8zIjTgZuFY8mLdsvX8vZDrQqWHWHbtHi2sn867GIKy8hPUBsb28aHFp6/0kcfixzyaHq2I/EsM8WTmh9ToOV3Mi7ZasDFOjjgTeHuGFakTyBjUernZvLdda56r3s83L+TRZ2Qbw1fpkt+CVTEfosGv1ZCfIu6c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778084664; c=relaxed/simple; bh=IolL4RY97C9MSpLuk3s2ulTCNVbomPxJq0SB6QX/VVE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rAM/myxgMg+rmBM98UPfiN/zE/tGoHh/pHol/ssj+OJ/mfWZ7pdcKfoUaI6HDREguYN6RWRFHvbEX9wDwGjPgA0qJNZy7rjlYTX3zs4kDAQKujCNvB4aqcLEZP8dhvEplZAk0Q46LEEebOaqZVbV2jbBF+HY3Qcip+fKkZUNdwk= 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=m02nBy6H; arc=none smtp.client-ip=209.85.210.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="m02nBy6H" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-82f8b60e485so3200686b3a.0 for ; Wed, 06 May 2026 09:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778084662; x=1778689462; 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=GLZ6Q/p8ThasrMA2tuzRiOEHpqfxlyjFwiRgvxlFlJI=; b=m02nBy6H9FHuDpsj6hEjva+fvvJ+x8HKVXbiMhC4fMmjbtKYgBK655dZzsD9kwOQu4 nCqyU7LIVoTh+PsKC7LhzcWbFxqZ2aiRvalgyeJVoeJ3zsr1H2R1XeQ/1Vf47PQg1fP/ g8rBbT95ddyGBcG66k2fyVPGXAT+F5GZH3rKNP2TkKjbufG5LGD7W1q4J4DiehgRsa5Q ypi9gLtWIne4UaTknIZnlDFxfGDYnkxi2LiCXGiDYAbKt6XrkM8ofxQIVKv89cU5vBT8 /3hR/5QG8JmreoqKLsAO70J/2cCkX3eeQBc0qMdWj/rfvs8k5OpcKSCuBSL53CtB3HE7 oIEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778084662; x=1778689462; 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=GLZ6Q/p8ThasrMA2tuzRiOEHpqfxlyjFwiRgvxlFlJI=; b=fMqqBxLbPPiZnMzlcgfncaRFf8/wzr7QaG6HFSkqBQ/mvO1WQ7YeS+rny+4l4V6flq 1ZVdTNJ/WjI/N8PELjztxqpOWAc2tl94KofOzSBA+nelE0IYSLTPJmXJAAeMq68n7ENv 87THApK366Yu3X0o+qsNw9R5SZpn/lqEjGyxVwCCC4Ww7EKtM0XO4zLDSj4IRQC6OPlt QuNhzNz8AAGEH6CSCA+fS/fvIkKNyz6aol5hoW3LxhYbTpc6PT4KsmSrAs8wNS8B7VBJ SnDo1fXZlwLnhbg+zXIh5TbHJZaES2wU3sGfSRumwG5UJgpnTFw0GlBFWQ2+zc5q917e FSgg== X-Forwarded-Encrypted: i=1; AFNElJ/O5JX/SARDjhlasO7Sd2QhSVcwohP6D6pE/850D1fY2gEu1q1tfbP0c9AIX2LdrPfxbUk0RUEXnXESb2M=@vger.kernel.org X-Gm-Message-State: AOJu0YyE0m2ei3biJMaEmKKmJxW0OK1s8qhpzd6QkGtyJj5aAISNy99Q H+ndL0NrWcnMvPPAO72RQI43dE+Yy4gOiEB3yWomfPvErFYshfSaO3lg X-Gm-Gg: AeBDiet7f1SHt8NEnhUrwCJg7eqTacRvzPNM+EwCqgAteOhXqt/KAlQNKYdCquC+fk5 Y1aV3j8kDOElXwskAxXyTPQB1c8QkD2KIurhxHHWO080liQX2JqRi/3veMOpXSO9blRIjHp01YL iChpWvJw3FpOB8WJgH89C2ZtGsSeo/GXE+TwgQ9kJbuPgELU6G7KNrDdusSCWQScZ7JTr1b3gqo JD4kZrlQCDJuMliMMENFSjuRwG69PWQDQanmO5lfU8ll7EoJcMCybNz2QUZsMhcTREcgfS3Kj60 bYEc7JvPRyXaO62+EmkxTrLkzQ6KVpJPd+yDGmmJVBKdXp9x5yJ2v17CST+GQresuDQdSEX/xgz A0PH8pbpg/Dcxmaw8wtNp2Jm7bbqWxpoMCnwwHsYOvQyUEw4Ldpxco3ZEng1ZoR6kCZ2Xrlt3FR B3+p13u+txEfhLwD4uQZybb0mwHczybZ/J+BTzhcXA2bOYWD9kzcss9pUe/fqdATEcnre6ek8ss QWvQc6d8Ki9Knq+dKud9FFiENCJAsDRhVwDPueSrRsI62j3 X-Received: by 2002:a05:6a00:4655:b0:82f:38:a5b2 with SMTP id d2e1a72fcca58-83a5dd5582bmr3835165b3a.40.1778084662312; Wed, 06 May 2026 09:24:22 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-839679c8629sm5980337b3a.36.2026.05.06.09.24.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 May 2026 09:24:21 -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 v8 4/4] virtio-mmio: wire up noirq system sleep PM callbacks Date: Thu, 7 May 2026 01:22:54 +0900 Message-Id: <20260506162254.25576-5-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260506162254.25576-1-baver.bae@gmail.com> References: <20260506162254.25576-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 | 137 +++++++++++++++++++++++++---------- 1 file changed, 97 insertions(+), 40 deletions(-) diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c index 595c2274fbb5..4f4836103b88 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 @@ -546,15 +583,35 @@ static int virtio_mmio_freeze(struct device *dev) static int virtio_mmio_restore(struct device *dev) { struct virtio_mmio_device *vm_dev =3D dev_get_drvdata(dev); + struct virtio_driver *drv =3D drv_to_virtio(vm_dev->vdev.dev.driver); =20 - if (vm_dev->version =3D=3D 1) + if (vm_dev->version =3D=3D 1 && (!drv || !drv->restore_noirq)) 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