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