Skip to contentSkip to conversation

virtio: allow drivers to validate features

PATCH In review Michael S. Tsirkin 3 participants 8 messages Mar 29, 2017 – Mar 31, 2017

Subsystems (4)
lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

PATCH 1/2 virtio: allow drivers to validate features

Some drivers can't support all features in all configurations. At the moment we blindly set FEATURES_OK and later FAILED. Support this better by adding a callback drivers can use to do some early checks.

---
 drivers/virtio/virtio.c | 6 ++++++
 include/linux/virtio.h  | 1 +
 2 files changed, 7 insertions(+)
drivers/virtio/virtio.c +6 -0
index 400d70b..48230a5 100644
@@ -232,6 +232,12 @@ static int virtio_dev_probe(struct device *_d)
 		if (device_features & (1ULL << i))
 			__virtio_set_bit(dev, i);
 
+	if (drv->validate) {
+		err = drv->validate(dev);
+		if (err)
+			goto err;
+	}
+
 	err = virtio_finalize_features(dev);
 	if (err)
 		goto err;
include/linux/virtio.h +1 -0
index 193fea9..ed04753 100644
@@ -176,6 +176,7 @@ struct virtio_driver {
 	unsigned int feature_table_size;
 	const unsigned int *feature_table_legacy;
 	unsigned int feature_table_size_legacy;
+	int (*validate)(struct virtio_device *dev);
 	int (*probe)(struct virtio_device *dev);
 	void (*scan)(struct virtio_device *dev);
 	void (*remove)(struct virtio_device *dev);
Signature · 1 line
MST
author maintainer
#1 lore raw

PATCH 2/2 virtio_net: clear MTU when out of range

virtio attempts to clear the MTU feature bit if the value is out of the supported range, but this has no real effect since FEATURES_OK has already been set.

Fix this up by checking the MTU in the new validate callback.

Fixes 14de9d114a82 ("virtio-net: Add initial MTU advice feature")
---
 drivers/net/virtio_net.c | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)
drivers/net/virtio_net.c +30 -11
index f6a379d..6aba098 100644
@@ -2312,14 +2312,8 @@ static bool virtnet_validate_features(struct virtio_device *vdev)
 #define MIN_MTU ETH_MIN_MTU
 #define MAX_MTU ETH_MAX_MTU
 
-static int virtnet_probe(struct virtio_device *vdev)
+static int virtnet_validate(struct virtio_device *vdev)
 {
-	int i, err;
-	struct net_device *dev;
-	struct virtnet_info *vi;
-	u16 max_queue_pairs;
-	int mtu;
-
 	if (!vdev->config->get) {
 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
 			__func__);
@@ -2329,6 +2323,25 @@ static int virtnet_probe(struct virtio_device *vdev)
 	if (!virtnet_validate_features(vdev))
 		return -EINVAL;
 
+	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
+		int mtu = virtio_cread16(vdev,
+					 offsetof(struct virtio_net_config,
+						  mtu));
+		if (mtu < MIN_MTU)
+			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
+	}
+
+	return 0;
+}
+
+static int virtnet_probe(struct virtio_device *vdev)
+{
+	int i, err;
+	struct net_device *dev;
+	struct virtnet_info *vi;
+	u16 max_queue_pairs;
+	int mtu;
+
 	/* Find if host supports multiqueue virtio_net device */
 	err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
 				   struct virtio_net_config,
@@ -2444,12 +2457,17 @@ static int virtnet_probe(struct virtio_device *vdev)
 				     offsetof(struct virtio_net_config,
 					      mtu));
 		if (mtu < dev->min_mtu) {
-			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
-		} else {
-			dev->mtu = mtu;
-			dev->max_mtu = mtu;
+			/* Should never trigger: MTU was previously validated
+			 * in virtnet_validate.
+			 */
+			dev_err(&vdev->dev, "device MTU appears to have changed "
+				"it is now %d < %d", mtu, dev->min_mtu);
+			goto free_stats;
 		}
 
+		dev->mtu = mtu;
+		dev->max_mtu = mtu;
+
 		/* TODO: size buffers correctly in this case. */
 		if (dev->mtu > ETH_DATA_LEN)
 			vi->big_packets = true;
@@ -2630,6 +2648,7 @@ static struct virtio_driver virtio_net_driver = {
 	.driver.name =	KBUILD_MODNAME,
 	.driver.owner =	THIS_MODULE,
 	.id_table =	id_table,
+	.validate =	virtnet_validate,
 	.probe =	virtnet_probe,
 	.remove =	virtnet_remove,
 	.config_changed = virtnet_config_changed,
Signature · 1 line
MST
#2 lore raw
In reply to Michael S. Tsirkin
Quoted 3 lines · On Wed, 29 Mar 2017 20:14:44 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Some drivers can't support all features in all configurations. At the moment we blindly set FEATURES_OK and later FAILED. Support this better by adding a callback drivers can use to do some early checks.

Looks reasonable. Do we need to document that the driver must not do anything beyond dealing with features and reading the config space that early?

Quoted 29 lines
---
 drivers/virtio/virtio.c | 6 ++++++
 include/linux/virtio.h  | 1 +
 2 files changed, 7 insertions(+)
drivers/virtio/virtio.c +6 -0
index 400d70b..48230a5 100644
@@ -232,6 +232,12 @@ static int virtio_dev_probe(struct device *_d)
 		if (device_features & (1ULL << i))
 			__virtio_set_bit(dev, i);
 
+	if (drv->validate) {
+		err = drv->validate(dev);
+		if (err)
+			goto err;
+	}
+
 	err = virtio_finalize_features(dev);
 	if (err)
 		goto err;
include/linux/virtio.h +0 -0
index 193fea9..ed04753 100644

include/linux/virtio.h struct virtio_driver { · line 182

Quoted 7 lines
include/linux/virtio.h +1 -0
@@ -176,6 +176,7 @@ struct virtio_driver {
 	unsigned int feature_table_size;
 	const unsigned int *feature_table_legacy;
 	unsigned int feature_table_size_legacy;
+	int (*validate)(struct virtio_device *dev);
 	int (*probe)(struct virtio_device *dev);
 	void (*scan)(struct virtio_device *dev);
 	void (*remove)(struct virtio_device *dev);

Would be good to add some doc; but other members are undocumented here already...

#4 lore raw
In reply to Michael S. Tsirkin
Quoted 3 lines · On Wed, 29 Mar 2017 20:14:44 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Some drivers can't support all features in all configurations. At the moment we blindly set FEATURES_OK and later FAILED. Support this better by adding a callback drivers can use to do some early checks.

Looks reasonable. Do we need to document that the driver must not do anything beyond dealing with features and reading the config space that early?

Quoted 29 lines
---
 drivers/virtio/virtio.c | 6 ++++++
 include/linux/virtio.h  | 1 +
 2 files changed, 7 insertions(+)
drivers/virtio/virtio.c +6 -0
index 400d70b..48230a5 100644
@@ -232,6 +232,12 @@ static int virtio_dev_probe(struct device *_d)
 		if (device_features & (1ULL << i))
 			__virtio_set_bit(dev, i);
 
+	if (drv->validate) {
+		err = drv->validate(dev);
+		if (err)
+			goto err;
+	}
+
 	err = virtio_finalize_features(dev);
 	if (err)
 		goto err;
include/linux/virtio.h +0 -0
index 193fea9..ed04753 100644

include/linux/virtio.h struct virtio_driver { · line 182

Quoted 7 lines
include/linux/virtio.h +1 -0
@@ -176,6 +176,7 @@ struct virtio_driver {
 	unsigned int feature_table_size;
 	const unsigned int *feature_table_legacy;
 	unsigned int feature_table_size_legacy;
+	int (*validate)(struct virtio_device *dev);
 	int (*probe)(struct virtio_device *dev);
 	void (*scan)(struct virtio_device *dev);
 	void (*remove)(struct virtio_device *dev);

Would be good to add some doc; but other members are undocumented here already...

author maintainer
#3 lore raw
In reply to Cornelia Huck
Quoted 10 lines · Cornelia Huck
Quoted 3 lines · On Wed, 29 Mar 2017 20:14:44 +0300 "Michael S. Tsirkin" <[email protected]> wrote:

Some drivers can't support all features in all configurations. At the moment we blindly set FEATURES_OK and later FAILED. Support this better by adding a callback drivers can use to do some early checks.

Looks reasonable. Do we need to document that the driver must not do anything beyond dealing with features and reading the config space that early?

It's up to the driver - we probably should document that on failure neither probe nor remove will be called. On success we proceed to probe.

Quoted 31 lines
Quoted 31 lines
---
 drivers/virtio/virtio.c | 6 ++++++
 include/linux/virtio.h  | 1 +
 2 files changed, 7 insertions(+)
drivers/virtio/virtio.c +6 -0
index 400d70b..48230a5 100644
@@ -232,6 +232,12 @@ static int virtio_dev_probe(struct device *_d)
 		if (device_features & (1ULL << i))
 			__virtio_set_bit(dev, i);
 
+	if (drv->validate) {
+		err = drv->validate(dev);
+		if (err)
+			goto err;
+	}
+
 	err = virtio_finalize_features(dev);
 	if (err)
 		goto err;
include/linux/virtio.h +0 -0
index 193fea9..ed04753 100644
@@ -176,6 +176,7 @@ struct virtio_driver {
 	unsigned int feature_table_size;
 	const unsigned int *feature_table_legacy;
Quoted 8 lines

include/linux/virtio.h struct virtio_driver { · line 182

Quoted 5 lines
include/linux/virtio.h +1 -0
 	unsigned int feature_table_size_legacy;
+	int (*validate)(struct virtio_device *dev);
 	int (*probe)(struct virtio_device *dev);
 	void (*scan)(struct virtio_device *dev);
 	void (*remove)(struct virtio_device *dev);

Would be good to add some doc; but other members are undocumented here already...

True. Patches welcome.

maintainer
#5 lore raw
In reply to Michael S. Tsirkin

From: "Michael S. Tsirkin" <[email protected]>
Date: Wed, 29 Mar 2017 20:14:44 +0300

Quoted 5 lines

Some drivers can't support all features in all configurations. At the moment we blindly set FEATURES_OK and later FAILED. Support this better by adding a callback drivers can use to do some early checks.

Michael do you want me to take these virtio networking fixes into my tree directly or are you going to send me a pull request or something after it all settles down?

Thanks.

author maintainer
#6 lore raw
Quoted 7 lines · David Miller

From: "Michael S. Tsirkin" <[email protected]>
Date: Wed, 29 Mar 2017 20:14:44 +0300

Quoted 4 lines

Some drivers can't support all features in all configurations. At the moment we blindly set FEATURES_OK and later FAILED. Support this better by adding a callback drivers can use to do some early checks.

Quoted 7 lines
Quoted 1 line

Michael do you want me to take these virtio networking fixes into my tree directly or are you going to send me a pull request or something after it all settles down?

Thanks.

I think I'll send a pull request.

Thanks,

Signature · 1 line
MST
author maintainer
#7 lore raw
In reply to David Miller
Quoted 7 lines · David Miller

From: "Michael S. Tsirkin" <[email protected]>
Date: Wed, 29 Mar 2017 20:14:44 +0300

Quoted 4 lines

Some drivers can't support all features in all configurations. At the moment we blindly set FEATURES_OK and later FAILED. Support this better by adding a callback drivers can use to do some early checks.

Quoted 7 lines
Quoted 1 line

Michael do you want me to take these virtio networking fixes into my tree directly or are you going to send me a pull request or something after it all settles down?

Thanks.

I think I'll send a pull request.

Thanks,

Signature · 1 line
MST