Skip to content

Commit 1b48fbb

Browse files
vmezzelagregkh
authored andcommitted
drivers: cacheinfo: use __free attribute instead of of_node_put()
Introduce the __free attribute for scope-based resource management. Resources allocated with __free are automatically released at the end of the scope. This enhancement aims to mitigate memory management issues associated with forgetting to release resources by utilizing __free instead of of_node_put(). To introduce this feature, some modifications to the code structure were necessary. The original pattern: ``` prev = np; while(...) { [...] np = of_find_next_cache_node(np); of_node_put(prev); prev = np; [...] } ``` has been updated to: ``` while(...) { [...] struct device_node __free(device_node) *prev = np; np = of_find_next_cache_node(np) [...] } ``` With this change, the previous node is automatically cleaned up at the end of each iteration, allowing the elimination of all of_node_put() calls and some goto statements. Suggested-by: Julia Lawall <[email protected]> Signed-off-by: Vincenzo Mezzela <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 30b968b commit 1b48fbb

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

drivers/base/cacheinfo.c

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -202,29 +202,24 @@ static void cache_of_set_props(struct cacheinfo *this_leaf,
202202

203203
static int cache_setup_of_node(unsigned int cpu)
204204
{
205-
struct device_node *np, *prev;
206205
struct cacheinfo *this_leaf;
207206
unsigned int index = 0;
208207

209-
np = of_cpu_device_node_get(cpu);
208+
struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
210209
if (!np) {
211210
pr_err("Failed to find cpu%d device node\n", cpu);
212211
return -ENOENT;
213212
}
214213

215214
if (!of_check_cache_nodes(np)) {
216-
of_node_put(np);
217215
return -ENOENT;
218216
}
219217

220-
prev = np;
221-
222218
while (index < cache_leaves(cpu)) {
223219
this_leaf = per_cpu_cacheinfo_idx(cpu, index);
224220
if (this_leaf->level != 1) {
221+
struct device_node *prev __free(device_node) = np;
225222
np = of_find_next_cache_node(np);
226-
of_node_put(prev);
227-
prev = np;
228223
if (!np)
229224
break;
230225
}
@@ -233,8 +228,6 @@ static int cache_setup_of_node(unsigned int cpu)
233228
index++;
234229
}
235230

236-
of_node_put(np);
237-
238231
if (index != cache_leaves(cpu)) /* not all OF nodes populated */
239232
return -ENOENT;
240233

@@ -243,17 +236,14 @@ static int cache_setup_of_node(unsigned int cpu)
243236

244237
static bool of_check_cache_nodes(struct device_node *np)
245238
{
246-
struct device_node *next;
247-
248239
if (of_property_present(np, "cache-size") ||
249240
of_property_present(np, "i-cache-size") ||
250241
of_property_present(np, "d-cache-size") ||
251242
of_property_present(np, "cache-unified"))
252243
return true;
253244

254-
next = of_find_next_cache_node(np);
245+
struct device_node *next __free(device_node) = of_find_next_cache_node(np);
255246
if (next) {
256-
of_node_put(next);
257247
return true;
258248
}
259249

@@ -287,43 +277,38 @@ static int of_count_cache_leaves(struct device_node *np)
287277
int init_of_cache_level(unsigned int cpu)
288278
{
289279
struct cpu_cacheinfo *this_cpu_ci = get_cpu_cacheinfo(cpu);
290-
struct device_node *np = of_cpu_device_node_get(cpu);
291-
struct device_node *prev = NULL;
280+
struct device_node *np __free(device_node) = of_cpu_device_node_get(cpu);
292281
unsigned int levels = 0, leaves, level;
293282

294283
if (!of_check_cache_nodes(np)) {
295-
of_node_put(np);
296284
return -ENOENT;
297285
}
298286

299287
leaves = of_count_cache_leaves(np);
300288
if (leaves > 0)
301289
levels = 1;
302290

303-
prev = np;
304-
while ((np = of_find_next_cache_node(np))) {
305-
of_node_put(prev);
306-
prev = np;
291+
while (1) {
292+
struct device_node *prev __free(device_node) = np;
293+
np = of_find_next_cache_node(np);
294+
if (!np)
295+
break;
296+
307297
if (!of_device_is_compatible(np, "cache"))
308-
goto err_out;
298+
return -EINVAL;
309299
if (of_property_read_u32(np, "cache-level", &level))
310-
goto err_out;
300+
return -EINVAL;
311301
if (level <= levels)
312-
goto err_out;
302+
return -EINVAL;
313303

314304
leaves += of_count_cache_leaves(np);
315305
levels = level;
316306
}
317307

318-
of_node_put(np);
319308
this_cpu_ci->num_levels = levels;
320309
this_cpu_ci->num_leaves = leaves;
321310

322311
return 0;
323-
324-
err_out:
325-
of_node_put(np);
326-
return -EINVAL;
327312
}
328313

329314
#else

0 commit comments

Comments
 (0)