From: Andrea Arcangeli <aarcange@redhat.com>
To: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>
Cc: Michael Rapoport <RAPOPORT@il.ibm.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
Mike Kravetz <mike.kravetz@oracle.com>,
Pavel Emelyanov <xemul@parallels.com>,
Hillf Danton <hillf.zj@alibaba-inc.com>
Subject: [PATCH 21/42] userfaultfd: hugetlbfs: allow registration of ranges containing huge pages
Date: Fri, 16 Dec 2016 15:48:00 +0100 [thread overview]
Message-ID: <20161216144821.5183-22-aarcange@redhat.com> (raw)
In-Reply-To: <20161216144821.5183-1-aarcange@redhat.com>
From: Mike Kravetz <mike.kravetz@oracle.com>
Expand the userfaultfd_register/unregister routines to allow VM_HUGETLB
vmas. huge page alignment checking is performed after a VM_HUGETLB
vma is encountered.
Also, since there is no UFFDIO_ZEROPAGE support for huge pages do not
return that as a valid ioctl method for huge page ranges.
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
---
fs/userfaultfd.c | 55 ++++++++++++++++++++++++++++++++++++----
include/uapi/linux/userfaultfd.h | 3 +++
2 files changed, 53 insertions(+), 5 deletions(-)
diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 22f978d..1268496 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -27,6 +27,7 @@
#include <linux/mempolicy.h>
#include <linux/ioctl.h>
#include <linux/security.h>
+#include <linux/hugetlb.h>
static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly;
@@ -1025,6 +1026,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
struct uffdio_register __user *user_uffdio_register;
unsigned long vm_flags, new_flags;
bool found;
+ bool huge_pages;
unsigned long start, end, vma_end;
user_uffdio_register = (struct uffdio_register __user *) arg;
@@ -1076,6 +1078,17 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
goto out_unlock;
/*
+ * If the first vma contains huge pages, make sure start address
+ * is aligned to huge page size.
+ */
+ if (is_vm_hugetlb_page(vma)) {
+ unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
+
+ if (start & (vma_hpagesize - 1))
+ goto out_unlock;
+ }
+
+ /*
* Search for not compatible vmas.
*
* FIXME: this shall be relaxed later so that it doesn't fail
@@ -1083,6 +1096,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
* on anonymous vmas).
*/
found = false;
+ huge_pages = false;
for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
cond_resched();
@@ -1091,8 +1105,21 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
/* check not compatible vmas */
ret = -EINVAL;
- if (!vma_is_anonymous(cur))
+ if (!vma_is_anonymous(cur) && !is_vm_hugetlb_page(cur))
goto out_unlock;
+ /*
+ * If this vma contains ending address, and huge pages
+ * check alignment.
+ */
+ if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
+ end > cur->vm_start) {
+ unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
+
+ ret = -EINVAL;
+
+ if (end & (vma_hpagesize - 1))
+ goto out_unlock;
+ }
/*
* Check that this vma isn't already owned by a
@@ -1105,6 +1132,12 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
cur->vm_userfaultfd_ctx.ctx != ctx)
goto out_unlock;
+ /*
+ * Note vmas containing huge pages
+ */
+ if (is_vm_hugetlb_page(cur))
+ huge_pages = true;
+
found = true;
}
BUG_ON(!found);
@@ -1116,7 +1149,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
do {
cond_resched();
- BUG_ON(!vma_is_anonymous(vma));
+ BUG_ON(!vma_is_anonymous(vma) && !is_vm_hugetlb_page(vma));
BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
vma->vm_userfaultfd_ctx.ctx != ctx);
@@ -1174,7 +1207,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
* userland which ioctls methods are guaranteed to
* succeed on this range.
*/
- if (put_user(UFFD_API_RANGE_IOCTLS,
+ if (put_user(huge_pages ? UFFD_API_RANGE_IOCTLS_HPAGE :
+ UFFD_API_RANGE_IOCTLS,
&user_uffdio_register->ioctls))
ret = -EFAULT;
}
@@ -1221,6 +1255,17 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
goto out_unlock;
/*
+ * If the first vma contains huge pages, make sure start address
+ * is aligned to huge page size.
+ */
+ if (is_vm_hugetlb_page(vma)) {
+ unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
+
+ if (start & (vma_hpagesize - 1))
+ goto out_unlock;
+ }
+
+ /*
* Search for not compatible vmas.
*
* FIXME: this shall be relaxed later so that it doesn't fail
@@ -1242,7 +1287,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
* provides for more strict behavior to notice
* unregistration errors.
*/
- if (!vma_is_anonymous(cur))
+ if (!vma_is_anonymous(cur) && !is_vm_hugetlb_page(cur))
goto out_unlock;
found = true;
@@ -1256,7 +1301,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
do {
cond_resched();
- BUG_ON(!vma_is_anonymous(vma));
+ BUG_ON(!vma_is_anonymous(vma) && !is_vm_hugetlb_page(vma));
/*
* Nothing to do: this vma is already registered into this
diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h
index 2bbf323..a3828a9 100644
--- a/include/uapi/linux/userfaultfd.h
+++ b/include/uapi/linux/userfaultfd.h
@@ -29,6 +29,9 @@
((__u64)1 << _UFFDIO_WAKE | \
(__u64)1 << _UFFDIO_COPY | \
(__u64)1 << _UFFDIO_ZEROPAGE)
+#define UFFD_API_RANGE_IOCTLS_HPAGE \
+ ((__u64)1 << _UFFDIO_WAKE | \
+ (__u64)1 << _UFFDIO_COPY)
/*
* Valid ioctl command number range with this API is from 0x00 to
--
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-12-16 14:48 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-16 14:47 [PATCH 00/42] userfaultfd tmpfs/hugetlbfs/non-cooperative v2 Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 01/42] userfaultfd: document _IOR/_IOW Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 02/42] userfaultfd: correct comment about UFFD_FEATURE_PAGEFAULT_FLAG_WP Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 03/42] userfaultfd: convert BUG() to WARN_ON_ONCE() Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 04/42] userfaultfd: use vma_is_anonymous Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 05/42] userfaultfd: non-cooperative: Split the find_userfault() routine Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 06/42] userfaultfd: non-cooperative: Add ability to report non-PF events from uffd descriptor Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 07/42] userfaultfd: non-cooperative: report all available features to userland Andrea Arcangeli
2017-01-04 23:01 ` Andrew Morton
2017-01-05 12:56 ` Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 08/42] userfaultfd: non-cooperative: Add fork() event Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 09/42] userfaultfd: non-cooperative: Add fork() event, build warning fix Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 10/42] userfaultfd: non-cooperative: dup_userfaultfd: use mm_count instead of mm_users Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 11/42] userfaultfd: non-cooperative: Add mremap() event Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 12/42] userfaultfd: non-cooperative: optimize mremap_userfaultfd_complete() Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 13/42] userfaultfd: non-cooperative: Add madvise() event for MADV_DONTNEED request Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 14/42] userfaultfd: non-cooperative: avoid MADV_DONTNEED race condition Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 15/42] userfaultfd: non-cooperative: wake userfaults after UFFDIO_UNREGISTER Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 16/42] userfaultfd: hugetlbfs: add copy_huge_page_from_user for hugetlb userfaultfd support Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 17/42] userfaultfd: hugetlbfs: add hugetlb_mcopy_atomic_pte for " Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 18/42] userfaultfd: hugetlbfs: add __mcopy_atomic_hugetlb for huge page UFFDIO_COPY Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 19/42] userfaultfd: hugetlbfs: fix __mcopy_atomic_hugetlb retry/error processing Andrea Arcangeli
2016-12-16 14:47 ` [PATCH 20/42] userfaultfd: hugetlbfs: add userfaultfd hugetlb hook Andrea Arcangeli
2016-12-16 14:48 ` Andrea Arcangeli [this message]
2016-12-16 14:48 ` [PATCH 22/42] userfaultfd: hugetlbfs: add userfaultfd_hugetlb test Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 23/42] userfaultfd: hugetlbfs: userfaultfd_huge_must_wait for hugepmd ranges Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 24/42] userfaultfd: hugetlbfs: gup: support VM_FAULT_RETRY Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 25/42] userfaultfd: hugetlbfs: reserve count on error in __mcopy_atomic_hugetlb Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 26/42] userfaultfd: hugetlbfs: UFFD_FEATURE_MISSING_HUGETLBFS Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 27/42] userfaultfd: introduce vma_can_userfault Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 28/42] userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 29/42] userfaultfd: shmem: introduce vma_is_shmem Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 30/42] userfaultfd: shmem: add tlbflush.h header for microblaze Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 31/42] userfaultfd: shmem: use shmem_mcopy_atomic_pte for shared memory Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 32/42] userfaultfd: shmem: add userfaultfd hook for shared memory faults Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 33/42] userfaultfd: shmem: allow registration of shared memory ranges Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 34/42] userfaultfd: shmem: add userfaultfd_shmem test Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 35/42] userfaultfd: shmem: lock the page before adding it to pagecache Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 36/42] userfaultfd: shmem: avoid leaking blocks and used blocks in UFFDIO_COPY Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 37/42] userfaultfd: hugetlbfs: UFFD_FEATURE_MISSING_SHMEM Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 38/42] userfaultfd: non-cooperative: selftest: introduce userfaultfd_open Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 39/42] userfaultfd: non-cooperative: selftest: add ufd parameter to copy_page Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 40/42] userfaultfd: non-cooperative: selftest: add test for FORK, MADVDONTNEED and REMAP events Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 41/42] userfaultfd: selftest: test UFFDIO_ZEROPAGE on all memory types Andrea Arcangeli
2016-12-16 14:48 ` [PATCH 42/42] mm: mprotect: use pmd_trans_unstable instead of taking the pmd_lock Andrea Arcangeli
2017-01-06 19:02 ` [PATCH 00/42] userfaultfd tmpfs/hugetlbfs/non-cooperative v2 Dr. David Alan Gilbert
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=20161216144821.5183-22-aarcange@redhat.com \
--to=aarcange@redhat.com \
--cc=RAPOPORT@il.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=dgilbert@redhat.com \
--cc=hillf.zj@alibaba-inc.com \
--cc=linux-mm@kvack.org \
--cc=mike.kravetz@oracle.com \
--cc=xemul@parallels.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).