From patchwork Mon Dec 26 07:49:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 36556 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp795312wrt; Sun, 25 Dec 2022 23:53:02 -0800 (PST) X-Google-Smtp-Source: AMrXdXvplU9/e4B638qgVfBra16GrMnnV4Das2/PzRIqrPcWlz52I8NZ2Ntmg8yw5YDl2lQ9Efot X-Received: by 2002:a17:907:7094:b0:844:1d1d:1fa with SMTP id yj20-20020a170907709400b008441d1d01famr14193937ejb.23.1672041182025; Sun, 25 Dec 2022 23:53:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672041182; cv=none; d=google.com; s=arc-20160816; b=QUCSiE8rHjQfym2wBncgVgGX5Io+9FQnu7fRQCXCldP3gFiDsdRlnq7puE5TEUL2El Hr541AQw2xaKPcRA2iJ+2Fy6uFYwXktrOzJU1SOfY6m9J3+98cO1NwR0r7Y9S5CuampN orjVBuU6VUi2Qd+PHSFxSSQsRdU4Uw1+Y2UufZDTai2HQqHnvybmOFnY0VICk632eBof DHtOpaM78a6FyeyYqpE/8xtefKZPerusSqpIhlhQJIHNUoTdZPSxyGsD7TES5fVeZN3Q zUvYSI9IEOF3eXrLns94LoFR0+eHJpXquVT1pdOG3qRdrr362+7ETYAlHtbJ6Ff7NW8X EV8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H/Y/p/qkj6rtja+GTIriOAzlecVLNXF/oN1BvsWBKKY=; b=Lp8jylE5bt82f3YzsgnFoIf7uidGeoJX3yiYbkBAN/ZGdYfvAedrG2ewJSi4THrfiU siN5buSDGCigv5qlukBWGdRQuc5ba3uXO6l5jh1fwXctuihL/wJGQfb/z77KozuHxdUV qX+Jhy0egARAbXkgvv2J82y0u+tEWMoAZwP4MD8QbtMt9GwF/YjlcuH1PHHxayyNkXyB curmyvOvEBlKcyP6MDGEpwZzZle+HXxvesRD2ztpXcWEzgGw7HSI1Qpmg6uvOkLcMfMc c6vr5gt066BZ72NByK6EpRY+hS5SDQww5ZVYND1ZkoG19DpyqjcGorhF3ShRDtojZnPD 8pCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=g07FrQHu; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id be5-20020a1709070a4500b007c1727f7c6fsi7825866ejc.261.2022.12.25.23.52.38; Sun, 25 Dec 2022 23:53:02 -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=g07FrQHu; 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 S231899AbiLZHuu (ORCPT + 99 others); Mon, 26 Dec 2022 02:50:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231814AbiLZHuX (ORCPT ); Mon, 26 Dec 2022 02:50:23 -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 93FAF5FE3 for ; Sun, 25 Dec 2022 23:49:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672040974; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=H/Y/p/qkj6rtja+GTIriOAzlecVLNXF/oN1BvsWBKKY=; b=g07FrQHuFKizmKBDHbtKSII5lRBSZWY/oaSQTCPKwDZaKYy3sOftsoG27E+EH1paIp/Be6 2MD8T1Pv5Lir6o8aqUxrueo2rKjGKCxY532Wj09JyV3XHb7jNODO1DMopPcK5+6uVEWgC3 ZvPUQ5j9o/cvQEdQmCQ1zhJyv9eorDE= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-112-Cl6FKtC-O92R4jqHTvdGlQ-1; Mon, 26 Dec 2022 02:49:31 -0500 X-MC-Unique: Cl6FKtC-O92R4jqHTvdGlQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1DD1A101A521; Mon, 26 Dec 2022 07:49:31 +0000 (UTC) Received: from localhost.localdomain (ovpn-13-100.pek2.redhat.com [10.72.13.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7642D492B00; Mon, 26 Dec 2022 07:49:26 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com 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: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue Date: Mon, 26 Dec 2022 15:49:07 +0800 Message-Id: <20221226074908.8154-4-jasowang@redhat.com> In-Reply-To: <20221226074908.8154-1-jasowang@redhat.com> References: <20221226074908.8154-1-jasowang@redhat.com> MIME-Version: 1.0 Content-type: text/plain X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 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=unavailable 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753262254622841933?= X-GMAIL-MSGID: =?utf-8?q?1753262254622841933?= 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); +} +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);