Skip to content

Commit 8444653

Browse files
crojewsk-inteltiwai
authored andcommitted
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-1086 [003] ..... 8.035939: snd_ctl_put: success: expected=0, actual=0 for ctl numid=1, iface=MIXER, name='Master Playback Volume', index=0, device=0, subdevice=0, card=0 amixer-1087 [003] ..... 8.938721: snd_ctl_put: success: expected=1, actual=1 for ctl numid=1, iface=MIXER, name='Master Playback Volume', index=0, device=0, subdevice=0, card=0 amixer-1088 [003] ..... 9.631470: snd_ctl_put: success: expected=1, actual=1 for ctl numid=1, iface=MIXER, name='Master Playback Volume', index=0, device=0, subdevice=0, card=0 amixer-1089 [000] ..... 9.636786: snd_ctl_put: fail: expected=1, actual=0 for ctl numid=5, iface=MIXER, name='Loopback Mute', index=0, device=0, subdevice=0, card=0 Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com> Reviewed-by: Mark Brown <broonie@kernel.org> Reviewed-by: Jaroslav Kysela <perex@perex.cz> Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://patch.msgid.link/20260224205619.584795-1-cezary.rojewski@intel.com
1 parent d4d5633 commit 8444653

5 files changed

Lines changed: 142 additions & 1 deletion

File tree

include/sound/core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ struct snd_card {
133133
#ifdef CONFIG_SND_DEBUG
134134
struct dentry *debugfs_root; /* debugfs root for card */
135135
#endif
136+
#ifdef CONFIG_SND_CTL_DEBUG
137+
struct snd_ctl_elem_value *value_buf; /* buffer for kctl->put() verification */
138+
#endif
136139

137140
#ifdef CONFIG_PM
138141
unsigned int power_state; /* power state */

sound/core/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ snd-pcm-$(CONFIG_SND_PCM_IEC958) += pcm_iec958.o
2323
# for trace-points
2424
CFLAGS_pcm_lib.o := -I$(src)
2525
CFLAGS_pcm_native.o := -I$(src)
26+
CFLAGS_control.o := -I$(src)
2627

2728
snd-pcm-dmaengine-y := pcm_dmaengine.o
2829

sound/core/control.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
#include <sound/info.h>
2020
#include <sound/control.h>
2121

22+
#ifdef CONFIG_SND_CTL_DEBUG
23+
#define CREATE_TRACE_POINTS
24+
#include "control_trace.h"
25+
#else
26+
#define trace_snd_ctl_put(card, kctl, iname, expected, actual)
27+
#endif
28+
2229
// Max allocation size for user controls.
2330
static int max_user_ctl_alloc_size = 8 * 1024 * 1024;
2431
module_param_named(max_user_ctl_alloc_size, max_user_ctl_alloc_size, int, 0444);
@@ -1264,6 +1271,72 @@ static int snd_ctl_elem_read_user(struct snd_card *card,
12641271
return result;
12651272
}
12661273

1274+
#if IS_ENABLED(CONFIG_SND_CTL_DEBUG)
1275+
1276+
static const char *const snd_ctl_elem_iface_names[] = {
1277+
[SNDRV_CTL_ELEM_IFACE_CARD] = "CARD",
1278+
[SNDRV_CTL_ELEM_IFACE_HWDEP] = "HWDEP",
1279+
[SNDRV_CTL_ELEM_IFACE_MIXER] = "MIXER",
1280+
[SNDRV_CTL_ELEM_IFACE_PCM] = "PCM",
1281+
[SNDRV_CTL_ELEM_IFACE_RAWMIDI] = "RAWMIDI",
1282+
[SNDRV_CTL_ELEM_IFACE_TIMER] = "TIMER",
1283+
[SNDRV_CTL_ELEM_IFACE_SEQUENCER] = "SEQUENCER",
1284+
};
1285+
1286+
static int snd_ctl_put_verify(struct snd_card *card, struct snd_kcontrol *kctl,
1287+
struct snd_ctl_elem_value *control)
1288+
{
1289+
struct snd_ctl_elem_value *original = card->value_buf;
1290+
struct snd_ctl_elem_info info;
1291+
const char *iname;
1292+
int ret, retcmp;
1293+
1294+
memset(original, 0, sizeof(*original));
1295+
memset(&info, 0, sizeof(info));
1296+
1297+
ret = kctl->info(kctl, &info);
1298+
if (ret)
1299+
return ret;
1300+
1301+
ret = kctl->get(kctl, original);
1302+
if (ret)
1303+
return ret;
1304+
1305+
ret = kctl->put(kctl, control);
1306+
if (ret < 0)
1307+
return ret;
1308+
1309+
/* Sanitize the new value (control->value) before comparing. */
1310+
fill_remaining_elem_value(control, &info, 0);
1311+
1312+
/* With known state for both new and original, do the comparison. */
1313+
retcmp = memcmp(&original->value, &control->value, sizeof(original->value));
1314+
if (retcmp)
1315+
retcmp = 1;
1316+
1317+
iname = snd_ctl_elem_iface_names[kctl->id.iface];
1318+
trace_snd_ctl_put(&kctl->id, iname, card->number, ret, retcmp);
1319+
1320+
return ret;
1321+
}
1322+
1323+
static int snd_ctl_put(struct snd_card *card, struct snd_kcontrol *kctl,
1324+
struct snd_ctl_elem_value *control, unsigned int access)
1325+
{
1326+
if ((access & SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK) ||
1327+
(access & SNDRV_CTL_ELEM_ACCESS_VOLATILE))
1328+
return kctl->put(kctl, control);
1329+
1330+
return snd_ctl_put_verify(card, kctl, control);
1331+
}
1332+
#else
1333+
static inline int snd_ctl_put(struct snd_card *card, struct snd_kcontrol *kctl,
1334+
struct snd_ctl_elem_value *control, unsigned int access)
1335+
{
1336+
return kctl->put(kctl, control);
1337+
}
1338+
#endif
1339+
12671340
static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
12681341
struct snd_ctl_elem_value *control)
12691342
{
@@ -1300,7 +1373,8 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
13001373
false);
13011374
}
13021375
if (!result)
1303-
result = kctl->put(kctl, control);
1376+
result = snd_ctl_put(card, kctl, control, vd->access);
1377+
13041378
if (result < 0) {
13051379
up_write(&card->controls_rwsem);
13061380
return result;

sound/core/control_trace.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#undef TRACE_SYSTEM
3+
#define TRACE_SYSTEM snd_ctl
4+
5+
#if !defined(_TRACE_SND_CTL_H) || defined(TRACE_HEADER_MULTI_READ)
6+
#define _TRACE_SND_CTL_H
7+
8+
#include <linux/tracepoint.h>
9+
#include <uapi/sound/asound.h>
10+
11+
TRACE_EVENT(snd_ctl_put,
12+
13+
TP_PROTO(struct snd_ctl_elem_id *id, const char *iname, unsigned int card,
14+
int expected, int actual),
15+
16+
TP_ARGS(id, iname, card, expected, actual),
17+
18+
TP_STRUCT__entry(
19+
__field(unsigned int, numid)
20+
__string(iname, iname)
21+
__string(kname, id->name)
22+
__field(unsigned int, index)
23+
__field(unsigned int, device)
24+
__field(unsigned int, subdevice)
25+
__field(unsigned int, card)
26+
__field(int, expected)
27+
__field(int, actual)
28+
),
29+
30+
TP_fast_assign(
31+
__entry->numid = id->numid;
32+
__assign_str(iname);
33+
__assign_str(kname);
34+
__entry->index = id->index;
35+
__entry->device = id->device;
36+
__entry->subdevice = id->subdevice;
37+
__entry->card = card;
38+
__entry->expected = expected;
39+
__entry->actual = actual;
40+
),
41+
42+
TP_printk("%s: expected=%d, actual=%d for ctl numid=%d, iface=%s, name='%s', index=%d, device=%d, subdevice=%d, card=%d\n",
43+
__entry->expected == __entry->actual ? "success" : "fail",
44+
__entry->expected, __entry->actual, __entry->numid,
45+
__get_str(iname), __get_str(kname), __entry->index,
46+
__entry->device, __entry->subdevice, __entry->card)
47+
);
48+
49+
#endif /* _TRACE_SND_CTL_H */
50+
51+
/* This part must be outside protection */
52+
#undef TRACE_INCLUDE_PATH
53+
#define TRACE_INCLUDE_PATH .
54+
#define TRACE_INCLUDE_FILE control_trace
55+
#include <trace/define_trace.h>

sound/core/init.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,11 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
362362
#ifdef CONFIG_SND_DEBUG
363363
card->debugfs_root = debugfs_create_dir(dev_name(&card->card_dev),
364364
sound_debugfs_root);
365+
#endif
366+
#ifdef CONFIG_SND_CTL_DEBUG
367+
card->value_buf = kmalloc(sizeof(*card->value_buf), GFP_KERNEL);
368+
if (!card->value_buf)
369+
return -ENOMEM;
365370
#endif
366371
return 0;
367372

@@ -587,6 +592,9 @@ static int snd_card_do_free(struct snd_card *card)
587592
snd_device_free_all(card);
588593
if (card->private_free)
589594
card->private_free(card);
595+
#ifdef CONFIG_SND_CTL_DEBUG
596+
kfree(card->value_buf);
597+
#endif
590598
if (snd_info_card_free(card) < 0) {
591599
dev_warn(card->dev, "unable to free card info\n");
592600
/* Not fatal error */

0 commit comments

Comments
 (0)