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