]> www.infradead.org Git - nvme.git/commitdiff
io_uring: always do atomic put from iowq
authorPavel Begunkov <asml.silence@gmail.com>
Thu, 3 Apr 2025 11:29:30 +0000 (12:29 +0100)
committerJens Axboe <axboe@kernel.dk>
Thu, 3 Apr 2025 14:31:57 +0000 (08:31 -0600)
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.

Reported-by: syzbot+903a2ad71fb3f1e47cf5@syzkaller.appspotmail.com
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/d880bc27fb8c3209b54641be4ff6ac02b0e5789a.1743679736.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
io_uring/io_uring.c
io_uring/refs.h

index a4065e3d13d0695fea0f24b340886344b82a652d..c6209fe44cb175c02687d6b734542c4aacf74d16 100644 (file)
@@ -1796,7 +1796,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);
index 63982ead9f7dabcfbdbab74777729816647ff368..0d928d87c4ed1372d32658236e857fbadc21e44b 100644 (file)
@@ -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)))