From nobody Mon Jun 29 19:43:35 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ABA2EC433F5 for ; Thu, 3 Feb 2022 15:12:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237673AbiBCPMV (ORCPT ); Thu, 3 Feb 2022 10:12:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351687AbiBCPMI (ORCPT ); Thu, 3 Feb 2022 10:12:08 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 838F6C06173D for ; Thu, 3 Feb 2022 07:12:07 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id j16so5578042wrd.8 for ; Thu, 03 Feb 2022 07:12:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gKaSUfzNLWzc9S5eqkGES2e8+ihER08nmitDxKLwVSg=; b=TNKYCQA+wbXmtwOR4LwhYEKwX+qlhdU32yu7OhwgLShkj/f14e+F0aBbz7+mn8R5al S2Q/FsoNWbSEbjUXxIeMq6gMoTaZt6eSHM6jUaF/jXwHXhbqQh3lXjEvR5G5iY1MBMST iz0B3EFwYdtn9nNqvHJpwFPEkL7u5AJZLRN2NH1y15DRG8rA6ResRm6UMCG89l1VYjPR iI6/VTJcF1f5rAn+cZ/C30juXtdYppeNYdOywRuBCZcca1wijFUsR42dSvZsWmHP4D9I +upsbQmINcFeB4+Zz/PKGlI0atZH3HvivbbZEFvoptnScr2vI/m+G73PrYA928sjVDGh F4gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=gKaSUfzNLWzc9S5eqkGES2e8+ihER08nmitDxKLwVSg=; b=78RHyna7/yKhHGmPwKbNZVFJo5RbTp3Jden/8ie0MdZTTY06RlssBmlRaOKXOBONOo Edm84SfJVQ70tQ2MEc0YSdfONelT/Adtn3BYu5Pv7Bi25FrurdBBxhCj0HscU6edbpyo 7xmwn3Eb8greJi9f5dLaRHLYxGrRWcxljoE9yvgw1kxVcIvzELISe/qLNIMvhgQBLFx8 mxbRPCD53rcgi7xkJimQjAbhctfmwUm1mcfI8OugjX9avoP9mbnQDjk3y70fxrWqF661 a+XeiEwWJqaoTlXoi+H9yW8YF5Z1uOjivVxBEK++S/YbYioSFhnS9IN+xw7U4I/HB3yO nMWg== X-Gm-Message-State: AOAM533d5C3XJBCcDR0SwS2h1aQ86c9734Q+5Ykr7qziPr6MIeQKDNXn dq+bMPY76c+BWNYdTEe2Bnou9Q== X-Google-Smtp-Source: ABdhPJxPOpL9BnOJQ+Xv6mJwZO0pm8dezDOrnwMvTSOpfbiMQygUPG3WHtzEUtmLozOKp1QlGY7BXg== X-Received: by 2002:a5d:6d03:: with SMTP id e3mr29832138wrq.536.1643901126162; Thu, 03 Feb 2022 07:12:06 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6d:f804:0:28c2:5854:c832:e580]) by smtp.gmail.com with ESMTPSA id m14sm25793665wrp.4.2022.02.03.07.12.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 07:12:05 -0800 (PST) From: Usama Arif To: io-uring@vger.kernel.org, axboe@kernel.dk, asml.silence@gmail.com, linux-kernel@vger.kernel.org Cc: fam.zheng@bytedance.com, Usama Arif Subject: [PATCH 1/2] io_uring: avoid ring quiesce while registering/unregistering eventfd Date: Thu, 3 Feb 2022 15:11:52 +0000 Message-Id: <20220203151153.574032-2-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220203151153.574032-1-usama.arif@bytedance.com> References: <20220203151153.574032-1-usama.arif@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is done by creating a new RCU data structure (io_ev_fd) as part of io_ring_ctx that holds the eventfd_ctx. The function io_eventfd_signal is executed under rcu_read_lock with a single rcu_dereference to io_ev_fd so that if another thread unregisters the eventfd while io_eventfd_signal is still being executed, the eventfd_signal for which io_eventfd_signal was called completes successfully. The process of registering/unregistering eventfd is done under a lock so multiple threads don't enter a race condition while registering/unregistering eventfd. With the above approach ring quiesce can be avoided which is much more expensive then using RCU lock. On the system tested, io_uring_reigster with IORING_REGISTER_EVENTFD takes less than 1ms with RCU lock, compared to 15ms before with ring quiesce. Signed-off-by: Usama Arif --- fs/io_uring.c | 103 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 23 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 2e04f718319d..f07cfbb387a6 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -326,6 +326,12 @@ struct io_submit_state { struct blk_plug plug; }; =20 +struct io_ev_fd { + struct eventfd_ctx *cq_ev_fd; + struct io_ring_ctx *ctx; + struct rcu_head rcu; +}; + struct io_ring_ctx { /* const or read-mostly hot data */ struct { @@ -399,7 +405,8 @@ struct io_ring_ctx { struct { unsigned cached_cq_tail; unsigned cq_entries; - struct eventfd_ctx *cq_ev_fd; + struct io_ev_fd __rcu *io_ev_fd; + struct mutex ev_fd_lock; struct wait_queue_head cq_wait; unsigned cq_extra; atomic_t cq_timeouts; @@ -1448,6 +1455,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(s= truct io_uring_params *p) xa_init_flags(&ctx->io_buffers, XA_FLAGS_ALLOC1); xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1); mutex_init(&ctx->uring_lock); + mutex_init(&ctx->ev_fd_lock); init_waitqueue_head(&ctx->cq_wait); spin_lock_init(&ctx->completion_lock); spin_lock_init(&ctx->timeout_lock); @@ -1726,15 +1734,30 @@ static inline struct io_uring_cqe *io_get_cqe(struc= t io_ring_ctx *ctx) return &rings->cqes[tail & mask]; } =20 -static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx) +static inline bool io_should_trigger_evfd(struct io_ring_ctx *ctx, struct = io_ev_fd *ev_fd) { - if (likely(!ctx->cq_ev_fd)) + if (likely(!ev_fd)) return false; if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) return false; return !ctx->eventfd_async || io_wq_current_is_worker(); } =20 +static void io_eventfd_signal(struct io_ring_ctx *ctx) +{ + struct io_ev_fd *ev_fd; + + rcu_read_lock(); + ev_fd =3D rcu_dereference(ctx->io_ev_fd); + + if (!io_should_trigger_evfd(ctx, ev_fd)) + goto out; + + eventfd_signal(ev_fd->cq_ev_fd, 1); +out: + rcu_read_unlock(); +} + /* * This should only get called when at least one event has been posted. * Some applications rely on the eventfd notification count only changing @@ -1751,8 +1774,7 @@ static void io_cqring_ev_posted(struct io_ring_ctx *c= tx) */ if (wq_has_sleeper(&ctx->cq_wait)) wake_up_all(&ctx->cq_wait); - if (io_should_trigger_evfd(ctx)) - eventfd_signal(ctx->cq_ev_fd, 1); + io_eventfd_signal(ctx); } =20 static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) @@ -1764,8 +1786,7 @@ static void io_cqring_ev_posted_iopoll(struct io_ring= _ctx *ctx) if (waitqueue_active(&ctx->cq_wait)) wake_up_all(&ctx->cq_wait); } - if (io_should_trigger_evfd(ctx)) - eventfd_signal(ctx->cq_ev_fd, 1); + io_eventfd_signal(ctx); } =20 /* Returns true if there are no backlogged entries after the flush */ @@ -9353,35 +9374,67 @@ static int __io_sqe_buffers_update(struct io_ring_c= tx *ctx, =20 static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) { + struct io_ev_fd *ev_fd; __s32 __user *fds =3D arg; - int fd; + int fd, ret; =20 - if (ctx->cq_ev_fd) - return -EBUSY; + mutex_lock(&ctx->ev_fd_lock); + ret =3D -EBUSY; + if (rcu_dereference_protected(ctx->io_ev_fd, lockdep_is_held(&ctx->ev_fd_= lock))) + goto out; =20 + ret =3D -EFAULT; if (copy_from_user(&fd, fds, sizeof(*fds))) - return -EFAULT; + goto out; =20 - ctx->cq_ev_fd =3D eventfd_ctx_fdget(fd); - if (IS_ERR(ctx->cq_ev_fd)) { - int ret =3D PTR_ERR(ctx->cq_ev_fd); + ret =3D -ENOMEM; + ev_fd =3D kmalloc(sizeof(*ev_fd), GFP_KERNEL); + if (!ev_fd) + goto out; =20 - ctx->cq_ev_fd =3D NULL; - return ret; + ev_fd->cq_ev_fd =3D eventfd_ctx_fdget(fd); + if (IS_ERR(ev_fd->cq_ev_fd)) { + ret =3D PTR_ERR(ev_fd->cq_ev_fd); + kfree(ev_fd); + goto out; } + ev_fd->ctx =3D ctx; =20 - return 0; + rcu_assign_pointer(ctx->io_ev_fd, ev_fd); + ret =3D 0; + +out: + mutex_unlock(&ctx->ev_fd_lock); + return ret; +} + +static void io_eventfd_put(struct rcu_head *rcu) +{ + struct io_ev_fd *ev_fd =3D container_of(rcu, struct io_ev_fd, rcu); + struct io_ring_ctx *ctx =3D ev_fd->ctx; + + eventfd_ctx_put(ev_fd->cq_ev_fd); + kfree(ev_fd); + rcu_assign_pointer(ctx->io_ev_fd, NULL); } =20 static int io_eventfd_unregister(struct io_ring_ctx *ctx) { - if (ctx->cq_ev_fd) { - eventfd_ctx_put(ctx->cq_ev_fd); - ctx->cq_ev_fd =3D NULL; - return 0; + struct io_ev_fd *ev_fd; + int ret; + + mutex_lock(&ctx->ev_fd_lock); + ev_fd =3D rcu_dereference_protected(ctx->io_ev_fd, lockdep_is_held(&ctx->= ev_fd_lock)); + if (ev_fd) { + call_rcu(&ev_fd->rcu, io_eventfd_put); + ret =3D 0; + goto out; } + ret =3D -ENXIO; =20 - return -ENXIO; +out: + mutex_unlock(&ctx->ev_fd_lock); + return ret; } =20 static void io_destroy_buffers(struct io_ring_ctx *ctx) @@ -10960,6 +11013,8 @@ static bool io_register_op_must_quiesce(int op) case IORING_REGISTER_FILES: case IORING_UNREGISTER_FILES: case IORING_REGISTER_FILES_UPDATE: + case IORING_REGISTER_EVENTFD: + case IORING_UNREGISTER_EVENTFD: case IORING_REGISTER_PROBE: case IORING_REGISTER_PERSONALITY: case IORING_UNREGISTER_PERSONALITY: @@ -11171,8 +11226,10 @@ SYSCALL_DEFINE4(io_uring_register, unsigned int, f= d, unsigned int, opcode, mutex_lock(&ctx->uring_lock); ret =3D __io_uring_register(ctx, opcode, arg, nr_args); mutex_unlock(&ctx->uring_lock); + rcu_read_lock(); trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_buf= s, - ctx->cq_ev_fd !=3D NULL, ret); + rcu_dereference(ctx->io_ev_fd) !=3D NULL, ret); + rcu_read_unlock(); out_fput: fdput(f); return ret; --=20 2.25.1 From nobody Mon Jun 29 19:43:35 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 608A3C433F5 for ; Thu, 3 Feb 2022 15:12:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351735AbiBCPMZ (ORCPT ); Thu, 3 Feb 2022 10:12:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239467AbiBCPMJ (ORCPT ); Thu, 3 Feb 2022 10:12:09 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A42FC061714 for ; Thu, 3 Feb 2022 07:12:09 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id f80-20020a1c1f53000000b0035399b8bedcso7094125wmf.0 for ; Thu, 03 Feb 2022 07:12:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rvWCTnK7RIGRGIa+VtDlo4Iv+yq/+Lu9evG31SQz/yM=; b=aW2ldL8OrNHngGrqChyY1ky0oKQbCrh9+FJ5S95lS+kjaw7Ym5fHIhG1ooDDMy2EG3 aVlN2KW7PlYjaf6Mjypd7qOZi6lqyqn1m12PkW5BNNCNRGPo1tZJIky1EuBeeb178tgN ApGjkm0vMph1dYj0p0SEWiy3DW+6pof1wBe9Mn+8PB66yFy7QDEpXwfjoF4vHpCn2bOz HaCygIYmbIQSTrQ84JGvaxCagkwh5EfXLYVO8JPvM+NH/VKVvrK/mRtFch87ogTwkrNm 2aodA6WlZqikQpgmboDZosh5pJ3d4w46nIH6iZ0zFLFV6zfqNeSNt/3xYN7r6uXQb0GT +4JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rvWCTnK7RIGRGIa+VtDlo4Iv+yq/+Lu9evG31SQz/yM=; b=BAtCw0KACLUdd9Z/FIsfuVm+1g6GHt9OnHhKTcAE9JwkOPLbZb7ODkiuNqls3D0y9r NQ+QmVZcHD/G+zbO0Fipt0ubThNsrlSMZszOdiZrElJW2oV9pf/BJzxNb3ApzW54ysa9 ZFaxQ5vimd0O8q7Nk/+JctGmHo0hsjkt0A703smCuHz4dOdewzhCl+OqhIrSWyxZ5txQ 84Geza1pm4/aN544C5m99UKPoHJfKclucOw/J4fsxZBRSMVDTmysqDvz1oeqUB8oKqSm B/hdTTsUJ3/ZArKMOwP/t7rWOmrcW6TUyGmJsrnAoRbycmypaBTzssWoMDNksJJBHpMM FNsg== X-Gm-Message-State: AOAM532Qo2UmqOXVLQWEre1XOGIuINq8ts1E8/2blScOY99uvbYhoZ8N 724N/dPk9uKqywypy9DFFKd3qQ== X-Google-Smtp-Source: ABdhPJwGqRQozSjn+lIsgMN0YR+bp/BBm28F1i1av7MLxP9PAKwUk51QlDUZ++M1LdLsc3DB92sdOw== X-Received: by 2002:a05:600c:3641:: with SMTP id y1mr10799974wmq.44.1643901128011; Thu, 03 Feb 2022 07:12:08 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6d:f804:0:28c2:5854:c832:e580]) by smtp.gmail.com with ESMTPSA id m14sm25793665wrp.4.2022.02.03.07.12.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 07:12:07 -0800 (PST) From: Usama Arif To: io-uring@vger.kernel.org, axboe@kernel.dk, asml.silence@gmail.com, linux-kernel@vger.kernel.org Cc: fam.zheng@bytedance.com, Usama Arif Subject: [PATCH 2/2] io_uring: avoid ring quiesce for IORING_REGISTER_EVENTFD_ASYNC Date: Thu, 3 Feb 2022 15:11:53 +0000 Message-Id: <20220203151153.574032-3-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220203151153.574032-1-usama.arif@bytedance.com> References: <20220203151153.574032-1-usama.arif@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is done using the RCU data structure (io_ev_fd). eventfd_async is moved from io_ring_ctx to io_ev_fd which is RCU protected hence avoiding ring quiesce which is much more expensive than an RCU lock. io_should_trigger_evfd is already under rcu_read_lock so there is no extra RCU read-side critical section needed. Signed-off-by: Usama Arif --- fs/io_uring.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index f07cfbb387a6..30ac08ad6810 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -329,6 +329,7 @@ struct io_submit_state { struct io_ev_fd { struct eventfd_ctx *cq_ev_fd; struct io_ring_ctx *ctx; + unsigned int eventfd_async: 1; struct rcu_head rcu; }; =20 @@ -341,7 +342,6 @@ struct io_ring_ctx { unsigned int flags; unsigned int compat: 1; unsigned int drain_next: 1; - unsigned int eventfd_async: 1; unsigned int restricted: 1; unsigned int off_timeout_used: 1; unsigned int drain_active: 1; @@ -1740,7 +1740,7 @@ static inline bool io_should_trigger_evfd(struct io_r= ing_ctx *ctx, struct io_ev_ return false; if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) return false; - return !ctx->eventfd_async || io_wq_current_is_worker(); + return !ev_fd->eventfd_async || io_wq_current_is_worker(); } =20 static void io_eventfd_signal(struct io_ring_ctx *ctx) @@ -9372,7 +9372,8 @@ static int __io_sqe_buffers_update(struct io_ring_ctx= *ctx, return done ? done : err; } =20 -static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) +static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg, + unsigned int eventfd_async) { struct io_ev_fd *ev_fd; __s32 __user *fds =3D arg; @@ -9399,6 +9400,7 @@ static int io_eventfd_register(struct io_ring_ctx *ct= x, void __user *arg) goto out; } ev_fd->ctx =3D ctx; + ev_fd->eventfd_async =3D eventfd_async; =20 rcu_assign_pointer(ctx->io_ev_fd, ev_fd); ret =3D 0; @@ -11014,6 +11016,7 @@ static bool io_register_op_must_quiesce(int op) case IORING_UNREGISTER_FILES: case IORING_REGISTER_FILES_UPDATE: case IORING_REGISTER_EVENTFD: + case IORING_REGISTER_EVENTFD_ASYNC: case IORING_UNREGISTER_EVENTFD: case IORING_REGISTER_PROBE: case IORING_REGISTER_PERSONALITY: @@ -11114,17 +11117,16 @@ static int __io_uring_register(struct io_ring_ctx= *ctx, unsigned opcode, ret =3D io_register_files_update(ctx, arg, nr_args); break; case IORING_REGISTER_EVENTFD: - case IORING_REGISTER_EVENTFD_ASYNC: ret =3D -EINVAL; if (nr_args !=3D 1) break; - ret =3D io_eventfd_register(ctx, arg); - if (ret) + ret =3D io_eventfd_register(ctx, arg, 0); + break; + case IORING_REGISTER_EVENTFD_ASYNC: + ret =3D -EINVAL; + if (nr_args !=3D 1) break; - if (opcode =3D=3D IORING_REGISTER_EVENTFD_ASYNC) - ctx->eventfd_async =3D 1; - else - ctx->eventfd_async =3D 0; + ret =3D io_eventfd_register(ctx, arg, 1); break; case IORING_UNREGISTER_EVENTFD: ret =3D -EINVAL; --=20 2.25.1