| 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 "ltt-events.h" |
| 11 | |
| 12 | static LIST_HEAD(sessions); |
| 13 | static DEFINE_MUTEX(sessions_mutex); |
| 14 | static struct kmem_cache *event_cache; |
| 15 | |
| 16 | static void synchronize_trace(void) |
| 17 | { |
| 18 | synchronize_sched(); |
| 19 | #ifdef CONFIG_PREEMPT_RT |
| 20 | synchronize_rcu(); |
| 21 | #endif |
| 22 | } |
| 23 | |
| 24 | struct ltt_session *ltt_session_create(void) |
| 25 | { |
| 26 | struct ltt_session *session; |
| 27 | |
| 28 | mutex_lock(&sessions_mutex); |
| 29 | session = kmalloc(sizeof(struct ltt_session)); |
| 30 | if (!session) |
| 31 | return NULL; |
| 32 | INIT_LIST_HEAD(&session->chan); |
| 33 | list_add(&session->list, &sessions); |
| 34 | mutex_unlock(&sessions_mutex); |
| 35 | return session; |
| 36 | |
| 37 | exist: |
| 38 | mutex_unlock(&sessions_mutex); |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | int ltt_session_destroy(struct ltt_session *session) |
| 43 | { |
| 44 | struct ltt_channel *chan, *tmpchan; |
| 45 | struct ltt_event *event, *tmpevent; |
| 46 | |
| 47 | mutex_lock(&sessions_mutex); |
| 48 | session->active = 0; |
| 49 | synchronize_trace(); /* Wait for in-flight events to complete */ |
| 50 | list_for_each_entry_safe(event, tmpevent, &session->events, list) |
| 51 | _ltt_event_destroy(event); |
| 52 | list_for_each_entry_safe(chan, tmpchan, &session->chan, list) |
| 53 | _ltt_channel_destroy(chan); |
| 54 | list_del(&session->list); |
| 55 | mutex_unlock(&sessions_mutex); |
| 56 | kfree(session); |
| 57 | } |
| 58 | |
| 59 | struct ltt_channel *ltt_channel_create(struct ltt_session *session, |
| 60 | int overwrite, void *buf_addr, |
| 61 | size_t subbuf_size, size_t num_subbuf, |
| 62 | unsigned int switch_timer_interval, |
| 63 | unsigned int read_timer_interval) |
| 64 | { |
| 65 | struct ltt_channel *chan; |
| 66 | |
| 67 | mutex_lock(&sessions_mutex); |
| 68 | if (session->active) |
| 69 | goto active; /* Refuse to add channel to active session */ |
| 70 | chan = kmalloc(sizeof(struct ltt_channel), GFP_KERNEL); |
| 71 | if (!chan) |
| 72 | return NULL; |
| 73 | chan->session = session; |
| 74 | init_waitqueue_head(&chan->notify_wait); |
| 75 | |
| 76 | /* TODO: create rb channel */ |
| 77 | list_add(&chan->list, &session->chan); |
| 78 | mutex_unlock(&sessions_mutex); |
| 79 | return chan; |
| 80 | |
| 81 | exist: |
| 82 | active: |
| 83 | mutex_unlock(&sessions_mutex); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Only used internally at session destruction. |
| 89 | */ |
| 90 | int _ltt_channel_destroy(struct ltt_channel *chan) |
| 91 | { |
| 92 | /* TODO: destroy rb channel */ |
| 93 | list_del(&chan->list); |
| 94 | kfree(chan); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Supports event creation while tracing session is active. |
| 99 | */ |
| 100 | struct ltt_event *ltt_event_create(struct ltt_channel *chan, char *name, |
| 101 | enum instrum_type itype, |
| 102 | void *probe, void *filter) |
| 103 | { |
| 104 | struct ltt_event *event; |
| 105 | int ret; |
| 106 | |
| 107 | mutex_lock(&sessions_mutex); |
| 108 | if (chan->free_event_id == -1UL) |
| 109 | goto full; |
| 110 | /* |
| 111 | * This is O(n^2) (for each event loop called at event creation). |
| 112 | * Might require a hash if we have lots of events. |
| 113 | */ |
| 114 | list_for_each_entry(event, &chan->session->events, list) |
| 115 | if (!strcmp(event->name, name)) |
| 116 | goto exist; |
| 117 | event = kmem_cache_zalloc(events_cache, GFP_KERNEL); |
| 118 | if (!event) |
| 119 | goto cache_error; |
| 120 | event->name = kmalloc(strlen(name) + 1, GFP_KERNEL); |
| 121 | if (!event->name) |
| 122 | goto error; |
| 123 | strcpy(event->name, name); |
| 124 | event->chan = chan; |
| 125 | event->probe = probe; |
| 126 | event->filter = filter; |
| 127 | event->id = chan->free_event_id++; |
| 128 | event->itype = itype; |
| 129 | mutex_unlock(&sessions_mutex); |
| 130 | /* Populate ltt_event structure before tracepoint registration. */ |
| 131 | smp_wmb(); |
| 132 | switch (itype) { |
| 133 | case INSTRUM_TRACEPOINTS: |
| 134 | ret = tracepoint_probe_register(name, probe, event); |
| 135 | break; |
| 136 | default: |
| 137 | WARN_ON_ONCE(1); |
| 138 | } |
| 139 | return event; |
| 140 | |
| 141 | error: |
| 142 | kmem_cache_free(event); |
| 143 | cache_error: |
| 144 | exist: |
| 145 | full: |
| 146 | mutex_unlock(&sessions_mutex); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Only used internally at session destruction. |
| 152 | */ |
| 153 | int _ltt_event_destroy(struct ltt_event *event) |
| 154 | { |
| 155 | switch (event->itype) { |
| 156 | case INSTRUM_TRACEPOINTS: |
| 157 | ret = tracepoint_probe_unregister(name, event->probe, event); |
| 158 | break; |
| 159 | default: |
| 160 | WARN_ON_ONCE(1); |
| 161 | } |
| 162 | kfree(event->name); |
| 163 | kmem_cache_free(event); |
| 164 | } |
| 165 | |
| 166 | static int __init ltt_events_init(void) |
| 167 | { |
| 168 | int ret; |
| 169 | |
| 170 | events_cache = KMEM_CACHE(ltt_event, 0); |
| 171 | if (!events_cache) |
| 172 | return -ENOMEM; |
| 173 | |
| 174 | /* TODO: show ABI to userspace */ |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static void __exit ltt_events_exit(void) |
| 180 | { |
| 181 | struct ltt_session *session, *tmpsession; |
| 182 | |
| 183 | /* TODO: hide ABI from userspace, wait for callers to release refs. */ |
| 184 | |
| 185 | list_for_each_entry_safe(session, tmpsession, &sessions, list) |
| 186 | ltt_session_destroy(session); |
| 187 | kmem_cache_destroy(events_cache); |
| 188 | } |
| 189 | |
| 190 | MODULE_LICENSE("GPL and additional rights"); |
| 191 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
| 192 | MODULE_DESCRIPTION("LTTng Events"); |