Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp3181155rwe; Sun, 16 Apr 2023 13:46:46 -0700 (PDT) X-Google-Smtp-Source: AKy350ZBI90ewqLPB4h4koEnObiask1vo+aPWjMdsszQ7mxakQZ6SDVhadxllsTVy0Mk0t3sSD8e X-Received: by 2002:a17:902:d4ca:b0:1a1:c6a7:44f5 with SMTP id o10-20020a170902d4ca00b001a1c6a744f5mr12609023plg.52.1681678005844; Sun, 16 Apr 2023 13:46:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681678005; cv=none; d=google.com; s=arc-20160816; b=DI6X6Jh0VtBpscHZIcAybp3lZVAl+kvybhwDNNpS19iWATD2osmB8f8KU9gjtqnSoK 9aXCodY+EMZfyygYXLjq+rXzy/hS4ZShCXzFem3yS6wTvi/VRsm13QhJjuKABjMzko8g Si1sJIoMSVHomYU4SHClS+G+BzMaqaQjHxdwhJC+6FraeFHXkF8xBjQs3aDT1rlQt1Hr B/SNRCis6M1kM/NKBjHRDmOpnV0JoLYCk1c6BqvxeHdKjv4tRf/Cbm4R4b0p8RqsVi7M 9hU2wQI0a6Y5nDKZDRDyrwXAY024ZDVYFGHHlGI3ZQxurN8DGA2h2D4l+wPBQwTxLEoQ /yuw== 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=1gmjEPQ1Z2707YDu2q8TGB4gvRcqR+kjHgJ0ZU8q2GE=; b=DA97ys3bc6xrgjHePHZCRwFQFmkFDmBDmzEFvh8rdy3ykZzL10gF8vSx3EAr4YX5W1 935CYDf02KPNP4DXSGRh3wDHuYfY8JJdLgxl0ssa+5/GLf4PW6h1+45i1TckNfn7YCBx J44Gdy6gK1vIFL3vtktBJxitBpKI+2bMYzKciy3JD2m34blHBM12xe6PmfA9YDM0KXoH VUGxOfJ3wlvXaXsGrF8k2ODUmj0KOlfqFggiVAAi8zQDfGAk+RZ0fXChxsXO5TfgK8Jy S8xASgBictFQ/7IDuwXfOwO44emnpmna4h178bE9viqRLKJhPXkWZvszI9fyjqjfB291 +V0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UABSA020; 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 x4-20020a170902ec8400b0019f3c9d839dsi10970134plg.576.2023.04.16.13.46.35; Sun, 16 Apr 2023 13:46:45 -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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UABSA020; 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 S229762AbjDPUjH (ORCPT + 99 others); Sun, 16 Apr 2023 16:39:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229519AbjDPUjG (ORCPT ); Sun, 16 Apr 2023 16:39:06 -0400 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 4C1242117 for ; Sun, 16 Apr 2023 13:38:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681677498; 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=1gmjEPQ1Z2707YDu2q8TGB4gvRcqR+kjHgJ0ZU8q2GE=; b=UABSA020eadNdRYgNssvgDc9HlSzrAN2ANud2WR7U5X3ZPIzghaOt7q32NRKV+FRdEtkE2 I8vuMMnXYhcYu0zZ12/sSyl4eKVcFljgMDckuskuhG2lepF695Pqy2m//FDNoX+wq4lDAL 155AuKsrXsv4J49Ra5h1w45bX8Apmzs= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-wBcq_esJOc-D5xoNm3eyWA-1; Sun, 16 Apr 2023 16:38:17 -0400 X-MC-Unique: wBcq_esJOc-D5xoNm3eyWA-1 Received: by mail-ed1-f69.google.com with SMTP id n6-20020a5099c6000000b00502c2f26133so22456765edb.12 for ; Sun, 16 Apr 2023 13:38:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681677496; x=1684269496; 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=1gmjEPQ1Z2707YDu2q8TGB4gvRcqR+kjHgJ0ZU8q2GE=; b=JWPSNXpd2YLrLdFGqQu7mbl+Nsoeh5Sp+WhgG7tojcNbvboQhi//hmr4eDAH++J8Ju AMQM92bYnkuA2EmklP3p2/WQu54ZDXEvfh3UToJv5EPbj8fbzel2HAageqHr+fKqNpgj gwPMOF17UkFBSebjecASmXDCJd0azCBBYFK2IC/GgpKaFOUb/Zcm6vt1N9YOYEEX/OIw SvQUCnekf1KqOQ10Ro3q9l/oT+tzQbpYSbaMXIgpT2WQsKL1DCEQCcncCPXZTn5ZSEjK ayOvpcrObWD+mYjV3NY0PLLTfAHbkgNmAY72DV0wv7XBmvpulJ+h/iu48cN9ZvM7HRqQ 777w== X-Gm-Message-State: AAQBX9dIzGjCWrjTP0bGLpBWfA+8prTMWd1+tNB2yDUpw1gTrJDaVBuI 22omrUU2xu0hf5ziUq6m5CiTGiEIvcFVd71NiWs21DQ3mJC5s5fihkuIxtP/fgCLuChy/qGmI9n /KOABbPg5n8XbukcNilVjZ0uK X-Received: by 2002:a17:906:bc42:b0:94f:5e17:e80d with SMTP id s2-20020a170906bc4200b0094f5e17e80dmr2534421ejv.45.1681677495993; Sun, 16 Apr 2023 13:38:15 -0700 (PDT) X-Received: by 2002:a17:906:bc42:b0:94f:5e17:e80d with SMTP id s2-20020a170906bc4200b0094f5e17e80dmr2534403ejv.45.1681677495687; Sun, 16 Apr 2023 13:38:15 -0700 (PDT) Received: from redhat.com ([2.52.136.129]) by smtp.gmail.com with ESMTPSA id xg12-20020a170907320c00b0094ee99eeb01sm3877035ejb.150.2023.04.16.13.38.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Apr 2023 13:38:15 -0700 (PDT) Date: Sun, 16 Apr 2023 16:38:11 -0400 From: "Michael S. Tsirkin" To: Alvaro Karsz Cc: jasowang@redhat.com, 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 Subject: Re: [PATCH net] virtio-net: reject small vring sizes Message-ID: <20230416163751-mutt-send-email-mst@kernel.org> References: <20230416074607.292616-1-alvaro.karsz@solid-run.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230416074607.292616-1-alvaro.karsz@solid-run.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,T_SCC_BODY_TEXT_LINE 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, Apr 16, 2023 at 10:46:07AM +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. > > 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); why power of two? > + > + /* 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