Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / lttng / probes / lttng-ftrace.c
1 /*
2  * probes/lttng-ftrace.c
3  *
4  * LTTng function tracer integration module.
5  *
6  * Copyright (C) 2009-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; only
11  * version 2.1 of the License.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /*
24  * Ftrace function tracer does not seem to provide synchronization between probe
25  * teardown and callback execution. Therefore, we make this module permanently
26  * loaded (unloadable).
27  *
28  * TODO: Move to register_ftrace_function() (which is exported for
29  * modules) for Linux >= 3.0. It is faster (only enables the selected
30  * functions), and will stay there.
31  */
32
33 #include <linux/module.h>
34 #include <linux/ftrace.h>
35 #include <linux/slab.h>
36 #include "../lttng-events.h"
37 #include "../wrapper/ringbuffer/frontend_types.h"
38 #include "../wrapper/ftrace.h"
39 #include "../wrapper/vmalloc.h"
40 #include "../lttng-tracer.h"
41
42 static
43 void lttng_ftrace_handler(unsigned long ip, unsigned long parent_ip, void **data)
44 {
45         struct lttng_event *event = *data;
46         struct lttng_channel *chan = event->chan;
47         struct lib_ring_buffer_ctx ctx;
48         struct {
49                 unsigned long ip;
50                 unsigned long parent_ip;
51         } payload;
52         int ret;
53
54         if (unlikely(!ACCESS_ONCE(chan->session->active)))
55                 return;
56         if (unlikely(!ACCESS_ONCE(chan->enabled)))
57                 return;
58         if (unlikely(!ACCESS_ONCE(event->enabled)))
59                 return;
60
61         lib_ring_buffer_ctx_init(&ctx, chan->chan, event,
62                                  sizeof(payload), lttng_alignof(payload), -1);
63         ret = chan->ops->event_reserve(&ctx, event->id);
64         if (ret < 0)
65                 return;
66         payload.ip = ip;
67         payload.parent_ip = parent_ip;
68         lib_ring_buffer_align_ctx(&ctx, lttng_alignof(payload));
69         chan->ops->event_write(&ctx, &payload, sizeof(payload));
70         chan->ops->event_commit(&ctx);
71         return;
72 }
73
74 /*
75  * Create event description
76  */
77 static
78 int lttng_create_ftrace_event(const char *name, struct lttng_event *event)
79 {
80         struct lttng_event_field *fields;
81         struct lttng_event_desc *desc;
82         int ret;
83
84         desc = kzalloc(sizeof(*event->desc), GFP_KERNEL);
85         if (!desc)
86                 return -ENOMEM;
87         desc->name = kstrdup(name, GFP_KERNEL);
88         if (!desc->name) {
89                 ret = -ENOMEM;
90                 goto error_str;
91         }
92         desc->nr_fields = 2;
93         desc->fields = fields =
94                 kzalloc(2 * sizeof(struct lttng_event_field), GFP_KERNEL);
95         if (!desc->fields) {
96                 ret = -ENOMEM;
97                 goto error_fields;
98         }
99         fields[0].name = "ip";
100         fields[0].type.atype = atype_integer;
101         fields[0].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
102         fields[0].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
103         fields[0].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
104         fields[0].type.u.basic.integer.reverse_byte_order = 0;
105         fields[0].type.u.basic.integer.base = 16;
106         fields[0].type.u.basic.integer.encoding = lttng_encode_none;
107
108         fields[1].name = "parent_ip";
109         fields[1].type.atype = atype_integer;
110         fields[1].type.u.basic.integer.size = sizeof(unsigned long) * CHAR_BIT;
111         fields[1].type.u.basic.integer.alignment = lttng_alignof(unsigned long) * CHAR_BIT;
112         fields[1].type.u.basic.integer.signedness = lttng_is_signed_type(unsigned long);
113         fields[1].type.u.basic.integer.reverse_byte_order = 0;
114         fields[1].type.u.basic.integer.base = 16;
115         fields[1].type.u.basic.integer.encoding = lttng_encode_none;
116
117         desc->owner = THIS_MODULE;
118         event->desc = desc;
119
120         return 0;
121
122 error_fields:
123         kfree(desc->name);
124 error_str:
125         kfree(desc);
126         return ret;
127 }
128
129 static
130 struct ftrace_probe_ops lttng_ftrace_ops = {
131         .func = lttng_ftrace_handler,
132 };
133
134 int lttng_ftrace_register(const char *name,
135                           const char *symbol_name,
136                           struct lttng_event *event)
137 {
138         int ret;
139
140         ret = lttng_create_ftrace_event(name, event);
141         if (ret)
142                 goto error;
143
144         event->u.ftrace.symbol_name = kstrdup(symbol_name, GFP_KERNEL);
145         if (!event->u.ftrace.symbol_name)
146                 goto name_error;
147
148         /* Ensure the memory we just allocated don't trigger page faults */
149         wrapper_vmalloc_sync_all();
150
151         ret = wrapper_register_ftrace_function_probe(event->u.ftrace.symbol_name,
152                         &lttng_ftrace_ops, event);
153         if (ret < 0)
154                 goto register_error;
155         return 0;
156
157 register_error:
158         kfree(event->u.ftrace.symbol_name);
159 name_error:
160         kfree(event->desc->name);
161         kfree(event->desc);
162 error:
163         return ret;
164 }
165 EXPORT_SYMBOL_GPL(lttng_ftrace_register);
166
167 void lttng_ftrace_unregister(struct lttng_event *event)
168 {
169         wrapper_unregister_ftrace_function_probe(event->u.ftrace.symbol_name,
170                         &lttng_ftrace_ops, event);
171 }
172 EXPORT_SYMBOL_GPL(lttng_ftrace_unregister);
173
174 void lttng_ftrace_destroy_private(struct lttng_event *event)
175 {
176         kfree(event->u.ftrace.symbol_name);
177         kfree(event->desc->fields);
178         kfree(event->desc->name);
179         kfree(event->desc);
180 }
181 EXPORT_SYMBOL_GPL(lttng_ftrace_destroy_private);
182
183 int lttng_ftrace_init(void)
184 {
185         wrapper_vmalloc_sync_all();
186         return 0;
187 }
188 module_init(lttng_ftrace_init)
189
190 /*
191  * Ftrace takes care of waiting for a grace period (RCU sched) at probe
192  * unregistration, and disables preemption around probe call.
193  */
194 void lttng_ftrace_exit(void)
195 {
196 }
197 module_exit(lttng_ftrace_exit)
198
199 MODULE_LICENSE("GPL and additional rights");
200 MODULE_AUTHOR("Mathieu Desnoyers");
201 MODULE_DESCRIPTION("Linux Trace Toolkit Ftrace Support");