tracing: Kill ftrace_event_call->files
[deliverable/linux.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
49aa2951
SR
30#define DEFAULT_SYS_FILTER_MESSAGE \
31 "### global filter ###\n" \
32 "# Use this to set filters for multiple events.\n" \
33 "# Only events with the given fields will be affected.\n" \
34 "# If no events are modified, an error message will be displayed here"
35
8b372562 36enum filter_op_ids
7ce7e424 37{
8b372562
TZ
38 OP_OR,
39 OP_AND,
b0f1a59a 40 OP_GLOB,
8b372562
TZ
41 OP_NE,
42 OP_EQ,
43 OP_LT,
44 OP_LE,
45 OP_GT,
46 OP_GE,
1a891cf1 47 OP_BAND,
8b372562
TZ
48 OP_NONE,
49 OP_OPEN_PAREN,
50};
51
52struct filter_op {
53 int id;
54 char *string;
55 int precedence;
56};
57
1a891cf1 58/* Order must be the same as enum filter_op_ids above */
8b372562 59static struct filter_op filter_ops[] = {
b0f1a59a
LZ
60 { OP_OR, "||", 1 },
61 { OP_AND, "&&", 2 },
62 { OP_GLOB, "~", 4 },
63 { OP_NE, "!=", 4 },
64 { OP_EQ, "==", 4 },
65 { OP_LT, "<", 5 },
66 { OP_LE, "<=", 5 },
67 { OP_GT, ">", 5 },
68 { OP_GE, ">=", 5 },
1a891cf1 69 { OP_BAND, "&", 6 },
b0f1a59a
LZ
70 { OP_NONE, "OP_NONE", 0 },
71 { OP_OPEN_PAREN, "(", 0 },
8b372562
TZ
72};
73
74enum {
75 FILT_ERR_NONE,
76 FILT_ERR_INVALID_OP,
77 FILT_ERR_UNBALANCED_PAREN,
78 FILT_ERR_TOO_MANY_OPERANDS,
79 FILT_ERR_OPERAND_TOO_LONG,
80 FILT_ERR_FIELD_NOT_FOUND,
81 FILT_ERR_ILLEGAL_FIELD_OP,
82 FILT_ERR_ILLEGAL_INTVAL,
83 FILT_ERR_BAD_SUBSYS_FILTER,
84 FILT_ERR_TOO_MANY_PREDS,
85 FILT_ERR_MISSING_FIELD,
86 FILT_ERR_INVALID_FILTER,
5500fa51 87 FILT_ERR_IP_FIELD_ONLY,
8b372562
TZ
88};
89
90static char *err_text[] = {
91 "No error",
92 "Invalid operator",
93 "Unbalanced parens",
94 "Too many operands",
95 "Operand too long",
96 "Field not found",
97 "Illegal operation for field type",
98 "Illegal integer value",
99 "Couldn't find or set field in one of a subsystem's events",
100 "Too many terms in predicate expression",
101 "Missing field name and/or value",
102 "Meaningless filter expression",
5500fa51 103 "Only 'ip' field is supported for function trace",
8b372562
TZ
104};
105
106struct opstack_op {
107 int op;
108 struct list_head list;
109};
110
111struct postfix_elt {
112 int op;
113 char *operand;
114 struct list_head list;
115};
116
117struct filter_parse_state {
118 struct filter_op *ops;
119 struct list_head opstack;
120 struct list_head postfix;
121 int lasterr;
122 int lasterr_pos;
123
124 struct {
125 char *string;
126 unsigned int cnt;
127 unsigned int tail;
128 } infix;
129
130 struct {
131 char string[MAX_FILTER_STR_VAL];
132 int pos;
133 unsigned int tail;
134 } operand;
135};
136
61e9dea2
SR
137struct pred_stack {
138 struct filter_pred **preds;
139 int index;
140};
141
197e2eab 142#define DEFINE_COMPARISON_PRED(type) \
58d9a597 143static int filter_pred_##type(struct filter_pred *pred, void *event) \
197e2eab
LZ
144{ \
145 type *addr = (type *)(event + pred->offset); \
146 type val = (type)pred->val; \
147 int match = 0; \
148 \
149 switch (pred->op) { \
150 case OP_LT: \
151 match = (*addr < val); \
152 break; \
153 case OP_LE: \
154 match = (*addr <= val); \
155 break; \
156 case OP_GT: \
157 match = (*addr > val); \
158 break; \
159 case OP_GE: \
160 match = (*addr >= val); \
161 break; \
1a891cf1
SR
162 case OP_BAND: \
163 match = (*addr & val); \
164 break; \
197e2eab
LZ
165 default: \
166 break; \
167 } \
168 \
169 return match; \
170}
171
172#define DEFINE_EQUALITY_PRED(size) \
58d9a597 173static int filter_pred_##size(struct filter_pred *pred, void *event) \
197e2eab
LZ
174{ \
175 u##size *addr = (u##size *)(event + pred->offset); \
176 u##size val = (u##size)pred->val; \
177 int match; \
178 \
179 match = (val == *addr) ^ pred->not; \
180 \
181 return match; \
182}
183
8b372562
TZ
184DEFINE_COMPARISON_PRED(s64);
185DEFINE_COMPARISON_PRED(u64);
186DEFINE_COMPARISON_PRED(s32);
187DEFINE_COMPARISON_PRED(u32);
188DEFINE_COMPARISON_PRED(s16);
189DEFINE_COMPARISON_PRED(u16);
190DEFINE_COMPARISON_PRED(s8);
191DEFINE_COMPARISON_PRED(u8);
192
193DEFINE_EQUALITY_PRED(64);
194DEFINE_EQUALITY_PRED(32);
195DEFINE_EQUALITY_PRED(16);
196DEFINE_EQUALITY_PRED(8);
197
e8808c10 198/* Filter predicate for fixed sized arrays of characters */
58d9a597 199static int filter_pred_string(struct filter_pred *pred, void *event)
7ce7e424
TZ
200{
201 char *addr = (char *)(event + pred->offset);
202 int cmp, match;
203
1889d209 204 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
7ce7e424 205
1889d209 206 match = cmp ^ pred->not;
7ce7e424
TZ
207
208 return match;
209}
210
87a342f5 211/* Filter predicate for char * pointers */
58d9a597 212static int filter_pred_pchar(struct filter_pred *pred, void *event)
87a342f5
LZ
213{
214 char **addr = (char **)(event + pred->offset);
215 int cmp, match;
16da27a8 216 int len = strlen(*addr) + 1; /* including tailing '\0' */
87a342f5 217
16da27a8 218 cmp = pred->regex.match(*addr, &pred->regex, len);
87a342f5 219
1889d209 220 match = cmp ^ pred->not;
87a342f5
LZ
221
222 return match;
223}
224
e8808c10
FW
225/*
226 * Filter predicate for dynamic sized arrays of characters.
227 * These are implemented through a list of strings at the end
228 * of the entry.
229 * Also each of these strings have a field in the entry which
230 * contains its offset from the beginning of the entry.
231 * We have then first to get this field, dereference it
232 * and add it to the address of the entry, and at last we have
233 * the address of the string.
234 */
58d9a597 235static int filter_pred_strloc(struct filter_pred *pred, void *event)
e8808c10 236{
7d536cb3
LZ
237 u32 str_item = *(u32 *)(event + pred->offset);
238 int str_loc = str_item & 0xffff;
239 int str_len = str_item >> 16;
e8808c10
FW
240 char *addr = (char *)(event + str_loc);
241 int cmp, match;
242
1889d209 243 cmp = pred->regex.match(addr, &pred->regex, str_len);
e8808c10 244
1889d209 245 match = cmp ^ pred->not;
e8808c10
FW
246
247 return match;
248}
249
58d9a597 250static int filter_pred_none(struct filter_pred *pred, void *event)
0a19e53c
TZ
251{
252 return 0;
253}
254
d1303dd1
LZ
255/*
256 * regex_match_foo - Basic regex callbacks
257 *
258 * @str: the string to be searched
259 * @r: the regex structure containing the pattern string
260 * @len: the length of the string to be searched (including '\0')
261 *
262 * Note:
263 * - @str might not be NULL-terminated if it's of type DYN_STRING
264 * or STATIC_STRING
265 */
266
1889d209
FW
267static int regex_match_full(char *str, struct regex *r, int len)
268{
269 if (strncmp(str, r->pattern, len) == 0)
270 return 1;
271 return 0;
272}
273
274static int regex_match_front(char *str, struct regex *r, int len)
275{
285caad4 276 if (strncmp(str, r->pattern, r->len) == 0)
1889d209
FW
277 return 1;
278 return 0;
279}
280
281static int regex_match_middle(char *str, struct regex *r, int len)
282{
b2af211f 283 if (strnstr(str, r->pattern, len))
1889d209
FW
284 return 1;
285 return 0;
286}
287
288static int regex_match_end(char *str, struct regex *r, int len)
289{
a3291c14 290 int strlen = len - 1;
1889d209 291
a3291c14
LZ
292 if (strlen >= r->len &&
293 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1889d209
FW
294 return 1;
295 return 0;
296}
297
3f6fe06d
FW
298/**
299 * filter_parse_regex - parse a basic regex
300 * @buff: the raw regex
301 * @len: length of the regex
302 * @search: will point to the beginning of the string to compare
303 * @not: tell whether the match will have to be inverted
304 *
305 * This passes in a buffer containing a regex and this function will
1889d209
FW
306 * set search to point to the search part of the buffer and
307 * return the type of search it is (see enum above).
308 * This does modify buff.
309 *
310 * Returns enum type.
311 * search returns the pointer to use for comparison.
312 * not returns 1 if buff started with a '!'
313 * 0 otherwise.
314 */
3f6fe06d 315enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1889d209
FW
316{
317 int type = MATCH_FULL;
318 int i;
319
320 if (buff[0] == '!') {
321 *not = 1;
322 buff++;
323 len--;
324 } else
325 *not = 0;
326
327 *search = buff;
328
329 for (i = 0; i < len; i++) {
330 if (buff[i] == '*') {
331 if (!i) {
332 *search = buff + 1;
333 type = MATCH_END_ONLY;
334 } else {
335 if (type == MATCH_END_ONLY)
336 type = MATCH_MIDDLE_ONLY;
337 else
338 type = MATCH_FRONT_ONLY;
339 buff[i] = 0;
340 break;
341 }
342 }
343 }
344
345 return type;
346}
347
b0f1a59a 348static void filter_build_regex(struct filter_pred *pred)
1889d209
FW
349{
350 struct regex *r = &pred->regex;
b0f1a59a
LZ
351 char *search;
352 enum regex_type type = MATCH_FULL;
353 int not = 0;
354
355 if (pred->op == OP_GLOB) {
356 type = filter_parse_regex(r->pattern, r->len, &search, &not);
357 r->len = strlen(search);
358 memmove(r->pattern, search, r->len+1);
359 }
1889d209
FW
360
361 switch (type) {
362 case MATCH_FULL:
363 r->match = regex_match_full;
364 break;
365 case MATCH_FRONT_ONLY:
366 r->match = regex_match_front;
367 break;
368 case MATCH_MIDDLE_ONLY:
369 r->match = regex_match_middle;
370 break;
371 case MATCH_END_ONLY:
372 r->match = regex_match_end;
373 break;
374 }
375
376 pred->not ^= not;
1889d209
FW
377}
378
61e9dea2
SR
379enum move_type {
380 MOVE_DOWN,
381 MOVE_UP_FROM_LEFT,
382 MOVE_UP_FROM_RIGHT
383};
384
385static struct filter_pred *
386get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
387 int index, enum move_type *move)
388{
389 if (pred->parent & FILTER_PRED_IS_RIGHT)
390 *move = MOVE_UP_FROM_RIGHT;
391 else
392 *move = MOVE_UP_FROM_LEFT;
393 pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
394
395 return pred;
396}
397
f03f5979
JO
398enum walk_return {
399 WALK_PRED_ABORT,
400 WALK_PRED_PARENT,
401 WALK_PRED_DEFAULT,
402};
403
404typedef int (*filter_pred_walkcb_t) (enum move_type move,
405 struct filter_pred *pred,
406 int *err, void *data);
407
408static int walk_pred_tree(struct filter_pred *preds,
409 struct filter_pred *root,
410 filter_pred_walkcb_t cb, void *data)
411{
412 struct filter_pred *pred = root;
413 enum move_type move = MOVE_DOWN;
414 int done = 0;
415
416 if (!preds)
417 return -EINVAL;
418
419 do {
420 int err = 0, ret;
421
422 ret = cb(move, pred, &err, data);
423 if (ret == WALK_PRED_ABORT)
424 return err;
425 if (ret == WALK_PRED_PARENT)
426 goto get_parent;
427
428 switch (move) {
429 case MOVE_DOWN:
430 if (pred->left != FILTER_PRED_INVALID) {
431 pred = &preds[pred->left];
432 continue;
433 }
434 goto get_parent;
435 case MOVE_UP_FROM_LEFT:
436 pred = &preds[pred->right];
437 move = MOVE_DOWN;
438 continue;
439 case MOVE_UP_FROM_RIGHT:
440 get_parent:
441 if (pred == root)
442 break;
443 pred = get_pred_parent(pred, preds,
444 pred->parent,
445 &move);
446 continue;
447 }
448 done = 1;
449 } while (!done);
450
451 /* We are fine. */
452 return 0;
453}
454
43cd4145
SR
455/*
456 * A series of AND or ORs where found together. Instead of
457 * climbing up and down the tree branches, an array of the
458 * ops were made in order of checks. We can just move across
459 * the array and short circuit if needed.
460 */
461static int process_ops(struct filter_pred *preds,
462 struct filter_pred *op, void *rec)
463{
464 struct filter_pred *pred;
1ef1d1c2 465 int match = 0;
43cd4145 466 int type;
43cd4145
SR
467 int i;
468
469 /*
470 * Micro-optimization: We set type to true if op
471 * is an OR and false otherwise (AND). Then we
472 * just need to test if the match is equal to
473 * the type, and if it is, we can short circuit the
474 * rest of the checks:
475 *
476 * if ((match && op->op == OP_OR) ||
477 * (!match && op->op == OP_AND))
478 * return match;
479 */
480 type = op->op == OP_OR;
481
482 for (i = 0; i < op->val; i++) {
483 pred = &preds[op->ops[i]];
f30120fc
JO
484 if (!WARN_ON_ONCE(!pred->fn))
485 match = pred->fn(pred, rec);
43cd4145
SR
486 if (!!match == type)
487 return match;
488 }
489 return match;
490}
491
f30120fc
JO
492struct filter_match_preds_data {
493 struct filter_pred *preds;
494 int match;
495 void *rec;
496};
497
498static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred,
499 int *err, void *data)
500{
501 struct filter_match_preds_data *d = data;
502
503 *err = 0;
504 switch (move) {
505 case MOVE_DOWN:
506 /* only AND and OR have children */
507 if (pred->left != FILTER_PRED_INVALID) {
508 /* If ops is set, then it was folded. */
509 if (!pred->ops)
510 return WALK_PRED_DEFAULT;
511 /* We can treat folded ops as a leaf node */
512 d->match = process_ops(d->preds, pred, d->rec);
513 } else {
514 if (!WARN_ON_ONCE(!pred->fn))
515 d->match = pred->fn(pred, d->rec);
516 }
517
518 return WALK_PRED_PARENT;
519 case MOVE_UP_FROM_LEFT:
520 /*
521 * Check for short circuits.
522 *
523 * Optimization: !!match == (pred->op == OP_OR)
524 * is the same as:
525 * if ((match && pred->op == OP_OR) ||
526 * (!match && pred->op == OP_AND))
527 */
528 if (!!d->match == (pred->op == OP_OR))
529 return WALK_PRED_PARENT;
530 break;
531 case MOVE_UP_FROM_RIGHT:
532 break;
533 }
534
535 return WALK_PRED_DEFAULT;
536}
537
7ce7e424 538/* return 1 if event matches, 0 otherwise (discard) */
6fb2915d 539int filter_match_preds(struct event_filter *filter, void *rec)
7ce7e424 540{
74e9e58c 541 struct filter_pred *preds;
61e9dea2 542 struct filter_pred *root;
f30120fc
JO
543 struct filter_match_preds_data data = {
544 /* match is currently meaningless */
545 .match = -1,
546 .rec = rec,
547 };
548 int n_preds, ret;
7ce7e424 549
6d54057d 550 /* no filter is considered a match */
75b8e982
SR
551 if (!filter)
552 return 1;
553
554 n_preds = filter->n_preds;
6d54057d
SR
555 if (!n_preds)
556 return 1;
557
c9c53ca0 558 /*
61e9dea2 559 * n_preds, root and filter->preds are protect with preemption disabled.
c9c53ca0 560 */
61e9dea2
SR
561 root = rcu_dereference_sched(filter->root);
562 if (!root)
563 return 1;
c9c53ca0 564
f30120fc
JO
565 data.preds = preds = rcu_dereference_sched(filter->preds);
566 ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data);
567 WARN_ON(ret);
568 return data.match;
7ce7e424 569}
17c873ec 570EXPORT_SYMBOL_GPL(filter_match_preds);
7ce7e424 571
8b372562 572static void parse_error(struct filter_parse_state *ps, int err, int pos)
7ce7e424 573{
8b372562
TZ
574 ps->lasterr = err;
575 ps->lasterr_pos = pos;
576}
7ce7e424 577
8b372562
TZ
578static void remove_filter_string(struct event_filter *filter)
579{
75b8e982
SR
580 if (!filter)
581 return;
582
8b372562
TZ
583 kfree(filter->filter_string);
584 filter->filter_string = NULL;
585}
586
587static int replace_filter_string(struct event_filter *filter,
588 char *filter_string)
589{
590 kfree(filter->filter_string);
591 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
592 if (!filter->filter_string)
593 return -ENOMEM;
594
595 return 0;
596}
597
598static int append_filter_string(struct event_filter *filter,
599 char *string)
600{
601 int newlen;
602 char *new_filter_string;
603
604 BUG_ON(!filter->filter_string);
605 newlen = strlen(filter->filter_string) + strlen(string) + 1;
606 new_filter_string = kmalloc(newlen, GFP_KERNEL);
607 if (!new_filter_string)
608 return -ENOMEM;
609
610 strcpy(new_filter_string, filter->filter_string);
611 strcat(new_filter_string, string);
612 kfree(filter->filter_string);
613 filter->filter_string = new_filter_string;
614
615 return 0;
616}
617
618static void append_filter_err(struct filter_parse_state *ps,
619 struct event_filter *filter)
620{
621 int pos = ps->lasterr_pos;
622 char *buf, *pbuf;
623
624 buf = (char *)__get_free_page(GFP_TEMPORARY);
625 if (!buf)
4bda2d51 626 return;
7ce7e424 627
8b372562
TZ
628 append_filter_string(filter, "\n");
629 memset(buf, ' ', PAGE_SIZE);
630 if (pos > PAGE_SIZE - 128)
631 pos = 0;
632 buf[pos] = '^';
633 pbuf = &buf[pos] + 1;
634
635 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
636 append_filter_string(filter, buf);
637 free_page((unsigned long) buf);
7ce7e424
TZ
638}
639
f306cc82
TZ
640static inline struct event_filter *event_filter(struct ftrace_event_file *file)
641{
642 if (file->event_call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
643 return file->event_call->filter;
644 else
645 return file->filter;
646}
647
e2912b09 648/* caller must hold event_mutex */
f306cc82 649void print_event_filter(struct ftrace_event_file *file, struct trace_seq *s)
ac1adc55 650{
f306cc82 651 struct event_filter *filter = event_filter(file);
8b372562 652
8e254c1d 653 if (filter && filter->filter_string)
8b372562
TZ
654 trace_seq_printf(s, "%s\n", filter->filter_string);
655 else
146c3442 656 trace_seq_puts(s, "none\n");
ac1adc55
TZ
657}
658
8b372562 659void print_subsystem_event_filter(struct event_subsystem *system,
ac1adc55
TZ
660 struct trace_seq *s)
661{
75b8e982 662 struct event_filter *filter;
8b372562 663
00e95830 664 mutex_lock(&event_mutex);
75b8e982 665 filter = system->filter;
8e254c1d 666 if (filter && filter->filter_string)
8b372562
TZ
667 trace_seq_printf(s, "%s\n", filter->filter_string);
668 else
146c3442 669 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
00e95830 670 mutex_unlock(&event_mutex);
ac1adc55
TZ
671}
672
61e9dea2
SR
673static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
674{
47b0edcb 675 stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
61e9dea2
SR
676 if (!stack->preds)
677 return -ENOMEM;
678 stack->index = n_preds;
679 return 0;
680}
681
682static void __free_pred_stack(struct pred_stack *stack)
683{
684 kfree(stack->preds);
685 stack->index = 0;
686}
687
688static int __push_pred_stack(struct pred_stack *stack,
689 struct filter_pred *pred)
690{
691 int index = stack->index;
692
693 if (WARN_ON(index == 0))
694 return -ENOSPC;
695
696 stack->preds[--index] = pred;
697 stack->index = index;
698 return 0;
699}
700
701static struct filter_pred *
702__pop_pred_stack(struct pred_stack *stack)
703{
704 struct filter_pred *pred;
705 int index = stack->index;
706
707 pred = stack->preds[index++];
708 if (!pred)
709 return NULL;
710
711 stack->index = index;
712 return pred;
713}
714
715static int filter_set_pred(struct event_filter *filter,
716 int idx,
717 struct pred_stack *stack,
9d96cd17 718 struct filter_pred *src)
0a19e53c 719{
61e9dea2
SR
720 struct filter_pred *dest = &filter->preds[idx];
721 struct filter_pred *left;
722 struct filter_pred *right;
723
0a19e53c 724 *dest = *src;
61e9dea2 725 dest->index = idx;
0a19e53c 726
61e9dea2
SR
727 if (dest->op == OP_OR || dest->op == OP_AND) {
728 right = __pop_pred_stack(stack);
729 left = __pop_pred_stack(stack);
730 if (!left || !right)
731 return -EINVAL;
43cd4145
SR
732 /*
733 * If both children can be folded
734 * and they are the same op as this op or a leaf,
735 * then this op can be folded.
736 */
737 if (left->index & FILTER_PRED_FOLD &&
738 (left->op == dest->op ||
739 left->left == FILTER_PRED_INVALID) &&
740 right->index & FILTER_PRED_FOLD &&
741 (right->op == dest->op ||
742 right->left == FILTER_PRED_INVALID))
743 dest->index |= FILTER_PRED_FOLD;
744
745 dest->left = left->index & ~FILTER_PRED_FOLD;
746 dest->right = right->index & ~FILTER_PRED_FOLD;
747 left->parent = dest->index & ~FILTER_PRED_FOLD;
61e9dea2 748 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
43cd4145 749 } else {
61e9dea2
SR
750 /*
751 * Make dest->left invalid to be used as a quick
752 * way to know this is a leaf node.
753 */
754 dest->left = FILTER_PRED_INVALID;
755
43cd4145
SR
756 /* All leafs allow folding the parent ops. */
757 dest->index |= FILTER_PRED_FOLD;
758 }
759
61e9dea2 760 return __push_pred_stack(stack, dest);
0a19e53c
TZ
761}
762
c9c53ca0
SR
763static void __free_preds(struct event_filter *filter)
764{
60705c89
SRRH
765 int i;
766
c9c53ca0 767 if (filter->preds) {
60705c89
SRRH
768 for (i = 0; i < filter->n_preds; i++)
769 kfree(filter->preds[i].ops);
c9c53ca0
SR
770 kfree(filter->preds);
771 filter->preds = NULL;
772 }
773 filter->a_preds = 0;
774 filter->n_preds = 0;
775}
776
f306cc82
TZ
777static void filter_disable(struct ftrace_event_file *file)
778{
779 struct ftrace_event_call *call = file->event_call;
780
781 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
b5d09db5 782 call->flags &= ~TRACE_EVENT_FL_FILTERED;
f306cc82
TZ
783 else
784 file->flags &= ~FTRACE_EVENT_FL_FILTERED;
785}
786
c9c53ca0 787static void __free_filter(struct event_filter *filter)
2df75e41 788{
8e254c1d
LZ
789 if (!filter)
790 return;
791
c9c53ca0 792 __free_preds(filter);
57be8887 793 kfree(filter->filter_string);
2df75e41 794 kfree(filter);
6fb2915d
LZ
795}
796
bac5fb97
TZ
797void free_event_filter(struct event_filter *filter)
798{
799 __free_filter(filter);
800}
801
c9c53ca0 802static struct event_filter *__alloc_filter(void)
0a19e53c 803{
30e673b2 804 struct event_filter *filter;
0a19e53c 805
6fb2915d 806 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
c9c53ca0
SR
807 return filter;
808}
809
810static int __alloc_preds(struct event_filter *filter, int n_preds)
811{
812 struct filter_pred *pred;
813 int i;
814
4defe682
SR
815 if (filter->preds)
816 __free_preds(filter);
817
47b0edcb 818 filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
c9c53ca0 819
30e673b2 820 if (!filter->preds)
c9c53ca0
SR
821 return -ENOMEM;
822
4defe682
SR
823 filter->a_preds = n_preds;
824 filter->n_preds = 0;
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
f306cc82 834static inline void __remove_filter(struct ftrace_event_file *file)
8e254c1d 835{
f306cc82
TZ
836 struct ftrace_event_call *call = file->event_call;
837
838 filter_disable(file);
839 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
840 remove_filter_string(call->filter);
841 else
842 remove_filter_string(file->filter);
843}
844
845static void filter_free_subsystem_preds(struct event_subsystem *system,
846 struct trace_array *tr)
847{
848 struct ftrace_event_file *file;
8e254c1d 849 struct ftrace_event_call *call;
8e254c1d 850
f306cc82
TZ
851 list_for_each_entry(file, &tr->events, list) {
852 call = file->event_call;
8f082018 853 if (strcmp(call->class->system, system->name) != 0)
8e254c1d
LZ
854 continue;
855
f306cc82 856 __remove_filter(file);
8e254c1d 857 }
8e254c1d 858}
7ce7e424 859
f306cc82 860static inline void __free_subsystem_filter(struct ftrace_event_file *file)
cfb180f3 861{
f306cc82
TZ
862 struct ftrace_event_call *call = file->event_call;
863
864 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) {
865 __free_filter(call->filter);
866 call->filter = NULL;
867 } else {
868 __free_filter(file->filter);
869 file->filter = NULL;
870 }
871}
872
873static void filter_free_subsystem_filters(struct event_subsystem *system,
874 struct trace_array *tr)
875{
876 struct ftrace_event_file *file;
a59fd602 877 struct ftrace_event_call *call;
cfb180f3 878
f306cc82
TZ
879 list_for_each_entry(file, &tr->events, list) {
880 call = file->event_call;
8f082018 881 if (strcmp(call->class->system, system->name) != 0)
8e254c1d 882 continue;
f306cc82 883 __free_subsystem_filter(file);
cfb180f3
TZ
884 }
885}
886
9d96cd17
JO
887static int filter_add_pred(struct filter_parse_state *ps,
888 struct event_filter *filter,
889 struct filter_pred *pred,
890 struct pred_stack *stack)
7ce7e424 891{
61aaef55 892 int err;
7ce7e424 893
c9c53ca0 894 if (WARN_ON(filter->n_preds == filter->a_preds)) {
8b372562 895 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
0a19e53c 896 return -ENOSPC;
8b372562 897 }
7ce7e424 898
61aaef55 899 err = filter_set_pred(filter, filter->n_preds, stack, pred);
0a19e53c
TZ
900 if (err)
901 return err;
902
30e673b2 903 filter->n_preds++;
7ce7e424 904
0a19e53c 905 return 0;
7ce7e424
TZ
906}
907
aa38e9fc 908int filter_assign_type(const char *type)
7ce7e424 909{
7fcb7c47
LZ
910 if (strstr(type, "__data_loc") && strstr(type, "char"))
911 return FILTER_DYN_STRING;
912
7ce7e424 913 if (strchr(type, '[') && strstr(type, "char"))
e8808c10
FW
914 return FILTER_STATIC_STRING;
915
aa38e9fc
LZ
916 return FILTER_OTHER;
917}
918
02aa3162
JO
919static bool is_function_field(struct ftrace_event_field *field)
920{
921 return field->filter_type == FILTER_TRACE_FN;
922}
923
aa38e9fc
LZ
924static bool is_string_field(struct ftrace_event_field *field)
925{
926 return field->filter_type == FILTER_DYN_STRING ||
87a342f5
LZ
927 field->filter_type == FILTER_STATIC_STRING ||
928 field->filter_type == FILTER_PTR_STRING;
7ce7e424
TZ
929}
930
8b372562
TZ
931static int is_legal_op(struct ftrace_event_field *field, int op)
932{
b0f1a59a
LZ
933 if (is_string_field(field) &&
934 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
935 return 0;
936 if (!is_string_field(field) && op == OP_GLOB)
8b372562
TZ
937 return 0;
938
939 return 1;
940}
941
942static filter_pred_fn_t select_comparison_fn(int op, int field_size,
943 int field_is_signed)
944{
945 filter_pred_fn_t fn = NULL;
946
947 switch (field_size) {
948 case 8:
949 if (op == OP_EQ || op == OP_NE)
950 fn = filter_pred_64;
951 else if (field_is_signed)
952 fn = filter_pred_s64;
953 else
954 fn = filter_pred_u64;
955 break;
956 case 4:
957 if (op == OP_EQ || op == OP_NE)
958 fn = filter_pred_32;
959 else if (field_is_signed)
960 fn = filter_pred_s32;
961 else
962 fn = filter_pred_u32;
963 break;
964 case 2:
965 if (op == OP_EQ || op == OP_NE)
966 fn = filter_pred_16;
967 else if (field_is_signed)
968 fn = filter_pred_s16;
969 else
970 fn = filter_pred_u16;
971 break;
972 case 1:
973 if (op == OP_EQ || op == OP_NE)
974 fn = filter_pred_8;
975 else if (field_is_signed)
976 fn = filter_pred_s8;
977 else
978 fn = filter_pred_u8;
979 break;
980 }
981
982 return fn;
983}
984
9d96cd17 985static int init_pred(struct filter_parse_state *ps,
61aaef55 986 struct ftrace_event_field *field,
9d96cd17
JO
987 struct filter_pred *pred)
988
7ce7e424 989{
9d96cd17 990 filter_pred_fn_t fn = filter_pred_none;
f66578a7 991 unsigned long long val;
5e4904cb 992 int ret;
7ce7e424 993
7ce7e424
TZ
994 pred->offset = field->offset;
995
8b372562
TZ
996 if (!is_legal_op(field, pred->op)) {
997 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
998 return -EINVAL;
999 }
1000
aa38e9fc 1001 if (is_string_field(field)) {
b0f1a59a 1002 filter_build_regex(pred);
87a342f5 1003
1889d209 1004 if (field->filter_type == FILTER_STATIC_STRING) {
e8808c10 1005 fn = filter_pred_string;
1889d209
FW
1006 pred->regex.field_len = field->size;
1007 } else if (field->filter_type == FILTER_DYN_STRING)
b0f1a59a 1008 fn = filter_pred_strloc;
16da27a8 1009 else
87a342f5 1010 fn = filter_pred_pchar;
5500fa51
JO
1011 } else if (is_function_field(field)) {
1012 if (strcmp(field->name, "ip")) {
1013 parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0);
1014 return -EINVAL;
1015 }
1016 } else {
5e4904cb 1017 if (field->is_signed)
bcd83ea6 1018 ret = kstrtoll(pred->regex.pattern, 0, &val);
5e4904cb 1019 else
bcd83ea6 1020 ret = kstrtoull(pred->regex.pattern, 0, &val);
5e4904cb 1021 if (ret) {
8b372562 1022 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
9f58a159 1023 return -EINVAL;
8b372562 1024 }
f66578a7 1025 pred->val = val;
7ce7e424 1026
1f9963cb
LZ
1027 fn = select_comparison_fn(pred->op, field->size,
1028 field->is_signed);
1029 if (!fn) {
1030 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1031 return -EINVAL;
1032 }
7ce7e424
TZ
1033 }
1034
8b372562
TZ
1035 if (pred->op == OP_NE)
1036 pred->not = 1;
ac1adc55 1037
9d96cd17 1038 pred->fn = fn;
1f9963cb 1039 return 0;
cfb180f3
TZ
1040}
1041
8b372562
TZ
1042static void parse_init(struct filter_parse_state *ps,
1043 struct filter_op *ops,
1044 char *infix_string)
1045{
1046 memset(ps, '\0', sizeof(*ps));
1047
1048 ps->infix.string = infix_string;
1049 ps->infix.cnt = strlen(infix_string);
1050 ps->ops = ops;
1051
1052 INIT_LIST_HEAD(&ps->opstack);
1053 INIT_LIST_HEAD(&ps->postfix);
1054}
1055
1056static char infix_next(struct filter_parse_state *ps)
1057{
1058 ps->infix.cnt--;
1059
1060 return ps->infix.string[ps->infix.tail++];
1061}
1062
1063static char infix_peek(struct filter_parse_state *ps)
1064{
1065 if (ps->infix.tail == strlen(ps->infix.string))
1066 return 0;
1067
1068 return ps->infix.string[ps->infix.tail];
1069}
1070
1071static void infix_advance(struct filter_parse_state *ps)
1072{
1073 ps->infix.cnt--;
1074 ps->infix.tail++;
1075}
1076
1077static inline int is_precedence_lower(struct filter_parse_state *ps,
1078 int a, int b)
1079{
1080 return ps->ops[a].precedence < ps->ops[b].precedence;
1081}
1082
1083static inline int is_op_char(struct filter_parse_state *ps, char c)
1084{
1085 int i;
1086
1087 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1088 if (ps->ops[i].string[0] == c)
1089 return 1;
1090 }
c4cff064 1091
0a19e53c 1092 return 0;
cfb180f3
TZ
1093}
1094
8b372562
TZ
1095static int infix_get_op(struct filter_parse_state *ps, char firstc)
1096{
1097 char nextc = infix_peek(ps);
1098 char opstr[3];
1099 int i;
1100
1101 opstr[0] = firstc;
1102 opstr[1] = nextc;
1103 opstr[2] = '\0';
1104
1105 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1106 if (!strcmp(opstr, ps->ops[i].string)) {
1107 infix_advance(ps);
1108 return ps->ops[i].id;
7ce7e424 1109 }
8b372562
TZ
1110 }
1111
1112 opstr[1] = '\0';
1113
1114 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1115 if (!strcmp(opstr, ps->ops[i].string))
1116 return ps->ops[i].id;
1117 }
1118
1119 return OP_NONE;
1120}
1121
1122static inline void clear_operand_string(struct filter_parse_state *ps)
1123{
1124 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1125 ps->operand.tail = 0;
1126}
1127
1128static inline int append_operand_char(struct filter_parse_state *ps, char c)
1129{
5872144f 1130 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
8b372562
TZ
1131 return -EINVAL;
1132
1133 ps->operand.string[ps->operand.tail++] = c;
1134
1135 return 0;
1136}
1137
1138static int filter_opstack_push(struct filter_parse_state *ps, int op)
1139{
1140 struct opstack_op *opstack_op;
1141
1142 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1143 if (!opstack_op)
1144 return -ENOMEM;
1145
1146 opstack_op->op = op;
1147 list_add(&opstack_op->list, &ps->opstack);
1148
1149 return 0;
1150}
1151
1152static int filter_opstack_empty(struct filter_parse_state *ps)
1153{
1154 return list_empty(&ps->opstack);
1155}
1156
1157static int filter_opstack_top(struct filter_parse_state *ps)
1158{
1159 struct opstack_op *opstack_op;
1160
1161 if (filter_opstack_empty(ps))
1162 return OP_NONE;
1163
1164 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1165
1166 return opstack_op->op;
1167}
1168
1169static int filter_opstack_pop(struct filter_parse_state *ps)
1170{
1171 struct opstack_op *opstack_op;
1172 int op;
1173
1174 if (filter_opstack_empty(ps))
1175 return OP_NONE;
1176
1177 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1178 op = opstack_op->op;
1179 list_del(&opstack_op->list);
1180
1181 kfree(opstack_op);
1182
1183 return op;
1184}
1185
1186static void filter_opstack_clear(struct filter_parse_state *ps)
1187{
1188 while (!filter_opstack_empty(ps))
1189 filter_opstack_pop(ps);
1190}
1191
1192static char *curr_operand(struct filter_parse_state *ps)
1193{
1194 return ps->operand.string;
1195}
1196
1197static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1198{
1199 struct postfix_elt *elt;
1200
1201 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1202 if (!elt)
1203 return -ENOMEM;
1204
1205 elt->op = OP_NONE;
1206 elt->operand = kstrdup(operand, GFP_KERNEL);
1207 if (!elt->operand) {
1208 kfree(elt);
1209 return -ENOMEM;
1210 }
1211
1212 list_add_tail(&elt->list, &ps->postfix);
1213
1214 return 0;
1215}
1216
1217static int postfix_append_op(struct filter_parse_state *ps, int op)
1218{
1219 struct postfix_elt *elt;
1220
1221 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1222 if (!elt)
1223 return -ENOMEM;
1224
1225 elt->op = op;
1226 elt->operand = NULL;
1227
1228 list_add_tail(&elt->list, &ps->postfix);
1229
1230 return 0;
1231}
1232
1233static void postfix_clear(struct filter_parse_state *ps)
1234{
1235 struct postfix_elt *elt;
1236
1237 while (!list_empty(&ps->postfix)) {
1238 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
8b372562 1239 list_del(&elt->list);
8ad80731
LZ
1240 kfree(elt->operand);
1241 kfree(elt);
8b372562
TZ
1242 }
1243}
1244
1245static int filter_parse(struct filter_parse_state *ps)
1246{
5928c3cc 1247 int in_string = 0;
8b372562
TZ
1248 int op, top_op;
1249 char ch;
1250
1251 while ((ch = infix_next(ps))) {
5928c3cc
FW
1252 if (ch == '"') {
1253 in_string ^= 1;
1254 continue;
1255 }
1256
1257 if (in_string)
1258 goto parse_operand;
1259
8b372562
TZ
1260 if (isspace(ch))
1261 continue;
1262
1263 if (is_op_char(ps, ch)) {
1264 op = infix_get_op(ps, ch);
1265 if (op == OP_NONE) {
1266 parse_error(ps, FILT_ERR_INVALID_OP, 0);
7ce7e424
TZ
1267 return -EINVAL;
1268 }
8b372562
TZ
1269
1270 if (strlen(curr_operand(ps))) {
1271 postfix_append_operand(ps, curr_operand(ps));
1272 clear_operand_string(ps);
1273 }
1274
1275 while (!filter_opstack_empty(ps)) {
1276 top_op = filter_opstack_top(ps);
1277 if (!is_precedence_lower(ps, top_op, op)) {
1278 top_op = filter_opstack_pop(ps);
1279 postfix_append_op(ps, top_op);
1280 continue;
1281 }
1282 break;
1283 }
1284
1285 filter_opstack_push(ps, op);
7ce7e424
TZ
1286 continue;
1287 }
8b372562
TZ
1288
1289 if (ch == '(') {
1290 filter_opstack_push(ps, OP_OPEN_PAREN);
1291 continue;
1292 }
1293
1294 if (ch == ')') {
1295 if (strlen(curr_operand(ps))) {
1296 postfix_append_operand(ps, curr_operand(ps));
1297 clear_operand_string(ps);
1298 }
1299
1300 top_op = filter_opstack_pop(ps);
1301 while (top_op != OP_NONE) {
1302 if (top_op == OP_OPEN_PAREN)
1303 break;
1304 postfix_append_op(ps, top_op);
1305 top_op = filter_opstack_pop(ps);
1306 }
1307 if (top_op == OP_NONE) {
1308 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1309 return -EINVAL;
7ce7e424 1310 }
7ce7e424
TZ
1311 continue;
1312 }
5928c3cc 1313parse_operand:
8b372562
TZ
1314 if (append_operand_char(ps, ch)) {
1315 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1316 return -EINVAL;
1317 }
1318 }
1319
1320 if (strlen(curr_operand(ps)))
1321 postfix_append_operand(ps, curr_operand(ps));
1322
1323 while (!filter_opstack_empty(ps)) {
1324 top_op = filter_opstack_pop(ps);
1325 if (top_op == OP_NONE)
1326 break;
1327 if (top_op == OP_OPEN_PAREN) {
1328 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1329 return -EINVAL;
1330 }
1331 postfix_append_op(ps, top_op);
1332 }
1333
1334 return 0;
1335}
1336
81570d9c 1337static struct filter_pred *create_pred(struct filter_parse_state *ps,
9d96cd17 1338 struct ftrace_event_call *call,
81570d9c 1339 int op, char *operand1, char *operand2)
8b372562 1340{
61aaef55 1341 struct ftrace_event_field *field;
81570d9c 1342 static struct filter_pred pred;
8b372562 1343
81570d9c
JO
1344 memset(&pred, 0, sizeof(pred));
1345 pred.op = op;
8b372562 1346
81570d9c
JO
1347 if (op == OP_AND || op == OP_OR)
1348 return &pred;
1349
1350 if (!operand1 || !operand2) {
1351 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
8b372562
TZ
1352 return NULL;
1353 }
1354
b3a8c6fd 1355 field = trace_find_event_field(call, operand1);
61aaef55
JO
1356 if (!field) {
1357 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
8b372562 1358 return NULL;
61aaef55 1359 }
8b372562 1360
81570d9c
JO
1361 strcpy(pred.regex.pattern, operand2);
1362 pred.regex.len = strlen(pred.regex.pattern);
1d0e78e3 1363 pred.field = field;
61aaef55 1364 return init_pred(ps, field, &pred) ? NULL : &pred;
8b372562
TZ
1365}
1366
1367static int check_preds(struct filter_parse_state *ps)
1368{
1369 int n_normal_preds = 0, n_logical_preds = 0;
1370 struct postfix_elt *elt;
1371
1372 list_for_each_entry(elt, &ps->postfix, list) {
1373 if (elt->op == OP_NONE)
1374 continue;
1375
1376 if (elt->op == OP_AND || elt->op == OP_OR) {
1377 n_logical_preds++;
1378 continue;
7ce7e424 1379 }
8b372562 1380 n_normal_preds++;
7ce7e424
TZ
1381 }
1382
8b372562
TZ
1383 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1384 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
bcabd91c
LZ
1385 return -EINVAL;
1386 }
1387
8b372562
TZ
1388 return 0;
1389}
f66578a7 1390
c9c53ca0
SR
1391static int count_preds(struct filter_parse_state *ps)
1392{
1393 struct postfix_elt *elt;
1394 int n_preds = 0;
1395
1396 list_for_each_entry(elt, &ps->postfix, list) {
1397 if (elt->op == OP_NONE)
1398 continue;
1399 n_preds++;
1400 }
1401
1402 return n_preds;
1403}
1404
f03f5979
JO
1405struct check_pred_data {
1406 int count;
1407 int max;
1408};
1409
1410static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1411 int *err, void *data)
1412{
1413 struct check_pred_data *d = data;
1414
1415 if (WARN_ON(d->count++ > d->max)) {
1416 *err = -EINVAL;
1417 return WALK_PRED_ABORT;
1418 }
1419 return WALK_PRED_DEFAULT;
1420}
1421
ec126cac
SR
1422/*
1423 * The tree is walked at filtering of an event. If the tree is not correctly
1424 * built, it may cause an infinite loop. Check here that the tree does
1425 * indeed terminate.
1426 */
1427static int check_pred_tree(struct event_filter *filter,
1428 struct filter_pred *root)
1429{
f03f5979
JO
1430 struct check_pred_data data = {
1431 /*
1432 * The max that we can hit a node is three times.
1433 * Once going down, once coming up from left, and
1434 * once coming up from right. This is more than enough
1435 * since leafs are only hit a single time.
1436 */
1437 .max = 3 * filter->n_preds,
1438 .count = 0,
1439 };
ec126cac 1440
f03f5979
JO
1441 return walk_pred_tree(filter->preds, root,
1442 check_pred_tree_cb, &data);
ec126cac
SR
1443}
1444
c00b060f
JO
1445static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
1446 int *err, void *data)
43cd4145 1447{
c00b060f 1448 int *count = data;
43cd4145 1449
c00b060f
JO
1450 if ((move == MOVE_DOWN) &&
1451 (pred->left == FILTER_PRED_INVALID))
1452 (*count)++;
43cd4145 1453
c00b060f
JO
1454 return WALK_PRED_DEFAULT;
1455}
1456
1457static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1458{
1459 int count = 0, ret;
43cd4145 1460
c00b060f
JO
1461 ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
1462 WARN_ON(ret);
43cd4145
SR
1463 return count;
1464}
1465
96bc293a
JO
1466struct fold_pred_data {
1467 struct filter_pred *root;
1468 int count;
1469 int children;
1470};
1471
1472static int fold_pred_cb(enum move_type move, struct filter_pred *pred,
1473 int *err, void *data)
1474{
1475 struct fold_pred_data *d = data;
1476 struct filter_pred *root = d->root;
1477
1478 if (move != MOVE_DOWN)
1479 return WALK_PRED_DEFAULT;
1480 if (pred->left != FILTER_PRED_INVALID)
1481 return WALK_PRED_DEFAULT;
1482
1483 if (WARN_ON(d->count == d->children)) {
1484 *err = -EINVAL;
1485 return WALK_PRED_ABORT;
1486 }
1487
1488 pred->index &= ~FILTER_PRED_FOLD;
1489 root->ops[d->count++] = pred->index;
1490 return WALK_PRED_DEFAULT;
1491}
1492
43cd4145
SR
1493static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1494{
96bc293a
JO
1495 struct fold_pred_data data = {
1496 .root = root,
1497 .count = 0,
1498 };
43cd4145 1499 int children;
43cd4145
SR
1500
1501 /* No need to keep the fold flag */
1502 root->index &= ~FILTER_PRED_FOLD;
1503
1504 /* If the root is a leaf then do nothing */
1505 if (root->left == FILTER_PRED_INVALID)
1506 return 0;
1507
1508 /* count the children */
1509 children = count_leafs(preds, &preds[root->left]);
1510 children += count_leafs(preds, &preds[root->right]);
1511
47b0edcb 1512 root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
43cd4145
SR
1513 if (!root->ops)
1514 return -ENOMEM;
1515
1516 root->val = children;
96bc293a
JO
1517 data.children = children;
1518 return walk_pred_tree(preds, root, fold_pred_cb, &data);
43cd4145
SR
1519}
1520
1b797fe5
JO
1521static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1522 int *err, void *data)
1523{
1524 struct filter_pred *preds = data;
1525
1526 if (move != MOVE_DOWN)
1527 return WALK_PRED_DEFAULT;
1528 if (!(pred->index & FILTER_PRED_FOLD))
1529 return WALK_PRED_DEFAULT;
1530
1531 *err = fold_pred(preds, pred);
1532 if (*err)
1533 return WALK_PRED_ABORT;
1534
1535 /* eveyrhing below is folded, continue with parent */
1536 return WALK_PRED_PARENT;
1537}
1538
43cd4145
SR
1539/*
1540 * To optimize the processing of the ops, if we have several "ors" or
1541 * "ands" together, we can put them in an array and process them all
1542 * together speeding up the filter logic.
1543 */
1544static int fold_pred_tree(struct event_filter *filter,
1545 struct filter_pred *root)
1546{
1b797fe5
JO
1547 return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
1548 filter->preds);
43cd4145
SR
1549}
1550
fce29d15 1551static int replace_preds(struct ftrace_event_call *call,
6fb2915d 1552 struct event_filter *filter,
8b372562 1553 struct filter_parse_state *ps,
1f9963cb
LZ
1554 char *filter_string,
1555 bool dry_run)
8b372562
TZ
1556{
1557 char *operand1 = NULL, *operand2 = NULL;
1558 struct filter_pred *pred;
ec126cac 1559 struct filter_pred *root;
8b372562 1560 struct postfix_elt *elt;
61e9dea2 1561 struct pred_stack stack = { }; /* init to NULL */
8b372562 1562 int err;
1f9963cb 1563 int n_preds = 0;
8b372562 1564
c9c53ca0
SR
1565 n_preds = count_preds(ps);
1566 if (n_preds >= MAX_FILTER_PRED) {
1567 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1568 return -ENOSPC;
1569 }
1570
8b372562
TZ
1571 err = check_preds(ps);
1572 if (err)
1573 return err;
1574
c9c53ca0 1575 if (!dry_run) {
61e9dea2 1576 err = __alloc_pred_stack(&stack, n_preds);
c9c53ca0
SR
1577 if (err)
1578 return err;
61e9dea2
SR
1579 err = __alloc_preds(filter, n_preds);
1580 if (err)
1581 goto fail;
c9c53ca0
SR
1582 }
1583
1584 n_preds = 0;
8b372562
TZ
1585 list_for_each_entry(elt, &ps->postfix, list) {
1586 if (elt->op == OP_NONE) {
1587 if (!operand1)
1588 operand1 = elt->operand;
1589 else if (!operand2)
1590 operand2 = elt->operand;
1591 else {
1592 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
61e9dea2
SR
1593 err = -EINVAL;
1594 goto fail;
8b372562
TZ
1595 }
1596 continue;
1597 }
1598
c9c53ca0 1599 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1f9963cb 1600 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
61e9dea2
SR
1601 err = -ENOSPC;
1602 goto fail;
1f9963cb
LZ
1603 }
1604
9d96cd17 1605 pred = create_pred(ps, call, elt->op, operand1, operand2);
61e9dea2 1606 if (!pred) {
61aaef55 1607 err = -EINVAL;
61e9dea2
SR
1608 goto fail;
1609 }
61aaef55 1610
9d96cd17
JO
1611 if (!dry_run) {
1612 err = filter_add_pred(ps, filter, pred, &stack);
61aaef55 1613 if (err)
9d96cd17 1614 goto fail;
9d96cd17 1615 }
8b372562
TZ
1616
1617 operand1 = operand2 = NULL;
1618 }
7ce7e424 1619
61e9dea2
SR
1620 if (!dry_run) {
1621 /* We should have one item left on the stack */
1622 pred = __pop_pred_stack(&stack);
1623 if (!pred)
1624 return -EINVAL;
1625 /* This item is where we start from in matching */
ec126cac 1626 root = pred;
61e9dea2
SR
1627 /* Make sure the stack is empty */
1628 pred = __pop_pred_stack(&stack);
1629 if (WARN_ON(pred)) {
1630 err = -EINVAL;
1631 filter->root = NULL;
1632 goto fail;
1633 }
ec126cac
SR
1634 err = check_pred_tree(filter, root);
1635 if (err)
1636 goto fail;
1637
43cd4145
SR
1638 /* Optimize the tree */
1639 err = fold_pred_tree(filter, root);
1640 if (err)
1641 goto fail;
1642
ec126cac
SR
1643 /* We don't set root until we know it works */
1644 barrier();
1645 filter->root = root;
61e9dea2
SR
1646 }
1647
1648 err = 0;
1649fail:
1650 __free_pred_stack(&stack);
1651 return err;
7ce7e424
TZ
1652}
1653
f306cc82
TZ
1654static inline void event_set_filtered_flag(struct ftrace_event_file *file)
1655{
1656 struct ftrace_event_call *call = file->event_call;
1657
1658 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1659 call->flags |= TRACE_EVENT_FL_FILTERED;
1660 else
1661 file->flags |= FTRACE_EVENT_FL_FILTERED;
1662}
1663
1664static inline void event_set_filter(struct ftrace_event_file *file,
1665 struct event_filter *filter)
1666{
1667 struct ftrace_event_call *call = file->event_call;
1668
1669 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1670 rcu_assign_pointer(call->filter, filter);
1671 else
1672 rcu_assign_pointer(file->filter, filter);
1673}
1674
1675static inline void event_clear_filter(struct ftrace_event_file *file)
1676{
1677 struct ftrace_event_call *call = file->event_call;
1678
1679 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1680 RCU_INIT_POINTER(call->filter, NULL);
1681 else
1682 RCU_INIT_POINTER(file->filter, NULL);
1683}
1684
1685static inline void
1686event_set_no_set_filter_flag(struct ftrace_event_file *file)
1687{
1688 struct ftrace_event_call *call = file->event_call;
1689
1690 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1691 call->flags |= TRACE_EVENT_FL_NO_SET_FILTER;
1692 else
1693 file->flags |= FTRACE_EVENT_FL_NO_SET_FILTER;
1694}
1695
1696static inline void
1697event_clear_no_set_filter_flag(struct ftrace_event_file *file)
1698{
1699 struct ftrace_event_call *call = file->event_call;
1700
1701 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1702 call->flags &= ~TRACE_EVENT_FL_NO_SET_FILTER;
1703 else
1704 file->flags &= ~FTRACE_EVENT_FL_NO_SET_FILTER;
1705}
1706
1707static inline bool
1708event_no_set_filter_flag(struct ftrace_event_file *file)
1709{
1710 struct ftrace_event_call *call = file->event_call;
1711
1712 if (file->flags & FTRACE_EVENT_FL_NO_SET_FILTER)
1713 return true;
1714
1715 if ((call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) &&
1716 (call->flags & TRACE_EVENT_FL_NO_SET_FILTER))
1717 return true;
1718
1719 return false;
1720}
1721
75b8e982
SR
1722struct filter_list {
1723 struct list_head list;
1724 struct event_filter *filter;
1725};
1726
fce29d15 1727static int replace_system_preds(struct event_subsystem *system,
f306cc82 1728 struct trace_array *tr,
fce29d15
LZ
1729 struct filter_parse_state *ps,
1730 char *filter_string)
1731{
f306cc82 1732 struct ftrace_event_file *file;
fce29d15 1733 struct ftrace_event_call *call;
75b8e982
SR
1734 struct filter_list *filter_item;
1735 struct filter_list *tmp;
1736 LIST_HEAD(filter_list);
fce29d15 1737 bool fail = true;
a66abe7f 1738 int err;
fce29d15 1739
f306cc82
TZ
1740 list_for_each_entry(file, &tr->events, list) {
1741 call = file->event_call;
8f082018 1742 if (strcmp(call->class->system, system->name) != 0)
fce29d15
LZ
1743 continue;
1744
75b8e982
SR
1745 /*
1746 * Try to see if the filter can be applied
1747 * (filter arg is ignored on dry_run)
1748 */
1749 err = replace_preds(call, NULL, ps, filter_string, true);
fce29d15 1750 if (err)
f306cc82 1751 event_set_no_set_filter_flag(file);
ed0449af 1752 else
f306cc82 1753 event_clear_no_set_filter_flag(file);
0fc3ca9a
SR
1754 }
1755
f306cc82 1756 list_for_each_entry(file, &tr->events, list) {
75b8e982 1757 struct event_filter *filter;
0fc3ca9a 1758
f306cc82
TZ
1759 call = file->event_call;
1760
0fc3ca9a
SR
1761 if (strcmp(call->class->system, system->name) != 0)
1762 continue;
1763
f306cc82 1764 if (event_no_set_filter_flag(file))
ed0449af
LZ
1765 continue;
1766
75b8e982
SR
1767 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1768 if (!filter_item)
1769 goto fail_mem;
0fc3ca9a 1770
75b8e982 1771 list_add_tail(&filter_item->list, &filter_list);
0fc3ca9a 1772
75b8e982
SR
1773 filter_item->filter = __alloc_filter();
1774 if (!filter_item->filter)
1775 goto fail_mem;
1776 filter = filter_item->filter;
0fc3ca9a 1777
75b8e982
SR
1778 /* Can only fail on no memory */
1779 err = replace_filter_string(filter, filter_string);
1780 if (err)
1781 goto fail_mem;
fce29d15 1782
6fb2915d 1783 err = replace_preds(call, filter, ps, filter_string, false);
75b8e982 1784 if (err) {
f306cc82 1785 filter_disable(file);
75b8e982
SR
1786 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1787 append_filter_err(ps, filter);
1788 } else
f306cc82 1789 event_set_filtered_flag(file);
75b8e982
SR
1790 /*
1791 * Regardless of if this returned an error, we still
1792 * replace the filter for the call.
1793 */
f306cc82
TZ
1794 filter = event_filter(file);
1795 event_set_filter(file, filter_item->filter);
75b8e982
SR
1796 filter_item->filter = filter;
1797
fce29d15
LZ
1798 fail = false;
1799 }
1800
0fc3ca9a
SR
1801 if (fail)
1802 goto fail;
1803
75b8e982
SR
1804 /*
1805 * The calls can still be using the old filters.
1806 * Do a synchronize_sched() to ensure all calls are
1807 * done with them before we free them.
1808 */
1809 synchronize_sched();
1810 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1811 __free_filter(filter_item->filter);
1812 list_del(&filter_item->list);
1813 kfree(filter_item);
1814 }
fce29d15 1815 return 0;
0fc3ca9a 1816 fail:
75b8e982
SR
1817 /* No call succeeded */
1818 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1819 list_del(&filter_item->list);
1820 kfree(filter_item);
1821 }
0fc3ca9a
SR
1822 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1823 return -EINVAL;
75b8e982
SR
1824 fail_mem:
1825 /* If any call succeeded, we still need to sync */
1826 if (!fail)
1827 synchronize_sched();
1828 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1829 __free_filter(filter_item->filter);
1830 list_del(&filter_item->list);
1831 kfree(filter_item);
1832 }
1833 return -ENOMEM;
fce29d15
LZ
1834}
1835
38b78eb8
TH
1836static int create_filter_start(char *filter_str, bool set_str,
1837 struct filter_parse_state **psp,
1838 struct event_filter **filterp)
1839{
1840 struct event_filter *filter;
1841 struct filter_parse_state *ps = NULL;
1842 int err = 0;
1843
1844 WARN_ON_ONCE(*psp || *filterp);
1845
1846 /* allocate everything, and if any fails, free all and fail */
1847 filter = __alloc_filter();
1848 if (filter && set_str)
1849 err = replace_filter_string(filter, filter_str);
1850
1851 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1852
1853 if (!filter || !ps || err) {
1854 kfree(ps);
1855 __free_filter(filter);
1856 return -ENOMEM;
1857 }
1858
1859 /* we're committed to creating a new filter */
1860 *filterp = filter;
1861 *psp = ps;
1862
1863 parse_init(ps, filter_ops, filter_str);
1864 err = filter_parse(ps);
1865 if (err && set_str)
1866 append_filter_err(ps, filter);
1867 return err;
1868}
1869
1870static void create_filter_finish(struct filter_parse_state *ps)
1871{
1872 if (ps) {
1873 filter_opstack_clear(ps);
1874 postfix_clear(ps);
1875 kfree(ps);
1876 }
1877}
1878
1879/**
1880 * create_filter - create a filter for a ftrace_event_call
1881 * @call: ftrace_event_call to create a filter for
1882 * @filter_str: filter string
1883 * @set_str: remember @filter_str and enable detailed error in filter
1884 * @filterp: out param for created filter (always updated on return)
1885 *
1886 * Creates a filter for @call with @filter_str. If @set_str is %true,
1887 * @filter_str is copied and recorded in the new filter.
1888 *
1889 * On success, returns 0 and *@filterp points to the new filter. On
1890 * failure, returns -errno and *@filterp may point to %NULL or to a new
1891 * filter. In the latter case, the returned filter contains error
1892 * information if @set_str is %true and the caller is responsible for
1893 * freeing it.
1894 */
1895static int create_filter(struct ftrace_event_call *call,
1896 char *filter_str, bool set_str,
1897 struct event_filter **filterp)
1898{
1899 struct event_filter *filter = NULL;
1900 struct filter_parse_state *ps = NULL;
1901 int err;
1902
1903 err = create_filter_start(filter_str, set_str, &ps, &filter);
1904 if (!err) {
1905 err = replace_preds(call, filter, ps, filter_str, false);
1906 if (err && set_str)
1907 append_filter_err(ps, filter);
1908 }
1909 create_filter_finish(ps);
1910
1911 *filterp = filter;
1912 return err;
1913}
1914
bac5fb97
TZ
1915int create_event_filter(struct ftrace_event_call *call,
1916 char *filter_str, bool set_str,
1917 struct event_filter **filterp)
1918{
1919 return create_filter(call, filter_str, set_str, filterp);
1920}
1921
38b78eb8
TH
1922/**
1923 * create_system_filter - create a filter for an event_subsystem
1924 * @system: event_subsystem to create a filter for
1925 * @filter_str: filter string
1926 * @filterp: out param for created filter (always updated on return)
1927 *
1928 * Identical to create_filter() except that it creates a subsystem filter
1929 * and always remembers @filter_str.
1930 */
1931static int create_system_filter(struct event_subsystem *system,
f306cc82 1932 struct trace_array *tr,
38b78eb8
TH
1933 char *filter_str, struct event_filter **filterp)
1934{
1935 struct event_filter *filter = NULL;
1936 struct filter_parse_state *ps = NULL;
1937 int err;
1938
1939 err = create_filter_start(filter_str, true, &ps, &filter);
1940 if (!err) {
f306cc82 1941 err = replace_system_preds(system, tr, ps, filter_str);
38b78eb8
TH
1942 if (!err) {
1943 /* System filters just show a default message */
1944 kfree(filter->filter_string);
1945 filter->filter_string = NULL;
1946 } else {
1947 append_filter_err(ps, filter);
1948 }
1949 }
1950 create_filter_finish(ps);
1951
1952 *filterp = filter;
1953 return err;
1954}
1955
e2912b09 1956/* caller must hold event_mutex */
f306cc82 1957int apply_event_filter(struct ftrace_event_file *file, char *filter_string)
8b372562 1958{
f306cc82 1959 struct ftrace_event_call *call = file->event_call;
75b8e982 1960 struct event_filter *filter;
e2912b09 1961 int err;
8b372562
TZ
1962
1963 if (!strcmp(strstrip(filter_string), "0")) {
f306cc82
TZ
1964 filter_disable(file);
1965 filter = event_filter(file);
1966
75b8e982 1967 if (!filter)
e2912b09 1968 return 0;
f306cc82
TZ
1969
1970 event_clear_filter(file);
1971
f76690af
SR
1972 /* Make sure the filter is not being used */
1973 synchronize_sched();
75b8e982 1974 __free_filter(filter);
f306cc82 1975
e2912b09 1976 return 0;
8b372562
TZ
1977 }
1978
38b78eb8 1979 err = create_filter(call, filter_string, true, &filter);
8b372562 1980
75b8e982
SR
1981 /*
1982 * Always swap the call filter with the new filter
1983 * even if there was an error. If there was an error
1984 * in the filter, we disable the filter and show the error
1985 * string
1986 */
38b78eb8 1987 if (filter) {
f306cc82 1988 struct event_filter *tmp;
38b78eb8 1989
f306cc82 1990 tmp = event_filter(file);
38b78eb8 1991 if (!err)
f306cc82 1992 event_set_filtered_flag(file);
38b78eb8 1993 else
f306cc82 1994 filter_disable(file);
38b78eb8 1995
f306cc82 1996 event_set_filter(file, filter);
38b78eb8
TH
1997
1998 if (tmp) {
1999 /* Make sure the call is done with the filter */
2000 synchronize_sched();
2001 __free_filter(tmp);
2002 }
75b8e982 2003 }
8b372562
TZ
2004
2005 return err;
2006}
2007
ae63b31e 2008int apply_subsystem_event_filter(struct ftrace_subsystem_dir *dir,
8b372562
TZ
2009 char *filter_string)
2010{
ae63b31e 2011 struct event_subsystem *system = dir->subsystem;
f306cc82 2012 struct trace_array *tr = dir->tr;
75b8e982
SR
2013 struct event_filter *filter;
2014 int err = 0;
8b372562 2015
00e95830 2016 mutex_lock(&event_mutex);
8b372562 2017
e9dbfae5 2018 /* Make sure the system still has events */
ae63b31e 2019 if (!dir->nr_events) {
e9dbfae5
SR
2020 err = -ENODEV;
2021 goto out_unlock;
2022 }
2023
8b372562 2024 if (!strcmp(strstrip(filter_string), "0")) {
f306cc82 2025 filter_free_subsystem_preds(system, tr);
8b372562 2026 remove_filter_string(system->filter);
75b8e982
SR
2027 filter = system->filter;
2028 system->filter = NULL;
2029 /* Ensure all filters are no longer used */
2030 synchronize_sched();
f306cc82 2031 filter_free_subsystem_filters(system, tr);
75b8e982 2032 __free_filter(filter);
a66abe7f 2033 goto out_unlock;
8b372562
TZ
2034 }
2035
f306cc82 2036 err = create_system_filter(system, tr, filter_string, &filter);
38b78eb8
TH
2037 if (filter) {
2038 /*
2039 * No event actually uses the system filter
2040 * we can free it without synchronize_sched().
2041 */
2042 __free_filter(system->filter);
2043 system->filter = filter;
2044 }
8cd995b6 2045out_unlock:
00e95830 2046 mutex_unlock(&event_mutex);
8b372562
TZ
2047
2048 return err;
2049}
7ce7e424 2050
07b139c8 2051#ifdef CONFIG_PERF_EVENTS
6fb2915d
LZ
2052
2053void ftrace_profile_free_filter(struct perf_event *event)
2054{
2055 struct event_filter *filter = event->filter;
2056
2057 event->filter = NULL;
c9c53ca0 2058 __free_filter(filter);
6fb2915d
LZ
2059}
2060
5500fa51
JO
2061struct function_filter_data {
2062 struct ftrace_ops *ops;
2063 int first_filter;
2064 int first_notrace;
2065};
2066
2067#ifdef CONFIG_FUNCTION_TRACER
2068static char **
2069ftrace_function_filter_re(char *buf, int len, int *count)
2070{
2071 char *str, *sep, **re;
2072
2073 str = kstrndup(buf, len, GFP_KERNEL);
2074 if (!str)
2075 return NULL;
2076
2077 /*
2078 * The argv_split function takes white space
2079 * as a separator, so convert ',' into spaces.
2080 */
2081 while ((sep = strchr(str, ',')))
2082 *sep = ' ';
2083
2084 re = argv_split(GFP_KERNEL, str, count);
2085 kfree(str);
2086 return re;
2087}
2088
2089static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2090 int reset, char *re, int len)
2091{
2092 int ret;
2093
2094 if (filter)
2095 ret = ftrace_set_filter(ops, re, len, reset);
2096 else
2097 ret = ftrace_set_notrace(ops, re, len, reset);
2098
2099 return ret;
2100}
2101
2102static int __ftrace_function_set_filter(int filter, char *buf, int len,
2103 struct function_filter_data *data)
2104{
92d8d4a8 2105 int i, re_cnt, ret = -EINVAL;
5500fa51
JO
2106 int *reset;
2107 char **re;
2108
2109 reset = filter ? &data->first_filter : &data->first_notrace;
2110
2111 /*
2112 * The 'ip' field could have multiple filters set, separated
2113 * either by space or comma. We first cut the filter and apply
2114 * all pieces separatelly.
2115 */
2116 re = ftrace_function_filter_re(buf, len, &re_cnt);
2117 if (!re)
2118 return -EINVAL;
2119
2120 for (i = 0; i < re_cnt; i++) {
2121 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2122 re[i], strlen(re[i]));
2123 if (ret)
2124 break;
2125
2126 if (*reset)
2127 *reset = 0;
2128 }
2129
2130 argv_free(re);
2131 return ret;
2132}
2133
2134static int ftrace_function_check_pred(struct filter_pred *pred, int leaf)
2135{
2136 struct ftrace_event_field *field = pred->field;
2137
2138 if (leaf) {
2139 /*
2140 * Check the leaf predicate for function trace, verify:
2141 * - only '==' and '!=' is used
2142 * - the 'ip' field is used
2143 */
2144 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2145 return -EINVAL;
2146
2147 if (strcmp(field->name, "ip"))
2148 return -EINVAL;
2149 } else {
2150 /*
2151 * Check the non leaf predicate for function trace, verify:
2152 * - only '||' is used
2153 */
2154 if (pred->op != OP_OR)
2155 return -EINVAL;
2156 }
2157
2158 return 0;
2159}
2160
2161static int ftrace_function_set_filter_cb(enum move_type move,
2162 struct filter_pred *pred,
2163 int *err, void *data)
2164{
2165 /* Checking the node is valid for function trace. */
2166 if ((move != MOVE_DOWN) ||
2167 (pred->left != FILTER_PRED_INVALID)) {
2168 *err = ftrace_function_check_pred(pred, 0);
2169 } else {
2170 *err = ftrace_function_check_pred(pred, 1);
2171 if (*err)
2172 return WALK_PRED_ABORT;
2173
2174 *err = __ftrace_function_set_filter(pred->op == OP_EQ,
2175 pred->regex.pattern,
2176 pred->regex.len,
2177 data);
2178 }
2179
2180 return (*err) ? WALK_PRED_ABORT : WALK_PRED_DEFAULT;
2181}
2182
2183static int ftrace_function_set_filter(struct perf_event *event,
2184 struct event_filter *filter)
2185{
2186 struct function_filter_data data = {
2187 .first_filter = 1,
2188 .first_notrace = 1,
2189 .ops = &event->ftrace_ops,
2190 };
2191
2192 return walk_pred_tree(filter->preds, filter->root,
2193 ftrace_function_set_filter_cb, &data);
2194}
2195#else
2196static int ftrace_function_set_filter(struct perf_event *event,
2197 struct event_filter *filter)
2198{
2199 return -ENODEV;
2200}
2201#endif /* CONFIG_FUNCTION_TRACER */
2202
6fb2915d
LZ
2203int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2204 char *filter_str)
2205{
2206 int err;
2207 struct event_filter *filter;
3f78f935 2208 struct ftrace_event_call *call;
6fb2915d
LZ
2209
2210 mutex_lock(&event_mutex);
2211
3f78f935 2212 call = event->tp_event;
a66abe7f
IM
2213
2214 err = -EINVAL;
3f78f935 2215 if (!call)
a66abe7f 2216 goto out_unlock;
6fb2915d 2217
a66abe7f 2218 err = -EEXIST;
6fb2915d 2219 if (event->filter)
a66abe7f 2220 goto out_unlock;
6fb2915d 2221
38b78eb8 2222 err = create_filter(call, filter_str, false, &filter);
5500fa51
JO
2223 if (err)
2224 goto free_filter;
2225
2226 if (ftrace_event_is_function(call))
2227 err = ftrace_function_set_filter(event, filter);
38b78eb8 2228 else
5500fa51
JO
2229 event->filter = filter;
2230
2231free_filter:
2232 if (err || ftrace_event_is_function(call))
c9c53ca0 2233 __free_filter(filter);
6fb2915d 2234
a66abe7f 2235out_unlock:
6fb2915d
LZ
2236 mutex_unlock(&event_mutex);
2237
2238 return err;
2239}
2240
07b139c8 2241#endif /* CONFIG_PERF_EVENTS */
6fb2915d 2242
1d0e78e3
JO
2243#ifdef CONFIG_FTRACE_STARTUP_TEST
2244
2245#include <linux/types.h>
2246#include <linux/tracepoint.h>
2247
2248#define CREATE_TRACE_POINTS
2249#include "trace_events_filter_test.h"
2250
1d0e78e3
JO
2251#define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2252{ \
2253 .filter = FILTER, \
2254 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2255 .e = ve, .f = vf, .g = vg, .h = vh }, \
2256 .match = m, \
2257 .not_visited = nvisit, \
2258}
2259#define YES 1
2260#define NO 0
2261
2262static struct test_filter_data_t {
2263 char *filter;
2264 struct ftrace_raw_ftrace_test_filter rec;
2265 int match;
2266 char *not_visited;
2267} test_filter_data[] = {
2268#define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2269 "e == 1 && f == 1 && g == 1 && h == 1"
2270 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2271 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2272 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2273#undef FILTER
2274#define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2275 "e == 1 || f == 1 || g == 1 || h == 1"
2276 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2277 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2278 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2279#undef FILTER
2280#define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2281 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2282 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2283 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2284 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2285 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2286#undef FILTER
2287#define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2288 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2289 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2290 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2291 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2292#undef FILTER
2293#define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2294 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2295 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2296 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2297 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2298#undef FILTER
2299#define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2300 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2301 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2302 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2303 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2304#undef FILTER
2305#define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2306 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2307 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2308 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2309 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2310#undef FILTER
2311#define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2312 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2313 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2314 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2315 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2316};
2317
2318#undef DATA_REC
2319#undef FILTER
2320#undef YES
2321#undef NO
2322
2323#define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2324
2325static int test_pred_visited;
2326
2327static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2328{
2329 struct ftrace_event_field *field = pred->field;
2330
2331 test_pred_visited = 1;
2332 printk(KERN_INFO "\npred visited %s\n", field->name);
2333 return 1;
2334}
2335
2336static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred,
2337 int *err, void *data)
2338{
2339 char *fields = data;
2340
2341 if ((move == MOVE_DOWN) &&
2342 (pred->left == FILTER_PRED_INVALID)) {
2343 struct ftrace_event_field *field = pred->field;
2344
2345 if (!field) {
2346 WARN(1, "all leafs should have field defined");
2347 return WALK_PRED_DEFAULT;
2348 }
2349 if (!strchr(fields, *field->name))
2350 return WALK_PRED_DEFAULT;
2351
2352 WARN_ON(!pred->fn);
2353 pred->fn = test_pred_visited_fn;
2354 }
2355 return WALK_PRED_DEFAULT;
2356}
2357
2358static __init int ftrace_test_event_filter(void)
2359{
2360 int i;
2361
2362 printk(KERN_INFO "Testing ftrace filter: ");
2363
2364 for (i = 0; i < DATA_CNT; i++) {
2365 struct event_filter *filter = NULL;
2366 struct test_filter_data_t *d = &test_filter_data[i];
2367 int err;
2368
38b78eb8
TH
2369 err = create_filter(&event_ftrace_test_filter, d->filter,
2370 false, &filter);
1d0e78e3
JO
2371 if (err) {
2372 printk(KERN_INFO
2373 "Failed to get filter for '%s', err %d\n",
2374 d->filter, err);
38b78eb8 2375 __free_filter(filter);
1d0e78e3
JO
2376 break;
2377 }
2378
86b6ef21
SR
2379 /*
2380 * The preemption disabling is not really needed for self
2381 * tests, but the rcu dereference will complain without it.
2382 */
2383 preempt_disable();
1d0e78e3
JO
2384 if (*d->not_visited)
2385 walk_pred_tree(filter->preds, filter->root,
2386 test_walk_pred_cb,
2387 d->not_visited);
2388
2389 test_pred_visited = 0;
2390 err = filter_match_preds(filter, &d->rec);
86b6ef21 2391 preempt_enable();
1d0e78e3
JO
2392
2393 __free_filter(filter);
2394
2395 if (test_pred_visited) {
2396 printk(KERN_INFO
2397 "Failed, unwanted pred visited for filter %s\n",
2398 d->filter);
2399 break;
2400 }
2401
2402 if (err != d->match) {
2403 printk(KERN_INFO
2404 "Failed to match filter '%s', expected %d\n",
2405 d->filter, d->match);
2406 break;
2407 }
2408 }
2409
2410 if (i == DATA_CNT)
2411 printk(KERN_CONT "OK\n");
2412
2413 return 0;
2414}
2415
2416late_initcall(ftrace_test_event_filter);
2417
2418#endif /* CONFIG_FTRACE_STARTUP_TEST */
This page took 0.315534 seconds and 5 git commands to generate.