Skip to contentSkip to conversation

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

PATCH Superseded Cezary Rojewski 3 participants 7 messages Feb 10 – Feb 19 Sound

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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.

Due to large size of struct snd_ctl_elem_value, 'value_buf' is provided as a reusable buffer for kctl->put() verification. This prevents exhausting the stack when verifying the operation.

---

Changes in v4:

- moved away from pr_xxx() in favor of trace events
- refactired the print format so that it aligns closer to 'amixer controls' output
- added 'value_buf' to 'struct snd_card' as suggested - to prevent stack overflow in snd_ctl_put_verify()

Changes in v3:

- simplified the memcmp() as suggested by Jaroslav
- added ->access verification for SKIP_CHECK and VOLATILE as suggested by Jaroslav and Takashi. For that very purpose I've deviced to use the kcontrol-volatile (vd) pointer that already part of snd_ctl_elem_write().

- as things are more complex now, replaced snd_ctl_put() macros with functions. This aligns with suggestion previously provided by Kuninori

- reordered operations within snd_ctl_put_verify() so that it's more intuitive to read, at least in my opinion:
get/info/put -> info/get/put

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.

 include/sound/core.h       |  3 ++
 sound/core/Makefile        |  1 +
 sound/core/control.c       | 78 +++++++++++++++++++++++++++++++++++++-
 sound/core/control_trace.h | 60 +++++++++++++++++++++++++++++
 sound/core/init.c          |  8 ++++
 5 files changed, 149 insertions(+), 1 deletion(-)
 create mode 100644 sound/core/control_trace.h
include/sound/core.h +3 -0
index 64327e971122..4093ec82a0a1 100644
@@ -133,6 +133,9 @@ struct snd_card {
 #ifdef CONFIG_SND_DEBUG
 	struct dentry *debugfs_root;    /* debugfs root for card */
 #endif
+#ifdef CONFIG_SND_CTL_DEBUG
+	struct snd_ctl_elem_value *value_buf; /* buffer for kctl->put() verification */
+#endif
 
 #ifdef CONFIG_PM
 	unsigned int power_state;	/* power state */
sound/core/Makefile +1 -0
index 31a0623cc89d..fdd3bb6e81a9 100644
@@ -23,6 +23,7 @@ snd-pcm-$(CONFIG_SND_PCM_IEC958) += pcm_iec958.o
 # for trace-points
 CFLAGS_pcm_lib.o := -I$(src)
 CFLAGS_pcm_native.o := -I$(src)
+CFLAGS_control.o := -I$(src)
 
 snd-pcm-dmaengine-y := pcm_dmaengine.o
 
sound/core/control.c +77 -1

96 lines not on this page · raw message

sound/core/control_trace.h new file +60 -0

60 lines not on this page · raw message

sound/core/init.c +8 -0
index c372b3228785..15868dd50f6a 100644
@@ -362,6 +362,11 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
 #ifdef CONFIG_SND_DEBUG
 	card->debugfs_root = debugfs_create_dir(dev_name(&card->card_dev),
 						sound_debugfs_root);
+#endif
+#ifdef CONFIG_SND_CTL_DEBUG
+	card->value_buf = kmalloc(sizeof(*card->value_buf), GFP_KERNEL);
+	if (!card->value_buf)
+		return -ENOMEM;
 #endif
 	return 0;
 
@@ -537,6 +542,9 @@ void snd_card_disconnect(struct snd_card *card)
 		synchronize_irq(card->sync_irq);
 
 	snd_info_card_disconnect(card);
+#ifdef CONFIG_SND_CTL_DEBUG
+	kfree(card->value_buf);
+#endif
 #ifdef CONFIG_SND_DEBUG
 	debugfs_remove(card->debugfs_root);
 	card->debugfs_root = NULL;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 4 lines · 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

...

Only just now I've realized that there is a typo in the title - should have been "PATCH v4" instead. Let me know if I should resend this one.

Kind regards,
Czarek

#2 lore raw
Quoted 10 lines · Cezary Rojewski
Quoted 4 lines · 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

...

Only just now I've realized that there is a typo in the title - should have been "PATCH v4" instead. Let me know if I should resend this one.

It doesn't matter :)

But beware that I'm not going to take this for 7.0, as it's no urgent bug fix. So we have enough time for more reviews.

thanks,

Takashi

#3 lore raw
In reply to Cezary Rojewski

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

Quoted 8 lines · Cezary Rojewski
sound/core/control.c +8 -0
+	/* With known state for both new and original, do the comparison. */
+	retcmp = memcmp(&original->value, &control->value, sizeof(original->value));
+	if (retcmp)
+		retcmp = 1;
+
+	iname = snd_ctl_elem_iface_names[kctl->id.iface];
+	trace_snd_ctl_put_success(card, kctl, iname, ret, retcmp);
+	trace_snd_ctl_put_fail(card, kctl, iname, ret, retcmp);

It feels weird that we have two events here - given the result of one you know the value of the other. It seems like it'd be more idiomatic to just log the result of the comparison, the output is structured so it's easy to parse.

#4 lore raw
Quoted 15 lines · Mark Brown

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

Quoted 8 lines · Cezary Rojewski
sound/core/control.c +8 -0
+	/* With known state for both new and original, do the comparison. */
+	retcmp = memcmp(&original->value, &control->value, sizeof(original->value));
+	if (retcmp)
+		retcmp = 1;
+
+	iname = snd_ctl_elem_iface_names[kctl->id.iface];
+	trace_snd_ctl_put_success(card, kctl, iname, ret, retcmp);
+	trace_snd_ctl_put_fail(card, kctl, iname, ret, retcmp);

It feels weird that we have two events here - given the result of one you know the value of the other. It seems like it'd be more idiomatic to just log the result of the comparison, the output is structured so it's easy to parse.

I don't mind either form. Typically, I filter just for the fails (here: trace_snd_ctl_put_fail) but greping for the 'fail' in the output is fine too.

#5 lore raw
Quoted 10 lines · Cezary Rojewski
Quoted 7 lines · Mark Brown

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

Quoted 2 lines
sound/core/control.c +2 -0
+	trace_snd_ctl_put_success(card, kctl, iname, ret, retcmp);
+	trace_snd_ctl_put_fail(card, kctl, iname, ret, retcmp);

It feels weird that we have two events here - given the result of one you know the value of the other. It seems like it'd be more idiomatic to just log the result of the comparison, the output is structured so it's easy to parse.

Quoted 3 lines

I don't mind either form. Typically, I filter just for the fails (here:
trace_snd_ctl_put_fail) but greping for the 'fail' in the output is fine too.

I'm not super bothered either way - it is a conditional event so only one will actually be generated for a given put but it looks odd in the code. Possibly worth a comment?

#6 lore raw
Quoted 11 lines · Mark Brown
Quoted 9 lines · Cezary Rojewski
Quoted 7 lines · Mark Brown

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

Quoted 2 lines
sound/core/control.c +2 -0
+	trace_snd_ctl_put_success(card, kctl, iname, ret, retcmp);
+	trace_snd_ctl_put_fail(card, kctl, iname, ret, retcmp);

It feels weird that we have two events here - given the result of one you know the value of the other. It seems like it'd be more idiomatic to just log the result of the comparison, the output is structured so it's easy to parse.

Quoted 7 lines
Quoted 3 lines

I don't mind either form. Typically, I filter just for the fails (here:
trace_snd_ctl_put_fail) but greping for the 'fail' in the output is fine too.

I'm not super bothered either way - it is a conditional event so only one will actually be generated for a given put but it looks odd in the code. Possibly worth a comment?

git diff shows +5/-18 after your suggestion, so now I mind the form :) Shipping it with v5!