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