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