[PATCH 2/2] firmware: cs_dsp: Store control cache data in struct cs_dsp_coeff_ctl

From: Richard Fitzgerald

Date: Fri Nov 27 2026 - 06:41:03 EST


Make the 'cache' member of struct cs_dsp_coeff_ctl a variable-length array
instead of a pointer to another block of memory. This needs a small update
to one KUnit test case to correctly check the expected length of the
allocated memory block.

The code originally allocated a block of memory for the data cache and
stored a pointer to that cache in struct cs_dsp_coeff_ctl. But most
controls are only 4 bytes, so this was wasteful on memory. On 64-bit
machines at least 12 bytes were used to store 4 bytes of data, and there
were extra wasted bytes from alignment and kmalloc block sizes. The data
can be stored directly in the struct cs_dsp_coeff_ctl in a variable-length
array.

On an x86_64 build:
Before:
total total
sizeof(*ctl) ksize(ctl) ctl->len ksize(ctl->cache) requested allocated
88 96 4 8 92 104

After:
80 96 4 84 96

Signed-off-by: Richard Fitzgerald <rf@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/firmware/cirrus/cs_dsp.c | 19 ++++++-------------
.../cirrus/test/cs_dsp_test_control_cache.c | 5 +++--
include/linux/firmware/cirrus/cs_dsp.h | 5 +++--
3 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/drivers/firmware/cirrus/cs_dsp.c b/drivers/firmware/cirrus/cs_dsp.c
index b9e4b9283df1..bb8e7c70ea49 100644
--- a/drivers/firmware/cirrus/cs_dsp.c
+++ b/drivers/firmware/cirrus/cs_dsp.c
@@ -19,6 +19,7 @@
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
+#include <linux/overflow.h>
#include <linux/ratelimit.h>
#include <linux/seq_file.h>
#include <linux/slab.h>
@@ -824,7 +825,7 @@ int cs_dsp_coeff_write_ctrl(struct cs_dsp_coeff_ctl *ctl,

if (ctl->flags & WMFW_CTL_FLAG_VOLATILE) {
ret = -EPERM;
- } else if (buf != ctl->cache) {
+ } else if (buf != &ctl->cache[0]) {
if (memcmp(ctl->cache + off * sizeof(u32), buf, len))
memcpy(ctl->cache + off * sizeof(u32), buf, len);
else
@@ -933,7 +934,7 @@ int cs_dsp_coeff_read_ctrl(struct cs_dsp_coeff_ctl *ctl,
if (!ctl->flags && ctl->enabled && ctl->dsp->running && !ctl->dsp->hibernating)
ret = cs_dsp_coeff_read_ctrl_raw(ctl, 0, ctl->cache, ctl->len);

- if (buf != ctl->cache)
+ if (buf != &ctl->cache[0])
memcpy(buf, ctl->cache + off * sizeof(u32), len);
}

@@ -1036,9 +1037,8 @@ static void cs_dsp_signal_event_controls(struct cs_dsp *dsp,

static void cs_dsp_free_ctl_blk(struct cs_dsp_coeff_ctl *ctl)
{
- kvfree(ctl->cache);
kfree(ctl->subname);
- kfree(ctl);
+ kvfree(ctl);
}

static int cs_dsp_create_control(struct cs_dsp *dsp,
@@ -1064,7 +1064,7 @@ static int cs_dsp_create_control(struct cs_dsp *dsp,
}
}

- ctl = kzalloc_obj(*ctl);
+ ctl = kvzalloc_flex(*ctl, cache, len);
if (!ctl)
return -ENOMEM;

@@ -1086,11 +1086,6 @@ static int cs_dsp_create_control(struct cs_dsp *dsp,
ctl->type = type;
ctl->offset = offset;
ctl->len = len;
- ctl->cache = kvzalloc(ctl->len, GFP_KERNEL);
- if (!ctl->cache) {
- ret = -ENOMEM;
- goto err_ctl_subname;
- }

list_add(&ctl->list, &dsp->ctl_list);

@@ -1104,11 +1099,9 @@ static int cs_dsp_create_control(struct cs_dsp *dsp,

err_list_del:
list_del(&ctl->list);
- kvfree(ctl->cache);
-err_ctl_subname:
kfree(ctl->subname);
err_ctl:
- kfree(ctl);
+ kvfree(ctl);

return ret;
}
diff --git a/drivers/firmware/cirrus/test/cs_dsp_test_control_cache.c b/drivers/firmware/cirrus/test/cs_dsp_test_control_cache.c
index b03cd7c37f6e..7a9f939fcf6f 100644
--- a/drivers/firmware/cirrus/test/cs_dsp_test_control_cache.c
+++ b/drivers/firmware/cirrus/test/cs_dsp_test_control_cache.c
@@ -13,6 +13,7 @@
#include <linux/firmware/cirrus/cs_dsp_test_utils.h>
#include <linux/firmware/cirrus/wmfw.h>
#include <linux/list.h>
+#include <linux/overflow.h>
#include <linux/random.h>
#include <linux/regmap.h>

@@ -186,9 +187,9 @@ static void cs_dsp_ctl_v2_cache_alloc(struct kunit *test)

KUNIT_EXPECT_EQ(test, list_count_nodes(&dsp->ctl_list), num_ctls);

- /* Check that the block allocated for the cache is large enough */
+ /* Check that the allocation holds the control and its cache */
list_for_each_entry(ctl, &dsp->ctl_list, list)
- KUNIT_EXPECT_GE(test, ksize(ctl->cache), ctl->len);
+ KUNIT_EXPECT_GE(test, ksize(ctl), struct_size(ctl, cache, ctl->len));
}

/*
diff --git a/include/linux/firmware/cirrus/cs_dsp.h b/include/linux/firmware/cirrus/cs_dsp.h
index 16542bdce5c4..b2492c55660c 100644
--- a/include/linux/firmware/cirrus/cs_dsp.h
+++ b/include/linux/firmware/cirrus/cs_dsp.h
@@ -80,7 +80,6 @@ struct cs_dsp_alg_region {
* struct cs_dsp_coeff_ctl - Describes a coefficient control
* @list: List node for internal use
* @dsp: DSP instance associated with this control
- * @cache: Cached value of the control
* @fw_name: Name of the firmware
* @subname: Name of the control parsed from the WMFW
* @subname_len: Length of subname
@@ -92,11 +91,11 @@ struct cs_dsp_alg_region {
* @enabled: Flag indicating whether control is enabled
* @alg_region: Logical region associated with this control
* @priv: For use by the client
+ * @cache: Cached value of the control
*/
struct cs_dsp_coeff_ctl {
struct list_head list;
struct cs_dsp *dsp;
- void *cache __counted_by_ptr(len);
const char *fw_name;
/* Subname is needed to match with firmware */
const char *subname;
@@ -110,6 +109,8 @@ struct cs_dsp_coeff_ctl {
struct cs_dsp_alg_region alg_region;

void *priv;
+
+ u8 cache[] __counted_by(len);
};

struct cs_dsp_ops;
--
2.47.3