Skip to content

Commit 2176c6b

Browse files
rfvirgiltiwai
authored andcommitted
ALSA: hda/cs_dsp_ctl: Fix mutex inversion when creating controls
Redesign the creation of ALSA controls so that the cs_dsp pwr_lock is not held when calling snd_ctl_add(). Instead of creating the ALSA control from the cs_dsp control_add callback, do it after cs_dsp_power_up() has completed. The existing functions are changed to return void instead of passing errors back - this duplicates the original behaviour, as cs_dsp does not abort firmware load if creation of a control fails. It is safe to walk the control list without taking any mutex provided that the caller is not trying to load a new firmware or remove the driver in parallel. There is no other situation that the list can change. So the caller can trigger creation of ALSA controls after cs_dsp_power_up() has returned. A cs_dsp control will have a non-NULL priv pointer if we have created an ALSA control. With the previous code the ALSA controls were created from the cs_dsp control_add callback. But this is called with pwr_lock held (as it is part of the DSP power-up sequence). The kernel lock checking will show a mutex inversion between this and the control creation path: control_add pwr_lock held, takes controls_rwsem (in snd_ctl_add) get/put controls_rwsem held, takes pwr_lock to call cs_dsp. This is not completely theoretical. Although the time window is very small, it is possible for these to run in parallel and deadlock the old implementation. Signed-off-by: Richard Fitzgerald <[email protected]> Signed-off-by: Stefan Binding <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 06f3a0a commit 2176c6b

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

sound/pci/hda/cs35l41_hda.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,18 @@ static const struct reg_sequence cs35l41_hda_mute[] = {
9191
{ CS35L41_AMP_DIG_VOL_CTRL, 0x0000A678 }, // AMP_VOL_PCM Mute
9292
};
9393

94-
static int cs35l41_control_add(struct cs_dsp_coeff_ctl *cs_ctl)
94+
static void cs35l41_add_controls(struct cs35l41_hda *cs35l41)
9595
{
96-
struct cs35l41_hda *cs35l41 = container_of(cs_ctl->dsp, struct cs35l41_hda, cs_dsp);
9796
struct hda_cs_dsp_ctl_info info;
9897

9998
info.device_name = cs35l41->amp_name;
10099
info.fw_type = cs35l41->firmware_type;
101100
info.card = cs35l41->codec->card;
102101

103-
return hda_cs_dsp_control_add(cs_ctl, &info);
102+
hda_cs_dsp_add_controls(&cs35l41->cs_dsp, &info);
104103
}
105104

106105
static const struct cs_dsp_client_ops client_ops = {
107-
.control_add = cs35l41_control_add,
108106
.control_remove = hda_cs_dsp_control_remove,
109107
};
110108

@@ -435,6 +433,8 @@ static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
435433
if (ret)
436434
goto err_release;
437435

436+
cs35l41_add_controls(cs35l41);
437+
438438
ret = cs35l41_save_calibration(cs35l41);
439439

440440
err_release:

sound/pci/hda/hda_cs_dsp_ctl.c

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static unsigned int wmfw_convert_flags(unsigned int in)
9797
return out;
9898
}
9999

100-
static int hda_cs_dsp_add_kcontrol(struct hda_cs_dsp_coeff_ctl *ctl, const char *name)
100+
static void hda_cs_dsp_add_kcontrol(struct hda_cs_dsp_coeff_ctl *ctl, const char *name)
101101
{
102102
struct cs_dsp_coeff_ctl *cs_ctl = ctl->cs_ctl;
103103
struct snd_kcontrol_new kcontrol = {0};
@@ -107,7 +107,7 @@ static int hda_cs_dsp_add_kcontrol(struct hda_cs_dsp_coeff_ctl *ctl, const char
107107
if (cs_ctl->len > ADSP_MAX_STD_CTRL_SIZE) {
108108
dev_err(cs_ctl->dsp->dev, "KControl %s: length %zu exceeds maximum %d\n", name,
109109
cs_ctl->len, ADSP_MAX_STD_CTRL_SIZE);
110-
return -EINVAL;
110+
return;
111111
}
112112

113113
kcontrol.name = name;
@@ -120,38 +120,32 @@ static int hda_cs_dsp_add_kcontrol(struct hda_cs_dsp_coeff_ctl *ctl, const char
120120
/* Save ctl inside private_data, ctl is owned by cs_dsp,
121121
* and will be freed when cs_dsp removes the control */
122122
kctl = snd_ctl_new1(&kcontrol, (void *)ctl);
123-
if (!kctl) {
124-
ret = -ENOMEM;
125-
return ret;
126-
}
123+
if (!kctl)
124+
return;
127125

128126
ret = snd_ctl_add(ctl->card, kctl);
129127
if (ret) {
130128
dev_err(cs_ctl->dsp->dev, "Failed to add KControl %s = %d\n", kcontrol.name, ret);
131-
return ret;
129+
return;
132130
}
133131

134132
dev_dbg(cs_ctl->dsp->dev, "Added KControl: %s\n", kcontrol.name);
135133
ctl->kctl = kctl;
136-
137-
return 0;
138134
}
139135

140-
int hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl, struct hda_cs_dsp_ctl_info *info)
136+
static void hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl,
137+
const struct hda_cs_dsp_ctl_info *info)
141138
{
142139
struct cs_dsp *cs_dsp = cs_ctl->dsp;
143140
char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
144141
struct hda_cs_dsp_coeff_ctl *ctl;
145142
const char *region_name;
146143
int ret;
147144

148-
if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
149-
return 0;
150-
151145
region_name = cs_dsp_mem_region_name(cs_ctl->alg_region.type);
152146
if (!region_name) {
153-
dev_err(cs_dsp->dev, "Unknown region type: %d\n", cs_ctl->alg_region.type);
154-
return -EINVAL;
147+
dev_warn(cs_dsp->dev, "Unknown region type: %d\n", cs_ctl->alg_region.type);
148+
return;
155149
}
156150

157151
ret = scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s %s %.12s %x", info->device_name,
@@ -171,22 +165,39 @@ int hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl, struct hda_cs_dsp_ct
171165

172166
ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
173167
if (!ctl)
174-
return -ENOMEM;
168+
return;
175169

176170
ctl->cs_ctl = cs_ctl;
177171
ctl->card = info->card;
178172
cs_ctl->priv = ctl;
179173

180-
ret = hda_cs_dsp_add_kcontrol(ctl, name);
181-
if (ret) {
182-
dev_err(cs_dsp->dev, "Error (%d) adding control %s\n", ret, name);
183-
kfree(ctl);
184-
return ret;
185-
}
174+
hda_cs_dsp_add_kcontrol(ctl, name);
175+
}
186176

187-
return 0;
177+
void hda_cs_dsp_add_controls(struct cs_dsp *dsp, const struct hda_cs_dsp_ctl_info *info)
178+
{
179+
struct cs_dsp_coeff_ctl *cs_ctl;
180+
181+
/*
182+
* pwr_lock would cause mutex inversion with ALSA control lock compared
183+
* to the get/put functions.
184+
* It is safe to walk the list without holding a mutex because entries
185+
* are persistent and only cs_dsp_power_up() or cs_dsp_remove() can
186+
* change the list.
187+
*/
188+
lockdep_assert_not_held(&dsp->pwr_lock);
189+
190+
list_for_each_entry(cs_ctl, &dsp->ctl_list, list) {
191+
if (cs_ctl->flags & WMFW_CTL_FLAG_SYS)
192+
continue;
193+
194+
if (cs_ctl->priv)
195+
continue;
196+
197+
hda_cs_dsp_control_add(cs_ctl, info);
198+
}
188199
}
189-
EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_control_add, SND_HDA_CS_DSP_CONTROLS);
200+
EXPORT_SYMBOL_NS_GPL(hda_cs_dsp_add_controls, SND_HDA_CS_DSP_CONTROLS);
190201

191202
void hda_cs_dsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl)
192203
{

sound/pci/hda/hda_cs_dsp_ctl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct hda_cs_dsp_ctl_info {
2929

3030
extern const char * const hda_cs_dsp_fw_ids[HDA_CS_DSP_NUM_FW];
3131

32-
int hda_cs_dsp_control_add(struct cs_dsp_coeff_ctl *cs_ctl, struct hda_cs_dsp_ctl_info *info);
32+
void hda_cs_dsp_add_controls(struct cs_dsp *dsp, const struct hda_cs_dsp_ctl_info *info);
3333
void hda_cs_dsp_control_remove(struct cs_dsp_coeff_ctl *cs_ctl);
3434
int hda_cs_dsp_write_ctl(struct cs_dsp *dsp, const char *name, int type,
3535
unsigned int alg, const void *buf, size_t len);

0 commit comments

Comments
 (0)