From mboxrd@z Thu Jan 1 00:00:00 1970 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 0547A2DF6E3 for ; Mon, 20 Oct 2025 07:11:14 +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=1760944276; cv=none; b=VbzuH7aJtbP4iQ4i0POL4AAxCWd9cklXYhtP0d7X6JqAxKbGZZJgO1VVEwdzcE1u5XJRTdtwhWL4fiEMfg53Jp86HP8CvhrTh++izel2oN2wD4DFKbh82QiQ/Qik6d0WjnKEq1sNsQEMTCRZtmu5VIZqS0lw7cNuWX86BGk953A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760944276; c=relaxed/simple; bh=dNNCy30nvPJf7Pj6/+q+qtxhShGub8YeDfIF1mCuNKo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Hch4rxSrEi9x+khLrZxMXf0oMOvzNvoH2L7vrOAoiyYukQ3n2O6Zmsf4dlCYnXVouvgIVNumK45SS97Z6W2hRAGlGzmTgxXQpOtWIaHZrfzxfa5mHR9EArGy2xr3eJsZ67JfqML1L5s0dmheRwN9gl1dM7pkUWWPXZCxCbay1QY= 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=ALwJjPBt; 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="ALwJjPBt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760944274; 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=CzFpi+MbdYGHcrbODt1UdYARRJA55E5Zp2RLvWcxMyk=; b=ALwJjPBts4/6kGJFGxAMvgZ7SN3ci3DnT2Ce7yDaLJPB5La3lAWqlTSNpgWH/AmahePMMC iFqhXkAyae7lxK3xsNl9NEE4BeUR2OWDCOeDn6419QuC7COjArFQuFuEIIyHUmz5l9phIQ AbSaFUReDqgVEcY9vzL8ZLHB25UTn0g= 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-19-6dUQ2vrJP82aoe3wfaOEPw-1; Mon, 20 Oct 2025 03:11:06 -0400 X-MC-Unique: 6dUQ2vrJP82aoe3wfaOEPw-1 X-Mimecast-MFC-AGG-ID: 6dUQ2vrJP82aoe3wfaOEPw_1760944265 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 4F7271956048; Mon, 20 Oct 2025 07:11:05 +0000 (UTC) Received: from localhost.localdomain (unknown [10.72.113.29]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 58A7D300019F; Mon, 20 Oct 2025 07:11:02 +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 V8 14/19] virtio_ring: determine descriptor flags at one time Date: Mon, 20 Oct 2025 15:09:58 +0800 Message-ID: <20251020071003.28834-15-jasowang@redhat.com> In-Reply-To: <20251020071003.28834-1-jasowang@redhat.com> References: <20251020071003.28834-1-jasowang@redhat.com> Precedence: bulk X-Mailing-List: virtualization@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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érez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 37b16ef906a4..20bc48b1241e 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -575,7 +575,7 @@ static inline int virtqueue_add_split(struct vring_virtqueue *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 = 0; int head; bool indirect; @@ -639,12 +639,11 @@ static inline int virtqueue_add_split(struct vring_virtqueue *vq, if (vring_map_one_sg(vq, sg, DMA_TO_DEVICE, &addr, &len, premapped)) goto unmap_release; - prev = i; /* Note that we trust indirect descriptor * table since it use stream DMA mapping. */ i = virtqueue_add_desc_split(vq, desc, extra, i, addr, len, - VRING_DESC_F_NEXT, + ++c == total_sg ? 0 : VRING_DESC_F_NEXT, premapped); } } @@ -656,21 +655,15 @@ static inline int virtqueue_add_split(struct vring_virtqueue *vq, if (vring_map_one_sg(vq, sg, DMA_FROM_DEVICE, &addr, &len, premapped)) goto unmap_release; - prev = i; /* Note that we trust indirect descriptor * table since it use stream DMA mapping. */ - i = virtqueue_add_desc_split(vq, desc, extra, i, addr, len, - VRING_DESC_F_NEXT | - VRING_DESC_F_WRITE, - premapped); + i = virtqueue_add_desc_split(vq, desc, extra, + i, addr, len, + (++c == total_sg ? 0 : VRING_DESC_F_NEXT) | + VRING_DESC_F_WRITE, premapped); } } - /* Last one doesn't continue. */ - desc[prev].flags &= 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 &= - ~VRING_DESC_F_NEXT; if (indirect) { /* Now that the indirect table is filled in, map it. */ -- 2.31.1