diff mbox series

[5/6] phy: core: Add missing of_node_put() in of_phy_provider_lookup()

Message ID 20241020-phy_core_fix-v1-5-078062f7da71@quicinc.com (mailing list archive)
State Not Applicable
Headers show
Series phy: core: Fix bugs for several APIs and simplify an API | expand

Checks

Context Check Description
netdev/series_format warning Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 4 of 4 maintainers
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 4 this patch: 4
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 13 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 14 this patch: 14
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-10-20--12-00 (tests: 765)

Commit Message

Zijun Hu Oct. 20, 2024, 5:27 a.m. UTC
From: Zijun Hu <quic_zijuhu@quicinc.com>

The for_each_child_of_node() macro automatically decrements the child
refcount at the end of every iteration. On early exits, of_node_put()
must be used to manually decrement the refcount and avoid memory leaks.

The macro called by of_phy_provider_lookup() has such early exit, but
it does not call of_node_put() before early exit.

Fixed by adding missing of_node_put() in of_phy_provider_lookup().

Fixes: 2a4c37016ca9 ("phy: core: Fix of_phy_provider_lookup to return PHY provider for sub node")
Cc: stable@vger.kernel.org
Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>

---
The impact of this change is wide since of_phy_provider_lookup()
is indirectly called by APIs phy_get(), of_phy_get(), and
devm_of_phy_get_by_index().

The following kernel mainline commit has similar fix:
Commit: b337cc3ce475 ("backlight: lm3509_bl: Fix early returns in for_each_child_of_node()")
---
 drivers/phy/phy-core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Christophe JAILLET Oct. 20, 2024, 7:23 a.m. UTC | #1
Le 20/10/2024 à 07:27, Zijun Hu a écrit :
> From: Zijun Hu <quic_zijuhu@quicinc.com>
> 
> The for_each_child_of_node() macro automatically decrements the child
> refcount at the end of every iteration. On early exits, of_node_put()
> must be used to manually decrement the refcount and avoid memory leaks.
> 
> The macro called by of_phy_provider_lookup() has such early exit, but
> it does not call of_node_put() before early exit.
> 
> Fixed by adding missing of_node_put() in of_phy_provider_lookup().
> 
> Fixes: 2a4c37016ca9 ("phy: core: Fix of_phy_provider_lookup to return PHY provider for sub node")
> Cc: stable@vger.kernel.org
> Signed-off-by: Zijun Hu <quic_zijuhu@quicinc.com>
> 
> ---
> The impact of this change is wide since of_phy_provider_lookup()
> is indirectly called by APIs phy_get(), of_phy_get(), and
> devm_of_phy_get_by_index().
> 
> The following kernel mainline commit has similar fix:
> Commit: b337cc3ce475 ("backlight: lm3509_bl: Fix early returns in for_each_child_of_node()")
> ---
>   drivers/phy/phy-core.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
> index 967878b78797..24bd619a33dd 100644
> --- a/drivers/phy/phy-core.c
> +++ b/drivers/phy/phy-core.c
> @@ -143,10 +143,11 @@ static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
>   	list_for_each_entry(phy_provider, &phy_provider_list, list) {
>   		if (phy_provider->dev->of_node == node)
>   			return phy_provider;
> -
>   		for_each_child_of_node(phy_provider->children, child)
> -			if (child == node)
> +			if (child == node) {
> +				of_node_put(child);
>   				return phy_provider;
> +			}

Hi,

Maybe for_each_child_of_node_scoped() to slightly simplify things at the 
same time?

>   	}
>   
>   	return ERR_PTR(-EPROBE_DEFER);
>
Zijun Hu Oct. 20, 2024, 8:18 a.m. UTC | #2
On 2024/10/20 15:23, Christophe JAILLET wrote:
> Le 20/10/2024 à 07:27, Zijun Hu a écrit :
>> From: Zijun Hu <quic_zijuhu@quicinc.com>

[snip]

>> ---
>>   drivers/phy/phy-core.c | 5 +++--
>>   1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
>> index 967878b78797..24bd619a33dd 100644
>> --- a/drivers/phy/phy-core.c
>> +++ b/drivers/phy/phy-core.c
>> @@ -143,10 +143,11 @@ static struct phy_provider
>> *of_phy_provider_lookup(struct device_node *node)
>>       list_for_each_entry(phy_provider, &phy_provider_list, list) {
>>           if (phy_provider->dev->of_node == node)
>>               return phy_provider;
>> -
>>           for_each_child_of_node(phy_provider->children, child)
>> -            if (child == node)
>> +            if (child == node) {
>> +                of_node_put(child);
>>                   return phy_provider;
>> +            }
> 
> Hi,
> 
> Maybe for_each_child_of_node_scoped() to slightly simplify things at the
> same time?
> 

thank you for code review.

it does not use _scoped() since for_each_child_of_node() usage here is
very simple and only has one early exit, _scoped() normally is for
complex case.

i maybe use _scoped() in next revision if one more reviewer also prefers
it.

thank you.

>>       }
>>         return ERR_PTR(-EPROBE_DEFER);
>>
>
diff mbox series

Patch

diff --git a/drivers/phy/phy-core.c b/drivers/phy/phy-core.c
index 967878b78797..24bd619a33dd 100644
--- a/drivers/phy/phy-core.c
+++ b/drivers/phy/phy-core.c
@@ -143,10 +143,11 @@  static struct phy_provider *of_phy_provider_lookup(struct device_node *node)
 	list_for_each_entry(phy_provider, &phy_provider_list, list) {
 		if (phy_provider->dev->of_node == node)
 			return phy_provider;
-
 		for_each_child_of_node(phy_provider->children, child)
-			if (child == node)
+			if (child == node) {
+				of_node_put(child);
 				return phy_provider;
+			}
 	}
 
 	return ERR_PTR(-EPROBE_DEFER);