Skip to content

Commit c6e8af2

Browse files
axboegregkh
authored andcommitted
io_uring/fdinfo: lock SQ thread while retrieving thread cpu/pid
commit 7644b1a upstream. We could race with SQ thread exit, and if we do, we'll hit a NULL pointer dereference when the thread is cleared. Grab the SQPOLL data lock before attempting to get the task cpu and pid for fdinfo, this ensures we have a stable view of it. Cc: [email protected] Link: https://bugzilla.kernel.org/show_bug.cgi?id=218032 Reviewed-by: Gabriel Krisman Bertazi <[email protected]> Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: He Gao <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent aeeb1ad commit c6e8af2

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

io_uring/io_uring.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -10238,7 +10238,7 @@ static int io_uring_show_cred(struct seq_file *m, unsigned int id,
1023810238

1023910239
static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
1024010240
{
10241-
struct io_sq_data *sq = NULL;
10241+
int sq_pid = -1, sq_cpu = -1;
1024210242
bool has_lock;
1024310243
int i;
1024410244

@@ -10251,13 +10251,19 @@ static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m)
1025110251
has_lock = mutex_trylock(&ctx->uring_lock);
1025210252

1025310253
if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
10254-
sq = ctx->sq_data;
10255-
if (!sq->thread)
10256-
sq = NULL;
10254+
struct io_sq_data *sq = ctx->sq_data;
10255+
10256+
if (mutex_trylock(&sq->lock)) {
10257+
if (sq->thread) {
10258+
sq_pid = task_pid_nr(sq->thread);
10259+
sq_cpu = task_cpu(sq->thread);
10260+
}
10261+
mutex_unlock(&sq->lock);
10262+
}
1025710263
}
1025810264

10259-
seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
10260-
seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
10265+
seq_printf(m, "SqThread:\t%d\n", sq_pid);
10266+
seq_printf(m, "SqThreadCpu:\t%d\n", sq_cpu);
1026110267
seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
1026210268
for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
1026310269
struct file *f = io_file_from_index(ctx, i);

0 commit comments

Comments
 (0)