Skip to contentSkip to conversation

Fix IRQ mutex inversion on cs42l43

PATCH In review Charles Keepax 2 participants 6 messages Aug 2, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v4 cover Fix IRQ mutex inversion on cs42l43

Fix a mutex inversion in the IRQ handling for the speaker shutters on cs42l43. Do this by adding some new helper functions to get kcontrol pointers and caching those in the driver rather than directly acquiring them in the IRQ handler.

Changes in v4:
- Add the missing helper that was causing problems the cs35l45 update.

Changes in v3:
- Also initialise the control cache for the ASP DAI.

Changes in v2:
- Rather than using a work queue for the IRQ handler, cache the control pointers.
- Add some helpers to get kcontrol pointers.
- Update cs35l45 to use the new helper functions.
- Add a patch fixing a pointless semi-colon.

Thanks,
Charles

Charles Keepax (4):
ASoC: cs42l43: Remove redundant semi-colon at end of function
ASoC: soc-component: Add new snd_soc_component_get_kcontrol() helpers
ASoC: cs35l45: Use new snd_soc_component_get_kcontrol_locked() helper
ASoC: cs42l43: Cache shutter IRQ control pointers

 include/sound/soc-component.h |  5 +++
 sound/soc/codecs/cs35l45.c    | 11 +----
 sound/soc/codecs/cs42l43.c    | 75 ++++++++++++++++++++++++++---------
 sound/soc/codecs/cs42l43.h    |  2 +
 sound/soc/soc-component.c     | 42 ++++++++++++++++----
 5 files changed, 100 insertions(+), 35 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH v4 1/4 ASoC: cs42l43: Remove redundant semi-colon at end of function

---

No changes since v4.

Thanks,
Charles

 sound/soc/codecs/cs42l43.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/cs42l43.c +1 -1
index 92674314227c4..80825777048a6 100644
@@ -608,7 +608,7 @@ static int cs42l43_sdw_hw_params(struct snd_pcm_substream *substream,
 		return ret;
 
 	return cs42l43_set_sample_rate(substream, params, dai);
-};
+}
 
 static const struct snd_soc_dai_ops cs42l43_sdw_ops = {
 	.startup	= cs42l43_startup,
Signature · 1 line
2.39.2
#2 lore raw

PATCH v4 2/4 ASoC: soc-component: Add new snd_soc_component_get_kcontrol() helpers

In reply to Charles Keepax

Add new helper functions snd_soc_component_get_kcontrol() and snd_soc_component_get_kcontrol_locked() that returns a kcontrol by name, but will factor in the components name_prefix, to handle situations where multiple components are present with the same controls.

---

Changes since v3:
- Add new _locked version of the helper.
- Factor out the control name generation to share between the two helpers.

Thanks,
Charles

 include/sound/soc-component.h |  5 +++++
 sound/soc/soc-component.c     | 42 ++++++++++++++++++++++++++++-------
 2 files changed, 39 insertions(+), 8 deletions(-)
include/sound/soc-component.h +5 -0
index ceca69b46a821..bf2e381cd124b 100644
@@ -462,6 +462,11 @@ int snd_soc_component_force_enable_pin_unlocked(
 	const char *pin);
 
 /* component controls */
+struct snd_kcontrol *snd_soc_component_get_kcontrol(struct snd_soc_component *component,
+						    const char * const ctl);
+struct snd_kcontrol *
+snd_soc_component_get_kcontrol_locked(struct snd_soc_component *component,
+				      const char * const ctl);
 int snd_soc_component_notify_control(struct snd_soc_component *component,
 				     const char * const ctl);
 
sound/soc/soc-component.c +34 -8

53 lines not on this page · raw message

Signature · 1 line
2.39.2
#3 lore raw

PATCH v4 3/4 ASoC: cs35l45: Use new snd_soc_component_get_kcontrol_locked() helper

In reply to Charles Keepax

No longer any need to hard code the addition of the name prefix, use the new helper function.

---

No change since v3, unless one counts adding the missing helper in the previous patch.

Thanks,
Charles

 sound/soc/codecs/cs35l45.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
sound/soc/codecs/cs35l45.c +2 -9
index 2392c6effed85..1e9d73bee3b4e 100644
@@ -176,17 +176,10 @@ static int cs35l45_activate_ctl(struct snd_soc_component *component,
 	struct snd_kcontrol *kcontrol;
 	struct snd_kcontrol_volatile *vd;
 	unsigned int index_offset;
-	char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
 
-	if (component->name_prefix)
-		snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s",
-			 component->name_prefix, ctl_name);
-	else
-		snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s", ctl_name);
-
-	kcontrol = snd_soc_card_get_kcontrol_locked(component->card, name);
+	kcontrol = snd_soc_component_get_kcontrol_locked(component, ctl_name);
 	if (!kcontrol) {
-		dev_err(component->dev, "Can't find kcontrol %s\n", name);
+		dev_err(component->dev, "Can't find kcontrol %s\n", ctl_name);
 		return -EINVAL;
 	}
 
Signature · 1 line
2.39.2
#4 lore raw

PATCH v4 4/4 ASoC: cs42l43: Cache shutter IRQ control pointers

In reply to Charles Keepax

The microphone/speaker privacy shutter ALSA control handlers need to call pm_runtime_resume, since the hardware needs to be powered up to check the hardware state of the shutter. The IRQ handler for the shutters also needs to notify the ALSA control to inform user-space the shutters updated. However this leads to a mutex inversion, between the sdw_dev_lock and the controls_rwsem.

To avoid this mutex inversion cache the kctl pointers before the IRQ handler, which avoids the need to lookup the control and take the controls_rwsem.

---

No changes since v3.

Thanks,
Charles

 sound/soc/codecs/cs42l43.c | 73 +++++++++++++++++++++++++++++---------
 sound/soc/codecs/cs42l43.h |  2 ++
 2 files changed, 58 insertions(+), 17 deletions(-)
sound/soc/codecs/cs42l43.c +56 -17
index 80825777048a6..5183b45864243 100644
@@ -7,6 +7,7 @@
 
 #include <linux/bitops.h>
 #include <linux/bits.h>
+#include <linux/build_bug.h>
 #include <linux/clk.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -252,24 +253,20 @@ CS42L43_IRQ_COMPLETE(load_detect)
 static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
 {
 	struct cs42l43_codec *priv = data;
-	static const char * const controls[] = {
-		"Decimator 1 Switch",
-		"Decimator 2 Switch",
-		"Decimator 3 Switch",
-		"Decimator 4 Switch",
-	};
-	int i, ret;
+	struct snd_soc_component *component = priv->component;
+	int i;
 
 	dev_dbg(priv->dev, "Microphone shutter changed\n");
 
-	if (!priv->component)
+	if (!component)
 		return IRQ_NONE;
 
-	for (i = 0; i < ARRAY_SIZE(controls); i++) {
-		ret = snd_soc_component_notify_control(priv->component,
-						       controls[i]);
-		if (ret)
+	for (i = 1; i < ARRAY_SIZE(priv->kctl); i++) {
+		if (!priv->kctl[i])
 			return IRQ_NONE;
+
+		snd_ctl_notify(component->card->snd_card,
+			       SNDRV_CTL_EVENT_MASK_VALUE, &priv->kctl[i]->id);
 	}
 
 	return IRQ_HANDLED;
@@ -278,18 +275,19 @@ static irqreturn_t cs42l43_mic_shutter(int irq, void *data)
 static irqreturn_t cs42l43_spk_shutter(int irq, void *data)
 {
 	struct cs42l43_codec *priv = data;
-	int ret;
+	struct snd_soc_component *component = priv->component;
 
 	dev_dbg(priv->dev, "Speaker shutter changed\n");
 
-	if (!priv->component)
+	if (!component)
 		return IRQ_NONE;
 
-	ret = snd_soc_component_notify_control(priv->component,
-					       "Speaker Digital Switch");
-	if (ret)
+	if (!priv->kctl[0])
 		return IRQ_NONE;
 
+	snd_ctl_notify(component->card->snd_card,
+		       SNDRV_CTL_EVENT_MASK_VALUE, &priv->kctl[0]->id);
+
 	return IRQ_HANDLED;
 }
 
@@ -590,7 +588,46 @@ static int cs42l43_asp_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mas
 	return 0;
 }
 
+static int cs42l43_dai_probe(struct snd_soc_dai *dai)
+{
+	struct snd_soc_component *component = dai->component;
+	struct cs42l43_codec *priv = snd_soc_component_get_drvdata(component);
+	static const char * const controls[] = {
+		"Speaker Digital Switch",
+		"Decimator 1 Switch",
+		"Decimator 2 Switch",
+		"Decimator 3 Switch",
+		"Decimator 4 Switch",
+	};
+	int i;
+
+	static_assert(ARRAY_SIZE(controls) == ARRAY_SIZE(priv->kctl));
+
+	for (i = 0; i < ARRAY_SIZE(controls); i++) {
+		if (priv->kctl[i])
+			continue;
+
+		priv->kctl[i] = snd_soc_component_get_kcontrol(component, controls[i]);
+	}
+
+	return 0;
+}
+
+static int cs42l43_dai_remove(struct snd_soc_dai *dai)
+{
+	struct snd_soc_component *component = dai->component;
+	struct cs42l43_codec *priv = snd_soc_component_get_drvdata(component);
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(priv->kctl); i++)
+		priv->kctl[i] = NULL;
+
+	return 0;
+}
+
 static const struct snd_soc_dai_ops cs42l43_asp_ops = {
+	.probe		= cs42l43_dai_probe,
+	.remove		= cs42l43_dai_remove,
 	.startup	= cs42l43_startup,
 	.hw_params	= cs42l43_asp_hw_params,
 	.set_fmt	= cs42l43_asp_set_fmt,
@@ -611,6 +648,8 @@ static int cs42l43_sdw_hw_params(struct snd_pcm_substream *substream,
 }
 
 static const struct snd_soc_dai_ops cs42l43_sdw_ops = {
+	.probe		= cs42l43_dai_probe,
+	.remove		= cs42l43_dai_remove,
 	.startup	= cs42l43_startup,
 	.set_stream	= cs42l43_sdw_set_stream,
 	.hw_params	= cs42l43_sdw_hw_params,
sound/soc/codecs/cs42l43.h +2 -0
index 9924c13e1eb53..9c144e129535f 100644
@@ -100,6 +100,8 @@ struct cs42l43_codec {
 	struct delayed_work hp_ilimit_clear_work;
 	bool hp_ilimited;
 	int hp_ilimit_count;
+
+	struct snd_kcontrol *kctl[5];
 };
 
 #if IS_REACHABLE(CONFIG_SND_SOC_CS42L43_SDW)
Signature · 1 line
2.39.2
maintainer
#5 lore raw
In reply to Charles Keepax
Quoted 9 lines · Charles Keepax

Fix a mutex inversion in the IRQ handling for the speaker shutters on cs42l43. Do this by adding some new helper functions to get kcontrol pointers and caching those in the driver rather than directly acquiring them in the IRQ handler.

Changes in v4:
- Add the missing helper that was causing problems the cs35l45 update.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/4] ASoC: cs42l43: Remove redundant semi-colon at end of function

      commit: becfa08bfefa2cbb22c84d9e583e81387f2f3bf2
[2/4] ASoC: soc-component: Add new snd_soc_component_get_kcontrol() helpers
      commit: c8a132e2e032b00828d51141ab34f9aeb24f44ae
[3/4] ASoC: cs35l45: Use new snd_soc_component_get_kcontrol_locked() helper
      commit: 4791c422981350d0de4ad02a14a08b99c766d06f
[4/4] ASoC: cs42l43: Cache shutter IRQ control pointers
      commit: 93afd028fb5f06a46a32375fd1f0473451eb1c5a

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark