Skip to content

Commit 1c5e3d9

Browse files
committed
of: Add a helper to free property struct
Freeing a property struct is 3 kfree()'s which is duplicated in multiple spots. Add a helper, __of_prop_free(), and replace all the open coded cases in the DT code. Reviewed-by: Saravana Kannan <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rob Herring <[email protected]>
1 parent 15f3df3 commit 1c5e3d9

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

drivers/of/dynamic.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,20 @@ int of_detach_node(struct device_node *np)
305305
}
306306
EXPORT_SYMBOL_GPL(of_detach_node);
307307

308+
void __of_prop_free(struct property *prop)
309+
{
310+
kfree(prop->name);
311+
kfree(prop->value);
312+
kfree(prop);
313+
}
314+
308315
static void property_list_free(struct property *prop_list)
309316
{
310317
struct property *prop, *next;
311318

312319
for (prop = prop_list; prop != NULL; prop = next) {
313320
next = prop->next;
314-
kfree(prop->name);
315-
kfree(prop->value);
316-
kfree(prop);
321+
__of_prop_free(prop);
317322
}
318323
}
319324

@@ -426,9 +431,7 @@ struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
426431
return new;
427432

428433
err_free:
429-
kfree(new->name);
430-
kfree(new->value);
431-
kfree(new);
434+
__of_prop_free(new);
432435
return NULL;
433436
}
434437

@@ -470,9 +473,7 @@ struct device_node *__of_node_dup(const struct device_node *np,
470473
if (!new_pp)
471474
goto err_prop;
472475
if (__of_add_property(node, new_pp)) {
473-
kfree(new_pp->name);
474-
kfree(new_pp->value);
475-
kfree(new_pp);
476+
__of_prop_free(new_pp);
476477
goto err_prop;
477478
}
478479
}
@@ -921,11 +922,8 @@ static int of_changeset_add_prop_helper(struct of_changeset *ocs,
921922
return -ENOMEM;
922923

923924
ret = of_changeset_add_property(ocs, np, new_pp);
924-
if (ret) {
925-
kfree(new_pp->name);
926-
kfree(new_pp->value);
927-
kfree(new_pp);
928-
}
925+
if (ret)
926+
__of_prop_free(new_pp);
929927

930928
return ret;
931929
}

drivers/of/of_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ extern void *__unflatten_device_tree(const void *blob,
123123
* own the devtree lock or work on detached trees only.
124124
*/
125125
struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags);
126+
void __of_prop_free(struct property *prop);
126127
struct device_node *__of_node_dup(const struct device_node *np,
127128
const char *full_name);
128129

drivers/of/overlay.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,7 @@ static struct property *dup_and_fixup_symbol_prop(
262262
return new_prop;
263263

264264
err_free_new_prop:
265-
kfree(new_prop->name);
266-
kfree(new_prop->value);
267-
kfree(new_prop);
265+
__of_prop_free(new_prop);
268266
err_free_target_path:
269267
kfree(target_path);
270268

@@ -361,11 +359,8 @@ static int add_changeset_property(struct overlay_changeset *ovcs,
361359
pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
362360
target->np, new_prop->name);
363361

364-
if (ret) {
365-
kfree(new_prop->name);
366-
kfree(new_prop->value);
367-
kfree(new_prop);
368-
}
362+
if (ret)
363+
__of_prop_free(new_prop);
369364
return ret;
370365
}
371366

drivers/of/unittest.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -795,15 +795,11 @@ static void __init of_unittest_property_copy(void)
795795

796796
new = __of_prop_dup(&p1, GFP_KERNEL);
797797
unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
798-
kfree(new->value);
799-
kfree(new->name);
800-
kfree(new);
798+
__of_prop_free(new);
801799

802800
new = __of_prop_dup(&p2, GFP_KERNEL);
803801
unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
804-
kfree(new->value);
805-
kfree(new->name);
806-
kfree(new);
802+
__of_prop_free(new);
807803
#endif
808804
}
809805

@@ -3718,9 +3714,7 @@ static __init void of_unittest_overlay_high_level(void)
37183714
goto err_unlock;
37193715
}
37203716
if (__of_add_property(of_symbols, new_prop)) {
3721-
kfree(new_prop->name);
3722-
kfree(new_prop->value);
3723-
kfree(new_prop);
3717+
__of_prop_free(new_prop);
37243718
/* "name" auto-generated by unflatten */
37253719
if (!strcmp(prop->name, "name"))
37263720
continue;

0 commit comments

Comments
 (0)