From nobody Mon Jun 29 18:41:55 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 5F451C4332F for ; Thu, 3 Feb 2022 23:34:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356018AbiBCXeq (ORCPT ); Thu, 3 Feb 2022 18:34:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356008AbiBCXeo (ORCPT ); Thu, 3 Feb 2022 18:34:44 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 309C3C061714 for ; Thu, 3 Feb 2022 15:34:44 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id l25so7982059wrb.13 for ; Thu, 03 Feb 2022 15:34:44 -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=m/kvsV2Fw6mBqvpDjxp3WYmDCchAjKBAAZ5SIZ3OfkM=; b=mVTIDSi/HCz5S+C4wI3cpowSR+XnVC1Iy3k/8hB7zPVs1EYiOggQF7HGdU0z5aWBmA kAoLNuQTMowykTBSGVN0yNNKEbFQyb1MwTP/MyFHMza6Jk+J8AYM059yNc3NsZuy7QF6 /TE0umdu8aM/OJlafGSO2/bh8Ig+RWXClWYEcS8w6+SOJZZ6vRXDpxBHUmvK3rhDXgrW 6dODn6ASZUImiHxAkcQHOdSVbHqtC8UoVHCCq9aluIjSNMkuX7F+bkNjHEuSLf0Ic3BB ZKyNBf/qMiCmDL0ZmXwl1Xt7k9JnI354RDENmEqfqjmKAYi1dF+7FEzJOZYiWx+wkWLY 8pDQ== 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=m/kvsV2Fw6mBqvpDjxp3WYmDCchAjKBAAZ5SIZ3OfkM=; b=rkYX7Fq6tTenuv9B95/SOJ1gsa3pEKFxSMY2P3KX49hLols0mhwpF25ZSgvK1iSDhT eAzPGVYZ8n9502jrvzgvmE4rk44kSEW9/q+xz7h5bfTdMOQmdJk/eMmj+fcIkHJwGMRL SkQdgPbWcasPEKFpYJqb+6CJBadv/54OWIKvGE78RhaEz0AeXPMr631EXoA2gNXUeSSG BNEUjRBrKBaHGHl1w0cujxHD+LV1utHUH4bubBMJBZtU4SiUj0StNTCQOtHx2JXLBdS8 9xaC9Fd8twbcjq1Bo34tViihcrHUSWV3oYNCbc2sN8/epBOWL6owlP187epk84VdGBz3 Xngg== X-Gm-Message-State: AOAM532voRairVPyECJUs6zSZU8KDvSPHZ6Pj+T0wGqsi4eJZcTO0uE5 lBDRU1mOWKbj1du1o0o8cmUBPEbnQxyDVw== X-Google-Smtp-Source: ABdhPJwCqoELBg5vuld6JA93Tu8jpMKQ+Gr+88N9P/mvn7C3jz2EpvjIKmRVJ0xclo3weA98HjTMVw== X-Received: by 2002:adf:e750:: with SMTP id c16mr179327wrn.431.1643931282795; Thu, 03 Feb 2022 15:34:42 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6d:f804:0:28c2:5854:c832:e580]) by smtp.gmail.com with ESMTPSA id j15sm148494wmq.19.2022.02.03.15.34.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 15:34:42 -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 v5 1/4] io_uring: remove trace for eventfd Date: Thu, 3 Feb 2022 23:34:36 +0000 Message-Id: <20220203233439.845408-2-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220203233439.845408-1-usama.arif@bytedance.com> References: <20220203233439.845408-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" The information on whether eventfd is registered is not very useful and would result in the tracepoint being enclosed in an rcu_readlock in a later patch that tries to avoid ring quiesce for registering eventfd. Suggested-by: Jens Axboe Signed-off-by: Usama Arif --- fs/io_uring.c | 3 +-- include/trace/events/io_uring.h | 13 +++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 2e04f718319d..21531609a9c6 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -11171,8 +11171,7 @@ SYSCALL_DEFINE4(io_uring_register, unsigned int, fd= , unsigned int, opcode, mutex_lock(&ctx->uring_lock); ret =3D __io_uring_register(ctx, opcode, arg, nr_args); mutex_unlock(&ctx->uring_lock); - trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_buf= s, - ctx->cq_ev_fd !=3D NULL, ret); + trace_io_uring_register(ctx, opcode, ctx->nr_user_files, ctx->nr_user_buf= s, ret); out_fput: fdput(f); return ret; diff --git a/include/trace/events/io_uring.h b/include/trace/events/io_urin= g.h index 7346f0164cf4..098beda7601a 100644 --- a/include/trace/events/io_uring.h +++ b/include/trace/events/io_uring.h @@ -57,10 +57,9 @@ TRACE_EVENT(io_uring_create, * @opcode: describes which operation to perform * @nr_user_files: number of registered files * @nr_user_bufs: number of registered buffers - * @cq_ev_fd: whether eventfs registered or not * @ret: return code * - * Allows to trace fixed files/buffers/eventfds, that could be registered = to + * Allows to trace fixed files/buffers, that could be registered to * avoid an overhead of getting references to them for every operation. Th= is * event, together with io_uring_file_get, can provide a full picture of h= ow * much overhead one can reduce via fixing. @@ -68,16 +67,15 @@ TRACE_EVENT(io_uring_create, TRACE_EVENT(io_uring_register, =20 TP_PROTO(void *ctx, unsigned opcode, unsigned nr_files, - unsigned nr_bufs, bool eventfd, long ret), + unsigned nr_bufs, long ret), =20 - TP_ARGS(ctx, opcode, nr_files, nr_bufs, eventfd, ret), + TP_ARGS(ctx, opcode, nr_files, nr_bufs, ret), =20 TP_STRUCT__entry ( __field( void *, ctx ) __field( unsigned, opcode ) __field( unsigned, nr_files ) __field( unsigned, nr_bufs ) - __field( bool, eventfd ) __field( long, ret ) ), =20 @@ -86,14 +84,13 @@ TRACE_EVENT(io_uring_register, __entry->opcode =3D opcode; __entry->nr_files =3D nr_files; __entry->nr_bufs =3D nr_bufs; - __entry->eventfd =3D eventfd; __entry->ret =3D ret; ), =20 TP_printk("ring %p, opcode %d, nr_user_files %d, nr_user_bufs %d, " - "eventfd %d, ret %ld", + "ret %ld", __entry->ctx, __entry->opcode, __entry->nr_files, - __entry->nr_bufs, __entry->eventfd, __entry->ret) + __entry->nr_bufs, __entry->ret) ); =20 /** --=20 2.25.1 From nobody Mon Jun 29 18:41:55 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 F3D0EC4332F for ; Thu, 3 Feb 2022 23:34:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356029AbiBCXes (ORCPT ); Thu, 3 Feb 2022 18:34:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356013AbiBCXep (ORCPT ); Thu, 3 Feb 2022 18:34:45 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40179C061714 for ; Thu, 3 Feb 2022 15:34:45 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id s18so8033985wrv.7 for ; Thu, 03 Feb 2022 15:34:45 -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=cJBmDQMGR7mb1aZCtExtihzWEy/il3b4LPSM3RsHxqc=; b=pZCHze8yFgOn1srkiVnah58AITuyWpmqL2tt1OpobYRxUBmJQtNVbOQbreFdotXQSM TtuZTOrSW1E3OjZZYSdh3ybsWr6PZef3i85g0bYcmu6TNiYRW5cjuz1rjSF6Z8ia6G2t aX24rRkyUE9WnaXVvLqBz7621ypkKdjpELDQrYcspfS0E6F47mImX9M5MhCJjaFp6hMa taxEGG5UTvQAVf2N1Q7hUvzATRwcxYSv/EDnoxyA0stvJQDGXftKUSpbuaPRBanfaEYD 8YZaTBejhytJ9Mjoid9qeBalZkxUb4pfT2cU2WWiRsRdfzHaGqlQ0ksJCUotOkpJAlP9 kAbg== 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=cJBmDQMGR7mb1aZCtExtihzWEy/il3b4LPSM3RsHxqc=; b=nD1p3gu9Spn8RhEU6rRoFfcqnDoxk1XPBZisdzPo7KPEYW4fDQVo+ebwMQf7b7Z4Ff QjvuWLyQ9eDl/D8iNLT49b87kAagHmk5Fh4TImmY/WYXl5oIPLeEaHhNtNqWIUcN962z 7TyRRBazD7z7QWOvGLkpXeHXQ/NQKvh/ruDW5y1nlLCpNibxJTTLjHrJ7SQsWTuFR1Ru niwqcM8vCxmIzF+PD960m+Xr8phEJ29RKoLJZaD9g1PF2h1lZj8YVwt/x8TzzpykkbKB JLhwyif0q2vA960nn8gflXBu1ZZ2WoskRO/DS4tea+hkCz6vO+GrzUrHp7phRHMinQoU gIOQ== X-Gm-Message-State: AOAM5316CVPKZ7TJGFjYASQTsYNBJvvSpwVjBp0l0LLhYE4+btjCn+VR LmlA+DE8xZBqCdQNPkqA8GA6PQ== X-Google-Smtp-Source: ABdhPJzLuv8/iIDDezS6//K3k0AeqLJGhgm6hT9AePbpu3pvUCJHnXevFGjdWR7vhYOvyle4x0xzKQ== X-Received: by 2002:a05:6000:1c1c:: with SMTP id ba28mr225221wrb.392.1643931283915; Thu, 03 Feb 2022 15:34:43 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6d:f804:0:28c2:5854:c832:e580]) by smtp.gmail.com with ESMTPSA id j15sm148494wmq.19.2022.02.03.15.34.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 15:34:43 -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 v5 2/4] io_uring: avoid ring quiesce while registering/unregistering eventfd Date: Thu, 3 Feb 2022 23:34:37 +0000 Message-Id: <20220203233439.845408-3-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220203233439.845408-1-usama.arif@bytedance.com> References: <20220203233439.845408-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 | 116 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 23 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 21531609a9c6..51602bddb9a8 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -326,6 +326,13 @@ 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; + bool unregistering; +}; + struct io_ring_ctx { /* const or read-mostly hot data */ struct { @@ -399,7 +406,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 +1456,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,13 +1735,32 @@ 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 void io_eventfd_signal(struct io_ring_ctx *ctx) { - if (likely(!ctx->cq_ev_fd)) - return false; + struct io_ev_fd *ev_fd; + + /* Return quickly if ctx->io_ev_fd doesn't exist */ + if (likely(!rcu_dereference_raw(ctx->io_ev_fd))) + return; + + rcu_read_lock(); + /* rcu_dereference ctx->io_ev_fd once and use it for both for checking an= d eventfd_signal */ + ev_fd =3D rcu_dereference(ctx->io_ev_fd); + + /* + * Check again if ev_fd exists incase an io_eventfd_unregister call compl= eted between + * the NULL check of ctx->io_ev_fd at the start of the function and rcu_r= ead_lock. + */ + if (unlikely(!ev_fd)) + goto out; if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) - return false; - return !ctx->eventfd_async || io_wq_current_is_worker(); + goto out; + + if (!ctx->eventfd_async || io_wq_current_is_worker()) + eventfd_signal(ev_fd->cq_ev_fd, 1); + +out: + rcu_read_unlock(); } =20 /* @@ -1751,8 +1779,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 +1791,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,34 +9379,76 @@ 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; + ev_fd =3D rcu_dereference_protected(ctx->io_ev_fd, lockdep_is_held(&ctx->= ev_fd_lock)); + if (ev_fd) { + /* + * If ev_fd exists, there are 2 possibilities: + * - The rcu_callback to io_eventfd_put hasn't finished while unregister= ing + * (hence ev_fd->unregistering is true) and io_eventfd_register + * can continue and overwrite ctx->io_ev_fd with the new eventfd. + * - Or io_eventfd_register has been called on an io_uring that has + * already registered a valid eventfd in which case return -EBUSY. + */ + if(!ev_fd->unregistering) + 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; + ev_fd->unregistering =3D false; =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; + struct io_ev_fd *ev_fd; + + 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) { + ev_fd->unregistering =3D true; + call_rcu(&ev_fd->rcu, io_eventfd_put); + mutex_unlock(&ctx->ev_fd_lock); return 0; } =20 + mutex_unlock(&ctx->ev_fd_lock); return -ENXIO; } =20 @@ -10960,6 +11028,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: --=20 2.25.1 From nobody Mon Jun 29 18:41:55 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 3031DC433F5 for ; Thu, 3 Feb 2022 23:34:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356054AbiBCXew (ORCPT ); Thu, 3 Feb 2022 18:34:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356021AbiBCXeq (ORCPT ); Thu, 3 Feb 2022 18:34:46 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E4CAC06173B for ; Thu, 3 Feb 2022 15:34:46 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id g18so7690579wrb.6 for ; Thu, 03 Feb 2022 15:34:46 -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=H/qApFabyCqynxzYsW+8dYD6QrjWQhPeK69O6kd4Euc=; b=6s9JhcKBntjBkKFlGZfvX6NL1mxXzuU5vbBiVSaI9+eNj6JbcfL4wbiHwme+u88Yn6 7b6goefqeUoqW917umdzcCP1sZUr2feWA36MqnrrLwtGbj76zU/P8WaB2wzY/hhrVXSC i5uFEyyvxFraNoC09AgegOVXhamcvWME8EgZQtfghNTgJOEKax+VzEaVds7OB1AWF1Yo tR3sNc++6aVtAArxqIU23GVYpS5wxWOzhZEJT9NkZMOhg/aXDSHuvAR2Zr7zIDMb7kCK grWgzRvkt4OmkLkKmMwZ5AErndsqfHQznFEzxf1QmJ5vs9KYDLJRMiWnmNV6vptd94EL WZJA== 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=H/qApFabyCqynxzYsW+8dYD6QrjWQhPeK69O6kd4Euc=; b=b34+ytygSmXC3JPoN4cEKBerioMwdm43IggxQkUbIGOF0mA831/gGQxgJ0N2BoFWg5 TPppOAqFfESHV2coCqJtlwz0u3EhgHWZ77+9C4uGx2d8EPhug/fyBt57Og/T1+CaSSXZ 0lXDxz6nJw6C/bIgPYPPXp9UbulPwVSDnJRv9hQXfQEsqF7hFzoLBOzeJ4NRgWKC8tDW xHUx7t08vopBV+qoxF1MaqSz4wyFGotuYu5RlUnC2stn2iukRlpEKRoe5yNSjJr5Ma5e 8Y72zsFzhsSAZhoNZF7vvEzGI5fDoogsYUX2CrtFONZJFWd8j9kYyEB5yTMMiRSoY7Qy EPIA== X-Gm-Message-State: AOAM5315v3wvQNgV1+SC15wui05Rlnu67uxMboOg1bTsTnZvWa1iAgFz FP3hRD/UOxbd0P5sae7McsRzKA== X-Google-Smtp-Source: ABdhPJx9B33DIul/jfnrSTzI4eCfy8T6j/MuusQGZtwW2rmZBbRbowZ88NOkXNJHe3FO4BqUYybjkg== X-Received: by 2002:a5d:55c3:: with SMTP id i3mr220314wrw.250.1643931284877; Thu, 03 Feb 2022 15:34:44 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6d:f804:0:28c2:5854:c832:e580]) by smtp.gmail.com with ESMTPSA id j15sm148494wmq.19.2022.02.03.15.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 15:34:44 -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 v5 3/4] io_uring: avoid ring quiesce for IORING_REGISTER_EVENTFD_ASYNC Date: Thu, 3 Feb 2022 23:34:38 +0000 Message-Id: <20220203233439.845408-4-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220203233439.845408-1-usama.arif@bytedance.com> References: <20220203233439.845408-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 51602bddb9a8..5ae51ea12f0f 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; bool unregistering; }; @@ -342,7 +343,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; @@ -1756,7 +1756,7 @@ static void io_eventfd_signal(struct io_ring_ctx *ctx) if (READ_ONCE(ctx->rings->cq_flags) & IORING_CQ_EVENTFD_DISABLED) goto out; =20 - if (!ctx->eventfd_async || io_wq_current_is_worker()) + if (!ev_fd->eventfd_async || io_wq_current_is_worker()) eventfd_signal(ev_fd->cq_ev_fd, 1); =20 out: @@ -9377,7 +9377,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; @@ -9416,6 +9417,7 @@ static int io_eventfd_register(struct io_ring_ctx *ct= x, void __user *arg) } ev_fd->ctx =3D ctx; ev_fd->unregistering =3D false; + ev_fd->eventfd_async =3D eventfd_async; =20 rcu_assign_pointer(ctx->io_ev_fd, ev_fd); ret =3D 0; @@ -11029,6 +11031,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: @@ -11129,17 +11132,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 From nobody Mon Jun 29 18:41:55 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 3E8BCC433EF for ; Thu, 3 Feb 2022 23:34:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238979AbiBCXe5 (ORCPT ); Thu, 3 Feb 2022 18:34:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356035AbiBCXet (ORCPT ); Thu, 3 Feb 2022 18:34:49 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98DCBC06173D for ; Thu, 3 Feb 2022 15:34:49 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id n8so3271626wmk.3 for ; Thu, 03 Feb 2022 15:34:49 -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=dtu5i1R4hfxOELb/nXJNtBsjoPa0tCK5wXG8XtHwg9o=; b=WDQ7UXWlnbH4SDADWxq915GaFbCRtyy2anBIIXO7sRRS7bvgSaKsWpcDjIYdGqV4J3 UZuo3ZB197owh0sNexELBc/NWhDYJDNG3opgDlruCLKi7p/r/9C2CE8+JsiEtmrvmpL/ jQ0jF95rZr2qfaNwV2rqLrLEGzm2UqbqdmFCKZEd9aHWh7qWnOTRxJRmCHPpHGhHdTyr EHedeO8dS8XtsoPvzDypDEII66xknPS5yXx6h7IR+JLWZrFNVKyFJG3YNsOoFPhCv06Y RvLBuhV3uhHeenOnsqKSsbvAKrvGXuH/5eIgk/p1QJ1+u3gm6K2lBjLxH5REOX0KXtz+ L5zg== 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=dtu5i1R4hfxOELb/nXJNtBsjoPa0tCK5wXG8XtHwg9o=; b=x7M+krp252y8/y9lopzZsDv+vjy2MnQ6P/mqN1t524u1PW1EOG3JS5jAylVxy3MOPL h3EuD7xtEgGvYajRSfMk8yBW304B6BATBQXujDfVwtFveGBqMcd3JaHfdq3b0MpSlAlv hsxtEAsihLD/XB3Bd0m8hCMw8+KJJqzYS0IrPFypgoDWfv7NOb1RKNIohXP+Ki73C5YW t+LPYEgKTsgmZnLUEpJUeT8EvTFKZBPcyCFFshg4eC1sck6fAnzVHXvGMPjueiQKrOGY 7KgltAupKSoakXMNES1fal/vJQITCtgJVSuyFcsdi//i0s2gAMbzrAapXzerua5+HUxZ Pm0A== X-Gm-Message-State: AOAM530+PSVzaDqm+AJUP2sHe5cCQP/ROeXL7Mw7HLlpPCqRDEVEGWUT NOa4IPLCT48GaOL5L1LifpdDWA== X-Google-Smtp-Source: ABdhPJwIfrb4/YByPVeNa9Oc0Ulr8ZQR5uQiDJfVsZBEIskH9buWEoEZaiAHxNGaUFm1tKPgvGLCBw== X-Received: by 2002:a1c:2b06:: with SMTP id r6mr62849wmr.4.1643931288274; Thu, 03 Feb 2022 15:34:48 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6d:f804:0:28c2:5854:c832:e580]) by smtp.gmail.com with ESMTPSA id j15sm148494wmq.19.2022.02.03.15.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 15:34:47 -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 v5 4/4] io_uring: remove ring quiesce for io_uring_register Date: Thu, 3 Feb 2022 23:34:39 +0000 Message-Id: <20220203233439.845408-5-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220203233439.845408-1-usama.arif@bytedance.com> References: <20220203233439.845408-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" Ring quiesce is currently only used for 2 opcodes IORING_REGISTER_ENABLE_RINGS and IORING_REGISTER_RESTRICTIONS. IORING_SETUP_R_DISABLED prevents submitting requests and so there will be no requests until IORING_REGISTER_ENABLE_RINGS is called. And IORING_REGISTER_RESTRICTIONS works only before IORING_REGISTER_ENABLE_RINGS is called. Hence ring quiesce is not needed for these opcodes and therefore io_uring_register. Signed-off-by: Usama Arif --- fs/io_uring.c | 69 --------------------------------------------------- 1 file changed, 69 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 5ae51ea12f0f..89e4dd7e8995 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -11022,64 +11022,6 @@ static __cold int io_register_iowq_max_workers(str= uct io_ring_ctx *ctx, return ret; } =20 -static bool io_register_op_must_quiesce(int op) -{ - switch (op) { - case IORING_REGISTER_BUFFERS: - case IORING_UNREGISTER_BUFFERS: - case IORING_REGISTER_FILES: - 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: - case IORING_UNREGISTER_PERSONALITY: - case IORING_REGISTER_FILES2: - case IORING_REGISTER_FILES_UPDATE2: - case IORING_REGISTER_BUFFERS2: - case IORING_REGISTER_BUFFERS_UPDATE: - case IORING_REGISTER_IOWQ_AFF: - case IORING_UNREGISTER_IOWQ_AFF: - case IORING_REGISTER_IOWQ_MAX_WORKERS: - return false; - default: - return true; - } -} - -static __cold int io_ctx_quiesce(struct io_ring_ctx *ctx) -{ - long ret; - - percpu_ref_kill(&ctx->refs); - - /* - * Drop uring mutex before waiting for references to exit. If another - * thread is currently inside io_uring_enter() it might need to grab the - * uring_lock to make progress. If we hold it here across the drain - * wait, then we can deadlock. It's safe to drop the mutex here, since - * no new references will come in after we've killed the percpu ref. - */ - mutex_unlock(&ctx->uring_lock); - do { - ret =3D wait_for_completion_interruptible_timeout(&ctx->ref_comp, HZ); - if (ret) { - ret =3D min(0L, ret); - break; - } - - ret =3D io_run_task_work_sig(); - io_req_caches_free(ctx); - } while (ret >=3D 0); - mutex_lock(&ctx->uring_lock); - - if (ret) - io_refs_resurrect(&ctx->refs, &ctx->ref_comp); - return ret; -} - static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, void __user *arg, unsigned nr_args) __releases(ctx->uring_lock) @@ -11103,12 +11045,6 @@ static int __io_uring_register(struct io_ring_ctx = *ctx, unsigned opcode, return -EACCES; } =20 - if (io_register_op_must_quiesce(opcode)) { - ret =3D io_ctx_quiesce(ctx); - if (ret) - return ret; - } - switch (opcode) { case IORING_REGISTER_BUFFERS: ret =3D io_sqe_buffers_register(ctx, arg, nr_args, NULL); @@ -11213,11 +11149,6 @@ static int __io_uring_register(struct io_ring_ctx = *ctx, unsigned opcode, break; } =20 - if (io_register_op_must_quiesce(opcode)) { - /* bring the ctx back to life */ - percpu_ref_reinit(&ctx->refs); - reinit_completion(&ctx->ref_comp); - } return ret; } =20 --=20 2.25.1