]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - block/blk-mq-debugfs.c
2a19237455d4ada28b7c74df36953b4376231278
[mirror_ubuntu-bionic-kernel.git] / block / blk-mq-debugfs.c
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>
22 #include "blk.h"
23 #include "blk-mq.h"
24 #include "blk-mq-tag.h"
25
26 struct blk_mq_debugfs_attr {
27 const char *name;
28 umode_t mode;
29 const struct file_operations *fops;
30 };
31
32 static 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
46 static 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 return 0;
64 }
65
66 #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
67 static const char *const blk_queue_flag_name[] = {
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),
97 };
98 #undef QUEUE_FLAG_NAME
99
100 static 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));
106 seq_puts(m, "\n");
107 return 0;
108 }
109
110 static ssize_t blk_queue_flags_store(struct file *file, const char __user *buf,
111 size_t count, loff_t *ppos)
112 {
113 struct request_queue *q = file_inode(file)->i_private;
114 char op[16] = { }, *s;
115
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))
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 {
130 pr_err("%s: unsupported operation '%s'\n", __func__, op);
131 inval:
132 pr_err("%s: use either 'run' or 'start'\n", __func__);
133 return -EINVAL;
134 }
135 return count;
136 }
137
138 static 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
143 static 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
151 static 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
161 static int queue_poll_stat_show(struct seq_file *m, void *v)
162 {
163 struct request_queue *q = m->private;
164 int bucket;
165
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");
170
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 }
175 return 0;
176 }
177
178 static 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
183 static 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
190 #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
191 static const char *const hctx_state_name[] = {
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),
197 };
198 #undef HCTX_STATE_NAME
199
200 static int hctx_state_show(struct seq_file *m, void *v)
201 {
202 struct blk_mq_hw_ctx *hctx = m->private;
203
204 blk_flags_show(m, hctx->state, hctx_state_name,
205 ARRAY_SIZE(hctx_state_name));
206 seq_puts(m, "\n");
207 return 0;
208 }
209
210 static int hctx_state_open(struct inode *inode, struct file *file)
211 {
212 return single_open(file, hctx_state_show, inode->i_private);
213 }
214
215 static 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
222 #define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
223 static const char *const alloc_policy_name[] = {
224 BLK_TAG_ALLOC_NAME(FIFO),
225 BLK_TAG_ALLOC_NAME(RR),
226 };
227 #undef BLK_TAG_ALLOC_NAME
228
229 #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
230 static const char *const hctx_flag_name[] = {
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),
236 };
237 #undef HCTX_FLAG_NAME
238
239 static int hctx_flags_show(struct seq_file *m, void *v)
240 {
241 struct blk_mq_hw_ctx *hctx = m->private;
242 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
243
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));
254 seq_puts(m, "\n");
255 return 0;
256 }
257
258 static int hctx_flags_open(struct inode *inode, struct file *file)
259 {
260 return single_open(file, hctx_flags_show, inode->i_private);
261 }
262
263 static 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
270 #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
271 static const char *const op_name[] = {
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),
285 };
286 #undef REQ_OP_NAME
287
288 #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
289 static const char *const cmd_flag_name[] = {
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),
304 };
305 #undef CMD_FLAG_NAME
306
307 #define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
308 static const char *const rqf_name[] = {
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),
328 };
329 #undef RQF_NAME
330
331 static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
332 {
333 struct request *rq = list_entry_rq(v);
334 const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
335 const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
336
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));
348 seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
349 rq->internal_tag);
350 if (mq_ops->show_rq)
351 mq_ops->show_rq(m, rq);
352 seq_puts(m, "}\n");
353 return 0;
354 }
355
356 static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
357 __acquires(&hctx->lock)
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
365 static 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
372 static void hctx_dispatch_stop(struct seq_file *m, void *v)
373 __releases(&hctx->lock)
374 {
375 struct blk_mq_hw_ctx *hctx = m->private;
376
377 spin_unlock(&hctx->lock);
378 }
379
380 static 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
387 static 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
392 static 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
399 static 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
407 static 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
412 static 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
419 static 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
436 static 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;
440 int res;
441
442 res = mutex_lock_interruptible(&q->sysfs_lock);
443 if (res)
444 goto out;
445 if (hctx->tags)
446 blk_mq_debugfs_tags_show(m, hctx->tags);
447 mutex_unlock(&q->sysfs_lock);
448
449 out:
450 return res;
451 }
452
453 static int hctx_tags_open(struct inode *inode, struct file *file)
454 {
455 return single_open(file, hctx_tags_show, inode->i_private);
456 }
457
458 static 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
465 static 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;
469 int res;
470
471 res = mutex_lock_interruptible(&q->sysfs_lock);
472 if (res)
473 goto out;
474 if (hctx->tags)
475 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
476 mutex_unlock(&q->sysfs_lock);
477
478 out:
479 return res;
480 }
481
482 static 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
487 static 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
494 static 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;
498 int res;
499
500 res = mutex_lock_interruptible(&q->sysfs_lock);
501 if (res)
502 goto out;
503 if (hctx->sched_tags)
504 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
505 mutex_unlock(&q->sysfs_lock);
506
507 out:
508 return res;
509 }
510
511 static 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
516 static 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
523 static 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;
527 int res;
528
529 res = mutex_lock_interruptible(&q->sysfs_lock);
530 if (res)
531 goto out;
532 if (hctx->sched_tags)
533 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
534 mutex_unlock(&q->sysfs_lock);
535
536 out:
537 return res;
538 }
539
540 static 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
545 static 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
552 static 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
562 static 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
567 static 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
577 static 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
585 static 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
602 static int hctx_dispatched_open(struct inode *inode, struct file *file)
603 {
604 return single_open(file, hctx_dispatched_show, inode->i_private);
605 }
606
607 static 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
619 static 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
627 static 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
635 static int hctx_queued_open(struct inode *inode, struct file *file)
636 {
637 return single_open(file, hctx_queued_show, inode->i_private);
638 }
639
640 static 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
650 static 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
658 static 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
666 static int hctx_run_open(struct inode *inode, struct file *file)
667 {
668 return single_open(file, hctx_run_show, inode->i_private);
669 }
670
671 static 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
681 static 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
689 static 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
697 static int hctx_active_open(struct inode *inode, struct file *file)
698 {
699 return single_open(file, hctx_active_show, inode->i_private);
700 }
701
702 static 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
709 static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
710 __acquires(&ctx->lock)
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
718 static 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
725 static void ctx_rq_list_stop(struct seq_file *m, void *v)
726 __releases(&ctx->lock)
727 {
728 struct blk_mq_ctx *ctx = m->private;
729
730 spin_unlock(&ctx->lock);
731 }
732
733 static 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
740 static 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
745 static 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
752 static 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
760 static int ctx_dispatched_open(struct inode *inode, struct file *file)
761 {
762 return single_open(file, ctx_dispatched_show, inode->i_private);
763 }
764
765 static 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
775 static 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
783 static 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
791 static int ctx_merged_open(struct inode *inode, struct file *file)
792 {
793 return single_open(file, ctx_merged_show, inode->i_private);
794 }
795
796 static 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
806 static 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
814 static 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
822 static int ctx_completed_open(struct inode *inode, struct file *file)
823 {
824 return single_open(file, ctx_completed_show, inode->i_private);
825 }
826
827 static 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
837 static 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
845 static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
846 {"poll_stat", 0400, &queue_poll_stat_fops},
847 {"state", 0600, &blk_queue_flags_fops},
848 {},
849 };
850
851 static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
852 {"state", 0400, &hctx_state_fops},
853 {"flags", 0400, &hctx_flags_fops},
854 {"dispatch", 0400, &hctx_dispatch_fops},
855 {"ctx_map", 0400, &hctx_ctx_map_fops},
856 {"tags", 0400, &hctx_tags_fops},
857 {"tags_bitmap", 0400, &hctx_tags_bitmap_fops},
858 {"sched_tags", 0400, &hctx_sched_tags_fops},
859 {"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops},
860 {"io_poll", 0600, &hctx_io_poll_fops},
861 {"dispatched", 0600, &hctx_dispatched_fops},
862 {"queued", 0600, &hctx_queued_fops},
863 {"run", 0600, &hctx_run_fops},
864 {"active", 0400, &hctx_active_fops},
865 {},
866 };
867
868 static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
869 {"rq_list", 0400, &ctx_rq_list_fops},
870 {"dispatched", 0600, &ctx_dispatched_fops},
871 {"merged", 0600, &ctx_merged_fops},
872 {"completed", 0600, &ctx_completed_fops},
873 {},
874 };
875
876 int blk_mq_debugfs_register(struct request_queue *q)
877 {
878 if (!blk_debugfs_root)
879 return -ENOENT;
880
881 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
882 blk_debugfs_root);
883 if (!q->debugfs_dir)
884 goto err;
885
886 if (blk_mq_debugfs_register_mq(q))
887 goto err;
888
889 return 0;
890
891 err:
892 blk_mq_debugfs_unregister(q);
893 return -ENOMEM;
894 }
895
896 void 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
903 static 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
914 static 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];
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
926 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
927 return -ENOMEM;
928
929 return 0;
930 }
931
932 static 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
945 if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
946 return -ENOMEM;
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
956 int blk_mq_debugfs_register_mq(struct request_queue *q)
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
968 if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
969 goto err;
970
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
978 err:
979 blk_mq_debugfs_unregister_mq(q);
980 return -ENOMEM;
981 }
982
983 void blk_mq_debugfs_unregister_mq(struct request_queue *q)
984 {
985 debugfs_remove_recursive(q->mq_debugfs_dir);
986 q->mq_debugfs_dir = NULL;
987 }