From nobody Wed Dec 17 06:00:26 2025 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 74AF826E71E for ; Mon, 16 Jun 2025 08:26:28 +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=1750062390; cv=none; b=lWYWgBTCZu3X/EB6FV9TKUHawIJtedLS5mTL5hSB5MbDt/pHm/34P0WOqvS3qafYnbG6OyUckioyLz8vVwQLCwg4VRlVJBiVxYSRI7nGaK57I38hTcxsjM96joyiBQ/dgJRJ0bFAEFubrcTHkxaDjomLiGbOsHwy+9hMFg6wsDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750062390; c=relaxed/simple; bh=7ibluwZ05Ku8GxFsKg/HCDY7lLwD+ET4m094INhsKUo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nKA/tLrndYLhjZoEGQzXNpzsDgGytBGhhZH/1bgCHVWYjU4T4bDjDSv+yPfwga9irAS2wUdRbuXxwst4KKhUpL8t7VIH5mYlGraDy+nxtjBPojHvsh7WRXwMjsfsnYF6bUP3Xs9g3GnWC8oTFMoOO/ZGTXbillDqbbWyCbYfpVc= 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=NVdi/BM+; 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="NVdi/BM+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750062387; 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=nm3UmQwERzvwBQjnk71E9B+OidwwIVFS/Ow+WmEslM4=; b=NVdi/BM+A8r1K3V5mABq84wGZWWAd9mN+oCsAsPBusfxp6g6lCC5pink/NtvAKRN+x8x2l 8O0GfZl/EGiLDKnhRUXtEpxilTAUOJH+WbScg1pkhUyI5cTwywC1C6jb71kQYuuqUnvjk7 rMk+EpzOEF6quI7fvap/Zt7JDVj1UeA= Received: from mx-prod-mc-04.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-121-AsT5ybUWOim3_H1gF9UZtw-1; Mon, 16 Jun 2025 04:26:23 -0400 X-MC-Unique: AsT5ybUWOim3_H1gF9UZtw-1 X-Mimecast-MFC-AGG-ID: AsT5ybUWOim3_H1gF9UZtw_1750062382 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B50BF19560BC; Mon, 16 Jun 2025 08:26:22 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.72]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 90D1B180045C; Mon, 16 Jun 2025 08:26: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 V3 14/19] virtio_ring: determine descriptor flags at one time Date: Mon, 16 Jun 2025 16:25:12 +0800 Message-ID: <20250616082518.10411-15-jasowang@redhat.com> In-Reply-To: <20250616082518.10411-1-jasowang@redhat.com> References: <20250616082518.10411-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.4.1 on 10.30.177.111 Content-Type: text/plain; charset="utf-8" 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. Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index af32d1a1a1db..d5e4d4cd2487 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -570,7 +570,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, c, avail, descs_used, err_idx; int head; bool indirect; =20 @@ -626,46 +626,47 @@ static inline int virtqueue_add_split(struct vring_vi= rtqueue *vq, return -ENOSPC; } =20 + c =3D 0; for (n =3D 0; n < out_sgs; n++) { + sg =3D sgs[n]; for (sg =3D sgs[n]; sg; sg =3D sg_next(sg)) { dma_addr_t addr; u32 len; + u16 flags =3D 0; =20 if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr, &len, premapped)) goto unmap_release; =20 - prev =3D i; + if (++c !=3D total_sg) + flags =3D VRING_DESC_F_NEXT; + /* 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, + flags, premapped); } } for (; n < (out_sgs + in_sgs); n++) { for (sg =3D sgs[n]; sg; sg =3D sg_next(sg)) { + u16 flags =3D VRING_DESC_F_WRITE; dma_addr_t addr; u32 len; =20 if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr, &len, premapped)) goto unmap_release; =20 - prev =3D i; + if (++c !=3D total_sg) + flags |=3D VRING_DESC_F_NEXT; + /* 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); + 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.34.1