Skip to content

Commit 0d02113

Browse files
Qian Caisuryasaimadhu
Qian Cai
authored andcommitted
x86/mm: Fix a crash with kmemleak_scan()
The first kmemleak_scan() call after boot would trigger the crash below because this callpath: kernel_init free_initmem mem_encrypt_free_decrypted_mem free_init_pages unmaps memory inside the .bss when DEBUG_PAGEALLOC=y. kmemleak_init() will register the .data/.bss sections and then kmemleak_scan() will scan those addresses and dereference them looking for pointer references. If free_init_pages() frees and unmaps pages in those sections, kmemleak_scan() will crash if referencing one of those addresses: BUG: unable to handle kernel paging request at ffffffffbd402000 CPU: 12 PID: 325 Comm: kmemleak Not tainted 5.1.0-rc4+ rib#4 RIP: 0010:scan_block Call Trace: scan_gray_list kmemleak_scan kmemleak_scan_thread kthread ret_from_fork Since kmemleak_free_part() is tolerant to unknown objects (not tracked by kmemleak), it is fine to call it from free_init_pages() even if not all address ranges passed to this function are known to kmemleak. [ bp: Massage. ] Fixes: b3f0907 ("x86/mm: Add .bss..decrypted section to hold shared variables") Signed-off-by: Qian Cai <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Reviewed-by: Catalin Marinas <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Brijesh Singh <[email protected]> Cc: Dave Hansen <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: x86-ml <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 36f0c42 commit 0d02113

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

arch/x86/mm/init.c

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/memblock.h>
66
#include <linux/swapfile.h>
77
#include <linux/swapops.h>
8+
#include <linux/kmemleak.h>
89

910
#include <asm/set_memory.h>
1011
#include <asm/e820/api.h>
@@ -766,6 +767,11 @@ void free_init_pages(const char *what, unsigned long begin, unsigned long end)
766767
if (debug_pagealloc_enabled()) {
767768
pr_info("debug: unmapping init [mem %#010lx-%#010lx]\n",
768769
begin, end - 1);
770+
/*
771+
* Inform kmemleak about the hole in the memory since the
772+
* corresponding pages will be unmapped.
773+
*/
774+
kmemleak_free_part((void *)begin, end - begin);
769775
set_memory_np(begin, (end - begin) >> PAGE_SHIFT);
770776
} else {
771777
/*

0 commit comments

Comments
 (0)