mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] ASoC: SOF: control: Check the volume and switch values
@ 2026-11-18  9:38 HyeongJun An
  2026-11-18  9:38 ` [PATCH v2 1/2] ASoC: SOF: control: Report the volume range as ASoC does HyeongJun An
  2026-11-18  9:38 ` [PATCH v2 2/2] ASoC: SOF: control: Reject out of range volume and switch values HyeongJun An
  0 siblings, 2 replies; 3+ messages in thread
From: HyeongJun An @ 2026-11-18  9:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Peter Ujfalusi, Liam Girdwood, Bard Liao, Daniel Baluta,
	Kai Vehmanen, Pierre-Louis Bossart, Vijendar Mukunda,
	Jaroslav Kysela, Takashi Iwai, Arnav Kapoor, sound-open-firmware,
	linux-sound, linux-kernel, HyeongJun An

SOF volume and switch controls accept any value from user space. Patch 2
rejects values outside the range the info callbacks report.

Peter pointed out in v1 that snd_sof_volume_info(), which the controls
with more than two channels use, reports the maximum differently from
snd_soc_info_volsw(). Patch 1 makes it report the same range, and patch
2 checks against that range.

Patches 1 and 2 of Arnav Kapoor's series overlap with patch 2 here:
https://lore.kernel.org/all/20261117105953.188645-1-kapoorarnav43@gmail.com/

Changes in v2:
- Add patch 1 to align snd_sof_volume_info() with snd_soc_info_volsw()
  (Peter Ujfalusi)
- Patch 2 takes the maximum from the helper added in patch 1

v1: https://lore.kernel.org/all/20261114021730.2349347-1-sammiee5311@gmail.com/

Tested on a Dell Latitude 9420 (Tiger Lake, SOF IPC3,
sof-tgl-rt711-rt1316-rt714.tplg) with only snd-sof replaced. The
mixer-test failures went from 9 to 0 and pcm-test is unchanged. Out of
range writes to the volumes and switches now fail with -EINVAL, and in
range writes still work. The laptop has no SOF control with more than
two channels, so patch 1 changes nothing there. The reported range of
every SOF control is the same before and after.

Prepared with Claude Code (Claude Opus 5.5).

HyeongJun An (2):
  ASoC: SOF: control: Report the volume range as ASoC does
  ASoC: SOF: control: Reject out of range volume and switch values

 sound/soc/sof/control.c | 49 +++++++++++++++++++++++++++++++++++------
 1 file changed, 42 insertions(+), 7 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 1/2] ASoC: SOF: control: Report the volume range as ASoC does
  2026-11-18  9:38 [PATCH v2 0/2] ASoC: SOF: control: Check the volume and switch values HyeongJun An
@ 2026-11-18  9:38 ` HyeongJun An
  2026-11-18  9:38 ` [PATCH v2 2/2] ASoC: SOF: control: Reject out of range volume and switch values HyeongJun An
  1 sibling, 0 replies; 3+ messages in thread
From: HyeongJun An @ 2026-11-18  9:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Peter Ujfalusi, Liam Girdwood, Bard Liao, Daniel Baluta,
	Kai Vehmanen, Pierre-Louis Bossart, Vijendar Mukunda,
	Jaroslav Kysela, Takashi Iwai, Arnav Kapoor, sound-open-firmware,
	linux-sound, linux-kernel, HyeongJun An

SOF controls with more than two channels use snd_sof_volume_info()
instead of snd_soc_info_volsw(). The snd_sof_volume_info() reports
platform_max - min as the maximum, while snd_soc_info_volsw() reports
max - min, limited by platform_max. With min 10, max 100 and
platform_max 80, a control reports 70 with more than two channels and
80 with up to two.

To fix this, compute the maximum the way snd_soc_info_volsw() does. The
helper is reused by the next patch to check the values written to the
controls. It also no longer stores max in platform_max as a side
effect.

No SOF topology sets min or platform_max today, so the reported ranges
do not change.

Suggested-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
Signed-off-by: HyeongJun An <sammiee5311@gmail.com>
---
 sound/soc/sof/control.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/sound/soc/sof/control.c b/sound/soc/sof/control.c
index 74d997a4f620..136e2ce15201 100644
--- a/sound/soc/sof/control.c
+++ b/sound/soc/sof/control.c
@@ -15,6 +15,17 @@
 #include "sof-priv.h"
 #include "sof-audio.h"
 
+/* the maximum value as snd_soc_info_volsw() reports it */
+static int sof_mixer_max(const struct soc_mixer_control *sm)
+{
+	int max = sm->max - sm->min;
+
+	if (sm->platform_max && sm->platform_max < max)
+		max = sm->platform_max;
+
+	return max;
+}
+
 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
 		       struct snd_ctl_elem_value *ucontrol)
 {
@@ -50,20 +61,16 @@ int snd_sof_volume_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info
 	struct soc_mixer_control *sm = (struct soc_mixer_control *)kcontrol->private_value;
 	struct snd_sof_control *scontrol = sm->dobj.private;
 	unsigned int channels = scontrol->num_channels;
-	int platform_max;
-
-	if (!sm->platform_max)
-		sm->platform_max = sm->max;
-	platform_max = sm->platform_max;
+	int max = sof_mixer_max(sm);
 
-	if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
+	if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
 		uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
 	else
 		uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 
 	uinfo->count = channels;
 	uinfo->value.integer.min = 0;
-	uinfo->value.integer.max = platform_max - sm->min;
+	uinfo->value.integer.max = max;
 	return 0;
 }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2 2/2] ASoC: SOF: control: Reject out of range volume and switch values
  2026-11-18  9:38 [PATCH v2 0/2] ASoC: SOF: control: Check the volume and switch values HyeongJun An
  2026-11-18  9:38 ` [PATCH v2 1/2] ASoC: SOF: control: Report the volume range as ASoC does HyeongJun An
@ 2026-11-18  9:38 ` HyeongJun An
  1 sibling, 0 replies; 3+ messages in thread
From: HyeongJun An @ 2026-11-18  9:38 UTC (permalink / raw)
  To: Mark Brown
  Cc: Peter Ujfalusi, Liam Girdwood, Bard Liao, Daniel Baluta,
	Kai Vehmanen, Pierre-Louis Bossart, Vijendar Mukunda,
	Jaroslav Kysela, Takashi Iwai, Arnav Kapoor, sound-open-firmware,
	linux-sound, linux-kernel, HyeongJun An

The snd_sof_volume_put() and snd_sof_switch_put() pass the user values
to the IPC put handlers without a range check. The switch handlers
store any value and send it to the firmware, so on IPC3 writing 2 or -1
to a switch reads back as 2 or 4294967295. The volume handlers convert
the value to u32 and clamp it to the end of the volume table. Writing -1
sets the maximum volume, and on 64-bit writing LONG_MIN sets the
minimum, both without an error.

Return -EINVAL for values outside the range the info callbacks report,
as snd_soc_put_volsw() does. This fixes the write_invalid failures in
mixer-test, and the event_spurious failures caused by the events those
writes generated.

Fixes: c3078f539704 ("ASoC: SOF: Add Sound Open Firmware KControl support")
Signed-off-by: HyeongJun An <sammiee5311@gmail.com>
---
 sound/soc/sof/control.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/sound/soc/sof/control.c b/sound/soc/sof/control.c
index 136e2ce15201..dcf101f7e1d5 100644
--- a/sound/soc/sof/control.c
+++ b/sound/soc/sof/control.c
@@ -26,6 +26,24 @@ static int sof_mixer_max(const struct soc_mixer_control *sm)
 	return max;
 }
 
+/* the IPC put handlers do not check the range */
+static int sof_mixer_check_values(const struct soc_mixer_control *sm,
+				  const struct snd_sof_control *scontrol,
+				  const struct snd_ctl_elem_value *ucontrol)
+{
+	int max = sof_mixer_max(sm);
+	unsigned int i;
+
+	for (i = 0; i < scontrol->num_channels; i++) {
+		long val = ucontrol->value.integer.value[i];
+
+		if (val < 0 || val > max)
+			return -EINVAL;
+	}
+
+	return 0;
+}
+
 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
 		       struct snd_ctl_elem_value *ucontrol)
 {
@@ -49,6 +67,11 @@ int snd_sof_volume_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);
+	int ret;
+
+	ret = sof_mixer_check_values(sm, scontrol, ucontrol);
+	if (ret)
+		return ret;
 
 	if (tplg_ops && tplg_ops->control && tplg_ops->control->volume_put)
 		return tplg_ops->control->volume_put(scontrol, ucontrol);
@@ -97,6 +120,11 @@ int snd_sof_switch_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);
+	int ret;
+
+	ret = sof_mixer_check_values(sm, scontrol, ucontrol);
+	if (ret)
+		return ret;
 
 	if (tplg_ops && tplg_ops->control && tplg_ops->control->switch_put)
 		return tplg_ops->control->switch_put(scontrol, ucontrol);
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-18  9:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18  9:38 [PATCH v2 0/2] ASoC: SOF: control: Check the volume and switch values HyeongJun An
2026-11-18  9:38 ` [PATCH v2 1/2] ASoC: SOF: control: Report the volume range as ASoC does HyeongJun An
2026-11-18  9:38 ` [PATCH v2 2/2] ASoC: SOF: control: Reject out of range volume and switch values HyeongJun An

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®