Skip to contentSkip to conversation

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

PATCH Superseded Cezary Rojewski 3 participants 6 messages Feb 23 – Feb 24 Sound

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

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

From user perspective, patch introduces a new trace/events category 'snd_ctl' containing a single 'snd_ctl_put' event type. Log sample:

  amixer-1107    [002] .....   9.553687: snd_ctl_put: success: expected=0, actual=0 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0
  amixer-1108    [003] .....   9.498897: snd_ctl_put: success: expected=1, actual=1 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0
  amixer-1111    [001] .....   9.882768: snd_ctl_put: success: expected=1, actual=1 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0
  amixer-1114    [002] .....   9.212184: snd_ctl_put: fail: expected=1, actual=0 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0

---

Changes in v6:
- the 'snd_ctl_put' event is now located in a new 'snd_ctl' trace/events category rather than being part of 'snd_pcm'
- provided an example of trace output for the new event within the commit message

Changes in v5:
- converged xxx_success() and xxx_fail() macros as suggested by Mark. The keyword "success" or "fail" is now denoted in the output of the trace event

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       | 76 +++++++++++++++++++++++++++++++++++++-
 sound/core/control_trace.h | 49 ++++++++++++++++++++++++
 sound/core/init.c          |  8 ++++
 5 files changed, 136 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 +75 -1

94 lines not on this page · raw message

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

49 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 18 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

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.

From user perspective, patch introduces a new trace/events category 'snd_ctl' containing a single 'snd_ctl_put' event type. Log sample:

Quoted 6 lines
  amixer-1107    [002] .....   9.553687: snd_ctl_put: success: expected=0, actual=0 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0
  amixer-1108    [003] .....   9.498897: snd_ctl_put: success: expected=1, actual=1 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0
  amixer-1111    [001] .....   9.882768: snd_ctl_put: success: expected=1, actual=1 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0
  amixer-1114    [002] .....   9.212184: snd_ctl_put: fail: expected=1, actual=0 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0

Looks almost good, just a minor nitpicking:

Quoted 2 lines
sound/core/init.c +0 -0

(snip)

sound/core/init.c void snd_card_disconnect(struct snd_card *card) · line 550

Quoted 10 lines
sound/core/init.c +3 -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;

IMO, it's better to be put in snd_card_do_free() which is the place to release the actual resources.

thanks,

Takashi

#2 lore raw
Quoted 2 lines · Takashi Iwai

On Mon, 23 Feb 2026 15:49:37 +0100,
Cezary Rojewski wrote:

...

Quoted 6 lines
Quoted 2 lines
sound/core/init.c +0 -0

(snip)

Quoted 3 lines
sound/core/init.c +0 -0
@@ -537,6 +542,9 @@ void snd_card_disconnect(struct snd_card *card)
  		synchronize_irq(card->sync_irq);
 
Quoted 10 lines

sound/core/init.c void snd_card_disconnect(struct snd_card *card) · line 550

Quoted 7 lines
sound/core/init.c +3 -0
  	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;

IMO, it's better to be put in snd_card_do_free() which is the place to release the actual resources.

No problem, a question though - I've written the above to be cohesive as CONFIG_SND_DEBUG-part isn't part of snd_card_do_free() and what I'm adding is definitely also of debug-related capability. IIRC debugfs_remove() may invoke kfree() operations. Why isn't it part of snd_card_do_free() too?

Kind regards,
Czarek

#3 lore raw
Quoted 23 lines · Cezary Rojewski
Quoted 2 lines · Takashi Iwai

On Mon, 23 Feb 2026 15:49:37 +0100,
Cezary Rojewski wrote:

...

Quoted 15 lines
Quoted 2 lines
sound/core/init.c +0 -0

(snip)

sound/core/init.c void snd_card_disconnect(struct snd_card *card) · line 549

Quoted 9 lines
sound/core/init.c +3 -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;

IMO, it's better to be put in snd_card_do_free() which is the place to release the actual resources.

Quoted 5 lines

No problem, a question though - I've written the above to be cohesive as CONFIG_SND_DEBUG-part isn't part of snd_card_do_free() and what I'm adding is definitely also of debug-related capability. IIRC debugfs_remove() may invoke kfree() operations. Why isn't it part of snd_card_do_free() too?

The *_disconnect() is a stage to stop the operation and disconnect the interfaces from user-space, so debugfs_remove() is invoked there. You can do kfree() if all things are detached, but in general we do the release in a later phase, as some objects may be still alive.

snd_card_do_free() is the actual destructor for the resources, where all things can be safely freed.

Takashi

maintainer
#4 lore raw
In reply to Cezary Rojewski
Quoted 14 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

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.

Quoted 7 lines
Quoted 1 line

From user perspective, patch introduces a new trace/events category

'snd_ctl' containing a single 'snd_ctl_put' event type. Log sample:

   amixer-1107    [002] .....   9.553687: snd_ctl_put: success: expected=0, actual=0 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0
   amixer-1108    [003] .....   9.498897: snd_ctl_put: success: expected=1, actual=1 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0
   amixer-1111    [001] .....   9.882768: snd_ctl_put: success: expected=1, actual=1 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0
   amixer-1114    [002] .....   9.212184: snd_ctl_put: fail: expected=1, actual=0 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0

Index (and maybe device/subdevice) numbers are missing to get full control identification to follow amixer. Also, "card id" is usually referred to automatically or manually assigned ASCII card identifier/alias (like USB-audio or PCH). I would just use 'card=<number>' in TP_printk.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#5 lore raw
Quoted 16 lines · Jaroslav Kysela
Quoted 15 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

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.

Quoted 1 line

From user perspective, patch introduces a new trace/events category

Quoted 8 lines
Quoted 6 lines

'snd_ctl' containing a single 'snd_ctl_put' event type. Log sample:

   amixer-1107    [002] .....   9.553687: snd_ctl_put: success: expected=0, actual=0 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0 amixer-1108    [003] .....   9.498897: snd_ctl_put: success: expected=1, actual=1 for ctl numid=1, iface=MIXER, name='Master Playback Volume', card id=0 amixer-1111    [001] .....   9.882768: snd_ctl_put: success: expected=1, actual=1 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0 amixer-1114    [002] .....   9.212184: snd_ctl_put: fail: expected=1, actual=0 for ctl numid=5, iface=MIXER, name='Loopback Mute', card id=0

Index (and maybe device/subdevice) numbers are missing to get full control identification to follow amixer. Also, "card id" is usually referred to automatically or manually assigned ASCII card identifier/ alias (like USB-audio or PCH). I would just use 'card=<number>' in TP_printk.

If the message length isn't a problem, then I'm going to add all of them in v7.