linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Li Xinhai <lixinhai.lxh@gmail.com>
To: linux-mm@kvack.org
Cc: Mike Kravetz <mike.kravetz@oracle.com>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH] mm: introduce MAP_FIXED_HUGETLB_LEN to mmap()
Date: Fri, 27 Mar 2020 12:59:04 +0000	[thread overview]
Message-ID: <1585313944-8627-1-git-send-email-lixinhai.lxh@gmail.com> (raw)

The purpose of MAP_FIXED_HUGETLB_LEN is to check whether the parameter
length is valid or not according to the target file's huge page size.
When it is used, if length is not aligned to underlying huge page size,
mmap() is failed with errno set to EINVAL. When it is not used, the
current semantic is maintained, i.e., length is round up to underlying
huge page size.

In current code, the vma related call, except mmap, are all consider
not correctly aligned length as invalid parameter, including mprotect,
munmap, mlock, etc., by checking through hugetlb_vm_op_split. So, user
will see failure, after successfully call mmap, although using same
length parameter to other mapping syscall.

With MAP_FIXED_HUGETLB_LEN, user can choose to check if length is
correctly aligned at first place when call mmap, instead of failure after
mapping has been created.

Signed-off-by: Li Xinhai <lixinhai.lxh@gmail.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
---
 include/uapi/asm-generic/mman-common.h |  1 +
 mm/mmap.c                              | 17 +++++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h
index f94f65d..1c9ba97 100644
--- a/include/uapi/asm-generic/mman-common.h
+++ b/include/uapi/asm-generic/mman-common.h
@@ -21,6 +21,7 @@
 #define MAP_TYPE	0x0f		/* Mask for type of mapping */
 #define MAP_FIXED	0x10		/* Interpret addr exactly */
 #define MAP_ANONYMOUS	0x20		/* don't use a file */
+#define MAP_FIXED_HUGETLB_LEN	0x40	/* check alignment of addr, length, offset */
 
 /* 0x0100 - 0x4000 flags are defined in asm-generic/mman.h */
 #define MAP_POPULATE		0x008000	/* populate (prefault) pagetables */
diff --git a/mm/mmap.c b/mm/mmap.c
index d681a20..50a12e0 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -1560,9 +1560,18 @@ unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
 		file = fget(fd);
 		if (!file)
 			return -EBADF;
-		if (is_file_hugepages(file))
-			len = ALIGN(len, huge_page_size(hstate_file(file)));
+
 		retval = -EINVAL;
+		if (is_file_hugepages(file)) {
+			struct hstate *hs = hstate_file(file);
+
+			if (flags & MAP_FIXED_HUGETLB_LEN &&
+				len & ~(huge_page_mask(hs)))
+				goto out_fput;
+
+			len = ALIGN(len, huge_page_size(hs));
+		}
+
 		if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
 			goto out_fput;
 	} else if (flags & MAP_HUGETLB) {
@@ -1573,6 +1582,10 @@ unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
 		if (!hs)
 			return -EINVAL;
 
+		if (flags & MAP_FIXED_HUGETLB_LEN &&
+			len & ~(huge_page_mask(hs)))
+			return -EINVAL;
+
 		len = ALIGN(len, huge_page_size(hs));
 		/*
 		 * VM_NORESERVE is used because the reservations will be
-- 
1.8.3.1



             reply	other threads:[~2020-03-27 13:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-27 12:59 Li Xinhai [this message]
2020-03-27 19:12 ` [PATCH] mm: introduce MAP_FIXED_HUGETLB_LEN to mmap() John Hubbard
2020-03-28  1:31   ` Mike Kravetz
2020-03-28  2:19     ` Li Xinhai
2020-03-29  3:20       ` Li Xinhai
2020-03-28  2:14   ` Li Xinhai

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=1585313944-8627-1-git-send-email-lixinhai.lxh@gmail.com \
    --to=lixinhai.lxh@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-mm@kvack.org \
    --cc=mike.kravetz@oracle.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).