Skip to contentSkip to conversation

ASoC: SOF: control: fix volume, switch and enum value handling

PATCH Superseded Arnav Kapoor 3 participants 7 messages Nov 17

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover ASoC: SOF: control: fix volume, switch and enum value handling

The ALSA mixer-test selftest fails on the SOF controls of a Lenovo V14 G2 ITL (Tiger Lake, sof-hda-generic-2ch.tplg). The HDA codec controls on the same card pass. These patches fix what it found, plus one case it doesn't catch:

1: a negative volume sets the control to its maximum, because
mixer_to_ipc() takes an unsigned int and clamps.
2: a switch accepts and stores values other than 0 and 1. 3: writing back the volume that was just read generates a change
event, because put() compares linear gains while get() reports table steps, and the 0 dB default is not a table entry.
4: an enum accepts item indexes past its last item (IPC4 topology
only has one, "Dmic0 Capture TDFB angle set enum").

Patches 1, 2 and 4 are in the IPC-independent control.c; patch 3 touches the IPC3 and IPC4 volume_put() ops.

Tested on that laptop (7.0.14 kernel, snd-sof rebuilt with these patches) with mixer-test:
- IPC3 (sof-tgl.ri, sof-hda-generic-2ch.tplg):
287 pass / 8 fail before, 295 pass / 0 fail after
- IPC4 (sof-ipc4/tgl, sof-ipc4-tplg/sof-hda-generic-2ch.tplg):
287 pass / 12 fail before, 296 pass / 3 fail after
The three remaining IPC4 failures are control names in the topology (boolean controls named "... switch" instead of "... Switch").

I also wrote out-of-range values to every control with plain alsa-lib calls (amixer clamps them before they reach the kernel). Before the series every SOF volume and switch accepted them with IPC3; after it every SOF volume, switch and enum rejects them with -EINVAL, with both IPC3 and IPC4. pcm-test results are the same with and without the patches.

Arnav Kapoor (4):
ASoC: SOF: control: reject out-of-range volume values
ASoC: SOF: control: reject invalid switch values
ASoC: SOF: report volume changes in mixer steps
ASoC: SOF: control: reject out-of-range enum values

 sound/soc/sof/control.c      | 26 ++++++++++++++++++++++++++
 sound/soc/sof/ipc3-control.c |  4 +++-
 sound/soc/sof/ipc4-control.c |  4 +++-
 3 files changed, 32 insertions(+), 2 deletions(-)

base-commit: 7cd6b1fd8f9c1fcb1da1b761d502649f50b9800f

Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/4 ASoC: SOF: control: reject out-of-range volume values

snd_sof_volume_put() passes the values from user space to the IPC volume_put() op without checking them. The IPC3 and IPC4 ops convert each value with mixer_to_ipc(), which takes an unsigned int and clamps anything past the end of the volume table to its last entry. A negative value therefore sets the volume to the maximum: on a Tiger Lake laptop with sof-hda-generic-2ch.tplg, writing -1 to "PGA2.0 2 Master Capture Volume" (0..80, set to 50) succeeds and reads back 80.

Reject values outside 0..max, as the ALSA control API expects, before calling the op.

Fixes c3078f539704 ("ASoC: SOF: Add Sound Open Firmware KControl support")
---
 sound/soc/sof/control.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
sound/soc/sof/control.c +18 -0
index 74d997a4f620..02cf59bf1f95 100644
@@ -15,6 +15,21 @@
 #include "sof-priv.h"
 #include "sof-audio.h"
 
+static bool snd_sof_values_in_range(const struct snd_sof_control *scontrol,
+				    const struct snd_ctl_elem_value *ucontrol)
+{
+	unsigned int i;
+
+	for (i = 0; i < scontrol->num_channels; i++) {
+		long value = ucontrol->value.integer.value[i];
+
+		if (value < 0 || value > scontrol->max)
+			return false;
+	}
+
+	return true;
+}
+
 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
 		       struct snd_ctl_elem_value *ucontrol)
 {
@@ -39,6 +54,9 @@ int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
 
+	if (!snd_sof_values_in_range(scontrol, ucontrol))
+		return -EINVAL;
+
 	if (tplg_ops && tplg_ops->control && tplg_ops->control->volume_put)
 		return tplg_ops->control->volume_put(scontrol, ucontrol);
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/4 ASoC: SOF: control: reject invalid switch values

In reply to Arnav Kapoor

snd_sof_switch_put() passes the values from user space to the IPC switch_put() op unchecked, and the IPC3 and IPC4 ops store them as they are and send them to the DSP. Writing 2 to a switch such as "Dmic0 Capture Switch" succeeds and reads back 2, and writing -1 reads back 4294967295, neither of which is a valid boolean value. The ALSA mixer-test selftest reports this as a write_invalid failure.

Reject values other than 0 and 1, using the same range check as the volume controls.

Fixes c3078f539704 ("ASoC: SOF: Add Sound Open Firmware KControl support")
---
 sound/soc/sof/control.c | 3 +++
 1 file changed, 3 insertions(+)
sound/soc/sof/control.c +3 -0
index 02cf59bf1f95..a36c4c1d4505 100644
@@ -109,6 +109,9 @@ int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
 
+	if (!snd_sof_values_in_range(scontrol, ucontrol))
+		return -EINVAL;
+
 	if (tplg_ops && tplg_ops->control && tplg_ops->control->switch_put)
 		return tplg_ops->control->switch_put(scontrol, ucontrol);
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH 3/4 ASoC: SOF: report volume changes in mixer steps

In reply to Arnav Kapoor

The IPC3 and IPC4 volume_put() ops decide whether the control changed by comparing the new linear gain from the volume table with the stored one. The stored gain does not always come from the table: topology loading initialises it to 0 dB (VOL_ZERO_DB or SOF_IPC4_VOL_ZERO_DB), and volume_get() reports that as the first table entry at or above it, which is usually not exactly 0 dB. Writing back the value that was just read then changes the stored gain, so put() returns true and user space gets a change notification although the control value is the same.

On a Tiger Lake laptop with sof-hda-generic-2ch.tplg (IPC3), the ALSA mixer-test selftest reports this as event_spurious failures for every PGA playback volume and for "Dmic0 Capture Volume".

Compare in mixer steps instead, using the same ipc_to_mixer() conversion as volume_get(), so put() returns true only when the value user space sees changes.

Fixes 95a32c98055f ("ASoC: SOF: control: return true when kcontrol values change")
Fixes 955e84fc0b6d ("ASoC: SOF: ipc4-topology: Add control IO ops")
---
 sound/soc/sof/ipc3-control.c | 4 +++-
 sound/soc/sof/ipc4-control.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
sound/soc/sof/ipc3-control.c +3 -1
index b1d00a2a860a..b8f4d963e07d 100644
@@ -180,10 +180,12 @@ static bool sof_ipc3_volume_put(struct snd_sof_control *scontrol,
 
 	/* update each channel */
 	for (i = 0; i < channels; i++) {
+		u32 old = ipc_to_mixer(cdata->chanv[i].value,
+				       scontrol->volume_table, scontrol->max + 1);
 		u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
 					 scontrol->volume_table, scontrol->max + 1);
 
-		change = change || (value != cdata->chanv[i].value);
+		change = change || (ucontrol->value.integer.value[i] != old);
 		cdata->chanv[i].channel = i;
 		cdata->chanv[i].value = value;
 	}
sound/soc/sof/ipc4-control.c +3 -1
index e29c8d89284a..8bf300373d22 100644
@@ -153,10 +153,12 @@ static bool sof_ipc4_volume_put(struct snd_sof_control *scontrol,
 
 	/* update each channel */
 	for (i = 0; i < channels; i++) {
+		u32 old = ipc_to_mixer(cdata->chanv[i].value,
+				       scontrol->volume_table, scontrol->max + 1);
 		u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
 					 scontrol->volume_table, scontrol->max + 1);
 
-		change = change || (value != cdata->chanv[i].value);
+		change = change || (ucontrol->value.integer.value[i] != old);
 		cdata->chanv[i].channel = i;
 		cdata->chanv[i].value = value;
 	}
Signature · 1 line
2.53.0
maintainer
#4 lore raw

Re: [PATCH 3/4] ASoC: SOF: report volume changes in mixer steps

Quoted 10 lines

The IPC3 and IPC4 volume_put() ops decide whether the control changed by comparing the new linear gain from the volume table with the stored one. The stored gain does not always come from the table: topology loading initialises it to 0 dB (VOL_ZERO_DB or SOF_IPC4_VOL_ZERO_DB), and volume_get() reports that as the first table entry at or above it, which is usually not exactly 0 dB. Writing back the value that was just read then changes the stored gain, so put() returns true and user space gets a change notification although the control value is the same.

sound/soc/sof/ipc3-control.c static bool sof_ipc3_volume_put(struct snd_sof_control *scontrol, · line 188

Quoted 10 lines
sound/soc/sof/ipc3-control.c +3 -1
@@ -180,10 +180,12 @@ static bool sof_ipc3_volume_put(struct snd_sof_control *scontrol,
 
 	/* update each channel */
 	for (i = 0; i < channels; i++) {
+		u32 old = ipc_to_mixer(cdata->chanv[i].value,
+				       scontrol->volume_table, scontrol->max + 1);
 		u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],
 					 scontrol->volume_table, scontrol->max + 1);
 
-		change = change || (value != cdata->chanv[i].value);
+		change = change || (ucontrol->value.integer.value[i] != old);

You need to compare ipc_to_mixer(), there can be duplicated steps.

Signature · 1 line
Mark Brown <[email protected]>
#5 lore raw

PATCH 4/4 ASoC: SOF: control: reject out-of-range enum values

In reply to Arnav Kapoor

snd_sof_enum_put() passes the item indexes from user space to the IPC enum_put() op unchecked, and the IPC3 and IPC4 ops store them as they are and send them to the DSP. With the IPC4 sof-hda-generic-2ch.tplg on a Tiger Lake laptop, "Dmic0 Capture TDFB angle set enum" has 13 items but accepts 13 and 0xffffffff, and reads them back. The ALSA mixer-test selftest reports this as a write_invalid failure.

Reject indexes past the last item, as snd_soc_put_enum_double() does.

Fixes c3078f539704 ("ASoC: SOF: Add Sound Open Firmware KControl support")
---
 sound/soc/sof/control.c | 5 +++++
 1 file changed, 5 insertions(+)
sound/soc/sof/control.c +5 -0
index a36c4c1d4505..aa02376d61e6 100644
@@ -141,6 +141,11 @@ int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
 	struct snd_soc_component *scomp = scontrol->scomp;
 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
 	const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
+	unsigned int i;
+
+	for (i = 0; i < scontrol->num_channels; i++)
+		if (ucontrol->value.enumerated.item[i] >= se->items)
+			return -EINVAL;
 
 	if (tplg_ops && tplg_ops->control && tplg_ops->control->enum_put)
 		return tplg_ops->control->enum_put(scontrol, ucontrol);
Signature · 1 line
2.53.0