Skip to content

Commit 7644b1a

Browse files
committed
io_uring/fdinfo: lock SQ thread while retrieving thread cpu/pid
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]>
1 parent 8b51a39 commit 7644b1a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

io_uring/fdinfo.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
5353
__cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
5454
{
5555
struct io_ring_ctx *ctx = f->private_data;
56-
struct io_sq_data *sq = NULL;
5756
struct io_overflow_cqe *ocqe;
5857
struct io_rings *r = ctx->rings;
5958
unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
@@ -64,6 +63,7 @@ __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
6463
unsigned int cq_shift = 0;
6564
unsigned int sq_shift = 0;
6665
unsigned int sq_entries, cq_entries;
66+
int sq_pid = -1, sq_cpu = -1;
6767
bool has_lock;
6868
unsigned int i;
6969

@@ -143,13 +143,19 @@ __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
143143
has_lock = mutex_trylock(&ctx->uring_lock);
144144

145145
if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
146-
sq = ctx->sq_data;
147-
if (!sq->thread)
148-
sq = NULL;
146+
struct io_sq_data *sq = ctx->sq_data;
147+
148+
if (mutex_trylock(&sq->lock)) {
149+
if (sq->thread) {
150+
sq_pid = task_pid_nr(sq->thread);
151+
sq_cpu = task_cpu(sq->thread);
152+
}
153+
mutex_unlock(&sq->lock);
154+
}
149155
}
150156

151-
seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
152-
seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
157+
seq_printf(m, "SqThread:\t%d\n", sq_pid);
158+
seq_printf(m, "SqThreadCpu:\t%d\n", sq_cpu);
153159
seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
154160
for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
155161
struct file *f = io_file_from_index(&ctx->file_table, i);

0 commit comments

Comments
 (0)