Skip to contentSkip to conversation

arch: x86: kernel: Add missing of_node_put() in devicetree.c

PATCH In review Liang He 3 participants 5 messages Jun 15, 2022 x86

lore
Share & tools
lore

Liang He

author
#0 lore raw

PATCH 1/1 arch: x86: kernel: Add missing of_node_put() in devicetree.c

In dtb_setup_hpet(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() when it is not used anymore.

---
 arch/x86/kernel/devicetree.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kernel/devicetree.c +3 -0
index 5cd51f25f446..6a386424ddf7 100644
@@ -120,6 +120,9 @@ static void __init dtb_setup_hpet(void)
 	if (!dn)
 		return;
 	ret = of_address_to_resource(dn, 0, &r);
+	
+	of_node_put(dn);
+	
 	if (ret) {
 		WARN_ON(1);
 		return;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 5 lines · Liang He

In dtb_setup_hpet(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() when it is not used anymore.

Seems like:

Fixes ffb9fc68dff3 ("x86: dtb: Add device tree support for HPET")

and would be appropriate, right?

Also, how was this found, and what is the impact from not fixing it? Was it causing horrible problems in production, or was it just something that was found by inspection that's not causing any real problems in practice?

#2 lore raw
Quoted 8 lines · Dave Hansen
Quoted 5 lines · Liang He

In dtb_setup_hpet(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() when it is not used anymore.

Quoted 7 lines

Seems like:

Fixes ffb9fc68dff3 ("x86: dtb: Add device tree support for HPET")

and would be appropriate, right?

Also, how was this found, and what is the impact from not fixing it?

No impact if the node is not dynamically removed. That's almost all cases except IBM pSeries cpu, memory, and pci. Overlays could change that, but their support in the kernel is limited.

If it really mattered, we'd probably come up with a different way to do the refcounting as it is hard to get right. In fact, this fix is not right. Will reply with the right context.

Quoted 3 lines

Was it causing horrible problems in production, or was it just something that was found by inspection that's not causing any real problems in practice?

#3 lore raw
In reply to Liang He
Quoted 15 lines · Liang He

In dtb_setup_hpet(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() when it is not used anymore.

---
 arch/x86/kernel/devicetree.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kernel/devicetree.c +0 -0
index 5cd51f25f446..6a386424ddf7 100644

arch/x86/kernel/devicetree.c static void __init dtb_setup_hpet(void) · line 125

Quoted 6 lines
arch/x86/kernel/devicetree.c +3 -0
@@ -120,6 +120,9 @@ static void __init dtb_setup_hpet(void)
        if (!dn)
                return;
        ret = of_address_to_resource(dn, 0, &r);
+
+       of_node_put(dn);
+

You don't want a put on success. If you are using the device, then you want to hold a reference to it.

I would guess that if you have an error here and don't have your timer, you're not going to finish booting anyways.

Finally, wouldn't dtb_lapic_setup() and dtb_add_ioapic() also need a similar change? But again, if those aren't initialized, you probably aren't getting very far.

Quoted 6 lines
arch/x86/kernel/devicetree.c +0 -1
        if (ret) {
                WARN_ON(1);
                return;
--

2.25.1

author
#4 lore raw

Hi, rob, thanks for your reply.

The of_find_xx will increase the refcounter for the local reference, so when the function return, we need a decrease for the destroy of the local reference. The device will not be freed as its refcounter will be sure larger than 0 when the function returns.

All we need is to keep the refcounting balance, right?

Quoted 40 lines · Rob Herring
Quoted 15 lines · Liang He

In dtb_setup_hpet(), of_find_compatible_node() will return a node pointer with refcount incremented. We should use of_node_put() when it is not used anymore.

---
 arch/x86/kernel/devicetree.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kernel/devicetree.c +0 -0
index 5cd51f25f446..6a386424ddf7 100644

arch/x86/kernel/devicetree.c static void __init dtb_setup_hpet(void) · line 125

Quoted 6 lines
arch/x86/kernel/devicetree.c +3 -0
@@ -120,6 +120,9 @@ static void __init dtb_setup_hpet(void)
        if (!dn)
                return;
        ret = of_address_to_resource(dn, 0, &r);
+
+       of_node_put(dn);
+

You don't want a put on success. If you are using the device, then you want to hold a reference to it.

I would guess that if you have an error here and don't have your timer, you're not going to finish booting anyways.

Finally, wouldn't dtb_lapic_setup() and dtb_add_ioapic() also need a similar change? But again, if those aren't initialized, you probably aren't getting very far.

Quoted 6 lines
arch/x86/kernel/devicetree.c +0 -1
        if (ret) {
                WARN_ON(1);
                return;
--

2.25.1