Skip to contentSkip to conversation

io_uring: consider the overflow of sequence for timeout req

PATCH Superseded yangerkun 2 participants 3 messages Oct 14, 2019 – Oct 15, 2019

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

yangerkun

author
#0 lore raw

PATCH v2 1/1 io_uring: consider the overflow of sequence for timeout req

The sequence for timeout req may overflow, and it will lead to wrong order of timeout req list. And we should consider two situation:

1. ctx->cached_sq_head + count - 1 may overflow;
2. cached_sq_head of now may overflow compare with before cached_sq_head.

Fix the wrong logic by add record of count and use type long long to record the overflow.

---
 fs/io_uring.c | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)
fs/io_uring.c +25 -6
index 76fdbe84aff5..c8dbf85c1c91 100644
@@ -288,6 +288,7 @@ struct io_poll_iocb {
 struct io_timeout {
 	struct file			*file;
 	struct hrtimer			timer;
+	unsigned			count;
 };
 
 /*
@@ -1884,7 +1885,7 @@ static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer)
 
 static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
-	unsigned count, req_dist, tail_index;
+	unsigned count;
 	struct io_ring_ctx *ctx = req->ctx;
 	struct list_head *entry;
 	struct timespec64 ts;
@@ -1907,21 +1908,39 @@ static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 		count = 1;
 
 	req->sequence = ctx->cached_sq_head + count - 1;
+	req->timeout.count = count;
 	req->flags |= REQ_F_TIMEOUT;
 
 	/*
 	 * Insertion sort, ensuring the first entry in the list is always
 	 * the one we need first.
 	 */
-	tail_index = ctx->cached_cq_tail - ctx->rings->sq_dropped;
-	req_dist = req->sequence - tail_index;
 	spin_lock_irq(&ctx->completion_lock);
 	list_for_each_prev(entry, &ctx->timeout_list) {
 		struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
-		unsigned dist;
+		unsigned nxt_sq_head;
+		long long tmp, tmp_nxt;
 
-		dist = nxt->sequence - tail_index;
-		if (req_dist >= dist)
+		/* count bigger than before should break directly. */
+		if (count >= nxt->timeout.count)
+			break;
+
+		/*
+		 * Since cached_sq_head + count - 1 can overflow, use type long
+		 * long to store it.
+		 */
+		tmp = (long long)ctx->cached_sq_head + count - 1;
+		nxt_sq_head = nxt->sequence - nxt->timeout.count + 1;
+		tmp_nxt = (long long)nxt_sq_head + nxt->timeout.count - 1;
+
+		/*
+		 * cached_sq_head may overflow, and it will never overflow twice
+		 * once there is some timeout req still be valid.
+		 */
+		if (ctx->cached_sq_head < nxt_sq_head)
+			tmp += UINT_MAX;
+
+		if (tmp >= tmp_nxt)
 			break;
 	}
 	list_add(&req->list, entry);
Signature · 1 line
2.17.2
maintainer
#1 lore raw
Quoted 20 lines · yangerkun

The sequence for timeout req may overflow, and it will lead to wrong order of timeout req list. And we should consider two situation:

1. ctx->cached_sq_head + count - 1 may overflow;
2. cached_sq_head of now may overflow compare with before cached_sq_head.

Fix the wrong logic by add record of count and use type long long to record the overflow.

---
  fs/io_uring.c | 31 +++++++++++++++++++++++++------
  1 file changed, 25 insertions(+), 6 deletions(-)
fs/io_uring.c +0 -0
index 76fdbe84aff5..c8dbf85c1c91 100644

fs/io_uring.c struct io_poll_iocb { · line 292

Quoted 5 lines
fs/io_uring.c +1 -0
@@ -288,6 +288,7 @@ struct io_poll_iocb {
  struct io_timeout {
  	struct file			*file;
  	struct hrtimer			timer;
+	unsigned			count;
  };

Can we reuse io_kiocb->submit->sequence for this? Unfortunately doing it the way that you did, which does make the most logical sense, means that struct io_kiocb will now spill into a 4th cacheline.

Or maybe fold ->sequence and ->submit.sequence to reclaim that space?

Quoted 15 lines
fs/io_uring.c +1 -2
@@ -1907,21 +1908,39 @@ static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  		count = 1;
 
  	req->sequence = ctx->cached_sq_head + count - 1;
+	req->timeout.count = count;
  	req->flags |= REQ_F_TIMEOUT;
 
  	/*
  	 * Insertion sort, ensuring the first entry in the list is always
  	 * the one we need first.
  	 */
-	tail_index = ctx->cached_cq_tail - ctx->rings->sq_dropped;
-	req_dist = req->sequence - tail_index;
  	spin_lock_irq(&ctx->completion_lock);
  	list_for_each_prev(entry, &ctx->timeout_list) {

fs/io_uring.c static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) · line 1926

Quoted 10 lines
fs/io_uring.c +5 -3
  		struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
-		unsigned dist;
+		unsigned nxt_sq_head;
+		long long tmp, tmp_nxt;
 
-		dist = nxt->sequence - tail_index;
-		if (req_dist >= dist)
+		/* count bigger than before should break directly. */
+		if (count >= nxt->timeout.count)
+			break;

Took me a bit, but I guess that's true. It's an optimization, maybe it'd be cleaner if we just stuck to the sequence checking?

Signature · 1 line
Jens Axboe
author
#2 lore raw
Quoted 31 lines · Jens Axboe
Quoted 20 lines · yangerkun

The sequence for timeout req may overflow, and it will lead to wrong order of timeout req list. And we should consider two situation:

1. ctx->cached_sq_head + count - 1 may overflow;
2. cached_sq_head of now may overflow compare with before cached_sq_head.

Fix the wrong logic by add record of count and use type long long to record the overflow.

---
   fs/io_uring.c | 31 +++++++++++++++++++++++++------
   1 file changed, 25 insertions(+), 6 deletions(-)
fs/io_uring.c +0 -0
index 76fdbe84aff5..c8dbf85c1c91 100644

fs/io_uring.c struct io_poll_iocb { · line 292

Quoted 5 lines
fs/io_uring.c +1 -0
@@ -288,6 +288,7 @@ struct io_poll_iocb {
   struct io_timeout {
   	struct file			*file;
   	struct hrtimer			timer;
+	unsigned			count;
   };

Can we reuse io_kiocb->submit->sequence for this? Unfortunately doing it the way that you did, which does make the most logical sense, means that struct io_kiocb will now spill into a 4th cacheline.

Quoted 1 line

Or maybe fold ->sequence and ->submit.sequence to reclaim that space?

Yeah, prefer to reuse ->submit.sequence to dump the count. I have never thought about the cacheline before. Thanks a lot!

Quoted 21 lines
Quoted 21 lines
fs/io_uring.c +3 -3
@@ -1907,21 +1908,39 @@ static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
   		count = 1;
 
   	req->sequence = ctx->cached_sq_head + count - 1;
+	req->timeout.count = count;
   	req->flags |= REQ_F_TIMEOUT;
 
   	/*
   	 * Insertion sort, ensuring the first entry in the list is always
   	 * the one we need first.
   	 */
-	tail_index = ctx->cached_cq_tail - ctx->rings->sq_dropped;
-	req_dist = req->sequence - tail_index;
   	spin_lock_irq(&ctx->completion_lock);
   	list_for_each_prev(entry, &ctx->timeout_list) {
   		struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list);
-		unsigned dist;
+		unsigned nxt_sq_head;
+		long long tmp, tmp_nxt;
 
Quoted 8 lines

fs/io_uring.c static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) · line 1926

Quoted 5 lines
fs/io_uring.c +3 -2
-		dist = nxt->sequence - tail_index;
-		if (req_dist >= dist)
+		/* count bigger than before should break directly. */
+		if (count >= nxt->timeout.count)
+			break;

Took me a bit, but I guess that's true. It's an optimization, maybe it'd be cleaner if we just stuck to the sequence checking?

It's a good idea and thanks for you suggestion! I will resend the patch soon!

Thanks,
Kun.

Quoted 1 line