Make event_notifier more useful and more used
cover Make event_notifier more useful and more used
Hi,
this patch adds all the eventfd bells and whistles from vl.c/cpus.c to event_notifier, including pipe emulation and Win32 support. It then modifies the iothread code to use it instead.
Paolo Bonzini (8):
move event_notifier into the main directory
add event_notifier_set
remove event_notifier_test
add and use virtqueue_from_guest_notifier
add and use event_notifier_set_handler
enable event_notifier to use pipes
add Win32 implementation of event notifiers
change ioevent to use event notifiers
cpus.c | 95 +++----------------------------------- event_notifier.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ event_notifier.h | 28 +++++++++++ hw/event_notifier.c | 62 ------------------------- hw/event_notifier.h | 16 ------- hw/virtio-pci.c | 11 ++--- hw/virtio.c | 5 ++ hw/virtio.h | 1 + 8 files changed, 171 insertions(+), 171 deletions(-) create mode 100644 event_notifier.c create mode 100644 event_notifier.h delete mode 100644 hw/event_notifier.c delete mode 100644 hw/event_notifier.h
PATCH 1/8 move event_notifier into the main directory
--- hw/event_notifier.c => event_notifier.c | 1 - hw/event_notifier.h => event_notifier.h | 0 2 files changed, 0 insertions(+), 1 deletions(-) rename hw/event_notifier.c => event_notifier.c (98%) rename hw/event_notifier.h => event_notifier.h (100%)
event_notifier.c renamed from hw/event_notifier.c +0 -1
@@ -10,7 +10,6 @@* the COPYING file in the top-level directory. */ -#include "hw.h" #include "event_notifier.h" #ifdef CONFIG_EVENTFD #include <sys/eventfd.h>
event_notifier.h renamed from hw/event_notifier.h +0 -0
Signature · 1 line
1.6.6.1
PATCH 2/8 add event_notifier_set
In reply to Paolo Bonzini--- event_notifier.c | 7 +++++++ event_notifier.h | 1 + 2 files changed, 8 insertions(+), 0 deletions(-)
event_notifier.c +7 -0
@@ -38,6 +38,13 @@ int event_notifier_get_fd(EventNotifier *e)return e->fd; } +int event_notifier_set (EventNotifier *e) +{ +uint64_t value =1 ; +int r =write (e->fd, &value,sizeof (value)); +return r ==sizeof (value); +} +int event_notifier_test_and_clear (EventNotifier *e) {uint64_t value;
event_notifier.h +1 -0
@@ -10,6 +10,7 @@ struct EventNotifier {int event_notifier_init (EventNotifier *,int active);void event_notifier_cleanup (EventNotifier *);int event_notifier_get_fd (EventNotifier *); +int event_notifier_set (EventNotifier *);int event_notifier_test_and_clear (EventNotifier *);int event_notifier_test (EventNotifier *);
Signature · 1 line
1.6.6.1
PATCH 3/8 remove event_notifier_test
In reply to Paolo BonziniThis is broken; since the eventfd is used in nonblocking mode there is a race between reading and writing.
--- event_notifier.c | 15 --------------- event_notifier.h | 1 - 2 files changed, 0 insertions(+), 16 deletions(-)
event_notifier.c +0 -15
@@ -51,18 +51,3 @@ int event_notifier_test_and_clear(EventNotifier *e)int r =read (e->fd, &value,sizeof (value));return r ==sizeof (value); } - -int event_notifier_test (EventNotifier *e) -{ -uint64_t value; -int r =read (e->fd, &value,sizeof (value)); -if (r ==sizeof (value)) { -/* restore previous value. */ -int s =write (e->fd, &value,sizeof (value)); -/* never blocks because we use EFD_SEMAPHORE. -* If we didn't we'd get EAGAIN on overflow -* and we'd have to write code to ignore it. */ -assert (s ==sizeof (value)); - } -return r ==sizeof (value); -}
event_notifier.h +0 -1
@@ -12,6 +12,5 @@ void event_notifier_cleanup(EventNotifier *);int event_notifier_get_fd (EventNotifier *);int event_notifier_set (EventNotifier *);int event_notifier_test_and_clear (EventNotifier *); -int event_notifier_test (EventNotifier *);#endif
Signature · 1 line
1.6.6.1
PATCH 4/8 add and use virtqueue_from_guest_notifier
In reply to Paolo BonziniThis changes the opaque pointer passed to the handler, from being the virtqueue to being the eventnotifier. It is useful as soon as the eventnotifier will be able to set its own (type-safe) handler.
---
I don't have a vhost-enabled machine yet. So only compile-tested for now, but pretty trivial.
hw/virtio-pci.c | 6 +++--- hw/virtio.c | 5 +++++ hw/virtio.h | 1 + 3 files changed, 9 insertions(+), 3 deletions(-)
hw/virtio-pci.c +3 -3
@@ -421,8 +421,8 @@ static unsigned virtio_pci_get_features(void *opaque)static void virtio_pci_guest_notifier_read (void *opaque) { -VirtQueue *vq = opaque; -EventNotifier *n =virtio_queue_get_guest_notifier (vq); +EventNotifier *n = opaque; +VirtQueue *vq =virtqueue_from_guest_notifier (n);if (event_notifier_test_and_clear (n)) {virtio_irq (vq); }
@@ -440,7 +440,7 @@ static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)return r; }qemu_set_fd_handler (event_notifier_get_fd (notifier), - virtio_pci_guest_notifier_read,NULL , vq); + virtio_pci_guest_notifier_read,NULL , notifier); }else {qemu_set_fd_handler (event_notifier_get_fd (notifier),NULL ,NULL ,NULL );
hw/virtio.c +5 -0
@@ -805,6 +805,11 @@ VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n)return vdev->vq + n; } +VirtQueue *virtqueue_from_guest_notifier (EventNotifier *e) +{ +return container_of (e, VirtQueue, guest_notifier); +} +EventNotifier *virtio_queue_get_guest_notifier (VirtQueue *vq) {return &vq->guest_notifier;
hw/virtio.h +1 -0
@@ -210,6 +210,7 @@ target_phys_addr_t virtio_queue_get_ring_size(VirtIODevice *vdev, int n);uint16_t virtio_queue_get_last_avail_idx (VirtIODevice *vdev,int n);void virtio_queue_set_last_avail_idx (VirtIODevice *vdev,int n,uint16_t idx);VirtQueue *virtio_get_queue (VirtIODevice *vdev,int n); +VirtQueue *virtqueue_from_guest_notifier (EventNotifier *vq);EventNotifier *virtio_queue_get_guest_notifier (VirtQueue *vq);EventNotifier *virtio_queue_get_host_notifier (VirtQueue *vq);void virtio_irq (VirtQueue *vq);
Signature · 1 line
1.6.6.1
PATCH 5/8 add and use event_notifier_set_handler
In reply to Paolo Bonzini--- event_notifier.c | 7 +++++++ event_notifier.h | 3 +++ hw/virtio-pci.c | 9 +++------ 3 files changed, 13 insertions(+), 6 deletions(-)
event_notifier.c +7 -0
@@ -11,6 +11,7 @@*/ #include "event_notifier.h" +#include "qemu-char.h" #ifdef CONFIG_EVENTFD #include <sys/eventfd.h> #endif
@@ -38,6 +39,12 @@ int event_notifier_get_fd(EventNotifier *e)return e->fd; } +int event_notifier_set_handler (EventNotifier *e, +EventNotifierHandler *handler) +{ +return qemu_set_fd_handler (e->fd, (IOHandler *)handler,NULL , e); +} +int event_notifier_set (EventNotifier *e) {uint64_t value =1 ;
event_notifier.h +3 -0
@@ -7,10 +7,13 @@ struct EventNotifier { int fd; }; +typedef void EventNotifierHandler (EventNotifier *); +int event_notifier_init (EventNotifier *,int active);void event_notifier_cleanup (EventNotifier *);int event_notifier_get_fd (EventNotifier *);int event_notifier_set (EventNotifier *);int event_notifier_test_and_clear (EventNotifier *); +int event_notifier_set_handler (EventNotifier *,EventNotifierHandler *);#endif
hw/virtio-pci.c +3 -6
@@ -419,9 +419,8 @@ static unsigned virtio_pci_get_features(void *opaque)return proxy->host_features; } -static void virtio_pci_guest_notifier_read (void *opaque) +static void virtio_pci_guest_notifier_read (EventNotifier *n) { -EventNotifier *n = opaque;VirtQueue *vq =virtqueue_from_guest_notifier (n);if (event_notifier_test_and_clear (n)) {virtio_irq (vq);
@@ -439,11 +438,9 @@ static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)if (r <0 ) {return r; } -qemu_set_fd_handler (event_notifier_get_fd (notifier), - virtio_pci_guest_notifier_read,NULL , notifier); +event_notifier_set_handler (notifier, virtio_pci_guest_notifier_read); }else { -qemu_set_fd_handler (event_notifier_get_fd (notifier), -NULL ,NULL ,NULL ); +event_notifier_set_handler (notifier,NULL );event_notifier_cleanup (notifier); }
Signature · 1 line
1.6.6.1
PATCH 6/8 enable event_notifier to use pipes
In reply to Paolo Bonzini--- event_notifier.c | 69 +++++++++++++++++++++++++++++++++++++++--------------- event_notifier.h | 3 +- 2 files changed, 52 insertions(+), 20 deletions(-)
event_notifier.c +50 -19
@@ -10,51 +10,82 @@* the COPYING file in the top-level directory. */ +#include "qemu-common.h" #include "event_notifier.h" #include "qemu-char.h" -#ifdef CONFIG_EVENTFD -#include <sys/eventfd.h> -#endif int event_notifier_init (EventNotifier *e,int active) { -#ifdef CONFIG_EVENTFD -int fd =eventfd (!!active,EFD_NONBLOCK |EFD_CLOEXEC ); -if (fd <0 ) +int fds[2 ]; +int err; +if (qemu_eventfd (fds) <0 )return -errno; - e->fd = fd; + + err =fcntl_setfl (fds[0 ],O_NONBLOCK ); +if (err <0 ) + goto fail; + + err =fcntl_setfl (fds[1 ],O_NONBLOCK ); +if (err <0 ) + goto fail; + + e->rfd = fds[0 ]; + e->wfd = fds[1 ]; +if (active) +event_notifier_set (e);return 0 ; -#else -return -ENOSYS ; -#endif + +fail: +close (fds[0 ]); +close (fds[1 ]); +return err; }void event_notifier_cleanup (EventNotifier *e) { -close (e->fd); +close (e->rfd); +close (e->wfd); }int event_notifier_get_fd (EventNotifier *e) { -return e->fd; +return e->wfd; }int event_notifier_set_handler (EventNotifier *e,EventNotifierHandler *handler) { -return qemu_set_fd_handler (e->fd, (IOHandler *)handler,NULL , e); +return qemu_set_fd_handler (e->rfd, (IOHandler *)handler,NULL , e); }int event_notifier_set (EventNotifier *e) { -uint64_t value =1 ; -int r =write (e->fd, &value,sizeof (value)); -return r ==sizeof (value); +static const uint64_t value =1 ; +ssize_t ret; + +do { + ret =write (e->wfd, &value,sizeof (value)); + }while (ret <0 && errno ==EINTR ); + +/* EAGAIN is fine, a read must be pending. */ +if (ret <0 && errno !=EAGAIN ) { +return -1 ; + } +return 0 ; }int event_notifier_test_and_clear (EventNotifier *e) { -uint64_t value; -int r =read (e->fd, &value,sizeof (value)); -return r ==sizeof (value); +int value; +ssize_t len; +char buffer[512 ]; + +/* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ + value =0 ; +do { + len =read (e->rfd, buffer,sizeof (buffer)); + value |= (len >0 ); + }while ((len ==-1 && errno ==EINTR ) || len ==sizeof (buffer)); + +return value; }
event_notifier.h +2 -1
@@ -4,7 +4,8 @@#include "qemu-common.h" struct EventNotifier { -int fd; +int rfd; +int wfd; };typedef void EventNotifierHandler (EventNotifier *);
Signature · 1 line
1.6.6.1
Re: [Qemu-devel] [PATCH 6/8] enable event_notifier to use pipes
event_notifier.c line 17
Quoted 3 lines · Paolo Bonzini
event_notifier.c +0
-3-#ifdef CONFIG_EVENTFD -#include <sys/eventfd.h> -#endif
Is there a reason not to use eventfd if it is available?
r~
[Qemu-devel] Re: [PATCH 6/8] enable event_notifier to use pipes
Quoted 6 lines · Richard Henderson
event_notifier.c line 17
Quoted 3 lines · Paolo Bonzini
event_notifier.c +0
-3-#ifdef CONFIG_EVENTFD -#include <sys/eventfd.h> -#endif Is there a reason not to use eventfd if it is available?
qemu_eventfd chooses between eventfd or pipes. The reads and writes are done so that both cases are accomodated.
Paolo
PATCH 7/8 add Win32 implementation of event notifiers
In reply to Paolo Bonzini---
Compile-tested only. iothread is broken anyway on Win32 due to missing implementation of qemu-threads.
event_notifier.c | 33 +++++++++++++++++++++++++++++++++ event_notifier.h | 8 ++++++++ 2 files changed, 41 insertions(+), 0 deletions(-)
event_notifier.c +33 -0
@@ -11,11 +11,13 @@*/ #include "qemu-common.h" +#include "sysemu.h" #include "event_notifier.h" #include "qemu-char.h" int event_notifier_init (EventNotifier *e,int active) { +#ifndef _WIN32int fds[2 ];int err;if (qemu_eventfd (fds) <0 )
@@ -39,27 +41,50 @@ fail:close (fds[0 ]);close (fds[1 ]);return err; +#else + e->event =CreateEvent (NULL , FALSE, FALSE,NULL ); +assert (e->event); +return 0 ; +#endif }void event_notifier_cleanup (EventNotifier *e) { +#ifndef _WIN32close (e->rfd);close (e->wfd); +#else +CloseHandle (e->event); +#endif }int event_notifier_get_fd (EventNotifier *e) { +#ifndef _WIN32return e->wfd; +#else +abort (); +#endif }int event_notifier_set_handler (EventNotifier *e,EventNotifierHandler *handler) { +#ifndef _WIN32return qemu_set_fd_handler (e->rfd, (IOHandler *)handler,NULL , e); +#else +if (handler) { +return qemu_add_wait_object (e->event, (IOHandler *)handler, e); + }else { +qemu_del_wait_object (e->event, (IOHandler *)handler, e); +return 0 ; + } +#endif }int event_notifier_set (EventNotifier *e) { +#ifndef _WIN32static const uint64_t value =1 ;ssize_t ret;
@@ -72,10 +97,15 @@ int event_notifier_set(EventNotifier *e) return -1; }return 0 ; +#else +SetEvent (e->event); +return 0 ; +#endif }int event_notifier_test_and_clear (EventNotifier *e) { +#ifndef _WIN32int value;ssize_t len;char buffer[512 ];
@@ -88,4 +118,7 @@ int event_notifier_test_and_clear(EventNotifier *e) } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));return value; +#else +return WaitForSingleObject (e->event,0 ) ==WAIT_OBJECT_0 ; +#endif }
event_notifier.h +8 -0
@@ -3,9 +3,17 @@#include "qemu-common.h" +#ifdef _WIN32 +#include <windows.h> +#endif +struct EventNotifier { +#ifdef _WIN32 +HANDLE event; +#else int rfd;int wfd; +#endif };typedef void EventNotifierHandler (EventNotifier *);
Signature · 1 line
1.6.6.1
PATCH 8/8 change ioevent to use event notifiers
In reply to Paolo Bonzini--- cpus.c | 95 +++++----------------------------------------------------------- 1 files changed, 9 insertions(+), 86 deletions(-)
cpus.c +9 -86
@@ -26,6 +26,7 @@#include "config-host.h" #include "monitor.h" +#include "event_notifier.h" #include "sysemu.h" #include "gdbstub.h" #include "dma.h"
@@ -141,97 +142,19 @@ static int tcg_has_work(void)return 0 ; } -#ifndef _WIN32 -static int io_thread_fd =-1 ; - -static void qemu_event_increment (void ) -{ -/* Write 8 bytes to be compatible with eventfd. */ -static const uint64_t val =1 ; -ssize_t ret; - -if (io_thread_fd ==-1 ) -return ; - -do { - ret =write (io_thread_fd, &val,sizeof (val)); - }while (ret <0 && errno ==EINTR ); - -/* EAGAIN is fine, a read must be pending. */ -if (ret <0 && errno !=EAGAIN ) { -fprintf (stderr,"qemu_event_increment: write() filed: %s\n" , -strerror (errno)); -exit (1 ); - } -} - -static void qemu_event_read (void *opaque) -{ -int fd = (unsigned long )opaque; -ssize_t len; -char buffer[512 ]; - -/* Drain the notify pipe. For eventfd, only 8 bytes will be read. */ -do { - len =read (fd, buffer,sizeof (buffer)); - }while ((len ==-1 && errno ==EINTR ) || len ==sizeof (buffer)); -} +static EventNotifier io_thread_notifier;static int qemu_event_init (void ) {int err; -int fds[2 ]; - - err =qemu_eventfd (fds); -if (err ==-1 ) -return -errno; + err =event_notifier_init (&io_thread_notifier,0 ); +if (err ==0 ) +event_notifier_set_handler (&io_thread_notifier, + (EventNotifierHandler *) + event_notifier_test_and_clear); - err =fcntl_setfl (fds[0 ],O_NONBLOCK ); -if (err <0 ) - goto fail; - - err =fcntl_setfl (fds[1 ],O_NONBLOCK ); -if (err <0 ) - goto fail; - -qemu_set_fd_handler2 (fds[0 ],NULL , qemu_event_read,NULL , - (void *)(unsigned long )fds[0 ]); - - io_thread_fd = fds[1 ]; -return 0 ; - -fail: -close (fds[0 ]); -close (fds[1 ]);return err; } -#else -HANDLE qemu_event_handle; - -static void dummy_event_handler (void *opaque) -{ -} - -static int qemu_event_init (void ) -{ - qemu_event_handle =CreateEvent (NULL , FALSE, FALSE,NULL ); -if (!qemu_event_handle) { -fprintf (stderr,"Failed CreateEvent: %ld\n" ,GetLastError ()); -return -1 ; - } -qemu_add_wait_object (qemu_event_handle, dummy_event_handler,NULL ); -return 0 ; -} - -static void qemu_event_increment (void ) -{ -if (!SetEvent (qemu_event_handle)) { -fprintf (stderr,"qemu_event_increment: SetEvent failed: %ld\n" , -GetLastError ()); -exit (1 ); - } -} -#endif #ifndef CONFIG_IOTHREAD int qemu_init_main_loop (void )
@@ -281,7 +203,7 @@ void qemu_notify_event(void) {CPUState *env = cpu_single_env; -qemu_event_increment (); +event_notifier_set (&io_thread_notifier);if (env) {cpu_exit (env); }
@@ -709,7 +631,7 @@ void qemu_init_vcpu(void *_env)void qemu_notify_event (void ) { -qemu_event_increment (); +event_notifier_set (&io_thread_notifier); }static void qemu_system_vmstop_request (int reason)
Signature · 1 line
1.6.6.1
Quoted 5 lines · Paolo Bonzini
Hi,
this patch adds all the eventfd bells and whistles from
vl.c/cpus.ctoevent_notifier, including pipe emulation and Win32 support. It then modifies the iothread code to use it instead.
I only seem to have got patches 4 and 5. Error at my end?
Quoted 24 lines
Paolo Bonzini (8):
moveevent_notifierinto the main directory
addevent_notifier_set
removeevent_notifier_test
add and usevirtqueue_from_guest_notifier
add and useevent_notifier_set_handler
enableevent_notifierto use pipes
add Win32 implementation of event notifiers
change ioevent to use event notifierscpus.c | 95 +++----------------------------------- event_notifier.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ event_notifier.h | 28 +++++++++++ hw/event_notifier.c | 62 ------------------------- hw/event_notifier.h | 16 ------- hw/virtio-pci.c | 11 ++--- hw/virtio.c | 5 ++ hw/virtio.h | 1 + 8 files changed, 171 insertions(+), 171 deletions(-) create mode 100644 event_notifier.c create mode 100644 event_notifier.h delete mode 100644 hw/event_notifier.c delete mode 100644 hw/event_notifier.h
Quoted 8 lines · Michael S. Tsirkin
Quoted 5 lines · Paolo Bonzini
Hi,
this patch adds all the eventfd bells and whistles from
vl.c/cpus.ctoevent_notifier, including pipe emulation and Win32 support. It then modifies the iothread code to use it instead.I only seem to have got patches 4 and 5. Error at my end?
Maybe Savannah's, OTOH gmane and patchwork show all eight.
Quoted 1 line
Quoted 1 line
move
event_notifierinto the main directory
http://patchwork.ozlabs.org/patch/53626/
Quoted 1 line
Quoted 1 line
add
event_notifier_set
http://patchwork.ozlabs.org/patch/53627/
Quoted 1 line
Quoted 1 line
remove
event_notifier_test
http://patchwork.ozlabs.org/patch/53628/
Quoted 1 line
Quoted 1 line
add and use
virtqueue_from_guest_notifier
http://patchwork.ozlabs.org/patch/53632/
Quoted 1 line
Quoted 1 line
add and use
event_notifier_set_handler
http://patchwork.ozlabs.org/patch/53630/
Quoted 1 line
Quoted 1 line
enable
event_notifierto use pipes
http://patchwork.ozlabs.org/patch/53619/
Quoted 1 line
Quoted 1 line
add Win32 implementation of event notifiers
http://patchwork.ozlabs.org/patch/53621/
Quoted 1 line
Quoted 1 line
change ioevent to use event notifiers
http://patchwork.ozlabs.org/patch/53635/
Paolo
Quoted 25 lines · Paolo Bonzini
Hi,
this patch adds all the eventfd bells and whistles from
vl.c/cpus.ctoevent_notifier, including pipe emulation and Win32 support. It then modifies the iothread code to use it instead.Paolo Bonzini (8):
moveevent_notifierinto the main directory
addevent_notifier_set
removeevent_notifier_test
add and usevirtqueue_from_guest_notifier
add and useevent_notifier_set_handler
enableevent_notifierto use pipes
add Win32 implementation of event notifiers
change ioevent to use event notifierscpus.c | 95 +++----------------------------------- event_notifier.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ event_notifier.h | 28 +++++++++++ hw/event_notifier.c | 62 ------------------------- hw/event_notifier.h | 16 ------- hw/virtio-pci.c | 11 ++--- hw/virtio.c | 5 ++ hw/virtio.h | 1 + 8 files changed, 171 insertions(+), 171 deletions(-)
Quoted 4 lines
create mode 100644
event_notifier.c
create mode 100644event_notifier.h
delete mode 100644hw/event_notifier.c
delete mode 100644hw/event_notifier.h
I'll redo the patches starting from qemu-kvm, since there's a bit more to do there. I'll post the master and uq/master sometime next week.
Paolo