Skip to contentSkip to conversation

virtio: early detect 'modern' virtio

RFC PATCH Superseded Halil Pasic 3 participants 10 messages Oct 28, 2021 – Nov 12, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover virtio: early detect 'modern' virtio

This is an early RFC for a transport specific early detecton of modern virtio, which is most relevant for transitional devices on big endian platforms, when drivers access the config space before FEATURES_OK is set.

The most important part that is missing here is fixing the problems that arrise in the situation described in the previous paragraph, when the config is managed by a vhost device (and thus outside QEMU).

This series was only lightly tested.

Halil Pasic (3):
virtio: introduce virtio_force_modern()
virtio-ccw: use virtio_force_modern
virtio-pci: use virtio_force_modern()

 hw/s390x/virtio-ccw.c      |  3 +++
 hw/virtio/virtio-pci.c     |  1 +
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h |  1 +
 4 files changed, 17 insertions(+)

base-commit: 2c3e83f92d93fbab071b8a96b8ab769b01902475

Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/3 virtio: introduce virtio_force_modern()

Legacy vs modern should be detected via transport specific means. We can't wait till feature negotiation is done. Let us introduce virtio_force_modern() as a means for the transport code to signal that the device should operate in modern mode (because a modern driver was detected).

---

I'm still struggling with how to deal with vhost-user and co. The problem is that I'm not very familiar with the life-cycle of, let us say, a vhost_user device.

Looks to me like the vhost part might be just an implementation detail, and could even become a hot swappable thing.

Another thing is, that vhost processes set_features differently. It might or might not be a good idea to change this.

Does anybody know why don't we propagate the features on features_set, but under a set of different conditions, one of which is the vhost device is started?

---
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h |  1 +
 2 files changed, 13 insertions(+)
hw/virtio/virtio.c +12 -0
index 3a1f6c520c..75aee0e098 100644
@@ -3281,6 +3281,18 @@ void virtio_init(VirtIODevice *vdev, const char *name,
     vdev->use_guest_notifier_mask = true;
 }
 
+void  virtio_force_modern(VirtIODevice *vdev)
+{
+    /*
+     * This takes care of the devices that implement config space access
+     * in QEMU. For vhost-user and similar we need to make sure the features
+     * are actually propagated to the device implementing the config space.
+     *
+     * A VirtioDeviceClass callback may be a good idea.
+     */
+    virtio_set_features(vdev, (1ULL << VIRTIO_F_VERSION_1));
+}
+
 /*
  * Only devices that have already been around prior to defining the virtio
  * standard support legacy mode; this includes devices not specified in the
include/hw/virtio/virtio.h +1 -0
index 8bab9cfb75..2ea92de7a5 100644
@@ -394,6 +394,7 @@ static inline bool virtio_device_disabled(VirtIODevice *vdev)
     return unlikely(vdev->disabled || vdev->broken);
 }
 
+void  virtio_force_modern(VirtIODevice *vdev);
 bool virtio_legacy_allowed(VirtIODevice *vdev);
 bool virtio_legacy_check_disabled(VirtIODevice *vdev);
 
Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC PATCH v1 1/3] virtio: introduce virtio_force_modern()

Quoted 32 lines · Halil Pasic

Legacy vs modern should be detected via transport specific means. We can't wait till feature negotiation is done. Let us introduce virtio_force_modern() as a means for the transport code to signal that the device should operate in modern mode (because a modern driver was detected).

---

I'm still struggling with how to deal with vhost-user and co. The problem is that I'm not very familiar with the life-cycle of, let us say, a vhost_user device.

Looks to me like the vhost part might be just an implementation detail, and could even become a hot swappable thing.

Another thing is, that vhost processes set_features differently. It might or might not be a good idea to change this.

Does anybody know why don't we propagate the features on features_set, but under a set of different conditions, one of which is the vhost device is started?

---
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h |  1 +
 2 files changed, 13 insertions(+)
hw/virtio/virtio.c +0 -0
index 3a1f6c520c..75aee0e098 100644

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3284

Quoted 4 lines
hw/virtio/virtio.c +1 -0
@@ -3281,6 +3281,18 @@ void virtio_init(VirtIODevice *vdev, const char *name,
     vdev->use_guest_notifier_mask = true;
 }
 
+void  virtio_force_modern(VirtIODevice *vdev)

<bikeshed> I'm not sure I like that name. We're not actually forcing the device to be modern, we just set an early indication in the device before proper feature negotiation has finished. Maybe virtio_indicate_modern()? </bikeshed>

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3293

Quoted 9 lines
hw/virtio/virtio.c +9 -0
+{
+    /*
+     * This takes care of the devices that implement config space access
+     * in QEMU. For vhost-user and similar we need to make sure the features
+     * are actually propagated to the device implementing the config space.
+     *
+     * A VirtioDeviceClass callback may be a good idea.
+     */
+    virtio_set_features(vdev, (1ULL << VIRTIO_F_VERSION_1));

Do we really need/want to do the whole song-and-dance for setting features, just for setting VERSION_1? Devices may modify some of their behaviour or features, depending on what features they are called with, and we will be calling this one again later with what is likely a different feature set. Also, the return code is not checked.

Maybe introduce a new function that sets guest_features directly and errors out if the features are not set in host_features? If we try to set VERSION_1 here despite the device not offering it, we are in a pickle anyway, as we should not have gotten here if we did not offer it, and we really should moan and fail in that case.

Quoted 5 lines
hw/virtio/virtio.c +2 -0
+}
+
 /*
  * Only devices that have already been around prior to defining the virtio
  * standard support legacy mode; this includes devices not specified in the
#3 lore raw

Re: [RFC PATCH v1 1/3] virtio: introduce virtio_force_modern()

Quoted 37 lines · On Fri, 29 Oct 2021 16:53:25 +0200 Cornelia Huck <[email protected]> wrote:
Quoted 35 lines · Halil Pasic

Legacy vs modern should be detected via transport specific means. We can't wait till feature negotiation is done. Let us introduce virtio_force_modern() as a means for the transport code to signal that the device should operate in modern mode (because a modern driver was detected).

---

I'm still struggling with how to deal with vhost-user and co. The problem is that I'm not very familiar with the life-cycle of, let us say, a vhost_user device.

Looks to me like the vhost part might be just an implementation detail, and could even become a hot swappable thing.

Another thing is, that vhost processes set_features differently. It might or might not be a good idea to change this.

Does anybody know why don't we propagate the features on features_set, but under a set of different conditions, one of which is the vhost device is started?

---
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h |  1 +
 2 files changed, 13 insertions(+)
hw/virtio/virtio.c +0 -0
index 3a1f6c520c..75aee0e098 100644
@@ -3281,6 +3281,18 @@ void virtio_init(VirtIODevice *vdev, const char *name,
     vdev->use_guest_notifier_mask = true;
 }
 
Quoted 6 lines

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3284

Quoted 1 line
hw/virtio/virtio.c +1 -0
+void  virtio_force_modern(VirtIODevice *vdev)  

<bikeshed> I'm not sure I like that name. We're not actually forcing the device to be modern, we just set an early indication in the device before proper feature negotiation has finished. Maybe virtio_indicate_modern()? </bikeshed>

I don't like virtio_indicate_modern(dev) form object orientation perspective. In an OO language one would write it like dev.virtio_indicate_modern()
which would read like the device should indicate modern to somebody.

In my opinion what happens is that we want to disable the legacy interface if it is exposed by the device, or in other words instruct the device that should act (precisely and exclusively) according to the interface specification of the modern interface.

Maybe we can find a better name than force_modern, but I don't think indicate_modern is a better name.

Quoted 11 lines

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3293

Quoted 10 lines
hw/virtio/virtio.c +9 -0
+{
+    /*
+     * This takes care of the devices that implement config space access
+     * in QEMU. For vhost-user and similar we need to make sure the features
+     * are actually propagated to the device implementing the config space.
+     *
+     * A VirtioDeviceClass callback may be a good idea.
+     */
+    virtio_set_features(vdev, (1ULL << VIRTIO_F_VERSION_1));  
Quoted 2 lines

Do we really need/want to do the whole song-and-dance for setting features, just for setting VERSION_1?

When doing the whole song-and-dance the chance is higher that the information will propagate to every place it needs to reach. For example to the acked_features of vhost_dev. I've just posted a v2 RFC. It should not be hard to see what I mean after examining that RFC.

Quoted 2 lines

Devices may modify some of their
behaviour or features, depending on what features they are called with,

I believe, if this is the case, we want the behavior that corresponds to VERSION_1 set, i.e. 'modern'. So in my understanding this is rather good than bad.

Quoted 2 lines

and we will be calling this one again later with what is likely a different feature set.

That is true, but the driver is allowed to set the features multiple times, and since transports only support piecemeal access to the features (32 bits at a time), I guess this is biz as usual.

Quoted 2 lines

Also, the return code is not checked.

That is true! It might be a good idea to log an error. Unfortunately I don't think there is anything else we can sanely do.

Quoted 2 lines

Maybe introduce a new function that sets guest_features directly and errors out if the features are not set in host_features?

See above.

Quoted 4 lines

If we try to
set VERSION_1 here despite the device not offering it, we are in a pickle anyway, as we should not have gotten here if we did not offer it, and we really should moan and fail in that case.

I agree about the moan part. I'm not sure what is the best way to 'fail'. Maybe we should continue this discussion in the v2 thread.

Thanks for your feedback! Sorry I didn't answer before sending out a v2.

Regards,
Halil

Quoted 7 lines
Quoted 6 lines
hw/virtio/virtio.c +2 -0
+}
+
 /*
  * Only devices that have already been around prior to defining the virtio
  * standard support legacy mode; this includes devices not specified in the  
#4 lore raw

Re: [RFC PATCH v1 1/3] virtio: introduce virtio_force_modern()

Quoted 48 lines · Halil Pasic
Quoted 37 lines · On Fri, 29 Oct 2021 16:53:25 +0200 Cornelia Huck <[email protected]> wrote:
Quoted 35 lines · Halil Pasic

Legacy vs modern should be detected via transport specific means. We can't wait till feature negotiation is done. Let us introduce virtio_force_modern() as a means for the transport code to signal that the device should operate in modern mode (because a modern driver was detected).

---

I'm still struggling with how to deal with vhost-user and co. The problem is that I'm not very familiar with the life-cycle of, let us say, a vhost_user device.

Looks to me like the vhost part might be just an implementation detail, and could even become a hot swappable thing.

Another thing is, that vhost processes set_features differently. It might or might not be a good idea to change this.

Does anybody know why don't we propagate the features on features_set, but under a set of different conditions, one of which is the vhost device is started?

---
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h |  1 +
 2 files changed, 13 insertions(+)
hw/virtio/virtio.c +0 -0
index 3a1f6c520c..75aee0e098 100644
@@ -3281,6 +3281,18 @@ void virtio_init(VirtIODevice *vdev, const char *name,
     vdev->use_guest_notifier_mask = true;
 }
 
Quoted 6 lines

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3284

Quoted 1 line
hw/virtio/virtio.c +1 -0
+void  virtio_force_modern(VirtIODevice *vdev)  

<bikeshed> I'm not sure I like that name. We're not actually forcing the device to be modern, we just set an early indication in the device before proper feature negotiation has finished. Maybe virtio_indicate_modern()? </bikeshed>

Quoted 4 lines

I don't like virtio_indicate_modern(dev) form object orientation perspective. In an OO language one would write it like dev.virtio_indicate_modern()
which would read like the device should indicate modern to somebody.

I think that is actually what happens: we indicate that it is a modern device to the code making the endianness decisions.

Quoted 5 lines

In my opinion what happens is that we want to disable the legacy interface if it is exposed by the device, or in other words instruct the device that should act (precisely and exclusively) according to the interface specification of the modern interface.

I don't see us disabling anything; the driver has already chosen what they want, and we simply need to make sure that all code honours that decision.

Quoted 30 lines

Maybe we can find a better name than force_modern, but I don't think indicate_modern is a better name.

Quoted 11 lines

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3293

Quoted 10 lines
hw/virtio/virtio.c +9 -0
+{
+    /*
+     * This takes care of the devices that implement config space access
+     * in QEMU. For vhost-user and similar we need to make sure the features
+     * are actually propagated to the device implementing the config space.
+     *
+     * A VirtioDeviceClass callback may be a good idea.
+     */
+    virtio_set_features(vdev, (1ULL << VIRTIO_F_VERSION_1));  
Quoted 2 lines

Do we really need/want to do the whole song-and-dance for setting features, just for setting VERSION_1?

When doing the whole song-and-dance the chance is higher that the information will propagate to every place it needs to reach. For example to the acked_features of vhost_dev. I've just posted a v2 RFC. It should not be hard to see what I mean after examining that RFC.

Quoted 2 lines

Devices may modify some of their
behaviour or features, depending on what features they are called with,

I believe, if this is the case, we want the behavior that corresponds to VERSION_1 set, i.e. 'modern'. So in my understanding this is rather good than bad.

Quoted 6 lines
Quoted 2 lines

and we will be calling this one again later with what is likely a different feature set.

That is true, but the driver is allowed to set the features multiple times, and since transports only support piecemeal access to the features (32 bits at a time), I guess this is biz as usual.

Also see my comment in the v2: I'm not sure how well tested that actually is.

Quoted 12 lines
Quoted 3 lines

Also, the return code is not checked.

That is true! It might be a good idea to log an error. Unfortunately I don't think there is anything else we can sanely do.

Quoted 2 lines

Maybe introduce a new function that sets guest_features directly and errors out if the features are not set in host_features?

See above.

Quoted 7 lines
Quoted 4 lines

If we try to
set VERSION_1 here despite the device not offering it, we are in a pickle anyway, as we should not have gotten here if we did not offer it, and we really should moan and fail in that case.

I agree about the moan part. I'm not sure what is the best way to 'fail'. Maybe we should continue this discussion in the v2 thread.

Yeah, let's continue there, since that code is a bit different.

#5 lore raw

Re: [RFC PATCH v1 1/3] virtio: introduce virtio_force_modern()

Quoted 50 lines · On Fri, 12 Nov 2021 16:55:10 +0100 Cornelia Huck <[email protected]> wrote:
Quoted 48 lines · Halil Pasic
Quoted 43 lines · On Fri, 29 Oct 2021 16:53:25 +0200 Cornelia Huck <[email protected]> wrote:
Quoted 32 lines · Halil Pasic

Legacy vs modern should be detected via transport specific means. We can't wait till feature negotiation is done. Let us introduce virtio_force_modern() as a means for the transport code to signal that the device should operate in modern mode (because a modern driver was detected).

---

I'm still struggling with how to deal with vhost-user and co. The problem is that I'm not very familiar with the life-cycle of, let us say, a vhost_user device.

Looks to me like the vhost part might be just an implementation detail, and could even become a hot swappable thing.

Another thing is, that vhost processes set_features differently. It might or might not be a good idea to change this.

Does anybody know why don't we propagate the features on features_set, but under a set of different conditions, one of which is the vhost device is started?

---
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h |  1 +
 2 files changed, 13 insertions(+)
hw/virtio/virtio.c +0 -0
index 3a1f6c520c..75aee0e098 100644

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3284

Quoted 4 lines
hw/virtio/virtio.c +1 -0
@@ -3281,6 +3281,18 @@ void virtio_init(VirtIODevice *vdev, const char *name,
     vdev->use_guest_notifier_mask = true;
 }
 
+void  virtio_force_modern(VirtIODevice *vdev)    

<bikeshed> I'm not sure I like that name. We're not actually forcing the device to be modern, we just set an early indication in the device before proper feature negotiation has finished. Maybe virtio_indicate_modern()? </bikeshed>

Quoted 8 lines
Quoted 4 lines

I don't like virtio_indicate_modern(dev) form object orientation perspective. In an OO language one would write it like dev.virtio_indicate_modern()
which would read like the device should indicate modern to somebody.

I think that is actually what happens: we indicate that it is a modern device to the code making the endianness decisions.

But in an OO school of thought that code belongs to the given virtio device object and is one of the building blocks that makes the object what it is. What I'm trying to explain is: that code ain't no external entity we have to indicate something to.

On the contrary, if we had to indicate 'modern' to the driver, how would you name that function? Clearly we don't need such functionality, I'm just trying to make an argument here.

To take a different example, imagine a ccw channel path. We may break the the channel path, we may indicate to the OS that the channel path is broken (via CRW), and we may do first break than indicate.

Quoted 9 lines
Quoted 5 lines

In my opinion what happens is that we want to disable the legacy interface if it is exposed by the device, or in other words instruct the device that should act (precisely and exclusively) according to the interface specification of the modern interface.

I don't see us disabling anything; the driver has already chosen what they want, and we simply need to make sure that all code honours that decision.

IMHO a buggy driver could make an attempt at using the legacy interface at least in case of pci.

My understanding is that the decision of the driver results in an interaction between the driver and the device, and as a result of that interaction, the state of the device changes. This function is supposed to implement that state-change.

Do we agree that there is a state change? If yes, how would you describe that state change?

Quoted 34 lines
Quoted 34 lines

Maybe we can find a better name than force_modern, but I don't think indicate_modern is a better name.

Quoted 11 lines

hw/virtio/virtio.c void virtio_init(VirtIODevice *vdev, const char *name, · line 3293

Quoted 10 lines
hw/virtio/virtio.c +9 -0
+{
+    /*
+     * This takes care of the devices that implement config space access
+     * in QEMU. For vhost-user and similar we need to make sure the features
+     * are actually propagated to the device implementing the config space.
+     *
+     * A VirtioDeviceClass callback may be a good idea.
+     */
+    virtio_set_features(vdev, (1ULL << VIRTIO_F_VERSION_1));    
Quoted 2 lines

Do we really need/want to do the whole song-and-dance for setting features, just for setting VERSION_1?

When doing the whole song-and-dance the chance is higher that the information will propagate to every place it needs to reach. For example to the acked_features of vhost_dev. I've just posted a v2 RFC. It should not be hard to see what I mean after examining that RFC.

Quoted 2 lines

Devices may modify some of their
behaviour or features, depending on what features they are called with,

I believe, if this is the case, we want the behavior that corresponds to VERSION_1 set, i.e. 'modern'. So in my understanding this is rather good than bad.

Quoted 2 lines

and we will be calling this one again later with what is likely a different feature set.

Quoted 7 lines
Quoted 3 lines

That is true, but the driver is allowed to set the features multiple times, and since transports only support piecemeal access to the features (32 bits at a time), I guess this is biz as usual.

Also see my comment in the v2: I'm not sure how well tested that actually is.

Will answer there.

Quoted 17 lines
Quoted 17 lines
Quoted 3 lines

Also, the return code is not checked.

That is true! It might be a good idea to log an error. Unfortunately I don't think there is anything else we can sanely do.

Quoted 2 lines

Maybe introduce a new function that sets guest_features directly and errors out if the features are not set in host_features?

See above.

Quoted 4 lines

If we try to
set VERSION_1 here despite the device not offering it, we are in a pickle anyway, as we should not have gotten here if we did not offer it, and we really should moan and fail in that case.

Quoted 5 lines
Quoted 2 lines

I agree about the moan part. I'm not sure what is the best way to 'fail'. Maybe we should continue this discussion in the v2 thread.

Yeah, let's continue there, since that code is a bit different.

Nod!

Thanks!
Halil

#6 lore raw

RFC PATCH 2/3 virtio-ccw: use virtio_force_modern

In reply to Halil Pasic

The fact that revision > 0 was negotiated implies that VIRTIO_VERSION_1 aka modern must be used. This negotiation is done before the obligatory reset. Let us call virtio_force_modern() after the reset if revision > 0 was negotiated, so that the VIRTIO_VERSION_1 feature can be set, and endianness starts working as it should for devices that comply to the virtio spec.

---
 hw/s390x/virtio-ccw.c | 3 +++
 1 file changed, 3 insertions(+)
hw/s390x/virtio-ccw.c +3 -0
index 6a2df1c1e9..88fbe87942 100644
@@ -266,6 +266,9 @@ static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev, VirtIODevice *vdev)
         dev->summary_indicator = NULL;
     }
     ccw_dev->sch->thinint_active = false;
+    if (dev->revision > 0) {
+        virtio_force_modern(vdev);
+    }
 }
 
 static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len,
Signature · 1 line
2.25.1
#7 lore raw

RFC PATCH 3/3 virtio-pci: use virtio_force_modern()

In reply to Halil Pasic

Let us detect usage via the modern interface by tapping into the place that implements the 'modern' reset.

---
 hw/virtio/virtio-pci.c | 1 +
 1 file changed, 1 insertion(+)
hw/virtio/virtio-pci.c +1 -0
index 6e16e2705c..8dd862da21 100644
@@ -1297,6 +1297,7 @@ static void virtio_pci_common_write(void *opaque, hwaddr addr,
 
         if (vdev->status == 0) {
             virtio_pci_reset(DEVICE(proxy));
+            virtio_force_modern(virtio_bus_get_device(&proxy->bus));
         }
 
         break;
Signature · 1 line
2.25.1
#8 lore raw
In reply to Halil Pasic
Quoted 10 lines · Halil Pasic

This is an early RFC for a transport specific early detecton of modern virtio, which is most relevant for transitional devices on big endian platforms, when drivers access the config space before FEATURES_OK is set.

The most important part that is missing here is fixing the problems that arrise in the situation described in the previous paragraph, when the config is managed by a vhost device (and thus outside QEMU).

This series was only lightly tested.

I think it's a good enough approach, and we are getting close to release. For vhost - a new callback just for that? Would be ok. Or just invoke set features - if this works.

Quoted 15 lines

Halil Pasic (3):
virtio: introduce virtio_force_modern()
virtio-ccw: use virtio_force_modern
virtio-pci: use virtio_force_modern()

 hw/s390x/virtio-ccw.c      |  3 +++
 hw/virtio/virtio-pci.c     |  1 +
 hw/virtio/virtio.c         | 12 ++++++++++++
 include/hw/virtio/virtio.h |  1 +
 4 files changed, 17 insertions(+)

base-commit: 2c3e83f92d93fbab071b8a96b8ab769b01902475

Signature · 1 line
2.25.1
#9 lore raw
Quoted 5 lines · On Fri, 5 Nov 2021 03:42:33 -0400 "Michael S. Tsirkin" <[email protected]> wrote:
Quoted 1 line

This series was only lightly tested.

I think it's a good enough approach, and we are getting close to release. For vhost - a new callback just for that? Would be ok. Or just invoke set features - if this works.

As of today I'm back from vacation. I intend to handle this problem/series with priority.

Regards,
Halil