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

Commit 683b473

Browse files
aagitgregkh
authored andcommitted
userfaultfd: shmem: allocate anonymous memory for MAP_PRIVATE shmem
commit 5b51072e97d587186c2f5390c8c9c1fb7e179505 upstream. Userfaultfd did not create private memory when UFFDIO_COPY was invoked on a MAP_PRIVATE shmem mapping. Instead it wrote to the shmem file, even when that had not been opened for writing. Though, fortunately, that could only happen where there was a hole in the file. Fix the shmem-backed implementation of UFFDIO_COPY to create private memory for MAP_PRIVATE mappings. The hugetlbfs-backed implementation was already correct. This change is visible to userland, if userfaultfd has been used in unintended ways: so it introduces a small risk of incompatibility, but is necessary in order to respect file permissions. An app that uses UFFDIO_COPY for anything like postcopy live migration won't notice the difference, and in fact it'll run faster because there will be no copy-on-write and memory waste in the tmpfs pagecache anymore. Userfaults on MAP_PRIVATE shmem keep triggering only on file holes like before. The real zeropage can also be built on a MAP_PRIVATE shmem mapping through UFFDIO_ZEROPAGE and that's safe because the zeropage pte is never dirty, in turn even an mprotect upgrading the vma permission from PROT_READ to PROT_READ|PROT_WRITE won't make the zeropage pte writable. Link: http://lkml.kernel.org/r/20181126173452.26955-3-aarcange@redhat.com Fixes: 4c27fe4 ("userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support") Signed-off-by: Andrea Arcangeli <aarcange@redhat.com> Reported-by: Mike Rapoport <rppt@linux.ibm.com> Reviewed-by: Hugh Dickins <hughd@google.com> Cc: <stable@vger.kernel.org> Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com> Cc: Jann Horn <jannh@google.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Peter Xu <peterx@redhat.com> Cc: stable@vger.kernel.org Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 82c5a8c commit 683b473

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

mm/userfaultfd.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,17 @@ static __always_inline ssize_t mfill_atomic_pte(struct mm_struct *dst_mm,
381381
{
382382
ssize_t err;
383383

384-
if (vma_is_anonymous(dst_vma)) {
384+
/*
385+
* The normal page fault path for a shmem will invoke the
386+
* fault, fill the hole in the file and COW it right away. The
387+
* result generates plain anonymous memory. So when we are
388+
* asked to fill an hole in a MAP_PRIVATE shmem mapping, we'll
389+
* generate anonymous memory directly without actually filling
390+
* the hole. For the MAP_PRIVATE case the robustness check
391+
* only happens in the pagetable (to verify it's still none)
392+
* and not in the radix tree.
393+
*/
394+
if (!(dst_vma->vm_flags & VM_SHARED)) {
385395
if (!zeropage)
386396
err = mcopy_atomic_pte(dst_mm, dst_pmd, dst_vma,
387397
dst_addr, src_addr, page);
@@ -480,7 +490,8 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm,
480490
* dst_vma.
481491
*/
482492
err = -ENOMEM;
483-
if (vma_is_anonymous(dst_vma) && unlikely(anon_vma_prepare(dst_vma)))
493+
if (!(dst_vma->vm_flags & VM_SHARED) &&
494+
unlikely(anon_vma_prepare(dst_vma)))
484495
goto out_unlock;
485496

486497
while (src_addr < src_start + len) {

0 commit comments

Comments
 (0)