firmware: cs_dsp: Store control length as 32-bit
Richard Fitzgerald
maintainerPATCH 1/1 firmware: cs_dsp: Store control length as 32-bit
The architectures supported by this driver have a maximum of 32-bits of address, so we don't need more than 32-bits to store the length of control data. Change the length in struct cs_dsp_coeff_ctl to an unsigned int instead of a size_t. Also make a corresponding trivial change to wm_adsp.c to prevent a compiler warning.
Tested on x86_64 builds this saves at least 4 bytes per control (another 4 bytes might be saved if the compiler was inserting padding to align the size_t).
--- drivers/firmware/cirrus/cs_dsp.c | 2 +- include/linux/firmware/cirrus/cs_dsp.h | 2 +- sound/soc/codecs/wm_adsp.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/cirrus/cs_dsp.c +1 -1
@@ -477,7 +477,7 @@ static int cs_dsp_debugfs_read_controls_show(struct seq_file *s, void *ignored)list_for_each_entry (ctl, &dsp->ctl_list, list) {cs_dsp_coeff_base_reg (ctl, ®,0 ); - seq_printf(s, "%22.*s: %#8zx %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n", + seq_printf(s, "%22.*s: %#8x %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n", ctl->subname_len, ctl->subname, ctl->len, cs_dsp_mem_region_name(ctl->alg_region.type), ctl->offset, reg, ctl->fw_name, ctl->alg_region.alg, ctl->type,
include/linux/firmware/cirrus/cs_dsp.h +1 -1
@@ -102,7 +102,7 @@ struct cs_dsp_coeff_ctl {const char *subname;unsigned int subname_len;unsigned int offset; -size_t len; +unsigned int len;unsigned int type;unsigned int flags; unsigned int set:1;
sound/soc/codecs/wm_adsp.c +1 -1
@@ -1561,7 +1561,7 @@ static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl)for (i =0 ; i <5 ; ++i) { ret =cs_dsp_coeff_read_ctrl (cs_ctl,0 , &coeff_v1, -min (cs_ctl->len,sizeof (coeff_v1))); +min ((size_t )cs_ctl->len,sizeof (coeff_v1)));if (ret <0 )return ret;
Signature · 1 line
2.47.3
Mark Brown
maintainerQuoted 11 lines · Richard Fitzgerald
The architectures supported by this driver have a maximum of 32-bits of address, so we don't need more than 32-bits to store the length of control data. Change the length in struct
cs_dsp_coeff_ctlto an unsigned int instead of asize_t. Also make a corresponding trivial change towm_adsp.cto prevent a compiler warning.Tested on
x86_64builds this saves at least 4 bytes per control (another 4 bytes might be saved if the compiler was inserting padding to align thesize_t).[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next
Thanks!
[1/1] firmware: cs_dsp: Store control length as 32-bit
commit: 7584edf15892e29190b2145294cc1680aa142586
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark
Quoted 48 lines · On Mon, 24 Nov 2025 17:15:35 +0000 Richard Fitzgerald…
The architectures supported by this driver have a maximum of 32-bits of address, so we don't need more than 32-bits to store the length of control data. Change the length in struct
cs_dsp_coeff_ctlto an unsigned int instead of asize_t. Also make a corresponding trivial change towm_adsp.cto prevent a compiler warning.Tested on
x86_64builds this saves at least 4 bytes per control (another 4 bytes might be saved if the compiler was inserting padding to align thesize_t).Signed-off-by Richard Fitzgerald <[email protected]>--- drivers/firmware/cirrus/cs_dsp.c | 2 +- include/linux/firmware/cirrus/cs_dsp.h | 2 +- sound/soc/codecs/wm_adsp.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)drivers/firmware/cirrus/cs_dsp.c +1
-1@@ -477,7 +477,7 @@ static int cs_dsp_debugfs_read_controls_show(struct seq_file *s, void *ignored)list_for_each_entry (ctl, &dsp->ctl_list, list) {cs_dsp_coeff_base_reg (ctl, ®,0 ); - seq_printf(s, "%22.*s: %#8zx %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n", + seq_printf(s, "%22.*s: %#8x %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n", ctl->subname_len, ctl->subname, ctl->len, cs_dsp_mem_region_name(ctl->alg_region.type), ctl->offset, reg, ctl->fw_name, ctl->alg_region.alg, ctl->type,include/linux/firmware/cirrus/cs_dsp.h +1
-1@@ -102,7 +102,7 @@ struct cs_dsp_coeff_ctl {const char *subname;unsigned int subname_len;unsigned int offset; -size_t len; +unsigned int len;unsigned int type;unsigned int flags; unsigned int set:1;sound/soc/codecs/wm_adsp.c +0
-0
sound/soc/codecs/wm_adsp.c static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl) · line 1564
Quoted 5 lines
sound/soc/codecs/wm_adsp.c +1
-1@@ -1561,7 +1561,7 @@ static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl)for (i =0 ; i <5 ; ++i) { ret =cs_dsp_coeff_read_ctrl (cs_ctl,0 , &coeff_v1, -min (cs_ctl->len,sizeof (coeff_v1))); +min ((size_t )cs_ctl->len,sizeof (coeff_v1)));
You don't (shouldn't) need that cast.min() won't object because both arguments are unsigned types. The compiler will then 'promote' cs_ctl->len for the compares, but might use the 'as if' rule and do 32bit maths anyway.
Given that the called function starts:
int cs_dsp_coeff_read_ctrl(struct cs_dsp_coeff_ctl *ctl,
unsigned int off, void *buf, size_t len)
{
...
if (len + off * sizeof(u32) > ctl->len) return -EINVAL;
Maybe you should be changing that 'len' to u32 as well?
David
Quoted 3 lines
sound/soc/codecs/wm_adsp.c +0
-0if (ret <0 )return ret;
Richard Fitzgerald
maintainerQuoted 52 lines · david laight
Quoted 49 lines · On Mon, 24 Nov 2025 17:15:35 +0000 Richard Fitzgerald…
The architectures supported by this driver have a maximum of 32-bits of address, so we don't need more than 32-bits to store the length of control data. Change the length in struct
cs_dsp_coeff_ctlto an unsigned int instead of asize_t. Also make a corresponding trivial change towm_adsp.cto prevent a compiler warning.Tested on
x86_64builds this saves at least 4 bytes per control (another 4 bytes might be saved if the compiler was inserting padding to align thesize_t).Signed-off-by Richard Fitzgerald <[email protected]>--- drivers/firmware/cirrus/cs_dsp.c | 2 +- include/linux/firmware/cirrus/cs_dsp.h | 2 +- sound/soc/codecs/wm_adsp.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)drivers/firmware/cirrus/cs_dsp.c +1
-1@@ -477,7 +477,7 @@ static int cs_dsp_debugfs_read_controls_show(struct seq_file *s, void *ignored)list_for_each_entry (ctl, &dsp->ctl_list, list) {cs_dsp_coeff_base_reg (ctl, ®,0 ); - seq_printf(s, "%22.*s: %#8zx %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n", + seq_printf(s, "%22.*s: %#8x %s:%08x %#8x %s %#8x %#4x %c%c%c%c %s %s\n", ctl->subname_len, ctl->subname, ctl->len, cs_dsp_mem_region_name(ctl->alg_region.type), ctl->offset, reg, ctl->fw_name, ctl->alg_region.alg, ctl->type,include/linux/firmware/cirrus/cs_dsp.h +1
-1@@ -102,7 +102,7 @@ struct cs_dsp_coeff_ctl {const char *subname;unsigned int subname_len;unsigned int offset; -size_t len; +unsigned int len;unsigned int type;unsigned int flags; unsigned int set:1;sound/soc/codecs/wm_adsp.c +0
-0@@ -1561,7 +1561,7 @@ static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl)
Quoted 7 lines
sound/soc/codecs/wm_adsp.c static int wm_adsp_buffer_parse_coeff(struct cs_dsp_coeff_ctl *cs_ctl) · line 1564
Quoted 4 lines
sound/soc/codecs/wm_adsp.c +1
-1for (i =0 ; i <5 ; ++i) { ret =cs_dsp_coeff_read_ctrl (cs_ctl,0 , &coeff_v1, -min (cs_ctl->len,sizeof (coeff_v1))); +min ((size_t )cs_ctl->len,sizeof (coeff_v1)));You don't (shouldn't) need that cast.
min()won't object because both arguments are unsigned types.
When this code was written one of gcc/clang/sparse didn't agree with you.
I built for multiple architectures so maybe some architecture version of the compiler complains.
Quoted 8 lines
The compiler will then 'promote'
cs_ctl->lenfor the compares, but might use the 'as if' rule and do 32bit maths anyway.Given that the called function starts:
int cs_dsp_coeff_read_ctrl(structcs_dsp_coeff_ctl*ctl,
unsigned int off, void *buf,size_tlen)
{
...
Quoted 5 lines
if (len + off * sizeof(u32) > ctl->len) return -EINVAL;Maybe you should be changing that 'len' to u32 as well?
Why? What's wrong with taking a size_t for length arguments?