Skip to contentSkip to conversation

powerpc: cell: cbe_regs: Fix refcount bugs

PATCH Applied Liang He 2 participants 3 messages Jul 1, 2022 – Sep 9, 2022

Subsystems (2)

v2 of 3 Applied Latest is v3 →

lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH v2 1/2 powerpc: cell: cbe_regs: Fix refcount bugs

There are several bugs as following:

(1) In cbe_get_be_node(), we should hold the reference returned by of_find_xxx and of_get_xxx OF APIs and use it to call of_node_put
(2) In cbe_fill_regs_map(), we should same as above
(3) In cbe_regs_init(), during the iteration of for_each_node_by_type(), the refcount of 'cpu' will be automatically increased and decreased. However, there is a reference escaped out into 'map->cpu_node' and we should properly handle it.

---
chagelog:

v2: (1) split v1's two files in to two commits
(2) merge all bugs for cbe_regs.c
(3) using 'check-then-put' coding style
v1: only detect bug (1)

v1-link: https://lore.kernel.org/all/[email protected]/

 arch/powerpc/platforms/cell/cbe_regs.c | 40 +++++++++++++++++++-------
 1 file changed, 30 insertions(+), 10 deletions(-)
arch/powerpc/platforms/cell/cbe_regs.c +30 -10
index 316e533afc00..3fd3634aa515 100644
@@ -182,9 +182,19 @@ static struct device_node *__init cbe_get_be_node(int cpu_id)
 		if (WARN_ON_ONCE(!cpu_handle))
 			return np;
 
-		for (i=0; i<len; i++)
-			if (of_find_node_by_phandle(cpu_handle[i]) == of_get_cpu_node(cpu_id, NULL))
+		for (i=0; i<len; i++) {
+			struct device_node *ch_np = of_find_node_by_phandle(cpu_handle[i]);
+			struct device_node *ci_np = of_get_cpu_node(cpu_id, NULL);
+
+			if (ch_np == ci_np) {
+				of_node_put(ch_np);
+				of_node_put(ci_np);
 				return np;
+			}
+
+			of_node_put(ch_np);
+			of_node_put(ci_np);
+		}
 	}
 
 	return NULL;
@@ -193,21 +203,30 @@ static struct device_node *__init cbe_get_be_node(int cpu_id)
 static void __init cbe_fill_regs_map(struct cbe_regs_map *map)
 {
 	if(map->be_node) {
-		struct device_node *be, *np;
+		struct device_node *be, *np, *parent_np;
 
 		be = map->be_node;
 
-		for_each_node_by_type(np, "pervasive")
-			if (of_get_parent(np) == be)
+		for_each_node_by_type(np, "pervasive") {
+			parent_np = of_get_parent(np);
+			if (parent_np == be)
 				map->pmd_regs = of_iomap(np, 0);
+			of_node_put(parent_np);
+		}
 
-		for_each_node_by_type(np, "CBEA-Internal-Interrupt-Controller")
-			if (of_get_parent(np) == be)
+		for_each_node_by_type(np, "CBEA-Internal-Interrupt-Controller") {
+			parent_np = of_get_parent(np);
+			if (parent_np == be)
 				map->iic_regs = of_iomap(np, 2);
+			of_node_put(parent_np);
+		}
 
-		for_each_node_by_type(np, "mic-tm")
-			if (of_get_parent(np) == be)
+		for_each_node_by_type(np, "mic-tm") {
+			parent_np = of_get_parent(np);
+			if (parent_np == be)
 				map->mic_tm_regs = of_iomap(np, 0);
+			of_node_put(parent_np);
+		}
 	} else {
 		struct device_node *cpu;
 		/* That hack must die die die ! */
@@ -261,7 +280,8 @@ void __init cbe_regs_init(void)
 			of_node_put(cpu);
 			return;
 		}
-		map->cpu_node = cpu;
+		of_node_put(map->cpu_node);
+		map->cpu_node = of_node_get(cpu);
 
 		for_each_possible_cpu(i) {
 			struct cbe_thread_map *thread = &cbe_thread_map[i];
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 2/2 powerpc: cell: iommu: Hold reference returned by of_find_node_by_name()

In cell_iommu_init_disabled(), we should hold the reference returned by of_find_node_by_name() and use it to call of_node_put() for reference balance.

---
changelog:

v2: (1) split v1's two files in to two commits
(2) using 'check-then-put' coding style
v1: hold the reference returned by of_find_xxx OF APIs

v1-link: https://lore.kernel.org/all/[email protected]/

 arch/powerpc/platforms/cell/iommu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/platforms/cell/iommu.c +3 -1
index 0ca3efeef293..8c7133039566 100644
@@ -720,8 +720,10 @@ static int __init cell_iommu_init_disabled(void)
 	cell_disable_iommus();
 
 	/* If we have no Axon, we set up the spider DMA magic offset */
-	if (of_find_node_by_name(NULL, "axon") == NULL)
+	np = of_find_node_by_name(NULL, "axon");
+	if (!np)
 		cell_dma_nommu_offset = SPIDER_DMA_OFFSET;
+	of_node_put(np);
 
 	/* Now we need to check to see where the memory is mapped
 	 * in PCI space. We assume that all busses use the same dma
Signature · 1 line
2.25.1
#2 lore raw
In reply to Liang He
Quoted 11 lines · Liang He

There are several bugs as following:

(1) In cbe_get_be_node(), we should hold the reference returned by of_find_xxx and of_get_xxx OF APIs and use it to call of_node_put
(2) In cbe_fill_regs_map(), we should same as above
(3) In cbe_regs_init(), during the iteration of for_each_node_by_type(), the refcount of 'cpu' will be automatically increased and decreased. However, there is a reference escaped out into 'map->cpu_node' and we should properly handle it.

[...]

Applied to powerpc/next.

[1/2] powerpc: cell: cbe_regs: Fix refcount bugs

      https://git.kernel.org/powerpc/c/ad4b323693abd221798a6479105d22c79605aa0f
[2/2] powerpc: cell: iommu: Hold reference returned by of_find_node_by_name()
      https://git.kernel.org/powerpc/c/f4f8320b01677b467c768c43c1e1d10383a0e30d

cheers