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