Skip to contentSkip to conversation

io_uring: use cpu_online() to check p->sq_thread_cpu instead of cpu_possible()

PATCH In review Shenghui Wang 3 participants 6 messages May 1, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring: use cpu_online() to check p->sq_thread_cpu instead of cpu_possible()

This issue is found by running liburing/test/io_uring_setup test.

When test run, the testcase "attempt to bind to invalid cpu" would not pass with messages like:
io_uring_setup(1, 0xbfc2f7c8), \
flags: IORING_SETUP_SQPOLL|IORING_SETUP_SQ_AFF, \
resv: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000, \
sq_thread_cpu: 2
expected -1, got 3
FAIL

On my system, there is:
CPU(s) possible : 0-3

   CPU(s) online   : 0-1
   CPU(s) offline  : 2-3
   CPU(s) present  : 0-1

The sq_thread_cpu 2 is offline on my system, so the bind should fail. But cpu_possible() will pass the check. We shouldn't be able to bind to an offline cpu. Use cpu_online() to do the check.

After the change, the testcase run as expected: EINVAL will be returned for cpu offlined.

---
 fs/io_uring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/io_uring.c +2 -2
index 0e9fb2cb1984..aa3d39860a1c 100644
@@ -2241,7 +2241,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 	ctx->sqo_mm = current->mm;
 
 	ret = -EINVAL;
-	if (!cpu_possible(p->sq_thread_cpu))
+	if (!cpu_online(p->sq_thread_cpu))
 		goto err;
 
 	if (ctx->flags & IORING_SETUP_SQPOLL) {
@@ -2258,7 +2258,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 
 			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ret = -EINVAL;
-			if (!cpu_possible(p->sq_thread_cpu))
+			if (!cpu_online(p->sq_thread_cpu))
 				goto err;
 
 			ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
Signature · 1 line
2.20.1
#1 lore raw
Quoted 43 lines · Shenghui Wang

This issue is found by running liburing/test/io_uring_setup test.

When test run, the testcase "attempt to bind to invalid cpu" would not pass with messages like:

   io_uring_setup(1, 0xbfc2f7c8), \

flags: IORING_SETUP_SQPOLL|IORING_SETUP_SQ_AFF, \
resv: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000, \
sq_thread_cpu: 2
expected -1, got 3
FAIL

On my system, there is:

   CPU(s) possible : 0-3
   CPU(s) online   : 0-1
   CPU(s) offline  : 2-3
   CPU(s) present  : 0-1

The sq_thread_cpu 2 is offline on my system, so the bind should fail. But cpu_possible() will pass the check. We shouldn't be able to bind to an offline cpu. Use cpu_online() to do the check.

After the change, the testcase run as expected: EINVAL will be returned for cpu offlined.

---
 fs/io_uring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/io_uring.c +1 -1
index 0e9fb2cb1984..aa3d39860a1c 100644
@@ -2241,7 +2241,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 	ctx->sqo_mm = current->mm;
 
 	ret = -EINVAL;
-	if (!cpu_possible(p->sq_thread_cpu))
+	if (!cpu_online(p->sq_thread_cpu))
 		goto err;
 
 	if (ctx->flags & IORING_SETUP_SQPOLL) {

fs/io_uring.c static int io_sq_offload_start(struct io_ring_ctx *ctx, · line 2264

Quoted 8 lines
fs/io_uring.c +1 -1
@@ -2258,7 +2258,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 
 			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ret = -EINVAL;
-			if (!cpu_possible(p->sq_thread_cpu))
+			if (!cpu_online(p->sq_thread_cpu))
 				goto err;
 
 			ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,

Hmm. Why are we doing this check twice? Oh... Jens, I think you braino'd commit 917257daa0fea. Have a look. You probably wanted to get rid of the first check for cpu_possible.

-Jeff

maintainer
#2 lore raw
Quoted 52 lines · Jeff Moyer
Quoted 50 lines · Shenghui Wang

This issue is found by running liburing/test/io_uring_setup test.

When test run, the testcase "attempt to bind to invalid cpu" would not pass with messages like:

   io_uring_setup(1, 0xbfc2f7c8), \

flags: IORING_SETUP_SQPOLL|IORING_SETUP_SQ_AFF, \
resv: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000, \
sq_thread_cpu: 2
expected -1, got 3
FAIL

On my system, there is:

   CPU(s) possible : 0-3
   CPU(s) online   : 0-1
   CPU(s) offline  : 2-3
   CPU(s) present  : 0-1

The sq_thread_cpu 2 is offline on my system, so the bind should fail. But cpu_possible() will pass the check. We shouldn't be able to bind to an offline cpu. Use cpu_online() to do the check.

After the change, the testcase run as expected: EINVAL will be returned for cpu offlined.

---
 fs/io_uring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/io_uring.c +2 -2
index 0e9fb2cb1984..aa3d39860a1c 100644
@@ -2241,7 +2241,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 	ctx->sqo_mm = current->mm;
 
 	ret = -EINVAL;
-	if (!cpu_possible(p->sq_thread_cpu))
+	if (!cpu_online(p->sq_thread_cpu))
 		goto err;
 
 	if (ctx->flags & IORING_SETUP_SQPOLL) {
@@ -2258,7 +2258,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 
 			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ret = -EINVAL;
-			if (!cpu_possible(p->sq_thread_cpu))
+			if (!cpu_online(p->sq_thread_cpu))
 				goto err;
 
Quoted 5 lines

fs/io_uring.c static int io_sq_offload_start(struct io_ring_ctx *ctx, · line 2264

Quoted 1 line
fs/io_uring.c +0 -0
 			ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,

Hmm. Why are we doing this check twice? Oh... Jens, I think you braino'd commit 917257daa0fea. Have a look. You probably wanted to get rid of the first check for cpu_possible.

Added a fixup patch the other day:

http://git.kernel.dk/cgit/linux-block/commit/?h=for-linus&id=362bf8670efccebca22efda1ee5a5ee831ec5efb

Signature · 1 line
Jens Axboe
#3 lore raw
Quoted 59 lines · Jens Axboe
Quoted 52 lines · Jeff Moyer
Quoted 50 lines · Shenghui Wang

This issue is found by running liburing/test/io_uring_setup test.

When test run, the testcase "attempt to bind to invalid cpu" would not pass with messages like:

   io_uring_setup(1, 0xbfc2f7c8), \

flags: IORING_SETUP_SQPOLL|IORING_SETUP_SQ_AFF, \
resv: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000, \
sq_thread_cpu: 2
expected -1, got 3
FAIL

On my system, there is:

   CPU(s) possible : 0-3
   CPU(s) online   : 0-1
   CPU(s) offline  : 2-3
   CPU(s) present  : 0-1

The sq_thread_cpu 2 is offline on my system, so the bind should fail. But cpu_possible() will pass the check. We shouldn't be able to bind to an offline cpu. Use cpu_online() to do the check.

After the change, the testcase run as expected: EINVAL will be returned for cpu offlined.

---
 fs/io_uring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/io_uring.c +2 -2
index 0e9fb2cb1984..aa3d39860a1c 100644
@@ -2241,7 +2241,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 	ctx->sqo_mm = current->mm;
 
 	ret = -EINVAL;
-	if (!cpu_possible(p->sq_thread_cpu))
+	if (!cpu_online(p->sq_thread_cpu))
 		goto err;
 
 	if (ctx->flags & IORING_SETUP_SQPOLL) {
@@ -2258,7 +2258,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 
 			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ret = -EINVAL;
-			if (!cpu_possible(p->sq_thread_cpu))
+			if (!cpu_online(p->sq_thread_cpu))
 				goto err;
 
Quoted 5 lines

fs/io_uring.c static int io_sq_offload_start(struct io_ring_ctx *ctx, · line 2264

Quoted 1 line
fs/io_uring.c +0 -0
 			ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,

Hmm. Why are we doing this check twice? Oh... Jens, I think you braino'd commit 917257daa0fea. Have a look. You probably wanted to get rid of the first check for cpu_possible.

Quoted 3 lines

Added a fixup patch the other day:

http://git.kernel.dk/cgit/linux-block/commit/?h=for-linus&id=362bf8670efccebca22efda1ee5a5ee831ec5efb

@@ -2333,13 +2329,14 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
ctx->sq_thread_idle = HZ;

if (p->flags & IORING_SETUP_SQ_AFF) {
- int cpu;
+ int cpu = p->sq_thread_cpu;

- cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
ret = -EINVAL;
- if (!cpu_possible(p->sq_thread_cpu))
+ if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
goto err;

+ cpu = array_index_nospec(cpu, nr_cpu_ids);
+

Why do you do the array_index_nospec last? Why wouldn't that be written as:

	if (p->flags & IORING_SETUP_SQ_AFF) {
		int cpu = array_index_nospec(p->sq_thread_cpu, nr_cpu_ids);

		ret = -EINVAL;
		if (!cpu_possible(cpu))
			goto err;

		ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
						ctx, cpu,
						"io_uring-sq");
	} else {

...

That would take away some head-scratching for me.

Cheers,
Jeff

maintainer
#4 lore raw
Quoted 95 lines · Jeff Moyer
Quoted 62 lines · Jens Axboe
Quoted 52 lines · Jeff Moyer
Quoted 50 lines · Shenghui Wang

This issue is found by running liburing/test/io_uring_setup test.

When test run, the testcase "attempt to bind to invalid cpu" would not pass with messages like:

   io_uring_setup(1, 0xbfc2f7c8), \

flags: IORING_SETUP_SQPOLL|IORING_SETUP_SQ_AFF, \
resv: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000, \
sq_thread_cpu: 2
expected -1, got 3
FAIL

On my system, there is:

   CPU(s) possible : 0-3
   CPU(s) online   : 0-1
   CPU(s) offline  : 2-3
   CPU(s) present  : 0-1

The sq_thread_cpu 2 is offline on my system, so the bind should fail. But cpu_possible() will pass the check. We shouldn't be able to bind to an offline cpu. Use cpu_online() to do the check.

After the change, the testcase run as expected: EINVAL will be returned for cpu offlined.

---
 fs/io_uring.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/io_uring.c +2 -2
index 0e9fb2cb1984..aa3d39860a1c 100644
@@ -2241,7 +2241,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 	ctx->sqo_mm = current->mm;
 
 	ret = -EINVAL;
-	if (!cpu_possible(p->sq_thread_cpu))
+	if (!cpu_online(p->sq_thread_cpu))
 		goto err;
 
 	if (ctx->flags & IORING_SETUP_SQPOLL) {
@@ -2258,7 +2258,7 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 
 			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ret = -EINVAL;
-			if (!cpu_possible(p->sq_thread_cpu))
+			if (!cpu_online(p->sq_thread_cpu))
 				goto err;
 
Quoted 5 lines

fs/io_uring.c static int io_sq_offload_start(struct io_ring_ctx *ctx, · line 2264

Quoted 1 line
fs/io_uring.c +0 -0
 			ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,

Hmm. Why are we doing this check twice? Oh... Jens, I think you braino'd commit 917257daa0fea. Have a look. You probably wanted to get rid of the first check for cpu_possible.

Added a fixup patch the other day:

http://git.kernel.dk/cgit/linux-block/commit/?h=for-linus&id=362bf8670efccebca22efda1ee5a5ee831ec5efb

(quoted diff) +4 -3
@@ -2333,13 +2329,14 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
 			ctx->sq_thread_idle = HZ;
 
 		if (p->flags & IORING_SETUP_SQ_AFF) {
-			int cpu;
+			int cpu = p->sq_thread_cpu;
 
-			cpu = array_index_nospec(p->sq_thread_cpu, NR_CPUS);
 			ret = -EINVAL;
-			if (!cpu_possible(p->sq_thread_cpu))
+			if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
 				goto err;
 
+			cpu = array_index_nospec(cpu, nr_cpu_ids);
+
 

Why do you do the array_index_nospec last? Why wouldn't that be written as:

	if (p->flags & IORING_SETUP_SQ_AFF) {
		int cpu = array_index_nospec(p->sq_thread_cpu, nr_cpu_ids);

		ret = -EINVAL;
		if (!cpu_possible(cpu))
			goto err;

		ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread,
						ctx, cpu,
						"io_uring-sq");
	} else {
Quoted 3 lines

...

That would take away some head-scratching for me.

Agree, I've cleaned it up, it was a bit of a mess.

Signature · 1 line
Jens Axboe
#5 lore raw
Quoted 1 line · Jens Axboe

Agree, I've cleaned it up, it was a bit of a mess.

LGTM, thanks!

-Jeff