iommu/amd: Apply the same IVRS IOAPIC workaround to Acer Aspire A315-41
[linux-2.6-block.git] / kernel / trace / trace_events_filter.c
CommitLineData
bcea3f96 1// SPDX-License-Identifier: GPL-2.0
7ce7e424
TZ
2/*
3 * trace_events_filter - generic event filtering
4 *
7ce7e424
TZ
5 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
6 */
7
7ce7e424
TZ
8#include <linux/module.h>
9#include <linux/ctype.h>
ac1adc55 10#include <linux/mutex.h>
6fb2915d 11#include <linux/perf_event.h>
5a0e3ad6 12#include <linux/slab.h>
7ce7e424
TZ
13
14#include "trace.h"
4bda2d51 15#include "trace_output.h"
7ce7e424 16
49aa2951
SR
17#define DEFAULT_SYS_FILTER_MESSAGE \
18 "### global filter ###\n" \
19 "# Use this to set filters for multiple events.\n" \
20 "# Only events with the given fields will be affected.\n" \
21 "# If no events are modified, an error message will be displayed here"
22
80765597 23/* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
e9baef0d 24#define OPS \
80765597
SRV
25 C( OP_GLOB, "~" ), \
26 C( OP_NE, "!=" ), \
27 C( OP_EQ, "==" ), \
28 C( OP_LE, "<=" ), \
29 C( OP_LT, "<" ), \
30 C( OP_GE, ">=" ), \
31 C( OP_GT, ">" ), \
32 C( OP_BAND, "&" ), \
33 C( OP_MAX, NULL )
e9baef0d
SRV
34
35#undef C
80765597 36#define C(a, b) a
e9baef0d
SRV
37
38enum filter_op_ids { OPS };
8b372562 39
e9baef0d 40#undef C
80765597 41#define C(a, b) b
8b372562 42
80765597 43static const char * ops[] = { OPS };
8b372562 44
478325f1 45/*
80765597 46 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
478325f1
SRV
47 * pred_funcs_##type below must match the order of them above.
48 */
80765597 49#define PRED_FUNC_START OP_LE
478325f1
SRV
50#define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
51
e9baef0d 52#define ERRORS \
80765597
SRV
53 C(NONE, "No error"), \
54 C(INVALID_OP, "Invalid operator"), \
55 C(TOO_MANY_OPEN, "Too many '('"), \
56 C(TOO_MANY_CLOSE, "Too few '('"), \
57 C(MISSING_QUOTE, "Missing matching quote"), \
58 C(OPERAND_TOO_LONG, "Operand too long"), \
59 C(EXPECT_STRING, "Expecting string field"), \
60 C(EXPECT_DIGIT, "Expecting numeric field"), \
61 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \
62 C(FIELD_NOT_FOUND, "Field not found"), \
63 C(ILLEGAL_INTVAL, "Illegal integer value"), \
64 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \
65 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \
66 C(INVALID_FILTER, "Meaningless filter expression"), \
67 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \
70303420 68 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \
34f76afa
TZ
69 C(ERRNO, "Error"), \
70 C(NO_FILTER, "No filter found")
e9baef0d
SRV
71
72#undef C
73#define C(a, b) FILT_ERR_##a
74
75enum { ERRORS };
76
77#undef C
78#define C(a, b) b
79
34f76afa 80static const char *err_text[] = { ERRORS };
8b372562 81
80765597
SRV
82/* Called after a '!' character but "!=" and "!~" are not "not"s */
83static bool is_not(const char *str)
84{
85 switch (str[1]) {
86 case '=':
87 case '~':
88 return false;
89 }
90 return true;
91}
8b372562 92
80765597
SRV
93/**
94 * prog_entry - a singe entry in the filter program
95 * @target: Index to jump to on a branch (actually one minus the index)
96 * @when_to_branch: The value of the result of the predicate to do a branch
97 * @pred: The predicate to execute.
98 */
99struct prog_entry {
100 int target;
101 int when_to_branch;
102 struct filter_pred *pred;
8b372562
TZ
103};
104
80765597
SRV
105/**
106 * update_preds- assign a program entry a label target
107 * @prog: The program array
108 * @N: The index of the current entry in @prog
109 * @when_to_branch: What to assign a program entry for its branch condition
110 *
111 * The program entry at @N has a target that points to the index of a program
112 * entry that can have its target and when_to_branch fields updated.
113 * Update the current program entry denoted by index @N target field to be
114 * that of the updated entry. This will denote the entry to update if
115 * we are processing an "||" after an "&&"
116 */
117static void update_preds(struct prog_entry *prog, int N, int invert)
118{
119 int t, s;
120
121 t = prog[N].target;
122 s = prog[t].target;
123 prog[t].when_to_branch = invert;
124 prog[t].target = N;
125 prog[N].target = s;
126}
127
128struct filter_parse_error {
8b372562
TZ
129 int lasterr;
130 int lasterr_pos;
8b372562
TZ
131};
132
80765597
SRV
133static void parse_error(struct filter_parse_error *pe, int err, int pos)
134{
135 pe->lasterr = err;
136 pe->lasterr_pos = pos;
137}
138
139typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
140 struct filter_parse_error *pe,
141 struct filter_pred **pred);
142
143enum {
144 INVERT = 1,
145 PROCESS_AND = 2,
146 PROCESS_OR = 4,
61e9dea2
SR
147};
148
80765597
SRV
149/*
150 * Without going into a formal proof, this explains the method that is used in
151 * parsing the logical expressions.
152 *
153 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
154 * The first pass will convert it into the following program:
155 *
156 * n1: r=a; l1: if (!r) goto l4;
157 * n2: r=b; l2: if (!r) goto l4;
158 * n3: r=c; r=!r; l3: if (r) goto l4;
159 * n4: r=g; r=!r; l4: if (r) goto l5;
160 * n5: r=d; l5: if (r) goto T
161 * n6: r=e; l6: if (!r) goto l7;
162 * n7: r=f; r=!r; l7: if (!r) goto F
163 * T: return TRUE
164 * F: return FALSE
165 *
166 * To do this, we use a data structure to represent each of the above
167 * predicate and conditions that has:
168 *
169 * predicate, when_to_branch, invert, target
170 *
171 * The "predicate" will hold the function to determine the result "r".
172 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
173 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
174 * The "invert" holds whether the value should be reversed before testing.
175 * The "target" contains the label "l#" to jump to.
176 *
177 * A stack is created to hold values when parentheses are used.
178 *
179 * To simplify the logic, the labels will start at 0 and not 1.
180 *
181 * The possible invert values are 1 and 0. The number of "!"s that are in scope
182 * before the predicate determines the invert value, if the number is odd then
183 * the invert value is 1 and 0 otherwise. This means the invert value only
184 * needs to be toggled when a new "!" is introduced compared to what is stored
185 * on the stack, where parentheses were used.
186 *
187 * The top of the stack and "invert" are initialized to zero.
188 *
189 * ** FIRST PASS **
190 *
191 * #1 A loop through all the tokens is done:
192 *
193 * #2 If the token is an "(", the stack is push, and the current stack value
194 * gets the current invert value, and the loop continues to the next token.
195 * The top of the stack saves the "invert" value to keep track of what
196 * the current inversion is. As "!(a && !b || c)" would require all
197 * predicates being affected separately by the "!" before the parentheses.
198 * And that would end up being equivalent to "(!a || b) && !c"
199 *
200 * #3 If the token is an "!", the current "invert" value gets inverted, and
201 * the loop continues. Note, if the next token is a predicate, then
202 * this "invert" value is only valid for the current program entry,
203 * and does not affect other predicates later on.
204 *
205 * The only other acceptable token is the predicate string.
206 *
207 * #4 A new entry into the program is added saving: the predicate and the
208 * current value of "invert". The target is currently assigned to the
209 * previous program index (this will not be its final value).
210 *
211 * #5 We now enter another loop and look at the next token. The only valid
212 * tokens are ")", "&&", "||" or end of the input string "\0".
213 *
214 * #6 The invert variable is reset to the current value saved on the top of
215 * the stack.
216 *
217 * #7 The top of the stack holds not only the current invert value, but also
218 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
219 * precedence than "||". That is "a && b || c && d" is equivalent to
220 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
221 * to be processed. This is the case if an "&&" was the last token. If it was
222 * then we call update_preds(). This takes the program, the current index in
223 * the program, and the current value of "invert". More will be described
224 * below about this function.
225 *
226 * #8 If the next token is "&&" then we set a flag in the top of the stack
227 * that denotes that "&&" needs to be processed, break out of this loop
228 * and continue with the outer loop.
229 *
230 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
231 * This is called with the program, the current index in the program, but
232 * this time with an inverted value of "invert" (that is !invert). This is
233 * because the value taken will become the "when_to_branch" value of the
234 * program.
235 * Note, this is called when the next token is not an "&&". As stated before,
236 * "&&" takes higher precedence, and "||" should not be processed yet if the
237 * next logical operation is "&&".
238 *
239 * #10 If the next token is "||" then we set a flag in the top of the stack
240 * that denotes that "||" needs to be processed, break out of this loop
241 * and continue with the outer loop.
242 *
243 * #11 If this is the end of the input string "\0" then we break out of both
244 * loops.
245 *
246 * #12 Otherwise, the next token is ")", where we pop the stack and continue
247 * this inner loop.
248 *
249 * Now to discuss the update_pred() function, as that is key to the setting up
250 * of the program. Remember the "target" of the program is initialized to the
251 * previous index and not the "l" label. The target holds the index into the
252 * program that gets affected by the operand. Thus if we have something like
253 * "a || b && c", when we process "a" the target will be "-1" (undefined).
254 * When we process "b", its target is "0", which is the index of "a", as that's
255 * the predicate that is affected by "||". But because the next token after "b"
256 * is "&&" we don't call update_preds(). Instead continue to "c". As the
257 * next token after "c" is not "&&" but the end of input, we first process the
258 * "&&" by calling update_preds() for the "&&" then we process the "||" by
259 * callin updates_preds() with the values for processing "||".
260 *
261 * What does that mean? What update_preds() does is to first save the "target"
262 * of the program entry indexed by the current program entry's "target"
263 * (remember the "target" is initialized to previous program entry), and then
264 * sets that "target" to the current index which represents the label "l#".
265 * That entry's "when_to_branch" is set to the value passed in (the "invert"
266 * or "!invert"). Then it sets the current program entry's target to the saved
267 * "target" value (the old value of the program that had its "target" updated
268 * to the label).
269 *
270 * Looking back at "a || b && c", we have the following steps:
271 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
272 * "||" - flag that we need to process "||"; continue outer loop
273 * "b" - prog[1] = { "b", X, 0 }
274 * "&&" - flag that we need to process "&&"; continue outer loop
275 * (Notice we did not process "||")
276 * "c" - prog[2] = { "c", X, 1 }
277 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
278 * t = prog[2].target; // t = 1
279 * s = prog[t].target; // s = 0
280 * prog[t].target = 2; // Set target to "l2"
281 * prog[t].when_to_branch = 0;
282 * prog[2].target = s;
283 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
284 * t = prog[2].target; // t = 0
285 * s = prog[t].target; // s = -1
286 * prog[t].target = 2; // Set target to "l2"
287 * prog[t].when_to_branch = 1;
288 * prog[2].target = s;
289 *
290 * #13 Which brings us to the final step of the first pass, which is to set
291 * the last program entry's when_to_branch and target, which will be
292 * when_to_branch = 0; target = N; ( the label after the program entry after
293 * the last program entry processed above).
294 *
295 * If we denote "TRUE" to be the entry after the last program entry processed,
296 * and "FALSE" the program entry after that, we are now done with the first
297 * pass.
298 *
299 * Making the above "a || b && c" have a progam of:
300 * prog[0] = { "a", 1, 2 }
301 * prog[1] = { "b", 0, 2 }
302 * prog[2] = { "c", 0, 3 }
303 *
304 * Which translates into:
305 * n0: r = a; l0: if (r) goto l2;
306 * n1: r = b; l1: if (!r) goto l2;
307 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
308 * T: return TRUE; l3:
309 * F: return FALSE
310 *
311 * Although, after the first pass, the program is correct, it is
312 * inefficient. The simple sample of "a || b && c" could be easily been
313 * converted into:
314 * n0: r = a; if (r) goto T
315 * n1: r = b; if (!r) goto F
316 * n2: r = c; if (!r) goto F
317 * T: return TRUE;
318 * F: return FALSE;
319 *
320 * The First Pass is over the input string. The next too passes are over
321 * the program itself.
322 *
323 * ** SECOND PASS **
324 *
325 * Which brings us to the second pass. If a jump to a label has the
326 * same condition as that label, it can instead jump to its target.
327 * The original example of "a && !(!b || (c && g)) || d || e && !f"
328 * where the first pass gives us:
329 *
330 * n1: r=a; l1: if (!r) goto l4;
331 * n2: r=b; l2: if (!r) goto l4;
332 * n3: r=c; r=!r; l3: if (r) goto l4;
333 * n4: r=g; r=!r; l4: if (r) goto l5;
334 * n5: r=d; l5: if (r) goto T
335 * n6: r=e; l6: if (!r) goto l7;
336 * n7: r=f; r=!r; l7: if (!r) goto F:
337 * T: return TRUE;
338 * F: return FALSE
339 *
340 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
341 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
342 * to go directly to T. To accomplish this, we start from the last
343 * entry in the program and work our way back. If the target of the entry
344 * has the same "when_to_branch" then we could use that entry's target.
345 * Doing this, the above would end up as:
346 *
347 * n1: r=a; l1: if (!r) goto l4;
348 * n2: r=b; l2: if (!r) goto l4;
349 * n3: r=c; r=!r; l3: if (r) goto T;
350 * n4: r=g; r=!r; l4: if (r) goto T;
351 * n5: r=d; l5: if (r) goto T;
352 * n6: r=e; l6: if (!r) goto F;
353 * n7: r=f; r=!r; l7: if (!r) goto F;
354 * T: return TRUE
355 * F: return FALSE
356 *
357 * In that same pass, if the "when_to_branch" doesn't match, we can simply
358 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
359 * where "l4: if (r) goto T;", then we can convert l2 to be:
360 * "l2: if (!r) goto n5;".
361 *
362 * This will have the second pass give us:
363 * n1: r=a; l1: if (!r) goto n5;
364 * n2: r=b; l2: if (!r) goto n5;
365 * n3: r=c; r=!r; l3: if (r) goto T;
366 * n4: r=g; r=!r; l4: if (r) goto T;
367 * n5: r=d; l5: if (r) goto T
368 * n6: r=e; l6: if (!r) goto F;
369 * n7: r=f; r=!r; l7: if (!r) goto F
370 * T: return TRUE
371 * F: return FALSE
372 *
373 * Notice, all the "l#" labels are no longer used, and they can now
374 * be discarded.
375 *
376 * ** THIRD PASS **
377 *
378 * For the third pass we deal with the inverts. As they simply just
379 * make the "when_to_branch" get inverted, a simple loop over the
380 * program to that does: "when_to_branch ^= invert;" will do the
381 * job, leaving us with:
382 * n1: r=a; if (!r) goto n5;
383 * n2: r=b; if (!r) goto n5;
384 * n3: r=c: if (!r) goto T;
385 * n4: r=g; if (!r) goto T;
386 * n5: r=d; if (r) goto T
387 * n6: r=e; if (!r) goto F;
388 * n7: r=f; if (r) goto F
389 * T: return TRUE
390 * F: return FALSE
391 *
392 * As "r = a; if (!r) goto n5;" is obviously the same as
393 * "if (!a) goto n5;" without doing anything we can interperate the
394 * program as:
395 * n1: if (!a) goto n5;
396 * n2: if (!b) goto n5;
397 * n3: if (!c) goto T;
398 * n4: if (!g) goto T;
399 * n5: if (d) goto T
400 * n6: if (!e) goto F;
401 * n7: if (f) goto F
402 * T: return TRUE
403 * F: return FALSE
404 *
405 * Since the inverts are discarded at the end, there's no reason to store
406 * them in the program array (and waste memory). A separate array to hold
407 * the inverts is used and freed at the end.
408 */
409static struct prog_entry *
410predicate_parse(const char *str, int nr_parens, int nr_preds,
411 parse_pred_fn parse_pred, void *data,
412 struct filter_parse_error *pe)
413{
414 struct prog_entry *prog_stack;
415 struct prog_entry *prog;
416 const char *ptr = str;
417 char *inverts = NULL;
418 int *op_stack;
419 int *top;
420 int invert = 0;
421 int ret = -ENOMEM;
422 int len;
423 int N = 0;
424 int i;
425
426 nr_preds += 2; /* For TRUE and FALSE */
427
6da2ec56 428 op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
80765597
SRV
429 if (!op_stack)
430 return ERR_PTR(-ENOMEM);
dfb4a6f2 431 prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
80765597
SRV
432 if (!prog_stack) {
433 parse_error(pe, -ENOMEM, 0);
434 goto out_free;
435 }
6da2ec56 436 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
80765597
SRV
437 if (!inverts) {
438 parse_error(pe, -ENOMEM, 0);
439 goto out_free;
440 }
441
442 top = op_stack;
443 prog = prog_stack;
444 *top = 0;
445
446 /* First pass */
447 while (*ptr) { /* #1 */
448 const char *next = ptr++;
449
450 if (isspace(*next))
451 continue;
452
453 switch (*next) {
454 case '(': /* #2 */
96c5c6e6
NE
455 if (top - op_stack > nr_parens) {
456 ret = -EINVAL;
457 goto out_free;
458 }
80765597
SRV
459 *(++top) = invert;
460 continue;
461 case '!': /* #3 */
462 if (!is_not(next))
463 break;
464 invert = !invert;
465 continue;
466 }
467
468 if (N >= nr_preds) {
469 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
470 goto out_free;
471 }
472
473 inverts[N] = invert; /* #4 */
474 prog[N].target = N-1;
475
476 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
477 if (len < 0) {
478 ret = len;
479 goto out_free;
480 }
481 ptr = next + len;
482
483 N++;
484
485 ret = -1;
486 while (1) { /* #5 */
487 next = ptr++;
488 if (isspace(*next))
489 continue;
490
491 switch (*next) {
492 case ')':
493 case '\0':
494 break;
495 case '&':
496 case '|':
6c6dbce1 497 /* accepting only "&&" or "||" */
80765597
SRV
498 if (next[1] == next[0]) {
499 ptr++;
500 break;
501 }
9399ca21 502 /* fall through */
80765597
SRV
503 default:
504 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
505 next - str);
506 goto out_free;
507 }
508
509 invert = *top & INVERT;
510
511 if (*top & PROCESS_AND) { /* #7 */
512 update_preds(prog, N - 1, invert);
513 *top &= ~PROCESS_AND;
514 }
515 if (*next == '&') { /* #8 */
516 *top |= PROCESS_AND;
517 break;
518 }
519 if (*top & PROCESS_OR) { /* #9 */
520 update_preds(prog, N - 1, !invert);
521 *top &= ~PROCESS_OR;
522 }
523 if (*next == '|') { /* #10 */
524 *top |= PROCESS_OR;
525 break;
526 }
527 if (!*next) /* #11 */
528 goto out;
529
530 if (top == op_stack) {
531 ret = -1;
532 /* Too few '(' */
533 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
534 goto out_free;
535 }
536 top--; /* #12 */
537 }
538 }
539 out:
540 if (top != op_stack) {
541 /* Too many '(' */
542 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
543 goto out_free;
544 }
545
70303420
SRV
546 if (!N) {
547 /* No program? */
548 ret = -EINVAL;
549 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
550 goto out_free;
551 }
552
80765597
SRV
553 prog[N].pred = NULL; /* #13 */
554 prog[N].target = 1; /* TRUE */
555 prog[N+1].pred = NULL;
556 prog[N+1].target = 0; /* FALSE */
557 prog[N-1].target = N;
558 prog[N-1].when_to_branch = false;
559
560 /* Second Pass */
561 for (i = N-1 ; i--; ) {
562 int target = prog[i].target;
563 if (prog[i].when_to_branch == prog[target].when_to_branch)
564 prog[i].target = prog[target].target;
565 }
566
567 /* Third Pass */
568 for (i = 0; i < N; i++) {
569 invert = inverts[i] ^ prog[i].when_to_branch;
570 prog[i].when_to_branch = invert;
571 /* Make sure the program always moves forward */
572 if (WARN_ON(prog[i].target <= i)) {
573 ret = -EINVAL;
574 goto out_free;
575 }
576 }
577
b61c1920
SRV
578 kfree(op_stack);
579 kfree(inverts);
80765597
SRV
580 return prog;
581out_free:
582 kfree(op_stack);
80765597 583 kfree(inverts);
dfb4a6f2
TB
584 if (prog_stack) {
585 for (i = 0; prog_stack[i].pred; i++)
586 kfree(prog_stack[i].pred);
587 kfree(prog_stack);
588 }
80765597
SRV
589 return ERR_PTR(ret);
590}
591
197e2eab 592#define DEFINE_COMPARISON_PRED(type) \
fdf5b679 593static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
197e2eab
LZ
594{ \
595 type *addr = (type *)(event + pred->offset); \
596 type val = (type)pred->val; \
80765597 597 return *addr < val; \
fdf5b679
SRRH
598} \
599static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
600{ \
601 type *addr = (type *)(event + pred->offset); \
602 type val = (type)pred->val; \
80765597 603 return *addr <= val; \
fdf5b679
SRRH
604} \
605static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
606{ \
607 type *addr = (type *)(event + pred->offset); \
608 type val = (type)pred->val; \
80765597 609 return *addr > val; \
fdf5b679
SRRH
610} \
611static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
612{ \
613 type *addr = (type *)(event + pred->offset); \
614 type val = (type)pred->val; \
80765597 615 return *addr >= val; \
fdf5b679
SRRH
616} \
617static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
618{ \
619 type *addr = (type *)(event + pred->offset); \
620 type val = (type)pred->val; \
80765597 621 return !!(*addr & val); \
fdf5b679
SRRH
622} \
623static const filter_pred_fn_t pred_funcs_##type[] = { \
fdf5b679 624 filter_pred_LE_##type, \
80765597 625 filter_pred_LT_##type, \
fdf5b679 626 filter_pred_GE_##type, \
80765597 627 filter_pred_GT_##type, \
fdf5b679
SRRH
628 filter_pred_BAND_##type, \
629};
630
197e2eab 631#define DEFINE_EQUALITY_PRED(size) \
58d9a597 632static int filter_pred_##size(struct filter_pred *pred, void *event) \
197e2eab
LZ
633{ \
634 u##size *addr = (u##size *)(event + pred->offset); \
635 u##size val = (u##size)pred->val; \
636 int match; \
637 \
638 match = (val == *addr) ^ pred->not; \
639 \
640 return match; \
641}
642
8b372562
TZ
643DEFINE_COMPARISON_PRED(s64);
644DEFINE_COMPARISON_PRED(u64);
645DEFINE_COMPARISON_PRED(s32);
646DEFINE_COMPARISON_PRED(u32);
647DEFINE_COMPARISON_PRED(s16);
648DEFINE_COMPARISON_PRED(u16);
649DEFINE_COMPARISON_PRED(s8);
650DEFINE_COMPARISON_PRED(u8);
651
652DEFINE_EQUALITY_PRED(64);
653DEFINE_EQUALITY_PRED(32);
654DEFINE_EQUALITY_PRED(16);
655DEFINE_EQUALITY_PRED(8);
656
e8808c10 657/* Filter predicate for fixed sized arrays of characters */
58d9a597 658static int filter_pred_string(struct filter_pred *pred, void *event)
7ce7e424
TZ
659{
660 char *addr = (char *)(event + pred->offset);
661 int cmp, match;
662
1889d209 663 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
7ce7e424 664
1889d209 665 match = cmp ^ pred->not;
7ce7e424
TZ
666
667 return match;
668}
669
87a342f5 670/* Filter predicate for char * pointers */
58d9a597 671static int filter_pred_pchar(struct filter_pred *pred, void *event)
87a342f5
LZ
672{
673 char **addr = (char **)(event + pred->offset);
674 int cmp, match;
16da27a8 675 int len = strlen(*addr) + 1; /* including tailing '\0' */
87a342f5 676
16da27a8 677 cmp = pred->regex.match(*addr, &pred->regex, len);
87a342f5 678
1889d209 679 match = cmp ^ pred->not;
87a342f5
LZ
680
681 return match;
682}
683
e8808c10
FW
684/*
685 * Filter predicate for dynamic sized arrays of characters.
686 * These are implemented through a list of strings at the end
687 * of the entry.
688 * Also each of these strings have a field in the entry which
689 * contains its offset from the beginning of the entry.
690 * We have then first to get this field, dereference it
691 * and add it to the address of the entry, and at last we have
692 * the address of the string.
693 */
58d9a597 694static int filter_pred_strloc(struct filter_pred *pred, void *event)
e8808c10 695{
7d536cb3
LZ
696 u32 str_item = *(u32 *)(event + pred->offset);
697 int str_loc = str_item & 0xffff;
698 int str_len = str_item >> 16;
e8808c10
FW
699 char *addr = (char *)(event + str_loc);
700 int cmp, match;
701
1889d209 702 cmp = pred->regex.match(addr, &pred->regex, str_len);
e8808c10 703
1889d209 704 match = cmp ^ pred->not;
e8808c10
FW
705
706 return match;
707}
708
9f616680
DW
709/* Filter predicate for CPUs. */
710static int filter_pred_cpu(struct filter_pred *pred, void *event)
711{
712 int cpu, cmp;
9f616680
DW
713
714 cpu = raw_smp_processor_id();
715 cmp = pred->val;
716
717 switch (pred->op) {
718 case OP_EQ:
80765597
SRV
719 return cpu == cmp;
720 case OP_NE:
721 return cpu != cmp;
9f616680 722 case OP_LT:
80765597 723 return cpu < cmp;
9f616680 724 case OP_LE:
80765597 725 return cpu <= cmp;
9f616680 726 case OP_GT:
80765597 727 return cpu > cmp;
9f616680 728 case OP_GE:
80765597 729 return cpu >= cmp;
9f616680 730 default:
80765597 731 return 0;
9f616680 732 }
9f616680
DW
733}
734
735/* Filter predicate for COMM. */
736static int filter_pred_comm(struct filter_pred *pred, void *event)
737{
80765597 738 int cmp;
9f616680
DW
739
740 cmp = pred->regex.match(current->comm, &pred->regex,
80765597
SRV
741 TASK_COMM_LEN);
742 return cmp ^ pred->not;
9f616680
DW
743}
744
58d9a597 745static int filter_pred_none(struct filter_pred *pred, void *event)
0a19e53c
TZ
746{
747 return 0;
748}
749
d1303dd1
LZ
750/*
751 * regex_match_foo - Basic regex callbacks
752 *
753 * @str: the string to be searched
754 * @r: the regex structure containing the pattern string
755 * @len: the length of the string to be searched (including '\0')
756 *
757 * Note:
758 * - @str might not be NULL-terminated if it's of type DYN_STRING
10f20e9f 759 * or STATIC_STRING, unless @len is zero.
d1303dd1
LZ
760 */
761
1889d209
FW
762static int regex_match_full(char *str, struct regex *r, int len)
763{
10f20e9f
SRV
764 /* len of zero means str is dynamic and ends with '\0' */
765 if (!len)
766 return strcmp(str, r->pattern) == 0;
767
768 return strncmp(str, r->pattern, len) == 0;
1889d209
FW
769}
770
771static int regex_match_front(char *str, struct regex *r, int len)
772{
10f20e9f 773 if (len && len < r->len)
dc432c3d
SRV
774 return 0;
775
10f20e9f 776 return strncmp(str, r->pattern, r->len) == 0;
1889d209
FW
777}
778
779static int regex_match_middle(char *str, struct regex *r, int len)
780{
10f20e9f
SRV
781 if (!len)
782 return strstr(str, r->pattern) != NULL;
783
784 return strnstr(str, r->pattern, len) != NULL;
1889d209
FW
785}
786
787static int regex_match_end(char *str, struct regex *r, int len)
788{
a3291c14 789 int strlen = len - 1;
1889d209 790
a3291c14
LZ
791 if (strlen >= r->len &&
792 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1889d209
FW
793 return 1;
794 return 0;
795}
796
60f1d5e3
MH
797static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
798{
799 if (glob_match(r->pattern, str))
800 return 1;
801 return 0;
802}
80765597 803
3f6fe06d
FW
804/**
805 * filter_parse_regex - parse a basic regex
806 * @buff: the raw regex
807 * @len: length of the regex
808 * @search: will point to the beginning of the string to compare
809 * @not: tell whether the match will have to be inverted
810 *
811 * This passes in a buffer containing a regex and this function will
1889d209
FW
812 * set search to point to the search part of the buffer and
813 * return the type of search it is (see enum above).
814 * This does modify buff.
815 *
816 * Returns enum type.
817 * search returns the pointer to use for comparison.
818 * not returns 1 if buff started with a '!'
819 * 0 otherwise.
820 */
3f6fe06d 821enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1889d209
FW
822{
823 int type = MATCH_FULL;
824 int i;
825
826 if (buff[0] == '!') {
827 *not = 1;
828 buff++;
829 len--;
830 } else
831 *not = 0;
832
833 *search = buff;
834
f79b3f33
SRV
835 if (isdigit(buff[0]))
836 return MATCH_INDEX;
837
1889d209
FW
838 for (i = 0; i < len; i++) {
839 if (buff[i] == '*') {
840 if (!i) {
1889d209 841 type = MATCH_END_ONLY;
60f1d5e3 842 } else if (i == len - 1) {
1889d209
FW
843 if (type == MATCH_END_ONLY)
844 type = MATCH_MIDDLE_ONLY;
845 else
846 type = MATCH_FRONT_ONLY;
847 buff[i] = 0;
848 break;
60f1d5e3 849 } else { /* pattern continues, use full glob */
07234021 850 return MATCH_GLOB;
1889d209 851 }
60f1d5e3 852 } else if (strchr("[?\\", buff[i])) {
07234021 853 return MATCH_GLOB;
1889d209
FW
854 }
855 }
07234021
SRV
856 if (buff[0] == '*')
857 *search = buff + 1;
1889d209
FW
858
859 return type;
860}
861
b0f1a59a 862static void filter_build_regex(struct filter_pred *pred)
1889d209
FW
863{
864 struct regex *r = &pred->regex;
b0f1a59a
LZ
865 char *search;
866 enum regex_type type = MATCH_FULL;
b0f1a59a
LZ
867
868 if (pred->op == OP_GLOB) {
80765597 869 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
b0f1a59a
LZ
870 r->len = strlen(search);
871 memmove(r->pattern, search, r->len+1);
872 }
1889d209
FW
873
874 switch (type) {
f79b3f33
SRV
875 /* MATCH_INDEX should not happen, but if it does, match full */
876 case MATCH_INDEX:
1889d209
FW
877 case MATCH_FULL:
878 r->match = regex_match_full;
879 break;
880 case MATCH_FRONT_ONLY:
881 r->match = regex_match_front;
882 break;
883 case MATCH_MIDDLE_ONLY:
884 r->match = regex_match_middle;
885 break;
886 case MATCH_END_ONLY:
887 r->match = regex_match_end;
888 break;
60f1d5e3
MH
889 case MATCH_GLOB:
890 r->match = regex_match_glob;
891 break;
1889d209 892 }
f30120fc
JO
893}
894
7ce7e424 895/* return 1 if event matches, 0 otherwise (discard) */
6fb2915d 896int filter_match_preds(struct event_filter *filter, void *rec)
7ce7e424 897{
80765597
SRV
898 struct prog_entry *prog;
899 int i;
7ce7e424 900
6d54057d 901 /* no filter is considered a match */
75b8e982
SR
902 if (!filter)
903 return 1;
904
e0a568dc
SRV
905 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
906 prog = rcu_dereference_raw(filter->prog);
80765597 907 if (!prog)
6d54057d
SR
908 return 1;
909
80765597
SRV
910 for (i = 0; prog[i].pred; i++) {
911 struct filter_pred *pred = prog[i].pred;
912 int match = pred->fn(pred, rec);
913 if (match == prog[i].when_to_branch)
914 i = prog[i].target;
915 }
916 return prog[i].target;
7ce7e424 917}
17c873ec 918EXPORT_SYMBOL_GPL(filter_match_preds);
7ce7e424 919
8b372562
TZ
920static void remove_filter_string(struct event_filter *filter)
921{
75b8e982
SR
922 if (!filter)
923 return;
924
8b372562
TZ
925 kfree(filter->filter_string);
926 filter->filter_string = NULL;
927}
928
1e144d73
SRV
929static void append_filter_err(struct trace_array *tr,
930 struct filter_parse_error *pe,
8b372562
TZ
931 struct event_filter *filter)
932{
559d4212 933 struct trace_seq *s;
80765597 934 int pos = pe->lasterr_pos;
559d4212
SRV
935 char *buf;
936 int len;
8b372562 937
559d4212 938 if (WARN_ON(!filter->filter_string))
4bda2d51 939 return;
7ce7e424 940
559d4212
SRV
941 s = kmalloc(sizeof(*s), GFP_KERNEL);
942 if (!s)
943 return;
944 trace_seq_init(s);
945
946 len = strlen(filter->filter_string);
947 if (pos > len)
80765597
SRV
948 pos = len;
949
950 /* indexing is off by one */
951 if (pos)
952 pos++;
559d4212
SRV
953
954 trace_seq_puts(s, filter->filter_string);
80765597
SRV
955 if (pe->lasterr > 0) {
956 trace_seq_printf(s, "\n%*s", pos, "^");
957 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
2f754e77 958 tracing_log_err(tr, "event filter parse error",
34f76afa
TZ
959 filter->filter_string, err_text,
960 pe->lasterr, pe->lasterr_pos);
80765597
SRV
961 } else {
962 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
2f754e77 963 tracing_log_err(tr, "event filter parse error",
34f76afa
TZ
964 filter->filter_string, err_text,
965 FILT_ERR_ERRNO, 0);
80765597 966 }
559d4212
SRV
967 trace_seq_putc(s, 0);
968 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
969 if (buf) {
970 kfree(filter->filter_string);
971 filter->filter_string = buf;
972 }
973 kfree(s);
7ce7e424
TZ
974}
975
7f1d2f82 976static inline struct event_filter *event_filter(struct trace_event_file *file)
f306cc82 977{
dcb0b557 978 return file->filter;
f306cc82
TZ
979}
980
e2912b09 981/* caller must hold event_mutex */
7f1d2f82 982void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
ac1adc55 983{
f306cc82 984 struct event_filter *filter = event_filter(file);
8b372562 985
8e254c1d 986 if (filter && filter->filter_string)
8b372562
TZ
987 trace_seq_printf(s, "%s\n", filter->filter_string);
988 else
146c3442 989 trace_seq_puts(s, "none\n");
ac1adc55
TZ
990}
991
8b372562 992void print_subsystem_event_filter(struct event_subsystem *system,
ac1adc55
TZ
993 struct trace_seq *s)
994{
75b8e982 995 struct event_filter *filter;
8b372562 996
00e95830 997 mutex_lock(&event_mutex);
75b8e982 998 filter = system->filter;
8e254c1d 999 if (filter && filter->filter_string)
8b372562
TZ
1000 trace_seq_printf(s, "%s\n", filter->filter_string);
1001 else
146c3442 1002 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
00e95830 1003 mutex_unlock(&event_mutex);
ac1adc55
TZ
1004}
1005
80765597 1006static void free_prog(struct event_filter *filter)
c9c53ca0 1007{
80765597 1008 struct prog_entry *prog;
60705c89
SRRH
1009 int i;
1010
80765597
SRV
1011 prog = rcu_access_pointer(filter->prog);
1012 if (!prog)
1013 return;
1014
1015 for (i = 0; prog[i].pred; i++)
1016 kfree(prog[i].pred);
1017 kfree(prog);
c9c53ca0
SR
1018}
1019
7f1d2f82 1020static void filter_disable(struct trace_event_file *file)
f306cc82 1021{
0fc1b09f
SRRH
1022 unsigned long old_flags = file->flags;
1023
dcb0b557 1024 file->flags &= ~EVENT_FILE_FL_FILTERED;
0fc1b09f
SRRH
1025
1026 if (old_flags != file->flags)
1027 trace_buffered_event_disable();
f306cc82
TZ
1028}
1029
c9c53ca0 1030static void __free_filter(struct event_filter *filter)
2df75e41 1031{
8e254c1d
LZ
1032 if (!filter)
1033 return;
1034
80765597 1035 free_prog(filter);
57be8887 1036 kfree(filter->filter_string);
2df75e41 1037 kfree(filter);
6fb2915d
LZ
1038}
1039
bac5fb97
TZ
1040void free_event_filter(struct event_filter *filter)
1041{
1042 __free_filter(filter);
1043}
1044
7f1d2f82 1045static inline void __remove_filter(struct trace_event_file *file)
8e254c1d 1046{
f306cc82 1047 filter_disable(file);
dcb0b557 1048 remove_filter_string(file->filter);
f306cc82
TZ
1049}
1050
7967b3e0 1051static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
f306cc82
TZ
1052 struct trace_array *tr)
1053{
7f1d2f82 1054 struct trace_event_file *file;
8e254c1d 1055
f306cc82 1056 list_for_each_entry(file, &tr->events, list) {
bb9ef1cb 1057 if (file->system != dir)
8e254c1d 1058 continue;
f306cc82 1059 __remove_filter(file);
8e254c1d 1060 }
8e254c1d 1061}
7ce7e424 1062
7f1d2f82 1063static inline void __free_subsystem_filter(struct trace_event_file *file)
cfb180f3 1064{
dcb0b557
SRRH
1065 __free_filter(file->filter);
1066 file->filter = NULL;
f306cc82
TZ
1067}
1068
7967b3e0 1069static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
f306cc82
TZ
1070 struct trace_array *tr)
1071{
7f1d2f82 1072 struct trace_event_file *file;
cfb180f3 1073
f306cc82 1074 list_for_each_entry(file, &tr->events, list) {
80765597 1075 if (file->system != dir)
7ce7e424 1076 continue;
80765597 1077 __free_subsystem_filter(file);
8b372562 1078 }
80765597 1079}
8b372562 1080
80765597
SRV
1081int filter_assign_type(const char *type)
1082{
1083 if (strstr(type, "__data_loc") && strstr(type, "char"))
1084 return FILTER_DYN_STRING;
8b372562 1085
80765597
SRV
1086 if (strchr(type, '[') && strstr(type, "char"))
1087 return FILTER_STATIC_STRING;
8b372562 1088
5967bd5c
CW
1089 if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1090 return FILTER_PTR_STRING;
1091
80765597 1092 return FILTER_OTHER;
8b372562
TZ
1093}
1094
80765597
SRV
1095static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
1096 int field_size, int field_is_signed)
8b372562 1097{
80765597
SRV
1098 filter_pred_fn_t fn = NULL;
1099 int pred_func_index = -1;
81570d9c 1100
80765597
SRV
1101 switch (op) {
1102 case OP_EQ:
1103 case OP_NE:
1104 break;
1105 default:
1106 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1107 return NULL;
1108 pred_func_index = op - PRED_FUNC_START;
1109 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1110 return NULL;
8b372562
TZ
1111 }
1112
80765597
SRV
1113 switch (field_size) {
1114 case 8:
1115 if (pred_func_index < 0)
1116 fn = filter_pred_64;
1117 else if (field_is_signed)
1118 fn = pred_funcs_s64[pred_func_index];
1119 else
1120 fn = pred_funcs_u64[pred_func_index];
1121 break;
1122 case 4:
1123 if (pred_func_index < 0)
1124 fn = filter_pred_32;
1125 else if (field_is_signed)
1126 fn = pred_funcs_s32[pred_func_index];
1127 else
1128 fn = pred_funcs_u32[pred_func_index];
1129 break;
1130 case 2:
1131 if (pred_func_index < 0)
1132 fn = filter_pred_16;
1133 else if (field_is_signed)
1134 fn = pred_funcs_s16[pred_func_index];
1135 else
1136 fn = pred_funcs_u16[pred_func_index];
1137 break;
1138 case 1:
1139 if (pred_func_index < 0)
1140 fn = filter_pred_8;
1141 else if (field_is_signed)
1142 fn = pred_funcs_s8[pred_func_index];
1143 else
1144 fn = pred_funcs_u8[pred_func_index];
1145 break;
61aaef55 1146 }
8b372562 1147
80765597 1148 return fn;
8b372562
TZ
1149}
1150
80765597
SRV
1151/* Called when a predicate is encountered by predicate_parse() */
1152static int parse_pred(const char *str, void *data,
1153 int pos, struct filter_parse_error *pe,
1154 struct filter_pred **pred_ptr)
8b372562 1155{
80765597
SRV
1156 struct trace_event_call *call = data;
1157 struct ftrace_event_field *field;
1158 struct filter_pred *pred = NULL;
1159 char num_buf[24]; /* Big enough to hold an address */
1160 char *field_name;
1161 char q;
1162 u64 val;
1163 int len;
1164 int ret;
1165 int op;
1166 int s;
1167 int i = 0;
8b372562 1168
80765597
SRV
1169 /* First find the field to associate to */
1170 while (isspace(str[i]))
1171 i++;
1172 s = i;
8b372562 1173
80765597
SRV
1174 while (isalnum(str[i]) || str[i] == '_')
1175 i++;
1176
1177 len = i - s;
1178
1179 if (!len)
1180 return -1;
1181
1182 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1183 if (!field_name)
1184 return -ENOMEM;
1185
1186 /* Make sure that the field exists */
7ce7e424 1187
80765597
SRV
1188 field = trace_find_event_field(call, field_name);
1189 kfree(field_name);
1190 if (!field) {
1191 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
bcabd91c
LZ
1192 return -EINVAL;
1193 }
1194
80765597
SRV
1195 while (isspace(str[i]))
1196 i++;
f66578a7 1197
80765597
SRV
1198 /* Make sure this op is supported */
1199 for (op = 0; ops[op]; op++) {
1200 /* This is why '<=' must come before '<' in ops[] */
1201 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1202 break;
1203 }
c9c53ca0 1204
80765597
SRV
1205 if (!ops[op]) {
1206 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1207 goto err_free;
c9c53ca0
SR
1208 }
1209
80765597 1210 i += strlen(ops[op]);
c9c53ca0 1211
80765597
SRV
1212 while (isspace(str[i]))
1213 i++;
f03f5979 1214
80765597 1215 s = i;
f03f5979 1216
80765597
SRV
1217 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1218 if (!pred)
1219 return -ENOMEM;
f03f5979 1220
80765597
SRV
1221 pred->field = field;
1222 pred->offset = field->offset;
1223 pred->op = op;
1224
1225 if (ftrace_event_is_function(call)) {
f03f5979 1226 /*
80765597
SRV
1227 * Perf does things different with function events.
1228 * It only allows an "ip" field, and expects a string.
1229 * But the string does not need to be surrounded by quotes.
1230 * If it is a string, the assigned function as a nop,
1231 * (perf doesn't use it) and grab everything.
f03f5979 1232 */
80765597 1233 if (strcmp(field->name, "ip") != 0) {
bfcd631e
CIK
1234 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1235 goto err_free;
1236 }
1237 pred->fn = filter_pred_none;
1238
1239 /*
1240 * Quotes are not required, but if they exist then we need
1241 * to read them till we hit a matching one.
1242 */
1243 if (str[i] == '\'' || str[i] == '"')
1244 q = str[i];
1245 else
1246 q = 0;
1247
1248 for (i++; str[i]; i++) {
1249 if (q && str[i] == q)
1250 break;
1251 if (!q && (str[i] == ')' || str[i] == '&' ||
1252 str[i] == '|'))
1253 break;
1254 }
1255 /* Skip quotes */
1256 if (q)
1257 s++;
80765597
SRV
1258 len = i - s;
1259 if (len >= MAX_FILTER_STR_VAL) {
1260 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1261 goto err_free;
1262 }
ec126cac 1263
80765597
SRV
1264 pred->regex.len = len;
1265 strncpy(pred->regex.pattern, str + s, len);
1266 pred->regex.pattern[len] = 0;
1267
1268 /* This is either a string, or an integer */
1269 } else if (str[i] == '\'' || str[i] == '"') {
1270 char q = str[i];
1271
1272 /* Make sure the op is OK for strings */
1273 switch (op) {
1274 case OP_NE:
1275 pred->not = 1;
1276 /* Fall through */
1277 case OP_GLOB:
1278 case OP_EQ:
1279 break;
1280 default:
1281 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1282 goto err_free;
1283 }
ec126cac 1284
80765597
SRV
1285 /* Make sure the field is OK for strings */
1286 if (!is_string_field(field)) {
1287 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1288 goto err_free;
1289 }
43cd4145 1290
80765597
SRV
1291 for (i++; str[i]; i++) {
1292 if (str[i] == q)
1293 break;
1294 }
1295 if (!str[i]) {
1296 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1297 goto err_free;
1298 }
43cd4145 1299
80765597
SRV
1300 /* Skip quotes */
1301 s++;
1302 len = i - s;
1303 if (len >= MAX_FILTER_STR_VAL) {
1304 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1305 goto err_free;
1306 }
c00b060f 1307
80765597
SRV
1308 pred->regex.len = len;
1309 strncpy(pred->regex.pattern, str + s, len);
1310 pred->regex.pattern[len] = 0;
43cd4145 1311
80765597 1312 filter_build_regex(pred);
43cd4145 1313
80765597
SRV
1314 if (field->filter_type == FILTER_COMM) {
1315 pred->fn = filter_pred_comm;
96bc293a 1316
80765597
SRV
1317 } else if (field->filter_type == FILTER_STATIC_STRING) {
1318 pred->fn = filter_pred_string;
1319 pred->regex.field_len = field->size;
96bc293a 1320
80765597
SRV
1321 } else if (field->filter_type == FILTER_DYN_STRING)
1322 pred->fn = filter_pred_strloc;
1323 else
1324 pred->fn = filter_pred_pchar;
1325 /* go past the last quote */
1326 i++;
96bc293a 1327
6a072128 1328 } else if (isdigit(str[i]) || str[i] == '-') {
96bc293a 1329
80765597
SRV
1330 /* Make sure the field is not a string */
1331 if (is_string_field(field)) {
1332 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1333 goto err_free;
1334 }
96bc293a 1335
80765597
SRV
1336 if (op == OP_GLOB) {
1337 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1338 goto err_free;
1339 }
43cd4145 1340
6a072128
PT
1341 if (str[i] == '-')
1342 i++;
1343
80765597
SRV
1344 /* We allow 0xDEADBEEF */
1345 while (isalnum(str[i]))
1346 i++;
43cd4145 1347
80765597
SRV
1348 len = i - s;
1349 /* 0xfeedfacedeadbeef is 18 chars max */
1350 if (len >= sizeof(num_buf)) {
1351 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1352 goto err_free;
1353 }
43cd4145 1354
80765597
SRV
1355 strncpy(num_buf, str + s, len);
1356 num_buf[len] = 0;
43cd4145 1357
80765597
SRV
1358 /* Make sure it is a value */
1359 if (field->is_signed)
1360 ret = kstrtoll(num_buf, 0, &val);
1361 else
1362 ret = kstrtoull(num_buf, 0, &val);
1363 if (ret) {
1364 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1365 goto err_free;
1366 }
43cd4145 1367
80765597 1368 pred->val = val;
43cd4145 1369
80765597
SRV
1370 if (field->filter_type == FILTER_CPU)
1371 pred->fn = filter_pred_cpu;
1372 else {
1373 pred->fn = select_comparison_fn(pred->op, field->size,
1374 field->is_signed);
1375 if (pred->op == OP_NE)
1376 pred->not = 1;
1377 }
1b797fe5 1378
80765597
SRV
1379 } else {
1380 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1381 goto err_free;
1382 }
1b797fe5 1383
80765597
SRV
1384 *pred_ptr = pred;
1385 return i;
1b797fe5 1386
80765597
SRV
1387err_free:
1388 kfree(pred);
1389 return -EINVAL;
1b797fe5
JO
1390}
1391
80765597
SRV
1392enum {
1393 TOO_MANY_CLOSE = -1,
1394 TOO_MANY_OPEN = -2,
1395 MISSING_QUOTE = -3,
1396};
1397
43cd4145 1398/*
80765597
SRV
1399 * Read the filter string once to calculate the number of predicates
1400 * as well as how deep the parentheses go.
1401 *
1402 * Returns:
1403 * 0 - everything is fine (err is undefined)
1404 * -1 - too many ')'
1405 * -2 - too many '('
1406 * -3 - No matching quote
43cd4145 1407 */
80765597
SRV
1408static int calc_stack(const char *str, int *parens, int *preds, int *err)
1409{
1410 bool is_pred = false;
1411 int nr_preds = 0;
1412 int open = 1; /* Count the expression as "(E)" */
1413 int last_quote = 0;
1414 int max_open = 1;
1415 int quote = 0;
1416 int i;
8b372562 1417
80765597 1418 *err = 0;
c9c53ca0 1419
80765597
SRV
1420 for (i = 0; str[i]; i++) {
1421 if (isspace(str[i]))
1422 continue;
1423 if (quote) {
1424 if (str[i] == quote)
1425 quote = 0;
8b372562
TZ
1426 continue;
1427 }
1428
80765597
SRV
1429 switch (str[i]) {
1430 case '\'':
1431 case '"':
1432 quote = str[i];
1433 last_quote = i;
1434 break;
1435 case '|':
1436 case '&':
1437 if (str[i+1] != str[i])
1438 break;
1439 is_pred = false;
1440 continue;
1441 case '(':
1442 is_pred = false;
1443 open++;
1444 if (open > max_open)
1445 max_open = open;
1446 continue;
1447 case ')':
1448 is_pred = false;
1449 if (open == 1) {
1450 *err = i;
1451 return TOO_MANY_CLOSE;
e12c09cf 1452 }
80765597 1453 open--;
e12c09cf
SRRH
1454 continue;
1455 }
80765597
SRV
1456 if (!is_pred) {
1457 nr_preds++;
1458 is_pred = true;
1f9963cb 1459 }
80765597 1460 }
1f9963cb 1461
80765597
SRV
1462 if (quote) {
1463 *err = last_quote;
1464 return MISSING_QUOTE;
1465 }
61aaef55 1466
80765597
SRV
1467 if (open != 1) {
1468 int level = open;
8b372562 1469
80765597
SRV
1470 /* find the bad open */
1471 for (i--; i; i--) {
1472 if (quote) {
1473 if (str[i] == quote)
1474 quote = 0;
1475 continue;
1476 }
1477 switch (str[i]) {
1478 case '(':
1479 if (level == open) {
1480 *err = i;
1481 return TOO_MANY_OPEN;
1482 }
1483 level--;
1484 break;
1485 case ')':
1486 level++;
1487 break;
1488 case '\'':
1489 case '"':
1490 quote = str[i];
1491 break;
1492 }
1493 }
1494 /* First character is the '(' with missing ')' */
1495 *err = 0;
1496 return TOO_MANY_OPEN;
8b372562 1497 }
7ce7e424 1498
80765597
SRV
1499 /* Set the size of the required stacks */
1500 *parens = max_open;
1501 *preds = nr_preds;
1502 return 0;
1503}
1504
1505static int process_preds(struct trace_event_call *call,
1506 const char *filter_string,
1507 struct event_filter *filter,
1508 struct filter_parse_error *pe)
1509{
1510 struct prog_entry *prog;
1511 int nr_parens;
1512 int nr_preds;
1513 int index;
1514 int ret;
1515
1516 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
1517 if (ret < 0) {
1518 switch (ret) {
1519 case MISSING_QUOTE:
1520 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
1521 break;
1522 case TOO_MANY_OPEN:
1523 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
1524 break;
1525 default:
1526 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
61e9dea2 1527 }
80765597 1528 return ret;
61e9dea2
SR
1529 }
1530
ba16293d
RB
1531 if (!nr_preds)
1532 return -EINVAL;
1533
1534 prog = predicate_parse(filter_string, nr_parens, nr_preds,
80765597 1535 parse_pred, call, pe);
ba16293d
RB
1536 if (IS_ERR(prog))
1537 return PTR_ERR(prog);
1538
80765597
SRV
1539 rcu_assign_pointer(filter->prog, prog);
1540 return 0;
7ce7e424
TZ
1541}
1542
7f1d2f82 1543static inline void event_set_filtered_flag(struct trace_event_file *file)
f306cc82 1544{
0fc1b09f
SRRH
1545 unsigned long old_flags = file->flags;
1546
dcb0b557 1547 file->flags |= EVENT_FILE_FL_FILTERED;
0fc1b09f
SRRH
1548
1549 if (old_flags != file->flags)
1550 trace_buffered_event_enable();
f306cc82
TZ
1551}
1552
7f1d2f82 1553static inline void event_set_filter(struct trace_event_file *file,
f306cc82
TZ
1554 struct event_filter *filter)
1555{
dcb0b557 1556 rcu_assign_pointer(file->filter, filter);
f306cc82
TZ
1557}
1558
7f1d2f82 1559static inline void event_clear_filter(struct trace_event_file *file)
f306cc82 1560{
dcb0b557 1561 RCU_INIT_POINTER(file->filter, NULL);
f306cc82
TZ
1562}
1563
1564static inline void
7f1d2f82 1565event_set_no_set_filter_flag(struct trace_event_file *file)
f306cc82 1566{
dcb0b557 1567 file->flags |= EVENT_FILE_FL_NO_SET_FILTER;
f306cc82
TZ
1568}
1569
1570static inline void
7f1d2f82 1571event_clear_no_set_filter_flag(struct trace_event_file *file)
f306cc82 1572{
dcb0b557 1573 file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER;
f306cc82
TZ
1574}
1575
1576static inline bool
7f1d2f82 1577event_no_set_filter_flag(struct trace_event_file *file)
f306cc82 1578{
5d6ad960 1579 if (file->flags & EVENT_FILE_FL_NO_SET_FILTER)
f306cc82
TZ
1580 return true;
1581
f306cc82
TZ
1582 return false;
1583}
1584
75b8e982
SR
1585struct filter_list {
1586 struct list_head list;
1587 struct event_filter *filter;
1588};
1589
80765597 1590static int process_system_preds(struct trace_subsystem_dir *dir,
f306cc82 1591 struct trace_array *tr,
80765597 1592 struct filter_parse_error *pe,
fce29d15
LZ
1593 char *filter_string)
1594{
7f1d2f82 1595 struct trace_event_file *file;
75b8e982 1596 struct filter_list *filter_item;
404a3add 1597 struct event_filter *filter = NULL;
75b8e982
SR
1598 struct filter_list *tmp;
1599 LIST_HEAD(filter_list);
fce29d15 1600 bool fail = true;
a66abe7f 1601 int err;
fce29d15 1602
f306cc82 1603 list_for_each_entry(file, &tr->events, list) {
0fc3ca9a 1604
bb9ef1cb 1605 if (file->system != dir)
0fc3ca9a
SR
1606 continue;
1607
404a3add
SRV
1608 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1609 if (!filter)
75b8e982 1610 goto fail_mem;
0fc3ca9a 1611
567f6989
SRV
1612 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1613 if (!filter->filter_string)
75b8e982 1614 goto fail_mem;
fce29d15 1615
80765597 1616 err = process_preds(file->event_call, filter_string, filter, pe);
75b8e982 1617 if (err) {
f306cc82 1618 filter_disable(file);
80765597 1619 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1e144d73 1620 append_filter_err(tr, pe, filter);
75b8e982 1621 } else
f306cc82 1622 event_set_filtered_flag(file);
404a3add
SRV
1623
1624
1625 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1626 if (!filter_item)
1627 goto fail_mem;
1628
1629 list_add_tail(&filter_item->list, &filter_list);
75b8e982
SR
1630 /*
1631 * Regardless of if this returned an error, we still
1632 * replace the filter for the call.
1633 */
404a3add
SRV
1634 filter_item->filter = event_filter(file);
1635 event_set_filter(file, filter);
1636 filter = NULL;
75b8e982 1637
fce29d15
LZ
1638 fail = false;
1639 }
1640
0fc3ca9a
SR
1641 if (fail)
1642 goto fail;
1643
75b8e982
SR
1644 /*
1645 * The calls can still be using the old filters.
74401729 1646 * Do a synchronize_rcu() and to ensure all calls are
75b8e982
SR
1647 * done with them before we free them.
1648 */
e0a568dc 1649 tracepoint_synchronize_unregister();
75b8e982
SR
1650 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1651 __free_filter(filter_item->filter);
1652 list_del(&filter_item->list);
1653 kfree(filter_item);
1654 }
fce29d15 1655 return 0;
0fc3ca9a 1656 fail:
75b8e982
SR
1657 /* No call succeeded */
1658 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1659 list_del(&filter_item->list);
1660 kfree(filter_item);
1661 }
80765597 1662 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
0fc3ca9a 1663 return -EINVAL;
75b8e982 1664 fail_mem:
404a3add 1665 kfree(filter);
75b8e982
SR
1666 /* If any call succeeded, we still need to sync */
1667 if (!fail)
e0a568dc 1668 tracepoint_synchronize_unregister();
75b8e982
SR
1669 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1670 __free_filter(filter_item->filter);
1671 list_del(&filter_item->list);
1672 kfree(filter_item);
1673 }
1674 return -ENOMEM;
fce29d15
LZ
1675}
1676
567f6989 1677static int create_filter_start(char *filter_string, bool set_str,
80765597 1678 struct filter_parse_error **pse,
38b78eb8
TH
1679 struct event_filter **filterp)
1680{
1681 struct event_filter *filter;
80765597 1682 struct filter_parse_error *pe = NULL;
38b78eb8
TH
1683 int err = 0;
1684
80765597
SRV
1685 if (WARN_ON_ONCE(*pse || *filterp))
1686 return -EINVAL;
38b78eb8 1687
c7399708 1688 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
567f6989
SRV
1689 if (filter && set_str) {
1690 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1691 if (!filter->filter_string)
1692 err = -ENOMEM;
1693 }
38b78eb8 1694
80765597 1695 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
38b78eb8 1696
80765597
SRV
1697 if (!filter || !pe || err) {
1698 kfree(pe);
38b78eb8
TH
1699 __free_filter(filter);
1700 return -ENOMEM;
1701 }
1702
1703 /* we're committed to creating a new filter */
1704 *filterp = filter;
80765597 1705 *pse = pe;
38b78eb8 1706
80765597 1707 return 0;
38b78eb8
TH
1708}
1709
80765597 1710static void create_filter_finish(struct filter_parse_error *pe)
38b78eb8 1711{
80765597 1712 kfree(pe);
38b78eb8
TH
1713}
1714
1715/**
2425bcb9
SRRH
1716 * create_filter - create a filter for a trace_event_call
1717 * @call: trace_event_call to create a filter for
38b78eb8
TH
1718 * @filter_str: filter string
1719 * @set_str: remember @filter_str and enable detailed error in filter
1720 * @filterp: out param for created filter (always updated on return)
f9065872 1721 * Must be a pointer that references a NULL pointer.
38b78eb8
TH
1722 *
1723 * Creates a filter for @call with @filter_str. If @set_str is %true,
1724 * @filter_str is copied and recorded in the new filter.
1725 *
1726 * On success, returns 0 and *@filterp points to the new filter. On
1727 * failure, returns -errno and *@filterp may point to %NULL or to a new
1728 * filter. In the latter case, the returned filter contains error
1729 * information if @set_str is %true and the caller is responsible for
1730 * freeing it.
1731 */
1e144d73
SRV
1732static int create_filter(struct trace_array *tr,
1733 struct trace_event_call *call,
80765597 1734 char *filter_string, bool set_str,
38b78eb8
TH
1735 struct event_filter **filterp)
1736{
80765597 1737 struct filter_parse_error *pe = NULL;
38b78eb8
TH
1738 int err;
1739
f9065872
SRV
1740 /* filterp must point to NULL */
1741 if (WARN_ON(*filterp))
1742 *filterp = NULL;
1743
0b3dec05 1744 err = create_filter_start(filter_string, set_str, &pe, filterp);
80765597
SRV
1745 if (err)
1746 return err;
1747
0b3dec05 1748 err = process_preds(call, filter_string, *filterp, pe);
80765597 1749 if (err && set_str)
1e144d73 1750 append_filter_err(tr, pe, *filterp);
b61c1920 1751 create_filter_finish(pe);
38b78eb8 1752
38b78eb8
TH
1753 return err;
1754}
1755
1e144d73
SRV
1756int create_event_filter(struct trace_array *tr,
1757 struct trace_event_call *call,
bac5fb97
TZ
1758 char *filter_str, bool set_str,
1759 struct event_filter **filterp)
1760{
1e144d73 1761 return create_filter(tr, call, filter_str, set_str, filterp);
bac5fb97
TZ
1762}
1763
38b78eb8
TH
1764/**
1765 * create_system_filter - create a filter for an event_subsystem
1766 * @system: event_subsystem to create a filter for
1767 * @filter_str: filter string
1768 * @filterp: out param for created filter (always updated on return)
1769 *
1770 * Identical to create_filter() except that it creates a subsystem filter
1771 * and always remembers @filter_str.
1772 */
7967b3e0 1773static int create_system_filter(struct trace_subsystem_dir *dir,
f306cc82 1774 struct trace_array *tr,
38b78eb8
TH
1775 char *filter_str, struct event_filter **filterp)
1776{
80765597 1777 struct filter_parse_error *pe = NULL;
38b78eb8
TH
1778 int err;
1779
0b3dec05 1780 err = create_filter_start(filter_str, true, &pe, filterp);
38b78eb8 1781 if (!err) {
80765597 1782 err = process_system_preds(dir, tr, pe, filter_str);
38b78eb8
TH
1783 if (!err) {
1784 /* System filters just show a default message */
0b3dec05
SRV
1785 kfree((*filterp)->filter_string);
1786 (*filterp)->filter_string = NULL;
38b78eb8 1787 } else {
1e144d73 1788 append_filter_err(tr, pe, *filterp);
38b78eb8
TH
1789 }
1790 }
80765597 1791 create_filter_finish(pe);
38b78eb8 1792
38b78eb8
TH
1793 return err;
1794}
1795
e2912b09 1796/* caller must hold event_mutex */
7f1d2f82 1797int apply_event_filter(struct trace_event_file *file, char *filter_string)
8b372562 1798{
2425bcb9 1799 struct trace_event_call *call = file->event_call;
0b3dec05 1800 struct event_filter *filter = NULL;
e2912b09 1801 int err;
8b372562
TZ
1802
1803 if (!strcmp(strstrip(filter_string), "0")) {
f306cc82
TZ
1804 filter_disable(file);
1805 filter = event_filter(file);
1806
75b8e982 1807 if (!filter)
e2912b09 1808 return 0;
f306cc82
TZ
1809
1810 event_clear_filter(file);
1811
f76690af 1812 /* Make sure the filter is not being used */
e0a568dc 1813 tracepoint_synchronize_unregister();
75b8e982 1814 __free_filter(filter);
f306cc82 1815
e2912b09 1816 return 0;
8b372562
TZ
1817 }
1818
1e144d73 1819 err = create_filter(file->tr, call, filter_string, true, &filter);
8b372562 1820
75b8e982
SR
1821 /*
1822 * Always swap the call filter with the new filter
1823 * even if there was an error. If there was an error
1824 * in the filter, we disable the filter and show the error
1825 * string
1826 */
38b78eb8 1827 if (filter) {
f306cc82 1828 struct event_filter *tmp;
38b78eb8 1829
f306cc82 1830 tmp = event_filter(file);
38b78eb8 1831 if (!err)
f306cc82 1832 event_set_filtered_flag(file);
38b78eb8 1833 else
f306cc82 1834 filter_disable(file);
38b78eb8 1835
f306cc82 1836 event_set_filter(file, filter);
38b78eb8
TH
1837
1838 if (tmp) {
1839 /* Make sure the call is done with the filter */
e0a568dc 1840 tracepoint_synchronize_unregister();
38b78eb8
TH
1841 __free_filter(tmp);
1842 }
75b8e982 1843 }
8b372562
TZ
1844
1845 return err;
1846}
1847
7967b3e0 1848int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
8b372562
TZ
1849 char *filter_string)
1850{
ae63b31e 1851 struct event_subsystem *system = dir->subsystem;
f306cc82 1852 struct trace_array *tr = dir->tr;
0b3dec05 1853 struct event_filter *filter = NULL;
75b8e982 1854 int err = 0;
8b372562 1855
00e95830 1856 mutex_lock(&event_mutex);
8b372562 1857
e9dbfae5 1858 /* Make sure the system still has events */
ae63b31e 1859 if (!dir->nr_events) {
e9dbfae5
SR
1860 err = -ENODEV;
1861 goto out_unlock;
1862 }
1863
8b372562 1864 if (!strcmp(strstrip(filter_string), "0")) {
bb9ef1cb 1865 filter_free_subsystem_preds(dir, tr);
8b372562 1866 remove_filter_string(system->filter);
75b8e982
SR
1867 filter = system->filter;
1868 system->filter = NULL;
1869 /* Ensure all filters are no longer used */
e0a568dc 1870 tracepoint_synchronize_unregister();
bb9ef1cb 1871 filter_free_subsystem_filters(dir, tr);
75b8e982 1872 __free_filter(filter);
a66abe7f 1873 goto out_unlock;
8b372562
TZ
1874 }
1875
bb9ef1cb 1876 err = create_system_filter(dir, tr, filter_string, &filter);
38b78eb8
TH
1877 if (filter) {
1878 /*
1879 * No event actually uses the system filter
74401729 1880 * we can free it without synchronize_rcu().
38b78eb8
TH
1881 */
1882 __free_filter(system->filter);
1883 system->filter = filter;
1884 }
8cd995b6 1885out_unlock:
00e95830 1886 mutex_unlock(&event_mutex);
8b372562
TZ
1887
1888 return err;
1889}
7ce7e424 1890
07b139c8 1891#ifdef CONFIG_PERF_EVENTS
6fb2915d
LZ
1892
1893void ftrace_profile_free_filter(struct perf_event *event)
1894{
1895 struct event_filter *filter = event->filter;
1896
1897 event->filter = NULL;
c9c53ca0 1898 __free_filter(filter);
6fb2915d
LZ
1899}
1900
5500fa51
JO
1901struct function_filter_data {
1902 struct ftrace_ops *ops;
1903 int first_filter;
1904 int first_notrace;
1905};
1906
1907#ifdef CONFIG_FUNCTION_TRACER
1908static char **
1909ftrace_function_filter_re(char *buf, int len, int *count)
1910{
1bb56471 1911 char *str, **re;
5500fa51
JO
1912
1913 str = kstrndup(buf, len, GFP_KERNEL);
1914 if (!str)
1915 return NULL;
1916
1917 /*
1918 * The argv_split function takes white space
1919 * as a separator, so convert ',' into spaces.
1920 */
1bb56471 1921 strreplace(str, ',', ' ');
5500fa51
JO
1922
1923 re = argv_split(GFP_KERNEL, str, count);
1924 kfree(str);
1925 return re;
1926}
1927
1928static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
1929 int reset, char *re, int len)
1930{
1931 int ret;
1932
1933 if (filter)
1934 ret = ftrace_set_filter(ops, re, len, reset);
1935 else
1936 ret = ftrace_set_notrace(ops, re, len, reset);
1937
1938 return ret;
1939}
1940
1941static int __ftrace_function_set_filter(int filter, char *buf, int len,
1942 struct function_filter_data *data)
1943{
92d8d4a8 1944 int i, re_cnt, ret = -EINVAL;
5500fa51
JO
1945 int *reset;
1946 char **re;
1947
1948 reset = filter ? &data->first_filter : &data->first_notrace;
1949
1950 /*
1951 * The 'ip' field could have multiple filters set, separated
1952 * either by space or comma. We first cut the filter and apply
1953 * all pieces separatelly.
1954 */
1955 re = ftrace_function_filter_re(buf, len, &re_cnt);
1956 if (!re)
1957 return -EINVAL;
1958
1959 for (i = 0; i < re_cnt; i++) {
1960 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
1961 re[i], strlen(re[i]));
1962 if (ret)
1963 break;
1964
1965 if (*reset)
1966 *reset = 0;
1967 }
1968
1969 argv_free(re);
1970 return ret;
1971}
1972
80765597 1973static int ftrace_function_check_pred(struct filter_pred *pred)
5500fa51
JO
1974{
1975 struct ftrace_event_field *field = pred->field;
1976
80765597
SRV
1977 /*
1978 * Check the predicate for function trace, verify:
1979 * - only '==' and '!=' is used
1980 * - the 'ip' field is used
1981 */
1982 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
1983 return -EINVAL;
5500fa51 1984
80765597
SRV
1985 if (strcmp(field->name, "ip"))
1986 return -EINVAL;
5500fa51
JO
1987
1988 return 0;
1989}
1990
80765597
SRV
1991static int ftrace_function_set_filter_pred(struct filter_pred *pred,
1992 struct function_filter_data *data)
5500fa51 1993{
80765597
SRV
1994 int ret;
1995
5500fa51 1996 /* Checking the node is valid for function trace. */
80765597
SRV
1997 ret = ftrace_function_check_pred(pred);
1998 if (ret)
1999 return ret;
2000
2001 return __ftrace_function_set_filter(pred->op == OP_EQ,
2002 pred->regex.pattern,
2003 pred->regex.len,
2004 data);
2005}
2006
2007static bool is_or(struct prog_entry *prog, int i)
2008{
2009 int target;
5500fa51 2010
80765597
SRV
2011 /*
2012 * Only "||" is allowed for function events, thus,
2013 * all true branches should jump to true, and any
2014 * false branch should jump to false.
2015 */
2016 target = prog[i].target + 1;
2017 /* True and false have NULL preds (all prog entries should jump to one */
2018 if (prog[target].pred)
2019 return false;
2020
2021 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2022 return prog[i].when_to_branch == prog[target].target;
5500fa51
JO
2023}
2024
2025static int ftrace_function_set_filter(struct perf_event *event,
2026 struct event_filter *filter)
2027{
1f3b0faa
SRV
2028 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2029 lockdep_is_held(&event_mutex));
5500fa51
JO
2030 struct function_filter_data data = {
2031 .first_filter = 1,
2032 .first_notrace = 1,
2033 .ops = &event->ftrace_ops,
2034 };
80765597
SRV
2035 int i;
2036
2037 for (i = 0; prog[i].pred; i++) {
2038 struct filter_pred *pred = prog[i].pred;
2039
2040 if (!is_or(prog, i))
2041 return -EINVAL;
5500fa51 2042
80765597
SRV
2043 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2044 return -EINVAL;
2045 }
2046 return 0;
5500fa51
JO
2047}
2048#else
2049static int ftrace_function_set_filter(struct perf_event *event,
2050 struct event_filter *filter)
2051{
2052 return -ENODEV;
2053}
2054#endif /* CONFIG_FUNCTION_TRACER */
2055
6fb2915d
LZ
2056int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2057 char *filter_str)
2058{
2059 int err;
0b3dec05 2060 struct event_filter *filter = NULL;
2425bcb9 2061 struct trace_event_call *call;
6fb2915d
LZ
2062
2063 mutex_lock(&event_mutex);
2064
3f78f935 2065 call = event->tp_event;
a66abe7f
IM
2066
2067 err = -EINVAL;
3f78f935 2068 if (!call)
a66abe7f 2069 goto out_unlock;
6fb2915d 2070
a66abe7f 2071 err = -EEXIST;
6fb2915d 2072 if (event->filter)
a66abe7f 2073 goto out_unlock;
6fb2915d 2074
1e144d73 2075 err = create_filter(NULL, call, filter_str, false, &filter);
5500fa51
JO
2076 if (err)
2077 goto free_filter;
2078
2079 if (ftrace_event_is_function(call))
2080 err = ftrace_function_set_filter(event, filter);
38b78eb8 2081 else
5500fa51
JO
2082 event->filter = filter;
2083
2084free_filter:
2085 if (err || ftrace_event_is_function(call))
c9c53ca0 2086 __free_filter(filter);
6fb2915d 2087
a66abe7f 2088out_unlock:
6fb2915d
LZ
2089 mutex_unlock(&event_mutex);
2090
2091 return err;
2092}
2093
07b139c8 2094#endif /* CONFIG_PERF_EVENTS */
6fb2915d 2095
1d0e78e3
JO
2096#ifdef CONFIG_FTRACE_STARTUP_TEST
2097
2098#include <linux/types.h>
2099#include <linux/tracepoint.h>
2100
2101#define CREATE_TRACE_POINTS
2102#include "trace_events_filter_test.h"
2103
1d0e78e3
JO
2104#define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2105{ \
2106 .filter = FILTER, \
2107 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2108 .e = ve, .f = vf, .g = vg, .h = vh }, \
2109 .match = m, \
2110 .not_visited = nvisit, \
2111}
2112#define YES 1
2113#define NO 0
2114
2115static struct test_filter_data_t {
2116 char *filter;
a7237765 2117 struct trace_event_raw_ftrace_test_filter rec;
1d0e78e3
JO
2118 int match;
2119 char *not_visited;
2120} test_filter_data[] = {
2121#define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2122 "e == 1 && f == 1 && g == 1 && h == 1"
2123 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2124 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2125 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2126#undef FILTER
2127#define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2128 "e == 1 || f == 1 || g == 1 || h == 1"
2129 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2130 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2131 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2132#undef FILTER
2133#define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2134 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2135 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2136 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2137 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2138 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2139#undef FILTER
2140#define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2141 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2142 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2143 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2144 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2145#undef FILTER
2146#define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2147 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2148 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2149 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2150 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2151#undef FILTER
2152#define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2153 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2154 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2155 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2156 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2157#undef FILTER
2158#define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2159 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2160 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2161 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2162 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2163#undef FILTER
2164#define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2165 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2166 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2167 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2168 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2169};
2170
2171#undef DATA_REC
2172#undef FILTER
2173#undef YES
2174#undef NO
2175
0a4d0564 2176#define DATA_CNT ARRAY_SIZE(test_filter_data)
1d0e78e3
JO
2177
2178static int test_pred_visited;
2179
2180static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2181{
2182 struct ftrace_event_field *field = pred->field;
2183
2184 test_pred_visited = 1;
2185 printk(KERN_INFO "\npred visited %s\n", field->name);
2186 return 1;
2187}
2188
80765597 2189static void update_pred_fn(struct event_filter *filter, char *fields)
1d0e78e3 2190{
8ec8405f
SRV
2191 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2192 lockdep_is_held(&event_mutex));
80765597 2193 int i;
1d0e78e3 2194
80765597
SRV
2195 for (i = 0; prog[i].pred; i++) {
2196 struct filter_pred *pred = prog[i].pred;
1d0e78e3
JO
2197 struct ftrace_event_field *field = pred->field;
2198
80765597
SRV
2199 WARN_ON_ONCE(!pred->fn);
2200
1d0e78e3 2201 if (!field) {
80765597
SRV
2202 WARN_ONCE(1, "all leafs should have field defined %d", i);
2203 continue;
1d0e78e3 2204 }
80765597 2205
1d0e78e3 2206 if (!strchr(fields, *field->name))
80765597 2207 continue;
1d0e78e3 2208
1d0e78e3
JO
2209 pred->fn = test_pred_visited_fn;
2210 }
1d0e78e3
JO
2211}
2212
2213static __init int ftrace_test_event_filter(void)
2214{
2215 int i;
2216
2217 printk(KERN_INFO "Testing ftrace filter: ");
2218
2219 for (i = 0; i < DATA_CNT; i++) {
2220 struct event_filter *filter = NULL;
2221 struct test_filter_data_t *d = &test_filter_data[i];
2222 int err;
2223
1e144d73
SRV
2224 err = create_filter(NULL, &event_ftrace_test_filter,
2225 d->filter, false, &filter);
1d0e78e3
JO
2226 if (err) {
2227 printk(KERN_INFO
2228 "Failed to get filter for '%s', err %d\n",
2229 d->filter, err);
38b78eb8 2230 __free_filter(filter);
1d0e78e3
JO
2231 break;
2232 }
2233
8ec8405f
SRV
2234 /* Needed to dereference filter->prog */
2235 mutex_lock(&event_mutex);
86b6ef21
SR
2236 /*
2237 * The preemption disabling is not really needed for self
2238 * tests, but the rcu dereference will complain without it.
2239 */
2240 preempt_disable();
1d0e78e3 2241 if (*d->not_visited)
80765597 2242 update_pred_fn(filter, d->not_visited);
1d0e78e3
JO
2243
2244 test_pred_visited = 0;
2245 err = filter_match_preds(filter, &d->rec);
86b6ef21 2246 preempt_enable();
1d0e78e3 2247
8ec8405f
SRV
2248 mutex_unlock(&event_mutex);
2249
1d0e78e3
JO
2250 __free_filter(filter);
2251
2252 if (test_pred_visited) {
2253 printk(KERN_INFO
2254 "Failed, unwanted pred visited for filter %s\n",
2255 d->filter);
2256 break;
2257 }
2258
2259 if (err != d->match) {
2260 printk(KERN_INFO
2261 "Failed to match filter '%s', expected %d\n",
2262 d->filter, d->match);
2263 break;
2264 }
2265 }
2266
2267 if (i == DATA_CNT)
2268 printk(KERN_CONT "OK\n");
2269
2270 return 0;
2271}
2272
2273late_initcall(ftrace_test_event_filter);
2274
2275#endif /* CONFIG_FTRACE_STARTUP_TEST */