From nobody Thu Oct 2 09:17:06 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 B3EF82EB5A5 for ; Fri, 19 Sep 2025 07:33:03 +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=1758267185; cv=none; b=MkmKqnPFsA4C7/Ornsh0Qh0WrOt9eVuBMnT9d4z6YkhZIn3o2ttpYW229CGi04dC0u7+VlIoqGfh1ThkWfnqRceZ79oQUyv4P7Zn1I0SMPzR9Bs+b7L6d2KPadKmJ0PMjTLg084tf2BipaF493LAzi8ncR6/EcK6fOgYWdOQrio= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267185; c=relaxed/simple; bh=t6B+ZbY/VMcwKcfhAYHqTcwHa/Xiu7FndQUCge126B8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cfmH3+DrJP8WTLAufwkJ7weoiSeun5rZiIIS5JFu22ahp5ckfTAFPsfI3WyKUvfnbZe9dCQah7BdlBOOlfxO/2TfihlYTJ5ZrEVagFQkMi7BkTTlw6aQcnCmiTn/HZKiYR6/PEU3o7gUWJBk3g31zzc8ROnlzfj/PWM9wfejMUg= 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=ctVQocnr; 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="ctVQocnr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SZwWaRgTPeERiAS+TPU75pItPhxCQoG0D3EUtPVk6C4=; b=ctVQocnrOrSi/Tmvv/Ir/0hlJUeshD9hVOQ99mhB42YM5ORUS1jG4YuIGd5dhfE/HsUfgR 59zExcCD/59VtEJNyObS/2P/SEY7Ji/3XoP8mz0m+MtnMZbAqENMdX5lhdcKgMY1x/RbGF KhrXrImkBCUf2WPE9ENSv8l0OBGapcI= 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-187-UOnkIUgfNEuul44xeW7F_w-1; Fri, 19 Sep 2025 03:32:59 -0400 X-MC-Unique: UOnkIUgfNEuul44xeW7F_w-1 X-Mimecast-MFC-AGG-ID: UOnkIUgfNEuul44xeW7F_w_1758267178 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 7E2A11800451; Fri, 19 Sep 2025 07:32:58 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.112.45]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 771E119560BB; Fri, 19 Sep 2025 07:32:55 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, eperezma@redhat.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH V6 14/19] virtio_ring: determine descriptor flags at one time Date: Fri, 19 Sep 2025 15:31:49 +0800 Message-ID: <20250919073154.49278-15-jasowang@redhat.com> In-Reply-To: <20250919073154.49278-1-jasowang@redhat.com> References: <20250919073154.49278-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.12 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 | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 1045c553ee65..0949675a3d12 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, c =3D 0; int head; bool indirect; =20 @@ -631,6 +631,7 @@ static inline int virtqueue_add_split(struct vring_virt= queue *vq, } =20 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; @@ -638,12 +639,12 @@ static inline int virtqueue_add_split(struct vring_vi= rtqueue *vq, 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, + ++c =3D=3D total_sg ? + 0 : VRING_DESC_F_NEXT, premapped); } } @@ -655,21 +656,15 @@ static inline int virtqueue_add_split(struct vring_vi= rtqueue *vq, 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, + (++c =3D=3D total_sg ? 0 : VRING_DESC_F_NEXT) | + VRING_DESC_F_WRITE, 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