Skip to content

Commit

Permalink
tracing: Add tracing_check_open_get_tr()
Browse files Browse the repository at this point in the history
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/[email protected]

Signed-off-by: Steven Rostedt (VMware) <[email protected]>
  • Loading branch information
rostedt committed Oct 13, 2019
1 parent aa07d71 commit 8530dec
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 60 deletions.
7 changes: 4 additions & 3 deletions kernel/trace/ftrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -3547,7 +3547,7 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
if (unlikely(ftrace_disabled))
return -ENODEV;

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

iter = kzalloc(sizeof(*iter), GFP_KERNEL);
Expand Down Expand Up @@ -6546,8 +6546,9 @@ ftrace_pid_open(struct inode *inode, struct file *file)
struct seq_file *m;
int ret = 0;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

if ((file->f_mode & FMODE_WRITE) &&
(file->f_flags & O_TRUNC))
Expand Down
117 changes: 65 additions & 52 deletions kernel/trace/trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ void trace_array_put(struct trace_array *this_tr)
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,
struct ring_buffer *buffer,
struct ring_buffer_event *event)
Expand Down Expand Up @@ -4140,8 +4151,11 @@ __tracing_open(struct inode *inode, struct file *file, bool snapshot)

int tracing_open_generic(struct inode *inode, struct file *filp)
{
if (tracing_disabled)
return -ENODEV;
int ret;

ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;

filp->private_data = inode->i_private;
return 0;
Expand All @@ -4159,12 +4173,11 @@ bool tracing_is_disabled(void)
int tracing_open_generic_tr(struct inode *inode, struct file *filp)
{
struct trace_array *tr = inode->i_private;
int ret;

if (tracing_disabled)
return -ENODEV;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

filp->private_data = inode->i_private;

Expand Down Expand Up @@ -4233,10 +4246,11 @@ static int tracing_open(struct inode *inode, struct file *file)
{
struct trace_array *tr = inode->i_private;
struct trace_iterator *iter;
int ret = 0;
int ret;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

/* If this file was open for write, then erase contents */
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
Expand Down Expand Up @@ -4352,11 +4366,9 @@ static int show_traces_open(struct inode *inode, struct file *file)
struct seq_file *m;
int ret;

if (tracing_disabled)
return -ENODEV;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

ret = seq_open(file, &show_traces_seq_ops);
if (ret) {
Expand Down Expand Up @@ -4710,11 +4722,9 @@ static int tracing_trace_options_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private;
int ret;

if (tracing_disabled)
return -ENODEV;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

ret = single_open(file, tracing_trace_options_show, inode->i_private);
if (ret < 0)
Expand Down Expand Up @@ -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)
{
if (tracing_disabled)
return -ENODEV;
int ret;

ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;

return seq_open(filp, &tracing_saved_tgids_seq_ops);
}
Expand Down Expand Up @@ -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)
{
if (tracing_disabled)
return -ENODEV;
int ret;

ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;

return seq_open(filp, &tracing_saved_cmdlines_seq_ops);
}
Expand Down Expand Up @@ -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)
{
if (tracing_disabled)
return -ENODEV;
int ret;

ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;

return seq_open(filp, &tracing_eval_map_seq_ops);
}
Expand Down Expand Up @@ -5817,13 +5836,11 @@ static int tracing_open_pipe(struct inode *inode, struct file *filp)
{
struct trace_array *tr = inode->i_private;
struct trace_iterator *iter;
int ret = 0;

if (tracing_disabled)
return -ENODEV;
int ret;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

mutex_lock(&trace_types_lock);

Expand Down Expand Up @@ -6560,11 +6577,9 @@ static int tracing_clock_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private;
int ret;

if (tracing_disabled)
return -ENODEV;

if (trace_array_get(tr))
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

ret = single_open(file, tracing_clock_show, inode->i_private);
if (ret < 0)
Expand Down Expand Up @@ -6594,11 +6609,9 @@ static int tracing_time_stamp_mode_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private;
int ret;

if (tracing_disabled)
return -ENODEV;

if (trace_array_get(tr))
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

ret = single_open(file, tracing_time_stamp_mode_show, inode->i_private);
if (ret < 0)
Expand Down Expand Up @@ -6651,10 +6664,11 @@ static int tracing_snapshot_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private;
struct trace_iterator *iter;
struct seq_file *m;
int ret = 0;
int ret;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

if (file->f_mode & FMODE_READ) {
iter = __tracing_open(inode, file, true);
Expand Down Expand Up @@ -7118,8 +7132,9 @@ static int tracing_err_log_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private;
int ret = 0;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

/* If this file was opened for write, then erase contents */
if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
Expand Down Expand Up @@ -7170,11 +7185,9 @@ static int tracing_buffers_open(struct inode *inode, struct file *filp)
struct ftrace_buffer_info *info;
int ret;

if (tracing_disabled)
return -ENODEV;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info) {
Expand Down
1 change: 1 addition & 0 deletions kernel/trace/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ extern struct mutex trace_types_lock;

extern int trace_array_get(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_clock(struct trace_array *tr, const char *clockstr);
Expand Down
4 changes: 4 additions & 0 deletions kernel/trace/trace_dynevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ static int dyn_event_open(struct inode *inode, struct file *file)
{
int ret;

ret = tracing_check_open_get_tr(NULL);
if (ret)
return ret;

if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC)) {
ret = dyn_events_release_all(NULL);
if (ret < 0)
Expand Down
10 changes: 6 additions & 4 deletions kernel/trace/trace_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -1794,8 +1794,9 @@ ftrace_event_set_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private;
int ret;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

if ((file->f_mode & FMODE_WRITE) &&
(file->f_flags & O_TRUNC))
Expand All @@ -1814,8 +1815,9 @@ ftrace_event_set_pid_open(struct inode *inode, struct file *file)
struct trace_array *tr = inode->i_private;
int ret;

if (trace_array_get(tr) < 0)
return -ENODEV;
ret = tracing_check_open_get_tr(tr);
if (ret)
return ret;

if ((file->f_mode & FMODE_WRITE) &&
(file->f_flags & O_TRUNC))
Expand Down
2 changes: 1 addition & 1 deletion kernel/trace/trace_events_hist.c
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ static int save_hist_vars(struct hist_trigger_data *hist_data)
if (var_data)
return 0;

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

var_data = kzalloc(sizeof(*var_data), GFP_KERNEL);
Expand Down

0 comments on commit 8530dec

Please sign in to comment.