xref: /linux/kernel/trace/trace_events_filter.c (revision 3ca933aad0aca463512d2f54a79fc65b8ecb0f48)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_events_filter - generic event filtering
4  *
5  * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
6  */
7 
8 #include <linux/uaccess.h>
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/mutex.h>
12 #include <linux/perf_event.h>
13 #include <linux/slab.h>
14 
15 #include "trace.h"
16 #include "trace_output.h"
17 
18 #define DEFAULT_SYS_FILTER_MESSAGE					\
19 	"### global filter ###\n"					\
20 	"# Use this to set filters for multiple events.\n"		\
21 	"# Only events with the given fields will be affected.\n"	\
22 	"# If no events are modified, an error message will be displayed here"
23 
24 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
25 #define OPS					\
26 	C( OP_GLOB,	"~"  ),			\
27 	C( OP_NE,	"!=" ),			\
28 	C( OP_EQ,	"==" ),			\
29 	C( OP_LE,	"<=" ),			\
30 	C( OP_LT,	"<"  ),			\
31 	C( OP_GE,	">=" ),			\
32 	C( OP_GT,	">"  ),			\
33 	C( OP_BAND,	"&"  ),			\
34 	C( OP_MAX,	NULL )
35 
36 #undef C
37 #define C(a, b)	a
38 
39 enum filter_op_ids { OPS };
40 
41 #undef C
42 #define C(a, b)	b
43 
44 static const char * ops[] = { OPS };
45 
46 enum filter_pred_fn {
47 	FILTER_PRED_FN_NOP,
48 	FILTER_PRED_FN_64,
49 	FILTER_PRED_FN_64_CPUMASK,
50 	FILTER_PRED_FN_S64,
51 	FILTER_PRED_FN_U64,
52 	FILTER_PRED_FN_32,
53 	FILTER_PRED_FN_32_CPUMASK,
54 	FILTER_PRED_FN_S32,
55 	FILTER_PRED_FN_U32,
56 	FILTER_PRED_FN_16,
57 	FILTER_PRED_FN_16_CPUMASK,
58 	FILTER_PRED_FN_S16,
59 	FILTER_PRED_FN_U16,
60 	FILTER_PRED_FN_8,
61 	FILTER_PRED_FN_8_CPUMASK,
62 	FILTER_PRED_FN_S8,
63 	FILTER_PRED_FN_U8,
64 	FILTER_PRED_FN_COMM,
65 	FILTER_PRED_FN_STRING,
66 	FILTER_PRED_FN_STRLOC,
67 	FILTER_PRED_FN_STRRELLOC,
68 	FILTER_PRED_FN_PCHAR_USER,
69 	FILTER_PRED_FN_PCHAR,
70 	FILTER_PRED_FN_CPU,
71 	FILTER_PRED_FN_CPU_CPUMASK,
72 	FILTER_PRED_FN_CPUMASK,
73 	FILTER_PRED_FN_CPUMASK_CPU,
74 	FILTER_PRED_FN_FUNCTION,
75 	FILTER_PRED_FN_,
76 	FILTER_PRED_TEST_VISITED,
77 };
78 
79 struct filter_pred {
80 	struct regex		*regex;
81 	struct cpumask          *mask;
82 	unsigned short		*ops;
83 	struct ftrace_event_field *field;
84 	u64			val;
85 	u64			val2;
86 	enum filter_pred_fn	fn_num;
87 	int			offset;
88 	int			not;
89 	int			op;
90 };
91 
92 /*
93  * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
94  * pred_funcs_##type below must match the order of them above.
95  */
96 #define PRED_FUNC_START			OP_LE
97 #define PRED_FUNC_MAX			(OP_BAND - PRED_FUNC_START)
98 
99 #define ERRORS								\
100 	C(NONE,			"No error"),				\
101 	C(INVALID_OP,		"Invalid operator"),			\
102 	C(TOO_MANY_OPEN,	"Too many '('"),			\
103 	C(TOO_MANY_CLOSE,	"Too few '('"),				\
104 	C(MISSING_QUOTE,	"Missing matching quote"),		\
105 	C(MISSING_BRACE_OPEN,   "Missing '{'"),				\
106 	C(MISSING_BRACE_CLOSE,  "Missing '}'"),				\
107 	C(OPERAND_TOO_LONG,	"Operand too long"),			\
108 	C(EXPECT_STRING,	"Expecting string field"),		\
109 	C(EXPECT_DIGIT,		"Expecting numeric field"),		\
110 	C(ILLEGAL_FIELD_OP,	"Illegal operation for field type"),	\
111 	C(FIELD_NOT_FOUND,	"Field not found"),			\
112 	C(ILLEGAL_INTVAL,	"Illegal integer value"),		\
113 	C(BAD_SUBSYS_FILTER,	"Couldn't find or set field in one of a subsystem's events"), \
114 	C(TOO_MANY_PREDS,	"Too many terms in predicate expression"), \
115 	C(INVALID_FILTER,	"Meaningless filter expression"),	\
116 	C(INVALID_CPULIST,	"Invalid cpulist"),	\
117 	C(IP_FIELD_ONLY,	"Only 'ip' field is supported for function trace"), \
118 	C(INVALID_VALUE,	"Invalid value (did you forget quotes)?"), \
119 	C(NO_FUNCTION,		"Function not found"),			\
120 	C(ERRNO,		"Error"),				\
121 	C(NO_FILTER,		"No filter found")
122 
123 #undef C
124 #define C(a, b)		FILT_ERR_##a
125 
126 enum { ERRORS };
127 
128 #undef C
129 #define C(a, b)		b
130 
131 static const char *err_text[] = { ERRORS };
132 
133 /* Called after a '!' character but "!=" and "!~" are not "not"s */
is_not(const char * str)134 static bool is_not(const char *str)
135 {
136 	switch (str[1]) {
137 	case '=':
138 	case '~':
139 		return false;
140 	}
141 	return true;
142 }
143 
144 /**
145  * struct prog_entry - a singe entry in the filter program
146  * @target:	     Index to jump to on a branch (actually one minus the index)
147  * @when_to_branch:  The value of the result of the predicate to do a branch
148  * @pred:	     The predicate to execute.
149  */
150 struct prog_entry {
151 	int			target;
152 	int			when_to_branch;
153 	struct filter_pred	*pred;
154 };
155 
156 /**
157  * update_preds - assign a program entry a label target
158  * @prog: The program array
159  * @N: The index of the current entry in @prog
160  * @invert: What to assign a program entry for its branch condition
161  *
162  * The program entry at @N has a target that points to the index of a program
163  * entry that can have its target and when_to_branch fields updated.
164  * Update the current program entry denoted by index @N target field to be
165  * that of the updated entry. This will denote the entry to update if
166  * we are processing an "||" after an "&&".
167  */
update_preds(struct prog_entry * prog,int N,int invert)168 static void update_preds(struct prog_entry *prog, int N, int invert)
169 {
170 	int t, s;
171 
172 	t = prog[N].target;
173 	s = prog[t].target;
174 	prog[t].when_to_branch = invert;
175 	prog[t].target = N;
176 	prog[N].target = s;
177 }
178 
179 struct filter_parse_error {
180 	int lasterr;
181 	int lasterr_pos;
182 };
183 
parse_error(struct filter_parse_error * pe,int err,int pos)184 static void parse_error(struct filter_parse_error *pe, int err, int pos)
185 {
186 	pe->lasterr = err;
187 	pe->lasterr_pos = pos;
188 }
189 
190 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
191 			     struct filter_parse_error *pe,
192 			     struct filter_pred **pred);
193 
194 enum {
195 	INVERT		= 1,
196 	PROCESS_AND	= 2,
197 	PROCESS_OR	= 4,
198 };
199 
free_predicate(struct filter_pred * pred)200 static void free_predicate(struct filter_pred *pred)
201 {
202 	if (pred) {
203 		kfree(pred->regex);
204 		kfree(pred->mask);
205 		kfree(pred);
206 	}
207 }
208 
209 /*
210  * Without going into a formal proof, this explains the method that is used in
211  * parsing the logical expressions.
212  *
213  * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
214  * The first pass will convert it into the following program:
215  *
216  * n1: r=a;       l1: if (!r) goto l4;
217  * n2: r=b;       l2: if (!r) goto l4;
218  * n3: r=c; r=!r; l3: if (r) goto l4;
219  * n4: r=g; r=!r; l4: if (r) goto l5;
220  * n5: r=d;       l5: if (r) goto T
221  * n6: r=e;       l6: if (!r) goto l7;
222  * n7: r=f; r=!r; l7: if (!r) goto F
223  * T: return TRUE
224  * F: return FALSE
225  *
226  * To do this, we use a data structure to represent each of the above
227  * predicate and conditions that has:
228  *
229  *  predicate, when_to_branch, invert, target
230  *
231  * The "predicate" will hold the function to determine the result "r".
232  * The "when_to_branch" denotes what "r" should be if a branch is to be taken
233  * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
234  * The "invert" holds whether the value should be reversed before testing.
235  * The "target" contains the label "l#" to jump to.
236  *
237  * A stack is created to hold values when parentheses are used.
238  *
239  * To simplify the logic, the labels will start at 0 and not 1.
240  *
241  * The possible invert values are 1 and 0. The number of "!"s that are in scope
242  * before the predicate determines the invert value, if the number is odd then
243  * the invert value is 1 and 0 otherwise. This means the invert value only
244  * needs to be toggled when a new "!" is introduced compared to what is stored
245  * on the stack, where parentheses were used.
246  *
247  * The top of the stack and "invert" are initialized to zero.
248  *
249  * ** FIRST PASS **
250  *
251  * #1 A loop through all the tokens is done:
252  *
253  * #2 If the token is an "(", the stack is push, and the current stack value
254  *    gets the current invert value, and the loop continues to the next token.
255  *    The top of the stack saves the "invert" value to keep track of what
256  *    the current inversion is. As "!(a && !b || c)" would require all
257  *    predicates being affected separately by the "!" before the parentheses.
258  *    And that would end up being equivalent to "(!a || b) && !c"
259  *
260  * #3 If the token is an "!", the current "invert" value gets inverted, and
261  *    the loop continues. Note, if the next token is a predicate, then
262  *    this "invert" value is only valid for the current program entry,
263  *    and does not affect other predicates later on.
264  *
265  * The only other acceptable token is the predicate string.
266  *
267  * #4 A new entry into the program is added saving: the predicate and the
268  *    current value of "invert". The target is currently assigned to the
269  *    previous program index (this will not be its final value).
270  *
271  * #5 We now enter another loop and look at the next token. The only valid
272  *    tokens are ")", "&&", "||" or end of the input string "\0".
273  *
274  * #6 The invert variable is reset to the current value saved on the top of
275  *    the stack.
276  *
277  * #7 The top of the stack holds not only the current invert value, but also
278  *    if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
279  *    precedence than "||". That is "a && b || c && d" is equivalent to
280  *    "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
281  *    to be processed. This is the case if an "&&" was the last token. If it was
282  *    then we call update_preds(). This takes the program, the current index in
283  *    the program, and the current value of "invert".  More will be described
284  *    below about this function.
285  *
286  * #8 If the next token is "&&" then we set a flag in the top of the stack
287  *    that denotes that "&&" needs to be processed, break out of this loop
288  *    and continue with the outer loop.
289  *
290  * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
291  *    This is called with the program, the current index in the program, but
292  *    this time with an inverted value of "invert" (that is !invert). This is
293  *    because the value taken will become the "when_to_branch" value of the
294  *    program.
295  *    Note, this is called when the next token is not an "&&". As stated before,
296  *    "&&" takes higher precedence, and "||" should not be processed yet if the
297  *    next logical operation is "&&".
298  *
299  * #10 If the next token is "||" then we set a flag in the top of the stack
300  *     that denotes that "||" needs to be processed, break out of this loop
301  *     and continue with the outer loop.
302  *
303  * #11 If this is the end of the input string "\0" then we break out of both
304  *     loops.
305  *
306  * #12 Otherwise, the next token is ")", where we pop the stack and continue
307  *     this inner loop.
308  *
309  * Now to discuss the update_pred() function, as that is key to the setting up
310  * of the program. Remember the "target" of the program is initialized to the
311  * previous index and not the "l" label. The target holds the index into the
312  * program that gets affected by the operand. Thus if we have something like
313  *  "a || b && c", when we process "a" the target will be "-1" (undefined).
314  * When we process "b", its target is "0", which is the index of "a", as that's
315  * the predicate that is affected by "||". But because the next token after "b"
316  * is "&&" we don't call update_preds(). Instead continue to "c". As the
317  * next token after "c" is not "&&" but the end of input, we first process the
318  * "&&" by calling update_preds() for the "&&" then we process the "||" by
319  * calling updates_preds() with the values for processing "||".
320  *
321  * What does that mean? What update_preds() does is to first save the "target"
322  * of the program entry indexed by the current program entry's "target"
323  * (remember the "target" is initialized to previous program entry), and then
324  * sets that "target" to the current index which represents the label "l#".
325  * That entry's "when_to_branch" is set to the value passed in (the "invert"
326  * or "!invert"). Then it sets the current program entry's target to the saved
327  * "target" value (the old value of the program that had its "target" updated
328  * to the label).
329  *
330  * Looking back at "a || b && c", we have the following steps:
331  *  "a"  - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
332  *  "||" - flag that we need to process "||"; continue outer loop
333  *  "b"  - prog[1] = { "b", X, 0 }
334  *  "&&" - flag that we need to process "&&"; continue outer loop
335  * (Notice we did not process "||")
336  *  "c"  - prog[2] = { "c", X, 1 }
337  *  update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
338  *    t = prog[2].target; // t = 1
339  *    s = prog[t].target; // s = 0
340  *    prog[t].target = 2; // Set target to "l2"
341  *    prog[t].when_to_branch = 0;
342  *    prog[2].target = s;
343  * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
344  *    t = prog[2].target; // t = 0
345  *    s = prog[t].target; // s = -1
346  *    prog[t].target = 2; // Set target to "l2"
347  *    prog[t].when_to_branch = 1;
348  *    prog[2].target = s;
349  *
350  * #13 Which brings us to the final step of the first pass, which is to set
351  *     the last program entry's when_to_branch and target, which will be
352  *     when_to_branch = 0; target = N; ( the label after the program entry after
353  *     the last program entry processed above).
354  *
355  * If we denote "TRUE" to be the entry after the last program entry processed,
356  * and "FALSE" the program entry after that, we are now done with the first
357  * pass.
358  *
359  * Making the above "a || b && c" have a program of:
360  *  prog[0] = { "a", 1, 2 }
361  *  prog[1] = { "b", 0, 2 }
362  *  prog[2] = { "c", 0, 3 }
363  *
364  * Which translates into:
365  * n0: r = a; l0: if (r) goto l2;
366  * n1: r = b; l1: if (!r) goto l2;
367  * n2: r = c; l2: if (!r) goto l3;  // Which is the same as "goto F;"
368  * T: return TRUE; l3:
369  * F: return FALSE
370  *
371  * Although, after the first pass, the program is correct, it is
372  * inefficient. The simple sample of "a || b && c" could be easily been
373  * converted into:
374  * n0: r = a; if (r) goto T
375  * n1: r = b; if (!r) goto F
376  * n2: r = c; if (!r) goto F
377  * T: return TRUE;
378  * F: return FALSE;
379  *
380  * The First Pass is over the input string. The next too passes are over
381  * the program itself.
382  *
383  * ** SECOND PASS **
384  *
385  * Which brings us to the second pass. If a jump to a label has the
386  * same condition as that label, it can instead jump to its target.
387  * The original example of "a && !(!b || (c && g)) || d || e && !f"
388  * where the first pass gives us:
389  *
390  * n1: r=a;       l1: if (!r) goto l4;
391  * n2: r=b;       l2: if (!r) goto l4;
392  * n3: r=c; r=!r; l3: if (r) goto l4;
393  * n4: r=g; r=!r; l4: if (r) goto l5;
394  * n5: r=d;       l5: if (r) goto T
395  * n6: r=e;       l6: if (!r) goto l7;
396  * n7: r=f; r=!r; l7: if (!r) goto F:
397  * T: return TRUE;
398  * F: return FALSE
399  *
400  * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
401  * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
402  * to go directly to T. To accomplish this, we start from the last
403  * entry in the program and work our way back. If the target of the entry
404  * has the same "when_to_branch" then we could use that entry's target.
405  * Doing this, the above would end up as:
406  *
407  * n1: r=a;       l1: if (!r) goto l4;
408  * n2: r=b;       l2: if (!r) goto l4;
409  * n3: r=c; r=!r; l3: if (r) goto T;
410  * n4: r=g; r=!r; l4: if (r) goto T;
411  * n5: r=d;       l5: if (r) goto T;
412  * n6: r=e;       l6: if (!r) goto F;
413  * n7: r=f; r=!r; l7: if (!r) goto F;
414  * T: return TRUE
415  * F: return FALSE
416  *
417  * In that same pass, if the "when_to_branch" doesn't match, we can simply
418  * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
419  * where "l4: if (r) goto T;", then we can convert l2 to be:
420  * "l2: if (!r) goto n5;".
421  *
422  * This will have the second pass give us:
423  * n1: r=a;       l1: if (!r) goto n5;
424  * n2: r=b;       l2: if (!r) goto n5;
425  * n3: r=c; r=!r; l3: if (r) goto T;
426  * n4: r=g; r=!r; l4: if (r) goto T;
427  * n5: r=d;       l5: if (r) goto T
428  * n6: r=e;       l6: if (!r) goto F;
429  * n7: r=f; r=!r; l7: if (!r) goto F
430  * T: return TRUE
431  * F: return FALSE
432  *
433  * Notice, all the "l#" labels are no longer used, and they can now
434  * be discarded.
435  *
436  * ** THIRD PASS **
437  *
438  * For the third pass we deal with the inverts. As they simply just
439  * make the "when_to_branch" get inverted, a simple loop over the
440  * program to that does: "when_to_branch ^= invert;" will do the
441  * job, leaving us with:
442  * n1: r=a; if (!r) goto n5;
443  * n2: r=b; if (!r) goto n5;
444  * n3: r=c: if (!r) goto T;
445  * n4: r=g; if (!r) goto T;
446  * n5: r=d; if (r) goto T
447  * n6: r=e; if (!r) goto F;
448  * n7: r=f; if (r) goto F
449  * T: return TRUE
450  * F: return FALSE
451  *
452  * As "r = a; if (!r) goto n5;" is obviously the same as
453  * "if (!a) goto n5;" without doing anything we can interpret the
454  * program as:
455  * n1: if (!a) goto n5;
456  * n2: if (!b) goto n5;
457  * n3: if (!c) goto T;
458  * n4: if (!g) goto T;
459  * n5: if (d) goto T
460  * n6: if (!e) goto F;
461  * n7: if (f) goto F
462  * T: return TRUE
463  * F: return FALSE
464  *
465  * Since the inverts are discarded at the end, there's no reason to store
466  * them in the program array (and waste memory). A separate array to hold
467  * the inverts is used and freed at the end.
468  */
469 static struct prog_entry *
predicate_parse(const char * str,int nr_parens,int nr_preds,parse_pred_fn parse_pred,void * data,struct filter_parse_error * pe)470 predicate_parse(const char *str, int nr_parens, int nr_preds,
471 		parse_pred_fn parse_pred, void *data,
472 		struct filter_parse_error *pe)
473 {
474 	struct prog_entry *prog_stack;
475 	struct prog_entry *prog;
476 	const char *ptr = str;
477 	char *inverts = NULL;
478 	int *op_stack;
479 	int *top;
480 	int invert = 0;
481 	int ret = -ENOMEM;
482 	int len;
483 	int N = 0;
484 	int i;
485 
486 	nr_preds += 2; /* For TRUE and FALSE */
487 
488 	op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
489 	if (!op_stack)
490 		return ERR_PTR(-ENOMEM);
491 	prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
492 	if (!prog_stack) {
493 		parse_error(pe, -ENOMEM, 0);
494 		goto out_free;
495 	}
496 	inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
497 	if (!inverts) {
498 		parse_error(pe, -ENOMEM, 0);
499 		goto out_free;
500 	}
501 
502 	top = op_stack;
503 	prog = prog_stack;
504 	*top = 0;
505 
506 	/* First pass */
507 	while (*ptr) {						/* #1 */
508 		const char *next = ptr++;
509 
510 		if (isspace(*next))
511 			continue;
512 
513 		switch (*next) {
514 		case '(':					/* #2 */
515 			if (top - op_stack > nr_parens) {
516 				ret = -EINVAL;
517 				goto out_free;
518 			}
519 			*(++top) = invert;
520 			continue;
521 		case '!':					/* #3 */
522 			if (!is_not(next))
523 				break;
524 			invert = !invert;
525 			continue;
526 		}
527 
528 		if (N >= nr_preds) {
529 			parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
530 			goto out_free;
531 		}
532 
533 		inverts[N] = invert;				/* #4 */
534 		prog[N].target = N-1;
535 
536 		len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
537 		if (len < 0) {
538 			ret = len;
539 			goto out_free;
540 		}
541 		ptr = next + len;
542 
543 		N++;
544 
545 		ret = -1;
546 		while (1) {					/* #5 */
547 			next = ptr++;
548 			if (isspace(*next))
549 				continue;
550 
551 			switch (*next) {
552 			case ')':
553 			case '\0':
554 				break;
555 			case '&':
556 			case '|':
557 				/* accepting only "&&" or "||" */
558 				if (next[1] == next[0]) {
559 					ptr++;
560 					break;
561 				}
562 				fallthrough;
563 			default:
564 				parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
565 					    next - str);
566 				goto out_free;
567 			}
568 
569 			invert = *top & INVERT;
570 
571 			if (*top & PROCESS_AND) {		/* #7 */
572 				update_preds(prog, N - 1, invert);
573 				*top &= ~PROCESS_AND;
574 			}
575 			if (*next == '&') {			/* #8 */
576 				*top |= PROCESS_AND;
577 				break;
578 			}
579 			if (*top & PROCESS_OR) {		/* #9 */
580 				update_preds(prog, N - 1, !invert);
581 				*top &= ~PROCESS_OR;
582 			}
583 			if (*next == '|') {			/* #10 */
584 				*top |= PROCESS_OR;
585 				break;
586 			}
587 			if (!*next)				/* #11 */
588 				goto out;
589 
590 			if (top == op_stack) {
591 				ret = -1;
592 				/* Too few '(' */
593 				parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
594 				goto out_free;
595 			}
596 			top--;					/* #12 */
597 		}
598 	}
599  out:
600 	if (top != op_stack) {
601 		/* Too many '(' */
602 		parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
603 		goto out_free;
604 	}
605 
606 	if (!N) {
607 		/* No program? */
608 		ret = -EINVAL;
609 		parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
610 		goto out_free;
611 	}
612 
613 	prog[N].pred = NULL;					/* #13 */
614 	prog[N].target = 1;		/* TRUE */
615 	prog[N+1].pred = NULL;
616 	prog[N+1].target = 0;		/* FALSE */
617 	prog[N-1].target = N;
618 	prog[N-1].when_to_branch = false;
619 
620 	/* Second Pass */
621 	for (i = N-1 ; i--; ) {
622 		int target = prog[i].target;
623 		if (prog[i].when_to_branch == prog[target].when_to_branch)
624 			prog[i].target = prog[target].target;
625 	}
626 
627 	/* Third Pass */
628 	for (i = 0; i < N; i++) {
629 		invert = inverts[i] ^ prog[i].when_to_branch;
630 		prog[i].when_to_branch = invert;
631 		/* Make sure the program always moves forward */
632 		if (WARN_ON(prog[i].target <= i)) {
633 			ret = -EINVAL;
634 			goto out_free;
635 		}
636 	}
637 
638 	kfree(op_stack);
639 	kfree(inverts);
640 	return prog;
641 out_free:
642 	kfree(op_stack);
643 	kfree(inverts);
644 	if (prog_stack) {
645 		for (i = 0; prog_stack[i].pred; i++)
646 			free_predicate(prog_stack[i].pred);
647 		kfree(prog_stack);
648 	}
649 	return ERR_PTR(ret);
650 }
651 
652 static inline int
do_filter_cpumask(int op,const struct cpumask * mask,const struct cpumask * cmp)653 do_filter_cpumask(int op, const struct cpumask *mask, const struct cpumask *cmp)
654 {
655 	switch (op) {
656 	case OP_EQ:
657 		return cpumask_equal(mask, cmp);
658 	case OP_NE:
659 		return !cpumask_equal(mask, cmp);
660 	case OP_BAND:
661 		return cpumask_intersects(mask, cmp);
662 	default:
663 		return 0;
664 	}
665 }
666 
667 /* Optimisation of do_filter_cpumask() for scalar fields */
668 static inline int
do_filter_scalar_cpumask(int op,unsigned int cpu,const struct cpumask * mask)669 do_filter_scalar_cpumask(int op, unsigned int cpu, const struct cpumask *mask)
670 {
671 	/*
672 	 * Per the weight-of-one cpumask optimisations, the mask passed in this
673 	 * function has a weight >= 2, so it is never equal to a single scalar.
674 	 */
675 	switch (op) {
676 	case OP_EQ:
677 		return false;
678 	case OP_NE:
679 		return true;
680 	case OP_BAND:
681 		return cpumask_test_cpu(cpu, mask);
682 	default:
683 		return 0;
684 	}
685 }
686 
687 static inline int
do_filter_cpumask_scalar(int op,const struct cpumask * mask,unsigned int cpu)688 do_filter_cpumask_scalar(int op, const struct cpumask *mask, unsigned int cpu)
689 {
690 	switch (op) {
691 	case OP_EQ:
692 		return cpumask_test_cpu(cpu, mask) &&
693 			cpumask_nth(1, mask) >= nr_cpu_ids;
694 	case OP_NE:
695 		return !cpumask_test_cpu(cpu, mask) ||
696 			cpumask_nth(1, mask) < nr_cpu_ids;
697 	case OP_BAND:
698 		return cpumask_test_cpu(cpu, mask);
699 	default:
700 		return 0;
701 	}
702 }
703 
704 enum pred_cmp_types {
705 	PRED_CMP_TYPE_NOP,
706 	PRED_CMP_TYPE_LT,
707 	PRED_CMP_TYPE_LE,
708 	PRED_CMP_TYPE_GT,
709 	PRED_CMP_TYPE_GE,
710 	PRED_CMP_TYPE_BAND,
711 };
712 
713 #define DEFINE_COMPARISON_PRED(type)					\
714 static int filter_pred_##type(struct filter_pred *pred, void *event)	\
715 {									\
716 	switch (pred->op) {						\
717 	case OP_LT: {							\
718 		type *addr = (type *)(event + pred->offset);		\
719 		type val = (type)pred->val;				\
720 		return *addr < val;					\
721 	}								\
722 	case OP_LE: {					\
723 		type *addr = (type *)(event + pred->offset);		\
724 		type val = (type)pred->val;				\
725 		return *addr <= val;					\
726 	}								\
727 	case OP_GT: {					\
728 		type *addr = (type *)(event + pred->offset);		\
729 		type val = (type)pred->val;				\
730 		return *addr > val;					\
731 	}								\
732 	case OP_GE: {					\
733 		type *addr = (type *)(event + pred->offset);		\
734 		type val = (type)pred->val;				\
735 		return *addr >= val;					\
736 	}								\
737 	case OP_BAND: {					\
738 		type *addr = (type *)(event + pred->offset);		\
739 		type val = (type)pred->val;				\
740 		return !!(*addr & val);					\
741 	}								\
742 	default:							\
743 		return 0;						\
744 	}								\
745 }
746 
747 #define DEFINE_CPUMASK_COMPARISON_PRED(size)					\
748 static int filter_pred_##size##_cpumask(struct filter_pred *pred, void *event)	\
749 {										\
750 	u##size *addr = (u##size *)(event + pred->offset);			\
751 	unsigned int cpu = *addr;						\
752 										\
753 	if (cpu >= nr_cpu_ids)							\
754 		return 0;							\
755 										\
756 	return do_filter_scalar_cpumask(pred->op, cpu, pred->mask);		\
757 }
758 
759 #define DEFINE_EQUALITY_PRED(size)					\
760 static int filter_pred_##size(struct filter_pred *pred, void *event)	\
761 {									\
762 	u##size *addr = (u##size *)(event + pred->offset);		\
763 	u##size val = (u##size)pred->val;				\
764 	int match;							\
765 									\
766 	match = (val == *addr) ^ pred->not;				\
767 									\
768 	return match;							\
769 }
770 
771 DEFINE_COMPARISON_PRED(s64);
772 DEFINE_COMPARISON_PRED(u64);
773 DEFINE_COMPARISON_PRED(s32);
774 DEFINE_COMPARISON_PRED(u32);
775 DEFINE_COMPARISON_PRED(s16);
776 DEFINE_COMPARISON_PRED(u16);
777 DEFINE_COMPARISON_PRED(s8);
778 DEFINE_COMPARISON_PRED(u8);
779 
780 DEFINE_CPUMASK_COMPARISON_PRED(64);
781 DEFINE_CPUMASK_COMPARISON_PRED(32);
782 DEFINE_CPUMASK_COMPARISON_PRED(16);
783 DEFINE_CPUMASK_COMPARISON_PRED(8);
784 
785 DEFINE_EQUALITY_PRED(64);
786 DEFINE_EQUALITY_PRED(32);
787 DEFINE_EQUALITY_PRED(16);
788 DEFINE_EQUALITY_PRED(8);
789 
790 /* user space strings temp buffer */
791 #define USTRING_BUF_SIZE	1024
792 
793 struct ustring_buffer {
794 	char		buffer[USTRING_BUF_SIZE];
795 };
796 
797 static __percpu struct ustring_buffer *ustring_per_cpu;
798 
test_string(char * str)799 static __always_inline char *test_string(char *str)
800 {
801 	struct ustring_buffer *ubuf;
802 	char *kstr;
803 
804 	if (!ustring_per_cpu)
805 		return NULL;
806 
807 	ubuf = this_cpu_ptr(ustring_per_cpu);
808 	kstr = ubuf->buffer;
809 
810 	/* For safety, do not trust the string pointer */
811 	if (strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE) < 0)
812 		return NULL;
813 	return kstr;
814 }
815 
test_ustring(char * str)816 static __always_inline char *test_ustring(char *str)
817 {
818 	struct ustring_buffer *ubuf;
819 	char __user *ustr;
820 	char *kstr;
821 
822 	if (!ustring_per_cpu)
823 		return NULL;
824 
825 	ubuf = this_cpu_ptr(ustring_per_cpu);
826 	kstr = ubuf->buffer;
827 
828 	/* user space address? */
829 	ustr = (char __user *)str;
830 	if (strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE) < 0)
831 		return NULL;
832 
833 	return kstr;
834 }
835 
836 /* Filter predicate for fixed sized arrays of characters */
filter_pred_string(struct filter_pred * pred,void * event)837 static int filter_pred_string(struct filter_pred *pred, void *event)
838 {
839 	char *addr = (char *)(event + pred->offset);
840 	int cmp, match;
841 
842 	cmp = pred->regex->match(addr, pred->regex, pred->regex->field_len);
843 
844 	match = cmp ^ pred->not;
845 
846 	return match;
847 }
848 
filter_pchar(struct filter_pred * pred,char * str)849 static __always_inline int filter_pchar(struct filter_pred *pred, char *str)
850 {
851 	int cmp, match;
852 	int len;
853 
854 	len = strlen(str) + 1;	/* including tailing '\0' */
855 	cmp = pred->regex->match(str, pred->regex, len);
856 
857 	match = cmp ^ pred->not;
858 
859 	return match;
860 }
861 /* Filter predicate for char * pointers */
filter_pred_pchar(struct filter_pred * pred,void * event)862 static int filter_pred_pchar(struct filter_pred *pred, void *event)
863 {
864 	char **addr = (char **)(event + pred->offset);
865 	char *str;
866 
867 	str = test_string(*addr);
868 	if (!str)
869 		return 0;
870 
871 	return filter_pchar(pred, str);
872 }
873 
874 /* Filter predicate for char * pointers in user space*/
filter_pred_pchar_user(struct filter_pred * pred,void * event)875 static int filter_pred_pchar_user(struct filter_pred *pred, void *event)
876 {
877 	char **addr = (char **)(event + pred->offset);
878 	char *str;
879 
880 	str = test_ustring(*addr);
881 	if (!str)
882 		return 0;
883 
884 	return filter_pchar(pred, str);
885 }
886 
887 /*
888  * Filter predicate for dynamic sized arrays of characters.
889  * These are implemented through a list of strings at the end
890  * of the entry.
891  * Also each of these strings have a field in the entry which
892  * contains its offset from the beginning of the entry.
893  * We have then first to get this field, dereference it
894  * and add it to the address of the entry, and at last we have
895  * the address of the string.
896  */
filter_pred_strloc(struct filter_pred * pred,void * event)897 static int filter_pred_strloc(struct filter_pred *pred, void *event)
898 {
899 	u32 str_item = *(u32 *)(event + pred->offset);
900 	int str_loc = str_item & 0xffff;
901 	int str_len = str_item >> 16;
902 	char *addr = (char *)(event + str_loc);
903 	int cmp, match;
904 
905 	cmp = pred->regex->match(addr, pred->regex, str_len);
906 
907 	match = cmp ^ pred->not;
908 
909 	return match;
910 }
911 
912 /*
913  * Filter predicate for relative dynamic sized arrays of characters.
914  * These are implemented through a list of strings at the end
915  * of the entry as same as dynamic string.
916  * The difference is that the relative one records the location offset
917  * from the field itself, not the event entry.
918  */
filter_pred_strrelloc(struct filter_pred * pred,void * event)919 static int filter_pred_strrelloc(struct filter_pred *pred, void *event)
920 {
921 	u32 *item = (u32 *)(event + pred->offset);
922 	u32 str_item = *item;
923 	int str_loc = str_item & 0xffff;
924 	int str_len = str_item >> 16;
925 	char *addr = (char *)(&item[1]) + str_loc;
926 	int cmp, match;
927 
928 	cmp = pred->regex->match(addr, pred->regex, str_len);
929 
930 	match = cmp ^ pred->not;
931 
932 	return match;
933 }
934 
935 /* Filter predicate for CPUs. */
filter_pred_cpu(struct filter_pred * pred,void * event)936 static int filter_pred_cpu(struct filter_pred *pred, void *event)
937 {
938 	int cpu, cmp;
939 
940 	cpu = raw_smp_processor_id();
941 	cmp = pred->val;
942 
943 	switch (pred->op) {
944 	case OP_EQ:
945 		return cpu == cmp;
946 	case OP_NE:
947 		return cpu != cmp;
948 	case OP_LT:
949 		return cpu < cmp;
950 	case OP_LE:
951 		return cpu <= cmp;
952 	case OP_GT:
953 		return cpu > cmp;
954 	case OP_GE:
955 		return cpu >= cmp;
956 	default:
957 		return 0;
958 	}
959 }
960 
961 /* Filter predicate for current CPU vs user-provided cpumask */
filter_pred_cpu_cpumask(struct filter_pred * pred,void * event)962 static int filter_pred_cpu_cpumask(struct filter_pred *pred, void *event)
963 {
964 	int cpu = raw_smp_processor_id();
965 
966 	return do_filter_scalar_cpumask(pred->op, cpu, pred->mask);
967 }
968 
969 /* Filter predicate for cpumask field vs user-provided cpumask */
filter_pred_cpumask(struct filter_pred * pred,void * event)970 static int filter_pred_cpumask(struct filter_pred *pred, void *event)
971 {
972 	u32 item = *(u32 *)(event + pred->offset);
973 	int loc = item & 0xffff;
974 	const struct cpumask *mask = (event + loc);
975 	const struct cpumask *cmp = pred->mask;
976 
977 	return do_filter_cpumask(pred->op, mask, cmp);
978 }
979 
980 /* Filter predicate for cpumask field vs user-provided scalar  */
filter_pred_cpumask_cpu(struct filter_pred * pred,void * event)981 static int filter_pred_cpumask_cpu(struct filter_pred *pred, void *event)
982 {
983 	u32 item = *(u32 *)(event + pred->offset);
984 	int loc = item & 0xffff;
985 	const struct cpumask *mask = (event + loc);
986 	unsigned int cpu = pred->val;
987 
988 	return do_filter_cpumask_scalar(pred->op, mask, cpu);
989 }
990 
991 /* Filter predicate for COMM. */
filter_pred_comm(struct filter_pred * pred,void * event)992 static int filter_pred_comm(struct filter_pred *pred, void *event)
993 {
994 	int cmp;
995 
996 	cmp = pred->regex->match(current->comm, pred->regex,
997 				TASK_COMM_LEN);
998 	return cmp ^ pred->not;
999 }
1000 
1001 /* Filter predicate for functions. */
filter_pred_function(struct filter_pred * pred,void * event)1002 static int filter_pred_function(struct filter_pred *pred, void *event)
1003 {
1004 	unsigned long *addr = (unsigned long *)(event + pred->offset);
1005 	unsigned long start = (unsigned long)pred->val;
1006 	unsigned long end = (unsigned long)pred->val2;
1007 	int ret = *addr >= start && *addr < end;
1008 
1009 	return pred->op == OP_EQ ? ret : !ret;
1010 }
1011 
1012 /*
1013  * regex_match_foo - Basic regex callbacks
1014  *
1015  * @str: the string to be searched
1016  * @r:   the regex structure containing the pattern string
1017  * @len: the length of the string to be searched (including '\0')
1018  *
1019  * Note:
1020  * - @str might not be NULL-terminated if it's of type DYN_STRING
1021  *   RDYN_STRING, or STATIC_STRING, unless @len is zero.
1022  */
1023 
regex_match_full(char * str,struct regex * r,int len)1024 static int regex_match_full(char *str, struct regex *r, int len)
1025 {
1026 	/* len of zero means str is dynamic and ends with '\0' */
1027 	if (!len)
1028 		return strcmp(str, r->pattern) == 0;
1029 
1030 	return strncmp(str, r->pattern, len) == 0;
1031 }
1032 
regex_match_front(char * str,struct regex * r,int len)1033 static int regex_match_front(char *str, struct regex *r, int len)
1034 {
1035 	if (len && len < r->len)
1036 		return 0;
1037 
1038 	return strncmp(str, r->pattern, r->len) == 0;
1039 }
1040 
regex_match_middle(char * str,struct regex * r,int len)1041 static int regex_match_middle(char *str, struct regex *r, int len)
1042 {
1043 	if (!len)
1044 		return strstr(str, r->pattern) != NULL;
1045 
1046 	return strnstr(str, r->pattern, len) != NULL;
1047 }
1048 
regex_match_end(char * str,struct regex * r,int len)1049 static int regex_match_end(char *str, struct regex *r, int len)
1050 {
1051 	int strlen = len - 1;
1052 
1053 	if (strlen >= r->len &&
1054 	    memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1055 		return 1;
1056 	return 0;
1057 }
1058 
regex_match_glob(char * str,struct regex * r,int len __maybe_unused)1059 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
1060 {
1061 	if (glob_match(r->pattern, str))
1062 		return 1;
1063 	return 0;
1064 }
1065 
1066 /**
1067  * filter_parse_regex - parse a basic regex
1068  * @buff:   the raw regex
1069  * @len:    length of the regex
1070  * @search: will point to the beginning of the string to compare
1071  * @not:    tell whether the match will have to be inverted
1072  *
1073  * This passes in a buffer containing a regex and this function will
1074  * set search to point to the search part of the buffer and
1075  * return the type of search it is (see enum above).
1076  * This does modify buff.
1077  *
1078  * Returns enum type.
1079  *  search returns the pointer to use for comparison.
1080  *  not returns 1 if buff started with a '!'
1081  *     0 otherwise.
1082  */
filter_parse_regex(char * buff,int len,char ** search,int * not)1083 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1084 {
1085 	int type = MATCH_FULL;
1086 	int i;
1087 
1088 	if (buff[0] == '!') {
1089 		*not = 1;
1090 		buff++;
1091 		len--;
1092 	} else
1093 		*not = 0;
1094 
1095 	*search = buff;
1096 
1097 	if (isdigit(buff[0]))
1098 		return MATCH_INDEX;
1099 
1100 	for (i = 0; i < len; i++) {
1101 		if (buff[i] == '*') {
1102 			if (!i) {
1103 				type = MATCH_END_ONLY;
1104 			} else if (i == len - 1) {
1105 				if (type == MATCH_END_ONLY)
1106 					type = MATCH_MIDDLE_ONLY;
1107 				else
1108 					type = MATCH_FRONT_ONLY;
1109 				buff[i] = 0;
1110 				break;
1111 			} else {	/* pattern continues, use full glob */
1112 				return MATCH_GLOB;
1113 			}
1114 		} else if (strchr("[?\\", buff[i])) {
1115 			return MATCH_GLOB;
1116 		}
1117 	}
1118 	if (buff[0] == '*')
1119 		*search = buff + 1;
1120 
1121 	return type;
1122 }
1123 
filter_build_regex(struct filter_pred * pred)1124 static void filter_build_regex(struct filter_pred *pred)
1125 {
1126 	struct regex *r = pred->regex;
1127 	char *search;
1128 	enum regex_type type = MATCH_FULL;
1129 
1130 	if (pred->op == OP_GLOB) {
1131 		type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
1132 		r->len = strlen(search);
1133 		memmove(r->pattern, search, r->len+1);
1134 	}
1135 
1136 	switch (type) {
1137 	/* MATCH_INDEX should not happen, but if it does, match full */
1138 	case MATCH_INDEX:
1139 	case MATCH_FULL:
1140 		r->match = regex_match_full;
1141 		break;
1142 	case MATCH_FRONT_ONLY:
1143 		r->match = regex_match_front;
1144 		break;
1145 	case MATCH_MIDDLE_ONLY:
1146 		r->match = regex_match_middle;
1147 		break;
1148 	case MATCH_END_ONLY:
1149 		r->match = regex_match_end;
1150 		break;
1151 	case MATCH_GLOB:
1152 		r->match = regex_match_glob;
1153 		break;
1154 	}
1155 }
1156 
1157 
1158 #ifdef CONFIG_FTRACE_STARTUP_TEST
1159 static int test_pred_visited_fn(struct filter_pred *pred, void *event);
1160 #else
test_pred_visited_fn(struct filter_pred * pred,void * event)1161 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
1162 {
1163 	return 0;
1164 }
1165 #endif
1166 
1167 
1168 static int filter_pred_fn_call(struct filter_pred *pred, void *event);
1169 
1170 /* return 1 if event matches, 0 otherwise (discard) */
filter_match_preds(struct event_filter * filter,void * rec)1171 int filter_match_preds(struct event_filter *filter, void *rec)
1172 {
1173 	struct prog_entry *prog;
1174 	int i;
1175 
1176 	/* no filter is considered a match */
1177 	if (!filter)
1178 		return 1;
1179 
1180 	/* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
1181 	prog = rcu_dereference_raw(filter->prog);
1182 	if (!prog)
1183 		return 1;
1184 
1185 	for (i = 0; prog[i].pred; i++) {
1186 		struct filter_pred *pred = prog[i].pred;
1187 		int match = filter_pred_fn_call(pred, rec);
1188 		if (match == prog[i].when_to_branch)
1189 			i = prog[i].target;
1190 	}
1191 	return prog[i].target;
1192 }
1193 EXPORT_SYMBOL_GPL(filter_match_preds);
1194 
remove_filter_string(struct event_filter * filter)1195 static void remove_filter_string(struct event_filter *filter)
1196 {
1197 	if (!filter)
1198 		return;
1199 
1200 	kfree(filter->filter_string);
1201 	filter->filter_string = NULL;
1202 }
1203 
append_filter_err(struct trace_array * tr,struct filter_parse_error * pe,struct event_filter * filter)1204 static void append_filter_err(struct trace_array *tr,
1205 			      struct filter_parse_error *pe,
1206 			      struct event_filter *filter)
1207 {
1208 	struct trace_seq *s;
1209 	int pos = pe->lasterr_pos;
1210 	char *buf;
1211 	int len;
1212 
1213 	if (WARN_ON(!filter->filter_string))
1214 		return;
1215 
1216 	s = kmalloc(sizeof(*s), GFP_KERNEL);
1217 	if (!s)
1218 		return;
1219 	trace_seq_init(s);
1220 
1221 	len = strlen(filter->filter_string);
1222 	if (pos > len)
1223 		pos = len;
1224 
1225 	/* indexing is off by one */
1226 	if (pos)
1227 		pos++;
1228 
1229 	trace_seq_puts(s, filter->filter_string);
1230 	if (pe->lasterr > 0) {
1231 		trace_seq_printf(s, "\n%*s", pos, "^");
1232 		trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
1233 		tracing_log_err(tr, "event filter parse error",
1234 				filter->filter_string, err_text,
1235 				pe->lasterr, pe->lasterr_pos);
1236 	} else {
1237 		trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
1238 		tracing_log_err(tr, "event filter parse error",
1239 				filter->filter_string, err_text,
1240 				FILT_ERR_ERRNO, 0);
1241 	}
1242 	trace_seq_putc(s, 0);
1243 	buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
1244 	if (buf) {
1245 		kfree(filter->filter_string);
1246 		filter->filter_string = buf;
1247 	}
1248 	kfree(s);
1249 }
1250 
event_filter(struct trace_event_file * file)1251 static inline struct event_filter *event_filter(struct trace_event_file *file)
1252 {
1253 	return rcu_dereference_protected(file->filter,
1254 					 lockdep_is_held(&event_mutex));
1255 
1256 }
1257 
1258 /* caller must hold event_mutex */
print_event_filter(struct trace_event_file * file,struct trace_seq * s)1259 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
1260 {
1261 	struct event_filter *filter = event_filter(file);
1262 
1263 	if (filter && filter->filter_string)
1264 		trace_seq_printf(s, "%s\n", filter->filter_string);
1265 	else
1266 		trace_seq_puts(s, "none\n");
1267 }
1268 
print_subsystem_event_filter(struct event_subsystem * system,struct trace_seq * s)1269 void print_subsystem_event_filter(struct event_subsystem *system,
1270 				  struct trace_seq *s)
1271 {
1272 	struct event_filter *filter;
1273 
1274 	mutex_lock(&event_mutex);
1275 	filter = system->filter;
1276 	if (filter && filter->filter_string)
1277 		trace_seq_printf(s, "%s\n", filter->filter_string);
1278 	else
1279 		trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1280 	mutex_unlock(&event_mutex);
1281 }
1282 
free_prog(struct event_filter * filter)1283 static void free_prog(struct event_filter *filter)
1284 {
1285 	struct prog_entry *prog;
1286 	int i;
1287 
1288 	prog = rcu_access_pointer(filter->prog);
1289 	if (!prog)
1290 		return;
1291 
1292 	for (i = 0; prog[i].pred; i++)
1293 		free_predicate(prog[i].pred);
1294 	kfree(prog);
1295 }
1296 
filter_disable(struct trace_event_file * file)1297 static void filter_disable(struct trace_event_file *file)
1298 {
1299 	unsigned long old_flags = file->flags;
1300 
1301 	file->flags &= ~EVENT_FILE_FL_FILTERED;
1302 
1303 	if (old_flags != file->flags)
1304 		trace_buffered_event_disable();
1305 }
1306 
__free_filter(struct event_filter * filter)1307 static void __free_filter(struct event_filter *filter)
1308 {
1309 	if (!filter)
1310 		return;
1311 
1312 	free_prog(filter);
1313 	kfree(filter->filter_string);
1314 	kfree(filter);
1315 }
1316 
free_event_filter(struct event_filter * filter)1317 void free_event_filter(struct event_filter *filter)
1318 {
1319 	__free_filter(filter);
1320 }
1321 
__remove_filter(struct trace_event_file * file)1322 static inline void __remove_filter(struct trace_event_file *file)
1323 {
1324 	filter_disable(file);
1325 	remove_filter_string(event_filter(file));
1326 }
1327 
filter_free_subsystem_preds(struct trace_subsystem_dir * dir,struct trace_array * tr)1328 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1329 					struct trace_array *tr)
1330 {
1331 	struct trace_event_file *file;
1332 
1333 	list_for_each_entry(file, &tr->events, list) {
1334 		if (file->system != dir)
1335 			continue;
1336 		__remove_filter(file);
1337 	}
1338 }
1339 
1340 struct filter_list {
1341 	struct list_head	list;
1342 	struct event_filter	*filter;
1343 };
1344 
1345 struct filter_head {
1346 	struct list_head	list;
1347 	struct rcu_head		rcu;
1348 };
1349 
1350 
free_filter_list(struct rcu_head * rhp)1351 static void free_filter_list(struct rcu_head *rhp)
1352 {
1353 	struct filter_head *filter_list = container_of(rhp, struct filter_head, rcu);
1354 	struct filter_list *filter_item, *tmp;
1355 
1356 	list_for_each_entry_safe(filter_item, tmp, &filter_list->list, list) {
1357 		__free_filter(filter_item->filter);
1358 		list_del(&filter_item->list);
1359 		kfree(filter_item);
1360 	}
1361 	kfree(filter_list);
1362 }
1363 
free_filter_list_tasks(struct rcu_head * rhp)1364 static void free_filter_list_tasks(struct rcu_head *rhp)
1365 {
1366 	call_rcu(rhp, free_filter_list);
1367 }
1368 
1369 /*
1370  * The tracepoint_synchronize_unregister() is a double rcu call.
1371  * It calls synchronize_rcu_tasks_trace() followed by synchronize_rcu().
1372  * Instead of waiting for it, simply call these via the call_rcu*()
1373  * variants.
1374  */
delay_free_filter(struct filter_head * head)1375 static void delay_free_filter(struct filter_head *head)
1376 {
1377 	call_rcu_tasks_trace(&head->rcu, free_filter_list_tasks);
1378 }
1379 
try_delay_free_filter(struct event_filter * filter)1380 static void try_delay_free_filter(struct event_filter *filter)
1381 {
1382 	struct filter_head *head;
1383 	struct filter_list *item;
1384 
1385 	head = kmalloc(sizeof(*head), GFP_KERNEL);
1386 	if (!head)
1387 		goto free_now;
1388 
1389 	INIT_LIST_HEAD(&head->list);
1390 
1391 	item = kmalloc(sizeof(*item), GFP_KERNEL);
1392 	if (!item) {
1393 		kfree(head);
1394 		goto free_now;
1395 	}
1396 
1397 	item->filter = filter;
1398 	list_add_tail(&item->list, &head->list);
1399 	delay_free_filter(head);
1400 	return;
1401 
1402  free_now:
1403 	/* Make sure the filter is not being used */
1404 	tracepoint_synchronize_unregister();
1405 	__free_filter(filter);
1406 }
1407 
__free_subsystem_filter(struct trace_event_file * file)1408 static inline void __free_subsystem_filter(struct trace_event_file *file)
1409 {
1410 	__free_filter(event_filter(file));
1411 	file->filter = NULL;
1412 }
1413 
event_set_filter(struct trace_event_file * file,struct event_filter * filter)1414 static inline void event_set_filter(struct trace_event_file *file,
1415 				    struct event_filter *filter)
1416 {
1417 	rcu_assign_pointer(file->filter, filter);
1418 }
1419 
event_clear_filter(struct trace_event_file * file)1420 static inline void event_clear_filter(struct trace_event_file *file)
1421 {
1422 	RCU_INIT_POINTER(file->filter, NULL);
1423 }
1424 
filter_free_subsystem_filters(struct trace_subsystem_dir * dir,struct trace_array * tr,struct event_filter * filter)1425 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1426 					  struct trace_array *tr,
1427 					  struct event_filter *filter)
1428 {
1429 	struct trace_event_file *file;
1430 	struct filter_head *head;
1431 	struct filter_list *item;
1432 
1433 	head = kmalloc(sizeof(*head), GFP_KERNEL);
1434 	if (!head)
1435 		goto free_now;
1436 
1437 	INIT_LIST_HEAD(&head->list);
1438 
1439 	item = kmalloc(sizeof(*item), GFP_KERNEL);
1440 	if (!item)
1441 		goto free_now;
1442 
1443 	item->filter = filter;
1444 	list_add_tail(&item->list, &head->list);
1445 
1446 	list_for_each_entry(file, &tr->events, list) {
1447 		if (file->system != dir)
1448 			continue;
1449 		item = kmalloc(sizeof(*item), GFP_KERNEL);
1450 		if (!item)
1451 			goto free_now;
1452 		item->filter = event_filter(file);
1453 		list_add_tail(&item->list, &head->list);
1454 		event_clear_filter(file);
1455 	}
1456 
1457 	delay_free_filter(head);
1458 	return;
1459  free_now:
1460 	tracepoint_synchronize_unregister();
1461 
1462 	if (head)
1463 		free_filter_list(&head->rcu);
1464 
1465 	list_for_each_entry(file, &tr->events, list) {
1466 		if (file->system != dir || !file->filter)
1467 			continue;
1468 		__free_subsystem_filter(file);
1469 	}
1470 	__free_filter(filter);
1471 }
1472 
filter_assign_type(const char * type)1473 int filter_assign_type(const char *type)
1474 {
1475 	if (strstr(type, "__data_loc")) {
1476 		if (strstr(type, "char"))
1477 			return FILTER_DYN_STRING;
1478 		if (strstr(type, "cpumask_t"))
1479 			return FILTER_CPUMASK;
1480 	}
1481 
1482 	if (strstr(type, "__rel_loc") && strstr(type, "char"))
1483 		return FILTER_RDYN_STRING;
1484 
1485 	if (strchr(type, '[') && strstr(type, "char"))
1486 		return FILTER_STATIC_STRING;
1487 
1488 	if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1489 		return FILTER_PTR_STRING;
1490 
1491 	return FILTER_OTHER;
1492 }
1493 
select_comparison_fn(enum filter_op_ids op,int field_size,int field_is_signed)1494 static enum filter_pred_fn select_comparison_fn(enum filter_op_ids op,
1495 						int field_size, int field_is_signed)
1496 {
1497 	enum filter_pred_fn fn = FILTER_PRED_FN_NOP;
1498 	int pred_func_index = -1;
1499 
1500 	switch (op) {
1501 	case OP_EQ:
1502 	case OP_NE:
1503 		break;
1504 	default:
1505 		if (WARN_ON_ONCE(op < PRED_FUNC_START))
1506 			return fn;
1507 		pred_func_index = op - PRED_FUNC_START;
1508 		if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1509 			return fn;
1510 	}
1511 
1512 	switch (field_size) {
1513 	case 8:
1514 		if (pred_func_index < 0)
1515 			fn = FILTER_PRED_FN_64;
1516 		else if (field_is_signed)
1517 			fn = FILTER_PRED_FN_S64;
1518 		else
1519 			fn = FILTER_PRED_FN_U64;
1520 		break;
1521 	case 4:
1522 		if (pred_func_index < 0)
1523 			fn = FILTER_PRED_FN_32;
1524 		else if (field_is_signed)
1525 			fn = FILTER_PRED_FN_S32;
1526 		else
1527 			fn = FILTER_PRED_FN_U32;
1528 		break;
1529 	case 2:
1530 		if (pred_func_index < 0)
1531 			fn = FILTER_PRED_FN_16;
1532 		else if (field_is_signed)
1533 			fn = FILTER_PRED_FN_S16;
1534 		else
1535 			fn = FILTER_PRED_FN_U16;
1536 		break;
1537 	case 1:
1538 		if (pred_func_index < 0)
1539 			fn = FILTER_PRED_FN_8;
1540 		else if (field_is_signed)
1541 			fn = FILTER_PRED_FN_S8;
1542 		else
1543 			fn = FILTER_PRED_FN_U8;
1544 		break;
1545 	}
1546 
1547 	return fn;
1548 }
1549 
1550 
filter_pred_fn_call(struct filter_pred * pred,void * event)1551 static int filter_pred_fn_call(struct filter_pred *pred, void *event)
1552 {
1553 	switch (pred->fn_num) {
1554 	case FILTER_PRED_FN_64:
1555 		return filter_pred_64(pred, event);
1556 	case FILTER_PRED_FN_64_CPUMASK:
1557 		return filter_pred_64_cpumask(pred, event);
1558 	case FILTER_PRED_FN_S64:
1559 		return filter_pred_s64(pred, event);
1560 	case FILTER_PRED_FN_U64:
1561 		return filter_pred_u64(pred, event);
1562 	case FILTER_PRED_FN_32:
1563 		return filter_pred_32(pred, event);
1564 	case FILTER_PRED_FN_32_CPUMASK:
1565 		return filter_pred_32_cpumask(pred, event);
1566 	case FILTER_PRED_FN_S32:
1567 		return filter_pred_s32(pred, event);
1568 	case FILTER_PRED_FN_U32:
1569 		return filter_pred_u32(pred, event);
1570 	case FILTER_PRED_FN_16:
1571 		return filter_pred_16(pred, event);
1572 	case FILTER_PRED_FN_16_CPUMASK:
1573 		return filter_pred_16_cpumask(pred, event);
1574 	case FILTER_PRED_FN_S16:
1575 		return filter_pred_s16(pred, event);
1576 	case FILTER_PRED_FN_U16:
1577 		return filter_pred_u16(pred, event);
1578 	case FILTER_PRED_FN_8:
1579 		return filter_pred_8(pred, event);
1580 	case FILTER_PRED_FN_8_CPUMASK:
1581 		return filter_pred_8_cpumask(pred, event);
1582 	case FILTER_PRED_FN_S8:
1583 		return filter_pred_s8(pred, event);
1584 	case FILTER_PRED_FN_U8:
1585 		return filter_pred_u8(pred, event);
1586 	case FILTER_PRED_FN_COMM:
1587 		return filter_pred_comm(pred, event);
1588 	case FILTER_PRED_FN_STRING:
1589 		return filter_pred_string(pred, event);
1590 	case FILTER_PRED_FN_STRLOC:
1591 		return filter_pred_strloc(pred, event);
1592 	case FILTER_PRED_FN_STRRELLOC:
1593 		return filter_pred_strrelloc(pred, event);
1594 	case FILTER_PRED_FN_PCHAR_USER:
1595 		return filter_pred_pchar_user(pred, event);
1596 	case FILTER_PRED_FN_PCHAR:
1597 		return filter_pred_pchar(pred, event);
1598 	case FILTER_PRED_FN_CPU:
1599 		return filter_pred_cpu(pred, event);
1600 	case FILTER_PRED_FN_CPU_CPUMASK:
1601 		return filter_pred_cpu_cpumask(pred, event);
1602 	case FILTER_PRED_FN_CPUMASK:
1603 		return filter_pred_cpumask(pred, event);
1604 	case FILTER_PRED_FN_CPUMASK_CPU:
1605 		return filter_pred_cpumask_cpu(pred, event);
1606 	case FILTER_PRED_FN_FUNCTION:
1607 		return filter_pred_function(pred, event);
1608 	case FILTER_PRED_TEST_VISITED:
1609 		return test_pred_visited_fn(pred, event);
1610 	default:
1611 		return 0;
1612 	}
1613 }
1614 
1615 /* Called when a predicate is encountered by predicate_parse() */
parse_pred(const char * str,void * data,int pos,struct filter_parse_error * pe,struct filter_pred ** pred_ptr)1616 static int parse_pred(const char *str, void *data,
1617 		      int pos, struct filter_parse_error *pe,
1618 		      struct filter_pred **pred_ptr)
1619 {
1620 	struct trace_event_call *call = data;
1621 	struct ftrace_event_field *field;
1622 	struct filter_pred *pred = NULL;
1623 	unsigned long offset;
1624 	unsigned long size;
1625 	unsigned long ip;
1626 	char num_buf[24];	/* Big enough to hold an address */
1627 	char *field_name;
1628 	char *name;
1629 	bool function = false;
1630 	bool ustring = false;
1631 	char q;
1632 	u64 val;
1633 	int len;
1634 	int ret;
1635 	int op;
1636 	int s;
1637 	int i = 0;
1638 
1639 	/* First find the field to associate to */
1640 	while (isspace(str[i]))
1641 		i++;
1642 	s = i;
1643 
1644 	while (isalnum(str[i]) || str[i] == '_')
1645 		i++;
1646 
1647 	len = i - s;
1648 
1649 	if (!len)
1650 		return -1;
1651 
1652 	field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1653 	if (!field_name)
1654 		return -ENOMEM;
1655 
1656 	/* Make sure that the field exists */
1657 
1658 	field = trace_find_event_field(call, field_name);
1659 	kfree(field_name);
1660 	if (!field) {
1661 		parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1662 		return -EINVAL;
1663 	}
1664 
1665 	/* See if the field is a user space string */
1666 	if ((len = str_has_prefix(str + i, ".ustring"))) {
1667 		ustring = true;
1668 		i += len;
1669 	}
1670 
1671 	/* See if the field is a kernel function name */
1672 	if ((len = str_has_prefix(str + i, ".function"))) {
1673 		function = true;
1674 		i += len;
1675 	}
1676 
1677 	while (isspace(str[i]))
1678 		i++;
1679 
1680 	/* Make sure this op is supported */
1681 	for (op = 0; ops[op]; op++) {
1682 		/* This is why '<=' must come before '<' in ops[] */
1683 		if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1684 			break;
1685 	}
1686 
1687 	if (!ops[op]) {
1688 		parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1689 		goto err_free;
1690 	}
1691 
1692 	i += strlen(ops[op]);
1693 
1694 	while (isspace(str[i]))
1695 		i++;
1696 
1697 	s = i;
1698 
1699 	pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1700 	if (!pred)
1701 		return -ENOMEM;
1702 
1703 	pred->field = field;
1704 	pred->offset = field->offset;
1705 	pred->op = op;
1706 
1707 	if (function) {
1708 		/* The field must be the same size as long */
1709 		if (field->size != sizeof(long)) {
1710 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1711 			goto err_free;
1712 		}
1713 
1714 		/* Function only works with '==' or '!=' and an unquoted string */
1715 		switch (op) {
1716 		case OP_NE:
1717 		case OP_EQ:
1718 			break;
1719 		default:
1720 			parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1721 			goto err_free;
1722 		}
1723 
1724 		if (isdigit(str[i])) {
1725 			/* We allow 0xDEADBEEF */
1726 			while (isalnum(str[i]))
1727 				i++;
1728 
1729 			len = i - s;
1730 			/* 0xfeedfacedeadbeef is 18 chars max */
1731 			if (len >= sizeof(num_buf)) {
1732 				parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1733 				goto err_free;
1734 			}
1735 
1736 			memcpy(num_buf, str + s, len);
1737 			num_buf[len] = 0;
1738 
1739 			ret = kstrtoul(num_buf, 0, &ip);
1740 			if (ret) {
1741 				parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1742 				goto err_free;
1743 			}
1744 		} else {
1745 			s = i;
1746 			for (; str[i] && !isspace(str[i]); i++)
1747 				;
1748 
1749 			len = i - s;
1750 			name = kmemdup_nul(str + s, len, GFP_KERNEL);
1751 			if (!name)
1752 				goto err_mem;
1753 			ip = kallsyms_lookup_name(name);
1754 			kfree(name);
1755 			if (!ip) {
1756 				parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i);
1757 				goto err_free;
1758 			}
1759 		}
1760 
1761 		/* Now find the function start and end address */
1762 		if (!kallsyms_lookup_size_offset(ip, &size, &offset)) {
1763 			parse_error(pe, FILT_ERR_NO_FUNCTION, pos + i);
1764 			goto err_free;
1765 		}
1766 
1767 		pred->fn_num = FILTER_PRED_FN_FUNCTION;
1768 		pred->val = ip - offset;
1769 		pred->val2 = pred->val + size;
1770 
1771 	} else if (ftrace_event_is_function(call)) {
1772 		/*
1773 		 * Perf does things different with function events.
1774 		 * It only allows an "ip" field, and expects a string.
1775 		 * But the string does not need to be surrounded by quotes.
1776 		 * If it is a string, the assigned function as a nop,
1777 		 * (perf doesn't use it) and grab everything.
1778 		 */
1779 		if (strcmp(field->name, "ip") != 0) {
1780 			parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1781 			goto err_free;
1782 		}
1783 		pred->fn_num = FILTER_PRED_FN_NOP;
1784 
1785 		/*
1786 		 * Quotes are not required, but if they exist then we need
1787 		 * to read them till we hit a matching one.
1788 		 */
1789 		if (str[i] == '\'' || str[i] == '"')
1790 			q = str[i];
1791 		else
1792 			q = 0;
1793 
1794 		for (i++; str[i]; i++) {
1795 			if (q && str[i] == q)
1796 				break;
1797 			if (!q && (str[i] == ')' || str[i] == '&' ||
1798 				   str[i] == '|'))
1799 				break;
1800 		}
1801 		/* Skip quotes */
1802 		if (q)
1803 			s++;
1804 		len = i - s;
1805 		if (len >= MAX_FILTER_STR_VAL) {
1806 			parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1807 			goto err_free;
1808 		}
1809 
1810 		pred->regex = kzalloc(sizeof(*pred->regex), GFP_KERNEL);
1811 		if (!pred->regex)
1812 			goto err_mem;
1813 		pred->regex->len = len;
1814 		memcpy(pred->regex->pattern, str + s, len);
1815 		pred->regex->pattern[len] = 0;
1816 
1817 	} else if (!strncmp(str + i, "CPUS", 4)) {
1818 		unsigned int maskstart;
1819 		bool single;
1820 		char *tmp;
1821 
1822 		switch (field->filter_type) {
1823 		case FILTER_CPUMASK:
1824 		case FILTER_CPU:
1825 		case FILTER_OTHER:
1826 			break;
1827 		default:
1828 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1829 			goto err_free;
1830 		}
1831 
1832 		switch (op) {
1833 		case OP_EQ:
1834 		case OP_NE:
1835 		case OP_BAND:
1836 			break;
1837 		default:
1838 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1839 			goto err_free;
1840 		}
1841 
1842 		/* Skip CPUS */
1843 		i += 4;
1844 		if (str[i++] != '{') {
1845 			parse_error(pe, FILT_ERR_MISSING_BRACE_OPEN, pos + i);
1846 			goto err_free;
1847 		}
1848 		maskstart = i;
1849 
1850 		/* Walk the cpulist until closing } */
1851 		for (; str[i] && str[i] != '}'; i++)
1852 			;
1853 
1854 		if (str[i] != '}') {
1855 			parse_error(pe, FILT_ERR_MISSING_BRACE_CLOSE, pos + i);
1856 			goto err_free;
1857 		}
1858 
1859 		if (maskstart == i) {
1860 			parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i);
1861 			goto err_free;
1862 		}
1863 
1864 		/* Copy the cpulist between { and } */
1865 		tmp = kmalloc((i - maskstart) + 1, GFP_KERNEL);
1866 		if (!tmp)
1867 			goto err_mem;
1868 
1869 		strscpy(tmp, str + maskstart, (i - maskstart) + 1);
1870 		pred->mask = kzalloc(cpumask_size(), GFP_KERNEL);
1871 		if (!pred->mask) {
1872 			kfree(tmp);
1873 			goto err_mem;
1874 		}
1875 
1876 		/* Now parse it */
1877 		if (cpulist_parse(tmp, pred->mask)) {
1878 			kfree(tmp);
1879 			parse_error(pe, FILT_ERR_INVALID_CPULIST, pos + i);
1880 			goto err_free;
1881 		}
1882 		kfree(tmp);
1883 
1884 		/* Move along */
1885 		i++;
1886 
1887 		/*
1888 		 * Optimisation: if the user-provided mask has a weight of one
1889 		 * then we can treat it as a scalar input.
1890 		 */
1891 		single = cpumask_weight(pred->mask) == 1;
1892 		if (single) {
1893 			pred->val = cpumask_first(pred->mask);
1894 			kfree(pred->mask);
1895 			pred->mask = NULL;
1896 		}
1897 
1898 		if (field->filter_type == FILTER_CPUMASK) {
1899 			pred->fn_num = single ?
1900 				FILTER_PRED_FN_CPUMASK_CPU :
1901 				FILTER_PRED_FN_CPUMASK;
1902 		} else if (field->filter_type == FILTER_CPU) {
1903 			if (single) {
1904 				if (pred->op == OP_BAND)
1905 					pred->op = OP_EQ;
1906 
1907 				pred->fn_num = FILTER_PRED_FN_CPU;
1908 			} else {
1909 				pred->fn_num = FILTER_PRED_FN_CPU_CPUMASK;
1910 			}
1911 		} else if (single) {
1912 			if (pred->op == OP_BAND)
1913 				pred->op = OP_EQ;
1914 
1915 			pred->fn_num = select_comparison_fn(pred->op, field->size, false);
1916 			if (pred->op == OP_NE)
1917 				pred->not = 1;
1918 		} else {
1919 			switch (field->size) {
1920 			case 8:
1921 				pred->fn_num = FILTER_PRED_FN_64_CPUMASK;
1922 				break;
1923 			case 4:
1924 				pred->fn_num = FILTER_PRED_FN_32_CPUMASK;
1925 				break;
1926 			case 2:
1927 				pred->fn_num = FILTER_PRED_FN_16_CPUMASK;
1928 				break;
1929 			case 1:
1930 				pred->fn_num = FILTER_PRED_FN_8_CPUMASK;
1931 				break;
1932 			}
1933 		}
1934 
1935 	/* This is either a string, or an integer */
1936 	} else if (str[i] == '\'' || str[i] == '"') {
1937 		char q = str[i];
1938 
1939 		/* Make sure the op is OK for strings */
1940 		switch (op) {
1941 		case OP_NE:
1942 			pred->not = 1;
1943 			fallthrough;
1944 		case OP_GLOB:
1945 		case OP_EQ:
1946 			break;
1947 		default:
1948 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1949 			goto err_free;
1950 		}
1951 
1952 		/* Make sure the field is OK for strings */
1953 		if (!is_string_field(field)) {
1954 			parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1955 			goto err_free;
1956 		}
1957 
1958 		for (i++; str[i]; i++) {
1959 			if (str[i] == q)
1960 				break;
1961 		}
1962 		if (!str[i]) {
1963 			parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1964 			goto err_free;
1965 		}
1966 
1967 		/* Skip quotes */
1968 		s++;
1969 		len = i - s;
1970 		if (len >= MAX_FILTER_STR_VAL) {
1971 			parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1972 			goto err_free;
1973 		}
1974 
1975 		pred->regex = kzalloc(sizeof(*pred->regex), GFP_KERNEL);
1976 		if (!pred->regex)
1977 			goto err_mem;
1978 		pred->regex->len = len;
1979 		memcpy(pred->regex->pattern, str + s, len);
1980 		pred->regex->pattern[len] = 0;
1981 
1982 		filter_build_regex(pred);
1983 
1984 		if (field->filter_type == FILTER_COMM) {
1985 			pred->fn_num = FILTER_PRED_FN_COMM;
1986 
1987 		} else if (field->filter_type == FILTER_STATIC_STRING) {
1988 			pred->fn_num = FILTER_PRED_FN_STRING;
1989 			pred->regex->field_len = field->size;
1990 
1991 		} else if (field->filter_type == FILTER_DYN_STRING) {
1992 			pred->fn_num = FILTER_PRED_FN_STRLOC;
1993 		} else if (field->filter_type == FILTER_RDYN_STRING)
1994 			pred->fn_num = FILTER_PRED_FN_STRRELLOC;
1995 		else {
1996 
1997 			if (!ustring_per_cpu) {
1998 				/* Once allocated, keep it around for good */
1999 				ustring_per_cpu = alloc_percpu(struct ustring_buffer);
2000 				if (!ustring_per_cpu)
2001 					goto err_mem;
2002 			}
2003 
2004 			if (ustring)
2005 				pred->fn_num = FILTER_PRED_FN_PCHAR_USER;
2006 			else
2007 				pred->fn_num = FILTER_PRED_FN_PCHAR;
2008 		}
2009 		/* go past the last quote */
2010 		i++;
2011 
2012 	} else if (isdigit(str[i]) || str[i] == '-') {
2013 
2014 		/* Make sure the field is not a string */
2015 		if (is_string_field(field)) {
2016 			parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
2017 			goto err_free;
2018 		}
2019 
2020 		if (op == OP_GLOB) {
2021 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
2022 			goto err_free;
2023 		}
2024 
2025 		if (str[i] == '-')
2026 			i++;
2027 
2028 		/* We allow 0xDEADBEEF */
2029 		while (isalnum(str[i]))
2030 			i++;
2031 
2032 		len = i - s;
2033 		/* 0xfeedfacedeadbeef is 18 chars max */
2034 		if (len >= sizeof(num_buf)) {
2035 			parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
2036 			goto err_free;
2037 		}
2038 
2039 		memcpy(num_buf, str + s, len);
2040 		num_buf[len] = 0;
2041 
2042 		/* Make sure it is a value */
2043 		if (field->is_signed)
2044 			ret = kstrtoll(num_buf, 0, &val);
2045 		else
2046 			ret = kstrtoull(num_buf, 0, &val);
2047 		if (ret) {
2048 			parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
2049 			goto err_free;
2050 		}
2051 
2052 		pred->val = val;
2053 
2054 		if (field->filter_type == FILTER_CPU)
2055 			pred->fn_num = FILTER_PRED_FN_CPU;
2056 		else {
2057 			pred->fn_num = select_comparison_fn(pred->op, field->size,
2058 							    field->is_signed);
2059 			if (pred->op == OP_NE)
2060 				pred->not = 1;
2061 		}
2062 
2063 	} else {
2064 		parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
2065 		goto err_free;
2066 	}
2067 
2068 	*pred_ptr = pred;
2069 	return i;
2070 
2071 err_free:
2072 	free_predicate(pred);
2073 	return -EINVAL;
2074 err_mem:
2075 	free_predicate(pred);
2076 	return -ENOMEM;
2077 }
2078 
2079 enum {
2080 	TOO_MANY_CLOSE		= -1,
2081 	TOO_MANY_OPEN		= -2,
2082 	MISSING_QUOTE		= -3,
2083 };
2084 
2085 /*
2086  * Read the filter string once to calculate the number of predicates
2087  * as well as how deep the parentheses go.
2088  *
2089  * Returns:
2090  *   0 - everything is fine (err is undefined)
2091  *  -1 - too many ')'
2092  *  -2 - too many '('
2093  *  -3 - No matching quote
2094  */
calc_stack(const char * str,int * parens,int * preds,int * err)2095 static int calc_stack(const char *str, int *parens, int *preds, int *err)
2096 {
2097 	bool is_pred = false;
2098 	int nr_preds = 0;
2099 	int open = 1; /* Count the expression as "(E)" */
2100 	int last_quote = 0;
2101 	int max_open = 1;
2102 	int quote = 0;
2103 	int i;
2104 
2105 	*err = 0;
2106 
2107 	for (i = 0; str[i]; i++) {
2108 		if (isspace(str[i]))
2109 			continue;
2110 		if (quote) {
2111 			if (str[i] == quote)
2112 			       quote = 0;
2113 			continue;
2114 		}
2115 
2116 		switch (str[i]) {
2117 		case '\'':
2118 		case '"':
2119 			quote = str[i];
2120 			last_quote = i;
2121 			break;
2122 		case '|':
2123 		case '&':
2124 			if (str[i+1] != str[i])
2125 				break;
2126 			is_pred = false;
2127 			continue;
2128 		case '(':
2129 			is_pred = false;
2130 			open++;
2131 			if (open > max_open)
2132 				max_open = open;
2133 			continue;
2134 		case ')':
2135 			is_pred = false;
2136 			if (open == 1) {
2137 				*err = i;
2138 				return TOO_MANY_CLOSE;
2139 			}
2140 			open--;
2141 			continue;
2142 		}
2143 		if (!is_pred) {
2144 			nr_preds++;
2145 			is_pred = true;
2146 		}
2147 	}
2148 
2149 	if (quote) {
2150 		*err = last_quote;
2151 		return MISSING_QUOTE;
2152 	}
2153 
2154 	if (open != 1) {
2155 		int level = open;
2156 
2157 		/* find the bad open */
2158 		for (i--; i; i--) {
2159 			if (quote) {
2160 				if (str[i] == quote)
2161 					quote = 0;
2162 				continue;
2163 			}
2164 			switch (str[i]) {
2165 			case '(':
2166 				if (level == open) {
2167 					*err = i;
2168 					return TOO_MANY_OPEN;
2169 				}
2170 				level--;
2171 				break;
2172 			case ')':
2173 				level++;
2174 				break;
2175 			case '\'':
2176 			case '"':
2177 				quote = str[i];
2178 				break;
2179 			}
2180 		}
2181 		/* First character is the '(' with missing ')' */
2182 		*err = 0;
2183 		return TOO_MANY_OPEN;
2184 	}
2185 
2186 	/* Set the size of the required stacks */
2187 	*parens = max_open;
2188 	*preds = nr_preds;
2189 	return 0;
2190 }
2191 
process_preds(struct trace_event_call * call,const char * filter_string,struct event_filter * filter,struct filter_parse_error * pe)2192 static int process_preds(struct trace_event_call *call,
2193 			 const char *filter_string,
2194 			 struct event_filter *filter,
2195 			 struct filter_parse_error *pe)
2196 {
2197 	struct prog_entry *prog;
2198 	int nr_parens;
2199 	int nr_preds;
2200 	int index;
2201 	int ret;
2202 
2203 	ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
2204 	if (ret < 0) {
2205 		switch (ret) {
2206 		case MISSING_QUOTE:
2207 			parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
2208 			break;
2209 		case TOO_MANY_OPEN:
2210 			parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
2211 			break;
2212 		default:
2213 			parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
2214 		}
2215 		return ret;
2216 	}
2217 
2218 	if (!nr_preds)
2219 		return -EINVAL;
2220 
2221 	prog = predicate_parse(filter_string, nr_parens, nr_preds,
2222 			       parse_pred, call, pe);
2223 	if (IS_ERR(prog))
2224 		return PTR_ERR(prog);
2225 
2226 	rcu_assign_pointer(filter->prog, prog);
2227 	return 0;
2228 }
2229 
event_set_filtered_flag(struct trace_event_file * file)2230 static inline void event_set_filtered_flag(struct trace_event_file *file)
2231 {
2232 	unsigned long old_flags = file->flags;
2233 
2234 	file->flags |= EVENT_FILE_FL_FILTERED;
2235 
2236 	if (old_flags != file->flags)
2237 		trace_buffered_event_enable();
2238 }
2239 
process_system_preds(struct trace_subsystem_dir * dir,struct trace_array * tr,struct filter_parse_error * pe,char * filter_string)2240 static int process_system_preds(struct trace_subsystem_dir *dir,
2241 				struct trace_array *tr,
2242 				struct filter_parse_error *pe,
2243 				char *filter_string)
2244 {
2245 	struct trace_event_file *file;
2246 	struct filter_list *filter_item;
2247 	struct event_filter *filter = NULL;
2248 	struct filter_head *filter_list;
2249 	bool fail = true;
2250 	int err;
2251 
2252 	filter_list = kmalloc(sizeof(*filter_list), GFP_KERNEL);
2253 	if (!filter_list)
2254 		return -ENOMEM;
2255 
2256 	INIT_LIST_HEAD(&filter_list->list);
2257 
2258 	list_for_each_entry(file, &tr->events, list) {
2259 
2260 		if (file->system != dir)
2261 			continue;
2262 
2263 		filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2264 		if (!filter)
2265 			goto fail_mem;
2266 
2267 		filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
2268 		if (!filter->filter_string)
2269 			goto fail_mem;
2270 
2271 		err = process_preds(file->event_call, filter_string, filter, pe);
2272 		if (err) {
2273 			filter_disable(file);
2274 			parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
2275 			append_filter_err(tr, pe, filter);
2276 		} else
2277 			event_set_filtered_flag(file);
2278 
2279 
2280 		filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
2281 		if (!filter_item)
2282 			goto fail_mem;
2283 
2284 		list_add_tail(&filter_item->list, &filter_list->list);
2285 		/*
2286 		 * Regardless of if this returned an error, we still
2287 		 * replace the filter for the call.
2288 		 */
2289 		filter_item->filter = event_filter(file);
2290 		event_set_filter(file, filter);
2291 		filter = NULL;
2292 
2293 		fail = false;
2294 	}
2295 
2296 	if (fail)
2297 		goto fail;
2298 
2299 	/*
2300 	 * The calls can still be using the old filters.
2301 	 * Do a synchronize_rcu() and to ensure all calls are
2302 	 * done with them before we free them.
2303 	 */
2304 	delay_free_filter(filter_list);
2305 	return 0;
2306  fail:
2307 	/* No call succeeded */
2308 	free_filter_list(&filter_list->rcu);
2309 	parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
2310 	return -EINVAL;
2311  fail_mem:
2312 	__free_filter(filter);
2313 
2314 	/* If any call succeeded, we still need to sync */
2315 	if (!fail)
2316 		delay_free_filter(filter_list);
2317 	else
2318 		free_filter_list(&filter_list->rcu);
2319 
2320 	return -ENOMEM;
2321 }
2322 
create_filter_start(char * filter_string,bool set_str,struct filter_parse_error ** pse,struct event_filter ** filterp)2323 static int create_filter_start(char *filter_string, bool set_str,
2324 			       struct filter_parse_error **pse,
2325 			       struct event_filter **filterp)
2326 {
2327 	struct event_filter *filter;
2328 	struct filter_parse_error *pe = NULL;
2329 	int err = 0;
2330 
2331 	if (WARN_ON_ONCE(*pse || *filterp))
2332 		return -EINVAL;
2333 
2334 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
2335 	if (filter && set_str) {
2336 		filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
2337 		if (!filter->filter_string)
2338 			err = -ENOMEM;
2339 	}
2340 
2341 	pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2342 
2343 	if (!filter || !pe || err) {
2344 		kfree(pe);
2345 		__free_filter(filter);
2346 		return -ENOMEM;
2347 	}
2348 
2349 	/* we're committed to creating a new filter */
2350 	*filterp = filter;
2351 	*pse = pe;
2352 
2353 	return 0;
2354 }
2355 
create_filter_finish(struct filter_parse_error * pe)2356 static void create_filter_finish(struct filter_parse_error *pe)
2357 {
2358 	kfree(pe);
2359 }
2360 
2361 /**
2362  * create_filter - create a filter for a trace_event_call
2363  * @tr: the trace array associated with these events
2364  * @call: trace_event_call to create a filter for
2365  * @filter_string: filter string
2366  * @set_str: remember @filter_str and enable detailed error in filter
2367  * @filterp: out param for created filter (always updated on return)
2368  *           Must be a pointer that references a NULL pointer.
2369  *
2370  * Creates a filter for @call with @filter_str.  If @set_str is %true,
2371  * @filter_str is copied and recorded in the new filter.
2372  *
2373  * On success, returns 0 and *@filterp points to the new filter.  On
2374  * failure, returns -errno and *@filterp may point to %NULL or to a new
2375  * filter.  In the latter case, the returned filter contains error
2376  * information if @set_str is %true and the caller is responsible for
2377  * freeing it.
2378  */
create_filter(struct trace_array * tr,struct trace_event_call * call,char * filter_string,bool set_str,struct event_filter ** filterp)2379 static int create_filter(struct trace_array *tr,
2380 			 struct trace_event_call *call,
2381 			 char *filter_string, bool set_str,
2382 			 struct event_filter **filterp)
2383 {
2384 	struct filter_parse_error *pe = NULL;
2385 	int err;
2386 
2387 	/* filterp must point to NULL */
2388 	if (WARN_ON(*filterp))
2389 		*filterp = NULL;
2390 
2391 	err = create_filter_start(filter_string, set_str, &pe, filterp);
2392 	if (err)
2393 		return err;
2394 
2395 	err = process_preds(call, filter_string, *filterp, pe);
2396 	if (err && set_str)
2397 		append_filter_err(tr, pe, *filterp);
2398 	create_filter_finish(pe);
2399 
2400 	return err;
2401 }
2402 
create_event_filter(struct trace_array * tr,struct trace_event_call * call,char * filter_str,bool set_str,struct event_filter ** filterp)2403 int create_event_filter(struct trace_array *tr,
2404 			struct trace_event_call *call,
2405 			char *filter_str, bool set_str,
2406 			struct event_filter **filterp)
2407 {
2408 	return create_filter(tr, call, filter_str, set_str, filterp);
2409 }
2410 
2411 /**
2412  * create_system_filter - create a filter for an event subsystem
2413  * @dir: the descriptor for the subsystem directory
2414  * @filter_str: filter string
2415  * @filterp: out param for created filter (always updated on return)
2416  *
2417  * Identical to create_filter() except that it creates a subsystem filter
2418  * and always remembers @filter_str.
2419  */
create_system_filter(struct trace_subsystem_dir * dir,char * filter_str,struct event_filter ** filterp)2420 static int create_system_filter(struct trace_subsystem_dir *dir,
2421 				char *filter_str, struct event_filter **filterp)
2422 {
2423 	struct filter_parse_error *pe = NULL;
2424 	int err;
2425 
2426 	err = create_filter_start(filter_str, true, &pe, filterp);
2427 	if (!err) {
2428 		err = process_system_preds(dir, dir->tr, pe, filter_str);
2429 		if (!err) {
2430 			/* System filters just show a default message */
2431 			kfree((*filterp)->filter_string);
2432 			(*filterp)->filter_string = NULL;
2433 		} else {
2434 			append_filter_err(dir->tr, pe, *filterp);
2435 		}
2436 	}
2437 	create_filter_finish(pe);
2438 
2439 	return err;
2440 }
2441 
2442 /* caller must hold event_mutex */
apply_event_filter(struct trace_event_file * file,char * filter_string)2443 int apply_event_filter(struct trace_event_file *file, char *filter_string)
2444 {
2445 	struct trace_event_call *call = file->event_call;
2446 	struct event_filter *filter = NULL;
2447 	int err;
2448 
2449 	if (file->flags & EVENT_FILE_FL_FREED)
2450 		return -ENODEV;
2451 
2452 	if (!strcmp(strstrip(filter_string), "0")) {
2453 		filter_disable(file);
2454 		filter = event_filter(file);
2455 
2456 		if (!filter)
2457 			return 0;
2458 
2459 		event_clear_filter(file);
2460 
2461 		try_delay_free_filter(filter);
2462 
2463 		return 0;
2464 	}
2465 
2466 	err = create_filter(file->tr, call, filter_string, true, &filter);
2467 
2468 	/*
2469 	 * Always swap the call filter with the new filter
2470 	 * even if there was an error. If there was an error
2471 	 * in the filter, we disable the filter and show the error
2472 	 * string
2473 	 */
2474 	if (filter) {
2475 		struct event_filter *tmp;
2476 
2477 		tmp = event_filter(file);
2478 		if (!err)
2479 			event_set_filtered_flag(file);
2480 		else
2481 			filter_disable(file);
2482 
2483 		event_set_filter(file, filter);
2484 
2485 		if (tmp)
2486 			try_delay_free_filter(tmp);
2487 	}
2488 
2489 	return err;
2490 }
2491 
apply_subsystem_event_filter(struct trace_subsystem_dir * dir,char * filter_string)2492 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
2493 				 char *filter_string)
2494 {
2495 	struct event_subsystem *system = dir->subsystem;
2496 	struct trace_array *tr = dir->tr;
2497 	struct event_filter *filter = NULL;
2498 	int err = 0;
2499 
2500 	guard(mutex)(&event_mutex);
2501 
2502 	/* Make sure the system still has events */
2503 	if (!dir->nr_events)
2504 		return -ENODEV;
2505 
2506 	if (!strcmp(strstrip(filter_string), "0")) {
2507 		filter_free_subsystem_preds(dir, tr);
2508 		remove_filter_string(system->filter);
2509 		filter = system->filter;
2510 		system->filter = NULL;
2511 		/* Ensure all filters are no longer used */
2512 		filter_free_subsystem_filters(dir, tr, filter);
2513 		return 0;
2514 	}
2515 
2516 	err = create_system_filter(dir, filter_string, &filter);
2517 	if (filter) {
2518 		/*
2519 		 * No event actually uses the system filter
2520 		 * we can free it without synchronize_rcu().
2521 		 */
2522 		__free_filter(system->filter);
2523 		system->filter = filter;
2524 	}
2525 
2526 	return err;
2527 }
2528 
2529 #ifdef CONFIG_PERF_EVENTS
2530 
ftrace_profile_free_filter(struct perf_event * event)2531 void ftrace_profile_free_filter(struct perf_event *event)
2532 {
2533 	struct event_filter *filter = event->filter;
2534 
2535 	event->filter = NULL;
2536 	__free_filter(filter);
2537 }
2538 
2539 struct function_filter_data {
2540 	struct ftrace_ops *ops;
2541 	int first_filter;
2542 	int first_notrace;
2543 };
2544 
2545 #ifdef CONFIG_FUNCTION_TRACER
2546 static char **
ftrace_function_filter_re(char * buf,int len,int * count)2547 ftrace_function_filter_re(char *buf, int len, int *count)
2548 {
2549 	char *str, **re;
2550 
2551 	str = kstrndup(buf, len, GFP_KERNEL);
2552 	if (!str)
2553 		return NULL;
2554 
2555 	/*
2556 	 * The argv_split function takes white space
2557 	 * as a separator, so convert ',' into spaces.
2558 	 */
2559 	strreplace(str, ',', ' ');
2560 
2561 	re = argv_split(GFP_KERNEL, str, count);
2562 	kfree(str);
2563 	return re;
2564 }
2565 
ftrace_function_set_regexp(struct ftrace_ops * ops,int filter,int reset,char * re,int len)2566 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2567 				      int reset, char *re, int len)
2568 {
2569 	int ret;
2570 
2571 	if (filter)
2572 		ret = ftrace_set_filter(ops, re, len, reset);
2573 	else
2574 		ret = ftrace_set_notrace(ops, re, len, reset);
2575 
2576 	return ret;
2577 }
2578 
__ftrace_function_set_filter(int filter,char * buf,int len,struct function_filter_data * data)2579 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2580 					struct function_filter_data *data)
2581 {
2582 	int i, re_cnt, ret = -EINVAL;
2583 	int *reset;
2584 	char **re;
2585 
2586 	reset = filter ? &data->first_filter : &data->first_notrace;
2587 
2588 	/*
2589 	 * The 'ip' field could have multiple filters set, separated
2590 	 * either by space or comma. We first cut the filter and apply
2591 	 * all pieces separately.
2592 	 */
2593 	re = ftrace_function_filter_re(buf, len, &re_cnt);
2594 	if (!re)
2595 		return -EINVAL;
2596 
2597 	for (i = 0; i < re_cnt; i++) {
2598 		ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2599 						 re[i], strlen(re[i]));
2600 		if (ret)
2601 			break;
2602 
2603 		if (*reset)
2604 			*reset = 0;
2605 	}
2606 
2607 	argv_free(re);
2608 	return ret;
2609 }
2610 
ftrace_function_check_pred(struct filter_pred * pred)2611 static int ftrace_function_check_pred(struct filter_pred *pred)
2612 {
2613 	struct ftrace_event_field *field = pred->field;
2614 
2615 	/*
2616 	 * Check the predicate for function trace, verify:
2617 	 *  - only '==' and '!=' is used
2618 	 *  - the 'ip' field is used
2619 	 */
2620 	if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2621 		return -EINVAL;
2622 
2623 	if (strcmp(field->name, "ip"))
2624 		return -EINVAL;
2625 
2626 	return 0;
2627 }
2628 
ftrace_function_set_filter_pred(struct filter_pred * pred,struct function_filter_data * data)2629 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
2630 					   struct function_filter_data *data)
2631 {
2632 	int ret;
2633 
2634 	/* Checking the node is valid for function trace. */
2635 	ret = ftrace_function_check_pred(pred);
2636 	if (ret)
2637 		return ret;
2638 
2639 	return __ftrace_function_set_filter(pred->op == OP_EQ,
2640 					    pred->regex->pattern,
2641 					    pred->regex->len,
2642 					    data);
2643 }
2644 
is_or(struct prog_entry * prog,int i)2645 static bool is_or(struct prog_entry *prog, int i)
2646 {
2647 	int target;
2648 
2649 	/*
2650 	 * Only "||" is allowed for function events, thus,
2651 	 * all true branches should jump to true, and any
2652 	 * false branch should jump to false.
2653 	 */
2654 	target = prog[i].target + 1;
2655 	/* True and false have NULL preds (all prog entries should jump to one */
2656 	if (prog[target].pred)
2657 		return false;
2658 
2659 	/* prog[target].target is 1 for TRUE, 0 for FALSE */
2660 	return prog[i].when_to_branch == prog[target].target;
2661 }
2662 
ftrace_function_set_filter(struct perf_event * event,struct event_filter * filter)2663 static int ftrace_function_set_filter(struct perf_event *event,
2664 				      struct event_filter *filter)
2665 {
2666 	struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2667 						lockdep_is_held(&event_mutex));
2668 	struct function_filter_data data = {
2669 		.first_filter  = 1,
2670 		.first_notrace = 1,
2671 		.ops           = &event->ftrace_ops,
2672 	};
2673 	int i;
2674 
2675 	for (i = 0; prog[i].pred; i++) {
2676 		struct filter_pred *pred = prog[i].pred;
2677 
2678 		if (!is_or(prog, i))
2679 			return -EINVAL;
2680 
2681 		if (ftrace_function_set_filter_pred(pred, &data) < 0)
2682 			return -EINVAL;
2683 	}
2684 	return 0;
2685 }
2686 #else
ftrace_function_set_filter(struct perf_event * event,struct event_filter * filter)2687 static int ftrace_function_set_filter(struct perf_event *event,
2688 				      struct event_filter *filter)
2689 {
2690 	return -ENODEV;
2691 }
2692 #endif /* CONFIG_FUNCTION_TRACER */
2693 
ftrace_profile_set_filter(struct perf_event * event,int event_id,char * filter_str)2694 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2695 			      char *filter_str)
2696 {
2697 	int err;
2698 	struct event_filter *filter = NULL;
2699 	struct trace_event_call *call;
2700 
2701 	guard(mutex)(&event_mutex);
2702 
2703 	call = event->tp_event;
2704 
2705 	if (!call)
2706 		return -EINVAL;
2707 
2708 	if (event->filter)
2709 		return -EEXIST;
2710 
2711 	err = create_filter(NULL, call, filter_str, false, &filter);
2712 	if (err)
2713 		goto free_filter;
2714 
2715 	if (ftrace_event_is_function(call))
2716 		err = ftrace_function_set_filter(event, filter);
2717 	else
2718 		event->filter = filter;
2719 
2720 free_filter:
2721 	if (err || ftrace_event_is_function(call))
2722 		__free_filter(filter);
2723 
2724 	return err;
2725 }
2726 
2727 #endif /* CONFIG_PERF_EVENTS */
2728 
2729 #ifdef CONFIG_FTRACE_STARTUP_TEST
2730 
2731 #include <linux/types.h>
2732 #include <linux/tracepoint.h>
2733 
2734 #define CREATE_TRACE_POINTS
2735 #include "trace_events_filter_test.h"
2736 
2737 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2738 { \
2739 	.filter = FILTER, \
2740 	.rec    = { .a = va, .b = vb, .c = vc, .d = vd, \
2741 		    .e = ve, .f = vf, .g = vg, .h = vh }, \
2742 	.match  = m, \
2743 	.not_visited = nvisit, \
2744 }
2745 #define YES 1
2746 #define NO  0
2747 
2748 static struct test_filter_data_t {
2749 	char *filter;
2750 	struct trace_event_raw_ftrace_test_filter rec;
2751 	int match;
2752 	char *not_visited;
2753 } test_filter_data[] = {
2754 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2755 	       "e == 1 && f == 1 && g == 1 && h == 1"
2756 	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2757 	DATA_REC(NO,  0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2758 	DATA_REC(NO,  1, 1, 1, 1, 1, 1, 1, 0, ""),
2759 #undef FILTER
2760 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2761 	       "e == 1 || f == 1 || g == 1 || h == 1"
2762 	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""),
2763 	DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2764 	DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2765 #undef FILTER
2766 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2767 	       "(e == 1 || f == 1) && (g == 1 || h == 1)"
2768 	DATA_REC(NO,  0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2769 	DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2770 	DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2771 	DATA_REC(NO,  1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2772 #undef FILTER
2773 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2774 	       "(e == 1 && f == 1) || (g == 1 && h == 1)"
2775 	DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2776 	DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2777 	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""),
2778 #undef FILTER
2779 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2780 	       "(e == 1 && f == 1) || (g == 1 && h == 1)"
2781 	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2782 	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""),
2783 	DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2784 #undef FILTER
2785 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2786 	       "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2787 	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2788 	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""),
2789 	DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2790 #undef FILTER
2791 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2792 	       "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2793 	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2794 	DATA_REC(NO,  0, 1, 0, 1, 0, 1, 0, 1, ""),
2795 	DATA_REC(NO,  1, 0, 1, 0, 1, 0, 1, 0, ""),
2796 #undef FILTER
2797 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2798 	       "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2799 	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2800 	DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2801 	DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2802 };
2803 
2804 #undef DATA_REC
2805 #undef FILTER
2806 #undef YES
2807 #undef NO
2808 
2809 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2810 
2811 static int test_pred_visited;
2812 
test_pred_visited_fn(struct filter_pred * pred,void * event)2813 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2814 {
2815 	struct ftrace_event_field *field = pred->field;
2816 
2817 	test_pred_visited = 1;
2818 	printk(KERN_INFO "\npred visited %s\n", field->name);
2819 	return 1;
2820 }
2821 
update_pred_fn(struct event_filter * filter,char * fields)2822 static void update_pred_fn(struct event_filter *filter, char *fields)
2823 {
2824 	struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2825 						lockdep_is_held(&event_mutex));
2826 	int i;
2827 
2828 	for (i = 0; prog[i].pred; i++) {
2829 		struct filter_pred *pred = prog[i].pred;
2830 		struct ftrace_event_field *field = pred->field;
2831 
2832 		WARN_ON_ONCE(pred->fn_num == FILTER_PRED_FN_NOP);
2833 
2834 		if (!field) {
2835 			WARN_ONCE(1, "all leafs should have field defined %d", i);
2836 			continue;
2837 		}
2838 
2839 		if (!strchr(fields, *field->name))
2840 			continue;
2841 
2842 		pred->fn_num = FILTER_PRED_TEST_VISITED;
2843 	}
2844 }
2845 
ftrace_test_event_filter(void)2846 static __init int ftrace_test_event_filter(void)
2847 {
2848 	int i;
2849 
2850 	printk(KERN_INFO "Testing ftrace filter: ");
2851 
2852 	for (i = 0; i < DATA_CNT; i++) {
2853 		struct event_filter *filter = NULL;
2854 		struct test_filter_data_t *d = &test_filter_data[i];
2855 		int err;
2856 
2857 		err = create_filter(NULL, &event_ftrace_test_filter,
2858 				    d->filter, false, &filter);
2859 		if (err) {
2860 			printk(KERN_INFO
2861 			       "Failed to get filter for '%s', err %d\n",
2862 			       d->filter, err);
2863 			__free_filter(filter);
2864 			break;
2865 		}
2866 
2867 		/* Needed to dereference filter->prog */
2868 		mutex_lock(&event_mutex);
2869 		/*
2870 		 * The preemption disabling is not really needed for self
2871 		 * tests, but the rcu dereference will complain without it.
2872 		 */
2873 		preempt_disable();
2874 		if (*d->not_visited)
2875 			update_pred_fn(filter, d->not_visited);
2876 
2877 		test_pred_visited = 0;
2878 		err = filter_match_preds(filter, &d->rec);
2879 		preempt_enable();
2880 
2881 		mutex_unlock(&event_mutex);
2882 
2883 		__free_filter(filter);
2884 
2885 		if (test_pred_visited) {
2886 			printk(KERN_INFO
2887 			       "Failed, unwanted pred visited for filter %s\n",
2888 			       d->filter);
2889 			break;
2890 		}
2891 
2892 		if (err != d->match) {
2893 			printk(KERN_INFO
2894 			       "Failed to match filter '%s', expected %d\n",
2895 			       d->filter, d->match);
2896 			break;
2897 		}
2898 	}
2899 
2900 	if (i == DATA_CNT)
2901 		printk(KERN_CONT "OK\n");
2902 
2903 	return 0;
2904 }
2905 
2906 late_initcall(ftrace_test_event_filter);
2907 
2908 #endif /* CONFIG_FTRACE_STARTUP_TEST */
2909