Commit b9b0c831 authored by Namhyung Kim's avatar Namhyung Kim Committed by Steven Rostedt (VMware)

ftrace: Convert graph filter to use hash tables

Use ftrace_hash instead of a static array of a fixed size.  This is
useful when a graph filter pattern matches to a large number of
functions.  Now hash lookup is done with preemption disabled to protect
from the hash being changed/freed.

Link: http://lkml.kernel.org/r/20170120024447.26097-3-namhyung@kernel.orgSigned-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Signed-off-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
parent 4046bf02
...@@ -4378,7 +4378,7 @@ __setup("ftrace_filter=", set_ftrace_filter); ...@@ -4378,7 +4378,7 @@ __setup("ftrace_filter=", set_ftrace_filter);
#ifdef CONFIG_FUNCTION_GRAPH_TRACER #ifdef CONFIG_FUNCTION_GRAPH_TRACER
static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata; static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer); static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
static unsigned long save_global_trampoline; static unsigned long save_global_trampoline;
static unsigned long save_global_flags; static unsigned long save_global_flags;
...@@ -4401,18 +4401,17 @@ static void __init set_ftrace_early_graph(char *buf, int enable) ...@@ -4401,18 +4401,17 @@ static void __init set_ftrace_early_graph(char *buf, int enable)
{ {
int ret; int ret;
char *func; char *func;
unsigned long *table = ftrace_graph_funcs; struct ftrace_hash *hash;
int *count = &ftrace_graph_count;
if (!enable) { if (enable)
table = ftrace_graph_notrace_funcs; hash = ftrace_graph_hash;
count = &ftrace_graph_notrace_count; else
} hash = ftrace_graph_notrace_hash;
while (buf) { while (buf) {
func = strsep(&buf, ","); func = strsep(&buf, ",");
/* we allow only one expression at a time */ /* we allow only one expression at a time */
ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func); ret = ftrace_graph_set_hash(hash, func);
if (ret) if (ret)
printk(KERN_DEBUG "ftrace: function %s not " printk(KERN_DEBUG "ftrace: function %s not "
"traceable\n", func); "traceable\n", func);
...@@ -4536,15 +4535,20 @@ static const struct file_operations ftrace_notrace_fops = { ...@@ -4536,15 +4535,20 @@ static const struct file_operations ftrace_notrace_fops = {
static DEFINE_MUTEX(graph_lock); static DEFINE_MUTEX(graph_lock);
int ftrace_graph_count; struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
int ftrace_graph_notrace_count; struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; enum graph_filter_type {
GRAPH_FILTER_NOTRACE = 0,
GRAPH_FILTER_FUNCTION,
};
struct ftrace_graph_data { struct ftrace_graph_data {
unsigned long *table; struct ftrace_hash *hash;
size_t size; struct ftrace_func_entry *entry;
int *count; int idx; /* for hash table iteration */
enum graph_filter_type type;
struct ftrace_hash *new_hash;
const struct seq_operations *seq_ops; const struct seq_operations *seq_ops;
}; };
...@@ -4552,10 +4556,31 @@ static void * ...@@ -4552,10 +4556,31 @@ static void *
__g_next(struct seq_file *m, loff_t *pos) __g_next(struct seq_file *m, loff_t *pos)
{ {
struct ftrace_graph_data *fgd = m->private; struct ftrace_graph_data *fgd = m->private;
struct ftrace_func_entry *entry = fgd->entry;
struct hlist_head *head;
int i, idx = fgd->idx;
if (*pos >= *fgd->count) if (*pos >= fgd->hash->count)
return NULL;
if (entry) {
hlist_for_each_entry_continue(entry, hlist) {
fgd->entry = entry;
return entry;
}
idx++;
}
for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
head = &fgd->hash->buckets[i];
hlist_for_each_entry(entry, head, hlist) {
fgd->entry = entry;
fgd->idx = i;
return entry;
}
}
return NULL; return NULL;
return &fgd->table[*pos];
} }
static void * static void *
...@@ -4572,9 +4597,11 @@ static void *g_start(struct seq_file *m, loff_t *pos) ...@@ -4572,9 +4597,11 @@ static void *g_start(struct seq_file *m, loff_t *pos)
mutex_lock(&graph_lock); mutex_lock(&graph_lock);
/* Nothing, tell g_show to print all functions are enabled */ /* Nothing, tell g_show to print all functions are enabled */
if (!*fgd->count && !*pos) if (ftrace_hash_empty(fgd->hash) && !*pos)
return (void *)1; return (void *)1;
fgd->idx = 0;
fgd->entry = NULL;
return __g_next(m, pos); return __g_next(m, pos);
} }
...@@ -4585,22 +4612,22 @@ static void g_stop(struct seq_file *m, void *p) ...@@ -4585,22 +4612,22 @@ static void g_stop(struct seq_file *m, void *p)
static int g_show(struct seq_file *m, void *v) static int g_show(struct seq_file *m, void *v)
{ {
unsigned long *ptr = v; struct ftrace_func_entry *entry = v;
if (!ptr) if (!entry)
return 0; return 0;
if (ptr == (unsigned long *)1) { if (entry == (void *)1) {
struct ftrace_graph_data *fgd = m->private; struct ftrace_graph_data *fgd = m->private;
if (fgd->table == ftrace_graph_funcs) if (fgd->type == GRAPH_FILTER_FUNCTION)
seq_puts(m, "#### all functions enabled ####\n"); seq_puts(m, "#### all functions enabled ####\n");
else else
seq_puts(m, "#### no functions disabled ####\n"); seq_puts(m, "#### no functions disabled ####\n");
return 0; return 0;
} }
seq_printf(m, "%ps\n", (void *)*ptr); seq_printf(m, "%ps\n", (void *)entry->ip);
return 0; return 0;
} }
...@@ -4617,24 +4644,37 @@ __ftrace_graph_open(struct inode *inode, struct file *file, ...@@ -4617,24 +4644,37 @@ __ftrace_graph_open(struct inode *inode, struct file *file,
struct ftrace_graph_data *fgd) struct ftrace_graph_data *fgd)
{ {
int ret = 0; int ret = 0;
struct ftrace_hash *new_hash = NULL;
mutex_lock(&graph_lock); if (file->f_mode & FMODE_WRITE) {
if ((file->f_mode & FMODE_WRITE) && const int size_bits = FTRACE_HASH_DEFAULT_BITS;
(file->f_flags & O_TRUNC)) {
*fgd->count = 0; if (file->f_flags & O_TRUNC)
memset(fgd->table, 0, fgd->size * sizeof(*fgd->table)); new_hash = alloc_ftrace_hash(size_bits);
else
new_hash = alloc_and_copy_ftrace_hash(size_bits,
fgd->hash);
if (!new_hash) {
ret = -ENOMEM;
goto out;
}
} }
mutex_unlock(&graph_lock);
if (file->f_mode & FMODE_READ) { if (file->f_mode & FMODE_READ) {
ret = seq_open(file, fgd->seq_ops); ret = seq_open(file, &ftrace_graph_seq_ops);
if (!ret) { if (!ret) {
struct seq_file *m = file->private_data; struct seq_file *m = file->private_data;
m->private = fgd; m->private = fgd;
} else {
/* Failed */
free_ftrace_hash(new_hash);
new_hash = NULL;
} }
} else } else
file->private_data = fgd; file->private_data = fgd;
out:
fgd->new_hash = new_hash;
return ret; return ret;
} }
...@@ -4642,6 +4682,7 @@ static int ...@@ -4642,6 +4682,7 @@ static int
ftrace_graph_open(struct inode *inode, struct file *file) ftrace_graph_open(struct inode *inode, struct file *file)
{ {
struct ftrace_graph_data *fgd; struct ftrace_graph_data *fgd;
int ret;
if (unlikely(ftrace_disabled)) if (unlikely(ftrace_disabled))
return -ENODEV; return -ENODEV;
...@@ -4650,18 +4691,25 @@ ftrace_graph_open(struct inode *inode, struct file *file) ...@@ -4650,18 +4691,25 @@ ftrace_graph_open(struct inode *inode, struct file *file)
if (fgd == NULL) if (fgd == NULL)
return -ENOMEM; return -ENOMEM;
fgd->table = ftrace_graph_funcs; mutex_lock(&graph_lock);
fgd->size = FTRACE_GRAPH_MAX_FUNCS;
fgd->count = &ftrace_graph_count; fgd->hash = ftrace_graph_hash;
fgd->type = GRAPH_FILTER_FUNCTION;
fgd->seq_ops = &ftrace_graph_seq_ops; fgd->seq_ops = &ftrace_graph_seq_ops;
return __ftrace_graph_open(inode, file, fgd); ret = __ftrace_graph_open(inode, file, fgd);
if (ret < 0)
kfree(fgd);
mutex_unlock(&graph_lock);
return ret;
} }
static int static int
ftrace_graph_notrace_open(struct inode *inode, struct file *file) ftrace_graph_notrace_open(struct inode *inode, struct file *file)
{ {
struct ftrace_graph_data *fgd; struct ftrace_graph_data *fgd;
int ret;
if (unlikely(ftrace_disabled)) if (unlikely(ftrace_disabled))
return -ENODEV; return -ENODEV;
...@@ -4670,45 +4718,53 @@ ftrace_graph_notrace_open(struct inode *inode, struct file *file) ...@@ -4670,45 +4718,53 @@ ftrace_graph_notrace_open(struct inode *inode, struct file *file)
if (fgd == NULL) if (fgd == NULL)
return -ENOMEM; return -ENOMEM;
fgd->table = ftrace_graph_notrace_funcs; mutex_lock(&graph_lock);
fgd->size = FTRACE_GRAPH_MAX_FUNCS;
fgd->count = &ftrace_graph_notrace_count; fgd->hash = ftrace_graph_notrace_hash;
fgd->type = GRAPH_FILTER_NOTRACE;
fgd->seq_ops = &ftrace_graph_seq_ops; fgd->seq_ops = &ftrace_graph_seq_ops;
return __ftrace_graph_open(inode, file, fgd); ret = __ftrace_graph_open(inode, file, fgd);
if (ret < 0)
kfree(fgd);
mutex_unlock(&graph_lock);
return ret;
} }
static int static int
ftrace_graph_release(struct inode *inode, struct file *file) ftrace_graph_release(struct inode *inode, struct file *file)
{ {
struct ftrace_graph_data *fgd;
if (file->f_mode & FMODE_READ) { if (file->f_mode & FMODE_READ) {
struct seq_file *m = file->private_data; struct seq_file *m = file->private_data;
kfree(m->private); fgd = m->private;
seq_release(inode, file); seq_release(inode, file);
} else { } else {
kfree(file->private_data); fgd = file->private_data;
} }
kfree(fgd->new_hash);
kfree(fgd);
return 0; return 0;
} }
static int static int
ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
{ {
struct ftrace_glob func_g; struct ftrace_glob func_g;
struct dyn_ftrace *rec; struct dyn_ftrace *rec;
struct ftrace_page *pg; struct ftrace_page *pg;
struct ftrace_func_entry *entry;
int fail = 1; int fail = 1;
int not; int not;
bool exists;
int i;
/* decode regex */ /* decode regex */
func_g.type = filter_parse_regex(buffer, strlen(buffer), func_g.type = filter_parse_regex(buffer, strlen(buffer),
&func_g.search, &not); &func_g.search, &not);
if (!not && *idx >= size)
return -EBUSY;
func_g.len = strlen(func_g.search); func_g.len = strlen(func_g.search);
...@@ -4725,26 +4781,18 @@ ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) ...@@ -4725,26 +4781,18 @@ ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
continue; continue;
if (ftrace_match_record(rec, &func_g, NULL, 0)) { if (ftrace_match_record(rec, &func_g, NULL, 0)) {
/* if it is in the array */ entry = ftrace_lookup_ip(hash, rec->ip);
exists = false;
for (i = 0; i < *idx; i++) {
if (array[i] == rec->ip) {
exists = true;
break;
}
}
if (!not) { if (!not) {
fail = 0; fail = 0;
if (!exists) {
array[(*idx)++] = rec->ip; if (entry)
if (*idx >= size) continue;
if (add_hash_entry(hash, rec->ip) < 0)
goto out; goto out;
}
} else { } else {
if (exists) { if (entry) {
array[i] = array[--(*idx)]; free_hash_entry(hash, entry);
array[*idx] = 0;
fail = 0; fail = 0;
} }
} }
...@@ -4766,6 +4814,7 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, ...@@ -4766,6 +4814,7 @@ ftrace_graph_write(struct file *file, const char __user *ubuf,
struct trace_parser parser; struct trace_parser parser;
ssize_t read, ret = 0; ssize_t read, ret = 0;
struct ftrace_graph_data *fgd = file->private_data; struct ftrace_graph_data *fgd = file->private_data;
struct ftrace_hash *old_hash, *new_hash;
if (!cnt) if (!cnt)
return 0; return 0;
...@@ -4781,10 +4830,25 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, ...@@ -4781,10 +4830,25 @@ ftrace_graph_write(struct file *file, const char __user *ubuf,
mutex_lock(&graph_lock); mutex_lock(&graph_lock);
/* we allow only one expression at a time */ /* we allow only one expression at a time */
ret = ftrace_set_func(fgd->table, fgd->count, fgd->size, ret = ftrace_graph_set_hash(fgd->new_hash,
parser.buffer); parser.buffer);
old_hash = fgd->hash;
new_hash = __ftrace_hash_move(fgd->new_hash);
if (!new_hash)
ret = -ENOMEM;
if (fgd->type == GRAPH_FILTER_FUNCTION)
rcu_assign_pointer(ftrace_graph_hash, new_hash);
else
rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
mutex_unlock(&graph_lock); mutex_unlock(&graph_lock);
/* Wait till all users are no longer using the old hash */
synchronize_sched();
free_ftrace_hash(old_hash);
} }
if (!ret) if (!ret)
......
...@@ -803,22 +803,21 @@ extern void __trace_graph_return(struct trace_array *tr, ...@@ -803,22 +803,21 @@ extern void __trace_graph_return(struct trace_array *tr,
unsigned long flags, int pc); unsigned long flags, int pc);
#ifdef CONFIG_DYNAMIC_FTRACE #ifdef CONFIG_DYNAMIC_FTRACE
/* TODO: make this variable */ extern struct ftrace_hash *ftrace_graph_hash;
#define FTRACE_GRAPH_MAX_FUNCS 32 extern struct ftrace_hash *ftrace_graph_notrace_hash;
extern int ftrace_graph_count;
extern unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS];
extern int ftrace_graph_notrace_count;
extern unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS];
static inline int ftrace_graph_addr(unsigned long addr) static inline int ftrace_graph_addr(unsigned long addr)
{ {
int i; int ret = 0;
if (!ftrace_graph_count) preempt_disable_notrace();
return 1;
if (ftrace_hash_empty(ftrace_graph_hash)) {
ret = 1;
goto out;
}
for (i = 0; i < ftrace_graph_count; i++) { if (ftrace_lookup_ip(ftrace_graph_hash, addr)) {
if (addr == ftrace_graph_funcs[i]) {
/* /*
* If no irqs are to be traced, but a set_graph_function * If no irqs are to be traced, but a set_graph_function
* is set, and called by an interrupt handler, we still * is set, and called by an interrupt handler, we still
...@@ -828,26 +827,25 @@ static inline int ftrace_graph_addr(unsigned long addr) ...@@ -828,26 +827,25 @@ static inline int ftrace_graph_addr(unsigned long addr)
trace_recursion_set(TRACE_IRQ_BIT); trace_recursion_set(TRACE_IRQ_BIT);
else else
trace_recursion_clear(TRACE_IRQ_BIT); trace_recursion_clear(TRACE_IRQ_BIT);
return 1; ret = 1;
}
} }
return 0; out:
preempt_enable_notrace();
return ret;
} }
static inline int ftrace_graph_notrace_addr(unsigned long addr) static inline int ftrace_graph_notrace_addr(unsigned long addr)
{ {
int i; int ret = 0;
if (!ftrace_graph_notrace_count) preempt_disable_notrace();
return 0;
for (i = 0; i < ftrace_graph_notrace_count; i++) { if (ftrace_lookup_ip(ftrace_graph_notrace_hash, addr))
if (addr == ftrace_graph_notrace_funcs[i]) ret = 1;
return 1;
}
return 0; preempt_enable_notrace();
return ret;
} }
#else #else
static inline int ftrace_graph_addr(unsigned long addr) static inline int ftrace_graph_addr(unsigned long addr)
......
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