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