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