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