Skip to contentSkip to conversation

ALSA: core: Use flexible array for card private data

PATCH Superseded Cássio Gabriel 1 participant 1 message May 29 Sound

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: core: Use flexible array for card private data

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 and use struct_size() for the 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.

---
 include/sound/core.h | 2 ++
 sound/core/init.c    | 8 +++++---
 2 files changed, 7 insertions(+), 3 deletions(-)
include/sound/core.h +2 -0
index 4093ec82a0a1..4bb76c21c956 100644
@@ -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)
sound/core/init.c +5 -3
index 0c316189e947..98e702ffa856 100644
@@ -181,7 +181,8 @@ 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(struct_size(card, private_data_area, extra_size),
+		       GFP_KERNEL);
 	if (!card)
 		return -ENOMEM;
 
@@ -232,7 +233,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 +282,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: c784d0e6a62abbd2af58bbbe2d20f88dd550e3eb
change-id: 20260526-alsa-card-private-flex-array-5f4dfbcbff23

Best regards,

Signature · 1 line
Cássio Gabriel <[email protected]>