Skip to contentSkip to conversation

of: fix incorrect device creation for reserved memory nodes

PATCH Superseded Kenny Cheng 2 participants 4 messages Feb 22 – Feb 27 Device tree

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 of: fix incorrect device creation for reserved memory nodes

The current global search for nodes in reserved_mem_matches can find nodes outside "/reserved-memory". These nodes might not have actual memory reserved (via memblock), leading to drivers (e.g., ramoops) accessing unreserved memory and causing memory corruption.

Prioritize scanning children of "/reserved-memory" to ensure the device is backed by reserved memory. A fallback is kept for compatibility. Proper reference counting is also added.

---
 drivers/of/platform.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/of/platform.c +11 -2
index ba591fbceb56..785ffef48c08 100644
@@ -563,8 +563,17 @@ static int __init of_platform_default_populate_init(void)
 		 * platform_devices for every node in /reserved-memory with a
 		 * "compatible",
 		 */
-		for_each_matching_node(node, reserved_mem_matches)
-			of_platform_device_create(node, NULL, NULL);
+		reserved = of_find_node_by_path("/reserved-memory");
+		if (reserved) {
+			for_each_child_of_node(reserved, node) {
+				if (of_match_node(reserved_mem_matches, node))
+					of_platform_device_create(node, NULL, NULL);
+			}
+			of_node_put(reserved);
+		} else {
+			for_each_matching_node(node, reserved_mem_matches)
+				of_platform_device_create(node, NULL, NULL);
+		}
 
 		node = of_find_node_by_path("/firmware");
 		if (node) {
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/1 of: fix incorrect device creation for reserved memory nodes

The current global search for nodes in reserved_mem_matches can find nodes outside "/reserved-memory". These nodes might not have actual memory reserved (via memblock), leading to drivers (e.g., ramoops) accessing unreserved memory and causing memory corruption.

Prioritize scanning children of "/reserved-memory" to ensure the device is backed by reserved memory. A fallback is kept for compatibility. Proper reference counting is also added.

---
v2:
- Fix missing variable declaration for 'reserved'.

---
 drivers/of/platform.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/of/platform.c +12 -3
index ba591fbceb56..68bba234cbea 100644
@@ -500,7 +500,7 @@ static const struct of_device_id reserved_mem_matches[] = {
 
 static int __init of_platform_default_populate_init(void)
 {
-	struct device_node *node;
+	struct device_node *node, *reserved;
 
 	device_links_supplier_sync_state_pause();
 
@@ -563,8 +563,17 @@ static int __init of_platform_default_populate_init(void)
 		 * platform_devices for every node in /reserved-memory with a
 		 * "compatible",
 		 */
-		for_each_matching_node(node, reserved_mem_matches)
-			of_platform_device_create(node, NULL, NULL);
+		reserved = of_find_node_by_path("/reserved-memory");
+		if (reserved) {
+			for_each_child_of_node(reserved, node) {
+				if (of_match_node(reserved_mem_matches, node))
+					of_platform_device_create(node, NULL, NULL);
+			}
+			of_node_put(reserved);
+		} else {
+			for_each_matching_node(node, reserved_mem_matches)
+				of_platform_device_create(node, NULL, NULL);
+		}
 
 		node = of_find_node_by_path("/firmware");
 		if (node) {
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 1/1 of: fix incorrect device creation for reserved memory nodes

The current global search for nodes in reserved_mem_matches can find nodes outside "/reserved-memory". These nodes might not have actual memory reserved (via memblock), leading to drivers (e.g., ramoops) accessing unreserved memory and causing memory corruption.

Restrict the scan to the "/reserved-memory" node to ensure created devices are correctly backed by reserved memory. This enforces specification compliance and avoids dangerous probing.

---
v3:
- Remove global search fallback to prevent memory corruption.
- Fix logic to only allow nodes within "/reserved-memory".
v2:
- Fix missing variable declaration for 'reserved'.

---
 drivers/of/platform.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/of/platform.c +9 -3
index ba591fbceb56..2a7111e8354d 100644
@@ -500,7 +500,7 @@ static const struct of_device_id reserved_mem_matches[] = {
 
 static int __init of_platform_default_populate_init(void)
 {
-	struct device_node *node;
+	struct device_node *node, *reserved;
 
 	device_links_supplier_sync_state_pause();
 
@@ -563,8 +563,14 @@ static int __init of_platform_default_populate_init(void)
 		 * platform_devices for every node in /reserved-memory with a
 		 * "compatible",
 		 */
-		for_each_matching_node(node, reserved_mem_matches)
-			of_platform_device_create(node, NULL, NULL);
+		reserved = of_find_node_by_path("/reserved-memory");
+		if (reserved) {
+			for_each_child_of_node(reserved, node) {
+				if (of_match_node(reserved_mem_matches, node))
+					of_platform_device_create(node, NULL, NULL);
+			}
+			of_node_put(reserved);
+		}
 
 		node = of_find_node_by_path("/firmware");
 		if (node) {
Signature · 1 line
2.34.1
maintainer Applied
#3 lore raw
Quoted 16 lines · Kenny Cheng

The current global search for nodes in reserved_mem_matches can find nodes outside "/reserved-memory". These nodes might not have actual memory reserved (via memblock), leading to drivers (e.g., ramoops) accessing unreserved memory and causing memory corruption.

Restrict the scan to the "/reserved-memory" node to ensure created devices are correctly backed by reserved memory. This enforces specification compliance and avoids dangerous probing.

---
v3:
- Remove global search fallback to prevent memory corruption.
- Fix logic to only allow nodes within "/reserved-memory".
v2:
- Fix missing variable declaration for 'reserved'.

Quoted 4 lines
---
 drivers/of/platform.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

Applied, thanks!