Skip to contentSkip to conversation

eventfd: making it thread safe

PATCH In review David Gibson 3 participants 4 messages Aug 1, 2012 – Aug 7, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eventfd: making it thread safe

From: Alexey Kardashevskiy <[email protected]>

QEMU uses IO handlers to run select() in the main loop. The handlers list is managed by qemu_set_fd_handler() helper which works fine when called from the main thread as it is called when select() is not waiting.

However IO handlers list can be changed in the thread other than the main one doing os_host_main_loop_wait(), for example, as a result of a hypercall which changes PCI config space (VFIO on POWER is the case) and enables/disabled MSI/MSIX which creates eventfd handles. As the main loop should be waiting on the newly created eventfds, it has to be restarted.

The patch adds the qemu_notify_event() call to interrupt select() to make main_loop() restart select() with the updated IO handlers list.

---
 iohandler.c |    1 +
 1 file changed, 1 insertion(+)
iohandler.c +1 -0
index 3c74de6..dea4355 100644
@@ -77,6 +77,7 @@ int qemu_set_fd_handler2(int fd,
         ioh->fd_write = fd_write;
         ioh->opaque = opaque;
         ioh->deleted = 0;
+        qemu_notify_event();
     }
     return 0;
 }
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 29 lines · David Gibson

From: Alexey Kardashevskiy <[email protected]>

QEMU uses IO handlers to run select() in the main loop. The handlers list is managed by qemu_set_fd_handler() helper which works fine when called from the main thread as it is called when select() is not waiting.

However IO handlers list can be changed in the thread other than the main one doing os_host_main_loop_wait(), for example, as a result of a hypercall which changes PCI config space (VFIO on POWER is the case) and enables/disabled MSI/MSIX which creates eventfd handles. As the main loop should be waiting on the newly created eventfds, it has to be restarted.

The patch adds the qemu_notify_event() call to interrupt select() to make main_loop() restart select() with the updated IO handlers list.

---
 iohandler.c |    1 +
 1 file changed, 1 insertion(+)
iohandler.c +0 -0
index 3c74de6..dea4355 100644

iohandler.c int qemu_set_fd_handler2(int fd, · line 83

Quoted 7 lines
iohandler.c +1 -0
@@ -77,6 +77,7 @@ int qemu_set_fd_handler2(int fd,
         ioh->fd_write = fd_write;
         ioh->opaque = opaque;
         ioh->deleted = 0;
+        qemu_notify_event();
     }
     return 0;
 }

Perhaps it's better to do this unconditionally (on the delete path too) so that removals are processed without delay and we don't have closed fds hanging around in select().

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 25 lines · Avi Kivity
Quoted 24 lines · David Gibson

From: Alexey Kardashevskiy <[email protected]>

QEMU uses IO handlers to run select() in the main loop. The handlers list is managed by qemu_set_fd_handler() helper which works fine when called from the main thread as it is called when select() is not waiting.

However IO handlers list can be changed in the thread other than the main one doing os_host_main_loop_wait(), for example, as a result of a hypercall which changes PCI config space (VFIO on POWER is the case) and enables/disabled MSI/MSIX which creates eventfd handles. As the main loop should be waiting on the newly created eventfds, it has to be restarted.

The patch adds the qemu_notify_event() call to interrupt select() to make main_loop() restart select() with the updated IO handlers list.

---
 iohandler.c |    1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines

iohandler.c int qemu_set_fd_handler2(int fd, · line 83

Quoted 12 lines
iohandler.c +1 -0
index 3c74de6..dea4355 100644
@@ -77,6 +77,7 @@ int qemu_set_fd_handler2(int fd,
         ioh->fd_write = fd_write;
         ioh->opaque = opaque;
         ioh->deleted = 0;
+        qemu_notify_event();
     }
     return 0;
 }

Perhaps it's better to do this unconditionally (on the delete path too) so that removals are processed without delay and we don't have closed fds hanging around in select().

Well, I understand that Alexey discussed the patch with Paolo and Michael Tsirkin, and this was the preferred approach for now. Since obviously no events will happen on deleted fds, removing them from the select() is not really urgent.

This is a very straightforward fix for a real problem, can we please just merge the damn thing.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#3 lore raw

Il 07/08/2012 06:02, David Gibson ha scritto:

Quoted 8 lines
Quoted 3 lines

Perhaps it's better to do this unconditionally (on the delete path too) so that removals are processed without delay and we don't have closed fds hanging around in select().

Well, I understand that Alexey discussed the patch with Paolo and Michael Tsirkin, and this was the preferred approach for now. Since obviously no events will happen on deleted fds, removing them from the select() is not really urgent.

Avi is not speaking about deleted fds, but about existing fds whose handlers are temporarily removed. I don't see it as a blocker for merging the patch because we've never observed it (and it's unlikely, because temporary removal of handlers typically occurs from within a handler, not from another thread).

Paolo