Skip to contentSkip to conversation

io_uring: always do atomic put from iowq

PATCH Applied Pavel Begunkov 2 participants 2 messages Apr 3, 2025 io_uring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring: always do atomic put from iowq

io_uring always switches requests to atomic refcounting for iowq execution before there is any parallilism by setting REQ_F_REFCOUNT, and the flag is not cleared until the request completes. That should be fine as long as the compiler doesn't make up a non existing value for the flags, however KCSAN still complains when the request owner changes oter flag bits:

BUG: KCSAN: data-race in io_req_task_cancel / io_wq_free_work

...
read to 0xffff888117207448 of 8 bytes by task 3871 on cpu 0:
req_ref_put_and_test io_uring/refs.h:22 [inline]

Skip REQ_F_REFCOUNT checks for iowq, we know it's set.

---
 io_uring/io_uring.c | 2 +-
 io_uring/refs.h     | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
io_uring/io_uring.c +1 -1
index 13e0b48d1aac..aad1fd9794b9 100644
@@ -1800,7 +1800,7 @@ struct io_wq_work *io_wq_free_work(struct io_wq_work *work)
 	struct io_kiocb *req = container_of(work, struct io_kiocb, work);
 	struct io_kiocb *nxt = NULL;
 
-	if (req_ref_put_and_test(req)) {
+	if (req_ref_put_and_test_atomic(req)) {
 		if (req->flags & IO_REQ_LINK_FLAGS)
 			nxt = io_req_find_next(req);
 		io_free_req(req);
io_uring/refs.h +7 -0
index 63982ead9f7d..0d928d87c4ed 100644
@@ -17,6 +17,13 @@ static inline bool req_ref_inc_not_zero(struct io_kiocb *req)
 	return atomic_inc_not_zero(&req->refs);
 }
 
+static inline bool req_ref_put_and_test_atomic(struct io_kiocb *req)
+{
+	WARN_ON_ONCE(!(data_race(req->flags) & REQ_F_REFCOUNT));
+	WARN_ON_ONCE(req_ref_zero_or_close_to_overflow(req));
+	return atomic_dec_and_test(&req->refs);
+}
+
 static inline bool req_ref_put_and_test(struct io_kiocb *req)
 {
 	if (likely(!(req->flags & REQ_F_REFCOUNT)))
Signature · 1 line
2.48.1
maintainer Applied
#1 lore raw
Quoted 8 lines · Pavel Begunkov

io_uring always switches requests to atomic refcounting for iowq execution before there is any parallilism by setting REQ_F_REFCOUNT, and the flag is not cleared until the request completes. That should be fine as long as the compiler doesn't make up a non existing value for the flags, however KCSAN still complains when the request owner changes oter flag bits:

[...]

Applied, thanks!

[1/1] io_uring: always do atomic put from iowq

      commit: 390513642ee6763c7ada07f0a1470474986e6c1c

Best regards,

Signature · 1 line
Jens Axboe