All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Binding <sbinding@opensource.cirrus.com>
To: Mark Brown <broonie@kernel.org>, Jaroslav Kysela <perex@perex.cz>,
	Takashi Iwai <tiwai@suse.com>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org,
	patches@opensource.cirrus.com,
	Stefan Binding <sbinding@opensource.cirrus.com>
Subject: [PATCH v1 7/7] ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races
Date: Fri, 20 Nov 2026 16:10:53 +0000	[thread overview]
Message-ID: <20261120161136.157593-8-sbinding@opensource.cirrus.com> (raw)
In-Reply-To: <20261120161136.157593-1-sbinding@opensource.cirrus.com>

Currently, in a small number of scenarios, the firmware on CS35L41 can
be reloaded:
- Using an ALSA control
- System Resume
- After Playback (after System Resume - if reload has not occurred)

In all these cases, the reload is done inside a work item and
scheduled on the default workqueue.

This should instead be queued on the system_freezable_wq, which ensures
that the work item is handled correctly during system suspend, where it
is not allowed to start whilst the workqueue is frozen.

There are 3 main scenarios where we need to ensure firmware load work
is flushed:
- Before System Suspend
- Before first playback after System Resume
- Error Paths

Register the driver for PM Notifications so that it can correctly flush
before suspending. Ensure any firmware work is either cancelled (if
pending) or flushed in the PM_*_PREPARE notification callbacks.
This ensures that the work item completes before any workqueues are
frozen. If the firmware load work item is still running when the
workqueue is frozen, then it would ordinarily be flushed, but may hang
on waiting for filesystem access in a call to request_firmware(), if
filesystem access requires work in a freezable workqueue itself.

Additionally, ensure that we flush the work before we start playback
after system resume. This ensures firmware is loaded before we restart
any playback after system suspend.

We also need to re-queue the firmware load in the PM_POST_* notifiers,
if the suspend was aborted before we finished suspending, but after
the PM_*_PREPARE notifier cancelled a pending firmware load.

Fixes: 88672826e2a4 ("ALSA: hda: cs35l41: Support System Suspend")

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
---
 sound/hda/codecs/side-codecs/cs35l41_hda.c | 129 +++++++++++++++++----
 sound/hda/codecs/side-codecs/cs35l41_hda.h |   2 +
 2 files changed, 109 insertions(+), 22 deletions(-)

diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.c b/sound/hda/codecs/side-codecs/cs35l41_hda.c
index 6f4b2088c5fe..512b1a009476 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.c
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.c
@@ -12,6 +12,7 @@
 #include <sound/hda_codec.h>
 #include <sound/soc.h>
 #include <linux/pm_runtime.h>
+#include <linux/suspend.h>
 #include <linux/spi/spi.h>
 #include <linux/vmalloc.h>
 #include "hda_local.h"
@@ -603,6 +604,38 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
 	return 0;
 }
 
+static void cs35l41_fw_load_request_unlocked(struct cs35l41_hda *cs35l41)
+{
+	/* firmware request is pending */
+	if (cs35l41->fw_request_ongoing)
+		return;
+
+	/* no need to request firmware load if DSP running state matches request */
+	if (cs35l41->cs_dsp.running == cs35l41->request_fw_load)
+		return;
+
+	/* block firmware load request if suspending */
+	if (cs35l41->suspending)
+		return;
+
+	cs35l41->fw_request_ongoing = queue_work(system_freezable_wq, &cs35l41->fw_load_work);
+}
+
+static void cs35l41_fw_load_request(struct cs35l41_hda *cs35l41)
+{
+	guard(mutex)(&cs35l41->fw_mutex);
+
+	cs35l41_fw_load_request_unlocked(cs35l41);
+}
+
+static void cs35l41_fw_load_cancel_sync(struct cs35l41_hda *cs35l41)
+{
+	cancel_work_sync(&cs35l41->fw_load_work);
+
+	guard(mutex)(&cs35l41->fw_mutex);
+	cs35l41->fw_request_ongoing = false;
+}
+
 static void cs35l41_shutdown_dsp(struct cs35l41_hda *cs35l41)
 {
 	struct cs_dsp *dsp = &cs35l41->cs_dsp;
@@ -617,7 +650,7 @@ static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41)
 {
 	struct cs_dsp *dsp = &cs35l41->cs_dsp;
 
-	cancel_work_sync(&cs35l41->fw_load_work);
+	cs35l41_fw_load_cancel_sync(cs35l41);
 
 	guard(mutex)(&cs35l41->fw_mutex);
 	cs35l41_shutdown_dsp(cs35l41);
@@ -811,6 +844,7 @@ static void cs35l41_hda_playback_hook(struct device *dev, int action)
 		pm_runtime_get_sync(dev);
 		break;
 	case HDA_GEN_PCM_ACT_PREPARE:
+		flush_work(&cs35l41->fw_load_work);
 		scoped_guard(mutex, &cs35l41->fw_mutex) {
 			cs35l41_hda_play_start(dev);
 		}
@@ -821,14 +855,7 @@ static void cs35l41_hda_playback_hook(struct device *dev, int action)
 		}
 		break;
 	case HDA_GEN_PCM_ACT_CLOSE:
-		scoped_guard(mutex, &cs35l41->fw_mutex) {
-			if (!cs35l41->cs_dsp.running && cs35l41->request_fw_load &&
-			    !cs35l41->fw_request_ongoing) {
-				dev_info(dev, "Requesting Firmware Load after HDA_GEN_PCM_ACT_CLOSE\n");
-				cs35l41->fw_request_ongoing = true;
-				schedule_work(&cs35l41->fw_load_work);
-			}
-		}
+		cs35l41_fw_load_request(cs35l41);
 
 		/*
 		 * Playback must be finished for all amps before we start runtime suspend.
@@ -925,6 +952,45 @@ static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
 	return 0;
 }
 
+static void cs35l41_set_suspending(struct cs35l41_hda *cs35l41, bool suspending)
+{
+	guard(mutex)(&cs35l41->fw_mutex);
+
+	cs35l41->suspending = suspending;
+}
+
+static int cs35l41_hda_pm_notifier(struct notifier_block *nb, unsigned long action, void *data)
+{
+	struct cs35l41_hda *cs35l41 = container_of(nb, struct cs35l41_hda, pm_notifier);
+
+	dev_dbg(cs35l41->dev, "PM notifier action: %lu\n", action);
+
+	switch (action) {
+	case PM_HIBERNATION_PREPARE:
+	case PM_SUSPEND_PREPARE:
+	case PM_RESTORE_PREPARE:
+		cs35l41_set_suspending(cs35l41, true);
+		cs35l41_fw_load_cancel_sync(cs35l41);
+		break;
+	case PM_POST_HIBERNATION:
+	case PM_POST_SUSPEND:
+	case PM_POST_RESTORE:
+		/*
+		 * If suspending is still set, we are in the recovery path - reload firmware
+		 * after failed suspend
+		 */
+		if (cs35l41->suspending) {
+			cs35l41_set_suspending(cs35l41, false);
+			cs35l41_fw_load_request(cs35l41);
+		}
+		break;
+	default:
+		break;
+	}
+
+	return NOTIFY_DONE;
+}
+
 static int cs35l41_system_suspend_prep(struct device *dev)
 {
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
@@ -980,6 +1046,7 @@ static int cs35l41_system_suspend(struct device *dev)
 		disable_irq(cs35l41->irq);
 
 	dev_dbg(cs35l41->dev, "System Suspended\n");
+	cs35l41_set_suspending(cs35l41, false);
 
 	return ret;
 }
@@ -1078,12 +1145,7 @@ static int cs35l41_system_resume(struct device *dev)
 		return ret;
 	}
 
-	guard(mutex)(&cs35l41->fw_mutex);
-
-	if (cs35l41->request_fw_load && !cs35l41->fw_request_ongoing) {
-		cs35l41->fw_request_ongoing = true;
-		schedule_work(&cs35l41->fw_load_work);
-	}
+	cs35l41_fw_load_request(cs35l41);
 
 	return 0;
 }
@@ -1320,9 +1382,11 @@ static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,
 		return -EBUSY;
 	}
 
-	cs35l41->fw_request_ongoing = true;
+	guard(mutex)(&cs35l41->fw_mutex);
+
 	cs35l41->request_fw_load = ucontrol->value.integer.value[0];
-	schedule_work(&cs35l41->fw_load_work);
+
+	cs35l41_fw_load_request_unlocked(cs35l41);
 
 	return 1;
 }
@@ -1464,7 +1528,7 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
 	struct hda_component_parent *parent = master_data;
 	struct hda_component *comp;
-	int ret = 0;
+	int ret;
 
 	comp = hda_component_from_index(parent, cs35l41->index);
 	if (!comp)
@@ -1494,8 +1558,6 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 		dev_dbg(cs35l41->dev, "Firmware Autostart is disabled.\n");
 	}
 
-	ret = cs35l41_create_controls(cs35l41);
-
 	comp->playback_hook = cs35l41_hda_playback_hook;
 	comp->pre_playback_hook = cs35l41_hda_pre_playback_hook;
 	comp->post_playback_hook = cs35l41_hda_post_playback_hook;
@@ -1510,9 +1572,18 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 
 	mutex_unlock(&cs35l41->fw_mutex);
 
+	/* Create the necessary controls, do not create them under fw_mutex */
+	ret = cs35l41_create_controls(cs35l41);
+	if (ret) {
+		dev_err(cs35l41->dev, "Failed to create controls: %d\n", ret);
+		goto err;
+	}
+
 	if (!device_link_add(&cs35l41->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS))
 		dev_warn(dev, "Unable to create device link\n");
 
+	register_pm_notifier(&cs35l41->pm_notifier);
+
 	dev_info(cs35l41->dev,
 		 "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
 		 cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,
@@ -1520,6 +1591,15 @@ static int cs35l41_hda_bind(struct device *dev, struct device *master, void *mas
 		 channel_name[cs35l41->hw_cfg.spk_pos],
 		 cs35l41->cs_dsp.running, cs35l41->speaker_id);
 
+	return 0;
+err:
+	memset(comp, 0, sizeof(*comp));
+	scoped_guard(mutex, &cs35l41->fw_mutex) {
+		cs35l41->request_fw_load = false;
+	}
+	cs35l41_fw_load_cancel_sync(cs35l41);
+	cs35l41->codec = NULL;
+
 	return ret;
 }
 
@@ -1538,7 +1618,11 @@ static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *
 		memset(comp, 0, sizeof(*comp));
 
 		cs35l41_remove_controls(cs35l41);
-		cancel_work_sync(&cs35l41->fw_load_work);
+		scoped_guard(mutex, &cs35l41->fw_mutex) {
+			cs35l41->request_fw_load = false;
+		}
+		unregister_pm_notifier(&cs35l41->pm_notifier);
+		cs35l41_fw_load_cancel_sync(cs35l41);
 		cs35l41->codec = NULL;
 	}
 }
@@ -1970,6 +2054,7 @@ int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int i
 	cs35l41->irq = irq;
 	cs35l41->regmap = regmap;
 	cs35l41->control_bus = control_bus;
+	cs35l41->pm_notifier.notifier_call = cs35l41_hda_pm_notifier;
 	dev_set_drvdata(dev, cs35l41);
 
 	ret = cs35l41_hda_read_acpi(cs35l41, device_name, id);
@@ -2076,7 +2161,7 @@ void cs35l41_hda_remove(struct device *dev)
 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
 
 	component_del(cs35l41->dev, &cs35l41_hda_comp_ops);
-	cancel_work_sync(&cs35l41->fw_load_work);
+	cs35l41_fw_load_cancel_sync(cs35l41);
 
 	pm_runtime_get_sync(cs35l41->dev);
 	pm_runtime_dont_use_autosuspend(cs35l41->dev);
diff --git a/sound/hda/codecs/side-codecs/cs35l41_hda.h b/sound/hda/codecs/side-codecs/cs35l41_hda.h
index 64ee70c31a43..b374836e8537 100644
--- a/sound/hda/codecs/side-codecs/cs35l41_hda.h
+++ b/sound/hda/codecs/side-codecs/cs35l41_hda.h
@@ -78,6 +78,8 @@ struct cs35l41_hda {
 	struct work_struct fw_load_work;
 	struct snd_kcontrol *fw_load_ctl;
 	struct snd_kcontrol *mute_override_ctl;
+	struct notifier_block pm_notifier;
+	bool suspending;
 
 	struct regmap_irq_chip_data *irq_data;
 	bool firmware_running;
-- 
2.43.0


  parent reply	other threads:[~2026-11-20 16:12 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 16:10 [PATCH v1 0/7] Fixes and improvements in CS35L41 HDA driver for System Suspend/Resume Stefan Binding
2026-11-20 16:10 ` [PATCH v1 1/7] ALSA: hda: cs35l41: Remove redundant lock_system_sleep() calls Stefan Binding
2026-11-20 16:10 ` [PATCH v1 2/7] ASoC: cs35l41: Prevent software reset when using SPI Stefan Binding
2026-11-20 16:22   ` sashiko-bot
2026-11-20 16:10 ` [PATCH v1 3/7] ALSA: hda: cs35l41: Unset irq if not used or configured Stefan Binding
2026-11-20 16:10 ` [PATCH v1 4/7] ALSA: hda/cs35l41: Split System Suspend/Resume callbacks Stefan Binding
2026-11-20 16:27   ` sashiko-bot
2026-11-20 16:10 ` [PATCH v1 5/7] ALSA: hda/cs35l41: Remove support for alternative firmware types Stefan Binding
2026-11-20 16:10 ` [PATCH v1 6/7] ALSA: hda/cs35l41: Store filenames of successfully loaded firmware and tunings Stefan Binding
2026-11-20 16:10 ` Stefan Binding [this message]
2026-11-20 16:41   ` [PATCH v1 7/7] ALSA: hda/cs35l41: rework how firmware requests are queued and flushed to prevent races sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261120161136.157593-8-sbinding@opensource.cirrus.com \
    --to=sbinding@opensource.cirrus.com \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.