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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/kernel.h>
22 #include <linux/blkdev.h>
23 #include <linux/blktrace_api.h>
24 #include <linux/percpu.h>
25 #include <linux/init.h>
26 #include <linux/mutex.h>
27 #include <linux/slab.h>
28 #include <linux/debugfs.h>
29 #include <linux/export.h>
30 #include <linux/time.h>
31 #include <linux/uaccess.h>
32 #include <linux/list.h>
34 #include <trace/events/block.h>
36 #include "trace_output.h"
38 #ifdef CONFIG_BLK_DEV_IO_TRACE
40 static unsigned int blktrace_seq __read_mostly = 1;
42 static struct trace_array *blk_tr;
43 static bool blk_tracer_enabled __read_mostly;
45 static LIST_HEAD(running_trace_list);
46 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock);
48 /* Select an alternative, minimalistic output than the original one */
49 #define TRACE_BLK_OPT_CLASSIC 0x1
51 static struct tracer_opt blk_tracer_opts[] = {
52 /* Default disable the minimalistic output */
53 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
57 static struct tracer_flags blk_tracer_flags = {
59 .opts = blk_tracer_opts,
62 /* Global reference count of probes */
63 static DEFINE_MUTEX(blk_probe_mutex);
64 static int blk_probes_ref;
66 static void blk_register_tracepoints(void);
67 static void blk_unregister_tracepoints(void);
70 * Send out a notify message.
72 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
73 const void *data, size_t len)
75 struct blk_io_trace *t;
76 struct ring_buffer_event *event = NULL;
77 struct ring_buffer *buffer = NULL;
79 int cpu = smp_processor_id();
80 bool blk_tracer = blk_tracer_enabled;
83 buffer = blk_tr->trace_buffer.buffer;
85 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
90 t = ring_buffer_event_data(event);
97 t = relay_reserve(bt->rchan, sizeof(*t) + len);
99 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
100 t->time = ktime_to_ns(ktime_get());
107 memcpy((void *) t + sizeof(*t), data, len);
110 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
115 * Send out a notify for this process, if we haven't done so since a trace
118 static void trace_note_tsk(struct task_struct *tsk)
121 struct blk_trace *bt;
123 tsk->btrace_seq = blktrace_seq;
124 spin_lock_irqsave(&running_trace_lock, flags);
125 list_for_each_entry(bt, &running_trace_list, running_list) {
126 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm,
129 spin_unlock_irqrestore(&running_trace_lock, flags);
132 static void trace_note_time(struct blk_trace *bt)
138 getnstimeofday(&now);
139 words[0] = now.tv_sec;
140 words[1] = now.tv_nsec;
142 local_irq_save(flags);
143 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
144 local_irq_restore(flags);
147 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
154 if (unlikely(bt->trace_state != Blktrace_running &&
155 !blk_tracer_enabled))
159 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
160 * message to the trace.
162 if (!(bt->act_mask & BLK_TC_NOTIFY))
165 local_irq_save(flags);
166 buf = this_cpu_ptr(bt->msg_data);
168 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
171 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
172 local_irq_restore(flags);
174 EXPORT_SYMBOL_GPL(__trace_note_message);
176 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
179 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
181 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
183 if (bt->pid && pid != bt->pid)
190 * Data direction bit lookup
192 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
193 BLK_TC_ACT(BLK_TC_WRITE) };
195 #define BLK_TC_RAHEAD BLK_TC_AHEAD
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 rw, u32 what, int error, int pdu_len, void *pdu_data)
208 struct task_struct *tsk = current;
209 struct ring_buffer_event *event = NULL;
210 struct ring_buffer *buffer = NULL;
211 struct blk_io_trace *t;
212 unsigned long flags = 0;
213 unsigned long *sequence;
216 bool blk_tracer = blk_tracer_enabled;
218 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
221 what |= ddir_act[rw & WRITE];
222 what |= MASK_TC_BIT(rw, SYNC);
223 what |= MASK_TC_BIT(rw, RAHEAD);
224 what |= MASK_TC_BIT(rw, META);
225 what |= MASK_TC_BIT(rw, DISCARD);
226 what |= MASK_TC_BIT(rw, FLUSH);
227 what |= MASK_TC_BIT(rw, FUA);
230 if (act_log_check(bt, what, sector, pid))
232 cpu = raw_smp_processor_id();
235 tracing_record_cmdline(current);
237 buffer = blk_tr->trace_buffer.buffer;
238 pc = preempt_count();
239 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
240 sizeof(*t) + pdu_len,
244 t = ring_buffer_event_data(event);
248 if (unlikely(tsk->btrace_seq != blktrace_seq))
252 * A word about the locking here - we disable interrupts to reserve
253 * some space in the relay per-cpu buffer, to prevent an irq
254 * from coming in and stepping on our toes.
256 local_irq_save(flags);
257 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
259 sequence = per_cpu_ptr(bt->sequence, cpu);
261 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
262 t->sequence = ++(*sequence);
263 t->time = ktime_to_ns(ktime_get());
266 * These two are not needed in ftrace as they are in the
267 * generic trace_entry, filled by tracing_generic_entry_update,
268 * but for the trace_event->bin() synthesizer benefit we do it
279 t->pdu_len = pdu_len;
282 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
285 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
290 local_irq_restore(flags);
293 static struct dentry *blk_tree_root;
294 static DEFINE_MUTEX(blk_tree_mutex);
296 static void blk_trace_free(struct blk_trace *bt)
298 debugfs_remove(bt->msg_file);
299 debugfs_remove(bt->dropped_file);
300 relay_close(bt->rchan);
301 debugfs_remove(bt->dir);
302 free_percpu(bt->sequence);
303 free_percpu(bt->msg_data);
307 static void get_probe_ref(void)
309 mutex_lock(&blk_probe_mutex);
310 if (++blk_probes_ref == 1)
311 blk_register_tracepoints();
312 mutex_unlock(&blk_probe_mutex);
315 static void put_probe_ref(void)
317 mutex_lock(&blk_probe_mutex);
318 if (!--blk_probes_ref)
319 blk_unregister_tracepoints();
320 mutex_unlock(&blk_probe_mutex);
323 static void blk_trace_cleanup(struct blk_trace *bt)
330 static int __blk_trace_remove(struct request_queue *q)
332 struct blk_trace *bt;
334 bt = xchg(&q->blk_trace, NULL);
338 if (bt->trace_state != Blktrace_running)
339 blk_trace_cleanup(bt);
344 int blk_trace_remove(struct request_queue *q)
348 mutex_lock(&q->blk_trace_mutex);
349 ret = __blk_trace_remove(q);
350 mutex_unlock(&q->blk_trace_mutex);
354 EXPORT_SYMBOL_GPL(blk_trace_remove);
356 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
357 size_t count, loff_t *ppos)
359 struct blk_trace *bt = filp->private_data;
362 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
364 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
367 static const struct file_operations blk_dropped_fops = {
368 .owner = THIS_MODULE,
370 .read = blk_dropped_read,
371 .llseek = default_llseek,
374 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
375 size_t count, loff_t *ppos)
378 struct blk_trace *bt;
380 if (count >= BLK_TN_MAX_MSG)
383 msg = kmalloc(count + 1, GFP_KERNEL);
387 if (copy_from_user(msg, buffer, count)) {
393 bt = filp->private_data;
394 __trace_note_message(bt, "%s", msg);
400 static const struct file_operations blk_msg_fops = {
401 .owner = THIS_MODULE,
403 .write = blk_msg_write,
404 .llseek = noop_llseek,
408 * Keep track of how many times we encountered a full subbuffer, to aid
409 * the user space app in telling how many lost events there were.
411 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
412 void *prev_subbuf, size_t prev_padding)
414 struct blk_trace *bt;
416 if (!relay_buf_full(buf))
419 bt = buf->chan->private_data;
420 atomic_inc(&bt->dropped);
424 static int blk_remove_buf_file_callback(struct dentry *dentry)
426 debugfs_remove(dentry);
431 static struct dentry *blk_create_buf_file_callback(const char *filename,
432 struct dentry *parent,
434 struct rchan_buf *buf,
437 return debugfs_create_file(filename, mode, parent, buf,
438 &relay_file_operations);
441 static struct rchan_callbacks blk_relay_callbacks = {
442 .subbuf_start = blk_subbuf_start_callback,
443 .create_buf_file = blk_create_buf_file_callback,
444 .remove_buf_file = blk_remove_buf_file_callback,
447 static void blk_trace_setup_lba(struct blk_trace *bt,
448 struct block_device *bdev)
450 struct hd_struct *part = NULL;
453 part = bdev->bd_part;
456 bt->start_lba = part->start_sect;
457 bt->end_lba = part->start_sect + part->nr_sects;
465 * Setup everything required to start tracing
467 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
468 struct block_device *bdev,
469 struct blk_user_trace_setup *buts)
471 struct blk_trace *bt = NULL;
472 struct dentry *dir = NULL;
475 if (!buts->buf_size || !buts->buf_nr)
478 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
479 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
482 * some device names have larger paths - convert the slashes
483 * to underscores for this to work as expected
485 strreplace(buts->name, '/', '_');
488 * bdev can be NULL, as with scsi-generic, this is a helpful as
492 pr_warn("Concurrent blktraces are not allowed on %s\n",
497 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
502 bt->sequence = alloc_percpu(unsigned long);
506 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
512 mutex_lock(&blk_tree_mutex);
513 if (!blk_tree_root) {
514 blk_tree_root = debugfs_create_dir("block", NULL);
515 if (!blk_tree_root) {
516 mutex_unlock(&blk_tree_mutex);
520 mutex_unlock(&blk_tree_mutex);
522 dir = debugfs_create_dir(buts->name, blk_tree_root);
529 atomic_set(&bt->dropped, 0);
530 INIT_LIST_HEAD(&bt->running_list);
533 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
535 if (!bt->dropped_file)
538 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
542 bt->rchan = relay_open("trace", dir, buts->buf_size,
543 buts->buf_nr, &blk_relay_callbacks, bt);
547 bt->act_mask = buts->act_mask;
549 bt->act_mask = (u16) -1;
551 blk_trace_setup_lba(bt, bdev);
553 /* overwrite with user settings */
555 bt->start_lba = buts->start_lba;
557 bt->end_lba = buts->end_lba;
560 bt->trace_state = Blktrace_setup;
563 if (cmpxchg(&q->blk_trace, NULL, bt))
574 static int __blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
575 struct block_device *bdev, char __user *arg)
577 struct blk_user_trace_setup buts;
580 ret = copy_from_user(&buts, arg, sizeof(buts));
584 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
588 if (copy_to_user(arg, &buts, sizeof(buts))) {
589 __blk_trace_remove(q);
595 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
596 struct block_device *bdev,
601 mutex_lock(&q->blk_trace_mutex);
602 ret = __blk_trace_setup(q, name, dev, bdev, arg);
603 mutex_unlock(&q->blk_trace_mutex);
607 EXPORT_SYMBOL_GPL(blk_trace_setup);
609 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
610 static int compat_blk_trace_setup(struct request_queue *q, char *name,
611 dev_t dev, struct block_device *bdev,
614 struct blk_user_trace_setup buts;
615 struct compat_blk_user_trace_setup cbuts;
618 if (copy_from_user(&cbuts, arg, sizeof(cbuts)))
621 buts = (struct blk_user_trace_setup) {
622 .act_mask = cbuts.act_mask,
623 .buf_size = cbuts.buf_size,
624 .buf_nr = cbuts.buf_nr,
625 .start_lba = cbuts.start_lba,
626 .end_lba = cbuts.end_lba,
630 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
634 if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) {
635 __blk_trace_remove(q);
643 static int __blk_trace_startstop(struct request_queue *q, int start)
646 struct blk_trace *bt;
648 bt = rcu_dereference_protected(q->blk_trace,
649 lockdep_is_held(&q->blk_trace_mutex));
654 * For starting a trace, we can transition from a setup or stopped
655 * trace. For stopping a trace, the state must be running
659 if (bt->trace_state == Blktrace_setup ||
660 bt->trace_state == Blktrace_stopped) {
663 bt->trace_state = Blktrace_running;
664 spin_lock_irq(&running_trace_lock);
665 list_add(&bt->running_list, &running_trace_list);
666 spin_unlock_irq(&running_trace_lock);
672 if (bt->trace_state == Blktrace_running) {
673 bt->trace_state = Blktrace_stopped;
674 spin_lock_irq(&running_trace_lock);
675 list_del_init(&bt->running_list);
676 spin_unlock_irq(&running_trace_lock);
677 relay_flush(bt->rchan);
685 int blk_trace_startstop(struct request_queue *q, int start)
689 mutex_lock(&q->blk_trace_mutex);
690 ret = __blk_trace_startstop(q, start);
691 mutex_unlock(&q->blk_trace_mutex);
695 EXPORT_SYMBOL_GPL(blk_trace_startstop);
698 * When reading or writing the blktrace sysfs files, the references to the
699 * opened sysfs or device files should prevent the underlying block device
700 * from being removed. So no further delete protection is really needed.
704 * blk_trace_ioctl: - handle the ioctls associated with tracing
705 * @bdev: the block device
706 * @cmd: the ioctl cmd
707 * @arg: the argument data, if any
710 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
712 struct request_queue *q;
714 char b[BDEVNAME_SIZE];
716 q = bdev_get_queue(bdev);
720 mutex_lock(&q->blk_trace_mutex);
725 ret = __blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
727 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
728 case BLKTRACESETUP32:
730 ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
736 ret = __blk_trace_startstop(q, start);
738 case BLKTRACETEARDOWN:
739 ret = __blk_trace_remove(q);
746 mutex_unlock(&q->blk_trace_mutex);
751 * blk_trace_shutdown: - stop and cleanup trace structures
752 * @q: the request queue associated with the device
755 void blk_trace_shutdown(struct request_queue *q)
757 mutex_lock(&q->blk_trace_mutex);
758 if (rcu_dereference_protected(q->blk_trace,
759 lockdep_is_held(&q->blk_trace_mutex))) {
760 __blk_trace_startstop(q, 0);
761 __blk_trace_remove(q);
764 mutex_unlock(&q->blk_trace_mutex);
772 * blk_add_trace_rq - Add a trace for a request oriented action
773 * @q: queue the io is for
774 * @rq: the source request
775 * @nr_bytes: number of completed bytes
779 * Records an action against a request. Will log the bio offset + size.
782 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
783 unsigned int nr_bytes, u32 what)
785 struct blk_trace *bt;
788 bt = rcu_dereference(q->blk_trace);
794 if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
795 what |= BLK_TC_ACT(BLK_TC_PC);
796 __blk_add_trace(bt, 0, nr_bytes, rq->cmd_flags,
797 what, rq->errors, rq->cmd_len, rq->cmd);
799 what |= BLK_TC_ACT(BLK_TC_FS);
800 __blk_add_trace(bt, blk_rq_pos(rq), nr_bytes,
801 rq->cmd_flags, what, rq->errors, 0, NULL);
806 static void blk_add_trace_rq_abort(void *ignore,
807 struct request_queue *q, struct request *rq)
809 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ABORT);
812 static void blk_add_trace_rq_insert(void *ignore,
813 struct request_queue *q, struct request *rq)
815 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_INSERT);
818 static void blk_add_trace_rq_issue(void *ignore,
819 struct request_queue *q, struct request *rq)
821 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_ISSUE);
824 static void blk_add_trace_rq_requeue(void *ignore,
825 struct request_queue *q,
828 blk_add_trace_rq(q, rq, blk_rq_bytes(rq), BLK_TA_REQUEUE);
831 static void blk_add_trace_rq_complete(void *ignore,
832 struct request_queue *q,
834 unsigned int nr_bytes)
836 blk_add_trace_rq(q, rq, nr_bytes, BLK_TA_COMPLETE);
840 * blk_add_trace_bio - Add a trace for a bio oriented action
841 * @q: queue the io is for
842 * @bio: the source bio
844 * @error: error, if any
847 * Records an action against a bio. Will log the bio offset + size.
850 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
853 struct blk_trace *bt;
856 bt = rcu_dereference(q->blk_trace);
862 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
863 bio->bi_rw, what, error, 0, NULL);
867 static void blk_add_trace_bio_bounce(void *ignore,
868 struct request_queue *q, struct bio *bio)
870 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0);
873 static void blk_add_trace_bio_complete(void *ignore,
874 struct request_queue *q, struct bio *bio,
877 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, error);
880 static void blk_add_trace_bio_backmerge(void *ignore,
881 struct request_queue *q,
885 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0);
888 static void blk_add_trace_bio_frontmerge(void *ignore,
889 struct request_queue *q,
893 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0);
896 static void blk_add_trace_bio_queue(void *ignore,
897 struct request_queue *q, struct bio *bio)
899 blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0);
902 static void blk_add_trace_getrq(void *ignore,
903 struct request_queue *q,
904 struct bio *bio, int rw)
907 blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0);
909 struct blk_trace *bt;
912 bt = rcu_dereference(q->blk_trace);
914 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
920 static void blk_add_trace_sleeprq(void *ignore,
921 struct request_queue *q,
922 struct bio *bio, int rw)
925 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
927 struct blk_trace *bt;
930 bt = rcu_dereference(q->blk_trace);
932 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
938 static void blk_add_trace_plug(void *ignore, struct request_queue *q)
940 struct blk_trace *bt;
943 bt = rcu_dereference(q->blk_trace);
945 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
949 static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
950 unsigned int depth, bool explicit)
952 struct blk_trace *bt;
955 bt = rcu_dereference(q->blk_trace);
957 __be64 rpdu = cpu_to_be64(depth);
961 what = BLK_TA_UNPLUG_IO;
963 what = BLK_TA_UNPLUG_TIMER;
965 __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
970 static void blk_add_trace_split(void *ignore,
971 struct request_queue *q, struct bio *bio,
974 struct blk_trace *bt;
977 bt = rcu_dereference(q->blk_trace);
979 __be64 rpdu = cpu_to_be64(pdu);
981 __blk_add_trace(bt, bio->bi_iter.bi_sector,
982 bio->bi_iter.bi_size, bio->bi_rw, BLK_TA_SPLIT,
983 bio->bi_error, sizeof(rpdu), &rpdu);
989 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
990 * @ignore: trace callback data parameter (not used)
991 * @q: queue the io is for
992 * @bio: the source bio
993 * @dev: target device
994 * @from: source sector
997 * Device mapper or raid target sometimes need to split a bio because
998 * it spans a stripe (or similar). Add a trace for that action.
1001 static void blk_add_trace_bio_remap(void *ignore,
1002 struct request_queue *q, struct bio *bio,
1003 dev_t dev, sector_t from)
1005 struct blk_trace *bt;
1006 struct blk_io_trace_remap r;
1009 bt = rcu_dereference(q->blk_trace);
1015 r.device_from = cpu_to_be32(dev);
1016 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
1017 r.sector_from = cpu_to_be64(from);
1019 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
1020 bio->bi_rw, BLK_TA_REMAP, bio->bi_error,
1026 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
1027 * @ignore: trace callback data parameter (not used)
1028 * @q: queue the io is for
1029 * @rq: the source request
1030 * @dev: target device
1031 * @from: source sector
1034 * Device mapper remaps request to other devices.
1035 * Add a trace for that action.
1038 static void blk_add_trace_rq_remap(void *ignore,
1039 struct request_queue *q,
1040 struct request *rq, dev_t dev,
1043 struct blk_trace *bt;
1044 struct blk_io_trace_remap r;
1047 bt = rcu_dereference(q->blk_trace);
1053 r.device_from = cpu_to_be32(dev);
1054 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
1055 r.sector_from = cpu_to_be64(from);
1057 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
1058 rq_data_dir(rq), BLK_TA_REMAP, !!rq->errors,
1064 * blk_add_driver_data - Add binary message with driver-specific data
1065 * @q: queue the io is for
1067 * @data: driver-specific data
1068 * @len: length of driver-specific data
1071 * Some drivers might want to write driver-specific data per request.
1074 void blk_add_driver_data(struct request_queue *q,
1076 void *data, size_t len)
1078 struct blk_trace *bt;
1081 bt = rcu_dereference(q->blk_trace);
1087 if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
1088 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
1089 BLK_TA_DRV_DATA, rq->errors, len, data);
1091 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
1092 BLK_TA_DRV_DATA, rq->errors, len, data);
1095 EXPORT_SYMBOL_GPL(blk_add_driver_data);
1097 static void blk_register_tracepoints(void)
1101 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1103 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1105 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1107 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1109 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1111 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1113 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1115 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1117 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1119 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1121 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
1123 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1125 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
1127 ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
1129 ret = register_trace_block_split(blk_add_trace_split, NULL);
1131 ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1133 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1137 static void blk_unregister_tracepoints(void)
1139 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1140 unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1141 unregister_trace_block_split(blk_add_trace_split, NULL);
1142 unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
1143 unregister_trace_block_plug(blk_add_trace_plug, NULL);
1144 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1145 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1146 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1147 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1148 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1149 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1150 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1151 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1152 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1153 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1154 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1155 unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1157 tracepoint_synchronize_unregister();
1161 * struct blk_io_tracer formatting routines
1164 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1167 int tc = t->action >> BLK_TC_SHIFT;
1169 if (t->action == BLK_TN_MESSAGE) {
1174 if (tc & BLK_TC_FLUSH)
1177 if (tc & BLK_TC_DISCARD)
1179 else if (tc & BLK_TC_WRITE)
1186 if (tc & BLK_TC_FUA)
1188 if (tc & BLK_TC_AHEAD)
1190 if (tc & BLK_TC_SYNC)
1192 if (tc & BLK_TC_META)
1199 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1201 return (const struct blk_io_trace *)ent;
1204 static inline const void *pdu_start(const struct trace_entry *ent)
1206 return te_blk_io_trace(ent) + 1;
1209 static inline u32 t_action(const struct trace_entry *ent)
1211 return te_blk_io_trace(ent)->action;
1214 static inline u32 t_bytes(const struct trace_entry *ent)
1216 return te_blk_io_trace(ent)->bytes;
1219 static inline u32 t_sec(const struct trace_entry *ent)
1221 return te_blk_io_trace(ent)->bytes >> 9;
1224 static inline unsigned long long t_sector(const struct trace_entry *ent)
1226 return te_blk_io_trace(ent)->sector;
1229 static inline __u16 t_error(const struct trace_entry *ent)
1231 return te_blk_io_trace(ent)->error;
1234 static __u64 get_pdu_int(const struct trace_entry *ent)
1236 const __u64 *val = pdu_start(ent);
1237 return be64_to_cpu(*val);
1240 static void get_pdu_remap(const struct trace_entry *ent,
1241 struct blk_io_trace_remap *r)
1243 const struct blk_io_trace_remap *__r = pdu_start(ent);
1244 __u64 sector_from = __r->sector_from;
1246 r->device_from = be32_to_cpu(__r->device_from);
1247 r->device_to = be32_to_cpu(__r->device_to);
1248 r->sector_from = be64_to_cpu(sector_from);
1251 typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1253 static void blk_log_action_classic(struct trace_iterator *iter, const char *act)
1255 char rwbs[RWBS_LEN];
1256 unsigned long long ts = iter->ts;
1257 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1258 unsigned secs = (unsigned long)ts;
1259 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1263 trace_seq_printf(&iter->seq,
1264 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1265 MAJOR(t->device), MINOR(t->device), iter->cpu,
1266 secs, nsec_rem, iter->ent->pid, act, rwbs);
1269 static void blk_log_action(struct trace_iterator *iter, const char *act)
1271 char rwbs[RWBS_LEN];
1272 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1275 trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1276 MAJOR(t->device), MINOR(t->device), act, rwbs);
1279 static void blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1281 const unsigned char *pdu_buf;
1285 pdu_buf = pdu_start(ent);
1286 pdu_len = te_blk_io_trace(ent)->pdu_len;
1291 /* find the last zero that needs to be printed */
1292 for (end = pdu_len - 1; end >= 0; end--)
1297 trace_seq_putc(s, '(');
1299 for (i = 0; i < pdu_len; i++) {
1301 trace_seq_printf(s, "%s%02x",
1302 i == 0 ? "" : " ", pdu_buf[i]);
1305 * stop when the rest is just zeroes and indicate so
1306 * with a ".." appended
1308 if (i == end && end != pdu_len - 1) {
1309 trace_seq_puts(s, " ..) ");
1314 trace_seq_puts(s, ") ");
1317 static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1319 char cmd[TASK_COMM_LEN];
1321 trace_find_cmdline(ent->pid, cmd);
1323 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1324 trace_seq_printf(s, "%u ", t_bytes(ent));
1325 blk_log_dump_pdu(s, ent);
1326 trace_seq_printf(s, "[%s]\n", cmd);
1329 trace_seq_printf(s, "%llu + %u [%s]\n",
1330 t_sector(ent), t_sec(ent), cmd);
1332 trace_seq_printf(s, "[%s]\n", cmd);
1336 static void blk_log_with_error(struct trace_seq *s,
1337 const struct trace_entry *ent)
1339 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1340 blk_log_dump_pdu(s, ent);
1341 trace_seq_printf(s, "[%d]\n", t_error(ent));
1344 trace_seq_printf(s, "%llu + %u [%d]\n",
1346 t_sec(ent), t_error(ent));
1348 trace_seq_printf(s, "%llu [%d]\n",
1349 t_sector(ent), t_error(ent));
1353 static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1355 struct blk_io_trace_remap r = { .device_from = 0, };
1357 get_pdu_remap(ent, &r);
1358 trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1359 t_sector(ent), t_sec(ent),
1360 MAJOR(r.device_from), MINOR(r.device_from),
1361 (unsigned long long)r.sector_from);
1364 static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1366 char cmd[TASK_COMM_LEN];
1368 trace_find_cmdline(ent->pid, cmd);
1370 trace_seq_printf(s, "[%s]\n", cmd);
1373 static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1375 char cmd[TASK_COMM_LEN];
1377 trace_find_cmdline(ent->pid, cmd);
1379 trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1382 static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1384 char cmd[TASK_COMM_LEN];
1386 trace_find_cmdline(ent->pid, cmd);
1388 trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1389 get_pdu_int(ent), cmd);
1392 static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1394 const struct blk_io_trace *t = te_blk_io_trace(ent);
1396 trace_seq_putmem(s, t + 1, t->pdu_len);
1397 trace_seq_putc(s, '\n');
1401 * struct tracer operations
1404 static void blk_tracer_print_header(struct seq_file *m)
1406 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1408 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1412 static void blk_tracer_start(struct trace_array *tr)
1414 blk_tracer_enabled = true;
1417 static int blk_tracer_init(struct trace_array *tr)
1420 blk_tracer_start(tr);
1424 static void blk_tracer_stop(struct trace_array *tr)
1426 blk_tracer_enabled = false;
1429 static void blk_tracer_reset(struct trace_array *tr)
1431 blk_tracer_stop(tr);
1434 static const struct {
1436 void (*print)(struct trace_seq *s, const struct trace_entry *ent);
1438 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1439 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1440 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1441 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1442 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1443 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1444 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1445 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1446 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1447 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1448 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1449 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1450 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1451 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1452 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1455 static enum print_line_t print_one_line(struct trace_iterator *iter,
1458 struct trace_array *tr = iter->tr;
1459 struct trace_seq *s = &iter->seq;
1460 const struct blk_io_trace *t;
1463 blk_log_action_t *log_action;
1465 t = te_blk_io_trace(iter->ent);
1466 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1467 long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
1468 log_action = classic ? &blk_log_action_classic : &blk_log_action;
1470 if (t->action == BLK_TN_MESSAGE) {
1471 log_action(iter, long_act ? "message" : "m");
1472 blk_log_msg(s, iter->ent);
1475 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1476 trace_seq_printf(s, "Unknown action %x\n", what);
1478 log_action(iter, what2act[what].act[long_act]);
1479 what2act[what].print(s, iter->ent);
1482 return trace_handle_return(s);
1485 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1486 int flags, struct trace_event *event)
1488 return print_one_line(iter, false);
1491 static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1493 struct trace_seq *s = &iter->seq;
1494 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1495 const int offset = offsetof(struct blk_io_trace, sector);
1496 struct blk_io_trace old = {
1497 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1501 trace_seq_putmem(s, &old, offset);
1502 trace_seq_putmem(s, &t->sector,
1503 sizeof(old) - offset + t->pdu_len);
1506 static enum print_line_t
1507 blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1508 struct trace_event *event)
1510 blk_trace_synthesize_old_trace(iter);
1512 return trace_handle_return(&iter->seq);
1515 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1517 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1518 return TRACE_TYPE_UNHANDLED;
1520 return print_one_line(iter, true);
1524 blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1526 /* don't output context-info for blk_classic output */
1527 if (bit == TRACE_BLK_OPT_CLASSIC) {
1529 tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1531 tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
1536 static struct tracer blk_tracer __read_mostly = {
1538 .init = blk_tracer_init,
1539 .reset = blk_tracer_reset,
1540 .start = blk_tracer_start,
1541 .stop = blk_tracer_stop,
1542 .print_header = blk_tracer_print_header,
1543 .print_line = blk_tracer_print_line,
1544 .flags = &blk_tracer_flags,
1545 .set_flag = blk_tracer_set_flag,
1548 static struct trace_event_functions trace_blk_event_funcs = {
1549 .trace = blk_trace_event_print,
1550 .binary = blk_trace_event_print_binary,
1553 static struct trace_event trace_blk_event = {
1555 .funcs = &trace_blk_event_funcs,
1558 static int __init init_blk_tracer(void)
1560 if (!register_trace_event(&trace_blk_event)) {
1561 pr_warning("Warning: could not register block events\n");
1565 if (register_tracer(&blk_tracer) != 0) {
1566 pr_warning("Warning: could not register the block tracer\n");
1567 unregister_trace_event(&trace_blk_event);
1574 device_initcall(init_blk_tracer);
1576 static int blk_trace_remove_queue(struct request_queue *q)
1578 struct blk_trace *bt;
1580 bt = xchg(&q->blk_trace, NULL);
1584 if (bt->trace_state == Blktrace_running) {
1585 bt->trace_state = Blktrace_stopped;
1586 spin_lock_irq(&running_trace_lock);
1587 list_del_init(&bt->running_list);
1588 spin_unlock_irq(&running_trace_lock);
1589 relay_flush(bt->rchan);
1599 * Setup everything required to start tracing
1601 static int blk_trace_setup_queue(struct request_queue *q,
1602 struct block_device *bdev)
1604 struct blk_trace *bt = NULL;
1607 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1611 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1615 bt->dev = bdev->bd_dev;
1616 bt->act_mask = (u16)-1;
1618 blk_trace_setup_lba(bt, bdev);
1621 if (cmpxchg(&q->blk_trace, NULL, bt))
1633 * sysfs interface to enable and configure tracing
1636 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1637 struct device_attribute *attr,
1639 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1640 struct device_attribute *attr,
1641 const char *buf, size_t count);
1642 #define BLK_TRACE_DEVICE_ATTR(_name) \
1643 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1644 sysfs_blk_trace_attr_show, \
1645 sysfs_blk_trace_attr_store)
1647 static BLK_TRACE_DEVICE_ATTR(enable);
1648 static BLK_TRACE_DEVICE_ATTR(act_mask);
1649 static BLK_TRACE_DEVICE_ATTR(pid);
1650 static BLK_TRACE_DEVICE_ATTR(start_lba);
1651 static BLK_TRACE_DEVICE_ATTR(end_lba);
1653 static struct attribute *blk_trace_attrs[] = {
1654 &dev_attr_enable.attr,
1655 &dev_attr_act_mask.attr,
1657 &dev_attr_start_lba.attr,
1658 &dev_attr_end_lba.attr,
1662 struct attribute_group blk_trace_attr_group = {
1664 .attrs = blk_trace_attrs,
1667 static const struct {
1671 { BLK_TC_READ, "read" },
1672 { BLK_TC_WRITE, "write" },
1673 { BLK_TC_FLUSH, "flush" },
1674 { BLK_TC_SYNC, "sync" },
1675 { BLK_TC_QUEUE, "queue" },
1676 { BLK_TC_REQUEUE, "requeue" },
1677 { BLK_TC_ISSUE, "issue" },
1678 { BLK_TC_COMPLETE, "complete" },
1679 { BLK_TC_FS, "fs" },
1680 { BLK_TC_PC, "pc" },
1681 { BLK_TC_AHEAD, "ahead" },
1682 { BLK_TC_META, "meta" },
1683 { BLK_TC_DISCARD, "discard" },
1684 { BLK_TC_DRV_DATA, "drv_data" },
1685 { BLK_TC_FUA, "fua" },
1688 static int blk_trace_str2mask(const char *str)
1692 char *buf, *s, *token;
1694 buf = kstrdup(str, GFP_KERNEL);
1700 token = strsep(&s, ",");
1707 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1708 if (strcasecmp(token, mask_maps[i].str) == 0) {
1709 mask |= mask_maps[i].mask;
1713 if (i == ARRAY_SIZE(mask_maps)) {
1723 static ssize_t blk_trace_mask2str(char *buf, int mask)
1728 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1729 if (mask & mask_maps[i].mask) {
1730 p += sprintf(p, "%s%s",
1731 (p == buf) ? "" : ",", mask_maps[i].str);
1739 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1741 if (bdev->bd_disk == NULL)
1744 return bdev_get_queue(bdev);
1747 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1748 struct device_attribute *attr,
1751 struct hd_struct *p = dev_to_part(dev);
1752 struct request_queue *q;
1753 struct block_device *bdev;
1754 struct blk_trace *bt;
1755 ssize_t ret = -ENXIO;
1757 bdev = bdget(part_devt(p));
1761 q = blk_trace_get_queue(bdev);
1765 mutex_lock(&q->blk_trace_mutex);
1767 bt = rcu_dereference_protected(q->blk_trace,
1768 lockdep_is_held(&q->blk_trace_mutex));
1769 if (attr == &dev_attr_enable) {
1770 ret = sprintf(buf, "%u\n", !!bt);
1771 goto out_unlock_bdev;
1775 ret = sprintf(buf, "disabled\n");
1776 else if (attr == &dev_attr_act_mask)
1777 ret = blk_trace_mask2str(buf, bt->act_mask);
1778 else if (attr == &dev_attr_pid)
1779 ret = sprintf(buf, "%u\n", bt->pid);
1780 else if (attr == &dev_attr_start_lba)
1781 ret = sprintf(buf, "%llu\n", bt->start_lba);
1782 else if (attr == &dev_attr_end_lba)
1783 ret = sprintf(buf, "%llu\n", bt->end_lba);
1786 mutex_unlock(&q->blk_trace_mutex);
1793 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1794 struct device_attribute *attr,
1795 const char *buf, size_t count)
1797 struct block_device *bdev;
1798 struct request_queue *q;
1799 struct hd_struct *p;
1800 struct blk_trace *bt;
1802 ssize_t ret = -EINVAL;
1807 if (attr == &dev_attr_act_mask) {
1808 if (sscanf(buf, "%llx", &value) != 1) {
1809 /* Assume it is a list of trace category names */
1810 ret = blk_trace_str2mask(buf);
1815 } else if (sscanf(buf, "%llu", &value) != 1)
1820 p = dev_to_part(dev);
1821 bdev = bdget(part_devt(p));
1825 q = blk_trace_get_queue(bdev);
1829 mutex_lock(&q->blk_trace_mutex);
1831 bt = rcu_dereference_protected(q->blk_trace,
1832 lockdep_is_held(&q->blk_trace_mutex));
1833 if (attr == &dev_attr_enable) {
1834 if (!!value == !!bt) {
1836 goto out_unlock_bdev;
1839 ret = blk_trace_setup_queue(q, bdev);
1841 ret = blk_trace_remove_queue(q);
1842 goto out_unlock_bdev;
1847 ret = blk_trace_setup_queue(q, bdev);
1848 bt = rcu_dereference_protected(q->blk_trace,
1849 lockdep_is_held(&q->blk_trace_mutex));
1853 if (attr == &dev_attr_act_mask)
1854 bt->act_mask = value;
1855 else if (attr == &dev_attr_pid)
1857 else if (attr == &dev_attr_start_lba)
1858 bt->start_lba = value;
1859 else if (attr == &dev_attr_end_lba)
1860 bt->end_lba = value;
1864 mutex_unlock(&q->blk_trace_mutex);
1868 return ret ? ret : count;
1871 int blk_trace_init_sysfs(struct device *dev)
1873 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1876 void blk_trace_remove_sysfs(struct device *dev)
1878 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1881 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1883 #ifdef CONFIG_EVENT_TRACING
1885 void blk_dump_cmd(char *buf, struct request *rq)
1888 int len = rq->cmd_len;
1889 unsigned char *cmd = rq->cmd;
1891 if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
1896 for (end = len - 1; end >= 0; end--)
1901 for (i = 0; i < len; i++) {
1902 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1903 if (i == end && end != len - 1) {
1904 sprintf(buf, " ..");
1910 void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1919 else if (rw & REQ_DISCARD)
1928 if (rw & REQ_RAHEAD)
1934 if (rw & REQ_SECURE)
1939 EXPORT_SYMBOL_GPL(blk_fill_rwbs);
1941 #endif /* CONFIG_EVENT_TRACING */