Skip to content
This repository was archived by the owner on Oct 30, 2021. It is now read-only.

Commit cac6732

Browse files
rppttorvalds
authored andcommitted
userfaultfd: shmem: allow registration of shared memory ranges
Expand the userfaultfd_register/unregister routines to allow shared memory VMAs. Currently, there is no UFFDIO_ZEROPAGE and write-protection support for shared memory VMAs, which is reflected in ioctl methods supported by uffdio_register. Link: http://lkml.kernel.org/r/20161216144821.5183-34-aarcange@redhat.com Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.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 Kravetz <mike.kravetz@oracle.com> Cc: Pavel Emelyanov <xemul@parallels.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent cfda052 commit cac6732

File tree

3 files changed

+9
-16
lines changed

3 files changed

+9
-16
lines changed

fs/userfaultfd.c

+7-14
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,8 @@ static __always_inline int validate_range(struct mm_struct *mm,
10981098

10991099
static inline bool vma_can_userfault(struct vm_area_struct *vma)
11001100
{
1101-
return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma);
1101+
return vma_is_anonymous(vma) || is_vm_hugetlb_page(vma) ||
1102+
vma_is_shmem(vma);
11021103
}
11031104

11041105
static int userfaultfd_register(struct userfaultfd_ctx *ctx,
@@ -1111,7 +1112,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
11111112
struct uffdio_register __user *user_uffdio_register;
11121113
unsigned long vm_flags, new_flags;
11131114
bool found;
1114-
bool huge_pages;
1115+
bool non_anon_pages;
11151116
unsigned long start, end, vma_end;
11161117

11171118
user_uffdio_register = (struct uffdio_register __user *) arg;
@@ -1175,13 +1176,9 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
11751176

11761177
/*
11771178
* Search for not compatible vmas.
1178-
*
1179-
* FIXME: this shall be relaxed later so that it doesn't fail
1180-
* on tmpfs backed vmas (in addition to the current allowance
1181-
* on anonymous vmas).
11821179
*/
11831180
found = false;
1184-
huge_pages = false;
1181+
non_anon_pages = false;
11851182
for (cur = vma; cur && cur->vm_start < end; cur = cur->vm_next) {
11861183
cond_resched();
11871184

@@ -1220,8 +1217,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
12201217
/*
12211218
* Note vmas containing huge pages
12221219
*/
1223-
if (is_vm_hugetlb_page(cur))
1224-
huge_pages = true;
1220+
if (is_vm_hugetlb_page(cur) || vma_is_shmem(cur))
1221+
non_anon_pages = true;
12251222

12261223
found = true;
12271224
}
@@ -1292,7 +1289,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
12921289
* userland which ioctls methods are guaranteed to
12931290
* succeed on this range.
12941291
*/
1295-
if (put_user(huge_pages ? UFFD_API_RANGE_IOCTLS_HPAGE :
1292+
if (put_user(non_anon_pages ? UFFD_API_RANGE_IOCTLS_BASIC :
12961293
UFFD_API_RANGE_IOCTLS,
12971294
&user_uffdio_register->ioctls))
12981295
ret = -EFAULT;
@@ -1352,10 +1349,6 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
13521349

13531350
/*
13541351
* Search for not compatible vmas.
1355-
*
1356-
* FIXME: this shall be relaxed later so that it doesn't fail
1357-
* on tmpfs backed vmas (in addition to the current allowance
1358-
* on anonymous vmas).
13591352
*/
13601353
found = false;
13611354
ret = -EINVAL;

include/uapi/linux/userfaultfd.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
((__u64)1 << _UFFDIO_WAKE | \
3131
(__u64)1 << _UFFDIO_COPY | \
3232
(__u64)1 << _UFFDIO_ZEROPAGE)
33-
#define UFFD_API_RANGE_IOCTLS_HPAGE \
33+
#define UFFD_API_RANGE_IOCTLS_BASIC \
3434
((__u64)1 << _UFFDIO_WAKE | \
3535
(__u64)1 << _UFFDIO_COPY)
3636

tools/testing/selftests/vm/userfaultfd.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static void allocate_area(void **alloc_area)
129129

130130
#else /* HUGETLB_TEST */
131131

132-
#define EXPECTED_IOCTLS UFFD_API_RANGE_IOCTLS_HPAGE
132+
#define EXPECTED_IOCTLS UFFD_API_RANGE_IOCTLS_BASIC
133133

134134
static int release_pages(char *rel_area)
135135
{

0 commit comments

Comments
 (0)