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)
134 struct timespec64 now;
138 /* need to check user space to see if this breaks in y2038 or y2106 */
139 ktime_get_real_ts64(&now);
140 words[0] = (u32)now.tv_sec;
141 words[1] = now.tv_nsec;
143 local_irq_save(flags);
144 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
145 local_irq_restore(flags);
148 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
155 if (unlikely(bt->trace_state != Blktrace_running &&
156 !blk_tracer_enabled))
160 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
161 * message to the trace.
163 if (!(bt->act_mask & BLK_TC_NOTIFY))
166 local_irq_save(flags);
167 buf = this_cpu_ptr(bt->msg_data);
169 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
172 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
173 local_irq_restore(flags);
175 EXPORT_SYMBOL_GPL(__trace_note_message);
177 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
180 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
182 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
184 if (bt->pid && pid != bt->pid)
191 * Data direction bit lookup
193 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
194 BLK_TC_ACT(BLK_TC_WRITE) };
196 #define BLK_TC_RAHEAD BLK_TC_AHEAD
197 #define BLK_TC_PREFLUSH BLK_TC_FLUSH
199 /* The ilog2() calls fall out because they're constant */
200 #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
201 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
204 * The worker for the various blk_add_trace*() types. Fills out a
205 * blk_io_trace structure and places it in a per-cpu subbuffer.
207 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
208 int op, int op_flags, u32 what, int error, int pdu_len,
211 struct task_struct *tsk = current;
212 struct ring_buffer_event *event = NULL;
213 struct ring_buffer *buffer = NULL;
214 struct blk_io_trace *t;
215 unsigned long flags = 0;
216 unsigned long *sequence;
219 bool blk_tracer = blk_tracer_enabled;
221 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
224 what |= ddir_act[op_is_write(op) ? WRITE : READ];
225 what |= MASK_TC_BIT(op_flags, SYNC);
226 what |= MASK_TC_BIT(op_flags, RAHEAD);
227 what |= MASK_TC_BIT(op_flags, META);
228 what |= MASK_TC_BIT(op_flags, PREFLUSH);
229 what |= MASK_TC_BIT(op_flags, FUA);
230 if (op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE)
231 what |= BLK_TC_ACT(BLK_TC_DISCARD);
232 if (op == REQ_OP_FLUSH)
233 what |= BLK_TC_ACT(BLK_TC_FLUSH);
236 if (act_log_check(bt, what, sector, pid))
238 cpu = raw_smp_processor_id();
241 tracing_record_cmdline(current);
243 buffer = blk_tr->trace_buffer.buffer;
244 pc = preempt_count();
245 event = trace_buffer_lock_reserve(buffer, TRACE_BLK,
246 sizeof(*t) + pdu_len,
250 t = ring_buffer_event_data(event);
254 if (unlikely(tsk->btrace_seq != blktrace_seq))
258 * A word about the locking here - we disable interrupts to reserve
259 * some space in the relay per-cpu buffer, to prevent an irq
260 * from coming in and stepping on our toes.
262 local_irq_save(flags);
263 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
265 sequence = per_cpu_ptr(bt->sequence, cpu);
267 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
268 t->sequence = ++(*sequence);
269 t->time = ktime_to_ns(ktime_get());
272 * These two are not needed in ftrace as they are in the
273 * generic trace_entry, filled by tracing_generic_entry_update,
274 * but for the trace_event->bin() synthesizer benefit we do it
285 t->pdu_len = pdu_len;
288 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
291 trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc);
296 local_irq_restore(flags);
299 static struct dentry *blk_tree_root;
300 static DEFINE_MUTEX(blk_tree_mutex);
302 static void blk_trace_free(struct blk_trace *bt)
304 debugfs_remove(bt->msg_file);
305 debugfs_remove(bt->dropped_file);
306 relay_close(bt->rchan);
307 debugfs_remove(bt->dir);
308 free_percpu(bt->sequence);
309 free_percpu(bt->msg_data);
313 static void get_probe_ref(void)
315 mutex_lock(&blk_probe_mutex);
316 if (++blk_probes_ref == 1)
317 blk_register_tracepoints();
318 mutex_unlock(&blk_probe_mutex);
321 static void put_probe_ref(void)
323 mutex_lock(&blk_probe_mutex);
324 if (!--blk_probes_ref)
325 blk_unregister_tracepoints();
326 mutex_unlock(&blk_probe_mutex);
329 static void blk_trace_cleanup(struct blk_trace *bt)
336 static int __blk_trace_remove(struct request_queue *q)
338 struct blk_trace *bt;
340 bt = xchg(&q->blk_trace, NULL);
344 if (bt->trace_state != Blktrace_running)
345 blk_trace_cleanup(bt);
350 int blk_trace_remove(struct request_queue *q)
354 mutex_lock(&q->blk_trace_mutex);
355 ret = __blk_trace_remove(q);
356 mutex_unlock(&q->blk_trace_mutex);
360 EXPORT_SYMBOL_GPL(blk_trace_remove);
362 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
363 size_t count, loff_t *ppos)
365 struct blk_trace *bt = filp->private_data;
368 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
370 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
373 static const struct file_operations blk_dropped_fops = {
374 .owner = THIS_MODULE,
376 .read = blk_dropped_read,
377 .llseek = default_llseek,
380 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
381 size_t count, loff_t *ppos)
384 struct blk_trace *bt;
386 if (count >= BLK_TN_MAX_MSG)
389 msg = memdup_user_nul(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, req_op(rq), 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, req_op(rq),
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_op(bio), bio->bi_opf, 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, 0, BLK_TA_GETRQ, 0, 0,
921 static void blk_add_trace_sleeprq(void *ignore,
922 struct request_queue *q,
923 struct bio *bio, int rw)
926 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0);
928 struct blk_trace *bt;
931 bt = rcu_dereference(q->blk_trace);
933 __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_SLEEPRQ,
939 static void blk_add_trace_plug(void *ignore, struct request_queue *q)
941 struct blk_trace *bt;
944 bt = rcu_dereference(q->blk_trace);
946 __blk_add_trace(bt, 0, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
950 static void blk_add_trace_unplug(void *ignore, struct request_queue *q,
951 unsigned int depth, bool explicit)
953 struct blk_trace *bt;
956 bt = rcu_dereference(q->blk_trace);
958 __be64 rpdu = cpu_to_be64(depth);
962 what = BLK_TA_UNPLUG_IO;
964 what = BLK_TA_UNPLUG_TIMER;
966 __blk_add_trace(bt, 0, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
971 static void blk_add_trace_split(void *ignore,
972 struct request_queue *q, struct bio *bio,
975 struct blk_trace *bt;
978 bt = rcu_dereference(q->blk_trace);
980 __be64 rpdu = cpu_to_be64(pdu);
982 __blk_add_trace(bt, bio->bi_iter.bi_sector,
983 bio->bi_iter.bi_size, bio_op(bio), bio->bi_opf,
984 BLK_TA_SPLIT, bio->bi_error, sizeof(rpdu),
991 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
992 * @ignore: trace callback data parameter (not used)
993 * @q: queue the io is for
994 * @bio: the source bio
995 * @dev: target device
996 * @from: source sector
999 * Device mapper or raid target sometimes need to split a bio because
1000 * it spans a stripe (or similar). Add a trace for that action.
1003 static void blk_add_trace_bio_remap(void *ignore,
1004 struct request_queue *q, struct bio *bio,
1005 dev_t dev, sector_t from)
1007 struct blk_trace *bt;
1008 struct blk_io_trace_remap r;
1011 bt = rcu_dereference(q->blk_trace);
1017 r.device_from = cpu_to_be32(dev);
1018 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
1019 r.sector_from = cpu_to_be64(from);
1021 __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size,
1022 bio_op(bio), bio->bi_opf, BLK_TA_REMAP, bio->bi_error,
1028 * blk_add_trace_rq_remap - Add a trace for a request-remap operation
1029 * @ignore: trace callback data parameter (not used)
1030 * @q: queue the io is for
1031 * @rq: the source request
1032 * @dev: target device
1033 * @from: source sector
1036 * Device mapper remaps request to other devices.
1037 * Add a trace for that action.
1040 static void blk_add_trace_rq_remap(void *ignore,
1041 struct request_queue *q,
1042 struct request *rq, dev_t dev,
1045 struct blk_trace *bt;
1046 struct blk_io_trace_remap r;
1049 bt = rcu_dereference(q->blk_trace);
1055 r.device_from = cpu_to_be32(dev);
1056 r.device_to = cpu_to_be32(disk_devt(rq->rq_disk));
1057 r.sector_from = cpu_to_be64(from);
1059 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq),
1060 rq_data_dir(rq), 0, BLK_TA_REMAP, !!rq->errors,
1066 * blk_add_driver_data - Add binary message with driver-specific data
1067 * @q: queue the io is for
1069 * @data: driver-specific data
1070 * @len: length of driver-specific data
1073 * Some drivers might want to write driver-specific data per request.
1076 void blk_add_driver_data(struct request_queue *q,
1078 void *data, size_t len)
1080 struct blk_trace *bt;
1083 bt = rcu_dereference(q->blk_trace);
1089 if (rq->cmd_type == REQ_TYPE_BLOCK_PC)
1090 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0, 0,
1091 BLK_TA_DRV_DATA, rq->errors, len, data);
1093 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0, 0,
1094 BLK_TA_DRV_DATA, rq->errors, len, data);
1097 EXPORT_SYMBOL_GPL(blk_add_driver_data);
1099 static void blk_register_tracepoints(void)
1103 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1105 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1107 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1109 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1111 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1113 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1115 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1117 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1119 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1121 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1123 ret = register_trace_block_getrq(blk_add_trace_getrq, NULL);
1125 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1127 ret = register_trace_block_plug(blk_add_trace_plug, NULL);
1129 ret = register_trace_block_unplug(blk_add_trace_unplug, NULL);
1131 ret = register_trace_block_split(blk_add_trace_split, NULL);
1133 ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1135 ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1139 static void blk_unregister_tracepoints(void)
1141 unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL);
1142 unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL);
1143 unregister_trace_block_split(blk_add_trace_split, NULL);
1144 unregister_trace_block_unplug(blk_add_trace_unplug, NULL);
1145 unregister_trace_block_plug(blk_add_trace_plug, NULL);
1146 unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL);
1147 unregister_trace_block_getrq(blk_add_trace_getrq, NULL);
1148 unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL);
1149 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL);
1150 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL);
1151 unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL);
1152 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL);
1153 unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL);
1154 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL);
1155 unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL);
1156 unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL);
1157 unregister_trace_block_rq_abort(blk_add_trace_rq_abort, NULL);
1159 tracepoint_synchronize_unregister();
1163 * struct blk_io_tracer formatting routines
1166 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
1169 int tc = t->action >> BLK_TC_SHIFT;
1171 if (t->action == BLK_TN_MESSAGE) {
1176 if (tc & BLK_TC_FLUSH)
1179 if (tc & BLK_TC_DISCARD)
1181 else if (tc & BLK_TC_WRITE)
1188 if (tc & BLK_TC_FUA)
1190 if (tc & BLK_TC_AHEAD)
1192 if (tc & BLK_TC_SYNC)
1194 if (tc & BLK_TC_META)
1201 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
1203 return (const struct blk_io_trace *)ent;
1206 static inline const void *pdu_start(const struct trace_entry *ent)
1208 return te_blk_io_trace(ent) + 1;
1211 static inline u32 t_action(const struct trace_entry *ent)
1213 return te_blk_io_trace(ent)->action;
1216 static inline u32 t_bytes(const struct trace_entry *ent)
1218 return te_blk_io_trace(ent)->bytes;
1221 static inline u32 t_sec(const struct trace_entry *ent)
1223 return te_blk_io_trace(ent)->bytes >> 9;
1226 static inline unsigned long long t_sector(const struct trace_entry *ent)
1228 return te_blk_io_trace(ent)->sector;
1231 static inline __u16 t_error(const struct trace_entry *ent)
1233 return te_blk_io_trace(ent)->error;
1236 static __u64 get_pdu_int(const struct trace_entry *ent)
1238 const __u64 *val = pdu_start(ent);
1239 return be64_to_cpu(*val);
1242 static void get_pdu_remap(const struct trace_entry *ent,
1243 struct blk_io_trace_remap *r)
1245 const struct blk_io_trace_remap *__r = pdu_start(ent);
1246 __u64 sector_from = __r->sector_from;
1248 r->device_from = be32_to_cpu(__r->device_from);
1249 r->device_to = be32_to_cpu(__r->device_to);
1250 r->sector_from = be64_to_cpu(sector_from);
1253 typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1255 static void blk_log_action_classic(struct trace_iterator *iter, const char *act)
1257 char rwbs[RWBS_LEN];
1258 unsigned long long ts = iter->ts;
1259 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1260 unsigned secs = (unsigned long)ts;
1261 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1265 trace_seq_printf(&iter->seq,
1266 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1267 MAJOR(t->device), MINOR(t->device), iter->cpu,
1268 secs, nsec_rem, iter->ent->pid, act, rwbs);
1271 static void blk_log_action(struct trace_iterator *iter, const char *act)
1273 char rwbs[RWBS_LEN];
1274 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1277 trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1278 MAJOR(t->device), MINOR(t->device), act, rwbs);
1281 static void blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1283 const unsigned char *pdu_buf;
1287 pdu_buf = pdu_start(ent);
1288 pdu_len = te_blk_io_trace(ent)->pdu_len;
1293 /* find the last zero that needs to be printed */
1294 for (end = pdu_len - 1; end >= 0; end--)
1299 trace_seq_putc(s, '(');
1301 for (i = 0; i < pdu_len; i++) {
1303 trace_seq_printf(s, "%s%02x",
1304 i == 0 ? "" : " ", pdu_buf[i]);
1307 * stop when the rest is just zeroes and indicate so
1308 * with a ".." appended
1310 if (i == end && end != pdu_len - 1) {
1311 trace_seq_puts(s, " ..) ");
1316 trace_seq_puts(s, ") ");
1319 static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1321 char cmd[TASK_COMM_LEN];
1323 trace_find_cmdline(ent->pid, cmd);
1325 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1326 trace_seq_printf(s, "%u ", t_bytes(ent));
1327 blk_log_dump_pdu(s, ent);
1328 trace_seq_printf(s, "[%s]\n", cmd);
1331 trace_seq_printf(s, "%llu + %u [%s]\n",
1332 t_sector(ent), t_sec(ent), cmd);
1334 trace_seq_printf(s, "[%s]\n", cmd);
1338 static void blk_log_with_error(struct trace_seq *s,
1339 const struct trace_entry *ent)
1341 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1342 blk_log_dump_pdu(s, ent);
1343 trace_seq_printf(s, "[%d]\n", t_error(ent));
1346 trace_seq_printf(s, "%llu + %u [%d]\n",
1348 t_sec(ent), t_error(ent));
1350 trace_seq_printf(s, "%llu [%d]\n",
1351 t_sector(ent), t_error(ent));
1355 static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1357 struct blk_io_trace_remap r = { .device_from = 0, };
1359 get_pdu_remap(ent, &r);
1360 trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1361 t_sector(ent), t_sec(ent),
1362 MAJOR(r.device_from), MINOR(r.device_from),
1363 (unsigned long long)r.sector_from);
1366 static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1368 char cmd[TASK_COMM_LEN];
1370 trace_find_cmdline(ent->pid, cmd);
1372 trace_seq_printf(s, "[%s]\n", cmd);
1375 static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1377 char cmd[TASK_COMM_LEN];
1379 trace_find_cmdline(ent->pid, cmd);
1381 trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1384 static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1386 char cmd[TASK_COMM_LEN];
1388 trace_find_cmdline(ent->pid, cmd);
1390 trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1391 get_pdu_int(ent), cmd);
1394 static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1396 const struct blk_io_trace *t = te_blk_io_trace(ent);
1398 trace_seq_putmem(s, t + 1, t->pdu_len);
1399 trace_seq_putc(s, '\n');
1403 * struct tracer operations
1406 static void blk_tracer_print_header(struct seq_file *m)
1408 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1410 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1414 static void blk_tracer_start(struct trace_array *tr)
1416 blk_tracer_enabled = true;
1419 static int blk_tracer_init(struct trace_array *tr)
1422 blk_tracer_start(tr);
1426 static void blk_tracer_stop(struct trace_array *tr)
1428 blk_tracer_enabled = false;
1431 static void blk_tracer_reset(struct trace_array *tr)
1433 blk_tracer_stop(tr);
1436 static const struct {
1438 void (*print)(struct trace_seq *s, const struct trace_entry *ent);
1440 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1441 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1442 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1443 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1444 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1445 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1446 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1447 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1448 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1449 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1450 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1451 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1452 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1453 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1454 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1457 static enum print_line_t print_one_line(struct trace_iterator *iter,
1460 struct trace_array *tr = iter->tr;
1461 struct trace_seq *s = &iter->seq;
1462 const struct blk_io_trace *t;
1465 blk_log_action_t *log_action;
1467 t = te_blk_io_trace(iter->ent);
1468 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1469 long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE);
1470 log_action = classic ? &blk_log_action_classic : &blk_log_action;
1472 if (t->action == BLK_TN_MESSAGE) {
1473 log_action(iter, long_act ? "message" : "m");
1474 blk_log_msg(s, iter->ent);
1475 return trace_handle_return(s);
1478 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1479 trace_seq_printf(s, "Unknown action %x\n", what);
1481 log_action(iter, what2act[what].act[long_act]);
1482 what2act[what].print(s, iter->ent);
1485 return trace_handle_return(s);
1488 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1489 int flags, struct trace_event *event)
1491 return print_one_line(iter, false);
1494 static void blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1496 struct trace_seq *s = &iter->seq;
1497 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1498 const int offset = offsetof(struct blk_io_trace, sector);
1499 struct blk_io_trace old = {
1500 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1504 trace_seq_putmem(s, &old, offset);
1505 trace_seq_putmem(s, &t->sector,
1506 sizeof(old) - offset + t->pdu_len);
1509 static enum print_line_t
1510 blk_trace_event_print_binary(struct trace_iterator *iter, int flags,
1511 struct trace_event *event)
1513 blk_trace_synthesize_old_trace(iter);
1515 return trace_handle_return(&iter->seq);
1518 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1520 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1521 return TRACE_TYPE_UNHANDLED;
1523 return print_one_line(iter, true);
1527 blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
1529 /* don't output context-info for blk_classic output */
1530 if (bit == TRACE_BLK_OPT_CLASSIC) {
1532 tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1534 tr->trace_flags |= TRACE_ITER_CONTEXT_INFO;
1539 static struct tracer blk_tracer __read_mostly = {
1541 .init = blk_tracer_init,
1542 .reset = blk_tracer_reset,
1543 .start = blk_tracer_start,
1544 .stop = blk_tracer_stop,
1545 .print_header = blk_tracer_print_header,
1546 .print_line = blk_tracer_print_line,
1547 .flags = &blk_tracer_flags,
1548 .set_flag = blk_tracer_set_flag,
1551 static struct trace_event_functions trace_blk_event_funcs = {
1552 .trace = blk_trace_event_print,
1553 .binary = blk_trace_event_print_binary,
1556 static struct trace_event trace_blk_event = {
1558 .funcs = &trace_blk_event_funcs,
1561 static int __init init_blk_tracer(void)
1563 if (!register_trace_event(&trace_blk_event)) {
1564 pr_warn("Warning: could not register block events\n");
1568 if (register_tracer(&blk_tracer) != 0) {
1569 pr_warn("Warning: could not register the block tracer\n");
1570 unregister_trace_event(&trace_blk_event);
1577 device_initcall(init_blk_tracer);
1579 static int blk_trace_remove_queue(struct request_queue *q)
1581 struct blk_trace *bt;
1583 bt = xchg(&q->blk_trace, NULL);
1587 if (bt->trace_state == Blktrace_running) {
1588 bt->trace_state = Blktrace_stopped;
1589 spin_lock_irq(&running_trace_lock);
1590 list_del_init(&bt->running_list);
1591 spin_unlock_irq(&running_trace_lock);
1592 relay_flush(bt->rchan);
1602 * Setup everything required to start tracing
1604 static int blk_trace_setup_queue(struct request_queue *q,
1605 struct block_device *bdev)
1607 struct blk_trace *bt = NULL;
1610 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1614 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1618 bt->dev = bdev->bd_dev;
1619 bt->act_mask = (u16)-1;
1621 blk_trace_setup_lba(bt, bdev);
1624 if (cmpxchg(&q->blk_trace, NULL, bt))
1636 * sysfs interface to enable and configure tracing
1639 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1640 struct device_attribute *attr,
1642 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1643 struct device_attribute *attr,
1644 const char *buf, size_t count);
1645 #define BLK_TRACE_DEVICE_ATTR(_name) \
1646 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1647 sysfs_blk_trace_attr_show, \
1648 sysfs_blk_trace_attr_store)
1650 static BLK_TRACE_DEVICE_ATTR(enable);
1651 static BLK_TRACE_DEVICE_ATTR(act_mask);
1652 static BLK_TRACE_DEVICE_ATTR(pid);
1653 static BLK_TRACE_DEVICE_ATTR(start_lba);
1654 static BLK_TRACE_DEVICE_ATTR(end_lba);
1656 static struct attribute *blk_trace_attrs[] = {
1657 &dev_attr_enable.attr,
1658 &dev_attr_act_mask.attr,
1660 &dev_attr_start_lba.attr,
1661 &dev_attr_end_lba.attr,
1665 struct attribute_group blk_trace_attr_group = {
1667 .attrs = blk_trace_attrs,
1670 static const struct {
1674 { BLK_TC_READ, "read" },
1675 { BLK_TC_WRITE, "write" },
1676 { BLK_TC_FLUSH, "flush" },
1677 { BLK_TC_SYNC, "sync" },
1678 { BLK_TC_QUEUE, "queue" },
1679 { BLK_TC_REQUEUE, "requeue" },
1680 { BLK_TC_ISSUE, "issue" },
1681 { BLK_TC_COMPLETE, "complete" },
1682 { BLK_TC_FS, "fs" },
1683 { BLK_TC_PC, "pc" },
1684 { BLK_TC_NOTIFY, "notify" },
1685 { BLK_TC_AHEAD, "ahead" },
1686 { BLK_TC_META, "meta" },
1687 { BLK_TC_DISCARD, "discard" },
1688 { BLK_TC_DRV_DATA, "drv_data" },
1689 { BLK_TC_FUA, "fua" },
1692 static int blk_trace_str2mask(const char *str)
1696 char *buf, *s, *token;
1698 buf = kstrdup(str, GFP_KERNEL);
1704 token = strsep(&s, ",");
1711 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1712 if (strcasecmp(token, mask_maps[i].str) == 0) {
1713 mask |= mask_maps[i].mask;
1717 if (i == ARRAY_SIZE(mask_maps)) {
1727 static ssize_t blk_trace_mask2str(char *buf, int mask)
1732 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1733 if (mask & mask_maps[i].mask) {
1734 p += sprintf(p, "%s%s",
1735 (p == buf) ? "" : ",", mask_maps[i].str);
1743 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1745 if (bdev->bd_disk == NULL)
1748 return bdev_get_queue(bdev);
1751 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1752 struct device_attribute *attr,
1755 struct hd_struct *p = dev_to_part(dev);
1756 struct request_queue *q;
1757 struct block_device *bdev;
1758 struct blk_trace *bt;
1759 ssize_t ret = -ENXIO;
1761 bdev = bdget(part_devt(p));
1765 q = blk_trace_get_queue(bdev);
1769 mutex_lock(&q->blk_trace_mutex);
1771 bt = rcu_dereference_protected(q->blk_trace,
1772 lockdep_is_held(&q->blk_trace_mutex));
1773 if (attr == &dev_attr_enable) {
1774 ret = sprintf(buf, "%u\n", !!bt);
1775 goto out_unlock_bdev;
1779 ret = sprintf(buf, "disabled\n");
1780 else if (attr == &dev_attr_act_mask)
1781 ret = blk_trace_mask2str(buf, bt->act_mask);
1782 else if (attr == &dev_attr_pid)
1783 ret = sprintf(buf, "%u\n", bt->pid);
1784 else if (attr == &dev_attr_start_lba)
1785 ret = sprintf(buf, "%llu\n", bt->start_lba);
1786 else if (attr == &dev_attr_end_lba)
1787 ret = sprintf(buf, "%llu\n", bt->end_lba);
1790 mutex_unlock(&q->blk_trace_mutex);
1797 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1798 struct device_attribute *attr,
1799 const char *buf, size_t count)
1801 struct block_device *bdev;
1802 struct request_queue *q;
1803 struct hd_struct *p;
1804 struct blk_trace *bt;
1806 ssize_t ret = -EINVAL;
1811 if (attr == &dev_attr_act_mask) {
1812 if (sscanf(buf, "%llx", &value) != 1) {
1813 /* Assume it is a list of trace category names */
1814 ret = blk_trace_str2mask(buf);
1819 } else if (sscanf(buf, "%llu", &value) != 1)
1824 p = dev_to_part(dev);
1825 bdev = bdget(part_devt(p));
1829 q = blk_trace_get_queue(bdev);
1833 mutex_lock(&q->blk_trace_mutex);
1835 bt = rcu_dereference_protected(q->blk_trace,
1836 lockdep_is_held(&q->blk_trace_mutex));
1837 if (attr == &dev_attr_enable) {
1838 if (!!value == !!bt) {
1840 goto out_unlock_bdev;
1843 ret = blk_trace_setup_queue(q, bdev);
1845 ret = blk_trace_remove_queue(q);
1846 goto out_unlock_bdev;
1851 ret = blk_trace_setup_queue(q, bdev);
1852 bt = rcu_dereference_protected(q->blk_trace,
1853 lockdep_is_held(&q->blk_trace_mutex));
1857 if (attr == &dev_attr_act_mask)
1858 bt->act_mask = value;
1859 else if (attr == &dev_attr_pid)
1861 else if (attr == &dev_attr_start_lba)
1862 bt->start_lba = value;
1863 else if (attr == &dev_attr_end_lba)
1864 bt->end_lba = value;
1868 mutex_unlock(&q->blk_trace_mutex);
1872 return ret ? ret : count;
1875 int blk_trace_init_sysfs(struct device *dev)
1877 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1880 void blk_trace_remove_sysfs(struct device *dev)
1882 sysfs_remove_group(&dev->kobj, &blk_trace_attr_group);
1885 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1887 #ifdef CONFIG_EVENT_TRACING
1889 void blk_dump_cmd(char *buf, struct request *rq)
1892 int len = rq->cmd_len;
1893 unsigned char *cmd = rq->cmd;
1895 if (rq->cmd_type != REQ_TYPE_BLOCK_PC) {
1900 for (end = len - 1; end >= 0; end--)
1905 for (i = 0; i < len; i++) {
1906 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1907 if (i == end && end != len - 1) {
1908 sprintf(buf, " ..");
1914 void blk_fill_rwbs(char *rwbs, int op, u32 rw, int bytes)
1918 if (rw & REQ_PREFLUSH)
1923 case REQ_OP_WRITE_SAME:
1926 case REQ_OP_DISCARD:
1929 case REQ_OP_SECURE_ERASE:
1945 if (rw & REQ_RAHEAD)
1954 EXPORT_SYMBOL_GPL(blk_fill_rwbs);
1956 #endif /* CONFIG_EVENT_TRACING */