Skip to contentSkip to conversation

clk: tegra: bmp: remove kcalloc

PATCH RESEND Superseded Rosen Penev 4 participants 5 messages Sep 29 – Sep 30

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 clk: tegra: bmp: remove kcalloc

Use a flexible array member to avoid allocating separately.

Use __counted_by for extra runtime analysis.

---
 drivers/clk/tegra/clk-bpmp.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/clk/tegra/clk-bpmp.c +3 -9
index 067a8555c4e7..15b976162b59 100644
@@ -32,7 +32,7 @@ struct tegra_bpmp_clk {
 	unsigned int id;
 
 	unsigned int num_parents;
-	unsigned int *parents;
+	unsigned int parents[] __counted_by(num_parents);
 };
 
 static inline struct tegra_bpmp_clk *to_tegra_bpmp_clk(struct clk_hw *hw)
@@ -518,20 +518,14 @@ tegra_bpmp_clk_register(struct tegra_bpmp *bpmp,
 	unsigned int i;
 	int err;
 
-	clk = devm_kzalloc(bpmp->dev, sizeof(*clk), GFP_KERNEL);
+	clk = devm_kzalloc(bpmp->dev, struct_size(clk, parents, info->num_parents), GFP_KERNEL);
 	if (!clk)
 		return ERR_PTR(-ENOMEM);
 
+	clk->num_parents = info->num_parents;
 	clk->id = info->id;
 	clk->bpmp = bpmp;
 
-	clk->parents = devm_kcalloc(bpmp->dev, info->num_parents,
-				    sizeof(*clk->parents), GFP_KERNEL);
-	if (!clk->parents)
-		return ERR_PTR(-ENOMEM);
-
-	clk->num_parents = info->num_parents;
-
 	/* hardware clock initialization */
 	memset(&init, 0, sizeof(init));
 	init.name = info->name;
Signature · 1 line
2.55.0
#1 lore raw
Quoted 32 lines · Rosen Penev

Use a flexible array member to avoid allocating separately.

Use __counted_by for extra runtime analysis.

---
 drivers/clk/tegra/clk-bpmp.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/clk/tegra/clk-bpmp.c +3 -2
index 067a8555c4e7..15b976162b59 100644
@@ -32,7 +32,7 @@ struct tegra_bpmp_clk {
 	unsigned int id;
 
 	unsigned int num_parents;
-	unsigned int *parents;
+	unsigned int parents[] __counted_by(num_parents);
 };
 
 static inline struct tegra_bpmp_clk *to_tegra_bpmp_clk(struct clk_hw *hw)
@@ -518,20 +518,14 @@ tegra_bpmp_clk_register(struct tegra_bpmp *bpmp,
 	unsigned int i;
 	int err;
 
-	clk = devm_kzalloc(bpmp->dev, sizeof(*clk), GFP_KERNEL);
+	clk = devm_kzalloc(bpmp->dev, struct_size(clk, parents, info->num_parents), GFP_KERNEL);
 	if (!clk)
 		return ERR_PTR(-ENOMEM);
 
+	clk->num_parents = info->num_parents;

drivers/clk/tegra/clk-bpmp.c tegra_bpmp_clk_register(struct tegra_bpmp *bpmp, · line 534

Quoted 10 lines
drivers/clk/tegra/clk-bpmp.c +0 -7
 	clk->id = info->id;
 	clk->bpmp = bpmp;
 
-	clk->parents = devm_kcalloc(bpmp->dev, info->num_parents,
-				    sizeof(*clk->parents), GFP_KERNEL);
-	if (!clk->parents)
-		return ERR_PTR(-ENOMEM);
-
-	clk->num_parents = info->num_parents;
-

What is the benefit of doing this ? It looks like you are just trading one alloc for another here ?

Quoted 6 lines
drivers/clk/tegra/clk-bpmp.c +0 -0
 	/* hardware clock initialization */
 	memset(&init, 0, sizeof(init));
 	init.name = info->name;
Signature · 1 line
2.55.0
Signature · 1 line
Jerome
maintainer
#2 lore raw
Quoted 37 lines · Jerome Brunet
Quoted 35 lines · Rosen Penev

Use a flexible array member to avoid allocating separately.

Use __counted_by for extra runtime analysis.

---
 drivers/clk/tegra/clk-bpmp.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/clk/tegra/clk-bpmp.c +3 -2
index 067a8555c4e7..15b976162b59 100644
@@ -32,7 +32,7 @@ struct tegra_bpmp_clk {
 	unsigned int id;
 
 	unsigned int num_parents;
-	unsigned int *parents;
+	unsigned int parents[] __counted_by(num_parents);
 };
 
 static inline struct tegra_bpmp_clk *to_tegra_bpmp_clk(struct clk_hw *hw)
@@ -518,20 +518,14 @@ tegra_bpmp_clk_register(struct tegra_bpmp *bpmp,
 	unsigned int i;
 	int err;
 
-	clk = devm_kzalloc(bpmp->dev, sizeof(*clk), GFP_KERNEL);
+	clk = devm_kzalloc(bpmp->dev, struct_size(clk, parents, info->num_parents), GFP_KERNEL);
 	if (!clk)
 		return ERR_PTR(-ENOMEM);
 
+	clk->num_parents = info->num_parents;
 	clk->id = info->id;
 	clk->bpmp = bpmp;
 
Quoted 10 lines

drivers/clk/tegra/clk-bpmp.c tegra_bpmp_clk_register(struct tegra_bpmp *bpmp, · line 534

Quoted 7 lines
drivers/clk/tegra/clk-bpmp.c +0 -7
-	clk->parents = devm_kcalloc(bpmp->dev, info->num_parents,
-				    sizeof(*clk->parents), GFP_KERNEL);
-	if (!clk->parents)
-		return ERR_PTR(-ENOMEM);
-
-	clk->num_parents = info->num_parents;
-

What is the benefit of doing this ? It looks like you are just trading one alloc for another here ?

I think the point is allocating the parents array as part of the main allocation, so we end up with one allocation instead of two. This avoids fragmentation and overhead and is possibly a little bit faster.

Seems fine to me.

Thierry

maintainer Reviewed-by Acked-by on v1 1/1
#3 lore raw
In reply to Rosen Penev
Quoted 23 lines · Rosen Penev

Use a flexible array member to avoid allocating separately.

Use __counted_by for extra runtime analysis.

---
 drivers/clk/tegra/clk-bpmp.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/clk/tegra/clk-bpmp.c +1 -1
index 067a8555c4e7..15b976162b59 100644
@@ -32,7 +32,7 @@ struct tegra_bpmp_clk {
 	unsigned int id;
 
 	unsigned int num_parents;
-	unsigned int *parents;
+	unsigned int parents[] __counted_by(num_parents);
 };
 
 static inline struct tegra_bpmp_clk *to_tegra_bpmp_clk(struct clk_hw *hw)

drivers/clk/tegra/clk-bpmp.c tegra_bpmp_clk_register(struct tegra_bpmp *bpmp, · line 521

Quoted 5 lines
drivers/clk/tegra/clk-bpmp.c +1 -1
@@ -518,20 +518,14 @@ tegra_bpmp_clk_register(struct tegra_bpmp *bpmp,
 	unsigned int i;
 	int err;
 
-	clk = devm_kzalloc(bpmp->dev, sizeof(*clk), GFP_KERNEL);
+	clk = devm_kzalloc(bpmp->dev, struct_size(clk, parents, info->num_parents), GFP_KERNEL);

k{m,z}alloc_flex() exists for this, but there's no devm variant of those, so I guess someone should add one. In the meantime, this is as good as it's going to get for now: