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