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>
29 static unsigned int blktrace_seq __read_mostly = 1;
31 /* Global reference count of probes */
32 static DEFINE_MUTEX(blk_probe_mutex);
33 static atomic_t blk_probes_ref = ATOMIC_INIT(0);
35 static int blk_register_tracepoints(void);
36 static void blk_unregister_tracepoints(void);
39 * Send out a notify message.
41 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
42 const void *data, size_t len)
44 struct blk_io_trace *t;
46 t = relay_reserve(bt->rchan, sizeof(*t) + len);
48 const int cpu = smp_processor_id();
50 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
51 t->time = ktime_to_ns(ktime_get());
57 memcpy((void *) t + sizeof(*t), data, len);
62 * Send out a notify for this process, if we haven't done so since a trace
65 static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
67 tsk->btrace_seq = blktrace_seq;
68 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
71 static void trace_note_time(struct blk_trace *bt)
78 words[0] = now.tv_sec;
79 words[1] = now.tv_nsec;
81 local_irq_save(flags);
82 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
83 local_irq_restore(flags);
86 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
93 local_irq_save(flags);
94 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
96 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
99 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
100 local_irq_restore(flags);
102 EXPORT_SYMBOL_GPL(__trace_note_message);
104 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
107 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
109 if (sector < bt->start_lba || sector > bt->end_lba)
111 if (bt->pid && pid != bt->pid)
118 * Data direction bit lookup
120 static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), BLK_TC_ACT(BLK_TC_WRITE) };
122 /* The ilog2() calls fall out because they're constant */
123 #define MASK_TC_BIT(rw, __name) ( (rw & (1 << BIO_RW_ ## __name)) << \
124 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name) )
127 * The worker for the various blk_add_trace*() types. Fills out a
128 * blk_io_trace structure and places it in a per-cpu subbuffer.
130 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
131 int rw, u32 what, int error, int pdu_len, void *pdu_data)
133 struct task_struct *tsk = current;
134 struct blk_io_trace *t;
136 unsigned long *sequence;
140 if (unlikely(bt->trace_state != Blktrace_running))
143 what |= ddir_act[rw & WRITE];
144 what |= MASK_TC_BIT(rw, BARRIER);
145 what |= MASK_TC_BIT(rw, SYNCIO);
146 what |= MASK_TC_BIT(rw, AHEAD);
147 what |= MASK_TC_BIT(rw, META);
148 what |= MASK_TC_BIT(rw, DISCARD);
151 if (unlikely(act_log_check(bt, what, sector, pid)))
155 * A word about the locking here - we disable interrupts to reserve
156 * some space in the relay per-cpu buffer, to prevent an irq
157 * from coming in and stepping on our toes.
159 local_irq_save(flags);
161 if (unlikely(tsk->btrace_seq != blktrace_seq))
162 trace_note_tsk(bt, tsk);
164 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
166 cpu = smp_processor_id();
167 sequence = per_cpu_ptr(bt->sequence, cpu);
169 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
170 t->sequence = ++(*sequence);
171 t->time = ktime_to_ns(ktime_get());
179 t->pdu_len = pdu_len;
182 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
185 local_irq_restore(flags);
188 static struct dentry *blk_tree_root;
189 static DEFINE_MUTEX(blk_tree_mutex);
191 static void blk_trace_cleanup(struct blk_trace *bt)
193 debugfs_remove(bt->msg_file);
194 debugfs_remove(bt->dropped_file);
195 relay_close(bt->rchan);
196 free_percpu(bt->sequence);
197 free_percpu(bt->msg_data);
199 mutex_lock(&blk_probe_mutex);
200 if (atomic_dec_and_test(&blk_probes_ref))
201 blk_unregister_tracepoints();
202 mutex_unlock(&blk_probe_mutex);
205 int blk_trace_remove(struct request_queue *q)
207 struct blk_trace *bt;
209 bt = xchg(&q->blk_trace, NULL);
213 if (bt->trace_state == Blktrace_setup ||
214 bt->trace_state == Blktrace_stopped)
215 blk_trace_cleanup(bt);
219 EXPORT_SYMBOL_GPL(blk_trace_remove);
221 static int blk_dropped_open(struct inode *inode, struct file *filp)
223 filp->private_data = inode->i_private;
228 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
229 size_t count, loff_t *ppos)
231 struct blk_trace *bt = filp->private_data;
234 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
236 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
239 static const struct file_operations blk_dropped_fops = {
240 .owner = THIS_MODULE,
241 .open = blk_dropped_open,
242 .read = blk_dropped_read,
245 static int blk_msg_open(struct inode *inode, struct file *filp)
247 filp->private_data = inode->i_private;
252 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
253 size_t count, loff_t *ppos)
256 struct blk_trace *bt;
258 if (count > BLK_TN_MAX_MSG)
261 msg = kmalloc(count, GFP_KERNEL);
265 if (copy_from_user(msg, buffer, count)) {
270 bt = filp->private_data;
271 __trace_note_message(bt, "%s", msg);
277 static const struct file_operations blk_msg_fops = {
278 .owner = THIS_MODULE,
279 .open = blk_msg_open,
280 .write = blk_msg_write,
284 * Keep track of how many times we encountered a full subbuffer, to aid
285 * the user space app in telling how many lost events there were.
287 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
288 void *prev_subbuf, size_t prev_padding)
290 struct blk_trace *bt;
292 if (!relay_buf_full(buf))
295 bt = buf->chan->private_data;
296 atomic_inc(&bt->dropped);
300 static int blk_remove_buf_file_callback(struct dentry *dentry)
302 struct dentry *parent = dentry->d_parent;
303 debugfs_remove(dentry);
306 * this will fail for all but the last file, but that is ok. what we
307 * care about is the top level buts->name directory going away, when
308 * the last trace file is gone. Then we don't have to rmdir() that
309 * manually on trace stop, so it nicely solves the issue with
310 * force killing of running traces.
313 debugfs_remove(parent);
317 static struct dentry *blk_create_buf_file_callback(const char *filename,
318 struct dentry *parent,
320 struct rchan_buf *buf,
323 return debugfs_create_file(filename, mode, parent, buf,
324 &relay_file_operations);
327 static struct rchan_callbacks blk_relay_callbacks = {
328 .subbuf_start = blk_subbuf_start_callback,
329 .create_buf_file = blk_create_buf_file_callback,
330 .remove_buf_file = blk_remove_buf_file_callback,
334 * Setup everything required to start tracing
336 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
337 struct blk_user_trace_setup *buts)
339 struct blk_trace *old_bt, *bt = NULL;
340 struct dentry *dir = NULL;
343 if (!buts->buf_size || !buts->buf_nr)
346 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
347 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
350 * some device names have larger paths - convert the slashes
351 * to underscores for this to work as expected
353 for (i = 0; i < strlen(buts->name); i++)
354 if (buts->name[i] == '/')
358 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
362 bt->sequence = alloc_percpu(unsigned long);
366 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
372 if (!blk_tree_root) {
373 blk_tree_root = debugfs_create_dir("block", NULL);
378 dir = debugfs_create_dir(buts->name, blk_tree_root);
385 atomic_set(&bt->dropped, 0);
388 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
389 if (!bt->dropped_file)
392 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
396 bt->rchan = relay_open("trace", dir, buts->buf_size,
397 buts->buf_nr, &blk_relay_callbacks, bt);
401 bt->act_mask = buts->act_mask;
403 bt->act_mask = (u16) -1;
405 bt->start_lba = buts->start_lba;
406 bt->end_lba = buts->end_lba;
411 bt->trace_state = Blktrace_setup;
413 mutex_lock(&blk_probe_mutex);
414 if (atomic_add_return(1, &blk_probes_ref) == 1) {
415 ret = blk_register_tracepoints();
419 mutex_unlock(&blk_probe_mutex);
422 old_bt = xchg(&q->blk_trace, bt);
424 (void) xchg(&q->blk_trace, old_bt);
430 atomic_dec(&blk_probes_ref);
431 mutex_unlock(&blk_probe_mutex);
435 debugfs_remove(bt->msg_file);
436 if (bt->dropped_file)
437 debugfs_remove(bt->dropped_file);
438 free_percpu(bt->sequence);
439 free_percpu(bt->msg_data);
441 relay_close(bt->rchan);
447 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
450 struct blk_user_trace_setup buts;
453 ret = copy_from_user(&buts, arg, sizeof(buts));
457 ret = do_blk_trace_setup(q, name, dev, &buts);
461 if (copy_to_user(arg, &buts, sizeof(buts)))
466 EXPORT_SYMBOL_GPL(blk_trace_setup);
468 int blk_trace_startstop(struct request_queue *q, int start)
470 struct blk_trace *bt;
473 if ((bt = q->blk_trace) == NULL)
477 * For starting a trace, we can transition from a setup or stopped
478 * trace. For stopping a trace, the state must be running
482 if (bt->trace_state == Blktrace_setup ||
483 bt->trace_state == Blktrace_stopped) {
486 bt->trace_state = Blktrace_running;
492 if (bt->trace_state == Blktrace_running) {
493 bt->trace_state = Blktrace_stopped;
494 relay_flush(bt->rchan);
501 EXPORT_SYMBOL_GPL(blk_trace_startstop);
504 * blk_trace_ioctl: - handle the ioctls associated with tracing
505 * @bdev: the block device
506 * @cmd: the ioctl cmd
507 * @arg: the argument data, if any
510 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
512 struct request_queue *q;
514 char b[BDEVNAME_SIZE];
516 q = bdev_get_queue(bdev);
520 mutex_lock(&bdev->bd_mutex);
525 ret = blk_trace_setup(q, b, bdev->bd_dev, arg);
530 ret = blk_trace_startstop(q, start);
532 case BLKTRACETEARDOWN:
533 ret = blk_trace_remove(q);
540 mutex_unlock(&bdev->bd_mutex);
545 * blk_trace_shutdown: - stop and cleanup trace structures
546 * @q: the request queue associated with the device
549 void blk_trace_shutdown(struct request_queue *q)
552 blk_trace_startstop(q, 0);
562 * blk_add_trace_rq - Add a trace for a request oriented action
563 * @q: queue the io is for
564 * @rq: the source request
568 * Records an action against a request. Will log the bio offset + size.
571 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
574 struct blk_trace *bt = q->blk_trace;
575 int rw = rq->cmd_flags & 0x03;
580 if (blk_discard_rq(rq))
581 rw |= (1 << BIO_RW_DISCARD);
583 if (blk_pc_request(rq)) {
584 what |= BLK_TC_ACT(BLK_TC_PC);
585 __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors,
586 sizeof(rq->cmd), rq->cmd);
588 what |= BLK_TC_ACT(BLK_TC_FS);
589 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
590 rw, what, rq->errors, 0, NULL);
594 static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
596 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
599 static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
601 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
604 static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
606 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
609 static void blk_add_trace_rq_requeue(struct request_queue *q, struct request *rq)
611 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
614 static void blk_add_trace_rq_complete(struct request_queue *q, struct request *rq)
616 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
620 * blk_add_trace_bio - Add a trace for a bio oriented action
621 * @q: queue the io is for
622 * @bio: the source bio
626 * Records an action against a bio. Will log the bio offset + size.
629 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
632 struct blk_trace *bt = q->blk_trace;
637 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
638 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
641 static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
643 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
646 static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
648 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
651 static void blk_add_trace_bio_backmerge(struct request_queue *q, struct bio *bio)
653 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
656 static void blk_add_trace_bio_frontmerge(struct request_queue *q, struct bio *bio)
658 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
661 static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
663 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
666 static void blk_add_trace_getrq(struct request_queue *q, struct bio *bio, int rw)
669 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
671 struct blk_trace *bt = q->blk_trace;
674 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
679 static void blk_add_trace_sleeprq(struct request_queue *q, struct bio *bio, int rw)
682 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
684 struct blk_trace *bt = q->blk_trace;
687 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ, 0, 0, NULL);
691 static void blk_add_trace_plug(struct request_queue *q)
693 struct blk_trace *bt = q->blk_trace;
696 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
699 static void blk_add_trace_unplug_io(struct request_queue *q)
701 struct blk_trace *bt = q->blk_trace;
704 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
705 __be64 rpdu = cpu_to_be64(pdu);
707 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
708 sizeof(rpdu), &rpdu);
712 static void blk_add_trace_unplug_timer(struct request_queue *q)
714 struct blk_trace *bt = q->blk_trace;
717 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
718 __be64 rpdu = cpu_to_be64(pdu);
720 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
721 sizeof(rpdu), &rpdu);
725 static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
728 struct blk_trace *bt = q->blk_trace;
731 __be64 rpdu = cpu_to_be64(pdu);
733 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
734 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
735 sizeof(rpdu), &rpdu);
740 * blk_add_trace_remap - Add a trace for a remap operation
741 * @q: queue the io is for
742 * @bio: the source bio
743 * @dev: target device
744 * @from: source sector
748 * Device mapper or raid target sometimes need to split a bio because
749 * it spans a stripe (or similar). Add a trace for that action.
752 static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
753 dev_t dev, sector_t from, sector_t to)
755 struct blk_trace *bt = q->blk_trace;
756 struct blk_io_trace_remap r;
761 r.device = cpu_to_be32(dev);
762 r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev);
763 r.sector = cpu_to_be64(to);
765 __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP,
766 !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
770 * blk_add_driver_data - Add binary message with driver-specific data
771 * @q: queue the io is for
773 * @data: driver-specific data
774 * @len: length of driver-specific data
777 * Some drivers might want to write driver-specific data per request.
780 void blk_add_driver_data(struct request_queue *q,
782 void *data, size_t len)
784 struct blk_trace *bt = q->blk_trace;
789 if (blk_pc_request(rq))
790 __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA,
791 rq->errors, len, data);
793 __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9,
794 0, BLK_TA_DRV_DATA, rq->errors, len, data);
796 EXPORT_SYMBOL_GPL(blk_add_driver_data);
798 static int blk_register_tracepoints(void)
802 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
804 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
806 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
808 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
810 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
812 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
814 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
816 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
818 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
820 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
822 ret = register_trace_block_getrq(blk_add_trace_getrq);
824 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
826 ret = register_trace_block_plug(blk_add_trace_plug);
828 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
830 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
832 ret = register_trace_block_split(blk_add_trace_split);
834 ret = register_trace_block_remap(blk_add_trace_remap);
839 static void blk_unregister_tracepoints(void)
841 unregister_trace_block_remap(blk_add_trace_remap);
842 unregister_trace_block_split(blk_add_trace_split);
843 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
844 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
845 unregister_trace_block_plug(blk_add_trace_plug);
846 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
847 unregister_trace_block_getrq(blk_add_trace_getrq);
848 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
849 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
850 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
851 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
852 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
853 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
854 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
855 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
856 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
857 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
859 tracepoint_synchronize_unregister();