mm: BUG in munlock_vma_pages_range

35 views
Skip to first unread message

Dmitry Vyukov

unread,
Nov 14, 2016, 11:47:47 PM11/14/16
to Andrew Morton, Vlastimil Babka, Kirill A. Shutemov, linu...@kvack.org, LKML, Konstantin Khlebnikov, Andrey Ryabinin, syzkaller
Hello,

The following program triggers BUG in munlock_vma_pages_range:

// autogenerated by syzkaller (http://github.com/google/syzkaller)
#include <sys/mman.h>

int main()
{
mmap((void*)0x20105000ul, 0xc00000ul, 0x2ul, 0x2172ul, -1, 0);
mremap((void*)0x201fd000ul, 0x4000ul, 0xc00000ul, 0x3ul, 0x203f0000ul);
return 0;
}


page:ffffea0001847cc0 count:0 mapcount:1 mapping:dead000000000400
index:0x20400 compound_mapcount: 1
flags: 0x5fffc0000000000()
page dumped because: VM_BUG_ON_PAGE(PageMlocked(page))
------------[ cut here ]------------
kernel BUG at mm/mlock.c:460!
invalid opcode: 0000 [#1] SMP DEBUG_PAGEALLOC KASAN
Modules linked in:
CPU: 2 PID: 6526 Comm: a.out Not tainted 4.9.0-rc5+ #28
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
task: ffff8800681ca0c0 task.stack: ffff8800637f0000
RIP: 0010:[<ffffffff81965272>] [<ffffffff81965272>]
munlock_vma_pages_range+0xcc2/0x1010 mm/mlock.c:460
RSP: 0018:ffff8800637f7178 EFLAGS: 00010292
RAX: 0000000000000000 RBX: ffffea0001847cc0 RCX: 0000000000000000
RDX: 0000000000000000 RSI: ffff88006d016e08 RDI: ffffed000c6fee20
RBP: ffff8800637f7638 R08: 0000000000000001 R09: 0000000000000000
R10: dffffc0000000000 R11: 0000000000000001 R12: ffffea0001847ce0
R13: ffffea0001840000 R14: dffffc0000000000 R15: ffff8800637f7610
FS: 0000000000000000(0000) GS:ffff88006d000000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00000000004b2080 CR3: 0000000064f87000 CR4: 00000000000006e0
Stack:
0000000041b58ab3 ffffffff894cfa90 ffff8800637f74f0 ffff8800637f7578
0000000000000005 ffff88007fff8000 1ffff1000c6fee42 ffff8800637f74b0
0000000020400000 000000050c6fee41 ffffea000196c55c ffffed000c6feeaf
Call Trace:
[< inline >] munlock_vma_pages_all mm/internal.h:277
[<ffffffff8197261b>] exit_mmap+0x1bb/0x4e0 mm/mmap.c:2924
[< inline >] __mmput kernel/fork.c:866
[<ffffffff813f24ce>] mmput+0x20e/0x4c0 kernel/fork.c:888
[< inline >] exit_mm kernel/exit.c:512
[<ffffffff814119a0>] do_exit+0x960/0x2640 kernel/exit.c:815
[<ffffffff8141383e>] do_group_exit+0x14e/0x420 kernel/exit.c:931
[< inline >] SYSC_exit_group kernel/exit.c:942
[<ffffffff81413b32>] SyS_exit_group+0x22/0x30 kernel/exit.c:940
[<ffffffff88143885>] entry_SYSCALL_64_fastpath+0x23/0xc6
arch/x86/entry/entry_64.S:209
Code: 0b e8 53 2e d8 ff 48 c7 c6 c0 32 31 88 48 89 df e8 54 2e fd ff
0f 0b e8 3d 2e d8 ff 48 c7 c6 80 35 31 88 48 89 df e8 3e 2e fd ff <0f>
0b 48 89 85 a8 fb ff ff e8 20 2e d8 ff 48 8b 85 a8 fb ff ff
RIP [<ffffffff81965272>] munlock_vma_pages_range+0xcc2/0x1010 mm/mlock.c:460
RSP <ffff8800637f7178>
---[ end trace 694dc6462f524cf9 ]---
Fixing recursive fault but reboot is needed!


On commit a25f0944ba9b1d8a6813fd6f1a86f1bd59ac25a6 (Nov 13).

Kirill A. Shutemov

unread,
Nov 15, 2016, 8:27:36 AM11/15/16
to Dmitry Vyukov, Andrew Morton, Vlastimil Babka, linu...@kvack.org, LKML, Konstantin Khlebnikov, Andrey Ryabinin, syzkaller
On Tue, Nov 15, 2016 at 05:47:26AM +0100, Dmitry Vyukov wrote:
> Hello,
>
> The following program triggers BUG in munlock_vma_pages_range:
>
> // autogenerated by syzkaller (http://github.com/google/syzkaller)
> #include <sys/mman.h>
>
> int main()
> {
> mmap((void*)0x20105000ul, 0xc00000ul, 0x2ul, 0x2172ul, -1, 0);
> mremap((void*)0x201fd000ul, 0x4000ul, 0xc00000ul, 0x3ul, 0x203f0000ul);
> return 0;
> }
>
>
> page:ffffea0001847cc0 count:0 mapcount:1 mapping:dead000000000400
> index:0x20400 compound_mapcount: 1
> flags: 0x5fffc0000000000()
> page dumped because: VM_BUG_ON_PAGE(PageMlocked(page))

Ughh.. mlock() again.

This should fix the issue:

From cc6bc279ca713a19288610c31f8256dc885c5d7e Mon Sep 17 00:00:00 2001
From: "Kirill A. Shutemov" <kirill....@linux.intel.com>
Date: Tue, 15 Nov 2016 16:11:12 +0300
Subject: [PATCH] thp: fix corner case of munlock() of PTE-mapped THPs

The following program triggers BUG() in munlock_vma_pages_range():

// autogenerated by syzkaller (http://github.com/google/syzkaller)
#include <sys/mman.h>

int main()
{
mmap((void*)0x20105000ul, 0xc00000ul, 0x2ul, 0x2172ul, -1, 0);
mremap((void*)0x201fd000ul, 0x4000ul, 0xc00000ul, 0x3ul, 0x203f0000ul);
return 0;
}

The test-case construct the situation when munlock_vma_pages_range()
finds PTE-mapped THP-head in the middle of page table and, by mistake,
skips HPAGE_PMD_NR pages after that.

As result, on the next iteration it hits the middle of PMD-mapped THP
and gets upset seeing mlocked tail page.

The solution is only skip HPAGE_PMD_NR pages if the THP was mlocked
during munlock_vma_page(). It would guarantee that the page is
PMD-mapped as we never mlock PTE-mapeed THPs.

Signed-off-by: Kirill A. Shutemov <kirill....@linux.intel.com>
Reported-by: Dmitry Vyukov <dvy...@google.com>
Fixes: e90309c9f772 ("thp: allow mlocked THP again")
Cc: sta...@vger.kernel.org # v4.5+
---
mm/mlock.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/mlock.c b/mm/mlock.c
index 145a4258ddbc..cdbed8aaa426 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -190,10 +190,13 @@ unsigned int munlock_vma_page(struct page *page)
*/
spin_lock_irq(zone_lru_lock(zone));

- nr_pages = hpage_nr_pages(page);
- if (!TestClearPageMlocked(page))
+ if (!TestClearPageMlocked(page)) {
+ /* Potentially, PTE-mapped THP: do not skip the rest PTEs */
+ nr_pages = 1;
goto unlock_out;
+ }

+ nr_pages = hpage_nr_pages(page);
__mod_zone_page_state(zone, NR_MLOCK, -nr_pages);

if (__munlock_isolate_lru_page(page, true)) {
--
Kirill A. Shutemov
Reply all
Reply to author
Forward
0 new messages