]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
thermal: testing: zone: Flush work items during cleanup
authorRafael J. Wysocki <rafael.j.wysocki@intel.com>
Fri, 19 Jun 2026 15:01:27 +0000 (17:01 +0200)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Wed, 24 Jun 2026 12:59:25 +0000 (14:59 +0200)
To prevent freed module code from being executed during the thermal
testing module unload, make it add a dedicated workqueue for thermal
testing work items and flush it in thermal_testing_exit().

Fixes: f6a034f2df42 ("thermal: Introduce a debugfs-based testing facility")
Link: https://sashiko.dev/#/patchset/20260605185212.2491144-1-sam.moelius%40trailofbits.com
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Link: https://patch.msgid.link/1959388.tdWV9SEqCh@rafael.j.wysocki
[ rjw: Make variable d_command static ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/thermal/testing/command.c
drivers/thermal/testing/thermal_testing.h
drivers/thermal/testing/zone.c

index fbf7ab9729b5a5a896ea17234dd7f224e5fa4b26..90e06c5933278ae072e37cef695b890a9f6add75 100644 (file)
 
 #include "thermal_testing.h"
 
-struct dentry *d_testing;
+struct workqueue_struct *tt_wq __ro_after_init;
+
+struct dentry *d_testing __ro_after_init;
+static struct dentry *d_command __ro_after_init;
 
 #define TT_COMMAND_SIZE                16
 
@@ -203,17 +206,42 @@ static const struct file_operations tt_command_fops = {
 
 static int __init thermal_testing_init(void)
 {
+       int error;
+
+       tt_wq = alloc_workqueue("thermal_testing", WQ_UNBOUND, 0);
+       if (!tt_wq)
+               return -ENOMEM;
+
        d_testing = debugfs_create_dir("thermal-testing", NULL);
-       if (!IS_ERR(d_testing))
-               debugfs_create_file("command", 0200, d_testing, NULL,
-                                   &tt_command_fops);
+       if (IS_ERR(d_testing)) {
+               error = PTR_ERR(d_testing);
+               goto destroy_wq;
+       }
+
+       d_command = debugfs_create_file("command", 0200, d_testing, NULL, &tt_command_fops);
+       if (IS_ERR(d_command)) {
+               error = PTR_ERR(d_command);
+               goto remove_d_testing;
+       }
 
        return 0;
+
+remove_d_testing:
+       debugfs_remove(d_testing);
+destroy_wq:
+       destroy_workqueue(tt_wq);
+       return error;
 }
 module_init(thermal_testing_init);
 
 static void __exit thermal_testing_exit(void)
 {
+       /* First, prevent new commands from being entered. */
+       debugfs_remove(d_command);
+       /* Flush commands in progress (if any). */
+       flush_workqueue(tt_wq);
+       destroy_workqueue(tt_wq);
+       /* Remove the directory structure and clean up. */
        debugfs_remove(d_testing);
        tt_zone_cleanup();
 }
index c790a32aae4ed7f383c7bdfc967d739d07a3a91e..5880c9a63dba99fe9302326ffcfcb70511dec08e 100644 (file)
@@ -1,4 +1,12 @@
 /* SPDX-License-Identifier: GPL-2.0 */
+#include <linux/workqueue.h>
+
+extern struct workqueue_struct *tt_wq;
+
+static inline void tt_queue_work(struct work_struct *work)
+{
+       queue_work(tt_wq, work);
+}
 
 extern struct dentry *d_testing;
 
index f7f9ca2f1f2cd76ed30ba6f4481cbe604d791892..6db26276dd4172fb50c2bfe4c28341a147ad6999 100644 (file)
@@ -13,7 +13,6 @@
 #include <linux/idr.h>
 #include <linux/list.h>
 #include <linux/thermal.h>
-#include <linux/workqueue.h>
 
 #include "thermal_testing.h"
 
@@ -207,7 +206,7 @@ int tt_add_tz(void)
 
        INIT_WORK(&tt_work->work, tt_add_tz_work_fn);
        tt_work->tt_zone = no_free_ptr(tt_zone);
-       schedule_work(&(no_free_ptr(tt_work)->work));
+       tt_queue_work(&(no_free_ptr(tt_work)->work));
 
        return 0;
 }
@@ -269,7 +268,7 @@ int tt_del_tz(const char *arg)
 
        INIT_WORK(&tt_work->work, tt_del_tz_work_fn);
        tt_work->tt_zone = tt_zone;
-       schedule_work(&(no_free_ptr(tt_work)->work));
+       tt_queue_work(&(no_free_ptr(tt_work)->work));
 
        return 0;
 }
@@ -358,7 +357,7 @@ int tt_zone_add_trip(const char *arg)
        INIT_WORK(&tt_work->work, tt_zone_add_trip_work_fn);
        tt_work->tt_zone = no_free_ptr(tt_zone);
        tt_work->tt_trip = no_free_ptr(tt_trip);
-       schedule_work(&(no_free_ptr(tt_work)->work));
+       tt_queue_work(&(no_free_ptr(tt_work)->work));
 
        return 0;
 }