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