Skip to contentSkip to conversation

firmware: Hold a reference for of_find_compatible_node()

PATCH Superseded Liang He 2 participants 5 messages Jun 21, 2022 – Jun 27, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH 1/1 firmware: Hold a reference for of_find_compatible_node()

In of_register_trusted_foundations(), we need to hold the reference returned by of_find_compatible_node() and then use it to call of_node_put() for refcount balance.

---
 include/linux/firmware/trusted_foundations.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/firmware/trusted_foundations.h +6 -2
index be5984bda592..399471c2f1c7 100644
@@ -71,12 +71,16 @@ static inline void register_trusted_foundations(
 
 static inline void of_register_trusted_foundations(void)
 {
+	struct device_node *np = of_find_compatible_node(NULL, NULL, "tlm,trusted-foundations");
+
+	of_node_put(np);
+	if (!np)
+		return;
 	/*
 	 * If we find the target should enable TF but does not support it,
 	 * fail as the system won't be able to do much anyway
 	 */
-	if (of_find_compatible_node(NULL, NULL, "tlm,trusted-foundations"))
-		register_trusted_foundations(NULL);
+	register_trusted_foundations(NULL);
 }
 
 static inline bool trusted_foundations_registered(void)
Signature · 1 line
2.25.1
#1 lore raw
Quoted 14 lines · Liang He

In of_register_trusted_foundations(), we need to hold the reference returned by of_find_compatible_node() and then use it to call of_node_put() for refcount balance.

---
 include/linux/firmware/trusted_foundations.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/firmware/trusted_foundations.h +0 -0
index be5984bda592..399471c2f1c7 100644

include/linux/firmware/trusted_foundations.h static inline void register_trusted_foundations( · line 77

Quoted 7 lines
include/linux/firmware/trusted_foundations.h +4 -0
@@ -71,12 +71,16 @@ static inline void register_trusted_foundations(
 
 static inline void of_register_trusted_foundations(void)
 {
+	struct device_node *np = of_find_compatible_node(NULL, NULL, "tlm,trusted-foundations");
+
+	of_node_put(np);
+	if (!np)

While this is technically correct, you are now checking to see if this points to a memory location that you no longer know what it really belongs to. C will let you do this, but it might be nicer to fix it up properly so it doesn't look like this.

thanks,

greg k-h

author
#2 lore raw
Quoted 28 lines · Greg KH
Quoted 14 lines · Liang He

In of_register_trusted_foundations(), we need to hold the reference returned by of_find_compatible_node() and then use it to call of_node_put() for refcount balance.

---
 include/linux/firmware/trusted_foundations.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/firmware/trusted_foundations.h +0 -0
index be5984bda592..399471c2f1c7 100644

include/linux/firmware/trusted_foundations.h static inline void register_trusted_foundations( · line 77

Quoted 7 lines
include/linux/firmware/trusted_foundations.h +4 -0
@@ -71,12 +71,16 @@ static inline void register_trusted_foundations(
 
 static inline void of_register_trusted_foundations(void)
 {
+	struct device_node *np = of_find_compatible_node(NULL, NULL, "tlm,trusted-foundations");
+
+	of_node_put(np);
+	if (!np)

While this is technically correct, you are now checking to see if this points to a memory location that you no longer know what it really belongs to. C will let you do this, but it might be nicer to fix it up properly so it doesn't look like this.

Quoted 3 lines

thanks,

greg k-h

Hi,Greg KH,

Thanks very much for your effort to review my patch.

In fact, I have reported a commit for this kind of 'check-after-put' coding style:
https://lore.kernel.org/all/[email protected]/

But I have been told to keep such style and I think the explanation is also reasonable. So when I make this patch, I am indeed confused what I should write.

Finally, I think it is better to be consistent with current coding style so I chose this 'check-after-put' style.

But if you think it is better to use a normal order, i.e., check-then-put, I am, of cause, very happy to send a new patch for this bug and I will also keep to use this coding style in future.

Thanks again.

Liang

#3 lore raw
Quoted 35 lines · Liang He
Quoted 28 lines · Greg KH
Quoted 14 lines · Liang He

In of_register_trusted_foundations(), we need to hold the reference returned by of_find_compatible_node() and then use it to call of_node_put() for refcount balance.

---
 include/linux/firmware/trusted_foundations.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/firmware/trusted_foundations.h +0 -0
index be5984bda592..399471c2f1c7 100644

include/linux/firmware/trusted_foundations.h static inline void register_trusted_foundations( · line 77

Quoted 7 lines
include/linux/firmware/trusted_foundations.h +4 -0
@@ -71,12 +71,16 @@ static inline void register_trusted_foundations(
 
 static inline void of_register_trusted_foundations(void)
 {
+	struct device_node *np = of_find_compatible_node(NULL, NULL, "tlm,trusted-foundations");
+
+	of_node_put(np);
+	if (!np)

While this is technically correct, you are now checking to see if this points to a memory location that you no longer know what it really belongs to. C will let you do this, but it might be nicer to fix it up properly so it doesn't look like this.

Quoted 3 lines

thanks,

greg k-h

Quoted 8 lines

Hi,Greg KH,

Thanks very much for your effort to review my patch.

In fact, I have reported a commit for this kind of 'check-after-put' coding style:
https://lore.kernel.org/all/[email protected]/

But I have been told to keep such style and I think the explanation is also reasonable.

It's not very reasonable if you talk to C compiler authors. They can do crazy things with dereferenced memory locations, including optimizing them away entirely as they now "know" that this does not point to any valid memory so it's an undefined thing that the compiler is being asked to do.

Quoted 8 lines

So when I make this patch, I am indeed confused what I should write.

Finally, I think it is better to be consistent with current coding style so I chose this 'check-after-put' style.

But if you think it is better to use a normal order, i.e., check-then-put, I am, of cause, very happy to send a new patch for this bug and I will also keep to use this coding style in future.

check and then put please. That prevents you from having to fix up this type of thing in a few years when the compilers all start to blow up on it.

thanks,

greg k-h

author
#4 lore raw
Quoted 60 lines · Greg KH
Quoted 35 lines · Liang He
Quoted 28 lines · Greg KH
Quoted 14 lines · Liang He

In of_register_trusted_foundations(), we need to hold the reference returned by of_find_compatible_node() and then use it to call of_node_put() for refcount balance.

---
 include/linux/firmware/trusted_foundations.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/firmware/trusted_foundations.h +0 -0
index be5984bda592..399471c2f1c7 100644

include/linux/firmware/trusted_foundations.h static inline void register_trusted_foundations( · line 77

Quoted 7 lines
include/linux/firmware/trusted_foundations.h +4 -0
@@ -71,12 +71,16 @@ static inline void register_trusted_foundations(
 
 static inline void of_register_trusted_foundations(void)
 {
+	struct device_node *np = of_find_compatible_node(NULL, NULL, "tlm,trusted-foundations");
+
+	of_node_put(np);
+	if (!np)

While this is technically correct, you are now checking to see if this points to a memory location that you no longer know what it really belongs to. C will let you do this, but it might be nicer to fix it up properly so it doesn't look like this.

Quoted 3 lines

thanks,

greg k-h

Quoted 8 lines

Hi,Greg KH,

Thanks very much for your effort to review my patch.

In fact, I have reported a commit for this kind of 'check-after-put' coding style:
https://lore.kernel.org/all/[email protected]/

But I have been told to keep such style and I think the explanation is also reasonable.

It's not very reasonable if you talk to C compiler authors. They can do crazy things with dereferenced memory locations, including optimizing them away entirely as they now "know" that this does not point to any valid memory so it's an undefined thing that the compiler is being asked to do.

Quoted 8 lines

So when I make this patch, I am indeed confused what I should write.

Finally, I think it is better to be consistent with current coding style so I chose this 'check-after-put' style.

But if you think it is better to use a normal order, i.e., check-then-put, I am, of cause, very happy to send a new patch for this bug and I will also keep to use this coding style in future.

Quoted 7 lines

check and then put please. That prevents you from having to fix up this type of thing in a few years when the compilers all start to blow up on it.

thanks,

greg k-h

OK, Greg KH,

I am very happy to hear this and I will send 'check-and-put' patch tomorrow.

Thanks very much.

Liang