# \[PATCH\] powerpc: fix unbalanced node refcount in check\_kvm\_guest()

- Subsystems: LINUX FOR POWERPC (32-BIT AND 64-BIT), THE REST
- Maintainers: Madhavan Srinivasan, Linus Torvalds
- Status: LANDED (56537faf8821 · 2021-10-08 13:15)
- Landed in mainline as 56537faf8821 on 2021-10-08.
- List: linuxppc-dev
- 4 messages, 4 authors
- Page: https://ratatoskr.run/linuxppc-dev/2021/09/9524531/t

## \[PATCH\] powerpc: fix unbalanced node refcount in check\_kvm\_guest()

- From: Nathan Lynch \<hidden\>
- Date: 2021-09-28 12:46:38 
- Message: https://ratatoskr.run/linuxppc-dev/2021/09/9524531.md

```text
When check_kvm_guest() succeeds in looking up a /hypervisor OF node, it
returns without performing a matching put for the lookup, leaving the
node's reference count elevated.

Add the necessary call to of_node_put(), rearranging the code slightly to
avoid repetition or goto.

Signed-off-by: Nathan Lynch <redacted>
Fixes: 107c55005fbd ("powerpc/pseries: Add KVM guest doorbell restrictions")
---
 arch/powerpc/kernel/firmware.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/arch/powerpc/kernel/firmware.c b/arch/powerpc/kernel/firmware.c
index c7022c41cc31..20328f72f9f2 100644
--- a/arch/powerpc/kernel/firmware.c
+++ b/arch/powerpc/kernel/firmware.c
@@ -31,11 +31,10 @@ int __init check_kvm_guest(void)
 	if (!hyper_node)
 		return 0;
 
-	if (!of_device_is_compatible(hyper_node, "linux,kvm"))
-		return 0;
-
-	static_branch_enable(&kvm_guest);
+	if (of_device_is_compatible(hyper_node, "linux,kvm"))
+		static_branch_enable(&kvm_guest);
 
+	of_node_put(hyper_node);
 	return 0;
 }
 core_initcall(check_kvm_guest); // before kvm_guest_init()
-- 
2.31.1

```

## Re: \[PATCH\] powerpc: fix unbalanced node refcount in check\_kvm\_guest()

- From: Tyrel Datwyler \<tyreld@linux.ibm.com\>
- Date: 2021-09-28 18:58:08 
- Message: https://ratatoskr.run/linuxppc-dev/2021/09/9524539.md

```text
On 9/28/21 5:45 AM, Nathan Lynch wrote:
> When check_kvm_guest() succeeds in looking up a /hypervisor OF node, it
> returns without performing a matching put for the lookup, leaving the
> node's reference count elevated.
> 
> Add the necessary call to of_node_put(), rearranging the code slightly to
> avoid repetition or goto.
> 
> Signed-off-by: Nathan Lynch <redacted>
> Fixes: 107c55005fbd ("powerpc/pseries: Add KVM guest doorbell restrictions")

Reviewed-by: Tyrel Datwyler <tyreld@linux.ibm.com>
```

## Re: \[PATCH\] powerpc: fix unbalanced node refcount in check\_kvm\_guest()

- From: Srikar Dronamraju \<hidden\>
- Date: 2021-09-29 07:30:24 
- Message: https://ratatoskr.run/linuxppc-dev/2021/09/9524547.md

```text
* Nathan Lynch [ref] [2021-09-28 07:45:50]:

> When check_kvm_guest() succeeds in looking up a /hypervisor OF node, it
> returns without performing a matching put for the lookup, leaving the
> node's reference count elevated.
> 
> Add the necessary call to of_node_put(), rearranging the code slightly to
> avoid repetition or goto.
> 

Looks good to me.

I do see few other cases where we call of_find_node calls without
of_node_put().

Some of them that I saw were in

find_legacy_serial_ports()  in  arch/powerpc/kernel/legacy_serial.c
proc_ppc64_create in arch/powerpc/proc/powerpc.c
update_events_in_group in arch/powerpc/perf/imc-pmu.c
cell_iommu_init_disabled in arch/powerpc/platforms/cell/iommu.c
cell_publish_devices in arch/powerpc/platforms/cell/setup.c
spufs_init_isolated_loader in arch/powerpc/platforms/cell/spufs/inode.c
holly_init_pci / holly_restart and holly_init_IRQ in arch/powerpc/platforms/embedded6xx/holly.c

Reviewed-by: Srikar Dronamraju <redacted>

> Signed-off-by: Nathan Lynch <redacted>
> Fixes: 107c55005fbd ("powerpc/pseries: Add KVM guest doorbell restrictions")
> ---
>  arch/powerpc/kernel/firmware.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/powerpc/kernel/firmware.c b/arch/powerpc/kernel/firmware.c
> index c7022c41cc31..20328f72f9f2 100644
> --- a/arch/powerpc/kernel/firmware.c
> +++ b/arch/powerpc/kernel/firmware.c
> @@ -31,11 +31,10 @@ int __init check_kvm_guest(void)
>  	if (!hyper_node)
>  		return 0;
> 
> -	if (!of_device_is_compatible(hyper_node, "linux,kvm"))
> -		return 0;
> -
> -	static_branch_enable(&kvm_guest);
> +	if (of_device_is_compatible(hyper_node, "linux,kvm"))
> +		static_branch_enable(&kvm_guest);
> 
> +	of_node_put(hyper_node);
>  	return 0;
>  }
>  core_initcall(check_kvm_guest); // before kvm_guest_init()
> -- 
> 2.31.1
> 

-- 
Thanks and Regards
Srikar Dronamraju
```

## Re: \[PATCH\] powerpc: fix unbalanced node refcount in check\_kvm\_guest()

- From: Michael Ellerman \<hidden\>
- Date: 2021-10-11 12:11:00 
- Message: https://ratatoskr.run/linuxppc-dev/2021/10/9524675.md

```text
On Tue, 28 Sep 2021 07:45:50 -0500, Nathan Lynch wrote:
> When check_kvm_guest() succeeds in looking up a /hypervisor OF node, it
> returns without performing a matching put for the lookup, leaving the
> node's reference count elevated.
> 
> Add the necessary call to of_node_put(), rearranging the code slightly to
> avoid repetition or goto.
> 
> [...]

Applied to powerpc/next.

[1/1] powerpc: fix unbalanced node refcount in check_kvm_guest()
      https://git.kernel.org/powerpc/c/56537faf8821e361d739fc5ff58c9c40f54a1d4c

cheers
```
