ABI runtime fixes
[lttng-modules.git] / ltt-events.c
1 /*
2 * ltt-events.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng per-session event registry.
7 */
8
9 #include <linux/module.h>
10 #include <linux/list.h>
11 #include <linux/mutex.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h> /* For vmalloc_sync_all */
15 #include "ltt-events.h"
16
17 static LIST_HEAD(sessions);
18 static LIST_HEAD(ltt_transport_list);
19 static DEFINE_MUTEX(sessions_mutex);
20 static struct kmem_cache *event_cache;
21
22 static void synchronize_trace(void)
23 {
24 synchronize_sched();
25 #ifdef CONFIG_PREEMPT_RT
26 synchronize_rcu();
27 #endif
28 }
29
30 struct ltt_session *ltt_session_create(void)
31 {
32 struct ltt_session *session;
33
34 mutex_lock(&sessions_mutex);
35 session = kzalloc(sizeof(struct ltt_session), GFP_KERNEL);
36 if (!session)
37 return NULL;
38 INIT_LIST_HEAD(&session->chan);
39 INIT_LIST_HEAD(&session->events);
40 list_add(&session->list, &sessions);
41 mutex_unlock(&sessions_mutex);
42 return session;
43 }
44
45 void ltt_session_destroy(struct ltt_session *session)
46 {
47 struct ltt_channel *chan, *tmpchan;
48 struct ltt_event *event, *tmpevent;
49
50 mutex_lock(&sessions_mutex);
51 session->active = 0;
52 synchronize_trace(); /* Wait for in-flight events to complete */
53 list_for_each_entry_safe(event, tmpevent, &session->events, list)
54 _ltt_event_destroy(event);
55 list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
56 _ltt_channel_destroy(chan);
57 list_del(&session->list);
58 mutex_unlock(&sessions_mutex);
59 kfree(session);
60 }
61
62 int ltt_session_start(struct ltt_session *session)
63 {
64 int ret = 0;
65
66 mutex_lock(&sessions_mutex);
67 if (session->active) {
68 ret = -EBUSY;
69 goto end;
70 }
71 session->active = 1;
72 synchronize_trace(); /* Wait for in-flight events to complete */
73 end:
74 mutex_unlock(&sessions_mutex);
75 return ret;
76 }
77
78 int ltt_session_stop(struct ltt_session *session)
79 {
80 int ret = 0;
81
82 mutex_lock(&sessions_mutex);
83 if (!session->active) {
84 ret = -EBUSY;
85 goto end;
86 }
87 session->active = 0;
88 synchronize_trace(); /* Wait for in-flight events to complete */
89 end:
90 mutex_unlock(&sessions_mutex);
91 return ret;
92 }
93
94 static struct ltt_transport *ltt_transport_find(char *name)
95 {
96 struct ltt_transport *transport;
97
98 list_for_each_entry(transport, &ltt_transport_list, node) {
99 if (!strcmp(transport->name, name))
100 return transport;
101 }
102 return NULL;
103 }
104
105 struct ltt_channel *ltt_channel_create(struct ltt_session *session,
106 int overwrite, void *buf_addr,
107 size_t subbuf_size, size_t num_subbuf,
108 unsigned int switch_timer_interval,
109 unsigned int read_timer_interval)
110 {
111 struct ltt_channel *chan;
112 struct ltt_transport *transport;
113 char *transport_name;
114
115 mutex_lock(&sessions_mutex);
116 if (session->active) {
117 printk(KERN_WARNING "LTTng refusing to add channel to active session\n");
118 goto active; /* Refuse to add channel to active session */
119 }
120 transport_name = overwrite ? "relay-overwrite" : "relay-discard";
121 transport = ltt_transport_find(transport_name);
122 if (!transport) {
123 printk(KERN_WARNING "LTTng transport %s not found\n",
124 transport_name);
125 goto notransport;
126 }
127 printk("got transport\n");
128 chan = kzalloc(sizeof(struct ltt_channel), GFP_KERNEL);
129 if (!chan)
130 goto nomem;
131 chan->session = session;
132 init_waitqueue_head(&chan->notify_wait);
133 chan->chan = transport->ops.channel_create("[lttng]", session, buf_addr,
134 subbuf_size, num_subbuf, switch_timer_interval,
135 read_timer_interval);
136 printk("chan create %p\n", chan->chan);
137 if (!chan->chan)
138 goto create_error;
139 chan->ops = &transport->ops;
140 list_add(&chan->list, &session->chan);
141 mutex_unlock(&sessions_mutex);
142 return chan;
143
144 create_error:
145 kfree(chan);
146 nomem:
147 notransport:
148 active:
149 mutex_unlock(&sessions_mutex);
150 return NULL;
151 }
152
153 /*
154 * Only used internally at session destruction.
155 */
156 void _ltt_channel_destroy(struct ltt_channel *chan)
157 {
158 chan->ops->channel_destroy(chan->chan);
159 list_del(&chan->list);
160 kfree(chan);
161 }
162
163 /*
164 * Supports event creation while tracing session is active.
165 */
166 struct ltt_event *ltt_event_create(struct ltt_channel *chan, char *name,
167 enum instrum_type itype,
168 void *probe, void *filter)
169 {
170 struct ltt_event *event;
171 int ret;
172
173 mutex_lock(&sessions_mutex);
174 if (chan->free_event_id == -1UL)
175 goto full;
176 /*
177 * This is O(n^2) (for each event loop called at event creation).
178 * Might require a hash if we have lots of events.
179 */
180 list_for_each_entry(event, &chan->session->events, list)
181 if (!strcmp(event->name, name))
182 goto exist;
183 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
184 if (!event)
185 goto cache_error;
186 event->name = kmalloc(strlen(name) + 1, GFP_KERNEL);
187 if (!event->name)
188 goto name_error;
189 strcpy(event->name, name);
190 event->chan = chan;
191 event->probe = probe;
192 event->filter = filter;
193 event->id = chan->free_event_id++;
194 event->itype = itype;
195 /* Populate ltt_event structure before tracepoint registration. */
196 smp_wmb();
197 switch (itype) {
198 case INSTRUM_TRACEPOINTS:
199 ret = tracepoint_probe_register(name, probe, event);
200 if (ret)
201 goto register_error;
202 break;
203 default:
204 WARN_ON_ONCE(1);
205 }
206 mutex_unlock(&sessions_mutex);
207 return event;
208
209 register_error:
210 kfree(event->name);
211 name_error:
212 kmem_cache_free(event_cache, event);
213 cache_error:
214 exist:
215 full:
216 mutex_unlock(&sessions_mutex);
217 return NULL;
218 }
219
220 /*
221 * Only used internally at session destruction.
222 */
223 int _ltt_event_destroy(struct ltt_event *event)
224 {
225 int ret = -EINVAL;
226
227 switch (event->itype) {
228 case INSTRUM_TRACEPOINTS:
229 ret = tracepoint_probe_unregister(event->name, event->probe,
230 event);
231 if (ret)
232 return ret;
233 break;
234 default:
235 WARN_ON_ONCE(1);
236 }
237 kfree(event->name);
238 kmem_cache_free(event_cache, event);
239 return ret;
240 }
241
242 /**
243 * ltt_transport_register - LTT transport registration
244 * @transport: transport structure
245 *
246 * Registers a transport which can be used as output to extract the data out of
247 * LTTng. The module calling this registration function must ensure that no
248 * trap-inducing code will be executed by the transport functions. E.g.
249 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
250 * is made visible to the transport function. This registration acts as a
251 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
252 * after its registration must it synchronize the TLBs.
253 */
254 void ltt_transport_register(struct ltt_transport *transport)
255 {
256 /*
257 * Make sure no page fault can be triggered by the module about to be
258 * registered. We deal with this here so we don't have to call
259 * vmalloc_sync_all() in each module's init.
260 */
261 vmalloc_sync_all();
262
263 mutex_lock(&sessions_mutex);
264 list_add_tail(&transport->node, &ltt_transport_list);
265 mutex_unlock(&sessions_mutex);
266 }
267 EXPORT_SYMBOL_GPL(ltt_transport_register);
268
269 /**
270 * ltt_transport_unregister - LTT transport unregistration
271 * @transport: transport structure
272 */
273 void ltt_transport_unregister(struct ltt_transport *transport)
274 {
275 mutex_lock(&sessions_mutex);
276 list_del(&transport->node);
277 mutex_unlock(&sessions_mutex);
278 }
279 EXPORT_SYMBOL_GPL(ltt_transport_unregister);
280
281
282 static int __init ltt_events_init(void)
283 {
284 int ret;
285
286 event_cache = KMEM_CACHE(ltt_event, 0);
287 if (!event_cache)
288 return -ENOMEM;
289 ret = ltt_debugfs_abi_init();
290 if (ret)
291 goto error;
292 return 0;
293 error:
294 kmem_cache_destroy(event_cache);
295 return ret;
296 }
297
298 module_init(ltt_events_init);
299
300 static void __exit ltt_events_exit(void)
301 {
302 struct ltt_session *session, *tmpsession;
303
304 ltt_debugfs_abi_exit();
305 list_for_each_entry_safe(session, tmpsession, &sessions, list)
306 ltt_session_destroy(session);
307 kmem_cache_destroy(event_cache);
308 }
309
310 module_exit(ltt_events_exit);
311
312 MODULE_LICENSE("GPL and additional rights");
313 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
314 MODULE_DESCRIPTION("LTTng Events");
This page took 0.035846 seconds and 4 git commands to generate.