Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp41533rwj; Wed, 21 Dec 2022 22:41:16 -0800 (PST) X-Google-Smtp-Source: AMrXdXs0Cq/cfg6DMp3Q9452zkKOcIA9UdCPpdktmRwzBSGalDLetYxBfun6007MA7FkvdpGFzd/ X-Received: by 2002:a05:6402:177b:b0:47e:5b70:de82 with SMTP id da27-20020a056402177b00b0047e5b70de82mr3137155edb.21.1671691276575; Wed, 21 Dec 2022 22:41:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671691276; cv=none; d=google.com; s=arc-20160816; b=M/kz42E+RhzikzhL1NpvQ+QtpF3GiWcpvizi40wPA4dHzHjRV030TVgE9uZ/KglOUR K/0we7bqnC350WcDWuCVTZQ2LLofIfs0QsqUgkPqwT6lmGa5rdw28i8v4aDw+S9Ot6xb HReiysv8g79KKAK0JxKTKuwiWEnCt+Qu0Mbul9pJlU94SenY3C9rXeW8tzGxbUITHdl1 NahQL3zJoIfCV7Ey2adWdzaitmWyBDIbeAHxl4JZwW2VVB1RpGSUMusOvSGmU3CPANA1 PbM4dQd7INZ/OR3TuaE5gZ6QNW8X1i6cjJDUVIxzcEj1uiN1AnwFQo3FRP0JZqxNP6M8 1SSg== 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=3AagJqkYYDM80wKW6v4HoTlfL3zWbTRSgD3VOT9tZZo=; b=qq+u8gpIwkBKkPOm8+0j2Uwel7Uurk0xBjtwKbc8TBGAQOEsu7/Y5v+wDy6eCXVhHi T31tnv81h7CYoLPXGRP/vGSAraGKDygbS7YeRTgKmiwerz/jNhYOTRKFPwcxyk2mycPR +bUPoSva8kK67aR6kXNv/1PF2TaEk5BXgR6+PxOvTrs1T1Dy4HY1p94HrOtGX4+23jCy e+bH1Xt3zZoV+rlf5obj0thrhOGwl5eVPbc8ZyD3y5q9mum11UmC+88EyOSofhnoWe+a CMTaIpFZjs3UavLQNnmzP2Is1wN3q1ScmVe7o4XJILncWKIu7P+qFiv5mzYjcOyonCiN AyGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HCxcG1xf; 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 sa11-20020a1709076d0b00b008318885e1f1si4955768ejc.176.2022.12.21.22.41.01; Wed, 21 Dec 2022 22:41:16 -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=HCxcG1xf; 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 S235096AbiLVGGU (ORCPT + 67 others); Thu, 22 Dec 2022 01:06:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235095AbiLVGGI (ORCPT ); Thu, 22 Dec 2022 01:06:08 -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 50D2C1B7A9 for ; Wed, 21 Dec 2022 22:05:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1671689101; 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=3AagJqkYYDM80wKW6v4HoTlfL3zWbTRSgD3VOT9tZZo=; b=HCxcG1xfGuIu2LKzja90HmGxsro640WzChF+SY3kSsgESNb0683x9Ce7wwQmRSFbzrf2NJ z3g2pnbtwRDRSe/4drPzChrRxXHsEXm411yD3yq/IWk2k5tSQrpMhTV6euqPz/FHxH1GJO Rqpvkjpdf/pTAcLYqivwvy9coiMjhlU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-638-LoPDdRuFMEmvKTedKAP_YA-1; Thu, 22 Dec 2022 01:04:55 -0500 X-MC-Unique: LoPDdRuFMEmvKTedKAP_YA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2D0341C04B79; Thu, 22 Dec 2022 06:04:55 +0000 (UTC) Received: from localhost.localdomain (ovpn-13-179.pek2.redhat.com [10.72.13.179]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8B80E112132C; Thu, 22 Dec 2022 06:04:49 +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: [RFC PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue Date: Thu, 22 Dec 2022 14:04:26 +0800 Message-Id: <20221222060427.21626-4-jasowang@redhat.com> In-Reply-To: <20221222060427.21626-1-jasowang@redhat.com> References: <20221222060427.21626-1-jasowang@redhat.com> MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 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 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 --- drivers/virtio/virtio_ring.c | 31 +++++++++++++++++++++++++++++++ include/linux/virtio.h | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 90c2034a77f3..4a2d5ac30b0f 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #ifdef DEBUG @@ -59,6 +60,7 @@ dev_err(&_vq->vq.vdev->dev, \ "%s:"fmt, (_vq)->vq.name, ##args); \ (_vq)->broken = true; \ + wake_up_interruptible(&(_vq)->wq); \ } while (0) #define START_USE(vq) #define END_USE(vq) @@ -202,6 +204,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; @@ -2023,6 +2028,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; @@ -2516,6 +2523,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); @@ -2653,6 +2662,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,24 @@ const struct vring *virtqueue_get_vring(struct virtqueue *vq) } EXPORT_SYMBOL_GPL(virtqueue_get_vring); +int virtqueue_wait_for_used(struct virtqueue *_vq, + unsigned int *len) +{ + struct vring_virtqueue *vq = to_vvq(_vq); + + /* Use a better timeout or simply start from no timeout */ + return wait_event_interruptible_timeout(vq->wq, + virtqueue_get_buf(_vq, len), + 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..4df098b8f1ca 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -72,6 +72,10 @@ 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, + unsigned int *len); +void virtqueue_wake_up(struct virtqueue *vq); + void virtqueue_disable_cb(struct virtqueue *vq); bool virtqueue_enable_cb(struct virtqueue *vq); -- 2.25.1