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