Skip to content

Commit eba2eb2

Browse files
rfvirgilbroonie
authored andcommitted
ASoC: soc-card: Fix missing locking in snd_soc_card_get_kcontrol()
snd_soc_card_get_kcontrol() must be holding a read lock on card->controls_rwsem while walking the controls list. Compare with snd_ctl_find_numid(). The existing function is renamed snd_soc_card_get_kcontrol_locked() so that it can be called from contexts that are already holding card->controls_rwsem (for example, control get/put functions). There are few direct or indirect callers of snd_soc_card_get_kcontrol(), and most are safe. Three require changes, which have been included in this patch: codecs/cs35l45.c: cs35l45_activate_ctl() is called from a control put() function so is changed to call snd_soc_card_get_kcontrol_locked(). codecs/cs35l56.c: cs35l56_sync_asp1_mixer_widgets_with_firmware() is called from control get()/put() functions so is changed to call snd_soc_card_get_kcontrol_locked(). fsl/fsl_xcvr.c: fsl_xcvr_activate_ctl() is called from three places, one of which already holds card->controls_rwsem: 1. fsl_xcvr_mode_put(), a control put function, which will already be holding card->controls_rwsem. 2. fsl_xcvr_startup(), a DAI startup function. 3. fsl_xcvr_shutdown(), a DAI shutdown function. To fix this, fsl_xcvr_activate_ctl() has been changed to call snd_soc_card_get_kcontrol_locked() so that it is safe to call directly from fsl_xcvr_mode_put(). The fsl_xcvr_startup() and fsl_xcvr_shutdown() functions have been changed to take a read lock on card->controls_rsem() around calls to fsl_xcvr_activate_ctl(). While this is not very elegant, it keeps the change small, to avoid this patch creating a large collateral churn in fsl/fsl_xcvr.c. Analysis of other callers of snd_soc_card_get_kcontrol() is that they do not need any changes, they are not holding card->controls_rwsem when they call snd_soc_card_get_kcontrol(). Direct callers of snd_soc_card_get_kcontrol(): fsl/fsl_spdif.c: fsl_spdif_dai_probe() - DAI probe function fsl/fsl_micfil.c: voice_detected_fn() - IRQ handler Indirect callers via soc_component_notify_control(): codecs/cs42l43: cs42l43_mic_shutter() - IRQ handler codecs/cs42l43: cs42l43_spk_shutter() - IRQ handler codecs/ak4118.c: ak4118_irq_handler() - IRQ handler codecs/wm_adsp.c: wm_adsp_write_ctl() - not currently used Indirect callers via snd_soc_limit_volume(): qcom/sc8280xp.c: sc8280xp_snd_init() - DAIlink init function ti/rx51.c: rx51_aic34_init() - DAI init function I don't have hardware to test the fsl/*, qcom/sc828xp.c, ti/rx51.c and ak4118.c changes. Backport note: The fsl/, qcom/, cs35l45, cs35l56 and cs42l43 callers were added since the Fixes commit so won't all be present on older kernels. Signed-off-by: Richard Fitzgerald <[email protected]> Fixes: 209c6cd ("ASoC: soc-card: move snd_soc_card_get_kcontrol() to soc-card") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent 1382d8b commit eba2eb2

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

include/sound/soc-card.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static inline void snd_soc_card_mutex_unlock(struct snd_soc_card *card)
3030

3131
struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
3232
const char *name);
33+
struct snd_kcontrol *snd_soc_card_get_kcontrol_locked(struct snd_soc_card *soc_card,
34+
const char *name);
3335
int snd_soc_card_jack_new(struct snd_soc_card *card, const char *id, int type,
3436
struct snd_soc_jack *jack);
3537
int snd_soc_card_jack_new_pins(struct snd_soc_card *card, const char *id,

sound/soc/codecs/cs35l45.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static int cs35l45_activate_ctl(struct snd_soc_component *component,
184184
else
185185
snprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s", ctl_name);
186186

187-
kcontrol = snd_soc_card_get_kcontrol(component->card, name);
187+
kcontrol = snd_soc_card_get_kcontrol_locked(component->card, name);
188188
if (!kcontrol) {
189189
dev_err(component->dev, "Can't find kcontrol %s\n", name);
190190
return -EINVAL;

sound/soc/codecs/cs35l56.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static int cs35l56_sync_asp1_mixer_widgets_with_firmware(struct cs35l56_private
114114
name = full_name;
115115
}
116116

117-
kcontrol = snd_soc_card_get_kcontrol(dapm->card, name);
117+
kcontrol = snd_soc_card_get_kcontrol_locked(dapm->card, name);
118118
if (!kcontrol) {
119119
dev_warn(cs35l56->base.dev, "Could not find control %s\n", name);
120120
continue;

sound/soc/fsl/fsl_xcvr.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ static int fsl_xcvr_activate_ctl(struct snd_soc_dai *dai, const char *name,
174174
struct snd_kcontrol *kctl;
175175
bool enabled;
176176

177-
kctl = snd_soc_card_get_kcontrol(card, name);
177+
lockdep_assert_held(&card->snd_card->controls_rwsem);
178+
179+
kctl = snd_soc_card_get_kcontrol_locked(card, name);
178180
if (kctl == NULL)
179181
return -ENOENT;
180182

@@ -576,10 +578,14 @@ static int fsl_xcvr_startup(struct snd_pcm_substream *substream,
576578
xcvr->streams |= BIT(substream->stream);
577579

578580
if (!xcvr->soc_data->spdif_only) {
581+
struct snd_soc_card *card = dai->component->card;
582+
579583
/* Disable XCVR controls if there is stream started */
584+
down_read(&card->snd_card->controls_rwsem);
580585
fsl_xcvr_activate_ctl(dai, fsl_xcvr_mode_kctl.name, false);
581586
fsl_xcvr_activate_ctl(dai, fsl_xcvr_arc_mode_kctl.name, false);
582587
fsl_xcvr_activate_ctl(dai, fsl_xcvr_earc_capds_kctl.name, false);
588+
up_read(&card->snd_card->controls_rwsem);
583589
}
584590

585591
return 0;
@@ -598,11 +604,15 @@ static void fsl_xcvr_shutdown(struct snd_pcm_substream *substream,
598604
/* Enable XCVR controls if there is no stream started */
599605
if (!xcvr->streams) {
600606
if (!xcvr->soc_data->spdif_only) {
607+
struct snd_soc_card *card = dai->component->card;
608+
609+
down_read(&card->snd_card->controls_rwsem);
601610
fsl_xcvr_activate_ctl(dai, fsl_xcvr_mode_kctl.name, true);
602611
fsl_xcvr_activate_ctl(dai, fsl_xcvr_arc_mode_kctl.name,
603612
(xcvr->mode == FSL_XCVR_MODE_ARC));
604613
fsl_xcvr_activate_ctl(dai, fsl_xcvr_earc_capds_kctl.name,
605614
(xcvr->mode == FSL_XCVR_MODE_EARC));
615+
up_read(&card->snd_card->controls_rwsem);
606616
}
607617
ret = regmap_update_bits(xcvr->regmap, FSL_XCVR_EXT_IER0,
608618
FSL_XCVR_IRQ_EARC_ALL, 0);

sound/soc/soc-card.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// Copyright (C) 2019 Renesas Electronics Corp.
66
// Kuninori Morimoto <[email protected]>
77
//
8+
9+
#include <linux/lockdep.h>
10+
#include <linux/rwsem.h>
811
#include <sound/soc.h>
912
#include <sound/jack.h>
1013

@@ -26,12 +29,15 @@ static inline int _soc_card_ret(struct snd_soc_card *card,
2629
return ret;
2730
}
2831

29-
struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
30-
const char *name)
32+
struct snd_kcontrol *snd_soc_card_get_kcontrol_locked(struct snd_soc_card *soc_card,
33+
const char *name)
3134
{
3235
struct snd_card *card = soc_card->snd_card;
3336
struct snd_kcontrol *kctl;
3437

38+
/* must be held read or write */
39+
lockdep_assert_held(&card->controls_rwsem);
40+
3541
if (unlikely(!name))
3642
return NULL;
3743

@@ -40,6 +46,20 @@ struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
4046
return kctl;
4147
return NULL;
4248
}
49+
EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol_locked);
50+
51+
struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
52+
const char *name)
53+
{
54+
struct snd_card *card = soc_card->snd_card;
55+
struct snd_kcontrol *kctl;
56+
57+
down_read(&card->controls_rwsem);
58+
kctl = snd_soc_card_get_kcontrol_locked(soc_card, name);
59+
up_read(&card->controls_rwsem);
60+
61+
return kctl;
62+
}
4363
EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
4464

4565
static int jack_new(struct snd_soc_card *card, const char *id, int type,

0 commit comments

Comments
 (0)