Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Facebook |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/blkdev.h> |
| 19 | #include <linux/debugfs.h> |
| 20 | |
| 21 | #include <linux/blk-mq.h> |
| 22 | #include "blk-mq.h" |
| 23 | |
| 24 | struct blk_mq_debugfs_attr { |
| 25 | const char *name; |
| 26 | umode_t mode; |
| 27 | const struct file_operations *fops; |
| 28 | }; |
| 29 | |
| 30 | static struct dentry *block_debugfs_root; |
| 31 | |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame^] | 32 | static int blk_mq_debugfs_seq_open(struct inode *inode, struct file *file, |
| 33 | const struct seq_operations *ops) |
| 34 | { |
| 35 | struct seq_file *m; |
| 36 | int ret; |
| 37 | |
| 38 | ret = seq_open(file, ops); |
| 39 | if (!ret) { |
| 40 | m = file->private_data; |
| 41 | m->private = inode->i_private; |
| 42 | } |
| 43 | return ret; |
| 44 | } |
| 45 | |
Omar Sandoval | 9abb2ad | 2017-01-25 08:06:41 -0800 | [diff] [blame] | 46 | static int hctx_state_show(struct seq_file *m, void *v) |
| 47 | { |
| 48 | struct blk_mq_hw_ctx *hctx = m->private; |
| 49 | |
| 50 | seq_printf(m, "0x%lx\n", hctx->state); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int hctx_state_open(struct inode *inode, struct file *file) |
| 55 | { |
| 56 | return single_open(file, hctx_state_show, inode->i_private); |
| 57 | } |
| 58 | |
| 59 | static const struct file_operations hctx_state_fops = { |
| 60 | .open = hctx_state_open, |
| 61 | .read = seq_read, |
| 62 | .llseek = seq_lseek, |
| 63 | .release = single_release, |
| 64 | }; |
| 65 | |
| 66 | static int hctx_flags_show(struct seq_file *m, void *v) |
| 67 | { |
| 68 | struct blk_mq_hw_ctx *hctx = m->private; |
| 69 | |
| 70 | seq_printf(m, "0x%lx\n", hctx->flags); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int hctx_flags_open(struct inode *inode, struct file *file) |
| 75 | { |
| 76 | return single_open(file, hctx_flags_show, inode->i_private); |
| 77 | } |
| 78 | |
| 79 | static const struct file_operations hctx_flags_fops = { |
| 80 | .open = hctx_flags_open, |
| 81 | .read = seq_read, |
| 82 | .llseek = seq_lseek, |
| 83 | .release = single_release, |
| 84 | }; |
| 85 | |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame^] | 86 | static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v) |
| 87 | { |
| 88 | struct request *rq = list_entry_rq(v); |
| 89 | |
| 90 | seq_printf(m, "%p\n", rq); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos) |
| 95 | { |
| 96 | struct blk_mq_hw_ctx *hctx = m->private; |
| 97 | |
| 98 | spin_lock(&hctx->lock); |
| 99 | return seq_list_start(&hctx->dispatch, *pos); |
| 100 | } |
| 101 | |
| 102 | static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos) |
| 103 | { |
| 104 | struct blk_mq_hw_ctx *hctx = m->private; |
| 105 | |
| 106 | return seq_list_next(v, &hctx->dispatch, pos); |
| 107 | } |
| 108 | |
| 109 | static void hctx_dispatch_stop(struct seq_file *m, void *v) |
| 110 | { |
| 111 | struct blk_mq_hw_ctx *hctx = m->private; |
| 112 | |
| 113 | spin_unlock(&hctx->lock); |
| 114 | } |
| 115 | |
| 116 | static const struct seq_operations hctx_dispatch_seq_ops = { |
| 117 | .start = hctx_dispatch_start, |
| 118 | .next = hctx_dispatch_next, |
| 119 | .stop = hctx_dispatch_stop, |
| 120 | .show = blk_mq_debugfs_rq_show, |
| 121 | }; |
| 122 | |
| 123 | static int hctx_dispatch_open(struct inode *inode, struct file *file) |
| 124 | { |
| 125 | return blk_mq_debugfs_seq_open(inode, file, &hctx_dispatch_seq_ops); |
| 126 | } |
| 127 | |
| 128 | static const struct file_operations hctx_dispatch_fops = { |
| 129 | .open = hctx_dispatch_open, |
| 130 | .read = seq_read, |
| 131 | .llseek = seq_lseek, |
| 132 | .release = seq_release, |
| 133 | }; |
| 134 | |
| 135 | static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos) |
| 136 | { |
| 137 | struct blk_mq_ctx *ctx = m->private; |
| 138 | |
| 139 | spin_lock(&ctx->lock); |
| 140 | return seq_list_start(&ctx->rq_list, *pos); |
| 141 | } |
| 142 | |
| 143 | static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos) |
| 144 | { |
| 145 | struct blk_mq_ctx *ctx = m->private; |
| 146 | |
| 147 | return seq_list_next(v, &ctx->rq_list, pos); |
| 148 | } |
| 149 | |
| 150 | static void ctx_rq_list_stop(struct seq_file *m, void *v) |
| 151 | { |
| 152 | struct blk_mq_ctx *ctx = m->private; |
| 153 | |
| 154 | spin_unlock(&ctx->lock); |
| 155 | } |
| 156 | |
| 157 | static const struct seq_operations ctx_rq_list_seq_ops = { |
| 158 | .start = ctx_rq_list_start, |
| 159 | .next = ctx_rq_list_next, |
| 160 | .stop = ctx_rq_list_stop, |
| 161 | .show = blk_mq_debugfs_rq_show, |
| 162 | }; |
| 163 | |
| 164 | static int ctx_rq_list_open(struct inode *inode, struct file *file) |
| 165 | { |
| 166 | return blk_mq_debugfs_seq_open(inode, file, &ctx_rq_list_seq_ops); |
| 167 | } |
| 168 | |
| 169 | static const struct file_operations ctx_rq_list_fops = { |
| 170 | .open = ctx_rq_list_open, |
| 171 | .read = seq_read, |
| 172 | .llseek = seq_lseek, |
| 173 | .release = seq_release, |
| 174 | }; |
| 175 | |
Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 176 | static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = { |
Omar Sandoval | 9abb2ad | 2017-01-25 08:06:41 -0800 | [diff] [blame] | 177 | {"state", 0400, &hctx_state_fops}, |
| 178 | {"flags", 0400, &hctx_flags_fops}, |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame^] | 179 | {"dispatch", 0400, &hctx_dispatch_fops}, |
Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = { |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame^] | 183 | {"rq_list", 0400, &ctx_rq_list_fops}, |
Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | int blk_mq_debugfs_register(struct request_queue *q, const char *name) |
| 187 | { |
| 188 | if (!block_debugfs_root) |
| 189 | return -ENOENT; |
| 190 | |
| 191 | q->debugfs_dir = debugfs_create_dir(name, block_debugfs_root); |
| 192 | if (!q->debugfs_dir) |
| 193 | goto err; |
| 194 | |
| 195 | if (blk_mq_debugfs_register_hctxs(q)) |
| 196 | goto err; |
| 197 | |
| 198 | return 0; |
| 199 | |
| 200 | err: |
| 201 | blk_mq_debugfs_unregister(q); |
| 202 | return -ENOMEM; |
| 203 | } |
| 204 | |
| 205 | void blk_mq_debugfs_unregister(struct request_queue *q) |
| 206 | { |
| 207 | debugfs_remove_recursive(q->debugfs_dir); |
| 208 | q->mq_debugfs_dir = NULL; |
| 209 | q->debugfs_dir = NULL; |
| 210 | } |
| 211 | |
| 212 | static int blk_mq_debugfs_register_ctx(struct request_queue *q, |
| 213 | struct blk_mq_ctx *ctx, |
| 214 | struct dentry *hctx_dir) |
| 215 | { |
| 216 | struct dentry *ctx_dir; |
| 217 | char name[20]; |
| 218 | int i; |
| 219 | |
| 220 | snprintf(name, sizeof(name), "cpu%u", ctx->cpu); |
| 221 | ctx_dir = debugfs_create_dir(name, hctx_dir); |
| 222 | if (!ctx_dir) |
| 223 | return -ENOMEM; |
| 224 | |
| 225 | for (i = 0; i < ARRAY_SIZE(blk_mq_debugfs_ctx_attrs); i++) { |
| 226 | const struct blk_mq_debugfs_attr *attr; |
| 227 | |
| 228 | attr = &blk_mq_debugfs_ctx_attrs[i]; |
| 229 | if (!debugfs_create_file(attr->name, attr->mode, ctx_dir, ctx, |
| 230 | attr->fops)) |
| 231 | return -ENOMEM; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int blk_mq_debugfs_register_hctx(struct request_queue *q, |
| 238 | struct blk_mq_hw_ctx *hctx) |
| 239 | { |
| 240 | struct blk_mq_ctx *ctx; |
| 241 | struct dentry *hctx_dir; |
| 242 | char name[20]; |
| 243 | int i; |
| 244 | |
| 245 | snprintf(name, sizeof(name), "%u", hctx->queue_num); |
| 246 | hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir); |
| 247 | if (!hctx_dir) |
| 248 | return -ENOMEM; |
| 249 | |
| 250 | for (i = 0; i < ARRAY_SIZE(blk_mq_debugfs_hctx_attrs); i++) { |
| 251 | const struct blk_mq_debugfs_attr *attr; |
| 252 | |
| 253 | attr = &blk_mq_debugfs_hctx_attrs[i]; |
| 254 | if (!debugfs_create_file(attr->name, attr->mode, hctx_dir, hctx, |
| 255 | attr->fops)) |
| 256 | return -ENOMEM; |
| 257 | } |
| 258 | |
| 259 | hctx_for_each_ctx(hctx, ctx, i) { |
| 260 | if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir)) |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int blk_mq_debugfs_register_hctxs(struct request_queue *q) |
| 268 | { |
| 269 | struct blk_mq_hw_ctx *hctx; |
| 270 | int i; |
| 271 | |
| 272 | if (!q->debugfs_dir) |
| 273 | return -ENOENT; |
| 274 | |
| 275 | q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir); |
| 276 | if (!q->mq_debugfs_dir) |
| 277 | goto err; |
| 278 | |
| 279 | queue_for_each_hw_ctx(q, hctx, i) { |
| 280 | if (blk_mq_debugfs_register_hctx(q, hctx)) |
| 281 | goto err; |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | |
| 286 | err: |
| 287 | blk_mq_debugfs_unregister_hctxs(q); |
| 288 | return -ENOMEM; |
| 289 | } |
| 290 | |
| 291 | void blk_mq_debugfs_unregister_hctxs(struct request_queue *q) |
| 292 | { |
| 293 | debugfs_remove_recursive(q->mq_debugfs_dir); |
| 294 | q->mq_debugfs_dir = NULL; |
| 295 | } |
| 296 | |
| 297 | void blk_mq_debugfs_init(void) |
| 298 | { |
| 299 | block_debugfs_root = debugfs_create_dir("block", NULL); |
| 300 | } |