[PATCH v2] ALSA: core: Use flexible array for card private data

Cássio Gabriel posted 1 patch 1 week ago
include/sound/core.h | 2 ++
sound/core/init.c    | 7 ++++---
2 files changed, 6 insertions(+), 3 deletions(-)
[PATCH v2] ALSA: core: Use flexible array for card private data
Posted by Cássio Gabriel 1 week ago
snd_card_new() and snd_devm_card_new() allocate struct snd_card
together with optional driver-private storage. The storage is currently
described only by open-coded sizeof(*card) + extra_size arithmetic, and
snd_card_init() reaches it by manually adding sizeof(struct snd_card) to
the card pointer.

Make the trailing storage explicit with a flexible array member. Use
kzalloc_flex() for the regular allocation path and struct_size() for the
devres allocation size. This documents the layout and avoids open-coded
variable-size object arithmetic.

Align the flexible array to unsigned long long so the driver-private area
does not become less aligned than the old sizeof(struct snd_card) tail
address on 32-bit ABIs.

Signed-off-by: Cássio Gabriel <cassiogabrielcontato@gmail.com>
---
Changes in v2:
- Use kzalloc_flex() for the snd_card_new() allocation path.
- Link to v1: https://patch.msgid.link/20260529-alsa-card-private-flex-array-v1-1-370eb58e251a@gmail.com
---
 include/sound/core.h | 2 ++
 sound/core/init.c    | 7 ++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/include/sound/core.h b/include/sound/core.h
index 4093ec82a0a1..4bb76c21c956 100644
--- a/include/sound/core.h
+++ b/include/sound/core.h
@@ -148,6 +148,8 @@ struct snd_card {
 	struct snd_mixer_oss *mixer_oss;
 	int mixer_oss_change_count;
 #endif
+
+	unsigned char private_data_area[] __aligned(__alignof__(unsigned long long));
 };
 
 #define dev_to_snd_card(p)	container_of(p, struct snd_card, card_dev)
diff --git a/sound/core/init.c b/sound/core/init.c
index 0c316189e947..2408160b8ea1 100644
--- a/sound/core/init.c
+++ b/sound/core/init.c
@@ -181,7 +181,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
 
 	if (extra_size < 0)
 		extra_size = 0;
-	card = kzalloc(sizeof(*card) + extra_size, GFP_KERNEL);
+	card = kzalloc_flex(*card, private_data_area, extra_size);
 	if (!card)
 		return -ENOMEM;
 
@@ -232,7 +232,8 @@ int snd_devm_card_new(struct device *parent, int idx, const char *xid,
 	int err;
 
 	*card_ret = NULL;
-	card = devres_alloc(__snd_card_release, sizeof(*card) + extra_size,
+	card = devres_alloc(__snd_card_release,
+			    struct_size(card, private_data_area, extra_size),
 			    GFP_KERNEL);
 	if (!card)
 		return -ENOMEM;
@@ -280,7 +281,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent,
 	int err;
 
 	if (extra_size > 0)
-		card->private_data = (char *)card + sizeof(struct snd_card);
+		card->private_data = card->private_data_area;
 	if (xid)
 		strscpy(card->id, xid, sizeof(card->id));
 	err = 0;

---
base-commit: 5fa82dd6492e9ac3ab27d2c15d73b601b4e6a87d
change-id: 20260531-alsa-card-private-flex-array-278c5b3091ac

Best regards,
--  
Cássio Gabriel <cassiogabrielcontato@gmail.com>

Re: [PATCH v2] ALSA: core: Use flexible array for card private data
Posted by Takashi Iwai 6 days, 21 hours ago
On Mon, 01 Jun 2026 03:23:35 +0200,
Cássio Gabriel wrote:
> 
> snd_card_new() and snd_devm_card_new() allocate struct snd_card
> together with optional driver-private storage. The storage is currently
> described only by open-coded sizeof(*card) + extra_size arithmetic, and
> snd_card_init() reaches it by manually adding sizeof(struct snd_card) to
> the card pointer.
> 
> Make the trailing storage explicit with a flexible array member. Use
> kzalloc_flex() for the regular allocation path and struct_size() for the
> devres allocation size. This documents the layout and avoids open-coded
> variable-size object arithmetic.
> 
> Align the flexible array to unsigned long long so the driver-private area
> does not become less aligned than the old sizeof(struct snd_card) tail
> address on 32-bit ABIs.
> 
> Signed-off-by: Cássio Gabriel <cassiogabrielcontato@gmail.com>

Applied to for-next branch now.  Thanks.


Takashi