Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 1 | /* |
Jens Axboe | 0fe2347 | 2006-09-04 15:41:16 +0200 | [diff] [blame] | 2 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | * |
| 17 | */ |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/blkdev.h> |
| 20 | #include <linux/blktrace_api.h> |
| 21 | #include <linux/percpu.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/debugfs.h> |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 25 | #include <linux/time.h> |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 26 | #include <trace/block.h> |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Frederic Weisbecker | 2db270a | 2009-02-07 20:46:45 +0100 | [diff] [blame] | 28 | #include "trace_output.h" |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 29 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 30 | static unsigned int blktrace_seq __read_mostly = 1; |
| 31 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 32 | static struct trace_array *blk_tr; |
| 33 | static int __read_mostly blk_tracer_enabled; |
| 34 | |
| 35 | /* Select an alternative, minimalistic output than the original one */ |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 36 | #define TRACE_BLK_OPT_CLASSIC 0x1 |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 37 | |
| 38 | static struct tracer_opt blk_tracer_opts[] = { |
| 39 | /* Default disable the minimalistic output */ |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 40 | { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) }, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 41 | { } |
| 42 | }; |
| 43 | |
| 44 | static struct tracer_flags blk_tracer_flags = { |
| 45 | .val = 0, |
| 46 | .opts = blk_tracer_opts, |
| 47 | }; |
| 48 | |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 49 | /* Global reference count of probes */ |
| 50 | static DEFINE_MUTEX(blk_probe_mutex); |
| 51 | static atomic_t blk_probes_ref = ATOMIC_INIT(0); |
| 52 | |
| 53 | static int blk_register_tracepoints(void); |
| 54 | static void blk_unregister_tracepoints(void); |
| 55 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 56 | /* |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 57 | * Send out a notify message. |
| 58 | */ |
Jens Axboe | a863055 | 2006-12-04 09:30:58 +0100 | [diff] [blame] | 59 | static void trace_note(struct blk_trace *bt, pid_t pid, int action, |
| 60 | const void *data, size_t len) |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 61 | { |
| 62 | struct blk_io_trace *t; |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 63 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 64 | if (!bt->rchan) |
| 65 | return; |
| 66 | |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 67 | t = relay_reserve(bt->rchan, sizeof(*t) + len); |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 68 | if (t) { |
| 69 | const int cpu = smp_processor_id(); |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 70 | |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 71 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; |
Ingo Molnar | 2997c8c | 2008-01-11 13:35:54 +0100 | [diff] [blame] | 72 | t->time = ktime_to_ns(ktime_get()); |
Jens Axboe | d3d9d2a | 2006-12-04 09:27:41 +0100 | [diff] [blame] | 73 | t->device = bt->dev; |
| 74 | t->action = action; |
| 75 | t->pid = pid; |
| 76 | t->cpu = cpu; |
| 77 | t->pdu_len = len; |
| 78 | memcpy((void *) t + sizeof(*t), data, len); |
| 79 | } |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 83 | * Send out a notify for this process, if we haven't done so since a trace |
| 84 | * started |
| 85 | */ |
| 86 | static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk) |
| 87 | { |
Jens Axboe | a863055 | 2006-12-04 09:30:58 +0100 | [diff] [blame] | 88 | tsk->btrace_seq = blktrace_seq; |
| 89 | trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm)); |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 90 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 91 | |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 92 | static void trace_note_time(struct blk_trace *bt) |
| 93 | { |
| 94 | struct timespec now; |
| 95 | unsigned long flags; |
| 96 | u32 words[2]; |
| 97 | |
| 98 | getnstimeofday(&now); |
| 99 | words[0] = now.tv_sec; |
| 100 | words[1] = now.tv_nsec; |
| 101 | |
| 102 | local_irq_save(flags); |
| 103 | trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words)); |
| 104 | local_irq_restore(flags); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 107 | void __trace_note_message(struct blk_trace *bt, const char *fmt, ...) |
| 108 | { |
| 109 | int n; |
| 110 | va_list args; |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 111 | unsigned long flags; |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 112 | char *buf; |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 113 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 114 | if (blk_tr) { |
| 115 | va_start(args, fmt); |
| 116 | ftrace_vprintk(fmt, args); |
| 117 | va_end(args); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (!bt->msg_data) |
| 122 | return; |
| 123 | |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 124 | local_irq_save(flags); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 125 | buf = per_cpu_ptr(bt->msg_data, smp_processor_id()); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 126 | va_start(args, fmt); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 127 | n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 128 | va_end(args); |
| 129 | |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 130 | trace_note(bt, 0, BLK_TN_MESSAGE, buf, n); |
Carl Henrik Lunde | 14a73f5 | 2008-06-12 20:13:58 +0200 | [diff] [blame] | 131 | local_irq_restore(flags); |
Alan D. Brunelle | 9d5f09a | 2008-05-27 14:54:41 +0200 | [diff] [blame] | 132 | } |
| 133 | EXPORT_SYMBOL_GPL(__trace_note_message); |
| 134 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 135 | static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector, |
| 136 | pid_t pid) |
| 137 | { |
| 138 | if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0) |
| 139 | return 1; |
| 140 | if (sector < bt->start_lba || sector > bt->end_lba) |
| 141 | return 1; |
| 142 | if (bt->pid && pid != bt->pid) |
| 143 | return 1; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Data direction bit lookup |
| 150 | */ |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 151 | static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), |
| 152 | BLK_TC_ACT(BLK_TC_WRITE) }; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 153 | |
David Woodhouse | 35ba8f7 | 2008-08-10 12:33:00 +0100 | [diff] [blame] | 154 | /* The ilog2() calls fall out because they're constant */ |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 155 | #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \ |
| 156 | (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | * The worker for the various blk_add_trace*() types. Fills out a |
| 160 | * blk_io_trace structure and places it in a per-cpu subbuffer. |
| 161 | */ |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 162 | static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 163 | int rw, u32 what, int error, int pdu_len, void *pdu_data) |
| 164 | { |
| 165 | struct task_struct *tsk = current; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 166 | struct ring_buffer_event *event = NULL; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 167 | struct blk_io_trace *t; |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 168 | unsigned long flags = 0; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 169 | unsigned long *sequence; |
| 170 | pid_t pid; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 171 | int cpu, pc = 0; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 172 | |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 173 | if (unlikely(bt->trace_state != Blktrace_running || |
| 174 | !blk_tracer_enabled)) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 175 | return; |
| 176 | |
| 177 | what |= ddir_act[rw & WRITE]; |
David Woodhouse | 35ba8f7 | 2008-08-10 12:33:00 +0100 | [diff] [blame] | 178 | what |= MASK_TC_BIT(rw, BARRIER); |
Ingo Molnar | 72c26c9 | 2009-02-19 09:00:35 +0100 | [diff] [blame] | 179 | what |= MASK_TC_BIT(rw, SYNCIO); |
David Woodhouse | 35ba8f7 | 2008-08-10 12:33:00 +0100 | [diff] [blame] | 180 | what |= MASK_TC_BIT(rw, AHEAD); |
| 181 | what |= MASK_TC_BIT(rw, META); |
| 182 | what |= MASK_TC_BIT(rw, DISCARD); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 183 | |
| 184 | pid = tsk->pid; |
| 185 | if (unlikely(act_log_check(bt, what, sector, pid))) |
| 186 | return; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 187 | cpu = raw_smp_processor_id(); |
| 188 | |
| 189 | if (blk_tr) { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 190 | tracing_record_cmdline(current); |
| 191 | |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 192 | pc = preempt_count(); |
| 193 | event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK, |
| 194 | sizeof(*t) + pdu_len, |
| 195 | 0, pc); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 196 | if (!event) |
| 197 | return; |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 198 | t = ring_buffer_event_data(event); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 199 | goto record_it; |
| 200 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * A word about the locking here - we disable interrupts to reserve |
| 204 | * 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] | 205 | * from coming in and stepping on our toes. |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 206 | */ |
| 207 | local_irq_save(flags); |
| 208 | |
| 209 | if (unlikely(tsk->btrace_seq != blktrace_seq)) |
| 210 | trace_note_tsk(bt, tsk); |
| 211 | |
| 212 | t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len); |
| 213 | if (t) { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 214 | sequence = per_cpu_ptr(bt->sequence, cpu); |
| 215 | |
| 216 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; |
| 217 | t->sequence = ++(*sequence); |
Ingo Molnar | 2997c8c | 2008-01-11 13:35:54 +0100 | [diff] [blame] | 218 | t->time = ktime_to_ns(ktime_get()); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 219 | record_it: |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 220 | /* |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 221 | * These two are not needed in ftrace as they are in the |
| 222 | * generic trace_entry, filled by tracing_generic_entry_update, |
| 223 | * but for the trace_event->bin() synthesizer benefit we do it |
| 224 | * here too. |
| 225 | */ |
| 226 | t->cpu = cpu; |
| 227 | t->pid = pid; |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 228 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 229 | t->sector = sector; |
| 230 | t->bytes = bytes; |
| 231 | t->action = what; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 232 | t->device = bt->dev; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 233 | t->error = error; |
| 234 | t->pdu_len = pdu_len; |
| 235 | |
| 236 | if (pdu_len) |
| 237 | memcpy((void *) t + sizeof(*t), pdu_data, pdu_len); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 238 | |
| 239 | if (blk_tr) { |
Arnaldo Carvalho de Melo | 51a763d | 2009-02-05 16:14:13 -0200 | [diff] [blame] | 240 | trace_buffer_unlock_commit(blk_tr, event, 0, pc); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 241 | return; |
| 242 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | local_irq_restore(flags); |
| 246 | } |
| 247 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 248 | static struct dentry *blk_tree_root; |
Jens Axboe | 11a5715 | 2008-01-11 13:37:01 +0100 | [diff] [blame] | 249 | static DEFINE_MUTEX(blk_tree_mutex); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 250 | |
| 251 | static void blk_trace_cleanup(struct blk_trace *bt) |
| 252 | { |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 253 | debugfs_remove(bt->msg_file); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 254 | debugfs_remove(bt->dropped_file); |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 255 | relay_close(bt->rchan); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 256 | free_percpu(bt->sequence); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 257 | free_percpu(bt->msg_data); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 258 | kfree(bt); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 259 | mutex_lock(&blk_probe_mutex); |
| 260 | if (atomic_dec_and_test(&blk_probes_ref)) |
| 261 | blk_unregister_tracepoints(); |
| 262 | mutex_unlock(&blk_probe_mutex); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 265 | int blk_trace_remove(struct request_queue *q) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 266 | { |
| 267 | struct blk_trace *bt; |
| 268 | |
| 269 | bt = xchg(&q->blk_trace, NULL); |
| 270 | if (!bt) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | if (bt->trace_state == Blktrace_setup || |
| 274 | bt->trace_state == Blktrace_stopped) |
| 275 | blk_trace_cleanup(bt); |
| 276 | |
| 277 | return 0; |
| 278 | } |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(blk_trace_remove); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 280 | |
| 281 | static int blk_dropped_open(struct inode *inode, struct file *filp) |
| 282 | { |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 283 | filp->private_data = inode->i_private; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | static ssize_t blk_dropped_read(struct file *filp, char __user *buffer, |
| 289 | size_t count, loff_t *ppos) |
| 290 | { |
| 291 | struct blk_trace *bt = filp->private_data; |
| 292 | char buf[16]; |
| 293 | |
| 294 | snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped)); |
| 295 | |
| 296 | return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf)); |
| 297 | } |
| 298 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 299 | static const struct file_operations blk_dropped_fops = { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 300 | .owner = THIS_MODULE, |
| 301 | .open = blk_dropped_open, |
| 302 | .read = blk_dropped_read, |
| 303 | }; |
| 304 | |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 305 | static int blk_msg_open(struct inode *inode, struct file *filp) |
| 306 | { |
| 307 | filp->private_data = inode->i_private; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static ssize_t blk_msg_write(struct file *filp, const char __user *buffer, |
| 313 | size_t count, loff_t *ppos) |
| 314 | { |
| 315 | char *msg; |
| 316 | struct blk_trace *bt; |
| 317 | |
| 318 | if (count > BLK_TN_MAX_MSG) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | msg = kmalloc(count, GFP_KERNEL); |
| 322 | if (msg == NULL) |
| 323 | return -ENOMEM; |
| 324 | |
| 325 | if (copy_from_user(msg, buffer, count)) { |
| 326 | kfree(msg); |
| 327 | return -EFAULT; |
| 328 | } |
| 329 | |
| 330 | bt = filp->private_data; |
| 331 | __trace_note_message(bt, "%s", msg); |
| 332 | kfree(msg); |
| 333 | |
| 334 | return count; |
| 335 | } |
| 336 | |
| 337 | static const struct file_operations blk_msg_fops = { |
| 338 | .owner = THIS_MODULE, |
| 339 | .open = blk_msg_open, |
| 340 | .write = blk_msg_write, |
| 341 | }; |
| 342 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 343 | /* |
| 344 | * Keep track of how many times we encountered a full subbuffer, to aid |
| 345 | * the user space app in telling how many lost events there were. |
| 346 | */ |
| 347 | static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf, |
| 348 | void *prev_subbuf, size_t prev_padding) |
| 349 | { |
| 350 | struct blk_trace *bt; |
| 351 | |
| 352 | if (!relay_buf_full(buf)) |
| 353 | return 1; |
| 354 | |
| 355 | bt = buf->chan->private_data; |
| 356 | atomic_inc(&bt->dropped); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int blk_remove_buf_file_callback(struct dentry *dentry) |
| 361 | { |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 362 | struct dentry *parent = dentry->d_parent; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 363 | debugfs_remove(dentry); |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 364 | |
| 365 | /* |
| 366 | * this will fail for all but the last file, but that is ok. what we |
| 367 | * care about is the top level buts->name directory going away, when |
| 368 | * the last trace file is gone. Then we don't have to rmdir() that |
| 369 | * manually on trace stop, so it nicely solves the issue with |
| 370 | * force killing of running traces. |
| 371 | */ |
| 372 | |
| 373 | debugfs_remove(parent); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static struct dentry *blk_create_buf_file_callback(const char *filename, |
| 378 | struct dentry *parent, |
| 379 | int mode, |
| 380 | struct rchan_buf *buf, |
| 381 | int *is_global) |
| 382 | { |
| 383 | return debugfs_create_file(filename, mode, parent, buf, |
| 384 | &relay_file_operations); |
| 385 | } |
| 386 | |
| 387 | static struct rchan_callbacks blk_relay_callbacks = { |
| 388 | .subbuf_start = blk_subbuf_start_callback, |
| 389 | .create_buf_file = blk_create_buf_file_callback, |
| 390 | .remove_buf_file = blk_remove_buf_file_callback, |
| 391 | }; |
| 392 | |
| 393 | /* |
| 394 | * Setup everything required to start tracing |
| 395 | */ |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 396 | int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev, |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 397 | struct blk_user_trace_setup *buts) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 398 | { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 399 | struct blk_trace *old_bt, *bt = NULL; |
| 400 | struct dentry *dir = NULL; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 401 | int ret, i; |
| 402 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 403 | if (!buts->buf_size || !buts->buf_nr) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 404 | return -EINVAL; |
| 405 | |
Jens Axboe | 0497b34 | 2008-10-01 16:16:25 +0200 | [diff] [blame] | 406 | strncpy(buts->name, name, BLKTRACE_BDEV_SIZE); |
| 407 | buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0'; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * some device names have larger paths - convert the slashes |
| 411 | * to underscores for this to work as expected |
| 412 | */ |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 413 | for (i = 0; i < strlen(buts->name); i++) |
| 414 | if (buts->name[i] == '/') |
| 415 | buts->name[i] = '_'; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 416 | |
| 417 | ret = -ENOMEM; |
| 418 | bt = kzalloc(sizeof(*bt), GFP_KERNEL); |
| 419 | if (!bt) |
| 420 | goto err; |
| 421 | |
| 422 | bt->sequence = alloc_percpu(unsigned long); |
| 423 | if (!bt->sequence) |
| 424 | goto err; |
| 425 | |
Ingo Molnar | f0ef039 | 2009-03-06 16:44:14 +0100 | [diff] [blame] | 426 | bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char)); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 427 | if (!bt->msg_data) |
| 428 | goto err; |
| 429 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 430 | ret = -ENOENT; |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 431 | |
| 432 | if (!blk_tree_root) { |
| 433 | blk_tree_root = debugfs_create_dir("block", NULL); |
| 434 | if (!blk_tree_root) |
Li Zefan | 1a17662 | 2009-03-20 09:47:30 +0800 | [diff] [blame^] | 435 | goto err; |
Jens Axboe | f48fc4d | 2009-01-05 10:17:25 +0100 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | dir = debugfs_create_dir(buts->name, blk_tree_root); |
| 439 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 440 | if (!dir) |
| 441 | goto err; |
| 442 | |
| 443 | bt->dir = dir; |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 444 | bt->dev = dev; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 445 | atomic_set(&bt->dropped, 0); |
| 446 | |
| 447 | ret = -EIO; |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 448 | bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, |
| 449 | &blk_dropped_fops); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 450 | if (!bt->dropped_file) |
| 451 | goto err; |
| 452 | |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 453 | bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops); |
| 454 | if (!bt->msg_file) |
| 455 | goto err; |
| 456 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 457 | bt->rchan = relay_open("trace", dir, buts->buf_size, |
| 458 | buts->buf_nr, &blk_relay_callbacks, bt); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 459 | if (!bt->rchan) |
| 460 | goto err; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 461 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 462 | bt->act_mask = buts->act_mask; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 463 | if (!bt->act_mask) |
| 464 | bt->act_mask = (u16) -1; |
| 465 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 466 | bt->start_lba = buts->start_lba; |
| 467 | bt->end_lba = buts->end_lba; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 468 | if (!bt->end_lba) |
| 469 | bt->end_lba = -1ULL; |
| 470 | |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 471 | bt->pid = buts->pid; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 472 | bt->trace_state = Blktrace_setup; |
| 473 | |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 474 | mutex_lock(&blk_probe_mutex); |
| 475 | if (atomic_add_return(1, &blk_probes_ref) == 1) { |
| 476 | ret = blk_register_tracepoints(); |
| 477 | if (ret) |
| 478 | goto probe_err; |
| 479 | } |
| 480 | mutex_unlock(&blk_probe_mutex); |
| 481 | |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 482 | ret = -EBUSY; |
| 483 | old_bt = xchg(&q->blk_trace, bt); |
| 484 | if (old_bt) { |
| 485 | (void) xchg(&q->blk_trace, old_bt); |
| 486 | goto err; |
| 487 | } |
| 488 | |
| 489 | return 0; |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 490 | probe_err: |
| 491 | atomic_dec(&blk_probes_ref); |
| 492 | mutex_unlock(&blk_probe_mutex); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 493 | err: |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 494 | if (bt) { |
Alan D. Brunelle | 02c6230 | 2008-06-11 09:12:52 +0200 | [diff] [blame] | 495 | if (bt->msg_file) |
| 496 | debugfs_remove(bt->msg_file); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 497 | if (bt->dropped_file) |
| 498 | debugfs_remove(bt->dropped_file); |
Alan Stern | a120586 | 2006-12-06 20:32:37 -0800 | [diff] [blame] | 499 | free_percpu(bt->sequence); |
Jens Axboe | 6456591 | 2008-05-28 14:45:33 +0200 | [diff] [blame] | 500 | free_percpu(bt->msg_data); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 501 | if (bt->rchan) |
| 502 | relay_close(bt->rchan); |
| 503 | kfree(bt); |
| 504 | } |
| 505 | return ret; |
| 506 | } |
| 507 | |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 508 | int blk_trace_setup(struct request_queue *q, char *name, dev_t dev, |
| 509 | char __user *arg) |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 510 | { |
| 511 | struct blk_user_trace_setup buts; |
| 512 | int ret; |
| 513 | |
| 514 | ret = copy_from_user(&buts, arg, sizeof(buts)); |
| 515 | if (ret) |
| 516 | return -EFAULT; |
| 517 | |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 518 | ret = do_blk_trace_setup(q, name, dev, &buts); |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 519 | if (ret) |
| 520 | return ret; |
| 521 | |
| 522 | if (copy_to_user(arg, &buts, sizeof(buts))) |
| 523 | return -EFAULT; |
| 524 | |
| 525 | return 0; |
| 526 | } |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 527 | EXPORT_SYMBOL_GPL(blk_trace_setup); |
Arnd Bergmann | 171044d4 | 2007-10-09 13:23:53 +0200 | [diff] [blame] | 528 | |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 529 | int blk_trace_startstop(struct request_queue *q, int start) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 530 | { |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 531 | int ret; |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 532 | struct blk_trace *bt = q->blk_trace; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 533 | |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 534 | if (bt == NULL) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 535 | return -EINVAL; |
| 536 | |
| 537 | /* |
| 538 | * For starting a trace, we can transition from a setup or stopped |
| 539 | * trace. For stopping a trace, the state must be running |
| 540 | */ |
| 541 | ret = -EINVAL; |
| 542 | if (start) { |
| 543 | if (bt->trace_state == Blktrace_setup || |
| 544 | bt->trace_state == Blktrace_stopped) { |
| 545 | blktrace_seq++; |
| 546 | smp_mb(); |
| 547 | bt->trace_state = Blktrace_running; |
Olaf Kirch | be1c634 | 2006-12-01 10:39:12 +0100 | [diff] [blame] | 548 | |
| 549 | trace_note_time(bt); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 550 | ret = 0; |
| 551 | } |
| 552 | } else { |
| 553 | if (bt->trace_state == Blktrace_running) { |
| 554 | bt->trace_state = Blktrace_stopped; |
| 555 | relay_flush(bt->rchan); |
| 556 | ret = 0; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | return ret; |
| 561 | } |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 562 | EXPORT_SYMBOL_GPL(blk_trace_startstop); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 563 | |
| 564 | /** |
| 565 | * blk_trace_ioctl: - handle the ioctls associated with tracing |
| 566 | * @bdev: the block device |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 567 | * @cmd: the ioctl cmd |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 568 | * @arg: the argument data, if any |
| 569 | * |
| 570 | **/ |
| 571 | int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg) |
| 572 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 573 | struct request_queue *q; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 574 | int ret, start = 0; |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 575 | char b[BDEVNAME_SIZE]; |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 576 | |
| 577 | q = bdev_get_queue(bdev); |
| 578 | if (!q) |
| 579 | return -ENXIO; |
| 580 | |
| 581 | mutex_lock(&bdev->bd_mutex); |
| 582 | |
| 583 | switch (cmd) { |
| 584 | case BLKTRACESETUP: |
Jean Delvare | f36f21e | 2008-05-12 14:02:33 -0700 | [diff] [blame] | 585 | bdevname(bdev, b); |
Christof Schmitt | 6da127a | 2008-01-11 10:09:43 +0100 | [diff] [blame] | 586 | ret = blk_trace_setup(q, b, bdev->bd_dev, arg); |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 587 | break; |
| 588 | case BLKTRACESTART: |
| 589 | start = 1; |
| 590 | case BLKTRACESTOP: |
| 591 | ret = blk_trace_startstop(q, start); |
| 592 | break; |
| 593 | case BLKTRACETEARDOWN: |
| 594 | ret = blk_trace_remove(q); |
| 595 | break; |
| 596 | default: |
| 597 | ret = -ENOTTY; |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | mutex_unlock(&bdev->bd_mutex); |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * blk_trace_shutdown: - stop and cleanup trace structures |
| 607 | * @q: the request queue associated with the device |
| 608 | * |
| 609 | **/ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 610 | void blk_trace_shutdown(struct request_queue *q) |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 611 | { |
Alexey Dobriyan | 6c5c934 | 2006-09-29 01:59:40 -0700 | [diff] [blame] | 612 | if (q->blk_trace) { |
| 613 | blk_trace_startstop(q, 0); |
| 614 | blk_trace_remove(q); |
| 615 | } |
Jens Axboe | 2056a78 | 2006-03-23 20:00:26 +0100 | [diff] [blame] | 616 | } |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 617 | |
| 618 | /* |
| 619 | * blktrace probes |
| 620 | */ |
| 621 | |
| 622 | /** |
| 623 | * blk_add_trace_rq - Add a trace for a request oriented action |
| 624 | * @q: queue the io is for |
| 625 | * @rq: the source request |
| 626 | * @what: the action |
| 627 | * |
| 628 | * Description: |
| 629 | * Records an action against a request. Will log the bio offset + size. |
| 630 | * |
| 631 | **/ |
| 632 | static void blk_add_trace_rq(struct request_queue *q, struct request *rq, |
| 633 | u32 what) |
| 634 | { |
| 635 | struct blk_trace *bt = q->blk_trace; |
| 636 | int rw = rq->cmd_flags & 0x03; |
| 637 | |
| 638 | if (likely(!bt)) |
| 639 | return; |
| 640 | |
| 641 | if (blk_discard_rq(rq)) |
| 642 | rw |= (1 << BIO_RW_DISCARD); |
| 643 | |
| 644 | if (blk_pc_request(rq)) { |
| 645 | what |= BLK_TC_ACT(BLK_TC_PC); |
| 646 | __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors, |
| 647 | sizeof(rq->cmd), rq->cmd); |
| 648 | } else { |
| 649 | what |= BLK_TC_ACT(BLK_TC_FS); |
| 650 | __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9, |
| 651 | rw, what, rq->errors, 0, NULL); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq) |
| 656 | { |
| 657 | blk_add_trace_rq(q, rq, BLK_TA_ABORT); |
| 658 | } |
| 659 | |
| 660 | static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq) |
| 661 | { |
| 662 | blk_add_trace_rq(q, rq, BLK_TA_INSERT); |
| 663 | } |
| 664 | |
| 665 | static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq) |
| 666 | { |
| 667 | blk_add_trace_rq(q, rq, BLK_TA_ISSUE); |
| 668 | } |
| 669 | |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 670 | static void blk_add_trace_rq_requeue(struct request_queue *q, |
| 671 | struct request *rq) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 672 | { |
| 673 | blk_add_trace_rq(q, rq, BLK_TA_REQUEUE); |
| 674 | } |
| 675 | |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 676 | static void blk_add_trace_rq_complete(struct request_queue *q, |
| 677 | struct request *rq) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 678 | { |
| 679 | blk_add_trace_rq(q, rq, BLK_TA_COMPLETE); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * blk_add_trace_bio - Add a trace for a bio oriented action |
| 684 | * @q: queue the io is for |
| 685 | * @bio: the source bio |
| 686 | * @what: the action |
| 687 | * |
| 688 | * Description: |
| 689 | * Records an action against a bio. Will log the bio offset + size. |
| 690 | * |
| 691 | **/ |
| 692 | static void blk_add_trace_bio(struct request_queue *q, struct bio *bio, |
| 693 | u32 what) |
| 694 | { |
| 695 | struct blk_trace *bt = q->blk_trace; |
| 696 | |
| 697 | if (likely(!bt)) |
| 698 | return; |
| 699 | |
| 700 | __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, |
| 701 | !bio_flagged(bio, BIO_UPTODATE), 0, NULL); |
| 702 | } |
| 703 | |
| 704 | static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio) |
| 705 | { |
| 706 | blk_add_trace_bio(q, bio, BLK_TA_BOUNCE); |
| 707 | } |
| 708 | |
| 709 | static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio) |
| 710 | { |
| 711 | blk_add_trace_bio(q, bio, BLK_TA_COMPLETE); |
| 712 | } |
| 713 | |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 714 | static void blk_add_trace_bio_backmerge(struct request_queue *q, |
| 715 | struct bio *bio) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 716 | { |
| 717 | blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE); |
| 718 | } |
| 719 | |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 720 | static void blk_add_trace_bio_frontmerge(struct request_queue *q, |
| 721 | struct bio *bio) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 722 | { |
| 723 | blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE); |
| 724 | } |
| 725 | |
| 726 | static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio) |
| 727 | { |
| 728 | blk_add_trace_bio(q, bio, BLK_TA_QUEUE); |
| 729 | } |
| 730 | |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 731 | static void blk_add_trace_getrq(struct request_queue *q, |
| 732 | struct bio *bio, int rw) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 733 | { |
| 734 | if (bio) |
| 735 | blk_add_trace_bio(q, bio, BLK_TA_GETRQ); |
| 736 | else { |
| 737 | struct blk_trace *bt = q->blk_trace; |
| 738 | |
| 739 | if (bt) |
| 740 | __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 745 | static void blk_add_trace_sleeprq(struct request_queue *q, |
| 746 | struct bio *bio, int rw) |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 747 | { |
| 748 | if (bio) |
| 749 | blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ); |
| 750 | else { |
| 751 | struct blk_trace *bt = q->blk_trace; |
| 752 | |
| 753 | if (bt) |
Arnaldo Carvalho de Melo | 939b366 | 2009-02-03 11:58:29 -0200 | [diff] [blame] | 754 | __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ, |
| 755 | 0, 0, NULL); |
Arnaldo Carvalho de Melo | 5f3ea37 | 2008-10-30 08:34:33 +0100 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
| 759 | static void blk_add_trace_plug(struct request_queue *q) |
| 760 | { |
| 761 | struct blk_trace *bt = q->blk_trace; |
| 762 | |
| 763 | if (bt) |
| 764 | __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL); |
| 765 | } |
| 766 | |
| 767 | static void blk_add_trace_unplug_io(struct request_queue *q) |
| 768 | { |
| 769 | struct blk_trace *bt = q->blk_trace; |
| 770 | |
| 771 | if (bt) { |
| 772 | unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE]; |
| 773 | __be64 rpdu = cpu_to_be64(pdu); |
| 774 | |
| 775 | __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0, |
| 776 | sizeof(rpdu), &rpdu); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | static void blk_add_trace_unplug_timer(struct request_queue *q) |
| 781 | { |
| 782 | struct blk_trace *bt = q->blk_trace; |
| 783 | |
| 784 | if (bt) { |
| 785 | unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE]; |
| 786 | __be64 rpdu = cpu_to_be64(pdu); |
| 787 | |
| 788 | __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0, |
| 789 | sizeof(rpdu), &rpdu); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | static void blk_add_trace_split(struct request_queue *q, struct bio *bio, |
| 794 | unsigned int pdu) |
| 795 | { |
| 796 | struct blk_trace *bt = q->blk_trace; |
| 797 | |
| 798 | if (bt) { |
| 799 | __be64 rpdu = cpu_to_be64(pdu); |
| 800 | |
| 801 | __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, |
| 802 | BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE), |
| 803 | sizeof(rpdu), &rpdu); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * blk_add_trace_remap - Add a trace for a remap operation |
| 809 | * @q: queue the io is for |
| 810 | * @bio: the source bio |
| 811 | * @dev: target device |
| 812 | * @from: source sector |
| 813 | * @to: target sector |
| 814 | * |
| 815 | * Description: |
| 816 | * Device mapper or raid target sometimes need to split a bio because |
| 817 | * it spans a stripe (or similar). Add a trace for that action. |
| 818 | * |
| 819 | **/ |
| 820 | static void blk_add_trace_remap(struct request_queue *q, struct bio *bio, |
| 821 | dev_t dev, sector_t from, sector_t to) |
| 822 | { |
| 823 | struct blk_trace *bt = q->blk_trace; |
| 824 | struct blk_io_trace_remap r; |
| 825 | |
| 826 | if (likely(!bt)) |
| 827 | return; |
| 828 | |
| 829 | r.device = cpu_to_be32(dev); |
| 830 | r.device_from = cpu_to_be32(bio->bi_bdev->bd_dev); |
| 831 | r.sector = cpu_to_be64(to); |
| 832 | |
| 833 | __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP, |
| 834 | !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r); |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * blk_add_driver_data - Add binary message with driver-specific data |
| 839 | * @q: queue the io is for |
| 840 | * @rq: io request |
| 841 | * @data: driver-specific data |
| 842 | * @len: length of driver-specific data |
| 843 | * |
| 844 | * Description: |
| 845 | * Some drivers might want to write driver-specific data per request. |
| 846 | * |
| 847 | **/ |
| 848 | void blk_add_driver_data(struct request_queue *q, |
| 849 | struct request *rq, |
| 850 | void *data, size_t len) |
| 851 | { |
| 852 | struct blk_trace *bt = q->blk_trace; |
| 853 | |
| 854 | if (likely(!bt)) |
| 855 | return; |
| 856 | |
| 857 | if (blk_pc_request(rq)) |
| 858 | __blk_add_trace(bt, 0, rq->data_len, 0, BLK_TA_DRV_DATA, |
| 859 | rq->errors, len, data); |
| 860 | else |
| 861 | __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9, |
| 862 | 0, BLK_TA_DRV_DATA, rq->errors, len, data); |
| 863 | } |
| 864 | EXPORT_SYMBOL_GPL(blk_add_driver_data); |
| 865 | |
| 866 | static int blk_register_tracepoints(void) |
| 867 | { |
| 868 | int ret; |
| 869 | |
| 870 | ret = register_trace_block_rq_abort(blk_add_trace_rq_abort); |
| 871 | WARN_ON(ret); |
| 872 | ret = register_trace_block_rq_insert(blk_add_trace_rq_insert); |
| 873 | WARN_ON(ret); |
| 874 | ret = register_trace_block_rq_issue(blk_add_trace_rq_issue); |
| 875 | WARN_ON(ret); |
| 876 | ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue); |
| 877 | WARN_ON(ret); |
| 878 | ret = register_trace_block_rq_complete(blk_add_trace_rq_complete); |
| 879 | WARN_ON(ret); |
| 880 | ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce); |
| 881 | WARN_ON(ret); |
| 882 | ret = register_trace_block_bio_complete(blk_add_trace_bio_complete); |
| 883 | WARN_ON(ret); |
| 884 | ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge); |
| 885 | WARN_ON(ret); |
| 886 | ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge); |
| 887 | WARN_ON(ret); |
| 888 | ret = register_trace_block_bio_queue(blk_add_trace_bio_queue); |
| 889 | WARN_ON(ret); |
| 890 | ret = register_trace_block_getrq(blk_add_trace_getrq); |
| 891 | WARN_ON(ret); |
| 892 | ret = register_trace_block_sleeprq(blk_add_trace_sleeprq); |
| 893 | WARN_ON(ret); |
| 894 | ret = register_trace_block_plug(blk_add_trace_plug); |
| 895 | WARN_ON(ret); |
| 896 | ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer); |
| 897 | WARN_ON(ret); |
| 898 | ret = register_trace_block_unplug_io(blk_add_trace_unplug_io); |
| 899 | WARN_ON(ret); |
| 900 | ret = register_trace_block_split(blk_add_trace_split); |
| 901 | WARN_ON(ret); |
| 902 | ret = register_trace_block_remap(blk_add_trace_remap); |
| 903 | WARN_ON(ret); |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | static void blk_unregister_tracepoints(void) |
| 908 | { |
| 909 | unregister_trace_block_remap(blk_add_trace_remap); |
| 910 | unregister_trace_block_split(blk_add_trace_split); |
| 911 | unregister_trace_block_unplug_io(blk_add_trace_unplug_io); |
| 912 | unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer); |
| 913 | unregister_trace_block_plug(blk_add_trace_plug); |
| 914 | unregister_trace_block_sleeprq(blk_add_trace_sleeprq); |
| 915 | unregister_trace_block_getrq(blk_add_trace_getrq); |
| 916 | unregister_trace_block_bio_queue(blk_add_trace_bio_queue); |
| 917 | unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge); |
| 918 | unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge); |
| 919 | unregister_trace_block_bio_complete(blk_add_trace_bio_complete); |
| 920 | unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce); |
| 921 | unregister_trace_block_rq_complete(blk_add_trace_rq_complete); |
| 922 | unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue); |
| 923 | unregister_trace_block_rq_issue(blk_add_trace_rq_issue); |
| 924 | unregister_trace_block_rq_insert(blk_add_trace_rq_insert); |
| 925 | unregister_trace_block_rq_abort(blk_add_trace_rq_abort); |
| 926 | |
| 927 | tracepoint_synchronize_unregister(); |
| 928 | } |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 929 | |
| 930 | /* |
| 931 | * struct blk_io_tracer formatting routines |
| 932 | */ |
| 933 | |
| 934 | static void fill_rwbs(char *rwbs, const struct blk_io_trace *t) |
| 935 | { |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 936 | int i = 0; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 937 | |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 938 | if (t->action & BLK_TC_DISCARD) |
| 939 | rwbs[i++] = 'D'; |
| 940 | else if (t->action & BLK_TC_WRITE) |
| 941 | rwbs[i++] = 'W'; |
| 942 | else if (t->bytes) |
| 943 | rwbs[i++] = 'R'; |
| 944 | else |
| 945 | rwbs[i++] = 'N'; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 946 | |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 947 | if (t->action & BLK_TC_AHEAD) |
| 948 | rwbs[i++] = 'A'; |
| 949 | if (t->action & BLK_TC_BARRIER) |
| 950 | rwbs[i++] = 'B'; |
| 951 | if (t->action & BLK_TC_SYNC) |
| 952 | rwbs[i++] = 'S'; |
| 953 | if (t->action & BLK_TC_META) |
| 954 | rwbs[i++] = 'M'; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 955 | |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 956 | rwbs[i] = '\0'; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static inline |
| 960 | const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent) |
| 961 | { |
| 962 | return (const struct blk_io_trace *)ent; |
| 963 | } |
| 964 | |
| 965 | static inline const void *pdu_start(const struct trace_entry *ent) |
| 966 | { |
| 967 | return te_blk_io_trace(ent) + 1; |
| 968 | } |
| 969 | |
| 970 | static inline u32 t_sec(const struct trace_entry *ent) |
| 971 | { |
| 972 | return te_blk_io_trace(ent)->bytes >> 9; |
| 973 | } |
| 974 | |
| 975 | static inline unsigned long long t_sector(const struct trace_entry *ent) |
| 976 | { |
| 977 | return te_blk_io_trace(ent)->sector; |
| 978 | } |
| 979 | |
| 980 | static inline __u16 t_error(const struct trace_entry *ent) |
| 981 | { |
| 982 | return te_blk_io_trace(ent)->sector; |
| 983 | } |
| 984 | |
| 985 | static __u64 get_pdu_int(const struct trace_entry *ent) |
| 986 | { |
| 987 | const __u64 *val = pdu_start(ent); |
| 988 | return be64_to_cpu(*val); |
| 989 | } |
| 990 | |
| 991 | static void get_pdu_remap(const struct trace_entry *ent, |
| 992 | struct blk_io_trace_remap *r) |
| 993 | { |
| 994 | const struct blk_io_trace_remap *__r = pdu_start(ent); |
| 995 | __u64 sector = __r->sector; |
| 996 | |
| 997 | r->device = be32_to_cpu(__r->device); |
| 998 | r->device_from = be32_to_cpu(__r->device_from); |
| 999 | r->sector = be64_to_cpu(sector); |
| 1000 | } |
| 1001 | |
| 1002 | static int blk_log_action_iter(struct trace_iterator *iter, const char *act) |
| 1003 | { |
| 1004 | char rwbs[6]; |
| 1005 | unsigned long long ts = ns2usecs(iter->ts); |
| 1006 | unsigned long usec_rem = do_div(ts, USEC_PER_SEC); |
| 1007 | unsigned secs = (unsigned long)ts; |
| 1008 | const struct trace_entry *ent = iter->ent; |
| 1009 | const struct blk_io_trace *t = (const struct blk_io_trace *)ent; |
| 1010 | |
| 1011 | fill_rwbs(rwbs, t); |
| 1012 | |
| 1013 | return trace_seq_printf(&iter->seq, |
| 1014 | "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ", |
| 1015 | MAJOR(t->device), MINOR(t->device), iter->cpu, |
| 1016 | secs, usec_rem, ent->pid, act, rwbs); |
| 1017 | } |
| 1018 | |
| 1019 | static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t, |
| 1020 | const char *act) |
| 1021 | { |
| 1022 | char rwbs[6]; |
| 1023 | fill_rwbs(rwbs, t); |
| 1024 | return trace_seq_printf(s, "%3d,%-3d %2s %3s ", |
| 1025 | MAJOR(t->device), MINOR(t->device), act, rwbs); |
| 1026 | } |
| 1027 | |
| 1028 | static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent) |
| 1029 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1030 | char cmd[TASK_COMM_LEN]; |
| 1031 | |
| 1032 | trace_find_cmdline(ent->pid, cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1033 | |
| 1034 | if (t_sec(ent)) |
| 1035 | return trace_seq_printf(s, "%llu + %u [%s]\n", |
| 1036 | t_sector(ent), t_sec(ent), cmd); |
| 1037 | return trace_seq_printf(s, "[%s]\n", cmd); |
| 1038 | } |
| 1039 | |
Arnaldo Carvalho de Melo | 157f9c0 | 2009-01-26 15:00:56 -0200 | [diff] [blame] | 1040 | static int blk_log_with_error(struct trace_seq *s, |
| 1041 | const struct trace_entry *ent) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1042 | { |
| 1043 | if (t_sec(ent)) |
| 1044 | return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent), |
| 1045 | t_sec(ent), t_error(ent)); |
| 1046 | return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent)); |
| 1047 | } |
| 1048 | |
| 1049 | static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent) |
| 1050 | { |
| 1051 | struct blk_io_trace_remap r = { .device = 0, }; |
| 1052 | |
| 1053 | get_pdu_remap(ent, &r); |
| 1054 | return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n", |
| 1055 | t_sector(ent), |
| 1056 | t_sec(ent), MAJOR(r.device), MINOR(r.device), |
| 1057 | (unsigned long long)r.sector); |
| 1058 | } |
| 1059 | |
| 1060 | static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent) |
| 1061 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1062 | char cmd[TASK_COMM_LEN]; |
| 1063 | |
| 1064 | trace_find_cmdline(ent->pid, cmd); |
| 1065 | |
| 1066 | return trace_seq_printf(s, "[%s]\n", cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent) |
| 1070 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1071 | char cmd[TASK_COMM_LEN]; |
| 1072 | |
| 1073 | trace_find_cmdline(ent->pid, cmd); |
| 1074 | |
| 1075 | return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent)); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent) |
| 1079 | { |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1080 | char cmd[TASK_COMM_LEN]; |
| 1081 | |
| 1082 | trace_find_cmdline(ent->pid, cmd); |
| 1083 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1084 | return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent), |
Steven Rostedt | 4ca53085 | 2009-03-16 19:20:15 -0400 | [diff] [blame] | 1085 | get_pdu_int(ent), cmd); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | /* |
| 1089 | * struct tracer operations |
| 1090 | */ |
| 1091 | |
| 1092 | static void blk_tracer_print_header(struct seq_file *m) |
| 1093 | { |
| 1094 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) |
| 1095 | return; |
| 1096 | seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n" |
| 1097 | "# | | | | | |\n"); |
| 1098 | } |
| 1099 | |
| 1100 | static void blk_tracer_start(struct trace_array *tr) |
| 1101 | { |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1102 | mutex_lock(&blk_probe_mutex); |
| 1103 | if (atomic_add_return(1, &blk_probes_ref) == 1) |
| 1104 | if (blk_register_tracepoints()) |
| 1105 | atomic_dec(&blk_probes_ref); |
| 1106 | mutex_unlock(&blk_probe_mutex); |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1107 | trace_flags &= ~TRACE_ITER_CONTEXT_INFO; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | static int blk_tracer_init(struct trace_array *tr) |
| 1111 | { |
| 1112 | blk_tr = tr; |
| 1113 | blk_tracer_start(tr); |
| 1114 | mutex_lock(&blk_probe_mutex); |
| 1115 | blk_tracer_enabled++; |
| 1116 | mutex_unlock(&blk_probe_mutex); |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static void blk_tracer_stop(struct trace_array *tr) |
| 1121 | { |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1122 | trace_flags |= TRACE_ITER_CONTEXT_INFO; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1123 | mutex_lock(&blk_probe_mutex); |
| 1124 | if (atomic_dec_and_test(&blk_probes_ref)) |
| 1125 | blk_unregister_tracepoints(); |
| 1126 | mutex_unlock(&blk_probe_mutex); |
| 1127 | } |
| 1128 | |
| 1129 | static void blk_tracer_reset(struct trace_array *tr) |
| 1130 | { |
| 1131 | if (!atomic_read(&blk_probes_ref)) |
| 1132 | return; |
| 1133 | |
| 1134 | mutex_lock(&blk_probe_mutex); |
| 1135 | blk_tracer_enabled--; |
| 1136 | WARN_ON(blk_tracer_enabled < 0); |
| 1137 | mutex_unlock(&blk_probe_mutex); |
| 1138 | |
| 1139 | blk_tracer_stop(tr); |
| 1140 | } |
| 1141 | |
| 1142 | static struct { |
| 1143 | const char *act[2]; |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 1144 | int (*print)(struct trace_seq *s, const struct trace_entry *ent); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1145 | } what2act[] __read_mostly = { |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 1146 | [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic }, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1147 | [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic }, |
| 1148 | [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic }, |
| 1149 | [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic }, |
| 1150 | [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic }, |
| 1151 | [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error }, |
| 1152 | [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic }, |
| 1153 | [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error }, |
| 1154 | [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug }, |
| 1155 | [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug }, |
| 1156 | [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug }, |
| 1157 | [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic }, |
| 1158 | [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split }, |
| 1159 | [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic }, |
| 1160 | [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap }, |
| 1161 | }; |
| 1162 | |
Arnaldo Carvalho de Melo | ae7462b | 2009-02-03 22:05:50 -0200 | [diff] [blame] | 1163 | static enum print_line_t blk_trace_event_print(struct trace_iterator *iter, |
| 1164 | int flags) |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1165 | { |
Arnaldo Carvalho de Melo | 2c9b238 | 2009-02-02 20:30:12 -0200 | [diff] [blame] | 1166 | struct trace_seq *s = &iter->seq; |
| 1167 | const struct blk_io_trace *t = (struct blk_io_trace *)iter->ent; |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1168 | const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1); |
| 1169 | int ret; |
| 1170 | |
Arnaldo Carvalho de Melo | d9793bd | 2009-02-03 20:20:41 -0200 | [diff] [blame] | 1171 | if (!trace_print_context(iter)) |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1172 | return TRACE_TYPE_PARTIAL_LINE; |
| 1173 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1174 | if (unlikely(what == 0 || what > ARRAY_SIZE(what2act))) |
| 1175 | ret = trace_seq_printf(s, "Bad pc action %x\n", what); |
| 1176 | else { |
| 1177 | const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE); |
| 1178 | ret = blk_log_action_seq(s, t, what2act[what].act[long_act]); |
| 1179 | if (ret) |
Arnaldo Carvalho de Melo | 2c9b238 | 2009-02-02 20:30:12 -0200 | [diff] [blame] | 1180 | ret = what2act[what].print(s, iter->ent); |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE; |
| 1184 | } |
| 1185 | |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1186 | static int blk_trace_synthesize_old_trace(struct trace_iterator *iter) |
| 1187 | { |
| 1188 | struct trace_seq *s = &iter->seq; |
| 1189 | struct blk_io_trace *t = (struct blk_io_trace *)iter->ent; |
| 1190 | const int offset = offsetof(struct blk_io_trace, sector); |
| 1191 | struct blk_io_trace old = { |
| 1192 | .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION, |
| 1193 | .time = ns2usecs(iter->ts), |
| 1194 | }; |
| 1195 | |
| 1196 | if (!trace_seq_putmem(s, &old, offset)) |
| 1197 | return 0; |
| 1198 | return trace_seq_putmem(s, &t->sector, |
| 1199 | sizeof(old) - offset + t->pdu_len); |
| 1200 | } |
| 1201 | |
Arnaldo Carvalho de Melo | ae7462b | 2009-02-03 22:05:50 -0200 | [diff] [blame] | 1202 | static enum print_line_t |
| 1203 | blk_trace_event_print_binary(struct trace_iterator *iter, int flags) |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1204 | { |
| 1205 | return blk_trace_synthesize_old_trace(iter) ? |
| 1206 | TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE; |
| 1207 | } |
| 1208 | |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1209 | static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter) |
| 1210 | { |
| 1211 | const struct blk_io_trace *t; |
| 1212 | u16 what; |
| 1213 | int ret; |
| 1214 | |
| 1215 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) |
| 1216 | return TRACE_TYPE_UNHANDLED; |
| 1217 | |
| 1218 | t = (const struct blk_io_trace *)iter->ent; |
| 1219 | what = t->action & ((1 << BLK_TC_SHIFT) - 1); |
| 1220 | |
| 1221 | if (unlikely(what == 0 || what > ARRAY_SIZE(what2act))) |
| 1222 | ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what); |
| 1223 | else { |
| 1224 | const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE); |
| 1225 | ret = blk_log_action_iter(iter, what2act[what].act[long_act]); |
| 1226 | if (ret) |
| 1227 | ret = what2act[what].print(&iter->seq, iter->ent); |
| 1228 | } |
| 1229 | |
| 1230 | return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE; |
| 1231 | } |
| 1232 | |
| 1233 | static struct tracer blk_tracer __read_mostly = { |
| 1234 | .name = "blk", |
| 1235 | .init = blk_tracer_init, |
| 1236 | .reset = blk_tracer_reset, |
| 1237 | .start = blk_tracer_start, |
| 1238 | .stop = blk_tracer_stop, |
| 1239 | .print_header = blk_tracer_print_header, |
| 1240 | .print_line = blk_tracer_print_line, |
| 1241 | .flags = &blk_tracer_flags, |
| 1242 | }; |
| 1243 | |
| 1244 | static struct trace_event trace_blk_event = { |
Steven Rostedt | ef18012 | 2009-03-10 14:10:56 -0400 | [diff] [blame] | 1245 | .type = TRACE_BLK, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1246 | .trace = blk_trace_event_print, |
Arnaldo Carvalho de Melo | 08a06b8 | 2009-02-02 20:30:40 -0200 | [diff] [blame] | 1247 | .binary = blk_trace_event_print_binary, |
Arnaldo Carvalho de Melo | c71a896 | 2009-01-23 12:06:27 -0200 | [diff] [blame] | 1248 | }; |
| 1249 | |
| 1250 | static int __init init_blk_tracer(void) |
| 1251 | { |
| 1252 | if (!register_ftrace_event(&trace_blk_event)) { |
| 1253 | pr_warning("Warning: could not register block events\n"); |
| 1254 | return 1; |
| 1255 | } |
| 1256 | |
| 1257 | if (register_tracer(&blk_tracer) != 0) { |
| 1258 | pr_warning("Warning: could not register the block tracer\n"); |
| 1259 | unregister_ftrace_event(&trace_blk_event); |
| 1260 | return 1; |
| 1261 | } |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | device_initcall(init_blk_tracer); |
| 1267 | |
| 1268 | static int blk_trace_remove_queue(struct request_queue *q) |
| 1269 | { |
| 1270 | struct blk_trace *bt; |
| 1271 | |
| 1272 | bt = xchg(&q->blk_trace, NULL); |
| 1273 | if (bt == NULL) |
| 1274 | return -EINVAL; |
| 1275 | |
| 1276 | kfree(bt); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Setup everything required to start tracing |
| 1282 | */ |
| 1283 | static int blk_trace_setup_queue(struct request_queue *q, dev_t dev) |
| 1284 | { |
| 1285 | struct blk_trace *old_bt, *bt = NULL; |
| 1286 | int ret; |
| 1287 | |
| 1288 | ret = -ENOMEM; |
| 1289 | bt = kzalloc(sizeof(*bt), GFP_KERNEL); |
| 1290 | if (!bt) |
| 1291 | goto err; |
| 1292 | |
| 1293 | bt->dev = dev; |
| 1294 | bt->act_mask = (u16)-1; |
| 1295 | bt->end_lba = -1ULL; |
| 1296 | bt->trace_state = Blktrace_running; |
| 1297 | |
| 1298 | old_bt = xchg(&q->blk_trace, bt); |
| 1299 | if (old_bt != NULL) { |
| 1300 | (void)xchg(&q->blk_trace, old_bt); |
| 1301 | kfree(bt); |
| 1302 | ret = -EBUSY; |
| 1303 | } |
| 1304 | return 0; |
| 1305 | err: |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | /* |
| 1310 | * sysfs interface to enable and configure tracing |
| 1311 | */ |
| 1312 | |
| 1313 | static ssize_t sysfs_blk_trace_enable_show(struct device *dev, |
| 1314 | struct device_attribute *attr, |
| 1315 | char *buf) |
| 1316 | { |
| 1317 | struct hd_struct *p = dev_to_part(dev); |
| 1318 | struct block_device *bdev; |
| 1319 | ssize_t ret = -ENXIO; |
| 1320 | |
| 1321 | lock_kernel(); |
| 1322 | bdev = bdget(part_devt(p)); |
| 1323 | if (bdev != NULL) { |
| 1324 | struct request_queue *q = bdev_get_queue(bdev); |
| 1325 | |
| 1326 | if (q != NULL) { |
| 1327 | mutex_lock(&bdev->bd_mutex); |
| 1328 | ret = sprintf(buf, "%u\n", !!q->blk_trace); |
| 1329 | mutex_unlock(&bdev->bd_mutex); |
| 1330 | } |
| 1331 | |
| 1332 | bdput(bdev); |
| 1333 | } |
| 1334 | |
| 1335 | unlock_kernel(); |
| 1336 | return ret; |
| 1337 | } |
| 1338 | |
| 1339 | static ssize_t sysfs_blk_trace_enable_store(struct device *dev, |
| 1340 | struct device_attribute *attr, |
| 1341 | const char *buf, size_t count) |
| 1342 | { |
| 1343 | struct block_device *bdev; |
| 1344 | struct request_queue *q; |
| 1345 | struct hd_struct *p; |
| 1346 | int value; |
| 1347 | ssize_t ret = -ENXIO; |
| 1348 | |
| 1349 | if (count == 0 || sscanf(buf, "%d", &value) != 1) |
| 1350 | goto out; |
| 1351 | |
| 1352 | lock_kernel(); |
| 1353 | p = dev_to_part(dev); |
| 1354 | bdev = bdget(part_devt(p)); |
| 1355 | if (bdev == NULL) |
| 1356 | goto out_unlock_kernel; |
| 1357 | |
| 1358 | q = bdev_get_queue(bdev); |
| 1359 | if (q == NULL) |
| 1360 | goto out_bdput; |
| 1361 | |
| 1362 | mutex_lock(&bdev->bd_mutex); |
| 1363 | if (value) |
| 1364 | ret = blk_trace_setup_queue(q, bdev->bd_dev); |
| 1365 | else |
| 1366 | ret = blk_trace_remove_queue(q); |
| 1367 | mutex_unlock(&bdev->bd_mutex); |
| 1368 | |
| 1369 | if (ret == 0) |
| 1370 | ret = count; |
| 1371 | out_bdput: |
| 1372 | bdput(bdev); |
| 1373 | out_unlock_kernel: |
| 1374 | unlock_kernel(); |
| 1375 | out: |
| 1376 | return ret; |
| 1377 | } |
| 1378 | |
| 1379 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, |
| 1380 | struct device_attribute *attr, |
| 1381 | char *buf); |
| 1382 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, |
| 1383 | struct device_attribute *attr, |
| 1384 | const char *buf, size_t count); |
| 1385 | #define BLK_TRACE_DEVICE_ATTR(_name) \ |
| 1386 | DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \ |
| 1387 | sysfs_blk_trace_attr_show, \ |
| 1388 | sysfs_blk_trace_attr_store) |
| 1389 | |
| 1390 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, |
| 1391 | sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store); |
| 1392 | static BLK_TRACE_DEVICE_ATTR(act_mask); |
| 1393 | static BLK_TRACE_DEVICE_ATTR(pid); |
| 1394 | static BLK_TRACE_DEVICE_ATTR(start_lba); |
| 1395 | static BLK_TRACE_DEVICE_ATTR(end_lba); |
| 1396 | |
| 1397 | static struct attribute *blk_trace_attrs[] = { |
| 1398 | &dev_attr_enable.attr, |
| 1399 | &dev_attr_act_mask.attr, |
| 1400 | &dev_attr_pid.attr, |
| 1401 | &dev_attr_start_lba.attr, |
| 1402 | &dev_attr_end_lba.attr, |
| 1403 | NULL |
| 1404 | }; |
| 1405 | |
| 1406 | struct attribute_group blk_trace_attr_group = { |
| 1407 | .name = "trace", |
| 1408 | .attrs = blk_trace_attrs, |
| 1409 | }; |
| 1410 | |
| 1411 | static int blk_str2act_mask(const char *str) |
| 1412 | { |
| 1413 | int mask = 0; |
| 1414 | char *copy = kstrdup(str, GFP_KERNEL), *s; |
| 1415 | |
| 1416 | if (copy == NULL) |
| 1417 | return -ENOMEM; |
| 1418 | |
| 1419 | s = strstrip(copy); |
| 1420 | |
| 1421 | while (1) { |
| 1422 | char *sep = strchr(s, ','); |
| 1423 | |
| 1424 | if (sep != NULL) |
| 1425 | *sep = '\0'; |
| 1426 | |
| 1427 | if (strcasecmp(s, "barrier") == 0) |
| 1428 | mask |= BLK_TC_BARRIER; |
| 1429 | else if (strcasecmp(s, "complete") == 0) |
| 1430 | mask |= BLK_TC_COMPLETE; |
| 1431 | else if (strcasecmp(s, "fs") == 0) |
| 1432 | mask |= BLK_TC_FS; |
| 1433 | else if (strcasecmp(s, "issue") == 0) |
| 1434 | mask |= BLK_TC_ISSUE; |
| 1435 | else if (strcasecmp(s, "pc") == 0) |
| 1436 | mask |= BLK_TC_PC; |
| 1437 | else if (strcasecmp(s, "queue") == 0) |
| 1438 | mask |= BLK_TC_QUEUE; |
| 1439 | else if (strcasecmp(s, "read") == 0) |
| 1440 | mask |= BLK_TC_READ; |
| 1441 | else if (strcasecmp(s, "requeue") == 0) |
| 1442 | mask |= BLK_TC_REQUEUE; |
| 1443 | else if (strcasecmp(s, "sync") == 0) |
| 1444 | mask |= BLK_TC_SYNC; |
| 1445 | else if (strcasecmp(s, "write") == 0) |
| 1446 | mask |= BLK_TC_WRITE; |
| 1447 | |
| 1448 | if (sep == NULL) |
| 1449 | break; |
| 1450 | |
| 1451 | s = sep + 1; |
| 1452 | } |
| 1453 | kfree(copy); |
| 1454 | |
| 1455 | return mask; |
| 1456 | } |
| 1457 | |
| 1458 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, |
| 1459 | struct device_attribute *attr, |
| 1460 | char *buf) |
| 1461 | { |
| 1462 | struct hd_struct *p = dev_to_part(dev); |
| 1463 | struct request_queue *q; |
| 1464 | struct block_device *bdev; |
| 1465 | ssize_t ret = -ENXIO; |
| 1466 | |
| 1467 | lock_kernel(); |
| 1468 | bdev = bdget(part_devt(p)); |
| 1469 | if (bdev == NULL) |
| 1470 | goto out_unlock_kernel; |
| 1471 | |
| 1472 | q = bdev_get_queue(bdev); |
| 1473 | if (q == NULL) |
| 1474 | goto out_bdput; |
| 1475 | mutex_lock(&bdev->bd_mutex); |
| 1476 | if (q->blk_trace == NULL) |
| 1477 | ret = sprintf(buf, "disabled\n"); |
| 1478 | else if (attr == &dev_attr_act_mask) |
| 1479 | ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask); |
| 1480 | else if (attr == &dev_attr_pid) |
| 1481 | ret = sprintf(buf, "%u\n", q->blk_trace->pid); |
| 1482 | else if (attr == &dev_attr_start_lba) |
| 1483 | ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba); |
| 1484 | else if (attr == &dev_attr_end_lba) |
| 1485 | ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba); |
| 1486 | mutex_unlock(&bdev->bd_mutex); |
| 1487 | out_bdput: |
| 1488 | bdput(bdev); |
| 1489 | out_unlock_kernel: |
| 1490 | unlock_kernel(); |
| 1491 | return ret; |
| 1492 | } |
| 1493 | |
| 1494 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, |
| 1495 | struct device_attribute *attr, |
| 1496 | const char *buf, size_t count) |
| 1497 | { |
| 1498 | struct block_device *bdev; |
| 1499 | struct request_queue *q; |
| 1500 | struct hd_struct *p; |
| 1501 | u64 value; |
| 1502 | ssize_t ret = -ENXIO; |
| 1503 | |
| 1504 | if (count == 0) |
| 1505 | goto out; |
| 1506 | |
| 1507 | if (attr == &dev_attr_act_mask) { |
| 1508 | if (sscanf(buf, "%llx", &value) != 1) { |
| 1509 | /* Assume it is a list of trace category names */ |
| 1510 | value = blk_str2act_mask(buf); |
| 1511 | if (value < 0) |
| 1512 | goto out; |
| 1513 | } |
| 1514 | } else if (sscanf(buf, "%llu", &value) != 1) |
| 1515 | goto out; |
| 1516 | |
| 1517 | lock_kernel(); |
| 1518 | p = dev_to_part(dev); |
| 1519 | bdev = bdget(part_devt(p)); |
| 1520 | if (bdev == NULL) |
| 1521 | goto out_unlock_kernel; |
| 1522 | |
| 1523 | q = bdev_get_queue(bdev); |
| 1524 | if (q == NULL) |
| 1525 | goto out_bdput; |
| 1526 | |
| 1527 | mutex_lock(&bdev->bd_mutex); |
| 1528 | ret = 0; |
| 1529 | if (q->blk_trace == NULL) |
| 1530 | ret = blk_trace_setup_queue(q, bdev->bd_dev); |
| 1531 | |
| 1532 | if (ret == 0) { |
| 1533 | if (attr == &dev_attr_act_mask) |
| 1534 | q->blk_trace->act_mask = value; |
| 1535 | else if (attr == &dev_attr_pid) |
| 1536 | q->blk_trace->pid = value; |
| 1537 | else if (attr == &dev_attr_start_lba) |
| 1538 | q->blk_trace->start_lba = value; |
| 1539 | else if (attr == &dev_attr_end_lba) |
| 1540 | q->blk_trace->end_lba = value; |
| 1541 | ret = count; |
| 1542 | } |
| 1543 | mutex_unlock(&bdev->bd_mutex); |
| 1544 | out_bdput: |
| 1545 | bdput(bdev); |
| 1546 | out_unlock_kernel: |
| 1547 | unlock_kernel(); |
| 1548 | out: |
| 1549 | return ret; |
| 1550 | } |