From nobody Thu Nov 6 16:23:15 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 154239503384785.93122175158703; Fri, 16 Nov 2018 11:03:53 -0800 (PST) Received: from localhost ([::1]:46435 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gNjPE-0007JL-QD for importer@patchew.org; Fri, 16 Nov 2018 14:03:52 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52568) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gNjNY-0006Wl-Ia for qemu-devel@nongnu.org; Fri, 16 Nov 2018 14:02:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gNjNX-0004dK-8x for qemu-devel@nongnu.org; Fri, 16 Nov 2018 14:02:08 -0500 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]:51958) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gNjNW-0004cT-VL for qemu-devel@nongnu.org; Fri, 16 Nov 2018 14:02:07 -0500 Received: by mail-wm1-x32a.google.com with SMTP id w7-v6so23041966wmc.1 for ; Fri, 16 Nov 2018 11:02:06 -0800 (PST) Received: from mocramis-ultrabook.localdomain ([178.208.16.32]) by smtp.gmail.com with ESMTPSA id t4-v6sm17435978wrb.67.2018.11.16.11.02.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Nov 2018 11:02:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blade-group.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BpFTCzIxTPC2dXy4EaVLo9+0VU+jWllvW1bH7XGpGP0=; b=hdsQI+8eHMVRFGZOH0WaBjC7vaPq3he3FqwM39JLPSybdx/ydsXFTgaA5SUBo4ukVH sxJPaqb1+4Q5CcViQmzaOOpOKK4J/RJwBp90cFMy+Q0M8wpLaVmzhCBnNhQR7UtW61IG wP8mvmuBaMztGohMhjNteuK6qgst0YBwaVKR9CmBMr2XYjvy8aUY9u+QYcqGI96ryova LUAz61i6Aol/EK1JcXdtYPBS3afC2wDES/DgyFCnFWZ2nqUMAdkPOQj03H/nOgzdun9N YuLQ1JxXBGek7yZg31HcFVMgyR3kX/+WoZrdu/yEqP56tnvkqD3DWoE2BC6SrD8oGAM+ X8yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BpFTCzIxTPC2dXy4EaVLo9+0VU+jWllvW1bH7XGpGP0=; b=MKUL5Ef6A1ou3Q87gqxTu/LnIvkvIvt4oujUlyj4xjOD2gJbT0tsxmL7HIqm5fYoRg UPUeOUu3Yt6jdJGtCcbHGqK2mz1bZDi6e70fjYxLL81I60uavKTkKjyRfNB5OX1RNNKO 76uO4KoygX8dBIJYUIjPVfgzgLbdJ24LoVo7CVDfq2vq4Z/Wd+2Xg8qzqv0YVdNIiFlQ Cv0+PymILd2HPBiXxKaOXLYP/deFL3bhAMnjrNR5Mhl04KUf7/cIWn1tSonlkwm8OGQs N6VnPiO2wk6HdidAvBWCe4VPP7d9WklGclBjT+TE5Ix13bTn8Z09ykLJl+n5Zs+Sia0/ bMdA== X-Gm-Message-State: AGRZ1gJy3ws3GTJJhw/mbFOHULAgs8GoVAcfhDyBbfLOLuJ4SU3IP4C4 8p2eBTOz1f0NOIDda4x/wUrGBq5AyZU= X-Google-Smtp-Source: AJdET5csnHA3eRfnaMvPSCISBUDAtRvjRv3nKWMbZ7+2hFw21H1XggJBJHB/wABG4ypd+6Ishv9NGw== X-Received: by 2002:a1c:b4c1:: with SMTP id d184-v6mr3719180wmf.143.1542394925514; Fri, 16 Nov 2018 11:02:05 -0800 (PST) From: remy.noel@blade-group.com To: qemu-devel@nongnu.org Date: Fri, 16 Nov 2018 20:02:08 +0100 Message-Id: <20181116190211.17622-2-remy.noel@blade-group.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181116190211.17622-1-remy.noel@blade-group.com> References: <20181116190211.17622-1-remy.noel@blade-group.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::32a Subject: [Qemu-devel] aio-posix: Fix concurrent aio_poll/set_fd_handler. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Stefan Weil , Remy Noel , Fam Zheng , "open list:Block I/O path" , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Remy Noel We no longer modify existing handlers entries and instead, always insert those after having properly initialized those. Also, we do not call aio_epoll_update for deleted handlers as this has no impact whastoever. Signed-off-by: Remy Noel --- util/aio-posix.c | 85 ++++++++++++++++++++++++++++-------------------- util/aio-win32.c | 54 ++++++++++++++---------------- 2 files changed, 74 insertions(+), 65 deletions(-) diff --git a/util/aio-posix.c b/util/aio-posix.c index 51c41ed3c9..b34d97292a 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -200,6 +200,31 @@ static AioHandler *find_aio_handler(AioContext *ctx, i= nt fd) return NULL; } =20 +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); + } + + /* If a read is in progress, just mark the node as deleted */ + if (qemu_lockcnt_count(&ctx->list_lock)) { + node->deleted =3D 1; + node->pfd.revents =3D 0; + 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. + */ + QLIST_REMOVE(node, node); + return true; +} + void aio_set_fd_handler(AioContext *ctx, int fd, bool is_external, @@ -209,6 +234,7 @@ void aio_set_fd_handler(AioContext *ctx, void *opaque) { AioHandler *node; + AioHandler *new_node =3D NULL; bool is_new =3D false; bool deleted =3D false; int poll_disable_change; @@ -223,50 +249,35 @@ void aio_set_fd_handler(AioContext *ctx, qemu_lockcnt_unlock(&ctx->list_lock); return; } - - /* 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); - } - - /* If a read is in progress, just mark the node as deleted */ - if (qemu_lockcnt_count(&ctx->list_lock)) { - node->deleted =3D 1; - node->pfd.revents =3D 0; - } else { - /* 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. - */ - QLIST_REMOVE(node, node); - deleted =3D true; - } + deleted =3D aio_remove_fd_handler(ctx, node); poll_disable_change =3D -!node->io_poll; } else { poll_disable_change =3D !io_poll - (node && !node->io_poll); if (node =3D=3D NULL) { - /* Alloc and insert if it's not already there */ - node =3D g_new0(AioHandler, 1); - node->pfd.fd =3D fd; - QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node); - - g_source_add_poll(&ctx->source, &node->pfd); is_new =3D true; } + /* Alloc and insert if it's not already there */ + new_node =3D g_new0(AioHandler, 1); =20 /* Update handler with latest information */ - node->io_read =3D io_read; - node->io_write =3D io_write; - node->io_poll =3D io_poll; - node->opaque =3D opaque; - node->is_external =3D is_external; + new_node->io_read =3D io_read; + new_node->io_write =3D io_write; + new_node->io_poll =3D io_poll; + new_node->opaque =3D opaque; + new_node->is_external =3D is_external; + + if (is_new) { + new_node->pfd.fd =3D fd; + } else { + deleted =3D aio_remove_fd_handler(ctx, node); + new_node->pfd =3D node->pfd; + } + g_source_add_poll(&ctx->source, &new_node->pfd); =20 - node->pfd.events =3D (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0); - node->pfd.events |=3D (io_write ? G_IO_OUT | G_IO_ERR : 0); + new_node->pfd.events =3D (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR = : 0); + new_node->pfd.events |=3D (io_write ? G_IO_OUT | G_IO_ERR : 0); + + QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, new_node, node); } =20 /* No need to order poll_disable_cnt writes against other updates; @@ -278,7 +289,9 @@ void aio_set_fd_handler(AioContext *ctx, atomic_set(&ctx->poll_disable_cnt, atomic_read(&ctx->poll_disable_cnt) + poll_disable_change); =20 - aio_epoll_update(ctx, node, is_new); + if (new_node) { + aio_epoll_update(ctx, new_node, is_new); + } qemu_lockcnt_unlock(&ctx->list_lock); aio_notify(ctx); =20 diff --git a/util/aio-win32.c b/util/aio-win32.c index c58957cc4b..00e38cdd9f 100644 --- a/util/aio-win32.c +++ b/util/aio-win32.c @@ -35,6 +35,22 @@ struct AioHandler { QLIST_ENTRY(AioHandler) node; }; =20 +static void aio_remove_fd_handler(AioContext *ctx, AioHandler *node) +{ + /* If aio_poll is in progress, just mark the node as deleted */ + if (qemu_lockcnt_count(&ctx->list_lock)) { + node->deleted =3D 1; + node->pfd.revents =3D 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); + } +} + void aio_set_fd_handler(AioContext *ctx, int fd, bool is_external, @@ -56,30 +72,20 @@ void aio_set_fd_handler(AioContext *ctx, /* Are we deleting the fd handler? */ if (!io_read && !io_write) { if (node) { - /* If aio_poll is in progress, just mark the node as deleted */ - if (qemu_lockcnt_count(&ctx->list_lock)) { - node->deleted =3D 1; - node->pfd.revents =3D 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); - } + aio_remove_fd_handler(ctx, node); } } else { HANDLE event; long bitmask =3D 0; =20 - if (node =3D=3D NULL) { - /* Alloc and insert if it's not already there */ - node =3D g_new0(AioHandler, 1); - node->pfd.fd =3D fd; - QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node); + if (node) { + aio_remove_fd_handler(ctx, node); } =20 + /* Alloc and insert if it's not already there */ + node =3D g_new0(AioHandler, 1); + node->pfd.fd =3D fd; + node->pfd.events =3D 0; if (node->io_read) { node->pfd.events |=3D G_IO_IN; @@ -104,6 +110,7 @@ void aio_set_fd_handler(AioContext *ctx, bitmask |=3D FD_WRITE | FD_CONNECT; } =20 + QLIST_INSERT_HEAD_RCU(&ctx->aio_handlers, node, node); event =3D event_notifier_get_handle(&ctx->notifier); WSAEventSelect(node->pfd.fd, event, bitmask); } @@ -139,18 +146,7 @@ void aio_set_event_notifier(AioContext *ctx, if (node) { g_source_remove_poll(&ctx->source, &node->pfd); =20 - /* aio_poll is in progress, just mark the node as deleted */ - if (qemu_lockcnt_count(&ctx->list_lock)) { - node->deleted =3D 1; - node->pfd.revents =3D 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); - } + aio_remove_fd_handler(ctx, node); } } else { if (node =3D=3D NULL) { --=20 2.19.1