blob: 88544f9bc0edfc40b7eb80ded51f8b60ffdcf675 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
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
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
19#include <linux/kthread.h>
20#include <linux/hardirq.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020021#include <linux/ftrace.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/module.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020023#include <linux/sysctl.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020024#include <linux/hash.h>
25#include <linux/list.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020026
Steven Rostedt3d083392008-05-12 21:20:42 +020027#include "trace.h"
28
Steven Rostedtd61f82d2008-05-12 21:20:43 +020029int ftrace_enabled;
30static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020031
Steven Rostedt3d083392008-05-12 21:20:42 +020032static DEFINE_SPINLOCK(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020033static DEFINE_MUTEX(ftrace_sysctl_lock);
34
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035static struct ftrace_ops ftrace_list_end __read_mostly =
36{
37 .func = ftrace_stub,
38};
39
40static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
41ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
42
43/* mcount is defined per arch in assembly */
44EXPORT_SYMBOL(mcount);
45
46notrace void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
47{
48 struct ftrace_ops *op = ftrace_list;
49
50 /* in case someone actually ports this to alpha! */
51 read_barrier_depends();
52
53 while (op != &ftrace_list_end) {
54 /* silly alpha */
55 read_barrier_depends();
56 op->func(ip, parent_ip);
57 op = op->next;
58 };
59}
60
61/**
Steven Rostedt3d083392008-05-12 21:20:42 +020062 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020063 *
Steven Rostedt3d083392008-05-12 21:20:42 +020064 * This NULLs the ftrace function and in essence stops
65 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020066 */
Steven Rostedt3d083392008-05-12 21:20:42 +020067void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020068{
Steven Rostedt3d083392008-05-12 21:20:42 +020069 ftrace_trace_function = ftrace_stub;
70}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020071
Steven Rostedt3d083392008-05-12 21:20:42 +020072static int notrace __register_ftrace_function(struct ftrace_ops *ops)
73{
74 /* Should never be called by interrupts */
75 spin_lock(&ftrace_lock);
76
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020077 ops->next = ftrace_list;
78 /*
79 * We are entering ops into the ftrace_list but another
80 * CPU might be walking that list. We need to make sure
81 * the ops->next pointer is valid before another CPU sees
82 * the ops pointer included into the ftrace_list.
83 */
84 smp_wmb();
85 ftrace_list = ops;
Steven Rostedt3d083392008-05-12 21:20:42 +020086
Steven Rostedtb0fc4942008-05-12 21:20:43 +020087 if (ftrace_enabled) {
88 /*
89 * For one func, simply call it directly.
90 * For more than one func, call the chain.
91 */
92 if (ops->next == &ftrace_list_end)
93 ftrace_trace_function = ops->func;
94 else
95 ftrace_trace_function = ftrace_list_func;
96 }
Steven Rostedt3d083392008-05-12 21:20:42 +020097
98 spin_unlock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020099
100 return 0;
101}
102
Steven Rostedt3d083392008-05-12 21:20:42 +0200103static int notrace __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104{
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105 struct ftrace_ops **p;
106 int ret = 0;
107
Steven Rostedt3d083392008-05-12 21:20:42 +0200108 spin_lock(&ftrace_lock);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109
110 /*
Steven Rostedt3d083392008-05-12 21:20:42 +0200111 * If we are removing the last function, then simply point
112 * to the ftrace_stub.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113 */
114 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
115 ftrace_trace_function = ftrace_stub;
116 ftrace_list = &ftrace_list_end;
117 goto out;
118 }
119
120 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
121 if (*p == ops)
122 break;
123
124 if (*p != ops) {
125 ret = -1;
126 goto out;
127 }
128
129 *p = (*p)->next;
130
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200131 if (ftrace_enabled) {
132 /* If we only have one func left, then call that directly */
133 if (ftrace_list == &ftrace_list_end ||
134 ftrace_list->next == &ftrace_list_end)
135 ftrace_trace_function = ftrace_list->func;
136 }
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200137
138 out:
Steven Rostedt3d083392008-05-12 21:20:42 +0200139 spin_unlock(&ftrace_lock);
140
141 return ret;
142}
143
144#ifdef CONFIG_DYNAMIC_FTRACE
145
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200146enum {
147 FTRACE_ENABLE_CALLS = (1 << 0),
148 FTRACE_DISABLE_CALLS = (1 << 1),
149 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
150 FTRACE_ENABLE_MCOUNT = (1 << 3),
151 FTRACE_DISABLE_MCOUNT = (1 << 4),
152};
153
Steven Rostedt3d083392008-05-12 21:20:42 +0200154static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
155
156static DEFINE_PER_CPU(int, ftrace_shutdown_disable_cpu);
157
158static DEFINE_SPINLOCK(ftrace_shutdown_lock);
159static DEFINE_MUTEX(ftraced_lock);
160
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200161struct ftrace_page {
162 struct ftrace_page *next;
163 int index;
164 struct dyn_ftrace records[];
165} __attribute__((packed));
166
167#define ENTRIES_PER_PAGE \
168 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
169
170/* estimate from running different kernels */
171#define NR_TO_INIT 10000
172
173static struct ftrace_page *ftrace_pages_start;
174static struct ftrace_page *ftrace_pages;
175
Steven Rostedt3d083392008-05-12 21:20:42 +0200176static int ftraced_trigger;
177static int ftraced_suspend;
178
179static int ftrace_record_suspend;
180
181static inline int
182notrace ftrace_ip_in_hash(unsigned long ip, unsigned long key)
183{
184 struct dyn_ftrace *p;
185 struct hlist_node *t;
186 int found = 0;
187
188 hlist_for_each_entry(p, t, &ftrace_hash[key], node) {
189 if (p->ip == ip) {
190 found = 1;
191 break;
192 }
193 }
194
195 return found;
196}
197
198static inline void notrace
199ftrace_add_hash(struct dyn_ftrace *node, unsigned long key)
200{
201 hlist_add_head(&node->node, &ftrace_hash[key]);
202}
203
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200204static notrace struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200205{
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200206 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
207 if (!ftrace_pages->next)
208 return NULL;
209 ftrace_pages = ftrace_pages->next;
210 }
211
212 return &ftrace_pages->records[ftrace_pages->index++];
213}
214
Steven Rostedt3d083392008-05-12 21:20:42 +0200215static void notrace
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200216ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +0200217{
218 struct dyn_ftrace *node;
219 unsigned long flags;
220 unsigned long key;
221 int resched;
222 int atomic;
223
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200224 if (!ftrace_enabled)
225 return;
226
Steven Rostedt3d083392008-05-12 21:20:42 +0200227 resched = need_resched();
228 preempt_disable_notrace();
229
230 /* We simply need to protect against recursion */
231 __get_cpu_var(ftrace_shutdown_disable_cpu)++;
232 if (__get_cpu_var(ftrace_shutdown_disable_cpu) != 1)
233 goto out;
234
235 if (unlikely(ftrace_record_suspend))
236 goto out;
237
238 key = hash_long(ip, FTRACE_HASHBITS);
239
240 WARN_ON_ONCE(key >= FTRACE_HASHSIZE);
241
242 if (ftrace_ip_in_hash(ip, key))
243 goto out;
244
245 atomic = irqs_disabled();
246
247 spin_lock_irqsave(&ftrace_shutdown_lock, flags);
248
249 /* This ip may have hit the hash before the lock */
250 if (ftrace_ip_in_hash(ip, key))
251 goto out_unlock;
252
253 /*
254 * There's a slight race that the ftraced will update the
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200255 * hash and reset here. If it is already converted, skip it.
Steven Rostedt3d083392008-05-12 21:20:42 +0200256 */
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200257 if (ftrace_ip_converted(ip))
258 goto out_unlock;
259
260 node = ftrace_alloc_dyn_node(ip);
Steven Rostedt3d083392008-05-12 21:20:42 +0200261 if (!node)
262 goto out_unlock;
263
264 node->ip = ip;
265
266 ftrace_add_hash(node, key);
267
268 ftraced_trigger = 1;
269
270 out_unlock:
271 spin_unlock_irqrestore(&ftrace_shutdown_lock, flags);
272 out:
273 __get_cpu_var(ftrace_shutdown_disable_cpu)--;
274
275 /* prevent recursion with scheduler */
276 if (resched)
277 preempt_enable_no_resched_notrace();
278 else
279 preempt_enable_notrace();
280}
281
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200282#define FTRACE_ADDR ((long)(&ftrace_caller))
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200283#define MCOUNT_ADDR ((long)(&mcount))
284
285static void notrace ftrace_replace_code(int saved)
286{
287 unsigned char *new = NULL, *old = NULL;
288 struct dyn_ftrace *rec;
289 struct ftrace_page *pg;
290 unsigned long ip;
291 int failed;
292 int i;
293
294 if (saved)
295 old = ftrace_nop_replace();
296 else
297 new = ftrace_nop_replace();
298
299 for (pg = ftrace_pages_start; pg; pg = pg->next) {
300 for (i = 0; i < pg->index; i++) {
301 rec = &pg->records[i];
302
303 /* don't modify code that has already faulted */
304 if (rec->flags & FTRACE_FL_FAILED)
305 continue;
306
307 ip = rec->ip;
308
309 if (saved)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200310 new = ftrace_call_replace(ip, FTRACE_ADDR);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200311 else
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200312 old = ftrace_call_replace(ip, FTRACE_ADDR);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200313
314 failed = ftrace_modify_code(ip, old, new);
315 if (failed)
316 rec->flags |= FTRACE_FL_FAILED;
317 }
318 }
319}
320
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200321static notrace void ftrace_shutdown_replenish(void)
322{
323 if (ftrace_pages->next)
324 return;
325
326 /* allocate another page */
327 ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
328}
Steven Rostedt3d083392008-05-12 21:20:42 +0200329
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200330static notrace void
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200331ftrace_code_disable(struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200332{
333 unsigned long ip;
334 unsigned char *nop, *call;
335 int failed;
336
337 ip = rec->ip;
338
339 nop = ftrace_nop_replace();
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200340 call = ftrace_call_replace(ip, MCOUNT_ADDR);
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200341
342 failed = ftrace_modify_code(ip, call, nop);
343 if (failed)
344 rec->flags |= FTRACE_FL_FAILED;
345}
346
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200347static int notrace __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200348{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200349 unsigned long addr;
350 int *command = data;
351
352 if (*command & FTRACE_ENABLE_CALLS)
353 ftrace_replace_code(1);
354 else if (*command & FTRACE_DISABLE_CALLS)
355 ftrace_replace_code(0);
356
357 if (*command & FTRACE_UPDATE_TRACE_FUNC)
358 ftrace_update_ftrace_func(ftrace_trace_function);
359
360 if (*command & FTRACE_ENABLE_MCOUNT) {
361 addr = (unsigned long)ftrace_record_ip;
362 ftrace_mcount_set(&addr);
363 } else if (*command & FTRACE_DISABLE_MCOUNT) {
364 addr = (unsigned long)ftrace_stub;
365 ftrace_mcount_set(&addr);
366 }
367
368 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200369}
370
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200371static void notrace ftrace_run_update_code(int command)
Steven Rostedt3d083392008-05-12 21:20:42 +0200372{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200373 stop_machine_run(__ftrace_modify_code, &command, NR_CPUS);
Steven Rostedt3d083392008-05-12 21:20:42 +0200374}
375
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200376static ftrace_func_t saved_ftrace_func;
377
Steven Rostedt3d083392008-05-12 21:20:42 +0200378static void notrace ftrace_startup(void)
379{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200380 int command = 0;
381
Steven Rostedt3d083392008-05-12 21:20:42 +0200382 mutex_lock(&ftraced_lock);
383 ftraced_suspend++;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200384 if (ftraced_suspend == 1)
385 command |= FTRACE_ENABLE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +0200386
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200387 if (saved_ftrace_func != ftrace_trace_function) {
388 saved_ftrace_func = ftrace_trace_function;
389 command |= FTRACE_UPDATE_TRACE_FUNC;
390 }
391
392 if (!command || !ftrace_enabled)
393 goto out;
394
395 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200396 out:
397 mutex_unlock(&ftraced_lock);
398}
399
400static void notrace ftrace_shutdown(void)
401{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200402 int command = 0;
403
Steven Rostedt3d083392008-05-12 21:20:42 +0200404 mutex_lock(&ftraced_lock);
405 ftraced_suspend--;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200406 if (!ftraced_suspend)
407 command |= FTRACE_DISABLE_CALLS;
408
409 if (saved_ftrace_func != ftrace_trace_function) {
410 saved_ftrace_func = ftrace_trace_function;
411 command |= FTRACE_UPDATE_TRACE_FUNC;
412 }
413
414 if (!command || !ftrace_enabled)
Steven Rostedt3d083392008-05-12 21:20:42 +0200415 goto out;
416
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200417 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +0200418 out:
419 mutex_unlock(&ftraced_lock);
420}
421
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200422static void notrace ftrace_startup_sysctl(void)
423{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200424 int command = FTRACE_ENABLE_MCOUNT;
425
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200426 mutex_lock(&ftraced_lock);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200427 /* Force update next time */
428 saved_ftrace_func = NULL;
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200429 /* ftraced_suspend is true if we want ftrace running */
430 if (ftraced_suspend)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200431 command |= FTRACE_ENABLE_CALLS;
432
433 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200434 mutex_unlock(&ftraced_lock);
435}
436
437static void notrace ftrace_shutdown_sysctl(void)
438{
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200439 int command = FTRACE_DISABLE_MCOUNT;
440
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200441 mutex_lock(&ftraced_lock);
442 /* ftraced_suspend is true if ftrace is running */
443 if (ftraced_suspend)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200444 command |= FTRACE_DISABLE_CALLS;
445
446 ftrace_run_update_code(command);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200447 mutex_unlock(&ftraced_lock);
448}
449
Steven Rostedt3d083392008-05-12 21:20:42 +0200450static cycle_t ftrace_update_time;
451static unsigned long ftrace_update_cnt;
452unsigned long ftrace_update_tot_cnt;
453
454static int notrace __ftrace_update_code(void *ignore)
455{
456 struct dyn_ftrace *p;
457 struct hlist_head head;
458 struct hlist_node *t;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200459 int save_ftrace_enabled;
Steven Rostedt3d083392008-05-12 21:20:42 +0200460 cycle_t start, stop;
461 int i;
462
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200463 /* Don't be recording funcs now */
464 save_ftrace_enabled = ftrace_enabled;
465 ftrace_enabled = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200466
467 start = now(raw_smp_processor_id());
468 ftrace_update_cnt = 0;
469
470 /* No locks needed, the machine is stopped! */
471 for (i = 0; i < FTRACE_HASHSIZE; i++) {
472 if (hlist_empty(&ftrace_hash[i]))
473 continue;
474
475 head = ftrace_hash[i];
476 INIT_HLIST_HEAD(&ftrace_hash[i]);
477
478 /* all CPUS are stopped, we are safe to modify code */
479 hlist_for_each_entry(p, t, &head, node) {
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200480 ftrace_code_disable(p);
Steven Rostedt3d083392008-05-12 21:20:42 +0200481 ftrace_update_cnt++;
482 }
483
484 }
485
486 stop = now(raw_smp_processor_id());
487 ftrace_update_time = stop - start;
488 ftrace_update_tot_cnt += ftrace_update_cnt;
489
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200490 ftrace_enabled = save_ftrace_enabled;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200491
492 return 0;
493}
494
Steven Rostedt3d083392008-05-12 21:20:42 +0200495static void notrace ftrace_update_code(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200496{
Steven Rostedt3d083392008-05-12 21:20:42 +0200497 stop_machine_run(__ftrace_update_code, NULL, NR_CPUS);
498}
499
500static int notrace ftraced(void *ignore)
501{
502 unsigned long usecs;
503
504 set_current_state(TASK_INTERRUPTIBLE);
505
506 while (!kthread_should_stop()) {
507
508 /* check once a second */
509 schedule_timeout(HZ);
510
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200511 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200512 mutex_lock(&ftraced_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200513 if (ftrace_enabled && ftraced_trigger && !ftraced_suspend) {
Steven Rostedt3d083392008-05-12 21:20:42 +0200514 ftrace_record_suspend++;
515 ftrace_update_code();
516 usecs = nsecs_to_usecs(ftrace_update_time);
517 if (ftrace_update_tot_cnt > 100000) {
518 ftrace_update_tot_cnt = 0;
519 pr_info("hm, dftrace overflow: %lu change%s"
520 " (%lu total) in %lu usec%s\n",
521 ftrace_update_cnt,
522 ftrace_update_cnt != 1 ? "s" : "",
523 ftrace_update_tot_cnt,
524 usecs, usecs != 1 ? "s" : "");
525 WARN_ON_ONCE(1);
526 }
527 ftraced_trigger = 0;
528 ftrace_record_suspend--;
529 }
530 mutex_unlock(&ftraced_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200531 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200532
533 ftrace_shutdown_replenish();
534
535 set_current_state(TASK_INTERRUPTIBLE);
536 }
537 __set_current_state(TASK_RUNNING);
538 return 0;
539}
540
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200541static int __init ftrace_dyn_table_alloc(void)
542{
543 struct ftrace_page *pg;
544 int cnt;
545 int i;
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200546
547 /* allocate a few pages */
548 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
549 if (!ftrace_pages_start)
550 return -1;
551
552 /*
553 * Allocate a few more pages.
554 *
555 * TODO: have some parser search vmlinux before
556 * final linking to find all calls to ftrace.
557 * Then we can:
558 * a) know how many pages to allocate.
559 * and/or
560 * b) set up the table then.
561 *
562 * The dynamic code is still necessary for
563 * modules.
564 */
565
566 pg = ftrace_pages = ftrace_pages_start;
567
568 cnt = NR_TO_INIT / ENTRIES_PER_PAGE;
569
570 for (i = 0; i < cnt; i++) {
571 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
572
573 /* If we fail, we'll try later anyway */
574 if (!pg->next)
575 break;
576
577 pg = pg->next;
578 }
579
580 return 0;
581}
582
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200583static int __init notrace ftrace_dynamic_init(void)
Steven Rostedt3d083392008-05-12 21:20:42 +0200584{
585 struct task_struct *p;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200586 unsigned long addr;
Steven Rostedt3d083392008-05-12 21:20:42 +0200587 int ret;
588
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200589 addr = (unsigned long)ftrace_record_ip;
590 stop_machine_run(ftrace_dyn_arch_init, &addr, NR_CPUS);
591
592 /* ftrace_dyn_arch_init places the return code in addr */
593 if (addr)
594 return addr;
595
Steven Rostedt3c1720f2008-05-12 21:20:43 +0200596 ret = ftrace_dyn_table_alloc();
Steven Rostedt3d083392008-05-12 21:20:42 +0200597 if (ret)
598 return ret;
599
600 p = kthread_run(ftraced, NULL, "ftraced");
601 if (IS_ERR(p))
602 return -1;
603
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200604 last_ftrace_enabled = ftrace_enabled = 1;
Steven Rostedt3d083392008-05-12 21:20:42 +0200605
606 return 0;
607}
608
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200609core_initcall(ftrace_dynamic_init);
Steven Rostedt3d083392008-05-12 21:20:42 +0200610#else
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200611# define ftrace_startup() do { } while (0)
612# define ftrace_shutdown() do { } while (0)
613# define ftrace_startup_sysctl() do { } while (0)
614# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedt3d083392008-05-12 21:20:42 +0200615#endif /* CONFIG_DYNAMIC_FTRACE */
616
617/**
618 * register_ftrace_function - register a function for profiling
619 * @ops - ops structure that holds the function for profiling.
620 *
621 * Register a function to be called by all functions in the
622 * kernel.
623 *
624 * Note: @ops->func and all the functions it calls must be labeled
625 * with "notrace", otherwise it will go into a
626 * recursive loop.
627 */
628int register_ftrace_function(struct ftrace_ops *ops)
629{
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200630 int ret;
631
632 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200633 ret = __register_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200634 ftrace_startup();
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200635 mutex_unlock(&ftrace_sysctl_lock);
636
637 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +0200638}
639
640/**
641 * unregister_ftrace_function - unresgister a function for profiling.
642 * @ops - ops structure that holds the function to unregister
643 *
644 * Unregister a function that was added to be called by ftrace profiling.
645 */
646int unregister_ftrace_function(struct ftrace_ops *ops)
647{
648 int ret;
649
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200650 mutex_lock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200651 ret = __unregister_ftrace_function(ops);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200652 ftrace_shutdown();
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200653 mutex_unlock(&ftrace_sysctl_lock);
654
655 return ret;
656}
657
658notrace int
659ftrace_enable_sysctl(struct ctl_table *table, int write,
660 struct file *filp, void __user *buffer, size_t *lenp,
661 loff_t *ppos)
662{
663 int ret;
664
665 mutex_lock(&ftrace_sysctl_lock);
666
667 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
668
669 if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
670 goto out;
671
672 last_ftrace_enabled = ftrace_enabled;
673
674 if (ftrace_enabled) {
675
676 ftrace_startup_sysctl();
677
678 /* we are starting ftrace again */
679 if (ftrace_list != &ftrace_list_end) {
680 if (ftrace_list->next == &ftrace_list_end)
681 ftrace_trace_function = ftrace_list->func;
682 else
683 ftrace_trace_function = ftrace_list_func;
684 }
685
686 } else {
687 /* stopping ftrace calls (just send to ftrace_stub) */
688 ftrace_trace_function = ftrace_stub;
689
690 ftrace_shutdown_sysctl();
691 }
692
693 out:
694 mutex_unlock(&ftrace_sysctl_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +0200695 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200696}