Skip to content

Commit 9a842b4

Browse files
jstancekgregkh
authored andcommitted
mm/memory.c: do_fault: avoid usage of stale vm_area_struct
commit fc8efd2 upstream. LTP testcase mtest06 [1] can trigger a crash on s390x running 5.0.0-rc8. This is a stress test, where one thread mmaps/writes/munmaps memory area and other thread is trying to read from it: CPU: 0 PID: 2611 Comm: mmap1 Not tainted 5.0.0-rc8+ #51 Hardware name: IBM 2964 N63 400 (z/VM 6.4.0) Krnl PSW : 0404e00180000000 00000000001ac8d8 (__lock_acquire+0x7/0x7a8) Call Trace: ([<0000000000000000>] (null)) [<00000000001adae4>] lock_acquire+0xec/0x258 [<000000000080d1ac>] _raw_spin_lock_bh+0x5c/0x98 [<000000000012a780>] page_table_free+0x48/0x1a8 [<00000000002f6e54>] do_fault+0xdc/0x670 [<00000000002fadae>] __handle_mm_fault+0x416/0x5f0 [<00000000002fb138>] handle_mm_fault+0x1b0/0x320 [<00000000001248cc>] do_dat_exception+0x19c/0x2c8 [<000000000080e5ee>] pgm_check_handler+0x19e/0x200 page_table_free() is called with NULL mm parameter, but because "0" is a valid address on s390 (see S390_lowcore), it keeps going until it eventually crashes in lockdep's lock_acquire. This crash is reproducible at least since 4.14. Problem is that "vmf->vma" used in do_fault() can become stale. Because mmap_sem may be released, other threads can come in, call munmap() and cause "vma" be returned to kmem cache, and get zeroed/re-initialized and re-used: handle_mm_fault | __handle_mm_fault | do_fault | vma = vmf->vma | do_read_fault | __do_fault | vma->vm_ops->fault(vmf); | mmap_sem is released | | | do_munmap() | remove_vma_list() | remove_vma() | vm_area_free() | # vma is released | ... | # same vma is allocated | # from kmem cache | do_mmap() | vm_area_alloc() | memset(vma, 0, ...) | pte_free(vma->vm_mm, ...); | page_table_free | spin_lock_bh(&mm->context.lock);| <crash> | Cache mm_struct to avoid using potentially stale "vma". [1] https://github.com/linux-test-project/ltp/blob/master/testcases/kernel/mem/mtest06/mmap1.c Link: http://lkml.kernel.org/r/5b3fdf19e2a5be460a384b936f5b56e13733f1b8.1551595137.git.jstancek@redhat.com Signed-off-by: Jan Stancek <[email protected]> Reviewed-by: Andrea Arcangeli <[email protected]> Reviewed-by: Matthew Wilcox <[email protected]> Acked-by: Rafael Aquini <[email protected]> Reviewed-by: Minchan Kim <[email protected]> Acked-by: Kirill A. Shutemov <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Huang Ying <[email protected]> Cc: Souptick Joarder <[email protected]> Cc: Jerome Glisse <[email protected]> Cc: Aneesh Kumar K.V <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: David Rientjes <[email protected]> Cc: Mel Gorman <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 8df6ab7 commit 9a842b4

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

mm/memory.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3517,10 +3517,13 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf)
35173517
* but allow concurrent faults).
35183518
* The mmap_sem may have been released depending on flags and our
35193519
* return value. See filemap_fault() and __lock_page_or_retry().
3520+
* If mmap_sem is released, vma may become invalid (for example
3521+
* by other thread calling munmap()).
35203522
*/
35213523
static vm_fault_t do_fault(struct vm_fault *vmf)
35223524
{
35233525
struct vm_area_struct *vma = vmf->vma;
3526+
struct mm_struct *vm_mm = vma->vm_mm;
35243527
vm_fault_t ret;
35253528

35263529
/*
@@ -3561,7 +3564,7 @@ static vm_fault_t do_fault(struct vm_fault *vmf)
35613564

35623565
/* preallocated pagetable is unused: free it */
35633566
if (vmf->prealloc_pte) {
3564-
pte_free(vma->vm_mm, vmf->prealloc_pte);
3567+
pte_free(vm_mm, vmf->prealloc_pte);
35653568
vmf->prealloc_pte = NULL;
35663569
}
35673570
return ret;

0 commit comments

Comments
 (0)