Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2717791pxb; Thu, 3 Feb 2022 12:39:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJzWEF+VFtivZbA8It3D3HxgbWCzgAU8qQ0KIt2opLD04RmqyukCKM4wJnLls62bOVpGQvY2 X-Received: by 2002:a17:902:6808:: with SMTP id h8mr36331837plk.23.1643920746336; Thu, 03 Feb 2022 12:39:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643920746; cv=none; d=google.com; s=arc-20160816; b=i8u8FFd4fCBLW+LykcKWoJwV4GK+0ZMFvY5HpZ2hpGIQc4WIyLRg3/1ReBz6E9f/O/ YpBhkxoV0EV1sQk7Wy+Gpa7ikUcdSw5SmhLGwRN4j3FUu8zQBge7e1gdL+9UWYYlFbQN l/pjhOqywY3FCLFoDqivboqQws2Ifyu7Algmw002kYtBBQV9sfrbHF7+nWaWWON7MIer +vjGf7QgiWZUX4pYxbyeK6LxF6FjjrDUW1c3hFlKdE2qzWU0vqriaPMohWWgI4TbTLHM zSyvYZZfJZobP0StUpmz+5VbEqcCf1R599Q/U8R9CdNv2lOACuvlXkfaEo5VH4eNXWTa h/fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=EVCTgqaqhd6XkJVbAQ3XRZlsjlSz2NkMW74pu+sTtmI=; b=m/S0GCKGUga8Ts1wETY9H4SEGITkqmqtpr2LfXRQ9plXy9uPH1cMbQ/U2reariCcIn qRCeFg18YjBg13Jjhi6wmQ7Fn5xwIIr999iN2cycQPBKMcAyLexraeNs8hpxGMOk8FXt H2u+iGMN/NoRxi+Z7YBaMo2GseslDrBxArLoyYjX6DgsWyx9pKox3dkDS2UEBLQ8uJty FTcYHXUwXx2F8TMHf5bhgmhwRG1PtRmskDQfe30eY8TiM4UzwecAWy0t3Kg//Vud2uIa Rq0yJYDDadgivdcmWKhYhjJCo8H8boKK6KeiaA5HUPhOqrXMsx846jZoe6s8g9rmOvvj AxhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=7aExreDu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bv18si8806367pjb.164.2022.02.03.12.38.55; Thu, 03 Feb 2022 12:39:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=7aExreDu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353490AbiBCSYx (ORCPT + 99 others); Thu, 3 Feb 2022 13:24:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353453AbiBCSYs (ORCPT ); Thu, 3 Feb 2022 13:24:48 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5A874C061714 for ; Thu, 3 Feb 2022 10:24:47 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id k18so6673606wrg.11 for ; Thu, 03 Feb 2022 10:24:47 -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=EVCTgqaqhd6XkJVbAQ3XRZlsjlSz2NkMW74pu+sTtmI=; b=7aExreDuZWnTRLTccRIxQ+K2jnuPv8F74gCaaRZFyGE26nVLIlAKi0V5AvS0SUFPXP JOpT/y7dROZZyiVCW4Y8dXS98V7lxyXnLybwaYPf9AUjV99k/XUTR8QrawqpAei5alCj QY81EBEn+6nZXjS3L1lzWND0FHtQU0DbWQSisS2g6jOu2vVA/xw9dw93Lq1K6Z4m8hok RneQnKa8NAkYW5F6x/RflLj2w48/vlu2ZGclt9p8gN9KIQF1WR6kuml97++DVGhWBK6X cdA1pGveuwWMt3ZQL/8aM/aRUhV8pY0EqKNDEiNNzAoeEi/qLOcpRzectDkudI9ELue9 ov5g== 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=EVCTgqaqhd6XkJVbAQ3XRZlsjlSz2NkMW74pu+sTtmI=; b=1KNQb4Nq9Cig3xH/MjGFuyQL5nH63PVos1udC59q0EqnRPiRDlR0v2fgtgdK2WDSP+ OGur9fVyqL9Ra3cwLEMFxny1wt0oSOojWKtxiZndohF9jKn7BKkRwXMzbXAd/Ok00Vlq iCAISl7Bw4gtuKTUt+428SdaZBBgAxySCV7VoAejvu287GXmQtjr1CjkcZs+Rr/4o6Zk lypx0ZL+NFXUtxmdPnBGBtdyspwf8hSF8y4gGAkTAe2wYLq7bzRh4Ipv2pLDw5N90sgq I4yUGfb2chXO7Y36pB+GdxSBtkkHFUsn3G4nwOaMjRYdt+plOBFUvxlleHmluSeC7Gb/ gyNw== X-Gm-Message-State: AOAM53362YxRX14P7//HS17kk2cOri+qUe/XzrVFAuxmeOZWczinISWk xQqKKHQrii99alrV20VDQ3mz43K4aG2Q4g== X-Received: by 2002:adf:ffd2:: with SMTP id x18mr29643939wrs.672.1643912685972; Thu, 03 Feb 2022 10:24:45 -0800 (PST) Received: from usaari01.cust.communityfibre.co.uk ([2a02:6b6d:f804:0:28c2:5854:c832:e580]) by smtp.gmail.com with ESMTPSA id h18sm3540056wro.9.2022.02.03.10.24.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Feb 2022 10:24:45 -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 v4 2/3] io_uring: avoid ring quiesce while registering/unregistering eventfd Date: Thu, 3 Feb 2022 18:24:40 +0000 Message-Id: <20220203182441.692354-3-usama.arif@bytedance.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220203182441.692354-1-usama.arif@bytedance.com> References: <20220203182441.692354-1-usama.arif@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 104 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 79 insertions(+), 25 deletions(-) diff --git a/fs/io_uring.c b/fs/io_uring.c index 21531609a9c6..7a8f4ac7a785 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -326,6 +326,12 @@ struct io_submit_state { struct blk_plug plug; }; +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(struct 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 +1734,24 @@ static inline struct io_uring_cqe *io_get_cqe(struct io_ring_ctx *ctx) return &rings->cqes[tail & mask]; } -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; + + rcu_read_lock(); + /* rcu_dereference ctx->io_ev_fd once and use it for both for checking and eventfd_signal */ + ev_fd = rcu_dereference(ctx->io_ev_fd); + + if (likely(!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(); } /* @@ -1751,8 +1770,7 @@ static void io_cqring_ev_posted(struct io_ring_ctx *ctx) */ 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); } static void io_cqring_ev_posted_iopoll(struct io_ring_ctx *ctx) @@ -1764,8 +1782,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); } /* Returns true if there are no backlogged entries after the flush */ @@ -9353,35 +9370,70 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *ctx, static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) { + struct io_ev_fd *ev_fd; __s32 __user *fds = arg; - int fd; + int fd, ret; - if (ctx->cq_ev_fd) - return -EBUSY; + mutex_lock(&ctx->ev_fd_lock); + ret = -EBUSY; + if (rcu_dereference_protected(ctx->io_ev_fd, lockdep_is_held(&ctx->ev_fd_lock))) { + rcu_barrier(); + if(rcu_dereference_protected(ctx->io_ev_fd, lockdep_is_held(&ctx->ev_fd_lock))) + goto out; + } + ret = -EFAULT; if (copy_from_user(&fd, fds, sizeof(*fds))) - return -EFAULT; + goto out; - ctx->cq_ev_fd = eventfd_ctx_fdget(fd); - if (IS_ERR(ctx->cq_ev_fd)) { - int ret = PTR_ERR(ctx->cq_ev_fd); + ret = -ENOMEM; + ev_fd = kmalloc(sizeof(*ev_fd), GFP_KERNEL); + if (!ev_fd) + goto out; - ctx->cq_ev_fd = NULL; - return ret; + ev_fd->cq_ev_fd = eventfd_ctx_fdget(fd); + if (IS_ERR(ev_fd->cq_ev_fd)) { + ret = PTR_ERR(ev_fd->cq_ev_fd); + kfree(ev_fd); + goto out; } + ev_fd->ctx = ctx; - return 0; + rcu_assign_pointer(ctx->io_ev_fd, ev_fd); + ret = 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 = container_of(rcu, struct io_ev_fd, rcu); + struct io_ring_ctx *ctx = ev_fd->ctx; + + eventfd_ctx_put(ev_fd->cq_ev_fd); + kfree(ev_fd); + rcu_assign_pointer(ctx->io_ev_fd, NULL); } 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 = NULL; - return 0; + struct io_ev_fd *ev_fd; + int ret; + + mutex_lock(&ctx->ev_fd_lock); + ev_fd = 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 = 0; + goto out; } + ret = -ENXIO; - return -ENXIO; +out: + mutex_unlock(&ctx->ev_fd_lock); + return ret; } static void io_destroy_buffers(struct io_ring_ctx *ctx) @@ -10960,6 +11012,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: -- 2.25.1