Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/gup: take mmap_lock in get_dump_page()

Properly take the mmap_lock before calling into the GUP code from
get_dump_page(); and play nice, allowing the GUP code to drop the
mmap_lock if it has to sleep.

As Linus pointed out, we don't actually need the VMA because
__get_user_pages() will flush the dcache for us if necessary.

Signed-off-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: "Eric W . Biederman" <ebiederm@xmission.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Hugh Dickins <hughd@google.com>
Link: http://lkml.kernel.org/r/20200827114932.3572699-7-jannh@google.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Jann Horn and committed by
Linus Torvalds
7f3bfab5 a07279c9

+10 -6
+10 -6
mm/gup.c
··· 1547 1547 * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - 1548 1548 * allowing a hole to be left in the corefile to save diskspace. 1549 1549 * 1550 - * Called without mmap_lock, but after all other threads have been killed. 1550 + * Called without mmap_lock (takes and releases the mmap_lock by itself). 1551 1551 */ 1552 1552 #ifdef CONFIG_ELF_CORE 1553 1553 struct page *get_dump_page(unsigned long addr) 1554 1554 { 1555 - struct vm_area_struct *vma; 1555 + struct mm_struct *mm = current->mm; 1556 1556 struct page *page; 1557 + int locked = 1; 1558 + int ret; 1557 1559 1558 - if (__get_user_pages_locked(current->mm, addr, 1, &page, &vma, NULL, 1559 - FOLL_FORCE | FOLL_DUMP | FOLL_GET) < 1) 1560 + if (mmap_read_lock_killable(mm)) 1560 1561 return NULL; 1561 - flush_cache_page(vma, addr, page_to_pfn(page)); 1562 - return page; 1562 + ret = __get_user_pages_locked(mm, addr, 1, &page, NULL, &locked, 1563 + FOLL_FORCE | FOLL_DUMP | FOLL_GET); 1564 + if (locked) 1565 + mmap_read_unlock(mm); 1566 + return (ret == 1) ? page : NULL; 1563 1567 } 1564 1568 #endif /* CONFIG_ELF_CORE */ 1565 1569