We've had all the necessary changes ready for both shmem and hugetlbfs.  Turn
on all the shmem/hugetlbfs switches for userfaultfd-wp.

Now we can remove the flags parameter for vma_can_userfault() since not used
any more.  Meanwhile, we can expand UFFD_API_RANGE_IOCTLS_BASIC with
_UFFDIO_WRITEPROTECT too because all existing types now support write
protection mode.

Since vma_can_userfault() will be used elsewhere, move into userfaultfd_k.h.

Signed-off-by: Peter Xu <pet...@redhat.com>
---
 fs/userfaultfd.c                 | 18 ------------------
 include/linux/userfaultfd_k.h    | 14 ++++++++++++++
 include/uapi/linux/userfaultfd.h |  7 +++++--
 mm/userfaultfd.c                 | 10 +++-------
 4 files changed, 22 insertions(+), 27 deletions(-)

diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index f6fa34f58c37..b4f30fe84aa3 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1275,24 +1275,6 @@ static __always_inline int validate_range(struct 
mm_struct *mm,
        return 0;
 }
 
-static inline bool vma_can_userfault(struct vm_area_struct *vma,
-                                    unsigned long vm_flags)
-{
-       /* FIXME: add WP support to hugetlbfs and shmem */
-       if (vm_flags & VM_UFFD_WP) {
-               if (is_vm_hugetlb_page(vma) || vma_is_shmem(vma))
-                       return false;
-       }
-
-       if (vm_flags & VM_UFFD_MINOR) {
-               if (!(is_vm_hugetlb_page(vma) || vma_is_shmem(vma)))
-                       return false;
-       }
-
-       return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
-              vma_is_shmem(vma);
-}
-
 static int userfaultfd_register(struct userfaultfd_ctx *ctx,
                                unsigned long arg)
 {
diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h
index fefebe6e9656..413323fc81ca 100644
--- a/include/linux/userfaultfd_k.h
+++ b/include/linux/userfaultfd_k.h
@@ -16,6 +16,7 @@
 #include <linux/fcntl.h>
 #include <linux/mm.h>
 #include <asm-generic/pgtable_uffd.h>
+#include <linux/hugetlb_inline.h>
 
 /* The set of all possible UFFD-related VM flags. */
 #define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)
@@ -132,6 +133,19 @@ static inline bool userfaultfd_armed(struct vm_area_struct 
*vma)
        return vma->vm_flags & __VM_UFFD_FLAGS;
 }
 
+static inline bool vma_can_userfault(struct vm_area_struct *vma,
+                                    unsigned long vm_flags)
+{
+       if (vm_flags & VM_UFFD_MINOR) {
+               if (!(is_vm_hugetlb_page(vma) || vma_is_shmem(vma)))
+                       return false;
+       }
+
+       return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
+              vma_is_shmem(vma);
+}
+
+
 extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *);
 extern void dup_userfaultfd_complete(struct list_head *);
 
diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h
index 47d9790d863d..000cc4cfc787 100644
--- a/include/uapi/linux/userfaultfd.h
+++ b/include/uapi/linux/userfaultfd.h
@@ -32,7 +32,8 @@
                           UFFD_FEATURE_SIGBUS |                \
                           UFFD_FEATURE_THREAD_ID |             \
                           UFFD_FEATURE_MINOR_HUGETLBFS |       \
-                          UFFD_FEATURE_MINOR_SHMEM)
+                          UFFD_FEATURE_MINOR_SHMEM |           \
+                          UFFD_FEATURE_WP_HUGETLBFS_SHMEM)
 #define UFFD_API_IOCTLS                                \
        ((__u64)1 << _UFFDIO_REGISTER |         \
         (__u64)1 << _UFFDIO_UNREGISTER |       \
@@ -46,7 +47,8 @@
 #define UFFD_API_RANGE_IOCTLS_BASIC            \
        ((__u64)1 << _UFFDIO_WAKE |             \
         (__u64)1 << _UFFDIO_COPY |             \
-        (__u64)1 << _UFFDIO_CONTINUE)
+        (__u64)1 << _UFFDIO_CONTINUE |         \
+        (__u64)1 << _UFFDIO_WRITEPROTECT)
 
 /*
  * Valid ioctl command number range with this API is from 0x00 to
@@ -198,6 +200,7 @@ struct uffdio_api {
 #define UFFD_FEATURE_THREAD_ID                 (1<<8)
 #define UFFD_FEATURE_MINOR_HUGETLBFS           (1<<9)
 #define UFFD_FEATURE_MINOR_SHMEM               (1<<10)
+#define UFFD_FEATURE_WP_HUGETLBFS_SHMEM                (1<<11)
        __u64 features;
 
        __u64 ioctls;
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index a2b0dcc80a19..e6c6095878bb 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -452,7 +452,6 @@ static __always_inline ssize_t mfill_atomic_pte(struct 
mm_struct *dst_mm,
                        break;
                }
        } else {
-               VM_WARN_ON_ONCE(wp_copy);
                err = shmem_mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
                                             src_addr, mode, page, wp_copy);
        }
@@ -683,15 +682,12 @@ int mwriteprotect_range(struct mm_struct *dst_mm, 
unsigned long start,
 
        err = -ENOENT;
        dst_vma = find_dst_vma(dst_mm, start, len);
-       /*
-        * Make sure the vma is not shared, that the dst range is
-        * both valid and fully within a single existing vma.
-        */
-       if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
+
+       if (!dst_vma)
                goto out_unlock;
        if (!userfaultfd_wp(dst_vma))
                goto out_unlock;
-       if (!vma_is_anonymous(dst_vma))
+       if (!vma_can_userfault(dst_vma, dst_vma->vm_flags))
                goto out_unlock;
 
        if (is_vm_hugetlb_page(dst_vma)) {
-- 
2.26.2

Reply via email to