2008-09-22 23:17:18 +04:00
|
|
|
/*
|
|
|
|
* QEMU aio implementation
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2008
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* 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 20:44:23 +04: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 23:17:18 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-29 20:50:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block.h"
|
2020-02-18 21:27:08 +03:00
|
|
|
#include "qemu/rcu.h"
|
2017-01-12 21:07:57 +03:00
|
|
|
#include "qemu/rcu_queue.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/sockets.h"
|
2016-12-01 22:26:42 +03:00
|
|
|
#include "qemu/cutils.h"
|
2017-02-13 16:52:18 +03:00
|
|
|
#include "trace.h"
|
2020-03-05 20:08:02 +03:00
|
|
|
#include "aio-posix.h"
|
2008-09-22 23:17:18 +04:00
|
|
|
|
2020-03-05 20:08:06 +03:00
|
|
|
/* Stop userspace polling on a handler if it isn't active for some time */
|
|
|
|
#define POLL_IDLE_INTERVAL_NS (7 * NANOSECONDS_PER_SECOND)
|
|
|
|
|
2020-03-05 20:08:05 +03:00
|
|
|
bool aio_poll_disabled(AioContext *ctx)
|
|
|
|
{
|
2020-09-23 13:56:46 +03:00
|
|
|
return qatomic_read(&ctx->poll_disable_cnt);
|
2020-03-05 20:08:05 +03:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:08:02 +03:00
|
|
|
void aio_add_ready_handler(AioHandlerList *ready_list,
|
|
|
|
AioHandler *node,
|
|
|
|
int revents)
|
2020-02-14 20:17:12 +03:00
|
|
|
{
|
|
|
|
QLIST_SAFE_REMOVE(node, node_ready); /* remove from nested parent's list */
|
|
|
|
node->pfd.revents = revents;
|
|
|
|
QLIST_INSERT_HEAD(ready_list, node, node_ready);
|
|
|
|
}
|
|
|
|
|
2012-09-13 14:28:51 +04:00
|
|
|
static AioHandler *find_aio_handler(AioContext *ctx, int fd)
|
2008-09-22 23:17:18 +04:00
|
|
|
{
|
|
|
|
AioHandler *node;
|
|
|
|
|
2012-09-13 14:28:51 +04:00
|
|
|
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
2020-02-14 20:17:11 +03:00
|
|
|
if (node->pfd.fd == fd) {
|
|
|
|
if (!QLIST_IS_INSERTED(node, node_deleted)) {
|
2009-05-06 04:58:48 +04:00
|
|
|
return node;
|
2020-02-14 20:17:11 +03:00
|
|
|
}
|
|
|
|
}
|
2008-09-22 23:17:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:20:30 +03:00
|
|
|
static bool aio_remove_fd_handler(AioContext *ctx, AioHandler *node)
|
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:08:04 +03:00
|
|
|
node->pfd.revents = 0;
|
|
|
|
|
|
|
|
/* If the fd monitor has already marked it deleted, leave it alone */
|
|
|
|
if (QLIST_IS_INSERTED(node, node_deleted)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-20 18:20:30 +03:00
|
|
|
/* If a read is in progress, just mark the node as deleted */
|
|
|
|
if (qemu_lockcnt_count(&ctx->list_lock)) {
|
2020-02-14 20:17:11 +03:00
|
|
|
QLIST_INSERT_HEAD_RCU(&ctx->deleted_aio_handlers, node, node_deleted);
|
2018-12-20 18:20:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* Otherwise, delete it for real. We can't just mark it as
|
|
|
|
* deleted because deleted nodes are only cleaned up while
|
|
|
|
* no one is walking the handlers list.
|
|
|
|
*/
|
2020-03-05 20:08:06 +03:00
|
|
|
QLIST_SAFE_REMOVE(node, node_poll);
|
2018-12-20 18:20:30 +03:00
|
|
|
QLIST_REMOVE(node, node);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-13 14:28:51 +04:00
|
|
|
void aio_set_fd_handler(AioContext *ctx,
|
|
|
|
int fd,
|
2015-10-23 06:08:05 +03:00
|
|
|
bool is_external,
|
2012-09-13 14:28:51 +04:00
|
|
|
IOHandler *io_read,
|
|
|
|
IOHandler *io_write,
|
2016-12-01 22:26:41 +03:00
|
|
|
AioPollFn *io_poll,
|
2012-09-13 14:28:51 +04:00
|
|
|
void *opaque)
|
2008-09-22 23:17:18 +04:00
|
|
|
{
|
|
|
|
AioHandler *node;
|
2018-12-20 18:20:30 +03:00
|
|
|
AioHandler *new_node = NULL;
|
2015-10-30 07:06:29 +03:00
|
|
|
bool is_new = false;
|
2015-11-16 09:32:14 +03:00
|
|
|
bool deleted = false;
|
2018-09-12 20:10:38 +03:00
|
|
|
int poll_disable_change;
|
2008-09-22 23:17:18 +04:00
|
|
|
|
2017-01-12 21:07:57 +03:00
|
|
|
qemu_lockcnt_lock(&ctx->list_lock);
|
|
|
|
|
2012-09-13 14:28:51 +04:00
|
|
|
node = find_aio_handler(ctx, fd);
|
2008-09-22 23:17:18 +04:00
|
|
|
|
|
|
|
/* Are we deleting the fd handler? */
|
2016-12-01 22:26:42 +03:00
|
|
|
if (!io_read && !io_write && !io_poll) {
|
2016-11-08 16:55:23 +03:00
|
|
|
if (node == NULL) {
|
2017-01-12 21:07:57 +03:00
|
|
|
qemu_lockcnt_unlock(&ctx->list_lock);
|
2016-11-08 16:55:23 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-12-20 18:20:29 +03:00
|
|
|
/* Clean events in order to unregister fd from the ctx epoll. */
|
|
|
|
node->pfd.events = 0;
|
|
|
|
|
2018-09-12 20:10:38 +03:00
|
|
|
poll_disable_change = -!node->io_poll;
|
2008-09-22 23:17:18 +04:00
|
|
|
} else {
|
2018-09-12 20:10:38 +03:00
|
|
|
poll_disable_change = !io_poll - (node && !node->io_poll);
|
2008-09-22 23:17:18 +04:00
|
|
|
if (node == NULL) {
|
2015-10-30 07:06:29 +03:00
|
|
|
is_new = true;
|
2008-09-22 23:17:18 +04:00
|
|
|
}
|
2018-12-20 18:20:30 +03:00
|
|
|
/* Alloc and insert if it's not already there */
|
|
|
|
new_node = g_new0(AioHandler, 1);
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2008-09-22 23:17:18 +04:00
|
|
|
/* Update handler with latest information */
|
2018-12-20 18:20:30 +03:00
|
|
|
new_node->io_read = io_read;
|
|
|
|
new_node->io_write = io_write;
|
|
|
|
new_node->io_poll = io_poll;
|
|
|
|
new_node->opaque = opaque;
|
|
|
|
new_node->is_external = is_external;
|
|
|
|
|
|
|
|
if (is_new) {
|
|
|
|
new_node->pfd.fd = fd;
|
|
|
|
} else {
|
|
|
|
new_node->pfd = node->pfd;
|
|
|
|
}
|
|
|
|
g_source_add_poll(&ctx->source, &new_node->pfd);
|
|
|
|
|
|
|
|
new_node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
|
|
|
|
new_node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
|
|
|
|
|
|
|
|
QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, new_node, node);
|
|
|
|
}
|
2012-09-25 12:22:39 +04:00
|
|
|
|
2018-09-12 20:10:38 +03:00
|
|
|
/* No need to order poll_disable_cnt writes against other updates;
|
|
|
|
* the counter is only used to avoid wasting time and latency on
|
|
|
|
* iterated polling when the system call will be ultimately necessary.
|
|
|
|
* Changing handlers is a rare event, and a little wasted polling until
|
|
|
|
* the aio_notify below is not an issue.
|
|
|
|
*/
|
2020-09-23 13:56:46 +03:00
|
|
|
qatomic_set(&ctx->poll_disable_cnt,
|
|
|
|
qatomic_read(&ctx->poll_disable_cnt) + poll_disable_change);
|
2018-09-12 20:10:38 +03:00
|
|
|
|
2020-03-05 20:08:03 +03:00
|
|
|
ctx->fdmon_ops->update(ctx, node, new_node);
|
2020-03-05 20:08:04 +03:00
|
|
|
if (node) {
|
|
|
|
deleted = aio_remove_fd_handler(ctx, node);
|
|
|
|
}
|
2017-01-12 21:07:57 +03:00
|
|
|
qemu_lockcnt_unlock(&ctx->list_lock);
|
2012-09-25 12:22:39 +04:00
|
|
|
aio_notify(ctx);
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2015-11-16 09:32:14 +03:00
|
|
|
if (deleted) {
|
|
|
|
g_free(node);
|
|
|
|
}
|
2012-06-09 05:44:00 +04:00
|
|
|
}
|
|
|
|
|
2016-12-01 22:26:49 +03:00
|
|
|
void aio_set_fd_poll(AioContext *ctx, int fd,
|
|
|
|
IOHandler *io_poll_begin,
|
|
|
|
IOHandler *io_poll_end)
|
|
|
|
{
|
|
|
|
AioHandler *node = find_aio_handler(ctx, fd);
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->io_poll_begin = io_poll_begin;
|
|
|
|
node->io_poll_end = io_poll_end;
|
|
|
|
}
|
|
|
|
|
2012-09-13 14:28:51 +04:00
|
|
|
void aio_set_event_notifier(AioContext *ctx,
|
|
|
|
EventNotifier *notifier,
|
2015-10-23 06:08:05 +03:00
|
|
|
bool is_external,
|
2016-12-01 22:26:41 +03:00
|
|
|
EventNotifierHandler *io_read,
|
|
|
|
AioPollFn *io_poll)
|
2008-09-22 23:17:18 +04:00
|
|
|
{
|
2016-12-01 22:26:41 +03:00
|
|
|
aio_set_fd_handler(ctx, event_notifier_get_fd(notifier), is_external,
|
|
|
|
(IOHandler *)io_read, NULL, io_poll, notifier);
|
2008-09-22 23:17:18 +04:00
|
|
|
}
|
|
|
|
|
2016-12-01 22:26:49 +03:00
|
|
|
void aio_set_event_notifier_poll(AioContext *ctx,
|
|
|
|
EventNotifier *notifier,
|
|
|
|
EventNotifierHandler *io_poll_begin,
|
|
|
|
EventNotifierHandler *io_poll_end)
|
|
|
|
{
|
|
|
|
aio_set_fd_poll(ctx, event_notifier_get_fd(notifier),
|
|
|
|
(IOHandler *)io_poll_begin,
|
|
|
|
(IOHandler *)io_poll_end);
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:08:00 +03:00
|
|
|
static bool poll_set_started(AioContext *ctx, bool started)
|
2016-12-01 22:26:49 +03:00
|
|
|
{
|
|
|
|
AioHandler *node;
|
2020-03-05 20:08:00 +03:00
|
|
|
bool progress = false;
|
2016-12-01 22:26:49 +03:00
|
|
|
|
|
|
|
if (started == ctx->poll_started) {
|
2020-03-05 20:08:00 +03:00
|
|
|
return false;
|
2016-12-01 22:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx->poll_started = started;
|
|
|
|
|
2017-01-12 21:07:57 +03:00
|
|
|
qemu_lockcnt_inc(&ctx->list_lock);
|
2020-03-05 20:08:06 +03:00
|
|
|
QLIST_FOREACH(node, &ctx->poll_aio_handlers, node_poll) {
|
2016-12-01 22:26:49 +03:00
|
|
|
IOHandler *fn;
|
|
|
|
|
2020-02-14 20:17:11 +03:00
|
|
|
if (QLIST_IS_INSERTED(node, node_deleted)) {
|
2016-12-01 22:26:49 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (started) {
|
|
|
|
fn = node->io_poll_begin;
|
|
|
|
} else {
|
|
|
|
fn = node->io_poll_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fn) {
|
|
|
|
fn(node->opaque);
|
|
|
|
}
|
2020-03-05 20:08:00 +03:00
|
|
|
|
|
|
|
/* Poll one last time in case ->io_poll_end() raced with the event */
|
|
|
|
if (!started) {
|
|
|
|
progress = node->io_poll(node->opaque) || progress;
|
|
|
|
}
|
2016-12-01 22:26:49 +03:00
|
|
|
}
|
2017-01-12 21:07:57 +03:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
2020-03-05 20:08:00 +03:00
|
|
|
|
|
|
|
return progress;
|
2016-12-01 22:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-09 13:53:08 +04:00
|
|
|
bool aio_prepare(AioContext *ctx)
|
|
|
|
{
|
2016-12-01 22:26:49 +03:00
|
|
|
/* Poll mode cannot be used with glib's event loop, disable it. */
|
|
|
|
poll_set_started(ctx, false);
|
|
|
|
|
2014-07-09 13:53:08 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-24 16:57:22 +04:00
|
|
|
bool aio_pending(AioContext *ctx)
|
|
|
|
{
|
|
|
|
AioHandler *node;
|
2017-01-12 21:07:57 +03:00
|
|
|
bool result = false;
|
2012-09-24 16:57:22 +04:00
|
|
|
|
2017-01-12 21:07:57 +03: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-09-24 16:57:22 +04:00
|
|
|
int revents;
|
|
|
|
|
|
|
|
revents = node->pfd.revents & node->pfd.events;
|
2016-04-22 16:53:55 +03:00
|
|
|
if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read &&
|
|
|
|
aio_node_check(ctx, node->is_external)) {
|
2017-01-12 21:07:57 +03:00
|
|
|
result = true;
|
|
|
|
break;
|
2012-09-24 16:57:22 +04:00
|
|
|
}
|
2016-04-22 16:53:55 +03:00
|
|
|
if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write &&
|
|
|
|
aio_node_check(ctx, node->is_external)) {
|
2017-01-12 21:07:57 +03:00
|
|
|
result = true;
|
|
|
|
break;
|
2012-09-24 16:57:22 +04:00
|
|
|
}
|
|
|
|
}
|
2017-01-12 21:07:57 +03:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
2012-09-24 16:57:22 +04:00
|
|
|
|
2017-01-12 21:07:57 +03:00
|
|
|
return result;
|
2012-09-24 16:57:22 +04:00
|
|
|
}
|
|
|
|
|
2020-02-14 20:17:11 +03:00
|
|
|
static void aio_free_deleted_handlers(AioContext *ctx)
|
|
|
|
{
|
|
|
|
AioHandler *node;
|
|
|
|
|
|
|
|
if (QLIST_EMPTY_RCU(&ctx->deleted_aio_handlers)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!qemu_lockcnt_dec_if_lock(&ctx->list_lock)) {
|
|
|
|
return; /* we are nested, let the parent do the freeing */
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((node = QLIST_FIRST_RCU(&ctx->deleted_aio_handlers))) {
|
|
|
|
QLIST_REMOVE(node, node);
|
|
|
|
QLIST_REMOVE(node, node_deleted);
|
2020-03-05 20:08:06 +03:00
|
|
|
QLIST_SAFE_REMOVE(node, node_poll);
|
2020-02-14 20:17:11 +03:00
|
|
|
g_free(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
qemu_lockcnt_inc_and_unlock(&ctx->list_lock);
|
|
|
|
}
|
|
|
|
|
2020-02-14 20:17:12 +03:00
|
|
|
static bool aio_dispatch_handler(AioContext *ctx, AioHandler *node)
|
2008-09-22 23:17:18 +04:00
|
|
|
{
|
2013-02-20 14:28:31 +04:00
|
|
|
bool progress = false;
|
2020-02-14 20:17:12 +03:00
|
|
|
int revents;
|
2012-09-24 16:37:53 +04:00
|
|
|
|
2020-02-14 20:17:12 +03:00
|
|
|
revents = node->pfd.revents & node->pfd.events;
|
|
|
|
node->pfd.revents = 0;
|
2012-09-24 16:57:22 +04:00
|
|
|
|
2020-03-05 20:08:06 +03:00
|
|
|
/*
|
|
|
|
* Start polling AioHandlers when they become ready because activity is
|
|
|
|
* likely to continue. Note that starvation is theoretically possible when
|
|
|
|
* fdmon_supports_polling(), but only until the fd fires for the first
|
|
|
|
* time.
|
|
|
|
*/
|
|
|
|
if (!QLIST_IS_INSERTED(node, node_deleted) &&
|
|
|
|
!QLIST_IS_INSERTED(node, node_poll) &&
|
|
|
|
node->io_poll) {
|
|
|
|
trace_poll_add(ctx, node, node->pfd.fd, revents);
|
|
|
|
if (ctx->poll_started && node->io_poll_begin) {
|
|
|
|
node->io_poll_begin(node->opaque);
|
|
|
|
}
|
|
|
|
QLIST_INSERT_HEAD(&ctx->poll_aio_handlers, node, node_poll);
|
|
|
|
}
|
|
|
|
|
2020-02-14 20:17:12 +03:00
|
|
|
if (!QLIST_IS_INSERTED(node, node_deleted) &&
|
|
|
|
(revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
|
|
|
|
aio_node_check(ctx, node->is_external) &&
|
|
|
|
node->io_read) {
|
|
|
|
node->io_read(node->opaque);
|
2012-09-24 16:57:22 +04:00
|
|
|
|
2020-02-14 20:17:12 +03:00
|
|
|
/* aio_notify() does not count as progress */
|
|
|
|
if (node->opaque != &ctx->notifier) {
|
2012-09-24 16:57:22 +04:00
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 20:17:12 +03:00
|
|
|
if (!QLIST_IS_INSERTED(node, node_deleted) &&
|
|
|
|
(revents & (G_IO_OUT | G_IO_ERR)) &&
|
|
|
|
aio_node_check(ctx, node->is_external) &&
|
|
|
|
node->io_write) {
|
|
|
|
node->io_write(node->opaque);
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have a list of ready handlers then this is more efficient than
|
|
|
|
* scanning all handlers with aio_dispatch_handlers().
|
|
|
|
*/
|
|
|
|
static bool aio_dispatch_ready_handlers(AioContext *ctx,
|
|
|
|
AioHandlerList *ready_list)
|
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
AioHandler *node;
|
|
|
|
|
|
|
|
while ((node = QLIST_FIRST(ready_list))) {
|
2020-02-24 13:34:06 +03:00
|
|
|
QLIST_REMOVE(node, node_ready);
|
2020-02-14 20:17:12 +03:00
|
|
|
progress = aio_dispatch_handler(ctx, node) || progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Slower than aio_dispatch_ready_handlers() but only used via glib */
|
|
|
|
static bool aio_dispatch_handlers(AioContext *ctx)
|
|
|
|
{
|
|
|
|
AioHandler *node, *tmp;
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
QLIST_FOREACH_SAFE_RCU(node, &ctx->aio_handlers, node, tmp) {
|
|
|
|
progress = aio_dispatch_handler(ctx, node) || progress;
|
|
|
|
}
|
2013-08-21 19:02:53 +04:00
|
|
|
|
2017-01-12 21:07:55 +03:00
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:52:33 +03:00
|
|
|
void aio_dispatch(AioContext *ctx)
|
2017-01-12 21:07:55 +03:00
|
|
|
{
|
2017-02-13 16:52:33 +03:00
|
|
|
qemu_lockcnt_inc(&ctx->list_lock);
|
2017-02-13 16:52:34 +03:00
|
|
|
aio_bh_poll(ctx);
|
2017-02-13 16:52:33 +03:00
|
|
|
aio_dispatch_handlers(ctx);
|
2020-02-14 20:17:11 +03:00
|
|
|
aio_free_deleted_handlers(ctx);
|
2017-02-13 16:52:33 +03:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
2013-08-21 19:02:53 +04:00
|
|
|
|
2017-02-13 16:52:33 +03:00
|
|
|
timerlistgroup_run_timers(&ctx->tlg);
|
2013-02-20 14:28:31 +04:00
|
|
|
}
|
|
|
|
|
2020-03-05 20:08:06 +03:00
|
|
|
static bool run_poll_handlers_once(AioContext *ctx,
|
|
|
|
int64_t now,
|
|
|
|
int64_t *timeout)
|
2016-12-01 22:26:49 +03:00
|
|
|
{
|
|
|
|
bool progress = false;
|
|
|
|
AioHandler *node;
|
2020-03-05 20:08:06 +03:00
|
|
|
AioHandler *tmp;
|
2016-12-01 22:26:49 +03:00
|
|
|
|
2020-03-05 20:08:06 +03:00
|
|
|
QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) {
|
|
|
|
if (aio_node_check(ctx, node->is_external) &&
|
2018-09-12 20:10:40 +03:00
|
|
|
node->io_poll(node->opaque)) {
|
2020-03-05 20:08:06 +03:00
|
|
|
node->poll_idle_timeout = now + POLL_IDLE_INTERVAL_NS;
|
|
|
|
|
2019-04-09 15:28:23 +03:00
|
|
|
/*
|
|
|
|
* Polling was successful, exit try_poll_mode immediately
|
|
|
|
* to adjust the next polling time.
|
|
|
|
*/
|
2018-09-12 20:10:39 +03:00
|
|
|
*timeout = 0;
|
2018-09-12 20:10:40 +03:00
|
|
|
if (node->opaque != &ctx->notifier) {
|
|
|
|
progress = true;
|
|
|
|
}
|
2016-12-01 22:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Caller handles freeing deleted nodes. Don't do it here. */
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:08:06 +03:00
|
|
|
static bool fdmon_supports_polling(AioContext *ctx)
|
|
|
|
{
|
|
|
|
return ctx->fdmon_ops->need_wait != aio_poll_disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool remove_idle_poll_handlers(AioContext *ctx, int64_t now)
|
|
|
|
{
|
|
|
|
AioHandler *node;
|
|
|
|
AioHandler *tmp;
|
|
|
|
bool progress = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File descriptor monitoring implementations without userspace polling
|
|
|
|
* support suffer from starvation when a subset of handlers is polled
|
|
|
|
* because fds will not be processed in a timely fashion. Don't remove
|
|
|
|
* idle poll handlers.
|
|
|
|
*/
|
|
|
|
if (!fdmon_supports_polling(ctx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLIST_FOREACH_SAFE(node, &ctx->poll_aio_handlers, node_poll, tmp) {
|
|
|
|
if (node->poll_idle_timeout == 0LL) {
|
|
|
|
node->poll_idle_timeout = now + POLL_IDLE_INTERVAL_NS;
|
|
|
|
} else if (now >= node->poll_idle_timeout) {
|
|
|
|
trace_poll_remove(ctx, node, node->pfd.fd);
|
|
|
|
node->poll_idle_timeout = 0LL;
|
|
|
|
QLIST_SAFE_REMOVE(node, node_poll);
|
|
|
|
if (ctx->poll_started && node->io_poll_end) {
|
|
|
|
node->io_poll_end(node->opaque);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Final poll in case ->io_poll_end() races with an event.
|
|
|
|
* Nevermind about re-adding the handler in the rare case where
|
|
|
|
* this causes progress.
|
|
|
|
*/
|
|
|
|
progress = node->io_poll(node->opaque) || progress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
2016-12-01 22:26:42 +03:00
|
|
|
/* run_poll_handlers:
|
|
|
|
* @ctx: the AioContext
|
|
|
|
* @max_ns: maximum time to poll for, in nanoseconds
|
|
|
|
*
|
|
|
|
* Polls for a given time.
|
|
|
|
*
|
2017-01-12 21:07:57 +03:00
|
|
|
* Note that the caller must have incremented ctx->list_lock.
|
2016-12-01 22:26:42 +03:00
|
|
|
*
|
|
|
|
* Returns: true if progress was made, false otherwise
|
|
|
|
*/
|
2018-09-12 20:10:39 +03:00
|
|
|
static bool run_poll_handlers(AioContext *ctx, int64_t max_ns, int64_t *timeout)
|
2016-12-01 22:26:42 +03:00
|
|
|
{
|
2016-12-01 22:26:49 +03:00
|
|
|
bool progress;
|
2018-09-12 20:10:39 +03:00
|
|
|
int64_t start_time, elapsed_time;
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2017-01-12 21:07:57 +03:00
|
|
|
assert(qemu_lockcnt_count(&ctx->list_lock) > 0);
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2018-09-12 20:10:39 +03:00
|
|
|
trace_run_poll_handlers_begin(ctx, max_ns, *timeout);
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2020-03-05 20:08:01 +03:00
|
|
|
/*
|
|
|
|
* Optimization: ->io_poll() handlers often contain RCU read critical
|
|
|
|
* sections and we therefore see many rcu_read_lock() -> rcu_read_unlock()
|
|
|
|
* -> rcu_read_lock() -> ... sequences with expensive memory
|
|
|
|
* synchronization primitives. Make the entire polling loop an RCU
|
|
|
|
* critical section because nested rcu_read_lock()/rcu_read_unlock() calls
|
|
|
|
* are cheap.
|
|
|
|
*/
|
|
|
|
RCU_READ_LOCK_GUARD();
|
|
|
|
|
2018-09-12 20:10:39 +03:00
|
|
|
start_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
2016-12-01 22:26:42 +03:00
|
|
|
do {
|
2020-03-05 20:08:06 +03:00
|
|
|
progress = run_poll_handlers_once(ctx, start_time, timeout);
|
2018-09-12 20:10:39 +03:00
|
|
|
elapsed_time = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time;
|
2019-04-09 15:28:23 +03:00
|
|
|
max_ns = qemu_soonest_timeout(*timeout, max_ns);
|
|
|
|
assert(!(max_ns && progress));
|
2020-03-05 20:08:05 +03:00
|
|
|
} while (elapsed_time < max_ns && !ctx->fdmon_ops->need_wait(ctx));
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2020-03-05 20:08:06 +03:00
|
|
|
if (remove_idle_poll_handlers(ctx, start_time + elapsed_time)) {
|
|
|
|
*timeout = 0;
|
|
|
|
progress = true;
|
|
|
|
}
|
|
|
|
|
2018-09-12 20:10:39 +03:00
|
|
|
/* If time has passed with no successful polling, adjust *timeout to
|
|
|
|
* keep the same ending time.
|
|
|
|
*/
|
|
|
|
if (*timeout != -1) {
|
|
|
|
*timeout -= MIN(*timeout, elapsed_time);
|
|
|
|
}
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2018-09-12 20:10:39 +03:00
|
|
|
trace_run_poll_handlers_end(ctx, progress, *timeout);
|
2016-12-01 22:26:42 +03:00
|
|
|
return progress;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try_poll_mode:
|
|
|
|
* @ctx: the AioContext
|
2018-09-12 20:10:39 +03:00
|
|
|
* @timeout: timeout for blocking wait, computed by the caller and updated if
|
|
|
|
* polling succeeds.
|
2016-12-01 22:26:42 +03:00
|
|
|
*
|
2017-01-12 21:07:57 +03:00
|
|
|
* Note that the caller must have incremented ctx->list_lock.
|
2016-12-01 22:26:42 +03:00
|
|
|
*
|
|
|
|
* Returns: true if progress was made, false otherwise
|
|
|
|
*/
|
2018-09-12 20:10:39 +03:00
|
|
|
static bool try_poll_mode(AioContext *ctx, int64_t *timeout)
|
2016-12-01 22:26:42 +03:00
|
|
|
{
|
2020-03-05 20:08:06 +03:00
|
|
|
int64_t max_ns;
|
|
|
|
|
|
|
|
if (QLIST_EMPTY_RCU(&ctx->poll_aio_handlers)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2020-03-05 20:08:06 +03:00
|
|
|
max_ns = qemu_soonest_timeout(*timeout, ctx->poll_ns);
|
2020-03-05 20:08:05 +03:00
|
|
|
if (max_ns && !ctx->fdmon_ops->need_wait(ctx)) {
|
2018-09-12 20:10:39 +03:00
|
|
|
poll_set_started(ctx, true);
|
2016-12-01 22:26:49 +03:00
|
|
|
|
2018-09-12 20:10:39 +03:00
|
|
|
if (run_poll_handlers(ctx, max_ns, timeout)) {
|
|
|
|
return true;
|
2016-12-01 22:26:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:08:00 +03:00
|
|
|
if (poll_set_started(ctx, false)) {
|
|
|
|
*timeout = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-01 22:26:49 +03:00
|
|
|
|
2020-03-05 20:08:00 +03:00
|
|
|
return false;
|
2016-12-01 22:26:42 +03:00
|
|
|
}
|
|
|
|
|
2013-02-20 14:28:31 +04:00
|
|
|
bool aio_poll(AioContext *ctx, bool blocking)
|
|
|
|
{
|
2020-02-14 20:17:12 +03:00
|
|
|
AioHandlerList ready_list = QLIST_HEAD_INITIALIZER(ready_list);
|
2016-12-01 22:26:42 +03:00
|
|
|
int ret = 0;
|
2013-04-11 18:56:50 +04:00
|
|
|
bool progress;
|
2020-08-06 16:18:02 +03:00
|
|
|
bool use_notify_me;
|
2015-02-20 19:26:50 +03:00
|
|
|
int64_t timeout;
|
2016-12-01 22:26:51 +03:00
|
|
|
int64_t start = 0;
|
2013-02-20 14:28:31 +04:00
|
|
|
|
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 17:07:46 +03: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.
|
|
|
|
*/
|
2019-02-14 15:13:36 +03:00
|
|
|
assert(in_aio_context_home_thread(ctx));
|
|
|
|
|
2020-08-06 16:18:02 +03:00
|
|
|
qemu_lockcnt_inc(&ctx->list_lock);
|
|
|
|
|
|
|
|
if (ctx->poll_max_ns) {
|
|
|
|
start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = blocking ? aio_compute_timeout(ctx) : 0;
|
|
|
|
progress = try_poll_mode(ctx, &timeout);
|
|
|
|
assert(!(timeout && progress));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* aio_notify can avoid the expensive event_notifier_set if
|
2014-07-07 17:18:04 +04:00
|
|
|
* everything (file descriptors, bottom halves, timers) will
|
2014-07-09 13:53:05 +04:00
|
|
|
* 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 17:07:51 +03:00
|
|
|
* if blocking == true, it is only true after poll() returns,
|
|
|
|
* so disable the optimization now.
|
2014-07-07 17:18:04 +04:00
|
|
|
*/
|
2020-08-06 16:18:02 +03:00
|
|
|
use_notify_me = timeout != 0;
|
|
|
|
if (use_notify_me) {
|
2020-09-23 13:56:46 +03: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 17:07:46 +03:00
|
|
|
/*
|
2020-08-06 16:18:02 +03:00
|
|
|
* Write ctx->notify_me before reading ctx->notified. Pairs with
|
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 17:07:46 +03:00
|
|
|
* smp_mb in aio_notify().
|
|
|
|
*/
|
|
|
|
smp_mb();
|
2008-09-22 23:17:18 +04:00
|
|
|
|
2020-08-06 16:18:02 +03:00
|
|
|
/* Don't block if aio_notify() was called */
|
2020-09-23 13:56:46 +03:00
|
|
|
if (qatomic_read(&ctx->notified)) {
|
2020-08-06 16:18:02 +03:00
|
|
|
timeout = 0;
|
|
|
|
}
|
2016-12-01 22:26:51 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 20:10:39 +03:00
|
|
|
/* If polling is allowed, non-blocking aio_poll does not need the
|
|
|
|
* system call---a single round of run_poll_handlers_once suffices.
|
|
|
|
*/
|
2020-03-05 20:08:05 +03:00
|
|
|
if (timeout || ctx->fdmon_ops->need_wait(ctx)) {
|
2020-03-05 20:08:02 +03:00
|
|
|
ret = ctx->fdmon_ops->wait(ctx, &ready_list, timeout);
|
2015-10-30 07:06:29 +03:00
|
|
|
}
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2020-08-06 16:18:02 +03:00
|
|
|
if (use_notify_me) {
|
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 17:07:46 +03:00
|
|
|
/* Finish the poll before clearing the flag. */
|
2020-09-23 13:56:46 +03:00
|
|
|
qatomic_store_release(&ctx->notify_me,
|
|
|
|
qatomic_read(&ctx->notify_me) - 2);
|
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 17:07:51 +03:00
|
|
|
}
|
2012-04-12 16:00:56 +04:00
|
|
|
|
2020-08-06 16:18:02 +03:00
|
|
|
aio_notify_accept(ctx);
|
|
|
|
|
2016-12-01 22:26:51 +03:00
|
|
|
/* Adjust polling time */
|
|
|
|
if (ctx->poll_max_ns) {
|
|
|
|
int64_t block_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start;
|
|
|
|
|
|
|
|
if (block_ns <= ctx->poll_ns) {
|
|
|
|
/* This is the sweet spot, no adjustment needed */
|
|
|
|
} else if (block_ns > ctx->poll_max_ns) {
|
|
|
|
/* We'd have to poll for too long, poll less */
|
|
|
|
int64_t old = ctx->poll_ns;
|
|
|
|
|
|
|
|
if (ctx->poll_shrink) {
|
|
|
|
ctx->poll_ns /= ctx->poll_shrink;
|
|
|
|
} else {
|
|
|
|
ctx->poll_ns = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_poll_shrink(ctx, old, ctx->poll_ns);
|
|
|
|
} else if (ctx->poll_ns < ctx->poll_max_ns &&
|
|
|
|
block_ns < ctx->poll_max_ns) {
|
|
|
|
/* There is room to grow, poll longer */
|
|
|
|
int64_t old = ctx->poll_ns;
|
|
|
|
int64_t grow = ctx->poll_grow;
|
|
|
|
|
|
|
|
if (grow == 0) {
|
|
|
|
grow = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->poll_ns) {
|
|
|
|
ctx->poll_ns *= grow;
|
|
|
|
} else {
|
|
|
|
ctx->poll_ns = 4000; /* start polling at 4 microseconds */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx->poll_ns > ctx->poll_max_ns) {
|
|
|
|
ctx->poll_ns = ctx->poll_max_ns;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_poll_grow(ctx, old, ctx->poll_ns);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 16:52:33 +03:00
|
|
|
progress |= aio_bh_poll(ctx);
|
|
|
|
|
|
|
|
if (ret > 0) {
|
2020-02-14 20:17:12 +03:00
|
|
|
progress |= aio_dispatch_ready_handlers(ctx, &ready_list);
|
2012-04-12 16:00:56 +04:00
|
|
|
}
|
2012-04-12 16:00:55 +04:00
|
|
|
|
2020-02-14 20:17:11 +03:00
|
|
|
aio_free_deleted_handlers(ctx);
|
|
|
|
|
2017-02-13 16:52:34 +03:00
|
|
|
qemu_lockcnt_dec(&ctx->list_lock);
|
|
|
|
|
2017-02-13 16:52:33 +03:00
|
|
|
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
|
|
|
|
2013-04-11 18:56:50 +04:00
|
|
|
return progress;
|
2008-09-22 23:17:18 +04:00
|
|
|
}
|
2015-10-30 07:06:28 +03:00
|
|
|
|
2016-07-15 13:28:44 +03:00
|
|
|
void aio_context_setup(AioContext *ctx)
|
2015-10-30 07:06:28 +03:00
|
|
|
{
|
2020-03-05 20:08:02 +03:00
|
|
|
ctx->fdmon_ops = &fdmon_poll_ops;
|
|
|
|
ctx->epollfd = -1;
|
|
|
|
|
2020-03-05 20:08:04 +03:00
|
|
|
/* Use the fastest fd monitoring implementation if available */
|
|
|
|
if (fdmon_io_uring_setup(ctx)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-05 20:08:02 +03:00
|
|
|
fdmon_epoll_setup(ctx);
|
2015-10-30 07:06:28 +03:00
|
|
|
}
|
2016-12-01 22:26:42 +03:00
|
|
|
|
2018-05-17 03:42:43 +03:00
|
|
|
void aio_context_destroy(AioContext *ctx)
|
|
|
|
{
|
2020-03-05 20:08:04 +03:00
|
|
|
fdmon_io_uring_destroy(ctx);
|
2020-03-05 20:08:02 +03:00
|
|
|
fdmon_epoll_disable(ctx);
|
2020-05-11 21:36:29 +03:00
|
|
|
aio_free_deleted_handlers(ctx);
|
2018-05-17 03:42:43 +03:00
|
|
|
}
|
|
|
|
|
2020-05-11 21:36:30 +03:00
|
|
|
void aio_context_use_g_source(AioContext *ctx)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Disable io_uring when the glib main loop is used because it doesn't
|
|
|
|
* support mixed glib/aio_poll() usage. It relies on aio_poll() being
|
|
|
|
* called regularly so that changes to the monitored file descriptors are
|
|
|
|
* submitted, otherwise a list of pending fd handlers builds up.
|
|
|
|
*/
|
|
|
|
fdmon_io_uring_destroy(ctx);
|
|
|
|
aio_free_deleted_handlers(ctx);
|
|
|
|
}
|
|
|
|
|
2016-12-01 22:26:51 +03:00
|
|
|
void aio_context_set_poll_params(AioContext *ctx, int64_t max_ns,
|
|
|
|
int64_t grow, int64_t shrink, Error **errp)
|
2016-12-01 22:26:42 +03:00
|
|
|
{
|
2016-12-01 22:26:51 +03:00
|
|
|
/* No thread synchronization here, it doesn't matter if an incorrect value
|
|
|
|
* is used once.
|
2016-12-01 22:26:42 +03:00
|
|
|
*/
|
|
|
|
ctx->poll_max_ns = max_ns;
|
2016-12-01 22:26:51 +03:00
|
|
|
ctx->poll_ns = 0;
|
|
|
|
ctx->poll_grow = grow;
|
|
|
|
ctx->poll_shrink = shrink;
|
2016-12-01 22:26:42 +03:00
|
|
|
|
|
|
|
aio_notify(ctx);
|
|
|
|
}
|