Commit 5d182f71 authored by Paolo Abeni's avatar Paolo Abeni
Browse files

Merge branch 'virtio_net-make-rss-interact-properly-with-queue-number'

Philo Lu says:

====================
virtio_net: Make RSS interact properly with queue number

With this patch set, RSS updates with queue_pairs changing:
- When virtnet_probe, init default rss and commit
- When queue_pairs changes _without_ user rss configuration, update rss
  with the new queue number
- When queue_pairs changes _with_ user rss configuration, keep rss as user
  configured

Patch 1 and 2 fix possible out of bound errors for indir_table and key.
Patch 3 and 4 add RSS update in probe() and set_queues().
====================

Link: https://patch.msgid.link/20241104085706.13872-1-lulie@linux.alibaba.com


Signed-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parents 25d70702 50bfcaed
Loading
Loading
Loading
Loading
+100 −19
Original line number Diff line number Diff line
@@ -368,15 +368,16 @@ struct receive_queue {
 * because table sizes may be differ according to the device configuration.
 */
#define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
#define VIRTIO_NET_RSS_MAX_TABLE_LEN    128
struct virtio_net_ctrl_rss {
	u32 hash_types;
	u16 indirection_table_mask;
	u16 unclassified_queue;
	u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
	u16 hash_cfg_reserved; /* for HASH_CONFIG (see virtio_net_hash_config for details) */
	u16 max_tx_vq;
	u8 hash_key_length;
	u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];

	u16 *indirection_table;
};

/* Control VQ buffers: protected by the rtnl lock */
@@ -512,6 +513,25 @@ static struct sk_buff *virtnet_skb_append_frag(struct sk_buff *head_skb,
					       struct page *page, void *buf,
					       int len, int truesize);

static int rss_indirection_table_alloc(struct virtio_net_ctrl_rss *rss, u16 indir_table_size)
{
	if (!indir_table_size) {
		rss->indirection_table = NULL;
		return 0;
	}

	rss->indirection_table = kmalloc_array(indir_table_size, sizeof(u16), GFP_KERNEL);
	if (!rss->indirection_table)
		return -ENOMEM;

	return 0;
}

static void rss_indirection_table_free(struct virtio_net_ctrl_rss *rss)
{
	kfree(rss->indirection_table);
}

static bool is_xdp_frame(void *ptr)
{
	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
@@ -3374,15 +3394,59 @@ static void virtnet_ack_link_announce(struct virtnet_info *vi)
		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
}

static bool virtnet_commit_rss_command(struct virtnet_info *vi);

static void virtnet_rss_update_by_qpairs(struct virtnet_info *vi, u16 queue_pairs)
{
	u32 indir_val = 0;
	int i = 0;

	for (; i < vi->rss_indir_table_size; ++i) {
		indir_val = ethtool_rxfh_indir_default(i, queue_pairs);
		vi->rss.indirection_table[i] = indir_val;
	}
	vi->rss.max_tx_vq = queue_pairs;
}

static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
{
	struct virtio_net_ctrl_mq *mq __free(kfree) = NULL;
	struct scatterlist sg;
	struct virtio_net_ctrl_rss old_rss;
	struct net_device *dev = vi->dev;
	struct scatterlist sg;

	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
		return 0;

	/* Firstly check if we need update rss. Do updating if both (1) rss enabled and
	 * (2) no user configuration.
	 *
	 * During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is,
	 * the device updates queue_pairs together with rss, so we can skip the sperate queue_pairs
	 * update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.
	 */
	if (vi->has_rss && !netif_is_rxfh_configured(dev)) {
		memcpy(&old_rss, &vi->rss, sizeof(old_rss));
		if (rss_indirection_table_alloc(&vi->rss, vi->rss_indir_table_size)) {
			vi->rss.indirection_table = old_rss.indirection_table;
			return -ENOMEM;
		}

		virtnet_rss_update_by_qpairs(vi, queue_pairs);

		if (!virtnet_commit_rss_command(vi)) {
			/* restore ctrl_rss if commit_rss_command failed */
			rss_indirection_table_free(&vi->rss);
			memcpy(&vi->rss, &old_rss, sizeof(old_rss));

			dev_warn(&dev->dev, "Fail to set num of queue pairs to %d, because committing RSS failed\n",
				 queue_pairs);
			return -EINVAL;
		}
		rss_indirection_table_free(&old_rss);
		goto succ;
	}

	mq = kzalloc(sizeof(*mq), GFP_KERNEL);
	if (!mq)
		return -ENOMEM;
@@ -3395,12 +3459,12 @@ static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
			 queue_pairs);
		return -EINVAL;
	} else {
	}
succ:
	vi->curr_queue_pairs = queue_pairs;
	/* virtnet_open() will refill when device is going to up. */
	if (dev->flags & IFF_UP)
		schedule_delayed_work(&vi->refill, 0);
	}

	return 0;
}
@@ -3828,11 +3892,15 @@ static bool virtnet_commit_rss_command(struct virtnet_info *vi)
	/* prepare sgs */
	sg_init_table(sgs, 4);

	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, hash_cfg_reserved);
	sg_set_buf(&sgs[0], &vi->rss, sg_buf_size);

	sg_buf_size = sizeof(uint16_t) * (vi->rss.indirection_table_mask + 1);
	if (vi->has_rss) {
		sg_buf_size = sizeof(uint16_t) * vi->rss_indir_table_size;
		sg_set_buf(&sgs[1], vi->rss.indirection_table, sg_buf_size);
	} else {
		sg_set_buf(&sgs[1], &vi->rss.hash_cfg_reserved, sizeof(uint16_t));
	}

	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
			- offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
@@ -3856,21 +3924,14 @@ static bool virtnet_commit_rss_command(struct virtnet_info *vi)

static void virtnet_init_default_rss(struct virtnet_info *vi)
{
	u32 indir_val = 0;
	int i = 0;

	vi->rss.hash_types = vi->rss_hash_types_supported;
	vi->rss_hash_types_saved = vi->rss_hash_types_supported;
	vi->rss.indirection_table_mask = vi->rss_indir_table_size
						? vi->rss_indir_table_size - 1 : 0;
	vi->rss.unclassified_queue = 0;

	for (; i < vi->rss_indir_table_size; ++i) {
		indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
		vi->rss.indirection_table[i] = indir_val;
	}
	virtnet_rss_update_by_qpairs(vi, vi->curr_queue_pairs);

	vi->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
	vi->rss.hash_key_length = vi->rss_key_size;

	netdev_rss_key_fill(vi->rss.key, vi->rss_key_size);
@@ -6420,10 +6481,19 @@ static int virtnet_probe(struct virtio_device *vdev)
			virtio_cread16(vdev, offsetof(struct virtio_net_config,
				rss_max_indirection_table_length));
	}
	err = rss_indirection_table_alloc(&vi->rss, vi->rss_indir_table_size);
	if (err)
		goto free;

	if (vi->has_rss || vi->has_rss_hash_report) {
		vi->rss_key_size =
			virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
		if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
			dev_err(&vdev->dev, "rss_max_key_size=%u exceeds the limit %u.\n",
				vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE);
			err = -EINVAL;
			goto free;
		}

		vi->rss_hash_types_supported =
		    virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
@@ -6551,6 +6621,15 @@ static int virtnet_probe(struct virtio_device *vdev)

	virtio_device_ready(vdev);

	if (vi->has_rss || vi->has_rss_hash_report) {
		if (!virtnet_commit_rss_command(vi)) {
			dev_warn(&vdev->dev, "RSS disabled because committing failed.\n");
			dev->hw_features &= ~NETIF_F_RXHASH;
			vi->has_rss_hash_report = false;
			vi->has_rss = false;
		}
	}

	virtnet_set_queues(vi, vi->curr_queue_pairs);

	/* a random MAC address has been assigned, notify the device.
@@ -6674,6 +6753,8 @@ static void virtnet_remove(struct virtio_device *vdev)

	remove_vq_common(vi);

	rss_indirection_table_free(&vi->rss);

	free_netdev(vi->dev);
}