Line data Source code
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/io_uring.h>
9 :
10 : #include <uapi/linux/io_uring.h>
11 :
12 : #include "io_uring.h"
13 : #include "sqpoll.h"
14 : #include "fdinfo.h"
15 : #include "cancel.h"
16 : #include "rsrc.h"
17 :
18 : #ifdef CONFIG_PROC_FS
19 0 : static __cold int io_uring_show_cred(struct seq_file *m, unsigned int id,
20 : const struct cred *cred)
21 : {
22 0 : struct user_namespace *uns = seq_user_ns(m);
23 : struct group_info *gi;
24 : kernel_cap_t cap;
25 : int g;
26 :
27 0 : seq_printf(m, "%5d\n", id);
28 0 : seq_put_decimal_ull(m, "\tUid:\t", from_kuid_munged(uns, cred->uid));
29 0 : seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->euid));
30 0 : seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->suid));
31 0 : seq_put_decimal_ull(m, "\t\t", from_kuid_munged(uns, cred->fsuid));
32 0 : seq_put_decimal_ull(m, "\n\tGid:\t", from_kgid_munged(uns, cred->gid));
33 0 : seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->egid));
34 0 : seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->sgid));
35 0 : seq_put_decimal_ull(m, "\t\t", from_kgid_munged(uns, cred->fsgid));
36 0 : seq_puts(m, "\n\tGroups:\t");
37 0 : gi = cred->group_info;
38 0 : for (g = 0; g < gi->ngroups; g++) {
39 0 : seq_put_decimal_ull(m, g ? " " : "",
40 0 : from_kgid_munged(uns, gi->gid[g]));
41 : }
42 0 : seq_puts(m, "\n\tCapEff:\t");
43 0 : cap = cred->cap_effective;
44 0 : seq_put_hex_ll(m, NULL, cap.val, 16);
45 0 : seq_putc(m, '\n');
46 0 : return 0;
47 : }
48 :
49 0 : static __cold void __io_uring_show_fdinfo(struct io_ring_ctx *ctx,
50 : struct seq_file *m)
51 : {
52 0 : struct io_sq_data *sq = NULL;
53 : struct io_overflow_cqe *ocqe;
54 0 : struct io_rings *r = ctx->rings;
55 0 : unsigned int sq_mask = ctx->sq_entries - 1, cq_mask = ctx->cq_entries - 1;
56 0 : unsigned int sq_head = READ_ONCE(r->sq.head);
57 0 : unsigned int sq_tail = READ_ONCE(r->sq.tail);
58 0 : unsigned int cq_head = READ_ONCE(r->cq.head);
59 0 : unsigned int cq_tail = READ_ONCE(r->cq.tail);
60 0 : unsigned int cq_shift = 0;
61 0 : unsigned int sq_shift = 0;
62 : unsigned int sq_entries, cq_entries;
63 : bool has_lock;
64 : unsigned int i;
65 :
66 0 : if (ctx->flags & IORING_SETUP_CQE32)
67 0 : cq_shift = 1;
68 0 : if (ctx->flags & IORING_SETUP_SQE128)
69 0 : sq_shift = 1;
70 :
71 : /*
72 : * we may get imprecise sqe and cqe info if uring is actively running
73 : * since we get cached_sq_head and cached_cq_tail without uring_lock
74 : * and sq_tail and cq_head are changed by userspace. But it's ok since
75 : * we usually use these info when it is stuck.
76 : */
77 0 : seq_printf(m, "SqMask:\t0x%x\n", sq_mask);
78 0 : seq_printf(m, "SqHead:\t%u\n", sq_head);
79 0 : seq_printf(m, "SqTail:\t%u\n", sq_tail);
80 0 : seq_printf(m, "CachedSqHead:\t%u\n", ctx->cached_sq_head);
81 0 : seq_printf(m, "CqMask:\t0x%x\n", cq_mask);
82 0 : seq_printf(m, "CqHead:\t%u\n", cq_head);
83 0 : seq_printf(m, "CqTail:\t%u\n", cq_tail);
84 0 : seq_printf(m, "CachedCqTail:\t%u\n", ctx->cached_cq_tail);
85 0 : seq_printf(m, "SQEs:\t%u\n", sq_tail - sq_head);
86 0 : sq_entries = min(sq_tail - sq_head, ctx->sq_entries);
87 0 : for (i = 0; i < sq_entries; i++) {
88 0 : unsigned int entry = i + sq_head;
89 : struct io_uring_sqe *sqe;
90 : unsigned int sq_idx;
91 :
92 0 : sq_idx = READ_ONCE(ctx->sq_array[entry & sq_mask]);
93 0 : if (sq_idx > sq_mask)
94 0 : continue;
95 0 : sqe = &ctx->sq_sqes[sq_idx << sq_shift];
96 0 : seq_printf(m, "%5u: opcode:%s, fd:%d, flags:%x, off:%llu, "
97 : "addr:0x%llx, rw_flags:0x%x, buf_index:%d "
98 : "user_data:%llu",
99 0 : sq_idx, io_uring_get_opcode(sqe->opcode), sqe->fd,
100 0 : sqe->flags, (unsigned long long) sqe->off,
101 0 : (unsigned long long) sqe->addr, sqe->rw_flags,
102 0 : sqe->buf_index, sqe->user_data);
103 0 : if (sq_shift) {
104 0 : u64 *sqeb = (void *) (sqe + 1);
105 0 : int size = sizeof(struct io_uring_sqe) / sizeof(u64);
106 : int j;
107 :
108 0 : for (j = 0; j < size; j++) {
109 0 : seq_printf(m, ", e%d:0x%llx", j,
110 : (unsigned long long) *sqeb);
111 0 : sqeb++;
112 : }
113 : }
114 0 : seq_printf(m, "\n");
115 : }
116 0 : seq_printf(m, "CQEs:\t%u\n", cq_tail - cq_head);
117 0 : cq_entries = min(cq_tail - cq_head, ctx->cq_entries);
118 0 : for (i = 0; i < cq_entries; i++) {
119 0 : unsigned int entry = i + cq_head;
120 0 : struct io_uring_cqe *cqe = &r->cqes[(entry & cq_mask) << cq_shift];
121 :
122 0 : seq_printf(m, "%5u: user_data:%llu, res:%d, flag:%x",
123 : entry & cq_mask, cqe->user_data, cqe->res,
124 : cqe->flags);
125 0 : if (cq_shift)
126 0 : seq_printf(m, ", extra1:%llu, extra2:%llu\n",
127 : cqe->big_cqe[0], cqe->big_cqe[1]);
128 0 : seq_printf(m, "\n");
129 : }
130 :
131 : /*
132 : * Avoid ABBA deadlock between the seq lock and the io_uring mutex,
133 : * since fdinfo case grabs it in the opposite direction of normal use
134 : * cases. If we fail to get the lock, we just don't iterate any
135 : * structures that could be going away outside the io_uring mutex.
136 : */
137 0 : has_lock = mutex_trylock(&ctx->uring_lock);
138 :
139 0 : if (has_lock && (ctx->flags & IORING_SETUP_SQPOLL)) {
140 0 : sq = ctx->sq_data;
141 0 : if (!sq->thread)
142 0 : sq = NULL;
143 : }
144 :
145 0 : seq_printf(m, "SqThread:\t%d\n", sq ? task_pid_nr(sq->thread) : -1);
146 0 : seq_printf(m, "SqThreadCpu:\t%d\n", sq ? task_cpu(sq->thread) : -1);
147 0 : seq_printf(m, "UserFiles:\t%u\n", ctx->nr_user_files);
148 0 : for (i = 0; has_lock && i < ctx->nr_user_files; i++) {
149 0 : struct file *f = io_file_from_index(&ctx->file_table, i);
150 :
151 0 : if (f)
152 0 : seq_printf(m, "%5u: %s\n", i, file_dentry(f)->d_iname);
153 : else
154 0 : seq_printf(m, "%5u: <none>\n", i);
155 : }
156 0 : seq_printf(m, "UserBufs:\t%u\n", ctx->nr_user_bufs);
157 0 : for (i = 0; has_lock && i < ctx->nr_user_bufs; i++) {
158 0 : struct io_mapped_ubuf *buf = ctx->user_bufs[i];
159 0 : unsigned int len = buf->ubuf_end - buf->ubuf;
160 :
161 0 : seq_printf(m, "%5u: 0x%llx/%u\n", i, buf->ubuf, len);
162 : }
163 0 : if (has_lock && !xa_empty(&ctx->personalities)) {
164 : unsigned long index;
165 : const struct cred *cred;
166 :
167 0 : seq_printf(m, "Personalities:\n");
168 0 : xa_for_each(&ctx->personalities, index, cred)
169 0 : io_uring_show_cred(m, index, cred);
170 : }
171 :
172 0 : seq_puts(m, "PollList:\n");
173 0 : for (i = 0; i < (1U << ctx->cancel_table.hash_bits); i++) {
174 0 : struct io_hash_bucket *hb = &ctx->cancel_table.hbs[i];
175 0 : struct io_hash_bucket *hbl = &ctx->cancel_table_locked.hbs[i];
176 : struct io_kiocb *req;
177 :
178 0 : spin_lock(&hb->lock);
179 0 : hlist_for_each_entry(req, &hb->list, hash_node)
180 0 : seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
181 0 : task_work_pending(req->task));
182 0 : spin_unlock(&hb->lock);
183 :
184 0 : if (!has_lock)
185 0 : continue;
186 0 : hlist_for_each_entry(req, &hbl->list, hash_node)
187 0 : seq_printf(m, " op=%d, task_works=%d\n", req->opcode,
188 0 : task_work_pending(req->task));
189 : }
190 :
191 0 : if (has_lock)
192 0 : mutex_unlock(&ctx->uring_lock);
193 :
194 0 : seq_puts(m, "CqOverflowList:\n");
195 0 : spin_lock(&ctx->completion_lock);
196 0 : list_for_each_entry(ocqe, &ctx->cq_overflow_list, list) {
197 0 : struct io_uring_cqe *cqe = &ocqe->cqe;
198 :
199 0 : seq_printf(m, " user_data=%llu, res=%d, flags=%x\n",
200 : cqe->user_data, cqe->res, cqe->flags);
201 :
202 : }
203 :
204 0 : spin_unlock(&ctx->completion_lock);
205 0 : }
206 :
207 0 : __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *f)
208 : {
209 0 : struct io_ring_ctx *ctx = f->private_data;
210 :
211 0 : if (percpu_ref_tryget(&ctx->refs)) {
212 0 : __io_uring_show_fdinfo(ctx, m);
213 0 : percpu_ref_put(&ctx->refs);
214 : }
215 0 : }
216 : #endif
|