Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit af247192 authored by Prateek Sood's avatar Prateek Sood Committed by Greg Kroah-Hartman
Browse files

tracing: Fix lock inversion in trace_event_enable_tgid_record()

commit 3a53acf1d9bea11b57c1f6205e3fe73f9d8a3688 upstream.

       Task T2                             Task T3
trace_options_core_write()            subsystem_open()

 mutex_lock(trace_types_lock)           mutex_lock(event_mutex)

 set_tracer_flag()

   trace_event_enable_tgid_record()       mutex_lock(trace_types_lock)

    mutex_lock(event_mutex)

This gives a circular dependency deadlock between trace_types_lock and
event_mutex. To fix this invert the usage of trace_types_lock and
event_mutex in trace_options_core_write(). This keeps the sequence of
lock usage consistent.

Link: http://lkml.kernel.org/r/0101016eef175e38-8ca71caf-a4eb-480d-a1e6-6f0bbc015495-000000@us-west-2.amazonses.com



Cc: stable@vger.kernel.org
Fixes: d914ba37 ("tracing: Add support for recording tgid of tasks")
Signed-off-by: default avatarPrateek Sood <prsood@codeaurora.org>
Signed-off-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 3444204d
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
@@ -4368,6 +4368,10 @@ int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)


int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
int set_tracer_flag(struct trace_array *tr, unsigned int mask, int enabled)
{
{
	if ((mask == TRACE_ITER_RECORD_TGID) ||
	    (mask == TRACE_ITER_RECORD_CMD))
		lockdep_assert_held(&event_mutex);

	/* do nothing if flag is already set */
	/* do nothing if flag is already set */
	if (!!(tr->trace_flags & mask) == !!enabled)
	if (!!(tr->trace_flags & mask) == !!enabled)
		return 0;
		return 0;
@@ -4433,6 +4437,7 @@ static int trace_set_options(struct trace_array *tr, char *option)
		cmp += 2;
		cmp += 2;
	}
	}


	mutex_lock(&event_mutex);
	mutex_lock(&trace_types_lock);
	mutex_lock(&trace_types_lock);


	for (i = 0; trace_options[i]; i++) {
	for (i = 0; trace_options[i]; i++) {
@@ -4447,6 +4452,7 @@ static int trace_set_options(struct trace_array *tr, char *option)
		ret = set_tracer_option(tr, cmp, neg);
		ret = set_tracer_option(tr, cmp, neg);


	mutex_unlock(&trace_types_lock);
	mutex_unlock(&trace_types_lock);
	mutex_unlock(&event_mutex);


	/*
	/*
	 * If the first trailing whitespace is replaced with '\0' by strstrip,
	 * If the first trailing whitespace is replaced with '\0' by strstrip,
@@ -7373,9 +7379,11 @@ trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
	if (val != 0 && val != 1)
	if (val != 0 && val != 1)
		return -EINVAL;
		return -EINVAL;


	mutex_lock(&event_mutex);
	mutex_lock(&trace_types_lock);
	mutex_lock(&trace_types_lock);
	ret = set_tracer_flag(tr, 1 << index, val);
	ret = set_tracer_flag(tr, 1 << index, val);
	mutex_unlock(&trace_types_lock);
	mutex_unlock(&trace_types_lock);
	mutex_unlock(&event_mutex);


	if (ret < 0)
	if (ret < 0)
		return ret;
		return ret;
+4 −4
Original line number Original line Diff line number Diff line
@@ -326,7 +326,8 @@ void trace_event_enable_cmd_record(bool enable)
	struct trace_event_file *file;
	struct trace_event_file *file;
	struct trace_array *tr;
	struct trace_array *tr;


	mutex_lock(&event_mutex);
	lockdep_assert_held(&event_mutex);

	do_for_each_event_file(tr, file) {
	do_for_each_event_file(tr, file) {


		if (!(file->flags & EVENT_FILE_FL_ENABLED))
		if (!(file->flags & EVENT_FILE_FL_ENABLED))
@@ -340,7 +341,6 @@ void trace_event_enable_cmd_record(bool enable)
			clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
			clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
		}
		}
	} while_for_each_event_file();
	} while_for_each_event_file();
	mutex_unlock(&event_mutex);
}
}


void trace_event_enable_tgid_record(bool enable)
void trace_event_enable_tgid_record(bool enable)
@@ -348,7 +348,8 @@ void trace_event_enable_tgid_record(bool enable)
	struct trace_event_file *file;
	struct trace_event_file *file;
	struct trace_array *tr;
	struct trace_array *tr;


	mutex_lock(&event_mutex);
	lockdep_assert_held(&event_mutex);

	do_for_each_event_file(tr, file) {
	do_for_each_event_file(tr, file) {
		if (!(file->flags & EVENT_FILE_FL_ENABLED))
		if (!(file->flags & EVENT_FILE_FL_ENABLED))
			continue;
			continue;
@@ -362,7 +363,6 @@ void trace_event_enable_tgid_record(bool enable)
				  &file->flags);
				  &file->flags);
		}
		}
	} while_for_each_event_file();
	} while_for_each_event_file();
	mutex_unlock(&event_mutex);
}
}


static int __ftrace_event_enable_disable(struct trace_event_file *file,
static int __ftrace_event_enable_disable(struct trace_event_file *file,