tracing/events: fix memory leak when unloading module
[linux-2.6.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/debugfs.h>
22 #include <linux/uaccess.h>
23 #include <linux/module.h>
24 #include <linux/ctype.h>
25 #include <linux/mutex.h>
26
27 #include "trace.h"
28 #include "trace_output.h"
29
30 static DEFINE_MUTEX(filter_mutex);
31
32 enum filter_op_ids
33 {
34         OP_OR,
35         OP_AND,
36         OP_NE,
37         OP_EQ,
38         OP_LT,
39         OP_LE,
40         OP_GT,
41         OP_GE,
42         OP_NONE,
43         OP_OPEN_PAREN,
44 };
45
46 struct filter_op {
47         int id;
48         char *string;
49         int precedence;
50 };
51
52 static struct filter_op filter_ops[] = {
53         { OP_OR, "||", 1 },
54         { OP_AND, "&&", 2 },
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 DEFINE_COMPARISON_PRED(s64);
127 DEFINE_COMPARISON_PRED(u64);
128 DEFINE_COMPARISON_PRED(s32);
129 DEFINE_COMPARISON_PRED(u32);
130 DEFINE_COMPARISON_PRED(s16);
131 DEFINE_COMPARISON_PRED(u16);
132 DEFINE_COMPARISON_PRED(s8);
133 DEFINE_COMPARISON_PRED(u8);
134
135 DEFINE_EQUALITY_PRED(64);
136 DEFINE_EQUALITY_PRED(32);
137 DEFINE_EQUALITY_PRED(16);
138 DEFINE_EQUALITY_PRED(8);
139
140 static int filter_pred_and(struct filter_pred *pred __attribute((unused)),
141                            void *event __attribute((unused)),
142                            int val1, int val2)
143 {
144         return val1 && val2;
145 }
146
147 static int filter_pred_or(struct filter_pred *pred __attribute((unused)),
148                           void *event __attribute((unused)),
149                           int val1, int val2)
150 {
151         return val1 || val2;
152 }
153
154 static int filter_pred_string(struct filter_pred *pred, void *event,
155                               int val1, int val2)
156 {
157         char *addr = (char *)(event + pred->offset);
158         int cmp, match;
159
160         cmp = strncmp(addr, pred->str_val, pred->str_len);
161
162         match = (!cmp) ^ pred->not;
163
164         return match;
165 }
166
167 static int filter_pred_none(struct filter_pred *pred, void *event,
168                             int val1, int val2)
169 {
170         return 0;
171 }
172
173 /* return 1 if event matches, 0 otherwise (discard) */
174 int filter_match_preds(struct ftrace_event_call *call, void *rec)
175 {
176         struct event_filter *filter = call->filter;
177         int match, top = 0, val1 = 0, val2 = 0;
178         int stack[MAX_FILTER_PRED];
179         struct filter_pred *pred;
180         int i;
181
182         for (i = 0; i < filter->n_preds; i++) {
183                 pred = filter->preds[i];
184                 if (!pred->pop_n) {
185                         match = pred->fn(pred, rec, val1, val2);
186                         stack[top++] = match;
187                         continue;
188                 }
189                 if (pred->pop_n > top) {
190                         WARN_ON_ONCE(1);
191                         return 0;
192                 }
193                 val1 = stack[--top];
194                 val2 = stack[--top];
195                 match = pred->fn(pred, rec, val1, val2);
196                 stack[top++] = match;
197         }
198
199         return stack[--top];
200 }
201 EXPORT_SYMBOL_GPL(filter_match_preds);
202
203 static void parse_error(struct filter_parse_state *ps, int err, int pos)
204 {
205         ps->lasterr = err;
206         ps->lasterr_pos = pos;
207 }
208
209 static void remove_filter_string(struct event_filter *filter)
210 {
211         kfree(filter->filter_string);
212         filter->filter_string = NULL;
213 }
214
215 static int replace_filter_string(struct event_filter *filter,
216                                  char *filter_string)
217 {
218         kfree(filter->filter_string);
219         filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
220         if (!filter->filter_string)
221                 return -ENOMEM;
222
223         return 0;
224 }
225
226 static int append_filter_string(struct event_filter *filter,
227                                 char *string)
228 {
229         int newlen;
230         char *new_filter_string;
231
232         BUG_ON(!filter->filter_string);
233         newlen = strlen(filter->filter_string) + strlen(string) + 1;
234         new_filter_string = kmalloc(newlen, GFP_KERNEL);
235         if (!new_filter_string)
236                 return -ENOMEM;
237
238         strcpy(new_filter_string, filter->filter_string);
239         strcat(new_filter_string, string);
240         kfree(filter->filter_string);
241         filter->filter_string = new_filter_string;
242
243         return 0;
244 }
245
246 static void append_filter_err(struct filter_parse_state *ps,
247                               struct event_filter *filter)
248 {
249         int pos = ps->lasterr_pos;
250         char *buf, *pbuf;
251
252         buf = (char *)__get_free_page(GFP_TEMPORARY);
253         if (!buf)
254                 return;
255
256         append_filter_string(filter, "\n");
257         memset(buf, ' ', PAGE_SIZE);
258         if (pos > PAGE_SIZE - 128)
259                 pos = 0;
260         buf[pos] = '^';
261         pbuf = &buf[pos] + 1;
262
263         sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
264         append_filter_string(filter, buf);
265         free_page((unsigned long) buf);
266 }
267
268 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
269 {
270         struct event_filter *filter = call->filter;
271
272         mutex_lock(&filter_mutex);
273         if (filter->filter_string)
274                 trace_seq_printf(s, "%s\n", filter->filter_string);
275         else
276                 trace_seq_printf(s, "none\n");
277         mutex_unlock(&filter_mutex);
278 }
279
280 void print_subsystem_event_filter(struct event_subsystem *system,
281                                   struct trace_seq *s)
282 {
283         struct event_filter *filter = system->filter;
284
285         mutex_lock(&filter_mutex);
286         if (filter->filter_string)
287                 trace_seq_printf(s, "%s\n", filter->filter_string);
288         else
289                 trace_seq_printf(s, "none\n");
290         mutex_unlock(&filter_mutex);
291 }
292
293 static struct ftrace_event_field *
294 find_event_field(struct ftrace_event_call *call, char *name)
295 {
296         struct ftrace_event_field *field;
297
298         list_for_each_entry(field, &call->fields, link) {
299                 if (!strcmp(field->name, name))
300                         return field;
301         }
302
303         return NULL;
304 }
305
306 static void filter_free_pred(struct filter_pred *pred)
307 {
308         if (!pred)
309                 return;
310
311         kfree(pred->field_name);
312         kfree(pred);
313 }
314
315 static void filter_clear_pred(struct filter_pred *pred)
316 {
317         kfree(pred->field_name);
318         pred->field_name = NULL;
319         pred->str_len = 0;
320 }
321
322 static int filter_set_pred(struct filter_pred *dest,
323                            struct filter_pred *src,
324                            filter_pred_fn_t fn)
325 {
326         *dest = *src;
327         if (src->field_name) {
328                 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
329                 if (!dest->field_name)
330                         return -ENOMEM;
331         }
332         dest->fn = fn;
333
334         return 0;
335 }
336
337 static void filter_disable_preds(struct ftrace_event_call *call)
338 {
339         struct event_filter *filter = call->filter;
340         int i;
341
342         call->filter_active = 0;
343         filter->n_preds = 0;
344
345         for (i = 0; i < MAX_FILTER_PRED; i++)
346                 filter->preds[i]->fn = filter_pred_none;
347 }
348
349 void destroy_preds(struct ftrace_event_call *call)
350 {
351         struct event_filter *filter = call->filter;
352         int i;
353
354         for (i = 0; i < MAX_FILTER_PRED; i++) {
355                 if (filter->preds[i])
356                         filter_free_pred(filter->preds[i]);
357         }
358         kfree(filter->preds);
359         kfree(filter);
360         call->filter = NULL;
361 }
362
363 int init_preds(struct ftrace_event_call *call)
364 {
365         struct event_filter *filter;
366         struct filter_pred *pred;
367         int i;
368
369         filter = call->filter = kzalloc(sizeof(*filter), GFP_KERNEL);
370         if (!call->filter)
371                 return -ENOMEM;
372
373         call->filter_active = 0;
374         filter->n_preds = 0;
375
376         filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL);
377         if (!filter->preds)
378                 goto oom;
379
380         for (i = 0; i < MAX_FILTER_PRED; i++) {
381                 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
382                 if (!pred)
383                         goto oom;
384                 pred->fn = filter_pred_none;
385                 filter->preds[i] = pred;
386         }
387
388         return 0;
389
390 oom:
391         destroy_preds(call);
392
393         return -ENOMEM;
394 }
395 EXPORT_SYMBOL_GPL(init_preds);
396
397 static void filter_free_subsystem_preds(struct event_subsystem *system)
398 {
399         struct event_filter *filter = system->filter;
400         struct ftrace_event_call *call;
401         int i;
402
403         if (filter->n_preds) {
404                 for (i = 0; i < filter->n_preds; i++)
405                         filter_free_pred(filter->preds[i]);
406                 kfree(filter->preds);
407                 filter->preds = NULL;
408                 filter->n_preds = 0;
409         }
410
411         list_for_each_entry(call, &ftrace_events, list) {
412                 if (!call->define_fields)
413                         continue;
414
415                 if (!strcmp(call->system, system->name)) {
416                         filter_disable_preds(call);
417                         remove_filter_string(call->filter);
418                 }
419         }
420 }
421
422 static int filter_add_pred_fn(struct filter_parse_state *ps,
423                               struct ftrace_event_call *call,
424                               struct filter_pred *pred,
425                               filter_pred_fn_t fn)
426 {
427         struct event_filter *filter = call->filter;
428         int idx, err;
429
430         if (filter->n_preds == MAX_FILTER_PRED) {
431                 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
432                 return -ENOSPC;
433         }
434
435         idx = filter->n_preds;
436         filter_clear_pred(filter->preds[idx]);
437         err = filter_set_pred(filter->preds[idx], pred, fn);
438         if (err)
439                 return err;
440
441         filter->n_preds++;
442         call->filter_active = 1;
443
444         return 0;
445 }
446
447 static int is_string_field(const char *type)
448 {
449         if (strchr(type, '[') && strstr(type, "char"))
450                 return 1;
451
452         return 0;
453 }
454
455 static int is_legal_op(struct ftrace_event_field *field, int op)
456 {
457         if (is_string_field(field->type) && (op != OP_EQ && op != OP_NE))
458                 return 0;
459
460         return 1;
461 }
462
463 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
464                                              int field_is_signed)
465 {
466         filter_pred_fn_t fn = NULL;
467
468         switch (field_size) {
469         case 8:
470                 if (op == OP_EQ || op == OP_NE)
471                         fn = filter_pred_64;
472                 else if (field_is_signed)
473                         fn = filter_pred_s64;
474                 else
475                         fn = filter_pred_u64;
476                 break;
477         case 4:
478                 if (op == OP_EQ || op == OP_NE)
479                         fn = filter_pred_32;
480                 else if (field_is_signed)
481                         fn = filter_pred_s32;
482                 else
483                         fn = filter_pred_u32;
484                 break;
485         case 2:
486                 if (op == OP_EQ || op == OP_NE)
487                         fn = filter_pred_16;
488                 else if (field_is_signed)
489                         fn = filter_pred_s16;
490                 else
491                         fn = filter_pred_u16;
492                 break;
493         case 1:
494                 if (op == OP_EQ || op == OP_NE)
495                         fn = filter_pred_8;
496                 else if (field_is_signed)
497                         fn = filter_pred_s8;
498                 else
499                         fn = filter_pred_u8;
500                 break;
501         }
502
503         return fn;
504 }
505
506 static int filter_add_pred(struct filter_parse_state *ps,
507                            struct ftrace_event_call *call,
508                            struct filter_pred *pred)
509 {
510         struct ftrace_event_field *field;
511         filter_pred_fn_t fn;
512         unsigned long long val;
513
514         pred->fn = filter_pred_none;
515
516         if (pred->op == OP_AND) {
517                 pred->pop_n = 2;
518                 return filter_add_pred_fn(ps, call, pred, filter_pred_and);
519         } else if (pred->op == OP_OR) {
520                 pred->pop_n = 2;
521                 return filter_add_pred_fn(ps, call, pred, filter_pred_or);
522         }
523
524         field = find_event_field(call, pred->field_name);
525         if (!field) {
526                 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
527                 return -EINVAL;
528         }
529
530         pred->offset = field->offset;
531
532         if (!is_legal_op(field, pred->op)) {
533                 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
534                 return -EINVAL;
535         }
536
537         if (is_string_field(field->type)) {
538                 fn = filter_pred_string;
539                 pred->str_len = field->size;
540                 if (pred->op == OP_NE)
541                         pred->not = 1;
542                 return filter_add_pred_fn(ps, call, pred, fn);
543         } else {
544                 if (strict_strtoull(pred->str_val, 0, &val)) {
545                         parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
546                         return -EINVAL;
547                 }
548                 pred->val = val;
549         }
550
551         fn = select_comparison_fn(pred->op, field->size, field->is_signed);
552         if (!fn) {
553                 parse_error(ps, FILT_ERR_INVALID_OP, 0);
554                 return -EINVAL;
555         }
556
557         if (pred->op == OP_NE)
558                 pred->not = 1;
559
560         return filter_add_pred_fn(ps, call, pred, fn);
561 }
562
563 static int filter_add_subsystem_pred(struct filter_parse_state *ps,
564                                      struct event_subsystem *system,
565                                      struct filter_pred *pred,
566                                      char *filter_string)
567 {
568         struct event_filter *filter = system->filter;
569         struct ftrace_event_call *call;
570
571         if (!filter->preds) {
572                 filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred),
573                                         GFP_KERNEL);
574
575                 if (!filter->preds)
576                         return -ENOMEM;
577         }
578
579         if (filter->n_preds == MAX_FILTER_PRED) {
580                 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
581                 return -ENOSPC;
582         }
583
584         filter->preds[filter->n_preds] = pred;
585         filter->n_preds++;
586
587         list_for_each_entry(call, &ftrace_events, list) {
588                 int err;
589
590                 if (!call->define_fields)
591                         continue;
592
593                 if (strcmp(call->system, system->name))
594                         continue;
595
596                 err = filter_add_pred(ps, call, pred);
597                 if (err) {
598                         filter_free_subsystem_preds(system);
599                         parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
600                         return err;
601                 }
602                 replace_filter_string(call->filter, filter_string);
603         }
604
605         return 0;
606 }
607
608 static void parse_init(struct filter_parse_state *ps,
609                        struct filter_op *ops,
610                        char *infix_string)
611 {
612         memset(ps, '\0', sizeof(*ps));
613
614         ps->infix.string = infix_string;
615         ps->infix.cnt = strlen(infix_string);
616         ps->ops = ops;
617
618         INIT_LIST_HEAD(&ps->opstack);
619         INIT_LIST_HEAD(&ps->postfix);
620 }
621
622 static char infix_next(struct filter_parse_state *ps)
623 {
624         ps->infix.cnt--;
625
626         return ps->infix.string[ps->infix.tail++];
627 }
628
629 static char infix_peek(struct filter_parse_state *ps)
630 {
631         if (ps->infix.tail == strlen(ps->infix.string))
632                 return 0;
633
634         return ps->infix.string[ps->infix.tail];
635 }
636
637 static void infix_advance(struct filter_parse_state *ps)
638 {
639         ps->infix.cnt--;
640         ps->infix.tail++;
641 }
642
643 static inline int is_precedence_lower(struct filter_parse_state *ps,
644                                       int a, int b)
645 {
646         return ps->ops[a].precedence < ps->ops[b].precedence;
647 }
648
649 static inline int is_op_char(struct filter_parse_state *ps, char c)
650 {
651         int i;
652
653         for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
654                 if (ps->ops[i].string[0] == c)
655                         return 1;
656         }
657
658         return 0;
659 }
660
661 static int infix_get_op(struct filter_parse_state *ps, char firstc)
662 {
663         char nextc = infix_peek(ps);
664         char opstr[3];
665         int i;
666
667         opstr[0] = firstc;
668         opstr[1] = nextc;
669         opstr[2] = '\0';
670
671         for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
672                 if (!strcmp(opstr, ps->ops[i].string)) {
673                         infix_advance(ps);
674                         return ps->ops[i].id;
675                 }
676         }
677
678         opstr[1] = '\0';
679
680         for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
681                 if (!strcmp(opstr, ps->ops[i].string))
682                         return ps->ops[i].id;
683         }
684
685         return OP_NONE;
686 }
687
688 static inline void clear_operand_string(struct filter_parse_state *ps)
689 {
690         memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
691         ps->operand.tail = 0;
692 }
693
694 static inline int append_operand_char(struct filter_parse_state *ps, char c)
695 {
696         if (ps->operand.tail == MAX_FILTER_STR_VAL)
697                 return -EINVAL;
698
699         ps->operand.string[ps->operand.tail++] = c;
700
701         return 0;
702 }
703
704 static int filter_opstack_push(struct filter_parse_state *ps, int op)
705 {
706         struct opstack_op *opstack_op;
707
708         opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
709         if (!opstack_op)
710                 return -ENOMEM;
711
712         opstack_op->op = op;
713         list_add(&opstack_op->list, &ps->opstack);
714
715         return 0;
716 }
717
718 static int filter_opstack_empty(struct filter_parse_state *ps)
719 {
720         return list_empty(&ps->opstack);
721 }
722
723 static int filter_opstack_top(struct filter_parse_state *ps)
724 {
725         struct opstack_op *opstack_op;
726
727         if (filter_opstack_empty(ps))
728                 return OP_NONE;
729
730         opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
731
732         return opstack_op->op;
733 }
734
735 static int filter_opstack_pop(struct filter_parse_state *ps)
736 {
737         struct opstack_op *opstack_op;
738         int op;
739
740         if (filter_opstack_empty(ps))
741                 return OP_NONE;
742
743         opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
744         op = opstack_op->op;
745         list_del(&opstack_op->list);
746
747         kfree(opstack_op);
748
749         return op;
750 }
751
752 static void filter_opstack_clear(struct filter_parse_state *ps)
753 {
754         while (!filter_opstack_empty(ps))
755                 filter_opstack_pop(ps);
756 }
757
758 static char *curr_operand(struct filter_parse_state *ps)
759 {
760         return ps->operand.string;
761 }
762
763 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
764 {
765         struct postfix_elt *elt;
766
767         elt = kmalloc(sizeof(*elt), GFP_KERNEL);
768         if (!elt)
769                 return -ENOMEM;
770
771         elt->op = OP_NONE;
772         elt->operand = kstrdup(operand, GFP_KERNEL);
773         if (!elt->operand) {
774                 kfree(elt);
775                 return -ENOMEM;
776         }
777
778         list_add_tail(&elt->list, &ps->postfix);
779
780         return 0;
781 }
782
783 static int postfix_append_op(struct filter_parse_state *ps, int op)
784 {
785         struct postfix_elt *elt;
786
787         elt = kmalloc(sizeof(*elt), GFP_KERNEL);
788         if (!elt)
789                 return -ENOMEM;
790
791         elt->op = op;
792         elt->operand = NULL;
793
794         list_add_tail(&elt->list, &ps->postfix);
795
796         return 0;
797 }
798
799 static void postfix_clear(struct filter_parse_state *ps)
800 {
801         struct postfix_elt *elt;
802
803         while (!list_empty(&ps->postfix)) {
804                 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
805                 kfree(elt->operand);
806                 list_del(&elt->list);
807         }
808 }
809
810 static int filter_parse(struct filter_parse_state *ps)
811 {
812         int op, top_op;
813         char ch;
814
815         while ((ch = infix_next(ps))) {
816                 if (isspace(ch))
817                         continue;
818
819                 if (is_op_char(ps, ch)) {
820                         op = infix_get_op(ps, ch);
821                         if (op == OP_NONE) {
822                                 parse_error(ps, FILT_ERR_INVALID_OP, 0);
823                                 return -EINVAL;
824                         }
825
826                         if (strlen(curr_operand(ps))) {
827                                 postfix_append_operand(ps, curr_operand(ps));
828                                 clear_operand_string(ps);
829                         }
830
831                         while (!filter_opstack_empty(ps)) {
832                                 top_op = filter_opstack_top(ps);
833                                 if (!is_precedence_lower(ps, top_op, op)) {
834                                         top_op = filter_opstack_pop(ps);
835                                         postfix_append_op(ps, top_op);
836                                         continue;
837                                 }
838                                 break;
839                         }
840
841                         filter_opstack_push(ps, op);
842                         continue;
843                 }
844
845                 if (ch == '(') {
846                         filter_opstack_push(ps, OP_OPEN_PAREN);
847                         continue;
848                 }
849
850                 if (ch == ')') {
851                         if (strlen(curr_operand(ps))) {
852                                 postfix_append_operand(ps, curr_operand(ps));
853                                 clear_operand_string(ps);
854                         }
855
856                         top_op = filter_opstack_pop(ps);
857                         while (top_op != OP_NONE) {
858                                 if (top_op == OP_OPEN_PAREN)
859                                         break;
860                                 postfix_append_op(ps, top_op);
861                                 top_op = filter_opstack_pop(ps);
862                         }
863                         if (top_op == OP_NONE) {
864                                 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
865                                 return -EINVAL;
866                         }
867                         continue;
868                 }
869                 if (append_operand_char(ps, ch)) {
870                         parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
871                         return -EINVAL;
872                 }
873         }
874
875         if (strlen(curr_operand(ps)))
876                 postfix_append_operand(ps, curr_operand(ps));
877
878         while (!filter_opstack_empty(ps)) {
879                 top_op = filter_opstack_pop(ps);
880                 if (top_op == OP_NONE)
881                         break;
882                 if (top_op == OP_OPEN_PAREN) {
883                         parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
884                         return -EINVAL;
885                 }
886                 postfix_append_op(ps, top_op);
887         }
888
889         return 0;
890 }
891
892 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
893 {
894         struct filter_pred *pred;
895
896         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
897         if (!pred)
898                 return NULL;
899
900         pred->field_name = kstrdup(operand1, GFP_KERNEL);
901         if (!pred->field_name) {
902                 kfree(pred);
903                 return NULL;
904         }
905
906         strcpy(pred->str_val, operand2);
907         pred->str_len = strlen(operand2);
908
909         pred->op = op;
910
911         return pred;
912 }
913
914 static struct filter_pred *create_logical_pred(int op)
915 {
916         struct filter_pred *pred;
917
918         pred = kzalloc(sizeof(*pred), GFP_KERNEL);
919         if (!pred)
920                 return NULL;
921
922         pred->op = op;
923
924         return pred;
925 }
926
927 static int check_preds(struct filter_parse_state *ps)
928 {
929         int n_normal_preds = 0, n_logical_preds = 0;
930         struct postfix_elt *elt;
931
932         list_for_each_entry(elt, &ps->postfix, list) {
933                 if (elt->op == OP_NONE)
934                         continue;
935
936                 if (elt->op == OP_AND || elt->op == OP_OR) {
937                         n_logical_preds++;
938                         continue;
939                 }
940                 n_normal_preds++;
941         }
942
943         if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
944                 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
945                 return -EINVAL;
946         }
947
948         return 0;
949 }
950
951 static int replace_preds(struct event_subsystem *system,
952                          struct ftrace_event_call *call,
953                          struct filter_parse_state *ps,
954                          char *filter_string)
955 {
956         char *operand1 = NULL, *operand2 = NULL;
957         struct filter_pred *pred;
958         struct postfix_elt *elt;
959         int err;
960
961         err = check_preds(ps);
962         if (err)
963                 return err;
964
965         list_for_each_entry(elt, &ps->postfix, list) {
966                 if (elt->op == OP_NONE) {
967                         if (!operand1)
968                                 operand1 = elt->operand;
969                         else if (!operand2)
970                                 operand2 = elt->operand;
971                         else {
972                                 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
973                                 return -EINVAL;
974                         }
975                         continue;
976                 }
977
978                 if (elt->op == OP_AND || elt->op == OP_OR) {
979                         pred = create_logical_pred(elt->op);
980                         if (call) {
981                                 err = filter_add_pred(ps, call, pred);
982                                 filter_free_pred(pred);
983                         } else
984                                 err = filter_add_subsystem_pred(ps, system,
985                                                         pred, filter_string);
986                         if (err)
987                                 return err;
988
989                         operand1 = operand2 = NULL;
990                         continue;
991                 }
992
993                 if (!operand1 || !operand2) {
994                         parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
995                         return -EINVAL;
996                 }
997
998                 pred = create_pred(elt->op, operand1, operand2);
999                 if (call) {
1000                         err = filter_add_pred(ps, call, pred);
1001                         filter_free_pred(pred);
1002                 } else
1003                         err = filter_add_subsystem_pred(ps, system, pred,
1004                                                         filter_string);
1005                 if (err)
1006                         return err;
1007
1008                 operand1 = operand2 = NULL;
1009         }
1010
1011         return 0;
1012 }
1013
1014 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1015 {
1016         int err;
1017
1018         struct filter_parse_state *ps;
1019
1020         mutex_lock(&filter_mutex);
1021
1022         if (!strcmp(strstrip(filter_string), "0")) {
1023                 filter_disable_preds(call);
1024                 remove_filter_string(call->filter);
1025                 mutex_unlock(&filter_mutex);
1026                 return 0;
1027         }
1028
1029         ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1030         if (!ps)
1031                 return -ENOMEM;
1032
1033         filter_disable_preds(call);
1034         replace_filter_string(call->filter, filter_string);
1035
1036         parse_init(ps, filter_ops, filter_string);
1037         err = filter_parse(ps);
1038         if (err) {
1039                 append_filter_err(ps, call->filter);
1040                 goto out;
1041         }
1042
1043         err = replace_preds(NULL, call, ps, filter_string);
1044         if (err)
1045                 append_filter_err(ps, call->filter);
1046
1047 out:
1048         filter_opstack_clear(ps);
1049         postfix_clear(ps);
1050         kfree(ps);
1051
1052         mutex_unlock(&filter_mutex);
1053
1054         return err;
1055 }
1056
1057 int apply_subsystem_event_filter(struct event_subsystem *system,
1058                                  char *filter_string)
1059 {
1060         int err;
1061
1062         struct filter_parse_state *ps;
1063
1064         mutex_lock(&filter_mutex);
1065
1066         if (!strcmp(strstrip(filter_string), "0")) {
1067                 filter_free_subsystem_preds(system);
1068                 remove_filter_string(system->filter);
1069                 mutex_unlock(&filter_mutex);
1070                 return 0;
1071         }
1072
1073         ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1074         if (!ps)
1075                 return -ENOMEM;
1076
1077         filter_free_subsystem_preds(system);
1078         replace_filter_string(system->filter, filter_string);
1079
1080         parse_init(ps, filter_ops, filter_string);
1081         err = filter_parse(ps);
1082         if (err) {
1083                 append_filter_err(ps, system->filter);
1084                 goto out;
1085         }
1086
1087         err = replace_preds(system, NULL, ps, filter_string);
1088         if (err)
1089                 append_filter_err(ps, system->filter);
1090
1091 out:
1092         filter_opstack_clear(ps);
1093         postfix_clear(ps);
1094         kfree(ps);
1095
1096         mutex_unlock(&filter_mutex);
1097
1098         return err;
1099 }
1100