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