blob: 1dc1847b53636c2f69f64ec183229609982110e4 [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 Sandovald173a252017-05-04 00:31:30 -070024#include "blk-mq-debugfs.h"
Omar Sandovald96b37c2017-01-25 08:06:46 -080025#include "blk-mq-tag.h"
Omar Sandoval07e4fea2017-01-25 08:06:40 -080026
27struct blk_mq_debugfs_attr {
28 const char *name;
29 umode_t mode;
Omar Sandovalf57de232017-05-04 00:31:28 -070030 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;
Omar Sandoval07e4fea2017-01-25 08:06:40 -080034};
35
Bart Van Assche91d68902017-04-10 16:13:15 -060036static 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)
Omar Sandovalbec03d62017-05-04 00:31:23 -070046 seq_puts(m, "|");
Bart Van Assche91d68902017-04-10 16:13:15 -060047 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 }
Bart Van Assche91d68902017-04-10 16:13:15 -060053 return 0;
54}
55
Omar Sandoval1a435112017-05-04 00:31:24 -070056#define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
Bart Van Assche91d68902017-04-10 16:13:15 -060057static const char *const blk_queue_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -070058 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),
Bart Van Assche91d68902017-04-10 16:13:15 -060087};
Omar Sandoval1a435112017-05-04 00:31:24 -070088#undef QUEUE_FLAG_NAME
Bart Van Assche91d68902017-04-10 16:13:15 -060089
Omar Sandovalf57de232017-05-04 00:31:28 -070090static int queue_state_show(void *data, struct seq_file *m)
Bart Van Assche91d68902017-04-10 16:13:15 -060091{
Omar Sandovalf57de232017-05-04 00:31:28 -070092 struct request_queue *q = data;
Bart Van Assche91d68902017-04-10 16:13:15 -060093
94 blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
95 ARRAY_SIZE(blk_queue_flag_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -070096 seq_puts(m, "\n");
Bart Van Assche91d68902017-04-10 16:13:15 -060097 return 0;
98}
99
Omar Sandovalf57de232017-05-04 00:31:28 -0700100static ssize_t queue_state_write(void *data, const char __user *buf,
101 size_t count, loff_t *ppos)
Bart Van Assche91d68902017-04-10 16:13:15 -0600102{
Omar Sandovalf57de232017-05-04 00:31:28 -0700103 struct request_queue *q = data;
Omar Sandoval71b90512017-05-04 00:31:26 -0700104 char opbuf[16] = { }, *op;
Bart Van Assche91d68902017-04-10 16:13:15 -0600105
Bart Van Assche18d4d7d2017-05-04 00:31:29 -0700106 /*
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
Omar Sandoval71b90512017-05-04 00:31:26 -0700114 if (count >= sizeof(opbuf)) {
Omar Sandovalc7e41452017-05-04 00:31:25 -0700115 pr_err("%s: operation too long\n", __func__);
116 goto inval;
117 }
118
Omar Sandoval71b90512017-05-04 00:31:26 -0700119 if (copy_from_user(opbuf, buf, count))
Bart Van Assche91d68902017-04-10 16:13:15 -0600120 return -EFAULT;
Omar Sandoval71b90512017-05-04 00:31:26 -0700121 op = strstrip(opbuf);
Bart Van Assche91d68902017-04-10 16:13:15 -0600122 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 {
Omar Sandovalc7e41452017-05-04 00:31:25 -0700127 pr_err("%s: unsupported operation '%s'\n", __func__, op);
128inval:
129 pr_err("%s: use either 'run' or 'start'\n", __func__);
Bart Van Assche91d68902017-04-10 16:13:15 -0600130 return -EINVAL;
131 }
Omar Sandovalc7e41452017-05-04 00:31:25 -0700132 return count;
Bart Van Assche91d68902017-04-10 16:13:15 -0600133}
134
Omar Sandoval34dbad52017-03-21 08:56:08 -0700135static 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
Omar Sandovalf57de232017-05-04 00:31:28 -0700145static int queue_poll_stat_show(void *data, struct seq_file *m)
Omar Sandoval34dbad52017-03-21 08:56:08 -0700146{
Omar Sandovalf57de232017-05-04 00:31:28 -0700147 struct request_queue *q = data;
Stephen Bates02063192017-04-20 16:59:11 -0600148 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700149
Stephen Bates02063192017-04-20 16:59:11 -0600150 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");
Omar Sandoval34dbad52017-03-21 08:56:08 -0700154
Stephen Bates02063192017-04-20 16:59:11 -0600155 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 }
Omar Sandoval34dbad52017-03-21 08:56:08 -0700159 return 0;
160}
161
Omar Sandoval1a435112017-05-04 00:31:24 -0700162#define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700163static const char *const hctx_state_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700164 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),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700169};
Omar Sandoval1a435112017-05-04 00:31:24 -0700170#undef HCTX_STATE_NAME
171
Omar Sandovalf57de232017-05-04 00:31:28 -0700172static int hctx_state_show(void *data, struct seq_file *m)
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800173{
Omar Sandovalf57de232017-05-04 00:31:28 -0700174 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800175
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700176 blk_flags_show(m, hctx->state, hctx_state_name,
177 ARRAY_SIZE(hctx_state_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700178 seq_puts(m, "\n");
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800179 return 0;
180}
181
Omar Sandoval1a435112017-05-04 00:31:24 -0700182#define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700183static const char *const alloc_policy_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700184 BLK_TAG_ALLOC_NAME(FIFO),
185 BLK_TAG_ALLOC_NAME(RR),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700186};
Omar Sandoval1a435112017-05-04 00:31:24 -0700187#undef BLK_TAG_ALLOC_NAME
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700188
Omar Sandoval1a435112017-05-04 00:31:24 -0700189#define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700190static const char *const hctx_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700191 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),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700196};
Omar Sandoval1a435112017-05-04 00:31:24 -0700197#undef HCTX_FLAG_NAME
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700198
Omar Sandovalf57de232017-05-04 00:31:28 -0700199static int hctx_flags_show(void *data, struct seq_file *m)
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800200{
Omar Sandovalf57de232017-05-04 00:31:28 -0700201 struct blk_mq_hw_ctx *hctx = data;
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700202 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800203
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700204 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));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700214 seq_puts(m, "\n");
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800215 return 0;
216}
217
Omar Sandoval1a435112017-05-04 00:31:24 -0700218#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700219static const char *const op_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700220 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),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700233};
Omar Sandoval1a435112017-05-04 00:31:24 -0700234#undef REQ_OP_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700235
Omar Sandoval1a435112017-05-04 00:31:24 -0700236#define CMD_FLAG_NAME(name) [__REQ_##name] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700237static const char *const cmd_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700238 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),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700252};
Omar Sandoval1a435112017-05-04 00:31:24 -0700253#undef CMD_FLAG_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700254
Omar Sandoval1a435112017-05-04 00:31:24 -0700255#define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700256static const char *const rqf_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700257 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),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700276};
Omar Sandoval1a435112017-05-04 00:31:24 -0700277#undef RQF_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700278
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800279static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
280{
281 struct request *rq = list_entry_rq(v);
Bart Van Assche2836ee42017-04-26 13:47:56 -0700282 const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
Bart Van Assche8658dca2017-04-26 13:47:55 -0700283 const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800284
Bart Van Assche8658dca2017-04-26 13:47:55 -0700285 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));
Bart Van Assche2836ee42017-04-26 13:47:56 -0700296 seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
Bart Van Assche8658dca2017-04-26 13:47:55 -0700297 rq->internal_tag);
Bart Van Assche2836ee42017-04-26 13:47:56 -0700298 if (mq_ops->show_rq)
299 mq_ops->show_rq(m, rq);
300 seq_puts(m, "}\n");
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800301 return 0;
302}
303
304static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800305 __acquires(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800306{
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)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800321 __releases(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800322{
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
Omar Sandovalf57de232017-05-04 00:31:28 -0700335static int hctx_ctx_map_show(void *data, struct seq_file *m)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800336{
Omar Sandovalf57de232017-05-04 00:31:28 -0700337 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval0bfa5282017-01-25 08:06:45 -0800338
339 sbitmap_bitmap_show(&hctx->ctx_map, m);
340 return 0;
341}
342
Omar Sandovald96b37c2017-01-25 08:06:46 -0800343static 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
Omar Sandovalf57de232017-05-04 00:31:28 -0700360static int hctx_tags_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800361{
Omar Sandovalf57de232017-05-04 00:31:28 -0700362 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800363 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800364 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800365
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800366 res = mutex_lock_interruptible(&q->sysfs_lock);
367 if (res)
368 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800369 if (hctx->tags)
370 blk_mq_debugfs_tags_show(m, hctx->tags);
371 mutex_unlock(&q->sysfs_lock);
372
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800373out:
374 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800375}
376
Omar Sandovalf57de232017-05-04 00:31:28 -0700377static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800378{
Omar Sandovalf57de232017-05-04 00:31:28 -0700379 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald7e36212017-01-25 08:06:47 -0800380 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800381 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800382
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800383 res = mutex_lock_interruptible(&q->sysfs_lock);
384 if (res)
385 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800386 if (hctx->tags)
387 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
388 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800389
390out:
391 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800392}
393
Omar Sandovalf57de232017-05-04 00:31:28 -0700394static int hctx_sched_tags_show(void *data, struct seq_file *m)
Omar Sandovald7e36212017-01-25 08:06:47 -0800395{
Omar Sandovalf57de232017-05-04 00:31:28 -0700396 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800397 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800398 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800399
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800400 res = mutex_lock_interruptible(&q->sysfs_lock);
401 if (res)
402 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800403 if (hctx->sched_tags)
404 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
405 mutex_unlock(&q->sysfs_lock);
406
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800407out:
408 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800409}
410
Omar Sandovalf57de232017-05-04 00:31:28 -0700411static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800412{
Omar Sandovalf57de232017-05-04 00:31:28 -0700413 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald7e36212017-01-25 08:06:47 -0800414 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800415 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800416
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800417 res = mutex_lock_interruptible(&q->sysfs_lock);
418 if (res)
419 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800420 if (hctx->sched_tags)
421 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
422 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800423
424out:
425 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800426}
427
Omar Sandovalf57de232017-05-04 00:31:28 -0700428static int hctx_io_poll_show(void *data, struct seq_file *m)
Omar Sandovald7e36212017-01-25 08:06:47 -0800429{
Omar Sandovalf57de232017-05-04 00:31:28 -0700430 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800431
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
Omar Sandovalf57de232017-05-04 00:31:28 -0700438static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
Omar Sandovalbe215472017-01-25 08:06:48 -0800439 size_t count, loff_t *ppos)
440{
Omar Sandovalf57de232017-05-04 00:31:28 -0700441 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800442
443 hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
444 return count;
445}
446
Omar Sandovalf57de232017-05-04 00:31:28 -0700447static int hctx_dispatched_show(void *data, struct seq_file *m)
Omar Sandovalbe215472017-01-25 08:06:48 -0800448{
Omar Sandovalf57de232017-05-04 00:31:28 -0700449 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800450 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
Omar Sandovalf57de232017-05-04 00:31:28 -0700464static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
Omar Sandovalbe215472017-01-25 08:06:48 -0800465 size_t count, loff_t *ppos)
466{
Omar Sandovalf57de232017-05-04 00:31:28 -0700467 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800468 int i;
469
470 for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
471 hctx->dispatched[i] = 0;
472 return count;
473}
474
Omar Sandovalf57de232017-05-04 00:31:28 -0700475static int hctx_queued_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800476{
Omar Sandovalf57de232017-05-04 00:31:28 -0700477 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800478
479 seq_printf(m, "%lu\n", hctx->queued);
480 return 0;
481}
482
Omar Sandovalf57de232017-05-04 00:31:28 -0700483static ssize_t hctx_queued_write(void *data, const char __user *buf,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800484 size_t count, loff_t *ppos)
485{
Omar Sandovalf57de232017-05-04 00:31:28 -0700486 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800487
488 hctx->queued = 0;
489 return count;
490}
491
Omar Sandovalf57de232017-05-04 00:31:28 -0700492static int hctx_run_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800493{
Omar Sandovalf57de232017-05-04 00:31:28 -0700494 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800495
496 seq_printf(m, "%lu\n", hctx->run);
497 return 0;
498}
499
Omar Sandovalf57de232017-05-04 00:31:28 -0700500static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
501 loff_t *ppos)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800502{
Omar Sandovalf57de232017-05-04 00:31:28 -0700503 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800504
505 hctx->run = 0;
506 return count;
507}
508
Omar Sandovalf57de232017-05-04 00:31:28 -0700509static int hctx_active_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800510{
Omar Sandovalf57de232017-05-04 00:31:28 -0700511 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800512
513 seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
514 return 0;
515}
516
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800517static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800518 __acquires(&ctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800519{
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)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800534 __releases(&ctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800535{
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};
Omar Sandovalf57de232017-05-04 00:31:28 -0700547static int ctx_dispatched_show(void *data, struct seq_file *m)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800548{
Omar Sandovalf57de232017-05-04 00:31:28 -0700549 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800550
551 seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
552 return 0;
553}
554
Omar Sandovalf57de232017-05-04 00:31:28 -0700555static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800556 size_t count, loff_t *ppos)
557{
Omar Sandovalf57de232017-05-04 00:31:28 -0700558 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800559
560 ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
561 return count;
562}
563
Omar Sandovalf57de232017-05-04 00:31:28 -0700564static int ctx_merged_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800565{
Omar Sandovalf57de232017-05-04 00:31:28 -0700566 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800567
568 seq_printf(m, "%lu\n", ctx->rq_merged);
569 return 0;
570}
571
Omar Sandovalf57de232017-05-04 00:31:28 -0700572static ssize_t ctx_merged_write(void *data, const char __user *buf,
573 size_t count, loff_t *ppos)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800574{
Omar Sandovalf57de232017-05-04 00:31:28 -0700575 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800576
577 ctx->rq_merged = 0;
578 return count;
579}
580
Omar Sandovalf57de232017-05-04 00:31:28 -0700581static int ctx_completed_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800582{
Omar Sandovalf57de232017-05-04 00:31:28 -0700583 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800584
585 seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
586 return 0;
587}
588
Omar Sandovalf57de232017-05-04 00:31:28 -0700589static ssize_t ctx_completed_write(void *data, const char __user *buf,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800590 size_t count, loff_t *ppos)
591{
Omar Sandovalf57de232017-05-04 00:31:28 -0700592 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800593
594 ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
595 return count;
596}
597
Omar Sandovalf57de232017-05-04 00:31:28 -0700598static 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)
608{
609 struct seq_file *m = file->private_data;
610 const struct blk_mq_debugfs_attr *attr = m->private;
611 void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
612
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);
639}
640
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,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800653 .read = seq_read,
Omar Sandovalf57de232017-05-04 00:31:28 -0700654 .write = blk_mq_debugfs_write,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800655 .llseek = seq_lseek,
Omar Sandovalf57de232017-05-04 00:31:28 -0700656 .release = blk_mq_debugfs_release,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800657};
658
Omar Sandoval34dbad52017-03-21 08:56:08 -0700659static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700660 {"poll_stat", 0400, queue_poll_stat_show},
661 {"state", 0600, queue_state_show, queue_state_write},
Omar Sandoval34dbad52017-03-21 08:56:08 -0700662 {},
663};
664
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800665static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700666 {"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},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800679 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800680};
681
682static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700683 {"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},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800687 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800688};
689
Bart Van Assche4c9e4012017-04-26 13:47:49 -0700690int blk_mq_debugfs_register(struct request_queue *q)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800691{
Omar Sandoval18fbda92017-01-31 14:53:20 -0800692 if (!blk_debugfs_root)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800693 return -ENOENT;
694
Bart Van Assche4c9e4012017-04-26 13:47:49 -0700695 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
696 blk_debugfs_root);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800697 if (!q->debugfs_dir)
698 goto err;
699
Bart Van Assche62d6c942017-04-26 13:47:50 -0700700 if (blk_mq_debugfs_register_mq(q))
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800701 goto err;
702
703 return 0;
704
705err:
706 blk_mq_debugfs_unregister(q);
707 return -ENOMEM;
708}
709
710void blk_mq_debugfs_unregister(struct request_queue *q)
711{
712 debugfs_remove_recursive(q->debugfs_dir);
713 q->mq_debugfs_dir = NULL;
714 q->debugfs_dir = NULL;
715}
716
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800717static bool debugfs_create_files(struct dentry *parent, void *data,
Omar Sandovalf57de232017-05-04 00:31:28 -0700718 const struct blk_mq_debugfs_attr *attr)
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800719{
Omar Sandovalf57de232017-05-04 00:31:28 -0700720 d_inode(parent)->i_private = data;
721
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800722 for (; attr->name; attr++) {
723 if (!debugfs_create_file(attr->name, attr->mode, parent,
Omar Sandovalf57de232017-05-04 00:31:28 -0700724 (void *)attr, &blk_mq_debugfs_fops))
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800725 return false;
726 }
727 return true;
728}
729
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800730static int blk_mq_debugfs_register_ctx(struct request_queue *q,
731 struct blk_mq_ctx *ctx,
732 struct dentry *hctx_dir)
733{
734 struct dentry *ctx_dir;
735 char name[20];
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800736
737 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
738 ctx_dir = debugfs_create_dir(name, hctx_dir);
739 if (!ctx_dir)
740 return -ENOMEM;
741
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800742 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
743 return -ENOMEM;
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800744
745 return 0;
746}
747
748static int blk_mq_debugfs_register_hctx(struct request_queue *q,
749 struct blk_mq_hw_ctx *hctx)
750{
751 struct blk_mq_ctx *ctx;
752 struct dentry *hctx_dir;
753 char name[20];
754 int i;
755
Omar Sandoval88aabbd2017-05-04 00:31:27 -0700756 snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800757 hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir);
758 if (!hctx_dir)
759 return -ENOMEM;
760
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800761 if (!debugfs_create_files(hctx_dir, hctx, blk_mq_debugfs_hctx_attrs))
762 return -ENOMEM;
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800763
764 hctx_for_each_ctx(hctx, ctx, i) {
765 if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir))
766 return -ENOMEM;
767 }
768
769 return 0;
770}
771
Bart Van Assche62d6c942017-04-26 13:47:50 -0700772int blk_mq_debugfs_register_mq(struct request_queue *q)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800773{
774 struct blk_mq_hw_ctx *hctx;
775 int i;
776
777 if (!q->debugfs_dir)
778 return -ENOENT;
779
780 q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir);
781 if (!q->mq_debugfs_dir)
782 goto err;
783
Omar Sandoval34dbad52017-03-21 08:56:08 -0700784 if (!debugfs_create_files(q->mq_debugfs_dir, q, blk_mq_debugfs_queue_attrs))
785 goto err;
786
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800787 queue_for_each_hw_ctx(q, hctx, i) {
788 if (blk_mq_debugfs_register_hctx(q, hctx))
789 goto err;
790 }
791
792 return 0;
793
794err:
Bart Van Assche62d6c942017-04-26 13:47:50 -0700795 blk_mq_debugfs_unregister_mq(q);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800796 return -ENOMEM;
797}
798
Bart Van Assche62d6c942017-04-26 13:47:50 -0700799void blk_mq_debugfs_unregister_mq(struct request_queue *q)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800800{
801 debugfs_remove_recursive(q->mq_debugfs_dir);
802 q->mq_debugfs_dir = NULL;
803}