]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
userfaultfd: hugetlbfs: allow registration of ranges containing huge pages
authorMike Kravetz <mike.kravetz@oracle.com>
Thu, 12 Jan 2017 01:19:16 +0000 (12:19 +1100)
committerDhaval Giani <dhaval.giani@oracle.com>
Fri, 20 Jan 2017 18:55:49 +0000 (13:55 -0500)
Orabug: 21685254

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.

Link: http://lkml.kernel.org/r/20161216144821.5183-22-aarcange@redhat.com
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Cc: Hillf Danton <hillf.zj@alibaba-inc.com>
Cc: Michael Rapoport <RAPOPORT@il.ibm.com>
Cc: Mike Rapoport <rppt@linux.vnet.ibm.com>
Cc: Pavel Emelyanov <xemul@parallels.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from linux-next next-20170117
 commit 6be4576b101b7026f72ec240f393c1dd5dfa02da)
Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com>
Reviewed-by: Dhaval Giani <dhaval.giani@oracle.com>
Conflicts:
fs/userfaultfd.c

Signed-off-by: Dhaval Giani <dhaval.giani@oracle.com>
fs/userfaultfd.c
include/uapi/linux/userfaultfd.h

index 2d97952e341a859ce977ddbd8968536a64c6290c..871ae23afaee1544f2a803038583a9d22078e716 100644 (file)
@@ -26,6 +26,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;
 
@@ -728,6 +729,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;
@@ -778,6 +780,17 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
        if (vma->vm_start >= end)
                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.
         *
@@ -786,6 +799,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();
 
@@ -794,8 +808,21 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 
                /* check not compatible vmas */
                ret = -EINVAL;
-               if (cur->vm_ops)
+               if (cur->vm_ops && !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
@@ -808,6 +835,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);
@@ -819,7 +852,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
        do {
                cond_resched();
 
-               BUG_ON(vma->vm_ops);
+               BUG_ON(vma->vm_ops && !is_vm_hugetlb_page(vma));
                BUG_ON(vma->vm_userfaultfd_ctx.ctx &&
                       vma->vm_userfaultfd_ctx.ctx != ctx);
 
@@ -877,7 +910,8 @@ out_unlock:
                 * 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;
        }
@@ -923,6 +957,17 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
        if (vma->vm_start >= end)
                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.
         *
@@ -945,7 +990,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
                 * provides for more strict behavior to notice
                 * unregistration errors.
                 */
-               if (cur->vm_ops)
+               if (cur->vm_ops && !is_vm_hugetlb_page(cur))
                        goto out_unlock;
 
                found = true;
@@ -959,7 +1004,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
        do {
                cond_resched();
 
-               BUG_ON(vma->vm_ops);
+               BUG_ON(vma->vm_ops && !is_vm_hugetlb_page(vma));
 
                /*
                 * Nothing to do: this vma is already registered into this
index 9057d7af3ae145ba711c837f4bcbe58e851f2320..751d8147b185958bb33bc8c472e3a16bd1aa7c72 100644 (file)
@@ -26,6 +26,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