ftrace.c 97.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * Infrastructure for profiling code inserted by 'gcc -pg'.
 *
 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
 *
 * Originally ported from the -rt patch by:
 *   Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
 *
 * Based on code in the latency_tracer, that is:
 *
 *  Copyright (C) 2004-2006 Ingo Molnar
 *  Copyright (C) 2004 William Lee Irwin III
 */

16
17
18
#include <linux/stop_machine.h>
#include <linux/clocksource.h>
#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>
Ingo Molnar's avatar
Ingo Molnar committed
23
#include <linux/kthread.h>
24
#include <linux/uaccess.h>
25
#include <linux/bsearch.h>
26
#include <linux/module.h>
Ingo Molnar's avatar
Ingo Molnar committed
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
46
	({					\
		int ___r = cond;		\
		if (WARN_ON(___r))		\
47
			ftrace_kill();		\
48
49
		___r;				\
	})
50
51

#define FTRACE_WARN_ON_ONCE(cond)		\
52
53
54
	({					\
		int ___r = cond;		\
		if (WARN_ON_ONCE(___r))		\
55
			ftrace_kill();		\
56
57
		___r;				\
	})
58

59
60
61
/* hash bits for specific function selection */
#define FTRACE_HASH_BITS 7
#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62
63
#define FTRACE_HASH_DEFAULT_BITS 10
#define FTRACE_HASH_MAX_BITS 12
64

65
66
/* ftrace_enabled is a method to turn ftrace on or off */
int ftrace_enabled __read_mostly;
67
static int last_ftrace_enabled;
68

69
70
71
/* Quick disabling of function tracer. */
int function_trace_stop;

72
73
74
75
76
77
78
/* List for set_ftrace_pid's pids. */
LIST_HEAD(ftrace_pids);
struct ftrace_pid {
	struct list_head list;
	struct pid *pid;
};

79
80
81
82
83
84
/*
 * ftrace_disabled is set when an anomaly is discovered.
 * ftrace_disabled is much stronger than ftrace_enabled.
 */
static int ftrace_disabled __read_mostly;

85
static DEFINE_MUTEX(ftrace_lock);
86

87
static struct ftrace_ops ftrace_list_end __read_mostly = {
88
	.func		= ftrace_stub,
89
90
};

91
92
static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
93
ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
94
static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
95
ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
96
ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
97
static struct ftrace_ops global_ops;
98

99
100
101
static void
ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);

102
/*
103
 * Traverse the ftrace_global_list, invoking all entries.  The reason that we
104
105
106
 * can use rcu_dereference_raw() is that elements removed from this list
 * are simply leaked, so there is no need to interact with a grace-period
 * mechanism.  The rcu_dereference_raw() calls are needed to handle
107
 * concurrent insertions into the ftrace_global_list.
108
109
110
 *
 * Silly Alpha and silly pointer-speculation compiler optimizations!
 */
111
112
static void ftrace_global_list_func(unsigned long ip,
				    unsigned long parent_ip)
113
{
114
115
116
117
	struct ftrace_ops *op;

	if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
		return;
118

119
120
	trace_recursion_set(TRACE_GLOBAL_BIT);
	op = rcu_dereference_raw(ftrace_global_list); /*see above*/
121
122
	while (op != &ftrace_list_end) {
		op->func(ip, parent_ip);
123
		op = rcu_dereference_raw(op->next); /*see above*/
124
	};
125
	trace_recursion_clear(TRACE_GLOBAL_BIT);
126
127
}

128
129
static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
{
130
	if (!test_tsk_trace_trace(current))
131
132
133
134
135
136
137
138
139
140
141
142
		return;

	ftrace_pid_function(ip, parent_ip);
}

static void set_ftrace_pid_function(ftrace_func_t func)
{
	/* do not set ftrace_pid_function to itself! */
	if (func != ftrace_pid_func)
		ftrace_pid_function = func;
}

143
/**
144
 * clear_ftrace_function - reset the ftrace function
145
 *
146
147
 * This NULLs the ftrace function and in essence stops
 * tracing.  There may be lag
148
 */
149
void clear_ftrace_function(void)
150
{
151
	ftrace_trace_function = ftrace_stub;
152
	__ftrace_trace_function = ftrace_stub;
153
	__ftrace_trace_function_delay = ftrace_stub;
154
	ftrace_pid_function = ftrace_stub;
155
156
}

157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
/*
 * For those archs that do not test ftrace_trace_stop in their
 * mcount call site, we need to do it from C.
 */
static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
{
	if (function_trace_stop)
		return;

	__ftrace_trace_function(ip, parent_ip);
}
#endif

171
static void update_global_ops(void)
172
173
174
175
176
177
178
179
{
	ftrace_func_t func;

	/*
	 * If there's only one function registered, then call that
	 * function directly. Otherwise, we need to iterate over the
	 * registered callers.
	 */
180
181
182
	if (ftrace_global_list == &ftrace_list_end ||
	    ftrace_global_list->next == &ftrace_list_end)
		func = ftrace_global_list->func;
183
	else
184
		func = ftrace_global_list_func;
185
186
187
188
189
190

	/* If we filter on pids, update to use the pid function */
	if (!list_empty(&ftrace_pids)) {
		set_ftrace_pid_function(func);
		func = ftrace_pid_func;
	}
191
192
193
194
195
196
197
198
199
200

	global_ops.func = func;
}

static void update_ftrace_function(void)
{
	ftrace_func_t func;

	update_global_ops();

201
202
203
204
205
	/*
	 * If we are at the end of the list and this ops is
	 * not dynamic, then have the mcount trampoline call
	 * the function directly
	 */
206
	if (ftrace_ops_list == &ftrace_list_end ||
207
208
	    (ftrace_ops_list->next == &ftrace_list_end &&
	     !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
209
210
211
		func = ftrace_ops_list->func;
	else
		func = ftrace_ops_list_func;
212

213
214
#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
	ftrace_trace_function = func;
215
216
217
218
#else
#ifdef CONFIG_DYNAMIC_FTRACE
	/* do not update till all functions have been modified */
	__ftrace_trace_function_delay = func;
219
220
#else
	__ftrace_trace_function = func;
221
#endif
222
223
224
225
	ftrace_trace_function = ftrace_test_stop_func;
#endif
}

226
static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
227
{
228
	ops->next = *list;
229
	/*
230
	 * We are entering ops into the list but another
231
232
	 * CPU might be walking that list. We need to make sure
	 * the ops->next pointer is valid before another CPU sees
233
	 * the ops pointer included into the list.
234
	 */
235
	rcu_assign_pointer(*list, ops);
236
237
}

238
static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
239
240
241
242
{
	struct ftrace_ops **p;

	/*
243
244
	 * If we are removing the last function, then simply point
	 * to the ftrace_stub.
245
	 */
246
247
	if (*list == ops && ops->next == &ftrace_list_end) {
		*list = &ftrace_list_end;
Steven Rostedt's avatar
Steven Rostedt committed
248
		return 0;
249
250
	}

251
	for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
252
253
254
		if (*p == ops)
			break;

Steven Rostedt's avatar
Steven Rostedt committed
255
256
	if (*p != ops)
		return -1;
257
258

	*p = (*p)->next;
259
260
	return 0;
}
261

262
263
264
265
266
267
268
269
static int __register_ftrace_function(struct ftrace_ops *ops)
{
	if (ftrace_disabled)
		return -ENODEV;

	if (FTRACE_WARN_ON(ops == &global_ops))
		return -EINVAL;

270
271
272
	if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
		return -EBUSY;

273
274
275
	if (!core_kernel_data((unsigned long)ops))
		ops->flags |= FTRACE_OPS_FL_DYNAMIC;

276
277
278
279
280
281
282
283
284
	if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
		int first = ftrace_global_list == &ftrace_list_end;
		add_ftrace_ops(&ftrace_global_list, ops);
		ops->flags |= FTRACE_OPS_FL_ENABLED;
		if (first)
			add_ftrace_ops(&ftrace_ops_list, &global_ops);
	} else
		add_ftrace_ops(&ftrace_ops_list, ops);

285
286
287
288
289
290
291
292
293
294
295
296
297
	if (ftrace_enabled)
		update_ftrace_function();

	return 0;
}

static int __unregister_ftrace_function(struct ftrace_ops *ops)
{
	int ret;

	if (ftrace_disabled)
		return -ENODEV;

298
299
300
	if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
		return -EBUSY;

301
302
303
	if (FTRACE_WARN_ON(ops == &global_ops))
		return -EINVAL;

304
305
306
307
308
309
310
311
312
	if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
		ret = remove_ftrace_ops(&ftrace_global_list, ops);
		if (!ret && ftrace_global_list == &ftrace_list_end)
			ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
		if (!ret)
			ops->flags &= ~FTRACE_OPS_FL_ENABLED;
	} else
		ret = remove_ftrace_ops(&ftrace_ops_list, ops);

313
314
	if (ret < 0)
		return ret;
315

316
317
	if (ftrace_enabled)
		update_ftrace_function();
318

319
320
321
322
323
324
325
	/*
	 * Dynamic ops may be freed, we must make sure that all
	 * callers are done before leaving this function.
	 */
	if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
		synchronize_sched();

Steven Rostedt's avatar
Steven Rostedt committed
326
	return 0;
327
328
}

329
330
static void ftrace_update_pid_func(void)
{
331
	/* Only do something if we are tracing something */
332
	if (ftrace_trace_function == ftrace_stub)
333
		return;
334

335
	update_ftrace_function();
336
337
}

338
339
340
341
342
#ifdef CONFIG_FUNCTION_PROFILER
struct ftrace_profile {
	struct hlist_node		node;
	unsigned long			ip;
	unsigned long			counter;
343
344
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
	unsigned long long		time;
345
	unsigned long long		time_squared;
346
#endif
347
348
};

349
350
351
352
struct ftrace_profile_page {
	struct ftrace_profile_page	*next;
	unsigned long			index;
	struct ftrace_profile		records[];
353
354
};

355
356
357
358
359
360
361
362
struct ftrace_profile_stat {
	atomic_t			disabled;
	struct hlist_head		*hash;
	struct ftrace_profile_page	*pages;
	struct ftrace_profile_page	*start;
	struct tracer_stat		stat;
};

363
364
#define PROFILE_RECORDS_SIZE						\
	(PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
365

366
367
#define PROFILES_PER_PAGE					\
	(PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
368

369
370
371
372
static int ftrace_profile_bits __read_mostly;
static int ftrace_profile_enabled __read_mostly;

/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
373
374
static DEFINE_MUTEX(ftrace_profile_lock);

375
static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
376
377
378

#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */

379
380
381
static void *
function_stat_next(void *v, int idx)
{
382
383
	struct ftrace_profile *rec = v;
	struct ftrace_profile_page *pg;
384

385
	pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
386
387

 again:
Li Zefan's avatar
Li Zefan committed
388
389
390
	if (idx != 0)
		rec++;

391
392
393
394
395
	if ((void *)rec >= (void *)&pg->records[pg->index]) {
		pg = pg->next;
		if (!pg)
			return NULL;
		rec = &pg->records[0];
396
397
		if (!rec->counter)
			goto again;
398
399
400
401
402
403
404
	}

	return rec;
}

static void *function_stat_start(struct tracer_stat *trace)
{
405
406
407
408
409
410
411
	struct ftrace_profile_stat *stat =
		container_of(trace, struct ftrace_profile_stat, stat);

	if (!stat || !stat->start)
		return NULL;

	return function_stat_next(&stat->start->records[0], 0);
412
413
}

414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
/* function graph compares on total time */
static int function_stat_cmp(void *p1, void *p2)
{
	struct ftrace_profile *a = p1;
	struct ftrace_profile *b = p2;

	if (a->time < b->time)
		return -1;
	if (a->time > b->time)
		return 1;
	else
		return 0;
}
#else
/* not function graph compares against hits */
430
431
static int function_stat_cmp(void *p1, void *p2)
{
432
433
	struct ftrace_profile *a = p1;
	struct ftrace_profile *b = p2;
434
435
436
437
438
439
440
441

	if (a->counter < b->counter)
		return -1;
	if (a->counter > b->counter)
		return 1;
	else
		return 0;
}
442
#endif
443
444
445

static int function_stat_headers(struct seq_file *m)
{
446
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
447
	seq_printf(m, "  Function                               "
448
		   "Hit    Time            Avg             s^2\n"
449
		      "  --------                               "
450
		   "---    ----            ---             ---\n");
451
#else
452
453
	seq_printf(m, "  Function                               Hit\n"
		      "  --------                               ---\n");
454
#endif
455
456
457
458
459
	return 0;
}

static int function_stat_show(struct seq_file *m, void *v)
{
460
	struct ftrace_profile *rec = v;
461
	char str[KSYM_SYMBOL_LEN];
462
	int ret = 0;
463
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
464
465
	static struct trace_seq s;
	unsigned long long avg;
466
	unsigned long long stddev;
467
#endif
468
469
470
471
472
473
474
	mutex_lock(&ftrace_profile_lock);

	/* we raced with function_profile_reset() */
	if (unlikely(rec->counter == 0)) {
		ret = -EBUSY;
		goto out;
	}
475
476

	kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
477
478
479
480
	seq_printf(m, "  %-30.30s  %10lu", str, rec->counter);

#ifdef CONFIG_FUNCTION_GRAPH_TRACER
	seq_printf(m, "    ");
481
482
483
	avg = rec->time;
	do_div(avg, rec->counter);

484
485
486
487
488
489
490
491
492
493
494
495
	/* Sample standard deviation (s^2) */
	if (rec->counter <= 1)
		stddev = 0;
	else {
		stddev = rec->time_squared - rec->counter * avg * avg;
		/*
		 * Divide only 1000 for ns^2 -> us^2 conversion.
		 * trace_print_graph_duration will divide 1000 again.
		 */
		do_div(stddev, (rec->counter - 1) * 1000);
	}

496
497
498
499
	trace_seq_init(&s);
	trace_print_graph_duration(rec->time, &s);
	trace_seq_puts(&s, "    ");
	trace_print_graph_duration(avg, &s);
500
501
	trace_seq_puts(&s, "    ");
	trace_print_graph_duration(stddev, &s);
502
503
504
	trace_print_seq(m, &s);
#endif
	seq_putc(m, '\n');
505
506
out:
	mutex_unlock(&ftrace_profile_lock);
507

508
	return ret;
509
510
}

511
static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
512
{
513
	struct ftrace_profile_page *pg;
514

515
	pg = stat->pages = stat->start;
516

517
518
519
520
	while (pg) {
		memset(pg->records, 0, PROFILE_RECORDS_SIZE);
		pg->index = 0;
		pg = pg->next;
521
522
	}

523
	memset(stat->hash, 0,
524
525
	       FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
}
526

527
int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
528
529
{
	struct ftrace_profile_page *pg;
530
531
	int functions;
	int pages;
532
	int i;
533

534
	/* If we already allocated, do nothing */
535
	if (stat->pages)
536
		return 0;
537

538
539
	stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
	if (!stat->pages)
540
		return -ENOMEM;
541

542
543
544
545
546
547
548
549
550
551
552
553
554
#ifdef CONFIG_DYNAMIC_FTRACE
	functions = ftrace_update_tot_cnt;
#else
	/*
	 * We do not know the number of functions that exist because
	 * dynamic tracing is what counts them. With past experience
	 * we have around 20K functions. That should be more than enough.
	 * It is highly unlikely we will execute every function in
	 * the kernel.
	 */
	functions = 20000;
#endif

555
	pg = stat->start = stat->pages;
556

557
558
559
	pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);

	for (i = 0; i < pages; i++) {
560
561
		pg->next = (void *)get_zeroed_page(GFP_KERNEL);
		if (!pg->next)
562
			goto out_free;
563
564
565
566
		pg = pg->next;
	}

	return 0;
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581

 out_free:
	pg = stat->start;
	while (pg) {
		unsigned long tmp = (unsigned long)pg;

		pg = pg->next;
		free_page(tmp);
	}

	free_page((unsigned long)stat->pages);
	stat->pages = NULL;
	stat->start = NULL;

	return -ENOMEM;
582
583
}

584
static int ftrace_profile_init_cpu(int cpu)
585
{
586
	struct ftrace_profile_stat *stat;
587
	int size;
588

589
590
591
	stat = &per_cpu(ftrace_profile_stats, cpu);

	if (stat->hash) {
592
		/* If the profile is already created, simply reset it */
593
		ftrace_profile_reset(stat);
594
595
		return 0;
	}
596

597
598
599
600
601
	/*
	 * We are profiling all functions, but usually only a few thousand
	 * functions are hit. We'll make a hash of 1024 items.
	 */
	size = FTRACE_PROFILE_HASH_SIZE;
602

603
	stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
604

605
	if (!stat->hash)
606
607
		return -ENOMEM;

608
609
	if (!ftrace_profile_bits) {
		size--;
610

611
612
613
		for (; size; size >>= 1)
			ftrace_profile_bits++;
	}
614

615
	/* Preallocate the function profiling pages */
616
617
618
	if (ftrace_profile_pages_init(stat) < 0) {
		kfree(stat->hash);
		stat->hash = NULL;
619
620
621
622
		return -ENOMEM;
	}

	return 0;
623
624
}

625
626
627
628
629
630
631
632
633
634
635
636
637
638
static int ftrace_profile_init(void)
{
	int cpu;
	int ret = 0;

	for_each_online_cpu(cpu) {
		ret = ftrace_profile_init_cpu(cpu);
		if (ret)
			break;
	}

	return ret;
}

639
/* interrupts must be disabled */
640
641
static struct ftrace_profile *
ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
642
{
643
	struct ftrace_profile *rec;
644
645
646
647
648
	struct hlist_head *hhd;
	struct hlist_node *n;
	unsigned long key;

	key = hash_long(ip, ftrace_profile_bits);
649
	hhd = &stat->hash[key];
650
651
652
653
654
655

	if (hlist_empty(hhd))
		return NULL;

	hlist_for_each_entry_rcu(rec, n, hhd, node) {
		if (rec->ip == ip)
656
657
658
659
660
661
			return rec;
	}

	return NULL;
}

662
663
static void ftrace_add_profile(struct ftrace_profile_stat *stat,
			       struct ftrace_profile *rec)
664
665
666
667
{
	unsigned long key;

	key = hash_long(rec->ip, ftrace_profile_bits);
668
	hlist_add_head_rcu(&rec->node, &stat->hash[key]);
669
670
}

671
672
673
/*
 * The memory is already allocated, this simply finds a new record to use.
 */
674
static struct ftrace_profile *
675
ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
676
677
678
{
	struct ftrace_profile *rec = NULL;

679
	/* prevent recursion (from NMIs) */
680
	if (atomic_inc_return(&stat->disabled) != 1)
681
682
683
		goto out;

	/*
684
685
	 * Try to find the function again since an NMI
	 * could have added it
686
	 */
687
	rec = ftrace_find_profiled_func(stat, ip);
688
	if (rec)
689
		goto out;
690

691
692
693
694
	if (stat->pages->index == PROFILES_PER_PAGE) {
		if (!stat->pages->next)
			goto out;
		stat->pages = stat->pages->next;
695
	}
696

697
	rec = &stat->pages->records[stat->pages->index++];
698
	rec->ip = ip;
699
	ftrace_add_profile(stat, rec);
700

701
 out:
702
	atomic_dec(&stat->disabled);
703
704
705
706
707
708
709

	return rec;
}

static void
function_profile_call(unsigned long ip, unsigned long parent_ip)
{
710
	struct ftrace_profile_stat *stat;
711
	struct ftrace_profile *rec;
712
713
714
715
716
717
	unsigned long flags;

	if (!ftrace_profile_enabled)
		return;

	local_irq_save(flags);
718
719

	stat = &__get_cpu_var(ftrace_profile_stats);
720
	if (!stat->hash || !ftrace_profile_enabled)
721
722
723
		goto out;

	rec = ftrace_find_profiled_func(stat, ip);
724
	if (!rec) {
725
		rec = ftrace_profile_alloc(stat, ip);
726
727
728
		if (!rec)
			goto out;
	}
729
730
731
732
733
734

	rec->counter++;
 out:
	local_irq_restore(flags);
}

735
736
737
738
739
740
741
742
743
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
static int profile_graph_entry(struct ftrace_graph_ent *trace)
{
	function_profile_call(trace->func, 0);
	return 1;
}

static void profile_graph_return(struct ftrace_graph_ret *trace)
{
744
	struct ftrace_profile_stat *stat;
745
	unsigned long long calltime;
746
	struct ftrace_profile *rec;
747
	unsigned long flags;
748
749

	local_irq_save(flags);
750
	stat = &__get_cpu_var(ftrace_profile_stats);
751
	if (!stat->hash || !ftrace_profile_enabled)
752
753
		goto out;

754
755
756
757
	/* If the calltime was zero'd ignore it */
	if (!trace->calltime)
		goto out;

758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
	calltime = trace->rettime - trace->calltime;

	if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
		int index;

		index = trace->depth;

		/* Append this call time to the parent time to subtract */
		if (index)
			current->ret_stack[index - 1].subtime += calltime;

		if (current->ret_stack[index].subtime < calltime)
			calltime -= current->ret_stack[index].subtime;
		else
			calltime = 0;
	}

775
	rec = ftrace_find_profiled_func(stat, trace->func);
776
	if (rec) {
777
		rec->time += calltime;
778
779
		rec->time_squared += calltime * calltime;
	}
780

781
 out:
782
783
784
785
786
787
788
789
790
791
792
793
794
795
	local_irq_restore(flags);
}

static int register_ftrace_profiler(void)
{
	return register_ftrace_graph(&profile_graph_return,
				     &profile_graph_entry);
}

static void unregister_ftrace_profiler(void)
{
	unregister_ftrace_graph();
}
#else
796
static struct ftrace_ops ftrace_profile_ops __read_mostly = {
797
	.func		= function_profile_call,
798
799
};

800
801
802
803
804
805
806
807
808
809
810
static int register_ftrace_profiler(void)
{
	return register_ftrace_function(&ftrace_profile_ops);
}

static void unregister_ftrace_profiler(void)
{
	unregister_ftrace_function(&ftrace_profile_ops);
}
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */

811
812
813
814
815
816
817
static ssize_t
ftrace_profile_write(struct file *filp, const char __user *ubuf,
		     size_t cnt, loff_t *ppos)
{
	unsigned long val;
	int ret;

818
819
	ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
	if (ret)
820
821
822
823
824
825
826
		return ret;

	val = !!val;

	mutex_lock(&ftrace_profile_lock);
	if (ftrace_profile_enabled ^ val) {
		if (val) {
827
828
829
830
831
832
			ret = ftrace_profile_init();
			if (ret < 0) {
				cnt = ret;
				goto out;
			}

833
834
835
836
837
			ret = register_ftrace_profiler();
			if (ret < 0) {
				cnt = ret;
				goto out;
			}
838
839
840
			ftrace_profile_enabled = 1;
		} else {
			ftrace_profile_enabled = 0;
841
842
843
844
			/*
			 * unregister_ftrace_profiler calls stop_machine
			 * so this acts like an synchronize_sched.
			 */
845
			unregister_ftrace_profiler();
846
847
		}
	}
848
 out:
849
850
	mutex_unlock(&ftrace_profile_lock);

851
	*ppos += cnt;
852
853
854
855

	return cnt;
}

856
857
858
859
static ssize_t
ftrace_profile_read(struct file *filp, char __user *ubuf,
		     size_t cnt, loff_t *ppos)
{
860
	char buf[64];		/* big enough to hold a number */
861
862
863
864
865
866
	int r;

	r = sprintf(buf, "%u\n", ftrace_profile_enabled);
	return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
}

867
868
869
870
static const struct file_operations ftrace_profile_fops = {
	.open		= tracing_open_generic,
	.read		= ftrace_profile_read,
	.write		= ftrace_profile_write,
871
	.llseek		= default_llseek,
872
873
};

874
875
/* used to initialize the real stat files */
static struct tracer_stat function_stats __initdata = {
876
877
878
879
880
881
	.name		= "functions",
	.stat_start	= function_stat_start,
	.stat_next	= function_stat_next,
	.stat_cmp	= function_stat_cmp,
	.stat_headers	= function_stat_headers,
	.stat_show	= function_stat_show
882
883
};

884
static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
885
{
886
	struct ftrace_profile_stat *stat;
887
	struct dentry *entry;
888
	char *name;
889
	int ret;
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
	int cpu;

	for_each_possible_cpu(cpu) {
		stat = &per_cpu(ftrace_profile_stats, cpu);

		/* allocate enough for function name + cpu number */
		name = kmalloc(32, GFP_KERNEL);
		if (!name) {
			/*
			 * The files created are permanent, if something happens
			 * we still do not free memory.
			 */
			WARN(1,
			     "Could not allocate stat file for cpu %d\n",
			     cpu);
			return;
		}
		stat->stat = function_stats;
		snprintf(name, 32, "function%d", cpu);
		stat->stat.name = name;
		ret = register_stat_tracer(&stat->stat);
		if (ret) {
			WARN(1,
			     "Could not register function stat for cpu %d\n",
			     cpu);
			kfree(name);
			return;
		}
918
919
920
921
922
923
924
925
926
927
	}

	entry = debugfs_create_file("function_profile_enabled", 0644,
				    d_tracer, NULL, &ftrace_profile_fops);
	if (!entry)
		pr_warning("Could not create debugfs "
			   "'function_profile_enabled' entry\n");
}

#else /* CONFIG_FUNCTION_PROFILER */
928
static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
929
930
931
932
{
}
#endif /* CONFIG_FUNCTION_PROFILER */

933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
static struct pid * const ftrace_swapper_pid = &init_struct_pid;

#ifdef CONFIG_DYNAMIC_FTRACE

#ifndef CONFIG_FTRACE_MCOUNT_RECORD
# error Dynamic ftrace depends on MCOUNT_RECORD
#endif

static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;

struct ftrace_func_probe {
	struct hlist_node	node;
	struct ftrace_probe_ops	*ops;
	unsigned long		flags;
	unsigned long		ip;
	void			*data;
	struct rcu_head		rcu;
};

952
953
954
955
956
957
958
959
960
struct ftrace_func_entry {
	struct hlist_node hlist;
	unsigned long ip;
};

struct ftrace_hash {
	unsigned long		size_bits;
	struct hlist_head	*buckets;
	unsigned long		count;
961
	struct rcu_head		rcu;
962
963
};

964
965
966
967
968
969
970
971
972
/*
 * We make these constant because no one should touch them,
 * but they are used as the default "empty hash", to avoid allocating
 * it all the time. These are in a read only section such that if
 * anyone does try to modify it, it will cause an exception.
 */
static const struct hlist_head empty_buckets[1];
static const struct ftrace_hash empty_hash = {
	.buckets = (struct hlist_head *)empty_buckets,
973
};
974
#define EMPTY_HASH	((struct ftrace_hash *)&empty_hash)
975

976
static struct ftrace_ops global_ops = {
977
	.func			= ftrace_stub,
978
979
	.notrace_hash		= EMPTY_HASH,
	.filter_hash		= EMPTY_HASH,
980
981
};

982
983
984
985
static DEFINE_MUTEX(ftrace_regex_lock);

struct ftrace_page {
	struct ftrace_page	*next;
986
	struct dyn_ftrace	*records;
987
	int			index;
988
	int			size;
989
990
};

991
992
static struct ftrace_page *ftrace_new_pgs;

993
994
#define ENTRY_SIZE sizeof(struct dyn_ftrace)
#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
995
996
997
998
999
1000
1001

/* estimate from running different kernels */
#define NR_TO_INIT		10000

static struct ftrace_page	*ftrace_pages_start;
static struct ftrace_page	*ftrace_pages;

1002
1003
1004
1005
1006
static bool ftrace_hash_empty(struct ftrace_hash *hash)
{
	return !hash || !hash->count;
}

1007
1008
1009
1010
1011
1012
1013
1014
static struct ftrace_func_entry *
ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
{
	unsigned long key;
	struct ftrace_func_entry *entry;
	struct hlist_head *hhd;
	struct hlist_node *n;

1015
	if (ftrace_hash_empty(hash))
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
		return NULL;

	if (hash->size_bits > 0)
		key = hash_long(ip, hash->size_bits);
	else
		key = 0;

	hhd = &hash->buckets[key];

	hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
		if (entry->ip == ip)
			return entry;
	}
	return NULL;
}

1032
1033
static void __add_hash_entry(struct ftrace_hash *hash,
			     struct ftrace_func_entry *entry)
1034
1035
1036
1037
1038
{
	struct hlist_head *hhd;
	unsigned long key;

	if (hash->size_bits)
1039
		key = hash_long(entry->ip, hash->size_bits);
1040
1041
1042
1043
1044
1045
	else
		key = 0;

	hhd = &hash->buckets[key];
	hlist_add_head(&entry->hlist, hhd);
	hash->count++;
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
}

static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
{
	struct ftrace_func_entry *entry;

	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
	if (!entry)
		return -ENOMEM;

	entry->ip = ip;
	__add_hash_entry(hash, entry);
1058
1059
1060
1061
1062

	return 0;
}

static void
1063
free_hash_entry(struct ftrace_hash *hash,
1064
1065
1066
1067
1068
1069
1070
		  struct ftrace_func_entry *entry)
{
	hlist_del(&entry->hlist);
	kfree(entry);
	hash->count--;
}

1071
1072
1073
1074
1075
1076
1077
1078
static void
remove_hash_entry(struct ftrace_hash *hash,
		  struct ftrace_func_entry *entry)
{
	hlist_del(&entry->hlist);
	hash->count--;
}

1079
1080
1081
1082
1083
1084
1085
1086
static void ftrace_hash_clear(struct ftrace_hash *hash)
{
	struct hlist_head *hhd;
	struct hlist_node *tp, *tn;
	struct ftrace_func_entry *entry;
	int size = 1 << hash->size_bits;
	int i;

1087
1088
1089
	if (!hash->count)
		return;

1090
1091
1092
	for (i = 0; i < size; i++) {
		hhd = &hash->buckets[i];
		hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1093
			free_hash_entry(hash, entry);
1094
1095
1096
1097
	}
	FTRACE_WARN_ON(hash->count);
}

1098
1099
1100
1101
1102
1103
1104
1105
1106
static void free_ftrace_hash(struct ftrace_hash *hash)
{
	if (!hash || hash == EMPTY_HASH)
		return;
	ftrace_hash_clear(hash);
	kfree(hash->buckets);
	kfree(hash);
}

1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
{
	struct ftrace_hash *hash;

	hash = container_of(rcu, struct ftrace_hash, rcu);
	free_ftrace_hash(hash);
}

static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
{
	if (!hash || hash == EMPTY_HASH)
		return;
	call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
}

1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
{
	struct ftrace_hash *hash;
	int size;

	hash = kzalloc(sizeof(*hash), GFP_KERNEL);
	if (!hash)
		return NULL;

	size = 1 << size_bits;
	hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);

	if (!hash->buckets) {
		kfree(hash);
		return NULL;
	}

	hash->size_bits = size_bits;

	return hash;
}

static struct ftrace_hash *
alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
{
	struct ftrace_func_entry *entry;
	struct ftrace_hash *new_hash;
	struct hlist_node *tp;
	int size;
	int ret;
	int i;

	new_hash = alloc_ftrace_hash(size_bits);
	if (!new_hash)
		return NULL;

	/* Empty hash? */
1159
	if (ftrace_hash_empty(hash))
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
		return new_hash;

	size = 1 << hash->size_bits;
	for (i = 0; i < size; i++) {
		hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
			ret = add_hash_entry(new_hash, entry->ip);
			if (ret < 0)
				goto free_hash;
		}
	}

	FTRACE_WARN_ON(new_hash->count != hash->count);

	return new_hash;

 free_hash:
	free_ftrace_hash(new_hash);
	return NULL;
}

1180
1181
1182
1183
1184
static void
ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
static void
ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);

1185
static int
1186
1187
ftrace_hash_move(struct ftrace_ops *ops, int enable,
		 struct ftrace_hash **dst, struct ftrace_hash *src)
1188
1189
1190
1191
{
	struct ftrace_func_entry *entry;
	struct hlist_node *tp, *tn;
	struct hlist_head *hhd;
1192
1193
	struct ftrace_hash *old_hash;
	struct ftrace_hash *new_hash;
1194
1195
1196
	unsigned long key;
	int size = src->count;
	int bits = 0;
1197
	int ret;
1198
1199
	int i;

1200
1201
1202
1203
1204
1205
	/*
	 * Remove the current set, update the hash and add
	 * them back.
	 */
	ftrace_hash_rec_disable(ops, enable);

1206
1207
1208
1209
1210
	/*
	 * If the new source is empty, just free dst and assign it
	 * the empty_hash.
	 */
	if (!src->count) {
1211
1212
		free_ftrace_hash_rcu(*dst);
		rcu_assign_pointer(*dst, EMPTY_HASH);
1213
1214
1215
		/* still need to update the function records */
		ret = 0;
		goto out;
1216
1217
1218
1219
1220
1221
1222