]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - kernel/trace/trace_events_filter.c
0a3e0502b50790e5e955c5f8b7a399aff74ba07e
[karo-tx-linux.git] / kernel / trace / trace_events_filter.c
1 /*
2  * trace_events_filter - generic event filtering
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
19  */
20
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <linux/mutex.h>
24 #include <linux/perf_event.h>
25 #include <linux/slab.h>
26
27 #include "trace.h"
28 #include "trace_output.h"
29
30 enum filter_op_ids
31 {
32         OP_OR,
33         OP_AND,
34         OP_GLOB,
35         OP_NE,
36         OP_EQ,
37         OP_LT,
38         OP_LE,
39         OP_GT,
40         OP_GE,
41         OP_NONE,
42         OP_OPEN_PAREN,
43 };
44
45 struct filter_op {
46         int id;
47         char *string;
48         int precedence;
49 };
50
51 static struct filter_op filter_ops[] = {
52         { OP_OR,        "||",           1 },
53         { OP_AND,       "&&",           2 },
54         { OP_GLOB,      "~",            4 },
55         { OP_NE,        "!=",           4 },
56         { OP_EQ,        "==",           4 },
57         { OP_LT,        "<",            5 },
58         { OP_LE,        "<=",           5 },
59         { OP_GT,        ">",            5 },
60         { OP_GE,        ">=",           5 },
61         { OP_NONE,      "OP_NONE",      0 },
62         { OP_OPEN_PAREN, "(",           0 },
63 };
64
65 enum {
66         FILT_ERR_NONE,
67         FILT_ERR_INVALID_OP,
68         FILT_ERR_UNBALANCED_PAREN,
69         FILT_ERR_TOO_MANY_OPERANDS,
70         FILT_ERR_OPERAND_TOO_LONG,
71         FILT_ERR_FIELD_NOT_FOUND,
72         FILT_ERR_ILLEGAL_FIELD_OP,
73         FILT_ERR_ILLEGAL_INTVAL,
74         FILT_ERR_BAD_SUBSYS_FILTER,
75         FILT_ERR_TOO_MANY_PREDS,
76         FILT_ERR_MISSING_FIELD,
77         FILT_ERR_INVALID_FILTER,
78 };
79
80 static char *err_text[] = {
81         "No error",
82         "Invalid operator",
83         "Unbalanced parens",
84         "Too many operands",
85         "Operand too long",
86         "Field not found",
87         "Illegal operation for field type",
88         "Illegal integer value",
89         "Couldn't find or set field in one of a subsystem's events",
90         "Too many terms in predicate expression",
91         "Missing field name and/or value",
92         "Meaningless filter expression",
93 };
94
95 struct opstack_op {
96         int op;
97         struct list_head list;
98 };
99
100 struct postfix_elt {
101         int op;
102         char *operand;
103         struct list_head list;
104 };
105
106 struct filter_parse_state {
107         struct filter_op *ops;
108         struct list_head opstack;
109         struct list_head postfix;
110         int lasterr;
111         int lasterr_pos;
112
113         struct {
114                 char *string;
115                 unsigned int cnt;
116                 unsigned int tail;
117         } infix;
118
119         struct {
120                 char string[MAX_FILTER_STR_VAL];
121                 int pos;
122                 unsigned int tail;
123         } operand;
124 };
125
126 struct pred_stack {
127         struct filter_pred      **preds;
128         int                     index;
129 };
130
131 #define DEFINE_COMPARISON_PRED(type)                                    \
132 static int filter_pred_##type(struct filter_pred *pred, void *event)    \
133 {                                                                       \
134         type *addr = (type *)(event + pred->offset);                    \
135         type val = (type)pred->val;                                     \
136         int match = 0;                                                  \
137                                                                         \
138         switch (pred->op) {                                             \
139         case OP_LT:                                                     \
140                 match = (*addr < val);                                  \
141                 break;                                                  \
142         case OP_LE:                                                     \
143                 match = (*addr <= val);                                 \
144                 break;                                                  \
145         case OP_GT:                                                     \
146                 match = (*addr > val);                                  \
147                 break;                                                  \
148         case OP_GE:                                                     \
149                 match = (*addr >= val);                                 \
150                 break;                                                  \
151         default:                                                        \
152                 break;                                                  \
153         }                                                               \
154                                                                         \
155         return match;                                                   \
156 }
157
158 #define DEFINE_EQUALITY_PRED(size)                                      \
159 static int filter_pred_##size(struct filter_pred *pred, void *event)    \
160 {                                                                       \
161         u##size *addr = (u##size *)(event + pred->offset);              \
162         u##size val = (u##size)pred->val;                               \
163         int match;                                                      \
164                                                                         \
165         match = (val == *addr) ^ pred->not;                             \
166                                                                         \
167         return match;                                                   \
168 }
169
170 DEFINE_COMPARISON_PRED(s64);
171 DEFINE_COMPARISON_PRED(u64);
172 DEFINE_COMPARISON_PRED(s32);
173 DEFINE_COMPARISON_PRED(u32);
174 DEFINE_COMPARISON_PRED(s16);
175 DEFINE_COMPARISON_PRED(u16);
176 DEFINE_COMPARISON_PRED(s8);
177 DEFINE_COMPARISON_PRED(u8);
178
179 DEFINE_EQUALITY_PRED(64);
180 DEFINE_EQUALITY_PRED(32);
181 DEFINE_EQUALITY_PRED(16);
182 DEFINE_EQUALITY_PRED(8);
183
184 /* Filter predicate for fixed sized arrays of characters */
185 static int filter_pred_string(struct filter_pred *pred, void *event)
186 {
187         char *addr = (char *)(event + pred->offset);
188         int cmp, match;
189
190         cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
191
192         match = cmp ^ pred->not;
193
194         return match;
195 }
196
197 /* Filter predicate for char * pointers */
198 static int filter_pred_pchar(struct filter_pred *pred, void *event)
199 {
200         char **addr = (char **)(event + pred->offset);
201         int cmp, match;
202         int len = strlen(*addr) + 1;    /* including tailing '\0' */
203
204         cmp = pred->regex.match(*addr, &pred->regex, len);
205
206         match = cmp ^ pred->not;
207
208         return match;
209 }
210
211 /*
212  * Filter predicate for dynamic sized arrays of characters.
213  * These are implemented through a list of strings at the end
214  * of the entry.
215  * Also each of these strings have a field in the entry which
216  * contains its offset from the beginning of the entry.
217  * We have then first to get this field, dereference it
218  * and add it to the address of the entry, and at last we have
219  * the address of the string.
220  */
221 static int filter_pred_strloc(struct filter_pred *pred, void *event)
222 {
223         u32 str_item = *(u32 *)(event + pred->offset);
224         int str_loc = str_item & 0xffff;
225         int str_len = str_item >> 16;
226         char *addr = (char *)(event + str_loc);
227         int cmp, match;
228
229         cmp = pred->regex.match(addr, &pred->regex, str_len);
230
231         match = cmp ^ pred->not;
232
233         return match;
234 }
235
236 static int filter_pred_none(struct filter_pred *pred, void *event)
237 {
238         return 0;
239 }
240
241 /*
242  * regex_match_foo - Basic regex callbacks
243  *
244  * @str: the string to be searched
245  * @r:   the regex structure containing the pattern string
246  * @len: the length of the string to be searched (including '\0')
247  *
248  * Note:
249  * - @str might not be NULL-terminated if it's of type DYN_STRING
250  *   or STATIC_STRING
251  */
252
253 static int regex_match_full(char *str, struct regex *r, int len)
254 {
255         if (strncmp(str, r->pattern, len) == 0)
256                 return 1;
257         return 0;
258 }
259
260 static int regex_match_front(char *str, struct regex *r, int len)
261 {
262         if (strncmp(str, r->pattern, r->len) == 0)
263                 return 1;
264         return 0;
265 }
266
267 static int regex_match_middle(char *str, struct regex *r, int len)
268 {
269         if (strnstr(str, r->pattern, len))
270                 return 1;
271         return 0;
272 }
273
274 static int regex_match_end(char *str, struct regex *r, int len)
275 {
276         int strlen = len - 1;
277
278         if (strlen >= r->len &&
279             memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
280                 return 1;
281         return 0;
282 }
283
284 /**
285  * filter_parse_regex - parse a basic regex
286  * @buff:   the raw regex
287  * @len:    length of the regex
288  * @search: will point to the beginning of the string to compare
289  * @not:    tell whether the match will have to be inverted
290  *
291  * This passes in a buffer containing a regex and this function will
292  * set search to point to the search part of the buffer and
293  * return the type of search it is (see enum above).
294  * This does modify buff.
295  *
296  * Returns enum type.
297  *  search returns the pointer to use for comparison.
298  *  not returns 1 if buff started with a '!'
299  *     0 otherwise.
300  */
301 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
302 {
303         int type = MATCH_FULL;
304         int i;
305
306         if (buff[0] == '!') {
307                 *not = 1;
308                 buff++;
309                 len--;
310         } else
311                 *not = 0;
312
313         *search = buff;
314
315         for (i = 0; i < len; i++) {
316                 if (buff[i] == '*') {
317                         if (!i) {
318                                 *search = buff + 1;
319                                 type = MATCH_END_ONLY;
320                         } else {
321                                 if (type == MATCH_END_ONLY)
322                                         type = MATCH_MIDDLE_ONLY;
323                                 else
324                                         type = MATCH_FRONT_ONLY;
325                                 buff[i] = 0;
326                                 break;
327                         }
328                 }
329         }
330
331         return type;
332 }
333
334 static void filter_build_regex(struct filter_pred *pred)
335 {
336         struct regex *r = &pred->regex;
337         char *search;
338         enum regex_type type = MATCH_FULL;
339         int not = 0;
340
341         if (pred->op == OP_GLOB) {
342                 type = filter_parse_regex(r->pattern, r->len, &search, &not);
343                 r->len = strlen(search);
344                 memmove(r->pattern, search, r->len+1);
345         }
346
347         switch (type) {
348         case MATCH_FULL:
349                 r->match = regex_match_full;
350                 break;
351         case MATCH_FRONT_ONLY:
352                 r->match = regex_match_front;
353                 break;
354         case MATCH_MIDDLE_ONLY:
355                 r->match = regex_match_middle;
356                 break;
357         case MATCH_END_ONLY:
358                 r->match = regex_match_end;
359                 break;
360         }
361
362         pred->not ^= not;
363 }
364
365 enum move_type {
366         MOVE_DOWN,
367         MOVE_UP_FROM_LEFT,
368         MOVE_UP_FROM_RIGHT
369 };
370
371 static struct filter_pred *
372 get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
373                 int index, enum move_type *move)
374 {
375         if (pred->parent & FILTER_PRED_IS_RIGHT)
376                 *move = MOVE_UP_FROM_RIGHT;
377         else
378                 *move = MOVE_UP_FROM_LEFT;
379         pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
380
381         return pred;
382 }
383
384 /* return 1 if event matches, 0 otherwise (discard) */
385 int filter_match_preds(struct event_filter *filter, void *rec)
386 {
387         int match = -1;
388         enum move_type move = MOVE_DOWN;
389         struct filter_pred *preds;
390         struct filter_pred *pred;
391         struct filter_pred *root;
392         int n_preds = ACCESS_ONCE(filter->n_preds);
393         int done = 0;
394
395         /* no filter is considered a match */
396         if (!n_preds)
397                 return 1;
398
399         /*
400          * n_preds, root and filter->preds are protect with preemption disabled.
401          */
402         preds = rcu_dereference_sched(filter->preds);
403         root = rcu_dereference_sched(filter->root);
404         if (!root)
405                 return 1;
406
407         pred = root;
408
409         /* match is currently meaningless */
410         match = -1;
411
412         do {
413                 switch (move) {
414                 case MOVE_DOWN:
415                         /* only AND and OR have children */
416                         if (pred->left != FILTER_PRED_INVALID) {
417                                 /* keep going to leaf node */
418                                 pred = &preds[pred->left];
419                                 continue;
420                         }
421                         match = pred->fn(pred, rec);
422                         /* If this pred is the only pred */
423                         if (pred == root)
424                                 break;
425                         pred = get_pred_parent(pred, preds,
426                                                pred->parent, &move);
427                         continue;
428                 case MOVE_UP_FROM_LEFT:
429                         /*
430                          * Check for short circuits.
431                          *
432                          * Optimization: !!match == (pred->op == OP_OR)
433                          *   is the same as:
434                          * if ((match && pred->op == OP_OR) ||
435                          *     (!match && pred->op == OP_AND))
436                          */
437                         if (!!match == (pred->op == OP_OR)) {
438                                 if (pred == root)
439                                         break;
440                                 pred = get_pred_parent(pred, preds,
441                                                        pred->parent, &move);
442                                 continue;
443                         }
444                         /* now go down the right side of the tree. */
445                         pred = &preds[pred->right];
446                         move = MOVE_DOWN;
447                         continue;
448                 case MOVE_UP_FROM_RIGHT:
449                         /* We finished this equation. */
450                         if (pred == root)
451                                 break;
452                         pred = get_pred_parent(pred, preds,
453                                                pred->parent, &move);
454                         continue;
455                 }
456                 done = 1;
457         } while (!done);
458
459         return match;
460 }
461 EXPORT_SYMBOL_GPL(filter_match_preds);
462
463 static void parse_error(struct filter_parse_state *ps, int err, int pos)
464 {
465         ps->lasterr = err;
466         ps->lasterr_pos = pos;
467 }
468
469 static void remove_filter_string(struct event_filter *filter)
470 {
471         kfree(filter->filter_string);
472         filter->filter_string = NULL;
473 }
474
475 static int replace_filter_string(struct event_filter *filter,
476                                  char *filter_string)
477 {
478         kfree(filter->filter_string);
479         filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
480         if (!filter->filter_string)
481                 return -ENOMEM;
482
483         return 0;
484 }
485
486 static int append_filter_string(struct event_filter *filter,
487                                 char *string)
488 {
489         int newlen;
490         char *new_filter_string;
491
492         BUG_ON(!filter->filter_string);
493         newlen = strlen(filter->filter_string) + strlen(string) + 1;
494         new_filter_string = kmalloc(newlen, GFP_KERNEL);
495         if (!new_filter_string)
496                 return -ENOMEM;
497
498         strcpy(new_filter_string, filter->filter_string);
499         strcat(new_filter_string, string);
500         kfree(filter->filter_string);
501         filter->filter_string = new_filter_string;
502
503         return 0;
504 }
505
506 static void append_filter_err(struct filter_parse_state *ps,
507                               struct event_filter *filter)
508 {
509         int pos = ps->lasterr_pos;
510         char *buf, *pbuf;
511
512         buf = (char *)__get_free_page(GFP_TEMPORARY);
513         if (!buf)
514                 return;
515
516         append_filter_string(filter, "\n");
517         memset(buf, ' ', PAGE_SIZE);
518         if (pos > PAGE_SIZE - 128)
519                 pos = 0;
520         buf[pos] = '^';
521         pbuf = &buf[pos] + 1;
522
523         sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
524         append_filter_string(filter, buf);
525         free_page((unsigned long) buf);
526 }
527
528 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
529 {
530         struct event_filter *filter = call->filter;
531
532         mutex_lock(&event_mutex);
533         if (filter && filter->filter_string)
534                 trace_seq_printf(s, "%s\n", filter->filter_string);
535         else
536                 trace_seq_printf(s, "none\n");
537         mutex_unlock(&event_mutex);
538 }
539
540 void print_subsystem_event_filter(struct event_subsystem *system,
541                                   struct trace_seq *s)
542 {
543         struct event_filter *filter = system->filter;
544
545         mutex_lock(&event_mutex);
546         if (filter && filter->filter_string)
547                 trace_seq_printf(s, "%s\n", filter->filter_string);
548         else
549                 trace_seq_printf(s, "none\n");
550         mutex_unlock(&event_mutex);
551 }
552
553 static struct ftrace_event_field *
554 __find_event_field(struct list_head *head, char *name)
555 {
556         struct ftrace_event_field *field;
557
558         list_for_each_entry(field, head, link) {
559                 if (!strcmp(field->name, name))
560                         return field;
561         }
562
563         return NULL;
564 }
565
566 static struct ftrace_event_field *
567 find_event_field(struct ftrace_event_call *call, char *name)
568 {
569         struct ftrace_event_field *field;
570         struct list_head *head;
571
572         field = __find_event_field(&ftrace_common_fields, name);
573         if (field)
574                 return field;
575
576         head = trace_get_fields(call);
577         return __find_event_field(head, name);
578 }
579
580 static void filter_free_pred(struct filter_pred *pred)
581 {
582         if (!pred)
583                 return;
584
585         kfree(pred->field_name);
586         kfree(pred);
587 }
588
589 static void filter_clear_pred(struct filter_pred *pred)
590 {
591         kfree(pred->field_name);
592         pred->field_name = NULL;
593         pred->regex.len = 0;
594 }
595
596 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
597 {
598         stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
599         if (!stack->preds)
600                 return -ENOMEM;
601         stack->index = n_preds;
602         return 0;
603 }
604
605 static void __free_pred_stack(struct pred_stack *stack)
606 {
607         kfree(stack->preds);
608         stack->index = 0;
609 }
610
611 static int __push_pred_stack(struct pred_stack *stack,
612                              struct filter_pred *pred)
613 {
614         int index = stack->index;
615
616         if (WARN_ON(index == 0))
617                 return -ENOSPC;
618
619         stack->preds[--index] = pred;
620         stack->index = index;
621         return 0;
622 }
623
624 static struct filter_pred *
625 __pop_pred_stack(struct pred_stack *stack)
626 {
627         struct filter_pred *pred;
628         int index = stack->index;
629
630         pred = stack->preds[index++];
631         if (!pred)
632                 return NULL;
633
634         stack->index = index;
635         return pred;
636 }
637
638 static int filter_set_pred(struct event_filter *filter,
639                            int idx,
640                            struct pred_stack *stack,
641                            struct filter_pred *src,
642                            filter_pred_fn_t fn)
643 {
644         struct filter_pred *dest = &filter->preds[idx];
645         struct filter_pred *left;
646         struct filter_pred *right;
647
648         *dest = *src;
649         if (src->field_name) {
650                 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
651                 if (!dest->field_name)
652                         return -ENOMEM;
653         }
654         dest->fn = fn;
655         dest->index = idx;
656
657         if (dest->op == OP_OR || dest->op == OP_AND) {
658                 right = __pop_pred_stack(stack);
659                 left = __pop_pred_stack(stack);
660                 if (!left || !right)
661                         return -EINVAL;
662                 dest->left = left->index;
663                 dest->right = right->index;
664                 left->parent = dest->index;
665                 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
666         } else
667                 /*
668                  * Make dest->left invalid to be used as a quick
669                  * way to know this is a leaf node.
670                  */
671                 dest->left = FILTER_PRED_INVALID;
672
673         return __push_pred_stack(stack, dest);
674 }
675
676 static void __free_preds(struct event_filter *filter)
677 {
678         int i;
679
680         if (filter->preds) {
681                 for (i = 0; i < filter->a_preds; i++)
682                         kfree(filter->preds[i].field_name);
683                 kfree(filter->preds);
684                 filter->preds = NULL;
685         }
686         filter->a_preds = 0;
687         filter->n_preds = 0;
688 }
689
690 static void reset_preds(struct event_filter *filter)
691 {
692         int n_preds = filter->n_preds;
693         int i;
694
695         filter->n_preds = 0;
696         filter->root = NULL;
697         if (!filter->preds)
698                 return;
699
700         for (i = 0; i < n_preds; i++)
701                 filter->preds[i].fn = filter_pred_none;
702 }
703
704 static void filter_disable_preds(struct ftrace_event_call *call)
705 {
706         struct event_filter *filter = call->filter;
707
708         call->flags &= ~TRACE_EVENT_FL_FILTERED;
709         reset_preds(filter);
710 }
711
712 static void __free_filter(struct event_filter *filter)
713 {
714         if (!filter)
715                 return;
716
717         __free_preds(filter);
718         kfree(filter->filter_string);
719         kfree(filter);
720 }
721
722 void destroy_preds(struct ftrace_event_call *call)
723 {
724         __free_filter(call->filter);
725         call->filter = NULL;
726         call->flags &= ~TRACE_EVENT_FL_FILTERED;
727 }
728
729 static struct event_filter *__alloc_filter(void)
730 {
731         struct event_filter *filter;
732
733         filter = kzalloc(sizeof(*filter), GFP_KERNEL);
734         if (!filter)
735                 return ERR_PTR(-ENOMEM);
736
737         filter->n_preds = 0;
738
739         return filter;
740 }
741
742 static int __alloc_preds(struct event_filter *filter, int n_preds)
743 {
744         struct filter_pred *pred;
745         int i;
746
747         if (filter->preds) {
748                 if (filter->a_preds < n_preds) {
749                         /*
750                          * We need to reallocate.
751                          * We should have already have zeroed out
752                          * the pred count and called synchronized_sched()
753                          * to make sure no one is using the preds.
754                          */
755                         if (WARN_ON_ONCE(filter->n_preds)) {
756                                 /* We need to reset it now */
757                                 filter->n_preds = 0;
758                                 synchronize_sched();
759                         }
760                         __free_preds(filter);
761                 }
762         }
763
764         if (!filter->preds) {
765                 filter->preds =
766                         kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
767                 filter->a_preds = n_preds;
768         }
769         if (!filter->preds)
770                 return -ENOMEM;
771
772         if (WARN_ON(filter->a_preds < n_preds))
773                 return -EINVAL;
774
775         for (i = 0; i < n_preds; i++) {
776                 pred = &filter->preds[i];
777                 pred->fn = filter_pred_none;
778         }
779
780         return 0;
781 }
782
783 static int init_filter(struct ftrace_event_call *call)
784 {
785         if (call->filter)
786                 return 0;
787
788         call->flags &= ~TRACE_EVENT_FL_FILTERED;
789         call->filter = __alloc_filter();
790         if (IS_ERR(call->filter))
791                 return PTR_ERR(call->filter);
792
793         return 0;
794 }
795
796 static int init_subsystem_preds(struct event_subsystem *system)
797 {
798         struct ftrace_event_call *call;
799         int err;
800
801         list_for_each_entry(call, &ftrace_events, list) {
802                 if (strcmp(call->class->system, system->name) != 0)
803                         continue;
804
805                 err = init_filter(call);
806                 if (err)
807                         return err;
808         }
809
810         return 0;
811 }
812
813 static void filter_free_subsystem_preds(struct event_subsystem *system)
814 {
815         struct ftrace_event_call *call;
816
817         list_for_each_entry(call, &ftrace_events, list) {
818                 if (strcmp(call->class->system, system->name) != 0)
819                         continue;
820
821                 filter_disable_preds(call);
822                 remove_filter_string(call->filter);
823         }
824 }
825
826 static int filter_add_pred_fn(struct filter_parse_state *ps,
827                               struct ftrace_event_call *call,
828                               struct event_filter *filter,
829                               struct filter_pred *pred,
830                               struct pred_stack *stack,
831                               filter_pred_fn_t fn)
832 {
833         int idx, err;
834
835         if (WARN_ON(filter->n_preds == filter->a_preds)) {
836                 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
837                 return -ENOSPC;
838         }
839
840         idx = filter->n_preds;
841         filter_clear_pred(&filter->preds[idx]);
842         err = filter_set_pred(filter, idx, stack, pred, fn);
843         if (err)
844                 return err;
845
846         filter->n_preds++;
847
848         return 0;
849 }
850
851 int filter_assign_type(const char *type)
852 {
853         if (strstr(type, "__data_loc") && strstr(type, "char"))
854                 return FILTER_DYN_STRING;
855
856         if (strchr(type, '[') && strstr(type, "char"))
857                 return FILTER_STATIC_STRING;
858
859         return FILTER_OTHER;
860 }
861
862 static bool is_string_field(struct ftrace_event_field *field)
863 {
864         return field->filter_type == FILTER_DYN_STRING ||
865                field->filter_type == FILTER_STATIC_STRING ||
866                field->filter_type == FILTER_PTR_STRING;
867 }
868
869 static int is_legal_op(struct ftrace_event_field *field, int op)
870 {
871         if (is_string_field(field) &&
872             (op != OP_EQ && op != OP_NE && op != OP_GLOB))
873                 return 0;
874         if (!is_string_field(field) && op == OP_GLOB)
875                 return 0;
876
877         return 1;
878 }
879
880 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
881                                              int field_is_signed)
882 {
883         filter_pred_fn_t fn = NULL;
884
885         switch (field_size) {
886         case 8:
887                 if (op == OP_EQ || op == OP_NE)
888                         fn = filter_pred_64;
889                 else if (field_is_signed)
890                         fn = filter_pred_s64;
891                 else
892                         fn = filter_pred_u64;
893                 break;
894         case 4:
895                 if (op == OP_EQ || op == OP_NE)
896                         fn = filter_pred_32;
897                 else if (field_is_signed)
898                         fn = filter_pred_s32;
899                 else
900                         fn = filter_pred_u32;
901                 break;
902         case 2:
903                 if (op == OP_EQ || op == OP_NE)
904                         fn = filter_pred_16;
905                 else if (field_is_signed)
906                         fn = filter_pred_s16;
907                 else
908                         fn = filter_pred_u16;
909                 break;
910         case 1:
911                 if (op == OP_EQ || op == OP_NE)
912                         fn = filter_pred_8;
913                 else if (field_is_signed)
914                         fn = filter_pred_s8;
915                 else
916                         fn = filter_pred_u8;
917                 break;
918         }
919
920         return fn;
921 }
922
923 static int filter_add_pred(struct filter_parse_state *ps,
924                            struct ftrace_event_call *call,
925                            struct event_filter *filter,
926                            struct filter_pred *pred,
927                            struct pred_stack *stack,
928                            bool dry_run)
929 {
930         struct ftrace_event_field *field;
931         filter_pred_fn_t fn;
932         unsigned long long val;
933         int ret;
934
935         fn = pred->fn = filter_pred_none;
936
937         if (pred->op == OP_AND)
938                 goto add_pred_fn;
939         else if (pred->op == OP_OR)
940                 goto add_pred_fn;
941
942         field = find_event_field(call, pred->field_name);
943         if (!field) {
944                 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
945                 return -EINVAL;
946         }
947
948         pred->offset = field->offset;
949
950         if (!is_legal_op(field, pred->op)) {
951                 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
952                 return -EINVAL;
953         }
954
955         if (is_string_field(field)) {
956                 filter_build_regex(pred);
957
958                 if (field->filter_type == FILTER_STATIC_STRING) {
959                         fn = filter_pred_string;
960                         pred->regex.field_len = field->size;
961                 } else if (field->filter_type == FILTER_DYN_STRING)
962                         fn = filter_pred_strloc;
963                 else
964                         fn = filter_pred_pchar;
965         } else {
966                 if (field->is_signed)
967                         ret = strict_strtoll(pred->regex.pattern, 0, &val);
968                 else
969                         ret = strict_strtoull(pred->regex.pattern, 0, &val);
970                 if (ret) {
971                         parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
972                         return -EINVAL;
973                 }
974                 pred->val = val;
975
976                 fn = select_comparison_fn(pred->op, field->size,
977                                           field->is_signed);
978                 if (!fn) {
979                         parse_error(ps, FILT_ERR_INVALID_OP, 0);
980                         return -EINVAL;
981                 }
982         }
983
984         if (pred->op == OP_NE)
985                 pred->not = 1;
986
987 add_pred_fn:
988         if (!dry_run)
989                 return filter_add_pred_fn(ps, call, filter, pred, stack, fn);
990         return 0;
991 }
992
993 static void parse_init(struct filter_parse_state *ps,
994                        struct filter_op *ops,
995                        char *infix_string)
996 {
997         memset(ps, '\0', sizeof(*ps));
998
999         ps->infix.string = infix_string;
1000         ps->infix.cnt = strlen(infix_string);
1001         ps->ops = ops;
1002
1003         INIT_LIST_HEAD(&ps->opstack);
1004         INIT_LIST_HEAD(&ps->postfix);
1005 }
1006
1007 static char infix_next(struct filter_parse_state *ps)
1008 {
1009         ps->infix.cnt--;
1010
1011         return ps->infix.string[ps->infix.tail++];
1012 }
1013
1014 static char infix_peek(struct filter_parse_state *ps)
1015 {
1016         if (ps->infix.tail == strlen(ps->infix.string))
1017                 return 0;
1018
1019         return ps->infix.string[ps->infix.tail];
1020 }
1021
1022 static void infix_advance(struct filter_parse_state *ps)
1023 {
1024         ps->infix.cnt--;
1025         ps->infix.tail++;
1026 }
1027
1028 static inline int is_precedence_lower(struct filter_parse_state *ps,
1029                                       int a, int b)
1030 {
1031         return ps->ops[a].precedence < ps->ops[b].precedence;
1032 }
1033
1034 static inline int is_op_char(struct filter_parse_state *ps, char c)
1035 {
1036         int i;
1037
1038         for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1039                 if (ps->ops[i].string[0] == c)
1040                         return 1;
1041         }
1042
1043         return 0;
1044 }
1045
1046 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1047 {
1048         char nextc = infix_peek(ps);
1049         char opstr[3];
1050         int i;
1051
1052         opstr[0] = firstc;
1053         opstr[1] = nextc;
1054         opstr[2] = '\0';
1055
1056         for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1057                 if (!strcmp(opstr, ps->ops[i].string)) {
1058                         infix_advance(ps);
1059                         return ps->ops[i].id;
1060                 }
1061         }
1062
1063         opstr[1] = '\0';
1064
1065         for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1066                 if (!strcmp(opstr, ps->ops[i].string))
1067                         return ps->ops[i].id;
1068         }
1069
1070         return OP_NONE;
1071 }
1072
1073 static inline void clear_operand_string(struct filter_parse_state *ps)
1074 {
1075         memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1076         ps->operand.tail = 0;
1077 }
1078
1079 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1080 {
1081         if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1082                 return -EINVAL;
1083
1084         ps->operand.string[ps->operand.tail++] = c;
1085
1086         return 0;
1087 }
1088
1089 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1090 {
1091         struct opstack_op *opstack_op;
1092
1093         opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1094         if (!opstack_op)
1095                 return -ENOMEM;
1096
1097         opstack_op->op = op;
1098         list_add(&opstack_op->list, &ps->opstack);
1099
1100         return 0;
1101 }
1102
1103 static int filter_opstack_empty(struct filter_parse_state *ps)
1104 {
1105         return list_empty(&ps->opstack);
1106 }
1107
1108 static int filter_opstack_top(struct filter_parse_state *ps)
1109 {
1110         struct opstack_op *opstack_op;
1111
1112         if (filter_opstack_empty(ps))
1113                 return OP_NONE;
1114
1115         opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1116
1117         return opstack_op->op;
1118 }
1119
1120 static int filter_opstack_pop(struct filter_parse_state *ps)
1121 {
1122         struct opstack_op *opstack_op;
1123         int op;
1124
1125         if (filter_opstack_empty(ps))
1126                 return OP_NONE;
1127
1128         opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1129         op = opstack_op->op;
1130         list_del(&opstack_op->list);
1131
1132         kfree(opstack_op);
1133
1134         return op;
1135 }
1136
1137 static void filter_opstack_clear(struct filter_parse_state *ps)
1138 {
1139         while (!filter_opstack_empty(ps))
1140                 filter_opstack_pop(ps);
1141 }
1142
1143 static char *curr_operand(struct filter_parse_state *ps)
1144 {
1145         return ps->operand.string;
1146 }
1147
1148 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1149 {
1150         struct postfix_elt *elt;
1151
1152         elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1153         if (!elt)
1154                 return -ENOMEM;
1155
1156         elt->op = OP_NONE;
1157         elt->operand = kstrdup(operand, GFP_KERNEL);
1158         if (!elt->operand) {
1159                 kfree(elt);
1160                 return -ENOMEM;
1161         }
1162
1163         list_add_tail(&elt->list, &ps->postfix);
1164
1165         return 0;
1166 }
1167
1168 static int postfix_append_op(struct filter_parse_state *ps, int op)
1169 {
1170         struct postfix_elt *elt;
1171
1172         elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1173         if (!elt)
1174                 return -ENOMEM;
1175
1176         elt->op = op;
1177         elt->operand = NULL;
1178
1179         list_add_tail(&elt->list, &ps->postfix);
1180
1181         return 0;
1182 }
1183
1184 static void postfix_clear(struct filter_parse_state *ps)
1185 {
1186         struct postfix_elt *elt;
1187
1188         while (!list_empty(&ps->postfix)) {
1189                 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1190                 list_del(&elt->list);
1191                 kfree(elt->operand);
1192                 kfree(elt);
1193         }
1194 }
1195
1196 static int filter_parse(struct filter_parse_state *ps)
1197 {
1198         int in_string = 0;
1199         int op, top_op;
1200         char ch;
1201
1202         while ((ch = infix_next(ps))) {
1203                 if (ch == '"') {
1204                         in_string ^= 1;
1205                         continue;
1206                 }
1207
1208                 if (in_string)
1209                         goto parse_operand;
1210
1211                 if (isspace(ch))
1212                         continue;
1213
1214                 if (is_op_char(ps, ch)) {
1215                         op = infix_get_op(ps, ch);
1216                         if (op == OP_NONE) {
1217                                 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1218                                 return -EINVAL;
1219                         }
1220
1221                         if (strlen(curr_operand(ps))) {
1222                                 postfix_append_operand(ps, curr_operand(ps));
1223                                 clear_operand_string(ps);
1224                         }
1225
1226                         while (!filter_opstack_empty(ps)) {
1227                                 top_op = filter_opstack_top(ps);
1228                                 if (!is_precedence_lower(ps, top_op, op)) {
1229                                         top_op = filter_opstack_pop(ps);
1230                                         postfix_append_op(ps, top_op);
1231                                         continue;
1232                                 }
1233                                 break;
1234                         }
1235
1236                         filter_opstack_push(ps, op);
1237                         continue;
1238                 }
1239
1240                 if (ch == '(') {
1241                         filter_opstack_push(ps, OP_OPEN_PAREN);
1242                         continue;
1243                 }
1244
1245                 if (ch == ')') {
1246                         if (strlen(curr_operand(ps))) {
1247                                 postfix_append_operand(ps, curr_operand(ps));
1248                                 clear_operand_string(ps);
1249                         }
1250
1251                         top_op = filter_opstack_pop(ps);
1252                         while (top_op != OP_NONE) {
1253                                 if (top_op == OP_OPEN_PAREN)
1254                                         break;
1255                                 postfix_append_op(ps, top_op);
1256                                 top_op = filter_opstack_pop(ps);
1257                         }
1258                         if (top_op == OP_NONE) {
1259                                 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1260                                 return -EINVAL;
1261                         }
1262                         continue;
1263                 }
1264 parse_operand:
1265                 if (append_operand_char(ps, ch)) {
1266                         parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1267                         return -EINVAL;
1268                 }
1269         }
1270
1271         if (strlen(curr_operand(ps)))
1272                 postfix_append_operand(ps, curr_operand(ps));
1273
1274         while (!filter_opstack_empty(ps)) {
1275                 top_op = filter_opstack_pop(ps);
1276                 if (top_op == OP_NONE)
1277                         break;
1278                 if (top_op == OP_OPEN_PAREN) {
1279                         parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1280                         return -EINVAL;
1281                 }
1282                 postfix_append_op(ps, top_op);
1283         }
1284
1285         return 0;
1286 }
1287
1288 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
1289 {
1290         struct filter_pred *pred;
1291
1292         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1293         if (!pred)
1294                 return NULL;
1295
1296         pred->field_name = kstrdup(operand1, GFP_KERNEL);
1297         if (!pred->field_name) {
1298                 kfree(pred);
1299                 return NULL;
1300         }
1301
1302         strcpy(pred->regex.pattern, operand2);
1303         pred->regex.len = strlen(pred->regex.pattern);
1304
1305         pred->op = op;
1306
1307         return pred;
1308 }
1309
1310 static struct filter_pred *create_logical_pred(int op)
1311 {
1312         struct filter_pred *pred;
1313
1314         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1315         if (!pred)
1316                 return NULL;
1317
1318         pred->op = op;
1319
1320         return pred;
1321 }
1322
1323 static int check_preds(struct filter_parse_state *ps)
1324 {
1325         int n_normal_preds = 0, n_logical_preds = 0;
1326         struct postfix_elt *elt;
1327
1328         list_for_each_entry(elt, &ps->postfix, list) {
1329                 if (elt->op == OP_NONE)
1330                         continue;
1331
1332                 if (elt->op == OP_AND || elt->op == OP_OR) {
1333                         n_logical_preds++;
1334                         continue;
1335                 }
1336                 n_normal_preds++;
1337         }
1338
1339         if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1340                 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1341                 return -EINVAL;
1342         }
1343
1344         return 0;
1345 }
1346
1347 static int count_preds(struct filter_parse_state *ps)
1348 {
1349         struct postfix_elt *elt;
1350         int n_preds = 0;
1351
1352         list_for_each_entry(elt, &ps->postfix, list) {
1353                 if (elt->op == OP_NONE)
1354                         continue;
1355                 n_preds++;
1356         }
1357
1358         return n_preds;
1359 }
1360
1361 static int replace_preds(struct ftrace_event_call *call,
1362                          struct event_filter *filter,
1363                          struct filter_parse_state *ps,
1364                          char *filter_string,
1365                          bool dry_run)
1366 {
1367         char *operand1 = NULL, *operand2 = NULL;
1368         struct filter_pred *pred;
1369         struct postfix_elt *elt;
1370         struct pred_stack stack = { }; /* init to NULL */
1371         int err;
1372         int n_preds = 0;
1373
1374         n_preds = count_preds(ps);
1375         if (n_preds >= MAX_FILTER_PRED) {
1376                 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1377                 return -ENOSPC;
1378         }
1379
1380         err = check_preds(ps);
1381         if (err)
1382                 return err;
1383
1384         if (!dry_run) {
1385                 err = __alloc_pred_stack(&stack, n_preds);
1386                 if (err)
1387                         return err;
1388                 err = __alloc_preds(filter, n_preds);
1389                 if (err)
1390                         goto fail;
1391         }
1392
1393         n_preds = 0;
1394         list_for_each_entry(elt, &ps->postfix, list) {
1395                 if (elt->op == OP_NONE) {
1396                         if (!operand1)
1397                                 operand1 = elt->operand;
1398                         else if (!operand2)
1399                                 operand2 = elt->operand;
1400                         else {
1401                                 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1402                                 err = -EINVAL;
1403                                 goto fail;
1404                         }
1405                         continue;
1406                 }
1407
1408                 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1409                         parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1410                         err = -ENOSPC;
1411                         goto fail;
1412                 }
1413
1414                 if (elt->op == OP_AND || elt->op == OP_OR) {
1415                         pred = create_logical_pred(elt->op);
1416                         goto add_pred;
1417                 }
1418
1419                 if (!operand1 || !operand2) {
1420                         parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1421                         err = -EINVAL;
1422                         goto fail;
1423                 }
1424
1425                 pred = create_pred(elt->op, operand1, operand2);
1426 add_pred:
1427                 if (!pred) {
1428                         err = -ENOMEM;
1429                         goto fail;
1430                 }
1431                 err = filter_add_pred(ps, call, filter, pred, &stack, dry_run);
1432                 filter_free_pred(pred);
1433                 if (err)
1434                         goto fail;
1435
1436                 operand1 = operand2 = NULL;
1437         }
1438
1439         if (!dry_run) {
1440                 /* We should have one item left on the stack */
1441                 pred = __pop_pred_stack(&stack);
1442                 if (!pred)
1443                         return -EINVAL;
1444                 /* This item is where we start from in matching */
1445                 filter->root = pred;
1446                 /* Make sure the stack is empty */
1447                 pred = __pop_pred_stack(&stack);
1448                 if (WARN_ON(pred)) {
1449                         err = -EINVAL;
1450                         filter->root = NULL;
1451                         goto fail;
1452                 }
1453         }
1454
1455         err = 0;
1456 fail:
1457         __free_pred_stack(&stack);
1458         return err;
1459 }
1460
1461 static int replace_system_preds(struct event_subsystem *system,
1462                                 struct filter_parse_state *ps,
1463                                 char *filter_string)
1464 {
1465         struct ftrace_event_call *call;
1466         bool fail = true;
1467         int err;
1468
1469         list_for_each_entry(call, &ftrace_events, list) {
1470                 struct event_filter *filter = call->filter;
1471
1472                 if (strcmp(call->class->system, system->name) != 0)
1473                         continue;
1474
1475                 /* try to see if the filter can be applied */
1476                 err = replace_preds(call, filter, ps, filter_string, true);
1477                 if (err)
1478                         goto fail;
1479         }
1480
1481         /* set all filter pred counts to zero */
1482         list_for_each_entry(call, &ftrace_events, list) {
1483                 struct event_filter *filter = call->filter;
1484
1485                 if (strcmp(call->class->system, system->name) != 0)
1486                         continue;
1487
1488                 reset_preds(filter);
1489         }
1490
1491         /*
1492          * Since some of the preds may be used under preemption
1493          * we need to wait for them to finish before we may
1494          * reallocate them.
1495          */
1496         synchronize_sched();
1497
1498         list_for_each_entry(call, &ftrace_events, list) {
1499                 struct event_filter *filter = call->filter;
1500
1501                 if (strcmp(call->class->system, system->name) != 0)
1502                         continue;
1503
1504                 /* really apply the filter */
1505                 filter_disable_preds(call);
1506                 err = replace_preds(call, filter, ps, filter_string, false);
1507                 if (err)
1508                         filter_disable_preds(call);
1509                 else {
1510                         call->flags |= TRACE_EVENT_FL_FILTERED;
1511                         replace_filter_string(filter, filter_string);
1512                 }
1513                 fail = false;
1514         }
1515
1516         if (fail)
1517                 goto fail;
1518
1519         return 0;
1520  fail:
1521         parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1522         return -EINVAL;
1523 }
1524
1525 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1526 {
1527         int err;
1528         struct filter_parse_state *ps;
1529
1530         mutex_lock(&event_mutex);
1531
1532         err = init_filter(call);
1533         if (err)
1534                 goto out_unlock;
1535
1536         if (!strcmp(strstrip(filter_string), "0")) {
1537                 filter_disable_preds(call);
1538                 reset_preds(call->filter);
1539                 /* Make sure the filter is not being used */
1540                 synchronize_sched();
1541                 __free_preds(call->filter);
1542                 remove_filter_string(call->filter);
1543                 goto out_unlock;
1544         }
1545
1546         err = -ENOMEM;
1547         ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1548         if (!ps)
1549                 goto out_unlock;
1550
1551         filter_disable_preds(call);
1552         replace_filter_string(call->filter, filter_string);
1553
1554         parse_init(ps, filter_ops, filter_string);
1555         err = filter_parse(ps);
1556         if (err) {
1557                 append_filter_err(ps, call->filter);
1558                 goto out;
1559         }
1560
1561         /*
1562          * Make sure all the pred counts are zero so that
1563          * no task is using it when we reallocate the preds array.
1564          */
1565         reset_preds(call->filter);
1566         synchronize_sched();
1567
1568         err = replace_preds(call, call->filter, ps, filter_string, false);
1569         if (err)
1570                 append_filter_err(ps, call->filter);
1571         else
1572                 call->flags |= TRACE_EVENT_FL_FILTERED;
1573 out:
1574         filter_opstack_clear(ps);
1575         postfix_clear(ps);
1576         kfree(ps);
1577 out_unlock:
1578         mutex_unlock(&event_mutex);
1579
1580         return err;
1581 }
1582
1583 int apply_subsystem_event_filter(struct event_subsystem *system,
1584                                  char *filter_string)
1585 {
1586         int err;
1587         struct filter_parse_state *ps;
1588
1589         mutex_lock(&event_mutex);
1590
1591         err = init_subsystem_preds(system);
1592         if (err)
1593                 goto out_unlock;
1594
1595         if (!strcmp(strstrip(filter_string), "0")) {
1596                 filter_free_subsystem_preds(system);
1597                 remove_filter_string(system->filter);
1598                 goto out_unlock;
1599         }
1600
1601         err = -ENOMEM;
1602         ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1603         if (!ps)
1604                 goto out_unlock;
1605
1606         replace_filter_string(system->filter, filter_string);
1607
1608         parse_init(ps, filter_ops, filter_string);
1609         err = filter_parse(ps);
1610         if (err) {
1611                 append_filter_err(ps, system->filter);
1612                 goto out;
1613         }
1614
1615         err = replace_system_preds(system, ps, filter_string);
1616         if (err)
1617                 append_filter_err(ps, system->filter);
1618
1619 out:
1620         filter_opstack_clear(ps);
1621         postfix_clear(ps);
1622         kfree(ps);
1623 out_unlock:
1624         mutex_unlock(&event_mutex);
1625
1626         return err;
1627 }
1628
1629 #ifdef CONFIG_PERF_EVENTS
1630
1631 void ftrace_profile_free_filter(struct perf_event *event)
1632 {
1633         struct event_filter *filter = event->filter;
1634
1635         event->filter = NULL;
1636         __free_filter(filter);
1637 }
1638
1639 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1640                               char *filter_str)
1641 {
1642         int err;
1643         struct event_filter *filter;
1644         struct filter_parse_state *ps;
1645         struct ftrace_event_call *call = NULL;
1646
1647         mutex_lock(&event_mutex);
1648
1649         list_for_each_entry(call, &ftrace_events, list) {
1650                 if (call->event.type == event_id)
1651                         break;
1652         }
1653
1654         err = -EINVAL;
1655         if (&call->list == &ftrace_events)
1656                 goto out_unlock;
1657
1658         err = -EEXIST;
1659         if (event->filter)
1660                 goto out_unlock;
1661
1662         filter = __alloc_filter();
1663         if (IS_ERR(filter)) {
1664                 err = PTR_ERR(filter);
1665                 goto out_unlock;
1666         }
1667
1668         err = -ENOMEM;
1669         ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1670         if (!ps)
1671                 goto free_filter;
1672
1673         parse_init(ps, filter_ops, filter_str);
1674         err = filter_parse(ps);
1675         if (err)
1676                 goto free_ps;
1677
1678         err = replace_preds(call, filter, ps, filter_str, false);
1679         if (!err)
1680                 event->filter = filter;
1681
1682 free_ps:
1683         filter_opstack_clear(ps);
1684         postfix_clear(ps);
1685         kfree(ps);
1686
1687 free_filter:
1688         if (err)
1689                 __free_filter(filter);
1690
1691 out_unlock:
1692         mutex_unlock(&event_mutex);
1693
1694         return err;
1695 }
1696
1697 #endif /* CONFIG_PERF_EVENTS */
1698