Skip to contentSkip to conversation

io_uring: fix IOPOLL with passthrough I/O

PATCH Superseded Jens Axboe 2 participants 4 messages Jan 14 io_uring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 io_uring: fix IOPOLL with passthrough I/O

A previous commit improving IOPOLL made an incorrect assumption that task_work isn't used with IOPOLL. This can cause crashes when doing passthrough I/O on nvme, where queueing the completion task_work will trample on the same memory that holds the completed list of requests.

Fix it up by shuffling the members around, so we're not sharing any parts that end up getting used in this path.

---

include/linux/io_uring_types.h +4 -7
index e4c804f99c30..211686ad89fd 100644
@@ -713,13 +713,10 @@ struct io_kiocb {
 	atomic_t			refs;
 	bool				cancel_seq_set;
 
-	/*
-	 * IOPOLL doesn't use task_work, so use the ->iopoll_node list
-	 * entry to manage pending iopoll requests.
-	 */
 	union {
 		struct io_task_work	io_task_work;
-		struct list_head	iopoll_node;
+		/* For IOPOLL setup queues, with hybrid polling */
+		u64                     iopoll_start;
 	};
 
 	union {
@@ -728,8 +725,8 @@ struct io_kiocb {
 		 * poll
 		 */
 		struct hlist_node	hash_node;
-		/* For IOPOLL setup queues, with hybrid polling */
-		u64                     iopoll_start;
+		/* IOPOLL completion handling */
+		struct list_head	iopoll_node;
 		/* for private io_kiocb freeing */
 		struct rcu_head		rcu_head;
 	};
Signature · 1 line
Jens Axboe
#1 lore raw
Quoted 36 lines · Jens Axboe

A previous commit improving IOPOLL made an incorrect assumption that task_work isn't used with IOPOLL. This can cause crashes when doing passthrough I/O on nvme, where queueing the completion task_work will trample on the same memory that holds the completed list of requests.

Fix it up by shuffling the members around, so we're not sharing any parts that end up getting used in this path.

---

include/linux/io_uring_types.h +2 -5
index e4c804f99c30..211686ad89fd 100644
@@ -713,13 +713,10 @@ struct io_kiocb {
 	atomic_t			refs;
 	bool				cancel_seq_set;
 
-	/*
-	 * IOPOLL doesn't use task_work, so use the ->iopoll_node list
-	 * entry to manage pending iopoll requests.
-	 */
 	union {
 		struct io_task_work	io_task_work;
-		struct list_head	iopoll_node;
+		/* For IOPOLL setup queues, with hybrid polling */
+		u64                     iopoll_start;
 	};
 
 	union {

include/linux/io_uring_types.h struct io_kiocb { · line 731

Quoted 9 lines
include/linux/io_uring_types.h +2 -2
@@ -728,8 +725,8 @@ struct io_kiocb {
 		 * poll
 		 */
 		struct hlist_node	hash_node;
-		/* For IOPOLL setup queues, with hybrid polling */
-		u64                     iopoll_start;
+		/* IOPOLL completion handling */
+		struct list_head	iopoll_node;
 		/* for private io_kiocb freeing */
 		struct rcu_head		rcu_head;

->hash_node is used by uring_cmd in io_uring_cmd_mark_cancelable()/io_uring_cmd_del_cancelable(), so this way may break uring_cmd if supporting iopoll and cancelable in future.

Thanks,
Ming

author maintainer
#2 lore raw
Quoted 35 lines · Ming Lei
Quoted 34 lines · Jens Axboe

A previous commit improving IOPOLL made an incorrect assumption that task_work isn't used with IOPOLL. This can cause crashes when doing passthrough I/O on nvme, where queueing the completion task_work will trample on the same memory that holds the completed list of requests.

Fix it up by shuffling the members around, so we're not sharing any parts that end up getting used in this path.

---

include/linux/io_uring_types.h +2 -5
index e4c804f99c30..211686ad89fd 100644
@@ -713,13 +713,10 @@ struct io_kiocb {
 	atomic_t			refs;
 	bool				cancel_seq_set;
 
-	/*
-	 * IOPOLL doesn't use task_work, so use the ->iopoll_node list
-	 * entry to manage pending iopoll requests.
-	 */
 	union {
 		struct io_task_work	io_task_work;
-		struct list_head	iopoll_node;
+		/* For IOPOLL setup queues, with hybrid polling */
+		u64                     iopoll_start;
 	};
 
Quoted 15 lines

include/linux/io_uring_types.h struct io_kiocb { · line 731

Quoted 11 lines
include/linux/io_uring_types.h +2 -2
 	union {
@@ -728,8 +725,8 @@ struct io_kiocb {
 		 * poll
 		 */
 		struct hlist_node	hash_node;
-		/* For IOPOLL setup queues, with hybrid polling */
-		u64                     iopoll_start;
+		/* IOPOLL completion handling */
+		struct list_head	iopoll_node;
 		/* for private io_kiocb freeing */
 		struct rcu_head		rcu_head;

->hash_node is used by uring_cmd in io_uring_cmd_mark_cancelable()/io_uring_cmd_del_cancelable(), so this way may break uring_cmd if supporting iopoll and cancelable in future.

We don't support cancelation on requests that go via the block stack, never have and probably never will. But I should make a comment about that, just in case...

Signature · 1 line
Jens Axboe
author maintainer
#3 lore raw
Quoted 52 lines · Jens Axboe
Quoted 35 lines · Ming Lei
Quoted 34 lines · Jens Axboe

A previous commit improving IOPOLL made an incorrect assumption that task_work isn't used with IOPOLL. This can cause crashes when doing passthrough I/O on nvme, where queueing the completion task_work will trample on the same memory that holds the completed list of requests.

Fix it up by shuffling the members around, so we're not sharing any parts that end up getting used in this path.

---

include/linux/io_uring_types.h +2 -5
index e4c804f99c30..211686ad89fd 100644
@@ -713,13 +713,10 @@ struct io_kiocb {
 	atomic_t			refs;
 	bool				cancel_seq_set;
 
-	/*
-	 * IOPOLL doesn't use task_work, so use the ->iopoll_node list
-	 * entry to manage pending iopoll requests.
-	 */
 	union {
 		struct io_task_work	io_task_work;
-		struct list_head	iopoll_node;
+		/* For IOPOLL setup queues, with hybrid polling */
+		u64                     iopoll_start;
 	};
 
Quoted 15 lines

include/linux/io_uring_types.h struct io_kiocb { · line 731

Quoted 11 lines
include/linux/io_uring_types.h +2 -2
 	union {
@@ -728,8 +725,8 @@ struct io_kiocb {
 		 * poll
 		 */
 		struct hlist_node	hash_node;
-		/* For IOPOLL setup queues, with hybrid polling */
-		u64                     iopoll_start;
+		/* IOPOLL completion handling */
+		struct list_head	iopoll_node;
 		/* for private io_kiocb freeing */
 		struct rcu_head		rcu_head;

->hash_node is used by uring_cmd in io_uring_cmd_mark_cancelable()/io_uring_cmd_del_cancelable(), so this way may break uring_cmd if supporting iopoll and cancelable in future.

Quoted 3 lines

We don't support cancelation on requests that go via the block stack, never have and probably never will. But I should make a comment about that, just in case...

Should be trivial enough to just explicitly disallow it.

io_uring/uring_cmd.c +9 -0
index 197474911f04..ee7b49f47cb5 100644
@@ -104,6 +104,15 @@ void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
 	struct io_kiocb *req = cmd_to_io_kiocb(cmd);
 	struct io_ring_ctx *ctx = req->ctx;
 
+	/*
+	 * Doing cancelations on IOPOLL requests are not supported. Both
+	 * because they can't get canceled in the block stack, but also
+	 * because iopoll completion data overlaps with the hash_node used
+	 * for tracking.
+	 */
+	if (ctx->flags & IORING_SETUP_IOPOLL)
+		return;
+
 	if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) {
 		cmd->flags |= IORING_URING_CMD_CANCELABLE;
 		io_ring_submit_lock(ctx, issue_flags);
Signature · 1 line
Jens Axboe