]>
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 | 19 | #include <linux/seq_file.h> |
4a2b8dda | 20 | #include <linux/suspend.h> |
5072c59f | 21 | #include <linux/debugfs.h> |
3d083395 | 22 | #include <linux/hardirq.h> |
2d8b820b | 23 | #include <linux/kthread.h> |
5072c59f | 24 | #include <linux/uaccess.h> |
2d8b820b | 25 | #include <linux/ftrace.h> |
b0fc494f | 26 | #include <linux/sysctl.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
5072c59f | 28 | #include <linux/ctype.h> |
3d083395 | 29 | #include <linux/list.h> |
59df055f | 30 | #include <linux/hash.h> |
3f379b03 | 31 | #include <linux/rcupdate.h> |
3d083395 | 32 | |
ad8d75ff | 33 | #include <trace/events/sched.h> |
8aef2d28 | 34 | |
395a59d0 | 35 | #include <asm/ftrace.h> |
2af15d6a | 36 | #include <asm/setup.h> |
395a59d0 | 37 | |
0706f1c4 | 38 | #include "trace_output.h" |
bac429f0 | 39 | #include "trace_stat.h" |
16444a8a | 40 | |
6912896e | 41 | #define FTRACE_WARN_ON(cond) \ |
0778d9ad SR |
42 | ({ \ |
43 | int ___r = cond; \ | |
44 | if (WARN_ON(___r)) \ | |
6912896e | 45 | ftrace_kill(); \ |
0778d9ad SR |
46 | ___r; \ |
47 | }) | |
6912896e SR |
48 | |
49 | #define FTRACE_WARN_ON_ONCE(cond) \ | |
0778d9ad SR |
50 | ({ \ |
51 | int ___r = cond; \ | |
52 | if (WARN_ON_ONCE(___r)) \ | |
6912896e | 53 | ftrace_kill(); \ |
0778d9ad SR |
54 | ___r; \ |
55 | }) | |
6912896e | 56 | |
8fc0c701 SR |
57 | /* hash bits for specific function selection */ |
58 | #define FTRACE_HASH_BITS 7 | |
59 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) | |
b448c4e3 | 60 | #define FTRACE_HASH_MAX_BITS 10 |
8fc0c701 | 61 | |
4eebcc81 SR |
62 | /* ftrace_enabled is a method to turn ftrace on or off */ |
63 | int ftrace_enabled __read_mostly; | |
d61f82d0 | 64 | static int last_ftrace_enabled; |
b0fc494f | 65 | |
60a7ecf4 SR |
66 | /* Quick disabling of function tracer. */ |
67 | int function_trace_stop; | |
68 | ||
756d17ee | 69 | /* List for set_ftrace_pid's pids. */ |
70 | LIST_HEAD(ftrace_pids); | |
71 | struct ftrace_pid { | |
72 | struct list_head list; | |
73 | struct pid *pid; | |
74 | }; | |
75 | ||
4eebcc81 SR |
76 | /* |
77 | * ftrace_disabled is set when an anomaly is discovered. | |
78 | * ftrace_disabled is much stronger than ftrace_enabled. | |
79 | */ | |
80 | static int ftrace_disabled __read_mostly; | |
81 | ||
52baf119 | 82 | static DEFINE_MUTEX(ftrace_lock); |
b0fc494f | 83 | |
16444a8a ACM |
84 | static struct ftrace_ops ftrace_list_end __read_mostly = |
85 | { | |
fb9fb015 | 86 | .func = ftrace_stub, |
16444a8a ACM |
87 | }; |
88 | ||
89 | static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end; | |
90 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; | |
60a7ecf4 | 91 | ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub; |
df4fc315 | 92 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
16444a8a | 93 | |
3f379b03 PM |
94 | /* |
95 | * Traverse the ftrace_list, invoking all entries. The reason that we | |
96 | * can use rcu_dereference_raw() is that elements removed from this list | |
97 | * are simply leaked, so there is no need to interact with a grace-period | |
98 | * mechanism. The rcu_dereference_raw() calls are needed to handle | |
99 | * concurrent insertions into the ftrace_list. | |
100 | * | |
101 | * Silly Alpha and silly pointer-speculation compiler optimizations! | |
102 | */ | |
f2252935 | 103 | static void ftrace_list_func(unsigned long ip, unsigned long parent_ip) |
16444a8a | 104 | { |
3f379b03 | 105 | struct ftrace_ops *op = rcu_dereference_raw(ftrace_list); /*see above*/ |
16444a8a ACM |
106 | |
107 | while (op != &ftrace_list_end) { | |
16444a8a | 108 | op->func(ip, parent_ip); |
3f379b03 | 109 | op = rcu_dereference_raw(op->next); /*see above*/ |
16444a8a ACM |
110 | }; |
111 | } | |
112 | ||
df4fc315 SR |
113 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip) |
114 | { | |
0ef8cde5 | 115 | if (!test_tsk_trace_trace(current)) |
df4fc315 SR |
116 | return; |
117 | ||
118 | ftrace_pid_function(ip, parent_ip); | |
119 | } | |
120 | ||
121 | static void set_ftrace_pid_function(ftrace_func_t func) | |
122 | { | |
123 | /* do not set ftrace_pid_function to itself! */ | |
124 | if (func != ftrace_pid_func) | |
125 | ftrace_pid_function = func; | |
126 | } | |
127 | ||
16444a8a | 128 | /** |
3d083395 | 129 | * clear_ftrace_function - reset the ftrace function |
16444a8a | 130 | * |
3d083395 SR |
131 | * This NULLs the ftrace function and in essence stops |
132 | * tracing. There may be lag | |
16444a8a | 133 | */ |
3d083395 | 134 | void clear_ftrace_function(void) |
16444a8a | 135 | { |
3d083395 | 136 | ftrace_trace_function = ftrace_stub; |
60a7ecf4 | 137 | __ftrace_trace_function = ftrace_stub; |
df4fc315 | 138 | ftrace_pid_function = ftrace_stub; |
3d083395 SR |
139 | } |
140 | ||
60a7ecf4 SR |
141 | #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
142 | /* | |
143 | * For those archs that do not test ftrace_trace_stop in their | |
144 | * mcount call site, we need to do it from C. | |
145 | */ | |
146 | static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip) | |
147 | { | |
148 | if (function_trace_stop) | |
149 | return; | |
150 | ||
151 | __ftrace_trace_function(ip, parent_ip); | |
152 | } | |
153 | #endif | |
154 | ||
491d0dcf SR |
155 | static void update_ftrace_function(void) |
156 | { | |
157 | ftrace_func_t func; | |
158 | ||
159 | /* | |
160 | * If there's only one function registered, then call that | |
161 | * function directly. Otherwise, we need to iterate over the | |
162 | * registered callers. | |
163 | */ | |
164 | if (ftrace_list == &ftrace_list_end || | |
165 | ftrace_list->next == &ftrace_list_end) | |
166 | func = ftrace_list->func; | |
167 | else | |
168 | func = ftrace_list_func; | |
169 | ||
170 | /* If we filter on pids, update to use the pid function */ | |
171 | if (!list_empty(&ftrace_pids)) { | |
172 | set_ftrace_pid_function(func); | |
173 | func = ftrace_pid_func; | |
174 | } | |
175 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST | |
176 | ftrace_trace_function = func; | |
177 | #else | |
178 | __ftrace_trace_function = func; | |
179 | ftrace_trace_function = ftrace_test_stop_func; | |
180 | #endif | |
181 | } | |
182 | ||
e309b41d | 183 | static int __register_ftrace_function(struct ftrace_ops *ops) |
3d083395 | 184 | { |
16444a8a ACM |
185 | ops->next = ftrace_list; |
186 | /* | |
187 | * We are entering ops into the ftrace_list but another | |
188 | * CPU might be walking that list. We need to make sure | |
189 | * the ops->next pointer is valid before another CPU sees | |
190 | * the ops pointer included into the ftrace_list. | |
191 | */ | |
3f379b03 | 192 | rcu_assign_pointer(ftrace_list, ops); |
3d083395 | 193 | |
491d0dcf SR |
194 | if (ftrace_enabled) |
195 | update_ftrace_function(); | |
3d083395 | 196 | |
16444a8a ACM |
197 | return 0; |
198 | } | |
199 | ||
e309b41d | 200 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 201 | { |
16444a8a | 202 | struct ftrace_ops **p; |
16444a8a ACM |
203 | |
204 | /* | |
3d083395 SR |
205 | * If we are removing the last function, then simply point |
206 | * to the ftrace_stub. | |
16444a8a ACM |
207 | */ |
208 | if (ftrace_list == ops && ops->next == &ftrace_list_end) { | |
209 | ftrace_trace_function = ftrace_stub; | |
210 | ftrace_list = &ftrace_list_end; | |
e6ea44e9 | 211 | return 0; |
16444a8a ACM |
212 | } |
213 | ||
214 | for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next) | |
215 | if (*p == ops) | |
216 | break; | |
217 | ||
e6ea44e9 SR |
218 | if (*p != ops) |
219 | return -1; | |
16444a8a ACM |
220 | |
221 | *p = (*p)->next; | |
222 | ||
491d0dcf SR |
223 | if (ftrace_enabled) |
224 | update_ftrace_function(); | |
16444a8a | 225 | |
e6ea44e9 | 226 | return 0; |
3d083395 SR |
227 | } |
228 | ||
df4fc315 SR |
229 | static void ftrace_update_pid_func(void) |
230 | { | |
491d0dcf | 231 | /* Only do something if we are tracing something */ |
df4fc315 | 232 | if (ftrace_trace_function == ftrace_stub) |
10dd3ebe | 233 | return; |
df4fc315 | 234 | |
491d0dcf | 235 | update_ftrace_function(); |
df4fc315 SR |
236 | } |
237 | ||
493762fc SR |
238 | #ifdef CONFIG_FUNCTION_PROFILER |
239 | struct ftrace_profile { | |
240 | struct hlist_node node; | |
241 | unsigned long ip; | |
242 | unsigned long counter; | |
0706f1c4 SR |
243 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
244 | unsigned long long time; | |
e330b3bc | 245 | unsigned long long time_squared; |
0706f1c4 | 246 | #endif |
8fc0c701 SR |
247 | }; |
248 | ||
493762fc SR |
249 | struct ftrace_profile_page { |
250 | struct ftrace_profile_page *next; | |
251 | unsigned long index; | |
252 | struct ftrace_profile records[]; | |
d61f82d0 SR |
253 | }; |
254 | ||
cafb168a SR |
255 | struct ftrace_profile_stat { |
256 | atomic_t disabled; | |
257 | struct hlist_head *hash; | |
258 | struct ftrace_profile_page *pages; | |
259 | struct ftrace_profile_page *start; | |
260 | struct tracer_stat stat; | |
261 | }; | |
262 | ||
493762fc SR |
263 | #define PROFILE_RECORDS_SIZE \ |
264 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) | |
5072c59f | 265 | |
493762fc SR |
266 | #define PROFILES_PER_PAGE \ |
267 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) | |
3d083395 | 268 | |
fb9fb015 SR |
269 | static int ftrace_profile_bits __read_mostly; |
270 | static int ftrace_profile_enabled __read_mostly; | |
271 | ||
272 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ | |
bac429f0 SR |
273 | static DEFINE_MUTEX(ftrace_profile_lock); |
274 | ||
cafb168a | 275 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
493762fc SR |
276 | |
277 | #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */ | |
278 | ||
bac429f0 SR |
279 | static void * |
280 | function_stat_next(void *v, int idx) | |
281 | { | |
493762fc SR |
282 | struct ftrace_profile *rec = v; |
283 | struct ftrace_profile_page *pg; | |
bac429f0 | 284 | |
493762fc | 285 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
bac429f0 SR |
286 | |
287 | again: | |
0296e425 LZ |
288 | if (idx != 0) |
289 | rec++; | |
290 | ||
bac429f0 SR |
291 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
292 | pg = pg->next; | |
293 | if (!pg) | |
294 | return NULL; | |
295 | rec = &pg->records[0]; | |
493762fc SR |
296 | if (!rec->counter) |
297 | goto again; | |
bac429f0 SR |
298 | } |
299 | ||
bac429f0 SR |
300 | return rec; |
301 | } | |
302 | ||
303 | static void *function_stat_start(struct tracer_stat *trace) | |
304 | { | |
cafb168a SR |
305 | struct ftrace_profile_stat *stat = |
306 | container_of(trace, struct ftrace_profile_stat, stat); | |
307 | ||
308 | if (!stat || !stat->start) | |
309 | return NULL; | |
310 | ||
311 | return function_stat_next(&stat->start->records[0], 0); | |
bac429f0 SR |
312 | } |
313 | ||
0706f1c4 SR |
314 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
315 | /* function graph compares on total time */ | |
316 | static int function_stat_cmp(void *p1, void *p2) | |
317 | { | |
318 | struct ftrace_profile *a = p1; | |
319 | struct ftrace_profile *b = p2; | |
320 | ||
321 | if (a->time < b->time) | |
322 | return -1; | |
323 | if (a->time > b->time) | |
324 | return 1; | |
325 | else | |
326 | return 0; | |
327 | } | |
328 | #else | |
329 | /* not function graph compares against hits */ | |
bac429f0 SR |
330 | static int function_stat_cmp(void *p1, void *p2) |
331 | { | |
493762fc SR |
332 | struct ftrace_profile *a = p1; |
333 | struct ftrace_profile *b = p2; | |
bac429f0 SR |
334 | |
335 | if (a->counter < b->counter) | |
336 | return -1; | |
337 | if (a->counter > b->counter) | |
338 | return 1; | |
339 | else | |
340 | return 0; | |
341 | } | |
0706f1c4 | 342 | #endif |
bac429f0 SR |
343 | |
344 | static int function_stat_headers(struct seq_file *m) | |
345 | { | |
0706f1c4 | 346 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
34886c8b | 347 | seq_printf(m, " Function " |
e330b3bc | 348 | "Hit Time Avg s^2\n" |
34886c8b | 349 | " -------- " |
e330b3bc | 350 | "--- ---- --- ---\n"); |
0706f1c4 | 351 | #else |
bac429f0 SR |
352 | seq_printf(m, " Function Hit\n" |
353 | " -------- ---\n"); | |
0706f1c4 | 354 | #endif |
bac429f0 SR |
355 | return 0; |
356 | } | |
357 | ||
358 | static int function_stat_show(struct seq_file *m, void *v) | |
359 | { | |
493762fc | 360 | struct ftrace_profile *rec = v; |
bac429f0 | 361 | char str[KSYM_SYMBOL_LEN]; |
3aaba20f | 362 | int ret = 0; |
0706f1c4 | 363 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
34886c8b SR |
364 | static struct trace_seq s; |
365 | unsigned long long avg; | |
e330b3bc | 366 | unsigned long long stddev; |
0706f1c4 | 367 | #endif |
3aaba20f LZ |
368 | mutex_lock(&ftrace_profile_lock); |
369 | ||
370 | /* we raced with function_profile_reset() */ | |
371 | if (unlikely(rec->counter == 0)) { | |
372 | ret = -EBUSY; | |
373 | goto out; | |
374 | } | |
bac429f0 SR |
375 | |
376 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
0706f1c4 SR |
377 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
378 | ||
379 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER | |
380 | seq_printf(m, " "); | |
34886c8b SR |
381 | avg = rec->time; |
382 | do_div(avg, rec->counter); | |
383 | ||
e330b3bc CD |
384 | /* Sample standard deviation (s^2) */ |
385 | if (rec->counter <= 1) | |
386 | stddev = 0; | |
387 | else { | |
388 | stddev = rec->time_squared - rec->counter * avg * avg; | |
389 | /* | |
390 | * Divide only 1000 for ns^2 -> us^2 conversion. | |
391 | * trace_print_graph_duration will divide 1000 again. | |
392 | */ | |
393 | do_div(stddev, (rec->counter - 1) * 1000); | |
394 | } | |
395 | ||
34886c8b SR |
396 | trace_seq_init(&s); |
397 | trace_print_graph_duration(rec->time, &s); | |
398 | trace_seq_puts(&s, " "); | |
399 | trace_print_graph_duration(avg, &s); | |
e330b3bc CD |
400 | trace_seq_puts(&s, " "); |
401 | trace_print_graph_duration(stddev, &s); | |
0706f1c4 | 402 | trace_print_seq(m, &s); |
0706f1c4 SR |
403 | #endif |
404 | seq_putc(m, '\n'); | |
3aaba20f LZ |
405 | out: |
406 | mutex_unlock(&ftrace_profile_lock); | |
bac429f0 | 407 | |
3aaba20f | 408 | return ret; |
bac429f0 SR |
409 | } |
410 | ||
cafb168a | 411 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
bac429f0 | 412 | { |
493762fc | 413 | struct ftrace_profile_page *pg; |
bac429f0 | 414 | |
cafb168a | 415 | pg = stat->pages = stat->start; |
bac429f0 | 416 | |
493762fc SR |
417 | while (pg) { |
418 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); | |
419 | pg->index = 0; | |
420 | pg = pg->next; | |
bac429f0 SR |
421 | } |
422 | ||
cafb168a | 423 | memset(stat->hash, 0, |
493762fc SR |
424 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
425 | } | |
bac429f0 | 426 | |
cafb168a | 427 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
493762fc SR |
428 | { |
429 | struct ftrace_profile_page *pg; | |
318e0a73 SR |
430 | int functions; |
431 | int pages; | |
493762fc | 432 | int i; |
bac429f0 | 433 | |
493762fc | 434 | /* If we already allocated, do nothing */ |
cafb168a | 435 | if (stat->pages) |
493762fc | 436 | return 0; |
bac429f0 | 437 | |
cafb168a SR |
438 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
439 | if (!stat->pages) | |
493762fc | 440 | return -ENOMEM; |
bac429f0 | 441 | |
318e0a73 SR |
442 | #ifdef CONFIG_DYNAMIC_FTRACE |
443 | functions = ftrace_update_tot_cnt; | |
444 | #else | |
445 | /* | |
446 | * We do not know the number of functions that exist because | |
447 | * dynamic tracing is what counts them. With past experience | |
448 | * we have around 20K functions. That should be more than enough. | |
449 | * It is highly unlikely we will execute every function in | |
450 | * the kernel. | |
451 | */ | |
452 | functions = 20000; | |
453 | #endif | |
454 | ||
cafb168a | 455 | pg = stat->start = stat->pages; |
bac429f0 | 456 | |
318e0a73 SR |
457 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
458 | ||
459 | for (i = 0; i < pages; i++) { | |
493762fc | 460 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
493762fc | 461 | if (!pg->next) |
318e0a73 | 462 | goto out_free; |
493762fc SR |
463 | pg = pg->next; |
464 | } | |
465 | ||
466 | return 0; | |
318e0a73 SR |
467 | |
468 | out_free: | |
469 | pg = stat->start; | |
470 | while (pg) { | |
471 | unsigned long tmp = (unsigned long)pg; | |
472 | ||
473 | pg = pg->next; | |
474 | free_page(tmp); | |
475 | } | |
476 | ||
477 | free_page((unsigned long)stat->pages); | |
478 | stat->pages = NULL; | |
479 | stat->start = NULL; | |
480 | ||
481 | return -ENOMEM; | |
bac429f0 SR |
482 | } |
483 | ||
cafb168a | 484 | static int ftrace_profile_init_cpu(int cpu) |
bac429f0 | 485 | { |
cafb168a | 486 | struct ftrace_profile_stat *stat; |
493762fc | 487 | int size; |
bac429f0 | 488 | |
cafb168a SR |
489 | stat = &per_cpu(ftrace_profile_stats, cpu); |
490 | ||
491 | if (stat->hash) { | |
493762fc | 492 | /* If the profile is already created, simply reset it */ |
cafb168a | 493 | ftrace_profile_reset(stat); |
493762fc SR |
494 | return 0; |
495 | } | |
bac429f0 | 496 | |
493762fc SR |
497 | /* |
498 | * We are profiling all functions, but usually only a few thousand | |
499 | * functions are hit. We'll make a hash of 1024 items. | |
500 | */ | |
501 | size = FTRACE_PROFILE_HASH_SIZE; | |
bac429f0 | 502 | |
cafb168a | 503 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
493762fc | 504 | |
cafb168a | 505 | if (!stat->hash) |
493762fc SR |
506 | return -ENOMEM; |
507 | ||
cafb168a SR |
508 | if (!ftrace_profile_bits) { |
509 | size--; | |
493762fc | 510 | |
cafb168a SR |
511 | for (; size; size >>= 1) |
512 | ftrace_profile_bits++; | |
513 | } | |
493762fc | 514 | |
318e0a73 | 515 | /* Preallocate the function profiling pages */ |
cafb168a SR |
516 | if (ftrace_profile_pages_init(stat) < 0) { |
517 | kfree(stat->hash); | |
518 | stat->hash = NULL; | |
493762fc SR |
519 | return -ENOMEM; |
520 | } | |
521 | ||
522 | return 0; | |
bac429f0 SR |
523 | } |
524 | ||
cafb168a SR |
525 | static int ftrace_profile_init(void) |
526 | { | |
527 | int cpu; | |
528 | int ret = 0; | |
529 | ||
530 | for_each_online_cpu(cpu) { | |
531 | ret = ftrace_profile_init_cpu(cpu); | |
532 | if (ret) | |
533 | break; | |
534 | } | |
535 | ||
536 | return ret; | |
537 | } | |
538 | ||
493762fc | 539 | /* interrupts must be disabled */ |
cafb168a SR |
540 | static struct ftrace_profile * |
541 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) | |
bac429f0 | 542 | { |
493762fc | 543 | struct ftrace_profile *rec; |
bac429f0 SR |
544 | struct hlist_head *hhd; |
545 | struct hlist_node *n; | |
bac429f0 SR |
546 | unsigned long key; |
547 | ||
bac429f0 | 548 | key = hash_long(ip, ftrace_profile_bits); |
cafb168a | 549 | hhd = &stat->hash[key]; |
bac429f0 SR |
550 | |
551 | if (hlist_empty(hhd)) | |
552 | return NULL; | |
553 | ||
bac429f0 SR |
554 | hlist_for_each_entry_rcu(rec, n, hhd, node) { |
555 | if (rec->ip == ip) | |
493762fc SR |
556 | return rec; |
557 | } | |
558 | ||
559 | return NULL; | |
560 | } | |
561 | ||
cafb168a SR |
562 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
563 | struct ftrace_profile *rec) | |
493762fc SR |
564 | { |
565 | unsigned long key; | |
566 | ||
567 | key = hash_long(rec->ip, ftrace_profile_bits); | |
cafb168a | 568 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
493762fc SR |
569 | } |
570 | ||
318e0a73 SR |
571 | /* |
572 | * The memory is already allocated, this simply finds a new record to use. | |
573 | */ | |
493762fc | 574 | static struct ftrace_profile * |
318e0a73 | 575 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
493762fc SR |
576 | { |
577 | struct ftrace_profile *rec = NULL; | |
578 | ||
318e0a73 | 579 | /* prevent recursion (from NMIs) */ |
cafb168a | 580 | if (atomic_inc_return(&stat->disabled) != 1) |
493762fc SR |
581 | goto out; |
582 | ||
493762fc | 583 | /* |
318e0a73 SR |
584 | * Try to find the function again since an NMI |
585 | * could have added it | |
493762fc | 586 | */ |
cafb168a | 587 | rec = ftrace_find_profiled_func(stat, ip); |
493762fc | 588 | if (rec) |
cafb168a | 589 | goto out; |
493762fc | 590 | |
cafb168a SR |
591 | if (stat->pages->index == PROFILES_PER_PAGE) { |
592 | if (!stat->pages->next) | |
593 | goto out; | |
594 | stat->pages = stat->pages->next; | |
bac429f0 | 595 | } |
493762fc | 596 | |
cafb168a | 597 | rec = &stat->pages->records[stat->pages->index++]; |
493762fc | 598 | rec->ip = ip; |
cafb168a | 599 | ftrace_add_profile(stat, rec); |
493762fc | 600 | |
bac429f0 | 601 | out: |
cafb168a | 602 | atomic_dec(&stat->disabled); |
bac429f0 SR |
603 | |
604 | return rec; | |
605 | } | |
606 | ||
607 | static void | |
608 | function_profile_call(unsigned long ip, unsigned long parent_ip) | |
609 | { | |
cafb168a | 610 | struct ftrace_profile_stat *stat; |
493762fc | 611 | struct ftrace_profile *rec; |
bac429f0 SR |
612 | unsigned long flags; |
613 | ||
614 | if (!ftrace_profile_enabled) | |
615 | return; | |
616 | ||
617 | local_irq_save(flags); | |
cafb168a SR |
618 | |
619 | stat = &__get_cpu_var(ftrace_profile_stats); | |
0f6ce3de | 620 | if (!stat->hash || !ftrace_profile_enabled) |
cafb168a SR |
621 | goto out; |
622 | ||
623 | rec = ftrace_find_profiled_func(stat, ip); | |
493762fc | 624 | if (!rec) { |
318e0a73 | 625 | rec = ftrace_profile_alloc(stat, ip); |
493762fc SR |
626 | if (!rec) |
627 | goto out; | |
628 | } | |
bac429f0 SR |
629 | |
630 | rec->counter++; | |
631 | out: | |
632 | local_irq_restore(flags); | |
633 | } | |
634 | ||
0706f1c4 SR |
635 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
636 | static int profile_graph_entry(struct ftrace_graph_ent *trace) | |
637 | { | |
638 | function_profile_call(trace->func, 0); | |
639 | return 1; | |
640 | } | |
641 | ||
642 | static void profile_graph_return(struct ftrace_graph_ret *trace) | |
643 | { | |
cafb168a | 644 | struct ftrace_profile_stat *stat; |
a2a16d6a | 645 | unsigned long long calltime; |
0706f1c4 | 646 | struct ftrace_profile *rec; |
cafb168a | 647 | unsigned long flags; |
0706f1c4 SR |
648 | |
649 | local_irq_save(flags); | |
cafb168a | 650 | stat = &__get_cpu_var(ftrace_profile_stats); |
0f6ce3de | 651 | if (!stat->hash || !ftrace_profile_enabled) |
cafb168a SR |
652 | goto out; |
653 | ||
37e44bc5 SR |
654 | /* If the calltime was zero'd ignore it */ |
655 | if (!trace->calltime) | |
656 | goto out; | |
657 | ||
a2a16d6a SR |
658 | calltime = trace->rettime - trace->calltime; |
659 | ||
660 | if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) { | |
661 | int index; | |
662 | ||
663 | index = trace->depth; | |
664 | ||
665 | /* Append this call time to the parent time to subtract */ | |
666 | if (index) | |
667 | current->ret_stack[index - 1].subtime += calltime; | |
668 | ||
669 | if (current->ret_stack[index].subtime < calltime) | |
670 | calltime -= current->ret_stack[index].subtime; | |
671 | else | |
672 | calltime = 0; | |
673 | } | |
674 | ||
cafb168a | 675 | rec = ftrace_find_profiled_func(stat, trace->func); |
e330b3bc | 676 | if (rec) { |
a2a16d6a | 677 | rec->time += calltime; |
e330b3bc CD |
678 | rec->time_squared += calltime * calltime; |
679 | } | |
a2a16d6a | 680 | |
cafb168a | 681 | out: |
0706f1c4 SR |
682 | local_irq_restore(flags); |
683 | } | |
684 | ||
685 | static int register_ftrace_profiler(void) | |
686 | { | |
687 | return register_ftrace_graph(&profile_graph_return, | |
688 | &profile_graph_entry); | |
689 | } | |
690 | ||
691 | static void unregister_ftrace_profiler(void) | |
692 | { | |
693 | unregister_ftrace_graph(); | |
694 | } | |
695 | #else | |
bac429f0 SR |
696 | static struct ftrace_ops ftrace_profile_ops __read_mostly = |
697 | { | |
fb9fb015 | 698 | .func = function_profile_call, |
bac429f0 SR |
699 | }; |
700 | ||
0706f1c4 SR |
701 | static int register_ftrace_profiler(void) |
702 | { | |
703 | return register_ftrace_function(&ftrace_profile_ops); | |
704 | } | |
705 | ||
706 | static void unregister_ftrace_profiler(void) | |
707 | { | |
708 | unregister_ftrace_function(&ftrace_profile_ops); | |
709 | } | |
710 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
711 | ||
bac429f0 SR |
712 | static ssize_t |
713 | ftrace_profile_write(struct file *filp, const char __user *ubuf, | |
714 | size_t cnt, loff_t *ppos) | |
715 | { | |
716 | unsigned long val; | |
fb9fb015 | 717 | char buf[64]; /* big enough to hold a number */ |
bac429f0 SR |
718 | int ret; |
719 | ||
bac429f0 SR |
720 | if (cnt >= sizeof(buf)) |
721 | return -EINVAL; | |
722 | ||
723 | if (copy_from_user(&buf, ubuf, cnt)) | |
724 | return -EFAULT; | |
725 | ||
726 | buf[cnt] = 0; | |
727 | ||
728 | ret = strict_strtoul(buf, 10, &val); | |
729 | if (ret < 0) | |
730 | return ret; | |
731 | ||
732 | val = !!val; | |
733 | ||
734 | mutex_lock(&ftrace_profile_lock); | |
735 | if (ftrace_profile_enabled ^ val) { | |
736 | if (val) { | |
493762fc SR |
737 | ret = ftrace_profile_init(); |
738 | if (ret < 0) { | |
739 | cnt = ret; | |
740 | goto out; | |
741 | } | |
742 | ||
0706f1c4 SR |
743 | ret = register_ftrace_profiler(); |
744 | if (ret < 0) { | |
745 | cnt = ret; | |
746 | goto out; | |
747 | } | |
bac429f0 SR |
748 | ftrace_profile_enabled = 1; |
749 | } else { | |
750 | ftrace_profile_enabled = 0; | |
0f6ce3de SR |
751 | /* |
752 | * unregister_ftrace_profiler calls stop_machine | |
753 | * so this acts like an synchronize_sched. | |
754 | */ | |
0706f1c4 | 755 | unregister_ftrace_profiler(); |
bac429f0 SR |
756 | } |
757 | } | |
493762fc | 758 | out: |
bac429f0 SR |
759 | mutex_unlock(&ftrace_profile_lock); |
760 | ||
cf8517cf | 761 | *ppos += cnt; |
bac429f0 SR |
762 | |
763 | return cnt; | |
764 | } | |
765 | ||
493762fc SR |
766 | static ssize_t |
767 | ftrace_profile_read(struct file *filp, char __user *ubuf, | |
768 | size_t cnt, loff_t *ppos) | |
769 | { | |
fb9fb015 | 770 | char buf[64]; /* big enough to hold a number */ |
493762fc SR |
771 | int r; |
772 | ||
773 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); | |
774 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
775 | } | |
776 | ||
bac429f0 SR |
777 | static const struct file_operations ftrace_profile_fops = { |
778 | .open = tracing_open_generic, | |
779 | .read = ftrace_profile_read, | |
780 | .write = ftrace_profile_write, | |
6038f373 | 781 | .llseek = default_llseek, |
bac429f0 SR |
782 | }; |
783 | ||
cafb168a SR |
784 | /* used to initialize the real stat files */ |
785 | static struct tracer_stat function_stats __initdata = { | |
fb9fb015 SR |
786 | .name = "functions", |
787 | .stat_start = function_stat_start, | |
788 | .stat_next = function_stat_next, | |
789 | .stat_cmp = function_stat_cmp, | |
790 | .stat_headers = function_stat_headers, | |
791 | .stat_show = function_stat_show | |
cafb168a SR |
792 | }; |
793 | ||
6ab5d668 | 794 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
bac429f0 | 795 | { |
cafb168a | 796 | struct ftrace_profile_stat *stat; |
bac429f0 | 797 | struct dentry *entry; |
cafb168a | 798 | char *name; |
bac429f0 | 799 | int ret; |
cafb168a SR |
800 | int cpu; |
801 | ||
802 | for_each_possible_cpu(cpu) { | |
803 | stat = &per_cpu(ftrace_profile_stats, cpu); | |
804 | ||
805 | /* allocate enough for function name + cpu number */ | |
806 | name = kmalloc(32, GFP_KERNEL); | |
807 | if (!name) { | |
808 | /* | |
809 | * The files created are permanent, if something happens | |
810 | * we still do not free memory. | |
811 | */ | |
cafb168a SR |
812 | WARN(1, |
813 | "Could not allocate stat file for cpu %d\n", | |
814 | cpu); | |
815 | return; | |
816 | } | |
817 | stat->stat = function_stats; | |
818 | snprintf(name, 32, "function%d", cpu); | |
819 | stat->stat.name = name; | |
820 | ret = register_stat_tracer(&stat->stat); | |
821 | if (ret) { | |
822 | WARN(1, | |
823 | "Could not register function stat for cpu %d\n", | |
824 | cpu); | |
825 | kfree(name); | |
826 | return; | |
827 | } | |
bac429f0 SR |
828 | } |
829 | ||
830 | entry = debugfs_create_file("function_profile_enabled", 0644, | |
831 | d_tracer, NULL, &ftrace_profile_fops); | |
832 | if (!entry) | |
833 | pr_warning("Could not create debugfs " | |
834 | "'function_profile_enabled' entry\n"); | |
835 | } | |
836 | ||
bac429f0 | 837 | #else /* CONFIG_FUNCTION_PROFILER */ |
6ab5d668 | 838 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
bac429f0 SR |
839 | { |
840 | } | |
bac429f0 SR |
841 | #endif /* CONFIG_FUNCTION_PROFILER */ |
842 | ||
493762fc SR |
843 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
844 | ||
845 | #ifdef CONFIG_DYNAMIC_FTRACE | |
846 | ||
847 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD | |
848 | # error Dynamic ftrace depends on MCOUNT_RECORD | |
849 | #endif | |
850 | ||
851 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; | |
852 | ||
853 | struct ftrace_func_probe { | |
854 | struct hlist_node node; | |
855 | struct ftrace_probe_ops *ops; | |
856 | unsigned long flags; | |
857 | unsigned long ip; | |
858 | void *data; | |
859 | struct rcu_head rcu; | |
860 | }; | |
861 | ||
862 | enum { | |
863 | FTRACE_ENABLE_CALLS = (1 << 0), | |
864 | FTRACE_DISABLE_CALLS = (1 << 1), | |
865 | FTRACE_UPDATE_TRACE_FUNC = (1 << 2), | |
79e406d7 SR |
866 | FTRACE_START_FUNC_RET = (1 << 3), |
867 | FTRACE_STOP_FUNC_RET = (1 << 4), | |
493762fc | 868 | }; |
b448c4e3 SR |
869 | struct ftrace_func_entry { |
870 | struct hlist_node hlist; | |
871 | unsigned long ip; | |
872 | }; | |
873 | ||
874 | struct ftrace_hash { | |
875 | unsigned long size_bits; | |
876 | struct hlist_head *buckets; | |
877 | unsigned long count; | |
878 | }; | |
879 | ||
880 | static struct hlist_head notrace_buckets[1 << FTRACE_HASH_MAX_BITS]; | |
881 | static struct ftrace_hash notrace_hash = { | |
882 | .size_bits = FTRACE_HASH_MAX_BITS, | |
883 | .buckets = notrace_buckets, | |
884 | }; | |
493762fc | 885 | |
1cf41dd7 SR |
886 | static struct hlist_head filter_buckets[1 << FTRACE_HASH_MAX_BITS]; |
887 | static struct ftrace_hash filter_hash = { | |
888 | .size_bits = FTRACE_HASH_MAX_BITS, | |
889 | .buckets = filter_buckets, | |
890 | }; | |
493762fc | 891 | |
f45948e8 SR |
892 | struct ftrace_ops global_ops = { |
893 | .func = ftrace_stub, | |
894 | .notrace_hash = ¬race_hash, | |
895 | .filter_hash = &filter_hash, | |
896 | }; | |
897 | ||
493762fc SR |
898 | static struct dyn_ftrace *ftrace_new_addrs; |
899 | ||
900 | static DEFINE_MUTEX(ftrace_regex_lock); | |
901 | ||
902 | struct ftrace_page { | |
903 | struct ftrace_page *next; | |
904 | int index; | |
905 | struct dyn_ftrace records[]; | |
906 | }; | |
907 | ||
908 | #define ENTRIES_PER_PAGE \ | |
909 | ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace)) | |
910 | ||
911 | /* estimate from running different kernels */ | |
912 | #define NR_TO_INIT 10000 | |
913 | ||
914 | static struct ftrace_page *ftrace_pages_start; | |
915 | static struct ftrace_page *ftrace_pages; | |
916 | ||
917 | static struct dyn_ftrace *ftrace_free_records; | |
918 | ||
b448c4e3 SR |
919 | static struct ftrace_func_entry * |
920 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) | |
921 | { | |
922 | unsigned long key; | |
923 | struct ftrace_func_entry *entry; | |
924 | struct hlist_head *hhd; | |
925 | struct hlist_node *n; | |
926 | ||
927 | if (!hash->count) | |
928 | return NULL; | |
929 | ||
930 | if (hash->size_bits > 0) | |
931 | key = hash_long(ip, hash->size_bits); | |
932 | else | |
933 | key = 0; | |
934 | ||
935 | hhd = &hash->buckets[key]; | |
936 | ||
937 | hlist_for_each_entry_rcu(entry, n, hhd, hlist) { | |
938 | if (entry->ip == ip) | |
939 | return entry; | |
940 | } | |
941 | return NULL; | |
942 | } | |
943 | ||
944 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) | |
945 | { | |
946 | struct ftrace_func_entry *entry; | |
947 | struct hlist_head *hhd; | |
948 | unsigned long key; | |
949 | ||
950 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
951 | if (!entry) | |
952 | return -ENOMEM; | |
953 | ||
954 | if (hash->size_bits) | |
955 | key = hash_long(ip, hash->size_bits); | |
956 | else | |
957 | key = 0; | |
958 | ||
959 | entry->ip = ip; | |
960 | hhd = &hash->buckets[key]; | |
961 | hlist_add_head(&entry->hlist, hhd); | |
962 | hash->count++; | |
963 | ||
964 | return 0; | |
965 | } | |
966 | ||
967 | static void | |
968 | remove_hash_entry(struct ftrace_hash *hash, | |
969 | struct ftrace_func_entry *entry) | |
970 | { | |
971 | hlist_del(&entry->hlist); | |
972 | kfree(entry); | |
973 | hash->count--; | |
974 | } | |
975 | ||
976 | static void ftrace_hash_clear(struct ftrace_hash *hash) | |
977 | { | |
978 | struct hlist_head *hhd; | |
979 | struct hlist_node *tp, *tn; | |
980 | struct ftrace_func_entry *entry; | |
981 | int size = 1 << hash->size_bits; | |
982 | int i; | |
983 | ||
984 | for (i = 0; i < size; i++) { | |
985 | hhd = &hash->buckets[i]; | |
986 | hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) | |
987 | remove_hash_entry(hash, entry); | |
988 | } | |
989 | FTRACE_WARN_ON(hash->count); | |
990 | } | |
991 | ||
493762fc SR |
992 | /* |
993 | * This is a double for. Do not use 'break' to break out of the loop, | |
994 | * you must use a goto. | |
995 | */ | |
996 | #define do_for_each_ftrace_rec(pg, rec) \ | |
997 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ | |
998 | int _____i; \ | |
999 | for (_____i = 0; _____i < pg->index; _____i++) { \ | |
1000 | rec = &pg->records[_____i]; | |
1001 | ||
1002 | #define while_for_each_ftrace_rec() \ | |
1003 | } \ | |
1004 | } | |
1005 | ||
e309b41d | 1006 | static void ftrace_free_rec(struct dyn_ftrace *rec) |
37ad5084 | 1007 | { |
ee000b7f | 1008 | rec->freelist = ftrace_free_records; |
37ad5084 SR |
1009 | ftrace_free_records = rec; |
1010 | rec->flags |= FTRACE_FL_FREE; | |
1011 | } | |
1012 | ||
e309b41d | 1013 | static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip) |
3c1720f0 | 1014 | { |
37ad5084 SR |
1015 | struct dyn_ftrace *rec; |
1016 | ||
1017 | /* First check for freed records */ | |
1018 | if (ftrace_free_records) { | |
1019 | rec = ftrace_free_records; | |
1020 | ||
37ad5084 | 1021 | if (unlikely(!(rec->flags & FTRACE_FL_FREE))) { |
6912896e | 1022 | FTRACE_WARN_ON_ONCE(1); |
37ad5084 SR |
1023 | ftrace_free_records = NULL; |
1024 | return NULL; | |
1025 | } | |
1026 | ||
ee000b7f | 1027 | ftrace_free_records = rec->freelist; |
37ad5084 SR |
1028 | memset(rec, 0, sizeof(*rec)); |
1029 | return rec; | |
1030 | } | |
1031 | ||
3c1720f0 | 1032 | if (ftrace_pages->index == ENTRIES_PER_PAGE) { |
08f5ac90 SR |
1033 | if (!ftrace_pages->next) { |
1034 | /* allocate another page */ | |
1035 | ftrace_pages->next = | |
1036 | (void *)get_zeroed_page(GFP_KERNEL); | |
1037 | if (!ftrace_pages->next) | |
1038 | return NULL; | |
1039 | } | |
3c1720f0 SR |
1040 | ftrace_pages = ftrace_pages->next; |
1041 | } | |
1042 | ||
1043 | return &ftrace_pages->records[ftrace_pages->index++]; | |
1044 | } | |
1045 | ||
08f5ac90 | 1046 | static struct dyn_ftrace * |
d61f82d0 | 1047 | ftrace_record_ip(unsigned long ip) |
3d083395 | 1048 | { |
08f5ac90 | 1049 | struct dyn_ftrace *rec; |
3d083395 | 1050 | |
f3c7ac40 | 1051 | if (ftrace_disabled) |
08f5ac90 | 1052 | return NULL; |
3d083395 | 1053 | |
08f5ac90 SR |
1054 | rec = ftrace_alloc_dyn_node(ip); |
1055 | if (!rec) | |
1056 | return NULL; | |
3d083395 | 1057 | |
08f5ac90 | 1058 | rec->ip = ip; |
ee000b7f | 1059 | rec->newlist = ftrace_new_addrs; |
e94142a6 | 1060 | ftrace_new_addrs = rec; |
3d083395 | 1061 | |
08f5ac90 | 1062 | return rec; |
3d083395 SR |
1063 | } |
1064 | ||
b17e8a37 SR |
1065 | static void print_ip_ins(const char *fmt, unsigned char *p) |
1066 | { | |
1067 | int i; | |
1068 | ||
1069 | printk(KERN_CONT "%s", fmt); | |
1070 | ||
1071 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) | |
1072 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); | |
1073 | } | |
1074 | ||
31e88909 | 1075 | static void ftrace_bug(int failed, unsigned long ip) |
b17e8a37 SR |
1076 | { |
1077 | switch (failed) { | |
1078 | case -EFAULT: | |
1079 | FTRACE_WARN_ON_ONCE(1); | |
1080 | pr_info("ftrace faulted on modifying "); | |
1081 | print_ip_sym(ip); | |
1082 | break; | |
1083 | case -EINVAL: | |
1084 | FTRACE_WARN_ON_ONCE(1); | |
1085 | pr_info("ftrace failed to modify "); | |
1086 | print_ip_sym(ip); | |
b17e8a37 | 1087 | print_ip_ins(" actual: ", (unsigned char *)ip); |
b17e8a37 SR |
1088 | printk(KERN_CONT "\n"); |
1089 | break; | |
1090 | case -EPERM: | |
1091 | FTRACE_WARN_ON_ONCE(1); | |
1092 | pr_info("ftrace faulted on writing "); | |
1093 | print_ip_sym(ip); | |
1094 | break; | |
1095 | default: | |
1096 | FTRACE_WARN_ON_ONCE(1); | |
1097 | pr_info("ftrace faulted on unknown error "); | |
1098 | print_ip_sym(ip); | |
1099 | } | |
1100 | } | |
1101 | ||
3c1720f0 | 1102 | |
2cfa1978 MH |
1103 | /* Return 1 if the address range is reserved for ftrace */ |
1104 | int ftrace_text_reserved(void *start, void *end) | |
1105 | { | |
1106 | struct dyn_ftrace *rec; | |
1107 | struct ftrace_page *pg; | |
1108 | ||
1109 | do_for_each_ftrace_rec(pg, rec) { | |
1110 | if (rec->ip <= (unsigned long)end && | |
1111 | rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start) | |
1112 | return 1; | |
1113 | } while_for_each_ftrace_rec(); | |
1114 | return 0; | |
1115 | } | |
1116 | ||
1117 | ||
0eb96701 | 1118 | static int |
31e88909 | 1119 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
5072c59f | 1120 | { |
f45948e8 | 1121 | struct ftrace_ops *ops = &global_ops; |
e7d3737e | 1122 | unsigned long ftrace_addr; |
64fbcd16 | 1123 | unsigned long flag = 0UL; |
e7d3737e | 1124 | |
f0001207 | 1125 | ftrace_addr = (unsigned long)FTRACE_ADDR; |
5072c59f | 1126 | |
982c350b | 1127 | /* |
64fbcd16 XG |
1128 | * If this record is not to be traced or we want to disable it, |
1129 | * then disable it. | |
982c350b | 1130 | * |
64fbcd16 | 1131 | * If we want to enable it and filtering is off, then enable it. |
982c350b | 1132 | * |
64fbcd16 XG |
1133 | * If we want to enable it and filtering is on, enable it only if |
1134 | * it's filtered | |
982c350b | 1135 | */ |
f45948e8 SR |
1136 | if (enable && !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) { |
1137 | if (!ops->filter_hash->count || | |
1138 | ftrace_lookup_ip(ops->filter_hash, rec->ip)) | |
64fbcd16 XG |
1139 | flag = FTRACE_FL_ENABLED; |
1140 | } | |
982c350b | 1141 | |
64fbcd16 XG |
1142 | /* If the state of this record hasn't changed, then do nothing */ |
1143 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) | |
1144 | return 0; | |
982c350b | 1145 | |
64fbcd16 XG |
1146 | if (flag) { |
1147 | rec->flags |= FTRACE_FL_ENABLED; | |
1148 | return ftrace_make_call(rec, ftrace_addr); | |
5072c59f SR |
1149 | } |
1150 | ||
64fbcd16 XG |
1151 | rec->flags &= ~FTRACE_FL_ENABLED; |
1152 | return ftrace_make_nop(NULL, rec, ftrace_addr); | |
5072c59f SR |
1153 | } |
1154 | ||
e309b41d | 1155 | static void ftrace_replace_code(int enable) |
3c1720f0 | 1156 | { |
3c1720f0 SR |
1157 | struct dyn_ftrace *rec; |
1158 | struct ftrace_page *pg; | |
6a24a244 | 1159 | int failed; |
3c1720f0 | 1160 | |
45a4a237 SR |
1161 | if (unlikely(ftrace_disabled)) |
1162 | return; | |
1163 | ||
265c831c | 1164 | do_for_each_ftrace_rec(pg, rec) { |
d2c8c3ea SR |
1165 | /* Skip over free records */ |
1166 | if (rec->flags & FTRACE_FL_FREE) | |
265c831c SR |
1167 | continue; |
1168 | ||
265c831c | 1169 | failed = __ftrace_replace_code(rec, enable); |
fa9d13cf | 1170 | if (failed) { |
3279ba37 SR |
1171 | ftrace_bug(failed, rec->ip); |
1172 | /* Stop processing */ | |
1173 | return; | |
3c1720f0 | 1174 | } |
265c831c | 1175 | } while_for_each_ftrace_rec(); |
3c1720f0 SR |
1176 | } |
1177 | ||
492a7ea5 | 1178 | static int |
31e88909 | 1179 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
3c1720f0 SR |
1180 | { |
1181 | unsigned long ip; | |
593eb8a2 | 1182 | int ret; |
3c1720f0 SR |
1183 | |
1184 | ip = rec->ip; | |
1185 | ||
45a4a237 SR |
1186 | if (unlikely(ftrace_disabled)) |
1187 | return 0; | |
1188 | ||
25aac9dc | 1189 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
593eb8a2 | 1190 | if (ret) { |
31e88909 | 1191 | ftrace_bug(ret, ip); |
492a7ea5 | 1192 | return 0; |
37ad5084 | 1193 | } |
492a7ea5 | 1194 | return 1; |
3c1720f0 SR |
1195 | } |
1196 | ||
000ab691 SR |
1197 | /* |
1198 | * archs can override this function if they must do something | |
1199 | * before the modifying code is performed. | |
1200 | */ | |
1201 | int __weak ftrace_arch_code_modify_prepare(void) | |
1202 | { | |
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | /* | |
1207 | * archs can override this function if they must do something | |
1208 | * after the modifying code is performed. | |
1209 | */ | |
1210 | int __weak ftrace_arch_code_modify_post_process(void) | |
1211 | { | |
1212 | return 0; | |
1213 | } | |
1214 | ||
e309b41d | 1215 | static int __ftrace_modify_code(void *data) |
3d083395 | 1216 | { |
d61f82d0 SR |
1217 | int *command = data; |
1218 | ||
a3583244 | 1219 | if (*command & FTRACE_ENABLE_CALLS) |
d61f82d0 | 1220 | ftrace_replace_code(1); |
a3583244 | 1221 | else if (*command & FTRACE_DISABLE_CALLS) |
d61f82d0 SR |
1222 | ftrace_replace_code(0); |
1223 | ||
1224 | if (*command & FTRACE_UPDATE_TRACE_FUNC) | |
1225 | ftrace_update_ftrace_func(ftrace_trace_function); | |
1226 | ||
5a45cfe1 SR |
1227 | if (*command & FTRACE_START_FUNC_RET) |
1228 | ftrace_enable_ftrace_graph_caller(); | |
1229 | else if (*command & FTRACE_STOP_FUNC_RET) | |
1230 | ftrace_disable_ftrace_graph_caller(); | |
1231 | ||
d61f82d0 | 1232 | return 0; |
3d083395 SR |
1233 | } |
1234 | ||
e309b41d | 1235 | static void ftrace_run_update_code(int command) |
3d083395 | 1236 | { |
000ab691 SR |
1237 | int ret; |
1238 | ||
1239 | ret = ftrace_arch_code_modify_prepare(); | |
1240 | FTRACE_WARN_ON(ret); | |
1241 | if (ret) | |
1242 | return; | |
1243 | ||
784e2d76 | 1244 | stop_machine(__ftrace_modify_code, &command, NULL); |
000ab691 SR |
1245 | |
1246 | ret = ftrace_arch_code_modify_post_process(); | |
1247 | FTRACE_WARN_ON(ret); | |
3d083395 SR |
1248 | } |
1249 | ||
d61f82d0 | 1250 | static ftrace_func_t saved_ftrace_func; |
60a7ecf4 | 1251 | static int ftrace_start_up; |
df4fc315 SR |
1252 | |
1253 | static void ftrace_startup_enable(int command) | |
1254 | { | |
1255 | if (saved_ftrace_func != ftrace_trace_function) { | |
1256 | saved_ftrace_func = ftrace_trace_function; | |
1257 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
1258 | } | |
1259 | ||
1260 | if (!command || !ftrace_enabled) | |
1261 | return; | |
1262 | ||
1263 | ftrace_run_update_code(command); | |
1264 | } | |
d61f82d0 | 1265 | |
5a45cfe1 | 1266 | static void ftrace_startup(int command) |
3d083395 | 1267 | { |
4eebcc81 SR |
1268 | if (unlikely(ftrace_disabled)) |
1269 | return; | |
1270 | ||
60a7ecf4 | 1271 | ftrace_start_up++; |
982c350b | 1272 | command |= FTRACE_ENABLE_CALLS; |
d61f82d0 | 1273 | |
df4fc315 | 1274 | ftrace_startup_enable(command); |
3d083395 SR |
1275 | } |
1276 | ||
5a45cfe1 | 1277 | static void ftrace_shutdown(int command) |
3d083395 | 1278 | { |
4eebcc81 SR |
1279 | if (unlikely(ftrace_disabled)) |
1280 | return; | |
1281 | ||
60a7ecf4 | 1282 | ftrace_start_up--; |
9ea1a153 FW |
1283 | /* |
1284 | * Just warn in case of unbalance, no need to kill ftrace, it's not | |
1285 | * critical but the ftrace_call callers may be never nopped again after | |
1286 | * further ftrace uses. | |
1287 | */ | |
1288 | WARN_ON_ONCE(ftrace_start_up < 0); | |
1289 | ||
60a7ecf4 | 1290 | if (!ftrace_start_up) |
d61f82d0 | 1291 | command |= FTRACE_DISABLE_CALLS; |
3d083395 | 1292 | |
d61f82d0 SR |
1293 | if (saved_ftrace_func != ftrace_trace_function) { |
1294 | saved_ftrace_func = ftrace_trace_function; | |
1295 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
1296 | } | |
3d083395 | 1297 | |
d61f82d0 | 1298 | if (!command || !ftrace_enabled) |
e6ea44e9 | 1299 | return; |
d61f82d0 SR |
1300 | |
1301 | ftrace_run_update_code(command); | |
3d083395 SR |
1302 | } |
1303 | ||
e309b41d | 1304 | static void ftrace_startup_sysctl(void) |
b0fc494f | 1305 | { |
4eebcc81 SR |
1306 | if (unlikely(ftrace_disabled)) |
1307 | return; | |
1308 | ||
d61f82d0 SR |
1309 | /* Force update next time */ |
1310 | saved_ftrace_func = NULL; | |
60a7ecf4 SR |
1311 | /* ftrace_start_up is true if we want ftrace running */ |
1312 | if (ftrace_start_up) | |
79e406d7 | 1313 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); |
b0fc494f SR |
1314 | } |
1315 | ||
e309b41d | 1316 | static void ftrace_shutdown_sysctl(void) |
b0fc494f | 1317 | { |
4eebcc81 SR |
1318 | if (unlikely(ftrace_disabled)) |
1319 | return; | |
1320 | ||
60a7ecf4 SR |
1321 | /* ftrace_start_up is true if ftrace is running */ |
1322 | if (ftrace_start_up) | |
79e406d7 | 1323 | ftrace_run_update_code(FTRACE_DISABLE_CALLS); |
b0fc494f SR |
1324 | } |
1325 | ||
3d083395 SR |
1326 | static cycle_t ftrace_update_time; |
1327 | static unsigned long ftrace_update_cnt; | |
1328 | unsigned long ftrace_update_tot_cnt; | |
1329 | ||
31e88909 | 1330 | static int ftrace_update_code(struct module *mod) |
3d083395 | 1331 | { |
e94142a6 | 1332 | struct dyn_ftrace *p; |
f22f9a89 | 1333 | cycle_t start, stop; |
3d083395 | 1334 | |
750ed1a4 | 1335 | start = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
1336 | ftrace_update_cnt = 0; |
1337 | ||
e94142a6 | 1338 | while (ftrace_new_addrs) { |
3d083395 | 1339 | |
08f5ac90 SR |
1340 | /* If something went wrong, bail without enabling anything */ |
1341 | if (unlikely(ftrace_disabled)) | |
1342 | return -1; | |
f22f9a89 | 1343 | |
e94142a6 | 1344 | p = ftrace_new_addrs; |
ee000b7f | 1345 | ftrace_new_addrs = p->newlist; |
e94142a6 | 1346 | p->flags = 0L; |
f22f9a89 | 1347 | |
5cb084bb | 1348 | /* |
25985edc | 1349 | * Do the initial record conversion from mcount jump |
5cb084bb JO |
1350 | * to the NOP instructions. |
1351 | */ | |
1352 | if (!ftrace_code_disable(mod, p)) { | |
08f5ac90 | 1353 | ftrace_free_rec(p); |
d2c8c3ea SR |
1354 | /* Game over */ |
1355 | break; | |
5cb084bb JO |
1356 | } |
1357 | ||
5cb084bb JO |
1358 | ftrace_update_cnt++; |
1359 | ||
1360 | /* | |
1361 | * If the tracing is enabled, go ahead and enable the record. | |
1362 | * | |
1363 | * The reason not to enable the record immediatelly is the | |
1364 | * inherent check of ftrace_make_nop/ftrace_make_call for | |
1365 | * correct previous instructions. Making first the NOP | |
1366 | * conversion puts the module to the correct state, thus | |
1367 | * passing the ftrace_make_call check. | |
1368 | */ | |
1369 | if (ftrace_start_up) { | |
1370 | int failed = __ftrace_replace_code(p, 1); | |
1371 | if (failed) { | |
1372 | ftrace_bug(failed, p->ip); | |
1373 | ftrace_free_rec(p); | |
1374 | } | |
1375 | } | |
3d083395 SR |
1376 | } |
1377 | ||
750ed1a4 | 1378 | stop = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
1379 | ftrace_update_time = stop - start; |
1380 | ftrace_update_tot_cnt += ftrace_update_cnt; | |
1381 | ||
16444a8a ACM |
1382 | return 0; |
1383 | } | |
1384 | ||
68bf21aa | 1385 | static int __init ftrace_dyn_table_alloc(unsigned long num_to_init) |
3c1720f0 SR |
1386 | { |
1387 | struct ftrace_page *pg; | |
1388 | int cnt; | |
1389 | int i; | |
3c1720f0 SR |
1390 | |
1391 | /* allocate a few pages */ | |
1392 | ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL); | |
1393 | if (!ftrace_pages_start) | |
1394 | return -1; | |
1395 | ||
1396 | /* | |
1397 | * Allocate a few more pages. | |
1398 | * | |
1399 | * TODO: have some parser search vmlinux before | |
1400 | * final linking to find all calls to ftrace. | |
1401 | * Then we can: | |
1402 | * a) know how many pages to allocate. | |
1403 | * and/or | |
1404 | * b) set up the table then. | |
1405 | * | |
1406 | * The dynamic code is still necessary for | |
1407 | * modules. | |
1408 | */ | |
1409 | ||
1410 | pg = ftrace_pages = ftrace_pages_start; | |
1411 | ||
68bf21aa | 1412 | cnt = num_to_init / ENTRIES_PER_PAGE; |
08f5ac90 | 1413 | pr_info("ftrace: allocating %ld entries in %d pages\n", |
5821e1b7 | 1414 | num_to_init, cnt + 1); |
3c1720f0 SR |
1415 | |
1416 | for (i = 0; i < cnt; i++) { | |
1417 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); | |
1418 | ||
1419 | /* If we fail, we'll try later anyway */ | |
1420 | if (!pg->next) | |
1421 | break; | |
1422 | ||
1423 | pg = pg->next; | |
1424 | } | |
1425 | ||
1426 | return 0; | |
1427 | } | |
1428 | ||
5072c59f SR |
1429 | enum { |
1430 | FTRACE_ITER_FILTER = (1 << 0), | |
689fd8b6 | 1431 | FTRACE_ITER_NOTRACE = (1 << 1), |
3499e461 SR |
1432 | FTRACE_ITER_PRINTALL = (1 << 2), |
1433 | FTRACE_ITER_HASH = (1 << 3), | |
5072c59f SR |
1434 | }; |
1435 | ||
1436 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ | |
1437 | ||
1438 | struct ftrace_iterator { | |
98c4fd04 | 1439 | loff_t pos; |
4aeb6967 SR |
1440 | loff_t func_pos; |
1441 | struct ftrace_page *pg; | |
1442 | struct dyn_ftrace *func; | |
1443 | struct ftrace_func_probe *probe; | |
1444 | struct trace_parser parser; | |
1cf41dd7 | 1445 | struct ftrace_hash *hash; |
4aeb6967 SR |
1446 | int hidx; |
1447 | int idx; | |
1448 | unsigned flags; | |
5072c59f SR |
1449 | }; |
1450 | ||
8fc0c701 | 1451 | static void * |
4aeb6967 | 1452 | t_hash_next(struct seq_file *m, loff_t *pos) |
8fc0c701 SR |
1453 | { |
1454 | struct ftrace_iterator *iter = m->private; | |
4aeb6967 | 1455 | struct hlist_node *hnd = NULL; |
8fc0c701 SR |
1456 | struct hlist_head *hhd; |
1457 | ||
8fc0c701 | 1458 | (*pos)++; |
98c4fd04 | 1459 | iter->pos = *pos; |
8fc0c701 | 1460 | |
4aeb6967 SR |
1461 | if (iter->probe) |
1462 | hnd = &iter->probe->node; | |
8fc0c701 SR |
1463 | retry: |
1464 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) | |
1465 | return NULL; | |
1466 | ||
1467 | hhd = &ftrace_func_hash[iter->hidx]; | |
1468 | ||
1469 | if (hlist_empty(hhd)) { | |
1470 | iter->hidx++; | |
1471 | hnd = NULL; | |
1472 | goto retry; | |
1473 | } | |
1474 | ||
1475 | if (!hnd) | |
1476 | hnd = hhd->first; | |
1477 | else { | |
1478 | hnd = hnd->next; | |
1479 | if (!hnd) { | |
1480 | iter->hidx++; | |
1481 | goto retry; | |
1482 | } | |
1483 | } | |
1484 | ||
4aeb6967 SR |
1485 | if (WARN_ON_ONCE(!hnd)) |
1486 | return NULL; | |
1487 | ||
1488 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); | |
1489 | ||
1490 | return iter; | |
8fc0c701 SR |
1491 | } |
1492 | ||
1493 | static void *t_hash_start(struct seq_file *m, loff_t *pos) | |
1494 | { | |
1495 | struct ftrace_iterator *iter = m->private; | |
1496 | void *p = NULL; | |
d82d6244 LZ |
1497 | loff_t l; |
1498 | ||
2bccfffd SR |
1499 | if (iter->func_pos > *pos) |
1500 | return NULL; | |
8fc0c701 | 1501 | |
d82d6244 | 1502 | iter->hidx = 0; |
2bccfffd | 1503 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
4aeb6967 | 1504 | p = t_hash_next(m, &l); |
d82d6244 LZ |
1505 | if (!p) |
1506 | break; | |
1507 | } | |
4aeb6967 SR |
1508 | if (!p) |
1509 | return NULL; | |
1510 | ||
98c4fd04 SR |
1511 | /* Only set this if we have an item */ |
1512 | iter->flags |= FTRACE_ITER_HASH; | |
1513 | ||
4aeb6967 | 1514 | return iter; |
8fc0c701 SR |
1515 | } |
1516 | ||
4aeb6967 SR |
1517 | static int |
1518 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) | |
8fc0c701 | 1519 | { |
b6887d79 | 1520 | struct ftrace_func_probe *rec; |
8fc0c701 | 1521 | |
4aeb6967 SR |
1522 | rec = iter->probe; |
1523 | if (WARN_ON_ONCE(!rec)) | |
1524 | return -EIO; | |
8fc0c701 | 1525 | |
809dcf29 SR |
1526 | if (rec->ops->print) |
1527 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); | |
1528 | ||
b375a11a | 1529 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
8fc0c701 SR |
1530 | |
1531 | if (rec->data) | |
1532 | seq_printf(m, ":%p", rec->data); | |
1533 | seq_putc(m, '\n'); | |
1534 | ||
1535 | return 0; | |
1536 | } | |
1537 | ||
e309b41d | 1538 | static void * |
5072c59f SR |
1539 | t_next(struct seq_file *m, void *v, loff_t *pos) |
1540 | { | |
1541 | struct ftrace_iterator *iter = m->private; | |
f45948e8 | 1542 | struct ftrace_ops *ops = &global_ops; |
5072c59f SR |
1543 | struct dyn_ftrace *rec = NULL; |
1544 | ||
45a4a237 SR |
1545 | if (unlikely(ftrace_disabled)) |
1546 | return NULL; | |
1547 | ||
8fc0c701 | 1548 | if (iter->flags & FTRACE_ITER_HASH) |
4aeb6967 | 1549 | return t_hash_next(m, pos); |
8fc0c701 | 1550 | |
5072c59f | 1551 | (*pos)++; |
1106b699 | 1552 | iter->pos = iter->func_pos = *pos; |
5072c59f | 1553 | |
0c75a3ed | 1554 | if (iter->flags & FTRACE_ITER_PRINTALL) |
57c072c7 | 1555 | return t_hash_start(m, pos); |
0c75a3ed | 1556 | |
5072c59f SR |
1557 | retry: |
1558 | if (iter->idx >= iter->pg->index) { | |
1559 | if (iter->pg->next) { | |
1560 | iter->pg = iter->pg->next; | |
1561 | iter->idx = 0; | |
1562 | goto retry; | |
1563 | } | |
1564 | } else { | |
1565 | rec = &iter->pg->records[iter->idx++]; | |
a9fdda33 SR |
1566 | if ((rec->flags & FTRACE_FL_FREE) || |
1567 | ||
0183fb1c | 1568 | ((iter->flags & FTRACE_ITER_FILTER) && |
f45948e8 | 1569 | !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) || |
0183fb1c | 1570 | |
41c52c0d | 1571 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
f45948e8 | 1572 | !ftrace_lookup_ip(ops->notrace_hash, rec->ip))) { |
5072c59f SR |
1573 | rec = NULL; |
1574 | goto retry; | |
1575 | } | |
1576 | } | |
1577 | ||
4aeb6967 | 1578 | if (!rec) |
57c072c7 | 1579 | return t_hash_start(m, pos); |
4aeb6967 SR |
1580 | |
1581 | iter->func = rec; | |
1582 | ||
1583 | return iter; | |
5072c59f SR |
1584 | } |
1585 | ||
98c4fd04 SR |
1586 | static void reset_iter_read(struct ftrace_iterator *iter) |
1587 | { | |
1588 | iter->pos = 0; | |
1589 | iter->func_pos = 0; | |
1590 | iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH); | |
5072c59f SR |
1591 | } |
1592 | ||
1593 | static void *t_start(struct seq_file *m, loff_t *pos) | |
1594 | { | |
1595 | struct ftrace_iterator *iter = m->private; | |
f45948e8 | 1596 | struct ftrace_ops *ops = &global_ops; |
5072c59f | 1597 | void *p = NULL; |
694ce0a5 | 1598 | loff_t l; |
5072c59f | 1599 | |
8fc0c701 | 1600 | mutex_lock(&ftrace_lock); |
45a4a237 SR |
1601 | |
1602 | if (unlikely(ftrace_disabled)) | |
1603 | return NULL; | |
1604 | ||
98c4fd04 SR |
1605 | /* |
1606 | * If an lseek was done, then reset and start from beginning. | |
1607 | */ | |
1608 | if (*pos < iter->pos) | |
1609 | reset_iter_read(iter); | |
1610 | ||
0c75a3ed SR |
1611 | /* |
1612 | * For set_ftrace_filter reading, if we have the filter | |
1613 | * off, we can short cut and just print out that all | |
1614 | * functions are enabled. | |
1615 | */ | |
f45948e8 | 1616 | if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) { |
0c75a3ed | 1617 | if (*pos > 0) |
8fc0c701 | 1618 | return t_hash_start(m, pos); |
0c75a3ed | 1619 | iter->flags |= FTRACE_ITER_PRINTALL; |
df091625 CW |
1620 | /* reset in case of seek/pread */ |
1621 | iter->flags &= ~FTRACE_ITER_HASH; | |
0c75a3ed SR |
1622 | return iter; |
1623 | } | |
1624 | ||
8fc0c701 SR |
1625 | if (iter->flags & FTRACE_ITER_HASH) |
1626 | return t_hash_start(m, pos); | |
1627 | ||
98c4fd04 SR |
1628 | /* |
1629 | * Unfortunately, we need to restart at ftrace_pages_start | |
1630 | * every time we let go of the ftrace_mutex. This is because | |
1631 | * those pointers can change without the lock. | |
1632 | */ | |
694ce0a5 LZ |
1633 | iter->pg = ftrace_pages_start; |
1634 | iter->idx = 0; | |
1635 | for (l = 0; l <= *pos; ) { | |
1636 | p = t_next(m, p, &l); | |
1637 | if (!p) | |
1638 | break; | |
50cdaf08 | 1639 | } |
5821e1b7 | 1640 | |
4aeb6967 SR |
1641 | if (!p) { |
1642 | if (iter->flags & FTRACE_ITER_FILTER) | |
1643 | return t_hash_start(m, pos); | |
8fc0c701 | 1644 | |
4aeb6967 SR |
1645 | return NULL; |
1646 | } | |
1647 | ||
1648 | return iter; | |
5072c59f SR |
1649 | } |
1650 | ||
1651 | static void t_stop(struct seq_file *m, void *p) | |
1652 | { | |
8fc0c701 | 1653 | mutex_unlock(&ftrace_lock); |
5072c59f SR |
1654 | } |
1655 | ||
1656 | static int t_show(struct seq_file *m, void *v) | |
1657 | { | |
0c75a3ed | 1658 | struct ftrace_iterator *iter = m->private; |
4aeb6967 | 1659 | struct dyn_ftrace *rec; |
5072c59f | 1660 | |
8fc0c701 | 1661 | if (iter->flags & FTRACE_ITER_HASH) |
4aeb6967 | 1662 | return t_hash_show(m, iter); |
8fc0c701 | 1663 | |
0c75a3ed SR |
1664 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
1665 | seq_printf(m, "#### all functions enabled ####\n"); | |
1666 | return 0; | |
1667 | } | |
1668 | ||
4aeb6967 SR |
1669 | rec = iter->func; |
1670 | ||
5072c59f SR |
1671 | if (!rec) |
1672 | return 0; | |
1673 | ||
b375a11a | 1674 | seq_printf(m, "%ps\n", (void *)rec->ip); |
5072c59f SR |
1675 | |
1676 | return 0; | |
1677 | } | |
1678 | ||
88e9d34c | 1679 | static const struct seq_operations show_ftrace_seq_ops = { |
5072c59f SR |
1680 | .start = t_start, |
1681 | .next = t_next, | |
1682 | .stop = t_stop, | |
1683 | .show = t_show, | |
1684 | }; | |
1685 | ||
e309b41d | 1686 | static int |
5072c59f SR |
1687 | ftrace_avail_open(struct inode *inode, struct file *file) |
1688 | { | |
1689 | struct ftrace_iterator *iter; | |
1690 | int ret; | |
1691 | ||
4eebcc81 SR |
1692 | if (unlikely(ftrace_disabled)) |
1693 | return -ENODEV; | |
1694 | ||
5072c59f SR |
1695 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
1696 | if (!iter) | |
1697 | return -ENOMEM; | |
1698 | ||
1699 | iter->pg = ftrace_pages_start; | |
5072c59f SR |
1700 | |
1701 | ret = seq_open(file, &show_ftrace_seq_ops); | |
1702 | if (!ret) { | |
1703 | struct seq_file *m = file->private_data; | |
4bf39a94 | 1704 | |
5072c59f | 1705 | m->private = iter; |
4bf39a94 | 1706 | } else { |
5072c59f | 1707 | kfree(iter); |
4bf39a94 | 1708 | } |
5072c59f SR |
1709 | |
1710 | return ret; | |
1711 | } | |
1712 | ||
1cf41dd7 | 1713 | static void ftrace_filter_reset(struct ftrace_hash *hash) |
5072c59f | 1714 | { |
52baf119 | 1715 | mutex_lock(&ftrace_lock); |
1cf41dd7 | 1716 | ftrace_hash_clear(hash); |
52baf119 | 1717 | mutex_unlock(&ftrace_lock); |
5072c59f SR |
1718 | } |
1719 | ||
e309b41d | 1720 | static int |
f45948e8 | 1721 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
1cf41dd7 | 1722 | struct inode *inode, struct file *file) |
5072c59f SR |
1723 | { |
1724 | struct ftrace_iterator *iter; | |
f45948e8 | 1725 | struct ftrace_hash *hash; |
5072c59f SR |
1726 | int ret = 0; |
1727 | ||
4eebcc81 SR |
1728 | if (unlikely(ftrace_disabled)) |
1729 | return -ENODEV; | |
1730 | ||
5072c59f SR |
1731 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
1732 | if (!iter) | |
1733 | return -ENOMEM; | |
1734 | ||
689fd8b6 | 1735 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
1736 | kfree(iter); | |
1737 | return -ENOMEM; | |
1738 | } | |
1739 | ||
f45948e8 SR |
1740 | if (flag & FTRACE_ITER_NOTRACE) |
1741 | hash = ops->notrace_hash; | |
1742 | else | |
1743 | hash = ops->filter_hash; | |
1744 | ||
1cf41dd7 SR |
1745 | iter->hash = hash; |
1746 | ||
41c52c0d | 1747 | mutex_lock(&ftrace_regex_lock); |
5072c59f | 1748 | if ((file->f_mode & FMODE_WRITE) && |
8650ae32 | 1749 | (file->f_flags & O_TRUNC)) |
1cf41dd7 | 1750 | ftrace_filter_reset(hash); |
5072c59f SR |
1751 | |
1752 | if (file->f_mode & FMODE_READ) { | |
1753 | iter->pg = ftrace_pages_start; | |
1cf41dd7 | 1754 | iter->flags = flag; |
5072c59f SR |
1755 | |
1756 | ret = seq_open(file, &show_ftrace_seq_ops); | |
1757 | if (!ret) { | |
1758 | struct seq_file *m = file->private_data; | |
1759 | m->private = iter; | |
79fe249c LZ |
1760 | } else { |
1761 | trace_parser_put(&iter->parser); | |
5072c59f | 1762 | kfree(iter); |
79fe249c | 1763 | } |
5072c59f SR |
1764 | } else |
1765 | file->private_data = iter; | |
41c52c0d | 1766 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
1767 | |
1768 | return ret; | |
1769 | } | |
1770 | ||
41c52c0d SR |
1771 | static int |
1772 | ftrace_filter_open(struct inode *inode, struct file *file) | |
1773 | { | |
f45948e8 | 1774 | return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER, |
1cf41dd7 | 1775 | inode, file); |
41c52c0d SR |
1776 | } |
1777 | ||
1778 | static int | |
1779 | ftrace_notrace_open(struct inode *inode, struct file *file) | |
1780 | { | |
f45948e8 | 1781 | return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE, |
1cf41dd7 | 1782 | inode, file); |
41c52c0d SR |
1783 | } |
1784 | ||
e309b41d | 1785 | static loff_t |
41c52c0d | 1786 | ftrace_regex_lseek(struct file *file, loff_t offset, int origin) |
5072c59f SR |
1787 | { |
1788 | loff_t ret; | |
1789 | ||
1790 | if (file->f_mode & FMODE_READ) | |
1791 | ret = seq_lseek(file, offset, origin); | |
1792 | else | |
1793 | file->f_pos = ret = 1; | |
1794 | ||
1795 | return ret; | |
1796 | } | |
1797 | ||
64e7c440 | 1798 | static int ftrace_match(char *str, char *regex, int len, int type) |
9f4801e3 | 1799 | { |
9f4801e3 | 1800 | int matched = 0; |
751e9983 | 1801 | int slen; |
9f4801e3 | 1802 | |
9f4801e3 SR |
1803 | switch (type) { |
1804 | case MATCH_FULL: | |
1805 | if (strcmp(str, regex) == 0) | |
1806 | matched = 1; | |
1807 | break; | |
1808 | case MATCH_FRONT_ONLY: | |
1809 | if (strncmp(str, regex, len) == 0) | |
1810 | matched = 1; | |
1811 | break; | |
1812 | case MATCH_MIDDLE_ONLY: | |
1813 | if (strstr(str, regex)) | |
1814 | matched = 1; | |
1815 | break; | |
1816 | case MATCH_END_ONLY: | |
751e9983 LZ |
1817 | slen = strlen(str); |
1818 | if (slen >= len && memcmp(str + slen - len, regex, len) == 0) | |
9f4801e3 SR |
1819 | matched = 1; |
1820 | break; | |
1821 | } | |
1822 | ||
1823 | return matched; | |
1824 | } | |
1825 | ||
b448c4e3 | 1826 | static int |
1cf41dd7 | 1827 | enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not) |
996e87be | 1828 | { |
b448c4e3 | 1829 | struct ftrace_func_entry *entry; |
b448c4e3 SR |
1830 | int ret = 0; |
1831 | ||
1cf41dd7 SR |
1832 | entry = ftrace_lookup_ip(hash, rec->ip); |
1833 | if (not) { | |
1834 | /* Do nothing if it doesn't exist */ | |
1835 | if (!entry) | |
1836 | return 0; | |
b448c4e3 | 1837 | |
1cf41dd7 SR |
1838 | remove_hash_entry(hash, entry); |
1839 | } else { | |
1840 | /* Do nothing if it exists */ | |
1841 | if (entry) | |
1842 | return 0; | |
b448c4e3 | 1843 | |
1cf41dd7 | 1844 | ret = add_hash_entry(hash, rec->ip); |
b448c4e3 SR |
1845 | } |
1846 | return ret; | |
996e87be SR |
1847 | } |
1848 | ||
64e7c440 | 1849 | static int |
b9df92d2 SR |
1850 | ftrace_match_record(struct dyn_ftrace *rec, char *mod, |
1851 | char *regex, int len, int type) | |
64e7c440 SR |
1852 | { |
1853 | char str[KSYM_SYMBOL_LEN]; | |
b9df92d2 SR |
1854 | char *modname; |
1855 | ||
1856 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); | |
1857 | ||
1858 | if (mod) { | |
1859 | /* module lookup requires matching the module */ | |
1860 | if (!modname || strcmp(modname, mod)) | |
1861 | return 0; | |
1862 | ||
1863 | /* blank search means to match all funcs in the mod */ | |
1864 | if (!len) | |
1865 | return 1; | |
1866 | } | |
64e7c440 | 1867 | |
64e7c440 SR |
1868 | return ftrace_match(str, regex, len, type); |
1869 | } | |
1870 | ||
1cf41dd7 SR |
1871 | static int |
1872 | match_records(struct ftrace_hash *hash, char *buff, | |
1873 | int len, char *mod, int not) | |
9f4801e3 | 1874 | { |
b9df92d2 | 1875 | unsigned search_len = 0; |
9f4801e3 SR |
1876 | struct ftrace_page *pg; |
1877 | struct dyn_ftrace *rec; | |
b9df92d2 SR |
1878 | int type = MATCH_FULL; |
1879 | char *search = buff; | |
311d16da | 1880 | int found = 0; |
b448c4e3 | 1881 | int ret; |
9f4801e3 | 1882 | |
b9df92d2 SR |
1883 | if (len) { |
1884 | type = filter_parse_regex(buff, len, &search, ¬); | |
1885 | search_len = strlen(search); | |
1886 | } | |
9f4801e3 | 1887 | |
52baf119 | 1888 | mutex_lock(&ftrace_lock); |
265c831c | 1889 | |
b9df92d2 SR |
1890 | if (unlikely(ftrace_disabled)) |
1891 | goto out_unlock; | |
9f4801e3 | 1892 | |
265c831c | 1893 | do_for_each_ftrace_rec(pg, rec) { |
265c831c | 1894 | |
b9df92d2 | 1895 | if (ftrace_match_record(rec, mod, search, search_len, type)) { |
1cf41dd7 | 1896 | ret = enter_record(hash, rec, not); |
b448c4e3 SR |
1897 | if (ret < 0) { |
1898 | found = ret; | |
1899 | goto out_unlock; | |
1900 | } | |
311d16da | 1901 | found = 1; |
265c831c SR |
1902 | } |
1903 | } while_for_each_ftrace_rec(); | |
b9df92d2 | 1904 | out_unlock: |
52baf119 | 1905 | mutex_unlock(&ftrace_lock); |
311d16da LZ |
1906 | |
1907 | return found; | |
5072c59f SR |
1908 | } |
1909 | ||
64e7c440 | 1910 | static int |
1cf41dd7 | 1911 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
64e7c440 | 1912 | { |
1cf41dd7 | 1913 | return match_records(hash, buff, len, NULL, 0); |
64e7c440 SR |
1914 | } |
1915 | ||
1cf41dd7 SR |
1916 | static int |
1917 | ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod) | |
64e7c440 | 1918 | { |
64e7c440 | 1919 | int not = 0; |
6a24a244 | 1920 | |
64e7c440 SR |
1921 | /* blank or '*' mean the same */ |
1922 | if (strcmp(buff, "*") == 0) | |
1923 | buff[0] = 0; | |
1924 | ||
1925 | /* handle the case of 'dont filter this module' */ | |
1926 | if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) { | |
1927 | buff[0] = 0; | |
1928 | not = 1; | |
1929 | } | |
1930 | ||
1cf41dd7 | 1931 | return match_records(hash, buff, strlen(buff), mod, not); |
64e7c440 SR |
1932 | } |
1933 | ||
f6180773 SR |
1934 | /* |
1935 | * We register the module command as a template to show others how | |
1936 | * to register the a command as well. | |
1937 | */ | |
1938 | ||
1939 | static int | |
1940 | ftrace_mod_callback(char *func, char *cmd, char *param, int enable) | |
1941 | { | |
f45948e8 | 1942 | struct ftrace_ops *ops = &global_ops; |
1cf41dd7 | 1943 | struct ftrace_hash *hash; |
f6180773 | 1944 | char *mod; |
b448c4e3 | 1945 | int ret = -EINVAL; |
f6180773 SR |
1946 | |
1947 | /* | |
1948 | * cmd == 'mod' because we only registered this func | |
1949 | * for the 'mod' ftrace_func_command. | |
1950 | * But if you register one func with multiple commands, | |
1951 | * you can tell which command was used by the cmd | |
1952 | * parameter. | |
1953 | */ | |
1954 | ||
1955 | /* we must have a module name */ | |
1956 | if (!param) | |
b448c4e3 | 1957 | return ret; |
f6180773 SR |
1958 | |
1959 | mod = strsep(¶m, ":"); | |
1960 | if (!strlen(mod)) | |
b448c4e3 | 1961 | return ret; |
f6180773 | 1962 | |
1cf41dd7 | 1963 | if (enable) |
f45948e8 | 1964 | hash = ops->filter_hash; |
1cf41dd7 | 1965 | else |
f45948e8 | 1966 | hash = ops->notrace_hash; |
1cf41dd7 SR |
1967 | |
1968 | ret = ftrace_match_module_records(hash, func, mod); | |
b448c4e3 SR |
1969 | if (!ret) |
1970 | ret = -EINVAL; | |
1971 | if (ret < 0) | |
1972 | return ret; | |
1973 | ||
1974 | return 0; | |
f6180773 SR |
1975 | } |
1976 | ||
1977 | static struct ftrace_func_command ftrace_mod_cmd = { | |
1978 | .name = "mod", | |
1979 | .func = ftrace_mod_callback, | |
1980 | }; | |
1981 | ||
1982 | static int __init ftrace_mod_cmd_init(void) | |
1983 | { | |
1984 | return register_ftrace_command(&ftrace_mod_cmd); | |
1985 | } | |
1986 | device_initcall(ftrace_mod_cmd_init); | |
1987 | ||
59df055f | 1988 | static void |
b6887d79 | 1989 | function_trace_probe_call(unsigned long ip, unsigned long parent_ip) |
59df055f | 1990 | { |
b6887d79 | 1991 | struct ftrace_func_probe *entry; |
59df055f SR |
1992 | struct hlist_head *hhd; |
1993 | struct hlist_node *n; | |
1994 | unsigned long key; | |
59df055f SR |
1995 | |
1996 | key = hash_long(ip, FTRACE_HASH_BITS); | |
1997 | ||
1998 | hhd = &ftrace_func_hash[key]; | |
1999 | ||
2000 | if (hlist_empty(hhd)) | |
2001 | return; | |
2002 | ||
2003 | /* | |
2004 | * Disable preemption for these calls to prevent a RCU grace | |
2005 | * period. This syncs the hash iteration and freeing of items | |
2006 | * on the hash. rcu_read_lock is too dangerous here. | |
2007 | */ | |
5168ae50 | 2008 | preempt_disable_notrace(); |
59df055f SR |
2009 | hlist_for_each_entry_rcu(entry, n, hhd, node) { |
2010 | if (entry->ip == ip) | |
2011 | entry->ops->func(ip, parent_ip, &entry->data); | |
2012 | } | |
5168ae50 | 2013 | preempt_enable_notrace(); |
59df055f SR |
2014 | } |
2015 | ||
b6887d79 | 2016 | static struct ftrace_ops trace_probe_ops __read_mostly = |
59df055f | 2017 | { |
fb9fb015 | 2018 | .func = function_trace_probe_call, |
59df055f SR |
2019 | }; |
2020 | ||
b6887d79 | 2021 | static int ftrace_probe_registered; |
59df055f | 2022 | |
b6887d79 | 2023 | static void __enable_ftrace_function_probe(void) |
59df055f SR |
2024 | { |
2025 | int i; | |
2026 | ||
b6887d79 | 2027 | if (ftrace_probe_registered) |
59df055f SR |
2028 | return; |
2029 | ||
2030 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
2031 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
2032 | if (hhd->first) | |
2033 | break; | |
2034 | } | |
2035 | /* Nothing registered? */ | |
2036 | if (i == FTRACE_FUNC_HASHSIZE) | |
2037 | return; | |
2038 | ||
b6887d79 | 2039 | __register_ftrace_function(&trace_probe_ops); |
59df055f | 2040 | ftrace_startup(0); |
b6887d79 | 2041 | ftrace_probe_registered = 1; |
59df055f SR |
2042 | } |
2043 | ||
b6887d79 | 2044 | static void __disable_ftrace_function_probe(void) |
59df055f SR |
2045 | { |
2046 | int i; | |
2047 | ||
b6887d79 | 2048 | if (!ftrace_probe_registered) |
59df055f SR |
2049 | return; |
2050 | ||
2051 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
2052 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
2053 | if (hhd->first) | |
2054 | return; | |
2055 | } | |
2056 | ||
2057 | /* no more funcs left */ | |
b6887d79 | 2058 | __unregister_ftrace_function(&trace_probe_ops); |
59df055f | 2059 | ftrace_shutdown(0); |
b6887d79 | 2060 | ftrace_probe_registered = 0; |
59df055f SR |
2061 | } |
2062 | ||
2063 | ||
2064 | static void ftrace_free_entry_rcu(struct rcu_head *rhp) | |
2065 | { | |
b6887d79 SR |
2066 | struct ftrace_func_probe *entry = |
2067 | container_of(rhp, struct ftrace_func_probe, rcu); | |
59df055f SR |
2068 | |
2069 | if (entry->ops->free) | |
2070 | entry->ops->free(&entry->data); | |
2071 | kfree(entry); | |
2072 | } | |
2073 | ||
2074 | ||
2075 | int | |
b6887d79 | 2076 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2077 | void *data) |
2078 | { | |
b6887d79 | 2079 | struct ftrace_func_probe *entry; |
59df055f SR |
2080 | struct ftrace_page *pg; |
2081 | struct dyn_ftrace *rec; | |
59df055f | 2082 | int type, len, not; |
6a24a244 | 2083 | unsigned long key; |
59df055f SR |
2084 | int count = 0; |
2085 | char *search; | |
2086 | ||
3f6fe06d | 2087 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
2088 | len = strlen(search); |
2089 | ||
b6887d79 | 2090 | /* we do not support '!' for function probes */ |
59df055f SR |
2091 | if (WARN_ON(not)) |
2092 | return -EINVAL; | |
2093 | ||
2094 | mutex_lock(&ftrace_lock); | |
59df055f | 2095 | |
45a4a237 SR |
2096 | if (unlikely(ftrace_disabled)) |
2097 | goto out_unlock; | |
59df055f | 2098 | |
45a4a237 | 2099 | do_for_each_ftrace_rec(pg, rec) { |
59df055f | 2100 | |
b9df92d2 | 2101 | if (!ftrace_match_record(rec, NULL, search, len, type)) |
59df055f SR |
2102 | continue; |
2103 | ||
2104 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
2105 | if (!entry) { | |
b6887d79 | 2106 | /* If we did not process any, then return error */ |
59df055f SR |
2107 | if (!count) |
2108 | count = -ENOMEM; | |
2109 | goto out_unlock; | |
2110 | } | |
2111 | ||
2112 | count++; | |
2113 | ||
2114 | entry->data = data; | |
2115 | ||
2116 | /* | |
2117 | * The caller might want to do something special | |
2118 | * for each function we find. We call the callback | |
2119 | * to give the caller an opportunity to do so. | |
2120 | */ | |
2121 | if (ops->callback) { | |
2122 | if (ops->callback(rec->ip, &entry->data) < 0) { | |
2123 | /* caller does not like this func */ | |
2124 | kfree(entry); | |
2125 | continue; | |
2126 | } | |
2127 | } | |
2128 | ||
2129 | entry->ops = ops; | |
2130 | entry->ip = rec->ip; | |
2131 | ||
2132 | key = hash_long(entry->ip, FTRACE_HASH_BITS); | |
2133 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); | |
2134 | ||
2135 | } while_for_each_ftrace_rec(); | |
b6887d79 | 2136 | __enable_ftrace_function_probe(); |
59df055f SR |
2137 | |
2138 | out_unlock: | |
2139 | mutex_unlock(&ftrace_lock); | |
2140 | ||
2141 | return count; | |
2142 | } | |
2143 | ||
2144 | enum { | |
b6887d79 SR |
2145 | PROBE_TEST_FUNC = 1, |
2146 | PROBE_TEST_DATA = 2 | |
59df055f SR |
2147 | }; |
2148 | ||
2149 | static void | |
b6887d79 | 2150 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2151 | void *data, int flags) |
2152 | { | |
b6887d79 | 2153 | struct ftrace_func_probe *entry; |
59df055f SR |
2154 | struct hlist_node *n, *tmp; |
2155 | char str[KSYM_SYMBOL_LEN]; | |
2156 | int type = MATCH_FULL; | |
2157 | int i, len = 0; | |
2158 | char *search; | |
2159 | ||
b36461da | 2160 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
59df055f | 2161 | glob = NULL; |
b36461da | 2162 | else if (glob) { |
59df055f SR |
2163 | int not; |
2164 | ||
3f6fe06d | 2165 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
2166 | len = strlen(search); |
2167 | ||
b6887d79 | 2168 | /* we do not support '!' for function probes */ |
59df055f SR |
2169 | if (WARN_ON(not)) |
2170 | return; | |
2171 | } | |
2172 | ||
2173 | mutex_lock(&ftrace_lock); | |
2174 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
2175 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
2176 | ||
2177 | hlist_for_each_entry_safe(entry, n, tmp, hhd, node) { | |
2178 | ||
2179 | /* break up if statements for readability */ | |
b6887d79 | 2180 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
59df055f SR |
2181 | continue; |
2182 | ||
b6887d79 | 2183 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
59df055f SR |
2184 | continue; |
2185 | ||
2186 | /* do this last, since it is the most expensive */ | |
2187 | if (glob) { | |
2188 | kallsyms_lookup(entry->ip, NULL, NULL, | |
2189 | NULL, str); | |
2190 | if (!ftrace_match(str, glob, len, type)) | |
2191 | continue; | |
2192 | } | |
2193 | ||
2194 | hlist_del(&entry->node); | |
2195 | call_rcu(&entry->rcu, ftrace_free_entry_rcu); | |
2196 | } | |
2197 | } | |
b6887d79 | 2198 | __disable_ftrace_function_probe(); |
59df055f SR |
2199 | mutex_unlock(&ftrace_lock); |
2200 | } | |
2201 | ||
2202 | void | |
b6887d79 | 2203 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2204 | void *data) |
2205 | { | |
b6887d79 SR |
2206 | __unregister_ftrace_function_probe(glob, ops, data, |
2207 | PROBE_TEST_FUNC | PROBE_TEST_DATA); | |
59df055f SR |
2208 | } |
2209 | ||
2210 | void | |
b6887d79 | 2211 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
59df055f | 2212 | { |
b6887d79 | 2213 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
59df055f SR |
2214 | } |
2215 | ||
b6887d79 | 2216 | void unregister_ftrace_function_probe_all(char *glob) |
59df055f | 2217 | { |
b6887d79 | 2218 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
59df055f SR |
2219 | } |
2220 | ||
f6180773 SR |
2221 | static LIST_HEAD(ftrace_commands); |
2222 | static DEFINE_MUTEX(ftrace_cmd_mutex); | |
2223 | ||
2224 | int register_ftrace_command(struct ftrace_func_command *cmd) | |
2225 | { | |
2226 | struct ftrace_func_command *p; | |
2227 | int ret = 0; | |
2228 | ||
2229 | mutex_lock(&ftrace_cmd_mutex); | |
2230 | list_for_each_entry(p, &ftrace_commands, list) { | |
2231 | if (strcmp(cmd->name, p->name) == 0) { | |
2232 | ret = -EBUSY; | |
2233 | goto out_unlock; | |
2234 | } | |
2235 | } | |
2236 | list_add(&cmd->list, &ftrace_commands); | |
2237 | out_unlock: | |
2238 | mutex_unlock(&ftrace_cmd_mutex); | |
2239 | ||
2240 | return ret; | |
2241 | } | |
2242 | ||
2243 | int unregister_ftrace_command(struct ftrace_func_command *cmd) | |
2244 | { | |
2245 | struct ftrace_func_command *p, *n; | |
2246 | int ret = -ENODEV; | |
2247 | ||
2248 | mutex_lock(&ftrace_cmd_mutex); | |
2249 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { | |
2250 | if (strcmp(cmd->name, p->name) == 0) { | |
2251 | ret = 0; | |
2252 | list_del_init(&p->list); | |
2253 | goto out_unlock; | |
2254 | } | |
2255 | } | |
2256 | out_unlock: | |
2257 | mutex_unlock(&ftrace_cmd_mutex); | |
2258 | ||
2259 | return ret; | |
2260 | } | |
2261 | ||
64e7c440 SR |
2262 | static int ftrace_process_regex(char *buff, int len, int enable) |
2263 | { | |
f6180773 | 2264 | char *func, *command, *next = buff; |
f45948e8 | 2265 | struct ftrace_ops *ops = &global_ops; |
6a24a244 | 2266 | struct ftrace_func_command *p; |
1cf41dd7 | 2267 | struct ftrace_hash *hash; |
b448c4e3 | 2268 | int ret; |
64e7c440 | 2269 | |
1cf41dd7 | 2270 | if (enable) |
f45948e8 | 2271 | hash = ops->filter_hash; |
1cf41dd7 | 2272 | else |
f45948e8 | 2273 | hash = ops->notrace_hash; |
1cf41dd7 | 2274 | |
64e7c440 SR |
2275 | func = strsep(&next, ":"); |
2276 | ||
2277 | if (!next) { | |
1cf41dd7 | 2278 | ret = ftrace_match_records(hash, func, len); |
b448c4e3 SR |
2279 | if (!ret) |
2280 | ret = -EINVAL; | |
2281 | if (ret < 0) | |
2282 | return ret; | |
2283 | return 0; | |
64e7c440 SR |
2284 | } |
2285 | ||
f6180773 | 2286 | /* command found */ |
64e7c440 SR |
2287 | |
2288 | command = strsep(&next, ":"); | |
2289 | ||
f6180773 SR |
2290 | mutex_lock(&ftrace_cmd_mutex); |
2291 | list_for_each_entry(p, &ftrace_commands, list) { | |
2292 | if (strcmp(p->name, command) == 0) { | |
2293 | ret = p->func(func, command, next, enable); | |
2294 | goto out_unlock; | |
2295 | } | |
64e7c440 | 2296 | } |
f6180773 SR |
2297 | out_unlock: |
2298 | mutex_unlock(&ftrace_cmd_mutex); | |
64e7c440 | 2299 | |
f6180773 | 2300 | return ret; |
64e7c440 SR |
2301 | } |
2302 | ||
e309b41d | 2303 | static ssize_t |
41c52c0d SR |
2304 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
2305 | size_t cnt, loff_t *ppos, int enable) | |
5072c59f SR |
2306 | { |
2307 | struct ftrace_iterator *iter; | |
689fd8b6 | 2308 | struct trace_parser *parser; |
2309 | ssize_t ret, read; | |
5072c59f | 2310 | |
4ba7978e | 2311 | if (!cnt) |
5072c59f SR |
2312 | return 0; |
2313 | ||
41c52c0d | 2314 | mutex_lock(&ftrace_regex_lock); |
5072c59f | 2315 | |
45a4a237 SR |
2316 | ret = -ENODEV; |
2317 | if (unlikely(ftrace_disabled)) | |
2318 | goto out_unlock; | |
2319 | ||
5072c59f SR |
2320 | if (file->f_mode & FMODE_READ) { |
2321 | struct seq_file *m = file->private_data; | |
2322 | iter = m->private; | |
2323 | } else | |
2324 | iter = file->private_data; | |
2325 | ||
689fd8b6 | 2326 | parser = &iter->parser; |
2327 | read = trace_get_user(parser, ubuf, cnt, ppos); | |
5072c59f | 2328 | |
4ba7978e | 2329 | if (read >= 0 && trace_parser_loaded(parser) && |
689fd8b6 | 2330 | !trace_parser_cont(parser)) { |
2331 | ret = ftrace_process_regex(parser->buffer, | |
2332 | parser->idx, enable); | |
313254a9 | 2333 | trace_parser_clear(parser); |
5072c59f | 2334 | if (ret) |
ed146b25 | 2335 | goto out_unlock; |
eda1e328 | 2336 | } |
5072c59f | 2337 | |
5072c59f | 2338 | ret = read; |
ed146b25 | 2339 | out_unlock: |
689fd8b6 | 2340 | mutex_unlock(&ftrace_regex_lock); |
ed146b25 | 2341 | |
5072c59f SR |
2342 | return ret; |
2343 | } | |
2344 | ||
41c52c0d SR |
2345 | static ssize_t |
2346 | ftrace_filter_write(struct file *file, const char __user *ubuf, | |
2347 | size_t cnt, loff_t *ppos) | |
2348 | { | |
2349 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); | |
2350 | } | |
2351 | ||
2352 | static ssize_t | |
2353 | ftrace_notrace_write(struct file *file, const char __user *ubuf, | |
2354 | size_t cnt, loff_t *ppos) | |
2355 | { | |
2356 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); | |
2357 | } | |
2358 | ||
2359 | static void | |
f45948e8 SR |
2360 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
2361 | int reset, int enable) | |
41c52c0d | 2362 | { |
f45948e8 SR |
2363 | struct ftrace_hash *hash; |
2364 | ||
41c52c0d SR |
2365 | if (unlikely(ftrace_disabled)) |
2366 | return; | |
2367 | ||
f45948e8 SR |
2368 | if (enable) |
2369 | hash = ops->filter_hash; | |
2370 | else | |
2371 | hash = ops->notrace_hash; | |
2372 | ||
41c52c0d SR |
2373 | mutex_lock(&ftrace_regex_lock); |
2374 | if (reset) | |
1cf41dd7 | 2375 | ftrace_filter_reset(hash); |
41c52c0d | 2376 | if (buf) |
1cf41dd7 | 2377 | ftrace_match_records(hash, buf, len); |
41c52c0d SR |
2378 | mutex_unlock(&ftrace_regex_lock); |
2379 | } | |
2380 | ||
77a2b37d SR |
2381 | /** |
2382 | * ftrace_set_filter - set a function to filter on in ftrace | |
2383 | * @buf - the string that holds the function filter text. | |
2384 | * @len - the length of the string. | |
2385 | * @reset - non zero to reset all filters before applying this filter. | |
2386 | * | |
2387 | * Filters denote which functions should be enabled when tracing is enabled. | |
2388 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
2389 | */ | |
e309b41d | 2390 | void ftrace_set_filter(unsigned char *buf, int len, int reset) |
77a2b37d | 2391 | { |
f45948e8 | 2392 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
41c52c0d | 2393 | } |
4eebcc81 | 2394 | |
41c52c0d SR |
2395 | /** |
2396 | * ftrace_set_notrace - set a function to not trace in ftrace | |
2397 | * @buf - the string that holds the function notrace text. | |
2398 | * @len - the length of the string. | |
2399 | * @reset - non zero to reset all filters before applying this filter. | |
2400 | * | |
2401 | * Notrace Filters denote which functions should not be enabled when tracing | |
2402 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
2403 | * for tracing. | |
2404 | */ | |
2405 | void ftrace_set_notrace(unsigned char *buf, int len, int reset) | |
2406 | { | |
f45948e8 | 2407 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
77a2b37d SR |
2408 | } |
2409 | ||
2af15d6a SR |
2410 | /* |
2411 | * command line interface to allow users to set filters on boot up. | |
2412 | */ | |
2413 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE | |
2414 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; | |
2415 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; | |
2416 | ||
2417 | static int __init set_ftrace_notrace(char *str) | |
2418 | { | |
2419 | strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); | |
2420 | return 1; | |
2421 | } | |
2422 | __setup("ftrace_notrace=", set_ftrace_notrace); | |
2423 | ||
2424 | static int __init set_ftrace_filter(char *str) | |
2425 | { | |
2426 | strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); | |
2427 | return 1; | |
2428 | } | |
2429 | __setup("ftrace_filter=", set_ftrace_filter); | |
2430 | ||
369bc18f | 2431 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
f6060f46 | 2432 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
801c29fd SR |
2433 | static int ftrace_set_func(unsigned long *array, int *idx, char *buffer); |
2434 | ||
369bc18f SA |
2435 | static int __init set_graph_function(char *str) |
2436 | { | |
06f43d66 | 2437 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
369bc18f SA |
2438 | return 1; |
2439 | } | |
2440 | __setup("ftrace_graph_filter=", set_graph_function); | |
2441 | ||
2442 | static void __init set_ftrace_early_graph(char *buf) | |
2443 | { | |
2444 | int ret; | |
2445 | char *func; | |
2446 | ||
2447 | while (buf) { | |
2448 | func = strsep(&buf, ","); | |
2449 | /* we allow only one expression at a time */ | |
2450 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, | |
2451 | func); | |
2452 | if (ret) | |
2453 | printk(KERN_DEBUG "ftrace: function %s not " | |
2454 | "traceable\n", func); | |
2455 | } | |
2456 | } | |
2457 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2458 | ||
f45948e8 SR |
2459 | static void __init |
2460 | set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable) | |
2af15d6a SR |
2461 | { |
2462 | char *func; | |
2463 | ||
2464 | while (buf) { | |
2465 | func = strsep(&buf, ","); | |
f45948e8 | 2466 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
2af15d6a SR |
2467 | } |
2468 | } | |
2469 | ||
2470 | static void __init set_ftrace_early_filters(void) | |
2471 | { | |
2472 | if (ftrace_filter_buf[0]) | |
f45948e8 | 2473 | set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1); |
2af15d6a | 2474 | if (ftrace_notrace_buf[0]) |
f45948e8 | 2475 | set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0); |
369bc18f SA |
2476 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
2477 | if (ftrace_graph_buf[0]) | |
2478 | set_ftrace_early_graph(ftrace_graph_buf); | |
2479 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2af15d6a SR |
2480 | } |
2481 | ||
e309b41d | 2482 | static int |
1cf41dd7 | 2483 | ftrace_regex_release(struct inode *inode, struct file *file) |
5072c59f SR |
2484 | { |
2485 | struct seq_file *m = (struct seq_file *)file->private_data; | |
2486 | struct ftrace_iterator *iter; | |
689fd8b6 | 2487 | struct trace_parser *parser; |
5072c59f | 2488 | |
41c52c0d | 2489 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
2490 | if (file->f_mode & FMODE_READ) { |
2491 | iter = m->private; | |
2492 | ||
2493 | seq_release(inode, file); | |
2494 | } else | |
2495 | iter = file->private_data; | |
2496 | ||
689fd8b6 | 2497 | parser = &iter->parser; |
2498 | if (trace_parser_loaded(parser)) { | |
2499 | parser->buffer[parser->idx] = 0; | |
1cf41dd7 | 2500 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
5072c59f SR |
2501 | } |
2502 | ||
689fd8b6 | 2503 | trace_parser_put(parser); |
5072c59f | 2504 | kfree(iter); |
689fd8b6 | 2505 | |
058e297d SR |
2506 | if (file->f_mode & FMODE_WRITE) { |
2507 | mutex_lock(&ftrace_lock); | |
2508 | if (ftrace_start_up && ftrace_enabled) | |
2509 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); | |
2510 | mutex_unlock(&ftrace_lock); | |
2511 | } | |
2512 | ||
41c52c0d | 2513 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
2514 | return 0; |
2515 | } | |
2516 | ||
5e2336a0 | 2517 | static const struct file_operations ftrace_avail_fops = { |
5072c59f SR |
2518 | .open = ftrace_avail_open, |
2519 | .read = seq_read, | |
2520 | .llseek = seq_lseek, | |
3be04b47 | 2521 | .release = seq_release_private, |
5072c59f SR |
2522 | }; |
2523 | ||
5e2336a0 | 2524 | static const struct file_operations ftrace_filter_fops = { |
5072c59f | 2525 | .open = ftrace_filter_open, |
850a80cf | 2526 | .read = seq_read, |
5072c59f | 2527 | .write = ftrace_filter_write, |
98c4fd04 | 2528 | .llseek = ftrace_regex_lseek, |
1cf41dd7 | 2529 | .release = ftrace_regex_release, |
5072c59f SR |
2530 | }; |
2531 | ||
5e2336a0 | 2532 | static const struct file_operations ftrace_notrace_fops = { |
41c52c0d | 2533 | .open = ftrace_notrace_open, |
850a80cf | 2534 | .read = seq_read, |
41c52c0d SR |
2535 | .write = ftrace_notrace_write, |
2536 | .llseek = ftrace_regex_lseek, | |
1cf41dd7 | 2537 | .release = ftrace_regex_release, |
41c52c0d SR |
2538 | }; |
2539 | ||
ea4e2bc4 SR |
2540 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
2541 | ||
2542 | static DEFINE_MUTEX(graph_lock); | |
2543 | ||
2544 | int ftrace_graph_count; | |
c7c6b1fe | 2545 | int ftrace_graph_filter_enabled; |
ea4e2bc4 SR |
2546 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
2547 | ||
2548 | static void * | |
85951842 | 2549 | __g_next(struct seq_file *m, loff_t *pos) |
ea4e2bc4 | 2550 | { |
85951842 | 2551 | if (*pos >= ftrace_graph_count) |
ea4e2bc4 | 2552 | return NULL; |
a4ec5e0c | 2553 | return &ftrace_graph_funcs[*pos]; |
85951842 | 2554 | } |
ea4e2bc4 | 2555 | |
85951842 LZ |
2556 | static void * |
2557 | g_next(struct seq_file *m, void *v, loff_t *pos) | |
2558 | { | |
2559 | (*pos)++; | |
2560 | return __g_next(m, pos); | |
ea4e2bc4 SR |
2561 | } |
2562 | ||
2563 | static void *g_start(struct seq_file *m, loff_t *pos) | |
2564 | { | |
ea4e2bc4 SR |
2565 | mutex_lock(&graph_lock); |
2566 | ||
f9349a8f | 2567 | /* Nothing, tell g_show to print all functions are enabled */ |
c7c6b1fe | 2568 | if (!ftrace_graph_filter_enabled && !*pos) |
f9349a8f FW |
2569 | return (void *)1; |
2570 | ||
85951842 | 2571 | return __g_next(m, pos); |
ea4e2bc4 SR |
2572 | } |
2573 | ||
2574 | static void g_stop(struct seq_file *m, void *p) | |
2575 | { | |
2576 | mutex_unlock(&graph_lock); | |
2577 | } | |
2578 | ||
2579 | static int g_show(struct seq_file *m, void *v) | |
2580 | { | |
2581 | unsigned long *ptr = v; | |
ea4e2bc4 SR |
2582 | |
2583 | if (!ptr) | |
2584 | return 0; | |
2585 | ||
f9349a8f FW |
2586 | if (ptr == (unsigned long *)1) { |
2587 | seq_printf(m, "#### all functions enabled ####\n"); | |
2588 | return 0; | |
2589 | } | |
2590 | ||
b375a11a | 2591 | seq_printf(m, "%ps\n", (void *)*ptr); |
ea4e2bc4 SR |
2592 | |
2593 | return 0; | |
2594 | } | |
2595 | ||
88e9d34c | 2596 | static const struct seq_operations ftrace_graph_seq_ops = { |
ea4e2bc4 SR |
2597 | .start = g_start, |
2598 | .next = g_next, | |
2599 | .stop = g_stop, | |
2600 | .show = g_show, | |
2601 | }; | |
2602 | ||
2603 | static int | |
2604 | ftrace_graph_open(struct inode *inode, struct file *file) | |
2605 | { | |
2606 | int ret = 0; | |
2607 | ||
2608 | if (unlikely(ftrace_disabled)) | |
2609 | return -ENODEV; | |
2610 | ||
2611 | mutex_lock(&graph_lock); | |
2612 | if ((file->f_mode & FMODE_WRITE) && | |
8650ae32 | 2613 | (file->f_flags & O_TRUNC)) { |
c7c6b1fe | 2614 | ftrace_graph_filter_enabled = 0; |
ea4e2bc4 SR |
2615 | ftrace_graph_count = 0; |
2616 | memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs)); | |
2617 | } | |
a4ec5e0c | 2618 | mutex_unlock(&graph_lock); |
ea4e2bc4 | 2619 | |
a4ec5e0c | 2620 | if (file->f_mode & FMODE_READ) |
ea4e2bc4 | 2621 | ret = seq_open(file, &ftrace_graph_seq_ops); |
ea4e2bc4 SR |
2622 | |
2623 | return ret; | |
2624 | } | |
2625 | ||
87827111 LZ |
2626 | static int |
2627 | ftrace_graph_release(struct inode *inode, struct file *file) | |
2628 | { | |
2629 | if (file->f_mode & FMODE_READ) | |
2630 | seq_release(inode, file); | |
2631 | return 0; | |
2632 | } | |
2633 | ||
ea4e2bc4 | 2634 | static int |
f9349a8f | 2635 | ftrace_set_func(unsigned long *array, int *idx, char *buffer) |
ea4e2bc4 | 2636 | { |
ea4e2bc4 SR |
2637 | struct dyn_ftrace *rec; |
2638 | struct ftrace_page *pg; | |
f9349a8f | 2639 | int search_len; |
c7c6b1fe | 2640 | int fail = 1; |
f9349a8f FW |
2641 | int type, not; |
2642 | char *search; | |
2643 | bool exists; | |
2644 | int i; | |
ea4e2bc4 | 2645 | |
f9349a8f | 2646 | /* decode regex */ |
3f6fe06d | 2647 | type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); |
c7c6b1fe LZ |
2648 | if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS) |
2649 | return -EBUSY; | |
f9349a8f FW |
2650 | |
2651 | search_len = strlen(search); | |
2652 | ||
52baf119 | 2653 | mutex_lock(&ftrace_lock); |
45a4a237 SR |
2654 | |
2655 | if (unlikely(ftrace_disabled)) { | |
2656 | mutex_unlock(&ftrace_lock); | |
2657 | return -ENODEV; | |
2658 | } | |
2659 | ||
265c831c SR |
2660 | do_for_each_ftrace_rec(pg, rec) { |
2661 | ||
45a4a237 | 2662 | if (rec->flags & FTRACE_FL_FREE) |
265c831c SR |
2663 | continue; |
2664 | ||
b9df92d2 | 2665 | if (ftrace_match_record(rec, NULL, search, search_len, type)) { |
c7c6b1fe | 2666 | /* if it is in the array */ |
f9349a8f | 2667 | exists = false; |
c7c6b1fe | 2668 | for (i = 0; i < *idx; i++) { |
f9349a8f FW |
2669 | if (array[i] == rec->ip) { |
2670 | exists = true; | |
265c831c SR |
2671 | break; |
2672 | } | |
c7c6b1fe LZ |
2673 | } |
2674 | ||
2675 | if (!not) { | |
2676 | fail = 0; | |
2677 | if (!exists) { | |
2678 | array[(*idx)++] = rec->ip; | |
2679 | if (*idx >= FTRACE_GRAPH_MAX_FUNCS) | |
2680 | goto out; | |
2681 | } | |
2682 | } else { | |
2683 | if (exists) { | |
2684 | array[i] = array[--(*idx)]; | |
2685 | array[*idx] = 0; | |
2686 | fail = 0; | |
2687 | } | |
2688 | } | |
ea4e2bc4 | 2689 | } |
265c831c | 2690 | } while_for_each_ftrace_rec(); |
c7c6b1fe | 2691 | out: |
52baf119 | 2692 | mutex_unlock(&ftrace_lock); |
ea4e2bc4 | 2693 | |
c7c6b1fe LZ |
2694 | if (fail) |
2695 | return -EINVAL; | |
2696 | ||
2697 | ftrace_graph_filter_enabled = 1; | |
2698 | return 0; | |
ea4e2bc4 SR |
2699 | } |
2700 | ||
2701 | static ssize_t | |
2702 | ftrace_graph_write(struct file *file, const char __user *ubuf, | |
2703 | size_t cnt, loff_t *ppos) | |
2704 | { | |
689fd8b6 | 2705 | struct trace_parser parser; |
4ba7978e | 2706 | ssize_t read, ret; |
ea4e2bc4 | 2707 | |
c7c6b1fe | 2708 | if (!cnt) |
ea4e2bc4 SR |
2709 | return 0; |
2710 | ||
2711 | mutex_lock(&graph_lock); | |
2712 | ||
689fd8b6 | 2713 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) { |
2714 | ret = -ENOMEM; | |
1eb90f13 | 2715 | goto out_unlock; |
ea4e2bc4 SR |
2716 | } |
2717 | ||
689fd8b6 | 2718 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
ea4e2bc4 | 2719 | |
4ba7978e | 2720 | if (read >= 0 && trace_parser_loaded((&parser))) { |
689fd8b6 | 2721 | parser.buffer[parser.idx] = 0; |
2722 | ||
2723 | /* we allow only one expression at a time */ | |
a4ec5e0c | 2724 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, |
689fd8b6 | 2725 | parser.buffer); |
ea4e2bc4 | 2726 | if (ret) |
1eb90f13 | 2727 | goto out_free; |
ea4e2bc4 | 2728 | } |
ea4e2bc4 SR |
2729 | |
2730 | ret = read; | |
1eb90f13 LZ |
2731 | |
2732 | out_free: | |
689fd8b6 | 2733 | trace_parser_put(&parser); |
1eb90f13 | 2734 | out_unlock: |
ea4e2bc4 SR |
2735 | mutex_unlock(&graph_lock); |
2736 | ||
2737 | return ret; | |
2738 | } | |
2739 | ||
2740 | static const struct file_operations ftrace_graph_fops = { | |
87827111 LZ |
2741 | .open = ftrace_graph_open, |
2742 | .read = seq_read, | |
2743 | .write = ftrace_graph_write, | |
2744 | .release = ftrace_graph_release, | |
6038f373 | 2745 | .llseek = seq_lseek, |
ea4e2bc4 SR |
2746 | }; |
2747 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2748 | ||
df4fc315 | 2749 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
5072c59f | 2750 | { |
5072c59f | 2751 | |
5452af66 FW |
2752 | trace_create_file("available_filter_functions", 0444, |
2753 | d_tracer, NULL, &ftrace_avail_fops); | |
5072c59f | 2754 | |
5452af66 FW |
2755 | trace_create_file("set_ftrace_filter", 0644, d_tracer, |
2756 | NULL, &ftrace_filter_fops); | |
41c52c0d | 2757 | |
5452af66 | 2758 | trace_create_file("set_ftrace_notrace", 0644, d_tracer, |
41c52c0d | 2759 | NULL, &ftrace_notrace_fops); |
ad90c0e3 | 2760 | |
ea4e2bc4 | 2761 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
5452af66 | 2762 | trace_create_file("set_graph_function", 0444, d_tracer, |
ea4e2bc4 SR |
2763 | NULL, |
2764 | &ftrace_graph_fops); | |
ea4e2bc4 SR |
2765 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
2766 | ||
5072c59f SR |
2767 | return 0; |
2768 | } | |
2769 | ||
5cb084bb | 2770 | static int ftrace_process_locs(struct module *mod, |
31e88909 | 2771 | unsigned long *start, |
68bf21aa SR |
2772 | unsigned long *end) |
2773 | { | |
2774 | unsigned long *p; | |
2775 | unsigned long addr; | |
68bf21aa | 2776 | |
e6ea44e9 | 2777 | mutex_lock(&ftrace_lock); |
68bf21aa SR |
2778 | p = start; |
2779 | while (p < end) { | |
2780 | addr = ftrace_call_adjust(*p++); | |
20e5227e SR |
2781 | /* |
2782 | * Some architecture linkers will pad between | |
2783 | * the different mcount_loc sections of different | |
2784 | * object files to satisfy alignments. | |
2785 | * Skip any NULL pointers. | |
2786 | */ | |
2787 | if (!addr) | |
2788 | continue; | |
68bf21aa | 2789 | ftrace_record_ip(addr); |
68bf21aa SR |
2790 | } |
2791 | ||
31e88909 | 2792 | ftrace_update_code(mod); |
e6ea44e9 | 2793 | mutex_unlock(&ftrace_lock); |
68bf21aa SR |
2794 | |
2795 | return 0; | |
2796 | } | |
2797 | ||
93eb677d | 2798 | #ifdef CONFIG_MODULES |
e7247a15 | 2799 | void ftrace_release_mod(struct module *mod) |
93eb677d SR |
2800 | { |
2801 | struct dyn_ftrace *rec; | |
2802 | struct ftrace_page *pg; | |
93eb677d | 2803 | |
45a4a237 SR |
2804 | mutex_lock(&ftrace_lock); |
2805 | ||
e7247a15 | 2806 | if (ftrace_disabled) |
45a4a237 | 2807 | goto out_unlock; |
93eb677d | 2808 | |
93eb677d | 2809 | do_for_each_ftrace_rec(pg, rec) { |
e7247a15 | 2810 | if (within_module_core(rec->ip, mod)) { |
93eb677d SR |
2811 | /* |
2812 | * rec->ip is changed in ftrace_free_rec() | |
2813 | * It should not between s and e if record was freed. | |
2814 | */ | |
2815 | FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE); | |
2816 | ftrace_free_rec(rec); | |
2817 | } | |
2818 | } while_for_each_ftrace_rec(); | |
45a4a237 | 2819 | out_unlock: |
93eb677d SR |
2820 | mutex_unlock(&ftrace_lock); |
2821 | } | |
2822 | ||
2823 | static void ftrace_init_module(struct module *mod, | |
2824 | unsigned long *start, unsigned long *end) | |
90d595fe | 2825 | { |
00fd61ae | 2826 | if (ftrace_disabled || start == end) |
fed1939c | 2827 | return; |
5cb084bb | 2828 | ftrace_process_locs(mod, start, end); |
90d595fe SR |
2829 | } |
2830 | ||
93eb677d SR |
2831 | static int ftrace_module_notify(struct notifier_block *self, |
2832 | unsigned long val, void *data) | |
2833 | { | |
2834 | struct module *mod = data; | |
2835 | ||
2836 | switch (val) { | |
2837 | case MODULE_STATE_COMING: | |
2838 | ftrace_init_module(mod, mod->ftrace_callsites, | |
2839 | mod->ftrace_callsites + | |
2840 | mod->num_ftrace_callsites); | |
2841 | break; | |
2842 | case MODULE_STATE_GOING: | |
e7247a15 | 2843 | ftrace_release_mod(mod); |
93eb677d SR |
2844 | break; |
2845 | } | |
2846 | ||
2847 | return 0; | |
2848 | } | |
2849 | #else | |
2850 | static int ftrace_module_notify(struct notifier_block *self, | |
2851 | unsigned long val, void *data) | |
2852 | { | |
2853 | return 0; | |
2854 | } | |
2855 | #endif /* CONFIG_MODULES */ | |
2856 | ||
2857 | struct notifier_block ftrace_module_nb = { | |
2858 | .notifier_call = ftrace_module_notify, | |
2859 | .priority = 0, | |
2860 | }; | |
2861 | ||
68bf21aa SR |
2862 | extern unsigned long __start_mcount_loc[]; |
2863 | extern unsigned long __stop_mcount_loc[]; | |
2864 | ||
2865 | void __init ftrace_init(void) | |
2866 | { | |
2867 | unsigned long count, addr, flags; | |
2868 | int ret; | |
2869 | ||
2870 | /* Keep the ftrace pointer to the stub */ | |
2871 | addr = (unsigned long)ftrace_stub; | |
2872 | ||
2873 | local_irq_save(flags); | |
2874 | ftrace_dyn_arch_init(&addr); | |
2875 | local_irq_restore(flags); | |
2876 | ||
2877 | /* ftrace_dyn_arch_init places the return code in addr */ | |
2878 | if (addr) | |
2879 | goto failed; | |
2880 | ||
2881 | count = __stop_mcount_loc - __start_mcount_loc; | |
2882 | ||
2883 | ret = ftrace_dyn_table_alloc(count); | |
2884 | if (ret) | |
2885 | goto failed; | |
2886 | ||
2887 | last_ftrace_enabled = ftrace_enabled = 1; | |
2888 | ||
5cb084bb | 2889 | ret = ftrace_process_locs(NULL, |
31e88909 | 2890 | __start_mcount_loc, |
68bf21aa SR |
2891 | __stop_mcount_loc); |
2892 | ||
93eb677d | 2893 | ret = register_module_notifier(&ftrace_module_nb); |
24ed0c4b | 2894 | if (ret) |
93eb677d SR |
2895 | pr_warning("Failed to register trace ftrace module notifier\n"); |
2896 | ||
2af15d6a SR |
2897 | set_ftrace_early_filters(); |
2898 | ||
68bf21aa SR |
2899 | return; |
2900 | failed: | |
2901 | ftrace_disabled = 1; | |
2902 | } | |
68bf21aa | 2903 | |
3d083395 | 2904 | #else |
0b6e4d56 FW |
2905 | |
2906 | static int __init ftrace_nodyn_init(void) | |
2907 | { | |
2908 | ftrace_enabled = 1; | |
2909 | return 0; | |
2910 | } | |
2911 | device_initcall(ftrace_nodyn_init); | |
2912 | ||
df4fc315 SR |
2913 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
2914 | static inline void ftrace_startup_enable(int command) { } | |
5a45cfe1 SR |
2915 | /* Keep as macros so we do not need to define the commands */ |
2916 | # define ftrace_startup(command) do { } while (0) | |
2917 | # define ftrace_shutdown(command) do { } while (0) | |
c7aafc54 IM |
2918 | # define ftrace_startup_sysctl() do { } while (0) |
2919 | # define ftrace_shutdown_sysctl() do { } while (0) | |
3d083395 SR |
2920 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
2921 | ||
e32d8956 | 2922 | static void clear_ftrace_swapper(void) |
978f3a45 SR |
2923 | { |
2924 | struct task_struct *p; | |
e32d8956 | 2925 | int cpu; |
978f3a45 | 2926 | |
e32d8956 SR |
2927 | get_online_cpus(); |
2928 | for_each_online_cpu(cpu) { | |
2929 | p = idle_task(cpu); | |
978f3a45 | 2930 | clear_tsk_trace_trace(p); |
e32d8956 SR |
2931 | } |
2932 | put_online_cpus(); | |
2933 | } | |
978f3a45 | 2934 | |
e32d8956 SR |
2935 | static void set_ftrace_swapper(void) |
2936 | { | |
2937 | struct task_struct *p; | |
2938 | int cpu; | |
2939 | ||
2940 | get_online_cpus(); | |
2941 | for_each_online_cpu(cpu) { | |
2942 | p = idle_task(cpu); | |
2943 | set_tsk_trace_trace(p); | |
2944 | } | |
2945 | put_online_cpus(); | |
978f3a45 SR |
2946 | } |
2947 | ||
e32d8956 SR |
2948 | static void clear_ftrace_pid(struct pid *pid) |
2949 | { | |
2950 | struct task_struct *p; | |
2951 | ||
229c4ef8 | 2952 | rcu_read_lock(); |
e32d8956 SR |
2953 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
2954 | clear_tsk_trace_trace(p); | |
2955 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 ON |
2956 | rcu_read_unlock(); |
2957 | ||
e32d8956 SR |
2958 | put_pid(pid); |
2959 | } | |
2960 | ||
2961 | static void set_ftrace_pid(struct pid *pid) | |
978f3a45 SR |
2962 | { |
2963 | struct task_struct *p; | |
2964 | ||
229c4ef8 | 2965 | rcu_read_lock(); |
978f3a45 SR |
2966 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
2967 | set_tsk_trace_trace(p); | |
2968 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 | 2969 | rcu_read_unlock(); |
978f3a45 SR |
2970 | } |
2971 | ||
756d17ee | 2972 | static void clear_ftrace_pid_task(struct pid *pid) |
e32d8956 | 2973 | { |
756d17ee | 2974 | if (pid == ftrace_swapper_pid) |
e32d8956 SR |
2975 | clear_ftrace_swapper(); |
2976 | else | |
756d17ee | 2977 | clear_ftrace_pid(pid); |
e32d8956 SR |
2978 | } |
2979 | ||
2980 | static void set_ftrace_pid_task(struct pid *pid) | |
2981 | { | |
2982 | if (pid == ftrace_swapper_pid) | |
2983 | set_ftrace_swapper(); | |
2984 | else | |
2985 | set_ftrace_pid(pid); | |
2986 | } | |
2987 | ||
756d17ee | 2988 | static int ftrace_pid_add(int p) |
df4fc315 | 2989 | { |
978f3a45 | 2990 | struct pid *pid; |
756d17ee | 2991 | struct ftrace_pid *fpid; |
2992 | int ret = -EINVAL; | |
df4fc315 | 2993 | |
756d17ee | 2994 | mutex_lock(&ftrace_lock); |
df4fc315 | 2995 | |
756d17ee | 2996 | if (!p) |
2997 | pid = ftrace_swapper_pid; | |
2998 | else | |
2999 | pid = find_get_pid(p); | |
df4fc315 | 3000 | |
756d17ee | 3001 | if (!pid) |
3002 | goto out; | |
df4fc315 | 3003 | |
756d17ee | 3004 | ret = 0; |
df4fc315 | 3005 | |
756d17ee | 3006 | list_for_each_entry(fpid, &ftrace_pids, list) |
3007 | if (fpid->pid == pid) | |
3008 | goto out_put; | |
978f3a45 | 3009 | |
756d17ee | 3010 | ret = -ENOMEM; |
df4fc315 | 3011 | |
756d17ee | 3012 | fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); |
3013 | if (!fpid) | |
3014 | goto out_put; | |
df4fc315 | 3015 | |
756d17ee | 3016 | list_add(&fpid->list, &ftrace_pids); |
3017 | fpid->pid = pid; | |
0ef8cde5 | 3018 | |
756d17ee | 3019 | set_ftrace_pid_task(pid); |
978f3a45 | 3020 | |
756d17ee | 3021 | ftrace_update_pid_func(); |
3022 | ftrace_startup_enable(0); | |
3023 | ||
3024 | mutex_unlock(&ftrace_lock); | |
3025 | return 0; | |
3026 | ||
3027 | out_put: | |
3028 | if (pid != ftrace_swapper_pid) | |
3029 | put_pid(pid); | |
978f3a45 | 3030 | |
756d17ee | 3031 | out: |
3032 | mutex_unlock(&ftrace_lock); | |
3033 | return ret; | |
3034 | } | |
3035 | ||
3036 | static void ftrace_pid_reset(void) | |
3037 | { | |
3038 | struct ftrace_pid *fpid, *safe; | |
978f3a45 | 3039 | |
756d17ee | 3040 | mutex_lock(&ftrace_lock); |
3041 | list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { | |
3042 | struct pid *pid = fpid->pid; | |
3043 | ||
3044 | clear_ftrace_pid_task(pid); | |
3045 | ||
3046 | list_del(&fpid->list); | |
3047 | kfree(fpid); | |
df4fc315 SR |
3048 | } |
3049 | ||
df4fc315 SR |
3050 | ftrace_update_pid_func(); |
3051 | ftrace_startup_enable(0); | |
3052 | ||
e6ea44e9 | 3053 | mutex_unlock(&ftrace_lock); |
756d17ee | 3054 | } |
df4fc315 | 3055 | |
756d17ee | 3056 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
3057 | { | |
3058 | mutex_lock(&ftrace_lock); | |
3059 | ||
3060 | if (list_empty(&ftrace_pids) && (!*pos)) | |
3061 | return (void *) 1; | |
3062 | ||
3063 | return seq_list_start(&ftrace_pids, *pos); | |
3064 | } | |
3065 | ||
3066 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) | |
3067 | { | |
3068 | if (v == (void *)1) | |
3069 | return NULL; | |
3070 | ||
3071 | return seq_list_next(v, &ftrace_pids, pos); | |
3072 | } | |
3073 | ||
3074 | static void fpid_stop(struct seq_file *m, void *p) | |
3075 | { | |
3076 | mutex_unlock(&ftrace_lock); | |
3077 | } | |
3078 | ||
3079 | static int fpid_show(struct seq_file *m, void *v) | |
3080 | { | |
3081 | const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); | |
3082 | ||
3083 | if (v == (void *)1) { | |
3084 | seq_printf(m, "no pid\n"); | |
3085 | return 0; | |
3086 | } | |
3087 | ||
3088 | if (fpid->pid == ftrace_swapper_pid) | |
3089 | seq_printf(m, "swapper tasks\n"); | |
3090 | else | |
3091 | seq_printf(m, "%u\n", pid_vnr(fpid->pid)); | |
3092 | ||
3093 | return 0; | |
3094 | } | |
3095 | ||
3096 | static const struct seq_operations ftrace_pid_sops = { | |
3097 | .start = fpid_start, | |
3098 | .next = fpid_next, | |
3099 | .stop = fpid_stop, | |
3100 | .show = fpid_show, | |
3101 | }; | |
3102 | ||
3103 | static int | |
3104 | ftrace_pid_open(struct inode *inode, struct file *file) | |
3105 | { | |
3106 | int ret = 0; | |
3107 | ||
3108 | if ((file->f_mode & FMODE_WRITE) && | |
3109 | (file->f_flags & O_TRUNC)) | |
3110 | ftrace_pid_reset(); | |
3111 | ||
3112 | if (file->f_mode & FMODE_READ) | |
3113 | ret = seq_open(file, &ftrace_pid_sops); | |
3114 | ||
3115 | return ret; | |
3116 | } | |
3117 | ||
df4fc315 SR |
3118 | static ssize_t |
3119 | ftrace_pid_write(struct file *filp, const char __user *ubuf, | |
3120 | size_t cnt, loff_t *ppos) | |
3121 | { | |
457dc928 | 3122 | char buf[64], *tmp; |
df4fc315 SR |
3123 | long val; |
3124 | int ret; | |
3125 | ||
3126 | if (cnt >= sizeof(buf)) | |
3127 | return -EINVAL; | |
3128 | ||
3129 | if (copy_from_user(&buf, ubuf, cnt)) | |
3130 | return -EFAULT; | |
3131 | ||
3132 | buf[cnt] = 0; | |
3133 | ||
756d17ee | 3134 | /* |
3135 | * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" | |
3136 | * to clean the filter quietly. | |
3137 | */ | |
457dc928 IM |
3138 | tmp = strstrip(buf); |
3139 | if (strlen(tmp) == 0) | |
756d17ee | 3140 | return 1; |
3141 | ||
457dc928 | 3142 | ret = strict_strtol(tmp, 10, &val); |
df4fc315 SR |
3143 | if (ret < 0) |
3144 | return ret; | |
3145 | ||
756d17ee | 3146 | ret = ftrace_pid_add(val); |
df4fc315 | 3147 | |
756d17ee | 3148 | return ret ? ret : cnt; |
3149 | } | |
df4fc315 | 3150 | |
756d17ee | 3151 | static int |
3152 | ftrace_pid_release(struct inode *inode, struct file *file) | |
3153 | { | |
3154 | if (file->f_mode & FMODE_READ) | |
3155 | seq_release(inode, file); | |
df4fc315 | 3156 | |
756d17ee | 3157 | return 0; |
df4fc315 SR |
3158 | } |
3159 | ||
5e2336a0 | 3160 | static const struct file_operations ftrace_pid_fops = { |
756d17ee | 3161 | .open = ftrace_pid_open, |
3162 | .write = ftrace_pid_write, | |
3163 | .read = seq_read, | |
3164 | .llseek = seq_lseek, | |
3165 | .release = ftrace_pid_release, | |
df4fc315 SR |
3166 | }; |
3167 | ||
3168 | static __init int ftrace_init_debugfs(void) | |
3169 | { | |
3170 | struct dentry *d_tracer; | |
df4fc315 SR |
3171 | |
3172 | d_tracer = tracing_init_dentry(); | |
3173 | if (!d_tracer) | |
3174 | return 0; | |
3175 | ||
3176 | ftrace_init_dyn_debugfs(d_tracer); | |
3177 | ||
5452af66 FW |
3178 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
3179 | NULL, &ftrace_pid_fops); | |
493762fc SR |
3180 | |
3181 | ftrace_profile_debugfs(d_tracer); | |
3182 | ||
df4fc315 SR |
3183 | return 0; |
3184 | } | |
df4fc315 SR |
3185 | fs_initcall(ftrace_init_debugfs); |
3186 | ||
a2bb6a3d | 3187 | /** |
81adbdc0 | 3188 | * ftrace_kill - kill ftrace |
a2bb6a3d SR |
3189 | * |
3190 | * This function should be used by panic code. It stops ftrace | |
3191 | * but in a not so nice way. If you need to simply kill ftrace | |
3192 | * from a non-atomic section, use ftrace_kill. | |
3193 | */ | |
81adbdc0 | 3194 | void ftrace_kill(void) |
a2bb6a3d SR |
3195 | { |
3196 | ftrace_disabled = 1; | |
3197 | ftrace_enabled = 0; | |
a2bb6a3d SR |
3198 | clear_ftrace_function(); |
3199 | } | |
3200 | ||
16444a8a | 3201 | /** |
3d083395 SR |
3202 | * register_ftrace_function - register a function for profiling |
3203 | * @ops - ops structure that holds the function for profiling. | |
16444a8a | 3204 | * |
3d083395 SR |
3205 | * Register a function to be called by all functions in the |
3206 | * kernel. | |
3207 | * | |
3208 | * Note: @ops->func and all the functions it calls must be labeled | |
3209 | * with "notrace", otherwise it will go into a | |
3210 | * recursive loop. | |
16444a8a | 3211 | */ |
3d083395 | 3212 | int register_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 3213 | { |
45a4a237 | 3214 | int ret = -1; |
4eebcc81 | 3215 | |
e6ea44e9 | 3216 | mutex_lock(&ftrace_lock); |
e7d3737e | 3217 | |
45a4a237 SR |
3218 | if (unlikely(ftrace_disabled)) |
3219 | goto out_unlock; | |
3220 | ||
b0fc494f | 3221 | ret = __register_ftrace_function(ops); |
5a45cfe1 | 3222 | ftrace_startup(0); |
b0fc494f | 3223 | |
45a4a237 | 3224 | out_unlock: |
e6ea44e9 | 3225 | mutex_unlock(&ftrace_lock); |
b0fc494f | 3226 | return ret; |
3d083395 SR |
3227 | } |
3228 | ||
3229 | /** | |
32632920 | 3230 | * unregister_ftrace_function - unregister a function for profiling. |
3d083395 SR |
3231 | * @ops - ops structure that holds the function to unregister |
3232 | * | |
3233 | * Unregister a function that was added to be called by ftrace profiling. | |
3234 | */ | |
3235 | int unregister_ftrace_function(struct ftrace_ops *ops) | |
3236 | { | |
3237 | int ret; | |
3238 | ||
e6ea44e9 | 3239 | mutex_lock(&ftrace_lock); |
3d083395 | 3240 | ret = __unregister_ftrace_function(ops); |
5a45cfe1 | 3241 | ftrace_shutdown(0); |
e6ea44e9 | 3242 | mutex_unlock(&ftrace_lock); |
b0fc494f SR |
3243 | |
3244 | return ret; | |
3245 | } | |
3246 | ||
e309b41d | 3247 | int |
b0fc494f | 3248 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
8d65af78 | 3249 | void __user *buffer, size_t *lenp, |
b0fc494f SR |
3250 | loff_t *ppos) |
3251 | { | |
45a4a237 | 3252 | int ret = -ENODEV; |
4eebcc81 | 3253 | |
e6ea44e9 | 3254 | mutex_lock(&ftrace_lock); |
b0fc494f | 3255 | |
45a4a237 SR |
3256 | if (unlikely(ftrace_disabled)) |
3257 | goto out; | |
3258 | ||
3259 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
b0fc494f | 3260 | |
a32c7765 | 3261 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
b0fc494f SR |
3262 | goto out; |
3263 | ||
a32c7765 | 3264 | last_ftrace_enabled = !!ftrace_enabled; |
b0fc494f SR |
3265 | |
3266 | if (ftrace_enabled) { | |
3267 | ||
3268 | ftrace_startup_sysctl(); | |
3269 | ||
3270 | /* we are starting ftrace again */ | |
3271 | if (ftrace_list != &ftrace_list_end) { | |
3272 | if (ftrace_list->next == &ftrace_list_end) | |
3273 | ftrace_trace_function = ftrace_list->func; | |
3274 | else | |
3275 | ftrace_trace_function = ftrace_list_func; | |
3276 | } | |
3277 | ||
3278 | } else { | |
3279 | /* stopping ftrace calls (just send to ftrace_stub) */ | |
3280 | ftrace_trace_function = ftrace_stub; | |
3281 | ||
3282 | ftrace_shutdown_sysctl(); | |
3283 | } | |
3284 | ||
3285 | out: | |
e6ea44e9 | 3286 | mutex_unlock(&ftrace_lock); |
3d083395 | 3287 | return ret; |
16444a8a | 3288 | } |
f17845e5 | 3289 | |
fb52607a | 3290 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
e7d3737e | 3291 | |
597af815 | 3292 | static int ftrace_graph_active; |
4a2b8dda | 3293 | static struct notifier_block ftrace_suspend_notifier; |
e7d3737e | 3294 | |
e49dc19c SR |
3295 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
3296 | { | |
3297 | return 0; | |
3298 | } | |
3299 | ||
287b6e68 FW |
3300 | /* The callbacks that hook a function */ |
3301 | trace_func_graph_ret_t ftrace_graph_return = | |
3302 | (trace_func_graph_ret_t)ftrace_stub; | |
e49dc19c | 3303 | trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
f201ae23 FW |
3304 | |
3305 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ | |
3306 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) | |
3307 | { | |
3308 | int i; | |
3309 | int ret = 0; | |
3310 | unsigned long flags; | |
3311 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; | |
3312 | struct task_struct *g, *t; | |
3313 | ||
3314 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { | |
3315 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH | |
3316 | * sizeof(struct ftrace_ret_stack), | |
3317 | GFP_KERNEL); | |
3318 | if (!ret_stack_list[i]) { | |
3319 | start = 0; | |
3320 | end = i; | |
3321 | ret = -ENOMEM; | |
3322 | goto free; | |
3323 | } | |
3324 | } | |
3325 | ||
3326 | read_lock_irqsave(&tasklist_lock, flags); | |
3327 | do_each_thread(g, t) { | |
3328 | if (start == end) { | |
3329 | ret = -EAGAIN; | |
3330 | goto unlock; | |
3331 | } | |
3332 | ||
3333 | if (t->ret_stack == NULL) { | |
380c4b14 | 3334 | atomic_set(&t->tracing_graph_pause, 0); |
f201ae23 | 3335 | atomic_set(&t->trace_overrun, 0); |
26c01624 SR |
3336 | t->curr_ret_stack = -1; |
3337 | /* Make sure the tasks see the -1 first: */ | |
3338 | smp_wmb(); | |
3339 | t->ret_stack = ret_stack_list[start++]; | |
f201ae23 FW |
3340 | } |
3341 | } while_each_thread(g, t); | |
3342 | ||
3343 | unlock: | |
3344 | read_unlock_irqrestore(&tasklist_lock, flags); | |
3345 | free: | |
3346 | for (i = start; i < end; i++) | |
3347 | kfree(ret_stack_list[i]); | |
3348 | return ret; | |
3349 | } | |
3350 | ||
8aef2d28 | 3351 | static void |
38516ab5 SR |
3352 | ftrace_graph_probe_sched_switch(void *ignore, |
3353 | struct task_struct *prev, struct task_struct *next) | |
8aef2d28 SR |
3354 | { |
3355 | unsigned long long timestamp; | |
3356 | int index; | |
3357 | ||
be6f164a SR |
3358 | /* |
3359 | * Does the user want to count the time a function was asleep. | |
3360 | * If so, do not update the time stamps. | |
3361 | */ | |
3362 | if (trace_flags & TRACE_ITER_SLEEP_TIME) | |
3363 | return; | |
3364 | ||
8aef2d28 SR |
3365 | timestamp = trace_clock_local(); |
3366 | ||
3367 | prev->ftrace_timestamp = timestamp; | |
3368 | ||
3369 | /* only process tasks that we timestamped */ | |
3370 | if (!next->ftrace_timestamp) | |
3371 | return; | |
3372 | ||
3373 | /* | |
3374 | * Update all the counters in next to make up for the | |
3375 | * time next was sleeping. | |
3376 | */ | |
3377 | timestamp -= next->ftrace_timestamp; | |
3378 | ||
3379 | for (index = next->curr_ret_stack; index >= 0; index--) | |
3380 | next->ret_stack[index].calltime += timestamp; | |
3381 | } | |
3382 | ||
f201ae23 | 3383 | /* Allocate a return stack for each task */ |
fb52607a | 3384 | static int start_graph_tracing(void) |
f201ae23 FW |
3385 | { |
3386 | struct ftrace_ret_stack **ret_stack_list; | |
5b058bcd | 3387 | int ret, cpu; |
f201ae23 FW |
3388 | |
3389 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * | |
3390 | sizeof(struct ftrace_ret_stack *), | |
3391 | GFP_KERNEL); | |
3392 | ||
3393 | if (!ret_stack_list) | |
3394 | return -ENOMEM; | |
3395 | ||
5b058bcd | 3396 | /* The cpu_boot init_task->ret_stack will never be freed */ |
179c498a SR |
3397 | for_each_online_cpu(cpu) { |
3398 | if (!idle_task(cpu)->ret_stack) | |
868baf07 | 3399 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
179c498a | 3400 | } |
5b058bcd | 3401 | |
f201ae23 FW |
3402 | do { |
3403 | ret = alloc_retstack_tasklist(ret_stack_list); | |
3404 | } while (ret == -EAGAIN); | |
3405 | ||
8aef2d28 | 3406 | if (!ret) { |
38516ab5 | 3407 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
8aef2d28 SR |
3408 | if (ret) |
3409 | pr_info("ftrace_graph: Couldn't activate tracepoint" | |
3410 | " probe to kernel_sched_switch\n"); | |
3411 | } | |
3412 | ||
f201ae23 FW |
3413 | kfree(ret_stack_list); |
3414 | return ret; | |
3415 | } | |
3416 | ||
4a2b8dda FW |
3417 | /* |
3418 | * Hibernation protection. | |
3419 | * The state of the current task is too much unstable during | |
3420 | * suspend/restore to disk. We want to protect against that. | |
3421 | */ | |
3422 | static int | |
3423 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, | |
3424 | void *unused) | |
3425 | { | |
3426 | switch (state) { | |
3427 | case PM_HIBERNATION_PREPARE: | |
3428 | pause_graph_tracing(); | |
3429 | break; | |
3430 | ||
3431 | case PM_POST_HIBERNATION: | |
3432 | unpause_graph_tracing(); | |
3433 | break; | |
3434 | } | |
3435 | return NOTIFY_DONE; | |
3436 | } | |
3437 | ||
287b6e68 FW |
3438 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
3439 | trace_func_graph_ent_t entryfunc) | |
15e6cb36 | 3440 | { |
e7d3737e FW |
3441 | int ret = 0; |
3442 | ||
e6ea44e9 | 3443 | mutex_lock(&ftrace_lock); |
e7d3737e | 3444 | |
05ce5818 | 3445 | /* we currently allow only one tracer registered at a time */ |
597af815 | 3446 | if (ftrace_graph_active) { |
05ce5818 SR |
3447 | ret = -EBUSY; |
3448 | goto out; | |
3449 | } | |
3450 | ||
4a2b8dda FW |
3451 | ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call; |
3452 | register_pm_notifier(&ftrace_suspend_notifier); | |
3453 | ||
597af815 | 3454 | ftrace_graph_active++; |
fb52607a | 3455 | ret = start_graph_tracing(); |
f201ae23 | 3456 | if (ret) { |
597af815 | 3457 | ftrace_graph_active--; |
f201ae23 FW |
3458 | goto out; |
3459 | } | |
e53a6319 | 3460 | |
287b6e68 FW |
3461 | ftrace_graph_return = retfunc; |
3462 | ftrace_graph_entry = entryfunc; | |
e53a6319 | 3463 | |
5a45cfe1 | 3464 | ftrace_startup(FTRACE_START_FUNC_RET); |
e7d3737e FW |
3465 | |
3466 | out: | |
e6ea44e9 | 3467 | mutex_unlock(&ftrace_lock); |
e7d3737e | 3468 | return ret; |
15e6cb36 FW |
3469 | } |
3470 | ||
fb52607a | 3471 | void unregister_ftrace_graph(void) |
15e6cb36 | 3472 | { |
e6ea44e9 | 3473 | mutex_lock(&ftrace_lock); |
e7d3737e | 3474 | |
597af815 | 3475 | if (unlikely(!ftrace_graph_active)) |
2aad1b76 SR |
3476 | goto out; |
3477 | ||
597af815 | 3478 | ftrace_graph_active--; |
287b6e68 | 3479 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
e49dc19c | 3480 | ftrace_graph_entry = ftrace_graph_entry_stub; |
5a45cfe1 | 3481 | ftrace_shutdown(FTRACE_STOP_FUNC_RET); |
4a2b8dda | 3482 | unregister_pm_notifier(&ftrace_suspend_notifier); |
38516ab5 | 3483 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
e7d3737e | 3484 | |
2aad1b76 | 3485 | out: |
e6ea44e9 | 3486 | mutex_unlock(&ftrace_lock); |
15e6cb36 | 3487 | } |
f201ae23 | 3488 | |
868baf07 SR |
3489 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
3490 | ||
3491 | static void | |
3492 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) | |
3493 | { | |
3494 | atomic_set(&t->tracing_graph_pause, 0); | |
3495 | atomic_set(&t->trace_overrun, 0); | |
3496 | t->ftrace_timestamp = 0; | |
25985edc | 3497 | /* make curr_ret_stack visible before we add the ret_stack */ |
868baf07 SR |
3498 | smp_wmb(); |
3499 | t->ret_stack = ret_stack; | |
3500 | } | |
3501 | ||
3502 | /* | |
3503 | * Allocate a return stack for the idle task. May be the first | |
3504 | * time through, or it may be done by CPU hotplug online. | |
3505 | */ | |
3506 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) | |
3507 | { | |
3508 | t->curr_ret_stack = -1; | |
3509 | /* | |
3510 | * The idle task has no parent, it either has its own | |
3511 | * stack or no stack at all. | |
3512 | */ | |
3513 | if (t->ret_stack) | |
3514 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); | |
3515 | ||
3516 | if (ftrace_graph_active) { | |
3517 | struct ftrace_ret_stack *ret_stack; | |
3518 | ||
3519 | ret_stack = per_cpu(idle_ret_stack, cpu); | |
3520 | if (!ret_stack) { | |
3521 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH | |
3522 | * sizeof(struct ftrace_ret_stack), | |
3523 | GFP_KERNEL); | |
3524 | if (!ret_stack) | |
3525 | return; | |
3526 | per_cpu(idle_ret_stack, cpu) = ret_stack; | |
3527 | } | |
3528 | graph_init_task(t, ret_stack); | |
3529 | } | |
3530 | } | |
3531 | ||
f201ae23 | 3532 | /* Allocate a return stack for newly created task */ |
fb52607a | 3533 | void ftrace_graph_init_task(struct task_struct *t) |
f201ae23 | 3534 | { |
84047e36 SR |
3535 | /* Make sure we do not use the parent ret_stack */ |
3536 | t->ret_stack = NULL; | |
ea14eb71 | 3537 | t->curr_ret_stack = -1; |
84047e36 | 3538 | |
597af815 | 3539 | if (ftrace_graph_active) { |
82310a32 SR |
3540 | struct ftrace_ret_stack *ret_stack; |
3541 | ||
3542 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH | |
f201ae23 FW |
3543 | * sizeof(struct ftrace_ret_stack), |
3544 | GFP_KERNEL); | |
82310a32 | 3545 | if (!ret_stack) |
f201ae23 | 3546 | return; |
868baf07 | 3547 | graph_init_task(t, ret_stack); |
84047e36 | 3548 | } |
f201ae23 FW |
3549 | } |
3550 | ||
fb52607a | 3551 | void ftrace_graph_exit_task(struct task_struct *t) |
f201ae23 | 3552 | { |
eae849ca FW |
3553 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
3554 | ||
f201ae23 | 3555 | t->ret_stack = NULL; |
eae849ca FW |
3556 | /* NULL must become visible to IRQs before we free it: */ |
3557 | barrier(); | |
3558 | ||
3559 | kfree(ret_stack); | |
f201ae23 | 3560 | } |
14a866c5 SR |
3561 | |
3562 | void ftrace_graph_stop(void) | |
3563 | { | |
3564 | ftrace_stop(); | |
3565 | } | |
15e6cb36 | 3566 | #endif |