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];
390 typedef int (*filter_pred_walkcb_t) (enum move_type move,
391 struct filter_pred *pred,
392 int *err, void *data);
394 static int walk_pred_tree(struct filter_pred *preds,
395 struct filter_pred *root,
396 filter_pred_walkcb_t cb, void *data)
398 struct filter_pred *pred = root;
399 enum move_type move = MOVE_DOWN;
408 ret = cb(move, pred, &err, data);
409 if (ret == WALK_PRED_ABORT)
411 if (ret == WALK_PRED_PARENT)
416 if (pred->left != FILTER_PRED_INVALID) {
417 pred = &preds[pred->left];
421 case MOVE_UP_FROM_LEFT:
422 pred = &preds[pred->right];
425 case MOVE_UP_FROM_RIGHT:
429 pred = get_pred_parent(pred, preds,
442 * A series of AND or ORs where found together. Instead of
443 * climbing up and down the tree branches, an array of the
444 * ops were made in order of checks. We can just move across
445 * the array and short circuit if needed.
447 static int process_ops(struct filter_pred *preds,
448 struct filter_pred *op, void *rec)
450 struct filter_pred *pred;
456 * Micro-optimization: We set type to true if op
457 * is an OR and false otherwise (AND). Then we
458 * just need to test if the match is equal to
459 * the type, and if it is, we can short circuit the
460 * rest of the checks:
462 * if ((match && op->op == OP_OR) ||
463 * (!match && op->op == OP_AND))
466 type = op->op == OP_OR;
468 for (i = 0; i < op->val; i++) {
469 pred = &preds[op->ops[i]];
470 match = pred->fn(pred, rec);
477 /* return 1 if event matches, 0 otherwise (discard) */
478 int filter_match_preds(struct event_filter *filter, void *rec)
481 enum move_type move = MOVE_DOWN;
482 struct filter_pred *preds;
483 struct filter_pred *pred;
484 struct filter_pred *root;
488 /* no filter is considered a match */
492 n_preds = filter->n_preds;
498 * n_preds, root and filter->preds are protect with preemption disabled.
500 preds = rcu_dereference_sched(filter->preds);
501 root = rcu_dereference_sched(filter->root);
507 /* match is currently meaningless */
513 /* only AND and OR have children */
514 if (pred->left != FILTER_PRED_INVALID) {
515 /* If ops is set, then it was folded. */
517 /* keep going to down the left side */
518 pred = &preds[pred->left];
521 /* We can treat folded ops as a leaf node */
522 match = process_ops(preds, pred, rec);
524 match = pred->fn(pred, rec);
525 /* If this pred is the only pred */
528 pred = get_pred_parent(pred, preds,
529 pred->parent, &move);
531 case MOVE_UP_FROM_LEFT:
533 * Check for short circuits.
535 * Optimization: !!match == (pred->op == OP_OR)
537 * if ((match && pred->op == OP_OR) ||
538 * (!match && pred->op == OP_AND))
540 if (!!match == (pred->op == OP_OR)) {
543 pred = get_pred_parent(pred, preds,
544 pred->parent, &move);
547 /* now go down the right side of the tree. */
548 pred = &preds[pred->right];
551 case MOVE_UP_FROM_RIGHT:
552 /* We finished this equation. */
555 pred = get_pred_parent(pred, preds,
556 pred->parent, &move);
564 EXPORT_SYMBOL_GPL(filter_match_preds);
566 static void parse_error(struct filter_parse_state *ps, int err, int pos)
569 ps->lasterr_pos = pos;
572 static void remove_filter_string(struct event_filter *filter)
577 kfree(filter->filter_string);
578 filter->filter_string = NULL;
581 static int replace_filter_string(struct event_filter *filter,
584 kfree(filter->filter_string);
585 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
586 if (!filter->filter_string)
592 static int append_filter_string(struct event_filter *filter,
596 char *new_filter_string;
598 BUG_ON(!filter->filter_string);
599 newlen = strlen(filter->filter_string) + strlen(string) + 1;
600 new_filter_string = kmalloc(newlen, GFP_KERNEL);
601 if (!new_filter_string)
604 strcpy(new_filter_string, filter->filter_string);
605 strcat(new_filter_string, string);
606 kfree(filter->filter_string);
607 filter->filter_string = new_filter_string;
612 static void append_filter_err(struct filter_parse_state *ps,
613 struct event_filter *filter)
615 int pos = ps->lasterr_pos;
618 buf = (char *)__get_free_page(GFP_TEMPORARY);
622 append_filter_string(filter, "\n");
623 memset(buf, ' ', PAGE_SIZE);
624 if (pos > PAGE_SIZE - 128)
627 pbuf = &buf[pos] + 1;
629 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
630 append_filter_string(filter, buf);
631 free_page((unsigned long) buf);
634 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
636 struct event_filter *filter;
638 mutex_lock(&event_mutex);
639 filter = call->filter;
640 if (filter && filter->filter_string)
641 trace_seq_printf(s, "%s\n", filter->filter_string);
643 trace_seq_printf(s, "none\n");
644 mutex_unlock(&event_mutex);
647 void print_subsystem_event_filter(struct event_subsystem *system,
650 struct event_filter *filter;
652 mutex_lock(&event_mutex);
653 filter = system->filter;
654 if (filter && filter->filter_string)
655 trace_seq_printf(s, "%s\n", filter->filter_string);
657 trace_seq_printf(s, "none\n");
658 mutex_unlock(&event_mutex);
661 static struct ftrace_event_field *
662 __find_event_field(struct list_head *head, char *name)
664 struct ftrace_event_field *field;
666 list_for_each_entry(field, head, link) {
667 if (!strcmp(field->name, name))
674 static struct ftrace_event_field *
675 find_event_field(struct ftrace_event_call *call, char *name)
677 struct ftrace_event_field *field;
678 struct list_head *head;
680 field = __find_event_field(&ftrace_common_fields, name);
684 head = trace_get_fields(call);
685 return __find_event_field(head, name);
688 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
690 stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
693 stack->index = n_preds;
697 static void __free_pred_stack(struct pred_stack *stack)
703 static int __push_pred_stack(struct pred_stack *stack,
704 struct filter_pred *pred)
706 int index = stack->index;
708 if (WARN_ON(index == 0))
711 stack->preds[--index] = pred;
712 stack->index = index;
716 static struct filter_pred *
717 __pop_pred_stack(struct pred_stack *stack)
719 struct filter_pred *pred;
720 int index = stack->index;
722 pred = stack->preds[index++];
726 stack->index = index;
730 static int filter_set_pred(struct event_filter *filter,
732 struct pred_stack *stack,
733 struct filter_pred *src)
735 struct filter_pred *dest = &filter->preds[idx];
736 struct filter_pred *left;
737 struct filter_pred *right;
742 if (dest->op == OP_OR || dest->op == OP_AND) {
743 right = __pop_pred_stack(stack);
744 left = __pop_pred_stack(stack);
748 * If both children can be folded
749 * and they are the same op as this op or a leaf,
750 * then this op can be folded.
752 if (left->index & FILTER_PRED_FOLD &&
753 (left->op == dest->op ||
754 left->left == FILTER_PRED_INVALID) &&
755 right->index & FILTER_PRED_FOLD &&
756 (right->op == dest->op ||
757 right->left == FILTER_PRED_INVALID))
758 dest->index |= FILTER_PRED_FOLD;
760 dest->left = left->index & ~FILTER_PRED_FOLD;
761 dest->right = right->index & ~FILTER_PRED_FOLD;
762 left->parent = dest->index & ~FILTER_PRED_FOLD;
763 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
766 * Make dest->left invalid to be used as a quick
767 * way to know this is a leaf node.
769 dest->left = FILTER_PRED_INVALID;
771 /* All leafs allow folding the parent ops. */
772 dest->index |= FILTER_PRED_FOLD;
775 return __push_pred_stack(stack, dest);
778 static void __free_preds(struct event_filter *filter)
781 kfree(filter->preds);
782 filter->preds = NULL;
788 static void filter_disable(struct ftrace_event_call *call)
790 call->flags &= ~TRACE_EVENT_FL_FILTERED;
793 static void __free_filter(struct event_filter *filter)
798 __free_preds(filter);
799 kfree(filter->filter_string);
804 * Called when destroying the ftrace_event_call.
805 * The call is being freed, so we do not need to worry about
806 * the call being currently used. This is for module code removing
807 * the tracepoints from within it.
809 void destroy_preds(struct ftrace_event_call *call)
811 __free_filter(call->filter);
815 static struct event_filter *__alloc_filter(void)
817 struct event_filter *filter;
819 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
823 static int __alloc_preds(struct event_filter *filter, int n_preds)
825 struct filter_pred *pred;
829 __free_preds(filter);
832 kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
837 filter->a_preds = n_preds;
840 for (i = 0; i < n_preds; i++) {
841 pred = &filter->preds[i];
842 pred->fn = filter_pred_none;
848 static void filter_free_subsystem_preds(struct event_subsystem *system)
850 struct ftrace_event_call *call;
852 list_for_each_entry(call, &ftrace_events, list) {
853 if (strcmp(call->class->system, system->name) != 0)
856 filter_disable(call);
857 remove_filter_string(call->filter);
861 static void filter_free_subsystem_filters(struct event_subsystem *system)
863 struct ftrace_event_call *call;
865 list_for_each_entry(call, &ftrace_events, list) {
866 if (strcmp(call->class->system, system->name) != 0)
868 __free_filter(call->filter);
873 static int filter_add_pred(struct filter_parse_state *ps,
874 struct event_filter *filter,
875 struct filter_pred *pred,
876 struct pred_stack *stack)
880 if (WARN_ON(filter->n_preds == filter->a_preds)) {
881 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
885 err = filter_set_pred(filter, filter->n_preds, stack, pred);
894 int filter_assign_type(const char *type)
896 if (strstr(type, "__data_loc") && strstr(type, "char"))
897 return FILTER_DYN_STRING;
899 if (strchr(type, '[') && strstr(type, "char"))
900 return FILTER_STATIC_STRING;
905 static bool is_string_field(struct ftrace_event_field *field)
907 return field->filter_type == FILTER_DYN_STRING ||
908 field->filter_type == FILTER_STATIC_STRING ||
909 field->filter_type == FILTER_PTR_STRING;
912 static int is_legal_op(struct ftrace_event_field *field, int op)
914 if (is_string_field(field) &&
915 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
917 if (!is_string_field(field) && op == OP_GLOB)
923 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
926 filter_pred_fn_t fn = NULL;
928 switch (field_size) {
930 if (op == OP_EQ || op == OP_NE)
932 else if (field_is_signed)
933 fn = filter_pred_s64;
935 fn = filter_pred_u64;
938 if (op == OP_EQ || op == OP_NE)
940 else if (field_is_signed)
941 fn = filter_pred_s32;
943 fn = filter_pred_u32;
946 if (op == OP_EQ || op == OP_NE)
948 else if (field_is_signed)
949 fn = filter_pred_s16;
951 fn = filter_pred_u16;
954 if (op == OP_EQ || op == OP_NE)
956 else if (field_is_signed)
966 static int init_pred(struct filter_parse_state *ps,
967 struct ftrace_event_field *field,
968 struct filter_pred *pred)
971 filter_pred_fn_t fn = filter_pred_none;
972 unsigned long long val;
975 pred->offset = field->offset;
977 if (!is_legal_op(field, pred->op)) {
978 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
982 if (is_string_field(field)) {
983 filter_build_regex(pred);
985 if (field->filter_type == FILTER_STATIC_STRING) {
986 fn = filter_pred_string;
987 pred->regex.field_len = field->size;
988 } else if (field->filter_type == FILTER_DYN_STRING)
989 fn = filter_pred_strloc;
991 fn = filter_pred_pchar;
993 if (field->is_signed)
994 ret = strict_strtoll(pred->regex.pattern, 0, &val);
996 ret = strict_strtoull(pred->regex.pattern, 0, &val);
998 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
1003 fn = select_comparison_fn(pred->op, field->size,
1006 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1011 if (pred->op == OP_NE)
1018 static void parse_init(struct filter_parse_state *ps,
1019 struct filter_op *ops,
1022 memset(ps, '\0', sizeof(*ps));
1024 ps->infix.string = infix_string;
1025 ps->infix.cnt = strlen(infix_string);
1028 INIT_LIST_HEAD(&ps->opstack);
1029 INIT_LIST_HEAD(&ps->postfix);
1032 static char infix_next(struct filter_parse_state *ps)
1036 return ps->infix.string[ps->infix.tail++];
1039 static char infix_peek(struct filter_parse_state *ps)
1041 if (ps->infix.tail == strlen(ps->infix.string))
1044 return ps->infix.string[ps->infix.tail];
1047 static void infix_advance(struct filter_parse_state *ps)
1053 static inline int is_precedence_lower(struct filter_parse_state *ps,
1056 return ps->ops[a].precedence < ps->ops[b].precedence;
1059 static inline int is_op_char(struct filter_parse_state *ps, char c)
1063 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1064 if (ps->ops[i].string[0] == c)
1071 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1073 char nextc = infix_peek(ps);
1081 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1082 if (!strcmp(opstr, ps->ops[i].string)) {
1084 return ps->ops[i].id;
1090 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1091 if (!strcmp(opstr, ps->ops[i].string))
1092 return ps->ops[i].id;
1098 static inline void clear_operand_string(struct filter_parse_state *ps)
1100 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1101 ps->operand.tail = 0;
1104 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1106 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1109 ps->operand.string[ps->operand.tail++] = c;
1114 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1116 struct opstack_op *opstack_op;
1118 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1122 opstack_op->op = op;
1123 list_add(&opstack_op->list, &ps->opstack);
1128 static int filter_opstack_empty(struct filter_parse_state *ps)
1130 return list_empty(&ps->opstack);
1133 static int filter_opstack_top(struct filter_parse_state *ps)
1135 struct opstack_op *opstack_op;
1137 if (filter_opstack_empty(ps))
1140 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1142 return opstack_op->op;
1145 static int filter_opstack_pop(struct filter_parse_state *ps)
1147 struct opstack_op *opstack_op;
1150 if (filter_opstack_empty(ps))
1153 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1154 op = opstack_op->op;
1155 list_del(&opstack_op->list);
1162 static void filter_opstack_clear(struct filter_parse_state *ps)
1164 while (!filter_opstack_empty(ps))
1165 filter_opstack_pop(ps);
1168 static char *curr_operand(struct filter_parse_state *ps)
1170 return ps->operand.string;
1173 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1175 struct postfix_elt *elt;
1177 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1182 elt->operand = kstrdup(operand, GFP_KERNEL);
1183 if (!elt->operand) {
1188 list_add_tail(&elt->list, &ps->postfix);
1193 static int postfix_append_op(struct filter_parse_state *ps, int op)
1195 struct postfix_elt *elt;
1197 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1202 elt->operand = NULL;
1204 list_add_tail(&elt->list, &ps->postfix);
1209 static void postfix_clear(struct filter_parse_state *ps)
1211 struct postfix_elt *elt;
1213 while (!list_empty(&ps->postfix)) {
1214 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1215 list_del(&elt->list);
1216 kfree(elt->operand);
1221 static int filter_parse(struct filter_parse_state *ps)
1227 while ((ch = infix_next(ps))) {
1239 if (is_op_char(ps, ch)) {
1240 op = infix_get_op(ps, ch);
1241 if (op == OP_NONE) {
1242 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1246 if (strlen(curr_operand(ps))) {
1247 postfix_append_operand(ps, curr_operand(ps));
1248 clear_operand_string(ps);
1251 while (!filter_opstack_empty(ps)) {
1252 top_op = filter_opstack_top(ps);
1253 if (!is_precedence_lower(ps, top_op, op)) {
1254 top_op = filter_opstack_pop(ps);
1255 postfix_append_op(ps, top_op);
1261 filter_opstack_push(ps, op);
1266 filter_opstack_push(ps, OP_OPEN_PAREN);
1271 if (strlen(curr_operand(ps))) {
1272 postfix_append_operand(ps, curr_operand(ps));
1273 clear_operand_string(ps);
1276 top_op = filter_opstack_pop(ps);
1277 while (top_op != OP_NONE) {
1278 if (top_op == OP_OPEN_PAREN)
1280 postfix_append_op(ps, top_op);
1281 top_op = filter_opstack_pop(ps);
1283 if (top_op == OP_NONE) {
1284 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1290 if (append_operand_char(ps, ch)) {
1291 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1296 if (strlen(curr_operand(ps)))
1297 postfix_append_operand(ps, curr_operand(ps));
1299 while (!filter_opstack_empty(ps)) {
1300 top_op = filter_opstack_pop(ps);
1301 if (top_op == OP_NONE)
1303 if (top_op == OP_OPEN_PAREN) {
1304 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1307 postfix_append_op(ps, top_op);
1313 static struct filter_pred *create_pred(struct filter_parse_state *ps,
1314 struct ftrace_event_call *call,
1315 int op, char *operand1, char *operand2)
1317 struct ftrace_event_field *field;
1318 static struct filter_pred pred;
1320 memset(&pred, 0, sizeof(pred));
1323 if (op == OP_AND || op == OP_OR)
1326 if (!operand1 || !operand2) {
1327 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1331 field = find_event_field(call, operand1);
1333 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
1337 strcpy(pred.regex.pattern, operand2);
1338 pred.regex.len = strlen(pred.regex.pattern);
1340 return init_pred(ps, field, &pred) ? NULL : &pred;
1343 static int check_preds(struct filter_parse_state *ps)
1345 int n_normal_preds = 0, n_logical_preds = 0;
1346 struct postfix_elt *elt;
1348 list_for_each_entry(elt, &ps->postfix, list) {
1349 if (elt->op == OP_NONE)
1352 if (elt->op == OP_AND || elt->op == OP_OR) {
1359 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1360 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1367 static int count_preds(struct filter_parse_state *ps)
1369 struct postfix_elt *elt;
1372 list_for_each_entry(elt, &ps->postfix, list) {
1373 if (elt->op == OP_NONE)
1381 struct check_pred_data {
1386 static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1387 int *err, void *data)
1389 struct check_pred_data *d = data;
1391 if (WARN_ON(d->count++ > d->max)) {
1393 return WALK_PRED_ABORT;
1395 return WALK_PRED_DEFAULT;
1399 * The tree is walked at filtering of an event. If the tree is not correctly
1400 * built, it may cause an infinite loop. Check here that the tree does
1403 static int check_pred_tree(struct event_filter *filter,
1404 struct filter_pred *root)
1406 struct check_pred_data data = {
1408 * The max that we can hit a node is three times.
1409 * Once going down, once coming up from left, and
1410 * once coming up from right. This is more than enough
1411 * since leafs are only hit a single time.
1413 .max = 3 * filter->n_preds,
1417 return walk_pred_tree(filter->preds, root,
1418 check_pred_tree_cb, &data);
1421 static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
1422 int *err, void *data)
1426 if ((move == MOVE_DOWN) &&
1427 (pred->left == FILTER_PRED_INVALID))
1430 return WALK_PRED_DEFAULT;
1433 static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1437 ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
1442 static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1444 struct filter_pred *pred;
1445 enum move_type move = MOVE_DOWN;
1450 /* No need to keep the fold flag */
1451 root->index &= ~FILTER_PRED_FOLD;
1453 /* If the root is a leaf then do nothing */
1454 if (root->left == FILTER_PRED_INVALID)
1457 /* count the children */
1458 children = count_leafs(preds, &preds[root->left]);
1459 children += count_leafs(preds, &preds[root->right]);
1461 root->ops = kzalloc(sizeof(*root->ops) * children, GFP_KERNEL);
1465 root->val = children;
1471 if (pred->left != FILTER_PRED_INVALID) {
1472 pred = &preds[pred->left];
1475 if (WARN_ON(count == children))
1477 pred->index &= ~FILTER_PRED_FOLD;
1478 root->ops[count++] = pred->index;
1479 pred = get_pred_parent(pred, preds,
1480 pred->parent, &move);
1482 case MOVE_UP_FROM_LEFT:
1483 pred = &preds[pred->right];
1486 case MOVE_UP_FROM_RIGHT:
1489 pred = get_pred_parent(pred, preds,
1490 pred->parent, &move);
1499 static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1500 int *err, void *data)
1502 struct filter_pred *preds = data;
1504 if (move != MOVE_DOWN)
1505 return WALK_PRED_DEFAULT;
1506 if (!(pred->index & FILTER_PRED_FOLD))
1507 return WALK_PRED_DEFAULT;
1509 *err = fold_pred(preds, pred);
1511 return WALK_PRED_ABORT;
1513 /* eveyrhing below is folded, continue with parent */
1514 return WALK_PRED_PARENT;
1518 * To optimize the processing of the ops, if we have several "ors" or
1519 * "ands" together, we can put them in an array and process them all
1520 * together speeding up the filter logic.
1522 static int fold_pred_tree(struct event_filter *filter,
1523 struct filter_pred *root)
1525 return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
1529 static int replace_preds(struct ftrace_event_call *call,
1530 struct event_filter *filter,
1531 struct filter_parse_state *ps,
1532 char *filter_string,
1535 char *operand1 = NULL, *operand2 = NULL;
1536 struct filter_pred *pred;
1537 struct filter_pred *root;
1538 struct postfix_elt *elt;
1539 struct pred_stack stack = { }; /* init to NULL */
1543 n_preds = count_preds(ps);
1544 if (n_preds >= MAX_FILTER_PRED) {
1545 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1549 err = check_preds(ps);
1554 err = __alloc_pred_stack(&stack, n_preds);
1557 err = __alloc_preds(filter, n_preds);
1563 list_for_each_entry(elt, &ps->postfix, list) {
1564 if (elt->op == OP_NONE) {
1566 operand1 = elt->operand;
1568 operand2 = elt->operand;
1570 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1577 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1578 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1583 pred = create_pred(ps, call, elt->op, operand1, operand2);
1590 err = filter_add_pred(ps, filter, pred, &stack);
1595 operand1 = operand2 = NULL;
1599 /* We should have one item left on the stack */
1600 pred = __pop_pred_stack(&stack);
1603 /* This item is where we start from in matching */
1605 /* Make sure the stack is empty */
1606 pred = __pop_pred_stack(&stack);
1607 if (WARN_ON(pred)) {
1609 filter->root = NULL;
1612 err = check_pred_tree(filter, root);
1616 /* Optimize the tree */
1617 err = fold_pred_tree(filter, root);
1621 /* We don't set root until we know it works */
1623 filter->root = root;
1628 __free_pred_stack(&stack);
1632 struct filter_list {
1633 struct list_head list;
1634 struct event_filter *filter;
1637 static int replace_system_preds(struct event_subsystem *system,
1638 struct filter_parse_state *ps,
1639 char *filter_string)
1641 struct ftrace_event_call *call;
1642 struct filter_list *filter_item;
1643 struct filter_list *tmp;
1644 LIST_HEAD(filter_list);
1648 list_for_each_entry(call, &ftrace_events, list) {
1650 if (strcmp(call->class->system, system->name) != 0)
1654 * Try to see if the filter can be applied
1655 * (filter arg is ignored on dry_run)
1657 err = replace_preds(call, NULL, ps, filter_string, true);
1662 list_for_each_entry(call, &ftrace_events, list) {
1663 struct event_filter *filter;
1665 if (strcmp(call->class->system, system->name) != 0)
1668 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1672 list_add_tail(&filter_item->list, &filter_list);
1674 filter_item->filter = __alloc_filter();
1675 if (!filter_item->filter)
1677 filter = filter_item->filter;
1679 /* Can only fail on no memory */
1680 err = replace_filter_string(filter, filter_string);
1684 err = replace_preds(call, filter, ps, filter_string, false);
1686 filter_disable(call);
1687 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1688 append_filter_err(ps, filter);
1690 call->flags |= TRACE_EVENT_FL_FILTERED;
1692 * Regardless of if this returned an error, we still
1693 * replace the filter for the call.
1695 filter = call->filter;
1696 call->filter = filter_item->filter;
1697 filter_item->filter = filter;
1706 * The calls can still be using the old filters.
1707 * Do a synchronize_sched() to ensure all calls are
1708 * done with them before we free them.
1710 synchronize_sched();
1711 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1712 __free_filter(filter_item->filter);
1713 list_del(&filter_item->list);
1718 /* No call succeeded */
1719 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1720 list_del(&filter_item->list);
1723 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1726 /* If any call succeeded, we still need to sync */
1728 synchronize_sched();
1729 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1730 __free_filter(filter_item->filter);
1731 list_del(&filter_item->list);
1737 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1739 struct filter_parse_state *ps;
1740 struct event_filter *filter;
1741 struct event_filter *tmp;
1744 mutex_lock(&event_mutex);
1746 if (!strcmp(strstrip(filter_string), "0")) {
1747 filter_disable(call);
1748 filter = call->filter;
1751 call->filter = NULL;
1752 /* Make sure the filter is not being used */
1753 synchronize_sched();
1754 __free_filter(filter);
1759 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1763 filter = __alloc_filter();
1769 replace_filter_string(filter, filter_string);
1771 parse_init(ps, filter_ops, filter_string);
1772 err = filter_parse(ps);
1774 append_filter_err(ps, filter);
1778 err = replace_preds(call, filter, ps, filter_string, false);
1780 filter_disable(call);
1781 append_filter_err(ps, filter);
1783 call->flags |= TRACE_EVENT_FL_FILTERED;
1786 * Always swap the call filter with the new filter
1787 * even if there was an error. If there was an error
1788 * in the filter, we disable the filter and show the error
1792 call->filter = filter;
1794 /* Make sure the call is done with the filter */
1795 synchronize_sched();
1798 filter_opstack_clear(ps);
1802 mutex_unlock(&event_mutex);
1807 int apply_subsystem_event_filter(struct event_subsystem *system,
1808 char *filter_string)
1810 struct filter_parse_state *ps;
1811 struct event_filter *filter;
1814 mutex_lock(&event_mutex);
1816 /* Make sure the system still has events */
1817 if (!system->nr_events) {
1822 if (!strcmp(strstrip(filter_string), "0")) {
1823 filter_free_subsystem_preds(system);
1824 remove_filter_string(system->filter);
1825 filter = system->filter;
1826 system->filter = NULL;
1827 /* Ensure all filters are no longer used */
1828 synchronize_sched();
1829 filter_free_subsystem_filters(system);
1830 __free_filter(filter);
1835 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1839 filter = __alloc_filter();
1843 replace_filter_string(filter, filter_string);
1845 * No event actually uses the system filter
1846 * we can free it without synchronize_sched().
1848 __free_filter(system->filter);
1849 system->filter = filter;
1851 parse_init(ps, filter_ops, filter_string);
1852 err = filter_parse(ps);
1854 append_filter_err(ps, system->filter);
1858 err = replace_system_preds(system, ps, filter_string);
1860 append_filter_err(ps, system->filter);
1863 filter_opstack_clear(ps);
1867 mutex_unlock(&event_mutex);
1872 #ifdef CONFIG_PERF_EVENTS
1874 void ftrace_profile_free_filter(struct perf_event *event)
1876 struct event_filter *filter = event->filter;
1878 event->filter = NULL;
1879 __free_filter(filter);
1882 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1886 struct event_filter *filter;
1887 struct filter_parse_state *ps;
1888 struct ftrace_event_call *call;
1890 mutex_lock(&event_mutex);
1892 call = event->tp_event;
1902 filter = __alloc_filter();
1904 err = PTR_ERR(filter);
1909 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1913 parse_init(ps, filter_ops, filter_str);
1914 err = filter_parse(ps);
1918 err = replace_preds(call, filter, ps, filter_str, false);
1920 event->filter = filter;
1923 filter_opstack_clear(ps);
1929 __free_filter(filter);
1932 mutex_unlock(&event_mutex);
1937 #endif /* CONFIG_PERF_EVENTS */