diff
 	mutex_lock(&thermal_dbg->lock);
 	tz->debugfs = NULL;
+	struct thermal_debugfs *to_remove = thermal_dbg;
+	thermal_dbg = NULL;
 	mutex_unlock(&thermal_dbg->lock);
-	thermal_debugfs_remove_id(thermal_dbg);
+	if (to_remove) {
+		thermal_debugfs_remove_id(to_remove);
+	}
