Skip to content

Commit

Permalink
tracing/filters: allow on-the-fly filter switching
Browse files Browse the repository at this point in the history
This patch allows event filters to be safely removed or switched
on-the-fly while avoiding the use of rcu or the suspension of tracing of
previous versions.

It does it by adding a new filter_pred_none() predicate function which
does nothing and by never deallocating either the predicates or any of
the filter_pred members used in matching; the predicate lists are
allocated and initialized during ftrace_event_calls initialization.

Whenever a filter is removed or replaced, the filter_pred_* functions
currently in use by the affected ftrace_event_call are immediately
switched over to to the filter_pred_none() function, while the rest of
the filter_pred members are left intact, allowing any currently
executing filter_pred_* functions to finish up, using the values they're
currently using.

In the case of filter replacement, the new predicate values are copied
into the old predicates after the above step, and the filter_pred_none()
functions are replaced by the filter_pred_* functions for the new
filter.  In this case, it is possible though very unlikely that a
previous filter_pred_* is still running even after the
filter_pred_none() switch and the switch to the new filter_pred_*.  In
that case, however, because nothing has been deallocated in the
filter_pred, the worst that can happen is that the old filter_pred_*
function sees the new values and as a result produces either a false
positive or a false negative, depending on the values it finds.

So one downside to this method is that rarely, it can produce a bad
match during the filter switch, but it should be possible to live with
that, IMHO.

The other downside is that at least in this patch the predicate lists
are always pre-allocated, taking up memory from the start.  They could
probably be allocated on first-use, and de-allocated when tracing is
completely stopped - if this patch makes sense, I could create another
one to do that later on.

Oh, and it also places a restriction on the size of __arrays in events,
currently set to 128, since they can't be larger than the now embedded
str_val arrays in the filter_pred struct.

Signed-off-by: Tom Zanussi <[email protected]>
Acked-by: Frederic Weisbecker <[email protected]>
Cc: Steven Rostedt <[email protected]>
Cc: [email protected]
LKML-Reference: <1239610670.6660.49.camel@tropicana>
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
zanussi authored and Ingo Molnar committed Apr 13, 2009
1 parent b5c851a commit 0a19e53
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 128 deletions.
14 changes: 9 additions & 5 deletions kernel/trace/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ struct ftrace_event_call {
int (*show_format)(struct trace_seq *s);
int (*define_fields)(void);
struct list_head fields;
int n_preds;
struct filter_pred **preds;

#ifdef CONFIG_EVENT_PROFILE
Expand All @@ -826,6 +827,7 @@ struct event_subsystem {
struct list_head list;
const char *name;
struct dentry *entry;
int n_preds;
struct filter_pred **preds;
};

Expand All @@ -834,7 +836,8 @@ struct event_subsystem {
(unsigned long)event < (unsigned long)__stop_ftrace_events; \
event++)

#define MAX_FILTER_PRED 8
#define MAX_FILTER_PRED 8
#define MAX_FILTER_STR_VAL 128

struct filter_pred;

Expand All @@ -843,7 +846,7 @@ typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event);
struct filter_pred {
filter_pred_fn_t fn;
u64 val;
char *str_val;
char str_val[MAX_FILTER_STR_VAL];
int str_len;
char *field_name;
int offset;
Expand All @@ -855,13 +858,14 @@ struct filter_pred {

int trace_define_field(struct ftrace_event_call *call, char *type,
char *name, int offset, int size);
extern int init_preds(struct ftrace_event_call *call);
extern void filter_free_pred(struct filter_pred *pred);
extern void filter_print_preds(struct filter_pred **preds,
extern void filter_print_preds(struct filter_pred **preds, int n_preds,
struct trace_seq *s);
extern int filter_parse(char **pbuf, struct filter_pred *pred);
extern int filter_add_pred(struct ftrace_event_call *call,
struct filter_pred *pred);
extern void filter_free_preds(struct ftrace_event_call *call);
extern void filter_disable_preds(struct ftrace_event_call *call);
extern int filter_match_preds(struct ftrace_event_call *call, void *rec);
extern void filter_free_subsystem_preds(struct event_subsystem *system);
extern int filter_add_subsystem_pred(struct event_subsystem *system,
Expand All @@ -875,7 +879,7 @@ filter_check_discard(struct ftrace_event_call *call, void *rec,
struct ring_buffer *buffer,
struct ring_buffer_event *event)
{
if (unlikely(call->preds) && !filter_match_preds(call, rec)) {
if (unlikely(call->n_preds) && !filter_match_preds(call, rec)) {
ring_buffer_discard_commit(buffer, event);
return 1;
}
Expand Down
9 changes: 6 additions & 3 deletions kernel/trace/trace_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,

trace_seq_init(s);

filter_print_preds(call->preds, s);
filter_print_preds(call->preds, call->n_preds, s);
r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);

kfree(s);
Expand Down Expand Up @@ -516,7 +516,7 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
}

if (pred->clear) {
filter_free_preds(call);
filter_disable_preds(call);
filter_free_pred(pred);
return cnt;
}
Expand All @@ -527,6 +527,8 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
return err;
}

filter_free_pred(pred);

*ppos += cnt;

return cnt;
Expand All @@ -549,7 +551,7 @@ subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,

trace_seq_init(s);

filter_print_preds(system->preds, s);
filter_print_preds(system->preds, system->n_preds, s);
r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);

kfree(s);
Expand Down Expand Up @@ -712,6 +714,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
list_add(&system->list, &event_subsystems);

system->preds = NULL;
system->n_preds = 0;

entry = debugfs_create_file("filter", 0644, system->entry, system,
&ftrace_subsystem_filter_fops);
Expand Down
Loading

0 comments on commit 0a19e53

Please sign in to comment.