Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH] ALSA: control: Verify put() result when in debug mode
@ 2026-01-30 13:55 Cezary Rojewski
  2026-01-30 14:12 ` Mark Brown
  2026-02-02  0:20 ` Kuninori Morimoto
  0 siblings, 2 replies; 11+ messages in thread
From: Cezary Rojewski @ 2026-01-30 13:55 UTC (permalink / raw)
  To: tiwai
  Cc: broonie, perex, amade, kuninori.morimoto.gx, linux-sound,
	Cezary Rojewski

The put() operation is expected to return:
1) 0 on success if no changes were made
2) 1 on success if changes were made
3) error code otherwise

Currently 2) is usually ignored when writing control-operations. While
forcing compliance is not an option right now, make it easier for
developers to adhere to the expectations and notice problems by logging
them when CONFIG_SND_CTL_DEBUG is enabled.

Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com>
---
 sound/core/control.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)

diff --git a/sound/core/control.c b/sound/core/control.c
index 9c3fd5113a61..257976efece1 100644
--- a/sound/core/control.c
+++ b/sound/core/control.c
@@ -1264,6 +1264,32 @@ static int snd_ctl_elem_read_user(struct snd_card *card,
 	return result;
 }
 
+__maybe_unused
+static int snd_ctl_write_verify(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *control)
+{
+	struct snd_ctl_elem_value original;
+	int ret, retcmp;
+
+	ret = kctl->get(kctl, &original);
+	if (ret)
+		return ret;
+
+	ret = kctl->put(kctl, control);
+	if (ret < 0)
+		return ret;
+
+	retcmp = memcmp(&original.value.bytes.data[0], &control->value.bytes.data[0],
+			sizeof(original.value.bytes.data[0]));
+	if (retcmp)
+		retcmp = 1;
+
+	if (retcmp == ret)
+		pr_info("kctl->put() returned the expected value of '%d'\n", ret);
+	else
+		pr_warn("expected kctl->put() to return '%d' but got '%d'\n", ret, retcmp);
+	return ret;
+}
+
 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
 			      struct snd_ctl_elem_value *control)
 {
@@ -1299,8 +1325,13 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
 			result = sanity_check_input_values(card, control, &info,
 							   false);
 	}
-	if (!result)
-		result = kctl->put(kctl, control);
+
+	if (!result) {
+		if (IS_ENABLED(CONFIG_SND_CTL_DEBUG))
+			result = snd_ctl_write_verify(kctl, control);
+		else
+			result = kctl->put(kctl, control);
+	}
 	if (result < 0) {
 		up_write(&card->controls_rwsem);
 		return result;
-- 
2.34.1


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

end of thread, other threads:[~2026-02-04 11:37 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-30 13:55 [PATCH] ALSA: control: Verify put() result when in debug mode Cezary Rojewski
2026-01-30 14:12 ` Mark Brown
2026-01-30 14:29   ` Takashi Iwai
2026-01-30 15:00     ` Cezary Rojewski
2026-01-30 15:13       ` Mark Brown
2026-02-04 11:37         ` Cezary Rojewski
2026-01-30 15:14       ` Takashi Iwai
2026-02-04 11:33         ` Cezary Rojewski
2026-01-30 14:49   ` Cezary Rojewski
2026-02-02  0:20 ` Kuninori Morimoto
2026-02-04 11:30   ` Cezary Rojewski

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