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 | |
2410 | ftrace_mod_callback(char *func, char *cmd, char *param, int enable) | |
2411 | { | |
f45948e8 | 2412 | struct ftrace_ops *ops = &global_ops; |
1cf41dd7 | 2413 | struct ftrace_hash *hash; |
f6180773 | 2414 | char *mod; |
b448c4e3 | 2415 | int ret = -EINVAL; |
f6180773 SR |
2416 | |
2417 | /* | |
2418 | * cmd == 'mod' because we only registered this func | |
2419 | * for the 'mod' ftrace_func_command. | |
2420 | * But if you register one func with multiple commands, | |
2421 | * you can tell which command was used by the cmd | |
2422 | * parameter. | |
2423 | */ | |
2424 | ||
2425 | /* we must have a module name */ | |
2426 | if (!param) | |
b448c4e3 | 2427 | return ret; |
f6180773 SR |
2428 | |
2429 | mod = strsep(¶m, ":"); | |
2430 | if (!strlen(mod)) | |
b448c4e3 | 2431 | return ret; |
f6180773 | 2432 | |
1cf41dd7 | 2433 | if (enable) |
f45948e8 | 2434 | hash = ops->filter_hash; |
1cf41dd7 | 2435 | else |
f45948e8 | 2436 | hash = ops->notrace_hash; |
1cf41dd7 SR |
2437 | |
2438 | ret = ftrace_match_module_records(hash, func, mod); | |
b448c4e3 SR |
2439 | if (!ret) |
2440 | ret = -EINVAL; | |
2441 | if (ret < 0) | |
2442 | return ret; | |
2443 | ||
2444 | return 0; | |
f6180773 SR |
2445 | } |
2446 | ||
2447 | static struct ftrace_func_command ftrace_mod_cmd = { | |
2448 | .name = "mod", | |
2449 | .func = ftrace_mod_callback, | |
2450 | }; | |
2451 | ||
2452 | static int __init ftrace_mod_cmd_init(void) | |
2453 | { | |
2454 | return register_ftrace_command(&ftrace_mod_cmd); | |
2455 | } | |
2456 | device_initcall(ftrace_mod_cmd_init); | |
2457 | ||
59df055f | 2458 | static void |
b6887d79 | 2459 | function_trace_probe_call(unsigned long ip, unsigned long parent_ip) |
59df055f | 2460 | { |
b6887d79 | 2461 | struct ftrace_func_probe *entry; |
59df055f SR |
2462 | struct hlist_head *hhd; |
2463 | struct hlist_node *n; | |
2464 | unsigned long key; | |
59df055f SR |
2465 | |
2466 | key = hash_long(ip, FTRACE_HASH_BITS); | |
2467 | ||
2468 | hhd = &ftrace_func_hash[key]; | |
2469 | ||
2470 | if (hlist_empty(hhd)) | |
2471 | return; | |
2472 | ||
2473 | /* | |
2474 | * Disable preemption for these calls to prevent a RCU grace | |
2475 | * period. This syncs the hash iteration and freeing of items | |
2476 | * on the hash. rcu_read_lock is too dangerous here. | |
2477 | */ | |
5168ae50 | 2478 | preempt_disable_notrace(); |
59df055f SR |
2479 | hlist_for_each_entry_rcu(entry, n, hhd, node) { |
2480 | if (entry->ip == ip) | |
2481 | entry->ops->func(ip, parent_ip, &entry->data); | |
2482 | } | |
5168ae50 | 2483 | preempt_enable_notrace(); |
59df055f SR |
2484 | } |
2485 | ||
b6887d79 | 2486 | static struct ftrace_ops trace_probe_ops __read_mostly = |
59df055f | 2487 | { |
fb9fb015 | 2488 | .func = function_trace_probe_call, |
59df055f SR |
2489 | }; |
2490 | ||
b6887d79 | 2491 | static int ftrace_probe_registered; |
59df055f | 2492 | |
b6887d79 | 2493 | static void __enable_ftrace_function_probe(void) |
59df055f | 2494 | { |
b848914c | 2495 | int ret; |
59df055f SR |
2496 | int i; |
2497 | ||
b6887d79 | 2498 | if (ftrace_probe_registered) |
59df055f SR |
2499 | return; |
2500 | ||
2501 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
2502 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
2503 | if (hhd->first) | |
2504 | break; | |
2505 | } | |
2506 | /* Nothing registered? */ | |
2507 | if (i == FTRACE_FUNC_HASHSIZE) | |
2508 | return; | |
2509 | ||
b848914c SR |
2510 | ret = __register_ftrace_function(&trace_probe_ops); |
2511 | if (!ret) | |
a1cd6173 | 2512 | ret = ftrace_startup(&trace_probe_ops, 0); |
b848914c | 2513 | |
b6887d79 | 2514 | ftrace_probe_registered = 1; |
59df055f SR |
2515 | } |
2516 | ||
b6887d79 | 2517 | static void __disable_ftrace_function_probe(void) |
59df055f | 2518 | { |
b848914c | 2519 | int ret; |
59df055f SR |
2520 | int i; |
2521 | ||
b6887d79 | 2522 | if (!ftrace_probe_registered) |
59df055f SR |
2523 | return; |
2524 | ||
2525 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
2526 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
2527 | if (hhd->first) | |
2528 | return; | |
2529 | } | |
2530 | ||
2531 | /* no more funcs left */ | |
b848914c SR |
2532 | ret = __unregister_ftrace_function(&trace_probe_ops); |
2533 | if (!ret) | |
2534 | ftrace_shutdown(&trace_probe_ops, 0); | |
2535 | ||
b6887d79 | 2536 | ftrace_probe_registered = 0; |
59df055f SR |
2537 | } |
2538 | ||
2539 | ||
2540 | static void ftrace_free_entry_rcu(struct rcu_head *rhp) | |
2541 | { | |
b6887d79 SR |
2542 | struct ftrace_func_probe *entry = |
2543 | container_of(rhp, struct ftrace_func_probe, rcu); | |
59df055f SR |
2544 | |
2545 | if (entry->ops->free) | |
2546 | entry->ops->free(&entry->data); | |
2547 | kfree(entry); | |
2548 | } | |
2549 | ||
2550 | ||
2551 | int | |
b6887d79 | 2552 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2553 | void *data) |
2554 | { | |
b6887d79 | 2555 | struct ftrace_func_probe *entry; |
59df055f SR |
2556 | struct ftrace_page *pg; |
2557 | struct dyn_ftrace *rec; | |
59df055f | 2558 | int type, len, not; |
6a24a244 | 2559 | unsigned long key; |
59df055f SR |
2560 | int count = 0; |
2561 | char *search; | |
2562 | ||
3f6fe06d | 2563 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
2564 | len = strlen(search); |
2565 | ||
b6887d79 | 2566 | /* we do not support '!' for function probes */ |
59df055f SR |
2567 | if (WARN_ON(not)) |
2568 | return -EINVAL; | |
2569 | ||
2570 | mutex_lock(&ftrace_lock); | |
59df055f | 2571 | |
45a4a237 SR |
2572 | if (unlikely(ftrace_disabled)) |
2573 | goto out_unlock; | |
59df055f | 2574 | |
45a4a237 | 2575 | do_for_each_ftrace_rec(pg, rec) { |
59df055f | 2576 | |
b9df92d2 | 2577 | if (!ftrace_match_record(rec, NULL, search, len, type)) |
59df055f SR |
2578 | continue; |
2579 | ||
2580 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
2581 | if (!entry) { | |
b6887d79 | 2582 | /* If we did not process any, then return error */ |
59df055f SR |
2583 | if (!count) |
2584 | count = -ENOMEM; | |
2585 | goto out_unlock; | |
2586 | } | |
2587 | ||
2588 | count++; | |
2589 | ||
2590 | entry->data = data; | |
2591 | ||
2592 | /* | |
2593 | * The caller might want to do something special | |
2594 | * for each function we find. We call the callback | |
2595 | * to give the caller an opportunity to do so. | |
2596 | */ | |
2597 | if (ops->callback) { | |
2598 | if (ops->callback(rec->ip, &entry->data) < 0) { | |
2599 | /* caller does not like this func */ | |
2600 | kfree(entry); | |
2601 | continue; | |
2602 | } | |
2603 | } | |
2604 | ||
2605 | entry->ops = ops; | |
2606 | entry->ip = rec->ip; | |
2607 | ||
2608 | key = hash_long(entry->ip, FTRACE_HASH_BITS); | |
2609 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); | |
2610 | ||
2611 | } while_for_each_ftrace_rec(); | |
b6887d79 | 2612 | __enable_ftrace_function_probe(); |
59df055f SR |
2613 | |
2614 | out_unlock: | |
2615 | mutex_unlock(&ftrace_lock); | |
2616 | ||
2617 | return count; | |
2618 | } | |
2619 | ||
2620 | enum { | |
b6887d79 SR |
2621 | PROBE_TEST_FUNC = 1, |
2622 | PROBE_TEST_DATA = 2 | |
59df055f SR |
2623 | }; |
2624 | ||
2625 | static void | |
b6887d79 | 2626 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2627 | void *data, int flags) |
2628 | { | |
b6887d79 | 2629 | struct ftrace_func_probe *entry; |
59df055f SR |
2630 | struct hlist_node *n, *tmp; |
2631 | char str[KSYM_SYMBOL_LEN]; | |
2632 | int type = MATCH_FULL; | |
2633 | int i, len = 0; | |
2634 | char *search; | |
2635 | ||
b36461da | 2636 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
59df055f | 2637 | glob = NULL; |
b36461da | 2638 | else if (glob) { |
59df055f SR |
2639 | int not; |
2640 | ||
3f6fe06d | 2641 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
2642 | len = strlen(search); |
2643 | ||
b6887d79 | 2644 | /* we do not support '!' for function probes */ |
59df055f SR |
2645 | if (WARN_ON(not)) |
2646 | return; | |
2647 | } | |
2648 | ||
2649 | mutex_lock(&ftrace_lock); | |
2650 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
2651 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
2652 | ||
2653 | hlist_for_each_entry_safe(entry, n, tmp, hhd, node) { | |
2654 | ||
2655 | /* break up if statements for readability */ | |
b6887d79 | 2656 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
59df055f SR |
2657 | continue; |
2658 | ||
b6887d79 | 2659 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
59df055f SR |
2660 | continue; |
2661 | ||
2662 | /* do this last, since it is the most expensive */ | |
2663 | if (glob) { | |
2664 | kallsyms_lookup(entry->ip, NULL, NULL, | |
2665 | NULL, str); | |
2666 | if (!ftrace_match(str, glob, len, type)) | |
2667 | continue; | |
2668 | } | |
2669 | ||
2670 | hlist_del(&entry->node); | |
2671 | call_rcu(&entry->rcu, ftrace_free_entry_rcu); | |
2672 | } | |
2673 | } | |
b6887d79 | 2674 | __disable_ftrace_function_probe(); |
59df055f SR |
2675 | mutex_unlock(&ftrace_lock); |
2676 | } | |
2677 | ||
2678 | void | |
b6887d79 | 2679 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2680 | void *data) |
2681 | { | |
b6887d79 SR |
2682 | __unregister_ftrace_function_probe(glob, ops, data, |
2683 | PROBE_TEST_FUNC | PROBE_TEST_DATA); | |
59df055f SR |
2684 | } |
2685 | ||
2686 | void | |
b6887d79 | 2687 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
59df055f | 2688 | { |
b6887d79 | 2689 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
59df055f SR |
2690 | } |
2691 | ||
b6887d79 | 2692 | void unregister_ftrace_function_probe_all(char *glob) |
59df055f | 2693 | { |
b6887d79 | 2694 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
59df055f SR |
2695 | } |
2696 | ||
f6180773 SR |
2697 | static LIST_HEAD(ftrace_commands); |
2698 | static DEFINE_MUTEX(ftrace_cmd_mutex); | |
2699 | ||
2700 | int register_ftrace_command(struct ftrace_func_command *cmd) | |
2701 | { | |
2702 | struct ftrace_func_command *p; | |
2703 | int ret = 0; | |
2704 | ||
2705 | mutex_lock(&ftrace_cmd_mutex); | |
2706 | list_for_each_entry(p, &ftrace_commands, list) { | |
2707 | if (strcmp(cmd->name, p->name) == 0) { | |
2708 | ret = -EBUSY; | |
2709 | goto out_unlock; | |
2710 | } | |
2711 | } | |
2712 | list_add(&cmd->list, &ftrace_commands); | |
2713 | out_unlock: | |
2714 | mutex_unlock(&ftrace_cmd_mutex); | |
2715 | ||
2716 | return ret; | |
2717 | } | |
2718 | ||
2719 | int unregister_ftrace_command(struct ftrace_func_command *cmd) | |
2720 | { | |
2721 | struct ftrace_func_command *p, *n; | |
2722 | int ret = -ENODEV; | |
2723 | ||
2724 | mutex_lock(&ftrace_cmd_mutex); | |
2725 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { | |
2726 | if (strcmp(cmd->name, p->name) == 0) { | |
2727 | ret = 0; | |
2728 | list_del_init(&p->list); | |
2729 | goto out_unlock; | |
2730 | } | |
2731 | } | |
2732 | out_unlock: | |
2733 | mutex_unlock(&ftrace_cmd_mutex); | |
2734 | ||
2735 | return ret; | |
2736 | } | |
2737 | ||
33dc9b12 SR |
2738 | static int ftrace_process_regex(struct ftrace_hash *hash, |
2739 | char *buff, int len, int enable) | |
64e7c440 | 2740 | { |
f6180773 | 2741 | char *func, *command, *next = buff; |
6a24a244 | 2742 | struct ftrace_func_command *p; |
b448c4e3 | 2743 | int ret; |
64e7c440 SR |
2744 | |
2745 | func = strsep(&next, ":"); | |
2746 | ||
2747 | if (!next) { | |
1cf41dd7 | 2748 | ret = ftrace_match_records(hash, func, len); |
b448c4e3 SR |
2749 | if (!ret) |
2750 | ret = -EINVAL; | |
2751 | if (ret < 0) | |
2752 | return ret; | |
2753 | return 0; | |
64e7c440 SR |
2754 | } |
2755 | ||
f6180773 | 2756 | /* command found */ |
64e7c440 SR |
2757 | |
2758 | command = strsep(&next, ":"); | |
2759 | ||
f6180773 SR |
2760 | mutex_lock(&ftrace_cmd_mutex); |
2761 | list_for_each_entry(p, &ftrace_commands, list) { | |
2762 | if (strcmp(p->name, command) == 0) { | |
2763 | ret = p->func(func, command, next, enable); | |
2764 | goto out_unlock; | |
2765 | } | |
64e7c440 | 2766 | } |
f6180773 SR |
2767 | out_unlock: |
2768 | mutex_unlock(&ftrace_cmd_mutex); | |
64e7c440 | 2769 | |
f6180773 | 2770 | return ret; |
64e7c440 SR |
2771 | } |
2772 | ||
e309b41d | 2773 | static ssize_t |
41c52c0d SR |
2774 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
2775 | size_t cnt, loff_t *ppos, int enable) | |
5072c59f SR |
2776 | { |
2777 | struct ftrace_iterator *iter; | |
689fd8b6 | 2778 | struct trace_parser *parser; |
2779 | ssize_t ret, read; | |
5072c59f | 2780 | |
4ba7978e | 2781 | if (!cnt) |
5072c59f SR |
2782 | return 0; |
2783 | ||
41c52c0d | 2784 | mutex_lock(&ftrace_regex_lock); |
5072c59f | 2785 | |
45a4a237 SR |
2786 | ret = -ENODEV; |
2787 | if (unlikely(ftrace_disabled)) | |
2788 | goto out_unlock; | |
2789 | ||
5072c59f SR |
2790 | if (file->f_mode & FMODE_READ) { |
2791 | struct seq_file *m = file->private_data; | |
2792 | iter = m->private; | |
2793 | } else | |
2794 | iter = file->private_data; | |
2795 | ||
689fd8b6 | 2796 | parser = &iter->parser; |
2797 | read = trace_get_user(parser, ubuf, cnt, ppos); | |
5072c59f | 2798 | |
4ba7978e | 2799 | if (read >= 0 && trace_parser_loaded(parser) && |
689fd8b6 | 2800 | !trace_parser_cont(parser)) { |
33dc9b12 | 2801 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
689fd8b6 | 2802 | parser->idx, enable); |
313254a9 | 2803 | trace_parser_clear(parser); |
5072c59f | 2804 | if (ret) |
ed146b25 | 2805 | goto out_unlock; |
eda1e328 | 2806 | } |
5072c59f | 2807 | |
5072c59f | 2808 | ret = read; |
ed146b25 | 2809 | out_unlock: |
689fd8b6 | 2810 | mutex_unlock(&ftrace_regex_lock); |
ed146b25 | 2811 | |
5072c59f SR |
2812 | return ret; |
2813 | } | |
2814 | ||
41c52c0d SR |
2815 | static ssize_t |
2816 | ftrace_filter_write(struct file *file, const char __user *ubuf, | |
2817 | size_t cnt, loff_t *ppos) | |
2818 | { | |
2819 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); | |
2820 | } | |
2821 | ||
2822 | static ssize_t | |
2823 | ftrace_notrace_write(struct file *file, const char __user *ubuf, | |
2824 | size_t cnt, loff_t *ppos) | |
2825 | { | |
2826 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); | |
2827 | } | |
2828 | ||
33dc9b12 | 2829 | static int |
f45948e8 SR |
2830 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, |
2831 | int reset, int enable) | |
41c52c0d | 2832 | { |
33dc9b12 | 2833 | struct ftrace_hash **orig_hash; |
f45948e8 | 2834 | struct ftrace_hash *hash; |
33dc9b12 | 2835 | int ret; |
f45948e8 | 2836 | |
936e074b SR |
2837 | /* All global ops uses the global ops filters */ |
2838 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) | |
2839 | ops = &global_ops; | |
2840 | ||
41c52c0d | 2841 | if (unlikely(ftrace_disabled)) |
33dc9b12 | 2842 | return -ENODEV; |
41c52c0d | 2843 | |
f45948e8 | 2844 | if (enable) |
33dc9b12 | 2845 | orig_hash = &ops->filter_hash; |
f45948e8 | 2846 | else |
33dc9b12 SR |
2847 | orig_hash = &ops->notrace_hash; |
2848 | ||
2849 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); | |
2850 | if (!hash) | |
2851 | return -ENOMEM; | |
f45948e8 | 2852 | |
41c52c0d SR |
2853 | mutex_lock(&ftrace_regex_lock); |
2854 | if (reset) | |
1cf41dd7 | 2855 | ftrace_filter_reset(hash); |
41c52c0d | 2856 | if (buf) |
1cf41dd7 | 2857 | ftrace_match_records(hash, buf, len); |
33dc9b12 SR |
2858 | |
2859 | mutex_lock(&ftrace_lock); | |
2860 | ret = ftrace_hash_move(orig_hash, hash); | |
2861 | mutex_unlock(&ftrace_lock); | |
2862 | ||
41c52c0d | 2863 | mutex_unlock(&ftrace_regex_lock); |
33dc9b12 SR |
2864 | |
2865 | free_ftrace_hash(hash); | |
2866 | return ret; | |
41c52c0d SR |
2867 | } |
2868 | ||
77a2b37d SR |
2869 | /** |
2870 | * ftrace_set_filter - set a function to filter on in ftrace | |
936e074b SR |
2871 | * @ops - the ops to set the filter with |
2872 | * @buf - the string that holds the function filter text. | |
2873 | * @len - the length of the string. | |
2874 | * @reset - non zero to reset all filters before applying this filter. | |
2875 | * | |
2876 | * Filters denote which functions should be enabled when tracing is enabled. | |
2877 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
2878 | */ | |
2879 | void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, | |
2880 | int len, int reset) | |
2881 | { | |
2882 | ftrace_set_regex(ops, buf, len, reset, 1); | |
2883 | } | |
2884 | EXPORT_SYMBOL_GPL(ftrace_set_filter); | |
2885 | ||
2886 | /** | |
2887 | * ftrace_set_notrace - set a function to not trace in ftrace | |
2888 | * @ops - the ops to set the notrace filter with | |
2889 | * @buf - the string that holds the function notrace text. | |
2890 | * @len - the length of the string. | |
2891 | * @reset - non zero to reset all filters before applying this filter. | |
2892 | * | |
2893 | * Notrace Filters denote which functions should not be enabled when tracing | |
2894 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
2895 | * for tracing. | |
2896 | */ | |
2897 | void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, | |
2898 | int len, int reset) | |
2899 | { | |
2900 | ftrace_set_regex(ops, buf, len, reset, 0); | |
2901 | } | |
2902 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); | |
2903 | /** | |
2904 | * ftrace_set_filter - set a function to filter on in ftrace | |
2905 | * @ops - the ops to set the filter with | |
77a2b37d SR |
2906 | * @buf - the string that holds the function filter text. |
2907 | * @len - the length of the string. | |
2908 | * @reset - non zero to reset all filters before applying this filter. | |
2909 | * | |
2910 | * Filters denote which functions should be enabled when tracing is enabled. | |
2911 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
2912 | */ | |
936e074b | 2913 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
77a2b37d | 2914 | { |
f45948e8 | 2915 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
41c52c0d | 2916 | } |
936e074b | 2917 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
4eebcc81 | 2918 | |
41c52c0d SR |
2919 | /** |
2920 | * ftrace_set_notrace - set a function to not trace in ftrace | |
936e074b | 2921 | * @ops - the ops to set the notrace filter with |
41c52c0d SR |
2922 | * @buf - the string that holds the function notrace text. |
2923 | * @len - the length of the string. | |
2924 | * @reset - non zero to reset all filters before applying this filter. | |
2925 | * | |
2926 | * Notrace Filters denote which functions should not be enabled when tracing | |
2927 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
2928 | * for tracing. | |
2929 | */ | |
936e074b | 2930 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
41c52c0d | 2931 | { |
f45948e8 | 2932 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
77a2b37d | 2933 | } |
936e074b | 2934 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
77a2b37d | 2935 | |
2af15d6a SR |
2936 | /* |
2937 | * command line interface to allow users to set filters on boot up. | |
2938 | */ | |
2939 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE | |
2940 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; | |
2941 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; | |
2942 | ||
2943 | static int __init set_ftrace_notrace(char *str) | |
2944 | { | |
2945 | strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); | |
2946 | return 1; | |
2947 | } | |
2948 | __setup("ftrace_notrace=", set_ftrace_notrace); | |
2949 | ||
2950 | static int __init set_ftrace_filter(char *str) | |
2951 | { | |
2952 | strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); | |
2953 | return 1; | |
2954 | } | |
2955 | __setup("ftrace_filter=", set_ftrace_filter); | |
2956 | ||
369bc18f | 2957 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
f6060f46 | 2958 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
801c29fd SR |
2959 | static int ftrace_set_func(unsigned long *array, int *idx, char *buffer); |
2960 | ||
369bc18f SA |
2961 | static int __init set_graph_function(char *str) |
2962 | { | |
06f43d66 | 2963 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
369bc18f SA |
2964 | return 1; |
2965 | } | |
2966 | __setup("ftrace_graph_filter=", set_graph_function); | |
2967 | ||
2968 | static void __init set_ftrace_early_graph(char *buf) | |
2969 | { | |
2970 | int ret; | |
2971 | char *func; | |
2972 | ||
2973 | while (buf) { | |
2974 | func = strsep(&buf, ","); | |
2975 | /* we allow only one expression at a time */ | |
2976 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, | |
2977 | func); | |
2978 | if (ret) | |
2979 | printk(KERN_DEBUG "ftrace: function %s not " | |
2980 | "traceable\n", func); | |
2981 | } | |
2982 | } | |
2983 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2984 | ||
f45948e8 SR |
2985 | static void __init |
2986 | set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable) | |
2af15d6a SR |
2987 | { |
2988 | char *func; | |
2989 | ||
2990 | while (buf) { | |
2991 | func = strsep(&buf, ","); | |
f45948e8 | 2992 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
2af15d6a SR |
2993 | } |
2994 | } | |
2995 | ||
2996 | static void __init set_ftrace_early_filters(void) | |
2997 | { | |
2998 | if (ftrace_filter_buf[0]) | |
f45948e8 | 2999 | set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1); |
2af15d6a | 3000 | if (ftrace_notrace_buf[0]) |
f45948e8 | 3001 | set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0); |
369bc18f SA |
3002 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
3003 | if (ftrace_graph_buf[0]) | |
3004 | set_ftrace_early_graph(ftrace_graph_buf); | |
3005 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2af15d6a SR |
3006 | } |
3007 | ||
e309b41d | 3008 | static int |
1cf41dd7 | 3009 | ftrace_regex_release(struct inode *inode, struct file *file) |
5072c59f SR |
3010 | { |
3011 | struct seq_file *m = (struct seq_file *)file->private_data; | |
3012 | struct ftrace_iterator *iter; | |
33dc9b12 | 3013 | struct ftrace_hash **orig_hash; |
689fd8b6 | 3014 | struct trace_parser *parser; |
ed926f9b | 3015 | int filter_hash; |
33dc9b12 | 3016 | int ret; |
5072c59f | 3017 | |
41c52c0d | 3018 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
3019 | if (file->f_mode & FMODE_READ) { |
3020 | iter = m->private; | |
3021 | ||
3022 | seq_release(inode, file); | |
3023 | } else | |
3024 | iter = file->private_data; | |
3025 | ||
689fd8b6 | 3026 | parser = &iter->parser; |
3027 | if (trace_parser_loaded(parser)) { | |
3028 | parser->buffer[parser->idx] = 0; | |
1cf41dd7 | 3029 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
5072c59f SR |
3030 | } |
3031 | ||
689fd8b6 | 3032 | trace_parser_put(parser); |
689fd8b6 | 3033 | |
058e297d | 3034 | if (file->f_mode & FMODE_WRITE) { |
ed926f9b SR |
3035 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
3036 | ||
3037 | if (filter_hash) | |
33dc9b12 | 3038 | orig_hash = &iter->ops->filter_hash; |
ed926f9b SR |
3039 | else |
3040 | orig_hash = &iter->ops->notrace_hash; | |
33dc9b12 | 3041 | |
058e297d | 3042 | mutex_lock(&ftrace_lock); |
ed926f9b SR |
3043 | /* |
3044 | * Remove the current set, update the hash and add | |
3045 | * them back. | |
3046 | */ | |
3047 | ftrace_hash_rec_disable(iter->ops, filter_hash); | |
33dc9b12 | 3048 | ret = ftrace_hash_move(orig_hash, iter->hash); |
ed926f9b SR |
3049 | if (!ret) { |
3050 | ftrace_hash_rec_enable(iter->ops, filter_hash); | |
3051 | if (iter->ops->flags & FTRACE_OPS_FL_ENABLED | |
3052 | && ftrace_enabled) | |
3053 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); | |
3054 | } | |
058e297d SR |
3055 | mutex_unlock(&ftrace_lock); |
3056 | } | |
33dc9b12 SR |
3057 | free_ftrace_hash(iter->hash); |
3058 | kfree(iter); | |
058e297d | 3059 | |
41c52c0d | 3060 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
3061 | return 0; |
3062 | } | |
3063 | ||
5e2336a0 | 3064 | static const struct file_operations ftrace_avail_fops = { |
5072c59f SR |
3065 | .open = ftrace_avail_open, |
3066 | .read = seq_read, | |
3067 | .llseek = seq_lseek, | |
3be04b47 | 3068 | .release = seq_release_private, |
5072c59f SR |
3069 | }; |
3070 | ||
647bcd03 SR |
3071 | static const struct file_operations ftrace_enabled_fops = { |
3072 | .open = ftrace_enabled_open, | |
3073 | .read = seq_read, | |
3074 | .llseek = seq_lseek, | |
3075 | .release = seq_release_private, | |
3076 | }; | |
3077 | ||
5e2336a0 | 3078 | static const struct file_operations ftrace_filter_fops = { |
5072c59f | 3079 | .open = ftrace_filter_open, |
850a80cf | 3080 | .read = seq_read, |
5072c59f | 3081 | .write = ftrace_filter_write, |
98c4fd04 | 3082 | .llseek = ftrace_regex_lseek, |
1cf41dd7 | 3083 | .release = ftrace_regex_release, |
5072c59f SR |
3084 | }; |
3085 | ||
5e2336a0 | 3086 | static const struct file_operations ftrace_notrace_fops = { |
41c52c0d | 3087 | .open = ftrace_notrace_open, |
850a80cf | 3088 | .read = seq_read, |
41c52c0d SR |
3089 | .write = ftrace_notrace_write, |
3090 | .llseek = ftrace_regex_lseek, | |
1cf41dd7 | 3091 | .release = ftrace_regex_release, |
41c52c0d SR |
3092 | }; |
3093 | ||
ea4e2bc4 SR |
3094 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
3095 | ||
3096 | static DEFINE_MUTEX(graph_lock); | |
3097 | ||
3098 | int ftrace_graph_count; | |
c7c6b1fe | 3099 | int ftrace_graph_filter_enabled; |
ea4e2bc4 SR |
3100 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
3101 | ||
3102 | static void * | |
85951842 | 3103 | __g_next(struct seq_file *m, loff_t *pos) |
ea4e2bc4 | 3104 | { |
85951842 | 3105 | if (*pos >= ftrace_graph_count) |
ea4e2bc4 | 3106 | return NULL; |
a4ec5e0c | 3107 | return &ftrace_graph_funcs[*pos]; |
85951842 | 3108 | } |
ea4e2bc4 | 3109 | |
85951842 LZ |
3110 | static void * |
3111 | g_next(struct seq_file *m, void *v, loff_t *pos) | |
3112 | { | |
3113 | (*pos)++; | |
3114 | return __g_next(m, pos); | |
ea4e2bc4 SR |
3115 | } |
3116 | ||
3117 | static void *g_start(struct seq_file *m, loff_t *pos) | |
3118 | { | |
ea4e2bc4 SR |
3119 | mutex_lock(&graph_lock); |
3120 | ||
f9349a8f | 3121 | /* Nothing, tell g_show to print all functions are enabled */ |
c7c6b1fe | 3122 | if (!ftrace_graph_filter_enabled && !*pos) |
f9349a8f FW |
3123 | return (void *)1; |
3124 | ||
85951842 | 3125 | return __g_next(m, pos); |
ea4e2bc4 SR |
3126 | } |
3127 | ||
3128 | static void g_stop(struct seq_file *m, void *p) | |
3129 | { | |
3130 | mutex_unlock(&graph_lock); | |
3131 | } | |
3132 | ||
3133 | static int g_show(struct seq_file *m, void *v) | |
3134 | { | |
3135 | unsigned long *ptr = v; | |
ea4e2bc4 SR |
3136 | |
3137 | if (!ptr) | |
3138 | return 0; | |
3139 | ||
f9349a8f FW |
3140 | if (ptr == (unsigned long *)1) { |
3141 | seq_printf(m, "#### all functions enabled ####\n"); | |
3142 | return 0; | |
3143 | } | |
3144 | ||
b375a11a | 3145 | seq_printf(m, "%ps\n", (void *)*ptr); |
ea4e2bc4 SR |
3146 | |
3147 | return 0; | |
3148 | } | |
3149 | ||
88e9d34c | 3150 | static const struct seq_operations ftrace_graph_seq_ops = { |
ea4e2bc4 SR |
3151 | .start = g_start, |
3152 | .next = g_next, | |
3153 | .stop = g_stop, | |
3154 | .show = g_show, | |
3155 | }; | |
3156 | ||
3157 | static int | |
3158 | ftrace_graph_open(struct inode *inode, struct file *file) | |
3159 | { | |
3160 | int ret = 0; | |
3161 | ||
3162 | if (unlikely(ftrace_disabled)) | |
3163 | return -ENODEV; | |
3164 | ||
3165 | mutex_lock(&graph_lock); | |
3166 | if ((file->f_mode & FMODE_WRITE) && | |
8650ae32 | 3167 | (file->f_flags & O_TRUNC)) { |
c7c6b1fe | 3168 | ftrace_graph_filter_enabled = 0; |
ea4e2bc4 SR |
3169 | ftrace_graph_count = 0; |
3170 | memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs)); | |
3171 | } | |
a4ec5e0c | 3172 | mutex_unlock(&graph_lock); |
ea4e2bc4 | 3173 | |
a4ec5e0c | 3174 | if (file->f_mode & FMODE_READ) |
ea4e2bc4 | 3175 | ret = seq_open(file, &ftrace_graph_seq_ops); |
ea4e2bc4 SR |
3176 | |
3177 | return ret; | |
3178 | } | |
3179 | ||
87827111 LZ |
3180 | static int |
3181 | ftrace_graph_release(struct inode *inode, struct file *file) | |
3182 | { | |
3183 | if (file->f_mode & FMODE_READ) | |
3184 | seq_release(inode, file); | |
3185 | return 0; | |
3186 | } | |
3187 | ||
ea4e2bc4 | 3188 | static int |
f9349a8f | 3189 | ftrace_set_func(unsigned long *array, int *idx, char *buffer) |
ea4e2bc4 | 3190 | { |
ea4e2bc4 SR |
3191 | struct dyn_ftrace *rec; |
3192 | struct ftrace_page *pg; | |
f9349a8f | 3193 | int search_len; |
c7c6b1fe | 3194 | int fail = 1; |
f9349a8f FW |
3195 | int type, not; |
3196 | char *search; | |
3197 | bool exists; | |
3198 | int i; | |
ea4e2bc4 | 3199 | |
f9349a8f | 3200 | /* decode regex */ |
3f6fe06d | 3201 | type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); |
c7c6b1fe LZ |
3202 | if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS) |
3203 | return -EBUSY; | |
f9349a8f FW |
3204 | |
3205 | search_len = strlen(search); | |
3206 | ||
52baf119 | 3207 | mutex_lock(&ftrace_lock); |
45a4a237 SR |
3208 | |
3209 | if (unlikely(ftrace_disabled)) { | |
3210 | mutex_unlock(&ftrace_lock); | |
3211 | return -ENODEV; | |
3212 | } | |
3213 | ||
265c831c SR |
3214 | do_for_each_ftrace_rec(pg, rec) { |
3215 | ||
45a4a237 | 3216 | if (rec->flags & FTRACE_FL_FREE) |
265c831c SR |
3217 | continue; |
3218 | ||
b9df92d2 | 3219 | if (ftrace_match_record(rec, NULL, search, search_len, type)) { |
c7c6b1fe | 3220 | /* if it is in the array */ |
f9349a8f | 3221 | exists = false; |
c7c6b1fe | 3222 | for (i = 0; i < *idx; i++) { |
f9349a8f FW |
3223 | if (array[i] == rec->ip) { |
3224 | exists = true; | |
265c831c SR |
3225 | break; |
3226 | } | |
c7c6b1fe LZ |
3227 | } |
3228 | ||
3229 | if (!not) { | |
3230 | fail = 0; | |
3231 | if (!exists) { | |
3232 | array[(*idx)++] = rec->ip; | |
3233 | if (*idx >= FTRACE_GRAPH_MAX_FUNCS) | |
3234 | goto out; | |
3235 | } | |
3236 | } else { | |
3237 | if (exists) { | |
3238 | array[i] = array[--(*idx)]; | |
3239 | array[*idx] = 0; | |
3240 | fail = 0; | |
3241 | } | |
3242 | } | |
ea4e2bc4 | 3243 | } |
265c831c | 3244 | } while_for_each_ftrace_rec(); |
c7c6b1fe | 3245 | out: |
52baf119 | 3246 | mutex_unlock(&ftrace_lock); |
ea4e2bc4 | 3247 | |
c7c6b1fe LZ |
3248 | if (fail) |
3249 | return -EINVAL; | |
3250 | ||
3251 | ftrace_graph_filter_enabled = 1; | |
3252 | return 0; | |
ea4e2bc4 SR |
3253 | } |
3254 | ||
3255 | static ssize_t | |
3256 | ftrace_graph_write(struct file *file, const char __user *ubuf, | |
3257 | size_t cnt, loff_t *ppos) | |
3258 | { | |
689fd8b6 | 3259 | struct trace_parser parser; |
4ba7978e | 3260 | ssize_t read, ret; |
ea4e2bc4 | 3261 | |
c7c6b1fe | 3262 | if (!cnt) |
ea4e2bc4 SR |
3263 | return 0; |
3264 | ||
3265 | mutex_lock(&graph_lock); | |
3266 | ||
689fd8b6 | 3267 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) { |
3268 | ret = -ENOMEM; | |
1eb90f13 | 3269 | goto out_unlock; |
ea4e2bc4 SR |
3270 | } |
3271 | ||
689fd8b6 | 3272 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
ea4e2bc4 | 3273 | |
4ba7978e | 3274 | if (read >= 0 && trace_parser_loaded((&parser))) { |
689fd8b6 | 3275 | parser.buffer[parser.idx] = 0; |
3276 | ||
3277 | /* we allow only one expression at a time */ | |
a4ec5e0c | 3278 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, |
689fd8b6 | 3279 | parser.buffer); |
ea4e2bc4 | 3280 | if (ret) |
1eb90f13 | 3281 | goto out_free; |
ea4e2bc4 | 3282 | } |
ea4e2bc4 SR |
3283 | |
3284 | ret = read; | |
1eb90f13 LZ |
3285 | |
3286 | out_free: | |
689fd8b6 | 3287 | trace_parser_put(&parser); |
1eb90f13 | 3288 | out_unlock: |
ea4e2bc4 SR |
3289 | mutex_unlock(&graph_lock); |
3290 | ||
3291 | return ret; | |
3292 | } | |
3293 | ||
3294 | static const struct file_operations ftrace_graph_fops = { | |
87827111 LZ |
3295 | .open = ftrace_graph_open, |
3296 | .read = seq_read, | |
3297 | .write = ftrace_graph_write, | |
3298 | .release = ftrace_graph_release, | |
6038f373 | 3299 | .llseek = seq_lseek, |
ea4e2bc4 SR |
3300 | }; |
3301 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
3302 | ||
df4fc315 | 3303 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
5072c59f | 3304 | { |
5072c59f | 3305 | |
5452af66 FW |
3306 | trace_create_file("available_filter_functions", 0444, |
3307 | d_tracer, NULL, &ftrace_avail_fops); | |
5072c59f | 3308 | |
647bcd03 SR |
3309 | trace_create_file("enabled_functions", 0444, |
3310 | d_tracer, NULL, &ftrace_enabled_fops); | |
3311 | ||
5452af66 FW |
3312 | trace_create_file("set_ftrace_filter", 0644, d_tracer, |
3313 | NULL, &ftrace_filter_fops); | |
41c52c0d | 3314 | |
5452af66 | 3315 | trace_create_file("set_ftrace_notrace", 0644, d_tracer, |
41c52c0d | 3316 | NULL, &ftrace_notrace_fops); |
ad90c0e3 | 3317 | |
ea4e2bc4 | 3318 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
5452af66 | 3319 | trace_create_file("set_graph_function", 0444, d_tracer, |
ea4e2bc4 SR |
3320 | NULL, |
3321 | &ftrace_graph_fops); | |
ea4e2bc4 SR |
3322 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
3323 | ||
5072c59f SR |
3324 | return 0; |
3325 | } | |
3326 | ||
5cb084bb | 3327 | static int ftrace_process_locs(struct module *mod, |
31e88909 | 3328 | unsigned long *start, |
68bf21aa SR |
3329 | unsigned long *end) |
3330 | { | |
3331 | unsigned long *p; | |
3332 | unsigned long addr; | |
68bf21aa | 3333 | |
e6ea44e9 | 3334 | mutex_lock(&ftrace_lock); |
68bf21aa SR |
3335 | p = start; |
3336 | while (p < end) { | |
3337 | addr = ftrace_call_adjust(*p++); | |
20e5227e SR |
3338 | /* |
3339 | * Some architecture linkers will pad between | |
3340 | * the different mcount_loc sections of different | |
3341 | * object files to satisfy alignments. | |
3342 | * Skip any NULL pointers. | |
3343 | */ | |
3344 | if (!addr) | |
3345 | continue; | |
68bf21aa | 3346 | ftrace_record_ip(addr); |
68bf21aa SR |
3347 | } |
3348 | ||
31e88909 | 3349 | ftrace_update_code(mod); |
e6ea44e9 | 3350 | mutex_unlock(&ftrace_lock); |
68bf21aa SR |
3351 | |
3352 | return 0; | |
3353 | } | |
3354 | ||
93eb677d | 3355 | #ifdef CONFIG_MODULES |
e7247a15 | 3356 | void ftrace_release_mod(struct module *mod) |
93eb677d SR |
3357 | { |
3358 | struct dyn_ftrace *rec; | |
3359 | struct ftrace_page *pg; | |
93eb677d | 3360 | |
45a4a237 SR |
3361 | mutex_lock(&ftrace_lock); |
3362 | ||
e7247a15 | 3363 | if (ftrace_disabled) |
45a4a237 | 3364 | goto out_unlock; |
93eb677d | 3365 | |
93eb677d | 3366 | do_for_each_ftrace_rec(pg, rec) { |
e7247a15 | 3367 | if (within_module_core(rec->ip, mod)) { |
93eb677d SR |
3368 | /* |
3369 | * rec->ip is changed in ftrace_free_rec() | |
3370 | * It should not between s and e if record was freed. | |
3371 | */ | |
3372 | FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE); | |
3373 | ftrace_free_rec(rec); | |
3374 | } | |
3375 | } while_for_each_ftrace_rec(); | |
45a4a237 | 3376 | out_unlock: |
93eb677d SR |
3377 | mutex_unlock(&ftrace_lock); |
3378 | } | |
3379 | ||
3380 | static void ftrace_init_module(struct module *mod, | |
3381 | unsigned long *start, unsigned long *end) | |
90d595fe | 3382 | { |
00fd61ae | 3383 | if (ftrace_disabled || start == end) |
fed1939c | 3384 | return; |
5cb084bb | 3385 | ftrace_process_locs(mod, start, end); |
90d595fe SR |
3386 | } |
3387 | ||
93eb677d SR |
3388 | static int ftrace_module_notify(struct notifier_block *self, |
3389 | unsigned long val, void *data) | |
3390 | { | |
3391 | struct module *mod = data; | |
3392 | ||
3393 | switch (val) { | |
3394 | case MODULE_STATE_COMING: | |
3395 | ftrace_init_module(mod, mod->ftrace_callsites, | |
3396 | mod->ftrace_callsites + | |
3397 | mod->num_ftrace_callsites); | |
3398 | break; | |
3399 | case MODULE_STATE_GOING: | |
e7247a15 | 3400 | ftrace_release_mod(mod); |
93eb677d SR |
3401 | break; |
3402 | } | |
3403 | ||
3404 | return 0; | |
3405 | } | |
3406 | #else | |
3407 | static int ftrace_module_notify(struct notifier_block *self, | |
3408 | unsigned long val, void *data) | |
3409 | { | |
3410 | return 0; | |
3411 | } | |
3412 | #endif /* CONFIG_MODULES */ | |
3413 | ||
3414 | struct notifier_block ftrace_module_nb = { | |
3415 | .notifier_call = ftrace_module_notify, | |
3416 | .priority = 0, | |
3417 | }; | |
3418 | ||
68bf21aa SR |
3419 | extern unsigned long __start_mcount_loc[]; |
3420 | extern unsigned long __stop_mcount_loc[]; | |
3421 | ||
3422 | void __init ftrace_init(void) | |
3423 | { | |
3424 | unsigned long count, addr, flags; | |
3425 | int ret; | |
3426 | ||
3427 | /* Keep the ftrace pointer to the stub */ | |
3428 | addr = (unsigned long)ftrace_stub; | |
3429 | ||
3430 | local_irq_save(flags); | |
3431 | ftrace_dyn_arch_init(&addr); | |
3432 | local_irq_restore(flags); | |
3433 | ||
3434 | /* ftrace_dyn_arch_init places the return code in addr */ | |
3435 | if (addr) | |
3436 | goto failed; | |
3437 | ||
3438 | count = __stop_mcount_loc - __start_mcount_loc; | |
3439 | ||
3440 | ret = ftrace_dyn_table_alloc(count); | |
3441 | if (ret) | |
3442 | goto failed; | |
3443 | ||
3444 | last_ftrace_enabled = ftrace_enabled = 1; | |
3445 | ||
5cb084bb | 3446 | ret = ftrace_process_locs(NULL, |
31e88909 | 3447 | __start_mcount_loc, |
68bf21aa SR |
3448 | __stop_mcount_loc); |
3449 | ||
93eb677d | 3450 | ret = register_module_notifier(&ftrace_module_nb); |
24ed0c4b | 3451 | if (ret) |
93eb677d SR |
3452 | pr_warning("Failed to register trace ftrace module notifier\n"); |
3453 | ||
2af15d6a SR |
3454 | set_ftrace_early_filters(); |
3455 | ||
68bf21aa SR |
3456 | return; |
3457 | failed: | |
3458 | ftrace_disabled = 1; | |
3459 | } | |
68bf21aa | 3460 | |
3d083395 | 3461 | #else |
0b6e4d56 | 3462 | |
2b499381 | 3463 | static struct ftrace_ops global_ops = { |
bd69c30b SR |
3464 | .func = ftrace_stub, |
3465 | }; | |
3466 | ||
0b6e4d56 FW |
3467 | static int __init ftrace_nodyn_init(void) |
3468 | { | |
3469 | ftrace_enabled = 1; | |
3470 | return 0; | |
3471 | } | |
3472 | device_initcall(ftrace_nodyn_init); | |
3473 | ||
df4fc315 SR |
3474 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
3475 | static inline void ftrace_startup_enable(int command) { } | |
5a45cfe1 | 3476 | /* Keep as macros so we do not need to define the commands */ |
3b6cfdb1 SR |
3477 | # define ftrace_startup(ops, command) \ |
3478 | ({ \ | |
3479 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ | |
3480 | 0; \ | |
3481 | }) | |
bd69c30b | 3482 | # define ftrace_shutdown(ops, command) do { } while (0) |
c7aafc54 IM |
3483 | # define ftrace_startup_sysctl() do { } while (0) |
3484 | # define ftrace_shutdown_sysctl() do { } while (0) | |
b848914c SR |
3485 | |
3486 | static inline int | |
3487 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip) | |
3488 | { | |
3489 | return 1; | |
3490 | } | |
3491 | ||
3d083395 SR |
3492 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
3493 | ||
b848914c SR |
3494 | static void |
3495 | ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip) | |
3496 | { | |
cdbe61bf | 3497 | struct ftrace_ops *op; |
b848914c | 3498 | |
b1cff0ad SR |
3499 | if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT))) |
3500 | return; | |
3501 | ||
3502 | trace_recursion_set(TRACE_INTERNAL_BIT); | |
cdbe61bf SR |
3503 | /* |
3504 | * Some of the ops may be dynamically allocated, | |
3505 | * they must be freed after a synchronize_sched(). | |
3506 | */ | |
3507 | preempt_disable_notrace(); | |
3508 | op = rcu_dereference_raw(ftrace_ops_list); | |
b848914c SR |
3509 | while (op != &ftrace_list_end) { |
3510 | if (ftrace_ops_test(op, ip)) | |
3511 | op->func(ip, parent_ip); | |
3512 | op = rcu_dereference_raw(op->next); | |
3513 | }; | |
cdbe61bf | 3514 | preempt_enable_notrace(); |
b1cff0ad | 3515 | trace_recursion_clear(TRACE_INTERNAL_BIT); |
b848914c SR |
3516 | } |
3517 | ||
e32d8956 | 3518 | static void clear_ftrace_swapper(void) |
978f3a45 SR |
3519 | { |
3520 | struct task_struct *p; | |
e32d8956 | 3521 | int cpu; |
978f3a45 | 3522 | |
e32d8956 SR |
3523 | get_online_cpus(); |
3524 | for_each_online_cpu(cpu) { | |
3525 | p = idle_task(cpu); | |
978f3a45 | 3526 | clear_tsk_trace_trace(p); |
e32d8956 SR |
3527 | } |
3528 | put_online_cpus(); | |
3529 | } | |
978f3a45 | 3530 | |
e32d8956 SR |
3531 | static void set_ftrace_swapper(void) |
3532 | { | |
3533 | struct task_struct *p; | |
3534 | int cpu; | |
3535 | ||
3536 | get_online_cpus(); | |
3537 | for_each_online_cpu(cpu) { | |
3538 | p = idle_task(cpu); | |
3539 | set_tsk_trace_trace(p); | |
3540 | } | |
3541 | put_online_cpus(); | |
978f3a45 SR |
3542 | } |
3543 | ||
e32d8956 SR |
3544 | static void clear_ftrace_pid(struct pid *pid) |
3545 | { | |
3546 | struct task_struct *p; | |
3547 | ||
229c4ef8 | 3548 | rcu_read_lock(); |
e32d8956 SR |
3549 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
3550 | clear_tsk_trace_trace(p); | |
3551 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 ON |
3552 | rcu_read_unlock(); |
3553 | ||
e32d8956 SR |
3554 | put_pid(pid); |
3555 | } | |
3556 | ||
3557 | static void set_ftrace_pid(struct pid *pid) | |
978f3a45 SR |
3558 | { |
3559 | struct task_struct *p; | |
3560 | ||
229c4ef8 | 3561 | rcu_read_lock(); |
978f3a45 SR |
3562 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
3563 | set_tsk_trace_trace(p); | |
3564 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 | 3565 | rcu_read_unlock(); |
978f3a45 SR |
3566 | } |
3567 | ||
756d17ee | 3568 | static void clear_ftrace_pid_task(struct pid *pid) |
e32d8956 | 3569 | { |
756d17ee | 3570 | if (pid == ftrace_swapper_pid) |
e32d8956 SR |
3571 | clear_ftrace_swapper(); |
3572 | else | |
756d17ee | 3573 | clear_ftrace_pid(pid); |
e32d8956 SR |
3574 | } |
3575 | ||
3576 | static void set_ftrace_pid_task(struct pid *pid) | |
3577 | { | |
3578 | if (pid == ftrace_swapper_pid) | |
3579 | set_ftrace_swapper(); | |
3580 | else | |
3581 | set_ftrace_pid(pid); | |
3582 | } | |
3583 | ||
756d17ee | 3584 | static int ftrace_pid_add(int p) |
df4fc315 | 3585 | { |
978f3a45 | 3586 | struct pid *pid; |
756d17ee | 3587 | struct ftrace_pid *fpid; |
3588 | int ret = -EINVAL; | |
df4fc315 | 3589 | |
756d17ee | 3590 | mutex_lock(&ftrace_lock); |
df4fc315 | 3591 | |
756d17ee | 3592 | if (!p) |
3593 | pid = ftrace_swapper_pid; | |
3594 | else | |
3595 | pid = find_get_pid(p); | |
df4fc315 | 3596 | |
756d17ee | 3597 | if (!pid) |
3598 | goto out; | |
df4fc315 | 3599 | |
756d17ee | 3600 | ret = 0; |
df4fc315 | 3601 | |
756d17ee | 3602 | list_for_each_entry(fpid, &ftrace_pids, list) |
3603 | if (fpid->pid == pid) | |
3604 | goto out_put; | |
978f3a45 | 3605 | |
756d17ee | 3606 | ret = -ENOMEM; |
df4fc315 | 3607 | |
756d17ee | 3608 | fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); |
3609 | if (!fpid) | |
3610 | goto out_put; | |
df4fc315 | 3611 | |
756d17ee | 3612 | list_add(&fpid->list, &ftrace_pids); |
3613 | fpid->pid = pid; | |
0ef8cde5 | 3614 | |
756d17ee | 3615 | set_ftrace_pid_task(pid); |
978f3a45 | 3616 | |
756d17ee | 3617 | ftrace_update_pid_func(); |
3618 | ftrace_startup_enable(0); | |
3619 | ||
3620 | mutex_unlock(&ftrace_lock); | |
3621 | return 0; | |
3622 | ||
3623 | out_put: | |
3624 | if (pid != ftrace_swapper_pid) | |
3625 | put_pid(pid); | |
978f3a45 | 3626 | |
756d17ee | 3627 | out: |
3628 | mutex_unlock(&ftrace_lock); | |
3629 | return ret; | |
3630 | } | |
3631 | ||
3632 | static void ftrace_pid_reset(void) | |
3633 | { | |
3634 | struct ftrace_pid *fpid, *safe; | |
978f3a45 | 3635 | |
756d17ee | 3636 | mutex_lock(&ftrace_lock); |
3637 | list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { | |
3638 | struct pid *pid = fpid->pid; | |
3639 | ||
3640 | clear_ftrace_pid_task(pid); | |
3641 | ||
3642 | list_del(&fpid->list); | |
3643 | kfree(fpid); | |
df4fc315 SR |
3644 | } |
3645 | ||
df4fc315 SR |
3646 | ftrace_update_pid_func(); |
3647 | ftrace_startup_enable(0); | |
3648 | ||
e6ea44e9 | 3649 | mutex_unlock(&ftrace_lock); |
756d17ee | 3650 | } |
df4fc315 | 3651 | |
756d17ee | 3652 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
3653 | { | |
3654 | mutex_lock(&ftrace_lock); | |
3655 | ||
3656 | if (list_empty(&ftrace_pids) && (!*pos)) | |
3657 | return (void *) 1; | |
3658 | ||
3659 | return seq_list_start(&ftrace_pids, *pos); | |
3660 | } | |
3661 | ||
3662 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) | |
3663 | { | |
3664 | if (v == (void *)1) | |
3665 | return NULL; | |
3666 | ||
3667 | return seq_list_next(v, &ftrace_pids, pos); | |
3668 | } | |
3669 | ||
3670 | static void fpid_stop(struct seq_file *m, void *p) | |
3671 | { | |
3672 | mutex_unlock(&ftrace_lock); | |
3673 | } | |
3674 | ||
3675 | static int fpid_show(struct seq_file *m, void *v) | |
3676 | { | |
3677 | const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); | |
3678 | ||
3679 | if (v == (void *)1) { | |
3680 | seq_printf(m, "no pid\n"); | |
3681 | return 0; | |
3682 | } | |
3683 | ||
3684 | if (fpid->pid == ftrace_swapper_pid) | |
3685 | seq_printf(m, "swapper tasks\n"); | |
3686 | else | |
3687 | seq_printf(m, "%u\n", pid_vnr(fpid->pid)); | |
3688 | ||
3689 | return 0; | |
3690 | } | |
3691 | ||
3692 | static const struct seq_operations ftrace_pid_sops = { | |
3693 | .start = fpid_start, | |
3694 | .next = fpid_next, | |
3695 | .stop = fpid_stop, | |
3696 | .show = fpid_show, | |
3697 | }; | |
3698 | ||
3699 | static int | |
3700 | ftrace_pid_open(struct inode *inode, struct file *file) | |
3701 | { | |
3702 | int ret = 0; | |
3703 | ||
3704 | if ((file->f_mode & FMODE_WRITE) && | |
3705 | (file->f_flags & O_TRUNC)) | |
3706 | ftrace_pid_reset(); | |
3707 | ||
3708 | if (file->f_mode & FMODE_READ) | |
3709 | ret = seq_open(file, &ftrace_pid_sops); | |
3710 | ||
3711 | return ret; | |
3712 | } | |
3713 | ||
df4fc315 SR |
3714 | static ssize_t |
3715 | ftrace_pid_write(struct file *filp, const char __user *ubuf, | |
3716 | size_t cnt, loff_t *ppos) | |
3717 | { | |
457dc928 | 3718 | char buf[64], *tmp; |
df4fc315 SR |
3719 | long val; |
3720 | int ret; | |
3721 | ||
3722 | if (cnt >= sizeof(buf)) | |
3723 | return -EINVAL; | |
3724 | ||
3725 | if (copy_from_user(&buf, ubuf, cnt)) | |
3726 | return -EFAULT; | |
3727 | ||
3728 | buf[cnt] = 0; | |
3729 | ||
756d17ee | 3730 | /* |
3731 | * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" | |
3732 | * to clean the filter quietly. | |
3733 | */ | |
457dc928 IM |
3734 | tmp = strstrip(buf); |
3735 | if (strlen(tmp) == 0) | |
756d17ee | 3736 | return 1; |
3737 | ||
457dc928 | 3738 | ret = strict_strtol(tmp, 10, &val); |
df4fc315 SR |
3739 | if (ret < 0) |
3740 | return ret; | |
3741 | ||
756d17ee | 3742 | ret = ftrace_pid_add(val); |
df4fc315 | 3743 | |
756d17ee | 3744 | return ret ? ret : cnt; |
3745 | } | |
df4fc315 | 3746 | |
756d17ee | 3747 | static int |
3748 | ftrace_pid_release(struct inode *inode, struct file *file) | |
3749 | { | |
3750 | if (file->f_mode & FMODE_READ) | |
3751 | seq_release(inode, file); | |
df4fc315 | 3752 | |
756d17ee | 3753 | return 0; |
df4fc315 SR |
3754 | } |
3755 | ||
5e2336a0 | 3756 | static const struct file_operations ftrace_pid_fops = { |
756d17ee | 3757 | .open = ftrace_pid_open, |
3758 | .write = ftrace_pid_write, | |
3759 | .read = seq_read, | |
3760 | .llseek = seq_lseek, | |
3761 | .release = ftrace_pid_release, | |
df4fc315 SR |
3762 | }; |
3763 | ||
3764 | static __init int ftrace_init_debugfs(void) | |
3765 | { | |
3766 | struct dentry *d_tracer; | |
df4fc315 SR |
3767 | |
3768 | d_tracer = tracing_init_dentry(); | |
3769 | if (!d_tracer) | |
3770 | return 0; | |
3771 | ||
3772 | ftrace_init_dyn_debugfs(d_tracer); | |
3773 | ||
5452af66 FW |
3774 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
3775 | NULL, &ftrace_pid_fops); | |
493762fc SR |
3776 | |
3777 | ftrace_profile_debugfs(d_tracer); | |
3778 | ||
df4fc315 SR |
3779 | return 0; |
3780 | } | |
df4fc315 SR |
3781 | fs_initcall(ftrace_init_debugfs); |
3782 | ||
a2bb6a3d | 3783 | /** |
81adbdc0 | 3784 | * ftrace_kill - kill ftrace |
a2bb6a3d SR |
3785 | * |
3786 | * This function should be used by panic code. It stops ftrace | |
3787 | * but in a not so nice way. If you need to simply kill ftrace | |
3788 | * from a non-atomic section, use ftrace_kill. | |
3789 | */ | |
81adbdc0 | 3790 | void ftrace_kill(void) |
a2bb6a3d SR |
3791 | { |
3792 | ftrace_disabled = 1; | |
3793 | ftrace_enabled = 0; | |
a2bb6a3d SR |
3794 | clear_ftrace_function(); |
3795 | } | |
3796 | ||
16444a8a | 3797 | /** |
3d083395 SR |
3798 | * register_ftrace_function - register a function for profiling |
3799 | * @ops - ops structure that holds the function for profiling. | |
16444a8a | 3800 | * |
3d083395 SR |
3801 | * Register a function to be called by all functions in the |
3802 | * kernel. | |
3803 | * | |
3804 | * Note: @ops->func and all the functions it calls must be labeled | |
3805 | * with "notrace", otherwise it will go into a | |
3806 | * recursive loop. | |
16444a8a | 3807 | */ |
3d083395 | 3808 | int register_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 3809 | { |
45a4a237 | 3810 | int ret = -1; |
4eebcc81 | 3811 | |
e6ea44e9 | 3812 | mutex_lock(&ftrace_lock); |
e7d3737e | 3813 | |
45a4a237 SR |
3814 | if (unlikely(ftrace_disabled)) |
3815 | goto out_unlock; | |
3816 | ||
b0fc494f | 3817 | ret = __register_ftrace_function(ops); |
b848914c | 3818 | if (!ret) |
a1cd6173 | 3819 | ret = ftrace_startup(ops, 0); |
b848914c | 3820 | |
b0fc494f | 3821 | |
45a4a237 | 3822 | out_unlock: |
e6ea44e9 | 3823 | mutex_unlock(&ftrace_lock); |
b0fc494f | 3824 | return ret; |
3d083395 | 3825 | } |
cdbe61bf | 3826 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
3d083395 SR |
3827 | |
3828 | /** | |
32632920 | 3829 | * unregister_ftrace_function - unregister a function for profiling. |
3d083395 SR |
3830 | * @ops - ops structure that holds the function to unregister |
3831 | * | |
3832 | * Unregister a function that was added to be called by ftrace profiling. | |
3833 | */ | |
3834 | int unregister_ftrace_function(struct ftrace_ops *ops) | |
3835 | { | |
3836 | int ret; | |
3837 | ||
e6ea44e9 | 3838 | mutex_lock(&ftrace_lock); |
3d083395 | 3839 | ret = __unregister_ftrace_function(ops); |
b848914c SR |
3840 | if (!ret) |
3841 | ftrace_shutdown(ops, 0); | |
e6ea44e9 | 3842 | mutex_unlock(&ftrace_lock); |
b0fc494f SR |
3843 | |
3844 | return ret; | |
3845 | } | |
cdbe61bf | 3846 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
b0fc494f | 3847 | |
e309b41d | 3848 | int |
b0fc494f | 3849 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
8d65af78 | 3850 | void __user *buffer, size_t *lenp, |
b0fc494f SR |
3851 | loff_t *ppos) |
3852 | { | |
45a4a237 | 3853 | int ret = -ENODEV; |
4eebcc81 | 3854 | |
e6ea44e9 | 3855 | mutex_lock(&ftrace_lock); |
b0fc494f | 3856 | |
45a4a237 SR |
3857 | if (unlikely(ftrace_disabled)) |
3858 | goto out; | |
3859 | ||
3860 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
b0fc494f | 3861 | |
a32c7765 | 3862 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
b0fc494f SR |
3863 | goto out; |
3864 | ||
a32c7765 | 3865 | last_ftrace_enabled = !!ftrace_enabled; |
b0fc494f SR |
3866 | |
3867 | if (ftrace_enabled) { | |
3868 | ||
3869 | ftrace_startup_sysctl(); | |
3870 | ||
3871 | /* we are starting ftrace again */ | |
b848914c SR |
3872 | if (ftrace_ops_list != &ftrace_list_end) { |
3873 | if (ftrace_ops_list->next == &ftrace_list_end) | |
3874 | ftrace_trace_function = ftrace_ops_list->func; | |
b0fc494f | 3875 | else |
b848914c | 3876 | ftrace_trace_function = ftrace_ops_list_func; |
b0fc494f SR |
3877 | } |
3878 | ||
3879 | } else { | |
3880 | /* stopping ftrace calls (just send to ftrace_stub) */ | |
3881 | ftrace_trace_function = ftrace_stub; | |
3882 | ||
3883 | ftrace_shutdown_sysctl(); | |
3884 | } | |
3885 | ||
3886 | out: | |
e6ea44e9 | 3887 | mutex_unlock(&ftrace_lock); |
3d083395 | 3888 | return ret; |
16444a8a | 3889 | } |
f17845e5 | 3890 | |
fb52607a | 3891 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
e7d3737e | 3892 | |
597af815 | 3893 | static int ftrace_graph_active; |
4a2b8dda | 3894 | static struct notifier_block ftrace_suspend_notifier; |
e7d3737e | 3895 | |
e49dc19c SR |
3896 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
3897 | { | |
3898 | return 0; | |
3899 | } | |
3900 | ||
287b6e68 FW |
3901 | /* The callbacks that hook a function */ |
3902 | trace_func_graph_ret_t ftrace_graph_return = | |
3903 | (trace_func_graph_ret_t)ftrace_stub; | |
e49dc19c | 3904 | trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
f201ae23 FW |
3905 | |
3906 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ | |
3907 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) | |
3908 | { | |
3909 | int i; | |
3910 | int ret = 0; | |
3911 | unsigned long flags; | |
3912 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; | |
3913 | struct task_struct *g, *t; | |
3914 | ||
3915 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { | |
3916 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH | |
3917 | * sizeof(struct ftrace_ret_stack), | |
3918 | GFP_KERNEL); | |
3919 | if (!ret_stack_list[i]) { | |
3920 | start = 0; | |
3921 | end = i; | |
3922 | ret = -ENOMEM; | |
3923 | goto free; | |
3924 | } | |
3925 | } | |
3926 | ||
3927 | read_lock_irqsave(&tasklist_lock, flags); | |
3928 | do_each_thread(g, t) { | |
3929 | if (start == end) { | |
3930 | ret = -EAGAIN; | |
3931 | goto unlock; | |
3932 | } | |
3933 | ||
3934 | if (t->ret_stack == NULL) { | |
380c4b14 | 3935 | atomic_set(&t->tracing_graph_pause, 0); |
f201ae23 | 3936 | atomic_set(&t->trace_overrun, 0); |
26c01624 SR |
3937 | t->curr_ret_stack = -1; |
3938 | /* Make sure the tasks see the -1 first: */ | |
3939 | smp_wmb(); | |
3940 | t->ret_stack = ret_stack_list[start++]; | |
f201ae23 FW |
3941 | } |
3942 | } while_each_thread(g, t); | |
3943 | ||
3944 | unlock: | |
3945 | read_unlock_irqrestore(&tasklist_lock, flags); | |
3946 | free: | |
3947 | for (i = start; i < end; i++) | |
3948 | kfree(ret_stack_list[i]); | |
3949 | return ret; | |
3950 | } | |
3951 | ||
8aef2d28 | 3952 | static void |
38516ab5 SR |
3953 | ftrace_graph_probe_sched_switch(void *ignore, |
3954 | struct task_struct *prev, struct task_struct *next) | |
8aef2d28 SR |
3955 | { |
3956 | unsigned long long timestamp; | |
3957 | int index; | |
3958 | ||
be6f164a SR |
3959 | /* |
3960 | * Does the user want to count the time a function was asleep. | |
3961 | * If so, do not update the time stamps. | |
3962 | */ | |
3963 | if (trace_flags & TRACE_ITER_SLEEP_TIME) | |
3964 | return; | |
3965 | ||
8aef2d28 SR |
3966 | timestamp = trace_clock_local(); |
3967 | ||
3968 | prev->ftrace_timestamp = timestamp; | |
3969 | ||
3970 | /* only process tasks that we timestamped */ | |
3971 | if (!next->ftrace_timestamp) | |
3972 | return; | |
3973 | ||
3974 | /* | |
3975 | * Update all the counters in next to make up for the | |
3976 | * time next was sleeping. | |
3977 | */ | |
3978 | timestamp -= next->ftrace_timestamp; | |
3979 | ||
3980 | for (index = next->curr_ret_stack; index >= 0; index--) | |
3981 | next->ret_stack[index].calltime += timestamp; | |
3982 | } | |
3983 | ||
f201ae23 | 3984 | /* Allocate a return stack for each task */ |
fb52607a | 3985 | static int start_graph_tracing(void) |
f201ae23 FW |
3986 | { |
3987 | struct ftrace_ret_stack **ret_stack_list; | |
5b058bcd | 3988 | int ret, cpu; |
f201ae23 FW |
3989 | |
3990 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * | |
3991 | sizeof(struct ftrace_ret_stack *), | |
3992 | GFP_KERNEL); | |
3993 | ||
3994 | if (!ret_stack_list) | |
3995 | return -ENOMEM; | |
3996 | ||
5b058bcd | 3997 | /* The cpu_boot init_task->ret_stack will never be freed */ |
179c498a SR |
3998 | for_each_online_cpu(cpu) { |
3999 | if (!idle_task(cpu)->ret_stack) | |
868baf07 | 4000 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
179c498a | 4001 | } |
5b058bcd | 4002 | |
f201ae23 FW |
4003 | do { |
4004 | ret = alloc_retstack_tasklist(ret_stack_list); | |
4005 | } while (ret == -EAGAIN); | |
4006 | ||
8aef2d28 | 4007 | if (!ret) { |
38516ab5 | 4008 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
8aef2d28 SR |
4009 | if (ret) |
4010 | pr_info("ftrace_graph: Couldn't activate tracepoint" | |
4011 | " probe to kernel_sched_switch\n"); | |
4012 | } | |
4013 | ||
f201ae23 FW |
4014 | kfree(ret_stack_list); |
4015 | return ret; | |
4016 | } | |
4017 | ||
4a2b8dda FW |
4018 | /* |
4019 | * Hibernation protection. | |
4020 | * The state of the current task is too much unstable during | |
4021 | * suspend/restore to disk. We want to protect against that. | |
4022 | */ | |
4023 | static int | |
4024 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, | |
4025 | void *unused) | |
4026 | { | |
4027 | switch (state) { | |
4028 | case PM_HIBERNATION_PREPARE: | |
4029 | pause_graph_tracing(); | |
4030 | break; | |
4031 | ||
4032 | case PM_POST_HIBERNATION: | |
4033 | unpause_graph_tracing(); | |
4034 | break; | |
4035 | } | |
4036 | return NOTIFY_DONE; | |
4037 | } | |
4038 | ||
287b6e68 FW |
4039 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
4040 | trace_func_graph_ent_t entryfunc) | |
15e6cb36 | 4041 | { |
e7d3737e FW |
4042 | int ret = 0; |
4043 | ||
e6ea44e9 | 4044 | mutex_lock(&ftrace_lock); |
e7d3737e | 4045 | |
05ce5818 | 4046 | /* we currently allow only one tracer registered at a time */ |
597af815 | 4047 | if (ftrace_graph_active) { |
05ce5818 SR |
4048 | ret = -EBUSY; |
4049 | goto out; | |
4050 | } | |
4051 | ||
4a2b8dda FW |
4052 | ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call; |
4053 | register_pm_notifier(&ftrace_suspend_notifier); | |
4054 | ||
597af815 | 4055 | ftrace_graph_active++; |
fb52607a | 4056 | ret = start_graph_tracing(); |
f201ae23 | 4057 | if (ret) { |
597af815 | 4058 | ftrace_graph_active--; |
f201ae23 FW |
4059 | goto out; |
4060 | } | |
e53a6319 | 4061 | |
287b6e68 FW |
4062 | ftrace_graph_return = retfunc; |
4063 | ftrace_graph_entry = entryfunc; | |
e53a6319 | 4064 | |
a1cd6173 | 4065 | ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET); |
e7d3737e FW |
4066 | |
4067 | out: | |
e6ea44e9 | 4068 | mutex_unlock(&ftrace_lock); |
e7d3737e | 4069 | return ret; |
15e6cb36 FW |
4070 | } |
4071 | ||
fb52607a | 4072 | void unregister_ftrace_graph(void) |
15e6cb36 | 4073 | { |
e6ea44e9 | 4074 | mutex_lock(&ftrace_lock); |
e7d3737e | 4075 | |
597af815 | 4076 | if (unlikely(!ftrace_graph_active)) |
2aad1b76 SR |
4077 | goto out; |
4078 | ||
597af815 | 4079 | ftrace_graph_active--; |
287b6e68 | 4080 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
e49dc19c | 4081 | ftrace_graph_entry = ftrace_graph_entry_stub; |
bd69c30b | 4082 | ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET); |
4a2b8dda | 4083 | unregister_pm_notifier(&ftrace_suspend_notifier); |
38516ab5 | 4084 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
e7d3737e | 4085 | |
2aad1b76 | 4086 | out: |
e6ea44e9 | 4087 | mutex_unlock(&ftrace_lock); |
15e6cb36 | 4088 | } |
f201ae23 | 4089 | |
868baf07 SR |
4090 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
4091 | ||
4092 | static void | |
4093 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) | |
4094 | { | |
4095 | atomic_set(&t->tracing_graph_pause, 0); | |
4096 | atomic_set(&t->trace_overrun, 0); | |
4097 | t->ftrace_timestamp = 0; | |
25985edc | 4098 | /* make curr_ret_stack visible before we add the ret_stack */ |
868baf07 SR |
4099 | smp_wmb(); |
4100 | t->ret_stack = ret_stack; | |
4101 | } | |
4102 | ||
4103 | /* | |
4104 | * Allocate a return stack for the idle task. May be the first | |
4105 | * time through, or it may be done by CPU hotplug online. | |
4106 | */ | |
4107 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) | |
4108 | { | |
4109 | t->curr_ret_stack = -1; | |
4110 | /* | |
4111 | * The idle task has no parent, it either has its own | |
4112 | * stack or no stack at all. | |
4113 | */ | |
4114 | if (t->ret_stack) | |
4115 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); | |
4116 | ||
4117 | if (ftrace_graph_active) { | |
4118 | struct ftrace_ret_stack *ret_stack; | |
4119 | ||
4120 | ret_stack = per_cpu(idle_ret_stack, cpu); | |
4121 | if (!ret_stack) { | |
4122 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH | |
4123 | * sizeof(struct ftrace_ret_stack), | |
4124 | GFP_KERNEL); | |
4125 | if (!ret_stack) | |
4126 | return; | |
4127 | per_cpu(idle_ret_stack, cpu) = ret_stack; | |
4128 | } | |
4129 | graph_init_task(t, ret_stack); | |
4130 | } | |
4131 | } | |
4132 | ||
f201ae23 | 4133 | /* Allocate a return stack for newly created task */ |
fb52607a | 4134 | void ftrace_graph_init_task(struct task_struct *t) |
f201ae23 | 4135 | { |
84047e36 SR |
4136 | /* Make sure we do not use the parent ret_stack */ |
4137 | t->ret_stack = NULL; | |
ea14eb71 | 4138 | t->curr_ret_stack = -1; |
84047e36 | 4139 | |
597af815 | 4140 | if (ftrace_graph_active) { |
82310a32 SR |
4141 | struct ftrace_ret_stack *ret_stack; |
4142 | ||
4143 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH | |
f201ae23 FW |
4144 | * sizeof(struct ftrace_ret_stack), |
4145 | GFP_KERNEL); | |
82310a32 | 4146 | if (!ret_stack) |
f201ae23 | 4147 | return; |
868baf07 | 4148 | graph_init_task(t, ret_stack); |
84047e36 | 4149 | } |
f201ae23 FW |
4150 | } |
4151 | ||
fb52607a | 4152 | void ftrace_graph_exit_task(struct task_struct *t) |
f201ae23 | 4153 | { |
eae849ca FW |
4154 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
4155 | ||
f201ae23 | 4156 | t->ret_stack = NULL; |
eae849ca FW |
4157 | /* NULL must become visible to IRQs before we free it: */ |
4158 | barrier(); | |
4159 | ||
4160 | kfree(ret_stack); | |
f201ae23 | 4161 | } |
14a866c5 SR |
4162 | |
4163 | void ftrace_graph_stop(void) | |
4164 | { | |
4165 | ftrace_stop(); | |
4166 | } | |
15e6cb36 | 4167 | #endif |