Blame view

kernel/trace/trace_events_filter.c 53.1 KB
7ce7e4249   Tom Zanussi   tracing: add per-...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  /*
   * trace_events_filter - generic event filtering
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   *
   * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
   */
7ce7e4249   Tom Zanussi   tracing: add per-...
20
21
  #include <linux/module.h>
  #include <linux/ctype.h>
ac1adc55f   Tom Zanussi   tracing/filters: ...
22
  #include <linux/mutex.h>
6fb2915df   Li Zefan   tracing/profile: ...
23
  #include <linux/perf_event.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
24
  #include <linux/slab.h>
7ce7e4249   Tom Zanussi   tracing: add per-...
25
26
  
  #include "trace.h"
4bda2d517   Tom Zanussi   tracing/filters: ...
27
  #include "trace_output.h"
7ce7e4249   Tom Zanussi   tracing: add per-...
28

49aa29513   Steven Rostedt   tracing: Add boil...
29
30
31
32
33
34
35
36
  #define DEFAULT_SYS_FILTER_MESSAGE					\
  	"### global filter ###
  "					\
  	"# Use this to set filters for multiple events.
  "		\
  	"# Only events with the given fields will be affected.
  "	\
  	"# If no events are modified, an error message will be displayed here"
8b3725621   Tom Zanussi   tracing/filters: ...
37
  enum filter_op_ids
7ce7e4249   Tom Zanussi   tracing: add per-...
38
  {
8b3725621   Tom Zanussi   tracing/filters: ...
39
40
  	OP_OR,
  	OP_AND,
b0f1a59a9   Li Zefan   tracing/filters: ...
41
  	OP_GLOB,
8b3725621   Tom Zanussi   tracing/filters: ...
42
43
44
45
46
47
  	OP_NE,
  	OP_EQ,
  	OP_LT,
  	OP_LE,
  	OP_GT,
  	OP_GE,
1a891cf19   Steven Rostedt   tracing: Add bina...
48
  	OP_BAND,
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
49
  	OP_NOT,
8b3725621   Tom Zanussi   tracing/filters: ...
50
51
52
53
54
55
56
57
58
  	OP_NONE,
  	OP_OPEN_PAREN,
  };
  
  struct filter_op {
  	int id;
  	char *string;
  	int precedence;
  };
1a891cf19   Steven Rostedt   tracing: Add bina...
59
  /* Order must be the same as enum filter_op_ids above */
8b3725621   Tom Zanussi   tracing/filters: ...
60
  static struct filter_op filter_ops[] = {
b0f1a59a9   Li Zefan   tracing/filters: ...
61
62
63
64
65
66
67
68
69
  	{ OP_OR,	"||",		1 },
  	{ OP_AND,	"&&",		2 },
  	{ OP_GLOB,	"~",		4 },
  	{ OP_NE,	"!=",		4 },
  	{ OP_EQ,	"==",		4 },
  	{ OP_LT,	"<",		5 },
  	{ OP_LE,	"<=",		5 },
  	{ OP_GT,	">",		5 },
  	{ OP_GE,	">=",		5 },
1a891cf19   Steven Rostedt   tracing: Add bina...
70
  	{ OP_BAND,	"&",		6 },
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
71
  	{ OP_NOT,	"!",		6 },
b0f1a59a9   Li Zefan   tracing/filters: ...
72
73
  	{ OP_NONE,	"OP_NONE",	0 },
  	{ OP_OPEN_PAREN, "(",		0 },
8b3725621   Tom Zanussi   tracing/filters: ...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  };
  
  enum {
  	FILT_ERR_NONE,
  	FILT_ERR_INVALID_OP,
  	FILT_ERR_UNBALANCED_PAREN,
  	FILT_ERR_TOO_MANY_OPERANDS,
  	FILT_ERR_OPERAND_TOO_LONG,
  	FILT_ERR_FIELD_NOT_FOUND,
  	FILT_ERR_ILLEGAL_FIELD_OP,
  	FILT_ERR_ILLEGAL_INTVAL,
  	FILT_ERR_BAD_SUBSYS_FILTER,
  	FILT_ERR_TOO_MANY_PREDS,
  	FILT_ERR_MISSING_FIELD,
  	FILT_ERR_INVALID_FILTER,
5500fa511   Jiri Olsa   ftrace, perf: Add...
89
  	FILT_ERR_IP_FIELD_ONLY,
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
90
  	FILT_ERR_ILLEGAL_NOT_OP,
8b3725621   Tom Zanussi   tracing/filters: ...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  };
  
  static char *err_text[] = {
  	"No error",
  	"Invalid operator",
  	"Unbalanced parens",
  	"Too many operands",
  	"Operand too long",
  	"Field not found",
  	"Illegal operation for field type",
  	"Illegal integer value",
  	"Couldn't find or set field in one of a subsystem's events",
  	"Too many terms in predicate expression",
  	"Missing field name and/or value",
  	"Meaningless filter expression",
5500fa511   Jiri Olsa   ftrace, perf: Add...
106
  	"Only 'ip' field is supported for function trace",
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
107
  	"Illegal use of '!'",
8b3725621   Tom Zanussi   tracing/filters: ...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  };
  
  struct opstack_op {
  	int op;
  	struct list_head list;
  };
  
  struct postfix_elt {
  	int op;
  	char *operand;
  	struct list_head list;
  };
  
  struct filter_parse_state {
  	struct filter_op *ops;
  	struct list_head opstack;
  	struct list_head postfix;
  	int lasterr;
  	int lasterr_pos;
  
  	struct {
  		char *string;
  		unsigned int cnt;
  		unsigned int tail;
  	} infix;
  
  	struct {
  		char string[MAX_FILTER_STR_VAL];
  		int pos;
  		unsigned int tail;
  	} operand;
  };
61e9dea20   Steven Rostedt   tracing/filter: U...
140
141
142
143
  struct pred_stack {
  	struct filter_pred	**preds;
  	int			index;
  };
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
144
  /* If not of not match is equal to not of not, then it is a match */
197e2eabc   Li Zefan   tracing: move PRE...
145
  #define DEFINE_COMPARISON_PRED(type)					\
58d9a597c   Steven Rostedt   tracing/filter: M...
146
  static int filter_pred_##type(struct filter_pred *pred, void *event)	\
197e2eabc   Li Zefan   tracing: move PRE...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  {									\
  	type *addr = (type *)(event + pred->offset);			\
  	type val = (type)pred->val;					\
  	int match = 0;							\
  									\
  	switch (pred->op) {						\
  	case OP_LT:							\
  		match = (*addr < val);					\
  		break;							\
  	case OP_LE:							\
  		match = (*addr <= val);					\
  		break;							\
  	case OP_GT:							\
  		match = (*addr > val);					\
  		break;							\
  	case OP_GE:							\
  		match = (*addr >= val);					\
  		break;							\
1a891cf19   Steven Rostedt   tracing: Add bina...
165
166
167
  	case OP_BAND:							\
  		match = (*addr & val);					\
  		break;							\
197e2eabc   Li Zefan   tracing: move PRE...
168
169
170
171
  	default:							\
  		break;							\
  	}								\
  									\
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
172
  	return !!match == !pred->not;					\
197e2eabc   Li Zefan   tracing: move PRE...
173
174
175
  }
  
  #define DEFINE_EQUALITY_PRED(size)					\
58d9a597c   Steven Rostedt   tracing/filter: M...
176
  static int filter_pred_##size(struct filter_pred *pred, void *event)	\
197e2eabc   Li Zefan   tracing: move PRE...
177
178
179
180
181
182
183
184
185
  {									\
  	u##size *addr = (u##size *)(event + pred->offset);		\
  	u##size val = (u##size)pred->val;				\
  	int match;							\
  									\
  	match = (val == *addr) ^ pred->not;				\
  									\
  	return match;							\
  }
8b3725621   Tom Zanussi   tracing/filters: ...
186
187
188
189
190
191
192
193
194
195
196
197
198
  DEFINE_COMPARISON_PRED(s64);
  DEFINE_COMPARISON_PRED(u64);
  DEFINE_COMPARISON_PRED(s32);
  DEFINE_COMPARISON_PRED(u32);
  DEFINE_COMPARISON_PRED(s16);
  DEFINE_COMPARISON_PRED(u16);
  DEFINE_COMPARISON_PRED(s8);
  DEFINE_COMPARISON_PRED(u8);
  
  DEFINE_EQUALITY_PRED(64);
  DEFINE_EQUALITY_PRED(32);
  DEFINE_EQUALITY_PRED(16);
  DEFINE_EQUALITY_PRED(8);
e8808c101   Frederic Weisbecker   tracing/filters: ...
199
  /* Filter predicate for fixed sized arrays of characters */
58d9a597c   Steven Rostedt   tracing/filter: M...
200
  static int filter_pred_string(struct filter_pred *pred, void *event)
7ce7e4249   Tom Zanussi   tracing: add per-...
201
202
203
  {
  	char *addr = (char *)(event + pred->offset);
  	int cmp, match;
1889d2092   Frederic Weisbecker   tracing/filters: ...
204
  	cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
7ce7e4249   Tom Zanussi   tracing: add per-...
205

1889d2092   Frederic Weisbecker   tracing/filters: ...
206
  	match = cmp ^ pred->not;
7ce7e4249   Tom Zanussi   tracing: add per-...
207
208
209
  
  	return match;
  }
87a342f5d   Li Zefan   tracing/filters: ...
210
  /* Filter predicate for char * pointers */
58d9a597c   Steven Rostedt   tracing/filter: M...
211
  static int filter_pred_pchar(struct filter_pred *pred, void *event)
87a342f5d   Li Zefan   tracing/filters: ...
212
213
214
  {
  	char **addr = (char **)(event + pred->offset);
  	int cmp, match;
16da27a8b   Li Zefan   tracing/filters: ...
215
  	int len = strlen(*addr) + 1;	/* including tailing '\0' */
87a342f5d   Li Zefan   tracing/filters: ...
216

16da27a8b   Li Zefan   tracing/filters: ...
217
  	cmp = pred->regex.match(*addr, &pred->regex, len);
87a342f5d   Li Zefan   tracing/filters: ...
218

1889d2092   Frederic Weisbecker   tracing/filters: ...
219
  	match = cmp ^ pred->not;
87a342f5d   Li Zefan   tracing/filters: ...
220
221
222
  
  	return match;
  }
e8808c101   Frederic Weisbecker   tracing/filters: ...
223
224
225
226
227
228
229
230
231
232
  /*
   * Filter predicate for dynamic sized arrays of characters.
   * These are implemented through a list of strings at the end
   * of the entry.
   * Also each of these strings have a field in the entry which
   * contains its offset from the beginning of the entry.
   * We have then first to get this field, dereference it
   * and add it to the address of the entry, and at last we have
   * the address of the string.
   */
58d9a597c   Steven Rostedt   tracing/filter: M...
233
  static int filter_pred_strloc(struct filter_pred *pred, void *event)
e8808c101   Frederic Weisbecker   tracing/filters: ...
234
  {
7d536cb3f   Li Zefan   tracing/events: r...
235
236
237
  	u32 str_item = *(u32 *)(event + pred->offset);
  	int str_loc = str_item & 0xffff;
  	int str_len = str_item >> 16;
e8808c101   Frederic Weisbecker   tracing/filters: ...
238
239
  	char *addr = (char *)(event + str_loc);
  	int cmp, match;
1889d2092   Frederic Weisbecker   tracing/filters: ...
240
  	cmp = pred->regex.match(addr, &pred->regex, str_len);
e8808c101   Frederic Weisbecker   tracing/filters: ...
241

1889d2092   Frederic Weisbecker   tracing/filters: ...
242
  	match = cmp ^ pred->not;
e8808c101   Frederic Weisbecker   tracing/filters: ...
243
244
245
  
  	return match;
  }
9f6166807   Daniel Wagner   tracing: Allow tr...
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  /* Filter predicate for CPUs. */
  static int filter_pred_cpu(struct filter_pred *pred, void *event)
  {
  	int cpu, cmp;
  	int match = 0;
  
  	cpu = raw_smp_processor_id();
  	cmp = pred->val;
  
  	switch (pred->op) {
  	case OP_EQ:
  		match = cpu == cmp;
  		break;
  	case OP_LT:
  		match = cpu < cmp;
  		break;
  	case OP_LE:
  		match = cpu <= cmp;
  		break;
  	case OP_GT:
  		match = cpu > cmp;
  		break;
  	case OP_GE:
  		match = cpu >= cmp;
  		break;
  	default:
  		break;
  	}
  
  	return !!match == !pred->not;
  }
  
  /* Filter predicate for COMM. */
  static int filter_pred_comm(struct filter_pred *pred, void *event)
  {
  	int cmp, match;
  
  	cmp = pred->regex.match(current->comm, &pred->regex,
  				pred->regex.field_len);
  	match = cmp ^ pred->not;
  
  	return match;
  }
58d9a597c   Steven Rostedt   tracing/filter: M...
289
  static int filter_pred_none(struct filter_pred *pred, void *event)
0a19e53c1   Tom Zanussi   tracing/filters: ...
290
291
292
  {
  	return 0;
  }
d1303dd1d   Li Zefan   tracing/filters: ...
293
294
295
296
297
298
299
300
301
302
303
  /*
   * regex_match_foo - Basic regex callbacks
   *
   * @str: the string to be searched
   * @r:   the regex structure containing the pattern string
   * @len: the length of the string to be searched (including '\0')
   *
   * Note:
   * - @str might not be NULL-terminated if it's of type DYN_STRING
   *   or STATIC_STRING
   */
1889d2092   Frederic Weisbecker   tracing/filters: ...
304
305
306
307
308
309
310
311
312
  static int regex_match_full(char *str, struct regex *r, int len)
  {
  	if (strncmp(str, r->pattern, len) == 0)
  		return 1;
  	return 0;
  }
  
  static int regex_match_front(char *str, struct regex *r, int len)
  {
285caad41   Li Zefan   tracing/filters: ...
313
  	if (strncmp(str, r->pattern, r->len) == 0)
1889d2092   Frederic Weisbecker   tracing/filters: ...
314
315
316
317
318
319
  		return 1;
  	return 0;
  }
  
  static int regex_match_middle(char *str, struct regex *r, int len)
  {
b2af211f2   Li Zefan   tracing/filters: ...
320
  	if (strnstr(str, r->pattern, len))
1889d2092   Frederic Weisbecker   tracing/filters: ...
321
322
323
324
325
326
  		return 1;
  	return 0;
  }
  
  static int regex_match_end(char *str, struct regex *r, int len)
  {
a3291c14e   Li Zefan   tracing/filters: ...
327
  	int strlen = len - 1;
1889d2092   Frederic Weisbecker   tracing/filters: ...
328

a3291c14e   Li Zefan   tracing/filters: ...
329
330
  	if (strlen >= r->len &&
  	    memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
1889d2092   Frederic Weisbecker   tracing/filters: ...
331
332
333
  		return 1;
  	return 0;
  }
3f6fe06db   Frederic Weisbecker   tracing/filters: ...
334
335
336
337
338
339
340
341
  /**
   * filter_parse_regex - parse a basic regex
   * @buff:   the raw regex
   * @len:    length of the regex
   * @search: will point to the beginning of the string to compare
   * @not:    tell whether the match will have to be inverted
   *
   * This passes in a buffer containing a regex and this function will
1889d2092   Frederic Weisbecker   tracing/filters: ...
342
343
344
345
346
347
348
349
350
   * set search to point to the search part of the buffer and
   * return the type of search it is (see enum above).
   * This does modify buff.
   *
   * Returns enum type.
   *  search returns the pointer to use for comparison.
   *  not returns 1 if buff started with a '!'
   *     0 otherwise.
   */
3f6fe06db   Frederic Weisbecker   tracing/filters: ...
351
  enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
1889d2092   Frederic Weisbecker   tracing/filters: ...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  {
  	int type = MATCH_FULL;
  	int i;
  
  	if (buff[0] == '!') {
  		*not = 1;
  		buff++;
  		len--;
  	} else
  		*not = 0;
  
  	*search = buff;
  
  	for (i = 0; i < len; i++) {
  		if (buff[i] == '*') {
  			if (!i) {
  				*search = buff + 1;
  				type = MATCH_END_ONLY;
  			} else {
  				if (type == MATCH_END_ONLY)
  					type = MATCH_MIDDLE_ONLY;
  				else
  					type = MATCH_FRONT_ONLY;
  				buff[i] = 0;
  				break;
  			}
  		}
  	}
  
  	return type;
  }
b0f1a59a9   Li Zefan   tracing/filters: ...
383
  static void filter_build_regex(struct filter_pred *pred)
1889d2092   Frederic Weisbecker   tracing/filters: ...
384
385
  {
  	struct regex *r = &pred->regex;
b0f1a59a9   Li Zefan   tracing/filters: ...
386
387
388
389
390
391
392
393
394
  	char *search;
  	enum regex_type type = MATCH_FULL;
  	int not = 0;
  
  	if (pred->op == OP_GLOB) {
  		type = filter_parse_regex(r->pattern, r->len, &search, &not);
  		r->len = strlen(search);
  		memmove(r->pattern, search, r->len+1);
  	}
1889d2092   Frederic Weisbecker   tracing/filters: ...
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  
  	switch (type) {
  	case MATCH_FULL:
  		r->match = regex_match_full;
  		break;
  	case MATCH_FRONT_ONLY:
  		r->match = regex_match_front;
  		break;
  	case MATCH_MIDDLE_ONLY:
  		r->match = regex_match_middle;
  		break;
  	case MATCH_END_ONLY:
  		r->match = regex_match_end;
  		break;
  	}
  
  	pred->not ^= not;
1889d2092   Frederic Weisbecker   tracing/filters: ...
412
  }
61e9dea20   Steven Rostedt   tracing/filter: U...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
  enum move_type {
  	MOVE_DOWN,
  	MOVE_UP_FROM_LEFT,
  	MOVE_UP_FROM_RIGHT
  };
  
  static struct filter_pred *
  get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
  		int index, enum move_type *move)
  {
  	if (pred->parent & FILTER_PRED_IS_RIGHT)
  		*move = MOVE_UP_FROM_RIGHT;
  	else
  		*move = MOVE_UP_FROM_LEFT;
  	pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
  
  	return pred;
  }
f03f59799   Jiri Olsa   tracing/filter: U...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  enum walk_return {
  	WALK_PRED_ABORT,
  	WALK_PRED_PARENT,
  	WALK_PRED_DEFAULT,
  };
  
  typedef int (*filter_pred_walkcb_t) (enum move_type move,
  				     struct filter_pred *pred,
  				     int *err, void *data);
  
  static int walk_pred_tree(struct filter_pred *preds,
  			  struct filter_pred *root,
  			  filter_pred_walkcb_t cb, void *data)
  {
  	struct filter_pred *pred = root;
  	enum move_type move = MOVE_DOWN;
  	int done = 0;
  
  	if  (!preds)
  		return -EINVAL;
  
  	do {
  		int err = 0, ret;
  
  		ret = cb(move, pred, &err, data);
  		if (ret == WALK_PRED_ABORT)
  			return err;
  		if (ret == WALK_PRED_PARENT)
  			goto get_parent;
  
  		switch (move) {
  		case MOVE_DOWN:
  			if (pred->left != FILTER_PRED_INVALID) {
  				pred = &preds[pred->left];
  				continue;
  			}
  			goto get_parent;
  		case MOVE_UP_FROM_LEFT:
  			pred = &preds[pred->right];
  			move = MOVE_DOWN;
  			continue;
  		case MOVE_UP_FROM_RIGHT:
   get_parent:
  			if (pred == root)
  				break;
  			pred = get_pred_parent(pred, preds,
  					       pred->parent,
  					       &move);
  			continue;
  		}
  		done = 1;
  	} while (!done);
  
  	/* We are fine. */
  	return 0;
  }
43cd41455   Steven Rostedt   tracing/filter: O...
487
488
489
490
491
492
493
494
495
496
  /*
   * A series of AND or ORs where found together. Instead of
   * climbing up and down the tree branches, an array of the
   * ops were made in order of checks. We can just move across
   * the array and short circuit if needed.
   */
  static int process_ops(struct filter_pred *preds,
  		       struct filter_pred *op, void *rec)
  {
  	struct filter_pred *pred;
1ef1d1c23   Ingo Molnar   trace, filters: I...
497
  	int match = 0;
43cd41455   Steven Rostedt   tracing/filter: O...
498
  	int type;
43cd41455   Steven Rostedt   tracing/filter: O...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
  	int i;
  
  	/*
  	 * Micro-optimization: We set type to true if op
  	 * is an OR and false otherwise (AND). Then we
  	 * just need to test if the match is equal to
  	 * the type, and if it is, we can short circuit the
  	 * rest of the checks:
  	 *
  	 * if ((match && op->op == OP_OR) ||
  	 *     (!match && op->op == OP_AND))
  	 *	  return match;
  	 */
  	type = op->op == OP_OR;
  
  	for (i = 0; i < op->val; i++) {
  		pred = &preds[op->ops[i]];
f30120fce   Jiri Olsa   tracing/filter: C...
516
517
  		if (!WARN_ON_ONCE(!pred->fn))
  			match = pred->fn(pred, rec);
43cd41455   Steven Rostedt   tracing/filter: O...
518
  		if (!!match == type)
eabb8980a   Steven Rostedt (Red Hat)   tracing: Allow NO...
519
  			break;
43cd41455   Steven Rostedt   tracing/filter: O...
520
  	}
eabb8980a   Steven Rostedt (Red Hat)   tracing: Allow NO...
521
522
  	/* If not of not match is equal to not of not, then it is a match */
  	return !!match == !op->not;
43cd41455   Steven Rostedt   tracing/filter: O...
523
  }
f30120fce   Jiri Olsa   tracing/filter: C...
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  struct filter_match_preds_data {
  	struct filter_pred *preds;
  	int match;
  	void *rec;
  };
  
  static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred,
  				 int *err, void *data)
  {
  	struct filter_match_preds_data *d = data;
  
  	*err = 0;
  	switch (move) {
  	case MOVE_DOWN:
  		/* only AND and OR have children */
  		if (pred->left != FILTER_PRED_INVALID) {
  			/* If ops is set, then it was folded. */
  			if (!pred->ops)
  				return WALK_PRED_DEFAULT;
  			/* We can treat folded ops as a leaf node */
  			d->match = process_ops(d->preds, pred, d->rec);
  		} else {
  			if (!WARN_ON_ONCE(!pred->fn))
  				d->match = pred->fn(pred, d->rec);
  		}
  
  		return WALK_PRED_PARENT;
  	case MOVE_UP_FROM_LEFT:
  		/*
  		 * Check for short circuits.
  		 *
  		 * Optimization: !!match == (pred->op == OP_OR)
  		 *   is the same as:
  		 * if ((match && pred->op == OP_OR) ||
  		 *     (!match && pred->op == OP_AND))
  		 */
  		if (!!d->match == (pred->op == OP_OR))
  			return WALK_PRED_PARENT;
  		break;
  	case MOVE_UP_FROM_RIGHT:
  		break;
  	}
  
  	return WALK_PRED_DEFAULT;
  }
7ce7e4249   Tom Zanussi   tracing: add per-...
569
  /* return 1 if event matches, 0 otherwise (discard) */
6fb2915df   Li Zefan   tracing/profile: ...
570
  int filter_match_preds(struct event_filter *filter, void *rec)
7ce7e4249   Tom Zanussi   tracing: add per-...
571
  {
74e9e58c3   Steven Rostedt   tracing/filter: A...
572
  	struct filter_pred *preds;
61e9dea20   Steven Rostedt   tracing/filter: U...
573
  	struct filter_pred *root;
f30120fce   Jiri Olsa   tracing/filter: C...
574
575
576
577
578
579
  	struct filter_match_preds_data data = {
  		/* match is currently meaningless */
  		.match = -1,
  		.rec   = rec,
  	};
  	int n_preds, ret;
7ce7e4249   Tom Zanussi   tracing: add per-...
580

6d54057d7   Steven Rostedt   tracing/filter: H...
581
  	/* no filter is considered a match */
75b8e9826   Steven Rostedt   tracing/filter: S...
582
583
584
585
  	if (!filter)
  		return 1;
  
  	n_preds = filter->n_preds;
6d54057d7   Steven Rostedt   tracing/filter: H...
586
587
  	if (!n_preds)
  		return 1;
c9c53ca03   Steven Rostedt   tracing/filter: D...
588
  	/*
61e9dea20   Steven Rostedt   tracing/filter: U...
589
  	 * n_preds, root and filter->preds are protect with preemption disabled.
c9c53ca03   Steven Rostedt   tracing/filter: D...
590
  	 */
61e9dea20   Steven Rostedt   tracing/filter: U...
591
592
593
  	root = rcu_dereference_sched(filter->root);
  	if (!root)
  		return 1;
c9c53ca03   Steven Rostedt   tracing/filter: D...
594

f30120fce   Jiri Olsa   tracing/filter: C...
595
596
597
598
  	data.preds = preds = rcu_dereference_sched(filter->preds);
  	ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data);
  	WARN_ON(ret);
  	return data.match;
7ce7e4249   Tom Zanussi   tracing: add per-...
599
  }
17c873ec2   Steven Rostedt   tracing/events: a...
600
  EXPORT_SYMBOL_GPL(filter_match_preds);
7ce7e4249   Tom Zanussi   tracing: add per-...
601

8b3725621   Tom Zanussi   tracing/filters: ...
602
  static void parse_error(struct filter_parse_state *ps, int err, int pos)
7ce7e4249   Tom Zanussi   tracing: add per-...
603
  {
8b3725621   Tom Zanussi   tracing/filters: ...
604
605
606
  	ps->lasterr = err;
  	ps->lasterr_pos = pos;
  }
7ce7e4249   Tom Zanussi   tracing: add per-...
607

8b3725621   Tom Zanussi   tracing/filters: ...
608
609
  static void remove_filter_string(struct event_filter *filter)
  {
75b8e9826   Steven Rostedt   tracing/filter: S...
610
611
  	if (!filter)
  		return;
8b3725621   Tom Zanussi   tracing/filters: ...
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
  	kfree(filter->filter_string);
  	filter->filter_string = NULL;
  }
  
  static int replace_filter_string(struct event_filter *filter,
  				 char *filter_string)
  {
  	kfree(filter->filter_string);
  	filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
  	if (!filter->filter_string)
  		return -ENOMEM;
  
  	return 0;
  }
  
  static int append_filter_string(struct event_filter *filter,
  				char *string)
  {
  	int newlen;
  	char *new_filter_string;
  
  	BUG_ON(!filter->filter_string);
  	newlen = strlen(filter->filter_string) + strlen(string) + 1;
  	new_filter_string = kmalloc(newlen, GFP_KERNEL);
  	if (!new_filter_string)
  		return -ENOMEM;
  
  	strcpy(new_filter_string, filter->filter_string);
  	strcat(new_filter_string, string);
  	kfree(filter->filter_string);
  	filter->filter_string = new_filter_string;
  
  	return 0;
  }
  
  static void append_filter_err(struct filter_parse_state *ps,
  			      struct event_filter *filter)
  {
  	int pos = ps->lasterr_pos;
  	char *buf, *pbuf;
  
  	buf = (char *)__get_free_page(GFP_TEMPORARY);
  	if (!buf)
4bda2d517   Tom Zanussi   tracing/filters: ...
655
  		return;
7ce7e4249   Tom Zanussi   tracing: add per-...
656

8b3725621   Tom Zanussi   tracing/filters: ...
657
658
659
660
661
662
663
664
665
666
667
668
669
  	append_filter_string(filter, "
  ");
  	memset(buf, ' ', PAGE_SIZE);
  	if (pos > PAGE_SIZE - 128)
  		pos = 0;
  	buf[pos] = '^';
  	pbuf = &buf[pos] + 1;
  
  	sprintf(pbuf, "
  parse_error: %s
  ", err_text[ps->lasterr]);
  	append_filter_string(filter, buf);
  	free_page((unsigned long) buf);
7ce7e4249   Tom Zanussi   tracing: add per-...
670
  }
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
671
  static inline struct event_filter *event_filter(struct trace_event_file *file)
f306cc82a   Tom Zanussi   tracing: Update e...
672
  {
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
673
  	return file->filter;
f306cc82a   Tom Zanussi   tracing: Update e...
674
  }
e2912b091   Oleg Nesterov   tracing: Change e...
675
  /* caller must hold event_mutex */
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
676
  void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
ac1adc55f   Tom Zanussi   tracing/filters: ...
677
  {
f306cc82a   Tom Zanussi   tracing: Update e...
678
  	struct event_filter *filter = event_filter(file);
8b3725621   Tom Zanussi   tracing/filters: ...
679

8e254c1d1   Li Zefan   tracing/filters: ...
680
  	if (filter && filter->filter_string)
8b3725621   Tom Zanussi   tracing/filters: ...
681
682
683
  		trace_seq_printf(s, "%s
  ", filter->filter_string);
  	else
146c3442f   zhangwei(Jovi)   tracing: Use trac...
684
685
  		trace_seq_puts(s, "none
  ");
ac1adc55f   Tom Zanussi   tracing/filters: ...
686
  }
8b3725621   Tom Zanussi   tracing/filters: ...
687
  void print_subsystem_event_filter(struct event_subsystem *system,
ac1adc55f   Tom Zanussi   tracing/filters: ...
688
689
  				  struct trace_seq *s)
  {
75b8e9826   Steven Rostedt   tracing/filter: S...
690
  	struct event_filter *filter;
8b3725621   Tom Zanussi   tracing/filters: ...
691

00e95830a   Li Zefan   tracing/filters: ...
692
  	mutex_lock(&event_mutex);
75b8e9826   Steven Rostedt   tracing/filter: S...
693
  	filter = system->filter;
8e254c1d1   Li Zefan   tracing/filters: ...
694
  	if (filter && filter->filter_string)
8b3725621   Tom Zanussi   tracing/filters: ...
695
696
697
  		trace_seq_printf(s, "%s
  ", filter->filter_string);
  	else
146c3442f   zhangwei(Jovi)   tracing: Use trac...
698
699
  		trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "
  ");
00e95830a   Li Zefan   tracing/filters: ...
700
  	mutex_unlock(&event_mutex);
ac1adc55f   Tom Zanussi   tracing/filters: ...
701
  }
61e9dea20   Steven Rostedt   tracing/filter: U...
702
703
  static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
  {
47b0edcb5   Thomas Meyer   tracing/trivial: ...
704
  	stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
61e9dea20   Steven Rostedt   tracing/filter: U...
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  	if (!stack->preds)
  		return -ENOMEM;
  	stack->index = n_preds;
  	return 0;
  }
  
  static void __free_pred_stack(struct pred_stack *stack)
  {
  	kfree(stack->preds);
  	stack->index = 0;
  }
  
  static int __push_pred_stack(struct pred_stack *stack,
  			     struct filter_pred *pred)
  {
  	int index = stack->index;
  
  	if (WARN_ON(index == 0))
  		return -ENOSPC;
  
  	stack->preds[--index] = pred;
  	stack->index = index;
  	return 0;
  }
  
  static struct filter_pred *
  __pop_pred_stack(struct pred_stack *stack)
  {
  	struct filter_pred *pred;
  	int index = stack->index;
  
  	pred = stack->preds[index++];
  	if (!pred)
  		return NULL;
  
  	stack->index = index;
  	return pred;
  }
  
  static int filter_set_pred(struct event_filter *filter,
  			   int idx,
  			   struct pred_stack *stack,
9d96cd174   Jiri Olsa   tracing/filter: S...
747
  			   struct filter_pred *src)
0a19e53c1   Tom Zanussi   tracing/filters: ...
748
  {
61e9dea20   Steven Rostedt   tracing/filter: U...
749
750
751
  	struct filter_pred *dest = &filter->preds[idx];
  	struct filter_pred *left;
  	struct filter_pred *right;
0a19e53c1   Tom Zanussi   tracing/filters: ...
752
  	*dest = *src;
61e9dea20   Steven Rostedt   tracing/filter: U...
753
  	dest->index = idx;
0a19e53c1   Tom Zanussi   tracing/filters: ...
754

61e9dea20   Steven Rostedt   tracing/filter: U...
755
756
757
758
759
  	if (dest->op == OP_OR || dest->op == OP_AND) {
  		right = __pop_pred_stack(stack);
  		left = __pop_pred_stack(stack);
  		if (!left || !right)
  			return -EINVAL;
43cd41455   Steven Rostedt   tracing/filter: O...
760
761
762
763
764
765
  		/*
  		 * If both children can be folded
  		 * and they are the same op as this op or a leaf,
  		 * then this op can be folded.
  		 */
  		if (left->index & FILTER_PRED_FOLD &&
eabb8980a   Steven Rostedt (Red Hat)   tracing: Allow NO...
766
  		    ((left->op == dest->op && !left->not) ||
43cd41455   Steven Rostedt   tracing/filter: O...
767
768
  		     left->left == FILTER_PRED_INVALID) &&
  		    right->index & FILTER_PRED_FOLD &&
eabb8980a   Steven Rostedt (Red Hat)   tracing: Allow NO...
769
  		    ((right->op == dest->op && !right->not) ||
43cd41455   Steven Rostedt   tracing/filter: O...
770
771
772
773
774
775
  		     right->left == FILTER_PRED_INVALID))
  			dest->index |= FILTER_PRED_FOLD;
  
  		dest->left = left->index & ~FILTER_PRED_FOLD;
  		dest->right = right->index & ~FILTER_PRED_FOLD;
  		left->parent = dest->index & ~FILTER_PRED_FOLD;
61e9dea20   Steven Rostedt   tracing/filter: U...
776
  		right->parent = dest->index | FILTER_PRED_IS_RIGHT;
43cd41455   Steven Rostedt   tracing/filter: O...
777
  	} else {
61e9dea20   Steven Rostedt   tracing/filter: U...
778
779
780
781
782
  		/*
  		 * Make dest->left invalid to be used as a quick
  		 * way to know this is a leaf node.
  		 */
  		dest->left = FILTER_PRED_INVALID;
43cd41455   Steven Rostedt   tracing/filter: O...
783
784
785
  		/* All leafs allow folding the parent ops. */
  		dest->index |= FILTER_PRED_FOLD;
  	}
61e9dea20   Steven Rostedt   tracing/filter: U...
786
  	return __push_pred_stack(stack, dest);
0a19e53c1   Tom Zanussi   tracing/filters: ...
787
  }
c9c53ca03   Steven Rostedt   tracing/filter: D...
788
789
  static void __free_preds(struct event_filter *filter)
  {
60705c894   Steven Rostedt (Red Hat)   tracing: Fix leak...
790
  	int i;
c9c53ca03   Steven Rostedt   tracing/filter: D...
791
  	if (filter->preds) {
60705c894   Steven Rostedt (Red Hat)   tracing: Fix leak...
792
793
  		for (i = 0; i < filter->n_preds; i++)
  			kfree(filter->preds[i].ops);
c9c53ca03   Steven Rostedt   tracing/filter: D...
794
795
796
797
798
799
  		kfree(filter->preds);
  		filter->preds = NULL;
  	}
  	filter->a_preds = 0;
  	filter->n_preds = 0;
  }
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
800
  static void filter_disable(struct trace_event_file *file)
f306cc82a   Tom Zanussi   tracing: Update e...
801
  {
0fc1b09ff   Steven Rostedt (Red Hat)   tracing: Use temp...
802
  	unsigned long old_flags = file->flags;
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
803
  	file->flags &= ~EVENT_FILE_FL_FILTERED;
0fc1b09ff   Steven Rostedt (Red Hat)   tracing: Use temp...
804
805
806
  
  	if (old_flags != file->flags)
  		trace_buffered_event_disable();
f306cc82a   Tom Zanussi   tracing: Update e...
807
  }
c9c53ca03   Steven Rostedt   tracing/filter: D...
808
  static void __free_filter(struct event_filter *filter)
2df75e415   Li Zefan   tracing/events: f...
809
  {
8e254c1d1   Li Zefan   tracing/filters: ...
810
811
  	if (!filter)
  		return;
c9c53ca03   Steven Rostedt   tracing/filter: D...
812
  	__free_preds(filter);
57be88878   Li Zefan   tracing/filters: ...
813
  	kfree(filter->filter_string);
2df75e415   Li Zefan   tracing/events: f...
814
  	kfree(filter);
6fb2915df   Li Zefan   tracing/profile: ...
815
  }
bac5fb97a   Tom Zanussi   tracing: Add and ...
816
817
818
819
  void free_event_filter(struct event_filter *filter)
  {
  	__free_filter(filter);
  }
c9c53ca03   Steven Rostedt   tracing/filter: D...
820
  static struct event_filter *__alloc_filter(void)
0a19e53c1   Tom Zanussi   tracing/filters: ...
821
  {
30e673b23   Tom Zanussi   tracing/filters: ...
822
  	struct event_filter *filter;
0a19e53c1   Tom Zanussi   tracing/filters: ...
823

6fb2915df   Li Zefan   tracing/profile: ...
824
  	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
c9c53ca03   Steven Rostedt   tracing/filter: D...
825
826
827
828
829
830
831
  	return filter;
  }
  
  static int __alloc_preds(struct event_filter *filter, int n_preds)
  {
  	struct filter_pred *pred;
  	int i;
4defe682d   Steven Rostedt   tracing/filter: R...
832
833
  	if (filter->preds)
  		__free_preds(filter);
47b0edcb5   Thomas Meyer   tracing/trivial: ...
834
  	filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
c9c53ca03   Steven Rostedt   tracing/filter: D...
835

30e673b23   Tom Zanussi   tracing/filters: ...
836
  	if (!filter->preds)
c9c53ca03   Steven Rostedt   tracing/filter: D...
837
  		return -ENOMEM;
4defe682d   Steven Rostedt   tracing/filter: R...
838
839
  	filter->a_preds = n_preds;
  	filter->n_preds = 0;
30e673b23   Tom Zanussi   tracing/filters: ...
840

c9c53ca03   Steven Rostedt   tracing/filter: D...
841
  	for (i = 0; i < n_preds; i++) {
74e9e58c3   Steven Rostedt   tracing/filter: A...
842
  		pred = &filter->preds[i];
0a19e53c1   Tom Zanussi   tracing/filters: ...
843
  		pred->fn = filter_pred_none;
0a19e53c1   Tom Zanussi   tracing/filters: ...
844
  	}
c9c53ca03   Steven Rostedt   tracing/filter: D...
845
  	return 0;
6fb2915df   Li Zefan   tracing/profile: ...
846
  }
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
847
  static inline void __remove_filter(struct trace_event_file *file)
8e254c1d1   Li Zefan   tracing/filters: ...
848
  {
f306cc82a   Tom Zanussi   tracing: Update e...
849
  	filter_disable(file);
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
850
  	remove_filter_string(file->filter);
f306cc82a   Tom Zanussi   tracing: Update e...
851
  }
7967b3e0c   Steven Rostedt (Red Hat)   tracing: Rename s...
852
  static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
f306cc82a   Tom Zanussi   tracing: Update e...
853
854
  					struct trace_array *tr)
  {
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
855
  	struct trace_event_file *file;
8e254c1d1   Li Zefan   tracing/filters: ...
856

f306cc82a   Tom Zanussi   tracing: Update e...
857
  	list_for_each_entry(file, &tr->events, list) {
bb9ef1cb7   Oleg Nesterov   tracing: Change a...
858
  		if (file->system != dir)
8e254c1d1   Li Zefan   tracing/filters: ...
859
  			continue;
f306cc82a   Tom Zanussi   tracing: Update e...
860
  		__remove_filter(file);
8e254c1d1   Li Zefan   tracing/filters: ...
861
  	}
8e254c1d1   Li Zefan   tracing/filters: ...
862
  }
7ce7e4249   Tom Zanussi   tracing: add per-...
863

7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
864
  static inline void __free_subsystem_filter(struct trace_event_file *file)
cfb180f3e   Tom Zanussi   tracing: add per-...
865
  {
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
866
867
  	__free_filter(file->filter);
  	file->filter = NULL;
f306cc82a   Tom Zanussi   tracing: Update e...
868
  }
7967b3e0c   Steven Rostedt (Red Hat)   tracing: Rename s...
869
  static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
f306cc82a   Tom Zanussi   tracing: Update e...
870
871
  					  struct trace_array *tr)
  {
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
872
  	struct trace_event_file *file;
cfb180f3e   Tom Zanussi   tracing: add per-...
873

f306cc82a   Tom Zanussi   tracing: Update e...
874
  	list_for_each_entry(file, &tr->events, list) {
bb9ef1cb7   Oleg Nesterov   tracing: Change a...
875
  		if (file->system != dir)
8e254c1d1   Li Zefan   tracing/filters: ...
876
  			continue;
f306cc82a   Tom Zanussi   tracing: Update e...
877
  		__free_subsystem_filter(file);
cfb180f3e   Tom Zanussi   tracing: add per-...
878
879
  	}
  }
9d96cd174   Jiri Olsa   tracing/filter: S...
880
881
882
883
  static int filter_add_pred(struct filter_parse_state *ps,
  			   struct event_filter *filter,
  			   struct filter_pred *pred,
  			   struct pred_stack *stack)
7ce7e4249   Tom Zanussi   tracing: add per-...
884
  {
61aaef553   Jiri Olsa   tracing/filter: R...
885
  	int err;
7ce7e4249   Tom Zanussi   tracing: add per-...
886

c9c53ca03   Steven Rostedt   tracing/filter: D...
887
  	if (WARN_ON(filter->n_preds == filter->a_preds)) {
8b3725621   Tom Zanussi   tracing/filters: ...
888
  		parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
0a19e53c1   Tom Zanussi   tracing/filters: ...
889
  		return -ENOSPC;
8b3725621   Tom Zanussi   tracing/filters: ...
890
  	}
7ce7e4249   Tom Zanussi   tracing: add per-...
891

61aaef553   Jiri Olsa   tracing/filter: R...
892
  	err = filter_set_pred(filter, filter->n_preds, stack, pred);
0a19e53c1   Tom Zanussi   tracing/filters: ...
893
894
  	if (err)
  		return err;
30e673b23   Tom Zanussi   tracing/filters: ...
895
  	filter->n_preds++;
7ce7e4249   Tom Zanussi   tracing: add per-...
896

0a19e53c1   Tom Zanussi   tracing/filters: ...
897
  	return 0;
7ce7e4249   Tom Zanussi   tracing: add per-...
898
  }
aa38e9fc3   Li Zefan   tracing/filters: ...
899
  int filter_assign_type(const char *type)
7ce7e4249   Tom Zanussi   tracing: add per-...
900
  {
7fcb7c472   Li Zefan   tracing/events: i...
901
902
  	if (strstr(type, "__data_loc") && strstr(type, "char"))
  		return FILTER_DYN_STRING;
7ce7e4249   Tom Zanussi   tracing: add per-...
903
  	if (strchr(type, '[') && strstr(type, "char"))
e8808c101   Frederic Weisbecker   tracing/filters: ...
904
  		return FILTER_STATIC_STRING;
aa38e9fc3   Li Zefan   tracing/filters: ...
905
906
  	return FILTER_OTHER;
  }
907bff917   Yaowei Bai   tracing: is_legal...
907
  static bool is_legal_op(struct ftrace_event_field *field, int op)
8b3725621   Tom Zanussi   tracing/filters: ...
908
  {
b0f1a59a9   Li Zefan   tracing/filters: ...
909
910
  	if (is_string_field(field) &&
  	    (op != OP_EQ && op != OP_NE && op != OP_GLOB))
907bff917   Yaowei Bai   tracing: is_legal...
911
  		return false;
b0f1a59a9   Li Zefan   tracing/filters: ...
912
  	if (!is_string_field(field) && op == OP_GLOB)
907bff917   Yaowei Bai   tracing: is_legal...
913
  		return false;
8b3725621   Tom Zanussi   tracing/filters: ...
914

907bff917   Yaowei Bai   tracing: is_legal...
915
  	return true;
8b3725621   Tom Zanussi   tracing/filters: ...
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
  }
  
  static filter_pred_fn_t select_comparison_fn(int op, int field_size,
  					     int field_is_signed)
  {
  	filter_pred_fn_t fn = NULL;
  
  	switch (field_size) {
  	case 8:
  		if (op == OP_EQ || op == OP_NE)
  			fn = filter_pred_64;
  		else if (field_is_signed)
  			fn = filter_pred_s64;
  		else
  			fn = filter_pred_u64;
  		break;
  	case 4:
  		if (op == OP_EQ || op == OP_NE)
  			fn = filter_pred_32;
  		else if (field_is_signed)
  			fn = filter_pred_s32;
  		else
  			fn = filter_pred_u32;
  		break;
  	case 2:
  		if (op == OP_EQ || op == OP_NE)
  			fn = filter_pred_16;
  		else if (field_is_signed)
  			fn = filter_pred_s16;
  		else
  			fn = filter_pred_u16;
  		break;
  	case 1:
  		if (op == OP_EQ || op == OP_NE)
  			fn = filter_pred_8;
  		else if (field_is_signed)
  			fn = filter_pred_s8;
  		else
  			fn = filter_pred_u8;
  		break;
  	}
  
  	return fn;
  }
9d96cd174   Jiri Olsa   tracing/filter: S...
960
  static int init_pred(struct filter_parse_state *ps,
61aaef553   Jiri Olsa   tracing/filter: R...
961
  		     struct ftrace_event_field *field,
9d96cd174   Jiri Olsa   tracing/filter: S...
962
  		     struct filter_pred *pred)
7ce7e4249   Tom Zanussi   tracing: add per-...
963
  {
9d96cd174   Jiri Olsa   tracing/filter: S...
964
  	filter_pred_fn_t fn = filter_pred_none;
f66578a76   Li Zefan   tracing/filters: ...
965
  	unsigned long long val;
5e4904cb6   Li Zefan   tracing/filters: ...
966
  	int ret;
7ce7e4249   Tom Zanussi   tracing: add per-...
967

7ce7e4249   Tom Zanussi   tracing: add per-...
968
  	pred->offset = field->offset;
8b3725621   Tom Zanussi   tracing/filters: ...
969
970
971
972
  	if (!is_legal_op(field, pred->op)) {
  		parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
  		return -EINVAL;
  	}
e57cbaf0e   Steven Rostedt (Red Hat)   tracing: Do not h...
973
974
975
976
977
  	if (field->filter_type == FILTER_COMM) {
  		filter_build_regex(pred);
  		fn = filter_pred_comm;
  		pred->regex.field_len = TASK_COMM_LEN;
  	} else if (is_string_field(field)) {
b0f1a59a9   Li Zefan   tracing/filters: ...
978
  		filter_build_regex(pred);
87a342f5d   Li Zefan   tracing/filters: ...
979

e57cbaf0e   Steven Rostedt (Red Hat)   tracing: Do not h...
980
  		if (field->filter_type == FILTER_STATIC_STRING) {
e8808c101   Frederic Weisbecker   tracing/filters: ...
981
  			fn = filter_pred_string;
1889d2092   Frederic Weisbecker   tracing/filters: ...
982
983
  			pred->regex.field_len = field->size;
  		} else if (field->filter_type == FILTER_DYN_STRING)
b0f1a59a9   Li Zefan   tracing/filters: ...
984
  			fn = filter_pred_strloc;
16da27a8b   Li Zefan   tracing/filters: ...
985
  		else
87a342f5d   Li Zefan   tracing/filters: ...
986
  			fn = filter_pred_pchar;
5500fa511   Jiri Olsa   ftrace, perf: Add...
987
988
989
990
991
992
  	} else if (is_function_field(field)) {
  		if (strcmp(field->name, "ip")) {
  			parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0);
  			return -EINVAL;
  		}
  	} else {
5e4904cb6   Li Zefan   tracing/filters: ...
993
  		if (field->is_signed)
bcd83ea6c   Daniel Walter   tracing: Replace ...
994
  			ret = kstrtoll(pred->regex.pattern, 0, &val);
5e4904cb6   Li Zefan   tracing/filters: ...
995
  		else
bcd83ea6c   Daniel Walter   tracing: Replace ...
996
  			ret = kstrtoull(pred->regex.pattern, 0, &val);
5e4904cb6   Li Zefan   tracing/filters: ...
997
  		if (ret) {
8b3725621   Tom Zanussi   tracing/filters: ...
998
  			parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
9f58a159d   Tom Zanussi   tracing/filters: ...
999
  			return -EINVAL;
8b3725621   Tom Zanussi   tracing/filters: ...
1000
  		}
f66578a76   Li Zefan   tracing/filters: ...
1001
  		pred->val = val;
7ce7e4249   Tom Zanussi   tracing: add per-...
1002

e57cbaf0e   Steven Rostedt (Red Hat)   tracing: Do not h...
1003
  		if (field->filter_type == FILTER_CPU)
9f6166807   Daniel Wagner   tracing: Allow tr...
1004
1005
1006
  			fn = filter_pred_cpu;
  		else
  			fn = select_comparison_fn(pred->op, field->size,
1f9963cbb   Li Zefan   tracing/filters: ...
1007
1008
1009
1010
1011
  					  field->is_signed);
  		if (!fn) {
  			parse_error(ps, FILT_ERR_INVALID_OP, 0);
  			return -EINVAL;
  		}
7ce7e4249   Tom Zanussi   tracing: add per-...
1012
  	}
8b3725621   Tom Zanussi   tracing/filters: ...
1013
  	if (pred->op == OP_NE)
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
1014
  		pred->not ^= 1;
ac1adc55f   Tom Zanussi   tracing/filters: ...
1015

9d96cd174   Jiri Olsa   tracing/filter: S...
1016
  	pred->fn = fn;
1f9963cbb   Li Zefan   tracing/filters: ...
1017
  	return 0;
cfb180f3e   Tom Zanussi   tracing: add per-...
1018
  }
8b3725621   Tom Zanussi   tracing/filters: ...
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
  static void parse_init(struct filter_parse_state *ps,
  		       struct filter_op *ops,
  		       char *infix_string)
  {
  	memset(ps, '\0', sizeof(*ps));
  
  	ps->infix.string = infix_string;
  	ps->infix.cnt = strlen(infix_string);
  	ps->ops = ops;
  
  	INIT_LIST_HEAD(&ps->opstack);
  	INIT_LIST_HEAD(&ps->postfix);
  }
  
  static char infix_next(struct filter_parse_state *ps)
  {
6b88f44e1   Steven Rostedt (Red Hat)   tracing/filter: D...
1035
1036
  	if (!ps->infix.cnt)
  		return 0;
8b3725621   Tom Zanussi   tracing/filters: ...
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
  	ps->infix.cnt--;
  
  	return ps->infix.string[ps->infix.tail++];
  }
  
  static char infix_peek(struct filter_parse_state *ps)
  {
  	if (ps->infix.tail == strlen(ps->infix.string))
  		return 0;
  
  	return ps->infix.string[ps->infix.tail];
  }
  
  static void infix_advance(struct filter_parse_state *ps)
  {
6b88f44e1   Steven Rostedt (Red Hat)   tracing/filter: D...
1052
1053
  	if (!ps->infix.cnt)
  		return;
8b3725621   Tom Zanussi   tracing/filters: ...
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  	ps->infix.cnt--;
  	ps->infix.tail++;
  }
  
  static inline int is_precedence_lower(struct filter_parse_state *ps,
  				      int a, int b)
  {
  	return ps->ops[a].precedence < ps->ops[b].precedence;
  }
  
  static inline int is_op_char(struct filter_parse_state *ps, char c)
  {
  	int i;
  
  	for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
  		if (ps->ops[i].string[0] == c)
  			return 1;
  	}
c4cff064b   Tom Zanussi   tracing/filters: ...
1072

0a19e53c1   Tom Zanussi   tracing/filters: ...
1073
  	return 0;
cfb180f3e   Tom Zanussi   tracing: add per-...
1074
  }
8b3725621   Tom Zanussi   tracing/filters: ...
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
  static int infix_get_op(struct filter_parse_state *ps, char firstc)
  {
  	char nextc = infix_peek(ps);
  	char opstr[3];
  	int i;
  
  	opstr[0] = firstc;
  	opstr[1] = nextc;
  	opstr[2] = '\0';
  
  	for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
  		if (!strcmp(opstr, ps->ops[i].string)) {
  			infix_advance(ps);
  			return ps->ops[i].id;
7ce7e4249   Tom Zanussi   tracing: add per-...
1089
  		}
8b3725621   Tom Zanussi   tracing/filters: ...
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
  	}
  
  	opstr[1] = '\0';
  
  	for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
  		if (!strcmp(opstr, ps->ops[i].string))
  			return ps->ops[i].id;
  	}
  
  	return OP_NONE;
  }
  
  static inline void clear_operand_string(struct filter_parse_state *ps)
  {
  	memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
  	ps->operand.tail = 0;
  }
  
  static inline int append_operand_char(struct filter_parse_state *ps, char c)
  {
5872144f6   Li Zefan   tracing/filters: ...
1110
  	if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
8b3725621   Tom Zanussi   tracing/filters: ...
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
  		return -EINVAL;
  
  	ps->operand.string[ps->operand.tail++] = c;
  
  	return 0;
  }
  
  static int filter_opstack_push(struct filter_parse_state *ps, int op)
  {
  	struct opstack_op *opstack_op;
  
  	opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
  	if (!opstack_op)
  		return -ENOMEM;
  
  	opstack_op->op = op;
  	list_add(&opstack_op->list, &ps->opstack);
  
  	return 0;
  }
  
  static int filter_opstack_empty(struct filter_parse_state *ps)
  {
  	return list_empty(&ps->opstack);
  }
  
  static int filter_opstack_top(struct filter_parse_state *ps)
  {
  	struct opstack_op *opstack_op;
  
  	if (filter_opstack_empty(ps))
  		return OP_NONE;
  
  	opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
  
  	return opstack_op->op;
  }
  
  static int filter_opstack_pop(struct filter_parse_state *ps)
  {
  	struct opstack_op *opstack_op;
  	int op;
  
  	if (filter_opstack_empty(ps))
  		return OP_NONE;
  
  	opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
  	op = opstack_op->op;
  	list_del(&opstack_op->list);
  
  	kfree(opstack_op);
  
  	return op;
  }
  
  static void filter_opstack_clear(struct filter_parse_state *ps)
  {
  	while (!filter_opstack_empty(ps))
  		filter_opstack_pop(ps);
  }
  
  static char *curr_operand(struct filter_parse_state *ps)
  {
  	return ps->operand.string;
  }
  
  static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
  {
  	struct postfix_elt *elt;
  
  	elt = kmalloc(sizeof(*elt), GFP_KERNEL);
  	if (!elt)
  		return -ENOMEM;
  
  	elt->op = OP_NONE;
  	elt->operand = kstrdup(operand, GFP_KERNEL);
  	if (!elt->operand) {
  		kfree(elt);
  		return -ENOMEM;
  	}
  
  	list_add_tail(&elt->list, &ps->postfix);
  
  	return 0;
  }
  
  static int postfix_append_op(struct filter_parse_state *ps, int op)
  {
  	struct postfix_elt *elt;
  
  	elt = kmalloc(sizeof(*elt), GFP_KERNEL);
  	if (!elt)
  		return -ENOMEM;
  
  	elt->op = op;
  	elt->operand = NULL;
  
  	list_add_tail(&elt->list, &ps->postfix);
  
  	return 0;
  }
  
  static void postfix_clear(struct filter_parse_state *ps)
  {
  	struct postfix_elt *elt;
  
  	while (!list_empty(&ps->postfix)) {
  		elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
8b3725621   Tom Zanussi   tracing/filters: ...
1219
  		list_del(&elt->list);
8ad807318   Li Zefan   tracing/filters: ...
1220
1221
  		kfree(elt->operand);
  		kfree(elt);
8b3725621   Tom Zanussi   tracing/filters: ...
1222
1223
1224
1225
1226
  	}
  }
  
  static int filter_parse(struct filter_parse_state *ps)
  {
5928c3cc0   Frederic Weisbecker   tracing/filters: ...
1227
  	int in_string = 0;
8b3725621   Tom Zanussi   tracing/filters: ...
1228
1229
1230
1231
  	int op, top_op;
  	char ch;
  
  	while ((ch = infix_next(ps))) {
5928c3cc0   Frederic Weisbecker   tracing/filters: ...
1232
1233
1234
1235
1236
1237
1238
  		if (ch == '"') {
  			in_string ^= 1;
  			continue;
  		}
  
  		if (in_string)
  			goto parse_operand;
8b3725621   Tom Zanussi   tracing/filters: ...
1239
1240
1241
1242
1243
1244
1245
  		if (isspace(ch))
  			continue;
  
  		if (is_op_char(ps, ch)) {
  			op = infix_get_op(ps, ch);
  			if (op == OP_NONE) {
  				parse_error(ps, FILT_ERR_INVALID_OP, 0);
7ce7e4249   Tom Zanussi   tracing: add per-...
1246
1247
  				return -EINVAL;
  			}
8b3725621   Tom Zanussi   tracing/filters: ...
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
  
  			if (strlen(curr_operand(ps))) {
  				postfix_append_operand(ps, curr_operand(ps));
  				clear_operand_string(ps);
  			}
  
  			while (!filter_opstack_empty(ps)) {
  				top_op = filter_opstack_top(ps);
  				if (!is_precedence_lower(ps, top_op, op)) {
  					top_op = filter_opstack_pop(ps);
  					postfix_append_op(ps, top_op);
  					continue;
  				}
  				break;
  			}
  
  			filter_opstack_push(ps, op);
7ce7e4249   Tom Zanussi   tracing: add per-...
1265
1266
  			continue;
  		}
8b3725621   Tom Zanussi   tracing/filters: ...
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
  
  		if (ch == '(') {
  			filter_opstack_push(ps, OP_OPEN_PAREN);
  			continue;
  		}
  
  		if (ch == ')') {
  			if (strlen(curr_operand(ps))) {
  				postfix_append_operand(ps, curr_operand(ps));
  				clear_operand_string(ps);
  			}
  
  			top_op = filter_opstack_pop(ps);
  			while (top_op != OP_NONE) {
  				if (top_op == OP_OPEN_PAREN)
  					break;
  				postfix_append_op(ps, top_op);
  				top_op = filter_opstack_pop(ps);
  			}
  			if (top_op == OP_NONE) {
  				parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
  				return -EINVAL;
7ce7e4249   Tom Zanussi   tracing: add per-...
1289
  			}
7ce7e4249   Tom Zanussi   tracing: add per-...
1290
1291
  			continue;
  		}
5928c3cc0   Frederic Weisbecker   tracing/filters: ...
1292
  parse_operand:
8b3725621   Tom Zanussi   tracing/filters: ...
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
  		if (append_operand_char(ps, ch)) {
  			parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
  			return -EINVAL;
  		}
  	}
  
  	if (strlen(curr_operand(ps)))
  		postfix_append_operand(ps, curr_operand(ps));
  
  	while (!filter_opstack_empty(ps)) {
  		top_op = filter_opstack_pop(ps);
  		if (top_op == OP_NONE)
  			break;
  		if (top_op == OP_OPEN_PAREN) {
  			parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
  			return -EINVAL;
  		}
  		postfix_append_op(ps, top_op);
  	}
  
  	return 0;
  }
81570d9ca   Jiri Olsa   tracing/filter: U...
1315
  static struct filter_pred *create_pred(struct filter_parse_state *ps,
2425bcb92   Steven Rostedt (Red Hat)   tracing: Rename f...
1316
  				       struct trace_event_call *call,
81570d9ca   Jiri Olsa   tracing/filter: U...
1317
  				       int op, char *operand1, char *operand2)
8b3725621   Tom Zanussi   tracing/filters: ...
1318
  {
61aaef553   Jiri Olsa   tracing/filter: R...
1319
  	struct ftrace_event_field *field;
81570d9ca   Jiri Olsa   tracing/filter: U...
1320
  	static struct filter_pred pred;
8b3725621   Tom Zanussi   tracing/filters: ...
1321

81570d9ca   Jiri Olsa   tracing/filter: U...
1322
1323
  	memset(&pred, 0, sizeof(pred));
  	pred.op = op;
8b3725621   Tom Zanussi   tracing/filters: ...
1324

81570d9ca   Jiri Olsa   tracing/filter: U...
1325
1326
1327
1328
1329
  	if (op == OP_AND || op == OP_OR)
  		return &pred;
  
  	if (!operand1 || !operand2) {
  		parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
8b3725621   Tom Zanussi   tracing/filters: ...
1330
1331
  		return NULL;
  	}
b3a8c6fd7   zhangwei(Jovi)   tracing: Move fin...
1332
  	field = trace_find_event_field(call, operand1);
61aaef553   Jiri Olsa   tracing/filter: R...
1333
1334
  	if (!field) {
  		parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
8b3725621   Tom Zanussi   tracing/filters: ...
1335
  		return NULL;
61aaef553   Jiri Olsa   tracing/filter: R...
1336
  	}
8b3725621   Tom Zanussi   tracing/filters: ...
1337

81570d9ca   Jiri Olsa   tracing/filter: U...
1338
1339
  	strcpy(pred.regex.pattern, operand2);
  	pred.regex.len = strlen(pred.regex.pattern);
1d0e78e38   Jiri Olsa   tracing/filter: A...
1340
  	pred.field = field;
61aaef553   Jiri Olsa   tracing/filter: R...
1341
  	return init_pred(ps, field, &pred) ? NULL : &pred;
8b3725621   Tom Zanussi   tracing/filters: ...
1342
1343
1344
1345
1346
1347
  }
  
  static int check_preds(struct filter_parse_state *ps)
  {
  	int n_normal_preds = 0, n_logical_preds = 0;
  	struct postfix_elt *elt;
2cf30dc18   Steven Rostedt   tracing: Have fil...
1348
  	int cnt = 0;
8b3725621   Tom Zanussi   tracing/filters: ...
1349
1350
  
  	list_for_each_entry(elt, &ps->postfix, list) {
2cf30dc18   Steven Rostedt   tracing: Have fil...
1351
1352
  		if (elt->op == OP_NONE) {
  			cnt++;
8b3725621   Tom Zanussi   tracing/filters: ...
1353
  			continue;
2cf30dc18   Steven Rostedt   tracing: Have fil...
1354
  		}
8b3725621   Tom Zanussi   tracing/filters: ...
1355
1356
1357
  
  		if (elt->op == OP_AND || elt->op == OP_OR) {
  			n_logical_preds++;
2cf30dc18   Steven Rostedt   tracing: Have fil...
1358
  			cnt--;
8b3725621   Tom Zanussi   tracing/filters: ...
1359
  			continue;
7ce7e4249   Tom Zanussi   tracing: add per-...
1360
  		}
2cf30dc18   Steven Rostedt   tracing: Have fil...
1361
1362
  		if (elt->op != OP_NOT)
  			cnt--;
8b3725621   Tom Zanussi   tracing/filters: ...
1363
  		n_normal_preds++;
b4875bbe7   Steven Rostedt (Red Hat)   tracing/filter: D...
1364
1365
1366
  		/* all ops should have operands */
  		if (cnt < 0)
  			break;
7ce7e4249   Tom Zanussi   tracing: add per-...
1367
  	}
2cf30dc18   Steven Rostedt   tracing: Have fil...
1368
  	if (cnt != 1 || !n_normal_preds || n_logical_preds >= n_normal_preds) {
8b3725621   Tom Zanussi   tracing/filters: ...
1369
  		parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
bcabd91c2   Li Zefan   tracing/filters: ...
1370
1371
  		return -EINVAL;
  	}
8b3725621   Tom Zanussi   tracing/filters: ...
1372
1373
  	return 0;
  }
f66578a76   Li Zefan   tracing/filters: ...
1374

c9c53ca03   Steven Rostedt   tracing/filter: D...
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
  static int count_preds(struct filter_parse_state *ps)
  {
  	struct postfix_elt *elt;
  	int n_preds = 0;
  
  	list_for_each_entry(elt, &ps->postfix, list) {
  		if (elt->op == OP_NONE)
  			continue;
  		n_preds++;
  	}
  
  	return n_preds;
  }
f03f59799   Jiri Olsa   tracing/filter: U...
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
  struct check_pred_data {
  	int count;
  	int max;
  };
  
  static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
  			      int *err, void *data)
  {
  	struct check_pred_data *d = data;
  
  	if (WARN_ON(d->count++ > d->max)) {
  		*err = -EINVAL;
  		return WALK_PRED_ABORT;
  	}
  	return WALK_PRED_DEFAULT;
  }
ec126cac2   Steven Rostedt   tracing/filter: C...
1404
1405
1406
1407
1408
1409
1410
1411
  /*
   * The tree is walked at filtering of an event. If the tree is not correctly
   * built, it may cause an infinite loop. Check here that the tree does
   * indeed terminate.
   */
  static int check_pred_tree(struct event_filter *filter,
  			   struct filter_pred *root)
  {
f03f59799   Jiri Olsa   tracing/filter: U...
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
  	struct check_pred_data data = {
  		/*
  		 * The max that we can hit a node is three times.
  		 * Once going down, once coming up from left, and
  		 * once coming up from right. This is more than enough
  		 * since leafs are only hit a single time.
  		 */
  		.max   = 3 * filter->n_preds,
  		.count = 0,
  	};
ec126cac2   Steven Rostedt   tracing/filter: C...
1422

f03f59799   Jiri Olsa   tracing/filter: U...
1423
1424
  	return walk_pred_tree(filter->preds, root,
  			      check_pred_tree_cb, &data);
ec126cac2   Steven Rostedt   tracing/filter: C...
1425
  }
c00b060f3   Jiri Olsa   tracing/filter: C...
1426
1427
  static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
  			  int *err, void *data)
43cd41455   Steven Rostedt   tracing/filter: O...
1428
  {
c00b060f3   Jiri Olsa   tracing/filter: C...
1429
  	int *count = data;
43cd41455   Steven Rostedt   tracing/filter: O...
1430

c00b060f3   Jiri Olsa   tracing/filter: C...
1431
1432
1433
  	if ((move == MOVE_DOWN) &&
  	    (pred->left == FILTER_PRED_INVALID))
  		(*count)++;
43cd41455   Steven Rostedt   tracing/filter: O...
1434

c00b060f3   Jiri Olsa   tracing/filter: C...
1435
1436
1437
1438
1439
1440
  	return WALK_PRED_DEFAULT;
  }
  
  static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
  {
  	int count = 0, ret;
43cd41455   Steven Rostedt   tracing/filter: O...
1441

c00b060f3   Jiri Olsa   tracing/filter: C...
1442
1443
  	ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
  	WARN_ON(ret);
43cd41455   Steven Rostedt   tracing/filter: O...
1444
1445
  	return count;
  }
96bc293a9   Jiri Olsa   tracing/filter: C...
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
  struct fold_pred_data {
  	struct filter_pred *root;
  	int count;
  	int children;
  };
  
  static int fold_pred_cb(enum move_type move, struct filter_pred *pred,
  			int *err, void *data)
  {
  	struct fold_pred_data *d = data;
  	struct filter_pred *root = d->root;
  
  	if (move != MOVE_DOWN)
  		return WALK_PRED_DEFAULT;
  	if (pred->left != FILTER_PRED_INVALID)
  		return WALK_PRED_DEFAULT;
  
  	if (WARN_ON(d->count == d->children)) {
  		*err = -EINVAL;
  		return WALK_PRED_ABORT;
  	}
  
  	pred->index &= ~FILTER_PRED_FOLD;
  	root->ops[d->count++] = pred->index;
  	return WALK_PRED_DEFAULT;
  }
43cd41455   Steven Rostedt   tracing/filter: O...
1472
1473
  static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
  {
96bc293a9   Jiri Olsa   tracing/filter: C...
1474
1475
1476
1477
  	struct fold_pred_data data = {
  		.root  = root,
  		.count = 0,
  	};
43cd41455   Steven Rostedt   tracing/filter: O...
1478
  	int children;
43cd41455   Steven Rostedt   tracing/filter: O...
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
  
  	/* No need to keep the fold flag */
  	root->index &= ~FILTER_PRED_FOLD;
  
  	/* If the root is a leaf then do nothing */
  	if (root->left == FILTER_PRED_INVALID)
  		return 0;
  
  	/* count the children */
  	children = count_leafs(preds, &preds[root->left]);
  	children += count_leafs(preds, &preds[root->right]);
47b0edcb5   Thomas Meyer   tracing/trivial: ...
1490
  	root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
43cd41455   Steven Rostedt   tracing/filter: O...
1491
1492
1493
1494
  	if (!root->ops)
  		return -ENOMEM;
  
  	root->val = children;
96bc293a9   Jiri Olsa   tracing/filter: C...
1495
1496
  	data.children = children;
  	return walk_pred_tree(preds, root, fold_pred_cb, &data);
43cd41455   Steven Rostedt   tracing/filter: O...
1497
  }
1b797fe5a   Jiri Olsa   tracing/filter: C...
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
  static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
  			     int *err, void *data)
  {
  	struct filter_pred *preds = data;
  
  	if (move != MOVE_DOWN)
  		return WALK_PRED_DEFAULT;
  	if (!(pred->index & FILTER_PRED_FOLD))
  		return WALK_PRED_DEFAULT;
  
  	*err = fold_pred(preds, pred);
  	if (*err)
  		return WALK_PRED_ABORT;
  
  	/* eveyrhing below is folded, continue with parent */
  	return WALK_PRED_PARENT;
  }
43cd41455   Steven Rostedt   tracing/filter: O...
1515
1516
1517
1518
1519
1520
1521
1522
  /*
   * To optimize the processing of the ops, if we have several "ors" or
   * "ands" together, we can put them in an array and process them all
   * together speeding up the filter logic.
   */
  static int fold_pred_tree(struct event_filter *filter,
  			   struct filter_pred *root)
  {
1b797fe5a   Jiri Olsa   tracing/filter: C...
1523
1524
  	return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
  			      filter->preds);
43cd41455   Steven Rostedt   tracing/filter: O...
1525
  }
2425bcb92   Steven Rostedt (Red Hat)   tracing: Rename f...
1526
  static int replace_preds(struct trace_event_call *call,
6fb2915df   Li Zefan   tracing/profile: ...
1527
  			 struct event_filter *filter,
8b3725621   Tom Zanussi   tracing/filters: ...
1528
  			 struct filter_parse_state *ps,
1f9963cbb   Li Zefan   tracing/filters: ...
1529
  			 bool dry_run)
8b3725621   Tom Zanussi   tracing/filters: ...
1530
1531
1532
  {
  	char *operand1 = NULL, *operand2 = NULL;
  	struct filter_pred *pred;
ec126cac2   Steven Rostedt   tracing/filter: C...
1533
  	struct filter_pred *root;
8b3725621   Tom Zanussi   tracing/filters: ...
1534
  	struct postfix_elt *elt;
61e9dea20   Steven Rostedt   tracing/filter: U...
1535
  	struct pred_stack stack = { }; /* init to NULL */
8b3725621   Tom Zanussi   tracing/filters: ...
1536
  	int err;
1f9963cbb   Li Zefan   tracing/filters: ...
1537
  	int n_preds = 0;
8b3725621   Tom Zanussi   tracing/filters: ...
1538

c9c53ca03   Steven Rostedt   tracing/filter: D...
1539
1540
1541
1542
1543
  	n_preds = count_preds(ps);
  	if (n_preds >= MAX_FILTER_PRED) {
  		parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
  		return -ENOSPC;
  	}
8b3725621   Tom Zanussi   tracing/filters: ...
1544
1545
1546
  	err = check_preds(ps);
  	if (err)
  		return err;
c9c53ca03   Steven Rostedt   tracing/filter: D...
1547
  	if (!dry_run) {
61e9dea20   Steven Rostedt   tracing/filter: U...
1548
  		err = __alloc_pred_stack(&stack, n_preds);
c9c53ca03   Steven Rostedt   tracing/filter: D...
1549
1550
  		if (err)
  			return err;
61e9dea20   Steven Rostedt   tracing/filter: U...
1551
1552
1553
  		err = __alloc_preds(filter, n_preds);
  		if (err)
  			goto fail;
c9c53ca03   Steven Rostedt   tracing/filter: D...
1554
1555
1556
  	}
  
  	n_preds = 0;
8b3725621   Tom Zanussi   tracing/filters: ...
1557
1558
1559
1560
1561
1562
1563
1564
  	list_for_each_entry(elt, &ps->postfix, list) {
  		if (elt->op == OP_NONE) {
  			if (!operand1)
  				operand1 = elt->operand;
  			else if (!operand2)
  				operand2 = elt->operand;
  			else {
  				parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
61e9dea20   Steven Rostedt   tracing/filter: U...
1565
1566
  				err = -EINVAL;
  				goto fail;
8b3725621   Tom Zanussi   tracing/filters: ...
1567
1568
1569
  			}
  			continue;
  		}
e12c09cf3   Steven Rostedt (Red Hat)   tracing: Add NOT ...
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
  		if (elt->op == OP_NOT) {
  			if (!n_preds || operand1 || operand2) {
  				parse_error(ps, FILT_ERR_ILLEGAL_NOT_OP, 0);
  				err = -EINVAL;
  				goto fail;
  			}
  			if (!dry_run)
  				filter->preds[n_preds - 1].not ^= 1;
  			continue;
  		}
c9c53ca03   Steven Rostedt   tracing/filter: D...
1580
  		if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1f9963cbb   Li Zefan   tracing/filters: ...
1581
  			parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
61e9dea20   Steven Rostedt   tracing/filter: U...
1582
1583
  			err = -ENOSPC;
  			goto fail;
1f9963cbb   Li Zefan   tracing/filters: ...
1584
  		}
9d96cd174   Jiri Olsa   tracing/filter: S...
1585
  		pred = create_pred(ps, call, elt->op, operand1, operand2);
61e9dea20   Steven Rostedt   tracing/filter: U...
1586
  		if (!pred) {
61aaef553   Jiri Olsa   tracing/filter: R...
1587
  			err = -EINVAL;
61e9dea20   Steven Rostedt   tracing/filter: U...
1588
1589
  			goto fail;
  		}
61aaef553   Jiri Olsa   tracing/filter: R...
1590

9d96cd174   Jiri Olsa   tracing/filter: S...
1591
1592
  		if (!dry_run) {
  			err = filter_add_pred(ps, filter, pred, &stack);
61aaef553   Jiri Olsa   tracing/filter: R...
1593
  			if (err)
9d96cd174   Jiri Olsa   tracing/filter: S...
1594
  				goto fail;
9d96cd174   Jiri Olsa   tracing/filter: S...
1595
  		}
8b3725621   Tom Zanussi   tracing/filters: ...
1596
1597
1598
  
  		operand1 = operand2 = NULL;
  	}
7ce7e4249   Tom Zanussi   tracing: add per-...
1599

61e9dea20   Steven Rostedt   tracing/filter: U...
1600
1601
1602
1603
1604
1605
  	if (!dry_run) {
  		/* We should have one item left on the stack */
  		pred = __pop_pred_stack(&stack);
  		if (!pred)
  			return -EINVAL;
  		/* This item is where we start from in matching */
ec126cac2   Steven Rostedt   tracing/filter: C...
1606
  		root = pred;
61e9dea20   Steven Rostedt   tracing/filter: U...
1607
1608
1609
1610
1611
1612
1613
  		/* Make sure the stack is empty */
  		pred = __pop_pred_stack(&stack);
  		if (WARN_ON(pred)) {
  			err = -EINVAL;
  			filter->root = NULL;
  			goto fail;
  		}
ec126cac2   Steven Rostedt   tracing/filter: C...
1614
1615
1616
  		err = check_pred_tree(filter, root);
  		if (err)
  			goto fail;
43cd41455   Steven Rostedt   tracing/filter: O...
1617
1618
1619
1620
  		/* Optimize the tree */
  		err = fold_pred_tree(filter, root);
  		if (err)
  			goto fail;
ec126cac2   Steven Rostedt   tracing/filter: C...
1621
1622
1623
  		/* We don't set root until we know it works */
  		barrier();
  		filter->root = root;
61e9dea20   Steven Rostedt   tracing/filter: U...
1624
1625
1626
1627
1628
1629
  	}
  
  	err = 0;
  fail:
  	__free_pred_stack(&stack);
  	return err;
7ce7e4249   Tom Zanussi   tracing: add per-...
1630
  }
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1631
  static inline void event_set_filtered_flag(struct trace_event_file *file)
f306cc82a   Tom Zanussi   tracing: Update e...
1632
  {
0fc1b09ff   Steven Rostedt (Red Hat)   tracing: Use temp...
1633
  	unsigned long old_flags = file->flags;
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
1634
  	file->flags |= EVENT_FILE_FL_FILTERED;
0fc1b09ff   Steven Rostedt (Red Hat)   tracing: Use temp...
1635
1636
1637
  
  	if (old_flags != file->flags)
  		trace_buffered_event_enable();
f306cc82a   Tom Zanussi   tracing: Update e...
1638
  }
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1639
  static inline void event_set_filter(struct trace_event_file *file,
f306cc82a   Tom Zanussi   tracing: Update e...
1640
1641
  				    struct event_filter *filter)
  {
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
1642
  	rcu_assign_pointer(file->filter, filter);
f306cc82a   Tom Zanussi   tracing: Update e...
1643
  }
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1644
  static inline void event_clear_filter(struct trace_event_file *file)
f306cc82a   Tom Zanussi   tracing: Update e...
1645
  {
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
1646
  	RCU_INIT_POINTER(file->filter, NULL);
f306cc82a   Tom Zanussi   tracing: Update e...
1647
1648
1649
  }
  
  static inline void
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1650
  event_set_no_set_filter_flag(struct trace_event_file *file)
f306cc82a   Tom Zanussi   tracing: Update e...
1651
  {
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
1652
  	file->flags |= EVENT_FILE_FL_NO_SET_FILTER;
f306cc82a   Tom Zanussi   tracing: Update e...
1653
1654
1655
  }
  
  static inline void
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1656
  event_clear_no_set_filter_flag(struct trace_event_file *file)
f306cc82a   Tom Zanussi   tracing: Update e...
1657
  {
dcb0b5575   Steven Rostedt (Red Hat)   tracing: Remove T...
1658
  	file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER;
f306cc82a   Tom Zanussi   tracing: Update e...
1659
1660
1661
  }
  
  static inline bool
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1662
  event_no_set_filter_flag(struct trace_event_file *file)
f306cc82a   Tom Zanussi   tracing: Update e...
1663
  {
5d6ad960a   Steven Rostedt (Red Hat)   tracing: Rename F...
1664
  	if (file->flags & EVENT_FILE_FL_NO_SET_FILTER)
f306cc82a   Tom Zanussi   tracing: Update e...
1665
  		return true;
f306cc82a   Tom Zanussi   tracing: Update e...
1666
1667
  	return false;
  }
75b8e9826   Steven Rostedt   tracing/filter: S...
1668
1669
1670
1671
  struct filter_list {
  	struct list_head	list;
  	struct event_filter	*filter;
  };
7967b3e0c   Steven Rostedt (Red Hat)   tracing: Rename s...
1672
  static int replace_system_preds(struct trace_subsystem_dir *dir,
f306cc82a   Tom Zanussi   tracing: Update e...
1673
  				struct trace_array *tr,
fce29d15b   Li Zefan   tracing/filters: ...
1674
1675
1676
  				struct filter_parse_state *ps,
  				char *filter_string)
  {
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1677
  	struct trace_event_file *file;
75b8e9826   Steven Rostedt   tracing/filter: S...
1678
1679
1680
  	struct filter_list *filter_item;
  	struct filter_list *tmp;
  	LIST_HEAD(filter_list);
fce29d15b   Li Zefan   tracing/filters: ...
1681
  	bool fail = true;
a66abe7fb   Ingo Molnar   tracing/events: F...
1682
  	int err;
fce29d15b   Li Zefan   tracing/filters: ...
1683

f306cc82a   Tom Zanussi   tracing: Update e...
1684
  	list_for_each_entry(file, &tr->events, list) {
bb9ef1cb7   Oleg Nesterov   tracing: Change a...
1685
  		if (file->system != dir)
fce29d15b   Li Zefan   tracing/filters: ...
1686
  			continue;
75b8e9826   Steven Rostedt   tracing/filter: S...
1687
1688
1689
1690
  		/*
  		 * Try to see if the filter can be applied
  		 *  (filter arg is ignored on dry_run)
  		 */
6355d5443   Oleg Nesterov   tracing: Kill "fi...
1691
  		err = replace_preds(file->event_call, NULL, ps, true);
fce29d15b   Li Zefan   tracing/filters: ...
1692
  		if (err)
f306cc82a   Tom Zanussi   tracing: Update e...
1693
  			event_set_no_set_filter_flag(file);
ed0449af5   Li Zefan   tracing: Restore ...
1694
  		else
f306cc82a   Tom Zanussi   tracing: Update e...
1695
  			event_clear_no_set_filter_flag(file);
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1696
  	}
f306cc82a   Tom Zanussi   tracing: Update e...
1697
  	list_for_each_entry(file, &tr->events, list) {
75b8e9826   Steven Rostedt   tracing/filter: S...
1698
  		struct event_filter *filter;
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1699

bb9ef1cb7   Oleg Nesterov   tracing: Change a...
1700
  		if (file->system != dir)
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1701
  			continue;
f306cc82a   Tom Zanussi   tracing: Update e...
1702
  		if (event_no_set_filter_flag(file))
ed0449af5   Li Zefan   tracing: Restore ...
1703
  			continue;
75b8e9826   Steven Rostedt   tracing/filter: S...
1704
1705
1706
  		filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
  		if (!filter_item)
  			goto fail_mem;
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1707

75b8e9826   Steven Rostedt   tracing/filter: S...
1708
  		list_add_tail(&filter_item->list, &filter_list);
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1709

75b8e9826   Steven Rostedt   tracing/filter: S...
1710
1711
1712
1713
  		filter_item->filter = __alloc_filter();
  		if (!filter_item->filter)
  			goto fail_mem;
  		filter = filter_item->filter;
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1714

75b8e9826   Steven Rostedt   tracing/filter: S...
1715
1716
1717
1718
  		/* Can only fail on no memory */
  		err = replace_filter_string(filter, filter_string);
  		if (err)
  			goto fail_mem;
fce29d15b   Li Zefan   tracing/filters: ...
1719

6355d5443   Oleg Nesterov   tracing: Kill "fi...
1720
  		err = replace_preds(file->event_call, filter, ps, false);
75b8e9826   Steven Rostedt   tracing/filter: S...
1721
  		if (err) {
f306cc82a   Tom Zanussi   tracing: Update e...
1722
  			filter_disable(file);
75b8e9826   Steven Rostedt   tracing/filter: S...
1723
1724
1725
  			parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
  			append_filter_err(ps, filter);
  		} else
f306cc82a   Tom Zanussi   tracing: Update e...
1726
  			event_set_filtered_flag(file);
75b8e9826   Steven Rostedt   tracing/filter: S...
1727
1728
1729
1730
  		/*
  		 * Regardless of if this returned an error, we still
  		 * replace the filter for the call.
  		 */
f306cc82a   Tom Zanussi   tracing: Update e...
1731
1732
  		filter = event_filter(file);
  		event_set_filter(file, filter_item->filter);
75b8e9826   Steven Rostedt   tracing/filter: S...
1733
  		filter_item->filter = filter;
fce29d15b   Li Zefan   tracing/filters: ...
1734
1735
  		fail = false;
  	}
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1736
1737
  	if (fail)
  		goto fail;
75b8e9826   Steven Rostedt   tracing/filter: S...
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
  	/*
  	 * The calls can still be using the old filters.
  	 * Do a synchronize_sched() to ensure all calls are
  	 * done with them before we free them.
  	 */
  	synchronize_sched();
  	list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
  		__free_filter(filter_item->filter);
  		list_del(&filter_item->list);
  		kfree(filter_item);
  	}
fce29d15b   Li Zefan   tracing/filters: ...
1749
  	return 0;
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1750
   fail:
75b8e9826   Steven Rostedt   tracing/filter: S...
1751
1752
1753
1754
1755
  	/* No call succeeded */
  	list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
  		list_del(&filter_item->list);
  		kfree(filter_item);
  	}
0fc3ca9a1   Steven Rostedt   tracing/filter: C...
1756
1757
  	parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
  	return -EINVAL;
75b8e9826   Steven Rostedt   tracing/filter: S...
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
   fail_mem:
  	/* If any call succeeded, we still need to sync */
  	if (!fail)
  		synchronize_sched();
  	list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
  		__free_filter(filter_item->filter);
  		list_del(&filter_item->list);
  		kfree(filter_item);
  	}
  	return -ENOMEM;
fce29d15b   Li Zefan   tracing/filters: ...
1768
  }
38b78eb85   Tejun Heo   tracing: Factoriz...
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
  static int create_filter_start(char *filter_str, bool set_str,
  			       struct filter_parse_state **psp,
  			       struct event_filter **filterp)
  {
  	struct event_filter *filter;
  	struct filter_parse_state *ps = NULL;
  	int err = 0;
  
  	WARN_ON_ONCE(*psp || *filterp);
  
  	/* allocate everything, and if any fails, free all and fail */
  	filter = __alloc_filter();
  	if (filter && set_str)
  		err = replace_filter_string(filter, filter_str);
  
  	ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  
  	if (!filter || !ps || err) {
  		kfree(ps);
  		__free_filter(filter);
  		return -ENOMEM;
  	}
  
  	/* we're committed to creating a new filter */
  	*filterp = filter;
  	*psp = ps;
  
  	parse_init(ps, filter_ops, filter_str);
  	err = filter_parse(ps);
  	if (err && set_str)
  		append_filter_err(ps, filter);
  	return err;
  }
  
  static void create_filter_finish(struct filter_parse_state *ps)
  {
  	if (ps) {
  		filter_opstack_clear(ps);
  		postfix_clear(ps);
  		kfree(ps);
  	}
  }
  
  /**
2425bcb92   Steven Rostedt (Red Hat)   tracing: Rename f...
1813
1814
   * create_filter - create a filter for a trace_event_call
   * @call: trace_event_call to create a filter for
38b78eb85   Tejun Heo   tracing: Factoriz...
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
   * @filter_str: filter string
   * @set_str: remember @filter_str and enable detailed error in filter
   * @filterp: out param for created filter (always updated on return)
   *
   * Creates a filter for @call with @filter_str.  If @set_str is %true,
   * @filter_str is copied and recorded in the new filter.
   *
   * On success, returns 0 and *@filterp points to the new filter.  On
   * failure, returns -errno and *@filterp may point to %NULL or to a new
   * filter.  In the latter case, the returned filter contains error
   * information if @set_str is %true and the caller is responsible for
   * freeing it.
   */
2425bcb92   Steven Rostedt (Red Hat)   tracing: Rename f...
1828
  static int create_filter(struct trace_event_call *call,
38b78eb85   Tejun Heo   tracing: Factoriz...
1829
1830
1831
1832
1833
1834
1835
1836
1837
  			 char *filter_str, bool set_str,
  			 struct event_filter **filterp)
  {
  	struct event_filter *filter = NULL;
  	struct filter_parse_state *ps = NULL;
  	int err;
  
  	err = create_filter_start(filter_str, set_str, &ps, &filter);
  	if (!err) {
6355d5443   Oleg Nesterov   tracing: Kill "fi...
1838
  		err = replace_preds(call, filter, ps, false);
38b78eb85   Tejun Heo   tracing: Factoriz...
1839
1840
1841
1842
1843
1844
1845
1846
  		if (err && set_str)
  			append_filter_err(ps, filter);
  	}
  	create_filter_finish(ps);
  
  	*filterp = filter;
  	return err;
  }
2425bcb92   Steven Rostedt (Red Hat)   tracing: Rename f...
1847
  int create_event_filter(struct trace_event_call *call,
bac5fb97a   Tom Zanussi   tracing: Add and ...
1848
1849
1850
1851
1852
  			char *filter_str, bool set_str,
  			struct event_filter **filterp)
  {
  	return create_filter(call, filter_str, set_str, filterp);
  }
38b78eb85   Tejun Heo   tracing: Factoriz...
1853
1854
1855
1856
1857
1858
1859
1860
1861
  /**
   * create_system_filter - create a filter for an event_subsystem
   * @system: event_subsystem to create a filter for
   * @filter_str: filter string
   * @filterp: out param for created filter (always updated on return)
   *
   * Identical to create_filter() except that it creates a subsystem filter
   * and always remembers @filter_str.
   */
7967b3e0c   Steven Rostedt (Red Hat)   tracing: Rename s...
1862
  static int create_system_filter(struct trace_subsystem_dir *dir,
f306cc82a   Tom Zanussi   tracing: Update e...
1863
  				struct trace_array *tr,
38b78eb85   Tejun Heo   tracing: Factoriz...
1864
1865
1866
1867
1868
1869
1870
1871
  				char *filter_str, struct event_filter **filterp)
  {
  	struct event_filter *filter = NULL;
  	struct filter_parse_state *ps = NULL;
  	int err;
  
  	err = create_filter_start(filter_str, true, &ps, &filter);
  	if (!err) {
bb9ef1cb7   Oleg Nesterov   tracing: Change a...
1872
  		err = replace_system_preds(dir, tr, ps, filter_str);
38b78eb85   Tejun Heo   tracing: Factoriz...
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
  		if (!err) {
  			/* System filters just show a default message */
  			kfree(filter->filter_string);
  			filter->filter_string = NULL;
  		} else {
  			append_filter_err(ps, filter);
  		}
  	}
  	create_filter_finish(ps);
  
  	*filterp = filter;
  	return err;
  }
e2912b091   Oleg Nesterov   tracing: Change e...
1886
  /* caller must hold event_mutex */
7f1d2f821   Steven Rostedt (Red Hat)   tracing: Rename f...
1887
  int apply_event_filter(struct trace_event_file *file, char *filter_string)
8b3725621   Tom Zanussi   tracing/filters: ...
1888
  {
2425bcb92   Steven Rostedt (Red Hat)   tracing: Rename f...
1889
  	struct trace_event_call *call = file->event_call;
75b8e9826   Steven Rostedt   tracing/filter: S...
1890
  	struct event_filter *filter;
e2912b091   Oleg Nesterov   tracing: Change e...
1891
  	int err;
8b3725621   Tom Zanussi   tracing/filters: ...
1892
1893
  
  	if (!strcmp(strstrip(filter_string), "0")) {
f306cc82a   Tom Zanussi   tracing: Update e...
1894
1895
  		filter_disable(file);
  		filter = event_filter(file);
75b8e9826   Steven Rostedt   tracing/filter: S...
1896
  		if (!filter)
e2912b091   Oleg Nesterov   tracing: Change e...
1897
  			return 0;
f306cc82a   Tom Zanussi   tracing: Update e...
1898
1899
  
  		event_clear_filter(file);
f76690afd   Steven Rostedt   tracing/filter: F...
1900
1901
  		/* Make sure the filter is not being used */
  		synchronize_sched();
75b8e9826   Steven Rostedt   tracing/filter: S...
1902
  		__free_filter(filter);
f306cc82a   Tom Zanussi   tracing: Update e...
1903

e2912b091   Oleg Nesterov   tracing: Change e...
1904
  		return 0;
8b3725621   Tom Zanussi   tracing/filters: ...
1905
  	}
38b78eb85   Tejun Heo   tracing: Factoriz...
1906
  	err = create_filter(call, filter_string, true, &filter);
8b3725621   Tom Zanussi   tracing/filters: ...
1907

75b8e9826   Steven Rostedt   tracing/filter: S...
1908
1909
1910
1911
1912
1913
  	/*
  	 * Always swap the call filter with the new filter
  	 * even if there was an error. If there was an error
  	 * in the filter, we disable the filter and show the error
  	 * string
  	 */
38b78eb85   Tejun Heo   tracing: Factoriz...
1914
  	if (filter) {
f306cc82a   Tom Zanussi   tracing: Update e...
1915
  		struct event_filter *tmp;
38b78eb85   Tejun Heo   tracing: Factoriz...
1916

f306cc82a   Tom Zanussi   tracing: Update e...
1917
  		tmp = event_filter(file);
38b78eb85   Tejun Heo   tracing: Factoriz...
1918
  		if (!err)
f306cc82a   Tom Zanussi   tracing: Update e...
1919
  			event_set_filtered_flag(file);
38b78eb85   Tejun Heo   tracing: Factoriz...
1920
  		else
f306cc82a   Tom Zanussi   tracing: Update e...
1921
  			filter_disable(file);
38b78eb85   Tejun Heo   tracing: Factoriz...
1922

f306cc82a   Tom Zanussi   tracing: Update e...
1923
  		event_set_filter(file, filter);
38b78eb85   Tejun Heo   tracing: Factoriz...
1924
1925
1926
1927
1928
1929
  
  		if (tmp) {
  			/* Make sure the call is done with the filter */
  			synchronize_sched();
  			__free_filter(tmp);
  		}
75b8e9826   Steven Rostedt   tracing/filter: S...
1930
  	}
8b3725621   Tom Zanussi   tracing/filters: ...
1931
1932
1933
  
  	return err;
  }
7967b3e0c   Steven Rostedt (Red Hat)   tracing: Rename s...
1934
  int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
8b3725621   Tom Zanussi   tracing/filters: ...
1935
1936
  				 char *filter_string)
  {
ae63b31e4   Steven Rostedt   tracing: Separate...
1937
  	struct event_subsystem *system = dir->subsystem;
f306cc82a   Tom Zanussi   tracing: Update e...
1938
  	struct trace_array *tr = dir->tr;
75b8e9826   Steven Rostedt   tracing/filter: S...
1939
1940
  	struct event_filter *filter;
  	int err = 0;
8b3725621   Tom Zanussi   tracing/filters: ...
1941

00e95830a   Li Zefan   tracing/filters: ...
1942
  	mutex_lock(&event_mutex);
8b3725621   Tom Zanussi   tracing/filters: ...
1943

e9dbfae53   Steven Rostedt   tracing: Fix bug ...
1944
  	/* Make sure the system still has events */
ae63b31e4   Steven Rostedt   tracing: Separate...
1945
  	if (!dir->nr_events) {
e9dbfae53   Steven Rostedt   tracing: Fix bug ...
1946
1947
1948
  		err = -ENODEV;
  		goto out_unlock;
  	}
8b3725621   Tom Zanussi   tracing/filters: ...
1949
  	if (!strcmp(strstrip(filter_string), "0")) {
bb9ef1cb7   Oleg Nesterov   tracing: Change a...
1950
  		filter_free_subsystem_preds(dir, tr);
8b3725621   Tom Zanussi   tracing/filters: ...
1951
  		remove_filter_string(system->filter);
75b8e9826   Steven Rostedt   tracing/filter: S...
1952
1953
1954
1955
  		filter = system->filter;
  		system->filter = NULL;
  		/* Ensure all filters are no longer used */
  		synchronize_sched();
bb9ef1cb7   Oleg Nesterov   tracing: Change a...
1956
  		filter_free_subsystem_filters(dir, tr);
75b8e9826   Steven Rostedt   tracing/filter: S...
1957
  		__free_filter(filter);
a66abe7fb   Ingo Molnar   tracing/events: F...
1958
  		goto out_unlock;
8b3725621   Tom Zanussi   tracing/filters: ...
1959
  	}
bb9ef1cb7   Oleg Nesterov   tracing: Change a...
1960
  	err = create_system_filter(dir, tr, filter_string, &filter);
38b78eb85   Tejun Heo   tracing: Factoriz...
1961
1962
1963
1964
1965
1966
1967
1968
  	if (filter) {
  		/*
  		 * No event actually uses the system filter
  		 * we can free it without synchronize_sched().
  		 */
  		__free_filter(system->filter);
  		system->filter = filter;
  	}
8cd995b6d   Li Zefan   tracing/filters: ...
1969
  out_unlock:
00e95830a   Li Zefan   tracing/filters: ...
1970
  	mutex_unlock(&event_mutex);
8b3725621   Tom Zanussi   tracing/filters: ...
1971
1972
1973
  
  	return err;
  }
7ce7e4249   Tom Zanussi   tracing: add per-...
1974

07b139c8c   Li Zefan   perf events: Remo...
1975
  #ifdef CONFIG_PERF_EVENTS
6fb2915df   Li Zefan   tracing/profile: ...
1976
1977
1978
1979
1980
1981
  
  void ftrace_profile_free_filter(struct perf_event *event)
  {
  	struct event_filter *filter = event->filter;
  
  	event->filter = NULL;
c9c53ca03   Steven Rostedt   tracing/filter: D...
1982
  	__free_filter(filter);
6fb2915df   Li Zefan   tracing/profile: ...
1983
  }
5500fa511   Jiri Olsa   ftrace, perf: Add...
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
  struct function_filter_data {
  	struct ftrace_ops *ops;
  	int first_filter;
  	int first_notrace;
  };
  
  #ifdef CONFIG_FUNCTION_TRACER
  static char **
  ftrace_function_filter_re(char *buf, int len, int *count)
  {
1bb564718   Rasmus Villemoes   kernel/trace/trac...
1994
  	char *str, **re;
5500fa511   Jiri Olsa   ftrace, perf: Add...
1995
1996
1997
1998
1999
2000
2001
2002
2003
  
  	str = kstrndup(buf, len, GFP_KERNEL);
  	if (!str)
  		return NULL;
  
  	/*
  	 * The argv_split function takes white space
  	 * as a separator, so convert ',' into spaces.
  	 */
1bb564718   Rasmus Villemoes   kernel/trace/trac...
2004
  	strreplace(str, ',', ' ');
5500fa511   Jiri Olsa   ftrace, perf: Add...
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
  
  	re = argv_split(GFP_KERNEL, str, count);
  	kfree(str);
  	return re;
  }
  
  static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
  				      int reset, char *re, int len)
  {
  	int ret;
  
  	if (filter)
  		ret = ftrace_set_filter(ops, re, len, reset);
  	else
  		ret = ftrace_set_notrace(ops, re, len, reset);
  
  	return ret;
  }
  
  static int __ftrace_function_set_filter(int filter, char *buf, int len,
  					struct function_filter_data *data)
  {
92d8d4a8b   Jiri Olsa   tracing/filter: A...
2027
  	int i, re_cnt, ret = -EINVAL;
5500fa511   Jiri Olsa   ftrace, perf: Add...
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
  	int *reset;
  	char **re;
  
  	reset = filter ? &data->first_filter : &data->first_notrace;
  
  	/*
  	 * The 'ip' field could have multiple filters set, separated
  	 * either by space or comma. We first cut the filter and apply
  	 * all pieces separatelly.
  	 */
  	re = ftrace_function_filter_re(buf, len, &re_cnt);
  	if (!re)
  		return -EINVAL;
  
  	for (i = 0; i < re_cnt; i++) {
  		ret = ftrace_function_set_regexp(data->ops, filter, *reset,
  						 re[i], strlen(re[i]));
  		if (ret)
  			break;
  
  		if (*reset)
  			*reset = 0;
  	}
  
  	argv_free(re);
  	return ret;
  }
  
  static int ftrace_function_check_pred(struct filter_pred *pred, int leaf)
  {
  	struct ftrace_event_field *field = pred->field;
  
  	if (leaf) {
  		/*
  		 * Check the leaf predicate for function trace, verify:
  		 *  - only '==' and '!=' is used
  		 *  - the 'ip' field is used
  		 */
  		if ((pred->op != OP_EQ) && (pred->op != OP_NE))
  			return -EINVAL;
  
  		if (strcmp(field->name, "ip"))
  			return -EINVAL;
  	} else {
  		/*
  		 * Check the non leaf predicate for function trace, verify:
  		 *  - only '||' is used
  		*/
  		if (pred->op != OP_OR)
  			return -EINVAL;
  	}
  
  	return 0;
  }
  
  static int ftrace_function_set_filter_cb(enum move_type move,
  					 struct filter_pred *pred,
  					 int *err, void *data)
  {
  	/* Checking the node is valid for function trace. */
  	if ((move != MOVE_DOWN) ||
  	    (pred->left != FILTER_PRED_INVALID)) {
  		*err = ftrace_function_check_pred(pred, 0);
  	} else {
  		*err = ftrace_function_check_pred(pred, 1);
  		if (*err)
  			return WALK_PRED_ABORT;
  
  		*err = __ftrace_function_set_filter(pred->op == OP_EQ,
  						    pred->regex.pattern,
  						    pred->regex.len,
  						    data);
  	}
  
  	return (*err) ? WALK_PRED_ABORT : WALK_PRED_DEFAULT;
  }
  
  static int ftrace_function_set_filter(struct perf_event *event,
  				      struct event_filter *filter)
  {
  	struct function_filter_data data = {
  		.first_filter  = 1,
  		.first_notrace = 1,
  		.ops           = &event->ftrace_ops,
  	};
  
  	return walk_pred_tree(filter->preds, filter->root,
  			      ftrace_function_set_filter_cb, &data);
  }
  #else
  static int ftrace_function_set_filter(struct perf_event *event,
  				      struct event_filter *filter)
  {
  	return -ENODEV;
  }
  #endif /* CONFIG_FUNCTION_TRACER */
6fb2915df   Li Zefan   tracing/profile: ...
2124
2125
2126
2127
2128
  int ftrace_profile_set_filter(struct perf_event *event, int event_id,
  			      char *filter_str)
  {
  	int err;
  	struct event_filter *filter;
2425bcb92   Steven Rostedt (Red Hat)   tracing: Rename f...
2129
  	struct trace_event_call *call;
6fb2915df   Li Zefan   tracing/profile: ...
2130
2131
  
  	mutex_lock(&event_mutex);
3f78f935e   Jiri Olsa   tracing/filter: S...
2132
  	call = event->tp_event;
a66abe7fb   Ingo Molnar   tracing/events: F...
2133
2134
  
  	err = -EINVAL;
3f78f935e   Jiri Olsa   tracing/filter: S...
2135
  	if (!call)
a66abe7fb   Ingo Molnar   tracing/events: F...
2136
  		goto out_unlock;
6fb2915df   Li Zefan   tracing/profile: ...
2137

a66abe7fb   Ingo Molnar   tracing/events: F...
2138
  	err = -EEXIST;
6fb2915df   Li Zefan   tracing/profile: ...
2139
  	if (event->filter)
a66abe7fb   Ingo Molnar   tracing/events: F...
2140
  		goto out_unlock;
6fb2915df   Li Zefan   tracing/profile: ...
2141

38b78eb85   Tejun Heo   tracing: Factoriz...
2142
  	err = create_filter(call, filter_str, false, &filter);
5500fa511   Jiri Olsa   ftrace, perf: Add...
2143
2144
2145
2146
2147
  	if (err)
  		goto free_filter;
  
  	if (ftrace_event_is_function(call))
  		err = ftrace_function_set_filter(event, filter);
38b78eb85   Tejun Heo   tracing: Factoriz...
2148
  	else
5500fa511   Jiri Olsa   ftrace, perf: Add...
2149
2150
2151
2152
  		event->filter = filter;
  
  free_filter:
  	if (err || ftrace_event_is_function(call))
c9c53ca03   Steven Rostedt   tracing/filter: D...
2153
  		__free_filter(filter);
6fb2915df   Li Zefan   tracing/profile: ...
2154

a66abe7fb   Ingo Molnar   tracing/events: F...
2155
  out_unlock:
6fb2915df   Li Zefan   tracing/profile: ...
2156
2157
2158
2159
  	mutex_unlock(&event_mutex);
  
  	return err;
  }
07b139c8c   Li Zefan   perf events: Remo...
2160
  #endif /* CONFIG_PERF_EVENTS */
6fb2915df   Li Zefan   tracing/profile: ...
2161

1d0e78e38   Jiri Olsa   tracing/filter: A...
2162
2163
2164
2165
2166
2167
2168
  #ifdef CONFIG_FTRACE_STARTUP_TEST
  
  #include <linux/types.h>
  #include <linux/tracepoint.h>
  
  #define CREATE_TRACE_POINTS
  #include "trace_events_filter_test.h"
1d0e78e38   Jiri Olsa   tracing/filter: A...
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
  #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
  { \
  	.filter = FILTER, \
  	.rec    = { .a = va, .b = vb, .c = vc, .d = vd, \
  		    .e = ve, .f = vf, .g = vg, .h = vh }, \
  	.match  = m, \
  	.not_visited = nvisit, \
  }
  #define YES 1
  #define NO  0
  
  static struct test_filter_data_t {
  	char *filter;
a72377657   Steven Rostedt (Red Hat)   tracing: Rename f...
2182
  	struct trace_event_raw_ftrace_test_filter rec;
1d0e78e38   Jiri Olsa   tracing/filter: A...
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
  	int match;
  	char *not_visited;
  } test_filter_data[] = {
  #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
  	       "e == 1 && f == 1 && g == 1 && h == 1"
  	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
  	DATA_REC(NO,  0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
  	DATA_REC(NO,  1, 1, 1, 1, 1, 1, 1, 0, ""),
  #undef FILTER
  #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
  	       "e == 1 || f == 1 || g == 1 || h == 1"
  	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""),
  	DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
  	DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
  #undef FILTER
  #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
  	       "(e == 1 || f == 1) && (g == 1 || h == 1)"
  	DATA_REC(NO,  0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
  	DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
  	DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
  	DATA_REC(NO,  1, 0, 1, 0, 0, 1, 0, 0, "bd"),
  #undef FILTER
  #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
  	       "(e == 1 && f == 1) || (g == 1 && h == 1)"
  	DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
  	DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
  	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""),
  #undef FILTER
  #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
  	       "(e == 1 && f == 1) || (g == 1 && h == 1)"
  	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
  	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""),
  	DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
  #undef FILTER
  #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
  	       "(e == 1 || f == 1)) && (g == 1 || h == 1)"
  	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
  	DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""),
  	DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
  #undef FILTER
  #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
  	       "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
  	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
  	DATA_REC(NO,  0, 1, 0, 1, 0, 1, 0, 1, ""),
  	DATA_REC(NO,  1, 0, 1, 0, 1, 0, 1, 0, ""),
  #undef FILTER
  #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
  	       "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
  	DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
  	DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
  	DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
  };
  
  #undef DATA_REC
  #undef FILTER
  #undef YES
  #undef NO
  
  #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
  
  static int test_pred_visited;
  
  static int test_pred_visited_fn(struct filter_pred *pred, void *event)
  {
  	struct ftrace_event_field *field = pred->field;
  
  	test_pred_visited = 1;
  	printk(KERN_INFO "
  pred visited %s
  ", field->name);
  	return 1;
  }
  
  static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred,
  			     int *err, void *data)
  {
  	char *fields = data;
  
  	if ((move == MOVE_DOWN) &&
  	    (pred->left == FILTER_PRED_INVALID)) {
  		struct ftrace_event_field *field = pred->field;
  
  		if (!field) {
  			WARN(1, "all leafs should have field defined");
  			return WALK_PRED_DEFAULT;
  		}
  		if (!strchr(fields, *field->name))
  			return WALK_PRED_DEFAULT;
  
  		WARN_ON(!pred->fn);
  		pred->fn = test_pred_visited_fn;
  	}
  	return WALK_PRED_DEFAULT;
  }
  
  static __init int ftrace_test_event_filter(void)
  {
  	int i;
  
  	printk(KERN_INFO "Testing ftrace filter: ");
  
  	for (i = 0; i < DATA_CNT; i++) {
  		struct event_filter *filter = NULL;
  		struct test_filter_data_t *d = &test_filter_data[i];
  		int err;
38b78eb85   Tejun Heo   tracing: Factoriz...
2288
2289
  		err = create_filter(&event_ftrace_test_filter, d->filter,
  				    false, &filter);
1d0e78e38   Jiri Olsa   tracing/filter: A...
2290
2291
2292
2293
2294
  		if (err) {
  			printk(KERN_INFO
  			       "Failed to get filter for '%s', err %d
  ",
  			       d->filter, err);
38b78eb85   Tejun Heo   tracing: Factoriz...
2295
  			__free_filter(filter);
1d0e78e38   Jiri Olsa   tracing/filter: A...
2296
2297
  			break;
  		}
86b6ef21b   Steven Rostedt   tracing: Add pree...
2298
2299
2300
2301
2302
  		/*
  		 * The preemption disabling is not really needed for self
  		 * tests, but the rcu dereference will complain without it.
  		 */
  		preempt_disable();
1d0e78e38   Jiri Olsa   tracing/filter: A...
2303
2304
2305
2306
2307
2308
2309
  		if (*d->not_visited)
  			walk_pred_tree(filter->preds, filter->root,
  				       test_walk_pred_cb,
  				       d->not_visited);
  
  		test_pred_visited = 0;
  		err = filter_match_preds(filter, &d->rec);
86b6ef21b   Steven Rostedt   tracing: Add pree...
2310
  		preempt_enable();
1d0e78e38   Jiri Olsa   tracing/filter: A...
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
  
  		__free_filter(filter);
  
  		if (test_pred_visited) {
  			printk(KERN_INFO
  			       "Failed, unwanted pred visited for filter %s
  ",
  			       d->filter);
  			break;
  		}
  
  		if (err != d->match) {
  			printk(KERN_INFO
  			       "Failed to match filter '%s', expected %d
  ",
  			       d->filter, d->match);
  			break;
  		}
  	}
  
  	if (i == DATA_CNT)
  		printk(KERN_CONT "OK
  ");
  
  	return 0;
  }
  
  late_initcall(ftrace_test_event_filter);
  
  #endif /* CONFIG_FTRACE_STARTUP_TEST */