diff options
Diffstat (limited to 'block/blktrace.c')
-rw-r--r-- | block/blktrace.c | 656 |
1 files changed, 651 insertions, 5 deletions
diff --git a/block/blktrace.c b/block/blktrace.c index 39cc3bfe56e4..3f25425ade12 100644 --- a/block/blktrace.c +++ b/block/blktrace.c | |||
@@ -25,9 +25,27 @@ | |||
25 | #include <linux/time.h> | 25 | #include <linux/time.h> |
26 | #include <trace/block.h> | 26 | #include <trace/block.h> |
27 | #include <asm/uaccess.h> | 27 | #include <asm/uaccess.h> |
28 | #include <../kernel/trace/trace_output.h> | ||
28 | 29 | ||
29 | static unsigned int blktrace_seq __read_mostly = 1; | 30 | static unsigned int blktrace_seq __read_mostly = 1; |
30 | 31 | ||
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 */ | ||
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 | |||
31 | /* Global reference count of probes */ | 49 | /* Global reference count of probes */ |
32 | static DEFINE_MUTEX(blk_probe_mutex); | 50 | static DEFINE_MUTEX(blk_probe_mutex); |
33 | static atomic_t blk_probes_ref = ATOMIC_INIT(0); | 51 | static atomic_t blk_probes_ref = ATOMIC_INIT(0); |
@@ -43,6 +61,9 @@ static void trace_note(struct blk_trace *bt, pid_t pid, int action, | |||
43 | { | 61 | { |
44 | struct blk_io_trace *t; | 62 | struct blk_io_trace *t; |
45 | 63 | ||
64 | if (!bt->rchan) | ||
65 | return; | ||
66 | |||
46 | t = relay_reserve(bt->rchan, sizeof(*t) + len); | 67 | t = relay_reserve(bt->rchan, sizeof(*t) + len); |
47 | if (t) { | 68 | if (t) { |
48 | const int cpu = smp_processor_id(); | 69 | const int cpu = smp_processor_id(); |
@@ -90,6 +111,16 @@ void __trace_note_message(struct blk_trace *bt, const char *fmt, ...) | |||
90 | unsigned long flags; | 111 | unsigned long flags; |
91 | char *buf; | 112 | char *buf; |
92 | 113 | ||
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 | |||
93 | local_irq_save(flags); | 124 | local_irq_save(flags); |
94 | buf = per_cpu_ptr(bt->msg_data, smp_processor_id()); | 125 | buf = per_cpu_ptr(bt->msg_data, smp_processor_id()); |
95 | va_start(args, fmt); | 126 | va_start(args, fmt); |
@@ -131,13 +162,15 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, | |||
131 | int rw, u32 what, int error, int pdu_len, void *pdu_data) | 162 | int rw, u32 what, int error, int pdu_len, void *pdu_data) |
132 | { | 163 | { |
133 | struct task_struct *tsk = current; | 164 | struct task_struct *tsk = current; |
165 | struct ring_buffer_event *event = NULL; | ||
134 | struct blk_io_trace *t; | 166 | struct blk_io_trace *t; |
135 | unsigned long flags; | 167 | unsigned long flags; |
136 | unsigned long *sequence; | 168 | unsigned long *sequence; |
137 | pid_t pid; | 169 | pid_t pid; |
138 | int cpu; | 170 | int cpu, pc = 0; |
139 | 171 | ||
140 | if (unlikely(bt->trace_state != Blktrace_running)) | 172 | if (unlikely(bt->trace_state != Blktrace_running || |
173 | !blk_tracer_enabled)) | ||
141 | return; | 174 | return; |
142 | 175 | ||
143 | what |= ddir_act[rw & WRITE]; | 176 | what |= ddir_act[rw & WRITE]; |
@@ -150,6 +183,24 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, | |||
150 | pid = tsk->pid; | 183 | pid = tsk->pid; |
151 | if (unlikely(act_log_check(bt, what, sector, pid))) | 184 | if (unlikely(act_log_check(bt, what, sector, pid))) |
152 | return; | 185 | return; |
186 | cpu = raw_smp_processor_id(); | ||
187 | |||
188 | if (blk_tr) { | ||
189 | struct trace_entry *ent; | ||
190 | tracing_record_cmdline(current); | ||
191 | |||
192 | event = ring_buffer_lock_reserve(blk_tr->buffer, | ||
193 | sizeof(*t) + pdu_len, &flags); | ||
194 | if (!event) | ||
195 | return; | ||
196 | |||
197 | ent = ring_buffer_event_data(event); | ||
198 | t = (struct blk_io_trace *)ent; | ||
199 | pc = preempt_count(); | ||
200 | tracing_generic_entry_update(ent, 0, pc); | ||
201 | ent->type = TRACE_BLK; | ||
202 | goto record_it; | ||
203 | } | ||
153 | 204 | ||
154 | /* | 205 | /* |
155 | * A word about the locking here - we disable interrupts to reserve | 206 | * A word about the locking here - we disable interrupts to reserve |
@@ -163,23 +214,33 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes, | |||
163 | 214 | ||
164 | t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len); | 215 | t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len); |
165 | if (t) { | 216 | if (t) { |
166 | cpu = smp_processor_id(); | ||
167 | sequence = per_cpu_ptr(bt->sequence, cpu); | 217 | sequence = per_cpu_ptr(bt->sequence, cpu); |
168 | 218 | ||
169 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; | 219 | t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION; |
170 | t->sequence = ++(*sequence); | 220 | t->sequence = ++(*sequence); |
171 | t->time = ktime_to_ns(ktime_get()); | 221 | t->time = ktime_to_ns(ktime_get()); |
222 | t->cpu = cpu; | ||
223 | t->pid = pid; | ||
224 | record_it: | ||
172 | t->sector = sector; | 225 | t->sector = sector; |
173 | t->bytes = bytes; | 226 | t->bytes = bytes; |
174 | t->action = what; | 227 | t->action = what; |
175 | t->pid = pid; | ||
176 | t->device = bt->dev; | 228 | t->device = bt->dev; |
177 | t->cpu = cpu; | ||
178 | t->error = error; | 229 | t->error = error; |
179 | t->pdu_len = pdu_len; | 230 | t->pdu_len = pdu_len; |
180 | 231 | ||
181 | if (pdu_len) | 232 | if (pdu_len) |
182 | memcpy((void *) t + sizeof(*t), pdu_data, pdu_len); | 233 | memcpy((void *) t + sizeof(*t), pdu_data, pdu_len); |
234 | |||
235 | if (blk_tr) { | ||
236 | ring_buffer_unlock_commit(blk_tr->buffer, event, flags); | ||
237 | if (pid != 0 && | ||
238 | !(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC) && | ||
239 | (trace_flags & TRACE_ITER_STACKTRACE) != 0) | ||
240 | __trace_stack(blk_tr, NULL, flags, 5, pc); | ||
241 | trace_wake_up(); | ||
242 | return; | ||
243 | } | ||
183 | } | 244 | } |
184 | 245 | ||
185 | local_irq_restore(flags); | 246 | local_irq_restore(flags); |
@@ -858,3 +919,588 @@ static void blk_unregister_tracepoints(void) | |||
858 | 919 | ||
859 | tracepoint_synchronize_unregister(); | 920 | tracepoint_synchronize_unregister(); |
860 | } | 921 | } |
922 | |||
923 | /* | ||
924 | * struct blk_io_tracer formatting routines | ||
925 | */ | ||
926 | |||
927 | static void fill_rwbs(char *rwbs, const struct blk_io_trace *t) | ||
928 | { | ||
929 | int i = 0; | ||
930 | |||
931 | if (t->action & BLK_TC_DISCARD) | ||
932 | rwbs[i++] = 'D'; | ||
933 | else if (t->action & BLK_TC_WRITE) | ||
934 | rwbs[i++] = 'W'; | ||
935 | else if (t->bytes) | ||
936 | rwbs[i++] = 'R'; | ||
937 | else | ||
938 | rwbs[i++] = 'N'; | ||
939 | |||
940 | if (t->action & BLK_TC_AHEAD) | ||
941 | rwbs[i++] = 'A'; | ||
942 | if (t->action & BLK_TC_BARRIER) | ||
943 | rwbs[i++] = 'B'; | ||
944 | if (t->action & BLK_TC_SYNC) | ||
945 | rwbs[i++] = 'S'; | ||
946 | if (t->action & BLK_TC_META) | ||
947 | rwbs[i++] = 'M'; | ||
948 | |||
949 | rwbs[i] = '\0'; | ||
950 | } | ||
951 | |||
952 | static inline | ||
953 | const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent) | ||
954 | { | ||
955 | return (const struct blk_io_trace *)ent; | ||
956 | } | ||
957 | |||
958 | static inline const void *pdu_start(const struct trace_entry *ent) | ||
959 | { | ||
960 | return te_blk_io_trace(ent) + 1; | ||
961 | } | ||
962 | |||
963 | static inline u32 t_sec(const struct trace_entry *ent) | ||
964 | { | ||
965 | return te_blk_io_trace(ent)->bytes >> 9; | ||
966 | } | ||
967 | |||
968 | static inline unsigned long long t_sector(const struct trace_entry *ent) | ||
969 | { | ||
970 | return te_blk_io_trace(ent)->sector; | ||
971 | } | ||
972 | |||
973 | static inline __u16 t_error(const struct trace_entry *ent) | ||
974 | { | ||
975 | return te_blk_io_trace(ent)->sector; | ||
976 | } | ||
977 | |||
978 | static __u64 get_pdu_int(const struct trace_entry *ent) | ||
979 | { | ||
980 | const __u64 *val = pdu_start(ent); | ||
981 | return be64_to_cpu(*val); | ||
982 | } | ||
983 | |||
984 | static void get_pdu_remap(const struct trace_entry *ent, | ||
985 | struct blk_io_trace_remap *r) | ||
986 | { | ||
987 | const struct blk_io_trace_remap *__r = pdu_start(ent); | ||
988 | __u64 sector = __r->sector; | ||
989 | |||
990 | r->device = be32_to_cpu(__r->device); | ||
991 | r->device_from = be32_to_cpu(__r->device_from); | ||
992 | r->sector = be64_to_cpu(sector); | ||
993 | } | ||
994 | |||
995 | static int blk_log_action_iter(struct trace_iterator *iter, const char *act) | ||
996 | { | ||
997 | char rwbs[6]; | ||
998 | unsigned long long ts = ns2usecs(iter->ts); | ||
999 | unsigned long usec_rem = do_div(ts, USEC_PER_SEC); | ||
1000 | unsigned secs = (unsigned long)ts; | ||
1001 | const struct trace_entry *ent = iter->ent; | ||
1002 | const struct blk_io_trace *t = (const struct blk_io_trace *)ent; | ||
1003 | |||
1004 | fill_rwbs(rwbs, t); | ||
1005 | |||
1006 | return trace_seq_printf(&iter->seq, | ||
1007 | "%3d,%-3d %2d %5d.%06lu %5u %2s %3s ", | ||
1008 | MAJOR(t->device), MINOR(t->device), iter->cpu, | ||
1009 | secs, usec_rem, ent->pid, act, rwbs); | ||
1010 | } | ||
1011 | |||
1012 | static int blk_log_action_seq(struct trace_seq *s, const struct blk_io_trace *t, | ||
1013 | const char *act) | ||
1014 | { | ||
1015 | char rwbs[6]; | ||
1016 | fill_rwbs(rwbs, t); | ||
1017 | return trace_seq_printf(s, "%3d,%-3d %2s %3s ", | ||
1018 | MAJOR(t->device), MINOR(t->device), act, rwbs); | ||
1019 | } | ||
1020 | |||
1021 | static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent) | ||
1022 | { | ||
1023 | const char *cmd = trace_find_cmdline(ent->pid); | ||
1024 | |||
1025 | if (t_sec(ent)) | ||
1026 | return trace_seq_printf(s, "%llu + %u [%s]\n", | ||
1027 | t_sector(ent), t_sec(ent), cmd); | ||
1028 | return trace_seq_printf(s, "[%s]\n", cmd); | ||
1029 | } | ||
1030 | |||
1031 | static int blk_log_with_error(struct trace_seq *s, | ||
1032 | const struct trace_entry *ent) | ||
1033 | { | ||
1034 | if (t_sec(ent)) | ||
1035 | return trace_seq_printf(s, "%llu + %u [%d]\n", t_sector(ent), | ||
1036 | t_sec(ent), t_error(ent)); | ||
1037 | return trace_seq_printf(s, "%llu [%d]\n", t_sector(ent), t_error(ent)); | ||
1038 | } | ||
1039 | |||
1040 | static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent) | ||
1041 | { | ||
1042 | struct blk_io_trace_remap r = { .device = 0, }; | ||
1043 | |||
1044 | get_pdu_remap(ent, &r); | ||
1045 | return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n", | ||
1046 | t_sector(ent), | ||
1047 | t_sec(ent), MAJOR(r.device), MINOR(r.device), | ||
1048 | (unsigned long long)r.sector); | ||
1049 | } | ||
1050 | |||
1051 | static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent) | ||
1052 | { | ||
1053 | return trace_seq_printf(s, "[%s]\n", trace_find_cmdline(ent->pid)); | ||
1054 | } | ||
1055 | |||
1056 | static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent) | ||
1057 | { | ||
1058 | return trace_seq_printf(s, "[%s] %llu\n", trace_find_cmdline(ent->pid), | ||
1059 | get_pdu_int(ent)); | ||
1060 | } | ||
1061 | |||
1062 | static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent) | ||
1063 | { | ||
1064 | return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent), | ||
1065 | get_pdu_int(ent), trace_find_cmdline(ent->pid)); | ||
1066 | } | ||
1067 | |||
1068 | /* | ||
1069 | * struct tracer operations | ||
1070 | */ | ||
1071 | |||
1072 | static void blk_tracer_print_header(struct seq_file *m) | ||
1073 | { | ||
1074 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) | ||
1075 | return; | ||
1076 | seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n" | ||
1077 | "# | | | | | |\n"); | ||
1078 | } | ||
1079 | |||
1080 | static void blk_tracer_start(struct trace_array *tr) | ||
1081 | { | ||
1082 | tracing_reset_online_cpus(tr); | ||
1083 | |||
1084 | mutex_lock(&blk_probe_mutex); | ||
1085 | if (atomic_add_return(1, &blk_probes_ref) == 1) | ||
1086 | if (blk_register_tracepoints()) | ||
1087 | atomic_dec(&blk_probes_ref); | ||
1088 | mutex_unlock(&blk_probe_mutex); | ||
1089 | } | ||
1090 | |||
1091 | static int blk_tracer_init(struct trace_array *tr) | ||
1092 | { | ||
1093 | blk_tr = tr; | ||
1094 | blk_tracer_start(tr); | ||
1095 | mutex_lock(&blk_probe_mutex); | ||
1096 | blk_tracer_enabled++; | ||
1097 | mutex_unlock(&blk_probe_mutex); | ||
1098 | return 0; | ||
1099 | } | ||
1100 | |||
1101 | static void blk_tracer_stop(struct trace_array *tr) | ||
1102 | { | ||
1103 | mutex_lock(&blk_probe_mutex); | ||
1104 | if (atomic_dec_and_test(&blk_probes_ref)) | ||
1105 | blk_unregister_tracepoints(); | ||
1106 | mutex_unlock(&blk_probe_mutex); | ||
1107 | } | ||
1108 | |||
1109 | static void blk_tracer_reset(struct trace_array *tr) | ||
1110 | { | ||
1111 | if (!atomic_read(&blk_probes_ref)) | ||
1112 | return; | ||
1113 | |||
1114 | mutex_lock(&blk_probe_mutex); | ||
1115 | blk_tracer_enabled--; | ||
1116 | WARN_ON(blk_tracer_enabled < 0); | ||
1117 | mutex_unlock(&blk_probe_mutex); | ||
1118 | |||
1119 | blk_tracer_stop(tr); | ||
1120 | } | ||
1121 | |||
1122 | static struct { | ||
1123 | const char *act[2]; | ||
1124 | int (*print)(struct trace_seq *s, const struct trace_entry *ent); | ||
1125 | } what2act[] __read_mostly = { | ||
1126 | [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic }, | ||
1127 | [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic }, | ||
1128 | [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic }, | ||
1129 | [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic }, | ||
1130 | [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic }, | ||
1131 | [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error }, | ||
1132 | [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic }, | ||
1133 | [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error }, | ||
1134 | [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug }, | ||
1135 | [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug }, | ||
1136 | [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug }, | ||
1137 | [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic }, | ||
1138 | [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split }, | ||
1139 | [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic }, | ||
1140 | [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap }, | ||
1141 | }; | ||
1142 | |||
1143 | static int blk_trace_event_print(struct trace_seq *s, struct trace_entry *ent, | ||
1144 | int flags) | ||
1145 | { | ||
1146 | const struct blk_io_trace *t = (struct blk_io_trace *)ent; | ||
1147 | const u16 what = t->action & ((1 << BLK_TC_SHIFT) - 1); | ||
1148 | int ret; | ||
1149 | |||
1150 | if (unlikely(what == 0 || what > ARRAY_SIZE(what2act))) | ||
1151 | ret = trace_seq_printf(s, "Bad pc action %x\n", what); | ||
1152 | else { | ||
1153 | const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE); | ||
1154 | ret = blk_log_action_seq(s, t, what2act[what].act[long_act]); | ||
1155 | if (ret) | ||
1156 | ret = what2act[what].print(s, ent); | ||
1157 | } | ||
1158 | |||
1159 | return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE; | ||
1160 | } | ||
1161 | |||
1162 | static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter) | ||
1163 | { | ||
1164 | const struct blk_io_trace *t; | ||
1165 | u16 what; | ||
1166 | int ret; | ||
1167 | |||
1168 | if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC)) | ||
1169 | return TRACE_TYPE_UNHANDLED; | ||
1170 | |||
1171 | t = (const struct blk_io_trace *)iter->ent; | ||
1172 | what = t->action & ((1 << BLK_TC_SHIFT) - 1); | ||
1173 | |||
1174 | if (unlikely(what == 0 || what > ARRAY_SIZE(what2act))) | ||
1175 | ret = trace_seq_printf(&iter->seq, "Bad pc action %x\n", what); | ||
1176 | else { | ||
1177 | const bool long_act = !!(trace_flags & TRACE_ITER_VERBOSE); | ||
1178 | ret = blk_log_action_iter(iter, what2act[what].act[long_act]); | ||
1179 | if (ret) | ||
1180 | ret = what2act[what].print(&iter->seq, iter->ent); | ||
1181 | } | ||
1182 | |||
1183 | return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE; | ||
1184 | } | ||
1185 | |||
1186 | static struct tracer blk_tracer __read_mostly = { | ||
1187 | .name = "blk", | ||
1188 | .init = blk_tracer_init, | ||
1189 | .reset = blk_tracer_reset, | ||
1190 | .start = blk_tracer_start, | ||
1191 | .stop = blk_tracer_stop, | ||
1192 | .print_header = blk_tracer_print_header, | ||
1193 | .print_line = blk_tracer_print_line, | ||
1194 | .flags = &blk_tracer_flags, | ||
1195 | }; | ||
1196 | |||
1197 | static struct trace_event trace_blk_event = { | ||
1198 | .type = TRACE_BLK, | ||
1199 | .trace = blk_trace_event_print, | ||
1200 | .latency_trace = blk_trace_event_print, | ||
1201 | .raw = trace_nop_print, | ||
1202 | .hex = trace_nop_print, | ||
1203 | .binary = trace_nop_print, | ||
1204 | }; | ||
1205 | |||
1206 | static int __init init_blk_tracer(void) | ||
1207 | { | ||
1208 | if (!register_ftrace_event(&trace_blk_event)) { | ||
1209 | pr_warning("Warning: could not register block events\n"); | ||
1210 | return 1; | ||
1211 | } | ||
1212 | |||
1213 | if (register_tracer(&blk_tracer) != 0) { | ||
1214 | pr_warning("Warning: could not register the block tracer\n"); | ||
1215 | unregister_ftrace_event(&trace_blk_event); | ||
1216 | return 1; | ||
1217 | } | ||
1218 | |||
1219 | return 0; | ||
1220 | } | ||
1221 | |||
1222 | device_initcall(init_blk_tracer); | ||
1223 | |||
1224 | static int blk_trace_remove_queue(struct request_queue *q) | ||
1225 | { | ||
1226 | struct blk_trace *bt; | ||
1227 | |||
1228 | bt = xchg(&q->blk_trace, NULL); | ||
1229 | if (bt == NULL) | ||
1230 | return -EINVAL; | ||
1231 | |||
1232 | kfree(bt); | ||
1233 | return 0; | ||
1234 | } | ||
1235 | |||
1236 | /* | ||
1237 | * Setup everything required to start tracing | ||
1238 | */ | ||
1239 | static int blk_trace_setup_queue(struct request_queue *q, dev_t dev) | ||
1240 | { | ||
1241 | struct blk_trace *old_bt, *bt = NULL; | ||
1242 | int ret; | ||
1243 | |||
1244 | ret = -ENOMEM; | ||
1245 | bt = kzalloc(sizeof(*bt), GFP_KERNEL); | ||
1246 | if (!bt) | ||
1247 | goto err; | ||
1248 | |||
1249 | bt->dev = dev; | ||
1250 | bt->act_mask = (u16)-1; | ||
1251 | bt->end_lba = -1ULL; | ||
1252 | bt->trace_state = Blktrace_running; | ||
1253 | |||
1254 | old_bt = xchg(&q->blk_trace, bt); | ||
1255 | if (old_bt != NULL) { | ||
1256 | (void)xchg(&q->blk_trace, old_bt); | ||
1257 | kfree(bt); | ||
1258 | ret = -EBUSY; | ||
1259 | } | ||
1260 | return 0; | ||
1261 | err: | ||
1262 | return ret; | ||
1263 | } | ||
1264 | |||
1265 | /* | ||
1266 | * sysfs interface to enable and configure tracing | ||
1267 | */ | ||
1268 | |||
1269 | static ssize_t sysfs_blk_trace_enable_show(struct device *dev, | ||
1270 | struct device_attribute *attr, | ||
1271 | char *buf) | ||
1272 | { | ||
1273 | struct hd_struct *p = dev_to_part(dev); | ||
1274 | struct block_device *bdev; | ||
1275 | ssize_t ret = -ENXIO; | ||
1276 | |||
1277 | lock_kernel(); | ||
1278 | bdev = bdget(part_devt(p)); | ||
1279 | if (bdev != NULL) { | ||
1280 | struct request_queue *q = bdev_get_queue(bdev); | ||
1281 | |||
1282 | if (q != NULL) { | ||
1283 | mutex_lock(&bdev->bd_mutex); | ||
1284 | ret = sprintf(buf, "%u\n", !!q->blk_trace); | ||
1285 | mutex_unlock(&bdev->bd_mutex); | ||
1286 | } | ||
1287 | |||
1288 | bdput(bdev); | ||
1289 | } | ||
1290 | |||
1291 | unlock_kernel(); | ||
1292 | return ret; | ||
1293 | } | ||
1294 | |||
1295 | static ssize_t sysfs_blk_trace_enable_store(struct device *dev, | ||
1296 | struct device_attribute *attr, | ||
1297 | const char *buf, size_t count) | ||
1298 | { | ||
1299 | struct block_device *bdev; | ||
1300 | struct request_queue *q; | ||
1301 | struct hd_struct *p; | ||
1302 | int value; | ||
1303 | ssize_t ret = -ENXIO; | ||
1304 | |||
1305 | if (count == 0 || sscanf(buf, "%d", &value) != 1) | ||
1306 | goto out; | ||
1307 | |||
1308 | lock_kernel(); | ||
1309 | p = dev_to_part(dev); | ||
1310 | bdev = bdget(part_devt(p)); | ||
1311 | if (bdev == NULL) | ||
1312 | goto out_unlock_kernel; | ||
1313 | |||
1314 | q = bdev_get_queue(bdev); | ||
1315 | if (q == NULL) | ||
1316 | goto out_bdput; | ||
1317 | |||
1318 | mutex_lock(&bdev->bd_mutex); | ||
1319 | if (value) | ||
1320 | ret = blk_trace_setup_queue(q, bdev->bd_dev); | ||
1321 | else | ||
1322 | ret = blk_trace_remove_queue(q); | ||
1323 | mutex_unlock(&bdev->bd_mutex); | ||
1324 | |||
1325 | if (ret == 0) | ||
1326 | ret = count; | ||
1327 | out_bdput: | ||
1328 | bdput(bdev); | ||
1329 | out_unlock_kernel: | ||
1330 | unlock_kernel(); | ||
1331 | out: | ||
1332 | return ret; | ||
1333 | } | ||
1334 | |||
1335 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, | ||
1336 | struct device_attribute *attr, | ||
1337 | char *buf); | ||
1338 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, | ||
1339 | struct device_attribute *attr, | ||
1340 | const char *buf, size_t count); | ||
1341 | #define BLK_TRACE_DEVICE_ATTR(_name) \ | ||
1342 | DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \ | ||
1343 | sysfs_blk_trace_attr_show, \ | ||
1344 | sysfs_blk_trace_attr_store) | ||
1345 | |||
1346 | static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, | ||
1347 | sysfs_blk_trace_enable_show, sysfs_blk_trace_enable_store); | ||
1348 | static BLK_TRACE_DEVICE_ATTR(act_mask); | ||
1349 | static BLK_TRACE_DEVICE_ATTR(pid); | ||
1350 | static BLK_TRACE_DEVICE_ATTR(start_lba); | ||
1351 | static BLK_TRACE_DEVICE_ATTR(end_lba); | ||
1352 | |||
1353 | static struct attribute *blk_trace_attrs[] = { | ||
1354 | &dev_attr_enable.attr, | ||
1355 | &dev_attr_act_mask.attr, | ||
1356 | &dev_attr_pid.attr, | ||
1357 | &dev_attr_start_lba.attr, | ||
1358 | &dev_attr_end_lba.attr, | ||
1359 | NULL | ||
1360 | }; | ||
1361 | |||
1362 | struct attribute_group blk_trace_attr_group = { | ||
1363 | .name = "trace", | ||
1364 | .attrs = blk_trace_attrs, | ||
1365 | }; | ||
1366 | |||
1367 | static int blk_str2act_mask(const char *str) | ||
1368 | { | ||
1369 | int mask = 0; | ||
1370 | char *copy = kstrdup(str, GFP_KERNEL), *s; | ||
1371 | |||
1372 | if (copy == NULL) | ||
1373 | return -ENOMEM; | ||
1374 | |||
1375 | s = strstrip(copy); | ||
1376 | |||
1377 | while (1) { | ||
1378 | char *sep = strchr(s, ','); | ||
1379 | |||
1380 | if (sep != NULL) | ||
1381 | *sep = '\0'; | ||
1382 | |||
1383 | if (strcasecmp(s, "barrier") == 0) | ||
1384 | mask |= BLK_TC_BARRIER; | ||
1385 | else if (strcasecmp(s, "complete") == 0) | ||
1386 | mask |= BLK_TC_COMPLETE; | ||
1387 | else if (strcasecmp(s, "fs") == 0) | ||
1388 | mask |= BLK_TC_FS; | ||
1389 | else if (strcasecmp(s, "issue") == 0) | ||
1390 | mask |= BLK_TC_ISSUE; | ||
1391 | else if (strcasecmp(s, "pc") == 0) | ||
1392 | mask |= BLK_TC_PC; | ||
1393 | else if (strcasecmp(s, "queue") == 0) | ||
1394 | mask |= BLK_TC_QUEUE; | ||
1395 | else if (strcasecmp(s, "read") == 0) | ||
1396 | mask |= BLK_TC_READ; | ||
1397 | else if (strcasecmp(s, "requeue") == 0) | ||
1398 | mask |= BLK_TC_REQUEUE; | ||
1399 | else if (strcasecmp(s, "sync") == 0) | ||
1400 | mask |= BLK_TC_SYNC; | ||
1401 | else if (strcasecmp(s, "write") == 0) | ||
1402 | mask |= BLK_TC_WRITE; | ||
1403 | |||
1404 | if (sep == NULL) | ||
1405 | break; | ||
1406 | |||
1407 | s = sep + 1; | ||
1408 | } | ||
1409 | kfree(copy); | ||
1410 | |||
1411 | return mask; | ||
1412 | } | ||
1413 | |||
1414 | static ssize_t sysfs_blk_trace_attr_show(struct device *dev, | ||
1415 | struct device_attribute *attr, | ||
1416 | char *buf) | ||
1417 | { | ||
1418 | struct hd_struct *p = dev_to_part(dev); | ||
1419 | struct request_queue *q; | ||
1420 | struct block_device *bdev; | ||
1421 | ssize_t ret = -ENXIO; | ||
1422 | |||
1423 | lock_kernel(); | ||
1424 | bdev = bdget(part_devt(p)); | ||
1425 | if (bdev == NULL) | ||
1426 | goto out_unlock_kernel; | ||
1427 | |||
1428 | q = bdev_get_queue(bdev); | ||
1429 | if (q == NULL) | ||
1430 | goto out_bdput; | ||
1431 | mutex_lock(&bdev->bd_mutex); | ||
1432 | if (q->blk_trace == NULL) | ||
1433 | ret = sprintf(buf, "disabled\n"); | ||
1434 | else if (attr == &dev_attr_act_mask) | ||
1435 | ret = sprintf(buf, "%#x\n", q->blk_trace->act_mask); | ||
1436 | else if (attr == &dev_attr_pid) | ||
1437 | ret = sprintf(buf, "%u\n", q->blk_trace->pid); | ||
1438 | else if (attr == &dev_attr_start_lba) | ||
1439 | ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba); | ||
1440 | else if (attr == &dev_attr_end_lba) | ||
1441 | ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba); | ||
1442 | mutex_unlock(&bdev->bd_mutex); | ||
1443 | out_bdput: | ||
1444 | bdput(bdev); | ||
1445 | out_unlock_kernel: | ||
1446 | unlock_kernel(); | ||
1447 | return ret; | ||
1448 | } | ||
1449 | |||
1450 | static ssize_t sysfs_blk_trace_attr_store(struct device *dev, | ||
1451 | struct device_attribute *attr, | ||
1452 | const char *buf, size_t count) | ||
1453 | { | ||
1454 | struct block_device *bdev; | ||
1455 | struct request_queue *q; | ||
1456 | struct hd_struct *p; | ||
1457 | u64 value; | ||
1458 | ssize_t ret = -ENXIO; | ||
1459 | |||
1460 | if (count == 0) | ||
1461 | goto out; | ||
1462 | |||
1463 | if (attr == &dev_attr_act_mask) { | ||
1464 | if (sscanf(buf, "%llx", &value) != 1) { | ||
1465 | /* Assume it is a list of trace category names */ | ||
1466 | value = blk_str2act_mask(buf); | ||
1467 | if (value < 0) | ||
1468 | goto out; | ||
1469 | } | ||
1470 | } else if (sscanf(buf, "%llu", &value) != 1) | ||
1471 | goto out; | ||
1472 | |||
1473 | lock_kernel(); | ||
1474 | p = dev_to_part(dev); | ||
1475 | bdev = bdget(part_devt(p)); | ||
1476 | if (bdev == NULL) | ||
1477 | goto out_unlock_kernel; | ||
1478 | |||
1479 | q = bdev_get_queue(bdev); | ||
1480 | if (q == NULL) | ||
1481 | goto out_bdput; | ||
1482 | |||
1483 | mutex_lock(&bdev->bd_mutex); | ||
1484 | ret = 0; | ||
1485 | if (q->blk_trace == NULL) | ||
1486 | ret = blk_trace_setup_queue(q, bdev->bd_dev); | ||
1487 | |||
1488 | if (ret == 0) { | ||
1489 | if (attr == &dev_attr_act_mask) | ||
1490 | q->blk_trace->act_mask = value; | ||
1491 | else if (attr == &dev_attr_pid) | ||
1492 | q->blk_trace->pid = value; | ||
1493 | else if (attr == &dev_attr_start_lba) | ||
1494 | q->blk_trace->start_lba = value; | ||
1495 | else if (attr == &dev_attr_end_lba) | ||
1496 | q->blk_trace->end_lba = value; | ||
1497 | ret = count; | ||
1498 | } | ||
1499 | mutex_unlock(&bdev->bd_mutex); | ||
1500 | out_bdput: | ||
1501 | bdput(bdev); | ||
1502 | out_unlock_kernel: | ||
1503 | unlock_kernel(); | ||
1504 | out: | ||
1505 | return ret; | ||
1506 | } | ||