Skip to content

Commit

Permalink
ftrace: Get a reference counter for the trace_array on filter files
Browse files Browse the repository at this point in the history
The ftrace set_ftrace_filter and set_ftrace_notrace files are specific for
an instance now. They need to take a reference to the instance otherwise
there could be a race between accessing the files and deleting the instance.

It wasn't until the :mod: caching where these file operations started
referencing the trace_array directly.

Cc: [email protected]
Fixes: 673feb9 ("ftrace: Add :mod: caching infrastructure to trace_array")
Signed-off-by: Steven Rostedt (VMware) <[email protected]>
  • Loading branch information
rostedt committed Oct 13, 2019
1 parent 3ed270b commit 9ef1669
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions kernel/trace/ftrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -3540,21 +3540,22 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
struct ftrace_hash *hash;
struct list_head *mod_head;
struct trace_array *tr = ops->private;
int ret = 0;
int ret = -ENOMEM;

ftrace_ops_init(ops);

if (unlikely(ftrace_disabled))
return -ENODEV;

if (tr && trace_array_get(tr) < 0)
return -ENODEV;

iter = kzalloc(sizeof(*iter), GFP_KERNEL);
if (!iter)
return -ENOMEM;
goto out;

if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
kfree(iter);
return -ENOMEM;
}
if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
goto out;

iter->ops = ops;
iter->flags = flag;
Expand Down Expand Up @@ -3584,13 +3585,13 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,

if (!iter->hash) {
trace_parser_put(&iter->parser);
kfree(iter);
ret = -ENOMEM;
goto out_unlock;
}
} else
iter->hash = hash;

ret = 0;

if (file->f_mode & FMODE_READ) {
iter->pg = ftrace_pages_start;

Expand All @@ -3602,14 +3603,20 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
/* Failed */
free_ftrace_hash(iter->hash);
trace_parser_put(&iter->parser);
kfree(iter);
}
} else
file->private_data = iter;

out_unlock:
mutex_unlock(&ops->func_hash->regex_lock);

out:
if (ret) {
kfree(iter);
if (tr)
trace_array_put(tr);
}

return ret;
}

Expand Down Expand Up @@ -5037,6 +5044,8 @@ int ftrace_regex_release(struct inode *inode, struct file *file)

mutex_unlock(&iter->ops->func_hash->regex_lock);
free_ftrace_hash(iter->hash);
if (iter->tr)
trace_array_put(iter->tr);
kfree(iter);

return 0;
Expand Down

0 comments on commit 9ef1669

Please sign in to comment.