public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH 1/2] io_uring: wait potential ->release() on resurrect
  2021-02-20  1:39 [PATCH 0/2] random fixes Pavel Begunkov
@ 2021-02-20  1:39 ` Pavel Begunkov
  2021-02-20  3:40   ` Jens Axboe
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Begunkov @ 2021-02-20  1:39 UTC (permalink / raw)
  To: Jens Axboe, io-uring; +Cc: stable

There is a short window where percpu_refs are already turned zero, but
we try to do resurrect(). Play nicer and wait for all users to leave RCU
section.

Cc: <[email protected]> # 5.5+
Signed-off-by: Pavel Begunkov <[email protected]>
---
 fs/io_uring.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index f3af499b12a9..ce5fccf00367 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -7351,6 +7351,7 @@ static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
 			break;
 
 		percpu_ref_resurrect(&data->refs);
+		synchronize_rcu();
 		io_sqe_rsrc_set_node(ctx, data, backup_node);
 		reinit_completion(&data->done);
 		mutex_unlock(&ctx->uring_lock);
@@ -10089,6 +10090,7 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
 
 		if (ret) {
 			percpu_ref_resurrect(&ctx->refs);
+			synchronize_rcu();
 			goto out_quiesce;
 		}
 	}
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] io_uring: wait potential ->release() on resurrect
  2021-02-20  1:39 ` [PATCH 1/2] io_uring: wait potential ->release() on resurrect Pavel Begunkov
@ 2021-02-20  3:40   ` Jens Axboe
  2021-02-20  3:47     ` Pavel Begunkov
  0 siblings, 1 reply; 8+ messages in thread
From: Jens Axboe @ 2021-02-20  3:40 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring; +Cc: stable

On 2/19/21 6:39 PM, Pavel Begunkov wrote:
> There is a short window where percpu_refs are already turned zero, but
> we try to do resurrect(). Play nicer and wait for all users to leave RCU
> section.

We need to do something better than synchronize_rcu() here, that can
take a long time on a loaded box. I'll try and think about this one.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] io_uring: wait potential ->release() on resurrect
  2021-02-20  3:40   ` Jens Axboe
@ 2021-02-20  3:47     ` Pavel Begunkov
  2021-02-20  3:53       ` Jens Axboe
  0 siblings, 1 reply; 8+ messages in thread
From: Pavel Begunkov @ 2021-02-20  3:47 UTC (permalink / raw)
  To: Jens Axboe, io-uring; +Cc: stable

On 20/02/2021 03:40, Jens Axboe wrote:
> On 2/19/21 6:39 PM, Pavel Begunkov wrote:
>> There is a short window where percpu_refs are already turned zero, but
>> we try to do resurrect(). Play nicer and wait for all users to leave RCU
>> section.
> 
> We need to do something better than synchronize_rcu() here, that can
> take a long time on a loaded box. I'll try and think about this one.

It only happens when it can't be drained and there are task_works or
signals. I have another patch, doing it via tryget, but it's uglier and
I'd rather prefer synchronize_rcu for stable.

Want me to send it tomorrow (on top or not)?

-- 
Pavel Begunkov

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/2] io_uring: wait potential ->release() on resurrect
  2021-02-20  3:47     ` Pavel Begunkov
@ 2021-02-20  3:53       ` Jens Axboe
  0 siblings, 0 replies; 8+ messages in thread
From: Jens Axboe @ 2021-02-20  3:53 UTC (permalink / raw)
  To: Pavel Begunkov, io-uring; +Cc: stable

On 2/19/21 8:47 PM, Pavel Begunkov wrote:
> On 20/02/2021 03:40, Jens Axboe wrote:
>> On 2/19/21 6:39 PM, Pavel Begunkov wrote:
>>> There is a short window where percpu_refs are already turned zero, but
>>> we try to do resurrect(). Play nicer and wait for all users to leave RCU
>>> section.
>>
>> We need to do something better than synchronize_rcu() here, that can
>> take a long time on a loaded box. I'll try and think about this one.
> 
> It only happens when it can't be drained and there are task_works or
> signals. I have another patch, doing it via tryget, but it's uglier and
> I'd rather prefer synchronize_rcu for stable.

Right, but the task_work coming in may not be unlikely. So it's not
strictly an error path.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 0/2] resurrect
@ 2021-02-20 17:21 Pavel Begunkov
  2021-02-20 17:21 ` [PATCH 1/2] io_uring: wait potential ->release() on resurrect Pavel Begunkov
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Pavel Begunkov @ 2021-02-20 17:21 UTC (permalink / raw)
  To: Jens Axboe, io-uring

Combined approach with tryget, fallbacking to trying completion and
doing synchronize_rcu, where the last one is very unlikely to happen
and limited by one such per register syscall.

I expect 1/2 to cleanly apply for stable, 2/2 needs to be backported
by hand.

Pavel Begunkov (2):
  io_uring: wait potential ->release() on resurrect
  io_uring: wait for ->release() on rsrc resurrect

 fs/io_uring.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

-- 
2.24.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/2] io_uring: wait potential ->release() on resurrect
  2021-02-20 17:21 [PATCH 0/2] resurrect Pavel Begunkov
@ 2021-02-20 17:21 ` Pavel Begunkov
  2021-02-20 17:21 ` [PATCH 2/2] io_uring: wait for ->release() on rsrc resurrect Pavel Begunkov
  2021-02-20 17:44 ` [PATCH 0/2] resurrect Pavel Begunkov
  2 siblings, 0 replies; 8+ messages in thread
From: Pavel Begunkov @ 2021-02-20 17:21 UTC (permalink / raw)
  To: Jens Axboe, io-uring; +Cc: stable

There is a short window where percpu_refs are already turned zero, but
we try to do resurrect(). Play nicer and wait for ->release() to happen
in this case and proceed as everything is ok. One little downside is
that we can ignore signal_pending() on a rare occasion, but someone
else should check for it later if needed.

Cc: <[email protected]> # 5.5+
Signed-off-by: Pavel Begunkov <[email protected]>
---
 fs/io_uring.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index f2fdebaf28fe..6ea4633e5ed5 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -1104,6 +1104,21 @@ static inline void io_set_resource_node(struct io_kiocb *req)
 	}
 }
 
+static bool io_refs_resurrect(struct percpu_ref *ref, struct completion *compl)
+{
+	if (!percpu_ref_tryget(ref)) {
+		/* already at zero, wait for ->release() */
+		if (!try_wait_for_completion(compl))
+			synchronize_rcu();
+		return false;
+	}
+
+	percpu_ref_resurrect(ref);
+	reinit_completion(compl);
+	percpu_ref_put(ref);
+	return true;
+}
+
 static bool io_match_task(struct io_kiocb *head,
 			  struct task_struct *task,
 			  struct files_struct *files)
@@ -10094,10 +10109,8 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
 
 		mutex_lock(&ctx->uring_lock);
 
-		if (ret) {
-			percpu_ref_resurrect(&ctx->refs);
-			goto out_quiesce;
-		}
+		if (ret && io_refs_resurrect(&ctx->refs, &ctx->ref_comp))
+			return ret;
 	}
 
 	if (ctx->restricted) {
@@ -10189,7 +10202,6 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,
 	if (io_register_op_must_quiesce(opcode)) {
 		/* bring the ctx back to life */
 		percpu_ref_reinit(&ctx->refs);
-out_quiesce:
 		reinit_completion(&ctx->ref_comp);
 	}
 	return ret;
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/2] io_uring: wait for ->release() on rsrc resurrect
  2021-02-20 17:21 [PATCH 0/2] resurrect Pavel Begunkov
  2021-02-20 17:21 ` [PATCH 1/2] io_uring: wait potential ->release() on resurrect Pavel Begunkov
@ 2021-02-20 17:21 ` Pavel Begunkov
  2021-02-20 17:44 ` [PATCH 0/2] resurrect Pavel Begunkov
  2 siblings, 0 replies; 8+ messages in thread
From: Pavel Begunkov @ 2021-02-20 17:21 UTC (permalink / raw)
  To: Jens Axboe, io-uring; +Cc: stable

As with ctx refs, on resurrect wait for potentially concurrently running
->release().

Cc: <[email protected]> # 5.10+
Signed-off-by: Pavel Begunkov <[email protected]>
---
 fs/io_uring.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 6ea4633e5ed5..c08d32523f79 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -7368,13 +7368,11 @@ static int io_rsrc_ref_quiesce(struct fixed_rsrc_data *data,
 		flush_delayed_work(&ctx->rsrc_put_work);
 
 		ret = wait_for_completion_interruptible(&data->done);
-		if (!ret)
+		if (!ret || !io_refs_resurrect(&data->refs, &data->done))
 			break;
 
-		percpu_ref_resurrect(&data->refs);
 		io_sqe_rsrc_set_node(ctx, data, backup_node);
 		backup_node = NULL;
-		reinit_completion(&data->done);
 		mutex_unlock(&ctx->uring_lock);
 		ret = io_run_task_work_sig();
 		mutex_lock(&ctx->uring_lock);
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 0/2] resurrect
  2021-02-20 17:21 [PATCH 0/2] resurrect Pavel Begunkov
  2021-02-20 17:21 ` [PATCH 1/2] io_uring: wait potential ->release() on resurrect Pavel Begunkov
  2021-02-20 17:21 ` [PATCH 2/2] io_uring: wait for ->release() on rsrc resurrect Pavel Begunkov
@ 2021-02-20 17:44 ` Pavel Begunkov
  2 siblings, 0 replies; 8+ messages in thread
From: Pavel Begunkov @ 2021-02-20 17:44 UTC (permalink / raw)
  To: Jens Axboe, io-uring

On 20/02/2021 17:21, Pavel Begunkov wrote:
> Combined approach with tryget, fallbacking to trying completion and
> doing synchronize_rcu, where the last one is very unlikely to happen
> and limited by one such per register syscall.
> 
> I expect 1/2 to cleanly apply for stable, 2/2 needs to be backported
> by hand.

nevermind this, need to rebase and resend

> 
> Pavel Begunkov (2):
>   io_uring: wait potential ->release() on resurrect
>   io_uring: wait for ->release() on rsrc resurrect
> 
>  fs/io_uring.c | 26 ++++++++++++++++++--------
>  1 file changed, 18 insertions(+), 8 deletions(-)
> 

-- 
Pavel Begunkov

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-02-20 17:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-02-20 17:21 [PATCH 0/2] resurrect Pavel Begunkov
2021-02-20 17:21 ` [PATCH 1/2] io_uring: wait potential ->release() on resurrect Pavel Begunkov
2021-02-20 17:21 ` [PATCH 2/2] io_uring: wait for ->release() on rsrc resurrect Pavel Begunkov
2021-02-20 17:44 ` [PATCH 0/2] resurrect Pavel Begunkov
  -- strict thread matches above, loose matches on Subject: below --
2021-02-20  1:39 [PATCH 0/2] random fixes Pavel Begunkov
2021-02-20  1:39 ` [PATCH 1/2] io_uring: wait potential ->release() on resurrect Pavel Begunkov
2021-02-20  3:40   ` Jens Axboe
2021-02-20  3:47     ` Pavel Begunkov
2021-02-20  3:53       ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox