]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
io_uring/cmd: let cmds to know about dying task
authorPavel Begunkov <asml.silence@gmail.com>
Mon, 4 Nov 2024 16:12:04 +0000 (16:12 +0000)
committerDavid Sterba <dsterba@suse.com>
Mon, 11 Nov 2024 13:34:21 +0000 (14:34 +0100)
When the taks that submitted a request is dying, a task work for that
request might get run by a kernel thread or even worse by a half
dismantled task. We can't just cancel the task work without running the
callback as the cmd might need to do some clean up, so pass a flag
instead. If set, it's not safe to access any task resources and the
callback is expected to cancel the cmd ASAP.

Reviewed-by: Jens Axboe <axboe@kernel.dk>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Signed-off-by: David Sterba <dsterba@suse.com>
include/linux/io_uring_types.h
io_uring/uring_cmd.c

index 4b9ba523978d203ae23fb4ec9622d6e4e35a5e36..2ee5dc105b58ab48aef347a845509367a8241b9b 100644 (file)
@@ -37,6 +37,7 @@ enum io_uring_cmd_flags {
        /* set when uring wants to cancel a previously issued command */
        IO_URING_F_CANCEL               = (1 << 11),
        IO_URING_F_COMPAT               = (1 << 12),
+       IO_URING_F_TASK_DEAD            = (1 << 13),
 };
 
 struct io_wq_work_node {
index 39c3c816ec7882b9aa26cd45df6ade531379e40f..e2e8485932d64dd73e34b68906b421a2931af246 100644 (file)
@@ -119,9 +119,13 @@ EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable);
 static void io_uring_cmd_work(struct io_kiocb *req, struct io_tw_state *ts)
 {
        struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
+       unsigned int flags = IO_URING_F_COMPLETE_DEFER;
+
+       if (current->flags & (PF_EXITING | PF_KTHREAD))
+               flags |= IO_URING_F_TASK_DEAD;
 
        /* task_work executor checks the deffered list completion */
-       ioucmd->task_work_cb(ioucmd, IO_URING_F_COMPLETE_DEFER);
+       ioucmd->task_work_cb(ioucmd, flags);
 }
 
 void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,