Skip to contentSkip to conversation

clk: Switch to use kmemdup_array()

PATCH Applied Andy Shevchenko 4 participants 13 messages Jun 6, 2024 – Aug 14, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover clk: Switch to use kmemdup_array()

Replace open coded kmemdup_array(), which does an additional overflow check.

Andy Shevchenko (4):
clk: mmp: Switch to use kmemdup_array()
clk: rockchip: Switch to use kmemdup_array()
clk: samsung: Switch to use kmemdup_array()
clk: visconti: Switch to use kmemdup_array()

 drivers/clk/mmp/clk-mix.c      | 10 ++++------
 drivers/clk/rockchip/clk-cpu.c |  5 ++---
 drivers/clk/rockchip/clk-pll.c |  8 ++++----
 drivers/clk/samsung/clk-cpu.c  |  4 ++--
 drivers/clk/samsung/clk-pll.c  |  8 ++++----
 drivers/clk/visconti/pll.c     |  6 +++---
 6 files changed, 19 insertions(+), 22 deletions(-)
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#1 lore raw

PATCH 1/4 clk: mmp: Switch to use kmemdup_array()

Let the kememdup_array() take care about multiplication and possible overflows.

---
 drivers/clk/mmp/clk-mix.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/clk/mmp/clk-mix.c +4 -6
index 454d131f475e..07ac9e6937e5 100644
@@ -447,7 +447,6 @@ struct clk *mmp_clk_register_mix(struct device *dev,
 	struct mmp_clk_mix *mix;
 	struct clk *clk;
 	struct clk_init_data init;
-	size_t table_bytes;
 
 	mix = kzalloc(sizeof(*mix), GFP_KERNEL);
 	if (!mix)
@@ -461,8 +460,8 @@ struct clk *mmp_clk_register_mix(struct device *dev,
 
 	memcpy(&mix->reg_info, &config->reg_info, sizeof(config->reg_info));
 	if (config->table) {
-		table_bytes = sizeof(*config->table) * config->table_size;
-		mix->table = kmemdup(config->table, table_bytes, GFP_KERNEL);
+		mix->table = kmemdup_array(config->table, config->table_size,
+					   sizeof(*mix->table), GFP_KERNEL);
 		if (!mix->table)
 			goto free_mix;
 
@@ -470,9 +469,8 @@ struct clk *mmp_clk_register_mix(struct device *dev,
 	}
 
 	if (config->mux_table) {
-		table_bytes = sizeof(u32) * num_parents;
-		mix->mux_table = kmemdup(config->mux_table, table_bytes,
-					 GFP_KERNEL);
+		mix->mux_table = kmemdup_array(config->mux_table, num_parents,
+					       sizeof(*mix->mux_table), GFP_KERNEL);
 		if (!mix->mux_table) {
 			kfree(mix->table);
 			goto free_mix;
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#2 lore raw

PATCH 2/4 clk: rockchip: Switch to use kmemdup_array()

In reply to Andy Shevchenko

Let the kememdup_array() take care about multiplication and possible overflows.

---
 drivers/clk/rockchip/clk-cpu.c | 5 ++---
 drivers/clk/rockchip/clk-pll.c | 8 ++++----
 2 files changed, 6 insertions(+), 7 deletions(-)
drivers/clk/rockchip/clk-cpu.c +2 -3
index 6ea7fba9f9e5..398a226ad34e 100644
@@ -369,9 +369,8 @@ struct clk *rockchip_clk_register_cpuclk(const char *name,
 
 	if (nrates > 0) {
 		cpuclk->rate_count = nrates;
-		cpuclk->rate_table = kmemdup(rates,
-					     sizeof(*rates) * nrates,
-					     GFP_KERNEL);
+		cpuclk->rate_table = kmemdup_array(rates, nrates, sizeof(*rates),
+						   GFP_KERNEL);
 		if (!cpuclk->rate_table) {
 			ret = -ENOMEM;
 			goto unregister_notifier;
drivers/clk/rockchip/clk-pll.c +4 -4
index 2d42eb628926..606ce5458f54 100644
@@ -1136,10 +1136,10 @@ struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
 			len++;
 
 		pll->rate_count = len;
-		pll->rate_table = kmemdup(rate_table,
-					pll->rate_count *
-					sizeof(struct rockchip_pll_rate_table),
-					GFP_KERNEL);
+		pll->rate_table = kmemdup_array(rate_table,
+						pll->rate_count,
+						sizeof(*pll->rate_table),
+						GFP_KERNEL);
 		WARN(!pll->rate_table,
 			"%s: could not allocate rate table for %s\n",
 			__func__, name);
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
maintainer
#3 lore raw

Re: [PATCH v1 2/4] clk: rockchip: Switch to use kmemdup_array()

Hi Andy,

Am Donnerstag, 6. Juni 2024, 18:09:32 CEST schrieb Andy Shevchenko:

Quoted 14 lines

Let the kememdup_array() take care about multiplication and possible overflows.

---
 drivers/clk/rockchip/clk-cpu.c | 5 ++---
 drivers/clk/rockchip/clk-pll.c | 8 ++++----
 2 files changed, 6 insertions(+), 7 deletions(-)
drivers/clk/rockchip/clk-cpu.c +0 -0
index 6ea7fba9f9e5..398a226ad34e 100644

drivers/clk/rockchip/clk-cpu.c struct clk *rockchip_clk_register_cpuclk(const char *name, · line 373

Quoted 8 lines
drivers/clk/rockchip/clk-cpu.c +2 -3
@@ -369,9 +369,8 @@ struct clk *rockchip_clk_register_cpuclk(const char *name,
 
 	if (nrates > 0) {
 		cpuclk->rate_count = nrates;
-		cpuclk->rate_table = kmemdup(rates,
-					     sizeof(*rates) * nrates,
-					     GFP_KERNEL);
+		cpuclk->rate_table = kmemdup_array(rates, nrates, sizeof(*rates),
+						   GFP_KERNEL);

are you sure the param order is correct?

According to [0], it's (src, element_size, count, gfp), while above (and below) element_size and count seems switched in the kmemdup_array calls.

Heiko

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/util.c#n149

Quoted 23 lines
drivers/clk/rockchip/clk-cpu.c +0 -0
 		if (!cpuclk->rate_table) {
 			ret = -ENOMEM;
 			goto unregister_notifier;
drivers/clk/rockchip/clk-pll.c +4 -4
index 2d42eb628926..606ce5458f54 100644
@@ -1136,10 +1136,10 @@ struct clk *rockchip_clk_register_pll(struct rockchip_clk_provider *ctx,
 			len++;
 
 		pll->rate_count = len;
-		pll->rate_table = kmemdup(rate_table,
-					pll->rate_count *
-					sizeof(struct rockchip_pll_rate_table),
-					GFP_KERNEL);
+		pll->rate_table = kmemdup_array(rate_table,
+						pll->rate_count,
+						sizeof(*pll->rate_table),
+						GFP_KERNEL);
 		WARN(!pll->rate_table,
 			"%s: could not allocate rate table for %s\n",
 			__func__, name);
#4 lore raw

Re: [PATCH v1 2/4] clk: rockchip: Switch to use kmemdup_array()

Quoted 1 line · Heiko Stübner

Am Donnerstag, 6. Juni 2024, 18:09:32 CEST schrieb Andy Shevchenko:

...

Quoted 11 lines

drivers/clk/rockchip/clk-cpu.c struct clk *rockchip_clk_register_cpuclk(const char *name, · line 373

Quoted 5 lines
drivers/clk/rockchip/clk-cpu.c +2 -3
-		cpuclk->rate_table = kmemdup(rates,
-					     sizeof(*rates) * nrates,
-					     GFP_KERNEL);
+		cpuclk->rate_table = kmemdup_array(rates, nrates, sizeof(*rates),
+						   GFP_KERNEL);

are you sure the param order is correct?

According to [0], it's (src, element_size, count, gfp), while above (and below) element_size and count seems switched in the kmemdup_array calls.

I'm glad you asked. The parameter order is going to be fixed [1].

Quoted 1 line

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/util.c#n149

[1]: 0ee14725471c ("mm/util: Swap kmemdup_array() arguments")

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#5 lore raw

Re: [PATCH v1 2/4] clk: rockchip: Switch to use kmemdup_array()

Am Dienstag, 11. Juni 2024, 15:20:05 CEST schrieb Andy Shevchenko:

Quoted 17 lines · Heiko Stübner
Quoted 1 line

Am Donnerstag, 6. Juni 2024, 18:09:32 CEST schrieb Andy Shevchenko:

...

Quoted 11 lines

drivers/clk/rockchip/clk-cpu.c struct clk *rockchip_clk_register_cpuclk(const char *name, · line 373

Quoted 5 lines
drivers/clk/rockchip/clk-cpu.c +2 -3
-		cpuclk->rate_table = kmemdup(rates,
-					     sizeof(*rates) * nrates,
-					     GFP_KERNEL);
+		cpuclk->rate_table = kmemdup_array(rates, nrates, sizeof(*rates),
+						   GFP_KERNEL);

are you sure the param order is correct?

According to [0], it's (src, element_size, count, gfp), while above (and below) element_size and count seems switched in the kmemdup_array calls.

Quoted 5 lines

I'm glad you asked. The parameter order is going to be fixed [1].

Quoted 1 line

[0] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/util.c#n149

[1]: 0ee14725471c ("mm/util: Swap kmemdup_array() arguments")

ah that clears it up :-)

Thanks for the pointer
Heiko

#6 lore raw

PATCH 3/4 clk: samsung: Switch to use kmemdup_array()

In reply to Andy Shevchenko

Let the kememdup_array() take care about multiplication and possible overflows.

---
 drivers/clk/samsung/clk-cpu.c | 4 ++--
 drivers/clk/samsung/clk-pll.c | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)
drivers/clk/samsung/clk-cpu.c +2 -2
index fbf4c4208e06..dfa149e648aa 100644
@@ -689,8 +689,8 @@ static int __init exynos_register_cpu_clock(struct samsung_clk_provider *ctx,
 	for (num_cfgs = 0; clk_data->cfg[num_cfgs].prate != 0; )
 		num_cfgs++;
 
-	cpuclk->cfg = kmemdup(clk_data->cfg, sizeof(*clk_data->cfg) * num_cfgs,
-			      GFP_KERNEL);
+	cpuclk->cfg = kmemdup_array(clk_data->cfg, num_cfgs, sizeof(*cpuclk->cfg),
+				    GFP_KERNEL);
 	if (!cpuclk->cfg) {
 		ret = -ENOMEM;
 		goto unregister_clk_nb;
drivers/clk/samsung/clk-pll.c +4 -4
index 4bbdf5e91650..4be879ab917e 100644
@@ -1286,10 +1286,10 @@ static void __init _samsung_clk_register_pll(struct samsung_clk_provider *ctx,
 			len++;
 
 		pll->rate_count = len;
-		pll->rate_table = kmemdup(pll_clk->rate_table,
-					pll->rate_count *
-					sizeof(struct samsung_pll_rate_table),
-					GFP_KERNEL);
+		pll->rate_table = kmemdup_array(pll_clk->rate_table,
+						pll->rate_count,
+						sizeof(*pll->rate_table),
+						GFP_KERNEL);
 		WARN(!pll->rate_table,
 			"%s: could not allocate rate table for %s\n",
 			__func__, pll_clk->name);
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#8 lore raw

PATCH 4/4 clk: visconti: Switch to use kmemdup_array()

In reply to Andy Shevchenko

Let the kememdup_array() take care about multiplication and possible overflows.

---
 drivers/clk/visconti/pll.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/clk/visconti/pll.c +3 -3
index e9cd80e085dc..3f929cf8dd2f 100644
@@ -262,9 +262,9 @@ static struct clk_hw *visconti_register_pll(struct visconti_pll_provider *ctx,
 	for (len = 0; rate_table[len].rate != 0; )
 		len++;
 	pll->rate_count = len;
-	pll->rate_table = kmemdup(rate_table,
-				  pll->rate_count * sizeof(struct visconti_pll_rate_table),
-				  GFP_KERNEL);
+	pll->rate_table = kmemdup_array(rate_table,
+					pll->rate_count, sizeof(*pll->rate_table),
+					GFP_KERNEL);
 	WARN(!pll->rate_table, "%s: could not allocate rate table for %s\n", __func__, name);
 
 	init.ops = &visconti_pll_ops;
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
maintainer Applied
#9 lore raw

Re: (subset) [PATCH v1 0/4] clk: Switch to use kmemdup_array()

In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

Replace open coded kmemdup_array(), which does an additional overflow check.

Andy Shevchenko (4):
clk: mmp: Switch to use kmemdup_array()
clk: rockchip: Switch to use kmemdup_array()
clk: samsung: Switch to use kmemdup_array()
clk: visconti: Switch to use kmemdup_array()

[...]

Applied, thanks!

[2/4] clk: rockchip: Switch to use kmemdup_array()

      commit: c9ba07b0c02acd89f2e521754357de30e704c254

Best regards,

Signature · 1 line
Heiko Stuebner <[email protected]>
#10 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Replace open coded kmemdup_array(), which does an additional overflow check.

...

Quoted 3 lines

clk: mmp: Switch to use kmemdup_array()

clk: visconti: Switch to use kmemdup_array()

Any news for these two?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#11 lore raw

Quoting Andy Shevchenko (2024-08-13 01:37:48)

Quoted 9 lines · Andy Shevchenko
Quoted 2 lines

Replace open coded kmemdup_array(), which does an additional overflow check.

...

Quoted 3 lines

clk: mmp: Switch to use kmemdup_array()

clk: visconti: Switch to use kmemdup_array()

I have them all as "changes requested" so please resend.

#12 lore raw
Quoted 2 lines · Stephen Boyd

Quoting Andy Shevchenko (2024-08-13 01:37:48)

Quoted 1 line · Andy Shevchenko

...

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

clk: mmp: Switch to use kmemdup_array()

clk: visconti: Switch to use kmemdup_array()

I have them all as "changes requested" so please resend.

Done (Message-Id: [email protected]).

Signature · 2 lines
With Best Regards,
Andy Shevchenko