tracing/filter: Swap entire filter of events
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / trace / trace_events_filter.c
CommitLineData
7ce7e424
TZ
1/*
2 * trace_events_filter - generic event filtering
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
19 */
20
7ce7e424
TZ
21#include <linux/module.h>
22#include <linux/ctype.h>
ac1adc55 23#include <linux/mutex.h>
6fb2915d 24#include <linux/perf_event.h>
5a0e3ad6 25#include <linux/slab.h>
7ce7e424
TZ
26
27#include "trace.h"
4bda2d51 28#include "trace_output.h"
7ce7e424 29
8b372562 30enum filter_op_ids
7ce7e424 31{
8b372562
TZ
32 OP_OR,
33 OP_AND,
b0f1a59a 34 OP_GLOB,
8b372562
TZ
35 OP_NE,
36 OP_EQ,
37 OP_LT,
38 OP_LE,
39 OP_GT,
40 OP_GE,
41 OP_NONE,
42 OP_OPEN_PAREN,
43};
44
45struct filter_op {
46 int id;
47 char *string;
48 int precedence;
49};
50
51static struct filter_op filter_ops[] = {
b0f1a59a
LZ
52 { OP_OR, "||", 1 },
53 { OP_AND, "&&", 2 },
54 { OP_GLOB, "~", 4 },
55 { OP_NE, "!=", 4 },
56 { OP_EQ, "==", 4 },
57 { OP_LT, "<", 5 },
58 { OP_LE, "<=", 5 },
59 { OP_GT, ">", 5 },
60 { OP_GE, ">=", 5 },
61 { OP_NONE, "OP_NONE", 0 },
62 { OP_OPEN_PAREN, "(", 0 },
8b372562
TZ
63};
64
65enum {
66 FILT_ERR_NONE,
67 FILT_ERR_INVALID_OP,
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,
78};
79
80static char *err_text[] = {
81 "No error",
82 "Invalid operator",
83 "Unbalanced parens",
84 "Too many operands",
85 "Operand too long",
86 "Field not found",
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",
93};
94
95struct opstack_op {
96 int op;
97 struct list_head list;
98};
99
100struct postfix_elt {
101 int op;
102 char *operand;
103 struct list_head list;
104};
105
106struct filter_parse_state {
107 struct filter_op *ops;
108 struct list_head opstack;
109 struct list_head postfix;
110 int lasterr;
111 int lasterr_pos;
112
113 struct {
114 char *string;
115 unsigned int cnt;
116 unsigned int tail;
117 } infix;
118
119 struct {
120 char string[MAX_FILTER_STR_VAL];
121 int pos;
122 unsigned int tail;
123 } operand;
124};
125
61e9dea2
SR
126struct pred_stack {
127 struct filter_pred **preds;
128 int index;
129};
130
197e2eab 131#define DEFINE_COMPARISON_PRED(type) \
58d9a597 132static int filter_pred_##type(struct filter_pred *pred, void *event) \
197e2eab
LZ
133{ \
134 type *addr = (type *)(event + pred->offset); \
135 type val = (type)pred->val; \
136 int match = 0; \
137 \
138 switch (pred->op) { \
139 case OP_LT: \
140 match = (*addr < val); \
141 break; \
142 case OP_LE: \
143 match = (*addr <= val); \
144 break; \
145 case OP_GT: \
146 match = (*addr > val); \
147 break; \
148 case OP_GE: \
149 match = (*addr >= val); \
150 break; \
151 default: \
152 break; \
153 } \
154 \
155 return match; \
156}
157
158#define DEFINE_EQUALITY_PRED(size) \
58d9a597 159static int filter_pred_##size(struct filter_pred *pred, void *event) \
197e2eab
LZ
160{ \
161 u##size *addr = (u##size *)(event + pred->offset); \
162 u##size val = (u##size)pred->val; \
163 int match; \
164 \
165 match = (val == *addr) ^ pred->not; \
166 \
167 return match; \
168}
169
8b372562
TZ
170DEFINE_COMPARISON_PRED(s64);
171DEFINE_COMPARISON_PRED(u64);
172DEFINE_COMPARISON_PRED(s32);
173DEFINE_COMPARISON_PRED(u32);
174DEFINE_COMPARISON_PRED(s16);
175DEFINE_COMPARISON_PRED(u16);
176DEFINE_COMPARISON_PRED(s8);
177DEFINE_COMPARISON_PRED(u8);
178
179DEFINE_EQUALITY_PRED(64);
180DEFINE_EQUALITY_PRED(32);
181DEFINE_EQUALITY_PRED(16);
182DEFINE_EQUALITY_PRED(8);
183
e8808c10 184/* Filter predicate for fixed sized arrays of characters */
58d9a597 185static int filter_pred_string(struct filter_pred *pred, void *event)
7ce7e424
TZ
186{
187 char *addr = (char *)(event + pred->offset);
188 int cmp, match;
189
1889d209 190 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
7ce7e424 191
1889d209 192 match = cmp ^ pred->not;
7ce7e424
TZ
193
194 return match;
195}
196
87a342f5 197/* Filter predicate for char * pointers */
58d9a597 198static int filter_pred_pchar(struct filter_pred *pred, void *event)
87a342f5
LZ
199{
200 char **addr = (char **)(event + pred->offset);
201 int cmp, match;
16da27a8 202 int len = strlen(*addr) + 1; /* including tailing '\0' */
87a342f5 203
16da27a8 204 cmp = pred->regex.match(*addr, &pred->regex, len);
87a342f5 205
1889d209 206 match = cmp ^ pred->not;
87a342f5
LZ
207
208 return match;
209}
210
e8808c10
FW
211/*
212 * Filter predicate for dynamic sized arrays of characters.
213 * These are implemented through a list of strings at the end
214 * of the entry.
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.
220 */
58d9a597 221static int filter_pred_strloc(struct filter_pred *pred, void *event)
e8808c10 222{
7d536cb3
LZ
223 u32 str_item = *(u32 *)(event + pred->offset);
224 int str_loc = str_item & 0xffff;
225 int str_len = str_item >> 16;
e8808c10
FW
226 char *addr = (char *)(event + str_loc);
227 int cmp, match;
228
1889d209 229 cmp = pred->regex.match(addr, &pred->regex, str_len);
e8808c10 230
1889d209 231 match = cmp ^ pred->not;
e8808c10
FW
232
233 return match;
234}
235
58d9a597 236static int filter_pred_none(struct filter_pred *pred, void *event)
0a19e53c
TZ
237{
238 return 0;
239}
240
d1303dd1
LZ
241/*
242 * regex_match_foo - Basic regex callbacks
243 *
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')
247 *
248 * Note:
249 * - @str might not be NULL-terminated if it's of type DYN_STRING
250 * or STATIC_STRING
251 */
252
1889d209
FW
253static int regex_match_full(char *str, struct regex *r, int len)
254{
255 if (strncmp(str, r->pattern, len) == 0)
256 return 1;
257 return 0;
258}
259
260static int regex_match_front(char *str, struct regex *r, int len)
261{
285caad4 262 if (strncmp(str, r->pattern, r->len) == 0)
1889d209
FW
263 return 1;
264 return 0;
265}
266
267static int regex_match_middle(char *str, struct regex *r, int len)
268{
b2af211f 269 if (strnstr(str, r->pattern, len))
1889d209
FW
270 return 1;
271 return 0;
272}
273
274static int regex_match_end(char *str, struct regex *r, int len)
275{
a3291c14 276 int strlen = len - 1;
1889d209 277
a3291c14
LZ
278 if (strlen >= r->len &&
279 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1889d209
FW
280 return 1;
281 return 0;
282}
283
3f6fe06d
FW
284/**
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
290 *
291 * This passes in a buffer containing a regex and this function will
1889d209
FW
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.
295 *
296 * Returns enum type.
297 * search returns the pointer to use for comparison.
298 * not returns 1 if buff started with a '!'
299 * 0 otherwise.
300 */
3f6fe06d 301enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1889d209
FW
302{
303 int type = MATCH_FULL;
304 int i;
305
306 if (buff[0] == '!') {
307 *not = 1;
308 buff++;
309 len--;
310 } else
311 *not = 0;
312
313 *search = buff;
314
315 for (i = 0; i < len; i++) {
316 if (buff[i] == '*') {
317 if (!i) {
318 *search = buff + 1;
319 type = MATCH_END_ONLY;
320 } else {
321 if (type == MATCH_END_ONLY)
322 type = MATCH_MIDDLE_ONLY;
323 else
324 type = MATCH_FRONT_ONLY;
325 buff[i] = 0;
326 break;
327 }
328 }
329 }
330
331 return type;
332}
333
b0f1a59a 334static void filter_build_regex(struct filter_pred *pred)
1889d209
FW
335{
336 struct regex *r = &pred->regex;
b0f1a59a
LZ
337 char *search;
338 enum regex_type type = MATCH_FULL;
339 int not = 0;
340
341 if (pred->op == OP_GLOB) {
342 type = filter_parse_regex(r->pattern, r->len, &search, &not);
343 r->len = strlen(search);
344 memmove(r->pattern, search, r->len+1);
345 }
1889d209
FW
346
347 switch (type) {
348 case MATCH_FULL:
349 r->match = regex_match_full;
350 break;
351 case MATCH_FRONT_ONLY:
352 r->match = regex_match_front;
353 break;
354 case MATCH_MIDDLE_ONLY:
355 r->match = regex_match_middle;
356 break;
357 case MATCH_END_ONLY:
358 r->match = regex_match_end;
359 break;
360 }
361
362 pred->not ^= not;
1889d209
FW
363}
364
61e9dea2
SR
365enum move_type {
366 MOVE_DOWN,
367 MOVE_UP_FROM_LEFT,
368 MOVE_UP_FROM_RIGHT
369};
370
371static struct filter_pred *
372get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
373 int index, enum move_type *move)
374{
375 if (pred->parent & FILTER_PRED_IS_RIGHT)
376 *move = MOVE_UP_FROM_RIGHT;
377 else
378 *move = MOVE_UP_FROM_LEFT;
379 pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
380
381 return pred;
382}
383
43cd4145
SR
384/*
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.
389 */
390static int process_ops(struct filter_pred *preds,
391 struct filter_pred *op, void *rec)
392{
393 struct filter_pred *pred;
394 int type;
395 int match;
396 int i;
397
398 /*
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:
404 *
405 * if ((match && op->op == OP_OR) ||
406 * (!match && op->op == OP_AND))
407 * return match;
408 */
409 type = op->op == OP_OR;
410
411 for (i = 0; i < op->val; i++) {
412 pred = &preds[op->ops[i]];
413 match = pred->fn(pred, rec);
414 if (!!match == type)
415 return match;
416 }
417 return match;
418}
419
7ce7e424 420/* return 1 if event matches, 0 otherwise (discard) */
6fb2915d 421int filter_match_preds(struct event_filter *filter, void *rec)
7ce7e424 422{
61e9dea2
SR
423 int match = -1;
424 enum move_type move = MOVE_DOWN;
74e9e58c 425 struct filter_pred *preds;
7ce7e424 426 struct filter_pred *pred;
61e9dea2 427 struct filter_pred *root;
75b8e982 428 int n_preds;
61e9dea2 429 int done = 0;
7ce7e424 430
6d54057d 431 /* no filter is considered a match */
75b8e982
SR
432 if (!filter)
433 return 1;
434
435 n_preds = filter->n_preds;
436
6d54057d
SR
437 if (!n_preds)
438 return 1;
439
c9c53ca0 440 /*
61e9dea2 441 * n_preds, root and filter->preds are protect with preemption disabled.
c9c53ca0
SR
442 */
443 preds = rcu_dereference_sched(filter->preds);
61e9dea2
SR
444 root = rcu_dereference_sched(filter->root);
445 if (!root)
446 return 1;
c9c53ca0 447
61e9dea2
SR
448 pred = root;
449
450 /* match is currently meaningless */
451 match = -1;
452
453 do {
454 switch (move) {
455 case MOVE_DOWN:
456 /* only AND and OR have children */
457 if (pred->left != FILTER_PRED_INVALID) {
43cd4145
SR
458 /* If ops is set, then it was folded. */
459 if (!pred->ops) {
460 /* keep going to down the left side */
461 pred = &preds[pred->left];
462 continue;
463 }
464 /* We can treat folded ops as a leaf node */
465 match = process_ops(preds, pred, rec);
466 } else
467 match = pred->fn(pred, rec);
61e9dea2
SR
468 /* If this pred is the only pred */
469 if (pred == root)
470 break;
471 pred = get_pred_parent(pred, preds,
472 pred->parent, &move);
473 continue;
474 case MOVE_UP_FROM_LEFT:
55719274
SR
475 /*
476 * Check for short circuits.
477 *
478 * Optimization: !!match == (pred->op == OP_OR)
479 * is the same as:
480 * if ((match && pred->op == OP_OR) ||
481 * (!match && pred->op == OP_AND))
482 */
483 if (!!match == (pred->op == OP_OR)) {
61e9dea2
SR
484 if (pred == root)
485 break;
486 pred = get_pred_parent(pred, preds,
487 pred->parent, &move);
488 continue;
489 }
490 /* now go down the right side of the tree. */
491 pred = &preds[pred->right];
492 move = MOVE_DOWN;
493 continue;
494 case MOVE_UP_FROM_RIGHT:
495 /* We finished this equation. */
496 if (pred == root)
497 break;
498 pred = get_pred_parent(pred, preds,
499 pred->parent, &move);
0a19e53c 500 continue;
8b372562 501 }
61e9dea2
SR
502 done = 1;
503 } while (!done);
7ce7e424 504
61e9dea2 505 return match;
7ce7e424 506}
17c873ec 507EXPORT_SYMBOL_GPL(filter_match_preds);
7ce7e424 508
8b372562 509static void parse_error(struct filter_parse_state *ps, int err, int pos)
7ce7e424 510{
8b372562
TZ
511 ps->lasterr = err;
512 ps->lasterr_pos = pos;
513}
7ce7e424 514
8b372562
TZ
515static void remove_filter_string(struct event_filter *filter)
516{
75b8e982
SR
517 if (!filter)
518 return;
519
8b372562
TZ
520 kfree(filter->filter_string);
521 filter->filter_string = NULL;
522}
523
524static int replace_filter_string(struct event_filter *filter,
525 char *filter_string)
526{
527 kfree(filter->filter_string);
528 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
529 if (!filter->filter_string)
530 return -ENOMEM;
531
532 return 0;
533}
534
535static int append_filter_string(struct event_filter *filter,
536 char *string)
537{
538 int newlen;
539 char *new_filter_string;
540
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)
545 return -ENOMEM;
546
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;
551
552 return 0;
553}
554
555static void append_filter_err(struct filter_parse_state *ps,
556 struct event_filter *filter)
557{
558 int pos = ps->lasterr_pos;
559 char *buf, *pbuf;
560
561 buf = (char *)__get_free_page(GFP_TEMPORARY);
562 if (!buf)
4bda2d51 563 return;
7ce7e424 564
8b372562
TZ
565 append_filter_string(filter, "\n");
566 memset(buf, ' ', PAGE_SIZE);
567 if (pos > PAGE_SIZE - 128)
568 pos = 0;
569 buf[pos] = '^';
570 pbuf = &buf[pos] + 1;
571
572 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
573 append_filter_string(filter, buf);
574 free_page((unsigned long) buf);
7ce7e424
TZ
575}
576
8b372562 577void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
ac1adc55 578{
75b8e982 579 struct event_filter *filter;
8b372562 580
00e95830 581 mutex_lock(&event_mutex);
75b8e982 582 filter = call->filter;
8e254c1d 583 if (filter && filter->filter_string)
8b372562
TZ
584 trace_seq_printf(s, "%s\n", filter->filter_string);
585 else
586 trace_seq_printf(s, "none\n");
00e95830 587 mutex_unlock(&event_mutex);
ac1adc55
TZ
588}
589
8b372562 590void print_subsystem_event_filter(struct event_subsystem *system,
ac1adc55
TZ
591 struct trace_seq *s)
592{
75b8e982 593 struct event_filter *filter;
8b372562 594
00e95830 595 mutex_lock(&event_mutex);
75b8e982 596 filter = system->filter;
8e254c1d 597 if (filter && filter->filter_string)
8b372562
TZ
598 trace_seq_printf(s, "%s\n", filter->filter_string);
599 else
600 trace_seq_printf(s, "none\n");
00e95830 601 mutex_unlock(&event_mutex);
ac1adc55
TZ
602}
603
7ce7e424 604static struct ftrace_event_field *
8728fe50 605__find_event_field(struct list_head *head, char *name)
7ce7e424 606{
1fc2d5c1 607 struct ftrace_event_field *field;
7ce7e424 608
2e33af02 609 list_for_each_entry(field, head, link) {
7ce7e424
TZ
610 if (!strcmp(field->name, name))
611 return field;
612 }
613
614 return NULL;
615}
616
8728fe50
LZ
617static struct ftrace_event_field *
618find_event_field(struct ftrace_event_call *call, char *name)
619{
620 struct ftrace_event_field *field;
621 struct list_head *head;
622
623 field = __find_event_field(&ftrace_common_fields, name);
624 if (field)
625 return field;
626
627 head = trace_get_fields(call);
628 return __find_event_field(head, name);
629}
630
8b372562 631static void filter_free_pred(struct filter_pred *pred)
7ce7e424
TZ
632{
633 if (!pred)
634 return;
635
636 kfree(pred->field_name);
7ce7e424
TZ
637 kfree(pred);
638}
639
0a19e53c
TZ
640static void filter_clear_pred(struct filter_pred *pred)
641{
642 kfree(pred->field_name);
643 pred->field_name = NULL;
1889d209 644 pred->regex.len = 0;
0a19e53c
TZ
645}
646
61e9dea2
SR
647static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
648{
649 stack->preds = kzalloc(sizeof(*stack->preds)*(n_preds + 1), GFP_KERNEL);
650 if (!stack->preds)
651 return -ENOMEM;
652 stack->index = n_preds;
653 return 0;
654}
655
656static void __free_pred_stack(struct pred_stack *stack)
657{
658 kfree(stack->preds);
659 stack->index = 0;
660}
661
662static int __push_pred_stack(struct pred_stack *stack,
663 struct filter_pred *pred)
664{
665 int index = stack->index;
666
667 if (WARN_ON(index == 0))
668 return -ENOSPC;
669
670 stack->preds[--index] = pred;
671 stack->index = index;
672 return 0;
673}
674
675static struct filter_pred *
676__pop_pred_stack(struct pred_stack *stack)
677{
678 struct filter_pred *pred;
679 int index = stack->index;
680
681 pred = stack->preds[index++];
682 if (!pred)
683 return NULL;
684
685 stack->index = index;
686 return pred;
687}
688
689static int filter_set_pred(struct event_filter *filter,
690 int idx,
691 struct pred_stack *stack,
0a19e53c
TZ
692 struct filter_pred *src,
693 filter_pred_fn_t fn)
694{
61e9dea2
SR
695 struct filter_pred *dest = &filter->preds[idx];
696 struct filter_pred *left;
697 struct filter_pred *right;
698
0a19e53c 699 *dest = *src;
8b372562
TZ
700 if (src->field_name) {
701 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
702 if (!dest->field_name)
703 return -ENOMEM;
704 }
0a19e53c 705 dest->fn = fn;
61e9dea2 706 dest->index = idx;
0a19e53c 707
61e9dea2
SR
708 if (dest->op == OP_OR || dest->op == OP_AND) {
709 right = __pop_pred_stack(stack);
710 left = __pop_pred_stack(stack);
711 if (!left || !right)
712 return -EINVAL;
43cd4145
SR
713 /*
714 * If both children can be folded
715 * and they are the same op as this op or a leaf,
716 * then this op can be folded.
717 */
718 if (left->index & FILTER_PRED_FOLD &&
719 (left->op == dest->op ||
720 left->left == FILTER_PRED_INVALID) &&
721 right->index & FILTER_PRED_FOLD &&
722 (right->op == dest->op ||
723 right->left == FILTER_PRED_INVALID))
724 dest->index |= FILTER_PRED_FOLD;
725
726 dest->left = left->index & ~FILTER_PRED_FOLD;
727 dest->right = right->index & ~FILTER_PRED_FOLD;
728 left->parent = dest->index & ~FILTER_PRED_FOLD;
61e9dea2 729 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
43cd4145 730 } else {
61e9dea2
SR
731 /*
732 * Make dest->left invalid to be used as a quick
733 * way to know this is a leaf node.
734 */
735 dest->left = FILTER_PRED_INVALID;
736
43cd4145
SR
737 /* All leafs allow folding the parent ops. */
738 dest->index |= FILTER_PRED_FOLD;
739 }
740
61e9dea2 741 return __push_pred_stack(stack, dest);
0a19e53c
TZ
742}
743
c9c53ca0
SR
744static void __free_preds(struct event_filter *filter)
745{
746 int i;
747
748 if (filter->preds) {
74e9e58c
SR
749 for (i = 0; i < filter->a_preds; i++)
750 kfree(filter->preds[i].field_name);
c9c53ca0
SR
751 kfree(filter->preds);
752 filter->preds = NULL;
753 }
754 filter->a_preds = 0;
755 filter->n_preds = 0;
756}
757
75b8e982 758static void filter_disable(struct ftrace_event_call *call)
7ce7e424 759{
553552ce 760 call->flags &= ~TRACE_EVENT_FL_FILTERED;
0a19e53c
TZ
761}
762
c9c53ca0 763static void __free_filter(struct event_filter *filter)
2df75e41 764{
8e254c1d
LZ
765 if (!filter)
766 return;
767
c9c53ca0 768 __free_preds(filter);
57be8887 769 kfree(filter->filter_string);
2df75e41 770 kfree(filter);
6fb2915d
LZ
771}
772
75b8e982
SR
773/*
774 * Called when destroying the ftrace_event_call.
775 * The call is being freed, so we do not need to worry about
776 * the call being currently used. This is for module code removing
777 * the tracepoints from within it.
778 */
6fb2915d
LZ
779void destroy_preds(struct ftrace_event_call *call)
780{
c9c53ca0 781 __free_filter(call->filter);
2df75e41
LZ
782 call->filter = NULL;
783}
784
c9c53ca0 785static struct event_filter *__alloc_filter(void)
0a19e53c 786{
30e673b2 787 struct event_filter *filter;
0a19e53c 788
6fb2915d 789 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
c9c53ca0
SR
790 return filter;
791}
792
793static int __alloc_preds(struct event_filter *filter, int n_preds)
794{
795 struct filter_pred *pred;
796 int i;
797
798 if (filter->preds) {
799 if (filter->a_preds < n_preds) {
c9c53ca0 800 /*
0fc3ca9a
SR
801 * We need to reallocate.
802 * We should have already have zeroed out
803 * the pred count and called synchronized_sched()
804 * to make sure no one is using the preds.
c9c53ca0 805 */
0fc3ca9a
SR
806 if (WARN_ON_ONCE(filter->n_preds)) {
807 /* We need to reset it now */
808 filter->n_preds = 0;
809 synchronize_sched();
810 }
c9c53ca0
SR
811 __free_preds(filter);
812 }
813 }
814
815 if (!filter->preds) {
816 filter->preds =
817 kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
818 filter->a_preds = n_preds;
819 }
30e673b2 820 if (!filter->preds)
c9c53ca0
SR
821 return -ENOMEM;
822
823 if (WARN_ON(filter->a_preds < n_preds))
824 return -EINVAL;
30e673b2 825
c9c53ca0 826 for (i = 0; i < n_preds; i++) {
74e9e58c 827 pred = &filter->preds[i];
0a19e53c 828 pred->fn = filter_pred_none;
0a19e53c
TZ
829 }
830
c9c53ca0 831 return 0;
6fb2915d
LZ
832}
833
75b8e982 834static void filter_free_subsystem_preds(struct event_subsystem *system)
8e254c1d
LZ
835{
836 struct ftrace_event_call *call;
8e254c1d
LZ
837
838 list_for_each_entry(call, &ftrace_events, list) {
8f082018 839 if (strcmp(call->class->system, system->name) != 0)
8e254c1d
LZ
840 continue;
841
75b8e982
SR
842 filter_disable(call);
843 remove_filter_string(call->filter);
8e254c1d 844 }
8e254c1d 845}
7ce7e424 846
75b8e982 847static void filter_free_subsystem_filters(struct event_subsystem *system)
cfb180f3 848{
a59fd602 849 struct ftrace_event_call *call;
cfb180f3 850
a59fd602 851 list_for_each_entry(call, &ftrace_events, list) {
8f082018 852 if (strcmp(call->class->system, system->name) != 0)
8e254c1d 853 continue;
75b8e982
SR
854 __free_filter(call->filter);
855 call->filter = NULL;
cfb180f3
TZ
856 }
857}
858
8b372562
TZ
859static int filter_add_pred_fn(struct filter_parse_state *ps,
860 struct ftrace_event_call *call,
6fb2915d 861 struct event_filter *filter,
ac1adc55 862 struct filter_pred *pred,
61e9dea2 863 struct pred_stack *stack,
ac1adc55 864 filter_pred_fn_t fn)
7ce7e424 865{
0a19e53c 866 int idx, err;
7ce7e424 867
c9c53ca0 868 if (WARN_ON(filter->n_preds == filter->a_preds)) {
8b372562 869 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
0a19e53c 870 return -ENOSPC;
8b372562 871 }
7ce7e424 872
30e673b2 873 idx = filter->n_preds;
74e9e58c 874 filter_clear_pred(&filter->preds[idx]);
61e9dea2 875 err = filter_set_pred(filter, idx, stack, pred, fn);
0a19e53c
TZ
876 if (err)
877 return err;
878
30e673b2 879 filter->n_preds++;
7ce7e424 880
0a19e53c 881 return 0;
7ce7e424
TZ
882}
883
aa38e9fc 884int filter_assign_type(const char *type)
7ce7e424 885{
7fcb7c47
LZ
886 if (strstr(type, "__data_loc") && strstr(type, "char"))
887 return FILTER_DYN_STRING;
888
7ce7e424 889 if (strchr(type, '[') && strstr(type, "char"))
e8808c10
FW
890 return FILTER_STATIC_STRING;
891
aa38e9fc
LZ
892 return FILTER_OTHER;
893}
894
895static bool is_string_field(struct ftrace_event_field *field)
896{
897 return field->filter_type == FILTER_DYN_STRING ||
87a342f5
LZ
898 field->filter_type == FILTER_STATIC_STRING ||
899 field->filter_type == FILTER_PTR_STRING;
7ce7e424
TZ
900}
901
8b372562
TZ
902static int is_legal_op(struct ftrace_event_field *field, int op)
903{
b0f1a59a
LZ
904 if (is_string_field(field) &&
905 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
906 return 0;
907 if (!is_string_field(field) && op == OP_GLOB)
8b372562
TZ
908 return 0;
909
910 return 1;
911}
912
913static filter_pred_fn_t select_comparison_fn(int op, int field_size,
914 int field_is_signed)
915{
916 filter_pred_fn_t fn = NULL;
917
918 switch (field_size) {
919 case 8:
920 if (op == OP_EQ || op == OP_NE)
921 fn = filter_pred_64;
922 else if (field_is_signed)
923 fn = filter_pred_s64;
924 else
925 fn = filter_pred_u64;
926 break;
927 case 4:
928 if (op == OP_EQ || op == OP_NE)
929 fn = filter_pred_32;
930 else if (field_is_signed)
931 fn = filter_pred_s32;
932 else
933 fn = filter_pred_u32;
934 break;
935 case 2:
936 if (op == OP_EQ || op == OP_NE)
937 fn = filter_pred_16;
938 else if (field_is_signed)
939 fn = filter_pred_s16;
940 else
941 fn = filter_pred_u16;
942 break;
943 case 1:
944 if (op == OP_EQ || op == OP_NE)
945 fn = filter_pred_8;
946 else if (field_is_signed)
947 fn = filter_pred_s8;
948 else
949 fn = filter_pred_u8;
950 break;
951 }
952
953 return fn;
954}
955
956static int filter_add_pred(struct filter_parse_state *ps,
957 struct ftrace_event_call *call,
6fb2915d 958 struct event_filter *filter,
1f9963cb 959 struct filter_pred *pred,
61e9dea2 960 struct pred_stack *stack,
1f9963cb 961 bool dry_run)
7ce7e424
TZ
962{
963 struct ftrace_event_field *field;
0a19e53c 964 filter_pred_fn_t fn;
f66578a7 965 unsigned long long val;
5e4904cb 966 int ret;
7ce7e424 967
58d9a597 968 fn = pred->fn = filter_pred_none;
8b372562 969
61e9dea2 970 if (pred->op == OP_AND)
1f9963cb 971 goto add_pred_fn;
61e9dea2 972 else if (pred->op == OP_OR)
1f9963cb 973 goto add_pred_fn;
8b372562 974
7ce7e424 975 field = find_event_field(call, pred->field_name);
8b372562
TZ
976 if (!field) {
977 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
7ce7e424 978 return -EINVAL;
8b372562 979 }
7ce7e424
TZ
980
981 pred->offset = field->offset;
982
8b372562
TZ
983 if (!is_legal_op(field, pred->op)) {
984 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
985 return -EINVAL;
986 }
987
aa38e9fc 988 if (is_string_field(field)) {
b0f1a59a 989 filter_build_regex(pred);
87a342f5 990
1889d209 991 if (field->filter_type == FILTER_STATIC_STRING) {
e8808c10 992 fn = filter_pred_string;
1889d209
FW
993 pred->regex.field_len = field->size;
994 } else if (field->filter_type == FILTER_DYN_STRING)
b0f1a59a 995 fn = filter_pred_strloc;
16da27a8 996 else
87a342f5 997 fn = filter_pred_pchar;
9f58a159 998 } else {
5e4904cb 999 if (field->is_signed)
1889d209 1000 ret = strict_strtoll(pred->regex.pattern, 0, &val);
5e4904cb 1001 else
1889d209 1002 ret = strict_strtoull(pred->regex.pattern, 0, &val);
5e4904cb 1003 if (ret) {
8b372562 1004 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
9f58a159 1005 return -EINVAL;
8b372562 1006 }
f66578a7 1007 pred->val = val;
7ce7e424 1008
1f9963cb
LZ
1009 fn = select_comparison_fn(pred->op, field->size,
1010 field->is_signed);
1011 if (!fn) {
1012 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1013 return -EINVAL;
1014 }
7ce7e424
TZ
1015 }
1016
8b372562
TZ
1017 if (pred->op == OP_NE)
1018 pred->not = 1;
ac1adc55 1019
1f9963cb
LZ
1020add_pred_fn:
1021 if (!dry_run)
61e9dea2 1022 return filter_add_pred_fn(ps, call, filter, pred, stack, fn);
1f9963cb 1023 return 0;
cfb180f3
TZ
1024}
1025
8b372562
TZ
1026static void parse_init(struct filter_parse_state *ps,
1027 struct filter_op *ops,
1028 char *infix_string)
1029{
1030 memset(ps, '\0', sizeof(*ps));
1031
1032 ps->infix.string = infix_string;
1033 ps->infix.cnt = strlen(infix_string);
1034 ps->ops = ops;
1035
1036 INIT_LIST_HEAD(&ps->opstack);
1037 INIT_LIST_HEAD(&ps->postfix);
1038}
1039
1040static char infix_next(struct filter_parse_state *ps)
1041{
1042 ps->infix.cnt--;
1043
1044 return ps->infix.string[ps->infix.tail++];
1045}
1046
1047static char infix_peek(struct filter_parse_state *ps)
1048{
1049 if (ps->infix.tail == strlen(ps->infix.string))
1050 return 0;
1051
1052 return ps->infix.string[ps->infix.tail];
1053}
1054
1055static void infix_advance(struct filter_parse_state *ps)
1056{
1057 ps->infix.cnt--;
1058 ps->infix.tail++;
1059}
1060
1061static inline int is_precedence_lower(struct filter_parse_state *ps,
1062 int a, int b)
1063{
1064 return ps->ops[a].precedence < ps->ops[b].precedence;
1065}
1066
1067static inline int is_op_char(struct filter_parse_state *ps, char c)
1068{
1069 int i;
1070
1071 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1072 if (ps->ops[i].string[0] == c)
1073 return 1;
1074 }
c4cff064 1075
0a19e53c 1076 return 0;
cfb180f3
TZ
1077}
1078
8b372562
TZ
1079static int infix_get_op(struct filter_parse_state *ps, char firstc)
1080{
1081 char nextc = infix_peek(ps);
1082 char opstr[3];
1083 int i;
1084
1085 opstr[0] = firstc;
1086 opstr[1] = nextc;
1087 opstr[2] = '\0';
1088
1089 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1090 if (!strcmp(opstr, ps->ops[i].string)) {
1091 infix_advance(ps);
1092 return ps->ops[i].id;
7ce7e424 1093 }
8b372562
TZ
1094 }
1095
1096 opstr[1] = '\0';
1097
1098 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1099 if (!strcmp(opstr, ps->ops[i].string))
1100 return ps->ops[i].id;
1101 }
1102
1103 return OP_NONE;
1104}
1105
1106static inline void clear_operand_string(struct filter_parse_state *ps)
1107{
1108 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1109 ps->operand.tail = 0;
1110}
1111
1112static inline int append_operand_char(struct filter_parse_state *ps, char c)
1113{
5872144f 1114 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
8b372562
TZ
1115 return -EINVAL;
1116
1117 ps->operand.string[ps->operand.tail++] = c;
1118
1119 return 0;
1120}
1121
1122static int filter_opstack_push(struct filter_parse_state *ps, int op)
1123{
1124 struct opstack_op *opstack_op;
1125
1126 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1127 if (!opstack_op)
1128 return -ENOMEM;
1129
1130 opstack_op->op = op;
1131 list_add(&opstack_op->list, &ps->opstack);
1132
1133 return 0;
1134}
1135
1136static int filter_opstack_empty(struct filter_parse_state *ps)
1137{
1138 return list_empty(&ps->opstack);
1139}
1140
1141static int filter_opstack_top(struct filter_parse_state *ps)
1142{
1143 struct opstack_op *opstack_op;
1144
1145 if (filter_opstack_empty(ps))
1146 return OP_NONE;
1147
1148 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1149
1150 return opstack_op->op;
1151}
1152
1153static int filter_opstack_pop(struct filter_parse_state *ps)
1154{
1155 struct opstack_op *opstack_op;
1156 int op;
1157
1158 if (filter_opstack_empty(ps))
1159 return OP_NONE;
1160
1161 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1162 op = opstack_op->op;
1163 list_del(&opstack_op->list);
1164
1165 kfree(opstack_op);
1166
1167 return op;
1168}
1169
1170static void filter_opstack_clear(struct filter_parse_state *ps)
1171{
1172 while (!filter_opstack_empty(ps))
1173 filter_opstack_pop(ps);
1174}
1175
1176static char *curr_operand(struct filter_parse_state *ps)
1177{
1178 return ps->operand.string;
1179}
1180
1181static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1182{
1183 struct postfix_elt *elt;
1184
1185 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1186 if (!elt)
1187 return -ENOMEM;
1188
1189 elt->op = OP_NONE;
1190 elt->operand = kstrdup(operand, GFP_KERNEL);
1191 if (!elt->operand) {
1192 kfree(elt);
1193 return -ENOMEM;
1194 }
1195
1196 list_add_tail(&elt->list, &ps->postfix);
1197
1198 return 0;
1199}
1200
1201static int postfix_append_op(struct filter_parse_state *ps, int op)
1202{
1203 struct postfix_elt *elt;
1204
1205 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1206 if (!elt)
1207 return -ENOMEM;
1208
1209 elt->op = op;
1210 elt->operand = NULL;
1211
1212 list_add_tail(&elt->list, &ps->postfix);
1213
1214 return 0;
1215}
1216
1217static void postfix_clear(struct filter_parse_state *ps)
1218{
1219 struct postfix_elt *elt;
1220
1221 while (!list_empty(&ps->postfix)) {
1222 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
8b372562 1223 list_del(&elt->list);
8ad80731
LZ
1224 kfree(elt->operand);
1225 kfree(elt);
8b372562
TZ
1226 }
1227}
1228
1229static int filter_parse(struct filter_parse_state *ps)
1230{
5928c3cc 1231 int in_string = 0;
8b372562
TZ
1232 int op, top_op;
1233 char ch;
1234
1235 while ((ch = infix_next(ps))) {
5928c3cc
FW
1236 if (ch == '"') {
1237 in_string ^= 1;
1238 continue;
1239 }
1240
1241 if (in_string)
1242 goto parse_operand;
1243
8b372562
TZ
1244 if (isspace(ch))
1245 continue;
1246
1247 if (is_op_char(ps, ch)) {
1248 op = infix_get_op(ps, ch);
1249 if (op == OP_NONE) {
1250 parse_error(ps, FILT_ERR_INVALID_OP, 0);
7ce7e424
TZ
1251 return -EINVAL;
1252 }
8b372562
TZ
1253
1254 if (strlen(curr_operand(ps))) {
1255 postfix_append_operand(ps, curr_operand(ps));
1256 clear_operand_string(ps);
1257 }
1258
1259 while (!filter_opstack_empty(ps)) {
1260 top_op = filter_opstack_top(ps);
1261 if (!is_precedence_lower(ps, top_op, op)) {
1262 top_op = filter_opstack_pop(ps);
1263 postfix_append_op(ps, top_op);
1264 continue;
1265 }
1266 break;
1267 }
1268
1269 filter_opstack_push(ps, op);
7ce7e424
TZ
1270 continue;
1271 }
8b372562
TZ
1272
1273 if (ch == '(') {
1274 filter_opstack_push(ps, OP_OPEN_PAREN);
1275 continue;
1276 }
1277
1278 if (ch == ')') {
1279 if (strlen(curr_operand(ps))) {
1280 postfix_append_operand(ps, curr_operand(ps));
1281 clear_operand_string(ps);
1282 }
1283
1284 top_op = filter_opstack_pop(ps);
1285 while (top_op != OP_NONE) {
1286 if (top_op == OP_OPEN_PAREN)
1287 break;
1288 postfix_append_op(ps, top_op);
1289 top_op = filter_opstack_pop(ps);
1290 }
1291 if (top_op == OP_NONE) {
1292 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1293 return -EINVAL;
7ce7e424 1294 }
7ce7e424
TZ
1295 continue;
1296 }
5928c3cc 1297parse_operand:
8b372562
TZ
1298 if (append_operand_char(ps, ch)) {
1299 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1300 return -EINVAL;
1301 }
1302 }
1303
1304 if (strlen(curr_operand(ps)))
1305 postfix_append_operand(ps, curr_operand(ps));
1306
1307 while (!filter_opstack_empty(ps)) {
1308 top_op = filter_opstack_pop(ps);
1309 if (top_op == OP_NONE)
1310 break;
1311 if (top_op == OP_OPEN_PAREN) {
1312 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1313 return -EINVAL;
1314 }
1315 postfix_append_op(ps, top_op);
1316 }
1317
1318 return 0;
1319}
1320
1321static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
1322{
1323 struct filter_pred *pred;
1324
1325 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1326 if (!pred)
1327 return NULL;
1328
1329 pred->field_name = kstrdup(operand1, GFP_KERNEL);
1330 if (!pred->field_name) {
1331 kfree(pred);
1332 return NULL;
1333 }
1334
1889d209
FW
1335 strcpy(pred->regex.pattern, operand2);
1336 pred->regex.len = strlen(pred->regex.pattern);
8b372562
TZ
1337
1338 pred->op = op;
1339
1340 return pred;
1341}
1342
1343static struct filter_pred *create_logical_pred(int op)
1344{
1345 struct filter_pred *pred;
1346
1347 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1348 if (!pred)
1349 return NULL;
1350
1351 pred->op = op;
1352
1353 return pred;
1354}
1355
1356static int check_preds(struct filter_parse_state *ps)
1357{
1358 int n_normal_preds = 0, n_logical_preds = 0;
1359 struct postfix_elt *elt;
1360
1361 list_for_each_entry(elt, &ps->postfix, list) {
1362 if (elt->op == OP_NONE)
1363 continue;
1364
1365 if (elt->op == OP_AND || elt->op == OP_OR) {
1366 n_logical_preds++;
1367 continue;
7ce7e424 1368 }
8b372562 1369 n_normal_preds++;
7ce7e424
TZ
1370 }
1371
8b372562
TZ
1372 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1373 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
bcabd91c
LZ
1374 return -EINVAL;
1375 }
1376
8b372562
TZ
1377 return 0;
1378}
f66578a7 1379
c9c53ca0
SR
1380static int count_preds(struct filter_parse_state *ps)
1381{
1382 struct postfix_elt *elt;
1383 int n_preds = 0;
1384
1385 list_for_each_entry(elt, &ps->postfix, list) {
1386 if (elt->op == OP_NONE)
1387 continue;
1388 n_preds++;
1389 }
1390
1391 return n_preds;
1392}
1393
ec126cac
SR
1394/*
1395 * The tree is walked at filtering of an event. If the tree is not correctly
1396 * built, it may cause an infinite loop. Check here that the tree does
1397 * indeed terminate.
1398 */
1399static int check_pred_tree(struct event_filter *filter,
1400 struct filter_pred *root)
1401{
1402 struct filter_pred *preds;
1403 struct filter_pred *pred;
1404 enum move_type move = MOVE_DOWN;
1405 int count = 0;
1406 int done = 0;
1407 int max;
1408
1409 /*
1410 * The max that we can hit a node is three times.
1411 * Once going down, once coming up from left, and
1412 * once coming up from right. This is more than enough
1413 * since leafs are only hit a single time.
1414 */
1415 max = 3 * filter->n_preds;
1416
1417 preds = filter->preds;
1418 if (!preds)
1419 return -EINVAL;
1420 pred = root;
1421
1422 do {
1423 if (WARN_ON(count++ > max))
1424 return -EINVAL;
1425
1426 switch (move) {
1427 case MOVE_DOWN:
1428 if (pred->left != FILTER_PRED_INVALID) {
1429 pred = &preds[pred->left];
1430 continue;
1431 }
1432 /* A leaf at the root is just a leaf in the tree */
1433 if (pred == root)
1434 break;
1435 pred = get_pred_parent(pred, preds,
1436 pred->parent, &move);
1437 continue;
1438 case MOVE_UP_FROM_LEFT:
1439 pred = &preds[pred->right];
1440 move = MOVE_DOWN;
1441 continue;
1442 case MOVE_UP_FROM_RIGHT:
1443 if (pred == root)
1444 break;
1445 pred = get_pred_parent(pred, preds,
1446 pred->parent, &move);
1447 continue;
1448 }
1449 done = 1;
1450 } while (!done);
1451
1452 /* We are fine. */
1453 return 0;
1454}
1455
43cd4145
SR
1456static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1457{
1458 struct filter_pred *pred;
1459 enum move_type move = MOVE_DOWN;
1460 int count = 0;
1461 int done = 0;
1462
1463 pred = root;
1464
1465 do {
1466 switch (move) {
1467 case MOVE_DOWN:
1468 if (pred->left != FILTER_PRED_INVALID) {
1469 pred = &preds[pred->left];
1470 continue;
1471 }
1472 /* A leaf at the root is just a leaf in the tree */
1473 if (pred == root)
1474 return 1;
1475 count++;
1476 pred = get_pred_parent(pred, preds,
1477 pred->parent, &move);
1478 continue;
1479 case MOVE_UP_FROM_LEFT:
1480 pred = &preds[pred->right];
1481 move = MOVE_DOWN;
1482 continue;
1483 case MOVE_UP_FROM_RIGHT:
1484 if (pred == root)
1485 break;
1486 pred = get_pred_parent(pred, preds,
1487 pred->parent, &move);
1488 continue;
1489 }
1490 done = 1;
1491 } while (!done);
1492
1493 return count;
1494}
1495
1496static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1497{
1498 struct filter_pred *pred;
1499 enum move_type move = MOVE_DOWN;
1500 int count = 0;
1501 int children;
1502 int done = 0;
1503
1504 /* No need to keep the fold flag */
1505 root->index &= ~FILTER_PRED_FOLD;
1506
1507 /* If the root is a leaf then do nothing */
1508 if (root->left == FILTER_PRED_INVALID)
1509 return 0;
1510
1511 /* count the children */
1512 children = count_leafs(preds, &preds[root->left]);
1513 children += count_leafs(preds, &preds[root->right]);
1514
1515 root->ops = kzalloc(sizeof(*root->ops) * children, GFP_KERNEL);
1516 if (!root->ops)
1517 return -ENOMEM;
1518
1519 root->val = children;
1520
1521 pred = root;
1522 do {
1523 switch (move) {
1524 case MOVE_DOWN:
1525 if (pred->left != FILTER_PRED_INVALID) {
1526 pred = &preds[pred->left];
1527 continue;
1528 }
1529 if (WARN_ON(count == children))
1530 return -EINVAL;
1531 pred->index &= ~FILTER_PRED_FOLD;
1532 root->ops[count++] = pred->index;
1533 pred = get_pred_parent(pred, preds,
1534 pred->parent, &move);
1535 continue;
1536 case MOVE_UP_FROM_LEFT:
1537 pred = &preds[pred->right];
1538 move = MOVE_DOWN;
1539 continue;
1540 case MOVE_UP_FROM_RIGHT:
1541 if (pred == root)
1542 break;
1543 pred = get_pred_parent(pred, preds,
1544 pred->parent, &move);
1545 continue;
1546 }
1547 done = 1;
1548 } while (!done);
1549
1550 return 0;
1551}
1552
1553/*
1554 * To optimize the processing of the ops, if we have several "ors" or
1555 * "ands" together, we can put them in an array and process them all
1556 * together speeding up the filter logic.
1557 */
1558static int fold_pred_tree(struct event_filter *filter,
1559 struct filter_pred *root)
1560{
1561 struct filter_pred *preds;
1562 struct filter_pred *pred;
1563 enum move_type move = MOVE_DOWN;
1564 int done = 0;
1565 int err;
1566
1567 preds = filter->preds;
1568 if (!preds)
1569 return -EINVAL;
1570 pred = root;
1571
1572 do {
1573 switch (move) {
1574 case MOVE_DOWN:
1575 if (pred->index & FILTER_PRED_FOLD) {
1576 err = fold_pred(preds, pred);
1577 if (err)
1578 return err;
1579 /* Folded nodes are like leafs */
1580 } else if (pred->left != FILTER_PRED_INVALID) {
1581 pred = &preds[pred->left];
1582 continue;
1583 }
1584
1585 /* A leaf at the root is just a leaf in the tree */
1586 if (pred == root)
1587 break;
1588 pred = get_pred_parent(pred, preds,
1589 pred->parent, &move);
1590 continue;
1591 case MOVE_UP_FROM_LEFT:
1592 pred = &preds[pred->right];
1593 move = MOVE_DOWN;
1594 continue;
1595 case MOVE_UP_FROM_RIGHT:
1596 if (pred == root)
1597 break;
1598 pred = get_pred_parent(pred, preds,
1599 pred->parent, &move);
1600 continue;
1601 }
1602 done = 1;
1603 } while (!done);
1604
1605 return 0;
1606}
1607
fce29d15 1608static int replace_preds(struct ftrace_event_call *call,
6fb2915d 1609 struct event_filter *filter,
8b372562 1610 struct filter_parse_state *ps,
1f9963cb
LZ
1611 char *filter_string,
1612 bool dry_run)
8b372562
TZ
1613{
1614 char *operand1 = NULL, *operand2 = NULL;
1615 struct filter_pred *pred;
ec126cac 1616 struct filter_pred *root;
8b372562 1617 struct postfix_elt *elt;
61e9dea2 1618 struct pred_stack stack = { }; /* init to NULL */
8b372562 1619 int err;
1f9963cb 1620 int n_preds = 0;
8b372562 1621
c9c53ca0
SR
1622 n_preds = count_preds(ps);
1623 if (n_preds >= MAX_FILTER_PRED) {
1624 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1625 return -ENOSPC;
1626 }
1627
8b372562
TZ
1628 err = check_preds(ps);
1629 if (err)
1630 return err;
1631
c9c53ca0 1632 if (!dry_run) {
61e9dea2 1633 err = __alloc_pred_stack(&stack, n_preds);
c9c53ca0
SR
1634 if (err)
1635 return err;
61e9dea2
SR
1636 err = __alloc_preds(filter, n_preds);
1637 if (err)
1638 goto fail;
c9c53ca0
SR
1639 }
1640
1641 n_preds = 0;
8b372562
TZ
1642 list_for_each_entry(elt, &ps->postfix, list) {
1643 if (elt->op == OP_NONE) {
1644 if (!operand1)
1645 operand1 = elt->operand;
1646 else if (!operand2)
1647 operand2 = elt->operand;
1648 else {
1649 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
61e9dea2
SR
1650 err = -EINVAL;
1651 goto fail;
8b372562
TZ
1652 }
1653 continue;
1654 }
1655
c9c53ca0 1656 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1f9963cb 1657 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
61e9dea2
SR
1658 err = -ENOSPC;
1659 goto fail;
1f9963cb
LZ
1660 }
1661
8b372562
TZ
1662 if (elt->op == OP_AND || elt->op == OP_OR) {
1663 pred = create_logical_pred(elt->op);
1f9963cb 1664 goto add_pred;
8b372562
TZ
1665 }
1666
1667 if (!operand1 || !operand2) {
1668 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
61e9dea2
SR
1669 err = -EINVAL;
1670 goto fail;
8b372562
TZ
1671 }
1672
1673 pred = create_pred(elt->op, operand1, operand2);
1f9963cb 1674add_pred:
61e9dea2
SR
1675 if (!pred) {
1676 err = -ENOMEM;
1677 goto fail;
1678 }
1679 err = filter_add_pred(ps, call, filter, pred, &stack, dry_run);
c5cb1836 1680 filter_free_pred(pred);
8b372562 1681 if (err)
61e9dea2 1682 goto fail;
8b372562
TZ
1683
1684 operand1 = operand2 = NULL;
1685 }
7ce7e424 1686
61e9dea2
SR
1687 if (!dry_run) {
1688 /* We should have one item left on the stack */
1689 pred = __pop_pred_stack(&stack);
1690 if (!pred)
1691 return -EINVAL;
1692 /* This item is where we start from in matching */
ec126cac 1693 root = pred;
61e9dea2
SR
1694 /* Make sure the stack is empty */
1695 pred = __pop_pred_stack(&stack);
1696 if (WARN_ON(pred)) {
1697 err = -EINVAL;
1698 filter->root = NULL;
1699 goto fail;
1700 }
ec126cac
SR
1701 err = check_pred_tree(filter, root);
1702 if (err)
1703 goto fail;
1704
43cd4145
SR
1705 /* Optimize the tree */
1706 err = fold_pred_tree(filter, root);
1707 if (err)
1708 goto fail;
1709
ec126cac
SR
1710 /* We don't set root until we know it works */
1711 barrier();
1712 filter->root = root;
61e9dea2
SR
1713 }
1714
1715 err = 0;
1716fail:
1717 __free_pred_stack(&stack);
1718 return err;
7ce7e424
TZ
1719}
1720
75b8e982
SR
1721struct filter_list {
1722 struct list_head list;
1723 struct event_filter *filter;
1724};
1725
fce29d15
LZ
1726static int replace_system_preds(struct event_subsystem *system,
1727 struct filter_parse_state *ps,
1728 char *filter_string)
1729{
1730 struct ftrace_event_call *call;
75b8e982
SR
1731 struct filter_list *filter_item;
1732 struct filter_list *tmp;
1733 LIST_HEAD(filter_list);
fce29d15 1734 bool fail = true;
a66abe7f 1735 int err;
fce29d15
LZ
1736
1737 list_for_each_entry(call, &ftrace_events, list) {
1738
8f082018 1739 if (strcmp(call->class->system, system->name) != 0)
fce29d15
LZ
1740 continue;
1741
75b8e982
SR
1742 /*
1743 * Try to see if the filter can be applied
1744 * (filter arg is ignored on dry_run)
1745 */
1746 err = replace_preds(call, NULL, ps, filter_string, true);
fce29d15 1747 if (err)
0fc3ca9a
SR
1748 goto fail;
1749 }
1750
0fc3ca9a 1751 list_for_each_entry(call, &ftrace_events, list) {
75b8e982 1752 struct event_filter *filter;
0fc3ca9a
SR
1753
1754 if (strcmp(call->class->system, system->name) != 0)
1755 continue;
1756
75b8e982
SR
1757 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1758 if (!filter_item)
1759 goto fail_mem;
0fc3ca9a 1760
75b8e982 1761 list_add_tail(&filter_item->list, &filter_list);
0fc3ca9a 1762
75b8e982
SR
1763 filter_item->filter = __alloc_filter();
1764 if (!filter_item->filter)
1765 goto fail_mem;
1766 filter = filter_item->filter;
0fc3ca9a 1767
75b8e982
SR
1768 /* Can only fail on no memory */
1769 err = replace_filter_string(filter, filter_string);
1770 if (err)
1771 goto fail_mem;
fce29d15 1772
6fb2915d 1773 err = replace_preds(call, filter, ps, filter_string, false);
75b8e982
SR
1774 if (err) {
1775 filter_disable(call);
1776 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1777 append_filter_err(ps, filter);
1778 } else
553552ce 1779 call->flags |= TRACE_EVENT_FL_FILTERED;
75b8e982
SR
1780 /*
1781 * Regardless of if this returned an error, we still
1782 * replace the filter for the call.
1783 */
1784 filter = call->filter;
1785 call->filter = filter_item->filter;
1786 filter_item->filter = filter;
1787
fce29d15
LZ
1788 fail = false;
1789 }
1790
0fc3ca9a
SR
1791 if (fail)
1792 goto fail;
1793
75b8e982
SR
1794 /*
1795 * The calls can still be using the old filters.
1796 * Do a synchronize_sched() to ensure all calls are
1797 * done with them before we free them.
1798 */
1799 synchronize_sched();
1800 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1801 __free_filter(filter_item->filter);
1802 list_del(&filter_item->list);
1803 kfree(filter_item);
1804 }
fce29d15 1805 return 0;
0fc3ca9a 1806 fail:
75b8e982
SR
1807 /* No call succeeded */
1808 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1809 list_del(&filter_item->list);
1810 kfree(filter_item);
1811 }
0fc3ca9a
SR
1812 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1813 return -EINVAL;
75b8e982
SR
1814 fail_mem:
1815 /* If any call succeeded, we still need to sync */
1816 if (!fail)
1817 synchronize_sched();
1818 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1819 __free_filter(filter_item->filter);
1820 list_del(&filter_item->list);
1821 kfree(filter_item);
1822 }
1823 return -ENOMEM;
fce29d15
LZ
1824}
1825
8b372562
TZ
1826int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1827{
8b372562 1828 struct filter_parse_state *ps;
75b8e982
SR
1829 struct event_filter *filter;
1830 struct event_filter *tmp;
1831 int err = 0;
8b372562 1832
00e95830 1833 mutex_lock(&event_mutex);
8b372562
TZ
1834
1835 if (!strcmp(strstrip(filter_string), "0")) {
75b8e982
SR
1836 filter_disable(call);
1837 filter = call->filter;
1838 if (!filter)
1839 goto out_unlock;
1840 call->filter = NULL;
f76690af
SR
1841 /* Make sure the filter is not being used */
1842 synchronize_sched();
75b8e982 1843 __free_filter(filter);
a66abe7f 1844 goto out_unlock;
8b372562
TZ
1845 }
1846
8cd995b6 1847 err = -ENOMEM;
8b372562
TZ
1848 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1849 if (!ps)
8cd995b6 1850 goto out_unlock;
8b372562 1851
75b8e982
SR
1852 filter = __alloc_filter();
1853 if (!filter) {
1854 kfree(ps);
1855 goto out_unlock;
1856 }
1857
1858 replace_filter_string(filter, filter_string);
8b372562
TZ
1859
1860 parse_init(ps, filter_ops, filter_string);
1861 err = filter_parse(ps);
1862 if (err) {
75b8e982 1863 append_filter_err(ps, filter);
8b372562
TZ
1864 goto out;
1865 }
1866
75b8e982
SR
1867 err = replace_preds(call, filter, ps, filter_string, false);
1868 if (err) {
1869 filter_disable(call);
1870 append_filter_err(ps, filter);
1871 } else
553552ce 1872 call->flags |= TRACE_EVENT_FL_FILTERED;
8b372562 1873out:
75b8e982
SR
1874 /*
1875 * Always swap the call filter with the new filter
1876 * even if there was an error. If there was an error
1877 * in the filter, we disable the filter and show the error
1878 * string
1879 */
1880 tmp = call->filter;
1881 call->filter = filter;
1882 if (tmp) {
1883 /* Make sure the call is done with the filter */
1884 synchronize_sched();
1885 __free_filter(tmp);
1886 }
8b372562
TZ
1887 filter_opstack_clear(ps);
1888 postfix_clear(ps);
1889 kfree(ps);
8cd995b6 1890out_unlock:
00e95830 1891 mutex_unlock(&event_mutex);
8b372562
TZ
1892
1893 return err;
1894}
1895
1896int apply_subsystem_event_filter(struct event_subsystem *system,
1897 char *filter_string)
1898{
8b372562 1899 struct filter_parse_state *ps;
75b8e982
SR
1900 struct event_filter *filter;
1901 int err = 0;
8b372562 1902
00e95830 1903 mutex_lock(&event_mutex);
8b372562
TZ
1904
1905 if (!strcmp(strstrip(filter_string), "0")) {
fce29d15 1906 filter_free_subsystem_preds(system);
8b372562 1907 remove_filter_string(system->filter);
75b8e982
SR
1908 filter = system->filter;
1909 system->filter = NULL;
1910 /* Ensure all filters are no longer used */
1911 synchronize_sched();
1912 filter_free_subsystem_filters(system);
1913 __free_filter(filter);
a66abe7f 1914 goto out_unlock;
8b372562
TZ
1915 }
1916
8cd995b6 1917 err = -ENOMEM;
8b372562
TZ
1918 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1919 if (!ps)
8cd995b6 1920 goto out_unlock;
8b372562 1921
75b8e982
SR
1922 filter = __alloc_filter();
1923 if (!filter)
1924 goto out;
1925
1926 replace_filter_string(filter, filter_string);
1927 /*
1928 * No event actually uses the system filter
1929 * we can free it without synchronize_sched().
1930 */
1931 __free_filter(system->filter);
1932 system->filter = filter;
8b372562
TZ
1933
1934 parse_init(ps, filter_ops, filter_string);
1935 err = filter_parse(ps);
1936 if (err) {
1937 append_filter_err(ps, system->filter);
1938 goto out;
1939 }
1940
fce29d15
LZ
1941 err = replace_system_preds(system, ps, filter_string);
1942 if (err)
8b372562
TZ
1943 append_filter_err(ps, system->filter);
1944
1945out:
1946 filter_opstack_clear(ps);
1947 postfix_clear(ps);
1948 kfree(ps);
8cd995b6 1949out_unlock:
00e95830 1950 mutex_unlock(&event_mutex);
8b372562
TZ
1951
1952 return err;
1953}
7ce7e424 1954
07b139c8 1955#ifdef CONFIG_PERF_EVENTS
6fb2915d
LZ
1956
1957void ftrace_profile_free_filter(struct perf_event *event)
1958{
1959 struct event_filter *filter = event->filter;
1960
1961 event->filter = NULL;
c9c53ca0 1962 __free_filter(filter);
6fb2915d
LZ
1963}
1964
1965int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1966 char *filter_str)
1967{
1968 int err;
1969 struct event_filter *filter;
1970 struct filter_parse_state *ps;
1971 struct ftrace_event_call *call = NULL;
1972
1973 mutex_lock(&event_mutex);
1974
1975 list_for_each_entry(call, &ftrace_events, list) {
32c0edae 1976 if (call->event.type == event_id)
6fb2915d
LZ
1977 break;
1978 }
a66abe7f
IM
1979
1980 err = -EINVAL;
d9f599e1 1981 if (&call->list == &ftrace_events)
a66abe7f 1982 goto out_unlock;
6fb2915d 1983
a66abe7f 1984 err = -EEXIST;
6fb2915d 1985 if (event->filter)
a66abe7f 1986 goto out_unlock;
6fb2915d 1987
c9c53ca0 1988 filter = __alloc_filter();
75b8e982 1989 if (!filter) {
a66abe7f
IM
1990 err = PTR_ERR(filter);
1991 goto out_unlock;
1992 }
6fb2915d
LZ
1993
1994 err = -ENOMEM;
1995 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1996 if (!ps)
c9c53ca0 1997 goto free_filter;
6fb2915d
LZ
1998
1999 parse_init(ps, filter_ops, filter_str);
2000 err = filter_parse(ps);
2001 if (err)
2002 goto free_ps;
2003
2004 err = replace_preds(call, filter, ps, filter_str, false);
2005 if (!err)
2006 event->filter = filter;
2007
2008free_ps:
2009 filter_opstack_clear(ps);
2010 postfix_clear(ps);
2011 kfree(ps);
2012
c9c53ca0 2013free_filter:
6fb2915d 2014 if (err)
c9c53ca0 2015 __free_filter(filter);
6fb2915d 2016
a66abe7f 2017out_unlock:
6fb2915d
LZ
2018 mutex_unlock(&event_mutex);
2019
2020 return err;
2021}
2022
07b139c8 2023#endif /* CONFIG_PERF_EVENTS */
6fb2915d 2024