Skip to content

Commit 40b0f17

Browse files
committed
of: Use scope based kfree() cleanups
Use the relatively new scope based kfree() cleanup to simplify error handling. Doing so reduces the chances of memory leaks and simplifies error paths by avoiding the need for goto statements. 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 1c5e3d9 commit 40b0f17

File tree

3 files changed

+25
-55
lines changed

3 files changed

+25
-55
lines changed

drivers/of/base.c

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#define pr_fmt(fmt) "OF: " fmt
1818

19+
#include <linux/cleanup.h>
1920
#include <linux/console.h>
2021
#include <linux/ctype.h>
2122
#include <linux/cpu.h>
@@ -1393,8 +1394,10 @@ int of_parse_phandle_with_args_map(const struct device_node *np,
13931394
const char *stem_name,
13941395
int index, struct of_phandle_args *out_args)
13951396
{
1396-
char *cells_name, *map_name = NULL, *mask_name = NULL;
1397-
char *pass_name = NULL;
1397+
char *cells_name __free(kfree) = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1398+
char *map_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1399+
char *mask_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1400+
char *pass_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
13981401
struct device_node *cur, *new = NULL;
13991402
const __be32 *map, *mask, *pass;
14001403
static const __be32 dummy_mask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
@@ -1407,27 +1410,13 @@ int of_parse_phandle_with_args_map(const struct device_node *np,
14071410
if (index < 0)
14081411
return -EINVAL;
14091412

1410-
cells_name = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
1411-
if (!cells_name)
1413+
if (!cells_name || !map_name || !mask_name || !pass_name)
14121414
return -ENOMEM;
14131415

1414-
ret = -ENOMEM;
1415-
map_name = kasprintf(GFP_KERNEL, "%s-map", stem_name);
1416-
if (!map_name)
1417-
goto free;
1418-
1419-
mask_name = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
1420-
if (!mask_name)
1421-
goto free;
1422-
1423-
pass_name = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
1424-
if (!pass_name)
1425-
goto free;
1426-
14271416
ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index,
14281417
out_args);
14291418
if (ret)
1430-
goto free;
1419+
return ret;
14311420

14321421
/* Get the #<list>-cells property */
14331422
cur = out_args->np;
@@ -1444,8 +1433,7 @@ int of_parse_phandle_with_args_map(const struct device_node *np,
14441433
/* Get the <list>-map property */
14451434
map = of_get_property(cur, map_name, &map_len);
14461435
if (!map) {
1447-
ret = 0;
1448-
goto free;
1436+
return 0;
14491437
}
14501438
map_len /= sizeof(u32);
14511439

@@ -1521,12 +1509,6 @@ int of_parse_phandle_with_args_map(const struct device_node *np,
15211509
put:
15221510
of_node_put(cur);
15231511
of_node_put(new);
1524-
free:
1525-
kfree(mask_name);
1526-
kfree(map_name);
1527-
kfree(cells_name);
1528-
kfree(pass_name);
1529-
15301512
return ret;
15311513
}
15321514
EXPORT_SYMBOL(of_parse_phandle_with_args_map);

drivers/of/dynamic.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#define pr_fmt(fmt) "OF: " fmt
1111

12+
#include <linux/cleanup.h>
1213
#include <linux/of.h>
1314
#include <linux/spinlock.h>
1415
#include <linux/slab.h>
@@ -1019,10 +1020,9 @@ int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
10191020
const u32 *array, size_t sz)
10201021
{
10211022
struct property prop;
1022-
__be32 *val;
1023-
int i, ret;
1023+
__be32 *val __free(kfree) = kcalloc(sz, sizeof(__be32), GFP_KERNEL);
1024+
int i;
10241025

1025-
val = kcalloc(sz, sizeof(__be32), GFP_KERNEL);
10261026
if (!val)
10271027
return -ENOMEM;
10281028

@@ -1032,9 +1032,6 @@ int of_changeset_add_prop_u32_array(struct of_changeset *ocs,
10321032
prop.length = sizeof(u32) * sz;
10331033
prop.value = (void *)val;
10341034

1035-
ret = of_changeset_add_prop_helper(ocs, np, &prop);
1036-
kfree(val);
1037-
1038-
return ret;
1035+
return of_changeset_add_prop_helper(ocs, np, &prop);
10391036
}
10401037
EXPORT_SYMBOL_GPL(of_changeset_add_prop_u32_array);

drivers/of/resolver.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#define pr_fmt(fmt) "OF: resolver: " fmt
1010

11+
#include <linux/cleanup.h>
1112
#include <linux/kernel.h>
1213
#include <linux/module.h>
1314
#include <linux/of.h>
@@ -74,11 +75,11 @@ static int update_usages_of_a_phandle_reference(struct device_node *overlay,
7475
{
7576
struct device_node *refnode;
7677
struct property *prop;
77-
char *value, *cur, *end, *node_path, *prop_name, *s;
78+
char *value __free(kfree) = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
79+
char *cur, *end, *node_path, *prop_name, *s;
7880
int offset, len;
7981
int err = 0;
8082

81-
value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
8283
if (!value)
8384
return -ENOMEM;
8485

@@ -89,23 +90,19 @@ static int update_usages_of_a_phandle_reference(struct device_node *overlay,
8990

9091
node_path = cur;
9192
s = strchr(cur, ':');
92-
if (!s) {
93-
err = -EINVAL;
94-
goto err_fail;
95-
}
93+
if (!s)
94+
return -EINVAL;
9695
*s++ = '\0';
9796

9897
prop_name = s;
9998
s = strchr(s, ':');
100-
if (!s) {
101-
err = -EINVAL;
102-
goto err_fail;
103-
}
99+
if (!s)
100+
return -EINVAL;
104101
*s++ = '\0';
105102

106103
err = kstrtoint(s, 10, &offset);
107104
if (err)
108-
goto err_fail;
105+
return err;
109106

110107
refnode = __of_find_node_by_full_path(of_node_get(overlay), node_path);
111108
if (!refnode)
@@ -117,22 +114,16 @@ static int update_usages_of_a_phandle_reference(struct device_node *overlay,
117114
}
118115
of_node_put(refnode);
119116

120-
if (!prop) {
121-
err = -ENOENT;
122-
goto err_fail;
123-
}
117+
if (!prop)
118+
return -ENOENT;
124119

125-
if (offset < 0 || offset + sizeof(__be32) > prop->length) {
126-
err = -EINVAL;
127-
goto err_fail;
128-
}
120+
if (offset < 0 || offset + sizeof(__be32) > prop->length)
121+
return -EINVAL;
129122

130123
*(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
131124
}
132125

133-
err_fail:
134-
kfree(value);
135-
return err;
126+
return 0;
136127
}
137128

138129
/* compare nodes taking into account that 'name' strips out the @ part */

0 commit comments

Comments
 (0)