xref: /linux/block/blk-mq-debugfs.c (revision a518d63777a4e94e4b2dd86501604ec49ffe86b2)
1 /*
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>
22 #include "blk.h"
23 #include "blk-mq.h"
24 #include "blk-mq-debugfs.h"
25 #include "blk-mq-tag.h"
26 
27 static 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)
37 			seq_puts(m, "|");
38 		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 	}
44 	return 0;
45 }
46 
47 #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
48 static const char *const blk_queue_flag_name[] = {
49 	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),
78 	QUEUE_FLAG_NAME(SCSI_PASSTHROUGH),
79 	QUEUE_FLAG_NAME(QUIESCED),
80 };
81 #undef QUEUE_FLAG_NAME
82 
83 static int queue_state_show(void *data, struct seq_file *m)
84 {
85 	struct request_queue *q = data;
86 
87 	blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
88 		       ARRAY_SIZE(blk_queue_flag_name));
89 	seq_puts(m, "\n");
90 	return 0;
91 }
92 
93 static ssize_t queue_state_write(void *data, const char __user *buf,
94 				 size_t count, loff_t *ppos)
95 {
96 	struct request_queue *q = data;
97 	char opbuf[16] = { }, *op;
98 
99 	/*
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 
107 	if (count >= sizeof(opbuf)) {
108 		pr_err("%s: operation too long\n", __func__);
109 		goto inval;
110 	}
111 
112 	if (copy_from_user(opbuf, buf, count))
113 		return -EFAULT;
114 	op = strstrip(opbuf);
115 	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);
119 	} else if (strcmp(op, "kick") == 0) {
120 		blk_mq_kick_requeue_list(q);
121 	} else {
122 		pr_err("%s: unsupported operation '%s'\n", __func__, op);
123 inval:
124 		pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
125 		return -EINVAL;
126 	}
127 	return count;
128 }
129 
130 static 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 
140 static 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 
151 static 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 
163 static int queue_poll_stat_show(void *data, struct seq_file *m)
164 {
165 	struct request_queue *q = data;
166 	int bucket;
167 
168 	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");
172 
173 		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 	}
177 	return 0;
178 }
179 
180 #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
181 static const char *const hctx_state_name[] = {
182 	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),
187 };
188 #undef HCTX_STATE_NAME
189 
190 static int hctx_state_show(void *data, struct seq_file *m)
191 {
192 	struct blk_mq_hw_ctx *hctx = data;
193 
194 	blk_flags_show(m, hctx->state, hctx_state_name,
195 		       ARRAY_SIZE(hctx_state_name));
196 	seq_puts(m, "\n");
197 	return 0;
198 }
199 
200 #define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
201 static const char *const alloc_policy_name[] = {
202 	BLK_TAG_ALLOC_NAME(FIFO),
203 	BLK_TAG_ALLOC_NAME(RR),
204 };
205 #undef BLK_TAG_ALLOC_NAME
206 
207 #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
208 static const char *const hctx_flag_name[] = {
209 	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),
214 };
215 #undef HCTX_FLAG_NAME
216 
217 static int hctx_flags_show(void *data, struct seq_file *m)
218 {
219 	struct blk_mq_hw_ctx *hctx = data;
220 	const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
221 
222 	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));
232 	seq_puts(m, "\n");
233 	return 0;
234 }
235 
236 #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
237 static const char *const op_name[] = {
238 	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),
251 };
252 #undef REQ_OP_NAME
253 
254 #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
255 static const char *const cmd_flag_name[] = {
256 	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),
270 	CMD_FLAG_NAME(NOWAIT),
271 };
272 #undef CMD_FLAG_NAME
273 
274 #define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
275 static const char *const rqf_name[] = {
276 	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),
295 };
296 #undef RQF_NAME
297 
298 #define RQAF_NAME(name) [REQ_ATOM_##name] = #name
299 static const char *const rqaf_name[] = {
300 	RQAF_NAME(COMPLETE),
301 	RQAF_NAME(STARTED),
302 	RQAF_NAME(POLL_SLEPT),
303 };
304 #undef RQAF_NAME
305 
306 int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
307 {
308 	const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
309 	const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
310 
311 	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));
322 	seq_puts(m, ", .atomic_flags=");
323 	blk_flags_show(m, rq->atomic_flags, rqaf_name, ARRAY_SIZE(rqaf_name));
324 	seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
325 		   rq->internal_tag);
326 	if (mq_ops->show_rq)
327 		mq_ops->show_rq(m, rq);
328 	seq_puts(m, "}\n");
329 	return 0;
330 }
331 EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
332 
333 int 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 }
337 EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
338 
339 static 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 
348 static 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 
355 static 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 
363 static 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 
370 static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
371 	__acquires(&hctx->lock)
372 {
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 
379 static 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 
386 static void hctx_dispatch_stop(struct seq_file *m, void *v)
387 	__releases(&hctx->lock)
388 {
389 	struct blk_mq_hw_ctx *hctx = m->private;
390 
391 	spin_unlock(&hctx->lock);
392 }
393 
394 static 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 
401 struct 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  */
410 static 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 
420 static 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 
431 static int hctx_ctx_map_show(void *data, struct seq_file *m)
432 {
433 	struct blk_mq_hw_ctx *hctx = data;
434 
435 	sbitmap_bitmap_show(&hctx->ctx_map, m);
436 	return 0;
437 }
438 
439 static 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 
456 static int hctx_tags_show(void *data, struct seq_file *m)
457 {
458 	struct blk_mq_hw_ctx *hctx = data;
459 	struct request_queue *q = hctx->queue;
460 	int res;
461 
462 	res = mutex_lock_interruptible(&q->sysfs_lock);
463 	if (res)
464 		goto out;
465 	if (hctx->tags)
466 		blk_mq_debugfs_tags_show(m, hctx->tags);
467 	mutex_unlock(&q->sysfs_lock);
468 
469 out:
470 	return res;
471 }
472 
473 static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
474 {
475 	struct blk_mq_hw_ctx *hctx = data;
476 	struct request_queue *q = hctx->queue;
477 	int res;
478 
479 	res = mutex_lock_interruptible(&q->sysfs_lock);
480 	if (res)
481 		goto out;
482 	if (hctx->tags)
483 		sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
484 	mutex_unlock(&q->sysfs_lock);
485 
486 out:
487 	return res;
488 }
489 
490 static int hctx_sched_tags_show(void *data, struct seq_file *m)
491 {
492 	struct blk_mq_hw_ctx *hctx = data;
493 	struct request_queue *q = hctx->queue;
494 	int res;
495 
496 	res = mutex_lock_interruptible(&q->sysfs_lock);
497 	if (res)
498 		goto out;
499 	if (hctx->sched_tags)
500 		blk_mq_debugfs_tags_show(m, hctx->sched_tags);
501 	mutex_unlock(&q->sysfs_lock);
502 
503 out:
504 	return res;
505 }
506 
507 static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
508 {
509 	struct blk_mq_hw_ctx *hctx = data;
510 	struct request_queue *q = hctx->queue;
511 	int res;
512 
513 	res = mutex_lock_interruptible(&q->sysfs_lock);
514 	if (res)
515 		goto out;
516 	if (hctx->sched_tags)
517 		sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
518 	mutex_unlock(&q->sysfs_lock);
519 
520 out:
521 	return res;
522 }
523 
524 static int hctx_io_poll_show(void *data, struct seq_file *m)
525 {
526 	struct blk_mq_hw_ctx *hctx = data;
527 
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 
534 static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
535 				  size_t count, loff_t *ppos)
536 {
537 	struct blk_mq_hw_ctx *hctx = data;
538 
539 	hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
540 	return count;
541 }
542 
543 static int hctx_dispatched_show(void *data, struct seq_file *m)
544 {
545 	struct blk_mq_hw_ctx *hctx = data;
546 	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 
560 static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
561 				     size_t count, loff_t *ppos)
562 {
563 	struct blk_mq_hw_ctx *hctx = data;
564 	int i;
565 
566 	for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
567 		hctx->dispatched[i] = 0;
568 	return count;
569 }
570 
571 static int hctx_queued_show(void *data, struct seq_file *m)
572 {
573 	struct blk_mq_hw_ctx *hctx = data;
574 
575 	seq_printf(m, "%lu\n", hctx->queued);
576 	return 0;
577 }
578 
579 static ssize_t hctx_queued_write(void *data, const char __user *buf,
580 				 size_t count, loff_t *ppos)
581 {
582 	struct blk_mq_hw_ctx *hctx = data;
583 
584 	hctx->queued = 0;
585 	return count;
586 }
587 
588 static int hctx_run_show(void *data, struct seq_file *m)
589 {
590 	struct blk_mq_hw_ctx *hctx = data;
591 
592 	seq_printf(m, "%lu\n", hctx->run);
593 	return 0;
594 }
595 
596 static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
597 			      loff_t *ppos)
598 {
599 	struct blk_mq_hw_ctx *hctx = data;
600 
601 	hctx->run = 0;
602 	return count;
603 }
604 
605 static int hctx_active_show(void *data, struct seq_file *m)
606 {
607 	struct blk_mq_hw_ctx *hctx = data;
608 
609 	seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
610 	return 0;
611 }
612 
613 static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
614 	__acquires(&ctx->lock)
615 {
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 
622 static 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 
629 static void ctx_rq_list_stop(struct seq_file *m, void *v)
630 	__releases(&ctx->lock)
631 {
632 	struct blk_mq_ctx *ctx = m->private;
633 
634 	spin_unlock(&ctx->lock);
635 }
636 
637 static 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 };
643 static int ctx_dispatched_show(void *data, struct seq_file *m)
644 {
645 	struct blk_mq_ctx *ctx = data;
646 
647 	seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
648 	return 0;
649 }
650 
651 static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
652 				    size_t count, loff_t *ppos)
653 {
654 	struct blk_mq_ctx *ctx = data;
655 
656 	ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
657 	return count;
658 }
659 
660 static int ctx_merged_show(void *data, struct seq_file *m)
661 {
662 	struct blk_mq_ctx *ctx = data;
663 
664 	seq_printf(m, "%lu\n", ctx->rq_merged);
665 	return 0;
666 }
667 
668 static ssize_t ctx_merged_write(void *data, const char __user *buf,
669 				size_t count, loff_t *ppos)
670 {
671 	struct blk_mq_ctx *ctx = data;
672 
673 	ctx->rq_merged = 0;
674 	return count;
675 }
676 
677 static int ctx_completed_show(void *data, struct seq_file *m)
678 {
679 	struct blk_mq_ctx *ctx = data;
680 
681 	seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
682 	return 0;
683 }
684 
685 static ssize_t ctx_completed_write(void *data, const char __user *buf,
686 				   size_t count, loff_t *ppos)
687 {
688 	struct blk_mq_ctx *ctx = data;
689 
690 	ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
691 	return count;
692 }
693 
694 static 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 
702 static 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 
715 static 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 
737 static 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 
747 const struct file_operations blk_mq_debugfs_fops = {
748 	.open		= blk_mq_debugfs_open,
749 	.read		= seq_read,
750 	.write		= blk_mq_debugfs_write,
751 	.llseek		= seq_lseek,
752 	.release	= blk_mq_debugfs_release,
753 };
754 
755 static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
756 	{"poll_stat", 0400, queue_poll_stat_show},
757 	{"requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops},
758 	{"state", 0600, queue_state_show, queue_state_write},
759 	{"write_hints", 0600, queue_write_hint_show, queue_write_hint_store},
760 	{},
761 };
762 
763 static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
764 	{"state", 0400, hctx_state_show},
765 	{"flags", 0400, hctx_flags_show},
766 	{"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
767 	{"busy", 0400, hctx_busy_show},
768 	{"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},
778 	{},
779 };
780 
781 static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
782 	{"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},
786 	{},
787 };
788 
789 static bool debugfs_create_files(struct dentry *parent, void *data,
790 				 const struct blk_mq_debugfs_attr *attr)
791 {
792 	d_inode(parent)->i_private = data;
793 
794 	for (; attr->name; attr++) {
795 		if (!debugfs_create_file(attr->name, attr->mode, parent,
796 					 (void *)attr, &blk_mq_debugfs_fops))
797 			return false;
798 	}
799 	return true;
800 }
801 
802 int 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;
827 		if (q->elevator && !hctx->sched_debugfs_dir &&
828 		    blk_mq_debugfs_register_sched_hctx(q, hctx))
829 			goto err;
830 	}
831 
832 	return 0;
833 
834 err:
835 	blk_mq_debugfs_unregister(q);
836 	return -ENOMEM;
837 }
838 
839 void blk_mq_debugfs_unregister(struct request_queue *q)
840 {
841 	debugfs_remove_recursive(q->debugfs_dir);
842 	q->sched_debugfs_dir = NULL;
843 	q->debugfs_dir = NULL;
844 }
845 
846 static int blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,
847 				       struct blk_mq_ctx *ctx)
848 {
849 	struct dentry *ctx_dir;
850 	char name[20];
851 
852 	snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
853 	ctx_dir = debugfs_create_dir(name, hctx->debugfs_dir);
854 	if (!ctx_dir)
855 		return -ENOMEM;
856 
857 	if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
858 		return -ENOMEM;
859 
860 	return 0;
861 }
862 
863 int blk_mq_debugfs_register_hctx(struct request_queue *q,
864 				 struct blk_mq_hw_ctx *hctx)
865 {
866 	struct blk_mq_ctx *ctx;
867 	char name[20];
868 	int i;
869 
870 	if (!q->debugfs_dir)
871 		return -ENOENT;
872 
873 	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))
880 		goto err;
881 
882 	hctx_for_each_ctx(hctx, ctx, i) {
883 		if (blk_mq_debugfs_register_ctx(hctx, ctx))
884 			goto err;
885 	}
886 
887 	return 0;
888 
889 err:
890 	blk_mq_debugfs_unregister_hctx(hctx);
891 	return -ENOMEM;
892 }
893 
894 void blk_mq_debugfs_unregister_hctx(struct blk_mq_hw_ctx *hctx)
895 {
896 	debugfs_remove_recursive(hctx->debugfs_dir);
897 	hctx->sched_debugfs_dir = NULL;
898 	hctx->debugfs_dir = NULL;
899 }
900 
901 int 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 
914 void 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);
921 }
922 
923 int 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 
943 err:
944 	blk_mq_debugfs_unregister_sched(q);
945 	return -ENOMEM;
946 }
947 
948 void 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 
954 int 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 
977 void 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 }
982