1 /* 2 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * 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 11 * GNU 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, write to the Free Software 15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 16 * 17 */ 18 #include <linux/kernel.h> 19 #include <linux/blkdev.h> 20 #include <linux/blktrace_api.h> 21 #include <linux/percpu.h> 22 #include <linux/init.h> 23 #include <linux/mutex.h> 24 #include <linux/slab.h> 25 #include <linux/debugfs.h> 26 #include <linux/export.h> 27 #include <linux/time.h> 28 #include <linux/uaccess.h> 29 #include <linux/list.h> 30 #include <linux/blk-cgroup.h> 31 32 #include "../../block/blk.h" 33 34 #include <trace/events/block.h> 35 36 #include "trace_output.h" 37 38 #ifdef CONFIG_BLK_DEV_IO_TRACE 39 40 static unsigned int blktrace_seq __read_mostly = 1; 41 42 static struct trace_array *blk_tr; 43 static bool blk_tracer_enabled __read_mostly; 44 45 static LIST_HEAD(running_trace_list); 46 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock); 47 48 /* Select an alternative, minimalistic output than the original one */ 49 #define TRACE_BLK_OPT_CLASSIC 0x1 50 #define TRACE_BLK_OPT_CGROUP 0x2 51 #define TRACE_BLK_OPT_CGNAME 0x4 52 53 static struct tracer_opt blk_tracer_opts[] = { 54 /* Default disable the minimalistic output */ 55 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) }, 56 #ifdef CONFIG_BLK_CGROUP 57 { TRACER_OPT(blk_cgroup, TRACE_BLK_OPT_CGROUP) }, 58 { TRACER_OPT(blk_cgname, TRACE_BLK_OPT_CGNAME) }, 59 #endif 60 { } 61 }; 62 63 static struct tracer_flags blk_tracer_flags = { 64 .val = 0, 65 .opts = blk_tracer_opts, 66 }; 67 68 /* Global reference count of probes */ 69 static atomic_t blk_probes_ref = ATOMIC_INIT(0); 70 71 static void blk_register_tracepoints(void); 72 static void blk_unregister_tracepoints(void); 73 74 /* 75 * Send out a notify message. 76 */ 77 static void trace_note(struct blk_trace *bt, pid_t pid, int action, 78 const void *data, size_t len, 79 union kernfs_node_id *cgid) 80 { 81 struct blk_io_trace *t; 82 struct ring_buffer_event *event = NULL; 83 struct ring_buffer *buffer = NULL; 84 int pc = 0; 85 int cpu = smp_processor_id(); 86 bool blk_tracer = blk_tracer_enabled; 87 ssize_t cgid_len = cgid ? sizeof(*cgid) : 0; 88 89 if (blk_tracer) { 90 buffer = blk_tr->trace_buffer.buffer; 91 pc = preempt_count(); 92 event = trace_buffer_lock_reserve(buffer, TRACE_BLK, 93 sizeof(*t) + len + cgid_len, 94 0, pc); 95 if (!event) 96 return; 97 t = ring_buffer_event_data(event); 98 goto record_it; 99 } 100 101 if (!bt->rchan) 102 return; 103 104 t = relay_reserve(bt->rchan, sizeof(*t) + len + cgid_len); 105 if (t) { 106 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; 107 t->time = ktime_to_ns(ktime_get()); 108 record_it: 109 t->device = bt->dev; 110 t->action = action | (cgid ? __BLK_TN_CGROUP : 0); 111 t->pid = pid; 112 t->cpu = cpu; 113 t->pdu_len = len + cgid_len; 114 if (cgid) 115 memcpy((void *)t + sizeof(*t), cgid, cgid_len); 116 memcpy((void *) t + sizeof(*t) + cgid_len, data, len); 117 118 if (blk_tracer) 119 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc); 120 } 121 } 122 123 /* 124 * Send out a notify for this process, if we haven't done so since a trace 125 * started 126 */ 127 static void trace_note_tsk(struct task_struct *tsk) 128 { 129 unsigned long flags; 130 struct blk_trace *bt; 131 132 tsk->btrace_seq = blktrace_seq; 133 spin_lock_irqsave(&running_trace_lock, flags); 134 list_for_each_entry(bt, &running_trace_list, running_list) { 135 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, 136 sizeof(tsk->comm), NULL); 137 } 138 spin_unlock_irqrestore(&running_trace_lock, flags); 139 } 140 141 static void trace_note_time(struct blk_trace *bt) 142 { 143 struct timespec64 now; 144 unsigned long flags; 145 u32 words[2]; 146 147 /* need to check user space to see if this breaks in y2038 or y2106 */ 148 ktime_get_real_ts64(&now); 149 words[0] = (u32)now.tv_sec; 150 words[1] = now.tv_nsec; 151 152 local_irq_save(flags); 153 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words), NULL); 154 local_irq_restore(flags); 155 } 156 157 void __trace_note_message(struct blk_trace *bt, struct blkcg *blkcg, 158 const char *fmt, ...) 159 { 160 int n; 161 va_list args; 162 unsigned long flags; 163 char *buf; 164 165 if (unlikely(bt->trace_state != Blktrace_running && 166 !blk_tracer_enabled)) 167 return; 168 169 /* 170 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note 171 * message to the trace. 172 */ 173 if (!(bt->act_mask & BLK_TC_NOTIFY)) 174 return; 175 176 local_irq_save(flags); 177 buf = this_cpu_ptr(bt->msg_data); 178 va_start(args, fmt); 179 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args); 180 va_end(args); 181 182 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP)) 183 blkcg = NULL; 184 #ifdef CONFIG_BLK_CGROUP 185 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n, 186 blkcg ? cgroup_get_kernfs_id(blkcg->css.cgroup) : NULL); 187 #else 188 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n, NULL); 189 #endif 190 local_irq_restore(flags); 191 } 192 EXPORT_SYMBOL_GPL(__trace_note_message); 193 194 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector, 195 pid_t pid) 196 { 197 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0) 198 return 1; 199 if (sector && (sector < bt->start_lba || sector > bt->end_lba)) 200 return 1; 201 if (bt->pid && pid != bt->pid) 202 return 1; 203 204 return 0; 205 } 206 207 /* 208 * Data direction bit lookup 209 */ 210 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ), 211 BLK_TC_ACT(BLK_TC_WRITE) }; 212 213 #define BLK_TC_RAHEAD BLK_TC_AHEAD 214 #define BLK_TC_PREFLUSH BLK_TC_FLUSH 215 216 /* The ilog2() calls fall out because they're constant */ 217 #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \ 218 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name)) 219 220 /* 221 * The worker for the various blk_add_trace*() types. Fills out a 222 * blk_io_trace structure and places it in a per-cpu subbuffer. 223 */ 224 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, 225 int op, int op_flags, u32 what, int error, int pdu_len, 226 void *pdu_data, union kernfs_node_id *cgid) 227 { 228 struct task_struct *tsk = current; 229 struct ring_buffer_event *event = NULL; 230 struct ring_buffer *buffer = NULL; 231 struct blk_io_trace *t; 232 unsigned long flags = 0; 233 unsigned long *sequence; 234 pid_t pid; 235 int cpu, pc = 0; 236 bool blk_tracer = blk_tracer_enabled; 237 ssize_t cgid_len = cgid ? sizeof(*cgid) : 0; 238 239 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer)) 240 return; 241 242 what |= ddir_act[op_is_write(op) ? WRITE : READ]; 243 what |= MASK_TC_BIT(op_flags, SYNC); 244 what |= MASK_TC_BIT(op_flags, RAHEAD); 245 what |= MASK_TC_BIT(op_flags, META); 246 what |= MASK_TC_BIT(op_flags, PREFLUSH); 247 what |= MASK_TC_BIT(op_flags, FUA); 248 if (op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE) 249 what |= BLK_TC_ACT(BLK_TC_DISCARD); 250 if (op == REQ_OP_FLUSH) 251 what |= BLK_TC_ACT(BLK_TC_FLUSH); 252 if (cgid) 253 what |= __BLK_TA_CGROUP; 254 255 pid = tsk->pid; 256 if (act_log_check(bt, what, sector, pid)) 257 return; 258 cpu = raw_smp_processor_id(); 259 260 if (blk_tracer) { 261 tracing_record_cmdline(current); 262 263 buffer = blk_tr->trace_buffer.buffer; 264 pc = preempt_count(); 265 event = trace_buffer_lock_reserve(buffer, TRACE_BLK, 266 sizeof(*t) + pdu_len + cgid_len, 267 0, pc); 268 if (!event) 269 return; 270 t = ring_buffer_event_data(event); 271 goto record_it; 272 } 273 274 if (unlikely(tsk->btrace_seq != blktrace_seq)) 275 trace_note_tsk(tsk); 276 277 /* 278 * A word about the locking here - we disable interrupts to reserve 279 * some space in the relay per-cpu buffer, to prevent an irq 280 * from coming in and stepping on our toes. 281 */ 282 local_irq_save(flags); 283 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len + cgid_len); 284 if (t) { 285 sequence = per_cpu_ptr(bt->sequence, cpu); 286 287 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; 288 t->sequence = ++(*sequence); 289 t->time = ktime_to_ns(ktime_get()); 290 record_it: 291 /* 292 * These two are not needed in ftrace as they are in the 293 * generic trace_entry, filled by tracing_generic_entry_update, 294 * but for the trace_event->bin() synthesizer benefit we do it 295 * here too. 296 */ 297 t->cpu = cpu; 298 t->pid = pid; 299 300 t->sector = sector; 301 t->bytes = bytes; 302 t->action = what; 303 t->device = bt->dev; 304 t->error = error; 305 t->pdu_len = pdu_len + cgid_len; 306 307 if (cgid_len) 308 memcpy((void *)t + sizeof(*t), cgid, cgid_len); 309 if (pdu_len) 310 memcpy((void *)t + sizeof(*t) + cgid_len, pdu_data, pdu_len); 311 312 if (blk_tracer) { 313 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc); 314 return; 315 } 316 } 317 318 local_irq_restore(flags); 319 } 320 321 static void blk_trace_free(struct blk_trace *bt) 322 { 323 debugfs_remove(bt->msg_file); 324 debugfs_remove(bt->dropped_file); 325 relay_close(bt->rchan); 326 debugfs_remove(bt->dir); 327 free_percpu(bt->sequence); 328 free_percpu(bt->msg_data); 329 kfree(bt); 330 } 331 332 static void blk_trace_cleanup(struct blk_trace *bt) 333 { 334 blk_trace_free(bt); 335 if (atomic_dec_and_test(&blk_probes_ref)) 336 blk_unregister_tracepoints(); 337 } 338 339 int blk_trace_remove(struct request_queue *q) 340 { 341 struct blk_trace *bt; 342 343 bt = xchg(&q->blk_trace, NULL); 344 if (!bt) 345 return -EINVAL; 346 347 if (bt->trace_state != Blktrace_running) 348 blk_trace_cleanup(bt); 349 350 return 0; 351 } 352 EXPORT_SYMBOL_GPL(blk_trace_remove); 353 354 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer, 355 size_t count, loff_t *ppos) 356 { 357 struct blk_trace *bt = filp->private_data; 358 char buf[16]; 359 360 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped)); 361 362 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf)); 363 } 364 365 static const struct file_operations blk_dropped_fops = { 366 .owner = THIS_MODULE, 367 .open = simple_open, 368 .read = blk_dropped_read, 369 .llseek = default_llseek, 370 }; 371 372 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer, 373 size_t count, loff_t *ppos) 374 { 375 char *msg; 376 struct blk_trace *bt; 377 378 if (count >= BLK_TN_MAX_MSG) 379 return -EINVAL; 380 381 msg = memdup_user_nul(buffer, count); 382 if (IS_ERR(msg)) 383 return PTR_ERR(msg); 384 385 bt = filp->private_data; 386 __trace_note_message(bt, NULL, "%s", msg); 387 kfree(msg); 388 389 return count; 390 } 391 392 static const struct file_operations blk_msg_fops = { 393 .owner = THIS_MODULE, 394 .open = simple_open, 395 .write = blk_msg_write, 396 .llseek = noop_llseek, 397 }; 398 399 /* 400 * Keep track of how many times we encountered a full subbuffer, to aid 401 * the user space app in telling how many lost events there were. 402 */ 403 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf, 404 void *prev_subbuf, size_t prev_padding) 405 { 406 struct blk_trace *bt; 407 408 if (!relay_buf_full(buf)) 409 return 1; 410 411 bt = buf->chan->private_data; 412 atomic_inc(&bt->dropped); 413 return 0; 414 } 415 416 static int blk_remove_buf_file_callback(struct dentry *dentry) 417 { 418 debugfs_remove(dentry); 419 420 return 0; 421 } 422 423 static struct dentry *blk_create_buf_file_callback(const char *filename, 424 struct dentry *parent, 425 umode_t mode, 426 struct rchan_buf *buf, 427 int *is_global) 428 { 429 return debugfs_create_file(filename, mode, parent, buf, 430 &relay_file_operations); 431 } 432 433 static struct rchan_callbacks blk_relay_callbacks = { 434 .subbuf_start = blk_subbuf_start_callback, 435 .create_buf_file = blk_create_buf_file_callback, 436 .remove_buf_file = blk_remove_buf_file_callback, 437 }; 438 439 static void blk_trace_setup_lba(struct blk_trace *bt, 440 struct block_device *bdev) 441 { 442 struct hd_struct *part = NULL; 443 444 if (bdev) 445 part = bdev->bd_part; 446 447 if (part) { 448 bt->start_lba = part->start_sect; 449 bt->end_lba = part->start_sect + part->nr_sects; 450 } else { 451 bt->start_lba = 0; 452 bt->end_lba = -1ULL; 453 } 454 } 455 456 /* 457 * Setup everything required to start tracing 458 */ 459 static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev, 460 struct block_device *bdev, 461 struct blk_user_trace_setup *buts) 462 { 463 struct blk_trace *bt = NULL; 464 struct dentry *dir = NULL; 465 int ret; 466 467 if (!buts->buf_size || !buts->buf_nr) 468 return -EINVAL; 469 470 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE); 471 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0'; 472 473 /* 474 * some device names have larger paths - convert the slashes 475 * to underscores for this to work as expected 476 */ 477 strreplace(buts->name, '/', '_'); 478 479 bt = kzalloc(sizeof(*bt), GFP_KERNEL); 480 if (!bt) 481 return -ENOMEM; 482 483 ret = -ENOMEM; 484 bt->sequence = alloc_percpu(unsigned long); 485 if (!bt->sequence) 486 goto err; 487 488 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char)); 489 if (!bt->msg_data) 490 goto err; 491 492 ret = -ENOENT; 493 494 if (!blk_debugfs_root) 495 goto err; 496 497 dir = debugfs_lookup(buts->name, blk_debugfs_root); 498 if (!dir) 499 bt->dir = dir = debugfs_create_dir(buts->name, blk_debugfs_root); 500 if (!dir) 501 goto err; 502 503 bt->dev = dev; 504 atomic_set(&bt->dropped, 0); 505 INIT_LIST_HEAD(&bt->running_list); 506 507 ret = -EIO; 508 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, 509 &blk_dropped_fops); 510 if (!bt->dropped_file) 511 goto err; 512 513 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops); 514 if (!bt->msg_file) 515 goto err; 516 517 bt->rchan = relay_open("trace", dir, buts->buf_size, 518 buts->buf_nr, &blk_relay_callbacks, bt); 519 if (!bt->rchan) 520 goto err; 521 522 bt->act_mask = buts->act_mask; 523 if (!bt->act_mask) 524 bt->act_mask = (u16) -1; 525 526 blk_trace_setup_lba(bt, bdev); 527 528 /* overwrite with user settings */ 529 if (buts->start_lba) 530 bt->start_lba = buts->start_lba; 531 if (buts->end_lba) 532 bt->end_lba = buts->end_lba; 533 534 bt->pid = buts->pid; 535 bt->trace_state = Blktrace_setup; 536 537 ret = -EBUSY; 538 if (cmpxchg(&q->blk_trace, NULL, bt)) 539 goto err; 540 541 if (atomic_inc_return(&blk_probes_ref) == 1) 542 blk_register_tracepoints(); 543 544 ret = 0; 545 err: 546 if (dir && !bt->dir) 547 dput(dir); 548 if (ret) 549 blk_trace_free(bt); 550 return ret; 551 } 552 553 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev, 554 struct block_device *bdev, 555 char __user *arg) 556 { 557 struct blk_user_trace_setup buts; 558 int ret; 559 560 ret = copy_from_user(&buts, arg, sizeof(buts)); 561 if (ret) 562 return -EFAULT; 563 564 ret = do_blk_trace_setup(q, name, dev, bdev, &buts); 565 if (ret) 566 return ret; 567 568 if (copy_to_user(arg, &buts, sizeof(buts))) { 569 blk_trace_remove(q); 570 return -EFAULT; 571 } 572 return 0; 573 } 574 EXPORT_SYMBOL_GPL(blk_trace_setup); 575 576 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64) 577 static int compat_blk_trace_setup(struct request_queue *q, char *name, 578 dev_t dev, struct block_device *bdev, 579 char __user *arg) 580 { 581 struct blk_user_trace_setup buts; 582 struct compat_blk_user_trace_setup cbuts; 583 int ret; 584 585 if (copy_from_user(&cbuts, arg, sizeof(cbuts))) 586 return -EFAULT; 587 588 buts = (struct blk_user_trace_setup) { 589 .act_mask = cbuts.act_mask, 590 .buf_size = cbuts.buf_size, 591 .buf_nr = cbuts.buf_nr, 592 .start_lba = cbuts.start_lba, 593 .end_lba = cbuts.end_lba, 594 .pid = cbuts.pid, 595 }; 596 597 ret = do_blk_trace_setup(q, name, dev, bdev, &buts); 598 if (ret) 599 return ret; 600 601 if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) { 602 blk_trace_remove(q); 603 return -EFAULT; 604 } 605 606 return 0; 607 } 608 #endif 609 610 int blk_trace_startstop(struct request_queue *q, int start) 611 { 612 int ret; 613 struct blk_trace *bt = q->blk_trace; 614 615 if (bt == NULL) 616 return -EINVAL; 617 618 /* 619 * For starting a trace, we can transition from a setup or stopped 620 * trace. For stopping a trace, the state must be running 621 */ 622 ret = -EINVAL; 623 if (start) { 624 if (bt->trace_state == Blktrace_setup || 625 bt->trace_state == Blktrace_stopped) { 626 blktrace_seq++; 627 smp_mb(); 628 bt->trace_state = Blktrace_running; 629 spin_lock_irq(&running_trace_lock); 630 list_add(&bt->running_list, &running_trace_list); 631 spin_unlock_irq(&running_trace_lock); 632 633 trace_note_time(bt); 634 ret = 0; 635 } 636 } else { 637 if (bt->trace_state == Blktrace_running) { 638 bt->trace_state = Blktrace_stopped; 639 spin_lock_irq(&running_trace_lock); 640 list_del_init(&bt->running_list); 641 spin_unlock_irq(&running_trace_lock); 642 relay_flush(bt->rchan); 643 ret = 0; 644 } 645 } 646 647 return ret; 648 } 649 EXPORT_SYMBOL_GPL(blk_trace_startstop); 650 651 /** 652 * blk_trace_ioctl: - handle the ioctls associated with tracing 653 * @bdev: the block device 654 * @cmd: the ioctl cmd 655 * @arg: the argument data, if any 656 * 657 **/ 658 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg) 659 { 660 struct request_queue *q; 661 int ret, start = 0; 662 char b[BDEVNAME_SIZE]; 663 664 q = bdev_get_queue(bdev); 665 if (!q) 666 return -ENXIO; 667 668 mutex_lock(&bdev->bd_mutex); 669 670 switch (cmd) { 671 case BLKTRACESETUP: 672 bdevname(bdev, b); 673 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg); 674 break; 675 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64) 676 case BLKTRACESETUP32: 677 bdevname(bdev, b); 678 ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg); 679 break; 680 #endif 681 case BLKTRACESTART: 682 start = 1; 683 case BLKTRACESTOP: 684 ret = blk_trace_startstop(q, start); 685 break; 686 case BLKTRACETEARDOWN: 687 ret = blk_trace_remove(q); 688 break; 689 default: 690 ret = -ENOTTY; 691 break; 692 } 693 694 mutex_unlock(&bdev->bd_mutex); 695 return ret; 696 } 697 698 /** 699 * blk_trace_shutdown: - stop and cleanup trace structures 700 * @q: the request queue associated with the device 701 * 702 **/ 703 void blk_trace_shutdown(struct request_queue *q) 704 { 705 if (q->blk_trace) { 706 blk_trace_startstop(q, 0); 707 blk_trace_remove(q); 708 } 709 } 710 711 #ifdef CONFIG_BLK_CGROUP 712 static union kernfs_node_id * 713 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio) 714 { 715 struct blk_trace *bt = q->blk_trace; 716 717 if (!bt || !(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP)) 718 return NULL; 719 720 if (!bio->bi_css) 721 return NULL; 722 return cgroup_get_kernfs_id(bio->bi_css->cgroup); 723 } 724 #else 725 static union kernfs_node_id * 726 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio) 727 { 728 return NULL; 729 } 730 #endif 731 732 static union kernfs_node_id * 733 blk_trace_request_get_cgid(struct request_queue *q, struct request *rq) 734 { 735 if (!rq->bio) 736 return NULL; 737 /* Use the first bio */ 738 return blk_trace_bio_get_cgid(q, rq->bio); 739 } 740 741 /* 742 * blktrace probes 743 */ 744 745 /** 746 * blk_add_trace_rq - Add a trace for a request oriented action 747 * @rq: the source request 748 * @error: return status to log 749 * @nr_bytes: number of completed bytes 750 * @what: the action 751 * @cgid: the cgroup info 752 * 753 * Description: 754 * Records an action against a request. Will log the bio offset + size. 755 * 756 **/ 757 static void blk_add_trace_rq(struct request *rq, int error, 758 unsigned int nr_bytes, u32 what, 759 union kernfs_node_id *cgid) 760 { 761 struct blk_trace *bt = rq->q->blk_trace; 762 763 if (likely(!bt)) 764 return; 765 766 if (blk_rq_is_passthrough(rq)) 767 what |= BLK_TC_ACT(BLK_TC_PC); 768 else 769 what |= BLK_TC_ACT(BLK_TC_FS); 770 771 __blk_add_trace(bt, blk_rq_trace_sector(rq), nr_bytes, req_op(rq), 772 rq->cmd_flags, what, error, 0, NULL, cgid); 773 } 774 775 static void blk_add_trace_rq_insert(void *ignore, 776 struct request_queue *q, struct request *rq) 777 { 778 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_INSERT, 779 blk_trace_request_get_cgid(q, rq)); 780 } 781 782 static void blk_add_trace_rq_issue(void *ignore, 783 struct request_queue *q, struct request *rq) 784 { 785 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_ISSUE, 786 blk_trace_request_get_cgid(q, rq)); 787 } 788 789 static void blk_add_trace_rq_requeue(void *ignore, 790 struct request_queue *q, 791 struct request *rq) 792 { 793 blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_REQUEUE, 794 blk_trace_request_get_cgid(q, rq)); 795 } 796 797 static void blk_add_trace_rq_complete(void *ignore, struct request *rq, 798 int error, unsigned int nr_bytes) 799 { 800 blk_add_trace_rq(rq, error, nr_bytes, BLK_TA_COMPLETE, 801 blk_trace_request_get_cgid(rq->q, rq)); 802 } 803 804 /** 805 * blk_add_trace_bio - Add a trace for a bio oriented action 806 * @q: queue the io is for 807 * @bio: the source bio 808 * @what: the action 809 * @error: error, if any 810 * 811 * Description: 812 * Records an action against a bio. Will log the bio offset + size. 813 * 814 **/ 815 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio, 816 u32 what, int error, union kernfs_node_id *cgid) 817 { 818 struct blk_trace *bt = q->blk_trace; 819 820 if (likely(!bt)) 821 return; 822 823 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size, 824 bio_op(bio), bio->bi_opf, what, error, 0, NULL, cgid); 825 } 826 827 static void blk_add_trace_bio_bounce(void *ignore, 828 struct request_queue *q, struct bio *bio) 829 { 830 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0, 831 blk_trace_bio_get_cgid(q, bio)); 832 } 833 834 static void blk_add_trace_bio_complete(void *ignore, 835 struct request_queue *q, struct bio *bio, 836 int error) 837 { 838 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, error, 839 blk_trace_bio_get_cgid(q, bio)); 840 } 841 842 static void blk_add_trace_bio_backmerge(void *ignore, 843 struct request_queue *q, 844 struct request *rq, 845 struct bio *bio) 846 { 847 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0, 848 blk_trace_bio_get_cgid(q, bio)); 849 } 850 851 static void blk_add_trace_bio_frontmerge(void *ignore, 852 struct request_queue *q, 853 struct request *rq, 854 struct bio *bio) 855 { 856 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0, 857 blk_trace_bio_get_cgid(q, bio)); 858 } 859 860 static void blk_add_trace_bio_queue(void *ignore, 861 struct request_queue *q, struct bio *bio) 862 { 863 blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0, 864 blk_trace_bio_get_cgid(q, bio)); 865 } 866 867 static void blk_add_trace_getrq(void *ignore, 868 struct request_queue *q, 869 struct bio *bio, int rw) 870 { 871 if (bio) 872 blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0, 873 blk_trace_bio_get_cgid(q, bio)); 874 else { 875 struct blk_trace *bt = q->blk_trace; 876 877 if (bt) 878 __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_GETRQ, 0, 0, 879 NULL, NULL); 880 } 881 } 882 883 884 static void blk_add_trace_sleeprq(void *ignore, 885 struct request_queue *q, 886 struct bio *bio, int rw) 887 { 888 if (bio) 889 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0, 890 blk_trace_bio_get_cgid(q, bio)); 891 else { 892 struct blk_trace *bt = q->blk_trace; 893 894 if (bt) 895 __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_SLEEPRQ, 896 0, 0, NULL, NULL); 897 } 898 } 899 900 static void blk_add_trace_plug(void *ignore, struct request_queue *q) 901 { 902 struct blk_trace *bt = q->blk_trace; 903 904 if (bt) 905 __blk_add_trace(bt, 0, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL, NULL); 906 } 907 908 static void blk_add_trace_unplug(void *ignore, struct request_queue *q, 909 unsigned int depth, bool explicit) 910 { 911 struct blk_trace *bt = q->blk_trace; 912 913 if (bt) { 914 __be64 rpdu = cpu_to_be64(depth); 915 u32 what; 916 917 if (explicit) 918 what = BLK_TA_UNPLUG_IO; 919 else 920 what = BLK_TA_UNPLUG_TIMER; 921 922 __blk_add_trace(bt, 0, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu, NULL); 923 } 924 } 925 926 static void blk_add_trace_split(void *ignore, 927 struct request_queue *q, struct bio *bio, 928 unsigned int pdu) 929 { 930 struct blk_trace *bt = q->blk_trace; 931 932 if (bt) { 933 __be64 rpdu = cpu_to_be64(pdu); 934 935 __blk_add_trace(bt, bio->bi_iter.bi_sector, 936 bio->bi_iter.bi_size, bio_op(bio), bio->bi_opf, 937 BLK_TA_SPLIT, bio->bi_status, sizeof(rpdu), 938 &rpdu, blk_trace_bio_get_cgid(q, bio)); 939 } 940 } 941 942 /** 943 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation 944 * @ignore: trace callback data parameter (not used) 945 * @q: queue the io is for 946 * @bio: the source bio 947 * @dev: target device 948 * @from: source sector 949 * 950 * Description: 951 * Device mapper or raid target sometimes need to split a bio because 952 * it spans a stripe (or similar). Add a trace for that action. 953 * 954 **/ 955 static void blk_add_trace_bio_remap(void *ignore, 956 struct request_queue *q, struct bio *bio, 957 dev_t dev, sector_t from) 958 { 959 struct blk_trace *bt = q->blk_trace; 960 struct blk_io_trace_remap r; 961 962 if (likely(!bt)) 963 return; 964 965 r.device_from = cpu_to_be32(dev); 966 r.device_to = cpu_to_be32(bio_dev(bio)); 967 r.sector_from = cpu_to_be64(from); 968 969 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size, 970 bio_op(bio), bio->bi_opf, BLK_TA_REMAP, bio->bi_status, 971 sizeof(r), &r, blk_trace_bio_get_cgid(q, bio)); 972 } 973 974 /** 975 * blk_add_trace_rq_remap - Add a trace for a request-remap operation 976 * @ignore: trace callback data parameter (not used) 977 * @q: queue the io is for 978 * @rq: the source request 979 * @dev: target device 980 * @from: source sector 981 * 982 * Description: 983 * Device mapper remaps request to other devices. 984 * Add a trace for that action. 985 * 986 **/ 987 static void blk_add_trace_rq_remap(void *ignore, 988 struct request_queue *q, 989 struct request *rq, dev_t dev, 990 sector_t from) 991 { 992 struct blk_trace *bt = q->blk_trace; 993 struct blk_io_trace_remap r; 994 995 if (likely(!bt)) 996 return; 997 998 r.device_from = cpu_to_be32(dev); 999 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk)); 1000 r.sector_from = cpu_to_be64(from); 1001 1002 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 1003 rq_data_dir(rq), 0, BLK_TA_REMAP, 0, 1004 sizeof(r), &r, blk_trace_request_get_cgid(q, rq)); 1005 } 1006 1007 /** 1008 * blk_add_driver_data - Add binary message with driver-specific data 1009 * @q: queue the io is for 1010 * @rq: io request 1011 * @data: driver-specific data 1012 * @len: length of driver-specific data 1013 * 1014 * Description: 1015 * Some drivers might want to write driver-specific data per request. 1016 * 1017 **/ 1018 void blk_add_driver_data(struct request_queue *q, 1019 struct request *rq, 1020 void *data, size_t len) 1021 { 1022 struct blk_trace *bt = q->blk_trace; 1023 1024 if (likely(!bt)) 1025 return; 1026 1027 __blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0, 0, 1028 BLK_TA_DRV_DATA, 0, len, data, 1029 blk_trace_request_get_cgid(q, rq)); 1030 } 1031 EXPORT_SYMBOL_GPL(blk_add_driver_data); 1032 1033 static void blk_register_tracepoints(void) 1034 { 1035 int ret; 1036 1037 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL); 1038 WARN_ON(ret); 1039 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL); 1040 WARN_ON(ret); 1041 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL); 1042 WARN_ON(ret); 1043 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL); 1044 WARN_ON(ret); 1045 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL); 1046 WARN_ON(ret); 1047 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL); 1048 WARN_ON(ret); 1049 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL); 1050 WARN_ON(ret); 1051 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL); 1052 WARN_ON(ret); 1053 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL); 1054 WARN_ON(ret); 1055 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL); 1056 WARN_ON(ret); 1057 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL); 1058 WARN_ON(ret); 1059 ret = register_trace_block_plug(blk_add_trace_plug, NULL); 1060 WARN_ON(ret); 1061 ret = register_trace_block_unplug(blk_add_trace_unplug, NULL); 1062 WARN_ON(ret); 1063 ret = register_trace_block_split(blk_add_trace_split, NULL); 1064 WARN_ON(ret); 1065 ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL); 1066 WARN_ON(ret); 1067 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL); 1068 WARN_ON(ret); 1069 } 1070 1071 static void blk_unregister_tracepoints(void) 1072 { 1073 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL); 1074 unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL); 1075 unregister_trace_block_split(blk_add_trace_split, NULL); 1076 unregister_trace_block_unplug(blk_add_trace_unplug, NULL); 1077 unregister_trace_block_plug(blk_add_trace_plug, NULL); 1078 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL); 1079 unregister_trace_block_getrq(blk_add_trace_getrq, NULL); 1080 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL); 1081 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL); 1082 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL); 1083 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL); 1084 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL); 1085 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL); 1086 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL); 1087 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL); 1088 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL); 1089 1090 tracepoint_synchronize_unregister(); 1091 } 1092 1093 /* 1094 * struct blk_io_tracer formatting routines 1095 */ 1096 1097 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t) 1098 { 1099 int i = 0; 1100 int tc = t->action >> BLK_TC_SHIFT; 1101 1102 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) { 1103 rwbs[i++] = 'N'; 1104 goto out; 1105 } 1106 1107 if (tc & BLK_TC_FLUSH) 1108 rwbs[i++] = 'F'; 1109 1110 if (tc & BLK_TC_DISCARD) 1111 rwbs[i++] = 'D'; 1112 else if (tc & BLK_TC_WRITE) 1113 rwbs[i++] = 'W'; 1114 else if (t->bytes) 1115 rwbs[i++] = 'R'; 1116 else 1117 rwbs[i++] = 'N'; 1118 1119 if (tc & BLK_TC_FUA) 1120 rwbs[i++] = 'F'; 1121 if (tc & BLK_TC_AHEAD) 1122 rwbs[i++] = 'A'; 1123 if (tc & BLK_TC_SYNC) 1124 rwbs[i++] = 'S'; 1125 if (tc & BLK_TC_META) 1126 rwbs[i++] = 'M'; 1127 out: 1128 rwbs[i] = '\0'; 1129 } 1130 1131 static inline 1132 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent) 1133 { 1134 return (const struct blk_io_trace *)ent; 1135 } 1136 1137 static inline const void *pdu_start(const struct trace_entry *ent, bool has_cg) 1138 { 1139 return (void *)(te_blk_io_trace(ent) + 1) + 1140 (has_cg ? sizeof(union kernfs_node_id) : 0); 1141 } 1142 1143 static inline const void *cgid_start(const struct trace_entry *ent) 1144 { 1145 return (void *)(te_blk_io_trace(ent) + 1); 1146 } 1147 1148 static inline int pdu_real_len(const struct trace_entry *ent, bool has_cg) 1149 { 1150 return te_blk_io_trace(ent)->pdu_len - 1151 (has_cg ? sizeof(union kernfs_node_id) : 0); 1152 } 1153 1154 static inline u32 t_action(const struct trace_entry *ent) 1155 { 1156 return te_blk_io_trace(ent)->action; 1157 } 1158 1159 static inline u32 t_bytes(const struct trace_entry *ent) 1160 { 1161 return te_blk_io_trace(ent)->bytes; 1162 } 1163 1164 static inline u32 t_sec(const struct trace_entry *ent) 1165 { 1166 return te_blk_io_trace(ent)->bytes >> 9; 1167 } 1168 1169 static inline unsigned long long t_sector(const struct trace_entry *ent) 1170 { 1171 return te_blk_io_trace(ent)->sector; 1172 } 1173 1174 static inline __u16 t_error(const struct trace_entry *ent) 1175 { 1176 return te_blk_io_trace(ent)->error; 1177 } 1178 1179 static __u64 get_pdu_int(const struct trace_entry *ent, bool has_cg) 1180 { 1181 const __u64 *val = pdu_start(ent, has_cg); 1182 return be64_to_cpu(*val); 1183 } 1184 1185 static void get_pdu_remap(const struct trace_entry *ent, 1186 struct blk_io_trace_remap *r, bool has_cg) 1187 { 1188 const struct blk_io_trace_remap *__r = pdu_start(ent, has_cg); 1189 __u64 sector_from = __r->sector_from; 1190 1191 r->device_from = be32_to_cpu(__r->device_from); 1192 r->device_to = be32_to_cpu(__r->device_to); 1193 r->sector_from = be64_to_cpu(sector_from); 1194 } 1195 1196 typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act, 1197 bool has_cg); 1198 1199 static void blk_log_action_classic(struct trace_iterator *iter, const char *act, 1200 bool has_cg) 1201 { 1202 char rwbs[RWBS_LEN]; 1203 unsigned long long ts = iter->ts; 1204 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC); 1205 unsigned secs = (unsigned long)ts; 1206 const struct blk_io_trace *t = te_blk_io_trace(iter->ent); 1207 1208 fill_rwbs(rwbs, t); 1209 1210 trace_seq_printf(&iter->seq, 1211 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ", 1212 MAJOR(t->device), MINOR(t->device), iter->cpu, 1213 secs, nsec_rem, iter->ent->pid, act, rwbs); 1214 } 1215 1216 static void blk_log_action(struct trace_iterator *iter, const char *act, 1217 bool has_cg) 1218 { 1219 char rwbs[RWBS_LEN]; 1220 const struct blk_io_trace *t = te_blk_io_trace(iter->ent); 1221 1222 fill_rwbs(rwbs, t); 1223 if (has_cg) { 1224 const union kernfs_node_id *id = cgid_start(iter->ent); 1225 1226 if (blk_tracer_flags.val & TRACE_BLK_OPT_CGNAME) { 1227 char blkcg_name_buf[NAME_MAX + 1] = "<...>"; 1228 1229 cgroup_path_from_kernfs_id(id, blkcg_name_buf, 1230 sizeof(blkcg_name_buf)); 1231 trace_seq_printf(&iter->seq, "%3d,%-3d %s %2s %3s ", 1232 MAJOR(t->device), MINOR(t->device), 1233 blkcg_name_buf, act, rwbs); 1234 } else 1235 trace_seq_printf(&iter->seq, 1236 "%3d,%-3d %x,%-x %2s %3s ", 1237 MAJOR(t->device), MINOR(t->device), 1238 id->ino, id->generation, act, rwbs); 1239 } else 1240 trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ", 1241 MAJOR(t->device), MINOR(t->device), act, rwbs); 1242 } 1243 1244 static void blk_log_dump_pdu(struct trace_seq *s, 1245 const struct trace_entry *ent, bool has_cg) 1246 { 1247 const unsigned char *pdu_buf; 1248 int pdu_len; 1249 int i, end; 1250 1251 pdu_buf = pdu_start(ent, has_cg); 1252 pdu_len = pdu_real_len(ent, has_cg); 1253 1254 if (!pdu_len) 1255 return; 1256 1257 /* find the last zero that needs to be printed */ 1258 for (end = pdu_len - 1; end >= 0; end--) 1259 if (pdu_buf[end]) 1260 break; 1261 end++; 1262 1263 trace_seq_putc(s, '('); 1264 1265 for (i = 0; i < pdu_len; i++) { 1266 1267 trace_seq_printf(s, "%s%02x", 1268 i == 0 ? "" : " ", pdu_buf[i]); 1269 1270 /* 1271 * stop when the rest is just zeroes and indicate so 1272 * with a ".." appended 1273 */ 1274 if (i == end && end != pdu_len - 1) { 1275 trace_seq_puts(s, " ..) "); 1276 return; 1277 } 1278 } 1279 1280 trace_seq_puts(s, ") "); 1281 } 1282 1283 static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) 1284 { 1285 char cmd[TASK_COMM_LEN]; 1286 1287 trace_find_cmdline(ent->pid, cmd); 1288 1289 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) { 1290 trace_seq_printf(s, "%u ", t_bytes(ent)); 1291 blk_log_dump_pdu(s, ent, has_cg); 1292 trace_seq_printf(s, "[%s]\n", cmd); 1293 } else { 1294 if (t_sec(ent)) 1295 trace_seq_printf(s, "%llu + %u [%s]\n", 1296 t_sector(ent), t_sec(ent), cmd); 1297 else 1298 trace_seq_printf(s, "[%s]\n", cmd); 1299 } 1300 } 1301 1302 static void blk_log_with_error(struct trace_seq *s, 1303 const struct trace_entry *ent, bool has_cg) 1304 { 1305 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) { 1306 blk_log_dump_pdu(s, ent, has_cg); 1307 trace_seq_printf(s, "[%d]\n", t_error(ent)); 1308 } else { 1309 if (t_sec(ent)) 1310 trace_seq_printf(s, "%llu + %u [%d]\n", 1311 t_sector(ent), 1312 t_sec(ent), t_error(ent)); 1313 else 1314 trace_seq_printf(s, "%llu [%d]\n", 1315 t_sector(ent), t_error(ent)); 1316 } 1317 } 1318 1319 static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) 1320 { 1321 struct blk_io_trace_remap r = { .device_from = 0, }; 1322 1323 get_pdu_remap(ent, &r, has_cg); 1324 trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n", 1325 t_sector(ent), t_sec(ent), 1326 MAJOR(r.device_from), MINOR(r.device_from), 1327 (unsigned long long)r.sector_from); 1328 } 1329 1330 static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) 1331 { 1332 char cmd[TASK_COMM_LEN]; 1333 1334 trace_find_cmdline(ent->pid, cmd); 1335 1336 trace_seq_printf(s, "[%s]\n", cmd); 1337 } 1338 1339 static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) 1340 { 1341 char cmd[TASK_COMM_LEN]; 1342 1343 trace_find_cmdline(ent->pid, cmd); 1344 1345 trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent, has_cg)); 1346 } 1347 1348 static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) 1349 { 1350 char cmd[TASK_COMM_LEN]; 1351 1352 trace_find_cmdline(ent->pid, cmd); 1353 1354 trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent), 1355 get_pdu_int(ent, has_cg), cmd); 1356 } 1357 1358 static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent, 1359 bool has_cg) 1360 { 1361 1362 trace_seq_putmem(s, pdu_start(ent, has_cg), 1363 pdu_real_len(ent, has_cg)); 1364 trace_seq_putc(s, '\n'); 1365 } 1366 1367 /* 1368 * struct tracer operations 1369 */ 1370 1371 static void blk_tracer_print_header(struct seq_file *m) 1372 { 1373 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) 1374 return; 1375 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n" 1376 "# | | | | | |\n"); 1377 } 1378 1379 static void blk_tracer_start(struct trace_array *tr) 1380 { 1381 blk_tracer_enabled = true; 1382 } 1383 1384 static int blk_tracer_init(struct trace_array *tr) 1385 { 1386 blk_tr = tr; 1387 blk_tracer_start(tr); 1388 return 0; 1389 } 1390 1391 static void blk_tracer_stop(struct trace_array *tr) 1392 { 1393 blk_tracer_enabled = false; 1394 } 1395 1396 static void blk_tracer_reset(struct trace_array *tr) 1397 { 1398 blk_tracer_stop(tr); 1399 } 1400 1401 static const struct { 1402 const char *act[2]; 1403 void (*print)(struct trace_seq *s, const struct trace_entry *ent, 1404 bool has_cg); 1405 } what2act[] = { 1406 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic }, 1407 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic }, 1408 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic }, 1409 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic }, 1410 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic }, 1411 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error }, 1412 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic }, 1413 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error }, 1414 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug }, 1415 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug }, 1416 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug }, 1417 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic }, 1418 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split }, 1419 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic }, 1420 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap }, 1421 }; 1422 1423 static enum print_line_t print_one_line(struct trace_iterator *iter, 1424 bool classic) 1425 { 1426 struct trace_array *tr = iter->tr; 1427 struct trace_seq *s = &iter->seq; 1428 const struct blk_io_trace *t; 1429 u16 what; 1430 bool long_act; 1431 blk_log_action_t *log_action; 1432 bool has_cg; 1433 1434 t = te_blk_io_trace(iter->ent); 1435 what = (t->action & ((1 << BLK_TC_SHIFT) - 1)) & ~__BLK_TA_CGROUP; 1436 long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE); 1437 log_action = classic ? &blk_log_action_classic : &blk_log_action; 1438 has_cg = t->action & __BLK_TA_CGROUP; 1439 1440 if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) { 1441 log_action(iter, long_act ? "message" : "m", has_cg); 1442 blk_log_msg(s, iter->ent, has_cg); 1443 return trace_handle_return(s); 1444 } 1445 1446 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act))) 1447 trace_seq_printf(s, "Unknown action %x\n", what); 1448 else { 1449 log_action(iter, what2act[what].act[long_act], has_cg); 1450 what2act[what].print(s, iter->ent, has_cg); 1451 } 1452 1453 return trace_handle_return(s); 1454 } 1455 1456 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter, 1457 int flags, struct trace_event *event) 1458 { 1459 return print_one_line(iter, false); 1460 } 1461 1462 static void blk_trace_synthesize_old_trace(struct trace_iterator *iter) 1463 { 1464 struct trace_seq *s = &iter->seq; 1465 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent; 1466 const int offset = offsetof(struct blk_io_trace, sector); 1467 struct blk_io_trace old = { 1468 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION, 1469 .time = iter->ts, 1470 }; 1471 1472 trace_seq_putmem(s, &old, offset); 1473 trace_seq_putmem(s, &t->sector, 1474 sizeof(old) - offset + t->pdu_len); 1475 } 1476 1477 static enum print_line_t 1478 blk_trace_event_print_binary(struct trace_iterator *iter, int flags, 1479 struct trace_event *event) 1480 { 1481 blk_trace_synthesize_old_trace(iter); 1482 1483 return trace_handle_return(&iter->seq); 1484 } 1485 1486 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter) 1487 { 1488 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) 1489 return TRACE_TYPE_UNHANDLED; 1490 1491 return print_one_line(iter, true); 1492 } 1493 1494 static int 1495 blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) 1496 { 1497 /* don't output context-info for blk_classic output */ 1498 if (bit == TRACE_BLK_OPT_CLASSIC) { 1499 if (set) 1500 tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO; 1501 else 1502 tr->trace_flags |= TRACE_ITER_CONTEXT_INFO; 1503 } 1504 return 0; 1505 } 1506 1507 static struct tracer blk_tracer __read_mostly = { 1508 .name = "blk", 1509 .init = blk_tracer_init, 1510 .reset = blk_tracer_reset, 1511 .start = blk_tracer_start, 1512 .stop = blk_tracer_stop, 1513 .print_header = blk_tracer_print_header, 1514 .print_line = blk_tracer_print_line, 1515 .flags = &blk_tracer_flags, 1516 .set_flag = blk_tracer_set_flag, 1517 }; 1518 1519 static struct trace_event_functions trace_blk_event_funcs = { 1520 .trace = blk_trace_event_print, 1521 .binary = blk_trace_event_print_binary, 1522 }; 1523 1524 static struct trace_event trace_blk_event = { 1525 .type = TRACE_BLK, 1526 .funcs = &trace_blk_event_funcs, 1527 }; 1528 1529 static int __init init_blk_tracer(void) 1530 { 1531 if (!register_trace_event(&trace_blk_event)) { 1532 pr_warn("Warning: could not register block events\n"); 1533 return 1; 1534 } 1535 1536 if (register_tracer(&blk_tracer) != 0) { 1537 pr_warn("Warning: could not register the block tracer\n"); 1538 unregister_trace_event(&trace_blk_event); 1539 return 1; 1540 } 1541 1542 return 0; 1543 } 1544 1545 device_initcall(init_blk_tracer); 1546 1547 static int blk_trace_remove_queue(struct request_queue *q) 1548 { 1549 struct blk_trace *bt; 1550 1551 bt = xchg(&q->blk_trace, NULL); 1552 if (bt == NULL) 1553 return -EINVAL; 1554 1555 if (atomic_dec_and_test(&blk_probes_ref)) 1556 blk_unregister_tracepoints(); 1557 1558 blk_trace_free(bt); 1559 return 0; 1560 } 1561 1562 /* 1563 * Setup everything required to start tracing 1564 */ 1565 static int blk_trace_setup_queue(struct request_queue *q, 1566 struct block_device *bdev) 1567 { 1568 struct blk_trace *bt = NULL; 1569 int ret = -ENOMEM; 1570 1571 bt = kzalloc(sizeof(*bt), GFP_KERNEL); 1572 if (!bt) 1573 return -ENOMEM; 1574 1575 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char)); 1576 if (!bt->msg_data) 1577 goto free_bt; 1578 1579 bt->dev = bdev->bd_dev; 1580 bt->act_mask = (u16)-1; 1581 1582 blk_trace_setup_lba(bt, bdev); 1583 1584 ret = -EBUSY; 1585 if (cmpxchg(&q->blk_trace, NULL, bt)) 1586 goto free_bt; 1587 1588 if (atomic_inc_return(&blk_probes_ref) == 1) 1589 blk_register_tracepoints(); 1590 return 0; 1591 1592 free_bt: 1593 blk_trace_free(bt); 1594 return ret; 1595 } 1596 1597 /* 1598 * sysfs interface to enable and configure tracing 1599 */ 1600 1601 static ssize_t sysfs_blk_trace_attr_show(struct device *dev, 1602 struct device_attribute *attr, 1603 char *buf); 1604 static ssize_t sysfs_blk_trace_attr_store(struct device *dev, 1605 struct device_attribute *attr, 1606 const char *buf, size_t count); 1607 #define BLK_TRACE_DEVICE_ATTR(_name) \ 1608 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \ 1609 sysfs_blk_trace_attr_show, \ 1610 sysfs_blk_trace_attr_store) 1611 1612 static BLK_TRACE_DEVICE_ATTR(enable); 1613 static BLK_TRACE_DEVICE_ATTR(act_mask); 1614 static BLK_TRACE_DEVICE_ATTR(pid); 1615 static BLK_TRACE_DEVICE_ATTR(start_lba); 1616 static BLK_TRACE_DEVICE_ATTR(end_lba); 1617 1618 static struct attribute *blk_trace_attrs[] = { 1619 &dev_attr_enable.attr, 1620 &dev_attr_act_mask.attr, 1621 &dev_attr_pid.attr, 1622 &dev_attr_start_lba.attr, 1623 &dev_attr_end_lba.attr, 1624 NULL 1625 }; 1626 1627 struct attribute_group blk_trace_attr_group = { 1628 .name = "trace", 1629 .attrs = blk_trace_attrs, 1630 }; 1631 1632 static const struct { 1633 int mask; 1634 const char *str; 1635 } mask_maps[] = { 1636 { BLK_TC_READ, "read" }, 1637 { BLK_TC_WRITE, "write" }, 1638 { BLK_TC_FLUSH, "flush" }, 1639 { BLK_TC_SYNC, "sync" }, 1640 { BLK_TC_QUEUE, "queue" }, 1641 { BLK_TC_REQUEUE, "requeue" }, 1642 { BLK_TC_ISSUE, "issue" }, 1643 { BLK_TC_COMPLETE, "complete" }, 1644 { BLK_TC_FS, "fs" }, 1645 { BLK_TC_PC, "pc" }, 1646 { BLK_TC_NOTIFY, "notify" }, 1647 { BLK_TC_AHEAD, "ahead" }, 1648 { BLK_TC_META, "meta" }, 1649 { BLK_TC_DISCARD, "discard" }, 1650 { BLK_TC_DRV_DATA, "drv_data" }, 1651 { BLK_TC_FUA, "fua" }, 1652 }; 1653 1654 static int blk_trace_str2mask(const char *str) 1655 { 1656 int i; 1657 int mask = 0; 1658 char *buf, *s, *token; 1659 1660 buf = kstrdup(str, GFP_KERNEL); 1661 if (buf == NULL) 1662 return -ENOMEM; 1663 s = strstrip(buf); 1664 1665 while (1) { 1666 token = strsep(&s, ","); 1667 if (token == NULL) 1668 break; 1669 1670 if (*token == '\0') 1671 continue; 1672 1673 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) { 1674 if (strcasecmp(token, mask_maps[i].str) == 0) { 1675 mask |= mask_maps[i].mask; 1676 break; 1677 } 1678 } 1679 if (i == ARRAY_SIZE(mask_maps)) { 1680 mask = -EINVAL; 1681 break; 1682 } 1683 } 1684 kfree(buf); 1685 1686 return mask; 1687 } 1688 1689 static ssize_t blk_trace_mask2str(char *buf, int mask) 1690 { 1691 int i; 1692 char *p = buf; 1693 1694 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) { 1695 if (mask & mask_maps[i].mask) { 1696 p += sprintf(p, "%s%s", 1697 (p == buf) ? "" : ",", mask_maps[i].str); 1698 } 1699 } 1700 *p++ = '\n'; 1701 1702 return p - buf; 1703 } 1704 1705 static struct request_queue *blk_trace_get_queue(struct block_device *bdev) 1706 { 1707 if (bdev->bd_disk == NULL) 1708 return NULL; 1709 1710 return bdev_get_queue(bdev); 1711 } 1712 1713 static ssize_t sysfs_blk_trace_attr_show(struct device *dev, 1714 struct device_attribute *attr, 1715 char *buf) 1716 { 1717 struct hd_struct *p = dev_to_part(dev); 1718 struct request_queue *q; 1719 struct block_device *bdev; 1720 ssize_t ret = -ENXIO; 1721 1722 bdev = bdget(part_devt(p)); 1723 if (bdev == NULL) 1724 goto out; 1725 1726 q = blk_trace_get_queue(bdev); 1727 if (q == NULL) 1728 goto out_bdput; 1729 1730 mutex_lock(&bdev->bd_mutex); 1731 1732 if (attr == &dev_attr_enable) { 1733 ret = sprintf(buf, "%u\n", !!q->blk_trace); 1734 goto out_unlock_bdev; 1735 } 1736 1737 if (q->blk_trace == NULL) 1738 ret = sprintf(buf, "disabled\n"); 1739 else if (attr == &dev_attr_act_mask) 1740 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask); 1741 else if (attr == &dev_attr_pid) 1742 ret = sprintf(buf, "%u\n", q->blk_trace->pid); 1743 else if (attr == &dev_attr_start_lba) 1744 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba); 1745 else if (attr == &dev_attr_end_lba) 1746 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba); 1747 1748 out_unlock_bdev: 1749 mutex_unlock(&bdev->bd_mutex); 1750 out_bdput: 1751 bdput(bdev); 1752 out: 1753 return ret; 1754 } 1755 1756 static ssize_t sysfs_blk_trace_attr_store(struct device *dev, 1757 struct device_attribute *attr, 1758 const char *buf, size_t count) 1759 { 1760 struct block_device *bdev; 1761 struct request_queue *q; 1762 struct hd_struct *p; 1763 u64 value; 1764 ssize_t ret = -EINVAL; 1765 1766 if (count == 0) 1767 goto out; 1768 1769 if (attr == &dev_attr_act_mask) { 1770 if (kstrtoull(buf, 0, &value)) { 1771 /* Assume it is a list of trace category names */ 1772 ret = blk_trace_str2mask(buf); 1773 if (ret < 0) 1774 goto out; 1775 value = ret; 1776 } 1777 } else if (kstrtoull(buf, 0, &value)) 1778 goto out; 1779 1780 ret = -ENXIO; 1781 1782 p = dev_to_part(dev); 1783 bdev = bdget(part_devt(p)); 1784 if (bdev == NULL) 1785 goto out; 1786 1787 q = blk_trace_get_queue(bdev); 1788 if (q == NULL) 1789 goto out_bdput; 1790 1791 mutex_lock(&bdev->bd_mutex); 1792 1793 if (attr == &dev_attr_enable) { 1794 if (value) 1795 ret = blk_trace_setup_queue(q, bdev); 1796 else 1797 ret = blk_trace_remove_queue(q); 1798 goto out_unlock_bdev; 1799 } 1800 1801 ret = 0; 1802 if (q->blk_trace == NULL) 1803 ret = blk_trace_setup_queue(q, bdev); 1804 1805 if (ret == 0) { 1806 if (attr == &dev_attr_act_mask) 1807 q->blk_trace->act_mask = value; 1808 else if (attr == &dev_attr_pid) 1809 q->blk_trace->pid = value; 1810 else if (attr == &dev_attr_start_lba) 1811 q->blk_trace->start_lba = value; 1812 else if (attr == &dev_attr_end_lba) 1813 q->blk_trace->end_lba = value; 1814 } 1815 1816 out_unlock_bdev: 1817 mutex_unlock(&bdev->bd_mutex); 1818 out_bdput: 1819 bdput(bdev); 1820 out: 1821 return ret ? ret : count; 1822 } 1823 1824 int blk_trace_init_sysfs(struct device *dev) 1825 { 1826 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group); 1827 } 1828 1829 void blk_trace_remove_sysfs(struct device *dev) 1830 { 1831 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group); 1832 } 1833 1834 #endif /* CONFIG_BLK_DEV_IO_TRACE */ 1835 1836 #ifdef CONFIG_EVENT_TRACING 1837 1838 void blk_fill_rwbs(char *rwbs, unsigned int op, int bytes) 1839 { 1840 int i = 0; 1841 1842 if (op & REQ_PREFLUSH) 1843 rwbs[i++] = 'F'; 1844 1845 switch (op & REQ_OP_MASK) { 1846 case REQ_OP_WRITE: 1847 case REQ_OP_WRITE_SAME: 1848 rwbs[i++] = 'W'; 1849 break; 1850 case REQ_OP_DISCARD: 1851 rwbs[i++] = 'D'; 1852 break; 1853 case REQ_OP_SECURE_ERASE: 1854 rwbs[i++] = 'D'; 1855 rwbs[i++] = 'E'; 1856 break; 1857 case REQ_OP_FLUSH: 1858 rwbs[i++] = 'F'; 1859 break; 1860 case REQ_OP_READ: 1861 rwbs[i++] = 'R'; 1862 break; 1863 default: 1864 rwbs[i++] = 'N'; 1865 } 1866 1867 if (op & REQ_FUA) 1868 rwbs[i++] = 'F'; 1869 if (op & REQ_RAHEAD) 1870 rwbs[i++] = 'A'; 1871 if (op & REQ_SYNC) 1872 rwbs[i++] = 'S'; 1873 if (op & REQ_META) 1874 rwbs[i++] = 'M'; 1875 1876 rwbs[i] = '\0'; 1877 } 1878 EXPORT_SYMBOL_GPL(blk_fill_rwbs); 1879 1880 #endif /* CONFIG_EVENT_TRACING */ 1881 1882