diff options
Diffstat (limited to 'kernel/trace/blktrace.c')
| -rw-r--r-- | kernel/trace/blktrace.c | 1549 |
1 files changed, 1549 insertions, 0 deletions
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c new file mode 100644 index 00000000000..947c5b3f90c --- /dev/null +++ b/kernel/trace/blktrace.c | |||
| @@ -0,0 +1,1549 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk> | ||
| 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 | */ | ||
| 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> | ||
| 25 | #include <linux/time.h> | ||
| 26 | #include <trace/block.h> | ||
| 27 | #include <linux/uaccess.h> | ||
| 28 | #include "trace_output.h" | ||
| 29 | |||
| 30 | static unsigned int blktrace_seq __read_mostly = 1; | ||
| 31 | |||
| 32 | static struct trace_array *blk_tr; | ||
| 33 | static bool blk_tracer_enabled __read_mostly; | ||
| 34 | |||
| 35 | /* Select an alternative, minimalistic output than the original one */ | ||
| 36 | #define TRACE_BLK_OPT_CLASSIC 0x1 | ||
| 37 | |||
| 38 | static struct tracer_opt blk_tracer_opts[] = { | ||
| 39 | /* Default disable the minimalistic output */ | ||
| 40 | { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) }, | ||
| 41 | { } | ||
| 42 | }; | ||
| 43 | |||
| 44 | static struct tracer_flags blk_tracer_flags = { | ||
| 45 | .val = 0, | ||
| 46 | .opts = blk_tracer_opts, | ||
| 47 | }; | ||
| 48 | |||
| 49 | /* Global reference count of probes */ | ||
| 50 | static atomic_t blk_probes_ref = ATOMIC_INIT(0); | ||
| 51 | |||
| 52 | static void blk_register_tracepoints(void); | ||
| 53 | static void blk_unregister_tracepoints(void); | ||
| 54 | |||
| 55 | /* | ||
| 56 | * Send out a notify message. | ||
| 57 | */ | ||
| 58 | static void trace_note(struct blk_trace *bt, pid_t pid, int action, | ||
| 59 | const void *data, size_t len) | ||
| 60 | { | ||
| 61 | struct blk_io_trace *t; | ||
| 62 | struct ring_buffer_event *event = NULL; | ||
| 63 | int pc = 0; | ||
| 64 | int cpu = smp_processor_id(); | ||
| 65 | bool blk_tracer = blk_tracer_enabled; | ||
| 66 | |||
| 67 | if (blk_tracer) { | ||
| 68 | pc = preempt_count(); | ||
| 69 | event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK, | ||
| 70 | sizeof(*t) + len, | ||
| 71 | 0, pc); | ||
| 72 | if (!event) | ||
| 73 | return; | ||
| 74 | t = ring_buffer_event_data(event); | ||
| 75 | goto record_it; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (!bt->rchan) | ||
| 79 | return; | ||
| 80 | |||
| 81 | t = relay_reserve(bt->rchan, sizeof(*t) + len); | ||
| 82 | if (t) { | ||
| 83 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; | ||
| 84 | t->time = ktime_to_ns(ktime_get()); | ||
| 85 | record_it: | ||
| 86 | t->device = bt->dev; | ||
| 87 | t->action = action; | ||
| 88 | t->pid = pid; | ||
| 89 | t->cpu = cpu; | ||
| 90 | t->pdu_len = len; | ||
| 91 | memcpy((void *) t + sizeof(*t), data, len); | ||
| 92 | |||
| 93 | if (blk_tracer) | ||
| 94 | trace_buffer_unlock_commit(blk_tr, event, 0, pc); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | /* | ||
| 99 | * Send out a notify for this process, if we haven't done so since a trace | ||
| 100 | * started | ||
| 101 | */ | ||
| 102 | static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk) | ||
| 103 | { | ||
| 104 | tsk->btrace_seq = blktrace_seq; | ||
| 105 | trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm)); | ||
| 106 | } | ||
| 107 | |||
| 108 | static void trace_note_time(struct blk_trace *bt) | ||
| 109 | { | ||
| 110 | struct timespec now; | ||
| 111 | unsigned long flags; | ||
| 112 | u32 words[2]; | ||
| 113 | |||
| 114 | getnstimeofday(&now); | ||
| 115 | words[0] = now.tv_sec; | ||
| 116 | words[1] = now.tv_nsec; | ||
| 117 | |||
| 118 | local_irq_save(flags); | ||
| 119 | trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words)); | ||
| 120 | local_irq_restore(flags); | ||
| 121 | } | ||
| 122 | |||
| 123 | void __trace_note_message(struct blk_trace *bt, const char *fmt, ...) | ||
| 124 | { | ||
| 125 | int n; | ||
| 126 | va_list args; | ||
| 127 | unsigned long flags; | ||
| 128 | char *buf; | ||
| 129 | |||
| 130 | if (unlikely(bt->trace_state != Blktrace_running && | ||
| 131 | !blk_tracer_enabled)) | ||
| 132 | return; | ||
| 133 | |||
| 134 | local_irq_save(flags); | ||
| 135 | buf = per_cpu_ptr(bt->msg_data, smp_processor_id()); | ||
| 136 | va_start(args, fmt); | ||
| 137 | n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args); | ||
| 138 | va_end(args); | ||
| 139 | |||
| 140 | trace_note(bt, 0, BLK_TN_MESSAGE, buf, n); | ||
| 141 | local_irq_restore(flags); | ||
| 142 | } | ||
| 143 | EXPORT_SYMBOL_GPL(__trace_note_message); | ||
| 144 | |||
| 145 | static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector, | ||
| 146 | pid_t pid) | ||
| 147 | { | ||
| 148 | if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0) | ||
| 149 | return 1; | ||
| 150 | if (sector < bt->start_lba || sector > bt->end_lba) | ||
| 151 | return 1; | ||
| 152 | if (bt->pid && pid != bt->pid) | ||
| 153 | return 1; | ||
| 154 | |||
| 155 | return 0; | ||
| 156 | } | ||
| 157 | |||
| 158 | /* | ||
| 159 | * Data direction bit lookup | ||
| 160 | */ | ||
| 161 | static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ), | ||
| 162 | BLK_TC_ACT(BLK_TC_WRITE) }; | ||
| 163 | |||
| 164 | /* The ilog2() calls fall out because they're constant */ | ||
| 165 | #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \ | ||
| 166 | (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name)) | ||
| 167 | |||
| 168 | /* | ||
| 169 | * The worker for the various blk_add_trace*() types. Fills out a | ||
| 170 | * blk_io_trace structure and places it in a per-cpu subbuffer. | ||
| 171 | */ | ||
| 172 | static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, | ||
| 173 | int rw, u32 what, int error, int pdu_len, void *pdu_data) | ||
| 174 | { | ||
| 175 | struct task_struct *tsk = current; | ||
| 176 | struct ring_buffer_event *event = NULL; | ||
| 177 | struct blk_io_trace *t; | ||
| 178 | unsigned long flags = 0; | ||
| 179 | unsigned long *sequence; | ||
| 180 | pid_t pid; | ||
| 181 | int cpu, pc = 0; | ||
| 182 | bool blk_tracer = blk_tracer_enabled; | ||
| 183 | |||
| 184 | if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer)) | ||
| 185 | return; | ||
| 186 | |||
| 187 | what |= ddir_act[rw & WRITE]; | ||
| 188 | what |= MASK_TC_BIT(rw, BARRIER); | ||
| 189 | what |= MASK_TC_BIT(rw, SYNCIO); | ||
| 190 | what |= MASK_TC_BIT(rw, AHEAD); | ||
| 191 | what |= MASK_TC_BIT(rw, META); | ||
| 192 | what |= MASK_TC_BIT(rw, DISCARD); | ||
| 193 | |||
| 194 | pid = tsk->pid; | ||
| 195 | if (unlikely(act_log_check(bt, what, sector, pid))) | ||
| 196 | return; | ||
| 197 | cpu = raw_smp_processor_id(); | ||
| 198 | |||
| 199 | if (blk_tracer) { | ||
| 200 | tracing_record_cmdline(current); | ||
| 201 | |||
| 202 | pc = preempt_count(); | ||
| 203 | event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK, | ||
| 204 | sizeof(*t) + pdu_len, | ||
| 205 | 0, pc); | ||
| 206 | if (!event) | ||
| 207 | return; | ||
| 208 | t = ring_buffer_event_data(event); | ||
| 209 | goto record_it; | ||
| 210 | } | ||
| 211 | |||
| 212 | /* | ||
| 213 | * A word about the locking here - we disable interrupts to reserve | ||
| 214 | * some space in the relay per-cpu buffer, to prevent an irq | ||
| 215 | * from coming in and stepping on our toes. | ||
| 216 | */ | ||
| 217 | local_irq_save(flags); | ||
