2 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
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.
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.
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
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/debugfs.h>
25 #include <linux/time.h>
26 #include <linux/uaccess.h>
28 #include <trace/events/block.h>
30 #include "trace_output.h"
32 #ifdef CONFIG_BLK_DEV_IO_TRACE
34 static unsigned int blktrace_seq __read_mostly = 1;
36 static struct trace_array *blk_tr;
37 static bool blk_tracer_enabled __read_mostly;
39 /* Select an alternative, minimalistic output than the original one */
40 #define TRACE_BLK_OPT_CLASSIC 0x1
42 static struct tracer_opt blk_tracer_opts[] = {
43 /* Default disable the minimalistic output */
44 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
48 static struct tracer_flags blk_tracer_flags = {
50 .opts = blk_tracer_opts,
53 /* Global reference count of probes */
54 static atomic_t blk_probes_ref = ATOMIC_INIT(0);
56 static void blk_register_tracepoints(void);
57 static void blk_unregister_tracepoints(void);
60 * Send out a notify message.
62 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
63 const void *data, size_t len)
65 struct blk_io_trace *t;
66 struct ring_buffer_event *event = NULL;
68 int cpu = smp_processor_id();
69 bool blk_tracer = blk_tracer_enabled;
73 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
78 t = ring_buffer_event_data(event);
85 t = relay_reserve(bt->rchan, sizeof(*t) + len);
87 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
88 t->time = ktime_to_ns(ktime_get());
95 memcpy((void *) t + sizeof(*t), data, len);
98 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
103 * Send out a notify for this process, if we haven't done so since a trace
106 static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
108 tsk->btrace_seq = blktrace_seq;
109 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
112 static void trace_note_time(struct blk_trace *bt)
118 getnstimeofday(&now);
119 words[0] = now.tv_sec;
120 words[1] = now.tv_nsec;
122 local_irq_save(flags);
123 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
124 local_irq_restore(flags);
127 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
134 if (unlikely(bt->trace_state != Blktrace_running &&
135 !blk_tracer_enabled))
138 local_irq_save(flags);
139 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
141 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
144 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
145 local_irq_restore(flags);
147 EXPORT_SYMBOL_GPL(__trace_note_message);
149 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
152 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
154 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
156 if (bt->pid && pid != bt->pid)
163 * Data direction bit lookup
165 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
166 BLK_TC_ACT(BLK_TC_WRITE) };
168 /* The ilog2() calls fall out because they're constant */
169 #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
170 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
173 * The worker for the various blk_add_trace*() types. Fills out a
174 * blk_io_trace structure and places it in a per-cpu subbuffer.
176 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
177 int rw, u32 what, int error, int pdu_len, void *pdu_data)
179 struct task_struct *tsk = current;
180 struct ring_buffer_event *event = NULL;
181 struct blk_io_trace *t;
182 unsigned long flags = 0;
183 unsigned long *sequence;
186 bool blk_tracer = blk_tracer_enabled;
188 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
191 what |= ddir_act[rw & WRITE];
192 what |= MASK_TC_BIT(rw, BARRIER);
193 what |= MASK_TC_BIT(rw, SYNCIO);
194 what |= MASK_TC_BIT(rw, AHEAD);
195 what |= MASK_TC_BIT(rw, META);
196 what |= MASK_TC_BIT(rw, DISCARD);
199 if (act_log_check(bt, what, sector, pid))
201 cpu = raw_smp_processor_id();
204 tracing_record_cmdline(current);
206 pc = preempt_count();
207 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
208 sizeof(*t) + pdu_len,
212 t = ring_buffer_event_data(event);
217 * A word about the locking here - we disable interrupts to reserve
218 * some space in the relay per-cpu buffer, to prevent an irq
219 * from coming in and stepping on our toes.
221 local_irq_save(flags);
223 if (unlikely(tsk->btrace_seq != blktrace_seq))
224 trace_note_tsk(bt, tsk);
226 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
228 sequence = per_cpu_ptr(bt->sequence, cpu);
230 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
231 t->sequence = ++(*sequence);
232 t->time = ktime_to_ns(ktime_get());
235 * These two are not needed in ftrace as they are in the
236 * generic trace_entry, filled by tracing_generic_entry_update,
237 * but for the trace_event->bin() synthesizer benefit we do it
248 t->pdu_len = pdu_len;
251 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
254 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
259 local_irq_restore(flags);
262 static struct dentry *blk_tree_root;
263 static DEFINE_MUTEX(blk_tree_mutex);
265 static void blk_trace_free(struct blk_trace *bt)
267 debugfs_remove(bt->msg_file);
268 debugfs_remove(bt->dropped_file);
269 debugfs_remove(bt->dir);
270 relay_close(bt->rchan);
271 free_percpu(bt->sequence);
272 free_percpu(bt->msg_data);
276 static void blk_trace_cleanup(struct blk_trace *bt)
279 if (atomic_dec_and_test(&blk_probes_ref))
280 blk_unregister_tracepoints();
283 int blk_trace_remove(struct request_queue *q)
285 struct blk_trace *bt;
287 bt = xchg(&q->blk_trace, NULL);
291 if (bt->trace_state != Blktrace_running)
292 blk_trace_cleanup(bt);
296 EXPORT_SYMBOL_GPL(blk_trace_remove);
298 static int blk_dropped_open(struct inode *inode, struct file *filp)
300 filp->private_data = inode->i_private;
305 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
306 size_t count, loff_t *ppos)
308 struct blk_trace *bt = filp->private_data;
311 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
313 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
316 static const struct file_operations blk_dropped_fops = {
317 .owner = THIS_MODULE,
318 .open = blk_dropped_open,
319 .read = blk_dropped_read,
322 static int blk_msg_open(struct inode *inode, struct file *filp)
324 filp->private_data = inode->i_private;
329 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
330 size_t count, loff_t *ppos)
333 struct blk_trace *bt;
335 if (count >= BLK_TN_MAX_MSG)
338 msg = kmalloc(count + 1, GFP_KERNEL);
342 if (copy_from_user(msg, buffer, count)) {
348 bt = filp->private_data;
349 __trace_note_message(bt, "%s", msg);
355 static const struct file_operations blk_msg_fops = {
356 .owner = THIS_MODULE,
357 .open = blk_msg_open,
358 .write = blk_msg_write,
362 * Keep track of how many times we encountered a full subbuffer, to aid
363 * the user space app in telling how many lost events there were.
365 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
366 void *prev_subbuf, size_t prev_padding)
368 struct blk_trace *bt;
370 if (!relay_buf_full(buf))
373 bt = buf->chan->private_data;
374 atomic_inc(&bt->dropped);
378 static int blk_remove_buf_file_callback(struct dentry *dentry)
380 struct dentry *parent = dentry->d_parent;
381 debugfs_remove(dentry);
384 * this will fail for all but the last file, but that is ok. what we
385 * care about is the top level buts->name directory going away, when
386 * the last trace file is gone. Then we don't have to rmdir() that
387 * manually on trace stop, so it nicely solves the issue with
388 * force killing of running traces.
391 debugfs_remove(parent);
395 static struct dentry *blk_create_buf_file_callback(const char *filename,
396 struct dentry *parent,
398 struct rchan_buf *buf,
401 return debugfs_create_file(filename, mode, parent, buf,
402 &relay_file_operations);
405 static struct rchan_callbacks blk_relay_callbacks = {
406 .subbuf_start = blk_subbuf_start_callback,
407 .create_buf_file = blk_create_buf_file_callback,
408 .remove_buf_file = blk_remove_buf_file_callback,
411 static void blk_trace_setup_lba(struct blk_trace *bt,
412 struct block_device *bdev)
414 struct hd_struct *part = NULL;
417 part = bdev->bd_part;
420 bt->start_lba = part->start_sect;
421 bt->end_lba = part->start_sect + part->nr_sects;
429 * Setup everything required to start tracing
431 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
432 struct block_device *bdev,
433 struct blk_user_trace_setup *buts)
435 struct blk_trace *old_bt, *bt = NULL;
436 struct dentry *dir = NULL;
439 if (!buts->buf_size || !buts->buf_nr)
442 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
443 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
446 * some device names have larger paths - convert the slashes
447 * to underscores for this to work as expected
449 for (i = 0; i < strlen(buts->name); i++)
450 if (buts->name[i] == '/')
453 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
458 bt->sequence = alloc_percpu(unsigned long);
462 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
468 mutex_lock(&blk_tree_mutex);
469 if (!blk_tree_root) {
470 blk_tree_root = debugfs_create_dir("block", NULL);
471 if (!blk_tree_root) {
472 mutex_unlock(&blk_tree_mutex);
476 mutex_unlock(&blk_tree_mutex);
478 dir = debugfs_create_dir(buts->name, blk_tree_root);
485 atomic_set(&bt->dropped, 0);
488 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
490 if (!bt->dropped_file)
493 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
497 bt->rchan = relay_open("trace", dir, buts->buf_size,
498 buts->buf_nr, &blk_relay_callbacks, bt);
502 bt->act_mask = buts->act_mask;
504 bt->act_mask = (u16) -1;
506 blk_trace_setup_lba(bt, bdev);
508 /* overwrite with user settings */
510 bt->start_lba = buts->start_lba;
512 bt->end_lba = buts->end_lba;
515 bt->trace_state = Blktrace_setup;
518 old_bt = xchg(&q->blk_trace, bt);
520 (void) xchg(&q->blk_trace, old_bt);
524 if (atomic_inc_return(&blk_probes_ref) == 1)
525 blk_register_tracepoints();
533 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
534 struct block_device *bdev,
537 struct blk_user_trace_setup buts;
540 ret = copy_from_user(&buts, arg, sizeof(buts));
544 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
548 if (copy_to_user(arg, &buts, sizeof(buts)))
553 EXPORT_SYMBOL_GPL(blk_trace_setup);
555 int blk_trace_startstop(struct request_queue *q, int start)
558 struct blk_trace *bt = q->blk_trace;
564 * For starting a trace, we can transition from a setup or stopped
565 * trace. For stopping a trace, the state must be running
569 if (bt->trace_state == Blktrace_setup ||
570 bt->trace_state == Blktrace_stopped) {
573 bt->trace_state = Blktrace_running;
579 if (bt->trace_state == Blktrace_running) {
580 bt->trace_state = Blktrace_stopped;
581 relay_flush(bt->rchan);
588 EXPORT_SYMBOL_GPL(blk_trace_startstop);
591 * blk_trace_ioctl: - handle the ioctls associated with tracing
592 * @bdev: the block device
593 * @cmd: the ioctl cmd
594 * @arg: the argument data, if any
597 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
599 struct request_queue *q;
601 char b[BDEVNAME_SIZE];
603 q = bdev_get_queue(bdev);
607 mutex_lock(&bdev->bd_mutex);
612 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
617 ret = blk_trace_startstop(q, start);
619 case BLKTRACETEARDOWN:
620 ret = blk_trace_remove(q);
627 mutex_unlock(&bdev->bd_mutex);
632 * blk_trace_shutdown: - stop and cleanup trace structures
633 * @q: the request queue associated with the device
636 void blk_trace_shutdown(struct request_queue *q)
639 blk_trace_startstop(q, 0);
649 * blk_add_trace_rq - Add a trace for a request oriented action
650 * @q: queue the io is for
651 * @rq: the source request
655 * Records an action against a request. Will log the bio offset + size.
658 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
661 struct blk_trace *bt = q->blk_trace;
662 int rw = rq->cmd_flags & 0x03;
667 if (blk_discard_rq(rq))
668 rw |= (1 << BIO_RW_DISCARD);
670 if (blk_pc_request(rq)) {
671 what |= BLK_TC_ACT(BLK_TC_PC);
672 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
673 what, rq->errors, rq->cmd_len, rq->cmd);
675 what |= BLK_TC_ACT(BLK_TC_FS);
676 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
677 what, rq->errors, 0, NULL);
681 static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
683 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
686 static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
688 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
691 static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
693 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
696 static void blk_add_trace_rq_requeue(struct request_queue *q,
699 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
702 static void blk_add_trace_rq_complete(struct request_queue *q,
705 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
709 * blk_add_trace_bio - Add a trace for a bio oriented action
710 * @q: queue the io is for
711 * @bio: the source bio
715 * Records an action against a bio. Will log the bio offset + size.
718 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
721 struct blk_trace *bt = q->blk_trace;
726 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
727 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
730 static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
732 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
735 static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
737 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
740 static void blk_add_trace_bio_backmerge(struct request_queue *q,
743 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
746 static void blk_add_trace_bio_frontmerge(struct request_queue *q,
749 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
752 static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
754 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
757 static void blk_add_trace_getrq(struct request_queue *q,
758 struct bio *bio, int rw)
761 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
763 struct blk_trace *bt = q->blk_trace;
766 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
771 static void blk_add_trace_sleeprq(struct request_queue *q,
772 struct bio *bio, int rw)
775 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
777 struct blk_trace *bt = q->blk_trace;
780 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
785 static void blk_add_trace_plug(struct request_queue *q)
787 struct blk_trace *bt = q->blk_trace;
790 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
793 static void blk_add_trace_unplug_io(struct request_queue *q)
795 struct blk_trace *bt = q->blk_trace;
798 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
799 __be64 rpdu = cpu_to_be64(pdu);
801 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
802 sizeof(rpdu), &rpdu);
806 static void blk_add_trace_unplug_timer(struct request_queue *q)
808 struct blk_trace *bt = q->blk_trace;
811 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
812 __be64 rpdu = cpu_to_be64(pdu);
814 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
815 sizeof(rpdu), &rpdu);
819 static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
822 struct blk_trace *bt = q->blk_trace;
825 __be64 rpdu = cpu_to_be64(pdu);
827 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
828 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
829 sizeof(rpdu), &rpdu);
834 * blk_add_trace_remap - Add a trace for a remap operation
835 * @q: queue the io is for
836 * @bio: the source bio
837 * @dev: target device
838 * @from: source sector
841 * Device mapper or raid target sometimes need to split a bio because
842 * it spans a stripe (or similar). Add a trace for that action.
845 static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
846 dev_t dev, sector_t from)
848 struct blk_trace *bt = q->blk_trace;
849 struct blk_io_trace_remap r;
854 r.device_from = cpu_to_be32(dev);
855 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
856 r.sector_from = cpu_to_be64(from);
858 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
859 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
864 * blk_add_driver_data - Add binary message with driver-specific data
865 * @q: queue the io is for
867 * @data: driver-specific data
868 * @len: length of driver-specific data
871 * Some drivers might want to write driver-specific data per request.
874 void blk_add_driver_data(struct request_queue *q,
876 void *data, size_t len)
878 struct blk_trace *bt = q->blk_trace;
883 if (blk_pc_request(rq))
884 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
885 BLK_TA_DRV_DATA, rq->errors, len, data);
887 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
888 BLK_TA_DRV_DATA, rq->errors, len, data);
890 EXPORT_SYMBOL_GPL(blk_add_driver_data);
892 static void blk_register_tracepoints(void)
896 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
898 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
900 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
902 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
904 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
906 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
908 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
910 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
912 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
914 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
916 ret = register_trace_block_getrq(blk_add_trace_getrq);
918 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
920 ret = register_trace_block_plug(blk_add_trace_plug);
922 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
924 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
926 ret = register_trace_block_split(blk_add_trace_split);
928 ret = register_trace_block_remap(blk_add_trace_remap);
932 static void blk_unregister_tracepoints(void)
934 unregister_trace_block_remap(blk_add_trace_remap);
935 unregister_trace_block_split(blk_add_trace_split);
936 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
937 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
938 unregister_trace_block_plug(blk_add_trace_plug);
939 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
940 unregister_trace_block_getrq(blk_add_trace_getrq);
941 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
942 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
943 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
944 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
945 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
946 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
947 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
948 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
949 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
950 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
952 tracepoint_synchronize_unregister();
956 * struct blk_io_tracer formatting routines
959 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
962 int tc = t->action >> BLK_TC_SHIFT;
964 if (t->action == BLK_TN_MESSAGE) {
969 if (tc & BLK_TC_DISCARD)
971 else if (tc & BLK_TC_WRITE)
978 if (tc & BLK_TC_AHEAD)
980 if (tc & BLK_TC_BARRIER)
982 if (tc & BLK_TC_SYNC)
984 if (tc & BLK_TC_META)
991 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
993 return (const struct blk_io_trace *)ent;
996 static inline const void *pdu_start(const struct trace_entry *ent)
998 return te_blk_io_trace(ent) + 1;
1001 static inline u32 t_action(const struct trace_entry *ent)
1003 return te_blk_io_trace(ent)->action;
1006 static inline u32 t_bytes(const struct trace_entry *ent)
1008 return te_blk_io_trace(ent)->bytes;
1011 static inline u32 t_sec(const struct trace_entry *ent)
1013 return te_blk_io_trace(ent)->bytes >> 9;
1016 static inline unsigned long long t_sector(const struct trace_entry *ent)
1018 return te_blk_io_trace(ent)->sector;
1021 static inline __u16 t_error(const struct trace_entry *ent)
1023 return te_blk_io_trace(ent)->error;
1026 static __u64 get_pdu_int(const struct trace_entry *ent)
1028 const __u64 *val = pdu_start(ent);
1029 return be64_to_cpu(*val);
1032 static void get_pdu_remap(const struct trace_entry *ent,
1033 struct blk_io_trace_remap *r)
1035 const struct blk_io_trace_remap *__r = pdu_start(ent);
1036 __u64 sector_from = __r->sector_from;
1038 r->device_from = be32_to_cpu(__r->device_from);
1039 r->device_to = be32_to_cpu(__r->device_to);
1040 r->sector_from = be64_to_cpu(sector_from);
1043 typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1045 static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
1048 unsigned long long ts = iter->ts;
1049 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1050 unsigned secs = (unsigned long)ts;
1051 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1055 return trace_seq_printf(&iter->seq,
1056 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1057 MAJOR(t->device), MINOR(t->device), iter->cpu,
1058 secs, nsec_rem, iter->ent->pid, act, rwbs);
1061 static int blk_log_action(struct trace_iterator *iter, const char *act)
1064 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1067 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1068 MAJOR(t->device), MINOR(t->device), act, rwbs);
1071 static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1073 const unsigned char *pdu_buf;
1077 pdu_buf = pdu_start(ent);
1078 pdu_len = te_blk_io_trace(ent)->pdu_len;
1083 /* find the last zero that needs to be printed */
1084 for (end = pdu_len - 1; end >= 0; end--)
1089 if (!trace_seq_putc(s, '('))
1092 for (i = 0; i < pdu_len; i++) {
1094 ret = trace_seq_printf(s, "%s%02x",
1095 i == 0 ? "" : " ", pdu_buf[i]);
1100 * stop when the rest is just zeroes and indicate so
1101 * with a ".." appended
1103 if (i == end && end != pdu_len - 1)
1104 return trace_seq_puts(s, " ..) ");
1107 return trace_seq_puts(s, ") ");
1110 static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1112 char cmd[TASK_COMM_LEN];
1114 trace_find_cmdline(ent->pid, cmd);
1116 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1119 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1122 ret = blk_log_dump_pdu(s, ent);
1125 return trace_seq_printf(s, "[%s]\n", cmd);
1128 return trace_seq_printf(s, "%llu + %u [%s]\n",
1129 t_sector(ent), t_sec(ent), cmd);
1130 return trace_seq_printf(s, "[%s]\n", cmd);
1134 static int blk_log_with_error(struct trace_seq *s,
1135 const struct trace_entry *ent)
1137 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1140 ret = blk_log_dump_pdu(s, ent);
1142 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1146 return trace_seq_printf(s, "%llu + %u [%d]\n",
1148 t_sec(ent), t_error(ent));
1149 return trace_seq_printf(s, "%llu [%d]\n",
1150 t_sector(ent), t_error(ent));
1154 static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1156 struct blk_io_trace_remap r = { .device_from = 0, };
1158 get_pdu_remap(ent, &r);
1159 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1160 t_sector(ent), t_sec(ent),
1161 MAJOR(r.device_from), MINOR(r.device_from),
1162 (unsigned long long)r.sector_from);
1165 static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1167 char cmd[TASK_COMM_LEN];
1169 trace_find_cmdline(ent->pid, cmd);
1171 return trace_seq_printf(s, "[%s]\n", cmd);
1174 static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1176 char cmd[TASK_COMM_LEN];
1178 trace_find_cmdline(ent->pid, cmd);
1180 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1183 static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1185 char cmd[TASK_COMM_LEN];
1187 trace_find_cmdline(ent->pid, cmd);
1189 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1190 get_pdu_int(ent), cmd);
1193 static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1196 const struct blk_io_trace *t = te_blk_io_trace(ent);
1198 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1200 return trace_seq_putc(s, '\n');
1205 * struct tracer operations
1208 static void blk_tracer_print_header(struct seq_file *m)
1210 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1212 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1216 static void blk_tracer_start(struct trace_array *tr)
1218 blk_tracer_enabled = true;
1221 static int blk_tracer_init(struct trace_array *tr)
1224 blk_tracer_start(tr);
1228 static void blk_tracer_stop(struct trace_array *tr)
1230 blk_tracer_enabled = false;
1233 static void blk_tracer_reset(struct trace_array *tr)
1235 blk_tracer_stop(tr);
1238 static const struct {
1240 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1242 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1243 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1244 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1245 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1246 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1247 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1248 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1249 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1250 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1251 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1252 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1253 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1254 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1255 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1256 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1259 static enum print_line_t print_one_line(struct trace_iterator *iter,
1262 struct trace_seq *s = &iter->seq;
1263 const struct blk_io_trace *t;
1267 blk_log_action_t *log_action;
1269 t = te_blk_io_trace(iter->ent);
1270 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1271 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1272 log_action = classic ? &blk_log_action_classic : &blk_log_action;
1274 if (t->action == BLK_TN_MESSAGE) {
1275 ret = log_action(iter, long_act ? "message" : "m");
1277 ret = blk_log_msg(s, iter->ent);
1281 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1282 ret = trace_seq_printf(s, "Unknown action %x\n", what);
1284 ret = log_action(iter, what2act[what].act[long_act]);
1286 ret = what2act[what].print(s, iter->ent);
1289 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1292 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1295 return print_one_line(iter, false);
1298 static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1300 struct trace_seq *s = &iter->seq;
1301 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1302 const int offset = offsetof(struct blk_io_trace, sector);
1303 struct blk_io_trace old = {
1304 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1308 if (!trace_seq_putmem(s, &old, offset))
1310 return trace_seq_putmem(s, &t->sector,
1311 sizeof(old) - offset + t->pdu_len);
1314 static enum print_line_t
1315 blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
1317 return blk_trace_synthesize_old_trace(iter) ?
1318 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1321 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1323 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1324 return TRACE_TYPE_UNHANDLED;
1326 return print_one_line(iter, true);
1329 static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1331 /* don't output context-info for blk_classic output */
1332 if (bit == TRACE_BLK_OPT_CLASSIC) {
1334 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1336 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1341 static struct tracer blk_tracer __read_mostly = {
1343 .init = blk_tracer_init,
1344 .reset = blk_tracer_reset,
1345 .start = blk_tracer_start,
1346 .stop = blk_tracer_stop,
1347 .print_header = blk_tracer_print_header,
1348 .print_line = blk_tracer_print_line,
1349 .flags = &blk_tracer_flags,
1350 .set_flag = blk_tracer_set_flag,
1353 static struct trace_event trace_blk_event = {
1355 .trace = blk_trace_event_print,
1356 .binary = blk_trace_event_print_binary,
1359 static int __init init_blk_tracer(void)
1361 if (!register_ftrace_event(&trace_blk_event)) {
1362 pr_warning("Warning: could not register block events\n");
1366 if (register_tracer(&blk_tracer) != 0) {
1367 pr_warning("Warning: could not register the block tracer\n");
1368 unregister_ftrace_event(&trace_blk_event);
1375 device_initcall(init_blk_tracer);
1377 static int blk_trace_remove_queue(struct request_queue *q)
1379 struct blk_trace *bt;
1381 bt = xchg(&q->blk_trace, NULL);
1385 if (atomic_dec_and_test(&blk_probes_ref))
1386 blk_unregister_tracepoints();
1393 * Setup everything required to start tracing
1395 static int blk_trace_setup_queue(struct request_queue *q,
1396 struct block_device *bdev)
1398 struct blk_trace *old_bt, *bt = NULL;
1401 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1405 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1409 bt->dev = bdev->bd_dev;
1410 bt->act_mask = (u16)-1;
1412 blk_trace_setup_lba(bt, bdev);
1414 old_bt = xchg(&q->blk_trace, bt);
1415 if (old_bt != NULL) {
1416 (void)xchg(&q->blk_trace, old_bt);
1421 if (atomic_inc_return(&blk_probes_ref) == 1)
1422 blk_register_tracepoints();
1431 * sysfs interface to enable and configure tracing
1434 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1435 struct device_attribute *attr,
1437 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1438 struct device_attribute *attr,
1439 const char *buf, size_t count);
1440 #define BLK_TRACE_DEVICE_ATTR(_name) \
1441 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1442 sysfs_blk_trace_attr_show, \
1443 sysfs_blk_trace_attr_store)
1445 static BLK_TRACE_DEVICE_ATTR(enable);
1446 static BLK_TRACE_DEVICE_ATTR(act_mask);
1447 static BLK_TRACE_DEVICE_ATTR(pid);
1448 static BLK_TRACE_DEVICE_ATTR(start_lba);
1449 static BLK_TRACE_DEVICE_ATTR(end_lba);
1451 static struct attribute *blk_trace_attrs[] = {
1452 &dev_attr_enable.attr,
1453 &dev_attr_act_mask.attr,
1455 &dev_attr_start_lba.attr,
1456 &dev_attr_end_lba.attr,
1460 struct attribute_group blk_trace_attr_group = {
1462 .attrs = blk_trace_attrs,
1465 static const struct {
1469 { BLK_TC_READ, "read" },
1470 { BLK_TC_WRITE, "write" },
1471 { BLK_TC_BARRIER, "barrier" },
1472 { BLK_TC_SYNC, "sync" },
1473 { BLK_TC_QUEUE, "queue" },
1474 { BLK_TC_REQUEUE, "requeue" },
1475 { BLK_TC_ISSUE, "issue" },
1476 { BLK_TC_COMPLETE, "complete" },
1477 { BLK_TC_FS, "fs" },
1478 { BLK_TC_PC, "pc" },
1479 { BLK_TC_AHEAD, "ahead" },
1480 { BLK_TC_META, "meta" },
1481 { BLK_TC_DISCARD, "discard" },
1482 { BLK_TC_DRV_DATA, "drv_data" },
1485 static int blk_trace_str2mask(const char *str)
1489 char *buf, *s, *token;
1491 buf = kstrdup(str, GFP_KERNEL);
1497 token = strsep(&s, ",");
1504 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1505 if (strcasecmp(token, mask_maps[i].str) == 0) {
1506 mask |= mask_maps[i].mask;
1510 if (i == ARRAY_SIZE(mask_maps)) {
1520 static ssize_t blk_trace_mask2str(char *buf, int mask)
1525 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1526 if (mask & mask_maps[i].mask) {
1527 p += sprintf(p, "%s%s",
1528 (p == buf) ? "" : ",", mask_maps[i].str);
1536 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1538 if (bdev->bd_disk == NULL)
1541 return bdev_get_queue(bdev);
1544 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1545 struct device_attribute *attr,
1548 struct hd_struct *p = dev_to_part(dev);
1549 struct request_queue *q;
1550 struct block_device *bdev;
1551 ssize_t ret = -ENXIO;
1554 bdev = bdget(part_devt(p));
1556 goto out_unlock_kernel;
1558 q = blk_trace_get_queue(bdev);
1562 mutex_lock(&bdev->bd_mutex);
1564 if (attr == &dev_attr_enable) {
1565 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1566 goto out_unlock_bdev;
1569 if (q->blk_trace == NULL)
1570 ret = sprintf(buf, "disabled\n");
1571 else if (attr == &dev_attr_act_mask)
1572 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
1573 else if (attr == &dev_attr_pid)
1574 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1575 else if (attr == &dev_attr_start_lba)
1576 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1577 else if (attr == &dev_attr_end_lba)
1578 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1581 mutex_unlock(&bdev->bd_mutex);
1589 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1590 struct device_attribute *attr,
1591 const char *buf, size_t count)
1593 struct block_device *bdev;
1594 struct request_queue *q;
1595 struct hd_struct *p;
1597 ssize_t ret = -EINVAL;
1602 if (attr == &dev_attr_act_mask) {
1603 if (sscanf(buf, "%llx", &value) != 1) {
1604 /* Assume it is a list of trace category names */
1605 ret = blk_trace_str2mask(buf);
1610 } else if (sscanf(buf, "%llu", &value) != 1)
1616 p = dev_to_part(dev);
1617 bdev = bdget(part_devt(p));
1619 goto out_unlock_kernel;
1621 q = blk_trace_get_queue(bdev);
1625 mutex_lock(&bdev->bd_mutex);
1627 if (attr == &dev_attr_enable) {
1629 ret = blk_trace_setup_queue(q, bdev);
1631 ret = blk_trace_remove_queue(q);
1632 goto out_unlock_bdev;
1636 if (q->blk_trace == NULL)
1637 ret = blk_trace_setup_queue(q, bdev);
1640 if (attr == &dev_attr_act_mask)
1641 q->blk_trace->act_mask = value;
1642 else if (attr == &dev_attr_pid)
1643 q->blk_trace->pid = value;
1644 else if (attr == &dev_attr_start_lba)
1645 q->blk_trace->start_lba = value;
1646 else if (attr == &dev_attr_end_lba)
1647 q->blk_trace->end_lba = value;
1651 mutex_unlock(&bdev->bd_mutex);
1657 return ret ? ret : count;
1660 int blk_trace_init_sysfs(struct device *dev)
1662 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1665 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1667 #ifdef CONFIG_EVENT_TRACING
1669 void blk_dump_cmd(char *buf, struct request *rq)
1672 int len = rq->cmd_len;
1673 unsigned char *cmd = rq->cmd;
1675 if (!blk_pc_request(rq)) {
1680 for (end = len - 1; end >= 0; end--)
1685 for (i = 0; i < len; i++) {
1686 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1687 if (i == end && end != len - 1) {
1688 sprintf(buf, " ..");
1694 void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1700 else if (rw & 1 << BIO_RW_DISCARD)
1707 if (rw & 1 << BIO_RW_AHEAD)
1709 if (rw & 1 << BIO_RW_BARRIER)
1711 if (rw & 1 << BIO_RW_SYNCIO)
1713 if (rw & 1 << BIO_RW_META)
1719 void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1721 int rw = rq->cmd_flags & 0x03;
1724 if (blk_discard_rq(rq))
1725 rw |= (1 << BIO_RW_DISCARD);
1727 bytes = blk_rq_bytes(rq);
1729 blk_fill_rwbs(rwbs, rw, bytes);
1732 #endif /* CONFIG_EVENT_TRACING */