[PATCH -next 2/5] drm/mediatek: Fix missing of_node_put() for mtk_drm_get_all_drm_priv()

Jinjie Ruan posted 5 patches 1 year, 5 months ago
[PATCH -next 2/5] drm/mediatek: Fix missing of_node_put() for mtk_drm_get_all_drm_priv()
Posted by Jinjie Ruan 1 year, 5 months ago
In mtk_drm_get_all_drm_priv(), break in for_each_child_of_node() should
call of_node_put() to avoid child node resource leak, use
for_each_child_of_node_scoped() to fix it.

And avoid the need for manual cleanup of_node_put() in early exits
from the loop for another one.

Fixes: d761b9450e31 ("drm/mediatek: Add cnt checking for coverity issue")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
---
 drivers/gpu/drm/mediatek/mtk_drm_drv.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/mediatek/mtk_drm_drv.c b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
index 77b50c56c124..41aff0183cbd 100644
--- a/drivers/gpu/drm/mediatek/mtk_drm_drv.c
+++ b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
@@ -371,12 +371,11 @@ static bool mtk_drm_get_all_drm_priv(struct device *dev)
 	struct mtk_drm_private *temp_drm_priv;
 	struct device_node *phandle = dev->parent->of_node;
 	const struct of_device_id *of_id;
-	struct device_node *node;
 	struct device *drm_dev;
 	unsigned int cnt = 0;
 	int i, j;
 
-	for_each_child_of_node(phandle->parent, node) {
+	for_each_child_of_node_scoped(phandle->parent, node) {
 		struct platform_device *pdev;
 
 		of_id = of_match_node(mtk_drm_of_ids, node);
@@ -828,7 +827,6 @@ static int mtk_drm_probe(struct platform_device *pdev)
 	struct device_node *phandle = dev->parent->of_node;
 	const struct of_device_id *of_id;
 	struct mtk_drm_private *private;
-	struct device_node *node;
 	struct component_match *match = NULL;
 	struct platform_device *ovl_adaptor;
 	int ret;
@@ -869,7 +867,7 @@ static int mtk_drm_probe(struct platform_device *pdev)
 	}
 
 	/* Iterate over sibling DISP function blocks */
-	for_each_child_of_node(phandle->parent, node) {
+	for_each_child_of_node_scoped(phandle->parent, node) {
 		const struct of_device_id *of_id;
 		enum mtk_ddp_comp_type comp_type;
 		int comp_id;
@@ -933,10 +931,8 @@ static int mtk_drm_probe(struct platform_device *pdev)
 		}
 
 		ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
-		if (ret) {
-			of_node_put(node);
+		if (ret)
 			goto err_node;
-		}
 	}
 
 	if (!private->mutex_node) {
-- 
2.34.1
Re: [PATCH -next 2/5] drm/mediatek: Fix missing of_node_put() for mtk_drm_get_all_drm_priv()
Posted by Christophe JAILLET 1 year, 5 months ago
Le 23/08/2024 à 11:20, Jinjie Ruan a écrit :
> In mtk_drm_get_all_drm_priv(), break in for_each_child_of_node() should
> call of_node_put() to avoid child node resource leak, use
> for_each_child_of_node_scoped() to fix it.
> 
> And avoid the need for manual cleanup of_node_put() in early exits
> from the loop for another one.
> 
> Fixes: d761b9450e31 ("drm/mediatek: Add cnt checking for coverity issue")
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> ---
>   drivers/gpu/drm/mediatek/mtk_drm_drv.c | 10 +++-------
>   1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_drv.c b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> index 77b50c56c124..41aff0183cbd 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> @@ -371,12 +371,11 @@ static bool mtk_drm_get_all_drm_priv(struct device *dev)
>   	struct mtk_drm_private *temp_drm_priv;
>   	struct device_node *phandle = dev->parent->of_node;
>   	const struct of_device_id *of_id;
> -	struct device_node *node;
>   	struct device *drm_dev;
>   	unsigned int cnt = 0;
>   	int i, j;
>   
> -	for_each_child_of_node(phandle->parent, node) {
> +	for_each_child_of_node_scoped(phandle->parent, node) {
>   		struct platform_device *pdev;
>   
>   		of_id = of_match_node(mtk_drm_of_ids, node);
> @@ -828,7 +827,6 @@ static int mtk_drm_probe(struct platform_device *pdev)
>   	struct device_node *phandle = dev->parent->of_node;
>   	const struct of_device_id *of_id;
>   	struct mtk_drm_private *private;
> -	struct device_node *node;
>   	struct component_match *match = NULL;
>   	struct platform_device *ovl_adaptor;
>   	int ret;
> @@ -869,7 +867,7 @@ static int mtk_drm_probe(struct platform_device *pdev)
>   	}
>   
>   	/* Iterate over sibling DISP function blocks */
> -	for_each_child_of_node(phandle->parent, node) {
> +	for_each_child_of_node_scoped(phandle->parent, node) {
>   		const struct of_device_id *of_id;
>   		enum mtk_ddp_comp_type comp_type;
>   		int comp_id;
> @@ -933,10 +931,8 @@ static int mtk_drm_probe(struct platform_device *pdev)
>   		}
>   
>   		ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
> -		if (ret) {
> -			of_node_put(node);
> +		if (ret)
>   			goto err_node;

Hi,

I've seen on another thread that is was not sure that scoped versions 
and gotos played well together.

It was asked to check more in details and confirm that it was safe 
before applying the patch.

I've not followed the discussion, so I just point it out, in case it helps.

I'll try to give it a look in the coming days.


CJ

> -		}
>   	}
>   
>   	if (!private->mutex_node) {

Re: [PATCH -next 2/5] drm/mediatek: Fix missing of_node_put() for mtk_drm_get_all_drm_priv()
Posted by Marion & Christophe JAILLET 1 year, 5 months ago

Le 23/08/2024 à 12:46, Christophe JAILLET a écrit :
>> @@ -933,10 +931,8 @@ static int mtk_drm_probe(struct platform_device 
>> *pdev)
>>           }
>>           ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], 
>> comp_id);
>> -        if (ret) {
>> -            of_node_put(node);
>> +        if (ret)
>>               goto err_node;
> 
> Hi,
> 
> I've seen on another thread that is was not sure that scoped versions 
> and gotos played well together.
> 
> It was asked to check more in details and confirm that it was safe 
> before applying the patch.
> 
> I've not followed the discussion, so I just point it out, in case it helps.
> 
> I'll try to give it a look in the coming days.
> 
> 
> CJ
> 

Hi,
looking at the generated asm file (gcc 14.2.1), everything looks fine.

# drivers/gpu/drm/mediatek/mtk_drm_drv.c:933: 		ret = 
mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
	salq	$5, %rax	#, _36
	movl	%r14d, %edx	# comp_id,
	movq	%rbx, %rdi	# node,
	leaq	552(%rbp,%rax), %rsi	#, _28
	call	mtk_ddp_comp_init	#
	movl	%eax, %r12d	# tmp205, <retval>
# drivers/gpu/drm/mediatek/mtk_drm_drv.c:934: 		if (ret)
	testl	%eax, %eax	# <retval>
	jne	.L212	#,

...

.L212:
# ./include/linux/of.h:138: DEFINE_FREE(device_node, struct device_node 
*, if (_T) of_node_put(_T))
	movq	%rbx, %rdi	# node,
	call	of_node_put	#
	jmp	.L171	#

CJ
Re: [PATCH -next 2/5] drm/mediatek: Fix missing of_node_put() for mtk_drm_get_all_drm_priv()
Posted by Jinjie Ruan 1 year, 5 months ago

On 2024/8/25 13:16, Marion & Christophe JAILLET wrote:
> 
> 
> Le 23/08/2024 à 12:46, Christophe JAILLET a écrit :
>>> @@ -933,10 +931,8 @@ static int mtk_drm_probe(struct platform_device
>>> *pdev)
>>>           }
>>>           ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id],
>>> comp_id);
>>> -        if (ret) {
>>> -            of_node_put(node);
>>> +        if (ret)
>>>               goto err_node;
>>
>> Hi,
>>
>> I've seen on another thread that is was not sure that scoped versions
>> and gotos played well together.
>>
>> It was asked to check more in details and confirm that it was safe
>> before applying the patch.
>>
>> I've not followed the discussion, so I just point it out, in case it
>> helps.
>>
>> I'll try to give it a look in the coming days.
>>
>>
>> CJ
>>
> 
> Hi,
> looking at the generated asm file (gcc 14.2.1), everything looks fine.

Yes, as I pointed out in another thread, the test show that goto with
this scoped function is good.

> 
> # drivers/gpu/drm/mediatek/mtk_drm_drv.c:933:         ret =
> mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
>     salq    $5, %rax    #, _36
>     movl    %r14d, %edx    # comp_id,
>     movq    %rbx, %rdi    # node,
>     leaq    552(%rbp,%rax), %rsi    #, _28
>     call    mtk_ddp_comp_init    #
>     movl    %eax, %r12d    # tmp205, <retval>
> # drivers/gpu/drm/mediatek/mtk_drm_drv.c:934:         if (ret)
>     testl    %eax, %eax    # <retval>
>     jne    .L212    #,
> 
> ...
> 
> .L212:
> # ./include/linux/of.h:138: DEFINE_FREE(device_node, struct device_node
> *, if (_T) of_node_put(_T))
>     movq    %rbx, %rdi    # node,
>     call    of_node_put    #
>     jmp    .L171    #
> 
> CJ
>