From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBE8823ED5B for ; Tue, 30 Dec 2025 06:47:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077227; cv=none; b=TtffI7QdnS3kf2voHt3kXxvB9KbS+ZiFP+UdW/AmcFHYGjIAwOC6jI2iNhqnLAVffmWYJhaS6begCsW/NL6a6XcRjTU0cOgIjYeyXryzNaVOyeMa+ULysyuG+nqkV0t4WtPjk0cypj2ueKTuTmuwfUGY6h9uDcOp6GFwPi0H/TA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077227; c=relaxed/simple; bh=3mJIVRuiBRr1DGrHmxdE+1bQyeQNd0+hbYY6OL8YR1Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=q0rVnjUhn097LzmMmQj6HDsMSmFhaDdwUKo+8BQIR/cFjd2lkMe/maTZAvHe1+bMfC81J6SJlB3egy0Cdc6WTgCocl8V5m5ntiuYZNEIt8Blr2Hefjj8JCrQKyRWnOMP21TULbR+8dchP9cal0vMuItjXIYnr/NBotitDd86nH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=EObCJGtM; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EObCJGtM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077224; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Wc3TM+0azwY6MCNmOZiraFDZZINoIrGASd58ariBiQM=; b=EObCJGtMZg0i//kLzFAfkGD+dWdwXI1/adW/quFVPZrueQHJQiuTgmJnS0Rjf3+fyiWb53 qL+D56hH31U8U+nYARNJSbNL5q7DrsUY6TJEU91o/NnEKMhlEWiM8W9UjbEzLD9gL5AtnC stWWE7T/8n3Q1HVv8Nfdu8vWpxwOS8c= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-464-gmWSBMNYO5-T7A_EucR8wg-1; Tue, 30 Dec 2025 01:47:01 -0500 X-MC-Unique: gmWSBMNYO5-T7A_EucR8wg-1 X-Mimecast-MFC-AGG-ID: gmWSBMNYO5-T7A_EucR8wg_1767077220 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0BF6E1800358; Tue, 30 Dec 2025 06:47:00 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B83FC1956053; Tue, 30 Dec 2025 06:46:56 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 01/19] virtio_ring: rename virtqueue_reinit_xxx to virtqueue_reset_xxx() Date: Tue, 30 Dec 2025 14:46:31 +0800 Message-ID: <20251230064649.55597-2-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 To be consistent with virtqueue_reset(). Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index ddab68959671..cc1b8bbc6774 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1009,7 +1009,7 @@ static void virtqueue_vring_init_split(struct vring_v= irtqueue_split *vring_split } } =20 -static void virtqueue_reinit_split(struct vring_virtqueue *vq) +static void virtqueue_reset_split(struct vring_virtqueue *vq) { int num; =20 @@ -1253,7 +1253,7 @@ static int virtqueue_resize_split(struct virtqueue *_= vq, u32 num) err_state_extra: vring_free_split(&vring_split, vdev, vq->map); err: - virtqueue_reinit_split(vq); + virtqueue_reset_split(vq); return -ENOMEM; } =20 @@ -2091,7 +2091,7 @@ static void virtqueue_vring_attach_packed(struct vrin= g_virtqueue *vq, vq->free_head =3D 0; } =20 -static void virtqueue_reinit_packed(struct vring_virtqueue *vq) +static void virtqueue_reset_packed(struct vring_virtqueue *vq) { memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes); memset(vq->packed.vring.driver, 0, vq->packed.event_size_in_bytes); @@ -2218,7 +2218,7 @@ static int virtqueue_resize_packed(struct virtqueue *= _vq, u32 num) err_state_extra: vring_free_packed(&vring_packed, vdev, vq->map); err_ring: - virtqueue_reinit_packed(vq); + virtqueue_reset_packed(vq); return -ENOMEM; } =20 @@ -2860,9 +2860,9 @@ int virtqueue_reset(struct virtqueue *_vq, recycle_done(_vq); =20 if (vq->packed_ring) - virtqueue_reinit_packed(vq); + virtqueue_reset_packed(vq); else - virtqueue_reinit_split(vq); + virtqueue_reset_split(vq); =20 return virtqueue_enable_after_reset(_vq); } --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E37B2DC767 for ; Tue, 30 Dec 2025 06:47:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077232; cv=none; b=N09y98yCNYi5k+sxFzwSipP/u+UC6FmyAxazT2cpwSNUxR7fCJTxv58SIJk+kzexgtOu67ulrHV6sJVHW3OnJ1oVnQ0W0TmtFPTJK2gKQyKw9qIa/wPYORK/arWKoLKjZFoVYiwdWM7sU6X2xAvNHbbgIcjZYg3iLyY8QKpmW1E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077232; c=relaxed/simple; bh=yXYGAEZQ8Iw2OCraZR8+b/6Q0QjT7RWJKnz+9vw/Ed8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qaPMbq11X1eRs68J+kF/Luaob67Dc+mP92Cs72GSjR+DCG1C+OL+FIAeZoHFA9YXCHZf9mDDYdxMlV9K3oIgAbIuZy51gRoYfrjBOC7L/s5t7SvnPbvYcfQ5XynO9KbLwBHp8zCgTJg2samyRF8eKtle9fex+rnNqSw4QCdX2Nk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Q3sGsMVm; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Q3sGsMVm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077228; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mJzLkNizPe0Q11iTpFozDICpZibb0u5PKmXWgxiw+ZE=; b=Q3sGsMVmbGpRD6HAw+x0brCCjIIxXwsNZUQTHQvWB1Izz73dpzzC955Mxmts+cGVsMQELp Dlr2en1AF+h61BftPsiYQ04DYrOWJdhhrlk4qXJ6jYXsl+STk8JyPINiHqjZPNdzxbjcYX nnxNIE5lWRbjSFS9PpKA6dpVJ8H00fw= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-556-JW64USQpNDOpawIrPs-iZA-1; Tue, 30 Dec 2025 01:47:04 -0500 X-MC-Unique: JW64USQpNDOpawIrPs-iZA-1 X-Mimecast-MFC-AGG-ID: JW64USQpNDOpawIrPs-iZA_1767077224 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D128C19560A5; Tue, 30 Dec 2025 06:47:03 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B781519560AB; Tue, 30 Dec 2025 06:47:00 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 02/19] virtio_ring: switch to use vring_virtqueue in virtqueue_poll variants Date: Tue, 30 Dec 2025 14:46:32 +0800 Message-ID: <20251230064649.55597-3-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index cc1b8bbc6774..d5a317cb885f 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -919,11 +919,10 @@ static unsigned int virtqueue_enable_cb_prepare_split= (struct virtqueue *_vq) return last_used_idx; } =20 -static bool virtqueue_poll_split(struct virtqueue *_vq, unsigned int last_= used_idx) +static bool virtqueue_poll_split(struct vring_virtqueue *vq, + unsigned int last_used_idx) { - struct vring_virtqueue *vq =3D to_vvq(_vq); - - return (u16)last_used_idx !=3D virtio16_to_cpu(_vq->vdev, + return (u16)last_used_idx !=3D virtio16_to_cpu(vq->vq.vdev, vq->split.vring.used->idx); } =20 @@ -1844,9 +1843,8 @@ static unsigned int virtqueue_enable_cb_prepare_packe= d(struct virtqueue *_vq) return vq->last_used_idx; } =20 -static bool virtqueue_poll_packed(struct virtqueue *_vq, u16 off_wrap) +static bool virtqueue_poll_packed(struct vring_virtqueue *vq, u16 off_wrap) { - struct vring_virtqueue *vq =3D to_vvq(_vq); bool wrap_counter; u16 used_idx; =20 @@ -2611,8 +2609,8 @@ bool virtqueue_poll(struct virtqueue *_vq, unsigned i= nt last_used_idx) return false; =20 virtio_mb(vq->weak_barriers); - return vq->packed_ring ? virtqueue_poll_packed(_vq, last_used_idx) : - virtqueue_poll_split(_vq, last_used_idx); + return vq->packed_ring ? virtqueue_poll_packed(vq, last_used_idx) : + virtqueue_poll_split(vq, last_used_idx); } EXPORT_SYMBOL_GPL(virtqueue_poll); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E92C019D8BC for ; Tue, 30 Dec 2025 06:47:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077234; cv=none; b=OtqrinGIkrP+bFmNmlnP2Zj0rym2xEDVJoueaK15T6TwAFtRiT7xLo3Zzin0yAjC2YmS/8Cud8DCxwz4L7VqU9M608LrooOySy9qJvMzpIIxIYnYV6NcdLThwJM6/qguENW5axll6osCfMWmTymRDUh+Hynlqm9cOnZI0R4S41k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077234; c=relaxed/simple; bh=LcAuwsOaaoYHSmtPJAb0/uB+j4lR+Aeogik9qRsHP24=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QDhz8FMBtCnrXRboGLbVSdRyu+0d4S4YnhbLpsw0/n55MVYOPvUxLmJ1G8158Zgw9QIs94lPK3BWTE+En6kAQ8iieM02U2pt9L4VXhDWFZGx0elr6aUqsAlpZ24Ovx5/flSVDXQpO8Br6aey02oNH38j9tcisDgLPOG59UWDyTQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=HqJ3Qqu9; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="HqJ3Qqu9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077230; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ko4ymhJxp0fSqryS5Xes+pZE6cR17YhQMvZ5bfkoLCE=; b=HqJ3Qqu9aUFVbKaX2wkCxbnbMsXmLbuD3jzt36dARgaG3MVV6/xQ9x+bw6Lt32i3fEgsiA xwt/hpgaS8CYMlHEYqt93x/vwR65oWQua2R+MUa4Rw0okG+HIz5hBuyVDWBdHvMRhEhfJs Ip/j0LlqquACTgzc8cNCHJ0lsa6dBOo= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-WQ7ydnPoOhaKJG14YFeASg-1; Tue, 30 Dec 2025 01:47:08 -0500 X-MC-Unique: WQ7ydnPoOhaKJG14YFeASg-1 X-Mimecast-MFC-AGG-ID: WQ7ydnPoOhaKJG14YFeASg_1767077227 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C470D1800654; Tue, 30 Dec 2025 06:47:07 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 884941956053; Tue, 30 Dec 2025 06:47:04 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 03/19] virtio_ring: unify logic of virtqueue_poll() and more_used() Date: Tue, 30 Dec 2025 14:46:33 +0800 Message-ID: <20251230064649.55597-4-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 This patch unifies the logic of virtqueue_poll() and more_used() for better code reusing and ease the future in order implementation. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 48 +++++++++++++++--------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index d5a317cb885f..a32050e1aa47 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -806,12 +806,18 @@ static void detach_buf_split(struct vring_virtqueue *= vq, unsigned int head, } } =20 -static bool more_used_split(const struct vring_virtqueue *vq) +static bool virtqueue_poll_split(const struct vring_virtqueue *vq, + unsigned int last_used_idx) { - return vq->last_used_idx !=3D virtio16_to_cpu(vq->vq.vdev, + return (u16)last_used_idx !=3D virtio16_to_cpu(vq->vq.vdev, vq->split.vring.used->idx); } =20 +static bool more_used_split(const struct vring_virtqueue *vq) +{ + return virtqueue_poll_split(vq, vq->last_used_idx); +} + static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, unsigned int *len, void **ctx) @@ -919,13 +925,6 @@ static unsigned int virtqueue_enable_cb_prepare_split(= struct virtqueue *_vq) return last_used_idx; } =20 -static bool virtqueue_poll_split(struct vring_virtqueue *vq, - unsigned int last_used_idx) -{ - return (u16)last_used_idx !=3D virtio16_to_cpu(vq->vq.vdev, - vq->split.vring.used->idx); -} - static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); @@ -1710,16 +1709,20 @@ static inline bool is_used_desc_packed(const struct= vring_virtqueue *vq, return avail =3D=3D used && used =3D=3D used_wrap_counter; } =20 -static bool more_used_packed(const struct vring_virtqueue *vq) +static bool virtqueue_poll_packed(const struct vring_virtqueue *vq, u16 of= f_wrap) { - u16 last_used; - u16 last_used_idx; - bool used_wrap_counter; + bool wrap_counter; + u16 used_idx; =20 - last_used_idx =3D READ_ONCE(vq->last_used_idx); - last_used =3D packed_last_used(last_used_idx); - used_wrap_counter =3D packed_used_wrap_counter(last_used_idx); - return is_used_desc_packed(vq, last_used, used_wrap_counter); + wrap_counter =3D off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; + used_idx =3D off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); + + return is_used_desc_packed(vq, used_idx, wrap_counter); +} + +static bool more_used_packed(const struct vring_virtqueue *vq) +{ + return virtqueue_poll_packed(vq, READ_ONCE(vq->last_used_idx)); } =20 static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, @@ -1843,17 +1846,6 @@ static unsigned int virtqueue_enable_cb_prepare_pack= ed(struct virtqueue *_vq) return vq->last_used_idx; } =20 -static bool virtqueue_poll_packed(struct vring_virtqueue *vq, u16 off_wrap) -{ - bool wrap_counter; - u16 used_idx; - - wrap_counter =3D off_wrap >> VRING_PACKED_EVENT_F_WRAP_CTR; - used_idx =3D off_wrap & ~(1 << VRING_PACKED_EVENT_F_WRAP_CTR); - - return is_used_desc_packed(vq, used_idx, wrap_counter); -} - static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 565CC21D3E8 for ; Tue, 30 Dec 2025 06:47:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077242; cv=none; b=nIqfU7eXhmBzDzxhu05gqU/Ccs09wQ0R+++BjqBaPSii1kaWHnNpQOrBbUtxM9iVGV8BhR7xumnWdTEF10byiMj2HIa8oDE1XU38JI267QQoDyF5pKoFo9E8YlqmEi3jIBx7N4OPT2uwCj1MMOKMprtz6qPxcBlo5HPbEYoFoTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077242; c=relaxed/simple; bh=aQYA6vyQ9HWcS/gGRHGCU43TQQaCLHZPZJmr5ol0iIc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=U9u5AhAP5MkPi7x1tHi/r9grHk1tlvWmJZo/y3sXAWvgraYkBccoIGqUTCH/Fdt0/c7Z8uMxj2L07z2P3Q3Nyb8eycCr3FxWUU88WwTpZ+QZFdoU90oNn/rsbVPGRzP3fHOCIvEfep04E+gn78/FaBqPyC78uiZrN5X4A1iKF3g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=OpSa1S7E; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OpSa1S7E" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077236; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OGYNGtV0a+1q20vKU+i0OW5XBJofD4LOIx8feJsdmD8=; b=OpSa1S7EXf7l/+yP19DTS8w0eZ6B/PKbomkZOY8dmw8GGRaSrdMO8OmkGydDu7g3kBLthI UbwUzekpKUlrQgInkbgr31ngaKc9aSPyZzJl3pDqh7OR93cmV31QlY269Ehzxvw4vQxu1k n0nkAE82IVVVmoKDTTwKxEdWA22yAQo= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-i-BAEVugOxqWJUo3rIPLkg-1; Tue, 30 Dec 2025 01:47:12 -0500 X-MC-Unique: i-BAEVugOxqWJUo3rIPLkg-1 X-Mimecast-MFC-AGG-ID: i-BAEVugOxqWJUo3rIPLkg_1767077231 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BC0C419560B2; Tue, 30 Dec 2025 06:47:11 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9C98119560AB; Tue, 30 Dec 2025 06:47:08 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 04/19] virtio_ring: switch to use vring_virtqueue for virtqueue resize variants Date: Tue, 30 Dec 2025 14:46:34 +0800 Message-ID: <20251230064649.55597-5-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index a32050e1aa47..3fe4c2f63dfa 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1221,11 +1221,10 @@ static struct virtqueue *vring_create_virtqueue_spl= it( return vq; } =20 -static int virtqueue_resize_split(struct virtqueue *_vq, u32 num) +static int virtqueue_resize_split(struct vring_virtqueue *vq, u32 num) { struct vring_virtqueue_split vring_split =3D {}; - struct vring_virtqueue *vq =3D to_vvq(_vq); - struct virtio_device *vdev =3D _vq->vdev; + struct virtio_device *vdev =3D vq->vq.vdev; int err; =20 err =3D vring_alloc_queue_split(&vring_split, vdev, num, @@ -2182,11 +2181,10 @@ static struct virtqueue *vring_create_virtqueue_pac= ked( return vq; } =20 -static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num) +static int virtqueue_resize_packed(struct vring_virtqueue *vq, u32 num) { struct vring_virtqueue_packed vring_packed =3D {}; - struct vring_virtqueue *vq =3D to_vvq(_vq); - struct virtio_device *vdev =3D _vq->vdev; + struct virtio_device *vdev =3D vq->vq.vdev; int err; =20 if (vring_alloc_queue_packed(&vring_packed, vdev, num, vq->map)) @@ -2809,9 +2807,9 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num, recycle_done(_vq); =20 if (vq->packed_ring) - err =3D virtqueue_resize_packed(_vq, num); + err =3D virtqueue_resize_packed(vq, num); else - err =3D virtqueue_resize_split(_vq, num); + err =3D virtqueue_resize_split(vq, num); =20 err_reset =3D virtqueue_enable_after_reset(_vq); if (err_reset) --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 890C32D29C2 for ; Tue, 30 Dec 2025 06:47:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077244; cv=none; b=TYiVJ8TssqAMImZ0rk2vsMD4gOahD74YswLHtN617zRFxXnKbOA8ncG0MAHiu6w3T4lLFZMPiclZ9+MpdiX/pe1KaIBHa+hN3GQT/1flRLjMx9PjufNfB8ud9tVjlicUypUWRWaaZkC2dE8YP9abpl/OJR1ESFtc0v6Rcth2u2k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077244; c=relaxed/simple; bh=9TmMfh6J1dBFzf8Y14rOmfF2Tf60V9zWrJCA38+KOZA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rBoxB7NGQtT7I3byLlTbrA8rLu1KWjQTCOS6CmZ4NwwEeBuepMFB20DGHwDSn1JrV+D3fXNDWyjqlg+hQEJaDgXnSChYsjlxLDd//FMsRnuU0ojzJ3m8XbgXS6P+Tds69KRoe8XJsc6eSGz7VQtv7rRT5RA/V6qSJVua3Y0L35g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MQUNNez7; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MQUNNez7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077240; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zltCyISUNNfpBUMsatwG0H9x3I9DfSVw5ou8yvT8XVU=; b=MQUNNez7AJoG597eKGscEDhUe/+oidpUSH1bF707eYhyTPSJwESZJh3I4qY23d58ehO3ao Eyu8dsKARZ7a2QZ9TVVY4my0ZtbrNWhNzZW0pFlgl0WEtH8tURUtlA5ZnH/s0XKw5s6Lmu gylebo/9IMVsMJdtP85j5JpKSRQQ6os= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-85-MYDqiOvlO0eZp-Ucy5FEzA-1; Tue, 30 Dec 2025 01:47:16 -0500 X-MC-Unique: MYDqiOvlO0eZp-Ucy5FEzA-1 X-Mimecast-MFC-AGG-ID: MYDqiOvlO0eZp-Ucy5FEzA_1767077235 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A24B01955F3F; Tue, 30 Dec 2025 06:47:15 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8C72019560AB; Tue, 30 Dec 2025 06:47:12 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 05/19] virtio_ring: switch to use vring_virtqueue for virtqueue_kick_prepare variants Date: Tue, 30 Dec 2025 14:46:35 +0800 Message-ID: <20251230064649.55597-6-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 3fe4c2f63dfa..d61d71b12776 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -717,9 +717,8 @@ static inline int virtqueue_add_split(struct virtqueue = *_vq, return -ENOMEM; } =20 -static bool virtqueue_kick_prepare_split(struct virtqueue *_vq) +static bool virtqueue_kick_prepare_split(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); u16 new, old; bool needs_kick; =20 @@ -736,12 +735,12 @@ static bool virtqueue_kick_prepare_split(struct virtq= ueue *_vq) LAST_ADD_TIME_INVALID(vq); =20 if (vq->event) { - needs_kick =3D vring_need_event(virtio16_to_cpu(_vq->vdev, + needs_kick =3D vring_need_event(virtio16_to_cpu(vq->vq.vdev, vring_avail_event(&vq->split.vring)), new, old); } else { needs_kick =3D !(vq->split.vring.used->flags & - cpu_to_virtio16(_vq->vdev, + cpu_to_virtio16(vq->vq.vdev, VRING_USED_F_NO_NOTIFY)); } END_USE(vq); @@ -1596,9 +1595,8 @@ static inline int virtqueue_add_packed(struct virtque= ue *_vq, return -EIO; } =20 -static bool virtqueue_kick_prepare_packed(struct virtqueue *_vq) +static bool virtqueue_kick_prepare_packed(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); u16 new, old, off_wrap, flags, wrap_counter, event_idx; bool needs_kick; union { @@ -2457,8 +2455,8 @@ bool virtqueue_kick_prepare(struct virtqueue *_vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_kick_prepare_packed(_vq) : - virtqueue_kick_prepare_split(_vq); + return vq->packed_ring ? virtqueue_kick_prepare_packed(vq) : + virtqueue_kick_prepare_split(vq); } EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64BF32F83A7 for ; Tue, 30 Dec 2025 06:47:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077247; cv=none; b=LUBJ2PsF/tlXPwyUqOw+qLrMNFhPA8r7WUBFHx4GJ7P5vMxk7/2yourgIdxzlZrSXFyMoV9Av82v9ghAgYgFTK5jBFOobsldzP7NEuAWJZsgMscfxGOBh8LFZXAHhPUXM23MtfFZH6x0ZtjAtROUB5e1TGKjYPxJTzCcB2aslAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077247; c=relaxed/simple; bh=RUyE9Fu22Qj6FSaf5wKtZKcJ5ddWx4WPH/GuYpr50s8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=LmxpUoVL1Ned9RleEyRMqMJk1HP21AVUjZrrPSjL8l82H8cAmW1EYywCsJhWnuUBuOxZJoQp94mrEUg5gHiBXYPw2Ol8I986UwZx23DkoQzeg3JF0LcVGm/r0JfFk0HR4Bwpfp8rWtMiMj+558z5Ws4SZaKPNk2BPrgRAXLgmOI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=OSp4ltvX; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OSp4ltvX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077244; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+6paBF/63GBeqjaTVV9F7xVlUyRx5EMXycs7hrxo4wM=; b=OSp4ltvX6J7KRWucF6lWVlSmSEg8dyOdScv+a/fkNxsJ6QaXYLXKHVNHghemiT5hO8875W gyS5HgOSD4Z/+8oodJ5rXafUD/dVK+iqDZ5ZS7wvSwARYKPxBdWmw7l5epC/idWwy8EfoI HjS2CC7QxUyb4YOYe5P3qhQQwmDPHoc= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-185-GkWfSrEEOmWgGDTrDpG61Q-1; Tue, 30 Dec 2025 01:47:20 -0500 X-MC-Unique: GkWfSrEEOmWgGDTrDpG61Q-1 X-Mimecast-MFC-AGG-ID: GkWfSrEEOmWgGDTrDpG61Q_1767077239 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6E46818002ED; Tue, 30 Dec 2025 06:47:19 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 70F3419560AB; Tue, 30 Dec 2025 06:47:16 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 06/19] virtio_ring: switch to use vring_virtqueue for virtqueue_add variants Date: Tue, 30 Dec 2025 14:46:36 +0800 Message-ID: <20251230064649.55597-7-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 39 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index d61d71b12776..f200d812416e 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -476,7 +476,7 @@ static unsigned int vring_unmap_one_split(const struct = vring_virtqueue *vq, return extra->next; } =20 -static struct vring_desc *alloc_indirect_split(struct virtqueue *_vq, +static struct vring_desc *alloc_indirect_split(struct vring_virtqueue *vq, unsigned int total_sg, gfp_t gfp) { @@ -505,7 +505,7 @@ static struct vring_desc *alloc_indirect_split(struct v= irtqueue *_vq, return desc; } =20 -static inline unsigned int virtqueue_add_desc_split(struct virtqueue *vq, +static inline unsigned int virtqueue_add_desc_split(struct vring_virtqueue= *vq, struct vring_desc *desc, struct vring_desc_extra *extra, unsigned int i, @@ -513,11 +513,12 @@ static inline unsigned int virtqueue_add_desc_split(s= truct virtqueue *vq, unsigned int len, u16 flags, bool premapped) { + struct virtio_device *vdev =3D vq->vq.vdev; u16 next; =20 - desc[i].flags =3D cpu_to_virtio16(vq->vdev, flags); - desc[i].addr =3D cpu_to_virtio64(vq->vdev, addr); - desc[i].len =3D cpu_to_virtio32(vq->vdev, len); + desc[i].flags =3D cpu_to_virtio16(vdev, flags); + desc[i].addr =3D cpu_to_virtio64(vdev, addr); + desc[i].len =3D cpu_to_virtio32(vdev, len); =20 extra[i].addr =3D premapped ? DMA_MAPPING_ERROR : addr; extra[i].len =3D len; @@ -525,12 +526,12 @@ static inline unsigned int virtqueue_add_desc_split(s= truct virtqueue *vq, =20 next =3D extra[i].next; =20 - desc[i].next =3D cpu_to_virtio16(vq->vdev, next); + desc[i].next =3D cpu_to_virtio16(vdev, next); =20 return next; } =20 -static inline int virtqueue_add_split(struct virtqueue *_vq, +static inline int virtqueue_add_split(struct vring_virtqueue *vq, struct scatterlist *sgs[], unsigned int total_sg, unsigned int out_sgs, @@ -540,7 +541,6 @@ static inline int virtqueue_add_split(struct virtqueue = *_vq, bool premapped, gfp_t gfp) { - struct vring_virtqueue *vq =3D to_vvq(_vq); struct vring_desc_extra *extra; struct scatterlist *sg; struct vring_desc *desc; @@ -565,7 +565,7 @@ static inline int virtqueue_add_split(struct virtqueue = *_vq, head =3D vq->free_head; =20 if (virtqueue_use_indirect(vq, total_sg)) - desc =3D alloc_indirect_split(_vq, total_sg, gfp); + desc =3D alloc_indirect_split(vq, total_sg, gfp); else { desc =3D NULL; WARN_ON_ONCE(total_sg > vq->split.vring.num && !vq->indirect); @@ -612,7 +612,7 @@ static inline int virtqueue_add_split(struct virtqueue = *_vq, /* Note that we trust indirect descriptor * table since it use stream DMA mapping. */ - i =3D virtqueue_add_desc_split(_vq, desc, extra, i, addr, len, + i =3D virtqueue_add_desc_split(vq, desc, extra, i, addr, len, VRING_DESC_F_NEXT, premapped); } @@ -629,14 +629,14 @@ static inline int virtqueue_add_split(struct virtqueu= e *_vq, /* Note that we trust indirect descriptor * table since it use stream DMA mapping. */ - i =3D virtqueue_add_desc_split(_vq, desc, extra, i, addr, len, + i =3D virtqueue_add_desc_split(vq, desc, extra, i, addr, len, VRING_DESC_F_NEXT | VRING_DESC_F_WRITE, premapped); } } /* Last one doesn't continue. */ - desc[prev].flags &=3D cpu_to_virtio16(_vq->vdev, ~VRING_DESC_F_NEXT); + desc[prev].flags &=3D cpu_to_virtio16(vq->vq.vdev, ~VRING_DESC_F_NEXT); if (!indirect && vring_need_unmap_buffer(vq, &extra[prev])) vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=3D ~VRING_DESC_F_NEXT; @@ -649,7 +649,7 @@ static inline int virtqueue_add_split(struct virtqueue = *_vq, if (vring_mapping_error(vq, addr)) goto unmap_release; =20 - virtqueue_add_desc_split(_vq, vq->split.vring.desc, + virtqueue_add_desc_split(vq, vq->split.vring.desc, vq->split.desc_extra, head, addr, total_sg * sizeof(struct vring_desc), @@ -675,13 +675,13 @@ static inline int virtqueue_add_split(struct virtqueu= e *_vq, /* Put entry in available array (but don't update avail->idx until they * do sync). */ avail =3D vq->split.avail_idx_shadow & (vq->split.vring.num - 1); - vq->split.vring.avail->ring[avail] =3D cpu_to_virtio16(_vq->vdev, head); + vq->split.vring.avail->ring[avail] =3D cpu_to_virtio16(vq->vq.vdev, head); =20 /* Descriptors and available array need to be set before we expose the * new available array entries. */ virtio_wmb(vq->weak_barriers); vq->split.avail_idx_shadow++; - vq->split.vring.avail->idx =3D cpu_to_virtio16(_vq->vdev, + vq->split.vring.avail->idx =3D cpu_to_virtio16(vq->vq.vdev, vq->split.avail_idx_shadow); vq->num_added++; =20 @@ -691,7 +691,7 @@ static inline int virtqueue_add_split(struct virtqueue = *_vq, /* This is very unlikely, but theoretically possible. Kick * just in case. */ if (unlikely(vq->num_added =3D=3D (1 << 16) - 1)) - virtqueue_kick(_vq); + virtqueue_kick(&vq->vq); =20 return 0; =20 @@ -1440,7 +1440,7 @@ static int virtqueue_add_indirect_packed(struct vring= _virtqueue *vq, return -ENOMEM; } =20 -static inline int virtqueue_add_packed(struct virtqueue *_vq, +static inline int virtqueue_add_packed(struct vring_virtqueue *vq, struct scatterlist *sgs[], unsigned int total_sg, unsigned int out_sgs, @@ -1450,7 +1450,6 @@ static inline int virtqueue_add_packed(struct virtque= ue *_vq, bool premapped, gfp_t gfp) { - struct vring_virtqueue *vq =3D to_vvq(_vq); struct vring_packed_desc *desc; struct scatterlist *sg; unsigned int i, n, c, descs_used, err_idx, len; @@ -2262,9 +2261,9 @@ static inline int virtqueue_add(struct virtqueue *_vq, { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_add_packed(_vq, sgs, total_sg, + return vq->packed_ring ? virtqueue_add_packed(vq, sgs, total_sg, out_sgs, in_sgs, data, ctx, premapped, gfp) : - virtqueue_add_split(_vq, sgs, total_sg, + virtqueue_add_split(vq, sgs, total_sg, out_sgs, in_sgs, data, ctx, premapped, gfp); } =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3A8302F90D8 for ; Tue, 30 Dec 2025 06:47:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077252; cv=none; b=rHMHSw02FY9exTM7VeLNC1pR3hdgZLTm4krBo8U4Spi66oQtcFZ/OB13XAqgq49ELgrnZ9BLctLkk4f6CWlD8/ksjyOVdluHanIxkPqgkbKbLDoT3AxSejOsV3fjvA2rdYD9iRH3C1JmbGLh+gIr2729iRRT3jbwb6WYJtLElDI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077252; c=relaxed/simple; bh=3VKRyp9kQWQPpjtHHEnSupQRYQa424uZZ/1gyfGM2yg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IvZHUykxEUMa9Oipv/BJnfOZq0OH3k7dGsKu8npYvS/l0VtQ7y5HFi/3mwZze+9JrL2e1Y5Dreobv6xTnjZV5Ta1TC9Dq575eM50+nGhPTz55borHdng3If6jJy/bRsLGA2+SoVMa378w7C4uJFcKmUxvZouhD4W5ZBIEaUsACg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cu3O1ewd; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cu3O1ewd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077249; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b9kEUufqGkJG1G/ClpQXVED7dV+XSDtVRJvxPgy80FI=; b=cu3O1ewdDt76PpmZkstEW0D0Xv1aGiyskjWwrCnBeIQYdh2UL5/yQ9aGZSE9egMN65mOCh 51nxniIRhn7cQfr9LPZGAvLSIQp+Pt+UGv3kB8Rb+ZgEdN+3KYHgHXF5m3p7O1RBdffA4P 0X+0E04CZlxgfpckzxdLy9ow0J2rues= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-633-HE37ems2PCiBtKkTiJ7J1Q-1; Tue, 30 Dec 2025 01:47:25 -0500 X-MC-Unique: HE37ems2PCiBtKkTiJ7J1Q-1 X-Mimecast-MFC-AGG-ID: HE37ems2PCiBtKkTiJ7J1Q_1767077244 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 76ABF195DE61; Tue, 30 Dec 2025 06:47:23 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2486219560AB; Tue, 30 Dec 2025 06:47:19 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 07/19] virtio: switch to use vring_virtqueue for virtqueue_get variants Date: Tue, 30 Dec 2025 14:46:37 +0800 Message-ID: <20251230064649.55597-8-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index f200d812416e..3fd65b1177a3 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -817,11 +817,10 @@ static bool more_used_split(const struct vring_virtqu= eue *vq) return virtqueue_poll_split(vq, vq->last_used_idx); } =20 -static void *virtqueue_get_buf_ctx_split(struct virtqueue *_vq, +static void *virtqueue_get_buf_ctx_split(struct vring_virtqueue *vq, unsigned int *len, void **ctx) { - struct vring_virtqueue *vq =3D to_vvq(_vq); void *ret; unsigned int i; u16 last_used; @@ -843,9 +842,9 @@ static void *virtqueue_get_buf_ctx_split(struct virtque= ue *_vq, virtio_rmb(vq->weak_barriers); =20 last_used =3D (vq->last_used_idx & (vq->split.vring.num - 1)); - i =3D virtio32_to_cpu(_vq->vdev, + i =3D virtio32_to_cpu(vq->vq.vdev, vq->split.vring.used->ring[last_used].id); - *len =3D virtio32_to_cpu(_vq->vdev, + *len =3D virtio32_to_cpu(vq->vq.vdev, vq->split.vring.used->ring[last_used].len); =20 if (unlikely(i >=3D vq->split.vring.num)) { @@ -867,7 +866,7 @@ static void *virtqueue_get_buf_ctx_split(struct virtque= ue *_vq, if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) virtio_store_mb(vq->weak_barriers, &vring_used_event(&vq->split.vring), - cpu_to_virtio16(_vq->vdev, vq->last_used_idx)); + cpu_to_virtio16(vq->vq.vdev, vq->last_used_idx)); =20 LAST_ADD_TIME_INVALID(vq); =20 @@ -1721,11 +1720,10 @@ static bool more_used_packed(const struct vring_vir= tqueue *vq) return virtqueue_poll_packed(vq, READ_ONCE(vq->last_used_idx)); } =20 -static void *virtqueue_get_buf_ctx_packed(struct virtqueue *_vq, +static void *virtqueue_get_buf_ctx_packed(struct vring_virtqueue *vq, unsigned int *len, void **ctx) { - struct vring_virtqueue *vq =3D to_vvq(_vq); u16 last_used, id, last_used_idx; bool used_wrap_counter; void *ret; @@ -2525,8 +2523,8 @@ void *virtqueue_get_buf_ctx(struct virtqueue *_vq, un= signed int *len, { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx) : - virtqueue_get_buf_ctx_split(_vq, len, ctx); + return vq->packed_ring ? virtqueue_get_buf_ctx_packed(vq, len, ctx) : + virtqueue_get_buf_ctx_split(vq, len, ctx); } EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 586862F8BC3 for ; Tue, 30 Dec 2025 06:47:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077253; cv=none; b=CxF4aKnbrF9wkr0w0JII2vBZw2AVTp2C7gQSJ5CqHmpcg+2MtH+T9fc9168jaz1yBcTJpV9liiMtTAFFoUHPZsjdI+2StOXL4cKQJEust2XjltHXspWWjOxYxOqirdPxWlxy773hXMwm/y3dy6k94TwtiCZdZk7kCrVDy6nOWAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077253; c=relaxed/simple; bh=5Dfj5IOBvxhWPF1wbe8qoF0YyD/64MDtHXXBVShzG4M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IMQ/0h7DRbCN99bgOiLk92JQAPht27tk0rNyaivTannbMQwyYftoTOu+tlLvp7sPTzHHYyH1S4IvQljv9/0PDC4GbSzg8gy42pCuo4TFOBhMNcUD47hY+oZnJ/Iy6Np1SYq4NOmRj7FhnzURaBNfjSgzVYjwB41UR68Y4Dz0scE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LO0w5kuN; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LO0w5kuN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077250; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ndAUlC27ozZob57i45Gbec3x37MLWrFO1aO92A9reJA=; b=LO0w5kuNjZ1WOHxVkDIxSYOLyRznrjsz4fFR1G4hgTFedXPA5tL78WJjiES/hGCQPvs8r+ zgvJPDp9XBumcR6I/5jI6gW7wRgVjyigKfyzZD+r8DVTi+8x7vCOmCdogyqSMtxz8LsEz4 AaOualiDrh8xnpkHkCxbpdGeo3KC3wU= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-437-01nr5WWiMCukj_6FT6guzA-1; Tue, 30 Dec 2025 01:47:28 -0500 X-MC-Unique: 01nr5WWiMCukj_6FT6guzA-1 X-Mimecast-MFC-AGG-ID: 01nr5WWiMCukj_6FT6guzA_1767077247 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2BC1118002C9; Tue, 30 Dec 2025 06:47:27 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 438AC1956053; Tue, 30 Dec 2025 06:47:23 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 08/19] virtio_ring: switch to use vring_virtqueue for enable_cb_prepare variants Date: Tue, 30 Dec 2025 14:46:38 +0800 Message-ID: <20251230064649.55597-9-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 3fd65b1177a3..8ffe7daa1e5f 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -898,9 +898,8 @@ static void virtqueue_disable_cb_split(struct virtqueue= *_vq) } } =20 -static unsigned int virtqueue_enable_cb_prepare_split(struct virtqueue *_v= q) +static unsigned int virtqueue_enable_cb_prepare_split(struct vring_virtque= ue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); u16 last_used_idx; =20 START_USE(vq); @@ -914,10 +913,10 @@ static unsigned int virtqueue_enable_cb_prepare_split= (struct virtqueue *_vq) vq->split.avail_flags_shadow &=3D ~VRING_AVAIL_F_NO_INTERRUPT; if (!vq->event) vq->split.vring.avail->flags =3D - cpu_to_virtio16(_vq->vdev, + cpu_to_virtio16(vq->vq.vdev, vq->split.avail_flags_shadow); } - vring_used_event(&vq->split.vring) =3D cpu_to_virtio16(_vq->vdev, + vring_used_event(&vq->split.vring) =3D cpu_to_virtio16(vq->vq.vdev, last_used_idx =3D vq->last_used_idx); END_USE(vq); return last_used_idx; @@ -1807,10 +1806,8 @@ static void virtqueue_disable_cb_packed(struct virtq= ueue *_vq) } } =20 -static unsigned int virtqueue_enable_cb_prepare_packed(struct virtqueue *_= vq) +static unsigned int virtqueue_enable_cb_prepare_packed(struct vring_virtqu= eue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); - START_USE(vq); =20 /* @@ -2572,8 +2569,8 @@ unsigned int virtqueue_enable_cb_prepare(struct virtq= ueue *_vq) if (vq->event_triggered) vq->event_triggered =3D false; =20 - return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(_vq) : - virtqueue_enable_cb_prepare_split(_vq); + return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(vq) : + virtqueue_enable_cb_prepare_split(vq); } EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5ADA923ED5B for ; Tue, 30 Dec 2025 06:47:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077256; cv=none; b=FFmlfRXwqCmCvZFJxKaBB4UBWqSdmk1CmiwLeDjuS4JARFPJA6EXeuXxZSz8bwfngmmuqf5E7qLstj8/jtqkgzV5SlGuzJ2ZCZrGhulc5MZE513kmkDoBRK2eLXgL9SL1LLac9+MoRdmX9zjEBx18ajBq6waNVdCmz96HpjPTBg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077256; c=relaxed/simple; bh=gBHebrMoKJITwEefOtcmwnB1A793iYd1frO9uIC0rEw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Q5NJMVLYGozby9XnXfoUf2nDwrfIXzprOZKzTxIJcE/hY2b2SMp1bymQ8NaEnIGzwUfU6ZbYjwdgi8aVEWExwBOkjJthkTxRp/p/cu3Mt5ts5qAc9x41ldL7cmCUczEobQaKA1aYqlzzFc56rHxvcNQxfItk219ZpxQyoANGlxY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Z8ARYlsh; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Z8ARYlsh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077253; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2QV3PF5uZrU6S5SJP/dZWzDFLrf6/r2tdzoIeK9vlmU=; b=Z8ARYlshD2+7pF5ERBZgo1z4S4ofIgx25E7nsJNLNMi9XVnDWWAak4xdZoZRmUrCxKm2di ZRv4CL73sGQV4Wd1/Mpvjutwud/q22XMAxGyKuULwg1JEvWWnGyWW0j2+9JCCaAgtP0Sfz QBRTJqHedspcoA+c2DzGvSMtfiwpU1w= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-BVdfx4oiOAyQds2nQW8Vyw-1; Tue, 30 Dec 2025 01:47:31 -0500 X-MC-Unique: BVdfx4oiOAyQds2nQW8Vyw-1 X-Mimecast-MFC-AGG-ID: BVdfx4oiOAyQds2nQW8Vyw_1767077251 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E1F3B1955F34; Tue, 30 Dec 2025 06:47:30 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E324419560AB; Tue, 30 Dec 2025 06:47:27 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 09/19] virtio_ring: use vring_virtqueue for enable_cb_delayed variants Date: Tue, 30 Dec 2025 14:46:39 +0800 Message-ID: <20251230064649.55597-10-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 8ffe7daa1e5f..00de30862777 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -922,9 +922,8 @@ static unsigned int virtqueue_enable_cb_prepare_split(s= truct vring_virtqueue *vq return last_used_idx; } =20 -static bool virtqueue_enable_cb_delayed_split(struct virtqueue *_vq) +static bool virtqueue_enable_cb_delayed_split(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); u16 bufs; =20 START_USE(vq); @@ -938,7 +937,7 @@ static bool virtqueue_enable_cb_delayed_split(struct vi= rtqueue *_vq) vq->split.avail_flags_shadow &=3D ~VRING_AVAIL_F_NO_INTERRUPT; if (!vq->event) vq->split.vring.avail->flags =3D - cpu_to_virtio16(_vq->vdev, + cpu_to_virtio16(vq->vq.vdev, vq->split.avail_flags_shadow); } /* TODO: tune this threshold */ @@ -946,9 +945,9 @@ static bool virtqueue_enable_cb_delayed_split(struct vi= rtqueue *_vq) =20 virtio_store_mb(vq->weak_barriers, &vring_used_event(&vq->split.vring), - cpu_to_virtio16(_vq->vdev, vq->last_used_idx + bufs)); + cpu_to_virtio16(vq->vq.vdev, vq->last_used_idx + bufs)); =20 - if (unlikely((u16)(virtio16_to_cpu(_vq->vdev, vq->split.vring.used->idx) + if (unlikely((u16)(virtio16_to_cpu(vq->vq.vdev, vq->split.vring.used->idx) - vq->last_used_idx) > bufs)) { END_USE(vq); return false; @@ -1837,9 +1836,8 @@ static unsigned int virtqueue_enable_cb_prepare_packe= d(struct vring_virtqueue *v return vq->last_used_idx; } =20 -static bool virtqueue_enable_cb_delayed_packed(struct virtqueue *_vq) +static bool virtqueue_enable_cb_delayed_packed(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); u16 used_idx, wrap_counter, last_used_idx; u16 bufs; =20 @@ -2635,8 +2633,8 @@ bool virtqueue_enable_cb_delayed(struct virtqueue *_v= q) if (vq->event_triggered) data_race(vq->event_triggered =3D false); =20 - return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(_vq) : - virtqueue_enable_cb_delayed_split(_vq); + return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(vq) : + virtqueue_enable_cb_delayed_split(vq); } EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 66A0D2F9DA1 for ; Tue, 30 Dec 2025 06:47:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077262; cv=none; b=Fl0HEWD+OpWchohJdOVqDlzEAmIzpPJJmGe0ANCH5n0axoLlsrvNcj2GIt2ULZN011HwAdZ2qOjvLT5CsrqBhGHG1u3t9bZmM8TuOQS2apVf99H6vhsFmZUzvDIg2htfNO1OohxFhCAYIYtooy57ipHQ/yZxAcpAWLTUDtyhARo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077262; c=relaxed/simple; bh=4J9oD5erJEAiDzZkvnsGsYSiKWVEmbakyVvZhYirf3w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EXN/0ep4XCyJncfuuOUPJB2Ac8JG1t6yZoqf5fPfIqGHgfgrwTHFuh8odUIczixOZxMTX2ss8kjjoguAHJF0QLHzrEXen9cfPsXZp6CiLWBVj9aDtd7s6x7XRsN09Yui/SWUmh3KRsMCRHlmARA/sFD/0oeN+f5vGp2pJX8YcFY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=RuwO6qm/; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RuwO6qm/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077260; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/e1enlQjSwfmyQkTZMSi4igBcCel8J22/QnS+o2docE=; b=RuwO6qm/W+VenuZA1V12kxVdQkU+y1zgwMidYpKOFEUs60bKcGLOQ+vNyABOQAH+JmHqaX EbtQCVbWH5LGBZ7T3ntLOwyTi/qa+tcMQqcz7u59p+B+fjfiQFKSGn5WMlfvgBKfS/Nit6 GaVn0K2BF8nSQ19/0YJSa4evEEJm79I= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-155-MZKWFjIpPOKK0QFkWdmYzg-1; Tue, 30 Dec 2025 01:47:35 -0500 X-MC-Unique: MZKWFjIpPOKK0QFkWdmYzg-1 X-Mimecast-MFC-AGG-ID: MZKWFjIpPOKK0QFkWdmYzg_1767077254 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 95603195605A; Tue, 30 Dec 2025 06:47:34 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9DDD819560AB; Tue, 30 Dec 2025 06:47:31 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 10/19] virtio_ring: switch to use vring_virtqueue for disable_cb variants Date: Tue, 30 Dec 2025 14:46:40 +0800 Message-ID: <20251230064649.55597-11-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 00de30862777..a4d084f5ca95 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -874,10 +874,8 @@ static void *virtqueue_get_buf_ctx_split(struct vring_= virtqueue *vq, return ret; } =20 -static void virtqueue_disable_cb_split(struct virtqueue *_vq) +static void virtqueue_disable_cb_split(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); - if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { vq->split.avail_flags_shadow |=3D VRING_AVAIL_F_NO_INTERRUPT; =20 @@ -893,7 +891,7 @@ static void virtqueue_disable_cb_split(struct virtqueue= *_vq) vring_used_event(&vq->split.vring) =3D 0x0; else vq->split.vring.avail->flags =3D - cpu_to_virtio16(_vq->vdev, + cpu_to_virtio16(vq->vq.vdev, vq->split.avail_flags_shadow); } } @@ -1786,10 +1784,8 @@ static void *virtqueue_get_buf_ctx_packed(struct vri= ng_virtqueue *vq, return ret; } =20 -static void virtqueue_disable_cb_packed(struct virtqueue *_vq) +static void virtqueue_disable_cb_packed(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); - if (vq->packed.event_flags_shadow !=3D VRING_PACKED_EVENT_FLAG_DISABLE) { vq->packed.event_flags_shadow =3D VRING_PACKED_EVENT_FLAG_DISABLE; =20 @@ -2542,9 +2538,9 @@ void virtqueue_disable_cb(struct virtqueue *_vq) struct vring_virtqueue *vq =3D to_vvq(_vq); =20 if (vq->packed_ring) - virtqueue_disable_cb_packed(_vq); + virtqueue_disable_cb_packed(vq); else - virtqueue_disable_cb_split(_vq); + virtqueue_disable_cb_split(vq); } EXPORT_SYMBOL_GPL(virtqueue_disable_cb); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 222F52F8BC3 for ; Tue, 30 Dec 2025 06:47:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077263; cv=none; b=PsNCGAkqO5MgN75mP8MM68H4RlAnB5sQRa2vKkxGmpUCpkZEDL/jcvTe46vKv/k+Tqb9Qi8UlP5eoqRY80HDzWCf2LNxrWAw1qHkcB1/p19e30inNalhl7g0cZ6HWeaA9hcF0fkiQqPWnEnatTPiwpFENq4iMePrIumrD/04Yp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077263; c=relaxed/simple; bh=uyo+y8yMNzRePczbBs8BkNYCElPAnblW8pyjqupRq/Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mYMlQLd7r8/U8ILa3cMDOuuSxj9vRnQb7voYERtU3tnkCOpl6Asz4xCR9wEL/61uRpYOsFHjG8ZZwBy7NHOoXakeIF0wYFqwaLExMULKdA2tVVAPxFuS4ugZzh1WGhxGgW0w9dj0vNtTMet7EM+FuqidHBasLGZDaM5zTqtONsc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=TRejh7E7; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TRejh7E7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077261; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=NhozjM3g2hApOE+D4QB0hdPzyPjgmFJvFsVo9a0LCYs=; b=TRejh7E7X6gqJ7d21KbQSp7ASC4O7Hpn42+SxZCMLdTME42p4UVR7cZK0V+ZI+cgQiKpOt nJgKG/Ng6uMwS8wv775jrHy/13Yna86Wgbm3wWrL0J5hRgPKeZPTApAShMolx6CnSNzrQf TcEI7Mr2zyD9zMMoLa85lxH8HL1nUfE= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-672-NOjL808FMVmwapPVNZEo-g-1; Tue, 30 Dec 2025 01:47:39 -0500 X-MC-Unique: NOjL808FMVmwapPVNZEo-g-1 X-Mimecast-MFC-AGG-ID: NOjL808FMVmwapPVNZEo-g_1767077258 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2F6FF1956095; Tue, 30 Dec 2025 06:47:38 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4B27E19560AB; Tue, 30 Dec 2025 06:47:34 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 11/19] virtio_ring: switch to use vring_virtqueue for detach_unused_buf variants Date: Tue, 30 Dec 2025 14:46:41 +0800 Message-ID: <20251230064649.55597-12-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Those variants are used internally so let's switch to use vring_virtqueue as parameter to be consistent with other internal virtqueue helpers. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index a4d084f5ca95..db5cdb5fcb20 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -955,9 +955,8 @@ static bool virtqueue_enable_cb_delayed_split(struct vr= ing_virtqueue *vq) return true; } =20 -static void *virtqueue_detach_unused_buf_split(struct virtqueue *_vq) +static void *virtqueue_detach_unused_buf_split(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); unsigned int i; void *buf; =20 @@ -970,7 +969,7 @@ static void *virtqueue_detach_unused_buf_split(struct v= irtqueue *_vq) buf =3D vq->split.desc_state[i].data; detach_buf_split(vq, i, NULL); vq->split.avail_idx_shadow--; - vq->split.vring.avail->idx =3D cpu_to_virtio16(_vq->vdev, + vq->split.vring.avail->idx =3D cpu_to_virtio16(vq->vq.vdev, vq->split.avail_idx_shadow); END_USE(vq); return buf; @@ -1892,9 +1891,8 @@ static bool virtqueue_enable_cb_delayed_packed(struct= vring_virtqueue *vq) return true; } =20 -static void *virtqueue_detach_unused_buf_packed(struct virtqueue *_vq) +static void *virtqueue_detach_unused_buf_packed(struct vring_virtqueue *vq) { - struct vring_virtqueue *vq =3D to_vvq(_vq); unsigned int i; void *buf; =20 @@ -2646,8 +2644,8 @@ void *virtqueue_detach_unused_buf(struct virtqueue *_= vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq) : - virtqueue_detach_unused_buf_split(_vq); + return vq->packed_ring ? virtqueue_detach_unused_buf_packed(vq) : + virtqueue_detach_unused_buf_split(vq); } EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 430872FC000 for ; Tue, 30 Dec 2025 06:47:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077272; cv=none; b=Oshd93Hc1Cm8tOEUA6uInRiHtw5GKaWGec0RUDCIkJ5yVsH5Y4TVqs37BSrkAUQLJKSWLCCrJbnifHHEHK5lavknX9KGQRFjWcPOeimJcJtOyFlETMU2vM8mz9r8l5sFzvtHc/Kr03rBZWPFcIve12ETNaVhYF6gOywzS5qsYe0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077272; c=relaxed/simple; bh=7+M6FY0CrnWAV+hhAExp3CegwI0pm5qlnSpU5bH5yfU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gF8ux9UhxjsklSlBBM7bb8t2IRB6W+NAktjM8smJz+JmNncbLiwMSBod0JyMJTEyZX4Kc5lLFpiKQHpyGAeR5oLnJTErKFhgIn1DNWamB8z33q2Q+bSmsgKyKCC/FT+ncsem5wytumIjmu71k69bT6kvkehSgelOuoY5h41lVm4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=AzP0pQSW; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="AzP0pQSW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077269; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v7SW6y3BaocdVTeTcaXd7PTcuFbJm5a3JDZdYiqSd8E=; b=AzP0pQSWjziJeoVWrYNBjJSmDF6utTJXyViFlUdHNuQypkSOhrjGj2WbhQEYo7qIoGhiik BTRT3xVr0xIjvZ0UKh2ZOWtsO3U46G9bJPZeQqZIoc/M1VdcPI8zPGzoJQ7ctoyLt6fhdC 3DoYtOTvd61WG9eTLXAilZtPX++J5U0= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-307-BKmWm6RTNnCMtfC5vIkRfQ-1; Tue, 30 Dec 2025 01:47:42 -0500 X-MC-Unique: BKmWm6RTNnCMtfC5vIkRfQ-1 X-Mimecast-MFC-AGG-ID: BKmWm6RTNnCMtfC5vIkRfQ_1767077261 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C971F18002ED; Tue, 30 Dec 2025 06:47:41 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DB68219560AB; Tue, 30 Dec 2025 06:47:38 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 12/19] virtio_ring: switch to use unsigned int for virtqueue_poll_packed() Date: Tue, 30 Dec 2025 14:46:42 +0800 Message-ID: <20251230064649.55597-13-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Type: text/plain; charset="utf-8" Switch to use unsigned int for virtqueue_poll_packed() to match virtqueue_poll() and virtqueue_poll_split() and to ease the abstraction of the virtqueue ops. Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index db5cdb5fcb20..f804c5c6975a 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1699,7 +1699,8 @@ static inline bool is_used_desc_packed(const struct v= ring_virtqueue *vq, return avail =3D=3D used && used =3D=3D used_wrap_counter; } =20 -static bool virtqueue_poll_packed(const struct vring_virtqueue *vq, u16 of= f_wrap) +static bool virtqueue_poll_packed(const struct vring_virtqueue *vq, + unsigned int off_wrap) { bool wrap_counter; u16 used_idx; --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F1E02FD1DC for ; Tue, 30 Dec 2025 06:47:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077271; cv=none; b=duoke5n07Mnoq2dShswdW88KREEwaPT7WYvkeQQhTy2+F6y+bdRDenz7rCoZd/rbVRbYtV9T0dZm/90bFs2AoLJD6D6GXluwug6fMdSmIcmkMuljAx9hSCIvttiXuZB8oNbWNdETLnag/Riza5KNv8TsfXUyIaHouo2/seNlXdE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077271; c=relaxed/simple; bh=B6/TDIMTjPIvLyUM5297rlUiOg9uSBwU7jV3aWvGStk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cxXaDafFi1nM9ou+fQBWGczOTF6pwZJ8xuy+JQ8kpcPxLpCZ1//MQNmJQUYbe8v13F70R7KyYLDQeMDMpC2+ELbbRGULVAMW5If+a3n+3GwZRFrfxAvePXr7RcsBjyv2alyJ7KJi/v1jV+k0BnQVwahSXxPQygX8Cq89bN7S35s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=PbU2MWIQ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PbU2MWIQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077268; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SwUK7JNJ7dztDPY7mJfS4dgHILDv7jovpWbhg+P+T1w=; b=PbU2MWIQ/gDM938PNWvcwfb8ZyoShv+JY2mrLUd9FI+/vF1XWC4VJ8BuaLywJelrYGt3Sh IF0R1HCyHqJZuFLvqzIpz5GlhirM5H1DK+CmP1BS8jHSEAQT8hzq2WHyQ8AdfzabCnrwCz g0WkCG9y29kQGXoQx9jiw0ivehSuxUo= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-16-dmqEFXGbN4eRop5xuUqomg-1; Tue, 30 Dec 2025 01:47:46 -0500 X-MC-Unique: dmqEFXGbN4eRop5xuUqomg-1 X-Mimecast-MFC-AGG-ID: dmqEFXGbN4eRop5xuUqomg_1767077265 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CBBE3195608F; Tue, 30 Dec 2025 06:47:45 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AD6FE19560AB; Tue, 30 Dec 2025 06:47:42 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 13/19] virtio_ring: introduce virtqueue ops Date: Tue, 30 Dec 2025 14:46:43 +0800 Message-ID: <20251230064649.55597-14-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 This patch introduces virtqueue ops which is a set of callbacks that will be called for different queue layout or features. This would help to avoid branches for split/packed and will ease the future implementation like in order. Note that in order to eliminate the indirect calls this patch uses global array of const ops to allow compiler to avoid indirect branches. Tested with CONFIG_MITIGATION_RETPOLINE, no performance differences were noticed. Acked-by: Eugenio P=C3=A9rez Suggested-by: Michael S. Tsirkin Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 171 ++++++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 44 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index f804c5c6975a..29174a77eacd 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -67,6 +67,11 @@ #define LAST_ADD_TIME_INVALID(vq) #endif =20 +enum vq_layout { + VQ_LAYOUT_SPLIT =3D 0, + VQ_LAYOUT_PACKED, +}; + struct vring_desc_state_split { void *data; /* Data for callback. */ =20 @@ -159,12 +164,29 @@ struct vring_virtqueue_packed { size_t event_size_in_bytes; }; =20 +struct vring_virtqueue; + +struct virtqueue_ops { + int (*add)(struct vring_virtqueue *vq, struct scatterlist *sgs[], + unsigned int total_sg, unsigned int out_sgs, + unsigned int in_sgs, void *data, + void *ctx, bool premapped, gfp_t gfp); + void *(*get)(struct vring_virtqueue *vq, unsigned int *len, void **ctx); + bool (*kick_prepare)(struct vring_virtqueue *vq); + void (*disable_cb)(struct vring_virtqueue *vq); + bool (*enable_cb_delayed)(struct vring_virtqueue *vq); + unsigned int (*enable_cb_prepare)(struct vring_virtqueue *vq); + bool (*poll)(const struct vring_virtqueue *vq, + unsigned int last_used_idx); + void *(*detach_unused_buf)(struct vring_virtqueue *vq); + bool (*more_used)(const struct vring_virtqueue *vq); + int (*resize)(struct vring_virtqueue *vq, u32 num); + void (*reset)(struct vring_virtqueue *vq); +}; + struct vring_virtqueue { struct virtqueue vq; =20 - /* Is this a packed ring? */ - bool packed_ring; - /* Is DMA API used? */ bool use_map_api; =20 @@ -180,6 +202,8 @@ struct vring_virtqueue { /* Host publishes avail event idx */ bool event; =20 + enum vq_layout layout; + /* Head of free buffer list. */ unsigned int free_head; /* Number we've added since last sync. */ @@ -231,6 +255,12 @@ static void vring_free(struct virtqueue *_vq); =20 #define to_vvq(_vq) container_of_const(_vq, struct vring_virtqueue, vq) =20 + +static inline bool virtqueue_is_packed(const struct vring_virtqueue *vq) +{ + return vq->layout =3D=3D VQ_LAYOUT_PACKED; +} + static bool virtqueue_use_indirect(const struct vring_virtqueue *vq, unsigned int total_sg) { @@ -433,7 +463,7 @@ static void virtqueue_init(struct vring_virtqueue *vq, = u32 num) { vq->vq.num_free =3D num; =20 - if (vq->packed_ring) + if (virtqueue_is_packed(vq)) vq->last_used_idx =3D 0 | (1 << VRING_PACKED_EVENT_F_WRAP_CTR); else vq->last_used_idx =3D 0; @@ -1122,6 +1152,8 @@ static int vring_alloc_queue_split(struct vring_virtq= ueue_split *vring_split, return 0; } =20 +static const struct virtqueue_ops split_ops; + static struct virtqueue *__vring_new_virtqueue_split(unsigned int index, struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, @@ -1139,7 +1171,7 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, if (!vq) return NULL; =20 - vq->packed_ring =3D false; + vq->layout =3D VQ_LAYOUT_SPLIT; vq->vq.callback =3D callback; vq->vq.vdev =3D vdev; vq->vq.name =3D name; @@ -2072,11 +2104,12 @@ static void virtqueue_reset_packed(struct vring_vir= tqueue *vq) =20 /* we need to reset the desc.flags. For more, see is_used_desc_packed() */ memset(vq->packed.vring.desc, 0, vq->packed.ring_size_in_bytes); - virtqueue_init(vq, vq->packed.vring.num); virtqueue_vring_init_packed(&vq->packed, !!vq->vq.callback); } =20 +static const struct virtqueue_ops packed_ops; + static struct virtqueue *__vring_new_virtqueue_packed(unsigned int index, struct vring_virtqueue_packed *vring_packed, struct virtio_device *vdev, @@ -2107,7 +2140,7 @@ static struct virtqueue *__vring_new_virtqueue_packed= (unsigned int index, #else vq->broken =3D false; #endif - vq->packed_ring =3D true; + vq->layout =3D VQ_LAYOUT_PACKED; vq->map =3D map; vq->use_map_api =3D vring_use_map_api(vdev); =20 @@ -2195,6 +2228,34 @@ static int virtqueue_resize_packed(struct vring_virt= queue *vq, u32 num) return -ENOMEM; } =20 +static const struct virtqueue_ops split_ops =3D { + .add =3D virtqueue_add_split, + .get =3D virtqueue_get_buf_ctx_split, + .kick_prepare =3D virtqueue_kick_prepare_split, + .disable_cb =3D virtqueue_disable_cb_split, + .enable_cb_delayed =3D virtqueue_enable_cb_delayed_split, + .enable_cb_prepare =3D virtqueue_enable_cb_prepare_split, + .poll =3D virtqueue_poll_split, + .detach_unused_buf =3D virtqueue_detach_unused_buf_split, + .more_used =3D more_used_split, + .resize =3D virtqueue_resize_split, + .reset =3D virtqueue_reset_split, +}; + +static const struct virtqueue_ops packed_ops =3D { + .add =3D virtqueue_add_packed, + .get =3D virtqueue_get_buf_ctx_packed, + .kick_prepare =3D virtqueue_kick_prepare_packed, + .disable_cb =3D virtqueue_disable_cb_packed, + .enable_cb_delayed =3D virtqueue_enable_cb_delayed_packed, + .enable_cb_prepare =3D virtqueue_enable_cb_prepare_packed, + .poll =3D virtqueue_poll_packed, + .detach_unused_buf =3D virtqueue_detach_unused_buf_packed, + .more_used =3D more_used_packed, + .resize =3D virtqueue_resize_packed, + .reset =3D virtqueue_reset_packed, +}; + static int virtqueue_disable_and_recycle(struct virtqueue *_vq, void (*recycle)(struct virtqueue *vq, void *buf)) { @@ -2237,6 +2298,42 @@ static int virtqueue_enable_after_reset(struct virtq= ueue *_vq) * Generic functions and exported symbols. */ =20 +#define VIRTQUEUE_CALL(vq, op, ...) \ + ({ \ + typeof(vq) __VIRTQUEUE_CALL_vq =3D (vq); \ + typeof(split_ops.op(__VIRTQUEUE_CALL_vq, ##__VA_ARGS__)) ret; \ + \ + switch (__VIRTQUEUE_CALL_vq->layout) { \ + case VQ_LAYOUT_SPLIT: \ + ret =3D split_ops.op(__VIRTQUEUE_CALL_vq, ##__VA_ARGS__); \ + break; \ + case VQ_LAYOUT_PACKED: \ + ret =3D packed_ops.op(__VIRTQUEUE_CALL_vq, ##__VA_ARGS__);\ + break; \ + default: \ + BUG(); \ + break; \ + } \ + ret; \ +}) + +#define VOID_VIRTQUEUE_CALL(vq, op, ...) \ + ({ \ + typeof(vq) __VIRTQUEUE_CALL_vq =3D (vq); \ + \ + switch (__VIRTQUEUE_CALL_vq->layout) { \ + case VQ_LAYOUT_SPLIT: \ + split_ops.op(__VIRTQUEUE_CALL_vq, ##__VA_ARGS__); \ + break; \ + case VQ_LAYOUT_PACKED: \ + packed_ops.op(__VIRTQUEUE_CALL_vq, ##__VA_ARGS__); \ + break; \ + default: \ + BUG(); \ + break; \ + } \ +}) + static inline int virtqueue_add(struct virtqueue *_vq, struct scatterlist *sgs[], unsigned int total_sg, @@ -2249,10 +2346,9 @@ static inline int virtqueue_add(struct virtqueue *_v= q, { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_add_packed(vq, sgs, total_sg, - out_sgs, in_sgs, data, ctx, premapped, gfp) : - virtqueue_add_split(vq, sgs, total_sg, - out_sgs, in_sgs, data, ctx, premapped, gfp); + return VIRTQUEUE_CALL(vq, add, sgs, total_sg, + out_sgs, in_sgs, data, + ctx, premapped, gfp); } =20 /** @@ -2442,8 +2538,7 @@ bool virtqueue_kick_prepare(struct virtqueue *_vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_kick_prepare_packed(vq) : - virtqueue_kick_prepare_split(vq); + return VIRTQUEUE_CALL(vq, kick_prepare); } EXPORT_SYMBOL_GPL(virtqueue_kick_prepare); =20 @@ -2513,8 +2608,7 @@ void *virtqueue_get_buf_ctx(struct virtqueue *_vq, un= signed int *len, { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_get_buf_ctx_packed(vq, len, ctx) : - virtqueue_get_buf_ctx_split(vq, len, ctx); + return VIRTQUEUE_CALL(vq, get, len, ctx); } EXPORT_SYMBOL_GPL(virtqueue_get_buf_ctx); =20 @@ -2536,10 +2630,7 @@ void virtqueue_disable_cb(struct virtqueue *_vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - if (vq->packed_ring) - virtqueue_disable_cb_packed(vq); - else - virtqueue_disable_cb_split(vq); + VOID_VIRTQUEUE_CALL(vq, disable_cb); } EXPORT_SYMBOL_GPL(virtqueue_disable_cb); =20 @@ -2562,8 +2653,7 @@ unsigned int virtqueue_enable_cb_prepare(struct virtq= ueue *_vq) if (vq->event_triggered) vq->event_triggered =3D false; =20 - return vq->packed_ring ? virtqueue_enable_cb_prepare_packed(vq) : - virtqueue_enable_cb_prepare_split(vq); + return VIRTQUEUE_CALL(vq, enable_cb_prepare); } EXPORT_SYMBOL_GPL(virtqueue_enable_cb_prepare); =20 @@ -2584,8 +2674,8 @@ bool virtqueue_poll(struct virtqueue *_vq, unsigned i= nt last_used_idx) return false; =20 virtio_mb(vq->weak_barriers); - return vq->packed_ring ? virtqueue_poll_packed(vq, last_used_idx) : - virtqueue_poll_split(vq, last_used_idx); + + return VIRTQUEUE_CALL(vq, poll, last_used_idx); } EXPORT_SYMBOL_GPL(virtqueue_poll); =20 @@ -2628,8 +2718,7 @@ bool virtqueue_enable_cb_delayed(struct virtqueue *_v= q) if (vq->event_triggered) data_race(vq->event_triggered =3D false); =20 - return vq->packed_ring ? virtqueue_enable_cb_delayed_packed(vq) : - virtqueue_enable_cb_delayed_split(vq); + return VIRTQUEUE_CALL(vq, enable_cb_delayed); } EXPORT_SYMBOL_GPL(virtqueue_enable_cb_delayed); =20 @@ -2645,14 +2734,13 @@ void *virtqueue_detach_unused_buf(struct virtqueue = *_vq) { struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? virtqueue_detach_unused_buf_packed(vq) : - virtqueue_detach_unused_buf_split(vq); + return VIRTQUEUE_CALL(vq, detach_unused_buf); } EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); =20 static inline bool more_used(const struct vring_virtqueue *vq) { - return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); + return VIRTQUEUE_CALL(vq, more_used); } =20 /** @@ -2782,7 +2870,7 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num, if (!num) return -EINVAL; =20 - if ((vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num) =3D=3D= num) + if (virtqueue_get_vring_size(_vq) =3D=3D num) return 0; =20 err =3D virtqueue_disable_and_recycle(_vq, recycle); @@ -2791,10 +2879,7 @@ int virtqueue_resize(struct virtqueue *_vq, u32 num, if (recycle_done) recycle_done(_vq); =20 - if (vq->packed_ring) - err =3D virtqueue_resize_packed(vq, num); - else - err =3D virtqueue_resize_split(vq, num); + err =3D VIRTQUEUE_CALL(vq, resize, num); =20 err_reset =3D virtqueue_enable_after_reset(_vq); if (err_reset) @@ -2832,10 +2917,7 @@ int virtqueue_reset(struct virtqueue *_vq, if (recycle_done) recycle_done(_vq); =20 - if (vq->packed_ring) - virtqueue_reset_packed(vq); - else - virtqueue_reset_split(vq); + VOID_VIRTQUEUE_CALL(vq, reset); =20 return virtqueue_enable_after_reset(_vq); } @@ -2878,7 +2960,7 @@ static void vring_free(struct virtqueue *_vq) struct vring_virtqueue *vq =3D to_vvq(_vq); =20 if (vq->we_own_ring) { - if (vq->packed_ring) { + if (virtqueue_is_packed(vq)) { vring_free_queue(vq->vq.vdev, vq->packed.ring_size_in_bytes, vq->packed.vring.desc, @@ -2907,7 +2989,7 @@ static void vring_free(struct virtqueue *_vq) vq->map); } } - if (!vq->packed_ring) { + if (!virtqueue_is_packed(vq)) { kfree(vq->split.desc_state); kfree(vq->split.desc_extra); } @@ -2932,7 +3014,7 @@ u32 vring_notification_data(struct virtqueue *_vq) struct vring_virtqueue *vq =3D to_vvq(_vq); u16 next; =20 - if (vq->packed_ring) + if (virtqueue_is_packed(vq)) next =3D (vq->packed.next_avail_idx & ~(-(1 << VRING_PACKED_EVENT_F_WRAP_CTR))) | vq->packed.avail_wrap_counter << @@ -2985,7 +3067,8 @@ unsigned int virtqueue_get_vring_size(const struct vi= rtqueue *_vq) =20 const struct vring_virtqueue *vq =3D to_vvq(_vq); =20 - return vq->packed_ring ? vq->packed.vring.num : vq->split.vring.num; + return virtqueue_is_packed(vq) ? vq->packed.vring.num : + vq->split.vring.num; } EXPORT_SYMBOL_GPL(virtqueue_get_vring_size); =20 @@ -3068,7 +3151,7 @@ dma_addr_t virtqueue_get_desc_addr(const struct virtq= ueue *_vq) =20 BUG_ON(!vq->we_own_ring); =20 - if (vq->packed_ring) + if (virtqueue_is_packed(vq)) return vq->packed.ring_dma_addr; =20 return vq->split.queue_dma_addr; @@ -3081,7 +3164,7 @@ dma_addr_t virtqueue_get_avail_addr(const struct virt= queue *_vq) =20 BUG_ON(!vq->we_own_ring); =20 - if (vq->packed_ring) + if (virtqueue_is_packed(vq)) return vq->packed.driver_event_dma_addr; =20 return vq->split.queue_dma_addr + @@ -3095,7 +3178,7 @@ dma_addr_t virtqueue_get_used_addr(const struct virtq= ueue *_vq) =20 BUG_ON(!vq->we_own_ring); =20 - if (vq->packed_ring) + if (virtqueue_is_packed(vq)) return vq->packed.device_event_dma_addr; =20 return vq->split.queue_dma_addr + --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C6C82FD7D5 for ; Tue, 30 Dec 2025 06:47:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077277; cv=none; b=kzz5ETRzILlSvnMy1HyDqQW3idSVqfT0XjMz0T4tEdza3qPFnVG9Prxoz6gumjqkEwIdE0VjX9tTPj+9BvyueXyewE5vpXtBVvgPSxHVhIBYEHQe76cZRyAdQgHfPywVvNcMDC40/XrB7tQ0s5I51D4Oe01ujTD2U+dH0PIEAuY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077277; c=relaxed/simple; bh=2T+VmzY02RNZh4eu2RiFMdlY9HfozvNxjeOJCjeIvvM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Pu109p2WNEEL09ycm26GcwK/7NROfJQd9v42kROwXrIz1dYntbuioVcPaS5eO/by3EC5hRyDLOqycay8A4mNn+kzzhMkic5BAgub5lWR3R9hLs+0VTA6kj6cZ7U6jyxyyrA0YZPjslLrvIEHWmMhvfaO2v1wRYw/bEGdL0BEeX4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=cE85zMVT; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="cE85zMVT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077274; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ig2g+mSdEvnUp9CmjNCKFtRPPldZC0LNYdzsiKrQidI=; b=cE85zMVTzZNk8gQg3hTm2t0W7GFrBigtGOZhkMpJ+gjKvNacsdJrSruCn+9jxnihMEueH/ XkHm9uG3ZhroD67RomiXmGAzv5Z769tddQTEgLd1+htDZCLSfZPHLikfD7IoLv0jVMhY1F WVbtkt1pW7aIkcCzKdFRbClxO7kcsh0= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-137-eWW04LoOPdWLu4o0u-BJIw-1; Tue, 30 Dec 2025 01:47:50 -0500 X-MC-Unique: eWW04LoOPdWLu4o0u-BJIw-1 X-Mimecast-MFC-AGG-ID: eWW04LoOPdWLu4o0u-BJIw_1767077269 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6229A195DE48; Tue, 30 Dec 2025 06:47:49 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6867619560AB; Tue, 30 Dec 2025 06:47:46 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 14/19] virtio_ring: determine descriptor flags at one time Date: Tue, 30 Dec 2025 14:46:44 +0800 Message-ID: <20251230064649.55597-15-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Let's determine the last descriptor by counting the number of sg. This would be consistent with packed virtqueue implementation and ease the future in-order implementation. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 29174a77eacd..33fa7fcdbd8a 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -574,7 +574,7 @@ static inline int virtqueue_add_split(struct vring_virt= queue *vq, struct vring_desc_extra *extra; struct scatterlist *sg; struct vring_desc *desc; - unsigned int i, n, avail, descs_used, prev, err_idx; + unsigned int i, n, avail, descs_used, err_idx, sg_count =3D 0; int head; bool indirect; =20 @@ -634,42 +634,40 @@ static inline int virtqueue_add_split(struct vring_vi= rtqueue *vq, for (sg =3D sgs[n]; sg; sg =3D sg_next(sg)) { dma_addr_t addr; u32 len; + u16 flags =3D 0; + + if (++sg_count !=3D total_sg) + flags |=3D VRING_DESC_F_NEXT; =20 if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr, &len, premapped)) goto unmap_release; =20 - prev =3D i; /* Note that we trust indirect descriptor * table since it use stream DMA mapping. */ - i =3D virtqueue_add_desc_split(vq, desc, extra, i, addr, len, - VRING_DESC_F_NEXT, - premapped); + i =3D virtqueue_add_desc_split(vq, desc, extra, i, addr, + len, flags, premapped); } } for (; n < (out_sgs + in_sgs); n++) { for (sg =3D sgs[n]; sg; sg =3D sg_next(sg)) { dma_addr_t addr; u32 len; + u16 flags =3D VRING_DESC_F_WRITE; + + if (++sg_count !=3D total_sg) + flags |=3D VRING_DESC_F_NEXT; =20 if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr, &len, premapped)) goto unmap_release; =20 - prev =3D i; /* Note that we trust indirect descriptor * table since it use stream DMA mapping. */ - i =3D virtqueue_add_desc_split(vq, desc, extra, i, addr, len, - VRING_DESC_F_NEXT | - VRING_DESC_F_WRITE, - premapped); + i =3D virtqueue_add_desc_split(vq, desc, extra, i, addr, + len, flags, premapped); } } - /* Last one doesn't continue. */ - desc[prev].flags &=3D cpu_to_virtio16(vq->vq.vdev, ~VRING_DESC_F_NEXT); - if (!indirect && vring_need_unmap_buffer(vq, &extra[prev])) - vq->split.desc_extra[prev & (vq->split.vring.num - 1)].flags &=3D - ~VRING_DESC_F_NEXT; =20 if (indirect) { /* Now that the indirect table is filled in, map it. */ --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBBE22FDC35 for ; Tue, 30 Dec 2025 06:47:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077280; cv=none; b=Rk280ISgMq/8H/KcUfBVn4arW1CReiKEF8XasFz/B+vbRIYfjOBO3G8eK7cZFn1S2/xrSIQLBVtDq/JpbGRbxJFhAT5soRTLlzu59dIed7Xbpb0WiF6sEG2Bi39KhfT6IJ2QDSGWJHg85yx3kXPz/8BAXLyQ8KWddVfIp+HH4co= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077280; c=relaxed/simple; bh=h5BSZKgj+acTNsFvAjwJJJ3zRVV/k4vnKOTMrurvSqw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Rb9NnsyM8qGzJ10QXI9Hm9xhTymv+Zpb3Xt2s6ryenkOfWL2DTD2Z+ZJZGx5UHY+aQwIqrJLijNwTbKBWizoXdQJ5IdubTp5cmKuH0GmPVo+hpUhrcGR8tNj2TnbB+JZoRWvep+1GrUKRy1tmrrDAHTJbKJ/bV6VpEDwewCURtA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dm3PURSC; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dm3PURSC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077277; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XykLdeypK6IM0kkjqZ/K4gOfhaWmVqdrNwu6i75cuSk=; b=dm3PURSCH3Uw7IAI4+K6swFFd3mFpYYwkCng7UoLReg/oskFhNZI0nADt+jh0hz7VPnfP1 rFtJKQYoOMG5HJhkhr5CnyAU42kvyibLc0ERkMcT9IVZvjYSRGY0rvh7Ns4Yym5UyxzbRP dhQ0lem6ZpJly0gl7Dbo+2sqnuvk+oA= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-90-9kFt0q_MPcqxXO8JOOlvbA-1; Tue, 30 Dec 2025 01:47:54 -0500 X-MC-Unique: 9kFt0q_MPcqxXO8JOOlvbA-1 X-Mimecast-MFC-AGG-ID: 9kFt0q_MPcqxXO8JOOlvbA_1767077273 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3133F1956089; Tue, 30 Dec 2025 06:47:53 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1819519560AB; Tue, 30 Dec 2025 06:47:49 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 15/19] virtio_ring: factor out core logic of buffer detaching Date: Tue, 30 Dec 2025 14:46:45 +0800 Message-ID: <20251230064649.55597-16-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Factor out core logic of buffer detaching and leave the free list management to the caller so in_order can just call the core logic. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 33fa7fcdbd8a..0a63a79c33dc 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1666,8 +1666,8 @@ static bool virtqueue_kick_prepare_packed(struct vrin= g_virtqueue *vq) return needs_kick; } =20 -static void detach_buf_packed(struct vring_virtqueue *vq, - unsigned int id, void **ctx) +static void detach_buf_packed_in_order(struct vring_virtqueue *vq, + unsigned int id, void **ctx) { struct vring_desc_state_packed *state =3D NULL; struct vring_packed_desc *desc; @@ -1678,8 +1678,6 @@ static void detach_buf_packed(struct vring_virtqueue = *vq, /* Clear data ptr. */ state->data =3D NULL; =20 - vq->packed.desc_extra[state->last].next =3D vq->free_head; - vq->free_head =3D id; vq->vq.num_free +=3D state->num; =20 if (unlikely(vq->use_map_api)) { @@ -1716,6 +1714,17 @@ static void detach_buf_packed(struct vring_virtqueue= *vq, } } =20 +static void detach_buf_packed(struct vring_virtqueue *vq, + unsigned int id, void **ctx) +{ + struct vring_desc_state_packed *state =3D &vq->packed.desc_state[id]; + + vq->packed.desc_extra[state->last].next =3D vq->free_head; + vq->free_head =3D id; + + detach_buf_packed_in_order(vq, id, ctx); +} + static inline bool is_used_desc_packed(const struct vring_virtqueue *vq, u16 idx, bool used_wrap_counter) { --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77A442FF14F for ; Tue, 30 Dec 2025 06:48:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077282; cv=none; b=SYb2+9pQVn73sF4Ee9MRpVjUZxYs+ra0y8KgGLCDbYL+gfT65LSwtolPysyzhlfs9YAZ48LSx9FOdWzlCh/Nuo+N9gQpqvTovIy+ncSJsnSZurCN9uueMTSt4nXu8HJNuNIobS1mNLKQKDUa54phMHQa0IToCHxNzPhUZ8zAZpw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077282; c=relaxed/simple; bh=Ux32GHH5Dx8iM1FCHnbe5A2QCaAyeENtd0Z0GJQ1Tvo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=oj1Vzid6cxJUAs3N2oVacBWFHgwWposD9ktqjrBNEsFCcgPYwUMiUegm3dgGGJHBJ/HW6TBu5cN9lpzOXdQHD/KRROxmQJiUNZ/Y1yvN+HSESILZHZYDIqCi3UKGotg1hE8DFX0JlMSxa/gFs/3eEIab/FBu+GBALeNwatO0J2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MOwe7Rje; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MOwe7Rje" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=y3qmApNq6h0nS5LflqR2sRkeOQaPYjaaTMOu0t/1cxA=; b=MOwe7RjeYvW2CBQlrIHkngtK+BmpRVK3ftKiIlZ82acnt7ISn1HFMnFV4NgsSa+kLiIdzi Ni9lj6IfUjRq2ddivJpj6DlqjbxWkHWAnwB2O7Myll2Y01VmHwHQJwvd4PL62A9lws12qW 8JKeViUX/YiAfHL5fxpXBqrG6R+53iM= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-192-VkxlkpZHPFGQd1HRQXHmRQ-1; Tue, 30 Dec 2025 01:47:57 -0500 X-MC-Unique: VkxlkpZHPFGQd1HRQXHmRQ-1 X-Mimecast-MFC-AGG-ID: VkxlkpZHPFGQd1HRQXHmRQ_1767077276 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CA5CA195608A; Tue, 30 Dec 2025 06:47:56 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id DAA0A19560AB; Tue, 30 Dec 2025 06:47:53 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 16/19] virtio_ring: factor out core logic for updating last_used_idx Date: Tue, 30 Dec 2025 14:46:46 +0800 Message-ID: <20251230064649.55597-17-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Factor out the core logic for updating last_used_idx to be reused by the packed in order implementation. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 43 +++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 0a63a79c33dc..613fd9646d84 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1755,6 +1755,30 @@ static bool more_used_packed(const struct vring_virt= queue *vq) return virtqueue_poll_packed(vq, READ_ONCE(vq->last_used_idx)); } =20 +static void update_last_used_idx_packed(struct vring_virtqueue *vq, + u16 id, u16 last_used, + u16 used_wrap_counter) +{ + last_used +=3D vq->packed.desc_state[id].num; + if (unlikely(last_used >=3D vq->packed.vring.num)) { + last_used -=3D vq->packed.vring.num; + used_wrap_counter ^=3D 1; + } + + last_used =3D (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRA= P_CTR)); + WRITE_ONCE(vq->last_used_idx, last_used); + + /* + * 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. + */ + if (vq->packed.event_flags_shadow =3D=3D VRING_PACKED_EVENT_FLAG_DESC) + virtio_store_mb(vq->weak_barriers, + &vq->packed.vring.driver->off_wrap, + cpu_to_le16(vq->last_used_idx)); +} + static void *virtqueue_get_buf_ctx_packed(struct vring_virtqueue *vq, unsigned int *len, void **ctx) @@ -1798,24 +1822,7 @@ static void *virtqueue_get_buf_ctx_packed(struct vri= ng_virtqueue *vq, ret =3D vq->packed.desc_state[id].data; detach_buf_packed(vq, id, ctx); =20 - last_used +=3D vq->packed.desc_state[id].num; - if (unlikely(last_used >=3D vq->packed.vring.num)) { - last_used -=3D vq->packed.vring.num; - used_wrap_counter ^=3D 1; - } - - last_used =3D (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRA= P_CTR)); - WRITE_ONCE(vq->last_used_idx, last_used); - - /* - * 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. - */ - if (vq->packed.event_flags_shadow =3D=3D VRING_PACKED_EVENT_FLAG_DESC) - virtio_store_mb(vq->weak_barriers, - &vq->packed.vring.driver->off_wrap, - cpu_to_le16(vq->last_used_idx)); + update_last_used_idx_packed(vq, id, last_used, used_wrap_counter); =20 LAST_ADD_TIME_INVALID(vq); =20 --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5893D2FC86C for ; Tue, 30 Dec 2025 06:48:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077294; cv=none; b=BjZPdnBnivZKc7kP/CbBK+k0gEBWF0UE0IDc/hcqgRTz6YEdJeZP0vfiTmRBtBJPvJuC0LiZbxwGc8oNfEPGwiWEjA1678kasQrU9gt6awcal/LNb4P/nTvUiLxbnlsFuzGxOXj2ecjQvIcTY3Vt7L0maSeV/zedQyO/FeW+nBE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077294; c=relaxed/simple; bh=McSvya6lDqBxPSvt5yWefNPI8zI0naXbvBM3CYgifZA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Wb0fRw1D5584O6ymk04QQXaOjmuvDYflEVjRh3JAPfii7k/BBmDSU8sXGNzEmo0cFny35yTh1SFBM6O9s2nQR6gKlkluYMTNZx3Ak7srdQk5EYs5Ht8eRY+CUqfr92vZuvkIrNNFfppPCQNxTbsdRVq4lc9z0DNxUJIjwyuwqLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YgD91naM; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YgD91naM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077285; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Aae1tLoOsetmi3cZAXlNCv/4auou6yXrbJt3618uUjo=; b=YgD91naMG3w+rzq8OIJfO2/co0ysVjhI9Xlp3sXJkWHQ3Smg27acnDr0C8nUjLGklWZTD0 gus5f6L2VJVRjK/Au71V+YCz32R56xfP7SQj7nAzcJgq/AunRPbA5h/Mq2geJMkBfE8Nd0 Kt4t0w5j/LOn97OOlFLKQ8YjH7H0DHs= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-367-FML_Q_xyO6WTiRsgXmEZIQ-1; Tue, 30 Dec 2025 01:48:01 -0500 X-MC-Unique: FML_Q_xyO6WTiRsgXmEZIQ-1 X-Mimecast-MFC-AGG-ID: FML_Q_xyO6WTiRsgXmEZIQ_1767077280 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9928219560AF; Tue, 30 Dec 2025 06:48:00 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8085619560AB; Tue, 30 Dec 2025 06:47:57 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 17/19] virtio_ring: factor out split indirect detaching logic Date: Tue, 30 Dec 2025 14:46:47 +0800 Message-ID: <20251230064649.55597-18-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Factor out the split indirect descriptor detaching logic in order to allow it to be reused by the in order support. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 62 ++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 613fd9646d84..3d0263a10f6f 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -775,11 +775,41 @@ static bool virtqueue_kick_prepare_split(struct vring= _virtqueue *vq) return needs_kick; } =20 +static void detach_indirect_split(struct vring_virtqueue *vq, + unsigned int head) +{ + struct vring_desc_extra *extra =3D vq->split.desc_extra; + struct vring_desc *indir_desc =3D vq->split.desc_state[head].indir_desc; + unsigned int j; + u32 len, num; + + /* Free the indirect table, if any, now that it's unmapped. */ + if (!indir_desc) + return; + len =3D vq->split.desc_extra[head].len; + + BUG_ON(!(vq->split.desc_extra[head].flags & + VRING_DESC_F_INDIRECT)); + BUG_ON(len =3D=3D 0 || len % sizeof(struct vring_desc)); + + num =3D len / sizeof(struct vring_desc); + + extra =3D (struct vring_desc_extra *)&indir_desc[num]; + + if (vq->use_map_api) { + for (j =3D 0; j < num; j++) + vring_unmap_one_split(vq, &extra[j]); + } + + kfree(indir_desc); + vq->split.desc_state[head].indir_desc =3D NULL; +} + static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, void **ctx) { struct vring_desc_extra *extra; - unsigned int i, j; + unsigned int i; __virtio16 nextflag =3D cpu_to_virtio16(vq->vq.vdev, VRING_DESC_F_NEXT); =20 /* Clear data ptr. */ @@ -803,34 +833,10 @@ static void detach_buf_split(struct vring_virtqueue *= vq, unsigned int head, /* Plus final descriptor */ vq->vq.num_free++; =20 - if (vq->indirect) { - struct vring_desc *indir_desc =3D - vq->split.desc_state[head].indir_desc; - u32 len, num; - - /* Free the indirect table, if any, now that it's unmapped. */ - if (!indir_desc) - return; - len =3D vq->split.desc_extra[head].len; - - BUG_ON(!(vq->split.desc_extra[head].flags & - VRING_DESC_F_INDIRECT)); - BUG_ON(len =3D=3D 0 || len % sizeof(struct vring_desc)); - - num =3D len / sizeof(struct vring_desc); - - extra =3D (struct vring_desc_extra *)&indir_desc[num]; - - if (vq->use_map_api) { - for (j =3D 0; j < num; j++) - vring_unmap_one_split(vq, &extra[j]); - } - - kfree(indir_desc); - vq->split.desc_state[head].indir_desc =3D NULL; - } else if (ctx) { + if (vq->indirect) + detach_indirect_split(vq, head); + else if (ctx) *ctx =3D vq->split.desc_state[head].indir_desc; - } } =20 static bool virtqueue_poll_split(const struct vring_virtqueue *vq, --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 393812FFFB5 for ; Tue, 30 Dec 2025 06:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077291; cv=none; b=CqAIFOV+N78f7VPFg/QIjdBUgqQ62q1AZS04OYLP7q8/t+fdkXV206tPJD4nKhEeu0rvYDAf9nPXdE+de0YL6FH+W001gjwNwhzgx+aEIPscTJgw3X57nJJvgpVkTL5k+K6AKvM6+uny8Jc8HzCRvXZHFHwP1dOYzxKwMWyt2C8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077291; c=relaxed/simple; bh=7VfrefuV0RyveHZDmWeVM/KBolm41k0Ra+JCI+z6+JI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jxIAv6Mgc0RmAmw4OuL90WlnznM3TwLYh0oD/Rxy9rcB/Ez6yDwBSDPccAFn39yDygbyFg227B29Q0h7xh4N1tJd6U3K1WoUHjPLMCenocHl3KOKupdI6AeW/nobvsuP5/sjoLI806APCZePheJIjyeUN5hoIDtBAbDmuMe/bNs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=iLAttjBE; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iLAttjBE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077288; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qsn3F2c//WA9V2xo8nKXDIJfNbf92PNIzZ1cj0+dpjM=; b=iLAttjBEBo/xFaQ7Mwp2v2l+oAhwquM8xZNF8D2SAp/I+85f1Ox1koos8hwk+W/C95KmNs reqoLEiG3QIGg14XPx0XZMCn/FxlKIZH9omSfyLEXiwVM3xkvBWjTYFbcfQj5o89M84bLr mwvq1ZLq7hXymF1a5LnxS/MBDtNZ6tE= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-390-fOxDKlIgNja0RMsTMq6IiA-1; Tue, 30 Dec 2025 01:48:05 -0500 X-MC-Unique: fOxDKlIgNja0RMsTMq6IiA-1 X-Mimecast-MFC-AGG-ID: fOxDKlIgNja0RMsTMq6IiA_1767077284 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 560621956050; Tue, 30 Dec 2025 06:48:04 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5A04819560AB; Tue, 30 Dec 2025 06:48:00 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 18/19] virtio_ring: factor out split detaching logic Date: Tue, 30 Dec 2025 14:46:48 +0800 Message-ID: <20251230064649.55597-19-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 This patch factors out the split core detaching logic that could be reused by in order feature into a dedicated function. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 3d0263a10f6f..f0b5511a2651 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -805,8 +805,9 @@ static void detach_indirect_split(struct vring_virtqueu= e *vq, vq->split.desc_state[head].indir_desc =3D NULL; } =20 -static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, - void **ctx) +static unsigned detach_buf_split_in_order(struct vring_virtqueue *vq, + unsigned int head, + void **ctx) { struct vring_desc_extra *extra; unsigned int i; @@ -827,8 +828,6 @@ static void detach_buf_split(struct vring_virtqueue *vq= , unsigned int head, } =20 vring_unmap_one_split(vq, &extra[i]); - vq->split.desc_extra[i].next =3D vq->free_head; - vq->free_head =3D head; =20 /* Plus final descriptor */ vq->vq.num_free++; @@ -837,6 +836,17 @@ static void detach_buf_split(struct vring_virtqueue *v= q, unsigned int head, detach_indirect_split(vq, head); else if (ctx) *ctx =3D vq->split.desc_state[head].indir_desc; + + return i; +} + +static void detach_buf_split(struct vring_virtqueue *vq, unsigned int head, + void **ctx) +{ + unsigned int i =3D detach_buf_split_in_order(vq, head, ctx); + + vq->split.desc_extra[i].next =3D vq->free_head; + vq->free_head =3D head; } =20 static bool virtqueue_poll_split(const struct vring_virtqueue *vq, --=20 2.31.1 From nobody Sat Feb 7 19:04:22 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE85E3002CF for ; Tue, 30 Dec 2025 06:48:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077294; cv=none; b=jytr4GW8NkmlrfbBfx777acWHfxeiARQvz2En4Xr5CmEgQ+SR0PnKFf3UC5acqCloCVIHdhOtcslVdRIdMkrMw7VYMLXDO4dyEsa8H0sKeNJ5rizMS1iIGaLuEAPDkViFR5BkVx0YQ3jsLzVyf/sZjhgPXs06LKC5dRUi8BqgTg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767077294; c=relaxed/simple; bh=MliylHkMXVgnYjt5oK4ebTh8e0RSqPLK+cVSYt0Hrgo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KVgdGx9RJQ7OketwEQ8XaJL9idvye1ZjtxNm7UsbggE7UMmmsQ9Ab143hFxlQhBnQqXs+s5CIe3ozEQEfRhtN1i7VyTPe4IWDGYxRoEHF0RR8Obv3SPOAn1qsb6QaM1YavAzXx3G2Zhov/QGzdgGHqL4Q+0vf+2TYEPBgdWkrxM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=O6B0tv2x; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="O6B0tv2x" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1767077290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=lbVeMCOtaOeIeG//Rp5tsWkM5yhtDLe7eAC0fmZIyC8=; b=O6B0tv2xQU+2ObejVIFbwF3E/F7TyqkL02F4gOaEKIeC7dWgtCLEzwxKNfM48//pc0NHsx Iw10FdSzipOQqqWGaI+iU3j5bBnJYCFx1PR25yhaT5YZwdeRoEEbOSBQVMWh1XW0HzjGZa KkqtXUfwmCtcP7QSo08zewJLbHfVr1E= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-94-hrq8xz_1N46E2WBGQ-lv8Q-1; Tue, 30 Dec 2025 01:48:08 -0500 X-MC-Unique: hrq8xz_1N46E2WBGQ-lv8Q-1 X-Mimecast-MFC-AGG-ID: hrq8xz_1N46E2WBGQ-lv8Q_1767077288 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D9BA7195DE48; Tue, 30 Dec 2025 06:48:07 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.134]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EA27819560AB; Tue, 30 Dec 2025 06:48:04 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V11 19/19] virtio_ring: add in order support Date: Tue, 30 Dec 2025 14:46:49 +0800 Message-ID: <20251230064649.55597-20-jasowang@redhat.com> In-Reply-To: <20251230064649.55597-1-jasowang@redhat.com> References: <20251230064649.55597-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 This patch implements in order support for both split virtqueue and packed virtqueue. Performance could be gained for the device where the memory access could be expensive (e.g vhost-net or a real PCI device): Benchmark with KVM guest: Vhost-net on the host: (pktgen + XDP_DROP): in_order=3Doff | in_order=3Don | +% TX: 4.51Mpps | 5.30Mpps | +17% RX: 3.47Mpps | 3.61Mpps | + 4% Vhost-user(testpmd) on the host: (pktgen/XDP_DROP): For split virtqueue: in_order=3Doff | in_order=3Don | +% TX: 5.60Mpps | 5.60Mpps | +0.0% RX: 9.16Mpps | 9.61Mpps | +4.9% For packed virtqueue: in_order=3Doff | in_order=3Don | +% TX: 5.60Mpps | 5.70Mpps | +1.7% RX: 10.6Mpps | 10.8Mpps | +1.8% Benchmark also shows no performance impact for in_order=3Doff for queue size with 256 and 1024. Reviewed-by: Eugenio P=C3=A9rez Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 448 +++++++++++++++++++++++++++++++++-- 1 file changed, 430 insertions(+), 18 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index f0b5511a2651..d04531771d3a 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -70,6 +70,8 @@ enum vq_layout { VQ_LAYOUT_SPLIT =3D 0, VQ_LAYOUT_PACKED, + VQ_LAYOUT_SPLIT_IN_ORDER, + VQ_LAYOUT_PACKED_IN_ORDER, }; =20 struct vring_desc_state_split { @@ -79,6 +81,7 @@ struct vring_desc_state_split { * allocated together. So we won't stress more to the memory allocator. */ struct vring_desc *indir_desc; + u32 total_in_len; }; =20 struct vring_desc_state_packed { @@ -90,6 +93,7 @@ struct vring_desc_state_packed { struct vring_packed_desc *indir_desc; u16 num; /* Descriptor list length. */ u16 last; /* The last desc state in a list. */ + u32 total_in_len; /* In length for the skipped buffer. */ }; =20 struct vring_desc_extra { @@ -204,8 +208,24 @@ struct vring_virtqueue { =20 enum vq_layout layout; =20 - /* Head of free buffer list. */ + /* + * Without IN_ORDER it's the head of free buffer list. With + * IN_ORDER and SPLIT, it's the next available buffer + * index. With IN_ORDER and PACKED, it's unused. + */ unsigned int free_head; + + /* + * With IN_ORDER, once we see an in-order batch, this stores + * this last entry, and until we return the last buffer. + * After this, id is set to UINT_MAX to mark it invalid. + * Unused without IN_ORDER. + */ + struct used_entry { + u32 id; + u32 len; + } batch_last; + /* Number we've added since last sync. */ unsigned int num_added; =20 @@ -217,6 +237,11 @@ struct vring_virtqueue { */ u16 last_used_idx; =20 + /* With IN_ORDER and SPLIT, last descriptor id we used to + * detach buffer. + */ + u16 last_used; + /* Hint for event idx: already triggered no need to disable. */ bool event_triggered; =20 @@ -258,7 +283,14 @@ static void vring_free(struct virtqueue *_vq); =20 static inline bool virtqueue_is_packed(const struct vring_virtqueue *vq) { - return vq->layout =3D=3D VQ_LAYOUT_PACKED; + return vq->layout =3D=3D VQ_LAYOUT_PACKED || + vq->layout =3D=3D VQ_LAYOUT_PACKED_IN_ORDER; +} + +static inline bool virtqueue_is_in_order(const struct vring_virtqueue *vq) +{ + return vq->layout =3D=3D VQ_LAYOUT_SPLIT_IN_ORDER || + vq->layout =3D=3D VQ_LAYOUT_PACKED_IN_ORDER; } =20 static bool virtqueue_use_indirect(const struct vring_virtqueue *vq, @@ -468,6 +500,8 @@ static void virtqueue_init(struct vring_virtqueue *vq, = u32 num) else vq->last_used_idx =3D 0; =20 + vq->last_used =3D 0; + vq->event_triggered =3D false; vq->num_added =3D 0; =20 @@ -575,6 +609,8 @@ static inline int virtqueue_add_split(struct vring_virt= queue *vq, struct scatterlist *sg; struct vring_desc *desc; unsigned int i, n, avail, descs_used, err_idx, sg_count =3D 0; + /* Total length for in-order */ + unsigned int total_in_len =3D 0; int head; bool indirect; =20 @@ -666,6 +702,7 @@ static inline int virtqueue_add_split(struct vring_virt= queue *vq, */ i =3D virtqueue_add_desc_split(vq, desc, extra, i, addr, len, flags, premapped); + total_in_len +=3D len; } } =20 @@ -688,7 +725,12 @@ static inline int virtqueue_add_split(struct vring_vir= tqueue *vq, vq->vq.num_free -=3D descs_used; =20 /* Update free pointer */ - if (indirect) + if (virtqueue_is_in_order(vq)) { + vq->free_head +=3D descs_used; + if (vq->free_head >=3D vq->split.vring.num) + vq->free_head -=3D vq->split.vring.num; + vq->split.desc_state[head].total_in_len =3D total_in_len; + } else if (indirect) vq->free_head =3D vq->split.desc_extra[head].next; else vq->free_head =3D i; @@ -861,6 +903,14 @@ static bool more_used_split(const struct vring_virtque= ue *vq) return virtqueue_poll_split(vq, vq->last_used_idx); } =20 +static bool more_used_split_in_order(const struct vring_virtqueue *vq) +{ + if (vq->batch_last.id !=3D UINT_MAX) + return true; + + return virtqueue_poll_split(vq, vq->last_used_idx); +} + static void *virtqueue_get_buf_ctx_split(struct vring_virtqueue *vq, unsigned int *len, void **ctx) @@ -918,6 +968,76 @@ static void *virtqueue_get_buf_ctx_split(struct vring_= virtqueue *vq, return ret; } =20 +static void *virtqueue_get_buf_ctx_split_in_order(struct vring_virtqueue *= vq, + unsigned int *len, + void **ctx) +{ + void *ret; + unsigned int num =3D vq->split.vring.num; + unsigned int num_free =3D vq->vq.num_free; + u16 last_used, last_used_idx; + + START_USE(vq); + + if (unlikely(vq->broken)) { + END_USE(vq); + return NULL; + } + + last_used =3D vq->last_used & (num - 1); + last_used_idx =3D vq->last_used_idx & (num - 1); + + if (vq->batch_last.id =3D=3D UINT_MAX) { + if (!more_used_split_in_order(vq)) { + pr_debug("No more buffers in queue\n"); + END_USE(vq); + return NULL; + } + + /* + * Only get used array entries after they have been + * exposed by host. + */ + virtio_rmb(vq->weak_barriers); + + vq->batch_last.id =3D virtio32_to_cpu(vq->vq.vdev, + vq->split.vring.used->ring[last_used_idx].id); + vq->batch_last.len =3D virtio32_to_cpu(vq->vq.vdev, + vq->split.vring.used->ring[last_used_idx].len); + } + + if (vq->batch_last.id =3D=3D last_used) { + vq->batch_last.id =3D UINT_MAX; + *len =3D vq->batch_last.len; + } else { + *len =3D vq->split.desc_state[last_used].total_in_len; + } + + if (unlikely(!vq->split.desc_state[last_used].data)) { + BAD_RING(vq, "id %u is not a head!\n", last_used); + return NULL; + } + + /* detach_buf_split clears data, so grab it now. */ + ret =3D vq->split.desc_state[last_used].data; + detach_buf_split_in_order(vq, last_used, ctx); + + vq->last_used_idx++; + vq->last_used +=3D (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 + * the read in the next get_buf call. */ + if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) + virtio_store_mb(vq->weak_barriers, + &vring_used_event(&vq->split.vring), + cpu_to_virtio16(vq->vq.vdev, vq->last_used_idx)); + + LAST_ADD_TIME_INVALID(vq); + + END_USE(vq); + return ret; +} + static void virtqueue_disable_cb_split(struct vring_virtqueue *vq) { if (!(vq->split.avail_flags_shadow & VRING_AVAIL_F_NO_INTERRUPT)) { @@ -1011,7 +1131,10 @@ static void *virtqueue_detach_unused_buf_split(struc= t vring_virtqueue *vq) continue; /* detach_buf_split clears data, so grab it now. */ buf =3D vq->split.desc_state[i].data; - detach_buf_split(vq, i, NULL); + if (virtqueue_is_in_order(vq)) + detach_buf_split_in_order(vq, i, NULL); + else + detach_buf_split(vq, i, NULL); vq->split.avail_idx_shadow--; vq->split.vring.avail->idx =3D cpu_to_virtio16(vq->vq.vdev, vq->split.avail_idx_shadow); @@ -1074,6 +1197,7 @@ static void virtqueue_vring_attach_split(struct vring= _virtqueue *vq, =20 /* Put everything in free lists. */ vq->free_head =3D 0; + vq->batch_last.id =3D UINT_MAX; } =20 static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vri= ng_split) @@ -1185,7 +1309,6 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, if (!vq) return NULL; =20 - vq->layout =3D VQ_LAYOUT_SPLIT; vq->vq.callback =3D callback; vq->vq.vdev =3D vdev; vq->vq.name =3D name; @@ -1205,6 +1328,8 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, vq->indirect =3D virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && !context; vq->event =3D virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); + vq->layout =3D virtio_has_feature(vdev, VIRTIO_F_IN_ORDER) ? + VQ_LAYOUT_SPLIT_IN_ORDER : VQ_LAYOUT_SPLIT; =20 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) vq->weak_barriers =3D false; @@ -1362,13 +1487,14 @@ static int virtqueue_add_indirect_packed(struct vri= ng_virtqueue *vq, unsigned int in_sgs, void *data, bool premapped, - gfp_t gfp) + gfp_t gfp, + u16 id) { struct vring_desc_extra *extra; struct vring_packed_desc *desc; struct scatterlist *sg; - unsigned int i, n, err_idx, len; - u16 head, id; + unsigned int i, n, err_idx, len, total_in_len =3D 0; + u16 head; dma_addr_t addr; =20 head =3D vq->packed.next_avail_idx; @@ -1386,8 +1512,6 @@ static int virtqueue_add_indirect_packed(struct vring= _virtqueue *vq, } =20 i =3D 0; - id =3D vq->free_head; - BUG_ON(id =3D=3D vq->packed.vring.num); =20 for (n =3D 0; n < out_sgs + in_sgs; n++) { for (sg =3D sgs[n]; sg; sg =3D sg_next(sg)) { @@ -1407,6 +1531,8 @@ static int virtqueue_add_indirect_packed(struct vring= _virtqueue *vq, extra[i].flags =3D n < out_sgs ? 0 : VRING_DESC_F_WRITE; } =20 + if (n >=3D out_sgs) + total_in_len +=3D len; i++; } } @@ -1453,13 +1579,15 @@ static int virtqueue_add_indirect_packed(struct vri= ng_virtqueue *vq, 1 << VRING_PACKED_DESC_F_USED; } vq->packed.next_avail_idx =3D n; - vq->free_head =3D vq->packed.desc_extra[id].next; + if (!virtqueue_is_in_order(vq)) + vq->free_head =3D vq->packed.desc_extra[id].next; =20 /* Store token and indirect buffer state. */ vq->packed.desc_state[id].num =3D 1; vq->packed.desc_state[id].data =3D data; vq->packed.desc_state[id].indir_desc =3D desc; vq->packed.desc_state[id].last =3D id; + vq->packed.desc_state[id].total_in_len =3D total_in_len; =20 vq->num_added +=3D 1; =20 @@ -1512,8 +1640,11 @@ static inline int virtqueue_add_packed(struct vring_= virtqueue *vq, BUG_ON(total_sg =3D=3D 0); =20 if (virtqueue_use_indirect(vq, total_sg)) { + id =3D vq->free_head; + BUG_ON(id =3D=3D vq->packed.vring.num); err =3D virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, - in_sgs, data, premapped, gfp); + in_sgs, data, premapped, + gfp, id); if (err !=3D -ENOMEM) { END_USE(vq); return err; @@ -1634,6 +1765,160 @@ static inline int virtqueue_add_packed(struct vring= _virtqueue *vq, return -EIO; } =20 +static inline int virtqueue_add_packed_in_order(struct vring_virtqueue *vq, + struct scatterlist *sgs[], + unsigned int total_sg, + unsigned int out_sgs, + unsigned int in_sgs, + void *data, + void *ctx, + bool premapped, + gfp_t gfp) +{ + struct vring_packed_desc *desc; + struct scatterlist *sg; + unsigned int i, n, sg_count, err_idx, total_in_len =3D 0; + __le16 head_flags, flags; + u16 head, avail_used_flags; + bool avail_wrap_counter; + int err; + + START_USE(vq); + + BUG_ON(data =3D=3D NULL); + BUG_ON(ctx && vq->indirect); + + if (unlikely(vq->broken)) { + END_USE(vq); + return -EIO; + } + + LAST_ADD_TIME_UPDATE(vq); + + BUG_ON(total_sg =3D=3D 0); + + if (virtqueue_use_indirect(vq, total_sg)) { + err =3D virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs, + in_sgs, data, premapped, gfp, + vq->packed.next_avail_idx); + if (err !=3D -ENOMEM) { + END_USE(vq); + return err; + } + + /* fall back on direct */ + } + + head =3D vq->packed.next_avail_idx; + avail_used_flags =3D vq->packed.avail_used_flags; + avail_wrap_counter =3D vq->packed.avail_wrap_counter; + + WARN_ON_ONCE(total_sg > vq->packed.vring.num && !vq->indirect); + + desc =3D vq->packed.vring.desc; + i =3D head; + + if (unlikely(vq->vq.num_free < total_sg)) { + pr_debug("Can't add buf len %i - avail =3D %i\n", + total_sg, vq->vq.num_free); + END_USE(vq); + return -ENOSPC; + } + + sg_count =3D 0; + for (n =3D 0; n < out_sgs + in_sgs; n++) { + for (sg =3D sgs[n]; sg; sg =3D sg_next(sg)) { + dma_addr_t addr; + u32 len; + + flags =3D 0; + if (++sg_count !=3D total_sg) + flags |=3D cpu_to_le16(VRING_DESC_F_NEXT); + if (n >=3D out_sgs) + flags |=3D cpu_to_le16(VRING_DESC_F_WRITE); + + if (vring_map_one_sg(vq, sg, n < out_sgs ? + DMA_TO_DEVICE : DMA_FROM_DEVICE, + &addr, &len, premapped)) + goto unmap_release; + + flags |=3D cpu_to_le16(vq->packed.avail_used_flags); + + if (i =3D=3D head) + head_flags =3D flags; + else + desc[i].flags =3D flags; + + desc[i].addr =3D cpu_to_le64(addr); + desc[i].len =3D cpu_to_le32(len); + desc[i].id =3D cpu_to_le16(head); + + if (unlikely(vq->use_map_api)) { + vq->packed.desc_extra[i].addr =3D premapped ? + DMA_MAPPING_ERROR : addr; + vq->packed.desc_extra[i].len =3D len; + vq->packed.desc_extra[i].flags =3D + le16_to_cpu(flags); + } + + if ((unlikely(++i >=3D vq->packed.vring.num))) { + i =3D 0; + vq->packed.avail_used_flags ^=3D + 1 << VRING_PACKED_DESC_F_AVAIL | + 1 << VRING_PACKED_DESC_F_USED; + vq->packed.avail_wrap_counter ^=3D 1; + } + + if (n >=3D out_sgs) + total_in_len +=3D len; + } + } + + /* We're using some buffers from the free list. */ + vq->vq.num_free -=3D total_sg; + + /* Update free pointer */ + vq->packed.next_avail_idx =3D i; + + /* Store token. */ + vq->packed.desc_state[head].num =3D total_sg; + vq->packed.desc_state[head].data =3D data; + vq->packed.desc_state[head].indir_desc =3D ctx; + vq->packed.desc_state[head].total_in_len =3D total_in_len; + + /* + * A driver MUST NOT make the first descriptor in the list + * available before all subsequent descriptors comprising + * the list are made available. + */ + virtio_wmb(vq->weak_barriers); + vq->packed.vring.desc[head].flags =3D head_flags; + vq->num_added +=3D total_sg; + + pr_debug("Added buffer head %i to %p\n", head, vq); + END_USE(vq); + + return 0; + +unmap_release: + err_idx =3D i; + i =3D head; + vq->packed.avail_used_flags =3D avail_used_flags; + vq->packed.avail_wrap_counter =3D avail_wrap_counter; + + for (n =3D 0; n < total_sg; n++) { + if (i =3D=3D err_idx) + break; + vring_unmap_extra_packed(vq, &vq->packed.desc_extra[i]); + i++; + if (i >=3D vq->packed.vring.num) + i =3D 0; + } + + END_USE(vq); + return -EIO; +} + static bool virtqueue_kick_prepare_packed(struct vring_virtqueue *vq) { u16 new, old, off_wrap, flags, wrap_counter, event_idx; @@ -1795,10 +2080,82 @@ static void update_last_used_idx_packed(struct vrin= g_virtqueue *vq, cpu_to_le16(vq->last_used_idx)); } =20 +static bool more_used_packed_in_order(const struct vring_virtqueue *vq) +{ + if (vq->batch_last.id !=3D UINT_MAX) + return true; + + return virtqueue_poll_packed(vq, READ_ONCE(vq->last_used_idx)); +} + +static void *virtqueue_get_buf_ctx_packed_in_order(struct vring_virtqueue = *vq, + unsigned int *len, + void **ctx) +{ + unsigned int num =3D vq->packed.vring.num; + u16 last_used, last_used_idx; + bool used_wrap_counter; + void *ret; + + START_USE(vq); + + if (unlikely(vq->broken)) { + END_USE(vq); + return NULL; + } + + last_used_idx =3D vq->last_used_idx; + used_wrap_counter =3D packed_used_wrap_counter(last_used_idx); + last_used =3D packed_last_used(last_used_idx); + + if (vq->batch_last.id =3D=3D UINT_MAX) { + if (!more_used_packed_in_order(vq)) { + pr_debug("No more buffers in queue\n"); + END_USE(vq); + return NULL; + } + /* Only get used elements after they have been exposed by host. */ + virtio_rmb(vq->weak_barriers); + vq->batch_last.id =3D + le16_to_cpu(vq->packed.vring.desc[last_used].id); + vq->batch_last.len =3D + le32_to_cpu(vq->packed.vring.desc[last_used].len); + } + + if (vq->batch_last.id =3D=3D last_used) { + vq->batch_last.id =3D UINT_MAX; + *len =3D vq->batch_last.len; + } else { + *len =3D vq->packed.desc_state[last_used].total_in_len; + } + + if (unlikely(last_used >=3D num)) { + BAD_RING(vq, "id %u out of range\n", last_used); + return NULL; + } + if (unlikely(!vq->packed.desc_state[last_used].data)) { + BAD_RING(vq, "id %u is not a head!\n", last_used); + return NULL; + } + + /* detach_buf_packed clears data, so grab it now. */ + ret =3D vq->packed.desc_state[last_used].data; + detach_buf_packed_in_order(vq, last_used, ctx); + + update_last_used_idx_packed(vq, last_used, last_used, + used_wrap_counter); + + LAST_ADD_TIME_INVALID(vq); + + END_USE(vq); + return ret; +} + static void *virtqueue_get_buf_ctx_packed(struct vring_virtqueue *vq, unsigned int *len, void **ctx) { + unsigned int num =3D vq->packed.vring.num; u16 last_used, id, last_used_idx; bool used_wrap_counter; void *ret; @@ -1825,7 +2182,7 @@ static void *virtqueue_get_buf_ctx_packed(struct vrin= g_virtqueue *vq, id =3D le16_to_cpu(vq->packed.vring.desc[last_used].id); *len =3D le32_to_cpu(vq->packed.vring.desc[last_used].len); =20 - if (unlikely(id >=3D vq->packed.vring.num)) { + if (unlikely(id >=3D num)) { BAD_RING(vq, "id %u out of range\n", id); return NULL; } @@ -1966,7 +2323,10 @@ static void *virtqueue_detach_unused_buf_packed(stru= ct vring_virtqueue *vq) continue; /* detach_buf clears data, so grab it now. */ buf =3D vq->packed.desc_state[i].data; - detach_buf_packed(vq, i, NULL); + if (virtqueue_is_in_order(vq)) + detach_buf_packed_in_order(vq, i, NULL); + else + detach_buf_packed(vq, i, NULL); END_USE(vq); return buf; } @@ -1992,6 +2352,8 @@ static struct vring_desc_extra *vring_alloc_desc_extr= a(unsigned int num) for (i =3D 0; i < num - 1; i++) desc_extra[i].next =3D i + 1; =20 + desc_extra[num - 1].next =3D 0; + return desc_extra; } =20 @@ -2123,10 +2485,17 @@ static void virtqueue_vring_attach_packed(struct vr= ing_virtqueue *vq, { vq->packed =3D *vring_packed; =20 - /* Put everything in free lists. */ - vq->free_head =3D 0; + if (virtqueue_is_in_order(vq)) { + vq->batch_last.id =3D UINT_MAX; + } else { + /* + * Put everything in free lists. Note that + * next_avail_idx is sufficient with IN_ORDER so + * free_head is unused. + */ + vq->free_head =3D 0; + } } - static void virtqueue_reset_packed(struct vring_virtqueue *vq) { memset(vq->packed.vring.device, 0, vq->packed.event_size_in_bytes); @@ -2170,13 +2539,14 @@ static struct virtqueue *__vring_new_virtqueue_pack= ed(unsigned int index, #else vq->broken =3D false; #endif - vq->layout =3D VQ_LAYOUT_PACKED; vq->map =3D map; vq->use_map_api =3D vring_use_map_api(vdev); =20 vq->indirect =3D virtio_has_feature(vdev, VIRTIO_RING_F_INDIRECT_DESC) && !context; vq->event =3D virtio_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX); + vq->layout =3D virtio_has_feature(vdev, VIRTIO_F_IN_ORDER) ? + VQ_LAYOUT_PACKED_IN_ORDER : VQ_LAYOUT_PACKED; =20 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) vq->weak_barriers =3D false; @@ -2286,6 +2656,34 @@ static const struct virtqueue_ops packed_ops =3D { .reset =3D virtqueue_reset_packed, }; =20 +static const struct virtqueue_ops split_in_order_ops =3D { + .add =3D virtqueue_add_split, + .get =3D virtqueue_get_buf_ctx_split_in_order, + .kick_prepare =3D virtqueue_kick_prepare_split, + .disable_cb =3D virtqueue_disable_cb_split, + .enable_cb_delayed =3D virtqueue_enable_cb_delayed_split, + .enable_cb_prepare =3D virtqueue_enable_cb_prepare_split, + .poll =3D virtqueue_poll_split, + .detach_unused_buf =3D virtqueue_detach_unused_buf_split, + .more_used =3D more_used_split_in_order, + .resize =3D virtqueue_resize_split, + .reset =3D virtqueue_reset_split, +}; + +static const struct virtqueue_ops packed_in_order_ops =3D { + .add =3D virtqueue_add_packed_in_order, + .get =3D virtqueue_get_buf_ctx_packed_in_order, + .kick_prepare =3D virtqueue_kick_prepare_packed, + .disable_cb =3D virtqueue_disable_cb_packed, + .enable_cb_delayed =3D virtqueue_enable_cb_delayed_packed, + .enable_cb_prepare =3D virtqueue_enable_cb_prepare_packed, + .poll =3D virtqueue_poll_packed, + .detach_unused_buf =3D virtqueue_detach_unused_buf_packed, + .more_used =3D more_used_packed_in_order, + .resize =3D virtqueue_resize_packed, + .reset =3D virtqueue_reset_packed, +}; + static int virtqueue_disable_and_recycle(struct virtqueue *_vq, void (*recycle)(struct virtqueue *vq, void *buf)) { @@ -2340,6 +2738,12 @@ static int virtqueue_enable_after_reset(struct virtq= ueue *_vq) case VQ_LAYOUT_PACKED: \ ret =3D packed_ops.op(__VIRTQUEUE_CALL_vq, ##__VA_ARGS__);\ break; \ + case VQ_LAYOUT_SPLIT_IN_ORDER: \ + ret =3D split_in_order_ops.op(vq, ##__VA_ARGS__); \ + break; \ + case VQ_LAYOUT_PACKED_IN_ORDER: \ + ret =3D packed_in_order_ops.op(vq, ##__VA_ARGS__); \ + break; \ default: \ BUG(); \ break; \ @@ -2358,6 +2762,12 @@ static int virtqueue_enable_after_reset(struct virtq= ueue *_vq) case VQ_LAYOUT_PACKED: \ packed_ops.op(__VIRTQUEUE_CALL_vq, ##__VA_ARGS__); \ break; \ + case VQ_LAYOUT_SPLIT_IN_ORDER: \ + split_in_order_ops.op(vq, ##__VA_ARGS__); \ + break; \ + case VQ_LAYOUT_PACKED_IN_ORDER: \ + packed_in_order_ops.op(vq, ##__VA_ARGS__); \ + break; \ default: \ BUG(); \ break; \ @@ -3077,6 +3487,8 @@ void vring_transport_features(struct virtio_device *v= dev) break; case VIRTIO_F_NOTIFICATION_DATA: break; + case VIRTIO_F_IN_ORDER: + break; default: /* We don't understand this bit. */ __virtio_clear_bit(vdev, i); --=20 2.31.1