* [PATCH 1/2] mremap should return -ENOMEM when __vm_enough_memory fail
@ 2015-03-17 23:35 Derek
2015-03-17 23:35 ` [PATCH 2/2] clean up goto just return ERR_PTR Derek
0 siblings, 1 reply; 4+ messages in thread
From: Derek @ 2015-03-17 23:35 UTC (permalink / raw)
To: linux-mm, Kirill A. Shutemov, David Rientjes; +Cc: Derek Che
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
$ ./remap
allocated one page @0x7f686bf71000, (PAGE_SIZE: 4096)
grabbed 7680512000 bytes of memory (1875125 pages) @ 00007f6690993000.
mremap failed Bad address (14).
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? this 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>
---
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 related [flat|nested] 4+ messages in thread
* [PATCH 2/2] clean up goto just return ERR_PTR
2015-03-17 23:35 [PATCH 1/2] mremap should return -ENOMEM when __vm_enough_memory fail Derek
@ 2015-03-17 23:35 ` Derek
0 siblings, 0 replies; 4+ messages in thread
From: Derek @ 2015-03-17 23:35 UTC (permalink / raw)
To: linux-mm, Kirill A. Shutemov, David Rientjes; +Cc: Derek Che
As suggested by Kirill the "goto"s in vma_to_resize aren't
neccessary, just change them to explicit return.
Signed-off-by: Derek Che <crquan@ymail.com>
Suggested-by: "Kirill A. Shutemov" <kirill@shutemov.name>
Acked-by: David Rientjes <rientjes@google.com>
---
mm/mremap.c | 25 ++++++++-----------------
1 file changed, 8 insertions(+), 17 deletions(-)
diff --git a/mm/mremap.c b/mm/mremap.c
index 5da81cb..afa3ab7 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -339,25 +339,25 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
struct vm_area_struct *vma = find_vma(mm, addr);
if (!vma || vma->vm_start > addr)
- goto Efault;
+ return ERR_PTR(-EFAULT);
if (is_vm_hugetlb_page(vma))
- goto Einval;
+ return ERR_PTR(-EINVAL);
/* We can't remap across vm area boundaries */
if (old_len > vma->vm_end - addr)
- goto Efault;
+ return ERR_PTR(-EFAULT);
/* Need to be careful about a growing mapping */
if (new_len > old_len) {
unsigned long pgoff;
if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
- goto Efault;
+ return ERR_PTR(-EFAULT);
pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
pgoff += vma->vm_pgoff;
if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
- goto Einval;
+ return ERR_PTR(-EINVAL);
}
if (vma->vm_flags & VM_LOCKED) {
@@ -366,29 +366,20 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
lock_limit = rlimit(RLIMIT_MEMLOCK);
locked += new_len - old_len;
if (locked > lock_limit && !capable(CAP_IPC_LOCK))
- goto Eagain;
+ return ERR_PTR(-EAGAIN);
}
if (!may_expand_vm(mm, (new_len - old_len) >> PAGE_SHIFT))
- goto Enomem;
+ return ERR_PTR(-ENOMEM);
if (vma->vm_flags & VM_ACCOUNT) {
unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
if (security_vm_enough_memory_mm(mm, charged))
- goto Enomem;
+ return ERR_PTR(-ENOMEM);
*p = charged;
}
return vma;
-
-Efault: /* very odd choice for most of the cases, but... */
- return ERR_PTR(-EFAULT);
-Einval:
- return ERR_PTR(-EINVAL);
-Enomem:
- return ERR_PTR(-ENOMEM);
-Eagain:
- return ERR_PTR(-EAGAIN);
}
static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
--
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 related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] mremap should return -ENOMEM when __vm_enough_memory fail
2015-03-17 8:25 [PATCH 1/2] mremap should return -ENOMEM when __vm_enough_memory fail denc716
@ 2015-03-17 22:52 ` David Rientjes
0 siblings, 0 replies; 4+ messages in thread
From: David Rientjes @ 2015-03-17 22:52 UTC (permalink / raw)
To: denc716; +Cc: linux-mm, Kirill A. Shutemov, Derek Che
On Tue, 17 Mar 2015, denc716@gmail.com 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
>
> $ ./remap
> allocated one page @0x7f686bf71000, (PAGE_SIZE: 4096)
> grabbed 7680512000 bytes of memory (1875125 pages) @ 00007f6690993000.
> mremap failed Bad address (14).
>
> 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? this 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: "Kirill A. Shutemov" <kirill@shutemov.name>
> Acked-by: David Rientjes <rientjes@google.com>
Did Kirill ack this patch?
> ---
> 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;
> }
The patch is corrupted and won't apply because there aren't three lines
after the changed line.
--
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] 4+ messages in thread
* [PATCH 1/2] mremap should return -ENOMEM when __vm_enough_memory fail
@ 2015-03-17 8:25 denc716
2015-03-17 22:52 ` David Rientjes
0 siblings, 1 reply; 4+ messages in thread
From: denc716 @ 2015-03-17 8:25 UTC (permalink / raw)
To: linux-mm, Kirill A. Shutemov, David Rientjes; +Cc: Derek Che
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
$ ./remap
allocated one page @0x7f686bf71000, (PAGE_SIZE: 4096)
grabbed 7680512000 bytes of memory (1875125 pages) @ 00007f6690993000.
mremap failed Bad address (14).
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? this 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: "Kirill A. Shutemov" <kirill@shutemov.name>
Acked-by: David Rientjes <rientjes@google.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 related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-03-17 23:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-17 23:35 [PATCH 1/2] mremap should return -ENOMEM when __vm_enough_memory fail Derek
2015-03-17 23:35 ` [PATCH 2/2] clean up goto just return ERR_PTR Derek
-- strict thread matches above, loose matches on Subject: below --
2015-03-17 8:25 [PATCH 1/2] mremap should return -ENOMEM when __vm_enough_memory fail denc716
2015-03-17 22:52 ` David Rientjes
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).