diff options
author | Mike Rapoport <rppt@linux.vnet.ibm.com> | 2017-02-24 17:58:28 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2017-02-24 20:46:55 -0500 |
commit | 27d02568f529e908399514dfbee8ee43bdfd5299 (patch) | |
tree | c8ff36cdc63f9a500355cdec086205de21033fda /mm | |
parent | ca49ca7114553587736fe78319e22f073b631380 (diff) |
userfaultfd: mcopy_atomic: return -ENOENT when no compatible VMA found
The memory mapping of a process may change between #PF event and the
call to mcopy_atomic that comes to resolve the page fault. In such
case, there will be no VMA covering the range passed to mcopy_atomic or
the VMA will not have userfaultfd context.
To allow uffd monitor to distinguish those case from other errors, let's
return -ENOENT instead of -EINVAL.
Note, that despite availability of UFFD_EVENT_UNMAP there still might be
race between the processing of UFFD_EVENT_UNMAP and outstanding
mcopy_atomic in case of non-cooperative uffd usage.
[rppt@linux.vnet.ibm.com: update cases returning -ENOENT]
Link: http://lkml.kernel.org/r/20170207150249.GA6709@rapoport-lnx
[aarcange@redhat.com: merge fix]
[akpm@linux-foundation.org: fix the merge fix]
Link: http://lkml.kernel.org/r/1485542673-24387-5-git-send-email-rppt@linux.vnet.ibm.com
Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
Acked-by: Hillf Danton <hillf.zj@alibaba-inc.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Pavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r-- | mm/userfaultfd.c | 58 |
1 files changed, 28 insertions, 30 deletions
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 1e5c2f94e8a3..9f0ad2a4f102 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c | |||
@@ -197,22 +197,25 @@ retry: | |||
197 | * retry, dst_vma will be set to NULL and we must lookup again. | 197 | * retry, dst_vma will be set to NULL and we must lookup again. |
198 | */ | 198 | */ |
199 | if (!dst_vma) { | 199 | if (!dst_vma) { |
200 | err = -EINVAL; | 200 | err = -ENOENT; |
201 | dst_vma = find_vma(dst_mm, dst_start); | 201 | dst_vma = find_vma(dst_mm, dst_start); |
202 | if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) | 202 | if (!dst_vma || !is_vm_hugetlb_page(dst_vma)) |
203 | goto out_unlock; | 203 | goto out_unlock; |
204 | |||
205 | if (vma_hpagesize != vma_kernel_pagesize(dst_vma)) | ||
206 | goto out_unlock; | ||
207 | |||
208 | /* | 204 | /* |
209 | * Make sure the remaining dst range is both valid and | 205 | * Only allow __mcopy_atomic_hugetlb on userfaultfd |
210 | * fully within a single existing vma. | 206 | * registered ranges. |
211 | */ | 207 | */ |
208 | if (!dst_vma->vm_userfaultfd_ctx.ctx) | ||
209 | goto out_unlock; | ||
210 | |||
212 | if (dst_start < dst_vma->vm_start || | 211 | if (dst_start < dst_vma->vm_start || |
213 | dst_start + len > dst_vma->vm_end) | 212 | dst_start + len > dst_vma->vm_end) |
214 | goto out_unlock; | 213 | goto out_unlock; |
215 | 214 | ||
215 | err = -EINVAL; | ||
216 | if (vma_hpagesize != vma_kernel_pagesize(dst_vma)) | ||
217 | goto out_unlock; | ||
218 | |||
216 | vm_shared = dst_vma->vm_flags & VM_SHARED; | 219 | vm_shared = dst_vma->vm_flags & VM_SHARED; |
217 | } | 220 | } |
218 | 221 | ||
@@ -221,12 +224,6 @@ retry: | |||
221 | goto out_unlock; | 224 | goto out_unlock; |
222 | 225 | ||
223 | /* | 226 | /* |
224 | * Only allow __mcopy_atomic_hugetlb on userfaultfd registered ranges. | ||
225 | */ | ||
226 | if (!dst_vma->vm_userfaultfd_ctx.ctx) | ||
227 | goto out_unlock; | ||
228 | |||
229 | /* | ||
230 | * If not shared, ensure the dst_vma has a anon_vma. | 227 | * If not shared, ensure the dst_vma has a anon_vma. |
231 | */ | 228 | */ |
232 | err = -ENOMEM; | 229 | err = -ENOMEM; |
@@ -404,22 +401,35 @@ retry: | |||
404 | * Make sure the vma is not shared, that the dst range is | 401 | * Make sure the vma is not shared, that the dst range is |
405 | * both valid and fully within a single existing vma. | 402 | * both valid and fully within a single existing vma. |
406 | */ | 403 | */ |
407 | err = -EINVAL; | 404 | err = -ENOENT; |
408 | dst_vma = find_vma(dst_mm, dst_start); | 405 | dst_vma = find_vma(dst_mm, dst_start); |
409 | if (!dst_vma) | 406 | if (!dst_vma) |
410 | goto out_unlock; | 407 | goto out_unlock; |
411 | /* | 408 | /* |
412 | * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but | 409 | * Be strict and only allow __mcopy_atomic on userfaultfd |
413 | * it will overwrite vm_ops, so vma_is_anonymous must return false. | 410 | * registered ranges to prevent userland errors going |
411 | * unnoticed. As far as the VM consistency is concerned, it | ||
412 | * would be perfectly safe to remove this check, but there's | ||
413 | * no useful usage for __mcopy_atomic ouside of userfaultfd | ||
414 | * registered ranges. This is after all why these are ioctls | ||
415 | * belonging to the userfaultfd and not syscalls. | ||
414 | */ | 416 | */ |
415 | if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && | 417 | if (!dst_vma->vm_userfaultfd_ctx.ctx) |
416 | dst_vma->vm_flags & VM_SHARED)) | ||
417 | goto out_unlock; | 418 | goto out_unlock; |
418 | 419 | ||
419 | if (dst_start < dst_vma->vm_start || | 420 | if (dst_start < dst_vma->vm_start || |
420 | dst_start + len > dst_vma->vm_end) | 421 | dst_start + len > dst_vma->vm_end) |
421 | goto out_unlock; | 422 | goto out_unlock; |
422 | 423 | ||
424 | err = -EINVAL; | ||
425 | /* | ||
426 | * shmem_zero_setup is invoked in mmap for MAP_ANONYMOUS|MAP_SHARED but | ||
427 | * it will overwrite vm_ops, so vma_is_anonymous must return false. | ||
428 | */ | ||
429 | if (WARN_ON_ONCE(vma_is_anonymous(dst_vma) && | ||
430 | dst_vma->vm_flags & VM_SHARED)) | ||
431 | goto out_unlock; | ||
432 | |||
423 | /* | 433 | /* |
424 | * If this is a HUGETLB vma, pass off to appropriate routine | 434 | * If this is a HUGETLB vma, pass off to appropriate routine |
425 | */ | 435 | */ |
@@ -427,18 +437,6 @@ retry: | |||
427 | return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start, | 437 | return __mcopy_atomic_hugetlb(dst_mm, dst_vma, dst_start, |
428 | src_start, len, zeropage); | 438 | src_start, len, zeropage); |
429 | 439 | ||
430 | /* | ||
431 | * Be strict and only allow __mcopy_atomic on userfaultfd | ||
432 | * registered ranges to prevent userland errors going | ||
433 | * unnoticed. As far as the VM consistency is concerned, it | ||
434 | * would be perfectly safe to remove this check, but there's | ||
435 | * no useful usage for __mcopy_atomic ouside of userfaultfd | ||
436 | * registered ranges. This is after all why these are ioctls | ||
437 | * belonging to the userfaultfd and not syscalls. | ||
438 | */ | ||
439 | if (!dst_vma->vm_userfaultfd_ctx.ctx) | ||
440 | goto out_unlock; | ||
441 | |||
442 | if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) | 440 | if (!vma_is_anonymous(dst_vma) && !vma_is_shmem(dst_vma)) |
443 | goto out_unlock; | 441 | goto out_unlock; |
444 | 442 | ||