drm/mediatek: Fix potential memory leak if vmap() fail

Message ID 20230626185801.684451-1-suijingfeng@loongson.cn
State New
Headers
Series drm/mediatek: Fix potential memory leak if vmap() fail |

Commit Message

Sui Jingfeng June 26, 2023, 6:58 p.m. UTC
  Also return -ENOMEM if such a failure happens, the implement should take
responsibility for the error handling.

Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn>
---
 drivers/gpu/drm/mediatek/mtk_drm_gem.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
  

Comments

Matthias Brugger July 6, 2023, 11:39 a.m. UTC | #1
On 26/06/2023 20:58, Sui Jingfeng wrote:
> Also return -ENOMEM if such a failure happens, the implement should take
> responsibility for the error handling.
> 
> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn>
> ---
>   drivers/gpu/drm/mediatek/mtk_drm_gem.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.c b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> index a25b28d3ee90..9f364df52478 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> @@ -247,7 +247,11 @@ int mtk_drm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
>   
>   	mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
>   			       pgprot_writecombine(PAGE_KERNEL));
> -
> +	if (!mtk_gem->kvaddr) {
> +		kfree(sgt);
> +		kfree(mtk_gem->pages);
> +		return -ENOMEM;
> +	}

Reviewed-by: Matthias Brugger <matthias.bgg@gmail.com>

>   out:
>   	kfree(sgt);
>   	iosys_map_set_vaddr(map, mtk_gem->kvaddr);
  
Alexandre Mergnat July 6, 2023, 12:13 p.m. UTC | #2
On 26/06/2023 20:58, Sui Jingfeng wrote:
> Also return -ENOMEM if such a failure happens, the implement should take
> responsibility for the error handling.

Reviewed-by: Alexandre Mergnat <amergnat@baylibre.com>
  
Sui Jingfeng July 6, 2023, 12:46 p.m. UTC | #3
Hi, Thanks a lot!


On 2023/7/6 19:39, Matthias Brugger wrote:
>
>
> On 26/06/2023 20:58, Sui Jingfeng wrote:
>> Also return -ENOMEM if such a failure happens, the implement should take
>> responsibility for the error handling.
>>
>> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn>
>> ---
>>   drivers/gpu/drm/mediatek/mtk_drm_gem.c | 6 +++++-
>>   1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.c 
>> b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
>> index a25b28d3ee90..9f364df52478 100644
>> --- a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
>> +++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
>> @@ -247,7 +247,11 @@ int mtk_drm_gem_prime_vmap(struct drm_gem_object 
>> *obj, struct iosys_map *map)
>>         mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
>>                      pgprot_writecombine(PAGE_KERNEL));
>> -
>> +    if (!mtk_gem->kvaddr) {
>> +        kfree(sgt);
>> +        kfree(mtk_gem->pages);
>> +        return -ENOMEM;
>> +    }
>
> Reviewed-by: Matthias Brugger <matthias.bgg@gmail.com>
>
>>   out:
>>       kfree(sgt);
>>       iosys_map_set_vaddr(map, mtk_gem->kvaddr);
  
Sui Jingfeng July 6, 2023, 12:47 p.m. UTC | #4
Hi, thanks a lot!


On 2023/7/6 20:13, Alexandre Mergnat wrote:
>
>
> On 26/06/2023 20:58, Sui Jingfeng wrote:
>> Also return -ENOMEM if such a failure happens, the implement should take
>> responsibility for the error handling.
>
> Reviewed-by: Alexandre Mergnat <amergnat@baylibre.com>
>
  
AngeloGioacchino Del Regno July 6, 2023, 12:47 p.m. UTC | #5
Il 26/06/23 20:58, Sui Jingfeng ha scritto:
> Also return -ENOMEM if such a failure happens, the implement should take
> responsibility for the error handling.
> 
> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn>

This commit needs a Fixes tag. Please add the relevant one and resend.

Thanks,
Angelo

> ---
>   drivers/gpu/drm/mediatek/mtk_drm_gem.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.c b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> index a25b28d3ee90..9f364df52478 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
> @@ -247,7 +247,11 @@ int mtk_drm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
>   
>   	mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
>   			       pgprot_writecombine(PAGE_KERNEL));
> -
> +	if (!mtk_gem->kvaddr) {
> +		kfree(sgt);
> +		kfree(mtk_gem->pages);
> +		return -ENOMEM;
> +	}
>   out:
>   	kfree(sgt);
>   	iosys_map_set_vaddr(map, mtk_gem->kvaddr);
  
Sui Jingfeng July 6, 2023, 1:43 p.m. UTC | #6
Hi,

On 2023/7/6 20:47, AngeloGioacchino Del Regno wrote:
> Il 26/06/23 20:58, Sui Jingfeng ha scritto:
>> Also return -ENOMEM if such a failure happens, the implement should take
>> responsibility for the error handling.
>>
>> Signed-off-by: Sui Jingfeng <suijingfeng@loongson.cn>
>
> This commit needs a Fixes tag. Please add the relevant one and resend.
>
Done at [1],


Thanks for point this out, I got it.

[1] https://patchwork.freedesktop.org/patch/545843/?series=119885&rev=2


> Thanks,
> Angelo
>
>> ---
>>   drivers/gpu/drm/mediatek/mtk_drm_gem.c | 6 +++++-
>>   1 file changed, 5 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.c 
>> b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
>> index a25b28d3ee90..9f364df52478 100644
>> --- a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
>> +++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
>> @@ -247,7 +247,11 @@ int mtk_drm_gem_prime_vmap(struct drm_gem_object 
>> *obj, struct iosys_map *map)
>>         mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
>>                      pgprot_writecombine(PAGE_KERNEL));
>> -
>> +    if (!mtk_gem->kvaddr) {
>> +        kfree(sgt);
>> +        kfree(mtk_gem->pages);
>> +        return -ENOMEM;
>> +    }
>>   out:
>>       kfree(sgt);
>>       iosys_map_set_vaddr(map, mtk_gem->kvaddr);
>
>
  

Patch

diff --git a/drivers/gpu/drm/mediatek/mtk_drm_gem.c b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
index a25b28d3ee90..9f364df52478 100644
--- a/drivers/gpu/drm/mediatek/mtk_drm_gem.c
+++ b/drivers/gpu/drm/mediatek/mtk_drm_gem.c
@@ -247,7 +247,11 @@  int mtk_drm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
 
 	mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
 			       pgprot_writecombine(PAGE_KERNEL));
-
+	if (!mtk_gem->kvaddr) {
+		kfree(sgt);
+		kfree(mtk_gem->pages);
+		return -ENOMEM;
+	}
 out:
 	kfree(sgt);
 	iosys_map_set_vaddr(map, mtk_gem->kvaddr);