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