blob: 344f9e503bdb32d145eba0e2f848a825529ae9fa [file] [log] [blame]
Christoph Hellwig8c165672019-04-30 14:42:39 -04001// SPDX-License-Identifier: GPL-2.0
Omar Sandoval07e4fea2017-01-25 08:06:40 -08002/*
3 * Copyright (C) 2017 Facebook
Omar Sandoval07e4fea2017-01-25 08:06:40 -08004 */
5
6#include <linux/kernel.h>
7#include <linux/blkdev.h>
8#include <linux/debugfs.h>
9
Omar Sandoval18fbda92017-01-31 14:53:20 -080010#include "blk.h"
Omar Sandoval07e4fea2017-01-25 08:06:40 -080011#include "blk-mq.h"
Omar Sandovald173a252017-05-04 00:31:30 -070012#include "blk-mq-debugfs.h"
Christoph Hellwig2aa77452021-11-23 19:53:08 +010013#include "blk-mq-sched.h"
Ming Leicc566942018-12-17 09:46:00 +080014#include "blk-rq-qos.h"
Omar Sandoval07e4fea2017-01-25 08:06:40 -080015
Bart Van Assche1209cb72018-02-27 16:32:13 -080016static int queue_poll_stat_show(void *data, struct seq_file *m)
17{
Bart Van Assche1209cb72018-02-27 16:32:13 -080018 return 0;
19}
20
21static void *queue_requeue_list_start(struct seq_file *m, loff_t *pos)
22 __acquires(&q->requeue_lock)
23{
24 struct request_queue *q = m->private;
25
26 spin_lock_irq(&q->requeue_lock);
27 return seq_list_start(&q->requeue_list, *pos);
28}
29
30static void *queue_requeue_list_next(struct seq_file *m, void *v, loff_t *pos)
31{
32 struct request_queue *q = m->private;
33
34 return seq_list_next(v, &q->requeue_list, pos);
35}
36
37static void queue_requeue_list_stop(struct seq_file *m, void *v)
38 __releases(&q->requeue_lock)
39{
40 struct request_queue *q = m->private;
41
42 spin_unlock_irq(&q->requeue_lock);
43}
44
45static const struct seq_operations queue_requeue_list_seq_ops = {
46 .start = queue_requeue_list_start,
47 .next = queue_requeue_list_next,
48 .stop = queue_requeue_list_stop,
49 .show = blk_mq_debugfs_rq_show,
50};
51
Bart Van Assche91d68902017-04-10 16:13:15 -060052static int blk_flags_show(struct seq_file *m, const unsigned long flags,
53 const char *const *flag_name, int flag_name_count)
54{
55 bool sep = false;
56 int i;
57
58 for (i = 0; i < sizeof(flags) * BITS_PER_BYTE; i++) {
59 if (!(flags & BIT(i)))
60 continue;
61 if (sep)
Omar Sandovalbec03d62017-05-04 00:31:23 -070062 seq_puts(m, "|");
Bart Van Assche91d68902017-04-10 16:13:15 -060063 sep = true;
64 if (i < flag_name_count && flag_name[i])
65 seq_puts(m, flag_name[i]);
66 else
67 seq_printf(m, "%d", i);
68 }
Bart Van Assche91d68902017-04-10 16:13:15 -060069 return 0;
70}
71
Bart Van Asschecd84a622018-09-26 14:01:04 -070072static int queue_pm_only_show(void *data, struct seq_file *m)
73{
74 struct request_queue *q = data;
75
76 seq_printf(m, "%d\n", atomic_read(&q->pm_only));
77 return 0;
78}
79
Omar Sandoval1a435112017-05-04 00:31:24 -070080#define QUEUE_FLAG_NAME(name) [QUEUE_FLAG_##name] = #name
Bart Van Assche91d68902017-04-10 16:13:15 -060081static const char *const blk_queue_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -070082 QUEUE_FLAG_NAME(STOPPED),
Omar Sandoval1a435112017-05-04 00:31:24 -070083 QUEUE_FLAG_NAME(DYING),
Omar Sandoval1a435112017-05-04 00:31:24 -070084 QUEUE_FLAG_NAME(NOMERGES),
85 QUEUE_FLAG_NAME(SAME_COMP),
86 QUEUE_FLAG_NAME(FAIL_IO),
Omar Sandoval1a435112017-05-04 00:31:24 -070087 QUEUE_FLAG_NAME(NOXMERGES),
Omar Sandoval1a435112017-05-04 00:31:24 -070088 QUEUE_FLAG_NAME(SAME_FORCE),
Omar Sandoval1a435112017-05-04 00:31:24 -070089 QUEUE_FLAG_NAME(INIT_DONE),
Omar Sandoval1a435112017-05-04 00:31:24 -070090 QUEUE_FLAG_NAME(STATS),
Omar Sandoval1a435112017-05-04 00:31:24 -070091 QUEUE_FLAG_NAME(REGISTERED),
Bart Van Assche22d53822017-08-18 15:52:54 -070092 QUEUE_FLAG_NAME(QUIESCED),
Hou Taobfe373f2020-04-28 09:54:56 +080093 QUEUE_FLAG_NAME(RQ_ALLOC_TIME),
Johannes Thumshirn1dbdd992021-10-04 16:22:07 +090094 QUEUE_FLAG_NAME(HCTX_ACTIVE),
Bart Van Assched5fb8722023-05-18 15:27:08 -070095 QUEUE_FLAG_NAME(SQ_SCHED),
Bart Van Assche91d68902017-04-10 16:13:15 -060096};
Omar Sandoval1a435112017-05-04 00:31:24 -070097#undef QUEUE_FLAG_NAME
Bart Van Assche91d68902017-04-10 16:13:15 -060098
Omar Sandovalf57de232017-05-04 00:31:28 -070099static int queue_state_show(void *data, struct seq_file *m)
Bart Van Assche91d68902017-04-10 16:13:15 -0600100{
Omar Sandovalf57de232017-05-04 00:31:28 -0700101 struct request_queue *q = data;
Bart Van Assche91d68902017-04-10 16:13:15 -0600102
103 blk_flags_show(m, q->queue_flags, blk_queue_flag_name,
104 ARRAY_SIZE(blk_queue_flag_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700105 seq_puts(m, "\n");
Bart Van Assche91d68902017-04-10 16:13:15 -0600106 return 0;
107}
108
Omar Sandovalf57de232017-05-04 00:31:28 -0700109static ssize_t queue_state_write(void *data, const char __user *buf,
110 size_t count, loff_t *ppos)
Bart Van Assche91d68902017-04-10 16:13:15 -0600111{
Omar Sandovalf57de232017-05-04 00:31:28 -0700112 struct request_queue *q = data;
Omar Sandoval71b90512017-05-04 00:31:26 -0700113 char opbuf[16] = { }, *op;
Bart Van Assche91d68902017-04-10 16:13:15 -0600114
Bart Van Assche18d4d7d2017-05-04 00:31:29 -0700115 /*
Christoph Hellwig1f903072022-06-19 08:05:49 +0200116 * The "state" attribute is removed when the queue is removed. Don't
117 * allow setting the state on a dying queue to avoid a use-after-free.
Bart Van Assche18d4d7d2017-05-04 00:31:29 -0700118 */
Christoph Hellwig1f903072022-06-19 08:05:49 +0200119 if (blk_queue_dying(q))
Bart Van Assche18d4d7d2017-05-04 00:31:29 -0700120 return -ENOENT;
121
Omar Sandoval71b90512017-05-04 00:31:26 -0700122 if (count >= sizeof(opbuf)) {
Omar Sandovalc7e41452017-05-04 00:31:25 -0700123 pr_err("%s: operation too long\n", __func__);
124 goto inval;
125 }
126
Omar Sandoval71b90512017-05-04 00:31:26 -0700127 if (copy_from_user(opbuf, buf, count))
Bart Van Assche91d68902017-04-10 16:13:15 -0600128 return -EFAULT;
Omar Sandoval71b90512017-05-04 00:31:26 -0700129 op = strstrip(opbuf);
Bart Van Assche91d68902017-04-10 16:13:15 -0600130 if (strcmp(op, "run") == 0) {
131 blk_mq_run_hw_queues(q, true);
132 } else if (strcmp(op, "start") == 0) {
133 blk_mq_start_stopped_hw_queues(q, true);
Bart Van Asscheedea55a2017-06-01 08:55:13 -0700134 } else if (strcmp(op, "kick") == 0) {
135 blk_mq_kick_requeue_list(q);
Bart Van Assche91d68902017-04-10 16:13:15 -0600136 } else {
Omar Sandovalc7e41452017-05-04 00:31:25 -0700137 pr_err("%s: unsupported operation '%s'\n", __func__, op);
138inval:
Bart Van Asscheedea55a2017-06-01 08:55:13 -0700139 pr_err("%s: use 'run', 'start' or 'kick'\n", __func__);
Bart Van Assche91d68902017-04-10 16:13:15 -0600140 return -EINVAL;
141 }
Omar Sandovalc7e41452017-05-04 00:31:25 -0700142 return count;
Bart Van Assche91d68902017-04-10 16:13:15 -0600143}
144
Bart Van Assche1209cb72018-02-27 16:32:13 -0800145static const struct blk_mq_debugfs_attr blk_mq_debugfs_queue_attrs[] = {
146 { "poll_stat", 0400, queue_poll_stat_show },
147 { "requeue_list", 0400, .seq_ops = &queue_requeue_list_seq_ops },
Bart Van Asschecd84a622018-09-26 14:01:04 -0700148 { "pm_only", 0600, queue_pm_only_show, NULL },
Bart Van Assche1209cb72018-02-27 16:32:13 -0800149 { "state", 0600, queue_state_show, queue_state_write },
Damien Le Moala98b05b2024-04-08 10:41:25 +0900150 { "zone_wplugs", 0400, queue_zone_wplugs_show, NULL },
Bart Van Assche1209cb72018-02-27 16:32:13 -0800151 { },
152};
Omar Sandoval34dbad52017-03-21 08:56:08 -0700153
Omar Sandoval1a435112017-05-04 00:31:24 -0700154#define HCTX_STATE_NAME(name) [BLK_MQ_S_##name] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700155static const char *const hctx_state_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700156 HCTX_STATE_NAME(STOPPED),
157 HCTX_STATE_NAME(TAG_ACTIVE),
158 HCTX_STATE_NAME(SCHED_RESTART),
Ming Leibf0beec2020-05-29 15:53:15 +0200159 HCTX_STATE_NAME(INACTIVE),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700160};
Omar Sandoval1a435112017-05-04 00:31:24 -0700161#undef HCTX_STATE_NAME
162
Omar Sandovalf57de232017-05-04 00:31:28 -0700163static int hctx_state_show(void *data, struct seq_file *m)
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800164{
Omar Sandovalf57de232017-05-04 00:31:28 -0700165 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800166
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700167 blk_flags_show(m, hctx->state, hctx_state_name,
168 ARRAY_SIZE(hctx_state_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700169 seq_puts(m, "\n");
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800170 return 0;
171}
172
Omar Sandoval1a435112017-05-04 00:31:24 -0700173#define BLK_TAG_ALLOC_NAME(name) [BLK_TAG_ALLOC_##name] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700174static const char *const alloc_policy_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700175 BLK_TAG_ALLOC_NAME(FIFO),
176 BLK_TAG_ALLOC_NAME(RR),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700177};
Omar Sandoval1a435112017-05-04 00:31:24 -0700178#undef BLK_TAG_ALLOC_NAME
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700179
Omar Sandoval1a435112017-05-04 00:31:24 -0700180#define HCTX_FLAG_NAME(name) [ilog2(BLK_MQ_F_##name)] = #name
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700181static const char *const hctx_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700182 HCTX_FLAG_NAME(SHOULD_MERGE),
Ming Lei51db1c32020-08-19 23:20:19 +0800183 HCTX_FLAG_NAME(TAG_QUEUE_SHARED),
Omar Sandoval1a435112017-05-04 00:31:24 -0700184 HCTX_FLAG_NAME(BLOCKING),
185 HCTX_FLAG_NAME(NO_SCHED),
Ming Leibf0beec2020-05-29 15:53:15 +0200186 HCTX_FLAG_NAME(STACKING),
John Garry02f938e2021-01-08 16:55:37 +0800187 HCTX_FLAG_NAME(TAG_HCTX_SHARED),
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700188};
Omar Sandoval1a435112017-05-04 00:31:24 -0700189#undef HCTX_FLAG_NAME
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700190
Omar Sandovalf57de232017-05-04 00:31:28 -0700191static int hctx_flags_show(void *data, struct seq_file *m)
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800192{
Omar Sandovalf57de232017-05-04 00:31:28 -0700193 struct blk_mq_hw_ctx *hctx = data;
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700194 const int alloc_policy = BLK_MQ_FLAG_TO_ALLOC_POLICY(hctx->flags);
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800195
Bart Van Asschef5c0b0912017-03-30 11:21:27 -0700196 seq_puts(m, "alloc_policy=");
197 if (alloc_policy < ARRAY_SIZE(alloc_policy_name) &&
198 alloc_policy_name[alloc_policy])
199 seq_puts(m, alloc_policy_name[alloc_policy]);
200 else
201 seq_printf(m, "%d", alloc_policy);
202 seq_puts(m, " ");
203 blk_flags_show(m,
204 hctx->flags ^ BLK_ALLOC_POLICY_TO_MQ_FLAG(alloc_policy),
205 hctx_flag_name, ARRAY_SIZE(hctx_flag_name));
Bart Van Asschefd07dc82017-04-26 13:47:54 -0700206 seq_puts(m, "\n");
Omar Sandoval9abb2ad2017-01-25 08:06:41 -0800207 return 0;
208}
209
Omar Sandoval1a435112017-05-04 00:31:24 -0700210#define CMD_FLAG_NAME(name) [__REQ_##name] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700211static const char *const cmd_flag_name[] = {
Omar Sandoval1a435112017-05-04 00:31:24 -0700212 CMD_FLAG_NAME(FAILFAST_DEV),
213 CMD_FLAG_NAME(FAILFAST_TRANSPORT),
214 CMD_FLAG_NAME(FAILFAST_DRIVER),
215 CMD_FLAG_NAME(SYNC),
216 CMD_FLAG_NAME(META),
217 CMD_FLAG_NAME(PRIO),
218 CMD_FLAG_NAME(NOMERGE),
219 CMD_FLAG_NAME(IDLE),
220 CMD_FLAG_NAME(INTEGRITY),
221 CMD_FLAG_NAME(FUA),
222 CMD_FLAG_NAME(PREFLUSH),
223 CMD_FLAG_NAME(RAHEAD),
224 CMD_FLAG_NAME(BACKGROUND),
Bart Van Assche22d53822017-08-18 15:52:54 -0700225 CMD_FLAG_NAME(NOWAIT),
Jianchao Wang1c260102019-01-24 18:28:55 +0800226 CMD_FLAG_NAME(NOUNMAP),
Christoph Hellwig6ce913f2021-10-12 13:12:21 +0200227 CMD_FLAG_NAME(POLLED),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700228};
Omar Sandoval1a435112017-05-04 00:31:24 -0700229#undef CMD_FLAG_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700230
Omar Sandoval1a435112017-05-04 00:31:24 -0700231#define RQF_NAME(name) [ilog2((__force u32)RQF_##name)] = #name
Bart Van Assche8658dca2017-04-26 13:47:55 -0700232static const char *const rqf_name[] = {
Jens Axboe85ba3ef2018-01-12 14:47:57 -0700233 RQF_NAME(STARTED),
Omar Sandoval1a435112017-05-04 00:31:24 -0700234 RQF_NAME(FLUSH_SEQ),
235 RQF_NAME(MIXED_MERGE),
Omar Sandoval1a435112017-05-04 00:31:24 -0700236 RQF_NAME(DONTPREP),
Christoph Hellwigdd6216b2023-05-18 07:31:01 +0200237 RQF_NAME(SCHED_TAGS),
238 RQF_NAME(USE_SCHED),
Omar Sandoval1a435112017-05-04 00:31:24 -0700239 RQF_NAME(FAILED),
240 RQF_NAME(QUIET),
Omar Sandoval1a435112017-05-04 00:31:24 -0700241 RQF_NAME(IO_STAT),
Omar Sandoval1a435112017-05-04 00:31:24 -0700242 RQF_NAME(PM),
243 RQF_NAME(HASHED),
244 RQF_NAME(STATS),
245 RQF_NAME(SPECIAL_PAYLOAD),
Jens Axboe745ed372022-09-08 17:26:59 -0600246 RQF_NAME(TIMED_OUT),
Jens Axboe745ed372022-09-08 17:26:59 -0600247 RQF_NAME(RESV),
Bart Van Assche8658dca2017-04-26 13:47:55 -0700248};
Omar Sandoval1a435112017-05-04 00:31:24 -0700249#undef RQF_NAME
Bart Van Assche8658dca2017-04-26 13:47:55 -0700250
Bart Van Asscheec6dcf62018-03-16 10:31:11 -0700251static const char *const blk_mq_rq_state_name_array[] = {
252 [MQ_RQ_IDLE] = "idle",
253 [MQ_RQ_IN_FLIGHT] = "in_flight",
254 [MQ_RQ_COMPLETE] = "complete",
255};
256
257static const char *blk_mq_rq_state_name(enum mq_rq_state rq_state)
258{
Dan Carpentera1e79182018-06-20 13:45:05 +0300259 if (WARN_ON_ONCE((unsigned int)rq_state >=
Bart Van Asscheec6dcf62018-03-16 10:31:11 -0700260 ARRAY_SIZE(blk_mq_rq_state_name_array)))
261 return "(?)";
262 return blk_mq_rq_state_name_array[rq_state];
263}
264
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700265int __blk_mq_debugfs_rq_show(struct seq_file *m, struct request *rq)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800266{
Bart Van Assche2836ee42017-04-26 13:47:56 -0700267 const struct blk_mq_ops *const mq_ops = rq->q->mq_ops;
Bart Van Assche77e7ffd2022-07-14 11:06:28 -0700268 const enum req_op op = req_op(rq);
Chaitanya Kulkarni874c8932019-06-20 10:59:17 -0700269 const char *op_str = blk_op_str(op);
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800270
Bart Van Assche8658dca2017-04-26 13:47:55 -0700271 seq_printf(m, "%p {.op=", rq);
Chaitanya Kulkarni874c8932019-06-20 10:59:17 -0700272 if (strcmp(op_str, "UNKNOWN") == 0)
Chaitanya Kulkarni3f6d3852019-06-19 15:01:49 -0700273 seq_printf(m, "%u", op);
Bart Van Assche8658dca2017-04-26 13:47:55 -0700274 else
Chaitanya Kulkarni874c8932019-06-20 10:59:17 -0700275 seq_printf(m, "%s", op_str);
Bart Van Assche8658dca2017-04-26 13:47:55 -0700276 seq_puts(m, ", .cmd_flags=");
Bart Van Assche16458cf2022-07-14 11:06:32 -0700277 blk_flags_show(m, (__force unsigned int)(rq->cmd_flags & ~REQ_OP_MASK),
278 cmd_flag_name, ARRAY_SIZE(cmd_flag_name));
Bart Van Assche8658dca2017-04-26 13:47:55 -0700279 seq_puts(m, ", .rq_flags=");
280 blk_flags_show(m, (__force unsigned int)rq->rq_flags, rqf_name,
281 ARRAY_SIZE(rqf_name));
Bart Van Asscheec6dcf62018-03-16 10:31:11 -0700282 seq_printf(m, ", .state=%s", blk_mq_rq_state_name(blk_mq_rq_state(rq)));
Bart Van Assche2836ee42017-04-26 13:47:56 -0700283 seq_printf(m, ", .tag=%d, .internal_tag=%d", rq->tag,
Bart Van Assche8658dca2017-04-26 13:47:55 -0700284 rq->internal_tag);
Bart Van Assche2836ee42017-04-26 13:47:56 -0700285 if (mq_ops->show_rq)
286 mq_ops->show_rq(m, rq);
287 seq_puts(m, "}\n");
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800288 return 0;
289}
Omar Sandovaldaaadb32017-05-04 00:31:34 -0700290EXPORT_SYMBOL_GPL(__blk_mq_debugfs_rq_show);
291
292int blk_mq_debugfs_rq_show(struct seq_file *m, void *v)
293{
294 return __blk_mq_debugfs_rq_show(m, list_entry_rq(v));
295}
Omar Sandoval16b738f2017-05-04 00:31:33 -0700296EXPORT_SYMBOL_GPL(blk_mq_debugfs_rq_show);
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800297
298static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800299 __acquires(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800300{
301 struct blk_mq_hw_ctx *hctx = m->private;
302
303 spin_lock(&hctx->lock);
304 return seq_list_start(&hctx->dispatch, *pos);
305}
306
307static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos)
308{
309 struct blk_mq_hw_ctx *hctx = m->private;
310
311 return seq_list_next(v, &hctx->dispatch, pos);
312}
313
314static void hctx_dispatch_stop(struct seq_file *m, void *v)
Bart Van Asschef3bcb0e2017-02-01 10:20:56 -0800315 __releases(&hctx->lock)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800316{
317 struct blk_mq_hw_ctx *hctx = m->private;
318
319 spin_unlock(&hctx->lock);
320}
321
322static const struct seq_operations hctx_dispatch_seq_ops = {
323 .start = hctx_dispatch_start,
324 .next = hctx_dispatch_next,
325 .stop = hctx_dispatch_stop,
326 .show = blk_mq_debugfs_rq_show,
327};
328
Bart Van Assche2720bab2017-06-01 08:55:12 -0700329struct show_busy_params {
330 struct seq_file *m;
331 struct blk_mq_hw_ctx *hctx;
332};
333
334/*
335 * Note: the state of a request may change while this function is in progress,
Jens Axboe7baa8572018-11-08 10:24:07 -0700336 * e.g. due to a concurrent blk_mq_finish_request() call. Returns true to
337 * keep iterating requests.
Bart Van Assche2720bab2017-06-01 08:55:12 -0700338 */
John Garry2dd65322022-07-06 20:03:53 +0800339static bool hctx_show_busy_rq(struct request *rq, void *data)
Bart Van Assche2720bab2017-06-01 08:55:12 -0700340{
341 const struct show_busy_params *params = data;
342
Jens Axboeea4f9952018-10-29 15:06:13 -0600343 if (rq->mq_hctx == params->hctx)
Hou Taob5fc1e82020-04-27 21:12:50 +0800344 __blk_mq_debugfs_rq_show(params->m, rq);
Jens Axboe7baa8572018-11-08 10:24:07 -0700345
346 return true;
Bart Van Assche2720bab2017-06-01 08:55:12 -0700347}
348
349static int hctx_busy_show(void *data, struct seq_file *m)
350{
351 struct blk_mq_hw_ctx *hctx = data;
352 struct show_busy_params params = { .m = m, .hctx = hctx };
353
354 blk_mq_tagset_busy_iter(hctx->queue->tag_set, hctx_show_busy_rq,
355 &params);
356
357 return 0;
358}
359
Ming Lei346fc102018-12-17 18:42:48 +0800360static const char *const hctx_types[] = {
361 [HCTX_TYPE_DEFAULT] = "default",
362 [HCTX_TYPE_READ] = "read",
363 [HCTX_TYPE_POLL] = "poll",
364};
365
366static int hctx_type_show(void *data, struct seq_file *m)
367{
368 struct blk_mq_hw_ctx *hctx = data;
369
370 BUILD_BUG_ON(ARRAY_SIZE(hctx_types) != HCTX_MAX_TYPES);
371 seq_printf(m, "%s\n", hctx_types[hctx->type]);
372 return 0;
373}
374
Omar Sandovalf57de232017-05-04 00:31:28 -0700375static int hctx_ctx_map_show(void *data, struct seq_file *m)
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800376{
Omar Sandovalf57de232017-05-04 00:31:28 -0700377 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval0bfa5282017-01-25 08:06:45 -0800378
379 sbitmap_bitmap_show(&hctx->ctx_map, m);
380 return 0;
381}
382
Omar Sandovald96b37c2017-01-25 08:06:46 -0800383static void blk_mq_debugfs_tags_show(struct seq_file *m,
384 struct blk_mq_tags *tags)
385{
386 seq_printf(m, "nr_tags=%u\n", tags->nr_tags);
387 seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags);
388 seq_printf(m, "active_queues=%d\n",
Yu Kuai4f1731df2023-06-10 10:30:43 +0800389 READ_ONCE(tags->active_queues));
Omar Sandovald96b37c2017-01-25 08:06:46 -0800390
391 seq_puts(m, "\nbitmap_tags:\n");
John Garryae0f1a72021-10-05 18:23:38 +0800392 sbitmap_queue_show(&tags->bitmap_tags, m);
Omar Sandovald96b37c2017-01-25 08:06:46 -0800393
394 if (tags->nr_reserved_tags) {
395 seq_puts(m, "\nbreserved_tags:\n");
John Garryae0f1a72021-10-05 18:23:38 +0800396 sbitmap_queue_show(&tags->breserved_tags, m);
Omar Sandovald96b37c2017-01-25 08:06:46 -0800397 }
398}
399
Omar Sandovalf57de232017-05-04 00:31:28 -0700400static int hctx_tags_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800401{
Omar Sandovalf57de232017-05-04 00:31:28 -0700402 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800403 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800404 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800405
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800406 res = mutex_lock_interruptible(&q->sysfs_lock);
407 if (res)
408 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800409 if (hctx->tags)
410 blk_mq_debugfs_tags_show(m, hctx->tags);
411 mutex_unlock(&q->sysfs_lock);
412
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800413out:
414 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800415}
416
Omar Sandovalf57de232017-05-04 00:31:28 -0700417static int hctx_tags_bitmap_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800418{
Omar Sandovalf57de232017-05-04 00:31:28 -0700419 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald7e36212017-01-25 08:06:47 -0800420 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800421 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800422
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800423 res = mutex_lock_interruptible(&q->sysfs_lock);
424 if (res)
425 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800426 if (hctx->tags)
John Garryae0f1a72021-10-05 18:23:38 +0800427 sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m);
Omar Sandovald7e36212017-01-25 08:06:47 -0800428 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800429
430out:
431 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800432}
433
Omar Sandovalf57de232017-05-04 00:31:28 -0700434static int hctx_sched_tags_show(void *data, struct seq_file *m)
Omar Sandovald7e36212017-01-25 08:06:47 -0800435{
Omar Sandovalf57de232017-05-04 00:31:28 -0700436 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800437 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800438 int res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800439
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800440 res = mutex_lock_interruptible(&q->sysfs_lock);
441 if (res)
442 goto out;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800443 if (hctx->sched_tags)
444 blk_mq_debugfs_tags_show(m, hctx->sched_tags);
445 mutex_unlock(&q->sysfs_lock);
446
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800447out:
448 return res;
Omar Sandovald96b37c2017-01-25 08:06:46 -0800449}
450
Omar Sandovalf57de232017-05-04 00:31:28 -0700451static int hctx_sched_tags_bitmap_show(void *data, struct seq_file *m)
Omar Sandovald96b37c2017-01-25 08:06:46 -0800452{
Omar Sandovalf57de232017-05-04 00:31:28 -0700453 struct blk_mq_hw_ctx *hctx = data;
Omar Sandovald7e36212017-01-25 08:06:47 -0800454 struct request_queue *q = hctx->queue;
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800455 int res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800456
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800457 res = mutex_lock_interruptible(&q->sysfs_lock);
458 if (res)
459 goto out;
Omar Sandovald7e36212017-01-25 08:06:47 -0800460 if (hctx->sched_tags)
John Garryae0f1a72021-10-05 18:23:38 +0800461 sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m);
Omar Sandovald7e36212017-01-25 08:06:47 -0800462 mutex_unlock(&q->sysfs_lock);
Bart Van Assche8c0f14e2017-02-01 10:20:58 -0800463
464out:
465 return res;
Omar Sandovald7e36212017-01-25 08:06:47 -0800466}
467
Omar Sandovalf57de232017-05-04 00:31:28 -0700468static int hctx_active_show(void *data, struct seq_file *m)
Omar Sandoval4a46f052017-01-25 08:06:49 -0800469{
Omar Sandovalf57de232017-05-04 00:31:28 -0700470 struct blk_mq_hw_ctx *hctx = data;
Omar Sandoval4a46f052017-01-25 08:06:49 -0800471
John Garry9b84c6292021-10-29 16:40:23 +0800472 seq_printf(m, "%d\n", __blk_mq_active_requests(hctx));
Omar Sandoval4a46f052017-01-25 08:06:49 -0800473 return 0;
474}
475
Ming Lei6e7687172018-07-03 09:03:16 -0600476static int hctx_dispatch_busy_show(void *data, struct seq_file *m)
477{
478 struct blk_mq_hw_ctx *hctx = data;
479
480 seq_printf(m, "%u\n", hctx->dispatch_busy);
481 return 0;
482}
483
Ming Leic16d6b52018-12-17 08:44:05 -0700484#define CTX_RQ_SEQ_OPS(name, type) \
485static void *ctx_##name##_rq_list_start(struct seq_file *m, loff_t *pos) \
486 __acquires(&ctx->lock) \
487{ \
488 struct blk_mq_ctx *ctx = m->private; \
489 \
490 spin_lock(&ctx->lock); \
491 return seq_list_start(&ctx->rq_lists[type], *pos); \
492} \
493 \
494static void *ctx_##name##_rq_list_next(struct seq_file *m, void *v, \
495 loff_t *pos) \
496{ \
497 struct blk_mq_ctx *ctx = m->private; \
498 \
499 return seq_list_next(v, &ctx->rq_lists[type], pos); \
500} \
501 \
502static void ctx_##name##_rq_list_stop(struct seq_file *m, void *v) \
503 __releases(&ctx->lock) \
504{ \
505 struct blk_mq_ctx *ctx = m->private; \
506 \
507 spin_unlock(&ctx->lock); \
508} \
509 \
510static const struct seq_operations ctx_##name##_rq_list_seq_ops = { \
511 .start = ctx_##name##_rq_list_start, \
512 .next = ctx_##name##_rq_list_next, \
513 .stop = ctx_##name##_rq_list_stop, \
514 .show = blk_mq_debugfs_rq_show, \
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800515}
516
Ming Leic16d6b52018-12-17 08:44:05 -0700517CTX_RQ_SEQ_OPS(default, HCTX_TYPE_DEFAULT);
518CTX_RQ_SEQ_OPS(read, HCTX_TYPE_READ);
519CTX_RQ_SEQ_OPS(poll, HCTX_TYPE_POLL);
Omar Sandoval950cd7e2017-01-25 08:06:42 -0800520
Omar Sandovalf57de232017-05-04 00:31:28 -0700521static int blk_mq_debugfs_show(struct seq_file *m, void *v)
522{
523 const struct blk_mq_debugfs_attr *attr = m->private;
524 void *data = d_inode(m->file->f_path.dentry->d_parent)->i_private;
525
526 return attr->show(data, m);
527}
528
529static ssize_t blk_mq_debugfs_write(struct file *file, const char __user *buf,
530 size_t count, loff_t *ppos)
531{
532 struct seq_file *m = file->private_data;
533 const struct blk_mq_debugfs_attr *attr = m->private;
534 void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
535
Eryu Guan6b136a22018-01-24 01:20:00 +0800536 /*
537 * Attributes that only implement .seq_ops are read-only and 'attr' is
538 * the same with 'data' in this case.
539 */
540 if (attr == data || !attr->write)
Omar Sandovalf57de232017-05-04 00:31:28 -0700541 return -EPERM;
542
543 return attr->write(data, buf, count, ppos);
544}
545
546static int blk_mq_debugfs_open(struct inode *inode, struct file *file)
547{
548 const struct blk_mq_debugfs_attr *attr = inode->i_private;
549 void *data = d_inode(file->f_path.dentry->d_parent)->i_private;
550 struct seq_file *m;
551 int ret;
552
553 if (attr->seq_ops) {
554 ret = seq_open(file, attr->seq_ops);
555 if (!ret) {
556 m = file->private_data;
557 m->private = data;
558 }
559 return ret;
560 }
561
562 if (WARN_ON_ONCE(!attr->show))
563 return -EPERM;
564
565 return single_open(file, blk_mq_debugfs_show, inode->i_private);
566}
567
568static int blk_mq_debugfs_release(struct inode *inode, struct file *file)
569{
570 const struct blk_mq_debugfs_attr *attr = inode->i_private;
571
572 if (attr->show)
573 return single_release(inode, file);
Chaitanya Kulkarniee1e0352019-06-19 15:01:48 -0700574
575 return seq_release(inode, file);
Omar Sandovalf57de232017-05-04 00:31:28 -0700576}
577
Bart Van Asschef8465932017-08-17 16:23:04 -0700578static const struct file_operations blk_mq_debugfs_fops = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700579 .open = blk_mq_debugfs_open,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800580 .read = seq_read,
Omar Sandovalf57de232017-05-04 00:31:28 -0700581 .write = blk_mq_debugfs_write,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800582 .llseek = seq_lseek,
Omar Sandovalf57de232017-05-04 00:31:28 -0700583 .release = blk_mq_debugfs_release,
Omar Sandoval4a46f052017-01-25 08:06:49 -0800584};
585
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800586static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = {
Omar Sandovalf57de232017-05-04 00:31:28 -0700587 {"state", 0400, hctx_state_show},
588 {"flags", 0400, hctx_flags_show},
589 {"dispatch", 0400, .seq_ops = &hctx_dispatch_seq_ops},
Bart Van Assche2720bab2017-06-01 08:55:12 -0700590 {"busy", 0400, hctx_busy_show},
Omar Sandovalf57de232017-05-04 00:31:28 -0700591 {"ctx_map", 0400, hctx_ctx_map_show},
592 {"tags", 0400, hctx_tags_show},
593 {"tags_bitmap", 0400, hctx_tags_bitmap_show},
594 {"sched_tags", 0400, hctx_sched_tags_show},
595 {"sched_tags_bitmap", 0400, hctx_sched_tags_bitmap_show},
Omar Sandovalf57de232017-05-04 00:31:28 -0700596 {"active", 0400, hctx_active_show},
Ming Lei6e7687172018-07-03 09:03:16 -0600597 {"dispatch_busy", 0400, hctx_dispatch_busy_show},
Ming Lei346fc102018-12-17 18:42:48 +0800598 {"type", 0400, hctx_type_show},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800599 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800600};
601
602static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = {
Ming Leic16d6b52018-12-17 08:44:05 -0700603 {"default_rq_list", 0400, .seq_ops = &ctx_default_rq_list_seq_ops},
604 {"read_rq_list", 0400, .seq_ops = &ctx_read_rq_list_seq_ops},
605 {"poll_rq_list", 0400, .seq_ops = &ctx_poll_rq_list_seq_ops},
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800606 {},
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800607};
608
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200609static void debugfs_create_files(struct dentry *parent, void *data,
Omar Sandovalf57de232017-05-04 00:31:28 -0700610 const struct blk_mq_debugfs_attr *attr)
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800611{
Greg Kroah-Hartman36991ca2019-01-23 14:48:54 +0100612 if (IS_ERR_OR_NULL(parent))
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200613 return;
Greg Kroah-Hartman36991ca2019-01-23 14:48:54 +0100614
Omar Sandovalf57de232017-05-04 00:31:28 -0700615 d_inode(parent)->i_private = data;
616
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200617 for (; attr->name; attr++)
618 debugfs_create_file(attr->name, attr->mode, parent,
619 (void *)attr, &blk_mq_debugfs_fops);
Bart Van Assche72f2f8f2017-02-01 10:20:59 -0800620}
621
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200622void blk_mq_debugfs_register(struct request_queue *q)
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600623{
624 struct blk_mq_hw_ctx *hctx;
Ming Lei4f481202022-03-08 15:32:18 +0800625 unsigned long i;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600626
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200627 debugfs_create_files(q->debugfs_dir, q, blk_mq_debugfs_queue_attrs);
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600628
629 /*
Omar Sandoval70e62f42017-10-03 14:57:16 -0700630 * blk_mq_init_sched() attempted to do this already, but q->debugfs_dir
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600631 * didn't exist yet (because we don't know what to name the directory
632 * until the queue is registered to a gendisk).
633 */
Omar Sandoval70e62f42017-10-03 14:57:16 -0700634 if (q->elevator && !q->sched_debugfs_dir)
635 blk_mq_debugfs_register_sched(q);
636
637 /* Similarly, blk_mq_init_hctx() couldn't do this previously. */
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600638 queue_for_each_hw_ctx(q, hctx, i) {
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200639 if (!hctx->debugfs_dir)
640 blk_mq_debugfs_register_hctx(q, hctx);
641 if (q->elevator && !hctx->sched_debugfs_dir)
642 blk_mq_debugfs_register_sched_hctx(q, hctx);
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600643 }
644
Ming Leicc566942018-12-17 09:46:00 +0800645 if (q->rq_qos) {
646 struct rq_qos *rqos = q->rq_qos;
647
648 while (rqos) {
649 blk_mq_debugfs_register_rqos(rqos);
650 rqos = rqos->next;
651 }
652 }
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600653}
654
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200655static void blk_mq_debugfs_register_ctx(struct blk_mq_hw_ctx *hctx,
656 struct blk_mq_ctx *ctx)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800657{
658 struct dentry *ctx_dir;
659 char name[20];
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800660
661 snprintf(name, sizeof(name), "cpu%u", ctx->cpu);
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600662 ctx_dir = debugfs_create_dir(name, hctx->debugfs_dir);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800663
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200664 debugfs_create_files(ctx_dir, ctx, blk_mq_debugfs_ctx_attrs);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800665}
666
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200667void blk_mq_debugfs_register_hctx(struct request_queue *q,
668 struct blk_mq_hw_ctx *hctx)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800669{
670 struct blk_mq_ctx *ctx;
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800671 char name[20];
672 int i;
673
Ming Leif3ec5d12022-07-11 17:08:08 +0800674 if (!q->debugfs_dir)
675 return;
676
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600677 snprintf(name, sizeof(name), "hctx%u", hctx->queue_num);
678 hctx->debugfs_dir = debugfs_create_dir(name, q->debugfs_dir);
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600679
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200680 debugfs_create_files(hctx->debugfs_dir, hctx, blk_mq_debugfs_hctx_attrs);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800681
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200682 hctx_for_each_ctx(hctx, ctx, i)
683 blk_mq_debugfs_register_ctx(hctx, ctx);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800684}
685
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600686void blk_mq_debugfs_unregister_hctx(struct blk_mq_hw_ctx *hctx)
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800687{
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200688 if (!hctx->queue->debugfs_dir)
689 return;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600690 debugfs_remove_recursive(hctx->debugfs_dir);
Omar Sandovald332ce02017-05-04 08:24:40 -0600691 hctx->sched_debugfs_dir = NULL;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600692 hctx->debugfs_dir = NULL;
693}
694
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200695void blk_mq_debugfs_register_hctxs(struct request_queue *q)
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600696{
697 struct blk_mq_hw_ctx *hctx;
Ming Lei4f481202022-03-08 15:32:18 +0800698 unsigned long i;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600699
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200700 queue_for_each_hw_ctx(q, hctx, i)
701 blk_mq_debugfs_register_hctx(q, hctx);
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600702}
703
704void blk_mq_debugfs_unregister_hctxs(struct request_queue *q)
705{
706 struct blk_mq_hw_ctx *hctx;
Ming Lei4f481202022-03-08 15:32:18 +0800707 unsigned long i;
Omar Sandoval9c1051a2017-05-04 08:17:21 -0600708
709 queue_for_each_hw_ctx(q, hctx, i)
710 blk_mq_debugfs_unregister_hctx(hctx);
Omar Sandoval07e4fea2017-01-25 08:06:40 -0800711}
Omar Sandovald332ce02017-05-04 08:24:40 -0600712
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200713void blk_mq_debugfs_register_sched(struct request_queue *q)
Omar Sandovald332ce02017-05-04 08:24:40 -0600714{
715 struct elevator_type *e = q->elevator->type;
716
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200717 lockdep_assert_held(&q->debugfs_mutex);
718
Greg Kroah-Hartman7e41c3c2019-07-06 17:50:32 +0200719 /*
720 * If the parent directory has not been created yet, return, we will be
721 * called again later on and the directory/files will be created then.
722 */
723 if (!q->debugfs_dir)
724 return;
725
Omar Sandovald332ce02017-05-04 08:24:40 -0600726 if (!e->queue_debugfs_attrs)
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200727 return;
Omar Sandovald332ce02017-05-04 08:24:40 -0600728
729 q->sched_debugfs_dir = debugfs_create_dir("sched", q->debugfs_dir);
Omar Sandovald332ce02017-05-04 08:24:40 -0600730
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200731 debugfs_create_files(q->sched_debugfs_dir, q, e->queue_debugfs_attrs);
Omar Sandovald332ce02017-05-04 08:24:40 -0600732}
733
734void blk_mq_debugfs_unregister_sched(struct request_queue *q)
735{
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200736 lockdep_assert_held(&q->debugfs_mutex);
737
Omar Sandovald332ce02017-05-04 08:24:40 -0600738 debugfs_remove_recursive(q->sched_debugfs_dir);
739 q->sched_debugfs_dir = NULL;
740}
741
Bart Van Asschefb440232021-06-17 17:44:43 -0700742static const char *rq_qos_id_to_name(enum rq_qos_id id)
743{
744 switch (id) {
745 case RQ_QOS_WBT:
746 return "wbt";
747 case RQ_QOS_LATENCY:
748 return "latency";
749 case RQ_QOS_COST:
750 return "cost";
751 }
752 return "unknown";
753}
754
Ming Leicc566942018-12-17 09:46:00 +0800755void blk_mq_debugfs_unregister_rqos(struct rq_qos *rqos)
756{
Christoph Hellwigba91c842023-02-03 16:03:56 +0100757 lockdep_assert_held(&rqos->disk->queue->debugfs_mutex);
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200758
Christoph Hellwigba91c842023-02-03 16:03:56 +0100759 if (!rqos->disk->queue->debugfs_dir)
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200760 return;
Ming Leicc566942018-12-17 09:46:00 +0800761 debugfs_remove_recursive(rqos->debugfs_dir);
762 rqos->debugfs_dir = NULL;
763}
764
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200765void blk_mq_debugfs_register_rqos(struct rq_qos *rqos)
Ming Leicc566942018-12-17 09:46:00 +0800766{
Christoph Hellwigba91c842023-02-03 16:03:56 +0100767 struct request_queue *q = rqos->disk->queue;
Ming Leicc566942018-12-17 09:46:00 +0800768 const char *dir_name = rq_qos_id_to_name(rqos->id);
769
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200770 lockdep_assert_held(&q->debugfs_mutex);
771
Ming Leicc566942018-12-17 09:46:00 +0800772 if (rqos->debugfs_dir || !rqos->ops->debugfs_attrs)
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200773 return;
Ming Leicc566942018-12-17 09:46:00 +0800774
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200775 if (!q->rqos_debugfs_dir)
Ming Leicc566942018-12-17 09:46:00 +0800776 q->rqos_debugfs_dir = debugfs_create_dir("rqos",
777 q->debugfs_dir);
Ming Leicc566942018-12-17 09:46:00 +0800778
Christoph Hellwigba91c842023-02-03 16:03:56 +0100779 rqos->debugfs_dir = debugfs_create_dir(dir_name, q->rqos_debugfs_dir);
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200780 debugfs_create_files(rqos->debugfs_dir, rqos, rqos->ops->debugfs_attrs);
Ming Leicc566942018-12-17 09:46:00 +0800781}
782
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200783void blk_mq_debugfs_register_sched_hctx(struct request_queue *q,
784 struct blk_mq_hw_ctx *hctx)
Omar Sandovald332ce02017-05-04 08:24:40 -0600785{
786 struct elevator_type *e = q->elevator->type;
787
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200788 lockdep_assert_held(&q->debugfs_mutex);
789
Saravanan D1e91e282021-04-07 10:59:58 -0700790 /*
791 * If the parent debugfs directory has not been created yet, return;
792 * We will be called again later on with appropriate parent debugfs
793 * directory from blk_register_queue()
794 */
795 if (!hctx->debugfs_dir)
796 return;
797
Omar Sandovald332ce02017-05-04 08:24:40 -0600798 if (!e->hctx_debugfs_attrs)
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200799 return;
Omar Sandovald332ce02017-05-04 08:24:40 -0600800
801 hctx->sched_debugfs_dir = debugfs_create_dir("sched",
802 hctx->debugfs_dir);
Greg Kroah-Hartman6cfc0082019-06-12 14:30:19 +0200803 debugfs_create_files(hctx->sched_debugfs_dir, hctx,
804 e->hctx_debugfs_attrs);
Omar Sandovald332ce02017-05-04 08:24:40 -0600805}
806
807void blk_mq_debugfs_unregister_sched_hctx(struct blk_mq_hw_ctx *hctx)
808{
Christoph Hellwig5cf9c912022-06-14 09:48:25 +0200809 lockdep_assert_held(&hctx->queue->debugfs_mutex);
810
811 if (!hctx->queue->debugfs_dir)
812 return;
Omar Sandovald332ce02017-05-04 08:24:40 -0600813 debugfs_remove_recursive(hctx->sched_debugfs_dir);
814 hctx->sched_debugfs_dir = NULL;
815}