Article View: linux.kernel.netdev
Article #19661Re:Re: [PATCH 1/3] virtio: add virtqueue_get_last_used_idx() helper
From: "Lange Tang"
Date: Fri, 20 Nov 2026 07:13
Date: Fri, 20 Nov 2026 07:13
63 lines
13489 bytes
13489 bytes
At 2026-11-13 06:50:33, "Michael S. Tsirkin" <mst@redhat.com> wrote:
>On Mon, Nov 09, 2026 at 05:43:08PM +0800, Longjun Tang wrote:
>> 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>
>
>This is not a good API, packed ring does not have a used ring at all.
Right, packed has no used ring. But last_used_idx is maintained for both split and packed
rings and can be used to get the position the driver last consumed.
>
>> ---
>> 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:
<62c513b2.5a25.1a1f7cd82ec.Coremail.lange_tang@163.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367731-linux-kernel-netdev=dewy.ruReferences:
<20261109094310.103979-1-lange_tang@163.com> <20261109094310.103979-2-lange_tang@163.com> <20261112174936-mutt-send-email-mst@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-367731-linux-kernel-netdev=dewy.ru
From: lange_tang@163.com ("Lange Tang")
Newsgroups: linux.kernel.netdev
Subject: Re:Re: [PATCH 1/3] virtio: add virtqueue_get_last_used_idx() helper
Date: Fri, 20 Nov 2026 15:13:23 +0800 (CST)
Organization: Usenet Network
Message-ID: <62c513b2.5a25.1a1f7cd82ec.Coremail.lange_tang@163.com>
References: <20261109094310.103979-1-lange_tang@163.com>
<20261109094310.103979-2-lange_tang@163.com>
<20261112174936-mutt-send-email-mst@kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
Injection-Info: usenet.network;
logging-data="3091371"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:rQdNJmJ8xkGiFXY46i8hEzBBNw4= sha256:EGoFXrN0izBMJu8hUXBOProbUppyXRa1VI+orjxjIpI=
sha1:hcU9eXUpD21OmmV2gGtvhImoMMg= sha256:ttaxPZ7fc56SHKcCH4j1vYr5zKksmUdrKGQx9FXfRqI=
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 B9F8E1158D8; Fri, 20 Nov 2026 10:14:03 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.049
X-Spam-Status: No, score=-3.049 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] 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 x2QLUzKoOtu3;
Fri, 20 Nov 2026 10:14:00 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-367731-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 6AFD31158E0; Fri, 20 Nov 2026 10:13:52 +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 AB58AC8B22; Fri, 20 Nov 2026 07:13:49 +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=1795158829;
b=isJJ0D7H9UNnvpFOWqpAMgURKvSOBBedec2pINqw2waLqF6Jfpx9YWPZ9yXawv5KJemm
nyxqL12Qd7lg4mb9wtgKKb7zKuHGwyJRekLs3m8916HODVXLqudRuP2pUuzUVppO63ORI
d99ju6opM0MnfhobmFOhJ7C0tDMcF+uisd9u70eE6bEd7Fnom35wE1CX0uQOHobNlXkPE
h+5S4zJ93aRpAKHtb3AJNBmODx6/QIIQiBVsKvAocbDXSif7LuJFF/VmaViFEbl3DV6Za
AaH8aPfImvATF52Z+IZjq/DZuwQAjsO9SAFGTCVQf5Y2JrFbOgwrGXS/4BqjY6mQDBA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795158829;
h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:Date:From:To:Cc:
Subject:X-Priority:X-Mailer:In-Reply-To:References:X-NTES-SC:
Content-Transfer-Encoding:Content-Type:X-Mailing-List:List-Id:
List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
Precedence:MIME-Version:Message-ID:X-Coremail-Locale:X-CM-TRANSID:
X-CM-SenderInfo:X-Coremail-Antispam;
bh=bEA86BmeuaVIl/WuC196ZahnWqjhESSU5lFaK7N0pcY=;
b=HACxGP1nUGdoQL/wM46yCjxFjrHX04hxXVCr8cs3XFEg8e5OTZuJpGJcTGOA9EU0tOUr
bt8BDq7ub3nQbKw7kLj65fQ3rwIEKoaBroJ8KaBBNIKSNgw4/IACIe+SE630ypb+O0PUS
T9GA4c6+WKa/pXIhqzH7D0wzOsCHmfb4/Gx0N/f46ov4Lw+eK27KM8hA+Wqj2QSZjxqBG
zY2GQsMQendv0hjV+CLiqk7abd9KqyLuc06vk53GvRTyZMkhFFfsEyz554JTDF4PtDAU4
6f064+uyydanXDCui9kI2Ay+Yu3FHA7z619UwOjG2tVsHHinSyB6hJ3NV9bJEGHI9Vg==
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=UNblf+6w;
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 83E5B606A339;
Fri, 20 Nov 2026 07:13:49 +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=UNblf+6w
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.4
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1795158828;
b=Ln9nlYYsGWm3MDdUoOTEzrSC1sjxfNqUa2DmPw+JzksrUE1twxl/F7aFGk4cUSLy2uwY
d9gde6M8dcQ85O40a+PVOYSELJq2/ghXJW+tEHE+lI0fWRq3vzFJDlbBWzymgr4h5WeJx
WWP8joMEbKVXoDLSd5AQ+2rul9rO35MeHsvCkYl3Ay6hAjFxPptYHQzWnlCehh6Kr6l0x
1QDXAxl8BC1IHFenDID/ZE2PK6aZHBNQMWIelPFUabiQhrtDXpcqTmFA/eZaJdm9EYzVt
98u4de6DHRHoU42JPpELA5vuxEx08oi4DJ/rw2+xLoHzZu0egdGgo99n357Qd3eJGkA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795158828;
h=DMARC-Filter:DKIM-Signature:Date:From:To:Cc:Subject:X-Priority:
X-Mailer:In-Reply-To:References:X-NTES-SC:Content-Transfer-Encoding:
Content-Type:MIME-Version:Message-ID:X-Coremail-Locale:X-CM-TRANSID:
X-CM-SenderInfo:X-Coremail-Antispam;
bh=bEA86BmeuaVIl/WuC196ZahnWqjhESSU5lFaK7N0pcY=;
b=SVlme4e1Nz8YyzwDhejFeMLzYOFZ6DMMRf3QFwrrj96e/sQ7FpuSZuRXubjUYZ8A0Tm0
KvZgpyDqvU1a5TikvYWXoU7Kq+VRmodGeFs3Qq5E44M1WZCCpXLBgtS1D+rcXVy1M+RKD
bJLv2ftxTnbg3lpZ1h9sAdFoEE3K+G4Iy7vzsDbDIFykkDD4As5zaxhhywGmzpHeB7vEB
jLlxrGCOs/VYXHaijIKQ+TIe+mSynOUx0aF6mcJiD8aLJdHolnt9lwmeVeWJbWkxJAh2w
X4JXyeRbUJPBM374/XaIZJfLzdgLU+LH/v0HutdkdMx7KMhK2vLtS4UM2xsGqKHQqvg==
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=UNblf+6w;
arc=none smtp.remote-ip=220.197.31.4
X-Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4])
(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 B49F2606A325
for <netdev@vger.kernel.org>; Fri, 20 Nov 2026 07:13:47 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B49F2606A325
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=Date:From:To:Subject:Content-Type:MIME-Version:
Message-ID; bh=bEA86BmeuaVIl/WuC196ZahnWqjhESSU5lFaK7N0pcY=; b=U
Nblf+6w0WzYFxY6o8Npm4UFIFHiBs8WZgaRhoefW3ilZu/HM8cBRdnMPBWTLggkz
TlPAr+21AFkabbHjuOWFDzNox/EXcjBfWIUCEuUmnkWGRAzL490GzeQ1nm6h+Ic3
3rHOiVDjoKZoLjQMxdakYfUBrNxRUxMsEHqmNG9eIM=
X-Cc: "Jakub Kicinski" <kuba@kernel.org>,
"Eugenio Perez" <eperezma@redhat.com>,
"jasowangio@gmail.com" <jasowangio@gmail.com>,
"xuanzhuo@linux.alibaba.com" <xuanzhuo@linux.alibaba.com>,
"virtualization@lists.linux.dev" <virtualization@lists.linux.dev>,
"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
"Tang Longjun" <tanglongjun@kylinos.cn>
X-Priority: 3
X-Mailer: Coremail Webmail Server Version 2023.4-cmXT build
20260511(2e539873) Copyright (c) 2002-2026 www.mailtech.cn 163com
X-In-Reply-To: <20261112174936-mutt-send-email-mst@kernel.org>
X-NTES-SC: AL_Qu2SB/2et0Ar5SGfZukfmUgRg+c/XsK4svUj24VVOJF8jCDp+SEJYHZTFGvuwfi3ASqsmSO8ahFRxdZ2WohybrgljfmB9rd7tqyaLw/zu0Gkkg==
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-Coremail-Locale: zh_CN
X-CM-TRANSID: aigvCgD3_8gT8_9qo31yAA--.319W
X-CM-SenderInfo: 5odqwvxbwd0wi6rwjhhfrp/xtbC-RObyWr-8xNxiwAA3z
X-Coremail-Antispam: 1U5529EdanIXcx71UUUUU7vcSsGvfC2KfnxnUU==