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