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