arm/xen: Fix refcount leak in xen_dt_guest_init
Subsystems (2)
PATCH 1/1 arm/xen: Fix refcount leak in xen_dt_guest_init
The of_find_compatible_node() function returns a node pointer with refcount incremented, We should use of_node_put() on it when done Add the missing of_node_put() to release the refcount.
--- arch/arm/xen/enlighten.c | 1 + 1 file changed, 1 insertion(+)
arch/arm/xen/enlighten.c +1 -0
@@ -424,6 +424,7 @@ static void __init xen_dt_guest_init(void)if (of_address_to_resource (xen_node,GRANT_TABLE_INDEX , &res)) {pr_err ("Xen grant table region is not found\n" ); +of_node_put (xen_node);return ; } xen_grant_frames = res.start;
Signature · 1 line
2.17.1
Stefano Stabellini
maintainerQuoted 6 lines · Miaoqian Lin
The
of_find_compatible_node()function returns a node pointer with refcount incremented, We should useof_node_put()on it when done Add the missingof_node_put()to release the refcount.Fixes 9b08aaa3199a ("ARM: XEN: Move xen_early_init() before efi_init()")Signed-off-by Miaoqian Lin <[email protected]>
Thanks for the patch!
Quoted 9 lines
--- arch/arm/xen/enlighten.c | 1 + 1 file changed, 1 insertion(+)arch/arm/xen/enlighten.c +0
-0
arch/arm/xen/enlighten.c static void __init xen_dt_guest_init(void) · line 429
Quoted 6 lines
arch/arm/xen/enlighten.c +1
-0@@ -424,6 +424,7 @@ static void __init xen_dt_guest_init(void)if (of_address_to_resource (xen_node,GRANT_TABLE_INDEX , &res)) {pr_err ("Xen grant table region is not found\n" ); +of_node_put (xen_node);return ; }
This is adding a call to of_node_put on the error path. Shouldn't it be called also in the non-error path?
Also, there is another instance of of_address_to_resource being called in this file (in arch_xen_unpopulated_init), does it make sense to call of_node_put there too?
Hi,
Quoted 21 lines · Stefano Stabellini
Quoted 6 lines · Miaoqian Lin
The
of_find_compatible_node()function returns a node pointer with refcount incremented, We should useof_node_put()on it when done Add the missingof_node_put()to release the refcount.Fixes 9b08aaa3199a ("ARM: XEN: Move xen_early_init() before efi_init()")Signed-off-by Miaoqian Lin <[email protected]>Thanks for the patch!
Quoted 10 lines
--- arch/arm/xen/enlighten.c | 1 + 1 file changed, 1 insertion(+)arch/arm/xen/enlighten.c +0
-0@@ -424,6 +424,7 @@ static void __init xen_dt_guest_init(void)
Quoted 8 lines
arch/arm/xen/enlighten.c static void __init xen_dt_guest_init(void) · line 429
Quoted 5 lines
arch/arm/xen/enlighten.c +1
-0if (of_address_to_resource (xen_node,GRANT_TABLE_INDEX , &res)) {pr_err ("Xen grant table region is not found\n" ); +of_node_put (xen_node);return ; }This is adding a call to
of_node_puton the error path. Shouldn't it be called also in the non-error path?
You're right. It should be called also in the non-error path. I made a mistake.
Quoted 3 lines
Also, there is another instance of
of_address_to_resourcebeing called in this file (inarch_xen_unpopulated_init), does it make sense to callof_node_putthere too?
I think so, becase device node pointer np is a local variable. So the reference it taken should be released in the scope.
I look into the whole codebase for this kind of usage pattern ($ret=of_find_compatible_node();of_address_to_resource($ret,_,_), $ret is a local variable), Most of them call of_node_put() when done. And document of of_find_compatible_node() also mentions
Quoted 2 lines
Return: A node pointer with refcount incremented, use
of_node_put()on it when done.
But I am not sure, Since I am unfamiliar with other code logic. It better if the developers could double check. I found some similar cases in arch/arm.
PATCH v2 1/1 arm/xen: Fix some refcount leaks
The of_find_compatible_node() function returns a node pointer with refcount incremented, We should use of_node_put() on it when done Add the missing of_node_put() to release the refcount.
---
changes in v2:
- call of_node_put in non-error path in xen_dt_guest_init
- fix same refcount leak error in arch_xen_unpopulated_init
--- arch/arm/xen/enlighten.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
arch/arm/xen/enlighten.c +7 -2
@@ -337,12 +337,15 @@ int __init arch_xen_unpopulated_init(struct resource **res)if (!nr_reg) {pr_err ("No extended regions are found\n" ); +of_node_put (np);return -EINVAL ; } regs =kcalloc (nr_reg,sizeof (*regs),GFP_KERNEL ); -if (!regs) +if (!regs) { +of_node_put (np);return -ENOMEM ; + }/* * Create resource from extended regions provided by the hypervisor to be
@@ -403,8 +406,8 @@ int __init arch_xen_unpopulated_init(struct resource **res) *res = &xen_resource; err: +of_node_put (np);kfree (regs); - return rc; }#endif
@@ -424,8 +427,10 @@ static void __init xen_dt_guest_init(void)if (of_address_to_resource (xen_node,GRANT_TABLE_INDEX , &res)) {pr_err ("Xen grant table region is not found\n" ); +of_node_put (xen_node);return ; } +of_node_put (xen_node); xen_grant_frames = res.start; }
Signature · 1 line
2.17.1
Re: [PATCH v2] arm/xen: Fix some refcount leaks
Quoted 7 lines · Miaoqian Lin
The
of_find_compatible_node()function returns a node pointer with refcount incremented, We should useof_node_put()on it when done Add the missingof_node_put()to release the refcount.Fixes 9b08aaa3199a ("ARM: XEN: Move xen_early_init() before efi_init()")Fixes b2371587fe0c ("arm/xen: Read extended regions from DT and init Xen resource")Signed-off-by Miaoqian Lin <[email protected]>
Juergen, Boris,
I applied the patch to for-linus-5.18
Quoted 53 lines
---
changes in v2:
- callof_node_putin non-error path inxen_dt_guest_init
- fix same refcount leak error inarch_xen_unpopulated_init--- arch/arm/xen/enlighten.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)arch/arm/xen/enlighten.c +7
-2@@ -337,12 +337,15 @@ int __init arch_xen_unpopulated_init(struct resource **res)if (!nr_reg) {pr_err ("No extended regions are found\n" ); +of_node_put (np);return -EINVAL ; } regs =kcalloc (nr_reg,sizeof (*regs),GFP_KERNEL ); -if (!regs) +if (!regs) { +of_node_put (np);return -ENOMEM ; + }/* * Create resource from extended regions provided by the hypervisor to be @@ -403,8 +406,8 @@ int __init arch_xen_unpopulated_init(struct resource **res) *res = &xen_resource; err: +of_node_put (np);kfree (regs); - return rc; }#endif @@ -424,8 +427,10 @@ static void __init xen_dt_guest_init(void)if (of_address_to_resource (xen_node,GRANT_TABLE_INDEX , &res)) {pr_err ("Xen grant table region is not found\n" ); +of_node_put (xen_node);return ; } +of_node_put (xen_node); xen_grant_frames = res.start; }Signature · 1 line
2.17.1