Skip to contentSkip to conversation

io_uring: fix xa_alloc_cycle() error return value check

PATCH In review Jens Axboe 2 participants 4 messages Aug 20, 2021

Subsystems (2)
lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 io_uring: fix xa_alloc_cycle() error return value check

We currently check for ret != 0 to indicate error, but '1' is a valid return and just indicates that the allocation succeeded with a wrap. Correct the check to be for < 0, like it was before the xarray conversion.

Fixes 61cf93700fe6 ("io_uring: Convert personality_idr to XArray")

---

fs/io_uring.c +1 -1
index 979941bcd15a..60851908ed6f 100644
@@ -9843,7 +9843,7 @@ static int io_register_personality(struct io_ring_ctx *ctx)
 
 	ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
 			XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
-	if (!ret)
+	if (ret < 0)
 		return id;
 	put_cred(creds);
 	return ret;
Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw
Quoted 8 lines · Jens Axboe

We currently check for ret != 0 to indicate error, but '1' is a valid return and just indicates that the allocation succeeded with a wrap. Correct the check to be for < 0, like it was before the xarray conversion.

Fixes 61cf93700fe6 ("io_uring: Convert personality_idr to XArray")

Gah, included a debug patch, not the fix. Here's the right one.

fs/io_uring.c +5 -4
index 979941bcd15a..a2e20a6fbfed 100644
@@ -9843,10 +9843,11 @@ static int io_register_personality(struct io_ring_ctx *ctx)
 
 	ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
 			XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
-	if (!ret)
-		return id;
-	put_cred(creds);
-	return ret;
+	if (ret < 0) {
+		put_cred(creds);
+		return ret;
+	}
+	return id;
 }
 
 static int io_register_restrictions(struct io_ring_ctx *ctx, void __user *arg,
Signature · 1 line
Jens Axboe
#2 lore raw

fs/io_uring.c static int io_register_personality(struct io_ring_ctx *ctx) · line 9851

Quoted 12 lines · Jens Axboe
fs/io_uring.c +5 -4
 	ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
 			XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
-	if (!ret)
-		return id;
-	put_cred(creds);
-	return ret;
+	if (ret < 0) {
+		put_cred(creds);
+		return ret;
+	}
+	return id;
 }

Wouldn't you rather:

	if (ret >= 0)
		return id;
	put_cred(creds);
	return ret;

?

author maintainer
#3 lore raw
Quoted 16 lines · Matthew Wilcox

fs/io_uring.c static int io_register_personality(struct io_ring_ctx *ctx) · line 9851

Quoted 12 lines · Jens Axboe
fs/io_uring.c +5 -4
 	ret = xa_alloc_cyclic(&ctx->personalities, &id, (void *)creds,
 			XA_LIMIT(0, USHRT_MAX), &ctx->pers_next, GFP_KERNEL);
-	if (!ret)
-		return id;
-	put_cred(creds);
-	return ret;
+	if (ret < 0) {
+		put_cred(creds);
+		return ret;
+	}
+	return id;
 }

Wouldn't you rather:

Quoted 4 lines
	if (ret >= 0)
		return id;
	put_cred(creds);
	return ret;

Don't really feel that strongly, I tend to like to do the error path like that unless it's a hot path. But I can go either way, if someone else feels strongly about it :-)

Signature · 1 line
Jens Axboe