From mboxrd@z Thu Jan 1 00:00:00 1970 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: 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