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];
385 * A series of AND or ORs where found together. Instead of
386 * climbing up and down the tree branches, an array of the
387 * ops were made in order of checks. We can just move across
388 * the array and short circuit if needed.
390 static int process_ops(struct filter_pred *preds,
391 struct filter_pred *op, void *rec)
393 struct filter_pred *pred;
399 * Micro-optimization: We set type to true if op
400 * is an OR and false otherwise (AND). Then we
401 * just need to test if the match is equal to
402 * the type, and if it is, we can short circuit the
403 * rest of the checks:
405 * if ((match && op->op == OP_OR) ||
406 * (!match && op->op == OP_AND))
409 type = op->op == OP_OR;
411 for (i = 0; i < op->val; i++) {
412 pred = &preds[op->ops[i]];
413 match = pred->fn(pred, rec);
420 /* return 1 if event matches, 0 otherwise (discard) */
421 int filter_match_preds(struct event_filter *filter, void *rec)
424 enum move_type move = MOVE_DOWN;
425 struct filter_pred *preds;
426 struct filter_pred *pred;
427 struct filter_pred *root;
431 /* no filter is considered a match */
435 n_preds = filter->n_preds;
441 * n_preds, root and filter->preds are protect with preemption disabled.
443 preds = rcu_dereference_sched(filter->preds);
444 root = rcu_dereference_sched(filter->root);
450 /* match is currently meaningless */
456 /* only AND and OR have children */
457 if (pred->left != FILTER_PRED_INVALID) {
458 /* If ops is set, then it was folded. */
460 /* keep going to down the left side */
461 pred = &preds[pred->left];
464 /* We can treat folded ops as a leaf node */
465 match = process_ops(preds, pred, rec);
467 match = pred->fn(pred, rec);
468 /* If this pred is the only pred */
471 pred = get_pred_parent(pred, preds,
472 pred->parent, &move);
474 case MOVE_UP_FROM_LEFT:
476 * Check for short circuits.
478 * Optimization: !!match == (pred->op == OP_OR)
480 * if ((match && pred->op == OP_OR) ||
481 * (!match && pred->op == OP_AND))
483 if (!!match == (pred->op == OP_OR)) {
486 pred = get_pred_parent(pred, preds,
487 pred->parent, &move);
490 /* now go down the right side of the tree. */
491 pred = &preds[pred->right];
494 case MOVE_UP_FROM_RIGHT:
495 /* We finished this equation. */
498 pred = get_pred_parent(pred, preds,
499 pred->parent, &move);
507 EXPORT_SYMBOL_GPL(filter_match_preds);
509 static void parse_error(struct filter_parse_state *ps, int err, int pos)
512 ps->lasterr_pos = pos;
515 static void remove_filter_string(struct event_filter *filter)
520 kfree(filter->filter_string);
521 filter->filter_string = NULL;
524 static int replace_filter_string(struct event_filter *filter,
527 kfree(filter->filter_string);
528 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
529 if (!filter->filter_string)
535 static int append_filter_string(struct event_filter *filter,
539 char *new_filter_string;
541 BUG_ON(!filter->filter_string);
542 newlen = strlen(filter->filter_string) + strlen(string) + 1;
543 new_filter_string = kmalloc(newlen, GFP_KERNEL);
544 if (!new_filter_string)
547 strcpy(new_filter_string, filter->filter_string);
548 strcat(new_filter_string, string);
549 kfree(filter->filter_string);
550 filter->filter_string = new_filter_string;
555 static void append_filter_err(struct filter_parse_state *ps,
556 struct event_filter *filter)
558 int pos = ps->lasterr_pos;
561 buf = (char *)__get_free_page(GFP_TEMPORARY);
565 append_filter_string(filter, "\n");
566 memset(buf, ' ', PAGE_SIZE);
567 if (pos > PAGE_SIZE - 128)
570 pbuf = &buf[pos] + 1;
572 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
573 append_filter_string(filter, buf);
574 free_page((unsigned long) buf);
577 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
579 struct event_filter *filter;
581 mutex_lock(&event_mutex);
582 filter = call->filter;
583 if (filter && filter->filter_string)
584 trace_seq_printf(s, "%s\n", filter->filter_string);
586 trace_seq_printf(s, "none\n");
587 mutex_unlock(&event_mutex);
590 void print_subsystem_event_filter(struct event_subsystem *system,
593 struct event_filter *filter;
595 mutex_lock(&event_mutex);
596 filter = system->filter;
597 if (filter && filter->filter_string)
598 trace_seq_printf(s, "%s\n", filter->filter_string);
600 trace_seq_printf(s, "none\n");
601 mutex_unlock(&event_mutex);
604 static struct ftrace_event_field *
605 __find_event_field(struct list_head *head, char *name)
607 struct ftrace_event_field *field;
609 list_for_each_entry(field, head, link) {
610 if (!strcmp(field->name, name))
617 static struct ftrace_event_field *
618 find_event_field(struct ftrace_event_call *call, char *name)
620 struct ftrace_event_field *field;
621 struct list_head *head;
623 field = __find_event_field(&ftrace_common_fields, name);
627 head = trace_get_fields(call);
628 return __find_event_field(head, name);
631 static void filter_free_pred(struct filter_pred *pred)
633 kfree(pred->field_name);
636 static void filter_clear_pred(struct filter_pred *pred)
638 kfree(pred->field_name);
639 pred->field_name = NULL;
643 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
645 stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
648 stack->index = n_preds;
652 static void __free_pred_stack(struct pred_stack *stack)
658 static int __push_pred_stack(struct pred_stack *stack,
659 struct filter_pred *pred)
661 int index = stack->index;
663 if (WARN_ON(index == 0))
666 stack->preds[--index] = pred;
667 stack->index = index;
671 static struct filter_pred *
672 __pop_pred_stack(struct pred_stack *stack)
674 struct filter_pred *pred;
675 int index = stack->index;
677 pred = stack->preds[index++];
681 stack->index = index;
685 static int filter_set_pred(struct event_filter *filter,
687 struct pred_stack *stack,
688 struct filter_pred *src,
691 struct filter_pred *dest = &filter->preds[idx];
692 struct filter_pred *left;
693 struct filter_pred *right;
696 if (src->field_name) {
697 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
698 if (!dest->field_name)
704 if (dest->op == OP_OR || dest->op == OP_AND) {
705 right = __pop_pred_stack(stack);
706 left = __pop_pred_stack(stack);
710 * If both children can be folded
711 * and they are the same op as this op or a leaf,
712 * then this op can be folded.
714 if (left->index & FILTER_PRED_FOLD &&
715 (left->op == dest->op ||
716 left->left == FILTER_PRED_INVALID) &&
717 right->index & FILTER_PRED_FOLD &&
718 (right->op == dest->op ||
719 right->left == FILTER_PRED_INVALID))
720 dest->index |= FILTER_PRED_FOLD;
722 dest->left = left->index & ~FILTER_PRED_FOLD;
723 dest->right = right->index & ~FILTER_PRED_FOLD;
724 left->parent = dest->index & ~FILTER_PRED_FOLD;
725 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
728 * Make dest->left invalid to be used as a quick
729 * way to know this is a leaf node.
731 dest->left = FILTER_PRED_INVALID;
733 /* All leafs allow folding the parent ops. */
734 dest->index |= FILTER_PRED_FOLD;
737 return __push_pred_stack(stack, dest);
740 static void __free_preds(struct event_filter *filter)
745 for (i = 0; i < filter->a_preds; i++)
746 kfree(filter->preds[i].field_name);
747 kfree(filter->preds);
748 filter->preds = NULL;
754 static void filter_disable(struct ftrace_event_call *call)
756 call->flags &= ~TRACE_EVENT_FL_FILTERED;
759 static void __free_filter(struct event_filter *filter)
764 __free_preds(filter);
765 kfree(filter->filter_string);
770 * Called when destroying the ftrace_event_call.
771 * The call is being freed, so we do not need to worry about
772 * the call being currently used. This is for module code removing
773 * the tracepoints from within it.
775 void destroy_preds(struct ftrace_event_call *call)
777 __free_filter(call->filter);
781 static struct event_filter *__alloc_filter(void)
783 struct event_filter *filter;
785 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
789 static int __alloc_preds(struct event_filter *filter, int n_preds)
791 struct filter_pred *pred;
795 __free_preds(filter);
798 kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
803 filter->a_preds = n_preds;
806 for (i = 0; i < n_preds; i++) {
807 pred = &filter->preds[i];
808 pred->fn = filter_pred_none;
814 static void filter_free_subsystem_preds(struct event_subsystem *system)
816 struct ftrace_event_call *call;
818 list_for_each_entry(call, &ftrace_events, list) {
819 if (strcmp(call->class->system, system->name) != 0)
822 filter_disable(call);
823 remove_filter_string(call->filter);
827 static void filter_free_subsystem_filters(struct event_subsystem *system)
829 struct ftrace_event_call *call;
831 list_for_each_entry(call, &ftrace_events, list) {
832 if (strcmp(call->class->system, system->name) != 0)
834 __free_filter(call->filter);
839 static int filter_add_pred_fn(struct filter_parse_state *ps,
840 struct ftrace_event_call *call,
841 struct event_filter *filter,
842 struct filter_pred *pred,
843 struct pred_stack *stack,
848 if (WARN_ON(filter->n_preds == filter->a_preds)) {
849 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
853 idx = filter->n_preds;
854 filter_clear_pred(&filter->preds[idx]);
855 err = filter_set_pred(filter, idx, stack, pred, fn);
864 int filter_assign_type(const char *type)
866 if (strstr(type, "__data_loc") && strstr(type, "char"))
867 return FILTER_DYN_STRING;
869 if (strchr(type, '[') && strstr(type, "char"))
870 return FILTER_STATIC_STRING;
875 static bool is_string_field(struct ftrace_event_field *field)
877 return field->filter_type == FILTER_DYN_STRING ||
878 field->filter_type == FILTER_STATIC_STRING ||
879 field->filter_type == FILTER_PTR_STRING;
882 static int is_legal_op(struct ftrace_event_field *field, int op)
884 if (is_string_field(field) &&
885 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
887 if (!is_string_field(field) && op == OP_GLOB)
893 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
896 filter_pred_fn_t fn = NULL;
898 switch (field_size) {
900 if (op == OP_EQ || op == OP_NE)
902 else if (field_is_signed)
903 fn = filter_pred_s64;
905 fn = filter_pred_u64;
908 if (op == OP_EQ || op == OP_NE)
910 else if (field_is_signed)
911 fn = filter_pred_s32;
913 fn = filter_pred_u32;
916 if (op == OP_EQ || op == OP_NE)
918 else if (field_is_signed)
919 fn = filter_pred_s16;
921 fn = filter_pred_u16;
924 if (op == OP_EQ || op == OP_NE)
926 else if (field_is_signed)
936 static int filter_add_pred(struct filter_parse_state *ps,
937 struct ftrace_event_call *call,
938 struct event_filter *filter,
939 struct filter_pred *pred,
940 struct pred_stack *stack,
943 struct ftrace_event_field *field;
945 unsigned long long val;
948 fn = pred->fn = filter_pred_none;
950 if (pred->op == OP_AND)
952 else if (pred->op == OP_OR)
955 field = find_event_field(call, pred->field_name);
957 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
961 pred->offset = field->offset;
963 if (!is_legal_op(field, pred->op)) {
964 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
968 if (is_string_field(field)) {
969 filter_build_regex(pred);
971 if (field->filter_type == FILTER_STATIC_STRING) {
972 fn = filter_pred_string;
973 pred->regex.field_len = field->size;
974 } else if (field->filter_type == FILTER_DYN_STRING)
975 fn = filter_pred_strloc;
977 fn = filter_pred_pchar;
979 if (field->is_signed)
980 ret = strict_strtoll(pred->regex.pattern, 0, &val);
982 ret = strict_strtoull(pred->regex.pattern, 0, &val);
984 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
989 fn = select_comparison_fn(pred->op, field->size,
992 parse_error(ps, FILT_ERR_INVALID_OP, 0);
997 if (pred->op == OP_NE)
1002 return filter_add_pred_fn(ps, call, filter, pred, stack, fn);
1006 static void parse_init(struct filter_parse_state *ps,
1007 struct filter_op *ops,
1010 memset(ps, '\0', sizeof(*ps));
1012 ps->infix.string = infix_string;
1013 ps->infix.cnt = strlen(infix_string);
1016 INIT_LIST_HEAD(&ps->opstack);
1017 INIT_LIST_HEAD(&ps->postfix);
1020 static char infix_next(struct filter_parse_state *ps)
1024 return ps->infix.string[ps->infix.tail++];
1027 static char infix_peek(struct filter_parse_state *ps)
1029 if (ps->infix.tail == strlen(ps->infix.string))
1032 return ps->infix.string[ps->infix.tail];
1035 static void infix_advance(struct filter_parse_state *ps)
1041 static inline int is_precedence_lower(struct filter_parse_state *ps,
1044 return ps->ops[a].precedence < ps->ops[b].precedence;
1047 static inline int is_op_char(struct filter_parse_state *ps, char c)
1051 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1052 if (ps->ops[i].string[0] == c)
1059 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1061 char nextc = infix_peek(ps);
1069 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1070 if (!strcmp(opstr, ps->ops[i].string)) {
1072 return ps->ops[i].id;
1078 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1079 if (!strcmp(opstr, ps->ops[i].string))
1080 return ps->ops[i].id;
1086 static inline void clear_operand_string(struct filter_parse_state *ps)
1088 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1089 ps->operand.tail = 0;
1092 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1094 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1097 ps->operand.string[ps->operand.tail++] = c;
1102 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1104 struct opstack_op *opstack_op;
1106 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1110 opstack_op->op = op;
1111 list_add(&opstack_op->list, &ps->opstack);
1116 static int filter_opstack_empty(struct filter_parse_state *ps)
1118 return list_empty(&ps->opstack);
1121 static int filter_opstack_top(struct filter_parse_state *ps)
1123 struct opstack_op *opstack_op;
1125 if (filter_opstack_empty(ps))
1128 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1130 return opstack_op->op;
1133 static int filter_opstack_pop(struct filter_parse_state *ps)
1135 struct opstack_op *opstack_op;
1138 if (filter_opstack_empty(ps))
1141 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1142 op = opstack_op->op;
1143 list_del(&opstack_op->list);
1150 static void filter_opstack_clear(struct filter_parse_state *ps)
1152 while (!filter_opstack_empty(ps))
1153 filter_opstack_pop(ps);
1156 static char *curr_operand(struct filter_parse_state *ps)
1158 return ps->operand.string;
1161 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1163 struct postfix_elt *elt;
1165 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1170 elt->operand = kstrdup(operand, GFP_KERNEL);
1171 if (!elt->operand) {
1176 list_add_tail(&elt->list, &ps->postfix);
1181 static int postfix_append_op(struct filter_parse_state *ps, int op)
1183 struct postfix_elt *elt;
1185 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1190 elt->operand = NULL;
1192 list_add_tail(&elt->list, &ps->postfix);
1197 static void postfix_clear(struct filter_parse_state *ps)
1199 struct postfix_elt *elt;
1201 while (!list_empty(&ps->postfix)) {
1202 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1203 list_del(&elt->list);
1204 kfree(elt->operand);
1209 static int filter_parse(struct filter_parse_state *ps)
1215 while ((ch = infix_next(ps))) {
1227 if (is_op_char(ps, ch)) {
1228 op = infix_get_op(ps, ch);
1229 if (op == OP_NONE) {
1230 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1234 if (strlen(curr_operand(ps))) {
1235 postfix_append_operand(ps, curr_operand(ps));
1236 clear_operand_string(ps);
1239 while (!filter_opstack_empty(ps)) {
1240 top_op = filter_opstack_top(ps);
1241 if (!is_precedence_lower(ps, top_op, op)) {
1242 top_op = filter_opstack_pop(ps);
1243 postfix_append_op(ps, top_op);
1249 filter_opstack_push(ps, op);
1254 filter_opstack_push(ps, OP_OPEN_PAREN);
1259 if (strlen(curr_operand(ps))) {
1260 postfix_append_operand(ps, curr_operand(ps));
1261 clear_operand_string(ps);
1264 top_op = filter_opstack_pop(ps);
1265 while (top_op != OP_NONE) {
1266 if (top_op == OP_OPEN_PAREN)
1268 postfix_append_op(ps, top_op);
1269 top_op = filter_opstack_pop(ps);
1271 if (top_op == OP_NONE) {
1272 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1278 if (append_operand_char(ps, ch)) {
1279 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1284 if (strlen(curr_operand(ps)))
1285 postfix_append_operand(ps, curr_operand(ps));
1287 while (!filter_opstack_empty(ps)) {
1288 top_op = filter_opstack_pop(ps);
1289 if (top_op == OP_NONE)
1291 if (top_op == OP_OPEN_PAREN) {
1292 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1295 postfix_append_op(ps, top_op);
1301 static struct filter_pred *create_pred(struct filter_parse_state *ps,
1302 int op, char *operand1, char *operand2)
1304 static struct filter_pred pred;
1306 memset(&pred, 0, sizeof(pred));
1309 if (op == OP_AND || op == OP_OR)
1312 if (!operand1 || !operand2) {
1313 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1317 pred.field_name = kstrdup(operand1, GFP_KERNEL);
1318 if (!pred.field_name)
1321 strcpy(pred.regex.pattern, operand2);
1322 pred.regex.len = strlen(pred.regex.pattern);
1327 static int check_preds(struct filter_parse_state *ps)
1329 int n_normal_preds = 0, n_logical_preds = 0;
1330 struct postfix_elt *elt;
1332 list_for_each_entry(elt, &ps->postfix, list) {
1333 if (elt->op == OP_NONE)
1336 if (elt->op == OP_AND || elt->op == OP_OR) {
1343 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1344 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1351 static int count_preds(struct filter_parse_state *ps)
1353 struct postfix_elt *elt;
1356 list_for_each_entry(elt, &ps->postfix, list) {
1357 if (elt->op == OP_NONE)
1366 * The tree is walked at filtering of an event. If the tree is not correctly
1367 * built, it may cause an infinite loop. Check here that the tree does
1370 static int check_pred_tree(struct event_filter *filter,
1371 struct filter_pred *root)
1373 struct filter_pred *preds;
1374 struct filter_pred *pred;
1375 enum move_type move = MOVE_DOWN;
1381 * The max that we can hit a node is three times.
1382 * Once going down, once coming up from left, and
1383 * once coming up from right. This is more than enough
1384 * since leafs are only hit a single time.
1386 max = 3 * filter->n_preds;
1388 preds = filter->preds;
1394 if (WARN_ON(count++ > max))
1399 if (pred->left != FILTER_PRED_INVALID) {
1400 pred = &preds[pred->left];
1403 /* A leaf at the root is just a leaf in the tree */
1406 pred = get_pred_parent(pred, preds,
1407 pred->parent, &move);
1409 case MOVE_UP_FROM_LEFT:
1410 pred = &preds[pred->right];
1413 case MOVE_UP_FROM_RIGHT:
1416 pred = get_pred_parent(pred, preds,
1417 pred->parent, &move);
1427 static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1429 struct filter_pred *pred;
1430 enum move_type move = MOVE_DOWN;
1439 if (pred->left != FILTER_PRED_INVALID) {
1440 pred = &preds[pred->left];
1443 /* A leaf at the root is just a leaf in the tree */
1447 pred = get_pred_parent(pred, preds,
1448 pred->parent, &move);
1450 case MOVE_UP_FROM_LEFT:
1451 pred = &preds[pred->right];
1454 case MOVE_UP_FROM_RIGHT:
1457 pred = get_pred_parent(pred, preds,
1458 pred->parent, &move);
1467 static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1469 struct filter_pred *pred;
1470 enum move_type move = MOVE_DOWN;
1475 /* No need to keep the fold flag */
1476 root->index &= ~FILTER_PRED_FOLD;
1478 /* If the root is a leaf then do nothing */
1479 if (root->left == FILTER_PRED_INVALID)
1482 /* count the children */
1483 children = count_leafs(preds, &preds[root->left]);
1484 children += count_leafs(preds, &preds[root->right]);
1486 root->ops = kzalloc(sizeof(*root->ops) * children, GFP_KERNEL);
1490 root->val = children;
1496 if (pred->left != FILTER_PRED_INVALID) {
1497 pred = &preds[pred->left];
1500 if (WARN_ON(count == children))
1502 pred->index &= ~FILTER_PRED_FOLD;
1503 root->ops[count++] = pred->index;
1504 pred = get_pred_parent(pred, preds,
1505 pred->parent, &move);
1507 case MOVE_UP_FROM_LEFT:
1508 pred = &preds[pred->right];
1511 case MOVE_UP_FROM_RIGHT:
1514 pred = get_pred_parent(pred, preds,
1515 pred->parent, &move);
1525 * To optimize the processing of the ops, if we have several "ors" or
1526 * "ands" together, we can put them in an array and process them all
1527 * together speeding up the filter logic.
1529 static int fold_pred_tree(struct event_filter *filter,
1530 struct filter_pred *root)
1532 struct filter_pred *preds;
1533 struct filter_pred *pred;
1534 enum move_type move = MOVE_DOWN;
1538 preds = filter->preds;
1546 if (pred->index & FILTER_PRED_FOLD) {
1547 err = fold_pred(preds, pred);
1550 /* Folded nodes are like leafs */
1551 } else if (pred->left != FILTER_PRED_INVALID) {
1552 pred = &preds[pred->left];
1556 /* A leaf at the root is just a leaf in the tree */
1559 pred = get_pred_parent(pred, preds,
1560 pred->parent, &move);
1562 case MOVE_UP_FROM_LEFT:
1563 pred = &preds[pred->right];
1566 case MOVE_UP_FROM_RIGHT:
1569 pred = get_pred_parent(pred, preds,
1570 pred->parent, &move);
1579 static int replace_preds(struct ftrace_event_call *call,
1580 struct event_filter *filter,
1581 struct filter_parse_state *ps,
1582 char *filter_string,
1585 char *operand1 = NULL, *operand2 = NULL;
1586 struct filter_pred *pred;
1587 struct filter_pred *root;
1588 struct postfix_elt *elt;
1589 struct pred_stack stack = { }; /* init to NULL */
1593 n_preds = count_preds(ps);
1594 if (n_preds >= MAX_FILTER_PRED) {
1595 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1599 err = check_preds(ps);
1604 err = __alloc_pred_stack(&stack, n_preds);
1607 err = __alloc_preds(filter, n_preds);
1613 list_for_each_entry(elt, &ps->postfix, list) {
1614 if (elt->op == OP_NONE) {
1616 operand1 = elt->operand;
1618 operand2 = elt->operand;
1620 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1627 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1628 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1633 pred = create_pred(ps, elt->op, operand1, operand2);
1638 err = filter_add_pred(ps, call, filter, pred, &stack, dry_run);
1639 filter_free_pred(pred);
1643 operand1 = operand2 = NULL;
1647 /* We should have one item left on the stack */
1648 pred = __pop_pred_stack(&stack);
1651 /* This item is where we start from in matching */
1653 /* Make sure the stack is empty */
1654 pred = __pop_pred_stack(&stack);
1655 if (WARN_ON(pred)) {
1657 filter->root = NULL;
1660 err = check_pred_tree(filter, root);
1664 /* Optimize the tree */
1665 err = fold_pred_tree(filter, root);
1669 /* We don't set root until we know it works */
1671 filter->root = root;
1676 __free_pred_stack(&stack);
1680 struct filter_list {
1681 struct list_head list;
1682 struct event_filter *filter;
1685 static int replace_system_preds(struct event_subsystem *system,
1686 struct filter_parse_state *ps,
1687 char *filter_string)
1689 struct ftrace_event_call *call;
1690 struct filter_list *filter_item;
1691 struct filter_list *tmp;
1692 LIST_HEAD(filter_list);
1696 list_for_each_entry(call, &ftrace_events, list) {
1698 if (strcmp(call->class->system, system->name) != 0)
1702 * Try to see if the filter can be applied
1703 * (filter arg is ignored on dry_run)
1705 err = replace_preds(call, NULL, ps, filter_string, true);
1710 list_for_each_entry(call, &ftrace_events, list) {
1711 struct event_filter *filter;
1713 if (strcmp(call->class->system, system->name) != 0)
1716 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1720 list_add_tail(&filter_item->list, &filter_list);
1722 filter_item->filter = __alloc_filter();
1723 if (!filter_item->filter)
1725 filter = filter_item->filter;
1727 /* Can only fail on no memory */
1728 err = replace_filter_string(filter, filter_string);
1732 err = replace_preds(call, filter, ps, filter_string, false);
1734 filter_disable(call);
1735 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1736 append_filter_err(ps, filter);
1738 call->flags |= TRACE_EVENT_FL_FILTERED;
1740 * Regardless of if this returned an error, we still
1741 * replace the filter for the call.
1743 filter = call->filter;
1744 call->filter = filter_item->filter;
1745 filter_item->filter = filter;
1754 * The calls can still be using the old filters.
1755 * Do a synchronize_sched() to ensure all calls are
1756 * done with them before we free them.
1758 synchronize_sched();
1759 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1760 __free_filter(filter_item->filter);
1761 list_del(&filter_item->list);
1766 /* No call succeeded */
1767 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1768 list_del(&filter_item->list);
1771 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1774 /* If any call succeeded, we still need to sync */
1776 synchronize_sched();
1777 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1778 __free_filter(filter_item->filter);
1779 list_del(&filter_item->list);
1785 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1787 struct filter_parse_state *ps;
1788 struct event_filter *filter;
1789 struct event_filter *tmp;
1792 mutex_lock(&event_mutex);
1794 if (!strcmp(strstrip(filter_string), "0")) {
1795 filter_disable(call);
1796 filter = call->filter;
1799 call->filter = NULL;
1800 /* Make sure the filter is not being used */
1801 synchronize_sched();
1802 __free_filter(filter);
1807 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1811 filter = __alloc_filter();
1817 replace_filter_string(filter, filter_string);
1819 parse_init(ps, filter_ops, filter_string);
1820 err = filter_parse(ps);
1822 append_filter_err(ps, filter);
1826 err = replace_preds(call, filter, ps, filter_string, false);
1828 filter_disable(call);
1829 append_filter_err(ps, filter);
1831 call->flags |= TRACE_EVENT_FL_FILTERED;
1834 * Always swap the call filter with the new filter
1835 * even if there was an error. If there was an error
1836 * in the filter, we disable the filter and show the error
1840 call->filter = filter;
1842 /* Make sure the call is done with the filter */
1843 synchronize_sched();
1846 filter_opstack_clear(ps);
1850 mutex_unlock(&event_mutex);
1855 int apply_subsystem_event_filter(struct event_subsystem *system,
1856 char *filter_string)
1858 struct filter_parse_state *ps;
1859 struct event_filter *filter;
1862 mutex_lock(&event_mutex);
1864 /* Make sure the system still has events */
1865 if (!system->nr_events) {
1870 if (!strcmp(strstrip(filter_string), "0")) {
1871 filter_free_subsystem_preds(system);
1872 remove_filter_string(system->filter);
1873 filter = system->filter;
1874 system->filter = NULL;
1875 /* Ensure all filters are no longer used */
1876 synchronize_sched();
1877 filter_free_subsystem_filters(system);
1878 __free_filter(filter);
1883 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1887 filter = __alloc_filter();
1891 replace_filter_string(filter, filter_string);
1893 * No event actually uses the system filter
1894 * we can free it without synchronize_sched().
1896 __free_filter(system->filter);
1897 system->filter = filter;
1899 parse_init(ps, filter_ops, filter_string);
1900 err = filter_parse(ps);
1902 append_filter_err(ps, system->filter);
1906 err = replace_system_preds(system, ps, filter_string);
1908 append_filter_err(ps, system->filter);
1911 filter_opstack_clear(ps);
1915 mutex_unlock(&event_mutex);
1920 #ifdef CONFIG_PERF_EVENTS
1922 void ftrace_profile_free_filter(struct perf_event *event)
1924 struct event_filter *filter = event->filter;
1926 event->filter = NULL;
1927 __free_filter(filter);
1930 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1934 struct event_filter *filter;
1935 struct filter_parse_state *ps;
1936 struct ftrace_event_call *call = NULL;
1938 mutex_lock(&event_mutex);
1940 list_for_each_entry(call, &ftrace_events, list) {
1941 if (call->event.type == event_id)
1946 if (&call->list == &ftrace_events)
1953 filter = __alloc_filter();
1955 err = PTR_ERR(filter);
1960 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1964 parse_init(ps, filter_ops, filter_str);
1965 err = filter_parse(ps);
1969 err = replace_preds(call, filter, ps, filter_str, false);
1971 event->filter = filter;
1974 filter_opstack_clear(ps);
1980 __free_filter(filter);
1983 mutex_unlock(&event_mutex);
1988 #endif /* CONFIG_PERF_EVENTS */