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