of: Add a helper to free property struct
authorRob Herring <robh@kernel.org>
Tue, 9 Apr 2024 18:59:39 +0000 (13:59 -0500)
committerRob Herring <robh@kernel.org>
Mon, 15 Apr 2024 13:40:40 +0000 (08:40 -0500)
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 <saravanak@google.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Link: https://lore.kernel.org/r/20240409-dt-cleanup-free-v2-1-5b419a4af38d@kernel.org
Signed-off-by: Rob Herring <robh@kernel.org>
drivers/of/dynamic.c
drivers/of/of_private.h
drivers/of/overlay.c
drivers/of/unittest.c

index 3bf27052832f302ac72d366b986629e03db4e900..af7c57a7a25d747e93fcbbd5d18e798cb0612852 100644 (file)
@@ -305,15 +305,20 @@ int of_detach_node(struct device_node *np)
 }
 EXPORT_SYMBOL_GPL(of_detach_node);
 
+void __of_prop_free(struct property *prop)
+{
+       kfree(prop->name);
+       kfree(prop->value);
+       kfree(prop);
+}
+
 static void property_list_free(struct property *prop_list)
 {
        struct property *prop, *next;
 
        for (prop = prop_list; prop != NULL; prop = next) {
                next = prop->next;
-               kfree(prop->name);
-               kfree(prop->value);
-               kfree(prop);
+               __of_prop_free(prop);
        }
 }
 
@@ -426,9 +431,7 @@ struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags)
        return new;
 
  err_free:
-       kfree(new->name);
-       kfree(new->value);
-       kfree(new);
+       __of_prop_free(new);
        return NULL;
 }
 
@@ -470,9 +473,7 @@ struct device_node *__of_node_dup(const struct device_node *np,
                        if (!new_pp)
                                goto err_prop;
                        if (__of_add_property(node, new_pp)) {
-                               kfree(new_pp->name);
-                               kfree(new_pp->value);
-                               kfree(new_pp);
+                               __of_prop_free(new_pp);
                                goto err_prop;
                        }
                }
@@ -921,11 +922,8 @@ static int of_changeset_add_prop_helper(struct of_changeset *ocs,
                return -ENOMEM;
 
        ret = of_changeset_add_property(ocs, np, new_pp);
-       if (ret) {
-               kfree(new_pp->name);
-               kfree(new_pp->value);
-               kfree(new_pp);
-       }
+       if (ret)
+               __of_prop_free(new_pp);
 
        return ret;
 }
index 485483524b7f1cf1e2c0833c063cd24637de7751..94fc0aa07af9e383a1e0d844ac620f26a025bfd1 100644 (file)
@@ -123,6 +123,7 @@ extern void *__unflatten_device_tree(const void *blob,
  * own the devtree lock or work on detached trees only.
  */
 struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags);
+void __of_prop_free(struct property *prop);
 struct device_node *__of_node_dup(const struct device_node *np,
                                  const char *full_name);
 
index 2ae7e9d24a645576ec34031547c384407232b382..4d861a75d6946247a9c397fbcaac4cfa0d71af4f 100644 (file)
@@ -262,9 +262,7 @@ static struct property *dup_and_fixup_symbol_prop(
        return new_prop;
 
 err_free_new_prop:
-       kfree(new_prop->name);
-       kfree(new_prop->value);
-       kfree(new_prop);
+       __of_prop_free(new_prop);
 err_free_target_path:
        kfree(target_path);
 
@@ -361,11 +359,8 @@ static int add_changeset_property(struct overlay_changeset *ovcs,
                pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",
                       target->np, new_prop->name);
 
-       if (ret) {
-               kfree(new_prop->name);
-               kfree(new_prop->value);
-               kfree(new_prop);
-       }
+       if (ret)
+               __of_prop_free(new_prop);
        return ret;
 }
 
index 6b5c36b6a75862010f443321da4aa9d8d0c1c5eb..a8c01c953a295e40b8979d10dd40c4ba3aa3717c 100644 (file)
@@ -795,15 +795,11 @@ static void __init of_unittest_property_copy(void)
 
        new = __of_prop_dup(&p1, GFP_KERNEL);
        unittest(new && propcmp(&p1, new), "empty property didn't copy correctly\n");
-       kfree(new->value);
-       kfree(new->name);
-       kfree(new);
+       __of_prop_free(new);
 
        new = __of_prop_dup(&p2, GFP_KERNEL);
        unittest(new && propcmp(&p2, new), "non-empty property didn't copy correctly\n");
-       kfree(new->value);
-       kfree(new->name);
-       kfree(new);
+       __of_prop_free(new);
 #endif
 }
 
@@ -3718,9 +3714,7 @@ static __init void of_unittest_overlay_high_level(void)
                                goto err_unlock;
                        }
                        if (__of_add_property(of_symbols, new_prop)) {
-                               kfree(new_prop->name);
-                               kfree(new_prop->value);
-                               kfree(new_prop);
+                               __of_prop_free(new_prop);
                                /* "name" auto-generated by unflatten */
                                if (!strcmp(prop->name, "name"))
                                        continue;