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