Version 2.7.7
[lttng-modules.git] / lttng-events.c
index cf2cae0f4f763de80cd378fd7e95a148c7fb3882..992ba58f2a96200650924dd6d0856b513b6eb529 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+/*
+ * This page_alloc.h wrapper needs to be included before gfpflags.h because it
+ * overrides a function with a define.
+ */
+#include "wrapper/page_alloc.h"
+
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/sched.h>
@@ -32,6 +38,8 @@
 #include <linux/anon_inodes.h>
 #include "wrapper/file.h"
 #include <linux/jhash.h>
+#include <linux/uaccess.h>
+#include <linux/vmalloc.h>
 
 #include "wrapper/uuid.h"
 #include "wrapper/vmalloc.h"   /* for wrapper_vmalloc_sync_all() */
 #include "lttng-events.h"
 #include "lttng-tracer.h"
 #include "lttng-abi-old.h"
+#include "lttng-endian.h"
+#include "wrapper/vzalloc.h"
+#include "wrapper/ringbuffer/backend.h"
+#include "wrapper/ringbuffer/frontend.h"
 
 #define METADATA_CACHE_DEFAULT_SIZE 4096
 
@@ -125,12 +137,12 @@ struct lttng_session *lttng_session_create(void)
                        GFP_KERNEL);
        if (!metadata_cache)
                goto err_free_session;
-       metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE,
-                       GFP_KERNEL);
+       metadata_cache->data = lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE);
        if (!metadata_cache->data)
                goto err_free_cache;
        metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
        kref_init(&metadata_cache->refcount);
+       mutex_init(&metadata_cache->lock);
        session->metadata_cache = metadata_cache;
        INIT_LIST_HEAD(&metadata_cache->metadata_stream);
        memcpy(&metadata_cache->uuid, &session->uuid,
@@ -155,7 +167,7 @@ void metadata_cache_destroy(struct kref *kref)
 {
        struct lttng_metadata_cache *cache =
                container_of(kref, struct lttng_metadata_cache, refcount);
-       kfree(cache->data);
+       vfree(cache->data);
        kfree(cache);
 }
 
@@ -227,6 +239,12 @@ int lttng_session_enable(struct lttng_session *session)
        /* We need to sync enablers with session before activation. */
        lttng_session_sync_enablers(session);
 
+       /* Clear each stream's quiescent state. */
+       list_for_each_entry(chan, &session->chan, list) {
+               if (chan->channel_type != METADATA_CHANNEL)
+                       lib_ring_buffer_clear_quiescent_channel(chan->chan);
+       }
+
        ACCESS_ONCE(session->active) = 1;
        ACCESS_ONCE(session->been_active) = 1;
        ret = _lttng_session_metadata_statedump(session);
@@ -245,6 +263,7 @@ end:
 int lttng_session_disable(struct lttng_session *session)
 {
        int ret = 0;
+       struct lttng_channel *chan;
 
        mutex_lock(&sessions_mutex);
        if (!session->active) {
@@ -256,6 +275,12 @@ int lttng_session_disable(struct lttng_session *session)
        /* Set transient enabler state to "disabled" */
        session->tstate = 0;
        lttng_session_sync_enablers(session);
+
+       /* Set each stream's quiescent state. */
+       list_for_each_entry(chan, &session->chan, list) {
+               if (chan->channel_type != METADATA_CHANNEL)
+                       lib_ring_buffer_set_quiescent_channel(chan->chan);
+       }
 end:
        mutex_unlock(&sessions_mutex);
        return ret;
@@ -320,8 +345,23 @@ int lttng_event_enable(struct lttng_event *event)
                ret = -EEXIST;
                goto end;
        }
-       ACCESS_ONCE(event->enabled) = 1;
-       lttng_session_sync_enablers(event->chan->session);
+       switch (event->instrumentation) {
+       case LTTNG_KERNEL_TRACEPOINT:
+       case LTTNG_KERNEL_SYSCALL:
+               ret = -EINVAL;
+               break;
+       case LTTNG_KERNEL_KPROBE:
+       case LTTNG_KERNEL_FUNCTION:
+       case LTTNG_KERNEL_NOOP:
+               ACCESS_ONCE(event->enabled) = 1;
+               break;
+       case LTTNG_KERNEL_KRETPROBE:
+               ret = lttng_kretprobes_event_enable_state(event, 1);
+               break;
+       default:
+               WARN_ON_ONCE(1);
+               ret = -EINVAL;
+       }
 end:
        mutex_unlock(&sessions_mutex);
        return ret;
@@ -340,8 +380,23 @@ int lttng_event_disable(struct lttng_event *event)
                ret = -EEXIST;
                goto end;
        }
-       ACCESS_ONCE(event->enabled) = 0;
-       lttng_session_sync_enablers(event->chan->session);
+       switch (event->instrumentation) {
+       case LTTNG_KERNEL_TRACEPOINT:
+       case LTTNG_KERNEL_SYSCALL:
+               ret = -EINVAL;
+               break;
+       case LTTNG_KERNEL_KPROBE:
+       case LTTNG_KERNEL_FUNCTION:
+       case LTTNG_KERNEL_NOOP:
+               ACCESS_ONCE(event->enabled) = 0;
+               break;
+       case LTTNG_KERNEL_KRETPROBE:
+               ret = lttng_kretprobes_event_enable_state(event, 0);
+               break;
+       default:
+               WARN_ON_ONCE(1);
+               ret = -EINVAL;
+       }
 end:
        mutex_unlock(&sessions_mutex);
        return ret;
@@ -492,7 +547,7 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
        name_len = strlen(event_name);
        hash = jhash(event_name, name_len, 0);
        head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
-       hlist_for_each_entry(event, head, hlist) {
+       lttng_hlist_for_each_entry(event, head, hlist) {
                WARN_ON_ONCE(!event->desc);
                if (!strncmp(event->desc->name, event_name,
                                        LTTNG_KERNEL_SYM_NAME_LEN - 1)
@@ -529,7 +584,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
                smp_wmb();
                break;
        case LTTNG_KERNEL_KPROBE:
-               event->enabled = 1;
+               /*
+                * Needs to be explicitly enabled after creation, since
+                * we may want to apply filters.
+                */
+               event->enabled = 0;
                event->registered = 1;
                /*
                 * Populate lttng_event structure before event
@@ -553,7 +612,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
                struct lttng_event *event_return;
 
                /* kretprobe defines 2 events */
-               event->enabled = 1;
+               /*
+                * Needs to be explicitly enabled after creation, since
+                * we may want to apply filters.
+                */
+               event->enabled = 0;
                event->registered = 1;
                event_return =
                        kmem_cache_zalloc(event_cache, GFP_KERNEL);
@@ -564,7 +627,7 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
                event_return->chan = chan;
                event_return->filter = filter;
                event_return->id = chan->free_event_id++;
-               event_return->enabled = 1;
+               event_return->enabled = 0;
                event_return->registered = 1;
                event_return->instrumentation = itype;
                /*
@@ -599,7 +662,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
                break;
        }
        case LTTNG_KERNEL_FUNCTION:
-               event->enabled = 1;
+               /*
+                * Needs to be explicitly enabled after creation, since
+                * we may want to apply filters.
+                */
+               event->enabled = 0;
                event->registered = 1;
                /*
                 * Populate lttng_event structure before event
@@ -617,7 +684,11 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
                break;
        case LTTNG_KERNEL_NOOP:
        case LTTNG_KERNEL_SYSCALL:
-               event->enabled = 1;
+               /*
+                * Needs to be explicitly enabled after creation, since
+                * we may want to apply filters.
+                */
+               event->enabled = 0;
                event->registered = 0;
                event->desc = event_desc;
                if (!event->desc) {
@@ -637,7 +708,6 @@ struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
        }
        hlist_add_head(&event->hlist, head);
        list_add(&event->list, &chan->session->events);
-       mutex_unlock(&sessions_mutex);
        return event;
 
 statedump_error:
@@ -989,17 +1059,22 @@ int lttng_session_list_tracker_pids(struct lttng_session *session)
                ret = PTR_ERR(tracker_pids_list_file);
                goto file_error;
        }
+       if (atomic_long_add_unless(&session->file->f_count,
+               1, INT_MAX) == INT_MAX) {
+               goto refcount_error;
+       }
        ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file);
        if (ret < 0)
                goto open_error;
        m = tracker_pids_list_file->private_data;
        m->private = session;
        fd_install(file_fd, tracker_pids_list_file);
-       atomic_long_inc(&session->file->f_count);
 
        return file_fd;
 
 open_error:
+       atomic_long_dec(&session->file->f_count);
+refcount_error:
        fput(tracker_pids_list_file);
 file_error:
        put_unused_fd(file_fd);
@@ -1131,7 +1206,7 @@ void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
                         */
                        hash = jhash(event_name, name_len, 0);
                        head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
-                       hlist_for_each_entry(event, head, hlist) {
+                       lttng_hlist_for_each_entry(event, head, hlist) {
                                if (event->desc == desc
                                                && event->chan == enabler->chan)
                                        found = 1;
@@ -1433,10 +1508,12 @@ void lttng_session_lazy_sync_enablers(struct lttng_session *session)
 /*
  * Serialize at most one packet worth of metadata into a metadata
  * channel.
- * We have exclusive access to our metadata buffer (protected by the
- * sessions_mutex), so we can do racy operations such as looking for
- * remaining space left in packet and write, since mutual exclusion
- * protects us from concurrent writes.
+ * We grab the metadata cache mutex to get exclusive access to our metadata
+ * buffer and to the metadata cache. Exclusive access to the metadata buffer
+ * allows us to do racy operations such as looking for remaining space left in
+ * packet and write, since mutual exclusion protects us from concurrent writes.
+ * Mutual exclusion on the metadata cache allow us to read the cache content
+ * without racing against reallocation of the cache by updates.
  * Returns the number of bytes written in the channel, 0 if no data
  * was written and a negative value on error.
  */
@@ -1448,13 +1525,15 @@ int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
        size_t len, reserve_len;
 
        /*
-        * Ensure we support mutiple get_next / put sequences followed
-        * by put_next. The metadata stream lock internally protects
-        * reading the metadata cache. It can indeed be read
-        * concurrently by "get_next_subbuf" and "flush" operations on
-        * the buffer invoked by different processes.
+        * Ensure we support mutiple get_next / put sequences followed by
+        * put_next. The metadata cache lock protects reading the metadata
+        * cache. It can indeed be read concurrently by "get_next_subbuf" and
+        * "flush" operations on the buffer invoked by different processes.
+        * Moreover, since the metadata cache memory can be reallocated, we
+        * need to have exclusive access against updates even though we only
+        * read it.
         */
-       mutex_lock(&stream->lock);
+       mutex_lock(&stream->metadata_cache->lock);
        WARN_ON(stream->metadata_in < stream->metadata_out);
        if (stream->metadata_in != stream->metadata_out)
                goto end;
@@ -1484,13 +1563,15 @@ int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
        ret = reserve_len;
 
 end:
-       mutex_unlock(&stream->lock);
+       mutex_unlock(&stream->metadata_cache->lock);
        return ret;
 }
 
 /*
  * Write the metadata to the metadata cache.
  * Must be called with sessions_mutex held.
+ * The metadata cache lock protects us from concurrent read access from
+ * thread outputting metadata content to ring buffer.
  */
 int lttng_metadata_printf(struct lttng_session *session,
                          const char *fmt, ...)
@@ -1509,6 +1590,7 @@ int lttng_metadata_printf(struct lttng_session *session,
                return -ENOMEM;
 
        len = strlen(str);
+       mutex_lock(&session->metadata_cache->lock);
        if (session->metadata_cache->metadata_written + len >
                        session->metadata_cache->cache_alloc) {
                char *tmp_cache_realloc;
@@ -1517,10 +1599,16 @@ int lttng_metadata_printf(struct lttng_session *session,
                tmp_cache_alloc_size = max_t(unsigned int,
                                session->metadata_cache->cache_alloc + len,
                                session->metadata_cache->cache_alloc << 1);
-               tmp_cache_realloc = krealloc(session->metadata_cache->data,
-                               tmp_cache_alloc_size, GFP_KERNEL);
+               tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size);
                if (!tmp_cache_realloc)
                        goto err;
+               if (session->metadata_cache->data) {
+                       memcpy(tmp_cache_realloc,
+                               session->metadata_cache->data,
+                               session->metadata_cache->cache_alloc);
+                       vfree(session->metadata_cache->data);
+               }
+
                session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
                session->metadata_cache->data = tmp_cache_realloc;
        }
@@ -1528,6 +1616,7 @@ int lttng_metadata_printf(struct lttng_session *session,
                        session->metadata_cache->metadata_written,
                        str, len);
        session->metadata_cache->metadata_written += len;
+       mutex_unlock(&session->metadata_cache->lock);
        kfree(str);
 
        list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
@@ -1536,6 +1625,7 @@ int lttng_metadata_printf(struct lttng_session *session,
        return 0;
 
 err:
+       mutex_unlock(&session->metadata_cache->lock);
        kfree(str);
        return -ENOMEM;
 }
@@ -1562,7 +1652,7 @@ int _lttng_field_statedump(struct lttng_session *session,
                                        ? "UTF8"
                                        : "ASCII",
                        field->type.u.basic.integer.base,
-#ifdef __BIG_ENDIAN
+#if __BYTE_ORDER == __BIG_ENDIAN
                        field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
 #else
                        field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
@@ -1591,7 +1681,7 @@ int _lttng_field_statedump(struct lttng_session *session,
                                        ? "UTF8"
                                        : "ASCII",
                        elem_type->u.basic.integer.base,
-#ifdef __BIG_ENDIAN
+#if __BYTE_ORDER == __BIG_ENDIAN
                        elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
 #else
                        elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
@@ -1617,7 +1707,7 @@ int _lttng_field_statedump(struct lttng_session *session,
                                        ? "UTF8"
                                        : "ASCII"),
                        length_type->u.basic.integer.base,
-#ifdef __BIG_ENDIAN
+#if __BYTE_ORDER == __BIG_ENDIAN
                        length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
 #else
                        length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
@@ -1637,7 +1727,7 @@ int _lttng_field_statedump(struct lttng_session *session,
                                        ? "UTF8"
                                        : "ASCII"),
                        elem_type->u.basic.integer.base,
-#ifdef __BIG_ENDIAN
+#if __BYTE_ORDER == __BIG_ENDIAN
                        elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
 #else
                        elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
@@ -1886,11 +1976,14 @@ int _lttng_event_header_declare(struct lttng_session *session)
  * taken at start of trace.
  * Yes, this is only an approximation. Yes, we can (and will) do better
  * in future versions.
+ * Return 0 if offset is negative. It may happen if the system sets
+ * the REALTIME clock to 0 after boot.
  */
 static
 uint64_t measure_clock_offset(void)
 {
-       uint64_t offset, monotonic[2], realtime;
+       uint64_t monotonic_avg, monotonic[2], realtime;
+       int64_t offset;
        struct timespec rts = { 0, 0 };
        unsigned long flags;
 
@@ -1901,10 +1994,12 @@ uint64_t measure_clock_offset(void)
        monotonic[1] = trace_clock_read64();
        local_irq_restore(flags);
 
-       offset = (monotonic[0] + monotonic[1]) >> 1;
+       monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
        realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
        realtime += rts.tv_nsec;
-       offset = realtime - offset;
+       offset = (int64_t) realtime - monotonic_avg;
+       if (offset < 0)
+               return 0;
        return offset;
 }
 
@@ -1962,7 +2057,7 @@ int _lttng_session_metadata_statedump(struct lttng_session *session)
                CTF_SPEC_MAJOR,
                CTF_SPEC_MINOR,
                uuid_s,
-#ifdef __BIG_ENDIAN
+#if __BYTE_ORDER == __BIG_ENDIAN
                "be"
 #else
                "le"
@@ -2115,7 +2210,12 @@ static int __init lttng_events_init(void)
        ret = wrapper_lttng_fixup_sig(THIS_MODULE);
        if (ret)
                return ret;
-
+       ret = wrapper_get_pfnblock_flags_mask_init();
+       if (ret)
+               return ret;
+       ret = wrapper_get_pageblock_flags_mask_init();
+       if (ret)
+               return ret;
        ret = lttng_context_init();
        if (ret)
                return ret;
This page took 0.028813 seconds and 4 git commands to generate.