From nobody Mon Sep 22 03:36:20 2025 Delivered-To: importer@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+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1757528098; cv=none; d=zohomail.com; s=zohoarc; b=DSVP6lDRNf9QUxqbrMj6enAUX6QbAzcaFjziF6nIM+FsU2/EWrjQvRc/3pvffzogj3Pb6llfrHkeqTzPkK7t8ve947OfvJ++dh4YAc69Y+yBiD0KSonwZGogzcoE0uuh9Q0aqP0jrmB5WuTvH6RYUDal+0MJepUZTxp1UznNOTc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1757528098; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eol21QjpcqS+1NzmihTA3L9wQ1fcb13YdBbC/Hdfqwo=; b=AR7/neANEFEjzSAll8/inQAXdSwI8M92V8dIQ2SNRXZHNZTtJWZwxDRzQJvWxid/t3fnolbgM9fZqLJ0nhr6QRuTDW+DFEzCaT9Z54cqAHqIPFHj6pvhAq/+viR+lmA14wO28DZXQukwhEZZ2Y/VGnGF2u18fv5H0QkLgSm46qE= 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+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1757528098886586.0084142905914; Wed, 10 Sep 2025 11:14:58 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uwPEp-0005pc-Sd; Wed, 10 Sep 2025 14:08:17 -0400 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 1uwPDn-0004jW-2U for qemu-devel@nongnu.org; Wed, 10 Sep 2025 14:07:08 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uwPDe-00055i-LP for qemu-devel@nongnu.org; Wed, 10 Sep 2025 14:07:05 -0400 Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-1-xOZMvGDaP8eXy7sKrBQqgw-1; Wed, 10 Sep 2025 14:06:54 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D64B7195609F; Wed, 10 Sep 2025 18:06:52 +0000 (UTC) Received: from localhost (unknown [10.2.17.66]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3499D3000198; Wed, 10 Sep 2025 18:06:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1757527617; 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=eol21QjpcqS+1NzmihTA3L9wQ1fcb13YdBbC/Hdfqwo=; b=iXIUo4Djvuh3kE1naQEzxDnm60JWSLz466p1DZoas2VTF8gIUHOd9SLSBiRs6af3KKYLWs SHGdErX8mUK7tXRhh+p+kqn1ZuOR24nd8abx/fZV0P5NHku980aB4kQEptWlDzQkCcWRnW f7cdV43i8oyxChZNT6mQlaI2NsmVY10= X-MC-Unique: xOZMvGDaP8eXy7sKrBQqgw-1 X-Mimecast-MFC-AGG-ID: xOZMvGDaP8eXy7sKrBQqgw_1757527613 From: Stefan Hajnoczi To: qemu-devel@nongnu.org Cc: Aarushi Mehta , Fam Zheng , Stefano Garzarella , Hanna Czenczek , eblake@redhat.com, Stefan Hajnoczi , Kevin Wolf , qemu-block@nongnu.org, hibriansong@gmail.com, Stefan Weil , Paolo Bonzini Subject: [PATCH v4 08/12] aio-posix: gracefully handle io_uring_queue_init() failure Date: Wed, 10 Sep 2025 13:56:59 -0400 Message-ID: <20250910175703.374499-9-stefanha@redhat.com> In-Reply-To: <20250910175703.374499-1-stefanha@redhat.com> References: <20250910175703.374499-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.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+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=stefanha@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_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, T_SPF_TEMPERROR=0.01 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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1757528099804116600 Content-Type: text/plain; charset="utf-8" io_uring may not be available at runtime due to system policies (e.g. the io_uring_disabled sysctl) or creation could fail due to file descriptor resource limits. Handle failure scenarios as follows: If another AioContext already has io_uring, then fail AioContext creation so that the aio_add_sqe() API is available uniformly from all QEMU threads. Otherwise fall back to epoll(7) if io_uring is unavailable. Notes: - Update the comment about selecting the fastest fdmon implementation. At this point it's not about speed anymore, it's about aio_add_sqe() API availability. - Uppercase the error message when converting from error_report() to error_setg_errno() for consistency (but there are instances of lowercase in the codebase). - It's easier to move the #ifdefs from aio-posix.h to aio-posix.c. Signed-off-by: Stefan Hajnoczi Reviewed-by: Eric Blake --- util/aio-posix.h | 12 ++---------- util/aio-posix.c | 29 ++++++++++++++++++++++++++--- util/fdmon-io_uring.c | 8 ++++---- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/util/aio-posix.h b/util/aio-posix.h index f9994ed79e..6f9d97d866 100644 --- a/util/aio-posix.h +++ b/util/aio-posix.h @@ -18,6 +18,7 @@ #define AIO_POSIX_H =20 #include "block/aio.h" +#include "qapi/error.h" =20 struct AioHandler { GPollFD pfd; @@ -72,17 +73,8 @@ static inline void fdmon_epoll_disable(AioContext *ctx) #endif /* !CONFIG_EPOLL_CREATE1 */ =20 #ifdef CONFIG_LINUX_IO_URING -bool fdmon_io_uring_setup(AioContext *ctx); +void fdmon_io_uring_setup(AioContext *ctx, Error **errp); void fdmon_io_uring_destroy(AioContext *ctx); -#else -static inline bool fdmon_io_uring_setup(AioContext *ctx) -{ - return false; -} - -static inline void fdmon_io_uring_destroy(AioContext *ctx) -{ -} #endif /* !CONFIG_LINUX_IO_URING */ =20 #endif /* AIO_POSIX_H */ diff --git a/util/aio-posix.c b/util/aio-posix.c index bbe470c145..800b4debbf 100644 --- a/util/aio-posix.c +++ b/util/aio-posix.c @@ -16,6 +16,7 @@ #include "qemu/osdep.h" #include "block/block.h" #include "block/thread-pool.h" +#include "qapi/error.h" #include "qemu/main-loop.h" #include "qemu/lockcnt.h" #include "qemu/rcu.h" @@ -724,17 +725,39 @@ void aio_context_setup(AioContext *ctx, Error **errp) ctx->epollfd =3D -1; ctx->epollfd_tag =3D NULL; =20 - /* Use the fastest fd monitoring implementation if available */ - if (fdmon_io_uring_setup(ctx)) { - return; +#ifdef CONFIG_LINUX_IO_URING + { + static bool need_io_uring; + Error *local_err =3D NULL; /* ERRP_GUARD() doesn't handle error_ab= ort */ + + /* io_uring takes precedence because it provides aio_add_sqe() sup= port */ + fdmon_io_uring_setup(ctx, &local_err); + if (!local_err) { + /* + * If one AioContext gets io_uring, then all AioContexts need = io_uring + * so that aio_add_sqe() support is available across all threa= ds. + */ + need_io_uring =3D true; + return; + } + if (need_io_uring) { + error_propagate(errp, local_err); + return; + } + + /* Silently fall back on systems where io_uring is unavailable */ + error_free(local_err); } +#endif /* CONFIG_LINUX_IO_URING */ =20 fdmon_epoll_setup(ctx); } =20 void aio_context_destroy(AioContext *ctx) { +#ifdef CONFIG_LINUX_IO_URING fdmon_io_uring_destroy(ctx); +#endif =20 qemu_lockcnt_lock(&ctx->list_lock); fdmon_epoll_disable(ctx); diff --git a/util/fdmon-io_uring.c b/util/fdmon-io_uring.c index 4b6dc8903f..3181601767 100644 --- a/util/fdmon-io_uring.c +++ b/util/fdmon-io_uring.c @@ -45,6 +45,7 @@ =20 #include "qemu/osdep.h" #include +#include "qapi/error.h" #include "qemu/rcu_queue.h" #include "aio-posix.h" =20 @@ -369,7 +370,7 @@ static const FDMonOps fdmon_io_uring_ops =3D { .gsource_dispatch =3D fdmon_io_uring_gsource_dispatch, }; =20 -bool fdmon_io_uring_setup(AioContext *ctx) +void fdmon_io_uring_setup(AioContext *ctx, Error **errp) { int ret; =20 @@ -377,15 +378,14 @@ bool fdmon_io_uring_setup(AioContext *ctx) =20 ret =3D io_uring_queue_init(FDMON_IO_URING_ENTRIES, &ctx->fdmon_io_uri= ng, 0); if (ret !=3D 0) { - return false; + error_setg_errno(errp, -ret, "Failed to initialize io_uring"); + return; } =20 QSLIST_INIT(&ctx->submit_list); ctx->fdmon_ops =3D &fdmon_io_uring_ops; ctx->io_uring_fd_tag =3D g_source_add_unix_fd(&ctx->source, ctx->fdmon_io_uring.ring_fd, G_IO_IN); - - return true; } =20 void fdmon_io_uring_destroy(AioContext *ctx) --=20 2.51.0