]> www.infradead.org Git - nvme.git/commitdiff
io_uring: fix cancellation overwriting req->flags
authorPavel Begunkov <asml.silence@gmail.com>
Fri, 14 Jun 2024 00:04:29 +0000 (01:04 +0100)
committerJens Axboe <axboe@kernel.dk>
Fri, 14 Jun 2024 01:25:28 +0000 (19:25 -0600)
Only the current owner of a request is allowed to write into req->flags.
Hence, the cancellation path should never touch it. Add a new field
instead of the flag, move it into the 3rd cache line because it should
always be initialised. poll_refs can move further as polling is an
involved process anyway.

It's a minimal patch, in the future we can and should find a better
place for it and remove now unused REQ_F_CANCEL_SEQ.

Fixes: 521223d7c229f ("io_uring/cancel: don't default to setting req->work.cancel_seq")
Cc: stable@vger.kernel.org
Reported-by: Li Shi <sl1589472800@gmail.com>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/6827b129f8f0ad76fa9d1f0a773de938b240ffab.1718323430.git.asml.silence@gmail.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/linux/io_uring_types.h
io_uring/cancel.h
io_uring/io_uring.c

index 7a6b190c7da74a9e8a8e8ade24bdceffc58684f2..b48570eaa4497f5447839be089c8190a102f225d 100644 (file)
@@ -648,7 +648,7 @@ struct io_kiocb {
        struct io_rsrc_node             *rsrc_node;
 
        atomic_t                        refs;
-       atomic_t                        poll_refs;
+       bool                            cancel_seq_set;
        struct io_task_work             io_task_work;
        /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
        struct hlist_node               hash_node;
@@ -657,6 +657,7 @@ struct io_kiocb {
        /* opcode allocated if it needs to store data for async defer */
        void                            *async_data;
        /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
+       atomic_t                        poll_refs;
        struct io_kiocb                 *link;
        /* custom credentials, valid IFF REQ_F_CREDS is set */
        const struct cred               *creds;
index 76b32e65c03cd72452e37293bfcdc5b604e10267..b33995e00ba90500308595530040e04014184fdc 100644 (file)
@@ -27,10 +27,10 @@ bool io_cancel_req_match(struct io_kiocb *req, struct io_cancel_data *cd);
 
 static inline bool io_cancel_match_sequence(struct io_kiocb *req, int sequence)
 {
-       if ((req->flags & REQ_F_CANCEL_SEQ) && sequence == req->work.cancel_seq)
+       if (req->cancel_seq_set && sequence == req->work.cancel_seq)
                return true;
 
-       req->flags |= REQ_F_CANCEL_SEQ;
+       req->cancel_seq_set = true;
        req->work.cancel_seq = sequence;
        return false;
 }
index 816e93e7f94907144f0c6a2a1f12340aab8b55fa..154b25b8a613b16ad7c07fb590ad74b379447fb5 100644 (file)
@@ -2058,6 +2058,7 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,
        req->file = NULL;
        req->rsrc_node = NULL;
        req->task = current;
+       req->cancel_seq_set = false;
 
        if (unlikely(opcode >= IORING_OP_LAST)) {
                req->opcode = 0;