From nobody Mon Sep 16 18:57:32 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1675430445; cv=none; d=zohomail.com; s=zohoarc; b=jMP/8L8kpqkbfhGoWZmLr3Bj62K+M25wNiroQOP/uWjg5zTeNjb3K9VYiJDnY+va4reEM++QtHtXdROWfhzSm5Guo2IPS4+QC4leTr3z94eQwOD1SCeEXSqfXDjaxwlrZyDDjxOM/eWcHOPYHeIEGp/OXk2tOpvcFo9RdXNhR2w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675430445; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=5rfukRz4EwI39UCHCn9ZQZjhf9HGOmdDQ8mwzAR/5og=; b=AJASBRvqp8R3lrUP8OdsNhs0Fcxwa9GscVO/2sCZ7RU8IU3wFUxwTcx0VrDGAQ9fXxkkQliDLUfp+YEOuYFrVZ3YXV3tqudm9XBeiFYoOqz7sBZjvBE4Vt3s4j7u6thpTpWsifp/h/9qSzcDuNcp0dbxuaVwxDd327WuexRUryE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1675430445422846.3241744942266; Fri, 3 Feb 2023 05:20:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pNvy7-0002rO-UM; Fri, 03 Feb 2023 08:19:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pNvwn-0002Uu-01 for qemu-devel@nongnu.org; Fri, 03 Feb 2023 08:17:45 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pNvwi-0005l6-QS for qemu-devel@nongnu.org; Fri, 03 Feb 2023 08:17:44 -0500 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-102-0v10CpXFPY2Iku6pK-c5pw-1; Fri, 03 Feb 2023 08:17:36 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E3A4738041D2; Fri, 3 Feb 2023 13:17:35 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60BA22026FFF; Fri, 3 Feb 2023 13:17:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675430259; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5rfukRz4EwI39UCHCn9ZQZjhf9HGOmdDQ8mwzAR/5og=; b=ZPrh0ltBlIWqqqJajw6q9MFGcAIurmr1BBs0s+J6lGxmdKU+qjmLBpram+Qld8aVmbcpWM 10K4uxfPO09ww23nAeY8Hw7S+4y2xqGTHG+9lZCIoX4hvkenIEdKkHrE8OBpHZlT6wz/Co Ui3CxJkj2mTWYukipR/sgHdbYeeL5hY= X-MC-Unique: 0v10CpXFPY2Iku6pK-c5pw-1 From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Cc: Stefan Berger , Kevin Wolf , Hanna Reitz , Stefan Weil , Aarushi Mehta , Julia Suvorova , Stefan Hajnoczi , Stefano Garzarella , Greg Kurz , Christian Schoenebeck , Daniel Henrique Barboza , =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , David Gibson , "Michael S. Tsirkin" , Fam Zheng , Paolo Bonzini , qemu-devel@nongnu.org, qemu-ppc@nongnu.org, Emanuele Giuseppe Esposito Subject: [PATCH v5 1/4] linux-aio: use LinuxAioState from the running thread Date: Fri, 3 Feb 2023 08:17:28 -0500 Message-Id: <20230203131731.851116-2-eesposit@redhat.com> In-Reply-To: <20230203131731.851116-1-eesposit@redhat.com> References: <20230203131731.851116-1-eesposit@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1675430447257100001 Content-Type: text/plain; charset="utf-8" Remove usage of aio_context_acquire by always submitting asynchronous AIO to the current thread's LinuxAioState. In order to prevent mistakes from the caller side, avoid passing LinuxAioSt= ate in laio_io_{plug/unplug} and laio_co_submit, and document the functions to make clear that they work in the current thread's AioContext. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Stefan Hajnoczi --- include/block/aio.h | 4 ---- include/block/raw-aio.h | 18 ++++++++++++------ include/sysemu/block-backend-io.h | 6 ++++++ block/file-posix.c | 10 +++------- block/linux-aio.c | 29 +++++++++++++++++------------ 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/include/block/aio.h b/include/block/aio.h index 8fba6a3584..b6b396cfcb 100644 --- a/include/block/aio.h +++ b/include/block/aio.h @@ -208,10 +208,6 @@ struct AioContext { struct ThreadPool *thread_pool; =20 #ifdef CONFIG_LINUX_AIO - /* - * State for native Linux AIO. Uses aio_context_acquire/release for - * locking. - */ struct LinuxAioState *linux_aio; #endif #ifdef CONFIG_LINUX_IO_URING diff --git a/include/block/raw-aio.h b/include/block/raw-aio.h index f8cda9df91..db614472e6 100644 --- a/include/block/raw-aio.h +++ b/include/block/raw-aio.h @@ -49,14 +49,20 @@ typedef struct LinuxAioState LinuxAioState; LinuxAioState *laio_init(Error **errp); void laio_cleanup(LinuxAioState *s); -int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, in= t fd, - uint64_t offset, QEMUIOVector *qiov, int t= ype, - uint64_t dev_max_batch); + +/* laio_co_submit: submit I/O requests in the thread's current AioContext.= */ +int coroutine_fn laio_co_submit(int fd, uint64_t offset, QEMUIOVector *qio= v, + int type, uint64_t dev_max_batch); + void laio_detach_aio_context(LinuxAioState *s, AioContext *old_context); void laio_attach_aio_context(LinuxAioState *s, AioContext *new_context); -void laio_io_plug(BlockDriverState *bs, LinuxAioState *s); -void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s, - uint64_t dev_max_batch); + +/* + * laio_io_plug/unplug work in the thread's current AioContext, therefore = the + * caller must ensure that they are paired in the same IOThread. + */ +void laio_io_plug(void); +void laio_io_unplug(uint64_t dev_max_batch); #endif /* io_uring.c - Linux io_uring implementation */ #ifdef CONFIG_LINUX_IO_URING diff --git a/include/sysemu/block-backend-io.h b/include/sysemu/block-backe= nd-io.h index 031a27ba10..d41698ccc5 100644 --- a/include/sysemu/block-backend-io.h +++ b/include/sysemu/block-backend-io.h @@ -74,8 +74,14 @@ void blk_iostatus_set_err(BlockBackend *blk, int error); int blk_get_max_iov(BlockBackend *blk); int blk_get_max_hw_iov(BlockBackend *blk); =20 +/* + * blk_io_plug/unplug are thread-local operations. This means that multiple + * IOThreads can simultaneously call plug/unplug, but the caller must ensu= re + * that each unplug() is called in the same IOThread of the matching plug(= ). + */ void blk_io_plug(BlockBackend *blk); void blk_io_unplug(BlockBackend *blk); + AioContext *blk_get_aio_context(BlockBackend *blk); BlockAcctStats *blk_get_stats(BlockBackend *blk); void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk, diff --git a/block/file-posix.c b/block/file-posix.c index fa227d9d14..fa99d1c25a 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2095,10 +2095,8 @@ static int coroutine_fn raw_co_prw(BlockDriverState = *bs, uint64_t offset, #endif #ifdef CONFIG_LINUX_AIO } else if (s->use_linux_aio) { - LinuxAioState *aio =3D aio_get_linux_aio(bdrv_get_aio_context(bs)); assert(qiov->size =3D=3D bytes); - return laio_co_submit(bs, aio, s->fd, offset, qiov, type, - s->aio_max_batch); + return laio_co_submit(s->fd, offset, qiov, type, s->aio_max_batch); #endif } =20 @@ -2137,8 +2135,7 @@ static void raw_aio_plug(BlockDriverState *bs) BDRVRawState __attribute__((unused)) *s =3D bs->opaque; #ifdef CONFIG_LINUX_AIO if (s->use_linux_aio) { - LinuxAioState *aio =3D aio_get_linux_aio(bdrv_get_aio_context(bs)); - laio_io_plug(bs, aio); + laio_io_plug(); } #endif #ifdef CONFIG_LINUX_IO_URING @@ -2154,8 +2151,7 @@ static void raw_aio_unplug(BlockDriverState *bs) BDRVRawState __attribute__((unused)) *s =3D bs->opaque; #ifdef CONFIG_LINUX_AIO if (s->use_linux_aio) { - LinuxAioState *aio =3D aio_get_linux_aio(bdrv_get_aio_context(bs)); - laio_io_unplug(bs, aio, s->aio_max_batch); + laio_io_unplug(s->aio_max_batch); } #endif #ifdef CONFIG_LINUX_IO_URING diff --git a/block/linux-aio.c b/block/linux-aio.c index d2cfb7f523..fc50cdd1bf 100644 --- a/block/linux-aio.c +++ b/block/linux-aio.c @@ -16,6 +16,9 @@ #include "qemu/coroutine.h" #include "qapi/error.h" =20 +/* Only used for assertions. */ +#include "qemu/coroutine_int.h" + #include =20 /* @@ -56,10 +59,8 @@ struct LinuxAioState { io_context_t ctx; EventNotifier e; =20 - /* io queue for submit at batch. Protected by AioContext lock. */ + /* No locking required, only accessed from AioContext home thread */ LaioQueue io_q; - - /* I/O completion processing. Only runs in I/O thread. */ QEMUBH *completion_bh; int event_idx; int event_max; @@ -102,6 +103,7 @@ static void qemu_laio_process_completion(struct qemu_la= iocb *laiocb) * later. Coroutines cannot be entered recursively so avoid doing * that! */ + assert(laiocb->co->ctx =3D=3D laiocb->ctx->aio_context); if (!qemu_coroutine_entered(laiocb->co)) { aio_co_wake(laiocb->co); } @@ -232,13 +234,11 @@ static void qemu_laio_process_completions(LinuxAioSta= te *s) =20 static void qemu_laio_process_completions_and_submit(LinuxAioState *s) { - aio_context_acquire(s->aio_context); qemu_laio_process_completions(s); =20 if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) { ioq_submit(s); } - aio_context_release(s->aio_context); } =20 static void qemu_laio_completion_bh(void *opaque) @@ -354,14 +354,19 @@ static uint64_t laio_max_batch(LinuxAioState *s, uint= 64_t dev_max_batch) return max_batch; } =20 -void laio_io_plug(BlockDriverState *bs, LinuxAioState *s) +void laio_io_plug(void) { + AioContext *ctx =3D qemu_get_current_aio_context(); + LinuxAioState *s =3D aio_get_linux_aio(ctx); + s->io_q.plugged++; } =20 -void laio_io_unplug(BlockDriverState *bs, LinuxAioState *s, - uint64_t dev_max_batch) +void laio_io_unplug(uint64_t dev_max_batch) { + AioContext *ctx =3D qemu_get_current_aio_context(); + LinuxAioState *s =3D aio_get_linux_aio(ctx); + assert(s->io_q.plugged); s->io_q.plugged--; =20 @@ -411,15 +416,15 @@ static int laio_do_submit(int fd, struct qemu_laiocb = *laiocb, off_t offset, return 0; } =20 -int coroutine_fn laio_co_submit(BlockDriverState *bs, LinuxAioState *s, in= t fd, - uint64_t offset, QEMUIOVector *qiov, int t= ype, - uint64_t dev_max_batch) +int coroutine_fn laio_co_submit(int fd, uint64_t offset, QEMUIOVector *qio= v, + int type, uint64_t dev_max_batch) { int ret; + AioContext *ctx =3D qemu_get_current_aio_context(); struct qemu_laiocb laiocb =3D { .co =3D qemu_coroutine_self(), .nbytes =3D qiov->size, - .ctx =3D s, + .ctx =3D aio_get_linux_aio(ctx), .ret =3D -EINPROGRESS, .is_read =3D (type =3D=3D QEMU_AIO_READ), .qiov =3D qiov, --=20 2.39.1