netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] virtio-net: reject small vring sizes
@ 2023-04-16  7:46 Alvaro Karsz
  2023-04-16 16:54 ` Alvaro Karsz
                   ` (3 more replies)
  0 siblings, 4 replies; 39+ messages in thread
From: Alvaro Karsz @ 2023-04-16  7:46 UTC (permalink / raw)
  To: mst, jasowang
  Cc: davem, edumazet, kuba, pabeni, virtualization, netdev,
	linux-kernel, Alvaro Karsz

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 <alvaro.karsz@solid-run.com>
---
 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


^ permalink raw reply related	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2023-04-25 13:09 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-16  7:46 [PATCH net] virtio-net: reject small vring sizes Alvaro Karsz
2023-04-16 16:54 ` Alvaro Karsz
2023-04-16 20:45   ` Michael S. Tsirkin
2023-04-17  3:24     ` Jason Wang
2023-04-17  6:20       ` Michael S. Tsirkin
2023-04-17  6:38         ` Alvaro Karsz
2023-04-17  6:41           ` Michael S. Tsirkin
2023-04-17  7:03             ` Alvaro Karsz
2023-04-17  7:10               ` Michael S. Tsirkin
2023-04-17  7:33                 ` Alvaro Karsz
2023-04-17  9:20                   ` Michael S. Tsirkin
2023-04-17 10:04                     ` Alvaro Karsz
2023-04-17 11:40                       ` Michael S. Tsirkin
2023-04-17 11:51                         ` Alvaro Karsz
2023-04-17 11:57                           ` Michael S. Tsirkin
2023-04-23  6:51                             ` Alvaro Karsz
2023-04-23  7:19                               ` Michael S. Tsirkin
2023-04-23  7:52                                 ` Alvaro Karsz
2023-04-23 11:06                                   ` Michael S. Tsirkin
2023-04-23 12:28                                     ` Alvaro Karsz
2023-04-23 20:17                                       ` Michael S. Tsirkin
2023-04-25  8:34                                       ` Michael S. Tsirkin
2023-04-25  9:41                                         ` Alvaro Karsz
2023-04-25 11:11                                           ` Alvaro Karsz
2023-04-25 12:33                                             ` Michael S. Tsirkin
2023-04-25 12:31                                           ` Michael S. Tsirkin
2023-04-25 13:02                                             ` Alvaro Karsz
2023-04-25 13:08                                               ` Michael S. Tsirkin
2023-04-23  8:01                                 ` Alvaro Karsz
2023-04-23 11:08                                   ` Michael S. Tsirkin
2023-04-17  6:44           ` Xuan Zhuo
2023-04-17  7:07             ` Alvaro Karsz
2023-04-17  7:11               ` Michael S. Tsirkin
2023-04-16 20:38 ` Michael S. Tsirkin
2023-04-17  6:43   ` Alvaro Karsz
2023-04-23 11:09     ` Michael S. Tsirkin
2023-04-17  1:53 ` Xuan Zhuo
2023-04-17  6:47   ` Alvaro Karsz
2023-04-17  3:34 ` Xuan Zhuo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).