blk-mq-debugfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017 Facebook
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/blkdev.h>
  7. #include <linux/build_bug.h>
  8. #include <linux/debugfs.h>
  9. #include "blk.h"
  10. #include "blk-mq.h"
  11. #include "blk-mq-debugfs.h"
  12. #include "blk-mq-sched.h"
  13. #include "blk-rq-qos.h"
  14. static int queue_poll_stat_show(void *data, struct seq_file *m)
  15. {
  16. return 0;
  17. }
  18. static void *queue_requeue_list_start(struct seq_file *m, loff_t *pos)
  19. __acquires(&q->requeue_lock)
  20. {
  21. struct request_queue *q = m->private;
  22. spin_lock_irq(&q->requeue_lock);
  23. return seq_list_start(&q->requeue_list, *pos);
  24. }
  25. static void *queue_requeue_list_next(struct seq_file *m, void *v, loff_t *pos)
  26. {
  27. struct request_queue *q = m->private;
  28. return seq_list_next(v, &q->requeue_list, pos);
  29. }
  30. static void queue_requeue_list_stop(struct seq_file *m, void *v)
  31. __releases(&q->requeue_lock)
  32. {
  33. struct request_queue *q = m->private;
  34. spin_unlock_irq(&q->requeue_lock);
  35. }
  36. static const struct seq_operations queue_requeue_list_seq_ops = {
  37. .start = queue_requeue_list_start,
  38. .next = queue_requeue_list_next,
  39. .stop = queue_requeue_list_stop,
  40. .show = blk_mq_debugfs_rq_show,
  41. };
  42. static int blk_flags_show(struct seq_file *m, const unsigned long flags,
  43. const char *const *flag_name, int flag_name_count)
  44. {
  45. bool sep = false;
  46. int i;
  47. for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
  48. if (!(flags & BIT(i)))
  49. continue;
  50. if (sep)
  51. seq_puts(m, "|");
  52. sep = true;
  53. if (i < flag_name_count && flag_name[i])
  54. seq_puts(m, flag_name[i]);
  55. else
  56. seq_printf(m, "%d", i);
  57. }
  58. return 0;
  59. }
  60. static int queue_pm_only_show(void *data, struct seq_file *m)
  61. {
  62. struct request_queue *q = data;
  63. seq_printf(m, "%d\n", atomic_read(&q->pm_only));
  64. return 0;
  65. }
  66. #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
  67. static const char *const blk_queue_flag_name[] = {
  68. QUEUE_FLAG_NAME(DYING),
  69. QUEUE_FLAG_NAME(NOMERGES),
  70. QUEUE_FLAG_NAME(SAME_COMP),
  71. QUEUE_FLAG_NAME(FAIL_IO),
  72. QUEUE_FLAG_NAME(NOXMERGES),
  73. QUEUE_FLAG_NAME(SAME_FORCE),
  74. QUEUE_FLAG_NAME(INIT_DONE),
  75. QUEUE_FLAG_NAME(STATS),
  76. QUEUE_FLAG_NAME(REGISTERED),
  77. QUEUE_FLAG_NAME(QUIESCED),
  78. QUEUE_FLAG_NAME(RQ_ALLOC_TIME),
  79. QUEUE_FLAG_NAME(HCTX_ACTIVE),
  80. QUEUE_FLAG_NAME(SQ_SCHED),
  81. QUEUE_FLAG_NAME(DISABLE_WBT_DEF),
  82. QUEUE_FLAG_NAME(NO_ELV_SWITCH),
  83. QUEUE_FLAG_NAME(QOS_ENABLED),
  84. QUEUE_FLAG_NAME(BIO_ISSUE_TIME),
  85. };
  86. #undef QUEUE_FLAG_NAME
  87. static int queue_state_show(void *data, struct seq_file *m)
  88. {
  89. struct request_queue *q = data;
  90. BUILD_BUG_ON(ARRAY_SIZE(blk_queue_flag_name) != QUEUE_FLAG_MAX);
  91. blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
  92. ARRAY_SIZE(blk_queue_flag_name));
  93. seq_puts(m, "\n");
  94. return 0;
  95. }
  96. static ssize_t queue_state_write(void *data, const char __user *buf,
  97. size_t count, loff_t *ppos)
  98. {
  99. struct request_queue *q = data;
  100. char opbuf[16] = { }, *op;
  101. /*
  102. * The "state" attribute is removed when the queue is removed. Don't
  103. * allow setting the state on a dying queue to avoid a use-after-free.
  104. */
  105. if (blk_queue_dying(q))
  106. return -ENOENT;
  107. if (count >= sizeof(opbuf)) {
  108. pr_err("%s: operation too long\n", __func__);
  109. goto inval;
  110. }
  111. if (copy_from_user(opbuf, buf, count))
  112. return -EFAULT;
  113. op = strstrip(opbuf);
  114. if (strcmp(op, "run") == 0) {
  115. blk_mq_run_hw_queues(q, true);
  116. } else if (strcmp(op, "start") == 0) {
  117. blk_mq_start_stopped_hw_queues(q, true);
  118. } else if (strcmp(op, "kick") == 0) {
  119. blk_mq_kick_requeue_list(q);
  120. } else {
  121. pr_err("%s: unsupported operation '%s'\n", __func__, op);
  122. inval:
  123. pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
  124. return -EINVAL;
  125. }
  126. return count;
  127. }
  128. static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
  129. { "poll_stat", 0400, queue_poll_stat_show },
  130. { "requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops },
  131. { "pm_only", 0600, queue_pm_only_show, NULL },
  132. { "state", 0600, queue_state_show, queue_state_write },
  133. { "zone_wplugs", 0400, queue_zone_wplugs_show, NULL },
  134. { },
  135. };
  136. #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
  137. static const char *const hctx_state_name[] = {
  138. HCTX_STATE_NAME(STOPPED),
  139. HCTX_STATE_NAME(TAG_ACTIVE),
  140. HCTX_STATE_NAME(SCHED_RESTART),
  141. HCTX_STATE_NAME(INACTIVE),
  142. };
  143. #undef HCTX_STATE_NAME
  144. static int hctx_state_show(void *data, struct seq_file *m)
  145. {
  146. struct blk_mq_hw_ctx *hctx = data;
  147. BUILD_BUG_ON(ARRAY_SIZE(hctx_state_name) != BLK_MQ_S_MAX);
  148. blk_flags_show(m, hctx->state, hctx_state_name,
  149. ARRAY_SIZE(hctx_state_name));
  150. seq_puts(m, "\n");
  151. return 0;
  152. }
  153. #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
  154. static const char *const hctx_flag_name[] = {
  155. HCTX_FLAG_NAME(TAG_QUEUE_SHARED),
  156. HCTX_FLAG_NAME(STACKING),
  157. HCTX_FLAG_NAME(TAG_HCTX_SHARED),
  158. HCTX_FLAG_NAME(BLOCKING),
  159. HCTX_FLAG_NAME(TAG_RR),
  160. HCTX_FLAG_NAME(NO_SCHED_BY_DEFAULT),
  161. };
  162. #undef HCTX_FLAG_NAME
  163. static int hctx_flags_show(void *data, struct seq_file *m)
  164. {
  165. struct blk_mq_hw_ctx *hctx = data;
  166. BUILD_BUG_ON(ARRAY_SIZE(hctx_flag_name) != ilog2(BLK_MQ_F_MAX));
  167. blk_flags_show(m, hctx->flags, hctx_flag_name,
  168. ARRAY_SIZE(hctx_flag_name));
  169. seq_puts(m, "\n");
  170. return 0;
  171. }
  172. #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
  173. static const char *const cmd_flag_name[] = {
  174. CMD_FLAG_NAME(FAILFAST_DEV),
  175. CMD_FLAG_NAME(FAILFAST_TRANSPORT),
  176. CMD_FLAG_NAME(FAILFAST_DRIVER),
  177. CMD_FLAG_NAME(SYNC),
  178. CMD_FLAG_NAME(META),
  179. CMD_FLAG_NAME(PRIO),
  180. CMD_FLAG_NAME(NOMERGE),
  181. CMD_FLAG_NAME(IDLE),
  182. CMD_FLAG_NAME(INTEGRITY),
  183. CMD_FLAG_NAME(FUA),
  184. CMD_FLAG_NAME(PREFLUSH),
  185. CMD_FLAG_NAME(RAHEAD),
  186. CMD_FLAG_NAME(BACKGROUND),
  187. CMD_FLAG_NAME(NOWAIT),
  188. CMD_FLAG_NAME(POLLED),
  189. CMD_FLAG_NAME(ALLOC_CACHE),
  190. CMD_FLAG_NAME(SWAP),
  191. CMD_FLAG_NAME(DRV),
  192. CMD_FLAG_NAME(FS_PRIVATE),
  193. CMD_FLAG_NAME(ATOMIC),
  194. CMD_FLAG_NAME(NOUNMAP),
  195. };
  196. #undef CMD_FLAG_NAME
  197. #define RQF_NAME(name) [__RQF_##name] = #name
  198. static const char *const rqf_name[] = {
  199. RQF_NAME(STARTED),
  200. RQF_NAME(FLUSH_SEQ),
  201. RQF_NAME(MIXED_MERGE),
  202. RQF_NAME(DONTPREP),
  203. RQF_NAME(SCHED_TAGS),
  204. RQF_NAME(USE_SCHED),
  205. RQF_NAME(FAILED),
  206. RQF_NAME(QUIET),
  207. RQF_NAME(IO_STAT),
  208. RQF_NAME(PM),
  209. RQF_NAME(HASHED),
  210. RQF_NAME(STATS),
  211. RQF_NAME(SPECIAL_PAYLOAD),
  212. RQF_NAME(ZONE_WRITE_PLUGGING),
  213. RQF_NAME(TIMED_OUT),
  214. RQF_NAME(RESV),
  215. };
  216. #undef RQF_NAME
  217. static const char *const blk_mq_rq_state_name_array[] = {
  218. [MQ_RQ_IDLE] = "idle",
  219. [MQ_RQ_IN_FLIGHT] = "in_flight",
  220. [MQ_RQ_COMPLETE] = "complete",
  221. };
  222. static const char *blk_mq_rq_state_name(enum mq_rq_state rq_state)
  223. {
  224. if (WARN_ON_ONCE((unsigned int)rq_state >=
  225. ARRAY_SIZE(blk_mq_rq_state_name_array)))
  226. return "(?)";
  227. return blk_mq_rq_state_name_array[rq_state];
  228. }
  229. int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
  230. {
  231. const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
  232. const enum req_op op = req_op(rq);
  233. const char *op_str = blk_op_str(op);
  234. BUILD_BUG_ON(ARRAY_SIZE(cmd_flag_name) != __REQ_NR_BITS);
  235. BUILD_BUG_ON(ARRAY_SIZE(rqf_name) != __RQF_BITS);
  236. seq_printf(m, "%p {.op=", rq);
  237. if (strcmp(op_str, "UNKNOWN") == 0)
  238. seq_printf(m, "%u", op);
  239. else
  240. seq_printf(m, "%s", op_str);
  241. seq_puts(m, ", .cmd_flags=");
  242. blk_flags_show(m, (__force unsigned int)(rq->cmd_flags & ~REQ_OP_MASK),
  243. cmd_flag_name, ARRAY_SIZE(cmd_flag_name));
  244. seq_puts(m, ", .rq_flags=");
  245. blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
  246. ARRAY_SIZE(rqf_name));
  247. seq_printf(m, ", .state=%s", blk_mq_rq_state_name(blk_mq_rq_state(rq)));
  248. seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
  249. rq->internal_tag);
  250. if (mq_ops->show_rq)
  251. mq_ops->show_rq(m, rq);
  252. seq_puts(m, "}\n");
  253. return 0;
  254. }
  255. EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
  256. int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
  257. {
  258. return __blk_mq_debugfs_rq_show(m, list_entry_rq(v));
  259. }
  260. EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
  261. static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
  262. __acquires(&hctx->lock)
  263. {
  264. struct blk_mq_hw_ctx *hctx = m->private;
  265. spin_lock(&hctx->lock);
  266. return seq_list_start(&hctx->dispatch, *pos);
  267. }
  268. static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
  269. {
  270. struct blk_mq_hw_ctx *hctx = m->private;
  271. return seq_list_next(v, &hctx->dispatch, pos);
  272. }
  273. static void hctx_dispatch_stop(struct seq_file *m, void *v)
  274. __releases(&hctx->lock)
  275. {
  276. struct blk_mq_hw_ctx *hctx = m->private;
  277. spin_unlock(&hctx->lock);
  278. }
  279. static const struct seq_operations hctx_dispatch_seq_ops = {
  280. .start = hctx_dispatch_start,
  281. .next = hctx_dispatch_next,
  282. .stop = hctx_dispatch_stop,
  283. .show = blk_mq_debugfs_rq_show,
  284. };
  285. struct show_busy_params {
  286. struct seq_file *m;
  287. struct blk_mq_hw_ctx *hctx;
  288. };
  289. /*
  290. * Note: the state of a request may change while this function is in progress,
  291. * e.g. due to a concurrent blk_mq_finish_request() call. Returns true to
  292. * keep iterating requests.
  293. */
  294. static bool hctx_show_busy_rq(struct request *rq, void *data)
  295. {
  296. const struct show_busy_params *params = data;
  297. if (rq->mq_hctx == params->hctx)
  298. __blk_mq_debugfs_rq_show(params->m, rq);
  299. return true;
  300. }
  301. static int hctx_busy_show(void *data, struct seq_file *m)
  302. {
  303. struct blk_mq_hw_ctx *hctx = data;
  304. struct show_busy_params params = { .m = m, .hctx = hctx };
  305. int res;
  306. res = mutex_lock_interruptible(&hctx->queue->elevator_lock);
  307. if (res)
  308. return res;
  309. blk_mq_tagset_busy_iter(hctx->queue->tag_set, hctx_show_busy_rq,
  310. &params);
  311. mutex_unlock(&hctx->queue->elevator_lock);
  312. return 0;
  313. }
  314. static const char *const hctx_types[] = {
  315. [HCTX_TYPE_DEFAULT] = "default",
  316. [HCTX_TYPE_READ] = "read",
  317. [HCTX_TYPE_POLL] = "poll",
  318. };
  319. static int hctx_type_show(void *data, struct seq_file *m)
  320. {
  321. struct blk_mq_hw_ctx *hctx = data;
  322. BUILD_BUG_ON(ARRAY_SIZE(hctx_types) != HCTX_MAX_TYPES);
  323. seq_printf(m, "%s\n", hctx_types[hctx->type]);
  324. return 0;
  325. }
  326. static int hctx_ctx_map_show(void *data, struct seq_file *m)
  327. {
  328. struct blk_mq_hw_ctx *hctx = data;
  329. sbitmap_bitmap_show(&hctx->ctx_map, m);
  330. return 0;
  331. }
  332. static void blk_mq_debugfs_tags_show(struct seq_file *m,
  333. struct blk_mq_tags *tags)
  334. {
  335. seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
  336. seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
  337. seq_printf(m, "active_queues=%d\n",
  338. READ_ONCE(tags->active_queues));
  339. seq_puts(m, "\nbitmap_tags:\n");
  340. sbitmap_queue_show(&tags->bitmap_tags, m);
  341. if (tags->nr_reserved_tags) {
  342. seq_puts(m, "\nbreserved_tags:\n");
  343. sbitmap_queue_show(&tags->breserved_tags, m);
  344. }
  345. }
  346. static int hctx_tags_show(void *data, struct seq_file *m)
  347. {
  348. struct blk_mq_hw_ctx *hctx = data;
  349. struct request_queue *q = hctx->queue;
  350. int res;
  351. res = mutex_lock_interruptible(&q->elevator_lock);
  352. if (res)
  353. return res;
  354. if (hctx->tags)
  355. blk_mq_debugfs_tags_show(m, hctx->tags);
  356. mutex_unlock(&q->elevator_lock);
  357. return 0;
  358. }
  359. static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
  360. {
  361. struct blk_mq_hw_ctx *hctx = data;
  362. struct request_queue *q = hctx->queue;
  363. int res;
  364. res = mutex_lock_interruptible(&q->elevator_lock);
  365. if (res)
  366. return res;
  367. if (hctx->tags)
  368. sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
  369. mutex_unlock(&q->elevator_lock);
  370. return 0;
  371. }
  372. static int hctx_sched_tags_show(void *data, struct seq_file *m)
  373. {
  374. struct blk_mq_hw_ctx *hctx = data;
  375. struct request_queue *q = hctx->queue;
  376. int res;
  377. res = mutex_lock_interruptible(&q->elevator_lock);
  378. if (res)
  379. return res;
  380. if (hctx->sched_tags)
  381. blk_mq_debugfs_tags_show(m, hctx->sched_tags);
  382. mutex_unlock(&q->elevator_lock);
  383. return 0;
  384. }
  385. static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
  386. {
  387. struct blk_mq_hw_ctx *hctx = data;
  388. struct request_queue *q = hctx->queue;
  389. int res;
  390. res = mutex_lock_interruptible(&q->elevator_lock);
  391. if (res)
  392. return res;
  393. if (hctx->sched_tags)
  394. sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
  395. mutex_unlock(&q->elevator_lock);
  396. return 0;
  397. }
  398. static int hctx_active_show(void *data, struct seq_file *m)
  399. {
  400. struct blk_mq_hw_ctx *hctx = data;
  401. seq_printf(m, "%d\n", __blk_mq_active_requests(hctx));
  402. return 0;
  403. }
  404. static int hctx_dispatch_busy_show(void *data, struct seq_file *m)
  405. {
  406. struct blk_mq_hw_ctx *hctx = data;
  407. seq_printf(m, "%u\n", hctx->dispatch_busy);
  408. return 0;
  409. }
  410. #define CTX_RQ_SEQ_OPS(name, type) \
  411. static void *ctx_##name##_rq_list_start(struct seq_file *m, loff_t *pos) \
  412. __acquires(&ctx->lock) \
  413. { \
  414. struct blk_mq_ctx *ctx = m->private; \
  415. \
  416. spin_lock(&ctx->lock); \
  417. return seq_list_start(&ctx->rq_lists[type], *pos); \
  418. } \
  419. \
  420. static void *ctx_##name##_rq_list_next(struct seq_file *m, void *v, \
  421. loff_t *pos) \
  422. { \
  423. struct blk_mq_ctx *ctx = m->private; \
  424. \
  425. return seq_list_next(v, &ctx->rq_lists[type], pos); \
  426. } \
  427. \
  428. static void ctx_##name##_rq_list_stop(struct seq_file *m, void *v) \
  429. __releases(&ctx->lock) \
  430. { \
  431. struct blk_mq_ctx *ctx = m->private; \
  432. \
  433. spin_unlock(&ctx->lock); \
  434. } \
  435. \
  436. static const struct seq_operations ctx_##name##_rq_list_seq_ops = { \
  437. .start = ctx_##name##_rq_list_start, \
  438. .next = ctx_##name##_rq_list_next, \
  439. .stop = ctx_##name##_rq_list_stop, \
  440. .show = blk_mq_debugfs_rq_show, \
  441. }
  442. CTX_RQ_SEQ_OPS(default, HCTX_TYPE_DEFAULT);
  443. CTX_RQ_SEQ_OPS(read, HCTX_TYPE_READ);
  444. CTX_RQ_SEQ_OPS(poll, HCTX_TYPE_POLL);
  445. static int blk_mq_debugfs_show(struct seq_file *m, void *v)
  446. {
  447. const struct blk_mq_debugfs_attr *attr = m->private;
  448. void *data = debugfs_get_aux(m->file);
  449. return attr->show(data, m);
  450. }
  451. static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
  452. size_t count, loff_t *ppos)
  453. {
  454. struct seq_file *m = file->private_data;
  455. const struct blk_mq_debugfs_attr *attr = m->private;
  456. void *data = debugfs_get_aux(file);
  457. /*
  458. * Attributes that only implement .seq_ops are read-only and 'attr' is
  459. * the same with 'data' in this case.
  460. */
  461. if (attr == data || !attr->write)
  462. return -EPERM;
  463. return attr->write(data, buf, count, ppos);
  464. }
  465. static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
  466. {
  467. const struct blk_mq_debugfs_attr *attr = inode->i_private;
  468. void *data = debugfs_get_aux(file);
  469. struct seq_file *m;
  470. int ret;
  471. if (attr->seq_ops) {
  472. ret = seq_open(file, attr->seq_ops);
  473. if (!ret) {
  474. m = file->private_data;
  475. m->private = data;
  476. }
  477. return ret;
  478. }
  479. if (WARN_ON_ONCE(!attr->show))
  480. return -EPERM;
  481. return single_open(file, blk_mq_debugfs_show, inode->i_private);
  482. }
  483. static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
  484. {
  485. const struct blk_mq_debugfs_attr *attr = inode->i_private;
  486. if (attr->show)
  487. return single_release(inode, file);
  488. return seq_release(inode, file);
  489. }
  490. static const struct file_operations blk_mq_debugfs_fops = {
  491. .open = blk_mq_debugfs_open,
  492. .read = seq_read,
  493. .write = blk_mq_debugfs_write,
  494. .llseek = seq_lseek,
  495. .release = blk_mq_debugfs_release,
  496. };
  497. static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
  498. {"state", 0400, hctx_state_show},
  499. {"flags", 0400, hctx_flags_show},
  500. {"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
  501. {"busy", 0400, hctx_busy_show},
  502. {"ctx_map", 0400, hctx_ctx_map_show},
  503. {"tags", 0400, hctx_tags_show},
  504. {"tags_bitmap", 0400, hctx_tags_bitmap_show},
  505. {"sched_tags", 0400, hctx_sched_tags_show},
  506. {"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
  507. {"active", 0400, hctx_active_show},
  508. {"dispatch_busy", 0400, hctx_dispatch_busy_show},
  509. {"type", 0400, hctx_type_show},
  510. {},
  511. };
  512. static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
  513. {"default_rq_list", 0400, .seq_ops = &ctx_default_rq_list_seq_ops},
  514. {"read_rq_list", 0400, .seq_ops = &ctx_read_rq_list_seq_ops},
  515. {"poll_rq_list", 0400, .seq_ops = &ctx_poll_rq_list_seq_ops},
  516. {},
  517. };
  518. static void debugfs_create_files(struct request_queue *q, struct dentry *parent,
  519. void *data,
  520. const struct blk_mq_debugfs_attr *attr)
  521. {
  522. lockdep_assert_held(&q->debugfs_mutex);
  523. /*
  524. * debugfs_mutex should not be nested under other locks that can be
  525. * grabbed while queue is frozen.
  526. */
  527. lockdep_assert_not_held(&q->elevator_lock);
  528. lockdep_assert_not_held(&q->rq_qos_mutex);
  529. if (IS_ERR_OR_NULL(parent))
  530. return;
  531. for (; attr->name; attr++)
  532. debugfs_create_file_aux(attr->name, attr->mode, parent,
  533. (void *)attr, data, &blk_mq_debugfs_fops);
  534. }
  535. void blk_mq_debugfs_register(struct request_queue *q)
  536. {
  537. struct blk_mq_hw_ctx *hctx;
  538. unsigned long i;
  539. debugfs_create_files(q, q->debugfs_dir, q, blk_mq_debugfs_queue_attrs);
  540. queue_for_each_hw_ctx(q, hctx, i) {
  541. if (!hctx->debugfs_dir)
  542. blk_mq_debugfs_register_hctx(q, hctx);
  543. }
  544. blk_mq_debugfs_register_rq_qos(q);
  545. }
  546. static void blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,
  547. struct blk_mq_ctx *ctx)
  548. {
  549. struct dentry *ctx_dir;
  550. char name[20];
  551. snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
  552. ctx_dir = debugfs_create_dir(name, hctx->debugfs_dir);
  553. debugfs_create_files(hctx->queue, ctx_dir, ctx,
  554. blk_mq_debugfs_ctx_attrs);
  555. }
  556. void blk_mq_debugfs_register_hctx(struct request_queue *q,
  557. struct blk_mq_hw_ctx *hctx)
  558. {
  559. struct blk_mq_ctx *ctx;
  560. char name[20];
  561. int i;
  562. if (!q->debugfs_dir)
  563. return;
  564. snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
  565. hctx->debugfs_dir = debugfs_create_dir(name, q->debugfs_dir);
  566. debugfs_create_files(q, hctx->debugfs_dir, hctx,
  567. blk_mq_debugfs_hctx_attrs);
  568. hctx_for_each_ctx(hctx, ctx, i)
  569. blk_mq_debugfs_register_ctx(hctx, ctx);
  570. }
  571. void blk_mq_debugfs_unregister_hctx(struct blk_mq_hw_ctx *hctx)
  572. {
  573. if (!hctx->queue->debugfs_dir)
  574. return;
  575. debugfs_remove_recursive(hctx->debugfs_dir);
  576. hctx->sched_debugfs_dir = NULL;
  577. hctx->debugfs_dir = NULL;
  578. }
  579. void blk_mq_debugfs_register_hctxs(struct request_queue *q)
  580. {
  581. struct blk_mq_hw_ctx *hctx;
  582. unsigned int memflags;
  583. unsigned long i;
  584. memflags = blk_debugfs_lock(q);
  585. queue_for_each_hw_ctx(q, hctx, i)
  586. blk_mq_debugfs_register_hctx(q, hctx);
  587. blk_debugfs_unlock(q, memflags);
  588. }
  589. void blk_mq_debugfs_unregister_hctxs(struct request_queue *q)
  590. {
  591. struct blk_mq_hw_ctx *hctx;
  592. unsigned long i;
  593. queue_for_each_hw_ctx(q, hctx, i)
  594. blk_mq_debugfs_unregister_hctx(hctx);
  595. }
  596. void blk_mq_debugfs_register_sched(struct request_queue *q)
  597. {
  598. struct elevator_type *e = q->elevator->type;
  599. lockdep_assert_held(&q->debugfs_mutex);
  600. /*
  601. * If the parent directory has not been created yet, return, we will be
  602. * called again later on and the directory/files will be created then.
  603. */
  604. if (!q->debugfs_dir)
  605. return;
  606. if (!e->queue_debugfs_attrs)
  607. return;
  608. q->sched_debugfs_dir = debugfs_create_dir("sched", q->debugfs_dir);
  609. debugfs_create_files(q, q->sched_debugfs_dir, q, e->queue_debugfs_attrs);
  610. }
  611. void blk_mq_debugfs_unregister_sched(struct request_queue *q)
  612. {
  613. lockdep_assert_held(&q->debugfs_mutex);
  614. debugfs_remove_recursive(q->sched_debugfs_dir);
  615. q->sched_debugfs_dir = NULL;
  616. }
  617. static const char *rq_qos_id_to_name(enum rq_qos_id id)
  618. {
  619. switch (id) {
  620. case RQ_QOS_WBT:
  621. return "wbt";
  622. case RQ_QOS_LATENCY:
  623. return "latency";
  624. case RQ_QOS_COST:
  625. return "cost";
  626. }
  627. return "unknown";
  628. }
  629. static void blk_mq_debugfs_register_rqos(struct rq_qos *rqos)
  630. {
  631. struct request_queue *q = rqos->disk->queue;
  632. const char *dir_name = rq_qos_id_to_name(rqos->id);
  633. lockdep_assert_held(&q->debugfs_mutex);
  634. if (rqos->debugfs_dir || !rqos->ops->debugfs_attrs)
  635. return;
  636. if (!q->rqos_debugfs_dir)
  637. q->rqos_debugfs_dir = debugfs_create_dir("rqos",
  638. q->debugfs_dir);
  639. rqos->debugfs_dir = debugfs_create_dir(dir_name, q->rqos_debugfs_dir);
  640. debugfs_create_files(q, rqos->debugfs_dir, rqos,
  641. rqos->ops->debugfs_attrs);
  642. }
  643. void blk_mq_debugfs_register_rq_qos(struct request_queue *q)
  644. {
  645. lockdep_assert_held(&q->debugfs_mutex);
  646. if (q->rq_qos) {
  647. struct rq_qos *rqos = q->rq_qos;
  648. while (rqos) {
  649. blk_mq_debugfs_register_rqos(rqos);
  650. rqos = rqos->next;
  651. }
  652. }
  653. }
  654. void blk_mq_debugfs_register_sched_hctx(struct request_queue *q,
  655. struct blk_mq_hw_ctx *hctx)
  656. {
  657. struct elevator_type *e = q->elevator->type;
  658. lockdep_assert_held(&q->debugfs_mutex);
  659. /*
  660. * If the parent debugfs directory has not been created yet, return;
  661. * We will be called again later on with appropriate parent debugfs
  662. * directory from blk_register_queue()
  663. */
  664. if (!hctx->debugfs_dir)
  665. return;
  666. if (!e->hctx_debugfs_attrs)
  667. return;
  668. hctx->sched_debugfs_dir = debugfs_create_dir("sched",
  669. hctx->debugfs_dir);
  670. debugfs_create_files(q, hctx->sched_debugfs_dir, hctx,
  671. e->hctx_debugfs_attrs);
  672. }
  673. void blk_mq_debugfs_unregister_sched_hctx(struct blk_mq_hw_ctx *hctx)
  674. {
  675. lockdep_assert_held(&hctx->queue->debugfs_mutex);
  676. if (!hctx->queue->debugfs_dir)
  677. return;
  678. debugfs_remove_recursive(hctx->sched_debugfs_dir);
  679. hctx->sched_debugfs_dir = NULL;
  680. }