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