Commit 0a19e53c authored by Tom Zanussi's avatar Tom Zanussi Committed by Ingo Molnar

tracing/filters: allow on-the-fly filter switching

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: default avatarTom Zanussi <tzanussi@gmail.com>
Acked-by: default avatarFrederic Weisbecker <fweisbec@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: paulmck@linux.vnet.ibm.com
LKML-Reference: <1239610670.6660.49.camel@tropicana>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent b5c851a8
...@@ -813,6 +813,7 @@ struct ftrace_event_call { ...@@ -813,6 +813,7 @@ struct ftrace_event_call {
int (*show_format)(struct trace_seq *s); int (*show_format)(struct trace_seq *s);
int (*define_fields)(void); int (*define_fields)(void);
struct list_head fields; struct list_head fields;
int n_preds;
struct filter_pred **preds; struct filter_pred **preds;
#ifdef CONFIG_EVENT_PROFILE #ifdef CONFIG_EVENT_PROFILE
...@@ -826,6 +827,7 @@ struct event_subsystem { ...@@ -826,6 +827,7 @@ struct event_subsystem {
struct list_head list; struct list_head list;
const char *name; const char *name;
struct dentry *entry; struct dentry *entry;
int n_preds;
struct filter_pred **preds; struct filter_pred **preds;
}; };
...@@ -834,7 +836,8 @@ struct event_subsystem { ...@@ -834,7 +836,8 @@ struct event_subsystem {
(unsigned long)event < (unsigned long)__stop_ftrace_events; \ (unsigned long)event < (unsigned long)__stop_ftrace_events; \
event++) event++)
#define MAX_FILTER_PRED 8 #define MAX_FILTER_PRED 8
#define MAX_FILTER_STR_VAL 128
struct filter_pred; struct filter_pred;
...@@ -843,7 +846,7 @@ typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event); ...@@ -843,7 +846,7 @@ typedef int (*filter_pred_fn_t) (struct filter_pred *pred, void *event);
struct filter_pred { struct filter_pred {
filter_pred_fn_t fn; filter_pred_fn_t fn;
u64 val; u64 val;
char *str_val; char str_val[MAX_FILTER_STR_VAL];
int str_len; int str_len;
char *field_name; char *field_name;
int offset; int offset;
...@@ -855,13 +858,14 @@ struct filter_pred { ...@@ -855,13 +858,14 @@ struct filter_pred {
int trace_define_field(struct ftrace_event_call *call, char *type, int trace_define_field(struct ftrace_event_call *call, char *type,
char *name, int offset, int size); 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_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); struct trace_seq *s);
extern int filter_parse(char **pbuf, struct filter_pred *pred); extern int filter_parse(char **pbuf, struct filter_pred *pred);
extern int filter_add_pred(struct ftrace_event_call *call, extern int filter_add_pred(struct ftrace_event_call *call,
struct filter_pred *pred); 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 int filter_match_preds(struct ftrace_event_call *call, void *rec);
extern void filter_free_subsystem_preds(struct event_subsystem *system); extern void filter_free_subsystem_preds(struct event_subsystem *system);
extern int filter_add_subsystem_pred(struct event_subsystem *system, extern int filter_add_subsystem_pred(struct event_subsystem *system,
...@@ -875,7 +879,7 @@ filter_check_discard(struct ftrace_event_call *call, void *rec, ...@@ -875,7 +879,7 @@ filter_check_discard(struct ftrace_event_call *call, void *rec,
struct ring_buffer *buffer, struct ring_buffer *buffer,
struct ring_buffer_event *event) 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); ring_buffer_discard_commit(buffer, event);
return 1; return 1;
} }
......
...@@ -481,7 +481,7 @@ event_filter_read(struct file *filp, char __user *ubuf, size_t cnt, ...@@ -481,7 +481,7 @@ event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
trace_seq_init(s); 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); r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
kfree(s); kfree(s);
...@@ -516,7 +516,7 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, ...@@ -516,7 +516,7 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
} }
if (pred->clear) { if (pred->clear) {
filter_free_preds(call); filter_disable_preds(call);
filter_free_pred(pred); filter_free_pred(pred);
return cnt; return cnt;
} }
...@@ -527,6 +527,8 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt, ...@@ -527,6 +527,8 @@ event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
return err; return err;
} }
filter_free_pred(pred);
*ppos += cnt; *ppos += cnt;
return cnt; return cnt;
...@@ -549,7 +551,7 @@ subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt, ...@@ -549,7 +551,7 @@ subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
trace_seq_init(s); 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); r = simple_read_from_buffer(ubuf, cnt, ppos, s->buffer, s->len);
kfree(s); kfree(s);
...@@ -712,6 +714,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events) ...@@ -712,6 +714,7 @@ event_subsystem_dir(const char *name, struct dentry *d_events)
list_add(&system->list, &event_subsystems); list_add(&system->list, &event_subsystems);
system->preds = NULL; system->preds = NULL;
system->n_preds = 0;
entry = debugfs_create_file("filter", 0644, system->entry, system, entry = debugfs_create_file("filter", 0644, system->entry, system,
&ftrace_subsystem_filter_fops); &ftrace_subsystem_filter_fops);
......
...@@ -82,25 +82,27 @@ static int filter_pred_string(struct filter_pred *pred, void *event) ...@@ -82,25 +82,27 @@ static int filter_pred_string(struct filter_pred *pred, void *event)
return match; return match;
} }
static int filter_pred_none(struct filter_pred *pred, void *event)
{
return 0;
}
/* return 1 if event matches, 0 otherwise (discard) */ /* return 1 if event matches, 0 otherwise (discard) */
int filter_match_preds(struct ftrace_event_call *call, void *rec) int filter_match_preds(struct ftrace_event_call *call, void *rec)
{ {
int i, matched, and_failed = 0; int i, matched, and_failed = 0;
struct filter_pred *pred; struct filter_pred *pred;
for (i = 0; i < MAX_FILTER_PRED; i++) { for (i = 0; i < call->n_preds; i++) {
if (call->preds[i]) { pred = call->preds[i];
pred = call->preds[i]; if (and_failed && !pred->or)
if (and_failed && !pred->or) continue;
continue; matched = pred->fn(pred, rec);
matched = pred->fn(pred, rec); if (!matched && !pred->or) {
if (!matched && !pred->or) { and_failed = 1;
and_failed = 1; continue;
continue; } else if (matched && pred->or)
} else if (matched && pred->or) return 1;
return 1;
} else
break;
} }
if (and_failed) if (and_failed)
...@@ -109,31 +111,29 @@ int filter_match_preds(struct ftrace_event_call *call, void *rec) ...@@ -109,31 +111,29 @@ int filter_match_preds(struct ftrace_event_call *call, void *rec)
return 1; return 1;
} }
void filter_print_preds(struct filter_pred **preds, struct trace_seq *s) void filter_print_preds(struct filter_pred **preds, int n_preds,
struct trace_seq *s)
{ {
char *field_name; char *field_name;
struct filter_pred *pred; struct filter_pred *pred;
int i; int i;
if (!preds) { if (!n_preds) {
trace_seq_printf(s, "none\n"); trace_seq_printf(s, "none\n");
return; return;
} }
for (i = 0; i < MAX_FILTER_PRED; i++) { for (i = 0; i < n_preds; i++) {
if (preds[i]) { pred = preds[i];
pred = preds[i]; field_name = pred->field_name;
field_name = pred->field_name; if (i)
if (i) trace_seq_printf(s, pred->or ? "|| " : "&& ");
trace_seq_printf(s, pred->or ? "|| " : "&& "); trace_seq_printf(s, "%s ", field_name);
trace_seq_printf(s, "%s ", field_name); trace_seq_printf(s, pred->not ? "!= " : "== ");
trace_seq_printf(s, pred->not ? "!= " : "== "); if (pred->str_len)
if (pred->str_val) trace_seq_printf(s, "%s\n", pred->str_val);
trace_seq_printf(s, "%s\n", pred->str_val); else
else trace_seq_printf(s, "%llu\n", pred->val);
trace_seq_printf(s, "%llu\n", pred->val);
} else
break;
} }
} }
...@@ -156,20 +156,69 @@ void filter_free_pred(struct filter_pred *pred) ...@@ -156,20 +156,69 @@ void filter_free_pred(struct filter_pred *pred)
return; return;
kfree(pred->field_name); kfree(pred->field_name);
kfree(pred->str_val);
kfree(pred); kfree(pred);
} }
void filter_free_preds(struct ftrace_event_call *call) static void filter_clear_pred(struct filter_pred *pred)
{
kfree(pred->field_name);
pred->field_name = NULL;
pred->str_len = 0;
}
static int filter_set_pred(struct filter_pred *dest,
struct filter_pred *src,
filter_pred_fn_t fn)
{
*dest = *src;
dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
if (!dest->field_name)
return -ENOMEM;
dest->fn = fn;
return 0;
}
void filter_disable_preds(struct ftrace_event_call *call)
{ {
int i; int i;
if (call->preds) { call->n_preds = 0;
for (i = 0; i < MAX_FILTER_PRED; i++)
for (i = 0; i < MAX_FILTER_PRED; i++)
call->preds[i]->fn = filter_pred_none;
}
int init_preds(struct ftrace_event_call *call)
{
struct filter_pred *pred;
int i;
call->n_preds = 0;
call->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL);
if (!call->preds)
return -ENOMEM;
for (i = 0; i < MAX_FILTER_PRED; i++) {
pred = kzalloc(sizeof(*pred), GFP_KERNEL);
if (!pred)
goto oom;
pred->fn = filter_pred_none;
call->preds[i] = pred;
}
return 0;
oom:
for (i = 0; i < MAX_FILTER_PRED; i++) {
if (call->preds[i])
filter_free_pred(call->preds[i]); filter_free_pred(call->preds[i]);
kfree(call->preds);
call->preds = NULL;
} }
kfree(call->preds);
call->preds = NULL;
return -ENOMEM;
} }
void filter_free_subsystem_preds(struct event_subsystem *system) void filter_free_subsystem_preds(struct event_subsystem *system)
...@@ -177,11 +226,12 @@ void filter_free_subsystem_preds(struct event_subsystem *system) ...@@ -177,11 +226,12 @@ void filter_free_subsystem_preds(struct event_subsystem *system)
struct ftrace_event_call *call = __start_ftrace_events; struct ftrace_event_call *call = __start_ftrace_events;
int i; int i;
if (system->preds) { if (system->n_preds) {
for (i = 0; i < MAX_FILTER_PRED; i++) for (i = 0; i < system->n_preds; i++)
filter_free_pred(system->preds[i]); filter_free_pred(system->preds[i]);
kfree(system->preds); kfree(system->preds);
system->preds = NULL; system->preds = NULL;
system->n_preds = 0;
} }
events_for_each(call) { events_for_each(call) {
...@@ -189,33 +239,31 @@ void filter_free_subsystem_preds(struct event_subsystem *system) ...@@ -189,33 +239,31 @@ void filter_free_subsystem_preds(struct event_subsystem *system)
continue; continue;
if (!strcmp(call->system, system->name)) if (!strcmp(call->system, system->name))
filter_free_preds(call); filter_disable_preds(call);
} }
} }
static int __filter_add_pred(struct ftrace_event_call *call, static int __filter_add_pred(struct ftrace_event_call *call,
struct filter_pred *pred) struct filter_pred *pred,
filter_pred_fn_t fn)
{ {
int i; int idx, err;
if (call->preds && !pred->compound) if (call->n_preds && !pred->compound)
filter_free_preds(call); filter_disable_preds(call);
if (!call->preds) { if (call->n_preds == MAX_FILTER_PRED)
call->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), return -ENOSPC;
GFP_KERNEL);
if (!call->preds)
return -ENOMEM;
}
for (i = 0; i < MAX_FILTER_PRED; i++) { idx = call->n_preds;
if (!call->preds[i]) { filter_clear_pred(call->preds[idx]);
call->preds[i] = pred; err = filter_set_pred(call->preds[idx], pred, fn);
return 0; if (err)
} return err;
}
call->n_preds++;
return -ENOSPC; return 0;
} }
static int is_string_field(const char *type) static int is_string_field(const char *type)
...@@ -229,98 +277,66 @@ static int is_string_field(const char *type) ...@@ -229,98 +277,66 @@ static int is_string_field(const char *type)
int filter_add_pred(struct ftrace_event_call *call, struct filter_pred *pred) int filter_add_pred(struct ftrace_event_call *call, struct filter_pred *pred)
{ {
struct ftrace_event_field *field; struct ftrace_event_field *field;
filter_pred_fn_t fn;
field = find_event_field(call, pred->field_name); field = find_event_field(call, pred->field_name);
if (!field) if (!field)
return -EINVAL; return -EINVAL;
pred->fn = filter_pred_none;
pred->offset = field->offset; pred->offset = field->offset;
if (is_string_field(field->type)) { if (is_string_field(field->type)) {
if (!pred->str_val) if (!pred->str_len)
return -EINVAL; return -EINVAL;
pred->fn = filter_pred_string; fn = filter_pred_string;
pred->str_len = field->size; pred->str_len = field->size;
return __filter_add_pred(call, pred); return __filter_add_pred(call, pred, fn);
} else { } else {
if (pred->str_val) if (pred->str_len)
return -EINVAL; return -EINVAL;
} }
switch (field->size) { switch (field->size) {
case 8: case 8:
pred->fn = filter_pred_64; fn = filter_pred_64;
break; break;
case 4: case 4:
pred->fn = filter_pred_32; fn = filter_pred_32;
break; break;
case 2: case 2:
pred->fn = filter_pred_16; fn = filter_pred_16;
break; break;
case 1: case 1:
pred->fn = filter_pred_8; fn = filter_pred_8;
break; break;
default: default:
return -EINVAL; return -EINVAL;
} }
return __filter_add_pred(call, pred); return __filter_add_pred(call, pred, fn);
}
static struct filter_pred *copy_pred(struct filter_pred *pred)
{
struct filter_pred *new_pred = kmalloc(sizeof(*pred), GFP_KERNEL);
if (!new_pred)
return NULL;
memcpy(new_pred, pred, sizeof(*pred));
if (pred->field_name) {
new_pred->field_name = kstrdup(pred->field_name, GFP_KERNEL);
if (!new_pred->field_name) {
kfree(new_pred);
return NULL;
}
}
if (pred->str_val) {
new_pred->str_val = kstrdup(pred->str_val, GFP_KERNEL);
if (!new_pred->str_val) {
filter_free_pred(new_pred);
return NULL;
}
}
return new_pred;
} }
int filter_add_subsystem_pred(struct event_subsystem *system, int filter_add_subsystem_pred(struct event_subsystem *system,
struct filter_pred *pred) struct filter_pred *pred)
{ {
struct ftrace_event_call *call = __start_ftrace_events; struct ftrace_event_call *call = __start_ftrace_events;
struct filter_pred *event_pred;
int i;
if (system->preds && !pred->compound) if (system->n_preds && !pred->compound)
filter_free_subsystem_preds(system); filter_free_subsystem_preds(system);
if (!system->preds) { if (!system->n_preds) {
system->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), system->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred),
GFP_KERNEL); GFP_KERNEL);
if (!system->preds) if (!system->preds)
return -ENOMEM; return -ENOMEM;
} }
for (i = 0; i < MAX_FILTER_PRED; i++) { if (system->n_preds == MAX_FILTER_PRED)
if (!system->preds[i]) {
system->preds[i] = pred;
break;
}
}
if (i == MAX_FILTER_PRED)
return -ENOSPC; return -ENOSPC;
system->preds[system->n_preds] = pred;
events_for_each(call) { events_for_each(call) {
int err; int err;
...@@ -333,22 +349,16 @@ int filter_add_subsystem_pred(struct event_subsystem *system, ...@@ -333,22 +349,16 @@ int filter_add_subsystem_pred(struct event_subsystem *system,
if (!find_event_field(call, pred->field_name)) if (!find_event_field(call, pred->field_name))
continue; continue;
event_pred = copy_pred(pred); err = filter_add_pred(call, pred);
if (!event_pred) if (err == -ENOMEM) {
goto oom; system->preds[system->n_preds] = NULL;
return err;
err = filter_add_pred(call, event_pred); }
if (err)
filter_free_pred(event_pred);
if (err == -ENOMEM)
goto oom;
} }
return 0; system->n_preds++;
oom: return 0;
system->preds[i] = NULL;
return -ENOMEM;
} }
int filter_parse(char **pbuf, struct filter_pred *pred) int filter_parse(char **pbuf, struct filter_pred *pred)
...@@ -410,7 +420,8 @@ int filter_parse(char **pbuf, struct filter_pred *pred) ...@@ -410,7 +420,8 @@ int filter_parse(char **pbuf, struct filter_pred *pred)
} }
} }
if (!val_str) { if (!val_str || !strlen(val_str)
|| strlen(val_str) >= MAX_FILTER_STR_VAL) {
pred->field_name = NULL; pred->field_name = NULL;
return -EINVAL; return -EINVAL;
} }
...@@ -419,11 +430,12 @@ int filter_parse(char **pbuf, struct filter_pred *pred) ...@@ -419,11 +430,12 @@ int filter_parse(char **pbuf, struct filter_pred *pred)
if (!pred->field_name) if (!pred->field_name)
return -ENOMEM; return -ENOMEM;
pred->str_len = 0;
pred->val = simple_strtoull(val_str, &tmp, 0); pred->val = simple_strtoull(val_str, &tmp, 0);
if (tmp == val_str) { if (tmp == val_str) {
pred->str_val = kstrdup(val_str, GFP_KERNEL); strncpy(pred->str_val, val_str, MAX_FILTER_STR_VAL);
if (!pred->str_val) pred->str_len = strlen(val_str);
return -ENOMEM; pred->str_val[pred->str_len] = '\0';
} else if (*tmp != '\0') } else if (*tmp != '\0')
return -EINVAL; return -EINVAL;
......
...@@ -140,6 +140,7 @@ ftrace_format_##call(struct trace_seq *s) \ ...@@ -140,6 +140,7 @@ ftrace_format_##call(struct trace_seq *s) \
#undef __array #undef __array
#define __array(type, item, len) \ #define __array(type, item, len) \
BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
ret = trace_define_field(event_call, #type "[" #len "]", #item, \ ret = trace_define_field(event_call, #type "[" #len "]", #item, \
offsetof(typeof(field), item), \ offsetof(typeof(field), item), \
sizeof(field.item)); \ sizeof(field.item)); \
......
...@@ -255,6 +255,7 @@ static int ftrace_raw_init_event_##call(void) \ ...@@ -255,6 +255,7 @@ static int ftrace_raw_init_event_##call(void) \
return -ENODEV; \ return -ENODEV; \
event_##call.id = id; \ event_##call.id = id; \
INIT_LIST_HEAD(&event_##call.fields); \ INIT_LIST_HEAD(&event_##call.fields); \
init_preds(&event_##call); \
return 0; \ return 0; \
} \ } \
\ \
......
...@@ -122,6 +122,7 @@ __attribute__((section("_ftrace_events"))) event_##call = { \ ...@@ -122,6 +122,7 @@ __attribute__((section("_ftrace_events"))) event_##call = { \
static int ftrace_raw_init_event_##call(void) \ static int ftrace_raw_init_event_##call(void) \
{ \ { \
INIT_LIST_HEAD(&event_##call.fields); \ INIT_LIST_HEAD(&event_##call.fields); \
init_preds(&event_##call); \
return 0; \ return 0; \
} \ } \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment