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>
30 #include <linux/blk-cgroup.h>
32 #include "../../block/blk.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
50 #define TRACE_BLK_OPT_CGROUP 0x2
51 #define TRACE_BLK_OPT_CGNAME 0x4
53 static struct tracer_opt blk_tracer_opts
[] = {
54 /* Default disable the minimalistic output */
55 { TRACER_OPT(blk_classic
, TRACE_BLK_OPT_CLASSIC
) },
56 #ifdef CONFIG_BLK_CGROUP
57 { TRACER_OPT(blk_cgroup
, TRACE_BLK_OPT_CGROUP
) },
58 { TRACER_OPT(blk_cgname
, TRACE_BLK_OPT_CGNAME
) },
63 static struct tracer_flags blk_tracer_flags
= {
65 .opts
= blk_tracer_opts
,
68 /* Global reference count of probes */
69 static DEFINE_MUTEX(blk_probe_mutex
);
70 static int blk_probes_ref
;
72 static void blk_register_tracepoints(void);
73 static void blk_unregister_tracepoints(void);
76 * Send out a notify message.
78 static void trace_note(struct blk_trace
*bt
, pid_t pid
, int action
,
79 const void *data
, size_t len
,
80 union kernfs_node_id
*cgid
)
82 struct blk_io_trace
*t
;
83 struct ring_buffer_event
*event
= NULL
;
84 struct ring_buffer
*buffer
= NULL
;
86 int cpu
= smp_processor_id();
87 bool blk_tracer
= blk_tracer_enabled
;
88 ssize_t cgid_len
= cgid
? sizeof(*cgid
) : 0;
91 buffer
= blk_tr
->trace_buffer
.buffer
;
93 event
= trace_buffer_lock_reserve(buffer
, TRACE_BLK
,
94 sizeof(*t
) + len
+ cgid_len
,
98 t
= ring_buffer_event_data(event
);
105 t
= relay_reserve(bt
->rchan
, sizeof(*t
) + len
+ cgid_len
);
107 t
->magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
;
108 t
->time
= ktime_to_ns(ktime_get());
111 t
->action
= action
| (cgid
? __BLK_TN_CGROUP
: 0);
114 t
->pdu_len
= len
+ cgid_len
;
116 memcpy((void *)t
+ sizeof(*t
), cgid
, cgid_len
);
117 memcpy((void *) t
+ sizeof(*t
) + cgid_len
, data
, len
);
120 trace_buffer_unlock_commit(blk_tr
, buffer
, event
, 0, pc
);
125 * Send out a notify for this process, if we haven't done so since a trace
128 static void trace_note_tsk(struct task_struct
*tsk
)
131 struct blk_trace
*bt
;
133 tsk
->btrace_seq
= blktrace_seq
;
134 spin_lock_irqsave(&running_trace_lock
, flags
);
135 list_for_each_entry(bt
, &running_trace_list
, running_list
) {
136 trace_note(bt
, tsk
->pid
, BLK_TN_PROCESS
, tsk
->comm
,
137 sizeof(tsk
->comm
), NULL
);
139 spin_unlock_irqrestore(&running_trace_lock
, flags
);
142 static void trace_note_time(struct blk_trace
*bt
)
144 struct timespec64 now
;
148 /* need to check user space to see if this breaks in y2038 or y2106 */
149 ktime_get_real_ts64(&now
);
150 words
[0] = (u32
)now
.tv_sec
;
151 words
[1] = now
.tv_nsec
;
153 local_irq_save(flags
);
154 trace_note(bt
, 0, BLK_TN_TIMESTAMP
, words
, sizeof(words
), NULL
);
155 local_irq_restore(flags
);
158 void __trace_note_message(struct blk_trace
*bt
, struct blkcg
*blkcg
,
159 const char *fmt
, ...)
166 if (unlikely(bt
->trace_state
!= Blktrace_running
&&
167 !blk_tracer_enabled
))
171 * If the BLK_TC_NOTIFY action mask isn't set, don't send any note
172 * message to the trace.
174 if (!(bt
->act_mask
& BLK_TC_NOTIFY
))
177 local_irq_save(flags
);
178 buf
= this_cpu_ptr(bt
->msg_data
);
180 n
= vscnprintf(buf
, BLK_TN_MAX_MSG
, fmt
, args
);
183 if (!(blk_tracer_flags
.val
& TRACE_BLK_OPT_CGROUP
))
185 #ifdef CONFIG_BLK_CGROUP
186 trace_note(bt
, 0, BLK_TN_MESSAGE
, buf
, n
,
187 blkcg
? cgroup_get_kernfs_id(blkcg
->css
.cgroup
) : NULL
);
189 trace_note(bt
, 0, BLK_TN_MESSAGE
, buf
, n
, NULL
);
191 local_irq_restore(flags
);
193 EXPORT_SYMBOL_GPL(__trace_note_message
);
195 static int act_log_check(struct blk_trace
*bt
, u32 what
, sector_t sector
,
198 if (((bt
->act_mask
<< BLK_TC_SHIFT
) & what
) == 0)
200 if (sector
&& (sector
< bt
->start_lba
|| sector
> bt
->end_lba
))
202 if (bt
->pid
&& pid
!= bt
->pid
)
209 * Data direction bit lookup
211 static const u32 ddir_act
[2] = { BLK_TC_ACT(BLK_TC_READ
),
212 BLK_TC_ACT(BLK_TC_WRITE
) };
214 #define BLK_TC_RAHEAD BLK_TC_AHEAD
215 #define BLK_TC_PREFLUSH BLK_TC_FLUSH
217 /* The ilog2() calls fall out because they're constant */
218 #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \
219 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name))
222 * The worker for the various blk_add_trace*() types. Fills out a
223 * blk_io_trace structure and places it in a per-cpu subbuffer.
225 static void __blk_add_trace(struct blk_trace
*bt
, sector_t sector
, int bytes
,
226 int op
, int op_flags
, u32 what
, int error
, int pdu_len
,
227 void *pdu_data
, union kernfs_node_id
*cgid
)
229 struct task_struct
*tsk
= current
;
230 struct ring_buffer_event
*event
= NULL
;
231 struct ring_buffer
*buffer
= NULL
;
232 struct blk_io_trace
*t
;
233 unsigned long flags
= 0;
234 unsigned long *sequence
;
237 bool blk_tracer
= blk_tracer_enabled
;
238 ssize_t cgid_len
= cgid
? sizeof(*cgid
) : 0;
240 if (unlikely(bt
->trace_state
!= Blktrace_running
&& !blk_tracer
))
243 what
|= ddir_act
[op_is_write(op
) ? WRITE
: READ
];
244 what
|= MASK_TC_BIT(op_flags
, SYNC
);
245 what
|= MASK_TC_BIT(op_flags
, RAHEAD
);
246 what
|= MASK_TC_BIT(op_flags
, META
);
247 what
|= MASK_TC_BIT(op_flags
, PREFLUSH
);
248 what
|= MASK_TC_BIT(op_flags
, FUA
);
249 if (op
== REQ_OP_DISCARD
|| op
== REQ_OP_SECURE_ERASE
)
250 what
|= BLK_TC_ACT(BLK_TC_DISCARD
);
251 if (op
== REQ_OP_FLUSH
)
252 what
|= BLK_TC_ACT(BLK_TC_FLUSH
);
254 what
|= __BLK_TA_CGROUP
;
257 if (act_log_check(bt
, what
, sector
, pid
))
259 cpu
= raw_smp_processor_id();
262 tracing_record_cmdline(current
);
264 buffer
= blk_tr
->trace_buffer
.buffer
;
265 pc
= preempt_count();
266 event
= trace_buffer_lock_reserve(buffer
, TRACE_BLK
,
267 sizeof(*t
) + pdu_len
+ cgid_len
,
271 t
= ring_buffer_event_data(event
);
275 if (unlikely(tsk
->btrace_seq
!= blktrace_seq
))
279 * A word about the locking here - we disable interrupts to reserve
280 * some space in the relay per-cpu buffer, to prevent an irq
281 * from coming in and stepping on our toes.
283 local_irq_save(flags
);
284 t
= relay_reserve(bt
->rchan
, sizeof(*t
) + pdu_len
+ cgid_len
);
286 sequence
= per_cpu_ptr(bt
->sequence
, cpu
);
288 t
->magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
;
289 t
->sequence
= ++(*sequence
);
290 t
->time
= ktime_to_ns(ktime_get());
293 * These two are not needed in ftrace as they are in the
294 * generic trace_entry, filled by tracing_generic_entry_update,
295 * but for the trace_event->bin() synthesizer benefit we do it
306 t
->pdu_len
= pdu_len
+ cgid_len
;
309 memcpy((void *)t
+ sizeof(*t
), cgid
, cgid_len
);
311 memcpy((void *)t
+ sizeof(*t
) + cgid_len
, pdu_data
, pdu_len
);
314 trace_buffer_unlock_commit(blk_tr
, buffer
, event
, 0, pc
);
319 local_irq_restore(flags
);
322 static void blk_trace_free(struct blk_trace
*bt
)
324 debugfs_remove(bt
->msg_file
);
325 debugfs_remove(bt
->dropped_file
);
326 relay_close(bt
->rchan
);
327 debugfs_remove(bt
->dir
);
328 free_percpu(bt
->sequence
);
329 free_percpu(bt
->msg_data
);
333 static void get_probe_ref(void)
335 mutex_lock(&blk_probe_mutex
);
336 if (++blk_probes_ref
== 1)
337 blk_register_tracepoints();
338 mutex_unlock(&blk_probe_mutex
);
341 static void put_probe_ref(void)
343 mutex_lock(&blk_probe_mutex
);
344 if (!--blk_probes_ref
)
345 blk_unregister_tracepoints();
346 mutex_unlock(&blk_probe_mutex
);
349 static void blk_trace_cleanup(struct blk_trace
*bt
)
355 static int __blk_trace_remove(struct request_queue
*q
)
357 struct blk_trace
*bt
;
359 bt
= xchg(&q
->blk_trace
, NULL
);
363 if (bt
->trace_state
!= Blktrace_running
)
364 blk_trace_cleanup(bt
);
369 int blk_trace_remove(struct request_queue
*q
)
373 mutex_lock(&q
->blk_trace_mutex
);
374 ret
= __blk_trace_remove(q
);
375 mutex_unlock(&q
->blk_trace_mutex
);
379 EXPORT_SYMBOL_GPL(blk_trace_remove
);
381 static ssize_t
blk_dropped_read(struct file
*filp
, char __user
*buffer
,
382 size_t count
, loff_t
*ppos
)
384 struct blk_trace
*bt
= filp
->private_data
;
387 snprintf(buf
, sizeof(buf
), "%u\n", atomic_read(&bt
->dropped
));
389 return simple_read_from_buffer(buffer
, count
, ppos
, buf
, strlen(buf
));
392 static const struct file_operations blk_dropped_fops
= {
393 .owner
= THIS_MODULE
,
395 .read
= blk_dropped_read
,
396 .llseek
= default_llseek
,
399 static ssize_t
blk_msg_write(struct file
*filp
, const char __user
*buffer
,
400 size_t count
, loff_t
*ppos
)
403 struct blk_trace
*bt
;
405 if (count
>= BLK_TN_MAX_MSG
)
408 msg
= memdup_user_nul(buffer
, count
);
412 bt
= filp
->private_data
;
413 __trace_note_message(bt
, NULL
, "%s", msg
);
419 static const struct file_operations blk_msg_fops
= {
420 .owner
= THIS_MODULE
,
422 .write
= blk_msg_write
,
423 .llseek
= noop_llseek
,
427 * Keep track of how many times we encountered a full subbuffer, to aid
428 * the user space app in telling how many lost events there were.
430 static int blk_subbuf_start_callback(struct rchan_buf
*buf
, void *subbuf
,
431 void *prev_subbuf
, size_t prev_padding
)
433 struct blk_trace
*bt
;
435 if (!relay_buf_full(buf
))
438 bt
= buf
->chan
->private_data
;
439 atomic_inc(&bt
->dropped
);
443 static int blk_remove_buf_file_callback(struct dentry
*dentry
)
445 debugfs_remove(dentry
);
450 static struct dentry
*blk_create_buf_file_callback(const char *filename
,
451 struct dentry
*parent
,
453 struct rchan_buf
*buf
,
456 return debugfs_create_file(filename
, mode
, parent
, buf
,
457 &relay_file_operations
);
460 static struct rchan_callbacks blk_relay_callbacks
= {
461 .subbuf_start
= blk_subbuf_start_callback
,
462 .create_buf_file
= blk_create_buf_file_callback
,
463 .remove_buf_file
= blk_remove_buf_file_callback
,
466 static void blk_trace_setup_lba(struct blk_trace
*bt
,
467 struct block_device
*bdev
)
469 struct hd_struct
*part
= NULL
;
472 part
= bdev
->bd_part
;
475 bt
->start_lba
= part
->start_sect
;
476 bt
->end_lba
= part
->start_sect
+ part
->nr_sects
;
484 * Setup everything required to start tracing
486 static int do_blk_trace_setup(struct request_queue
*q
, char *name
, dev_t dev
,
487 struct block_device
*bdev
,
488 struct blk_user_trace_setup
*buts
)
490 struct blk_trace
*bt
= NULL
;
491 struct dentry
*dir
= NULL
;
494 if (!buts
->buf_size
|| !buts
->buf_nr
)
497 strncpy(buts
->name
, name
, BLKTRACE_BDEV_SIZE
);
498 buts
->name
[BLKTRACE_BDEV_SIZE
- 1] = '\0';
501 * some device names have larger paths - convert the slashes
502 * to underscores for this to work as expected
504 strreplace(buts
->name
, '/', '_');
506 bt
= kzalloc(sizeof(*bt
), GFP_KERNEL
);
511 bt
->sequence
= alloc_percpu(unsigned long);
515 bt
->msg_data
= __alloc_percpu(BLK_TN_MAX_MSG
, __alignof__(char));
521 if (!blk_debugfs_root
)
524 dir
= debugfs_lookup(buts
->name
, blk_debugfs_root
);
526 bt
->dir
= dir
= debugfs_create_dir(buts
->name
, blk_debugfs_root
);
531 atomic_set(&bt
->dropped
, 0);
532 INIT_LIST_HEAD(&bt
->running_list
);
535 bt
->dropped_file
= debugfs_create_file("dropped", 0444, dir
, bt
,
537 if (!bt
->dropped_file
)
540 bt
->msg_file
= debugfs_create_file("msg", 0222, dir
, bt
, &blk_msg_fops
);
544 bt
->rchan
= relay_open("trace", dir
, buts
->buf_size
,
545 buts
->buf_nr
, &blk_relay_callbacks
, bt
);
549 bt
->act_mask
= buts
->act_mask
;
551 bt
->act_mask
= (u16
) -1;
553 blk_trace_setup_lba(bt
, bdev
);
555 /* overwrite with user settings */
557 bt
->start_lba
= buts
->start_lba
;
559 bt
->end_lba
= buts
->end_lba
;
562 bt
->trace_state
= Blktrace_setup
;
565 if (cmpxchg(&q
->blk_trace
, NULL
, bt
))
579 static int __blk_trace_setup(struct request_queue
*q
, char *name
, dev_t dev
,
580 struct block_device
*bdev
, char __user
*arg
)
582 struct blk_user_trace_setup buts
;
585 ret
= copy_from_user(&buts
, arg
, sizeof(buts
));
589 ret
= do_blk_trace_setup(q
, name
, dev
, bdev
, &buts
);
593 if (copy_to_user(arg
, &buts
, sizeof(buts
))) {
594 __blk_trace_remove(q
);
600 int blk_trace_setup(struct request_queue
*q
, char *name
, dev_t dev
,
601 struct block_device
*bdev
,
606 mutex_lock(&q
->blk_trace_mutex
);
607 ret
= __blk_trace_setup(q
, name
, dev
, bdev
, arg
);
608 mutex_unlock(&q
->blk_trace_mutex
);
612 EXPORT_SYMBOL_GPL(blk_trace_setup
);
614 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
615 static int compat_blk_trace_setup(struct request_queue
*q
, char *name
,
616 dev_t dev
, struct block_device
*bdev
,
619 struct blk_user_trace_setup buts
;
620 struct compat_blk_user_trace_setup cbuts
;
623 if (copy_from_user(&cbuts
, arg
, sizeof(cbuts
)))
626 buts
= (struct blk_user_trace_setup
) {
627 .act_mask
= cbuts
.act_mask
,
628 .buf_size
= cbuts
.buf_size
,
629 .buf_nr
= cbuts
.buf_nr
,
630 .start_lba
= cbuts
.start_lba
,
631 .end_lba
= cbuts
.end_lba
,
635 ret
= do_blk_trace_setup(q
, name
, dev
, bdev
, &buts
);
639 if (copy_to_user(arg
, &buts
.name
, ARRAY_SIZE(buts
.name
))) {
640 __blk_trace_remove(q
);
648 static int __blk_trace_startstop(struct request_queue
*q
, int start
)
651 struct blk_trace
*bt
= q
->blk_trace
;
657 * For starting a trace, we can transition from a setup or stopped
658 * trace. For stopping a trace, the state must be running
662 if (bt
->trace_state
== Blktrace_setup
||
663 bt
->trace_state
== Blktrace_stopped
) {
666 bt
->trace_state
= Blktrace_running
;
667 spin_lock_irq(&running_trace_lock
);
668 list_add(&bt
->running_list
, &running_trace_list
);
669 spin_unlock_irq(&running_trace_lock
);
675 if (bt
->trace_state
== Blktrace_running
) {
676 bt
->trace_state
= Blktrace_stopped
;
677 spin_lock_irq(&running_trace_lock
);
678 list_del_init(&bt
->running_list
);
679 spin_unlock_irq(&running_trace_lock
);
680 relay_flush(bt
->rchan
);
688 int blk_trace_startstop(struct request_queue
*q
, int start
)
692 mutex_lock(&q
->blk_trace_mutex
);
693 ret
= __blk_trace_startstop(q
, start
);
694 mutex_unlock(&q
->blk_trace_mutex
);
698 EXPORT_SYMBOL_GPL(blk_trace_startstop
);
701 * When reading or writing the blktrace sysfs files, the references to the
702 * opened sysfs or device files should prevent the underlying block device
703 * from being removed. So no further delete protection is really needed.
707 * blk_trace_ioctl: - handle the ioctls associated with tracing
708 * @bdev: the block device
709 * @cmd: the ioctl cmd
710 * @arg: the argument data, if any
713 int blk_trace_ioctl(struct block_device
*bdev
, unsigned cmd
, char __user
*arg
)
715 struct request_queue
*q
;
717 char b
[BDEVNAME_SIZE
];
719 q
= bdev_get_queue(bdev
);
723 mutex_lock(&q
->blk_trace_mutex
);
728 ret
= __blk_trace_setup(q
, b
, bdev
->bd_dev
, bdev
, arg
);
730 #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64)
731 case BLKTRACESETUP32
:
733 ret
= compat_blk_trace_setup(q
, b
, bdev
->bd_dev
, bdev
, arg
);
739 ret
= __blk_trace_startstop(q
, start
);
741 case BLKTRACETEARDOWN
:
742 ret
= __blk_trace_remove(q
);
749 mutex_unlock(&q
->blk_trace_mutex
);
754 * blk_trace_shutdown: - stop and cleanup trace structures
755 * @q: the request queue associated with the device
758 void blk_trace_shutdown(struct request_queue
*q
)
760 mutex_lock(&q
->blk_trace_mutex
);
763 __blk_trace_startstop(q
, 0);
764 __blk_trace_remove(q
);
767 mutex_unlock(&q
->blk_trace_mutex
);
770 #ifdef CONFIG_BLK_CGROUP
771 static union kernfs_node_id
*
772 blk_trace_bio_get_cgid(struct request_queue
*q
, struct bio
*bio
)
774 struct blk_trace
*bt
= q
->blk_trace
;
776 if (!bt
|| !(blk_tracer_flags
.val
& TRACE_BLK_OPT_CGROUP
))
781 return cgroup_get_kernfs_id(bio
->bi_css
->cgroup
);
784 static union kernfs_node_id
*
785 blk_trace_bio_get_cgid(struct request_queue
*q
, struct bio
*bio
)
791 static union kernfs_node_id
*
792 blk_trace_request_get_cgid(struct request_queue
*q
, struct request
*rq
)
796 /* Use the first bio */
797 return blk_trace_bio_get_cgid(q
, rq
->bio
);
805 * blk_add_trace_rq - Add a trace for a request oriented action
806 * @rq: the source request
807 * @error: return status to log
808 * @nr_bytes: number of completed bytes
810 * @cgid: the cgroup info
813 * Records an action against a request. Will log the bio offset + size.
816 static void blk_add_trace_rq(struct request
*rq
, int error
,
817 unsigned int nr_bytes
, u32 what
,
818 union kernfs_node_id
*cgid
)
820 struct blk_trace
*bt
= rq
->q
->blk_trace
;
825 if (blk_rq_is_passthrough(rq
))
826 what
|= BLK_TC_ACT(BLK_TC_PC
);
828 what
|= BLK_TC_ACT(BLK_TC_FS
);
830 __blk_add_trace(bt
, blk_rq_trace_sector(rq
), nr_bytes
, req_op(rq
),
831 rq
->cmd_flags
, what
, error
, 0, NULL
, cgid
);
834 static void blk_add_trace_rq_insert(void *ignore
,
835 struct request_queue
*q
, struct request
*rq
)
837 blk_add_trace_rq(rq
, 0, blk_rq_bytes(rq
), BLK_TA_INSERT
,
838 blk_trace_request_get_cgid(q
, rq
));
841 static void blk_add_trace_rq_issue(void *ignore
,
842 struct request_queue
*q
, struct request
*rq
)
844 blk_add_trace_rq(rq
, 0, blk_rq_bytes(rq
), BLK_TA_ISSUE
,
845 blk_trace_request_get_cgid(q
, rq
));
848 static void blk_add_trace_rq_requeue(void *ignore
,
849 struct request_queue
*q
,
852 blk_add_trace_rq(rq
, 0, blk_rq_bytes(rq
), BLK_TA_REQUEUE
,
853 blk_trace_request_get_cgid(q
, rq
));
856 static void blk_add_trace_rq_complete(void *ignore
, struct request
*rq
,
857 int error
, unsigned int nr_bytes
)
859 blk_add_trace_rq(rq
, error
, nr_bytes
, BLK_TA_COMPLETE
,
860 blk_trace_request_get_cgid(rq
->q
, rq
));
864 * blk_add_trace_bio - Add a trace for a bio oriented action
865 * @q: queue the io is for
866 * @bio: the source bio
868 * @error: error, if any
871 * Records an action against a bio. Will log the bio offset + size.
874 static void blk_add_trace_bio(struct request_queue
*q
, struct bio
*bio
,
877 struct blk_trace
*bt
= q
->blk_trace
;
882 __blk_add_trace(bt
, bio
->bi_iter
.bi_sector
, bio
->bi_iter
.bi_size
,
883 bio_op(bio
), bio
->bi_opf
, what
, error
, 0, NULL
,
884 blk_trace_bio_get_cgid(q
, bio
));
887 static void blk_add_trace_bio_bounce(void *ignore
,
888 struct request_queue
*q
, struct bio
*bio
)
890 blk_add_trace_bio(q
, bio
, BLK_TA_BOUNCE
, 0);
893 static void blk_add_trace_bio_complete(void *ignore
,
894 struct request_queue
*q
, struct bio
*bio
,
897 blk_add_trace_bio(q
, bio
, BLK_TA_COMPLETE
, error
);
900 static void blk_add_trace_bio_backmerge(void *ignore
,
901 struct request_queue
*q
,
905 blk_add_trace_bio(q
, bio
, BLK_TA_BACKMERGE
, 0);
908 static void blk_add_trace_bio_frontmerge(void *ignore
,
909 struct request_queue
*q
,
913 blk_add_trace_bio(q
, bio
, BLK_TA_FRONTMERGE
, 0);
916 static void blk_add_trace_bio_queue(void *ignore
,
917 struct request_queue
*q
, struct bio
*bio
)
919 blk_add_trace_bio(q
, bio
, BLK_TA_QUEUE
, 0);
922 static void blk_add_trace_getrq(void *ignore
,
923 struct request_queue
*q
,
924 struct bio
*bio
, int rw
)
927 blk_add_trace_bio(q
, bio
, BLK_TA_GETRQ
, 0);
929 struct blk_trace
*bt
= q
->blk_trace
;
932 __blk_add_trace(bt
, 0, 0, rw
, 0, BLK_TA_GETRQ
, 0, 0,
938 static void blk_add_trace_sleeprq(void *ignore
,
939 struct request_queue
*q
,
940 struct bio
*bio
, int rw
)
943 blk_add_trace_bio(q
, bio
, BLK_TA_SLEEPRQ
, 0);
945 struct blk_trace
*bt
= q
->blk_trace
;
948 __blk_add_trace(bt
, 0, 0, rw
, 0, BLK_TA_SLEEPRQ
,
953 static void blk_add_trace_plug(void *ignore
, struct request_queue
*q
)
955 struct blk_trace
*bt
= q
->blk_trace
;
958 __blk_add_trace(bt
, 0, 0, 0, 0, BLK_TA_PLUG
, 0, 0, NULL
, NULL
);
961 static void blk_add_trace_unplug(void *ignore
, struct request_queue
*q
,
962 unsigned int depth
, bool explicit)
964 struct blk_trace
*bt
= q
->blk_trace
;
967 __be64 rpdu
= cpu_to_be64(depth
);
971 what
= BLK_TA_UNPLUG_IO
;
973 what
= BLK_TA_UNPLUG_TIMER
;
975 __blk_add_trace(bt
, 0, 0, 0, 0, what
, 0, sizeof(rpdu
), &rpdu
, NULL
);
979 static void blk_add_trace_split(void *ignore
,
980 struct request_queue
*q
, struct bio
*bio
,
983 struct blk_trace
*bt
= q
->blk_trace
;
986 __be64 rpdu
= cpu_to_be64(pdu
);
988 __blk_add_trace(bt
, bio
->bi_iter
.bi_sector
,
989 bio
->bi_iter
.bi_size
, bio_op(bio
), bio
->bi_opf
,
990 BLK_TA_SPLIT
, bio
->bi_status
, sizeof(rpdu
),
991 &rpdu
, blk_trace_bio_get_cgid(q
, bio
));
996 * blk_add_trace_bio_remap - Add a trace for a bio-remap operation
997 * @ignore: trace callback data parameter (not used)
998 * @q: queue the io is for
999 * @bio: the source bio
1000 * @dev: target device
1001 * @from: source sector
1004 * Device mapper or raid target sometimes need to split a bio because
1005 * it spans a stripe (or similar). Add a trace for that action.
1008 static void blk_add_trace_bio_remap(void *ignore
,
1009 struct request_queue
*q
, struct bio
*bio
,
1010 dev_t dev
, sector_t from
)
1012 struct blk_trace
*bt
= q
->blk_trace
;
1013 struct blk_io_trace_remap r
;
1018 r
.device_from
= cpu_to_be32(dev
);
1019 r
.device_to
= cpu_to_be32(bio_dev(bio
));
1020 r
.sector_from
= cpu_to_be64(from
);
1022 __blk_add_trace(bt
, bio
->bi_iter
.bi_sector
, bio
->bi_iter
.bi_size
,
1023 bio_op(bio
), bio
->bi_opf
, BLK_TA_REMAP
, bio
->bi_status
,
1024 sizeof(r
), &r
, blk_trace_bio_get_cgid(q
, bio
));
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
= q
->blk_trace
;
1046 struct blk_io_trace_remap r
;
1051 r
.device_from
= cpu_to_be32(dev
);
1052 r
.device_to
= cpu_to_be32(disk_devt(rq
->rq_disk
));
1053 r
.sector_from
= cpu_to_be64(from
);
1055 __blk_add_trace(bt
, blk_rq_pos(rq
), blk_rq_bytes(rq
),
1056 rq_data_dir(rq
), 0, BLK_TA_REMAP
, 0,
1057 sizeof(r
), &r
, blk_trace_request_get_cgid(q
, rq
));
1061 * blk_add_driver_data - Add binary message with driver-specific data
1062 * @q: queue the io is for
1064 * @data: driver-specific data
1065 * @len: length of driver-specific data
1068 * Some drivers might want to write driver-specific data per request.
1071 void blk_add_driver_data(struct request_queue
*q
,
1073 void *data
, size_t len
)
1075 struct blk_trace
*bt
= q
->blk_trace
;
1080 __blk_add_trace(bt
, blk_rq_trace_sector(rq
), blk_rq_bytes(rq
), 0, 0,
1081 BLK_TA_DRV_DATA
, 0, len
, data
,
1082 blk_trace_request_get_cgid(q
, rq
));
1084 EXPORT_SYMBOL_GPL(blk_add_driver_data
);
1086 static void blk_register_tracepoints(void)
1090 ret
= register_trace_block_rq_insert(blk_add_trace_rq_insert
, NULL
);
1092 ret
= register_trace_block_rq_issue(blk_add_trace_rq_issue
, NULL
);
1094 ret
= register_trace_block_rq_requeue(blk_add_trace_rq_requeue
, NULL
);
1096 ret
= register_trace_block_rq_complete(blk_add_trace_rq_complete
, NULL
);
1098 ret
= register_trace_block_bio_bounce(blk_add_trace_bio_bounce
, NULL
);
1100 ret
= register_trace_block_bio_complete(blk_add_trace_bio_complete
, NULL
);
1102 ret
= register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge
, NULL
);
1104 ret
= register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge
, NULL
);
1106 ret
= register_trace_block_bio_queue(blk_add_trace_bio_queue
, NULL
);
1108 ret
= register_trace_block_getrq(blk_add_trace_getrq
, NULL
);
1110 ret
= register_trace_block_sleeprq(blk_add_trace_sleeprq
, NULL
);
1112 ret
= register_trace_block_plug(blk_add_trace_plug
, NULL
);
1114 ret
= register_trace_block_unplug(blk_add_trace_unplug
, NULL
);
1116 ret
= register_trace_block_split(blk_add_trace_split
, NULL
);
1118 ret
= register_trace_block_bio_remap(blk_add_trace_bio_remap
, NULL
);
1120 ret
= register_trace_block_rq_remap(blk_add_trace_rq_remap
, NULL
);
1124 static void blk_unregister_tracepoints(void)
1126 unregister_trace_block_rq_remap(blk_add_trace_rq_remap
, NULL
);
1127 unregister_trace_block_bio_remap(blk_add_trace_bio_remap
, NULL
);
1128 unregister_trace_block_split(blk_add_trace_split
, NULL
);
1129 unregister_trace_block_unplug(blk_add_trace_unplug
, NULL
);
1130 unregister_trace_block_plug(blk_add_trace_plug
, NULL
);
1131 unregister_trace_block_sleeprq(blk_add_trace_sleeprq
, NULL
);
1132 unregister_trace_block_getrq(blk_add_trace_getrq
, NULL
);
1133 unregister_trace_block_bio_queue(blk_add_trace_bio_queue
, NULL
);
1134 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge
, NULL
);
1135 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge
, NULL
);
1136 unregister_trace_block_bio_complete(blk_add_trace_bio_complete
, NULL
);
1137 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce
, NULL
);
1138 unregister_trace_block_rq_complete(blk_add_trace_rq_complete
, NULL
);
1139 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue
, NULL
);
1140 unregister_trace_block_rq_issue(blk_add_trace_rq_issue
, NULL
);
1141 unregister_trace_block_rq_insert(blk_add_trace_rq_insert
, NULL
);
1143 tracepoint_synchronize_unregister();
1147 * struct blk_io_tracer formatting routines
1150 static void fill_rwbs(char *rwbs
, const struct blk_io_trace
*t
)
1153 int tc
= t
->action
>> BLK_TC_SHIFT
;
1155 if ((t
->action
& ~__BLK_TN_CGROUP
) == BLK_TN_MESSAGE
) {
1160 if (tc
& BLK_TC_FLUSH
)
1163 if (tc
& BLK_TC_DISCARD
)
1165 else if (tc
& BLK_TC_WRITE
)
1172 if (tc
& BLK_TC_FUA
)
1174 if (tc
& BLK_TC_AHEAD
)
1176 if (tc
& BLK_TC_SYNC
)
1178 if (tc
& BLK_TC_META
)
1185 const struct blk_io_trace
*te_blk_io_trace(const struct trace_entry
*ent
)
1187 return (const struct blk_io_trace
*)ent
;
1190 static inline const void *pdu_start(const struct trace_entry
*ent
, bool has_cg
)
1192 return (void *)(te_blk_io_trace(ent
) + 1) +
1193 (has_cg
? sizeof(union kernfs_node_id
) : 0);
1196 static inline const void *cgid_start(const struct trace_entry
*ent
)
1198 return (void *)(te_blk_io_trace(ent
) + 1);
1201 static inline int pdu_real_len(const struct trace_entry
*ent
, bool has_cg
)
1203 return te_blk_io_trace(ent
)->pdu_len
-
1204 (has_cg
? sizeof(union kernfs_node_id
) : 0);
1207 static inline u32
t_action(const struct trace_entry
*ent
)
1209 return te_blk_io_trace(ent
)->action
;
1212 static inline u32
t_bytes(const struct trace_entry
*ent
)
1214 return te_blk_io_trace(ent
)->bytes
;
1217 static inline u32
t_sec(const struct trace_entry
*ent
)
1219 return te_blk_io_trace(ent
)->bytes
>> 9;
1222 static inline unsigned long long t_sector(const struct trace_entry
*ent
)
1224 return te_blk_io_trace(ent
)->sector
;
1227 static inline __u16
t_error(const struct trace_entry
*ent
)
1229 return te_blk_io_trace(ent
)->error
;
1232 static __u64
get_pdu_int(const struct trace_entry
*ent
, bool has_cg
)
1234 const __u64
*val
= pdu_start(ent
, has_cg
);
1235 return be64_to_cpu(*val
);
1238 static void get_pdu_remap(const struct trace_entry
*ent
,
1239 struct blk_io_trace_remap
*r
, bool has_cg
)
1241 const struct blk_io_trace_remap
*__r
= pdu_start(ent
, has_cg
);
1242 __u64 sector_from
= __r
->sector_from
;
1244 r
->device_from
= be32_to_cpu(__r
->device_from
);
1245 r
->device_to
= be32_to_cpu(__r
->device_to
);
1246 r
->sector_from
= be64_to_cpu(sector_from
);
1249 typedef void (blk_log_action_t
) (struct trace_iterator
*iter
, const char *act
,
1252 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
,
1272 char rwbs
[RWBS_LEN
];
1273 const struct blk_io_trace
*t
= te_blk_io_trace(iter
->ent
);
1277 const union kernfs_node_id
*id
= cgid_start(iter
->ent
);
1279 if (blk_tracer_flags
.val
& TRACE_BLK_OPT_CGNAME
) {
1280 char blkcg_name_buf
[NAME_MAX
+ 1] = "<...>";
1282 cgroup_path_from_kernfs_id(id
, blkcg_name_buf
,
1283 sizeof(blkcg_name_buf
));
1284 trace_seq_printf(&iter
->seq
, "%3d,%-3d %s %2s %3s ",
1285 MAJOR(t
->device
), MINOR(t
->device
),
1286 blkcg_name_buf
, act
, rwbs
);
1288 trace_seq_printf(&iter
->seq
,
1289 "%3d,%-3d %x,%-x %2s %3s ",
1290 MAJOR(t
->device
), MINOR(t
->device
),
1291 id
->ino
, id
->generation
, act
, rwbs
);
1293 trace_seq_printf(&iter
->seq
, "%3d,%-3d %2s %3s ",
1294 MAJOR(t
->device
), MINOR(t
->device
), act
, rwbs
);
1297 static void blk_log_dump_pdu(struct trace_seq
*s
,
1298 const struct trace_entry
*ent
, bool has_cg
)
1300 const unsigned char *pdu_buf
;
1304 pdu_buf
= pdu_start(ent
, has_cg
);
1305 pdu_len
= pdu_real_len(ent
, has_cg
);
1310 /* find the last zero that needs to be printed */
1311 for (end
= pdu_len
- 1; end
>= 0; end
--)
1316 trace_seq_putc(s
, '(');
1318 for (i
= 0; i
< pdu_len
; i
++) {
1320 trace_seq_printf(s
, "%s%02x",
1321 i
== 0 ? "" : " ", pdu_buf
[i
]);
1324 * stop when the rest is just zeroes and indicate so
1325 * with a ".." appended
1327 if (i
== end
&& end
!= pdu_len
- 1) {
1328 trace_seq_puts(s
, " ..) ");
1333 trace_seq_puts(s
, ") ");
1336 static void blk_log_generic(struct trace_seq
*s
, const struct trace_entry
*ent
, bool has_cg
)
1338 char cmd
[TASK_COMM_LEN
];
1340 trace_find_cmdline(ent
->pid
, cmd
);
1342 if (t_action(ent
) & BLK_TC_ACT(BLK_TC_PC
)) {
1343 trace_seq_printf(s
, "%u ", t_bytes(ent
));
1344 blk_log_dump_pdu(s
, ent
, has_cg
);
1345 trace_seq_printf(s
, "[%s]\n", cmd
);
1348 trace_seq_printf(s
, "%llu + %u [%s]\n",
1349 t_sector(ent
), t_sec(ent
), cmd
);
1351 trace_seq_printf(s
, "[%s]\n", cmd
);
1355 static void blk_log_with_error(struct trace_seq
*s
,
1356 const struct trace_entry
*ent
, bool has_cg
)
1358 if (t_action(ent
) & BLK_TC_ACT(BLK_TC_PC
)) {
1359 blk_log_dump_pdu(s
, ent
, has_cg
);
1360 trace_seq_printf(s
, "[%d]\n", t_error(ent
));
1363 trace_seq_printf(s
, "%llu + %u [%d]\n",
1365 t_sec(ent
), t_error(ent
));
1367 trace_seq_printf(s
, "%llu [%d]\n",
1368 t_sector(ent
), t_error(ent
));
1372 static void blk_log_remap(struct trace_seq
*s
, const struct trace_entry
*ent
, bool has_cg
)
1374 struct blk_io_trace_remap r
= { .device_from
= 0, };
1376 get_pdu_remap(ent
, &r
, has_cg
);
1377 trace_seq_printf(s
, "%llu + %u <- (%d,%d) %llu\n",
1378 t_sector(ent
), t_sec(ent
),
1379 MAJOR(r
.device_from
), MINOR(r
.device_from
),
1380 (unsigned long long)r
.sector_from
);
1383 static void blk_log_plug(struct trace_seq
*s
, const struct trace_entry
*ent
, bool has_cg
)
1385 char cmd
[TASK_COMM_LEN
];
1387 trace_find_cmdline(ent
->pid
, cmd
);
1389 trace_seq_printf(s
, "[%s]\n", cmd
);
1392 static void blk_log_unplug(struct trace_seq
*s
, const struct trace_entry
*ent
, bool has_cg
)
1394 char cmd
[TASK_COMM_LEN
];
1396 trace_find_cmdline(ent
->pid
, cmd
);
1398 trace_seq_printf(s
, "[%s] %llu\n", cmd
, get_pdu_int(ent
, has_cg
));
1401 static void blk_log_split(struct trace_seq
*s
, const struct trace_entry
*ent
, bool has_cg
)
1403 char cmd
[TASK_COMM_LEN
];
1405 trace_find_cmdline(ent
->pid
, cmd
);
1407 trace_seq_printf(s
, "%llu / %llu [%s]\n", t_sector(ent
),
1408 get_pdu_int(ent
, has_cg
), cmd
);
1411 static void blk_log_msg(struct trace_seq
*s
, const struct trace_entry
*ent
,
1415 trace_seq_putmem(s
, pdu_start(ent
, has_cg
),
1416 pdu_real_len(ent
, has_cg
));
1417 trace_seq_putc(s
, '\n');
1421 * struct tracer operations
1424 static void blk_tracer_print_header(struct seq_file
*m
)
1426 if (!(blk_tracer_flags
.val
& TRACE_BLK_OPT_CLASSIC
))
1428 seq_puts(m
, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1432 static void blk_tracer_start(struct trace_array
*tr
)
1434 blk_tracer_enabled
= true;
1437 static int blk_tracer_init(struct trace_array
*tr
)
1440 blk_tracer_start(tr
);
1444 static void blk_tracer_stop(struct trace_array
*tr
)
1446 blk_tracer_enabled
= false;
1449 static void blk_tracer_reset(struct trace_array
*tr
)
1451 blk_tracer_stop(tr
);
1454 static const struct {
1456 void (*print
)(struct trace_seq
*s
, const struct trace_entry
*ent
,
1459 [__BLK_TA_QUEUE
] = {{ "Q", "queue" }, blk_log_generic
},
1460 [__BLK_TA_BACKMERGE
] = {{ "M", "backmerge" }, blk_log_generic
},
1461 [__BLK_TA_FRONTMERGE
] = {{ "F", "frontmerge" }, blk_log_generic
},
1462 [__BLK_TA_GETRQ
] = {{ "G", "getrq" }, blk_log_generic
},
1463 [__BLK_TA_SLEEPRQ
] = {{ "S", "sleeprq" }, blk_log_generic
},
1464 [__BLK_TA_REQUEUE
] = {{ "R", "requeue" }, blk_log_with_error
},
1465 [__BLK_TA_ISSUE
] = {{ "D", "issue" }, blk_log_generic
},
1466 [__BLK_TA_COMPLETE
] = {{ "C", "complete" }, blk_log_with_error
},
1467 [__BLK_TA_PLUG
] = {{ "P", "plug" }, blk_log_plug
},
1468 [__BLK_TA_UNPLUG_IO
] = {{ "U", "unplug_io" }, blk_log_unplug
},
1469 [__BLK_TA_UNPLUG_TIMER
] = {{ "UT", "unplug_timer" }, blk_log_unplug
},
1470 [__BLK_TA_INSERT
] = {{ "I", "insert" }, blk_log_generic
},
1471 [__BLK_TA_SPLIT
] = {{ "X", "split" }, blk_log_split
},
1472 [__BLK_TA_BOUNCE
] = {{ "B", "bounce" }, blk_log_generic
},
1473 [__BLK_TA_REMAP
] = {{ "A", "remap" }, blk_log_remap
},
1476 static enum print_line_t
print_one_line(struct trace_iterator
*iter
,
1479 struct trace_array
*tr
= iter
->tr
;
1480 struct trace_seq
*s
= &iter
->seq
;
1481 const struct blk_io_trace
*t
;
1484 blk_log_action_t
*log_action
;
1487 t
= te_blk_io_trace(iter
->ent
);
1488 what
= (t
->action
& ((1 << BLK_TC_SHIFT
) - 1)) & ~__BLK_TA_CGROUP
;
1489 long_act
= !!(tr
->trace_flags
& TRACE_ITER_VERBOSE
);
1490 log_action
= classic
? &blk_log_action_classic
: &blk_log_action
;
1491 has_cg
= t
->action
& __BLK_TA_CGROUP
;
1493 if ((t
->action
& ~__BLK_TN_CGROUP
) == BLK_TN_MESSAGE
) {
1494 log_action(iter
, long_act
? "message" : "m", has_cg
);
1495 blk_log_msg(s
, iter
->ent
, has_cg
);
1496 return trace_handle_return(s
);
1499 if (unlikely(what
== 0 || what
>= ARRAY_SIZE(what2act
)))
1500 trace_seq_printf(s
, "Unknown action %x\n", what
);
1502 log_action(iter
, what2act
[what
].act
[long_act
], has_cg
);
1503 what2act
[what
].print(s
, iter
->ent
, has_cg
);
1506 return trace_handle_return(s
);
1509 static enum print_line_t
blk_trace_event_print(struct trace_iterator
*iter
,
1510 int flags
, struct trace_event
*event
)
1512 return print_one_line(iter
, false);
1515 static void blk_trace_synthesize_old_trace(struct trace_iterator
*iter
)
1517 struct trace_seq
*s
= &iter
->seq
;
1518 struct blk_io_trace
*t
= (struct blk_io_trace
*)iter
->ent
;
1519 const int offset
= offsetof(struct blk_io_trace
, sector
);
1520 struct blk_io_trace old
= {
1521 .magic
= BLK_IO_TRACE_MAGIC
| BLK_IO_TRACE_VERSION
,
1525 trace_seq_putmem(s
, &old
, offset
);
1526 trace_seq_putmem(s
, &t
->sector
,
1527 sizeof(old
) - offset
+ t
->pdu_len
);
1530 static enum print_line_t
1531 blk_trace_event_print_binary(struct trace_iterator
*iter
, int flags
,
1532 struct trace_event
*event
)
1534 blk_trace_synthesize_old_trace(iter
);
1536 return trace_handle_return(&iter
->seq
);
1539 static enum print_line_t
blk_tracer_print_line(struct trace_iterator
*iter
)
1541 if (!(blk_tracer_flags
.val
& TRACE_BLK_OPT_CLASSIC
))
1542 return TRACE_TYPE_UNHANDLED
;
1544 return print_one_line(iter
, true);
1548 blk_tracer_set_flag(struct trace_array
*tr
, u32 old_flags
, u32 bit
, int set
)
1550 /* don't output context-info for blk_classic output */
1551 if (bit
== TRACE_BLK_OPT_CLASSIC
) {
1553 tr
->trace_flags
&= ~TRACE_ITER_CONTEXT_INFO
;
1555 tr
->trace_flags
|= TRACE_ITER_CONTEXT_INFO
;
1560 static struct tracer blk_tracer __read_mostly
= {
1562 .init
= blk_tracer_init
,
1563 .reset
= blk_tracer_reset
,
1564 .start
= blk_tracer_start
,
1565 .stop
= blk_tracer_stop
,
1566 .print_header
= blk_tracer_print_header
,
1567 .print_line
= blk_tracer_print_line
,
1568 .flags
= &blk_tracer_flags
,
1569 .set_flag
= blk_tracer_set_flag
,
1572 static struct trace_event_functions trace_blk_event_funcs
= {
1573 .trace
= blk_trace_event_print
,
1574 .binary
= blk_trace_event_print_binary
,
1577 static struct trace_event trace_blk_event
= {
1579 .funcs
= &trace_blk_event_funcs
,
1582 static int __init
init_blk_tracer(void)
1584 if (!register_trace_event(&trace_blk_event
)) {
1585 pr_warn("Warning: could not register block events\n");
1589 if (register_tracer(&blk_tracer
) != 0) {
1590 pr_warn("Warning: could not register the block tracer\n");
1591 unregister_trace_event(&trace_blk_event
);
1598 device_initcall(init_blk_tracer
);
1600 static int blk_trace_remove_queue(struct request_queue
*q
)
1602 struct blk_trace
*bt
;
1604 bt
= xchg(&q
->blk_trace
, NULL
);
1614 * Setup everything required to start tracing
1616 static int blk_trace_setup_queue(struct request_queue
*q
,
1617 struct block_device
*bdev
)
1619 struct blk_trace
*bt
= NULL
;
1622 bt
= kzalloc(sizeof(*bt
), GFP_KERNEL
);
1626 bt
->msg_data
= __alloc_percpu(BLK_TN_MAX_MSG
, __alignof__(char));
1630 bt
->dev
= bdev
->bd_dev
;
1631 bt
->act_mask
= (u16
)-1;
1633 blk_trace_setup_lba(bt
, bdev
);
1636 if (cmpxchg(&q
->blk_trace
, NULL
, bt
))
1648 * sysfs interface to enable and configure tracing
1651 static ssize_t
sysfs_blk_trace_attr_show(struct device
*dev
,
1652 struct device_attribute
*attr
,
1654 static ssize_t
sysfs_blk_trace_attr_store(struct device
*dev
,
1655 struct device_attribute
*attr
,
1656 const char *buf
, size_t count
);
1657 #define BLK_TRACE_DEVICE_ATTR(_name) \
1658 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1659 sysfs_blk_trace_attr_show, \
1660 sysfs_blk_trace_attr_store)
1662 static BLK_TRACE_DEVICE_ATTR(enable
);
1663 static BLK_TRACE_DEVICE_ATTR(act_mask
);
1664 static BLK_TRACE_DEVICE_ATTR(pid
);
1665 static BLK_TRACE_DEVICE_ATTR(start_lba
);
1666 static BLK_TRACE_DEVICE_ATTR(end_lba
);
1668 static struct attribute
*blk_trace_attrs
[] = {
1669 &dev_attr_enable
.attr
,
1670 &dev_attr_act_mask
.attr
,
1672 &dev_attr_start_lba
.attr
,
1673 &dev_attr_end_lba
.attr
,
1677 struct attribute_group blk_trace_attr_group
= {
1679 .attrs
= blk_trace_attrs
,
1682 static const struct {
1686 { BLK_TC_READ
, "read" },
1687 { BLK_TC_WRITE
, "write" },
1688 { BLK_TC_FLUSH
, "flush" },
1689 { BLK_TC_SYNC
, "sync" },
1690 { BLK_TC_QUEUE
, "queue" },
1691 { BLK_TC_REQUEUE
, "requeue" },
1692 { BLK_TC_ISSUE
, "issue" },
1693 { BLK_TC_COMPLETE
, "complete" },
1694 { BLK_TC_FS
, "fs" },
1695 { BLK_TC_PC
, "pc" },
1696 { BLK_TC_NOTIFY
, "notify" },
1697 { BLK_TC_AHEAD
, "ahead" },
1698 { BLK_TC_META
, "meta" },
1699 { BLK_TC_DISCARD
, "discard" },
1700 { BLK_TC_DRV_DATA
, "drv_data" },
1701 { BLK_TC_FUA
, "fua" },
1704 static int blk_trace_str2mask(const char *str
)
1708 char *buf
, *s
, *token
;
1710 buf
= kstrdup(str
, GFP_KERNEL
);
1716 token
= strsep(&s
, ",");
1723 for (i
= 0; i
< ARRAY_SIZE(mask_maps
); i
++) {
1724 if (strcasecmp(token
, mask_maps
[i
].str
) == 0) {
1725 mask
|= mask_maps
[i
].mask
;
1729 if (i
== ARRAY_SIZE(mask_maps
)) {
1739 static ssize_t
blk_trace_mask2str(char *buf
, int mask
)
1744 for (i
= 0; i
< ARRAY_SIZE(mask_maps
); i
++) {
1745 if (mask
& mask_maps
[i
].mask
) {
1746 p
+= sprintf(p
, "%s%s",
1747 (p
== buf
) ? "" : ",", mask_maps
[i
].str
);
1755 static struct request_queue
*blk_trace_get_queue(struct block_device
*bdev
)
1757 if (bdev
->bd_disk
== NULL
)
1760 return bdev_get_queue(bdev
);
1763 static ssize_t
sysfs_blk_trace_attr_show(struct device
*dev
,
1764 struct device_attribute
*attr
,
1767 struct hd_struct
*p
= dev_to_part(dev
);
1768 struct request_queue
*q
;
1769 struct block_device
*bdev
;
1770 ssize_t ret
= -ENXIO
;
1772 bdev
= bdget(part_devt(p
));
1776 q
= blk_trace_get_queue(bdev
);
1780 mutex_lock(&q
->blk_trace_mutex
);
1782 if (attr
== &dev_attr_enable
) {
1783 ret
= sprintf(buf
, "%u\n", !!q
->blk_trace
);
1784 goto out_unlock_bdev
;
1787 if (q
->blk_trace
== NULL
)
1788 ret
= sprintf(buf
, "disabled\n");
1789 else if (attr
== &dev_attr_act_mask
)
1790 ret
= blk_trace_mask2str(buf
, q
->blk_trace
->act_mask
);
1791 else if (attr
== &dev_attr_pid
)
1792 ret
= sprintf(buf
, "%u\n", q
->blk_trace
->pid
);
1793 else if (attr
== &dev_attr_start_lba
)
1794 ret
= sprintf(buf
, "%llu\n", q
->blk_trace
->start_lba
);
1795 else if (attr
== &dev_attr_end_lba
)
1796 ret
= sprintf(buf
, "%llu\n", q
->blk_trace
->end_lba
);
1799 mutex_unlock(&q
->blk_trace_mutex
);
1806 static ssize_t
sysfs_blk_trace_attr_store(struct device
*dev
,
1807 struct device_attribute
*attr
,
1808 const char *buf
, size_t count
)
1810 struct block_device
*bdev
;
1811 struct request_queue
*q
;
1812 struct hd_struct
*p
;
1814 ssize_t ret
= -EINVAL
;
1819 if (attr
== &dev_attr_act_mask
) {
1820 if (kstrtoull(buf
, 0, &value
)) {
1821 /* Assume it is a list of trace category names */
1822 ret
= blk_trace_str2mask(buf
);
1827 } else if (kstrtoull(buf
, 0, &value
))
1832 p
= dev_to_part(dev
);
1833 bdev
= bdget(part_devt(p
));
1837 q
= blk_trace_get_queue(bdev
);
1841 mutex_lock(&q
->blk_trace_mutex
);
1843 if (attr
== &dev_attr_enable
) {
1845 ret
= blk_trace_setup_queue(q
, bdev
);
1847 ret
= blk_trace_remove_queue(q
);
1848 goto out_unlock_bdev
;
1852 if (q
->blk_trace
== NULL
)
1853 ret
= blk_trace_setup_queue(q
, bdev
);
1856 if (attr
== &dev_attr_act_mask
)
1857 q
->blk_trace
->act_mask
= value
;
1858 else if (attr
== &dev_attr_pid
)
1859 q
->blk_trace
->pid
= value
;
1860 else if (attr
== &dev_attr_start_lba
)
1861 q
->blk_trace
->start_lba
= value
;
1862 else if (attr
== &dev_attr_end_lba
)
1863 q
->blk_trace
->end_lba
= value
;
1867 mutex_unlock(&q
->blk_trace_mutex
);
1871 return ret
? ret
: count
;
1874 int blk_trace_init_sysfs(struct device
*dev
)
1876 return sysfs_create_group(&dev
->kobj
, &blk_trace_attr_group
);
1879 void blk_trace_remove_sysfs(struct device
*dev
)
1881 sysfs_remove_group(&dev
->kobj
, &blk_trace_attr_group
);
1884 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1886 #ifdef CONFIG_EVENT_TRACING
1888 void blk_fill_rwbs(char *rwbs
, unsigned int op
, int bytes
)
1892 if (op
& REQ_PREFLUSH
)
1895 switch (op
& REQ_OP_MASK
) {
1897 case REQ_OP_WRITE_SAME
:
1900 case REQ_OP_DISCARD
:
1903 case REQ_OP_SECURE_ERASE
:
1919 if (op
& REQ_RAHEAD
)
1928 EXPORT_SYMBOL_GPL(blk_fill_rwbs
);
1930 #endif /* CONFIG_EVENT_TRACING */