Skip to content

Commit b1f9e87

Browse files
xzpeterakpm00
authored andcommitted
mm/uffd: enable write protection for shmem & hugetlbfs
We've had all the necessary changes ready for both shmem and hugetlbfs. Turn on all the shmem/hugetlbfs switches for userfaultfd-wp. 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. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Peter Xu <[email protected]> Cc: Alistair Popple <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Axel Rasmussen <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jerome Glisse <[email protected]> Cc: "Kirill A . Shutemov" <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Nadav Amit <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 8e165e7 commit b1f9e87

File tree

4 files changed

+34
-26
lines changed

4 files changed

+34
-26
lines changed

fs/userfaultfd.c

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,24 +1258,6 @@ static __always_inline int validate_range(struct mm_struct *mm,
12581258
return 0;
12591259
}
12601260

1261-
static inline bool vma_can_userfault(struct vm_area_struct *vma,
1262-
unsigned long vm_flags)
1263-
{
1264-
/* FIXME: add WP support to hugetlbfs and shmem */
1265-
if (vm_flags & VM_UFFD_WP) {
1266-
if (is_vm_hugetlb_page(vma) || vma_is_shmem(vma))
1267-
return false;
1268-
}
1269-
1270-
if (vm_flags & VM_UFFD_MINOR) {
1271-
if (!(is_vm_hugetlb_page(vma) || vma_is_shmem(vma)))
1272-
return false;
1273-
}
1274-
1275-
return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1276-
vma_is_shmem(vma);
1277-
}
1278-
12791261
static int userfaultfd_register(struct userfaultfd_ctx *ctx,
12801262
unsigned long arg)
12811263
{
@@ -1956,6 +1938,9 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx,
19561938
#endif
19571939
#ifndef CONFIG_HAVE_ARCH_USERFAULTFD_WP
19581940
uffdio_api.features &= ~UFFD_FEATURE_PAGEFAULT_FLAG_WP;
1941+
#endif
1942+
#ifndef CONFIG_PTE_MARKER_UFFD_WP
1943+
uffdio_api.features &= ~UFFD_FEATURE_WP_HUGETLBFS_SHMEM;
19591944
#endif
19601945
uffdio_api.ioctls = UFFD_API_IOCTLS;
19611946
ret = -EFAULT;

include/linux/userfaultfd_k.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/swap.h>
1919
#include <linux/swapops.h>
2020
#include <asm-generic/pgtable_uffd.h>
21+
#include <linux/hugetlb_inline.h>
2122

2223
/* The set of all possible UFFD-related VM flags. */
2324
#define __VM_UFFD_FLAGS (VM_UFFD_MISSING | VM_UFFD_WP | VM_UFFD_MINOR)
@@ -140,6 +141,25 @@ static inline bool userfaultfd_armed(struct vm_area_struct *vma)
140141
return vma->vm_flags & __VM_UFFD_FLAGS;
141142
}
142143

144+
static inline bool vma_can_userfault(struct vm_area_struct *vma,
145+
unsigned long vm_flags)
146+
{
147+
if (vm_flags & VM_UFFD_MINOR)
148+
return is_vm_hugetlb_page(vma) || vma_is_shmem(vma);
149+
150+
#ifndef CONFIG_PTE_MARKER_UFFD_WP
151+
/*
152+
* If user requested uffd-wp but not enabled pte markers for
153+
* uffd-wp, then shmem & hugetlbfs are not supported but only
154+
* anonymous.
155+
*/
156+
if ((vm_flags & VM_UFFD_WP) && !vma_is_anonymous(vma))
157+
return false;
158+
#endif
159+
return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
160+
vma_is_shmem(vma);
161+
}
162+
143163
extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *);
144164
extern void dup_userfaultfd_complete(struct list_head *);
145165

include/uapi/linux/userfaultfd.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
UFFD_FEATURE_THREAD_ID | \
3434
UFFD_FEATURE_MINOR_HUGETLBFS | \
3535
UFFD_FEATURE_MINOR_SHMEM | \
36-
UFFD_FEATURE_EXACT_ADDRESS)
36+
UFFD_FEATURE_EXACT_ADDRESS | \
37+
UFFD_FEATURE_WP_HUGETLBFS_SHMEM)
3738
#define UFFD_API_IOCTLS \
3839
((__u64)1 << _UFFDIO_REGISTER | \
3940
(__u64)1 << _UFFDIO_UNREGISTER | \
@@ -47,7 +48,8 @@
4748
#define UFFD_API_RANGE_IOCTLS_BASIC \
4849
((__u64)1 << _UFFDIO_WAKE | \
4950
(__u64)1 << _UFFDIO_COPY | \
50-
(__u64)1 << _UFFDIO_CONTINUE)
51+
(__u64)1 << _UFFDIO_CONTINUE | \
52+
(__u64)1 << _UFFDIO_WRITEPROTECT)
5153

5254
/*
5355
* Valid ioctl command number range with this API is from 0x00 to
@@ -194,6 +196,9 @@ struct uffdio_api {
194196
* UFFD_FEATURE_EXACT_ADDRESS indicates that the exact address of page
195197
* faults would be provided and the offset within the page would not be
196198
* masked.
199+
*
200+
* UFFD_FEATURE_WP_HUGETLBFS_SHMEM indicates that userfaultfd
201+
* write-protection mode is supported on both shmem and hugetlbfs.
197202
*/
198203
#define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0)
199204
#define UFFD_FEATURE_EVENT_FORK (1<<1)
@@ -207,6 +212,7 @@ struct uffdio_api {
207212
#define UFFD_FEATURE_MINOR_HUGETLBFS (1<<9)
208213
#define UFFD_FEATURE_MINOR_SHMEM (1<<10)
209214
#define UFFD_FEATURE_EXACT_ADDRESS (1<<11)
215+
#define UFFD_FEATURE_WP_HUGETLBFS_SHMEM (1<<12)
210216
__u64 features;
211217

212218
__u64 ioctls;

mm/userfaultfd.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -732,15 +732,12 @@ int mwriteprotect_range(struct mm_struct *dst_mm, unsigned long start,
732732

733733
err = -ENOENT;
734734
dst_vma = find_dst_vma(dst_mm, start, len);
735-
/*
736-
* Make sure the vma is not shared, that the dst range is
737-
* both valid and fully within a single existing vma.
738-
*/
739-
if (!dst_vma || (dst_vma->vm_flags & VM_SHARED))
735+
736+
if (!dst_vma)
740737
goto out_unlock;
741738
if (!userfaultfd_wp(dst_vma))
742739
goto out_unlock;
743-
if (!vma_is_anonymous(dst_vma))
740+
if (!vma_can_userfault(dst_vma, dst_vma->vm_flags))
744741
goto out_unlock;
745742

746743
if (is_vm_hugetlb_page(dst_vma)) {

0 commit comments

Comments
 (0)