mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] firmware: cs_dsp: Remove some more wasted bytes per control
@ 2026-11-27 11:40 Richard Fitzgerald
  2026-11-27 11:40 ` [PATCH 1/2] firmware: cs_dsp: Resize members of cs_dsp_coeff_ctl to save bytes Richard Fitzgerald
  2026-11-27 11:40 ` [PATCH 2/2] firmware: cs_dsp: Store control cache data in struct cs_dsp_coeff_ctl Richard Fitzgerald
  0 siblings, 2 replies; 3+ messages in thread
From: Richard Fitzgerald @ 2026-11-27 11:40 UTC (permalink / raw)
  To: broonie; +Cc: linux-sound, linux-kernel, patches

This series removes another 16 wasted bytes (on x86_64) per control from
struct cs_dsp_coeff_ctl.

Richard Fitzgerald (2):
  firmware: cs_dsp: Resize members of cs_dsp_coeff_ctl to save bytes
  firmware: cs_dsp: Store control cache data in struct cs_dsp_coeff_ctl

 drivers/firmware/cirrus/cs_dsp.c              | 34 ++++++++-----------
 .../cirrus/test/cs_dsp_test_control_cache.c   |  5 +--
 include/linux/firmware/cirrus/cs_dsp.h        | 13 +++----
 3 files changed, 25 insertions(+), 27 deletions(-)

-- 
2.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] firmware: cs_dsp: Resize members of cs_dsp_coeff_ctl to save bytes
  2026-11-27 11:40 [PATCH 0/2] firmware: cs_dsp: Remove some more wasted bytes per control Richard Fitzgerald
@ 2026-11-27 11:40 ` Richard Fitzgerald
  2026-11-27 11:40 ` [PATCH 2/2] firmware: cs_dsp: Store control cache data in struct cs_dsp_coeff_ctl Richard Fitzgerald
  1 sibling, 0 replies; 3+ messages in thread
From: Richard Fitzgerald @ 2026-11-27 11:40 UTC (permalink / raw)
  To: broonie; +Cc: linux-sound, linux-kernel, patches

Reduce the size of some members in struct cs_dsp_coeff_ctl to the size of
the data they are storing. On an x86_64 build this saves 8 bytes per
control.

- The subname_len string can never be more than 16-bits. In V0 and V1
  firmwares it is a 256-character array, and in later firmwares it is an
  8-bit field. (In any case it is impractical to have a control name
  >65535 characters long.)

- Control type and flags are 16-bit values in the firmware.

- Type is relocated adjacent to subname_len to avoid introducing
  alignment padding bytes after subname_len

The ctl_type and flags fields in struct cs_dsp_coeff_parsed_coeff have
been changed to match these widths.

The arguments to cs_dsp_create_control() have been changed to the same
types so that consistent widths are used.

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
---
 drivers/firmware/cirrus/cs_dsp.c       | 15 +++++++++------
 include/linux/firmware/cirrus/cs_dsp.h |  8 ++++----
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/drivers/firmware/cirrus/cs_dsp.c b/drivers/firmware/cirrus/cs_dsp.c
index abf9196db883..b9e4b9283df1 100644
--- a/drivers/firmware/cirrus/cs_dsp.c
+++ b/drivers/firmware/cirrus/cs_dsp.c
@@ -1044,8 +1044,8 @@ static void cs_dsp_free_ctl_blk(struct cs_dsp_coeff_ctl *ctl)
 static int cs_dsp_create_control(struct cs_dsp *dsp,
 				 const struct cs_dsp_alg_region *alg_region,
 				 unsigned int offset, unsigned int len,
-				 const char *subname, unsigned int subname_len,
-				 unsigned int flags, unsigned int type)
+				 const char *subname, u16 subname_len,
+				 u16 flags, u16 type)
 {
 	struct cs_dsp_coeff_ctl *ctl;
 	int ret;
@@ -1162,9 +1162,9 @@ struct cs_dsp_coeff_parsed_coeff {
 	int offset;
 	int mem_type;
 	const u8 *name;
-	int name_len;
-	unsigned int ctl_type;
-	int flags;
+	u16 name_len;
+	u16 ctl_type;
+	u16 flags;
 	int len;
 };
 
@@ -1312,6 +1312,9 @@ static int cs_dsp_coeff_parse_coeff(struct cs_dsp *dsp,
 	blk->offset = le16_to_cpu(raw->hdr.offset);
 	blk->mem_type = le16_to_cpu(raw->hdr.type);
 
+	static_assert(sizeof(blk->ctl_type) >= sizeof(raw->ctl_type));
+	static_assert(sizeof(blk->flags) >= sizeof(raw->flags));
+
 	switch (dsp->wmfw_ver) {
 	case 0:
 	case 1:
@@ -1319,7 +1322,7 @@ static int cs_dsp_coeff_parse_coeff(struct cs_dsp *dsp,
 			return -EOVERFLOW;
 
 		blk->name = raw->name;
-		blk->name_len = strnlen(raw->name, ARRAY_SIZE(raw->name));
+		blk->name_len = (u16)strnlen(raw->name, ARRAY_SIZE(raw->name));
 		blk->ctl_type = le16_to_cpu(raw->ctl_type);
 		blk->flags = le16_to_cpu(raw->flags);
 		blk->len = le32_to_cpu(raw->len);
diff --git a/include/linux/firmware/cirrus/cs_dsp.h b/include/linux/firmware/cirrus/cs_dsp.h
index 6aa1e1b2b4a5..16542bdce5c4 100644
--- a/include/linux/firmware/cirrus/cs_dsp.h
+++ b/include/linux/firmware/cirrus/cs_dsp.h
@@ -84,9 +84,9 @@ struct cs_dsp_alg_region {
  * @fw_name:		Name of the firmware
  * @subname:		Name of the control parsed from the WMFW
  * @subname_len:	Length of subname
+ * @type:		One of the WMFW_CTL_TYPE_ control types defined in wmfw.h
  * @offset:		Offset of control within alg_region in words
  * @len:		Length of the cached value in bytes
- * @type:		One of the WMFW_CTL_TYPE_ control types defined in wmfw.h
  * @flags:		Bitfield of WMFW_CTL_FLAG_ control flags defined in wmfw.h
  * @set:		Flag indicating the value has been written by the user
  * @enabled:		Flag indicating whether control is enabled
@@ -100,11 +100,11 @@ struct cs_dsp_coeff_ctl {
 	const char *fw_name;
 	/* Subname is needed to match with firmware */
 	const char *subname;
-	unsigned int subname_len;
+	u16 subname_len;
+	u16 type;
 	unsigned int offset;
 	unsigned int len;
-	unsigned int type;
-	unsigned int flags;
+	u16 flags;
 	unsigned int set:1;
 	unsigned int enabled:1;
 	struct cs_dsp_alg_region alg_region;
-- 
2.47.3


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] firmware: cs_dsp: Store control cache data in struct cs_dsp_coeff_ctl
  2026-11-27 11:40 [PATCH 0/2] firmware: cs_dsp: Remove some more wasted bytes per control Richard Fitzgerald
  2026-11-27 11:40 ` [PATCH 1/2] firmware: cs_dsp: Resize members of cs_dsp_coeff_ctl to save bytes Richard Fitzgerald
@ 2026-11-27 11:40 ` Richard Fitzgerald
  1 sibling, 0 replies; 3+ messages in thread
From: Richard Fitzgerald @ 2026-11-27 11:40 UTC (permalink / raw)
  To: broonie; +Cc: linux-sound, linux-kernel, patches

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@opensource.cirrus.com>
---
 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-27 11:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 11:40 [PATCH 0/2] firmware: cs_dsp: Remove some more wasted bytes per control Richard Fitzgerald
2026-11-27 11:40 ` [PATCH 1/2] firmware: cs_dsp: Resize members of cs_dsp_coeff_ctl to save bytes Richard Fitzgerald
2026-11-27 11:40 ` [PATCH 2/2] firmware: cs_dsp: Store control cache data in struct cs_dsp_coeff_ctl Richard Fitzgerald

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®