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