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/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>
31 #include "../../block/blk.h"
33 #include <trace/events/block.h>
35 #include "trace_output.h"
37 #ifdef CONFIG_BLK_DEV_IO_TRACE
39 static unsigned int blktrace_seq __read_mostly = 1;
41 static struct trace_array *blk_tr;
42 static bool blk_tracer_enabled __read_mostly;
44 static LIST_HEAD(running_trace_list);
45 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock);
47 /* Select an alternative, minimalistic output than the original one */
48 #define TRACE_BLK_OPT_CLASSIC 0x1
50 static struct tracer_opt blk_tracer_opts[] = {
51 /* Default disable the minimalistic output */
52 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
56 static struct tracer_flags blk_tracer_flags = {
58 .opts = blk_tracer_opts,
61 /* Global reference count of probes */
62 static atomic_t blk_probes_ref = ATOMIC_INIT(0);
64 static void blk_register_tracepoints(void);
65 static void blk_unregister_tracepoints(void);
68 * Send out a notify message.
70 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
71 const void *data, size_t len)
73 struct blk_io_trace *t;
74 struct ring_buffer_event *event = NULL;
75 struct ring_buffer *buffer = NULL;
77 int cpu = smp_processor_id();
78 bool blk_tracer = blk_tracer_enabled;
81 buffer = blk_tr->trace_buffer.buffer;
83 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
88 t = ring_buffer_event_data(event);
95 t = relay_reserve(bt->rchan, sizeof(*t) + len);
97 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
98 t->time = ktime_to_ns(ktime_get());
105 memcpy((void *) t + sizeof(*t), data, len);
108 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
113 * Send out a notify for this process, if we haven't done so since a trace
116 static void trace_note_tsk(struct task_struct *tsk)
119 struct blk_trace *bt;
121 tsk->btrace_seq = blktrace_seq;
122 spin_lock_irqsave(&running_trace_lock, flags);
123 list_for_each_entry(bt, &running_trace_list, running_list) {
124 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm,
127 spin_unlock_irqrestore(&running_trace_lock, flags);
130 static void trace_note_time(struct blk_trace *bt)
132 struct timespec64 now;
136 /* need to check user space to see if this breaks in y2038 or y2106 */
137 ktime_get_real_ts64(&now);
138 words[0] = (u32)now.tv_sec;
139 words[1] = now.tv_nsec;
141 local_irq_save(flags);
142 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
143 local_irq_restore(flags);
146 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
153 if (unlikely(bt->trace_state != Blktrace_running &&
154 !blk_tracer_enabled))
158 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
159 * message to the trace.
161 if (!(bt->act_mask & BLK_TC_NOTIFY))
164 local_irq_save(flags);
165 buf = this_cpu_ptr(bt->msg_data);
167 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
170 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
171 local_irq_restore(flags);
173 EXPORT_SYMBOL_GPL(__trace_note_message);
175 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
178 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
180 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
182 if (bt->pid && pid != bt->pid)
189 * Data direction bit lookup
191 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
192 BLK_TC_ACT(BLK_TC_WRITE) };
194 #define BLK_TC_RAHEAD BLK_TC_AHEAD
195 #define BLK_TC_PREFLUSH BLK_TC_FLUSH
197 /* The ilog2() calls fall out because they're constant */
198 #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
199 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
202 * The worker for the various blk_add_trace*() types. Fills out a
203 * blk_io_trace structure and places it in a per-cpu subbuffer.
205 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
206 int op, int op_flags, u32 what, int error, int pdu_len,
209 struct task_struct *tsk = current;
210 struct ring_buffer_event *event = NULL;
211 struct ring_buffer *buffer = NULL;
212 struct blk_io_trace *t;
213 unsigned long flags = 0;
214 unsigned long *sequence;
217 bool blk_tracer = blk_tracer_enabled;
219 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
222 what |= ddir_act[op_is_write(op) ? WRITE : READ];
223 what |= MASK_TC_BIT(op_flags, SYNC);
224 what |= MASK_TC_BIT(op_flags, RAHEAD);
225 what |= MASK_TC_BIT(op_flags, META);
226 what |= MASK_TC_BIT(op_flags, PREFLUSH);
227 what |= MASK_TC_BIT(op_flags, FUA);
228 if (op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)
229 what |= BLK_TC_ACT(BLK_TC_DISCARD);
230 if (op == REQ_OP_FLUSH)
231 what |= BLK_TC_ACT(BLK_TC_FLUSH);
234 if (act_log_check(bt, what, sector, pid))
236 cpu = raw_smp_processor_id();
239 tracing_record_cmdline(current);
241 buffer = blk_tr->trace_buffer.buffer;
242 pc = preempt_count();
243 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
244 sizeof(*t) + pdu_len,
248 t = ring_buffer_event_data(event);
252 if (unlikely(tsk->btrace_seq != blktrace_seq))
256 * A word about the locking here - we disable interrupts to reserve
257 * some space in the relay per-cpu buffer, to prevent an irq
258 * from coming in and stepping on our toes.
260 local_irq_save(flags);
261 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
263 sequence = per_cpu_ptr(bt->sequence, cpu);
265 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
266 t->sequence = ++(*sequence);
267 t->time = ktime_to_ns(ktime_get());
270 * These two are not needed in ftrace as they are in the
271 * generic trace_entry, filled by tracing_generic_entry_update,
272 * but for the trace_event->bin() synthesizer benefit we do it
283 t->pdu_len = pdu_len;
286 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
289 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
294 local_irq_restore(flags);
297 static void blk_trace_free(struct blk_trace *bt)
299 debugfs_remove(bt->msg_file);
300 debugfs_remove(bt->dropped_file);
301 relay_close(bt->rchan);
302 debugfs_remove(bt->dir);
303 free_percpu(bt->sequence);
304 free_percpu(bt->msg_data);
308 static void blk_trace_cleanup(struct blk_trace *bt)
311 if (atomic_dec_and_test(&blk_probes_ref))
312 blk_unregister_tracepoints();
315 int blk_trace_remove(struct request_queue *q)
317 struct blk_trace *bt;
319 bt = xchg(&q->blk_trace, NULL);
323 if (bt->trace_state != Blktrace_running)
324 blk_trace_cleanup(bt);
328 EXPORT_SYMBOL_GPL(blk_trace_remove);
330 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
331 size_t count, loff_t *ppos)
333 struct blk_trace *bt = filp->private_data;
336 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
338 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
341 static const struct file_operations blk_dropped_fops = {
342 .owner = THIS_MODULE,
344 .read = blk_dropped_read,
345 .llseek = default_llseek,
348 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
349 size_t count, loff_t *ppos)
352 struct blk_trace *bt;
354 if (count >= BLK_TN_MAX_MSG)
357 msg = memdup_user_nul(buffer, count);
361 bt = filp->private_data;
362 __trace_note_message(bt, "%s", msg);
368 static const struct file_operations blk_msg_fops = {
369 .owner = THIS_MODULE,
371 .write = blk_msg_write,
372 .llseek = noop_llseek,
376 * Keep track of how many times we encountered a full subbuffer, to aid
377 * the user space app in telling how many lost events there were.
379 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
380 void *prev_subbuf, size_t prev_padding)
382 struct blk_trace *bt;
384 if (!relay_buf_full(buf))
387 bt = buf->chan->private_data;
388 atomic_inc(&bt->dropped);
392 static int blk_remove_buf_file_callback(struct dentry *dentry)
394 debugfs_remove(dentry);
399 static struct dentry *blk_create_buf_file_callback(const char *filename,
400 struct dentry *parent,
402 struct rchan_buf *buf,
405 return debugfs_create_file(filename, mode, parent, buf,
406 &relay_file_operations);
409 static struct rchan_callbacks blk_relay_callbacks = {
410 .subbuf_start = blk_subbuf_start_callback,
411 .create_buf_file = blk_create_buf_file_callback,
412 .remove_buf_file = blk_remove_buf_file_callback,
415 static void blk_trace_setup_lba(struct blk_trace *bt,
416 struct block_device *bdev)
418 struct hd_struct *part = NULL;
421 part = bdev->bd_part;
424 bt->start_lba = part->start_sect;
425 bt->end_lba = part->start_sect + part->nr_sects;
433 * Setup everything required to start tracing
435 static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
436 struct block_device *bdev,
437 struct blk_user_trace_setup *buts)
439 struct blk_trace *bt = NULL;
440 struct dentry *dir = NULL;
443 if (!buts->buf_size || !buts->buf_nr)
446 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
447 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
450 * some device names have larger paths - convert the slashes
451 * to underscores for this to work as expected
453 strreplace(buts->name, '/', '_');
455 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
460 bt->sequence = alloc_percpu(unsigned long);
464 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
470 if (!blk_debugfs_root)
473 dir = debugfs_lookup(buts->name, blk_debugfs_root);
475 bt->dir = dir = debugfs_create_dir(buts->name, blk_debugfs_root);
480 atomic_set(&bt->dropped, 0);
481 INIT_LIST_HEAD(&bt->running_list);
484 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
486 if (!bt->dropped_file)
489 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
493 bt->rchan = relay_open("trace", dir, buts->buf_size,
494 buts->buf_nr, &blk_relay_callbacks, bt);
498 bt->act_mask = buts->act_mask;
500 bt->act_mask = (u16) -1;
502 blk_trace_setup_lba(bt, bdev);
504 /* overwrite with user settings */
506 bt->start_lba = buts->start_lba;
508 bt->end_lba = buts->end_lba;
511 bt->trace_state = Blktrace_setup;
514 if (cmpxchg(&q->blk_trace, NULL, bt))
517 if (atomic_inc_return(&blk_probes_ref) == 1)
518 blk_register_tracepoints();
529 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
530 struct block_device *bdev,
533 struct blk_user_trace_setup buts;
536 ret = copy_from_user(&buts, arg, sizeof(buts));
540 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
544 if (copy_to_user(arg, &buts, sizeof(buts))) {
550 EXPORT_SYMBOL_GPL(blk_trace_setup);
552 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
553 static int compat_blk_trace_setup(struct request_queue *q, char *name,
554 dev_t dev, struct block_device *bdev,
557 struct blk_user_trace_setup buts;
558 struct compat_blk_user_trace_setup cbuts;
561 if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
564 buts = (struct blk_user_trace_setup) {
565 .act_mask = cbuts.act_mask,
566 .buf_size = cbuts.buf_size,
567 .buf_nr = cbuts.buf_nr,
568 .start_lba = cbuts.start_lba,
569 .end_lba = cbuts.end_lba,
573 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
577 if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) {
586 int blk_trace_startstop(struct request_queue *q, int start)
589 struct blk_trace *bt = q->blk_trace;
595 * For starting a trace, we can transition from a setup or stopped
596 * trace. For stopping a trace, the state must be running
600 if (bt->trace_state == Blktrace_setup ||
601 bt->trace_state == Blktrace_stopped) {
604 bt->trace_state = Blktrace_running;
605 spin_lock_irq(&running_trace_lock);
606 list_add(&bt->running_list, &running_trace_list);
607 spin_unlock_irq(&running_trace_lock);
613 if (bt->trace_state == Blktrace_running) {
614 bt->trace_state = Blktrace_stopped;
615 spin_lock_irq(&running_trace_lock);
616 list_del_init(&bt->running_list);
617 spin_unlock_irq(&running_trace_lock);
618 relay_flush(bt->rchan);
625 EXPORT_SYMBOL_GPL(blk_trace_startstop);
628 * blk_trace_ioctl: - handle the ioctls associated with tracing
629 * @bdev: the block device
630 * @cmd: the ioctl cmd
631 * @arg: the argument data, if any
634 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
636 struct request_queue *q;
638 char b[BDEVNAME_SIZE];
640 q = bdev_get_queue(bdev);
644 mutex_lock(&bdev->bd_mutex);
649 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
651 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
652 case BLKTRACESETUP32:
654 ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
660 ret = blk_trace_startstop(q, start);
662 case BLKTRACETEARDOWN:
663 ret = blk_trace_remove(q);
670 mutex_unlock(&bdev->bd_mutex);
675 * blk_trace_shutdown: - stop and cleanup trace structures
676 * @q: the request queue associated with the device
679 void blk_trace_shutdown(struct request_queue *q)
682 blk_trace_startstop(q, 0);
692 * blk_add_trace_rq - Add a trace for a request oriented action
693 * @q: queue the io is for
694 * @rq: the source request
695 * @nr_bytes: number of completed bytes
699 * Records an action against a request. Will log the bio offset + size.
702 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
703 unsigned int nr_bytes, u32 what)
705 struct blk_trace *bt = q->blk_trace;
710 if (blk_rq_is_passthrough(rq))
711 what |= BLK_TC_ACT(BLK_TC_PC);
713 what |= BLK_TC_ACT(BLK_TC_FS);
715 __blk_add_trace(bt, blk_rq_trace_sector(rq), nr_bytes, req_op(rq),
716 rq->cmd_flags, what, rq->errors, 0, NULL);
719 static void blk_add_trace_rq_abort(void *ignore,
720 struct request_queue *q, struct request *rq)
722 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ABORT);
725 static void blk_add_trace_rq_insert(void *ignore,
726 struct request_queue *q, struct request *rq)
728 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_INSERT);
731 static void blk_add_trace_rq_issue(void *ignore,
732 struct request_queue *q, struct request *rq)
734 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ISSUE);
737 static void blk_add_trace_rq_requeue(void *ignore,
738 struct request_queue *q,
741 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_REQUEUE);
744 static void blk_add_trace_rq_complete(void *ignore,
745 struct request_queue *q,
747 unsigned int nr_bytes)
749 blk_add_trace_rq(q, rq, nr_bytes, BLK_TA_COMPLETE);
753 * blk_add_trace_bio - Add a trace for a bio oriented action
754 * @q: queue the io is for
755 * @bio: the source bio
757 * @error: error, if any
760 * Records an action against a bio. Will log the bio offset + size.
763 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
766 struct blk_trace *bt = q->blk_trace;
771 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
772 bio_op(bio), bio->bi_opf, what, error, 0, NULL);
775 static void blk_add_trace_bio_bounce(void *ignore,
776 struct request_queue *q, struct bio *bio)
778 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0);
781 static void blk_add_trace_bio_complete(void *ignore,
782 struct request_queue *q, struct bio *bio,
785 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, error);
788 static void blk_add_trace_bio_backmerge(void *ignore,
789 struct request_queue *q,
793 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0);
796 static void blk_add_trace_bio_frontmerge(void *ignore,
797 struct request_queue *q,
801 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0);
804 static void blk_add_trace_bio_queue(void *ignore,
805 struct request_queue *q, struct bio *bio)
807 blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0);
810 static void blk_add_trace_getrq(void *ignore,
811 struct request_queue *q,
812 struct bio *bio, int rw)
815 blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0);
817 struct blk_trace *bt = q->blk_trace;
820 __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_GETRQ, 0, 0,
826 static void blk_add_trace_sleeprq(void *ignore,
827 struct request_queue *q,
828 struct bio *bio, int rw)
831 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
833 struct blk_trace *bt = q->blk_trace;
836 __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_SLEEPRQ,
841 static void blk_add_trace_plug(void *ignore, struct request_queue *q)
843 struct blk_trace *bt = q->blk_trace;
846 __blk_add_trace(bt, 0, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
849 static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
850 unsigned int depth, bool explicit)
852 struct blk_trace *bt = q->blk_trace;
855 __be64 rpdu = cpu_to_be64(depth);
859 what = BLK_TA_UNPLUG_IO;
861 what = BLK_TA_UNPLUG_TIMER;
863 __blk_add_trace(bt, 0, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
867 static void blk_add_trace_split(void *ignore,
868 struct request_queue *q, struct bio *bio,
871 struct blk_trace *bt = q->blk_trace;
874 __be64 rpdu = cpu_to_be64(pdu);
876 __blk_add_trace(bt, bio->bi_iter.bi_sector,
877 bio->bi_iter.bi_size, bio_op(bio), bio->bi_opf,
878 BLK_TA_SPLIT, bio->bi_error, sizeof(rpdu),
884 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
885 * @ignore: trace callback data parameter (not used)
886 * @q: queue the io is for
887 * @bio: the source bio
888 * @dev: target device
889 * @from: source sector
892 * Device mapper or raid target sometimes need to split a bio because
893 * it spans a stripe (or similar). Add a trace for that action.
896 static void blk_add_trace_bio_remap(void *ignore,
897 struct request_queue *q, struct bio *bio,
898 dev_t dev, sector_t from)
900 struct blk_trace *bt = q->blk_trace;
901 struct blk_io_trace_remap r;
906 r.device_from = cpu_to_be32(dev);
907 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
908 r.sector_from = cpu_to_be64(from);
910 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
911 bio_op(bio), bio->bi_opf, BLK_TA_REMAP, bio->bi_error,
916 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
917 * @ignore: trace callback data parameter (not used)
918 * @q: queue the io is for
919 * @rq: the source request
920 * @dev: target device
921 * @from: source sector
924 * Device mapper remaps request to other devices.
925 * Add a trace for that action.
928 static void blk_add_trace_rq_remap(void *ignore,
929 struct request_queue *q,
930 struct request *rq, dev_t dev,
933 struct blk_trace *bt = q->blk_trace;
934 struct blk_io_trace_remap r;
939 r.device_from = cpu_to_be32(dev);
940 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
941 r.sector_from = cpu_to_be64(from);
943 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
944 rq_data_dir(rq), 0, BLK_TA_REMAP, !!rq->errors,
949 * blk_add_driver_data - Add binary message with driver-specific data
950 * @q: queue the io is for
952 * @data: driver-specific data
953 * @len: length of driver-specific data
956 * Some drivers might want to write driver-specific data per request.
959 void blk_add_driver_data(struct request_queue *q,
961 void *data, size_t len)
963 struct blk_trace *bt = q->blk_trace;
968 __blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0, 0,
969 BLK_TA_DRV_DATA, rq->errors, len, data);
971 EXPORT_SYMBOL_GPL(blk_add_driver_data);
973 static void blk_register_tracepoints(void)
977 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
979 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
981 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
983 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
985 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
987 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
989 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
991 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
993 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
995 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
997 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
999 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1001 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
1003 ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
1005 ret = register_trace_block_split(blk_add_trace_split, NULL);
1007 ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1009 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1013 static void blk_unregister_tracepoints(void)
1015 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1016 unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1017 unregister_trace_block_split(blk_add_trace_split, NULL);
1018 unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
1019 unregister_trace_block_plug(blk_add_trace_plug, NULL);
1020 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1021 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1022 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1023 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1024 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1025 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1026 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1027 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1028 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1029 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1030 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1031 unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1033 tracepoint_synchronize_unregister();
1037 * struct blk_io_tracer formatting routines
1040 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1043 int tc = t->action >> BLK_TC_SHIFT;
1045 if (t->action == BLK_TN_MESSAGE) {
1050 if (tc & BLK_TC_FLUSH)
1053 if (tc & BLK_TC_DISCARD)
1055 else if (tc & BLK_TC_WRITE)
1062 if (tc & BLK_TC_FUA)
1064 if (tc & BLK_TC_AHEAD)
1066 if (tc & BLK_TC_SYNC)
1068 if (tc & BLK_TC_META)
1075 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1077 return (const struct blk_io_trace *)ent;
1080 static inline const void *pdu_start(const struct trace_entry *ent)
1082 return te_blk_io_trace(ent) + 1;
1085 static inline u32 t_action(const struct trace_entry *ent)
1087 return te_blk_io_trace(ent)->action;
1090 static inline u32 t_bytes(const struct trace_entry *ent)
1092 return te_blk_io_trace(ent)->bytes;
1095 static inline u32 t_sec(const struct trace_entry *ent)
1097 return te_blk_io_trace(ent)->bytes >> 9;
1100 static inline unsigned long long t_sector(const struct trace_entry *ent)
1102 return te_blk_io_trace(ent)->sector;
1105 static inline __u16 t_error(const struct trace_entry *ent)
1107 return te_blk_io_trace(ent)->error;
1110 static __u64 get_pdu_int(const struct trace_entry *ent)
1112 const __u64 *val = pdu_start(ent);
1113 return be64_to_cpu(*val);
1116 static void get_pdu_remap(const struct trace_entry *ent,
1117 struct blk_io_trace_remap *r)
1119 const struct blk_io_trace_remap *__r = pdu_start(ent);
1120 __u64 sector_from = __r->sector_from;
1122 r->device_from = be32_to_cpu(__r->device_from);
1123 r->device_to = be32_to_cpu(__r->device_to);
1124 r->sector_from = be64_to_cpu(sector_from);
1127 typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1129 static void blk_log_action_classic(struct trace_iterator *iter, const char *act)
1131 char rwbs[RWBS_LEN];
1132 unsigned long long ts = iter->ts;
1133 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1134 unsigned secs = (unsigned long)ts;
1135 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1139 trace_seq_printf(&iter->seq,
1140 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1141 MAJOR(t->device), MINOR(t->device), iter->cpu,
1142 secs, nsec_rem, iter->ent->pid, act, rwbs);
1145 static void blk_log_action(struct trace_iterator *iter, const char *act)
1147 char rwbs[RWBS_LEN];
1148 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1151 trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1152 MAJOR(t->device), MINOR(t->device), act, rwbs);
1155 static void blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1157 const unsigned char *pdu_buf;
1161 pdu_buf = pdu_start(ent);
1162 pdu_len = te_blk_io_trace(ent)->pdu_len;
1167 /* find the last zero that needs to be printed */
1168 for (end = pdu_len - 1; end >= 0; end--)
1173 trace_seq_putc(s, '(');
1175 for (i = 0; i < pdu_len; i++) {
1177 trace_seq_printf(s, "%s%02x",
1178 i == 0 ? "" : " ", pdu_buf[i]);
1181 * stop when the rest is just zeroes and indicate so
1182 * with a ".." appended
1184 if (i == end && end != pdu_len - 1) {
1185 trace_seq_puts(s, " ..) ");
1190 trace_seq_puts(s, ") ");
1193 static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1195 char cmd[TASK_COMM_LEN];
1197 trace_find_cmdline(ent->pid, cmd);
1199 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1200 trace_seq_printf(s, "%u ", t_bytes(ent));
1201 blk_log_dump_pdu(s, ent);
1202 trace_seq_printf(s, "[%s]\n", cmd);
1205 trace_seq_printf(s, "%llu + %u [%s]\n",
1206 t_sector(ent), t_sec(ent), cmd);
1208 trace_seq_printf(s, "[%s]\n", cmd);
1212 static void blk_log_with_error(struct trace_seq *s,
1213 const struct trace_entry *ent)
1215 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1216 blk_log_dump_pdu(s, ent);
1217 trace_seq_printf(s, "[%d]\n", t_error(ent));
1220 trace_seq_printf(s, "%llu + %u [%d]\n",
1222 t_sec(ent), t_error(ent));
1224 trace_seq_printf(s, "%llu [%d]\n",
1225 t_sector(ent), t_error(ent));
1229 static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1231 struct blk_io_trace_remap r = { .device_from = 0, };
1233 get_pdu_remap(ent, &r);
1234 trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1235 t_sector(ent), t_sec(ent),
1236 MAJOR(r.device_from), MINOR(r.device_from),
1237 (unsigned long long)r.sector_from);
1240 static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1242 char cmd[TASK_COMM_LEN];
1244 trace_find_cmdline(ent->pid, cmd);
1246 trace_seq_printf(s, "[%s]\n", cmd);
1249 static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1251 char cmd[TASK_COMM_LEN];
1253 trace_find_cmdline(ent->pid, cmd);
1255 trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1258 static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1260 char cmd[TASK_COMM_LEN];
1262 trace_find_cmdline(ent->pid, cmd);
1264 trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1265 get_pdu_int(ent), cmd);
1268 static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1270 const struct blk_io_trace *t = te_blk_io_trace(ent);
1272 trace_seq_putmem(s, t + 1, t->pdu_len);
1273 trace_seq_putc(s, '\n');
1277 * struct tracer operations
1280 static void blk_tracer_print_header(struct seq_file *m)
1282 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1284 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1288 static void blk_tracer_start(struct trace_array *tr)
1290 blk_tracer_enabled = true;
1293 static int blk_tracer_init(struct trace_array *tr)
1296 blk_tracer_start(tr);
1300 static void blk_tracer_stop(struct trace_array *tr)
1302 blk_tracer_enabled = false;
1305 static void blk_tracer_reset(struct trace_array *tr)
1307 blk_tracer_stop(tr);
1310 static const struct {
1312 void (*print)(struct trace_seq *s, const struct trace_entry *ent);
1314 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1315 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1316 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1317 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1318 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1319 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1320 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1321 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1322 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1323 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1324 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1325 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1326 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1327 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1328 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1331 static enum print_line_t print_one_line(struct trace_iterator *iter,
1334 struct trace_array *tr = iter->tr;
1335 struct trace_seq *s = &iter->seq;
1336 const struct blk_io_trace *t;
1339 blk_log_action_t *log_action;
1341 t = te_blk_io_trace(iter->ent);
1342 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1343 long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
1344 log_action = classic ? &blk_log_action_classic : &blk_log_action;
1346 if (t->action == BLK_TN_MESSAGE) {
1347 log_action(iter, long_act ? "message" : "m");
1348 blk_log_msg(s, iter->ent);
1349 return trace_handle_return(s);
1352 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1353 trace_seq_printf(s, "Unknown action %x\n", what);
1355 log_action(iter, what2act[what].act[long_act]);
1356 what2act[what].print(s, iter->ent);
1359 return trace_handle_return(s);
1362 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1363 int flags, struct trace_event *event)
1365 return print_one_line(iter, false);
1368 static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1370 struct trace_seq *s = &iter->seq;
1371 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1372 const int offset = offsetof(struct blk_io_trace, sector);
1373 struct blk_io_trace old = {
1374 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1378 trace_seq_putmem(s, &old, offset);
1379 trace_seq_putmem(s, &t->sector,
1380 sizeof(old) - offset + t->pdu_len);
1383 static enum print_line_t
1384 blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1385 struct trace_event *event)
1387 blk_trace_synthesize_old_trace(iter);
1389 return trace_handle_return(&iter->seq);
1392 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1394 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1395 return TRACE_TYPE_UNHANDLED;
1397 return print_one_line(iter, true);
1401 blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1403 /* don't output context-info for blk_classic output */
1404 if (bit == TRACE_BLK_OPT_CLASSIC) {
1406 tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1408 tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
1413 static struct tracer blk_tracer __read_mostly = {
1415 .init = blk_tracer_init,
1416 .reset = blk_tracer_reset,
1417 .start = blk_tracer_start,
1418 .stop = blk_tracer_stop,
1419 .print_header = blk_tracer_print_header,
1420 .print_line = blk_tracer_print_line,
1421 .flags = &blk_tracer_flags,
1422 .set_flag = blk_tracer_set_flag,
1425 static struct trace_event_functions trace_blk_event_funcs = {
1426 .trace = blk_trace_event_print,
1427 .binary = blk_trace_event_print_binary,
1430 static struct trace_event trace_blk_event = {
1432 .funcs = &trace_blk_event_funcs,
1435 static int __init init_blk_tracer(void)
1437 if (!register_trace_event(&trace_blk_event)) {
1438 pr_warn("Warning: could not register block events\n");
1442 if (register_tracer(&blk_tracer) != 0) {
1443 pr_warn("Warning: could not register the block tracer\n");
1444 unregister_trace_event(&trace_blk_event);
1451 device_initcall(init_blk_tracer);
1453 static int blk_trace_remove_queue(struct request_queue *q)
1455 struct blk_trace *bt;
1457 bt = xchg(&q->blk_trace, NULL);
1461 if (atomic_dec_and_test(&blk_probes_ref))
1462 blk_unregister_tracepoints();
1469 * Setup everything required to start tracing
1471 static int blk_trace_setup_queue(struct request_queue *q,
1472 struct block_device *bdev)
1474 struct blk_trace *bt = NULL;
1477 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1481 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1485 bt->dev = bdev->bd_dev;
1486 bt->act_mask = (u16)-1;
1488 blk_trace_setup_lba(bt, bdev);
1491 if (cmpxchg(&q->blk_trace, NULL, bt))
1494 if (atomic_inc_return(&blk_probes_ref) == 1)
1495 blk_register_tracepoints();
1504 * sysfs interface to enable and configure tracing
1507 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1508 struct device_attribute *attr,
1510 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1511 struct device_attribute *attr,
1512 const char *buf, size_t count);
1513 #define BLK_TRACE_DEVICE_ATTR(_name) \
1514 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1515 sysfs_blk_trace_attr_show, \
1516 sysfs_blk_trace_attr_store)
1518 static BLK_TRACE_DEVICE_ATTR(enable);
1519 static BLK_TRACE_DEVICE_ATTR(act_mask);
1520 static BLK_TRACE_DEVICE_ATTR(pid);
1521 static BLK_TRACE_DEVICE_ATTR(start_lba);
1522 static BLK_TRACE_DEVICE_ATTR(end_lba);
1524 static struct attribute *blk_trace_attrs[] = {
1525 &dev_attr_enable.attr,
1526 &dev_attr_act_mask.attr,
1528 &dev_attr_start_lba.attr,
1529 &dev_attr_end_lba.attr,
1533 struct attribute_group blk_trace_attr_group = {
1535 .attrs = blk_trace_attrs,
1538 static const struct {
1542 { BLK_TC_READ, "read" },
1543 { BLK_TC_WRITE, "write" },
1544 { BLK_TC_FLUSH, "flush" },
1545 { BLK_TC_SYNC, "sync" },
1546 { BLK_TC_QUEUE, "queue" },
1547 { BLK_TC_REQUEUE, "requeue" },
1548 { BLK_TC_ISSUE, "issue" },
1549 { BLK_TC_COMPLETE, "complete" },
1550 { BLK_TC_FS, "fs" },
1551 { BLK_TC_PC, "pc" },
1552 { BLK_TC_NOTIFY, "notify" },
1553 { BLK_TC_AHEAD, "ahead" },
1554 { BLK_TC_META, "meta" },
1555 { BLK_TC_DISCARD, "discard" },
1556 { BLK_TC_DRV_DATA, "drv_data" },
1557 { BLK_TC_FUA, "fua" },
1560 static int blk_trace_str2mask(const char *str)
1564 char *buf, *s, *token;
1566 buf = kstrdup(str, GFP_KERNEL);
1572 token = strsep(&s, ",");
1579 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1580 if (strcasecmp(token, mask_maps[i].str) == 0) {
1581 mask |= mask_maps[i].mask;
1585 if (i == ARRAY_SIZE(mask_maps)) {
1595 static ssize_t blk_trace_mask2str(char *buf, int mask)
1600 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1601 if (mask & mask_maps[i].mask) {
1602 p += sprintf(p, "%s%s",
1603 (p == buf) ? "" : ",", mask_maps[i].str);
1611 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1613 if (bdev->bd_disk == NULL)
1616 return bdev_get_queue(bdev);
1619 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1620 struct device_attribute *attr,
1623 struct hd_struct *p = dev_to_part(dev);
1624 struct request_queue *q;
1625 struct block_device *bdev;
1626 ssize_t ret = -ENXIO;
1628 bdev = bdget(part_devt(p));
1632 q = blk_trace_get_queue(bdev);
1636 mutex_lock(&bdev->bd_mutex);
1638 if (attr == &dev_attr_enable) {
1639 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1640 goto out_unlock_bdev;
1643 if (q->blk_trace == NULL)
1644 ret = sprintf(buf, "disabled\n");
1645 else if (attr == &dev_attr_act_mask)
1646 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
1647 else if (attr == &dev_attr_pid)
1648 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1649 else if (attr == &dev_attr_start_lba)
1650 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1651 else if (attr == &dev_attr_end_lba)
1652 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1655 mutex_unlock(&bdev->bd_mutex);
1662 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1663 struct device_attribute *attr,
1664 const char *buf, size_t count)
1666 struct block_device *bdev;
1667 struct request_queue *q;
1668 struct hd_struct *p;
1670 ssize_t ret = -EINVAL;
1675 if (attr == &dev_attr_act_mask) {
1676 if (sscanf(buf, "%llx", &value) != 1) {
1677 /* Assume it is a list of trace category names */
1678 ret = blk_trace_str2mask(buf);
1683 } else if (sscanf(buf, "%llu", &value) != 1)
1688 p = dev_to_part(dev);
1689 bdev = bdget(part_devt(p));
1693 q = blk_trace_get_queue(bdev);
1697 mutex_lock(&bdev->bd_mutex);
1699 if (attr == &dev_attr_enable) {
1701 ret = blk_trace_setup_queue(q, bdev);
1703 ret = blk_trace_remove_queue(q);
1704 goto out_unlock_bdev;
1708 if (q->blk_trace == NULL)
1709 ret = blk_trace_setup_queue(q, bdev);
1712 if (attr == &dev_attr_act_mask)
1713 q->blk_trace->act_mask = value;
1714 else if (attr == &dev_attr_pid)
1715 q->blk_trace->pid = value;
1716 else if (attr == &dev_attr_start_lba)
1717 q->blk_trace->start_lba = value;
1718 else if (attr == &dev_attr_end_lba)
1719 q->blk_trace->end_lba = value;
1723 mutex_unlock(&bdev->bd_mutex);
1727 return ret ? ret : count;
1730 int blk_trace_init_sysfs(struct device *dev)
1732 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1735 void blk_trace_remove_sysfs(struct device *dev)
1737 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1740 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1742 #ifdef CONFIG_EVENT_TRACING
1744 void blk_fill_rwbs(char *rwbs, unsigned int op, int bytes)
1748 if (op & REQ_PREFLUSH)
1751 switch (op & REQ_OP_MASK) {
1753 case REQ_OP_WRITE_SAME:
1756 case REQ_OP_DISCARD:
1759 case REQ_OP_SECURE_ERASE:
1775 if (op & REQ_RAHEAD)
1784 EXPORT_SYMBOL_GPL(blk_fill_rwbs);
1786 #endif /* CONFIG_EVENT_TRACING */