1 From 5b53a6ea886700a128b697a6fe8375340dea2c30 Mon Sep 17 00:00:00 2001
2 From: Michal Hocko <mhocko@suse.com>
3 Date: Fri, 18 Aug 2017 15:16:12 -0700
4 Subject: mm: fix double mmap_sem unlock on MMF_UNSTABLE enforced SIGBUS
6 From: Michal Hocko <mhocko@suse.com>
8 commit 5b53a6ea886700a128b697a6fe8375340dea2c30 upstream.
10 Tetsuo Handa has noticed that MMF_UNSTABLE SIGBUS path in
11 handle_mm_fault causes a lockdep splat
13 Out of memory: Kill process 1056 (a.out) score 603 or sacrifice child
14 Killed process 1056 (a.out) total-vm:4268108kB, anon-rss:2246048kB, file-rss:0kB, shmem-rss:0kB
15 a.out (1169) used greatest stack depth: 11664 bytes left
16 DEBUG_LOCKS_WARN_ON(depth <= 0)
17 ------------[ cut here ]------------
18 WARNING: CPU: 6 PID: 1339 at kernel/locking/lockdep.c:3617 lock_release+0x172/0x1e0
19 CPU: 6 PID: 1339 Comm: a.out Not tainted 4.13.0-rc3-next-20170803+ #142
20 Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 07/02/2015
21 RIP: 0010:lock_release+0x172/0x1e0
24 __do_page_fault+0x28e/0x4c0
25 do_page_fault+0x30/0x80
28 The reason is that the page fault path might have dropped the mmap_sem
29 and returned with VM_FAULT_RETRY. MMF_UNSTABLE check however rewrites
30 the error path to VM_FAULT_SIGBUS and we always expect mmap_sem taken in
31 that path. Fix this by taking mmap_sem when VM_FAULT_RETRY is held in
32 the MMF_UNSTABLE path.
34 We cannot simply add VM_FAULT_SIGBUS to the existing error code because
35 all arch specific page fault handlers and g-u-p would have to learn a
36 new error code combination.
38 Link: http://lkml.kernel.org/r/20170807113839.16695-2-mhocko@kernel.org
39 Fixes: 3f70dc38cec2 ("mm: make sure that kthreads will not refault oom reaped memory")
40 Reported-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
41 Signed-off-by: Michal Hocko <mhocko@suse.com>
42 Acked-by: David Rientjes <rientjes@google.com>
43 Cc: Andrea Argangeli <andrea@kernel.org>
44 Cc: "Kirill A. Shutemov" <kirill@shutemov.name>
45 Cc: Oleg Nesterov <oleg@redhat.com>
46 Cc: Wenwei Tao <wenwei.tww@alibaba-inc.com>
47 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
48 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
49 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
52 mm/memory.c | 12 +++++++++++-
53 1 file changed, 11 insertions(+), 1 deletion(-)
57 @@ -3635,8 +3635,18 @@ int handle_mm_fault(struct vm_area_struc
60 if (unlikely((current->flags & PF_KTHREAD) && !(ret & VM_FAULT_ERROR)
61 - && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags)))
62 + && test_bit(MMF_UNSTABLE, &vma->vm_mm->flags))) {
65 + * We are going to enforce SIGBUS but the PF path might have
66 + * dropped the mmap_sem already so take it again so that
67 + * we do not break expectations of all arch specific PF paths
70 + if (ret & VM_FAULT_RETRY)
71 + down_read(&vma->vm_mm->mmap_sem);
72 ret = VM_FAULT_SIGBUS;