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