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