xref: /linux/block/blk-mq-debugfs.c (revision ea4f995ee8b8f0578b3319949f2edd5d812fdb0a)
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 
105cd84a62eSBart Van Assche static int queue_pm_only_show(void *data, struct seq_file *m)
106cd84a62eSBart Van Assche {
107cd84a62eSBart Van Assche 	struct request_queue *q = data;
108cd84a62eSBart Van Assche 
109cd84a62eSBart Van Assche 	seq_printf(m, "%d\n", atomic_read(&q->pm_only));
110cd84a62eSBart Van Assche 	return 0;
111cd84a62eSBart Van Assche }
112cd84a62eSBart 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(STOPPED),
1161a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DYING),
1171a435111SOmar Sandoval 	QUEUE_FLAG_NAME(BYPASS),
1181a435111SOmar Sandoval 	QUEUE_FLAG_NAME(BIDI),
1191a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NOMERGES),
1201a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SAME_COMP),
1211a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FAIL_IO),
1221a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NONROT),
1231a435111SOmar Sandoval 	QUEUE_FLAG_NAME(IO_STAT),
1241a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DISCARD),
1251a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NOXMERGES),
1261a435111SOmar Sandoval 	QUEUE_FLAG_NAME(ADD_RANDOM),
1271a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SECERASE),
1281a435111SOmar Sandoval 	QUEUE_FLAG_NAME(SAME_FORCE),
1291a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DEAD),
1301a435111SOmar Sandoval 	QUEUE_FLAG_NAME(INIT_DONE),
1311a435111SOmar Sandoval 	QUEUE_FLAG_NAME(NO_SG_MERGE),
1321a435111SOmar Sandoval 	QUEUE_FLAG_NAME(POLL),
1331a435111SOmar Sandoval 	QUEUE_FLAG_NAME(WC),
1341a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FUA),
1351a435111SOmar Sandoval 	QUEUE_FLAG_NAME(FLUSH_NQ),
1361a435111SOmar Sandoval 	QUEUE_FLAG_NAME(DAX),
1371a435111SOmar Sandoval 	QUEUE_FLAG_NAME(STATS),
1381a435111SOmar Sandoval 	QUEUE_FLAG_NAME(POLL_STATS),
1391a435111SOmar Sandoval 	QUEUE_FLAG_NAME(REGISTERED),
14022d53821SBart Van Assche 	QUEUE_FLAG_NAME(SCSI_PASSTHROUGH),
14122d53821SBart Van Assche 	QUEUE_FLAG_NAME(QUIESCED),
14291d68905SBart Van Assche };
1431a435111SOmar Sandoval #undef QUEUE_FLAG_NAME
14491d68905SBart Van Assche 
145f57de23aSOmar Sandoval static int queue_state_show(void *data, struct seq_file *m)
14691d68905SBart Van Assche {
147f57de23aSOmar Sandoval 	struct request_queue *q = data;
14891d68905SBart Van Assche 
14991d68905SBart Van Assche 	blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
15091d68905SBart Van Assche 		       ARRAY_SIZE(blk_queue_flag_name));
151fd07dc81SBart Van Assche 	seq_puts(m, "\n");
15291d68905SBart Van Assche 	return 0;
15391d68905SBart Van Assche }
15491d68905SBart Van Assche 
155f57de23aSOmar Sandoval static ssize_t queue_state_write(void *data, const char __user *buf,
156c7e4145aSOmar Sandoval 				 size_t count, loff_t *ppos)
15791d68905SBart Van Assche {
158f57de23aSOmar Sandoval 	struct request_queue *q = data;
15971b90511SOmar Sandoval 	char opbuf[16] = { }, *op;
16091d68905SBart Van Assche 
16118d4d7d0SBart Van Assche 	/*
16218d4d7d0SBart Van Assche 	 * The "state" attribute is removed after blk_cleanup_queue() has called
16318d4d7d0SBart Van Assche 	 * blk_mq_free_queue(). Return if QUEUE_FLAG_DEAD has been set to avoid
16418d4d7d0SBart Van Assche 	 * triggering a use-after-free.
16518d4d7d0SBart Van Assche 	 */
16618d4d7d0SBart Van Assche 	if (blk_queue_dead(q))
16718d4d7d0SBart Van Assche 		return -ENOENT;
16818d4d7d0SBart Van Assche 
16971b90511SOmar Sandoval 	if (count >= sizeof(opbuf)) {
170c7e4145aSOmar Sandoval 		pr_err("%s: operation too long\n", __func__);
171c7e4145aSOmar Sandoval 		goto inval;
172c7e4145aSOmar Sandoval 	}
173c7e4145aSOmar Sandoval 
17471b90511SOmar Sandoval 	if (copy_from_user(opbuf, buf, count))
17591d68905SBart Van Assche 		return -EFAULT;
17671b90511SOmar Sandoval 	op = strstrip(opbuf);
17791d68905SBart Van Assche 	if (strcmp(op, "run") == 0) {
17891d68905SBart Van Assche 		blk_mq_run_hw_queues(q, true);
17991d68905SBart Van Assche 	} else if (strcmp(op, "start") == 0) {
18091d68905SBart Van Assche 		blk_mq_start_stopped_hw_queues(q, true);
181edea55abSBart Van Assche 	} else if (strcmp(op, "kick") == 0) {
182edea55abSBart Van Assche 		blk_mq_kick_requeue_list(q);
18391d68905SBart Van Assche 	} else {
184c7e4145aSOmar Sandoval 		pr_err("%s: unsupported operation '%s'\n", __func__, op);
185c7e4145aSOmar Sandoval inval:
186edea55abSBart Van Assche 		pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
18791d68905SBart Van Assche 		return -EINVAL;
18891d68905SBart Van Assche 	}
189c7e4145aSOmar Sandoval 	return count;
19091d68905SBart Van Assche }
19191d68905SBart Van Assche 
192f793dfd3SJens Axboe static int queue_write_hint_show(void *data, struct seq_file *m)
193f793dfd3SJens Axboe {
194f793dfd3SJens Axboe 	struct request_queue *q = data;
195f793dfd3SJens Axboe 	int i;
196f793dfd3SJens Axboe 
197f793dfd3SJens Axboe 	for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
198f793dfd3SJens Axboe 		seq_printf(m, "hint%d: %llu\n", i, q->write_hints[i]);
199f793dfd3SJens Axboe 
200f793dfd3SJens Axboe 	return 0;
201f793dfd3SJens Axboe }
202f793dfd3SJens Axboe 
203f793dfd3SJens Axboe static ssize_t queue_write_hint_store(void *data, const char __user *buf,
204f793dfd3SJens Axboe 				      size_t count, loff_t *ppos)
205f793dfd3SJens Axboe {
206f793dfd3SJens Axboe 	struct request_queue *q = data;
207f793dfd3SJens Axboe 	int i;
208f793dfd3SJens Axboe 
209f793dfd3SJens Axboe 	for (i = 0; i < BLK_MAX_WRITE_HINTS; i++)
210f793dfd3SJens Axboe 		q->write_hints[i] = 0;
211f793dfd3SJens Axboe 
212f793dfd3SJens Axboe 	return count;
213f793dfd3SJens Axboe }
214f793dfd3SJens Axboe 
2151209cb7fSBart Van Assche static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
2161209cb7fSBart Van Assche 	{ "poll_stat", 0400, queue_poll_stat_show },
2171209cb7fSBart Van Assche 	{ "requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops },
218cd84a62eSBart Van Assche 	{ "pm_only", 0600, queue_pm_only_show, NULL },
2191209cb7fSBart Van Assche 	{ "state", 0600, queue_state_show, queue_state_write },
2201209cb7fSBart Van Assche 	{ "write_hints", 0600, queue_write_hint_show, queue_write_hint_store },
22118bc4230SBart Van Assche 	{ "zone_wlock", 0400, queue_zone_wlock_show, NULL },
2221209cb7fSBart Van Assche 	{ },
2231209cb7fSBart Van Assche };
22434dbad5dSOmar Sandoval 
2251a435111SOmar Sandoval #define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
226f5c0b091SBart Van Assche static const char *const hctx_state_name[] = {
2271a435111SOmar Sandoval 	HCTX_STATE_NAME(STOPPED),
2281a435111SOmar Sandoval 	HCTX_STATE_NAME(TAG_ACTIVE),
2291a435111SOmar Sandoval 	HCTX_STATE_NAME(SCHED_RESTART),
230f5c0b091SBart Van Assche };
2311a435111SOmar Sandoval #undef HCTX_STATE_NAME
2321a435111SOmar Sandoval 
233f57de23aSOmar Sandoval static int hctx_state_show(void *data, struct seq_file *m)
2349abb2ad2SOmar Sandoval {
235f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
2369abb2ad2SOmar Sandoval 
237f5c0b091SBart Van Assche 	blk_flags_show(m, hctx->state, hctx_state_name,
238f5c0b091SBart Van Assche 		       ARRAY_SIZE(hctx_state_name));
239fd07dc81SBart Van Assche 	seq_puts(m, "\n");
2409abb2ad2SOmar Sandoval 	return 0;
2419abb2ad2SOmar Sandoval }
2429abb2ad2SOmar Sandoval 
2431a435111SOmar Sandoval #define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
244f5c0b091SBart Van Assche static const char *const alloc_policy_name[] = {
2451a435111SOmar Sandoval 	BLK_TAG_ALLOC_NAME(FIFO),
2461a435111SOmar Sandoval 	BLK_TAG_ALLOC_NAME(RR),
247f5c0b091SBart Van Assche };
2481a435111SOmar Sandoval #undef BLK_TAG_ALLOC_NAME
249f5c0b091SBart Van Assche 
2501a435111SOmar Sandoval #define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
251f5c0b091SBart Van Assche static const char *const hctx_flag_name[] = {
2521a435111SOmar Sandoval 	HCTX_FLAG_NAME(SHOULD_MERGE),
2531a435111SOmar Sandoval 	HCTX_FLAG_NAME(TAG_SHARED),
2541a435111SOmar Sandoval 	HCTX_FLAG_NAME(SG_MERGE),
2551a435111SOmar Sandoval 	HCTX_FLAG_NAME(BLOCKING),
2561a435111SOmar Sandoval 	HCTX_FLAG_NAME(NO_SCHED),
257f5c0b091SBart Van Assche };
2581a435111SOmar Sandoval #undef HCTX_FLAG_NAME
259f5c0b091SBart Van Assche 
260f57de23aSOmar Sandoval static int hctx_flags_show(void *data, struct seq_file *m)
2619abb2ad2SOmar Sandoval {
262f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
263f5c0b091SBart Van Assche 	const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
2649abb2ad2SOmar Sandoval 
265f5c0b091SBart Van Assche 	seq_puts(m, "alloc_policy=");
266f5c0b091SBart Van Assche 	if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
267f5c0b091SBart Van Assche 	    alloc_policy_name[alloc_policy])
268f5c0b091SBart Van Assche 		seq_puts(m, alloc_policy_name[alloc_policy]);
269f5c0b091SBart Van Assche 	else
270f5c0b091SBart Van Assche 		seq_printf(m, "%d", alloc_policy);
271f5c0b091SBart Van Assche 	seq_puts(m, " ");
272f5c0b091SBart Van Assche 	blk_flags_show(m,
273f5c0b091SBart Van Assche 		       hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
274f5c0b091SBart Van Assche 		       hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
275fd07dc81SBart Van Assche 	seq_puts(m, "\n");
2769abb2ad2SOmar Sandoval 	return 0;
2779abb2ad2SOmar Sandoval }
2789abb2ad2SOmar Sandoval 
2791a435111SOmar Sandoval #define REQ_OP_NAME(name) [REQ_OP_##name] = #name
2808658dca8SBart Van Assche static const char *const op_name[] = {
2811a435111SOmar Sandoval 	REQ_OP_NAME(READ),
2821a435111SOmar Sandoval 	REQ_OP_NAME(WRITE),
2831a435111SOmar Sandoval 	REQ_OP_NAME(FLUSH),
2841a435111SOmar Sandoval 	REQ_OP_NAME(DISCARD),
2851a435111SOmar Sandoval 	REQ_OP_NAME(SECURE_ERASE),
2861a435111SOmar Sandoval 	REQ_OP_NAME(ZONE_RESET),
2871a435111SOmar Sandoval 	REQ_OP_NAME(WRITE_SAME),
2881a435111SOmar Sandoval 	REQ_OP_NAME(WRITE_ZEROES),
2891a435111SOmar Sandoval 	REQ_OP_NAME(SCSI_IN),
2901a435111SOmar Sandoval 	REQ_OP_NAME(SCSI_OUT),
2911a435111SOmar Sandoval 	REQ_OP_NAME(DRV_IN),
2921a435111SOmar Sandoval 	REQ_OP_NAME(DRV_OUT),
2938658dca8SBart Van Assche };
2941a435111SOmar Sandoval #undef REQ_OP_NAME
2958658dca8SBart Van Assche 
2961a435111SOmar Sandoval #define CMD_FLAG_NAME(name) [__REQ_##name] = #name
2978658dca8SBart Van Assche static const char *const cmd_flag_name[] = {
2981a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_DEV),
2991a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_TRANSPORT),
3001a435111SOmar Sandoval 	CMD_FLAG_NAME(FAILFAST_DRIVER),
3011a435111SOmar Sandoval 	CMD_FLAG_NAME(SYNC),
3021a435111SOmar Sandoval 	CMD_FLAG_NAME(META),
3031a435111SOmar Sandoval 	CMD_FLAG_NAME(PRIO),
3041a435111SOmar Sandoval 	CMD_FLAG_NAME(NOMERGE),
3051a435111SOmar Sandoval 	CMD_FLAG_NAME(IDLE),
3061a435111SOmar Sandoval 	CMD_FLAG_NAME(INTEGRITY),
3071a435111SOmar Sandoval 	CMD_FLAG_NAME(FUA),
3081a435111SOmar Sandoval 	CMD_FLAG_NAME(PREFLUSH),
3091a435111SOmar Sandoval 	CMD_FLAG_NAME(RAHEAD),
3101a435111SOmar Sandoval 	CMD_FLAG_NAME(BACKGROUND),
3111a435111SOmar Sandoval 	CMD_FLAG_NAME(NOUNMAP),
31222d53821SBart Van Assche 	CMD_FLAG_NAME(NOWAIT),
3138658dca8SBart Van Assche };
3141a435111SOmar Sandoval #undef CMD_FLAG_NAME
3158658dca8SBart Van Assche 
3161a435111SOmar Sandoval #define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
3178658dca8SBart Van Assche static const char *const rqf_name[] = {
3181a435111SOmar Sandoval 	RQF_NAME(SORTED),
31985ba3effSJens Axboe 	RQF_NAME(STARTED),
3201a435111SOmar Sandoval 	RQF_NAME(SOFTBARRIER),
3211a435111SOmar Sandoval 	RQF_NAME(FLUSH_SEQ),
3221a435111SOmar Sandoval 	RQF_NAME(MIXED_MERGE),
3231a435111SOmar Sandoval 	RQF_NAME(MQ_INFLIGHT),
3241a435111SOmar Sandoval 	RQF_NAME(DONTPREP),
3251a435111SOmar Sandoval 	RQF_NAME(PREEMPT),
3261a435111SOmar Sandoval 	RQF_NAME(COPY_USER),
3271a435111SOmar Sandoval 	RQF_NAME(FAILED),
3281a435111SOmar Sandoval 	RQF_NAME(QUIET),
3291a435111SOmar Sandoval 	RQF_NAME(ELVPRIV),
3301a435111SOmar Sandoval 	RQF_NAME(IO_STAT),
3311a435111SOmar Sandoval 	RQF_NAME(ALLOCED),
3321a435111SOmar Sandoval 	RQF_NAME(PM),
3331a435111SOmar Sandoval 	RQF_NAME(HASHED),
3341a435111SOmar Sandoval 	RQF_NAME(STATS),
3351a435111SOmar Sandoval 	RQF_NAME(SPECIAL_PAYLOAD),
3365d75d3f2SJens Axboe 	RQF_NAME(ZONE_WRITE_LOCKED),
33776a86f9dSJens Axboe 	RQF_NAME(MQ_POLL_SLEPT),
3388658dca8SBart Van Assche };
3391a435111SOmar Sandoval #undef RQF_NAME
3408658dca8SBart Van Assche 
341ec6dcf63SBart Van Assche static const char *const blk_mq_rq_state_name_array[] = {
342ec6dcf63SBart Van Assche 	[MQ_RQ_IDLE]		= "idle",
343ec6dcf63SBart Van Assche 	[MQ_RQ_IN_FLIGHT]	= "in_flight",
344ec6dcf63SBart Van Assche 	[MQ_RQ_COMPLETE]	= "complete",
345ec6dcf63SBart Van Assche };
346ec6dcf63SBart Van Assche 
347ec6dcf63SBart Van Assche static const char *blk_mq_rq_state_name(enum mq_rq_state rq_state)
348ec6dcf63SBart Van Assche {
349a1e79188SDan Carpenter 	if (WARN_ON_ONCE((unsigned int)rq_state >=
350ec6dcf63SBart Van Assche 			 ARRAY_SIZE(blk_mq_rq_state_name_array)))
351ec6dcf63SBart Van Assche 		return "(?)";
352ec6dcf63SBart Van Assche 	return blk_mq_rq_state_name_array[rq_state];
353ec6dcf63SBart Van Assche }
354ec6dcf63SBart Van Assche 
355daaadb3eSOmar Sandoval int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
356950cd7e9SOmar Sandoval {
3572836ee4bSBart Van Assche 	const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
3588658dca8SBart Van Assche 	const unsigned int op = rq->cmd_flags & REQ_OP_MASK;
359950cd7e9SOmar Sandoval 
3608658dca8SBart Van Assche 	seq_printf(m, "%p {.op=", rq);
3618658dca8SBart Van Assche 	if (op < ARRAY_SIZE(op_name) && op_name[op])
3628658dca8SBart Van Assche 		seq_printf(m, "%s", op_name[op]);
3638658dca8SBart Van Assche 	else
3648658dca8SBart Van Assche 		seq_printf(m, "%d", op);
3658658dca8SBart Van Assche 	seq_puts(m, ", .cmd_flags=");
3668658dca8SBart Van Assche 	blk_flags_show(m, rq->cmd_flags & ~REQ_OP_MASK, cmd_flag_name,
3678658dca8SBart Van Assche 		       ARRAY_SIZE(cmd_flag_name));
3688658dca8SBart Van Assche 	seq_puts(m, ", .rq_flags=");
3698658dca8SBart Van Assche 	blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
3708658dca8SBart Van Assche 		       ARRAY_SIZE(rqf_name));
371ec6dcf63SBart Van Assche 	seq_printf(m, ", .state=%s", blk_mq_rq_state_name(blk_mq_rq_state(rq)));
3722836ee4bSBart Van Assche 	seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
3738658dca8SBart Van Assche 		   rq->internal_tag);
3742836ee4bSBart Van Assche 	if (mq_ops->show_rq)
3752836ee4bSBart Van Assche 		mq_ops->show_rq(m, rq);
3762836ee4bSBart Van Assche 	seq_puts(m, "}\n");
377950cd7e9SOmar Sandoval 	return 0;
378950cd7e9SOmar Sandoval }
379daaadb3eSOmar Sandoval EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
380daaadb3eSOmar Sandoval 
381daaadb3eSOmar Sandoval int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
382daaadb3eSOmar Sandoval {
383daaadb3eSOmar Sandoval 	return __blk_mq_debugfs_rq_show(m, list_entry_rq(v));
384daaadb3eSOmar Sandoval }
38516b738f6SOmar Sandoval EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
386950cd7e9SOmar Sandoval 
387950cd7e9SOmar Sandoval static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
388f3bcb0e6SBart Van Assche 	__acquires(&hctx->lock)
389950cd7e9SOmar Sandoval {
390950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
391950cd7e9SOmar Sandoval 
392950cd7e9SOmar Sandoval 	spin_lock(&hctx->lock);
393950cd7e9SOmar Sandoval 	return seq_list_start(&hctx->dispatch, *pos);
394950cd7e9SOmar Sandoval }
395950cd7e9SOmar Sandoval 
396950cd7e9SOmar Sandoval static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
397950cd7e9SOmar Sandoval {
398950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
399950cd7e9SOmar Sandoval 
400950cd7e9SOmar Sandoval 	return seq_list_next(v, &hctx->dispatch, pos);
401950cd7e9SOmar Sandoval }
402950cd7e9SOmar Sandoval 
403950cd7e9SOmar Sandoval static void hctx_dispatch_stop(struct seq_file *m, void *v)
404f3bcb0e6SBart Van Assche 	__releases(&hctx->lock)
405950cd7e9SOmar Sandoval {
406950cd7e9SOmar Sandoval 	struct blk_mq_hw_ctx *hctx = m->private;
407950cd7e9SOmar Sandoval 
408950cd7e9SOmar Sandoval 	spin_unlock(&hctx->lock);
409950cd7e9SOmar Sandoval }
410950cd7e9SOmar Sandoval 
411950cd7e9SOmar Sandoval static const struct seq_operations hctx_dispatch_seq_ops = {
412950cd7e9SOmar Sandoval 	.start	= hctx_dispatch_start,
413950cd7e9SOmar Sandoval 	.next	= hctx_dispatch_next,
414950cd7e9SOmar Sandoval 	.stop	= hctx_dispatch_stop,
415950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
416950cd7e9SOmar Sandoval };
417950cd7e9SOmar Sandoval 
4182720bab5SBart Van Assche struct show_busy_params {
4192720bab5SBart Van Assche 	struct seq_file		*m;
4202720bab5SBart Van Assche 	struct blk_mq_hw_ctx	*hctx;
4212720bab5SBart Van Assche };
4222720bab5SBart Van Assche 
4232720bab5SBart Van Assche /*
4242720bab5SBart Van Assche  * Note: the state of a request may change while this function is in progress,
4252720bab5SBart Van Assche  * e.g. due to a concurrent blk_mq_finish_request() call.
4262720bab5SBart Van Assche  */
4272720bab5SBart Van Assche static void hctx_show_busy_rq(struct request *rq, void *data, bool reserved)
4282720bab5SBart Van Assche {
4292720bab5SBart Van Assche 	const struct show_busy_params *params = data;
4302720bab5SBart Van Assche 
431*ea4f995eSJens Axboe 	if (rq->mq_hctx == params->hctx)
4322720bab5SBart Van Assche 		__blk_mq_debugfs_rq_show(params->m,
4332720bab5SBart Van Assche 					 list_entry_rq(&rq->queuelist));
4342720bab5SBart Van Assche }
4352720bab5SBart Van Assche 
4362720bab5SBart Van Assche static int hctx_busy_show(void *data, struct seq_file *m)
4372720bab5SBart Van Assche {
4382720bab5SBart Van Assche 	struct blk_mq_hw_ctx *hctx = data;
4392720bab5SBart Van Assche 	struct show_busy_params params = { .m = m, .hctx = hctx };
4402720bab5SBart Van Assche 
4412720bab5SBart Van Assche 	blk_mq_tagset_busy_iter(hctx->queue->tag_set, hctx_show_busy_rq,
4422720bab5SBart Van Assche 				&params);
4432720bab5SBart Van Assche 
4442720bab5SBart Van Assche 	return 0;
4452720bab5SBart Van Assche }
4462720bab5SBart Van Assche 
447f57de23aSOmar Sandoval static int hctx_ctx_map_show(void *data, struct seq_file *m)
448950cd7e9SOmar Sandoval {
449f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
4500bfa5288SOmar Sandoval 
4510bfa5288SOmar Sandoval 	sbitmap_bitmap_show(&hctx->ctx_map, m);
4520bfa5288SOmar Sandoval 	return 0;
4530bfa5288SOmar Sandoval }
4540bfa5288SOmar Sandoval 
455d96b37c0SOmar Sandoval static void blk_mq_debugfs_tags_show(struct seq_file *m,
456d96b37c0SOmar Sandoval 				     struct blk_mq_tags *tags)
457d96b37c0SOmar Sandoval {
458d96b37c0SOmar Sandoval 	seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
459d96b37c0SOmar Sandoval 	seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
460d96b37c0SOmar Sandoval 	seq_printf(m, "active_queues=%d\n",
461d96b37c0SOmar Sandoval 		   atomic_read(&tags->active_queues));
462d96b37c0SOmar Sandoval 
463d96b37c0SOmar Sandoval 	seq_puts(m, "\nbitmap_tags:\n");
464d96b37c0SOmar Sandoval 	sbitmap_queue_show(&tags->bitmap_tags, m);
465d96b37c0SOmar Sandoval 
466d96b37c0SOmar Sandoval 	if (tags->nr_reserved_tags) {
467d96b37c0SOmar Sandoval 		seq_puts(m, "\nbreserved_tags:\n");
468d96b37c0SOmar Sandoval 		sbitmap_queue_show(&tags->breserved_tags, m);
469d96b37c0SOmar Sandoval 	}
470d96b37c0SOmar Sandoval }
471d96b37c0SOmar Sandoval 
472f57de23aSOmar Sandoval static int hctx_tags_show(void *data, struct seq_file *m)
473d96b37c0SOmar Sandoval {
474f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
475d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
4768c0f14eaSBart Van Assche 	int res;
477d96b37c0SOmar Sandoval 
4788c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
4798c0f14eaSBart Van Assche 	if (res)
4808c0f14eaSBart Van Assche 		goto out;
481d96b37c0SOmar Sandoval 	if (hctx->tags)
482d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->tags);
483d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
484d96b37c0SOmar Sandoval 
4858c0f14eaSBart Van Assche out:
4868c0f14eaSBart Van Assche 	return res;
487d96b37c0SOmar Sandoval }
488d96b37c0SOmar Sandoval 
489f57de23aSOmar Sandoval static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
490d96b37c0SOmar Sandoval {
491f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
492d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
4938c0f14eaSBart Van Assche 	int res;
494d7e3621aSOmar Sandoval 
4958c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
4968c0f14eaSBart Van Assche 	if (res)
4978c0f14eaSBart Van Assche 		goto out;
498d7e3621aSOmar Sandoval 	if (hctx->tags)
499d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
500d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
5018c0f14eaSBart Van Assche 
5028c0f14eaSBart Van Assche out:
5038c0f14eaSBart Van Assche 	return res;
504d7e3621aSOmar Sandoval }
505d7e3621aSOmar Sandoval 
506f57de23aSOmar Sandoval static int hctx_sched_tags_show(void *data, struct seq_file *m)
507d7e3621aSOmar Sandoval {
508f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
509d96b37c0SOmar Sandoval 	struct request_queue *q = hctx->queue;
5108c0f14eaSBart Van Assche 	int res;
511d96b37c0SOmar Sandoval 
5128c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5138c0f14eaSBart Van Assche 	if (res)
5148c0f14eaSBart Van Assche 		goto out;
515d96b37c0SOmar Sandoval 	if (hctx->sched_tags)
516d96b37c0SOmar Sandoval 		blk_mq_debugfs_tags_show(m, hctx->sched_tags);
517d96b37c0SOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
518d96b37c0SOmar Sandoval 
5198c0f14eaSBart Van Assche out:
5208c0f14eaSBart Van Assche 	return res;
521d96b37c0SOmar Sandoval }
522d96b37c0SOmar Sandoval 
523f57de23aSOmar Sandoval static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
524d96b37c0SOmar Sandoval {
525f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
526d7e3621aSOmar Sandoval 	struct request_queue *q = hctx->queue;
5278c0f14eaSBart Van Assche 	int res;
528d7e3621aSOmar Sandoval 
5298c0f14eaSBart Van Assche 	res = mutex_lock_interruptible(&q->sysfs_lock);
5308c0f14eaSBart Van Assche 	if (res)
5318c0f14eaSBart Van Assche 		goto out;
532d7e3621aSOmar Sandoval 	if (hctx->sched_tags)
533d7e3621aSOmar Sandoval 		sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
534d7e3621aSOmar Sandoval 	mutex_unlock(&q->sysfs_lock);
5358c0f14eaSBart Van Assche 
5368c0f14eaSBart Van Assche out:
5378c0f14eaSBart Van Assche 	return res;
538d7e3621aSOmar Sandoval }
539d7e3621aSOmar Sandoval 
540f57de23aSOmar Sandoval static int hctx_io_poll_show(void *data, struct seq_file *m)
541d7e3621aSOmar Sandoval {
542f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
543be215473SOmar Sandoval 
544be215473SOmar Sandoval 	seq_printf(m, "considered=%lu\n", hctx->poll_considered);
545be215473SOmar Sandoval 	seq_printf(m, "invoked=%lu\n", hctx->poll_invoked);
546be215473SOmar Sandoval 	seq_printf(m, "success=%lu\n", hctx->poll_success);
547be215473SOmar Sandoval 	return 0;
548be215473SOmar Sandoval }
549be215473SOmar Sandoval 
550f57de23aSOmar Sandoval static ssize_t hctx_io_poll_write(void *data, const char __user *buf,
551be215473SOmar Sandoval 				  size_t count, loff_t *ppos)
552be215473SOmar Sandoval {
553f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
554be215473SOmar Sandoval 
555be215473SOmar Sandoval 	hctx->poll_considered = hctx->poll_invoked = hctx->poll_success = 0;
556be215473SOmar Sandoval 	return count;
557be215473SOmar Sandoval }
558be215473SOmar Sandoval 
559f57de23aSOmar Sandoval static int hctx_dispatched_show(void *data, struct seq_file *m)
560be215473SOmar Sandoval {
561f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
562be215473SOmar Sandoval 	int i;
563be215473SOmar Sandoval 
564be215473SOmar Sandoval 	seq_printf(m, "%8u\t%lu\n", 0U, hctx->dispatched[0]);
565be215473SOmar Sandoval 
566be215473SOmar Sandoval 	for (i = 1; i < BLK_MQ_MAX_DISPATCH_ORDER - 1; i++) {
567be215473SOmar Sandoval 		unsigned int d = 1U << (i - 1);
568be215473SOmar Sandoval 
569be215473SOmar Sandoval 		seq_printf(m, "%8u\t%lu\n", d, hctx->dispatched[i]);
570be215473SOmar Sandoval 	}
571be215473SOmar Sandoval 
572be215473SOmar Sandoval 	seq_printf(m, "%8u+\t%lu\n", 1U << (i - 1), hctx->dispatched[i]);
573be215473SOmar Sandoval 	return 0;
574be215473SOmar Sandoval }
575be215473SOmar Sandoval 
576f57de23aSOmar Sandoval static ssize_t hctx_dispatched_write(void *data, const char __user *buf,
577be215473SOmar Sandoval 				     size_t count, loff_t *ppos)
578be215473SOmar Sandoval {
579f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
580be215473SOmar Sandoval 	int i;
581be215473SOmar Sandoval 
582be215473SOmar Sandoval 	for (i = 0; i < BLK_MQ_MAX_DISPATCH_ORDER; i++)
583be215473SOmar Sandoval 		hctx->dispatched[i] = 0;
584be215473SOmar Sandoval 	return count;
585be215473SOmar Sandoval }
586be215473SOmar Sandoval 
587f57de23aSOmar Sandoval static int hctx_queued_show(void *data, struct seq_file *m)
5884a46f05eSOmar Sandoval {
589f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
5904a46f05eSOmar Sandoval 
5914a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->queued);
5924a46f05eSOmar Sandoval 	return 0;
5934a46f05eSOmar Sandoval }
5944a46f05eSOmar Sandoval 
595f57de23aSOmar Sandoval static ssize_t hctx_queued_write(void *data, const char __user *buf,
5964a46f05eSOmar Sandoval 				 size_t count, loff_t *ppos)
5974a46f05eSOmar Sandoval {
598f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
5994a46f05eSOmar Sandoval 
6004a46f05eSOmar Sandoval 	hctx->queued = 0;
6014a46f05eSOmar Sandoval 	return count;
6024a46f05eSOmar Sandoval }
6034a46f05eSOmar Sandoval 
604f57de23aSOmar Sandoval static int hctx_run_show(void *data, struct seq_file *m)
6054a46f05eSOmar Sandoval {
606f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6074a46f05eSOmar Sandoval 
6084a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", hctx->run);
6094a46f05eSOmar Sandoval 	return 0;
6104a46f05eSOmar Sandoval }
6114a46f05eSOmar Sandoval 
612f57de23aSOmar Sandoval static ssize_t hctx_run_write(void *data, const char __user *buf, size_t count,
613f57de23aSOmar Sandoval 			      loff_t *ppos)
6144a46f05eSOmar Sandoval {
615f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6164a46f05eSOmar Sandoval 
6174a46f05eSOmar Sandoval 	hctx->run = 0;
6184a46f05eSOmar Sandoval 	return count;
6194a46f05eSOmar Sandoval }
6204a46f05eSOmar Sandoval 
621f57de23aSOmar Sandoval static int hctx_active_show(void *data, struct seq_file *m)
6224a46f05eSOmar Sandoval {
623f57de23aSOmar Sandoval 	struct blk_mq_hw_ctx *hctx = data;
6244a46f05eSOmar Sandoval 
6254a46f05eSOmar Sandoval 	seq_printf(m, "%d\n", atomic_read(&hctx->nr_active));
6264a46f05eSOmar Sandoval 	return 0;
6274a46f05eSOmar Sandoval }
6284a46f05eSOmar Sandoval 
6296e768717SMing Lei static int hctx_dispatch_busy_show(void *data, struct seq_file *m)
6306e768717SMing Lei {
6316e768717SMing Lei 	struct blk_mq_hw_ctx *hctx = data;
6326e768717SMing Lei 
6336e768717SMing Lei 	seq_printf(m, "%u\n", hctx->dispatch_busy);
6346e768717SMing Lei 	return 0;
6356e768717SMing Lei }
6366e768717SMing Lei 
637950cd7e9SOmar Sandoval static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos)
638f3bcb0e6SBart Van Assche 	__acquires(&ctx->lock)
639950cd7e9SOmar Sandoval {
640950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
641950cd7e9SOmar Sandoval 
642950cd7e9SOmar Sandoval 	spin_lock(&ctx->lock);
643950cd7e9SOmar Sandoval 	return seq_list_start(&ctx->rq_list, *pos);
644950cd7e9SOmar Sandoval }
645950cd7e9SOmar Sandoval 
646950cd7e9SOmar Sandoval static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos)
647950cd7e9SOmar Sandoval {
648950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
649950cd7e9SOmar Sandoval 
650950cd7e9SOmar Sandoval 	return seq_list_next(v, &ctx->rq_list, pos);
651950cd7e9SOmar Sandoval }
652950cd7e9SOmar Sandoval 
653950cd7e9SOmar Sandoval static void ctx_rq_list_stop(struct seq_file *m, void *v)
654f3bcb0e6SBart Van Assche 	__releases(&ctx->lock)
655950cd7e9SOmar Sandoval {
656950cd7e9SOmar Sandoval 	struct blk_mq_ctx *ctx = m->private;
657950cd7e9SOmar Sandoval 
658950cd7e9SOmar Sandoval 	spin_unlock(&ctx->lock);
659950cd7e9SOmar Sandoval }
660950cd7e9SOmar Sandoval 
661950cd7e9SOmar Sandoval static const struct seq_operations ctx_rq_list_seq_ops = {
662950cd7e9SOmar Sandoval 	.start	= ctx_rq_list_start,
663950cd7e9SOmar Sandoval 	.next	= ctx_rq_list_next,
664950cd7e9SOmar Sandoval 	.stop	= ctx_rq_list_stop,
665950cd7e9SOmar Sandoval 	.show	= blk_mq_debugfs_rq_show,
666950cd7e9SOmar Sandoval };
667f57de23aSOmar Sandoval static int ctx_dispatched_show(void *data, struct seq_file *m)
668950cd7e9SOmar Sandoval {
669f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6704a46f05eSOmar Sandoval 
6714a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_dispatched[1], ctx->rq_dispatched[0]);
6724a46f05eSOmar Sandoval 	return 0;
6734a46f05eSOmar Sandoval }
6744a46f05eSOmar Sandoval 
675f57de23aSOmar Sandoval static ssize_t ctx_dispatched_write(void *data, const char __user *buf,
6764a46f05eSOmar Sandoval 				    size_t count, loff_t *ppos)
6774a46f05eSOmar Sandoval {
678f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6794a46f05eSOmar Sandoval 
6804a46f05eSOmar Sandoval 	ctx->rq_dispatched[0] = ctx->rq_dispatched[1] = 0;
6814a46f05eSOmar Sandoval 	return count;
6824a46f05eSOmar Sandoval }
6834a46f05eSOmar Sandoval 
684f57de23aSOmar Sandoval static int ctx_merged_show(void *data, struct seq_file *m)
6854a46f05eSOmar Sandoval {
686f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6874a46f05eSOmar Sandoval 
6884a46f05eSOmar Sandoval 	seq_printf(m, "%lu\n", ctx->rq_merged);
6894a46f05eSOmar Sandoval 	return 0;
6904a46f05eSOmar Sandoval }
6914a46f05eSOmar Sandoval 
692f57de23aSOmar Sandoval static ssize_t ctx_merged_write(void *data, const char __user *buf,
6934a46f05eSOmar Sandoval 				size_t count, loff_t *ppos)
6944a46f05eSOmar Sandoval {
695f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
6964a46f05eSOmar Sandoval 
6974a46f05eSOmar Sandoval 	ctx->rq_merged = 0;
6984a46f05eSOmar Sandoval 	return count;
6994a46f05eSOmar Sandoval }
7004a46f05eSOmar Sandoval 
701f57de23aSOmar Sandoval static int ctx_completed_show(void *data, struct seq_file *m)
7024a46f05eSOmar Sandoval {
703f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7044a46f05eSOmar Sandoval 
7054a46f05eSOmar Sandoval 	seq_printf(m, "%lu %lu\n", ctx->rq_completed[1], ctx->rq_completed[0]);
7064a46f05eSOmar Sandoval 	return 0;
7074a46f05eSOmar Sandoval }
7084a46f05eSOmar Sandoval 
709f57de23aSOmar Sandoval static ssize_t ctx_completed_write(void *data, const char __user *buf,
7104a46f05eSOmar Sandoval 				   size_t count, loff_t *ppos)
7114a46f05eSOmar Sandoval {
712f57de23aSOmar Sandoval 	struct blk_mq_ctx *ctx = data;
7134a46f05eSOmar Sandoval 
7144a46f05eSOmar Sandoval 	ctx->rq_completed[0] = ctx->rq_completed[1] = 0;
7154a46f05eSOmar Sandoval 	return count;
7164a46f05eSOmar Sandoval }
7174a46f05eSOmar Sandoval 
718f57de23aSOmar Sandoval static int blk_mq_debugfs_show(struct seq_file *m, void *v)
719f57de23aSOmar Sandoval {
720f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = m->private;
721f57de23aSOmar Sandoval 	void *data = d_inode(m->file->f_path.dentry->d_parent)->i_private;
722f57de23aSOmar Sandoval 
723f57de23aSOmar Sandoval 	return attr->show(data, m);
724f57de23aSOmar Sandoval }
725f57de23aSOmar Sandoval 
726f57de23aSOmar Sandoval static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
727f57de23aSOmar Sandoval 				    size_t count, loff_t *ppos)
728f57de23aSOmar Sandoval {
729f57de23aSOmar Sandoval 	struct seq_file *m = file->private_data;
730f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = m->private;
731f57de23aSOmar Sandoval 	void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
732f57de23aSOmar Sandoval 
7336b136a24SEryu Guan 	/*
7346b136a24SEryu Guan 	 * Attributes that only implement .seq_ops are read-only and 'attr' is
7356b136a24SEryu Guan 	 * the same with 'data' in this case.
7366b136a24SEryu Guan 	 */
7376b136a24SEryu Guan 	if (attr == data || !attr->write)
738f57de23aSOmar Sandoval 		return -EPERM;
739f57de23aSOmar Sandoval 
740f57de23aSOmar Sandoval 	return attr->write(data, buf, count, ppos);
741f57de23aSOmar Sandoval }
742f57de23aSOmar Sandoval 
743f57de23aSOmar Sandoval static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
744f57de23aSOmar Sandoval {
745f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
746f57de23aSOmar Sandoval 	void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
747f57de23aSOmar Sandoval 	struct seq_file *m;
748f57de23aSOmar Sandoval 	int ret;
749f57de23aSOmar Sandoval 
750f57de23aSOmar Sandoval 	if (attr->seq_ops) {
751f57de23aSOmar Sandoval 		ret = seq_open(file, attr->seq_ops);
752f57de23aSOmar Sandoval 		if (!ret) {
753f57de23aSOmar Sandoval 			m = file->private_data;
754f57de23aSOmar Sandoval 			m->private = data;
755f57de23aSOmar Sandoval 		}
756f57de23aSOmar Sandoval 		return ret;
757f57de23aSOmar Sandoval 	}
758f57de23aSOmar Sandoval 
759f57de23aSOmar Sandoval 	if (WARN_ON_ONCE(!attr->show))
760f57de23aSOmar Sandoval 		return -EPERM;
761f57de23aSOmar Sandoval 
762f57de23aSOmar Sandoval 	return single_open(file, blk_mq_debugfs_show, inode->i_private);
763f57de23aSOmar Sandoval }
764f57de23aSOmar Sandoval 
765f57de23aSOmar Sandoval static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
766f57de23aSOmar Sandoval {
767f57de23aSOmar Sandoval 	const struct blk_mq_debugfs_attr *attr = inode->i_private;
768f57de23aSOmar Sandoval 
769f57de23aSOmar Sandoval 	if (attr->show)
770f57de23aSOmar Sandoval 		return single_release(inode, file);
771f57de23aSOmar Sandoval 	else
772f57de23aSOmar Sandoval 		return seq_release(inode, file);
773f57de23aSOmar Sandoval }
774f57de23aSOmar Sandoval 
775f8465933SBart Van Assche static const struct file_operations blk_mq_debugfs_fops = {
776f57de23aSOmar Sandoval 	.open		= blk_mq_debugfs_open,
7774a46f05eSOmar Sandoval 	.read		= seq_read,
778f57de23aSOmar Sandoval 	.write		= blk_mq_debugfs_write,
7794a46f05eSOmar Sandoval 	.llseek		= seq_lseek,
780f57de23aSOmar Sandoval 	.release	= blk_mq_debugfs_release,
7814a46f05eSOmar Sandoval };
7824a46f05eSOmar Sandoval 
78307e4feadSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
784f57de23aSOmar Sandoval 	{"state", 0400, hctx_state_show},
785f57de23aSOmar Sandoval 	{"flags", 0400, hctx_flags_show},
786f57de23aSOmar Sandoval 	{"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
7872720bab5SBart Van Assche 	{"busy", 0400, hctx_busy_show},
788f57de23aSOmar Sandoval 	{"ctx_map", 0400, hctx_ctx_map_show},
789f57de23aSOmar Sandoval 	{"tags", 0400, hctx_tags_show},
790f57de23aSOmar Sandoval 	{"tags_bitmap", 0400, hctx_tags_bitmap_show},
791f57de23aSOmar Sandoval 	{"sched_tags", 0400, hctx_sched_tags_show},
792f57de23aSOmar Sandoval 	{"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
793f57de23aSOmar Sandoval 	{"io_poll", 0600, hctx_io_poll_show, hctx_io_poll_write},
794f57de23aSOmar Sandoval 	{"dispatched", 0600, hctx_dispatched_show, hctx_dispatched_write},
795f57de23aSOmar Sandoval 	{"queued", 0600, hctx_queued_show, hctx_queued_write},
796f57de23aSOmar Sandoval 	{"run", 0600, hctx_run_show, hctx_run_write},
797f57de23aSOmar Sandoval 	{"active", 0400, hctx_active_show},
7986e768717SMing Lei 	{"dispatch_busy", 0400, hctx_dispatch_busy_show},
79972f2f8f6SBart Van Assche 	{},
80007e4feadSOmar Sandoval };
80107e4feadSOmar Sandoval 
80207e4feadSOmar Sandoval static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
803f57de23aSOmar Sandoval 	{"rq_list", 0400, .seq_ops = &ctx_rq_list_seq_ops},
804f57de23aSOmar Sandoval 	{"dispatched", 0600, ctx_dispatched_show, ctx_dispatched_write},
805f57de23aSOmar Sandoval 	{"merged", 0600, ctx_merged_show, ctx_merged_write},
806f57de23aSOmar Sandoval 	{"completed", 0600, ctx_completed_show, ctx_completed_write},
80772f2f8f6SBart Van Assche 	{},
80807e4feadSOmar Sandoval };
80907e4feadSOmar Sandoval 
81072f2f8f6SBart Van Assche static bool debugfs_create_files(struct dentry *parent, void *data,
81172f2f8f6SBart Van Assche 				 const struct blk_mq_debugfs_attr *attr)
81272f2f8f6SBart Van Assche {
813f57de23aSOmar Sandoval 	d_inode(parent)->i_private = data;
814f57de23aSOmar Sandoval 
81572f2f8f6SBart Van Assche 	for (; attr->name; attr++) {
81672f2f8f6SBart Van Assche 		if (!debugfs_create_file(attr->name, attr->mode, parent,
817f57de23aSOmar Sandoval 					 (void *)attr, &blk_mq_debugfs_fops))
81872f2f8f6SBart Van Assche 			return false;
81972f2f8f6SBart Van Assche 	}
82072f2f8f6SBart Van Assche 	return true;
82172f2f8f6SBart Van Assche }
82272f2f8f6SBart Van Assche 
8239c1051aaSOmar Sandoval int blk_mq_debugfs_register(struct request_queue *q)
8249c1051aaSOmar Sandoval {
8259c1051aaSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
8269c1051aaSOmar Sandoval 	int i;
8279c1051aaSOmar Sandoval 
8289c1051aaSOmar Sandoval 	if (!blk_debugfs_root)
8299c1051aaSOmar Sandoval 		return -ENOENT;
8309c1051aaSOmar Sandoval 
8319c1051aaSOmar Sandoval 	q->debugfs_dir = debugfs_create_dir(kobject_name(q->kobj.parent),
8329c1051aaSOmar Sandoval 					    blk_debugfs_root);
8339c1051aaSOmar Sandoval 	if (!q->debugfs_dir)
8349c1051aaSOmar Sandoval 		return -ENOMEM;
8359c1051aaSOmar Sandoval 
8369c1051aaSOmar Sandoval 	if (!debugfs_create_files(q->debugfs_dir, q,
8379c1051aaSOmar Sandoval 				  blk_mq_debugfs_queue_attrs))
8389c1051aaSOmar Sandoval 		goto err;
8399c1051aaSOmar Sandoval 
8409c1051aaSOmar Sandoval 	/*
84170e62f4bSOmar Sandoval 	 * blk_mq_init_sched() attempted to do this already, but q->debugfs_dir
8429c1051aaSOmar Sandoval 	 * didn't exist yet (because we don't know what to name the directory
8439c1051aaSOmar Sandoval 	 * until the queue is registered to a gendisk).
8449c1051aaSOmar Sandoval 	 */
84570e62f4bSOmar Sandoval 	if (q->elevator && !q->sched_debugfs_dir)
84670e62f4bSOmar Sandoval 		blk_mq_debugfs_register_sched(q);
84770e62f4bSOmar Sandoval 
84870e62f4bSOmar Sandoval 	/* Similarly, blk_mq_init_hctx() couldn't do this previously. */
8499c1051aaSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
8509c1051aaSOmar Sandoval 		if (!hctx->debugfs_dir && blk_mq_debugfs_register_hctx(q, hctx))
8519c1051aaSOmar Sandoval 			goto err;
852d332ce09SOmar Sandoval 		if (q->elevator && !hctx->sched_debugfs_dir &&
853d332ce09SOmar Sandoval 		    blk_mq_debugfs_register_sched_hctx(q, hctx))
854d332ce09SOmar Sandoval 			goto err;
8559c1051aaSOmar Sandoval 	}
8569c1051aaSOmar Sandoval 
8579c1051aaSOmar Sandoval 	return 0;
8589c1051aaSOmar Sandoval 
8599c1051aaSOmar Sandoval err:
8609c1051aaSOmar Sandoval 	blk_mq_debugfs_unregister(q);
8619c1051aaSOmar Sandoval 	return -ENOMEM;
8629c1051aaSOmar Sandoval }
8639c1051aaSOmar Sandoval 
8649c1051aaSOmar Sandoval void blk_mq_debugfs_unregister(struct request_queue *q)
8659c1051aaSOmar Sandoval {
8669c1051aaSOmar Sandoval 	debugfs_remove_recursive(q->debugfs_dir);
867d332ce09SOmar Sandoval 	q->sched_debugfs_dir = NULL;
8689c1051aaSOmar Sandoval 	q->debugfs_dir = NULL;
8699c1051aaSOmar Sandoval }
8709c1051aaSOmar Sandoval 
8719c1051aaSOmar Sandoval static int blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,
8729c1051aaSOmar Sandoval 				       struct blk_mq_ctx *ctx)
87307e4feadSOmar Sandoval {
87407e4feadSOmar Sandoval 	struct dentry *ctx_dir;
87507e4feadSOmar Sandoval 	char name[20];
87607e4feadSOmar Sandoval 
87707e4feadSOmar Sandoval 	snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
8789c1051aaSOmar Sandoval 	ctx_dir = debugfs_create_dir(name, hctx->debugfs_dir);
87907e4feadSOmar Sandoval 	if (!ctx_dir)
88007e4feadSOmar Sandoval 		return -ENOMEM;
88107e4feadSOmar Sandoval 
88272f2f8f6SBart Van Assche 	if (!debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs))
88307e4feadSOmar Sandoval 		return -ENOMEM;
88407e4feadSOmar Sandoval 
88507e4feadSOmar Sandoval 	return 0;
88607e4feadSOmar Sandoval }
88707e4feadSOmar Sandoval 
8889c1051aaSOmar Sandoval int blk_mq_debugfs_register_hctx(struct request_queue *q,
88907e4feadSOmar Sandoval 				 struct blk_mq_hw_ctx *hctx)
89007e4feadSOmar Sandoval {
89107e4feadSOmar Sandoval 	struct blk_mq_ctx *ctx;
89207e4feadSOmar Sandoval 	char name[20];
89307e4feadSOmar Sandoval 	int i;
89407e4feadSOmar Sandoval 
89507e4feadSOmar Sandoval 	if (!q->debugfs_dir)
89607e4feadSOmar Sandoval 		return -ENOENT;
89707e4feadSOmar Sandoval 
8989c1051aaSOmar Sandoval 	snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
8999c1051aaSOmar Sandoval 	hctx->debugfs_dir = debugfs_create_dir(name, q->debugfs_dir);
9009c1051aaSOmar Sandoval 	if (!hctx->debugfs_dir)
9019c1051aaSOmar Sandoval 		return -ENOMEM;
9029c1051aaSOmar Sandoval 
9039c1051aaSOmar Sandoval 	if (!debugfs_create_files(hctx->debugfs_dir, hctx,
9049c1051aaSOmar Sandoval 				  blk_mq_debugfs_hctx_attrs))
90507e4feadSOmar Sandoval 		goto err;
90607e4feadSOmar Sandoval 
9079c1051aaSOmar Sandoval 	hctx_for_each_ctx(hctx, ctx, i) {
9089c1051aaSOmar Sandoval 		if (blk_mq_debugfs_register_ctx(hctx, ctx))
90907e4feadSOmar Sandoval 			goto err;
91007e4feadSOmar Sandoval 	}
91107e4feadSOmar Sandoval 
91207e4feadSOmar Sandoval 	return 0;
91307e4feadSOmar Sandoval 
91407e4feadSOmar Sandoval err:
9159c1051aaSOmar Sandoval 	blk_mq_debugfs_unregister_hctx(hctx);
91607e4feadSOmar Sandoval 	return -ENOMEM;
91707e4feadSOmar Sandoval }
91807e4feadSOmar Sandoval 
9199c1051aaSOmar Sandoval void blk_mq_debugfs_unregister_hctx(struct blk_mq_hw_ctx *hctx)
92007e4feadSOmar Sandoval {
9219c1051aaSOmar Sandoval 	debugfs_remove_recursive(hctx->debugfs_dir);
922d332ce09SOmar Sandoval 	hctx->sched_debugfs_dir = NULL;
9239c1051aaSOmar Sandoval 	hctx->debugfs_dir = NULL;
9249c1051aaSOmar Sandoval }
9259c1051aaSOmar Sandoval 
9269c1051aaSOmar Sandoval int blk_mq_debugfs_register_hctxs(struct request_queue *q)
9279c1051aaSOmar Sandoval {
9289c1051aaSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
9299c1051aaSOmar Sandoval 	int i;
9309c1051aaSOmar Sandoval 
9319c1051aaSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i) {
9329c1051aaSOmar Sandoval 		if (blk_mq_debugfs_register_hctx(q, hctx))
9339c1051aaSOmar Sandoval 			return -ENOMEM;
9349c1051aaSOmar Sandoval 	}
9359c1051aaSOmar Sandoval 
9369c1051aaSOmar Sandoval 	return 0;
9379c1051aaSOmar Sandoval }
9389c1051aaSOmar Sandoval 
9399c1051aaSOmar Sandoval void blk_mq_debugfs_unregister_hctxs(struct request_queue *q)
9409c1051aaSOmar Sandoval {
9419c1051aaSOmar Sandoval 	struct blk_mq_hw_ctx *hctx;
9429c1051aaSOmar Sandoval 	int i;
9439c1051aaSOmar Sandoval 
9449c1051aaSOmar Sandoval 	queue_for_each_hw_ctx(q, hctx, i)
9459c1051aaSOmar Sandoval 		blk_mq_debugfs_unregister_hctx(hctx);
94607e4feadSOmar Sandoval }
947d332ce09SOmar Sandoval 
948d332ce09SOmar Sandoval int blk_mq_debugfs_register_sched(struct request_queue *q)
949d332ce09SOmar Sandoval {
950d332ce09SOmar Sandoval 	struct elevator_type *e = q->elevator->type;
951d332ce09SOmar Sandoval 
952d332ce09SOmar Sandoval 	if (!q->debugfs_dir)
953d332ce09SOmar Sandoval 		return -ENOENT;
954d332ce09SOmar Sandoval 
955d332ce09SOmar Sandoval 	if (!e->queue_debugfs_attrs)
956d332ce09SOmar Sandoval 		return 0;
957d332ce09SOmar Sandoval 
958d332ce09SOmar Sandoval 	q->sched_debugfs_dir = debugfs_create_dir("sched", q->debugfs_dir);
959d332ce09SOmar Sandoval 	if (!q->sched_debugfs_dir)
960d332ce09SOmar Sandoval 		return -ENOMEM;
961d332ce09SOmar Sandoval 
962d332ce09SOmar Sandoval 	if (!debugfs_create_files(q->sched_debugfs_dir, q,
963d332ce09SOmar Sandoval 				  e->queue_debugfs_attrs))
964d332ce09SOmar Sandoval 		goto err;
965d332ce09SOmar Sandoval 
966d332ce09SOmar Sandoval 	return 0;
967d332ce09SOmar Sandoval 
968d332ce09SOmar Sandoval err:
969d332ce09SOmar Sandoval 	blk_mq_debugfs_unregister_sched(q);
970d332ce09SOmar Sandoval 	return -ENOMEM;
971d332ce09SOmar Sandoval }
972d332ce09SOmar Sandoval 
973d332ce09SOmar Sandoval void blk_mq_debugfs_unregister_sched(struct request_queue *q)
974d332ce09SOmar Sandoval {
975d332ce09SOmar Sandoval 	debugfs_remove_recursive(q->sched_debugfs_dir);
976d332ce09SOmar Sandoval 	q->sched_debugfs_dir = NULL;
977d332ce09SOmar Sandoval }
978d332ce09SOmar Sandoval 
979d332ce09SOmar Sandoval int blk_mq_debugfs_register_sched_hctx(struct request_queue *q,
980d332ce09SOmar Sandoval 				       struct blk_mq_hw_ctx *hctx)
981d332ce09SOmar Sandoval {
982d332ce09SOmar Sandoval 	struct elevator_type *e = q->elevator->type;
983d332ce09SOmar Sandoval 
984d332ce09SOmar Sandoval 	if (!hctx->debugfs_dir)
985d332ce09SOmar Sandoval 		return -ENOENT;
986d332ce09SOmar Sandoval 
987d332ce09SOmar Sandoval 	if (!e->hctx_debugfs_attrs)
988d332ce09SOmar Sandoval 		return 0;
989d332ce09SOmar Sandoval 
990d332ce09SOmar Sandoval 	hctx->sched_debugfs_dir = debugfs_create_dir("sched",
991d332ce09SOmar Sandoval 						     hctx->debugfs_dir);
992d332ce09SOmar Sandoval 	if (!hctx->sched_debugfs_dir)
993d332ce09SOmar Sandoval 		return -ENOMEM;
994d332ce09SOmar Sandoval 
995d332ce09SOmar Sandoval 	if (!debugfs_create_files(hctx->sched_debugfs_dir, hctx,
996d332ce09SOmar Sandoval 				  e->hctx_debugfs_attrs))
997d332ce09SOmar Sandoval 		return -ENOMEM;
998d332ce09SOmar Sandoval 
999d332ce09SOmar Sandoval 	return 0;
1000d332ce09SOmar Sandoval }
1001d332ce09SOmar Sandoval 
1002d332ce09SOmar Sandoval void blk_mq_debugfs_unregister_sched_hctx(struct blk_mq_hw_ctx *hctx)
1003d332ce09SOmar Sandoval {
1004d332ce09SOmar Sandoval 	debugfs_remove_recursive(hctx->sched_debugfs_dir);
1005d332ce09SOmar Sandoval 	hctx->sched_debugfs_dir = NULL;
1006d332ce09SOmar Sandoval }
1007