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