Skip to contentSkip to conversation

virtio_net: Improve RSS key size validation

PATCH Superseded Srujana Challa 2 participants 3 messages Feb 12 – Feb 13

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 virtio_net: Improve RSS key size validation

Replace hardcoded RSS max key size limit with a type based definition. Add validation for RSS key size against spec minimum (40 bytes). When validation fails, gracefully disable RSS features and continue initialization rather than failing completely.

Fixes 3f7d9c1964fc ("virtio_net: Add hash_key_length check")

v3:
- Moved RSS key validation checks to virtnet_validate.
- Add fixes: tag and CC -stable

---
 drivers/net/virtio_net.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +21 -8
index db88dcaefb20..e61cea50dcab 100644
@@ -381,7 +381,9 @@ struct receive_queue {
 	struct xdp_buff **xsk_buffs;
 };
 
-#define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
+#define VIRTIO_NET_RSS_MAX_KEY_SIZE \
+	(type_max(((struct virtio_net_config *)0)->rss_max_key_size) + 1)
+#define VIRTIO_NET_RSS_MIN_KEY_SIZE 40
 
 /* Control VQ buffers: protected by the rtnl lock */
 struct control_buf {
@@ -6627,6 +6629,24 @@ static int virtnet_validate(struct virtio_device *vdev)
 		__virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
 	}
 
+	if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS) ||
+	    virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
+		u8 key_sz = virtio_cread8(vdev,
+					  offsetof(struct virtio_net_config,
+						   rss_max_key_size));
+		/* Spec requires at least 40 bytes */
+		if (key_sz < VIRTIO_NET_RSS_MIN_KEY_SIZE) {
+			dev_warn(&vdev->dev,
+				 "rss_max_key_size=%u is less than spec minimum %u, disabling RSS\n",
+				 key_sz, VIRTIO_NET_RSS_MIN_KEY_SIZE);
+			if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
+				__virtio_clear_bit(vdev, VIRTIO_NET_F_RSS);
+			if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
+				__virtio_clear_bit(vdev,
+						   VIRTIO_NET_F_HASH_REPORT);
+		}
+	}
+
 	return 0;
 }
 
@@ -6839,13 +6859,6 @@ static int virtnet_probe(struct virtio_device *vdev)
 	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));
 		vi->rss_hash_types_supported &=
Signature · 1 line
2.25.1
#1 lore raw
Quoted 21 lines · Srujana Challa

Replace hardcoded RSS max key size limit with a type based definition. Add validation for RSS key size against spec minimum (40 bytes). When validation fails, gracefully disable RSS features and continue initialization rather than failing completely.

Fixes 3f7d9c1964fc ("virtio_net: Add hash_key_length check")

v3:
- Moved RSS key validation checks to virtnet_validate.
- Add fixes: tag and CC -stable

---
 drivers/net/virtio_net.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c +0 -0
index db88dcaefb20..e61cea50dcab 100644

drivers/net/virtio_net.c struct receive_queue { · line 385

Quoted 6 lines
drivers/net/virtio_net.c +2 -1
@@ -381,7 +381,9 @@ struct receive_queue {
 	struct xdp_buff **xsk_buffs;
 };
 
-#define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
+#define VIRTIO_NET_RSS_MAX_KEY_SIZE \
+	(type_max(((struct virtio_net_config *)0)->rss_max_key_size) + 1)

+1 here really unintuitive.
It does not look like it's still used, though?

Quoted 5 lines
drivers/net/virtio_net.c +1 -0
+#define VIRTIO_NET_RSS_MIN_KEY_SIZE 40
 
 /* Control VQ buffers: protected by the rtnl lock */
 struct control_buf {

drivers/net/virtio_net.c static int virtnet_validate(struct virtio_device *vdev) · line 6637

Quoted 9 lines
drivers/net/virtio_net.c +6 -0
@@ -6627,6 +6629,24 @@ static int virtnet_validate(struct virtio_device *vdev)
 		__virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
 	}
 
+	if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS) ||
+	    virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
+		u8 key_sz = virtio_cread8(vdev,
+					  offsetof(struct virtio_net_config,
+						   rss_max_key_size));
+		/* Spec requires at least 40 bytes */

move the define here then?

drivers/net/virtio_net.c static int virtnet_validate(struct virtio_device *vdev) · line 6646

Quoted 9 lines
drivers/net/virtio_net.c +9 -0
+		if (key_sz < VIRTIO_NET_RSS_MIN_KEY_SIZE) {
+			dev_warn(&vdev->dev,
+				 "rss_max_key_size=%u is less than spec minimum %u, disabling RSS\n",
+				 key_sz, VIRTIO_NET_RSS_MIN_KEY_SIZE);
+			if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
+				__virtio_clear_bit(vdev, VIRTIO_NET_F_RSS);
+			if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
+				__virtio_clear_bit(vdev,
+						   VIRTIO_NET_F_HASH_REPORT);

why not clear them unconditionally?

Quoted 22 lines
drivers/net/virtio_net.c +3 -7
+		}
+	}
+
 	return 0;
 }
 
@@ -6839,13 +6859,6 @@ static int virtnet_probe(struct virtio_device *vdev)
 	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));
 		vi->rss_hash_types_supported &=
Signature · 1 line
2.25.1
#2 lore raw
Quoted 25 lines · Srujana Challa
Quoted 24 lines

Replace hardcoded RSS max key size limit with a type based definition. Add validation for RSS key size against spec minimum (40 bytes). When validation fails, gracefully disable RSS features and continue initialization rather than failing completely.

Fixes 3f7d9c1964fc ("virtio_net: Add hash_key_length check")

v3:
- Moved RSS key validation checks to virtnet_validate.
- Add fixes: tag and CC -stable

---
 drivers/net/virtio_net.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
drivers/net/virtio_net.c index renamed from drivers/net/virtio_net.c +0 -0

db88dcaefb20..e61cea50dcab 100644

drivers/net/virtio_net.c +0 -0
@@ -381,7 +381,9 @@ struct receive_queue {
 	struct xdp_buff **xsk_buffs;
 };
 
Quoted 6 lines

drivers/net/virtio_net.c struct receive_queue { · line 385

Quoted 3 lines
drivers/net/virtio_net.c +2 -1
-#define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
+#define VIRTIO_NET_RSS_MAX_KEY_SIZE \
+	(type_max(((struct virtio_net_config *)0)->rss_max_key_size) + 1)

+1 here really unintuitive.
It does not look like it's still used, though?

It is still used for the rss_hash_key_data[VIRTIO_NET_RSS_MAX_KEY_SIZE] in struct virtnet_info. Should I change it to use NETDEV_RSS_KEY_LEN?

Quoted 10 lines
Quoted 10 lines
drivers/net/virtio_net.c +1 -0
+#define VIRTIO_NET_RSS_MIN_KEY_SIZE 40
 
 /* Control VQ buffers: protected by the rtnl lock */  struct

control_buf { @@ -6627,6 +6629,24 @@ static int
virtnet_validate(struct virtio_device *vdev)

drivers/net/virtio_net.c +0 -0
 		__virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
 	}
Quoted 8 lines

drivers/net/virtio_net.c static int virtnet_validate(struct virtio_device *vdev) · line 6637

Quoted 6 lines
drivers/net/virtio_net.c +6 -0
+	if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS) ||
+	    virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT)) {
+		u8 key_sz = virtio_cread8(vdev,
+					  offsetof(struct virtio_net_config,
+						   rss_max_key_size));
+		/* Spec requires at least 40 bytes */

move the define here then?

Will move.

Quoted 44 lines

drivers/net/virtio_net.c static int virtnet_validate(struct virtio_device *vdev)

Quoted 4 lines
drivers/net/virtio_net.c +3 -0
+		if (key_sz < VIRTIO_NET_RSS_MIN_KEY_SIZE) {
+			dev_warn(&vdev->dev,
+				 "rss_max_key_size=%u is less than spec

minimum %u, disabling RSS\n",

drivers/net/virtio_net.c static int virtnet_validate(struct virtio_device *vdev)

Quoted 4 lines
drivers/net/virtio_net.c +4 -0
+				 key_sz, VIRTIO_NET_RSS_MIN_KEY_SIZE);
+			if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
+				__virtio_clear_bit(vdev, VIRTIO_NET_F_RSS);
+			if (virtio_has_feature(vdev,

VIRTIO_NET_F_HASH_REPORT))

drivers/net/virtio_net.c static int virtnet_validate(struct virtio_device *vdev) · line 6649

Quoted 2 lines
drivers/net/virtio_net.c +2 -0
+				__virtio_clear_bit(vdev,
+

VIRTIO_NET_F_HASH_REPORT);

why not clear them unconditionally?

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device

Quoted 7 lines
drivers/net/virtio_net.c +3 -0
+		}
+	}
+
 	return 0;
 }
@@ -6839,13 +6859,6 @@ static int virtnet_probe(struct virtio_device

*vdev)

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev)

Quoted 3 lines
drivers/net/virtio_net.c +0 -0
 	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));

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device · line 6843

Quoted 2 lines
drivers/net/virtio_net.c +0 -2
-		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",

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device · line 6844

Quoted 1 line
drivers/net/virtio_net.c +0 -1
-				vi->rss_key_size,

VIRTIO_NET_RSS_MAX_KEY_SIZE);

drivers/net/virtio_net.c static int virtnet_probe(struct virtio_device *vdev)

Quoted 6 lines
drivers/net/virtio_net.c +0 -4
-			err = -EINVAL;
-			goto free;
-		}
-
 		vi->rss_hash_types_supported =
 		    virtio_cread32(vdev, offsetof(struct virtio_net_config,

supported_hash_types));

Quoted 3 lines
drivers/net/virtio_net.c +0 -1
 		vi->rss_hash_types_supported &=
--

2.25.1