Skip to contentSkip to conversation

clk: ti: dra7-atl: don't allocate `parent_names' variable

PATCH In review Dario Binacchi 2 participants 4 messages Oct 18, 2022 – Nov 1, 2022

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

PATCH v2 1/1 clk: ti: dra7-atl: don't allocate `parent_names' variable

The `parent_names' variable was freed also in case of kzalloc() error. Instead of modifying the code to perform a proper memory release, I decided to fix the bug by not allocating memory.
Since only one parent name is referenced, it is not necessary to allocate this variable at runtime and therefore you can avoid calling the kzalloc() function. This simplifies the code (even calls to kfree can be removed) and improves the performance of the routine.

Note: Although no operation is performed by kfree() on a NULL pointer, it was however suboptimal and semantically wrong doing it.

---

Changes in v2:
- Fix compiling error
- Add kernel test robot's Reported-by tag.

 drivers/clk/ti/clk-dra7-atl.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/clk/ti/clk-dra7-atl.c +2 -9
index ff4d6a951681..78482d1a4a33 100644
@@ -164,7 +164,7 @@ static void __init of_dra7_atl_clock_setup(struct device_node *node)
 {
 	struct dra7_atl_desc *clk_hw = NULL;
 	struct clk_init_data init = { NULL };
-	const char **parent_names = NULL;
+	const char *parent_names[1];
 	const char *name;
 	struct clk *clk;
 
@@ -188,24 +188,17 @@ static void __init of_dra7_atl_clock_setup(struct device_node *node)
 		goto cleanup;
 	}
 
-	parent_names = kzalloc(sizeof(char *), GFP_KERNEL);
-
-	if (!parent_names)
-		goto cleanup;
-
 	parent_names[0] = of_clk_get_parent_name(node, 0);
 
 	init.parent_names = parent_names;
 
 	clk = ti_clk_register(NULL, &clk_hw->hw, name);
-
 	if (!IS_ERR(clk)) {
 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
-		kfree(parent_names);
 		return;
 	}
+
 cleanup:
-	kfree(parent_names);
 	kfree(clk_hw);
 }
 CLK_OF_DECLARE(dra7_atl_clock, "ti,dra7-atl-clock", of_dra7_atl_clock_setup);
Signature · 1 line
2.32.0
maintainer
#1 lore raw

Quoting Dario Binacchi (2022-10-18 09:03:52)

Quoted 5 lines
drivers/clk/ti/clk-dra7-atl.c +0 -0
index ff4d6a951681..78482d1a4a33 100644

drivers/clk/ti/clk-dra7-atl.c static void __init of_dra7_atl_clock_setup(struct device_node *node) · line 191

Quoted 9 lines
drivers/clk/ti/clk-dra7-atl.c +0 -5
@@ -188,24 +188,17 @@ static void __init of_dra7_atl_clock_setup(struct device_node *node)
                goto cleanup;
        }
 
-       parent_names = kzalloc(sizeof(char *), GFP_KERNEL);
-
-       if (!parent_names)
-               goto cleanup;
-
        parent_names[0] = of_clk_get_parent_name(node, 0);

Can you use struct clk_parent_data instead and assign index to 0? Then we don't even need to use of_clk_get_parent_name() here.

#2 lore raw

Hi Stephen,

Quoted 10 lines · Stephen Boyd

Quoting Dario Binacchi (2022-10-18 09:03:52)

Quoted 8 lines
drivers/clk/ti/clk-dra7-atl.c +0 -0
index ff4d6a951681..78482d1a4a33 100644
@@ -188,24 +188,17 @@ static void __init of_dra7_atl_clock_setup(struct device_node *node)
                goto cleanup;
        }
 
Quoted 9 lines

drivers/clk/ti/clk-dra7-atl.c static void __init of_dra7_atl_clock_setup(struct device_node *node) · line 191

Quoted 6 lines
drivers/clk/ti/clk-dra7-atl.c +0 -5
-       parent_names = kzalloc(sizeof(char *), GFP_KERNEL);
-
-       if (!parent_names)
-               goto cleanup;
-
        parent_names[0] = of_clk_get_parent_name(node, 0);

Can you use struct clk_parent_data instead and assign index to 0? Then we don't even need to use of_clk_get_parent_name() here.

I tried to test your suggestions on another platform (I don't have the hw to test the driver change) but if I
don't add pdata.name = of_clk_get_parent_name () the board boot up fails.

As far I can see from the clk_core_populate_parent_map()

....
/* Copy everything over because it might be __initdata */

for (i = 0, parent = parents; i < num_parents; i++, parent++) {
    parent->index = -1;
    if (parent_names) {
        /* throw a WARN if any entries are NULL */
       WARN(!parent_names[i],
            "%s: invalid NULL in %s's .parent_names\n",
            __func__, core->name);
        ret = clk_cpy_name(&parent->name, parent_names[i],
                                        true);
    } else if (parent_data) {
        parent->hw = parent_data[i].hw;
        parent->index = parent_data[i].index;
        ret = clk_cpy_name(&parent->fw_name,
                                         parent_data[i].fw_name, false);
        if (!ret)
            ret = clk_cpy_name(&parent->name,
                                            parent_data[i].name,
                                            false);

...

The function clk_cpy_name() is called with the parameter "mus_exist" to true in the path "parent_names" and false
in the path "parent_data". Therefore, in the path "parent_data" it is allowed that parent-> name is not set.
In doing so, therefore, the change would not even be backward compatible.

So, IMHO, there are 2 possible options:
1 okay to use parent_data, but we keep using of_clk_get_parent_name
() to set parent_data::name.
2 okay to use the version v2 of the patch.

What do you think?

Thanks and regards,
Dario

Signature · 17 lines
Dario Binacchi

Embedded Linux Developer

[email protected]

__________________________________


Amarula Solutions SRL

Via Le Canevare 30, 31100 Treviso, Veneto, IT

T. +39 042 243 5310
[email protected]

www.amarulasolutions.com
maintainer
#3 lore raw

Quoting Dario Binacchi (2022-10-30 06:00:46)

Quoted 36 lines

I tried to test your suggestions on another platform (I don't have the hw to test the driver change) but if I
don't add pdata.name = of_clk_get_parent_name () the board boot up fails.

As far I can see from the clk_core_populate_parent_map()

....
/* Copy everything over because it might be __initdata */

for (i = 0, parent = parents; i < num_parents; i++, parent++) {
    parent->index = -1;
    if (parent_names) {
        /* throw a WARN if any entries are NULL */
       WARN(!parent_names[i],
            "%s: invalid NULL in %s's .parent_names\n",
            __func__, core->name);
        ret = clk_cpy_name(&parent->name, parent_names[i],
                                        true);
    } else if (parent_data) {
        parent->hw = parent_data[i].hw;
        parent->index = parent_data[i].index;
        ret = clk_cpy_name(&parent->fw_name,
                                         parent_data[i].fw_name, false);
        if (!ret)
            ret = clk_cpy_name(&parent->name,
                                            parent_data[i].name,
                                            false);

...

The function clk_cpy_name() is called with the parameter "mus_exist" to true in the path "parent_names" and false
in the path "parent_data". Therefore, in the path "parent_data" it is allowed that parent-> name is not set.
In doing so, therefore, the change would not even be backward compatible.

Quoted 6 lines

So, IMHO, there are 2 possible options:
1 okay to use parent_data, but we keep using of_clk_get_parent_name
() to set parent_data::name.
2 okay to use the version v2 of the patch.

What do you think?

I am confused.

The struct clk_parent_data::name being used is whatever string is returned by of_clk_get_parent_name(node, 0). That is the same as setting struct clk_parent_data::index to 0, and not assigning the 'name' or 'fw_name' field of the parent data structure. This is a compatible change because of_clk_get_parent_name() is getting the name of the clk in 'clocks' for 'node' at index 0. Using the index 0 in clk_parent_data tells clk framework that the parent of the clk being registered is the clk in 'clocks' for the 'dev->node' that is passed in during clk_register(). If you don't have a device pointer, use of_clk_hw_register() to pass 'node' directly. It looks like you will have to do that in this case to get the node pointer registered with this clk.