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