|
Gabriel Krisman Bertazi |
27fc32 |
From a33ae9ce16a8ca62c5dffbe8909d185c6c5b4d77 Mon Sep 17 00:00:00 2001
|
|
Gabriel Krisman Bertazi |
27fc32 |
From: Pavel Begunkov <asml.silence@gmail.com>
|
|
Gabriel Krisman Bertazi |
27fc32 |
Date: Mon, 4 Oct 2021 20:02:49 +0100
|
|
Gabriel Krisman Bertazi |
27fc32 |
Subject: [PATCH] io_uring: optimise request allocation
|
|
Gabriel Krisman Bertazi |
27fc32 |
Git-commit: a33ae9ce16a8ca62c5dffbe8909d185c6c5b4d77
|
|
Gabriel Krisman Bertazi |
27fc32 |
Patch-mainline: v5.16-rc1
|
|
Gabriel Krisman Bertazi |
27fc32 |
References: bsc#1205205
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
Even after fully inlining io_alloc_req() my compiler does a NULL check
|
|
Gabriel Krisman Bertazi |
27fc32 |
in the path of successful allocation, no hacks like an empty dereference
|
|
Gabriel Krisman Bertazi |
27fc32 |
help it. Restructure io_alloc_req() by splitting out refilling part, so
|
|
Gabriel Krisman Bertazi |
27fc32 |
the compiler generate a slightly better binary.
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
|
|
Gabriel Krisman Bertazi |
27fc32 |
Link: https://lore.kernel.org/r/eda17571bdc7248d8e617b23e7132a5416e4680b.1633373302.git.asml.silence@gmail.com
|
|
Gabriel Krisman Bertazi |
27fc32 |
Signed-off-by: Jens Axboe <axboe@kernel.dk>
|
|
Gabriel Krisman Bertazi |
27fc32 |
Signed-off-by: Gabriel Krisman Bertazi <krisman@suse.de>
|
|
Gabriel Krisman Bertazi |
27fc32 |
---
|
|
Gabriel Krisman Bertazi |
27fc32 |
fs/io_uring.c | 28 ++++++++++++++++++++--------
|
|
Gabriel Krisman Bertazi |
27fc32 |
1 file changed, 20 insertions(+), 8 deletions(-)
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
diff --git a/fs/io_uring.c b/fs/io_uring.c
|
|
Gabriel Krisman Bertazi |
27fc32 |
index 045474b7b9c2..614c982c2697 100644
|
|
Gabriel Krisman Bertazi |
27fc32 |
--- a/fs/io_uring.c
|
|
Gabriel Krisman Bertazi |
27fc32 |
+++ b/fs/io_uring.c
|
|
Gabriel Krisman Bertazi |
27fc32 |
@@ -1912,18 +1912,17 @@ static bool io_flush_cached_reqs(struct io_ring_ctx *ctx)
|
|
Gabriel Krisman Bertazi |
27fc32 |
* Because of that, io_alloc_req() should be called only under ->uring_lock
|
|
Gabriel Krisman Bertazi |
27fc32 |
* and with extra caution to not get a request that is still worked on.
|
|
Gabriel Krisman Bertazi |
27fc32 |
*/
|
|
Gabriel Krisman Bertazi |
27fc32 |
-static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
|
|
Gabriel Krisman Bertazi |
27fc32 |
+static bool __io_alloc_req_refill(struct io_ring_ctx *ctx)
|
|
Gabriel Krisman Bertazi |
27fc32 |
__must_hold(&ctx->uring_lock)
|
|
Gabriel Krisman Bertazi |
27fc32 |
{
|
|
Gabriel Krisman Bertazi |
27fc32 |
struct io_submit_state *state = &ctx->submit_state;
|
|
Gabriel Krisman Bertazi |
27fc32 |
gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
|
|
Gabriel Krisman Bertazi |
27fc32 |
void *reqs[IO_REQ_ALLOC_BATCH];
|
|
Gabriel Krisman Bertazi |
27fc32 |
- struct io_wq_work_node *node;
|
|
Gabriel Krisman Bertazi |
27fc32 |
struct io_kiocb *req;
|
|
Gabriel Krisman Bertazi |
27fc32 |
int ret, i;
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
if (likely(state->free_list.next || io_flush_cached_reqs(ctx)))
|
|
Gabriel Krisman Bertazi |
27fc32 |
- goto got_req;
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ return true;
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
ret = kmem_cache_alloc_bulk(req_cachep, gfp, ARRAY_SIZE(reqs), reqs);
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
@@ -1934,7 +1933,7 @@ static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
|
|
Gabriel Krisman Bertazi |
27fc32 |
if (unlikely(ret <= 0)) {
|
|
Gabriel Krisman Bertazi |
27fc32 |
reqs[0] = kmem_cache_alloc(req_cachep, gfp);
|
|
Gabriel Krisman Bertazi |
27fc32 |
if (!reqs[0])
|
|
Gabriel Krisman Bertazi |
27fc32 |
- return NULL;
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ return false;
|
|
Gabriel Krisman Bertazi |
27fc32 |
ret = 1;
|
|
Gabriel Krisman Bertazi |
27fc32 |
}
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
@@ -1944,8 +1943,21 @@ static struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
|
|
Gabriel Krisman Bertazi |
27fc32 |
io_preinit_req(req, ctx);
|
|
Gabriel Krisman Bertazi |
27fc32 |
wq_stack_add_head(&req->comp_list, &state->free_list);
|
|
Gabriel Krisman Bertazi |
27fc32 |
}
|
|
Gabriel Krisman Bertazi |
27fc32 |
-got_req:
|
|
Gabriel Krisman Bertazi |
27fc32 |
- node = wq_stack_extract(&state->free_list);
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ return true;
|
|
Gabriel Krisman Bertazi |
27fc32 |
+}
|
|
Gabriel Krisman Bertazi |
27fc32 |
+
|
|
Gabriel Krisman Bertazi |
27fc32 |
+static inline bool io_alloc_req_refill(struct io_ring_ctx *ctx)
|
|
Gabriel Krisman Bertazi |
27fc32 |
+{
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ if (unlikely(!ctx->submit_state.free_list.next))
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ return __io_alloc_req_refill(ctx);
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ return true;
|
|
Gabriel Krisman Bertazi |
27fc32 |
+}
|
|
Gabriel Krisman Bertazi |
27fc32 |
+
|
|
Gabriel Krisman Bertazi |
27fc32 |
+static inline struct io_kiocb *io_alloc_req(struct io_ring_ctx *ctx)
|
|
Gabriel Krisman Bertazi |
27fc32 |
+{
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ struct io_wq_work_node *node;
|
|
Gabriel Krisman Bertazi |
27fc32 |
+
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ node = wq_stack_extract(&ctx->submit_state.free_list);
|
|
Gabriel Krisman Bertazi |
27fc32 |
return container_of(node, struct io_kiocb, comp_list);
|
|
Gabriel Krisman Bertazi |
27fc32 |
}
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
@@ -7217,12 +7229,12 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr)
|
|
Gabriel Krisman Bertazi |
27fc32 |
const struct io_uring_sqe *sqe;
|
|
Gabriel Krisman Bertazi |
27fc32 |
struct io_kiocb *req;
|
|
Gabriel Krisman Bertazi |
27fc32 |
|
|
Gabriel Krisman Bertazi |
27fc32 |
- req = io_alloc_req(ctx);
|
|
Gabriel Krisman Bertazi |
27fc32 |
- if (unlikely(!req)) {
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ if (unlikely(!io_alloc_req_refill(ctx))) {
|
|
Gabriel Krisman Bertazi |
27fc32 |
if (!submitted)
|
|
Gabriel Krisman Bertazi |
27fc32 |
submitted = -EAGAIN;
|
|
Gabriel Krisman Bertazi |
27fc32 |
break;
|
|
Gabriel Krisman Bertazi |
27fc32 |
}
|
|
Gabriel Krisman Bertazi |
27fc32 |
+ req = io_alloc_req(ctx);
|
|
Gabriel Krisman Bertazi |
27fc32 |
sqe = io_get_sqe(ctx);
|
|
Gabriel Krisman Bertazi |
27fc32 |
if (unlikely(!sqe)) {
|
|
Gabriel Krisman Bertazi |
27fc32 |
wq_stack_add_head(&req->comp_list, &ctx->submit_state.free_list);
|
|
Gabriel Krisman Bertazi |
27fc32 |
--
|
|
Gabriel Krisman Bertazi |
27fc32 |
2.35.3
|
|
Gabriel Krisman Bertazi |
27fc32 |
|