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