Skip to contentSkip to conversation

io_uring: ensure io_queue_deferred() is out-of-line

PATCH In review Jens Axboe 2 participants 3 messages Dec 31, 2024 – Jan 2, 2025 io_uring

lore
Share & tools
lore

Jens Axboe

author maintainer
#0 lore raw

PATCH 1/1 io_uring: ensure io_queue_deferred() is out-of-line

This is not the hot path, it's a slow path. Yet the locking for it is in the hot path, and __cold does not prevent it from being inlined.

Move the locking to the function itself, and mark it noinline as well to avoid it polluting the icache of the hot path.

---

io_uring/io_uring.c +4 -5
index 42d4cc5da73b..db198bd435b5 100644
@@ -550,8 +550,9 @@ void io_req_queue_iowq(struct io_kiocb *req)
 	io_req_task_work_add(req);
 }
 
-static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
+static __cold noinline void io_queue_deferred(struct io_ring_ctx *ctx)
 {
+	spin_lock(&ctx->completion_lock);
 	while (!list_empty(&ctx->defer_list)) {
 		struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
 						struct io_defer_entry, list);
@@ -562,6 +563,7 @@ static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
 		io_req_task_queue(de->req);
 		kfree(de);
 	}
+	spin_unlock(&ctx->completion_lock);
 }
 
 void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
@@ -570,11 +572,8 @@ void __io_commit_cqring_flush(struct io_ring_ctx *ctx)
 		io_poll_wq_wake(ctx);
 	if (ctx->off_timeout_used)
 		io_flush_timeouts(ctx);
-	if (ctx->drain_active) {
-		spin_lock(&ctx->completion_lock);
+	if (ctx->drain_active)
 		io_queue_deferred(ctx);
-		spin_unlock(&ctx->completion_lock);
-	}
 	if (ctx->has_evfd)
 		io_eventfd_flush_signal(ctx);
 }
Signature · 1 line
Jens Axboe
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 21 lines

-----Original Message-----
From: Jens Axboe <[email protected]>
Sent: Tuesday, December 31, 2024 8:37 AM
To: io-uring <[email protected]>
Subject: [PATCH for-next] io_uring: ensure io_queue_deferred() is out-of-line

This is not the hot path, it's a slow path. Yet the locking for it is in the hot path, and __cold does not prevent it from being inlined.

Move the locking to the function itself, and mark it noinline as well to avoid it polluting the icache of the hot path.

---

io_uring/io_uring.c index renamed from io_uring/io_uring.c +0 -0

42d4cc5da73b..db198bd435b5 100644

io_uring/io_uring.c void io_req_queue_iowq(struct io_kiocb *req) · line 555

Quoted 7 lines
io_uring/io_uring.c +2 -1
@@ -550,8 +550,9 @@ void io_req_queue_iowq(struct io_kiocb *req)
 	io_req_task_work_add(req);
 }
 
-static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
+static __cold noinline void io_queue_deferred(struct io_ring_ctx *ctx)
 {
+	spin_lock(&ctx->completion_lock);

Just a digression, whether the io_uring subsystem welcomes scope-based cleanup helpers, this is somewhat controversial in other submodules.

Quoted 27 lines
io_uring/io_uring.c +1 -0
 	while (!list_empty(&ctx->defer_list)) {
 		struct io_defer_entry *de = list_first_entry(&ctx->defer_list,
 						struct io_defer_entry, list);
@@ -562,6 +563,7 @@ static __cold void io_queue_deferred(struct io_ring_ctx
 *ctx)
 		io_req_task_queue(de->req);
 		kfree(de);
 	}
+	spin_unlock(&ctx->completion_lock);
 }
 

void __io_commit_cqring_flush(struct io_ring_ctx *ctx) @@ -570,11 +572,8
@@ void __io_commit_cqring_flush(struct io_ring_ctx *ctx)

io_uring/io_uring.c +1 -5
 		io_poll_wq_wake(ctx);
 	if (ctx->off_timeout_used)
 		io_flush_timeouts(ctx);
-	if (ctx->drain_active) {
-		spin_lock(&ctx->completion_lock);
+	if (ctx->drain_active)
 		io_queue_deferred(ctx);
-		spin_unlock(&ctx->completion_lock);
-	}
 	if (ctx->has_evfd)
 		io_eventfd_flush_signal(ctx);
 }
 
--
Quoted 2 lines

Jens Axboe

---
Li Zetao

author maintainer
#2 lore raw
Quoted 26 lines · lizetao

Hi,

Quoted 24 lines

-----Original Message-----
From: Jens Axboe <[email protected]>
Sent: Tuesday, December 31, 2024 8:37 AM
To: io-uring <[email protected]>
Subject: [PATCH for-next] io_uring: ensure io_queue_deferred() is out-of-line

This is not the hot path, it's a slow path. Yet the locking for it is in the hot path, and __cold does not prevent it from being inlined.

Move the locking to the function itself, and mark it noinline as well to avoid it polluting the icache of the hot path.

---

io_uring/io_uring.c index renamed from io_uring/io_uring.c +0 -0

42d4cc5da73b..db198bd435b5 100644

io_uring/io_uring.c +0 -0
@@ -550,8 +550,9 @@ void io_req_queue_iowq(struct io_kiocb *req)
 	io_req_task_work_add(req);
 }
 
Quoted 6 lines

io_uring/io_uring.c void io_req_queue_iowq(struct io_kiocb *req) · line 555

Quoted 4 lines
io_uring/io_uring.c +2 -1
-static __cold void io_queue_deferred(struct io_ring_ctx *ctx)
+static __cold noinline void io_queue_deferred(struct io_ring_ctx *ctx)
 {
+	spin_lock(&ctx->completion_lock);

Just a digression, whether the io_uring subsystem welcomes scope-based cleanup helpers, this is somewhat

We welcome any change that makes sense :-)

For this particular one, no point having io_queue_deferred() marked as cold yet still inlined, and no point having the locking outside of the helper as that gets inlined as well.

Signature · 1 line
Jens Axboe