d9c60f80aa0d20958c647a86310b29701b4bcdcc
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / trace / trace_event_profile.c
1 /*
2  * trace event based perf counter profiling
3  *
4  * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
5  *
6  */
7
8 #include <linux/module.h>
9 #include "trace.h"
10
11
12 char *perf_trace_buf;
13 EXPORT_SYMBOL_GPL(perf_trace_buf);
14
15 char *perf_trace_buf_nmi;
16 EXPORT_SYMBOL_GPL(perf_trace_buf_nmi);
17
18 typedef typeof(char [FTRACE_MAX_PROFILE_SIZE]) perf_trace_t ;
19
20 /* Count the events in use (per event id, not per instance) */
21 static int      total_profile_count;
22
23 static int ftrace_profile_enable_event(struct ftrace_event_call *event)
24 {
25         char *buf;
26         int ret = -ENOMEM;
27
28         if (atomic_inc_return(&event->profile_count))
29                 return 0;
30
31         if (!total_profile_count) {
32                 buf = (char *)alloc_percpu(perf_trace_t);
33                 if (!buf)
34                         goto fail_buf;
35
36                 rcu_assign_pointer(perf_trace_buf, buf);
37
38                 buf = (char *)alloc_percpu(perf_trace_t);
39                 if (!buf)
40                         goto fail_buf_nmi;
41
42                 rcu_assign_pointer(perf_trace_buf_nmi, buf);
43         }
44
45         ret = event->profile_enable(event);
46         if (!ret) {
47                 total_profile_count++;
48                 return 0;
49         }
50
51 fail_buf_nmi:
52         if (!total_profile_count) {
53                 free_percpu(perf_trace_buf_nmi);
54                 free_percpu(perf_trace_buf);
55                 perf_trace_buf_nmi = NULL;
56                 perf_trace_buf = NULL;
57         }
58 fail_buf:
59         atomic_dec(&event->profile_count);
60
61         return ret;
62 }
63
64 int ftrace_profile_enable(int event_id)
65 {
66         struct ftrace_event_call *event;
67         int ret = -EINVAL;
68
69         mutex_lock(&event_mutex);
70         list_for_each_entry(event, &ftrace_events, list) {
71                 if (event->id == event_id && event->profile_enable &&
72                     try_module_get(event->mod)) {
73                         ret = ftrace_profile_enable_event(event);
74                         break;
75                 }
76         }
77         mutex_unlock(&event_mutex);
78
79         return ret;
80 }
81
82 static void ftrace_profile_disable_event(struct ftrace_event_call *event)
83 {
84         char *buf, *nmi_buf;
85
86         if (!atomic_add_negative(-1, &event->profile_count))
87                 return;
88
89         event->profile_disable(event);
90
91         if (!--total_profile_count) {
92                 buf = perf_trace_buf;
93                 rcu_assign_pointer(perf_trace_buf, NULL);
94
95                 nmi_buf = perf_trace_buf_nmi;
96                 rcu_assign_pointer(perf_trace_buf_nmi, NULL);
97
98                 /*
99                  * Ensure every events in profiling have finished before
100                  * releasing the buffers
101                  */
102                 synchronize_sched();
103
104                 free_percpu(buf);
105                 free_percpu(nmi_buf);
106         }
107 }
108
109 void ftrace_profile_disable(int event_id)
110 {
111         struct ftrace_event_call *event;
112
113         mutex_lock(&event_mutex);
114         list_for_each_entry(event, &ftrace_events, list) {
115                 if (event->id == event_id) {
116                         ftrace_profile_disable_event(event);
117                         module_put(event->mod);
118                         break;
119                 }
120         }
121         mutex_unlock(&event_mutex);
122 }