Commit | Line | Data |
---|---|---|
16444a8a ACM |
1 | /* |
2 | * Infrastructure for profiling code inserted by 'gcc -pg'. | |
3 | * | |
4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> | |
5 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * Originally ported from the -rt patch by: | |
8 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> | |
9 | * | |
10 | * Based on code in the latency_tracer, that is: | |
11 | * | |
12 | * Copyright (C) 2004-2006 Ingo Molnar | |
13 | * Copyright (C) 2004 William Lee Irwin III | |
14 | */ | |
15 | ||
3d083395 SR |
16 | #include <linux/stop_machine.h> |
17 | #include <linux/clocksource.h> | |
18 | #include <linux/kallsyms.h> | |
5072c59f SR |
19 | #include <linux/seq_file.h> |
20 | #include <linux/debugfs.h> | |
3d083395 | 21 | #include <linux/hardirq.h> |
2d8b820b | 22 | #include <linux/kthread.h> |
5072c59f | 23 | #include <linux/uaccess.h> |
f22f9a89 | 24 | #include <linux/kprobes.h> |
2d8b820b | 25 | #include <linux/ftrace.h> |
b0fc494f | 26 | #include <linux/sysctl.h> |
5072c59f | 27 | #include <linux/ctype.h> |
3d083395 SR |
28 | #include <linux/list.h> |
29 | ||
395a59d0 AS |
30 | #include <asm/ftrace.h> |
31 | ||
3d083395 | 32 | #include "trace.h" |
16444a8a | 33 | |
6912896e SR |
34 | #define FTRACE_WARN_ON(cond) \ |
35 | do { \ | |
36 | if (WARN_ON(cond)) \ | |
37 | ftrace_kill(); \ | |
38 | } while (0) | |
39 | ||
40 | #define FTRACE_WARN_ON_ONCE(cond) \ | |
41 | do { \ | |
42 | if (WARN_ON_ONCE(cond)) \ | |
43 | ftrace_kill(); \ | |
44 | } while (0) | |
45 | ||
4eebcc81 SR |
46 | /* ftrace_enabled is a method to turn ftrace on or off */ |
47 | int ftrace_enabled __read_mostly; | |
d61f82d0 | 48 | static int last_ftrace_enabled; |
b0fc494f | 49 | |
4eebcc81 SR |
50 | /* |
51 | * ftrace_disabled is set when an anomaly is discovered. | |
52 | * ftrace_disabled is much stronger than ftrace_enabled. | |
53 | */ | |
54 | static int ftrace_disabled __read_mostly; | |
55 | ||
3d083395 | 56 | static DEFINE_SPINLOCK(ftrace_lock); |
b0fc494f SR |
57 | static DEFINE_MUTEX(ftrace_sysctl_lock); |
58 | ||
16444a8a ACM |
59 | static struct ftrace_ops ftrace_list_end __read_mostly = |
60 | { | |
61 | .func = ftrace_stub, | |
62 | }; | |
63 | ||
64 | static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end; | |
65 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; | |
66 | ||
f2252935 | 67 | static void ftrace_list_func(unsigned long ip, unsigned long parent_ip) |
16444a8a ACM |
68 | { |
69 | struct ftrace_ops *op = ftrace_list; | |
70 | ||
71 | /* in case someone actually ports this to alpha! */ | |
72 | read_barrier_depends(); | |
73 | ||
74 | while (op != &ftrace_list_end) { | |
75 | /* silly alpha */ | |
76 | read_barrier_depends(); | |
77 | op->func(ip, parent_ip); | |
78 | op = op->next; | |
79 | }; | |
80 | } | |
81 | ||
82 | /** | |
3d083395 | 83 | * clear_ftrace_function - reset the ftrace function |
16444a8a | 84 | * |
3d083395 SR |
85 | * This NULLs the ftrace function and in essence stops |
86 | * tracing. There may be lag | |
16444a8a | 87 | */ |
3d083395 | 88 | void clear_ftrace_function(void) |
16444a8a | 89 | { |
3d083395 SR |
90 | ftrace_trace_function = ftrace_stub; |
91 | } | |
92 | ||
e309b41d | 93 | static int __register_ftrace_function(struct ftrace_ops *ops) |
3d083395 | 94 | { |
99ecdc43 | 95 | /* should not be called from interrupt context */ |
3d083395 | 96 | spin_lock(&ftrace_lock); |
16444a8a | 97 | |
16444a8a ACM |
98 | ops->next = ftrace_list; |
99 | /* | |
100 | * We are entering ops into the ftrace_list but another | |
101 | * CPU might be walking that list. We need to make sure | |
102 | * the ops->next pointer is valid before another CPU sees | |
103 | * the ops pointer included into the ftrace_list. | |
104 | */ | |
105 | smp_wmb(); | |
106 | ftrace_list = ops; | |
3d083395 | 107 | |
b0fc494f SR |
108 | if (ftrace_enabled) { |
109 | /* | |
110 | * For one func, simply call it directly. | |
111 | * For more than one func, call the chain. | |
112 | */ | |
113 | if (ops->next == &ftrace_list_end) | |
114 | ftrace_trace_function = ops->func; | |
115 | else | |
116 | ftrace_trace_function = ftrace_list_func; | |
117 | } | |
3d083395 SR |
118 | |
119 | spin_unlock(&ftrace_lock); | |
16444a8a ACM |
120 | |
121 | return 0; | |
122 | } | |
123 | ||
e309b41d | 124 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 125 | { |
16444a8a ACM |
126 | struct ftrace_ops **p; |
127 | int ret = 0; | |
128 | ||
99ecdc43 | 129 | /* should not be called from interrupt context */ |
3d083395 | 130 | spin_lock(&ftrace_lock); |
16444a8a ACM |
131 | |
132 | /* | |
3d083395 SR |
133 | * If we are removing the last function, then simply point |
134 | * to the ftrace_stub. | |
16444a8a ACM |
135 | */ |
136 | if (ftrace_list == ops && ops->next == &ftrace_list_end) { | |
137 | ftrace_trace_function = ftrace_stub; | |
138 | ftrace_list = &ftrace_list_end; | |
139 | goto out; | |
140 | } | |
141 | ||
142 | for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next) | |
143 | if (*p == ops) | |
144 | break; | |
145 | ||
146 | if (*p != ops) { | |
147 | ret = -1; | |
148 | goto out; | |
149 | } | |
150 | ||
151 | *p = (*p)->next; | |
152 | ||
b0fc494f SR |
153 | if (ftrace_enabled) { |
154 | /* If we only have one func left, then call that directly */ | |
155 | if (ftrace_list == &ftrace_list_end || | |
156 | ftrace_list->next == &ftrace_list_end) | |
157 | ftrace_trace_function = ftrace_list->func; | |
158 | } | |
16444a8a ACM |
159 | |
160 | out: | |
3d083395 SR |
161 | spin_unlock(&ftrace_lock); |
162 | ||
163 | return ret; | |
164 | } | |
165 | ||
166 | #ifdef CONFIG_DYNAMIC_FTRACE | |
99ecdc43 | 167 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD |
cb7be3b2 | 168 | # error Dynamic ftrace depends on MCOUNT_RECORD |
99ecdc43 SR |
169 | #endif |
170 | ||
71c67d58 SN |
171 | /* |
172 | * Since MCOUNT_ADDR may point to mcount itself, we do not want | |
173 | * to get it confused by reading a reference in the code as we | |
174 | * are parsing on objcopy output of text. Use a variable for | |
175 | * it instead. | |
176 | */ | |
177 | static unsigned long mcount_addr = MCOUNT_ADDR; | |
178 | ||
d61f82d0 SR |
179 | enum { |
180 | FTRACE_ENABLE_CALLS = (1 << 0), | |
181 | FTRACE_DISABLE_CALLS = (1 << 1), | |
182 | FTRACE_UPDATE_TRACE_FUNC = (1 << 2), | |
183 | FTRACE_ENABLE_MCOUNT = (1 << 3), | |
184 | FTRACE_DISABLE_MCOUNT = (1 << 4), | |
185 | }; | |
186 | ||
5072c59f | 187 | static int ftrace_filtered; |
ecea656d AS |
188 | static int tracing_on; |
189 | static int frozen_record_count; | |
5072c59f | 190 | |
08f5ac90 | 191 | static LIST_HEAD(ftrace_new_addrs); |
3d083395 | 192 | |
41c52c0d | 193 | static DEFINE_MUTEX(ftrace_regex_lock); |
3d083395 | 194 | |
3c1720f0 SR |
195 | struct ftrace_page { |
196 | struct ftrace_page *next; | |
aa5e5cea | 197 | unsigned long index; |
3c1720f0 | 198 | struct dyn_ftrace records[]; |
aa5e5cea | 199 | }; |
3c1720f0 SR |
200 | |
201 | #define ENTRIES_PER_PAGE \ | |
202 | ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace)) | |
203 | ||
204 | /* estimate from running different kernels */ | |
205 | #define NR_TO_INIT 10000 | |
206 | ||
207 | static struct ftrace_page *ftrace_pages_start; | |
208 | static struct ftrace_page *ftrace_pages; | |
209 | ||
37ad5084 SR |
210 | static struct dyn_ftrace *ftrace_free_records; |
211 | ||
ecea656d AS |
212 | |
213 | #ifdef CONFIG_KPROBES | |
214 | static inline void freeze_record(struct dyn_ftrace *rec) | |
215 | { | |
216 | if (!(rec->flags & FTRACE_FL_FROZEN)) { | |
217 | rec->flags |= FTRACE_FL_FROZEN; | |
218 | frozen_record_count++; | |
219 | } | |
220 | } | |
221 | ||
222 | static inline void unfreeze_record(struct dyn_ftrace *rec) | |
223 | { | |
224 | if (rec->flags & FTRACE_FL_FROZEN) { | |
225 | rec->flags &= ~FTRACE_FL_FROZEN; | |
226 | frozen_record_count--; | |
227 | } | |
228 | } | |
229 | ||
230 | static inline int record_frozen(struct dyn_ftrace *rec) | |
231 | { | |
232 | return rec->flags & FTRACE_FL_FROZEN; | |
233 | } | |
234 | #else | |
235 | # define freeze_record(rec) ({ 0; }) | |
236 | # define unfreeze_record(rec) ({ 0; }) | |
237 | # define record_frozen(rec) ({ 0; }) | |
238 | #endif /* CONFIG_KPROBES */ | |
239 | ||
e309b41d | 240 | static void ftrace_free_rec(struct dyn_ftrace *rec) |
37ad5084 | 241 | { |
37ad5084 SR |
242 | rec->ip = (unsigned long)ftrace_free_records; |
243 | ftrace_free_records = rec; | |
244 | rec->flags |= FTRACE_FL_FREE; | |
245 | } | |
246 | ||
fed1939c SR |
247 | void ftrace_release(void *start, unsigned long size) |
248 | { | |
249 | struct dyn_ftrace *rec; | |
250 | struct ftrace_page *pg; | |
251 | unsigned long s = (unsigned long)start; | |
252 | unsigned long e = s + size; | |
253 | int i; | |
254 | ||
00fd61ae | 255 | if (ftrace_disabled || !start) |
fed1939c SR |
256 | return; |
257 | ||
99ecdc43 | 258 | /* should not be called from interrupt context */ |
fed1939c SR |
259 | spin_lock(&ftrace_lock); |
260 | ||
261 | for (pg = ftrace_pages_start; pg; pg = pg->next) { | |
262 | for (i = 0; i < pg->index; i++) { | |
263 | rec = &pg->records[i]; | |
264 | ||
265 | if ((rec->ip >= s) && (rec->ip < e)) | |
266 | ftrace_free_rec(rec); | |
267 | } | |
268 | } | |
269 | spin_unlock(&ftrace_lock); | |
fed1939c SR |
270 | } |
271 | ||
e309b41d | 272 | static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip) |
3c1720f0 | 273 | { |
37ad5084 SR |
274 | struct dyn_ftrace *rec; |
275 | ||
276 | /* First check for freed records */ | |
277 | if (ftrace_free_records) { | |
278 | rec = ftrace_free_records; | |
279 | ||
37ad5084 | 280 | if (unlikely(!(rec->flags & FTRACE_FL_FREE))) { |
6912896e | 281 | FTRACE_WARN_ON_ONCE(1); |
37ad5084 SR |
282 | ftrace_free_records = NULL; |
283 | return NULL; | |
284 | } | |
285 | ||
286 | ftrace_free_records = (void *)rec->ip; | |
287 | memset(rec, 0, sizeof(*rec)); | |
288 | return rec; | |
289 | } | |
290 | ||
3c1720f0 | 291 | if (ftrace_pages->index == ENTRIES_PER_PAGE) { |
08f5ac90 SR |
292 | if (!ftrace_pages->next) { |
293 | /* allocate another page */ | |
294 | ftrace_pages->next = | |
295 | (void *)get_zeroed_page(GFP_KERNEL); | |
296 | if (!ftrace_pages->next) | |
297 | return NULL; | |
298 | } | |
3c1720f0 SR |
299 | ftrace_pages = ftrace_pages->next; |
300 | } | |
301 | ||
302 | return &ftrace_pages->records[ftrace_pages->index++]; | |
303 | } | |
304 | ||
08f5ac90 | 305 | static struct dyn_ftrace * |
d61f82d0 | 306 | ftrace_record_ip(unsigned long ip) |
3d083395 | 307 | { |
08f5ac90 | 308 | struct dyn_ftrace *rec; |
3d083395 | 309 | |
4eebcc81 | 310 | if (!ftrace_enabled || ftrace_disabled) |
08f5ac90 | 311 | return NULL; |
3d083395 | 312 | |
08f5ac90 SR |
313 | rec = ftrace_alloc_dyn_node(ip); |
314 | if (!rec) | |
315 | return NULL; | |
3d083395 | 316 | |
08f5ac90 | 317 | rec->ip = ip; |
3d083395 | 318 | |
08f5ac90 | 319 | list_add(&rec->list, &ftrace_new_addrs); |
3d083395 | 320 | |
08f5ac90 | 321 | return rec; |
3d083395 SR |
322 | } |
323 | ||
caf8cdeb | 324 | #define FTRACE_ADDR ((long)(ftrace_caller)) |
3c1720f0 | 325 | |
0eb96701 | 326 | static int |
5072c59f SR |
327 | __ftrace_replace_code(struct dyn_ftrace *rec, |
328 | unsigned char *old, unsigned char *new, int enable) | |
329 | { | |
41c52c0d | 330 | unsigned long ip, fl; |
5072c59f SR |
331 | |
332 | ip = rec->ip; | |
333 | ||
334 | if (ftrace_filtered && enable) { | |
5072c59f SR |
335 | /* |
336 | * If filtering is on: | |
337 | * | |
338 | * If this record is set to be filtered and | |
339 | * is enabled then do nothing. | |
340 | * | |
341 | * If this record is set to be filtered and | |
342 | * it is not enabled, enable it. | |
343 | * | |
344 | * If this record is not set to be filtered | |
345 | * and it is not enabled do nothing. | |
346 | * | |
41c52c0d SR |
347 | * If this record is set not to trace then |
348 | * do nothing. | |
349 | * | |
a4500b84 AS |
350 | * If this record is set not to trace and |
351 | * it is enabled then disable it. | |
352 | * | |
5072c59f SR |
353 | * If this record is not set to be filtered and |
354 | * it is enabled, disable it. | |
355 | */ | |
a4500b84 AS |
356 | |
357 | fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE | | |
358 | FTRACE_FL_ENABLED); | |
5072c59f SR |
359 | |
360 | if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) || | |
a4500b84 AS |
361 | (fl == (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) || |
362 | !fl || (fl == FTRACE_FL_NOTRACE)) | |
0eb96701 | 363 | return 0; |
5072c59f SR |
364 | |
365 | /* | |
366 | * If it is enabled disable it, | |
367 | * otherwise enable it! | |
368 | */ | |
a4500b84 | 369 | if (fl & FTRACE_FL_ENABLED) { |
5072c59f SR |
370 | /* swap new and old */ |
371 | new = old; | |
372 | old = ftrace_call_replace(ip, FTRACE_ADDR); | |
373 | rec->flags &= ~FTRACE_FL_ENABLED; | |
374 | } else { | |
375 | new = ftrace_call_replace(ip, FTRACE_ADDR); | |
376 | rec->flags |= FTRACE_FL_ENABLED; | |
377 | } | |
378 | } else { | |
379 | ||
41c52c0d SR |
380 | if (enable) { |
381 | /* | |
382 | * If this record is set not to trace and is | |
383 | * not enabled, do nothing. | |
384 | */ | |
385 | fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED); | |
386 | if (fl == FTRACE_FL_NOTRACE) | |
0eb96701 | 387 | return 0; |
41c52c0d | 388 | |
5072c59f | 389 | new = ftrace_call_replace(ip, FTRACE_ADDR); |
41c52c0d | 390 | } else |
5072c59f SR |
391 | old = ftrace_call_replace(ip, FTRACE_ADDR); |
392 | ||
393 | if (enable) { | |
394 | if (rec->flags & FTRACE_FL_ENABLED) | |
0eb96701 | 395 | return 0; |
5072c59f SR |
396 | rec->flags |= FTRACE_FL_ENABLED; |
397 | } else { | |
398 | if (!(rec->flags & FTRACE_FL_ENABLED)) | |
0eb96701 | 399 | return 0; |
5072c59f SR |
400 | rec->flags &= ~FTRACE_FL_ENABLED; |
401 | } | |
402 | } | |
403 | ||
0eb96701 | 404 | return ftrace_modify_code(ip, old, new); |
5072c59f SR |
405 | } |
406 | ||
e309b41d | 407 | static void ftrace_replace_code(int enable) |
3c1720f0 | 408 | { |
0eb96701 | 409 | int i, failed; |
3c1720f0 SR |
410 | unsigned char *new = NULL, *old = NULL; |
411 | struct dyn_ftrace *rec; | |
412 | struct ftrace_page *pg; | |
3c1720f0 | 413 | |
5072c59f | 414 | if (enable) |
3c1720f0 SR |
415 | old = ftrace_nop_replace(); |
416 | else | |
417 | new = ftrace_nop_replace(); | |
418 | ||
419 | for (pg = ftrace_pages_start; pg; pg = pg->next) { | |
420 | for (i = 0; i < pg->index; i++) { | |
421 | rec = &pg->records[i]; | |
422 | ||
423 | /* don't modify code that has already faulted */ | |
424 | if (rec->flags & FTRACE_FL_FAILED) | |
425 | continue; | |
426 | ||
f22f9a89 | 427 | /* ignore updates to this record's mcount site */ |
98a05ed4 AS |
428 | if (get_kprobe((void *)rec->ip)) { |
429 | freeze_record(rec); | |
f22f9a89 | 430 | continue; |
98a05ed4 AS |
431 | } else { |
432 | unfreeze_record(rec); | |
433 | } | |
f22f9a89 | 434 | |
0eb96701 AS |
435 | failed = __ftrace_replace_code(rec, old, new, enable); |
436 | if (failed && (rec->flags & FTRACE_FL_CONVERTED)) { | |
437 | rec->flags |= FTRACE_FL_FAILED; | |
438 | if ((system_state == SYSTEM_BOOTING) || | |
34078a5e | 439 | !core_kernel_text(rec->ip)) { |
0eb96701 AS |
440 | ftrace_free_rec(rec); |
441 | } | |
442 | } | |
3c1720f0 SR |
443 | } |
444 | } | |
445 | } | |
446 | ||
05736a42 SR |
447 | static void print_ip_ins(const char *fmt, unsigned char *p) |
448 | { | |
449 | int i; | |
450 | ||
451 | printk(KERN_CONT "%s", fmt); | |
452 | ||
453 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) | |
454 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); | |
455 | } | |
456 | ||
492a7ea5 | 457 | static int |
d61f82d0 | 458 | ftrace_code_disable(struct dyn_ftrace *rec) |
3c1720f0 SR |
459 | { |
460 | unsigned long ip; | |
461 | unsigned char *nop, *call; | |
593eb8a2 | 462 | int ret; |
3c1720f0 SR |
463 | |
464 | ip = rec->ip; | |
465 | ||
466 | nop = ftrace_nop_replace(); | |
3b47bfc1 | 467 | call = ftrace_call_replace(ip, mcount_addr); |
3c1720f0 | 468 | |
593eb8a2 SR |
469 | ret = ftrace_modify_code(ip, call, nop); |
470 | if (ret) { | |
471 | switch (ret) { | |
472 | case -EFAULT: | |
6912896e | 473 | FTRACE_WARN_ON_ONCE(1); |
05736a42 SR |
474 | pr_info("ftrace faulted on modifying "); |
475 | print_ip_sym(ip); | |
476 | break; | |
593eb8a2 | 477 | case -EINVAL: |
6912896e | 478 | FTRACE_WARN_ON_ONCE(1); |
05736a42 SR |
479 | pr_info("ftrace failed to modify "); |
480 | print_ip_sym(ip); | |
481 | print_ip_ins(" expected: ", call); | |
482 | print_ip_ins(" actual: ", (unsigned char *)ip); | |
483 | print_ip_ins(" replace: ", nop); | |
484 | printk(KERN_CONT "\n"); | |
485 | break; | |
593eb8a2 | 486 | case -EPERM: |
6912896e | 487 | FTRACE_WARN_ON_ONCE(1); |
593eb8a2 SR |
488 | pr_info("ftrace faulted on writing "); |
489 | print_ip_sym(ip); | |
490 | break; | |
491 | default: | |
6912896e | 492 | FTRACE_WARN_ON_ONCE(1); |
593eb8a2 SR |
493 | pr_info("ftrace faulted on unknown error "); |
494 | print_ip_sym(ip); | |
05736a42 SR |
495 | } |
496 | ||
3c1720f0 | 497 | rec->flags |= FTRACE_FL_FAILED; |
492a7ea5 | 498 | return 0; |
37ad5084 | 499 | } |
492a7ea5 | 500 | return 1; |
3c1720f0 SR |
501 | } |
502 | ||
e309b41d | 503 | static int __ftrace_modify_code(void *data) |
3d083395 | 504 | { |
d61f82d0 SR |
505 | int *command = data; |
506 | ||
ad90c0e3 | 507 | if (*command & FTRACE_ENABLE_CALLS) { |
d61f82d0 | 508 | ftrace_replace_code(1); |
ecea656d AS |
509 | tracing_on = 1; |
510 | } else if (*command & FTRACE_DISABLE_CALLS) { | |
d61f82d0 | 511 | ftrace_replace_code(0); |
ecea656d AS |
512 | tracing_on = 0; |
513 | } | |
d61f82d0 SR |
514 | |
515 | if (*command & FTRACE_UPDATE_TRACE_FUNC) | |
516 | ftrace_update_ftrace_func(ftrace_trace_function); | |
517 | ||
d61f82d0 | 518 | return 0; |
3d083395 SR |
519 | } |
520 | ||
e309b41d | 521 | static void ftrace_run_update_code(int command) |
3d083395 | 522 | { |
784e2d76 | 523 | stop_machine(__ftrace_modify_code, &command, NULL); |
3d083395 SR |
524 | } |
525 | ||
d61f82d0 | 526 | static ftrace_func_t saved_ftrace_func; |
cb7be3b2 SR |
527 | static int ftrace_start; |
528 | static DEFINE_MUTEX(ftrace_start_lock); | |
d61f82d0 | 529 | |
e309b41d | 530 | static void ftrace_startup(void) |
3d083395 | 531 | { |
d61f82d0 SR |
532 | int command = 0; |
533 | ||
4eebcc81 SR |
534 | if (unlikely(ftrace_disabled)) |
535 | return; | |
536 | ||
cb7be3b2 SR |
537 | mutex_lock(&ftrace_start_lock); |
538 | ftrace_start++; | |
539 | if (ftrace_start == 1) | |
d61f82d0 SR |
540 | command |= FTRACE_ENABLE_CALLS; |
541 | ||
542 | if (saved_ftrace_func != ftrace_trace_function) { | |
543 | saved_ftrace_func = ftrace_trace_function; | |
544 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
545 | } | |
546 | ||
547 | if (!command || !ftrace_enabled) | |
3d083395 | 548 | goto out; |
3d083395 | 549 | |
d61f82d0 | 550 | ftrace_run_update_code(command); |
3d083395 | 551 | out: |
cb7be3b2 | 552 | mutex_unlock(&ftrace_start_lock); |
3d083395 SR |
553 | } |
554 | ||
e309b41d | 555 | static void ftrace_shutdown(void) |
3d083395 | 556 | { |
d61f82d0 SR |
557 | int command = 0; |
558 | ||
4eebcc81 SR |
559 | if (unlikely(ftrace_disabled)) |
560 | return; | |
561 | ||
cb7be3b2 SR |
562 | mutex_lock(&ftrace_start_lock); |
563 | ftrace_start--; | |
564 | if (!ftrace_start) | |
d61f82d0 | 565 | command |= FTRACE_DISABLE_CALLS; |
3d083395 | 566 | |
d61f82d0 SR |
567 | if (saved_ftrace_func != ftrace_trace_function) { |
568 | saved_ftrace_func = ftrace_trace_function; | |
569 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
570 | } | |
3d083395 | 571 | |
d61f82d0 SR |
572 | if (!command || !ftrace_enabled) |
573 | goto out; | |
574 | ||
575 | ftrace_run_update_code(command); | |
3d083395 | 576 | out: |
cb7be3b2 | 577 | mutex_unlock(&ftrace_start_lock); |
3d083395 SR |
578 | } |
579 | ||
e309b41d | 580 | static void ftrace_startup_sysctl(void) |
b0fc494f | 581 | { |
d61f82d0 SR |
582 | int command = FTRACE_ENABLE_MCOUNT; |
583 | ||
4eebcc81 SR |
584 | if (unlikely(ftrace_disabled)) |
585 | return; | |
586 | ||
cb7be3b2 | 587 | mutex_lock(&ftrace_start_lock); |
d61f82d0 SR |
588 | /* Force update next time */ |
589 | saved_ftrace_func = NULL; | |
cb7be3b2 SR |
590 | /* ftrace_start is true if we want ftrace running */ |
591 | if (ftrace_start) | |
d61f82d0 SR |
592 | command |= FTRACE_ENABLE_CALLS; |
593 | ||
594 | ftrace_run_update_code(command); | |
cb7be3b2 | 595 | mutex_unlock(&ftrace_start_lock); |
b0fc494f SR |
596 | } |
597 | ||
e309b41d | 598 | static void ftrace_shutdown_sysctl(void) |
b0fc494f | 599 | { |
d61f82d0 SR |
600 | int command = FTRACE_DISABLE_MCOUNT; |
601 | ||
4eebcc81 SR |
602 | if (unlikely(ftrace_disabled)) |
603 | return; | |
604 | ||
cb7be3b2 SR |
605 | mutex_lock(&ftrace_start_lock); |
606 | /* ftrace_start is true if ftrace is running */ | |
607 | if (ftrace_start) | |
d61f82d0 SR |
608 | command |= FTRACE_DISABLE_CALLS; |
609 | ||
610 | ftrace_run_update_code(command); | |
cb7be3b2 | 611 | mutex_unlock(&ftrace_start_lock); |
b0fc494f SR |
612 | } |
613 | ||
3d083395 SR |
614 | static cycle_t ftrace_update_time; |
615 | static unsigned long ftrace_update_cnt; | |
616 | unsigned long ftrace_update_tot_cnt; | |
617 | ||
08f5ac90 | 618 | static int ftrace_update_code(void) |
3d083395 | 619 | { |
08f5ac90 | 620 | struct dyn_ftrace *p, *t; |
f22f9a89 | 621 | cycle_t start, stop; |
3d083395 | 622 | |
750ed1a4 | 623 | start = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
624 | ftrace_update_cnt = 0; |
625 | ||
08f5ac90 | 626 | list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) { |
3d083395 | 627 | |
08f5ac90 SR |
628 | /* If something went wrong, bail without enabling anything */ |
629 | if (unlikely(ftrace_disabled)) | |
630 | return -1; | |
f22f9a89 | 631 | |
08f5ac90 | 632 | list_del_init(&p->list); |
f22f9a89 | 633 | |
08f5ac90 SR |
634 | /* convert record (i.e, patch mcount-call with NOP) */ |
635 | if (ftrace_code_disable(p)) { | |
636 | p->flags |= FTRACE_FL_CONVERTED; | |
637 | ftrace_update_cnt++; | |
638 | } else | |
639 | ftrace_free_rec(p); | |
3d083395 SR |
640 | } |
641 | ||
750ed1a4 | 642 | stop = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
643 | ftrace_update_time = stop - start; |
644 | ftrace_update_tot_cnt += ftrace_update_cnt; | |
645 | ||
16444a8a ACM |
646 | return 0; |
647 | } | |
648 | ||
68bf21aa | 649 | static int __init ftrace_dyn_table_alloc(unsigned long num_to_init) |
3c1720f0 SR |
650 | { |
651 | struct ftrace_page *pg; | |
652 | int cnt; | |
653 | int i; | |
3c1720f0 SR |
654 | |
655 | /* allocate a few pages */ | |
656 | ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL); | |
657 | if (!ftrace_pages_start) | |
658 | return -1; | |
659 | ||
660 | /* | |
661 | * Allocate a few more pages. | |
662 | * | |
663 | * TODO: have some parser search vmlinux before | |
664 | * final linking to find all calls to ftrace. | |
665 | * Then we can: | |
666 | * a) know how many pages to allocate. | |
667 | * and/or | |
668 | * b) set up the table then. | |
669 | * | |
670 | * The dynamic code is still necessary for | |
671 | * modules. | |
672 | */ | |
673 | ||
674 | pg = ftrace_pages = ftrace_pages_start; | |
675 | ||
68bf21aa | 676 | cnt = num_to_init / ENTRIES_PER_PAGE; |
08f5ac90 | 677 | pr_info("ftrace: allocating %ld entries in %d pages\n", |
68bf21aa | 678 | num_to_init, cnt); |
3c1720f0 SR |
679 | |
680 | for (i = 0; i < cnt; i++) { | |
681 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); | |
682 | ||
683 | /* If we fail, we'll try later anyway */ | |
684 | if (!pg->next) | |
685 | break; | |
686 | ||
687 | pg = pg->next; | |
688 | } | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
5072c59f SR |
693 | enum { |
694 | FTRACE_ITER_FILTER = (1 << 0), | |
695 | FTRACE_ITER_CONT = (1 << 1), | |
41c52c0d | 696 | FTRACE_ITER_NOTRACE = (1 << 2), |
eb9a7bf0 | 697 | FTRACE_ITER_FAILURES = (1 << 3), |
5072c59f SR |
698 | }; |
699 | ||
700 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ | |
701 | ||
702 | struct ftrace_iterator { | |
703 | loff_t pos; | |
704 | struct ftrace_page *pg; | |
705 | unsigned idx; | |
706 | unsigned flags; | |
707 | unsigned char buffer[FTRACE_BUFF_MAX+1]; | |
708 | unsigned buffer_idx; | |
709 | unsigned filtered; | |
710 | }; | |
711 | ||
e309b41d | 712 | static void * |
5072c59f SR |
713 | t_next(struct seq_file *m, void *v, loff_t *pos) |
714 | { | |
715 | struct ftrace_iterator *iter = m->private; | |
716 | struct dyn_ftrace *rec = NULL; | |
717 | ||
718 | (*pos)++; | |
719 | ||
99ecdc43 SR |
720 | /* should not be called from interrupt context */ |
721 | spin_lock(&ftrace_lock); | |
5072c59f SR |
722 | retry: |
723 | if (iter->idx >= iter->pg->index) { | |
724 | if (iter->pg->next) { | |
725 | iter->pg = iter->pg->next; | |
726 | iter->idx = 0; | |
727 | goto retry; | |
728 | } | |
729 | } else { | |
730 | rec = &iter->pg->records[iter->idx++]; | |
a9fdda33 SR |
731 | if ((rec->flags & FTRACE_FL_FREE) || |
732 | ||
733 | (!(iter->flags & FTRACE_ITER_FAILURES) && | |
eb9a7bf0 AS |
734 | (rec->flags & FTRACE_FL_FAILED)) || |
735 | ||
736 | ((iter->flags & FTRACE_ITER_FAILURES) && | |
a9fdda33 | 737 | !(rec->flags & FTRACE_FL_FAILED)) || |
eb9a7bf0 | 738 | |
41c52c0d SR |
739 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
740 | !(rec->flags & FTRACE_FL_NOTRACE))) { | |
5072c59f SR |
741 | rec = NULL; |
742 | goto retry; | |
743 | } | |
744 | } | |
99ecdc43 | 745 | spin_unlock(&ftrace_lock); |
5072c59f SR |
746 | |
747 | iter->pos = *pos; | |
748 | ||
749 | return rec; | |
750 | } | |
751 | ||
752 | static void *t_start(struct seq_file *m, loff_t *pos) | |
753 | { | |
754 | struct ftrace_iterator *iter = m->private; | |
755 | void *p = NULL; | |
756 | loff_t l = -1; | |
757 | ||
758 | if (*pos != iter->pos) { | |
759 | for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l)) | |
760 | ; | |
761 | } else { | |
762 | l = *pos; | |
763 | p = t_next(m, p, &l); | |
764 | } | |
765 | ||
766 | return p; | |
767 | } | |
768 | ||
769 | static void t_stop(struct seq_file *m, void *p) | |
770 | { | |
771 | } | |
772 | ||
773 | static int t_show(struct seq_file *m, void *v) | |
774 | { | |
775 | struct dyn_ftrace *rec = v; | |
776 | char str[KSYM_SYMBOL_LEN]; | |
777 | ||
778 | if (!rec) | |
779 | return 0; | |
780 | ||
781 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
782 | ||
783 | seq_printf(m, "%s\n", str); | |
784 | ||
785 | return 0; | |
786 | } | |
787 | ||
788 | static struct seq_operations show_ftrace_seq_ops = { | |
789 | .start = t_start, | |
790 | .next = t_next, | |
791 | .stop = t_stop, | |
792 | .show = t_show, | |
793 | }; | |
794 | ||
e309b41d | 795 | static int |
5072c59f SR |
796 | ftrace_avail_open(struct inode *inode, struct file *file) |
797 | { | |
798 | struct ftrace_iterator *iter; | |
799 | int ret; | |
800 | ||
4eebcc81 SR |
801 | if (unlikely(ftrace_disabled)) |
802 | return -ENODEV; | |
803 | ||
5072c59f SR |
804 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
805 | if (!iter) | |
806 | return -ENOMEM; | |
807 | ||
808 | iter->pg = ftrace_pages_start; | |
809 | iter->pos = -1; | |
810 | ||
811 | ret = seq_open(file, &show_ftrace_seq_ops); | |
812 | if (!ret) { | |
813 | struct seq_file *m = file->private_data; | |
4bf39a94 | 814 | |
5072c59f | 815 | m->private = iter; |
4bf39a94 | 816 | } else { |
5072c59f | 817 | kfree(iter); |
4bf39a94 | 818 | } |
5072c59f SR |
819 | |
820 | return ret; | |
821 | } | |
822 | ||
823 | int ftrace_avail_release(struct inode *inode, struct file *file) | |
824 | { | |
825 | struct seq_file *m = (struct seq_file *)file->private_data; | |
826 | struct ftrace_iterator *iter = m->private; | |
827 | ||
828 | seq_release(inode, file); | |
829 | kfree(iter); | |
4bf39a94 | 830 | |
5072c59f SR |
831 | return 0; |
832 | } | |
833 | ||
eb9a7bf0 AS |
834 | static int |
835 | ftrace_failures_open(struct inode *inode, struct file *file) | |
836 | { | |
837 | int ret; | |
838 | struct seq_file *m; | |
839 | struct ftrace_iterator *iter; | |
840 | ||
841 | ret = ftrace_avail_open(inode, file); | |
842 | if (!ret) { | |
843 | m = (struct seq_file *)file->private_data; | |
844 | iter = (struct ftrace_iterator *)m->private; | |
845 | iter->flags = FTRACE_ITER_FAILURES; | |
846 | } | |
847 | ||
848 | return ret; | |
849 | } | |
850 | ||
851 | ||
41c52c0d | 852 | static void ftrace_filter_reset(int enable) |
5072c59f SR |
853 | { |
854 | struct ftrace_page *pg; | |
855 | struct dyn_ftrace *rec; | |
41c52c0d | 856 | unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
5072c59f SR |
857 | unsigned i; |
858 | ||
99ecdc43 SR |
859 | /* should not be called from interrupt context */ |
860 | spin_lock(&ftrace_lock); | |
41c52c0d SR |
861 | if (enable) |
862 | ftrace_filtered = 0; | |
5072c59f SR |
863 | pg = ftrace_pages_start; |
864 | while (pg) { | |
865 | for (i = 0; i < pg->index; i++) { | |
866 | rec = &pg->records[i]; | |
867 | if (rec->flags & FTRACE_FL_FAILED) | |
868 | continue; | |
41c52c0d | 869 | rec->flags &= ~type; |
5072c59f SR |
870 | } |
871 | pg = pg->next; | |
872 | } | |
99ecdc43 | 873 | spin_unlock(&ftrace_lock); |
5072c59f SR |
874 | } |
875 | ||
e309b41d | 876 | static int |
41c52c0d | 877 | ftrace_regex_open(struct inode *inode, struct file *file, int enable) |
5072c59f SR |
878 | { |
879 | struct ftrace_iterator *iter; | |
880 | int ret = 0; | |
881 | ||
4eebcc81 SR |
882 | if (unlikely(ftrace_disabled)) |
883 | return -ENODEV; | |
884 | ||
5072c59f SR |
885 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
886 | if (!iter) | |
887 | return -ENOMEM; | |
888 | ||
41c52c0d | 889 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
890 | if ((file->f_mode & FMODE_WRITE) && |
891 | !(file->f_flags & O_APPEND)) | |
41c52c0d | 892 | ftrace_filter_reset(enable); |
5072c59f SR |
893 | |
894 | if (file->f_mode & FMODE_READ) { | |
895 | iter->pg = ftrace_pages_start; | |
896 | iter->pos = -1; | |
41c52c0d SR |
897 | iter->flags = enable ? FTRACE_ITER_FILTER : |
898 | FTRACE_ITER_NOTRACE; | |
5072c59f SR |
899 | |
900 | ret = seq_open(file, &show_ftrace_seq_ops); | |
901 | if (!ret) { | |
902 | struct seq_file *m = file->private_data; | |
903 | m->private = iter; | |
904 | } else | |
905 | kfree(iter); | |
906 | } else | |
907 | file->private_data = iter; | |
41c52c0d | 908 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
909 | |
910 | return ret; | |
911 | } | |
912 | ||
41c52c0d SR |
913 | static int |
914 | ftrace_filter_open(struct inode *inode, struct file *file) | |
915 | { | |
916 | return ftrace_regex_open(inode, file, 1); | |
917 | } | |
918 | ||
919 | static int | |
920 | ftrace_notrace_open(struct inode *inode, struct file *file) | |
921 | { | |
922 | return ftrace_regex_open(inode, file, 0); | |
923 | } | |
924 | ||
e309b41d | 925 | static ssize_t |
41c52c0d | 926 | ftrace_regex_read(struct file *file, char __user *ubuf, |
5072c59f SR |
927 | size_t cnt, loff_t *ppos) |
928 | { | |
929 | if (file->f_mode & FMODE_READ) | |
930 | return seq_read(file, ubuf, cnt, ppos); | |
931 | else | |
932 | return -EPERM; | |
933 | } | |
934 | ||
e309b41d | 935 | static loff_t |
41c52c0d | 936 | ftrace_regex_lseek(struct file *file, loff_t offset, int origin) |
5072c59f SR |
937 | { |
938 | loff_t ret; | |
939 | ||
940 | if (file->f_mode & FMODE_READ) | |
941 | ret = seq_lseek(file, offset, origin); | |
942 | else | |
943 | file->f_pos = ret = 1; | |
944 | ||
945 | return ret; | |
946 | } | |
947 | ||
948 | enum { | |
949 | MATCH_FULL, | |
950 | MATCH_FRONT_ONLY, | |
951 | MATCH_MIDDLE_ONLY, | |
952 | MATCH_END_ONLY, | |
953 | }; | |
954 | ||
e309b41d | 955 | static void |
41c52c0d | 956 | ftrace_match(unsigned char *buff, int len, int enable) |
5072c59f SR |
957 | { |
958 | char str[KSYM_SYMBOL_LEN]; | |
959 | char *search = NULL; | |
960 | struct ftrace_page *pg; | |
961 | struct dyn_ftrace *rec; | |
962 | int type = MATCH_FULL; | |
41c52c0d | 963 | unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
5072c59f SR |
964 | unsigned i, match = 0, search_len = 0; |
965 | ||
966 | for (i = 0; i < len; i++) { | |
967 | if (buff[i] == '*') { | |
968 | if (!i) { | |
969 | search = buff + i + 1; | |
970 | type = MATCH_END_ONLY; | |
971 | search_len = len - (i + 1); | |
972 | } else { | |
973 | if (type == MATCH_END_ONLY) { | |
974 | type = MATCH_MIDDLE_ONLY; | |
975 | } else { | |
976 | match = i; | |
977 | type = MATCH_FRONT_ONLY; | |
978 | } | |
979 | buff[i] = 0; | |
980 | break; | |
981 | } | |
982 | } | |
983 | } | |
984 | ||
99ecdc43 SR |
985 | /* should not be called from interrupt context */ |
986 | spin_lock(&ftrace_lock); | |
41c52c0d SR |
987 | if (enable) |
988 | ftrace_filtered = 1; | |
5072c59f SR |
989 | pg = ftrace_pages_start; |
990 | while (pg) { | |
991 | for (i = 0; i < pg->index; i++) { | |
992 | int matched = 0; | |
993 | char *ptr; | |
994 | ||
995 | rec = &pg->records[i]; | |
996 | if (rec->flags & FTRACE_FL_FAILED) | |
997 | continue; | |
998 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
999 | switch (type) { | |
1000 | case MATCH_FULL: | |
1001 | if (strcmp(str, buff) == 0) | |
1002 | matched = 1; | |
1003 | break; | |
1004 | case MATCH_FRONT_ONLY: | |
1005 | if (memcmp(str, buff, match) == 0) | |
1006 | matched = 1; | |
1007 | break; | |
1008 | case MATCH_MIDDLE_ONLY: | |
1009 | if (strstr(str, search)) | |
1010 | matched = 1; | |
1011 | break; | |
1012 | case MATCH_END_ONLY: | |
1013 | ptr = strstr(str, search); | |
1014 | if (ptr && (ptr[search_len] == 0)) | |
1015 | matched = 1; | |
1016 | break; | |
1017 | } | |
1018 | if (matched) | |
41c52c0d | 1019 | rec->flags |= flag; |
5072c59f SR |
1020 | } |
1021 | pg = pg->next; | |
1022 | } | |
99ecdc43 | 1023 | spin_unlock(&ftrace_lock); |
5072c59f SR |
1024 | } |
1025 | ||
e309b41d | 1026 | static ssize_t |
41c52c0d SR |
1027 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
1028 | size_t cnt, loff_t *ppos, int enable) | |
5072c59f SR |
1029 | { |
1030 | struct ftrace_iterator *iter; | |
1031 | char ch; | |
1032 | size_t read = 0; | |
1033 | ssize_t ret; | |
1034 | ||
1035 | if (!cnt || cnt < 0) | |
1036 | return 0; | |
1037 | ||
41c52c0d | 1038 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
1039 | |
1040 | if (file->f_mode & FMODE_READ) { | |
1041 | struct seq_file *m = file->private_data; | |
1042 | iter = m->private; | |
1043 | } else | |
1044 | iter = file->private_data; | |
1045 | ||
1046 | if (!*ppos) { | |
1047 | iter->flags &= ~FTRACE_ITER_CONT; | |
1048 | iter->buffer_idx = 0; | |
1049 | } | |
1050 | ||
1051 | ret = get_user(ch, ubuf++); | |
1052 | if (ret) | |
1053 | goto out; | |
1054 | read++; | |
1055 | cnt--; | |
1056 | ||
1057 | if (!(iter->flags & ~FTRACE_ITER_CONT)) { | |
1058 | /* skip white space */ | |
1059 | while (cnt && isspace(ch)) { | |
1060 | ret = get_user(ch, ubuf++); | |
1061 | if (ret) | |
1062 | goto out; | |
1063 | read++; | |
1064 | cnt--; | |
1065 | } | |
1066 | ||
5072c59f SR |
1067 | if (isspace(ch)) { |
1068 | file->f_pos += read; | |
1069 | ret = read; | |
1070 | goto out; | |
1071 | } | |
1072 | ||
1073 | iter->buffer_idx = 0; | |
1074 | } | |
1075 | ||
1076 | while (cnt && !isspace(ch)) { | |
1077 | if (iter->buffer_idx < FTRACE_BUFF_MAX) | |
1078 | iter->buffer[iter->buffer_idx++] = ch; | |
1079 | else { | |
1080 | ret = -EINVAL; | |
1081 | goto out; | |
1082 | } | |
1083 | ret = get_user(ch, ubuf++); | |
1084 | if (ret) | |
1085 | goto out; | |
1086 | read++; | |
1087 | cnt--; | |
1088 | } | |
1089 | ||
1090 | if (isspace(ch)) { | |
1091 | iter->filtered++; | |
1092 | iter->buffer[iter->buffer_idx] = 0; | |
41c52c0d | 1093 | ftrace_match(iter->buffer, iter->buffer_idx, enable); |
5072c59f SR |
1094 | iter->buffer_idx = 0; |
1095 | } else | |
1096 | iter->flags |= FTRACE_ITER_CONT; | |
1097 | ||
1098 | ||
1099 | file->f_pos += read; | |
1100 | ||
1101 | ret = read; | |
1102 | out: | |
41c52c0d | 1103 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
1104 | |
1105 | return ret; | |
1106 | } | |
1107 | ||
41c52c0d SR |
1108 | static ssize_t |
1109 | ftrace_filter_write(struct file *file, const char __user *ubuf, | |
1110 | size_t cnt, loff_t *ppos) | |
1111 | { | |
1112 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); | |
1113 | } | |
1114 | ||
1115 | static ssize_t | |
1116 | ftrace_notrace_write(struct file *file, const char __user *ubuf, | |
1117 | size_t cnt, loff_t *ppos) | |
1118 | { | |
1119 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); | |
1120 | } | |
1121 | ||
1122 | static void | |
1123 | ftrace_set_regex(unsigned char *buf, int len, int reset, int enable) | |
1124 | { | |
1125 | if (unlikely(ftrace_disabled)) | |
1126 | return; | |
1127 | ||
1128 | mutex_lock(&ftrace_regex_lock); | |
1129 | if (reset) | |
1130 | ftrace_filter_reset(enable); | |
1131 | if (buf) | |
1132 | ftrace_match(buf, len, enable); | |
1133 | mutex_unlock(&ftrace_regex_lock); | |
1134 | } | |
1135 | ||
77a2b37d SR |
1136 | /** |
1137 | * ftrace_set_filter - set a function to filter on in ftrace | |
1138 | * @buf - the string that holds the function filter text. | |
1139 | * @len - the length of the string. | |
1140 | * @reset - non zero to reset all filters before applying this filter. | |
1141 | * | |
1142 | * Filters denote which functions should be enabled when tracing is enabled. | |
1143 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
1144 | */ | |
e309b41d | 1145 | void ftrace_set_filter(unsigned char *buf, int len, int reset) |
77a2b37d | 1146 | { |
41c52c0d SR |
1147 | ftrace_set_regex(buf, len, reset, 1); |
1148 | } | |
4eebcc81 | 1149 | |
41c52c0d SR |
1150 | /** |
1151 | * ftrace_set_notrace - set a function to not trace in ftrace | |
1152 | * @buf - the string that holds the function notrace text. | |
1153 | * @len - the length of the string. | |
1154 | * @reset - non zero to reset all filters before applying this filter. | |
1155 | * | |
1156 | * Notrace Filters denote which functions should not be enabled when tracing | |
1157 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
1158 | * for tracing. | |
1159 | */ | |
1160 | void ftrace_set_notrace(unsigned char *buf, int len, int reset) | |
1161 | { | |
1162 | ftrace_set_regex(buf, len, reset, 0); | |
77a2b37d SR |
1163 | } |
1164 | ||
e309b41d | 1165 | static int |
41c52c0d | 1166 | ftrace_regex_release(struct inode *inode, struct file *file, int enable) |
5072c59f SR |
1167 | { |
1168 | struct seq_file *m = (struct seq_file *)file->private_data; | |
1169 | struct ftrace_iterator *iter; | |
1170 | ||
41c52c0d | 1171 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
1172 | if (file->f_mode & FMODE_READ) { |
1173 | iter = m->private; | |
1174 | ||
1175 | seq_release(inode, file); | |
1176 | } else | |
1177 | iter = file->private_data; | |
1178 | ||
1179 | if (iter->buffer_idx) { | |
1180 | iter->filtered++; | |
1181 | iter->buffer[iter->buffer_idx] = 0; | |
41c52c0d | 1182 | ftrace_match(iter->buffer, iter->buffer_idx, enable); |
5072c59f SR |
1183 | } |
1184 | ||
1185 | mutex_lock(&ftrace_sysctl_lock); | |
cb7be3b2 SR |
1186 | mutex_lock(&ftrace_start_lock); |
1187 | if (iter->filtered && ftrace_start && ftrace_enabled) | |
5072c59f | 1188 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); |
cb7be3b2 | 1189 | mutex_unlock(&ftrace_start_lock); |
5072c59f SR |
1190 | mutex_unlock(&ftrace_sysctl_lock); |
1191 | ||
1192 | kfree(iter); | |
41c52c0d | 1193 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
1194 | return 0; |
1195 | } | |
1196 | ||
41c52c0d SR |
1197 | static int |
1198 | ftrace_filter_release(struct inode *inode, struct file *file) | |
1199 | { | |
1200 | return ftrace_regex_release(inode, file, 1); | |
1201 | } | |
1202 | ||
1203 | static int | |
1204 | ftrace_notrace_release(struct inode *inode, struct file *file) | |
1205 | { | |
1206 | return ftrace_regex_release(inode, file, 0); | |
1207 | } | |
1208 | ||
5072c59f SR |
1209 | static struct file_operations ftrace_avail_fops = { |
1210 | .open = ftrace_avail_open, | |
1211 | .read = seq_read, | |
1212 | .llseek = seq_lseek, | |
1213 | .release = ftrace_avail_release, | |
1214 | }; | |
1215 | ||
eb9a7bf0 AS |
1216 | static struct file_operations ftrace_failures_fops = { |
1217 | .open = ftrace_failures_open, | |
1218 | .read = seq_read, | |
1219 | .llseek = seq_lseek, | |
1220 | .release = ftrace_avail_release, | |
1221 | }; | |
1222 | ||
5072c59f SR |
1223 | static struct file_operations ftrace_filter_fops = { |
1224 | .open = ftrace_filter_open, | |
41c52c0d | 1225 | .read = ftrace_regex_read, |
5072c59f | 1226 | .write = ftrace_filter_write, |
41c52c0d | 1227 | .llseek = ftrace_regex_lseek, |
5072c59f SR |
1228 | .release = ftrace_filter_release, |
1229 | }; | |
1230 | ||
41c52c0d SR |
1231 | static struct file_operations ftrace_notrace_fops = { |
1232 | .open = ftrace_notrace_open, | |
1233 | .read = ftrace_regex_read, | |
1234 | .write = ftrace_notrace_write, | |
1235 | .llseek = ftrace_regex_lseek, | |
1236 | .release = ftrace_notrace_release, | |
1237 | }; | |
1238 | ||
5072c59f SR |
1239 | static __init int ftrace_init_debugfs(void) |
1240 | { | |
1241 | struct dentry *d_tracer; | |
1242 | struct dentry *entry; | |
1243 | ||
1244 | d_tracer = tracing_init_dentry(); | |
1245 | ||
1246 | entry = debugfs_create_file("available_filter_functions", 0444, | |
1247 | d_tracer, NULL, &ftrace_avail_fops); | |
1248 | if (!entry) | |
1249 | pr_warning("Could not create debugfs " | |
1250 | "'available_filter_functions' entry\n"); | |
1251 | ||
eb9a7bf0 AS |
1252 | entry = debugfs_create_file("failures", 0444, |
1253 | d_tracer, NULL, &ftrace_failures_fops); | |
1254 | if (!entry) | |
1255 | pr_warning("Could not create debugfs 'failures' entry\n"); | |
1256 | ||
5072c59f SR |
1257 | entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer, |
1258 | NULL, &ftrace_filter_fops); | |
1259 | if (!entry) | |
1260 | pr_warning("Could not create debugfs " | |
1261 | "'set_ftrace_filter' entry\n"); | |
41c52c0d SR |
1262 | |
1263 | entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer, | |
1264 | NULL, &ftrace_notrace_fops); | |
1265 | if (!entry) | |
1266 | pr_warning("Could not create debugfs " | |
1267 | "'set_ftrace_notrace' entry\n"); | |
ad90c0e3 | 1268 | |
5072c59f SR |
1269 | return 0; |
1270 | } | |
1271 | ||
1272 | fs_initcall(ftrace_init_debugfs); | |
1273 | ||
68bf21aa SR |
1274 | static int ftrace_convert_nops(unsigned long *start, |
1275 | unsigned long *end) | |
1276 | { | |
1277 | unsigned long *p; | |
1278 | unsigned long addr; | |
1279 | unsigned long flags; | |
1280 | ||
08f5ac90 | 1281 | mutex_lock(&ftrace_start_lock); |
68bf21aa SR |
1282 | p = start; |
1283 | while (p < end) { | |
1284 | addr = ftrace_call_adjust(*p++); | |
1285 | ftrace_record_ip(addr); | |
68bf21aa SR |
1286 | } |
1287 | ||
08f5ac90 | 1288 | /* disable interrupts to prevent kstop machine */ |
68bf21aa | 1289 | local_irq_save(flags); |
08f5ac90 | 1290 | ftrace_update_code(); |
68bf21aa | 1291 | local_irq_restore(flags); |
08f5ac90 | 1292 | mutex_unlock(&ftrace_start_lock); |
68bf21aa SR |
1293 | |
1294 | return 0; | |
1295 | } | |
1296 | ||
90d595fe SR |
1297 | void ftrace_init_module(unsigned long *start, unsigned long *end) |
1298 | { | |
00fd61ae | 1299 | if (ftrace_disabled || start == end) |
fed1939c | 1300 | return; |
90d595fe SR |
1301 | ftrace_convert_nops(start, end); |
1302 | } | |
1303 | ||
68bf21aa SR |
1304 | extern unsigned long __start_mcount_loc[]; |
1305 | extern unsigned long __stop_mcount_loc[]; | |
1306 | ||
1307 | void __init ftrace_init(void) | |
1308 | { | |
1309 | unsigned long count, addr, flags; | |
1310 | int ret; | |
1311 | ||
1312 | /* Keep the ftrace pointer to the stub */ | |
1313 | addr = (unsigned long)ftrace_stub; | |
1314 | ||
1315 | local_irq_save(flags); | |
1316 | ftrace_dyn_arch_init(&addr); | |
1317 | local_irq_restore(flags); | |
1318 | ||
1319 | /* ftrace_dyn_arch_init places the return code in addr */ | |
1320 | if (addr) | |
1321 | goto failed; | |
1322 | ||
1323 | count = __stop_mcount_loc - __start_mcount_loc; | |
1324 | ||
1325 | ret = ftrace_dyn_table_alloc(count); | |
1326 | if (ret) | |
1327 | goto failed; | |
1328 | ||
1329 | last_ftrace_enabled = ftrace_enabled = 1; | |
1330 | ||
1331 | ret = ftrace_convert_nops(__start_mcount_loc, | |
1332 | __stop_mcount_loc); | |
1333 | ||
1334 | return; | |
1335 | failed: | |
1336 | ftrace_disabled = 1; | |
1337 | } | |
68bf21aa | 1338 | |
3d083395 | 1339 | #else |
c7aafc54 IM |
1340 | # define ftrace_startup() do { } while (0) |
1341 | # define ftrace_shutdown() do { } while (0) | |
1342 | # define ftrace_startup_sysctl() do { } while (0) | |
1343 | # define ftrace_shutdown_sysctl() do { } while (0) | |
3d083395 SR |
1344 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
1345 | ||
a2bb6a3d | 1346 | /** |
81adbdc0 | 1347 | * ftrace_kill - kill ftrace |
a2bb6a3d SR |
1348 | * |
1349 | * This function should be used by panic code. It stops ftrace | |
1350 | * but in a not so nice way. If you need to simply kill ftrace | |
1351 | * from a non-atomic section, use ftrace_kill. | |
1352 | */ | |
81adbdc0 | 1353 | void ftrace_kill(void) |
a2bb6a3d SR |
1354 | { |
1355 | ftrace_disabled = 1; | |
1356 | ftrace_enabled = 0; | |
a2bb6a3d SR |
1357 | clear_ftrace_function(); |
1358 | } | |
1359 | ||
16444a8a | 1360 | /** |
3d083395 SR |
1361 | * register_ftrace_function - register a function for profiling |
1362 | * @ops - ops structure that holds the function for profiling. | |
16444a8a | 1363 | * |
3d083395 SR |
1364 | * Register a function to be called by all functions in the |
1365 | * kernel. | |
1366 | * | |
1367 | * Note: @ops->func and all the functions it calls must be labeled | |
1368 | * with "notrace", otherwise it will go into a | |
1369 | * recursive loop. | |
16444a8a | 1370 | */ |
3d083395 | 1371 | int register_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 1372 | { |
b0fc494f SR |
1373 | int ret; |
1374 | ||
4eebcc81 SR |
1375 | if (unlikely(ftrace_disabled)) |
1376 | return -1; | |
1377 | ||
b0fc494f | 1378 | mutex_lock(&ftrace_sysctl_lock); |
b0fc494f | 1379 | ret = __register_ftrace_function(ops); |
d61f82d0 | 1380 | ftrace_startup(); |
b0fc494f SR |
1381 | mutex_unlock(&ftrace_sysctl_lock); |
1382 | ||
1383 | return ret; | |
3d083395 SR |
1384 | } |
1385 | ||
1386 | /** | |
1387 | * unregister_ftrace_function - unresgister a function for profiling. | |
1388 | * @ops - ops structure that holds the function to unregister | |
1389 | * | |
1390 | * Unregister a function that was added to be called by ftrace profiling. | |
1391 | */ | |
1392 | int unregister_ftrace_function(struct ftrace_ops *ops) | |
1393 | { | |
1394 | int ret; | |
1395 | ||
b0fc494f | 1396 | mutex_lock(&ftrace_sysctl_lock); |
3d083395 | 1397 | ret = __unregister_ftrace_function(ops); |
d61f82d0 | 1398 | ftrace_shutdown(); |
b0fc494f SR |
1399 | mutex_unlock(&ftrace_sysctl_lock); |
1400 | ||
1401 | return ret; | |
1402 | } | |
1403 | ||
e309b41d | 1404 | int |
b0fc494f | 1405 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
5072c59f | 1406 | struct file *file, void __user *buffer, size_t *lenp, |
b0fc494f SR |
1407 | loff_t *ppos) |
1408 | { | |
1409 | int ret; | |
1410 | ||
4eebcc81 SR |
1411 | if (unlikely(ftrace_disabled)) |
1412 | return -ENODEV; | |
1413 | ||
b0fc494f SR |
1414 | mutex_lock(&ftrace_sysctl_lock); |
1415 | ||
5072c59f | 1416 | ret = proc_dointvec(table, write, file, buffer, lenp, ppos); |
b0fc494f SR |
1417 | |
1418 | if (ret || !write || (last_ftrace_enabled == ftrace_enabled)) | |
1419 | goto out; | |
1420 | ||
1421 | last_ftrace_enabled = ftrace_enabled; | |
1422 | ||
1423 | if (ftrace_enabled) { | |
1424 | ||
1425 | ftrace_startup_sysctl(); | |
1426 | ||
1427 | /* we are starting ftrace again */ | |
1428 | if (ftrace_list != &ftrace_list_end) { | |
1429 | if (ftrace_list->next == &ftrace_list_end) | |
1430 | ftrace_trace_function = ftrace_list->func; | |
1431 | else | |
1432 | ftrace_trace_function = ftrace_list_func; | |
1433 | } | |
1434 | ||
1435 | } else { | |
1436 | /* stopping ftrace calls (just send to ftrace_stub) */ | |
1437 | ftrace_trace_function = ftrace_stub; | |
1438 | ||
1439 | ftrace_shutdown_sysctl(); | |
1440 | } | |
1441 | ||
1442 | out: | |
1443 | mutex_unlock(&ftrace_sysctl_lock); | |
3d083395 | 1444 | return ret; |
16444a8a | 1445 | } |