linux-stable/arch/powerpc/mm
Michael Ellerman ed0ba37e7b powerpc/mm: Fix boot crash with FLATMEM
[ Upstream commit daa9ada209 ]

Erhard reported that his G5 was crashing with v6.6-rc kernels:

  mpic: Setting up HT PICs workarounds for U3/U4
  BUG: Unable to handle kernel data access at 0xfeffbb62ffec65fe
  Faulting instruction address: 0xc00000000005dc40
  Oops: Kernel access of bad area, sig: 11 [#1]
  BE PAGE_SIZE=4K MMU=Hash SMP NR_CPUS=2 PowerMac
  Modules linked in:
  CPU: 0 PID: 0 Comm: swapper/0 Tainted: G                T  6.6.0-rc3-PMacGS #1
  Hardware name: PowerMac11,2 PPC970MP 0x440101 PowerMac
  NIP:  c00000000005dc40 LR: c000000000066660 CTR: c000000000007730
  REGS: c0000000022bf510 TRAP: 0380   Tainted: G                T (6.6.0-rc3-PMacGS)
  MSR:  9000000000001032 <SF,HV,ME,IR,DR,RI>  CR: 44004242  XER: 00000000
  IRQMASK: 3
  GPR00: 0000000000000000 c0000000022bf7b0 c0000000010c0b00 00000000000001ac
  GPR04: 0000000003c80000 0000000000000300 c0000000f20001ae 0000000000000300
  GPR08: 0000000000000006 feffbb62ffec65ff 0000000000000001 0000000000000000
  GPR12: 9000000000001032 c000000002362000 c000000000f76b80 000000000349ecd8
  GPR16: 0000000002367ba8 0000000002367f08 0000000000000006 0000000000000000
  GPR20: 00000000000001ac c000000000f6f920 c0000000022cd985 000000000000000c
  GPR24: 0000000000000300 00000003b0a3691d c0003e008030000e 0000000000000000
  GPR28: c00000000000000c c0000000f20001ee feffbb62ffec65fe 00000000000001ac
  NIP hash_page_do_lazy_icache+0x50/0x100
  LR  __hash_page_4K+0x420/0x590
  Call Trace:
    hash_page_mm+0x364/0x6f0
    do_hash_fault+0x114/0x2b0
    data_access_common_virt+0x198/0x1f0
  --- interrupt: 300 at mpic_init+0x4bc/0x10c4
  NIP:  c000000002020a5c LR: c000000002020a04 CTR: 0000000000000000
  REGS: c0000000022bf9f0 TRAP: 0300   Tainted: G                T (6.6.0-rc3-PMacGS)
  MSR:  9000000000001032 <SF,HV,ME,IR,DR,RI>  CR: 24004248  XER: 00000000
  DAR: c0003e008030000e DSISR: 40000000 IRQMASK: 1
  ...
  NIP mpic_init+0x4bc/0x10c4
  LR  mpic_init+0x464/0x10c4
  --- interrupt: 300
    pmac_setup_one_mpic+0x258/0x2dc
    pmac_pic_init+0x28c/0x3d8
    init_IRQ+0x90/0x140
    start_kernel+0x57c/0x78c
    start_here_common+0x1c/0x20

A bisect pointed to the breakage beginning with commit 9fee28baa6 ("powerpc:
implement the new page table range API").

Analysis of the oops pointed to a struct page with a corrupted
compound_head being loaded via page_folio() -> _compound_head() in
hash_page_do_lazy_icache().

The access by the mpic code is to an MMIO address, so the expectation
is that the struct page for that address would be initialised by
init_unavailable_range(), as pointed out by Aneesh.

Instrumentation showed that was not the case, which eventually lead to
the realisation that pfn_valid() was returning false for that address,
causing the struct page to not be initialised.

Because the system is using FLATMEM, the version of pfn_valid() in
memory_model.h is used:

static inline int pfn_valid(unsigned long pfn)
{
	...
	return pfn >= pfn_offset && (pfn - pfn_offset) < max_mapnr;
}

Which relies on max_mapnr being initialised. Early in boot max_mapnr is
zero meaning no PFNs are valid.

max_mapnr is initialised in mem_init() called via:

  start_kernel()
    mm_core_init()  # init/main.c:928
      mem_init()

But that is too late for the usage in init_unavailable_range() called via:

  start_kernel()
    setup_arch()    # init/main.c:893
      paging_init()
        free_area_init()
          init_unavailable_range()

Although max_mapnr is currently set in mem_init(), the value is actually
already available much earlier, as soon as mem_topology_setup() has
completed, which is also before paging_init() is called. So move the
initialisation there, which causes paging_init() to correctly initialise
the struct page and fixes the bug.

This bug seems to have been lurking for years, but went unnoticed
because the pre-folio code was inspecting the uninitialised page->flags
but not dereferencing it.

Thanks to Erhard and Aneesh for help debugging.

Reported-by: Erhard Furtner <erhard_f@mailbox.org>
Closes: https://lore.kernel.org/all/20230929132750.3cd98452@yea/
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://msgid.link/20231023112500.1550208-1-mpe@ellerman.id.au
Signed-off-by: Sasha Levin <sashal@kernel.org>
2023-11-08 14:11:02 +01:00
..
book3s32 - Yu Zhao's Multi-Gen LRU patches are here. They've been under test in 2022-10-10 17:53:04 -07:00
book3s64 powerpc: Don't include lppaca.h in paca.h 2023-09-13 09:42:48 +02:00
kasan powerpc/kasan: Disable KCOV in KASAN code 2023-08-23 17:52:27 +02:00
nohash powerpc/64e: provide an addressing macro for use with TOC in alternate register 2022-09-28 19:22:12 +10:00
ptdump powerpc: Remove CONFIG_PPC_BOOK3E_MMU 2022-09-26 23:00:14 +10:00
cacheflush.c powerpc: Fix all occurences of "the the" 2022-05-22 15:59:43 +10:00
copro_fault.c powerpc/mm: convert coprocessor fault to lock_mm_and_find_vma() 2023-07-01 13:16:25 +02:00
dma-noncoherent.c
drmem.c powerpc: Add missing headers 2022-05-08 22:15:40 +10:00
fault.c powerpc/mm: Convert to using lock_mm_and_find_vma() 2023-07-01 13:16:24 +02:00
hugetlbpage.c powerpc/mm: Fix UBSAN warning reported on hugetlb 2022-09-30 18:35:52 +10:00
init-common.c powerpc/kuep: Remove 'nosmep' boot time parameter except for book3s/64 2021-12-09 22:41:18 +11:00
init_32.c powerpc: Remove CONFIG_FSL_BOOKE 2022-09-26 22:47:37 +10:00
init_64.c powerpc/mm/altmap: Fix altmap boundary check 2023-08-11 12:08:25 +02:00
ioremap.c powerpc: make memremap_compat_align 64s-only 2021-12-02 22:57:24 +11:00
ioremap_32.c powerpc/mm: Leave a gap between early allocated IO areas 2021-06-25 00:07:10 +10:00
ioremap_64.c powerpc/mm: Leave a gap between early allocated IO areas 2021-06-25 00:07:10 +10:00
maccess.c powerpc/inst: Optimise copy_inst_from_kernel_nofault() 2021-12-09 22:41:21 +11:00
Makefile powerpc/mm: Convert to default topdown mmap layout 2022-05-05 22:11:58 +10:00
mem.c powerpc/mm: Fix boot crash with FLATMEM 2023-11-08 14:11:02 +01:00
mmu_context.c powerpc/mm: Switch obsolete dssall to .long 2021-12-23 22:35:13 +11:00
mmu_decl.h powerpc: Remove CONFIG_PPC_FSL_BOOK3E 2022-09-26 23:00:13 +10:00
numa.c powerpc/papr_scm: Update the NUMA distance table for the target node 2023-04-20 12:35:14 +02:00
pageattr.c powerpc: align address to page boundary in change_page_attr() 2022-05-08 22:15:41 +10:00
pgtable-frag.c powerpc: fix typos in comments 2022-05-05 22:12:44 +10:00
pgtable.c powerpc/mm: move protection_map[] inside the platform 2022-07-17 17:14:37 -07:00
pgtable_32.c powerpc: move __end_rodata to cover arch read-only sections 2022-09-26 20:58:16 +10:00
pgtable_64.c powerpc: Remove asm/prom.h from all files that don't need it 2022-05-08 22:15:04 +10:00