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