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