Compilation fixes
[lttng-modules.git] / ltt-events.c
index 3060c07e05d2386d8d90d0482b3040d3d0f970b9..7c7cda6525a4250379521ddf40b4a4bcce389dc2 100644 (file)
@@ -7,9 +7,15 @@
  */
 
 #include <linux/module.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>     /* For vmalloc_sync_all */
 #include "ltt-events.h"
 
 static LIST_HEAD(sessions);
+static LIST_HEAD(ltt_transport_list);
 static DEFINE_MUTEX(sessions_mutex);
 static struct kmem_cache *event_cache;
 
@@ -26,20 +32,16 @@ struct ltt_session *ltt_session_create(void)
        struct ltt_session *session;
 
        mutex_lock(&sessions_mutex);
-       session = kmalloc(sizeof(struct ltt_session));
+       session = kmalloc(sizeof(struct ltt_session), GFP_KERNEL);
        if (!session)
                return NULL;
        INIT_LIST_HEAD(&session->chan);
        list_add(&session->list, &sessions);
        mutex_unlock(&sessions_mutex);
        return session;
-
-exist:
-       mutex_unlock(&sessions_mutex);
-       return NULL;
 }
 
-int ltt_session_destroy(struct ltt_session *session)
+void ltt_session_destroy(struct ltt_session *session)
 {
        struct ltt_channel *chan, *tmpchan;
        struct ltt_event *event, *tmpevent;
@@ -56,6 +58,38 @@ int ltt_session_destroy(struct ltt_session *session)
        kfree(session);
 }
 
+int ltt_session_start(struct ltt_session *session)
+{
+       int ret = 0;
+
+       mutex_lock(&sessions_mutex);
+       if (session->active) {
+               ret = -EBUSY;
+               goto end;
+       }
+       session->active = 1;
+       synchronize_trace();    /* Wait for in-flight events to complete */
+end:
+       mutex_unlock(&sessions_mutex);
+       return ret;
+}
+
+int ltt_session_stop(struct ltt_session *session)
+{
+       int ret = 0;
+
+       mutex_lock(&sessions_mutex);
+       if (!session->active) {
+               ret = -EBUSY;
+               goto end;
+       }
+       session->active = 0;
+       synchronize_trace();    /* Wait for in-flight events to complete */
+end:
+       mutex_unlock(&sessions_mutex);
+       return ret;
+}
+
 struct ltt_channel *ltt_channel_create(struct ltt_session *session,
                                       int overwrite, void *buf_addr,
                                       size_t subbuf_size, size_t num_subbuf,
@@ -63,22 +97,36 @@ struct ltt_channel *ltt_channel_create(struct ltt_session *session,
                                       unsigned int read_timer_interval)
 {
        struct ltt_channel *chan;
+       struct ltt_transport *transport = NULL, *tran_iter;
+       char *transport_name;
 
        mutex_lock(&sessions_mutex);
        if (session->active)
                goto active;    /* Refuse to add channel to active session */
+       transport_name = overwrite ? "relay-overwrite" : "relay-discard";
+       list_for_each_entry(tran_iter, &ltt_transport_list, node) {
+               if (!strcmp(tran_iter->name, transport_name)) {
+                       transport = tran_iter;
+                       break;
+               }
+       }
+       if (!transport)
+               goto notransport;
        chan = kmalloc(sizeof(struct ltt_channel), GFP_KERNEL);
        if (!chan)
-               return NULL;
+               goto nomem;
        chan->session = session;
        init_waitqueue_head(&chan->notify_wait);
-
-       /* TODO: create rb channel */
+       chan->chan = transport->ops.channel_create("[lttng]", session, buf_addr,
+                       subbuf_size, num_subbuf, switch_timer_interval,
+                       read_timer_interval);
+       chan->ops = &transport->ops;
        list_add(&chan->list, &session->chan);
        mutex_unlock(&sessions_mutex);
        return chan;
 
-exist:
+nomem:
+notransport:
 active:
        mutex_unlock(&sessions_mutex);
        return NULL;
@@ -87,9 +135,9 @@ active:
 /*
  * Only used internally at session destruction.
  */
-int _ltt_channel_destroy(struct ltt_channel *chan)
+void _ltt_channel_destroy(struct ltt_channel *chan)
 {
-       /* TODO: destroy rb channel */
+       chan->ops->channel_destroy(chan->chan);
        list_del(&chan->list);
        kfree(chan);
 }
@@ -114,32 +162,36 @@ struct ltt_event *ltt_event_create(struct ltt_channel *chan, char *name,
        list_for_each_entry(event, &chan->session->events, list)
                if (!strcmp(event->name, name))
                        goto exist;
-       event = kmem_cache_zalloc(events_cache, GFP_KERNEL);
+       event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
        if (!event)
                goto cache_error;
        event->name = kmalloc(strlen(name) + 1, GFP_KERNEL);
        if (!event->name)
-               goto error;
+               goto name_error;
        strcpy(event->name, name);
        event->chan = chan;
        event->probe = probe;
        event->filter = filter;
        event->id = chan->free_event_id++;
        event->itype = itype;
-       mutex_unlock(&sessions_mutex);
        /* Populate ltt_event structure before tracepoint registration. */
        smp_wmb();
        switch (itype) {
        case INSTRUM_TRACEPOINTS:
                ret = tracepoint_probe_register(name, probe, event);
+               if (ret)
+                       goto register_error;
                break;
        default:
                WARN_ON_ONCE(1);
        }
+       mutex_unlock(&sessions_mutex);
        return event;
 
-error:
-       kmem_cache_free(event);
+register_error:
+       kfree(event->name);
+name_error:
+       kmem_cache_free(event_cache, event);
 cache_error:
 exist:
 full:
@@ -152,41 +204,84 @@ full:
  */
 int _ltt_event_destroy(struct ltt_event *event)
 {
+       int ret = -EINVAL;
+
        switch (event->itype) {
        case INSTRUM_TRACEPOINTS:
-               ret = tracepoint_probe_unregister(name, event->probe, event);
+               ret = tracepoint_probe_unregister(event->name, event->probe,
+                                                 event);
+               if (ret)
+                       return ret;
                break;
        default:
                WARN_ON_ONCE(1);
        }
        kfree(event->name);
-       kmem_cache_free(event);
+       kmem_cache_free(event_cache, event);
+       return ret;
 }
 
-static int __init ltt_events_init(void)
+/**
+ * ltt_transport_register - LTT transport registration
+ * @transport: transport structure
+ *
+ * Registers a transport which can be used as output to extract the data out of
+ * LTTng. The module calling this registration function must ensure that no
+ * trap-inducing code will be executed by the transport functions. E.g.
+ * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
+ * is made visible to the transport function. This registration acts as a
+ * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
+ * after its registration must it synchronize the TLBs.
+ */
+void ltt_transport_register(struct ltt_transport *transport)
 {
-       int ret;
+       /*
+        * Make sure no page fault can be triggered by the module about to be
+        * registered. We deal with this here so we don't have to call
+        * vmalloc_sync_all() in each module's init.
+        */
+       vmalloc_sync_all();
 
-       events_cache = KMEM_CACHE(ltt_event, 0);
-       if (!events_cache)
-               return -ENOMEM;
+       mutex_lock(&sessions_mutex);
+       list_add_tail(&transport->node, &ltt_transport_list);
+       mutex_unlock(&sessions_mutex);
+}
+EXPORT_SYMBOL_GPL(ltt_transport_register);
+
+/**
+ * ltt_transport_unregister - LTT transport unregistration
+ * @transport: transport structure
+ */
+void ltt_transport_unregister(struct ltt_transport *transport)
+{
+       mutex_lock(&sessions_mutex);
+       list_del(&transport->node);
+       mutex_unlock(&sessions_mutex);
+}
+EXPORT_SYMBOL_GPL(ltt_transport_unregister);
 
-       /* TODO: show ABI to userspace */
 
+static int __init ltt_events_init(void)
+{
+       event_cache = KMEM_CACHE(ltt_event, 0);
+       if (!event_cache)
+               return -ENOMEM;
        return 0;
 }
 
+module_init(ltt_events_init);
+
 static void __exit ltt_events_exit(void)
 {
        struct ltt_session *session, *tmpsession;
 
-       /* TODO: hide ABI from userspace, wait for callers to release refs. */
-
        list_for_each_entry_safe(session, tmpsession, &sessions, list)
                ltt_session_destroy(session);
-       kmem_cache_destroy(events_cache);
+       kmem_cache_destroy(event_cache);
 }
 
+module_exit(ltt_events_exit);
+
 MODULE_LICENSE("GPL and additional rights");
 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
 MODULE_DESCRIPTION("LTTng Events");
This page took 0.0259 seconds and 4 git commands to generate.