From: Masami Hiramatsu (Google) <[email protected]>

Sashiko pointed that this sample code does not correctly handle the
failure of thread creation because kthread_run() can return -errno.

This removes the counter-based thread creation/stops but just
checking the simple_tsk_fn is correctly initialized (created) or not.

Link: 
https://sashiko.dev/#/patchset/178092865666.163648.10457567771536160909.stgit%40devnote2

Fixes: 9cfe06f8cd5c ("tracing/events: add trace-events-sample")
Signed-off-by: Masami Hiramatsu (Google) <[email protected]>
---
 samples/trace_events/trace-events-sample.c |   16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/samples/trace_events/trace-events-sample.c 
b/samples/trace_events/trace-events-sample.c
index ecc7db237f2e..b61766864b54 100644
--- a/samples/trace_events/trace-events-sample.c
+++ b/samples/trace_events/trace-events-sample.c
@@ -92,12 +92,11 @@ static int simple_thread_fn(void *arg)
 }
 
 static DEFINE_MUTEX(thread_mutex);
-static int simple_thread_cnt;
 
 int foo_bar_reg(void)
 {
        mutex_lock(&thread_mutex);
-       if (simple_thread_cnt++)
+       if (!IS_ERR_OR_NULL(simple_tsk_fn))
                goto out;
 
        pr_info("Starting thread for foo_bar_fn\n");
@@ -115,14 +114,11 @@ int foo_bar_reg(void)
 void foo_bar_unreg(void)
 {
        mutex_lock(&thread_mutex);
-       if (--simple_thread_cnt)
-               goto out;
-
-       pr_info("Killing thread for foo_bar_fn\n");
-       if (simple_tsk_fn)
+       if (!IS_ERR_OR_NULL(simple_tsk_fn)) {
+               pr_info("Killing thread for foo_bar_fn\n");
                kthread_stop(simple_tsk_fn);
-       simple_tsk_fn = NULL;
- out:
+               simple_tsk_fn = NULL;
+       }
        mutex_unlock(&thread_mutex);
 }
 
@@ -139,7 +135,7 @@ static void __exit trace_event_exit(void)
 {
        kthread_stop(simple_tsk);
        mutex_lock(&thread_mutex);
-       if (simple_tsk_fn)
+       if (!IS_ERR_OR_NULL(simple_tsk_fn))
                kthread_stop(simple_tsk_fn);
        simple_tsk_fn = NULL;
        mutex_unlock(&thread_mutex);


Reply via email to