Skip to contentSkip to conversation

virtio: add API to detect legacy devices

RFC PATCH Superseded Michael S. Tsirkin 2 participants 9 messages Dec 4, 2014

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Michael S. Tsirkin

author maintainer
#0 lore raw

RFC PATCH 1/3 virtio: add API to detect legacy devices

transports need to be able to detect legacy-only
devices (ATM balloon only) to use legacy path
to drive them.

Add a core API to do just that.
The implementation just blacklists balloon:
not too pretty, but let's not over-engineer.

---
 include/linux/virtio.h  | 2 ++
 drivers/virtio/virtio.c | 6 ++++++
 2 files changed, 8 insertions(+)
include/linux/virtio.h +2 -0
index 2bbf626..d666bcb 100644
@@ -108,6 +108,8 @@ struct virtio_device {
 	void *priv;
 };
 
+bool virtio_device_is_legacy_only(struct virtio_device_id id);
+
 static inline struct virtio_device *dev_to_virtio(struct device *_dev)
 {
 	return container_of(_dev, struct virtio_device, dev);
drivers/virtio/virtio.c +6 -0
index fa6b75d..6b4c1113 100644
@@ -267,6 +267,12 @@ static struct bus_type virtio_bus = {
 	.remove = virtio_dev_remove,
 };
 
+bool virtio_device_is_legacy_only(struct virtio_device_id id)
+{
+	return id.device == VIRTIO_ID_BALLOON;
+}
+EXPORT_SYMBOL_GPL(register_virtio_driver);
+
 int register_virtio_driver(struct virtio_driver *driver)
 {
 	/* Catch this early. */
Signature · 1 line
MST
author maintainer
#1 lore raw

RFC PATCH 2/3 virtio_ccw: legacy: don't negotiate rev 1/features

Legacy balloon device doesn't pretend to support revision 1 or 64 bit features.

But just in case someone implements a broken one that does, let's not even try to drive legacy only devices using revision 1, and let's not give them a chance to say they support VIRTIO_F_VERSION_1 by not reading high feature bits.

---
 drivers/s390/kvm/virtio_ccw.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/s390/kvm/virtio_ccw.c +10 -3
index 4a3e6e5..088bdf1 100644
@@ -733,6 +733,9 @@ static u64 virtio_ccw_get_features(struct virtio_device *vdev)
 
 	rc = le32_to_cpu(features->features);
 
+	if (vcdev->revision == 0)
+		goto out_free;
+
 	/* Read second half of the feature bits from the host. */
 	features->index = 1;
 	ccw->cmd_code = CCW_CMD_READ_FEAT;
@@ -1182,9 +1185,13 @@ static int virtio_ccw_online(struct ccw_device *cdev)
 	vcdev->vdev.id.vendor = cdev->id.cu_type;
 	vcdev->vdev.id.device = cdev->id.cu_model;
 
-	ret = virtio_ccw_set_transport_rev(vcdev);
-	if (ret)
-		goto out_free;
+	if (virtio_device_is_legacy_only(vcdev->vdev.id)) {
+		ret = virtio_ccw_set_transport_rev(vcdev);
+		if (ret)
+			goto out_free;
+	} else {
+		vcdev->revision = 0;
+	}
 
 	ret = register_virtio_device(&vcdev->vdev);
 	if (ret) {
Signature · 1 line
MST
#2 lore raw

Re: [PATCH RFC 2/3] virtio_ccw: legacy: don't negotiate rev 1/features

Quoted 28 lines · On Thu, 4 Dec 2014 19:26:45 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

Legacy balloon device doesn't pretend to support revision 1 or 64 bit features.

But just in case someone implements a broken one that does, let's not even try to drive legacy only devices using revision 1, and let's not give them a chance to say they support VIRTIO_F_VERSION_1 by not reading high feature bits.

---
 drivers/s390/kvm/virtio_ccw.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/s390/kvm/virtio_ccw.c +3 -0
index 4a3e6e5..088bdf1 100644
@@ -733,6 +733,9 @@ static u64 virtio_ccw_get_features(struct virtio_device *vdev)
 
 	rc = le32_to_cpu(features->features);
 
+	if (vcdev->revision == 0)
+		goto out_free;
+
 	/* Read second half of the feature bits from the host. */
 	features->index = 1;
 	ccw->cmd_code = CCW_CMD_READ_FEAT;

drivers/s390/kvm/virtio_ccw.c static int virtio_ccw_online(struct ccw_device *cdev) · line 1188

Quoted 7 lines
drivers/s390/kvm/virtio_ccw.c +1 -3
@@ -1182,9 +1185,13 @@ static int virtio_ccw_online(struct ccw_device *cdev)
 	vcdev->vdev.id.vendor = cdev->id.cu_type;
 	vcdev->vdev.id.device = cdev->id.cu_model;
 
-	ret = virtio_ccw_set_transport_rev(vcdev);
-	if (ret)
-		goto out_free;
+	if (virtio_device_is_legacy_only(vcdev->vdev.id)) {

Inverted logic?

Quoted 9 lines
drivers/s390/kvm/virtio_ccw.c +6 -0
+		ret = virtio_ccw_set_transport_rev(vcdev);
+		if (ret)
+			goto out_free;
+	} else {
+		vcdev->revision = 0;
+	}
 
 	ret = register_virtio_device(&vcdev->vdev);
 	if (ret) {
author maintainer
#3 lore raw

Re: [PATCH RFC 2/3] virtio_ccw: legacy: don't negotiate rev 1/features

Quoted 34 lines · Cornelia Huck
Quoted 31 lines · On Thu, 4 Dec 2014 19:26:45 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

Legacy balloon device doesn't pretend to support revision 1 or 64 bit features.

But just in case someone implements a broken one that does, let's not even try to drive legacy only devices using revision 1, and let's not give them a chance to say they support VIRTIO_F_VERSION_1 by not reading high feature bits.

---
 drivers/s390/kvm/virtio_ccw.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/s390/kvm/virtio_ccw.c +3 -0
index 4a3e6e5..088bdf1 100644
@@ -733,6 +733,9 @@ static u64 virtio_ccw_get_features(struct virtio_device *vdev)
 
 	rc = le32_to_cpu(features->features);
 
+	if (vcdev->revision == 0)
+		goto out_free;
+
 	/* Read second half of the feature bits from the host. */
 	features->index = 1;
 	ccw->cmd_code = CCW_CMD_READ_FEAT;
@@ -1182,9 +1185,13 @@ static int virtio_ccw_online(struct ccw_device *cdev)
 	vcdev->vdev.id.vendor = cdev->id.cu_type;
 	vcdev->vdev.id.device = cdev->id.cu_model;
 
Quoted 6 lines

drivers/s390/kvm/virtio_ccw.c static int virtio_ccw_online(struct ccw_device *cdev) · line 1188

Quoted 4 lines
drivers/s390/kvm/virtio_ccw.c +1 -3
-	ret = virtio_ccw_set_transport_rev(vcdev);
-	if (ret)
-		goto out_free;
+	if (virtio_device_is_legacy_only(vcdev->vdev.id)) {

Inverted logic?

Ugh, clearly.

Quoted 9 lines
Quoted 9 lines
drivers/s390/kvm/virtio_ccw.c +6 -0
+		ret = virtio_ccw_set_transport_rev(vcdev);
+		if (ret)
+			goto out_free;
+	} else {
+		vcdev->revision = 0;
+	}
 
 	ret = register_virtio_device(&vcdev->vdev);
 	if (ret) {
author maintainer
#4 lore raw

RFC PATCH 3/3 virtio_ccw: rev 1 devices set VIRTIO_F_VERSION_1

In reply to Michael S. Tsirkin

What does it mean if rev 1 device does not set
VIRTIO_F_VERSION_1? E.g. is it native endian?

Let's not even try to drive such devices:
skip attempts to finalize features or set status.
virtio core will detect this and bail out.

---
 drivers/s390/kvm/virtio_ccw.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/s390/kvm/virtio_ccw.c +15 -0
index 088bdf1..0ceeb25 100644
@@ -65,6 +65,7 @@ struct virtio_ccw_device {
 	bool is_thinint;
 	bool going_away;
 	bool device_lost;
+	bool features_invalid;
 	void *airq_info;
 };
 
@@ -746,6 +747,12 @@ static u64 virtio_ccw_get_features(struct virtio_device *vdev)
 	if (ret == 0)
 		rc |= (u64)le32_to_cpu(features->features) << 32;
 
+	/* Devices MUST set VIRTIO_F_VERSION_1 */
+	if (!(rc & BIT_ULL(VIRTIO_F_VERSION_1))) {
+		vcdev->features_invalid = true;
+		rc |= BIT_ULL(VIRTIO_F_VERSION_1);
+	}
+
 out_free:
 	kfree(features);
 	kfree(ccw);
@@ -758,6 +765,10 @@ static void virtio_ccw_finalize_features(struct virtio_device *vdev)
 	struct virtio_feature_desc *features;
 	struct ccw1 *ccw;
 
+	/* Invalid features? Let's not try to drive this device. */
+	if (vcdev->features_invalid)
+		return;
+
 	ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
 	if (!ccw)
 		return;
@@ -869,6 +880,10 @@ static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
 	struct ccw1 *ccw;
 	int ret;
 
+	/* Invalid features? Let's not try to drive this device. */
+	if (vcdev->features_invalid)
+		return;
+
 	ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
 	if (!ccw)
 		return;
Signature · 1 line
MST
#5 lore raw

Re: [PATCH RFC 3/3] virtio_ccw: rev 1 devices set VIRTIO_F_VERSION_1

Quoted 5 lines · On Thu, 4 Dec 2014 19:26:50 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

What does it mean if rev 1 device does not set
VIRTIO_F_VERSION_1? E.g. is it native endian?

Let's not even try to drive such devices:
skip attempts to finalize features or set status.

Why not set status_failed?

Quoted 7 lines

virtio core will detect this and bail out.

---
 drivers/s390/kvm/virtio_ccw.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

I'm not sure yet whether I like this approach. I'll think about it over the weekend.

author maintainer
#6 lore raw

Re: [PATCH RFC 3/3] virtio_ccw: rev 1 devices set VIRTIO_F_VERSION_1

Quoted 10 lines · Cornelia Huck
Quoted 5 lines · On Thu, 4 Dec 2014 19:26:50 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

What does it mean if rev 1 device does not set
VIRTIO_F_VERSION_1? E.g. is it native endian?

Let's not even try to drive such devices:
skip attempts to finalize features or set status.

Why not set status_failed?

What's status_failed?

Quoted 10 lines
Quoted 7 lines

virtio core will detect this and bail out.

---
 drivers/s390/kvm/virtio_ccw.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

I'm not sure yet whether I like this approach. I'll think about it over the weekend.

author maintainer
#7 lore raw

Re: [PATCH RFC 3/3] virtio_ccw: rev 1 devices set VIRTIO_F_VERSION_1

In reply to Cornelia Huck
Quoted 10 lines · Cornelia Huck
Quoted 5 lines · On Thu, 4 Dec 2014 19:26:50 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

What does it mean if rev 1 device does not set
VIRTIO_F_VERSION_1? E.g. is it native endian?

Let's not even try to drive such devices:
skip attempts to finalize features or set status.

Why not set status_failed?

It might be cleanest to just teach core that finalize_features can fail.
I'll look into this.

Quoted 10 lines
Quoted 7 lines

virtio core will detect this and bail out.

---
 drivers/s390/kvm/virtio_ccw.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

I'm not sure yet whether I like this approach. I'll think about it over the weekend.

Acked-by on v1 1/3
#8 lore raw
In reply to Michael S. Tsirkin
Quoted 8 lines · On Thu, 4 Dec 2014 19:26:42 +0200 "Michael S. Tsirkin" <[email protected]> wrote:

transports need to be able to detect legacy-only
devices (ATM balloon only) to use legacy path
to drive them.

Add a core API to do just that.
The implementation just blacklists balloon:
not too pretty, but let's not over-engineer.

Quoted 5 lines
---
 include/linux/virtio.h  | 2 ++
 drivers/virtio/virtio.c | 6 ++++++
 2 files changed, 8 insertions(+)