public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH] io_uring: fix possible race condition against REQ_F_NEED_CLEANUP
@ 2020-06-18  7:01 Xiaoguang Wang
  2020-06-18 14:32 ` Jens Axboe
  0 siblings, 1 reply; 2+ messages in thread
From: Xiaoguang Wang @ 2020-06-18  7:01 UTC (permalink / raw)
  To: io-uring; +Cc: axboe, asml.silence, joseph.qi, Xiaoguang Wang

In io_read() or io_write(), when io request is submitted successfully,
it'll go through below codes:
    kfree(iovec);
    req->flags &= ~REQ_F_NEED_CLEANUP;
    return ret;

But indeed the "req->flags &= ~REQ_F_NEED_CLEANUP;" maybe dangerous,
io request may already have been completed, then io_complete_rw_iopoll()
and io_complete_rw() will be called, both of them will also modify
req->flags if needed, race condition will occur, concurrent modifaction
will happen, which is neither protected by locks nor atomic operations.

To eliminate this race, in io_read() or io_write(), if io request is
submitted successfully, we don't remove REQ_F_NEED_CLEANUP flag. If
REQ_F_NEED_CLEANUP is set, we'll leave __io_req_aux_free() to the
iovec cleanup work correspondingly.

Signed-off-by: Xiaoguang Wang <[email protected]>
---
 fs/io_uring.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/io_uring.c b/fs/io_uring.c
index 2038d52c5450..a78201b96179 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -2670,8 +2670,8 @@ static int io_read(struct io_kiocb *req, bool force_nonblock)
 		}
 	}
 out_free:
-	kfree(iovec);
-	req->flags &= ~REQ_F_NEED_CLEANUP;
+	if (!(req->flags & REQ_F_NEED_CLEANUP))
+		kfree(iovec);
 	return ret;
 }
 
@@ -2793,8 +2793,8 @@ static int io_write(struct io_kiocb *req, bool force_nonblock)
 		}
 	}
 out_free:
-	req->flags &= ~REQ_F_NEED_CLEANUP;
-	kfree(iovec);
+	if (!(req->flags & REQ_F_NEED_CLEANUP))
+		kfree(iovec);
 	return ret;
 }
 
-- 
2.17.2


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

* Re: [PATCH] io_uring: fix possible race condition against REQ_F_NEED_CLEANUP
  2020-06-18  7:01 [PATCH] io_uring: fix possible race condition against REQ_F_NEED_CLEANUP Xiaoguang Wang
@ 2020-06-18 14:32 ` Jens Axboe
  0 siblings, 0 replies; 2+ messages in thread
From: Jens Axboe @ 2020-06-18 14:32 UTC (permalink / raw)
  To: Xiaoguang Wang, io-uring; +Cc: asml.silence, joseph.qi

On 6/18/20 1:01 AM, Xiaoguang Wang wrote:
> In io_read() or io_write(), when io request is submitted successfully,
> it'll go through below codes:
>     kfree(iovec);
>     req->flags &= ~REQ_F_NEED_CLEANUP;
>     return ret;
> 
> But indeed the "req->flags &= ~REQ_F_NEED_CLEANUP;" maybe dangerous,
> io request may already have been completed, then io_complete_rw_iopoll()
> and io_complete_rw() will be called, both of them will also modify
> req->flags if needed, race condition will occur, concurrent modifaction
> will happen, which is neither protected by locks nor atomic operations.
> 
> To eliminate this race, in io_read() or io_write(), if io request is
> submitted successfully, we don't remove REQ_F_NEED_CLEANUP flag. If
> REQ_F_NEED_CLEANUP is set, we'll leave __io_req_aux_free() to the
> iovec cleanup work correspondingly.

Thanks, good catch!

-- 
Jens Axboe


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

end of thread, other threads:[~2020-06-18 14:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-18  7:01 [PATCH] io_uring: fix possible race condition against REQ_F_NEED_CLEANUP Xiaoguang Wang
2020-06-18 14:32 ` Jens Axboe

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