Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2921862rwl; Tue, 27 Dec 2022 01:29:18 -0800 (PST) X-Google-Smtp-Source: AMrXdXs+lHqz69CCnVE+4DIP4TDu9406DRBKDRT4PBwkKuTGqYctzW+0CcWeawINpjzP2ed46Dv5 X-Received: by 2002:a05:6a20:c508:b0:af:82aa:49e with SMTP id gm8-20020a056a20c50800b000af82aa049emr23205150pzb.2.1672133358577; Tue, 27 Dec 2022 01:29:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672133358; cv=none; d=google.com; s=arc-20160816; b=lAGkLJYWmDiR50e2T7Wd92eSmK1yVPnQeb3AZ6uV5sAXjM/oQJVumQeRV9f4SrfsTM iwQls8GiVFXy+vbCFncUR1LHTRwXTCahUiJbJEkbzkqRIx6SM7E2OzvU5QxcWclwwOK/ 4pGf6UxEZN/hdOgIy1NJqX5ES/2ezhYS9epWUIZKSRlVRSdK4k67HhpbuU1t9bZJKJ04 p7fVXV4TJPnGfroaN79pgQO4cKKj7erLDsnJXoXI0O1e9KTj60XQZuolxMH3yR/8RCU0 w4DK9/VYxMz2ZvTQqUczJfkfVWeCn8X8TmNYOxjMWuD++FrgT4U+vIB0FaWf7XKzY0ZV uOmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=ZSradRKv8X4tppTdp+nyd/8jKoHTyaIYm93jfzfVBMs=; b=fUlk/gpE3GgIBkYc3qU0hkNgPoXmHLkaGY+UG32P/fQbwkEEgBd8Q8f71/AGQYzLz9 TQEHPpHoQ0aiot9KDUx6jirS1ghCOyTUg+p6DgoLdWMO6tm0+t6rO4CPYsXpAnKRaSh2 dj47bLmIjJ91K8Kr1yqMTdMj60jTicRAcSfucVqgeMYEIAz2+RSUpgEu8Oow767W2iHN Ej90KOHPIg5KNx2B+J9jFpf1nOIP2xwQxnPRNwhfep6t2BNMFJ/XrRSr/b3NOCMn5kwL PtoMKW+TXxMVzWG9TJI9szC4xIJWG9C1/MTM9UlOQmH+PVcuQK7Z9cTgcsrGqtwLZToD wdQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="eh+AA/aI"; 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 o16-20020a63fb10000000b004703be83ebesi13296699pgh.79.2022.12.27.01.29.09; Tue, 27 Dec 2022 01:29:18 -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="eh+AA/aI"; 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 S229751AbiL0JKV (ORCPT + 66 others); Tue, 27 Dec 2022 04:10:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229936AbiL0JKL (ORCPT ); Tue, 27 Dec 2022 04:10:11 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CF786545 for ; Tue, 27 Dec 2022 01:09:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1672132163; 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=ZSradRKv8X4tppTdp+nyd/8jKoHTyaIYm93jfzfVBMs=; b=eh+AA/aIHMOf3t4J8IZFY+kd4svqiFvpdZ9Ie/+PTTWLN10zgjaG+C/rzVvjuC3KF+KxML B3cmW+8PSGHpXwoL+XB2pQ04/sZHjOHxEsCoIyq9M7VgJkecujZ2zknnLz9yP5ci5ZLmnJ 9rbqrISFf1WckdlbZcvP/E6hCZw9OUw= Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-549-lHPc77iNOcSJuLRYEDcHlA-1; Tue, 27 Dec 2022 04:09:21 -0500 X-MC-Unique: lHPc77iNOcSJuLRYEDcHlA-1 Received: by mail-pf1-f200.google.com with SMTP id b6-20020aa78706000000b00579e3853125so6698910pfo.6 for ; Tue, 27 Dec 2022 01:09:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZSradRKv8X4tppTdp+nyd/8jKoHTyaIYm93jfzfVBMs=; b=emojtfHrCLsqkeBAqhSKdrNPPxEZvevBLdNGWGNfWijZoF9a4ORf5m0GxxyvbKALhg aPb+5f9+v3HfxPnfN4K6MADy/jrP1L3GSxkX4pYokfWbuX8+n4jx9TEcheuZJdWUonLF cs/TplhqfTnw/iHBWUN0fs1Skpw6sgVMZGe3DAffvFzyQuhpWT19k8CcmjMCIbgfWfsO X+cp6ipTkpITR0h88GWA6xh4SZksJe3ijLzAKLM8DSJNBdKTRGH1OGul4ID81H7DZ/1I kAoQo7tj1zgl84p+CwnrFsWfMRLQ3DuxM6yvAarf2HjaiWkAhFsruIaJzsK6v3uI/+l0 2/Cw== X-Gm-Message-State: AFqh2koiB6+ZO8J1KyD8VxmjkUGJp0gkqfEC5A+GUnHxOT2e+11i6xBL WE2Vn8T1JM7ap3nsq2mreQlOslgUo9fDMFY40neU8Rs2E/G8E59wERLzjdfwe35hetH7S9jLRhd Kq364o6/fkHGdoLkdFDOaONQO X-Received: by 2002:a05:6a21:9990:b0:a6:f26b:558 with SMTP id ve16-20020a056a21999000b000a6f26b0558mr34620492pzb.16.1672132160881; Tue, 27 Dec 2022 01:09:20 -0800 (PST) X-Received: by 2002:a05:6a21:9990:b0:a6:f26b:558 with SMTP id ve16-20020a056a21999000b000a6f26b0558mr34620466pzb.16.1672132160560; Tue, 27 Dec 2022 01:09:20 -0800 (PST) Received: from [10.72.13.143] ([43.228.180.230]) by smtp.gmail.com with ESMTPSA id p188-20020a625bc5000000b00580fe3b958esm4044388pfb.131.2022.12.27.01.09.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 27 Dec 2022 01:09:20 -0800 (PST) Message-ID: <0abaec22-ec5f-9136-b043-0989d97b209f@redhat.com> Date: Tue, 27 Dec 2022 17:09:12 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH 3/4] virtio_ring: introduce a per virtqueue waitqueue Content-Language: en-US To: "Michael S. Tsirkin" 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 References: <20221226074908.8154-1-jasowang@redhat.com> <20221226074908.8154-4-jasowang@redhat.com> <20221226183348-mutt-send-email-mst@kernel.org> <20221227020901-mutt-send-email-mst@kernel.org> From: Jason Wang In-Reply-To: <20221227020901-mutt-send-email-mst@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, 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 在 2022/12/27 15:19, Michael S. Tsirkin 写道: > On Tue, Dec 27, 2022 at 11:47:34AM +0800, Jason Wang wrote: >> On Tue, Dec 27, 2022 at 7:34 AM Michael S. Tsirkin wrote: >>> 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); >>> There's no good timeout. Let's not even go there, if device goes >>> bad it should set the need reset bit. >> The problem is that we can't depend on the device. If it takes too >> long for the device to respond to cvq, there's a high possibility that >> the device is buggy or even malicious. We can have a higher timeout >> here and it should be still better than waiting forever (the cvq >> commands need to be serialized so it needs to hold a lock anyway >> (RTNL) ). >> >> Thanks > With a TODO item like this I'd expect this to be an RFC. > Here's why: > > Making driver more robust from device failures is a laudable goal but it's really > hard to be 100% foolproof here. E.g. device can just block pci reads and > it would be very hard to recover. Yes. > So I'm going to only merge patches > like this if they at least theoretically have very little chance > of breaking existing users. AFAIK, this is not theoretical, consider: 1) DPU may implement virtio-net CVQ with codes running in CPU 2) VDUSE may want to support CVQ in the future > > And note that in most setups, CVQ is only used at startup and then left mostly alone. > > Finally, note that lots of guests need virtio to do anything useful at all. > So just failing commands is not enough to recover - you need to try > harder maybe by attempting to reset device. This requires upper layer support which seems not existed in the networking subsystem. > Could be a question of > policy - might need to make this guest configurable. Yes. Thanks > > > >>> >>>> +} >>>> +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