2 * trace_events_filter - generic event filtering
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.
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.
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.
18 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
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>
28 #include "trace_output.h"
51 static struct filter_op filter_ops[] = {
61 { OP_NONE, "OP_NONE", 0 },
62 { OP_OPEN_PAREN, "(", 0 },
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,
80 static char *err_text[] = {
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",
97 struct list_head list;
103 struct list_head list;
106 struct filter_parse_state {
107 struct filter_op *ops;
108 struct list_head opstack;
109 struct list_head postfix;
120 char string[MAX_FILTER_STR_VAL];
127 struct filter_pred **preds;
131 #define DEFINE_COMPARISON_PRED(type) \
132 static int filter_pred_##type(struct filter_pred *pred, void *event) \
134 type *addr = (type *)(event + pred->offset); \
135 type val = (type)pred->val; \
138 switch (pred->op) { \
140 match = (*addr < val); \
143 match = (*addr <= val); \
146 match = (*addr > val); \
149 match = (*addr >= val); \
158 #define DEFINE_EQUALITY_PRED(size) \
159 static int filter_pred_##size(struct filter_pred *pred, void *event) \
161 u##size *addr = (u##size *)(event + pred->offset); \
162 u##size val = (u##size)pred->val; \
165 match = (val == *addr) ^ pred->not; \
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);
179 DEFINE_EQUALITY_PRED(64);
180 DEFINE_EQUALITY_PRED(32);
181 DEFINE_EQUALITY_PRED(16);
182 DEFINE_EQUALITY_PRED(8);
184 /* Filter predicate for fixed sized arrays of characters */
185 static int filter_pred_string(struct filter_pred *pred, void *event)
187 char *addr = (char *)(event + pred->offset);
190 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
192 match = cmp ^ pred->not;
197 /* Filter predicate for char * pointers */
198 static int filter_pred_pchar(struct filter_pred *pred, void *event)
200 char **addr = (char **)(event + pred->offset);
202 int len = strlen(*addr) + 1; /* including tailing '\0' */
204 cmp = pred->regex.match(*addr, &pred->regex, len);
206 match = cmp ^ pred->not;
212 * Filter predicate for dynamic sized arrays of characters.
213 * These are implemented through a list of strings at the end
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.
221 static int filter_pred_strloc(struct filter_pred *pred, void *event)
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);
229 cmp = pred->regex.match(addr, &pred->regex, str_len);
231 match = cmp ^ pred->not;
236 static int filter_pred_none(struct filter_pred *pred, void *event)
242 * regex_match_foo - Basic regex callbacks
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')
249 * - @str might not be NULL-terminated if it's of type DYN_STRING
253 static int regex_match_full(char *str, struct regex *r, int len)
255 if (strncmp(str, r->pattern, len) == 0)
260 static int regex_match_front(char *str, struct regex *r, int len)
262 if (strncmp(str, r->pattern, r->len) == 0)
267 static int regex_match_middle(char *str, struct regex *r, int len)
269 if (strnstr(str, r->pattern, len))
274 static int regex_match_end(char *str, struct regex *r, int len)
276 int strlen = len - 1;
278 if (strlen >= r->len &&
279 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
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
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.
297 * search returns the pointer to use for comparison.
298 * not returns 1 if buff started with a '!'
301 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
303 int type = MATCH_FULL;
306 if (buff[0] == '!') {
315 for (i = 0; i < len; i++) {
316 if (buff[i] == '*') {
319 type = MATCH_END_ONLY;
321 if (type == MATCH_END_ONLY)
322 type = MATCH_MIDDLE_ONLY;
324 type = MATCH_FRONT_ONLY;
334 static void filter_build_regex(struct filter_pred *pred)
336 struct regex *r = &pred->regex;
338 enum regex_type type = MATCH_FULL;
341 if (pred->op == OP_GLOB) {
342 type = filter_parse_regex(r->pattern, r->len, &search, ¬);
343 r->len = strlen(search);
344 memmove(r->pattern, search, r->len+1);
349 r->match = regex_match_full;
351 case MATCH_FRONT_ONLY:
352 r->match = regex_match_front;
354 case MATCH_MIDDLE_ONLY:
355 r->match = regex_match_middle;
358 r->match = regex_match_end;
371 static struct filter_pred *
372 get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
373 int index, enum move_type *move)
375 if (pred->parent & FILTER_PRED_IS_RIGHT)
376 *move = MOVE_UP_FROM_RIGHT;
378 *move = MOVE_UP_FROM_LEFT;
379 pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
384 /* return 1 if event matches, 0 otherwise (discard) */
385 int filter_match_preds(struct event_filter *filter, void *rec)
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);
395 /* no filter is considered a match */
400 * n_preds, root and filter->preds are protect with preemption disabled.
402 preds = rcu_dereference_sched(filter->preds);
403 root = rcu_dereference_sched(filter->root);
409 /* match is currently meaningless */
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];
421 match = pred->fn(pred, rec);
422 /* If this pred is the only pred */
425 pred = get_pred_parent(pred, preds,
426 pred->parent, &move);
428 case MOVE_UP_FROM_LEFT:
430 * Check for short circuits.
432 * Optimization: !!match == (pred->op == OP_OR)
434 * if ((match && pred->op == OP_OR) ||
435 * (!match && pred->op == OP_AND))
437 if (!!match == (pred->op == OP_OR)) {
440 pred = get_pred_parent(pred, preds,
441 pred->parent, &move);
444 /* now go down the right side of the tree. */
445 pred = &preds[pred->right];
448 case MOVE_UP_FROM_RIGHT:
449 /* We finished this equation. */
452 pred = get_pred_parent(pred, preds,
453 pred->parent, &move);
461 EXPORT_SYMBOL_GPL(filter_match_preds);
463 static void parse_error(struct filter_parse_state *ps, int err, int pos)
466 ps->lasterr_pos = pos;
469 static void remove_filter_string(struct event_filter *filter)
471 kfree(filter->filter_string);
472 filter->filter_string = NULL;
475 static int replace_filter_string(struct event_filter *filter,
478 kfree(filter->filter_string);
479 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
480 if (!filter->filter_string)
486 static int append_filter_string(struct event_filter *filter,
490 char *new_filter_string;
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)
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;
506 static void append_filter_err(struct filter_parse_state *ps,
507 struct event_filter *filter)
509 int pos = ps->lasterr_pos;
512 buf = (char *)__get_free_page(GFP_TEMPORARY);
516 append_filter_string(filter, "\n");
517 memset(buf, ' ', PAGE_SIZE);
518 if (pos > PAGE_SIZE - 128)
521 pbuf = &buf[pos] + 1;
523 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
524 append_filter_string(filter, buf);
525 free_page((unsigned long) buf);
528 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
530 struct event_filter *filter = call->filter;
532 mutex_lock(&event_mutex);
533 if (filter && filter->filter_string)
534 trace_seq_printf(s, "%s\n", filter->filter_string);
536 trace_seq_printf(s, "none\n");
537 mutex_unlock(&event_mutex);
540 void print_subsystem_event_filter(struct event_subsystem *system,
543 struct event_filter *filter = system->filter;
545 mutex_lock(&event_mutex);
546 if (filter && filter->filter_string)
547 trace_seq_printf(s, "%s\n", filter->filter_string);
549 trace_seq_printf(s, "none\n");
550 mutex_unlock(&event_mutex);
553 static struct ftrace_event_field *
554 __find_event_field(struct list_head *head, char *name)
556 struct ftrace_event_field *field;
558 list_for_each_entry(field, head, link) {
559 if (!strcmp(field->name, name))
566 static struct ftrace_event_field *
567 find_event_field(struct ftrace_event_call *call, char *name)
569 struct ftrace_event_field *field;
570 struct list_head *head;
572 field = __find_event_field(&ftrace_common_fields, name);
576 head = trace_get_fields(call);
577 return __find_event_field(head, name);
580 static void filter_free_pred(struct filter_pred *pred)
585 kfree(pred->field_name);
589 static void filter_clear_pred(struct filter_pred *pred)
591 kfree(pred->field_name);
592 pred->field_name = NULL;
596 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
598 stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
601 stack->index = n_preds;
605 static void __free_pred_stack(struct pred_stack *stack)
611 static int __push_pred_stack(struct pred_stack *stack,
612 struct filter_pred *pred)
614 int index = stack->index;
616 if (WARN_ON(index == 0))
619 stack->preds[--index] = pred;
620 stack->index = index;
624 static struct filter_pred *
625 __pop_pred_stack(struct pred_stack *stack)
627 struct filter_pred *pred;
628 int index = stack->index;
630 pred = stack->preds[index++];
634 stack->index = index;
638 static int filter_set_pred(struct event_filter *filter,
640 struct pred_stack *stack,
641 struct filter_pred *src,
644 struct filter_pred *dest = &filter->preds[idx];
645 struct filter_pred *left;
646 struct filter_pred *right;
649 if (src->field_name) {
650 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
651 if (!dest->field_name)
657 if (dest->op == OP_OR || dest->op == OP_AND) {
658 right = __pop_pred_stack(stack);
659 left = __pop_pred_stack(stack);
662 dest->left = left->index;
663 dest->right = right->index;
664 left->parent = dest->index;
665 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
668 * Make dest->left invalid to be used as a quick
669 * way to know this is a leaf node.
671 dest->left = FILTER_PRED_INVALID;
673 return __push_pred_stack(stack, dest);
676 static void __free_preds(struct event_filter *filter)
681 for (i = 0; i < filter->a_preds; i++)
682 kfree(filter->preds[i].field_name);
683 kfree(filter->preds);
684 filter->preds = NULL;
690 static void reset_preds(struct event_filter *filter)
692 int n_preds = filter->n_preds;
700 for (i = 0; i < n_preds; i++)
701 filter->preds[i].fn = filter_pred_none;
704 static void filter_disable_preds(struct ftrace_event_call *call)
706 struct event_filter *filter = call->filter;
708 call->flags &= ~TRACE_EVENT_FL_FILTERED;
712 static void __free_filter(struct event_filter *filter)
717 __free_preds(filter);
718 kfree(filter->filter_string);
722 void destroy_preds(struct ftrace_event_call *call)
724 __free_filter(call->filter);
726 call->flags &= ~TRACE_EVENT_FL_FILTERED;
729 static struct event_filter *__alloc_filter(void)
731 struct event_filter *filter;
733 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
735 return ERR_PTR(-ENOMEM);
742 static int __alloc_preds(struct event_filter *filter, int n_preds)
744 struct filter_pred *pred;
748 if (filter->a_preds < n_preds) {
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.
755 if (WARN_ON_ONCE(filter->n_preds)) {
756 /* We need to reset it now */
760 __free_preds(filter);
764 if (!filter->preds) {
766 kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
767 filter->a_preds = n_preds;
772 if (WARN_ON(filter->a_preds < n_preds))
775 for (i = 0; i < n_preds; i++) {
776 pred = &filter->preds[i];
777 pred->fn = filter_pred_none;
783 static int init_filter(struct ftrace_event_call *call)
788 call->flags &= ~TRACE_EVENT_FL_FILTERED;
789 call->filter = __alloc_filter();
790 if (IS_ERR(call->filter))
791 return PTR_ERR(call->filter);
796 static int init_subsystem_preds(struct event_subsystem *system)
798 struct ftrace_event_call *call;
801 list_for_each_entry(call, &ftrace_events, list) {
802 if (strcmp(call->class->system, system->name) != 0)
805 err = init_filter(call);
813 static void filter_free_subsystem_preds(struct event_subsystem *system)
815 struct ftrace_event_call *call;
817 list_for_each_entry(call, &ftrace_events, list) {
818 if (strcmp(call->class->system, system->name) != 0)
821 filter_disable_preds(call);
822 remove_filter_string(call->filter);
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,
835 if (WARN_ON(filter->n_preds == filter->a_preds)) {
836 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
840 idx = filter->n_preds;
841 filter_clear_pred(&filter->preds[idx]);
842 err = filter_set_pred(filter, idx, stack, pred, fn);
851 int filter_assign_type(const char *type)
853 if (strstr(type, "__data_loc") && strstr(type, "char"))
854 return FILTER_DYN_STRING;
856 if (strchr(type, '[') && strstr(type, "char"))
857 return FILTER_STATIC_STRING;
862 static bool is_string_field(struct ftrace_event_field *field)
864 return field->filter_type == FILTER_DYN_STRING ||
865 field->filter_type == FILTER_STATIC_STRING ||
866 field->filter_type == FILTER_PTR_STRING;
869 static int is_legal_op(struct ftrace_event_field *field, int op)
871 if (is_string_field(field) &&
872 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
874 if (!is_string_field(field) && op == OP_GLOB)
880 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
883 filter_pred_fn_t fn = NULL;
885 switch (field_size) {
887 if (op == OP_EQ || op == OP_NE)
889 else if (field_is_signed)
890 fn = filter_pred_s64;
892 fn = filter_pred_u64;
895 if (op == OP_EQ || op == OP_NE)
897 else if (field_is_signed)
898 fn = filter_pred_s32;
900 fn = filter_pred_u32;
903 if (op == OP_EQ || op == OP_NE)
905 else if (field_is_signed)
906 fn = filter_pred_s16;
908 fn = filter_pred_u16;
911 if (op == OP_EQ || op == OP_NE)
913 else if (field_is_signed)
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,
930 struct ftrace_event_field *field;
932 unsigned long long val;
935 fn = pred->fn = filter_pred_none;
937 if (pred->op == OP_AND)
939 else if (pred->op == OP_OR)
942 field = find_event_field(call, pred->field_name);
944 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
948 pred->offset = field->offset;
950 if (!is_legal_op(field, pred->op)) {
951 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
955 if (is_string_field(field)) {
956 filter_build_regex(pred);
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;
964 fn = filter_pred_pchar;
966 if (field->is_signed)
967 ret = strict_strtoll(pred->regex.pattern, 0, &val);
969 ret = strict_strtoull(pred->regex.pattern, 0, &val);
971 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
976 fn = select_comparison_fn(pred->op, field->size,
979 parse_error(ps, FILT_ERR_INVALID_OP, 0);
984 if (pred->op == OP_NE)
989 return filter_add_pred_fn(ps, call, filter, pred, stack, fn);
993 static void parse_init(struct filter_parse_state *ps,
994 struct filter_op *ops,
997 memset(ps, '\0', sizeof(*ps));
999 ps->infix.string = infix_string;
1000 ps->infix.cnt = strlen(infix_string);
1003 INIT_LIST_HEAD(&ps->opstack);
1004 INIT_LIST_HEAD(&ps->postfix);
1007 static char infix_next(struct filter_parse_state *ps)
1011 return ps->infix.string[ps->infix.tail++];
1014 static char infix_peek(struct filter_parse_state *ps)
1016 if (ps->infix.tail == strlen(ps->infix.string))
1019 return ps->infix.string[ps->infix.tail];
1022 static void infix_advance(struct filter_parse_state *ps)
1028 static inline int is_precedence_lower(struct filter_parse_state *ps,
1031 return ps->ops[a].precedence < ps->ops[b].precedence;
1034 static inline int is_op_char(struct filter_parse_state *ps, char c)
1038 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1039 if (ps->ops[i].string[0] == c)
1046 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1048 char nextc = infix_peek(ps);
1056 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1057 if (!strcmp(opstr, ps->ops[i].string)) {
1059 return ps->ops[i].id;
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;
1073 static inline void clear_operand_string(struct filter_parse_state *ps)
1075 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1076 ps->operand.tail = 0;
1079 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1081 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1084 ps->operand.string[ps->operand.tail++] = c;
1089 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1091 struct opstack_op *opstack_op;
1093 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1097 opstack_op->op = op;
1098 list_add(&opstack_op->list, &ps->opstack);
1103 static int filter_opstack_empty(struct filter_parse_state *ps)
1105 return list_empty(&ps->opstack);
1108 static int filter_opstack_top(struct filter_parse_state *ps)
1110 struct opstack_op *opstack_op;
1112 if (filter_opstack_empty(ps))
1115 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1117 return opstack_op->op;
1120 static int filter_opstack_pop(struct filter_parse_state *ps)
1122 struct opstack_op *opstack_op;
1125 if (filter_opstack_empty(ps))
1128 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1129 op = opstack_op->op;
1130 list_del(&opstack_op->list);
1137 static void filter_opstack_clear(struct filter_parse_state *ps)
1139 while (!filter_opstack_empty(ps))
1140 filter_opstack_pop(ps);
1143 static char *curr_operand(struct filter_parse_state *ps)
1145 return ps->operand.string;
1148 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1150 struct postfix_elt *elt;
1152 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1157 elt->operand = kstrdup(operand, GFP_KERNEL);
1158 if (!elt->operand) {
1163 list_add_tail(&elt->list, &ps->postfix);
1168 static int postfix_append_op(struct filter_parse_state *ps, int op)
1170 struct postfix_elt *elt;
1172 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1177 elt->operand = NULL;
1179 list_add_tail(&elt->list, &ps->postfix);
1184 static void postfix_clear(struct filter_parse_state *ps)
1186 struct postfix_elt *elt;
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);
1196 static int filter_parse(struct filter_parse_state *ps)
1202 while ((ch = infix_next(ps))) {
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);
1221 if (strlen(curr_operand(ps))) {
1222 postfix_append_operand(ps, curr_operand(ps));
1223 clear_operand_string(ps);
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);
1236 filter_opstack_push(ps, op);
1241 filter_opstack_push(ps, OP_OPEN_PAREN);
1246 if (strlen(curr_operand(ps))) {
1247 postfix_append_operand(ps, curr_operand(ps));
1248 clear_operand_string(ps);
1251 top_op = filter_opstack_pop(ps);
1252 while (top_op != OP_NONE) {
1253 if (top_op == OP_OPEN_PAREN)
1255 postfix_append_op(ps, top_op);
1256 top_op = filter_opstack_pop(ps);
1258 if (top_op == OP_NONE) {
1259 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1265 if (append_operand_char(ps, ch)) {
1266 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1271 if (strlen(curr_operand(ps)))
1272 postfix_append_operand(ps, curr_operand(ps));
1274 while (!filter_opstack_empty(ps)) {
1275 top_op = filter_opstack_pop(ps);
1276 if (top_op == OP_NONE)
1278 if (top_op == OP_OPEN_PAREN) {
1279 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1282 postfix_append_op(ps, top_op);
1288 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
1290 struct filter_pred *pred;
1292 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1296 pred->field_name = kstrdup(operand1, GFP_KERNEL);
1297 if (!pred->field_name) {
1302 strcpy(pred->regex.pattern, operand2);
1303 pred->regex.len = strlen(pred->regex.pattern);
1310 static struct filter_pred *create_logical_pred(int op)
1312 struct filter_pred *pred;
1314 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1323 static int check_preds(struct filter_parse_state *ps)
1325 int n_normal_preds = 0, n_logical_preds = 0;
1326 struct postfix_elt *elt;
1328 list_for_each_entry(elt, &ps->postfix, list) {
1329 if (elt->op == OP_NONE)
1332 if (elt->op == OP_AND || elt->op == OP_OR) {
1339 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1340 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1347 static int count_preds(struct filter_parse_state *ps)
1349 struct postfix_elt *elt;
1352 list_for_each_entry(elt, &ps->postfix, list) {
1353 if (elt->op == OP_NONE)
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,
1367 char *operand1 = NULL, *operand2 = NULL;
1368 struct filter_pred *pred;
1369 struct postfix_elt *elt;
1370 struct pred_stack stack = { }; /* init to NULL */
1374 n_preds = count_preds(ps);
1375 if (n_preds >= MAX_FILTER_PRED) {
1376 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1380 err = check_preds(ps);
1385 err = __alloc_pred_stack(&stack, n_preds);
1388 err = __alloc_preds(filter, n_preds);
1394 list_for_each_entry(elt, &ps->postfix, list) {
1395 if (elt->op == OP_NONE) {
1397 operand1 = elt->operand;
1399 operand2 = elt->operand;
1401 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1408 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1409 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1414 if (elt->op == OP_AND || elt->op == OP_OR) {
1415 pred = create_logical_pred(elt->op);
1419 if (!operand1 || !operand2) {
1420 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1425 pred = create_pred(elt->op, operand1, operand2);
1431 err = filter_add_pred(ps, call, filter, pred, &stack, dry_run);
1432 filter_free_pred(pred);
1436 operand1 = operand2 = NULL;
1440 /* We should have one item left on the stack */
1441 pred = __pop_pred_stack(&stack);
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)) {
1450 filter->root = NULL;
1457 __free_pred_stack(&stack);
1461 static int replace_system_preds(struct event_subsystem *system,
1462 struct filter_parse_state *ps,
1463 char *filter_string)
1465 struct ftrace_event_call *call;
1469 list_for_each_entry(call, &ftrace_events, list) {
1470 struct event_filter *filter = call->filter;
1472 if (strcmp(call->class->system, system->name) != 0)
1475 /* try to see if the filter can be applied */
1476 err = replace_preds(call, filter, ps, filter_string, true);
1481 /* set all filter pred counts to zero */
1482 list_for_each_entry(call, &ftrace_events, list) {
1483 struct event_filter *filter = call->filter;
1485 if (strcmp(call->class->system, system->name) != 0)
1488 reset_preds(filter);
1492 * Since some of the preds may be used under preemption
1493 * we need to wait for them to finish before we may
1496 synchronize_sched();
1498 list_for_each_entry(call, &ftrace_events, list) {
1499 struct event_filter *filter = call->filter;
1501 if (strcmp(call->class->system, system->name) != 0)
1504 /* really apply the filter */
1505 filter_disable_preds(call);
1506 err = replace_preds(call, filter, ps, filter_string, false);
1508 filter_disable_preds(call);
1510 call->flags |= TRACE_EVENT_FL_FILTERED;
1511 replace_filter_string(filter, filter_string);
1521 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1525 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1528 struct filter_parse_state *ps;
1530 mutex_lock(&event_mutex);
1532 err = init_filter(call);
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);
1547 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1551 filter_disable_preds(call);
1552 replace_filter_string(call->filter, filter_string);
1554 parse_init(ps, filter_ops, filter_string);
1555 err = filter_parse(ps);
1557 append_filter_err(ps, call->filter);
1562 * Make sure all the pred counts are zero so that
1563 * no task is using it when we reallocate the preds array.
1565 reset_preds(call->filter);
1566 synchronize_sched();
1568 err = replace_preds(call, call->filter, ps, filter_string, false);
1570 append_filter_err(ps, call->filter);
1572 call->flags |= TRACE_EVENT_FL_FILTERED;
1574 filter_opstack_clear(ps);
1578 mutex_unlock(&event_mutex);
1583 int apply_subsystem_event_filter(struct event_subsystem *system,
1584 char *filter_string)
1587 struct filter_parse_state *ps;
1589 mutex_lock(&event_mutex);
1591 err = init_subsystem_preds(system);
1595 if (!strcmp(strstrip(filter_string), "0")) {
1596 filter_free_subsystem_preds(system);
1597 remove_filter_string(system->filter);
1602 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1606 replace_filter_string(system->filter, filter_string);
1608 parse_init(ps, filter_ops, filter_string);
1609 err = filter_parse(ps);
1611 append_filter_err(ps, system->filter);
1615 err = replace_system_preds(system, ps, filter_string);
1617 append_filter_err(ps, system->filter);
1620 filter_opstack_clear(ps);
1624 mutex_unlock(&event_mutex);
1629 #ifdef CONFIG_PERF_EVENTS
1631 void ftrace_profile_free_filter(struct perf_event *event)
1633 struct event_filter *filter = event->filter;
1635 event->filter = NULL;
1636 __free_filter(filter);
1639 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1643 struct event_filter *filter;
1644 struct filter_parse_state *ps;
1645 struct ftrace_event_call *call = NULL;
1647 mutex_lock(&event_mutex);
1649 list_for_each_entry(call, &ftrace_events, list) {
1650 if (call->event.type == event_id)
1655 if (&call->list == &ftrace_events)
1662 filter = __alloc_filter();
1663 if (IS_ERR(filter)) {
1664 err = PTR_ERR(filter);
1669 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1673 parse_init(ps, filter_ops, filter_str);
1674 err = filter_parse(ps);
1678 err = replace_preds(call, filter, ps, filter_str, false);
1680 event->filter = filter;
1683 filter_opstack_clear(ps);
1689 __free_filter(filter);
1692 mutex_unlock(&event_mutex);
1697 #endif /* CONFIG_PERF_EVENTS */