fdinfo.c 7.5 KB

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