2008-09-22 21:17:18 +02:00
|
|
|
/*
|
|
|
|
* QEMU aio implementation
|
|
|
|
*
|
2012-06-09 04:01:51 +02:00
|
|
|
* Copyright IBM Corp., 2008
|
|
|
|
* Copyright Red Hat Inc., 2012
|
2008-09-22 21:17:18 +02:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
2012-06-09 04:01:51 +02:00
|
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
2008-09-22 21:17:18 +02:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
2012-01-13 17:44:23 +01:00
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2008-09-22 21:17:18 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-29 18:50:05 +01:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 18:19:44 +01:00
|
|
|
#include "block/block.h"
|
2020-10-21 08:40:33 +02:00
|
|
|
#include "qemu/main-loop.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/queue.h"
|
|
|
|
#include "qemu/sockets.h"
|
2016-12-01 20:26:42 +01:00
|
|
|
#include "qapi/error.h"
|
2017-01-12 19:07:58 +01:00
|
|
|
#include "qemu/rcu_queue.h"
|
2008-09-22 21:17:18 +02:00
|
|
|
|
2012-06-09 04:01:51 +02:00
|
|
|
struct AioHandler {
|
|
|
|
EventNotifier *e;
|
2014-07-09 11:53:10 +02:00
|
|
|
IOHandler *io_read;
|
|
|
|
IOHandler *io_write;
|
2012-06-09 04:01:51 +02:00
|
|
|
EventNotifierHandler *io_notify;
|
2012-09-24 14:57:22 +02:00
|
|
|
GPollFD pfd;
|
2008-09-22 21:17:18 +02:00
|
|
|
int deleted;
|
2014-07-09 11:53:10 +02:00
|
|
|
void *opaque;
|
2015-10-23 05:08:05 +02:00
|
|
|
bool is_external;
|
2009-09-12 09:36:22 +02:00
|
|
|
QLIST_ENTRY(AioHandler) node;
|
2008-09-22 21:17:18 +02:00
|
|
|
};
|
|
|
|
|
2018-12-20 16:20:30 +01:00
|
|
|
static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
|
|
|
|
{
|
2020-09-15 19:12:26 +02:00
|
|
|
/*
|
|
|
|
* If the GSource is in the process of being destroyed then
|
|
|
|
* g_source_remove_poll() causes an assertion failure. Skip
|
|
|
|
* removal in that case, because glib cleans up its state during
|
|
|
|
* destruction anyway.
|
|
|
|
*/
|
|
|
|
if (!g_source_is_destroyed(&ctx->source)) {
|
|
|
|
g_source_remove_poll(&ctx->source, &node->pfd);
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:20:30 +01:00
|
|
|
/* If aio_poll is in progress, just mark the node as deleted */
|
|
|
|
if (qemu_lockcnt_count(&ctx->list_lock)) {
|
|
|
|
node->deleted = 1;
|
|
|
|
node->pfd.revents = 0;
|
|
|
|
} else {
|
|
|
|
/* Otherwise, delete it for real. We can't just mark it as
|
|
|
|
* deleted because deleted nodes are only cleaned up after
|
|
|
|
* releasing the list_lock.
|
|
|
|
*/
|
|
|
|
QLIST_REMOVE(node, node);
|
|
|
|
g_free(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 11:53:10 +02:00
|
|
|
void aio_set_fd_handler(AioContext *ctx,
|
|
|
|
int fd,
|
2015-10-23 05:08:05 +02:00
|
|
|
bool is_external,
|
2014-07-09 11:53:10 +02:00
|
|
|
IOHandler *io_read,
|
|
|
|
IOHandler *io_write,
|
2016-12-01 20:26:42 +01:00
|
|
|
AioPollFn *io_poll,
|
aio-posix: split poll check from ready handler
Adaptive polling measures the execution time of the polling check plus
handlers called when a polled event becomes ready. Handlers can take a
significant amount of time, making it look like polling was running for
a long time when in fact the event handler was running for a long time.
For example, on Linux the io_submit(2) syscall invoked when a virtio-blk
device's virtqueue becomes ready can take 10s of microseconds. This
can exceed the default polling interval (32 microseconds) and cause
adaptive polling to stop polling.
By excluding the handler's execution time from the polling check we make
the adaptive polling calculation more accurate. As a result, the event
loop now stays in polling mode where previously it would have fallen
back to file descriptor monitoring.
The following data was collected with virtio-blk num-queues=2
event_idx=off using an IOThread. Before:
168k IOPS, IOThread syscalls:
9837.115 ( 0.020 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 16, iocbpp: 0x7fcb9f937db0) = 16
9837.158 ( 0.002 ms): IO iothread1/620155 write(fd: 103, buf: 0x556a2ef71b88, count: 8) = 8
9837.161 ( 0.001 ms): IO iothread1/620155 write(fd: 104, buf: 0x556a2ef71b88, count: 8) = 8
9837.163 ( 0.001 ms): IO iothread1/620155 ppoll(ufds: 0x7fcb90002800, nfds: 4, tsp: 0x7fcb9f1342d0, sigsetsize: 8) = 3
9837.164 ( 0.001 ms): IO iothread1/620155 read(fd: 107, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.174 ( 0.001 ms): IO iothread1/620155 read(fd: 105, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.176 ( 0.001 ms): IO iothread1/620155 read(fd: 106, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.209 ( 0.035 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 32, iocbpp: 0x7fca7d0cebe0) = 32
174k IOPS (+3.6%), IOThread syscalls:
9809.566 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0cdd62be0) = 32
9809.625 ( 0.001 ms): IO iothread1/623061 write(fd: 103, buf: 0x5647cfba5f58, count: 8) = 8
9809.627 ( 0.002 ms): IO iothread1/623061 write(fd: 104, buf: 0x5647cfba5f58, count: 8) = 8
9809.663 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0d0388b50) = 32
Notice that ppoll(2) and eventfd read(2) syscalls are eliminated because
the IOThread stays in polling mode instead of falling back to file
descriptor monitoring.
As usual, polling is not implemented on Windows so this patch ignores
the new io_poll_read() callback in aio-win32.c.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Message-id: 20211207132336.36627-2-stefanha@redhat.com
[Fixed up aio_set_event_notifier() calls in
tests/unit/test-fdmon-epoll.c added after this series was queued.
--Stefan]
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2021-12-07 14:23:31 +01:00
|
|
|
IOHandler *io_poll_ready,
|
2014-07-09 11:53:10 +02:00
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
/* fd is a SOCKET in our case */
|
2018-12-20 16:20:30 +01:00
|
|
|
AioHandler *old_node;
|
|
|
|
AioHandler *node = NULL;
|
2014-07-09 11:53:10 +02:00
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_lock(&ctx->list_lock);
|
2018-12-20 16:20:30 +01:00
|
|
|
QLIST_FOREACH(old_node, &ctx->aio_handlers, node) {
|
|
|
|
if (old_node->pfd.fd == fd && !old_node->deleted) {
|
2014-07-09 11:53:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:20:30 +01:00
|
|
|
if (io_read || io_write) {
|
2014-07-09 11:53:10 +02:00
|
|
|
HANDLE event;
|
2017-07-06 22:15:14 +02:00
|
|
|
long bitmask = 0;
|
2014-07-09 11:53:10 +02:00
|
|
|
|
2018-12-20 16:20:30 +01:00
|
|
|
/* Alloc and insert if it's not already there */
|
|
|
|
node = g_new0(AioHandler, 1);
|
|
|
|
node->pfd.fd = fd;
|
2014-07-09 11:53:10 +02:00
|
|
|
|
|
|
|
node->pfd.events = 0;
|
|
|
|
if (node->io_read) {
|
|
|
|
node->pfd.events |= G_IO_IN;
|
|
|
|
}
|
|
|
|
if (node->io_write) {
|
|
|
|
node->pfd.events |= G_IO_OUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->e = &ctx->notifier;
|
|
|
|
|
|
|
|
/* Update handler with latest information */
|
|
|
|
node->opaque = opaque;
|
|
|
|
node->io_read = io_read;
|
|
|
|
node->io_write = io_write;
|
2015-10-23 05:08:05 +02:00
|
|
|
node->is_external = is_external;
|
2014-07-09 11:53:10 +02:00
|
|
|
|
2017-07-06 22:15:14 +02:00
|
|
|
if (io_read) {
|
|
|
|
bitmask |= FD_READ | FD_ACCEPT | FD_CLOSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (io_write) {
|
|
|
|
bitmask |= FD_WRITE | FD_CONNECT;
|
|
|
|
}
|
|
|
|
|
2018-12-20 16:20:30 +01:00
|
|
|
QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
|
2014-07-09 11:53:10 +02:00
|
|
|
event = event_notifier_get_handle(&ctx->notifier);
|
2017-07-06 22:15:14 +02:00
|
|
|
WSAEventSelect(node->pfd.fd, event, bitmask);
|
2014-07-09 11:53:10 +02:00
|
|
|
}
|
2018-12-20 16:20:30 +01:00
|
|
|
if (old_node) {
|
|
|
|
aio_remove_fd_handler(ctx, old_node);
|
|
|
|
}
|
2014-07-09 11:53:10 +02:00
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_unlock(&ctx->list_lock);
|
2014-07-09 11:53:10 +02:00
|
|
|
aio_notify(ctx);
|
|
|
|
}
|
|
|
|
|
2016-12-01 20:26:49 +01:00
|
|
|
void aio_set_fd_poll(AioContext *ctx, int fd,
|
|
|
|
IOHandler *io_poll_begin,
|
|
|
|
IOHandler *io_poll_end)
|
|
|
|
{
|
|
|
|
/* Not implemented */
|
|
|
|
}
|
|
|
|
|
2012-06-09 04:01:51 +02:00
|
|
|
void aio_set_event_notifier(AioContext *ctx,
|
|
|
|
EventNotifier *e,
|
2015-10-23 05:08:05 +02:00
|
|
|
bool is_external,
|
2016-12-01 20:26:42 +01:00
|
|
|
EventNotifierHandler *io_notify,
|
aio-posix: split poll check from ready handler
Adaptive polling measures the execution time of the polling check plus
handlers called when a polled event becomes ready. Handlers can take a
significant amount of time, making it look like polling was running for
a long time when in fact the event handler was running for a long time.
For example, on Linux the io_submit(2) syscall invoked when a virtio-blk
device's virtqueue becomes ready can take 10s of microseconds. This
can exceed the default polling interval (32 microseconds) and cause
adaptive polling to stop polling.
By excluding the handler's execution time from the polling check we make
the adaptive polling calculation more accurate. As a result, the event
loop now stays in polling mode where previously it would have fallen
back to file descriptor monitoring.
The following data was collected with virtio-blk num-queues=2
event_idx=off using an IOThread. Before:
168k IOPS, IOThread syscalls:
9837.115 ( 0.020 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 16, iocbpp: 0x7fcb9f937db0) = 16
9837.158 ( 0.002 ms): IO iothread1/620155 write(fd: 103, buf: 0x556a2ef71b88, count: 8) = 8
9837.161 ( 0.001 ms): IO iothread1/620155 write(fd: 104, buf: 0x556a2ef71b88, count: 8) = 8
9837.163 ( 0.001 ms): IO iothread1/620155 ppoll(ufds: 0x7fcb90002800, nfds: 4, tsp: 0x7fcb9f1342d0, sigsetsize: 8) = 3
9837.164 ( 0.001 ms): IO iothread1/620155 read(fd: 107, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.174 ( 0.001 ms): IO iothread1/620155 read(fd: 105, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.176 ( 0.001 ms): IO iothread1/620155 read(fd: 106, buf: 0x7fcb9f939cc0, count: 512) = 8
9837.209 ( 0.035 ms): IO iothread1/620155 io_submit(ctx_id: 140512552468480, nr: 32, iocbpp: 0x7fca7d0cebe0) = 32
174k IOPS (+3.6%), IOThread syscalls:
9809.566 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0cdd62be0) = 32
9809.625 ( 0.001 ms): IO iothread1/623061 write(fd: 103, buf: 0x5647cfba5f58, count: 8) = 8
9809.627 ( 0.002 ms): IO iothread1/623061 write(fd: 104, buf: 0x5647cfba5f58, count: 8) = 8
9809.663 ( 0.036 ms): IO iothread1/623061 io_submit(ctx_id: 140539805028352, nr: 32, iocbpp: 0x7fd0d0388b50) = 32
Notice that ppoll(2) and eventfd read(2) syscalls are eliminated because
the IOThread stays in polling mode instead of falling back to file
descriptor monitoring.
As usual, polling is not implemented on Windows so this patch ignores
the new io_poll_read() callback in aio-win32.c.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>
Message-id: 20211207132336.36627-2-stefanha@redhat.com
[Fixed up aio_set_event_notifier() calls in
tests/unit/test-fdmon-epoll.c added after this series was queued.
--Stefan]
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2021-12-07 14:23:31 +01:00
|
|
|
AioPollFn *io_poll,
|
|
|
|
EventNotifierHandler *io_poll_ready)
|
2008-09-22 21:17:18 +02:00
|
|
|
{
|
|
|
|
AioHandler *node;
|
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_lock(&ctx->list_lock);
|
2012-09-13 12:28:51 +02:00
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
2012-06-09 04:01:51 +02:00
|
|
|
if (node->e == e && !node->deleted) {
|
|
|
|
break;
|
|
|
|
}
|
2008-09-22 21:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Are we deleting the fd handler? */
|
2012-06-09 04:01:51 +02:00
|
|
|
if (!io_notify) {
|
2008-09-22 21:17:18 +02:00
|
|
|
if (node) {
|
2018-12-20 16:20:30 +01:00
|
|
|
aio_remove_fd_handler(ctx, node);
|
2008-09-22 21:17:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (node == NULL) {
|
|
|
|
/* Alloc and insert if it's not already there */
|
2014-12-04 13:55:09 +01:00
|
|
|
node = g_new0(AioHandler, 1);
|
2012-06-09 04:01:51 +02:00
|
|
|
node->e = e;
|
|
|
|
node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
|
|
|
|
node->pfd.events = G_IO_IN;
|
2015-10-23 05:08:05 +02:00
|
|
|
node->is_external = is_external;
|
2017-01-12 19:07:58 +01:00
|
|
|
QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node);
|
2012-09-24 14:57:41 +02:00
|
|
|
|
|
|
|
g_source_add_poll(&ctx->source, &node->pfd);
|
2008-09-22 21:17:18 +02:00
|
|
|
}
|
|
|
|
/* Update handler with latest information */
|
2012-06-09 04:01:51 +02:00
|
|
|
node->io_notify = io_notify;
|
2008-09-22 21:17:18 +02:00
|
|
|
}
|
2012-09-25 10:22:39 +02:00
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_unlock(&ctx->list_lock);
|
2012-09-25 10:22:39 +02:00
|
|
|
aio_notify(ctx);
|
2012-06-09 03:44:00 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 20:26:49 +01:00
|
|
|
void aio_set_event_notifier_poll(AioContext *ctx,
|
|
|
|
EventNotifier *notifier,
|
|
|
|
EventNotifierHandler *io_poll_begin,
|
|
|
|
EventNotifierHandler *io_poll_end)
|
|
|
|
{
|
|
|
|
/* Not implemented */
|
|
|
|
}
|
|
|
|
|
2014-07-09 11:53:08 +02:00
|
|
|
bool aio_prepare(AioContext *ctx)
|
|
|
|
{
|
2014-07-09 11:53:10 +02:00
|
|
|
static struct timeval tv0;
|
|
|
|
AioHandler *node;
|
|
|
|
bool have_select_revents = false;
|
|
|
|
fd_set rfds, wfds;
|
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
/*
|
|
|
|
* We have to walk very carefully in case aio_set_fd_handler is
|
|
|
|
* called while we're walking.
|
|
|
|
*/
|
|
|
|
qemu_lockcnt_inc(&ctx->list_lock);
|
|
|
|
|
2014-07-09 11:53:10 +02:00
|
|
|
/* fill fd sets */
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
2017-01-12 19:07:58 +01:00
|
|
|
QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
|
2014-07-09 11:53:10 +02:00
|
|
|
if (node->io_read) {
|
|
|
|
FD_SET ((SOCKET)node->pfd.fd, &rfds);
|
|
|
|
}
|
|
|
|
if (node->io_write) {
|
|
|
|
FD_SET ((SOCKET)node->pfd.fd, &wfds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
|
2017-01-12 19:07:58 +01:00
|
|
|
QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
|
2014-07-09 11:53:10 +02:00
|
|
|
node->pfd.revents = 0;
|
|
|
|
if (FD_ISSET(node->pfd.fd, &rfds)) {
|
|
|
|
node->pfd.revents |= G_IO_IN;
|
|
|
|
have_select_revents = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(node->pfd.fd, &wfds)) {
|
|
|
|
node->pfd.revents |= G_IO_OUT;
|
|
|
|
have_select_revents = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
2014-07-09 11:53:10 +02:00
|
|
|
return have_select_revents;
|
2014-07-09 11:53:08 +02:00
|
|
|
}
|
|
|
|
|
2012-09-24 14:57:22 +02:00
|
|
|
bool aio_pending(AioContext *ctx)
|
|
|
|
{
|
|
|
|
AioHandler *node;
|
2017-01-12 19:07:58 +01:00
|
|
|
bool result = false;
|
2012-09-24 14:57:22 +02:00
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
/*
|
|
|
|
* We have to walk very carefully in case aio_set_fd_handler is
|
|
|
|
* called while we're walking.
|
|
|
|
*/
|
|
|
|
qemu_lockcnt_inc(&ctx->list_lock);
|
|
|
|
QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
|
2012-06-09 04:01:51 +02:00
|
|
|
if (node->pfd.revents && node->io_notify) {
|
2017-01-12 19:07:58 +01:00
|
|
|
result = true;
|
|
|
|
break;
|
2012-09-24 14:57:22 +02:00
|
|
|
}
|
2014-07-09 11:53:10 +02:00
|
|
|
|
|
|
|
if ((node->pfd.revents & G_IO_IN) && node->io_read) {
|
2017-01-12 19:07:58 +01:00
|
|
|
result = true;
|
|
|
|
break;
|
2014-07-09 11:53:10 +02:00
|
|
|
}
|
|
|
|
if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
|
2017-01-12 19:07:58 +01:00
|
|
|
result = true;
|
|
|
|
break;
|
2014-07-09 11:53:10 +02:00
|
|
|
}
|
2012-09-24 14:57:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
|
|
|
return result;
|
2012-09-24 14:57:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 11:53:03 +02:00
|
|
|
static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
|
2008-09-22 21:17:18 +02:00
|
|
|
{
|
2017-01-12 19:07:58 +01:00
|
|
|
AioHandler *node;
|
2014-07-09 11:53:03 +02:00
|
|
|
bool progress = false;
|
2017-01-12 19:07:58 +01:00
|
|
|
AioHandler *tmp;
|
2012-09-24 14:37:53 +02:00
|
|
|
|
2012-09-24 14:57:22 +02:00
|
|
|
/*
|
2014-07-07 15:18:02 +02:00
|
|
|
* We have to walk very carefully in case aio_set_fd_handler is
|
2012-09-24 14:57:22 +02:00
|
|
|
* called while we're walking.
|
|
|
|
*/
|
2017-01-12 19:07:58 +01:00
|
|
|
QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
|
2014-07-09 11:53:10 +02:00
|
|
|
int revents = node->pfd.revents;
|
2012-09-24 14:57:22 +02:00
|
|
|
|
2014-07-09 11:53:03 +02:00
|
|
|
if (!node->deleted &&
|
2014-07-09 11:53:10 +02:00
|
|
|
(revents || event_notifier_get_handle(node->e) == event) &&
|
2014-07-09 11:53:03 +02:00
|
|
|
node->io_notify) {
|
2012-06-09 04:01:51 +02:00
|
|
|
node->pfd.revents = 0;
|
|
|
|
node->io_notify(node->e);
|
2013-04-11 16:56:50 +02:00
|
|
|
|
|
|
|
/* aio_notify() does not count as progress */
|
2013-08-22 15:28:35 +02:00
|
|
|
if (node->e != &ctx->notifier) {
|
2013-04-11 16:56:50 +02:00
|
|
|
progress = true;
|
|
|
|
}
|
2012-09-24 14:57:22 +02:00
|
|
|
}
|
|
|
|
|
2014-07-09 11:53:10 +02:00
|
|
|
if (!node->deleted &&
|
|
|
|
(node->io_read || node->io_write)) {
|
|
|
|
node->pfd.revents = 0;
|
|
|
|
if ((revents & G_IO_IN) && node->io_read) {
|
|
|
|
node->io_read(node->opaque);
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
if ((revents & G_IO_OUT) && node->io_write) {
|
|
|
|
node->io_write(node->opaque);
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if the next select() will return an event, we have progressed */
|
|
|
|
if (event == event_notifier_get_handle(&ctx->notifier)) {
|
|
|
|
WSANETWORKEVENTS ev;
|
|
|
|
WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
|
|
|
|
if (ev.lNetworkEvents) {
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 19:07:56 +01:00
|
|
|
if (node->deleted) {
|
2017-01-12 19:07:58 +01:00
|
|
|
if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
|
2017-01-12 19:07:56 +01:00
|
|
|
QLIST_REMOVE(node, node);
|
|
|
|
g_free(node);
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
|
2017-01-12 19:07:56 +01:00
|
|
|
}
|
2012-09-24 14:57:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 11:53:03 +02:00
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2017-02-13 14:52:33 +01:00
|
|
|
void aio_dispatch(AioContext *ctx)
|
2014-07-09 11:53:03 +02:00
|
|
|
{
|
2017-02-13 14:52:34 +01:00
|
|
|
qemu_lockcnt_inc(&ctx->list_lock);
|
2017-02-13 14:52:33 +01:00
|
|
|
aio_bh_poll(ctx);
|
|
|
|
aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
|
2017-02-13 14:52:34 +01:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
2017-02-13 14:52:33 +01:00
|
|
|
timerlistgroup_run_timers(&ctx->tlg);
|
2014-07-09 11:53:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool aio_poll(AioContext *ctx, bool blocking)
|
|
|
|
{
|
|
|
|
AioHandler *node;
|
2022-10-19 12:20:15 +02:00
|
|
|
HANDLE events[MAXIMUM_WAIT_OBJECTS];
|
AioContext: fix broken ctx->dispatching optimization
This patch rewrites the ctx->dispatching optimization, which was the cause
of some mysterious hangs that could be reproduced on aarch64 KVM only.
The hangs were indirectly caused by aio_poll() and in particular by
flash memory updates's call to blk_write(), which invokes aio_poll().
Fun stuff: they had an extremely short race window, so much that
adding all kind of tracing to either the kernel or QEMU made it
go away (a single printf made it half as reproducible).
On the plus side, the failure mode (a hang until the next keypress)
made it very easy to examine the state of the process with a debugger.
And there was a very nice reproducer from Laszlo, which failed pretty
often (more than half of the time) on any version of QEMU with a non-debug
kernel; it also failed fast, while still in the firmware. So, it could
have been worse.
For some unknown reason they happened only with virtio-scsi, but
that's not important. It's more interesting that they disappeared with
io=native, making thread-pool.c a likely suspect for where the bug arose.
thread-pool.c is also one of the few places which use bottom halves
across threads, by the way.
I hope that no other similar bugs exist, but just in case :) I am
going to describe how the successful debugging went... Since the
likely culprit was the ctx->dispatching optimization, which mostly
affects bottom halves, the first observation was that there are two
qemu_bh_schedule() invocations in the thread pool: the one in the aio
worker and the one in thread_pool_completion_bh. The latter always
causes the optimization to trigger, the former may or may not. In
order to restrict the possibilities, I introduced new functions
qemu_bh_schedule_slow() and qemu_bh_schedule_fast():
/* qemu_bh_schedule_slow: */
ctx = bh->ctx;
bh->idle = 0;
if (atomic_xchg(&bh->scheduled, 1) == 0) {
event_notifier_set(&ctx->notifier);
}
/* qemu_bh_schedule_fast: */
ctx = bh->ctx;
bh->idle = 0;
assert(ctx->dispatching);
atomic_xchg(&bh->scheduled, 1);
Notice how the atomic_xchg is still in qemu_bh_schedule_slow(). This
was already debated a few months ago, so I assumed it to be correct.
In retrospect this was a very good idea, as you'll see later.
Changing thread_pool_completion_bh() to qemu_bh_schedule_fast() didn't
trigger the assertion (as expected). Changing the worker's invocation
to qemu_bh_schedule_slow() didn't hide the bug (another assumption
which luckily held). This already limited heavily the amount of
interaction between the threads, hinting that the problematic events
must have triggered around thread_pool_completion_bh().
As mentioned early, invoking a debugger to examine the state of a
hung process was pretty easy; the iothread was always waiting on a
poll(..., -1) system call. Infinite timeouts are much rarer on x86,
and this could be the reason why the bug was never observed there.
With the buggy sequence more or less resolved to an interaction between
thread_pool_completion_bh() and poll(..., -1), my "tracing" strategy was
to just add a few qemu_clock_get_ns(QEMU_CLOCK_REALTIME) calls, hoping
that the ordering of aio_ctx_prepare(), aio_ctx_dispatch, poll() and
qemu_bh_schedule_fast() would provide some hint. The output was:
(gdb) p last_prepare
$3 = 103885451
(gdb) p last_dispatch
$4 = 103876492
(gdb) p last_poll
$5 = 115909333
(gdb) p last_schedule
$6 = 115925212
Notice how the last call to qemu_poll_ns() came after aio_ctx_dispatch().
This makes little sense unless there is an aio_poll() call involved,
and indeed with a slightly different instrumentation you can see that
there is one:
(gdb) p last_prepare
$3 = 107569679
(gdb) p last_dispatch
$4 = 107561600
(gdb) p last_aio_poll
$5 = 110671400
(gdb) p last_schedule
$6 = 110698917
So the scenario becomes clearer:
iothread VCPU thread
--------------------------------------------------------------------------
aio_ctx_prepare
aio_ctx_check
qemu_poll_ns(timeout=-1)
aio_poll
aio_dispatch
thread_pool_completion_bh
qemu_bh_schedule()
At this point bh->scheduled = 1 and the iothread has not been woken up.
The solution must be close, but this alone should not be a problem,
because the bottom half is only rescheduled to account for rare situations
(see commit 3c80ca1, thread-pool: avoid deadlock in nested aio_poll()
calls, 2014-07-15).
Introducing a third thread---a thread pool worker thread, which
also does qemu_bh_schedule()---does bring out the problematic case.
The third thread must be awakened *after* the callback is complete and
thread_pool_completion_bh has redone the whole loop, explaining the
short race window. And then this is what happens:
thread pool worker
--------------------------------------------------------------------------
<I/O completes>
qemu_bh_schedule()
Tada, bh->scheduled is already 1, so qemu_bh_schedule() does nothing
and the iothread is never woken up. This is where the bh->scheduled
optimization comes into play---it is correct, but removing it would
have masked the bug.
So, what is the bug?
Well, the question asked by the ctx->dispatching optimization ("is any
active aio_poll dispatching?") was wrong. The right question to ask
instead is "is any active aio_poll *not* dispatching", i.e. in the prepare
or poll phases? In that case, the aio_poll is sleeping or might go to
sleep anytime soon, and the EventNotifier must be invoked to wake
it up.
In any other case (including if there is *no* active aio_poll at all!)
we can just wait for the next prepare phase to pick up the event (e.g. a
bottom half); the prepare phase will avoid the blocking and service the
bottom half.
Expressing the invariant with a logic formula, the broken one looked like:
!(exists(thread): in_dispatching(thread)) => !optimize
or equivalently:
!(exists(thread):
in_aio_poll(thread) && in_dispatching(thread)) => !optimize
In the correct one, the negation is in a slightly different place:
(exists(thread):
in_aio_poll(thread) && !in_dispatching(thread)) => !optimize
or equivalently:
(exists(thread): in_prepare_or_poll(thread)) => !optimize
Even if the difference boils down to moving an exclamation mark :)
the implementation is quite different. However, I think the new
one is simpler to understand.
In the old implementation, the "exists" was implemented with a boolean
value. This didn't really support well the case of multiple concurrent
event loops, but I thought that this was okay: aio_poll holds the
AioContext lock so there cannot be concurrent aio_poll invocations, and
I was just considering nested event loops. However, aio_poll _could_
indeed be concurrent with the GSource. This is why I came up with the
wrong invariant.
In the new implementation, "exists" is computed simply by counting how many
threads are in the prepare or poll phases. There are some interesting
points to consider, but the gist of the idea remains:
1) AioContext can be used through GSource as well; as mentioned in the
patch, bit 0 of the counter is reserved for the GSource.
2) the counter need not be updated for a non-blocking aio_poll, because
it won't sleep forever anyway. This is just a matter of checking
the "blocking" variable. This requires some changes to the win32
implementation, but is otherwise not too complicated.
3) as mentioned above, the new implementation will not call aio_notify
when there is *no* active aio_poll at all. The tests have to be
adjusted for this change. The calls to aio_notify in async.c are fine;
they only want to kick aio_poll out of a blocking wait, but need not
do anything if aio_poll is not running.
4) nested aio_poll: these just work with the new implementation; when
a nested event loop is invoked, the outer event loop is never in the
prepare or poll phases. The outer event loop thus has already decremented
the counter.
Reported-by: Richard W. M. Jones <rjones@redhat.com>
Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Message-id: 1437487673-23740-5-git-send-email-pbonzini@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2015-07-21 16:07:51 +02:00
|
|
|
bool progress, have_select_revents, first;
|
2022-10-19 12:20:15 +02:00
|
|
|
unsigned count;
|
2014-07-09 11:53:03 +02:00
|
|
|
int timeout;
|
|
|
|
|
async: use explicit memory barriers
When using C11 atomics, non-seqcst reads and writes do not participate
in the total order of seqcst operations. In util/async.c and util/aio-posix.c,
in particular, the pattern that we use
write ctx->notify_me write bh->scheduled
read bh->scheduled read ctx->notify_me
if !bh->scheduled, sleep if ctx->notify_me, notify
needs to use seqcst operations for both the write and the read. In
general this is something that we do not want, because there can be
many sources that are polled in addition to bottom halves. The
alternative is to place a seqcst memory barrier between the write
and the read. This also comes with a disadvantage, in that the
memory barrier is implicit on strongly-ordered architectures and
it wastes a few dozen clock cycles.
Fortunately, ctx->notify_me is never written concurrently by two
threads, so we can assert that and relax the writes to ctx->notify_me.
The resulting solution works and performs well on both aarch64 and x86.
Note that the atomic_set/atomic_read combination is not an atomic
read-modify-write, and therefore it is even weaker than C11 ATOMIC_RELAXED;
on x86, ATOMIC_RELAXED compiles to a locked operation.
Analyzed-by: Ying Fang <fangying1@huawei.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Tested-by: Ying Fang <fangying1@huawei.com>
Message-Id: <20200407140746.8041-6-pbonzini@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2020-04-07 16:07:46 +02:00
|
|
|
/*
|
|
|
|
* There cannot be two concurrent aio_poll calls for the same AioContext (or
|
|
|
|
* an aio_poll concurrent with a GSource prepare/check/dispatch callback).
|
|
|
|
* We rely on this below to avoid slow locked accesses to ctx->notify_me.
|
2020-10-21 08:40:33 +02:00
|
|
|
*
|
|
|
|
* aio_poll() may only be called in the AioContext's thread. iohandler_ctx
|
|
|
|
* is special in that it runs in the main thread, but that thread's context
|
|
|
|
* is qemu_aio_context.
|
async: use explicit memory barriers
When using C11 atomics, non-seqcst reads and writes do not participate
in the total order of seqcst operations. In util/async.c and util/aio-posix.c,
in particular, the pattern that we use
write ctx->notify_me write bh->scheduled
read bh->scheduled read ctx->notify_me
if !bh->scheduled, sleep if ctx->notify_me, notify
needs to use seqcst operations for both the write and the read. In
general this is something that we do not want, because there can be
many sources that are polled in addition to bottom halves. The
alternative is to place a seqcst memory barrier between the write
and the read. This also comes with a disadvantage, in that the
memory barrier is implicit on strongly-ordered architectures and
it wastes a few dozen clock cycles.
Fortunately, ctx->notify_me is never written concurrently by two
threads, so we can assert that and relax the writes to ctx->notify_me.
The resulting solution works and performs well on both aarch64 and x86.
Note that the atomic_set/atomic_read combination is not an atomic
read-modify-write, and therefore it is even weaker than C11 ATOMIC_RELAXED;
on x86, ATOMIC_RELAXED compiles to a locked operation.
Analyzed-by: Ying Fang <fangying1@huawei.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Tested-by: Ying Fang <fangying1@huawei.com>
Message-Id: <20200407140746.8041-6-pbonzini@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2020-04-07 16:07:46 +02:00
|
|
|
*/
|
2020-10-21 08:40:33 +02:00
|
|
|
assert(in_aio_context_home_thread(ctx == iohandler_get_aio_context() ?
|
|
|
|
qemu_get_aio_context() : ctx));
|
2014-07-09 11:53:03 +02:00
|
|
|
progress = false;
|
|
|
|
|
2014-07-09 11:53:07 +02:00
|
|
|
/* aio_notify can avoid the expensive event_notifier_set if
|
|
|
|
* everything (file descriptors, bottom halves, timers) will
|
|
|
|
* be re-evaluated before the next blocking poll(). This is
|
|
|
|
* already true when aio_poll is called with blocking == false;
|
AioContext: fix broken ctx->dispatching optimization
This patch rewrites the ctx->dispatching optimization, which was the cause
of some mysterious hangs that could be reproduced on aarch64 KVM only.
The hangs were indirectly caused by aio_poll() and in particular by
flash memory updates's call to blk_write(), which invokes aio_poll().
Fun stuff: they had an extremely short race window, so much that
adding all kind of tracing to either the kernel or QEMU made it
go away (a single printf made it half as reproducible).
On the plus side, the failure mode (a hang until the next keypress)
made it very easy to examine the state of the process with a debugger.
And there was a very nice reproducer from Laszlo, which failed pretty
often (more than half of the time) on any version of QEMU with a non-debug
kernel; it also failed fast, while still in the firmware. So, it could
have been worse.
For some unknown reason they happened only with virtio-scsi, but
that's not important. It's more interesting that they disappeared with
io=native, making thread-pool.c a likely suspect for where the bug arose.
thread-pool.c is also one of the few places which use bottom halves
across threads, by the way.
I hope that no other similar bugs exist, but just in case :) I am
going to describe how the successful debugging went... Since the
likely culprit was the ctx->dispatching optimization, which mostly
affects bottom halves, the first observation was that there are two
qemu_bh_schedule() invocations in the thread pool: the one in the aio
worker and the one in thread_pool_completion_bh. The latter always
causes the optimization to trigger, the former may or may not. In
order to restrict the possibilities, I introduced new functions
qemu_bh_schedule_slow() and qemu_bh_schedule_fast():
/* qemu_bh_schedule_slow: */
ctx = bh->ctx;
bh->idle = 0;
if (atomic_xchg(&bh->scheduled, 1) == 0) {
event_notifier_set(&ctx->notifier);
}
/* qemu_bh_schedule_fast: */
ctx = bh->ctx;
bh->idle = 0;
assert(ctx->dispatching);
atomic_xchg(&bh->scheduled, 1);
Notice how the atomic_xchg is still in qemu_bh_schedule_slow(). This
was already debated a few months ago, so I assumed it to be correct.
In retrospect this was a very good idea, as you'll see later.
Changing thread_pool_completion_bh() to qemu_bh_schedule_fast() didn't
trigger the assertion (as expected). Changing the worker's invocation
to qemu_bh_schedule_slow() didn't hide the bug (another assumption
which luckily held). This already limited heavily the amount of
interaction between the threads, hinting that the problematic events
must have triggered around thread_pool_completion_bh().
As mentioned early, invoking a debugger to examine the state of a
hung process was pretty easy; the iothread was always waiting on a
poll(..., -1) system call. Infinite timeouts are much rarer on x86,
and this could be the reason why the bug was never observed there.
With the buggy sequence more or less resolved to an interaction between
thread_pool_completion_bh() and poll(..., -1), my "tracing" strategy was
to just add a few qemu_clock_get_ns(QEMU_CLOCK_REALTIME) calls, hoping
that the ordering of aio_ctx_prepare(), aio_ctx_dispatch, poll() and
qemu_bh_schedule_fast() would provide some hint. The output was:
(gdb) p last_prepare
$3 = 103885451
(gdb) p last_dispatch
$4 = 103876492
(gdb) p last_poll
$5 = 115909333
(gdb) p last_schedule
$6 = 115925212
Notice how the last call to qemu_poll_ns() came after aio_ctx_dispatch().
This makes little sense unless there is an aio_poll() call involved,
and indeed with a slightly different instrumentation you can see that
there is one:
(gdb) p last_prepare
$3 = 107569679
(gdb) p last_dispatch
$4 = 107561600
(gdb) p last_aio_poll
$5 = 110671400
(gdb) p last_schedule
$6 = 110698917
So the scenario becomes clearer:
iothread VCPU thread
--------------------------------------------------------------------------
aio_ctx_prepare
aio_ctx_check
qemu_poll_ns(timeout=-1)
aio_poll
aio_dispatch
thread_pool_completion_bh
qemu_bh_schedule()
At this point bh->scheduled = 1 and the iothread has not been woken up.
The solution must be close, but this alone should not be a problem,
because the bottom half is only rescheduled to account for rare situations
(see commit 3c80ca1, thread-pool: avoid deadlock in nested aio_poll()
calls, 2014-07-15).
Introducing a third thread---a thread pool worker thread, which
also does qemu_bh_schedule()---does bring out the problematic case.
The third thread must be awakened *after* the callback is complete and
thread_pool_completion_bh has redone the whole loop, explaining the
short race window. And then this is what happens:
thread pool worker
--------------------------------------------------------------------------
<I/O completes>
qemu_bh_schedule()
Tada, bh->scheduled is already 1, so qemu_bh_schedule() does nothing
and the iothread is never woken up. This is where the bh->scheduled
optimization comes into play---it is correct, but removing it would
have masked the bug.
So, what is the bug?
Well, the question asked by the ctx->dispatching optimization ("is any
active aio_poll dispatching?") was wrong. The right question to ask
instead is "is any active aio_poll *not* dispatching", i.e. in the prepare
or poll phases? In that case, the aio_poll is sleeping or might go to
sleep anytime soon, and the EventNotifier must be invoked to wake
it up.
In any other case (including if there is *no* active aio_poll at all!)
we can just wait for the next prepare phase to pick up the event (e.g. a
bottom half); the prepare phase will avoid the blocking and service the
bottom half.
Expressing the invariant with a logic formula, the broken one looked like:
!(exists(thread): in_dispatching(thread)) => !optimize
or equivalently:
!(exists(thread):
in_aio_poll(thread) && in_dispatching(thread)) => !optimize
In the correct one, the negation is in a slightly different place:
(exists(thread):
in_aio_poll(thread) && !in_dispatching(thread)) => !optimize
or equivalently:
(exists(thread): in_prepare_or_poll(thread)) => !optimize
Even if the difference boils down to moving an exclamation mark :)
the implementation is quite different. However, I think the new
one is simpler to understand.
In the old implementation, the "exists" was implemented with a boolean
value. This didn't really support well the case of multiple concurrent
event loops, but I thought that this was okay: aio_poll holds the
AioContext lock so there cannot be concurrent aio_poll invocations, and
I was just considering nested event loops. However, aio_poll _could_
indeed be concurrent with the GSource. This is why I came up with the
wrong invariant.
In the new implementation, "exists" is computed simply by counting how many
threads are in the prepare or poll phases. There are some interesting
points to consider, but the gist of the idea remains:
1) AioContext can be used through GSource as well; as mentioned in the
patch, bit 0 of the counter is reserved for the GSource.
2) the counter need not be updated for a non-blocking aio_poll, because
it won't sleep forever anyway. This is just a matter of checking
the "blocking" variable. This requires some changes to the win32
implementation, but is otherwise not too complicated.
3) as mentioned above, the new implementation will not call aio_notify
when there is *no* active aio_poll at all. The tests have to be
adjusted for this change. The calls to aio_notify in async.c are fine;
they only want to kick aio_poll out of a blocking wait, but need not
do anything if aio_poll is not running.
4) nested aio_poll: these just work with the new implementation; when
a nested event loop is invoked, the outer event loop is never in the
prepare or poll phases. The outer event loop thus has already decremented
the counter.
Reported-by: Richard W. M. Jones <rjones@redhat.com>
Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Message-id: 1437487673-23740-5-git-send-email-pbonzini@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2015-07-21 16:07:51 +02:00
|
|
|
* if blocking == true, it is only true after poll() returns,
|
|
|
|
* so disable the optimization now.
|
2014-07-09 11:53:07 +02:00
|
|
|
*/
|
AioContext: fix broken ctx->dispatching optimization
This patch rewrites the ctx->dispatching optimization, which was the cause
of some mysterious hangs that could be reproduced on aarch64 KVM only.
The hangs were indirectly caused by aio_poll() and in particular by
flash memory updates's call to blk_write(), which invokes aio_poll().
Fun stuff: they had an extremely short race window, so much that
adding all kind of tracing to either the kernel or QEMU made it
go away (a single printf made it half as reproducible).
On the plus side, the failure mode (a hang until the next keypress)
made it very easy to examine the state of the process with a debugger.
And there was a very nice reproducer from Laszlo, which failed pretty
often (more than half of the time) on any version of QEMU with a non-debug
kernel; it also failed fast, while still in the firmware. So, it could
have been worse.
For some unknown reason they happened only with virtio-scsi, but
that's not important. It's more interesting that they disappeared with
io=native, making thread-pool.c a likely suspect for where the bug arose.
thread-pool.c is also one of the few places which use bottom halves
across threads, by the way.
I hope that no other similar bugs exist, but just in case :) I am
going to describe how the successful debugging went... Since the
likely culprit was the ctx->dispatching optimization, which mostly
affects bottom halves, the first observation was that there are two
qemu_bh_schedule() invocations in the thread pool: the one in the aio
worker and the one in thread_pool_completion_bh. The latter always
causes the optimization to trigger, the former may or may not. In
order to restrict the possibilities, I introduced new functions
qemu_bh_schedule_slow() and qemu_bh_schedule_fast():
/* qemu_bh_schedule_slow: */
ctx = bh->ctx;
bh->idle = 0;
if (atomic_xchg(&bh->scheduled, 1) == 0) {
event_notifier_set(&ctx->notifier);
}
/* qemu_bh_schedule_fast: */
ctx = bh->ctx;
bh->idle = 0;
assert(ctx->dispatching);
atomic_xchg(&bh->scheduled, 1);
Notice how the atomic_xchg is still in qemu_bh_schedule_slow(). This
was already debated a few months ago, so I assumed it to be correct.
In retrospect this was a very good idea, as you'll see later.
Changing thread_pool_completion_bh() to qemu_bh_schedule_fast() didn't
trigger the assertion (as expected). Changing the worker's invocation
to qemu_bh_schedule_slow() didn't hide the bug (another assumption
which luckily held). This already limited heavily the amount of
interaction between the threads, hinting that the problematic events
must have triggered around thread_pool_completion_bh().
As mentioned early, invoking a debugger to examine the state of a
hung process was pretty easy; the iothread was always waiting on a
poll(..., -1) system call. Infinite timeouts are much rarer on x86,
and this could be the reason why the bug was never observed there.
With the buggy sequence more or less resolved to an interaction between
thread_pool_completion_bh() and poll(..., -1), my "tracing" strategy was
to just add a few qemu_clock_get_ns(QEMU_CLOCK_REALTIME) calls, hoping
that the ordering of aio_ctx_prepare(), aio_ctx_dispatch, poll() and
qemu_bh_schedule_fast() would provide some hint. The output was:
(gdb) p last_prepare
$3 = 103885451
(gdb) p last_dispatch
$4 = 103876492
(gdb) p last_poll
$5 = 115909333
(gdb) p last_schedule
$6 = 115925212
Notice how the last call to qemu_poll_ns() came after aio_ctx_dispatch().
This makes little sense unless there is an aio_poll() call involved,
and indeed with a slightly different instrumentation you can see that
there is one:
(gdb) p last_prepare
$3 = 107569679
(gdb) p last_dispatch
$4 = 107561600
(gdb) p last_aio_poll
$5 = 110671400
(gdb) p last_schedule
$6 = 110698917
So the scenario becomes clearer:
iothread VCPU thread
--------------------------------------------------------------------------
aio_ctx_prepare
aio_ctx_check
qemu_poll_ns(timeout=-1)
aio_poll
aio_dispatch
thread_pool_completion_bh
qemu_bh_schedule()
At this point bh->scheduled = 1 and the iothread has not been woken up.
The solution must be close, but this alone should not be a problem,
because the bottom half is only rescheduled to account for rare situations
(see commit 3c80ca1, thread-pool: avoid deadlock in nested aio_poll()
calls, 2014-07-15).
Introducing a third thread---a thread pool worker thread, which
also does qemu_bh_schedule()---does bring out the problematic case.
The third thread must be awakened *after* the callback is complete and
thread_pool_completion_bh has redone the whole loop, explaining the
short race window. And then this is what happens:
thread pool worker
--------------------------------------------------------------------------
<I/O completes>
qemu_bh_schedule()
Tada, bh->scheduled is already 1, so qemu_bh_schedule() does nothing
and the iothread is never woken up. This is where the bh->scheduled
optimization comes into play---it is correct, but removing it would
have masked the bug.
So, what is the bug?
Well, the question asked by the ctx->dispatching optimization ("is any
active aio_poll dispatching?") was wrong. The right question to ask
instead is "is any active aio_poll *not* dispatching", i.e. in the prepare
or poll phases? In that case, the aio_poll is sleeping or might go to
sleep anytime soon, and the EventNotifier must be invoked to wake
it up.
In any other case (including if there is *no* active aio_poll at all!)
we can just wait for the next prepare phase to pick up the event (e.g. a
bottom half); the prepare phase will avoid the blocking and service the
bottom half.
Expressing the invariant with a logic formula, the broken one looked like:
!(exists(thread): in_dispatching(thread)) => !optimize
or equivalently:
!(exists(thread):
in_aio_poll(thread) && in_dispatching(thread)) => !optimize
In the correct one, the negation is in a slightly different place:
(exists(thread):
in_aio_poll(thread) && !in_dispatching(thread)) => !optimize
or equivalently:
(exists(thread): in_prepare_or_poll(thread)) => !optimize
Even if the difference boils down to moving an exclamation mark :)
the implementation is quite different. However, I think the new
one is simpler to understand.
In the old implementation, the "exists" was implemented with a boolean
value. This didn't really support well the case of multiple concurrent
event loops, but I thought that this was okay: aio_poll holds the
AioContext lock so there cannot be concurrent aio_poll invocations, and
I was just considering nested event loops. However, aio_poll _could_
indeed be concurrent with the GSource. This is why I came up with the
wrong invariant.
In the new implementation, "exists" is computed simply by counting how many
threads are in the prepare or poll phases. There are some interesting
points to consider, but the gist of the idea remains:
1) AioContext can be used through GSource as well; as mentioned in the
patch, bit 0 of the counter is reserved for the GSource.
2) the counter need not be updated for a non-blocking aio_poll, because
it won't sleep forever anyway. This is just a matter of checking
the "blocking" variable. This requires some changes to the win32
implementation, but is otherwise not too complicated.
3) as mentioned above, the new implementation will not call aio_notify
when there is *no* active aio_poll at all. The tests have to be
adjusted for this change. The calls to aio_notify in async.c are fine;
they only want to kick aio_poll out of a blocking wait, but need not
do anything if aio_poll is not running.
4) nested aio_poll: these just work with the new implementation; when
a nested event loop is invoked, the outer event loop is never in the
prepare or poll phases. The outer event loop thus has already decremented
the counter.
Reported-by: Richard W. M. Jones <rjones@redhat.com>
Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Message-id: 1437487673-23740-5-git-send-email-pbonzini@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2015-07-21 16:07:51 +02:00
|
|
|
if (blocking) {
|
2020-09-23 12:56:46 +02:00
|
|
|
qatomic_set(&ctx->notify_me, qatomic_read(&ctx->notify_me) + 2);
|
async: use explicit memory barriers
When using C11 atomics, non-seqcst reads and writes do not participate
in the total order of seqcst operations. In util/async.c and util/aio-posix.c,
in particular, the pattern that we use
write ctx->notify_me write bh->scheduled
read bh->scheduled read ctx->notify_me
if !bh->scheduled, sleep if ctx->notify_me, notify
needs to use seqcst operations for both the write and the read. In
general this is something that we do not want, because there can be
many sources that are polled in addition to bottom halves. The
alternative is to place a seqcst memory barrier between the write
and the read. This also comes with a disadvantage, in that the
memory barrier is implicit on strongly-ordered architectures and
it wastes a few dozen clock cycles.
Fortunately, ctx->notify_me is never written concurrently by two
threads, so we can assert that and relax the writes to ctx->notify_me.
The resulting solution works and performs well on both aarch64 and x86.
Note that the atomic_set/atomic_read combination is not an atomic
read-modify-write, and therefore it is even weaker than C11 ATOMIC_RELAXED;
on x86, ATOMIC_RELAXED compiles to a locked operation.
Analyzed-by: Ying Fang <fangying1@huawei.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Tested-by: Ying Fang <fangying1@huawei.com>
Message-Id: <20200407140746.8041-6-pbonzini@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2020-04-07 16:07:46 +02:00
|
|
|
/*
|
|
|
|
* Write ctx->notify_me before computing the timeout
|
|
|
|
* (reading bottom half flags, etc.). Pairs with
|
|
|
|
* smp_mb in aio_notify().
|
|
|
|
*/
|
|
|
|
smp_mb();
|
AioContext: fix broken ctx->dispatching optimization
This patch rewrites the ctx->dispatching optimization, which was the cause
of some mysterious hangs that could be reproduced on aarch64 KVM only.
The hangs were indirectly caused by aio_poll() and in particular by
flash memory updates's call to blk_write(), which invokes aio_poll().
Fun stuff: they had an extremely short race window, so much that
adding all kind of tracing to either the kernel or QEMU made it
go away (a single printf made it half as reproducible).
On the plus side, the failure mode (a hang until the next keypress)
made it very easy to examine the state of the process with a debugger.
And there was a very nice reproducer from Laszlo, which failed pretty
often (more than half of the time) on any version of QEMU with a non-debug
kernel; it also failed fast, while still in the firmware. So, it could
have been worse.
For some unknown reason they happened only with virtio-scsi, but
that's not important. It's more interesting that they disappeared with
io=native, making thread-pool.c a likely suspect for where the bug arose.
thread-pool.c is also one of the few places which use bottom halves
across threads, by the way.
I hope that no other similar bugs exist, but just in case :) I am
going to describe how the successful debugging went... Since the
likely culprit was the ctx->dispatching optimization, which mostly
affects bottom halves, the first observation was that there are two
qemu_bh_schedule() invocations in the thread pool: the one in the aio
worker and the one in thread_pool_completion_bh. The latter always
causes the optimization to trigger, the former may or may not. In
order to restrict the possibilities, I introduced new functions
qemu_bh_schedule_slow() and qemu_bh_schedule_fast():
/* qemu_bh_schedule_slow: */
ctx = bh->ctx;
bh->idle = 0;
if (atomic_xchg(&bh->scheduled, 1) == 0) {
event_notifier_set(&ctx->notifier);
}
/* qemu_bh_schedule_fast: */
ctx = bh->ctx;
bh->idle = 0;
assert(ctx->dispatching);
atomic_xchg(&bh->scheduled, 1);
Notice how the atomic_xchg is still in qemu_bh_schedule_slow(). This
was already debated a few months ago, so I assumed it to be correct.
In retrospect this was a very good idea, as you'll see later.
Changing thread_pool_completion_bh() to qemu_bh_schedule_fast() didn't
trigger the assertion (as expected). Changing the worker's invocation
to qemu_bh_schedule_slow() didn't hide the bug (another assumption
which luckily held). This already limited heavily the amount of
interaction between the threads, hinting that the problematic events
must have triggered around thread_pool_completion_bh().
As mentioned early, invoking a debugger to examine the state of a
hung process was pretty easy; the iothread was always waiting on a
poll(..., -1) system call. Infinite timeouts are much rarer on x86,
and this could be the reason why the bug was never observed there.
With the buggy sequence more or less resolved to an interaction between
thread_pool_completion_bh() and poll(..., -1), my "tracing" strategy was
to just add a few qemu_clock_get_ns(QEMU_CLOCK_REALTIME) calls, hoping
that the ordering of aio_ctx_prepare(), aio_ctx_dispatch, poll() and
qemu_bh_schedule_fast() would provide some hint. The output was:
(gdb) p last_prepare
$3 = 103885451
(gdb) p last_dispatch
$4 = 103876492
(gdb) p last_poll
$5 = 115909333
(gdb) p last_schedule
$6 = 115925212
Notice how the last call to qemu_poll_ns() came after aio_ctx_dispatch().
This makes little sense unless there is an aio_poll() call involved,
and indeed with a slightly different instrumentation you can see that
there is one:
(gdb) p last_prepare
$3 = 107569679
(gdb) p last_dispatch
$4 = 107561600
(gdb) p last_aio_poll
$5 = 110671400
(gdb) p last_schedule
$6 = 110698917
So the scenario becomes clearer:
iothread VCPU thread
--------------------------------------------------------------------------
aio_ctx_prepare
aio_ctx_check
qemu_poll_ns(timeout=-1)
aio_poll
aio_dispatch
thread_pool_completion_bh
qemu_bh_schedule()
At this point bh->scheduled = 1 and the iothread has not been woken up.
The solution must be close, but this alone should not be a problem,
because the bottom half is only rescheduled to account for rare situations
(see commit 3c80ca1, thread-pool: avoid deadlock in nested aio_poll()
calls, 2014-07-15).
Introducing a third thread---a thread pool worker thread, which
also does qemu_bh_schedule()---does bring out the problematic case.
The third thread must be awakened *after* the callback is complete and
thread_pool_completion_bh has redone the whole loop, explaining the
short race window. And then this is what happens:
thread pool worker
--------------------------------------------------------------------------
<I/O completes>
qemu_bh_schedule()
Tada, bh->scheduled is already 1, so qemu_bh_schedule() does nothing
and the iothread is never woken up. This is where the bh->scheduled
optimization comes into play---it is correct, but removing it would
have masked the bug.
So, what is the bug?
Well, the question asked by the ctx->dispatching optimization ("is any
active aio_poll dispatching?") was wrong. The right question to ask
instead is "is any active aio_poll *not* dispatching", i.e. in the prepare
or poll phases? In that case, the aio_poll is sleeping or might go to
sleep anytime soon, and the EventNotifier must be invoked to wake
it up.
In any other case (including if there is *no* active aio_poll at all!)
we can just wait for the next prepare phase to pick up the event (e.g. a
bottom half); the prepare phase will avoid the blocking and service the
bottom half.
Expressing the invariant with a logic formula, the broken one looked like:
!(exists(thread): in_dispatching(thread)) => !optimize
or equivalently:
!(exists(thread):
in_aio_poll(thread) && in_dispatching(thread)) => !optimize
In the correct one, the negation is in a slightly different place:
(exists(thread):
in_aio_poll(thread) && !in_dispatching(thread)) => !optimize
or equivalently:
(exists(thread): in_prepare_or_poll(thread)) => !optimize
Even if the difference boils down to moving an exclamation mark :)
the implementation is quite different. However, I think the new
one is simpler to understand.
In the old implementation, the "exists" was implemented with a boolean
value. This didn't really support well the case of multiple concurrent
event loops, but I thought that this was okay: aio_poll holds the
AioContext lock so there cannot be concurrent aio_poll invocations, and
I was just considering nested event loops. However, aio_poll _could_
indeed be concurrent with the GSource. This is why I came up with the
wrong invariant.
In the new implementation, "exists" is computed simply by counting how many
threads are in the prepare or poll phases. There are some interesting
points to consider, but the gist of the idea remains:
1) AioContext can be used through GSource as well; as mentioned in the
patch, bit 0 of the counter is reserved for the GSource.
2) the counter need not be updated for a non-blocking aio_poll, because
it won't sleep forever anyway. This is just a matter of checking
the "blocking" variable. This requires some changes to the win32
implementation, but is otherwise not too complicated.
3) as mentioned above, the new implementation will not call aio_notify
when there is *no* active aio_poll at all. The tests have to be
adjusted for this change. The calls to aio_notify in async.c are fine;
they only want to kick aio_poll out of a blocking wait, but need not
do anything if aio_poll is not running.
4) nested aio_poll: these just work with the new implementation; when
a nested event loop is invoked, the outer event loop is never in the
prepare or poll phases. The outer event loop thus has already decremented
the counter.
Reported-by: Richard W. M. Jones <rjones@redhat.com>
Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Message-id: 1437487673-23740-5-git-send-email-pbonzini@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2015-07-21 16:07:51 +02:00
|
|
|
}
|
2014-07-09 11:53:07 +02:00
|
|
|
|
2017-01-12 19:07:58 +01:00
|
|
|
qemu_lockcnt_inc(&ctx->list_lock);
|
2015-07-21 16:07:50 +02:00
|
|
|
have_select_revents = aio_prepare(ctx);
|
|
|
|
|
2012-04-12 14:00:56 +02:00
|
|
|
/* fill fd sets */
|
2012-06-09 04:01:51 +02:00
|
|
|
count = 0;
|
2017-01-12 19:07:58 +01:00
|
|
|
QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
|
2015-10-23 05:08:08 +02:00
|
|
|
if (!node->deleted && node->io_notify
|
|
|
|
&& aio_node_check(ctx, node->is_external)) {
|
2022-10-19 12:20:15 +02:00
|
|
|
assert(count < MAXIMUM_WAIT_OBJECTS);
|
2012-06-09 04:01:51 +02:00
|
|
|
events[count++] = event_notifier_get_handle(node->e);
|
2012-04-12 14:00:56 +02:00
|
|
|
}
|
|
|
|
}
|
2008-09-22 21:17:18 +02:00
|
|
|
|
2014-07-09 11:53:04 +02:00
|
|
|
first = true;
|
2008-09-22 21:17:18 +02:00
|
|
|
|
2015-07-21 16:07:50 +02:00
|
|
|
/* ctx->notifier is always registered. */
|
|
|
|
assert(count > 0);
|
|
|
|
|
|
|
|
/* Multiple iterations, all of them non-blocking except the first,
|
|
|
|
* may be necessary to process all pending events. After the first
|
|
|
|
* WaitForMultipleObjects call ctx->notify_me will be decremented.
|
|
|
|
*/
|
|
|
|
do {
|
2014-07-09 11:53:10 +02:00
|
|
|
HANDLE event;
|
2013-08-21 17:02:53 +02:00
|
|
|
int ret;
|
|
|
|
|
2015-07-21 16:07:50 +02:00
|
|
|
timeout = blocking && !have_select_revents
|
2014-07-09 11:53:01 +02:00
|
|
|
? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
|
2013-08-21 17:02:53 +02:00
|
|
|
ret = WaitForMultipleObjects(count, events, FALSE, timeout);
|
AioContext: fix broken ctx->dispatching optimization
This patch rewrites the ctx->dispatching optimization, which was the cause
of some mysterious hangs that could be reproduced on aarch64 KVM only.
The hangs were indirectly caused by aio_poll() and in particular by
flash memory updates's call to blk_write(), which invokes aio_poll().
Fun stuff: they had an extremely short race window, so much that
adding all kind of tracing to either the kernel or QEMU made it
go away (a single printf made it half as reproducible).
On the plus side, the failure mode (a hang until the next keypress)
made it very easy to examine the state of the process with a debugger.
And there was a very nice reproducer from Laszlo, which failed pretty
often (more than half of the time) on any version of QEMU with a non-debug
kernel; it also failed fast, while still in the firmware. So, it could
have been worse.
For some unknown reason they happened only with virtio-scsi, but
that's not important. It's more interesting that they disappeared with
io=native, making thread-pool.c a likely suspect for where the bug arose.
thread-pool.c is also one of the few places which use bottom halves
across threads, by the way.
I hope that no other similar bugs exist, but just in case :) I am
going to describe how the successful debugging went... Since the
likely culprit was the ctx->dispatching optimization, which mostly
affects bottom halves, the first observation was that there are two
qemu_bh_schedule() invocations in the thread pool: the one in the aio
worker and the one in thread_pool_completion_bh. The latter always
causes the optimization to trigger, the former may or may not. In
order to restrict the possibilities, I introduced new functions
qemu_bh_schedule_slow() and qemu_bh_schedule_fast():
/* qemu_bh_schedule_slow: */
ctx = bh->ctx;
bh->idle = 0;
if (atomic_xchg(&bh->scheduled, 1) == 0) {
event_notifier_set(&ctx->notifier);
}
/* qemu_bh_schedule_fast: */
ctx = bh->ctx;
bh->idle = 0;
assert(ctx->dispatching);
atomic_xchg(&bh->scheduled, 1);
Notice how the atomic_xchg is still in qemu_bh_schedule_slow(). This
was already debated a few months ago, so I assumed it to be correct.
In retrospect this was a very good idea, as you'll see later.
Changing thread_pool_completion_bh() to qemu_bh_schedule_fast() didn't
trigger the assertion (as expected). Changing the worker's invocation
to qemu_bh_schedule_slow() didn't hide the bug (another assumption
which luckily held). This already limited heavily the amount of
interaction between the threads, hinting that the problematic events
must have triggered around thread_pool_completion_bh().
As mentioned early, invoking a debugger to examine the state of a
hung process was pretty easy; the iothread was always waiting on a
poll(..., -1) system call. Infinite timeouts are much rarer on x86,
and this could be the reason why the bug was never observed there.
With the buggy sequence more or less resolved to an interaction between
thread_pool_completion_bh() and poll(..., -1), my "tracing" strategy was
to just add a few qemu_clock_get_ns(QEMU_CLOCK_REALTIME) calls, hoping
that the ordering of aio_ctx_prepare(), aio_ctx_dispatch, poll() and
qemu_bh_schedule_fast() would provide some hint. The output was:
(gdb) p last_prepare
$3 = 103885451
(gdb) p last_dispatch
$4 = 103876492
(gdb) p last_poll
$5 = 115909333
(gdb) p last_schedule
$6 = 115925212
Notice how the last call to qemu_poll_ns() came after aio_ctx_dispatch().
This makes little sense unless there is an aio_poll() call involved,
and indeed with a slightly different instrumentation you can see that
there is one:
(gdb) p last_prepare
$3 = 107569679
(gdb) p last_dispatch
$4 = 107561600
(gdb) p last_aio_poll
$5 = 110671400
(gdb) p last_schedule
$6 = 110698917
So the scenario becomes clearer:
iothread VCPU thread
--------------------------------------------------------------------------
aio_ctx_prepare
aio_ctx_check
qemu_poll_ns(timeout=-1)
aio_poll
aio_dispatch
thread_pool_completion_bh
qemu_bh_schedule()
At this point bh->scheduled = 1 and the iothread has not been woken up.
The solution must be close, but this alone should not be a problem,
because the bottom half is only rescheduled to account for rare situations
(see commit 3c80ca1, thread-pool: avoid deadlock in nested aio_poll()
calls, 2014-07-15).
Introducing a third thread---a thread pool worker thread, which
also does qemu_bh_schedule()---does bring out the problematic case.
The third thread must be awakened *after* the callback is complete and
thread_pool_completion_bh has redone the whole loop, explaining the
short race window. And then this is what happens:
thread pool worker
--------------------------------------------------------------------------
<I/O completes>
qemu_bh_schedule()
Tada, bh->scheduled is already 1, so qemu_bh_schedule() does nothing
and the iothread is never woken up. This is where the bh->scheduled
optimization comes into play---it is correct, but removing it would
have masked the bug.
So, what is the bug?
Well, the question asked by the ctx->dispatching optimization ("is any
active aio_poll dispatching?") was wrong. The right question to ask
instead is "is any active aio_poll *not* dispatching", i.e. in the prepare
or poll phases? In that case, the aio_poll is sleeping or might go to
sleep anytime soon, and the EventNotifier must be invoked to wake
it up.
In any other case (including if there is *no* active aio_poll at all!)
we can just wait for the next prepare phase to pick up the event (e.g. a
bottom half); the prepare phase will avoid the blocking and service the
bottom half.
Expressing the invariant with a logic formula, the broken one looked like:
!(exists(thread): in_dispatching(thread)) => !optimize
or equivalently:
!(exists(thread):
in_aio_poll(thread) && in_dispatching(thread)) => !optimize
In the correct one, the negation is in a slightly different place:
(exists(thread):
in_aio_poll(thread) && !in_dispatching(thread)) => !optimize
or equivalently:
(exists(thread): in_prepare_or_poll(thread)) => !optimize
Even if the difference boils down to moving an exclamation mark :)
the implementation is quite different. However, I think the new
one is simpler to understand.
In the old implementation, the "exists" was implemented with a boolean
value. This didn't really support well the case of multiple concurrent
event loops, but I thought that this was okay: aio_poll holds the
AioContext lock so there cannot be concurrent aio_poll invocations, and
I was just considering nested event loops. However, aio_poll _could_
indeed be concurrent with the GSource. This is why I came up with the
wrong invariant.
In the new implementation, "exists" is computed simply by counting how many
threads are in the prepare or poll phases. There are some interesting
points to consider, but the gist of the idea remains:
1) AioContext can be used through GSource as well; as mentioned in the
patch, bit 0 of the counter is reserved for the GSource.
2) the counter need not be updated for a non-blocking aio_poll, because
it won't sleep forever anyway. This is just a matter of checking
the "blocking" variable. This requires some changes to the win32
implementation, but is otherwise not too complicated.
3) as mentioned above, the new implementation will not call aio_notify
when there is *no* active aio_poll at all. The tests have to be
adjusted for this change. The calls to aio_notify in async.c are fine;
they only want to kick aio_poll out of a blocking wait, but need not
do anything if aio_poll is not running.
4) nested aio_poll: these just work with the new implementation; when
a nested event loop is invoked, the outer event loop is never in the
prepare or poll phases. The outer event loop thus has already decremented
the counter.
Reported-by: Richard W. M. Jones <rjones@redhat.com>
Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Message-id: 1437487673-23740-5-git-send-email-pbonzini@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2015-07-21 16:07:51 +02:00
|
|
|
if (blocking) {
|
|
|
|
assert(first);
|
2020-09-23 12:56:46 +02:00
|
|
|
qatomic_store_release(&ctx->notify_me,
|
|
|
|
qatomic_read(&ctx->notify_me) - 2);
|
aio: Do aio_notify_accept only during blocking aio_poll
An aio_notify() pairs with an aio_notify_accept(). The former should
happen in the main thread or a vCPU thread, and the latter should be
done in the IOThread.
There is one rare case that the main thread or vCPU thread may "steal"
the aio_notify() event just raised by itself, in bdrv_set_aio_context()
[1]. The sequence is like this:
main thread IO Thread
===============================================================
bdrv_drained_begin()
aio_disable_external(ctx)
aio_poll(ctx, true)
ctx->notify_me += 2
...
bdrv_drained_end()
...
aio_notify()
...
bdrv_set_aio_context()
aio_poll(ctx, false)
[1] aio_notify_accept(ctx)
ppoll() /* Hang! */
[1] is problematic. It will clear the ctx->notifier event so that
the blocked ppoll() will not return.
(For the curious, this bug was noticed when booting a number of VMs
simultaneously in RHV. One or two of the VMs will hit this race
condition, making the VIRTIO device unresponsive to I/O commands. When
it hangs, Seabios is busy waiting for a read request to complete (read
MBR), right after initializing the virtio-blk-pci device, using 100%
guest CPU. See also https://bugzilla.redhat.com/show_bug.cgi?id=1562750
for the original bug analysis.)
aio_notify() only injects an event when ctx->notify_me is set,
correspondingly aio_notify_accept() is only useful when ctx->notify_me
_was_ set. Move the call to it into the "blocking" branch. This will
effectively skip [1] and fix the hang.
Furthermore, blocking aio_poll is only allowed on home thread
(in_aio_context_home_thread), because otherwise two blocking
aio_poll()'s can steal each other's ctx->notifier event and cause
hanging just like described above.
Cc: qemu-stable@nongnu.org
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <20180809132259.18402-3-famz@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
2018-08-09 15:22:59 +02:00
|
|
|
aio_notify_accept(ctx);
|
AioContext: fix broken ctx->dispatching optimization
This patch rewrites the ctx->dispatching optimization, which was the cause
of some mysterious hangs that could be reproduced on aarch64 KVM only.
The hangs were indirectly caused by aio_poll() and in particular by
flash memory updates's call to blk_write(), which invokes aio_poll().
Fun stuff: they had an extremely short race window, so much that
adding all kind of tracing to either the kernel or QEMU made it
go away (a single printf made it half as reproducible).
On the plus side, the failure mode (a hang until the next keypress)
made it very easy to examine the state of the process with a debugger.
And there was a very nice reproducer from Laszlo, which failed pretty
often (more than half of the time) on any version of QEMU with a non-debug
kernel; it also failed fast, while still in the firmware. So, it could
have been worse.
For some unknown reason they happened only with virtio-scsi, but
that's not important. It's more interesting that they disappeared with
io=native, making thread-pool.c a likely suspect for where the bug arose.
thread-pool.c is also one of the few places which use bottom halves
across threads, by the way.
I hope that no other similar bugs exist, but just in case :) I am
going to describe how the successful debugging went... Since the
likely culprit was the ctx->dispatching optimization, which mostly
affects bottom halves, the first observation was that there are two
qemu_bh_schedule() invocations in the thread pool: the one in the aio
worker and the one in thread_pool_completion_bh. The latter always
causes the optimization to trigger, the former may or may not. In
order to restrict the possibilities, I introduced new functions
qemu_bh_schedule_slow() and qemu_bh_schedule_fast():
/* qemu_bh_schedule_slow: */
ctx = bh->ctx;
bh->idle = 0;
if (atomic_xchg(&bh->scheduled, 1) == 0) {
event_notifier_set(&ctx->notifier);
}
/* qemu_bh_schedule_fast: */
ctx = bh->ctx;
bh->idle = 0;
assert(ctx->dispatching);
atomic_xchg(&bh->scheduled, 1);
Notice how the atomic_xchg is still in qemu_bh_schedule_slow(). This
was already debated a few months ago, so I assumed it to be correct.
In retrospect this was a very good idea, as you'll see later.
Changing thread_pool_completion_bh() to qemu_bh_schedule_fast() didn't
trigger the assertion (as expected). Changing the worker's invocation
to qemu_bh_schedule_slow() didn't hide the bug (another assumption
which luckily held). This already limited heavily the amount of
interaction between the threads, hinting that the problematic events
must have triggered around thread_pool_completion_bh().
As mentioned early, invoking a debugger to examine the state of a
hung process was pretty easy; the iothread was always waiting on a
poll(..., -1) system call. Infinite timeouts are much rarer on x86,
and this could be the reason why the bug was never observed there.
With the buggy sequence more or less resolved to an interaction between
thread_pool_completion_bh() and poll(..., -1), my "tracing" strategy was
to just add a few qemu_clock_get_ns(QEMU_CLOCK_REALTIME) calls, hoping
that the ordering of aio_ctx_prepare(), aio_ctx_dispatch, poll() and
qemu_bh_schedule_fast() would provide some hint. The output was:
(gdb) p last_prepare
$3 = 103885451
(gdb) p last_dispatch
$4 = 103876492
(gdb) p last_poll
$5 = 115909333
(gdb) p last_schedule
$6 = 115925212
Notice how the last call to qemu_poll_ns() came after aio_ctx_dispatch().
This makes little sense unless there is an aio_poll() call involved,
and indeed with a slightly different instrumentation you can see that
there is one:
(gdb) p last_prepare
$3 = 107569679
(gdb) p last_dispatch
$4 = 107561600
(gdb) p last_aio_poll
$5 = 110671400
(gdb) p last_schedule
$6 = 110698917
So the scenario becomes clearer:
iothread VCPU thread
--------------------------------------------------------------------------
aio_ctx_prepare
aio_ctx_check
qemu_poll_ns(timeout=-1)
aio_poll
aio_dispatch
thread_pool_completion_bh
qemu_bh_schedule()
At this point bh->scheduled = 1 and the iothread has not been woken up.
The solution must be close, but this alone should not be a problem,
because the bottom half is only rescheduled to account for rare situations
(see commit 3c80ca1, thread-pool: avoid deadlock in nested aio_poll()
calls, 2014-07-15).
Introducing a third thread---a thread pool worker thread, which
also does qemu_bh_schedule()---does bring out the problematic case.
The third thread must be awakened *after* the callback is complete and
thread_pool_completion_bh has redone the whole loop, explaining the
short race window. And then this is what happens:
thread pool worker
--------------------------------------------------------------------------
<I/O completes>
qemu_bh_schedule()
Tada, bh->scheduled is already 1, so qemu_bh_schedule() does nothing
and the iothread is never woken up. This is where the bh->scheduled
optimization comes into play---it is correct, but removing it would
have masked the bug.
So, what is the bug?
Well, the question asked by the ctx->dispatching optimization ("is any
active aio_poll dispatching?") was wrong. The right question to ask
instead is "is any active aio_poll *not* dispatching", i.e. in the prepare
or poll phases? In that case, the aio_poll is sleeping or might go to
sleep anytime soon, and the EventNotifier must be invoked to wake
it up.
In any other case (including if there is *no* active aio_poll at all!)
we can just wait for the next prepare phase to pick up the event (e.g. a
bottom half); the prepare phase will avoid the blocking and service the
bottom half.
Expressing the invariant with a logic formula, the broken one looked like:
!(exists(thread): in_dispatching(thread)) => !optimize
or equivalently:
!(exists(thread):
in_aio_poll(thread) && in_dispatching(thread)) => !optimize
In the correct one, the negation is in a slightly different place:
(exists(thread):
in_aio_poll(thread) && !in_dispatching(thread)) => !optimize
or equivalently:
(exists(thread): in_prepare_or_poll(thread)) => !optimize
Even if the difference boils down to moving an exclamation mark :)
the implementation is quite different. However, I think the new
one is simpler to understand.
In the old implementation, the "exists" was implemented with a boolean
value. This didn't really support well the case of multiple concurrent
event loops, but I thought that this was okay: aio_poll holds the
AioContext lock so there cannot be concurrent aio_poll invocations, and
I was just considering nested event loops. However, aio_poll _could_
indeed be concurrent with the GSource. This is why I came up with the
wrong invariant.
In the new implementation, "exists" is computed simply by counting how many
threads are in the prepare or poll phases. There are some interesting
points to consider, but the gist of the idea remains:
1) AioContext can be used through GSource as well; as mentioned in the
patch, bit 0 of the counter is reserved for the GSource.
2) the counter need not be updated for a non-blocking aio_poll, because
it won't sleep forever anyway. This is just a matter of checking
the "blocking" variable. This requires some changes to the win32
implementation, but is otherwise not too complicated.
3) as mentioned above, the new implementation will not call aio_notify
when there is *no* active aio_poll at all. The tests have to be
adjusted for this change. The calls to aio_notify in async.c are fine;
they only want to kick aio_poll out of a blocking wait, but need not
do anything if aio_poll is not running.
4) nested aio_poll: these just work with the new implementation; when
a nested event loop is invoked, the outer event loop is never in the
prepare or poll phases. The outer event loop thus has already decremented
the counter.
Reported-by: Richard W. M. Jones <rjones@redhat.com>
Reported-by: Laszlo Ersek <lersek@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Fam Zheng <famz@redhat.com>
Tested-by: Richard W.M. Jones <rjones@redhat.com>
Message-id: 1437487673-23740-5-git-send-email-pbonzini@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2015-07-21 16:07:51 +02:00
|
|
|
}
|
2012-06-09 04:01:51 +02:00
|
|
|
|
2015-07-21 16:07:52 +02:00
|
|
|
if (first) {
|
|
|
|
progress |= aio_bh_poll(ctx);
|
|
|
|
first = false;
|
2014-07-09 11:53:04 +02:00
|
|
|
}
|
|
|
|
|
2012-06-09 04:01:51 +02:00
|
|
|
/* if we have any signaled events, dispatch event */
|
2014-07-09 11:53:10 +02:00
|
|
|
event = NULL;
|
|
|
|
if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
|
|
|
|
event = events[ret - WAIT_OBJECT_0];
|
2014-09-15 14:52:58 +02:00
|
|
|
events[ret - WAIT_OBJECT_0] = events[--count];
|
2014-07-09 11:53:10 +02:00
|
|
|
} else if (!have_select_revents) {
|
2012-06-09 04:01:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-07-09 11:53:10 +02:00
|
|
|
have_select_revents = false;
|
2012-06-09 04:01:51 +02:00
|
|
|
blocking = false;
|
2012-04-12 14:00:56 +02:00
|
|
|
|
2014-07-09 11:53:10 +02:00
|
|
|
progress |= aio_dispatch_handlers(ctx, event);
|
2015-07-21 16:07:50 +02:00
|
|
|
} while (count > 0);
|
2012-04-12 14:00:55 +02:00
|
|
|
|
2017-02-13 14:52:34 +01:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
|
|
|
|
2014-07-09 11:53:05 +02:00
|
|
|
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
2013-04-11 16:56:50 +02:00
|
|
|
return progress;
|
2008-09-22 21:17:18 +02:00
|
|
|
}
|
2015-10-30 05:06:28 +01:00
|
|
|
|
2016-07-15 12:28:44 +02:00
|
|
|
void aio_context_setup(AioContext *ctx)
|
2015-10-30 05:06:28 +01:00
|
|
|
{
|
|
|
|
}
|
2016-12-01 20:26:42 +01:00
|
|
|
|
2018-05-17 02:42:43 +02:00
|
|
|
void aio_context_destroy(AioContext *ctx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-05-11 20:36:30 +02:00
|
|
|
void aio_context_use_g_source(AioContext *ctx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-12-01 20:26:51 +01:00
|
|
|
void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
|
|
|
|
int64_t grow, int64_t shrink, Error **errp)
|
2016-12-01 20:26:42 +01:00
|
|
|
{
|
2018-03-22 09:56:30 +01:00
|
|
|
if (max_ns) {
|
|
|
|
error_setg(errp, "AioContext polling is not implemented on Windows");
|
|
|
|
}
|
2016-12-01 20:26:42 +01:00
|
|
|
}
|
2021-07-21 11:42:10 +02:00
|
|
|
|
|
|
|
void aio_context_set_aio_params(AioContext *ctx, int64_t max_batch,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
}
|