virtio: move common part of _set_guest_notifier to generic code
virtio-pci, virtio-mmio and virtio-ccw handle config notifier equally but with different code (mmio adds a separate function, when pci use common function). Let's chose the more compact way (pci) and reuse it for mmio. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> Acked-by: Eric Farman <farman@linux.ibm.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20260420200339.708640-8-vsementsov@yandex-team.ru>
Vladimir Sementsov-Ogievskiy committed
Apr 20, 2026 at 23:03 UTC
a8f6b13f068b753d16588630654dfd51d7009436
6 files changed
+82
-93
hw/s390x/virtio-ccw.c
+17
-18
@@ -1025,20 +1025,27 @@ static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1025
VirtQueue *vq = virtio_get_queue(vdev, n);
1026
EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
1027
VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
1028
+ int r;
1029
1029
- if (assign) {
1030
- int r = event_notifier_init(notifier, 0);
1031
-
1032
- if (r < 0) {
1033
- return r;
1030
+ if (!assign) {
1031
+ if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1032
+ k->guest_notifier_mask(vdev, n, true);
1033
}
1035
- virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
1034
+ if (with_irqfd) {
1035
+ virtio_ccw_remove_irqfd(dev, n);
1036
+ }
1037
+ }
1038
+
1039
+ r = virtio_set_guest_notifier(vdev, n, assign, with_irqfd);
1040
+ if (r < 0) {
1041
+ return r;
1042
+ }
1043
+
1044
+ if (assign) {
1045
if (with_irqfd) {
1046
r = virtio_ccw_add_irqfd(dev, n);
1047
if (r) {
1039
- virtio_queue_set_guest_notifier_fd_handler(vq, false,
1040
- with_irqfd);
1041
- event_notifier_cleanup(notifier);
1048
+ virtio_set_guest_notifier(vdev, n, false, with_irqfd);
1049
return r;
1050
}
1051
}
@@ -1054,16 +1061,8 @@ static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n,
1061
k->guest_notifier_pending(vdev, n)) {
1062
event_notifier_set(notifier);
1063
}
1057
- } else {
1058
- if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) {
1059
- k->guest_notifier_mask(vdev, n, true);
1060
- }
1061
- if (with_irqfd) {
1062
- virtio_ccw_remove_irqfd(dev, n);
1063
- }
1064
- virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
1065
- event_notifier_cleanup(notifier);
1064
}
1065
+
1066
return 0;
1067
}
1068
hw/virtio/virtio-mmio.c
+6
-35
@@ -670,18 +670,11 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
670
VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
671
VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
672
VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
673
- VirtQueue *vq = virtio_get_queue(vdev, n);
674
- EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
673
+ int r;
674
676
- if (assign) {
677
- int r = event_notifier_init(notifier, 0);
678
- if (r < 0) {
679
- return r;
680
- }
681
- virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
682
- } else {
683
- virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
684
- event_notifier_cleanup(notifier);
675
+ r = virtio_set_guest_notifier(vdev, n, assign, with_irqfd);
676
+ if (r < 0) {
677
+ return r;
678
}
679
680
if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
@@ -690,30 +683,7 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign,
683
684
return 0;
685
}
693
-static int virtio_mmio_set_config_guest_notifier(DeviceState *d, bool assign,
694
- bool with_irqfd)
695
-{
696
- VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d);
697
- VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
698
- VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
699
- EventNotifier *notifier = virtio_config_get_guest_notifier(vdev);
700
- int r = 0;
686
702
- if (assign) {
703
- r = event_notifier_init(notifier, 0);
704
- if (r < 0) {
705
- return r;
706
- }
707
- virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd);
708
- } else {
709
- virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd);
710
- event_notifier_cleanup(notifier);
711
- }
712
- if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) {
713
- vdc->guest_notifier_mask(vdev, VIRTIO_CONFIG_IRQ_IDX, !assign);
714
- }
715
- return r;
716
-}
687
static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
688
bool assign)
689
{
@@ -735,7 +705,8 @@ static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs,
705
goto assign_error;
706
}
707
}
738
- r = virtio_mmio_set_config_guest_notifier(d, assign, with_irqfd);
708
+ r = virtio_mmio_set_guest_notifier(d, VIRTIO_CONFIG_IRQ_IDX, assign,
709
+ with_irqfd);
710
if (r < 0) {
711
goto assign_error;
712
}
hw/virtio/virtio-pci.c
+4
-30
@@ -1208,43 +1208,17 @@ static void virtio_pci_vector_poll(PCIDevice *dev,
1208
}
1209
}
1210
1211
-void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq,
1212
- int n, bool assign,
1213
- bool with_irqfd)
1214
-{
1215
- if (n == VIRTIO_CONFIG_IRQ_IDX) {
1216
- virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd);
1217
- } else {
1218
- virtio_queue_set_guest_notifier_fd_handler(vq, assign, with_irqfd);
1219
- }
1220
-}
1221
-
1211
static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
1212
bool with_irqfd)
1213
{
1214
VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
1215
VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus);
1216
VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
1228
- VirtQueue *vq = NULL;
1229
- EventNotifier *notifier = NULL;
1217
+ int r;
1218
1231
- if (n == VIRTIO_CONFIG_IRQ_IDX) {
1232
- notifier = virtio_config_get_guest_notifier(vdev);
1233
- } else {
1234
- vq = virtio_get_queue(vdev, n);
1235
- notifier = virtio_queue_get_guest_notifier(vq);
1236
- }
1237
-
1238
- if (assign) {
1239
- int r = event_notifier_init(notifier, 0);
1240
- if (r < 0) {
1241
- return r;
1242
- }
1243
- virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, true, with_irqfd);
1244
- } else {
1245
- virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, false,
1246
- with_irqfd);
1247
- event_notifier_cleanup(notifier);
1219
+ r = virtio_set_guest_notifier(vdev, n, assign, with_irqfd);
1220
+ if (r < 0) {
1221
+ return r;
1222
}
1223
1224
if (!msix_enabled(&proxy->pci_dev) &&
hw/virtio/virtio.c
+44
-3
@@ -3835,8 +3835,10 @@ static void virtio_config_guest_notifier_read(EventNotifier *n)
3835
virtio_notify_config(vdev);
3836
}
3837
}
3838
-void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
3839
- bool with_irqfd)
3838
+
3839
+static void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq,
3840
+ bool assign,
3841
+ bool with_irqfd)
3842
{
3843
if (assign && !with_irqfd) {
3844
event_notifier_set_handler(&vq->guest_notifier,
@@ -3851,7 +3853,7 @@ void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
3853
}
3854
}
3855
3854
-void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev,
3856
+static void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev,
3857
bool assign, bool with_irqfd)
3858
{
3859
EventNotifier *n;
@@ -3868,6 +3870,45 @@ void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev,
3870
}
3871
}
3872
3873
+static void virtio_set_guest_notifier_fd_handler(VirtIODevice *vdev,
3874
+ VirtQueue *vq,
3875
+ int n, bool assign,
3876
+ bool with_irqfd)
3877
+{
3878
+ if (n == VIRTIO_CONFIG_IRQ_IDX) {
3879
+ virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd);
3880
+ } else {
3881
+ virtio_queue_set_guest_notifier_fd_handler(vq, assign, with_irqfd);
3882
+ }
3883
+}
3884
+
3885
+int virtio_set_guest_notifier(VirtIODevice *vdev, int n, bool assign,
3886
+ bool with_irqfd)
3887
+{
3888
+ VirtQueue *vq = NULL;
3889
+ EventNotifier *notifier = NULL;
3890
+
3891
+ if (n == VIRTIO_CONFIG_IRQ_IDX) {
3892
+ notifier = virtio_config_get_guest_notifier(vdev);
3893
+ } else {
3894
+ vq = virtio_get_queue(vdev, n);
3895
+ notifier = virtio_queue_get_guest_notifier(vq);
3896
+ }
3897
+
3898
+ if (assign) {
3899
+ int r = event_notifier_init(notifier, 0);
3900
+ if (r < 0) {
3901
+ return r;
3902
+ }
3903
+ virtio_set_guest_notifier_fd_handler(vdev, vq, n, true, with_irqfd);
3904
+ } else {
3905
+ virtio_set_guest_notifier_fd_handler(vdev, vq, n, false, with_irqfd);
3906
+ event_notifier_cleanup(notifier);
3907
+ }
3908
+
3909
+ return 0;
3910
+}
3911
+
3912
EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
3913
{
3914
return &vq->guest_notifier;
include/hw/virtio/virtio-pci.h
-3
@@ -250,9 +250,6 @@ void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t);
250
* @fixed_queues.
251
*/
252
unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues);
253
-void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq,
254
- int n, bool assign,
255
- bool with_irqfd);
253
254
int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset,
255
uint64_t length, uint8_t id);
include/hw/virtio/virtio.h
+11
-4
@@ -420,8 +420,6 @@ void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
420
VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
421
uint16_t virtio_get_queue_index(VirtQueue *vq);
422
EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
423
-void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
424
- bool with_irqfd);
423
int virtio_device_start_ioeventfd(VirtIODevice *vdev);
424
int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
425
void virtio_device_release_ioeventfd(VirtIODevice *vdev);
@@ -435,8 +433,17 @@ void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx);
433
VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
434
VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
435
EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev);
438
-void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev,
439
- bool assign, bool with_irqfd);
436
+
437
+/**
438
+ * virtio_set_guest_notifier - set/unset queue or config guest notifier
439
+ *
440
+ * @vdev: the VirtIO device
441
+ * @n: queue number, or VIRTIO_CONFIG_IRQ_IDX to set config notifer
442
+ * @assign: true to set notifier, false to unset
443
+ * @with_irqfd: irqfd enabled
444
+ */
445
+int virtio_set_guest_notifier(VirtIODevice *vdev, int n, bool assign,
446
+ bool with_irqfd);
447
448
static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
449
{