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 <trace/block.h>
27 #include <asm/uaccess.h>
28 #include <../kernel/trace/trace_output.h>
30 static unsigned int blktrace_seq __read_mostly = 1;
32 static struct trace_array *blk_tr;
33 static int __read_mostly blk_tracer_enabled;
35 /* Select an alternative, minimalistic output than the original one */
36 #define TRACE_BLK_OPT_CLASSIC 0x1
38 static struct tracer_opt blk_tracer_opts[] = {
39 /* Default disable the minimalistic output */
40 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC ) },
44 static struct tracer_flags blk_tracer_flags = {
46 .opts = blk_tracer_opts,
49 /* Global reference count of probes */
50 static DEFINE_MUTEX(blk_probe_mutex);
51 static atomic_t blk_probes_ref = ATOMIC_INIT(0);
53 static int blk_register_tracepoints(void);
54 static void blk_unregister_tracepoints(void);
57 * Send out a notify message.
59 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
60 const void *data, size_t len)
62 struct blk_io_trace *t;
67 t = relay_reserve(bt->rchan, sizeof(*t) + len);
69 const int cpu = smp_processor_id();
71 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
72 t->time = ktime_to_ns(ktime_get());
78 memcpy((void *) t + sizeof(*t), data, len);
83 * Send out a notify for this process, if we haven't done so since a trace
86 static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
88 tsk->btrace_seq = blktrace_seq;
89 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
92 static void trace_note_time(struct blk_trace *bt)
99 words[0] = now.tv_sec;
100 words[1] = now.tv_nsec;
102 local_irq_save(flags);
103 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
104 local_irq_restore(flags);
107 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
116 ftrace_vprintk(fmt, args);
124 local_irq_save(flags);
125 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
127 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
130 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
131 local_irq_restore(flags);
133 EXPORT_SYMBOL_GPL(__trace_note_message);
135 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
138 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
140 if (sector < bt->start_lba || sector > bt->end_lba)
142 if (bt->pid && pid != bt->pid)
149 * Data direction bit lookup
151 static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), BLK_TC_ACT(BLK_TC_WRITE) };
153 /* The ilog2() calls fall out because they're constant */
154 #define MASK_TC_BIT(rw, __name) ( (rw & (1 << BIO_RW_ ## __name)) << \
155 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name) )
158 * The worker for the various blk_add_trace*() types. Fills out a
159 * blk_io_trace structure and places it in a per-cpu subbuffer.
161 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
162 int rw, u32 what, int error, int pdu_len, void *pdu_data)
164 struct task_struct *tsk = current;
165 struct ring_buffer_event *event = NULL;
166 struct blk_io_trace *t;
168 unsigned long *sequence;
172 if (unlikely(bt->trace_state != Blktrace_running || !blk_tracer_enabled))
175 what |= ddir_act[rw & WRITE];
176 what |= MASK_TC_BIT(rw, BARRIER);
177 what |= MASK_TC_BIT(rw, SYNC);
178 what |= MASK_TC_BIT(rw, AHEAD);
179 what |= MASK_TC_BIT(rw, META);
180 what |= MASK_TC_BIT(rw, DISCARD);
183 if (unlikely(act_log_check(bt, what, sector, pid)))
185 cpu = raw_smp_processor_id();
188 struct trace_entry *ent;
189 tracing_record_cmdline(current);
191 event = ring_buffer_lock_reserve(blk_tr->buffer,
192 sizeof(*t) + pdu_len, &flags);
196 ent = ring_buffer_event_data(event);
197 t = (struct blk_io_trace *)ent;
198 pc = preempt_count();
199 tracing_generic_entry_update(ent, 0, pc);
200 ent->type = TRACE_BLK;
205 * A word about the locking here - we disable interrupts to reserve
206 * some space in the relay per-cpu buffer, to prevent an irq
207 * from coming in and stepping on our toes.
209 local_irq_save(flags);
211 if (unlikely(tsk->btrace_seq != blktrace_seq))
212 trace_note_tsk(bt, tsk);
214 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
216 sequence = per_cpu_ptr(bt->sequence, cpu);
218 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
219 t->sequence = ++(*sequence);
220 t->time = ktime_to_ns(ktime_get());
229 t->pdu_len = pdu_len;
232 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
235 ring_buffer_unlock_commit(blk_tr->buffer, event, flags);
237 (blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC) == 0 &&
238 (trace_flags & TRACE_ITER_STACKTRACE) != 0)
239 __trace_stack(blk_tr, NULL, flags, 5, pc);
245 local_irq_restore(flags);
248 static struct dentry *blk_tree_root;
249 static DEFINE_MUTEX(blk_tree_mutex);
250 static unsigned int root_users;
252 static inline void blk_remove_root(void)
255 debugfs_remove(blk_tree_root);
256 blk_tree_root = NULL;
260 static void blk_remove_tree(struct dentry *dir)
262 mutex_lock(&blk_tree_mutex);
264 if (--root_users == 0)
266 mutex_unlock(&blk_tree_mutex);
269 static struct dentry *blk_create_tree(const char *blk_name)
271 struct dentry *dir = NULL;
274 mutex_lock(&blk_tree_mutex);
276 if (!blk_tree_root) {
277 blk_tree_root = debugfs_create_dir("block", NULL);
283 dir = debugfs_create_dir(blk_name, blk_tree_root);
287 /* Delete root only if we created it */
293 mutex_unlock(&blk_tree_mutex);
297 static void blk_trace_cleanup(struct blk_trace *bt)
299 relay_close(bt->rchan);
300 debugfs_remove(bt->msg_file);
301 debugfs_remove(bt->dropped_file);
302 blk_remove_tree(bt->dir);
303 free_percpu(bt->sequence);
304 free_percpu(bt->msg_data);
306 mutex_lock(&blk_probe_mutex);
307 if (atomic_dec_and_test(&blk_probes_ref))
308 blk_unregister_tracepoints();
309 mutex_unlock(&blk_probe_mutex);
312 int blk_trace_remove(struct request_queue *q)
314 struct blk_trace *bt;
316 bt = xchg(&q->blk_trace, NULL);
320 if (bt->trace_state == Blktrace_setup ||
321 bt->trace_state == Blktrace_stopped)
322 blk_trace_cleanup(bt);
326 EXPORT_SYMBOL_GPL(blk_trace_remove);
328 static int blk_dropped_open(struct inode *inode, struct file *filp)
330 filp->private_data = inode->i_private;
335 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
336 size_t count, loff_t *ppos)
338 struct blk_trace *bt = filp->private_data;
341 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
343 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
346 static const struct file_operations blk_dropped_fops = {
347 .owner = THIS_MODULE,
348 .open = blk_dropped_open,
349 .read = blk_dropped_read,
352 static int blk_msg_open(struct inode *inode, struct file *filp)
354 filp->private_data = inode->i_private;
359 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
360 size_t count, loff_t *ppos)
363 struct blk_trace *bt;
365 if (count > BLK_TN_MAX_MSG)
368 msg = kmalloc(count, GFP_KERNEL);
372 if (copy_from_user(msg, buffer, count)) {
377 bt = filp->private_data;
378 __trace_note_message(bt, "%s", msg);
384 static const struct file_operations blk_msg_fops = {
385 .owner = THIS_MODULE,
386 .open = blk_msg_open,
387 .write = blk_msg_write,
391 * Keep track of how many times we encountered a full subbuffer, to aid
392 * the user space app in telling how many lost events there were.
394 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
395 void *prev_subbuf, size_t prev_padding)
397 struct blk_trace *bt;
399 if (!relay_buf_full(buf))
402 bt = buf->chan->private_data;
403 atomic_inc(&bt->dropped);
407 static int blk_remove_buf_file_callback(struct dentry *dentry)
409 debugfs_remove(dentry);
413 static struct dentry *blk_create_buf_file_callback(const char *filename,
414 struct dentry *parent,
416 struct rchan_buf *buf,
419 return debugfs_create_file(filename, mode, parent, buf,
420 &relay_file_operations);
423 static struct rchan_callbacks blk_relay_callbacks = {
424 .subbuf_start = blk_subbuf_start_callback,
425 .create_buf_file = blk_create_buf_file_callback,
426 .remove_buf_file = blk_remove_buf_file_callback,
430 * Setup everything required to start tracing
432 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
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] == '/')
454 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
458 bt->sequence = alloc_percpu(unsigned long);
462 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG);
467 dir = blk_create_tree(buts->name);
473 atomic_set(&bt->dropped, 0);
476 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
477 if (!bt->dropped_file)
480 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
484 bt->rchan = relay_open("trace", dir, buts->buf_size,
485 buts->buf_nr, &blk_relay_callbacks, bt);
489 bt->act_mask = buts->act_mask;
491 bt->act_mask = (u16) -1;
493 bt->start_lba = buts->start_lba;
494 bt->end_lba = buts->end_lba;
499 bt->trace_state = Blktrace_setup;
501 mutex_lock(&blk_probe_mutex);
502 if (atomic_add_return(1, &blk_probes_ref) == 1) {
503 ret = blk_register_tracepoints();
507 mutex_unlock(&blk_probe_mutex);
510 old_bt = xchg(&q->blk_trace, bt);
512 (void) xchg(&q->blk_trace, old_bt);
518 atomic_dec(&blk_probes_ref);
519 mutex_unlock(&blk_probe_mutex);
522 blk_remove_tree(dir);
525 debugfs_remove(bt->msg_file);
526 if (bt->dropped_file)
527 debugfs_remove(bt->dropped_file);
528 free_percpu(bt->sequence);
529 free_percpu(bt->msg_data);
531 relay_close(bt->rchan);
537 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
540 struct blk_user_trace_setup buts;
543 ret = copy_from_user(&buts, arg, sizeof(buts));
547 ret = do_blk_trace_setup(q, name, dev, &buts);
551 if (copy_to_user(arg, &buts, sizeof(buts)))
556 EXPORT_SYMBOL_GPL(blk_trace_setup);
558 int blk_trace_startstop(struct request_queue *q, int start)
560 struct blk_trace *bt;
563 if ((bt = q->blk_trace) == NULL)
567 * For starting a trace, we can transition from a setup or stopped
568 * trace. For stopping a trace, the state must be running
572 if (bt->trace_state == Blktrace_setup ||
573 bt->trace_state == Blktrace_stopped) {
576 bt->trace_state = Blktrace_running;
582 if (bt->trace_state == Blktrace_running) {
583 bt->trace_state = Blktrace_stopped;
584 relay_flush(bt->rchan);
591 EXPORT_SYMBOL_GPL(blk_trace_startstop);
594 * blk_trace_ioctl: - handle the ioctls associated with tracing
595 * @bdev: the block device
596 * @cmd: the ioctl cmd
597 * @arg: the argument data, if any
600 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
602 struct request_queue *q;
604 char b[BDEVNAME_SIZE];
606 q = bdev_get_queue(bdev);
610 mutex_lock(&bdev->bd_mutex);
615 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
620 ret = blk_trace_startstop(q, start);
622 case BLKTRACETEARDOWN:
623 ret = blk_trace_remove(q);
630 mutex_unlock(&bdev->bd_mutex);
635 * blk_trace_shutdown: - stop and cleanup trace structures
636 * @q: the request queue associated with the device
639 void blk_trace_shutdown(struct request_queue *q)
642 blk_trace_startstop(q, 0);
652 * blk_add_trace_rq - Add a trace for a request oriented action
653 * @q: queue the io is for
654 * @rq: the source request
658 * Records an action against a request. Will log the bio offset + size.
661 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
664 struct blk_trace *bt = q->blk_trace;
665 int rw = rq->cmd_flags & 0x03;
670 if (blk_discard_rq(rq))
671 rw |= (1 << BIO_RW_DISCARD);
673 if (blk_pc_request(rq)) {
674 what |= BLK_TC_ACT(BLK_TC_PC);
675 __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
676 sizeof(rq->cmd), rq->cmd);
678 what |= BLK_TC_ACT(BLK_TC_FS);
679 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
680 rw, what, rq->errors, 0, NULL);
684 static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
686 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
689 static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
691 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
694 static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
696 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
699 static void blk_add_trace_rq_requeue(struct request_queue *q, struct request *rq)
701 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
704 static void blk_add_trace_rq_complete(struct request_queue *q, struct request *rq)
706 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
710 * blk_add_trace_bio - Add a trace for a bio oriented action
711 * @q: queue the io is for
712 * @bio: the source bio
716 * Records an action against a bio. Will log the bio offset + size.
719 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
722 struct blk_trace *bt = q->blk_trace;
727 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
728 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
731 static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
733 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
736 static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
738 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
741 static void blk_add_trace_bio_backmerge(struct request_queue *q, struct bio *bio)
743 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
746 static void blk_add_trace_bio_frontmerge(struct request_queue *q, struct bio *bio)
748 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
751 static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
753 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
756 static void blk_add_trace_getrq(struct request_queue *q, struct bio *bio, int rw)
759 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
761 struct blk_trace *bt = q->blk_trace;
764 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
769 static void blk_add_trace_sleeprq(struct request_queue *q, struct bio *bio, int rw)
772 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
774 struct blk_trace *bt = q->blk_trace;
777 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ, 0, 0, NULL);
781 static void blk_add_trace_plug(struct request_queue *q)
783 struct blk_trace *bt = q->blk_trace;
786 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
789 static void blk_add_trace_unplug_io(struct request_queue *q)
791 struct blk_trace *bt = q->blk_trace;
794 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
795 __be64 rpdu = cpu_to_be64(pdu);
797 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
798 sizeof(rpdu), &rpdu);
802 static void blk_add_trace_unplug_timer(struct request_queue *q)
804 struct blk_trace *bt = q->blk_trace;
807 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
808 __be64 rpdu = cpu_to_be64(pdu);
810 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
811 sizeof(rpdu), &rpdu);
815 static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
818 struct blk_trace *bt = q->blk_trace;
821 __be64 rpdu = cpu_to_be64(pdu);
823 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
824 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
825 sizeof(rpdu), &rpdu);
830 * blk_add_trace_remap - Add a trace for a remap operation
831 * @q: queue the io is for
832 * @bio: the source bio
833 * @dev: target device
834 * @from: source sector
838 * Device mapper or raid target sometimes need to split a bio because
839 * it spans a stripe (or similar). Add a trace for that action.
842 static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
843 dev_t dev, sector_t from, sector_t to)
845 struct blk_trace *bt = q->blk_trace;
846 struct blk_io_trace_remap r;
851 r.device = cpu_to_be32(dev);
852 r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
853 r.sector = cpu_to_be64(to);
855 __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
856 !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
860 * blk_add_driver_data - Add binary message with driver-specific data
861 * @q: queue the io is for
863 * @data: driver-specific data
864 * @len: length of driver-specific data
867 * Some drivers might want to write driver-specific data per request.
870 void blk_add_driver_data(struct request_queue *q,
872 void *data, size_t len)
874 struct blk_trace *bt = q->blk_trace;
879 if (blk_pc_request(rq))
880 __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
881 rq->errors, len, data);
883 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
884 0, BLK_TA_DRV_DATA, rq->errors, len, data);
886 EXPORT_SYMBOL_GPL(blk_add_driver_data);
888 static int blk_register_tracepoints(void)
892 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
894 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
896 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
898 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
900 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
902 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
904 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
906 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
908 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
910 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
912 ret = register_trace_block_getrq(blk_add_trace_getrq);
914 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
916 ret = register_trace_block_plug(blk_add_trace_plug);
918 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
920 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
922 ret = register_trace_block_split(blk_add_trace_split);
924 ret = register_trace_block_remap(blk_add_trace_remap);
929 static void blk_unregister_tracepoints(void)
931 unregister_trace_block_remap(blk_add_trace_remap);
932 unregister_trace_block_split(blk_add_trace_split);
933 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
934 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
935 unregister_trace_block_plug(blk_add_trace_plug);
936 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
937 unregister_trace_block_getrq(blk_add_trace_getrq);
938 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
939 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
940 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
941 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
942 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
943 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
944 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
945 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
946 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
947 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
949 tracepoint_synchronize_unregister();
953 * struct blk_io_tracer formatting routines
956 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
960 if (t->action & BLK_TC_DISCARD) rwbs[i++] = 'D';
961 else if (t->action & BLK_TC_WRITE) rwbs[i++] = 'W';
962 else if (t->bytes) rwbs[i++] = 'R';
963 else rwbs[i++] = 'N';
965 if (t->action & BLK_TC_AHEAD) rwbs[i++] = 'A';
966 if (t->action & BLK_TC_BARRIER) rwbs[i++] = 'B';
967 if (t->action & BLK_TC_SYNC) rwbs[i++] = 'S';
968 if (t->action & BLK_TC_META) rwbs[i++] = 'M';
974 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
976 return (const struct blk_io_trace *)ent;
979 static inline const void *pdu_start(const struct trace_entry *ent)
981 return te_blk_io_trace(ent) + 1;
984 static inline u32 t_sec(const struct trace_entry *ent)
986 return te_blk_io_trace(ent)->bytes >> 9;
989 static inline unsigned long long t_sector(const struct trace_entry *ent)
991 return te_blk_io_trace(ent)->sector;
994 static inline __u16 t_error(const struct trace_entry *ent)
996 return te_blk_io_trace(ent)->sector;
999 static __u64 get_pdu_int(const struct trace_entry *ent)
1001 const __u64 *val = pdu_start(ent);
1002 return be64_to_cpu(*val);
1005 static void get_pdu_remap(const struct trace_entry *ent,
1006 struct blk_io_trace_remap *r)
1008 const struct blk_io_trace_remap *__r = pdu_start(ent);
1009 __u64 sector = __r->sector;
1011 r->device = be32_to_cpu(__r->device);
1012 r->device_from = be32_to_cpu(__r->device_from);
1013 r->sector = be64_to_cpu(sector);
1016 static int blk_log_action_iter(struct trace_iterator *iter, const char *act)
1019 unsigned long long ts = ns2usecs(iter->ts);
1020 unsigned long usec_rem = do_div(ts, USEC_PER_SEC);
1021 unsigned secs = (unsigned long)ts;
1022 const struct trace_entry *ent = iter->ent;
1023 const struct blk_io_trace *t = (const struct blk_io_trace *)ent;
1027 return trace_seq_printf(&iter->seq,
1028 "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ",
1029 MAJOR(t->device), MINOR(t->device), iter->cpu,
1030 secs, usec_rem, ent->pid, act, rwbs);
1033 static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t,
1038 return trace_seq_printf(s, "%3d,%-3d %2s %3s ",
1039 MAJOR(t->device), MINOR(t->device), act, rwbs);
1042 static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1044 const char *cmd = trace_find_cmdline(ent->pid);
1047 return trace_seq_printf(s, "%llu + %u [%s]\n",
1048 t_sector(ent), t_sec(ent), cmd);
1049 return trace_seq_printf(s, "[%s]\n", cmd);
1052 static int blk_log_with_error(struct trace_seq *s, const struct trace_entry *ent)
1055 return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent),
1056 t_sec(ent), t_error(ent));
1057 return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent));
1060 static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1062 struct blk_io_trace_remap r = { .device = 0, };
1064 get_pdu_remap(ent, &r);
1065 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1067 t_sec(ent), MAJOR(r.device), MINOR(r.device),
1068 (unsigned long long)r.sector);
1071 static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1073 return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid));
1076 static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1078 return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid),
1082 static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1084 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1085 get_pdu_int(ent), trace_find_cmdline(ent->pid));
1089 * struct tracer operations
1092 static void blk_tracer_print_header(struct seq_file *m)
1094 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1096 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1100 static void blk_tracer_start(struct trace_array *tr)
1104 tr->time_start = ftrace_now(tr->cpu);
1106 for_each_online_cpu(cpu)
1107 tracing_reset(tr, cpu);
1109 mutex_lock(&blk_probe_mutex);
1110 if (atomic_add_return(1, &blk_probes_ref) == 1)
1111 if (blk_register_tracepoints())
1112 atomic_dec(&blk_probes_ref);
1113 mutex_unlock(&blk_probe_mutex);
1116 static int blk_tracer_init(struct trace_array *tr)
1119 blk_tracer_start(tr);
1120 mutex_lock(&blk_probe_mutex);
1121 blk_tracer_enabled++;
1122 mutex_unlock(&blk_probe_mutex);
1126 static void blk_tracer_stop(struct trace_array *tr)
1128 mutex_lock(&blk_probe_mutex);
1129 if (atomic_dec_and_test(&blk_probes_ref))
1130 blk_unregister_tracepoints();
1131 mutex_unlock(&blk_probe_mutex);
1134 static void blk_tracer_reset(struct trace_array *tr)
1136 if (!atomic_read(&blk_probes_ref))
1139 mutex_lock(&blk_probe_mutex);
1140 blk_tracer_enabled--;
1141 WARN_ON(blk_tracer_enabled < 0);
1142 mutex_unlock(&blk_probe_mutex);
1144 blk_tracer_stop(tr);
1149 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1150 } what2act[] __read_mostly = {
1151 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1152 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1153 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1154 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1155 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1156 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1157 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1158 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1159 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1160 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1161 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1162 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1163 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1164 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1165 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1168 static int blk_trace_event_print(struct trace_seq *s, struct trace_entry *ent,
1171 const struct blk_io_trace *t = (struct blk_io_trace *)ent;
1172 const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1175 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1176 ret = trace_seq_printf(s, "Bad pc action %x\n", what);
1178 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1179 ret = blk_log_action_seq(s, t, what2act[what].act[long_act]);
1181 ret = what2act[what].print(s, ent);
1184 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1187 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1189 const struct blk_io_trace *t;
1193 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1194 return TRACE_TYPE_UNHANDLED;
1196 t = (const struct blk_io_trace *)iter->ent;
1197 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1199 if (unlikely(what == 0 || what > ARRAY_SIZE(what2act)))
1200 ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what);
1202 const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1203 ret = blk_log_action_iter(iter, what2act[what].act[long_act]);
1205 ret = what2act[what].print(&iter->seq, iter->ent);
1208 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1211 static struct tracer blk_tracer __read_mostly = {
1213 .init = blk_tracer_init,
1214 .reset = blk_tracer_reset,
1215 .start = blk_tracer_start,
1216 .stop = blk_tracer_stop,
1217 .print_header = blk_tracer_print_header,
1218 .print_line = blk_tracer_print_line,
1219 .flags = &blk_tracer_flags,
1222 static struct trace_event trace_blk_event = {
1224 .trace = blk_trace_event_print,
1225 .latency_trace = blk_trace_event_print,
1226 .raw = trace_nop_print,
1227 .hex = trace_nop_print,
1228 .binary = trace_nop_print,
1231 static int __init init_blk_tracer(void)
1233 if (!register_ftrace_event(&trace_blk_event)) {
1234 pr_warning("Warning: could not register block events\n");
1238 if (register_tracer(&blk_tracer) != 0) {
1239 pr_warning("Warning: could not register the block tracer\n");
1240 unregister_ftrace_event(&trace_blk_event);
1247 device_initcall(init_blk_tracer);
1249 static int blk_trace_remove_queue(struct request_queue *q)
1251 struct blk_trace *bt;
1253 bt = xchg(&q->blk_trace, NULL);
1262 * Setup everything required to start tracing
1264 static int blk_trace_setup_queue(struct request_queue *q, dev_t dev)
1266 struct blk_trace *old_bt, *bt = NULL;
1270 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1275 bt->act_mask = (u16)-1;
1276 bt->end_lba = -1ULL;
1277 bt->trace_state = Blktrace_running;
1279 old_bt = xchg(&q->blk_trace, bt);
1280 if (old_bt != NULL) {
1281 (void)xchg(&q->blk_trace, old_bt);
1291 * sysfs interface to enable and configure tracing
1294 static ssize_t sysfs_blk_trace_enable_show(struct device *dev,
1295 struct device_attribute *attr,
1298 struct hd_struct *p = dev_to_part(dev);
1299 struct block_device *bdev;
1300 ssize_t ret = -ENXIO;
1303 bdev = bdget(part_devt(p));
1305 struct request_queue *q = bdev_get_queue(bdev);
1308 mutex_lock(&bdev->bd_mutex);
1309 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1310 mutex_unlock(&bdev->bd_mutex);
1320 static ssize_t sysfs_blk_trace_enable_store(struct device *dev,
1321 struct device_attribute *attr,
1322 const char *buf, size_t count)
1324 struct block_device *bdev;
1325 struct request_queue *q;
1326 struct hd_struct *p;
1328 ssize_t ret = -ENXIO;
1330 if (count == 0 || sscanf(buf, "%d", &value) != 1)
1334 p = dev_to_part(dev);
1335 bdev = bdget(part_devt(p));
1337 goto out_unlock_kernel;
1339 q = bdev_get_queue(bdev);
1343 mutex_lock(&bdev->bd_mutex);
1345 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1347 ret = blk_trace_remove_queue(q);
1348 mutex_unlock(&bdev->bd_mutex);
1360 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1361 struct device_attribute *attr,
1363 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1364 struct device_attribute *attr,
1365 const char *buf, size_t count);
1366 #define BLK_TRACE_DEVICE_ATTR(_name) \
1367 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1368 sysfs_blk_trace_attr_show, \
1369 sysfs_blk_trace_attr_store)
1371 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
1372 sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store);
1373 static BLK_TRACE_DEVICE_ATTR(act_mask);
1374 static BLK_TRACE_DEVICE_ATTR(pid);
1375 static BLK_TRACE_DEVICE_ATTR(start_lba);
1376 static BLK_TRACE_DEVICE_ATTR(end_lba);
1378 static struct attribute *blk_trace_attrs[] = {
1379 &dev_attr_enable.attr,
1380 &dev_attr_act_mask.attr,
1382 &dev_attr_start_lba.attr,
1383 &dev_attr_end_lba.attr,
1387 struct attribute_group blk_trace_attr_group = {
1389 .attrs = blk_trace_attrs,
1392 static int blk_str2act_mask(const char *str)
1395 char *copy = kstrdup(str, GFP_KERNEL), *s;
1403 char *sep = strchr(s, ',');
1408 if (strcasecmp(s, "barrier") == 0)
1409 mask |= BLK_TC_BARRIER;
1410 else if (strcasecmp(s, "complete") == 0)
1411 mask |= BLK_TC_COMPLETE;
1412 else if (strcasecmp(s, "fs") == 0)
1414 else if (strcasecmp(s, "issue") == 0)
1415 mask |= BLK_TC_ISSUE;
1416 else if (strcasecmp(s, "pc") == 0)
1418 else if (strcasecmp(s, "queue") == 0)
1419 mask |= BLK_TC_QUEUE;
1420 else if (strcasecmp(s, "read") == 0)
1421 mask |= BLK_TC_READ;
1422 else if (strcasecmp(s, "requeue") == 0)
1423 mask |= BLK_TC_REQUEUE;
1424 else if (strcasecmp(s, "sync") == 0)
1425 mask |= BLK_TC_SYNC;
1426 else if (strcasecmp(s, "write") == 0)
1427 mask |= BLK_TC_WRITE;
1439 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1440 struct device_attribute *attr,
1443 struct hd_struct *p = dev_to_part(dev);
1444 struct request_queue *q;
1445 struct block_device *bdev;
1446 ssize_t ret = -ENXIO;
1449 bdev = bdget(part_devt(p));
1451 goto out_unlock_kernel;
1453 q = bdev_get_queue(bdev);
1456 mutex_lock(&bdev->bd_mutex);
1457 if (q->blk_trace == NULL)
1458 ret = sprintf(buf, "disabled\n");
1459 else if (attr == &dev_attr_act_mask)
1460 ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask);
1461 else if (attr == &dev_attr_pid)
1462 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1463 else if (attr == &dev_attr_start_lba)
1464 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1465 else if (attr == &dev_attr_end_lba)
1466 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1467 mutex_unlock(&bdev->bd_mutex);
1475 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1476 struct device_attribute *attr,
1477 const char *buf, size_t count)
1479 struct block_device *bdev;
1480 struct request_queue *q;
1481 struct hd_struct *p;
1483 ssize_t ret = -ENXIO;
1488 if (attr == &dev_attr_act_mask) {
1489 if (sscanf(buf, "%llx", &value) != 1) {
1490 /* Assume it is a list of trace category names */
1491 value = blk_str2act_mask(buf);
1495 } else if (sscanf(buf, "%llu", &value) != 1)
1499 p = dev_to_part(dev);
1500 bdev = bdget(part_devt(p));
1502 goto out_unlock_kernel;
1504 q = bdev_get_queue(bdev);
1508 mutex_lock(&bdev->bd_mutex);
1510 if (q->blk_trace == NULL)
1511 ret = blk_trace_setup_queue(q, bdev->bd_dev);
1514 if (attr == &dev_attr_act_mask)
1515 q->blk_trace->act_mask = value;
1516 else if (attr == &dev_attr_pid)
1517 q->blk_trace->pid = value;
1518 else if (attr == &dev_attr_start_lba)
1519 q->blk_trace->start_lba = value;
1520 else if (attr == &dev_attr_end_lba)
1521 q->blk_trace->end_lba = value;
1524 mutex_unlock(&bdev->bd_mutex);