Skip to contentSkip to conversation

ALSA: control: Verify put() result when in debug mode

PATCH Superseded Cezary Rojewski 4 participants 6 messages Feb 4 Sound

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ALSA: control: Verify put() result when in debug mode

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.

---

Changes in v2:

A number of fixes as suggested by Mark and Takashi. The initial version did not account for possibility of invalid payload sent from the userspace and was buggy.
- enlisted ->info() operation and reused existing fill_remaining_elem_value() to sanitize the 'new' value provided by user
- fixed size provided to memcmp()
- the 'original' value is now initilized with memset()

Readability improvements suggested by Kuninori.
- added conditional #define snd_ctl_put() so that no additional if-statements are needed in the actual code.

 sound/core/control.c | 46 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
sound/core/control.c +45 -1
index 9c3fd5113a61..6515861bbd84 100644
@@ -1264,6 +1264,49 @@ static int snd_ctl_elem_read_user(struct snd_card *card,
 	return result;
 }
 
+#if IS_ENABLED(CONFIG_SND_CTL_DEBUG)
+static int snd_ctl_put_verify(struct snd_kcontrol *kctl, struct snd_ctl_elem_value *control)
+{
+	struct snd_ctl_elem_value original;
+	struct snd_ctl_elem_info info;
+	int ret, retcmp;
+
+	memset(&original, 0, sizeof(original));
+	memset(&info, 0, sizeof(info));
+
+	ret = kctl->get(kctl, &original);
+	if (ret)
+		return ret;
+
+	/* Sanitize the new value (control->value) before comparing. */
+	ret = kctl->info(kctl, &info);
+	if (ret)
+		return ret;
+
+	ret = kctl->put(kctl, control);
+	if (ret < 0)
+		return ret;
+
+	fill_remaining_elem_value(control, &info, 0);
+
+	/* With known state for both new and original, do the comparison. */
+	retcmp = memcmp(&original.value.bytes.data[0], &control->value.bytes.data[0],
+			sizeof(original.value.bytes.data));
+	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;
+}
+
+#define snd_ctl_put(kctl, control)	snd_ctl_put_verify(kctl, control)
+#else
+#define snd_ctl_put(kctl, control)	((kctl)->put(kctl, control))
+#endif
+
 static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
 			      struct snd_ctl_elem_value *control)
 {
@@ -1299,8 +1342,9 @@ 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);
+		result = snd_ctl_put(kctl, control);
 	if (result < 0) {
 		up_write(&card->controls_rwsem);
 		return result;
Signature · 1 line
2.34.1
maintainer
#1 lore raw

sound/core/control.c static int snd_ctl_elem_read_user(struct snd_card *card, · line 1293

Quoted 2 lines · Cezary Rojewski
sound/core/control.c +2 -0
+	/* With known state for both new and original, do the comparison. */
+	retcmp = memcmp(&original.value.bytes.data[0], &control->value.bytes.data[0],

I think that shorter &original.value and &control->value pointer reference should be enough here. It's also more readable.

sound/core/control.c static int snd_ctl_elem_read_user(struct snd_card *card, · line 1301

Quoted 8 lines
sound/core/control.c +8 -0
+			sizeof(original.value.bytes.data));
+	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);

For volatile controls (SNDRV_CTL_ELEM_ACCESS_VOLATILE), the result may be fine. I would downgrade the level to pr_info for those controls here with another message.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#2 lore raw
Quoted 13 lines · Jaroslav Kysela

sound/core/control.c static int snd_ctl_elem_read_user(struct snd_card *card, · line 1301

Quoted 8 lines · Cezary Rojewski
sound/core/control.c +8 -0
+			sizeof(original.value.bytes.data));
+	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);

For volatile controls (SNDRV_CTL_ELEM_ACCESS_VOLATILE), the result may be fine. I would downgrade the level to pr_info for those controls here with another message.

I believe we can simply skip the validation for controls with SNDRV_CTL_ELEM_ACCESS_VOLATILE. As the second read is unreliable, the check makes little sense.

Also the controls with SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK (which can be checked via snd_ctl_skip_validation()) should be skipped, too.

thanks,

Takashi

maintainer
#3 lore raw
Quoted 16 lines · Takashi Iwai
Quoted 13 lines · Jaroslav Kysela

sound/core/control.c static int snd_ctl_elem_read_user(struct snd_card *card, · line 1301

Quoted 8 lines · Cezary Rojewski
sound/core/control.c +8 -0
+			sizeof(original.value.bytes.data));
+	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);

For volatile controls (SNDRV_CTL_ELEM_ACCESS_VOLATILE), the result may be fine. I would downgrade the level to pr_info for those controls here with another message.

Quoted 3 lines

I believe we can simply skip the validation for controls with SNDRV_CTL_ELEM_ACCESS_VOLATILE. As the second read is unreliable, the check makes little sense.

The current proposed code prints something for both success and failure cases. So we should decide if only problems should be notified or everything.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#4 lore raw
In reply to Jaroslav Kysela
Quoted 7 lines · Jaroslav Kysela
Quoted 3 lines · Cezary Rojewski

+    /* With known state for both new and original, do the comparison. */ +    retcmp = memcmp(&original.value.bytes.data[0], &control-

Quoted 1 line

value.bytes.data[0],

I think that shorter &original.value and &control->value pointer reference should be enough here. It's also more readable.

Agree, will do in v3, thank you!

Quoted 11 lines
Quoted 10 lines

+            sizeof(original.value.bytes.data));
+    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);

Quoted 1 line

For volatile controls (SNDRV_CTL_ELEM_ACCESS_VOLATILE), the result may be fine. I would downgrade the level to pr_info for those controls here with another message.

Does that mean that verification for controls of access VOLATILE may never be fully correct? If so, maybe let's leave the developer to make the final call, whether it's false-positive or not?

Another thing: Many info() implementations, if not all, ignore the assess assignment. If I'm not mistaken, info->access is left as default unless __snd_ctl_elem_info() gets called. Reusing it would translate put-verification into everything-verification.

Kind regards,
Czarek

#5 lore raw
Quoted 9 lines · Cezary Rojewski
Quoted 3 lines · Jaroslav Kysela

For volatile controls (SNDRV_CTL_ELEM_ACCESS_VOLATILE), the result may be fine. I would downgrade the level to pr_info for those controls here with another message.

Does that mean that verification for controls of access VOLATILE may never be fully correct? If so, maybe let's leave the developer to make the final call, whether it's false-positive or not?

Yes, volatile controls could change at any time so we can never guarantee that a read will match a write.