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