xref: /linux/block/blk-mq-debugfs.c (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2017 Facebook
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/blkdev.h>
8 #include <linux/build_bug.h>
9 #include <linux/debugfs.h>
10 
11 #include "blk.h"
12 #include "blk-mq.h"
13 #include "blk-mq-debugfs.h"
14 #include "blk-mq-sched.h"
15 #include "blk-rq-qos.h"
16 
17 static int queue_poll_stat_show(void *data, struct seq_file *m)
18 {
19 	return 0;
20 }
21 
22 static void *queue_requeue_list_start(struct seq_file *m, loff_t *pos)
23 	__acquires(&q->requeue_lock)
24 {
25 	struct request_queue *q = m->private;
26 
27 	spin_lock_irq(&q->requeue_lock);
28 	return seq_list_start(&q->requeue_list, *pos);
29 }
30 
31 static void *queue_requeue_list_next(struct seq_file *m, void *v, loff_t *pos)
32 {
33 	struct request_queue *q = m->private;
34 
35 	return seq_list_next(v, &q->requeue_list, pos);
36 }
37 
38 static void queue_requeue_list_stop(struct seq_file *m, void *v)
39 	__releases(&q->requeue_lock)
40 {
41 	struct request_queue *q = m->private;
42 
43 	spin_unlock_irq(&q->requeue_lock);
44 }
45 
46 static const struct seq_operations queue_requeue_list_seq_ops = {
47 	.start	= queue_requeue_list_start,
48 	.next	= queue_requeue_list_next,
49 	.stop	= queue_requeue_list_stop,
50 	.show	= blk_mq_debugfs_rq_show,
51 };
52 
53 static int blk_flags_show(struct seq_file *m, const unsigned long flags,
54 			  const char *const *flag_name, int flag_name_count)
55 {
56 	bool sep = false;
57 	int i;
58 
59 	for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
60 		if (!(flags & BIT(i)))
61 			continue;
62 		if (sep)
63 			seq_puts(m, "|");
64 		sep = true;
65 		if (i < flag_name_count && flag_name[i])
66 			seq_puts(m, flag_name[i]);
67 		else
68 			seq_printf(m, "%d", i);
69 	}
70 	return 0;
71 }
72 
73 static int queue_pm_only_show(void *data, struct seq_file *m)
74 {
75 	struct request_queue *q = data;
76 
77 	seq_printf(m, "%d\n", atomic_read(&q->pm_only));
78 	return 0;
79 }
80 
81 #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
82 static const char *const blk_queue_flag_name[] = {
83 	QUEUE_FLAG_NAME(DYING),
84 	QUEUE_FLAG_NAME(NOMERGES),
85 	QUEUE_FLAG_NAME(SAME_COMP),
86 	QUEUE_FLAG_NAME(FAIL_IO),
87 	QUEUE_FLAG_NAME(NOXMERGES),
88 	QUEUE_FLAG_NAME(SAME_FORCE),
89 	QUEUE_FLAG_NAME(INIT_DONE),
90 	QUEUE_FLAG_NAME(STATS),
91 	QUEUE_FLAG_NAME(REGISTERED),
92 	QUEUE_FLAG_NAME(QUIESCED),
93 	QUEUE_FLAG_NAME(RQ_ALLOC_TIME),
94 	QUEUE_FLAG_NAME(HCTX_ACTIVE),
95 	QUEUE_FLAG_NAME(SQ_SCHED),
96 	QUEUE_FLAG_NAME(DISABLE_WBT_DEF),
97 	QUEUE_FLAG_NAME(NO_ELV_SWITCH),
98 	QUEUE_FLAG_NAME(QOS_ENABLED),
99 	QUEUE_FLAG_NAME(BIO_ISSUE_TIME),
100 	QUEUE_FLAG_NAME(ZONED_QD1_WRITES),
101 };
102 #undef QUEUE_FLAG_NAME
103 
104 static int queue_state_show(void *data, struct seq_file *m)
105 {
106 	struct request_queue *q = data;
107 
108 	BUILD_BUG_ON(ARRAY_SIZE(blk_queue_flag_name) != QUEUE_FLAG_MAX);
109 	blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
110 		       ARRAY_SIZE(blk_queue_flag_name));
111 	seq_puts(m, "\n");
112 	return 0;
113 }
114 
115 static ssize_t queue_state_write(void *data, const char __user *buf,
116 				 size_t count, loff_t *ppos)
117 {
118 	struct request_queue *q = data;
119 	char opbuf[16] = { }, *op;
120 
121 	/*
122 	 * The "state" attribute is removed when the queue is removed.  Don't
123 	 * allow setting the state on a dying queue to avoid a use-after-free.
124 	 */
125 	if (blk_queue_dying(q))
126 		return -ENOENT;
127 
128 	if (count >= sizeof(opbuf)) {
129 		pr_err("%s: operation too long\n", __func__);
130 		goto inval;
131 	}
132 
133 	if (copy_from_user(opbuf, buf, count))
134 		return -EFAULT;
135 	op = strstrip(opbuf);
136 	if (strcmp(op, "run") == 0) {
137 		blk_mq_run_hw_queues(q, true);
138 	} else if (strcmp(op, "start") == 0) {
139 		blk_mq_start_stopped_hw_queues(q, true);
140 	} else if (strcmp(op, "kick") == 0) {
141 		blk_mq_kick_requeue_list(q);
142 	} else {
143 		pr_err("%s: unsupported operation '%s'\n", __func__, op);
144 inval:
145 		pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
146 		return -EINVAL;
147 	}
148 	return count;
149 }
150 
151 static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
152 	{ "poll_stat", 0400, queue_poll_stat_show },
153 	{ "requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops },
154 	{ "pm_only", 0600, queue_pm_only_show, NULL },
155 	{ "state", 0600, queue_state_show, queue_state_write },
156 	{ "zone_wplugs", 0400, queue_zone_wplugs_show, NULL },
157 	{ },
158 };
159 
160 #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
161 static const char *const hctx_state_name[] = {
162 	HCTX_STATE_NAME(STOPPED),
163 	HCTX_STATE_NAME(TAG_ACTIVE),
164 	HCTX_STATE_NAME(SCHED_RESTART),
165 	HCTX_STATE_NAME(INACTIVE),
166 };
167 #undef HCTX_STATE_NAME
168 
169 static int hctx_state_show(void *data, struct seq_file *m)
170 {
171 	struct blk_mq_hw_ctx *hctx = data;
172 
173 	BUILD_BUG_ON(ARRAY_SIZE(hctx_state_name) != BLK_MQ_S_MAX);
174 	blk_flags_show(m, hctx->state, hctx_state_name,
175 		       ARRAY_SIZE(hctx_state_name));
176 	seq_puts(m, "\n");
177 	return 0;
178 }
179 
180 #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
181 static const char *const hctx_flag_name[] = {
182 	HCTX_FLAG_NAME(TAG_QUEUE_SHARED),
183 	HCTX_FLAG_NAME(STACKING),
184 	HCTX_FLAG_NAME(TAG_HCTX_SHARED),
185 	HCTX_FLAG_NAME(BLOCKING),
186 	HCTX_FLAG_NAME(TAG_RR),
187 	HCTX_FLAG_NAME(NO_SCHED_BY_DEFAULT),
188 };
189 #undef HCTX_FLAG_NAME
190 
191 static int hctx_flags_show(void *data, struct seq_file *m)
192 {
193 	struct blk_mq_hw_ctx *hctx = data;
194 
195 	BUILD_BUG_ON(ARRAY_SIZE(hctx_flag_name) != ilog2(BLK_MQ_F_MAX));
196 
197 	blk_flags_show(m, hctx->flags, hctx_flag_name,
198 			ARRAY_SIZE(hctx_flag_name));
199 	seq_puts(m, "\n");
200 	return 0;
201 }
202 
203 #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
204 static const char *const cmd_flag_name[] = {
205 	CMD_FLAG_NAME(FAILFAST_DEV),
206 	CMD_FLAG_NAME(FAILFAST_TRANSPORT),
207 	CMD_FLAG_NAME(FAILFAST_DRIVER),
208 	CMD_FLAG_NAME(SYNC),
209 	CMD_FLAG_NAME(META),
210 	CMD_FLAG_NAME(PRIO),
211 	CMD_FLAG_NAME(NOMERGE),
212 	CMD_FLAG_NAME(IDLE),
213 	CMD_FLAG_NAME(INTEGRITY),
214 	CMD_FLAG_NAME(FUA),
215 	CMD_FLAG_NAME(PREFLUSH),
216 	CMD_FLAG_NAME(RAHEAD),
217 	CMD_FLAG_NAME(BACKGROUND),
218 	CMD_FLAG_NAME(NOWAIT),
219 	CMD_FLAG_NAME(POLLED),
220 	CMD_FLAG_NAME(ALLOC_CACHE),
221 	CMD_FLAG_NAME(SWAP),
222 	CMD_FLAG_NAME(DRV),
223 	CMD_FLAG_NAME(FS_PRIVATE),
224 	CMD_FLAG_NAME(ATOMIC),
225 	CMD_FLAG_NAME(NOUNMAP),
226 };
227 #undef CMD_FLAG_NAME
228 
229 #define RQF_NAME(name) [__RQF_##name] = #name
230 static const char *const rqf_name[] = {
231 	RQF_NAME(STARTED),
232 	RQF_NAME(FLUSH_SEQ),
233 	RQF_NAME(MIXED_MERGE),
234 	RQF_NAME(DONTPREP),
235 	RQF_NAME(SCHED_TAGS),
236 	RQF_NAME(USE_SCHED),
237 	RQF_NAME(FAILED),
238 	RQF_NAME(QUIET),
239 	RQF_NAME(IO_STAT),
240 	RQF_NAME(PM),
241 	RQF_NAME(HASHED),
242 	RQF_NAME(STATS),
243 	RQF_NAME(SPECIAL_PAYLOAD),
244 	RQF_NAME(ZONE_WRITE_PLUGGING),
245 	RQF_NAME(TIMED_OUT),
246 	RQF_NAME(RESV),
247 };
248 #undef RQF_NAME
249 
250 static const char *const blk_mq_rq_state_name_array[] = {
251 	[MQ_RQ_IDLE]		= "idle",
252 	[MQ_RQ_IN_FLIGHT]	= "in_flight",
253 	[MQ_RQ_COMPLETE]	= "complete",
254 };
255 
256 static const char *blk_mq_rq_state_name(enum mq_rq_state rq_state)
257 {
258 	if (WARN_ON_ONCE((unsigned int)rq_state >=
259 			 ARRAY_SIZE(blk_mq_rq_state_name_array)))
260 		return "(?)";
261 	return blk_mq_rq_state_name_array[rq_state];
262 }
263 
264 int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
265 {
266 	const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
267 	const enum req_op op = req_op(rq);
268 	const char *op_str = blk_op_str(op);
269 
270 	BUILD_BUG_ON(ARRAY_SIZE(cmd_flag_name) != __REQ_NR_BITS);
271 	BUILD_BUG_ON(ARRAY_SIZE(rqf_name) != __RQF_BITS);
272 
273 	seq_printf(m, "%p {.op=", rq);
274 	if (strcmp(op_str, "UNKNOWN") == 0)
275 		seq_printf(m, "%u", op);
276 	else
277 		seq_printf(m, "%s", op_str);
278 	seq_puts(m, ", .cmd_flags=");
279 	blk_flags_show(m, (__force unsigned int)(rq->cmd_flags & ~REQ_OP_MASK),
280 		       cmd_flag_name, ARRAY_SIZE(cmd_flag_name));
281 	seq_puts(m, ", .rq_flags=");
282 	blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
283 		       ARRAY_SIZE(rqf_name));
284 	seq_printf(m, ", .state=%s", blk_mq_rq_state_name(blk_mq_rq_state(rq)));
285 	seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
286 		   rq->internal_tag);
287 	if (mq_ops->show_rq)
288 		mq_ops->show_rq(m, rq);
289 	seq_puts(m, "}\n");
290 	return 0;
291 }
292 EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
293 
294 int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
295 {
296 	return __blk_mq_debugfs_rq_show(m, list_entry_rq(v));
297 }
298 EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
299 
300 static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
301 	__acquires(&hctx->lock)
302 {
303 	struct blk_mq_hw_ctx *hctx = m->private;
304 
305 	spin_lock(&hctx->lock);
306 	return seq_list_start(&hctx->dispatch, *pos);
307 }
308 
309 static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
310 {
311 	struct blk_mq_hw_ctx *hctx = m->private;
312 
313 	return seq_list_next(v, &hctx->dispatch, pos);
314 }
315 
316 static void hctx_dispatch_stop(struct seq_file *m, void *v)
317 	__releases(&hctx->lock)
318 {
319 	struct blk_mq_hw_ctx *hctx = m->private;
320 
321 	spin_unlock(&hctx->lock);
322 }
323 
324 static const struct seq_operations hctx_dispatch_seq_ops = {
325 	.start	= hctx_dispatch_start,
326 	.next	= hctx_dispatch_next,
327 	.stop	= hctx_dispatch_stop,
328 	.show	= blk_mq_debugfs_rq_show,
329 };
330 
331 struct show_busy_params {
332 	struct seq_file		*m;
333 	struct blk_mq_hw_ctx	*hctx;
334 };
335 
336 /*
337  * Note: the state of a request may change while this function is in progress,
338  * e.g. due to a concurrent blk_mq_finish_request() call. Returns true to
339  * keep iterating requests.
340  */
341 static bool hctx_show_busy_rq(struct request *rq, void *data)
342 {
343 	const struct show_busy_params *params = data;
344 
345 	if (rq->mq_hctx == params->hctx)
346 		__blk_mq_debugfs_rq_show(params->m, rq);
347 
348 	return true;
349 }
350 
351 static int hctx_busy_show(void *data, struct seq_file *m)
352 {
353 	struct blk_mq_hw_ctx *hctx = data;
354 	struct show_busy_params params = { .m = m, .hctx = hctx };
355 	int res;
356 
357 	res = mutex_lock_interruptible(&hctx->queue->elevator_lock);
358 	if (res)
359 		return res;
360 	blk_mq_tagset_busy_iter(hctx->queue->tag_set, hctx_show_busy_rq,
361 				&params);
362 	mutex_unlock(&hctx->queue->elevator_lock);
363 
364 	return 0;
365 }
366 
367 static const char *const hctx_types[] = {
368 	[HCTX_TYPE_DEFAULT]	= "default",
369 	[HCTX_TYPE_READ]	= "read",
370 	[HCTX_TYPE_POLL]	= "poll",
371 };
372 
373 static int hctx_type_show(void *data, struct seq_file *m)
374 {
375 	struct blk_mq_hw_ctx *hctx = data;
376 
377 	BUILD_BUG_ON(ARRAY_SIZE(hctx_types) != HCTX_MAX_TYPES);
378 	seq_printf(m, "%s\n", hctx_types[hctx->type]);
379 	return 0;
380 }
381 
382 static int hctx_ctx_map_show(void *data, struct seq_file *m)
383 {
384 	struct blk_mq_hw_ctx *hctx = data;
385 
386 	sbitmap_bitmap_show(&hctx->ctx_map, m);
387 	return 0;
388 }
389 
390 static void blk_mq_debugfs_tags_show(struct seq_file *m,
391 				     struct blk_mq_tags *tags)
392 {
393 	seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
394 	seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
395 	seq_printf(m, "active_queues=%d\n",
396 		   READ_ONCE(tags->active_queues));
397 
398 	seq_puts(m, "\nbitmap_tags:\n");
399 	sbitmap_queue_show(&tags->bitmap_tags, m);
400 
401 	if (tags->nr_reserved_tags) {
402 		seq_puts(m, "\nbreserved_tags:\n");
403 		sbitmap_queue_show(&tags->breserved_tags, m);
404 	}
405 }
406 
407 static int hctx_tags_show(void *data, struct seq_file *m)
408 {
409 	struct blk_mq_hw_ctx *hctx = data;
410 	struct request_queue *q = hctx->queue;
411 	int res;
412 
413 	res = mutex_lock_interruptible(&q->elevator_lock);
414 	if (res)
415 		return res;
416 	if (hctx->tags)
417 		blk_mq_debugfs_tags_show(m, hctx->tags);
418 	mutex_unlock(&q->elevator_lock);
419 
420 	return 0;
421 }
422 
423 static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
424 {
425 	struct blk_mq_hw_ctx *hctx = data;
426 	struct request_queue *q = hctx->queue;
427 	int res;
428 
429 	res = mutex_lock_interruptible(&q->elevator_lock);
430 	if (res)
431 		return res;
432 	if (hctx->tags)
433 		sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
434 	mutex_unlock(&q->elevator_lock);
435 
436 	return 0;
437 }
438 
439 static int hctx_sched_tags_show(void *data, struct seq_file *m)
440 {
441 	struct blk_mq_hw_ctx *hctx = data;
442 	struct request_queue *q = hctx->queue;
443 	int res;
444 
445 	res = mutex_lock_interruptible(&q->elevator_lock);
446 	if (res)
447 		return res;
448 	if (hctx->sched_tags)
449 		blk_mq_debugfs_tags_show(m, hctx->sched_tags);
450 	mutex_unlock(&q->elevator_lock);
451 
452 	return 0;
453 }
454 
455 static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
456 {
457 	struct blk_mq_hw_ctx *hctx = data;
458 	struct request_queue *q = hctx->queue;
459 	int res;
460 
461 	res = mutex_lock_interruptible(&q->elevator_lock);
462 	if (res)
463 		return res;
464 	if (hctx->sched_tags)
465 		sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
466 	mutex_unlock(&q->elevator_lock);
467 
468 	return 0;
469 }
470 
471 static int hctx_active_show(void *data, struct seq_file *m)
472 {
473 	struct blk_mq_hw_ctx *hctx = data;
474 
475 	seq_printf(m, "%d\n", __blk_mq_active_requests(hctx));
476 	return 0;
477 }
478 
479 static int hctx_dispatch_busy_show(void *data, struct seq_file *m)
480 {
481 	struct blk_mq_hw_ctx *hctx = data;
482 
483 	seq_printf(m, "%u\n", hctx->dispatch_busy);
484 	return 0;
485 }
486 
487 #define CTX_RQ_SEQ_OPS(name, type)					\
488 static void *ctx_##name##_rq_list_start(struct seq_file *m, loff_t *pos) \
489 	__acquires(&ctx->lock)						\
490 {									\
491 	struct blk_mq_ctx *ctx = m->private;				\
492 									\
493 	spin_lock(&ctx->lock);						\
494 	return seq_list_start(&ctx->rq_lists[type], *pos);		\
495 }									\
496 									\
497 static void *ctx_##name##_rq_list_next(struct seq_file *m, void *v,	\
498 				     loff_t *pos)			\
499 {									\
500 	struct blk_mq_ctx *ctx = m->private;				\
501 									\
502 	return seq_list_next(v, &ctx->rq_lists[type], pos);		\
503 }									\
504 									\
505 static void ctx_##name##_rq_list_stop(struct seq_file *m, void *v)	\
506 	__releases(&ctx->lock)						\
507 {									\
508 	struct blk_mq_ctx *ctx = m->private;				\
509 									\
510 	spin_unlock(&ctx->lock);					\
511 }									\
512 									\
513 static const struct seq_operations ctx_##name##_rq_list_seq_ops = {	\
514 	.start	= ctx_##name##_rq_list_start,				\
515 	.next	= ctx_##name##_rq_list_next,				\
516 	.stop	= ctx_##name##_rq_list_stop,				\
517 	.show	= blk_mq_debugfs_rq_show,				\
518 }
519 
520 CTX_RQ_SEQ_OPS(default, HCTX_TYPE_DEFAULT);
521 CTX_RQ_SEQ_OPS(read, HCTX_TYPE_READ);
522 CTX_RQ_SEQ_OPS(poll, HCTX_TYPE_POLL);
523 
524 static int blk_mq_debugfs_show(struct seq_file *m, void *v)
525 {
526 	const struct blk_mq_debugfs_attr *attr = m->private;
527 	void *data = debugfs_get_aux(m->file);
528 
529 	return attr->show(data, m);
530 }
531 
532 static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
533 				    size_t count, loff_t *ppos)
534 {
535 	struct seq_file *m = file->private_data;
536 	const struct blk_mq_debugfs_attr *attr = m->private;
537 	void *data = debugfs_get_aux(file);
538 
539 	/*
540 	 * Attributes that only implement .seq_ops are read-only and 'attr' is
541 	 * the same with 'data' in this case.
542 	 */
543 	if (attr == data || !attr->write)
544 		return -EPERM;
545 
546 	return attr->write(data, buf, count, ppos);
547 }
548 
549 static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
550 {
551 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
552 	void *data = debugfs_get_aux(file);
553 	struct seq_file *m;
554 	int ret;
555 
556 	if (attr->seq_ops) {
557 		ret = seq_open(file, attr->seq_ops);
558 		if (!ret) {
559 			m = file->private_data;
560 			m->private = data;
561 		}
562 		return ret;
563 	}
564 
565 	if (WARN_ON_ONCE(!attr->show))
566 		return -EPERM;
567 
568 	return single_open(file, blk_mq_debugfs_show, inode->i_private);
569 }
570 
571 static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
572 {
573 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
574 
575 	if (attr->show)
576 		return single_release(inode, file);
577 
578 	return seq_release(inode, file);
579 }
580 
581 static const struct file_operations blk_mq_debugfs_fops = {
582 	.open		= blk_mq_debugfs_open,
583 	.read		= seq_read,
584 	.write		= blk_mq_debugfs_write,
585 	.llseek		= seq_lseek,
586 	.release	= blk_mq_debugfs_release,
587 };
588 
589 static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
590 	{"state", 0400, hctx_state_show},
591 	{"flags", 0400, hctx_flags_show},
592 	{"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
593 	{"busy", 0400, hctx_busy_show},
594 	{"ctx_map", 0400, hctx_ctx_map_show},
595 	{"tags", 0400, hctx_tags_show},
596 	{"tags_bitmap", 0400, hctx_tags_bitmap_show},
597 	{"sched_tags", 0400, hctx_sched_tags_show},
598 	{"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
599 	{"active", 0400, hctx_active_show},
600 	{"dispatch_busy", 0400, hctx_dispatch_busy_show},
601 	{"type", 0400, hctx_type_show},
602 	{},
603 };
604 
605 static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
606 	{"default_rq_list", 0400, .seq_ops = &ctx_default_rq_list_seq_ops},
607 	{"read_rq_list", 0400, .seq_ops = &ctx_read_rq_list_seq_ops},
608 	{"poll_rq_list", 0400, .seq_ops = &ctx_poll_rq_list_seq_ops},
609 	{},
610 };
611 
612 static void debugfs_create_files(struct request_queue *q, struct dentry *parent,
613 				 void *data,
614 				 const struct blk_mq_debugfs_attr *attr)
615 {
616 	lockdep_assert_held(&q->debugfs_mutex);
617 	/*
618 	 * debugfs_mutex should not be nested under other locks that can be
619 	 * grabbed while queue is frozen.
620 	 */
621 	lockdep_assert_not_held(&q->elevator_lock);
622 	lockdep_assert_not_held(&q->rq_qos_mutex);
623 
624 	if (IS_ERR_OR_NULL(parent))
625 		return;
626 
627 	for (; attr->name; attr++)
628 		debugfs_create_file_aux(attr->name, attr->mode, parent,
629 				    (void *)attr, data, &blk_mq_debugfs_fops);
630 }
631 
632 void blk_mq_debugfs_register(struct request_queue *q)
633 {
634 	struct blk_mq_hw_ctx *hctx;
635 	unsigned long i;
636 
637 	debugfs_create_files(q, q->debugfs_dir, q, blk_mq_debugfs_queue_attrs);
638 
639 	queue_for_each_hw_ctx(q, hctx, i) {
640 		if (!hctx->debugfs_dir)
641 			blk_mq_debugfs_register_hctx(q, hctx);
642 	}
643 
644 	blk_mq_debugfs_register_rq_qos(q);
645 }
646 
647 static void blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,
648 					struct blk_mq_ctx *ctx)
649 {
650 	struct dentry *ctx_dir;
651 	char name[20];
652 
653 	snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
654 	ctx_dir = debugfs_create_dir(name, hctx->debugfs_dir);
655 
656 	debugfs_create_files(hctx->queue, ctx_dir, ctx,
657 			     blk_mq_debugfs_ctx_attrs);
658 }
659 
660 void blk_mq_debugfs_register_hctx(struct request_queue *q,
661 				  struct blk_mq_hw_ctx *hctx)
662 {
663 	struct blk_mq_ctx *ctx;
664 	char name[20];
665 	int i;
666 
667 	if (!q->debugfs_dir)
668 		return;
669 
670 	snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
671 	hctx->debugfs_dir = debugfs_create_dir(name, q->debugfs_dir);
672 
673 	debugfs_create_files(q, hctx->debugfs_dir, hctx,
674 			     blk_mq_debugfs_hctx_attrs);
675 
676 	hctx_for_each_ctx(hctx, ctx, i)
677 		blk_mq_debugfs_register_ctx(hctx, ctx);
678 }
679 
680 void blk_mq_debugfs_unregister_hctx(struct blk_mq_hw_ctx *hctx)
681 {
682 	if (!hctx->queue->debugfs_dir)
683 		return;
684 	debugfs_remove_recursive(hctx->debugfs_dir);
685 	hctx->sched_debugfs_dir = NULL;
686 	hctx->debugfs_dir = NULL;
687 }
688 
689 void blk_mq_debugfs_register_hctxs(struct request_queue *q)
690 {
691 	struct blk_mq_hw_ctx *hctx;
692 	unsigned int memflags;
693 	unsigned long i;
694 
695 	memflags = blk_debugfs_lock(q);
696 	queue_for_each_hw_ctx(q, hctx, i)
697 		blk_mq_debugfs_register_hctx(q, hctx);
698 	blk_debugfs_unlock(q, memflags);
699 }
700 
701 void blk_mq_debugfs_unregister_hctxs(struct request_queue *q)
702 {
703 	struct blk_mq_hw_ctx *hctx;
704 	unsigned long i;
705 
706 	queue_for_each_hw_ctx(q, hctx, i)
707 		blk_mq_debugfs_unregister_hctx(hctx);
708 }
709 
710 void blk_mq_debugfs_register_sched(struct request_queue *q)
711 {
712 	struct elevator_type *e = q->elevator->type;
713 
714 	lockdep_assert_held(&q->debugfs_mutex);
715 
716 	/*
717 	 * If the parent directory has not been created yet, return, we will be
718 	 * called again later on and the directory/files will be created then.
719 	 */
720 	if (!q->debugfs_dir)
721 		return;
722 
723 	if (!e->queue_debugfs_attrs)
724 		return;
725 
726 	q->sched_debugfs_dir = debugfs_create_dir("sched", q->debugfs_dir);
727 
728 	debugfs_create_files(q, q->sched_debugfs_dir, q, e->queue_debugfs_attrs);
729 }
730 
731 void blk_mq_debugfs_unregister_sched(struct request_queue *q)
732 {
733 	lockdep_assert_held(&q->debugfs_mutex);
734 
735 	debugfs_remove_recursive(q->sched_debugfs_dir);
736 	q->sched_debugfs_dir = NULL;
737 }
738 
739 static const char *rq_qos_id_to_name(enum rq_qos_id id)
740 {
741 	switch (id) {
742 	case RQ_QOS_WBT:
743 		return "wbt";
744 	case RQ_QOS_LATENCY:
745 		return "latency";
746 	case RQ_QOS_COST:
747 		return "cost";
748 	}
749 	return "unknown";
750 }
751 
752 static void blk_mq_debugfs_register_rqos(struct rq_qos *rqos)
753 {
754 	struct request_queue *q = rqos->disk->queue;
755 	const char *dir_name = rq_qos_id_to_name(rqos->id);
756 
757 	lockdep_assert_held(&q->debugfs_mutex);
758 
759 	if (rqos->debugfs_dir || !rqos->ops->debugfs_attrs)
760 		return;
761 
762 	if (!q->rqos_debugfs_dir)
763 		q->rqos_debugfs_dir = debugfs_create_dir("rqos",
764 							 q->debugfs_dir);
765 
766 	rqos->debugfs_dir = debugfs_create_dir(dir_name, q->rqos_debugfs_dir);
767 	debugfs_create_files(q, rqos->debugfs_dir, rqos,
768 			     rqos->ops->debugfs_attrs);
769 }
770 
771 void blk_mq_debugfs_register_rq_qos(struct request_queue *q)
772 {
773 	lockdep_assert_held(&q->debugfs_mutex);
774 
775 	if (q->rq_qos) {
776 		struct rq_qos *rqos = q->rq_qos;
777 
778 		while (rqos) {
779 			blk_mq_debugfs_register_rqos(rqos);
780 			rqos = rqos->next;
781 		}
782 	}
783 }
784 
785 void blk_mq_debugfs_register_sched_hctx(struct request_queue *q,
786 					struct blk_mq_hw_ctx *hctx)
787 {
788 	struct elevator_type *e = q->elevator->type;
789 
790 	lockdep_assert_held(&q->debugfs_mutex);
791 
792 	/*
793 	 * If the parent debugfs directory has not been created yet, return;
794 	 * We will be called again later on with appropriate parent debugfs
795 	 * directory from blk_register_queue()
796 	 */
797 	if (!hctx->debugfs_dir)
798 		return;
799 
800 	if (!e->hctx_debugfs_attrs)
801 		return;
802 
803 	hctx->sched_debugfs_dir = debugfs_create_dir("sched",
804 						     hctx->debugfs_dir);
805 	debugfs_create_files(q, hctx->sched_debugfs_dir, hctx,
806 			     e->hctx_debugfs_attrs);
807 }
808 
809 void blk_mq_debugfs_unregister_sched_hctx(struct blk_mq_hw_ctx *hctx)
810 {
811 	lockdep_assert_held(&hctx->queue->debugfs_mutex);
812 
813 	if (!hctx->queue->debugfs_dir)
814 		return;
815 	debugfs_remove_recursive(hctx->sched_debugfs_dir);
816 	hctx->sched_debugfs_dir = NULL;
817 }
818