🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #13109

[PATCH 1/3] virtio: add virtqueue_get_last_used_idx() helper

#13109
From: Longjun Tang
Date: Mon, 09 Nov 2026 09:43
76 lines
11309 bytes
From: Longjun Tang <tanglongjun@kylinos.cn>

Export a read-only accessor for the last consumed used ring index.
It's useful for drivers that need to detect whether the device has
produced buffers that have not been consumed yet, by passing the
returned value to virtqueue_poll().

The split ring now writes the field with WRITE_ONCE() (the packed
ring already did) to document that the new reader can run concurrently
with the poll path.

Signed-off-by: Longjun Tang <tanglongjun@kylinos.cn>
---
 drivers/virtio/virtio_ring.c | 18 ++++++++++++++++--
 include/linux/virtio.h       |  2 ++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index db678f5a80e0..6d8485c73881 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1001,7 +1001,7 @@ static void *virtqueue_get_buf_ctx_split(struct vring_virtqueue *vq,
 	/* detach_buf_split clears data, so grab it now. */
 	ret = vq->split.desc_state[i].data;
 	detach_buf_split(vq, i, ctx);
-	vq->last_used_idx++;
+	WRITE_ONCE(vq->last_used_idx, vq->last_used_idx + 1);
 	/* If we expect an interrupt for the next entry, tell host
 	 * by writing event index and flush out the write before
 	 * the read in the next get_buf call. */
@@ -1068,7 +1068,7 @@ static void *virtqueue_get_buf_ctx_split_in_order(struct vring_virtqueue *vq,
 	ret = vq->split.desc_state[last_used].data;
 	detach_buf_split_in_order(vq, last_used, ctx);
 
-	vq->last_used_idx++;
+	WRITE_ONCE(vq->last_used_idx, vq->last_used_idx + 1);
 	vq->last_used += (vq->vq.num_free - num_free);
 	/* If we expect an interrupt for the next entry, tell host
 	 * by writing event index and flush out the write before
@@ -3227,6 +3227,20 @@ bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
 }
 EXPORT_SYMBOL_GPL(virtqueue_poll);
 
+/**
+ * virtqueue_get_last_used_idx - get the last consumed used ring index
+ * @_vq: the struct virtqueue we're talking about.
+ *
+ * Returns the index of the most recently consumed used buffer.
+ */
+unsigned int virtqueue_get_last_used_idx(const struct virtqueue *_vq)
+{
+	const struct vring_virtqueue *vq = to_vvq(_vq);
+
+	return READ_ONCE(vq->last_used_idx);
+}
+EXPORT_SYMBOL_GPL(virtqueue_get_last_used_idx);
+
 /**
  * virtqueue_enable_cb - restart callbacks after disable_cb.
  * @_vq: the struct virtqueue we're talking about.
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index f923e42cfd01..1aed4d9235c8 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -112,6 +112,8 @@ unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
 
 bool virtqueue_poll(struct virtqueue *vq, unsigned);
 
+unsigned int virtqueue_get_last_used_idx(const struct virtqueue *vq);
+
 bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
 
 void *virtqueue_detach_unused_buf(struct virtqueue *vq);
-- 
2.51.0
Technical Details
Message-ID: <20261109094310.103979-2-lange_tang@163.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-359182-linux-kernel-netdev=dewy.ru
References: <20261109094310.103979-1-lange_tang@163.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-359182-linux-kernel-netdev=dewy.ru
From: lange_tang@163.com (Longjun Tang)
Newsgroups: linux.kernel.netdev
Subject: [PATCH 1/3] virtio: add virtqueue_get_last_used_idx() helper
Date: Mon,  9 Nov 2026 17:43:08 +0800
Organization: Usenet Network
Message-ID: <20261109094310.103979-2-lange_tang@163.com>
References: <20261109094310.103979-1-lange_tang@163.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="324337"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:f5qekwEPzmdqSzNYm2fv/YRKK7Q= sha256:VG5C6dgLpHg5yBCu3C0ajPjbx41oybHD+LyMZjglQYU=
	sha1:bGfODaDF9WaIALEvrsOf6Ur1/V8= sha256:69ojcWAU8owHk88M2afsGHnpfxU2VSphBxIRsl93jOA=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id DC848113781; Mon,  9 Nov 2026 13:12:09 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (1024-bit key)
	header.d=163.com
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 2xo-G8PE8dsf;
	Mon,  9 Nov 2026 13:12:03 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=netdev+bounces-359182-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=163.com
X-Received: from sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id CBA3711375F; Mon,  9 Nov 2026 13:11:52 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id 71630A8166; Mon,  9 Nov 2026 09:43:52 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1794217428;
	b=AuKUGs6uIrJ/DpBfli/dGoUohu61JlVntK9b93VYJY+KZgYfoVUEnAtVhhychtkC7Wfn
	 lucQHfMw3VZHYSbch5HM+HzM3NagPpgonSZLDQeTLN2ziVc3wkr8Rs99nDYpD4z6JYwii
	 OfV/4qaCXCzSEGLDq3lVZetXLcuUfbNwcNE6++H7Qed9qfe6K6l28rk+yN1cXH0NMAxA0
	 APzQacVm9Dp5UL/lM/R83Xl1eSgSNWlLW3KEqutj6pi5GqzFEn7Uvmzav4BzBBLKra4VF
	 eDrNtSNU7wy8vmdD4JPTvpslakpJg44knvM+vA01+mq1lf/UxA4aZYw79s0K1glivJg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794217428;
	h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:Received:From:To:
	 Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam:
	 X-CM-SenderInfo;
	bh=nXAH+OU9RqM5dRAWXG7ExEKpjcRp36K9MyANZBxAazs=;
	b=N40efhGlmIlmqSxoWOEQP5WXRFyhtiC5tAunsy+HkmP2D8BJ9H6uSgnE9PHYPR3CmaKj
	 QeDqsO8rH58oUA23BWFEol7A5pwba8rimGm4sayWZR4kVZHTk4WsaYnOFUGCwSfj6c13b
	 qFYCvi5pNgOCK6yGpMtqL4GbW7tULT3LtF9Sk0AxT9NJA9qZunWhu6fTfNyQ9butE1cfk
	 MlcmmgTYUW+DEmLz873Qw4VJ5+BR/zgbp0cvQU5j6Kip8HICmGCurKfcDrDaEhzfD0ciy
	 19HUN0cxr/pzIMGmVYhVM/dD9H2DDRD7u99XekWUHYeFRYlKVZFJCa+4AQIjfY/9m/g==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=hSE3cG+M;
	dmarc=pass header.from=163.com;
	spf=pass smtp.mailfrom=163.com;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 421D9606A33D;
	Mon,  9 Nov 2026 09:43:48 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=hSE3cG+M
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.5
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1794217427;
	b=EeBODewrCLsfzAnZtizzUEzws0oUGQvTc1tmhHwAmpC2323vwiay439URMTPY1x+Yelc
	 gHFLbpuVc4WxRumSNE0s6K1fAUKFIzGPlwIKX+0KXcfbqRHh2lHuWeSOl6Tr3mOZ8V8Fl
	 l20nF0lKDLnzQiotzPqKYlyQelONPAxVh6E5HjQzVz9fcHdyTr6OYFw8w/1Z3Y+YDbmz9
	 oVGYILb4rxT1yEdB/eRhwLbr7lzgXjHjIGiSwdFz3MlwPkYsVWS1hSQKt2AN9GW/nLbhM
	 T5S+LFok4txhOhoYhbKPiRES0oL/zvxVnhDAvfZO3EPZgfxEILP3zsrzVL8iFjjsYPw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1794217427;
	h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date:
	 Message-Id:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam:
	 X-CM-SenderInfo;
	bh=nXAH+OU9RqM5dRAWXG7ExEKpjcRp36K9MyANZBxAazs=;
	b=lpipBHnNdCvzDIi4tV68FkmlYhrx2fV0VLuP5ePOau1tttBxYfnZvWGM4exbnt2nI+R+
	 vJKibOq4BnGRKjUU4rF+RnuLN1PyWnFseRZLKEZegYWApGNPwGlSs+fNeH3AeaEk84z/R
	 wFwXRjHCsiQAm4cPRlpCjCiPkXa5Hw8q2+K2rw7pjX7c07v+8q+luhfJU2GOGAS0zrD1Z
	 KbgmGA9A4LEOiJleTuxtEoRy1JwQWxROmEMINCXqRTnVAaG2YPPdxTFQc6Mau4DQdHbl3
	 qQWrH8St6HsJ0ItpM3zRC6WeF+CU3Z+0GbGuVS90x+mLhG/2ejPhRCv1Npo3wNL6AJA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=163.com;
	spf=pass smtp.mailfrom=163.com;
	dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=hSE3cG+M;
	arc=none smtp.remote-ip=220.197.31.5
X-Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.5])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id D73A7606A331
	for <netdev@vger.kernel.org>; Mon,  9 Nov 2026 09:43:45 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D73A7606A331
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com;
	s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=nX
	AH+OU9RqM5dRAWXG7ExEKpjcRp36K9MyANZBxAazs=; b=hSE3cG+MfLLbFxCrQx
	BkegqjPlQx2fmJOhAOyLxP1vpj/AF51qIPbthFlT6tpL251zcqmKvKBn2cwwHRxW
	gX5cAO/23UYvyjoWD2/Wj+p7BsZoUnV3Qe/DXwuJLonBIMuRW7sd8PwTbjsFNGaf
	kF8l4IVbYYuI7PCccL6n5+MEs=
X-Received: from localhost.localdomain (unknown [])
	by gzga-smtp-mtada-g0-2 (Coremail) with SMTP id _____wBHJCGvlfFq58sxGQ--.4049S3;
	Mon, 09 Nov 2026 17:43:12 +0800 (CST)
X-Cc: xuanzhuo@linux.alibaba.com,
	virtualization@lists.linux.dev,
	netdev@vger.kernel.org,
	tanglongjun@kylinos.cn
X-Mailer: git-send-email 2.25.1
X-In-Reply-To: <20261109094310.103979-1-lange_tang@163.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-CM-TRANSID: _____wBHJCGvlfFq58sxGQ--.4049S3
X-Coremail-Antispam: 1Uf129KBjvJXoWxJw4UWF48GF4rtFW3JFWxCrg_yoW5Aw4DpF
	4DKrWUtrWkGFWxZFyfWF4qvrnI9wsrGw13KrWxuw1DuF98tFyq9F1j9w1rJr1fAr4kC34x
	Zrs0grZ8KF1DuFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2
	9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j0a0PUUUUU=
X-CM-SenderInfo: 5odqwvxbwd0wi6rwjhhfrp/xtbC-hCo1mrxlbBWIQAA3Z