Skip to content

Commit e704eff

Browse files
committed
ftrace: Have set_graph_function handle multiple functions in one write
Currently, only one function can be written to set_graph_function and set_graph_notrace. The last function in the list will have saved, even though other functions will be added then removed. Change the behavior to be the same as set_ftrace_function as to allow multiple functions to be written. If any one fails, none of them will be added. The addition of the functions are done at the end when the file is closed. Acked-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent 649b988 commit e704eff

1 file changed

Lines changed: 64 additions & 41 deletions

File tree

‎kernel/trace/ftrace.c‎

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,12 +4564,13 @@ enum graph_filter_type {
45644564
#define FTRACE_GRAPH_EMPTY ((void *)1)
45654565

45664566
struct ftrace_graph_data {
4567-
struct ftrace_hash *hash;
4568-
struct ftrace_func_entry *entry;
4569-
int idx; /* for hash table iteration */
4570-
enum graph_filter_type type;
4571-
struct ftrace_hash *new_hash;
4572-
const struct seq_operations *seq_ops;
4567+
struct ftrace_hash *hash;
4568+
struct ftrace_func_entry *entry;
4569+
int idx; /* for hash table iteration */
4570+
enum graph_filter_type type;
4571+
struct ftrace_hash *new_hash;
4572+
const struct seq_operations *seq_ops;
4573+
struct trace_parser parser;
45734574
};
45744575

45754576
static void *
@@ -4676,6 +4677,9 @@ __ftrace_graph_open(struct inode *inode, struct file *file,
46764677
if (file->f_mode & FMODE_WRITE) {
46774678
const int size_bits = FTRACE_HASH_DEFAULT_BITS;
46784679

4680+
if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
4681+
return -ENOMEM;
4682+
46794683
if (file->f_flags & O_TRUNC)
46804684
new_hash = alloc_ftrace_hash(size_bits);
46814685
else
@@ -4701,6 +4705,9 @@ __ftrace_graph_open(struct inode *inode, struct file *file,
47014705
file->private_data = fgd;
47024706

47034707
out:
4708+
if (ret < 0 && file->f_mode & FMODE_WRITE)
4709+
trace_parser_put(&fgd->parser);
4710+
47044711
fgd->new_hash = new_hash;
47054712

47064713
/*
@@ -4773,6 +4780,9 @@ static int
47734780
ftrace_graph_release(struct inode *inode, struct file *file)
47744781
{
47754782
struct ftrace_graph_data *fgd;
4783+
struct ftrace_hash *old_hash, *new_hash;
4784+
struct trace_parser *parser;
4785+
int ret = 0;
47764786

47774787
if (file->f_mode & FMODE_READ) {
47784788
struct seq_file *m = file->private_data;
@@ -4783,10 +4793,50 @@ ftrace_graph_release(struct inode *inode, struct file *file)
47834793
fgd = file->private_data;
47844794
}
47854795

4796+
4797+
if (file->f_mode & FMODE_WRITE) {
4798+
4799+
parser = &fgd->parser;
4800+
4801+
if (trace_parser_loaded((parser))) {
4802+
parser->buffer[parser->idx] = 0;
4803+
ret = ftrace_graph_set_hash(fgd->new_hash,
4804+
parser->buffer);
4805+
}
4806+
4807+
trace_parser_put(parser);
4808+
4809+
new_hash = __ftrace_hash_move(fgd->new_hash);
4810+
if (!new_hash) {
4811+
ret = -ENOMEM;
4812+
goto out;
4813+
}
4814+
4815+
mutex_lock(&graph_lock);
4816+
4817+
if (fgd->type == GRAPH_FILTER_FUNCTION) {
4818+
old_hash = rcu_dereference_protected(ftrace_graph_hash,
4819+
lockdep_is_held(&graph_lock));
4820+
rcu_assign_pointer(ftrace_graph_hash, new_hash);
4821+
} else {
4822+
old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4823+
lockdep_is_held(&graph_lock));
4824+
rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
4825+
}
4826+
4827+
mutex_unlock(&graph_lock);
4828+
4829+
/* Wait till all users are no longer using the old hash */
4830+
synchronize_sched();
4831+
4832+
free_ftrace_hash(old_hash);
4833+
}
4834+
4835+
out:
47864836
kfree(fgd->new_hash);
47874837
kfree(fgd);
47884838

4789-
return 0;
4839+
return ret;
47904840
}
47914841

47924842
static int
@@ -4848,61 +4898,34 @@ static ssize_t
48484898
ftrace_graph_write(struct file *file, const char __user *ubuf,
48494899
size_t cnt, loff_t *ppos)
48504900
{
4851-
struct trace_parser parser;
48524901
ssize_t read, ret = 0;
48534902
struct ftrace_graph_data *fgd = file->private_data;
4854-
struct ftrace_hash *old_hash, *new_hash;
4903+
struct trace_parser *parser;
48554904

48564905
if (!cnt)
48574906
return 0;
48584907

4859-
if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4860-
return -ENOMEM;
4861-
48624908
/* Read mode uses seq functions */
48634909
if (file->f_mode & FMODE_READ) {
48644910
struct seq_file *m = file->private_data;
48654911
fgd = m->private;
48664912
}
48674913

4868-
read = trace_get_user(&parser, ubuf, cnt, ppos);
4914+
parser = &fgd->parser;
48694915

4870-
if (read >= 0 && trace_parser_loaded((&parser))) {
4871-
parser.buffer[parser.idx] = 0;
4916+
read = trace_get_user(parser, ubuf, cnt, ppos);
48724917

4873-
mutex_lock(&graph_lock);
4918+
if (read >= 0 && trace_parser_loaded(parser) &&
4919+
!trace_parser_cont(parser)) {
48744920

4875-
/* we allow only one expression at a time */
48764921
ret = ftrace_graph_set_hash(fgd->new_hash,
4877-
parser.buffer);
4878-
4879-
new_hash = __ftrace_hash_move(fgd->new_hash);
4880-
if (!new_hash)
4881-
ret = -ENOMEM;
4882-
4883-
if (fgd->type == GRAPH_FILTER_FUNCTION) {
4884-
old_hash = rcu_dereference_protected(ftrace_graph_hash,
4885-
lockdep_is_held(&graph_lock));
4886-
rcu_assign_pointer(ftrace_graph_hash, new_hash);
4887-
} else {
4888-
old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4889-
lockdep_is_held(&graph_lock));
4890-
rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
4891-
}
4892-
4893-
mutex_unlock(&graph_lock);
4894-
4895-
/* Wait till all users are no longer using the old hash */
4896-
synchronize_sched();
4897-
4898-
free_ftrace_hash(old_hash);
4922+
parser->buffer);
4923+
trace_parser_clear(parser);
48994924
}
49004925

49014926
if (!ret)
49024927
ret = read;
49034928

4904-
trace_parser_put(&parser);
4905-
49064929
return ret;
49074930
}
49084931

0 commit comments

Comments
 (0)