]>
Commit | Line | Data |
---|---|---|
16444a8a ACM |
1 | /* |
2 | * Infrastructure for profiling code inserted by 'gcc -pg'. | |
3 | * | |
4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> | |
5 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * Originally ported from the -rt patch by: | |
8 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> | |
9 | * | |
10 | * Based on code in the latency_tracer, that is: | |
11 | * | |
12 | * Copyright (C) 2004-2006 Ingo Molnar | |
13 | * Copyright (C) 2004 William Lee Irwin III | |
14 | */ | |
15 | ||
3d083395 SR |
16 | #include <linux/stop_machine.h> |
17 | #include <linux/clocksource.h> | |
18 | #include <linux/kallsyms.h> | |
5072c59f SR |
19 | #include <linux/seq_file.h> |
20 | #include <linux/debugfs.h> | |
3d083395 | 21 | #include <linux/hardirq.h> |
2d8b820b | 22 | #include <linux/kthread.h> |
5072c59f | 23 | #include <linux/uaccess.h> |
2d8b820b | 24 | #include <linux/ftrace.h> |
b0fc494f | 25 | #include <linux/sysctl.h> |
5072c59f | 26 | #include <linux/ctype.h> |
2d8b820b | 27 | #include <linux/hash.h> |
3d083395 SR |
28 | #include <linux/list.h> |
29 | ||
30 | #include "trace.h" | |
16444a8a | 31 | |
4eebcc81 SR |
32 | /* ftrace_enabled is a method to turn ftrace on or off */ |
33 | int ftrace_enabled __read_mostly; | |
d61f82d0 | 34 | static int last_ftrace_enabled; |
b0fc494f | 35 | |
4eebcc81 SR |
36 | /* |
37 | * ftrace_disabled is set when an anomaly is discovered. | |
38 | * ftrace_disabled is much stronger than ftrace_enabled. | |
39 | */ | |
40 | static int ftrace_disabled __read_mostly; | |
41 | ||
3d083395 | 42 | static DEFINE_SPINLOCK(ftrace_lock); |
b0fc494f SR |
43 | static DEFINE_MUTEX(ftrace_sysctl_lock); |
44 | ||
16444a8a ACM |
45 | static struct ftrace_ops ftrace_list_end __read_mostly = |
46 | { | |
47 | .func = ftrace_stub, | |
48 | }; | |
49 | ||
50 | static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end; | |
51 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; | |
52 | ||
e309b41d | 53 | void ftrace_list_func(unsigned long ip, unsigned long parent_ip) |
16444a8a ACM |
54 | { |
55 | struct ftrace_ops *op = ftrace_list; | |
56 | ||
57 | /* in case someone actually ports this to alpha! */ | |
58 | read_barrier_depends(); | |
59 | ||
60 | while (op != &ftrace_list_end) { | |
61 | /* silly alpha */ | |
62 | read_barrier_depends(); | |
63 | op->func(ip, parent_ip); | |
64 | op = op->next; | |
65 | }; | |
66 | } | |
67 | ||
68 | /** | |
3d083395 | 69 | * clear_ftrace_function - reset the ftrace function |
16444a8a | 70 | * |
3d083395 SR |
71 | * This NULLs the ftrace function and in essence stops |
72 | * tracing. There may be lag | |
16444a8a | 73 | */ |
3d083395 | 74 | void clear_ftrace_function(void) |
16444a8a | 75 | { |
3d083395 SR |
76 | ftrace_trace_function = ftrace_stub; |
77 | } | |
78 | ||
e309b41d | 79 | static int __register_ftrace_function(struct ftrace_ops *ops) |
3d083395 SR |
80 | { |
81 | /* Should never be called by interrupts */ | |
82 | spin_lock(&ftrace_lock); | |
16444a8a | 83 | |
16444a8a ACM |
84 | ops->next = ftrace_list; |
85 | /* | |
86 | * We are entering ops into the ftrace_list but another | |
87 | * CPU might be walking that list. We need to make sure | |
88 | * the ops->next pointer is valid before another CPU sees | |
89 | * the ops pointer included into the ftrace_list. | |
90 | */ | |
91 | smp_wmb(); | |
92 | ftrace_list = ops; | |
3d083395 | 93 | |
b0fc494f SR |
94 | if (ftrace_enabled) { |
95 | /* | |
96 | * For one func, simply call it directly. | |
97 | * For more than one func, call the chain. | |
98 | */ | |
99 | if (ops->next == &ftrace_list_end) | |
100 | ftrace_trace_function = ops->func; | |
101 | else | |
102 | ftrace_trace_function = ftrace_list_func; | |
103 | } | |
3d083395 SR |
104 | |
105 | spin_unlock(&ftrace_lock); | |
16444a8a ACM |
106 | |
107 | return 0; | |
108 | } | |
109 | ||
e309b41d | 110 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 111 | { |
16444a8a ACM |
112 | struct ftrace_ops **p; |
113 | int ret = 0; | |
114 | ||
3d083395 | 115 | spin_lock(&ftrace_lock); |
16444a8a ACM |
116 | |
117 | /* | |
3d083395 SR |
118 | * If we are removing the last function, then simply point |
119 | * to the ftrace_stub. | |
16444a8a ACM |
120 | */ |
121 | if (ftrace_list == ops && ops->next == &ftrace_list_end) { | |
122 | ftrace_trace_function = ftrace_stub; | |
123 | ftrace_list = &ftrace_list_end; | |
124 | goto out; | |
125 | } | |
126 | ||
127 | for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next) | |
128 | if (*p == ops) | |
129 | break; | |
130 | ||
131 | if (*p != ops) { | |
132 | ret = -1; | |
133 | goto out; | |
134 | } | |
135 | ||
136 | *p = (*p)->next; | |
137 | ||
b0fc494f SR |
138 | if (ftrace_enabled) { |
139 | /* If we only have one func left, then call that directly */ | |
140 | if (ftrace_list == &ftrace_list_end || | |
141 | ftrace_list->next == &ftrace_list_end) | |
142 | ftrace_trace_function = ftrace_list->func; | |
143 | } | |
16444a8a ACM |
144 | |
145 | out: | |
3d083395 SR |
146 | spin_unlock(&ftrace_lock); |
147 | ||
148 | return ret; | |
149 | } | |
150 | ||
151 | #ifdef CONFIG_DYNAMIC_FTRACE | |
152 | ||
e1c08bdd SR |
153 | static struct task_struct *ftraced_task; |
154 | static DECLARE_WAIT_QUEUE_HEAD(ftraced_waiters); | |
155 | static unsigned long ftraced_iteration_counter; | |
156 | ||
d61f82d0 SR |
157 | enum { |
158 | FTRACE_ENABLE_CALLS = (1 << 0), | |
159 | FTRACE_DISABLE_CALLS = (1 << 1), | |
160 | FTRACE_UPDATE_TRACE_FUNC = (1 << 2), | |
161 | FTRACE_ENABLE_MCOUNT = (1 << 3), | |
162 | FTRACE_DISABLE_MCOUNT = (1 << 4), | |
163 | }; | |
164 | ||
5072c59f SR |
165 | static int ftrace_filtered; |
166 | ||
3d083395 SR |
167 | static struct hlist_head ftrace_hash[FTRACE_HASHSIZE]; |
168 | ||
169 | static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu); | |
170 | ||
171 | static DEFINE_SPINLOCK(ftrace_shutdown_lock); | |
172 | static DEFINE_MUTEX(ftraced_lock); | |
41c52c0d | 173 | static DEFINE_MUTEX(ftrace_regex_lock); |
3d083395 | 174 | |
3c1720f0 SR |
175 | struct ftrace_page { |
176 | struct ftrace_page *next; | |
aa5e5cea | 177 | unsigned long index; |
3c1720f0 | 178 | struct dyn_ftrace records[]; |
aa5e5cea | 179 | }; |
3c1720f0 SR |
180 | |
181 | #define ENTRIES_PER_PAGE \ | |
182 | ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace)) | |
183 | ||
184 | /* estimate from running different kernels */ | |
185 | #define NR_TO_INIT 10000 | |
186 | ||
187 | static struct ftrace_page *ftrace_pages_start; | |
188 | static struct ftrace_page *ftrace_pages; | |
189 | ||
3d083395 SR |
190 | static int ftraced_trigger; |
191 | static int ftraced_suspend; | |
192 | ||
193 | static int ftrace_record_suspend; | |
194 | ||
37ad5084 SR |
195 | static struct dyn_ftrace *ftrace_free_records; |
196 | ||
e309b41d | 197 | static inline int |
9ff9cdb2 | 198 | ftrace_ip_in_hash(unsigned long ip, unsigned long key) |
3d083395 SR |
199 | { |
200 | struct dyn_ftrace *p; | |
201 | struct hlist_node *t; | |
202 | int found = 0; | |
203 | ||
ffdaa358 | 204 | hlist_for_each_entry_rcu(p, t, &ftrace_hash[key], node) { |
3d083395 SR |
205 | if (p->ip == ip) { |
206 | found = 1; | |
207 | break; | |
208 | } | |
209 | } | |
210 | ||
211 | return found; | |
212 | } | |
213 | ||
e309b41d | 214 | static inline void |
3d083395 SR |
215 | ftrace_add_hash(struct dyn_ftrace *node, unsigned long key) |
216 | { | |
ffdaa358 | 217 | hlist_add_head_rcu(&node->node, &ftrace_hash[key]); |
3d083395 SR |
218 | } |
219 | ||
e309b41d | 220 | static void ftrace_free_rec(struct dyn_ftrace *rec) |
37ad5084 SR |
221 | { |
222 | /* no locking, only called from kstop_machine */ | |
223 | ||
224 | rec->ip = (unsigned long)ftrace_free_records; | |
225 | ftrace_free_records = rec; | |
226 | rec->flags |= FTRACE_FL_FREE; | |
227 | } | |
228 | ||
e309b41d | 229 | static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip) |
3c1720f0 | 230 | { |
37ad5084 SR |
231 | struct dyn_ftrace *rec; |
232 | ||
233 | /* First check for freed records */ | |
234 | if (ftrace_free_records) { | |
235 | rec = ftrace_free_records; | |
236 | ||
37ad5084 SR |
237 | if (unlikely(!(rec->flags & FTRACE_FL_FREE))) { |
238 | WARN_ON_ONCE(1); | |
239 | ftrace_free_records = NULL; | |
4eebcc81 SR |
240 | ftrace_disabled = 1; |
241 | ftrace_enabled = 0; | |
37ad5084 SR |
242 | return NULL; |
243 | } | |
244 | ||
245 | ftrace_free_records = (void *)rec->ip; | |
246 | memset(rec, 0, sizeof(*rec)); | |
247 | return rec; | |
248 | } | |
249 | ||
3c1720f0 SR |
250 | if (ftrace_pages->index == ENTRIES_PER_PAGE) { |
251 | if (!ftrace_pages->next) | |
252 | return NULL; | |
253 | ftrace_pages = ftrace_pages->next; | |
254 | } | |
255 | ||
256 | return &ftrace_pages->records[ftrace_pages->index++]; | |
257 | } | |
258 | ||
e309b41d | 259 | static void |
d61f82d0 | 260 | ftrace_record_ip(unsigned long ip) |
3d083395 SR |
261 | { |
262 | struct dyn_ftrace *node; | |
263 | unsigned long flags; | |
264 | unsigned long key; | |
265 | int resched; | |
266 | int atomic; | |
2bb6f8d6 | 267 | int cpu; |
3d083395 | 268 | |
4eebcc81 | 269 | if (!ftrace_enabled || ftrace_disabled) |
d61f82d0 SR |
270 | return; |
271 | ||
3d083395 SR |
272 | resched = need_resched(); |
273 | preempt_disable_notrace(); | |
274 | ||
2bb6f8d6 SR |
275 | /* |
276 | * We simply need to protect against recursion. | |
277 | * Use the the raw version of smp_processor_id and not | |
278 | * __get_cpu_var which can call debug hooks that can | |
279 | * cause a recursive crash here. | |
280 | */ | |
281 | cpu = raw_smp_processor_id(); | |
282 | per_cpu(ftrace_shutdown_disable_cpu, cpu)++; | |
283 | if (per_cpu(ftrace_shutdown_disable_cpu, cpu) != 1) | |
3d083395 SR |
284 | goto out; |
285 | ||
286 | if (unlikely(ftrace_record_suspend)) | |
287 | goto out; | |
288 | ||
289 | key = hash_long(ip, FTRACE_HASHBITS); | |
290 | ||
291 | WARN_ON_ONCE(key >= FTRACE_HASHSIZE); | |
292 | ||
293 | if (ftrace_ip_in_hash(ip, key)) | |
294 | goto out; | |
295 | ||
296 | atomic = irqs_disabled(); | |
297 | ||
298 | spin_lock_irqsave(&ftrace_shutdown_lock, flags); | |
299 | ||
300 | /* This ip may have hit the hash before the lock */ | |
301 | if (ftrace_ip_in_hash(ip, key)) | |
302 | goto out_unlock; | |
303 | ||
304 | /* | |
305 | * There's a slight race that the ftraced will update the | |
d61f82d0 | 306 | * hash and reset here. If it is already converted, skip it. |
3d083395 | 307 | */ |
d61f82d0 SR |
308 | if (ftrace_ip_converted(ip)) |
309 | goto out_unlock; | |
310 | ||
311 | node = ftrace_alloc_dyn_node(ip); | |
3d083395 SR |
312 | if (!node) |
313 | goto out_unlock; | |
314 | ||
315 | node->ip = ip; | |
316 | ||
317 | ftrace_add_hash(node, key); | |
318 | ||
319 | ftraced_trigger = 1; | |
320 | ||
321 | out_unlock: | |
322 | spin_unlock_irqrestore(&ftrace_shutdown_lock, flags); | |
323 | out: | |
2bb6f8d6 | 324 | per_cpu(ftrace_shutdown_disable_cpu, cpu)--; |
3d083395 SR |
325 | |
326 | /* prevent recursion with scheduler */ | |
327 | if (resched) | |
328 | preempt_enable_no_resched_notrace(); | |
329 | else | |
330 | preempt_enable_notrace(); | |
331 | } | |
332 | ||
caf8cdeb SR |
333 | #define FTRACE_ADDR ((long)(ftrace_caller)) |
334 | #define MCOUNT_ADDR ((long)(mcount)) | |
3c1720f0 | 335 | |
e309b41d | 336 | static void |
5072c59f SR |
337 | __ftrace_replace_code(struct dyn_ftrace *rec, |
338 | unsigned char *old, unsigned char *new, int enable) | |
339 | { | |
41c52c0d | 340 | unsigned long ip, fl; |
5072c59f SR |
341 | int failed; |
342 | ||
343 | ip = rec->ip; | |
344 | ||
345 | if (ftrace_filtered && enable) { | |
5072c59f SR |
346 | /* |
347 | * If filtering is on: | |
348 | * | |
349 | * If this record is set to be filtered and | |
350 | * is enabled then do nothing. | |
351 | * | |
352 | * If this record is set to be filtered and | |
353 | * it is not enabled, enable it. | |
354 | * | |
355 | * If this record is not set to be filtered | |
356 | * and it is not enabled do nothing. | |
357 | * | |
41c52c0d SR |
358 | * If this record is set not to trace then |
359 | * do nothing. | |
360 | * | |
5072c59f SR |
361 | * If this record is not set to be filtered and |
362 | * it is enabled, disable it. | |
363 | */ | |
364 | fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED); | |
365 | ||
366 | if ((fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) || | |
41c52c0d | 367 | (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE)) |
5072c59f SR |
368 | return; |
369 | ||
370 | /* | |
371 | * If it is enabled disable it, | |
372 | * otherwise enable it! | |
373 | */ | |
374 | if (fl == FTRACE_FL_ENABLED) { | |
375 | /* swap new and old */ | |
376 | new = old; | |
377 | old = ftrace_call_replace(ip, FTRACE_ADDR); | |
378 | rec->flags &= ~FTRACE_FL_ENABLED; | |
379 | } else { | |
380 | new = ftrace_call_replace(ip, FTRACE_ADDR); | |
381 | rec->flags |= FTRACE_FL_ENABLED; | |
382 | } | |
383 | } else { | |
384 | ||
41c52c0d SR |
385 | if (enable) { |
386 | /* | |
387 | * If this record is set not to trace and is | |
388 | * not enabled, do nothing. | |
389 | */ | |
390 | fl = rec->flags & (FTRACE_FL_NOTRACE | FTRACE_FL_ENABLED); | |
391 | if (fl == FTRACE_FL_NOTRACE) | |
392 | return; | |
393 | ||
5072c59f | 394 | new = ftrace_call_replace(ip, FTRACE_ADDR); |
41c52c0d | 395 | } else |
5072c59f SR |
396 | old = ftrace_call_replace(ip, FTRACE_ADDR); |
397 | ||
398 | if (enable) { | |
399 | if (rec->flags & FTRACE_FL_ENABLED) | |
400 | return; | |
401 | rec->flags |= FTRACE_FL_ENABLED; | |
402 | } else { | |
403 | if (!(rec->flags & FTRACE_FL_ENABLED)) | |
404 | return; | |
405 | rec->flags &= ~FTRACE_FL_ENABLED; | |
406 | } | |
407 | } | |
408 | ||
409 | failed = ftrace_modify_code(ip, old, new); | |
37ad5084 SR |
410 | if (failed) { |
411 | unsigned long key; | |
412 | /* It is possible that the function hasn't been converted yet */ | |
413 | key = hash_long(ip, FTRACE_HASHBITS); | |
414 | if (!ftrace_ip_in_hash(ip, key)) { | |
415 | rec->flags |= FTRACE_FL_FAILED; | |
416 | ftrace_free_rec(rec); | |
417 | } | |
418 | ||
419 | } | |
5072c59f SR |
420 | } |
421 | ||
e309b41d | 422 | static void ftrace_replace_code(int enable) |
3c1720f0 SR |
423 | { |
424 | unsigned char *new = NULL, *old = NULL; | |
425 | struct dyn_ftrace *rec; | |
426 | struct ftrace_page *pg; | |
3c1720f0 SR |
427 | int i; |
428 | ||
5072c59f | 429 | if (enable) |
3c1720f0 SR |
430 | old = ftrace_nop_replace(); |
431 | else | |
432 | new = ftrace_nop_replace(); | |
433 | ||
434 | for (pg = ftrace_pages_start; pg; pg = pg->next) { | |
435 | for (i = 0; i < pg->index; i++) { | |
436 | rec = &pg->records[i]; | |
437 | ||
438 | /* don't modify code that has already faulted */ | |
439 | if (rec->flags & FTRACE_FL_FAILED) | |
440 | continue; | |
441 | ||
5072c59f | 442 | __ftrace_replace_code(rec, old, new, enable); |
3c1720f0 SR |
443 | } |
444 | } | |
445 | } | |
446 | ||
e309b41d | 447 | static void ftrace_shutdown_replenish(void) |
3c1720f0 SR |
448 | { |
449 | if (ftrace_pages->next) | |
450 | return; | |
451 | ||
452 | /* allocate another page */ | |
453 | ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL); | |
454 | } | |
3d083395 | 455 | |
492a7ea5 | 456 | static int |
d61f82d0 | 457 | ftrace_code_disable(struct dyn_ftrace *rec) |
3c1720f0 SR |
458 | { |
459 | unsigned long ip; | |
460 | unsigned char *nop, *call; | |
461 | int failed; | |
462 | ||
463 | ip = rec->ip; | |
464 | ||
465 | nop = ftrace_nop_replace(); | |
d61f82d0 | 466 | call = ftrace_call_replace(ip, MCOUNT_ADDR); |
3c1720f0 SR |
467 | |
468 | failed = ftrace_modify_code(ip, call, nop); | |
37ad5084 | 469 | if (failed) { |
3c1720f0 | 470 | rec->flags |= FTRACE_FL_FAILED; |
37ad5084 | 471 | ftrace_free_rec(rec); |
492a7ea5 | 472 | return 0; |
37ad5084 | 473 | } |
492a7ea5 | 474 | return 1; |
3c1720f0 SR |
475 | } |
476 | ||
e309b41d | 477 | static int __ftrace_modify_code(void *data) |
3d083395 | 478 | { |
d61f82d0 SR |
479 | unsigned long addr; |
480 | int *command = data; | |
481 | ||
482 | if (*command & FTRACE_ENABLE_CALLS) | |
483 | ftrace_replace_code(1); | |
484 | else if (*command & FTRACE_DISABLE_CALLS) | |
485 | ftrace_replace_code(0); | |
486 | ||
487 | if (*command & FTRACE_UPDATE_TRACE_FUNC) | |
488 | ftrace_update_ftrace_func(ftrace_trace_function); | |
489 | ||
490 | if (*command & FTRACE_ENABLE_MCOUNT) { | |
491 | addr = (unsigned long)ftrace_record_ip; | |
492 | ftrace_mcount_set(&addr); | |
493 | } else if (*command & FTRACE_DISABLE_MCOUNT) { | |
494 | addr = (unsigned long)ftrace_stub; | |
495 | ftrace_mcount_set(&addr); | |
496 | } | |
497 | ||
498 | return 0; | |
3d083395 SR |
499 | } |
500 | ||
e309b41d | 501 | static void ftrace_run_update_code(int command) |
3d083395 | 502 | { |
d61f82d0 | 503 | stop_machine_run(__ftrace_modify_code, &command, NR_CPUS); |
3d083395 SR |
504 | } |
505 | ||
d61f82d0 SR |
506 | static ftrace_func_t saved_ftrace_func; |
507 | ||
e309b41d | 508 | static void ftrace_startup(void) |
3d083395 | 509 | { |
d61f82d0 SR |
510 | int command = 0; |
511 | ||
4eebcc81 SR |
512 | if (unlikely(ftrace_disabled)) |
513 | return; | |
514 | ||
3d083395 SR |
515 | mutex_lock(&ftraced_lock); |
516 | ftraced_suspend++; | |
d61f82d0 SR |
517 | if (ftraced_suspend == 1) |
518 | command |= FTRACE_ENABLE_CALLS; | |
519 | ||
520 | if (saved_ftrace_func != ftrace_trace_function) { | |
521 | saved_ftrace_func = ftrace_trace_function; | |
522 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
523 | } | |
524 | ||
525 | if (!command || !ftrace_enabled) | |
3d083395 | 526 | goto out; |
3d083395 | 527 | |
d61f82d0 | 528 | ftrace_run_update_code(command); |
3d083395 SR |
529 | out: |
530 | mutex_unlock(&ftraced_lock); | |
531 | } | |
532 | ||
e309b41d | 533 | static void ftrace_shutdown(void) |
3d083395 | 534 | { |
d61f82d0 SR |
535 | int command = 0; |
536 | ||
4eebcc81 SR |
537 | if (unlikely(ftrace_disabled)) |
538 | return; | |
539 | ||
3d083395 SR |
540 | mutex_lock(&ftraced_lock); |
541 | ftraced_suspend--; | |
d61f82d0 SR |
542 | if (!ftraced_suspend) |
543 | command |= FTRACE_DISABLE_CALLS; | |
3d083395 | 544 | |
d61f82d0 SR |
545 | if (saved_ftrace_func != ftrace_trace_function) { |
546 | saved_ftrace_func = ftrace_trace_function; | |
547 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
548 | } | |
3d083395 | 549 | |
d61f82d0 SR |
550 | if (!command || !ftrace_enabled) |
551 | goto out; | |
552 | ||
553 | ftrace_run_update_code(command); | |
3d083395 SR |
554 | out: |
555 | mutex_unlock(&ftraced_lock); | |
556 | } | |
557 | ||
e309b41d | 558 | static void ftrace_startup_sysctl(void) |
b0fc494f | 559 | { |
d61f82d0 SR |
560 | int command = FTRACE_ENABLE_MCOUNT; |
561 | ||
4eebcc81 SR |
562 | if (unlikely(ftrace_disabled)) |
563 | return; | |
564 | ||
b0fc494f | 565 | mutex_lock(&ftraced_lock); |
d61f82d0 SR |
566 | /* Force update next time */ |
567 | saved_ftrace_func = NULL; | |
b0fc494f SR |
568 | /* ftraced_suspend is true if we want ftrace running */ |
569 | if (ftraced_suspend) | |
d61f82d0 SR |
570 | command |= FTRACE_ENABLE_CALLS; |
571 | ||
572 | ftrace_run_update_code(command); | |
b0fc494f SR |
573 | mutex_unlock(&ftraced_lock); |
574 | } | |
575 | ||
e309b41d | 576 | static void ftrace_shutdown_sysctl(void) |
b0fc494f | 577 | { |
d61f82d0 SR |
578 | int command = FTRACE_DISABLE_MCOUNT; |
579 | ||
4eebcc81 SR |
580 | if (unlikely(ftrace_disabled)) |
581 | return; | |
582 | ||
b0fc494f SR |
583 | mutex_lock(&ftraced_lock); |
584 | /* ftraced_suspend is true if ftrace is running */ | |
585 | if (ftraced_suspend) | |
d61f82d0 SR |
586 | command |= FTRACE_DISABLE_CALLS; |
587 | ||
588 | ftrace_run_update_code(command); | |
b0fc494f SR |
589 | mutex_unlock(&ftraced_lock); |
590 | } | |
591 | ||
3d083395 SR |
592 | static cycle_t ftrace_update_time; |
593 | static unsigned long ftrace_update_cnt; | |
594 | unsigned long ftrace_update_tot_cnt; | |
595 | ||
e309b41d | 596 | static int __ftrace_update_code(void *ignore) |
3d083395 SR |
597 | { |
598 | struct dyn_ftrace *p; | |
599 | struct hlist_head head; | |
600 | struct hlist_node *t; | |
d61f82d0 | 601 | int save_ftrace_enabled; |
3d083395 SR |
602 | cycle_t start, stop; |
603 | int i; | |
604 | ||
d61f82d0 SR |
605 | /* Don't be recording funcs now */ |
606 | save_ftrace_enabled = ftrace_enabled; | |
607 | ftrace_enabled = 0; | |
3d083395 | 608 | |
750ed1a4 | 609 | start = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
610 | ftrace_update_cnt = 0; |
611 | ||
612 | /* No locks needed, the machine is stopped! */ | |
613 | for (i = 0; i < FTRACE_HASHSIZE; i++) { | |
614 | if (hlist_empty(&ftrace_hash[i])) | |
615 | continue; | |
616 | ||
617 | head = ftrace_hash[i]; | |
618 | INIT_HLIST_HEAD(&ftrace_hash[i]); | |
619 | ||
620 | /* all CPUS are stopped, we are safe to modify code */ | |
621 | hlist_for_each_entry(p, t, &head, node) { | |
492a7ea5 AS |
622 | if (ftrace_code_disable(p)) |
623 | ftrace_update_cnt++; | |
3d083395 SR |
624 | } |
625 | ||
626 | } | |
627 | ||
750ed1a4 | 628 | stop = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
629 | ftrace_update_time = stop - start; |
630 | ftrace_update_tot_cnt += ftrace_update_cnt; | |
631 | ||
d61f82d0 | 632 | ftrace_enabled = save_ftrace_enabled; |
16444a8a ACM |
633 | |
634 | return 0; | |
635 | } | |
636 | ||
e309b41d | 637 | static void ftrace_update_code(void) |
3d083395 | 638 | { |
4eebcc81 SR |
639 | if (unlikely(ftrace_disabled)) |
640 | return; | |
641 | ||
3d083395 SR |
642 | stop_machine_run(__ftrace_update_code, NULL, NR_CPUS); |
643 | } | |
644 | ||
e309b41d | 645 | static int ftraced(void *ignore) |
3d083395 SR |
646 | { |
647 | unsigned long usecs; | |
648 | ||
3d083395 SR |
649 | while (!kthread_should_stop()) { |
650 | ||
07a267cd SR |
651 | set_current_state(TASK_INTERRUPTIBLE); |
652 | ||
3d083395 SR |
653 | /* check once a second */ |
654 | schedule_timeout(HZ); | |
655 | ||
4eebcc81 SR |
656 | if (unlikely(ftrace_disabled)) |
657 | continue; | |
658 | ||
b0fc494f | 659 | mutex_lock(&ftrace_sysctl_lock); |
3d083395 | 660 | mutex_lock(&ftraced_lock); |
b0fc494f | 661 | if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) { |
3d083395 SR |
662 | ftrace_record_suspend++; |
663 | ftrace_update_code(); | |
664 | usecs = nsecs_to_usecs(ftrace_update_time); | |
665 | if (ftrace_update_tot_cnt > 100000) { | |
666 | ftrace_update_tot_cnt = 0; | |
667 | pr_info("hm, dftrace overflow: %lu change%s" | |
668 | " (%lu total) in %lu usec%s\n", | |
669 | ftrace_update_cnt, | |
670 | ftrace_update_cnt != 1 ? "s" : "", | |
671 | ftrace_update_tot_cnt, | |
672 | usecs, usecs != 1 ? "s" : ""); | |
4eebcc81 | 673 | ftrace_disabled = 1; |
3d083395 SR |
674 | WARN_ON_ONCE(1); |
675 | } | |
676 | ftraced_trigger = 0; | |
677 | ftrace_record_suspend--; | |
678 | } | |
e1c08bdd | 679 | ftraced_iteration_counter++; |
3d083395 | 680 | mutex_unlock(&ftraced_lock); |
b0fc494f | 681 | mutex_unlock(&ftrace_sysctl_lock); |
3d083395 | 682 | |
e1c08bdd SR |
683 | wake_up_interruptible(&ftraced_waiters); |
684 | ||
3d083395 | 685 | ftrace_shutdown_replenish(); |
3d083395 SR |
686 | } |
687 | __set_current_state(TASK_RUNNING); | |
688 | return 0; | |
689 | } | |
690 | ||
3c1720f0 SR |
691 | static int __init ftrace_dyn_table_alloc(void) |
692 | { | |
693 | struct ftrace_page *pg; | |
694 | int cnt; | |
695 | int i; | |
3c1720f0 SR |
696 | |
697 | /* allocate a few pages */ | |
698 | ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL); | |
699 | if (!ftrace_pages_start) | |
700 | return -1; | |
701 | ||
702 | /* | |
703 | * Allocate a few more pages. | |
704 | * | |
705 | * TODO: have some parser search vmlinux before | |
706 | * final linking to find all calls to ftrace. | |
707 | * Then we can: | |
708 | * a) know how many pages to allocate. | |
709 | * and/or | |
710 | * b) set up the table then. | |
711 | * | |
712 | * The dynamic code is still necessary for | |
713 | * modules. | |
714 | */ | |
715 | ||
716 | pg = ftrace_pages = ftrace_pages_start; | |
717 | ||
718 | cnt = NR_TO_INIT / ENTRIES_PER_PAGE; | |
719 | ||
720 | for (i = 0; i < cnt; i++) { | |
721 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); | |
722 | ||
723 | /* If we fail, we'll try later anyway */ | |
724 | if (!pg->next) | |
725 | break; | |
726 | ||
727 | pg = pg->next; | |
728 | } | |
729 | ||
730 | return 0; | |
731 | } | |
732 | ||
5072c59f SR |
733 | enum { |
734 | FTRACE_ITER_FILTER = (1 << 0), | |
735 | FTRACE_ITER_CONT = (1 << 1), | |
41c52c0d | 736 | FTRACE_ITER_NOTRACE = (1 << 2), |
5072c59f SR |
737 | }; |
738 | ||
739 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ | |
740 | ||
741 | struct ftrace_iterator { | |
742 | loff_t pos; | |
743 | struct ftrace_page *pg; | |
744 | unsigned idx; | |
745 | unsigned flags; | |
746 | unsigned char buffer[FTRACE_BUFF_MAX+1]; | |
747 | unsigned buffer_idx; | |
748 | unsigned filtered; | |
749 | }; | |
750 | ||
e309b41d | 751 | static void * |
5072c59f SR |
752 | t_next(struct seq_file *m, void *v, loff_t *pos) |
753 | { | |
754 | struct ftrace_iterator *iter = m->private; | |
755 | struct dyn_ftrace *rec = NULL; | |
756 | ||
757 | (*pos)++; | |
758 | ||
759 | retry: | |
760 | if (iter->idx >= iter->pg->index) { | |
761 | if (iter->pg->next) { | |
762 | iter->pg = iter->pg->next; | |
763 | iter->idx = 0; | |
764 | goto retry; | |
765 | } | |
766 | } else { | |
767 | rec = &iter->pg->records[iter->idx++]; | |
768 | if ((rec->flags & FTRACE_FL_FAILED) || | |
769 | ((iter->flags & FTRACE_ITER_FILTER) && | |
41c52c0d SR |
770 | !(rec->flags & FTRACE_FL_FILTER)) || |
771 | ((iter->flags & FTRACE_ITER_NOTRACE) && | |
772 | !(rec->flags & FTRACE_FL_NOTRACE))) { | |
5072c59f SR |
773 | rec = NULL; |
774 | goto retry; | |
775 | } | |
776 | } | |
777 | ||
778 | iter->pos = *pos; | |
779 | ||
780 | return rec; | |
781 | } | |
782 | ||
783 | static void *t_start(struct seq_file *m, loff_t *pos) | |
784 | { | |
785 | struct ftrace_iterator *iter = m->private; | |
786 | void *p = NULL; | |
787 | loff_t l = -1; | |
788 | ||
789 | if (*pos != iter->pos) { | |
790 | for (p = t_next(m, p, &l); p && l < *pos; p = t_next(m, p, &l)) | |
791 | ; | |
792 | } else { | |
793 | l = *pos; | |
794 | p = t_next(m, p, &l); | |
795 | } | |
796 | ||
797 | return p; | |
798 | } | |
799 | ||
800 | static void t_stop(struct seq_file *m, void *p) | |
801 | { | |
802 | } | |
803 | ||
804 | static int t_show(struct seq_file *m, void *v) | |
805 | { | |
806 | struct dyn_ftrace *rec = v; | |
807 | char str[KSYM_SYMBOL_LEN]; | |
808 | ||
809 | if (!rec) | |
810 | return 0; | |
811 | ||
812 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
813 | ||
814 | seq_printf(m, "%s\n", str); | |
815 | ||
816 | return 0; | |
817 | } | |
818 | ||
819 | static struct seq_operations show_ftrace_seq_ops = { | |
820 | .start = t_start, | |
821 | .next = t_next, | |
822 | .stop = t_stop, | |
823 | .show = t_show, | |
824 | }; | |
825 | ||
e309b41d | 826 | static int |
5072c59f SR |
827 | ftrace_avail_open(struct inode *inode, struct file *file) |
828 | { | |
829 | struct ftrace_iterator *iter; | |
830 | int ret; | |
831 | ||
4eebcc81 SR |
832 | if (unlikely(ftrace_disabled)) |
833 | return -ENODEV; | |
834 | ||
5072c59f SR |
835 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
836 | if (!iter) | |
837 | return -ENOMEM; | |
838 | ||
839 | iter->pg = ftrace_pages_start; | |
840 | iter->pos = -1; | |
841 | ||
842 | ret = seq_open(file, &show_ftrace_seq_ops); | |
843 | if (!ret) { | |
844 | struct seq_file *m = file->private_data; | |
4bf39a94 | 845 | |
5072c59f | 846 | m->private = iter; |
4bf39a94 | 847 | } else { |
5072c59f | 848 | kfree(iter); |
4bf39a94 | 849 | } |
5072c59f SR |
850 | |
851 | return ret; | |
852 | } | |
853 | ||
854 | int ftrace_avail_release(struct inode *inode, struct file *file) | |
855 | { | |
856 | struct seq_file *m = (struct seq_file *)file->private_data; | |
857 | struct ftrace_iterator *iter = m->private; | |
858 | ||
859 | seq_release(inode, file); | |
860 | kfree(iter); | |
4bf39a94 | 861 | |
5072c59f SR |
862 | return 0; |
863 | } | |
864 | ||
41c52c0d | 865 | static void ftrace_filter_reset(int enable) |
5072c59f SR |
866 | { |
867 | struct ftrace_page *pg; | |
868 | struct dyn_ftrace *rec; | |
41c52c0d | 869 | unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
5072c59f SR |
870 | unsigned i; |
871 | ||
872 | /* keep kstop machine from running */ | |
873 | preempt_disable(); | |
41c52c0d SR |
874 | if (enable) |
875 | ftrace_filtered = 0; | |
5072c59f SR |
876 | pg = ftrace_pages_start; |
877 | while (pg) { | |
878 | for (i = 0; i < pg->index; i++) { | |
879 | rec = &pg->records[i]; | |
880 | if (rec->flags & FTRACE_FL_FAILED) | |
881 | continue; | |
41c52c0d | 882 | rec->flags &= ~type; |
5072c59f SR |
883 | } |
884 | pg = pg->next; | |
885 | } | |
886 | preempt_enable(); | |
887 | } | |
888 | ||
e309b41d | 889 | static int |
41c52c0d | 890 | ftrace_regex_open(struct inode *inode, struct file *file, int enable) |
5072c59f SR |
891 | { |
892 | struct ftrace_iterator *iter; | |
893 | int ret = 0; | |
894 | ||
4eebcc81 SR |
895 | if (unlikely(ftrace_disabled)) |
896 | return -ENODEV; | |
897 | ||
5072c59f SR |
898 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
899 | if (!iter) | |
900 | return -ENOMEM; | |
901 | ||
41c52c0d | 902 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
903 | if ((file->f_mode & FMODE_WRITE) && |
904 | !(file->f_flags & O_APPEND)) | |
41c52c0d | 905 | ftrace_filter_reset(enable); |
5072c59f SR |
906 | |
907 | if (file->f_mode & FMODE_READ) { | |
908 | iter->pg = ftrace_pages_start; | |
909 | iter->pos = -1; | |
41c52c0d SR |
910 | iter->flags = enable ? FTRACE_ITER_FILTER : |
911 | FTRACE_ITER_NOTRACE; | |
5072c59f SR |
912 | |
913 | ret = seq_open(file, &show_ftrace_seq_ops); | |
914 | if (!ret) { | |
915 | struct seq_file *m = file->private_data; | |
916 | m->private = iter; | |
917 | } else | |
918 | kfree(iter); | |
919 | } else | |
920 | file->private_data = iter; | |
41c52c0d | 921 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
922 | |
923 | return ret; | |
924 | } | |
925 | ||
41c52c0d SR |
926 | static int |
927 | ftrace_filter_open(struct inode *inode, struct file *file) | |
928 | { | |
929 | return ftrace_regex_open(inode, file, 1); | |
930 | } | |
931 | ||
932 | static int | |
933 | ftrace_notrace_open(struct inode *inode, struct file *file) | |
934 | { | |
935 | return ftrace_regex_open(inode, file, 0); | |
936 | } | |
937 | ||
e309b41d | 938 | static ssize_t |
41c52c0d | 939 | ftrace_regex_read(struct file *file, char __user *ubuf, |
5072c59f SR |
940 | size_t cnt, loff_t *ppos) |
941 | { | |
942 | if (file->f_mode & FMODE_READ) | |
943 | return seq_read(file, ubuf, cnt, ppos); | |
944 | else | |
945 | return -EPERM; | |
946 | } | |
947 | ||
e309b41d | 948 | static loff_t |
41c52c0d | 949 | ftrace_regex_lseek(struct file *file, loff_t offset, int origin) |
5072c59f SR |
950 | { |
951 | loff_t ret; | |
952 | ||
953 | if (file->f_mode & FMODE_READ) | |
954 | ret = seq_lseek(file, offset, origin); | |
955 | else | |
956 | file->f_pos = ret = 1; | |
957 | ||
958 | return ret; | |
959 | } | |
960 | ||
961 | enum { | |
962 | MATCH_FULL, | |
963 | MATCH_FRONT_ONLY, | |
964 | MATCH_MIDDLE_ONLY, | |
965 | MATCH_END_ONLY, | |
966 | }; | |
967 | ||
e309b41d | 968 | static void |
41c52c0d | 969 | ftrace_match(unsigned char *buff, int len, int enable) |
5072c59f SR |
970 | { |
971 | char str[KSYM_SYMBOL_LEN]; | |
972 | char *search = NULL; | |
973 | struct ftrace_page *pg; | |
974 | struct dyn_ftrace *rec; | |
975 | int type = MATCH_FULL; | |
41c52c0d | 976 | unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
5072c59f SR |
977 | unsigned i, match = 0, search_len = 0; |
978 | ||
979 | for (i = 0; i < len; i++) { | |
980 | if (buff[i] == '*') { | |
981 | if (!i) { | |
982 | search = buff + i + 1; | |
983 | type = MATCH_END_ONLY; | |
984 | search_len = len - (i + 1); | |
985 | } else { | |
986 | if (type == MATCH_END_ONLY) { | |
987 | type = MATCH_MIDDLE_ONLY; | |
988 | } else { | |
989 | match = i; | |
990 | type = MATCH_FRONT_ONLY; | |
991 | } | |
992 | buff[i] = 0; | |
993 | break; | |
994 | } | |
995 | } | |
996 | } | |
997 | ||
998 | /* keep kstop machine from running */ | |
999 | preempt_disable(); | |
41c52c0d SR |
1000 | if (enable) |
1001 | ftrace_filtered = 1; | |
5072c59f SR |
1002 | pg = ftrace_pages_start; |
1003 | while (pg) { | |
1004 | for (i = 0; i < pg->index; i++) { | |
1005 | int matched = 0; | |
1006 | char *ptr; | |
1007 | ||
1008 | rec = &pg->records[i]; | |
1009 | if (rec->flags & FTRACE_FL_FAILED) | |
1010 | continue; | |
1011 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
1012 | switch (type) { | |
1013 | case MATCH_FULL: | |
1014 | if (strcmp(str, buff) == 0) | |
1015 | matched = 1; | |
1016 | break; | |
1017 | case MATCH_FRONT_ONLY: | |
1018 | if (memcmp(str, buff, match) == 0) | |
1019 | matched = 1; | |
1020 | break; | |
1021 | case MATCH_MIDDLE_ONLY: | |
1022 | if (strstr(str, search)) | |
1023 | matched = 1; | |
1024 | break; | |
1025 | case MATCH_END_ONLY: | |
1026 | ptr = strstr(str, search); | |
1027 | if (ptr && (ptr[search_len] == 0)) | |
1028 | matched = 1; | |
1029 | break; | |
1030 | } | |
1031 | if (matched) | |
41c52c0d | 1032 | rec->flags |= flag; |
5072c59f SR |
1033 | } |
1034 | pg = pg->next; | |
1035 | } | |
1036 | preempt_enable(); | |
1037 | } | |
1038 | ||
e309b41d | 1039 | static ssize_t |
41c52c0d SR |
1040 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
1041 | size_t cnt, loff_t *ppos, int enable) | |
5072c59f SR |
1042 | { |
1043 | struct ftrace_iterator *iter; | |
1044 | char ch; | |
1045 | size_t read = 0; | |
1046 | ssize_t ret; | |
1047 | ||
1048 | if (!cnt || cnt < 0) | |
1049 | return 0; | |
1050 | ||
41c52c0d | 1051 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
1052 | |
1053 | if (file->f_mode & FMODE_READ) { | |
1054 | struct seq_file *m = file->private_data; | |
1055 | iter = m->private; | |
1056 | } else | |
1057 | iter = file->private_data; | |
1058 | ||
1059 | if (!*ppos) { | |
1060 | iter->flags &= ~FTRACE_ITER_CONT; | |
1061 | iter->buffer_idx = 0; | |
1062 | } | |
1063 | ||
1064 | ret = get_user(ch, ubuf++); | |
1065 | if (ret) | |
1066 | goto out; | |
1067 | read++; | |
1068 | cnt--; | |
1069 | ||
1070 | if (!(iter->flags & ~FTRACE_ITER_CONT)) { | |
1071 | /* skip white space */ | |
1072 | while (cnt && isspace(ch)) { | |
1073 | ret = get_user(ch, ubuf++); | |
1074 | if (ret) | |
1075 | goto out; | |
1076 | read++; | |
1077 | cnt--; | |
1078 | } | |
1079 | ||
5072c59f SR |
1080 | if (isspace(ch)) { |
1081 | file->f_pos += read; | |
1082 | ret = read; | |
1083 | goto out; | |
1084 | } | |
1085 | ||
1086 | iter->buffer_idx = 0; | |
1087 | } | |
1088 | ||
1089 | while (cnt && !isspace(ch)) { | |
1090 | if (iter->buffer_idx < FTRACE_BUFF_MAX) | |
1091 | iter->buffer[iter->buffer_idx++] = ch; | |
1092 | else { | |
1093 | ret = -EINVAL; | |
1094 | goto out; | |
1095 | } | |
1096 | ret = get_user(ch, ubuf++); | |
1097 | if (ret) | |
1098 | goto out; | |
1099 | read++; | |
1100 | cnt--; | |
1101 | } | |
1102 | ||
1103 | if (isspace(ch)) { | |
1104 | iter->filtered++; | |
1105 | iter->buffer[iter->buffer_idx] = 0; | |
41c52c0d | 1106 | ftrace_match(iter->buffer, iter->buffer_idx, enable); |
5072c59f SR |
1107 | iter->buffer_idx = 0; |
1108 | } else | |
1109 | iter->flags |= FTRACE_ITER_CONT; | |
1110 | ||
1111 | ||
1112 | file->f_pos += read; | |
1113 | ||
1114 | ret = read; | |
1115 | out: | |
41c52c0d | 1116 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
1117 | |
1118 | return ret; | |
1119 | } | |
1120 | ||
41c52c0d SR |
1121 | static ssize_t |
1122 | ftrace_filter_write(struct file *file, const char __user *ubuf, | |
1123 | size_t cnt, loff_t *ppos) | |
1124 | { | |
1125 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); | |
1126 | } | |
1127 | ||
1128 | static ssize_t | |
1129 | ftrace_notrace_write(struct file *file, const char __user *ubuf, | |
1130 | size_t cnt, loff_t *ppos) | |
1131 | { | |
1132 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); | |
1133 | } | |
1134 | ||
1135 | static void | |
1136 | ftrace_set_regex(unsigned char *buf, int len, int reset, int enable) | |
1137 | { | |
1138 | if (unlikely(ftrace_disabled)) | |
1139 | return; | |
1140 | ||
1141 | mutex_lock(&ftrace_regex_lock); | |
1142 | if (reset) | |
1143 | ftrace_filter_reset(enable); | |
1144 | if (buf) | |
1145 | ftrace_match(buf, len, enable); | |
1146 | mutex_unlock(&ftrace_regex_lock); | |
1147 | } | |
1148 | ||
77a2b37d SR |
1149 | /** |
1150 | * ftrace_set_filter - set a function to filter on in ftrace | |
1151 | * @buf - the string that holds the function filter text. | |
1152 | * @len - the length of the string. | |
1153 | * @reset - non zero to reset all filters before applying this filter. | |
1154 | * | |
1155 | * Filters denote which functions should be enabled when tracing is enabled. | |
1156 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
1157 | */ | |
e309b41d | 1158 | void ftrace_set_filter(unsigned char *buf, int len, int reset) |
77a2b37d | 1159 | { |
41c52c0d SR |
1160 | ftrace_set_regex(buf, len, reset, 1); |
1161 | } | |
4eebcc81 | 1162 | |
41c52c0d SR |
1163 | /** |
1164 | * ftrace_set_notrace - set a function to not trace in ftrace | |
1165 | * @buf - the string that holds the function notrace text. | |
1166 | * @len - the length of the string. | |
1167 | * @reset - non zero to reset all filters before applying this filter. | |
1168 | * | |
1169 | * Notrace Filters denote which functions should not be enabled when tracing | |
1170 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
1171 | * for tracing. | |
1172 | */ | |
1173 | void ftrace_set_notrace(unsigned char *buf, int len, int reset) | |
1174 | { | |
1175 | ftrace_set_regex(buf, len, reset, 0); | |
77a2b37d SR |
1176 | } |
1177 | ||
e309b41d | 1178 | static int |
41c52c0d | 1179 | ftrace_regex_release(struct inode *inode, struct file *file, int enable) |
5072c59f SR |
1180 | { |
1181 | struct seq_file *m = (struct seq_file *)file->private_data; | |
1182 | struct ftrace_iterator *iter; | |
1183 | ||
41c52c0d | 1184 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
1185 | if (file->f_mode & FMODE_READ) { |
1186 | iter = m->private; | |
1187 | ||
1188 | seq_release(inode, file); | |
1189 | } else | |
1190 | iter = file->private_data; | |
1191 | ||
1192 | if (iter->buffer_idx) { | |
1193 | iter->filtered++; | |
1194 | iter->buffer[iter->buffer_idx] = 0; | |
41c52c0d | 1195 | ftrace_match(iter->buffer, iter->buffer_idx, enable); |
5072c59f SR |
1196 | } |
1197 | ||
1198 | mutex_lock(&ftrace_sysctl_lock); | |
1199 | mutex_lock(&ftraced_lock); | |
1200 | if (iter->filtered && ftraced_suspend && ftrace_enabled) | |
1201 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); | |
1202 | mutex_unlock(&ftraced_lock); | |
1203 | mutex_unlock(&ftrace_sysctl_lock); | |
1204 | ||
1205 | kfree(iter); | |
41c52c0d | 1206 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
1207 | return 0; |
1208 | } | |
1209 | ||
41c52c0d SR |
1210 | static int |
1211 | ftrace_filter_release(struct inode *inode, struct file *file) | |
1212 | { | |
1213 | return ftrace_regex_release(inode, file, 1); | |
1214 | } | |
1215 | ||
1216 | static int | |
1217 | ftrace_notrace_release(struct inode *inode, struct file *file) | |
1218 | { | |
1219 | return ftrace_regex_release(inode, file, 0); | |
1220 | } | |
1221 | ||
5072c59f SR |
1222 | static struct file_operations ftrace_avail_fops = { |
1223 | .open = ftrace_avail_open, | |
1224 | .read = seq_read, | |
1225 | .llseek = seq_lseek, | |
1226 | .release = ftrace_avail_release, | |
1227 | }; | |
1228 | ||
1229 | static struct file_operations ftrace_filter_fops = { | |
1230 | .open = ftrace_filter_open, | |
41c52c0d | 1231 | .read = ftrace_regex_read, |
5072c59f | 1232 | .write = ftrace_filter_write, |
41c52c0d | 1233 | .llseek = ftrace_regex_lseek, |
5072c59f SR |
1234 | .release = ftrace_filter_release, |
1235 | }; | |
1236 | ||
41c52c0d SR |
1237 | static struct file_operations ftrace_notrace_fops = { |
1238 | .open = ftrace_notrace_open, | |
1239 | .read = ftrace_regex_read, | |
1240 | .write = ftrace_notrace_write, | |
1241 | .llseek = ftrace_regex_lseek, | |
1242 | .release = ftrace_notrace_release, | |
1243 | }; | |
1244 | ||
e1c08bdd SR |
1245 | /** |
1246 | * ftrace_force_update - force an update to all recording ftrace functions | |
1247 | * | |
1248 | * The ftrace dynamic update daemon only wakes up once a second. | |
1249 | * There may be cases where an update needs to be done immediately | |
1250 | * for tests or internal kernel tracing to begin. This function | |
1251 | * wakes the daemon to do an update and will not return until the | |
1252 | * update is complete. | |
1253 | */ | |
1254 | int ftrace_force_update(void) | |
1255 | { | |
1256 | unsigned long last_counter; | |
1257 | DECLARE_WAITQUEUE(wait, current); | |
1258 | int ret = 0; | |
1259 | ||
4eebcc81 | 1260 | if (unlikely(ftrace_disabled)) |
e1c08bdd SR |
1261 | return -ENODEV; |
1262 | ||
1263 | mutex_lock(&ftraced_lock); | |
1264 | last_counter = ftraced_iteration_counter; | |
1265 | ||
1266 | set_current_state(TASK_INTERRUPTIBLE); | |
1267 | add_wait_queue(&ftraced_waiters, &wait); | |
1268 | ||
4eebcc81 SR |
1269 | if (unlikely(!ftraced_task)) { |
1270 | ret = -ENODEV; | |
1271 | goto out; | |
1272 | } | |
1273 | ||
e1c08bdd SR |
1274 | do { |
1275 | mutex_unlock(&ftraced_lock); | |
1276 | wake_up_process(ftraced_task); | |
1277 | schedule(); | |
1278 | mutex_lock(&ftraced_lock); | |
1279 | if (signal_pending(current)) { | |
1280 | ret = -EINTR; | |
1281 | break; | |
1282 | } | |
1283 | set_current_state(TASK_INTERRUPTIBLE); | |
1284 | } while (last_counter == ftraced_iteration_counter); | |
1285 | ||
4eebcc81 | 1286 | out: |
e1c08bdd SR |
1287 | mutex_unlock(&ftraced_lock); |
1288 | remove_wait_queue(&ftraced_waiters, &wait); | |
1289 | set_current_state(TASK_RUNNING); | |
1290 | ||
1291 | return ret; | |
1292 | } | |
1293 | ||
4eebcc81 SR |
1294 | static void ftrace_force_shutdown(void) |
1295 | { | |
1296 | struct task_struct *task; | |
1297 | int command = FTRACE_DISABLE_CALLS | FTRACE_UPDATE_TRACE_FUNC; | |
1298 | ||
1299 | mutex_lock(&ftraced_lock); | |
1300 | task = ftraced_task; | |
1301 | ftraced_task = NULL; | |
1302 | ftraced_suspend = -1; | |
1303 | ftrace_run_update_code(command); | |
1304 | mutex_unlock(&ftraced_lock); | |
1305 | ||
1306 | if (task) | |
1307 | kthread_stop(task); | |
1308 | } | |
1309 | ||
5072c59f SR |
1310 | static __init int ftrace_init_debugfs(void) |
1311 | { | |
1312 | struct dentry *d_tracer; | |
1313 | struct dentry *entry; | |
1314 | ||
1315 | d_tracer = tracing_init_dentry(); | |
1316 | ||
1317 | entry = debugfs_create_file("available_filter_functions", 0444, | |
1318 | d_tracer, NULL, &ftrace_avail_fops); | |
1319 | if (!entry) | |
1320 | pr_warning("Could not create debugfs " | |
1321 | "'available_filter_functions' entry\n"); | |
1322 | ||
1323 | entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer, | |
1324 | NULL, &ftrace_filter_fops); | |
1325 | if (!entry) | |
1326 | pr_warning("Could not create debugfs " | |
1327 | "'set_ftrace_filter' entry\n"); | |
41c52c0d SR |
1328 | |
1329 | entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer, | |
1330 | NULL, &ftrace_notrace_fops); | |
1331 | if (!entry) | |
1332 | pr_warning("Could not create debugfs " | |
1333 | "'set_ftrace_notrace' entry\n"); | |
5072c59f SR |
1334 | return 0; |
1335 | } | |
1336 | ||
1337 | fs_initcall(ftrace_init_debugfs); | |
1338 | ||
e309b41d | 1339 | static int __init ftrace_dynamic_init(void) |
3d083395 SR |
1340 | { |
1341 | struct task_struct *p; | |
d61f82d0 | 1342 | unsigned long addr; |
3d083395 SR |
1343 | int ret; |
1344 | ||
d61f82d0 | 1345 | addr = (unsigned long)ftrace_record_ip; |
9ff9cdb2 | 1346 | |
d61f82d0 SR |
1347 | stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS); |
1348 | ||
1349 | /* ftrace_dyn_arch_init places the return code in addr */ | |
4eebcc81 SR |
1350 | if (addr) { |
1351 | ret = (int)addr; | |
1352 | goto failed; | |
1353 | } | |
d61f82d0 | 1354 | |
3c1720f0 | 1355 | ret = ftrace_dyn_table_alloc(); |
3d083395 | 1356 | if (ret) |
4eebcc81 | 1357 | goto failed; |
3d083395 SR |
1358 | |
1359 | p = kthread_run(ftraced, NULL, "ftraced"); | |
4eebcc81 SR |
1360 | if (IS_ERR(p)) { |
1361 | ret = -1; | |
1362 | goto failed; | |
1363 | } | |
3d083395 | 1364 | |
d61f82d0 | 1365 | last_ftrace_enabled = ftrace_enabled = 1; |
e1c08bdd | 1366 | ftraced_task = p; |
3d083395 SR |
1367 | |
1368 | return 0; | |
4eebcc81 SR |
1369 | |
1370 | failed: | |
1371 | ftrace_disabled = 1; | |
1372 | return ret; | |
3d083395 SR |
1373 | } |
1374 | ||
d61f82d0 | 1375 | core_initcall(ftrace_dynamic_init); |
3d083395 | 1376 | #else |
c7aafc54 IM |
1377 | # define ftrace_startup() do { } while (0) |
1378 | # define ftrace_shutdown() do { } while (0) | |
1379 | # define ftrace_startup_sysctl() do { } while (0) | |
1380 | # define ftrace_shutdown_sysctl() do { } while (0) | |
4eebcc81 | 1381 | # define ftrace_force_shutdown() do { } while (0) |
3d083395 SR |
1382 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
1383 | ||
4eebcc81 SR |
1384 | /** |
1385 | * ftrace_kill - totally shutdown ftrace | |
1386 | * | |
1387 | * This is a safety measure. If something was detected that seems | |
1388 | * wrong, calling this function will keep ftrace from doing | |
1389 | * any more modifications, and updates. | |
1390 | * used when something went wrong. | |
1391 | */ | |
1392 | void ftrace_kill(void) | |
1393 | { | |
1394 | mutex_lock(&ftrace_sysctl_lock); | |
1395 | ftrace_disabled = 1; | |
1396 | ftrace_enabled = 0; | |
1397 | ||
1398 | clear_ftrace_function(); | |
1399 | mutex_unlock(&ftrace_sysctl_lock); | |
1400 | ||
1401 | /* Try to totally disable ftrace */ | |
1402 | ftrace_force_shutdown(); | |
1403 | } | |
1404 | ||
16444a8a | 1405 | /** |
3d083395 SR |
1406 | * register_ftrace_function - register a function for profiling |
1407 | * @ops - ops structure that holds the function for profiling. | |
16444a8a | 1408 | * |
3d083395 SR |
1409 | * Register a function to be called by all functions in the |
1410 | * kernel. | |
1411 | * | |
1412 | * Note: @ops->func and all the functions it calls must be labeled | |
1413 | * with "notrace", otherwise it will go into a | |
1414 | * recursive loop. | |
16444a8a | 1415 | */ |
3d083395 | 1416 | int register_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 1417 | { |
b0fc494f SR |
1418 | int ret; |
1419 | ||
4eebcc81 SR |
1420 | if (unlikely(ftrace_disabled)) |
1421 | return -1; | |
1422 | ||
b0fc494f | 1423 | mutex_lock(&ftrace_sysctl_lock); |
b0fc494f | 1424 | ret = __register_ftrace_function(ops); |
d61f82d0 | 1425 | ftrace_startup(); |
b0fc494f SR |
1426 | mutex_unlock(&ftrace_sysctl_lock); |
1427 | ||
1428 | return ret; | |
3d083395 SR |
1429 | } |
1430 | ||
1431 | /** | |
1432 | * unregister_ftrace_function - unresgister a function for profiling. | |
1433 | * @ops - ops structure that holds the function to unregister | |
1434 | * | |
1435 | * Unregister a function that was added to be called by ftrace profiling. | |
1436 | */ | |
1437 | int unregister_ftrace_function(struct ftrace_ops *ops) | |
1438 | { | |
1439 | int ret; | |
1440 | ||
b0fc494f | 1441 | mutex_lock(&ftrace_sysctl_lock); |
3d083395 | 1442 | ret = __unregister_ftrace_function(ops); |
d61f82d0 | 1443 | ftrace_shutdown(); |
b0fc494f SR |
1444 | mutex_unlock(&ftrace_sysctl_lock); |
1445 | ||
1446 | return ret; | |
1447 | } | |
1448 | ||
e309b41d | 1449 | int |
b0fc494f | 1450 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
5072c59f | 1451 | struct file *file, void __user *buffer, size_t *lenp, |
b0fc494f SR |
1452 | loff_t *ppos) |
1453 | { | |
1454 | int ret; | |
1455 | ||
4eebcc81 SR |
1456 | if (unlikely(ftrace_disabled)) |
1457 | return -ENODEV; | |
1458 | ||
b0fc494f SR |
1459 | mutex_lock(&ftrace_sysctl_lock); |
1460 | ||
5072c59f | 1461 | ret = proc_dointvec(table, write, file, buffer, lenp, ppos); |
b0fc494f SR |
1462 | |
1463 | if (ret || !write || (last_ftrace_enabled == ftrace_enabled)) | |
1464 | goto out; | |
1465 | ||
1466 | last_ftrace_enabled = ftrace_enabled; | |
1467 | ||
1468 | if (ftrace_enabled) { | |
1469 | ||
1470 | ftrace_startup_sysctl(); | |
1471 | ||
1472 | /* we are starting ftrace again */ | |
1473 | if (ftrace_list != &ftrace_list_end) { | |
1474 | if (ftrace_list->next == &ftrace_list_end) | |
1475 | ftrace_trace_function = ftrace_list->func; | |
1476 | else | |
1477 | ftrace_trace_function = ftrace_list_func; | |
1478 | } | |
1479 | ||
1480 | } else { | |
1481 | /* stopping ftrace calls (just send to ftrace_stub) */ | |
1482 | ftrace_trace_function = ftrace_stub; | |
1483 | ||
1484 | ftrace_shutdown_sysctl(); | |
1485 | } | |
1486 | ||
1487 | out: | |
1488 | mutex_unlock(&ftrace_sysctl_lock); | |
3d083395 | 1489 | return ret; |
16444a8a | 1490 | } |