Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[linux-2.6.git] / kernel / trace / kmemtrace.c
index 7ebc58c..a91da69 100644 (file)
@@ -6,14 +6,16 @@
  * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
  */
 
-#include <linux/dcache.h>
+#include <linux/tracepoint.h>
+#include <linux/seq_file.h>
 #include <linux/debugfs.h>
+#include <linux/dcache.h>
 #include <linux/fs.h>
-#include <linux/seq_file.h>
-#include <trace/kmemtrace.h>
 
-#include "trace.h"
+#include <linux/kmemtrace.h>
+
 #include "trace_output.h"
+#include "trace.h"
 
 /* Select an alternative, minimalistic output than the original one */
 #define TRACE_KMEM_OPT_MINIMAL 0x1
@@ -25,30 +27,174 @@ static struct tracer_opt kmem_opts[] = {
 };
 
 static struct tracer_flags kmem_tracer_flags = {
-       .val = 0,
-       .opts = kmem_opts
+       .val                    = 0,
+       .opts                   = kmem_opts
 };
 
-
-static bool kmem_tracing_enabled __read_mostly;
 static struct trace_array *kmemtrace_array;
 
+/* Trace allocations */
+static inline void kmemtrace_alloc(enum kmemtrace_type_id type_id,
+                                  unsigned long call_site,
+                                  const void *ptr,
+                                  size_t bytes_req,
+                                  size_t bytes_alloc,
+                                  gfp_t gfp_flags,
+                                  int node)
+{
+       struct ftrace_event_call *call = &event_kmem_alloc;
+       struct trace_array *tr = kmemtrace_array;
+       struct kmemtrace_alloc_entry *entry;
+       struct ring_buffer_event *event;
+
+       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry));
+       if (!event)
+               return;
+
+       entry = ring_buffer_event_data(event);
+       tracing_generic_entry_update(&entry->ent, 0, 0);
+
+       entry->ent.type         = TRACE_KMEM_ALLOC;
+       entry->type_id          = type_id;
+       entry->call_site        = call_site;
+       entry->ptr              = ptr;
+       entry->bytes_req        = bytes_req;
+       entry->bytes_alloc      = bytes_alloc;
+       entry->gfp_flags        = gfp_flags;
+       entry->node             = node;
+
+       if (!filter_check_discard(call, entry, tr->buffer, event))
+               ring_buffer_unlock_commit(tr->buffer, event);
+
+       trace_wake_up();
+}
+
+static inline void kmemtrace_free(enum kmemtrace_type_id type_id,
+                                 unsigned long call_site,
+                                 const void *ptr)
+{
+       struct ftrace_event_call *call = &event_kmem_free;
+       struct trace_array *tr = kmemtrace_array;
+       struct kmemtrace_free_entry *entry;
+       struct ring_buffer_event *event;
+
+       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry));
+       if (!event)
+               return;
+       entry   = ring_buffer_event_data(event);
+       tracing_generic_entry_update(&entry->ent, 0, 0);
+
+       entry->ent.type         = TRACE_KMEM_FREE;
+       entry->type_id          = type_id;
+       entry->call_site        = call_site;
+       entry->ptr              = ptr;
+
+       if (!filter_check_discard(call, entry, tr->buffer, event))
+               ring_buffer_unlock_commit(tr->buffer, event);
+
+       trace_wake_up();
+}
+
+static void kmemtrace_kmalloc(unsigned long call_site,
+                             const void *ptr,
+                             size_t bytes_req,
+                             size_t bytes_alloc,
+                             gfp_t gfp_flags)
+{
+       kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC, call_site, ptr,
+                       bytes_req, bytes_alloc, gfp_flags, -1);
+}
+
+static void kmemtrace_kmem_cache_alloc(unsigned long call_site,
+                                      const void *ptr,
+                                      size_t bytes_req,
+                                      size_t bytes_alloc,
+                                      gfp_t gfp_flags)
+{
+       kmemtrace_alloc(KMEMTRACE_TYPE_CACHE, call_site, ptr,
+                       bytes_req, bytes_alloc, gfp_flags, -1);
+}
+
+static void kmemtrace_kmalloc_node(unsigned long call_site,
+                                  const void *ptr,
+                                  size_t bytes_req,
+                                  size_t bytes_alloc,
+                                  gfp_t gfp_flags,
+                                  int node)
+{
+       kmemtrace_alloc(KMEMTRACE_TYPE_KMALLOC, call_site, ptr,
+                       bytes_req, bytes_alloc, gfp_flags, node);
+}
+
+static void kmemtrace_kmem_cache_alloc_node(unsigned long call_site,
+                                           const void *ptr,
+                                           size_t bytes_req,
+                                           size_t bytes_alloc,
+                                           gfp_t gfp_flags,
+                                           int node)
+{
+       kmemtrace_alloc(KMEMTRACE_TYPE_CACHE, call_site, ptr,
+                       bytes_req, bytes_alloc, gfp_flags, node);
+}
+
+static void kmemtrace_kfree(unsigned long call_site, const void *ptr)
+{
+       kmemtrace_free(KMEMTRACE_TYPE_KMALLOC, call_site, ptr);
+}
+
+static void kmemtrace_kmem_cache_free(unsigned long call_site, const void *ptr)
+{
+       kmemtrace_free(KMEMTRACE_TYPE_CACHE, call_site, ptr);
+}
+
+static int kmemtrace_start_probes(void)
+{
+       int err;
+
+       err = register_trace_kmalloc(kmemtrace_kmalloc);
+       if (err)
+               return err;
+       err = register_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc);
+       if (err)
+               return err;
+       err = register_trace_kmalloc_node(kmemtrace_kmalloc_node);
+       if (err)
+               return err;
+       err = register_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node);
+       if (err)
+               return err;
+       err = register_trace_kfree(kmemtrace_kfree);
+       if (err)
+               return err;
+       err = register_trace_kmem_cache_free(kmemtrace_kmem_cache_free);
+
+       return err;
+}
+
+static void kmemtrace_stop_probes(void)
+{
+       unregister_trace_kmalloc(kmemtrace_kmalloc);
+       unregister_trace_kmem_cache_alloc(kmemtrace_kmem_cache_alloc);
+       unregister_trace_kmalloc_node(kmemtrace_kmalloc_node);
+       unregister_trace_kmem_cache_alloc_node(kmemtrace_kmem_cache_alloc_node);
+       unregister_trace_kfree(kmemtrace_kfree);
+       unregister_trace_kmem_cache_free(kmemtrace_kmem_cache_free);
+}
+
 static int kmem_trace_init(struct trace_array *tr)
 {
-       int cpu;
        kmemtrace_array = tr;
 
-       for_each_cpu_mask(cpu, cpu_possible_map)
-               tracing_reset(tr, cpu);
+       tracing_reset_online_cpus(tr);
 
-       kmem_tracing_enabled = true;
+       kmemtrace_start_probes();
 
        return 0;
 }
 
 static void kmem_trace_reset(struct trace_array *tr)
 {
-       kmem_tracing_enabled = false;
+       kmemtrace_stop_probes();
 }
 
 static void kmemtrace_headers(struct seq_file *s)
@@ -66,55 +212,136 @@ static void kmemtrace_headers(struct seq_file *s)
 }
 
 /*
- * The two following functions give the original output from kmemtrace,
- * or something close to....perhaps they need some missing things
+ * The following functions give the original output from kmemtrace,
+ * plus the origin CPU, since reordering occurs in-kernel now.
  */
+
+#define KMEMTRACE_USER_ALLOC   0
+#define KMEMTRACE_USER_FREE    1
+
+struct kmemtrace_user_event {
+       u8                      event_id;
+       u8                      type_id;
+       u16                     event_size;
+       u32                     cpu;
+       u64                     timestamp;
+       unsigned long           call_site;
+       unsigned long           ptr;
+};
+
+struct kmemtrace_user_event_alloc {
+       size_t                  bytes_req;
+       size_t                  bytes_alloc;
+       unsigned                gfp_flags;
+       int                     node;
+};
+
 static enum print_line_t
-kmemtrace_print_alloc_original(struct trace_iterator *iter,
-                               struct kmemtrace_alloc_entry *entry)
+kmemtrace_print_alloc(struct trace_iterator *iter, int flags)
 {
        struct trace_seq *s = &iter->seq;
+       struct kmemtrace_alloc_entry *entry;
        int ret;
 
-       /* Taken from the old linux/kmemtrace.h */
-       ret = trace_seq_printf(s, "type_id %d call_site %lu ptr %lu "
-         "bytes_req %lu bytes_alloc %lu gfp_flags %lu node %d\n",
-          entry->type_id, entry->call_site, (unsigned long) entry->ptr,
-          (unsigned long) entry->bytes_req, (unsigned long) entry->bytes_alloc,
-          (unsigned long) entry->gfp_flags, entry->node);
+       trace_assign_type(entry, iter->ent);
+
+       ret = trace_seq_printf(s, "type_id %d call_site %pF ptr %lu "
+           "bytes_req %lu bytes_alloc %lu gfp_flags %lu node %d\n",
+           entry->type_id, (void *)entry->call_site, (unsigned long)entry->ptr,
+           (unsigned long)entry->bytes_req, (unsigned long)entry->bytes_alloc,
+           (unsigned long)entry->gfp_flags, entry->node);
 
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
-
        return TRACE_TYPE_HANDLED;
 }
 
 static enum print_line_t
-kmemtrace_print_free_original(struct trace_iterator *iter,
-                               struct kmemtrace_free_entry *entry)
+kmemtrace_print_free(struct trace_iterator *iter, int flags)
 {
        struct trace_seq *s = &iter->seq;
+       struct kmemtrace_free_entry *entry;
        int ret;
 
-       /* Taken from the old linux/kmemtrace.h */
-       ret = trace_seq_printf(s, "type_id %d call_site %lu ptr %lu\n",
-          entry->type_id, entry->call_site, (unsigned long) entry->ptr);
+       trace_assign_type(entry, iter->ent);
+
+       ret = trace_seq_printf(s, "type_id %d call_site %pF ptr %lu\n",
+                              entry->type_id, (void *)entry->call_site,
+                              (unsigned long)entry->ptr);
 
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
+       return TRACE_TYPE_HANDLED;
+}
+
+static enum print_line_t
+kmemtrace_print_alloc_user(struct trace_iterator *iter, int flags)
+{
+       struct trace_seq *s = &iter->seq;
+       struct kmemtrace_alloc_entry *entry;
+       struct kmemtrace_user_event *ev;
+       struct kmemtrace_user_event_alloc *ev_alloc;
+
+       trace_assign_type(entry, iter->ent);
+
+       ev = trace_seq_reserve(s, sizeof(*ev));
+       if (!ev)
+               return TRACE_TYPE_PARTIAL_LINE;
+
+       ev->event_id            = KMEMTRACE_USER_ALLOC;
+       ev->type_id             = entry->type_id;
+       ev->event_size          = sizeof(*ev) + sizeof(*ev_alloc);
+       ev->cpu                 = iter->cpu;
+       ev->timestamp           = iter->ts;
+       ev->call_site           = entry->call_site;
+       ev->ptr                 = (unsigned long)entry->ptr;
+
+       ev_alloc = trace_seq_reserve(s, sizeof(*ev_alloc));
+       if (!ev_alloc)
+               return TRACE_TYPE_PARTIAL_LINE;
+
+       ev_alloc->bytes_req     = entry->bytes_req;
+       ev_alloc->bytes_alloc   = entry->bytes_alloc;
+       ev_alloc->gfp_flags     = entry->gfp_flags;
+       ev_alloc->node          = entry->node;
 
        return TRACE_TYPE_HANDLED;
 }
 
+static enum print_line_t
+kmemtrace_print_free_user(struct trace_iterator *iter, int flags)
+{
+       struct trace_seq *s = &iter->seq;
+       struct kmemtrace_free_entry *entry;
+       struct kmemtrace_user_event *ev;
+
+       trace_assign_type(entry, iter->ent);
+
+       ev = trace_seq_reserve(s, sizeof(*ev));
+       if (!ev)
+               return TRACE_TYPE_PARTIAL_LINE;
+
+       ev->event_id            = KMEMTRACE_USER_FREE;
+       ev->type_id             = entry->type_id;
+       ev->event_size          = sizeof(*ev);
+       ev->cpu                 = iter->cpu;
+       ev->timestamp           = iter->ts;
+       ev->call_site           = entry->call_site;
+       ev->ptr                 = (unsigned long)entry->ptr;
+
+       return TRACE_TYPE_HANDLED;
+}
 
 /* The two other following provide a more minimalistic output */
 static enum print_line_t
-kmemtrace_print_alloc_compress(struct trace_iterator *iter,
-                                       struct kmemtrace_alloc_entry *entry)
+kmemtrace_print_alloc_compress(struct trace_iterator *iter)
 {
+       struct kmemtrace_alloc_entry *entry;
        struct trace_seq *s = &iter->seq;
        int ret;
 
+       trace_assign_type(entry, iter->ent);
+
        /* Alloc entry */
        ret = trace_seq_printf(s, "  +      ");
        if (!ret)
@@ -139,12 +366,12 @@ kmemtrace_print_alloc_compress(struct trace_iterator *iter,
                return TRACE_TYPE_PARTIAL_LINE;
 
        /* Requested */
-       ret = trace_seq_printf(s, "%4ld   ", entry->bytes_req);
+       ret = trace_seq_printf(s, "%4zu   ", entry->bytes_req);
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
 
        /* Allocated */
-       ret = trace_seq_printf(s, "%4ld   ", entry->bytes_alloc);
+       ret = trace_seq_printf(s, "%4zu   ", entry->bytes_alloc);
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
 
@@ -160,29 +387,24 @@ kmemtrace_print_alloc_compress(struct trace_iterator *iter,
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
 
-       /* Node */
-       ret = trace_seq_printf(s, "%4d   ", entry->node);
+       /* Node and call site*/
+       ret = trace_seq_printf(s, "%4d   %pf\n", entry->node,
+                                                (void *)entry->call_site);
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
 
-       /* Call site */
-       ret = seq_print_ip_sym(s, entry->call_site, 0);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
-
-       if (!trace_seq_printf(s, "\n"))
-               return TRACE_TYPE_PARTIAL_LINE;
-
        return TRACE_TYPE_HANDLED;
 }
 
 static enum print_line_t
-kmemtrace_print_free_compress(struct trace_iterator *iter,
-                               struct kmemtrace_free_entry *entry)
+kmemtrace_print_free_compress(struct trace_iterator *iter)
 {
+       struct kmemtrace_free_entry *entry;
        struct trace_seq *s = &iter->seq;
        int ret;
 
+       trace_assign_type(entry, iter->ent);
+
        /* Free entry */
        ret = trace_seq_printf(s, "  -      ");
        if (!ret)
@@ -216,19 +438,11 @@ kmemtrace_print_free_compress(struct trace_iterator *iter,
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
 
-       /* Skip node */
-       ret = trace_seq_printf(s, "       ");
+       /* Skip node and print call site*/
+       ret = trace_seq_printf(s, "       %pf\n", (void *)entry->call_site);
        if (!ret)
                return TRACE_TYPE_PARTIAL_LINE;
 
-       /* Call site */
-       ret = seq_print_ip_sym(s, entry->call_site, 0);
-       if (!ret)
-               return TRACE_TYPE_PARTIAL_LINE;
-
-       if (!trace_seq_printf(s, "\n"))
-               return TRACE_TYPE_PARTIAL_LINE;
-
        return TRACE_TYPE_HANDLED;
 }
 
@@ -236,105 +450,38 @@ static enum print_line_t kmemtrace_print_line(struct trace_iterator *iter)
 {
        struct trace_entry *entry = iter->ent;
 
-       switch (entry->type) {
-       case TRACE_KMEM_ALLOC: {
-               struct kmemtrace_alloc_entry *field;
-               trace_assign_type(field, entry);
-               if (kmem_tracer_flags.val & TRACE_KMEM_OPT_MINIMAL)
-                       return kmemtrace_print_alloc_compress(iter, field);
-               else
-                       return kmemtrace_print_alloc_original(iter, field);
-       }
-
-       case TRACE_KMEM_FREE: {
-               struct kmemtrace_free_entry *field;
-               trace_assign_type(field, entry);
-               if (kmem_tracer_flags.val & TRACE_KMEM_OPT_MINIMAL)
-                       return kmemtrace_print_free_compress(iter, field);
-               else
-                       return kmemtrace_print_free_original(iter, field);
-       }
+       if (!(kmem_tracer_flags.val & TRACE_KMEM_OPT_MINIMAL))
+               return TRACE_TYPE_UNHANDLED;
 
+       switch (entry->type) {
+       case TRACE_KMEM_ALLOC:
+               return kmemtrace_print_alloc_compress(iter);
+       case TRACE_KMEM_FREE:
+               return kmemtrace_print_free_compress(iter);
        default:
                return TRACE_TYPE_UNHANDLED;
        }
 }
 
-/* Trace allocations */
-void kmemtrace_mark_alloc_node(enum kmemtrace_type_id type_id,
-                            unsigned long call_site,
-                            const void *ptr,
-                            size_t bytes_req,
-                            size_t bytes_alloc,
-                            gfp_t gfp_flags,
-                            int node)
-{
-       struct ring_buffer_event *event;
-       struct kmemtrace_alloc_entry *entry;
-       struct trace_array *tr = kmemtrace_array;
-       unsigned long irq_flags;
-
-       if (!kmem_tracing_enabled)
-               return;
-
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                        &irq_flags);
-       if (!event)
-               return;
-       entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, 0, 0);
-
-       entry->ent.type = TRACE_KMEM_ALLOC;
-       entry->call_site = call_site;
-       entry->ptr = ptr;
-       entry->bytes_req = bytes_req;
-       entry->bytes_alloc = bytes_alloc;
-       entry->gfp_flags = gfp_flags;
-       entry->node     =       node;
-
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
-
-       trace_wake_up();
-}
-EXPORT_SYMBOL(kmemtrace_mark_alloc_node);
-
-void kmemtrace_mark_free(enum kmemtrace_type_id type_id,
-                      unsigned long call_site,
-                      const void *ptr)
-{
-       struct ring_buffer_event *event;
-       struct kmemtrace_free_entry *entry;
-       struct trace_array *tr = kmemtrace_array;
-       unsigned long irq_flags;
-
-       if (!kmem_tracing_enabled)
-               return;
-
-       event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
-                                        &irq_flags);
-       if (!event)
-               return;
-       entry   = ring_buffer_event_data(event);
-       tracing_generic_entry_update(&entry->ent, 0, 0);
-
-       entry->ent.type = TRACE_KMEM_FREE;
-       entry->type_id  = type_id;
-       entry->call_site = call_site;
-       entry->ptr = ptr;
-
-       ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
+static struct trace_event kmem_trace_alloc = {
+       .type                   = TRACE_KMEM_ALLOC,
+       .trace                  = kmemtrace_print_alloc,
+       .binary                 = kmemtrace_print_alloc_user,
+};
 
-       trace_wake_up();
-}
-EXPORT_SYMBOL(kmemtrace_mark_free);
+static struct trace_event kmem_trace_free = {
+       .type                   = TRACE_KMEM_FREE,
+       .trace                  = kmemtrace_print_free,
+       .binary                 = kmemtrace_print_free_user,
+};
 
 static struct tracer kmem_tracer __read_mostly = {
-       .name           = "kmemtrace",
-       .init           = kmem_trace_init,
-       .reset          = kmem_trace_reset,
-       .print_line     = kmemtrace_print_line,
-       .print_header = kmemtrace_headers,
-       .flags          = &kmem_tracer_flags
+       .name                   = "kmemtrace",
+       .init                   = kmem_trace_init,
+       .reset                  = kmem_trace_reset,
+       .print_line             = kmemtrace_print_line,
+       .print_header           = kmemtrace_headers,
+       .flags                  = &kmem_tracer_flags
 };
 
 void kmemtrace_init(void)
@@ -344,7 +491,21 @@ void kmemtrace_init(void)
 
 static int __init init_kmem_tracer(void)
 {
-       return register_tracer(&kmem_tracer);
-}
+       if (!register_ftrace_event(&kmem_trace_alloc)) {
+               pr_warning("Warning: could not register kmem events\n");
+               return 1;
+       }
+
+       if (!register_ftrace_event(&kmem_trace_free)) {
+               pr_warning("Warning: could not register kmem events\n");
+               return 1;
+       }
+
+       if (register_tracer(&kmem_tracer) != 0) {
+               pr_warning("Warning: could not register the kmem tracer\n");
+               return 1;
+       }
 
+       return 0;
+}
 device_initcall(init_kmem_tracer);