mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2025-01-16 02:14:58 +00:00
drivers: arch_topology: 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(). The declaration of the device_node used within the do-while loops is moved directly within the loop so that the resource is automatically freed at the end of each iteration. Acked-by: Sudeep Holla <sudeep.holla@arm.com> Suggested-by: Julia Lawall <julia.lawall@inria.fr> Signed-off-by: Vincenzo Mezzela <vincenzo.mezzela@gmail.com> Link: https://lore.kernel.org/r/20240607163350.392971-3-vincenzo.mezzela@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
97b1974547
commit
880f5f58fd
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include <linux/acpi.h>
|
#include <linux/acpi.h>
|
||||||
#include <linux/cacheinfo.h>
|
#include <linux/cacheinfo.h>
|
||||||
|
#include <linux/cleanup.h>
|
||||||
#include <linux/cpu.h>
|
#include <linux/cpu.h>
|
||||||
#include <linux/cpufreq.h>
|
#include <linux/cpufreq.h>
|
||||||
#include <linux/device.h>
|
#include <linux/device.h>
|
||||||
@ -513,10 +514,10 @@ core_initcall(free_raw_capacity);
|
|||||||
*/
|
*/
|
||||||
static int __init get_cpu_for_node(struct device_node *node)
|
static int __init get_cpu_for_node(struct device_node *node)
|
||||||
{
|
{
|
||||||
struct device_node *cpu_node;
|
|
||||||
int cpu;
|
int cpu;
|
||||||
|
struct device_node *cpu_node __free(device_node) =
|
||||||
|
of_parse_phandle(node, "cpu", 0);
|
||||||
|
|
||||||
cpu_node = of_parse_phandle(node, "cpu", 0);
|
|
||||||
if (!cpu_node)
|
if (!cpu_node)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@ -527,7 +528,6 @@ static int __init get_cpu_for_node(struct device_node *node)
|
|||||||
pr_info("CPU node for %pOF exist but the possible cpu range is :%*pbl\n",
|
pr_info("CPU node for %pOF exist but the possible cpu range is :%*pbl\n",
|
||||||
cpu_node, cpumask_pr_args(cpu_possible_mask));
|
cpu_node, cpumask_pr_args(cpu_possible_mask));
|
||||||
|
|
||||||
of_node_put(cpu_node);
|
|
||||||
return cpu;
|
return cpu;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -538,11 +538,12 @@ static int __init parse_core(struct device_node *core, int package_id,
|
|||||||
bool leaf = true;
|
bool leaf = true;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int cpu;
|
int cpu;
|
||||||
struct device_node *t;
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
snprintf(name, sizeof(name), "thread%d", i);
|
snprintf(name, sizeof(name), "thread%d", i);
|
||||||
t = of_get_child_by_name(core, name);
|
struct device_node *t __free(device_node) =
|
||||||
|
of_get_child_by_name(core, name);
|
||||||
|
|
||||||
if (!t)
|
if (!t)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -555,10 +556,8 @@ static int __init parse_core(struct device_node *core, int package_id,
|
|||||||
cpu_topology[cpu].thread_id = i;
|
cpu_topology[cpu].thread_id = i;
|
||||||
} else if (cpu != -ENODEV) {
|
} else if (cpu != -ENODEV) {
|
||||||
pr_err("%pOF: Can't get CPU for thread\n", t);
|
pr_err("%pOF: Can't get CPU for thread\n", t);
|
||||||
of_node_put(t);
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
of_node_put(t);
|
|
||||||
i++;
|
i++;
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
@ -587,7 +586,6 @@ static int __init parse_cluster(struct device_node *cluster, int package_id,
|
|||||||
char name[20];
|
char name[20];
|
||||||
bool leaf = true;
|
bool leaf = true;
|
||||||
bool has_cores = false;
|
bool has_cores = false;
|
||||||
struct device_node *c;
|
|
||||||
int core_id = 0;
|
int core_id = 0;
|
||||||
int i, ret;
|
int i, ret;
|
||||||
|
|
||||||
@ -599,7 +597,9 @@ static int __init parse_cluster(struct device_node *cluster, int package_id,
|
|||||||
i = 0;
|
i = 0;
|
||||||
do {
|
do {
|
||||||
snprintf(name, sizeof(name), "cluster%d", i);
|
snprintf(name, sizeof(name), "cluster%d", i);
|
||||||
c = of_get_child_by_name(cluster, name);
|
struct device_node *c __free(device_node) =
|
||||||
|
of_get_child_by_name(cluster, name);
|
||||||
|
|
||||||
if (!c)
|
if (!c)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -607,7 +607,6 @@ static int __init parse_cluster(struct device_node *cluster, int package_id,
|
|||||||
ret = parse_cluster(c, package_id, i, depth + 1);
|
ret = parse_cluster(c, package_id, i, depth + 1);
|
||||||
if (depth > 0)
|
if (depth > 0)
|
||||||
pr_warn("Topology for clusters of clusters not yet supported\n");
|
pr_warn("Topology for clusters of clusters not yet supported\n");
|
||||||
of_node_put(c);
|
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
return ret;
|
||||||
i++;
|
i++;
|
||||||
@ -617,7 +616,9 @@ static int __init parse_cluster(struct device_node *cluster, int package_id,
|
|||||||
i = 0;
|
i = 0;
|
||||||
do {
|
do {
|
||||||
snprintf(name, sizeof(name), "core%d", i);
|
snprintf(name, sizeof(name), "core%d", i);
|
||||||
c = of_get_child_by_name(cluster, name);
|
struct device_node *c __free(device_node) =
|
||||||
|
of_get_child_by_name(cluster, name);
|
||||||
|
|
||||||
if (!c)
|
if (!c)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -625,21 +626,19 @@ static int __init parse_cluster(struct device_node *cluster, int package_id,
|
|||||||
|
|
||||||
if (depth == 0) {
|
if (depth == 0) {
|
||||||
pr_err("%pOF: cpu-map children should be clusters\n", c);
|
pr_err("%pOF: cpu-map children should be clusters\n", c);
|
||||||
of_node_put(c);
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leaf) {
|
if (leaf) {
|
||||||
ret = parse_core(c, package_id, cluster_id, core_id++);
|
ret = parse_core(c, package_id, cluster_id, core_id++);
|
||||||
|
if (ret != 0)
|
||||||
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
pr_err("%pOF: Non-leaf cluster with core %s\n",
|
pr_err("%pOF: Non-leaf cluster with core %s\n",
|
||||||
cluster, name);
|
cluster, name);
|
||||||
ret = -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
of_node_put(c);
|
|
||||||
if (ret != 0)
|
|
||||||
return ret;
|
|
||||||
i++;
|
i++;
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
@ -652,19 +651,19 @@ static int __init parse_cluster(struct device_node *cluster, int package_id,
|
|||||||
static int __init parse_socket(struct device_node *socket)
|
static int __init parse_socket(struct device_node *socket)
|
||||||
{
|
{
|
||||||
char name[20];
|
char name[20];
|
||||||
struct device_node *c;
|
|
||||||
bool has_socket = false;
|
bool has_socket = false;
|
||||||
int package_id = 0, ret;
|
int package_id = 0, ret;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
snprintf(name, sizeof(name), "socket%d", package_id);
|
snprintf(name, sizeof(name), "socket%d", package_id);
|
||||||
c = of_get_child_by_name(socket, name);
|
struct device_node *c __free(device_node) =
|
||||||
|
of_get_child_by_name(socket, name);
|
||||||
|
|
||||||
if (!c)
|
if (!c)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
has_socket = true;
|
has_socket = true;
|
||||||
ret = parse_cluster(c, package_id, -1, 0);
|
ret = parse_cluster(c, package_id, -1, 0);
|
||||||
of_node_put(c);
|
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
@ -679,11 +678,11 @@ static int __init parse_socket(struct device_node *socket)
|
|||||||
|
|
||||||
static int __init parse_dt_topology(void)
|
static int __init parse_dt_topology(void)
|
||||||
{
|
{
|
||||||
struct device_node *cn, *map;
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
int cpu;
|
int cpu;
|
||||||
|
struct device_node *cn __free(device_node) =
|
||||||
|
of_find_node_by_path("/cpus");
|
||||||
|
|
||||||
cn = of_find_node_by_path("/cpus");
|
|
||||||
if (!cn) {
|
if (!cn) {
|
||||||
pr_err("No CPU information found in DT\n");
|
pr_err("No CPU information found in DT\n");
|
||||||
return 0;
|
return 0;
|
||||||
@ -693,13 +692,15 @@ static int __init parse_dt_topology(void)
|
|||||||
* When topology is provided cpu-map is essentially a root
|
* When topology is provided cpu-map is essentially a root
|
||||||
* cluster with restricted subnodes.
|
* cluster with restricted subnodes.
|
||||||
*/
|
*/
|
||||||
map = of_get_child_by_name(cn, "cpu-map");
|
struct device_node *map __free(device_node) =
|
||||||
|
of_get_child_by_name(cn, "cpu-map");
|
||||||
|
|
||||||
if (!map)
|
if (!map)
|
||||||
goto out;
|
return ret;
|
||||||
|
|
||||||
ret = parse_socket(map);
|
ret = parse_socket(map);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
goto out_map;
|
return ret;
|
||||||
|
|
||||||
topology_normalize_cpu_scale();
|
topology_normalize_cpu_scale();
|
||||||
|
|
||||||
@ -709,14 +710,9 @@ static int __init parse_dt_topology(void)
|
|||||||
*/
|
*/
|
||||||
for_each_possible_cpu(cpu)
|
for_each_possible_cpu(cpu)
|
||||||
if (cpu_topology[cpu].package_id < 0) {
|
if (cpu_topology[cpu].package_id < 0) {
|
||||||
ret = -EINVAL;
|
return -EINVAL;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out_map:
|
|
||||||
of_node_put(map);
|
|
||||||
out:
|
|
||||||
of_node_put(cn);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user