Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp3379673rwe; Sun, 16 Apr 2023 18:54:18 -0700 (PDT) X-Google-Smtp-Source: AKy350Yk4u4OCBJJejmKSzDOwFmoBrSG+ofzNDD73XJNMFbm/2hfBs9uJGhpTbPnw0FV8VF0PYiG X-Received: by 2002:a17:90a:d783:b0:247:78a3:927f with SMTP id z3-20020a17090ad78300b0024778a3927fmr5508843pju.7.1681696458506; Sun, 16 Apr 2023 18:54:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681696458; cv=none; d=google.com; s=arc-20160816; b=qG76jKRq0NrykSa/PIMl+0Qf+Yrrjo4AbexDC6Gp4/k+4XgROKA00X8o0lf3SMSyb+ 42uWedIHiCgw03aZB6+UCBVQVmlPRXCjE4mmr7CHT7tVwuNFzXV3JVcSsDs45qxALEBh Ng2bJmdfWz3YmJSBaUKwvTJYAAe76QOaa7Y+gn4fjv9zdmwKDN2OEQYZuNBPV5uDNv3W 5hczB+lLDc45299PInsGYwO78QEw54Y4TGVtJ/eXLrzcjb/EjK7Xm+A+VO72/87HYBdy 4p48y1I5ZbouCuLE7AXBrXxAkA+IUbNHogI0n9EaQh9g+wPZ0CAJ5IcBfUhsRpoibkBX DYFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:cc:to:from:date:subject :message-id; bh=CKSRyw93qRaZL+B85atpIw6yY0UVNBA8nI+zJ4i7sp0=; b=e0hbslVukJN9OAGz7TGamvPkwMjhNJACDBg84uClN7LhcxGKm5z687PV5kzRY+3UbU 29CrGIiEFLmfdCAUvwX5JCnjJd6Pxh8g8s7lOFeEHI8GDXzGDmRVt1Cv0X5sbO4TrIx4 N/Fi9YZVJPqXv1TfFbcfyC2zKa0Ea5Q4Fio/RpJwApVbichQUZEI0JMmQdpzEEarJVmt mwsAIYy0XpKkCpf6hjYgqUzIxqUorL2Gnh/f5Miscm6bWW4yNtLQgT9YC3o1Aw2qWFQj gUlMPi44hV1p7ri/Pa8XMEABFKnJr4UrxTE+8s9NEN9zpYjFBJ8Qu+HmLKU2EAecKyz2 Nbnw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bc22-20020a656d96000000b00513965a1e60si9723429pgb.902.2023.04.16.18.54.05; Sun, 16 Apr 2023 18:54:18 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229540AbjDQBx7 (ORCPT + 99 others); Sun, 16 Apr 2023 21:53:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbjDQBx6 (ORCPT ); Sun, 16 Apr 2023 21:53:58 -0400 Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 586051987; Sun, 16 Apr 2023 18:53:56 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R721e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046060;MF=xuanzhuo@linux.alibaba.com;NM=1;PH=DS;RN=10;SR=0;TI=SMTPD_---0VgBVEby_1681696431; Received: from localhost(mailfrom:xuanzhuo@linux.alibaba.com fp:SMTPD_---0VgBVEby_1681696431) by smtp.aliyun-inc.com; Mon, 17 Apr 2023 09:53:52 +0800 Message-ID: <1681696410.7972026-1-xuanzhuo@linux.alibaba.com> Subject: Re: [PATCH net] virtio-net: reject small vring sizes Date: Mon, 17 Apr 2023 09:53:30 +0800 From: Xuan Zhuo To: Alvaro Karsz 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, Alvaro Karsz , mst@redhat.com, jasowang@redhat.com References: <20230416074607.292616-1-alvaro.karsz@solid-run.com> In-Reply-To: <20230416074607.292616-1-alvaro.karsz@solid-run.com> X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY, USER_IN_DEF_SPF_WL 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 Sun, 16 Apr 2023 10:46:07 +0300, Alvaro Karsz wrote: > Check vring size and fail probe if a transmit/receive vring size is > smaller than MAX_SKB_FRAGS + 2. > > At the moment, any vring size is accepted. This is problematic because > it may result in attempting to transmit a packet with more fragments > than there are descriptors in the ring. So, why we check the rx ring? Thanks. > > Furthermore, it leads to an immediate bug: > > The condition: (sq->vq->num_free >= 2 + MAX_SKB_FRAGS) in > virtnet_poll_cleantx and virtnet_poll_tx always evaluates to false, > so netif_tx_wake_queue is not called, leading to TX timeouts. > > Signed-off-by: Alvaro Karsz > --- > drivers/net/virtio_net.c | 24 ++++++++++++++++++++++++ > 1 file changed, 24 insertions(+) > > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c > index 2396c28c012..59676252c5c 100644 > --- a/drivers/net/virtio_net.c > +++ b/drivers/net/virtio_net.c > @@ -3745,6 +3745,26 @@ static int init_vqs(struct virtnet_info *vi) > return ret; > } > > +static int virtnet_validate_vqs(struct virtnet_info *vi) > +{ > + u32 i, min_size = roundup_pow_of_two(MAX_SKB_FRAGS + 2); > + > + /* Transmit/Receive vring size must be at least MAX_SKB_FRAGS + 2 > + * (fragments + linear part + virtio header) > + */ > + for (i = 0; i < vi->max_queue_pairs; i++) { > + if (virtqueue_get_vring_size(vi->sq[i].vq) < min_size || > + virtqueue_get_vring_size(vi->rq[i].vq) < min_size) { > + dev_warn(&vi->vdev->dev, > + "Transmit/Receive virtqueue vring size must be at least %u\n", > + min_size); > + return -EINVAL; > + } > + } > + > + return 0; > +} > + > #ifdef CONFIG_SYSFS > static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue, > char *buf) > @@ -4056,6 +4076,10 @@ static int virtnet_probe(struct virtio_device *vdev) > if (err) > goto free; > > + err = virtnet_validate_vqs(vi); > + if (err) > + goto free_vqs; > + > #ifdef CONFIG_SYSFS > if (vi->mergeable_rx_bufs) > dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group; > -- > 2.34.1 >