From: Usama Arif <[email protected]>
To: Jens Axboe <[email protected]>,
[email protected], [email protected],
[email protected]
Cc: [email protected]
Subject: Re: [External] Re: [PATCH v3 2/3] io_uring: avoid ring quiesce while registering/unregistering eventfd
Date: Thu, 3 Feb 2022 18:26:41 +0000 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
On 03/02/2022 17:56, Jens Axboe wrote:
> On 2/3/22 10:41 AM, Usama Arif wrote:
>> @@ -1726,13 +1732,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();
>> }
>
> Like Pavel pointed out, we still need the fast path (of not having an
> event fd registered at all) to just do the cheap check and not need rcu
> lock/unlock. Outside of that, I think this looks fine.
>
Hmm, maybe i didn't understand you and Pavel correctly. Are you
suggesting to do the below diff over patch 3? I dont think that would be
correct, as it is possible that just after checking if ctx->io_ev_fd is
present unregister can be called by another thread and set ctx->io_ev_fd
to NULL that would cause a NULL pointer exception later? In the current
patch, the check of whether ev_fd exists happens as the first thing
after rcu_read_lock and the rcu_read_lock are extremely cheap i believe.
diff --git a/fs/io_uring.c b/fs/io_uring.c
index 25ed86533910..0cf282fba14d 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -1736,12 +1736,13 @@ static void io_eventfd_signal(struct io_ring_ctx
*ctx)
{
struct io_ev_fd *ev_fd;
+ if (likely(!ctx->io_ev_fd))
+ return;
+
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)
goto out;
>> 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) {
>> + ret = -ENXIO;
>> + goto out;
>> }
>> + synchronize_rcu();
>> + eventfd_ctx_put(ev_fd->cq_ev_fd);
>> + kfree(ev_fd);
>> + rcu_assign_pointer(ctx->io_ev_fd, NULL);
>> + ret = 0;
>>
>> - return -ENXIO;
>> +out:
>> + mutex_unlock(&ctx->ev_fd_lock);
>> + return ret;
>> }
>
> synchronize_rcu() can take a long time, and I think this is in the wrong
> spot. It should be on the register side, IFF we need to expedite the
> completion of a previous event fd unregistration. If we do it that way,
> at least it'll only happen if it's necessary. What do you think?
>
How about the approach in v4? so switching back to call_rcu as in v2 and
if ctx->io_ev_fd is NULL then we call rcu_barrier to make sure all rcu
callbacks are finished and check for NULL again.
Thanks!
Usama
next prev parent reply other threads:[~2022-02-03 18:26 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-03 17:41 [PATCH v3 0/3] io_uring: avoid ring quiesce in io_uring_register for eventfd opcodes Usama Arif
2022-02-03 17:41 ` [PATCH v3 1/3] io_uring: remove trace for eventfd Usama Arif
2022-02-03 17:41 ` [PATCH v3 2/3] io_uring: avoid ring quiesce while registering/unregistering eventfd Usama Arif
2022-02-03 17:56 ` Jens Axboe
2022-02-03 18:26 ` Usama Arif [this message]
2022-02-03 18:29 ` [External] " Jens Axboe
2022-02-03 19:00 ` Pavel Begunkov
2022-02-03 19:06 ` Jens Axboe
2022-02-03 19:43 ` Pavel Begunkov
2022-02-03 22:18 ` Jens Axboe
2022-02-03 19:54 ` Usama Arif
2022-02-03 21:47 ` Pavel Begunkov
2022-02-03 22:16 ` Jens Axboe
2022-02-03 23:21 ` Pavel Begunkov
2022-02-03 22:02 ` Pavel Begunkov
2022-02-03 17:41 ` [PATCH v3 3/3] io_uring: avoid ring quiesce for IORING_REGISTER_EVENTFD_ASYNC Usama Arif
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox