Skip to contentSkip to conversation

virtio-serial: add enable_backend callback

PATCH Superseded Pavel Butsykin 5 participants 11 messages Jul 7, 2017 – Sep 19, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-serial: add enable_backend callback

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

---
 hw/char/virtio-console.c          | 21 +++++++++++++++++++++
 hw/char/virtio-serial-bus.c       |  7 +++++++
 include/hw/virtio/virtio-serial.h |  3 +++
 3 files changed, 31 insertions(+)
hw/char/virtio-console.c +21 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
     }
 }
 
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}
+
 static void virtconsole_realize(DeviceState *dev, Error **errp)
 {
     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
     k->unrealize = virtconsole_unrealize;
     k->have_data = flush_buf;
     k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;
     k->guest_writable = guest_writable;
     dc->props = virtserialport_properties;
 }
hw/char/virtio-serial-bus.c +7 -0
index f5bc173844..f0f18c8e7c 100644
@@ -637,6 +637,13 @@ static void set_status(VirtIODevice *vdev, uint8_t status)
     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
         guest_reset(vser);
     }
+
+    QTAILQ_FOREACH(port, &vser->ports, next) {
+        VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+        if (vsc->enable_backend) {
+            vsc->enable_backend(port, vdev->vm_running);
+        }
+    }
 }
 
 static void vser_reset(VirtIODevice *vdev)
include/hw/virtio/virtio-serial.h +3 -0
index b19c44727f..12657a9f39 100644
@@ -58,6 +58,9 @@ typedef struct VirtIOSerialPortClass {
         /* Guest opened/closed device. */
     void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
 
+    /* Enable/disable backend for virtio serial port */
+    void (*enable_backend)(VirtIOSerialPort *port, bool enable);
+
         /* Guest is now ready to accept data (virtqueues set up). */
     void (*guest_ready)(VirtIOSerialPort *port);
 
Signature · 1 line
2.13.0
#1 lore raw
Quoted 34 lines · Pavel Butsykin

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

---
 hw/char/virtio-console.c          | 21 +++++++++++++++++++++
 hw/char/virtio-serial-bus.c       |  7 +++++++
 include/hw/virtio/virtio-serial.h |  3 +++
 3 files changed, 31 insertions(+)
hw/char/virtio-console.c +9 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
     }
 }
 
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {

hw/char/virtio-console.c static void chr_event(void *opaque, int event) · line 184

Quoted 10 lines
hw/char/virtio-console.c +10 -0
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}

I think you can also factorize the code in virtconsole_realize() to call this new function.

hw/char/virtio-console.c static void virtserialport_class_init(ObjectClass *klass, void *data) · line 256

Quoted 8 lines
hw/char/virtio-console.c +1 -0
 static void virtconsole_realize(DeviceState *dev, Error **errp)
 {
     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
     k->unrealize = virtconsole_unrealize;
     k->have_data = flush_buf;
     k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;

Why don't you register a vm_state change handler to change the state of the virtconsole according to the state of the machine instead of adding a new function in the VirtIOSerialPortClass?

See a23a6d1 ("virtio-rng: stop virtqueue while the CPU is stopped")

Thanks,
Laurent

#2 lore raw
Quoted 13 lines · Laurent Vivier

hw/char/virtio-console.c static void virtserialport_class_init(ObjectClass *klass, void *data) · line 256

Quoted 8 lines

static void virtconsole_realize(DeviceState *dev, Error **errp)

 {
     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
hw/char/virtio-console.c +1 -0
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
     k->unrealize = virtconsole_unrealize;
     k->have_data = flush_buf;
     k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;

Why don't you register a vm_state change handler to change the state of the virtconsole according to the state of the machine instead of adding a new function in the VirtIOSerialPortClass?

Quoted 4 lines

See a23a6d1 ("virtio-rng: stop virtqueue while the CPU is stopped")

Thanks,
Laurent

In fact that commit does it the wrong way IMHO.

The order of this call wrt other virtio calls is not guaranteed.

IMHO the right way is to set a vm state change handler in VirtioBusClass or status change handler in VirtioDeviceClass.

Signature · 1 line
MST
#3 lore raw
Quoted 20 lines · Michael S. Tsirkin
Quoted 13 lines · Laurent Vivier

hw/char/virtio-console.c static void virtserialport_class_init(ObjectClass *klass, void *data) · line 256

Quoted 8 lines

static void virtconsole_realize(DeviceState *dev, Error **errp)

 {
     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
hw/char/virtio-console.c +1 -0
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
     k->unrealize = virtconsole_unrealize;
     k->have_data = flush_buf;
     k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;

Why don't you register a vm_state change handler to change the state of the virtconsole according to the state of the machine instead of adding a new function in the VirtIOSerialPortClass?

Quoted 4 lines

See a23a6d1 ("virtio-rng: stop virtqueue while the CPU is stopped")

Thanks,
Laurent

Quoted 8 lines

In fact that commit does it the wrong way IMHO.

The order of this call wrt other virtio calls is not guaranteed.

IMHO the right way is to set a vm state change handler in VirtioBusClass or status change handler in VirtioDeviceClass.

OK, so I guess Pavel's solution is the good one as it calls his handler from virtio-serial-bus set_status() handler which is called by virtio_vmstate_change() function, which is registered with qemu_add_vm_change_state_handler() by virtio_init()...

Thanks,
Laurent

#4 lore raw
In reply to Laurent Vivier
Quoted 62 lines · Laurent Vivier
Quoted 34 lines · Pavel Butsykin

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

---
  hw/char/virtio-console.c          | 21 +++++++++++++++++++++
  hw/char/virtio-serial-bus.c       |  7 +++++++
  include/hw/virtio/virtio-serial.h |  3 +++
  3 files changed, 31 insertions(+)
hw/char/virtio-console.c +9 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
      }
  }
 
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {

hw/char/virtio-console.c static void chr_event(void *opaque, int event) · line 184

Quoted 10 lines
hw/char/virtio-console.c +10 -0
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}

I think you can also factorize the code in virtconsole_realize() to call this new function.

hw/char/virtio-console.c static void virtserialport_class_init(ObjectClass *klass, void *data) · line 256

Quoted 8 lines
hw/char/virtio-console.c +1 -0
  static void virtconsole_realize(DeviceState *dev, Error **errp)
  {
      VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
      k->unrealize = virtconsole_unrealize;
      k->have_data = flush_buf;
      k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;

Why don't you register a vm_state change handler to change the state of the virtconsole according to the state of the machine instead of adding a new function in the VirtIOSerialPortClass?

Quoted 1 line

See a23a6d1 ("virtio-rng: stop virtqueue while the CPU is stopped")

I tried to follow the existing approach. Look at vhost_net or virtio-input. This is similar to the hierarchical structure, we have the root device which notifies the virtio devices at the levels above (virtio-device -> virtio-bus -> virtio_*_device ). It ensures the state of devices will be modified consistently, the first is a bus, then the device on that bus. I'm not sure that following this order during the device state changes is absolute necessity. But it looks nicer than the registration of notifications for each device.

Although this is just a guess, I have no clear idea of how to do it right, so I'm waiting for comments :)

Quoted 3 lines

Thanks,
Laurent

#5 lore raw
In reply to Pavel Butsykin
Quoted 80 lines · Pavel Butsykin

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

---
  hw/char/virtio-console.c          | 21 +++++++++++++++++++++
  hw/char/virtio-serial-bus.c       |  7 +++++++
  include/hw/virtio/virtio-serial.h |  3 +++
  3 files changed, 31 insertions(+)
hw/char/virtio-console.c +21 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
      }
  }
 
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}
+
  static void virtconsole_realize(DeviceState *dev, Error **errp)
  {
      VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
      k->unrealize = virtconsole_unrealize;
      k->have_data = flush_buf;
      k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;
      k->guest_writable = guest_writable;
      dc->props = virtserialport_properties;
  }
hw/char/virtio-serial-bus.c +7 -0
index f5bc173844..f0f18c8e7c 100644
@@ -637,6 +637,13 @@ static void set_status(VirtIODevice *vdev, uint8_t status)
      if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
          guest_reset(vser);
      }
+
+    QTAILQ_FOREACH(port, &vser->ports, next) {
+        VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+        if (vsc->enable_backend) {
+            vsc->enable_backend(port, vdev->vm_running);
+        }
+    }
  }
 
  static void vser_reset(VirtIODevice *vdev)
include/hw/virtio/virtio-serial.h +0 -0
index b19c44727f..12657a9f39 100644

include/hw/virtio/virtio-serial.h typedef struct VirtIOSerialPortClass { · line 66

Quoted 9 lines
include/hw/virtio/virtio-serial.h +3 -0
@@ -58,6 +58,9 @@ typedef struct VirtIOSerialPortClass {
          /* Guest opened/closed device. */
      void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
 
+    /* Enable/disable backend for virtio serial port */
+    void (*enable_backend)(VirtIOSerialPort *port, bool enable);
+
          /* Guest is now ready to accept data (virtqueues set up). */
      void (*guest_ready)(VirtIOSerialPort *port);
 

ping

#6 lore raw
Quoted 84 lines · Pavel Butsykin
Quoted 83 lines · Pavel Butsykin

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

---
  hw/char/virtio-console.c          | 21 +++++++++++++++++++++
  hw/char/virtio-serial-bus.c       |  7 +++++++
  include/hw/virtio/virtio-serial.h |  3 +++
  3 files changed, 31 insertions(+)
hw/char/virtio-console.c +21 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
      }
  }
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool 
 enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}
+
  static void virtconsole_realize(DeviceState *dev, Error **errp)
  {
      VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass 
 *klass, void *data)
      k->unrealize = virtconsole_unrealize;
      k->have_data = flush_buf;
      k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;
      k->guest_writable = guest_writable;
      dc->props = virtserialport_properties;
  }
hw/char/virtio-serial-bus.c +7 -0
index f5bc173844..f0f18c8e7c 100644
@@ -637,6 +637,13 @@ static void set_status(VirtIODevice *vdev, 
 uint8_t status)
      if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
          guest_reset(vser);
      }
+
+    QTAILQ_FOREACH(port, &vser->ports, next) {
+        VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+        if (vsc->enable_backend) {
+            vsc->enable_backend(port, vdev->vm_running);
+        }
+    }
  }
  static void vser_reset(VirtIODevice *vdev)
include/hw/virtio/virtio-serial.h +0 -0
index b19c44727f..12657a9f39 100644
@@ -58,6 +58,9 @@ typedef struct VirtIOSerialPortClass {
          /* Guest opened/closed device. */
      void (*set_guest_connected)(VirtIOSerialPort *port, int 
 guest_connected);
Quoted 8 lines

include/hw/virtio/virtio-serial.h typedef struct VirtIOSerialPortClass { · line 66

Quoted 6 lines
include/hw/virtio/virtio-serial.h +3 -0
+    /* Enable/disable backend for virtio serial port */
+    void (*enable_backend)(VirtIOSerialPort *port, bool enable);
+
          /* Guest is now ready to accept data (virtqueues set up). */
      void (*guest_ready)(VirtIOSerialPort *port);
 

ping

ping^2

#7 lore raw
In reply to Pavel Butsykin
Quoted 79 lines · Pavel Butsykin

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

---
 hw/char/virtio-console.c          | 21 +++++++++++++++++++++
 hw/char/virtio-serial-bus.c       |  7 +++++++
 include/hw/virtio/virtio-serial.h |  3 +++
 3 files changed, 31 insertions(+)
hw/char/virtio-console.c +21 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
     }
 }
 
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}
+
 static void virtconsole_realize(DeviceState *dev, Error **errp)
 {
     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
     k->unrealize = virtconsole_unrealize;
     k->have_data = flush_buf;
     k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;
     k->guest_writable = guest_writable;
     dc->props = virtserialport_properties;
 }
hw/char/virtio-serial-bus.c +7 -0
index f5bc173844..f0f18c8e7c 100644
@@ -637,6 +637,13 @@ static void set_status(VirtIODevice *vdev, uint8_t status)
     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
         guest_reset(vser);
     }
+
+    QTAILQ_FOREACH(port, &vser->ports, next) {
+        VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+        if (vsc->enable_backend) {
+            vsc->enable_backend(port, vdev->vm_running);
+        }
+    }
 }
 
 static void vser_reset(VirtIODevice *vdev)
include/hw/virtio/virtio-serial.h +0 -0
index b19c44727f..12657a9f39 100644

include/hw/virtio/virtio-serial.h typedef struct VirtIOSerialPortClass { · line 66

Quoted 9 lines
include/hw/virtio/virtio-serial.h +3 -0
@@ -58,6 +58,9 @@ typedef struct VirtIOSerialPortClass {
         /* Guest opened/closed device. */
     void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
 
+    /* Enable/disable backend for virtio serial port */
+    void (*enable_backend)(VirtIOSerialPort *port, bool enable);
+
         /* Guest is now ready to accept data (virtqueues set up). */
     void (*guest_ready)(VirtIOSerialPort *port);
 

guys, what was the destiny of this patch? It is a bit unclear. Have we get into the agreement or not?

Den

#8 lore raw
In reply to Pavel Butsykin
Quoted 11 lines · Pavel Butsykin

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

To understand the patch better this doesn't fix _all_ stopped states, only migration, right? That said it's a valid bugfix even independent of the effects for stopped runstate.

Thanks,

Paolo

Quoted 78 lines
---
 hw/char/virtio-console.c          | 21 +++++++++++++++++++++
 hw/char/virtio-serial-bus.c       |  7 +++++++
 include/hw/virtio/virtio-serial.h |  3 +++
 3 files changed, 31 insertions(+)
hw/char/virtio-console.c +21 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
     }
 }
 
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}
+
 static void virtconsole_realize(DeviceState *dev, Error **errp)
 {
     VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
     k->unrealize = virtconsole_unrealize;
     k->have_data = flush_buf;
     k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;
     k->guest_writable = guest_writable;
     dc->props = virtserialport_properties;
 }
hw/char/virtio-serial-bus.c +7 -0
index f5bc173844..f0f18c8e7c 100644
@@ -637,6 +637,13 @@ static void set_status(VirtIODevice *vdev, uint8_t status)
     if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
         guest_reset(vser);
     }
+
+    QTAILQ_FOREACH(port, &vser->ports, next) {
+        VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+        if (vsc->enable_backend) {
+            vsc->enable_backend(port, vdev->vm_running);
+        }
+    }
 }
 
 static void vser_reset(VirtIODevice *vdev)
include/hw/virtio/virtio-serial.h +3 -0
index b19c44727f..12657a9f39 100644
@@ -58,6 +58,9 @@ typedef struct VirtIOSerialPortClass {
         /* Guest opened/closed device. */
     void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
 
+    /* Enable/disable backend for virtio serial port */
+    void (*enable_backend)(VirtIOSerialPort *port, bool enable);
+
         /* Guest is now ready to accept data (virtqueues set up). */
     void (*guest_ready)(VirtIOSerialPort *port);
 
#9 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Pavel Butsykin

We should guarantee that RAM will not be modified while VM has a stopped state, otherwise it can lead to negative consequences during post-copy migration. In RUN_STATE_FINISH_MIGRATE step, it's expected that RAM on source side will not be modified as this could lead to non-consistent vm state on the destination side. Also RAM access during postcopy-ram migration with enabled release-ram capability can lead to sad consequences.

Quoted 8 lines
Quoted 4 lines

Let's add enable_backend() callback to avoid undesirable virtioqueue changes in the guest memory.

To understand the patch better this doesn't fix _all_ stopped states, only migration, right? That said it's a valid bugfix even independent of the effects for stopped runstate.

Yes, the bug only appears on the migration. Actually, to protect memory during the migration, this approach is already used for other virtio devices, for example net_vhost, see virtio_net_vhost_status().

Quoted 83 lines

Thanks,

Paolo

Quoted 78 lines
---
  hw/char/virtio-console.c          | 21 +++++++++++++++++++++
  hw/char/virtio-serial-bus.c       |  7 +++++++
  include/hw/virtio/virtio-serial.h |  3 +++
  3 files changed, 31 insertions(+)
hw/char/virtio-console.c +21 -0
index 0cb1668c8a..b55905892e 100644
@@ -163,6 +163,26 @@ static void chr_event(void *opaque, int event)
      }
  }
 
+static void virtconsole_enable_backend(VirtIOSerialPort *port, bool enable)
+{
+    VirtConsole *vcon = VIRTIO_CONSOLE(port);
+
+    if (!qemu_chr_fe_get_driver(&vcon->chr)) {
+        return;
+    }
+
+    if (enable) {
+        VirtIOSerialPortClass *k = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+
+        qemu_chr_fe_set_handlers(&vcon->chr, chr_can_read, chr_read,
+                                 k->is_console ? NULL : chr_event,
+                                 vcon, NULL, false);
+    } else {
+        qemu_chr_fe_set_handlers(&vcon->chr, NULL, NULL,
+                                 NULL, NULL, NULL, false);
+    }
+}
+
  static void virtconsole_realize(DeviceState *dev, Error **errp)
  {
      VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
@@ -233,6 +253,7 @@ static void virtserialport_class_init(ObjectClass *klass, void *data)
      k->unrealize = virtconsole_unrealize;
      k->have_data = flush_buf;
      k->set_guest_connected = set_guest_connected;
+    k->enable_backend = virtconsole_enable_backend;
      k->guest_writable = guest_writable;
      dc->props = virtserialport_properties;
  }
hw/char/virtio-serial-bus.c +7 -0
index f5bc173844..f0f18c8e7c 100644
@@ -637,6 +637,13 @@ static void set_status(VirtIODevice *vdev, uint8_t status)
      if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
          guest_reset(vser);
      }
+
+    QTAILQ_FOREACH(port, &vser->ports, next) {
+        VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
+        if (vsc->enable_backend) {
+            vsc->enable_backend(port, vdev->vm_running);
+        }
+    }
  }
 
  static void vser_reset(VirtIODevice *vdev)
include/hw/virtio/virtio-serial.h +3 -0
index b19c44727f..12657a9f39 100644
@@ -58,6 +58,9 @@ typedef struct VirtIOSerialPortClass {
          /* Guest opened/closed device. */
      void (*set_guest_connected)(VirtIOSerialPort *port, int guest_connected);
 
+    /* Enable/disable backend for virtio serial port */
+    void (*enable_backend)(VirtIOSerialPort *port, bool enable);
+
          /* Guest is now ready to accept data (virtqueues set up). */
      void (*guest_ready)(VirtIOSerialPort *port);
 
#10 lore raw
Quoted 8 lines · Pavel Butsykin
Quoted 5 lines
Quoted 1 line

To understand the patch better this doesn't fix _all_ stopped states, only migration, right? That said it's a valid bugfix even independent of the effects for stopped runstate.

Yes, the bug only appears on the migration. Actually, to protect memory during the migration, this approach is already used for other virtio devices, for example net_vhost, see virtio_net_vhost_status().

Cool, can you rebase?

Paolo