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 if (!WARN_ON_ONCE(!pred->fn))
471 match = pred->fn(pred, rec);
478 struct filter_match_preds_data {
479 struct filter_pred *preds;
484 static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred,
485 int *err, void *data)
487 struct filter_match_preds_data *d = data;
492 /* only AND and OR have children */
493 if (pred->left != FILTER_PRED_INVALID) {
494 /* If ops is set, then it was folded. */
496 return WALK_PRED_DEFAULT;
497 /* We can treat folded ops as a leaf node */
498 d->match = process_ops(d->preds, pred, d->rec);
500 if (!WARN_ON_ONCE(!pred->fn))
501 d->match = pred->fn(pred, d->rec);
504 return WALK_PRED_PARENT;
505 case MOVE_UP_FROM_LEFT:
507 * Check for short circuits.
509 * Optimization: !!match == (pred->op == OP_OR)
511 * if ((match && pred->op == OP_OR) ||
512 * (!match && pred->op == OP_AND))
514 if (!!d->match == (pred->op == OP_OR))
515 return WALK_PRED_PARENT;
517 case MOVE_UP_FROM_RIGHT:
521 return WALK_PRED_DEFAULT;
524 /* return 1 if event matches, 0 otherwise (discard) */
525 int filter_match_preds(struct event_filter *filter, void *rec)
527 struct filter_pred *preds;
528 struct filter_pred *root;
529 struct filter_match_preds_data data = {
530 /* match is currently meaningless */
536 /* no filter is considered a match */
540 n_preds = filter->n_preds;
545 * n_preds, root and filter->preds are protect with preemption disabled.
547 root = rcu_dereference_sched(filter->root);
551 data.preds = preds = rcu_dereference_sched(filter->preds);
552 ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data);
556 EXPORT_SYMBOL_GPL(filter_match_preds);
558 static void parse_error(struct filter_parse_state *ps, int err, int pos)
561 ps->lasterr_pos = pos;
564 static void remove_filter_string(struct event_filter *filter)
569 kfree(filter->filter_string);
570 filter->filter_string = NULL;
573 static int replace_filter_string(struct event_filter *filter,
576 kfree(filter->filter_string);
577 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
578 if (!filter->filter_string)
584 static int append_filter_string(struct event_filter *filter,
588 char *new_filter_string;
590 BUG_ON(!filter->filter_string);
591 newlen = strlen(filter->filter_string) + strlen(string) + 1;
592 new_filter_string = kmalloc(newlen, GFP_KERNEL);
593 if (!new_filter_string)
596 strcpy(new_filter_string, filter->filter_string);
597 strcat(new_filter_string, string);
598 kfree(filter->filter_string);
599 filter->filter_string = new_filter_string;
604 static void append_filter_err(struct filter_parse_state *ps,
605 struct event_filter *filter)
607 int pos = ps->lasterr_pos;
610 buf = (char *)__get_free_page(GFP_TEMPORARY);
614 append_filter_string(filter, "\n");
615 memset(buf, ' ', PAGE_SIZE);
616 if (pos > PAGE_SIZE - 128)
619 pbuf = &buf[pos] + 1;
621 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
622 append_filter_string(filter, buf);
623 free_page((unsigned long) buf);
626 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
628 struct event_filter *filter;
630 mutex_lock(&event_mutex);
631 filter = call->filter;
632 if (filter && filter->filter_string)
633 trace_seq_printf(s, "%s\n", filter->filter_string);
635 trace_seq_printf(s, "none\n");
636 mutex_unlock(&event_mutex);
639 void print_subsystem_event_filter(struct event_subsystem *system,
642 struct event_filter *filter;
644 mutex_lock(&event_mutex);
645 filter = system->filter;
646 if (filter && filter->filter_string)
647 trace_seq_printf(s, "%s\n", filter->filter_string);
649 trace_seq_printf(s, "none\n");
650 mutex_unlock(&event_mutex);
653 static struct ftrace_event_field *
654 __find_event_field(struct list_head *head, char *name)
656 struct ftrace_event_field *field;
658 list_for_each_entry(field, head, link) {
659 if (!strcmp(field->name, name))
666 static struct ftrace_event_field *
667 find_event_field(struct ftrace_event_call *call, char *name)
669 struct ftrace_event_field *field;
670 struct list_head *head;
672 field = __find_event_field(&ftrace_common_fields, name);
676 head = trace_get_fields(call);
677 return __find_event_field(head, name);
680 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
682 stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
685 stack->index = n_preds;
689 static void __free_pred_stack(struct pred_stack *stack)
695 static int __push_pred_stack(struct pred_stack *stack,
696 struct filter_pred *pred)
698 int index = stack->index;
700 if (WARN_ON(index == 0))
703 stack->preds[--index] = pred;
704 stack->index = index;
708 static struct filter_pred *
709 __pop_pred_stack(struct pred_stack *stack)
711 struct filter_pred *pred;
712 int index = stack->index;
714 pred = stack->preds[index++];
718 stack->index = index;
722 static int filter_set_pred(struct event_filter *filter,
724 struct pred_stack *stack,
725 struct filter_pred *src)
727 struct filter_pred *dest = &filter->preds[idx];
728 struct filter_pred *left;
729 struct filter_pred *right;
734 if (dest->op == OP_OR || dest->op == OP_AND) {
735 right = __pop_pred_stack(stack);
736 left = __pop_pred_stack(stack);
740 * If both children can be folded
741 * and they are the same op as this op or a leaf,
742 * then this op can be folded.
744 if (left->index & FILTER_PRED_FOLD &&
745 (left->op == dest->op ||
746 left->left == FILTER_PRED_INVALID) &&
747 right->index & FILTER_PRED_FOLD &&
748 (right->op == dest->op ||
749 right->left == FILTER_PRED_INVALID))
750 dest->index |= FILTER_PRED_FOLD;
752 dest->left = left->index & ~FILTER_PRED_FOLD;
753 dest->right = right->index & ~FILTER_PRED_FOLD;
754 left->parent = dest->index & ~FILTER_PRED_FOLD;
755 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
758 * Make dest->left invalid to be used as a quick
759 * way to know this is a leaf node.
761 dest->left = FILTER_PRED_INVALID;
763 /* All leafs allow folding the parent ops. */
764 dest->index |= FILTER_PRED_FOLD;
767 return __push_pred_stack(stack, dest);
770 static void __free_preds(struct event_filter *filter)
775 for (i = 0; i < filter->n_preds; i++)
776 kfree(filter->preds[i].ops);
777 kfree(filter->preds);
778 filter->preds = NULL;
784 static void filter_disable(struct ftrace_event_call *call)
786 call->flags &= ~TRACE_EVENT_FL_FILTERED;
789 static void __free_filter(struct event_filter *filter)
794 __free_preds(filter);
795 kfree(filter->filter_string);
800 * Called when destroying the ftrace_event_call.
801 * The call is being freed, so we do not need to worry about
802 * the call being currently used. This is for module code removing
803 * the tracepoints from within it.
805 void destroy_preds(struct ftrace_event_call *call)
807 __free_filter(call->filter);
811 static struct event_filter *__alloc_filter(void)
813 struct event_filter *filter;
815 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
819 static int __alloc_preds(struct event_filter *filter, int n_preds)
821 struct filter_pred *pred;
825 __free_preds(filter);
828 kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
833 filter->a_preds = n_preds;
836 for (i = 0; i < n_preds; i++) {
837 pred = &filter->preds[i];
838 pred->fn = filter_pred_none;
844 static void filter_free_subsystem_preds(struct event_subsystem *system)
846 struct ftrace_event_call *call;
848 list_for_each_entry(call, &ftrace_events, list) {
849 if (strcmp(call->class->system, system->name) != 0)
852 filter_disable(call);
853 remove_filter_string(call->filter);
857 static void filter_free_subsystem_filters(struct event_subsystem *system)
859 struct ftrace_event_call *call;
861 list_for_each_entry(call, &ftrace_events, list) {
862 if (strcmp(call->class->system, system->name) != 0)
864 __free_filter(call->filter);
869 static int filter_add_pred(struct filter_parse_state *ps,
870 struct event_filter *filter,
871 struct filter_pred *pred,
872 struct pred_stack *stack)
876 if (WARN_ON(filter->n_preds == filter->a_preds)) {
877 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
881 err = filter_set_pred(filter, filter->n_preds, stack, pred);
890 int filter_assign_type(const char *type)
892 if (strstr(type, "__data_loc") && strstr(type, "char"))
893 return FILTER_DYN_STRING;
895 if (strchr(type, '[') && strstr(type, "char"))
896 return FILTER_STATIC_STRING;
901 static bool is_string_field(struct ftrace_event_field *field)
903 return field->filter_type == FILTER_DYN_STRING ||
904 field->filter_type == FILTER_STATIC_STRING ||
905 field->filter_type == FILTER_PTR_STRING;
908 static int is_legal_op(struct ftrace_event_field *field, int op)
910 if (is_string_field(field) &&
911 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
913 if (!is_string_field(field) && op == OP_GLOB)
919 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
922 filter_pred_fn_t fn = NULL;
924 switch (field_size) {
926 if (op == OP_EQ || op == OP_NE)
928 else if (field_is_signed)
929 fn = filter_pred_s64;
931 fn = filter_pred_u64;
934 if (op == OP_EQ || op == OP_NE)
936 else if (field_is_signed)
937 fn = filter_pred_s32;
939 fn = filter_pred_u32;
942 if (op == OP_EQ || op == OP_NE)
944 else if (field_is_signed)
945 fn = filter_pred_s16;
947 fn = filter_pred_u16;
950 if (op == OP_EQ || op == OP_NE)
952 else if (field_is_signed)
962 static int init_pred(struct filter_parse_state *ps,
963 struct ftrace_event_field *field,
964 struct filter_pred *pred)
967 filter_pred_fn_t fn = filter_pred_none;
968 unsigned long long val;
971 pred->offset = field->offset;
973 if (!is_legal_op(field, pred->op)) {
974 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
978 if (is_string_field(field)) {
979 filter_build_regex(pred);
981 if (field->filter_type == FILTER_STATIC_STRING) {
982 fn = filter_pred_string;
983 pred->regex.field_len = field->size;
984 } else if (field->filter_type == FILTER_DYN_STRING)
985 fn = filter_pred_strloc;
987 fn = filter_pred_pchar;
989 if (field->is_signed)
990 ret = strict_strtoll(pred->regex.pattern, 0, &val);
992 ret = strict_strtoull(pred->regex.pattern, 0, &val);
994 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
999 fn = select_comparison_fn(pred->op, field->size,
1002 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1007 if (pred->op == OP_NE)
1014 static void parse_init(struct filter_parse_state *ps,
1015 struct filter_op *ops,
1018 memset(ps, '\0', sizeof(*ps));
1020 ps->infix.string = infix_string;
1021 ps->infix.cnt = strlen(infix_string);
1024 INIT_LIST_HEAD(&ps->opstack);
1025 INIT_LIST_HEAD(&ps->postfix);
1028 static char infix_next(struct filter_parse_state *ps)
1035 return ps->infix.string[ps->infix.tail++];
1038 static char infix_peek(struct filter_parse_state *ps)
1040 if (ps->infix.tail == strlen(ps->infix.string))
1043 return ps->infix.string[ps->infix.tail];
1046 static void infix_advance(struct filter_parse_state *ps)
1055 static inline int is_precedence_lower(struct filter_parse_state *ps,
1058 return ps->ops[a].precedence < ps->ops[b].precedence;
1061 static inline int is_op_char(struct filter_parse_state *ps, char c)
1065 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1066 if (ps->ops[i].string[0] == c)
1073 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1075 char nextc = infix_peek(ps);
1083 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1084 if (!strcmp(opstr, ps->ops[i].string)) {
1086 return ps->ops[i].id;
1092 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1093 if (!strcmp(opstr, ps->ops[i].string))
1094 return ps->ops[i].id;
1100 static inline void clear_operand_string(struct filter_parse_state *ps)
1102 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1103 ps->operand.tail = 0;
1106 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1108 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1111 ps->operand.string[ps->operand.tail++] = c;
1116 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1118 struct opstack_op *opstack_op;
1120 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1124 opstack_op->op = op;
1125 list_add(&opstack_op->list, &ps->opstack);
1130 static int filter_opstack_empty(struct filter_parse_state *ps)
1132 return list_empty(&ps->opstack);
1135 static int filter_opstack_top(struct filter_parse_state *ps)
1137 struct opstack_op *opstack_op;
1139 if (filter_opstack_empty(ps))
1142 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1144 return opstack_op->op;
1147 static int filter_opstack_pop(struct filter_parse_state *ps)
1149 struct opstack_op *opstack_op;
1152 if (filter_opstack_empty(ps))
1155 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1156 op = opstack_op->op;
1157 list_del(&opstack_op->list);
1164 static void filter_opstack_clear(struct filter_parse_state *ps)
1166 while (!filter_opstack_empty(ps))
1167 filter_opstack_pop(ps);
1170 static char *curr_operand(struct filter_parse_state *ps)
1172 return ps->operand.string;
1175 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1177 struct postfix_elt *elt;
1179 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1184 elt->operand = kstrdup(operand, GFP_KERNEL);
1185 if (!elt->operand) {
1190 list_add_tail(&elt->list, &ps->postfix);
1195 static int postfix_append_op(struct filter_parse_state *ps, int op)
1197 struct postfix_elt *elt;
1199 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1204 elt->operand = NULL;
1206 list_add_tail(&elt->list, &ps->postfix);
1211 static void postfix_clear(struct filter_parse_state *ps)
1213 struct postfix_elt *elt;
1215 while (!list_empty(&ps->postfix)) {
1216 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1217 list_del(&elt->list);
1218 kfree(elt->operand);
1223 static int filter_parse(struct filter_parse_state *ps)
1229 while ((ch = infix_next(ps))) {
1241 if (is_op_char(ps, ch)) {
1242 op = infix_get_op(ps, ch);
1243 if (op == OP_NONE) {
1244 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1248 if (strlen(curr_operand(ps))) {
1249 postfix_append_operand(ps, curr_operand(ps));
1250 clear_operand_string(ps);
1253 while (!filter_opstack_empty(ps)) {
1254 top_op = filter_opstack_top(ps);
1255 if (!is_precedence_lower(ps, top_op, op)) {
1256 top_op = filter_opstack_pop(ps);
1257 postfix_append_op(ps, top_op);
1263 filter_opstack_push(ps, op);
1268 filter_opstack_push(ps, OP_OPEN_PAREN);
1273 if (strlen(curr_operand(ps))) {
1274 postfix_append_operand(ps, curr_operand(ps));
1275 clear_operand_string(ps);
1278 top_op = filter_opstack_pop(ps);
1279 while (top_op != OP_NONE) {
1280 if (top_op == OP_OPEN_PAREN)
1282 postfix_append_op(ps, top_op);
1283 top_op = filter_opstack_pop(ps);
1285 if (top_op == OP_NONE) {
1286 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1292 if (append_operand_char(ps, ch)) {
1293 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1298 if (strlen(curr_operand(ps)))
1299 postfix_append_operand(ps, curr_operand(ps));
1301 while (!filter_opstack_empty(ps)) {
1302 top_op = filter_opstack_pop(ps);
1303 if (top_op == OP_NONE)
1305 if (top_op == OP_OPEN_PAREN) {
1306 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1309 postfix_append_op(ps, top_op);
1315 static struct filter_pred *create_pred(struct filter_parse_state *ps,
1316 struct ftrace_event_call *call,
1317 int op, char *operand1, char *operand2)
1319 struct ftrace_event_field *field;
1320 static struct filter_pred pred;
1322 memset(&pred, 0, sizeof(pred));
1325 if (op == OP_AND || op == OP_OR)
1328 if (!operand1 || !operand2) {
1329 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1333 field = find_event_field(call, operand1);
1335 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
1339 strcpy(pred.regex.pattern, operand2);
1340 pred.regex.len = strlen(pred.regex.pattern);
1342 #ifdef CONFIG_FTRACE_STARTUP_TEST
1345 return init_pred(ps, field, &pred) ? NULL : &pred;
1348 static int check_preds(struct filter_parse_state *ps)
1350 int n_normal_preds = 0, n_logical_preds = 0;
1351 struct postfix_elt *elt;
1354 list_for_each_entry(elt, &ps->postfix, list) {
1355 if (elt->op == OP_NONE) {
1360 if (elt->op == OP_AND || elt->op == OP_OR) {
1367 /* all ops should have operands */
1372 if (cnt != 1 || !n_normal_preds || n_logical_preds >= n_normal_preds) {
1373 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1380 static int count_preds(struct filter_parse_state *ps)
1382 struct postfix_elt *elt;
1385 list_for_each_entry(elt, &ps->postfix, list) {
1386 if (elt->op == OP_NONE)
1394 struct check_pred_data {
1399 static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1400 int *err, void *data)
1402 struct check_pred_data *d = data;
1404 if (WARN_ON(d->count++ > d->max)) {
1406 return WALK_PRED_ABORT;
1408 return WALK_PRED_DEFAULT;
1412 * The tree is walked at filtering of an event. If the tree is not correctly
1413 * built, it may cause an infinite loop. Check here that the tree does
1416 static int check_pred_tree(struct event_filter *filter,
1417 struct filter_pred *root)
1419 struct check_pred_data data = {
1421 * The max that we can hit a node is three times.
1422 * Once going down, once coming up from left, and
1423 * once coming up from right. This is more than enough
1424 * since leafs are only hit a single time.
1426 .max = 3 * filter->n_preds,
1430 return walk_pred_tree(filter->preds, root,
1431 check_pred_tree_cb, &data);
1434 static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
1435 int *err, void *data)
1439 if ((move == MOVE_DOWN) &&
1440 (pred->left == FILTER_PRED_INVALID))
1443 return WALK_PRED_DEFAULT;
1446 static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1450 ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
1455 struct fold_pred_data {
1456 struct filter_pred *root;
1461 static int fold_pred_cb(enum move_type move, struct filter_pred *pred,
1462 int *err, void *data)
1464 struct fold_pred_data *d = data;
1465 struct filter_pred *root = d->root;
1467 if (move != MOVE_DOWN)
1468 return WALK_PRED_DEFAULT;
1469 if (pred->left != FILTER_PRED_INVALID)
1470 return WALK_PRED_DEFAULT;
1472 if (WARN_ON(d->count == d->children)) {
1474 return WALK_PRED_ABORT;
1477 pred->index &= ~FILTER_PRED_FOLD;
1478 root->ops[d->count++] = pred->index;
1479 return WALK_PRED_DEFAULT;
1482 static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1484 struct fold_pred_data data = {
1490 /* No need to keep the fold flag */
1491 root->index &= ~FILTER_PRED_FOLD;
1493 /* If the root is a leaf then do nothing */
1494 if (root->left == FILTER_PRED_INVALID)
1497 /* count the children */
1498 children = count_leafs(preds, &preds[root->left]);
1499 children += count_leafs(preds, &preds[root->right]);
1501 root->ops = kzalloc(sizeof(*root->ops) * children, GFP_KERNEL);
1505 root->val = children;
1506 data.children = children;
1507 return walk_pred_tree(preds, root, fold_pred_cb, &data);
1510 static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1511 int *err, void *data)
1513 struct filter_pred *preds = data;
1515 if (move != MOVE_DOWN)
1516 return WALK_PRED_DEFAULT;
1517 if (!(pred->index & FILTER_PRED_FOLD))
1518 return WALK_PRED_DEFAULT;
1520 *err = fold_pred(preds, pred);
1522 return WALK_PRED_ABORT;
1524 /* eveyrhing below is folded, continue with parent */
1525 return WALK_PRED_PARENT;
1529 * To optimize the processing of the ops, if we have several "ors" or
1530 * "ands" together, we can put them in an array and process them all
1531 * together speeding up the filter logic.
1533 static int fold_pred_tree(struct event_filter *filter,
1534 struct filter_pred *root)
1536 return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
1540 static int replace_preds(struct ftrace_event_call *call,
1541 struct event_filter *filter,
1542 struct filter_parse_state *ps,
1543 char *filter_string,
1546 char *operand1 = NULL, *operand2 = NULL;
1547 struct filter_pred *pred;
1548 struct filter_pred *root;
1549 struct postfix_elt *elt;
1550 struct pred_stack stack = { }; /* init to NULL */
1554 n_preds = count_preds(ps);
1555 if (n_preds >= MAX_FILTER_PRED) {
1556 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1560 err = check_preds(ps);
1565 err = __alloc_pred_stack(&stack, n_preds);
1568 err = __alloc_preds(filter, n_preds);
1574 list_for_each_entry(elt, &ps->postfix, list) {
1575 if (elt->op == OP_NONE) {
1577 operand1 = elt->operand;
1579 operand2 = elt->operand;
1581 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1588 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1589 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1594 pred = create_pred(ps, call, elt->op, operand1, operand2);
1601 err = filter_add_pred(ps, filter, pred, &stack);
1606 operand1 = operand2 = NULL;
1610 /* We should have one item left on the stack */
1611 pred = __pop_pred_stack(&stack);
1614 /* This item is where we start from in matching */
1616 /* Make sure the stack is empty */
1617 pred = __pop_pred_stack(&stack);
1618 if (WARN_ON(pred)) {
1620 filter->root = NULL;
1623 err = check_pred_tree(filter, root);
1627 /* Optimize the tree */
1628 err = fold_pred_tree(filter, root);
1632 /* We don't set root until we know it works */
1634 filter->root = root;
1639 __free_pred_stack(&stack);
1643 struct filter_list {
1644 struct list_head list;
1645 struct event_filter *filter;
1648 static int replace_system_preds(struct event_subsystem *system,
1649 struct filter_parse_state *ps,
1650 char *filter_string)
1652 struct ftrace_event_call *call;
1653 struct filter_list *filter_item;
1654 struct filter_list *tmp;
1655 LIST_HEAD(filter_list);
1659 list_for_each_entry(call, &ftrace_events, list) {
1661 if (strcmp(call->class->system, system->name) != 0)
1665 * Try to see if the filter can be applied
1666 * (filter arg is ignored on dry_run)
1668 err = replace_preds(call, NULL, ps, filter_string, true);
1670 call->flags |= TRACE_EVENT_FL_NO_SET_FILTER;
1672 call->flags &= ~TRACE_EVENT_FL_NO_SET_FILTER;
1675 list_for_each_entry(call, &ftrace_events, list) {
1676 struct event_filter *filter;
1678 if (strcmp(call->class->system, system->name) != 0)
1681 if (call->flags & TRACE_EVENT_FL_NO_SET_FILTER)
1684 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1688 list_add_tail(&filter_item->list, &filter_list);
1690 filter_item->filter = __alloc_filter();
1691 if (!filter_item->filter)
1693 filter = filter_item->filter;
1695 /* Can only fail on no memory */
1696 err = replace_filter_string(filter, filter_string);
1700 err = replace_preds(call, filter, ps, filter_string, false);
1702 filter_disable(call);
1703 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1704 append_filter_err(ps, filter);
1706 call->flags |= TRACE_EVENT_FL_FILTERED;
1708 * Regardless of if this returned an error, we still
1709 * replace the filter for the call.
1711 filter = call->filter;
1712 rcu_assign_pointer(call->filter, filter_item->filter);
1713 filter_item->filter = filter;
1722 * The calls can still be using the old filters.
1723 * Do a synchronize_sched() to ensure all calls are
1724 * done with them before we free them.
1726 synchronize_sched();
1727 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1728 __free_filter(filter_item->filter);
1729 list_del(&filter_item->list);
1734 /* No call succeeded */
1735 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1736 list_del(&filter_item->list);
1739 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1742 /* If any call succeeded, we still need to sync */
1744 synchronize_sched();
1745 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1746 __free_filter(filter_item->filter);
1747 list_del(&filter_item->list);
1753 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1755 struct filter_parse_state *ps;
1756 struct event_filter *filter;
1757 struct event_filter *tmp;
1760 mutex_lock(&event_mutex);
1762 if (!strcmp(strstrip(filter_string), "0")) {
1763 filter_disable(call);
1764 filter = call->filter;
1767 RCU_INIT_POINTER(call->filter, NULL);
1768 /* Make sure the filter is not being used */
1769 synchronize_sched();
1770 __free_filter(filter);
1775 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1779 filter = __alloc_filter();
1785 replace_filter_string(filter, filter_string);
1787 parse_init(ps, filter_ops, filter_string);
1788 err = filter_parse(ps);
1790 append_filter_err(ps, filter);
1794 err = replace_preds(call, filter, ps, filter_string, false);
1796 filter_disable(call);
1797 append_filter_err(ps, filter);
1799 call->flags |= TRACE_EVENT_FL_FILTERED;
1802 * Always swap the call filter with the new filter
1803 * even if there was an error. If there was an error
1804 * in the filter, we disable the filter and show the error
1808 rcu_assign_pointer(call->filter, filter);
1810 /* Make sure the call is done with the filter */
1811 synchronize_sched();
1814 filter_opstack_clear(ps);
1818 mutex_unlock(&event_mutex);
1823 int apply_subsystem_event_filter(struct event_subsystem *system,
1824 char *filter_string)
1826 struct filter_parse_state *ps;
1827 struct event_filter *filter;
1830 mutex_lock(&event_mutex);
1832 /* Make sure the system still has events */
1833 if (!system->nr_events) {
1838 if (!strcmp(strstrip(filter_string), "0")) {
1839 filter_free_subsystem_preds(system);
1840 remove_filter_string(system->filter);
1841 filter = system->filter;
1842 system->filter = NULL;
1843 /* Ensure all filters are no longer used */
1844 synchronize_sched();
1845 filter_free_subsystem_filters(system);
1846 __free_filter(filter);
1851 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1855 filter = __alloc_filter();
1859 replace_filter_string(filter, filter_string);
1861 * No event actually uses the system filter
1862 * we can free it without synchronize_sched().
1864 __free_filter(system->filter);
1865 system->filter = filter;
1867 parse_init(ps, filter_ops, filter_string);
1868 err = filter_parse(ps);
1870 append_filter_err(ps, system->filter);
1874 err = replace_system_preds(system, ps, filter_string);
1876 append_filter_err(ps, system->filter);
1879 filter_opstack_clear(ps);
1883 mutex_unlock(&event_mutex);
1888 #ifdef CONFIG_PERF_EVENTS
1890 void ftrace_profile_free_filter(struct perf_event *event)
1892 struct event_filter *filter = event->filter;
1894 event->filter = NULL;
1895 __free_filter(filter);
1898 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1902 struct event_filter *filter;
1903 struct filter_parse_state *ps;
1904 struct ftrace_event_call *call;
1906 mutex_lock(&event_mutex);
1908 call = event->tp_event;
1918 filter = __alloc_filter();
1920 err = PTR_ERR(filter);
1925 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1929 parse_init(ps, filter_ops, filter_str);
1930 err = filter_parse(ps);
1934 err = replace_preds(call, filter, ps, filter_str, false);
1936 event->filter = filter;
1939 filter_opstack_clear(ps);
1945 __free_filter(filter);
1948 mutex_unlock(&event_mutex);
1953 #endif /* CONFIG_PERF_EVENTS */
1955 #ifdef CONFIG_FTRACE_STARTUP_TEST
1957 #include <linux/types.h>
1958 #include <linux/tracepoint.h>
1960 #define CREATE_TRACE_POINTS
1961 #include "trace_events_filter_test.h"
1963 static int test_get_filter(char *filter_str, struct ftrace_event_call *call,
1964 struct event_filter **pfilter)
1966 struct event_filter *filter;
1967 struct filter_parse_state *ps;
1970 filter = __alloc_filter();
1974 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1978 parse_init(ps, filter_ops, filter_str);
1979 err = filter_parse(ps);
1983 err = replace_preds(call, filter, ps, filter_str, false);
1988 filter_opstack_clear(ps);
1994 __free_filter(filter);
2000 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2003 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2004 .e = ve, .f = vf, .g = vg, .h = vh }, \
2006 .not_visited = nvisit, \
2011 static struct test_filter_data_t {
2013 struct ftrace_raw_ftrace_test_filter rec;
2016 } test_filter_data[] = {
2017 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2018 "e == 1 && f == 1 && g == 1 && h == 1"
2019 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2020 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2021 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2023 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2024 "e == 1 || f == 1 || g == 1 || h == 1"
2025 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2026 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2027 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2029 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2030 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2031 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2032 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2033 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2034 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2036 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2037 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2038 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2039 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2040 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2042 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2043 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2044 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2045 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2046 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2048 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2049 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2050 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2051 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2052 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2054 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2055 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2056 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2057 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2058 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2060 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2061 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2062 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2063 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2064 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2072 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2074 static int test_pred_visited;
2076 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2078 struct ftrace_event_field *field = pred->field;
2080 test_pred_visited = 1;
2081 printk(KERN_INFO "\npred visited %s\n", field->name);
2085 static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred,
2086 int *err, void *data)
2088 char *fields = data;
2090 if ((move == MOVE_DOWN) &&
2091 (pred->left == FILTER_PRED_INVALID)) {
2092 struct ftrace_event_field *field = pred->field;
2095 WARN(1, "all leafs should have field defined");
2096 return WALK_PRED_DEFAULT;
2098 if (!strchr(fields, *field->name))
2099 return WALK_PRED_DEFAULT;
2102 pred->fn = test_pred_visited_fn;
2104 return WALK_PRED_DEFAULT;
2107 static __init int ftrace_test_event_filter(void)
2111 printk(KERN_INFO "Testing ftrace filter: ");
2113 for (i = 0; i < DATA_CNT; i++) {
2114 struct event_filter *filter = NULL;
2115 struct test_filter_data_t *d = &test_filter_data[i];
2118 err = test_get_filter(d->filter, &event_ftrace_test_filter,
2122 "Failed to get filter for '%s', err %d\n",
2128 * The preemption disabling is not really needed for self
2129 * tests, but the rcu dereference will complain without it.
2132 if (*d->not_visited)
2133 walk_pred_tree(filter->preds, filter->root,
2137 test_pred_visited = 0;
2138 err = filter_match_preds(filter, &d->rec);
2141 __free_filter(filter);
2143 if (test_pred_visited) {
2145 "Failed, unwanted pred visited for filter %s\n",
2150 if (err != d->match) {
2152 "Failed to match filter '%s', expected %d\n",
2153 d->filter, d->match);
2159 printk(KERN_CONT "OK\n");
2164 late_initcall(ftrace_test_event_filter);
2166 #endif /* CONFIG_FTRACE_STARTUP_TEST */