Skip to content

Commit e6c8aa9

Browse files
committed
io_uring: enable task/files specific overflow flushing
This allows us to selectively flush out pending overflows, depending on the task and/or files_struct being passed in. No intended functional changes in this patch. Reviewed-by: Pavel Begunkov <asml.silence@gmail.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 76e1b64 commit e6c8aa9

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

fs/io_uring.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,12 +1344,24 @@ static void io_cqring_mark_overflow(struct io_ring_ctx *ctx)
13441344
}
13451345
}
13461346

1347+
static inline bool io_match_files(struct io_kiocb *req,
1348+
struct files_struct *files)
1349+
{
1350+
if (!files)
1351+
return true;
1352+
if (req->flags & REQ_F_WORK_INITIALIZED)
1353+
return req->work.files == files;
1354+
return false;
1355+
}
1356+
13471357
/* Returns true if there are no backlogged entries after the flush */
1348-
static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
1358+
static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force,
1359+
struct task_struct *tsk,
1360+
struct files_struct *files)
13491361
{
13501362
struct io_rings *rings = ctx->rings;
1363+
struct io_kiocb *req, *tmp;
13511364
struct io_uring_cqe *cqe;
1352-
struct io_kiocb *req;
13531365
unsigned long flags;
13541366
LIST_HEAD(list);
13551367

@@ -1368,13 +1380,16 @@ static bool io_cqring_overflow_flush(struct io_ring_ctx *ctx, bool force)
13681380
ctx->cq_overflow_flushed = 1;
13691381

13701382
cqe = NULL;
1371-
while (!list_empty(&ctx->cq_overflow_list)) {
1383+
list_for_each_entry_safe(req, tmp, &ctx->cq_overflow_list, compl.list) {
1384+
if (tsk && req->task != tsk)
1385+
continue;
1386+
if (!io_match_files(req, files))
1387+
continue;
1388+
13721389
cqe = io_get_cqring(ctx);
13731390
if (!cqe && !force)
13741391
break;
13751392

1376-
req = list_first_entry(&ctx->cq_overflow_list, struct io_kiocb,
1377-
compl.list);
13781393
list_move(&req->compl.list, &list);
13791394
if (cqe) {
13801395
WRITE_ONCE(cqe->user_data, req->user_data);
@@ -1988,7 +2003,7 @@ static unsigned io_cqring_events(struct io_ring_ctx *ctx, bool noflush)
19882003
if (noflush && !list_empty(&ctx->cq_overflow_list))
19892004
return -1U;
19902005

1991-
io_cqring_overflow_flush(ctx, false);
2006+
io_cqring_overflow_flush(ctx, false, NULL, NULL);
19922007
}
19932008

19942009
/* See comment at the top of this file */
@@ -6489,7 +6504,7 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr,
64896504
/* if we have a backlog and couldn't flush it all, return BUSY */
64906505
if (test_bit(0, &ctx->sq_check_overflow)) {
64916506
if (!list_empty(&ctx->cq_overflow_list) &&
6492-
!io_cqring_overflow_flush(ctx, false))
6507+
!io_cqring_overflow_flush(ctx, false, NULL, NULL))
64936508
return -EBUSY;
64946509
}
64956510

@@ -7993,7 +8008,7 @@ static void io_ring_exit_work(struct work_struct *work)
79938008
*/
79948009
do {
79958010
if (ctx->rings)
7996-
io_cqring_overflow_flush(ctx, true);
8011+
io_cqring_overflow_flush(ctx, true, NULL, NULL);
79978012
io_iopoll_try_reap_events(ctx);
79988013
} while (!wait_for_completion_timeout(&ctx->ref_comp, HZ/20));
79998014
io_ring_ctx_free(ctx);
@@ -8013,7 +8028,7 @@ static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)
80138028

80148029
/* if we failed setting up the ctx, we might not have any rings */
80158030
if (ctx->rings)
8016-
io_cqring_overflow_flush(ctx, true);
8031+
io_cqring_overflow_flush(ctx, true, NULL, NULL);
80178032
io_iopoll_try_reap_events(ctx);
80188033
idr_for_each(&ctx->personality_idr, io_remove_personalities, ctx);
80198034

@@ -8069,12 +8084,6 @@ static bool io_match_link(struct io_kiocb *preq, struct io_kiocb *req)
80698084
return false;
80708085
}
80718086

8072-
static inline bool io_match_files(struct io_kiocb *req,
8073-
struct files_struct *files)
8074-
{
8075-
return (req->flags & REQ_F_WORK_INITIALIZED) && req->work.files == files;
8076-
}
8077-
80788087
static bool io_match_link_files(struct io_kiocb *req,
80798088
struct files_struct *files)
80808089
{
@@ -8365,7 +8374,7 @@ SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit,
83658374
ret = 0;
83668375
if (ctx->flags & IORING_SETUP_SQPOLL) {
83678376
if (!list_empty_careful(&ctx->cq_overflow_list))
8368-
io_cqring_overflow_flush(ctx, false);
8377+
io_cqring_overflow_flush(ctx, false, NULL, NULL);
83698378
if (flags & IORING_ENTER_SQ_WAKEUP)
83708379
wake_up(&ctx->sqo_wait);
83718380
submitted = to_submit;

0 commit comments

Comments
 (0)