linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/mmap: Fix memory leak in mmap_region()
@ 2022-10-27  2:58 Li Zetao
  2022-10-27  7:30 ` Liam Howlett
  0 siblings, 1 reply; 5+ messages in thread
From: Li Zetao @ 2022-10-27  2:58 UTC (permalink / raw)
  To: akpm, Liam.Howlett, willy, catalin.marinas
  Cc: lizetao, mike.kravetz, cmllamas, linux-mm, linux-kernel, Li Zetao

There is a memory leak reported by kmemleak:

  unreferenced object 0xffff88817231ce40 (size 224):
    comm "mount.cifs", pid 19308, jiffies 4295917571 (age 405.880s)
    hex dump (first 32 bytes):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
      60 c0 b2 00 81 88 ff ff 98 83 01 42 81 88 ff ff  `..........B....
    backtrace:
      [<ffffffff81936171>] __alloc_file+0x21/0x250
      [<ffffffff81937051>] alloc_empty_file+0x41/0xf0
      [<ffffffff81937159>] alloc_file+0x59/0x710
      [<ffffffff81937964>] alloc_file_pseudo+0x154/0x210
      [<ffffffff81741dbf>] __shmem_file_setup+0xff/0x2a0
      [<ffffffff817502cd>] shmem_zero_setup+0x8d/0x160
      [<ffffffff817cc1d5>] mmap_region+0x1075/0x19d0
      [<ffffffff817cd257>] do_mmap+0x727/0x1110
      [<ffffffff817518b2>] vm_mmap_pgoff+0x112/0x1e0
      [<ffffffff83adf955>] do_syscall_64+0x35/0x80
      [<ffffffff83c0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0

The root cause was traced to an error handing path in mmap_region()
when arch_validate_flags() or mas_preallocate() fails. In the shared
anonymous mapping sence, vma will be setuped and mapped with a new
shared anonymous file via shmem_zero_setup(). So in this case, the
file resource needs to be released.

Fix it by calling fput(vma->vm_file) when arch_validate_flags() or
mas_preallocate() returns an error. And for the beauty of the code,
put fput() under mapping_unmap_writable().

Fixes: d4af56c5c7c6 ("mm: start tracking VMAs with maple tree")
Fixes: c462ac288f2c ("mm: Introduce arch_validate_flags()")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
 mm/mmap.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/mm/mmap.c b/mm/mmap.c
index e270057ed04e..8530195b3ec5 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2674,6 +2674,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 		error = -EINVAL;
 		if (file)
 			goto close_and_free_vma;
+		else if (vm_flags & VM_SHARED)
+			goto put_vma_file;
 		else
 			goto free_vma;
 	}
@@ -2682,6 +2684,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 		error = -ENOMEM;
 		if (file)
 			goto close_and_free_vma;
+		else if (vm_flags & VM_SHARED)
+			goto put_vma_file;
 		else
 			goto free_vma;
 	}
@@ -2746,13 +2750,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 	if (vma->vm_ops && vma->vm_ops->close)
 		vma->vm_ops->close(vma);
 unmap_and_free_vma:
-	fput(vma->vm_file);
-	vma->vm_file = NULL;
-
 	/* Undo any partial mapping done by a device driver. */
 	unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end);
 	if (vm_flags & VM_SHARED)
 		mapping_unmap_writable(file->f_mapping);
+put_vma_file:
+	fput(vma->vm_file);
+	vma->vm_file = NULL;
 free_vma:
 	vm_area_free(vma);
 unacct_error:
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm/mmap: Fix memory leak in mmap_region()
  2022-10-27  2:58 [PATCH] mm/mmap: Fix memory leak in mmap_region() Li Zetao
@ 2022-10-27  7:30 ` Liam Howlett
  2022-10-28  2:17   ` Li Zetao
  2022-10-28  7:37   ` [PATCH v2] " Li Zetao
  0 siblings, 2 replies; 5+ messages in thread
From: Liam Howlett @ 2022-10-27  7:30 UTC (permalink / raw)
  To: Li Zetao
  Cc: akpm, willy, catalin.marinas, lizetao, Mike Kravetz, cmllamas,
	linux-mm, linux-kernel

* Li Zetao <lizetao1@huawei.com> [221026 21:55]:
> There is a memory leak reported by kmemleak:
> 
>   unreferenced object 0xffff88817231ce40 (size 224):
>     comm "mount.cifs", pid 19308, jiffies 4295917571 (age 405.880s)
>     hex dump (first 32 bytes):
>       00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>       60 c0 b2 00 81 88 ff ff 98 83 01 42 81 88 ff ff  `..........B....
>     backtrace:
>       [<ffffffff81936171>] __alloc_file+0x21/0x250
>       [<ffffffff81937051>] alloc_empty_file+0x41/0xf0
>       [<ffffffff81937159>] alloc_file+0x59/0x710
>       [<ffffffff81937964>] alloc_file_pseudo+0x154/0x210
>       [<ffffffff81741dbf>] __shmem_file_setup+0xff/0x2a0
>       [<ffffffff817502cd>] shmem_zero_setup+0x8d/0x160
>       [<ffffffff817cc1d5>] mmap_region+0x1075/0x19d0
>       [<ffffffff817cd257>] do_mmap+0x727/0x1110
>       [<ffffffff817518b2>] vm_mmap_pgoff+0x112/0x1e0
>       [<ffffffff83adf955>] do_syscall_64+0x35/0x80
>       [<ffffffff83c0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0
> 
> The root cause was traced to an error handing path in mmap_region()
> when arch_validate_flags() or mas_preallocate() fails. In the shared
> anonymous mapping sence, vma will be setuped and mapped with a new
> shared anonymous file via shmem_zero_setup(). So in this case, the
> file resource needs to be released.
> 
> Fix it by calling fput(vma->vm_file) when arch_validate_flags() or
> mas_preallocate() returns an error. And for the beauty of the code,
> put fput() under mapping_unmap_writable().

It does look like the unrolling is in the wrong order in that section.

> 
> Fixes: d4af56c5c7c6 ("mm: start tracking VMAs with maple tree")
> Fixes: c462ac288f2c ("mm: Introduce arch_validate_flags()")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
>  mm/mmap.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/mmap.c b/mm/mmap.c
> index e270057ed04e..8530195b3ec5 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -2674,6 +2674,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>  		error = -EINVAL;
>  		if (file)
>  			goto close_and_free_vma;
> +		else if (vm_flags & VM_SHARED)
> +			goto put_vma_file;
>  		else
>  			goto free_vma;
>  	}
> @@ -2682,6 +2684,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>  		error = -ENOMEM;
>  		if (file)
>  			goto close_and_free_vma;
> +		else if (vm_flags & VM_SHARED)
> +			goto put_vma_file;
>  		else
>  			goto free_vma;
>  	}

I am not happy about this getting more complex as it already duplicates
too much code.

> @@ -2746,13 +2750,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>  	if (vma->vm_ops && vma->vm_ops->close)
>  		vma->vm_ops->close(vma);
>  unmap_and_free_vma:
> -	fput(vma->vm_file);
> -	vma->vm_file = NULL;
> -
>  	/* Undo any partial mapping done by a device driver. */
>  	unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end);
>  	if (vm_flags & VM_SHARED)

Could we change the above if to "if (file && vm_flags & VM_SHARED)" and
jump to unmap_and_free_vma "if (vma->vm_file)"?  We could then drop your
new goto label. I still think the reodering is correct and worth while.

Or am I missing something?

>  		mapping_unmap_writable(file->f_mapping);
> +put_vma_file:
> +	fput(vma->vm_file);
> +	vma->vm_file = NULL;
>  free_vma:
>  	vm_area_free(vma);
>  unacct_error:
> -- 
> 2.31.1
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm/mmap: Fix memory leak in mmap_region()
  2022-10-27  7:30 ` Liam Howlett
@ 2022-10-28  2:17   ` Li Zetao
  2022-10-28  7:37   ` [PATCH v2] " Li Zetao
  1 sibling, 0 replies; 5+ messages in thread
From: Li Zetao @ 2022-10-28  2:17 UTC (permalink / raw)
  To: Liam Howlett
  Cc: akpm, willy, catalin.marinas, lizetao1, Mike Kravetz, cmllamas,
	linux-mm, linux-kernel

Hi,

On 2022/10/27 15:30, Liam Howlett wrote:
> * Li Zetao <lizetao1@huawei.com> [221026 21:55]:
>> There is a memory leak reported by kmemleak:
>>
>>    unreferenced object 0xffff88817231ce40 (size 224):
>>      comm "mount.cifs", pid 19308, jiffies 4295917571 (age 405.880s)
>>      hex dump (first 32 bytes):
>>        00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>>        60 c0 b2 00 81 88 ff ff 98 83 01 42 81 88 ff ff  `..........B....
>>      backtrace:
>>        [<ffffffff81936171>] __alloc_file+0x21/0x250
>>        [<ffffffff81937051>] alloc_empty_file+0x41/0xf0
>>        [<ffffffff81937159>] alloc_file+0x59/0x710
>>        [<ffffffff81937964>] alloc_file_pseudo+0x154/0x210
>>        [<ffffffff81741dbf>] __shmem_file_setup+0xff/0x2a0
>>        [<ffffffff817502cd>] shmem_zero_setup+0x8d/0x160
>>        [<ffffffff817cc1d5>] mmap_region+0x1075/0x19d0
>>        [<ffffffff817cd257>] do_mmap+0x727/0x1110
>>        [<ffffffff817518b2>] vm_mmap_pgoff+0x112/0x1e0
>>        [<ffffffff83adf955>] do_syscall_64+0x35/0x80
>>        [<ffffffff83c0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0
>>
>> The root cause was traced to an error handing path in mmap_region()
>> when arch_validate_flags() or mas_preallocate() fails. In the shared
>> anonymous mapping sence, vma will be setuped and mapped with a new
>> shared anonymous file via shmem_zero_setup(). So in this case, the
>> file resource needs to be released.
>>
>> Fix it by calling fput(vma->vm_file) when arch_validate_flags() or
>> mas_preallocate() returns an error. And for the beauty of the code,
>> put fput() under mapping_unmap_writable().
> It does look like the unrolling is in the wrong order in that section.
>
>> Fixes: d4af56c5c7c6 ("mm: start tracking VMAs with maple tree")
>> Fixes: c462ac288f2c ("mm: Introduce arch_validate_flags()")
>> Signed-off-by: Li Zetao <lizetao1@huawei.com>
>> ---
>>   mm/mmap.c | 10 +++++++---
>>   1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/mm/mmap.c b/mm/mmap.c
>> index e270057ed04e..8530195b3ec5 100644
>> --- a/mm/mmap.c
>> +++ b/mm/mmap.c
>> @@ -2674,6 +2674,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>>   		error = -EINVAL;
>>   		if (file)
>>   			goto close_and_free_vma;I will modify it in the second version
>> +		else if (vm_flags & VM_SHARED)
>> +			goto put_vma_file;
>>   		else
>>   			goto free_vma;
>>   	}
>> @@ -2682,6 +2684,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>>   		error = -ENOMEM;
>>   		if (file)
>>   			goto close_and_free_vma;
>> +		else if (vm_flags & VM_SHARED)
>> +			goto put_vma_file;
>>   		else
>>   			goto free_vma;
>>   	}
> I am not happy about this getting more complex as it already duplicates
> too much code.

It's true that my modification would cause the code to further 
complicate. I will rework it in the

next version.

>> @@ -2746,13 +2750,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>>   	if (vma->vm_ops && vma->vm_ops->close)
>>   		vma->vm_ops->close(vma);
>>   unmap_and_free_vma:
>> -	fput(vma->vm_file);
>> -	vma->vm_file = NULL;
>> -
>>   	/* Undo any partial mapping done by a device driver. */
>>   	unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end);
>>   	if (vm_flags & VM_SHARED)
> Could we change the above if to "if (file && vm_flags & VM_SHARED)" and
> jump to unmap_and_free_vma "if (vma->vm_file)"?  We could then drop your
> new goto label. I still think the reodering is correct and worth while.
>
> Or am I missing something?
Thanks for your constructive comments.
>>   		mapping_unmap_writable(file->f_mapping);
>> +put_vma_file:
>> +	fput(vma->vm_file);
>> +	vma->vm_file = NULL;
>>   free_vma:
>>   	vm_area_free(vma);
>>   unacct_error:
>> -- 
>> 2.31.1

Regards,

Li Zetao



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] mm/mmap: Fix memory leak in mmap_region()
  2022-10-27  7:30 ` Liam Howlett
  2022-10-28  2:17   ` Li Zetao
@ 2022-10-28  7:37   ` Li Zetao
  2022-10-28 14:37     ` Liam Howlett
  1 sibling, 1 reply; 5+ messages in thread
From: Li Zetao @ 2022-10-28  7:37 UTC (permalink / raw)
  To: liam.howlett
  Cc: akpm, catalin.marinas, cmllamas, linux-kernel, linux-mm,
	lizetao1, mike.kravetz, willy

There is a memory leak reported by kmemleak:

  unreferenced object 0xffff88817231ce40 (size 224):
    comm "mount.cifs", pid 19308, jiffies 4295917571 (age 405.880s)
    hex dump (first 32 bytes):
      00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
      60 c0 b2 00 81 88 ff ff 98 83 01 42 81 88 ff ff  `..........B....
    backtrace:
      [<ffffffff81936171>] __alloc_file+0x21/0x250
      [<ffffffff81937051>] alloc_empty_file+0x41/0xf0
      [<ffffffff81937159>] alloc_file+0x59/0x710
      [<ffffffff81937964>] alloc_file_pseudo+0x154/0x210
      [<ffffffff81741dbf>] __shmem_file_setup+0xff/0x2a0
      [<ffffffff817502cd>] shmem_zero_setup+0x8d/0x160
      [<ffffffff817cc1d5>] mmap_region+0x1075/0x19d0
      [<ffffffff817cd257>] do_mmap+0x727/0x1110
      [<ffffffff817518b2>] vm_mmap_pgoff+0x112/0x1e0
      [<ffffffff83adf955>] do_syscall_64+0x35/0x80
      [<ffffffff83c0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0

The root cause was traced to an error handing path in mmap_region()
when arch_validate_flags() or mas_preallocate() fails. In the shared
anonymous mapping sence, vma will be setuped and mapped with a new
shared anonymous file via shmem_zero_setup(). So in this case, the
file resource needs to be released.

Fix it by calling fput(vma->vm_file) and unmap_region() when
arch_validate_flags() or mas_preallocate() returns an error in
the shared anonymous mapping sence.

Fixes: d4af56c5c7c6 ("mm: start tracking VMAs with maple tree")
Fixes: c462ac288f2c ("mm: Introduce arch_validate_flags()")
Signed-off-by: Li Zetao <lizetao1@huawei.com>
---
v1 was posted at: https://lore.kernel.org/all/20221027025837.136492-1-lizetao1@huawei.com/
v1 -> v2: Drop the new goto label, and jump to unmap_and_free_vma "if (vma->vm_file)"

 mm/mmap.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/mm/mmap.c b/mm/mmap.c
index e270057ed04e..77846d8cf9d4 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2674,6 +2674,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 		error = -EINVAL;
 		if (file)
 			goto close_and_free_vma;
+		else if (vma->vm_file)
+			goto unmap_and_free_vma;
 		else
 			goto free_vma;
 	}
@@ -2682,6 +2684,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 		error = -ENOMEM;
 		if (file)
 			goto close_and_free_vma;
+		else if (vma->vm_file)
+			goto unmap_and_free_vma;
 		else
 			goto free_vma;
 	}
@@ -2751,7 +2755,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 
 	/* Undo any partial mapping done by a device driver. */
 	unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end);
-	if (vm_flags & VM_SHARED)
+	if (file && (vm_flags & VM_SHARED))
 		mapping_unmap_writable(file->f_mapping);
 free_vma:
 	vm_area_free(vma);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] mm/mmap: Fix memory leak in mmap_region()
  2022-10-28  7:37   ` [PATCH v2] " Li Zetao
@ 2022-10-28 14:37     ` Liam Howlett
  0 siblings, 0 replies; 5+ messages in thread
From: Liam Howlett @ 2022-10-28 14:37 UTC (permalink / raw)
  To: Li Zetao
  Cc: akpm, catalin.marinas, cmllamas, linux-kernel, linux-mm,
	Mike Kravetz, willy

* Li Zetao <lizetao1@huawei.com> [221028 02:48]:
> There is a memory leak reported by kmemleak:
> 
>   unreferenced object 0xffff88817231ce40 (size 224):
>     comm "mount.cifs", pid 19308, jiffies 4295917571 (age 405.880s)
>     hex dump (first 32 bytes):
>       00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>       60 c0 b2 00 81 88 ff ff 98 83 01 42 81 88 ff ff  `..........B....
>     backtrace:
>       [<ffffffff81936171>] __alloc_file+0x21/0x250
>       [<ffffffff81937051>] alloc_empty_file+0x41/0xf0
>       [<ffffffff81937159>] alloc_file+0x59/0x710
>       [<ffffffff81937964>] alloc_file_pseudo+0x154/0x210
>       [<ffffffff81741dbf>] __shmem_file_setup+0xff/0x2a0
>       [<ffffffff817502cd>] shmem_zero_setup+0x8d/0x160
>       [<ffffffff817cc1d5>] mmap_region+0x1075/0x19d0
>       [<ffffffff817cd257>] do_mmap+0x727/0x1110
>       [<ffffffff817518b2>] vm_mmap_pgoff+0x112/0x1e0
>       [<ffffffff83adf955>] do_syscall_64+0x35/0x80
>       [<ffffffff83c0006a>] entry_SYSCALL_64_after_hwframe+0x46/0xb0
> 
> The root cause was traced to an error handing path in mmap_region()
> when arch_validate_flags() or mas_preallocate() fails. In the shared
> anonymous mapping sence, vma will be setuped and mapped with a new
> shared anonymous file via shmem_zero_setup(). So in this case, the
> file resource needs to be released.
> 
> Fix it by calling fput(vma->vm_file) and unmap_region() when
> arch_validate_flags() or mas_preallocate() returns an error in
> the shared anonymous mapping sence.
> 

Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com>

> Fixes: d4af56c5c7c6 ("mm: start tracking VMAs with maple tree")
> Fixes: c462ac288f2c ("mm: Introduce arch_validate_flags()")
> Signed-off-by: Li Zetao <lizetao1@huawei.com>
> ---
> v1 was posted at: https://lore.kernel.org/all/20221027025837.136492-1-lizetao1@huawei.com/
> v1 -> v2: Drop the new goto label, and jump to unmap_and_free_vma "if (vma->vm_file)"
> 
>  mm/mmap.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/mm/mmap.c b/mm/mmap.c
> index e270057ed04e..77846d8cf9d4 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -2674,6 +2674,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>  		error = -EINVAL;
>  		if (file)
>  			goto close_and_free_vma;
> +		else if (vma->vm_file)
> +			goto unmap_and_free_vma;
>  		else
>  			goto free_vma;
>  	}
> @@ -2682,6 +2684,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>  		error = -ENOMEM;
>  		if (file)
>  			goto close_and_free_vma;
> +		else if (vma->vm_file)
> +			goto unmap_and_free_vma;
>  		else
>  			goto free_vma;
>  	}
> @@ -2751,7 +2755,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>  
>  	/* Undo any partial mapping done by a device driver. */
>  	unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end);
> -	if (vm_flags & VM_SHARED)
> +	if (file && (vm_flags & VM_SHARED))
>  		mapping_unmap_writable(file->f_mapping);
>  free_vma:
>  	vm_area_free(vma);
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-10-28 14:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-27  2:58 [PATCH] mm/mmap: Fix memory leak in mmap_region() Li Zetao
2022-10-27  7:30 ` Liam Howlett
2022-10-28  2:17   ` Li Zetao
2022-10-28  7:37   ` [PATCH v2] " Li Zetao
2022-10-28 14:37     ` Liam Howlett

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).