linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jason Wang <jasowang@redhat.com>
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 V2 16/19] virtio_ring: factor out core logic for updating last_used_idx
Date: Wed, 28 May 2025 14:42:31 +0800	[thread overview]
Message-ID: <20250528064234.12228-17-jasowang@redhat.com> (raw)
In-Reply-To: <20250528064234.12228-1-jasowang@redhat.com>

Factor out the core logic for updating last_used_idx to be reused by
the packed in order implementation.

Reviewed-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 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 1bdd332d515e..e8e0d1204f52 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -1757,6 +1757,30 @@ static bool more_used_packed(const struct vring_virtqueue *vq)
 	return virtqueue_poll_packed(vq, READ_ONCE(vq->last_used_idx));
 }
 
+static void update_last_used_idx_packed(struct vring_virtqueue *vq,
+					u16 id, u16 last_used,
+					u16 used_wrap_counter)
+{
+	last_used += vq->packed.desc_state[id].num;
+	if (unlikely(last_used >= vq->packed.vring.num)) {
+		last_used -= vq->packed.vring.num;
+		used_wrap_counter ^= 1;
+	}
+
+	last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_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 == 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)
@@ -1800,24 +1824,7 @@ static void *virtqueue_get_buf_ctx_packed(struct vring_virtqueue *vq,
 	ret = vq->packed.desc_state[id].data;
 	detach_buf_packed(vq, id, ctx);
 
-	last_used += vq->packed.desc_state[id].num;
-	if (unlikely(last_used >= vq->packed.vring.num)) {
-		last_used -= vq->packed.vring.num;
-		used_wrap_counter ^= 1;
-	}
-
-	last_used = (last_used | (used_wrap_counter << VRING_PACKED_EVENT_F_WRAP_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 == 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);
 
 	LAST_ADD_TIME_INVALID(vq);
 
-- 
2.31.1


  parent reply	other threads:[~2025-05-28  6:43 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-28  6:42 [PATCH V2 00/19] virtio_ring in order support Jason Wang
2025-05-28  6:42 ` [PATCH V2 01/19] virtio_ring: rename virtqueue_reinit_xxx to virtqueue_reset_xxx() Jason Wang
2025-05-28  6:42 ` [PATCH V2 02/19] virtio_ring: switch to use vring_virtqueue in virtqueue_poll variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 03/19] virtio_ring: unify logic of virtqueue_poll() and more_used() Jason Wang
2025-05-28  6:42 ` [PATCH V2 04/19] virtio_ring: switch to use vring_virtqueue for virtqueue resize variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 05/19] virtio_ring: switch to use vring_virtqueue for virtqueue_kick_prepare variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 06/19] virtio_ring: switch to use vring_virtqueue for virtqueue_add variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 07/19] virtio: " Jason Wang
2025-05-28  6:42 ` [PATCH V2 08/19] virtio_ring: switch to use vring_virtqueue for enable_cb_prepare variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 09/19] virtio_ring: use vring_virtqueue for enable_cb_delayed variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 10/19] virtio_ring: switch to use vring_virtqueue for disable_cb variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 11/19] virtio_ring: switch to use vring_virtqueue for detach_unused_buf variants Jason Wang
2025-05-28  6:42 ` [PATCH V2 12/19] virtio_ring: use u16 for last_used_idx in virtqueue_poll_split() Jason Wang
2025-05-28  6:42 ` [PATCH V2 13/19] virtio_ring: introduce virtqueue ops Jason Wang
2025-05-28  6:42 ` [PATCH V2 14/19] virtio_ring: determine descriptor flags at one time Jason Wang
2025-05-28  6:42 ` [PATCH V2 15/19] virtio_ring: factor out core logic of buffer detaching Jason Wang
2025-05-28  6:42 ` Jason Wang [this message]
2025-05-28  6:42 ` [PATCH V2 17/19] virtio_ring: factor out split indirect detaching logic Jason Wang
2025-05-28  6:42 ` [PATCH V2 18/19] virtio_ring: factor out split " Jason Wang
2025-05-28  6:42 ` [PATCH V2 19/19] virtio_ring: add in order support Jason Wang
2025-05-28  7:10 ` [PATCH V2 00/19] virtio_ring " Eugenio Perez Martin
2025-05-28 12:50 ` Michael S. Tsirkin
2025-05-29  2:23   ` Jason Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250528064234.12228-17-jasowang@redhat.com \
    --to=jasowang@redhat.com \
    --cc=eperezma@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=virtualization@lists.linux.dev \
    --cc=xuanzhuo@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).