ALSA: control: Verify put() result when in debug mode
PATCH v5 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 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
@@ -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
@@ -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
@@ -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
sound/core/control_trace.h line 3
Quoted 6 lines · Cezary Rojewski
sound/core/control_trace.h new file +3
-0@@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM snd_pcm
It doesn't look like a right place?
thanks,
Takashi
Quoted 11 lines · Takashi Iwai
sound/core/control_trace.h line 3
Quoted 6 lines · Cezary Rojewski
sound/core/control_trace.h new file +3
-0@@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM snd_pcmIt doesn't look like a right place?
Also, it'd be more helpful if you put example outputs in the description, too. Then one can have a clue what to look for.
Takashi
Quoted 14 lines · Takashi Iwai
Quoted 11 lines · Takashi Iwai
sound/core/control_trace.h line 3
Quoted 6 lines · Cezary Rojewski
sound/core/control_trace.h new file +3
-0@@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM snd_pcmIt doesn't look like a right place?
Quoted 2 lines
Also, it'd be more helpful if you put example outputs in the description, too. Then one can have a clue what to look for.
Agreed, an example in the commit message would be nice.
In regard to the "trace system", not a fan of adding a new "system" just for the sake of a single event. If we are going that route, I'd rather reword "snd_pcm" to "snd" or something similar. From sound-core perspective, I believe there should be 1 directory for the events declared in sound/core. The event-names are prefixed already, further division looks redundant.
Quoted 16 lines · Cezary Rojewski
Quoted 14 lines · Takashi Iwai
Quoted 11 lines · Takashi Iwai
sound/core/control_trace.h line 3
Quoted 6 lines · Cezary Rojewski
sound/core/control_trace.h new file +3
-0@@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM snd_pcmIt doesn't look like a right place?
Quoted 7 lines
Quoted 2 lines
Also, it'd be more helpful if you put example outputs in the description, too. Then one can have a clue what to look for.
Agreed, an example in the commit message would be nice.
In regard to the "trace system", not a fan of adding a new "system" just for the sake of a single event.
Why not? We may extend the tracing of controls, too, and it'll make easier to enable all tracepoints relevant either for PCM or control.
Quoted 5 lines
If we are going that route, I'd
rather reword "snd_pcm" to "snd" or something similar. From sound-core perspective, I believe there should be 1 directory for the events declared insound/core. The event-names are prefixed already, further division looks redundant.
That'll be messy under /sys/kernel/debug/tracing/events/*; it'll become snd/applptr, snd/hwptr, etc. So, if we want to consolidate into a single directory, we need to put "pcm_" prefix again to each.
Or maybe better to use subdirectories "snd/pcm" and "snd/ctl" instead.
thanks,
Takashi
Quoted 2 lines · Takashi Iwai
On Thu, 19 Feb 2026 11:15:41 +0100,
Cezary Rojewski wrote:
...
Quoted 12 lines
Quoted 2 lines
In regard to the "trace system", not a fan of adding a new "system" just for the sake of a single event.
Why not? We may extend the tracing of controls, too, and it'll make easier to enable all tracepoints relevant either for PCM or control.
Quoted 5 lines
If we are going that route, I'd
rather reword "snd_pcm" to "snd" or something similar. From sound-core perspective, I believe there should be 1 directory for the events declared insound/core. The event-names are prefixed already, further division looks redundant.
Quoted 5 lines
That'll be messy under
/sys/kernel/debug/tracing/events/*; it'll become snd/applptr, snd/hwptr, etc. So, if we want to consolidate into a single directory, we need to put "pcm_" prefix again to each.Or maybe better to use subdirectories "snd/pcm" and "snd/ctl" instead.
Well, I'm taking example from existing directories e.g.: trace/events/power or trace/events/sched. TBH, trace/event/asoc fits here too - all of them just group events together. sched_kthread_xxx are in the same bucket as sched_process_xxx (as are plenty of others). The 'name' alone is enough of a filter. I believe that until one reaches certain number of event-types, there is no reason to dilute the hierarchy.
However, looking at the subject from a high-level perspective, I think it isn't in the scope of this patch - I can align to any suggested pattern and we can work on the details in a dedicated series.
Kind regards,
Czakre
Jaroslav Kysela
maintainersound/core/control_trace.h line 25
Quoted 7 lines · Cezary Rojewski
sound/core/control_trace.h new file +7
-0+TP_STRUCT__entry ( +__field (unsigned int ,card_id ) + __field(unsigned int, numid) + __string(kname, kctl->id.name) +__string (iname, iname) +__field (int, expected) + __field(int, actual)
Trace also the control index. I know that ASoC ignores this field to allow simple string concat (easy name stacking), but we have drivers which are using those integer index numbers.
Thanks,
Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Quoted 10 lines · Jaroslav Kysela
Quoted 7 lines · Cezary Rojewski
+ TP_STRUCT__entry(
+__field(unsigned int, card_id)
+__field(unsigned int, numid)
+__string(kname, kctl->id.name)
+__string(iname, iname)
+__field(int, expected)
+__field(int, actual)Trace also the control index. I know that ASoC ignores this field to allow simple string concat (easy name stacking), but we have drivers which are using those integer index numbers.
amixer seems not to do so. Isn't ctl's numid sufficient? The reason I'm asking is that the message is quite long as is already.
Jaroslav Kysela
maintainerQuoted 10 lines · Cezary Rojewski
Quoted 9 lines · Jaroslav Kysela
Quoted 7 lines · Cezary Rojewski
+ TP_STRUCT__entry(
+__field(unsigned int, card_id)
+__field(unsigned int, numid)
+__string(kname, kctl->id.name)
+__string(iname, iname)
+__field(int, expected)
+__field(int, actual)
Quoted 6 lines
Quoted 3 lines
Trace also the control index. I know that ASoC ignores this field to allow simple string concat (easy name stacking), but we have drivers which are using those integer index numbers.
amixer seems not to do so. Isn't ctl's numid sufficient? The reason I'm asking is that the message is quite long as is already.
Amixer prints index and also device and subdevice numbers when they are not zero. See function snd_ctl_ascii_elem_id_get() in [1].
Jaroslav
[1] https://github.com/alsa-project/alsa-lib/blob/master/src/control/ctlparse.c#L117
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Quoted 15 lines · Jaroslav Kysela
Quoted 14 lines · Cezary Rojewski
Quoted 12 lines · Jaroslav Kysela
Quoted 7 lines · Cezary Rojewski
+ TP_STRUCT__entry(
+__field(unsigned int, card_id)
+__field(unsigned int, numid)
+__string(kname, kctl->id.name)
+__string(iname, iname)
+__field(int, expected)
+__field(int, actual)Trace also the control index. I know that ASoC ignores this field to allow simple string concat (easy name stacking), but we have drivers which are using those integer index numbers.
Quoted 8 lines
Quoted 2 lines
amixer seems not to do so. Isn't ctl's numid sufficient? The reason I'm asking is that the message is quite long as is already.
Amixer prints index and also device and subdevice numbers when they are not zero. See function
snd_ctl_ascii_elem_id_get()in [1].Jaroslav
[1] https://github.com/alsa-project/alsa-lib/blob/master/src/control/ ctlparse.c#L117
Thank you for the explanation. I've never seen such output when working with asoc-based drivers. Looking at the code now I know why.