Skip to contentSkip to conversation

cache: sifive_ccache: skip disabled device tree nodes

PATCH Superseded Avi Weiss 2 participants 6 messages Oct 28 – Nov 17

Subsystems (3)
lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 cache: sifive_ccache: skip disabled device tree nodes

sifive_ccache_init() uses of_find_matching_node_and_match(), which also returns nodes marked as disabled. The initializer can therefore map and access a cache controller that the device tree marks unavailable.

Select the first available matching node before accessing its registers. This also handles a disabled node preceding an enabled one.

Fixes a967a289f169 ("RISC-V: sifive_l2_cache: Add L2 cache controller driver for SiFive SoCs")
---
 drivers/cache/sifive_ccache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cache/sifive_ccache.c +3 -1
index 71d3f94987fa..02878d54381d 100644
@@ -297,7 +297,9 @@ static int __init sifive_ccache_init(void)
 	unsigned long quirks __maybe_unused;
 	int rc;
 
-	np = of_find_matching_node_and_match(NULL, sifive_ccache_ids, &match);
+	for_each_matching_node_and_match(np, sifive_ccache_ids, &match)
+		if (of_device_is_available(np))
+			break;
 	if (!np)
 		return -ENODEV;
 

base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 18 lines · Avi Weiss

sifive_ccache_init() uses of_find_matching_node_and_match(), which also returns nodes marked as disabled. The initializer can therefore map and access a cache controller that the device tree marks unavailable.

Select the first available matching node before accessing its registers. This also handles a disabled node preceding an enabled one.

Fixes a967a289f169 ("RISC-V: sifive_l2_cache: Add L2 cache controller driver for SiFive SoCs")
---
 drivers/cache/sifive_ccache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cache/sifive_ccache.c +0 -0
index 71d3f94987fa..02878d54381d 100644

drivers/cache/sifive_ccache.c static int __init sifive_ccache_init(void) · line 301

Quoted 6 lines
drivers/cache/sifive_ccache.c +2 -1
@@ -297,7 +297,9 @@ static int __init sifive_ccache_init(void)
 	unsigned long quirks __maybe_unused;
 	int rc;
 
-	np = of_find_matching_node_and_match(NULL, sifive_ccache_ids, &match);
+	for_each_matching_node_and_match(np, sifive_ccache_ids, &match)
+		if (of_device_is_available(np))

Are there any devices where this is actually possible? IOW, where there are 2 ccache instances.

Quoted 9 lines
drivers/cache/sifive_ccache.c +1 -0
+			break;
 	if (!np)
 		return -ENODEV;
 

base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d

Signature · 1 line
2.43.0
author
#2 lore raw

It's more of a defensive thing. The 2 ccache instance was an example - the issue is also relevant for one disabled ccache node no? Since of_find_matching_node_and_match() doesn't check whether a node is enabled, it can return a disabled one.

sifive_ccache_init() then goes ahead and maps and accesses its registers, even though the device tree marks it as disabled and normal OF platform-device creation would skip it.

Quoted 39 lines · Conor Dooley
Quoted 18 lines · Avi Weiss

sifive_ccache_init() uses of_find_matching_node_and_match(), which also returns nodes marked as disabled. The initializer can therefore map and access a cache controller that the device tree marks unavailable.

Select the first available matching node before accessing its registers. This also handles a disabled node preceding an enabled one.

Fixes a967a289f169 ("RISC-V: sifive_l2_cache: Add L2 cache controller driver for SiFive SoCs")
---
 drivers/cache/sifive_ccache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cache/sifive_ccache.c +0 -0
index 71d3f94987fa..02878d54381d 100644

drivers/cache/sifive_ccache.c static int __init sifive_ccache_init(void) · line 301

Quoted 6 lines
drivers/cache/sifive_ccache.c +2 -1
@@ -297,7 +297,9 @@ static int __init sifive_ccache_init(void)
      unsigned long quirks __maybe_unused;
      int rc;
 
-     np = of_find_matching_node_and_match(NULL, sifive_ccache_ids, &match);
+     for_each_matching_node_and_match(np, sifive_ccache_ids, &match)
+             if (of_device_is_available(np))

Are there any devices where this is actually possible? IOW, where there are 2 ccache instances.

Quoted 9 lines
drivers/cache/sifive_ccache.c +1 -0
+                     break;
      if (!np)
              return -ENODEV;
 

base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d --
2.43.0

maintainer
#3 lore raw
Quoted 31 lines · Avi
Quoted 23 lines · Conor Dooley
Quoted 21 lines · Avi Weiss

sifive_ccache_init() uses of_find_matching_node_and_match(), which also returns nodes marked as disabled. The initializer can therefore map and access a cache controller that the device tree marks unavailable.

Select the first available matching node before accessing its registers. This also handles a disabled node preceding an enabled one.

Fixes a967a289f169 ("RISC-V: sifive_l2_cache: Add L2 cache controller driver for SiFive SoCs")
---
 drivers/cache/sifive_ccache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cache/sifive_ccache.c +0 -0
index 71d3f94987fa..02878d54381d 100644
@@ -297,7 +297,9 @@ static int __init sifive_ccache_init(void)
      unsigned long quirks __maybe_unused;
      int rc;
 
Quoted 6 lines

drivers/cache/sifive_ccache.c static int __init sifive_ccache_init(void) · line 301

Quoted 3 lines
drivers/cache/sifive_ccache.c +2 -1
-     np = of_find_matching_node_and_match(NULL, sifive_ccache_ids, &match);
+     for_each_matching_node_and_match(np, sifive_ccache_ids, &match)
+             if (of_device_is_available(np))

Are there any devices where this is actually possible? IOW, where there are 2 ccache instances.

Quoted 2 lines

It's more of a defensive thing. The 2 ccache instance was an example - the issue is also relevant for one disabled ccache node no?

That is true.
And on the two node front, the driver doesn't support that anyway either before or after the patch. Consider what the new code does if there are two nodes, both enabled.

But I don't think the ccache or llc-cache successor are intended for that kind of situation anyway.

Quoted 6 lines

Since of_find_matching_node_and_match() doesn't check whether a node is enabled, it can return a disabled one.

sifive_ccache_init() then goes ahead and maps and accesses its registers, even though the device tree marks it as disabled and normal OF platform-device creation would skip it.

Could we instead drop the loop and just check for availability, while retaining of_find_matching_node_and_match()? I kinda feel like the loop gives the impression that the driver supports multiple nodes, when it realistically does not.

Cheers,
Conor.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/cache/sifive_ccache.c +1 -0
+                     break;
      if (!np)
              return -ENODEV;
 

base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d --
2.43.0

author
#4 lore raw

Yes your suggestion makes sense Conor.
I'll quickly check it and if I get it validated send a v2 shortly. Cheers

Quoted 70 lines · Conor Dooley
Quoted 31 lines · Avi
Quoted 23 lines · Conor Dooley
Quoted 21 lines · Avi Weiss

sifive_ccache_init() uses of_find_matching_node_and_match(), which also returns nodes marked as disabled. The initializer can therefore map and access a cache controller that the device tree marks unavailable.

Select the first available matching node before accessing its registers. This also handles a disabled node preceding an enabled one.

Fixes a967a289f169 ("RISC-V: sifive_l2_cache: Add L2 cache controller driver for SiFive SoCs")
---
 drivers/cache/sifive_ccache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cache/sifive_ccache.c +0 -0
index 71d3f94987fa..02878d54381d 100644
@@ -297,7 +297,9 @@ static int __init sifive_ccache_init(void)
      unsigned long quirks __maybe_unused;
      int rc;
 
Quoted 6 lines

drivers/cache/sifive_ccache.c static int __init sifive_ccache_init(void) · line 301

Quoted 3 lines
drivers/cache/sifive_ccache.c +2 -1
-     np = of_find_matching_node_and_match(NULL, sifive_ccache_ids, &match);
+     for_each_matching_node_and_match(np, sifive_ccache_ids, &match)
+             if (of_device_is_available(np))

Are there any devices where this is actually possible? IOW, where there are 2 ccache instances.

Quoted 2 lines

It's more of a defensive thing. The 2 ccache instance was an example - the issue is also relevant for one disabled ccache node no?

That is true.
And on the two node front, the driver doesn't support that anyway either before or after the patch. Consider what the new code does if there are two nodes, both enabled.

But I don't think the ccache or llc-cache successor are intended for that kind of situation anyway.

Quoted 6 lines

Since of_find_matching_node_and_match() doesn't check whether a node is enabled, it can return a disabled one.

sifive_ccache_init() then goes ahead and maps and accesses its registers, even though the device tree marks it as disabled and normal OF platform-device creation would skip it.

Could we instead drop the loop and just check for availability, while retaining of_find_matching_node_and_match()? I kinda feel like the loop gives the impression that the driver supports multiple nodes, when it realistically does not.

Cheers,
Conor.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
drivers/cache/sifive_ccache.c +1 -0
+                     break;
      if (!np)
              return -ENODEV;
 

base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d --
2.43.0

author
#5 lore raw

PATCH v2 1/1 cache: sifive_ccache: skip disabled device tree nodes

In reply to Avi Weiss

sifive_ccache_init() uses of_find_matching_node_and_match(), which may return a device tree node marked disabled. The driver can then map and access registers of a cache controller that the device tree marks unavailable.

Check the selected node's availability before accessing its registers. Return -ENODEV for a disabled node, releasing the reference through the existing error path.

Fixes a967a289f169 ("RISC-V: sifive_l2_cache: Add L2 cache controller driver for SiFive SoCs")

---
v2:
- retain the single-node lookup instead of iterating over matches - as suggested by Conor Dooley
- use the existing error path to release a disabled node

Link to v1: https://lore.kernel.org/r/[email protected]

 drivers/cache/sifive_ccache.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/cache/sifive_ccache.c +5 -0
@@ -301,6 +301,11 @@ static int __init sifive_ccache_init(void)
 	if (!np)
 		return -ENODEV;
 
+	if (!of_device_is_available(np)) {
+		rc = -ENODEV;
+		goto err_node_put;
+	}
+
 	quirks = (uintptr_t)match->data;
 
 	if (of_address_to_resource(np, 0, &res)) {
Signature · 1 line
2.47.3