Skip to content

Commit dcf7fe9

Browse files
aagittorvalds
authored andcommitted
userfaultfd: shmem: UFFDIO_COPY: set the page dirty if VM_WRITE is not set
Set the page dirty if VM_WRITE is not set because in such case the pte won't be marked dirty and the page would be reclaimed without writepage (i.e. swapout in the shmem case). This was found by source review. Most apps (certainly including QEMU) only use UFFDIO_COPY on PROT_READ|PROT_WRITE mappings or the app can't modify the memory in the first place. This is for correctness and it could help the non cooperative use case to avoid unexpected data loss. Link: http://lkml.kernel.org/r/20181126173452.26955-6-aarcange@redhat.com Reviewed-by: Hugh Dickins <hughd@google.com> Cc: stable@vger.kernel.org Fixes: 4c27fe4 ("userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support") Reported-by: Hugh Dickins <hughd@google.com> Signed-off-by: Andrea Arcangeli <aarcange@redhat.com> Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com> Cc: Jann Horn <jannh@google.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Mike Rapoport <rppt@linux.ibm.com> Cc: Peter Xu <peterx@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e2a50c1 commit dcf7fe9

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

mm/shmem.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,16 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
22742274
_dst_pte = mk_pte(page, dst_vma->vm_page_prot);
22752275
if (dst_vma->vm_flags & VM_WRITE)
22762276
_dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
2277+
else {
2278+
/*
2279+
* We don't set the pte dirty if the vma has no
2280+
* VM_WRITE permission, so mark the page dirty or it
2281+
* could be freed from under us. We could do it
2282+
* unconditionally before unlock_page(), but doing it
2283+
* only if VM_WRITE is not set is faster.
2284+
*/
2285+
set_page_dirty(page);
2286+
}
22772287

22782288
dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
22792289

@@ -2307,6 +2317,7 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
23072317
return ret;
23082318
out_release_uncharge_unlock:
23092319
pte_unmap_unlock(dst_pte, ptl);
2320+
ClearPageDirty(page);
23102321
delete_from_page_cache(page);
23112322
out_release_uncharge:
23122323
mem_cgroup_cancel_charge(page, memcg, false);

0 commit comments

Comments
 (0)