From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 580133E4C89 for ; Mon, 27 Apr 2026 17:32:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311129; cv=none; b=B0H7/4CN7sxTiB8OQfu/I2v7SX0E6cTBdrGrJC7quNU1g8BBwdLq5k5aCFSjjiKGvDJ2Z3YLV4YTJiSMGsFh1iqDKhH483zFL8mlf6MohieJSW+UoQSlnTO/q6YAFDvTlHERftbQmN4R/ttT5NvSnE4qsxpK6gTTXhzAVu9UXS4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777311129; c=relaxed/simple; bh=MWBv1DHfHJ5MORln2uC6zteVz62qSKn6Mmg1BPVPwwk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=e9mGkCcYHYSi/t5hLECcafsQLretcjnR+dna+iJfl/0VDAfa74u7cuRbV8mXLaSKJi0lvmMRGL2fP9NtMSd338uIOU5xhcBe7YukoNbxLpF5+OqxM5znGj5Hfv9OV5liYAqwVAedXv/U1LZKz/vttSUmwRZ5XOLx6cC4gaFgaIU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=axMaK+Ik; arc=none smtp.client-ip=209.85.215.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="axMaK+Ik" Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-c70ea5e9e9dso4673490a12.1 for ; Mon, 27 Apr 2026 10:32:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777311128; x=1777915928; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=r0D2OjrENWJxNTLidObtJYsE1ofQWJtzpBHNBO0DSMM=; b=axMaK+IkBwxPK9YTGutORrC6oxBwFwzW4opBQXK2fQ8k7wyM3E0P4+CkXc1Nnr2JvN F4ewFOY3IVEoXEomuEd0c618teqQFT/Z2KmE/R0f2yKlLT08wUX8xB+NiBcJCA3Ers/6 5kcYxWKtiV0y0ehHRoPDCEsSbOp3FXjrj5xG4pBVwyHQpso85FIhF57dRh0HRfAeUmJ9 KlTzRmJifWDLjyaM9lvYVB7q32sAiuCWoF5ZbRXLkTUnr6+1llXIuBLKo6zUu+ORq9Zh 9aWueXwlDV68Po9QjGmrH8lmWncG9xaH5JPeGqksmr1Gdfz4BQy4p0uepY9eGO3j2mAv 0L2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777311128; x=1777915928; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=r0D2OjrENWJxNTLidObtJYsE1ofQWJtzpBHNBO0DSMM=; b=O2VLJveM6dgTswnlYeJRDoKRaaYqw5V+7wFin9/eu9ZtnuryD4Sazp40nuHIsp4JP+ n37vlF2FGemehoe+M+yFn7AsffWQTjOut5VazzPSLlgbr7aEkhPgf15wFUFTIEX0CzdP qUZdchb2+uKCGW7SbpoRbdPmqZF78p3ZZ8KwlwumJPcU/yLdVc4xi3e1/gVhFZqz3ClK 5l0dbOodIOjLlgP8nIGl+vkQuM4AvUQYFGL5ycOp42Yq2UNuEis1LGOKbsnUkTe6eJPs ykDpQh3n+4TB1skH7H1S2MR1UndeiuchgnHV4Tl2X+neX7hinmWc7ifu2pLTuSfhBRox 1+IA== X-Forwarded-Encrypted: i=1; AFNElJ8dww9LbrUfvQfgHj5YOZ07eRm6v33Sak7TKyMS/ySlZfbFucpJBCx56VfOjFSTWSHWu3BPNE/vfPPiqmg=@vger.kernel.org X-Gm-Message-State: AOJu0YxnJDHGzKQ2FKzkKE6koynwQWeBZjvZjGYqlSumLu9HzhOaal6m ey7ZQZNlZHa2ETlVXQ6sxf1Us4TPvBNdpui+kKewAXnZt6ntBS9sjFIz X-Gm-Gg: AeBDietSqIFpu7K/lvBY4cS6BiNlTZobWyhCjVOD+Yr4PwKxSpMFF0NAok21PQS4Ycx UeBKeL1wwQAsvKrip1BYSRisLvjB6d7Wx7ZlSql8B3tEC2Ih0764+Y1yTnd/uc0IzSO9XxNamtw FSEv0hWvLGJbqQt22q1KIjhwzNNudGbYMBCZgfT/Ujix526QFuKSMFhdyxX2hEBHqwKyF60ZJyy 4pRfGWW5u9BhdcObDrGFNOOpRuRaIZ8DvNq0m5pcoAuRV6uWWGZHXSIn6iB18ANlNRhihZFkfu9 HUBsWYaEHejRnVZIcVD5JIUatTIZl1zessvtC4JUTjDDGnG2TpTJi0A9nep5B5fy3L2sLuhYXqe crBhzo5stKDGafajvFVAlt60DSy5ZtIUUHvWe4aW6lrOWwlY0GXYK5NLJ7YG/g7h1plkvrmbW9v fMdhwa8aDxIPbZ7TFbLsUzbSyWUrmLrB+btE0wvt3+tTvY8ink1Vr/DKuW+T/Q+pPfYEXfoUMyZ edm8uYmWNEgt9X+DvpaCvqbiUQSbny9B4CIIA== X-Received: by 2002:a05:6a20:394a:b0:3a2:d68d:9e7e with SMTP id adf61e73a8af0-3a398e542acmr321376637.41.1777311127480; Mon, 27 Apr 2026 10:32:07 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c7977031729sm26246246a12.25.2026.04.27.10.32.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 10:32:06 -0700 (PDT) From: Sungho Bae To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Sungho Bae Subject: [RFC PATCH v6 2/4] virtio_ring: export virtqueue_reinit_vring() for noirq restore Date: Tue, 28 Apr 2026 02:30:43 +0900 Message-Id: <20260427173045.28652-3-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260427173045.28652-1-baver.bae@gmail.com> References: <20260427173045.28652-1-baver.bae@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 = false; vq->num_added = 0; + /* + * 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 = 0; + vq->batch_last.id = UINT_MAX; + } + #ifdef DEBUG vq->in_use = false; vq->last_add_time_valid = false; @@ -3936,5 +3945,54 @@ void virtqueue_map_sync_single_range_for_device(const struct virtqueue *_vq, } EXPORT_SYMBOL_GPL(virtqueue_map_sync_single_range_for_device); +/** + * 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 callback). + * 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 corrupted: + * 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 = to_vvq(_vq); + unsigned int num = 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 != 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 != + 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); +/* Reinitialize a virtqueue without reallocation (safe in noirq context) */ +int virtqueue_reinit_vring(struct virtqueue *_vq); + irqreturn_t vring_interrupt(int irq, void *_vq); u32 vring_notification_data(struct virtqueue *_vq); -- 2.43.0