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