xref: /linux/block/blk-mq-debugfs.c (revision cd84a62e0078dce09f4ed349bec84f86c9d54b30)
107e4feadSOmar Sandoval /*
207e4feadSOmar Sandoval  * Copyright (C) 2017 Facebook
307e4feadSOmar Sandoval  *
407e4feadSOmar Sandoval  * This program is free software; you can redistribute it and/or
507e4feadSOmar Sandoval  * modify it under the terms of the GNU General Public
607e4feadSOmar Sandoval  * License v2 as published by the Free Software Foundation.
707e4feadSOmar Sandoval  *
807e4feadSOmar Sandoval  * This program is distributed in the hope that it will be useful,
907e4feadSOmar Sandoval  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1007e4feadSOmar Sandoval  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1107e4feadSOmar Sandoval  * General Public License for more details.
1207e4feadSOmar Sandoval  *
1307e4feadSOmar Sandoval  * You should have received a copy of the GNU General Public License
1407e4feadSOmar Sandoval  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
1507e4feadSOmar Sandoval  */
1607e4feadSOmar Sandoval 
1707e4feadSOmar Sandoval #include <linux/kernel.h>
1807e4feadSOmar Sandoval #include <linux/blkdev.h>
1907e4feadSOmar Sandoval #include <linux/debugfs.h>
2007e4feadSOmar Sandoval 
2107e4feadSOmar Sandoval #include <linux/blk-mq.h>
2218fbda91SOmar Sandoval #include "blk.h"
2307e4feadSOmar Sandoval #include "blk-mq.h"
24d173a251SOmar Sandoval #include "blk-mq-debugfs.h"
25d96b37c0SOmar Sandoval #include "blk-mq-tag.h"
2607e4feadSOmar Sandoval 
271209cb7fSBart Van Assche static void print_stat(struct seq_file *m, struct blk_rq_stat *stat)
281209cb7fSBart Van Assche {
291209cb7fSBart Van Assche 	if (stat->nr_samples) {
301209cb7fSBart Van Assche 		seq_printf(m, "samples=%d, mean=%lld, min=%llu, max=%llu",
311209cb7fSBart Van Assche 			   stat->nr_samples, stat->mean, stat->min, stat->max);
321209cb7fSBart Van Assche 	} else {
331209cb7fSBart Van Assche 		seq_puts(m, "samples=0");
341209cb7fSBart Van Assche 	}
351209cb7fSBart Van Assche }
361209cb7fSBart Van Assche 
371209cb7fSBart Van Assche static int queue_poll_stat_show(void *data, struct seq_file *m)
381209cb7fSBart Van Assche {
391209cb7fSBart Van Assche 	struct request_queue *q = data;
401209cb7fSBart Van Assche 	int bucket;
411209cb7fSBart Van Assche 
421209cb7fSBart Van Assche 	for (bucket = 0; bucket < BLK_MQ_POLL_STATS_BKTS/2; bucket++) {
431209cb7fSBart Van Assche 		seq_printf(m, "read  (%d Bytes): ", 1 << (9+bucket));
441209cb7fSBart Van Assche 		print_stat(m, &q->poll_stat[2*bucket]);
451209cb7fSBart Van Assche 		seq_puts(m, "\n");
461209cb7fSBart Van Assche 
471209cb7fSBart Van Assche 		seq_printf(m, "write (%d Bytes): ",  1 << (9+bucket));
481209cb7fSBart Van Assche 		print_stat(m, &q->poll_stat[2*bucket+1]);
491209cb7fSBart Van Assche 		seq_puts(m, "\n");
501209cb7fSBart Van Assche 	}
511209cb7fSBart Van Assche 	return 0;
521209cb7fSBart Van Assche }
531209cb7fSBart Van Assche 
541209cb7fSBart Van Assche static void *queue_requeue_list_start(struct seq_file *m, loff_t *pos)
551209cb7fSBart Van Assche 	__acquires(&q->requeue_lock)
561209cb7fSBart Van Assche {
571209cb7fSBart Van Assche 	struct request_queue *q = m->private;
581209cb7fSBart Van Assche 
591209cb7fSBart Van Assche 	spin_lock_irq(&q->requeue_lock);
601209cb7fSBart Van Assche 	return seq_list_start(&q->requeue_list, *pos);
611209cb7fSBart Van Assche }
621209cb7fSBart Van Assche 
631209cb7fSBart Van Assche static void *queue_requeue_list_next(struct seq_file *m, void *v, loff_t *pos)
641209cb7fSBart Van Assche {
651209cb7fSBart Van Assche 	struct request_queue *q = m->private;
661209cb7fSBart Van Assche 
671209cb7fSBart Van Assche 	return seq_list_next(v, &q->requeue_list, pos);
681209cb7fSBart Van Assche }
691209cb7fSBart Van Assche 
701209cb7fSBart Van Assche static void queue_requeue_list_stop(struct seq_file *m, void *v)
711209cb7fSBart Van Assche 	__releases(&q->requeue_lock)
721209cb7fSBart Van Assche {
731209cb7fSBart Van Assche 	struct request_queue *q = m->private;
741209cb7fSBart Van Assche 
751209cb7fSBart Van Assche 	spin_unlock_irq(&q->requeue_lock);
761209cb7fSBart Van Assche }
771209cb7fSBart Van Assche 
781209cb7fSBart Van Assche static const struct seq_operations queue_requeue_list_seq_ops = {
791209cb7fSBart Van Assche 	.start	= queue_requeue_list_start,
801209cb7fSBart Van Assche 	.next	= queue_requeue_list_next,
811209cb7fSBart Van Assche 	.stop	= queue_requeue_list_stop,
821209cb7fSBart Van Assche 	.show	= blk_mq_debugfs_rq_show,
831209cb7fSBart Van Assche };
841209cb7fSBart Van Assche 
8591d68905SBart Van Assche static int blk_flags_show(struct seq_file *m, const unsigned long flags,
8691d68905SBart Van Assche 			  const char *const *flag_name, int flag_name_count)
8791d68905SBart Van Assche {
8891d68905SBart Van Assche 	bool sep = false;
8991d68905SBart Van Assche 	int i;
9091d68905SBart Van Assche 
9191d68905SBart Van Assche 	for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
9291d68905SBart Van Assche 		if (!(flags & BIT(i)))
9391d68905SBart Van Assche 			continue;
9491d68905SBart Van Assche 		if (sep)
95bec03d6bSOmar Sandoval 			seq_puts(m, "|");
9691d68905SBart Van Assche 		sep = true;
9791d68905SBart Van Assche 		if (i < flag_name_count && flag_name[i])
9891d68905SBart Van Assche 			seq_puts(m, flag_name[i]);
9991d68905SBart Van Assche 		else
10091d68905SBart Van Assche 			seq_printf(m, "%d", i);
10191d68905SBart Van Assche 	}
10291d68905SBart Van Assche 	return 0;
10391d68905SBart Van Assche }
10491d68905SBart Van Assche 
105*cd84a62eSBart Van Assche static int queue_pm_only_show(void *data, struct seq_file *m)
106*cd84a62eSBart Van Assche {
107*cd84a62eSBart Van Assche 	struct request_queue *q = data;
108*cd84a62eSBart Van Assche 
109*cd84a62eSBart Van Assche 	seq_printf(m, "%d\n", atomic_read(&q->pm_only));
110*cd84a62eSBart Van Assche 	return 0;
111*cd84a62eSBart Van Assche }
112*cd84a62eSBart Van Assche 
1131a435111SOmar Sandoval #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
11491d68905SBart Van Assche static const char *const blk_queue_flag_name[] = {
1151a435111SOmar Sandoval 	QUEUE_FLAG_NAME(QUEUED),
1161a435111SOmar Sandoval 	QUEUE_FLAG_NAME(STOPPED),
1171a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DYING),
1181a435111SOmar Sandoval 	QUEUE_FLAG_NAME(BYPASS),
1191a435111SOmar Sandoval 	QUEUE_FLAG_NAME(BIDI),
1201a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NOMERGES),
1211a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SAME_COMP),
1221a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FAIL_IO),
1231a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NONROT),
1241a435111SOmar Sandoval 	QUEUE_FLAG_NAME(IO_STAT),
1251a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DISCARD),
1261a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NOXMERGES),
1271a435111SOmar Sandoval 	QUEUE_FLAG_NAME(ADD_RANDOM),
1281a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SECERASE),
1291a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SAME_FORCE),
1301a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DEAD),
1311a435111SOmar Sandoval 	QUEUE_FLAG_NAME(INIT_DONE),
1321a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NO_SG_MERGE),
1331a435111SOmar Sandoval 	QUEUE_FLAG_NAME(POLL),
1341a435111SOmar Sandoval 	QUEUE_FLAG_NAME(WC),
1351a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FUA),
1361a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FLUSH_NQ),
1371a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DAX),
1381a435111SOmar Sandoval 	QUEUE_FLAG_NAME(STATS),
1391a435111SOmar Sandoval 	QUEUE_FLAG_NAME(POLL_STATS),
1401a435111SOmar Sandoval 	QUEUE_FLAG_NAME(REGISTERED),
14122d53821SBart Van Assche 	QUEUE_FLAG_NAME(SCSI_PASSTHROUGH),
14222d53821SBart Van Assche 	QUEUE_FLAG_NAME(QUIESCED),
14391d68905SBart Van Assche };
1441a435111SOmar Sandoval #undef QUEUE_FLAG_NAME
14591d68905SBart Van Assche 
146f57de23aSOmar Sandoval static int queue_state_show(void *data, struct seq_file *m)
14791d68905SBart Van Assche {
148f57de23aSOmar Sandoval 	struct request_queue *q = data;
14991d68905SBart Van Assche 
15091d68905SBart Van Assche 	blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
15191d68905SBart Van Assche 		       ARRAY_SIZE(blk_queue_flag_name));
152fd07dc81SBart Van Assche 	seq_puts(m, "\n");
15391d68905SBart Van Assche 	return 0;
15491d68905SBart Van Assche }
15591d68905SBart Van Assche 
156f57de23aSOmar Sandoval static ssize_t queue_state_write(void *data, const char __user *buf,
157c7e4145aSOmar Sandoval 				 size_t count, loff_t *ppos)
15891d68905SBart Van Assche {
159f57de23aSOmar Sandoval 	struct request_queue *q = data;
16071b90511SOmar Sandoval 	char opbuf[16] = { }, *op;
16191d68905SBart Van Assche 
16218d4d7d0SBart Van Assche 	/*
16318d4d7d0SBart Van Assche 	 * The "state" attribute is removed after blk_cleanup_queue() has called
16418d4d7d0SBart Van Assche 	 * blk_mq_free_queue(). Return if QUEUE_FLAG_DEAD has been set to avoid
16518d4d7d0SBart Van Assche 	 * triggering a use-after-free.
16618d4d7d0SBart Van Assche 	 */
16718d4d7d0SBart Van Assche 	if (blk_queue_dead(q))
16818d4d7d0SBart Van Assche 		return -ENOENT;
16918d4d7d0SBart Van Assche 
17071b90511SOmar Sandoval 	if (count >= sizeof(opbuf)) {
171c7e4145aSOmar Sandoval 		pr_err("%s: operation too long\n", __func__);
172c7e4145aSOmar Sandoval 		goto inval;
173c7e4145aSOmar Sandoval 	}
174c7e4145aSOmar Sandoval 
17571b90511SOmar Sandoval 	if (copy_from_user(opbuf, buf, count))
17691d68905SBart Van Assche 		return -EFAULT;
17771b90511SOmar Sandoval 	op = strstrip(opbuf);
17891d68905SBart Van Assche 	if (strcmp(op, "run") == 0) {
17991d68905SBart Van Assche 		blk_mq_run_hw_queues(q, true);
18091d68905SBart Van Assche 	} else if (strcmp(op, "start") == 0) {
18191d68905SBart Van Assche 		blk_mq_start_stopped_hw_queues(q, true);
182edea55abSBart Van Assche 	} else if (strcmp(op, "kick") == 0) {
183edea55abSBart Van Assche 		blk_mq_kick_requeue_list(q);
18491d68905SBart Van Assche 	} else {
185c7e4145aSOmar Sandoval 		pr_err("%s: unsupported operation '%s'\n", __func__, op);
186c7e4145aSOmar Sandoval inval:
187edea55abSBart Van Assche 		pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
18891d68905SBart Van Assche 		return -EINVAL;
18991d68905SBart Van Assche 	}
190c7e4145aSOmar Sandoval 	return count;
19191d68905SBart Van Assche }
19291d68905SBart Van Assche 
193f793dfd3SJens Axboe static int queue_write_hint_show(void *data, struct seq_file *m)
194f793dfd3SJens Axboe {
195f793dfd3SJens Axboe 	struct request_queue *q = data;
196f793dfd3SJens Axboe 	int i;
197f793dfd3SJens Axboe 
198f793dfd3SJens Axboe 	for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
199f793dfd3SJens Axboe 		seq_printf(m, "hint%d: %llu\n", i, q->write_hints[i]);
200f793dfd3SJens Axboe 
201f793dfd3SJens Axboe 	return 0;
202f793dfd3SJens Axboe }
203f793dfd3SJens Axboe 
204f793dfd3SJens Axboe static ssize_t queue_write_hint_store(void *data, const char __user *buf,
205f793dfd3SJens Axboe 				      size_t count, loff_t *ppos)
206f793dfd3SJens Axboe {
207f793dfd3SJens Axboe 	struct request_queue *q = data;
208f793dfd3SJens Axboe 	int i;
209f793dfd3SJens Axboe 
210f793dfd3SJens Axboe 	for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
211f793dfd3SJens Axboe 		q->write_hints[i] = 0;
212f793dfd3SJens Axboe 
213f793dfd3SJens Axboe 	return count;
214f793dfd3SJens Axboe }
215f793dfd3SJens Axboe 
2161209cb7fSBart Van Assche static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
2171209cb7fSBart Van Assche 	{ "poll_stat", 0400, queue_poll_stat_show },
2181209cb7fSBart Van Assche 	{ "requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops },
219*cd84a62eSBart Van Assche 	{ "pm_only", 0600, queue_pm_only_show, NULL },
2201209cb7fSBart Van Assche 	{ "state", 0600, queue_state_show, queue_state_write },
2211209cb7fSBart Van Assche 	{ "write_hints", 0600, queue_write_hint_show, queue_write_hint_store },
22218bc4230SBart Van Assche 	{ "zone_wlock", 0400, queue_zone_wlock_show, NULL },
2231209cb7fSBart Van Assche 	{ },
2241209cb7fSBart Van Assche };
22534dbad5dSOmar Sandoval 
2261a435111SOmar Sandoval #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
227f5c0b091SBart Van Assche static const char *const hctx_state_name[] = {
2281a435111SOmar Sandoval 	HCTX_STATE_NAME(STOPPED),
2291a435111SOmar Sandoval 	HCTX_STATE_NAME(TAG_ACTIVE),
2301a435111SOmar Sandoval 	HCTX_STATE_NAME(SCHED_RESTART),
231f5c0b091SBart Van Assche };
2321a435111SOmar Sandoval #undef HCTX_STATE_NAME
2331a435111SOmar Sandoval 
234f57de23aSOmar Sandoval static int hctx_state_show(void *data, struct seq_file *m)
2359abb2ad2SOmar Sandoval {
236f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
2379abb2ad2SOmar Sandoval 
238f5c0b091SBart Van Assche 	blk_flags_show(m, hctx->state, hctx_state_name,
239f5c0b091SBart Van Assche 		       ARRAY_SIZE(hctx_state_name));
240fd07dc81SBart Van Assche 	seq_puts(m, "\n");
2419abb2ad2SOmar Sandoval 	return 0;
2429abb2ad2SOmar Sandoval }
2439abb2ad2SOmar Sandoval 
2441a435111SOmar Sandoval #define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
245f5c0b091SBart Van Assche static const char *const alloc_policy_name[] = {
2461a435111SOmar Sandoval 	BLK_TAG_ALLOC_NAME(FIFO),
2471a435111SOmar Sandoval 	BLK_TAG_ALLOC_NAME(RR),
248f5c0b091SBart Van Assche };
2491a435111SOmar Sandoval #undef BLK_TAG_ALLOC_NAME
250f5c0b091SBart Van Assche 
2511a435111SOmar Sandoval #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
252f5c0b091SBart Van Assche static const char *const hctx_flag_name[] = {
2531a435111SOmar Sandoval 	HCTX_FLAG_NAME(SHOULD_MERGE),
2541a435111SOmar Sandoval 	HCTX_FLAG_NAME(TAG_SHARED),
2551a435111SOmar Sandoval 	HCTX_FLAG_NAME(SG_MERGE),
2561a435111SOmar Sandoval 	HCTX_FLAG_NAME(BLOCKING),
2571a435111SOmar Sandoval 	HCTX_FLAG_NAME(NO_SCHED),
258f5c0b091SBart Van Assche };
2591a435111SOmar Sandoval #undef HCTX_FLAG_NAME
260f5c0b091SBart Van Assche 
261f57de23aSOmar Sandoval static int hctx_flags_show(void *data, struct seq_file *m)
2629abb2ad2SOmar Sandoval {
263f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
264f5c0b091SBart Van Assche 	const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
2659abb2ad2SOmar Sandoval 
266f5c0b091SBart Van Assche 	seq_puts(m, "alloc_policy=");
267f5c0b091SBart Van Assche 	if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
268f5c0b091SBart Van Assche 	    alloc_policy_name[alloc_policy])
269f5c0b091SBart Van Assche 		seq_puts(m, alloc_policy_name[alloc_policy]);
270f5c0b091SBart Van Assche 	else
271f5c0b091SBart Van Assche 		seq_printf(m, "%d", alloc_policy);
272f5c0b091SBart Van Assche 	seq_puts(m, " ");
273f5c0b091SBart Van Assche 	blk_flags_show(m,
274f5c0b091SBart Van Assche 		       hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
275f5c0b091SBart Van Assche 		       hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
276fd07dc81SBart Van Assche 	seq_puts(m, "\n");
2779abb2ad2SOmar Sandoval 	return 0;
2789abb2ad2SOmar Sandoval }
2799abb2ad2SOmar Sandoval 
2801a435111SOmar Sandoval #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
2818658dca8SBart Van Assche static const char *const op_name[] = {
2821a435111SOmar Sandoval 	REQ_OP_NAME(READ),
2831a435111SOmar Sandoval 	REQ_OP_NAME(WRITE),
2841a435111SOmar Sandoval 	REQ_OP_NAME(FLUSH),
2851a435111SOmar Sandoval 	REQ_OP_NAME(DISCARD),
2861a435111SOmar Sandoval 	REQ_OP_NAME(ZONE_REPORT),
2871a435111SOmar Sandoval 	REQ_OP_NAME(SECURE_ERASE),
2881a435111SOmar Sandoval 	REQ_OP_NAME(ZONE_RESET),
2891a435111SOmar Sandoval 	REQ_OP_NAME(WRITE_SAME),
2901a435111SOmar Sandoval 	REQ_OP_NAME(WRITE_ZEROES),
2911a435111SOmar Sandoval 	REQ_OP_NAME(SCSI_IN),
2921a435111SOmar Sandoval 	REQ_OP_NAME(SCSI_OUT),
2931a435111SOmar Sandoval 	REQ_OP_NAME(DRV_IN),
2941a435111SOmar Sandoval 	REQ_OP_NAME(DRV_OUT),
2958658dca8SBart Van Assche };
2961a435111SOmar Sandoval #undef REQ_OP_NAME
2978658dca8SBart Van Assche 
2981a435111SOmar Sandoval #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
2998658dca8SBart Van Assche static const char *const cmd_flag_name[] = {
3001a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_DEV),
3011a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_TRANSPORT),
3021a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_DRIVER),
3031a435111SOmar Sandoval 	CMD_FLAG_NAME(SYNC),
3041a435111SOmar Sandoval 	CMD_FLAG_NAME(META),
3051a435111SOmar Sandoval 	CMD_FLAG_NAME(PRIO),
3061a435111SOmar Sandoval 	CMD_FLAG_NAME(NOMERGE),
3071a435111SOmar Sandoval 	CMD_FLAG_NAME(IDLE),
3081a435111SOmar Sandoval 	CMD_FLAG_NAME(INTEGRITY),
3091a435111SOmar Sandoval 	CMD_FLAG_NAME(FUA),
3101a435111SOmar Sandoval 	CMD_FLAG_NAME(PREFLUSH),
3111a435111SOmar Sandoval 	CMD_FLAG_NAME(RAHEAD),
3121a435111SOmar Sandoval 	CMD_FLAG_NAME(BACKGROUND),
3131a435111SOmar Sandoval 	CMD_FLAG_NAME(NOUNMAP),
31422d53821SBart Van Assche 	CMD_FLAG_NAME(NOWAIT),
3158658dca8SBart Van Assche };
3161a435111SOmar Sandoval #undef CMD_FLAG_NAME
3178658dca8SBart Van Assche 
3181a435111SOmar Sandoval #define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
3198658dca8SBart Van Assche static const char *const rqf_name[] = {
3201a435111SOmar Sandoval 	RQF_NAME(SORTED),
32185ba3effSJens Axboe 	RQF_NAME(STARTED),
3221a435111SOmar Sandoval 	RQF_NAME(QUEUED),
3231a435111SOmar Sandoval 	RQF_NAME(SOFTBARRIER),
3241a435111SOmar Sandoval 	RQF_NAME(FLUSH_SEQ),
3251a435111SOmar Sandoval 	RQF_NAME(MIXED_MERGE),
3261a435111SOmar Sandoval 	RQF_NAME(MQ_INFLIGHT),
3271a435111SOmar Sandoval 	RQF_NAME(DONTPREP),
3281a435111SOmar Sandoval 	RQF_NAME(PREEMPT),
3291a435111SOmar Sandoval 	RQF_NAME(COPY_USER),
3301a435111SOmar Sandoval 	RQF_NAME(FAILED),
3311a435111SOmar Sandoval 	RQF_NAME(QUIET),
3321a435111SOmar Sandoval 	RQF_NAME(ELVPRIV),
3331a435111SOmar Sandoval 	RQF_NAME(IO_STAT),
3341a435111SOmar Sandoval 	RQF_NAME(ALLOCED),
3351a435111SOmar Sandoval 	RQF_NAME(PM),
3361a435111SOmar Sandoval 	RQF_NAME(HASHED),
3371a435111SOmar Sandoval 	RQF_NAME(STATS),
3381a435111SOmar Sandoval 	RQF_NAME(SPECIAL_PAYLOAD),
3395d75d3f2SJens Axboe 	RQF_NAME(ZONE_WRITE_LOCKED),
34076a86f9dSJens Axboe 	RQF_NAME(MQ_POLL_SLEPT),
3418658dca8SBart Van Assche };
3421a435111SOmar Sandoval #undef RQF_NAME
3438658dca8SBart Van Assche 
344ec6dcf63SBart Van Assche static const char *const blk_mq_rq_state_name_array[] = {
345ec6dcf63SBart Van Assche 	[MQ_RQ_IDLE]		= "idle",
346ec6dcf63SBart Van Assche 	[MQ_RQ_IN_FLIGHT]	= "in_flight",
347ec6dcf63SBart Van Assche 	[MQ_RQ_COMPLETE]	= "complete",
348ec6dcf63SBart Van Assche };
349ec6dcf63SBart Van Assche 
350ec6dcf63SBart Van Assche static const char *blk_mq_rq_state_name(enum mq_rq_state rq_state)
351ec6dcf63SBart Van Assche {
352a1e79188SDan Carpenter 	if (WARN_ON_ONCE((unsigned int)rq_state >=
353ec6dcf63SBart Van Assche 			 ARRAY_SIZE(blk_mq_rq_state_name_array)))
354ec6dcf63SBart Van Assche 		return "(?)";
355ec6dcf63SBart Van Assche 	return blk_mq_rq_state_name_array[rq_state];
356ec6dcf63SBart Van Assche }
357ec6dcf63SBart Van Assche 
358daaadb3eSOmar Sandoval int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
359950cd7e9SOmar Sandoval {
3602836ee4bSBart Van Assche 	const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
3618658dca8SBart Van Assche 	const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
362950cd7e9SOmar Sandoval 
3638658dca8SBart Van Assche 	seq_printf(m, "%p {.op=", rq);
3648658dca8SBart Van Assche 	if (op < ARRAY_SIZE(op_name) && op_name[op])
3658658dca8SBart Van Assche 		seq_printf(m, "%s", op_name[op]);
3668658dca8SBart Van Assche 	else
3678658dca8SBart Van Assche 		seq_printf(m, "%d", op);
3688658dca8SBart Van Assche 	seq_puts(m, ", .cmd_flags=");
3698658dca8SBart Van Assche 	blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
3708658dca8SBart Van Assche 		       ARRAY_SIZE(cmd_flag_name));
3718658dca8SBart Van Assche 	seq_puts(m, ", .rq_flags=");
3728658dca8SBart Van Assche 	blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
3738658dca8SBart Van Assche 		       ARRAY_SIZE(rqf_name));
374ec6dcf63SBart Van Assche 	seq_printf(m, ", .state=%s", blk_mq_rq_state_name(blk_mq_rq_state(rq)));
3752836ee4bSBart Van Assche 	seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
3768658dca8SBart Van Assche 		   rq->internal_tag);
3772836ee4bSBart Van Assche 	if (mq_ops->show_rq)
3782836ee4bSBart Van Assche 		mq_ops->show_rq(m, rq);
3792836ee4bSBart Van Assche 	seq_puts(m, "}\n");
380950cd7e9SOmar Sandoval 	return 0;
381950cd7e9SOmar Sandoval }
382daaadb3eSOmar Sandoval EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
383daaadb3eSOmar Sandoval 
384daaadb3eSOmar Sandoval int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
385daaadb3eSOmar Sandoval {
386daaadb3eSOmar Sandoval 	return __blk_mq_debugfs_rq_show(m, list_entry_rq(v));
387daaadb3eSOmar Sandoval }
38816b738f6SOmar Sandoval EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
389950cd7e9SOmar Sandoval 
390950cd7e9SOmar Sandoval static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
391f3bcb0e6SBart Van Assche 	__acquires(&hctx->lock)
392950cd7e9SOmar Sandoval {
393950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
394950cd7e9SOmar Sandoval 
395950cd7e9SOmar Sandoval 	spin_lock(&hctx->lock);
396950cd7e9SOmar Sandoval 	return seq_list_start(&hctx->dispatch, *pos);
397950cd7e9SOmar Sandoval }
398950cd7e9SOmar Sandoval 
399950cd7e9SOmar Sandoval static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
400950cd7e9SOmar Sandoval {
401950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
402950cd7e9SOmar Sandoval 
403950cd7e9SOmar Sandoval 	return seq_list_next(v, &hctx->dispatch, pos);
404950cd7e9SOmar Sandoval }
405950cd7e9SOmar Sandoval 
406950cd7e9SOmar Sandoval static void hctx_dispatch_stop(struct seq_file *m, void *v)
407f3bcb0e6SBart Van Assche 	__releases(&hctx->lock)
408950cd7e9SOmar Sandoval {
409950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
410950cd7e9SOmar Sandoval 
411950cd7e9SOmar Sandoval 	spin_unlock(&hctx->lock);
412950cd7e9SOmar Sandoval }
413950cd7e9SOmar Sandoval 
414950cd7e9SOmar Sandoval static const struct seq_operations hctx_dispatch_seq_ops = {
415950cd7e9SOmar Sandoval 	.start	= hctx_dispatch_start,
416950cd7e9SOmar Sandoval 	.next	= hctx_dispatch_next,
417950cd7e9SOmar Sandoval 	.stop	= hctx_dispatch_stop,
418950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
419950cd7e9SOmar Sandoval };
420950cd7e9SOmar Sandoval 
4212720bab5SBart Van Assche struct show_busy_params {
4222720bab5SBart Van Assche 	struct seq_file		*m;
4232720bab5SBart Van Assche 	struct blk_mq_hw_ctx	*hctx;
4242720bab5SBart Van Assche };
4252720bab5SBart Van Assche 
4262720bab5SBart Van Assche /*
4272720bab5SBart Van Assche  * Note: the state of a request may change while this function is in progress,
4282720bab5SBart Van Assche  * e.g. due to a concurrent blk_mq_finish_request() call.
4292720bab5SBart Van Assche  */
4302720bab5SBart Van Assche static void hctx_show_busy_rq(struct request *rq, void *data, bool reserved)
4312720bab5SBart Van Assche {
4322720bab5SBart Van Assche 	const struct show_busy_params *params = data;
4332720bab5SBart Van Assche 
4342720bab5SBart Van Assche 	if (blk_mq_map_queue(rq->q, rq->mq_ctx->cpu) == params->hctx &&
4355a61c363STejun Heo 	    blk_mq_rq_state(rq) != MQ_RQ_IDLE)
4362720bab5SBart Van Assche 		__blk_mq_debugfs_rq_show(params->m,
4372720bab5SBart Van Assche 					 list_entry_rq(&rq->queuelist));
4382720bab5SBart Van Assche }
4392720bab5SBart Van Assche 
4402720bab5SBart Van Assche static int hctx_busy_show(void *data, struct seq_file *m)
4412720bab5SBart Van Assche {
4422720bab5SBart Van Assche 	struct blk_mq_hw_ctx *hctx = data;
4432720bab5SBart Van Assche 	struct show_busy_params params = { .m = m, .hctx = hctx };
4442720bab5SBart Van Assche 
4452720bab5SBart Van Assche 	blk_mq_tagset_busy_iter(hctx->queue->tag_set, hctx_show_busy_rq,
4462720bab5SBart Van Assche 				&params);
4472720bab5SBart Van Assche 
4482720bab5SBart Van Assche 	return 0;
4492720bab5SBart Van Assche }
4502720bab5SBart Van Assche 
451f57de23aSOmar Sandoval static int hctx_ctx_map_show(void *data, struct seq_file *m)
452950cd7e9SOmar Sandoval {
453f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
4540bfa5288SOmar Sandoval 
4550bfa5288SOmar Sandoval 	sbitmap_bitmap_show(&hctx->ctx_map, m);
4560bfa5288SOmar Sandoval 	return 0;
4570bfa5288SOmar Sandoval }
4580bfa5288SOmar Sandoval 
459d96b37c0SOmar Sandoval static void blk_mq_debugfs_tags_show(struct seq_file *m,
460d96b37c0SOmar Sandoval 				     struct blk_mq_tags *tags)
461d96b37c0SOmar Sandoval {
462d96b37c0SOmar Sandoval 	seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
463d96b37c0SOmar Sandoval 	seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
464d96b37c0SOmar Sandoval 	seq_printf(m, "active_queues=%d\n",
465d96b37c0SOmar Sandoval 		   atomic_read(&tags->active_queues));
466d96b37c0SOmar Sandoval 
467d96b37c0SOmar Sandoval 	seq_puts(m, "\nbitmap_tags:\n");
468d96b37c0SOmar Sandoval 	sbitmap_queue_show(&tags->bitmap_tags, m);
469d96b37c0SOmar Sandoval 
470d96b37c0SOmar Sandoval 	if (tags->nr_reserved_tags) {
471d96b37c0SOmar Sandoval 		seq_puts(m, "\nbreserved_tags:\n");
472d96b37c0SOmar Sandoval 		sbitmap_queue_show(&tags->breserved_tags, m);
473d96b37c0SOmar Sandoval 	}
474d96b37c0SOmar Sandoval }
475d96b37c0SOmar Sandoval 
476f57de23aSOmar Sandoval static int hctx_tags_show(void *data, struct seq_file *m)
477d96b37c0SOmar Sandoval {
478f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
479d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
4808c0f14eaSBart Van Assche 	int res;
481d96b37c0SOmar Sandoval 
4828c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
4838c0f14eaSBart Van Assche 	if (res)
4848c0f14eaSBart Van Assche 		goto out;
485d96b37c0SOmar Sandoval 	if (hctx->tags)
486d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->tags);
487d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
488d96b37c0SOmar Sandoval 
4898c0f14eaSBart Van Assche out:
4908c0f14eaSBart Van Assche 	return res;
491d96b37c0SOmar Sandoval }
492d96b37c0SOmar Sandoval 
493f57de23aSOmar Sandoval static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
494d96b37c0SOmar Sandoval {
495f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
496d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
4978c0f14eaSBart Van Assche 	int res;
498d7e3621aSOmar Sandoval 
4998c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5008c0f14eaSBart Van Assche 	if (res)
5018c0f14eaSBart Van Assche 		goto out;
502d7e3621aSOmar Sandoval 	if (hctx->tags)
503d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
504d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
5058c0f14eaSBart Van Assche 
5068c0f14eaSBart Van Assche out:
5078c0f14eaSBart Van Assche 	return res;
508d7e3621aSOmar Sandoval }
509d7e3621aSOmar Sandoval 
510f57de23aSOmar Sandoval static int hctx_sched_tags_show(void *data, struct seq_file *m)
511d7e3621aSOmar Sandoval {
512f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
513d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
5148c0f14eaSBart Van Assche 	int res;
515d96b37c0SOmar Sandoval 
5168c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5178c0f14eaSBart Van Assche 	if (res)
5188c0f14eaSBart Van Assche 		goto out;
519d96b37c0SOmar Sandoval 	if (hctx->sched_tags)
520d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->sched_tags);
521d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
522d96b37c0SOmar Sandoval 
5238c0f14eaSBart Van Assche out:
5248c0f14eaSBart Van Assche 	return res;
525d96b37c0SOmar Sandoval }
526d96b37c0SOmar Sandoval 
527f57de23aSOmar Sandoval static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
528d96b37c0SOmar Sandoval {
529f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
530d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
5318c0f14eaSBart Van Assche 	int res;
532d7e3621aSOmar Sandoval 
5338c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5348c0f14eaSBart Van Assche 	if (res)
5358c0f14eaSBart Van Assche 		goto out;
536d7e3621aSOmar Sandoval 	if (hctx->sched_tags)
537d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
538d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
5398c0f14eaSBart Van Assche 
5408c0f14eaSBart Van Assche out:
5418c0f14eaSBart Van Assche 	return res;
542d7e3621aSOmar Sandoval }
543d7e3621aSOmar Sandoval 
544f57de23aSOmar Sandoval static int hctx_io_poll_show(void *data, struct seq_file *m)
545d7e3621aSOmar Sandoval {
546f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
547be215473SOmar Sandoval 
548be215473SOmar Sandoval 	seq_printf(m, "considered=%lu\n", hctx->poll_considered);
549be215473SOmar Sandoval 	seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
550be215473SOmar Sandoval 	seq_printf(m, "success=%lu\n", hctx->poll_success);
551be215473SOmar Sandoval 	return 0;
552be215473SOmar Sandoval }
553be215473SOmar Sandoval 
554f57de23aSOmar Sandoval static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
555be215473SOmar Sandoval 				  size_t count, loff_t *ppos)
556be215473SOmar Sandoval {
557f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
558be215473SOmar Sandoval 
559be215473SOmar Sandoval 	hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
560be215473SOmar Sandoval 	return count;
561be215473SOmar Sandoval }
562be215473SOmar Sandoval 
563f57de23aSOmar Sandoval static int hctx_dispatched_show(void *data, struct seq_file *m)
564be215473SOmar Sandoval {
565f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
566be215473SOmar Sandoval 	int i;
567be215473SOmar Sandoval 
568be215473SOmar Sandoval 	seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
569be215473SOmar Sandoval 
570be215473SOmar Sandoval 	for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
571be215473SOmar Sandoval 		unsigned int d = 1U << (i - 1);
572be215473SOmar Sandoval 
573be215473SOmar Sandoval 		seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
574be215473SOmar Sandoval 	}
575be215473SOmar Sandoval 
576be215473SOmar Sandoval 	seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
577be215473SOmar Sandoval 	return 0;
578be215473SOmar Sandoval }
579be215473SOmar Sandoval 
580f57de23aSOmar Sandoval static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
581be215473SOmar Sandoval 				     size_t count, loff_t *ppos)
582be215473SOmar Sandoval {
583f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
584be215473SOmar Sandoval 	int i;
585be215473SOmar Sandoval 
586be215473SOmar Sandoval 	for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
587be215473SOmar Sandoval 		hctx->dispatched[i] = 0;
588be215473SOmar Sandoval 	return count;
589be215473SOmar Sandoval }
590be215473SOmar Sandoval 
591f57de23aSOmar Sandoval static int hctx_queued_show(void *data, struct seq_file *m)
5924a46f05eSOmar Sandoval {
593f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
5944a46f05eSOmar Sandoval 
5954a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->queued);
5964a46f05eSOmar Sandoval 	return 0;
5974a46f05eSOmar Sandoval }
5984a46f05eSOmar Sandoval 
599f57de23aSOmar Sandoval static ssize_t hctx_queued_write(void *data, const char __user *buf,
6004a46f05eSOmar Sandoval 				 size_t count, loff_t *ppos)
6014a46f05eSOmar Sandoval {
602f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6034a46f05eSOmar Sandoval 
6044a46f05eSOmar Sandoval 	hctx->queued = 0;
6054a46f05eSOmar Sandoval 	return count;
6064a46f05eSOmar Sandoval }
6074a46f05eSOmar Sandoval 
608f57de23aSOmar Sandoval static int hctx_run_show(void *data, struct seq_file *m)
6094a46f05eSOmar Sandoval {
610f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6114a46f05eSOmar Sandoval 
6124a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->run);
6134a46f05eSOmar Sandoval 	return 0;
6144a46f05eSOmar Sandoval }
6154a46f05eSOmar Sandoval 
616f57de23aSOmar Sandoval static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
617f57de23aSOmar Sandoval 			      loff_t *ppos)
6184a46f05eSOmar Sandoval {
619f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6204a46f05eSOmar Sandoval 
6214a46f05eSOmar Sandoval 	hctx->run = 0;
6224a46f05eSOmar Sandoval 	return count;
6234a46f05eSOmar Sandoval }
6244a46f05eSOmar Sandoval 
625f57de23aSOmar Sandoval static int hctx_active_show(void *data, struct seq_file *m)
6264a46f05eSOmar Sandoval {
627f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6284a46f05eSOmar Sandoval 
6294a46f05eSOmar Sandoval 	seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
6304a46f05eSOmar Sandoval 	return 0;
6314a46f05eSOmar Sandoval }
6324a46f05eSOmar Sandoval 
6336e768717SMing Lei static int hctx_dispatch_busy_show(void *data, struct seq_file *m)
6346e768717SMing Lei {
6356e768717SMing Lei 	struct blk_mq_hw_ctx *hctx = data;
6366e768717SMing Lei 
6376e768717SMing Lei 	seq_printf(m, "%u\n", hctx->dispatch_busy);
6386e768717SMing Lei 	return 0;
6396e768717SMing Lei }
6406e768717SMing Lei 
641950cd7e9SOmar Sandoval static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
642f3bcb0e6SBart Van Assche 	__acquires(&ctx->lock)
643950cd7e9SOmar Sandoval {
644950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
645950cd7e9SOmar Sandoval 
646950cd7e9SOmar Sandoval 	spin_lock(&ctx->lock);
647950cd7e9SOmar Sandoval 	return seq_list_start(&ctx->rq_list, *pos);
648950cd7e9SOmar Sandoval }
649950cd7e9SOmar Sandoval 
650950cd7e9SOmar Sandoval static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
651950cd7e9SOmar Sandoval {
652950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
653950cd7e9SOmar Sandoval 
654950cd7e9SOmar Sandoval 	return seq_list_next(v, &ctx->rq_list, pos);
655950cd7e9SOmar Sandoval }
656950cd7e9SOmar Sandoval 
657950cd7e9SOmar Sandoval static void ctx_rq_list_stop(struct seq_file *m, void *v)
658f3bcb0e6SBart Van Assche 	__releases(&ctx->lock)
659950cd7e9SOmar Sandoval {
660950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
661950cd7e9SOmar Sandoval 
662950cd7e9SOmar Sandoval 	spin_unlock(&ctx->lock);
663950cd7e9SOmar Sandoval }
664950cd7e9SOmar Sandoval 
665950cd7e9SOmar Sandoval static const struct seq_operations ctx_rq_list_seq_ops = {
666950cd7e9SOmar Sandoval 	.start	= ctx_rq_list_start,
667950cd7e9SOmar Sandoval 	.next	= ctx_rq_list_next,
668950cd7e9SOmar Sandoval 	.stop	= ctx_rq_list_stop,
669950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
670950cd7e9SOmar Sandoval };
671f57de23aSOmar Sandoval static int ctx_dispatched_show(void *data, struct seq_file *m)
672950cd7e9SOmar Sandoval {
673f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6744a46f05eSOmar Sandoval 
6754a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
6764a46f05eSOmar Sandoval 	return 0;
6774a46f05eSOmar Sandoval }
6784a46f05eSOmar Sandoval 
679f57de23aSOmar Sandoval static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
6804a46f05eSOmar Sandoval 				    size_t count, loff_t *ppos)
6814a46f05eSOmar Sandoval {
682f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6834a46f05eSOmar Sandoval 
6844a46f05eSOmar Sandoval 	ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
6854a46f05eSOmar Sandoval 	return count;
6864a46f05eSOmar Sandoval }
6874a46f05eSOmar Sandoval 
688f57de23aSOmar Sandoval static int ctx_merged_show(void *data, struct seq_file *m)
6894a46f05eSOmar Sandoval {
690f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6914a46f05eSOmar Sandoval 
6924a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", ctx->rq_merged);
6934a46f05eSOmar Sandoval 	return 0;
6944a46f05eSOmar Sandoval }
6954a46f05eSOmar Sandoval 
696f57de23aSOmar Sandoval static ssize_t ctx_merged_write(void *data, const char __user *buf,
6974a46f05eSOmar Sandoval 				size_t count, loff_t *ppos)
6984a46f05eSOmar Sandoval {
699f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7004a46f05eSOmar Sandoval 
7014a46f05eSOmar Sandoval 	ctx->rq_merged = 0;
7024a46f05eSOmar Sandoval 	return count;
7034a46f05eSOmar Sandoval }
7044a46f05eSOmar Sandoval 
705f57de23aSOmar Sandoval static int ctx_completed_show(void *data, struct seq_file *m)
7064a46f05eSOmar Sandoval {
707f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7084a46f05eSOmar Sandoval 
7094a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
7104a46f05eSOmar Sandoval 	return 0;
7114a46f05eSOmar Sandoval }
7124a46f05eSOmar Sandoval 
713f57de23aSOmar Sandoval static ssize_t ctx_completed_write(void *data, const char __user *buf,
7144a46f05eSOmar Sandoval 				   size_t count, loff_t *ppos)
7154a46f05eSOmar Sandoval {
716f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7174a46f05eSOmar Sandoval 
7184a46f05eSOmar Sandoval 	ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
7194a46f05eSOmar Sandoval 	return count;
7204a46f05eSOmar Sandoval }
7214a46f05eSOmar Sandoval 
722f57de23aSOmar Sandoval static int blk_mq_debugfs_show(struct seq_file *m, void *v)
723f57de23aSOmar Sandoval {
724f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = m->private;
725f57de23aSOmar Sandoval 	void *data = d_inode(m->file->f_path.dentry->d_parent)->i_private;
726f57de23aSOmar Sandoval 
727f57de23aSOmar Sandoval 	return attr->show(data, m);
728f57de23aSOmar Sandoval }
729f57de23aSOmar Sandoval 
730f57de23aSOmar Sandoval static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
731f57de23aSOmar Sandoval 				    size_t count, loff_t *ppos)
732f57de23aSOmar Sandoval {
733f57de23aSOmar Sandoval 	struct seq_file *m = file->private_data;
734f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = m->private;
735f57de23aSOmar Sandoval 	void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
736f57de23aSOmar Sandoval 
7376b136a24SEryu Guan 	/*
7386b136a24SEryu Guan 	 * Attributes that only implement .seq_ops are read-only and 'attr' is
7396b136a24SEryu Guan 	 * the same with 'data' in this case.
7406b136a24SEryu Guan 	 */
7416b136a24SEryu Guan 	if (attr == data || !attr->write)
742f57de23aSOmar Sandoval 		return -EPERM;
743f57de23aSOmar Sandoval 
744f57de23aSOmar Sandoval 	return attr->write(data, buf, count, ppos);
745f57de23aSOmar Sandoval }
746f57de23aSOmar Sandoval 
747f57de23aSOmar Sandoval static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
748f57de23aSOmar Sandoval {
749f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
750f57de23aSOmar Sandoval 	void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
751f57de23aSOmar Sandoval 	struct seq_file *m;
752f57de23aSOmar Sandoval 	int ret;
753f57de23aSOmar Sandoval 
754f57de23aSOmar Sandoval 	if (attr->seq_ops) {
755f57de23aSOmar Sandoval 		ret = seq_open(file, attr->seq_ops);
756f57de23aSOmar Sandoval 		if (!ret) {
757f57de23aSOmar Sandoval 			m = file->private_data;
758f57de23aSOmar Sandoval 			m->private = data;
759f57de23aSOmar Sandoval 		}
760f57de23aSOmar Sandoval 		return ret;
761f57de23aSOmar Sandoval 	}
762f57de23aSOmar Sandoval 
763f57de23aSOmar Sandoval 	if (WARN_ON_ONCE(!attr->show))
764f57de23aSOmar Sandoval 		return -EPERM;
765f57de23aSOmar Sandoval 
766f57de23aSOmar Sandoval 	return single_open(file, blk_mq_debugfs_show, inode->i_private);
767f57de23aSOmar Sandoval }
768f57de23aSOmar Sandoval 
769f57de23aSOmar Sandoval static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
770f57de23aSOmar Sandoval {
771f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
772f57de23aSOmar Sandoval 
773f57de23aSOmar Sandoval 	if (attr->show)
774f57de23aSOmar Sandoval 		return single_release(inode, file);
775f57de23aSOmar Sandoval 	else
776f57de23aSOmar Sandoval 		return seq_release(inode, file);
777f57de23aSOmar Sandoval }
778f57de23aSOmar Sandoval 
779f8465933SBart Van Assche static const struct file_operations blk_mq_debugfs_fops = {
780f57de23aSOmar Sandoval 	.open		= blk_mq_debugfs_open,
7814a46f05eSOmar Sandoval 	.read		= seq_read,
782f57de23aSOmar Sandoval 	.write		= blk_mq_debugfs_write,
7834a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
784f57de23aSOmar Sandoval 	.release	= blk_mq_debugfs_release,
7854a46f05eSOmar Sandoval };
7864a46f05eSOmar Sandoval 
78707e4feadSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
788f57de23aSOmar Sandoval 	{"state", 0400, hctx_state_show},
789f57de23aSOmar Sandoval 	{"flags", 0400, hctx_flags_show},
790f57de23aSOmar Sandoval 	{"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
7912720bab5SBart Van Assche 	{"busy", 0400, hctx_busy_show},
792f57de23aSOmar Sandoval 	{"ctx_map", 0400, hctx_ctx_map_show},
793f57de23aSOmar Sandoval 	{"tags", 0400, hctx_tags_show},
794f57de23aSOmar Sandoval 	{"tags_bitmap", 0400, hctx_tags_bitmap_show},
795f57de23aSOmar Sandoval 	{"sched_tags", 0400, hctx_sched_tags_show},
796f57de23aSOmar Sandoval 	{"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
797f57de23aSOmar Sandoval 	{"io_poll", 0600, hctx_io_poll_show, hctx_io_poll_write},
798f57de23aSOmar Sandoval 	{"dispatched", 0600, hctx_dispatched_show, hctx_dispatched_write},
799f57de23aSOmar Sandoval 	{"queued", 0600, hctx_queued_show, hctx_queued_write},
800f57de23aSOmar Sandoval 	{"run", 0600, hctx_run_show, hctx_run_write},
801f57de23aSOmar Sandoval 	{"active", 0400, hctx_active_show},
8026e768717SMing Lei 	{"dispatch_busy", 0400, hctx_dispatch_busy_show},
80372f2f8f6SBart Van Assche 	{},
80407e4feadSOmar Sandoval };
80507e4feadSOmar Sandoval 
80607e4feadSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
807f57de23aSOmar Sandoval 	{"rq_list", 0400, .seq_ops = &ctx_rq_list_seq_ops},
808f57de23aSOmar Sandoval 	{"dispatched", 0600, ctx_dispatched_show, ctx_dispatched_write},
809f57de23aSOmar Sandoval 	{"merged", 0600, ctx_merged_show, ctx_merged_write},
810f57de23aSOmar Sandoval 	{"completed", 0600, ctx_completed_show, ctx_completed_write},
81172f2f8f6SBart Van Assche 	{},
81207e4feadSOmar Sandoval };
81307e4feadSOmar Sandoval 
81472f2f8f6SBart Van Assche static bool debugfs_create_files(struct dentry *parent, void *data,
81572f2f8f6SBart Van Assche 				 const struct blk_mq_debugfs_attr *attr)
81672f2f8f6SBart Van Assche {
817f57de23aSOmar Sandoval 	d_inode(parent)->i_private = data;
818f57de23aSOmar Sandoval 
81972f2f8f6SBart Van Assche 	for (; attr->name; attr++) {
82072f2f8f6SBart Van Assche 		if (!debugfs_create_file(attr->name, attr->mode, parent,
821f57de23aSOmar Sandoval 					 (void *)attr, &blk_mq_debugfs_fops))
82272f2f8f6SBart Van Assche 			return false;
82372f2f8f6SBart Van Assche 	}
82472f2f8f6SBart Van Assche 	return true;
82572f2f8f6SBart Van Assche }
82672f2f8f6SBart Van Assche 
8279c1051aaSOmar Sandoval int blk_mq_debugfs_register(struct request_queue *q)
8289c1051aaSOmar Sandoval {
8299c1051aaSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
8309c1051aaSOmar Sandoval 	int i;
8319c1051aaSOmar Sandoval 
8329c1051aaSOmar Sandoval 	if (!blk_debugfs_root)
8339c1051aaSOmar Sandoval 		return -ENOENT;
8349c1051aaSOmar Sandoval 
8359c1051aaSOmar Sandoval 	q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
8369c1051aaSOmar Sandoval 					    blk_debugfs_root);
8379c1051aaSOmar Sandoval 	if (!q->debugfs_dir)
8389c1051aaSOmar Sandoval 		return -ENOMEM;
8399c1051aaSOmar Sandoval 
8409c1051aaSOmar Sandoval 	if (!debugfs_create_files(q->debugfs_dir, q,
8419c1051aaSOmar Sandoval 				  blk_mq_debugfs_queue_attrs))
8429c1051aaSOmar Sandoval 		goto err;
8439c1051aaSOmar Sandoval 
8449c1051aaSOmar Sandoval 	/*
84570e62f4bSOmar Sandoval 	 * blk_mq_init_sched() attempted to do this already, but q->debugfs_dir
8469c1051aaSOmar Sandoval 	 * didn't exist yet (because we don't know what to name the directory
8479c1051aaSOmar Sandoval 	 * until the queue is registered to a gendisk).
8489c1051aaSOmar Sandoval 	 */
84970e62f4bSOmar Sandoval 	if (q->elevator && !q->sched_debugfs_dir)
85070e62f4bSOmar Sandoval 		blk_mq_debugfs_register_sched(q);
85170e62f4bSOmar Sandoval 
85270e62f4bSOmar Sandoval 	/* Similarly, blk_mq_init_hctx() couldn't do this previously. */
8539c1051aaSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
8549c1051aaSOmar Sandoval 		if (!hctx->debugfs_dir && blk_mq_debugfs_register_hctx(q, hctx))
8559c1051aaSOmar Sandoval 			goto err;
856d332ce09SOmar Sandoval 		if (q->elevator && !hctx->sched_debugfs_dir &&
857d332ce09SOmar Sandoval 		    blk_mq_debugfs_register_sched_hctx(q, hctx))
858d332ce09SOmar Sandoval 			goto err;
8599c1051aaSOmar Sandoval 	}
8609c1051aaSOmar Sandoval 
8619c1051aaSOmar Sandoval 	return 0;
8629c1051aaSOmar Sandoval 
8639c1051aaSOmar Sandoval err:
8649c1051aaSOmar Sandoval 	blk_mq_debugfs_unregister(q);
8659c1051aaSOmar Sandoval 	return -ENOMEM;
8669c1051aaSOmar Sandoval }
8679c1051aaSOmar Sandoval 
8689c1051aaSOmar Sandoval void blk_mq_debugfs_unregister(struct request_queue *q)
8699c1051aaSOmar Sandoval {
8709c1051aaSOmar Sandoval 	debugfs_remove_recursive(q->debugfs_dir);
871d332ce09SOmar Sandoval 	q->sched_debugfs_dir = NULL;
8729c1051aaSOmar Sandoval 	q->debugfs_dir = NULL;
8739c1051aaSOmar Sandoval }
8749c1051aaSOmar Sandoval 
8759c1051aaSOmar Sandoval static int blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,
8769c1051aaSOmar Sandoval 				       struct blk_mq_ctx *ctx)
87707e4feadSOmar Sandoval {
87807e4feadSOmar Sandoval 	struct dentry *ctx_dir;
87907e4feadSOmar Sandoval 	char name[20];
88007e4feadSOmar Sandoval 
88107e4feadSOmar Sandoval 	snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
8829c1051aaSOmar Sandoval 	ctx_dir = debugfs_create_dir(name, hctx->debugfs_dir);
88307e4feadSOmar Sandoval 	if (!ctx_dir)
88407e4feadSOmar Sandoval 		return -ENOMEM;
88507e4feadSOmar Sandoval 
88672f2f8f6SBart Van Assche 	if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
88707e4feadSOmar Sandoval 		return -ENOMEM;
88807e4feadSOmar Sandoval 
88907e4feadSOmar Sandoval 	return 0;
89007e4feadSOmar Sandoval }
89107e4feadSOmar Sandoval 
8929c1051aaSOmar Sandoval int blk_mq_debugfs_register_hctx(struct request_queue *q,
89307e4feadSOmar Sandoval 				 struct blk_mq_hw_ctx *hctx)
89407e4feadSOmar Sandoval {
89507e4feadSOmar Sandoval 	struct blk_mq_ctx *ctx;
89607e4feadSOmar Sandoval 	char name[20];
89707e4feadSOmar Sandoval 	int i;
89807e4feadSOmar Sandoval 
89907e4feadSOmar Sandoval 	if (!q->debugfs_dir)
90007e4feadSOmar Sandoval 		return -ENOENT;
90107e4feadSOmar Sandoval 
9029c1051aaSOmar Sandoval 	snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
9039c1051aaSOmar Sandoval 	hctx->debugfs_dir = debugfs_create_dir(name, q->debugfs_dir);
9049c1051aaSOmar Sandoval 	if (!hctx->debugfs_dir)
9059c1051aaSOmar Sandoval 		return -ENOMEM;
9069c1051aaSOmar Sandoval 
9079c1051aaSOmar Sandoval 	if (!debugfs_create_files(hctx->debugfs_dir, hctx,
9089c1051aaSOmar Sandoval 				  blk_mq_debugfs_hctx_attrs))
90907e4feadSOmar Sandoval 		goto err;
91007e4feadSOmar Sandoval 
9119c1051aaSOmar Sandoval 	hctx_for_each_ctx(hctx, ctx, i) {
9129c1051aaSOmar Sandoval 		if (blk_mq_debugfs_register_ctx(hctx, ctx))
91307e4feadSOmar Sandoval 			goto err;
91407e4feadSOmar Sandoval 	}
91507e4feadSOmar Sandoval 
91607e4feadSOmar Sandoval 	return 0;
91707e4feadSOmar Sandoval 
91807e4feadSOmar Sandoval err:
9199c1051aaSOmar Sandoval 	blk_mq_debugfs_unregister_hctx(hctx);
92007e4feadSOmar Sandoval 	return -ENOMEM;
92107e4feadSOmar Sandoval }
92207e4feadSOmar Sandoval 
9239c1051aaSOmar Sandoval void blk_mq_debugfs_unregister_hctx(struct blk_mq_hw_ctx *hctx)
92407e4feadSOmar Sandoval {
9259c1051aaSOmar Sandoval 	debugfs_remove_recursive(hctx->debugfs_dir);
926d332ce09SOmar Sandoval 	hctx->sched_debugfs_dir = NULL;
9279c1051aaSOmar Sandoval 	hctx->debugfs_dir = NULL;
9289c1051aaSOmar Sandoval }
9299c1051aaSOmar Sandoval 
9309c1051aaSOmar Sandoval int blk_mq_debugfs_register_hctxs(struct request_queue *q)
9319c1051aaSOmar Sandoval {
9329c1051aaSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
9339c1051aaSOmar Sandoval 	int i;
9349c1051aaSOmar Sandoval 
9359c1051aaSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
9369c1051aaSOmar Sandoval 		if (blk_mq_debugfs_register_hctx(q, hctx))
9379c1051aaSOmar Sandoval 			return -ENOMEM;
9389c1051aaSOmar Sandoval 	}
9399c1051aaSOmar Sandoval 
9409c1051aaSOmar Sandoval 	return 0;
9419c1051aaSOmar Sandoval }
9429c1051aaSOmar Sandoval 
9439c1051aaSOmar Sandoval void blk_mq_debugfs_unregister_hctxs(struct request_queue *q)
9449c1051aaSOmar Sandoval {
9459c1051aaSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
9469c1051aaSOmar Sandoval 	int i;
9479c1051aaSOmar Sandoval 
9489c1051aaSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i)
9499c1051aaSOmar Sandoval 		blk_mq_debugfs_unregister_hctx(hctx);
95007e4feadSOmar Sandoval }
951d332ce09SOmar Sandoval 
952d332ce09SOmar Sandoval int blk_mq_debugfs_register_sched(struct request_queue *q)
953d332ce09SOmar Sandoval {
954d332ce09SOmar Sandoval 	struct elevator_type *e = q->elevator->type;
955d332ce09SOmar Sandoval 
956d332ce09SOmar Sandoval 	if (!q->debugfs_dir)
957d332ce09SOmar Sandoval 		return -ENOENT;
958d332ce09SOmar Sandoval 
959d332ce09SOmar Sandoval 	if (!e->queue_debugfs_attrs)
960d332ce09SOmar Sandoval 		return 0;
961d332ce09SOmar Sandoval 
962d332ce09SOmar Sandoval 	q->sched_debugfs_dir = debugfs_create_dir("sched", q->debugfs_dir);
963d332ce09SOmar Sandoval 	if (!q->sched_debugfs_dir)
964d332ce09SOmar Sandoval 		return -ENOMEM;
965d332ce09SOmar Sandoval 
966d332ce09SOmar Sandoval 	if (!debugfs_create_files(q->sched_debugfs_dir, q,
967d332ce09SOmar Sandoval 				  e->queue_debugfs_attrs))
968d332ce09SOmar Sandoval 		goto err;
969d332ce09SOmar Sandoval 
970d332ce09SOmar Sandoval 	return 0;
971d332ce09SOmar Sandoval 
972d332ce09SOmar Sandoval err:
973d332ce09SOmar Sandoval 	blk_mq_debugfs_unregister_sched(q);
974d332ce09SOmar Sandoval 	return -ENOMEM;
975d332ce09SOmar Sandoval }
976d332ce09SOmar Sandoval 
977d332ce09SOmar Sandoval void blk_mq_debugfs_unregister_sched(struct request_queue *q)
978d332ce09SOmar Sandoval {
979d332ce09SOmar Sandoval 	debugfs_remove_recursive(q->sched_debugfs_dir);
980d332ce09SOmar Sandoval 	q->sched_debugfs_dir = NULL;
981d332ce09SOmar Sandoval }
982d332ce09SOmar Sandoval 
983d332ce09SOmar Sandoval int blk_mq_debugfs_register_sched_hctx(struct request_queue *q,
984d332ce09SOmar Sandoval 				       struct blk_mq_hw_ctx *hctx)
985d332ce09SOmar Sandoval {
986d332ce09SOmar Sandoval 	struct elevator_type *e = q->elevator->type;
987d332ce09SOmar Sandoval 
988d332ce09SOmar Sandoval 	if (!hctx->debugfs_dir)
989d332ce09SOmar Sandoval 		return -ENOENT;
990d332ce09SOmar Sandoval 
991d332ce09SOmar Sandoval 	if (!e->hctx_debugfs_attrs)
992d332ce09SOmar Sandoval 		return 0;
993d332ce09SOmar Sandoval 
994d332ce09SOmar Sandoval 	hctx->sched_debugfs_dir = debugfs_create_dir("sched",
995d332ce09SOmar Sandoval 						     hctx->debugfs_dir);
996d332ce09SOmar Sandoval 	if (!hctx->sched_debugfs_dir)
997d332ce09SOmar Sandoval 		return -ENOMEM;
998d332ce09SOmar Sandoval 
999d332ce09SOmar Sandoval 	if (!debugfs_create_files(hctx->sched_debugfs_dir, hctx,
1000d332ce09SOmar Sandoval 				  e->hctx_debugfs_attrs))
1001d332ce09SOmar Sandoval 		return -ENOMEM;
1002d332ce09SOmar Sandoval 
1003d332ce09SOmar Sandoval 	return 0;
1004d332ce09SOmar Sandoval }
1005d332ce09SOmar Sandoval 
1006d332ce09SOmar Sandoval void blk_mq_debugfs_unregister_sched_hctx(struct blk_mq_hw_ctx *hctx)
1007d332ce09SOmar Sandoval {
1008d332ce09SOmar Sandoval 	debugfs_remove_recursive(hctx->sched_debugfs_dir);
1009d332ce09SOmar Sandoval 	hctx->sched_debugfs_dir = NULL;
1010d332ce09SOmar Sandoval }
1011