tracing: Fix failure path in ftrace_regex_open()
[pandora-kernel.git] / kernel / trace / ftrace.c
index 25edd5c..e70af98 100644 (file)
@@ -1016,71 +1016,35 @@ static int
 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
 {
        unsigned long ftrace_addr;
-       unsigned long ip, fl;
+       unsigned long flag = 0UL;
 
        ftrace_addr = (unsigned long)FTRACE_ADDR;
 
-       ip = rec->ip;
-
        /*
-        * If this record is not to be traced and
-        * it is not enabled then do nothing.
+        * If this record is not to be traced or we want to disable it,
+        * then disable it.
         *
-        * If this record is not to be traced and
-        * it is enabled then disable it.
+        * If we want to enable it and filtering is off, then enable it.
         *
+        * If we want to enable it and filtering is on, enable it only if
+        * it's filtered
         */
-       if (rec->flags & FTRACE_FL_NOTRACE) {
-               if (rec->flags & FTRACE_FL_ENABLED)
-                       rec->flags &= ~FTRACE_FL_ENABLED;
-               else
-                       return 0;
-
-       } else if (ftrace_filtered && enable) {
-               /*
-                * Filtering is on:
-                */
-
-               fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
-
-               /* Record is filtered and enabled, do nothing */
-               if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
-                       return 0;
-
-               /* Record is not filtered or enabled, do nothing */
-               if (!fl)
-                       return 0;
-
-               /* Record is not filtered but enabled, disable it */
-               if (fl == FTRACE_FL_ENABLED)
-                       rec->flags &= ~FTRACE_FL_ENABLED;
-               else
-               /* Otherwise record is filtered but not enabled, enable it */
-                       rec->flags |= FTRACE_FL_ENABLED;
-       } else {
-               /* Disable or not filtered */
-
-               if (enable) {
-                       /* if record is enabled, do nothing */
-                       if (rec->flags & FTRACE_FL_ENABLED)
-                               return 0;
-
-                       rec->flags |= FTRACE_FL_ENABLED;
-
-               } else {
+       if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
+               if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
+                       flag = FTRACE_FL_ENABLED;
+       }
 
-                       /* if record is not enabled, do nothing */
-                       if (!(rec->flags & FTRACE_FL_ENABLED))
-                               return 0;
+       /* If the state of this record hasn't changed, then do nothing */
+       if ((rec->flags & FTRACE_FL_ENABLED) == flag)
+               return 0;
 
-                       rec->flags &= ~FTRACE_FL_ENABLED;
-               }
+       if (flag) {
+               rec->flags |= FTRACE_FL_ENABLED;
+               return ftrace_make_call(rec, ftrace_addr);
        }
 
-       if (rec->flags & FTRACE_FL_ENABLED)
-               return ftrace_make_call(rec, ftrace_addr);
-       else
-               return ftrace_make_nop(NULL, rec, ftrace_addr);
+       rec->flags &= ~FTRACE_FL_ENABLED;
+       return ftrace_make_nop(NULL, rec, ftrace_addr);
 }
 
 static void ftrace_replace_code(int enable)
@@ -1359,11 +1323,10 @@ static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
 
 enum {
        FTRACE_ITER_FILTER      = (1 << 0),
-       FTRACE_ITER_CONT        = (1 << 1),
-       FTRACE_ITER_NOTRACE     = (1 << 2),
-       FTRACE_ITER_FAILURES    = (1 << 3),
-       FTRACE_ITER_PRINTALL    = (1 << 4),
-       FTRACE_ITER_HASH        = (1 << 5),
+       FTRACE_ITER_NOTRACE     = (1 << 1),
+       FTRACE_ITER_FAILURES    = (1 << 2),
+       FTRACE_ITER_PRINTALL    = (1 << 3),
+       FTRACE_ITER_HASH        = (1 << 4),
 };
 
 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
@@ -1373,9 +1336,7 @@ struct ftrace_iterator {
        int                     hidx;
        int                     idx;
        unsigned                flags;
-       unsigned char           buffer[FTRACE_BUFF_MAX+1];
-       unsigned                buffer_idx;
-       unsigned                filtered;
+       struct trace_parser     parser;
 };
 
 static void *
@@ -1438,18 +1399,13 @@ static int t_hash_show(struct seq_file *m, void *v)
 {
        struct ftrace_func_probe *rec;
        struct hlist_node *hnd = v;
-       char str[KSYM_SYMBOL_LEN];
 
        rec = hlist_entry(hnd, struct ftrace_func_probe, node);
 
        if (rec->ops->print)
                return rec->ops->print(m, rec->ip, rec->ops, rec->data);
 
-       kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
-       seq_printf(m, "%s:", str);
-
-       kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
-       seq_printf(m, "%s", str);
+       seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
 
        if (rec->data)
                seq_printf(m, ":%p", rec->data);
@@ -1547,7 +1503,6 @@ static int t_show(struct seq_file *m, void *v)
 {
        struct ftrace_iterator *iter = m->private;
        struct dyn_ftrace *rec = v;
-       char str[KSYM_SYMBOL_LEN];
 
        if (iter->flags & FTRACE_ITER_HASH)
                return t_hash_show(m, v);
@@ -1560,9 +1515,7 @@ static int t_show(struct seq_file *m, void *v)
        if (!rec)
                return 0;
 
-       kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
-
-       seq_printf(m, "%s\n", str);
+       seq_printf(m, "%ps\n", (void *)rec->ip);
 
        return 0;
 }
@@ -1601,17 +1554,6 @@ ftrace_avail_open(struct inode *inode, struct file *file)
        return ret;
 }
 
-int ftrace_avail_release(struct inode *inode, struct file *file)
-{
-       struct seq_file *m = (struct seq_file *)file->private_data;
-       struct ftrace_iterator *iter = m->private;
-
-       seq_release(inode, file);
-       kfree(iter);
-
-       return 0;
-}
-
 static int
 ftrace_failures_open(struct inode *inode, struct file *file)
 {
@@ -1660,6 +1602,11 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
        if (!iter)
                return -ENOMEM;
 
+       if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
+               kfree(iter);
+               return -ENOMEM;
+       }
+
        mutex_lock(&ftrace_regex_lock);
        if ((file->f_mode & FMODE_WRITE) &&
            (file->f_flags & O_TRUNC))
@@ -1674,8 +1621,10 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
                if (!ret) {
                        struct seq_file *m = file->private_data;
                        m->private = iter;
-               } else
+               } else {
+                       trace_parser_put(&iter->parser);
                        kfree(iter);
+               }
        } else
                file->private_data = iter;
        mutex_unlock(&ftrace_regex_lock);
@@ -2115,9 +2064,9 @@ __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
        int i, len = 0;
        char *search;
 
-       if (glob && (strcmp(glob, "*") || !strlen(glob)))
+       if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
                glob = NULL;
-       else {
+       else if (glob) {
                int not;
 
                type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
@@ -2252,11 +2201,10 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
                   size_t cnt, loff_t *ppos, int enable)
 {
        struct ftrace_iterator *iter;
-       char ch;
-       size_t read = 0;
-       ssize_t ret;
+       struct trace_parser *parser;
+       ssize_t ret, read;
 
-       if (!cnt || cnt < 0)
+       if (!cnt)
                return 0;
 
        mutex_lock(&ftrace_regex_lock);
@@ -2267,73 +2215,23 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
        } else
                iter = file->private_data;
 
-       if (!*ppos) {
-               iter->flags &= ~FTRACE_ITER_CONT;
-               iter->buffer_idx = 0;
-       }
+       parser = &iter->parser;
+       read = trace_get_user(parser, ubuf, cnt, ppos);
 
-       ret = get_user(ch, ubuf++);
-       if (ret)
-               goto out;
-       read++;
-       cnt--;
-
-       /*
-        * If the parser haven't finished with the last write,
-        * continue reading the user input without skipping spaces.
-        */
-       if (!(iter->flags & FTRACE_ITER_CONT)) {
-               /* skip white space */
-               while (cnt && isspace(ch)) {
-                       ret = get_user(ch, ubuf++);
-                       if (ret)
-                               goto out;
-                       read++;
-                       cnt--;
-               }
-
-               /* only spaces were written */
-               if (isspace(ch)) {
-                       *ppos += read;
-                       ret = read;
-                       goto out;
-               }
-
-               iter->buffer_idx = 0;
-       }
-
-       while (cnt && !isspace(ch)) {
-               if (iter->buffer_idx < FTRACE_BUFF_MAX)
-                       iter->buffer[iter->buffer_idx++] = ch;
-               else {
-                       ret = -EINVAL;
-                       goto out;
-               }
-               ret = get_user(ch, ubuf++);
+       if (read >= 0 && trace_parser_loaded(parser) &&
+           !trace_parser_cont(parser)) {
+               ret = ftrace_process_regex(parser->buffer,
+                                          parser->idx, enable);
                if (ret)
                        goto out;
-               read++;
-               cnt--;
-       }
 
-       if (isspace(ch)) {
-               iter->filtered++;
-               iter->buffer[iter->buffer_idx] = 0;
-               ret = ftrace_process_regex(iter->buffer,
-                                          iter->buffer_idx, enable);
-               if (ret)
-                       goto out;
-               iter->buffer_idx = 0;
-       } else {
-               iter->flags |= FTRACE_ITER_CONT;
-               iter->buffer[iter->buffer_idx++] = ch;
+               trace_parser_clear(parser);
        }
 
-       *ppos += read;
        ret = read;
- out:
-       mutex_unlock(&ftrace_regex_lock);
 
+       mutex_unlock(&ftrace_regex_lock);
+out:
        return ret;
 }
 
@@ -2438,6 +2336,7 @@ ftrace_regex_release(struct inode *inode, struct file *file, int enable)
 {
        struct seq_file *m = (struct seq_file *)file->private_data;
        struct ftrace_iterator *iter;
+       struct trace_parser *parser;
 
        mutex_lock(&ftrace_regex_lock);
        if (file->f_mode & FMODE_READ) {
@@ -2447,10 +2346,10 @@ ftrace_regex_release(struct inode *inode, struct file *file, int enable)
        } else
                iter = file->private_data;
 
-       if (iter->buffer_idx) {
-               iter->filtered++;
-               iter->buffer[iter->buffer_idx] = 0;
-               ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
+       parser = &iter->parser;
+       if (trace_parser_loaded(parser)) {
+               parser->buffer[parser->idx] = 0;
+               ftrace_match_records(parser->buffer, parser->idx, enable);
        }
 
        mutex_lock(&ftrace_lock);
@@ -2458,7 +2357,9 @@ ftrace_regex_release(struct inode *inode, struct file *file, int enable)
                ftrace_run_update_code(FTRACE_ENABLE_CALLS);
        mutex_unlock(&ftrace_lock);
 
+       trace_parser_put(parser);
        kfree(iter);
+
        mutex_unlock(&ftrace_regex_lock);
        return 0;
 }
@@ -2479,14 +2380,14 @@ static const struct file_operations ftrace_avail_fops = {
        .open = ftrace_avail_open,
        .read = seq_read,
        .llseek = seq_lseek,
-       .release = ftrace_avail_release,
+       .release = seq_release_private,
 };
 
 static const struct file_operations ftrace_failures_fops = {
        .open = ftrace_failures_open,
        .read = seq_read,
        .llseek = seq_lseek,
-       .release = ftrace_avail_release,
+       .release = seq_release_private,
 };
 
 static const struct file_operations ftrace_filter_fops = {
@@ -2515,11 +2416,9 @@ unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
 static void *
 __g_next(struct seq_file *m, loff_t *pos)
 {
-       unsigned long *array = m->private;
-
        if (*pos >= ftrace_graph_count)
                return NULL;
-       return &array[*pos];
+       return &ftrace_graph_funcs[*pos];
 }
 
 static void *
@@ -2548,7 +2447,6 @@ static void g_stop(struct seq_file *m, void *p)
 static int g_show(struct seq_file *m, void *v)
 {
        unsigned long *ptr = v;
-       char str[KSYM_SYMBOL_LEN];
 
        if (!ptr)
                return 0;
@@ -2558,9 +2456,7 @@ static int g_show(struct seq_file *m, void *v)
                return 0;
        }
 
-       kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
-
-       seq_printf(m, "%s\n", str);
+       seq_printf(m, "%ps\n", (void *)*ptr);
 
        return 0;
 }
@@ -2586,16 +2482,10 @@ ftrace_graph_open(struct inode *inode, struct file *file)
                ftrace_graph_count = 0;
                memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
        }
+       mutex_unlock(&graph_lock);
 
-       if (file->f_mode & FMODE_READ) {
+       if (file->f_mode & FMODE_READ)
                ret = seq_open(file, &ftrace_graph_seq_ops);
-               if (!ret) {
-                       struct seq_file *m = file->private_data;
-                       m->private = ftrace_graph_funcs;
-               }
-       } else
-               file->private_data = ftrace_graph_funcs;
-       mutex_unlock(&graph_lock);
 
        return ret;
 }
@@ -2663,12 +2553,8 @@ static ssize_t
 ftrace_graph_write(struct file *file, const char __user *ubuf,
                   size_t cnt, loff_t *ppos)
 {
-       unsigned char buffer[FTRACE_BUFF_MAX+1];
-       unsigned long *array;
-       size_t read = 0;
-       ssize_t ret;
-       int index = 0;
-       char ch;
+       struct trace_parser parser;
+       ssize_t read, ret;
 
        if (!cnt || cnt < 0)
                return 0;
@@ -2677,60 +2563,31 @@ ftrace_graph_write(struct file *file, const char __user *ubuf,
 
        if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
                ret = -EBUSY;
-               goto out;
+               goto out_unlock;
        }
 
-       if (file->f_mode & FMODE_READ) {
-               struct seq_file *m = file->private_data;
-               array = m->private;
-       } else
-               array = file->private_data;
-
-       ret = get_user(ch, ubuf++);
-       if (ret)
-               goto out;
-       read++;
-       cnt--;
-
-       /* skip white space */
-       while (cnt && isspace(ch)) {
-               ret = get_user(ch, ubuf++);
-               if (ret)
-                       goto out;
-               read++;
-               cnt--;
+       if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
+               ret = -ENOMEM;
+               goto out_unlock;
        }
 
-       if (isspace(ch)) {
-               *ppos += read;
-               ret = read;
-               goto out;
-       }
+       read = trace_get_user(&parser, ubuf, cnt, ppos);
 
-       while (cnt && !isspace(ch)) {
-               if (index < FTRACE_BUFF_MAX)
-                       buffer[index++] = ch;
-               else {
-                       ret = -EINVAL;
-                       goto out;
-               }
-               ret = get_user(ch, ubuf++);
+       if (read >= 0 && trace_parser_loaded((&parser))) {
+               parser.buffer[parser.idx] = 0;
+
+               /* we allow only one expression at a time */
+               ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
+                                       parser.buffer);
                if (ret)
-                       goto out;
-               read++;
-               cnt--;
+                       goto out_free;
        }
-       buffer[index] = 0;
-
-       /* we allow only one expression at a time */
-       ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
-       if (ret)
-               goto out;
-
-       file->f_pos += read;
 
        ret = read;
- out:
+
+out_free:
+       trace_parser_put(&parser);
+out_unlock:
        mutex_unlock(&graph_lock);
 
        return ret;