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