From nobody Mon Feb 9 16:44:53 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1581700751035836.5495198877828; Fri, 14 Feb 2020 09:19:11 -0800 (PST) Received: from localhost ([::1]:42050 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2ecP-00036V-Mu for importer@patchew.org; Fri, 14 Feb 2020 12:19:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49269) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j2ebM-0001Sc-OQ for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:18:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j2ebL-00084A-CO for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:18:04 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:23874 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j2ebL-00083Q-8w for qemu-devel@nongnu.org; Fri, 14 Feb 2020 12:18:03 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-58-e2kK7k8KNWqLJnbpetpojw-1; Fri, 14 Feb 2020 12:17:48 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 73B92189F760; Fri, 14 Feb 2020 17:17:47 +0000 (UTC) Received: from localhost (ovpn-116-135.ams2.redhat.com [10.36.116.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE3915C1C3; Fri, 14 Feb 2020 17:17:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1581700682; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pEfGyIySH/diqfJDkGzBmpZrSWEyb9Lev6WK6Q4sL5A=; b=EhD9vZXx/GvM5rD6ulrUHpARYyd/K1fzJ8JPndrzIVh3RfucCnqTGgxnj3e3NBaYEcCe7+ 8fQEHVCux8cvruye65GgP/hxaNKqvuB2EyE4Ofe4blUn6e5p4VdxXDc5+nhNfLRoowBp1s IZYl01kIpWr3Wvw0WpQ1kyb9hAdY2rg= From: Stefan Hajnoczi To: qemu-devel@nongnu.org Subject: [PATCH 4/5] aio-posix: make AioHandler deletion O(1) Date: Fri, 14 Feb 2020 17:17:11 +0000 Message-Id: <20200214171712.541358-5-stefanha@redhat.com> In-Reply-To: <20200214171712.541358-1-stefanha@redhat.com> References: <20200214171712.541358-1-stefanha@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-MC-Unique: e2kK7k8KNWqLJnbpetpojw-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Max Reitz , Stefan Hajnoczi , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini 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" It is not necessary to scan all AioHandlers for deletion. Keep a list of deleted handlers instead of scanning the full list of all handlers. The AioHandler->deleted field can be dropped. Let's check if the handler has been inserted into the deleted list instead. Add a new QLIST_IS_INSERTED() API for this check. Signed-off-by: Stefan Hajnoczi Reviewed-by: Sergio Lopez --- include/block/aio.h | 6 ++++- include/qemu/queue.h | 3 +++ util/aio-posix.c | 53 +++++++++++++++++++++++++++++--------------- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/include/block/aio.h b/include/block/aio.h index 7ba9bd7874..1a0de1508c 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -42,6 +42,7 @@ void qemu_aio_unref(void *p); void qemu_aio_ref(void *p); =20 typedef struct AioHandler AioHandler; +typedef QLIST_HEAD(, AioHandler) AioHandlerList; typedef void QEMUBHFunc(void *opaque); typedef bool AioPollFn(void *opaque); typedef void IOHandler(void *opaque); @@ -58,7 +59,10 @@ struct AioContext { QemuRecMutex lock; =20 /* The list of registered AIO handlers. Protected by ctx->list_lock. = */ - QLIST_HEAD(, AioHandler) aio_handlers; + AioHandlerList aio_handlers; + + /* The list of AIO handlers to be deleted. Protected by ctx->list_loc= k. */ + AioHandlerList deleted_aio_handlers; =20 /* Used to avoid unnecessary event_notifier_set calls in aio_notify; * accessed with atomic primitives. If this field is 0, everything diff --git a/include/qemu/queue.h b/include/qemu/queue.h index a276363372..699a8a0568 100644 --- a/include/qemu/queue.h +++ b/include/qemu/queue.h @@ -158,6 +158,9 @@ struct { = \ } \ } while (/*CONSTCOND*/0) =20 +/* Is elm in a list? */ +#define QLIST_IS_INSERTED(elm, field) ((elm)->field.le_prev !=3D NULL) + #define QLIST_FOREACH(var, head, field) \ for ((var) =3D ((head)->lh_first); \ (var); \ diff --git a/util/aio-posix.c b/util/aio-posix.c index b21bcd8e97..3a98a2acb9 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -31,10 +31,10 @@ struct AioHandler AioPollFn *io_poll; IOHandler *io_poll_begin; IOHandler *io_poll_end; - int deleted; void *opaque; bool is_external; QLIST_ENTRY(AioHandler) node; + QLIST_ENTRY(AioHandler) node_deleted; }; =20 #ifdef CONFIG_EPOLL_CREATE1 @@ -67,7 +67,7 @@ static bool aio_epoll_try_enable(AioContext *ctx) =20 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { int r; - if (node->deleted || !node->pfd.events) { + if (QLIST_IS_INSERTED(node, node_deleted) || !node->pfd.events) { continue; } event.events =3D epoll_events_from_pfd(node->pfd.events); @@ -195,9 +195,11 @@ static AioHandler *find_aio_handler(AioContext *ctx, i= nt fd) AioHandler *node; =20 QLIST_FOREACH(node, &ctx->aio_handlers, node) { - if (node->pfd.fd =3D=3D fd) - if (!node->deleted) + if (node->pfd.fd =3D=3D fd) { + if (!QLIST_IS_INSERTED(node, node_deleted)) { return node; + } + } } =20 return NULL; @@ -216,7 +218,7 @@ static bool aio_remove_fd_handler(AioContext *ctx, AioH= andler *node) =20 /* If a read is in progress, just mark the node as deleted */ if (qemu_lockcnt_count(&ctx->list_lock)) { - node->deleted =3D 1; + QLIST_INSERT_HEAD_RCU(&ctx->deleted_aio_handlers, node, node_delet= ed); node->pfd.revents =3D 0; return false; } @@ -358,7 +360,7 @@ static void poll_set_started(AioContext *ctx, bool star= ted) QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { IOHandler *fn; =20 - if (node->deleted) { + if (QLIST_IS_INSERTED(node, node_deleted)) { continue; } =20 @@ -415,6 +417,26 @@ bool aio_pending(AioContext *ctx) return result; } =20 +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 =3D QLIST_FIRST_RCU(&ctx->deleted_aio_handlers))) { + QLIST_REMOVE(node, node); + QLIST_REMOVE(node, node_deleted); + g_free(node); + } + + qemu_lockcnt_inc_and_unlock(&ctx->list_lock); +} + static bool aio_dispatch_handlers(AioContext *ctx) { AioHandler *node, *tmp; @@ -426,7 +448,7 @@ static bool aio_dispatch_handlers(AioContext *ctx) revents =3D node->pfd.revents & node->pfd.events; node->pfd.revents =3D 0; =20 - if (!node->deleted && + 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) { @@ -437,21 +459,13 @@ static bool aio_dispatch_handlers(AioContext *ctx) progress =3D true; } } - if (!node->deleted && + 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 =3D true; } - - if (node->deleted) { - if (qemu_lockcnt_dec_if_lock(&ctx->list_lock)) { - QLIST_REMOVE(node, node); - g_free(node); - qemu_lockcnt_inc_and_unlock(&ctx->list_lock); - } - } } =20 return progress; @@ -462,6 +476,7 @@ void aio_dispatch(AioContext *ctx) qemu_lockcnt_inc(&ctx->list_lock); aio_bh_poll(ctx); aio_dispatch_handlers(ctx); + aio_free_deleted_handlers(ctx); qemu_lockcnt_dec(&ctx->list_lock); =20 timerlistgroup_run_timers(&ctx->tlg); @@ -519,7 +534,7 @@ static bool run_poll_handlers_once(AioContext *ctx, int= 64_t *timeout) AioHandler *node; =20 QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { - if (!node->deleted && node->io_poll && + if (!QLIST_IS_INSERTED(node, node_deleted) && node->io_poll && aio_node_check(ctx, node->is_external) && node->io_poll(node->opaque)) { /* @@ -653,7 +668,7 @@ bool aio_poll(AioContext *ctx, bool blocking) =20 if (!aio_epoll_enabled(ctx)) { QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) { - if (!node->deleted && node->pfd.events + if (!QLIST_IS_INSERTED(node, node_deleted) && node->pfd.ev= ents && aio_node_check(ctx, node->is_external)) { add_pollfd(node); } @@ -730,6 +745,8 @@ bool aio_poll(AioContext *ctx, bool blocking) progress |=3D aio_dispatch_handlers(ctx); } =20 + aio_free_deleted_handlers(ctx); + qemu_lockcnt_dec(&ctx->list_lock); =20 progress |=3D timerlistgroup_run_timers(&ctx->tlg); --=20 2.24.1