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-tag.h"
26 struct blk_mq_debugfs_attr
{
29 const struct file_operations
*fops
;
32 static int blk_mq_debugfs_seq_open(struct inode
*inode
, struct file
*file
,
33 const struct seq_operations
*ops
)
38 ret
= seq_open(file
, ops
);
40 m
= file
->private_data
;
41 m
->private = inode
->i_private
;
46 static int hctx_state_show(struct seq_file
*m
, void *v
)
48 struct blk_mq_hw_ctx
*hctx
= m
->private;
50 seq_printf(m
, "0x%lx\n", hctx
->state
);
54 static int hctx_state_open(struct inode
*inode
, struct file
*file
)
56 return single_open(file
, hctx_state_show
, inode
->i_private
);
59 static const struct file_operations hctx_state_fops
= {
60 .open
= hctx_state_open
,
63 .release
= single_release
,
66 static int hctx_flags_show(struct seq_file
*m
, void *v
)
68 struct blk_mq_hw_ctx
*hctx
= m
->private;
70 seq_printf(m
, "0x%lx\n", hctx
->flags
);
74 static int hctx_flags_open(struct inode
*inode
, struct file
*file
)
76 return single_open(file
, hctx_flags_show
, inode
->i_private
);
79 static const struct file_operations hctx_flags_fops
= {
80 .open
= hctx_flags_open
,
83 .release
= single_release
,
86 static int blk_mq_debugfs_rq_show(struct seq_file
*m
, void *v
)
88 struct request
*rq
= list_entry_rq(v
);
90 seq_printf(m
, "%p {.cmd_flags=0x%x, .rq_flags=0x%x, .tag=%d, .internal_tag=%d}\n",
91 rq
, rq
->cmd_flags
, (__force
unsigned int)rq
->rq_flags
,
92 rq
->tag
, rq
->internal_tag
);
96 static void *hctx_dispatch_start(struct seq_file
*m
, loff_t
*pos
)
97 __acquires(&hctx
->lock
)
99 struct blk_mq_hw_ctx
*hctx
= m
->private;
101 spin_lock(&hctx
->lock
);
102 return seq_list_start(&hctx
->dispatch
, *pos
);
105 static void *hctx_dispatch_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
107 struct blk_mq_hw_ctx
*hctx
= m
->private;
109 return seq_list_next(v
, &hctx
->dispatch
, pos
);
112 static void hctx_dispatch_stop(struct seq_file
*m
, void *v
)
113 __releases(&hctx
->lock
)
115 struct blk_mq_hw_ctx
*hctx
= m
->private;
117 spin_unlock(&hctx
->lock
);
120 static const struct seq_operations hctx_dispatch_seq_ops
= {
121 .start
= hctx_dispatch_start
,
122 .next
= hctx_dispatch_next
,
123 .stop
= hctx_dispatch_stop
,
124 .show
= blk_mq_debugfs_rq_show
,
127 static int hctx_dispatch_open(struct inode
*inode
, struct file
*file
)
129 return blk_mq_debugfs_seq_open(inode
, file
, &hctx_dispatch_seq_ops
);
132 static const struct file_operations hctx_dispatch_fops
= {
133 .open
= hctx_dispatch_open
,
136 .release
= seq_release
,
139 static int hctx_ctx_map_show(struct seq_file
*m
, void *v
)
141 struct blk_mq_hw_ctx
*hctx
= m
->private;
143 sbitmap_bitmap_show(&hctx
->ctx_map
, m
);
147 static int hctx_ctx_map_open(struct inode
*inode
, struct file
*file
)
149 return single_open(file
, hctx_ctx_map_show
, inode
->i_private
);
152 static const struct file_operations hctx_ctx_map_fops
= {
153 .open
= hctx_ctx_map_open
,
156 .release
= single_release
,
159 static void blk_mq_debugfs_tags_show(struct seq_file
*m
,
160 struct blk_mq_tags
*tags
)
162 seq_printf(m
, "nr_tags=%u\n", tags
->nr_tags
);
163 seq_printf(m
, "nr_reserved_tags=%u\n", tags
->nr_reserved_tags
);
164 seq_printf(m
, "active_queues=%d\n",
165 atomic_read(&tags
->active_queues
));
167 seq_puts(m
, "\nbitmap_tags:\n");
168 sbitmap_queue_show(&tags
->bitmap_tags
, m
);
170 if (tags
->nr_reserved_tags
) {
171 seq_puts(m
, "\nbreserved_tags:\n");
172 sbitmap_queue_show(&tags
->breserved_tags
, m
);
176 static int hctx_tags_show(struct seq_file
*m
, void *v
)
178 struct blk_mq_hw_ctx
*hctx
= m
->private;
179 struct request_queue
*q
= hctx
->queue
;
182 res
= mutex_lock_interruptible(&q
->sysfs_lock
);
186 blk_mq_debugfs_tags_show(m
, hctx
->tags
);
187 mutex_unlock(&q
->sysfs_lock
);
193 static int hctx_tags_open(struct inode
*inode
, struct file
*file
)
195 return single_open(file
, hctx_tags_show
, inode
->i_private
);
198 static const struct file_operations hctx_tags_fops
= {
199 .open
= hctx_tags_open
,
202 .release
= single_release
,
205 static int hctx_tags_bitmap_show(struct seq_file
*m
, void *v
)
207 struct blk_mq_hw_ctx
*hctx
= m
->private;
208 struct request_queue
*q
= hctx
->queue
;
211 res
= mutex_lock_interruptible(&q
->sysfs_lock
);
215 sbitmap_bitmap_show(&hctx
->tags
->bitmap_tags
.sb
, m
);
216 mutex_unlock(&q
->sysfs_lock
);
222 static int hctx_tags_bitmap_open(struct inode
*inode
, struct file
*file
)
224 return single_open(file
, hctx_tags_bitmap_show
, inode
->i_private
);
227 static const struct file_operations hctx_tags_bitmap_fops
= {
228 .open
= hctx_tags_bitmap_open
,
231 .release
= single_release
,
234 static int hctx_sched_tags_show(struct seq_file
*m
, void *v
)
236 struct blk_mq_hw_ctx
*hctx
= m
->private;
237 struct request_queue
*q
= hctx
->queue
;
240 res
= mutex_lock_interruptible(&q
->sysfs_lock
);
243 if (hctx
->sched_tags
)
244 blk_mq_debugfs_tags_show(m
, hctx
->sched_tags
);
245 mutex_unlock(&q
->sysfs_lock
);
251 static int hctx_sched_tags_open(struct inode
*inode
, struct file
*file
)
253 return single_open(file
, hctx_sched_tags_show
, inode
->i_private
);
256 static const struct file_operations hctx_sched_tags_fops
= {
257 .open
= hctx_sched_tags_open
,
260 .release
= single_release
,
263 static int hctx_sched_tags_bitmap_show(struct seq_file
*m
, void *v
)
265 struct blk_mq_hw_ctx
*hctx
= m
->private;
266 struct request_queue
*q
= hctx
->queue
;
269 res
= mutex_lock_interruptible(&q
->sysfs_lock
);
272 if (hctx
->sched_tags
)
273 sbitmap_bitmap_show(&hctx
->sched_tags
->bitmap_tags
.sb
, m
);
274 mutex_unlock(&q
->sysfs_lock
);
280 static int hctx_sched_tags_bitmap_open(struct inode
*inode
, struct file
*file
)
282 return single_open(file
, hctx_sched_tags_bitmap_show
, inode
->i_private
);
285 static const struct file_operations hctx_sched_tags_bitmap_fops
= {
286 .open
= hctx_sched_tags_bitmap_open
,
289 .release
= single_release
,
292 static int hctx_io_poll_show(struct seq_file
*m
, void *v
)
294 struct blk_mq_hw_ctx
*hctx
= m
->private;
296 seq_printf(m
, "considered=%lu\n", hctx
->poll_considered
);
297 seq_printf(m
, "invoked=%lu\n", hctx
->poll_invoked
);
298 seq_printf(m
, "success=%lu\n", hctx
->poll_success
);
302 static int hctx_io_poll_open(struct inode
*inode
, struct file
*file
)
304 return single_open(file
, hctx_io_poll_show
, inode
->i_private
);
307 static ssize_t
hctx_io_poll_write(struct file
*file
, const char __user
*buf
,
308 size_t count
, loff_t
*ppos
)
310 struct seq_file
*m
= file
->private_data
;
311 struct blk_mq_hw_ctx
*hctx
= m
->private;
313 hctx
->poll_considered
= hctx
->poll_invoked
= hctx
->poll_success
= 0;
317 static const struct file_operations hctx_io_poll_fops
= {
318 .open
= hctx_io_poll_open
,
320 .write
= hctx_io_poll_write
,
322 .release
= single_release
,
325 static void print_stat(struct seq_file
*m
, struct blk_rq_stat
*stat
)
327 seq_printf(m
, "samples=%d, mean=%lld, min=%llu, max=%llu",
328 stat
->nr_samples
, stat
->mean
, stat
->min
, stat
->max
);
331 static int hctx_stats_show(struct seq_file
*m
, void *v
)
333 struct blk_mq_hw_ctx
*hctx
= m
->private;
334 struct blk_rq_stat stat
[2];
336 blk_stat_init(&stat
[BLK_STAT_READ
]);
337 blk_stat_init(&stat
[BLK_STAT_WRITE
]);
339 blk_hctx_stat_get(hctx
, stat
);
341 seq_puts(m
, "read: ");
342 print_stat(m
, &stat
[BLK_STAT_READ
]);
345 seq_puts(m
, "write: ");
346 print_stat(m
, &stat
[BLK_STAT_WRITE
]);
351 static int hctx_stats_open(struct inode
*inode
, struct file
*file
)
353 return single_open(file
, hctx_stats_show
, inode
->i_private
);
356 static ssize_t
hctx_stats_write(struct file
*file
, const char __user
*buf
,
357 size_t count
, loff_t
*ppos
)
359 struct seq_file
*m
= file
->private_data
;
360 struct blk_mq_hw_ctx
*hctx
= m
->private;
361 struct blk_mq_ctx
*ctx
;
364 hctx_for_each_ctx(hctx
, ctx
, i
) {
365 blk_stat_init(&ctx
->stat
[BLK_STAT_READ
]);
366 blk_stat_init(&ctx
->stat
[BLK_STAT_WRITE
]);
371 static const struct file_operations hctx_stats_fops
= {
372 .open
= hctx_stats_open
,
374 .write
= hctx_stats_write
,
376 .release
= single_release
,
379 static int hctx_dispatched_show(struct seq_file
*m
, void *v
)
381 struct blk_mq_hw_ctx
*hctx
= m
->private;
384 seq_printf(m
, "%8u\t%lu\n", 0U, hctx
->dispatched
[0]);
386 for (i
= 1; i
< BLK_MQ_MAX_DISPATCH_ORDER
- 1; i
++) {
387 unsigned int d
= 1U << (i
- 1);
389 seq_printf(m
, "%8u\t%lu\n", d
, hctx
->dispatched
[i
]);
392 seq_printf(m
, "%8u+\t%lu\n", 1U << (i
- 1), hctx
->dispatched
[i
]);
396 static int hctx_dispatched_open(struct inode
*inode
, struct file
*file
)
398 return single_open(file
, hctx_dispatched_show
, inode
->i_private
);
401 static ssize_t
hctx_dispatched_write(struct file
*file
, const char __user
*buf
,
402 size_t count
, loff_t
*ppos
)
404 struct seq_file
*m
= file
->private_data
;
405 struct blk_mq_hw_ctx
*hctx
= m
->private;
408 for (i
= 0; i
< BLK_MQ_MAX_DISPATCH_ORDER
; i
++)
409 hctx
->dispatched
[i
] = 0;
413 static const struct file_operations hctx_dispatched_fops
= {
414 .open
= hctx_dispatched_open
,
416 .write
= hctx_dispatched_write
,
418 .release
= single_release
,
421 static int hctx_queued_show(struct seq_file
*m
, void *v
)
423 struct blk_mq_hw_ctx
*hctx
= m
->private;
425 seq_printf(m
, "%lu\n", hctx
->queued
);
429 static int hctx_queued_open(struct inode
*inode
, struct file
*file
)
431 return single_open(file
, hctx_queued_show
, inode
->i_private
);
434 static ssize_t
hctx_queued_write(struct file
*file
, const char __user
*buf
,
435 size_t count
, loff_t
*ppos
)
437 struct seq_file
*m
= file
->private_data
;
438 struct blk_mq_hw_ctx
*hctx
= m
->private;
444 static const struct file_operations hctx_queued_fops
= {
445 .open
= hctx_queued_open
,
447 .write
= hctx_queued_write
,
449 .release
= single_release
,
452 static int hctx_run_show(struct seq_file
*m
, void *v
)
454 struct blk_mq_hw_ctx
*hctx
= m
->private;
456 seq_printf(m
, "%lu\n", hctx
->run
);
460 static int hctx_run_open(struct inode
*inode
, struct file
*file
)
462 return single_open(file
, hctx_run_show
, inode
->i_private
);
465 static ssize_t
hctx_run_write(struct file
*file
, const char __user
*buf
,
466 size_t count
, loff_t
*ppos
)
468 struct seq_file
*m
= file
->private_data
;
469 struct blk_mq_hw_ctx
*hctx
= m
->private;
475 static const struct file_operations hctx_run_fops
= {
476 .open
= hctx_run_open
,
478 .write
= hctx_run_write
,
480 .release
= single_release
,
483 static int hctx_active_show(struct seq_file
*m
, void *v
)
485 struct blk_mq_hw_ctx
*hctx
= m
->private;
487 seq_printf(m
, "%d\n", atomic_read(&hctx
->nr_active
));
491 static int hctx_active_open(struct inode
*inode
, struct file
*file
)
493 return single_open(file
, hctx_active_show
, inode
->i_private
);
496 static const struct file_operations hctx_active_fops
= {
497 .open
= hctx_active_open
,
500 .release
= single_release
,
503 static void *ctx_rq_list_start(struct seq_file
*m
, loff_t
*pos
)
504 __acquires(&ctx
->lock
)
506 struct blk_mq_ctx
*ctx
= m
->private;
508 spin_lock(&ctx
->lock
);
509 return seq_list_start(&ctx
->rq_list
, *pos
);
512 static void *ctx_rq_list_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
514 struct blk_mq_ctx
*ctx
= m
->private;
516 return seq_list_next(v
, &ctx
->rq_list
, pos
);
519 static void ctx_rq_list_stop(struct seq_file
*m
, void *v
)
520 __releases(&ctx
->lock
)
522 struct blk_mq_ctx
*ctx
= m
->private;
524 spin_unlock(&ctx
->lock
);
527 static const struct seq_operations ctx_rq_list_seq_ops
= {
528 .start
= ctx_rq_list_start
,
529 .next
= ctx_rq_list_next
,
530 .stop
= ctx_rq_list_stop
,
531 .show
= blk_mq_debugfs_rq_show
,
534 static int ctx_rq_list_open(struct inode
*inode
, struct file
*file
)
536 return blk_mq_debugfs_seq_open(inode
, file
, &ctx_rq_list_seq_ops
);
539 static const struct file_operations ctx_rq_list_fops
= {
540 .open
= ctx_rq_list_open
,
543 .release
= seq_release
,
546 static int ctx_dispatched_show(struct seq_file
*m
, void *v
)
548 struct blk_mq_ctx
*ctx
= m
->private;
550 seq_printf(m
, "%lu %lu\n", ctx
->rq_dispatched
[1], ctx
->rq_dispatched
[0]);
554 static int ctx_dispatched_open(struct inode
*inode
, struct file
*file
)
556 return single_open(file
, ctx_dispatched_show
, inode
->i_private
);
559 static ssize_t
ctx_dispatched_write(struct file
*file
, const char __user
*buf
,
560 size_t count
, loff_t
*ppos
)
562 struct seq_file
*m
= file
->private_data
;
563 struct blk_mq_ctx
*ctx
= m
->private;
565 ctx
->rq_dispatched
[0] = ctx
->rq_dispatched
[1] = 0;
569 static const struct file_operations ctx_dispatched_fops
= {
570 .open
= ctx_dispatched_open
,
572 .write
= ctx_dispatched_write
,
574 .release
= single_release
,
577 static int ctx_merged_show(struct seq_file
*m
, void *v
)
579 struct blk_mq_ctx
*ctx
= m
->private;
581 seq_printf(m
, "%lu\n", ctx
->rq_merged
);
585 static int ctx_merged_open(struct inode
*inode
, struct file
*file
)
587 return single_open(file
, ctx_merged_show
, inode
->i_private
);
590 static ssize_t
ctx_merged_write(struct file
*file
, const char __user
*buf
,
591 size_t count
, loff_t
*ppos
)
593 struct seq_file
*m
= file
->private_data
;
594 struct blk_mq_ctx
*ctx
= m
->private;
600 static const struct file_operations ctx_merged_fops
= {
601 .open
= ctx_merged_open
,
603 .write
= ctx_merged_write
,
605 .release
= single_release
,
608 static int ctx_completed_show(struct seq_file
*m
, void *v
)
610 struct blk_mq_ctx
*ctx
= m
->private;
612 seq_printf(m
, "%lu %lu\n", ctx
->rq_completed
[1], ctx
->rq_completed
[0]);
616 static int ctx_completed_open(struct inode
*inode
, struct file
*file
)
618 return single_open(file
, ctx_completed_show
, inode
->i_private
);
621 static ssize_t
ctx_completed_write(struct file
*file
, const char __user
*buf
,
622 size_t count
, loff_t
*ppos
)
624 struct seq_file
*m
= file
->private_data
;
625 struct blk_mq_ctx
*ctx
= m
->private;
627 ctx
->rq_completed
[0] = ctx
->rq_completed
[1] = 0;
631 static const struct file_operations ctx_completed_fops
= {
632 .open
= ctx_completed_open
,
634 .write
= ctx_completed_write
,
636 .release
= single_release
,
639 static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs
[] = {
640 {"state", 0400, &hctx_state_fops
},
641 {"flags", 0400, &hctx_flags_fops
},
642 {"dispatch", 0400, &hctx_dispatch_fops
},
643 {"ctx_map", 0400, &hctx_ctx_map_fops
},
644 {"tags", 0400, &hctx_tags_fops
},
645 {"tags_bitmap", 0400, &hctx_tags_bitmap_fops
},
646 {"sched_tags", 0400, &hctx_sched_tags_fops
},
647 {"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops
},
648 {"io_poll", 0600, &hctx_io_poll_fops
},
649 {"stats", 0600, &hctx_stats_fops
},
650 {"dispatched", 0600, &hctx_dispatched_fops
},
651 {"queued", 0600, &hctx_queued_fops
},
652 {"run", 0600, &hctx_run_fops
},
653 {"active", 0400, &hctx_active_fops
},
657 static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs
[] = {
658 {"rq_list", 0400, &ctx_rq_list_fops
},
659 {"dispatched", 0600, &ctx_dispatched_fops
},
660 {"merged", 0600, &ctx_merged_fops
},
661 {"completed", 0600, &ctx_completed_fops
},
665 int blk_mq_debugfs_register(struct request_queue
*q
, const char *name
)
667 if (!blk_debugfs_root
)
670 q
->debugfs_dir
= debugfs_create_dir(name
, blk_debugfs_root
);
674 if (blk_mq_debugfs_register_hctxs(q
))
680 blk_mq_debugfs_unregister(q
);
684 void blk_mq_debugfs_unregister(struct request_queue
*q
)
686 debugfs_remove_recursive(q
->debugfs_dir
);
687 q
->mq_debugfs_dir
= NULL
;
688 q
->debugfs_dir
= NULL
;
691 static bool debugfs_create_files(struct dentry
*parent
, void *data
,
692 const struct blk_mq_debugfs_attr
*attr
)
694 for (; attr
->name
; attr
++) {
695 if (!debugfs_create_file(attr
->name
, attr
->mode
, parent
,
702 static int blk_mq_debugfs_register_ctx(struct request_queue
*q
,
703 struct blk_mq_ctx
*ctx
,
704 struct dentry
*hctx_dir
)
706 struct dentry
*ctx_dir
;
709 snprintf(name
, sizeof(name
), "cpu%u", ctx
->cpu
);
710 ctx_dir
= debugfs_create_dir(name
, hctx_dir
);
714 if (!debugfs_create_files(ctx_dir
, ctx
, blk_mq_debugfs_ctx_attrs
))
720 static int blk_mq_debugfs_register_hctx(struct request_queue
*q
,
721 struct blk_mq_hw_ctx
*hctx
)
723 struct blk_mq_ctx
*ctx
;
724 struct dentry
*hctx_dir
;
728 snprintf(name
, sizeof(name
), "%u", hctx
->queue_num
);
729 hctx_dir
= debugfs_create_dir(name
, q
->mq_debugfs_dir
);
733 if (!debugfs_create_files(hctx_dir
, hctx
, blk_mq_debugfs_hctx_attrs
))
736 hctx_for_each_ctx(hctx
, ctx
, i
) {
737 if (blk_mq_debugfs_register_ctx(q
, ctx
, hctx_dir
))
744 int blk_mq_debugfs_register_hctxs(struct request_queue
*q
)
746 struct blk_mq_hw_ctx
*hctx
;
752 q
->mq_debugfs_dir
= debugfs_create_dir("mq", q
->debugfs_dir
);
753 if (!q
->mq_debugfs_dir
)
756 queue_for_each_hw_ctx(q
, hctx
, i
) {
757 if (blk_mq_debugfs_register_hctx(q
, hctx
))
764 blk_mq_debugfs_unregister_hctxs(q
);
768 void blk_mq_debugfs_unregister_hctxs(struct request_queue
*q
)
770 debugfs_remove_recursive(q
->mq_debugfs_dir
);
771 q
->mq_debugfs_dir
= NULL
;