From nobody Wed Jun 17 01:32:59 2026 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 3B0133C9ECF for ; Tue, 28 Apr 2026 15:08:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388892; cv=none; b=i5GEpV9Lz/x2A8lLyIuDsMJJ+6Qx7Y6WmlG6J2ZHunt+OGJplX9ooYyL9+cuFCPfaK1dTp5DS7yOyb/nlR1+WLnL5dsDsv/CK56iS2VYKb6Okygazgy+SK4Q6Sn2zatYlv1xj2a4MR9MYik/d7HhWA+8tomtKebDVRjFQcAXMd0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388892; c=relaxed/simple; bh=ElyyymLN3BzEU2n3paseyXgS1oFdDA7FSr0KdZ24I/8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bfwEtcfFwY9FGbuRjWbrmluvEJhB/f0LdOhaqLbFeHRwyQ+zhBt/BpJPsWxT+qWi2rgB+YYihLmetlMnszL7UcPfQSme1Dh/nAUNGxV+b19ERgAKlWpnxpxEFcJjbZDBUTAfOy+ySwP4ryMeiEUhYEqIbN01RBX2tnagjGYXX4U= 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=C7Ys/wOh; arc=none smtp.client-ip=209.85.216.51 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="C7Ys/wOh" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-35da9692ec3so9715342a91.1 for ; Tue, 28 Apr 2026 08:08:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777388890; x=1777993690; 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=C7Ys/wOhA3XXdikR7lrjjTgInVnwbkeaFp1kEdSgNNq1YEFIHqLCYHcsMxQvFPYD5M bAFJsqt9QvP+fnDWuFtMG8sQyCy5bTKecE7IZ9vI3diGVQ+Zap3tow/9QbLpkVG7xdx8 SclOuPyrBOOEaJkehUhcLZPLDBM/fTnn04O9ILh3ElzW9SeyFrejYE1/9E4flsxbXhdT jmS9cFvh5t0qAbQVmlAaKCBrO4dBoAqvSAf3JXv0heOnbBHWeekwhySXVQbwLcRhKjlM nvmKb5j8SAAzu0mjs2Dd+rGDB3s2ZPtgg9/8OXTDEajDSkHtLiK9cc0NgwCWwCEsalon xIkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777388890; x=1777993690; 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=BE5dsWls3COARAQcxp4OOpUxCUpr/u73apAULKjHLSkIgnbeLSSl5ymtVctA2TE7Oj sghpzsYzPdhjlGaPkhvpwfDsXwnpsMnesvtahZDQGSx0uFWlqQeCXCKb+h6YoVOQCJrP VGbd38nMge38pnNFTXgrFoBbffW5uVShkmyWAfr1vhcggyxs4vS98YJORpjuS5xGsV3i R7JVIc6l/Nhv8MVv2fuzBZvDdqJVEVTHmZ62qGuPd0bbYXogP35neeJQgsiC3SVeua/4 N5SNBqII9N8KhqxX5ZAJT2U5cwCBKK734IQzz2MVgtoiQYNFLV5ZZVApEYVm42FE2Fie +ogA== X-Forwarded-Encrypted: i=1; AFNElJ/8pjICaOOhnEsDS3dezW6xCIXxw5bcy0uEbdgt2gti7tlOnEDqp02NcMcCtkIJd6MbsASQ6IXgiq5rIi0=@vger.kernel.org X-Gm-Message-State: AOJu0Yzyic3qaryuQg6xPgTiUSCKqN/6Dfccx4jZioyrkmRq7NiX15Gj dgYTjubGPoeJaO5CzSy+xAJvPUeuZf2xjmxGD5+bY5ICUZwAcnMOSUno X-Gm-Gg: AeBDievikKA7+twDFzo4UAr4UUn1vV6lFIoh6Xm8+k6Xwop1S1ezAtUqo4i7u9x95Ir +7zcZFSKKq1o5mSDid3UCpRVTphl+/s9wa8GcyfuKiqwuCVXfEDzGJJ5qi8lLU//SVW7SlOCHI1 Nzl7jrdYqJbA28Qjjfj4T/tbz4uaM0U4TsAq4FKY8gQRsUjvjQ+46d0+BmwvR8b+0NJJ2j43Lwp LpxQ75fGFJsalCYNHrAWHygk8iA6iEqnSX+sRGUOO0M1OeTQ+9bxFDdIR9HIfrTiXp3sdweqlLh gZbmX+akbsFyKRI7UH3Wf9hBFXg3SazH0m17ShZ3lVbuua3POAG2j1SrocVDJwwEFiDSQ/pyJFU ytLkXMh/TQKIfzk6jKOZNkohK6Vlr617TvRSh1rDU7hb6QiXB2XZ9HnTku+lZmvgswTv82DD9CJ Xy3l4x0k5ozK5GbBAQGDam9s8YV27xUVTrv8eL0NwVRc8fjMxr8o9aQsIbkAiaFBvuO2Eptz79W qIvteMVEOFcCrIEw/XtVE6aom3wlLBn2cNLfQ== X-Received: by 2002:a17:90b:5148:b0:359:fe72:3559 with SMTP id 98e67ed59e1d1-3649202b5aemr4084196a91.21.1777388890159; Tue, 28 Apr 2026 08:08:10 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36490bd66f6sm1606127a91.3.2026.04.28.08.08.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 08:08:09 -0700 (PDT) From: Sungho Bae To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Sungho Bae Subject: [RFC PATCH v7 1/4] virtio: separate PM restore and reset_done paths Date: Wed, 29 Apr 2026 00:07:39 +0900 Message-Id: <20260428150742.23999-2-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260428150742.23999-1-baver.bae@gmail.com> References: <20260428150742.23999-1-baver.bae@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sungho Bae Refactor virtio_device_restore_priv() by extracting the common device re-initialization sequence into virtio_device_reinit(). This helper performs the full bring-up sequence: reset, status acknowledgment, feature finalization, and feature negotiation. virtio_device_restore() and virtio_device_reset_done() now each call virtio_device_reinit() directly instead of going through a boolean- dispatched wrapper. This makes each path independently readable and extensible without further complicating the dispatch logic. A follow-up series will add noirq PM callbacks that only affect the restore path; having the two paths separated avoids adding more conditionals to a shared function. No functional change. Signed-off-by: Sungho Bae --- drivers/virtio/virtio.c | 81 +++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 5bdc6b82b30b..98f1875f8df1 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -588,7 +588,7 @@ void unregister_virtio_device(struct virtio_device *dev) } EXPORT_SYMBOL_GPL(unregister_virtio_device); =20 -static int virtio_device_restore_priv(struct virtio_device *dev, bool rest= ore) +static int virtio_device_reinit(struct virtio_device *dev) { struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); int ret; @@ -613,35 +613,9 @@ static int virtio_device_restore_priv(struct virtio_de= vice *dev, bool restore) =20 ret =3D dev->config->finalize_features(dev); if (ret) - goto err; - - ret =3D virtio_features_ok(dev); - if (ret) - goto err; - - if (restore) { - if (drv->restore) { - ret =3D drv->restore(dev); - if (ret) - goto err; - } - } else { - ret =3D drv->reset_done(dev); - if (ret) - goto err; - } - - /* If restore didn't do it, mark device DRIVER_OK ourselves. */ - if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) - virtio_device_ready(dev); - - virtio_config_core_enable(dev); - - return 0; + return ret; =20 -err: - virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); - return ret; + return virtio_features_ok(dev); } =20 #ifdef CONFIG_PM_SLEEP @@ -668,7 +642,33 @@ EXPORT_SYMBOL_GPL(virtio_device_freeze); =20 int virtio_device_restore(struct virtio_device *dev) { - return virtio_device_restore_priv(dev, true); + struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); + int ret; + + ret =3D virtio_device_reinit(dev); + if (ret) + goto err; + + if (!drv) + return 0; + + if (drv->restore) { + ret =3D drv->restore(dev); + if (ret) + goto err; + } + + /* If restore didn't do it, mark device DRIVER_OK ourselves. */ + if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) + virtio_device_ready(dev); + + virtio_config_core_enable(dev); + + return 0; + +err: + virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); + return ret; } EXPORT_SYMBOL_GPL(virtio_device_restore); #endif @@ -698,11 +698,30 @@ EXPORT_SYMBOL_GPL(virtio_device_reset_prepare); int virtio_device_reset_done(struct virtio_device *dev) { struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); + int ret; =20 if (!drv || !drv->reset_done) return -EOPNOTSUPP; =20 - return virtio_device_restore_priv(dev, false); + ret =3D virtio_device_reinit(dev); + if (ret) + goto err; + + ret =3D drv->reset_done(dev); + if (ret) + goto err; + + /* If reset_done didn't do it, mark device DRIVER_OK ourselves. */ + if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) + virtio_device_ready(dev); + + virtio_config_core_enable(dev); + + return 0; + +err: + virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); + return ret; } EXPORT_SYMBOL_GPL(virtio_device_reset_done); =20 --=20 2.43.0 From nobody Wed Jun 17 01:32:59 2026 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) (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 F06CA42983E for ; Tue, 28 Apr 2026 15:08:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388896; cv=none; b=p4l8YcXh1445aplkB8LDZ5tctkRaHqRWYOjxyexG0w63bTAEDx55cLR9SxLMrgyf6gVS9WLjO2/urw9+M4Cg5XDpWfErYTq5JNyShjjAhlLLzyIbA0+JZqDWAcoxxywCWgqeuFNtbwFXK9XO8CGQzIoDeN/WSVVv0VAvfJ2OA/0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388896; c=relaxed/simple; bh=MWBv1DHfHJ5MORln2uC6zteVz62qSKn6Mmg1BPVPwwk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k8mGv1GzYVyOdL1RhkCTu0Qxb6HlRlAqSy5kQT3ZOiqlU3AbVlecr1XnivsJoCxjl9KNnoSorrgs8R1fH0KdTe7hV8BWcvOAeG/QOXRoO6mHChIKpEr/JPuFopUT2X/AiX2HPWzvJNptGZRY36Uk5gAAVWt17ZoLMtqE+I7hnqc= 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=YMsPIICU; arc=none smtp.client-ip=209.85.216.41 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="YMsPIICU" Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-3591cc98871so5327348a91.3 for ; Tue, 28 Apr 2026 08:08:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777388893; x=1777993693; 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=YMsPIICU32dADgBVJdBzluZTSM8h3YwmFd/ACYDGvzuo9ElLlMLe1N3L52FbFPneqS nabf7UFky7/uFQZvNvKGuqw/ZE+5PF50wxLLWUbBm2xFyMCM4tCLkcYFEPu3CRLQQOHM DgcAArnIFfAEGiMIjgfUW2FoVurv+19faIWVgjb7RbmZgbhRWx8h+H/ogBfvJ2ZDpAIx R6JLkkH/hU6aFgjMpr/Kq3xC1/EfG52SGS6HetwdlUvOj8Vtn3bnZfNJPMW8jqi2qnAs 7Qskav4tFoNTZBONw1bglLbuQfZ1VRfP8wJC2I+yzNuR4ghmedd+Hv+ajhcfDEl/rEmX sR5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777388893; x=1777993693; 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=p2+kHJ0KIuuHOQ4lUCMFbaVhJAUBhTe5fR+7pgLEqXVVA62xwkaACoMtqVjCNzV0l6 56Ve3OIo7+OOe10S3lT+lnXCUa5ka94DHRIWR+0S56iVv97xNBExVRsjyX8xvy8ghEiu FYiF41A8/0WbixXl6sSHx5Dz5/e8HT8LvQx0iN4q/IAIWaCWtIoR5QLodO6LuVPU2lqj LMtbILWpc3lxMGQRTrWY7WN5bx5wg1Pp0PKm13R7t8P3FeQbMlDjrj7qk4Px6F2OmpOc naeKKmW1e1A04pLy1C3qC41aXjMFymn+TKKiP7qQ9uWI/3idJFtFZocyJLt8y61HzRVT nsNw== X-Forwarded-Encrypted: i=1; AFNElJ/RXQHvxcUzhwsW3QGwkP9YqxrPrZBfn7NWSOrkBIBblUwklgM1Ksc9+kZiTLkpAGMnPaV/zsCOD7DCw4Y=@vger.kernel.org X-Gm-Message-State: AOJu0Yzkq3k6M8nlZCYZTGN38101N2VUSJl8rZT27UKrNT8g8AOicLo/ ap6uetK8WZ0fp7t8P/hPMtcljBy5VUYK+9LKsQPFqxk1x9ckeD8JTBhr X-Gm-Gg: AeBDiet2LMg/+cTCXijUHCPDJj4tA4IyxQHW4pRjEQ6nvmQUdW2Cv/Xhq0DDG/AZJVd KuIuqO0NFIq6Z8e1x+Pmsyqp4EumhBA1a5oP1SdMG29UwRYgxC39lnb3wMx0GJ/sg1lnbYQ9t83 QGFzf2Pmy9biiYHiCoiKSYaRkmVWXv4tW9GrLsZ5qBdyxQ/XhviIuqpi+5OaUI/mCenCTGl8poT FoL/tFFx5tzqN0dXOlI25vcjszmbbv3Vr4p/bZZVVnTZPEg4JzJF7mXqrUDv+oZt5uLF1F/Aprq Cyf2SdDDkKFrbgQ50u0t9c6X+AoVxD4hLR8IvjP0HC1Jzn+ebSBiC8Ten5xshzargB5VBpJ8e20 yJppsJt7/2yfQkiZPrzw7WMdoScMA8o5ZakRt0Jm5TdgEPAi3FMefUk1+NNOFT06ns3+WOfam8j +LKps+q5SUZ5cVQpgkc5gBXYkh0tdzUCJ8eohmXvbhBXUFsspHg4kZlnzdPAQnMz21JXW+1NEWF SNCn8dTF6++pAZ323mM32ARUo6Iku0dnYQw7A== X-Received: by 2002:a17:90b:3bcd:b0:35f:b5df:449 with SMTP id 98e67ed59e1d1-3649204f819mr3632492a91.21.1777388892918; Tue, 28 Apr 2026 08:08:12 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36490bd66f6sm1606127a91.3.2026.04.28.08.08.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 08:08:12 -0700 (PDT) From: Sungho Bae To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Sungho Bae Subject: [RFC PATCH v7 2/4] virtio_ring: export virtqueue_reinit_vring() for noirq restore Date: Wed, 29 Apr 2026 00:07:40 +0900 Message-Id: <20260428150742.23999-3-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260428150742.23999-1-baver.bae@gmail.com> References: <20260428150742.23999-1-baver.bae@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sungho Bae After a device reset in noirq context the existing vrings must be re-initialized without any memory allocation, because GFP_KERNEL is not available. The internal helpers virtqueue_reset_split() and virtqueue_reset_packed() already reset vring indices and descriptor state in place. Add a thin exported wrapper, virtqueue_reinit_vring(), that dispatches to the appropriate helper based on the ring layout. This will be used by a subsequent patch that adds noirq system-sleep PM callbacks for virtio-mmio. Signed-off-by: Sungho Bae --- drivers/virtio/virtio_ring.c | 58 ++++++++++++++++++++++++++++++++++++ include/linux/virtio_ring.h | 3 ++ 2 files changed, 61 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index fbca7ce1c6bf..d3339b820f6b 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -506,6 +506,15 @@ static void virtqueue_init(struct vring_virtqueue *vq,= u32 num) vq->event_triggered =3D false; vq->num_added =3D 0; =20 + /* + * Keep IN_ORDER state aligned with a freshly initialized/reset queue. + * For packed IN_ORDER, free_head is unused but harmlessly reset. + */ + if (virtqueue_is_in_order(vq)) { + vq->free_head =3D 0; + vq->batch_last.id =3D UINT_MAX; + } + #ifdef DEBUG vq->in_use =3D false; vq->last_add_time_valid =3D false; @@ -3936,5 +3945,54 @@ void virtqueue_map_sync_single_range_for_device(cons= t struct virtqueue *_vq, } EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_device); =20 +/** + * virtqueue_reinit_vring - reinitialize vring state without reallocation + * @_vq: the virtqueue + * + * Reset the avail/used indices and descriptor state of an existing + * virtqueue so it can be reused after a device reset. No memory is + * allocated or freed, making this safe for use in noirq context. + * + * Preconditions for callers: + * 1) The vq must be fully quiesced (no concurrent add/get/kick/IRQ callba= ck). + * 2) Transport/device side must already have stopped/reset this queue. + * 3) All in-flight buffers must already be completed or detached. + * + * If called with outstanding descriptors, free-list state can be corrupte= d: + * num_free is restored to full capacity while desc_extra next-chain/free_= head + * may still represent a partially consumed list. + * + * Return: + * 0 on success, or -EBUSY if preconditions are not met. + */ +int virtqueue_reinit_vring(struct virtqueue *_vq) +{ + struct vring_virtqueue *vq =3D to_vvq(_vq); + unsigned int num =3D virtqueue_is_packed(vq) ? + vq->packed.vring.num : vq->split.vring.num; + + /* All in-flight descriptors must be completed or detached */ + if (WARN_ON(vq->vq.num_free !=3D num)) + return -EBUSY; + + if (virtqueue_is_packed(vq)) { + virtqueue_reset_packed(vq); + } else { + /* + * Split queue shadow index should match the visible avail + * index when the queue is fully quiesced. + */ + if (WARN_ON(vq->split.avail_idx_shadow !=3D + virtio16_to_cpu(vq->vq.vdev, + vq->split.vring.avail->idx))) + return -EBUSY; + + virtqueue_reset_split(vq); + } + + return 0; +} +EXPORT_SYMBOL_GPL(virtqueue_reinit_vring); + MODULE_DESCRIPTION("Virtio ring implementation"); MODULE_LICENSE("GPL"); diff --git a/include/linux/virtio_ring.h b/include/linux/virtio_ring.h index c97a12c1cda3..8b421fef4fef 100644 --- a/include/linux/virtio_ring.h +++ b/include/linux/virtio_ring.h @@ -118,6 +118,9 @@ void vring_del_virtqueue(struct virtqueue *vq); /* Filter out transport-specific feature bits. */ void vring_transport_features(struct virtio_device *vdev); =20 +/* Reinitialize a virtqueue without reallocation (safe in noirq context) */ +int virtqueue_reinit_vring(struct virtqueue *_vq); + irqreturn_t vring_interrupt(int irq, void *_vq); =20 u32 vring_notification_data(struct virtqueue *_vq); --=20 2.43.0 From nobody Wed Jun 17 01:32:59 2026 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 A16CD3C6A56 for ; Tue, 28 Apr 2026 15:08:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388898; cv=none; b=sqeJKNYqdv2ZT9C36SNkE0VwmLwPf+G2aJJpcDTedC+WR7tubQotVkJFyR1hFT8GXbGazcN2pkB5+GZ8jTq49z09gSE+VWf7rHjl6qS7lHJ9bgJpPPCcNf7AyQKZCj9U1iSNCmIUTpb1An0UmlxD5nR+Yo4Sz5OyrY+LLF2krGU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388898; c=relaxed/simple; bh=kUL17ow9gatkJ5Q2fTN0VXTszcyTDQg4H2ZFw0+QBWA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ejiJSXFNicXCRwE5UHwboq1aXmG0Qj//zFypWTjcp7bgbANy6asmGbvCWJhMrPbij+O/OQIpZKaM79ifbcFsc1SFgbXsfwnmoxZx7zy37/xP1jA2bJSQ/AWc2fjQwcR2vSTd/GbjgqP7a237hzuhfZmf5OyYUaeuEjRNNjriB5c= 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=mJnP0bAx; arc=none smtp.client-ip=209.85.216.47 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="mJnP0bAx" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-35fb0bb27e7so7726320a91.1 for ; Tue, 28 Apr 2026 08:08:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777388896; x=1777993696; 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=tsLVv1y3HuEkiecFQkNxqEahLsQhYdFU08TfdQwzyxY=; b=mJnP0bAxxmiTb5B5dDaWM8y11Q2acJvqwRya+3JbZT9UoG17z9m4J4QZFqxTt1MkKh mpBF/8jLPSr3fb0khNwjn5DvjJ9DLOYhYRNRMnk5P2aLHCNFy3Umr1HRAMvHxR0Ht4U8 if9zOHUfS5tzIHJGNIjCrirCwjkWqJReRXkUCN4zZV4NnvKSxXkerwMJziDObA+PqXc/ u3loiX3HCrG12Ck/jmWrk5HlSsN9GOi09hfq92KBrxgSh/joIEjJhpSMBQw6J5LEpjc8 3QC56dPGATji8M7kGAW6D9Sx+B+EzJcm+Qcdd4eh4asAWdlAwQRkFVD0Z5YVgQibA42s fxNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777388896; x=1777993696; 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=tsLVv1y3HuEkiecFQkNxqEahLsQhYdFU08TfdQwzyxY=; b=slB8koI3blzV2pqnNOdwjJs4imh7V604qEYsgzNO2d4Fgvptbn1jaCqBsudEmu6FAk ar6g4OMhTe1GFX1L/eahmLo/m+um6j918uSzR7dGCd2mM2c2gBXbsZOpYsK40hcYUJKk Ch4tZLIM/NWLEGknMxoyDTifbQpddHNHwSX83S3zkyPe/sSjdt9H+DIvp/0g8Dauo529 KdhJm5ARwvUPFnHLbKOXzngx09FWqLfs9CX08uPSZXZVolTruCe3RNNMJHdlgheEYmaY 5DkuwwTISE//yBWXpM4Lvv433JUoH99hn4Z5O/1BBse7q1LLZhgJ22dXkRshB/YXVIy8 Ic7w== X-Forwarded-Encrypted: i=1; AFNElJ9O4PGbXUs9h0heHt7O1KnTSZP88Ml6IbsP7NdNE/AeebBZVwJfiseoVdaPvqk3ADfHDAns++kxmKL1v5Q=@vger.kernel.org X-Gm-Message-State: AOJu0YwbiaEtIW4g09NItZeKnFL4TXoi8pV+SYzhbLANgUKPgaSqABFb 5e7FM7/Uimx5TDR2VU5Y+UPK59FzmYMqo2A7ZYsuHbqqr0Fs80qiQzyM X-Gm-Gg: AeBDiesSLyOhiRccnaAObMnWJwtZyXa8JFangtYTo6t8SWpTDfsyuAQwiq5FS2o/PJJ J4AwiBJMrDVbpdn0My/iVxyKyP0vfr74uR2qsLJ9M9K2f8+ML0bj0HKIX62HtSiNFz4xIK4ZZJf DF51t/X2C3m8vPRV3wgJZ4nH4tWkTgWXdOC9AWpteNUGI1vuypLwpLhU0rEnSgpbf2PxLhkctYe iG9tsqlBIcmP79EVvBy+GccH6k7BkdvgfWGW2LOQIkSf3XKAAEpZVV5ezzDbevmUc+LPuI59K4U /DgY1UiAHbfFHLH5XWT8TX2UCBaRddngaOtFjbtvoqG8CvOe1iitIr54YYJ1AChx1AhyRJmh86z T8TUrtQpQuoN3eNSeIbk3hGkW4ItNzPwH0IzCqGAq+Hdyel1d326x2uortP0qv7uA6J+sNL/KjE A8bXDKlyPynQOpodZUkJvGYtKc9f4IkrERN7JvcBfwM0rXpb6Yvlhmq5i89SgnfND2Dwx4ILeqc rnXketN0MTMRsY8cg0KX19uIB7jO2ADKa60Gg== X-Received: by 2002:a17:90b:1802:b0:359:15c8:e8e1 with SMTP id 98e67ed59e1d1-36492078018mr3654631a91.25.1777388895658; Tue, 28 Apr 2026 08:08:15 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36490bd66f6sm1606127a91.3.2026.04.28.08.08.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 08:08:15 -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 v7 3/4] virtio: add noirq system sleep PM infrastructure Date: Wed, 29 Apr 2026 00:07:41 +0900 Message-Id: <20260428150742.23999-4-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260428150742.23999-1-baver.bae@gmail.com> References: <20260428150742.23999-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 | 262 +++++++++++++++++++++++++++++++++- include/linux/virtio.h | 42 ++++++ include/linux/virtio_config.h | 39 +++++ 3 files changed, 339 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 98f1875f8df1..50e23f10be40 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_state =3D VIRTIO_NOIRQ_NONE; =20 INIT_LIST_HEAD(&dev->vqs); spin_lock_init(&dev->vqs_list_lock); @@ -618,6 +699,47 @@ static int virtio_device_reinit(struct virtio_device *= dev) return virtio_features_ok(dev); } =20 +/* + * noirq-safe variant of virtio_device_reinit(). + * + * Requires the transport to declare config_ops->noirq_safe, which means + * reset, get_status, set_status, and finalize_features are safe to call + * during the noirq PM phase. + */ +static int virtio_device_reinit_noirq(struct virtio_device *dev) +{ + struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); + int ret; + + /* + * We always start by resetting the device, in case a previous + * driver messed it up. + */ + virtio_reset_device_noirq(dev); + + /* Acknowledge that we've seen the device. */ + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); + + /* + * Maybe driver failed before freeze. + * Restore the failed status, for debugging. + */ + if (dev->failed) + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_FAILED); + + if (!drv) + return 0; + + /* We have a driver! */ + virtio_add_status_noirq(dev, VIRTIO_CONFIG_S_DRIVER); + + ret =3D dev->config->finalize_features(dev); + if (ret) + return ret; + + return virtio_features_ok_noirq(dev); +} + #ifdef CONFIG_PM_SLEEP int virtio_device_freeze(struct virtio_device *dev) { @@ -627,6 +749,20 @@ int virtio_device_freeze(struct virtio_device *dev) virtio_config_core_disable(dev); =20 dev->failed =3D dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED; + dev->noirq_state =3D VIRTIO_NOIRQ_NONE; + + /* + * If the driver provides restore_noirq, verify that the transport + * supports noirq PM. It will fail early so the PM core can abort + * the transition gracefully, rather than silently skipping noirq + * restore and then failing in the normal restore path. + */ + if (drv && drv->restore_noirq && !dev->config->noirq_safe) { + dev_warn(&dev->dev, + "transport does not support noirq PM\n"); + virtio_config_core_enable(dev); + return -EOPNOTSUPP; + } =20 if (drv && drv->freeze) { ret =3D drv->freeze(dev); @@ -645,12 +781,35 @@ 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.) + */ + 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 +830,101 @@ 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; + } + + /* Mark that the noirq phase has been entered. */ + dev->noirq_state =3D VIRTIO_NOIRQ_ENTERED; + + if (drv->freeze_noirq) + return drv->freeze_noirq(dev); + + return 0; +} +EXPORT_SYMBOL_GPL(virtio_device_freeze_noirq); + +int virtio_device_restore_noirq(struct virtio_device *dev) +{ + struct virtio_driver *drv =3D drv_to_virtio(dev->dev.driver); + int ret; + + if (!drv || !drv->restore_noirq) + return 0; + + /* + * All transport ops called below (reset, get_status, set_status) must + * be noirq-safe. Return early if not - this should normally have + * been caught at freeze_noirq time. + */ + if (!dev->config->noirq_safe) { + dev_warn(&dev->dev, + "transport does not support noirq PM; skipping restore\n"); + return -EOPNOTSUPP; + } + + ret =3D virtio_device_reinit_noirq(dev); + if (ret) + goto err; + + if (!list_empty(&dev->vqs)) { + if (!dev->config->reset_vqs) { + ret =3D -EOPNOTSUPP; + goto err; + } + + ret =3D dev->config->reset_vqs(dev); + if (ret) + goto err; + } + + ret =3D drv->restore_noirq(dev); + if (ret) + goto err; + + /* Mark that noirq restore has completed successfully. */ + dev->noirq_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 Wed Jun 17 01:32:59 2026 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 4148343D4F6 for ; Tue, 28 Apr 2026 15:08:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388900; cv=none; b=B1b1PitMZ+6AeOMqtRBqxhE7zy3HDo9TCNLLzn6w66DOIaxbwH4d28x467MZbWWh0c1G54yzD704sjihZ2Q4djBQj1/qAFAhpZ1akmKw9ts7++Qu8YJ0245mmWizgeH/wHNf5GII/DainU8wfgcAEOrxWl0NZZ/lSFaNeSx+WZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777388900; c=relaxed/simple; bh=IolL4RY97C9MSpLuk3s2ulTCNVbomPxJq0SB6QX/VVE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Xy7iYTk8Rue7FP8sS5hOG2s3YqVGmjoXJSw8JOMwNfilde1i4h1LqB0M88R+ln8FrQgrWiFO/xMWCSrhKbPuqht7B95UNS5eIYISBb78MxcG1rF6VJe18NBldjt14Ndm3y0QjHh7+LUXHcD9y+P6fwKKBfOiJO04PKLQzBGEgf4= 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=ZwLMbFlC; arc=none smtp.client-ip=209.85.216.45 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="ZwLMbFlC" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-35d965648a2so10352304a91.0 for ; Tue, 28 Apr 2026 08:08:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777388898; x=1777993698; 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=ZwLMbFlChfK/jh44H+QZt6/NSTtqn3fwtlqW9bMAXKFJqwm3gOy7sn/0rjDQWODPCe zMJE8AaOaP1m/HXWhgYbuekoQm07JTfxmva7pI3GkTl6uEbuNN1KaVaEcVRkC91/4CRx Jn8wf9Q8KipvDoJb2WH86Hmaf5twSNadphZvvoRCM5daJWK93/7QE+iY6QSoyr7qW6Qa pbE7wy70FYhCafy7vFu5FXvcphUPj3eT4xajnJ4+z/PzsPzZG/vdSUjNjeGF5NsCyoXc r8DyIzSiJZMluxh7V1cmuHZdN5YhaXIv8X6tkmTdXbPxL4UC4ZiJj4mAoHFt48s7uHgq HdPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777388898; x=1777993698; 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=PWzFJk9cRWy+EOCyIzLUTVazUItkUBhqlrRalxKHCpJl28ZVNQLRcyDszkNlguPN4f gqbf23gt12l90txoxnjMzr5DZx2g6y4N1AO0LpsbifrrU49hI9iy2DS214uTG+gTdMPC WBbeKnbLS2QwP3Hh0ffE9fBKgWkxZT5AjxnG3nWXxbxHWytJpZchcUpjijT4vIXs8jXc 7bjs5D9Qhehe64RYDvLblAu8CGCzgepjCjJHqsHMGC5o+8QkKh41nAWAdo0yx3ZhAUH1 nNHQ30Na4RNc/IARLtTgu4dkizmNUzH3EFzPuIUuLXWlc4hm9qMUoZeQYEGnL8yC83QN grJA== X-Forwarded-Encrypted: i=1; AFNElJ+6HeZVf+YFK1JIwkLRIlhilHMzhN6yn9oCYEersg+Hl8w0/+Ab5zArTmGzdZmvqtF7yO7SWNiRjlMr14M=@vger.kernel.org X-Gm-Message-State: AOJu0YwgXezcrQKVyQXFr9H1Lzv859GUSNX99muxAw5lQMaxRSj7Y5WZ 0wGPBk547+PejV/lNTp4rx1cEsxd3mBvj28ZlTqQ8sJ39atx/MQCQxop X-Gm-Gg: AeBDieuUt7qzEMrLOIQ+JUHPZVhiN/bNtsPTLkwZaKtiQVUn3SdfzLwkBO+sp6TFCo2 rgLjgiSRc3E/e+U2xQiahupeEocoBVhxzZ4ggV/8p7N6z6ujHXa0HlGP0QXIJWdDs1CHQ7LzHXo qpaZQ5IiJbrumNF3Q7yt0v76vvqG7xveV6FCn/J6mRi9tJuzF/qYS8Mg+Z/Y1Xg9wCKZJFMuN/w dTdvIGPtXlwRJPLSt3IImsSrSh8s+mS3eclzuyJimSUkS+vHN1HZwO138vmiq+QXH2LjBe4vwX8 +J+zsy8c4n+d+DNYHW75FbhsRhZDAGjKo1vM9j6yZDS4WI8ZLAcZUWu4riaKzYk4Vj4hela+22Q jyyNQbDeDx0rjSR0rRaHbxyfzo+HUEkU09l2mdUZgUvsD7sssUzzaAg2sXw3LK8AZE92AN4DuN+ JDJSlHcoCnB/LBYA4AIC8Ikoc8/hpWh/ShZkxoYQBsRbEc8rAY+4r0scJuR54tD7Bdj+5NhAtB+ OI81mUDckRJiLk944fIYADKphajvgsoLv7c0Q== X-Received: by 2002:a17:90a:e7cc:b0:35f:9ab2:a5b4 with SMTP id 98e67ed59e1d1-36491f6cacamr3923091a91.6.1777388898478; Tue, 28 Apr 2026 08:08:18 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-36490bd66f6sm1606127a91.3.2026.04.28.08.08.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Apr 2026 08:08:18 -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 v7 4/4] virtio-mmio: wire up noirq system sleep PM callbacks Date: Wed, 29 Apr 2026 00:07:42 +0900 Message-Id: <20260428150742.23999-5-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260428150742.23999-1-baver.bae@gmail.com> References: <20260428150742.23999-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