1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/kernel.h> 3 #include <linux/errno.h> 4 #include <linux/fs.h> 5 #include <linux/file.h> 6 #include <linux/proc_fs.h> 7 #include <linux/seq_file.h> 8 #include <linux/nospec.h> 9 #include <linux/io_uring.h> 10 11 #include <uapi/linux/io_uring.h> 12 13 #include "filetable.h" 14 #include "sqpoll.h" 15 #include "fdinfo.h" 16 #include "cancel.h" 17 #include "rsrc.h" 18 #include "opdef.h" 19 20 #ifdef CONFIG_NET_RX_BUSY_POLL 21 static __cold void common_tracking_show_fdinfo(struct io_ring_ctx *ctx, 22 struct seq_file *m, 23 const char *tracking_strategy) 24 { 25 seq_puts(m, "NAPI:\tenabled\n"); 26 seq_printf(m, "napi tracking:\t%s\n", tracking_strategy); 27 seq_printf(m, "napi_busy_poll_dt:\t%llu\n", ctx->napi_busy_poll_dt); 28 if (ctx->napi_prefer_busy_poll) 29 seq_puts(m, "napi_prefer_busy_poll:\ttrue\n"); 30 else 31 seq_puts(m, "napi_prefer_busy_poll:\tfalse\n"); 32 } 33 34 static __cold void napi_show_fdinfo(struct io_ring_ctx *ctx, 35 struct seq_file *m) 36 { 37 unsigned int mode = READ_ONCE(ctx->napi_track_mode); 38 39 switch (mode) { 40 case IO_URING_NAPI_TRACKING_INACTIVE: 41 seq_puts(m, "NAPI:\tdisabled\n"); 42 break; 43 case IO_URING_NAPI_TRACKING_DYNAMIC: 44 common_tracking_show_fdinfo(ctx, m, "dynamic"); 45 break; 46 case IO_URING_NAPI_TRACKING_STATIC: 47 common_tracking_show_fdinfo(ctx, m, "static"); 48 break; 49 default: 50 seq_printf(m, "NAPI:\tunknown mode (%u)\n", mode); 51 } 52 } 53 #else 54 static inline void napi_show_fdinfo(struct io_ring_ctx *ctx, 55 struct seq_file *m) 56 { 57 } 58 #endif 59 60 static void __io_uring_show_fdinfo(struct io_ring_ctx *ctx, struct seq_file *m) 61 { 62 struct io_overflow_cqe *ocqe; 63 struct io_rings *r = ctx->rings; 64 unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1; 65 unsigned int sq_head = READ_ONCE(r->sq.head); 66 unsigned int sq_tail = READ_ONCE(r->sq.tail); 67 unsigned int cq_head = READ_ONCE(r->cq.head); 68 unsigned int cq_tail = READ_ONCE(r->cq.tail); 69 unsigned int sq_shift = 0; 70 unsigned int cq_entries, sq_entries; 71 int sq_pid = -1, sq_cpu = -1; 72 u64 sq_total_time = 0, sq_work_time = 0; 73 unsigned int i; 74 75 if (ctx->flags & IORING_SETUP_SQE128) 76 sq_shift = 1; 77 78 /* 79 * we may get imprecise sqe and cqe info if uring is actively running 80 * since we get cached_sq_head and cached_cq_tail without uring_lock 81 * and sq_tail and cq_head are changed by userspace. But it's ok since 82 * we usually use these info when it is stuck. 83 */ 84 seq_printf(m, "SqMask:\t0x%x\n", sq_mask); 85 seq_printf(m, "SqHead:\t%u\n", sq_head); 86 seq_printf(m, "SqTail:\t%u\n", sq_tail); 87 seq_printf(m, "CachedSqHead:\t%u\n", data_race(ctx->cached_sq_head)); 88 seq_printf(m, "CqMask:\t0x%x\n", cq_mask); 89 seq_printf(m, "CqHead:\t%u\n", cq_head); 90 seq_printf(m, "CqTail:\t%u\n", cq_tail); 91 seq_printf(m, "CachedCqTail:\t%u\n", data_race(ctx->cached_cq_tail)); 92 seq_printf(m, "SQEs:\t%u\n", sq_tail - sq_head); 93 sq_entries = min(sq_tail - sq_head, ctx->sq_entries); 94 for (i = 0; i < sq_entries; i++) { 95 unsigned int entry = i + sq_head; 96 struct io_uring_sqe *sqe; 97 unsigned int sq_idx; 98 bool sqe128 = false; 99 u8 opcode; 100 101 if (ctx->flags & IORING_SETUP_NO_SQARRAY) 102 sq_idx = entry & sq_mask; 103 else 104 sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]); 105 if (sq_idx > sq_mask) 106 continue; 107 108 sqe = &ctx->sq_sqes[sq_idx << sq_shift]; 109 opcode = READ_ONCE(sqe->opcode); 110 if (opcode >= IORING_OP_LAST) 111 continue; 112 opcode = array_index_nospec(opcode, IORING_OP_LAST); 113 if (sq_shift) { 114 sqe128 = true; 115 } else if (io_issue_defs[opcode].is_128) { 116 if (!(ctx->flags & IORING_SETUP_SQE_MIXED)) { 117 seq_printf(m, 118 "%5u: invalid sqe, 128B entry on non-mixed sq\n", 119 sq_idx); 120 break; 121 } 122 if ((++sq_head & sq_mask) == 0) { 123 seq_printf(m, 124 "%5u: corrupted sqe, wrapping 128B entry\n", 125 sq_idx); 126 break; 127 } 128 sqe128 = true; 129 } 130 seq_printf(m, "%5u: opcode:%s, fd:%d, flags:%x, off:%llu, " 131 "addr:0x%llx, rw_flags:0x%x, buf_index:%d " 132 "user_data:%llu", 133 sq_idx, io_uring_get_opcode(opcode), sqe->fd, 134 sqe->flags, (unsigned long long) sqe->off, 135 (unsigned long long) sqe->addr, sqe->rw_flags, 136 sqe->buf_index, sqe->user_data); 137 if (sqe128) { 138 u64 *sqeb = (void *) (sqe + 1); 139 int size = sizeof(struct io_uring_sqe) / sizeof(u64); 140 int j; 141 142 for (j = 0; j < size; j++) { 143 seq_printf(m, ", e%d:0x%llx", j, 144 (unsigned long long) *sqeb); 145 sqeb++; 146 } 147 } 148 seq_printf(m, "\n"); 149 cond_resched(); 150 } 151 seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head); 152 cq_entries = min(cq_tail - cq_head, ctx->cq_entries); 153 for (i = 0; i < cq_entries; i++) { 154 struct io_uring_cqe *cqe; 155 bool cqe32 = false; 156 157 cqe = &r->cqes[(cq_head & cq_mask)]; 158 if (cqe->flags & IORING_CQE_F_32 || ctx->flags & IORING_SETUP_CQE32) 159 cqe32 = true; 160 seq_printf(m, "%5u: user_data:%llu, res:%d, flags:%x", 161 cq_head & cq_mask, cqe->user_data, cqe->res, 162 cqe->flags); 163 if (cqe32) 164 seq_printf(m, ", extra1:%llu, extra2:%llu", 165 cqe->big_cqe[0], cqe->big_cqe[1]); 166 seq_printf(m, "\n"); 167 cq_head++; 168 if (cqe32) { 169 cq_head++; 170 i++; 171 } 172 cond_resched(); 173 } 174 175 if (ctx->flags & IORING_SETUP_SQPOLL) { 176 struct io_sq_data *sq = ctx->sq_data; 177 struct task_struct *tsk; 178 179 rcu_read_lock(); 180 tsk = rcu_dereference(sq->thread); 181 /* 182 * sq->thread might be NULL if we raced with the sqpoll 183 * thread termination. 184 */ 185 if (tsk) { 186 u64 usec; 187 188 get_task_struct(tsk); 189 rcu_read_unlock(); 190 usec = io_sq_cpu_usec(tsk); 191 put_task_struct(tsk); 192 sq_pid = sq->task_pid; 193 sq_cpu = sq->sq_cpu; 194 sq_total_time = usec; 195 sq_work_time = sq->work_time; 196 } else { 197 rcu_read_unlock(); 198 } 199 } 200 201 seq_printf(m, "SqThread:\t%d\n", sq_pid); 202 seq_printf(m, "SqThreadCpu:\t%d\n", sq_cpu); 203 seq_printf(m, "SqTotalTime:\t%llu\n", sq_total_time); 204 seq_printf(m, "SqWorkTime:\t%llu\n", sq_work_time); 205 seq_printf(m, "UserFiles:\t%u\n", ctx->file_table.data.nr); 206 for (i = 0; i < ctx->file_table.data.nr; i++) { 207 struct file *f = NULL; 208 209 if (ctx->file_table.data.nodes[i]) 210 f = io_slot_file(ctx->file_table.data.nodes[i]); 211 if (f) { 212 seq_printf(m, "%5u: ", i); 213 seq_file_path(m, f, " \t\n\\"); 214 seq_puts(m, "\n"); 215 } 216 } 217 seq_printf(m, "UserBufs:\t%u\n", ctx->buf_table.nr); 218 for (i = 0; i < ctx->buf_table.nr; i++) { 219 struct io_mapped_ubuf *buf = NULL; 220 221 if (ctx->buf_table.nodes[i]) 222 buf = ctx->buf_table.nodes[i]->buf; 223 if (buf) 224 seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, buf->len); 225 else 226 seq_printf(m, "%5u: <none>\n", i); 227 } 228 229 seq_puts(m, "PollList:\n"); 230 for (i = 0; i < (1U << ctx->cancel_table.hash_bits); i++) { 231 struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i]; 232 struct io_kiocb *req; 233 234 hlist_for_each_entry(req, &hb->list, hash_node) 235 seq_printf(m, " op=%d, task_works=%d\n", req->opcode, 236 task_work_pending(req->tctx->task)); 237 } 238 239 seq_puts(m, "CqOverflowList:\n"); 240 spin_lock(&ctx->completion_lock); 241 list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) { 242 struct io_uring_cqe *cqe = &ocqe->cqe; 243 244 seq_printf(m, " user_data=%llu, res=%d, flags=%x\n", 245 cqe->user_data, cqe->res, cqe->flags); 246 247 } 248 spin_unlock(&ctx->completion_lock); 249 napi_show_fdinfo(ctx, m); 250 } 251 252 /* 253 * Caller holds a reference to the file already, we don't need to do 254 * anything else to get an extra reference. 255 */ 256 __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *file) 257 { 258 struct io_ring_ctx *ctx = file->private_data; 259 260 /* 261 * Avoid ABBA deadlock between the seq lock and the io_uring mutex, 262 * since fdinfo case grabs it in the opposite direction of normal use 263 * cases. 264 */ 265 if (mutex_trylock(&ctx->uring_lock)) { 266 __io_uring_show_fdinfo(ctx, m); 267 mutex_unlock(&ctx->uring_lock); 268 } 269 } 270