]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - block/blk-mq-debugfs.c
blk-mq: Unregister debugfs attributes earlier
[mirror_ubuntu-bionic-kernel.git] / block / blk-mq-debugfs.c
CommitLineData
07e4fead
OS
1/*
2 * Copyright (C) 2017 Facebook
3 *
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.
7 *
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.
12 *
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/>.
15 */
16
17#include <linux/kernel.h>
18#include <linux/blkdev.h>
19#include <linux/debugfs.h>
20
21#include <linux/blk-mq.h>
18fbda91 22#include "blk.h"
07e4fead 23#include "blk-mq.h"
d96b37c0 24#include "blk-mq-tag.h"
07e4fead
OS
25
26struct blk_mq_debugfs_attr {
27 const char *name;
28 umode_t mode;
29 const struct file_operations *fops;
30};
31
950cd7e9
OS
32static int blk_mq_debugfs_seq_open(struct inode *inode, struct file *file,
33 const struct seq_operations *ops)
34{
35 struct seq_file *m;
36 int ret;
37
38 ret = seq_open(file, ops);
39 if (!ret) {
40 m = file->private_data;
41 m->private = inode->i_private;
42 }
43 return ret;
44}
45
91d68905
BVA
46static int blk_flags_show(struct seq_file *m, const unsigned long flags,
47 const char *const *flag_name, int flag_name_count)
48{
49 bool sep = false;
50 int i;
51
52 for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
53 if (!(flags & BIT(i)))
54 continue;
55 if (sep)
56 seq_puts(m, " ");
57 sep = true;
58 if (i < flag_name_count && flag_name[i])
59 seq_puts(m, flag_name[i]);
60 else
61 seq_printf(m, "%d", i);
62 }
63 seq_puts(m, "\n");
64 return 0;
65}
66
67static const char *const blk_queue_flag_name[] = {
68 [QUEUE_FLAG_QUEUED] = "QUEUED",
69 [QUEUE_FLAG_STOPPED] = "STOPPED",
70 [QUEUE_FLAG_SYNCFULL] = "SYNCFULL",
71 [QUEUE_FLAG_ASYNCFULL] = "ASYNCFULL",
72 [QUEUE_FLAG_DYING] = "DYING",
73 [QUEUE_FLAG_BYPASS] = "BYPASS",
74 [QUEUE_FLAG_BIDI] = "BIDI",
75 [QUEUE_FLAG_NOMERGES] = "NOMERGES",
76 [QUEUE_FLAG_SAME_COMP] = "SAME_COMP",
77 [QUEUE_FLAG_FAIL_IO] = "FAIL_IO",
78 [QUEUE_FLAG_STACKABLE] = "STACKABLE",
79 [QUEUE_FLAG_NONROT] = "NONROT",
80 [QUEUE_FLAG_IO_STAT] = "IO_STAT",
81 [QUEUE_FLAG_DISCARD] = "DISCARD",
82 [QUEUE_FLAG_NOXMERGES] = "NOXMERGES",
83 [QUEUE_FLAG_ADD_RANDOM] = "ADD_RANDOM",
84 [QUEUE_FLAG_SECERASE] = "SECERASE",
85 [QUEUE_FLAG_SAME_FORCE] = "SAME_FORCE",
86 [QUEUE_FLAG_DEAD] = "DEAD",
87 [QUEUE_FLAG_INIT_DONE] = "INIT_DONE",
88 [QUEUE_FLAG_NO_SG_MERGE] = "NO_SG_MERGE",
89 [QUEUE_FLAG_POLL] = "POLL",
90 [QUEUE_FLAG_WC] = "WC",
91 [QUEUE_FLAG_FUA] = "FUA",
92 [QUEUE_FLAG_FLUSH_NQ] = "FLUSH_NQ",
93 [QUEUE_FLAG_DAX] = "DAX",
94 [QUEUE_FLAG_STATS] = "STATS",
95 [QUEUE_FLAG_POLL_STATS] = "POLL_STATS",
96 [QUEUE_FLAG_REGISTERED] = "REGISTERED",
97};
98
99static int blk_queue_flags_show(struct seq_file *m, void *v)
100{
101 struct request_queue *q = m->private;
102
103 blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
104 ARRAY_SIZE(blk_queue_flag_name));
105 return 0;
106}
107
108static ssize_t blk_queue_flags_store(struct file *file, const char __user *ubuf,
109 size_t len, loff_t *offp)
110{
111 struct request_queue *q = file_inode(file)->i_private;
112 char op[16] = { }, *s;
113
114 len = min(len, sizeof(op) - 1);
115 if (copy_from_user(op, ubuf, len))
116 return -EFAULT;
117 s = op;
118 strsep(&s, " \t\n"); /* strip trailing whitespace */
119 if (strcmp(op, "run") == 0) {
120 blk_mq_run_hw_queues(q, true);
121 } else if (strcmp(op, "start") == 0) {
122 blk_mq_start_stopped_hw_queues(q, true);
123 } else {
124 pr_err("%s: unsupported operation %s. Use either 'run' or 'start'\n",
125 __func__, op);
126 return -EINVAL;
127 }
128 return len;
129}
130
131static int blk_queue_flags_open(struct inode *inode, struct file *file)
132{
133 return single_open(file, blk_queue_flags_show, inode->i_private);
134}
135
136static const struct file_operations blk_queue_flags_fops = {
137 .open = blk_queue_flags_open,
138 .read = seq_read,
139 .llseek = seq_lseek,
140 .release = single_release,
141 .write = blk_queue_flags_store,
142};
143
144static const struct blk_mq_debugfs_attr blk_queue_attrs[] = {
145 {"state", 0600, &blk_queue_flags_fops},
146 {},
147};
148
34dbad5d
OS
149static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
150{
151 if (stat->nr_samples) {
152 seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
153 stat->nr_samples, stat->mean, stat->min, stat->max);
154 } else {
155 seq_puts(m, "samples=0");
156 }
157}
158
159static int queue_poll_stat_show(struct seq_file *m, void *v)
160{
161 struct request_queue *q = m->private;
0206319f 162 int bucket;
34dbad5d 163
0206319f
SB
164 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
165 seq_printf(m, "read (%d Bytes): ", 1 << (9+bucket));
166 print_stat(m, &q->poll_stat[2*bucket]);
167 seq_puts(m, "\n");
34dbad5d 168
0206319f
SB
169 seq_printf(m, "write (%d Bytes): ", 1 << (9+bucket));
170 print_stat(m, &q->poll_stat[2*bucket+1]);
171 seq_puts(m, "\n");
172 }
34dbad5d
OS
173 return 0;
174}
175
176static int queue_poll_stat_open(struct inode *inode, struct file *file)
177{
178 return single_open(file, queue_poll_stat_show, inode->i_private);
179}
180
181static const struct file_operations queue_poll_stat_fops = {
182 .open = queue_poll_stat_open,
183 .read = seq_read,
184 .llseek = seq_lseek,
185 .release = single_release,
186};
187
f5c0b091
BVA
188static const char *const hctx_state_name[] = {
189 [BLK_MQ_S_STOPPED] = "STOPPED",
190 [BLK_MQ_S_TAG_ACTIVE] = "TAG_ACTIVE",
191 [BLK_MQ_S_SCHED_RESTART] = "SCHED_RESTART",
192 [BLK_MQ_S_TAG_WAITING] = "TAG_WAITING",
193
194};
9abb2ad2
OS
195static int hctx_state_show(struct seq_file *m, void *v)
196{
197 struct blk_mq_hw_ctx *hctx = m->private;
198
f5c0b091
BVA
199 blk_flags_show(m, hctx->state, hctx_state_name,
200 ARRAY_SIZE(hctx_state_name));
9abb2ad2
OS
201 return 0;
202}
203
204static int hctx_state_open(struct inode *inode, struct file *file)
205{
206 return single_open(file, hctx_state_show, inode->i_private);
207}
208
209static const struct file_operations hctx_state_fops = {
210 .open = hctx_state_open,
211 .read = seq_read,
212 .llseek = seq_lseek,
213 .release = single_release,
214};
215
f5c0b091
BVA
216static const char *const alloc_policy_name[] = {
217 [BLK_TAG_ALLOC_FIFO] = "fifo",
218 [BLK_TAG_ALLOC_RR] = "rr",
219};
220
221static const char *const hctx_flag_name[] = {
222 [ilog2(BLK_MQ_F_SHOULD_MERGE)] = "SHOULD_MERGE",
223 [ilog2(BLK_MQ_F_TAG_SHARED)] = "TAG_SHARED",
224 [ilog2(BLK_MQ_F_SG_MERGE)] = "SG_MERGE",
225 [ilog2(BLK_MQ_F_BLOCKING)] = "BLOCKING",
226 [ilog2(BLK_MQ_F_NO_SCHED)] = "NO_SCHED",
227};
228
9abb2ad2
OS
229static int hctx_flags_show(struct seq_file *m, void *v)
230{
231 struct blk_mq_hw_ctx *hctx = m->private;
f5c0b091 232 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
9abb2ad2 233
f5c0b091
BVA
234 seq_puts(m, "alloc_policy=");
235 if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
236 alloc_policy_name[alloc_policy])
237 seq_puts(m, alloc_policy_name[alloc_policy]);
238 else
239 seq_printf(m, "%d", alloc_policy);
240 seq_puts(m, " ");
241 blk_flags_show(m,
242 hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
243 hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
9abb2ad2
OS
244 return 0;
245}
246
247static int hctx_flags_open(struct inode *inode, struct file *file)
248{
249 return single_open(file, hctx_flags_show, inode->i_private);
250}
251
252static const struct file_operations hctx_flags_fops = {
253 .open = hctx_flags_open,
254 .read = seq_read,
255 .llseek = seq_lseek,
256 .release = single_release,
257};
258
950cd7e9
OS
259static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
260{
261 struct request *rq = list_entry_rq(v);
262
aebf526b 263 seq_printf(m, "%p {.cmd_flags=0x%x, .rq_flags=0x%x, .tag=%d, .internal_tag=%d}\n",
a1ae0f74 264 rq, rq->cmd_flags, (__force unsigned int)rq->rq_flags,
7b393852 265 rq->tag, rq->internal_tag);
950cd7e9
OS
266 return 0;
267}
268
269static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
f3bcb0e6 270 __acquires(&hctx->lock)
950cd7e9
OS
271{
272 struct blk_mq_hw_ctx *hctx = m->private;
273
274 spin_lock(&hctx->lock);
275 return seq_list_start(&hctx->dispatch, *pos);
276}
277
278static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
279{
280 struct blk_mq_hw_ctx *hctx = m->private;
281
282 return seq_list_next(v, &hctx->dispatch, pos);
283}
284
285static void hctx_dispatch_stop(struct seq_file *m, void *v)
f3bcb0e6 286 __releases(&hctx->lock)
950cd7e9
OS
287{
288 struct blk_mq_hw_ctx *hctx = m->private;
289
290 spin_unlock(&hctx->lock);
291}
292
293static const struct seq_operations hctx_dispatch_seq_ops = {
294 .start = hctx_dispatch_start,
295 .next = hctx_dispatch_next,
296 .stop = hctx_dispatch_stop,
297 .show = blk_mq_debugfs_rq_show,
298};
299
300static int hctx_dispatch_open(struct inode *inode, struct file *file)
301{
302 return blk_mq_debugfs_seq_open(inode, file, &hctx_dispatch_seq_ops);
303}
304
305static const struct file_operations hctx_dispatch_fops = {
306 .open = hctx_dispatch_open,
307 .read = seq_read,
308 .llseek = seq_lseek,
309 .release = seq_release,
310};
311
0bfa5288
OS
312static int hctx_ctx_map_show(struct seq_file *m, void *v)
313{
314 struct blk_mq_hw_ctx *hctx = m->private;
315
316 sbitmap_bitmap_show(&hctx->ctx_map, m);
317 return 0;
318}
319
320static int hctx_ctx_map_open(struct inode *inode, struct file *file)
321{
322 return single_open(file, hctx_ctx_map_show, inode->i_private);
323}
324
325static const struct file_operations hctx_ctx_map_fops = {
326 .open = hctx_ctx_map_open,
327 .read = seq_read,
328 .llseek = seq_lseek,
329 .release = single_release,
330};
331
d96b37c0
OS
332static 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 atomic_read(&tags->active_queues));
339
340 seq_puts(m, "\nbitmap_tags:\n");
341 sbitmap_queue_show(&tags->bitmap_tags, m);
342
343 if (tags->nr_reserved_tags) {
344 seq_puts(m, "\nbreserved_tags:\n");
345 sbitmap_queue_show(&tags->breserved_tags, m);
346 }
347}
348
349static int hctx_tags_show(struct seq_file *m, void *v)
350{
351 struct blk_mq_hw_ctx *hctx = m->private;
352 struct request_queue *q = hctx->queue;
8c0f14ea 353 int res;
d96b37c0 354
8c0f14ea
BVA
355 res = mutex_lock_interruptible(&q->sysfs_lock);
356 if (res)
357 goto out;
d96b37c0
OS
358 if (hctx->tags)
359 blk_mq_debugfs_tags_show(m, hctx->tags);
360 mutex_unlock(&q->sysfs_lock);
361
8c0f14ea
BVA
362out:
363 return res;
d96b37c0
OS
364}
365
366static int hctx_tags_open(struct inode *inode, struct file *file)
367{
368 return single_open(file, hctx_tags_show, inode->i_private);
369}
370
371static const struct file_operations hctx_tags_fops = {
372 .open = hctx_tags_open,
373 .read = seq_read,
374 .llseek = seq_lseek,
375 .release = single_release,
376};
377
d7e3621a
OS
378static int hctx_tags_bitmap_show(struct seq_file *m, void *v)
379{
380 struct blk_mq_hw_ctx *hctx = m->private;
381 struct request_queue *q = hctx->queue;
8c0f14ea 382 int res;
d7e3621a 383
8c0f14ea
BVA
384 res = mutex_lock_interruptible(&q->sysfs_lock);
385 if (res)
386 goto out;
d7e3621a
OS
387 if (hctx->tags)
388 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
389 mutex_unlock(&q->sysfs_lock);
8c0f14ea
BVA
390
391out:
392 return res;
d7e3621a
OS
393}
394
395static int hctx_tags_bitmap_open(struct inode *inode, struct file *file)
396{
397 return single_open(file, hctx_tags_bitmap_show, inode->i_private);
398}
399
400static const struct file_operations hctx_tags_bitmap_fops = {
401 .open = hctx_tags_bitmap_open,
402 .read = seq_read,
403 .llseek = seq_lseek,
404 .release = single_release,
405};
406
d96b37c0
OS
407static int hctx_sched_tags_show(struct seq_file *m, void *v)
408{
409 struct blk_mq_hw_ctx *hctx = m->private;
410 struct request_queue *q = hctx->queue;
8c0f14ea 411 int res;
d96b37c0 412
8c0f14ea
BVA
413 res = mutex_lock_interruptible(&q->sysfs_lock);
414 if (res)
415 goto out;
d96b37c0
OS
416 if (hctx->sched_tags)
417 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
418 mutex_unlock(&q->sysfs_lock);
419
8c0f14ea
BVA
420out:
421 return res;
d96b37c0
OS
422}
423
424static int hctx_sched_tags_open(struct inode *inode, struct file *file)
425{
426 return single_open(file, hctx_sched_tags_show, inode->i_private);
427}
428
429static const struct file_operations hctx_sched_tags_fops = {
430 .open = hctx_sched_tags_open,
431 .read = seq_read,
432 .llseek = seq_lseek,
433 .release = single_release,
434};
435
d7e3621a
OS
436static int hctx_sched_tags_bitmap_show(struct seq_file *m, void *v)
437{
438 struct blk_mq_hw_ctx *hctx = m->private;
439 struct request_queue *q = hctx->queue;
8c0f14ea 440 int res;
d7e3621a 441
8c0f14ea
BVA
442 res = mutex_lock_interruptible(&q->sysfs_lock);
443 if (res)
444 goto out;
d7e3621a
OS
445 if (hctx->sched_tags)
446 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
447 mutex_unlock(&q->sysfs_lock);
8c0f14ea
BVA
448
449out:
450 return res;
d7e3621a
OS
451}
452
453static int hctx_sched_tags_bitmap_open(struct inode *inode, struct file *file)
454{
455 return single_open(file, hctx_sched_tags_bitmap_show, inode->i_private);
456}
457
458static const struct file_operations hctx_sched_tags_bitmap_fops = {
459 .open = hctx_sched_tags_bitmap_open,
460 .read = seq_read,
461 .llseek = seq_lseek,
462 .release = single_release,
463};
464
be215473
OS
465static int hctx_io_poll_show(struct seq_file *m, void *v)
466{
467 struct blk_mq_hw_ctx *hctx = m->private;
468
469 seq_printf(m, "considered=%lu\n", hctx->poll_considered);
470 seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
471 seq_printf(m, "success=%lu\n", hctx->poll_success);
472 return 0;
473}
474
475static int hctx_io_poll_open(struct inode *inode, struct file *file)
476{
477 return single_open(file, hctx_io_poll_show, inode->i_private);
478}
479
480static ssize_t hctx_io_poll_write(struct file *file, const char __user *buf,
481 size_t count, loff_t *ppos)
482{
483 struct seq_file *m = file->private_data;
484 struct blk_mq_hw_ctx *hctx = m->private;
485
486 hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
487 return count;
488}
489
490static const struct file_operations hctx_io_poll_fops = {
491 .open = hctx_io_poll_open,
492 .read = seq_read,
493 .write = hctx_io_poll_write,
494 .llseek = seq_lseek,
495 .release = single_release,
496};
497
be215473
OS
498static int hctx_dispatched_show(struct seq_file *m, void *v)
499{
500 struct blk_mq_hw_ctx *hctx = m->private;
501 int i;
502
503 seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
504
505 for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
506 unsigned int d = 1U << (i - 1);
507
508 seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
509 }
510
511 seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
512 return 0;
513}
514
515static int hctx_dispatched_open(struct inode *inode, struct file *file)
516{
517 return single_open(file, hctx_dispatched_show, inode->i_private);
518}
519
520static ssize_t hctx_dispatched_write(struct file *file, const char __user *buf,
521 size_t count, loff_t *ppos)
522{
523 struct seq_file *m = file->private_data;
524 struct blk_mq_hw_ctx *hctx = m->private;
525 int i;
526
527 for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
528 hctx->dispatched[i] = 0;
529 return count;
530}
531
532static const struct file_operations hctx_dispatched_fops = {
533 .open = hctx_dispatched_open,
534 .read = seq_read,
535 .write = hctx_dispatched_write,
536 .llseek = seq_lseek,
537 .release = single_release,
538};
539
4a46f05e
OS
540static int hctx_queued_show(struct seq_file *m, void *v)
541{
542 struct blk_mq_hw_ctx *hctx = m->private;
543
544 seq_printf(m, "%lu\n", hctx->queued);
545 return 0;
546}
547
548static int hctx_queued_open(struct inode *inode, struct file *file)
549{
550 return single_open(file, hctx_queued_show, inode->i_private);
551}
552
553static ssize_t hctx_queued_write(struct file *file, const char __user *buf,
554 size_t count, loff_t *ppos)
555{
556 struct seq_file *m = file->private_data;
557 struct blk_mq_hw_ctx *hctx = m->private;
558
559 hctx->queued = 0;
560 return count;
561}
562
563static const struct file_operations hctx_queued_fops = {
564 .open = hctx_queued_open,
565 .read = seq_read,
566 .write = hctx_queued_write,
567 .llseek = seq_lseek,
568 .release = single_release,
569};
570
571static int hctx_run_show(struct seq_file *m, void *v)
572{
573 struct blk_mq_hw_ctx *hctx = m->private;
574
575 seq_printf(m, "%lu\n", hctx->run);
576 return 0;
577}
578
579static int hctx_run_open(struct inode *inode, struct file *file)
580{
581 return single_open(file, hctx_run_show, inode->i_private);
582}
583
584static ssize_t hctx_run_write(struct file *file, const char __user *buf,
585 size_t count, loff_t *ppos)
586{
587 struct seq_file *m = file->private_data;
588 struct blk_mq_hw_ctx *hctx = m->private;
589
590 hctx->run = 0;
591 return count;
592}
593
594static const struct file_operations hctx_run_fops = {
595 .open = hctx_run_open,
596 .read = seq_read,
597 .write = hctx_run_write,
598 .llseek = seq_lseek,
599 .release = single_release,
600};
601
602static int hctx_active_show(struct seq_file *m, void *v)
603{
604 struct blk_mq_hw_ctx *hctx = m->private;
605
606 seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
607 return 0;
608}
609
610static int hctx_active_open(struct inode *inode, struct file *file)
611{
612 return single_open(file, hctx_active_show, inode->i_private);
613}
614
615static const struct file_operations hctx_active_fops = {
616 .open = hctx_active_open,
617 .read = seq_read,
618 .llseek = seq_lseek,
619 .release = single_release,
620};
621
950cd7e9 622static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
f3bcb0e6 623 __acquires(&ctx->lock)
950cd7e9
OS
624{
625 struct blk_mq_ctx *ctx = m->private;
626
627 spin_lock(&ctx->lock);
628 return seq_list_start(&ctx->rq_list, *pos);
629}
630
631static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
632{
633 struct blk_mq_ctx *ctx = m->private;
634
635 return seq_list_next(v, &ctx->rq_list, pos);
636}
637
638static void ctx_rq_list_stop(struct seq_file *m, void *v)
f3bcb0e6 639 __releases(&ctx->lock)
950cd7e9
OS
640{
641 struct blk_mq_ctx *ctx = m->private;
642
643 spin_unlock(&ctx->lock);
644}
645
646static const struct seq_operations ctx_rq_list_seq_ops = {
647 .start = ctx_rq_list_start,
648 .next = ctx_rq_list_next,
649 .stop = ctx_rq_list_stop,
650 .show = blk_mq_debugfs_rq_show,
651};
652
653static int ctx_rq_list_open(struct inode *inode, struct file *file)
654{
655 return blk_mq_debugfs_seq_open(inode, file, &ctx_rq_list_seq_ops);
656}
657
658static const struct file_operations ctx_rq_list_fops = {
659 .open = ctx_rq_list_open,
660 .read = seq_read,
661 .llseek = seq_lseek,
662 .release = seq_release,
663};
664
4a46f05e
OS
665static int ctx_dispatched_show(struct seq_file *m, void *v)
666{
667 struct blk_mq_ctx *ctx = m->private;
668
669 seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
670 return 0;
671}
672
673static int ctx_dispatched_open(struct inode *inode, struct file *file)
674{
675 return single_open(file, ctx_dispatched_show, inode->i_private);
676}
677
678static ssize_t ctx_dispatched_write(struct file *file, const char __user *buf,
679 size_t count, loff_t *ppos)
680{
681 struct seq_file *m = file->private_data;
682 struct blk_mq_ctx *ctx = m->private;
683
684 ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
685 return count;
686}
687
688static const struct file_operations ctx_dispatched_fops = {
689 .open = ctx_dispatched_open,
690 .read = seq_read,
691 .write = ctx_dispatched_write,
692 .llseek = seq_lseek,
693 .release = single_release,
694};
695
696static int ctx_merged_show(struct seq_file *m, void *v)
697{
698 struct blk_mq_ctx *ctx = m->private;
699
700 seq_printf(m, "%lu\n", ctx->rq_merged);
701 return 0;
702}
703
704static int ctx_merged_open(struct inode *inode, struct file *file)
705{
706 return single_open(file, ctx_merged_show, inode->i_private);
707}
708
709static ssize_t ctx_merged_write(struct file *file, const char __user *buf,
710 size_t count, loff_t *ppos)
711{
712 struct seq_file *m = file->private_data;
713 struct blk_mq_ctx *ctx = m->private;
714
715 ctx->rq_merged = 0;
716 return count;
717}
718
719static const struct file_operations ctx_merged_fops = {
720 .open = ctx_merged_open,
721 .read = seq_read,
722 .write = ctx_merged_write,
723 .llseek = seq_lseek,
724 .release = single_release,
725};
726
727static int ctx_completed_show(struct seq_file *m, void *v)
728{
729 struct blk_mq_ctx *ctx = m->private;
730
731 seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
732 return 0;
733}
734
735static int ctx_completed_open(struct inode *inode, struct file *file)
736{
737 return single_open(file, ctx_completed_show, inode->i_private);
738}
739
740static ssize_t ctx_completed_write(struct file *file, const char __user *buf,
741 size_t count, loff_t *ppos)
742{
743 struct seq_file *m = file->private_data;
744 struct blk_mq_ctx *ctx = m->private;
745
746 ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
747 return count;
748}
749
750static const struct file_operations ctx_completed_fops = {
751 .open = ctx_completed_open,
752 .read = seq_read,
753 .write = ctx_completed_write,
754 .llseek = seq_lseek,
755 .release = single_release,
756};
757
34dbad5d
OS
758static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
759 {"poll_stat", 0400, &queue_poll_stat_fops},
760 {},
761};
762
07e4fead 763static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
9abb2ad2
OS
764 {"state", 0400, &hctx_state_fops},
765 {"flags", 0400, &hctx_flags_fops},
950cd7e9 766 {"dispatch", 0400, &hctx_dispatch_fops},
0bfa5288 767 {"ctx_map", 0400, &hctx_ctx_map_fops},
d96b37c0 768 {"tags", 0400, &hctx_tags_fops},
d7e3621a 769 {"tags_bitmap", 0400, &hctx_tags_bitmap_fops},
d96b37c0 770 {"sched_tags", 0400, &hctx_sched_tags_fops},
d7e3621a 771 {"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops},
be215473 772 {"io_poll", 0600, &hctx_io_poll_fops},
be215473 773 {"dispatched", 0600, &hctx_dispatched_fops},
4a46f05e
OS
774 {"queued", 0600, &hctx_queued_fops},
775 {"run", 0600, &hctx_run_fops},
776 {"active", 0400, &hctx_active_fops},
72f2f8f6 777 {},
07e4fead
OS
778};
779
780static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
950cd7e9 781 {"rq_list", 0400, &ctx_rq_list_fops},
4a46f05e
OS
782 {"dispatched", 0600, &ctx_dispatched_fops},
783 {"merged", 0600, &ctx_merged_fops},
784 {"completed", 0600, &ctx_completed_fops},
72f2f8f6 785 {},
07e4fead
OS
786};
787
4c9e4019 788int blk_mq_debugfs_register(struct request_queue *q)
07e4fead 789{
18fbda91 790 if (!blk_debugfs_root)
07e4fead
OS
791 return -ENOENT;
792
4c9e4019
BVA
793 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
794 blk_debugfs_root);
07e4fead
OS
795 if (!q->debugfs_dir)
796 goto err;
797
62d6c949 798 if (blk_mq_debugfs_register_mq(q))
07e4fead
OS
799 goto err;
800
801 return 0;
802
803err:
804 blk_mq_debugfs_unregister(q);
805 return -ENOMEM;
806}
807
808void blk_mq_debugfs_unregister(struct request_queue *q)
809{
810 debugfs_remove_recursive(q->debugfs_dir);
811 q->mq_debugfs_dir = NULL;
812 q->debugfs_dir = NULL;
813}
814
72f2f8f6
BVA
815static bool debugfs_create_files(struct dentry *parent, void *data,
816 const struct blk_mq_debugfs_attr *attr)
817{
818 for (; attr->name; attr++) {
819 if (!debugfs_create_file(attr->name, attr->mode, parent,
820 data, attr->fops))
821 return false;
822 }
823 return true;
824}
825
07e4fead
OS
826static int blk_mq_debugfs_register_ctx(struct request_queue *q,
827 struct blk_mq_ctx *ctx,
828 struct dentry *hctx_dir)
829{
830 struct dentry *ctx_dir;
831 char name[20];
07e4fead
OS
832
833 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
834 ctx_dir = debugfs_create_dir(name, hctx_dir);
835 if (!ctx_dir)
836 return -ENOMEM;
837
72f2f8f6
BVA
838 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
839 return -ENOMEM;
07e4fead
OS
840
841 return 0;
842}
843
844static int blk_mq_debugfs_register_hctx(struct request_queue *q,
845 struct blk_mq_hw_ctx *hctx)
846{
847 struct blk_mq_ctx *ctx;
848 struct dentry *hctx_dir;
849 char name[20];
850 int i;
851
852 snprintf(name, sizeof(name), "%u", hctx->queue_num);
853 hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
854 if (!hctx_dir)
855 return -ENOMEM;
856
72f2f8f6
BVA
857 if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
858 return -ENOMEM;
07e4fead
OS
859
860 hctx_for_each_ctx(hctx, ctx, i) {
861 if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
862 return -ENOMEM;
863 }
864
865 return 0;
866}
867
62d6c949 868int blk_mq_debugfs_register_mq(struct request_queue *q)
07e4fead
OS
869{
870 struct blk_mq_hw_ctx *hctx;
871 int i;
872
873 if (!q->debugfs_dir)
874 return -ENOENT;
875
91d68905
BVA
876 if (!debugfs_create_files(q->debugfs_dir, q, blk_queue_attrs))
877 goto err;
878
07e4fead
OS
879 q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
880 if (!q->mq_debugfs_dir)
881 goto err;
882
34dbad5d
OS
883 if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
884 goto err;
885
07e4fead
OS
886 queue_for_each_hw_ctx(q, hctx, i) {
887 if (blk_mq_debugfs_register_hctx(q, hctx))
888 goto err;
889 }
890
891 return 0;
892
893err:
62d6c949 894 blk_mq_debugfs_unregister_mq(q);
07e4fead
OS
895 return -ENOMEM;
896}
897
62d6c949 898void blk_mq_debugfs_unregister_mq(struct request_queue *q)
07e4fead
OS
899{
900 debugfs_remove_recursive(q->mq_debugfs_dir);
901 q->mq_debugfs_dir = NULL;
902}