Jason Wang
2014-Nov-17 09:17 UTC
[PATCH V3 1/2] virtio: introduce methods of sanitizing device features
Buggy host may advertised buggy host features (a usual case is that host advertise a feature whose dependencies were missed). In this case, driver should detect and disable the buggy features by itself. This patch introduces driver specific sanitize_features() method which is called just before features finalizing to detect and disable buggy features advertised by host. Virtio-net will be the first user. Cc: Rusty Russell <rusty at rustcorp.com.au> Cc: Michael S. Tsirkin <mst at redhat.com> Cc: Cornelia Huck <cornelia.huck at de.ibm.com> Cc: Wanlong Gao <gaowanlong at cn.fujitsu.com> Signed-off-by: Jason Wang <jasowang at redhat.com> --- Changes from V2: - fix typo - rename fix_features to sanitize_features --- drivers/virtio/virtio.c | 4 ++++ include/linux/virtio.h | 1 + include/linux/virtio_config.h | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index df598dd..6a86b4f 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -181,6 +181,10 @@ static int virtio_dev_probe(struct device *_d) if (device_features & (1 << i)) set_bit(i, dev->features); + /* Sanitize buggy features advertised by host */ + if (drv->sanitize_features) + drv->sanitize_features(dev); + dev->config->finalize_features(dev); err = drv->probe(dev); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 65261a7..5aed283 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -142,6 +142,7 @@ struct virtio_driver { void (*scan)(struct virtio_device *dev); void (*remove)(struct virtio_device *dev); void (*config_changed)(struct virtio_device *dev); + void (*sanitize_features)(struct virtio_device *dev); #ifdef CONFIG_PM int (*freeze)(struct virtio_device *dev); int (*restore)(struct virtio_device *dev); diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index 7f4ef66..7bd89ea 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -96,6 +96,18 @@ static inline bool virtio_has_feature(const struct virtio_device *vdev, return test_bit(fbit, vdev->features); } +static inline void virtio_disable_feature(struct virtio_device *vdev, + unsigned int fbit) +{ + BUG_ON(fbit >= VIRTIO_TRANSPORT_F_START); + BUG_ON(vdev->config->get_status(vdev) & + ~(VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)); + + virtio_check_driver_offered_feature(vdev, fbit); + + clear_bit(fbit, vdev->features); +} + static inline struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev, vq_callback_t *c, const char *n) -- 1.9.1
Jason Wang
2014-Nov-17 09:17 UTC
[PATCH V3 2/2] virtio-net: sanitize buggy features advertised by host
This patch tries to detect the possible buggy features advertised by host and sanitize them. One example is booting virtio-net with only ctrl_vq disabled, qemu may still advertise many features which depends on it. This will trigger several BUG()s in virtnet_send_command(). This patch utilizes the sanitize_features() method, and disables all features that depends on ctrl_vq if it was not advertised. This fixes the crash when booting with ctrl_vq=off using qemu. Cc: Rusty Russell <rusty at rustcorp.com.au> Cc: Michael S. Tsirkin <mst at redhat.com> Cc: Cornelia Huck <cornelia.huck at de.ibm.com> Cc: Wanlong Gao <gaowanlong at cn.fujitsu.com> Signed-off-by: Jason Wang <jasowang at redhat.com> --- Changes from V1: - fix the cut-and-paste error Changes from V2: - loop through an array of feature bits - switch to use dev_warn() --- drivers/net/virtio_net.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index ec2a8b4..6fadd8c 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -1948,6 +1948,31 @@ static int virtnet_restore(struct virtio_device *vdev) } #endif +static void virtnet_sanitize_features(struct virtio_device *dev) +{ + unsigned int features_for_ctrl_vq[] = { + VIRTIO_NET_F_CTRL_RX, + VIRTIO_NET_F_CTRL_VLAN, + VIRTIO_NET_F_GUEST_ANNOUNCE, + VIRTIO_NET_F_MQ, + VIRTIO_NET_F_CTRL_MAC_ADDR + }; + int i; + + if (!virtio_has_feature(dev, VIRTIO_NET_F_CTRL_VQ)) { + for (i = 0; i < ARRAY_SIZE(features_for_ctrl_vq); i++) { + unsigned int f = features_for_ctrl_vq[i]; + if (virtio_has_feature(dev, f)) { + virtio_disable_feature(dev, f); + dev_warn(&dev->dev, + "buggy hyperviser: disable feature " + "0x%x since VIRTIO_NET_F_CTRL_VQ was " + "not advertised.\n", f); + } + } + } +} + static struct virtio_device_id id_table[] = { { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, { 0 }, @@ -1975,6 +2000,7 @@ static struct virtio_driver virtio_net_driver = { .probe = virtnet_probe, .remove = virtnet_remove, .config_changed = virtnet_config_changed, + .sanitize_features = virtnet_sanitize_features, #ifdef CONFIG_PM_SLEEP .freeze = virtnet_freeze, .restore = virtnet_restore, -- 1.9.1
Michael S. Tsirkin
2014-Nov-17 09:37 UTC
[PATCH V3 1/2] virtio: introduce methods of sanitizing device features
On Mon, Nov 17, 2014 at 05:17:17PM +0800, Jason Wang wrote:> Buggy host may advertised buggy host features (a usual case is that host > advertise a feature whose dependencies were missed). In this case, driver > should detect and disable the buggy features by itself. > > This patch introduces driver specific sanitize_features() method which is > called just before features finalizing to detect and disable buggy features > advertised by host. > > Virtio-net will be the first user. > > Cc: Rusty Russell <rusty at rustcorp.com.au> > Cc: Michael S. Tsirkin <mst at redhat.com> > Cc: Cornelia Huck <cornelia.huck at de.ibm.com> > Cc: Wanlong Gao <gaowanlong at cn.fujitsu.com> > Signed-off-by: Jason Wang <jasowang at redhat.com>Hmm this conflicts with virtio 1.0 work: we drop features as bitmap there.> --- > Changes from V2: > - fix typo > - rename fix_features to sanitize_features > --- > drivers/virtio/virtio.c | 4 ++++ > include/linux/virtio.h | 1 + > include/linux/virtio_config.h | 12 ++++++++++++ > 3 files changed, 17 insertions(+) > > diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c > index df598dd..6a86b4f 100644 > --- a/drivers/virtio/virtio.c > +++ b/drivers/virtio/virtio.c > @@ -181,6 +181,10 @@ static int virtio_dev_probe(struct device *_d) > if (device_features & (1 << i)) > set_bit(i, dev->features); > > + /* Sanitize buggy features advertised by host */ > + if (drv->sanitize_features) > + drv->sanitize_features(dev); > + > dev->config->finalize_features(dev); > > err = drv->probe(dev); > diff --git a/include/linux/virtio.h b/include/linux/virtio.h > index 65261a7..5aed283 100644 > --- a/include/linux/virtio.h > +++ b/include/linux/virtio.h > @@ -142,6 +142,7 @@ struct virtio_driver { > void (*scan)(struct virtio_device *dev); > void (*remove)(struct virtio_device *dev); > void (*config_changed)(struct virtio_device *dev); > + void (*sanitize_features)(struct virtio_device *dev); > #ifdef CONFIG_PM > int (*freeze)(struct virtio_device *dev); > int (*restore)(struct virtio_device *dev); > diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h > index 7f4ef66..7bd89ea 100644 > --- a/include/linux/virtio_config.h > +++ b/include/linux/virtio_config.h > @@ -96,6 +96,18 @@ static inline bool virtio_has_feature(const struct virtio_device *vdev, > return test_bit(fbit, vdev->features); > } > > +static inline void virtio_disable_feature(struct virtio_device *vdev, > + unsigned int fbit) > +{ > + BUG_ON(fbit >= VIRTIO_TRANSPORT_F_START); > + BUG_ON(vdev->config->get_status(vdev) & > + ~(VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER)); > + > + virtio_check_driver_offered_feature(vdev, fbit); > + > + clear_bit(fbit, vdev->features); > +} > + > static inline > struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev, > vq_callback_t *c, const char *n) > -- > 1.9.1
Cornelia Huck
2014-Nov-17 09:44 UTC
[PATCH V3 1/2] virtio: introduce methods of sanitizing device features
On Mon, 17 Nov 2014 11:37:01 +0200 "Michael S. Tsirkin" <mst at redhat.com> wrote:> On Mon, Nov 17, 2014 at 05:17:17PM +0800, Jason Wang wrote: > > Buggy host may advertised buggy host features (a usual case is that host > > advertise a feature whose dependencies were missed). In this case, driver > > should detect and disable the buggy features by itself. > > > > This patch introduces driver specific sanitize_features() method which is > > called just before features finalizing to detect and disable buggy features > > advertised by host. > > > > Virtio-net will be the first user. > > > > Cc: Rusty Russell <rusty at rustcorp.com.au> > > Cc: Michael S. Tsirkin <mst at redhat.com> > > Cc: Cornelia Huck <cornelia.huck at de.ibm.com> > > Cc: Wanlong Gao <gaowanlong at cn.fujitsu.com> > > Signed-off-by: Jason Wang <jasowang at redhat.com> > > Hmm this conflicts with virtio 1.0 work: we drop > features as bitmap there.But that's an implementation detail, no? We'll still need a way for the driver to sanitize features, and I think this interface works just fine.
Michael S. Tsirkin
2014-Nov-17 10:08 UTC
[PATCH V3 2/2] virtio-net: sanitize buggy features advertised by host
On Mon, Nov 17, 2014 at 05:17:18PM +0800, Jason Wang wrote:> This patch tries to detect the possible buggy features advertised by host > and sanitize them. One example is booting virtio-net with only ctrl_vq > disabled, qemu may still advertise many features which depends on it. This > will trigger several BUG()s in virtnet_send_command(). > > This patch utilizes the sanitize_features() method, and disables all > features that depends on ctrl_vq if it was not advertised. > > This fixes the crash when booting with ctrl_vq=off using qemu. > > Cc: Rusty Russell <rusty at rustcorp.com.au> > Cc: Michael S. Tsirkin <mst at redhat.com> > Cc: Cornelia Huck <cornelia.huck at de.ibm.com> > Cc: Wanlong Gao <gaowanlong at cn.fujitsu.com> > Signed-off-by: Jason Wang <jasowang at redhat.com>So I'm not sure this is useful. The spec says: The device MUST NOT offer a feature which requires another feature which was not offered. So this is a buggy hypervisor, and I believe we should just fail probe. This can be done without crashing, and is generally a better idea that second-guessing what hypervisor wants us to do. However, assuming that we do want this change: This can be replaced with a table driven design in virtio core, but since you chose to open code it, I would drop table below altogether. Just make it if (!virtio_has_feature(dev, VIRTIO_NET_F_CTRL_VQ)) { virtio_disable_feature(dev, VIRTIO_NET_F_CTRL_RX); virtio_disable_feature(dev, VIRTIO_NET_F_CTRL_VLAN); virtio_disable_feature(dev, VIRTIO_NET_F_GUEST_ANNOUNCE); virtio_disable_feature(dev, VIRTIO_NET_F_MQ); virtio_disable_feature(dev, VIRTIO_NET_F_CTRL_MAC_ADDR); }> --- > Changes from V1: > - fix the cut-and-paste error > Changes from V2: > - loop through an array of feature bits > - switch to use dev_warn() > --- > drivers/net/virtio_net.c | 26 ++++++++++++++++++++++++++ > 1 file changed, 26 insertions(+) > > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c > index ec2a8b4..6fadd8c 100644 > --- a/drivers/net/virtio_net.c > +++ b/drivers/net/virtio_net.c > @@ -1948,6 +1948,31 @@ static int virtnet_restore(struct virtio_device *vdev) > } > #endif > > +static void virtnet_sanitize_features(struct virtio_device *dev) > +{ > + unsigned int features_for_ctrl_vq[] = { > + VIRTIO_NET_F_CTRL_RX, > + VIRTIO_NET_F_CTRL_VLAN, > + VIRTIO_NET_F_GUEST_ANNOUNCE, > + VIRTIO_NET_F_MQ, > + VIRTIO_NET_F_CTRL_MAC_ADDR > + };This is not the only dependency: checksums have dependencies too. See virtio 1.0 spec.> + int i; > + > + if (!virtio_has_feature(dev, VIRTIO_NET_F_CTRL_VQ)) { > + for (i = 0; i < ARRAY_SIZE(features_for_ctrl_vq); i++) { > + unsigned int f = features_for_ctrl_vq[i]; > + if (virtio_has_feature(dev, f)) { > + virtio_disable_feature(dev, f); > + dev_warn(&dev->dev, > + "buggy hyperviser: disable feature " > + "0x%x since VIRTIO_NET_F_CTRL_VQ was " > + "not advertised.\n", f); > + } > + } > + } > +} > + > static struct virtio_device_id id_table[] = { > { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, > { 0 }, > @@ -1975,6 +2000,7 @@ static struct virtio_driver virtio_net_driver = { > .probe = virtnet_probe, > .remove = virtnet_remove, > .config_changed = virtnet_config_changed, > + .sanitize_features = virtnet_sanitize_features, > #ifdef CONFIG_PM_SLEEP > .freeze = virtnet_freeze, > .restore = virtnet_restore, > -- > 1.9.1
Apparently Analagous Threads
- [PATCH V3 1/2] virtio: introduce methods of sanitizing device features
- [PATCH V3 2/2] virtio-net: sanitize buggy features advertised by host
- [PATCH V3 2/2] virtio-net: sanitize buggy features advertised by host
- [PATCH V3 2/2] virtio-net: sanitize buggy features advertised by host
- [PATCH V3 2/2] virtio-net: sanitize buggy features advertised by host