xref: /linux/block/blk-mq-debugfs.c (revision ec6dcf63c55c8b09b44fe5990082b07baf139c69)
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 
1051a435111SOmar Sandoval #define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
10691d68905SBart Van Assche static const char *const blk_queue_flag_name[] = {
1071a435111SOmar Sandoval 	QUEUE_FLAG_NAME(QUEUED),
1081a435111SOmar Sandoval 	QUEUE_FLAG_NAME(STOPPED),
1091a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DYING),
1101a435111SOmar Sandoval 	QUEUE_FLAG_NAME(BYPASS),
1111a435111SOmar Sandoval 	QUEUE_FLAG_NAME(BIDI),
1121a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NOMERGES),
1131a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SAME_COMP),
1141a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FAIL_IO),
1151a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NONROT),
1161a435111SOmar Sandoval 	QUEUE_FLAG_NAME(IO_STAT),
1171a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DISCARD),
1181a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NOXMERGES),
1191a435111SOmar Sandoval 	QUEUE_FLAG_NAME(ADD_RANDOM),
1201a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SECERASE),
1211a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SAME_FORCE),
1221a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DEAD),
1231a435111SOmar Sandoval 	QUEUE_FLAG_NAME(INIT_DONE),
1241a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NO_SG_MERGE),
1251a435111SOmar Sandoval 	QUEUE_FLAG_NAME(POLL),
1261a435111SOmar Sandoval 	QUEUE_FLAG_NAME(WC),
1271a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FUA),
1281a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FLUSH_NQ),
1291a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DAX),
1301a435111SOmar Sandoval 	QUEUE_FLAG_NAME(STATS),
1311a435111SOmar Sandoval 	QUEUE_FLAG_NAME(POLL_STATS),
1321a435111SOmar Sandoval 	QUEUE_FLAG_NAME(REGISTERED),
13322d53821SBart Van Assche 	QUEUE_FLAG_NAME(SCSI_PASSTHROUGH),
13422d53821SBart Van Assche 	QUEUE_FLAG_NAME(QUIESCED),
135c9254f2dSBart Van Assche 	QUEUE_FLAG_NAME(PREEMPT_ONLY),
13691d68905SBart Van Assche };
1371a435111SOmar Sandoval #undef QUEUE_FLAG_NAME
13891d68905SBart Van Assche 
139f57de23aSOmar Sandoval static int queue_state_show(void *data, struct seq_file *m)
14091d68905SBart Van Assche {
141f57de23aSOmar Sandoval 	struct request_queue *q = data;
14291d68905SBart Van Assche 
14391d68905SBart Van Assche 	blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
14491d68905SBart Van Assche 		       ARRAY_SIZE(blk_queue_flag_name));
145fd07dc81SBart Van Assche 	seq_puts(m, "\n");
14691d68905SBart Van Assche 	return 0;
14791d68905SBart Van Assche }
14891d68905SBart Van Assche 
149f57de23aSOmar Sandoval static ssize_t queue_state_write(void *data, const char __user *buf,
150c7e4145aSOmar Sandoval 				 size_t count, loff_t *ppos)
15191d68905SBart Van Assche {
152f57de23aSOmar Sandoval 	struct request_queue *q = data;
15371b90511SOmar Sandoval 	char opbuf[16] = { }, *op;
15491d68905SBart Van Assche 
15518d4d7d0SBart Van Assche 	/*
15618d4d7d0SBart Van Assche 	 * The "state" attribute is removed after blk_cleanup_queue() has called
15718d4d7d0SBart Van Assche 	 * blk_mq_free_queue(). Return if QUEUE_FLAG_DEAD has been set to avoid
15818d4d7d0SBart Van Assche 	 * triggering a use-after-free.
15918d4d7d0SBart Van Assche 	 */
16018d4d7d0SBart Van Assche 	if (blk_queue_dead(q))
16118d4d7d0SBart Van Assche 		return -ENOENT;
16218d4d7d0SBart Van Assche 
16371b90511SOmar Sandoval 	if (count >= sizeof(opbuf)) {
164c7e4145aSOmar Sandoval 		pr_err("%s: operation too long\n", __func__);
165c7e4145aSOmar Sandoval 		goto inval;
166c7e4145aSOmar Sandoval 	}
167c7e4145aSOmar Sandoval 
16871b90511SOmar Sandoval 	if (copy_from_user(opbuf, buf, count))
16991d68905SBart Van Assche 		return -EFAULT;
17071b90511SOmar Sandoval 	op = strstrip(opbuf);
17191d68905SBart Van Assche 	if (strcmp(op, "run") == 0) {
17291d68905SBart Van Assche 		blk_mq_run_hw_queues(q, true);
17391d68905SBart Van Assche 	} else if (strcmp(op, "start") == 0) {
17491d68905SBart Van Assche 		blk_mq_start_stopped_hw_queues(q, true);
175edea55abSBart Van Assche 	} else if (strcmp(op, "kick") == 0) {
176edea55abSBart Van Assche 		blk_mq_kick_requeue_list(q);
17791d68905SBart Van Assche 	} else {
178c7e4145aSOmar Sandoval 		pr_err("%s: unsupported operation '%s'\n", __func__, op);
179c7e4145aSOmar Sandoval inval:
180edea55abSBart Van Assche 		pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
18191d68905SBart Van Assche 		return -EINVAL;
18291d68905SBart Van Assche 	}
183c7e4145aSOmar Sandoval 	return count;
18491d68905SBart Van Assche }
18591d68905SBart Van Assche 
186f793dfd3SJens Axboe static int queue_write_hint_show(void *data, struct seq_file *m)
187f793dfd3SJens Axboe {
188f793dfd3SJens Axboe 	struct request_queue *q = data;
189f793dfd3SJens Axboe 	int i;
190f793dfd3SJens Axboe 
191f793dfd3SJens Axboe 	for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
192f793dfd3SJens Axboe 		seq_printf(m, "hint%d: %llu\n", i, q->write_hints[i]);
193f793dfd3SJens Axboe 
194f793dfd3SJens Axboe 	return 0;
195f793dfd3SJens Axboe }
196f793dfd3SJens Axboe 
197f793dfd3SJens Axboe static ssize_t queue_write_hint_store(void *data, const char __user *buf,
198f793dfd3SJens Axboe 				      size_t count, loff_t *ppos)
199f793dfd3SJens Axboe {
200f793dfd3SJens Axboe 	struct request_queue *q = data;
201f793dfd3SJens Axboe 	int i;
202f793dfd3SJens Axboe 
203f793dfd3SJens Axboe 	for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
204f793dfd3SJens Axboe 		q->write_hints[i] = 0;
205f793dfd3SJens Axboe 
206f793dfd3SJens Axboe 	return count;
207f793dfd3SJens Axboe }
208f793dfd3SJens Axboe 
20918bc4230SBart Van Assche static int queue_zone_wlock_show(void *data, struct seq_file *m)
21018bc4230SBart Van Assche {
21118bc4230SBart Van Assche 	struct request_queue *q = data;
21218bc4230SBart Van Assche 	unsigned int i;
21318bc4230SBart Van Assche 
21418bc4230SBart Van Assche 	if (!q->seq_zones_wlock)
21518bc4230SBart Van Assche 		return 0;
21618bc4230SBart Van Assche 
21718bc4230SBart Van Assche 	for (i = 0; i < blk_queue_nr_zones(q); i++)
21818bc4230SBart Van Assche 		if (test_bit(i, q->seq_zones_wlock))
21918bc4230SBart Van Assche 			seq_printf(m, "%u\n", i);
22018bc4230SBart Van Assche 
22118bc4230SBart Van Assche 	return 0;
22218bc4230SBart Van Assche }
22318bc4230SBart Van Assche 
2241209cb7fSBart Van Assche static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
2251209cb7fSBart Van Assche 	{ "poll_stat", 0400, queue_poll_stat_show },
2261209cb7fSBart Van Assche 	{ "requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops },
2271209cb7fSBart Van Assche 	{ "state", 0600, queue_state_show, queue_state_write },
2281209cb7fSBart Van Assche 	{ "write_hints", 0600, queue_write_hint_show, queue_write_hint_store },
22918bc4230SBart Van Assche 	{ "zone_wlock", 0400, queue_zone_wlock_show, NULL },
2301209cb7fSBart Van Assche 	{ },
2311209cb7fSBart Van Assche };
23234dbad5dSOmar Sandoval 
2331a435111SOmar Sandoval #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
234f5c0b091SBart Van Assche static const char *const hctx_state_name[] = {
2351a435111SOmar Sandoval 	HCTX_STATE_NAME(STOPPED),
2361a435111SOmar Sandoval 	HCTX_STATE_NAME(TAG_ACTIVE),
2371a435111SOmar Sandoval 	HCTX_STATE_NAME(SCHED_RESTART),
2381a435111SOmar Sandoval 	HCTX_STATE_NAME(START_ON_RUN),
239f5c0b091SBart Van Assche };
2401a435111SOmar Sandoval #undef HCTX_STATE_NAME
2411a435111SOmar Sandoval 
242f57de23aSOmar Sandoval static int hctx_state_show(void *data, struct seq_file *m)
2439abb2ad2SOmar Sandoval {
244f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
2459abb2ad2SOmar Sandoval 
246f5c0b091SBart Van Assche 	blk_flags_show(m, hctx->state, hctx_state_name,
247f5c0b091SBart Van Assche 		       ARRAY_SIZE(hctx_state_name));
248fd07dc81SBart Van Assche 	seq_puts(m, "\n");
2499abb2ad2SOmar Sandoval 	return 0;
2509abb2ad2SOmar Sandoval }
2519abb2ad2SOmar Sandoval 
2521a435111SOmar Sandoval #define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
253f5c0b091SBart Van Assche static const char *const alloc_policy_name[] = {
2541a435111SOmar Sandoval 	BLK_TAG_ALLOC_NAME(FIFO),
2551a435111SOmar Sandoval 	BLK_TAG_ALLOC_NAME(RR),
256f5c0b091SBart Van Assche };
2571a435111SOmar Sandoval #undef BLK_TAG_ALLOC_NAME
258f5c0b091SBart Van Assche 
2591a435111SOmar Sandoval #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
260f5c0b091SBart Van Assche static const char *const hctx_flag_name[] = {
2611a435111SOmar Sandoval 	HCTX_FLAG_NAME(SHOULD_MERGE),
2621a435111SOmar Sandoval 	HCTX_FLAG_NAME(TAG_SHARED),
2631a435111SOmar Sandoval 	HCTX_FLAG_NAME(SG_MERGE),
2641a435111SOmar Sandoval 	HCTX_FLAG_NAME(BLOCKING),
2651a435111SOmar Sandoval 	HCTX_FLAG_NAME(NO_SCHED),
266f5c0b091SBart Van Assche };
2671a435111SOmar Sandoval #undef HCTX_FLAG_NAME
268f5c0b091SBart Van Assche 
269f57de23aSOmar Sandoval static int hctx_flags_show(void *data, struct seq_file *m)
2709abb2ad2SOmar Sandoval {
271f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
272f5c0b091SBart Van Assche 	const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
2739abb2ad2SOmar Sandoval 
274f5c0b091SBart Van Assche 	seq_puts(m, "alloc_policy=");
275f5c0b091SBart Van Assche 	if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
276f5c0b091SBart Van Assche 	    alloc_policy_name[alloc_policy])
277f5c0b091SBart Van Assche 		seq_puts(m, alloc_policy_name[alloc_policy]);
278f5c0b091SBart Van Assche 	else
279f5c0b091SBart Van Assche 		seq_printf(m, "%d", alloc_policy);
280f5c0b091SBart Van Assche 	seq_puts(m, " ");
281f5c0b091SBart Van Assche 	blk_flags_show(m,
282f5c0b091SBart Van Assche 		       hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
283f5c0b091SBart Van Assche 		       hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
284fd07dc81SBart Van Assche 	seq_puts(m, "\n");
2859abb2ad2SOmar Sandoval 	return 0;
2869abb2ad2SOmar Sandoval }
2879abb2ad2SOmar Sandoval 
2881a435111SOmar Sandoval #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
2898658dca8SBart Van Assche static const char *const op_name[] = {
2901a435111SOmar Sandoval 	REQ_OP_NAME(READ),
2911a435111SOmar Sandoval 	REQ_OP_NAME(WRITE),
2921a435111SOmar Sandoval 	REQ_OP_NAME(FLUSH),
2931a435111SOmar Sandoval 	REQ_OP_NAME(DISCARD),
2941a435111SOmar Sandoval 	REQ_OP_NAME(ZONE_REPORT),
2951a435111SOmar Sandoval 	REQ_OP_NAME(SECURE_ERASE),
2961a435111SOmar Sandoval 	REQ_OP_NAME(ZONE_RESET),
2971a435111SOmar Sandoval 	REQ_OP_NAME(WRITE_SAME),
2981a435111SOmar Sandoval 	REQ_OP_NAME(WRITE_ZEROES),
2991a435111SOmar Sandoval 	REQ_OP_NAME(SCSI_IN),
3001a435111SOmar Sandoval 	REQ_OP_NAME(SCSI_OUT),
3011a435111SOmar Sandoval 	REQ_OP_NAME(DRV_IN),
3021a435111SOmar Sandoval 	REQ_OP_NAME(DRV_OUT),
3038658dca8SBart Van Assche };
3041a435111SOmar Sandoval #undef REQ_OP_NAME
3058658dca8SBart Van Assche 
3061a435111SOmar Sandoval #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
3078658dca8SBart Van Assche static const char *const cmd_flag_name[] = {
3081a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_DEV),
3091a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_TRANSPORT),
3101a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_DRIVER),
3111a435111SOmar Sandoval 	CMD_FLAG_NAME(SYNC),
3121a435111SOmar Sandoval 	CMD_FLAG_NAME(META),
3131a435111SOmar Sandoval 	CMD_FLAG_NAME(PRIO),
3141a435111SOmar Sandoval 	CMD_FLAG_NAME(NOMERGE),
3151a435111SOmar Sandoval 	CMD_FLAG_NAME(IDLE),
3161a435111SOmar Sandoval 	CMD_FLAG_NAME(INTEGRITY),
3171a435111SOmar Sandoval 	CMD_FLAG_NAME(FUA),
3181a435111SOmar Sandoval 	CMD_FLAG_NAME(PREFLUSH),
3191a435111SOmar Sandoval 	CMD_FLAG_NAME(RAHEAD),
3201a435111SOmar Sandoval 	CMD_FLAG_NAME(BACKGROUND),
3211a435111SOmar Sandoval 	CMD_FLAG_NAME(NOUNMAP),
32222d53821SBart Van Assche 	CMD_FLAG_NAME(NOWAIT),
3238658dca8SBart Van Assche };
3241a435111SOmar Sandoval #undef CMD_FLAG_NAME
3258658dca8SBart Van Assche 
3261a435111SOmar Sandoval #define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
3278658dca8SBart Van Assche static const char *const rqf_name[] = {
3281a435111SOmar Sandoval 	RQF_NAME(SORTED),
32985ba3effSJens Axboe 	RQF_NAME(STARTED),
3301a435111SOmar Sandoval 	RQF_NAME(QUEUED),
3311a435111SOmar Sandoval 	RQF_NAME(SOFTBARRIER),
3321a435111SOmar Sandoval 	RQF_NAME(FLUSH_SEQ),
3331a435111SOmar Sandoval 	RQF_NAME(MIXED_MERGE),
3341a435111SOmar Sandoval 	RQF_NAME(MQ_INFLIGHT),
3351a435111SOmar Sandoval 	RQF_NAME(DONTPREP),
3361a435111SOmar Sandoval 	RQF_NAME(PREEMPT),
3371a435111SOmar Sandoval 	RQF_NAME(COPY_USER),
3381a435111SOmar Sandoval 	RQF_NAME(FAILED),
3391a435111SOmar Sandoval 	RQF_NAME(QUIET),
3401a435111SOmar Sandoval 	RQF_NAME(ELVPRIV),
3411a435111SOmar Sandoval 	RQF_NAME(IO_STAT),
3421a435111SOmar Sandoval 	RQF_NAME(ALLOCED),
3431a435111SOmar Sandoval 	RQF_NAME(PM),
3441a435111SOmar Sandoval 	RQF_NAME(HASHED),
3451a435111SOmar Sandoval 	RQF_NAME(STATS),
3461a435111SOmar Sandoval 	RQF_NAME(SPECIAL_PAYLOAD),
3475d75d3f2SJens Axboe 	RQF_NAME(ZONE_WRITE_LOCKED),
3485d75d3f2SJens Axboe 	RQF_NAME(MQ_TIMEOUT_EXPIRED),
34976a86f9dSJens Axboe 	RQF_NAME(MQ_POLL_SLEPT),
3508658dca8SBart Van Assche };
3511a435111SOmar Sandoval #undef RQF_NAME
3528658dca8SBart Van Assche 
353*ec6dcf63SBart Van Assche static const char *const blk_mq_rq_state_name_array[] = {
354*ec6dcf63SBart Van Assche 	[MQ_RQ_IDLE]		= "idle",
355*ec6dcf63SBart Van Assche 	[MQ_RQ_IN_FLIGHT]	= "in_flight",
356*ec6dcf63SBart Van Assche 	[MQ_RQ_COMPLETE]	= "complete",
357*ec6dcf63SBart Van Assche };
358*ec6dcf63SBart Van Assche 
359*ec6dcf63SBart Van Assche static const char *blk_mq_rq_state_name(enum mq_rq_state rq_state)
360*ec6dcf63SBart Van Assche {
361*ec6dcf63SBart Van Assche 	if (WARN_ON_ONCE((unsigned int)rq_state >
362*ec6dcf63SBart Van Assche 			 ARRAY_SIZE(blk_mq_rq_state_name_array)))
363*ec6dcf63SBart Van Assche 		return "(?)";
364*ec6dcf63SBart Van Assche 	return blk_mq_rq_state_name_array[rq_state];
365*ec6dcf63SBart Van Assche }
366*ec6dcf63SBart Van Assche 
367daaadb3eSOmar Sandoval int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
368950cd7e9SOmar Sandoval {
3692836ee4bSBart Van Assche 	const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
3708658dca8SBart Van Assche 	const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
371950cd7e9SOmar Sandoval 
3728658dca8SBart Van Assche 	seq_printf(m, "%p {.op=", rq);
3738658dca8SBart Van Assche 	if (op < ARRAY_SIZE(op_name) && op_name[op])
3748658dca8SBart Van Assche 		seq_printf(m, "%s", op_name[op]);
3758658dca8SBart Van Assche 	else
3768658dca8SBart Van Assche 		seq_printf(m, "%d", op);
3778658dca8SBart Van Assche 	seq_puts(m, ", .cmd_flags=");
3788658dca8SBart Van Assche 	blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
3798658dca8SBart Van Assche 		       ARRAY_SIZE(cmd_flag_name));
3808658dca8SBart Van Assche 	seq_puts(m, ", .rq_flags=");
3818658dca8SBart Van Assche 	blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
3828658dca8SBart Van Assche 		       ARRAY_SIZE(rqf_name));
383*ec6dcf63SBart Van Assche 	seq_printf(m, ", .state=%s", blk_mq_rq_state_name(blk_mq_rq_state(rq)));
3842836ee4bSBart Van Assche 	seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
3858658dca8SBart Van Assche 		   rq->internal_tag);
3862836ee4bSBart Van Assche 	if (mq_ops->show_rq)
3872836ee4bSBart Van Assche 		mq_ops->show_rq(m, rq);
3882836ee4bSBart Van Assche 	seq_puts(m, "}\n");
389950cd7e9SOmar Sandoval 	return 0;
390950cd7e9SOmar Sandoval }
391daaadb3eSOmar Sandoval EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
392daaadb3eSOmar Sandoval 
393daaadb3eSOmar Sandoval int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
394daaadb3eSOmar Sandoval {
395daaadb3eSOmar Sandoval 	return __blk_mq_debugfs_rq_show(m, list_entry_rq(v));
396daaadb3eSOmar Sandoval }
39716b738f6SOmar Sandoval EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
398950cd7e9SOmar Sandoval 
399950cd7e9SOmar Sandoval static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
400f3bcb0e6SBart Van Assche 	__acquires(&hctx->lock)
401950cd7e9SOmar Sandoval {
402950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
403950cd7e9SOmar Sandoval 
404950cd7e9SOmar Sandoval 	spin_lock(&hctx->lock);
405950cd7e9SOmar Sandoval 	return seq_list_start(&hctx->dispatch, *pos);
406950cd7e9SOmar Sandoval }
407950cd7e9SOmar Sandoval 
408950cd7e9SOmar Sandoval static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
409950cd7e9SOmar Sandoval {
410950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
411950cd7e9SOmar Sandoval 
412950cd7e9SOmar Sandoval 	return seq_list_next(v, &hctx->dispatch, pos);
413950cd7e9SOmar Sandoval }
414950cd7e9SOmar Sandoval 
415950cd7e9SOmar Sandoval static void hctx_dispatch_stop(struct seq_file *m, void *v)
416f3bcb0e6SBart Van Assche 	__releases(&hctx->lock)
417950cd7e9SOmar Sandoval {
418950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
419950cd7e9SOmar Sandoval 
420950cd7e9SOmar Sandoval 	spin_unlock(&hctx->lock);
421950cd7e9SOmar Sandoval }
422950cd7e9SOmar Sandoval 
423950cd7e9SOmar Sandoval static const struct seq_operations hctx_dispatch_seq_ops = {
424950cd7e9SOmar Sandoval 	.start	= hctx_dispatch_start,
425950cd7e9SOmar Sandoval 	.next	= hctx_dispatch_next,
426950cd7e9SOmar Sandoval 	.stop	= hctx_dispatch_stop,
427950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
428950cd7e9SOmar Sandoval };
429950cd7e9SOmar Sandoval 
4302720bab5SBart Van Assche struct show_busy_params {
4312720bab5SBart Van Assche 	struct seq_file		*m;
4322720bab5SBart Van Assche 	struct blk_mq_hw_ctx	*hctx;
4332720bab5SBart Van Assche };
4342720bab5SBart Van Assche 
4352720bab5SBart Van Assche /*
4362720bab5SBart Van Assche  * Note: the state of a request may change while this function is in progress,
4372720bab5SBart Van Assche  * e.g. due to a concurrent blk_mq_finish_request() call.
4382720bab5SBart Van Assche  */
4392720bab5SBart Van Assche static void hctx_show_busy_rq(struct request *rq, void *data, bool reserved)
4402720bab5SBart Van Assche {
4412720bab5SBart Van Assche 	const struct show_busy_params *params = data;
4422720bab5SBart Van Assche 
4432720bab5SBart Van Assche 	if (blk_mq_map_queue(rq->q, rq->mq_ctx->cpu) == params->hctx &&
4445a61c363STejun Heo 	    blk_mq_rq_state(rq) != MQ_RQ_IDLE)
4452720bab5SBart Van Assche 		__blk_mq_debugfs_rq_show(params->m,
4462720bab5SBart Van Assche 					 list_entry_rq(&rq->queuelist));
4472720bab5SBart Van Assche }
4482720bab5SBart Van Assche 
4492720bab5SBart Van Assche static int hctx_busy_show(void *data, struct seq_file *m)
4502720bab5SBart Van Assche {
4512720bab5SBart Van Assche 	struct blk_mq_hw_ctx *hctx = data;
4522720bab5SBart Van Assche 	struct show_busy_params params = { .m = m, .hctx = hctx };
4532720bab5SBart Van Assche 
4542720bab5SBart Van Assche 	blk_mq_tagset_busy_iter(hctx->queue->tag_set, hctx_show_busy_rq,
4552720bab5SBart Van Assche 				&params);
4562720bab5SBart Van Assche 
4572720bab5SBart Van Assche 	return 0;
4582720bab5SBart Van Assche }
4592720bab5SBart Van Assche 
460f57de23aSOmar Sandoval static int hctx_ctx_map_show(void *data, struct seq_file *m)
461950cd7e9SOmar Sandoval {
462f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
4630bfa5288SOmar Sandoval 
4640bfa5288SOmar Sandoval 	sbitmap_bitmap_show(&hctx->ctx_map, m);
4650bfa5288SOmar Sandoval 	return 0;
4660bfa5288SOmar Sandoval }
4670bfa5288SOmar Sandoval 
468d96b37c0SOmar Sandoval static void blk_mq_debugfs_tags_show(struct seq_file *m,
469d96b37c0SOmar Sandoval 				     struct blk_mq_tags *tags)
470d96b37c0SOmar Sandoval {
471d96b37c0SOmar Sandoval 	seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
472d96b37c0SOmar Sandoval 	seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
473d96b37c0SOmar Sandoval 	seq_printf(m, "active_queues=%d\n",
474d96b37c0SOmar Sandoval 		   atomic_read(&tags->active_queues));
475d96b37c0SOmar Sandoval 
476d96b37c0SOmar Sandoval 	seq_puts(m, "\nbitmap_tags:\n");
477d96b37c0SOmar Sandoval 	sbitmap_queue_show(&tags->bitmap_tags, m);
478d96b37c0SOmar Sandoval 
479d96b37c0SOmar Sandoval 	if (tags->nr_reserved_tags) {
480d96b37c0SOmar Sandoval 		seq_puts(m, "\nbreserved_tags:\n");
481d96b37c0SOmar Sandoval 		sbitmap_queue_show(&tags->breserved_tags, m);
482d96b37c0SOmar Sandoval 	}
483d96b37c0SOmar Sandoval }
484d96b37c0SOmar Sandoval 
485f57de23aSOmar Sandoval static int hctx_tags_show(void *data, struct seq_file *m)
486d96b37c0SOmar Sandoval {
487f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
488d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
4898c0f14eaSBart Van Assche 	int res;
490d96b37c0SOmar Sandoval 
4918c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
4928c0f14eaSBart Van Assche 	if (res)
4938c0f14eaSBart Van Assche 		goto out;
494d96b37c0SOmar Sandoval 	if (hctx->tags)
495d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->tags);
496d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
497d96b37c0SOmar Sandoval 
4988c0f14eaSBart Van Assche out:
4998c0f14eaSBart Van Assche 	return res;
500d96b37c0SOmar Sandoval }
501d96b37c0SOmar Sandoval 
502f57de23aSOmar Sandoval static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
503d96b37c0SOmar Sandoval {
504f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
505d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
5068c0f14eaSBart Van Assche 	int res;
507d7e3621aSOmar Sandoval 
5088c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5098c0f14eaSBart Van Assche 	if (res)
5108c0f14eaSBart Van Assche 		goto out;
511d7e3621aSOmar Sandoval 	if (hctx->tags)
512d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
513d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
5148c0f14eaSBart Van Assche 
5158c0f14eaSBart Van Assche out:
5168c0f14eaSBart Van Assche 	return res;
517d7e3621aSOmar Sandoval }
518d7e3621aSOmar Sandoval 
519f57de23aSOmar Sandoval static int hctx_sched_tags_show(void *data, struct seq_file *m)
520d7e3621aSOmar Sandoval {
521f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
522d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
5238c0f14eaSBart Van Assche 	int res;
524d96b37c0SOmar Sandoval 
5258c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5268c0f14eaSBart Van Assche 	if (res)
5278c0f14eaSBart Van Assche 		goto out;
528d96b37c0SOmar Sandoval 	if (hctx->sched_tags)
529d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->sched_tags);
530d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
531d96b37c0SOmar Sandoval 
5328c0f14eaSBart Van Assche out:
5338c0f14eaSBart Van Assche 	return res;
534d96b37c0SOmar Sandoval }
535d96b37c0SOmar Sandoval 
536f57de23aSOmar Sandoval static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
537d96b37c0SOmar Sandoval {
538f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
539d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
5408c0f14eaSBart Van Assche 	int res;
541d7e3621aSOmar Sandoval 
5428c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5438c0f14eaSBart Van Assche 	if (res)
5448c0f14eaSBart Van Assche 		goto out;
545d7e3621aSOmar Sandoval 	if (hctx->sched_tags)
546d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
547d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
5488c0f14eaSBart Van Assche 
5498c0f14eaSBart Van Assche out:
5508c0f14eaSBart Van Assche 	return res;
551d7e3621aSOmar Sandoval }
552d7e3621aSOmar Sandoval 
553f57de23aSOmar Sandoval static int hctx_io_poll_show(void *data, struct seq_file *m)
554d7e3621aSOmar Sandoval {
555f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
556be215473SOmar Sandoval 
557be215473SOmar Sandoval 	seq_printf(m, "considered=%lu\n", hctx->poll_considered);
558be215473SOmar Sandoval 	seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
559be215473SOmar Sandoval 	seq_printf(m, "success=%lu\n", hctx->poll_success);
560be215473SOmar Sandoval 	return 0;
561be215473SOmar Sandoval }
562be215473SOmar Sandoval 
563f57de23aSOmar Sandoval static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
564be215473SOmar Sandoval 				  size_t count, loff_t *ppos)
565be215473SOmar Sandoval {
566f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
567be215473SOmar Sandoval 
568be215473SOmar Sandoval 	hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
569be215473SOmar Sandoval 	return count;
570be215473SOmar Sandoval }
571be215473SOmar Sandoval 
572f57de23aSOmar Sandoval static int hctx_dispatched_show(void *data, struct seq_file *m)
573be215473SOmar Sandoval {
574f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
575be215473SOmar Sandoval 	int i;
576be215473SOmar Sandoval 
577be215473SOmar Sandoval 	seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
578be215473SOmar Sandoval 
579be215473SOmar Sandoval 	for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
580be215473SOmar Sandoval 		unsigned int d = 1U << (i - 1);
581be215473SOmar Sandoval 
582be215473SOmar Sandoval 		seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
583be215473SOmar Sandoval 	}
584be215473SOmar Sandoval 
585be215473SOmar Sandoval 	seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
586be215473SOmar Sandoval 	return 0;
587be215473SOmar Sandoval }
588be215473SOmar Sandoval 
589f57de23aSOmar Sandoval static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
590be215473SOmar Sandoval 				     size_t count, loff_t *ppos)
591be215473SOmar Sandoval {
592f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
593be215473SOmar Sandoval 	int i;
594be215473SOmar Sandoval 
595be215473SOmar Sandoval 	for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
596be215473SOmar Sandoval 		hctx->dispatched[i] = 0;
597be215473SOmar Sandoval 	return count;
598be215473SOmar Sandoval }
599be215473SOmar Sandoval 
600f57de23aSOmar Sandoval static int hctx_queued_show(void *data, struct seq_file *m)
6014a46f05eSOmar Sandoval {
602f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6034a46f05eSOmar Sandoval 
6044a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->queued);
6054a46f05eSOmar Sandoval 	return 0;
6064a46f05eSOmar Sandoval }
6074a46f05eSOmar Sandoval 
608f57de23aSOmar Sandoval static ssize_t hctx_queued_write(void *data, const char __user *buf,
6094a46f05eSOmar Sandoval 				 size_t count, loff_t *ppos)
6104a46f05eSOmar Sandoval {
611f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6124a46f05eSOmar Sandoval 
6134a46f05eSOmar Sandoval 	hctx->queued = 0;
6144a46f05eSOmar Sandoval 	return count;
6154a46f05eSOmar Sandoval }
6164a46f05eSOmar Sandoval 
617f57de23aSOmar Sandoval static int hctx_run_show(void *data, struct seq_file *m)
6184a46f05eSOmar Sandoval {
619f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6204a46f05eSOmar Sandoval 
6214a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->run);
6224a46f05eSOmar Sandoval 	return 0;
6234a46f05eSOmar Sandoval }
6244a46f05eSOmar Sandoval 
625f57de23aSOmar Sandoval static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
626f57de23aSOmar Sandoval 			      loff_t *ppos)
6274a46f05eSOmar Sandoval {
628f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6294a46f05eSOmar Sandoval 
6304a46f05eSOmar Sandoval 	hctx->run = 0;
6314a46f05eSOmar Sandoval 	return count;
6324a46f05eSOmar Sandoval }
6334a46f05eSOmar Sandoval 
634f57de23aSOmar Sandoval static int hctx_active_show(void *data, struct seq_file *m)
6354a46f05eSOmar Sandoval {
636f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6374a46f05eSOmar Sandoval 
6384a46f05eSOmar Sandoval 	seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
6394a46f05eSOmar Sandoval 	return 0;
6404a46f05eSOmar Sandoval }
6414a46f05eSOmar Sandoval 
642950cd7e9SOmar Sandoval static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
643f3bcb0e6SBart Van Assche 	__acquires(&ctx->lock)
644950cd7e9SOmar Sandoval {
645950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
646950cd7e9SOmar Sandoval 
647950cd7e9SOmar Sandoval 	spin_lock(&ctx->lock);
648950cd7e9SOmar Sandoval 	return seq_list_start(&ctx->rq_list, *pos);
649950cd7e9SOmar Sandoval }
650950cd7e9SOmar Sandoval 
651950cd7e9SOmar Sandoval static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
652950cd7e9SOmar Sandoval {
653950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
654950cd7e9SOmar Sandoval 
655950cd7e9SOmar Sandoval 	return seq_list_next(v, &ctx->rq_list, pos);
656950cd7e9SOmar Sandoval }
657950cd7e9SOmar Sandoval 
658950cd7e9SOmar Sandoval static void ctx_rq_list_stop(struct seq_file *m, void *v)
659f3bcb0e6SBart Van Assche 	__releases(&ctx->lock)
660950cd7e9SOmar Sandoval {
661950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
662950cd7e9SOmar Sandoval 
663950cd7e9SOmar Sandoval 	spin_unlock(&ctx->lock);
664950cd7e9SOmar Sandoval }
665950cd7e9SOmar Sandoval 
666950cd7e9SOmar Sandoval static const struct seq_operations ctx_rq_list_seq_ops = {
667950cd7e9SOmar Sandoval 	.start	= ctx_rq_list_start,
668950cd7e9SOmar Sandoval 	.next	= ctx_rq_list_next,
669950cd7e9SOmar Sandoval 	.stop	= ctx_rq_list_stop,
670950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
671950cd7e9SOmar Sandoval };
672f57de23aSOmar Sandoval static int ctx_dispatched_show(void *data, struct seq_file *m)
673950cd7e9SOmar Sandoval {
674f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6754a46f05eSOmar Sandoval 
6764a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
6774a46f05eSOmar Sandoval 	return 0;
6784a46f05eSOmar Sandoval }
6794a46f05eSOmar Sandoval 
680f57de23aSOmar Sandoval static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
6814a46f05eSOmar Sandoval 				    size_t count, loff_t *ppos)
6824a46f05eSOmar Sandoval {
683f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6844a46f05eSOmar Sandoval 
6854a46f05eSOmar Sandoval 	ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
6864a46f05eSOmar Sandoval 	return count;
6874a46f05eSOmar Sandoval }
6884a46f05eSOmar Sandoval 
689f57de23aSOmar Sandoval static int ctx_merged_show(void *data, struct seq_file *m)
6904a46f05eSOmar Sandoval {
691f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6924a46f05eSOmar Sandoval 
6934a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", ctx->rq_merged);
6944a46f05eSOmar Sandoval 	return 0;
6954a46f05eSOmar Sandoval }
6964a46f05eSOmar Sandoval 
697f57de23aSOmar Sandoval static ssize_t ctx_merged_write(void *data, const char __user *buf,
6984a46f05eSOmar Sandoval 				size_t count, loff_t *ppos)
6994a46f05eSOmar Sandoval {
700f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7014a46f05eSOmar Sandoval 
7024a46f05eSOmar Sandoval 	ctx->rq_merged = 0;
7034a46f05eSOmar Sandoval 	return count;
7044a46f05eSOmar Sandoval }
7054a46f05eSOmar Sandoval 
706f57de23aSOmar Sandoval static int ctx_completed_show(void *data, struct seq_file *m)
7074a46f05eSOmar Sandoval {
708f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7094a46f05eSOmar Sandoval 
7104a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
7114a46f05eSOmar Sandoval 	return 0;
7124a46f05eSOmar Sandoval }
7134a46f05eSOmar Sandoval 
714f57de23aSOmar Sandoval static ssize_t ctx_completed_write(void *data, const char __user *buf,
7154a46f05eSOmar Sandoval 				   size_t count, loff_t *ppos)
7164a46f05eSOmar Sandoval {
717f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7184a46f05eSOmar Sandoval 
7194a46f05eSOmar Sandoval 	ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
7204a46f05eSOmar Sandoval 	return count;
7214a46f05eSOmar Sandoval }
7224a46f05eSOmar Sandoval 
723f57de23aSOmar Sandoval static int blk_mq_debugfs_show(struct seq_file *m, void *v)
724f57de23aSOmar Sandoval {
725f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = m->private;
726f57de23aSOmar Sandoval 	void *data = d_inode(m->file->f_path.dentry->d_parent)->i_private;
727f57de23aSOmar Sandoval 
728f57de23aSOmar Sandoval 	return attr->show(data, m);
729f57de23aSOmar Sandoval }
730f57de23aSOmar Sandoval 
731f57de23aSOmar Sandoval static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
732f57de23aSOmar Sandoval 				    size_t count, loff_t *ppos)
733f57de23aSOmar Sandoval {
734f57de23aSOmar Sandoval 	struct seq_file *m = file->private_data;
735f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = m->private;
736f57de23aSOmar Sandoval 	void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
737f57de23aSOmar Sandoval 
7386b136a24SEryu Guan 	/*
7396b136a24SEryu Guan 	 * Attributes that only implement .seq_ops are read-only and 'attr' is
7406b136a24SEryu Guan 	 * the same with 'data' in this case.
7416b136a24SEryu Guan 	 */
7426b136a24SEryu Guan 	if (attr == data || !attr->write)
743f57de23aSOmar Sandoval 		return -EPERM;
744f57de23aSOmar Sandoval 
745f57de23aSOmar Sandoval 	return attr->write(data, buf, count, ppos);
746f57de23aSOmar Sandoval }
747f57de23aSOmar Sandoval 
748f57de23aSOmar Sandoval static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
749f57de23aSOmar Sandoval {
750f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
751f57de23aSOmar Sandoval 	void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
752f57de23aSOmar Sandoval 	struct seq_file *m;
753f57de23aSOmar Sandoval 	int ret;
754f57de23aSOmar Sandoval 
755f57de23aSOmar Sandoval 	if (attr->seq_ops) {
756f57de23aSOmar Sandoval 		ret = seq_open(file, attr->seq_ops);
757f57de23aSOmar Sandoval 		if (!ret) {
758f57de23aSOmar Sandoval 			m = file->private_data;
759f57de23aSOmar Sandoval 			m->private = data;
760f57de23aSOmar Sandoval 		}
761f57de23aSOmar Sandoval 		return ret;
762f57de23aSOmar Sandoval 	}
763f57de23aSOmar Sandoval 
764f57de23aSOmar Sandoval 	if (WARN_ON_ONCE(!attr->show))
765f57de23aSOmar Sandoval 		return -EPERM;
766f57de23aSOmar Sandoval 
767f57de23aSOmar Sandoval 	return single_open(file, blk_mq_debugfs_show, inode->i_private);
768f57de23aSOmar Sandoval }
769f57de23aSOmar Sandoval 
770f57de23aSOmar Sandoval static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
771f57de23aSOmar Sandoval {
772f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
773f57de23aSOmar Sandoval 
774f57de23aSOmar Sandoval 	if (attr->show)
775f57de23aSOmar Sandoval 		return single_release(inode, file);
776f57de23aSOmar Sandoval 	else
777f57de23aSOmar Sandoval 		return seq_release(inode, file);
778f57de23aSOmar Sandoval }
779f57de23aSOmar Sandoval 
780f8465933SBart Van Assche static const struct file_operations blk_mq_debugfs_fops = {
781f57de23aSOmar Sandoval 	.open		= blk_mq_debugfs_open,
7824a46f05eSOmar Sandoval 	.read		= seq_read,
783f57de23aSOmar Sandoval 	.write		= blk_mq_debugfs_write,
7844a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
785f57de23aSOmar Sandoval 	.release	= blk_mq_debugfs_release,
7864a46f05eSOmar Sandoval };
7874a46f05eSOmar Sandoval 
78807e4feadSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
789f57de23aSOmar Sandoval 	{"state", 0400, hctx_state_show},
790f57de23aSOmar Sandoval 	{"flags", 0400, hctx_flags_show},
791f57de23aSOmar Sandoval 	{"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
7922720bab5SBart Van Assche 	{"busy", 0400, hctx_busy_show},
793f57de23aSOmar Sandoval 	{"ctx_map", 0400, hctx_ctx_map_show},
794f57de23aSOmar Sandoval 	{"tags", 0400, hctx_tags_show},
795f57de23aSOmar Sandoval 	{"tags_bitmap", 0400, hctx_tags_bitmap_show},
796f57de23aSOmar Sandoval 	{"sched_tags", 0400, hctx_sched_tags_show},
797f57de23aSOmar Sandoval 	{"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
798f57de23aSOmar Sandoval 	{"io_poll", 0600, hctx_io_poll_show, hctx_io_poll_write},
799f57de23aSOmar Sandoval 	{"dispatched", 0600, hctx_dispatched_show, hctx_dispatched_write},
800f57de23aSOmar Sandoval 	{"queued", 0600, hctx_queued_show, hctx_queued_write},
801f57de23aSOmar Sandoval 	{"run", 0600, hctx_run_show, hctx_run_write},
802f57de23aSOmar Sandoval 	{"active", 0400, hctx_active_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