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
39enum filter_op_ids { OPS };
40
41#undef C
42#define C(a, b) b
43
44static const char * ops[] = { OPS };
45
46enum 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
79struct 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
126enum { ERRORS };
127
128#undef C
129#define C(a, b) b
130
131static const char *err_text[] = { ERRORS };
132
133/* Called after a '!' character but "!=" and "!~" are not "not"s */
134static 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 */
150struct 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 */
168static 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
179struct filter_parse_error {
180 int lasterr;
181 int lasterr_pos;
182};
183
184static void parse_error(struct filter_parse_error *pe, int err, int pos)
185{
186 pe->lasterr = err;
187 pe->lasterr_pos = pos;
188}
189
190typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
191 struct filter_parse_error *pe,
192 struct filter_pred **pred);
193
194enum {
195 INVERT = 1,
196 PROCESS_AND = 2,
197 PROCESS_OR = 4,
198};
199
200static void free_predicate(struct filter_pred *pred)
201{
202 if (pred) {
203 kfree(objp: pred->regex);
204 kfree(objp: pred->mask);
205 kfree(objp: 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 */
469static struct prog_entry *
470predicate_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(n: nr_parens, size: sizeof(*op_stack), GFP_KERNEL);
489 if (!op_stack)
490 return ERR_PTR(error: -ENOMEM);
491 prog_stack = kcalloc(n: nr_preds, size: sizeof(*prog_stack), GFP_KERNEL);
492 if (!prog_stack) {
493 parse_error(pe, err: -ENOMEM, pos: 0);
494 goto out_free;
495 }
496 inverts = kmalloc_array(n: nr_preds, size: sizeof(*inverts), GFP_KERNEL);
497 if (!inverts) {
498 parse_error(pe, err: -ENOMEM, pos: 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(str: next))
523 break;
524 invert = !invert;
525 continue;
526 }
527
528 if (N >= nr_preds) {
529 parse_error(pe, err: FILT_ERR_TOO_MANY_PREDS, pos: 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, err: FILT_ERR_TOO_MANY_PREDS,
565 pos: next - str);
566 goto out_free;
567 }
568
569 invert = *top & INVERT;
570
571 if (*top & PROCESS_AND) { /* #7 */
572 update_preds(prog, N: 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: N - 1, invert: !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, err: FILT_ERR_TOO_MANY_CLOSE, pos: 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, err: FILT_ERR_TOO_MANY_OPEN, pos: ptr - str);
603 goto out_free;
604 }
605
606 if (!N) {
607 /* No program? */
608 ret = -EINVAL;
609 parse_error(pe, err: FILT_ERR_NO_FILTER, pos: 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(objp: op_stack);
639 kfree(objp: inverts);
640 return prog;
641out_free:
642 kfree(objp: op_stack);
643 kfree(objp: inverts);
644 if (prog_stack) {
645 for (i = 0; prog_stack[i].pred; i++)
646 free_predicate(pred: prog_stack[i].pred);
647 kfree(objp: prog_stack);
648 }
649 return ERR_PTR(error: ret);
650}
651
652static inline int
653do_filter_cpumask(int op, const struct cpumask *mask, const struct cpumask *cmp)
654{
655 switch (op) {
656 case OP_EQ:
657 return cpumask_equal(src1p: mask, src2p: cmp);
658 case OP_NE:
659 return !cpumask_equal(src1p: mask, src2p: cmp);
660 case OP_BAND:
661 return cpumask_intersects(src1p: mask, src2p: cmp);
662 default:
663 return 0;
664 }
665}
666
667/* Optimisation of do_filter_cpumask() for scalar fields */
668static inline int
669do_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, cpumask: mask);
682 default:
683 return 0;
684 }
685}
686
687static inline int
688do_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, cpumask: mask) &&
693 cpumask_nth(cpu: 1, srcp: mask) >= nr_cpu_ids;
694 case OP_NE:
695 return !cpumask_test_cpu(cpu, cpumask: mask) ||
696 cpumask_nth(cpu: 1, srcp: mask) < nr_cpu_ids;
697 case OP_BAND:
698 return cpumask_test_cpu(cpu, cpumask: mask);
699 default:
700 return 0;
701 }
702}
703
704enum 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) \
714static 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) \
748static 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) \
760static 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
771DEFINE_COMPARISON_PRED(s64);
772DEFINE_COMPARISON_PRED(u64);
773DEFINE_COMPARISON_PRED(s32);
774DEFINE_COMPARISON_PRED(u32);
775DEFINE_COMPARISON_PRED(s16);
776DEFINE_COMPARISON_PRED(u16);
777DEFINE_COMPARISON_PRED(s8);
778DEFINE_COMPARISON_PRED(u8);
779
780DEFINE_CPUMASK_COMPARISON_PRED(64);
781DEFINE_CPUMASK_COMPARISON_PRED(32);
782DEFINE_CPUMASK_COMPARISON_PRED(16);
783DEFINE_CPUMASK_COMPARISON_PRED(8);
784
785DEFINE_EQUALITY_PRED(64);
786DEFINE_EQUALITY_PRED(32);
787DEFINE_EQUALITY_PRED(16);
788DEFINE_EQUALITY_PRED(8);
789
790/* user space strings temp buffer */
791#define USTRING_BUF_SIZE 1024
792
793struct ustring_buffer {
794 char buffer[USTRING_BUF_SIZE];
795};
796
797static __percpu struct ustring_buffer *ustring_per_cpu;
798
799static __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(dst: kstr, unsafe_addr: str, USTRING_BUF_SIZE))
812 return NULL;
813 return kstr;
814}
815
816static __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(dst: kstr, unsafe_addr: ustr, USTRING_BUF_SIZE))
831 return NULL;
832
833 return kstr;
834}
835
836/* Filter predicate for fixed sized arrays of characters */
837static 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
849static __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 */
862static 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(str: *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*/
875static 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(str: *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 */
897static 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 */
919static 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. */
936static 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 */
962static 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(op: pred->op, cpu, mask: pred->mask);
967}
968
969/* Filter predicate for cpumask field vs user-provided cpumask */
970static 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(op: pred->op, mask, cmp);
978}
979
980/* Filter predicate for cpumask field vs user-provided scalar */
981static 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(op: pred->op, mask, cpu);
989}
990
991/* Filter predicate for COMM. */
992static 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. */
1002static 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
1024static 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
1033static 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
1041static 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
1049static 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(p: str + strlen - r->len, q: r->pattern, size: r->len) == 0)
1055 return 1;
1056 return 0;
1057}
1058
1059static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
1060{
1061 if (glob_match(pat: 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 */
1083enum 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(c: 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
1124static 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(buff: r->pattern, len: r->len, search: &search, not: &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
1159static int test_pred_visited_fn(struct filter_pred *pred, void *event);
1160#else
1161static int test_pred_visited_fn(struct filter_pred *pred, void *event)
1162{
1163 return 0;
1164}
1165#endif
1166
1167
1168static int filter_pred_fn_call(struct filter_pred *pred, void *event);
1169
1170/* return 1 if event matches, 0 otherwise (discard) */
1171int 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, event: rec);
1188 if (match == prog[i].when_to_branch)
1189 i = prog[i].target;
1190 }
1191 return prog[i].target;
1192}
1193EXPORT_SYMBOL_GPL(filter_match_preds);
1194
1195static void remove_filter_string(struct event_filter *filter)
1196{
1197 if (!filter)
1198 return;
1199
1200 kfree(objp: filter->filter_string);
1201 filter->filter_string = NULL;
1202}
1203
1204static 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(size: 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, str: filter->filter_string);
1230 if (pe->lasterr > 0) {
1231 trace_seq_printf(s, fmt: "\n%*s", pos, "^");
1232 trace_seq_printf(s, fmt: "\nparse_error: %s\n", err_text[pe->lasterr]);
1233 tracing_log_err(tr, loc: "event filter parse error",
1234 cmd: filter->filter_string, errs: err_text,
1235 type: pe->lasterr, pos: pe->lasterr_pos);
1236 } else {
1237 trace_seq_printf(s, fmt: "\nError: (%d)\n", pe->lasterr);
1238 tracing_log_err(tr, loc: "event filter parse error",
1239 cmd: filter->filter_string, errs: err_text,
1240 type: FILT_ERR_ERRNO, pos: 0);
1241 }
1242 trace_seq_putc(s, c: 0);
1243 buf = kmemdup_nul(s: s->buffer, len: s->seq.len, GFP_KERNEL);
1244 if (buf) {
1245 kfree(objp: filter->filter_string);
1246 filter->filter_string = buf;
1247 }
1248 kfree(objp: s);
1249}
1250
1251static inline struct event_filter *event_filter(struct trace_event_file *file)
1252{
1253 return file->filter;
1254}
1255
1256/* caller must hold event_mutex */
1257void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
1258{
1259 struct event_filter *filter = event_filter(file);
1260
1261 if (filter && filter->filter_string)
1262 trace_seq_printf(s, fmt: "%s\n", filter->filter_string);
1263 else
1264 trace_seq_puts(s, str: "none\n");
1265}
1266
1267void print_subsystem_event_filter(struct event_subsystem *system,
1268 struct trace_seq *s)
1269{
1270 struct event_filter *filter;
1271
1272 mutex_lock(&event_mutex);
1273 filter = system->filter;
1274 if (filter && filter->filter_string)
1275 trace_seq_printf(s, fmt: "%s\n", filter->filter_string);
1276 else
1277 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1278 mutex_unlock(lock: &event_mutex);
1279}
1280
1281static void free_prog(struct event_filter *filter)
1282{
1283 struct prog_entry *prog;
1284 int i;
1285
1286 prog = rcu_access_pointer(filter->prog);
1287 if (!prog)
1288 return;
1289
1290 for (i = 0; prog[i].pred; i++)
1291 free_predicate(pred: prog[i].pred);
1292 kfree(objp: prog);
1293}
1294
1295static void filter_disable(struct trace_event_file *file)
1296{
1297 unsigned long old_flags = file->flags;
1298
1299 file->flags &= ~EVENT_FILE_FL_FILTERED;
1300
1301 if (old_flags != file->flags)
1302 trace_buffered_event_disable();
1303}
1304
1305static void __free_filter(struct event_filter *filter)
1306{
1307 if (!filter)
1308 return;
1309
1310 free_prog(filter);
1311 kfree(objp: filter->filter_string);
1312 kfree(objp: filter);
1313}
1314
1315void free_event_filter(struct event_filter *filter)
1316{
1317 __free_filter(filter);
1318}
1319
1320static inline void __remove_filter(struct trace_event_file *file)
1321{
1322 filter_disable(file);
1323 remove_filter_string(filter: file->filter);
1324}
1325
1326static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1327 struct trace_array *tr)
1328{
1329 struct trace_event_file *file;
1330
1331 list_for_each_entry(file, &tr->events, list) {
1332 if (file->system != dir)
1333 continue;
1334 __remove_filter(file);
1335 }
1336}
1337
1338static inline void __free_subsystem_filter(struct trace_event_file *file)
1339{
1340 __free_filter(filter: file->filter);
1341 file->filter = NULL;
1342}
1343
1344static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1345 struct trace_array *tr)
1346{
1347 struct trace_event_file *file;
1348
1349 list_for_each_entry(file, &tr->events, list) {
1350 if (file->system != dir)
1351 continue;
1352 __free_subsystem_filter(file);
1353 }
1354}
1355
1356int filter_assign_type(const char *type)
1357{
1358 if (strstr(type, "__data_loc")) {
1359 if (strstr(type, "char"))
1360 return FILTER_DYN_STRING;
1361 if (strstr(type, "cpumask_t"))
1362 return FILTER_CPUMASK;
1363 }
1364
1365 if (strstr(type, "__rel_loc") && strstr(type, "char"))
1366 return FILTER_RDYN_STRING;
1367
1368 if (strchr(type, '[') && strstr(type, "char"))
1369 return FILTER_STATIC_STRING;
1370
1371 if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1372 return FILTER_PTR_STRING;
1373
1374 return FILTER_OTHER;
1375}
1376
1377static enum filter_pred_fn select_comparison_fn(enum filter_op_ids op,
1378 int field_size, int field_is_signed)
1379{
1380 enum filter_pred_fn fn = FILTER_PRED_FN_NOP;
1381 int pred_func_index = -1;
1382
1383 switch (op) {
1384 case OP_EQ:
1385 case OP_NE:
1386 break;
1387 default:
1388 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1389 return fn;
1390 pred_func_index = op - PRED_FUNC_START;
1391 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1392 return fn;
1393 }
1394
1395 switch (field_size) {
1396 case 8:
1397 if (pred_func_index < 0)
1398 fn = FILTER_PRED_FN_64;
1399 else if (field_is_signed)
1400 fn = FILTER_PRED_FN_S64;
1401 else
1402 fn = FILTER_PRED_FN_U64;
1403 break;
1404 case 4:
1405 if (pred_func_index < 0)
1406 fn = FILTER_PRED_FN_32;
1407 else if (field_is_signed)
1408 fn = FILTER_PRED_FN_S32;
1409 else
1410 fn = FILTER_PRED_FN_U32;
1411 break;
1412 case 2:
1413 if (pred_func_index < 0)
1414 fn = FILTER_PRED_FN_16;
1415 else if (field_is_signed)
1416 fn = FILTER_PRED_FN_S16;
1417 else
1418 fn = FILTER_PRED_FN_U16;
1419 break;
1420 case 1:
1421 if (pred_func_index < 0)
1422 fn = FILTER_PRED_FN_8;
1423 else if (field_is_signed)
1424 fn = FILTER_PRED_FN_S8;
1425 else
1426 fn = FILTER_PRED_FN_U8;
1427 break;
1428 }
1429
1430 return fn;
1431}
1432
1433
1434static int filter_pred_fn_call(struct filter_pred *pred, void *event)
1435{
1436 switch (pred->fn_num) {
1437 case FILTER_PRED_FN_64:
1438 return filter_pred_64(pred, event);
1439 case FILTER_PRED_FN_64_CPUMASK:
1440 return filter_pred_64_cpumask(pred, event);
1441 case FILTER_PRED_FN_S64:
1442 return filter_pred_s64(pred, event);
1443 case FILTER_PRED_FN_U64:
1444 return filter_pred_u64(pred, event);
1445 case FILTER_PRED_FN_32:
1446 return filter_pred_32(pred, event);
1447 case FILTER_PRED_FN_32_CPUMASK:
1448 return filter_pred_32_cpumask(pred, event);
1449 case FILTER_PRED_FN_S32:
1450 return filter_pred_s32(pred, event);
1451 case FILTER_PRED_FN_U32:
1452 return filter_pred_u32(pred, event);
1453 case FILTER_PRED_FN_16:
1454 return filter_pred_16(pred, event);
1455 case FILTER_PRED_FN_16_CPUMASK:
1456 return filter_pred_16_cpumask(pred, event);
1457 case FILTER_PRED_FN_S16:
1458 return filter_pred_s16(pred, event);
1459 case FILTER_PRED_FN_U16:
1460 return filter_pred_u16(pred, event);
1461 case FILTER_PRED_FN_8:
1462 return filter_pred_8(pred, event);
1463 case FILTER_PRED_FN_8_CPUMASK:
1464 return filter_pred_8_cpumask(pred, event);
1465 case FILTER_PRED_FN_S8:
1466 return filter_pred_s8(pred, event);
1467 case FILTER_PRED_FN_U8:
1468 return filter_pred_u8(pred, event);
1469 case FILTER_PRED_FN_COMM:
1470 return filter_pred_comm(pred, event);
1471 case FILTER_PRED_FN_STRING:
1472 return filter_pred_string(pred, event);
1473 case FILTER_PRED_FN_STRLOC:
1474 return filter_pred_strloc(pred, event);
1475 case FILTER_PRED_FN_STRRELLOC:
1476 return filter_pred_strrelloc(pred, event);
1477 case FILTER_PRED_FN_PCHAR_USER:
1478 return filter_pred_pchar_user(pred, event);
1479 case FILTER_PRED_FN_PCHAR:
1480 return filter_pred_pchar(pred, event);
1481 case FILTER_PRED_FN_CPU:
1482 return filter_pred_cpu(pred, event);
1483 case FILTER_PRED_FN_CPU_CPUMASK:
1484 return filter_pred_cpu_cpumask(pred, event);
1485 case FILTER_PRED_FN_CPUMASK:
1486 return filter_pred_cpumask(pred, event);
1487 case FILTER_PRED_FN_CPUMASK_CPU:
1488 return filter_pred_cpumask_cpu(pred, event);
1489 case FILTER_PRED_FN_FUNCTION:
1490 return filter_pred_function(pred, event);
1491 case FILTER_PRED_TEST_VISITED:
1492 return test_pred_visited_fn(pred, event);
1493 default:
1494 return 0;
1495 }
1496}
1497
1498/* Called when a predicate is encountered by predicate_parse() */
1499static int parse_pred(const char *str, void *data,
1500 int pos, struct filter_parse_error *pe,
1501 struct filter_pred **pred_ptr)
1502{
1503 struct trace_event_call *call = data;
1504 struct ftrace_event_field *field;
1505 struct filter_pred *pred = NULL;
1506 unsigned long offset;
1507 unsigned long size;
1508 unsigned long ip;
1509 char num_buf[24]; /* Big enough to hold an address */
1510 char *field_name;
1511 char *name;
1512 bool function = false;
1513 bool ustring = false;
1514 char q;
1515 u64 val;
1516 int len;
1517 int ret;
1518 int op;
1519 int s;
1520 int i = 0;
1521
1522 /* First find the field to associate to */
1523 while (isspace(str[i]))
1524 i++;
1525 s = i;
1526
1527 while (isalnum(str[i]) || str[i] == '_')
1528 i++;
1529
1530 len = i - s;
1531
1532 if (!len)
1533 return -1;
1534
1535 field_name = kmemdup_nul(s: str + s, len, GFP_KERNEL);
1536 if (!field_name)
1537 return -ENOMEM;
1538
1539 /* Make sure that the field exists */
1540
1541 field = trace_find_event_field(call, name: field_name);
1542 kfree(objp: field_name);
1543 if (!field) {
1544 parse_error(pe, err: FILT_ERR_FIELD_NOT_FOUND, pos: pos + i);
1545 return -EINVAL;
1546 }
1547
1548 /* See if the field is a user space string */
1549 if ((len = str_has_prefix(str: str + i, prefix: ".ustring"))) {
1550 ustring = true;
1551 i += len;
1552 }
1553
1554 /* See if the field is a kernel function name */
1555 if ((len = str_has_prefix(str: str + i, prefix: ".function"))) {
1556 function = true;
1557 i += len;
1558 }
1559
1560 while (isspace(str[i]))
1561 i++;
1562
1563 /* Make sure this op is supported */
1564 for (op = 0; ops[op]; op++) {
1565 /* This is why '<=' must come before '<' in ops[] */
1566 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1567 break;
1568 }
1569
1570 if (!ops[op]) {
1571 parse_error(pe, err: FILT_ERR_INVALID_OP, pos: pos + i);
1572 goto err_free;
1573 }
1574
1575 i += strlen(ops[op]);
1576
1577 while (isspace(str[i]))
1578 i++;
1579
1580 s = i;
1581
1582 pred = kzalloc(size: sizeof(*pred), GFP_KERNEL);
1583 if (!pred)
1584 return -ENOMEM;
1585
1586 pred->field = field;
1587 pred->offset = field->offset;
1588 pred->op = op;
1589
1590 if (function) {
1591 /* The field must be the same size as long */
1592 if (field->size != sizeof(long)) {
1593 parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i);
1594 goto err_free;
1595 }
1596
1597 /* Function only works with '==' or '!=' and an unquoted string */
1598 switch (op) {
1599 case OP_NE:
1600 case OP_EQ:
1601 break;
1602 default:
1603 parse_error(pe, err: FILT_ERR_INVALID_OP, pos: pos + i);
1604 goto err_free;
1605 }
1606
1607 if (isdigit(c: str[i])) {
1608 /* We allow 0xDEADBEEF */
1609 while (isalnum(str[i]))
1610 i++;
1611
1612 len = i - s;
1613 /* 0xfeedfacedeadbeef is 18 chars max */
1614 if (len >= sizeof(num_buf)) {
1615 parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i);
1616 goto err_free;
1617 }
1618
1619 strncpy(p: num_buf, q: str + s, size: len);
1620 num_buf[len] = 0;
1621
1622 ret = kstrtoul(s: num_buf, base: 0, res: &ip);
1623 if (ret) {
1624 parse_error(pe, err: FILT_ERR_INVALID_VALUE, pos: pos + i);
1625 goto err_free;
1626 }
1627 } else {
1628 s = i;
1629 for (; str[i] && !isspace(str[i]); i++)
1630 ;
1631
1632 len = i - s;
1633 name = kmemdup_nul(s: str + s, len, GFP_KERNEL);
1634 if (!name)
1635 goto err_mem;
1636 ip = kallsyms_lookup_name(name);
1637 kfree(objp: name);
1638 if (!ip) {
1639 parse_error(pe, err: FILT_ERR_NO_FUNCTION, pos: pos + i);
1640 goto err_free;
1641 }
1642 }
1643
1644 /* Now find the function start and end address */
1645 if (!kallsyms_lookup_size_offset(addr: ip, symbolsize: &size, offset: &offset)) {
1646 parse_error(pe, err: FILT_ERR_NO_FUNCTION, pos: pos + i);
1647 goto err_free;
1648 }
1649
1650 pred->fn_num = FILTER_PRED_FN_FUNCTION;
1651 pred->val = ip - offset;
1652 pred->val2 = pred->val + size;
1653
1654 } else if (ftrace_event_is_function(call)) {
1655 /*
1656 * Perf does things different with function events.
1657 * It only allows an "ip" field, and expects a string.
1658 * But the string does not need to be surrounded by quotes.
1659 * If it is a string, the assigned function as a nop,
1660 * (perf doesn't use it) and grab everything.
1661 */
1662 if (strcmp(field->name, "ip") != 0) {
1663 parse_error(pe, err: FILT_ERR_IP_FIELD_ONLY, pos: pos + i);
1664 goto err_free;
1665 }
1666 pred->fn_num = FILTER_PRED_FN_NOP;
1667
1668 /*
1669 * Quotes are not required, but if they exist then we need
1670 * to read them till we hit a matching one.
1671 */
1672 if (str[i] == '\'' || str[i] == '"')
1673 q = str[i];
1674 else
1675 q = 0;
1676
1677 for (i++; str[i]; i++) {
1678 if (q && str[i] == q)
1679 break;
1680 if (!q && (str[i] == ')' || str[i] == '&' ||
1681 str[i] == '|'))
1682 break;
1683 }
1684 /* Skip quotes */
1685 if (q)
1686 s++;
1687 len = i - s;
1688 if (len >= MAX_FILTER_STR_VAL) {
1689 parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i);
1690 goto err_free;
1691 }
1692
1693 pred->regex = kzalloc(size: sizeof(*pred->regex), GFP_KERNEL);
1694 if (!pred->regex)
1695 goto err_mem;
1696 pred->regex->len = len;
1697 strncpy(p: pred->regex->pattern, q: str + s, size: len);
1698 pred->regex->pattern[len] = 0;
1699
1700 } else if (!strncmp(str + i, "CPUS", 4)) {
1701 unsigned int maskstart;
1702 bool single;
1703 char *tmp;
1704
1705 switch (field->filter_type) {
1706 case FILTER_CPUMASK:
1707 case FILTER_CPU:
1708 case FILTER_OTHER:
1709 break;
1710 default:
1711 parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i);
1712 goto err_free;
1713 }
1714
1715 switch (op) {
1716 case OP_EQ:
1717 case OP_NE:
1718 case OP_BAND:
1719 break;
1720 default:
1721 parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i);
1722 goto err_free;
1723 }
1724
1725 /* Skip CPUS */
1726 i += 4;
1727 if (str[i++] != '{') {
1728 parse_error(pe, err: FILT_ERR_MISSING_BRACE_OPEN, pos: pos + i);
1729 goto err_free;
1730 }
1731 maskstart = i;
1732
1733 /* Walk the cpulist until closing } */
1734 for (; str[i] && str[i] != '}'; i++)
1735 ;
1736
1737 if (str[i] != '}') {
1738 parse_error(pe, err: FILT_ERR_MISSING_BRACE_CLOSE, pos: pos + i);
1739 goto err_free;
1740 }
1741
1742 if (maskstart == i) {
1743 parse_error(pe, err: FILT_ERR_INVALID_CPULIST, pos: pos + i);
1744 goto err_free;
1745 }
1746
1747 /* Copy the cpulist between { and } */
1748 tmp = kmalloc(size: (i - maskstart) + 1, GFP_KERNEL);
1749 if (!tmp)
1750 goto err_mem;
1751
1752 strscpy(p: tmp, q: str + maskstart, size: (i - maskstart) + 1);
1753 pred->mask = kzalloc(size: cpumask_size(), GFP_KERNEL);
1754 if (!pred->mask) {
1755 kfree(objp: tmp);
1756 goto err_mem;
1757 }
1758
1759 /* Now parse it */
1760 if (cpulist_parse(buf: tmp, dstp: pred->mask)) {
1761 kfree(objp: tmp);
1762 parse_error(pe, err: FILT_ERR_INVALID_CPULIST, pos: pos + i);
1763 goto err_free;
1764 }
1765 kfree(objp: tmp);
1766
1767 /* Move along */
1768 i++;
1769
1770 /*
1771 * Optimisation: if the user-provided mask has a weight of one
1772 * then we can treat it as a scalar input.
1773 */
1774 single = cpumask_weight(srcp: pred->mask) == 1;
1775 if (single) {
1776 pred->val = cpumask_first(srcp: pred->mask);
1777 kfree(objp: pred->mask);
1778 pred->mask = NULL;
1779 }
1780
1781 if (field->filter_type == FILTER_CPUMASK) {
1782 pred->fn_num = single ?
1783 FILTER_PRED_FN_CPUMASK_CPU :
1784 FILTER_PRED_FN_CPUMASK;
1785 } else if (field->filter_type == FILTER_CPU) {
1786 if (single) {
1787 if (pred->op == OP_BAND)
1788 pred->op = OP_EQ;
1789
1790 pred->fn_num = FILTER_PRED_FN_CPU;
1791 } else {
1792 pred->fn_num = FILTER_PRED_FN_CPU_CPUMASK;
1793 }
1794 } else if (single) {
1795 if (pred->op == OP_BAND)
1796 pred->op = OP_EQ;
1797
1798 pred->fn_num = select_comparison_fn(op: pred->op, field_size: field->size, field_is_signed: false);
1799 if (pred->op == OP_NE)
1800 pred->not = 1;
1801 } else {
1802 switch (field->size) {
1803 case 8:
1804 pred->fn_num = FILTER_PRED_FN_64_CPUMASK;
1805 break;
1806 case 4:
1807 pred->fn_num = FILTER_PRED_FN_32_CPUMASK;
1808 break;
1809 case 2:
1810 pred->fn_num = FILTER_PRED_FN_16_CPUMASK;
1811 break;
1812 case 1:
1813 pred->fn_num = FILTER_PRED_FN_8_CPUMASK;
1814 break;
1815 }
1816 }
1817
1818 /* This is either a string, or an integer */
1819 } else if (str[i] == '\'' || str[i] == '"') {
1820 char q = str[i];
1821
1822 /* Make sure the op is OK for strings */
1823 switch (op) {
1824 case OP_NE:
1825 pred->not = 1;
1826 fallthrough;
1827 case OP_GLOB:
1828 case OP_EQ:
1829 break;
1830 default:
1831 parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i);
1832 goto err_free;
1833 }
1834
1835 /* Make sure the field is OK for strings */
1836 if (!is_string_field(field)) {
1837 parse_error(pe, err: FILT_ERR_EXPECT_DIGIT, pos: pos + i);
1838 goto err_free;
1839 }
1840
1841 for (i++; str[i]; i++) {
1842 if (str[i] == q)
1843 break;
1844 }
1845 if (!str[i]) {
1846 parse_error(pe, err: FILT_ERR_MISSING_QUOTE, pos: pos + i);
1847 goto err_free;
1848 }
1849
1850 /* Skip quotes */
1851 s++;
1852 len = i - s;
1853 if (len >= MAX_FILTER_STR_VAL) {
1854 parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i);
1855 goto err_free;
1856 }
1857
1858 pred->regex = kzalloc(size: sizeof(*pred->regex), GFP_KERNEL);
1859 if (!pred->regex)
1860 goto err_mem;
1861 pred->regex->len = len;
1862 strncpy(p: pred->regex->pattern, q: str + s, size: len);
1863 pred->regex->pattern[len] = 0;
1864
1865 filter_build_regex(pred);
1866
1867 if (field->filter_type == FILTER_COMM) {
1868 pred->fn_num = FILTER_PRED_FN_COMM;
1869
1870 } else if (field->filter_type == FILTER_STATIC_STRING) {
1871 pred->fn_num = FILTER_PRED_FN_STRING;
1872 pred->regex->field_len = field->size;
1873
1874 } else if (field->filter_type == FILTER_DYN_STRING) {
1875 pred->fn_num = FILTER_PRED_FN_STRLOC;
1876 } else if (field->filter_type == FILTER_RDYN_STRING)
1877 pred->fn_num = FILTER_PRED_FN_STRRELLOC;
1878 else {
1879
1880 if (!ustring_per_cpu) {
1881 /* Once allocated, keep it around for good */
1882 ustring_per_cpu = alloc_percpu(struct ustring_buffer);
1883 if (!ustring_per_cpu)
1884 goto err_mem;
1885 }
1886
1887 if (ustring)
1888 pred->fn_num = FILTER_PRED_FN_PCHAR_USER;
1889 else
1890 pred->fn_num = FILTER_PRED_FN_PCHAR;
1891 }
1892 /* go past the last quote */
1893 i++;
1894
1895 } else if (isdigit(c: str[i]) || str[i] == '-') {
1896
1897 /* Make sure the field is not a string */
1898 if (is_string_field(field)) {
1899 parse_error(pe, err: FILT_ERR_EXPECT_STRING, pos: pos + i);
1900 goto err_free;
1901 }
1902
1903 if (op == OP_GLOB) {
1904 parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i);
1905 goto err_free;
1906 }
1907
1908 if (str[i] == '-')
1909 i++;
1910
1911 /* We allow 0xDEADBEEF */
1912 while (isalnum(str[i]))
1913 i++;
1914
1915 len = i - s;
1916 /* 0xfeedfacedeadbeef is 18 chars max */
1917 if (len >= sizeof(num_buf)) {
1918 parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i);
1919 goto err_free;
1920 }
1921
1922 strncpy(p: num_buf, q: str + s, size: len);
1923 num_buf[len] = 0;
1924
1925 /* Make sure it is a value */
1926 if (field->is_signed)
1927 ret = kstrtoll(s: num_buf, base: 0, res: &val);
1928 else
1929 ret = kstrtoull(s: num_buf, base: 0, res: &val);
1930 if (ret) {
1931 parse_error(pe, err: FILT_ERR_ILLEGAL_INTVAL, pos: pos + s);
1932 goto err_free;
1933 }
1934
1935 pred->val = val;
1936
1937 if (field->filter_type == FILTER_CPU)
1938 pred->fn_num = FILTER_PRED_FN_CPU;
1939 else {
1940 pred->fn_num = select_comparison_fn(op: pred->op, field_size: field->size,
1941 field_is_signed: field->is_signed);
1942 if (pred->op == OP_NE)
1943 pred->not = 1;
1944 }
1945
1946 } else {
1947 parse_error(pe, err: FILT_ERR_INVALID_VALUE, pos: pos + i);
1948 goto err_free;
1949 }
1950
1951 *pred_ptr = pred;
1952 return i;
1953
1954err_free:
1955 free_predicate(pred);
1956 return -EINVAL;
1957err_mem:
1958 free_predicate(pred);
1959 return -ENOMEM;
1960}
1961
1962enum {
1963 TOO_MANY_CLOSE = -1,
1964 TOO_MANY_OPEN = -2,
1965 MISSING_QUOTE = -3,
1966};
1967
1968/*
1969 * Read the filter string once to calculate the number of predicates
1970 * as well as how deep the parentheses go.
1971 *
1972 * Returns:
1973 * 0 - everything is fine (err is undefined)
1974 * -1 - too many ')'
1975 * -2 - too many '('
1976 * -3 - No matching quote
1977 */
1978static int calc_stack(const char *str, int *parens, int *preds, int *err)
1979{
1980 bool is_pred = false;
1981 int nr_preds = 0;
1982 int open = 1; /* Count the expression as "(E)" */
1983 int last_quote = 0;
1984 int max_open = 1;
1985 int quote = 0;
1986 int i;
1987
1988 *err = 0;
1989
1990 for (i = 0; str[i]; i++) {
1991 if (isspace(str[i]))
1992 continue;
1993 if (quote) {
1994 if (str[i] == quote)
1995 quote = 0;
1996 continue;
1997 }
1998
1999 switch (str[i]) {
2000 case '\'':
2001 case '"':
2002 quote = str[i];
2003 last_quote = i;
2004 break;
2005 case '|':
2006 case '&':
2007 if (str[i+1] != str[i])
2008 break;
2009 is_pred = false;
2010 continue;
2011 case '(':
2012 is_pred = false;
2013 open++;
2014 if (open > max_open)
2015 max_open = open;
2016 continue;
2017 case ')':
2018 is_pred = false;
2019 if (open == 1) {
2020 *err = i;
2021 return TOO_MANY_CLOSE;
2022 }
2023 open--;
2024 continue;
2025 }
2026 if (!is_pred) {
2027 nr_preds++;
2028 is_pred = true;
2029 }
2030 }
2031
2032 if (quote) {
2033 *err = last_quote;
2034 return MISSING_QUOTE;
2035 }
2036
2037 if (open != 1) {
2038 int level = open;
2039
2040 /* find the bad open */
2041 for (i--; i; i--) {
2042 if (quote) {
2043 if (str[i] == quote)
2044 quote = 0;
2045 continue;
2046 }
2047 switch (str[i]) {
2048 case '(':
2049 if (level == open) {
2050 *err = i;
2051 return TOO_MANY_OPEN;
2052 }
2053 level--;
2054 break;
2055 case ')':
2056 level++;
2057 break;
2058 case '\'':
2059 case '"':
2060 quote = str[i];
2061 break;
2062 }
2063 }
2064 /* First character is the '(' with missing ')' */
2065 *err = 0;
2066 return TOO_MANY_OPEN;
2067 }
2068
2069 /* Set the size of the required stacks */
2070 *parens = max_open;
2071 *preds = nr_preds;
2072 return 0;
2073}
2074
2075static int process_preds(struct trace_event_call *call,
2076 const char *filter_string,
2077 struct event_filter *filter,
2078 struct filter_parse_error *pe)
2079{
2080 struct prog_entry *prog;
2081 int nr_parens;
2082 int nr_preds;
2083 int index;
2084 int ret;
2085
2086 ret = calc_stack(str: filter_string, parens: &nr_parens, preds: &nr_preds, err: &index);
2087 if (ret < 0) {
2088 switch (ret) {
2089 case MISSING_QUOTE:
2090 parse_error(pe, err: FILT_ERR_MISSING_QUOTE, pos: index);
2091 break;
2092 case TOO_MANY_OPEN:
2093 parse_error(pe, err: FILT_ERR_TOO_MANY_OPEN, pos: index);
2094 break;
2095 default:
2096 parse_error(pe, err: FILT_ERR_TOO_MANY_CLOSE, pos: index);
2097 }
2098 return ret;
2099 }
2100
2101 if (!nr_preds)
2102 return -EINVAL;
2103
2104 prog = predicate_parse(str: filter_string, nr_parens, nr_preds,
2105 parse_pred, data: call, pe);
2106 if (IS_ERR(ptr: prog))
2107 return PTR_ERR(ptr: prog);
2108
2109 rcu_assign_pointer(filter->prog, prog);
2110 return 0;
2111}
2112
2113static inline void event_set_filtered_flag(struct trace_event_file *file)
2114{
2115 unsigned long old_flags = file->flags;
2116
2117 file->flags |= EVENT_FILE_FL_FILTERED;
2118
2119 if (old_flags != file->flags)
2120 trace_buffered_event_enable();
2121}
2122
2123static inline void event_set_filter(struct trace_event_file *file,
2124 struct event_filter *filter)
2125{
2126 rcu_assign_pointer(file->filter, filter);
2127}
2128
2129static inline void event_clear_filter(struct trace_event_file *file)
2130{
2131 RCU_INIT_POINTER(file->filter, NULL);
2132}
2133
2134struct filter_list {
2135 struct list_head list;
2136 struct event_filter *filter;
2137};
2138
2139static int process_system_preds(struct trace_subsystem_dir *dir,
2140 struct trace_array *tr,
2141 struct filter_parse_error *pe,
2142 char *filter_string)
2143{
2144 struct trace_event_file *file;
2145 struct filter_list *filter_item;
2146 struct event_filter *filter = NULL;
2147 struct filter_list *tmp;
2148 LIST_HEAD(filter_list);
2149 bool fail = true;
2150 int err;
2151
2152 list_for_each_entry(file, &tr->events, list) {
2153
2154 if (file->system != dir)
2155 continue;
2156
2157 filter = kzalloc(size: sizeof(*filter), GFP_KERNEL);
2158 if (!filter)
2159 goto fail_mem;
2160
2161 filter->filter_string = kstrdup(s: filter_string, GFP_KERNEL);
2162 if (!filter->filter_string)
2163 goto fail_mem;
2164
2165 err = process_preds(call: file->event_call, filter_string, filter, pe);
2166 if (err) {
2167 filter_disable(file);
2168 parse_error(pe, err: FILT_ERR_BAD_SUBSYS_FILTER, pos: 0);
2169 append_filter_err(tr, pe, filter);
2170 } else
2171 event_set_filtered_flag(file);
2172
2173
2174 filter_item = kzalloc(size: sizeof(*filter_item), GFP_KERNEL);
2175 if (!filter_item)
2176 goto fail_mem;
2177
2178 list_add_tail(new: &filter_item->list, head: &filter_list);
2179 /*
2180 * Regardless of if this returned an error, we still
2181 * replace the filter for the call.
2182 */
2183 filter_item->filter = event_filter(file);
2184 event_set_filter(file, filter);
2185 filter = NULL;
2186
2187 fail = false;
2188 }
2189
2190 if (fail)
2191 goto fail;
2192
2193 /*
2194 * The calls can still be using the old filters.
2195 * Do a synchronize_rcu() and to ensure all calls are
2196 * done with them before we free them.
2197 */
2198 tracepoint_synchronize_unregister();
2199 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
2200 __free_filter(filter: filter_item->filter);
2201 list_del(entry: &filter_item->list);
2202 kfree(objp: filter_item);
2203 }
2204 return 0;
2205 fail:
2206 /* No call succeeded */
2207 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
2208 list_del(entry: &filter_item->list);
2209 kfree(objp: filter_item);
2210 }
2211 parse_error(pe, err: FILT_ERR_BAD_SUBSYS_FILTER, pos: 0);
2212 return -EINVAL;
2213 fail_mem:
2214 __free_filter(filter);
2215 /* If any call succeeded, we still need to sync */
2216 if (!fail)
2217 tracepoint_synchronize_unregister();
2218 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
2219 __free_filter(filter: filter_item->filter);
2220 list_del(entry: &filter_item->list);
2221 kfree(objp: filter_item);
2222 }
2223 return -ENOMEM;
2224}
2225
2226static int create_filter_start(char *filter_string, bool set_str,
2227 struct filter_parse_error **pse,
2228 struct event_filter **filterp)
2229{
2230 struct event_filter *filter;
2231 struct filter_parse_error *pe = NULL;
2232 int err = 0;
2233
2234 if (WARN_ON_ONCE(*pse || *filterp))
2235 return -EINVAL;
2236
2237 filter = kzalloc(size: sizeof(*filter), GFP_KERNEL);
2238 if (filter && set_str) {
2239 filter->filter_string = kstrdup(s: filter_string, GFP_KERNEL);
2240 if (!filter->filter_string)
2241 err = -ENOMEM;
2242 }
2243
2244 pe = kzalloc(size: sizeof(*pe), GFP_KERNEL);
2245
2246 if (!filter || !pe || err) {
2247 kfree(objp: pe);
2248 __free_filter(filter);
2249 return -ENOMEM;
2250 }
2251
2252 /* we're committed to creating a new filter */
2253 *filterp = filter;
2254 *pse = pe;
2255
2256 return 0;
2257}
2258
2259static void create_filter_finish(struct filter_parse_error *pe)
2260{
2261 kfree(objp: pe);
2262}
2263
2264/**
2265 * create_filter - create a filter for a trace_event_call
2266 * @tr: the trace array associated with these events
2267 * @call: trace_event_call to create a filter for
2268 * @filter_string: filter string
2269 * @set_str: remember @filter_str and enable detailed error in filter
2270 * @filterp: out param for created filter (always updated on return)
2271 * Must be a pointer that references a NULL pointer.
2272 *
2273 * Creates a filter for @call with @filter_str. If @set_str is %true,
2274 * @filter_str is copied and recorded in the new filter.
2275 *
2276 * On success, returns 0 and *@filterp points to the new filter. On
2277 * failure, returns -errno and *@filterp may point to %NULL or to a new
2278 * filter. In the latter case, the returned filter contains error
2279 * information if @set_str is %true and the caller is responsible for
2280 * freeing it.
2281 */
2282static int create_filter(struct trace_array *tr,
2283 struct trace_event_call *call,
2284 char *filter_string, bool set_str,
2285 struct event_filter **filterp)
2286{
2287 struct filter_parse_error *pe = NULL;
2288 int err;
2289
2290 /* filterp must point to NULL */
2291 if (WARN_ON(*filterp))
2292 *filterp = NULL;
2293
2294 err = create_filter_start(filter_string, set_str, pse: &pe, filterp);
2295 if (err)
2296 return err;
2297
2298 err = process_preds(call, filter_string, filter: *filterp, pe);
2299 if (err && set_str)
2300 append_filter_err(tr, pe, filter: *filterp);
2301 create_filter_finish(pe);
2302
2303 return err;
2304}
2305
2306int create_event_filter(struct trace_array *tr,
2307 struct trace_event_call *call,
2308 char *filter_str, bool set_str,
2309 struct event_filter **filterp)
2310{
2311 return create_filter(tr, call, filter_string: filter_str, set_str, filterp);
2312}
2313
2314/**
2315 * create_system_filter - create a filter for an event subsystem
2316 * @dir: the descriptor for the subsystem directory
2317 * @filter_str: filter string
2318 * @filterp: out param for created filter (always updated on return)
2319 *
2320 * Identical to create_filter() except that it creates a subsystem filter
2321 * and always remembers @filter_str.
2322 */
2323static int create_system_filter(struct trace_subsystem_dir *dir,
2324 char *filter_str, struct event_filter **filterp)
2325{
2326 struct filter_parse_error *pe = NULL;
2327 int err;
2328
2329 err = create_filter_start(filter_string: filter_str, set_str: true, pse: &pe, filterp);
2330 if (!err) {
2331 err = process_system_preds(dir, tr: dir->tr, pe, filter_string: filter_str);
2332 if (!err) {
2333 /* System filters just show a default message */
2334 kfree(objp: (*filterp)->filter_string);
2335 (*filterp)->filter_string = NULL;
2336 } else {
2337 append_filter_err(tr: dir->tr, pe, filter: *filterp);
2338 }
2339 }
2340 create_filter_finish(pe);
2341
2342 return err;
2343}
2344
2345/* caller must hold event_mutex */
2346int apply_event_filter(struct trace_event_file *file, char *filter_string)
2347{
2348 struct trace_event_call *call = file->event_call;
2349 struct event_filter *filter = NULL;
2350 int err;
2351
2352 if (file->flags & EVENT_FILE_FL_FREED)
2353 return -ENODEV;
2354
2355 if (!strcmp(strstrip(str: filter_string), "0")) {
2356 filter_disable(file);
2357 filter = event_filter(file);
2358
2359 if (!filter)
2360 return 0;
2361
2362 event_clear_filter(file);
2363
2364 /* Make sure the filter is not being used */
2365 tracepoint_synchronize_unregister();
2366 __free_filter(filter);
2367
2368 return 0;
2369 }
2370
2371 err = create_filter(tr: file->tr, call, filter_string, set_str: true, filterp: &filter);
2372
2373 /*
2374 * Always swap the call filter with the new filter
2375 * even if there was an error. If there was an error
2376 * in the filter, we disable the filter and show the error
2377 * string
2378 */
2379 if (filter) {
2380 struct event_filter *tmp;
2381
2382 tmp = event_filter(file);
2383 if (!err)
2384 event_set_filtered_flag(file);
2385 else
2386 filter_disable(file);
2387
2388 event_set_filter(file, filter);
2389
2390 if (tmp) {
2391 /* Make sure the call is done with the filter */
2392 tracepoint_synchronize_unregister();
2393 __free_filter(filter: tmp);
2394 }
2395 }
2396
2397 return err;
2398}
2399
2400int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
2401 char *filter_string)
2402{
2403 struct event_subsystem *system = dir->subsystem;
2404 struct trace_array *tr = dir->tr;
2405 struct event_filter *filter = NULL;
2406 int err = 0;
2407
2408 mutex_lock(&event_mutex);
2409
2410 /* Make sure the system still has events */
2411 if (!dir->nr_events) {
2412 err = -ENODEV;
2413 goto out_unlock;
2414 }
2415
2416 if (!strcmp(strstrip(str: filter_string), "0")) {
2417 filter_free_subsystem_preds(dir, tr);
2418 remove_filter_string(filter: system->filter);
2419 filter = system->filter;
2420 system->filter = NULL;
2421 /* Ensure all filters are no longer used */
2422 tracepoint_synchronize_unregister();
2423 filter_free_subsystem_filters(dir, tr);
2424 __free_filter(filter);
2425 goto out_unlock;
2426 }
2427
2428 err = create_system_filter(dir, filter_str: filter_string, filterp: &filter);
2429 if (filter) {
2430 /*
2431 * No event actually uses the system filter
2432 * we can free it without synchronize_rcu().
2433 */
2434 __free_filter(filter: system->filter);
2435 system->filter = filter;
2436 }
2437out_unlock:
2438 mutex_unlock(lock: &event_mutex);
2439
2440 return err;
2441}
2442
2443#ifdef CONFIG_PERF_EVENTS
2444
2445void ftrace_profile_free_filter(struct perf_event *event)
2446{
2447 struct event_filter *filter = event->filter;
2448
2449 event->filter = NULL;
2450 __free_filter(filter);
2451}
2452
2453struct function_filter_data {
2454 struct ftrace_ops *ops;
2455 int first_filter;
2456 int first_notrace;
2457};
2458
2459#ifdef CONFIG_FUNCTION_TRACER
2460static char **
2461ftrace_function_filter_re(char *buf, int len, int *count)
2462{
2463 char *str, **re;
2464
2465 str = kstrndup(s: buf, len, GFP_KERNEL);
2466 if (!str)
2467 return NULL;
2468
2469 /*
2470 * The argv_split function takes white space
2471 * as a separator, so convert ',' into spaces.
2472 */
2473 strreplace(str, old: ',', new: ' ');
2474
2475 re = argv_split(GFP_KERNEL, str, argcp: count);
2476 kfree(objp: str);
2477 return re;
2478}
2479
2480static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2481 int reset, char *re, int len)
2482{
2483 int ret;
2484
2485 if (filter)
2486 ret = ftrace_set_filter(ops, buf: re, len, reset);
2487 else
2488 ret = ftrace_set_notrace(ops, buf: re, len, reset);
2489
2490 return ret;
2491}
2492
2493static int __ftrace_function_set_filter(int filter, char *buf, int len,
2494 struct function_filter_data *data)
2495{
2496 int i, re_cnt, ret = -EINVAL;
2497 int *reset;
2498 char **re;
2499
2500 reset = filter ? &data->first_filter : &data->first_notrace;
2501
2502 /*
2503 * The 'ip' field could have multiple filters set, separated
2504 * either by space or comma. We first cut the filter and apply
2505 * all pieces separately.
2506 */
2507 re = ftrace_function_filter_re(buf, len, count: &re_cnt);
2508 if (!re)
2509 return -EINVAL;
2510
2511 for (i = 0; i < re_cnt; i++) {
2512 ret = ftrace_function_set_regexp(ops: data->ops, filter, reset: *reset,
2513 re: re[i], strlen(re[i]));
2514 if (ret)
2515 break;
2516
2517 if (*reset)
2518 *reset = 0;
2519 }
2520
2521 argv_free(argv: re);
2522 return ret;
2523}
2524
2525static int ftrace_function_check_pred(struct filter_pred *pred)
2526{
2527 struct ftrace_event_field *field = pred->field;
2528
2529 /*
2530 * Check the predicate for function trace, verify:
2531 * - only '==' and '!=' is used
2532 * - the 'ip' field is used
2533 */
2534 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2535 return -EINVAL;
2536
2537 if (strcmp(field->name, "ip"))
2538 return -EINVAL;
2539
2540 return 0;
2541}
2542
2543static int ftrace_function_set_filter_pred(struct filter_pred *pred,
2544 struct function_filter_data *data)
2545{
2546 int ret;
2547
2548 /* Checking the node is valid for function trace. */
2549 ret = ftrace_function_check_pred(pred);
2550 if (ret)
2551 return ret;
2552
2553 return __ftrace_function_set_filter(filter: pred->op == OP_EQ,
2554 buf: pred->regex->pattern,
2555 len: pred->regex->len,
2556 data);
2557}
2558
2559static bool is_or(struct prog_entry *prog, int i)
2560{
2561 int target;
2562
2563 /*
2564 * Only "||" is allowed for function events, thus,
2565 * all true branches should jump to true, and any
2566 * false branch should jump to false.
2567 */
2568 target = prog[i].target + 1;
2569 /* True and false have NULL preds (all prog entries should jump to one */
2570 if (prog[target].pred)
2571 return false;
2572
2573 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2574 return prog[i].when_to_branch == prog[target].target;
2575}
2576
2577static int ftrace_function_set_filter(struct perf_event *event,
2578 struct event_filter *filter)
2579{
2580 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2581 lockdep_is_held(&event_mutex));
2582 struct function_filter_data data = {
2583 .first_filter = 1,
2584 .first_notrace = 1,
2585 .ops = &event->ftrace_ops,
2586 };
2587 int i;
2588
2589 for (i = 0; prog[i].pred; i++) {
2590 struct filter_pred *pred = prog[i].pred;
2591
2592 if (!is_or(prog, i))
2593 return -EINVAL;
2594
2595 if (ftrace_function_set_filter_pred(pred, data: &data) < 0)
2596 return -EINVAL;
2597 }
2598 return 0;
2599}
2600#else
2601static int ftrace_function_set_filter(struct perf_event *event,
2602 struct event_filter *filter)
2603{
2604 return -ENODEV;
2605}
2606#endif /* CONFIG_FUNCTION_TRACER */
2607
2608int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2609 char *filter_str)
2610{
2611 int err;
2612 struct event_filter *filter = NULL;
2613 struct trace_event_call *call;
2614
2615 mutex_lock(&event_mutex);
2616
2617 call = event->tp_event;
2618
2619 err = -EINVAL;
2620 if (!call)
2621 goto out_unlock;
2622
2623 err = -EEXIST;
2624 if (event->filter)
2625 goto out_unlock;
2626
2627 err = create_filter(NULL, call, filter_string: filter_str, set_str: false, filterp: &filter);
2628 if (err)
2629 goto free_filter;
2630
2631 if (ftrace_event_is_function(call))
2632 err = ftrace_function_set_filter(event, filter);
2633 else
2634 event->filter = filter;
2635
2636free_filter:
2637 if (err || ftrace_event_is_function(call))
2638 __free_filter(filter);
2639
2640out_unlock:
2641 mutex_unlock(lock: &event_mutex);
2642
2643 return err;
2644}
2645
2646#endif /* CONFIG_PERF_EVENTS */
2647
2648#ifdef CONFIG_FTRACE_STARTUP_TEST
2649
2650#include <linux/types.h>
2651#include <linux/tracepoint.h>
2652
2653#define CREATE_TRACE_POINTS
2654#include "trace_events_filter_test.h"
2655
2656#define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2657{ \
2658 .filter = FILTER, \
2659 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2660 .e = ve, .f = vf, .g = vg, .h = vh }, \
2661 .match = m, \
2662 .not_visited = nvisit, \
2663}
2664#define YES 1
2665#define NO 0
2666
2667static struct test_filter_data_t {
2668 char *filter;
2669 struct trace_event_raw_ftrace_test_filter rec;
2670 int match;
2671 char *not_visited;
2672} test_filter_data[] = {
2673#define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2674 "e == 1 && f == 1 && g == 1 && h == 1"
2675 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2676 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2677 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2678#undef FILTER
2679#define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2680 "e == 1 || f == 1 || g == 1 || h == 1"
2681 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2682 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2683 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2684#undef FILTER
2685#define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2686 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2687 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2688 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2689 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2690 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2691#undef FILTER
2692#define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2693 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2694 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2695 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2696 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2697#undef FILTER
2698#define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2699 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2700 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2701 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2702 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2703#undef FILTER
2704#define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2705 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2706 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2707 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2708 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2709#undef FILTER
2710#define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2711 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2712 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2713 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2714 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2715#undef FILTER
2716#define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2717 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2718 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2719 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2720 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2721};
2722
2723#undef DATA_REC
2724#undef FILTER
2725#undef YES
2726#undef NO
2727
2728#define DATA_CNT ARRAY_SIZE(test_filter_data)
2729
2730static int test_pred_visited;
2731
2732static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2733{
2734 struct ftrace_event_field *field = pred->field;
2735
2736 test_pred_visited = 1;
2737 printk(KERN_INFO "\npred visited %s\n", field->name);
2738 return 1;
2739}
2740
2741static void update_pred_fn(struct event_filter *filter, char *fields)
2742{
2743 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2744 lockdep_is_held(&event_mutex));
2745 int i;
2746
2747 for (i = 0; prog[i].pred; i++) {
2748 struct filter_pred *pred = prog[i].pred;
2749 struct ftrace_event_field *field = pred->field;
2750
2751 WARN_ON_ONCE(pred->fn_num == FILTER_PRED_FN_NOP);
2752
2753 if (!field) {
2754 WARN_ONCE(1, "all leafs should have field defined %d", i);
2755 continue;
2756 }
2757
2758 if (!strchr(fields, *field->name))
2759 continue;
2760
2761 pred->fn_num = FILTER_PRED_TEST_VISITED;
2762 }
2763}
2764
2765static __init int ftrace_test_event_filter(void)
2766{
2767 int i;
2768
2769 printk(KERN_INFO "Testing ftrace filter: ");
2770
2771 for (i = 0; i < DATA_CNT; i++) {
2772 struct event_filter *filter = NULL;
2773 struct test_filter_data_t *d = &test_filter_data[i];
2774 int err;
2775
2776 err = create_filter(NULL, call: &event_ftrace_test_filter,
2777 filter_string: d->filter, set_str: false, filterp: &filter);
2778 if (err) {
2779 printk(KERN_INFO
2780 "Failed to get filter for '%s', err %d\n",
2781 d->filter, err);
2782 __free_filter(filter);
2783 break;
2784 }
2785
2786 /* Needed to dereference filter->prog */
2787 mutex_lock(&event_mutex);
2788 /*
2789 * The preemption disabling is not really needed for self
2790 * tests, but the rcu dereference will complain without it.
2791 */
2792 preempt_disable();
2793 if (*d->not_visited)
2794 update_pred_fn(filter, fields: d->not_visited);
2795
2796 test_pred_visited = 0;
2797 err = filter_match_preds(filter, &d->rec);
2798 preempt_enable();
2799
2800 mutex_unlock(lock: &event_mutex);
2801
2802 __free_filter(filter);
2803
2804 if (test_pred_visited) {
2805 printk(KERN_INFO
2806 "Failed, unwanted pred visited for filter %s\n",
2807 d->filter);
2808 break;
2809 }
2810
2811 if (err != d->match) {
2812 printk(KERN_INFO
2813 "Failed to match filter '%s', expected %d\n",
2814 d->filter, d->match);
2815 break;
2816 }
2817 }
2818
2819 if (i == DATA_CNT)
2820 printk(KERN_CONT "OK\n");
2821
2822 return 0;
2823}
2824
2825late_initcall(ftrace_test_event_filter);
2826
2827#endif /* CONFIG_FTRACE_STARTUP_TEST */
2828

source code of linux/kernel/trace/trace_events_filter.c