2 * Copyright (C) 2017 Facebook
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 #include <linux/kernel.h>
18 #include <linux/blkdev.h>
19 #include <linux/debugfs.h>
21 #include <linux/blk-mq.h>
24 #include "blk-mq-debugfs.h"
25 #include "blk-mq-tag.h"
27 struct blk_mq_debugfs_attr {
30 int (*show)(void *, struct seq_file *);
31 ssize_t (*write)(void *, const char __user *, size_t, loff_t *);
32 /* Set either .show or .seq_ops. */
33 const struct seq_operations *seq_ops;
36 static int blk_flags_show(struct seq_file *m, const unsigned long flags,
37 const char *const *flag_name, int flag_name_count)
42 for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
43 if (!(flags & BIT(i)))
48 if (i < flag_name_count && flag_name[i])
49 seq_puts(m, flag_name[i]);
51 seq_printf(m, "%d", i);
56 #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
57 static const char *const blk_queue_flag_name[] = {
58 QUEUE_FLAG_NAME(QUEUED),
59 QUEUE_FLAG_NAME(STOPPED),
60 QUEUE_FLAG_NAME(SYNCFULL),
61 QUEUE_FLAG_NAME(ASYNCFULL),
62 QUEUE_FLAG_NAME(DYING),
63 QUEUE_FLAG_NAME(BYPASS),
64 QUEUE_FLAG_NAME(BIDI),
65 QUEUE_FLAG_NAME(NOMERGES),
66 QUEUE_FLAG_NAME(SAME_COMP),
67 QUEUE_FLAG_NAME(FAIL_IO),
68 QUEUE_FLAG_NAME(STACKABLE),
69 QUEUE_FLAG_NAME(NONROT),
70 QUEUE_FLAG_NAME(IO_STAT),
71 QUEUE_FLAG_NAME(DISCARD),
72 QUEUE_FLAG_NAME(NOXMERGES),
73 QUEUE_FLAG_NAME(ADD_RANDOM),
74 QUEUE_FLAG_NAME(SECERASE),
75 QUEUE_FLAG_NAME(SAME_FORCE),
76 QUEUE_FLAG_NAME(DEAD),
77 QUEUE_FLAG_NAME(INIT_DONE),
78 QUEUE_FLAG_NAME(NO_SG_MERGE),
79 QUEUE_FLAG_NAME(POLL),
82 QUEUE_FLAG_NAME(FLUSH_NQ),
84 QUEUE_FLAG_NAME(STATS),
85 QUEUE_FLAG_NAME(POLL_STATS),
86 QUEUE_FLAG_NAME(REGISTERED),
88 #undef QUEUE_FLAG_NAME
90 static int queue_state_show(void *data, struct seq_file *m)
92 struct request_queue *q = data;
94 blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
95 ARRAY_SIZE(blk_queue_flag_name));
100 static ssize_t queue_state_write(void *data, const char __user *buf,
101 size_t count, loff_t *ppos)
103 struct request_queue *q = data;
104 char opbuf[16] = { }, *op;
107 * The "state" attribute is removed after blk_cleanup_queue() has called
108 * blk_mq_free_queue(). Return if QUEUE_FLAG_DEAD has been set to avoid
109 * triggering a use-after-free.
111 if (blk_queue_dead(q))
114 if (count >= sizeof(opbuf)) {
115 pr_err("%s: operation too long\n", __func__);
119 if (copy_from_user(opbuf, buf, count))
121 op = strstrip(opbuf);
122 if (strcmp(op, "run") == 0) {
123 blk_mq_run_hw_queues(q, true);
124 } else if (strcmp(op, "start") == 0) {
125 blk_mq_start_stopped_hw_queues(q, true);
127 pr_err("%s: unsupported operation '%s'\n", __func__, op);
129 pr_err("%s: use either 'run' or 'start'\n", __func__);
135 static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
137 if (stat->nr_samples) {
138 seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
139 stat->nr_samples, stat->mean, stat->min, stat->max);
141 seq_puts(m, "samples=0");
145 static int queue_poll_stat_show(void *data, struct seq_file *m)
147 struct request_queue *q = data;
150 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
151 seq_printf(m, "read (%d Bytes): ", 1 << (9+bucket));
152 print_stat(m, &q->poll_stat[2*bucket]);
155 seq_printf(m, "write (%d Bytes): ", 1 << (9+bucket));
156 print_stat(m, &q->poll_stat[2*bucket+1]);
162 #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
163 static const char *const hctx_state_name[] = {
164 HCTX_STATE_NAME(STOPPED),
165 HCTX_STATE_NAME(TAG_ACTIVE),
166 HCTX_STATE_NAME(SCHED_RESTART),
167 HCTX_STATE_NAME(TAG_WAITING),
168 HCTX_STATE_NAME(START_ON_RUN),
170 #undef HCTX_STATE_NAME
172 static int hctx_state_show(void *data, struct seq_file *m)
174 struct blk_mq_hw_ctx *hctx = data;
176 blk_flags_show(m, hctx->state, hctx_state_name,
177 ARRAY_SIZE(hctx_state_name));
182 #define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
183 static const char *const alloc_policy_name[] = {
184 BLK_TAG_ALLOC_NAME(FIFO),
185 BLK_TAG_ALLOC_NAME(RR),
187 #undef BLK_TAG_ALLOC_NAME
189 #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
190 static const char *const hctx_flag_name[] = {
191 HCTX_FLAG_NAME(SHOULD_MERGE),
192 HCTX_FLAG_NAME(TAG_SHARED),
193 HCTX_FLAG_NAME(SG_MERGE),
194 HCTX_FLAG_NAME(BLOCKING),
195 HCTX_FLAG_NAME(NO_SCHED),
197 #undef HCTX_FLAG_NAME
199 static int hctx_flags_show(void *data, struct seq_file *m)
201 struct blk_mq_hw_ctx *hctx = data;
202 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
204 seq_puts(m, "alloc_policy=");
205 if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
206 alloc_policy_name[alloc_policy])
207 seq_puts(m, alloc_policy_name[alloc_policy]);
209 seq_printf(m, "%d", alloc_policy);
212 hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
213 hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
218 #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
219 static const char *const op_name[] = {
223 REQ_OP_NAME(DISCARD),
224 REQ_OP_NAME(ZONE_REPORT),
225 REQ_OP_NAME(SECURE_ERASE),
226 REQ_OP_NAME(ZONE_RESET),
227 REQ_OP_NAME(WRITE_SAME),
228 REQ_OP_NAME(WRITE_ZEROES),
229 REQ_OP_NAME(SCSI_IN),
230 REQ_OP_NAME(SCSI_OUT),
232 REQ_OP_NAME(DRV_OUT),
236 #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
237 static const char *const cmd_flag_name[] = {
238 CMD_FLAG_NAME(FAILFAST_DEV),
239 CMD_FLAG_NAME(FAILFAST_TRANSPORT),
240 CMD_FLAG_NAME(FAILFAST_DRIVER),
244 CMD_FLAG_NAME(NOMERGE),
246 CMD_FLAG_NAME(INTEGRITY),
248 CMD_FLAG_NAME(PREFLUSH),
249 CMD_FLAG_NAME(RAHEAD),
250 CMD_FLAG_NAME(BACKGROUND),
251 CMD_FLAG_NAME(NOUNMAP),
255 #define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
256 static const char *const rqf_name[] = {
260 RQF_NAME(SOFTBARRIER),
262 RQF_NAME(MIXED_MERGE),
263 RQF_NAME(MQ_INFLIGHT),
275 RQF_NAME(SPECIAL_PAYLOAD),
279 static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
281 struct request *rq = list_entry_rq(v);
282 const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
283 const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
285 seq_printf(m, "%p {.op=", rq);
286 if (op < ARRAY_SIZE(op_name) && op_name[op])
287 seq_printf(m, "%s", op_name[op]);
289 seq_printf(m, "%d", op);
290 seq_puts(m, ", .cmd_flags=");
291 blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
292 ARRAY_SIZE(cmd_flag_name));
293 seq_puts(m, ", .rq_flags=");
294 blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
295 ARRAY_SIZE(rqf_name));
296 seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
299 mq_ops->show_rq(m, rq);
304 static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
305 __acquires(&hctx->lock)
307 struct blk_mq_hw_ctx *hctx = m->private;
309 spin_lock(&hctx->lock);
310 return seq_list_start(&hctx->dispatch, *pos);
313 static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
315 struct blk_mq_hw_ctx *hctx = m->private;
317 return seq_list_next(v, &hctx->dispatch, pos);
320 static void hctx_dispatch_stop(struct seq_file *m, void *v)
321 __releases(&hctx->lock)
323 struct blk_mq_hw_ctx *hctx = m->private;
325 spin_unlock(&hctx->lock);
328 static const struct seq_operations hctx_dispatch_seq_ops = {
329 .start = hctx_dispatch_start,
330 .next = hctx_dispatch_next,
331 .stop = hctx_dispatch_stop,
332 .show = blk_mq_debugfs_rq_show,
335 static int hctx_ctx_map_show(void *data, struct seq_file *m)
337 struct blk_mq_hw_ctx *hctx = data;
339 sbitmap_bitmap_show(&hctx->ctx_map, m);
343 static void blk_mq_debugfs_tags_show(struct seq_file *m,
344 struct blk_mq_tags *tags)
346 seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
347 seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
348 seq_printf(m, "active_queues=%d\n",
349 atomic_read(&tags->active_queues));
351 seq_puts(m, "\nbitmap_tags:\n");
352 sbitmap_queue_show(&tags->bitmap_tags, m);
354 if (tags->nr_reserved_tags) {
355 seq_puts(m, "\nbreserved_tags:\n");
356 sbitmap_queue_show(&tags->breserved_tags, m);
360 static int hctx_tags_show(void *data, struct seq_file *m)
362 struct blk_mq_hw_ctx *hctx = data;
363 struct request_queue *q = hctx->queue;
366 res = mutex_lock_interruptible(&q->sysfs_lock);
370 blk_mq_debugfs_tags_show(m, hctx->tags);
371 mutex_unlock(&q->sysfs_lock);
377 static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
379 struct blk_mq_hw_ctx *hctx = data;
380 struct request_queue *q = hctx->queue;
383 res = mutex_lock_interruptible(&q->sysfs_lock);
387 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
388 mutex_unlock(&q->sysfs_lock);
394 static int hctx_sched_tags_show(void *data, struct seq_file *m)
396 struct blk_mq_hw_ctx *hctx = data;
397 struct request_queue *q = hctx->queue;
400 res = mutex_lock_interruptible(&q->sysfs_lock);
403 if (hctx->sched_tags)
404 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
405 mutex_unlock(&q->sysfs_lock);
411 static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
413 struct blk_mq_hw_ctx *hctx = data;
414 struct request_queue *q = hctx->queue;
417 res = mutex_lock_interruptible(&q->sysfs_lock);
420 if (hctx->sched_tags)
421 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
422 mutex_unlock(&q->sysfs_lock);
428 static int hctx_io_poll_show(void *data, struct seq_file *m)
430 struct blk_mq_hw_ctx *hctx = data;
432 seq_printf(m, "considered=%lu\n", hctx->poll_considered);
433 seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
434 seq_printf(m, "success=%lu\n", hctx->poll_success);
438 static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
439 size_t count, loff_t *ppos)
441 struct blk_mq_hw_ctx *hctx = data;
443 hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
447 static int hctx_dispatched_show(void *data, struct seq_file *m)
449 struct blk_mq_hw_ctx *hctx = data;
452 seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
454 for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
455 unsigned int d = 1U << (i - 1);
457 seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
460 seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
464 static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
465 size_t count, loff_t *ppos)
467 struct blk_mq_hw_ctx *hctx = data;
470 for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
471 hctx->dispatched[i] = 0;
475 static int hctx_queued_show(void *data, struct seq_file *m)
477 struct blk_mq_hw_ctx *hctx = data;
479 seq_printf(m, "%lu\n", hctx->queued);
483 static ssize_t hctx_queued_write(void *data, const char __user *buf,
484 size_t count, loff_t *ppos)
486 struct blk_mq_hw_ctx *hctx = data;
492 static int hctx_run_show(void *data, struct seq_file *m)
494 struct blk_mq_hw_ctx *hctx = data;
496 seq_printf(m, "%lu\n", hctx->run);
500 static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
503 struct blk_mq_hw_ctx *hctx = data;
509 static int hctx_active_show(void *data, struct seq_file *m)
511 struct blk_mq_hw_ctx *hctx = data;
513 seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
517 static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
518 __acquires(&ctx->lock)
520 struct blk_mq_ctx *ctx = m->private;
522 spin_lock(&ctx->lock);
523 return seq_list_start(&ctx->rq_list, *pos);
526 static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
528 struct blk_mq_ctx *ctx = m->private;
530 return seq_list_next(v, &ctx->rq_list, pos);
533 static void ctx_rq_list_stop(struct seq_file *m, void *v)
534 __releases(&ctx->lock)
536 struct blk_mq_ctx *ctx = m->private;
538 spin_unlock(&ctx->lock);
541 static const struct seq_operations ctx_rq_list_seq_ops = {
542 .start = ctx_rq_list_start,
543 .next = ctx_rq_list_next,
544 .stop = ctx_rq_list_stop,
545 .show = blk_mq_debugfs_rq_show,
547 static int ctx_dispatched_show(void *data, struct seq_file *m)
549 struct blk_mq_ctx *ctx = data;
551 seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
555 static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
556 size_t count, loff_t *ppos)
558 struct blk_mq_ctx *ctx = data;
560 ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
564 static int ctx_merged_show(void *data, struct seq_file *m)
566 struct blk_mq_ctx *ctx = data;
568 seq_printf(m, "%lu\n", ctx->rq_merged);
572 static ssize_t ctx_merged_write(void *data, const char __user *buf,
573 size_t count, loff_t *ppos)
575 struct blk_mq_ctx *ctx = data;
581 static int ctx_completed_show(void *data, struct seq_file *m)
583 struct blk_mq_ctx *ctx = data;
585 seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
589 static ssize_t ctx_completed_write(void *data, const char __user *buf,
590 size_t count, loff_t *ppos)
592 struct blk_mq_ctx *ctx = data;
594 ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
598 static int blk_mq_debugfs_show(struct seq_file *m, void *v)
600 const struct blk_mq_debugfs_attr *attr = m->private;
601 void *data = d_inode(m->file->f_path.dentry->d_parent)->i_private;
603 return attr->show(data, m);
606 static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
607 size_t count, loff_t *ppos)
609 struct seq_file *m = file->private_data;
610 const struct blk_mq_debugfs_attr *attr = m->private;
611 void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
616 return attr->write(data, buf, count, ppos);
619 static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
621 const struct blk_mq_debugfs_attr *attr = inode->i_private;
622 void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
627 ret = seq_open(file, attr->seq_ops);
629 m = file->private_data;
635 if (WARN_ON_ONCE(!attr->show))
638 return single_open(file, blk_mq_debugfs_show, inode->i_private);
641 static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
643 const struct blk_mq_debugfs_attr *attr = inode->i_private;
646 return single_release(inode, file);
648 return seq_release(inode, file);
651 const struct file_operations blk_mq_debugfs_fops = {
652 .open = blk_mq_debugfs_open,
654 .write = blk_mq_debugfs_write,
656 .release = blk_mq_debugfs_release,
659 static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
660 {"poll_stat", 0400, queue_poll_stat_show},
661 {"state", 0600, queue_state_show, queue_state_write},
665 static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
666 {"state", 0400, hctx_state_show},
667 {"flags", 0400, hctx_flags_show},
668 {"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
669 {"ctx_map", 0400, hctx_ctx_map_show},
670 {"tags", 0400, hctx_tags_show},
671 {"tags_bitmap", 0400, hctx_tags_bitmap_show},
672 {"sched_tags", 0400, hctx_sched_tags_show},
673 {"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
674 {"io_poll", 0600, hctx_io_poll_show, hctx_io_poll_write},
675 {"dispatched", 0600, hctx_dispatched_show, hctx_dispatched_write},
676 {"queued", 0600, hctx_queued_show, hctx_queued_write},
677 {"run", 0600, hctx_run_show, hctx_run_write},
678 {"active", 0400, hctx_active_show},
682 static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
683 {"rq_list", 0400, .seq_ops = &ctx_rq_list_seq_ops},
684 {"dispatched", 0600, ctx_dispatched_show, ctx_dispatched_write},
685 {"merged", 0600, ctx_merged_show, ctx_merged_write},
686 {"completed", 0600, ctx_completed_show, ctx_completed_write},
690 int blk_mq_debugfs_register(struct request_queue *q)
692 if (!blk_debugfs_root)
695 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
700 if (blk_mq_debugfs_register_mq(q))
706 blk_mq_debugfs_unregister(q);
710 void blk_mq_debugfs_unregister(struct request_queue *q)
712 debugfs_remove_recursive(q->debugfs_dir);
713 q->mq_debugfs_dir = NULL;
714 q->debugfs_dir = NULL;
717 static bool debugfs_create_files(struct dentry *parent, void *data,
718 const struct blk_mq_debugfs_attr *attr)
720 d_inode(parent)->i_private = data;
722 for (; attr->name; attr++) {
723 if (!debugfs_create_file(attr->name, attr->mode, parent,
724 (void *)attr, &blk_mq_debugfs_fops))
730 static int blk_mq_debugfs_register_ctx(struct request_queue *q,
731 struct blk_mq_ctx *ctx,
732 struct dentry *hctx_dir)
734 struct dentry *ctx_dir;
737 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
738 ctx_dir = debugfs_create_dir(name, hctx_dir);
742 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
748 static int blk_mq_debugfs_register_hctx(struct request_queue *q,
749 struct blk_mq_hw_ctx *hctx)
751 struct blk_mq_ctx *ctx;
752 struct dentry *hctx_dir;
756 snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
757 hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
761 if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
764 hctx_for_each_ctx(hctx, ctx, i) {
765 if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
772 int blk_mq_debugfs_register_mq(struct request_queue *q)
774 struct blk_mq_hw_ctx *hctx;
780 q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
781 if (!q->mq_debugfs_dir)
784 if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
787 queue_for_each_hw_ctx(q, hctx, i) {
788 if (blk_mq_debugfs_register_hctx(q, hctx))
795 blk_mq_debugfs_unregister_mq(q);
799 void blk_mq_debugfs_unregister_mq(struct request_queue *q)
801 debugfs_remove_recursive(q->mq_debugfs_dir);
802 q->mq_debugfs_dir = NULL;