From: Piotr Kwapulinski <kwapulinski.piotr@gmail.com>
To: akpm@linux-foundation.org
Cc: mhocko@suse.com, mtk.manpages@gmail.com, cmetcalf@mellanox.com,
arnd@arndb.de, viro@zeniv.linux.org.uk, mszeredi@suse.cz,
dave@stgolabs.net, kirill.shutemov@linux.intel.com,
vbabka@suse.cz, mingo@kernel.org, dan.j.williams@intel.com,
dave.hansen@linux.intel.com, koct9i@gmail.com,
hannes@cmpxchg.org, jack@suse.cz, xiexiuqi@huawei.com,
iamjoonsoo.kim@lge.com, oleg@redhat.com,
gang.chen.5i5j@gmail.com, aarcange@redhat.com,
aryabinin@virtuozzo.com, rientjes@google.com, denc716@gmail.com,
toshi.kani@hpe.com, ldufour@linux.vnet.ibm.com,
kuleshovmail@gmail.com, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, linux-arch@vger.kernel.org,
Piotr Kwapulinski <kwapulinski.piotr@gmail.com>
Subject: [PATCH 2/3] mm/mremap.c: don't unmap the overlapping VMA(s)
Date: Sat, 2 Apr 2016 21:17:33 +0200 [thread overview]
Message-ID: <1459624654-7955-3-git-send-email-kwapulinski.piotr@gmail.com> (raw)
In-Reply-To: <1459624654-7955-1-git-send-email-kwapulinski.piotr@gmail.com>
Currently the
mremap(new_size, MREMAP_MAYMOVE | MREMAP_FIXED, new_address)
discards the part of existing VMA(s) if it overlaps the memory region
specified by new_address and new_size.
Introduce the new MREMAP_DONTUNMAP flag which forces the mremap to
fail with ENOMEM whenever the overlapping occurs. No existing
mapping(s) is discarded.
The implementation tests the MAP_DONTUNMAP flag and scans the AS for
the overlapping VMA(s) right before unmapping the area.
I did the isolated tests and also tested it with Gentoo full
installation.
Signed-off-by: Piotr Kwapulinski <kwapulinski.piotr@gmail.com>
---
include/uapi/linux/mman.h | 5 +++--
mm/mremap.c | 23 +++++++++++++++++------
2 files changed, 20 insertions(+), 8 deletions(-)
diff --git a/include/uapi/linux/mman.h b/include/uapi/linux/mman.h
index ade4acd..bc6478e 100644
--- a/include/uapi/linux/mman.h
+++ b/include/uapi/linux/mman.h
@@ -3,8 +3,9 @@
#include <asm/mman.h>
-#define MREMAP_MAYMOVE 1
-#define MREMAP_FIXED 2
+#define MREMAP_MAYMOVE 1
+#define MREMAP_FIXED 2
+#define MREMAP_DONTUNMAP 4
#define OVERCOMMIT_GUESS 0
#define OVERCOMMIT_ALWAYS 1
diff --git a/mm/mremap.c b/mm/mremap.c
index 3fa0a467..f57d396 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -397,7 +397,8 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
}
static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
- unsigned long new_addr, unsigned long new_len, bool *locked)
+ unsigned long new_addr, unsigned long new_len,
+ unsigned long flags, bool *locked)
{
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma;
@@ -415,9 +416,16 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
if (addr + old_len > new_addr && new_addr + new_len > addr)
goto out;
- ret = do_munmap(mm, new_addr, new_len);
- if (ret)
- goto out;
+ if (flags & MREMAP_DONTUNMAP) {
+ if (find_vma_intersection(mm, new_addr, new_len)) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ } else {
+ ret = do_munmap(mm, new_addr, new_len);
+ if (ret)
+ goto out;
+ }
if (old_len >= new_len) {
ret = do_munmap(mm, addr+new_len, old_len - new_len);
@@ -482,12 +490,15 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
unsigned long charged = 0;
bool locked = false;
- if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
+ if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE | MREMAP_DONTUNMAP))
return ret;
if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
return ret;
+ if (flags & MREMAP_DONTUNMAP && !(flags & MREMAP_FIXED))
+ return ret;
+
if (offset_in_page(addr))
return ret;
@@ -505,7 +516,7 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
down_write(¤t->mm->mmap_sem);
if (flags & MREMAP_FIXED) {
- ret = mremap_to(addr, old_len, new_addr, new_len,
+ ret = mremap_to(addr, old_len, new_addr, new_len, flags,
&locked);
goto out;
}
--
2.7.4
--
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>
next prev parent reply other threads:[~2016-04-02 19:18 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-02 19:17 [PATCH 0/3] mm/mmap.c: don't unmap the overlapping VMA(s) Piotr Kwapulinski
2016-04-02 19:17 ` [PATCH 1/3] man/mmap.2: " Piotr Kwapulinski
2016-04-02 19:17 ` Piotr Kwapulinski [this message]
2016-04-02 19:17 ` [PATCH 3/3] man/mremap.2: " Piotr Kwapulinski
2016-04-02 21:54 ` [PATCH 0/3] mm/mmap.c: " Kirill A. Shutemov
2016-04-03 5:52 ` Konstantin Khlebnikov
2016-04-04 7:31 ` Michal Hocko
2016-04-04 15:26 ` Vlastimil Babka
2016-04-07 16:11 ` Piotr Kwapulinski
2016-04-07 16:31 ` Michal Hocko
2016-04-08 15:32 ` Piotr Kwapulinski
2016-04-07 16:20 ` Piotr Kwapulinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1459624654-7955-3-git-send-email-kwapulinski.piotr@gmail.com \
--to=kwapulinski.piotr@gmail.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=aryabinin@virtuozzo.com \
--cc=cmetcalf@mellanox.com \
--cc=dan.j.williams@intel.com \
--cc=dave.hansen@linux.intel.com \
--cc=dave@stgolabs.net \
--cc=denc716@gmail.com \
--cc=gang.chen.5i5j@gmail.com \
--cc=hannes@cmpxchg.org \
--cc=iamjoonsoo.kim@lge.com \
--cc=jack@suse.cz \
--cc=kirill.shutemov@linux.intel.com \
--cc=koct9i@gmail.com \
--cc=kuleshovmail@gmail.com \
--cc=ldufour@linux.vnet.ibm.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=mingo@kernel.org \
--cc=mszeredi@suse.cz \
--cc=mtk.manpages@gmail.com \
--cc=oleg@redhat.com \
--cc=rientjes@google.com \
--cc=toshi.kani@hpe.com \
--cc=vbabka@suse.cz \
--cc=viro@zeniv.linux.org.uk \
--cc=xiexiuqi@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).