tracing: Add tracing_check_open_get_tr()

Currently, most files in the tracefs directory test if tracing_disabled is
set. If so, it should return -ENODEV. The tracing_disabled is called when
tracing is found to be broken. Originally it was done in case the ring
buffer was found to be corrupted, and we wanted to prevent reading it from
crashing the kernel. But it's also called if a tracing selftest fails on
boot. It's a one way switch. That is, once it is triggered, tracing is
disabled until reboot.

As most tracefs files can also be used by instances in the tracefs
directory, they need to be carefully done. Each instance has a trace_array
associated to it, and when the instance is removed, the trace_array is
freed. But if an instance is opened with a reference to the trace_array,
then it requires looking up the trace_array to get its ref counter (as there
could be a race with it being deleted and the open itself). Once it is
found, a reference is added to prevent the instance from being removed (and
the trace_array associated with it freed).

Combine the two checks (tracing_disabled and trace_array_get()) into a
single helper function. This will also make it easier to add lockdown to
tracefs later.

Link: http://lkml.kernel.org/r/20191011135458.7399da44@gandalf.local.homeSigned-off-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
parent aa07d71f
...@@ -3547,7 +3547,7 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag, ...@@ -3547,7 +3547,7 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
if (unlikely(ftrace_disabled)) if (unlikely(ftrace_disabled))
return -ENODEV; return -ENODEV;
if (tr && trace_array_get(tr) < 0) if (tracing_check_open_get_tr(tr))
return -ENODEV; return -ENODEV;
iter = kzalloc(sizeof(*iter), GFP_KERNEL); iter = kzalloc(sizeof(*iter), GFP_KERNEL);
...@@ -6546,8 +6546,9 @@ ftrace_pid_open(struct inode *inode, struct file *file) ...@@ -6546,8 +6546,9 @@ ftrace_pid_open(struct inode *inode, struct file *file)
struct seq_file *m; struct seq_file *m;
int ret = 0; int ret = 0;
if (trace_array_get(tr) < 0) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if ((file->f_mode & FMODE_WRITE) && if ((file->f_mode & FMODE_WRITE) &&
(file->f_flags & O_TRUNC)) (file->f_flags & O_TRUNC))
......
...@@ -304,6 +304,17 @@ void trace_array_put(struct trace_array *this_tr) ...@@ -304,6 +304,17 @@ void trace_array_put(struct trace_array *this_tr)
mutex_unlock(&trace_types_lock); mutex_unlock(&trace_types_lock);
} }
int tracing_check_open_get_tr(struct trace_array *tr)
{
if (tracing_disabled)
return -ENODEV;
if (tr && trace_array_get(tr) < 0)
return -ENODEV;
return 0;
}
int call_filter_check_discard(struct trace_event_call *call, void *rec, int call_filter_check_discard(struct trace_event_call *call, void *rec,
struct ring_buffer *buffer, struct ring_buffer *buffer,
struct ring_buffer_event *event) struct ring_buffer_event *event)
...@@ -4140,8 +4151,11 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot) ...@@ -4140,8 +4151,11 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot)
int tracing_open_generic(struct inode *inode, struct file *filp) int tracing_open_generic(struct inode *inode, struct file *filp)
{ {
if (tracing_disabled) int ret;
return -ENODEV;
ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;
filp->private_data = inode->i_private; filp->private_data = inode->i_private;
return 0; return 0;
...@@ -4159,12 +4173,11 @@ bool tracing_is_disabled(void) ...@@ -4159,12 +4173,11 @@ bool tracing_is_disabled(void)
int tracing_open_generic_tr(struct inode *inode, struct file *filp) int tracing_open_generic_tr(struct inode *inode, struct file *filp)
{ {
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
int ret;
if (tracing_disabled) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if (trace_array_get(tr) < 0)
return -ENODEV;
filp->private_data = inode->i_private; filp->private_data = inode->i_private;
...@@ -4233,10 +4246,11 @@ static int tracing_open(struct inode *inode, struct file *file) ...@@ -4233,10 +4246,11 @@ static int tracing_open(struct inode *inode, struct file *file)
{ {
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
struct trace_iterator *iter; struct trace_iterator *iter;
int ret = 0; int ret;
if (trace_array_get(tr) < 0) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
/* If this file was open for write, then erase contents */ /* If this file was open for write, then erase contents */
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
...@@ -4352,11 +4366,9 @@ static int show_traces_open(struct inode *inode, struct file *file) ...@@ -4352,11 +4366,9 @@ static int show_traces_open(struct inode *inode, struct file *file)
struct seq_file *m; struct seq_file *m;
int ret; int ret;
if (tracing_disabled) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if (trace_array_get(tr) < 0)
return -ENODEV;
ret = seq_open(file, &show_traces_seq_ops); ret = seq_open(file, &show_traces_seq_ops);
if (ret) { if (ret) {
...@@ -4710,11 +4722,9 @@ static int tracing_trace_options_open(struct inode *inode, struct file *file) ...@@ -4710,11 +4722,9 @@ static int tracing_trace_options_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
int ret; int ret;
if (tracing_disabled) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if (trace_array_get(tr) < 0)
return -ENODEV;
ret = single_open(file, tracing_trace_options_show, inode->i_private); ret = single_open(file, tracing_trace_options_show, inode->i_private);
if (ret < 0) if (ret < 0)
...@@ -5051,8 +5061,11 @@ static const struct seq_operations tracing_saved_tgids_seq_ops = { ...@@ -5051,8 +5061,11 @@ static const struct seq_operations tracing_saved_tgids_seq_ops = {
static int tracing_saved_tgids_open(struct inode *inode, struct file *filp) static int tracing_saved_tgids_open(struct inode *inode, struct file *filp)
{ {
if (tracing_disabled) int ret;
return -ENODEV;
ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;
return seq_open(filp, &tracing_saved_tgids_seq_ops); return seq_open(filp, &tracing_saved_tgids_seq_ops);
} }
...@@ -5128,8 +5141,11 @@ static const struct seq_operations tracing_saved_cmdlines_seq_ops = { ...@@ -5128,8 +5141,11 @@ static const struct seq_operations tracing_saved_cmdlines_seq_ops = {
static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp) static int tracing_saved_cmdlines_open(struct inode *inode, struct file *filp)
{ {
if (tracing_disabled) int ret;
return -ENODEV;
ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;
return seq_open(filp, &tracing_saved_cmdlines_seq_ops); return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
} }
...@@ -5293,8 +5309,11 @@ static const struct seq_operations tracing_eval_map_seq_ops = { ...@@ -5293,8 +5309,11 @@ static const struct seq_operations tracing_eval_map_seq_ops = {
static int tracing_eval_map_open(struct inode *inode, struct file *filp) static int tracing_eval_map_open(struct inode *inode, struct file *filp)
{ {
if (tracing_disabled) int ret;
return -ENODEV;
ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;
return seq_open(filp, &tracing_eval_map_seq_ops); return seq_open(filp, &tracing_eval_map_seq_ops);
} }
...@@ -5817,13 +5836,11 @@ static int tracing_open_pipe(struct inode *inode, struct file *filp) ...@@ -5817,13 +5836,11 @@ static int tracing_open_pipe(struct inode *inode, struct file *filp)
{ {
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
struct trace_iterator *iter; struct trace_iterator *iter;
int ret = 0; int ret;
if (tracing_disabled)
return -ENODEV;
if (trace_array_get(tr) < 0) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
mutex_lock(&trace_types_lock); mutex_lock(&trace_types_lock);
...@@ -6560,11 +6577,9 @@ static int tracing_clock_open(struct inode *inode, struct file *file) ...@@ -6560,11 +6577,9 @@ static int tracing_clock_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
int ret; int ret;
if (tracing_disabled) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if (trace_array_get(tr))
return -ENODEV;
ret = single_open(file, tracing_clock_show, inode->i_private); ret = single_open(file, tracing_clock_show, inode->i_private);
if (ret < 0) if (ret < 0)
...@@ -6594,11 +6609,9 @@ static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file) ...@@ -6594,11 +6609,9 @@ static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
int ret; int ret;
if (tracing_disabled) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if (trace_array_get(tr))
return -ENODEV;
ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private); ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
if (ret < 0) if (ret < 0)
...@@ -6651,10 +6664,11 @@ static int tracing_snapshot_open(struct inode *inode, struct file *file) ...@@ -6651,10 +6664,11 @@ static int tracing_snapshot_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
struct trace_iterator *iter; struct trace_iterator *iter;
struct seq_file *m; struct seq_file *m;
int ret = 0; int ret;
if (trace_array_get(tr) < 0) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if (file->f_mode & FMODE_READ) { if (file->f_mode & FMODE_READ) {
iter = __tracing_open(inode, file, true); iter = __tracing_open(inode, file, true);
...@@ -7118,8 +7132,9 @@ static int tracing_err_log_open(struct inode *inode, struct file *file) ...@@ -7118,8 +7132,9 @@ static int tracing_err_log_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
int ret = 0; int ret = 0;
if (trace_array_get(tr) < 0) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
/* If this file was opened for write, then erase contents */ /* If this file was opened for write, then erase contents */
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
...@@ -7170,11 +7185,9 @@ static int tracing_buffers_open(struct inode *inode, struct file *filp) ...@@ -7170,11 +7185,9 @@ static int tracing_buffers_open(struct inode *inode, struct file *filp)
struct ftrace_buffer_info *info; struct ftrace_buffer_info *info;
int ret; int ret;
if (tracing_disabled) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if (trace_array_get(tr) < 0)
return -ENODEV;
info = kzalloc(sizeof(*info), GFP_KERNEL); info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info) { if (!info) {
......
...@@ -338,6 +338,7 @@ extern struct mutex trace_types_lock; ...@@ -338,6 +338,7 @@ extern struct mutex trace_types_lock;
extern int trace_array_get(struct trace_array *tr); extern int trace_array_get(struct trace_array *tr);
extern void trace_array_put(struct trace_array *tr); extern void trace_array_put(struct trace_array *tr);
extern int tracing_check_open_get_tr(struct trace_array *tr);
extern int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs); extern int tracing_set_time_stamp_abs(struct trace_array *tr, bool abs);
extern int tracing_set_clock(struct trace_array *tr, const char *clockstr); extern int tracing_set_clock(struct trace_array *tr, const char *clockstr);
......
...@@ -174,6 +174,10 @@ static int dyn_event_open(struct inode *inode, struct file *file) ...@@ -174,6 +174,10 @@ static int dyn_event_open(struct inode *inode, struct file *file)
{ {
int ret; int ret;
ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) { if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
ret = dyn_events_release_all(NULL); ret = dyn_events_release_all(NULL);
if (ret < 0) if (ret < 0)
......
...@@ -1794,8 +1794,9 @@ ftrace_event_set_open(struct inode *inode, struct file *file) ...@@ -1794,8 +1794,9 @@ ftrace_event_set_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
int ret; int ret;
if (trace_array_get(tr) < 0) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if ((file->f_mode & FMODE_WRITE) && if ((file->f_mode & FMODE_WRITE) &&
(file->f_flags & O_TRUNC)) (file->f_flags & O_TRUNC))
...@@ -1814,8 +1815,9 @@ ftrace_event_set_pid_open(struct inode *inode, struct file *file) ...@@ -1814,8 +1815,9 @@ ftrace_event_set_pid_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private; struct trace_array *tr = inode->i_private;
int ret; int ret;
if (trace_array_get(tr) < 0) ret = tracing_check_open_get_tr(tr);
return -ENODEV; if (ret)
return ret;
if ((file->f_mode & FMODE_WRITE) && if ((file->f_mode & FMODE_WRITE) &&
(file->f_flags & O_TRUNC)) (file->f_flags & O_TRUNC))
......
...@@ -1680,7 +1680,7 @@ static int save_hist_vars(struct hist_trigger_data *hist_data) ...@@ -1680,7 +1680,7 @@ static int save_hist_vars(struct hist_trigger_data *hist_data)
if (var_data) if (var_data)
return 0; return 0;
if (trace_array_get(tr) < 0) if (tracing_check_open_get_tr(tr))
return -ENODEV; return -ENODEV;
var_data = kzalloc(sizeof(*var_data), GFP_KERNEL); var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
......
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