Skip to contentSkip to conversation

io_uring: fix lacking of protection for compl_nr

PATCH Applied Hao Xu 2 participants 2 messages Aug 20, 2021

Subsystems (2)
lore
Share & tools
lore

Hao Xu

author
#0 lore raw

PATCH v2 1/1 io_uring: fix lacking of protection for compl_nr

coml_nr in ctx_flush_and_put() is not protected by uring_lock, this may cause problems when accessing it parallelly:

say coml_nr > 0

ctx_flush_and put other context
if (compl_nr) get mutex

                                      coml_nr > 0
                                      do flush
                                          coml_nr = 0
                                      release mutex
        get mutex
           do flush (*)
        release mutex

in (*) place, we call io_cqring_ev_posted() and users likely get none events there.

Fixes 2c32395d8111 ("io_uring: fix __tctx_task_work() ctx race")
---
 fs/io_uring.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/io_uring.c +2 -1
index c755efdac71f..c3bd2b3fc46b 100644
@@ -2005,7 +2005,8 @@ static void ctx_flush_and_put(struct io_ring_ctx *ctx)
 		return;
 	if (ctx->submit_state.compl_nr) {
 		mutex_lock(&ctx->uring_lock);
-		io_submit_flush_completions(ctx);
+		if (ctx->submit_state.compl_nr)
+			io_submit_flush_completions(ctx);
 		mutex_unlock(&ctx->uring_lock);
 	}
 	percpu_ref_put(&ctx->refs);
Signature · 1 line
2.24.4
maintainer Applied
#1 lore raw
Quoted 15 lines · Hao Xu

coml_nr in ctx_flush_and_put() is not protected by uring_lock, this may cause problems when accessing it parallelly:

say coml_nr > 0

ctx_flush_and put other context
if (compl_nr) get mutex

                                      coml_nr > 0
                                      do flush
                                          coml_nr = 0
                                      release mutex
        get mutex
           do flush (*)
        release mutex
Quoted 2 lines

in (*) place, we call io_cqring_ev_posted() and users likely get none events there.

Applied, thanks.

Signature · 1 line
Jens Axboe