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