From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D67B3C43217 for ; Thu, 10 Mar 2022 12:35:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241987AbiCJMgy (ORCPT ); Thu, 10 Mar 2022 07:36:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235301AbiCJMgy (ORCPT ); Thu, 10 Mar 2022 07:36:54 -0500 Received: from out30-54.freemail.mail.aliyun.com (out30-54.freemail.mail.aliyun.com [115.124.30.54]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28B6213D932; Thu, 10 Mar 2022 04:35:47 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R661e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=e01e04426;MF=xuanzhuo@linux.alibaba.com;NM=1;PH=DS;RN=33;SR=0;TI=SMTPD_---0V6oh3qv_1646915740; Received: from localhost(mailfrom:xuanzhuo@linux.alibaba.com fp:SMTPD_---0V6oh3qv_1646915740) by smtp.aliyun-inc.com(127.0.0.1); Thu, 10 Mar 2022 20:35:41 +0800 Message-ID: <1646915610.3936472-1-xuanzhuo@linux.alibaba.com> Subject: Re: [PATCH v7 09/26] virtio_ring: split: implement virtqueue_reset_vring_split() Date: Thu, 10 Mar 2022 20:33:30 +0800 From: Xuan Zhuo To: "Michael S. Tsirkin" Cc: virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, Jeff Dike , Richard Weinberger , Anton Ivanov , Jason Wang , "David S. Miller" , Jakub Kicinski , Hans de Goede , Mark Gross , Vadim Pasternak , Bjorn Andersson , Mathieu Poirier , Cornelia Huck , Halil Pasic , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Alexander Gordeev , Sven Schnelle , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Johannes Berg , Vincent Whitchurch , linux-um@lists.infradead.org, platform-driver-x86@vger.kernel.org, linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org, kvm@vger.kernel.org, bpf@vger.kernel.org References: <20220308123518.33800-1-xuanzhuo@linux.alibaba.com> <20220308123518.33800-10-xuanzhuo@linux.alibaba.com> <20220310015418-mutt-send-email-mst@kernel.org> <1646896623.3794115-2-xuanzhuo@linux.alibaba.com> <20220310025930-mutt-send-email-mst@kernel.org> <1646900056.7775025-1-xuanzhuo@linux.alibaba.com> <20220310071335-mutt-send-email-mst@kernel.org> In-Reply-To: <20220310071335-mutt-send-email-mst@kernel.org> Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Thu, 10 Mar 2022 07:17:09 -0500, "Michael S. Tsirkin" wrote: > On Thu, Mar 10, 2022 at 04:14:16PM +0800, Xuan Zhuo wrote: > > On Thu, 10 Mar 2022 03:07:22 -0500, "Michael S. Tsirkin" wrote: > > > On Thu, Mar 10, 2022 at 03:17:03PM +0800, Xuan Zhuo wrote: > > > > On Thu, 10 Mar 2022 02:00:39 -0500, "Michael S. Tsirkin" wrote: > > > > > On Tue, Mar 08, 2022 at 08:35:01PM +0800, Xuan Zhuo wrote: > > > > > > virtio ring supports reset. > > > > > > > > > > > > Queue reset is divided into several stages. > > > > > > > > > > > > 1. notify device queue reset > > > > > > 2. vring release > > > > > > 3. attach new vring > > > > > > 4. notify device queue re-enable > > > > > > > > > > > > After the first step is completed, the vring reset operation can be > > > > > > performed. If the newly set vring num does not change, then just reset > > > > > > the vq related value. > > > > > > > > > > > > Otherwise, the vring will be released and the vring will be reallocated. > > > > > > And the vring will be attached to the vq. If this process fails, the > > > > > > function will exit, and the state of the vq will be the vring release > > > > > > state. You can call this function again to reallocate the vring. > > > > > > > > > > > > In addition, vring_align, may_reduce_num are necessary for reallocating > > > > > > vring, so they are retained when creating vq. > > > > > > > > > > > > Signed-off-by: Xuan Zhuo > > > > > > --- > > > > > > drivers/virtio/virtio_ring.c | 69 ++++++++++++++++++++++++++++++++++++ > > > > > > 1 file changed, 69 insertions(+) > > > > > > > > > > > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > > > > > > index e0422c04c903..148fb1fd3d5a 100644 > > > > > > --- a/drivers/virtio/virtio_ring.c > > > > > > +++ b/drivers/virtio/virtio_ring.c > > > > > > @@ -158,6 +158,12 @@ struct vring_virtqueue { > > > > > > /* DMA address and size information */ > > > > > > dma_addr_t queue_dma_addr; > > > > > > size_t queue_size_in_bytes; > > > > > > + > > > > > > + /* The parameters for creating vrings are reserved for > > > > > > + * creating new vrings when enabling reset queue. > > > > > > + */ > > > > > > + u32 vring_align; > > > > > > + bool may_reduce_num; > > > > > > } split; > > > > > > > > > > > > /* Available for packed ring */ > > > > > > @@ -217,6 +223,12 @@ struct vring_virtqueue { > > > > > > #endif > > > > > > }; > > > > > > > > > > > > +static void vring_free(struct virtqueue *vq); > > > > > > +static void __vring_virtqueue_init_split(struct vring_virtqueue *vq, > > > > > > + struct virtio_device *vdev); > > > > > > +static int __vring_virtqueue_attach_split(struct vring_virtqueue *vq, > > > > > > + struct virtio_device *vdev, > > > > > > + struct vring vring); > > > > > > > > > > > > /* > > > > > > * Helpers. > > > > > > @@ -1012,6 +1024,8 @@ static struct virtqueue *vring_create_virtqueue_split( > > > > > > return NULL; > > > > > > } > > > > > > > > > > > > + to_vvq(vq)->split.vring_align = vring_align; > > > > > > + to_vvq(vq)->split.may_reduce_num = may_reduce_num; > > > > > > to_vvq(vq)->split.queue_dma_addr = vring.dma_addr; > > > > > > to_vvq(vq)->split.queue_size_in_bytes = vring.queue_size_in_bytes; > > > > > > to_vvq(vq)->we_own_ring = true; > > > > > > @@ -1019,6 +1033,59 @@ static struct virtqueue *vring_create_virtqueue_split( > > > > > > return vq; > > > > > > } > > > > > > > > > > > > +static int virtqueue_reset_vring_split(struct virtqueue *_vq, u32 num) > > > > > > +{ > > > > > > + struct vring_virtqueue *vq = to_vvq(_vq); > > > > > > + struct virtio_device *vdev = _vq->vdev; > > > > > > + struct vring_split vring; > > > > > > + int err; > > > > > > + > > > > > > + if (num > _vq->num_max) > > > > > > + return -E2BIG; > > > > > > + > > > > > > + switch (vq->vq.reset) { > > > > > > + case VIRTIO_VQ_RESET_STEP_NONE: > > > > > > + return -ENOENT; > > > > > > + > > > > > > + case VIRTIO_VQ_RESET_STEP_VRING_ATTACH: > > > > > > + case VIRTIO_VQ_RESET_STEP_DEVICE: > > > > > > + if (vq->split.vring.num == num || !num) > > > > > > + break; > > > > > > + > > > > > > + vring_free(_vq); > > > > > > + > > > > > > + fallthrough; > > > > > > + > > > > > > + case VIRTIO_VQ_RESET_STEP_VRING_RELEASE: > > > > > > + if (!num) > > > > > > + num = vq->split.vring.num; > > > > > > + > > > > > > + err = vring_create_vring_split(&vring, vdev, > > > > > > + vq->split.vring_align, > > > > > > + vq->weak_barriers, > > > > > > + vq->split.may_reduce_num, num); > > > > > > + if (err) > > > > > > + return -ENOMEM; > > > > > > + > > > > > > + err = __vring_virtqueue_attach_split(vq, vdev, vring.vring); > > > > > > + if (err) { > > > > > > + vring_free_queue(vdev, vring.queue_size_in_bytes, > > > > > > + vring.queue, > > > > > > + vring.dma_addr); > > > > > > + return -ENOMEM; > > > > > > + } > > > > > > + > > > > > > + vq->split.queue_dma_addr = vring.dma_addr; > > > > > > + vq->split.queue_size_in_bytes = vring.queue_size_in_bytes; > > > > > > + } > > > > > > + > > > > > > + __vring_virtqueue_init_split(vq, vdev); > > > > > > + vq->we_own_ring = true; > > > > > > + vq->vq.reset = VIRTIO_VQ_RESET_STEP_VRING_ATTACH; > > > > > > + > > > > > > + return 0; > > > > > > +} > > > > > > + > > > > > > > > > > I kind of dislike this state machine. > > > > > > > > > > Hacks like special-casing num = 0 to mean "reset" are especially > > > > > confusing. > > > > > > > > I'm removing it. I'll say in the function description that this function is > > > > currently only called when vq has been reset. I'm no longer checking it based on > > > > state. > > > > > > > > > > > > > > And as Jason points out, when we want a resize then yes this currently > > > > > implies reset but that is an implementation detail. > > > > > > > > > > There should be a way to just make these cases separate functions > > > > > and then use them to compose consistent external APIs. > > > > > > > > Yes, virtqueue_resize_split() is fine for ethtool -G. > > > > > > > > But in the case of AF_XDP, just execute reset to free the buffer. The name > > > > virtqueue_reset_vring_split() I think can cover both cases. Or we use two apis > > > > to handle both scenarios? > > > > > > > > Or can anyone think of a better name. ^_^ > > > > > > > > Thanks. > > > > > > > > > I'd say resize should be called resize and reset should be called reset. > > > > > > OK, I'll change it to resize here. > > > > But I want to know that when I implement virtio-net to support AF_XDP, its > > requirement is to release all submitted buffers. Then should I add a new api > > such as virtqueue_reset_vring()? > > Sounds like a reasonable name. > > > > > > > The big issue is a sane API for resize. Ideally it would resubmit > > > buffers which did not get used. Question is what to do > > > about buffers which don't fit (if ring has been downsized)? > > > Maybe a callback that will handle them? > > > And then what? Queue them up and readd later? Drop? > > > If we drop we should drop from the head not the tail ... > > > > It's a good idea, let's implement it later. > > > > Thanks. > > Well ... not sure how you are going to support resize > if you don't know what to do with buffers that were > in the ring. The current solution is to call virtqueue_detach_unused_buf() to release buffers before resize ring. Thanks.