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" |
Omar Sandoval | d96b37c | 2017-01-25 08:06:46 -0800 | [diff] [blame] | 23 | #include "blk-mq-tag.h" |
Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 24 | |
| 25 | struct blk_mq_debugfs_attr { |
| 26 | const char *name; |
| 27 | umode_t mode; |
| 28 | const struct file_operations *fops; |
| 29 | }; |
| 30 | |
| 31 | static struct dentry *block_debugfs_root; |
| 32 | |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame] | 33 | static int blk_mq_debugfs_seq_open(struct inode *inode, struct file *file, |
| 34 | const struct seq_operations *ops) |
| 35 | { |
| 36 | struct seq_file *m; |
| 37 | int ret; |
| 38 | |
| 39 | ret = seq_open(file, ops); |
| 40 | if (!ret) { |
| 41 | m = file->private_data; |
| 42 | m->private = inode->i_private; |
| 43 | } |
| 44 | return ret; |
| 45 | } |
| 46 | |
Omar Sandoval | 9abb2ad | 2017-01-25 08:06:41 -0800 | [diff] [blame] | 47 | static int hctx_state_show(struct seq_file *m, void *v) |
| 48 | { |
| 49 | struct blk_mq_hw_ctx *hctx = m->private; |
| 50 | |
| 51 | seq_printf(m, "0x%lx\n", hctx->state); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static int hctx_state_open(struct inode *inode, struct file *file) |
| 56 | { |
| 57 | return single_open(file, hctx_state_show, inode->i_private); |
| 58 | } |
| 59 | |
| 60 | static const struct file_operations hctx_state_fops = { |
| 61 | .open = hctx_state_open, |
| 62 | .read = seq_read, |
| 63 | .llseek = seq_lseek, |
| 64 | .release = single_release, |
| 65 | }; |
| 66 | |
| 67 | static int hctx_flags_show(struct seq_file *m, void *v) |
| 68 | { |
| 69 | struct blk_mq_hw_ctx *hctx = m->private; |
| 70 | |
| 71 | seq_printf(m, "0x%lx\n", hctx->flags); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int hctx_flags_open(struct inode *inode, struct file *file) |
| 76 | { |
| 77 | return single_open(file, hctx_flags_show, inode->i_private); |
| 78 | } |
| 79 | |
| 80 | static const struct file_operations hctx_flags_fops = { |
| 81 | .open = hctx_flags_open, |
| 82 | .read = seq_read, |
| 83 | .llseek = seq_lseek, |
| 84 | .release = single_release, |
| 85 | }; |
| 86 | |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame] | 87 | static int blk_mq_debugfs_rq_show(struct seq_file *m, void *v) |
| 88 | { |
| 89 | struct request *rq = list_entry_rq(v); |
| 90 | |
Omar Sandoval | 7b39385 | 2017-01-25 08:06:43 -0800 | [diff] [blame] | 91 | seq_printf(m, "%p {.cmd_type=%u, .cmd_flags=0x%x, .rq_flags=0x%x, .tag=%d, .internal_tag=%d}\n", |
| 92 | rq, rq->cmd_type, rq->cmd_flags, (unsigned int)rq->rq_flags, |
| 93 | rq->tag, rq->internal_tag); |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static void *hctx_dispatch_start(struct seq_file *m, loff_t *pos) |
| 98 | { |
| 99 | struct blk_mq_hw_ctx *hctx = m->private; |
| 100 | |
| 101 | spin_lock(&hctx->lock); |
| 102 | return seq_list_start(&hctx->dispatch, *pos); |
| 103 | } |
| 104 | |
| 105 | static void *hctx_dispatch_next(struct seq_file *m, void *v, loff_t *pos) |
| 106 | { |
| 107 | struct blk_mq_hw_ctx *hctx = m->private; |
| 108 | |
| 109 | return seq_list_next(v, &hctx->dispatch, pos); |
| 110 | } |
| 111 | |
| 112 | static void hctx_dispatch_stop(struct seq_file *m, void *v) |
| 113 | { |
| 114 | struct blk_mq_hw_ctx *hctx = m->private; |
| 115 | |
| 116 | spin_unlock(&hctx->lock); |
| 117 | } |
| 118 | |
| 119 | static const struct seq_operations hctx_dispatch_seq_ops = { |
| 120 | .start = hctx_dispatch_start, |
| 121 | .next = hctx_dispatch_next, |
| 122 | .stop = hctx_dispatch_stop, |
| 123 | .show = blk_mq_debugfs_rq_show, |
| 124 | }; |
| 125 | |
| 126 | static int hctx_dispatch_open(struct inode *inode, struct file *file) |
| 127 | { |
| 128 | return blk_mq_debugfs_seq_open(inode, file, &hctx_dispatch_seq_ops); |
| 129 | } |
| 130 | |
| 131 | static const struct file_operations hctx_dispatch_fops = { |
| 132 | .open = hctx_dispatch_open, |
| 133 | .read = seq_read, |
| 134 | .llseek = seq_lseek, |
| 135 | .release = seq_release, |
| 136 | }; |
| 137 | |
Omar Sandoval | 0bfa528 | 2017-01-25 08:06:45 -0800 | [diff] [blame] | 138 | static int hctx_ctx_map_show(struct seq_file *m, void *v) |
| 139 | { |
| 140 | struct blk_mq_hw_ctx *hctx = m->private; |
| 141 | |
| 142 | sbitmap_bitmap_show(&hctx->ctx_map, m); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int hctx_ctx_map_open(struct inode *inode, struct file *file) |
| 147 | { |
| 148 | return single_open(file, hctx_ctx_map_show, inode->i_private); |
| 149 | } |
| 150 | |
| 151 | static const struct file_operations hctx_ctx_map_fops = { |
| 152 | .open = hctx_ctx_map_open, |
| 153 | .read = seq_read, |
| 154 | .llseek = seq_lseek, |
| 155 | .release = single_release, |
| 156 | }; |
| 157 | |
Omar Sandoval | d96b37c | 2017-01-25 08:06:46 -0800 | [diff] [blame] | 158 | static void blk_mq_debugfs_tags_show(struct seq_file *m, |
| 159 | struct blk_mq_tags *tags) |
| 160 | { |
| 161 | seq_printf(m, "nr_tags=%u\n", tags->nr_tags); |
| 162 | seq_printf(m, "nr_reserved_tags=%u\n", tags->nr_reserved_tags); |
| 163 | seq_printf(m, "active_queues=%d\n", |
| 164 | atomic_read(&tags->active_queues)); |
| 165 | |
| 166 | seq_puts(m, "\nbitmap_tags:\n"); |
| 167 | sbitmap_queue_show(&tags->bitmap_tags, m); |
| 168 | |
| 169 | if (tags->nr_reserved_tags) { |
| 170 | seq_puts(m, "\nbreserved_tags:\n"); |
| 171 | sbitmap_queue_show(&tags->breserved_tags, m); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static int hctx_tags_show(struct seq_file *m, void *v) |
| 176 | { |
| 177 | struct blk_mq_hw_ctx *hctx = m->private; |
| 178 | struct request_queue *q = hctx->queue; |
| 179 | |
| 180 | mutex_lock(&q->sysfs_lock); |
| 181 | if (hctx->tags) |
| 182 | blk_mq_debugfs_tags_show(m, hctx->tags); |
| 183 | mutex_unlock(&q->sysfs_lock); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int hctx_tags_open(struct inode *inode, struct file *file) |
| 189 | { |
| 190 | return single_open(file, hctx_tags_show, inode->i_private); |
| 191 | } |
| 192 | |
| 193 | static const struct file_operations hctx_tags_fops = { |
| 194 | .open = hctx_tags_open, |
| 195 | .read = seq_read, |
| 196 | .llseek = seq_lseek, |
| 197 | .release = single_release, |
| 198 | }; |
| 199 | |
Omar Sandoval | d7e3621 | 2017-01-25 08:06:47 -0800 | [diff] [blame^] | 200 | static int hctx_tags_bitmap_show(struct seq_file *m, void *v) |
| 201 | { |
| 202 | struct blk_mq_hw_ctx *hctx = m->private; |
| 203 | struct request_queue *q = hctx->queue; |
| 204 | |
| 205 | mutex_lock(&q->sysfs_lock); |
| 206 | if (hctx->tags) |
| 207 | sbitmap_bitmap_show(&hctx->tags->bitmap_tags.sb, m); |
| 208 | mutex_unlock(&q->sysfs_lock); |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int hctx_tags_bitmap_open(struct inode *inode, struct file *file) |
| 213 | { |
| 214 | return single_open(file, hctx_tags_bitmap_show, inode->i_private); |
| 215 | } |
| 216 | |
| 217 | static const struct file_operations hctx_tags_bitmap_fops = { |
| 218 | .open = hctx_tags_bitmap_open, |
| 219 | .read = seq_read, |
| 220 | .llseek = seq_lseek, |
| 221 | .release = single_release, |
| 222 | }; |
| 223 | |
Omar Sandoval | d96b37c | 2017-01-25 08:06:46 -0800 | [diff] [blame] | 224 | static int hctx_sched_tags_show(struct seq_file *m, void *v) |
| 225 | { |
| 226 | struct blk_mq_hw_ctx *hctx = m->private; |
| 227 | struct request_queue *q = hctx->queue; |
| 228 | |
| 229 | mutex_lock(&q->sysfs_lock); |
| 230 | if (hctx->sched_tags) |
| 231 | blk_mq_debugfs_tags_show(m, hctx->sched_tags); |
| 232 | mutex_unlock(&q->sysfs_lock); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int hctx_sched_tags_open(struct inode *inode, struct file *file) |
| 238 | { |
| 239 | return single_open(file, hctx_sched_tags_show, inode->i_private); |
| 240 | } |
| 241 | |
| 242 | static const struct file_operations hctx_sched_tags_fops = { |
| 243 | .open = hctx_sched_tags_open, |
| 244 | .read = seq_read, |
| 245 | .llseek = seq_lseek, |
| 246 | .release = single_release, |
| 247 | }; |
| 248 | |
Omar Sandoval | d7e3621 | 2017-01-25 08:06:47 -0800 | [diff] [blame^] | 249 | static int hctx_sched_tags_bitmap_show(struct seq_file *m, void *v) |
| 250 | { |
| 251 | struct blk_mq_hw_ctx *hctx = m->private; |
| 252 | struct request_queue *q = hctx->queue; |
| 253 | |
| 254 | mutex_lock(&q->sysfs_lock); |
| 255 | if (hctx->sched_tags) |
| 256 | sbitmap_bitmap_show(&hctx->sched_tags->bitmap_tags.sb, m); |
| 257 | mutex_unlock(&q->sysfs_lock); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static int hctx_sched_tags_bitmap_open(struct inode *inode, struct file *file) |
| 262 | { |
| 263 | return single_open(file, hctx_sched_tags_bitmap_show, inode->i_private); |
| 264 | } |
| 265 | |
| 266 | static const struct file_operations hctx_sched_tags_bitmap_fops = { |
| 267 | .open = hctx_sched_tags_bitmap_open, |
| 268 | .read = seq_read, |
| 269 | .llseek = seq_lseek, |
| 270 | .release = single_release, |
| 271 | }; |
| 272 | |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame] | 273 | static void *ctx_rq_list_start(struct seq_file *m, loff_t *pos) |
| 274 | { |
| 275 | struct blk_mq_ctx *ctx = m->private; |
| 276 | |
| 277 | spin_lock(&ctx->lock); |
| 278 | return seq_list_start(&ctx->rq_list, *pos); |
| 279 | } |
| 280 | |
| 281 | static void *ctx_rq_list_next(struct seq_file *m, void *v, loff_t *pos) |
| 282 | { |
| 283 | struct blk_mq_ctx *ctx = m->private; |
| 284 | |
| 285 | return seq_list_next(v, &ctx->rq_list, pos); |
| 286 | } |
| 287 | |
| 288 | static void ctx_rq_list_stop(struct seq_file *m, void *v) |
| 289 | { |
| 290 | struct blk_mq_ctx *ctx = m->private; |
| 291 | |
| 292 | spin_unlock(&ctx->lock); |
| 293 | } |
| 294 | |
| 295 | static const struct seq_operations ctx_rq_list_seq_ops = { |
| 296 | .start = ctx_rq_list_start, |
| 297 | .next = ctx_rq_list_next, |
| 298 | .stop = ctx_rq_list_stop, |
| 299 | .show = blk_mq_debugfs_rq_show, |
| 300 | }; |
| 301 | |
| 302 | static int ctx_rq_list_open(struct inode *inode, struct file *file) |
| 303 | { |
| 304 | return blk_mq_debugfs_seq_open(inode, file, &ctx_rq_list_seq_ops); |
| 305 | } |
| 306 | |
| 307 | static const struct file_operations ctx_rq_list_fops = { |
| 308 | .open = ctx_rq_list_open, |
| 309 | .read = seq_read, |
| 310 | .llseek = seq_lseek, |
| 311 | .release = seq_release, |
| 312 | }; |
| 313 | |
Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 314 | static const struct blk_mq_debugfs_attr blk_mq_debugfs_hctx_attrs[] = { |
Omar Sandoval | 9abb2ad | 2017-01-25 08:06:41 -0800 | [diff] [blame] | 315 | {"state", 0400, &hctx_state_fops}, |
| 316 | {"flags", 0400, &hctx_flags_fops}, |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame] | 317 | {"dispatch", 0400, &hctx_dispatch_fops}, |
Omar Sandoval | 0bfa528 | 2017-01-25 08:06:45 -0800 | [diff] [blame] | 318 | {"ctx_map", 0400, &hctx_ctx_map_fops}, |
Omar Sandoval | d96b37c | 2017-01-25 08:06:46 -0800 | [diff] [blame] | 319 | {"tags", 0400, &hctx_tags_fops}, |
Omar Sandoval | d7e3621 | 2017-01-25 08:06:47 -0800 | [diff] [blame^] | 320 | {"tags_bitmap", 0400, &hctx_tags_bitmap_fops}, |
Omar Sandoval | d96b37c | 2017-01-25 08:06:46 -0800 | [diff] [blame] | 321 | {"sched_tags", 0400, &hctx_sched_tags_fops}, |
Omar Sandoval | d7e3621 | 2017-01-25 08:06:47 -0800 | [diff] [blame^] | 322 | {"sched_tags_bitmap", 0400, &hctx_sched_tags_bitmap_fops}, |
Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 323 | }; |
| 324 | |
| 325 | static const struct blk_mq_debugfs_attr blk_mq_debugfs_ctx_attrs[] = { |
Omar Sandoval | 950cd7e | 2017-01-25 08:06:42 -0800 | [diff] [blame] | 326 | {"rq_list", 0400, &ctx_rq_list_fops}, |
Omar Sandoval | 07e4fea | 2017-01-25 08:06:40 -0800 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | int blk_mq_debugfs_register(struct request_queue *q, const char *name) |
| 330 | { |
| 331 | if (!block_debugfs_root) |
| 332 | return -ENOENT; |
| 333 | |
| 334 | q->debugfs_dir = debugfs_create_dir(name, block_debugfs_root); |
| 335 | if (!q->debugfs_dir) |
| 336 | goto err; |
| 337 | |
| 338 | if (blk_mq_debugfs_register_hctxs(q)) |
| 339 | goto err; |
| 340 | |
| 341 | return 0; |
| 342 | |
| 343 | err: |
| 344 | blk_mq_debugfs_unregister(q); |
| 345 | return -ENOMEM; |
| 346 | } |
| 347 | |
| 348 | void blk_mq_debugfs_unregister(struct request_queue *q) |
| 349 | { |
| 350 | debugfs_remove_recursive(q->debugfs_dir); |
| 351 | q->mq_debugfs_dir = NULL; |
| 352 | q->debugfs_dir = NULL; |
| 353 | } |
| 354 | |
| 355 | static int blk_mq_debugfs_register_ctx(struct request_queue *q, |
| 356 | struct blk_mq_ctx *ctx, |
| 357 | struct dentry *hctx_dir) |
| 358 | { |
| 359 | struct dentry *ctx_dir; |
| 360 | char name[20]; |
| 361 | int i; |
| 362 | |
| 363 | snprintf(name, sizeof(name), "cpu%u", ctx->cpu); |
| 364 | ctx_dir = debugfs_create_dir(name, hctx_dir); |
| 365 | if (!ctx_dir) |
| 366 | return -ENOMEM; |
| 367 | |
| 368 | for (i = 0; i < ARRAY_SIZE(blk_mq_debugfs_ctx_attrs); i++) { |
| 369 | const struct blk_mq_debugfs_attr *attr; |
| 370 | |
| 371 | attr = &blk_mq_debugfs_ctx_attrs[i]; |
| 372 | if (!debugfs_create_file(attr->name, attr->mode, ctx_dir, ctx, |
| 373 | attr->fops)) |
| 374 | return -ENOMEM; |
| 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | static int blk_mq_debugfs_register_hctx(struct request_queue *q, |
| 381 | struct blk_mq_hw_ctx *hctx) |
| 382 | { |
| 383 | struct blk_mq_ctx *ctx; |
| 384 | struct dentry *hctx_dir; |
| 385 | char name[20]; |
| 386 | int i; |
| 387 | |
| 388 | snprintf(name, sizeof(name), "%u", hctx->queue_num); |
| 389 | hctx_dir = debugfs_create_dir(name, q->mq_debugfs_dir); |
| 390 | if (!hctx_dir) |
| 391 | return -ENOMEM; |
| 392 | |
| 393 | for (i = 0; i < ARRAY_SIZE(blk_mq_debugfs_hctx_attrs); i++) { |
| 394 | const struct blk_mq_debugfs_attr *attr; |
| 395 | |
| 396 | attr = &blk_mq_debugfs_hctx_attrs[i]; |
| 397 | if (!debugfs_create_file(attr->name, attr->mode, hctx_dir, hctx, |
| 398 | attr->fops)) |
| 399 | return -ENOMEM; |
| 400 | } |
| 401 | |
| 402 | hctx_for_each_ctx(hctx, ctx, i) { |
| 403 | if (blk_mq_debugfs_register_ctx(q, ctx, hctx_dir)) |
| 404 | return -ENOMEM; |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int blk_mq_debugfs_register_hctxs(struct request_queue *q) |
| 411 | { |
| 412 | struct blk_mq_hw_ctx *hctx; |
| 413 | int i; |
| 414 | |
| 415 | if (!q->debugfs_dir) |
| 416 | return -ENOENT; |
| 417 | |
| 418 | q->mq_debugfs_dir = debugfs_create_dir("mq", q->debugfs_dir); |
| 419 | if (!q->mq_debugfs_dir) |
| 420 | goto err; |
| 421 | |
| 422 | queue_for_each_hw_ctx(q, hctx, i) { |
| 423 | if (blk_mq_debugfs_register_hctx(q, hctx)) |
| 424 | goto err; |
| 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | |
| 429 | err: |
| 430 | blk_mq_debugfs_unregister_hctxs(q); |
| 431 | return -ENOMEM; |
| 432 | } |
| 433 | |
| 434 | void blk_mq_debugfs_unregister_hctxs(struct request_queue *q) |
| 435 | { |
| 436 | debugfs_remove_recursive(q->mq_debugfs_dir); |
| 437 | q->mq_debugfs_dir = NULL; |
| 438 | } |
| 439 | |
| 440 | void blk_mq_debugfs_init(void) |
| 441 | { |
| 442 | block_debugfs_root = debugfs_create_dir("block", NULL); |
| 443 | } |