blob: 4f927a58dff86183a95baf9c7f4eb474c218f0f2 [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
Bart Van Assche91d68902017-04-10 16:13:15 -060027static 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)
Omar Sandovalbec03d62017-05-04 00:31:23 -070037 seq_puts(m, "|");
Bart Van Assche91d68902017-04-10 16:13:15 -060038 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 }
Bart Van Assche91d68902017-04-10 16:13:15 -060044 return 0;
45}
46
Omar Sandoval1a435112017-05-04 00:31:24 -070047#define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
Bart Van Assche91d68902017-04-10 16:13:15 -060048static const char *const blk_queue_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -070049 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),
Bart Van Assche22d53822017-08-18 15:52:54 -070078 QUEUE_FLAG_NAME(SCSI_PASSTHROUGH),
79 QUEUE_FLAG_NAME(QUIESCED),
Bart Van Assche91d68902017-04-10 16:13:15 -060080};
Omar Sandoval1a435112017-05-04 00:31:24 -070081#undef QUEUE_FLAG_NAME
Bart Van Assche91d68902017-04-10 16:13:15 -060082
Omar Sandovalf57de232017-05-04 00:31:28 -070083static int queue_state_show(void *data, struct seq_file *m)
Bart Van Assche91d68902017-04-10 16:13:15 -060084{
Omar Sandovalf57de232017-05-04 00:31:28 -070085 struct request_queue *q = data;
Bart Van Assche91d68902017-04-10 16:13:15 -060086
87 blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
88 ARRAY_SIZE(blk_queue_flag_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -070089 seq_puts(m, "\n");
Bart Van Assche91d68902017-04-10 16:13:15 -060090 return 0;
91}
92
Omar Sandovalf57de232017-05-04 00:31:28 -070093static ssize_t queue_state_write(void *data, const char __user *buf,
94 size_t count, loff_t *ppos)
Bart Van Assche91d68902017-04-10 16:13:15 -060095{
Omar Sandovalf57de232017-05-04 00:31:28 -070096 struct request_queue *q = data;
Omar Sandoval71b90512017-05-04 00:31:26 -070097 char opbuf[16] = { }, *op;
Bart Van Assche91d68902017-04-10 16:13:15 -060098
Bart Van Assche18d4d7d2017-05-04 00:31:29 -070099 /*
100 * The "state" attribute is removed after blk_cleanup_queue() has called
101 * blk_mq_free_queue(). Return if QUEUE_FLAG_DEAD has been set to avoid
102 * triggering a use-after-free.
103 */
104 if (blk_queue_dead(q))
105 return -ENOENT;
106
Omar Sandoval71b90512017-05-04 00:31:26 -0700107 if (count >= sizeof(opbuf)) {
Omar Sandovalc7e41452017-05-04 00:31:25 -0700108 pr_err("%s: operation too long\n", __func__);
109 goto inval;
110 }
111
Omar Sandoval71b90512017-05-04 00:31:26 -0700112 if (copy_from_user(opbuf, buf, count))
Bart Van Assche91d68902017-04-10 16:13:15 -0600113 return -EFAULT;
Omar Sandoval71b90512017-05-04 00:31:26 -0700114 op = strstrip(opbuf);
Bart Van Assche91d68902017-04-10 16:13:15 -0600115 if (strcmp(op, "run") == 0) {
116 blk_mq_run_hw_queues(q, true);
117 } else if (strcmp(op, "start") == 0) {
118 blk_mq_start_stopped_hw_queues(q, true);
Bart Van Asscheedea55a2017-06-01 08:55:13 -0700119 } else if (strcmp(op, "kick") == 0) {
120 blk_mq_kick_requeue_list(q);
Bart Van Assche91d68902017-04-10 16:13:15 -0600121 } else {
Omar Sandovalc7e41452017-05-04 00:31:25 -0700122 pr_err("%s: unsupported operation '%s'\n", __func__, op);
123inval:
Bart Van Asscheedea55a2017-06-01 08:55:13 -0700124 pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
Bart Van Assche91d68902017-04-10 16:13:15 -0600125 return -EINVAL;
126 }
Omar Sandovalc7e41452017-05-04 00:31:25 -0700127 return count;
Bart Van Assche91d68902017-04-10 16:13:15 -0600128}
129
Omar Sandoval34dbad52017-03-21 08:56:08 -0700130static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
131{
132 if (stat->nr_samples) {
133 seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
134 stat->nr_samples, stat->mean, stat->min, stat->max);
135 } else {
136 seq_puts(m, "samples=0");
137 }
138}
139
Jens Axboef793dfd2017-06-26 08:15:27 -0600140static int queue_write_hint_show(void *data, struct seq_file *m)
141{
142 struct request_queue *q = data;
143 int i;
144
145 for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
146 seq_printf(m, "hint%d: %llu\n", i, q->write_hints[i]);
147
148 return 0;
149}
150
151static ssize_t queue_write_hint_store(void *data, const char __user *buf,
152 size_t count, loff_t *ppos)
153{
154 struct request_queue *q = data;
155 int i;
156
157 for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
158 q->write_hints[i] = 0;
159
160 return count;
161}
162
Omar Sandovalf57de232017-05-04 00:31:28 -0700163static int queue_poll_stat_show(void *data, struct seq_file *m)
Omar Sandoval34dbad52017-03-21 08:56:08 -0700164{
Omar Sandovalf57de232017-05-04 00:31:28 -0700165 struct request_queue *q = data;
Stephen Bates02063192017-04-20 16:59:11 -0600166 int bucket;
Omar Sandoval34dbad52017-03-21 08:56:08 -0700167
Stephen Bates02063192017-04-20 16:59:11 -0600168 for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
169 seq_printf(m, "read (%d Bytes): ", 1 << (9+bucket));
170 print_stat(m, &q->poll_stat[2*bucket]);
171 seq_puts(m, "\n");
Omar Sandoval34dbad52017-03-21 08:56:08 -0700172
Stephen Bates02063192017-04-20 16:59:11 -0600173 seq_printf(m, "write (%d Bytes): ", 1 << (9+bucket));
174 print_stat(m, &q->poll_stat[2*bucket+1]);
175 seq_puts(m, "\n");
176 }
Omar Sandoval34dbad52017-03-21 08:56:08 -0700177 return 0;
178}
179
Omar Sandoval1a435112017-05-04 00:31:24 -0700180#define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700181static const char *const hctx_state_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700182 HCTX_STATE_NAME(STOPPED),
183 HCTX_STATE_NAME(TAG_ACTIVE),
184 HCTX_STATE_NAME(SCHED_RESTART),
185 HCTX_STATE_NAME(TAG_WAITING),
186 HCTX_STATE_NAME(START_ON_RUN),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700187};
Omar Sandoval1a435112017-05-04 00:31:24 -0700188#undef HCTX_STATE_NAME
189
Omar Sandovalf57de232017-05-04 00:31:28 -0700190static int hctx_state_show(void *data, struct seq_file *m)
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800191{
Omar Sandovalf57de232017-05-04 00:31:28 -0700192 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800193
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
Omar Sandoval1a435112017-05-04 00:31:24 -0700200#define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700201static const char *const alloc_policy_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700202 BLK_TAG_ALLOC_NAME(FIFO),
203 BLK_TAG_ALLOC_NAME(RR),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700204};
Omar Sandoval1a435112017-05-04 00:31:24 -0700205#undef BLK_TAG_ALLOC_NAME
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700206
Omar Sandoval1a435112017-05-04 00:31:24 -0700207#define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700208static const char *const hctx_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700209 HCTX_FLAG_NAME(SHOULD_MERGE),
210 HCTX_FLAG_NAME(TAG_SHARED),
211 HCTX_FLAG_NAME(SG_MERGE),
212 HCTX_FLAG_NAME(BLOCKING),
213 HCTX_FLAG_NAME(NO_SCHED),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700214};
Omar Sandoval1a435112017-05-04 00:31:24 -0700215#undef HCTX_FLAG_NAME
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700216
Omar Sandovalf57de232017-05-04 00:31:28 -0700217static int hctx_flags_show(void *data, struct seq_file *m)
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800218{
Omar Sandovalf57de232017-05-04 00:31:28 -0700219 struct blk_mq_hw_ctx *hctx = data;
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700220 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800221
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700222 seq_puts(m, "alloc_policy=");
223 if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
224 alloc_policy_name[alloc_policy])
225 seq_puts(m, alloc_policy_name[alloc_policy]);
226 else
227 seq_printf(m, "%d", alloc_policy);
228 seq_puts(m, " ");
229 blk_flags_show(m,
230 hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
231 hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700232 seq_puts(m, "\n");
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800233 return 0;
234}
235
Omar Sandoval1a435112017-05-04 00:31:24 -0700236#define REQ_OP_NAME(name) [REQ_OP_##name] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700237static const char *const op_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700238 REQ_OP_NAME(READ),
239 REQ_OP_NAME(WRITE),
240 REQ_OP_NAME(FLUSH),
241 REQ_OP_NAME(DISCARD),
242 REQ_OP_NAME(ZONE_REPORT),
243 REQ_OP_NAME(SECURE_ERASE),
244 REQ_OP_NAME(ZONE_RESET),
245 REQ_OP_NAME(WRITE_SAME),
246 REQ_OP_NAME(WRITE_ZEROES),
247 REQ_OP_NAME(SCSI_IN),
248 REQ_OP_NAME(SCSI_OUT),
249 REQ_OP_NAME(DRV_IN),
250 REQ_OP_NAME(DRV_OUT),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700251};
Omar Sandoval1a435112017-05-04 00:31:24 -0700252#undef REQ_OP_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700253
Omar Sandoval1a435112017-05-04 00:31:24 -0700254#define CMD_FLAG_NAME(name) [__REQ_##name] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700255static const char *const cmd_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700256 CMD_FLAG_NAME(FAILFAST_DEV),
257 CMD_FLAG_NAME(FAILFAST_TRANSPORT),
258 CMD_FLAG_NAME(FAILFAST_DRIVER),
259 CMD_FLAG_NAME(SYNC),
260 CMD_FLAG_NAME(META),
261 CMD_FLAG_NAME(PRIO),
262 CMD_FLAG_NAME(NOMERGE),
263 CMD_FLAG_NAME(IDLE),
264 CMD_FLAG_NAME(INTEGRITY),
265 CMD_FLAG_NAME(FUA),
266 CMD_FLAG_NAME(PREFLUSH),
267 CMD_FLAG_NAME(RAHEAD),
268 CMD_FLAG_NAME(BACKGROUND),
269 CMD_FLAG_NAME(NOUNMAP),
Bart Van Assche22d53822017-08-18 15:52:54 -0700270 CMD_FLAG_NAME(NOWAIT),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700271};
Omar Sandoval1a435112017-05-04 00:31:24 -0700272#undef CMD_FLAG_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700273
Omar Sandoval1a435112017-05-04 00:31:24 -0700274#define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700275static const char *const rqf_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700276 RQF_NAME(SORTED),
277 RQF_NAME(STARTED),
278 RQF_NAME(QUEUED),
279 RQF_NAME(SOFTBARRIER),
280 RQF_NAME(FLUSH_SEQ),
281 RQF_NAME(MIXED_MERGE),
282 RQF_NAME(MQ_INFLIGHT),
283 RQF_NAME(DONTPREP),
284 RQF_NAME(PREEMPT),
285 RQF_NAME(COPY_USER),
286 RQF_NAME(FAILED),
287 RQF_NAME(QUIET),
288 RQF_NAME(ELVPRIV),
289 RQF_NAME(IO_STAT),
290 RQF_NAME(ALLOCED),
291 RQF_NAME(PM),
292 RQF_NAME(HASHED),
293 RQF_NAME(STATS),
294 RQF_NAME(SPECIAL_PAYLOAD),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700295};
Omar Sandoval1a435112017-05-04 00:31:24 -0700296#undef RQF_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700297
Bart Van Asschec0cb1c62017-06-01 08:55:10 -0700298#define RQAF_NAME(name) [REQ_ATOM_##name] = #name
299static const char *const rqaf_name[] = {
300 RQAF_NAME(COMPLETE),
301 RQAF_NAME(STARTED),
302 RQAF_NAME(POLL_SLEPT),
303};
304#undef RQAF_NAME
305
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700306int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800307{
Bart Van Assche2836ee42017-04-26 13:47:56 -0700308 const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
Bart Van Assche8658dca2017-04-26 13:47:55 -0700309 const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800310
Bart Van Assche8658dca2017-04-26 13:47:55 -0700311 seq_printf(m, "%p {.op=", rq);
312 if (op < ARRAY_SIZE(op_name) && op_name[op])
313 seq_printf(m, "%s", op_name[op]);
314 else
315 seq_printf(m, "%d", op);
316 seq_puts(m, ", .cmd_flags=");
317 blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
318 ARRAY_SIZE(cmd_flag_name));
319 seq_puts(m, ", .rq_flags=");
320 blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
321 ARRAY_SIZE(rqf_name));
Bart Van Asschec0cb1c62017-06-01 08:55:10 -0700322 seq_puts(m, ", .atomic_flags=");
323 blk_flags_show(m, rq->atomic_flags, rqaf_name, ARRAY_SIZE(rqaf_name));
Bart Van Assche2836ee42017-04-26 13:47:56 -0700324 seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
Bart Van Assche8658dca2017-04-26 13:47:55 -0700325 rq->internal_tag);
Bart Van Assche2836ee42017-04-26 13:47:56 -0700326 if (mq_ops->show_rq)
327 mq_ops->show_rq(m, rq);
328 seq_puts(m, "}\n");
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800329 return 0;
330}
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700331EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
332
333int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
334{
335 return __blk_mq_debugfs_rq_show(m, list_entry_rq(v));
336}
Omar Sandoval16b738f2017-05-04 00:31:33 -0700337EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800338
Bart Van Assche8ef1a192017-06-01 08:55:11 -0700339static void *queue_requeue_list_start(struct seq_file *m, loff_t *pos)
340 __acquires(&q->requeue_lock)
341{
342 struct request_queue *q = m->private;
343
344 spin_lock_irq(&q->requeue_lock);
345 return seq_list_start(&q->requeue_list, *pos);
346}
347
348static void *queue_requeue_list_next(struct seq_file *m, void *v, loff_t *pos)
349{
350 struct request_queue *q = m->private;
351
352 return seq_list_next(v, &q->requeue_list, pos);
353}
354
355static void queue_requeue_list_stop(struct seq_file *m, void *v)
356 __releases(&q->requeue_lock)
357{
358 struct request_queue *q = m->private;
359
360 spin_unlock_irq(&q->requeue_lock);
361}
362
363static const struct seq_operations queue_requeue_list_seq_ops = {
364 .start = queue_requeue_list_start,
365 .next = queue_requeue_list_next,
366 .stop = queue_requeue_list_stop,
367 .show = blk_mq_debugfs_rq_show,
368};
369
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800370static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800371 __acquires(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800372{
373 struct blk_mq_hw_ctx *hctx = m->private;
374
375 spin_lock(&hctx->lock);
376 return seq_list_start(&hctx->dispatch, *pos);
377}
378
379static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
380{
381 struct blk_mq_hw_ctx *hctx = m->private;
382
383 return seq_list_next(v, &hctx->dispatch, pos);
384}
385
386static void hctx_dispatch_stop(struct seq_file *m, void *v)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800387 __releases(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800388{
389 struct blk_mq_hw_ctx *hctx = m->private;
390
391 spin_unlock(&hctx->lock);
392}
393
394static const struct seq_operations hctx_dispatch_seq_ops = {
395 .start = hctx_dispatch_start,
396 .next = hctx_dispatch_next,
397 .stop = hctx_dispatch_stop,
398 .show = blk_mq_debugfs_rq_show,
399};
400
Bart Van Assche2720bab2017-06-01 08:55:12 -0700401struct show_busy_params {
402 struct seq_file *m;
403 struct blk_mq_hw_ctx *hctx;
404};
405
406/*
407 * Note: the state of a request may change while this function is in progress,
408 * e.g. due to a concurrent blk_mq_finish_request() call.
409 */
410static void hctx_show_busy_rq(struct request *rq, void *data, bool reserved)
411{
412 const struct show_busy_params *params = data;
413
414 if (blk_mq_map_queue(rq->q, rq->mq_ctx->cpu) == params->hctx &&
415 test_bit(REQ_ATOM_STARTED, &rq->atomic_flags))
416 __blk_mq_debugfs_rq_show(params->m,
417 list_entry_rq(&rq->queuelist));
418}
419
420static int hctx_busy_show(void *data, struct seq_file *m)
421{
422 struct blk_mq_hw_ctx *hctx = data;
423 struct show_busy_params params = { .m = m, .hctx = hctx };
424
425 blk_mq_tagset_busy_iter(hctx->queue->tag_set, hctx_show_busy_rq,
426 &params);
427
428 return 0;
429}
430
Omar Sandovalf57de232017-05-04 00:31:28 -0700431static int hctx_ctx_map_show(void *data, struct seq_file *m)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800432{
Omar Sandovalf57de232017-05-04 00:31:28 -0700433 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval0bfa5282017-01-25 08:06:45 -0800434
435 sbitmap_bitmap_show(&hctx->ctx_map, m);
436 return 0;
437}
438
Omar Sandovald96b37c2017-01-25 08:06:46 -0800439static void blk_mq_debugfs_tags_show(struct seq_file *m,
440 struct blk_mq_tags *tags)
441{
442 seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
443 seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
444 seq_printf(m, "active_queues=%d\n",
445 atomic_read(&tags->active_queues));
446
447 seq_puts(m, "\nbitmap_tags:\n");
448 sbitmap_queue_show(&tags->bitmap_tags, m);
449
450 if (tags->nr_reserved_tags) {
451 seq_puts(m, "\nbreserved_tags:\n");
452 sbitmap_queue_show(&tags->breserved_tags, m);
453 }
454}
455
Omar Sandovalf57de232017-05-04 00:31:28 -0700456static int hctx_tags_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800457{
Omar Sandovalf57de232017-05-04 00:31:28 -0700458 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800459 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800460 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800461
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800462 res = mutex_lock_interruptible(&q->sysfs_lock);
463 if (res)
464 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800465 if (hctx->tags)
466 blk_mq_debugfs_tags_show(m, hctx->tags);
467 mutex_unlock(&q->sysfs_lock);
468
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800469out:
470 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800471}
472
Omar Sandovalf57de232017-05-04 00:31:28 -0700473static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800474{
Omar Sandovalf57de232017-05-04 00:31:28 -0700475 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald7e36212017-01-25 08:06:47 -0800476 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800477 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800478
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800479 res = mutex_lock_interruptible(&q->sysfs_lock);
480 if (res)
481 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800482 if (hctx->tags)
483 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
484 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800485
486out:
487 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800488}
489
Omar Sandovalf57de232017-05-04 00:31:28 -0700490static int hctx_sched_tags_show(void *data, struct seq_file *m)
Omar Sandovald7e36212017-01-25 08:06:47 -0800491{
Omar Sandovalf57de232017-05-04 00:31:28 -0700492 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800493 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800494 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800495
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800496 res = mutex_lock_interruptible(&q->sysfs_lock);
497 if (res)
498 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800499 if (hctx->sched_tags)
500 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
501 mutex_unlock(&q->sysfs_lock);
502
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800503out:
504 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800505}
506
Omar Sandovalf57de232017-05-04 00:31:28 -0700507static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800508{
Omar Sandovalf57de232017-05-04 00:31:28 -0700509 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald7e36212017-01-25 08:06:47 -0800510 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800511 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800512
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800513 res = mutex_lock_interruptible(&q->sysfs_lock);
514 if (res)
515 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800516 if (hctx->sched_tags)
517 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
518 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800519
520out:
521 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800522}
523
Omar Sandovalf57de232017-05-04 00:31:28 -0700524static int hctx_io_poll_show(void *data, struct seq_file *m)
Omar Sandovald7e36212017-01-25 08:06:47 -0800525{
Omar Sandovalf57de232017-05-04 00:31:28 -0700526 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800527
528 seq_printf(m, "considered=%lu\n", hctx->poll_considered);
529 seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
530 seq_printf(m, "success=%lu\n", hctx->poll_success);
531 return 0;
532}
533
Omar Sandovalf57de232017-05-04 00:31:28 -0700534static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
Omar Sandovalbe215472017-01-25 08:06:48 -0800535 size_t count, loff_t *ppos)
536{
Omar Sandovalf57de232017-05-04 00:31:28 -0700537 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800538
539 hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
540 return count;
541}
542
Omar Sandovalf57de232017-05-04 00:31:28 -0700543static int hctx_dispatched_show(void *data, struct seq_file *m)
Omar Sandovalbe215472017-01-25 08:06:48 -0800544{
Omar Sandovalf57de232017-05-04 00:31:28 -0700545 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800546 int i;
547
548 seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
549
550 for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
551 unsigned int d = 1U << (i - 1);
552
553 seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
554 }
555
556 seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
557 return 0;
558}
559
Omar Sandovalf57de232017-05-04 00:31:28 -0700560static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
Omar Sandovalbe215472017-01-25 08:06:48 -0800561 size_t count, loff_t *ppos)
562{
Omar Sandovalf57de232017-05-04 00:31:28 -0700563 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovalbe215472017-01-25 08:06:48 -0800564 int i;
565
566 for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
567 hctx->dispatched[i] = 0;
568 return count;
569}
570
Omar Sandovalf57de232017-05-04 00:31:28 -0700571static int hctx_queued_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800572{
Omar Sandovalf57de232017-05-04 00:31:28 -0700573 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800574
575 seq_printf(m, "%lu\n", hctx->queued);
576 return 0;
577}
578
Omar Sandovalf57de232017-05-04 00:31:28 -0700579static ssize_t hctx_queued_write(void *data, const char __user *buf,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800580 size_t count, loff_t *ppos)
581{
Omar Sandovalf57de232017-05-04 00:31:28 -0700582 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800583
584 hctx->queued = 0;
585 return count;
586}
587
Omar Sandovalf57de232017-05-04 00:31:28 -0700588static int hctx_run_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800589{
Omar Sandovalf57de232017-05-04 00:31:28 -0700590 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800591
592 seq_printf(m, "%lu\n", hctx->run);
593 return 0;
594}
595
Omar Sandovalf57de232017-05-04 00:31:28 -0700596static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
597 loff_t *ppos)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800598{
Omar Sandovalf57de232017-05-04 00:31:28 -0700599 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800600
601 hctx->run = 0;
602 return count;
603}
604
Omar Sandovalf57de232017-05-04 00:31:28 -0700605static int hctx_active_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800606{
Omar Sandovalf57de232017-05-04 00:31:28 -0700607 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800608
609 seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
610 return 0;
611}
612
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800613static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800614 __acquires(&ctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800615{
616 struct blk_mq_ctx *ctx = m->private;
617
618 spin_lock(&ctx->lock);
619 return seq_list_start(&ctx->rq_list, *pos);
620}
621
622static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
623{
624 struct blk_mq_ctx *ctx = m->private;
625
626 return seq_list_next(v, &ctx->rq_list, pos);
627}
628
629static void ctx_rq_list_stop(struct seq_file *m, void *v)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800630 __releases(&ctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800631{
632 struct blk_mq_ctx *ctx = m->private;
633
634 spin_unlock(&ctx->lock);
635}
636
637static const struct seq_operations ctx_rq_list_seq_ops = {
638 .start = ctx_rq_list_start,
639 .next = ctx_rq_list_next,
640 .stop = ctx_rq_list_stop,
641 .show = blk_mq_debugfs_rq_show,
642};
Omar Sandovalf57de232017-05-04 00:31:28 -0700643static int ctx_dispatched_show(void *data, struct seq_file *m)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800644{
Omar Sandovalf57de232017-05-04 00:31:28 -0700645 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800646
647 seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
648 return 0;
649}
650
Omar Sandovalf57de232017-05-04 00:31:28 -0700651static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800652 size_t count, loff_t *ppos)
653{
Omar Sandovalf57de232017-05-04 00:31:28 -0700654 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800655
656 ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
657 return count;
658}
659
Omar Sandovalf57de232017-05-04 00:31:28 -0700660static int ctx_merged_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800661{
Omar Sandovalf57de232017-05-04 00:31:28 -0700662 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800663
664 seq_printf(m, "%lu\n", ctx->rq_merged);
665 return 0;
666}
667
Omar Sandovalf57de232017-05-04 00:31:28 -0700668static ssize_t ctx_merged_write(void *data, const char __user *buf,
669 size_t count, loff_t *ppos)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800670{
Omar Sandovalf57de232017-05-04 00:31:28 -0700671 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800672
673 ctx->rq_merged = 0;
674 return count;
675}
676
Omar Sandovalf57de232017-05-04 00:31:28 -0700677static int ctx_completed_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800678{
Omar Sandovalf57de232017-05-04 00:31:28 -0700679 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800680
681 seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
682 return 0;
683}
684
Omar Sandovalf57de232017-05-04 00:31:28 -0700685static ssize_t ctx_completed_write(void *data, const char __user *buf,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800686 size_t count, loff_t *ppos)
687{
Omar Sandovalf57de232017-05-04 00:31:28 -0700688 struct blk_mq_ctx *ctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800689
690 ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
691 return count;
692}
693
Omar Sandovalf57de232017-05-04 00:31:28 -0700694static int blk_mq_debugfs_show(struct seq_file *m, void *v)
695{
696 const struct blk_mq_debugfs_attr *attr = m->private;
697 void *data = d_inode(m->file->f_path.dentry->d_parent)->i_private;
698
699 return attr->show(data, m);
700}
701
702static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
703 size_t count, loff_t *ppos)
704{
705 struct seq_file *m = file->private_data;
706 const struct blk_mq_debugfs_attr *attr = m->private;
707 void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
708
709 if (!attr->write)
710 return -EPERM;
711
712 return attr->write(data, buf, count, ppos);
713}
714
715static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
716{
717 const struct blk_mq_debugfs_attr *attr = inode->i_private;
718 void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
719 struct seq_file *m;
720 int ret;
721
722 if (attr->seq_ops) {
723 ret = seq_open(file, attr->seq_ops);
724 if (!ret) {
725 m = file->private_data;
726 m->private = data;
727 }
728 return ret;
729 }
730
731 if (WARN_ON_ONCE(!attr->show))
732 return -EPERM;
733
734 return single_open(file, blk_mq_debugfs_show, inode->i_private);
735}
736
737static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
738{
739 const struct blk_mq_debugfs_attr *attr = inode->i_private;
740
741 if (attr->show)
742 return single_release(inode, file);
743 else
744 return seq_release(inode, file);
745}
746
747const struct file_operations blk_mq_debugfs_fops = {
748 .open = blk_mq_debugfs_open,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800749 .read = seq_read,
Omar Sandovalf57de232017-05-04 00:31:28 -0700750 .write = blk_mq_debugfs_write,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800751 .llseek = seq_lseek,
Omar Sandovalf57de232017-05-04 00:31:28 -0700752 .release = blk_mq_debugfs_release,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800753};
754
Omar Sandoval34dbad52017-03-21 08:56:08 -0700755static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700756 {"poll_stat", 0400, queue_poll_stat_show},
Bart Van Assche8ef1a192017-06-01 08:55:11 -0700757 {"requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops},
Omar Sandovalf57de232017-05-04 00:31:28 -0700758 {"state", 0600, queue_state_show, queue_state_write},
Jens Axboef793dfd2017-06-26 08:15:27 -0600759 {"write_hints", 0600, queue_write_hint_show, queue_write_hint_store},
Omar Sandoval34dbad52017-03-21 08:56:08 -0700760 {},
761};
762
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800763static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700764 {"state", 0400, hctx_state_show},
765 {"flags", 0400, hctx_flags_show},
766 {"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
Bart Van Assche2720bab2017-06-01 08:55:12 -0700767 {"busy", 0400, hctx_busy_show},
Omar Sandovalf57de232017-05-04 00:31:28 -0700768 {"ctx_map", 0400, hctx_ctx_map_show},
769 {"tags", 0400, hctx_tags_show},
770 {"tags_bitmap", 0400, hctx_tags_bitmap_show},
771 {"sched_tags", 0400, hctx_sched_tags_show},
772 {"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
773 {"io_poll", 0600, hctx_io_poll_show, hctx_io_poll_write},
774 {"dispatched", 0600, hctx_dispatched_show, hctx_dispatched_write},
775 {"queued", 0600, hctx_queued_show, hctx_queued_write},
776 {"run", 0600, hctx_run_show, hctx_run_write},
777 {"active", 0400, hctx_active_show},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800778 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800779};
780
781static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700782 {"rq_list", 0400, .seq_ops = &ctx_rq_list_seq_ops},
783 {"dispatched", 0600, ctx_dispatched_show, ctx_dispatched_write},
784 {"merged", 0600, ctx_merged_show, ctx_merged_write},
785 {"completed", 0600, ctx_completed_show, ctx_completed_write},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800786 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800787};
788
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800789static bool debugfs_create_files(struct dentry *parent, void *data,
Omar Sandovalf57de232017-05-04 00:31:28 -0700790 const struct blk_mq_debugfs_attr *attr)
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800791{
Omar Sandovalf57de232017-05-04 00:31:28 -0700792 d_inode(parent)->i_private = data;
793
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800794 for (; attr->name; attr++) {
795 if (!debugfs_create_file(attr->name, attr->mode, parent,
Omar Sandovalf57de232017-05-04 00:31:28 -0700796 (void *)attr, &blk_mq_debugfs_fops))
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800797 return false;
798 }
799 return true;
800}
801
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600802int blk_mq_debugfs_register(struct request_queue *q)
803{
804 struct blk_mq_hw_ctx *hctx;
805 int i;
806
807 if (!blk_debugfs_root)
808 return -ENOENT;
809
810 q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
811 blk_debugfs_root);
812 if (!q->debugfs_dir)
813 return -ENOMEM;
814
815 if (!debugfs_create_files(q->debugfs_dir, q,
816 blk_mq_debugfs_queue_attrs))
817 goto err;
818
819 /*
820 * blk_mq_init_hctx() attempted to do this already, but q->debugfs_dir
821 * didn't exist yet (because we don't know what to name the directory
822 * until the queue is registered to a gendisk).
823 */
824 queue_for_each_hw_ctx(q, hctx, i) {
825 if (!hctx->debugfs_dir && blk_mq_debugfs_register_hctx(q, hctx))
826 goto err;
Omar Sandovald332ce02017-05-04 08:24:40 -0600827 if (q->elevator && !hctx->sched_debugfs_dir &&
828 blk_mq_debugfs_register_sched_hctx(q, hctx))
829 goto err;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600830 }
831
832 return 0;
833
834err:
835 blk_mq_debugfs_unregister(q);
836 return -ENOMEM;
837}
838
839void blk_mq_debugfs_unregister(struct request_queue *q)
840{
841 debugfs_remove_recursive(q->debugfs_dir);
Omar Sandovald332ce02017-05-04 08:24:40 -0600842 q->sched_debugfs_dir = NULL;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600843 q->debugfs_dir = NULL;
844}
845
846static int blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,
847 struct blk_mq_ctx *ctx)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800848{
849 struct dentry *ctx_dir;
850 char name[20];
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800851
852 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600853 ctx_dir = debugfs_create_dir(name, hctx->debugfs_dir);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800854 if (!ctx_dir)
855 return -ENOMEM;
856
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800857 if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
858 return -ENOMEM;
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800859
860 return 0;
861}
862
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600863int blk_mq_debugfs_register_hctx(struct request_queue *q,
864 struct blk_mq_hw_ctx *hctx)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800865{
866 struct blk_mq_ctx *ctx;
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800867 char name[20];
868 int i;
869
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800870 if (!q->debugfs_dir)
871 return -ENOENT;
872
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600873 snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
874 hctx->debugfs_dir = debugfs_create_dir(name, q->debugfs_dir);
875 if (!hctx->debugfs_dir)
876 return -ENOMEM;
877
878 if (!debugfs_create_files(hctx->debugfs_dir, hctx,
879 blk_mq_debugfs_hctx_attrs))
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800880 goto err;
881
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600882 hctx_for_each_ctx(hctx, ctx, i) {
883 if (blk_mq_debugfs_register_ctx(hctx, ctx))
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800884 goto err;
885 }
886
887 return 0;
888
889err:
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600890 blk_mq_debugfs_unregister_hctx(hctx);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800891 return -ENOMEM;
892}
893
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600894void blk_mq_debugfs_unregister_hctx(struct blk_mq_hw_ctx *hctx)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800895{
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600896 debugfs_remove_recursive(hctx->debugfs_dir);
Omar Sandovald332ce02017-05-04 08:24:40 -0600897 hctx->sched_debugfs_dir = NULL;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600898 hctx->debugfs_dir = NULL;
899}
900
901int blk_mq_debugfs_register_hctxs(struct request_queue *q)
902{
903 struct blk_mq_hw_ctx *hctx;
904 int i;
905
906 queue_for_each_hw_ctx(q, hctx, i) {
907 if (blk_mq_debugfs_register_hctx(q, hctx))
908 return -ENOMEM;
909 }
910
911 return 0;
912}
913
914void blk_mq_debugfs_unregister_hctxs(struct request_queue *q)
915{
916 struct blk_mq_hw_ctx *hctx;
917 int i;
918
919 queue_for_each_hw_ctx(q, hctx, i)
920 blk_mq_debugfs_unregister_hctx(hctx);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800921}
Omar Sandovald332ce02017-05-04 08:24:40 -0600922
923int blk_mq_debugfs_register_sched(struct request_queue *q)
924{
925 struct elevator_type *e = q->elevator->type;
926
927 if (!q->debugfs_dir)
928 return -ENOENT;
929
930 if (!e->queue_debugfs_attrs)
931 return 0;
932
933 q->sched_debugfs_dir = debugfs_create_dir("sched", q->debugfs_dir);
934 if (!q->sched_debugfs_dir)
935 return -ENOMEM;
936
937 if (!debugfs_create_files(q->sched_debugfs_dir, q,
938 e->queue_debugfs_attrs))
939 goto err;
940
941 return 0;
942
943err:
944 blk_mq_debugfs_unregister_sched(q);
945 return -ENOMEM;
946}
947
948void blk_mq_debugfs_unregister_sched(struct request_queue *q)
949{
950 debugfs_remove_recursive(q->sched_debugfs_dir);
951 q->sched_debugfs_dir = NULL;
952}
953
954int blk_mq_debugfs_register_sched_hctx(struct request_queue *q,
955 struct blk_mq_hw_ctx *hctx)
956{
957 struct elevator_type *e = q->elevator->type;
958
959 if (!hctx->debugfs_dir)
960 return -ENOENT;
961
962 if (!e->hctx_debugfs_attrs)
963 return 0;
964
965 hctx->sched_debugfs_dir = debugfs_create_dir("sched",
966 hctx->debugfs_dir);
967 if (!hctx->sched_debugfs_dir)
968 return -ENOMEM;
969
970 if (!debugfs_create_files(hctx->sched_debugfs_dir, hctx,
971 e->hctx_debugfs_attrs))
972 return -ENOMEM;
973
974 return 0;
975}
976
977void blk_mq_debugfs_unregister_sched_hctx(struct blk_mq_hw_ctx *hctx)
978{
979 debugfs_remove_recursive(hctx->sched_debugfs_dir);
980 hctx->sched_debugfs_dir = NULL;
981}