Skip to contentSkip to conversation

clk: Switch to use kmemdup_array()

PATCH Applied Andy Shevchenko 2 participants 5 messages Aug 14, 2024 – Aug 15, 2024

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

v2 cover clk: Switch to use kmemdup_array()

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

v2:
- dropped upstreamed changes
- fixed a typo in the commit messages

Andy Shevchenko (2):
clk: mmp: Switch to use kmemdup_array()
clk: visconti: Switch to use kmemdup_array()

 drivers/clk/mmp/clk-mix.c  | 10 ++++------
 drivers/clk/visconti/pll.c |  6 +++---
 2 files changed, 7 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#1 lore raw

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

Let the kmemdup_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
maintainer Applied
#2 lore raw

Re: [PATCH v2 1/2] clk: mmp: Switch to use kmemdup_array()

Quoting Andy Shevchenko (2024-08-14 05:54:07)

Quoted 5 lines

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

---

Applied to clk-next

#3 lore raw

PATCH v2 2/2 clk: visconti: Switch to use kmemdup_array()

In reply to Andy Shevchenko

Let the kmemdup_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
#4 lore raw

Re: [PATCH v2 2/2] clk: visconti: Switch to use kmemdup_array()

Quoting Andy Shevchenko (2024-08-14 05:54:08)

Quoted 5 lines

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

---

Applied to clk-next