linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mremap should return -ENOMEM when __vm_enough_memory fail
@ 2015-03-13  9:21 Derek
  2015-03-13 10:39 ` Kirill A. Shutemov
  2015-03-13 23:08 ` David Rientjes
  0 siblings, 2 replies; 3+ messages in thread
From: Derek @ 2015-03-13  9:21 UTC (permalink / raw)
  To: linux-mm

Recently I straced bash behavior in this dd zero pipe to read test,
in part of testing under vm.overcommit_memory=2 (OVERCOMMIT_NEVER mode):
    # dd if=/dev/zero | read x

The bash sub shell is calling mremap to reallocate more and more memory
untill it finally failed -ENOMEM (I expect), or to be killed by system
OOM killer (which should not happen under OVERCOMMIT_NEVER mode);
But the mremap system call actually failed of -EFAULT, which is a surprise
to me, I think it's supposed to be -ENOMEM? then I wrote this piece
of C code testing confirmed it:
https://gist.github.com/crquan/326bde37e1ddda8effe5

The -EFAULT comes from the branch of security_vm_enough_memory_mm failure,
underlyingly it calls __vm_enough_memory which returns only 0 for success
or -ENOMEM; So why vma_to_resize needs to return -EFAULT in this case?
it sounds like a mistake to me.

Some more digging into git history:
1) Before commit 119f657c7 in May 1 2005 (pre 2.6.12 days) it was returning
   -ENOMEM for this failure;
2) but commit 119f657c7 changed it accidentally, to what ever is preserved
   in local ret, which happened to be -EFAULT, in a previous assignment;
3) then in commit 54f5de709 code refactoring, it's explicitly returning
   -EFAULT, should be wrong.

Signed-off-by: Derek Che <crquan@ymail.com>
---
 mm/mremap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/mremap.c b/mm/mremap.c
index 57dadc0..5da81cb 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -375,7 +375,7 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (vma->vm_flags & VM_ACCOUNT) {
 		unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
 		if (security_vm_enough_memory_mm(mm, charged))
-			goto Efault;
+			goto Enomem;
 		*p = charged;
 	}
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mremap should return -ENOMEM when __vm_enough_memory fail
  2015-03-13  9:21 [PATCH] mremap should return -ENOMEM when __vm_enough_memory fail Derek
@ 2015-03-13 10:39 ` Kirill A. Shutemov
  2015-03-13 23:08 ` David Rientjes
  1 sibling, 0 replies; 3+ messages in thread
From: Kirill A. Shutemov @ 2015-03-13 10:39 UTC (permalink / raw)
  To: Derek; +Cc: linux-mm

On Fri, Mar 13, 2015 at 02:21:38AM -0700, Derek wrote:
> Recently I straced bash behavior in this dd zero pipe to read test,
> in part of testing under vm.overcommit_memory=2 (OVERCOMMIT_NEVER mode):
>     # dd if=/dev/zero | read x
> 
> The bash sub shell is calling mremap to reallocate more and more memory
> untill it finally failed -ENOMEM (I expect), or to be killed by system
> OOM killer (which should not happen under OVERCOMMIT_NEVER mode);
> But the mremap system call actually failed of -EFAULT, which is a surprise
> to me, I think it's supposed to be -ENOMEM? then I wrote this piece
> of C code testing confirmed it:
> https://gist.github.com/crquan/326bde37e1ddda8effe5
> 
> The -EFAULT comes from the branch of security_vm_enough_memory_mm failure,
> underlyingly it calls __vm_enough_memory which returns only 0 for success
> or -ENOMEM; So why vma_to_resize needs to return -EFAULT in this case?
> it sounds like a mistake to me.
> 
> Some more digging into git history:
> 1) Before commit 119f657c7 in May 1 2005 (pre 2.6.12 days) it was returning
>    -ENOMEM for this failure;
> 2) but commit 119f657c7 changed it accidentally, to what ever is preserved
>    in local ret, which happened to be -EFAULT, in a previous assignment;
> 3) then in commit 54f5de709 code refactoring, it's explicitly returning
>    -EFAULT, should be wrong.
> 
> Signed-off-by: Derek Che <crquan@ymail.com>
> ---
>  mm/mremap.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/mm/mremap.c b/mm/mremap.c
> index 57dadc0..5da81cb 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -375,7 +375,7 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
>  	if (vma->vm_flags & VM_ACCOUNT) {
>  		unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
>  		if (security_vm_enough_memory_mm(mm, charged))
> -			goto Efault;
> +			goto Enomem;
>  		*p = charged;
>  	}

Looks good to me.
But that would be nice to get rid of these pointless gotos. Just plain
return would work too.

-- 
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: [PATCH] mremap should return -ENOMEM when __vm_enough_memory fail
  2015-03-13  9:21 [PATCH] mremap should return -ENOMEM when __vm_enough_memory fail Derek
  2015-03-13 10:39 ` Kirill A. Shutemov
@ 2015-03-13 23:08 ` David Rientjes
  1 sibling, 0 replies; 3+ messages in thread
From: David Rientjes @ 2015-03-13 23:08 UTC (permalink / raw)
  To: Derek; +Cc: linux-mm

On Fri, 13 Mar 2015, Derek wrote:

> Recently I straced bash behavior in this dd zero pipe to read test,
> in part of testing under vm.overcommit_memory=2 (OVERCOMMIT_NEVER mode):
>     # dd if=/dev/zero | read x
> 
> The bash sub shell is calling mremap to reallocate more and more memory
> untill it finally failed -ENOMEM (I expect), or to be killed by system
> OOM killer (which should not happen under OVERCOMMIT_NEVER mode);
> But the mremap system call actually failed of -EFAULT, which is a surprise
> to me, I think it's supposed to be -ENOMEM? then I wrote this piece
> of C code testing confirmed it:
> https://gist.github.com/crquan/326bde37e1ddda8effe5
> 
> The -EFAULT comes from the branch of security_vm_enough_memory_mm failure,
> underlyingly it calls __vm_enough_memory which returns only 0 for success
> or -ENOMEM; So why vma_to_resize needs to return -EFAULT in this case?
> it sounds like a mistake to me.
> 
> Some more digging into git history:
> 1) Before commit 119f657c7 in May 1 2005 (pre 2.6.12 days) it was returning
>    -ENOMEM for this failure;
> 2) but commit 119f657c7 changed it accidentally, to what ever is preserved
>    in local ret, which happened to be -EFAULT, in a previous assignment;
> 3) then in commit 54f5de709 code refactoring, it's explicitly returning
>    -EFAULT, should be wrong.
> 
> Signed-off-by: Derek Che <crquan@ymail.com>

Acked-by: David Rientjes <rientjes@google.com>

vma_to_resize() could certainly be cleaned up to just return ERR_PTR() and 
avoiding the "goto"s since there is no other cleanup needed as suggested 
by Kirill if you have time for a cleanup patch on top of this.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2015-03-13 23:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-13  9:21 [PATCH] mremap should return -ENOMEM when __vm_enough_memory fail Derek
2015-03-13 10:39 ` Kirill A. Shutemov
2015-03-13 23:08 ` David Rientjes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox