Skip to content

Commit a48fce6

Browse files
rostedtgregkh
authored andcommitted
tracing/samples: Fix creation and deletion of simple_thread_fn creation
commit 6575257c60e1a26a5319ccf2b5ce5b6449001017 upstream. Commit 7496946 ("tracing: Add samples of DECLARE_EVENT_CLASS() and DEFINE_EVENT()") added template examples for all the events. It created a DEFINE_EVENT_FN() example which reused the foo_bar_reg and foo_bar_unreg functions. Enabling both the TRACE_EVENT_FN() and DEFINE_EVENT_FN() example trace events caused the foo_bar_reg to be called twice, creating the test thread twice. The foo_bar_unreg would remove it only once, even if it was called multiple times, leaving a thread existing when the module is unloaded, causing an oops. Add a ref count and allow foo_bar_reg() and foo_bar_unreg() be called by multiple trace events. Fixes: 7496946 ("tracing: Add samples of DECLARE_EVENT_CLASS() and DEFINE_EVENT()") Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ded34f9 commit a48fce6

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

samples/trace_events/trace-events-sample.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,36 @@ static int simple_thread_fn(void *arg)
7878
}
7979

8080
static DEFINE_MUTEX(thread_mutex);
81+
static bool simple_thread_cnt;
8182

8283
void foo_bar_reg(void)
8384
{
85+
mutex_lock(&thread_mutex);
86+
if (simple_thread_cnt++)
87+
goto out;
88+
8489
pr_info("Starting thread for foo_bar_fn\n");
8590
/*
8691
* We shouldn't be able to start a trace when the module is
8792
* unloading (there's other locks to prevent that). But
8893
* for consistency sake, we still take the thread_mutex.
8994
*/
90-
mutex_lock(&thread_mutex);
9195
simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
96+
out:
9297
mutex_unlock(&thread_mutex);
9398
}
9499

95100
void foo_bar_unreg(void)
96101
{
97-
pr_info("Killing thread for foo_bar_fn\n");
98-
/* protect against module unloading */
99102
mutex_lock(&thread_mutex);
103+
if (--simple_thread_cnt)
104+
goto out;
105+
106+
pr_info("Killing thread for foo_bar_fn\n");
100107
if (simple_tsk_fn)
101108
kthread_stop(simple_tsk_fn);
102109
simple_tsk_fn = NULL;
110+
out:
103111
mutex_unlock(&thread_mutex);
104112
}
105113

0 commit comments

Comments
 (0)