fdinfo.c 7.5 KB

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