ftrace: add ftrace_kill_atomic
[safe/jmp/linux-2.6] / kernel / trace / ftrace.c
index ec54cb7..1359632 100644 (file)
 #include <linux/hardirq.h>
 #include <linux/kthread.h>
 #include <linux/uaccess.h>
+#include <linux/kprobes.h>
 #include <linux/ftrace.h>
 #include <linux/sysctl.h>
 #include <linux/ctype.h>
 #include <linux/hash.h>
 #include <linux/list.h>
 
+#include <asm/ftrace.h>
+
 #include "trace.h"
 
 /* ftrace_enabled is a method to turn ftrace on or off */
@@ -50,7 +53,7 @@ static struct ftrace_ops ftrace_list_end __read_mostly =
 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
 
-void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
+static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
 {
        struct ftrace_ops *op = ftrace_list;
 
@@ -161,6 +164,8 @@ enum {
 };
 
 static int ftrace_filtered;
+static int tracing_on;
+static int frozen_record_count;
 
 static struct hlist_head ftrace_hash[FTRACE_HASHSIZE];
 
@@ -193,6 +198,71 @@ static int ftrace_record_suspend;
 
 static struct dyn_ftrace *ftrace_free_records;
 
+
+#ifdef CONFIG_KPROBES
+static inline void freeze_record(struct dyn_ftrace *rec)
+{
+       if (!(rec->flags & FTRACE_FL_FROZEN)) {
+               rec->flags |= FTRACE_FL_FROZEN;
+               frozen_record_count++;
+       }
+}
+
+static inline void unfreeze_record(struct dyn_ftrace *rec)
+{
+       if (rec->flags & FTRACE_FL_FROZEN) {
+               rec->flags &= ~FTRACE_FL_FROZEN;
+               frozen_record_count--;
+       }
+}
+
+static inline int record_frozen(struct dyn_ftrace *rec)
+{
+       return rec->flags & FTRACE_FL_FROZEN;
+}
+#else
+# define freeze_record(rec)                    ({ 0; })
+# define unfreeze_record(rec)                  ({ 0; })
+# define record_frozen(rec)                    ({ 0; })
+#endif /* CONFIG_KPROBES */
+
+int skip_trace(unsigned long ip)
+{
+       unsigned long fl;
+       struct dyn_ftrace *rec;
+       struct hlist_node *t;
+       struct hlist_head *head;
+
+       if (frozen_record_count == 0)
+               return 0;
+
+       head = &ftrace_hash[hash_long(ip, FTRACE_HASHBITS)];
+       hlist_for_each_entry_rcu(rec, t, head, node) {
+               if (rec->ip == ip) {
+                       if (record_frozen(rec)) {
+                               if (rec->flags & FTRACE_FL_FAILED)
+                                       return 1;
+
+                               if (!(rec->flags & FTRACE_FL_CONVERTED))
+                                       return 1;
+
+                               if (!tracing_on || !ftrace_enabled)
+                                       return 1;
+
+                               if (ftrace_filtered) {
+                                       fl = rec->flags & (FTRACE_FL_FILTER |
+                                                          FTRACE_FL_NOTRACE);
+                                       if (!fl || (fl & FTRACE_FL_NOTRACE))
+                                               return 1;
+                               }
+                       }
+                       break;
+               }
+       }
+
+       return 0;
+}
+
 static inline int
 ftrace_ip_in_hash(unsigned long ip, unsigned long key)
 {
@@ -306,13 +376,6 @@ ftrace_record_ip(unsigned long ip)
        if (ftrace_ip_in_hash(ip, key))
                goto out_unlock;
 
-       /*
-        * There's a slight race that the ftraced will update the
-        * hash and reset here. If it is already converted, skip it.
-        */
-       if (ftrace_ip_converted(ip))
-               goto out_unlock;
-
        node = ftrace_alloc_dyn_node(ip);
        if (!node)
                goto out_unlock;
@@ -336,7 +399,6 @@ ftrace_record_ip(unsigned long ip)
 }
 
 #define FTRACE_ADDR ((long)(ftrace_caller))
-#define MCOUNT_ADDR ((long)(mcount))
 
 static int
 __ftrace_replace_code(struct dyn_ftrace *rec,
@@ -362,20 +424,26 @@ __ftrace_replace_code(struct dyn_ftrace *rec,
                 * If this record is set not to trace then
                 * do nothing.
                 *
+                * If this record is set not to trace and
+                * it is enabled then disable it.
+                *
                 * If this record is not set to be filtered and
                 * it is enabled, disable it.
                 */
-               fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
+
+               fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE |
+                                  FTRACE_FL_ENABLED);
 
                if ((fl ==  (FTRACE_FL_FILTER | FTRACE_FL_ENABLED)) ||
-                   (fl == 0) || (rec->flags & FTRACE_FL_NOTRACE))
+                   (fl ==  (FTRACE_FL_FILTER | FTRACE_FL_NOTRACE)) ||
+                   !fl || (fl == FTRACE_FL_NOTRACE))
                        return 0;
 
                /*
                 * If it is enabled disable it,
                 * otherwise enable it!
                 */
-               if (fl == FTRACE_FL_ENABLED) {
+               if (fl & FTRACE_FL_ENABLED) {
                        /* swap new and old */
                        new = old;
                        old = ftrace_call_replace(ip, FTRACE_ADDR);
@@ -433,11 +501,19 @@ static void ftrace_replace_code(int enable)
                        if (rec->flags & FTRACE_FL_FAILED)
                                continue;
 
+                       /* ignore updates to this record's mcount site */
+                       if (get_kprobe((void *)rec->ip)) {
+                               freeze_record(rec);
+                               continue;
+                       } else {
+                               unfreeze_record(rec);
+                       }
+
                        failed = __ftrace_replace_code(rec, old, new, enable);
                        if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
                                rec->flags |= FTRACE_FL_FAILED;
                                if ((system_state == SYSTEM_BOOTING) ||
-                                   !kernel_text_address(rec->ip)) {
+                                   !core_kernel_text(rec->ip)) {
                                        ftrace_del_hash(rec);
                                        ftrace_free_rec(rec);
                                }
@@ -489,8 +565,11 @@ static int __ftrace_modify_code(void *data)
                 */
                __ftrace_update_code(NULL);
                ftrace_replace_code(1);
-       } else if (*command & FTRACE_DISABLE_CALLS)
+               tracing_on = 1;
+       } else if (*command & FTRACE_DISABLE_CALLS) {
                ftrace_replace_code(0);
+               tracing_on = 0;
+       }
 
        if (*command & FTRACE_UPDATE_TRACE_FUNC)
                ftrace_update_ftrace_func(ftrace_trace_function);
@@ -622,11 +701,11 @@ unsigned long             ftrace_update_tot_cnt;
 
 static int __ftrace_update_code(void *ignore)
 {
+       int i, save_ftrace_enabled;
+       cycle_t start, stop;
        struct dyn_ftrace *p;
        struct hlist_node *t, *n;
-       int save_ftrace_enabled;
-       cycle_t start, stop;
-       int i;
+       struct hlist_head *head, temp_list;
 
        /* Don't be recording funcs now */
        ftrace_record_suspend++;
@@ -638,8 +717,11 @@ static int __ftrace_update_code(void *ignore)
 
        /* No locks needed, the machine is stopped! */
        for (i = 0; i < FTRACE_HASHSIZE; i++) {
+               INIT_HLIST_HEAD(&temp_list);
+               head = &ftrace_hash[i];
+
                /* all CPUS are stopped, we are safe to modify code */
-               hlist_for_each_entry_safe(p, t, n, &ftrace_hash[i], node) {
+               hlist_for_each_entry_safe(p, t, n, head, node) {
                        /* Skip over failed records which have not been
                         * freed. */
                        if (p->flags & FTRACE_FL_FAILED)
@@ -653,18 +735,39 @@ static int __ftrace_update_code(void *ignore)
                        if (p->flags & (FTRACE_FL_CONVERTED))
                                break;
 
+                       /* Ignore updates to this record's mcount site.
+                        * Reintroduce this record at the head of this
+                        * bucket to attempt to "convert" it again if
+                        * the kprobe on it is unregistered before the
+                        * next run. */
+                       if (get_kprobe((void *)p->ip)) {
+                               ftrace_del_hash(p);
+                               INIT_HLIST_NODE(&p->node);
+                               hlist_add_head(&p->node, &temp_list);
+                               freeze_record(p);
+                               continue;
+                       } else {
+                               unfreeze_record(p);
+                       }
+
+                       /* convert record (i.e, patch mcount-call with NOP) */
                        if (ftrace_code_disable(p)) {
                                p->flags |= FTRACE_FL_CONVERTED;
                                ftrace_update_cnt++;
                        } else {
                                if ((system_state == SYSTEM_BOOTING) ||
-                                   !kernel_text_address(p->ip)) {
+                                   !core_kernel_text(p->ip)) {
                                        ftrace_del_hash(p);
                                        ftrace_free_rec(p);
-
                                }
                        }
                }
+
+               hlist_for_each_entry_safe(p, t, n, &temp_list, node) {
+                       hlist_del(&p->node);
+                       INIT_HLIST_NODE(&p->node);
+                       hlist_add_head(&p->node, head);
+               }
        }
 
        stop = ftrace_now(raw_smp_processor_id());
@@ -775,6 +878,7 @@ enum {
        FTRACE_ITER_FILTER      = (1 << 0),
        FTRACE_ITER_CONT        = (1 << 1),
        FTRACE_ITER_NOTRACE     = (1 << 2),
+       FTRACE_ITER_FAILURES    = (1 << 3),
 };
 
 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
@@ -806,9 +910,16 @@ t_next(struct seq_file *m, void *v, loff_t *pos)
                }
        } else {
                rec = &iter->pg->records[iter->idx++];
-               if ((rec->flags & FTRACE_FL_FAILED) ||
+               if ((!(iter->flags & FTRACE_ITER_FAILURES) &&
+                    (rec->flags & FTRACE_FL_FAILED)) ||
+
+                   ((iter->flags & FTRACE_ITER_FAILURES) &&
+                    (!(rec->flags & FTRACE_FL_FAILED) ||
+                     (rec->flags & FTRACE_FL_FREE))) ||
+
                    ((iter->flags & FTRACE_ITER_FILTER) &&
                     !(rec->flags & FTRACE_FL_FILTER)) ||
+
                    ((iter->flags & FTRACE_ITER_NOTRACE) &&
                     !(rec->flags & FTRACE_FL_NOTRACE))) {
                        rec = NULL;
@@ -903,6 +1014,24 @@ int ftrace_avail_release(struct inode *inode, struct file *file)
        return 0;
 }
 
+static int
+ftrace_failures_open(struct inode *inode, struct file *file)
+{
+       int ret;
+       struct seq_file *m;
+       struct ftrace_iterator *iter;
+
+       ret = ftrace_avail_open(inode, file);
+       if (!ret) {
+               m = (struct seq_file *)file->private_data;
+               iter = (struct ftrace_iterator *)m->private;
+               iter->flags = FTRACE_ITER_FAILURES;
+       }
+
+       return ret;
+}
+
+
 static void ftrace_filter_reset(int enable)
 {
        struct ftrace_page *pg;
@@ -1316,6 +1445,13 @@ static struct file_operations ftrace_avail_fops = {
        .release = ftrace_avail_release,
 };
 
+static struct file_operations ftrace_failures_fops = {
+       .open = ftrace_failures_open,
+       .read = seq_read,
+       .llseek = seq_lseek,
+       .release = ftrace_avail_release,
+};
+
 static struct file_operations ftrace_filter_fops = {
        .open = ftrace_filter_open,
        .read = ftrace_regex_read,
@@ -1393,6 +1529,11 @@ static __init int ftrace_init_debugfs(void)
                pr_warning("Could not create debugfs "
                           "'available_filter_functions' entry\n");
 
+       entry = debugfs_create_file("failures", 0444,
+                                   d_tracer, NULL, &ftrace_failures_fops);
+       if (!entry)
+               pr_warning("Could not create debugfs 'failures' entry\n");
+
        entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
                                    NULL, &ftrace_filter_fops);
        if (!entry)
@@ -1461,6 +1602,21 @@ core_initcall(ftrace_dynamic_init);
 #endif /* CONFIG_DYNAMIC_FTRACE */
 
 /**
+ * ftrace_kill_atomic - kill ftrace from critical sections
+ *
+ * This function should be used by panic code. It stops ftrace
+ * but in a not so nice way. If you need to simply kill ftrace
+ * from a non-atomic section, use ftrace_kill.
+ */
+void ftrace_kill_atomic(void)
+{
+       ftrace_disabled = 1;
+       ftrace_enabled = 0;
+       ftraced_suspend = -1;
+       clear_ftrace_function();
+}
+
+/**
  * ftrace_kill - totally shutdown ftrace
  *
  * This is a safety measure. If something was detected that seems