ftrace/filter: Trace events filtering related fixies
cover ftrace/filter: Trace events filtering related fixies
hi,
on the way to perf/ftrace filtering changes, I made some changes to the current events filtering code. It could be divided into 3 parts roughly:
- remove unnecessary dynamic allocations (patches 1 - 4)
- unify the predicate tree walking (patches 5 - 9)
- added startup test for event filtering (patch 10)
attached patches:
- 01/10 tracing/filter: Use static allocation for filter predicates
- 02/10 tracing/filter: Separate predicate init and filter addition
- 03/10 tracing/filter: Remove field_name from filter_pred struct
- 04/10 tracing/filter: Simplify tracepoint event lookup
- 05/10 tracing/filter: Unify predicate tree walking, change check_pred_tree function to use it
- 06/10 tracing/filter: Change count_leafs function to use walk_pred_tree
- 07/10 tracing/filter: Change fold_pred_tree function to use walk_pred_tree
- 08/10 tracing/filter: Change fold_pred function to use walk_pred_tree
- 09/10 tracing/filter: Change filter_match_preds function to use walk_pred_tree
- 10/10 tracing/filter: Add startup tests for events filter
wbr,
jirka
--- include/trace/events/test.h | 45 ++ kernel/trace/trace.h | 14 +- kernel/trace/trace_events_filter.c | 778 +++++++++++++++++++++--------------- 3 files changed, 497 insertions(+), 340 deletions(-)
PATCH 1/10 tracing/filter: Use static allocation for filter predicates
Don't dynamically allocate filter_pred struct, use static memory. This way we can get rid of the code managing the dynamic filter_pred struct object.
The create_pred function integrates create_logical_pred function. This way the static predicate memory is returned only from one place.
--- kernel/trace/trace_events_filter.c | 57 ++++++++++-------------------------- 1 files changed, 16 insertions(+), 41 deletions(-)
kernel/trace/trace_events_filter.c +16 -41
@@ -630,11 +630,7 @@ find_event_field(struct ftrace_event_call *call, char *name)static void filter_free_pred (struct filter_pred *pred) { -if (!pred) -return ; -kfree (pred->field_name); -kfree (pred); }static void filter_clear_pred (struct filter_pred *pred)
@@ -1302,39 +1298,30 @@ parse_operand:return 0 ; } -static struct filter_pred *create_pred (int op,char *operand1,char *operand2) +static struct filter_pred *create_pred (struct filter_parse_state *ps, +int op,char *operand1,char *operand2) { -struct filter_pred *pred; +static struct filter_pred pred; - pred =kzalloc (sizeof (*pred),GFP_KERNEL ); -if (!pred) -return NULL ; +memset (&pred,0 ,sizeof (pred)); + pred.op = op; - pred->field_name =kstrdup (operand1,GFP_KERNEL ); -if (!pred->field_name) { -kfree (pred); +if (op ==OP_AND || op ==OP_OR ) +return &pred; + +if (!operand1 || !operand2) { +parse_error (ps,FILT_ERR_MISSING_FIELD ,0 );return NULL ; } -strcpy (pred->regex.pattern, operand2); - pred->regex.len =strlen (pred->regex.pattern); - - pred->op = op; - -return pred; -} - -static struct filter_pred *create_logical_pred (int op) -{ -struct filter_pred *pred; - - pred =kzalloc (sizeof (*pred),GFP_KERNEL ); -if (!pred) + pred.field_name =kstrdup (operand1,GFP_KERNEL ); +if (!pred.field_name)return NULL ; - pred->op = op; +strcpy (pred.regex.pattern, operand2); + pred.regex.len =strlen (pred.regex.pattern); -return pred; +return &pred; }static int check_preds (struct filter_parse_state *ps)
@@ -1643,19 +1630,7 @@ static int replace_preds(struct ftrace_event_call *call, goto fail; } -if (elt->op ==OP_AND || elt->op ==OP_OR ) { - pred =create_logical_pred (elt->op); - goto add_pred; - } - -if (!operand1 || !operand2) { -parse_error (ps,FILT_ERR_MISSING_FIELD ,0 ); - err = -EINVAL ; - goto fail; - } - - pred =create_pred (elt->op, operand1, operand2); -add_pred: + pred =create_pred (ps, elt->op, operand1, operand2);if (!pred) { err = -ENOMEM ; goto fail;
Signature · 1 line
1.7.1
PATCH 2/10 tracing/filter: Separate predicate init and filter addition
In reply to Jiri OlsaSeparating the filter_pred initialization from code that adds the filter_pred to the filter.
The create_pred returns initialized predicate, which is added to the filter by filter_add_pred function.
Keeping dry_run variable inside replace_preds function.
--- kernel/trace/trace_events_filter.c | 56 +++++++++++++++--------------------- 1 files changed, 23 insertions(+), 33 deletions(-)
kernel/trace/trace_events_filter.c +23 -33
@@ -685,8 +685,7 @@ __pop_pred_stack(struct pred_stack *stack)static int filter_set_pred (struct event_filter *filter,int idx,struct pred_stack *stack, -struct filter_pred *src, -filter_pred_fn_t fn) +struct filter_pred *src) {struct filter_pred *dest = &filter->preds[idx];struct filter_pred *left;
@@ -698,7 +697,6 @@ static int filter_set_pred(struct event_filter *filter,if (!dest->field_name)return -ENOMEM ; } - dest->fn = fn; dest->index = idx;if (dest->op ==OP_OR || dest->op ==OP_AND ) {
@@ -836,12 +834,10 @@ static void filter_free_subsystem_filters(struct event_subsystem *system) } } -static int filter_add_pred_fn (struct filter_parse_state *ps, -struct ftrace_event_call *call, -struct event_filter *filter, -struct filter_pred *pred, -struct pred_stack *stack, -filter_pred_fn_t fn) +static int filter_add_pred (struct filter_parse_state *ps, +struct event_filter *filter, +struct filter_pred *pred, +struct pred_stack *stack) {int idx, err;
@@ -852,7 +848,7 @@ static int filter_add_pred_fn(struct filter_parse_state *ps, idx = filter->n_preds;filter_clear_pred (&filter->preds[idx]); - err =filter_set_pred (filter, idx, stack, pred, fn); + err =filter_set_pred (filter, idx, stack, pred);if (err)return err;
@@ -933,25 +929,16 @@ static filter_pred_fn_t select_comparison_fn(int op, int field_size,return fn; } -static int filter_add_pred (struct filter_parse_state *ps, -struct ftrace_event_call *call, -struct event_filter *filter, -struct filter_pred *pred, -struct pred_stack *stack, -bool dry_run) +static int init_pred (struct filter_parse_state *ps, +struct ftrace_event_call *call, +struct filter_pred *pred) + {struct ftrace_event_field *field; -filter_pred_fn_t fn; +filter_pred_fn_t fn = filter_pred_none;unsigned long long val;int ret; - fn = pred->fn = filter_pred_none; - -if (pred->op ==OP_AND ) - goto add_pred_fn; -else if (pred->op ==OP_OR ) - goto add_pred_fn; - field =find_event_field (call, pred->field_name);if (!field) {parse_error (ps,FILT_ERR_FIELD_NOT_FOUND ,0 );
@@ -997,9 +984,7 @@ static int filter_add_pred(struct filter_parse_state *ps,if (pred->op ==OP_NE ) pred->not =1 ; -add_pred_fn: -if (!dry_run) -return filter_add_pred_fn (ps, call, filter, pred, stack, fn); + pred->fn = fn;return 0 ; }
@@ -1299,6 +1284,7 @@ parse_operand: }static struct filter_pred *create_pred (struct filter_parse_state *ps, +struct ftrace_event_call *call,int op,char *operand1,char *operand2) {static struct filter_pred pred;
@@ -1321,7 +1307,7 @@ static struct filter_pred *create_pred(struct filter_parse_state *ps,strcpy (pred.regex.pattern, operand2); pred.regex.len =strlen (pred.regex.pattern); -return &pred; +return init_pred (ps, call, &pred) ?NULL : &pred; }static int check_preds (struct filter_parse_state *ps)
@@ -1630,16 +1616,20 @@ static int replace_preds(struct ftrace_event_call *call, goto fail; } - pred =create_pred (ps, elt->op, operand1, operand2); + pred =create_pred (ps, call, elt->op, operand1, operand2);if (!pred) { err = -ENOMEM ; goto fail; } - err =filter_add_pred (ps, call, filter, pred, &stack, dry_run); -filter_free_pred (pred); -if (err) - goto fail; +if (!dry_run) { + err =filter_add_pred (ps, filter, pred, &stack); +if (err) { +filter_free_pred (pred); + goto fail; + } + } +filter_free_pred (pred); operand1 = operand2 =NULL ; }
Signature · 1 line
1.7.1
Steven Rostedt
maintainerRe: [PATCH 02/10] tracing/filter: Separate predicate init and filter addition
Quoted 7 lines · Jiri Olsa
Separating the
filter_predinitialization from code that adds thefilter_predto the filter.The
create_predreturns initialized predicate, which is added to the filter byfilter_add_predfunction.Keeping
dry_runvariable insidereplace_predsfunction.
The only issue I have with this patch is the change log. It tells me what you are doing, but does not tell us why you are doing it.
I'm pretty clueless about reviewing this patch, as I don't know what the goal of the patch is. It's like a patch that says, increment i
+ i++;
-- Steve
Quoted 2 lines
Signed-off-by Jiri Olsa <[email protected]>
PATCH 3/10 tracing/filter: Remove field_name from filter_pred struct
In reply to Jiri OlsaThe field_name was used just for finding event's fields. This way we don't need to care about field_name allocation/free.
--- kernel/trace/trace.h | 11 +------- kernel/trace/trace_events_filter.c | 53 ++++++++--------------------------- 2 files changed, 13 insertions(+), 51 deletions(-)
kernel/trace/trace.h +1 -10
@@ -761,16 +761,7 @@ struct filter_pred {filter_pred_fn_t fn;u64 val;struct regex regex; -/* -* Leaf nodes use field_name, ops is used by AND and OR -* nodes. The field_name is always freed when freeing a pred. -* We can overload field_name for ops and have it freed -* as well. -*/ -union { -char *field_name; -unsigned short *ops; - }; +unsigned short *ops;int offset;int not;int op;
kernel/trace/trace_events_filter.c +12 -41
127 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH 4/10 tracing/filter: Simplify tracepoint event lookup
In reply to Jiri OlsaWe dont need to perform lookup through the ftrace_events list, instead we can use the 'tp_event' field.
Each perf_event contains tracepoint event field 'tp_event', which got initialized during the tracepoint event initialization.
--- kernel/trace/trace_events_filter.c | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-)
kernel/trace/trace_events_filter.c +3 -6
@@ -1894,17 +1894,14 @@ int ftrace_profile_set_filter(struct perf_event *event, int event_id,int err;struct event_filter *filter;struct filter_parse_state *ps; -struct ftrace_event_call *call =NULL ; +struct ftrace_event_call *call;mutex_lock (&event_mutex); -list_for_each_entry (call, &ftrace_events, list) { -if (call->event.type == event_id) -break ; - } + call = event->tp_event; err = -EINVAL ; -if (&call->list == &ftrace_events) +if (!call) goto out_unlock; err = -EEXIST ;
Signature · 1 line
1.7.1
PATCH 5/10 tracing/filter: Unify predicate tree walking, change check_pred_tree function to use it
In reply to Jiri OlsaAdding walk_pred_tree function to be used for walking throught the filter predicates.
For each predicate the callback function is called, allowing users to add their own functionality or customize their way through the filter predicates.
Changing check_pred_tree function to use walk_pred_tree.
--- kernel/trace/trace_events_filter.c | 137 ++++++++++++++++++++++------------- 1 files changed, 86 insertions(+), 51 deletions(-)
kernel/trace/trace_events_filter.c +86 -51
@@ -381,6 +381,63 @@ get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,return pred; } +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 ; +} +/* * A series of AND or ORs where found together. Instead of * climbing up and down the tree branches, an array of the
@@ -1321,6 +1378,23 @@ static int count_preds(struct filter_parse_state *ps)return n_preds; } +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 ; +} +/* * 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
@@ -1329,58 +1403,19 @@ static int count_preds(struct filter_parse_state *ps)static int check_pred_tree (struct event_filter *filter,struct filter_pred *root) { -struct filter_pred *preds; -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int count =0 ; -int done =0 ; -int max; - -/* -* 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; - - preds = filter->preds; -if (!preds) -return -EINVAL ; - pred = root; - -do { -if (WARN_ON (count++ > max)) -return -EINVAL ; - -switch (move) { -case MOVE_DOWN : -if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } -/* A leaf at the root is just a leaf in the tree */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); +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 , + }; -/* We are fine. */ -return 0 ; +return walk_pred_tree (filter->preds, root, + check_pred_tree_cb, &data); }static int count_leafs (struct filter_pred *preds,struct filter_pred *root)
Signature · 1 line
1.7.1
PATCH 6/10 tracing/filter: Change count_leafs function to use walk_pred_tree
In reply to Jiri OlsaChanging count_leafs function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 45 +++++++++-------------------------- 1 files changed, 12 insertions(+), 33 deletions(-)
kernel/trace/trace_events_filter.c +12 -33
@@ -1418,43 +1418,22 @@ static int check_pred_tree(struct event_filter *filter, check_pred_tree_cb, &data); } -static int count_leafs (struct filter_pred *preds,struct filter_pred *root) +static int count_leafs_cb (enum move_type move,struct filter_pred *pred, +int *err,void *data) { -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int count =0 ; -int done =0 ; +int *count = data; - pred = root; +if ((move ==MOVE_DOWN ) && + (pred->left ==FILTER_PRED_INVALID )) + (*count)++; -do { -switch (move) { -case MOVE_DOWN : -if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } -/* A leaf at the root is just a leaf in the tree */ -if (pred == root) -return 1 ; - count++; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); +return WALK_PRED_DEFAULT ; +} +static int count_leafs (struct filter_pred *preds,struct filter_pred *root) +{ +int count =0 ; +WARN_ON (walk_pred_tree (preds, root, count_leafs_cb, &count));return count; }
Signature · 1 line
1.7.1
Steven Rostedt
maintainerRe: [PATCH 06/10] tracing/filter: Change count_leafs function to use walk_pred_tree
I really like these two patches (5 and 6) as I hated the duplicate code of the two tree walks. But... see below.
Quoted 55 lines · Jiri Olsa
Changing
count_leafsfunction to use unified predicates tree processing.Signed-off-by Jiri Olsa <[email protected]>--- kernel/trace/trace_events_filter.c | 45 +++++++++-------------------------- 1 files changed, 12 insertions(+), 33 deletions(-)kernel/trace/trace_events_filter.c +6
-30@@ -1418,43 +1418,22 @@ static int check_pred_tree(struct event_filter *filter, check_pred_tree_cb, &data); } -static int count_leafs (struct filter_pred *preds,struct filter_pred *root) +static int count_leafs_cb (enum move_type move,struct filter_pred *pred, +int *err,void *data) { -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int count =0 ; -int done =0 ; +int *count = data; - pred = root; +if ((move ==MOVE_DOWN ) && + (pred->left ==FILTER_PRED_INVALID )) + (*count)++; - do { - switch (move) { - case MOVE_DOWN: - if (pred->left != FILTER_PRED_INVALID) { - pred = &preds[pred->left]; - continue; - } - /* A leaf at the root is just a leaf in the tree */ - if (pred == root) - return 1; - count++; - pred = get_pred_parent(pred, preds, - pred->parent, &move); - continue; - case MOVE_UP_FROM_LEFT: - pred = &preds[pred->right]; - move = MOVE_DOWN; - continue; - case MOVE_UP_FROM_RIGHT: - if (pred == root) - break; - pred = get_pred_parent(pred, preds, - pred->parent, &move); - continue;
kernel/trace/trace_events_filter.c static int check_pred_tree(struct event_filter *filter, · line 1436
Quoted 10 lines
kernel/trace/trace_events_filter.c +6
-3- } - done =1 ; - }while (!done); +return WALK_PRED_DEFAULT ; +} +static int count_leafs (struct filter_pred *preds,struct filter_pred *root) +{ +int count =0 ; +WARN_ON (walk_pred_tree (preds, root, count_leafs_cb, &count));
Please do not put functionality in a WARN_ON(). Some people have WARN_ON() turn into nops.
Make this a:
ret = walk_pred_tree(preds, root, count_leafs_cb, &count); WARN_ON(ret);
-- Steve
Quoted 3 lines
kernel/trace/trace_events_filter.c +0
-0return count; }
PATCH 7/10 tracing/filter: Change fold_pred_tree function to use walk_pred_tree
In reply to Jiri OlsaChanging fold_pred_tree function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 65 +++++++++++------------------------- 1 files changed, 20 insertions(+), 45 deletions(-)
kernel/trace/trace_events_filter.c +20 -45
@@ -1494,6 +1494,24 @@ static int fold_pred(struct filter_pred *preds, struct filter_pred *root)return 0 ; } +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 ; +} +/* * 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
@@ -1502,51 +1520,8 @@ static int fold_pred(struct filter_pred *preds, struct filter_pred *root)static int fold_pred_tree (struct event_filter *filter,struct filter_pred *root) { -struct filter_pred *preds; -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int done =0 ; -int err; - - preds = filter->preds; -if (!preds) -return -EINVAL ; - pred = root; - -do { -switch (move) { -case MOVE_DOWN : -if (pred->index &FILTER_PRED_FOLD ) { - err =fold_pred (preds, pred); -if (err) -return err; -/* Folded nodes are like leafs */ - }else if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } - -/* A leaf at the root is just a leaf in the tree */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); - -return 0 ; +return walk_pred_tree (filter->preds, root, fold_pred_tree_cb, + filter->preds); } static int replace_preds(struct ftrace_event_call *call,
Signature · 1 line
1.7.1
PATCH 8/10 tracing/filter: Change fold_pred function to use walk_pred_tree
In reply to Jiri OlsaChanging fold_pred_tree function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 68 +++++++++++++++++------------------ 1 files changed, 33 insertions(+), 35 deletions(-)
kernel/trace/trace_events_filter.c +33 -35
@@ -1437,13 +1437,40 @@ static int count_leafs(struct filter_pred *preds, struct filter_pred *root)return count; } +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 ; +} +static int fold_pred (struct filter_pred *preds,struct filter_pred *root) { -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int count =0 ; +struct fold_pred_data data = { + .root = root, + .count =0 , + };int children; -int done =0 ;/* No need to keep the fold flag */ root->index &= ~FILTER_PRED_FOLD ;
@@ -1461,37 +1488,8 @@ static int fold_pred(struct filter_pred *preds, struct filter_pred *root)return -ENOMEM ; root->val = children; - - pred = root; -do { -switch (move) { -case MOVE_DOWN : -if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } -if (WARN_ON (count == children)) -return -EINVAL ; - pred->index &= ~FILTER_PRED_FOLD ; - root->ops[count++] = pred->index; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); - -return 0 ; + data.children = children; +return walk_pred_tree (preds, root, fold_pred_cb, &data); } static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
Signature · 1 line
1.7.1
PATCH 9/10 tracing/filter: Change filter_match_preds function to use walk_pred_tree
In reply to Jiri OlsaChanging filter_match_preds function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 119 +++++++++++++++++------------------- 1 files changed, 55 insertions(+), 64 deletions(-)
kernel/trace/trace_events_filter.c +55 -64
@@ -467,6 +467,7 @@ static int process_ops(struct filter_pred *preds,for (i =0 ; i < op->val; i++) { pred = &preds[op->ops[i]]; +BUG_ON (!pred->fn); match = pred->fn (pred, rec);if (!!match == type)return match;
@@ -474,92 +475,82 @@ static int process_ops(struct filter_pred *preds,return match; } +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 { +BUG_ON (!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 ; +} +/* return 1 if event matches, 0 otherwise (discard) */ int filter_match_preds (struct event_filter *filter,void *rec) { -int match =-1 ; -enum move_type move =MOVE_DOWN ;struct filter_pred *preds; -struct filter_pred *pred;struct filter_pred *root; +struct filter_match_preds_data data = { +/* match is currently meaningless */ + .match =-1 , + .rec = rec, + };int n_preds; -int done =0 ;/* no filter is considered a match */ if (!filter)return 1 ; n_preds = filter->n_preds; -if (!n_preds)return 1 ;/* * n_preds, root and filter->preds are protect with preemption disabled. */ - preds =rcu_dereference_sched (filter->preds); root =rcu_dereference_sched (filter->root);if (!root)return 1 ; - pred = root; - -/* match is currently meaningless */ - match =-1 ; - -do { -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) { -/* keep going to down the left side */ - pred = &preds[pred->left]; -continue ; - } -/* We can treat folded ops as a leaf node */ - match =process_ops (preds, pred, rec); - }else - match = pred->fn (pred, rec); -/* If this pred is the only pred */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -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 (!!match == (pred->op ==OP_OR )) { -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } -/* now go down the right side of the tree. */ - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -/* We finished this equation. */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); - -return match; + data.preds = preds =rcu_dereference_sched (filter->preds); +WARN_ON (walk_pred_tree (preds, root, filter_match_preds_cb, &data)); +return data.match; }EXPORT_SYMBOL_GPL (filter_match_preds);
Signature · 1 line
1.7.1
Steven Rostedt
maintainerRe: [PATCH 09/10] tracing/filter: Change filter_match_preds function to use walk_pred_tree
Quoted 13 lines · Jiri Olsa
Changing
filter_match_predsfunction to use unified predicates tree processing.Signed-off-by Jiri Olsa <[email protected]>--- kernel/trace/trace_events_filter.c | 119 +++++++++++++++++------------------- 1 files changed, 55 insertions(+), 64 deletions(-)kernel/trace/trace_events_filter.c +0
-0
kernel/trace/trace_events_filter.c static int process_ops(struct filter_pred *preds, · line 470
Quoted 4 lines
kernel/trace/trace_events_filter.c +1
-0@@ -467,6 +467,7 @@ static int process_ops(struct filter_pred *preds,for (i =0 ; i < op->val; i++) { pred = &preds[op->ops[i]]; +BUG_ON (!pred->fn);
Why the added BUG_ON()s? Do we really need to crash the kernel on error here?
-- Steve
Quoted 32 lines
kernel/trace/trace_events_filter.c +25
-0match = pred->fn (pred, rec);if (!!match == type)return match;@@ -474,92 +475,82 @@ static int process_ops(struct filter_pred *preds,return match; } +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 { +BUG_ON (!pred->fn); + d->match = pred->fn (pred, d->rec); + }
Steven Rostedt
maintainerRe: [PATCH 09/10] tracing/filter: Change filter_match_preds function to use walk_pred_tree
Quoted 15 lines · Steven Rostedt
Quoted 14 lines · Jiri Olsa
Changing
filter_match_predsfunction to use unified predicates tree processing.Signed-off-by Jiri Olsa <[email protected]>--- kernel/trace/trace_events_filter.c | 119 +++++++++++++++++------------------- 1 files changed, 55 insertions(+), 64 deletions(-)kernel/trace/trace_events_filter.c +0
-0@@ -467,6 +467,7 @@ static int process_ops(struct filter_pred *preds,
Quoted 6 lines
kernel/trace/trace_events_filter.c static int process_ops(struct filter_pred *preds, · line 470
Quoted 3 lines
kernel/trace/trace_events_filter.c +1
-0for (i =0 ; i < op->val; i++) { pred = &preds[op->ops[i]]; +BUG_ON (!pred->fn);Why the added BUG_ON()s? Do we really need to crash the kernel on error here?
I mean, could be instead have:
if (!WARN_ON_ONCE(!pred->fn))
Quoted 24 lines
Quoted 24 lines
kernel/trace/trace_events_filter.c +0
-0match = pred->fn (pred, rec);kernel/trace/trace_events_filter.c +15
-0if (!!match == type)return match;@@ -474,92 +475,82 @@ static int process_ops(struct filter_pred *preds,return match; } +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 */
Quoted 8 lines
kernel/trace/trace_events_filter.c static int process_ops(struct filter_pred *preds, · line 500
Quoted 8 lines
kernel/trace/trace_events_filter.c +8
-0+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 { +BUG_ON (!pred->fn);
if (!WARN_ON_ONCE(!pred->fn))
Quoted 1 line
kernel/trace/trace_events_filter.c static int process_ops(struct filter_pred *preds, · line 501
Quoted 1 line
kernel/trace/trace_events_filter.c +1
-0+ d->match = pred->fn (pred, d->rec);
-- Steve
Quoted 2 lines
Quoted 1 line
kernel/trace/trace_events_filter.c +1
-0+ }
PATCH 10/10 tracing/filter: Add startup tests for events filter
In reply to Jiri OlsaAdding automated tests running as late_initcall. Tests are compiled in with CONFIG_FTRACE_STARTUP_TEST option.
Adding test event "ftrace_test_filter" used to simulate filter processing during event occurance.
String filters are compiled and tested against several test events with different values.
Also testing that evaluation of explicit predicates is ommited due to the lazy filter evaluation.
--- include/trace/events/test.h | 45 ++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_events_filter.c | 207 ++++++++++++++++++++++++++++++++++++ 3 files changed, 255 insertions(+), 0 deletions(-) create mode 100644 include/trace/events/test.h
include/trace/events/test.h new file +45 -0
@@ -0,0 +1,45 @@ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM test + +#if !defined(_TRACE_TEST_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_TEST_H + +#include <linux/tracepoint.h> + +TRACE_EVENT (ftrace_test_filter, + + TP_PROTO(int a, int b, int c, int d, int e, int f, int g, int h), + +TP_ARGS (a, b, c, d, e, f, g, h), + +TP_STRUCT__entry ( + __field(int, a) + __field(int, b) + __field(int, c) + __field(int, d) + __field(int, e) + __field(int, f) + __field(int, g) +__field (int, h) + ), + +TP_fast_assign ( + __entry->a = a; + __entry->b = b; + __entry->c = c; + __entry->d = d; + __entry->e = e; + __entry->f = f; + __entry->g = g; + __entry->h = h; + ), + +TP_printk ("a %d, b %d, c %d, d %d, e %d, f %d, g %d, h %d" , + __entry->a, __entry->b, __entry->c, __entry->d, + __entry->e, __entry->f, __entry->g, __entry->h) +); + +#endif /* _TRACE_TEST_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h>
kernel/trace/trace.h +3 -0
@@ -762,6 +762,9 @@ struct filter_pred {u64 val;struct regex regex;unsigned short *ops; +#ifdef CONFIG_FTRACE_STARTUP_TEST +struct ftrace_event_field *field; +#endif int offset;int not;int op;
kernel/trace/trace_events_filter.c +207 -0
216 lines not on this page · raw message
Signature · 1 line
1.7.1
Re: [PATCHv2 10/10] tracing/filter: Add startup tests for events filter
oops,
I sent wrong version with small leak, proper one attached..
thanks,
jirka
---
Adding automated tests running as late_initcall. Tests are compiled in with CONFIG_FTRACE_STARTUP_TEST option.
Adding test event "ftrace_test_filter" used to simulate filter processing during event occurance.
String filters are compiled and tested against several test events with different values.
Also testing that evaluation of explicit predicates is ommited due to the lazy filter evaluation.
--- include/trace/events/test.h | 45 ++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_events_filter.c | 209 ++++++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+), 0 deletions(-) create mode 100644 include/trace/events/test.h
include/trace/events/test.h new file +45 -0
@@ -0,0 +1,45 @@ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM test + +#if !defined(_TRACE_TEST_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_TEST_H + +#include <linux/tracepoint.h> + +TRACE_EVENT (ftrace_test_filter, + + TP_PROTO(int a, int b, int c, int d, int e, int f, int g, int h), + +TP_ARGS (a, b, c, d, e, f, g, h), + +TP_STRUCT__entry ( + __field(int, a) + __field(int, b) + __field(int, c) + __field(int, d) + __field(int, e) + __field(int, f) + __field(int, g) +__field (int, h) + ), + +TP_fast_assign ( + __entry->a = a; + __entry->b = b; + __entry->c = c; + __entry->d = d; + __entry->e = e; + __entry->f = f; + __entry->g = g; + __entry->h = h; + ), + +TP_printk ("a %d, b %d, c %d, d %d, e %d, f %d, g %d, h %d" , + __entry->a, __entry->b, __entry->c, __entry->d, + __entry->e, __entry->f, __entry->g, __entry->h) +); + +#endif /* _TRACE_TEST_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h>
kernel/trace/trace.h +3 -0
@@ -762,6 +762,9 @@ struct filter_pred {u64 val;struct regex regex;unsigned short *ops; +#ifdef CONFIG_FTRACE_STARTUP_TEST +struct ftrace_event_field *field; +#endif int offset;int not;int op;
kernel/trace/trace_events_filter.c +209 -0
218 lines not on this page · raw message
Signature · 1 line
1.7.1
Steven Rostedt
maintainerRe: [PATCHv2 10/10] tracing/filter: Add startup tests for events filter
include/trace/events/test.h
Quoted 33 lines · Jiri Olsa
oops,
I sent wrong version with small leak, proper one attached..thanks,
jirka---
Adding automated tests running aslate_initcall. Tests are compiled in withCONFIG_FTRACE_STARTUP_TESToption.Adding test event "
ftrace_test_filter" used to simulate filter processing during event occurance.String filters are compiled and tested against several test events with different values.
Also testing that evaluation of explicit predicates is ommited due to the lazy filter evaluation.
Signed-off-by Jiri Olsa <[email protected]>--- include/trace/events/test.h | 45 ++++++++ kernel/trace/trace.h | 3 + kernel/trace/trace_events_filter.c | 209 ++++++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+), 0 deletions(-) create mode 100644 include/trace/events/test.hinclude/trace/events/test.h new file +0
-0
Hmm, I'd like to keep this out of the include/trace/events directory. Could you put this directly into the kernel/trace/ directory, using the Makefile tricks in samples/trace_events/Makefile and the TRACE_INCLUDE_PATH as seen in samples/trace_events/trace-events-sample.h
-- Steve
Quoted 42 lines
include/trace/events/test.h new file +41
-0@@ -0,0 +1,45 @@ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM test + +#if !defined(_TRACE_TEST_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_TEST_H + +#include <linux/tracepoint.h> + +TRACE_EVENT (ftrace_test_filter, + + TP_PROTO(int a, int b, int c, int d, int e, int f, int g, int h), + +TP_ARGS (a, b, c, d, e, f, g, h), + +TP_STRUCT__entry ( + __field(int, a) + __field(int, b) + __field(int, c) + __field(int, d) + __field(int, e) + __field(int, f) + __field(int, g) +__field (int, h) + ), + +TP_fast_assign ( + __entry->a = a; + __entry->b = b; + __entry->c = c; + __entry->d = d; + __entry->e = e; + __entry->f = f; + __entry->g = g; + __entry->h = h; + ), + +TP_printk ("a %d, b %d, c %d, d %d, e %d, f %d, g %d, h %d" , + __entry->a, __entry->b, __entry->c, __entry->d, + __entry->e, __entry->f, __entry->g, __entry->h) +); +
Steven Rostedt
maintainerQuoted 10 lines · Jiri Olsa
hi,
on the way to perf/ftrace filtering changes, I made some changes to the current events filtering code. It could be divided into 3 parts roughly:
- remove unnecessary dynamic allocations (patches 1 - 4)
- unify the predicate tree walking (patches 5 - 9)
- added startup test for event filtering (patch 10)
Quoted 11 lines
attached patches:
- 01/10 tracing/filter: Use static allocation for filter predicates
- 02/10 tracing/filter: Separate predicate init and filter addition
- 03/10 tracing/filter: Removefield_namefromfilter_predstruct
- 04/10 tracing/filter: Simplify tracepoint event lookup
- 05/10 tracing/filter: Unify predicate tree walking, changecheck_pred_treefunction to use it
- 06/10 tracing/filter: Changecount_leafsfunction to usewalk_pred_tree
- 07/10 tracing/filter: Changefold_pred_treefunction to usewalk_pred_tree
- 08/10 tracing/filter: Changefold_predfunction to usewalk_pred_tree
- 09/10 tracing/filter: Changefilter_match_predsfunction to usewalk_pred_tree
- 10/10 tracing/filter: Add startup tests for events filter
Other than the comments I made, I really like this patch set. Thanks a lot for the hard work put into this!
If you do an update, I'll pull it into my queue for 3.2.
-- Steve
Quoted 23 lines · Steven Rostedt
Quoted 10 lines · Jiri Olsa
hi,
on the way to perf/ftrace filtering changes, I made some changes to the current events filtering code. It could be divided into 3 parts roughly:
- remove unnecessary dynamic allocations (patches 1 - 4)
- unify the predicate tree walking (patches 5 - 9)
- added startup test for event filtering (patch 10)Quoted 11 lines
attached patches:
- 01/10 tracing/filter: Use static allocation for filter predicates
- 02/10 tracing/filter: Separate predicate init and filter addition
- 03/10 tracing/filter: Removefield_namefromfilter_predstruct
- 04/10 tracing/filter: Simplify tracepoint event lookup
- 05/10 tracing/filter: Unify predicate tree walking, changecheck_pred_treefunction to use it
- 06/10 tracing/filter: Changecount_leafsfunction to usewalk_pred_tree
- 07/10 tracing/filter: Changefold_pred_treefunction to usewalk_pred_tree
- 08/10 tracing/filter: Changefold_predfunction to usewalk_pred_tree
- 09/10 tracing/filter: Changefilter_match_predsfunction to usewalk_pred_tree
- 10/10 tracing/filter: Add startup tests for events filter
Quoted 4 lines
Other than the comments I made, I really like this patch set. Thanks a lot for the hard work put into this!
If you do an update, I'll pull it into my queue for 3.2.
thanks for comments, I'll send out v2 shortly
jirka
v2 cover ftrace/filter: Trace events filtering related fixies
In reply to Steven Rostedthi,
on the way to perf/ftrace filtering changes, I made some changes to the current events filtering code. It could be divided into 3 parts roughly:
- remove unnecessary dynamic allocations (patches 1 - 4)
- unify the predicate tree walking (patches 5 - 9)
- added startup test for event filtering (patch 10)
attached patches:
- 01/10 tracing/filter: Use static allocation for filter predicates
- 02/10 tracing/filter: Separate predicate init and filter addition
- 03/10 tracing/filter: Remove field_name from filter_pred struct
- 04/10 tracing/filter: Simplify tracepoint event lookup
- 05/10 tracing/filter: Unify predicate tree walking, change check_pred_tree function to use it
- 06/10 tracing/filter: Change count_leafs function to use walk_pred_tree
- 07/10 tracing/filter: Change fold_pred_tree function to use walk_pred_tree
- 08/10 tracing/filter: Change fold_pred function to use walk_pred_tree
- 09/10 tracing/filter: Change filter_match_preds function to use walk_pred_tree
- 10/10 tracing/filter: Add startup tests for events filter
v2 changes:
- 02/10 - changed patch change log
- 06/10 - putting functionality out of WARN_ON
- 09/10 - using WARN_ON_ONCE instead of BUG_ON
- putting functionality out of WARN_ON
- 10/10 - moved/renamed test.h into kernel/trace/trace_events_filter_test.h
wbr,
jirka
--- kernel/trace/Makefile | 2 + kernel/trace/trace.h | 14 +- kernel/trace/trace_events_filter.c | 786 ++++++++++++++++++------------- kernel/trace/trace_events_filter_test.h | 50 ++ 4 files changed, 510 insertions(+), 342 deletions(-)
PATCH v2 1/10 tracing/filter: Use static allocation for filter predicates
Don't dynamically allocate filter_pred struct, use static memory. This way we can get rid of the code managing the dynamic filter_pred struct object.
The create_pred function integrates create_logical_pred function. This way the static predicate memory is returned only from one place.
--- kernel/trace/trace_events_filter.c | 57 ++++++++++-------------------------- 1 files changed, 16 insertions(+), 41 deletions(-)
kernel/trace/trace_events_filter.c +16 -41
@@ -630,11 +630,7 @@ find_event_field(struct ftrace_event_call *call, char *name)static void filter_free_pred (struct filter_pred *pred) { -if (!pred) -return ; -kfree (pred->field_name); -kfree (pred); }static void filter_clear_pred (struct filter_pred *pred)
@@ -1302,39 +1298,30 @@ parse_operand:return 0 ; } -static struct filter_pred *create_pred (int op,char *operand1,char *operand2) +static struct filter_pred *create_pred (struct filter_parse_state *ps, +int op,char *operand1,char *operand2) { -struct filter_pred *pred; +static struct filter_pred pred; - pred =kzalloc (sizeof (*pred),GFP_KERNEL ); -if (!pred) -return NULL ; +memset (&pred,0 ,sizeof (pred)); + pred.op = op; - pred->field_name =kstrdup (operand1,GFP_KERNEL ); -if (!pred->field_name) { -kfree (pred); +if (op ==OP_AND || op ==OP_OR ) +return &pred; + +if (!operand1 || !operand2) { +parse_error (ps,FILT_ERR_MISSING_FIELD ,0 );return NULL ; } -strcpy (pred->regex.pattern, operand2); - pred->regex.len =strlen (pred->regex.pattern); - - pred->op = op; - -return pred; -} - -static struct filter_pred *create_logical_pred (int op) -{ -struct filter_pred *pred; - - pred =kzalloc (sizeof (*pred),GFP_KERNEL ); -if (!pred) + pred.field_name =kstrdup (operand1,GFP_KERNEL ); +if (!pred.field_name)return NULL ; - pred->op = op; +strcpy (pred.regex.pattern, operand2); + pred.regex.len =strlen (pred.regex.pattern); -return pred; +return &pred; }static int check_preds (struct filter_parse_state *ps)
@@ -1643,19 +1630,7 @@ static int replace_preds(struct ftrace_event_call *call, goto fail; } -if (elt->op ==OP_AND || elt->op ==OP_OR ) { - pred =create_logical_pred (elt->op); - goto add_pred; - } - -if (!operand1 || !operand2) { -parse_error (ps,FILT_ERR_MISSING_FIELD ,0 ); - err = -EINVAL ; - goto fail; - } - - pred =create_pred (elt->op, operand1, operand2); -add_pred: + pred =create_pred (ps, elt->op, operand1, operand2);if (!pred) { err = -ENOMEM ; goto fail;
Signature · 1 line
1.7.1
PATCH v2 2/10 tracing/filter: Separate predicate init and filter addition
In reply to Jiri OlsaMaking the code cleaner by having one function to fully prepare the predicate (create_pred), and another to add the predicate to the filter (filter_add_pred).
As a benefit, this way the dry_run flag stays only inside the replace_preds function and is not passed deeper.
--- kernel/trace/trace_events_filter.c | 56 +++++++++++++++--------------------- 1 files changed, 23 insertions(+), 33 deletions(-)
kernel/trace/trace_events_filter.c +23 -33
@@ -685,8 +685,7 @@ __pop_pred_stack(struct pred_stack *stack)static int filter_set_pred (struct event_filter *filter,int idx,struct pred_stack *stack, -struct filter_pred *src, -filter_pred_fn_t fn) +struct filter_pred *src) {struct filter_pred *dest = &filter->preds[idx];struct filter_pred *left;
@@ -698,7 +697,6 @@ static int filter_set_pred(struct event_filter *filter,if (!dest->field_name)return -ENOMEM ; } - dest->fn = fn; dest->index = idx;if (dest->op ==OP_OR || dest->op ==OP_AND ) {
@@ -836,12 +834,10 @@ static void filter_free_subsystem_filters(struct event_subsystem *system) } } -static int filter_add_pred_fn (struct filter_parse_state *ps, -struct ftrace_event_call *call, -struct event_filter *filter, -struct filter_pred *pred, -struct pred_stack *stack, -filter_pred_fn_t fn) +static int filter_add_pred (struct filter_parse_state *ps, +struct event_filter *filter, +struct filter_pred *pred, +struct pred_stack *stack) {int idx, err;
@@ -852,7 +848,7 @@ static int filter_add_pred_fn(struct filter_parse_state *ps, idx = filter->n_preds;filter_clear_pred (&filter->preds[idx]); - err =filter_set_pred (filter, idx, stack, pred, fn); + err =filter_set_pred (filter, idx, stack, pred);if (err)return err;
@@ -933,25 +929,16 @@ static filter_pred_fn_t select_comparison_fn(int op, int field_size,return fn; } -static int filter_add_pred (struct filter_parse_state *ps, -struct ftrace_event_call *call, -struct event_filter *filter, -struct filter_pred *pred, -struct pred_stack *stack, -bool dry_run) +static int init_pred (struct filter_parse_state *ps, +struct ftrace_event_call *call, +struct filter_pred *pred) + {struct ftrace_event_field *field; -filter_pred_fn_t fn; +filter_pred_fn_t fn = filter_pred_none;unsigned long long val;int ret; - fn = pred->fn = filter_pred_none; - -if (pred->op ==OP_AND ) - goto add_pred_fn; -else if (pred->op ==OP_OR ) - goto add_pred_fn; - field =find_event_field (call, pred->field_name);if (!field) {parse_error (ps,FILT_ERR_FIELD_NOT_FOUND ,0 );
@@ -997,9 +984,7 @@ static int filter_add_pred(struct filter_parse_state *ps,if (pred->op ==OP_NE ) pred->not =1 ; -add_pred_fn: -if (!dry_run) -return filter_add_pred_fn (ps, call, filter, pred, stack, fn); + pred->fn = fn;return 0 ; }
@@ -1299,6 +1284,7 @@ parse_operand: }static struct filter_pred *create_pred (struct filter_parse_state *ps, +struct ftrace_event_call *call,int op,char *operand1,char *operand2) {static struct filter_pred pred;
@@ -1321,7 +1307,7 @@ static struct filter_pred *create_pred(struct filter_parse_state *ps,strcpy (pred.regex.pattern, operand2); pred.regex.len =strlen (pred.regex.pattern); -return &pred; +return init_pred (ps, call, &pred) ?NULL : &pred; }static int check_preds (struct filter_parse_state *ps)
@@ -1630,16 +1616,20 @@ static int replace_preds(struct ftrace_event_call *call, goto fail; } - pred =create_pred (ps, elt->op, operand1, operand2); + pred =create_pred (ps, call, elt->op, operand1, operand2);if (!pred) { err = -ENOMEM ; goto fail; } - err =filter_add_pred (ps, call, filter, pred, &stack, dry_run); -filter_free_pred (pred); -if (err) - goto fail; +if (!dry_run) { + err =filter_add_pred (ps, filter, pred, &stack); +if (err) { +filter_free_pred (pred); + goto fail; + } + } +filter_free_pred (pred); operand1 = operand2 =NULL ; }
Signature · 1 line
1.7.1
PATCH v2 3/10 tracing/filter: Remove field_name from filter_pred struct
In reply to Jiri OlsaThe field_name was used just for finding event's fields. This way we don't need to care about field_name allocation/free.
--- kernel/trace/trace.h | 11 +------- kernel/trace/trace_events_filter.c | 53 ++++++++--------------------------- 2 files changed, 13 insertions(+), 51 deletions(-)
kernel/trace/trace.h +1 -10
@@ -761,16 +761,7 @@ struct filter_pred {filter_pred_fn_t fn;u64 val;struct regex regex; -/* -* Leaf nodes use field_name, ops is used by AND and OR -* nodes. The field_name is always freed when freeing a pred. -* We can overload field_name for ops and have it freed -* as well. -*/ -union { -char *field_name; -unsigned short *ops; - }; +unsigned short *ops;int offset;int not;int op;
kernel/trace/trace_events_filter.c +12 -41
127 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v2 4/10 tracing/filter: Simplify tracepoint event lookup
In reply to Jiri OlsaWe dont need to perform lookup through the ftrace_events list, instead we can use the 'tp_event' field.
Each perf_event contains tracepoint event field 'tp_event', which got initialized during the tracepoint event initialization.
--- kernel/trace/trace_events_filter.c | 9 +++------ 1 files changed, 3 insertions(+), 6 deletions(-)
kernel/trace/trace_events_filter.c +3 -6
@@ -1894,17 +1894,14 @@ int ftrace_profile_set_filter(struct perf_event *event, int event_id,int err;struct event_filter *filter;struct filter_parse_state *ps; -struct ftrace_event_call *call =NULL ; +struct ftrace_event_call *call;mutex_lock (&event_mutex); -list_for_each_entry (call, &ftrace_events, list) { -if (call->event.type == event_id) -break ; - } + call = event->tp_event; err = -EINVAL ; -if (&call->list == &ftrace_events) +if (!call) goto out_unlock; err = -EEXIST ;
Signature · 1 line
1.7.1
PATCH v2 5/10 tracing/filter: Unify predicate tree walking, change check_pred_tree function to use it
In reply to Jiri OlsaAdding walk_pred_tree function to be used for walking throught the filter predicates.
For each predicate the callback function is called, allowing users to add their own functionality or customize their way through the filter predicates.
Changing check_pred_tree function to use walk_pred_tree.
--- kernel/trace/trace_events_filter.c | 137 ++++++++++++++++++++++------------- 1 files changed, 86 insertions(+), 51 deletions(-)
kernel/trace/trace_events_filter.c +86 -51
@@ -381,6 +381,63 @@ get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,return pred; } +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 ; +} +/* * A series of AND or ORs where found together. Instead of * climbing up and down the tree branches, an array of the
@@ -1321,6 +1378,23 @@ static int count_preds(struct filter_parse_state *ps)return n_preds; } +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 ; +} +/* * 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
@@ -1329,58 +1403,19 @@ static int count_preds(struct filter_parse_state *ps)static int check_pred_tree (struct event_filter *filter,struct filter_pred *root) { -struct filter_pred *preds; -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int count =0 ; -int done =0 ; -int max; - -/* -* 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; - - preds = filter->preds; -if (!preds) -return -EINVAL ; - pred = root; - -do { -if (WARN_ON (count++ > max)) -return -EINVAL ; - -switch (move) { -case MOVE_DOWN : -if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } -/* A leaf at the root is just a leaf in the tree */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); +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 , + }; -/* We are fine. */ -return 0 ; +return walk_pred_tree (filter->preds, root, + check_pred_tree_cb, &data); }static int count_leafs (struct filter_pred *preds,struct filter_pred *root)
Signature · 1 line
1.7.1
PATCH v2 6/10 tracing/filter: Change count_leafs function to use walk_pred_tree
In reply to Jiri OlsaChanging count_leafs function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 47 ++++++++++------------------------- 1 files changed, 14 insertions(+), 33 deletions(-)
kernel/trace/trace_events_filter.c +14 -33
@@ -1418,43 +1418,24 @@ static int check_pred_tree(struct event_filter *filter, check_pred_tree_cb, &data); } -static int count_leafs (struct filter_pred *preds,struct filter_pred *root) +static int count_leafs_cb (enum move_type move,struct filter_pred *pred, +int *err,void *data) { -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int count =0 ; -int done =0 ; +int *count = data; - pred = root; +if ((move ==MOVE_DOWN ) && + (pred->left ==FILTER_PRED_INVALID )) + (*count)++; -do { -switch (move) { -case MOVE_DOWN : -if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } -/* A leaf at the root is just a leaf in the tree */ -if (pred == root) -return 1 ; - count++; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); +return WALK_PRED_DEFAULT ; +} + +static int count_leafs (struct filter_pred *preds,struct filter_pred *root) +{ +int count =0 , ret; + ret =walk_pred_tree (preds, root, count_leafs_cb, &count); +WARN_ON (ret);return count; }
Signature · 1 line
1.7.1
PATCH v2 7/10 tracing/filter: Change fold_pred_tree function to use walk_pred_tree
In reply to Jiri OlsaChanging fold_pred_tree function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 65 +++++++++++------------------------- 1 files changed, 20 insertions(+), 45 deletions(-)
kernel/trace/trace_events_filter.c +20 -45
@@ -1496,6 +1496,24 @@ static int fold_pred(struct filter_pred *preds, struct filter_pred *root)return 0 ; } +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 ; +} +/* * 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
@@ -1504,51 +1522,8 @@ static int fold_pred(struct filter_pred *preds, struct filter_pred *root)static int fold_pred_tree (struct event_filter *filter,struct filter_pred *root) { -struct filter_pred *preds; -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int done =0 ; -int err; - - preds = filter->preds; -if (!preds) -return -EINVAL ; - pred = root; - -do { -switch (move) { -case MOVE_DOWN : -if (pred->index &FILTER_PRED_FOLD ) { - err =fold_pred (preds, pred); -if (err) -return err; -/* Folded nodes are like leafs */ - }else if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } - -/* A leaf at the root is just a leaf in the tree */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); - -return 0 ; +return walk_pred_tree (filter->preds, root, fold_pred_tree_cb, + filter->preds); } static int replace_preds(struct ftrace_event_call *call,
Signature · 1 line
1.7.1
PATCH v2 8/10 tracing/filter: Change fold_pred function to use walk_pred_tree
In reply to Jiri OlsaChanging fold_pred_tree function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 68 +++++++++++++++++------------------ 1 files changed, 33 insertions(+), 35 deletions(-)
kernel/trace/trace_events_filter.c +33 -35
@@ -1439,13 +1439,40 @@ static int count_leafs(struct filter_pred *preds, struct filter_pred *root)return count; } +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 ; +} +static int fold_pred (struct filter_pred *preds,struct filter_pred *root) { -struct filter_pred *pred; -enum move_type move =MOVE_DOWN ; -int count =0 ; +struct fold_pred_data data = { + .root = root, + .count =0 , + };int children; -int done =0 ;/* No need to keep the fold flag */ root->index &= ~FILTER_PRED_FOLD ;
@@ -1463,37 +1490,8 @@ static int fold_pred(struct filter_pred *preds, struct filter_pred *root)return -ENOMEM ; root->val = children; - - pred = root; -do { -switch (move) { -case MOVE_DOWN : -if (pred->left !=FILTER_PRED_INVALID ) { - pred = &preds[pred->left]; -continue ; - } -if (WARN_ON (count == children)) -return -EINVAL ; - pred->index &= ~FILTER_PRED_FOLD ; - root->ops[count++] = pred->index; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -case MOVE_UP_FROM_LEFT : - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); - -return 0 ; + data.children = children; +return walk_pred_tree (preds, root, fold_pred_cb, &data); } static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
Signature · 1 line
1.7.1
PATCH v2 9/10 tracing/filter: Change filter_match_preds function to use walk_pred_tree
In reply to Jiri OlsaChanging filter_match_preds function to use unified predicates tree processing.
--- kernel/trace/trace_events_filter.c | 124 +++++++++++++++++------------------- 1 files changed, 58 insertions(+), 66 deletions(-)
kernel/trace/trace_events_filter.c +58 -66
@@ -467,99 +467,91 @@ static int process_ops(struct filter_pred *preds,for (i =0 ; i < op->val; i++) { pred = &preds[op->ops[i]]; - match = pred->fn (pred, rec); +if (!WARN_ON_ONCE (!pred->fn)) + match = pred->fn (pred, rec);if (!!match == type)return match; }return match; } +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 ; +} +/* return 1 if event matches, 0 otherwise (discard) */ int filter_match_preds (struct event_filter *filter,void *rec) { -int match =-1 ; -enum move_type move =MOVE_DOWN ;struct filter_pred *preds; -struct filter_pred *pred;struct filter_pred *root; -int n_preds; -int done =0 ; +struct filter_match_preds_data data = { +/* match is currently meaningless */ + .match =-1 , + .rec = rec, + }; +int n_preds, ret;/* no filter is considered a match */ if (!filter)return 1 ; n_preds = filter->n_preds; -if (!n_preds)return 1 ;/* * n_preds, root and filter->preds are protect with preemption disabled. */ - preds =rcu_dereference_sched (filter->preds); root =rcu_dereference_sched (filter->root);if (!root)return 1 ; - pred = root; - -/* match is currently meaningless */ - match =-1 ; - -do { -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) { -/* keep going to down the left side */ - pred = &preds[pred->left]; -continue ; - } -/* We can treat folded ops as a leaf node */ - match =process_ops (preds, pred, rec); - }else - match = pred->fn (pred, rec); -/* If this pred is the only pred */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; -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 (!!match == (pred->op ==OP_OR )) { -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } -/* now go down the right side of the tree. */ - pred = &preds[pred->right]; - move =MOVE_DOWN ; -continue ; -case MOVE_UP_FROM_RIGHT : -/* We finished this equation. */ -if (pred == root) -break ; - pred =get_pred_parent (pred, preds, - pred->parent, &move); -continue ; - } - done =1 ; - }while (!done); - -return match; + 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; }EXPORT_SYMBOL_GPL (filter_match_preds);
Signature · 1 line
1.7.1
PATCH v2 10/10 tracing/filter: Add startup tests for events filter
In reply to Jiri OlsaAdding automated tests running as late_initcall. Tests are compiled in with CONFIG_FTRACE_STARTUP_TEST option.
Adding test event "ftrace_test_filter" used to simulate filter processing during event occurance.
String filters are compiled and tested against several test events with different values.
Also testing that evaluation of explicit predicates is ommited due to the lazy filter evaluation.
--- kernel/trace/Makefile | 2 + kernel/trace/trace.h | 3 + kernel/trace/trace_events_filter.c | 209 +++++++++++++++++++++++++++++++ kernel/trace/trace_events_filter_test.h | 50 ++++++++ 4 files changed, 264 insertions(+), 0 deletions(-) create mode 100644 kernel/trace/trace_events_filter_test.h
kernel/trace/Makefile +2 -0
@@ -15,6 +15,8 @@ ifdef CONFIG_TRACING_BRANCHES KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING endif +CFLAGS_trace_events_filter.o := -I$(src) + # # Make the trace clocks available generally: it's infrastructure # relied on by ptrace for example:
kernel/trace/trace.h +3 -0
@@ -762,6 +762,9 @@ struct filter_pred {u64 val;struct regex regex;unsigned short *ops; +#ifdef CONFIG_FTRACE_STARTUP_TEST +struct ftrace_event_field *field; +#endif int offset;int not;int op;
kernel/trace/trace_events_filter.c +209 -0
218 lines not on this page · raw message
kernel/trace/trace_events_filter_test.h new file +50 -0
50 lines not on this page · raw message
Signature · 1 line
1.7.1