From nobody Thu Oct 2 07:43:52 2025 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 DD94D2D0C82 for ; Fri, 19 Sep 2025 07:32:10 +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=1758267132; cv=none; b=EaHFCSkcNLy0k/xDWtwn6NKII+AlOKDpnUbMjLmh4HpnWHPhmm1u+SKZHk3ajFItx0w8ezxmnV07caB/t7N//TrQJUAdqdV1BDEa9B9ZY0qkvX755mf014K1x//39eROC7wk6a+B+xj+PwenP/sziW33/iv5jPNa4ZsRCq27r7A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267132; c=relaxed/simple; bh=Prhd+LkqoYp0VJ2YSuQDc0m2lgDkGgYa2aWwnVG/VQw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=L7n5onTAPUgkUWZOeTu3CAfqOR1TjRA19pU49pcAlsse9Pcj4xOCMeDIzJP6N9IlBJLtjPb2S4jhpRafeQoYCx3rYK44pwfbO386nEm57vL8tynzssDOKdtHnNNDDD4ZOJzZufHoikkgL4KamyLXzCWqnLls4oWrnvDQfQqTAlI= 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=Cc7BLB64; 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="Cc7BLB64" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267129; 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=9HvYkyijRiL6uD+3Th5PyNitUXfDZOQ8r0roTL8rtAQ=; b=Cc7BLB648KsE3eexxWrYp/hsbE1OvO2ByrVsBHdNm6lfRayIsxxe1W+BH1ZImZiIw319Jj mL1WO2pq8s2M4gAqjB2gD2hyIGfCi2DRW4OodM6qEi8oCMOBcMJWzsXbZi/a02C+GOQA9V QljhOyrKCrQuaMUqllRdZIDeNowIh5Y= 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-96-LGPLmJFWPKWtApI-EQLuCw-1; Fri, 19 Sep 2025 03:32:07 -0400 X-MC-Unique: LGPLmJFWPKWtApI-EQLuCw-1 X-Mimecast-MFC-AGG-ID: LGPLmJFWPKWtApI-EQLuCw_1758267126 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3992C18002D6; Fri, 19 Sep 2025 07:32:06 +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 D325F19560BB; Fri, 19 Sep 2025 07:32: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 V6 01/19] virtio_ring: rename virtqueue_reinit_xxx to virtqueue_reset_xxx() Date: Fri, 19 Sep 2025 15:31:36 +0800 Message-ID: <20250919073154.49278-2-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 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 f91a432b3e53..73790593523a 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 Thu Oct 2 07:43:52 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 A3AA42D0C82 for ; Fri, 19 Sep 2025 07:32:16 +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=1758267139; cv=none; b=rE6KofngY/hVMaIzSUSLOY/XrTMDh7p8DxLqrrJgxMJUH95yjfX68a1nA74jSns54Rh2TFCa2YH+uK9Rig8BymwGUy6SespdLd6FR/l2BMOSdkUHc6ZTbBbnLsx6gJsAkT9sA9ljccVDTgK9kFe9LARjIjsHAs6DdoTK7j7XO9M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267139; c=relaxed/simple; bh=HYsHOGN3EYNOH6tOJeaoOb+gHk0SE0FJEJ4VPLlA9Z4=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TXkDo8UJ6NbjDCGHeqexXaXKB+PpLOQTsonV5clknLTb6gNndpRz/OXPLMDyDvWgKNC63F73ZnVphowHno2LpE5uWrMEU8xtJdNVDkQqYSXgoodIVJHwYBZnL65vQXE0FnFaNgB/DWVKq7zuH8FylTyTMEMt2VLKO5I18v6dmRU= 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=FTIu0oW9; 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="FTIu0oW9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267135; 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=JDhkOP2irC9AYhWmwD5Iofw1hRzZ9N7g2ReMYfK0ONA=; b=FTIu0oW9v4CggfwfauqPvA6bTF9JakPIgTidUd56lqd0bPVQuOaCcplP8TBRFOuKZ5HTae uI+EQxOOwB/IkzfBedcLo5NVBKCK93VsPzSvywv2XI7rPf+Z96jUSDTgfyS1bTv/6f2mK4 DhFVb0AHXfJiiNW6wkKzzSY2dPTL2as= 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-507-S4wRZITPOz2BXZ1Z_P9p1g-1; Fri, 19 Sep 2025 03:32:11 -0400 X-MC-Unique: S4wRZITPOz2BXZ1Z_P9p1g-1 X-Mimecast-MFC-AGG-ID: S4wRZITPOz2BXZ1Z_P9p1g_1758267130 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 157931800562; Fri, 19 Sep 2025 07:32:10 +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 ECADD19560BB; Fri, 19 Sep 2025 07:32:06 +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 02/19] virtio_ring: switch to use vring_virtqueue in virtqueue_poll variants Date: Fri, 19 Sep 2025 15:31:37 +0800 Message-ID: <20250919073154.49278-3-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 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 73790593523a..fed3962411a1 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 Thu Oct 2 07:43:52 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 617432C11DB for ; Fri, 19 Sep 2025 07:32:18 +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=1758267141; cv=none; b=PHbEKyp6Rxj0cLbsTn2dsjNXQ96h2JTCQLSohzSPjPMqhILu+ai5x5YxkSRwtg+O9bLc1AdiyKFXUZNn9wvYkR1pqSrLsPTYBxEYTITnHuNzVbVzVz3TYbD9umfVRlPw1nuoOVVNcyeUthvsEgxtKR0LGkalw8+UH7CN0H64izE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267141; c=relaxed/simple; bh=4t6ixKfADhlr9xzvfuDQl77U06Jc1JbbTyleI9sfI7U=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=uvTES3Tu1zJ5eF0zjMh9TKDXMKRUFbEylquLyTSihB6gHPZaP4CAUBD5/wzNT5MQA2cfL3hxqRJbi7Rruuk1Zwks7Vz/hMk0lHp58R9nmrQWvCYmbR23AI/tU4f3tEEq12Z8wsTgkptK1JJnZs0nq9rtyZsjUk7pYnIFGhsso9Q= 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=DNWxEp5p; 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="DNWxEp5p" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267138; 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=vFIZuPKUpfLWKhMoVS+8GFV45HPQ85k5vLQT16WEDbY=; b=DNWxEp5pWTquR9M9Yf9hgsjnjKaONliNjgoA/nybsOVHgpkYRu23IgZSdlEMjTy+ZNRfkJ naHSNbkTPkeI6Nd0SQxi+v3KyBkksclJRL78PIIjZBzKS25JkH6T/wUiI4i5E+uf0Hr53/ TGFXjM4TIAoReBmgewmLAONpAx7lRHQ= 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-127-nsbn7sWgO7ydFh65dozYOg-1; Fri, 19 Sep 2025 03:32:14 -0400 X-MC-Unique: nsbn7sWgO7ydFh65dozYOg-1 X-Mimecast-MFC-AGG-ID: nsbn7sWgO7ydFh65dozYOg_1758267134 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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EE30119560A2; Fri, 19 Sep 2025 07:32:13 +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 DE9D81956045; Fri, 19 Sep 2025 07:32:10 +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 03/19] virtio_ring: unify logic of virtqueue_poll() and more_used() Date: Fri, 19 Sep 2025 15:31:38 +0800 Message-ID: <20250919073154.49278-4-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 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 fed3962411a1..d8a07e0d9fa8 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 Thu Oct 2 07:43:52 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 E007F2D29D7 for ; Fri, 19 Sep 2025 07:32:24 +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=1758267146; cv=none; b=je1f+IfQRt385vnEKkl1v2JAyCU2YEVN/jTMJ8UpuVdZXi9yIpac/TvoYdH+ZWi3ApGWn85vi1ITN+Rgkh5mzb2VsubUhCVsScYauFm1VfQKDHbgKYkA4BcKKGLEqo7rqlLdUZNtiLloy5+NrvF/c7AKxoAvLRB+MpsHtxAZivs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267146; c=relaxed/simple; bh=92dzLgz7GPIFGxrV7u59xZvyZkD4ZOwGTYSuMOriM2E=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BZY4DOK0Tvjm3q7w3wshomHsevqGRWT91fEmVMckzr8BlgyfdokY0+gQj8+2rVWvMVfm+JmLeVsY56lWB2DgoEP/eiD0dklS11Pm4mAf/uEaF5OloNLGtqxpzZlXGDT9s2FwGtp6Wn8alBA94bfVChlblIpUm3rYLfVQLlRhE/g= 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=iXhdb38c; 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="iXhdb38c" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267143; 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=/9X+8nJzs108oA3zz8mRtrO71MkKZ7x5Ta9F7UOdDxY=; b=iXhdb38cXxusMR9xWXcoRTBbWlJHf6nvybacll8C5q27xbXM3nnTTP6Suo7mwcUoOr1zTc IN1s5QKEnzLToQbRGANh+W13DqwTlo2+51z9L2q1Kd+x/sBBqSFb7jpbdzjrkEDMWNMEA6 PgcU95hzG5mKoRcZjFLa4XvBKmyRL2g= 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-247--pLkI3JeNPqYgCdLK4PJOw-1; Fri, 19 Sep 2025 03:32:19 -0400 X-MC-Unique: -pLkI3JeNPqYgCdLK4PJOw-1 X-Mimecast-MFC-AGG-ID: -pLkI3JeNPqYgCdLK4PJOw_1758267138 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 329561800451; Fri, 19 Sep 2025 07:32:18 +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 E98E819560BB; Fri, 19 Sep 2025 07:32:14 +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 04/19] virtio_ring: switch to use vring_virtqueue for virtqueue resize variants Date: Fri, 19 Sep 2025 15:31:39 +0800 Message-ID: <20250919073154.49278-5-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 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 d8a07e0d9fa8..693671bac841 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 Thu Oct 2 07:43:52 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 470582D0627 for ; Fri, 19 Sep 2025 07:32:27 +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=1758267148; cv=none; b=t2H1cvhvhl+rn/AnpYipHtxFL/wym0CoS0lOesRUlZ5tvdEz652fBq0Ut4cMHqCmvAPOqPzYt2FM2O0Ui+H5bUaFcqiAJo7Z1AuXlgVMuzzyTqNAyUeoP8acpmj7JpwbrgZ9TQeKXKDLCbN4xoUrSsxzsnPS82UUPJ0RLRjp/KQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267148; c=relaxed/simple; bh=/P9F+XZD4Jw4baApW319M4i8p1Tx8WVEcfea/cWb69Q=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WD4m2IfwWjW20JndXiBu5hFl6j1m0GsSzBx5fxQqFxHduZuSfXbnm28eLuUDLTCjZDdVGUo6qD6KpUcjyW8TwDtFNPxPgF7P8p1t2R0dczeFHnUv6LGidS6gba1r8Rx1L3hBUxIYT3aO3DE7zmRZcx9nhzIXx8UxzpdQrXbH8NE= 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=IA9r7lo7; 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="IA9r7lo7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267146; 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=RirLQpnZ2Apia0UR72xDNP7krHXcdWwsna0GSCf+FV4=; b=IA9r7lo7xyiK8y69fv5oKH5hRkVqxHqvmVcIo3Sk9zNnLNPi6I13RlEySsxudeLCXk0sNF xGmihic7QJ+rlmkTI6di7bqcHndjmmLuIEIveCrnYO0rmyuRaDrkQjAhh3oJqgBKwcg4Uf EfQluL2sshV/M5NhYRsfrrkG2LThbTI= 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-117-aRdOv39QMK2vfLJzXSByLA-1; Fri, 19 Sep 2025 03:32:23 -0400 X-MC-Unique: aRdOv39QMK2vfLJzXSByLA-1 X-Mimecast-MFC-AGG-ID: aRdOv39QMK2vfLJzXSByLA_1758267142 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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F1767195608B; Fri, 19 Sep 2025 07:32:21 +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 E77DC19560BB; Fri, 19 Sep 2025 07:32:18 +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 05/19] virtio_ring: switch to use vring_virtqueue for virtqueue_kick_prepare variants Date: Fri, 19 Sep 2025 15:31:40 +0800 Message-ID: <20250919073154.49278-6-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 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 693671bac841..aadeab66e57c 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 Thu Oct 2 07:43:52 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 3677A2D46C6 for ; Fri, 19 Sep 2025 07:32: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=1758267152; cv=none; b=SxCtHiZiGzF8VFlppJ5hiGs6U8KML00TGDWYWi7l3wQGFv6HZOErQwXa0j4DYWb7FupWY0/mDtcqez2UO8WJWVbF3lbAJXc+xBD4/aq6Q3uczh58c5SpZnsbBgaq/TG7KxCqtvrHB0RFcyGX0Qy7xeJX61QuxPn/blKfU3hz41U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267152; c=relaxed/simple; bh=4QjQQt7FhlBMMNfCdijzFIrxETBMVjHBCei8Cag4cNk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=TCZz5hAoQUOy9U/LcThcGvxnu05C/cniKPsrcaB20YPYE4G56SWYGojmFQcO9UmlX8liSp9/mqdNXdQwWC+Yz5yE5xSjnFV7RRzv5YpISvHbKQ2THtawyxtEDdQRFjW1n7HFVVpcXoee27XisuDuusMeLHdF7n9SIvSrXB2yl4E= 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=Qi9vMu7+; 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="Qi9vMu7+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267150; 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=qqVjgRVYTyXk4aefzATba9pEScbbb4drMUS+64g1cTw=; b=Qi9vMu7+pBkwDvHtuuSyrp1KhotmfJ0Xl1HA3MLycIExNkUmBpzqv38KPcWVw1co8QQAsH 0Lw9/g/ZSKqIxTtCBTWyFtJEo7sp5R3x7EQ35bi47gT1oxv3sLUilmup7nVyIy7clW24y+ IT8VjeYnCTucqbIy/21Iiwl11TpLcNI= 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-523-5cSZeCaRNKi5eBRFXnaFkg-1; Fri, 19 Sep 2025 03:32:26 -0400 X-MC-Unique: 5cSZeCaRNKi5eBRFXnaFkg-1 X-Mimecast-MFC-AGG-ID: 5cSZeCaRNKi5eBRFXnaFkg_1758267145 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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BD9331956058; Fri, 19 Sep 2025 07:32:25 +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 B1CAF19560BB; Fri, 19 Sep 2025 07:32:22 +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 06/19] virtio_ring: switch to use vring_virtqueue for virtqueue_add variants Date: Fri, 19 Sep 2025 15:31:41 +0800 Message-ID: <20250919073154.49278-7-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 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 | 40 +++++++++++++++++------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index aadeab66e57c..93c36314b5e7 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 @@ -706,7 +706,6 @@ static inline int virtqueue_add_split(struct virtqueue = *_vq, for (n =3D 0; n < total_sg; n++) { if (i =3D=3D err_idx) break; - i =3D vring_unmap_one_split(vq, &extra[i]); } =20 @@ -1440,7 +1439,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 +1449,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 +2260,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 Thu Oct 2 07:43:52 2025 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 AFA622D46A9 for ; Fri, 19 Sep 2025 07:32:35 +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=1758267157; cv=none; b=oOZn+m1rla7B45xhfQfigIOJdby7HS16NgtUYEoA1VgVUXZG8Z3RTUX/MC/wkX/1PNxgWJ40svicjQrN240a01JagGFwSjByn4qKBzUKwriZW0YfPaZmUGDSEd6uJafAOJ6KAvHTn6YmjrUE2vkdQbWqvbX2qUajsfjD8OPTIXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267157; c=relaxed/simple; bh=YT3vrhS+PUCC+eZtNTyINuu6CeMWKNqHlIf2nyOKauw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=b002VTe/uzCeBpN0tePgdTh2GwSS2LMjS4hXxWJIH8ggC9d5voj5W9toCltet/1LskWLkAnUG34H7w8hiukTuakC/3CqxCEnxJtdWVqFN3fTE+W7qVK+POQUV/Zp6+GjBj/ZJK1rm26kgxuzZZXARyf+XXg5Kb4ofwPneVdLRxA= 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=LrQ5WLMk; 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="LrQ5WLMk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267154; 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=oQNJDdpKQN4RItYG+T6p4p6o0SV+o4+2aVHoBVZKfjc=; b=LrQ5WLMkd0s8oywqSrqSxfkNFZrm1mQLke3j+SgdHMcMbropRcbrnys8DkMqbUYUZPzBY+ lfbbPwe/dD5oytoa8L2QwLuSWP6rJ4exy8BTWV5pVE7a4wpiqV0eOshsvXei7PlZqdBhri N1ELGFpX2i3WDOK9bVcMWL4MvihUg/w= 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-86-remhx1rwO6iOy5zSVdwKVg-1; Fri, 19 Sep 2025 03:32:30 -0400 X-MC-Unique: remhx1rwO6iOy5zSVdwKVg-1 X-Mimecast-MFC-AGG-ID: remhx1rwO6iOy5zSVdwKVg_1758267149 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 B235F18004D4; Fri, 19 Sep 2025 07:32:29 +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 AC8B51956048; Fri, 19 Sep 2025 07:32:26 +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 07/19] virtio: switch to use vring_virtqueue for virtqueue_add variants Date: Fri, 19 Sep 2025 15:31:42 +0800 Message-ID: <20250919073154.49278-8-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 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 93c36314b5e7..3625c7ed2fbb 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -816,11 +816,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; @@ -842,9 +841,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)) { @@ -866,7 +865,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 @@ -1720,11 +1719,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; @@ -2524,8 +2522,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 Thu Oct 2 07:43:52 2025 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 7F16C2D46C6 for ; Fri, 19 Sep 2025 07:32:37 +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=1758267159; cv=none; b=YX1W8gx3IGUwREIFGdWGwGHnyaVIiFsBCHeJqtVe4FrowHqLT66wqMaq4q0a7yXXUYQO/351yTCs2Xq+jEuZK3eFB8vA4rUjm6ygBF2zyPS6eesKG0s0mXPtroK3SUnRHi7L1oJ/Y1buEB9xSraQly9pQ4pujJ8jBhh71WBI+TA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267159; c=relaxed/simple; bh=7zljtCVbcHvjJV3/tYZqzrREfe2J4B4MpPf8LrTkJnY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lSnWqRvMv215zsBppmMntIwz/5wWl+S9En8hwMYSJRiHZBAi/emY5AuAxy0ZHIjBBYrE5LGjdvsgxPQbYdVjLdUBvVmzwL18oqU+On0frAvSUbP3pvFkgzXCYtiKLA8dv2i0Exv1NJFecgd4Uo3jROoXmON/LV2AeksX1REnRC8= 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=KAjv9wBH; 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="KAjv9wBH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267156; 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=3a8IWtyWZ8b41J55mepjRKgRGiV+wNXwzgNi9byEBXk=; b=KAjv9wBH2oRlAUdUoJD5DvjcYuXoRTEgfjOWj98okPiy1yRZfrPSd87f48+lER+bydXo+U 4HAwIjDS0u04zbenzk1aYjgseo8s7haEtfdm27zMYh90l3jVNcQMKjtE1SNqEiPAjlxM4q uYH0QRF9kdXfIg8SwApY/4t0xbn96ZA= Received: from mx-prod-mc-02.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-554-CRV-S9O0OdywLU-1lRvc7w-1; Fri, 19 Sep 2025 03:32:34 -0400 X-MC-Unique: CRV-S9O0OdywLU-1lRvc7w-1 X-Mimecast-MFC-AGG-ID: CRV-S9O0OdywLU-1lRvc7w_1758267153 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C1DB1195609F; Fri, 19 Sep 2025 07:32:33 +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 AEAC919560BB; Fri, 19 Sep 2025 07:32:30 +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 08/19] virtio_ring: switch to use vring_virtqueue for enable_cb_prepare variants Date: Fri, 19 Sep 2025 15:31:43 +0800 Message-ID: <20250919073154.49278-9-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 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 3625c7ed2fbb..cb013af446f9 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -897,9 +897,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); @@ -913,10 +912,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; @@ -1806,10 +1805,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 /* @@ -2571,8 +2568,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 Thu Oct 2 07:43:52 2025 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 1EFA82D2394 for ; Fri, 19 Sep 2025 07:32:42 +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=1758267164; cv=none; b=IQHD3nRXnCTExOhZUCWw2GuLmUa/tVtfxmV7h1ICqMhPzVdodSQdu8sXUcsgqZO7IfIgOu4EjHRvsKsNQF0zwTpEf8SoeWsZSPqPQS+PbTrAjAsZQNcX4ICWg7dMk23XCGL3ECG4mmSLbfZHQDybQjQDzogNnh7v+ZcraoDbYe8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267164; c=relaxed/simple; bh=hj4zdUqw8mgxEXYwj41/vMIslhq1YXNkIg53sHKRob0=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=J97KSBTDjqQLlTs9WVTone1zJJY06ZlXpe1pCvREbX/7MO+kohBODkaLddgCrsmsJK77G3Ci56A8Mj3Hh+32ZL4Y02qTwuMWnWqEJ5zRIgM/6DqwUHcwAhXIWe2Sj2rBSBqqnw0DsCc2eT2276hkBcegH58dLgLOzW3Q4kNYL0Q= 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=COVu9jYc; 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="COVu9jYc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267162; 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=Qjy106GAJQLvSllQWGNFmfA8KKCXBAvtE6Ru60SIbkk=; b=COVu9jYcYh0f/nmO2VEGjryvmf2xDUrBqG5Nyo94h64Tvjee27tDUxvdvEovMx24X6GrGm N1VSSRlWFCSTuw1m4zg3n1O5uY5e91USURPY6mkEdH+tJ7bsNImXBlHfwHjpaiAA5G7o67 sZtGxsCTBLEMYluiqB7OScPH+XkztyU= 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-520-expodq3mNcCxoivna6fiPA-1; Fri, 19 Sep 2025 03:32:38 -0400 X-MC-Unique: expodq3mNcCxoivna6fiPA-1 X-Mimecast-MFC-AGG-ID: expodq3mNcCxoivna6fiPA_1758267157 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 93FB3195608B; Fri, 19 Sep 2025 07:32:37 +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 833CE1956045; Fri, 19 Sep 2025 07:32:34 +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 09/19] virtio_ring: use vring_virtqueue for enable_cb_delayed variants Date: Fri, 19 Sep 2025 15:31:44 +0800 Message-ID: <20250919073154.49278-10-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 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 cb013af446f9..4cf98767af0f 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -921,9 +921,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); @@ -937,7 +936,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 */ @@ -945,9 +944,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; @@ -1836,9 +1835,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 @@ -2634,8 +2632,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 Thu Oct 2 07:43:52 2025 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 B34EC2C08AA for ; Fri, 19 Sep 2025 07:32:48 +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=1758267170; cv=none; b=BA/AeDFU5a7zCn+sRA3qtFZivgykEqO9FvDgmmj+jt4ZOx5d9EybbrUOAx0fa1gHojRHgTqU2A3Fn+hQJOwBJNoWauXefEh8J0qkpNwmodil7rCoVKM5kvEhSGCGUUQBvM1ZiKlBZmddMLkiS62blqkggm5RMBq5A3ZAsq5Pkas= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267170; c=relaxed/simple; bh=xURi67NKtNA1MI+5jZHe1gEtppzi5PO0pGNSXeYkaOE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mTwZeyBg0Gk9xSTdWBb0zD35VgmB/00KRaYHDDpMtG56sYuAYmJwjChByRsxNvMurh3N6yFo/9dUcFBJwA4WCAurKNf0PyeUIgfnLqOf4yCqPbsRTQ1TRMGz1nhd/AuZXeh/crOQNxeVQPF7lliqg2SZ/N2F23DJuPTgSzyGGhs= 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=fZJX0rBL; 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="fZJX0rBL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267167; 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=dVkaHzMHBzkynVOCX8NJK5GF5hPwY2fqBjVJXaCUx5Q=; b=fZJX0rBL8jnXO/aee8j21yHbNDPrvE0TIg+FuxwgQHz2/ceG6VsTqREbcxmnHQ5RXmzbeQ hqPWKCdN+XIW13AJM68p3vloWRNSdPa80GJX0CcCUh+EdpjAqLmXBL/ZfV+OP7oHiwCwuZ o6xbnGpXLslyxejq+2PSKcg7NmVrqlc= 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-638-YbkzUEOOPEyQatOQNGKf0g-1; Fri, 19 Sep 2025 03:32:43 -0400 X-MC-Unique: YbkzUEOOPEyQatOQNGKf0g-1 X-Mimecast-MFC-AGG-ID: YbkzUEOOPEyQatOQNGKf0g_1758267162 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 C6CBF1800365; Fri, 19 Sep 2025 07:32:42 +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 975B51956048; Fri, 19 Sep 2025 07:32:37 +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 10/19] virtio_ring: switch to use vring_virtqueue for disable_cb variants Date: Fri, 19 Sep 2025 15:31:45 +0800 Message-ID: <20250919073154.49278-11-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 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 4cf98767af0f..7733d78247de 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -873,10 +873,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 @@ -892,7 +890,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); } } @@ -1785,10 +1783,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 @@ -2541,9 +2537,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 Thu Oct 2 07:43:52 2025 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 6142A2DEA62 for ; Fri, 19 Sep 2025 07:32:52 +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=1758267173; cv=none; b=HwRTIlT45yEQvnfNB02Dw4WQdXU6vELS2ufhTCgyEnI6t4zmHx60t4hO5Zgl/kI24jC6OjKxno3P4c1LN5K37ElKZ97DILFkI+i/dIAVN+gDcvLWHqkkF+msxBvQWWsJOqbmIiyFTwlAkqPkK8Arykx02Udhrm7O5O6tHMeH/Kc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267173; c=relaxed/simple; bh=TRWNPidh1tTaEooGt7NYlVqOM2fxkUYrswNbfu7XWrw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EKF420lMhHKBkEJdHOvzN+2MfkRW+HAG9wYddTZOWDvoIJDkOKjfNJ6gu3dP4/XyJqTcBWYPwlfdn1FlCqn0vDOjoQFZrNbBBefEsIEKw8oKGyRCDZ+pm3p2rlLKIuAIprnPLAXHCwtMCVLE4xgXcdOnO3KsW4CkRVALMPCwQKQ= 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=M2u8Od/E; 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="M2u8Od/E" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267171; 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=5DFQqMaSQ1YF3G4QWZU6Tl/pO92+5eLab9kIaDxET3I=; b=M2u8Od/EuKVaneJj4LiVNrpvbAB81NE2un4lLOp5l33TyzvxonUMBEnZzf7DoHq24l99UD G5Ra7Q0wplLLorhgptkUw1CB1blUsjgyrgheabDvVk+4Iotk+6hGlSYf5eIEsWkDJ5cmyW iFsBgetjUnbubSwbH/e/IhQ+vvBvx/U= 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-658-UfaKCSfMPVuO5Hi0-EK8IQ-1; Fri, 19 Sep 2025 03:32:47 -0400 X-MC-Unique: UfaKCSfMPVuO5Hi0-EK8IQ-1 X-Mimecast-MFC-AGG-ID: UfaKCSfMPVuO5Hi0-EK8IQ_1758267166 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 C556D180057B; Fri, 19 Sep 2025 07:32:46 +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 9ED421956048; Fri, 19 Sep 2025 07:32:43 +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 11/19] virtio_ring: switch to use vring_virtqueue for detach_unused_buf variants Date: Fri, 19 Sep 2025 15:31:46 +0800 Message-ID: <20250919073154.49278-12-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 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 7733d78247de..2290ee917c35 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -954,9 +954,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 @@ -969,7 +968,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; @@ -1891,9 +1890,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 @@ -2645,8 +2643,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 Thu Oct 2 07:43:52 2025 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 695562E040A for ; Fri, 19 Sep 2025 07:32:56 +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=1758267177; cv=none; b=eHK14RxbV8b75TBQ8GjW1I6n8x58aRfDUGz5XuQzOXURtXwgerj3Oam+vSBCqTjhTxoYvIiyeh5P+iyJZRzIwthOE5MEHD0gvb7CXer7pQPrncTySJJmsZgca1FZQUwDod17lHDUwwf4Z9YGkqiz1HomG7dtoSuWTmkQ5gDvwcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267177; c=relaxed/simple; bh=w1C0RSEHh1VzfvXKM+zrRAOIr93whV01vaguRwMBpio=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Ug8iFlyUNld8qOOeOxHhl6a7yhwWMh4d+4bBaiDVFNt5pHNHLCATwFKgJAWr82crvRqV5wncUKGqv/EoomGZKLtT2mGuZ9OvCucy/iiWoeRUBRmmyvP0XQO4mx6H1WaQs97ypHj+B0kT7uMvZnYzDnuXavcfpRPPn6Yr9z6/vTs= 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=S+quF2w4; 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="S+quF2w4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267175; 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=/wBklSwaQ2zaTA0CULKLUrmSkAgxtpiImNu2NlSDBBY=; b=S+quF2w4C1+bp/p97o7pCgOudSGD8o9zDRmqGfhDn9JkQAb+WvU/XuDkBixlNMgbobPf1L s6BpbmBpao9lyK+zI4eeTQSdfjJbKiEeBU/sFoDfm77s27GbjjhL0Ozv1+voG3m7ZgJkoL 0OSCsnKgPUMvXrB7apxEh5PJricTwtU= 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-622-WxoiZlbhNdm1gJ_pPX2_Vg-1; Fri, 19 Sep 2025 03:32:52 -0400 X-MC-Unique: WxoiZlbhNdm1gJ_pPX2_Vg-1 X-Mimecast-MFC-AGG-ID: WxoiZlbhNdm1gJ_pPX2_Vg_1758267171 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 EA36E1800290; Fri, 19 Sep 2025 07:32:50 +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 CC35E19560BB; Fri, 19 Sep 2025 07:32:47 +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 12/19] virtio_ring: use u16 for last_used_idx in virtqueue_poll_split() Date: Fri, 19 Sep 2025 15:31:47 +0800 Message-ID: <20250919073154.49278-13-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 Use u16 for last_used_idx in virtqueue_poll_split() to align with the spec. Acked-by: Eugenio P=C3=A9rez Reviewed-by: Xuan Zhuo Signed-off-by: Jason Wang --- drivers/virtio/virtio_ring.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 2290ee917c35..b1b6af297061 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -805,7 +805,7 @@ static void detach_buf_split(struct vring_virtqueue *vq= , unsigned int head, } =20 static bool virtqueue_poll_split(const struct vring_virtqueue *vq, - unsigned int last_used_idx) + u16 last_used_idx) { return (u16)last_used_idx !=3D virtio16_to_cpu(vq->vq.vdev, vq->split.vring.used->idx); --=20 2.31.1 From nobody Thu Oct 2 07:43:52 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 C67592E8B8F for ; Fri, 19 Sep 2025 07:32:59 +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=1758267181; cv=none; b=oYkXenH+m2slXNo8nsO5kLIySWRatYzAsXNuSEDPDm51e+EQqO6ugH2wGKsrj+4yEmJbNzGRuqj0kWjWWLlCzGDcpwOIJFFMs2mdlmxHGttHhfYIamzkt1kAUtLvU9GUtk/rjesqnOBespI7v6yralRovr5dMD4F04udh0bIwtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267181; c=relaxed/simple; bh=P10u1Gp6Bu3m9/69HGk1e7DP53JaAnZyb8ucWdbDFcc=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=nGEqHuTKGdPIpMBTZI+T0s6wGGzNBAka6rTLwUcqBF6fHV+Ea5Mj0ndYTHrMj+tme76GF3UJAdhTWz8JBHa5Emz3yAYQARa8+fdfmGzGm0mdzs/RxKNSNon1SILazQ3Pd6svPmpCLgwuooGzTNOj5TV4xYqCvq8cDfecw9uqk+g= 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=RlaXrZkG; 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="RlaXrZkG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267179; 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=2CQJlcMvjDfjW+tZJXmP6WCfdyVi5wtraXyI2mzKwig=; b=RlaXrZkGyQ/VmHC3Hw2Hsi1xNnNVevNd400MggaF+TvgsFdyJ5Y6TGafVQr+DewA5eWJT9 KIbpx6N3jnI+I14Zd13Gtg8Apbk7a5l4dGNNdjNB//7s9r3hwOcNAU00blNd1kguLBiBwj +Dv26xS0CS2MyXw8XiORr4/HXQgnJAc= 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-385-9yUCwJbyMGW6WKA9NTh6OQ-1; Fri, 19 Sep 2025 03:32:55 -0400 X-MC-Unique: 9yUCwJbyMGW6WKA9NTh6OQ-1 X-Mimecast-MFC-AGG-ID: 9yUCwJbyMGW6WKA9NTh6OQ_1758267174 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-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B64EB180034C; Fri, 19 Sep 2025 07:32:54 +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 AB8CD19560BB; Fri, 19 Sep 2025 07:32:51 +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 13/19] virtio_ring: introduce virtqueue ops Date: Fri, 19 Sep 2025 15:31:48 +0800 Message-ID: <20250919073154.49278-14-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 This patch introduces virtqueue ops which is a set of the 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 | 173 ++++++++++++++++++++++++++--------- 1 file changed, 130 insertions(+), 43 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index b1b6af297061..1045c553ee65 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -67,6 +67,12 @@ #define LAST_ADD_TIME_INVALID(vq) #endif =20 +enum vq_layout { + SPLIT =3D 0, + PACKED, + VQ_TYPE_MAX, +}; + struct vring_desc_state_split { void *data; /* Data for callback. */ =20 @@ -159,12 +165,28 @@ 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, u16 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 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; @@ -1121,6 +1151,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, @@ -1138,7 +1170,7 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, if (!vq) return NULL; =20 - vq->packed_ring =3D false; + vq->layout =3D SPLIT; vq->vq.callback =3D callback; vq->vq.vdev =3D vdev; vq->vq.name =3D name; @@ -2075,6 +2107,8 @@ static void virtqueue_reset_packed(struct vring_virtq= ueue *vq) 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, @@ -2105,7 +2139,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 PACKED; vq->map =3D map; vq->use_map_api =3D vring_use_map_api(vdev); =20 @@ -2193,6 +2227,39 @@ 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 const struct virtqueue_ops *const all_ops[VQ_TYPE_MAX] =3D { + [SPLIT] =3D &split_ops, + [PACKED] =3D &packed_ops +}; + static int virtqueue_disable_and_recycle(struct virtqueue *_vq, void (*recycle)(struct virtqueue *vq, void *buf)) { @@ -2235,6 +2302,39 @@ static int virtqueue_enable_after_reset(struct virtq= ueue *_vq) * Generic functions and exported symbols. */ =20 +#define VIRTQUEUE_CALL(vq, op, ...) \ + ({ \ + typeof(all_ops[SPLIT]->op(vq, ##__VA_ARGS__)) ret; \ + \ + switch (vq->layout) { \ + case SPLIT: \ + ret =3D all_ops[SPLIT]->op(vq, ##__VA_ARGS__); \ + break; \ + case PACKED: \ + ret =3D all_ops[PACKED]->op(vq, ##__VA_ARGS__); \ + break; \ + default: \ + BUG(); \ + break; \ + } \ + ret; \ +}) + +#define VOID_VIRTQUEUE_CALL(vq, op, ...) \ + ({ \ + switch ((vq)->layout) { \ + case SPLIT: \ + all_ops[SPLIT]->op(vq, ##__VA_ARGS__); \ + break; \ + case PACKED: \ + all_ops[PACKED]->op(vq, ##__VA_ARGS__); \ + break; \ + default: \ + BUG(); \ + break; \ + } \ +}) + static inline int virtqueue_add(struct virtqueue *_vq, struct scatterlist *sgs[], unsigned int total_sg, @@ -2247,10 +2347,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 /** @@ -2440,8 +2539,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 @@ -2511,8 +2609,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 @@ -2534,10 +2631,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 @@ -2560,8 +2654,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 @@ -2582,8 +2675,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 @@ -2626,8 +2719,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 @@ -2643,14 +2735,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 /** @@ -2780,7 +2871,8 @@ 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_is_packed(vq) ? vq->packed.vring.num : + vq->split.vring.num) =3D=3D num) return 0; =20 err =3D virtqueue_disable_and_recycle(_vq, recycle); @@ -2789,10 +2881,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) @@ -2830,10 +2919,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); } @@ -2876,7 +2962,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, @@ -2905,7 +2991,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); } @@ -2930,7 +3016,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 << @@ -2983,7 +3069,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 @@ -3066,7 +3153,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; @@ -3079,7 +3166,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 + @@ -3093,7 +3180,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 Thu Oct 2 07:43:52 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 From nobody Thu Oct 2 07:43:52 2025 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 304812F1FCC for ; Fri, 19 Sep 2025 07:33:07 +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=1758267189; cv=none; b=DU5H2C6JXHkUgYx599dZxkPSXSkv6kdFHPL9ZZ4RbAmunleePJpnKydnedoMQVpIVwbXHPH7GZvT4TOKDWtHGG3l99ThMOjnuBVAq1d6lQ4R/VF4yjHHdQU/GsSHTTBgtjmtBCFuKwPZvGAD/VWR3sbdlJXA5gG2f1AfBXnUa04= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267189; c=relaxed/simple; bh=JVKitmswlq4ZK/WfT8XMVZwz/iP0I5UvB5KjGP8wgvE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=mVStE+Fs6Ou25wnRk+qwpOX2t0eQdqrkVYhZyL6qUNo89d/p2IYZhDGe9y4tcwhrtB2HsQkHC0m6Zx3ktRRD/WOiS95ENNDH0DspOLMZR6yklKVY1hntJvvic60Ape4Da+j0bftXqQEW0+cYEM6RH8rZBhDgDliO1tSwW4nafdI= 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=EibhF3Id; 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="EibhF3Id" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267187; 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=A9YZjEGI6Oy8XJp9Y3nYR0EGZPGfX0eXgaSvK8i8cBc=; b=EibhF3IdsJ9ak5mu8S3ayzxpg+VSAEcouefrePSJG8gY8iwvkX8312JXTS5WoB2Z683iNI Dsv1FUGIohsXLBa3oquCR7+sFxJwKltHhVVl663vK+Yk08ImrMRrFbuiLSSNw95jXZZor2 rrpOB18LBTkP8nNtMHOOQUA/kqrF+OA= Received: from mx-prod-mc-02.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-392-FfOlTCitN3Sw1oghdoK_Fg-1; Fri, 19 Sep 2025 03:33:03 -0400 X-MC-Unique: FfOlTCitN3Sw1oghdoK_Fg-1 X-Mimecast-MFC-AGG-ID: FfOlTCitN3Sw1oghdoK_Fg_1758267182 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9D50D195608D; Fri, 19 Sep 2025 07:33:02 +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 3ED7619560BB; Fri, 19 Sep 2025 07:32:58 +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 15/19] virtio_ring: factor out core logic of buffer detaching Date: Fri, 19 Sep 2025 15:31:50 +0800 Message-ID: <20250919073154.49278-16-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 Factor out core logic of buffer detaching and leave the id population 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 0949675a3d12..a83d08070e8a 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1662,8 +1662,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; @@ -1674,8 +1674,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)) { @@ -1712,6 +1710,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; + + return 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 Thu Oct 2 07:43:52 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 2CCCE2F2617 for ; Fri, 19 Sep 2025 07:33:09 +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=1758267191; cv=none; b=XluFFMk8dTwEcH9eW7Gw9VMu7DnvYMRZvDgXwgT9K0kIy4bUoWTxTMsIlZ0qqL4PYOc6bce1T+SmT0EGnG4jz/ac2c20n9j6fJhK0iMCugOD7sADi8C+BPA4GG+9ptSxn20OjBZxLkMTzqwRP7adlNMSvrvsH+fJmbQFsR3Q0+k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267191; c=relaxed/simple; bh=FqYnqYtU8O+wC82H2JjbzckwHeqMY/a4PgAq00ynxJM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PQpMgzcMph+C55alAgomcpqQ6nGtr4o3h+f2Wv+hgnU+JlvQ2zndGK03Xd8Go6dWL/BGBgsb00bTmYG5nPmKzNJewymS1oyrNRdKJL+RuU3exkoyWh0bAUnX3CvMnR3HpFbQHJ0KVbLeElZ8Fz+UzrYN3RoOaT0Wd+OuzBOSZK8= 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=TYemelR1; 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="TYemelR1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267189; 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=1Ptrr4BTPt5ya8hfXz4kLOZPYwSv02w1Hgkd7CHw9yg=; b=TYemelR1stTM7UkEo5xcaacdGaZ2lWa4k481BhvWpUGwnNJt6Q8RJnf8t3JsMjiQTjUPAq qT65B1AURFArb7HgOu7U/HHZzbtEunnQ3xf36j0uEd7JqSOvgimarJl+Qri4B/VomTlJMj 747+IfmDY3G6JlPwB4JMl+sP1Zn1Ljk= Received: from mx-prod-mc-02.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-483-19uLT56POC-QNCDjjc2Wpg-1; Fri, 19 Sep 2025 03:33:07 -0400 X-MC-Unique: 19uLT56POC-QNCDjjc2Wpg-1 X-Mimecast-MFC-AGG-ID: 19uLT56POC-QNCDjjc2Wpg_1758267186 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CB4BA1956086; Fri, 19 Sep 2025 07:33:06 +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 9C68E1956045; Fri, 19 Sep 2025 07:33:03 +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 16/19] virtio_ring: factor out core logic for updating last_used_idx Date: Fri, 19 Sep 2025 15:31:51 +0800 Message-ID: <20250919073154.49278-17-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 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 a83d08070e8a..f376f717c8e4 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -1750,6 +1750,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) @@ -1793,24 +1817,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 Thu Oct 2 07:43:52 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 B3EB12F261F for ; Fri, 19 Sep 2025 07:33:16 +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=1758267199; cv=none; b=F7uazTvlyEXNHVdmY3shvZsE/gj1cDzdcxY/JvK2X3Pv05NZq3BVa4LER6prnf0EkQ54UP6f4Kg1JLOD0jemEhKiul+ighYmyDiJ8jHxy5GEGnwQ/lYp7Rbr/e3vYRNbqZf0mvi31W12yp2PadMFb7zdotjN/GPQSUjk33kfs1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267199; c=relaxed/simple; bh=r82W1lKVa/2er0+TG3wmw0sL9wy4EaTIDRdRRomCyQM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RFWv802btddg+e/YuYLdVZKsHAAOElxNIitPA4yZ7xDnHc/rjo5Q3BdTURx3eAIpmZ4CHJhVhPLVF2MHTCatpMLbB81VzdMbWWt9llM3lFkLd6BXBa+t4AxX8K2jddD6E4JOgv8CztlcE+Nac5yO8v+iCgj9paot/MVWd8NKBGA= 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=dK3hY+iD; 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="dK3hY+iD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267195; 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=+T23W7DIKB49P1edCgTfToWIbfhHLg1Pf5xyIauhPFg=; b=dK3hY+iDxu2WMleJ2HT+8u2wkiL9dqzTJUCYObwicXje3QG9Wwg17yOid4IEGbi+xYG2Dy neQ9eHPrbWwa5vdLDpm+I2faZD2GiLWYjZuEMGAWAgMY6QkvPUo7+09sAFkoD0MNLPahhS /wxaAv3kD7HXgqITVJ0sIwFtElpxW2M= 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-47-YhlN9d2UOb2dfRjJ4MgT6Q-1; Fri, 19 Sep 2025 03:33:11 -0400 X-MC-Unique: YhlN9d2UOb2dfRjJ4MgT6Q-1 X-Mimecast-MFC-AGG-ID: YhlN9d2UOb2dfRjJ4MgT6Q_1758267190 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9E23919560B2; Fri, 19 Sep 2025 07:33:10 +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 8C1C219560BB; Fri, 19 Sep 2025 07:33:07 +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 17/19] virtio_ring: factor out split indirect detaching logic Date: Fri, 19 Sep 2025 15:31:52 +0800 Message-ID: <20250919073154.49278-18-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 Factor out the split indirect descriptor detaching logic in order to make it 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 | 63 ++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index f376f717c8e4..5aa0cd785362 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -771,11 +771,42 @@ 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. */ @@ -799,34 +830,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 Thu Oct 2 07:43:52 2025 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 487FC2F5A1B for ; Fri, 19 Sep 2025 07:33:21 +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=1758267203; cv=none; b=PLtDT18hUt41bm9oNCQBX44+XDA5sPL+L+hVALSUQYgz/JXCf1WxJXf0jc7T3mitYSpwUu8HSpdY/Tx2x1Q28wIxZdxXVvoO7CN+6J5ObXNozl97YpuQEmkd1wX0cATUPy0YcFBZuCUnQVhQEUWROnaaJsvZVolJwOo+o0n1n6I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267203; c=relaxed/simple; bh=3MKcuH54idOyqRtbKjhs5zfWkgHNYzLchqcZmZg9NjE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=P7awEPOhGfq8+CnC0AJhLA4T3vrWmzGRS7VZdhO7yngLXH8XGNk06BwBAEqrVHSvIEgMLukmQPOmDPBistE9xxFPM4WqodgS9uGEAki7konR2hs6nxcwWgM4NKYQD/jBKS8wQYCML/+qVBda/TDPeaC8rJz8QBYPe8Tsue48SB4= 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=Ty/oyFRj; 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="Ty/oyFRj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267201; 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=r4ZsZyqXT2g6mrbpOGIJ7RWG7WDvS9RJS8jyzULj2Co=; b=Ty/oyFRjK+PkEpkZ9pxwlw1hM8Q944wMAuu7BoRpmdghG1SuD0Fym7Skyyxw9xna2FIQDQ o0hFTJ7x4HpV5vN9o6N7PLvGSepLO38nFaFESTx0VdxCgINZz4NRBiRrUE2vQsyZqqi65Y bdmGWRDNOuQJ49fUz74azzWEOot+mR4= Received: from mx-prod-mc-02.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-352-NBvYgI_rN6qIqxnzKvDvMg-1; Fri, 19 Sep 2025 03:33:15 -0400 X-MC-Unique: NBvYgI_rN6qIqxnzKvDvMg-1 X-Mimecast-MFC-AGG-ID: NBvYgI_rN6qIqxnzKvDvMg_1758267194 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7AA971956087; Fri, 19 Sep 2025 07:33:14 +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 6775519560BB; Fri, 19 Sep 2025 07:33:11 +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 18/19] virtio_ring: factor out split detaching logic Date: Fri, 19 Sep 2025 15:31:53 +0800 Message-ID: <20250919073154.49278-19-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 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 5aa0cd785362..b700aa3e56c3 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -802,8 +802,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; @@ -824,8 +825,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++; @@ -834,6 +833,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 Thu Oct 2 07:43:52 2025 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 488812F60A3 for ; Fri, 19 Sep 2025 07:33:22 +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=1758267204; cv=none; b=E7HVWUIhKrdP4joM6T1cToGQGGCQu5qC1VQH1H6nuLbsiUOZXFMQ+mcnAXHM6tNMaCnnd8K0dGbHqcmN2paZ1K39QUOEVjnlKH6v2WkN3QoEkWuzuTC995JQ996u/Ri0fKn1mxAKDHPqJdBh5rGTeTGox7yscajU/K7MfF07kDs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758267204; c=relaxed/simple; bh=aiF0RFrqNch3/rNd9XhvMKdSHGaglcKX0bdYQATdMJM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b3uPwo799mVamU5/O65+pk8bELTcAyzPeexPNhMi1TxY6G2c0B1+xULtgY2G8xgpl/tiJXYr48I9SQ3ztsybMqgh3lrbOVa5dPDd2ajteeDjhq7+GAeH8jTB9umMRwC5PYAb44LOt1YvSEVf08hxpytelHlJ7uCoGbJx0Re+PJY= 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=gvefxkLq; 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="gvefxkLq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1758267201; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+vDSpgvXb7NkLtASpYpa20zBO60pXmLHbL7RfCTbgmI=; b=gvefxkLqCKLXUPEc2PeiaSt8o6aYs+gQ+REZh/F7TEJ5tKe6H74I7Y+tGexRRwD+Qu4Qhw Y/+eCnSs+0nhbCjHkiKtysaK+ux2Ie/foRKhKxVNc1xa8K0vJGAHbNU2FfT+p5TG+aOYku nKdfhtKWtX9CnI6L9T9a0d9/AXtceJo= 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-224-1hJz2z5tNaGoMKtrh-zXOw-1; Fri, 19 Sep 2025 03:33:19 -0400 X-MC-Unique: 1hJz2z5tNaGoMKtrh-zXOw-1 X-Mimecast-MFC-AGG-ID: 1hJz2z5tNaGoMKtrh-zXOw_1758267198 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 59E8219560B0; Fri, 19 Sep 2025 07:33:18 +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 3CCAF19560BB; Fri, 19 Sep 2025 07:33:14 +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 19/19] virtio_ring: add in order support Date: Fri, 19 Sep 2025 15:31:54 +0800 Message-ID: <20250919073154.49278-20-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" This patch implements in order support for both split virtqueue and packed virtqueue. Perfomance 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: 5.20Mpps | 6.20Mpps | +19% 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. Signed-off-by: Jason Wang Signed-off-by: Michael S. Tsirkin --- drivers/virtio/virtio_ring.c | 421 +++++++++++++++++++++++++++++++++-- 1 file changed, 401 insertions(+), 20 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index b700aa3e56c3..c00b5e57f2fc 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -70,6 +70,8 @@ enum vq_layout { SPLIT =3D 0, PACKED, + SPLIT_IN_ORDER, + PACKED_IN_ORDER, VQ_TYPE_MAX, }; =20 @@ -80,6 +82,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_len; /* Buffer Length */ }; =20 struct vring_desc_state_packed { @@ -91,6 +94,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_len; /* Buffer Length */ }; =20 struct vring_desc_extra { @@ -206,6 +210,17 @@ struct vring_virtqueue { =20 /* Head of free buffer list. */ unsigned int free_head; + + /* + * With IN_ORDER, devices write a single used ring entry with + * the id corresponding to the head entry of the descriptor chain + * describing the last buffer in the batch + */ + struct used_entry { + u32 id; + u32 len; + } batch_last; + /* Number we've added since last sync. */ unsigned int num_added; =20 @@ -258,7 +273,12 @@ static void vring_free(struct virtqueue *_vq); =20 static inline bool virtqueue_is_packed(const struct vring_virtqueue *vq) { - return vq->layout =3D=3D PACKED; + return vq->layout =3D=3D PACKED || vq->layout =3D=3D PACKED_IN_ORDER; +} + +static inline bool virtqueue_is_in_order(const struct vring_virtqueue *vq) +{ + return vq->layout =3D=3D SPLIT_IN_ORDER || vq->layout =3D=3D PACKED_IN_OR= DER; } =20 static bool virtqueue_use_indirect(const struct vring_virtqueue *vq, @@ -575,6 +595,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, c =3D 0; + /* Total length for in-order */ + unsigned int total_len =3D 0; int head; bool indirect; =20 @@ -646,6 +668,7 @@ static inline int virtqueue_add_split(struct vring_virt= queue *vq, ++c =3D=3D total_sg ? 0 : VRING_DESC_F_NEXT, premapped); + total_len +=3D len; } } for (; n < (out_sgs + in_sgs); n++) { @@ -663,6 +686,7 @@ static inline int virtqueue_add_split(struct vring_virt= queue *vq, i, addr, len, (++c =3D=3D total_sg ? 0 : VRING_DESC_F_NEXT) | VRING_DESC_F_WRITE, premapped); + total_len +=3D len; } } =20 @@ -685,7 +709,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_len =3D total_len;; + } else if (indirect) vq->free_head =3D vq->split.desc_extra[head].next; else vq->free_head =3D i; @@ -858,6 +887,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 vq->packed.vring.num) + 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) @@ -915,6 +952,73 @@ 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; + u16 last_used; + + START_USE(vq); + + if (unlikely(vq->broken)) { + END_USE(vq); + return NULL; + } + + last_used =3D (vq->last_used_idx & (vq->split.vring.num - 1)); + + if (vq->batch_last.id =3D=3D num) { + if (!more_used_split(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].id); + vq->batch_last.len =3D virtio32_to_cpu(vq->vq.vdev, + vq->split.vring.used->ring[last_used].len); + } + + if (vq->batch_last.id =3D=3D last_used) { + vq->batch_last.id =3D num; + *len =3D vq->batch_last.len; + } else + *len =3D vq->split.desc_state[last_used].total_len; + + if (unlikely(last_used >=3D num)) { + BAD_RING(vq, "id %u out of range\n", last_used); + return NULL; + } + 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++; + /* 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)) { @@ -1008,7 +1112,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); @@ -1071,6 +1178,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 vq->split.vring.num; } =20 static int vring_alloc_state_extra_split(struct vring_virtqueue_split *vri= ng_split) @@ -1182,7 +1290,6 @@ static struct virtqueue *__vring_new_virtqueue_split(= unsigned int index, if (!vq) return NULL; =20 - vq->layout =3D SPLIT; vq->vq.callback =3D callback; vq->vq.vdev =3D vdev; vq->vq.name =3D name; @@ -1202,6 +1309,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) ? + SPLIT_IN_ORDER : SPLIT; =20 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) vq->weak_barriers =3D false; @@ -1359,13 +1468,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_len =3D 0; + u16 head; dma_addr_t addr; =20 head =3D vq->packed.next_avail_idx; @@ -1383,8 +1493,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)) { @@ -1404,6 +1512,7 @@ static int virtqueue_add_indirect_packed(struct vring= _virtqueue *vq, extra[i].flags =3D n < out_sgs ? 0 : VRING_DESC_F_WRITE; } =20 + total_len +=3D len; i++; } } @@ -1457,6 +1566,7 @@ static int virtqueue_add_indirect_packed(struct vring= _virtqueue *vq, 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_len =3D total_len; =20 vq->num_added +=3D 1; =20 @@ -1509,8 +1619,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; @@ -1631,6 +1744,152 @@ 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, c, err_idx, total_len =3D 0; + __le16 head_flags, flags; + u16 head, avail_used_flags; + 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; + + 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; + } + + c =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; + + 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 | + (++c =3D=3D total_sg ? 0 : VRING_DESC_F_NEXT) | + (n < out_sgs ? 0 : VRING_DESC_F_WRITE)); + 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; + } + + total_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_len =3D total_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; + + 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; @@ -1791,10 +2050,81 @@ 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 vq->packed.vring.num) + 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 num) { + if (!more_used_packed(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 num; + *len =3D vq->batch_last.len; + } else + *len =3D vq->packed.desc_state[last_used].total_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; @@ -1821,7 +2151,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; } @@ -1962,7 +2292,7 @@ static void *virtqueue_detach_unused_buf_packed(struc= t 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); + detach_buf_packed_in_order(vq, i, NULL); END_USE(vq); return buf; } @@ -1988,6 +2318,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 @@ -2119,8 +2451,12 @@ static void virtqueue_vring_attach_packed(struct vri= ng_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 vq->split.vring.num; + else { + /* Put everything in free lists. */ + vq->free_head =3D 0; + } } =20 static void virtqueue_reset_packed(struct vring_virtqueue *vq) @@ -2167,13 +2503,14 @@ static struct virtqueue *__vring_new_virtqueue_pack= ed(unsigned int index, #else vq->broken =3D false; #endif - vq->layout =3D 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) ? + PACKED_IN_ORDER : PACKED; =20 if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) vq->weak_barriers =3D false; @@ -2283,9 +2620,39 @@ 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 const struct virtqueue_ops *const all_ops[VQ_TYPE_MAX] =3D { [SPLIT] =3D &split_ops, - [PACKED] =3D &packed_ops + [PACKED] =3D &packed_ops, + [SPLIT_IN_ORDER] =3D &split_in_order_ops, + [PACKED_IN_ORDER] =3D &packed_in_order_ops, }; =20 static int virtqueue_disable_and_recycle(struct virtqueue *_vq, @@ -2341,6 +2708,12 @@ static int virtqueue_enable_after_reset(struct virtq= ueue *_vq) case PACKED: \ ret =3D all_ops[PACKED]->op(vq, ##__VA_ARGS__); \ break; \ + case SPLIT_IN_ORDER: \ + ret =3D all_ops[SPLIT_IN_ORDER]->op(vq, ##__VA_ARGS__); \ + break; \ + case PACKED_IN_ORDER: \ + ret =3D all_ops[PACKED_IN_ORDER]->op(vq, ##__VA_ARGS__); \ + break; \ default: \ BUG(); \ break; \ @@ -2357,10 +2730,16 @@ static int virtqueue_enable_after_reset(struct virt= queue *_vq) case PACKED: \ all_ops[PACKED]->op(vq, ##__VA_ARGS__); \ break; \ - default: \ - BUG(); \ - break; \ - } \ + case SPLIT_IN_ORDER: \ + all_ops[SPLIT_IN_ORDER]->op(vq, ##__VA_ARGS__); \ + break; \ + case PACKED_IN_ORDER: \ + all_ops[PACKED_IN_ORDER]->op(vq, ##__VA_ARGS__); \ + break; \ + default: \ + BUG(); \ + break; \ + } \ }) =20 static inline int virtqueue_add(struct virtqueue *_vq, @@ -3077,6 +3456,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