Skip to content

Commit d10f19d

Browse files
committed
io_uring/uring_cmd: switch to always allocating async data
Basic conversion ensuring async_data is allocated off the prep path. Adds a basic alloc cache as well, as passthrough IO can be quite high in rate. Tested-by: Anuj Gupta <anuj20.g@samsung.com> Reviewed-by: Anuj Gupta <anuj20.g@samsung.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent e2ea5a7 commit d10f19d

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

include/linux/io_uring_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ struct io_ring_ctx {
301301
struct io_alloc_cache apoll_cache;
302302
struct io_alloc_cache netmsg_cache;
303303
struct io_alloc_cache rw_cache;
304+
struct io_alloc_cache uring_cache;
304305

305306
/*
306307
* Any cancelable uring_cmd is added to this list in

io_uring/io_uring.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)
313313
sizeof(struct io_async_msghdr));
314314
io_alloc_cache_init(&ctx->rw_cache, IO_ALLOC_CACHE_MAX,
315315
sizeof(struct io_async_rw));
316+
io_alloc_cache_init(&ctx->uring_cache, IO_ALLOC_CACHE_MAX,
317+
sizeof(struct uring_cache));
316318
io_futex_cache_init(ctx);
317319
init_completion(&ctx->ref_comp);
318320
xa_init_flags(&ctx->personalities, XA_FLAGS_ALLOC1);
@@ -2826,6 +2828,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)
28262828
io_alloc_cache_free(&ctx->apoll_cache, io_apoll_cache_free);
28272829
io_alloc_cache_free(&ctx->netmsg_cache, io_netmsg_cache_free);
28282830
io_alloc_cache_free(&ctx->rw_cache, io_rw_cache_free);
2831+
io_alloc_cache_free(&ctx->uring_cache, io_uring_cache_free);
28292832
io_futex_cache_free(ctx);
28302833
io_destroy_buffers(ctx);
28312834
mutex_unlock(&ctx->uring_lock);

io_uring/opdef.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,6 @@ const struct io_cold_def io_cold_defs[] = {
677677
[IORING_OP_URING_CMD] = {
678678
.name = "URING_CMD",
679679
.async_size = 2 * sizeof(struct io_uring_sqe),
680-
.prep_async = io_uring_cmd_prep_async,
681680
},
682681
[IORING_OP_SEND_ZC] = {
683682
.name = "SEND_ZC",

io_uring/uring_cmd.c

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@
1414
#include "rsrc.h"
1515
#include "uring_cmd.h"
1616

17+
static struct uring_cache *io_uring_async_get(struct io_kiocb *req)
18+
{
19+
struct io_ring_ctx *ctx = req->ctx;
20+
struct io_cache_entry *entry;
21+
struct uring_cache *cache;
22+
23+
entry = io_alloc_cache_get(&ctx->uring_cache);
24+
if (entry) {
25+
cache = container_of(entry, struct uring_cache, cache);
26+
req->flags |= REQ_F_ASYNC_DATA;
27+
req->async_data = cache;
28+
return cache;
29+
}
30+
if (!io_alloc_async_data(req))
31+
return req->async_data;
32+
return NULL;
33+
}
34+
35+
static void io_req_uring_cleanup(struct io_kiocb *req, unsigned int issue_flags)
36+
{
37+
struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
38+
struct uring_cache *cache = req->async_data;
39+
40+
if (issue_flags & IO_URING_F_UNLOCKED)
41+
return;
42+
if (io_alloc_cache_put(&req->ctx->uring_cache, &cache->cache)) {
43+
ioucmd->sqe = NULL;
44+
req->async_data = NULL;
45+
req->flags &= ~REQ_F_ASYNC_DATA;
46+
}
47+
}
48+
1749
bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx,
1850
struct task_struct *task, bool cancel_all)
1951
{
@@ -128,6 +160,7 @@ void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, ssize_t res2,
128160
io_req_set_res(req, ret, 0);
129161
if (req->ctx->flags & IORING_SETUP_CQE32)
130162
io_req_set_cqe32_extra(req, res2, 0);
163+
io_req_uring_cleanup(req, issue_flags);
131164
if (req->ctx->flags & IORING_SETUP_IOPOLL) {
132165
/* order with io_iopoll_req_issued() checking ->iopoll_complete */
133166
smp_store_release(&req->iopoll_completed, 1);
@@ -142,13 +175,19 @@ void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, ssize_t res2,
142175
}
143176
EXPORT_SYMBOL_GPL(io_uring_cmd_done);
144177

145-
int io_uring_cmd_prep_async(struct io_kiocb *req)
178+
static int io_uring_cmd_prep_setup(struct io_kiocb *req,
179+
const struct io_uring_sqe *sqe)
146180
{
147181
struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
182+
struct uring_cache *cache;
148183

149-
memcpy(req->async_data, ioucmd->sqe, uring_sqe_size(req->ctx));
150-
ioucmd->sqe = req->async_data;
151-
return 0;
184+
cache = io_uring_async_get(req);
185+
if (cache) {
186+
memcpy(cache->sqes, sqe, uring_sqe_size(req->ctx));
187+
ioucmd->sqe = req->async_data;
188+
return 0;
189+
}
190+
return -ENOMEM;
152191
}
153192

154193
int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
@@ -173,9 +212,9 @@ int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
173212
req->imu = ctx->user_bufs[index];
174213
io_req_set_rsrc_node(req, ctx, 0);
175214
}
176-
ioucmd->sqe = sqe;
177215
ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
178-
return 0;
216+
217+
return io_uring_cmd_prep_setup(req, sqe);
179218
}
180219

181220
int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
@@ -206,23 +245,14 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
206245
}
207246

208247
ret = file->f_op->uring_cmd(ioucmd, issue_flags);
209-
if (ret == -EAGAIN) {
210-
if (!req_has_async_data(req)) {
211-
if (io_alloc_async_data(req))
212-
return -ENOMEM;
213-
io_uring_cmd_prep_async(req);
214-
}
215-
return -EAGAIN;
216-
}
217-
218-
if (ret != -EIOCBQUEUED) {
219-
if (ret < 0)
220-
req_set_fail(req);
221-
io_req_set_res(req, ret, 0);
248+
if (ret == -EAGAIN || ret == -EIOCBQUEUED)
222249
return ret;
223-
}
224250

225-
return IOU_ISSUE_SKIP_COMPLETE;
251+
if (ret < 0)
252+
req_set_fail(req);
253+
io_req_uring_cleanup(req, issue_flags);
254+
io_req_set_res(req, ret, 0);
255+
return ret;
226256
}
227257

228258
int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
@@ -311,3 +341,8 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags)
311341
}
312342
EXPORT_SYMBOL_GPL(io_uring_cmd_sock);
313343
#endif
344+
345+
void io_uring_cache_free(struct io_cache_entry *entry)
346+
{
347+
kfree(container_of(entry, struct uring_cache, cache));
348+
}

io_uring/uring_cmd.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3+
struct uring_cache {
4+
union {
5+
struct io_cache_entry cache;
6+
struct io_uring_sqe sqes[2];
7+
};
8+
};
9+
310
int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags);
411
int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
512
int io_uring_cmd_prep_async(struct io_kiocb *req);
13+
void io_uring_cache_free(struct io_cache_entry *entry);
614

715
bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx,
8-
struct task_struct *task, bool cancel_all);
16+
struct task_struct *task, bool cancel_all);

0 commit comments

Comments
 (0)