]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - block/blk-mq-debugfs.c
blk-mq: fix schedule-while-atomic with scheduler attached
[mirror_ubuntu-artful-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;
162
163 seq_puts(m, "read: ");
164 print_stat(m, &q->poll_stat[READ]);
165 seq_puts(m, "\n");
166
167 seq_puts(m, "write: ");
168 print_stat(m, &q->poll_stat[WRITE]);
169 seq_puts(m, "\n");
170 return 0;
171}
172
173static int queue_poll_stat_open(struct inode *inode, struct file *file)
174{
175 return single_open(file, queue_poll_stat_show, inode->i_private);
176}
177
178static const struct file_operations queue_poll_stat_fops = {
179 .open = queue_poll_stat_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = single_release,
183};
184
f5c0b091
BVA
185static const char *const hctx_state_name[] = {
186 [BLK_MQ_S_STOPPED] = "STOPPED",
187 [BLK_MQ_S_TAG_ACTIVE] = "TAG_ACTIVE",
188 [BLK_MQ_S_SCHED_RESTART] = "SCHED_RESTART",
189 [BLK_MQ_S_TAG_WAITING] = "TAG_WAITING",
190
191};
9abb2ad2
OS
192static int hctx_state_show(struct seq_file *m, void *v)
193{
194 struct blk_mq_hw_ctx *hctx = m->private;
195
f5c0b091
BVA
196 blk_flags_show(m, hctx->state, hctx_state_name,
197 ARRAY_SIZE(hctx_state_name));
9abb2ad2
OS
198 return 0;
199}
200
201static int hctx_state_open(struct inode *inode, struct file *file)
202{
203 return single_open(file, hctx_state_show, inode->i_private);
204}
205
206static const struct file_operations hctx_state_fops = {
207 .open = hctx_state_open,
208 .read = seq_read,
209 .llseek = seq_lseek,
210 .release = single_release,
211};
212
f5c0b091
BVA
213static const char *const alloc_policy_name[] = {
214 [BLK_TAG_ALLOC_FIFO] = "fifo",
215 [BLK_TAG_ALLOC_RR] = "rr",
216};
217
218static const char *const hctx_flag_name[] = {
219 [ilog2(BLK_MQ_F_SHOULD_MERGE)] = "SHOULD_MERGE",
220 [ilog2(BLK_MQ_F_TAG_SHARED)] = "TAG_SHARED",
221 [ilog2(BLK_MQ_F_SG_MERGE)] = "SG_MERGE",
222 [ilog2(BLK_MQ_F_BLOCKING)] = "BLOCKING",
223 [ilog2(BLK_MQ_F_NO_SCHED)] = "NO_SCHED",
224};
225
9abb2ad2
OS
226static int hctx_flags_show(struct seq_file *m, void *v)
227{
228 struct blk_mq_hw_ctx *hctx = m->private;
f5c0b091 229 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
9abb2ad2 230
f5c0b091
BVA
231 seq_puts(m, "alloc_policy=");
232 if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
233 alloc_policy_name[alloc_policy])
234 seq_puts(m, alloc_policy_name[alloc_policy]);
235 else
236 seq_printf(m, "%d", alloc_policy);
237 seq_puts(m, " ");
238 blk_flags_show(m,
239 hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
240 hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
9abb2ad2
OS
241 return 0;
242}
243
244static int hctx_flags_open(struct inode *inode, struct file *file)
245{
246 return single_open(file, hctx_flags_show, inode->i_private);
247}
248
249static const struct file_operations hctx_flags_fops = {
250 .open = hctx_flags_open,
251 .read = seq_read,
252 .llseek = seq_lseek,
253 .release = single_release,
254};
255
950cd7e9
OS
256static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
257{
258 struct request *rq = list_entry_rq(v);
259
aebf526b 260 seq_printf(m, "%p {.cmd_flags=0x%x, .rq_flags=0x%x, .tag=%d, .internal_tag=%d}\n",
a1ae0f74 261 rq, rq->cmd_flags, (__force unsigned int)rq->rq_flags,
7b393852 262 rq->tag, rq->internal_tag);
950cd7e9
OS
263 return 0;
264}
265
266static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
f3bcb0e6 267 __acquires(&hctx->lock)
950cd7e9
OS
268{
269 struct blk_mq_hw_ctx *hctx = m->private;
270
271 spin_lock(&hctx->lock);
272 return seq_list_start(&hctx->dispatch, *pos);
273}
274
275static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
276{
277 struct blk_mq_hw_ctx *hctx = m->private;
278
279 return seq_list_next(v, &hctx->dispatch, pos);
280}
281
282static void hctx_dispatch_stop(struct seq_file *m, void *v)
f3bcb0e6 283 __releases(&hctx->lock)
950cd7e9
OS
284{
285 struct blk_mq_hw_ctx *hctx = m->private;
286
287 spin_unlock(&hctx->lock);
288}
289
290static const struct seq_operations hctx_dispatch_seq_ops = {
291 .start = hctx_dispatch_start,
292 .next = hctx_dispatch_next,
293 .stop = hctx_dispatch_stop,
294 .show = blk_mq_debugfs_rq_show,
295};
296
297static int hctx_dispatch_open(struct inode *inode, struct file *file)
298{
299 return blk_mq_debugfs_seq_open(inode, file, &hctx_dispatch_seq_ops);
300}
301
302static const struct file_operations hctx_dispatch_fops = {
303 .open = hctx_dispatch_open,
304 .read = seq_read,
305 .llseek = seq_lseek,
306 .release = seq_release,
307};
308
0bfa5288
OS
309static int hctx_ctx_map_show(struct seq_file *m, void *v)
310{
311 struct blk_mq_hw_ctx *hctx = m->private;
312
313 sbitmap_bitmap_show(&hctx->ctx_map, m);
314 return 0;
315}
316
317static int hctx_ctx_map_open(struct inode *inode, struct file *file)
318{
319 return single_open(file, hctx_ctx_map_show, inode->i_private);
320}
321
322static const struct file_operations hctx_ctx_map_fops = {
323 .open = hctx_ctx_map_open,
324 .read = seq_read,
325 .llseek = seq_lseek,
326 .release = single_release,
327};
328
d96b37c0
OS
329static void blk_mq_debugfs_tags_show(struct seq_file *m,
330 struct blk_mq_tags *tags)
331{
332 seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
333 seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
334 seq_printf(m, "active_queues=%d\n",
335 atomic_read(&tags->active_queues));
336
337 seq_puts(m, "\nbitmap_tags:\n");
338 sbitmap_queue_show(&tags->bitmap_tags, m);
339
340 if (tags->nr_reserved_tags) {
341 seq_puts(m, "\nbreserved_tags:\n");
342 sbitmap_queue_show(&tags->breserved_tags, m);
343 }
344}
345
346static int hctx_tags_show(struct seq_file *m, void *v)
347{
348 struct blk_mq_hw_ctx *hctx = m->private;
349 struct request_queue *q = hctx->queue;
8c0f14ea 350 int res;
d96b37c0 351
8c0f14ea
BVA
352 res = mutex_lock_interruptible(&q->sysfs_lock);
353 if (res)
354 goto out;
d96b37c0
OS
355 if (hctx->tags)
356 blk_mq_debugfs_tags_show(m, hctx->tags);
357 mutex_unlock(&q->sysfs_lock);
358
8c0f14ea
BVA
359out:
360 return res;
d96b37c0
OS
361}
362
363static int hctx_tags_open(struct inode *inode, struct file *file)
364{
365 return single_open(file, hctx_tags_show, inode->i_private);
366}
367
368static const struct file_operations hctx_tags_fops = {
369 .open = hctx_tags_open,
370 .read = seq_read,
371 .llseek = seq_lseek,
372 .release = single_release,
373};
374
d7e3621a
OS
375static int hctx_tags_bitmap_show(struct seq_file *m, void *v)
376{
377 struct blk_mq_hw_ctx *hctx = m->private;
378 struct request_queue *q = hctx->queue;
8c0f14ea 379 int res;
d7e3621a 380
8c0f14ea
BVA
381 res = mutex_lock_interruptible(&q->sysfs_lock);
382 if (res)
383 goto out;
d7e3621a
OS
384 if (hctx->tags)
385 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
386 mutex_unlock(&q->sysfs_lock);
8c0f14ea
BVA
387
388out:
389 return res;
d7e3621a
OS
390}
391
392static int hctx_tags_bitmap_open(struct inode *inode, struct file *file)
393{
394 return single_open(file, hctx_tags_bitmap_show, inode->i_private);
395}
396
397static const struct file_operations hctx_tags_bitmap_fops = {
398 .open = hctx_tags_bitmap_open,
399 .read = seq_read,
400 .llseek = seq_lseek,
401 .release = single_release,
402};
403
d96b37c0
OS
404static int hctx_sched_tags_show(struct seq_file *m, void *v)
405{
406 struct blk_mq_hw_ctx *hctx = m->private;
407 struct request_queue *q = hctx->queue;
8c0f14ea 408 int res;
d96b37c0 409
8c0f14ea
BVA
410 res = mutex_lock_interruptible(&q->sysfs_lock);
411 if (res)
412 goto out;
d96b37c0
OS
413 if (hctx->sched_tags)
414 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
415 mutex_unlock(&q->sysfs_lock);
416
8c0f14ea
BVA
417out:
418 return res;
d96b37c0
OS
419}
420
421static int hctx_sched_tags_open(struct inode *inode, struct file *file)
422{
423 return single_open(file, hctx_sched_tags_show, inode->i_private);
424}
425
426static const struct file_operations hctx_sched_tags_fops = {
427 .open = hctx_sched_tags_open,
428 .read = seq_read,
429 .llseek = seq_lseek,
430 .release = single_release,
431};
432
d7e3621a
OS
433static int hctx_sched_tags_bitmap_show(struct seq_file *m, void *v)
434{
435 struct blk_mq_hw_ctx *hctx = m->private;
436 struct request_queue *q = hctx->queue;
8c0f14ea 437 int res;
d7e3621a 438
8c0f14ea
BVA
439 res = mutex_lock_interruptible(&q->sysfs_lock);
440 if (res)
441 goto out;
d7e3621a
OS
442 if (hctx->sched_tags)
443 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
444 mutex_unlock(&q->sysfs_lock);
8c0f14ea
BVA
445
446out:
447 return res;
d7e3621a
OS
448}
449
450static int hctx_sched_tags_bitmap_open(struct inode *inode, struct file *file)
451{
452 return single_open(file, hctx_sched_tags_bitmap_show, inode->i_private);
453}
454
455static const struct file_operations hctx_sched_tags_bitmap_fops = {
456 .open = hctx_sched_tags_bitmap_open,
457 .read = seq_read,
458 .llseek = seq_lseek,
459 .release = single_release,
460};
461
be215473
OS
462static int hctx_io_poll_show(struct seq_file *m, void *v)
463{
464 struct blk_mq_hw_ctx *hctx = m->private;
465
466 seq_printf(m, "considered=%lu\n", hctx->poll_considered);
467 seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
468 seq_printf(m, "success=%lu\n", hctx->poll_success);
469 return 0;
470}
471
472static int hctx_io_poll_open(struct inode *inode, struct file *file)
473{
474 return single_open(file, hctx_io_poll_show, inode->i_private);
475}
476
477static ssize_t hctx_io_poll_write(struct file *file, const char __user *buf,
478 size_t count, loff_t *ppos)
479{
480 struct seq_file *m = file->private_data;
481 struct blk_mq_hw_ctx *hctx = m->private;
482
483 hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
484 return count;
485}
486
487static const struct file_operations hctx_io_poll_fops = {
488 .open = hctx_io_poll_open,
489 .read = seq_read,
490 .write = hctx_io_poll_write,
491 .llseek = seq_lseek,
492 .release = single_release,
493};
494
be215473
OS
495static int hctx_dispatched_show(struct seq_file *m, void *v)
496{
497 struct blk_mq_hw_ctx *hctx = m->private;
498 int i;
499
500 seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
501
502 for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
503 unsigned int d = 1U << (i - 1);
504
505 seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
506 }
507
508 seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
509 return 0;
510}
511
512static int hctx_dispatched_open(struct inode *inode, struct file *file)
513{
514 return single_open(file, hctx_dispatched_show, inode->i_private);
515}
516
517static ssize_t hctx_dispatched_write(struct file *file, const char __user *buf,
518 size_t count, loff_t *ppos)
519{
520 struct seq_file *m = file->private_data;
521 struct blk_mq_hw_ctx *hctx = m->private;
522 int i;
523
524 for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
525 hctx->dispatched[i] = 0;
526 return count;
527}
528
529static const struct file_operations hctx_dispatched_fops = {
530 .open = hctx_dispatched_open,
531 .read = seq_read,
532 .write = hctx_dispatched_write,
533 .llseek = seq_lseek,
534 .release = single_release,
535};
536
4a46f05e
OS
537static int hctx_queued_show(struct seq_file *m, void *v)
538{
539 struct blk_mq_hw_ctx *hctx = m->private;
540
541 seq_printf(m, "%lu\n", hctx->queued);
542 return 0;
543}
544
545static int hctx_queued_open(struct inode *inode, struct file *file)
546{
547 return single_open(file, hctx_queued_show, inode->i_private);
548}
549
550static ssize_t hctx_queued_write(struct file *file, const char __user *buf,
551 size_t count, loff_t *ppos)
552{
553 struct seq_file *m = file->private_data;
554 struct blk_mq_hw_ctx *hctx = m->private;
555
556 hctx->queued = 0;
557 return count;
558}
559
560static const struct file_operations hctx_queued_fops = {
561 .open = hctx_queued_open,
562 .read = seq_read,
563 .write = hctx_queued_write,
564 .llseek = seq_lseek,
565 .release = single_release,
566};
567
568static int hctx_run_show(struct seq_file *m, void *v)
569{
570 struct blk_mq_hw_ctx *hctx = m->private;
571
572 seq_printf(m, "%lu\n", hctx->run);
573 return 0;
574}
575
576static int hctx_run_open(struct inode *inode, struct file *file)
577{
578 return single_open(file, hctx_run_show, inode->i_private);
579}
580
581static ssize_t hctx_run_write(struct file *file, const char __user *buf,
582 size_t count, loff_t *ppos)
583{
584 struct seq_file *m = file->private_data;
585 struct blk_mq_hw_ctx *hctx = m->private;
586
587 hctx->run = 0;
588 return count;
589}
590
591static const struct file_operations hctx_run_fops = {
592 .open = hctx_run_open,
593 .read = seq_read,
594 .write = hctx_run_write,
595 .llseek = seq_lseek,
596 .release = single_release,
597};
598
599static int hctx_active_show(struct seq_file *m, void *v)
600{
601 struct blk_mq_hw_ctx *hctx = m->private;
602
603 seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
604 return 0;
605}
606
607static int hctx_active_open(struct inode *inode, struct file *file)
608{
609 return single_open(file, hctx_active_show, inode->i_private);
610}
611
612static const struct file_operations hctx_active_fops = {
613 .open = hctx_active_open,
614 .read = seq_read,
615 .llseek = seq_lseek,
616 .release = single_release,
617};
618
950cd7e9 619static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
f3bcb0e6 620 __acquires(&ctx->lock)
950cd7e9
OS
621{
622 struct blk_mq_ctx *ctx = m->private;
623
624 spin_lock(&ctx->lock);
625 return seq_list_start(&ctx->rq_list, *pos);
626}
627
628static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
629{
630 struct blk_mq_ctx *ctx = m->private;
631
632 return seq_list_next(v, &ctx->rq_list, pos);
633}
634
635static void ctx_rq_list_stop(struct seq_file *m, void *v)
f3bcb0e6 636 __releases(&ctx->lock)
950cd7e9
OS
637{
638 struct blk_mq_ctx *ctx = m->private;
639
640 spin_unlock(&ctx->lock);
641}
642
643static const struct seq_operations ctx_rq_list_seq_ops = {
644 .start = ctx_rq_list_start,
645 .next = ctx_rq_list_next,
646 .stop = ctx_rq_list_stop,
647 .show = blk_mq_debugfs_rq_show,
648};
649
650static int ctx_rq_list_open(struct inode *inode, struct file *file)
651{
652 return blk_mq_debugfs_seq_open(inode, file, &ctx_rq_list_seq_ops);
653}
654
655static const struct file_operations ctx_rq_list_fops = {
656 .open = ctx_rq_list_open,
657 .read = seq_read,
658 .llseek = seq_lseek,
659 .release = seq_release,
660};
661
4a46f05e
OS
662static int ctx_dispatched_show(struct seq_file *m, void *v)
663{
664 struct blk_mq_ctx *ctx = m->private;
665
666 seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
667 return 0;
668}
669
670static int ctx_dispatched_open(struct inode *inode, struct file *file)
671{
672 return single_open(file, ctx_dispatched_show, inode->i_private);
673}
674
675static ssize_t ctx_dispatched_write(struct file *file, const char __user *buf,
676 size_t count, loff_t *ppos)
677{
678 struct seq_file *m = file->private_data;
679 struct blk_mq_ctx *ctx = m->private;
680
681 ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
682 return count;
683}
684
685static const struct file_operations ctx_dispatched_fops = {
686 .open = ctx_dispatched_open,
687 .read = seq_read,
688 .write = ctx_dispatched_write,
689 .llseek = seq_lseek,
690 .release = single_release,
691};
692
693static int ctx_merged_show(struct seq_file *m, void *v)
694{
695 struct blk_mq_ctx *ctx = m->private;
696
697 seq_printf(m, "%lu\n", ctx->rq_merged);
698 return 0;
699}
700
701static int ctx_merged_open(struct inode *inode, struct file *file)
702{
703 return single_open(file, ctx_merged_show, inode->i_private);
704}
705
706static ssize_t ctx_merged_write(struct file *file, const char __user *buf,
707 size_t count, loff_t *ppos)
708{
709 struct seq_file *m = file->private_data;
710 struct blk_mq_ctx *ctx = m->private;
711
712 ctx->rq_merged = 0;
713 return count;
714}
715
716static const struct file_operations ctx_merged_fops = {
717 .open = ctx_merged_open,
718 .read = seq_read,
719 .write = ctx_merged_write,
720 .llseek = seq_lseek,
721 .release = single_release,
722};
723
724static int ctx_completed_show(struct seq_file *m, void *v)
725{
726 struct blk_mq_ctx *ctx = m->private;
727
728 seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
729 return 0;
730}
731
732static int ctx_completed_open(struct inode *inode, struct file *file)
733{
734 return single_open(file, ctx_completed_show, inode->i_private);
735}
736
737static ssize_t ctx_completed_write(struct file *file, const char __user *buf,
738 size_t count, loff_t *ppos)
739{
740 struct seq_file *m = file->private_data;
741 struct blk_mq_ctx *ctx = m->private;
742
743 ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
744 return count;
745}
746
747static const struct file_operations ctx_completed_fops = {
748 .open = ctx_completed_open,
749 .read = seq_read,
750 .write = ctx_completed_write,
751 .llseek = seq_lseek,
752 .release = single_release,
753};
754
34dbad5d
OS
755static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
756 {"poll_stat", 0400, &queue_poll_stat_fops},
757 {},
758};
759
07e4fead 760static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
9abb2ad2
OS
761 {"state", 0400, &hctx_state_fops},
762 {"flags", 0400, &hctx_flags_fops},
950cd7e9 763 {"dispatch", 0400, &hctx_dispatch_fops},
0bfa5288 764 {"ctx_map", 0400, &hctx_ctx_map_fops},
d96b37c0 765 {"tags", 0400, &hctx_tags_fops},
d7e3621a 766 {"tags_bitmap", 0400, &hctx_tags_bitmap_fops},
d96b37c0 767 {"sched_tags", 0400, &hctx_sched_tags_fops},
d7e3621a 768 {"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops},
be215473 769 {"io_poll", 0600, &hctx_io_poll_fops},
be215473 770 {"dispatched", 0600, &hctx_dispatched_fops},
4a46f05e
OS
771 {"queued", 0600, &hctx_queued_fops},
772 {"run", 0600, &hctx_run_fops},
773 {"active", 0400, &hctx_active_fops},
72f2f8f6 774 {},
07e4fead
OS
775};
776
777static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
950cd7e9 778 {"rq_list", 0400, &ctx_rq_list_fops},
4a46f05e
OS
779 {"dispatched", 0600, &ctx_dispatched_fops},
780 {"merged", 0600, &ctx_merged_fops},
781 {"completed", 0600, &ctx_completed_fops},
72f2f8f6 782 {},
07e4fead
OS
783};
784
785int blk_mq_debugfs_register(struct request_queue *q, const char *name)
786{
18fbda91 787 if (!blk_debugfs_root)
07e4fead
OS
788 return -ENOENT;
789
18fbda91 790 q->debugfs_dir = debugfs_create_dir(name, blk_debugfs_root);
07e4fead
OS
791 if (!q->debugfs_dir)
792 goto err;
793
794 if (blk_mq_debugfs_register_hctxs(q))
795 goto err;
796
797 return 0;
798
799err:
800 blk_mq_debugfs_unregister(q);
801 return -ENOMEM;
802}
803
804void blk_mq_debugfs_unregister(struct request_queue *q)
805{
806 debugfs_remove_recursive(q->debugfs_dir);
807 q->mq_debugfs_dir = NULL;
808 q->debugfs_dir = NULL;
809}
810
72f2f8f6
BVA
811static bool debugfs_create_files(struct dentry *parent, void *data,
812 const struct blk_mq_debugfs_attr *attr)
813{
814 for (; attr->name; attr++) {
815 if (!debugfs_create_file(attr->name, attr->mode, parent,
816 data, attr->fops))
817 return false;
818 }
819 return true;
820}
821
07e4fead
OS
822static int blk_mq_debugfs_register_ctx(struct request_queue *q,
823 struct blk_mq_ctx *ctx,
824 struct dentry *hctx_dir)
825{
826 struct dentry *ctx_dir;
827 char name[20];
07e4fead
OS
828
829 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
830 ctx_dir = debugfs_create_dir(name, hctx_dir);
831 if (!ctx_dir)
832 return -ENOMEM;
833
72f2f8f6
BVA
834 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
835 return -ENOMEM;
07e4fead
OS
836
837 return 0;
838}
839
840static int blk_mq_debugfs_register_hctx(struct request_queue *q,
841 struct blk_mq_hw_ctx *hctx)
842{
843 struct blk_mq_ctx *ctx;
844 struct dentry *hctx_dir;
845 char name[20];
846 int i;
847
848 snprintf(name, sizeof(name), "%u", hctx->queue_num);
849 hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
850 if (!hctx_dir)
851 return -ENOMEM;
852
72f2f8f6
BVA
853 if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
854 return -ENOMEM;
07e4fead
OS
855
856 hctx_for_each_ctx(hctx, ctx, i) {
857 if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
858 return -ENOMEM;
859 }
860
861 return 0;
862}
863
864int blk_mq_debugfs_register_hctxs(struct request_queue *q)
865{
866 struct blk_mq_hw_ctx *hctx;
867 int i;
868
869 if (!q->debugfs_dir)
870 return -ENOENT;
871
91d68905
BVA
872 if (!debugfs_create_files(q->debugfs_dir, q, blk_queue_attrs))
873 goto err;
874
07e4fead
OS
875 q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
876 if (!q->mq_debugfs_dir)
877 goto err;
878
34dbad5d
OS
879 if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
880 goto err;
881
07e4fead
OS
882 queue_for_each_hw_ctx(q, hctx, i) {
883 if (blk_mq_debugfs_register_hctx(q, hctx))
884 goto err;
885 }
886
887 return 0;
888
889err:
890 blk_mq_debugfs_unregister_hctxs(q);
891 return -ENOMEM;
892}
893
894void blk_mq_debugfs_unregister_hctxs(struct request_queue *q)
895{
896 debugfs_remove_recursive(q->mq_debugfs_dir);
897 q->mq_debugfs_dir = NULL;
898}