blob: 1ff3d726fb038c2e06e4f45d06c6436600f458e3 [file] [log] [blame]
Omar Sandoval07e4fea2017-01-25 08:06:40 -08001/*
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>
Omar Sandoval18fbda92017-01-31 14:53:20 -080022#include "blk.h"
Omar Sandoval07e4fea2017-01-25 08:06:40 -080023#include "blk-mq.h"
Omar Sandovald96b37c2017-01-25 08:06:46 -080024#include "blk-mq-tag.h"
Omar Sandoval07e4fea2017-01-25 08:06:40 -080025
26struct blk_mq_debugfs_attr {
27 const char *name;
28 umode_t mode;
29 const struct file_operations *fops;
30};
31
Omar Sandoval950cd7e2017-01-25 08:06:42 -080032static 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
Bart Van Assche91d68902017-04-10 16:13:15 -060046static 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)
Omar Sandovalbec03d62017-05-04 00:31:23 -070056 seq_puts(m, "|");
Bart Van Assche91d68902017-04-10 16:13:15 -060057 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 }
Bart Van Assche91d68902017-04-10 16:13:15 -060063 return 0;
64}
65
66static const char *const blk_queue_flag_name[] = {
67 [QUEUE_FLAG_QUEUED] = "QUEUED",
68 [QUEUE_FLAG_STOPPED] = "STOPPED",
69 [QUEUE_FLAG_SYNCFULL] = "SYNCFULL",
70 [QUEUE_FLAG_ASYNCFULL] = "ASYNCFULL",
71 [QUEUE_FLAG_DYING] = "DYING",
72 [QUEUE_FLAG_BYPASS] = "BYPASS",
73 [QUEUE_FLAG_BIDI] = "BIDI",
74 [QUEUE_FLAG_NOMERGES] = "NOMERGES",
75 [QUEUE_FLAG_SAME_COMP] = "SAME_COMP",
76 [QUEUE_FLAG_FAIL_IO] = "FAIL_IO",
77 [QUEUE_FLAG_STACKABLE] = "STACKABLE",
78 [QUEUE_FLAG_NONROT] = "NONROT",
79 [QUEUE_FLAG_IO_STAT] = "IO_STAT",
80 [QUEUE_FLAG_DISCARD] = "DISCARD",
81 [QUEUE_FLAG_NOXMERGES] = "NOXMERGES",
82 [QUEUE_FLAG_ADD_RANDOM] = "ADD_RANDOM",
83 [QUEUE_FLAG_SECERASE] = "SECERASE",
84 [QUEUE_FLAG_SAME_FORCE] = "SAME_FORCE",
85 [QUEUE_FLAG_DEAD] = "DEAD",
86 [QUEUE_FLAG_INIT_DONE] = "INIT_DONE",
87 [QUEUE_FLAG_NO_SG_MERGE] = "NO_SG_MERGE",
88 [QUEUE_FLAG_POLL] = "POLL",
89 [QUEUE_FLAG_WC] = "WC",
90 [QUEUE_FLAG_FUA] = "FUA",
91 [QUEUE_FLAG_FLUSH_NQ] = "FLUSH_NQ",
92 [QUEUE_FLAG_DAX] = "DAX",
93 [QUEUE_FLAG_STATS] = "STATS",
94 [QUEUE_FLAG_POLL_STATS] = "POLL_STATS",
95 [QUEUE_FLAG_REGISTERED] = "REGISTERED",
96};
97
98static int blk_queue_flags_show(struct seq_file *m, void *v)
99{
100 struct request_queue *q = m->private;
101
102 blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
103 ARRAY_SIZE(blk_queue_flag_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700104 seq_puts(m, "\n");
Bart Van Assche91d68902017-04-10 16:13:15 -0600105 return 0;
106}
107
108static ssize_t blk_queue_flags_store(struct file *file, const char __user *ubuf,
109 size_t len, loff_t *offp)
110{
111 struct request_queue *q = file_inode(file)->i_private;
112 char op[16] = { }, *s;
113
114 len = min(len, sizeof(op) - 1);
115 if (copy_from_user(op, ubuf, len))
116 return -EFAULT;
117 s = op;
118 strsep(&s, " \t\n"); /* strip trailing whitespace */
119 if (strcmp(op, "run") == 0) {
120 blk_mq_run_hw_queues(q, true);
121 } else if (strcmp(op, "start") == 0) {
122 blk_mq_start_stopped_hw_queues(q, true);
123 } else {
124 pr_err("%s: unsupported operation %s. Use either 'run' or 'start'\n",
125 __func__, op);
126 return -EINVAL;
127 }
128 return len;
129}
130
131static int blk_queue_flags_open(struct inode *inode, struct file *file)
132{
133 return single_open(file, blk_queue_flags_show, inode->i_private);
134}
135
136static const struct file_operations blk_queue_flags_fops = {
137 .open = blk_queue_flags_open,
138 .read = seq_read,
139 .llseek = seq_lseek,
140 .release = single_release,
141 .write = blk_queue_flags_store,
142};
143
Omar Sandoval34dbad52017-03-21 08:56:08 -0700144static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
145{
146 if (stat->nr_samples) {
147 seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
148 stat->nr_samples, stat->mean, stat->min, stat->max);
149 } else {
150 seq_puts(m, "samples=0");
151 }
152}
153
154static int queue_poll_stat_show(struct seq_file *m, void *v)
155{
156 struct request_queue *q = m->private;
Stephen Bates02063192017-04-20 16:59:11 -0600157 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700158
Stephen Bates02063192017-04-20 16:59:11 -0600159 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
160 seq_printf(m, "read (%d Bytes): ", 1 << (9+bucket));
161 print_stat(m, &q->poll_stat[2*bucket]);
162 seq_puts(m, "\n");
Omar Sandoval34dbad52017-03-21 08:56:08 -0700163
Stephen Bates02063192017-04-20 16:59:11 -0600164 seq_printf(m, "write (%d Bytes): ", 1 << (9+bucket));
165 print_stat(m, &q->poll_stat[2*bucket+1]);
166 seq_puts(m, "\n");
167 }
Omar Sandoval34dbad52017-03-21 08:56:08 -0700168 return 0;
169}
170
171static int queue_poll_stat_open(struct inode *inode, struct file *file)
172{
173 return single_open(file, queue_poll_stat_show, inode->i_private);
174}
175
176static const struct file_operations queue_poll_stat_fops = {
177 .open = queue_poll_stat_open,
178 .read = seq_read,
179 .llseek = seq_lseek,
180 .release = single_release,
181};
182
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700183static const char *const hctx_state_name[] = {
184 [BLK_MQ_S_STOPPED] = "STOPPED",
185 [BLK_MQ_S_TAG_ACTIVE] = "TAG_ACTIVE",
186 [BLK_MQ_S_SCHED_RESTART] = "SCHED_RESTART",
187 [BLK_MQ_S_TAG_WAITING] = "TAG_WAITING",
188
189};
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800190static int hctx_state_show(struct seq_file *m, void *v)
191{
192 struct blk_mq_hw_ctx *hctx = m->private;
193
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700194 blk_flags_show(m, hctx->state, hctx_state_name,
195 ARRAY_SIZE(hctx_state_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700196 seq_puts(m, "\n");
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800197 return 0;
198}
199
200static int hctx_state_open(struct inode *inode, struct file *file)
201{
202 return single_open(file, hctx_state_show, inode->i_private);
203}
204
205static const struct file_operations hctx_state_fops = {
206 .open = hctx_state_open,
207 .read = seq_read,
208 .llseek = seq_lseek,
209 .release = single_release,
210};
211
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700212static const char *const alloc_policy_name[] = {
213 [BLK_TAG_ALLOC_FIFO] = "fifo",
214 [BLK_TAG_ALLOC_RR] = "rr",
215};
216
217static const char *const hctx_flag_name[] = {
218 [ilog2(BLK_MQ_F_SHOULD_MERGE)] = "SHOULD_MERGE",
219 [ilog2(BLK_MQ_F_TAG_SHARED)] = "TAG_SHARED",
220 [ilog2(BLK_MQ_F_SG_MERGE)] = "SG_MERGE",
221 [ilog2(BLK_MQ_F_BLOCKING)] = "BLOCKING",
222 [ilog2(BLK_MQ_F_NO_SCHED)] = "NO_SCHED",
223};
224
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800225static int hctx_flags_show(struct seq_file *m, void *v)
226{
227 struct blk_mq_hw_ctx *hctx = m->private;
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700228 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800229
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700230 seq_puts(m, "alloc_policy=");
231 if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
232 alloc_policy_name[alloc_policy])
233 seq_puts(m, alloc_policy_name[alloc_policy]);
234 else
235 seq_printf(m, "%d", alloc_policy);
236 seq_puts(m, " ");
237 blk_flags_show(m,
238 hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
239 hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700240 seq_puts(m, "\n");
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800241 return 0;
242}
243
244static int hctx_flags_open(struct inode *inode, struct file *file)
245{
246 return single_open(file, hctx_flags_show, inode->i_private);
247}
248
249static const struct file_operations hctx_flags_fops = {
250 .open = hctx_flags_open,
251 .read = seq_read,
252 .llseek = seq_lseek,
253 .release = single_release,
254};
255
Bart Van Assche8658dca2017-04-26 13:47:55 -0700256static const char *const op_name[] = {
257 [REQ_OP_READ] = "READ",
258 [REQ_OP_WRITE] = "WRITE",
259 [REQ_OP_FLUSH] = "FLUSH",
260 [REQ_OP_DISCARD] = "DISCARD",
261 [REQ_OP_ZONE_REPORT] = "ZONE_REPORT",
262 [REQ_OP_SECURE_ERASE] = "SECURE_ERASE",
263 [REQ_OP_ZONE_RESET] = "ZONE_RESET",
264 [REQ_OP_WRITE_SAME] = "WRITE_SAME",
265 [REQ_OP_WRITE_ZEROES] = "WRITE_ZEROES",
266 [REQ_OP_SCSI_IN] = "SCSI_IN",
267 [REQ_OP_SCSI_OUT] = "SCSI_OUT",
268 [REQ_OP_DRV_IN] = "DRV_IN",
269 [REQ_OP_DRV_OUT] = "DRV_OUT",
270};
271
272static const char *const cmd_flag_name[] = {
273 [__REQ_FAILFAST_DEV] = "FAILFAST_DEV",
274 [__REQ_FAILFAST_TRANSPORT] = "FAILFAST_TRANSPORT",
275 [__REQ_FAILFAST_DRIVER] = "FAILFAST_DRIVER",
276 [__REQ_SYNC] = "SYNC",
277 [__REQ_META] = "META",
278 [__REQ_PRIO] = "PRIO",
279 [__REQ_NOMERGE] = "NOMERGE",
280 [__REQ_IDLE] = "IDLE",
281 [__REQ_INTEGRITY] = "INTEGRITY",
282 [__REQ_FUA] = "FUA",
283 [__REQ_PREFLUSH] = "PREFLUSH",
284 [__REQ_RAHEAD] = "RAHEAD",
285 [__REQ_BACKGROUND] = "BACKGROUND",
286 [__REQ_NR_BITS] = "NR_BITS",
287};
288
289static const char *const rqf_name[] = {
290 [ilog2((__force u32)RQF_SORTED)] = "SORTED",
291 [ilog2((__force u32)RQF_STARTED)] = "STARTED",
292 [ilog2((__force u32)RQF_QUEUED)] = "QUEUED",
293 [ilog2((__force u32)RQF_SOFTBARRIER)] = "SOFTBARRIER",
294 [ilog2((__force u32)RQF_FLUSH_SEQ)] = "FLUSH_SEQ",
295 [ilog2((__force u32)RQF_MIXED_MERGE)] = "MIXED_MERGE",
296 [ilog2((__force u32)RQF_MQ_INFLIGHT)] = "MQ_INFLIGHT",
297 [ilog2((__force u32)RQF_DONTPREP)] = "DONTPREP",
298 [ilog2((__force u32)RQF_PREEMPT)] = "PREEMPT",
299 [ilog2((__force u32)RQF_COPY_USER)] = "COPY_USER",
300 [ilog2((__force u32)RQF_FAILED)] = "FAILED",
301 [ilog2((__force u32)RQF_QUIET)] = "QUIET",
302 [ilog2((__force u32)RQF_ELVPRIV)] = "ELVPRIV",
303 [ilog2((__force u32)RQF_IO_STAT)] = "IO_STAT",
304 [ilog2((__force u32)RQF_ALLOCED)] = "ALLOCED",
305 [ilog2((__force u32)RQF_PM)] = "PM",
306 [ilog2((__force u32)RQF_HASHED)] = "HASHED",
307 [ilog2((__force u32)RQF_STATS)] = "STATS",
308 [ilog2((__force u32)RQF_SPECIAL_PAYLOAD)] = "SPECIAL_PAYLOAD",
309};
310
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800311static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
312{
313 struct request *rq = list_entry_rq(v);
Bart Van Assche2836ee42017-04-26 13:47:56 -0700314 const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
Bart Van Assche8658dca2017-04-26 13:47:55 -0700315 const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800316
Bart Van Assche8658dca2017-04-26 13:47:55 -0700317 seq_printf(m, "%p {.op=", rq);
318 if (op < ARRAY_SIZE(op_name) && op_name[op])
319 seq_printf(m, "%s", op_name[op]);
320 else
321 seq_printf(m, "%d", op);
322 seq_puts(m, ", .cmd_flags=");
323 blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
324 ARRAY_SIZE(cmd_flag_name));
325 seq_puts(m, ", .rq_flags=");
326 blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
327 ARRAY_SIZE(rqf_name));
Bart Van Assche2836ee42017-04-26 13:47:56 -0700328 seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
Bart Van Assche8658dca2017-04-26 13:47:55 -0700329 rq->internal_tag);
Bart Van Assche2836ee42017-04-26 13:47:56 -0700330 if (mq_ops->show_rq)
331 mq_ops->show_rq(m, rq);
332 seq_puts(m, "}\n");
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800333 return 0;
334}
335
336static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800337 __acquires(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800338{
339 struct blk_mq_hw_ctx *hctx = m->private;
340
341 spin_lock(&hctx->lock);
342 return seq_list_start(&hctx->dispatch, *pos);
343}
344
345static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
346{
347 struct blk_mq_hw_ctx *hctx = m->private;
348
349 return seq_list_next(v, &hctx->dispatch, pos);
350}
351
352static void hctx_dispatch_stop(struct seq_file *m, void *v)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800353 __releases(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800354{
355 struct blk_mq_hw_ctx *hctx = m->private;
356
357 spin_unlock(&hctx->lock);
358}
359
360static const struct seq_operations hctx_dispatch_seq_ops = {
361 .start = hctx_dispatch_start,
362 .next = hctx_dispatch_next,
363 .stop = hctx_dispatch_stop,
364 .show = blk_mq_debugfs_rq_show,
365};
366
367static int hctx_dispatch_open(struct inode *inode, struct file *file)
368{
369 return blk_mq_debugfs_seq_open(inode, file, &hctx_dispatch_seq_ops);
370}
371
372static const struct file_operations hctx_dispatch_fops = {
373 .open = hctx_dispatch_open,
374 .read = seq_read,
375 .llseek = seq_lseek,
376 .release = seq_release,
377};
378
Omar Sandoval0bfa5282017-01-25 08:06:45 -0800379static int hctx_ctx_map_show(struct seq_file *m, void *v)
380{
381 struct blk_mq_hw_ctx *hctx = m->private;
382
383 sbitmap_bitmap_show(&hctx->ctx_map, m);
384 return 0;
385}
386
387static int hctx_ctx_map_open(struct inode *inode, struct file *file)
388{
389 return single_open(file, hctx_ctx_map_show, inode->i_private);
390}
391
392static const struct file_operations hctx_ctx_map_fops = {
393 .open = hctx_ctx_map_open,
394 .read = seq_read,
395 .llseek = seq_lseek,
396 .release = single_release,
397};
398
Omar Sandovald96b37c2017-01-25 08:06:46 -0800399static void blk_mq_debugfs_tags_show(struct seq_file *m,
400 struct blk_mq_tags *tags)
401{
402 seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
403 seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
404 seq_printf(m, "active_queues=%d\n",
405 atomic_read(&tags->active_queues));
406
407 seq_puts(m, "\nbitmap_tags:\n");
408 sbitmap_queue_show(&tags->bitmap_tags, m);
409
410 if (tags->nr_reserved_tags) {
411 seq_puts(m, "\nbreserved_tags:\n");
412 sbitmap_queue_show(&tags->breserved_tags, m);
413 }
414}
415
416static int hctx_tags_show(struct seq_file *m, void *v)
417{
418 struct blk_mq_hw_ctx *hctx = m->private;
419 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800420 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800421
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800422 res = mutex_lock_interruptible(&q->sysfs_lock);
423 if (res)
424 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800425 if (hctx->tags)
426 blk_mq_debugfs_tags_show(m, hctx->tags);
427 mutex_unlock(&q->sysfs_lock);
428
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800429out:
430 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800431}
432
433static int hctx_tags_open(struct inode *inode, struct file *file)
434{
435 return single_open(file, hctx_tags_show, inode->i_private);
436}
437
438static const struct file_operations hctx_tags_fops = {
439 .open = hctx_tags_open,
440 .read = seq_read,
441 .llseek = seq_lseek,
442 .release = single_release,
443};
444
Omar Sandovald7e36212017-01-25 08:06:47 -0800445static int hctx_tags_bitmap_show(struct seq_file *m, void *v)
446{
447 struct blk_mq_hw_ctx *hctx = m->private;
448 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800449 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800450
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800451 res = mutex_lock_interruptible(&q->sysfs_lock);
452 if (res)
453 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800454 if (hctx->tags)
455 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
456 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800457
458out:
459 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800460}
461
462static int hctx_tags_bitmap_open(struct inode *inode, struct file *file)
463{
464 return single_open(file, hctx_tags_bitmap_show, inode->i_private);
465}
466
467static const struct file_operations hctx_tags_bitmap_fops = {
468 .open = hctx_tags_bitmap_open,
469 .read = seq_read,
470 .llseek = seq_lseek,
471 .release = single_release,
472};
473
Omar Sandovald96b37c2017-01-25 08:06:46 -0800474static int hctx_sched_tags_show(struct seq_file *m, void *v)
475{
476 struct blk_mq_hw_ctx *hctx = m->private;
477 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800478 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800479
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800480 res = mutex_lock_interruptible(&q->sysfs_lock);
481 if (res)
482 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800483 if (hctx->sched_tags)
484 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
485 mutex_unlock(&q->sysfs_lock);
486
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800487out:
488 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800489}
490
491static int hctx_sched_tags_open(struct inode *inode, struct file *file)
492{
493 return single_open(file, hctx_sched_tags_show, inode->i_private);
494}
495
496static const struct file_operations hctx_sched_tags_fops = {
497 .open = hctx_sched_tags_open,
498 .read = seq_read,
499 .llseek = seq_lseek,
500 .release = single_release,
501};
502
Omar Sandovald7e36212017-01-25 08:06:47 -0800503static int hctx_sched_tags_bitmap_show(struct seq_file *m, void *v)
504{
505 struct blk_mq_hw_ctx *hctx = m->private;
506 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800507 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800508
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800509 res = mutex_lock_interruptible(&q->sysfs_lock);
510 if (res)
511 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800512 if (hctx->sched_tags)
513 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
514 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800515
516out:
517 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800518}
519
520static int hctx_sched_tags_bitmap_open(struct inode *inode, struct file *file)
521{
522 return single_open(file, hctx_sched_tags_bitmap_show, inode->i_private);
523}
524
525static const struct file_operations hctx_sched_tags_bitmap_fops = {
526 .open = hctx_sched_tags_bitmap_open,
527 .read = seq_read,
528 .llseek = seq_lseek,
529 .release = single_release,
530};
531
Omar Sandovalbe215472017-01-25 08:06:48 -0800532static int hctx_io_poll_show(struct seq_file *m, void *v)
533{
534 struct blk_mq_hw_ctx *hctx = m->private;
535
536 seq_printf(m, "considered=%lu\n", hctx->poll_considered);
537 seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
538 seq_printf(m, "success=%lu\n", hctx->poll_success);
539 return 0;
540}
541
542static int hctx_io_poll_open(struct inode *inode, struct file *file)
543{
544 return single_open(file, hctx_io_poll_show, inode->i_private);
545}
546
547static ssize_t hctx_io_poll_write(struct file *file, const char __user *buf,
548 size_t count, loff_t *ppos)
549{
550 struct seq_file *m = file->private_data;
551 struct blk_mq_hw_ctx *hctx = m->private;
552
553 hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
554 return count;
555}
556
557static const struct file_operations hctx_io_poll_fops = {
558 .open = hctx_io_poll_open,
559 .read = seq_read,
560 .write = hctx_io_poll_write,
561 .llseek = seq_lseek,
562 .release = single_release,
563};
564
Omar Sandovalbe215472017-01-25 08:06:48 -0800565static int hctx_dispatched_show(struct seq_file *m, void *v)
566{
567 struct blk_mq_hw_ctx *hctx = m->private;
568 int i;
569
570 seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
571
572 for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
573 unsigned int d = 1U << (i - 1);
574
575 seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
576 }
577
578 seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
579 return 0;
580}
581
582static int hctx_dispatched_open(struct inode *inode, struct file *file)
583{
584 return single_open(file, hctx_dispatched_show, inode->i_private);
585}
586
587static ssize_t hctx_dispatched_write(struct file *file, const char __user *buf,
588 size_t count, loff_t *ppos)
589{
590 struct seq_file *m = file->private_data;
591 struct blk_mq_hw_ctx *hctx = m->private;
592 int i;
593
594 for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
595 hctx->dispatched[i] = 0;
596 return count;
597}
598
599static const struct file_operations hctx_dispatched_fops = {
600 .open = hctx_dispatched_open,
601 .read = seq_read,
602 .write = hctx_dispatched_write,
603 .llseek = seq_lseek,
604 .release = single_release,
605};
606
Omar Sandoval4a46f052017-01-25 08:06:49 -0800607static int hctx_queued_show(struct seq_file *m, void *v)
608{
609 struct blk_mq_hw_ctx *hctx = m->private;
610
611 seq_printf(m, "%lu\n", hctx->queued);
612 return 0;
613}
614
615static int hctx_queued_open(struct inode *inode, struct file *file)
616{
617 return single_open(file, hctx_queued_show, inode->i_private);
618}
619
620static ssize_t hctx_queued_write(struct file *file, const char __user *buf,
621 size_t count, loff_t *ppos)
622{
623 struct seq_file *m = file->private_data;
624 struct blk_mq_hw_ctx *hctx = m->private;
625
626 hctx->queued = 0;
627 return count;
628}
629
630static const struct file_operations hctx_queued_fops = {
631 .open = hctx_queued_open,
632 .read = seq_read,
633 .write = hctx_queued_write,
634 .llseek = seq_lseek,
635 .release = single_release,
636};
637
638static int hctx_run_show(struct seq_file *m, void *v)
639{
640 struct blk_mq_hw_ctx *hctx = m->private;
641
642 seq_printf(m, "%lu\n", hctx->run);
643 return 0;
644}
645
646static int hctx_run_open(struct inode *inode, struct file *file)
647{
648 return single_open(file, hctx_run_show, inode->i_private);
649}
650
651static ssize_t hctx_run_write(struct file *file, const char __user *buf,
652 size_t count, loff_t *ppos)
653{
654 struct seq_file *m = file->private_data;
655 struct blk_mq_hw_ctx *hctx = m->private;
656
657 hctx->run = 0;
658 return count;
659}
660
661static const struct file_operations hctx_run_fops = {
662 .open = hctx_run_open,
663 .read = seq_read,
664 .write = hctx_run_write,
665 .llseek = seq_lseek,
666 .release = single_release,
667};
668
669static int hctx_active_show(struct seq_file *m, void *v)
670{
671 struct blk_mq_hw_ctx *hctx = m->private;
672
673 seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
674 return 0;
675}
676
677static int hctx_active_open(struct inode *inode, struct file *file)
678{
679 return single_open(file, hctx_active_show, inode->i_private);
680}
681
682static const struct file_operations hctx_active_fops = {
683 .open = hctx_active_open,
684 .read = seq_read,
685 .llseek = seq_lseek,
686 .release = single_release,
687};
688
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800689static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800690 __acquires(&ctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800691{
692 struct blk_mq_ctx *ctx = m->private;
693
694 spin_lock(&ctx->lock);
695 return seq_list_start(&ctx->rq_list, *pos);
696}
697
698static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
699{
700 struct blk_mq_ctx *ctx = m->private;
701
702 return seq_list_next(v, &ctx->rq_list, pos);
703}
704
705static void ctx_rq_list_stop(struct seq_file *m, void *v)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800706 __releases(&ctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800707{
708 struct blk_mq_ctx *ctx = m->private;
709
710 spin_unlock(&ctx->lock);
711}
712
713static const struct seq_operations ctx_rq_list_seq_ops = {
714 .start = ctx_rq_list_start,
715 .next = ctx_rq_list_next,
716 .stop = ctx_rq_list_stop,
717 .show = blk_mq_debugfs_rq_show,
718};
719
720static int ctx_rq_list_open(struct inode *inode, struct file *file)
721{
722 return blk_mq_debugfs_seq_open(inode, file, &ctx_rq_list_seq_ops);
723}
724
725static const struct file_operations ctx_rq_list_fops = {
726 .open = ctx_rq_list_open,
727 .read = seq_read,
728 .llseek = seq_lseek,
729 .release = seq_release,
730};
731
Omar Sandoval4a46f052017-01-25 08:06:49 -0800732static int ctx_dispatched_show(struct seq_file *m, void *v)
733{
734 struct blk_mq_ctx *ctx = m->private;
735
736 seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
737 return 0;
738}
739
740static int ctx_dispatched_open(struct inode *inode, struct file *file)
741{
742 return single_open(file, ctx_dispatched_show, inode->i_private);
743}
744
745static ssize_t ctx_dispatched_write(struct file *file, const char __user *buf,
746 size_t count, loff_t *ppos)
747{
748 struct seq_file *m = file->private_data;
749 struct blk_mq_ctx *ctx = m->private;
750
751 ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
752 return count;
753}
754
755static const struct file_operations ctx_dispatched_fops = {
756 .open = ctx_dispatched_open,
757 .read = seq_read,
758 .write = ctx_dispatched_write,
759 .llseek = seq_lseek,
760 .release = single_release,
761};
762
763static int ctx_merged_show(struct seq_file *m, void *v)
764{
765 struct blk_mq_ctx *ctx = m->private;
766
767 seq_printf(m, "%lu\n", ctx->rq_merged);
768 return 0;
769}
770
771static int ctx_merged_open(struct inode *inode, struct file *file)
772{
773 return single_open(file, ctx_merged_show, inode->i_private);
774}
775
776static ssize_t ctx_merged_write(struct file *file, const char __user *buf,
777 size_t count, loff_t *ppos)
778{
779 struct seq_file *m = file->private_data;
780 struct blk_mq_ctx *ctx = m->private;
781
782 ctx->rq_merged = 0;
783 return count;
784}
785
786static const struct file_operations ctx_merged_fops = {
787 .open = ctx_merged_open,
788 .read = seq_read,
789 .write = ctx_merged_write,
790 .llseek = seq_lseek,
791 .release = single_release,
792};
793
794static int ctx_completed_show(struct seq_file *m, void *v)
795{
796 struct blk_mq_ctx *ctx = m->private;
797
798 seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
799 return 0;
800}
801
802static int ctx_completed_open(struct inode *inode, struct file *file)
803{
804 return single_open(file, ctx_completed_show, inode->i_private);
805}
806
807static ssize_t ctx_completed_write(struct file *file, const char __user *buf,
808 size_t count, loff_t *ppos)
809{
810 struct seq_file *m = file->private_data;
811 struct blk_mq_ctx *ctx = m->private;
812
813 ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
814 return count;
815}
816
817static const struct file_operations ctx_completed_fops = {
818 .open = ctx_completed_open,
819 .read = seq_read,
820 .write = ctx_completed_write,
821 .llseek = seq_lseek,
822 .release = single_release,
823};
824
Omar Sandoval34dbad52017-03-21 08:56:08 -0700825static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
826 {"poll_stat", 0400, &queue_poll_stat_fops},
Bart Van Assche65ca1ca2017-04-26 13:47:53 -0700827 {"state", 0600, &blk_queue_flags_fops},
Omar Sandoval34dbad52017-03-21 08:56:08 -0700828 {},
829};
830
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800831static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800832 {"state", 0400, &hctx_state_fops},
833 {"flags", 0400, &hctx_flags_fops},
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800834 {"dispatch", 0400, &hctx_dispatch_fops},
Omar Sandoval0bfa5282017-01-25 08:06:45 -0800835 {"ctx_map", 0400, &hctx_ctx_map_fops},
Omar Sandovald96b37c2017-01-25 08:06:46 -0800836 {"tags", 0400, &hctx_tags_fops},
Omar Sandovald7e36212017-01-25 08:06:47 -0800837 {"tags_bitmap", 0400, &hctx_tags_bitmap_fops},
Omar Sandovald96b37c2017-01-25 08:06:46 -0800838 {"sched_tags", 0400, &hctx_sched_tags_fops},
Omar Sandovald7e36212017-01-25 08:06:47 -0800839 {"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops},
Omar Sandovalbe215472017-01-25 08:06:48 -0800840 {"io_poll", 0600, &hctx_io_poll_fops},
Omar Sandovalbe215472017-01-25 08:06:48 -0800841 {"dispatched", 0600, &hctx_dispatched_fops},
Omar Sandoval4a46f052017-01-25 08:06:49 -0800842 {"queued", 0600, &hctx_queued_fops},
843 {"run", 0600, &hctx_run_fops},
844 {"active", 0400, &hctx_active_fops},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800845 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800846};
847
848static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800849 {"rq_list", 0400, &ctx_rq_list_fops},
Omar Sandoval4a46f052017-01-25 08:06:49 -0800850 {"dispatched", 0600, &ctx_dispatched_fops},
851 {"merged", 0600, &ctx_merged_fops},
852 {"completed", 0600, &ctx_completed_fops},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800853 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800854};
855
Bart Van Assche4c9e4012017-04-26 13:47:49 -0700856int blk_mq_debugfs_register(struct request_queue *q)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800857{
Omar Sandoval18fbda92017-01-31 14:53:20 -0800858 if (!blk_debugfs_root)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800859 return -ENOENT;
860
Bart Van Assche4c9e4012017-04-26 13:47:49 -0700861 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
862 blk_debugfs_root);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800863 if (!q->debugfs_dir)
864 goto err;
865
Bart Van Assche62d6c942017-04-26 13:47:50 -0700866 if (blk_mq_debugfs_register_mq(q))
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800867 goto err;
868
869 return 0;
870
871err:
872 blk_mq_debugfs_unregister(q);
873 return -ENOMEM;
874}
875
876void blk_mq_debugfs_unregister(struct request_queue *q)
877{
878 debugfs_remove_recursive(q->debugfs_dir);
879 q->mq_debugfs_dir = NULL;
880 q->debugfs_dir = NULL;
881}
882
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800883static bool debugfs_create_files(struct dentry *parent, void *data,
884 const struct blk_mq_debugfs_attr *attr)
885{
886 for (; attr->name; attr++) {
887 if (!debugfs_create_file(attr->name, attr->mode, parent,
888 data, attr->fops))
889 return false;
890 }
891 return true;
892}
893
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800894static int blk_mq_debugfs_register_ctx(struct request_queue *q,
895 struct blk_mq_ctx *ctx,
896 struct dentry *hctx_dir)
897{
898 struct dentry *ctx_dir;
899 char name[20];
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800900
901 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
902 ctx_dir = debugfs_create_dir(name, hctx_dir);
903 if (!ctx_dir)
904 return -ENOMEM;
905
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800906 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
907 return -ENOMEM;
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800908
909 return 0;
910}
911
912static int blk_mq_debugfs_register_hctx(struct request_queue *q,
913 struct blk_mq_hw_ctx *hctx)
914{
915 struct blk_mq_ctx *ctx;
916 struct dentry *hctx_dir;
917 char name[20];
918 int i;
919
920 snprintf(name, sizeof(name), "%u", hctx->queue_num);
921 hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
922 if (!hctx_dir)
923 return -ENOMEM;
924
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800925 if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
926 return -ENOMEM;
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800927
928 hctx_for_each_ctx(hctx, ctx, i) {
929 if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
930 return -ENOMEM;
931 }
932
933 return 0;
934}
935
Bart Van Assche62d6c942017-04-26 13:47:50 -0700936int blk_mq_debugfs_register_mq(struct request_queue *q)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800937{
938 struct blk_mq_hw_ctx *hctx;
939 int i;
940
941 if (!q->debugfs_dir)
942 return -ENOENT;
943
944 q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
945 if (!q->mq_debugfs_dir)
946 goto err;
947
Omar Sandoval34dbad52017-03-21 08:56:08 -0700948 if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
949 goto err;
950
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800951 queue_for_each_hw_ctx(q, hctx, i) {
952 if (blk_mq_debugfs_register_hctx(q, hctx))
953 goto err;
954 }
955
956 return 0;
957
958err:
Bart Van Assche62d6c942017-04-26 13:47:50 -0700959 blk_mq_debugfs_unregister_mq(q);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800960 return -ENOMEM;
961}
962
Bart Van Assche62d6c942017-04-26 13:47:50 -0700963void blk_mq_debugfs_unregister_mq(struct request_queue *q)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800964{
965 debugfs_remove_recursive(q->mq_debugfs_dir);
966 q->mq_debugfs_dir = NULL;
967}