From nobody Thu Nov 28 02:40:46 2024 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76FF676036 for ; Sat, 5 Oct 2024 07:42:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728114129; cv=none; b=ZKYo4YfF4ThO4XdY2KEoa+2jChF766H1pIuboaL0e2fJNBb8UZg8pp7U+mBx6lte0OnKNqwgbQ8SeYneSo2YjHUufgDvin2FG9Y8whykrzC0smcvQOBQhKEbMFdC1X93RM0+10f+iTeVsMffnZuWVOcvrhZd/nKXu7dqUKK/SCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728114129; c=relaxed/simple; bh=4o2ZBouF/RIEL8PVlxLCIYfr3i1YsvyNkXY0A9TrhDo=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=FdlU4lR12rVnEgW7fetaCwYll8HGFFWiK7EpPZ3DgURMa63gmfPgeAPjixGoHdalxF+SjZMZrMezWVs9UvBZSHBSVY16J7iqVU043BkaL7tZ0qe79WndKTBaSkfR5QqweKQOQoF8wJInEOLVGUbb8AqV6uPmgvfAQ8xY2h+gkCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jUtppEGf; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jUtppEGf" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7ae3d7222d4so2395822a12.3 for ; Sat, 05 Oct 2024 00:42:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728114127; x=1728718927; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=lOFEUyKULg3jg189V7VdXZrfZtPhx0+n63wz8tInHPg=; b=jUtppEGf9VMZi5jrALrhQ9RVeOhJAhEOt40gOh3+Mh3MYL+wgdNOPVfLZRnrOLkgmq jo40LYSHJ5wZm52cNvf3Csjf9MDV/T6dJrt1PWkCEpYMzlvUcYBPIQvz/ndlyfNNwByA hUxQ8BDN+johlc2cCkstylFEQ43F/u3I8Qg6/CVUcuCqSbQrBEH3T1LSLx8WAikG8/Jv ipZpDWRRJHe5ppE9Q9mHS3i3eyv/LTlqiISB4/aPNC7CeRBHHaACKJ/If43RJR7mBRlv cCMPPM3c96LrK724WVzeEsENu4Sn9+qMXQ9tJSmxzYR+Hz2Nm4WizkiE+q4mfDMRv37H B9VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728114127; x=1728718927; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lOFEUyKULg3jg189V7VdXZrfZtPhx0+n63wz8tInHPg=; b=u33MBKIwgkgUvK+Shx0wYLaKspkQQ0d8GJgekHF1WeX/JQzt75n8/KoUhhtqMLSxjt c+iZx2q53HkwhEHTs3Vjx+eSLGUO0M5+Uq4pmPUq37iK5Lmi1fgarYWowEd6xuWgU/7e QUge6tOu/pso5SuQjqNMmBm8Jfdq8fsWDcyAMZF5C6u6J3MZX+QJg6j4Ka8Tb8uIdgu8 aaZCIzQ5nSOx607rE4IjdStBY+xH7FqYh4TZS7MvRszQBj9Ye7/e7xs2ta8ex14s14Hi 7CXJ3m7ZrFTSXJf/VBIbR6m0CRBucUn2HRywGjPLucMQrstN09sFw2o/mjhMY3E0VEeW Y+6g== X-Forwarded-Encrypted: i=1; AJvYcCV1nKX5vo5mefra2ae3Onpq2JqgHMj3juvgxCMGjPftJ0DzSVa6JXPK7Y90/yWFdlWKwZCUoK5cHW3tGKo=@vger.kernel.org X-Gm-Message-State: AOJu0Ywo0RsW/tzw4jnJJohnyWFouuWeTxmUnO1+xAv0CwtjYCYsIsUY uZ8HpiB/jm9a0akGtj1YWsCQ+lMD0rTza1x3S56q6lgkWqCCjgfY X-Google-Smtp-Source: AGHT+IGEHwuzQqCSQvYTkfhD03hBps5slTUoC0Afed/i2Wdmjp/eCFJaabgxuJJkMzy54qqGQqUjNg== X-Received: by 2002:a05:6a21:3983:b0:1d6:d0d3:c520 with SMTP id adf61e73a8af0-1d6dfa24bb7mr8661281637.9.1728114126841; Sat, 05 Oct 2024 00:42:06 -0700 (PDT) Received: from localhost.localdomain ([222.78.61.133]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-71df0cd14f8sm983179b3a.65.2024.10.05.00.42.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 05 Oct 2024 00:42:06 -0700 (PDT) From: huangwenyu1998@gmail.com To: mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, huangwenyu1998@gmail.com Subject: [PATCH v2] virtio: Make vring_new_virtqueue support for packed vring Date: Sat, 5 Oct 2024 15:42:01 +0800 Message-Id: <20241005074201.52727-1-huangwenyu1998@gmail.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) 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 Content-Type: text/plain; charset="utf-8" From: Wenyu Huang It used for testing in tools/virtio/vringh_test.c. If vring_new_virtqueue supports packed vring, we can add support for packed vring to vringh and test it. Signed-off-by: Wenyu Huang --- Changes in v2: Make the commit title and description more clearer. drivers/virtio/virtio_ring.c | 153 +++++++++++++++++++++-------------- 1 file changed, 92 insertions(+), 61 deletions(-) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index be7309b1e860..664a0c40ee05 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -223,7 +223,7 @@ struct vring_virtqueue { #endif }; =20 -static struct virtqueue *__vring_new_virtqueue(unsigned int index, +static struct virtqueue *__vring_new_virtqueue_split(unsigned int index, struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, bool weak_barriers, @@ -232,6 +232,15 @@ static struct virtqueue *__vring_new_virtqueue(unsigne= d int index, void (*callback)(struct virtqueue *), const char *name, struct device *dma_dev); +static struct virtqueue *__vring_new_virtqueue_packed(unsigned int index, + struct vring_virtqueue_packed *vring_packed, + struct virtio_device *vdev, + bool weak_barriers, + bool context, + bool (*notify)(struct virtqueue *), + void (*callback)(struct virtqueue *), + const char *name, + struct device *dma_dev); static struct vring_desc_extra *vring_alloc_desc_extra(unsigned int num); static void vring_free(struct virtqueue *_vq); =20 @@ -1160,7 +1169,7 @@ static struct virtqueue *vring_create_virtqueue_split( if (err) return NULL; =20 - vq =3D __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers, + vq =3D __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barrie= rs, context, notify, callback, name, dma_dev); if (!vq) { vring_free_split(&vring_split, vdev, dma_dev); @@ -2064,62 +2073,21 @@ static struct virtqueue *vring_create_virtqueue_pac= ked( struct device *dma_dev) { struct vring_virtqueue_packed vring_packed =3D {}; - struct vring_virtqueue *vq; - int err; + struct virtqueue *vq; =20 if (vring_alloc_queue_packed(&vring_packed, vdev, num, dma_dev)) - goto err_ring; - - vq =3D kmalloc(sizeof(*vq), GFP_KERNEL); - if (!vq) - goto err_vq; - - vq->vq.callback =3D callback; - vq->vq.vdev =3D vdev; - vq->vq.name =3D name; - vq->vq.index =3D index; - vq->vq.reset =3D false; - vq->we_own_ring =3D true; - vq->notify =3D notify; - vq->weak_barriers =3D weak_barriers; -#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION - vq->broken =3D true; -#else - vq->broken =3D false; -#endif - vq->packed_ring =3D true; - vq->dma_dev =3D dma_dev; - vq->use_dma_api =3D vring_use_dma_api(vdev); - vq->premapped =3D false; - vq->do_unmap =3D vq->use_dma_api; - - 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); - - if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) - vq->weak_barriers =3D false; - - err =3D vring_alloc_state_extra_packed(&vring_packed); - if (err) - goto err_state_extra; - - virtqueue_vring_init_packed(&vring_packed, !!callback); + return NULL; =20 - virtqueue_init(vq, num); - virtqueue_vring_attach_packed(vq, &vring_packed); + vq =3D __vring_new_virtqueue_packed(index, &vring_packed, vdev, weak_barr= iers, + context, notify, callback, name, dma_dev); + if (!vq) { + vring_free_packed(&vring_packed, vdev, dma_dev); + return NULL; + } =20 - spin_lock(&vdev->vqs_list_lock); - list_add_tail(&vq->vq.list, &vdev->vqs); - spin_unlock(&vdev->vqs_list_lock); - return &vq->vq; + to_vvq(vq)->we_own_ring =3D true; =20 -err_state_extra: - kfree(vq); -err_vq: - vring_free_packed(&vring_packed, vdev, dma_dev); -err_ring: - return NULL; + return vq; } =20 static int virtqueue_resize_packed(struct virtqueue *_vq, u32 num) @@ -2599,7 +2567,7 @@ irqreturn_t vring_interrupt(int irq, void *_vq) EXPORT_SYMBOL_GPL(vring_interrupt); =20 /* Only available for split ring */ -static struct virtqueue *__vring_new_virtqueue(unsigned int index, +static struct virtqueue *__vring_new_virtqueue_split(unsigned int index, struct vring_virtqueue_split *vring_split, struct virtio_device *vdev, bool weak_barriers, @@ -2612,9 +2580,6 @@ static struct virtqueue *__vring_new_virtqueue(unsign= ed int index, struct vring_virtqueue *vq; int err; =20 - if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) - return NULL; - vq =3D kmalloc(sizeof(*vq), GFP_KERNEL); if (!vq) return NULL; @@ -2662,6 +2627,66 @@ static struct virtqueue *__vring_new_virtqueue(unsig= ned int index, return &vq->vq; } =20 +static struct virtqueue *__vring_new_virtqueue_packed(unsigned int index, + struct vring_virtqueue_packed *vring_packed, + struct virtio_device *vdev, + bool weak_barriers, + bool context, + bool (*notify)(struct virtqueue *), + void (*callback)(struct virtqueue *), + const char *name, + struct device *dma_dev) +{ + struct vring_virtqueue *vq; + int err; + + vq =3D kmalloc(sizeof(*vq), GFP_KERNEL); + if (!vq) + return NULL; + + vq->vq.callback =3D callback; + vq->vq.vdev =3D vdev; + vq->vq.name =3D name; + vq->vq.index =3D index; + vq->vq.reset =3D false; + vq->we_own_ring =3D false; + vq->notify =3D notify; + vq->weak_barriers =3D weak_barriers; +#ifdef CONFIG_VIRTIO_HARDEN_NOTIFICATION + vq->broken =3D true; +#else + vq->broken =3D false; +#endif + vq->packed_ring =3D true; + vq->dma_dev =3D dma_dev; + vq->use_dma_api =3D vring_use_dma_api(vdev); + vq->premapped =3D false; + vq->do_unmap =3D vq->use_dma_api; + + 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); + + if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) + vq->weak_barriers =3D false; + + err =3D vring_alloc_state_extra_packed(vring_packed); + if (err) { + kfree(vq); + return NULL; + } + + virtqueue_vring_init_packed(vring_packed, !!callback); + + virtqueue_init(vq, vring_packed->vring.num); + virtqueue_vring_attach_packed(vq, vring_packed); + + spin_lock(&vdev->vqs_list_lock); + list_add_tail(&vq->vq.list, &vdev->vqs); + spin_unlock(&vdev->vqs_list_lock); + return &vq->vq; +} + struct virtqueue *vring_create_virtqueue( unsigned int index, unsigned int num, @@ -2840,7 +2865,6 @@ int virtqueue_reset(struct virtqueue *_vq, } EXPORT_SYMBOL_GPL(virtqueue_reset); =20 -/* Only available for split ring */ struct virtqueue *vring_new_virtqueue(unsigned int index, unsigned int num, unsigned int vring_align, @@ -2853,12 +2877,19 @@ struct virtqueue *vring_new_virtqueue(unsigned int = index, const char *name) { struct vring_virtqueue_split vring_split =3D {}; + struct vring_virtqueue_packed vring_packed =3D {}; =20 - if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) - return NULL; + if (virtio_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + vring_packed.vring.num =3D num; + vring_packed.vring.desc =3D pages; + return __vring_new_virtqueue_packed(index, &vring_packed, + vdev, weak_barriers, + context, notify, callback, + name, vdev->dev.parent); + } =20 vring_init(&vring_split.vring, num, pages, vring_align); - return __vring_new_virtqueue(index, &vring_split, vdev, weak_barriers, + return __vring_new_virtqueue_split(index, &vring_split, vdev, weak_barrie= rs, context, notify, callback, name, vdev->dev.parent); } --=20 2.43.0