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