Skip to contentSkip to conversation

msg_ring fixes

PATCH In review Jens Axboe 2 participants 5 messages Jul 1, 2024 – Jul 2, 2024 io_uring

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

cover msg_ring fixes

Hi,

A fix for the issue that Pavel highlighted in the conversion to using internal task_work for data requests, and while in there, postted an error in the caching where the wrong freeing function is used.

 io_uring/msg_ring.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
Signature · 1 line
Jens Axboe
author maintainer
#1 lore raw

PATCH 1/2 io_uring/msg_ring: check for dead submitter task

The change for improving the handling of the target CQE posting inadvertently dropped the NULL check for the submitter task on the target ring, reinstate that.

Fixes 0617bb500bfa ("io_uring/msg_ring: improve handling of target CQE posting")
---
 io_uring/msg_ring.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
io_uring/msg_ring.c +10 -5
index 47a754e83b49..c2171495098b 100644
@@ -86,16 +86,21 @@ static void io_msg_tw_complete(struct io_kiocb *req, struct io_tw_state *ts)
 	percpu_ref_put(&ctx->refs);
 }
 
-static void io_msg_remote_post(struct io_ring_ctx *ctx, struct io_kiocb *req,
-			       int res, u32 cflags, u64 user_data)
+static int io_msg_remote_post(struct io_ring_ctx *ctx, struct io_kiocb *req,
+			      int res, u32 cflags, u64 user_data)
 {
+	req->task = READ_ONCE(ctx->submitter_task);
+	if (!req->task) {
+		kmem_cache_free(req_cachep, req);
+		return -EOWNERDEAD;
+	}
 	req->cqe.user_data = user_data;
 	io_req_set_res(req, res, cflags);
 	percpu_ref_get(&ctx->refs);
 	req->ctx = ctx;
-	req->task = READ_ONCE(ctx->submitter_task);
 	req->io_task_work.func = io_msg_tw_complete;
 	io_req_task_work_add_remote(req, ctx, IOU_F_TWQ_LAZY_WAKE);
+	return 0;
 }
 
 static struct io_kiocb *io_msg_get_kiocb(struct io_ring_ctx *ctx)
@@ -125,8 +130,8 @@ static int io_msg_data_remote(struct io_kiocb *req)
 	if (msg->flags & IORING_MSG_RING_FLAGS_PASS)
 		flags = msg->cqe_flags;
 
-	io_msg_remote_post(target_ctx, target, msg->len, flags, msg->user_data);
-	return 0;
+	return io_msg_remote_post(target_ctx, target, msg->len, flags,
+					msg->user_data);
 }
 
 static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags)
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/2 io_uring/msg_ring: use kmem_cache_free() to free request

In reply to Jens Axboe

The change adding caching around the request allocated and freed for data messages changed a kmem_cache_free() to a kfree(), which isn't correct as the request came from slab in the first place. Fix that up and use the right freeing function if the cache is already at its limit.

Fixes 50cf5f3842af ("io_uring/msg_ring: add an alloc cache for io_kiocb entries")
---
 io_uring/msg_ring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/msg_ring.c +1 -1
index c2171495098b..29fa9285a33d 100644
@@ -82,7 +82,7 @@ static void io_msg_tw_complete(struct io_kiocb *req, struct io_tw_state *ts)
 		spin_unlock(&ctx->msg_lock);
 	}
 	if (req)
-		kfree(req);
+		kmem_cache_free(req_cachep, req);
 	percpu_ref_put(&ctx->refs);
 }
 
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/2] io_uring/msg_ring: use kmem_cache_free() to free request

Quoted 7 lines · Jens Axboe

The change adding caching around the request allocated and freed for data messages changed a kmem_cache_free() to a kfree(), which isn't correct as the request came from slab in the first place. Fix that up and use the right freeing function if the cache is already at its limit.

Fixes 50cf5f3842af ("io_uring/msg_ring: add an alloc cache for io_kiocb entries")

Fwiw, kfree works fine for kmem_cache_alloc objects since 6.4, when SLOB was removed. Either way, it doesn't harm.

Quoted 16 lines
---
 io_uring/msg_ring.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
io_uring/msg_ring.c +1 -1
index c2171495098b..29fa9285a33d 100644
@@ -82,7 +82,7 @@ static void io_msg_tw_complete(struct io_kiocb *req, struct io_tw_state *ts)
 		spin_unlock(&ctx->msg_lock);
 	}
 	if (req)
-		kfree(req);
+		kmem_cache_free(req_cachep, req);
 	percpu_ref_put(&ctx->refs);
 }
Signature · 1 line
Gabriel Krisman Bertazi
author maintainer
#4 lore raw

Re: [PATCH 2/2] io_uring/msg_ring: use kmem_cache_free() to free request

Quoted 7 lines · Gabriel Krisman Bertazi
Quoted 5 lines · Jens Axboe

The change adding caching around the request allocated and freed for data messages changed a kmem_cache_free() to a kfree(), which isn't correct as the request came from slab in the first place. Fix that up and use the right freeing function if the cache is already at its limit.

Quoted 5 lines
Quoted 2 lines
Fixes 50cf5f3842af ("io_uring/msg_ring: add an alloc cache for io_kiocb entries")

Fwiw, kfree works fine for kmem_cache_alloc objects since 6.4, when SLOB was removed. Either way, it doesn't harm.

Right, it's more for consistency sake, it's not fixing a real bug.

Signature · 1 line
Jens Axboe