🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #19671

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

#19671
From: Longjun Tang
Date: Fri, 20 Nov 2026 07:18
77 lines
11383 bytes
From: Longjun Tang <tanglongjun@kylinos.cn>

Expose last_used_idx through a new helper, which is used by both the split
and the packed ring and denotes the position the driver has last consumed.
It is 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 f683a2d6a200..e8af6d5c6432 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1005,7 +1005,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. */
@@ -1072,7 +1072,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
@@ -3236,6 +3236,20 @@ bool virtqueue_poll(struct virtqueue *_vq, unsigned int last_used_idx)
 }
 EXPORT_SYMBOL_GPL(virtqueue_poll);
 
+/**
+ * virtqueue_get_last_used_idx - get the ring's last consumed position
+ * @_vq: the struct virtqueue we're talking about.
+ *
+ * Returns: the vring_virtqueue's last_used_idx.
+ */
+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.25.1
Technical Details
Message-ID: <20261120071826.198604-2-lange_tang@163.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367740-linux-kernel-netdev=dewy.ru
References: <20261120071826.198604-1-lange_tang@163.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367740-linux-kernel-netdev=dewy.ru
From: lange_tang@163.com (Longjun Tang)
Newsgroups: linux.kernel.netdev
Subject: [PATCH v2 1/3] virtio: add virtqueue_get_last_used_idx() helper
Date: Fri, 20 Nov 2026 15:18:24 +0800
Organization: Usenet Network
Message-ID: <20261120071826.198604-2-lange_tang@163.com>
References: <20261120071826.198604-1-lange_tang@163.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3093049"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:MmQ48SXW6MhAuvYSrfDA8MVn0ME= sha256:R5QcMgycoNcHzWdPH3vw6Ya5lOSHpeMDld6YPKgOuBI=
	sha1:19/gW2xp0g/VZl44S27PpMr0l8A= sha256:mRlB2FOWjhRvSWYGMFSM/Z8AtQXum+YoRTLiujqWCQU=
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 B1E291158E2; Fri, 20 Nov 2026 10:19:16 +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 M5qWkA9ogC1j;
	Fri, 20 Nov 2026 10:19:13 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-367740-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 sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 8EBF31158EA; Fri, 20 Nov 2026 10:19:04 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id A63FE31A12; Fri, 20 Nov 2026 07:18:57 +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=1795159137;
	b=j+ZwLXJwRCEHYzmWKjE1asP92+4iUNfbUJMXR/mNiOUPuFLJ6Sy+6t9+K1KdMKOeYWfh
	 iYwUnN1mITwbYiZjKAIKoiBt3rUVdzag/yCqoqTq4LIfAakOP9BL1KMli8Hhu6hag4X4i
	 xl7cwd5IILsc1g7JoHu2jLwx1w1ocn167v97XCng1zJMvndsiQ1l8/jrGeF4lxzcqMCOi
	 gUe42xTG3BFEy2wHwZAyBydNXjb2z/afQxbwXjrDBs16dDp6DGW5mG0G8Dqtd0nJPVfjz
	 b1uW/qsRaLj7DsmTqzxPRoq/HVVTfdtXJMyJk+J95WahdW1bVwZvIKyrjHQO/JAVcyg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795159137;
	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=W0JhyG/3pAXwDfIBhCcxcDEJVltQTzcmdJUOS5CF/wY=;
	b=VaHehYv5iAi43xkHJendbI5RbBvhbuCli7fu+kScoNLWUlm+RNaDVQU35XUWKZqZTDz8
	 YQaXX1W+lJ4TORoc+e4b5trntf+0SVTjlHv3HmOfeU+YP8PP5WmbE5HQ8VaBOHJcJZTpA
	 fmEi/YkPIVjtDGP5B5lf/+zbvsPdhIMuRwvmRUDhjw4MR/Y5BtjT2nKFtqt3G+mhkLieA
	 +ermGAH40spJsCDbGqta55y09V4Y8BkmdoA0wDUmXxSBwuYmQVKNEUae8Q1wiYdYQfIDd
	 1CngHdb3dfjSl2+tOUhafY1LYsVJO7tlPlC+pudvWmiWmVbjirYmeSYgHspgLsIFV6w==
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=i4tdVMCo;
	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 7D98A606A331;
	Fri, 20 Nov 2026 07:18:57 +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=i4tdVMCo
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.2
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795159136;
	b=QIMRhdSblpfApf6X1quMXjWEr4EkuxYjb2ENYqZvGfcykFYoHl5J0bU9fIa1R9Iu8KoK
	 cQMerNc3tj5Qc/guwi6Dc8SDGfhQKF1RFIv2tCnr6dZab2eSl+rpk2+lQ8V5765VKGSn5
	 G0ILOFJO9oy0PRzrp6REkbJEitix1uan8pPbBPn8OOgX9j9MiqousZJg9L/N4bVvSpShk
	 bLv2/m+prxGszR+yriO0Wr0ZCfDLHSYAv/4rqswf8wW/gfzr6tAep8F3jwuZ5KwP8h7e/
	 LOHfZ7IXH7R/JEvX++041Fd/ZXmHekNBklONwFc1SdNPGcCbWOF+A9kbTb09qSMLwsg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795159136;
	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=W0JhyG/3pAXwDfIBhCcxcDEJVltQTzcmdJUOS5CF/wY=;
	b=xvC/CUUAxLd+Zk8BYa/RgANEw4bz1BOTzEapPw663vSGo8KJwzJJPZeVsBcICJlgWaf2
	 I6OXYoKioSpQp8VVjWpyqFgr0/+gqbmEkGWLYRm1VwUZkYEOOouKHhi9zoA6rgIYf8QmY
	 54gFGhPgcp9umVJnINr4zx3S9Uupo5v4579s3tMMF6Zm8yVlIYRZTm24KES+oRTXxV98H
	 xXDT6zn8DfhS6EL6KY4O9tW398WS6PTxMPQqjqDrTfdI4QJDBamWHHARVF7mWDFHzzYBs
	 K+kerZTZvJlFd+mzYhPNCL5g5u68PgMhka/e8iBE/ShVZYlsM+Fx1hp30izAHgYxzrg==
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=i4tdVMCo;
	arc=none smtp.remote-ip=117.135.210.2
X-Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.2])
	(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 3C290606A333
	for <netdev@vger.kernel.org>; Fri, 20 Nov 2026 07:18:55 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3C290606A333
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=W0
	JhyG/3pAXwDfIBhCcxcDEJVltQTzcmdJUOS5CF/wY=; b=i4tdVMCo95A+8q1cZZ
	EFTUXlGe/u1aTzl7DzwpADxCq70uXULNVLFVJWBXONI1lNHbQ/6EidhfMDkoT/h1
	/RRYMn0W7l9jEUhm5oqXOXeZLtkPqEW7Ehs7N4OtDvANsWfMXLZ/hV59BqNY2SQw
	DsZaBSuQsvuUBRtPovKXLMdq0=
X-Received: from localhost.localdomain (unknown [])
	by gzga-smtp-mtada-g0-3 (Coremail) with SMTP id _____wD354ZD9P9qmowxJA--.12595S3;
	Fri, 20 Nov 2026 15:18:28 +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: <20261120071826.198604-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: _____wD354ZD9P9qmowxJA--.12595S3
X-Coremail-Antispam: 1Uf129KBjvJXoWxJw4Utr1DGrW7Jr48GFy8Zrb_yoW5CF4DpF
	srKry5trWkGFWxZFyfWr4qvr1a9wsxGw13KrWxuw1DW3Z8tFyq9F1j9w1rJr18Ar4kC347
	ZrsIgrZ8KF1DuaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2
	9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07j0c_-UUUUU=
X-CM-SenderInfo: 5odqwvxbwd0wi6rwjhhfrp/xtbC4QToF2r-9ESxfwAA3x