public inbox for linux-sound@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ALSA: hda: allocate nodes with tree
@ 2026-04-25  2:34 Rosen Penev
  2026-04-27 11:47 ` Takashi Iwai
  0 siblings, 1 reply; 3+ messages in thread
From: Rosen Penev @ 2026-04-25  2:34 UTC (permalink / raw)
  To: linux-sound; +Cc: Jaroslav Kysela, Takashi Iwai, open list

Use a flexible array member to combine allocations. Simplifies
allocation and freeing.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 sound/hda/core/sysfs.c | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)

diff --git a/sound/hda/core/sysfs.c b/sound/hda/core/sysfs.c
index 3e102538e058..f14ce1203a9f 100644
--- a/sound/hda/core/sysfs.c
+++ b/sound/hda/core/sysfs.c
@@ -13,7 +13,7 @@
 struct hdac_widget_tree {
 	struct kobject *root;
 	struct kobject *afg;
-	struct kobject **nodes;
+	struct kobject *nodes[];
 };
 
 #define CODEC_ATTR(type)					\
@@ -325,11 +325,8 @@ static void widget_tree_free(struct hdac_device *codec)
 	if (!tree)
 		return;
 	free_widget_node(tree->afg, &widget_afg_group);
-	if (tree->nodes) {
-		for (p = tree->nodes; *p; p++)
-			free_widget_node(*p, &widget_node_group);
-		kfree(tree->nodes);
-	}
+	for (p = tree->nodes; *p; p++)
+		free_widget_node(*p, &widget_node_group);
 	kobject_put(tree->root);
 	kfree(tree);
 	codec->widgets = NULL;
@@ -366,7 +363,7 @@ static int widget_tree_create(struct hdac_device *codec)
 	int i, err;
 	hda_nid_t nid;
 
-	tree = codec->widgets = kzalloc_obj(*tree);
+	tree = codec->widgets = kzalloc_flex(*tree, nodes, codec->num_nodes + 1);
 	if (!tree)
 		return -ENOMEM;
 
@@ -374,10 +371,6 @@ static int widget_tree_create(struct hdac_device *codec)
 	if (!tree->root)
 		return -ENOMEM;
 
-	tree->nodes = kzalloc_objs(*tree->nodes, codec->num_nodes + 1);
-	if (!tree->nodes)
-		return -ENOMEM;
-
 	for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
 		err = add_widget_node(tree->root, nid, &widget_node_group,
 				      &tree->nodes[i]);
@@ -435,12 +428,6 @@ int hda_widget_sysfs_reinit(struct hdac_device *codec,
 	if (!tree)
 		return -ENOMEM;
 
-	tree->nodes = kzalloc_objs(*tree->nodes, num_nodes + 1);
-	if (!tree->nodes) {
-		kfree(tree);
-		return -ENOMEM;
-	}
-
 	/* prune non-existing nodes */
 	for (i = 0, nid = codec->start_nid; i < codec->num_nodes; i++, nid++) {
 		if (nid < start_nid || nid >= end_nid)
@@ -459,7 +446,6 @@ int hda_widget_sysfs_reinit(struct hdac_device *codec,
 	}
 
 	/* replace with the new tree */
-	kfree(codec->widgets->nodes);
 	kfree(codec->widgets);
 	codec->widgets = tree;
 
-- 
2.54.0


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

* Re: [PATCH] ALSA: hda: allocate nodes with tree
  2026-04-25  2:34 [PATCH] ALSA: hda: allocate nodes with tree Rosen Penev
@ 2026-04-27 11:47 ` Takashi Iwai
  2026-04-27 11:55   ` Takashi Iwai
  0 siblings, 1 reply; 3+ messages in thread
From: Takashi Iwai @ 2026-04-27 11:47 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-sound, Jaroslav Kysela, Takashi Iwai, open list

On Sat, 25 Apr 2026 04:34:21 +0200,
Rosen Penev wrote:
> 
> Use a flexible array member to combine allocations. Simplifies
> allocation and freeing.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>

Applied to for-next branch now.  Thanks.


Takashi

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

* Re: [PATCH] ALSA: hda: allocate nodes with tree
  2026-04-27 11:47 ` Takashi Iwai
@ 2026-04-27 11:55   ` Takashi Iwai
  0 siblings, 0 replies; 3+ messages in thread
From: Takashi Iwai @ 2026-04-27 11:55 UTC (permalink / raw)
  To: Rosen Penev; +Cc: linux-sound, Jaroslav Kysela, Takashi Iwai, open list

On Mon, 27 Apr 2026 13:47:12 +0200,
Takashi Iwai wrote:
> 
> On Sat, 25 Apr 2026 04:34:21 +0200,
> Rosen Penev wrote:
> > 
> > Use a flexible array member to combine allocations. Simplifies
> > allocation and freeing.
> > 
> > Signed-off-by: Rosen Penev <rosenp@gmail.com>
> 
> Applied to for-next branch now.  Thanks.

Scratch that.  I re-read the code, and noticed that it doesn't handle
the case in hda_widget_sysfs_reinit().  It can resize and reallocate
the node table, hence the embedded flex array won't work.


thanks,

Takashi

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

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

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-25  2:34 [PATCH] ALSA: hda: allocate nodes with tree Rosen Penev
2026-04-27 11:47 ` Takashi Iwai
2026-04-27 11:55   ` Takashi Iwai

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