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