From nobody Sat Feb 7 04:40:18 2026 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; 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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1511967241225687.3646195229359; Wed, 29 Nov 2017 06:54:01 -0800 (PST) Received: from localhost ([::1]:43471 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eK3k9-0007Tq-Ut for importer@patchew.org; Wed, 29 Nov 2017 09:53:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41416) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eK3i8-0005TU-Od for qemu-devel@nongnu.org; Wed, 29 Nov 2017 09:51:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eK3i7-00061K-I1 for qemu-devel@nongnu.org; Wed, 29 Nov 2017 09:51:40 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56264) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eK3i2-0005wU-QW; Wed, 29 Nov 2017 09:51:34 -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 mx1.redhat.com (Postfix) with ESMTPS id D7D1180480; Wed, 29 Nov 2017 14:51:33 +0000 (UTC) Received: from lemon.redhat.com (ovpn-12-98.pek2.redhat.com [10.72.12.98]) by smtp.corp.redhat.com (Postfix) with ESMTP id F0F2C5C269; Wed, 29 Nov 2017 14:51:13 +0000 (UTC) From: Fam Zheng To: qemu-devel@nongnu.org Date: Wed, 29 Nov 2017 22:49:49 +0800 Message-Id: <20171129144956.11409-3-famz@redhat.com> In-Reply-To: <20171129144956.11409-1-famz@redhat.com> References: <20171129144956.11409-1-famz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 29 Nov 2017 14:51:33 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH RFC 2/9] aio: Add drain begin/end API to AioContext 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: Kevin Wolf , Fam Zheng , qemu-block@nongnu.org, jcody@redhat.com, Max Reitz , Stefan Hajnoczi , pbonzini@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Fam Zheng --- include/block/aio.h | 27 +++++++++++++++++--- util/async.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/include/block/aio.h b/include/block/aio.h index e9aeeaec94..40c2f64544 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -47,6 +47,15 @@ typedef void QEMUBHFunc(void *opaque); typedef bool AioPollFn(void *opaque); typedef void IOHandler(void *opaque); =20 +typedef void AioDrainFn(void *opaque); +typedef struct AioDrainOps { + AioDrainFn *drained_begin; + AioDrainFn *drained_end; + void *opaque; + bool is_new; + QTAILQ_ENTRY(AioDrainOps) next; +} AioDrainOps; + struct Coroutine; struct ThreadPool; struct LinuxAioState; @@ -147,6 +156,9 @@ struct AioContext { int epollfd; bool epoll_enabled; bool epoll_available; + + QTAILQ_HEAD(, AioDrainOps) drain_ops; + bool drain_ops_updated; }; =20 /** @@ -441,9 +453,9 @@ int64_t aio_compute_timeout(AioContext *ctx); * * Disable the further processing of external clients. */ -static inline void aio_disable_external(AioContext *ctx) +static inline bool aio_disable_external(AioContext *ctx) { - atomic_inc(&ctx->external_disable_cnt); + return atomic_fetch_inc(&ctx->external_disable_cnt) =3D=3D 0; } =20 /** @@ -452,7 +464,7 @@ static inline void aio_disable_external(AioContext *ctx) * * Enable the processing of external clients. */ -static inline void aio_enable_external(AioContext *ctx) +static inline bool aio_enable_external(AioContext *ctx) { int old; =20 @@ -462,6 +474,7 @@ static inline void aio_enable_external(AioContext *ctx) /* Kick event loop so it re-arms file descriptors */ aio_notify(ctx); } + return old =3D=3D 1; } =20 /** @@ -564,4 +577,12 @@ void aio_context_set_poll_params(AioContext *ctx, int6= 4_t max_ns, int64_t grow, int64_t shrink, Error **errp); =20 +void aio_context_drained_begin(AioContext *ctx); +void aio_context_drained_end(AioContext *ctx); + +void aio_context_add_drain_ops(AioContext *ctx, + AioDrainFn *begin, AioDrainFn *end, void *o= paque); +void aio_context_del_drain_ops(AioContext *ctx, + AioDrainFn *begin, AioDrainFn *end, void *o= paque); + #endif diff --git a/util/async.c b/util/async.c index 4dd9d95a9e..cca0efd263 100644 --- a/util/async.c +++ b/util/async.c @@ -402,6 +402,7 @@ AioContext *aio_context_new(Error **errp) AioContext *ctx; =20 ctx =3D (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioConte= xt)); + QTAILQ_INIT(&ctx->drain_ops); aio_context_setup(ctx); =20 ret =3D event_notifier_init(&ctx->notifier, false); @@ -506,3 +507,75 @@ void aio_context_release(AioContext *ctx) { qemu_rec_mutex_unlock(&ctx->lock); } + +/* Called with ctx->lock */ +void aio_context_drained_begin(AioContext *ctx) +{ + AioDrainOps *ops; + + /* TODO: When all external fds are handled in the following drain_ops + * callbacks, aio_disable_external can be dropped. */ + aio_disable_external(ctx); +restart: + ctx->drain_ops_updated =3D false; + QTAILQ_FOREACH(ops, &ctx->drain_ops, next) { + ops->drained_begin(ops->opaque); + if (ctx->drain_ops_updated) { + goto restart; + } + } +} + +/* Called with ctx->lock */ +void aio_context_drained_end(AioContext *ctx) +{ + AioDrainOps *ops; + +restart: + ctx->drain_ops_updated =3D false; + QTAILQ_FOREACH(ops, &ctx->drain_ops, next) { + if (ops->is_new) { + continue; + } + ops->drained_end(ops->opaque); + if (ctx->drain_ops_updated) { + goto restart; + } + } + if (aio_enable_external(ctx)) { + QTAILQ_FOREACH(ops, &ctx->drain_ops, next) { + ops->is_new =3D false; + } + } +} + +/* Called with ctx->lock */ +void aio_context_add_drain_ops(AioContext *ctx, + AioDrainFn *begin, AioDrainFn *end, void *o= paque) +{ + AioDrainOps *ops =3D g_new0(AioDrainOps, 1); + ops->drained_begin =3D begin; + ops->drained_end =3D end; + ops->opaque =3D opaque; + ops->is_new =3D true; + QTAILQ_INSERT_TAIL(&ctx->drain_ops, ops, next); + ctx->drain_ops_updated =3D true; +} + +/* Called with ctx->lock */ +void aio_context_del_drain_ops(AioContext *ctx, + AioDrainFn *begin, AioDrainFn *end, void *o= paque) +{ + AioDrainOps *ops; + + QTAILQ_FOREACH(ops, &ctx->drain_ops, next) { + if (ops->drained_begin =3D=3D begin && + ops->drained_end =3D=3D end && + ops->opaque =3D=3D opaque) { + QTAILQ_REMOVE(&ctx->drain_ops, ops, next); + ctx->drain_ops_updated =3D true; + g_free(ops); + return; + } + } +} --=20 2.14.3