virtio: refactor host notifiers
cover virtio: refactor host notifiers
Hi,
this is a resend of the host notifier refactoring I last sent out on 2016/04/25. Just rebased on current master and pushed out to github.com/cohuck/qemu host-notifier-refactor.
Blurb from the last posting follows:
This still fixes an issue where we have no ioeventfd for a short time, but it does not break our necks any more with the recent fixes. Still, I think this is a net win, especially considering we remove a lot of similar code from the individual transports.
Cornelia Huck (6):
virtio-bus: common ioeventfd infrastructure
virtio-bus: have callers tolerate new host notifier api
virtio-ccw: convert to ioeventfd callbacks
virtio-pci: convert to ioeventfd callbacks
virtio-mmio: convert to ioeventfd callbacks
virtio-bus: remove old set_host_notifier callback
hw/block/dataplane/virtio-blk.c | 6 +- hw/s390x/virtio-ccw.c | 133 ++++++++++++++-------------------------- hw/scsi/virtio-scsi-dataplane.c | 9 ++- hw/virtio/vhost.c | 13 ++-- hw/virtio/virtio-bus.c | 132 +++++++++++++++++++++++++++++++++++++++ hw/virtio/virtio-mmio.c | 128 +++++++++++++------------------------- hw/virtio/virtio-pci.c | 124 +++++++++++++------------------------ include/hw/virtio/virtio-bus.h | 31 +++++++++- 8 files changed, 303 insertions(+), 273 deletions(-)
Signature · 1 line
2.6.6
PATCH 1/6 virtio-bus: common ioeventfd infrastructure
Introduce a set of ioeventfd callbacks on the virtio-bus level that can be implemented by the individual transports. At the virtio-bus level, do common handling for host notifiers (which is actually most of it).
Two things of note:
- When setting the host notifier, we only switch from/to the generic ioeventfd handler. This fixes a latent bug where we had no ioeventfd assigned for a certain window.
- We always iterate over all possible virtio queues, even though ccw (currently) has a lower limit. It does not really matter here.
--- hw/virtio/virtio-bus.c | 132 +++++++++++++++++++++++++++++++++++++++++ include/hw/virtio/virtio-bus.h | 30 ++++++++++ 2 files changed, 162 insertions(+)
hw/virtio/virtio-bus.c +132 -0
@@ -146,6 +146,138 @@ void virtio_bus_set_vdev_config(VirtioBusState *bus, uint8_t *config) } } +/* +* This function handles both assigning the ioeventfd handler and +* registering it with the kernel. +* assign: register/deregister ioeventfd with the kernel +* set_handler: use the generic ioeventfd handler +*/ +static int set_host_notifier_internal (DeviceState *proxy,VirtioBusState *bus, +int n,bool assign,bool set_handler) +{ +VirtIODevice *vdev =virtio_bus_get_device (bus); +VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (bus); +VirtQueue *vq =virtio_get_queue (vdev, n); +EventNotifier *notifier =virtio_queue_get_host_notifier (vq); +int r =0 ; + +if (assign) { + r =event_notifier_init (notifier,1 ); +if (r <0 ) { +error_report ("%s: unable to init event notifier: %d" , __func__, r); +return r; + } +virtio_queue_set_host_notifier_fd_handler (vq, true, set_handler); + r = k->ioeventfd_assign (proxy, notifier, n, assign); +if (r <0 ) { +error_report ("%s: unable to assign ioeventfd: %d" , __func__, r); +virtio_queue_set_host_notifier_fd_handler (vq, false, false); +event_notifier_cleanup (notifier); +return r; + } + }else { +virtio_queue_set_host_notifier_fd_handler (vq, false, false); + k->ioeventfd_assign (proxy, notifier, n, assign); +event_notifier_cleanup (notifier); + } +return r; +} + +void virtio_bus_start_ioeventfd (VirtioBusState *bus) +{ +VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (bus); +DeviceState *proxy =DEVICE (BUS (bus)->parent); +VirtIODevice *vdev; +int n, r; + +if (!k->ioeventfd_started || k->ioeventfd_started (proxy)) { +return ; + } +if (k->ioeventfd_disabled (proxy)) { +return ; + } + vdev =virtio_bus_get_device (bus); +for (n =0 ; n <VIRTIO_QUEUE_MAX ; n++) { +if (!virtio_queue_get_num (vdev, n)) { +continue ; + } + r =set_host_notifier_internal (proxy, bus, n, true, true); +if (r <0 ) { + goto assign_error; + } + } + k->ioeventfd_set_started (proxy, true, false); +return ; + +assign_error: +while (--n >=0 ) { +if (!virtio_queue_get_num (vdev, n)) { +continue ; + } + + r =set_host_notifier_internal (proxy, bus, n, false, false); +assert (r >=0 ); + } + k->ioeventfd_set_started (proxy, false, true); +error_report ("%s: failed. Fallback to userspace (slower)." , __func__); +} + +void virtio_bus_stop_ioeventfd (VirtioBusState *bus) +{ +VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (bus); +DeviceState *proxy =DEVICE (BUS (bus)->parent); +VirtIODevice *vdev; +int n, r; + +if (!k->ioeventfd_started || !k->ioeventfd_started (proxy)) { +return ; + } + vdev =virtio_bus_get_device (bus); +for (n =0 ; n <VIRTIO_QUEUE_MAX ; n++) { +if (!virtio_queue_get_num (vdev, n)) { +continue ; + } + r =set_host_notifier_internal (proxy, bus, n, false, false); +assert (r >=0 ); + } + k->ioeventfd_set_started (proxy, false, false); +} + +/* +* This function switches from/to the generic ioeventfd handler. +* assign==false means 'use generic ioeventfd handler'. +*/ +int virtio_bus_set_host_notifier (VirtioBusState *bus,int n,bool assign) +{ +VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (bus); +DeviceState *proxy =DEVICE (BUS (bus)->parent); +VirtIODevice *vdev =virtio_bus_get_device (bus); +VirtQueue *vq =virtio_get_queue (vdev, n); + +if (!k->ioeventfd_started) { +return -ENOSYS ; + } +if (assign) { +/* +* Stop using the generic ioeventfd, we are doing eventfd handling +* ourselves below +*/ + k->ioeventfd_set_disabled (proxy, true); + } +/* +* Just switch the handler, don't deassign the ioeventfd. +* Otherwise, there's a window where we don't have an +* ioeventfd and we may end up with a notification where +* we don't expect one. +*/ +virtio_queue_set_host_notifier_fd_handler (vq, assign, !assign); +if (!assign) { +/* Use generic ioeventfd handler again. */ + k->ioeventfd_set_disabled (proxy, false); + } +return 0 ; +} +static char *virtio_bus_get_dev_path (DeviceState *dev) {BusState *bus =qdev_get_parent_bus (dev);
include/hw/virtio/virtio-bus.h +30 -0
@@ -71,6 +71,29 @@ typedef struct VirtioBusClass {void (*device_unplugged)(DeviceState *d);int (*query_nvectors)(DeviceState *d);/* +* ioeventfd handling: if the transport implements ioeventfd_started, +* it must implement the other ioeventfd callbacks as well +*/ +/* Returns true if the ioeventfd has been started for the device. */ +bool (*ioeventfd_started)(DeviceState *d); +/* +* Sets the 'ioeventfd started' state after the ioeventfd has been +* started/stopped for the device. err signifies whether an error +* had occurred. +*/ +void (*ioeventfd_set_started)(DeviceState *d,bool started,bool err); +/* Returns true if the ioeventfd has been disabled for the device. */ +bool (*ioeventfd_disabled)(DeviceState *d); +/* Sets the 'ioeventfd disabled' state for the device. */ +void (*ioeventfd_set_disabled)(DeviceState *d,bool disabled); +/* +* Assigns/deassigns the ioeventfd backing for the transport on +* the device for queue number n. Returns an error value on +* failure. +*/ +int (*ioeventfd_assign)(DeviceState *d,EventNotifier *notifier, +int n,bool assign); +/* * Does the transport have variable vring alignment? * (ie can it ever call virtio_queue_set_align()?) * Note that changing this will break migration for this transport.
@@ -111,4 +134,11 @@ static inline VirtIODevice *virtio_bus_get_device(VirtioBusState *bus) return (VirtIODevice *)qdev; } +/* Start the ioeventfd. */ +void virtio_bus_start_ioeventfd (VirtioBusState *bus); +/* Stop the ioeventfd. */ +void virtio_bus_stop_ioeventfd (VirtioBusState *bus); +/* Switch from/to the generic ioeventfd handler */ +int virtio_bus_set_host_notifier (VirtioBusState *bus,int n,bool assign); +#endif /* VIRTIO_BUS_H */
Signature · 1 line
2.6.6
PATCH 2/6 virtio-bus: have callers tolerate new host notifier api
In reply to Cornelia HuckHave vhost and dataplane use the new api for transports that have been converted.
--- hw/block/dataplane/virtio-blk.c | 14 +++++++++++--- hw/scsi/virtio-scsi-dataplane.c | 20 +++++++++++++++----- hw/virtio/vhost.c | 20 ++++++++++++++++---- 3 files changed, 42 insertions(+), 12 deletions(-)
hw/block/dataplane/virtio-blk.c +11 -3
@@ -79,7 +79,8 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf, }/* Don't try if transport does not support notifiers. */ -if (!k->set_guest_notifiers || !k->set_host_notifier) { +if (!k->set_guest_notifiers || + (!k->set_host_notifier && !k->ioeventfd_started)) {error_setg (errp,"device is incompatible with dataplane " "(transport does not support notifiers)" );
@@ -157,7 +158,10 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s) s->guest_notifier =virtio_queue_get_guest_notifier (s->vq);/* Set up virtqueue notify */ - r = k->set_host_notifier (qbus->parent,0 , true); + r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus),0 , true); +if (r == -ENOSYS ) { + r = k->set_host_notifier (qbus->parent,0 , true); + }if (r !=0 ) {fprintf (stderr,"virtio-blk failed to set host notifier (%d)\n" , r); goto fail_host_notifier;
@@ -193,6 +197,7 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (s->vdev)));VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);VirtIOBlock *vblk =VIRTIO_BLK (s->vdev); +int r;if (!vblk->dataplane_started || s->stopping) {return ;
@@ -217,7 +222,10 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)aio_context_release (s->ctx); - k->set_host_notifier (qbus->parent,0 , false); + r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus),0 , false); +if (r == -ENOSYS ) { + k->set_host_notifier (qbus->parent,0 , false); + }/* Clean up guest notifier (irq) */ k->set_guest_notifiers (qbus->parent,1 , false);
hw/scsi/virtio-scsi-dataplane.c +15 -5
@@ -31,7 +31,8 @@ void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread) s->ctx =iothread_get_aio_context (vs->conf.iothread);/* Don't try if transport does not support notifiers. */ -if (!k->set_guest_notifiers || !k->set_host_notifier) { +if (!k->set_guest_notifiers || + (!k->set_host_notifier && !k->ioeventfd_started)) {fprintf (stderr,"virtio-scsi: Failed to set iothread " "(transport does not support notifiers)" );exit (1 );
@@ -73,7 +74,10 @@ static int virtio_scsi_vring_init(VirtIOSCSI *s, VirtQueue *vq, int n,int rc;/* Set up virtqueue notify */ - rc = k->set_host_notifier (qbus->parent, n, true); + rc =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), n, true); +if (rc == -ENOSYS ) { + rc = k->set_host_notifier (qbus->parent, n, true); + }if (rc !=0 ) {fprintf (stderr,"virtio-scsi: Failed to set host notifier (%d)\n" , rc);
@@ -159,7 +163,10 @@ fail_vrings:virtio_scsi_clear_aio (s);aio_context_release (s->ctx);for (i =0 ; i < vs->conf.num_queues +2 ; i++) { - k->set_host_notifier (qbus->parent, i, false); + rc =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), i, false); +if (rc == -ENOSYS ) { + k->set_host_notifier (qbus->parent, i, false); + } } k->set_guest_notifiers (qbus->parent, vs->conf.num_queues +2 , false); fail_guest_notifiers:
@@ -174,7 +181,7 @@ void virtio_scsi_dataplane_stop(VirtIOSCSI *s)BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (s)));VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (s); -int i; +int i, rc;if (!s->dataplane_started || s->dataplane_stopping) {return ;
@@ -198,7 +205,10 @@ void virtio_scsi_dataplane_stop(VirtIOSCSI *s)aio_context_release (s->ctx);for (i =0 ; i < vs->conf.num_queues +2 ; i++) { - k->set_host_notifier (qbus->parent, i, false); + rc =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), i, false); +if (rc == -ENOSYS ) { + k->set_host_notifier (qbus->parent, i, false); + } }/* Clean up guest notifier (irq) */
hw/virtio/vhost.c +16 -4
@@ -1110,14 +1110,18 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)VirtioBusState *vbus =VIRTIO_BUS (qbus);VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (vbus);int i, r, e; -if (!k->set_host_notifier) { +if (!k->set_host_notifier || !k->ioeventfd_started) {fprintf (stderr,"binding does not support host notifiers\n" ); r = -ENOSYS ; goto fail; }for (i =0 ; i < hdev->nvqs; ++i) { - r = k->set_host_notifier (qbus->parent, hdev->vq_index + i, true); + r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), hdev->vq_index + i, + true); +if (r == -ENOSYS ) { + r = k->set_host_notifier (qbus->parent, hdev->vq_index + i, true); + }if (r <0 ) {fprintf (stderr,"vhost VQ %d notifier binding failed: %d\n" , i, -r); goto fail_vq;
@@ -1127,7 +1131,11 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)return 0 ; fail_vq:while (--i >=0 ) { - e = k->set_host_notifier (qbus->parent, hdev->vq_index + i, false); + e =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), hdev->vq_index + i, + false); +if (e == -ENOSYS ) { + e = k->set_host_notifier (qbus->parent, hdev->vq_index + i, false); + }if (e <0 ) {fprintf (stderr,"vhost VQ %d notifier cleanup error: %d\n" , i, -r);fflush (stderr);
@@ -1151,7 +1159,11 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)int i, r;for (i =0 ; i < hdev->nvqs; ++i) { - r = k->set_host_notifier (qbus->parent, hdev->vq_index + i, false); + r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), hdev->vq_index + i, + false); +if (r == -ENOSYS ) { + r = k->set_host_notifier (qbus->parent, hdev->vq_index + i, false); + }if (r <0 ) {fprintf (stderr,"vhost VQ %d notifier cleanup failed: %d\n" , i, -r);fflush (stderr);
Signature · 1 line
2.6.6
PATCH 3/6 virtio-ccw: convert to ioeventfd callbacks
In reply to Cornelia HuckUse the new interface.
--- hw/s390x/virtio-ccw.c | 133 +++++++++++++++++--------------------------------- 1 file changed, 45 insertions(+), 88 deletions(-)
hw/s390x/virtio-ccw.c +45 -88
@@ -69,92 +69,58 @@ VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch)return vdev; } -static int virtio_ccw_set_guest2host_notifier (VirtioCcwDevice *dev,int n, -bool assign,bool set_handler) +static void virtio_ccw_start_ioeventfd (VirtioCcwDevice *dev) { -VirtIODevice *vdev =virtio_bus_get_device (&dev->bus); -VirtQueue *vq =virtio_get_queue (vdev, n); -EventNotifier *notifier =virtio_queue_get_host_notifier (vq); -int r =0 ; -SubchDev *sch = dev->sch; -uint32_t sch_id = (css_build_subchannel_id (sch) <<16 ) | sch->schid; +virtio_bus_start_ioeventfd (&dev->bus); +} -if (assign) { - r =event_notifier_init (notifier,1 ); -if (r <0 ) { -error_report ("%s: unable to init event notifier: %d" , __func__, r); -return r; - } -virtio_queue_set_host_notifier_fd_handler (vq, true, set_handler); - r =s390_assign_subch_ioeventfd (notifier, sch_id, n, assign); -if (r <0 ) { -error_report ("%s: unable to assign ioeventfd: %d" , __func__, r); -virtio_queue_set_host_notifier_fd_handler (vq, false, false); -event_notifier_cleanup (notifier); -return r; - } - }else { -virtio_queue_set_host_notifier_fd_handler (vq, false, false); -s390_assign_subch_ioeventfd (notifier, sch_id, n, assign); -event_notifier_cleanup (notifier); - } -return r; +static void virtio_ccw_stop_ioeventfd (VirtioCcwDevice *dev) +{ +virtio_bus_stop_ioeventfd (&dev->bus); } -static void virtio_ccw_start_ioeventfd (VirtioCcwDevice *dev) +static bool virtio_ccw_ioeventfd_started (DeviceState *d) { -VirtIODevice *vdev; -int n, r; +VirtioCcwDevice *dev =VIRTIO_CCW_DEVICE (d); -if (!(dev->flags &VIRTIO_CCW_FLAG_USE_IOEVENTFD ) || - dev->ioeventfd_disabled || - dev->ioeventfd_started) { -return ; - } - vdev =virtio_bus_get_device (&dev->bus); -for (n =0 ; n <VIRTIO_CCW_QUEUE_MAX ; n++) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } - r =virtio_ccw_set_guest2host_notifier (dev, n, true, true); -if (r <0 ) { - goto assign_error; - } - } - dev->ioeventfd_started = true; -return ; +return dev->ioeventfd_started; +} - assign_error: -while (--n >=0 ) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } - r =virtio_ccw_set_guest2host_notifier (dev, n, false, false); -assert (r >=0 ); +static void virtio_ccw_ioeventfd_set_started (DeviceState *d,bool started, +bool err) +{ +VirtioCcwDevice *dev =VIRTIO_CCW_DEVICE (d); + + dev->ioeventfd_started = started; +if (err) { +/* Disable ioeventfd for this device. */ + dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD ; } - dev->ioeventfd_started = false; -/* Disable ioeventfd for this device. */ - dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD ; -error_report ("%s: failed. Fallback to userspace (slower)." , __func__); } -static void virtio_ccw_stop_ioeventfd (VirtioCcwDevice *dev) +static bool virtio_ccw_ioeventfd_disabled (DeviceState *d) { -VirtIODevice *vdev; -int n, r; +VirtioCcwDevice *dev =VIRTIO_CCW_DEVICE (d); -if (!dev->ioeventfd_started) { -return ; - } - vdev =virtio_bus_get_device (&dev->bus); -for (n =0 ; n <VIRTIO_CCW_QUEUE_MAX ; n++) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } - r =virtio_ccw_set_guest2host_notifier (dev, n, false, false); -assert (r >=0 ); - } - dev->ioeventfd_started = false; +return dev->ioeventfd_disabled || + !(dev->flags &VIRTIO_CCW_FLAG_USE_IOEVENTFD ); +} + +static void virtio_ccw_ioeventfd_set_disabled (DeviceState *d,bool disabled) +{ +VirtioCcwDevice *dev =VIRTIO_CCW_DEVICE (d); + + dev->ioeventfd_disabled = disabled; +} + +static int virtio_ccw_ioeventfd_assign (DeviceState *d,EventNotifier *notifier, +int n,bool assign) +{ +VirtioCcwDevice *dev =VIRTIO_CCW_DEVICE (d); +SubchDev *sch = dev->sch; +uint32_t sch_id = (css_build_subchannel_id (sch) <<16 ) | sch->schid; + +return s390_assign_subch_ioeventfd (notifier, sch_id, n, assign); }VirtualCssBus *virtual_css_bus_init (void )
@@ -1195,19 +1161,6 @@ static bool virtio_ccw_query_guest_notifiers(DeviceState *d)return !!(dev->sch->curr_status.pmcw.flags &PMCW_FLAGS_MASK_ENA ); } -static int virtio_ccw_set_host_notifier (DeviceState *d,int n,bool assign) -{ -VirtioCcwDevice *dev =VIRTIO_CCW_DEVICE (d); - -/* Stop using the generic ioeventfd, we are doing eventfd handling -* ourselves below */ - dev->ioeventfd_disabled = assign; -if (assign) { -virtio_ccw_stop_ioeventfd (dev); - } -return virtio_ccw_set_guest2host_notifier (dev, n, assign, false); -} -static int virtio_ccw_get_mappings (VirtioCcwDevice *dev) {int r;
@@ -1836,7 +1789,6 @@ static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data) k->notify = virtio_ccw_notify; k->vmstate_change = virtio_ccw_vmstate_change; k->query_guest_notifiers = virtio_ccw_query_guest_notifiers; - k->set_host_notifier = virtio_ccw_set_host_notifier; k->set_guest_notifiers = virtio_ccw_set_guest_notifiers; k->save_queue = virtio_ccw_save_queue; k->load_queue = virtio_ccw_load_queue;
@@ -1845,6 +1797,11 @@ static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data) k->device_plugged = virtio_ccw_device_plugged; k->post_plugged = virtio_ccw_post_plugged; k->device_unplugged = virtio_ccw_device_unplugged; + k->ioeventfd_started = virtio_ccw_ioeventfd_started; + k->ioeventfd_set_started = virtio_ccw_ioeventfd_set_started; + k->ioeventfd_disabled = virtio_ccw_ioeventfd_disabled; + k->ioeventfd_set_disabled = virtio_ccw_ioeventfd_set_disabled; + k->ioeventfd_assign = virtio_ccw_ioeventfd_assign; }static const TypeInfo virtio_ccw_bus_info = {
Signature · 1 line
2.6.6
PATCH 4/6 virtio-pci: convert to ioeventfd callbacks
In reply to Cornelia HuckConvert to new interface.
--- hw/virtio/virtio-pci.c | 124 ++++++++++++++++--------------------------------- 1 file changed, 41 insertions(+), 83 deletions(-)
hw/virtio/virtio-pci.c +41 -83
@@ -262,14 +262,44 @@ static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)return 0 ; } +static bool virtio_pci_ioeventfd_started (DeviceState *d) +{ +VirtIOPCIProxy *proxy =to_virtio_pci_proxy (d); + +return proxy->ioeventfd_started; +} + +static void virtio_pci_ioeventfd_set_started (DeviceState *d,bool started, +bool err) +{ +VirtIOPCIProxy *proxy =to_virtio_pci_proxy (d); + + proxy->ioeventfd_started = started; +} + +static bool virtio_pci_ioeventfd_disabled (DeviceState *d) +{ +VirtIOPCIProxy *proxy =to_virtio_pci_proxy (d); + +return proxy->ioeventfd_disabled || + !(proxy->flags &VIRTIO_PCI_FLAG_USE_IOEVENTFD ); +} + +static void virtio_pci_ioeventfd_set_disabled (DeviceState *d,bool disabled) +{ +VirtIOPCIProxy *proxy =to_virtio_pci_proxy (d); + + proxy->ioeventfd_disabled = disabled; +} +#define QEMU_VIRTIO_PCI_QUEUE_MEM_MULT 0x1000 -static int virtio_pci_set_host_notifier_internal (VirtIOPCIProxy *proxy, -int n,bool assign,bool set_handler) +static int virtio_pci_ioeventfd_assign (DeviceState *d,EventNotifier *notifier, +int n,bool assign) { +VirtIOPCIProxy *proxy =to_virtio_pci_proxy (d);VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus);VirtQueue *vq =virtio_get_queue (vdev, n); -EventNotifier *notifier =virtio_queue_get_host_notifier (vq);bool legacy = !(proxy->flags &VIRTIO_PCI_FLAG_DISABLE_LEGACY );bool modern = !(proxy->flags &VIRTIO_PCI_FLAG_DISABLE_MODERN );bool fast_mmio =kvm_ioeventfd_any_length_enabled ();
@@ -280,16 +310,8 @@ static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,hwaddr modern_addr =QEMU_VIRTIO_PCI_QUEUE_MEM_MULT *virtio_get_queue_index (vq);hwaddr legacy_addr =VIRTIO_PCI_QUEUE_NOTIFY ; -int r =0 ;if (assign) { - r =event_notifier_init (notifier,1 ); -if (r <0 ) { -error_report ("%s: unable to init event notifier: %d" , - __func__, r); -return r; - } -virtio_queue_set_host_notifier_fd_handler (vq, true, set_handler);if (modern) {if (fast_mmio) { memory_region_add_eventfd(modern_mr, modern_addr, 0,
@@ -325,68 +347,18 @@ static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,memory_region_del_eventfd (legacy_mr, legacy_addr,2 , true, n, notifier); } -virtio_queue_set_host_notifier_fd_handler (vq, false, false); -event_notifier_cleanup (notifier); } -return r; +return 0 ; }static void virtio_pci_start_ioeventfd (VirtIOPCIProxy *proxy) { -VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus); -int n, r; - -if (!(proxy->flags &VIRTIO_PCI_FLAG_USE_IOEVENTFD ) || - proxy->ioeventfd_disabled || - proxy->ioeventfd_started) { -return ; - } - -for (n =0 ; n <VIRTIO_QUEUE_MAX ; n++) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } - - r =virtio_pci_set_host_notifier_internal (proxy, n, true, true); -if (r <0 ) { - goto assign_error; - } - } - proxy->ioeventfd_started = true; -return ; - -assign_error: -while (--n >=0 ) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } - - r =virtio_pci_set_host_notifier_internal (proxy, n, false, false); -assert (r >=0 ); - } - proxy->ioeventfd_started = false; -error_report ("%s: failed. Fallback to a userspace (slower)." , __func__); +virtio_bus_start_ioeventfd (&proxy->bus); }static void virtio_pci_stop_ioeventfd (VirtIOPCIProxy *proxy) { -VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus); -int r; -int n; - -if (!proxy->ioeventfd_started) { -return ; - } - -for (n =0 ; n <VIRTIO_QUEUE_MAX ; n++) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } - - r =virtio_pci_set_host_notifier_internal (proxy, n, false, false); -assert (r >=0 ); - } - proxy->ioeventfd_started = false; +virtio_bus_stop_ioeventfd (&proxy->bus); }static void virtio_ioport_write (void *opaque,uint32_t addr,uint32_t val)
@@ -1112,24 +1084,6 @@ assign_error:return r; } -static int virtio_pci_set_host_notifier (DeviceState *d,int n,bool assign) -{ -VirtIOPCIProxy *proxy =to_virtio_pci_proxy (d); - -/* Stop using ioeventfd for virtqueue kick if the device starts using host -* notifiers. This makes it easy to avoid stepping on each others' toes. -*/ - proxy->ioeventfd_disabled = assign; -if (assign) { -virtio_pci_stop_ioeventfd (proxy); - } -/* We don't need to start here: it's not needed because backend -* currently only stops on status change away from ok, -* reset, vmstop and such. If we do add code to start here, -* need to check vmstate, device state etc. */ -return virtio_pci_set_host_notifier_internal (proxy, n, assign, false); -} -static void virtio_pci_vmstate_change (DeviceState *d,bool running) {VirtIOPCIProxy *proxy =to_virtio_pci_proxy (d);
@@ -2490,12 +2444,16 @@ static void virtio_pci_bus_class_init(ObjectClass *klass, void *data) k->load_extra_state = virtio_pci_load_extra_state; k->has_extra_state = virtio_pci_has_extra_state; k->query_guest_notifiers = virtio_pci_query_guest_notifiers; - k->set_host_notifier = virtio_pci_set_host_notifier; k->set_guest_notifiers = virtio_pci_set_guest_notifiers; k->vmstate_change = virtio_pci_vmstate_change; k->device_plugged = virtio_pci_device_plugged; k->device_unplugged = virtio_pci_device_unplugged; k->query_nvectors = virtio_pci_query_nvectors; + k->ioeventfd_started = virtio_pci_ioeventfd_started; + k->ioeventfd_set_started = virtio_pci_ioeventfd_set_started; + k->ioeventfd_disabled = virtio_pci_ioeventfd_disabled; + k->ioeventfd_set_disabled = virtio_pci_ioeventfd_set_disabled; + k->ioeventfd_assign = virtio_pci_ioeventfd_assign; }static const TypeInfo virtio_pci_bus_info = {
Signature · 1 line
2.6.6
PATCH 5/6 virtio-mmio: convert to ioeventfd callbacks
In reply to Cornelia HuckConvert to the new interface.
--- hw/virtio/virtio-mmio.c | 128 ++++++++++++++++-------------------------------- 1 file changed, 41 insertions(+), 87 deletions(-)
hw/virtio/virtio-mmio.c +41 -87
@@ -93,90 +93,59 @@ typedef struct {bool ioeventfd_started; } VirtIOMMIOProxy; -static int virtio_mmio_set_host_notifier_internal (VirtIOMMIOProxy *proxy, -int n,bool assign, -bool set_handler) +static bool virtio_mmio_ioeventfd_started (DeviceState *d) { -VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus); -VirtQueue *vq =virtio_get_queue (vdev, n); -EventNotifier *notifier =virtio_queue_get_host_notifier (vq); -int r =0 ; +VirtIOMMIOProxy *proxy =VIRTIO_MMIO (d); -if (assign) { - r =event_notifier_init (notifier,1 ); -if (r <0 ) { -error_report ("%s: unable to init event notifier: %d" , - __func__, r); -return r; - } -virtio_queue_set_host_notifier_fd_handler (vq, true, set_handler); -memory_region_add_eventfd (&proxy->iomem,VIRTIO_MMIO_QUEUENOTIFY ,4 , - true, n, notifier); - }else { -memory_region_del_eventfd (&proxy->iomem,VIRTIO_MMIO_QUEUENOTIFY ,4 , - true, n, notifier); -virtio_queue_set_host_notifier_fd_handler (vq, false, false); -event_notifier_cleanup (notifier); - } -return r; +return proxy->ioeventfd_started; } -static void virtio_mmio_start_ioeventfd (VirtIOMMIOProxy *proxy) +static void virtio_mmio_ioeventfd_set_started (DeviceState *d,bool started, +bool err) { -VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus); -int n, r; +VirtIOMMIOProxy *proxy =VIRTIO_MMIO (d); -if (!kvm_eventfds_enabled () || - proxy->ioeventfd_disabled || - proxy->ioeventfd_started) { -return ; - } + proxy->ioeventfd_started = started; +} -for (n =0 ; n <VIRTIO_QUEUE_MAX ; n++) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } +static bool virtio_mmio_ioeventfd_disabled (DeviceState *d) +{ +VirtIOMMIOProxy *proxy =VIRTIO_MMIO (d); - r =virtio_mmio_set_host_notifier_internal (proxy, n, true, true); -if (r <0 ) { - goto assign_error; - } - } - proxy->ioeventfd_started = true; -return ; +return !kvm_eventfds_enabled () || proxy->ioeventfd_disabled; +} -assign_error: -while (--n >=0 ) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } +static void virtio_mmio_ioeventfd_set_disabled (DeviceState *d,bool disabled) +{ +VirtIOMMIOProxy *proxy =VIRTIO_MMIO (d); - r =virtio_mmio_set_host_notifier_internal (proxy, n, false, false); -assert (r >=0 ); - } - proxy->ioeventfd_started = false; -error_report ("%s: failed. Fallback to a userspace (slower)." , __func__); + proxy->ioeventfd_disabled = disabled; } -static void virtio_mmio_stop_ioeventfd (VirtIOMMIOProxy *proxy) +static int virtio_mmio_ioeventfd_assign (DeviceState *d, +EventNotifier *notifier, +int n,bool assign) { -int r; -int n; -VirtIODevice *vdev =virtio_bus_get_device (&proxy->bus); +VirtIOMMIOProxy *proxy =VIRTIO_MMIO (d); -if (!proxy->ioeventfd_started) { -return ; +if (assign) { +memory_region_add_eventfd (&proxy->iomem,VIRTIO_MMIO_QUEUENOTIFY ,4 , + true, n, notifier); + }else { +memory_region_del_eventfd (&proxy->iomem,VIRTIO_MMIO_QUEUENOTIFY ,4 , + true, n, notifier); } +return 0 ; +} -for (n =0 ; n <VIRTIO_QUEUE_MAX ; n++) { -if (!virtio_queue_get_num (vdev, n)) { -continue ; - } +static void virtio_mmio_start_ioeventfd (VirtIOMMIOProxy *proxy) +{ +virtio_bus_start_ioeventfd (&proxy->bus); +} - r =virtio_mmio_set_host_notifier_internal (proxy, n, false, false); -assert (r >=0 ); - } - proxy->ioeventfd_started = false; +static void virtio_mmio_stop_ioeventfd (VirtIOMMIOProxy *proxy) +{ +virtio_bus_stop_ioeventfd (&proxy->bus); }static uint64_t virtio_mmio_read (void *opaque,hwaddr offset,unsigned size)
@@ -498,25 +467,6 @@ assign_error:return r; } -static int virtio_mmio_set_host_notifier (DeviceState *opaque,int n, -bool assign) -{ -VirtIOMMIOProxy *proxy =VIRTIO_MMIO (opaque); - -/* Stop using ioeventfd for virtqueue kick if the device starts using host -* notifiers. This makes it easy to avoid stepping on each others' toes. -*/ - proxy->ioeventfd_disabled = assign; -if (assign) { -virtio_mmio_stop_ioeventfd (proxy); - } -/* We don't need to start here: it's not needed because backend -* currently only stops on status change away from ok, -* reset, vmstop and such. If we do add code to start here, -* need to check vmstate, device state etc. */ -return virtio_mmio_set_host_notifier_internal (proxy, n, assign, false); -} -/* virtio-mmio device */ static void virtio_mmio_realizefn (DeviceState *d,Error **errp)
@@ -558,8 +508,12 @@ static void virtio_mmio_bus_class_init(ObjectClass *klass, void *data) k->notify = virtio_mmio_update_irq; k->save_config = virtio_mmio_save_config; k->load_config = virtio_mmio_load_config; - k->set_host_notifier = virtio_mmio_set_host_notifier; k->set_guest_notifiers = virtio_mmio_set_guest_notifiers; + k->ioeventfd_started = virtio_mmio_ioeventfd_started; + k->ioeventfd_set_started = virtio_mmio_ioeventfd_set_started; + k->ioeventfd_disabled = virtio_mmio_ioeventfd_disabled; + k->ioeventfd_set_disabled = virtio_mmio_ioeventfd_set_disabled; + k->ioeventfd_assign = virtio_mmio_ioeventfd_assign; k->has_variable_vring_alignment = true; bus_class->max_dev =1 ; }
Signature · 1 line
2.6.6
PATCH 6/6 virtio-bus: remove old set_host_notifier callback
In reply to Cornelia HuckAll users have been converted to the new ioevent callbacks.
--- hw/block/dataplane/virtio-blk.c | 12 ++---------- hw/scsi/virtio-scsi-dataplane.c | 19 ++++--------------- hw/virtio/vhost.c | 13 +------------ include/hw/virtio/virtio-bus.h | 1 - 4 files changed, 7 insertions(+), 38 deletions(-)
hw/block/dataplane/virtio-blk.c +2 -10
@@ -79,8 +79,7 @@ void virtio_blk_data_plane_create(VirtIODevice *vdev, VirtIOBlkConf *conf, }/* Don't try if transport does not support notifiers. */ -if (!k->set_guest_notifiers || - (!k->set_host_notifier && !k->ioeventfd_started)) { +if (!k->set_guest_notifiers || !k->ioeventfd_started) {error_setg (errp,"device is incompatible with dataplane " "(transport does not support notifiers)" );
@@ -159,9 +158,6 @@ void virtio_blk_data_plane_start(VirtIOBlockDataPlane *s)/* Set up virtqueue notify */ r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus),0 , true); -if (r == -ENOSYS ) { - r = k->set_host_notifier (qbus->parent,0 , true); - }if (r !=0 ) {fprintf (stderr,"virtio-blk failed to set host notifier (%d)\n" , r); goto fail_host_notifier;
@@ -197,7 +193,6 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (s->vdev)));VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);VirtIOBlock *vblk =VIRTIO_BLK (s->vdev); -int r;if (!vblk->dataplane_started || s->stopping) {return ;
@@ -222,10 +217,7 @@ void virtio_blk_data_plane_stop(VirtIOBlockDataPlane *s)aio_context_release (s->ctx); - r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus),0 , false); -if (r == -ENOSYS ) { - k->set_host_notifier (qbus->parent,0 , false); - } +virtio_bus_set_host_notifier (VIRTIO_BUS (qbus),0 , false);/* Clean up guest notifier (irq) */ k->set_guest_notifiers (qbus->parent,1 , false);
hw/scsi/virtio-scsi-dataplane.c +4 -15
@@ -31,8 +31,7 @@ void virtio_scsi_set_iothread(VirtIOSCSI *s, IOThread *iothread) s->ctx =iothread_get_aio_context (vs->conf.iothread);/* Don't try if transport does not support notifiers. */ -if (!k->set_guest_notifiers || - (!k->set_host_notifier && !k->ioeventfd_started)) { +if (!k->set_guest_notifiers || !k->ioeventfd_started) {fprintf (stderr,"virtio-scsi: Failed to set iothread " "(transport does not support notifiers)" );exit (1 );
@@ -70,14 +69,10 @@ static int virtio_scsi_vring_init(VirtIOSCSI *s, VirtQueue *vq, int n, void (*fn)(VirtIODevice *vdev, VirtQueue *vq)) {BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (s))); -VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);int rc;/* Set up virtqueue notify */ rc =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), n, true); -if (rc == -ENOSYS ) { - rc = k->set_host_notifier (qbus->parent, n, true); - }if (rc !=0 ) {fprintf (stderr,"virtio-scsi: Failed to set host notifier (%d)\n" , rc);
@@ -163,10 +158,7 @@ fail_vrings:virtio_scsi_clear_aio (s);aio_context_release (s->ctx);for (i =0 ; i < vs->conf.num_queues +2 ; i++) { - rc =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), i, false); -if (rc == -ENOSYS ) { - k->set_host_notifier (qbus->parent, i, false); - } +virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), i, false); } k->set_guest_notifiers (qbus->parent, vs->conf.num_queues +2 , false); fail_guest_notifiers:
@@ -181,7 +173,7 @@ void virtio_scsi_dataplane_stop(VirtIOSCSI *s)BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (s)));VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (qbus);VirtIOSCSICommon *vs =VIRTIO_SCSI_COMMON (s); -int i, rc; +int i;if (!s->dataplane_started || s->dataplane_stopping) {return ;
@@ -205,10 +197,7 @@ void virtio_scsi_dataplane_stop(VirtIOSCSI *s)aio_context_release (s->ctx);for (i =0 ; i < vs->conf.num_queues +2 ; i++) { - rc =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), i, false); -if (rc == -ENOSYS ) { - k->set_host_notifier (qbus->parent, i, false); - } +virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), i, false); }/* Clean up guest notifier (irq) */
hw/virtio/vhost.c +1 -12
@@ -1110,7 +1110,7 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)VirtioBusState *vbus =VIRTIO_BUS (qbus);VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (vbus);int i, r, e; -if (!k->set_host_notifier || !k->ioeventfd_started) { +if (!k->ioeventfd_started) {fprintf (stderr,"binding does not support host notifiers\n" ); r = -ENOSYS ; goto fail;
@@ -1119,9 +1119,6 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)for (i =0 ; i < hdev->nvqs; ++i) { r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), hdev->vq_index + i, true); -if (r == -ENOSYS ) { - r = k->set_host_notifier (qbus->parent, hdev->vq_index + i, true); - }if (r <0 ) {fprintf (stderr,"vhost VQ %d notifier binding failed: %d\n" , i, -r); goto fail_vq;
@@ -1133,9 +1130,6 @@ fail_vq:while (--i >=0 ) { e =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), hdev->vq_index + i, false); -if (e == -ENOSYS ) { - e = k->set_host_notifier (qbus->parent, hdev->vq_index + i, false); - }if (e <0 ) {fprintf (stderr,"vhost VQ %d notifier cleanup error: %d\n" , i, -r);fflush (stderr);
@@ -1154,16 +1148,11 @@ fail:void vhost_dev_disable_notifiers (struct vhost_dev *hdev,VirtIODevice *vdev) {BusState *qbus =BUS (qdev_get_parent_bus (DEVICE (vdev))); -VirtioBusState *vbus =VIRTIO_BUS (qbus); -VirtioBusClass *k =VIRTIO_BUS_GET_CLASS (vbus);int i, r;for (i =0 ; i < hdev->nvqs; ++i) { r =virtio_bus_set_host_notifier (VIRTIO_BUS (qbus), hdev->vq_index + i, false); -if (r == -ENOSYS ) { - r = k->set_host_notifier (qbus->parent, hdev->vq_index + i, false); - }if (r <0 ) {fprintf (stderr,"vhost VQ %d notifier cleanup failed: %d\n" , i, -r);fflush (stderr);
include/hw/virtio/virtio-bus.h +0 -1
@@ -52,7 +52,6 @@ typedef struct VirtioBusClass {bool (*has_extra_state)(DeviceState *d);bool (*query_guest_notifiers)(DeviceState *d);int (*set_guest_notifiers)(DeviceState *d,int nvqs,bool assign); -int (*set_host_notifier)(DeviceState *d,int n,bool assigned);void (*vmstate_change)(DeviceState *d,bool running);/* * transport independent init function.
Signature · 1 line
2.6.6
Quoted 6 lines · Cornelia Huck
Hi,
this is a resend of the host notifier refactoring I last sent out on 2016/04/25. Just rebased on current master and pushed out to github.com/cohuck/qemu host-notifier-refactor.
Quoted 6 lines
Blurb from the last posting follows:
This still fixes an issue where we have no ioeventfd for a short time, but it does not break our necks any more with the recent fixes. Still, I think this is a net win, especially considering we remove a lot of similar code from the individual transports.
Agreed. And the changes look good to me, thanks.
Quoted 22 lines
Cornelia Huck (6):
virtio-bus: common ioeventfd infrastructure
virtio-bus: have callers tolerate new host notifier api
virtio-ccw: convert to ioeventfd callbacks
virtio-pci: convert to ioeventfd callbacks
virtio-mmio: convert to ioeventfd callbacks
virtio-bus: remove oldset_host_notifiercallbackhw/block/dataplane/virtio-blk.c | 6 +- hw/s390x/virtio-ccw.c | 133 ++++++++++++++-------------------------- hw/scsi/virtio-scsi-dataplane.c | 9 ++- hw/virtio/vhost.c | 13 ++-- hw/virtio/virtio-bus.c | 132 +++++++++++++++++++++++++++++++++++++++ hw/virtio/virtio-mmio.c | 128 +++++++++++++------------------------- hw/virtio/virtio-pci.c | 124 +++++++++++++------------------------ include/hw/virtio/virtio-bus.h | 31 +++++++++- 8 files changed, 303 insertions(+), 273 deletions(-)Signature · 1 line
2.6.6
Quoted 21 lines · Cornelia Huck
Hi,
this is a resend of the host notifier refactoring I last sent out on 2016/04/25. Just rebased on current master and pushed out to github.com/cohuck/qemu host-notifier-refactor.
Blurb from the last posting follows:
This still fixes an issue where we have no ioeventfd for a short time, but it does not break our necks any more with the recent fixes. Still, I think this is a net win, especially considering we remove a lot of similar code from the individual transports.
Cornelia Huck (6):
virtio-bus: common ioeventfd infrastructure
virtio-bus: have callers tolerate new host notifier api
virtio-ccw: convert to ioeventfd callbacks
virtio-pci: convert to ioeventfd callbacks
virtio-mmio: convert to ioeventfd callbacks
virtio-bus: remove oldset_host_notifiercallback
Quoted 1 line
hw/block/dataplane/virtio-blk.c | 6 +-
Reviewed just the small dataplane/virtio-blk.c portion but agree with the series in general: