Steven Rostedt (VMware) | 91c1e6b | 2018-08-16 11:19:04 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 2 | /* |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 3 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 4 | * |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 5 | */ |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 6 | #include <linux/kernel.h> |
| 7 | #include <linux/blkdev.h> |
| 8 | #include <linux/blktrace_api.h> |
| 9 | #include <linux/percpu.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 13 | #include <linux/debugfs.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 14 | #include <linux/export.h> |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 15 | #include <linux/time.h> |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 16 | #include <linux/uaccess.h> |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 17 | #include <linux/list.h> |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 18 | #include <linux/blk-cgroup.h> |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 19 | |
Omar Sandoval | 18fbda9 | 2017-01-31 14:53:20 -0800 | [diff] [blame] | 20 | #include "../../block/blk.h" |
| 21 | |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 22 | #include <trace/events/block.h> |
| 23 | |
Frederic Weisbecker | 2db270a | 2009-02-07 20:46:45 +0100 | [diff] [blame] | 24 | #include "trace_output.h" |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 25 | |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 26 | #ifdef CONFIG_BLK_DEV_IO_TRACE |
| 27 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 28 | static unsigned int blktrace_seq __read_mostly = 1; |
| 29 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 30 | static struct trace_array *blk_tr; |
Li Zefan | 5006ea7 | 2009-03-20 09:48:03 +0800 | [diff] [blame] | 31 | static bool blk_tracer_enabled __read_mostly; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 32 | |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 33 | static LIST_HEAD(running_trace_list); |
| 34 | static __cacheline_aligned_in_smp DEFINE_SPINLOCK(running_trace_lock); |
| 35 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 36 | /* Select an alternative, minimalistic output than the original one */ |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 37 | #define TRACE_BLK_OPT_CLASSIC 0x1 |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 38 | #define TRACE_BLK_OPT_CGROUP 0x2 |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 39 | #define TRACE_BLK_OPT_CGNAME 0x4 |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 40 | |
| 41 | static struct tracer_opt blk_tracer_opts[] = { |
| 42 | /* Default disable the minimalistic output */ |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 43 | { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) }, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 44 | #ifdef CONFIG_BLK_CGROUP |
| 45 | { TRACER_OPT(blk_cgroup, TRACE_BLK_OPT_CGROUP) }, |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 46 | { TRACER_OPT(blk_cgname, TRACE_BLK_OPT_CGNAME) }, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 47 | #endif |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 48 | { } |
| 49 | }; |
| 50 | |
| 51 | static struct tracer_flags blk_tracer_flags = { |
| 52 | .val = 0, |
| 53 | .opts = blk_tracer_opts, |
| 54 | }; |
| 55 | |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 56 | /* Global reference count of probes */ |
Jens Axboe | a6da002 | 2017-11-05 09:16:09 -0700 | [diff] [blame] | 57 | static DEFINE_MUTEX(blk_probe_mutex); |
| 58 | static int blk_probes_ref; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 59 | |
Li Zefan | 3c289ba | 2009-03-20 09:48:26 +0800 | [diff] [blame] | 60 | static void blk_register_tracepoints(void); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 61 | static void blk_unregister_tracepoints(void); |
| 62 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 63 | /* |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 64 | * Send out a notify message. |
| 65 | */ |
Jens Axboe | a863055 | 2006-12-04 09:30:58 +0100 | [diff] [blame] | 66 | static void trace_note(struct blk_trace *bt, pid_t pid, int action, |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 67 | const void *data, size_t len, u64 cgid) |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 68 | { |
| 69 | struct blk_io_trace *t; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 70 | struct ring_buffer_event *event = NULL; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 71 | struct trace_buffer *buffer = NULL; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 72 | int pc = 0; |
| 73 | int cpu = smp_processor_id(); |
| 74 | bool blk_tracer = blk_tracer_enabled; |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 75 | ssize_t cgid_len = cgid ? sizeof(cgid) : 0; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 76 | |
| 77 | if (blk_tracer) { |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 78 | buffer = blk_tr->array_buffer.buffer; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 79 | pc = preempt_count(); |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 80 | event = trace_buffer_lock_reserve(buffer, TRACE_BLK, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 81 | sizeof(*t) + len + cgid_len, |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 82 | 0, pc); |
| 83 | if (!event) |
| 84 | return; |
| 85 | t = ring_buffer_event_data(event); |
| 86 | goto record_it; |
| 87 | } |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 88 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 89 | if (!bt->rchan) |
| 90 | return; |
| 91 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 92 | t = relay_reserve(bt->rchan, sizeof(*t) + len + cgid_len); |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 93 | if (t) { |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 94 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; |
Ingo Molnar | 2997c8c | 2008-01-11 13:35:54 +0100 | [diff] [blame] | 95 | t->time = ktime_to_ns(ktime_get()); |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 96 | record_it: |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 97 | t->device = bt->dev; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 98 | t->action = action | (cgid ? __BLK_TN_CGROUP : 0); |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 99 | t->pid = pid; |
| 100 | t->cpu = cpu; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 101 | t->pdu_len = len + cgid_len; |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 102 | if (cgid_len) |
| 103 | memcpy((void *)t + sizeof(*t), &cgid, cgid_len); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 104 | memcpy((void *) t + sizeof(*t) + cgid_len, data, len); |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 105 | |
| 106 | if (blk_tracer) |
Steven Rostedt (Red Hat) | b7f0c95 | 2015-09-25 17:38:44 -0400 | [diff] [blame] | 107 | trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc); |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 108 | } |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 112 | * Send out a notify for this process, if we haven't done so since a trace |
| 113 | * started |
| 114 | */ |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 115 | static void trace_note_tsk(struct task_struct *tsk) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 116 | { |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 117 | unsigned long flags; |
| 118 | struct blk_trace *bt; |
| 119 | |
Jens Axboe | a863055 | 2006-12-04 09:30:58 +0100 | [diff] [blame] | 120 | tsk->btrace_seq = blktrace_seq; |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 121 | spin_lock_irqsave(&running_trace_lock, flags); |
| 122 | list_for_each_entry(bt, &running_trace_list, running_list) { |
| 123 | trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 124 | sizeof(tsk->comm), 0); |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 125 | } |
| 126 | spin_unlock_irqrestore(&running_trace_lock, flags); |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 127 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 128 | |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 129 | static void trace_note_time(struct blk_trace *bt) |
| 130 | { |
Arnd Bergmann | 59a37f8 | 2016-06-17 16:58:26 +0200 | [diff] [blame] | 131 | struct timespec64 now; |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 132 | unsigned long flags; |
| 133 | u32 words[2]; |
| 134 | |
Arnd Bergmann | 59a37f8 | 2016-06-17 16:58:26 +0200 | [diff] [blame] | 135 | /* need to check user space to see if this breaks in y2038 or y2106 */ |
| 136 | ktime_get_real_ts64(&now); |
| 137 | words[0] = (u32)now.tv_sec; |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 138 | words[1] = now.tv_nsec; |
| 139 | |
| 140 | local_irq_save(flags); |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 141 | trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words), 0); |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 142 | local_irq_restore(flags); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 145 | void __trace_note_message(struct blk_trace *bt, struct blkcg *blkcg, |
| 146 | const char *fmt, ...) |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 147 | { |
| 148 | int n; |
| 149 | va_list args; |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 150 | unsigned long flags; |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 151 | char *buf; |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 152 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 153 | if (unlikely(bt->trace_state != Blktrace_running && |
| 154 | !blk_tracer_enabled)) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 155 | return; |
| 156 | |
Tao Ma | 490da40 | 2011-01-19 10:51:44 +0800 | [diff] [blame] | 157 | /* |
| 158 | * If the BLK_TC_NOTIFY action mask isn't set, don't send any note |
| 159 | * message to the trace. |
| 160 | */ |
| 161 | if (!(bt->act_mask & BLK_TC_NOTIFY)) |
| 162 | return; |
| 163 | |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 164 | local_irq_save(flags); |
Shan Wei | d8a0349 | 2012-11-13 09:53:04 +0800 | [diff] [blame] | 165 | buf = this_cpu_ptr(bt->msg_data); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 166 | va_start(args, fmt); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 167 | n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 168 | va_end(args); |
| 169 | |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 170 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP)) |
| 171 | blkcg = NULL; |
| 172 | #ifdef CONFIG_BLK_CGROUP |
Jan Kara | 870c153 | 2020-05-13 18:02:23 +0200 | [diff] [blame] | 173 | trace_note(bt, current->pid, BLK_TN_MESSAGE, buf, n, |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 174 | blkcg ? cgroup_id(blkcg->css.cgroup) : 1); |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 175 | #else |
Jan Kara | 870c153 | 2020-05-13 18:02:23 +0200 | [diff] [blame] | 176 | trace_note(bt, current->pid, BLK_TN_MESSAGE, buf, n, 0); |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 177 | #endif |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 178 | local_irq_restore(flags); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 179 | } |
| 180 | EXPORT_SYMBOL_GPL(__trace_note_message); |
| 181 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 182 | static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector, |
| 183 | pid_t pid) |
| 184 | { |
| 185 | if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0) |
| 186 | return 1; |
Shawn Du | d0deef5 | 2009-04-14 13:58:56 +0800 | [diff] [blame] | 187 | if (sector && (sector < bt->start_lba || sector > bt->end_lba)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 188 | return 1; |
| 189 | if (bt->pid && pid != bt->pid) |
| 190 | return 1; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Data direction bit lookup |
| 197 | */ |
Li Zefan | e4955c9 | 2009-03-24 16:04:37 +0800 | [diff] [blame] | 198 | static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ), |
| 199 | BLK_TC_ACT(BLK_TC_WRITE) }; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 200 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 201 | #define BLK_TC_RAHEAD BLK_TC_AHEAD |
Mike Christie | 28a8f0d | 2016-06-05 14:32:25 -0500 | [diff] [blame] | 202 | #define BLK_TC_PREFLUSH BLK_TC_FLUSH |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 203 | |
David Woodhouse | 35ba8f7 | 2008-08-10 12:33:00 +0100 | [diff] [blame] | 204 | /* The ilog2() calls fall out because they're constant */ |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 205 | #define MASK_TC_BIT(rw, __name) ((rw & REQ_ ## __name) << \ |
| 206 | (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - __REQ_ ## __name)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * The worker for the various blk_add_trace*() types. Fills out a |
| 210 | * blk_io_trace structure and places it in a per-cpu subbuffer. |
| 211 | */ |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 212 | static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 213 | int op, int op_flags, u32 what, int error, int pdu_len, |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 214 | void *pdu_data, u64 cgid) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 215 | { |
| 216 | struct task_struct *tsk = current; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 217 | struct ring_buffer_event *event = NULL; |
Steven Rostedt (VMware) | 1329249 | 2019-12-13 13:58:57 -0500 | [diff] [blame] | 218 | struct trace_buffer *buffer = NULL; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 219 | struct blk_io_trace *t; |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 220 | unsigned long flags = 0; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 221 | unsigned long *sequence; |
| 222 | pid_t pid; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 223 | int cpu, pc = 0; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 224 | bool blk_tracer = blk_tracer_enabled; |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 225 | ssize_t cgid_len = cgid ? sizeof(cgid) : 0; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 226 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 227 | if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 228 | return; |
| 229 | |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 230 | what |= ddir_act[op_is_write(op) ? WRITE : READ]; |
| 231 | what |= MASK_TC_BIT(op_flags, SYNC); |
| 232 | what |= MASK_TC_BIT(op_flags, RAHEAD); |
| 233 | what |= MASK_TC_BIT(op_flags, META); |
Mike Christie | 28a8f0d | 2016-06-05 14:32:25 -0500 | [diff] [blame] | 234 | what |= MASK_TC_BIT(op_flags, PREFLUSH); |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 235 | what |= MASK_TC_BIT(op_flags, FUA); |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 236 | if (op == REQ_OP_DISCARD || op == REQ_OP_SECURE_ERASE) |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 237 | what |= BLK_TC_ACT(BLK_TC_DISCARD); |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 238 | if (op == REQ_OP_FLUSH) |
| 239 | what |= BLK_TC_ACT(BLK_TC_FLUSH); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 240 | if (cgid) |
| 241 | what |= __BLK_TA_CGROUP; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 242 | |
| 243 | pid = tsk->pid; |
Shawn Du | d0deef5 | 2009-04-14 13:58:56 +0800 | [diff] [blame] | 244 | if (act_log_check(bt, what, sector, pid)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 245 | return; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 246 | cpu = raw_smp_processor_id(); |
| 247 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 248 | if (blk_tracer) { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 249 | tracing_record_cmdline(current); |
| 250 | |
Steven Rostedt (VMware) | 1c5eb44 | 2020-01-09 18:53:48 -0500 | [diff] [blame] | 251 | buffer = blk_tr->array_buffer.buffer; |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 252 | pc = preempt_count(); |
Steven Rostedt | e77405a | 2009-09-02 14:17:06 -0400 | [diff] [blame] | 253 | event = trace_buffer_lock_reserve(buffer, TRACE_BLK, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 254 | sizeof(*t) + pdu_len + cgid_len, |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 255 | 0, pc); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 256 | if (!event) |
| 257 | return; |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 258 | t = ring_buffer_event_data(event); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 259 | goto record_it; |
| 260 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 261 | |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 262 | if (unlikely(tsk->btrace_seq != blktrace_seq)) |
| 263 | trace_note_tsk(tsk); |
| 264 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 265 | /* |
| 266 | * A word about the locking here - we disable interrupts to reserve |
| 267 | * some space in the relay per-cpu buffer, to prevent an irq |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 268 | * from coming in and stepping on our toes. |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 269 | */ |
| 270 | local_irq_save(flags); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 271 | t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len + cgid_len); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 272 | if (t) { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 273 | sequence = per_cpu_ptr(bt->sequence, cpu); |
| 274 | |
| 275 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; |
| 276 | t->sequence = ++(*sequence); |
Ingo Molnar | 2997c8c | 2008-01-11 13:35:54 +0100 | [diff] [blame] | 277 | t->time = ktime_to_ns(ktime_get()); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 278 | record_it: |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 279 | /* |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 280 | * These two are not needed in ftrace as they are in the |
| 281 | * generic trace_entry, filled by tracing_generic_entry_update, |
| 282 | * but for the trace_event->bin() synthesizer benefit we do it |
| 283 | * here too. |
| 284 | */ |
| 285 | t->cpu = cpu; |
| 286 | t->pid = pid; |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 287 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 288 | t->sector = sector; |
| 289 | t->bytes = bytes; |
| 290 | t->action = what; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 291 | t->device = bt->dev; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 292 | t->error = error; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 293 | t->pdu_len = pdu_len + cgid_len; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 294 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 295 | if (cgid_len) |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 296 | memcpy((void *)t + sizeof(*t), &cgid, cgid_len); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 297 | if (pdu_len) |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 298 | memcpy((void *)t + sizeof(*t) + cgid_len, pdu_data, pdu_len); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 299 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 300 | if (blk_tracer) { |
Steven Rostedt (Red Hat) | b7f0c95 | 2015-09-25 17:38:44 -0400 | [diff] [blame] | 301 | trace_buffer_unlock_commit(blk_tr, buffer, event, 0, pc); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 302 | return; |
| 303 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | local_irq_restore(flags); |
| 307 | } |
| 308 | |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 309 | static void blk_trace_free(struct blk_trace *bt) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 310 | { |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 311 | debugfs_remove(bt->msg_file); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 312 | debugfs_remove(bt->dropped_file); |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 313 | relay_close(bt->rchan); |
Alan D. Brunelle | 39cbb60 | 2009-08-07 12:01:08 -0400 | [diff] [blame] | 314 | debugfs_remove(bt->dir); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 315 | free_percpu(bt->sequence); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 316 | free_percpu(bt->msg_data); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 317 | kfree(bt); |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 318 | } |
| 319 | |
Jens Axboe | a6da002 | 2017-11-05 09:16:09 -0700 | [diff] [blame] | 320 | static void get_probe_ref(void) |
| 321 | { |
| 322 | mutex_lock(&blk_probe_mutex); |
| 323 | if (++blk_probes_ref == 1) |
| 324 | blk_register_tracepoints(); |
| 325 | mutex_unlock(&blk_probe_mutex); |
| 326 | } |
| 327 | |
| 328 | static void put_probe_ref(void) |
| 329 | { |
| 330 | mutex_lock(&blk_probe_mutex); |
| 331 | if (!--blk_probes_ref) |
| 332 | blk_unregister_tracepoints(); |
| 333 | mutex_unlock(&blk_probe_mutex); |
| 334 | } |
| 335 | |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 336 | static void blk_trace_cleanup(struct blk_trace *bt) |
| 337 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 338 | synchronize_rcu(); |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 339 | blk_trace_free(bt); |
Jens Axboe | a6da002 | 2017-11-05 09:16:09 -0700 | [diff] [blame] | 340 | put_probe_ref(); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 343 | static int __blk_trace_remove(struct request_queue *q) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 344 | { |
| 345 | struct blk_trace *bt; |
| 346 | |
| 347 | bt = xchg(&q->blk_trace, NULL); |
| 348 | if (!bt) |
| 349 | return -EINVAL; |
| 350 | |
Li Zefan | 5554720 | 2009-03-25 17:21:26 +0800 | [diff] [blame] | 351 | if (bt->trace_state != Blktrace_running) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 352 | blk_trace_cleanup(bt); |
| 353 | |
| 354 | return 0; |
| 355 | } |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 356 | |
| 357 | int blk_trace_remove(struct request_queue *q) |
| 358 | { |
| 359 | int ret; |
| 360 | |
| 361 | mutex_lock(&q->blk_trace_mutex); |
| 362 | ret = __blk_trace_remove(q); |
| 363 | mutex_unlock(&q->blk_trace_mutex); |
| 364 | |
| 365 | return ret; |
| 366 | } |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 367 | EXPORT_SYMBOL_GPL(blk_trace_remove); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 368 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 369 | static ssize_t blk_dropped_read(struct file *filp, char __user *buffer, |
| 370 | size_t count, loff_t *ppos) |
| 371 | { |
| 372 | struct blk_trace *bt = filp->private_data; |
| 373 | char buf[16]; |
| 374 | |
| 375 | snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped)); |
| 376 | |
| 377 | return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf)); |
| 378 | } |
| 379 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 380 | static const struct file_operations blk_dropped_fops = { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 381 | .owner = THIS_MODULE, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 382 | .open = simple_open, |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 383 | .read = blk_dropped_read, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 384 | .llseek = default_llseek, |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 385 | }; |
| 386 | |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 387 | static ssize_t blk_msg_write(struct file *filp, const char __user *buffer, |
| 388 | size_t count, loff_t *ppos) |
| 389 | { |
| 390 | char *msg; |
| 391 | struct blk_trace *bt; |
| 392 | |
Li Zefan | 7635b03 | 2009-04-03 15:31:34 +0800 | [diff] [blame] | 393 | if (count >= BLK_TN_MAX_MSG) |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 394 | return -EINVAL; |
| 395 | |
Al Viro | 16e5c1f | 2015-12-24 00:06:05 -0500 | [diff] [blame] | 396 | msg = memdup_user_nul(buffer, count); |
| 397 | if (IS_ERR(msg)) |
| 398 | return PTR_ERR(msg); |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 399 | |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 400 | bt = filp->private_data; |
Shaohua Li | 35fe6d7 | 2017-07-12 11:49:56 -0700 | [diff] [blame] | 401 | __trace_note_message(bt, NULL, "%s", msg); |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 402 | kfree(msg); |
| 403 | |
| 404 | return count; |
| 405 | } |
| 406 | |
| 407 | static const struct file_operations blk_msg_fops = { |
| 408 | .owner = THIS_MODULE, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 409 | .open = simple_open, |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 410 | .write = blk_msg_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 411 | .llseek = noop_llseek, |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 412 | }; |
| 413 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 414 | /* |
| 415 | * Keep track of how many times we encountered a full subbuffer, to aid |
| 416 | * the user space app in telling how many lost events there were. |
| 417 | */ |
| 418 | static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf, |
| 419 | void *prev_subbuf, size_t prev_padding) |
| 420 | { |
| 421 | struct blk_trace *bt; |
| 422 | |
| 423 | if (!relay_buf_full(buf)) |
| 424 | return 1; |
| 425 | |
| 426 | bt = buf->chan->private_data; |
| 427 | atomic_inc(&bt->dropped); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int blk_remove_buf_file_callback(struct dentry *dentry) |
| 432 | { |
| 433 | debugfs_remove(dentry); |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 434 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static struct dentry *blk_create_buf_file_callback(const char *filename, |
| 439 | struct dentry *parent, |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 440 | umode_t mode, |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 441 | struct rchan_buf *buf, |
| 442 | int *is_global) |
| 443 | { |
| 444 | return debugfs_create_file(filename, mode, parent, buf, |
| 445 | &relay_file_operations); |
| 446 | } |
| 447 | |
| 448 | static struct rchan_callbacks blk_relay_callbacks = { |
| 449 | .subbuf_start = blk_subbuf_start_callback, |
| 450 | .create_buf_file = blk_create_buf_file_callback, |
| 451 | .remove_buf_file = blk_remove_buf_file_callback, |
| 452 | }; |
| 453 | |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 454 | static void blk_trace_setup_lba(struct blk_trace *bt, |
| 455 | struct block_device *bdev) |
| 456 | { |
| 457 | struct hd_struct *part = NULL; |
| 458 | |
| 459 | if (bdev) |
| 460 | part = bdev->bd_part; |
| 461 | |
| 462 | if (part) { |
| 463 | bt->start_lba = part->start_sect; |
| 464 | bt->end_lba = part->start_sect + part->nr_sects; |
| 465 | } else { |
| 466 | bt->start_lba = 0; |
| 467 | bt->end_lba = -1ULL; |
| 468 | } |
| 469 | } |
| 470 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 471 | /* |
| 472 | * Setup everything required to start tracing |
| 473 | */ |
Omar Sandoval | a428d31 | 2017-01-31 14:53:19 -0800 | [diff] [blame] | 474 | static int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev, |
| 475 | struct block_device *bdev, |
| 476 | struct blk_user_trace_setup *buts) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 477 | { |
Davidlohr Bueso | cdea01b | 2015-10-30 05:25:59 +0900 | [diff] [blame] | 478 | struct blk_trace *bt = NULL; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 479 | struct dentry *dir = NULL; |
Rasmus Villemoes | ff14417 | 2015-06-25 15:02:28 -0700 | [diff] [blame] | 480 | int ret; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 481 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 482 | if (!buts->buf_size || !buts->buf_nr) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 483 | return -EINVAL; |
| 484 | |
Liu Bo | e1a4132 | 2018-06-29 09:56:08 +0800 | [diff] [blame] | 485 | if (!blk_debugfs_root) |
| 486 | return -ENOENT; |
| 487 | |
Jens Axboe | 0497b34 | 2008-10-01 16:16:25 +0200 | [diff] [blame] | 488 | strncpy(buts->name, name, BLKTRACE_BDEV_SIZE); |
| 489 | buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0'; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 490 | |
| 491 | /* |
| 492 | * some device names have larger paths - convert the slashes |
| 493 | * to underscores for this to work as expected |
| 494 | */ |
Rasmus Villemoes | ff14417 | 2015-06-25 15:02:28 -0700 | [diff] [blame] | 495 | strreplace(buts->name, '/', '_'); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 496 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 497 | bt = kzalloc(sizeof(*bt), GFP_KERNEL); |
| 498 | if (!bt) |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 499 | return -ENOMEM; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 500 | |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 501 | ret = -ENOMEM; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 502 | bt->sequence = alloc_percpu(unsigned long); |
| 503 | if (!bt->sequence) |
| 504 | goto err; |
| 505 | |
Ingo Molnar | f0ef039 | 2009-03-06 16:44:14 +0100 | [diff] [blame] | 506 | bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char)); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 507 | if (!bt->msg_data) |
| 508 | goto err; |
| 509 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 510 | ret = -ENOENT; |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 511 | |
Omar Sandoval | 6ac9311 | 2017-01-31 14:53:22 -0800 | [diff] [blame] | 512 | dir = debugfs_lookup(buts->name, blk_debugfs_root); |
| 513 | if (!dir) |
| 514 | bt->dir = dir = debugfs_create_dir(buts->name, blk_debugfs_root); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 515 | |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 516 | bt->dev = dev; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 517 | atomic_set(&bt->dropped, 0); |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 518 | INIT_LIST_HEAD(&bt->running_list); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 519 | |
| 520 | ret = -EIO; |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 521 | bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, |
| 522 | &blk_dropped_fops); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 523 | |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 524 | bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops); |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 525 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 526 | bt->rchan = relay_open("trace", dir, buts->buf_size, |
| 527 | buts->buf_nr, &blk_relay_callbacks, bt); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 528 | if (!bt->rchan) |
| 529 | goto err; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 530 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 531 | bt->act_mask = buts->act_mask; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 532 | if (!bt->act_mask) |
| 533 | bt->act_mask = (u16) -1; |
| 534 | |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 535 | blk_trace_setup_lba(bt, bdev); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 536 | |
Shawn Du | d0deef5 | 2009-04-14 13:58:56 +0800 | [diff] [blame] | 537 | /* overwrite with user settings */ |
| 538 | if (buts->start_lba) |
| 539 | bt->start_lba = buts->start_lba; |
| 540 | if (buts->end_lba) |
| 541 | bt->end_lba = buts->end_lba; |
| 542 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 543 | bt->pid = buts->pid; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 544 | bt->trace_state = Blktrace_setup; |
| 545 | |
| 546 | ret = -EBUSY; |
Davidlohr Bueso | cdea01b | 2015-10-30 05:25:59 +0900 | [diff] [blame] | 547 | if (cmpxchg(&q->blk_trace, NULL, bt)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 548 | goto err; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 549 | |
Jens Axboe | a6da002 | 2017-11-05 09:16:09 -0700 | [diff] [blame] | 550 | get_probe_ref(); |
Li Zefan | cbe2829 | 2009-03-20 09:48:47 +0800 | [diff] [blame] | 551 | |
Omar Sandoval | 6ac9311 | 2017-01-31 14:53:22 -0800 | [diff] [blame] | 552 | ret = 0; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 553 | err: |
Omar Sandoval | 6ac9311 | 2017-01-31 14:53:22 -0800 | [diff] [blame] | 554 | if (dir && !bt->dir) |
| 555 | dput(dir); |
| 556 | if (ret) |
| 557 | blk_trace_free(bt); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 558 | return ret; |
| 559 | } |
| 560 | |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 561 | static int __blk_trace_setup(struct request_queue *q, char *name, dev_t dev, |
| 562 | struct block_device *bdev, char __user *arg) |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 563 | { |
| 564 | struct blk_user_trace_setup buts; |
| 565 | int ret; |
| 566 | |
| 567 | ret = copy_from_user(&buts, arg, sizeof(buts)); |
| 568 | if (ret) |
| 569 | return -EFAULT; |
| 570 | |
Shawn Du | d0deef5 | 2009-04-14 13:58:56 +0800 | [diff] [blame] | 571 | ret = do_blk_trace_setup(q, name, dev, bdev, &buts); |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 572 | if (ret) |
| 573 | return ret; |
| 574 | |
Dmitry Monakhov | 9a8c28c | 2010-02-27 20:35:13 +0300 | [diff] [blame] | 575 | if (copy_to_user(arg, &buts, sizeof(buts))) { |
Jens Axboe | 2967acb | 2017-11-19 11:52:55 -0700 | [diff] [blame] | 576 | __blk_trace_remove(q); |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 577 | return -EFAULT; |
Dmitry Monakhov | 9a8c28c | 2010-02-27 20:35:13 +0300 | [diff] [blame] | 578 | } |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 579 | return 0; |
| 580 | } |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 581 | |
| 582 | int blk_trace_setup(struct request_queue *q, char *name, dev_t dev, |
| 583 | struct block_device *bdev, |
| 584 | char __user *arg) |
| 585 | { |
| 586 | int ret; |
| 587 | |
| 588 | mutex_lock(&q->blk_trace_mutex); |
| 589 | ret = __blk_trace_setup(q, name, dev, bdev, arg); |
| 590 | mutex_unlock(&q->blk_trace_mutex); |
| 591 | |
| 592 | return ret; |
| 593 | } |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 594 | EXPORT_SYMBOL_GPL(blk_trace_setup); |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 595 | |
Arnd Bergmann | 62c2a7d | 2010-07-07 16:51:26 +0200 | [diff] [blame] | 596 | #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64) |
| 597 | static int compat_blk_trace_setup(struct request_queue *q, char *name, |
| 598 | dev_t dev, struct block_device *bdev, |
| 599 | char __user *arg) |
| 600 | { |
| 601 | struct blk_user_trace_setup buts; |
| 602 | struct compat_blk_user_trace_setup cbuts; |
| 603 | int ret; |
| 604 | |
| 605 | if (copy_from_user(&cbuts, arg, sizeof(cbuts))) |
| 606 | return -EFAULT; |
| 607 | |
| 608 | buts = (struct blk_user_trace_setup) { |
| 609 | .act_mask = cbuts.act_mask, |
| 610 | .buf_size = cbuts.buf_size, |
| 611 | .buf_nr = cbuts.buf_nr, |
| 612 | .start_lba = cbuts.start_lba, |
| 613 | .end_lba = cbuts.end_lba, |
| 614 | .pid = cbuts.pid, |
| 615 | }; |
Arnd Bergmann | 62c2a7d | 2010-07-07 16:51:26 +0200 | [diff] [blame] | 616 | |
| 617 | ret = do_blk_trace_setup(q, name, dev, bdev, &buts); |
| 618 | if (ret) |
| 619 | return ret; |
| 620 | |
Chen Gang | f8c5e94 | 2013-11-03 22:23:39 +0800 | [diff] [blame] | 621 | if (copy_to_user(arg, &buts.name, ARRAY_SIZE(buts.name))) { |
Jens Axboe | 2967acb | 2017-11-19 11:52:55 -0700 | [diff] [blame] | 622 | __blk_trace_remove(q); |
Arnd Bergmann | 62c2a7d | 2010-07-07 16:51:26 +0200 | [diff] [blame] | 623 | return -EFAULT; |
| 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | #endif |
| 629 | |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 630 | static int __blk_trace_startstop(struct request_queue *q, int start) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 631 | { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 632 | int ret; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 633 | struct blk_trace *bt; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 634 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 635 | bt = rcu_dereference_protected(q->blk_trace, |
| 636 | lockdep_is_held(&q->blk_trace_mutex)); |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 637 | if (bt == NULL) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 638 | return -EINVAL; |
| 639 | |
| 640 | /* |
| 641 | * For starting a trace, we can transition from a setup or stopped |
| 642 | * trace. For stopping a trace, the state must be running |
| 643 | */ |
| 644 | ret = -EINVAL; |
| 645 | if (start) { |
| 646 | if (bt->trace_state == Blktrace_setup || |
| 647 | bt->trace_state == Blktrace_stopped) { |
| 648 | blktrace_seq++; |
| 649 | smp_mb(); |
| 650 | bt->trace_state = Blktrace_running; |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 651 | spin_lock_irq(&running_trace_lock); |
| 652 | list_add(&bt->running_list, &running_trace_list); |
| 653 | spin_unlock_irq(&running_trace_lock); |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 654 | |
| 655 | trace_note_time(bt); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 656 | ret = 0; |
| 657 | } |
| 658 | } else { |
| 659 | if (bt->trace_state == Blktrace_running) { |
| 660 | bt->trace_state = Blktrace_stopped; |
Jan Kara | a404d55 | 2013-09-17 22:30:31 +0200 | [diff] [blame] | 661 | spin_lock_irq(&running_trace_lock); |
| 662 | list_del_init(&bt->running_list); |
| 663 | spin_unlock_irq(&running_trace_lock); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 664 | relay_flush(bt->rchan); |
| 665 | ret = 0; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return ret; |
| 670 | } |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 671 | |
| 672 | int blk_trace_startstop(struct request_queue *q, int start) |
| 673 | { |
| 674 | int ret; |
| 675 | |
| 676 | mutex_lock(&q->blk_trace_mutex); |
| 677 | ret = __blk_trace_startstop(q, start); |
| 678 | mutex_unlock(&q->blk_trace_mutex); |
| 679 | |
| 680 | return ret; |
| 681 | } |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 682 | EXPORT_SYMBOL_GPL(blk_trace_startstop); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 683 | |
Waiman Long | 5acb3cc | 2017-09-20 13:12:20 -0600 | [diff] [blame] | 684 | /* |
| 685 | * When reading or writing the blktrace sysfs files, the references to the |
| 686 | * opened sysfs or device files should prevent the underlying block device |
| 687 | * from being removed. So no further delete protection is really needed. |
| 688 | */ |
| 689 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 690 | /** |
| 691 | * blk_trace_ioctl: - handle the ioctls associated with tracing |
| 692 | * @bdev: the block device |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 693 | * @cmd: the ioctl cmd |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 694 | * @arg: the argument data, if any |
| 695 | * |
| 696 | **/ |
| 697 | int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg) |
| 698 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 699 | struct request_queue *q; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 700 | int ret, start = 0; |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 701 | char b[BDEVNAME_SIZE]; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 702 | |
| 703 | q = bdev_get_queue(bdev); |
| 704 | if (!q) |
| 705 | return -ENXIO; |
| 706 | |
Waiman Long | 5acb3cc | 2017-09-20 13:12:20 -0600 | [diff] [blame] | 707 | mutex_lock(&q->blk_trace_mutex); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 708 | |
| 709 | switch (cmd) { |
| 710 | case BLKTRACESETUP: |
Jean Delvare | f36f21e | 2008-05-12 14:02:33 -0700 | [diff] [blame] | 711 | bdevname(bdev, b); |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 712 | ret = __blk_trace_setup(q, b, bdev->bd_dev, bdev, arg); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 713 | break; |
Arnd Bergmann | 62c2a7d | 2010-07-07 16:51:26 +0200 | [diff] [blame] | 714 | #if defined(CONFIG_COMPAT) && defined(CONFIG_X86_64) |
| 715 | case BLKTRACESETUP32: |
| 716 | bdevname(bdev, b); |
| 717 | ret = compat_blk_trace_setup(q, b, bdev->bd_dev, bdev, arg); |
| 718 | break; |
| 719 | #endif |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 720 | case BLKTRACESTART: |
| 721 | start = 1; |
Mathieu Malaterre | f6d85f0 | 2019-01-14 21:31:13 +0100 | [diff] [blame] | 722 | /* fall through */ |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 723 | case BLKTRACESTOP: |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 724 | ret = __blk_trace_startstop(q, start); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 725 | break; |
| 726 | case BLKTRACETEARDOWN: |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 727 | ret = __blk_trace_remove(q); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 728 | break; |
| 729 | default: |
| 730 | ret = -ENOTTY; |
| 731 | break; |
| 732 | } |
| 733 | |
Waiman Long | 5acb3cc | 2017-09-20 13:12:20 -0600 | [diff] [blame] | 734 | mutex_unlock(&q->blk_trace_mutex); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * blk_trace_shutdown: - stop and cleanup trace structures |
| 740 | * @q: the request queue associated with the device |
| 741 | * |
| 742 | **/ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 743 | void blk_trace_shutdown(struct request_queue *q) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 744 | { |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 745 | mutex_lock(&q->blk_trace_mutex); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 746 | if (rcu_dereference_protected(q->blk_trace, |
| 747 | lockdep_is_held(&q->blk_trace_mutex))) { |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 748 | __blk_trace_startstop(q, 0); |
| 749 | __blk_trace_remove(q); |
Alexey Dobriyan | 6c5c934 | 2006-09-29 01:59:40 -0700 | [diff] [blame] | 750 | } |
Jens Axboe | 1f2cac1 | 2017-11-05 09:13:48 -0700 | [diff] [blame] | 751 | |
| 752 | mutex_unlock(&q->blk_trace_mutex); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 753 | } |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 754 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 755 | #ifdef CONFIG_BLK_CGROUP |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 756 | static u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio) |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 757 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 758 | struct blk_trace *bt; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 759 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 760 | /* We don't use the 'bt' value here except as an optimization... */ |
| 761 | bt = rcu_dereference_protected(q->blk_trace, 1); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 762 | if (!bt || !(blk_tracer_flags.val & TRACE_BLK_OPT_CGROUP)) |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 763 | return 0; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 764 | |
Dennis Zhou | db6638d | 2018-12-05 12:10:35 -0500 | [diff] [blame] | 765 | if (!bio->bi_blkg) |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 766 | return 0; |
Tejun Heo | 7432103 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 767 | return cgroup_id(bio_blkcg(bio)->css.cgroup); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 768 | } |
| 769 | #else |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 770 | u64 blk_trace_bio_get_cgid(struct request_queue *q, struct bio *bio) |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 771 | { |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 772 | return 0; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 773 | } |
| 774 | #endif |
| 775 | |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 776 | static u64 |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 777 | blk_trace_request_get_cgid(struct request_queue *q, struct request *rq) |
| 778 | { |
| 779 | if (!rq->bio) |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 780 | return 0; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 781 | /* Use the first bio */ |
| 782 | return blk_trace_bio_get_cgid(q, rq->bio); |
| 783 | } |
| 784 | |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 785 | /* |
| 786 | * blktrace probes |
| 787 | */ |
| 788 | |
| 789 | /** |
| 790 | * blk_add_trace_rq - Add a trace for a request oriented action |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 791 | * @rq: the source request |
Christoph Hellwig | caf7df1 | 2017-04-20 16:03:16 +0200 | [diff] [blame] | 792 | * @error: return status to log |
Roman Pen | af5040d | 2014-03-04 23:13:10 +0900 | [diff] [blame] | 793 | * @nr_bytes: number of completed bytes |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 794 | * @what: the action |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 795 | * @cgid: the cgroup info |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 796 | * |
| 797 | * Description: |
| 798 | * Records an action against a request. Will log the bio offset + size. |
| 799 | * |
| 800 | **/ |
Christoph Hellwig | caf7df1 | 2017-04-20 16:03:16 +0200 | [diff] [blame] | 801 | static void blk_add_trace_rq(struct request *rq, int error, |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 802 | unsigned int nr_bytes, u32 what, u64 cgid) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 803 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 804 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 805 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 806 | rcu_read_lock(); |
| 807 | bt = rcu_dereference(rq->q->blk_trace); |
| 808 | if (likely(!bt)) { |
| 809 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 810 | return; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 811 | } |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 812 | |
Christoph Hellwig | 57292b5 | 2017-01-31 16:57:29 +0100 | [diff] [blame] | 813 | if (blk_rq_is_passthrough(rq)) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 814 | what |= BLK_TC_ACT(BLK_TC_PC); |
Christoph Hellwig | 48b77ad | 2017-01-27 09:35:54 +0100 | [diff] [blame] | 815 | else |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 816 | what |= BLK_TC_ACT(BLK_TC_FS); |
Christoph Hellwig | 48b77ad | 2017-01-27 09:35:54 +0100 | [diff] [blame] | 817 | |
| 818 | __blk_add_trace(bt, blk_rq_trace_sector(rq), nr_bytes, req_op(rq), |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 819 | rq->cmd_flags, what, error, 0, NULL, cgid); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 820 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 823 | static void blk_add_trace_rq_insert(void *ignore, |
| 824 | struct request_queue *q, struct request *rq) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 825 | { |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 826 | blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_INSERT, |
| 827 | blk_trace_request_get_cgid(q, rq)); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 828 | } |
| 829 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 830 | static void blk_add_trace_rq_issue(void *ignore, |
| 831 | struct request_queue *q, struct request *rq) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 832 | { |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 833 | blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_ISSUE, |
| 834 | blk_trace_request_get_cgid(q, rq)); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 835 | } |
| 836 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 837 | static void blk_add_trace_rq_requeue(void *ignore, |
| 838 | struct request_queue *q, |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 839 | struct request *rq) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 840 | { |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 841 | blk_add_trace_rq(rq, 0, blk_rq_bytes(rq), BLK_TA_REQUEUE, |
| 842 | blk_trace_request_get_cgid(q, rq)); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Christoph Hellwig | caf7df1 | 2017-04-20 16:03:16 +0200 | [diff] [blame] | 845 | static void blk_add_trace_rq_complete(void *ignore, struct request *rq, |
| 846 | int error, unsigned int nr_bytes) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 847 | { |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 848 | blk_add_trace_rq(rq, error, nr_bytes, BLK_TA_COMPLETE, |
| 849 | blk_trace_request_get_cgid(rq->q, rq)); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /** |
| 853 | * blk_add_trace_bio - Add a trace for a bio oriented action |
| 854 | * @q: queue the io is for |
| 855 | * @bio: the source bio |
| 856 | * @what: the action |
Jens Axboe | 797a455 | 2011-01-10 09:06:44 +0100 | [diff] [blame] | 857 | * @error: error, if any |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 858 | * |
| 859 | * Description: |
| 860 | * Records an action against a bio. Will log the bio offset + size. |
| 861 | * |
| 862 | **/ |
| 863 | static void blk_add_trace_bio(struct request_queue *q, struct bio *bio, |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 864 | u32 what, int error) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 865 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 866 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 867 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 868 | rcu_read_lock(); |
| 869 | bt = rcu_dereference(q->blk_trace); |
| 870 | if (likely(!bt)) { |
| 871 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 872 | return; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 873 | } |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 874 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 875 | __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size, |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 876 | bio_op(bio), bio->bi_opf, what, error, 0, NULL, |
| 877 | blk_trace_bio_get_cgid(q, bio)); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 878 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 881 | static void blk_add_trace_bio_bounce(void *ignore, |
| 882 | struct request_queue *q, struct bio *bio) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 883 | { |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 884 | blk_add_trace_bio(q, bio, BLK_TA_BOUNCE, 0); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 885 | } |
| 886 | |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 887 | static void blk_add_trace_bio_complete(void *ignore, |
Christoph Hellwig | d24de76a | 2020-06-03 07:14:43 +0200 | [diff] [blame] | 888 | struct request_queue *q, struct bio *bio) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 889 | { |
Christoph Hellwig | d24de76a | 2020-06-03 07:14:43 +0200 | [diff] [blame] | 890 | blk_add_trace_bio(q, bio, BLK_TA_COMPLETE, |
| 891 | blk_status_to_errno(bio->bi_status)); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 892 | } |
| 893 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 894 | static void blk_add_trace_bio_backmerge(void *ignore, |
| 895 | struct request_queue *q, |
Tejun Heo | 8c1cf6b | 2013-01-11 13:06:34 -0800 | [diff] [blame] | 896 | struct request *rq, |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 897 | struct bio *bio) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 898 | { |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 899 | blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE, 0); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 902 | static void blk_add_trace_bio_frontmerge(void *ignore, |
| 903 | struct request_queue *q, |
Tejun Heo | 8c1cf6b | 2013-01-11 13:06:34 -0800 | [diff] [blame] | 904 | struct request *rq, |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 905 | struct bio *bio) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 906 | { |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 907 | blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE, 0); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 908 | } |
| 909 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 910 | static void blk_add_trace_bio_queue(void *ignore, |
| 911 | struct request_queue *q, struct bio *bio) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 912 | { |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 913 | blk_add_trace_bio(q, bio, BLK_TA_QUEUE, 0); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 914 | } |
| 915 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 916 | static void blk_add_trace_getrq(void *ignore, |
| 917 | struct request_queue *q, |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 918 | struct bio *bio, int rw) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 919 | { |
| 920 | if (bio) |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 921 | blk_add_trace_bio(q, bio, BLK_TA_GETRQ, 0); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 922 | else { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 923 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 924 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 925 | rcu_read_lock(); |
| 926 | bt = rcu_dereference(q->blk_trace); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 927 | if (bt) |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 928 | __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_GETRQ, 0, 0, |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 929 | NULL, 0); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 930 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
| 934 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 935 | static void blk_add_trace_sleeprq(void *ignore, |
| 936 | struct request_queue *q, |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 937 | struct bio *bio, int rw) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 938 | { |
| 939 | if (bio) |
Marcos Paulo de Souza | 1690102 | 2017-11-19 16:48:13 -0200 | [diff] [blame] | 940 | blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ, 0); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 941 | else { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 942 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 943 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 944 | rcu_read_lock(); |
| 945 | bt = rcu_dereference(q->blk_trace); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 946 | if (bt) |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 947 | __blk_add_trace(bt, 0, 0, rw, 0, BLK_TA_SLEEPRQ, |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 948 | 0, 0, NULL, 0); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 949 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 953 | static void blk_add_trace_plug(void *ignore, struct request_queue *q) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 954 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 955 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 956 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 957 | rcu_read_lock(); |
| 958 | bt = rcu_dereference(q->blk_trace); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 959 | if (bt) |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 960 | __blk_add_trace(bt, 0, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL, 0); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 961 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Jens Axboe | 49cac01 | 2011-04-16 13:51:05 +0200 | [diff] [blame] | 964 | static void blk_add_trace_unplug(void *ignore, struct request_queue *q, |
| 965 | unsigned int depth, bool explicit) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 966 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 967 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 968 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 969 | rcu_read_lock(); |
| 970 | bt = rcu_dereference(q->blk_trace); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 971 | if (bt) { |
Jens Axboe | 94b5eb2 | 2011-04-12 10:12:19 +0200 | [diff] [blame] | 972 | __be64 rpdu = cpu_to_be64(depth); |
Jens Axboe | 49cac01 | 2011-04-16 13:51:05 +0200 | [diff] [blame] | 973 | u32 what; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 974 | |
Jens Axboe | 49cac01 | 2011-04-16 13:51:05 +0200 | [diff] [blame] | 975 | if (explicit) |
| 976 | what = BLK_TA_UNPLUG_IO; |
| 977 | else |
| 978 | what = BLK_TA_UNPLUG_TIMER; |
| 979 | |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 980 | __blk_add_trace(bt, 0, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu, 0); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 981 | } |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 982 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 983 | } |
| 984 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 985 | static void blk_add_trace_split(void *ignore, |
| 986 | struct request_queue *q, struct bio *bio, |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 987 | unsigned int pdu) |
| 988 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 989 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 990 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 991 | rcu_read_lock(); |
| 992 | bt = rcu_dereference(q->blk_trace); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 993 | if (bt) { |
| 994 | __be64 rpdu = cpu_to_be64(pdu); |
| 995 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 996 | __blk_add_trace(bt, bio->bi_iter.bi_sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 997 | bio->bi_iter.bi_size, bio_op(bio), bio->bi_opf, |
Chaitanya Kulkarni | 48bc3cd | 2020-06-04 00:13:28 -0700 | [diff] [blame] | 998 | BLK_TA_SPLIT, |
| 999 | blk_status_to_errno(bio->bi_status), |
| 1000 | sizeof(rpdu), &rpdu, |
| 1001 | blk_trace_bio_get_cgid(q, bio)); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1002 | } |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1003 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /** |
Mike Snitzer | d07335e | 2010-11-16 12:52:38 +0100 | [diff] [blame] | 1007 | * blk_add_trace_bio_remap - Add a trace for a bio-remap operation |
Randy Dunlap | 546cf44 | 2010-05-29 11:45:07 -0700 | [diff] [blame] | 1008 | * @ignore: trace callback data parameter (not used) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1009 | * @q: queue the io is for |
| 1010 | * @bio: the source bio |
| 1011 | * @dev: target device |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame] | 1012 | * @from: source sector |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1013 | * |
| 1014 | * Description: |
| 1015 | * Device mapper or raid target sometimes need to split a bio because |
| 1016 | * it spans a stripe (or similar). Add a trace for that action. |
| 1017 | * |
| 1018 | **/ |
Mike Snitzer | d07335e | 2010-11-16 12:52:38 +0100 | [diff] [blame] | 1019 | static void blk_add_trace_bio_remap(void *ignore, |
| 1020 | struct request_queue *q, struct bio *bio, |
| 1021 | dev_t dev, sector_t from) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1022 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1023 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1024 | struct blk_io_trace_remap r; |
| 1025 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1026 | rcu_read_lock(); |
| 1027 | bt = rcu_dereference(q->blk_trace); |
| 1028 | if (likely(!bt)) { |
| 1029 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1030 | return; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1031 | } |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1032 | |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame] | 1033 | r.device_from = cpu_to_be32(dev); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1034 | r.device_to = cpu_to_be32(bio_dev(bio)); |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame] | 1035 | r.sector_from = cpu_to_be64(from); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1036 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1037 | __blk_add_trace(bt, bio->bi_iter.bi_sector, bio->bi_iter.bi_size, |
Chaitanya Kulkarni | 48bc3cd | 2020-06-04 00:13:28 -0700 | [diff] [blame] | 1038 | bio_op(bio), bio->bi_opf, BLK_TA_REMAP, |
| 1039 | blk_status_to_errno(bio->bi_status), |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1040 | sizeof(r), &r, blk_trace_bio_get_cgid(q, bio)); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1041 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /** |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1045 | * blk_add_trace_rq_remap - Add a trace for a request-remap operation |
Randy Dunlap | 546cf44 | 2010-05-29 11:45:07 -0700 | [diff] [blame] | 1046 | * @ignore: trace callback data parameter (not used) |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1047 | * @q: queue the io is for |
| 1048 | * @rq: the source request |
| 1049 | * @dev: target device |
| 1050 | * @from: source sector |
| 1051 | * |
| 1052 | * Description: |
| 1053 | * Device mapper remaps request to other devices. |
| 1054 | * Add a trace for that action. |
| 1055 | * |
| 1056 | **/ |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1057 | static void blk_add_trace_rq_remap(void *ignore, |
| 1058 | struct request_queue *q, |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1059 | struct request *rq, dev_t dev, |
| 1060 | sector_t from) |
| 1061 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1062 | struct blk_trace *bt; |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1063 | struct blk_io_trace_remap r; |
| 1064 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1065 | rcu_read_lock(); |
| 1066 | bt = rcu_dereference(q->blk_trace); |
| 1067 | if (likely(!bt)) { |
| 1068 | rcu_read_unlock(); |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1069 | return; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1070 | } |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1071 | |
| 1072 | r.device_from = cpu_to_be32(dev); |
| 1073 | r.device_to = cpu_to_be32(disk_devt(rq->rq_disk)); |
| 1074 | r.sector_from = cpu_to_be64(from); |
| 1075 | |
| 1076 | __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), |
Christoph Hellwig | caf7df1 | 2017-04-20 16:03:16 +0200 | [diff] [blame] | 1077 | rq_data_dir(rq), 0, BLK_TA_REMAP, 0, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1078 | sizeof(r), &r, blk_trace_request_get_cgid(q, rq)); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1079 | rcu_read_unlock(); |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | /** |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1083 | * blk_add_driver_data - Add binary message with driver-specific data |
| 1084 | * @q: queue the io is for |
| 1085 | * @rq: io request |
| 1086 | * @data: driver-specific data |
| 1087 | * @len: length of driver-specific data |
| 1088 | * |
| 1089 | * Description: |
| 1090 | * Some drivers might want to write driver-specific data per request. |
| 1091 | * |
| 1092 | **/ |
| 1093 | void blk_add_driver_data(struct request_queue *q, |
| 1094 | struct request *rq, |
| 1095 | void *data, size_t len) |
| 1096 | { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1097 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1098 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1099 | rcu_read_lock(); |
| 1100 | bt = rcu_dereference(q->blk_trace); |
| 1101 | if (likely(!bt)) { |
| 1102 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1103 | return; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1104 | } |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1105 | |
Christoph Hellwig | 48b77ad | 2017-01-27 09:35:54 +0100 | [diff] [blame] | 1106 | __blk_add_trace(bt, blk_rq_trace_sector(rq), blk_rq_bytes(rq), 0, 0, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1107 | BLK_TA_DRV_DATA, 0, len, data, |
| 1108 | blk_trace_request_get_cgid(q, rq)); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1109 | rcu_read_unlock(); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1110 | } |
| 1111 | EXPORT_SYMBOL_GPL(blk_add_driver_data); |
| 1112 | |
Li Zefan | 3c289ba | 2009-03-20 09:48:26 +0800 | [diff] [blame] | 1113 | static void blk_register_tracepoints(void) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1114 | { |
| 1115 | int ret; |
| 1116 | |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1117 | ret = register_trace_block_rq_insert(blk_add_trace_rq_insert, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1118 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1119 | ret = register_trace_block_rq_issue(blk_add_trace_rq_issue, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1120 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1121 | ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1122 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1123 | ret = register_trace_block_rq_complete(blk_add_trace_rq_complete, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1124 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1125 | ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1126 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1127 | ret = register_trace_block_bio_complete(blk_add_trace_bio_complete, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1128 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1129 | ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1130 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1131 | ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1132 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1133 | ret = register_trace_block_bio_queue(blk_add_trace_bio_queue, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1134 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1135 | ret = register_trace_block_getrq(blk_add_trace_getrq, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1136 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1137 | ret = register_trace_block_sleeprq(blk_add_trace_sleeprq, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1138 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1139 | ret = register_trace_block_plug(blk_add_trace_plug, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1140 | WARN_ON(ret); |
Jens Axboe | 49cac01 | 2011-04-16 13:51:05 +0200 | [diff] [blame] | 1141 | ret = register_trace_block_unplug(blk_add_trace_unplug, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1142 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1143 | ret = register_trace_block_split(blk_add_trace_split, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1144 | WARN_ON(ret); |
Mike Snitzer | d07335e | 2010-11-16 12:52:38 +0100 | [diff] [blame] | 1145 | ret = register_trace_block_bio_remap(blk_add_trace_bio_remap, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1146 | WARN_ON(ret); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1147 | ret = register_trace_block_rq_remap(blk_add_trace_rq_remap, NULL); |
Jun'ichi Nomura | b0da3f0 | 2009-10-01 21:16:13 +0200 | [diff] [blame] | 1148 | WARN_ON(ret); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | static void blk_unregister_tracepoints(void) |
| 1152 | { |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1153 | unregister_trace_block_rq_remap(blk_add_trace_rq_remap, NULL); |
Mike Snitzer | d07335e | 2010-11-16 12:52:38 +0100 | [diff] [blame] | 1154 | unregister_trace_block_bio_remap(blk_add_trace_bio_remap, NULL); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1155 | unregister_trace_block_split(blk_add_trace_split, NULL); |
Jens Axboe | 49cac01 | 2011-04-16 13:51:05 +0200 | [diff] [blame] | 1156 | unregister_trace_block_unplug(blk_add_trace_unplug, NULL); |
Steven Rostedt | 38516ab | 2010-04-20 17:04:50 -0400 | [diff] [blame] | 1157 | unregister_trace_block_plug(blk_add_trace_plug, NULL); |
| 1158 | unregister_trace_block_sleeprq(blk_add_trace_sleeprq, NULL); |
| 1159 | unregister_trace_block_getrq(blk_add_trace_getrq, NULL); |
| 1160 | unregister_trace_block_bio_queue(blk_add_trace_bio_queue, NULL); |
| 1161 | unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge, NULL); |
| 1162 | unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge, NULL); |
| 1163 | unregister_trace_block_bio_complete(blk_add_trace_bio_complete, NULL); |
| 1164 | unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce, NULL); |
| 1165 | unregister_trace_block_rq_complete(blk_add_trace_rq_complete, NULL); |
| 1166 | unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue, NULL); |
| 1167 | unregister_trace_block_rq_issue(blk_add_trace_rq_issue, NULL); |
| 1168 | unregister_trace_block_rq_insert(blk_add_trace_rq_insert, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 1169 | |
| 1170 | tracepoint_synchronize_unregister(); |
| 1171 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1172 | |
| 1173 | /* |
| 1174 | * struct blk_io_tracer formatting routines |
| 1175 | */ |
| 1176 | |
| 1177 | static void fill_rwbs(char *rwbs, const struct blk_io_trace *t) |
| 1178 | { |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1179 | int i = 0; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 1180 | int tc = t->action >> BLK_TC_SHIFT; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1181 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1182 | if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) { |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1183 | rwbs[i++] = 'N'; |
| 1184 | goto out; |
| 1185 | } |
| 1186 | |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1187 | if (tc & BLK_TC_FLUSH) |
| 1188 | rwbs[i++] = 'F'; |
| 1189 | |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 1190 | if (tc & BLK_TC_DISCARD) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1191 | rwbs[i++] = 'D'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 1192 | else if (tc & BLK_TC_WRITE) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1193 | rwbs[i++] = 'W'; |
| 1194 | else if (t->bytes) |
| 1195 | rwbs[i++] = 'R'; |
| 1196 | else |
| 1197 | rwbs[i++] = 'N'; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1198 | |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1199 | if (tc & BLK_TC_FUA) |
| 1200 | rwbs[i++] = 'F'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 1201 | if (tc & BLK_TC_AHEAD) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1202 | rwbs[i++] = 'A'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 1203 | if (tc & BLK_TC_SYNC) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1204 | rwbs[i++] = 'S'; |
Li Zefan | 6579634 | 2009-03-24 16:05:06 +0800 | [diff] [blame] | 1205 | if (tc & BLK_TC_META) |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1206 | rwbs[i++] = 'M'; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1207 | out: |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1208 | rwbs[i] = '\0'; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | static inline |
| 1212 | const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent) |
| 1213 | { |
| 1214 | return (const struct blk_io_trace *)ent; |
| 1215 | } |
| 1216 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1217 | static inline const void *pdu_start(const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1218 | { |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1219 | return (void *)(te_blk_io_trace(ent) + 1) + (has_cg ? sizeof(u64) : 0); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1222 | static inline u64 t_cgid(const struct trace_entry *ent) |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1223 | { |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1224 | return *(u64 *)(te_blk_io_trace(ent) + 1); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | static inline int pdu_real_len(const struct trace_entry *ent, bool has_cg) |
| 1228 | { |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1229 | return te_blk_io_trace(ent)->pdu_len - (has_cg ? sizeof(u64) : 0); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1230 | } |
| 1231 | |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1232 | static inline u32 t_action(const struct trace_entry *ent) |
| 1233 | { |
| 1234 | return te_blk_io_trace(ent)->action; |
| 1235 | } |
| 1236 | |
| 1237 | static inline u32 t_bytes(const struct trace_entry *ent) |
| 1238 | { |
| 1239 | return te_blk_io_trace(ent)->bytes; |
| 1240 | } |
| 1241 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1242 | static inline u32 t_sec(const struct trace_entry *ent) |
| 1243 | { |
| 1244 | return te_blk_io_trace(ent)->bytes >> 9; |
| 1245 | } |
| 1246 | |
| 1247 | static inline unsigned long long t_sector(const struct trace_entry *ent) |
| 1248 | { |
| 1249 | return te_blk_io_trace(ent)->sector; |
| 1250 | } |
| 1251 | |
| 1252 | static inline __u16 t_error(const struct trace_entry *ent) |
| 1253 | { |
Li Zefan | e0dc81b | 2009-03-24 16:05:51 +0800 | [diff] [blame] | 1254 | return te_blk_io_trace(ent)->error; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1257 | static __u64 get_pdu_int(const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1258 | { |
Chaitanya Kulkarni | 71df3fd | 2020-06-04 00:13:29 -0700 | [diff] [blame^] | 1259 | const __be64 *val = pdu_start(ent, has_cg); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1260 | return be64_to_cpu(*val); |
| 1261 | } |
| 1262 | |
| 1263 | static void get_pdu_remap(const struct trace_entry *ent, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1264 | struct blk_io_trace_remap *r, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1265 | { |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1266 | const struct blk_io_trace_remap *__r = pdu_start(ent, has_cg); |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame] | 1267 | __u64 sector_from = __r->sector_from; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1268 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1269 | r->device_from = be32_to_cpu(__r->device_from); |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame] | 1270 | r->device_to = be32_to_cpu(__r->device_to); |
| 1271 | r->sector_from = be64_to_cpu(sector_from); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1272 | } |
| 1273 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1274 | typedef void (blk_log_action_t) (struct trace_iterator *iter, const char *act, |
| 1275 | bool has_cg); |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1276 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1277 | static void blk_log_action_classic(struct trace_iterator *iter, const char *act, |
| 1278 | bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1279 | { |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1280 | char rwbs[RWBS_LEN]; |
Li Zefan | 35ac51b | 2009-03-27 10:19:46 +0800 | [diff] [blame] | 1281 | unsigned long long ts = iter->ts; |
| 1282 | unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1283 | unsigned secs = (unsigned long)ts; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1284 | const struct blk_io_trace *t = te_blk_io_trace(iter->ent); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1285 | |
| 1286 | fill_rwbs(rwbs, t); |
| 1287 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1288 | trace_seq_printf(&iter->seq, |
| 1289 | "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ", |
| 1290 | MAJOR(t->device), MINOR(t->device), iter->cpu, |
| 1291 | secs, nsec_rem, iter->ent->pid, act, rwbs); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1292 | } |
| 1293 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1294 | static void blk_log_action(struct trace_iterator *iter, const char *act, |
| 1295 | bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1296 | { |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1297 | char rwbs[RWBS_LEN]; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1298 | const struct blk_io_trace *t = te_blk_io_trace(iter->ent); |
| 1299 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1300 | fill_rwbs(rwbs, t); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1301 | if (has_cg) { |
Tejun Heo | 67c0496 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1302 | u64 id = t_cgid(iter->ent); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1303 | |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 1304 | if (blk_tracer_flags.val & TRACE_BLK_OPT_CGNAME) { |
| 1305 | char blkcg_name_buf[NAME_MAX + 1] = "<...>"; |
| 1306 | |
| 1307 | cgroup_path_from_kernfs_id(id, blkcg_name_buf, |
| 1308 | sizeof(blkcg_name_buf)); |
| 1309 | trace_seq_printf(&iter->seq, "%3d,%-3d %s %2s %3s ", |
| 1310 | MAJOR(t->device), MINOR(t->device), |
| 1311 | blkcg_name_buf, act, rwbs); |
Tejun Heo | 4043045 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1312 | } else { |
| 1313 | /* |
| 1314 | * The cgid portion used to be "INO,GEN". Userland |
| 1315 | * builds a FILEID_INO32_GEN fid out of them and |
| 1316 | * opens the cgroup using open_by_handle_at(2). |
| 1317 | * While 32bit ino setups are still the same, 64bit |
| 1318 | * ones now use the 64bit ino as the whole ID and |
| 1319 | * no longer use generation. |
| 1320 | * |
| 1321 | * Regarldess of the content, always output |
| 1322 | * "LOW32,HIGH32" so that FILEID_INO32_GEN fid can |
| 1323 | * be mapped back to @id on both 64 and 32bit ino |
| 1324 | * setups. See __kernfs_fh_to_dentry(). |
| 1325 | */ |
Shaohua Li | 69fd5c3 | 2017-07-12 11:49:55 -0700 | [diff] [blame] | 1326 | trace_seq_printf(&iter->seq, |
Tejun Heo | 4043045 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1327 | "%3d,%-3d %llx,%-llx %2s %3s ", |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1328 | MAJOR(t->device), MINOR(t->device), |
Tejun Heo | 4043045 | 2019-11-04 15:54:30 -0800 | [diff] [blame] | 1329 | id & U32_MAX, id >> 32, act, rwbs); |
| 1330 | } |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1331 | } else |
| 1332 | trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ", |
| 1333 | MAJOR(t->device), MINOR(t->device), act, rwbs); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1334 | } |
| 1335 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1336 | static void blk_log_dump_pdu(struct trace_seq *s, |
| 1337 | const struct trace_entry *ent, bool has_cg) |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1338 | { |
Li Zefan | 04986257 | 2009-05-11 14:33:23 +0800 | [diff] [blame] | 1339 | const unsigned char *pdu_buf; |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1340 | int pdu_len; |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1341 | int i, end; |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1342 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1343 | pdu_buf = pdu_start(ent, has_cg); |
| 1344 | pdu_len = pdu_real_len(ent, has_cg); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1345 | |
| 1346 | if (!pdu_len) |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1347 | return; |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1348 | |
| 1349 | /* find the last zero that needs to be printed */ |
| 1350 | for (end = pdu_len - 1; end >= 0; end--) |
| 1351 | if (pdu_buf[end]) |
| 1352 | break; |
| 1353 | end++; |
| 1354 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1355 | trace_seq_putc(s, '('); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1356 | |
| 1357 | for (i = 0; i < pdu_len; i++) { |
| 1358 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1359 | trace_seq_printf(s, "%s%02x", |
| 1360 | i == 0 ? "" : " ", pdu_buf[i]); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1361 | |
| 1362 | /* |
| 1363 | * stop when the rest is just zeroes and indicate so |
| 1364 | * with a ".." appended |
| 1365 | */ |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1366 | if (i == end && end != pdu_len - 1) { |
| 1367 | trace_seq_puts(s, " ..) "); |
| 1368 | return; |
| 1369 | } |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1372 | trace_seq_puts(s, ") "); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1375 | static void blk_log_generic(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1376 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1377 | char cmd[TASK_COMM_LEN]; |
| 1378 | |
| 1379 | trace_find_cmdline(ent->pid, cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1380 | |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1381 | if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) { |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1382 | trace_seq_printf(s, "%u ", t_bytes(ent)); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1383 | blk_log_dump_pdu(s, ent, has_cg); |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1384 | trace_seq_printf(s, "[%s]\n", cmd); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1385 | } else { |
| 1386 | if (t_sec(ent)) |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1387 | trace_seq_printf(s, "%llu + %u [%s]\n", |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1388 | t_sector(ent), t_sec(ent), cmd); |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1389 | else |
| 1390 | trace_seq_printf(s, "[%s]\n", cmd); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1391 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1392 | } |
| 1393 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1394 | static void blk_log_with_error(struct trace_seq *s, |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1395 | const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1396 | { |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1397 | if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) { |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1398 | blk_log_dump_pdu(s, ent, has_cg); |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1399 | trace_seq_printf(s, "[%d]\n", t_error(ent)); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1400 | } else { |
| 1401 | if (t_sec(ent)) |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1402 | trace_seq_printf(s, "%llu + %u [%d]\n", |
| 1403 | t_sector(ent), |
| 1404 | t_sec(ent), t_error(ent)); |
| 1405 | else |
| 1406 | trace_seq_printf(s, "%llu [%d]\n", |
| 1407 | t_sector(ent), t_error(ent)); |
Li Zefan | 66de779 | 2009-04-01 16:19:19 +0800 | [diff] [blame] | 1408 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1409 | } |
| 1410 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1411 | static void blk_log_remap(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1412 | { |
Alan D. Brunelle | a42aaa3 | 2009-05-04 16:27:26 -0400 | [diff] [blame] | 1413 | struct blk_io_trace_remap r = { .device_from = 0, }; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1414 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1415 | get_pdu_remap(ent, &r, has_cg); |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1416 | trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n", |
| 1417 | t_sector(ent), t_sec(ent), |
| 1418 | MAJOR(r.device_from), MINOR(r.device_from), |
| 1419 | (unsigned long long)r.sector_from); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1420 | } |
| 1421 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1422 | static void blk_log_plug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1423 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1424 | char cmd[TASK_COMM_LEN]; |
| 1425 | |
| 1426 | trace_find_cmdline(ent->pid, cmd); |
| 1427 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1428 | trace_seq_printf(s, "[%s]\n", cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1429 | } |
| 1430 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1431 | static void blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1432 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1433 | char cmd[TASK_COMM_LEN]; |
| 1434 | |
| 1435 | trace_find_cmdline(ent->pid, cmd); |
| 1436 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1437 | trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent, has_cg)); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1438 | } |
| 1439 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1440 | static void blk_log_split(struct trace_seq *s, const struct trace_entry *ent, bool has_cg) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1441 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1442 | char cmd[TASK_COMM_LEN]; |
| 1443 | |
| 1444 | trace_find_cmdline(ent->pid, cmd); |
| 1445 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1446 | trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent), |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1447 | get_pdu_int(ent, has_cg), cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1448 | } |
| 1449 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1450 | static void blk_log_msg(struct trace_seq *s, const struct trace_entry *ent, |
| 1451 | bool has_cg) |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1452 | { |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1453 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1454 | trace_seq_putmem(s, pdu_start(ent, has_cg), |
| 1455 | pdu_real_len(ent, has_cg)); |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1456 | trace_seq_putc(s, '\n'); |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1459 | /* |
| 1460 | * struct tracer operations |
| 1461 | */ |
| 1462 | |
| 1463 | static void blk_tracer_print_header(struct seq_file *m) |
| 1464 | { |
| 1465 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) |
| 1466 | return; |
| 1467 | seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n" |
| 1468 | "# | | | | | |\n"); |
| 1469 | } |
| 1470 | |
| 1471 | static void blk_tracer_start(struct trace_array *tr) |
| 1472 | { |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 1473 | blk_tracer_enabled = true; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | static int blk_tracer_init(struct trace_array *tr) |
| 1477 | { |
| 1478 | blk_tr = tr; |
| 1479 | blk_tracer_start(tr); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1480 | return 0; |
| 1481 | } |
| 1482 | |
| 1483 | static void blk_tracer_stop(struct trace_array *tr) |
| 1484 | { |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 1485 | blk_tracer_enabled = false; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | static void blk_tracer_reset(struct trace_array *tr) |
| 1489 | { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1490 | blk_tracer_stop(tr); |
| 1491 | } |
| 1492 | |
Li Zefan | e4955c9 | 2009-03-24 16:04:37 +0800 | [diff] [blame] | 1493 | static const struct { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1494 | const char *act[2]; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1495 | void (*print)(struct trace_seq *s, const struct trace_entry *ent, |
| 1496 | bool has_cg); |
Li Zefan | e4955c9 | 2009-03-24 16:04:37 +0800 | [diff] [blame] | 1497 | } what2act[] = { |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 1498 | [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic }, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1499 | [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic }, |
| 1500 | [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic }, |
| 1501 | [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic }, |
| 1502 | [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic }, |
| 1503 | [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error }, |
| 1504 | [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic }, |
| 1505 | [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error }, |
| 1506 | [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug }, |
| 1507 | [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug }, |
Jens Axboe | 49cac01 | 2011-04-16 13:51:05 +0200 | [diff] [blame] | 1508 | [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug }, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1509 | [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic }, |
| 1510 | [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split }, |
| 1511 | [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic }, |
| 1512 | [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap }, |
| 1513 | }; |
| 1514 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1515 | static enum print_line_t print_one_line(struct trace_iterator *iter, |
| 1516 | bool classic) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1517 | { |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 1518 | struct trace_array *tr = iter->tr; |
Arnaldo Carvalho de Melo | 2c9b238 | 2009-02-02 20:30:12 -0200 | [diff] [blame] | 1519 | struct trace_seq *s = &iter->seq; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1520 | const struct blk_io_trace *t; |
| 1521 | u16 what; |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1522 | bool long_act; |
| 1523 | blk_log_action_t *log_action; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1524 | bool has_cg; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1525 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1526 | t = te_blk_io_trace(iter->ent); |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1527 | what = (t->action & ((1 << BLK_TC_SHIFT) - 1)) & ~__BLK_TA_CGROUP; |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 1528 | long_act = !!(tr->trace_flags & TRACE_ITER_VERBOSE); |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1529 | log_action = classic ? &blk_log_action_classic : &blk_log_action; |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1530 | has_cg = t->action & __BLK_TA_CGROUP; |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1531 | |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1532 | if ((t->action & ~__BLK_TN_CGROUP) == BLK_TN_MESSAGE) { |
| 1533 | log_action(iter, long_act ? "message" : "m", has_cg); |
| 1534 | blk_log_msg(s, iter->ent, has_cg); |
Shaohua Li | b7d7641 | 2016-05-09 17:22:13 -0700 | [diff] [blame] | 1535 | return trace_handle_return(s); |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1536 | } |
| 1537 | |
Li Zefan | eb08f8e | 2009-03-24 16:05:27 +0800 | [diff] [blame] | 1538 | if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act))) |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1539 | trace_seq_printf(s, "Unknown action %x\n", what); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1540 | else { |
Shaohua Li | ca1136c | 2017-07-12 11:49:53 -0700 | [diff] [blame] | 1541 | log_action(iter, what2act[what].act[long_act], has_cg); |
| 1542 | what2act[what].print(s, iter->ent, has_cg); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1543 | } |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1544 | |
| 1545 | return trace_handle_return(s); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1546 | } |
| 1547 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1548 | static enum print_line_t blk_trace_event_print(struct trace_iterator *iter, |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1549 | int flags, struct trace_event *event) |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1550 | { |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1551 | return print_one_line(iter, false); |
| 1552 | } |
| 1553 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1554 | static void blk_trace_synthesize_old_trace(struct trace_iterator *iter) |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1555 | { |
| 1556 | struct trace_seq *s = &iter->seq; |
| 1557 | struct blk_io_trace *t = (struct blk_io_trace *)iter->ent; |
| 1558 | const int offset = offsetof(struct blk_io_trace, sector); |
| 1559 | struct blk_io_trace old = { |
| 1560 | .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION, |
Li Zefan | 6c051ce | 2009-03-25 17:18:56 +0800 | [diff] [blame] | 1561 | .time = iter->ts, |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1562 | }; |
| 1563 | |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1564 | trace_seq_putmem(s, &old, offset); |
| 1565 | trace_seq_putmem(s, &t->sector, |
| 1566 | sizeof(old) - offset + t->pdu_len); |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Arnaldo Carvalho de Melo | ae7462b | 2009-02-03 22:05:50 -0200 | [diff] [blame] | 1569 | static enum print_line_t |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1570 | blk_trace_event_print_binary(struct trace_iterator *iter, int flags, |
| 1571 | struct trace_event *event) |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1572 | { |
Steven Rostedt (Red Hat) | f4a1d08 | 2014-11-12 11:35:48 -0500 | [diff] [blame] | 1573 | blk_trace_synthesize_old_trace(iter); |
| 1574 | |
| 1575 | return trace_handle_return(&iter->seq); |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1576 | } |
| 1577 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1578 | static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter) |
| 1579 | { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1580 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) |
| 1581 | return TRACE_TYPE_UNHANDLED; |
| 1582 | |
Li Zefan | b6a4b0c | 2009-03-27 10:21:23 +0800 | [diff] [blame] | 1583 | return print_one_line(iter, true); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Steven Rostedt (Red Hat) | 8c1a49a | 2014-01-10 11:13:54 -0500 | [diff] [blame] | 1586 | static int |
| 1587 | blk_tracer_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set) |
Li Zefan | f3948f8 | 2009-04-15 11:02:56 +0800 | [diff] [blame] | 1588 | { |
| 1589 | /* don't output context-info for blk_classic output */ |
| 1590 | if (bit == TRACE_BLK_OPT_CLASSIC) { |
| 1591 | if (set) |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 1592 | tr->trace_flags &= ~TRACE_ITER_CONTEXT_INFO; |
Li Zefan | f3948f8 | 2009-04-15 11:02:56 +0800 | [diff] [blame] | 1593 | else |
Steven Rostedt (Red Hat) | 983f938 | 2015-09-30 09:42:05 -0400 | [diff] [blame] | 1594 | tr->trace_flags |= TRACE_ITER_CONTEXT_INFO; |
Li Zefan | f3948f8 | 2009-04-15 11:02:56 +0800 | [diff] [blame] | 1595 | } |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1599 | static struct tracer blk_tracer __read_mostly = { |
| 1600 | .name = "blk", |
| 1601 | .init = blk_tracer_init, |
| 1602 | .reset = blk_tracer_reset, |
| 1603 | .start = blk_tracer_start, |
| 1604 | .stop = blk_tracer_stop, |
| 1605 | .print_header = blk_tracer_print_header, |
| 1606 | .print_line = blk_tracer_print_line, |
| 1607 | .flags = &blk_tracer_flags, |
Li Zefan | f3948f8 | 2009-04-15 11:02:56 +0800 | [diff] [blame] | 1608 | .set_flag = blk_tracer_set_flag, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1609 | }; |
| 1610 | |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1611 | static struct trace_event_functions trace_blk_event_funcs = { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1612 | .trace = blk_trace_event_print, |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1613 | .binary = blk_trace_event_print_binary, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1614 | }; |
| 1615 | |
Steven Rostedt | a9a5776 | 2010-04-22 18:46:14 -0400 | [diff] [blame] | 1616 | static struct trace_event trace_blk_event = { |
| 1617 | .type = TRACE_BLK, |
| 1618 | .funcs = &trace_blk_event_funcs, |
| 1619 | }; |
| 1620 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1621 | static int __init init_blk_tracer(void) |
| 1622 | { |
Steven Rostedt (Red Hat) | 9023c93 | 2015-05-05 09:39:12 -0400 | [diff] [blame] | 1623 | if (!register_trace_event(&trace_blk_event)) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1624 | pr_warn("Warning: could not register block events\n"); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1625 | return 1; |
| 1626 | } |
| 1627 | |
| 1628 | if (register_tracer(&blk_tracer) != 0) { |
Joe Perches | a395d6a | 2016-03-22 14:28:09 -0700 | [diff] [blame] | 1629 | pr_warn("Warning: could not register the block tracer\n"); |
Steven Rostedt (Red Hat) | 9023c93 | 2015-05-05 09:39:12 -0400 | [diff] [blame] | 1630 | unregister_trace_event(&trace_blk_event); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1631 | return 1; |
| 1632 | } |
| 1633 | |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | device_initcall(init_blk_tracer); |
| 1638 | |
| 1639 | static int blk_trace_remove_queue(struct request_queue *q) |
| 1640 | { |
| 1641 | struct blk_trace *bt; |
| 1642 | |
| 1643 | bt = xchg(&q->blk_trace, NULL); |
| 1644 | if (bt == NULL) |
| 1645 | return -EINVAL; |
| 1646 | |
Jens Axboe | a6da002 | 2017-11-05 09:16:09 -0700 | [diff] [blame] | 1647 | put_probe_ref(); |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1648 | synchronize_rcu(); |
Li Zefan | ad5dd54 | 2009-03-27 10:20:24 +0800 | [diff] [blame] | 1649 | blk_trace_free(bt); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1650 | return 0; |
| 1651 | } |
| 1652 | |
| 1653 | /* |
| 1654 | * Setup everything required to start tracing |
| 1655 | */ |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1656 | static int blk_trace_setup_queue(struct request_queue *q, |
| 1657 | struct block_device *bdev) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1658 | { |
Davidlohr Bueso | cdea01b | 2015-10-30 05:25:59 +0900 | [diff] [blame] | 1659 | struct blk_trace *bt = NULL; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1660 | int ret = -ENOMEM; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1661 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1662 | bt = kzalloc(sizeof(*bt), GFP_KERNEL); |
| 1663 | if (!bt) |
Li Zefan | 15152e4 | 2009-03-20 09:49:08 +0800 | [diff] [blame] | 1664 | return -ENOMEM; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1665 | |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1666 | bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char)); |
| 1667 | if (!bt->msg_data) |
| 1668 | goto free_bt; |
| 1669 | |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1670 | bt->dev = bdev->bd_dev; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1671 | bt->act_mask = (u16)-1; |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1672 | |
| 1673 | blk_trace_setup_lba(bt, bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1674 | |
Davidlohr Bueso | cdea01b | 2015-10-30 05:25:59 +0900 | [diff] [blame] | 1675 | ret = -EBUSY; |
| 1676 | if (cmpxchg(&q->blk_trace, NULL, bt)) |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1677 | goto free_bt; |
Li Zefan | 15152e4 | 2009-03-20 09:49:08 +0800 | [diff] [blame] | 1678 | |
Jens Axboe | a6da002 | 2017-11-05 09:16:09 -0700 | [diff] [blame] | 1679 | get_probe_ref(); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1680 | return 0; |
Li Zefan | 18cea45 | 2009-03-27 10:21:54 +0800 | [diff] [blame] | 1681 | |
| 1682 | free_bt: |
| 1683 | blk_trace_free(bt); |
| 1684 | return ret; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /* |
| 1688 | * sysfs interface to enable and configure tracing |
| 1689 | */ |
| 1690 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1691 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, |
| 1692 | struct device_attribute *attr, |
| 1693 | char *buf); |
| 1694 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, |
| 1695 | struct device_attribute *attr, |
| 1696 | const char *buf, size_t count); |
| 1697 | #define BLK_TRACE_DEVICE_ATTR(_name) \ |
| 1698 | DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \ |
| 1699 | sysfs_blk_trace_attr_show, \ |
| 1700 | sysfs_blk_trace_attr_store) |
| 1701 | |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1702 | static BLK_TRACE_DEVICE_ATTR(enable); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1703 | static BLK_TRACE_DEVICE_ATTR(act_mask); |
| 1704 | static BLK_TRACE_DEVICE_ATTR(pid); |
| 1705 | static BLK_TRACE_DEVICE_ATTR(start_lba); |
| 1706 | static BLK_TRACE_DEVICE_ATTR(end_lba); |
| 1707 | |
| 1708 | static struct attribute *blk_trace_attrs[] = { |
| 1709 | &dev_attr_enable.attr, |
| 1710 | &dev_attr_act_mask.attr, |
| 1711 | &dev_attr_pid.attr, |
| 1712 | &dev_attr_start_lba.attr, |
| 1713 | &dev_attr_end_lba.attr, |
| 1714 | NULL |
| 1715 | }; |
| 1716 | |
| 1717 | struct attribute_group blk_trace_attr_group = { |
| 1718 | .name = "trace", |
| 1719 | .attrs = blk_trace_attrs, |
| 1720 | }; |
| 1721 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1722 | static const struct { |
| 1723 | int mask; |
| 1724 | const char *str; |
| 1725 | } mask_maps[] = { |
| 1726 | { BLK_TC_READ, "read" }, |
| 1727 | { BLK_TC_WRITE, "write" }, |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1728 | { BLK_TC_FLUSH, "flush" }, |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1729 | { BLK_TC_SYNC, "sync" }, |
| 1730 | { BLK_TC_QUEUE, "queue" }, |
| 1731 | { BLK_TC_REQUEUE, "requeue" }, |
| 1732 | { BLK_TC_ISSUE, "issue" }, |
| 1733 | { BLK_TC_COMPLETE, "complete" }, |
| 1734 | { BLK_TC_FS, "fs" }, |
| 1735 | { BLK_TC_PC, "pc" }, |
Shaohua Li | 8d1547e | 2016-05-09 17:22:14 -0700 | [diff] [blame] | 1736 | { BLK_TC_NOTIFY, "notify" }, |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1737 | { BLK_TC_AHEAD, "ahead" }, |
| 1738 | { BLK_TC_META, "meta" }, |
| 1739 | { BLK_TC_DISCARD, "discard" }, |
| 1740 | { BLK_TC_DRV_DATA, "drv_data" }, |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1741 | { BLK_TC_FUA, "fua" }, |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1742 | }; |
| 1743 | |
| 1744 | static int blk_trace_str2mask(const char *str) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1745 | { |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1746 | int i; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1747 | int mask = 0; |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1748 | char *buf, *s, *token; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1749 | |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1750 | buf = kstrdup(str, GFP_KERNEL); |
| 1751 | if (buf == NULL) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1752 | return -ENOMEM; |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1753 | s = strstrip(buf); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1754 | |
| 1755 | while (1) { |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1756 | token = strsep(&s, ","); |
| 1757 | if (token == NULL) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1758 | break; |
| 1759 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1760 | if (*token == '\0') |
| 1761 | continue; |
| 1762 | |
| 1763 | for (i = 0; i < ARRAY_SIZE(mask_maps); i++) { |
| 1764 | if (strcasecmp(token, mask_maps[i].str) == 0) { |
| 1765 | mask |= mask_maps[i].mask; |
| 1766 | break; |
| 1767 | } |
| 1768 | } |
| 1769 | if (i == ARRAY_SIZE(mask_maps)) { |
| 1770 | mask = -EINVAL; |
| 1771 | break; |
| 1772 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1773 | } |
Li Zefan | 9eb8512 | 2009-04-09 11:19:40 +0800 | [diff] [blame] | 1774 | kfree(buf); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1775 | |
| 1776 | return mask; |
| 1777 | } |
| 1778 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1779 | static ssize_t blk_trace_mask2str(char *buf, int mask) |
| 1780 | { |
| 1781 | int i; |
| 1782 | char *p = buf; |
| 1783 | |
| 1784 | for (i = 0; i < ARRAY_SIZE(mask_maps); i++) { |
| 1785 | if (mask & mask_maps[i].mask) { |
| 1786 | p += sprintf(p, "%s%s", |
| 1787 | (p == buf) ? "" : ",", mask_maps[i].str); |
| 1788 | } |
| 1789 | } |
| 1790 | *p++ = '\n'; |
| 1791 | |
| 1792 | return p - buf; |
| 1793 | } |
| 1794 | |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1795 | static struct request_queue *blk_trace_get_queue(struct block_device *bdev) |
| 1796 | { |
| 1797 | if (bdev->bd_disk == NULL) |
| 1798 | return NULL; |
| 1799 | |
| 1800 | return bdev_get_queue(bdev); |
| 1801 | } |
| 1802 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1803 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, |
| 1804 | struct device_attribute *attr, |
| 1805 | char *buf) |
| 1806 | { |
| 1807 | struct hd_struct *p = dev_to_part(dev); |
| 1808 | struct request_queue *q; |
| 1809 | struct block_device *bdev; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1810 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1811 | ssize_t ret = -ENXIO; |
| 1812 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1813 | bdev = bdget(part_devt(p)); |
| 1814 | if (bdev == NULL) |
Arnd Bergmann | 01b284f | 2010-09-17 20:39:22 +0200 | [diff] [blame] | 1815 | goto out; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1816 | |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1817 | q = blk_trace_get_queue(bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1818 | if (q == NULL) |
| 1819 | goto out_bdput; |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1820 | |
Waiman Long | 5acb3cc | 2017-09-20 13:12:20 -0600 | [diff] [blame] | 1821 | mutex_lock(&q->blk_trace_mutex); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1822 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1823 | bt = rcu_dereference_protected(q->blk_trace, |
| 1824 | lockdep_is_held(&q->blk_trace_mutex)); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1825 | if (attr == &dev_attr_enable) { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1826 | ret = sprintf(buf, "%u\n", !!bt); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1827 | goto out_unlock_bdev; |
| 1828 | } |
| 1829 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1830 | if (bt == NULL) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1831 | ret = sprintf(buf, "disabled\n"); |
| 1832 | else if (attr == &dev_attr_act_mask) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1833 | ret = blk_trace_mask2str(buf, bt->act_mask); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1834 | else if (attr == &dev_attr_pid) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1835 | ret = sprintf(buf, "%u\n", bt->pid); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1836 | else if (attr == &dev_attr_start_lba) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1837 | ret = sprintf(buf, "%llu\n", bt->start_lba); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1838 | else if (attr == &dev_attr_end_lba) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1839 | ret = sprintf(buf, "%llu\n", bt->end_lba); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1840 | |
| 1841 | out_unlock_bdev: |
Waiman Long | 5acb3cc | 2017-09-20 13:12:20 -0600 | [diff] [blame] | 1842 | mutex_unlock(&q->blk_trace_mutex); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1843 | out_bdput: |
| 1844 | bdput(bdev); |
Arnd Bergmann | 01b284f | 2010-09-17 20:39:22 +0200 | [diff] [blame] | 1845 | out: |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1846 | return ret; |
| 1847 | } |
| 1848 | |
| 1849 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, |
| 1850 | struct device_attribute *attr, |
| 1851 | const char *buf, size_t count) |
| 1852 | { |
| 1853 | struct block_device *bdev; |
| 1854 | struct request_queue *q; |
| 1855 | struct hd_struct *p; |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1856 | struct blk_trace *bt; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1857 | u64 value; |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1858 | ssize_t ret = -EINVAL; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1859 | |
| 1860 | if (count == 0) |
| 1861 | goto out; |
| 1862 | |
| 1863 | if (attr == &dev_attr_act_mask) { |
Shaohua Li | 5f33945 | 2017-05-19 08:04:59 -0700 | [diff] [blame] | 1864 | if (kstrtoull(buf, 0, &value)) { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1865 | /* Assume it is a list of trace category names */ |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1866 | ret = blk_trace_str2mask(buf); |
| 1867 | if (ret < 0) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1868 | goto out; |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1869 | value = ret; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1870 | } |
Shaohua Li | 5f33945 | 2017-05-19 08:04:59 -0700 | [diff] [blame] | 1871 | } else if (kstrtoull(buf, 0, &value)) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1872 | goto out; |
| 1873 | |
Li Zefan | 0934199 | 2009-03-24 17:43:30 +0800 | [diff] [blame] | 1874 | ret = -ENXIO; |
| 1875 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1876 | p = dev_to_part(dev); |
| 1877 | bdev = bdget(part_devt(p)); |
| 1878 | if (bdev == NULL) |
Arnd Bergmann | 01b284f | 2010-09-17 20:39:22 +0200 | [diff] [blame] | 1879 | goto out; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1880 | |
Li Zefan | b125130 | 2009-03-20 10:34:00 +0800 | [diff] [blame] | 1881 | q = blk_trace_get_queue(bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1882 | if (q == NULL) |
| 1883 | goto out_bdput; |
| 1884 | |
Waiman Long | 5acb3cc | 2017-09-20 13:12:20 -0600 | [diff] [blame] | 1885 | mutex_lock(&q->blk_trace_mutex); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1886 | |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1887 | bt = rcu_dereference_protected(q->blk_trace, |
| 1888 | lockdep_is_held(&q->blk_trace_mutex)); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1889 | if (attr == &dev_attr_enable) { |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1890 | if (!!value == !!bt) { |
Steven Rostedt (VMware) | 757d914 | 2018-08-16 16:08:37 -0400 | [diff] [blame] | 1891 | ret = 0; |
| 1892 | goto out_unlock_bdev; |
| 1893 | } |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1894 | if (value) |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1895 | ret = blk_trace_setup_queue(q, bdev); |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1896 | else |
| 1897 | ret = blk_trace_remove_queue(q); |
| 1898 | goto out_unlock_bdev; |
| 1899 | } |
| 1900 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1901 | ret = 0; |
Cengiz Can | 153031a | 2020-03-04 13:58:19 +0300 | [diff] [blame] | 1902 | if (bt == NULL) { |
Li Zefan | 9908c30 | 2009-04-14 13:59:34 +0800 | [diff] [blame] | 1903 | ret = blk_trace_setup_queue(q, bdev); |
Cengiz Can | 153031a | 2020-03-04 13:58:19 +0300 | [diff] [blame] | 1904 | bt = rcu_dereference_protected(q->blk_trace, |
| 1905 | lockdep_is_held(&q->blk_trace_mutex)); |
| 1906 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1907 | |
| 1908 | if (ret == 0) { |
| 1909 | if (attr == &dev_attr_act_mask) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1910 | bt->act_mask = value; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1911 | else if (attr == &dev_attr_pid) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1912 | bt->pid = value; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1913 | else if (attr == &dev_attr_start_lba) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1914 | bt->start_lba = value; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1915 | else if (attr == &dev_attr_end_lba) |
Jan Kara | c780e86 | 2020-02-06 15:28:12 +0100 | [diff] [blame] | 1916 | bt->end_lba = value; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1917 | } |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1918 | |
| 1919 | out_unlock_bdev: |
Waiman Long | 5acb3cc | 2017-09-20 13:12:20 -0600 | [diff] [blame] | 1920 | mutex_unlock(&q->blk_trace_mutex); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1921 | out_bdput: |
| 1922 | bdput(bdev); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1923 | out: |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1924 | return ret ? ret : count; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1925 | } |
Li Zefan | cd649b8 | 2009-03-20 11:33:55 +0800 | [diff] [blame] | 1926 | |
Li Zefan | 1d54ad6 | 2009-04-14 14:00:05 +0800 | [diff] [blame] | 1927 | int blk_trace_init_sysfs(struct device *dev) |
| 1928 | { |
| 1929 | return sysfs_create_group(&dev->kobj, &blk_trace_attr_group); |
| 1930 | } |
| 1931 | |
Zdenek Kabelac | 48c0d4d | 2009-09-25 06:19:26 +0200 | [diff] [blame] | 1932 | void blk_trace_remove_sysfs(struct device *dev) |
| 1933 | { |
| 1934 | sysfs_remove_group(&dev->kobj, &blk_trace_attr_group); |
| 1935 | } |
| 1936 | |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1937 | #endif /* CONFIG_BLK_DEV_IO_TRACE */ |
| 1938 | |
| 1939 | #ifdef CONFIG_EVENT_TRACING |
| 1940 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1941 | void blk_fill_rwbs(char *rwbs, unsigned int op, int bytes) |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1942 | { |
| 1943 | int i = 0; |
| 1944 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1945 | if (op & REQ_PREFLUSH) |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1946 | rwbs[i++] = 'F'; |
| 1947 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1948 | switch (op & REQ_OP_MASK) { |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 1949 | case REQ_OP_WRITE: |
| 1950 | case REQ_OP_WRITE_SAME: |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1951 | rwbs[i++] = 'W'; |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 1952 | break; |
| 1953 | case REQ_OP_DISCARD: |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1954 | rwbs[i++] = 'D'; |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 1955 | break; |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 1956 | case REQ_OP_SECURE_ERASE: |
| 1957 | rwbs[i++] = 'D'; |
| 1958 | rwbs[i++] = 'E'; |
| 1959 | break; |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 1960 | case REQ_OP_FLUSH: |
| 1961 | rwbs[i++] = 'F'; |
| 1962 | break; |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 1963 | case REQ_OP_READ: |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1964 | rwbs[i++] = 'R'; |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 1965 | break; |
| 1966 | default: |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1967 | rwbs[i++] = 'N'; |
Mike Christie | 1b9a9ab | 2016-06-05 14:32:18 -0500 | [diff] [blame] | 1968 | } |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1969 | |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1970 | if (op & REQ_FUA) |
Namhyung Kim | c09c47c | 2011-08-11 10:36:05 +0200 | [diff] [blame] | 1971 | rwbs[i++] = 'F'; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1972 | if (op & REQ_RAHEAD) |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1973 | rwbs[i++] = 'A'; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1974 | if (op & REQ_SYNC) |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1975 | rwbs[i++] = 'S'; |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1976 | if (op & REQ_META) |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1977 | rwbs[i++] = 'M'; |
| 1978 | |
| 1979 | rwbs[i] = '\0'; |
| 1980 | } |
Kent Overstreet | 9ca8f8e | 2012-04-13 21:56:43 -0400 | [diff] [blame] | 1981 | EXPORT_SYMBOL_GPL(blk_fill_rwbs); |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1982 | |
Li Zefan | 5578213 | 2009-06-09 13:43:05 +0800 | [diff] [blame] | 1983 | #endif /* CONFIG_EVENT_TRACING */ |
| 1984 | |