xref: /linux/io_uring/fdinfo.c (revision 3f1c07fc21c68bd3bd2df9d2c9441f6485e934d9)
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
common_tracking_show_fdinfo(struct io_ring_ctx * ctx,struct seq_file * m,const char * tracking_strategy)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 
napi_show_fdinfo(struct io_ring_ctx * ctx,struct seq_file * m)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
napi_show_fdinfo(struct io_ring_ctx * ctx,struct seq_file * m)54 static inline void napi_show_fdinfo(struct io_ring_ctx *ctx,
55 				    struct seq_file *m)
56 {
57 }
58 #endif
59 
__io_uring_show_fdinfo(struct io_ring_ctx * ctx,struct seq_file * m)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 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 	}
150 	seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
151 	while (cq_head < cq_tail) {
152 		struct io_uring_cqe *cqe;
153 		bool cqe32 = false;
154 
155 		cqe = &r->cqes[(cq_head & cq_mask)];
156 		if (cqe->flags & IORING_CQE_F_32 || ctx->flags & IORING_SETUP_CQE32)
157 			cqe32 = true;
158 		seq_printf(m, "%5u: user_data:%llu, res:%d, flags:%x",
159 			   cq_head & cq_mask, cqe->user_data, cqe->res,
160 			   cqe->flags);
161 		if (cqe32)
162 			seq_printf(m, ", extra1:%llu, extra2:%llu\n",
163 					cqe->big_cqe[0], cqe->big_cqe[1]);
164 		seq_printf(m, "\n");
165 		cq_head++;
166 		if (cqe32)
167 			cq_head++;
168 	}
169 
170 	if (ctx->flags & IORING_SETUP_SQPOLL) {
171 		struct io_sq_data *sq = ctx->sq_data;
172 		struct task_struct *tsk;
173 
174 		rcu_read_lock();
175 		tsk = rcu_dereference(sq->thread);
176 		/*
177 		 * sq->thread might be NULL if we raced with the sqpoll
178 		 * thread termination.
179 		 */
180 		if (tsk) {
181 			u64 usec;
182 
183 			get_task_struct(tsk);
184 			rcu_read_unlock();
185 			usec = io_sq_cpu_usec(tsk);
186 			put_task_struct(tsk);
187 			sq_pid = sq->task_pid;
188 			sq_cpu = sq->sq_cpu;
189 			sq_total_time = usec;
190 			sq_work_time = sq->work_time;
191 		} else {
192 			rcu_read_unlock();
193 		}
194 	}
195 
196 	seq_printf(m, "SqThread:\t%d\n", sq_pid);
197 	seq_printf(m, "SqThreadCpu:\t%d\n", sq_cpu);
198 	seq_printf(m, "SqTotalTime:\t%llu\n", sq_total_time);
199 	seq_printf(m, "SqWorkTime:\t%llu\n", sq_work_time);
200 	seq_printf(m, "UserFiles:\t%u\n", ctx->file_table.data.nr);
201 	for (i = 0; i < ctx->file_table.data.nr; i++) {
202 		struct file *f = NULL;
203 
204 		if (ctx->file_table.data.nodes[i])
205 			f = io_slot_file(ctx->file_table.data.nodes[i]);
206 		if (f) {
207 			seq_printf(m, "%5u: ", i);
208 			seq_file_path(m, f, " \t\n\\");
209 			seq_puts(m, "\n");
210 		}
211 	}
212 	seq_printf(m, "UserBufs:\t%u\n", ctx->buf_table.nr);
213 	for (i = 0; i < ctx->buf_table.nr; i++) {
214 		struct io_mapped_ubuf *buf = NULL;
215 
216 		if (ctx->buf_table.nodes[i])
217 			buf = ctx->buf_table.nodes[i]->buf;
218 		if (buf)
219 			seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, buf->len);
220 		else
221 			seq_printf(m, "%5u: <none>\n", i);
222 	}
223 
224 	seq_puts(m, "PollList:\n");
225 	for (i = 0; i < (1U << ctx->cancel_table.hash_bits); i++) {
226 		struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i];
227 		struct io_kiocb *req;
228 
229 		hlist_for_each_entry(req, &hb->list, hash_node)
230 			seq_printf(m, "  op=%d, task_works=%d\n", req->opcode,
231 					task_work_pending(req->tctx->task));
232 	}
233 
234 	seq_puts(m, "CqOverflowList:\n");
235 	spin_lock(&ctx->completion_lock);
236 	list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
237 		struct io_uring_cqe *cqe = &ocqe->cqe;
238 
239 		seq_printf(m, "  user_data=%llu, res=%d, flags=%x\n",
240 			   cqe->user_data, cqe->res, cqe->flags);
241 
242 	}
243 	spin_unlock(&ctx->completion_lock);
244 	napi_show_fdinfo(ctx, m);
245 }
246 
247 /*
248  * Caller holds a reference to the file already, we don't need to do
249  * anything else to get an extra reference.
250  */
io_uring_show_fdinfo(struct seq_file * m,struct file * file)251 __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *file)
252 {
253 	struct io_ring_ctx *ctx = file->private_data;
254 
255 	/*
256 	 * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
257 	 * since fdinfo case grabs it in the opposite direction of normal use
258 	 * cases.
259 	 */
260 	if (mutex_trylock(&ctx->uring_lock)) {
261 		__io_uring_show_fdinfo(ctx, m);
262 		mutex_unlock(&ctx->uring_lock);
263 	}
264 }
265