Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2489651rwl; Mon, 26 Dec 2022 16:15:43 -0800 (PST) X-Google-Smtp-Source: AMrXdXuDxpmc9MTofjOKXnvMH5IES9PYlF14d23+E8q4oCIq3tqMWAMw572gWkCvoi5hblDjYcNZ X-Received: by 2002:a17:902:e952:b0:192:4ce1:3687 with SMTP id b18-20020a170902e95200b001924ce13687mr25199826pll.52.1672100143586; Mon, 26 Dec 2022 16:15:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672100143; cv=none; d=google.com; s=arc-20160816; b=KTyPD73LrDk1RFtYIy8GxaLKGgJarHXZL86kQqlB3p/NXe3Pc7671NgUb0M4hE9hST 6Q7/ZjsHsqIKN427IJYYUDo5wfg4ABvqSAFlyv2bTdGtLwMc5FWg4XhyIYHH3k6fdkMt clzWNPimaQg9+m07Xwj5Gx/+4hWTbtcNMJpQ6NgQF9j1mJunHncLONaokr4zn9q6rXBz FrRgk8yWpybb2diBNeWUTSMdcGMAtKeAk44dqk0xql2LgU+RQu1KfmZ86EpPEerHil5d 67rsA4JhjuzgJBpyQ9zqIlEDQQnjTH3b1cHnfQJS4yzqPSeNZaZqBqjN5/JJY2N8cfS6 6faw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ezEM/TKhEZZEiA0qvJ103L6FIDKOFYr7NEo4WCCY3ZI=; b=Hfl9eknydsEMN7fZYbCcZQrmzWtk5TlHwQve+5P+WRjLjY0M5UeOEmUJVh0K6opQdl UMvjxDtu08SsB8xqa46i4ht99okko5G5SG2O+XcsPCyuRuFYjx+dHKOdqJoN61MvBuk/ lv/uOD6GEI9Ck3chhX4cGcX9wQizREp65/gKqMmTtHgTbVscVySBTuULNTOwUmEQRuWI NVykZYX5aY8dDQTNXHpxHLkb49GQB91ye1SacMMP9wzhE4/l656MuEnBzeuq4zV1FAes KvTrCmO9EnCZAuuNjK3WMgaI+3JpEF4ZI+w0JfwY39WJJLw+/hiCaOUSVCHnyjP/RWAX +l+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=d15TV1CZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e10-20020a170902cf4a00b0017865059c4bsi12572686plg.401.2022.12.26.16.15.35; Mon, 26 Dec 2022 16:15:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=d15TV1CZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232499AbiLZXj4 (ORCPT + 66 others); Mon, 26 Dec 2022 18:39:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232495AbiLZXjU (ORCPT ); Mon, 26 Dec 2022 18:39:20 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3AA29329 for ; Mon, 26 Dec 2022 15:38:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672097911; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ezEM/TKhEZZEiA0qvJ103L6FIDKOFYr7NEo4WCCY3ZI=; b=d15TV1CZsQW0FLIM61cYRBQQtheP5QSmKgzF0vM5N9wa2MWJQwZD07KIyw9mYRcGowlXgx FxmeQ1qTr+XWhY1wCSgyhSpq8iRx/sggkuKSLZ8wz9GdnQDRuKS1WrNM7dJ2l+KddriLev eKQa7O7V1/2sIxKeacW05p6rmkVhMSM= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-368-CzwTwy4GPUmB0323YqS7lg-1; Mon, 26 Dec 2022 18:38:30 -0500 X-MC-Unique: CzwTwy4GPUmB0323YqS7lg-1 Received: by mail-ej1-f72.google.com with SMTP id qf33-20020a1709077f2100b007c155ab74e9so8068922ejc.18 for ; Mon, 26 Dec 2022 15:38:29 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ezEM/TKhEZZEiA0qvJ103L6FIDKOFYr7NEo4WCCY3ZI=; b=YucmuS8zH6n15DjPNNx6CtjUsb7iatZeSf9jU/VuHjADSUW60XX+GrwRVNvR0weKeL vQw0pmvCIR2IFxlNjWKbdbUL60G3MIPuIev0T1JGeq7M36vky0tE2IXwznLjRE1mV/wF Ze92+cjeYiwvladsM+v60P24SbO7r6AaqztxGbnhaVVejyLGC06+76z9aeETzrrCdRL1 uWvzFbln4c0VwHgvUUiOdVUX2VHP4uYxzNXZgZQx0vWWPs+43MW64CYIsoB1qH9SoLky 6DUC6U9hEcScdtpsdDv2yJtCzA1rtiAF6d8V2Dem2mGrgYEcLZC5xIQ72q5x17UlSeQg D1VA== X-Gm-Message-State: AFqh2kpNMu6zREs5sRycrZDChq2Mqm++V0yuzAywLiWzsEtelwRFfFid fo+t+WRMABy9/E5Fz714BGhkz1AqjDTrqJpnl/X6PeuFyh+j6Cp3nC5JDALPyWyhvf++7zuNsh7 xWmXdfvHWOw0IE2kUEcQvrDXY X-Received: by 2002:a17:906:184a:b0:78d:f456:1ed0 with SMTP id w10-20020a170906184a00b0078df4561ed0mr21402802eje.33.1672097908811; Mon, 26 Dec 2022 15:38:28 -0800 (PST) X-Received: by 2002:a17:906:184a:b0:78d:f456:1ed0 with SMTP id w10-20020a170906184a00b0078df4561ed0mr21402774eje.33.1672097908469; Mon, 26 Dec 2022 15:38:28 -0800 (PST) Received: from redhat.com ([2.52.151.85]) by smtp.gmail.com with ESMTPSA id s10-20020a1709064d8a00b007815ca7ae57sm5362124eju.212.2022.12.26.15.38.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Dec 2022 15:38:28 -0800 (PST) Date: Mon, 26 Dec 2022 18:38:24 -0500 From: "Michael S. Tsirkin" To: Jason Wang Cc: davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, maxime.coquelin@redhat.com, alvaro.karsz@solid-run.com, eperezma@redhat.com Subject: Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue Message-ID: <20221226183705-mutt-send-email-mst@kernel.org> References: <20221226074908.8154-1-jasowang@redhat.com> <20221226074908.8154-4-jasowang@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221226074908.8154-4-jasowang@redhat.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 26, 2022 at 03:49:07PM +0800, Jason Wang wrote: > This patch introduces a per virtqueue waitqueue to allow driver to > sleep and wait for more used. Two new helpers are introduced to allow > driver to sleep and wake up. > > Signed-off-by: Jason Wang > --- > Changes since V1: > - check virtqueue_is_broken() as well > - use more_used() instead of virtqueue_get_buf() to allow caller to > get buffers afterwards > --- > drivers/virtio/virtio_ring.c | 29 +++++++++++++++++++++++++++++ > include/linux/virtio.h | 3 +++ > 2 files changed, 32 insertions(+) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > index 5cfb2fa8abee..9c83eb945493 100644 > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -13,6 +13,7 @@ > #include > #include > #include > +#include > #include > > #ifdef DEBUG > @@ -60,6 +61,7 @@ > "%s:"fmt, (_vq)->vq.name, ##args); \ > /* Pairs with READ_ONCE() in virtqueue_is_broken(). */ \ > WRITE_ONCE((_vq)->broken, true); \ > + wake_up_interruptible(&(_vq)->wq); \ > } while (0) > #define START_USE(vq) > #define END_USE(vq) > @@ -203,6 +205,9 @@ struct vring_virtqueue { > /* DMA, allocation, and size information */ > bool we_own_ring; > > + /* Wait for buffer to be used */ > + wait_queue_head_t wq; > + > #ifdef DEBUG > /* They're supposed to lock for us. */ > unsigned int in_use; > @@ -2024,6 +2029,8 @@ static struct virtqueue *vring_create_virtqueue_packed( > if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) > vq->weak_barriers = false; > > + init_waitqueue_head(&vq->wq); > + > err = vring_alloc_state_extra_packed(&vring_packed); > if (err) > goto err_state_extra; > @@ -2517,6 +2524,8 @@ static struct virtqueue *__vring_new_virtqueue(unsigned int index, > if (virtio_has_feature(vdev, VIRTIO_F_ORDER_PLATFORM)) > vq->weak_barriers = false; > > + init_waitqueue_head(&vq->wq); > + > err = vring_alloc_state_extra_split(vring_split); > if (err) { > kfree(vq); > @@ -2654,6 +2663,8 @@ static void vring_free(struct virtqueue *_vq) > { > struct vring_virtqueue *vq = to_vvq(_vq); > > + wake_up_interruptible(&vq->wq); > + > if (vq->we_own_ring) { > if (vq->packed_ring) { > vring_free_queue(vq->vq.vdev, > @@ -2863,4 +2874,22 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq) > } > EXPORT_SYMBOL_GPL(virtqueue_get_vring); > > +int virtqueue_wait_for_used(struct virtqueue *_vq) > +{ > + struct vring_virtqueue *vq = to_vvq(_vq); > + > + /* TODO: Tweak the timeout. */ > + return wait_event_interruptible_timeout(vq->wq, > + virtqueue_is_broken(_vq) || more_used(vq), HZ); BTW undocumented that you also make it interruptible. So if we get an interrupt then this will fail. But device is still going and will later use the buffers. Same for timeout really. > +} > +EXPORT_SYMBOL_GPL(virtqueue_wait_for_used); > + > +void virtqueue_wake_up(struct virtqueue *_vq) > +{ > + struct vring_virtqueue *vq = to_vvq(_vq); > + > + wake_up_interruptible(&vq->wq); > +} > +EXPORT_SYMBOL_GPL(virtqueue_wake_up); > + > MODULE_LICENSE("GPL"); > diff --git a/include/linux/virtio.h b/include/linux/virtio.h > index dcab9c7e8784..2eb62c774895 100644 > --- a/include/linux/virtio.h > +++ b/include/linux/virtio.h > @@ -72,6 +72,9 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len); > void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len, > void **ctx); > > +int virtqueue_wait_for_used(struct virtqueue *vq); > +void virtqueue_wake_up(struct virtqueue *vq); > + > void virtqueue_disable_cb(struct virtqueue *vq); > > bool virtqueue_enable_cb(struct virtqueue *vq); > -- > 2.25.1