[v2,4/4] mm, arch: add generic implementation of pfn_valid() for FLATMEM

Message ID 20230129124235.209895-5-rppt@kernel.org
State New
Headers
Series mm, arch: add generic implementation of pfn_valid() for FLATMEM |

Commit Message

Mike Rapoport Jan. 29, 2023, 12:42 p.m. UTC
  From: "Mike Rapoport (IBM)" <rppt@kernel.org>

Every architecture that supports FLATMEM memory model defines its own
version of pfn_valid() that essentially compares a pfn to max_mapnr.

Use mips/powerpc version implemented as static inline as a generic
implementation of pfn_valid() and drop its per-architecture definitions.

Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
Acked-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Guo Ren <guoren@kernel.org>		# csky
Acked-by: Huacai Chen <chenhuacai@loongson.cn>	# LoongArch
Acked-by: Stafford Horne <shorne@gmail.com>	# OpenRISC
---
 arch/alpha/include/asm/page.h      |  4 ----
 arch/arc/include/asm/page.h        |  1 -
 arch/csky/include/asm/page.h       |  1 -
 arch/hexagon/include/asm/page.h    |  1 -
 arch/ia64/include/asm/page.h       |  4 ----
 arch/loongarch/include/asm/page.h  | 13 -------------
 arch/m68k/include/asm/page_no.h    |  2 --
 arch/microblaze/include/asm/page.h |  1 -
 arch/mips/include/asm/page.h       | 13 -------------
 arch/nios2/include/asm/page.h      |  9 ---------
 arch/openrisc/include/asm/page.h   |  2 --
 arch/parisc/include/asm/page.h     |  4 ----
 arch/powerpc/include/asm/page.h    |  9 ---------
 arch/riscv/include/asm/page.h      |  5 -----
 arch/sh/include/asm/page.h         |  3 ---
 arch/sparc/include/asm/page_32.h   |  1 -
 arch/um/include/asm/page.h         |  1 -
 arch/x86/include/asm/page_32.h     |  4 ----
 arch/x86/include/asm/page_64.h     |  4 ----
 arch/xtensa/include/asm/page.h     |  4 ++--
 include/asm-generic/memory_model.h | 12 ++++++++++++
 include/asm-generic/page.h         |  2 --
 22 files changed, 14 insertions(+), 86 deletions(-)
  

Comments

Michael Ellerman Jan. 30, 2023, 5:06 a.m. UTC | #1
Mike Rapoport <rppt@kernel.org> writes:
> From: "Mike Rapoport (IBM)" <rppt@kernel.org>
>
> Every architecture that supports FLATMEM memory model defines its own
> version of pfn_valid() that essentially compares a pfn to max_mapnr.
>
> Use mips/powerpc version implemented as static inline as a generic
> implementation of pfn_valid() and drop its per-architecture definitions.
>
> Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
> Acked-by: Arnd Bergmann <arnd@arndb.de>
> Acked-by: Guo Ren <guoren@kernel.org>		# csky
> Acked-by: Huacai Chen <chenhuacai@loongson.cn>	# LoongArch
> Acked-by: Stafford Horne <shorne@gmail.com>	# OpenRISC
> ---
>  arch/alpha/include/asm/page.h      |  4 ----
>  arch/arc/include/asm/page.h        |  1 -
>  arch/csky/include/asm/page.h       |  1 -
>  arch/hexagon/include/asm/page.h    |  1 -
>  arch/ia64/include/asm/page.h       |  4 ----
>  arch/loongarch/include/asm/page.h  | 13 -------------
>  arch/m68k/include/asm/page_no.h    |  2 --
>  arch/microblaze/include/asm/page.h |  1 -
>  arch/mips/include/asm/page.h       | 13 -------------
>  arch/nios2/include/asm/page.h      |  9 ---------
>  arch/openrisc/include/asm/page.h   |  2 --
>  arch/parisc/include/asm/page.h     |  4 ----
>  arch/powerpc/include/asm/page.h    |  9 ---------

Acked-by: Michael Ellerman <mpe@ellerman.id.au> (powerpc)

cheers
  
Conor Dooley Jan. 31, 2023, 5:47 p.m. UTC | #2
Hey Mike,

On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
> From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> 
> Every architecture that supports FLATMEM memory model defines its own
> version of pfn_valid() that essentially compares a pfn to max_mapnr.
> 
> Use mips/powerpc version implemented as static inline as a generic
> implementation of pfn_valid() and drop its per-architecture definitions.
> 
> Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
> Acked-by: Arnd Bergmann <arnd@arndb.de>
> Acked-by: Guo Ren <guoren@kernel.org>		# csky
> Acked-by: Huacai Chen <chenhuacai@loongson.cn>	# LoongArch
> Acked-by: Stafford Horne <shorne@gmail.com>	# OpenRISC

Hmm, so this landed in linux-next today and I bisected a boot failure in
my CI to it. However, I am not really sure if it is a real issue worth
worrying about as the platform it triggered on is supposed to be using
SPARSEMEM, but isn't.
I had thought that my CI was using a config with SPARSEMEM since that
became required for riscv defconfig builds to boot in v6.1-rc1, but I
must have just forgotten to add it to my $platform_defconfig builds too.
However, those $platform_defconfig builds continued booting without
SPARSEMEM enabled until today.
for $platform_defconfig builds I now see the following, which is in turn
followed by an opps. The full output is at the bottom of this mail.

	WARNING: CPU: 0 PID: 0 at mm/vmalloc.c:479 __vmap_pages_range_noflush+0x2d4/0x4c8
	Modules linked in:
	CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.2.0-rc4-00538-g6e265f6be0c8 #1
	Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
	epc : __vmap_pages_range_noflush+0x2d4/0x4c8
	 ra : __vmap_pages_range_noflush+0x3e0/0x4c8
	epc : ffffffff801461e4 ra : ffffffff801462f0 sp : ffffffff81203b90
	 gp : ffffffff812e9fe0 tp : ffffffff8120fa80 t0 : ffffffe7bfe57000
	 t1 : ffffffffffff8000 t2 : 00000000000001a7 s0 : ffffffff81203c50
	 s1 : ffffffe7bfe56040 a0 : 0000000000000000 a1 : ffffffe7c7df2840
	 a2 : ffffffff812ebae8 a3 : 0000000000001000 a4 : 0000000000080200
	 a5 : 0000000000fffe00 a6 : 0000000001040056 a7 : ffffffffffffffff
	 s2 : 0000000001040024 s3 : ffffffff80de1118 s4 : ffffffc80400c000
	 s5 : ffffffc80400c000 s6 : 00000000000000e7 s7 : 0000000000000000
	 s8 : ffffffc80400bfff s9 : 00fffffffffff000 s10: ffffffc804008000
	 s11: ffffffe7bfe0d100 t3 : 0000000000000000 t4 : fffffffffff00000
	 t5 : 00000000000f0000 t6 : ffffffe7bfe22ac4
	status: 0000000200000120 badaddr: 0000000000000000 cause: 0000000000000003
	[<ffffffff8014762a>] __vmalloc_node_range+0x392/0x52e
	[<ffffffff8000dfc4>] copy_process+0x636/0x1196
	[<ffffffff8000ec42>] kernel_clone+0x4a/0x2f4
	[<ffffffff8000f15e>] user_mode_thread+0x7c/0x9a
	[<ffffffff8077056e>] rest_init+0x28/0xea
	[<ffffffff80800670>] arch_post_acpi_subsys_init+0x0/0x18
	[<ffffffff80800de4>] start_kernel+0x72c/0x75c

In my mind, there's a kernel misconfiguration issue, but it's not the
same splat as I used to get when using FLATMEM in this configuration:
	OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
	Machine model: Microchip PolarFire-SoC Icicle Kit
	earlycon: ns16550a0 at MMIO32 0x0000000020100000 (options '115200n8')
	printk: bootconsole [ns16550a0] enabled
	printk: debug: skip boot console de-registration.
	efi: UEFI not found.
	Zone ranges:
	  DMA32    [mem 0x0000000080200000-0x00000000ffffffff]
	  Normal   [mem 0x0000000100000000-0x000000107fffffff]
	Movable zone start for each node
	Early memory node ranges
	  node   0: [mem 0x0000000080200000-0x00000000bfbfffff]
	  node   0: [mem 0x00000000bfc00000-0x00000000bfffffff]
	  node   0: [mem 0x0000001040000000-0x000000107fffffff]
	Initmem setup node 0 [mem 0x0000000080200000-0x000000107fffffff]
	Kernel panic - not syncing: Failed to allocate 1073741824 bytes for node 0 memory map
	CPU: 0 PID: 0 Comm: swapper Not tainted 5.19.0-dirty #1
	Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
	Call Trace:
	[<ffffffff800057f0>] show_stack+0x30/0x3c
	[<ffffffff807d5802>] dump_stack_lvl+0x4a/0x66
	[<ffffffff807d5836>] dump_stack+0x18/0x20
	[<ffffffff807d1ae8>] panic+0x124/0x2c6
	[<ffffffff80814064>] free_area_init_core+0x0/0x11e
	[<ffffffff80813720>] free_area_init_node+0xc2/0xf6
	[<ffffffff8081331e>] free_area_init+0x222/0x260
	[<ffffffff808064d6>] misc_mem_init+0x62/0x9a
	[<ffffffff80803cb2>] setup_arch+0xb0/0xea
	[<ffffffff8080039a>] start_kernel+0x88/0x4ee

Turning on SPARSEMEM fixes both problems, but I'm just not sure if there
is some underlying issue here that I don't know enough about the area to
understand.

Thanks,
Conor.

[    0.000000] Linux version 6.2.0-rc4-00538-g6e265f6be0c8 (conor@wendy) (riscv64-unknown-linux-gnu-gcc (g5964b5cd727)
 11.1.0, GNU ld (GNU Binutils) 2.37) #1 SMP PREEMPT @7
[    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
[    0.000000] Machine model: Microchip PolarFire-SoC Icicle Kit
[    0.000000] earlycon: ns16550a0 at MMIO32 0x0000000020100000 (options '115200n8')
[    0.000000] printk: bootconsole [ns16550a0] enabled
[    0.000000] printk: debug: skip boot console de-registration.
[    0.000000] efi: UEFI not found.
[    0.000000] Zone ranges:
[    0.000000]   DMA32    [mem 0x0000000080200000-0x00000000ffffffff]
[    0.000000]   Normal   [mem 0x0000000100000000-0x000000107fffffff]
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000080200000-0x00000000bfbfffff]
[    0.000000]   node   0: [mem 0x00000000bfc00000-0x00000000bfffffff]
[    0.000000]   node   0: [mem 0x0000001040000000-0x000000107fffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000080200000-0x000000107fffffff]
[    0.000000] On node 0, zone Normal: 15990272 pages in unavailable ranges
[    0.000000] SBI specification v0.3 detected
[    0.000000] SBI implementation ID=0x1 Version=0x10000
[    0.000000] SBI TIME extension detected
[    0.000000] SBI IPI extension detected
[    0.000000] SBI RFENCE extension detected
[    0.000000] SBI SRST extension detected
[    0.000000] SBI HSM extension detected
[    0.000000] CPU with hartid=0 is not available
[    0.000000] CPU with hartid=0 is not available
[    0.000000] CPU with hartid=0 is not available
[    0.000000] riscv: base ISA extensions acdfim
[    0.000000] riscv: ELF capabilities acdfim
[    0.000000] percpu: Embedded 19 pages/cpu s37048 r8192 d32584 u77824
[    0.000000] CPU node for /cpus/cpu@0 exist but the possible cpu range is :0-3
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 294407
[    0.000000] Kernel command line: earlycon keep_bootcon reboot=cold
[    0.000000] Dentry cache hash table entries: 262144 (order: 9, 2097152 bytes, linear)
[    0.000000] Inode-cache hash table entries: 131072 (order: 8, 1048576 bytes, linear)
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
[    0.000000] software IO TLB: area num 4.
[    0.000000] software IO TLB: mapped [mem 0x00000000bbc00000-0x00000000bfc00000] (64MB)
[    0.000000] Virtual kernel memory layout:
[    0.000000]       fixmap : 0xffffffc6fee00000 - 0xffffffc6ff000000   (2048 kB)
[    0.000000]       pci io : 0xffffffc6ff000000 - 0xffffffc700000000   (  16 MB)
[    0.000000]      vmemmap : 0xffffffc700000000 - 0xffffffc800000000   (4096 MB)
[    0.000000]      vmalloc : 0xffffffc800000000 - 0xffffffd800000000   (  64 GB)
[    0.000000]      modules : 0xffffffff01352000 - 0xffffffff80000000   (2028 MB)
[    0.000000]       lowmem : 0xffffffd800000000 - 0xffffffe7ffe00000   (  63 GB)
[    0.000000]       kernel : 0xffffffff80000000 - 0xffffffffffffffff   (2047 MB)
[    0.000000] Memory: 1067600K/2095104K available (7639K kernel code, 4898K rwdata, 4096K rodata, 2183K init, 410K bss, 1027504K reserved, 0K cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
[    0.000000] rcu: Preemptible hierarchical RCU implementation.
[    0.000000] rcu:     RCU restricting CPUs from NR_CPUS=64 to nr_cpu_ids=4.
[    0.000000] rcu:     RCU debug extended QS entry/exit.
[    0.000000]  Trampoline variant of Tasks RCU enabled.
[    0.000000]  Tracing variant of Tasks RCU enabled.
[    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 25 jiffies.
[    0.000000] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
[    0.000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0
[    0.000000] CPU with hartid=0 is not available
[    0.000000] riscv-intc: unable to find hart id for /cpus/cpu@0/interrupt-controller
[    0.000000] riscv-intc: 64 local interrupts mapped
[    0.000000] plic: interrupt-controller@c000000: mapped 186 interrupts with 4 handlers for 9 contexts.
[    0.000000] rcu: srcu_init: Setting srcu_struct sizes based on contention.
[    0.000000] riscv-timer: riscv_timer_init_dt: Registering clocksource cpuid [0] hartid [1]
[    0.000000] clocksource: riscv_clocksource: mask: 0xffffffffffffffff max_cycles: 0x1d854df40, max_idle_ns: 3526361616960 ns
[    0.000004] sched_clock: 64 bits at 1000kHz, resolution 1000ns, wraps every 2199023255500ns
[    0.009687] Console: colour dummy device 80x25
[    0.014654] printk: console [tty0] enabled
[    0.019313] Calibrating delay loop (skipped), value calculated using timer frequency.. 2.00 BogoMIPS (lpj=4000)
[    0.030490] pid_max: default: 32768 minimum: 301
[    0.036094] Mount-cache hash table entries: 4096 (order: 3, 32768 bytes, linear)
[    0.044458] Mountpoint-cache hash table entries: 4096 (order: 3, 32768 bytes, linear)
[    0.055493] ------------[ cut here ]------------
[    0.060606] WARNING: CPU: 0 PID: 0 at mm/vmalloc.c:479 __vmap_pages_range_noflush+0x2d4/0x4c8
[    0.070040] Modules linked in:
[    0.073421] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.2.0-rc4-00538-g6e265f6be0c8 #1
[    0.082157] Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
[    0.088972] epc : __vmap_pages_range_noflush+0x2d4/0x4c8
[    0.094838]  ra : __vmap_pages_range_noflush+0x3e0/0x4c8
[    0.100707] epc : ffffffff801461e4 ra : ffffffff801462f0 sp : ffffffff81203b90
[    0.108674]  gp : ffffffff812e9fe0 tp : ffffffff8120fa80 t0 : ffffffe7bfe57000
[    0.116643]  t1 : ffffffffffff8000 t2 : 00000000000001a7 s0 : ffffffff81203c50
[    0.124611]  s1 : ffffffe7bfe56040 a0 : 0000000000000000 a1 : ffffffe7c7df2840
[    0.132588]  a2 : ffffffff812ebae8 a3 : 0000000000001000 a4 : 0000000000080200
[    0.140564]  a5 : 0000000000fffe00 a6 : 0000000001040056 a7 : ffffffffffffffff
[    0.148541]  s2 : 0000000001040024 s3 : ffffffff80de1118 s4 : ffffffc80400c000
[    0.156518]  s5 : ffffffc80400c000 s6 : 00000000000000e7 s7 : 0000000000000000
[    0.164495]  s8 : ffffffc80400bfff s9 : 00fffffffffff000 s10: ffffffc804008000
[    0.172472]  s11: ffffffe7bfe0d100 t3 : 0000000000000000 t4 : fffffffffff00000
[    0.180449]  t5 : 00000000000f0000 t6 : ffffffe7bfe22ac4
[    0.186317] status: 0000000200000120 badaddr: 0000000000000000 cause: 0000000000000003
[    0.195060] [<ffffffff8014762a>] __vmalloc_node_range+0x392/0x52e
[    0.201802] [<ffffffff8000dfc4>] copy_process+0x636/0x1196
[    0.207878] [<ffffffff8000ec42>] kernel_clone+0x4a/0x2f4
[    0.213755] [<ffffffff8000f15e>] user_mode_thread+0x7c/0x9a
[    0.219918] [<ffffffff8077056e>] rest_init+0x28/0xea
[    0.225429] [<ffffffff80800670>] arch_post_acpi_subsys_init+0x0/0x18
[    0.232460] [<ffffffff80800de4>] start_kernel+0x72c/0x75c
[    0.238434] ---[ end trace 0000000000000000 ]---
n):0kB isolated(file):0kB mapped:0kB dirty:0kB writeback:0kB shmem:0kB shmem_thp: 0kB shmem_pmdmapped: 0kB anon_thp: 0kB writeback_tmp:0kB kernel_stack:0kB pagetables:4kB sec_pagetables:0kB all_unreclaimable? no
[    0.423801] DMA32 free:940780kB boost:0kB min:0kB low:0kB high:0kB reserved_highatomic:0KB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB writepending:0kB present:1046528kB managed:940780kB mlocked:0kB bounce:0kB free_pcp:0kB local_pcp:0kB free_cma:0kB
[    0.452034] lowmem_reserve[]: 0 0 0
[    0.455919] Normal free:126248kB boost:0kB min:0kB low:0kB high:0kB reserved_highatomic:0KB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB writepending:0kB present:1048576kB managed:126820kB mlocked:0kB bounce:0kB free_pcp:156kB local_pcp:156kB free_cma:0kB
[    0.484652] lowmem_reserve[]: 0 0 0
[    0.488538] DMA32: 1*4kB (M) 1*8kB (M) 0*16kB 1*32kB (M) 1*64kB (M) 1*128kB (M) 0*256kB 1*512kB (M) 0*1024kB 1*2048kB (M) 229*4096kB (M) = 940780kB
[    0.503367] Normal: 2*4kB (ME) 0*8kB 2*16kB (UE) 2*32kB (ME) 3*64kB (UME) 0*128kB 2*256kB (ME) 3*512kB (UME) 3*1024kB (UME) 3*2048kB (UME) 28*4096kB (M) = 126248kB
[    0.519722] 0 total pagecache pages
[    0.523579] 0 pages in swap cache
[    0.527264] Free swap  = 0kB
[    0.530445] Total swap = 0kB
[    0.533652] 523776 pages RAM
[    0.536833] 0 pages HighMem/MovableOnly
[    0.541090] 256876 pages reserved
[    0.544833] Unable to handle kernel NULL pointer dereference at virtual address 000000000000003c
[    0.554576] Oops [#1]
[    0.557092] Modules linked in:
[    0.560470] CPU: 0 PID: 0 Comm: swapper/0 Tainted: G        W          6.2.0-rc4-00538-g6e265f6be0c8 #1
[    0.570841] Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
[    0.577664] epc : rest_init+0x44/0xea
[    0.581726]  ra : rest_init+0x44/0xea
[    0.585788] epc : ffffffff8077058a ra : ffffffff8077058a sp : ffffffff81203f70
[    0.593764]  gp : ffffffff812e9fe0 tp : ffffffff8120fa80 t0 : 0000000000000000
[    0.601741]  t1 : 0000000000000000 t2 : 0000000000000040 s0 : ffffffff81203f90
[    0.609718]  s1 : fffffffffffffff4 a0 : 0000000000000000 a1 : fffffffffffffff4
[    0.617695]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
[    0.625672]  a5 : 0000000000000000 a6 : 0000000001212dec a7 : ffffffffd8bc86ae
[    0.633649]  s2 : ffffffff80de0bf0 s3 : ffffffe7c7de7700 s4 : 0000000000000000
[    0.641626]  s5 : ffffffff812eb018 s6 : ffffffff80a00008 s7 : 0000000000000000
[    0.649603]  s8 : 00000000bfb85036 s9 : 0000000000000000 s10: 0000000000000000
[    0.657580]  s11: 0000000000000000 t3 : 0000000000000002 t4 : 0000000000000402
[    0.665557]  t5 : ffffffff812158a8 t6 : 0000000000000000
[    0.671416] status: 0000000200000120 badaddr: 000000000000003c cause: 000000000000000d
[    0.680157] [<ffffffff80800670>] arch_post_acpi_subsys_init+0x0/0x18
[    0.687188] [<ffffffff80800de4>] start_kernel+0x72c/0x75c
[    0.693195] ---[ end trace 0000000000000000 ]---
[    0.698325] Kernel panic - not syncing: Attempted to kill the idle task!
[    0.705729] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---
  
Mike Rapoport Jan. 31, 2023, 6:41 p.m. UTC | #3
Hi Conor,

On Tue, Jan 31, 2023 at 05:47:24PM +0000, Conor Dooley wrote:
> Hey Mike,
> 
> On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
> > From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> > 
> > Every architecture that supports FLATMEM memory model defines its own
> > version of pfn_valid() that essentially compares a pfn to max_mapnr.
> > 
> > Use mips/powerpc version implemented as static inline as a generic
> > implementation of pfn_valid() and drop its per-architecture definitions.
> > 
> > Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
> > Acked-by: Arnd Bergmann <arnd@arndb.de>
> > Acked-by: Guo Ren <guoren@kernel.org>		# csky
> > Acked-by: Huacai Chen <chenhuacai@loongson.cn>	# LoongArch
> > Acked-by: Stafford Horne <shorne@gmail.com>	# OpenRISC
> 
> Hmm, so this landed in linux-next today and I bisected a boot failure in
> my CI to it. However, I am not really sure if it is a real issue worth
> worrying about as the platform it triggered on is supposed to be using
> SPARSEMEM, but isn't.
> I had thought that my CI was using a config with SPARSEMEM since that
> became required for riscv defconfig builds to boot in v6.1-rc1, but I
> must have just forgotten to add it to my $platform_defconfig builds too.
> However, those $platform_defconfig builds continued booting without
> SPARSEMEM enabled until today.

The issue seems to be that the generic pfn_valid() does not take into
account pfn_offset when it compares it with max_mapnr.
Can you please test with the patch below?

diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memory_model.h
index 13d2a844d928..6796abe1900e 100644
--- a/include/asm-generic/memory_model.h
+++ b/include/asm-generic/memory_model.h
@@ -26,7 +26,7 @@ static inline int pfn_valid(unsigned long pfn)
 	extern unsigned long max_mapnr;
 	unsigned long pfn_offset = ARCH_PFN_OFFSET;
 
-	return pfn >= pfn_offset && pfn < max_mapnr;
+	return pfn >= pfn_offset && (pfn - pfn_offset) < max_mapnr;
 }
 #define pfn_valid pfn_valid
 #endif

> for $platform_defconfig builds I now see the following, which is in turn
> followed by an opps. The full output is at the bottom of this mail.
> 
> 	WARNING: CPU: 0 PID: 0 at mm/vmalloc.c:479 __vmap_pages_range_noflush+0x2d4/0x4c8
> 	Modules linked in:
> 	CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.2.0-rc4-00538-g6e265f6be0c8 #1
> 	Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
> 	epc : __vmap_pages_range_noflush+0x2d4/0x4c8
> 	 ra : __vmap_pages_range_noflush+0x3e0/0x4c8
> 	epc : ffffffff801461e4 ra : ffffffff801462f0 sp : ffffffff81203b90
> 	 gp : ffffffff812e9fe0 tp : ffffffff8120fa80 t0 : ffffffe7bfe57000
> 	 t1 : ffffffffffff8000 t2 : 00000000000001a7 s0 : ffffffff81203c50
> 	 s1 : ffffffe7bfe56040 a0 : 0000000000000000 a1 : ffffffe7c7df2840
> 	 a2 : ffffffff812ebae8 a3 : 0000000000001000 a4 : 0000000000080200
> 	 a5 : 0000000000fffe00 a6 : 0000000001040056 a7 : ffffffffffffffff
> 	 s2 : 0000000001040024 s3 : ffffffff80de1118 s4 : ffffffc80400c000
> 	 s5 : ffffffc80400c000 s6 : 00000000000000e7 s7 : 0000000000000000
> 	 s8 : ffffffc80400bfff s9 : 00fffffffffff000 s10: ffffffc804008000
> 	 s11: ffffffe7bfe0d100 t3 : 0000000000000000 t4 : fffffffffff00000
> 	 t5 : 00000000000f0000 t6 : ffffffe7bfe22ac4
> 	status: 0000000200000120 badaddr: 0000000000000000 cause: 0000000000000003
> 	[<ffffffff8014762a>] __vmalloc_node_range+0x392/0x52e
> 	[<ffffffff8000dfc4>] copy_process+0x636/0x1196
> 	[<ffffffff8000ec42>] kernel_clone+0x4a/0x2f4
> 	[<ffffffff8000f15e>] user_mode_thread+0x7c/0x9a
> 	[<ffffffff8077056e>] rest_init+0x28/0xea
> 	[<ffffffff80800670>] arch_post_acpi_subsys_init+0x0/0x18
> 	[<ffffffff80800de4>] start_kernel+0x72c/0x75c
> 
> In my mind, there's a kernel misconfiguration issue, but it's not the
> same splat as I used to get when using FLATMEM in this configuration:
> 	OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
> 	Machine model: Microchip PolarFire-SoC Icicle Kit
> 	earlycon: ns16550a0 at MMIO32 0x0000000020100000 (options '115200n8')
> 	printk: bootconsole [ns16550a0] enabled
> 	printk: debug: skip boot console de-registration.
> 	efi: UEFI not found.
> 	Zone ranges:
> 	  DMA32    [mem 0x0000000080200000-0x00000000ffffffff]
> 	  Normal   [mem 0x0000000100000000-0x000000107fffffff]
> 	Movable zone start for each node
> 	Early memory node ranges
> 	  node   0: [mem 0x0000000080200000-0x00000000bfbfffff]
> 	  node   0: [mem 0x00000000bfc00000-0x00000000bfffffff]
> 	  node   0: [mem 0x0000001040000000-0x000000107fffffff]
> 	Initmem setup node 0 [mem 0x0000000080200000-0x000000107fffffff]
> 	Kernel panic - not syncing: Failed to allocate 1073741824 bytes for node 0 memory map
> 	CPU: 0 PID: 0 Comm: swapper Not tainted 5.19.0-dirty #1
> 	Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
> 	Call Trace:
> 	[<ffffffff800057f0>] show_stack+0x30/0x3c
> 	[<ffffffff807d5802>] dump_stack_lvl+0x4a/0x66
> 	[<ffffffff807d5836>] dump_stack+0x18/0x20
> 	[<ffffffff807d1ae8>] panic+0x124/0x2c6
> 	[<ffffffff80814064>] free_area_init_core+0x0/0x11e
> 	[<ffffffff80813720>] free_area_init_node+0xc2/0xf6
> 	[<ffffffff8081331e>] free_area_init+0x222/0x260
> 	[<ffffffff808064d6>] misc_mem_init+0x62/0x9a
> 	[<ffffffff80803cb2>] setup_arch+0xb0/0xea
> 	[<ffffffff8080039a>] start_kernel+0x88/0x4ee
> 
> Turning on SPARSEMEM fixes both problems, but I'm just not sure if there
> is some underlying issue here that I don't know enough about the area to
> understand.
> 
> Thanks,
> Conor.
> 
> [    0.000000] Linux version 6.2.0-rc4-00538-g6e265f6be0c8 (conor@wendy) (riscv64-unknown-linux-gnu-gcc (g5964b5cd727)
>  11.1.0, GNU ld (GNU Binutils) 2.37) #1 SMP PREEMPT @7
> [    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
> [    0.000000] Machine model: Microchip PolarFire-SoC Icicle Kit
> [    0.000000] earlycon: ns16550a0 at MMIO32 0x0000000020100000 (options '115200n8')
> [    0.000000] printk: bootconsole [ns16550a0] enabled
> [    0.000000] printk: debug: skip boot console de-registration.
> [    0.000000] efi: UEFI not found.
> [    0.000000] Zone ranges:
> [    0.000000]   DMA32    [mem 0x0000000080200000-0x00000000ffffffff]
> [    0.000000]   Normal   [mem 0x0000000100000000-0x000000107fffffff]
> [    0.000000] Movable zone start for each node
> [    0.000000] Early memory node ranges
> [    0.000000]   node   0: [mem 0x0000000080200000-0x00000000bfbfffff]
> [    0.000000]   node   0: [mem 0x00000000bfc00000-0x00000000bfffffff]
> [    0.000000]   node   0: [mem 0x0000001040000000-0x000000107fffffff]
> [    0.000000] Initmem setup node 0 [mem 0x0000000080200000-0x000000107fffffff]
> [    0.000000] On node 0, zone Normal: 15990272 pages in unavailable ranges
> [    0.000000] SBI specification v0.3 detected
> [    0.000000] SBI implementation ID=0x1 Version=0x10000
> [    0.000000] SBI TIME extension detected
> [    0.000000] SBI IPI extension detected
> [    0.000000] SBI RFENCE extension detected
> [    0.000000] SBI SRST extension detected
> [    0.000000] SBI HSM extension detected
> [    0.000000] CPU with hartid=0 is not available
> [    0.000000] CPU with hartid=0 is not available
> [    0.000000] CPU with hartid=0 is not available
> [    0.000000] riscv: base ISA extensions acdfim
> [    0.000000] riscv: ELF capabilities acdfim
> [    0.000000] percpu: Embedded 19 pages/cpu s37048 r8192 d32584 u77824
> [    0.000000] CPU node for /cpus/cpu@0 exist but the possible cpu range is :0-3
> [    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 294407
> [    0.000000] Kernel command line: earlycon keep_bootcon reboot=cold
> [    0.000000] Dentry cache hash table entries: 262144 (order: 9, 2097152 bytes, linear)
> [    0.000000] Inode-cache hash table entries: 131072 (order: 8, 1048576 bytes, linear)
> [    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
> [    0.000000] software IO TLB: area num 4.
> [    0.000000] software IO TLB: mapped [mem 0x00000000bbc00000-0x00000000bfc00000] (64MB)
> [    0.000000] Virtual kernel memory layout:
> [    0.000000]       fixmap : 0xffffffc6fee00000 - 0xffffffc6ff000000   (2048 kB)
> [    0.000000]       pci io : 0xffffffc6ff000000 - 0xffffffc700000000   (  16 MB)
> [    0.000000]      vmemmap : 0xffffffc700000000 - 0xffffffc800000000   (4096 MB)
> [    0.000000]      vmalloc : 0xffffffc800000000 - 0xffffffd800000000   (  64 GB)
> [    0.000000]      modules : 0xffffffff01352000 - 0xffffffff80000000   (2028 MB)
> [    0.000000]       lowmem : 0xffffffd800000000 - 0xffffffe7ffe00000   (  63 GB)
> [    0.000000]       kernel : 0xffffffff80000000 - 0xffffffffffffffff   (2047 MB)
> [    0.000000] Memory: 1067600K/2095104K available (7639K kernel code, 4898K rwdata, 4096K rodata, 2183K init, 410K bss, 1027504K reserved, 0K cma-reserved)
> [    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
> [    0.000000] rcu: Preemptible hierarchical RCU implementation.
> [    0.000000] rcu:     RCU restricting CPUs from NR_CPUS=64 to nr_cpu_ids=4.
> [    0.000000] rcu:     RCU debug extended QS entry/exit.
> [    0.000000]  Trampoline variant of Tasks RCU enabled.
> [    0.000000]  Tracing variant of Tasks RCU enabled.
> [    0.000000] rcu: RCU calculated value of scheduler-enlistment delay is 25 jiffies.
> [    0.000000] rcu: Adjusting geometry for rcu_fanout_leaf=16, nr_cpu_ids=4
> [    0.000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0
> [    0.000000] CPU with hartid=0 is not available
> [    0.000000] riscv-intc: unable to find hart id for /cpus/cpu@0/interrupt-controller
> [    0.000000] riscv-intc: 64 local interrupts mapped
> [    0.000000] plic: interrupt-controller@c000000: mapped 186 interrupts with 4 handlers for 9 contexts.
> [    0.000000] rcu: srcu_init: Setting srcu_struct sizes based on contention.
> [    0.000000] riscv-timer: riscv_timer_init_dt: Registering clocksource cpuid [0] hartid [1]
> [    0.000000] clocksource: riscv_clocksource: mask: 0xffffffffffffffff max_cycles: 0x1d854df40, max_idle_ns: 3526361616960 ns
> [    0.000004] sched_clock: 64 bits at 1000kHz, resolution 1000ns, wraps every 2199023255500ns
> [    0.009687] Console: colour dummy device 80x25
> [    0.014654] printk: console [tty0] enabled
> [    0.019313] Calibrating delay loop (skipped), value calculated using timer frequency.. 2.00 BogoMIPS (lpj=4000)
> [    0.030490] pid_max: default: 32768 minimum: 301
> [    0.036094] Mount-cache hash table entries: 4096 (order: 3, 32768 bytes, linear)
> [    0.044458] Mountpoint-cache hash table entries: 4096 (order: 3, 32768 bytes, linear)
> [    0.055493] ------------[ cut here ]------------
> [    0.060606] WARNING: CPU: 0 PID: 0 at mm/vmalloc.c:479 __vmap_pages_range_noflush+0x2d4/0x4c8
> [    0.070040] Modules linked in:
> [    0.073421] CPU: 0 PID: 0 Comm: swapper/0 Not tainted 6.2.0-rc4-00538-g6e265f6be0c8 #1
> [    0.082157] Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
> [    0.088972] epc : __vmap_pages_range_noflush+0x2d4/0x4c8
> [    0.094838]  ra : __vmap_pages_range_noflush+0x3e0/0x4c8
> [    0.100707] epc : ffffffff801461e4 ra : ffffffff801462f0 sp : ffffffff81203b90
> [    0.108674]  gp : ffffffff812e9fe0 tp : ffffffff8120fa80 t0 : ffffffe7bfe57000
> [    0.116643]  t1 : ffffffffffff8000 t2 : 00000000000001a7 s0 : ffffffff81203c50
> [    0.124611]  s1 : ffffffe7bfe56040 a0 : 0000000000000000 a1 : ffffffe7c7df2840
> [    0.132588]  a2 : ffffffff812ebae8 a3 : 0000000000001000 a4 : 0000000000080200
> [    0.140564]  a5 : 0000000000fffe00 a6 : 0000000001040056 a7 : ffffffffffffffff
> [    0.148541]  s2 : 0000000001040024 s3 : ffffffff80de1118 s4 : ffffffc80400c000
> [    0.156518]  s5 : ffffffc80400c000 s6 : 00000000000000e7 s7 : 0000000000000000
> [    0.164495]  s8 : ffffffc80400bfff s9 : 00fffffffffff000 s10: ffffffc804008000
> [    0.172472]  s11: ffffffe7bfe0d100 t3 : 0000000000000000 t4 : fffffffffff00000
> [    0.180449]  t5 : 00000000000f0000 t6 : ffffffe7bfe22ac4
> [    0.186317] status: 0000000200000120 badaddr: 0000000000000000 cause: 0000000000000003
> [    0.195060] [<ffffffff8014762a>] __vmalloc_node_range+0x392/0x52e
> [    0.201802] [<ffffffff8000dfc4>] copy_process+0x636/0x1196
> [    0.207878] [<ffffffff8000ec42>] kernel_clone+0x4a/0x2f4
> [    0.213755] [<ffffffff8000f15e>] user_mode_thread+0x7c/0x9a
> [    0.219918] [<ffffffff8077056e>] rest_init+0x28/0xea
> [    0.225429] [<ffffffff80800670>] arch_post_acpi_subsys_init+0x0/0x18
> [    0.232460] [<ffffffff80800de4>] start_kernel+0x72c/0x75c
> [    0.238434] ---[ end trace 0000000000000000 ]---
> n):0kB isolated(file):0kB mapped:0kB dirty:0kB writeback:0kB shmem:0kB shmem_thp: 0kB shmem_pmdmapped: 0kB anon_thp: 0kB writeback_tmp:0kB kernel_stack:0kB pagetables:4kB sec_pagetables:0kB all_unreclaimable? no
> [    0.423801] DMA32 free:940780kB boost:0kB min:0kB low:0kB high:0kB reserved_highatomic:0KB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB writepending:0kB present:1046528kB managed:940780kB mlocked:0kB bounce:0kB free_pcp:0kB local_pcp:0kB free_cma:0kB
> [    0.452034] lowmem_reserve[]: 0 0 0
> [    0.455919] Normal free:126248kB boost:0kB min:0kB low:0kB high:0kB reserved_highatomic:0KB active_anon:0kB inactive_anon:0kB active_file:0kB inactive_file:0kB unevictable:0kB writepending:0kB present:1048576kB managed:126820kB mlocked:0kB bounce:0kB free_pcp:156kB local_pcp:156kB free_cma:0kB
> [    0.484652] lowmem_reserve[]: 0 0 0
> [    0.488538] DMA32: 1*4kB (M) 1*8kB (M) 0*16kB 1*32kB (M) 1*64kB (M) 1*128kB (M) 0*256kB 1*512kB (M) 0*1024kB 1*2048kB (M) 229*4096kB (M) = 940780kB
> [    0.503367] Normal: 2*4kB (ME) 0*8kB 2*16kB (UE) 2*32kB (ME) 3*64kB (UME) 0*128kB 2*256kB (ME) 3*512kB (UME) 3*1024kB (UME) 3*2048kB (UME) 28*4096kB (M) = 126248kB
> [    0.519722] 0 total pagecache pages
> [    0.523579] 0 pages in swap cache
> [    0.527264] Free swap  = 0kB
> [    0.530445] Total swap = 0kB
> [    0.533652] 523776 pages RAM
> [    0.536833] 0 pages HighMem/MovableOnly
> [    0.541090] 256876 pages reserved
> [    0.544833] Unable to handle kernel NULL pointer dereference at virtual address 000000000000003c
> [    0.554576] Oops [#1]
> [    0.557092] Modules linked in:
> [    0.560470] CPU: 0 PID: 0 Comm: swapper/0 Tainted: G        W          6.2.0-rc4-00538-g6e265f6be0c8 #1
> [    0.570841] Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
> [    0.577664] epc : rest_init+0x44/0xea
> [    0.581726]  ra : rest_init+0x44/0xea
> [    0.585788] epc : ffffffff8077058a ra : ffffffff8077058a sp : ffffffff81203f70
> [    0.593764]  gp : ffffffff812e9fe0 tp : ffffffff8120fa80 t0 : 0000000000000000
> [    0.601741]  t1 : 0000000000000000 t2 : 0000000000000040 s0 : ffffffff81203f90
> [    0.609718]  s1 : fffffffffffffff4 a0 : 0000000000000000 a1 : fffffffffffffff4
> [    0.617695]  a2 : 0000000000000000 a3 : 0000000000000000 a4 : 0000000000000000
> [    0.625672]  a5 : 0000000000000000 a6 : 0000000001212dec a7 : ffffffffd8bc86ae
> [    0.633649]  s2 : ffffffff80de0bf0 s3 : ffffffe7c7de7700 s4 : 0000000000000000
> [    0.641626]  s5 : ffffffff812eb018 s6 : ffffffff80a00008 s7 : 0000000000000000
> [    0.649603]  s8 : 00000000bfb85036 s9 : 0000000000000000 s10: 0000000000000000
> [    0.657580]  s11: 0000000000000000 t3 : 0000000000000002 t4 : 0000000000000402
> [    0.665557]  t5 : ffffffff812158a8 t6 : 0000000000000000
> [    0.671416] status: 0000000200000120 badaddr: 000000000000003c cause: 000000000000000d
> [    0.680157] [<ffffffff80800670>] arch_post_acpi_subsys_init+0x0/0x18
> [    0.687188] [<ffffffff80800de4>] start_kernel+0x72c/0x75c
> [    0.693195] ---[ end trace 0000000000000000 ]---
> [    0.698325] Kernel panic - not syncing: Attempted to kill the idle task!
> [    0.705729] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---
  
Conor Dooley Jan. 31, 2023, 6:55 p.m. UTC | #4
Hi Mike,

On Tue, Jan 31, 2023 at 08:41:49PM +0200, Mike Rapoport wrote:
> On Tue, Jan 31, 2023 at 05:47:24PM +0000, Conor Dooley wrote:
> > On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
> > > From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> > > 
> > > Every architecture that supports FLATMEM memory model defines its own
> > > version of pfn_valid() that essentially compares a pfn to max_mapnr.
> > > 
> > > Use mips/powerpc version implemented as static inline as a generic
> > > implementation of pfn_valid() and drop its per-architecture definitions.
> > > 
> > > Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
> > > Acked-by: Arnd Bergmann <arnd@arndb.de>
> > > Acked-by: Guo Ren <guoren@kernel.org>		# csky
> > > Acked-by: Huacai Chen <chenhuacai@loongson.cn>	# LoongArch
> > > Acked-by: Stafford Horne <shorne@gmail.com>	# OpenRISC
> > 
> > Hmm, so this landed in linux-next today and I bisected a boot failure in
> > my CI to it. However, I am not really sure if it is a real issue worth
> > worrying about as the platform it triggered on is supposed to be using
> > SPARSEMEM, but isn't.
> > I had thought that my CI was using a config with SPARSEMEM since that
> > became required for riscv defconfig builds to boot in v6.1-rc1, but I
> > must have just forgotten to add it to my $platform_defconfig builds too.
> > However, those $platform_defconfig builds continued booting without
> > SPARSEMEM enabled until today.
> 
> The issue seems to be that the generic pfn_valid() does not take into
> account pfn_offset when it compares it with max_mapnr.
> Can you please test with the patch below?
> 
> diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memory_model.h
> index 13d2a844d928..6796abe1900e 100644
> --- a/include/asm-generic/memory_model.h
> +++ b/include/asm-generic/memory_model.h
> @@ -26,7 +26,7 @@ static inline int pfn_valid(unsigned long pfn)
>  	extern unsigned long max_mapnr;
>  	unsigned long pfn_offset = ARCH_PFN_OFFSET;
>  
> -	return pfn >= pfn_offset && pfn < max_mapnr;
> +	return pfn >= pfn_offset && (pfn - pfn_offset) < max_mapnr;
>  }
>  #define pfn_valid pfn_valid
>  #endif

Gave that a go, board is booting properly again! Feel free to add a:
Tested-by: Conor Dooley <conor.dooley@microchip.com>

Thanks for the prompt fix!
  
David Hildenbrand Feb. 3, 2023, 2:33 p.m. UTC | #5
On 29.01.23 13:42, Mike Rapoport wrote:
> From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> 
> Every architecture that supports FLATMEM memory model defines its own
> version of pfn_valid() that essentially compares a pfn to max_mapnr.
> 
> Use mips/powerpc version implemented as static inline as a generic
> implementation of pfn_valid() and drop its per-architecture definitions.
> 
> Signed-off-by: Mike Rapoport (IBM) <rppt@kernel.org>
> Acked-by: Arnd Bergmann <arnd@arndb.de>
> Acked-by: Guo Ren <guoren@kernel.org>		# csky
> Acked-by: Huacai Chen <chenhuacai@loongson.cn>	# LoongArch
> Acked-by: Stafford Horne <shorne@gmail.com>	# OpenRISC
> ---

LGTM with the fixup

Reviewed-by: David Hildenbrand <david@redhat.com>
  
Guenter Roeck Feb. 12, 2023, 4:13 p.m. UTC | #6
On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
> From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> 
> Every architecture that supports FLATMEM memory model defines its own
> version of pfn_valid() that essentially compares a pfn to max_mapnr.
> 
> Use mips/powerpc version implemented as static inline as a generic
> implementation of pfn_valid() and drop its per-architecture definitions.
> 

With this patch in the tree, sh4 and sh4eb qemu emulations no longer boot.
Reverting this patch fixes the problem.

Guenter

---
# bad: [6ba8a227fd19d19779005fb66ad7562608e1df83] Add linux-next specific files for 20230210
# good: [4ec5183ec48656cec489c49f989c508b68b518e3] Linux 6.2-rc7
git bisect start 'HEAD' 'v6.2-rc7'
# good: [94613f0efc69ed41f9229ef5c294db3ec37145da] Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
git bisect good 94613f0efc69ed41f9229ef5c294db3ec37145da
# good: [19e62c715fe70dae4582c2874ed3e66715d09af6] Merge branch 'rcu/next' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git
git bisect good 19e62c715fe70dae4582c2874ed3e66715d09af6
# good: [5d8b7ecef7f4a681b6e5538db59ff26c389c0ab6] Merge branch 'for-next' of https://gitlab.com/peda-linux/mux.git
git bisect good 5d8b7ecef7f4a681b6e5538db59ff26c389c0ab6
# good: [c349bf6ec83903b20fe570c5609b9a864a64e09c] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/krisman/unicode.git
git bisect good c349bf6ec83903b20fe570c5609b9a864a64e09c
# good: [5a06a9f17454df38f35672be522ff5eb9b4277d2] selftest: add testing unsharing and counting ksm zero page
git bisect good 5a06a9f17454df38f35672be522ff5eb9b4277d2
# bad: [f5d115a7b06e5661ed5218ffa9a2644c4ff1c135] Merge branch 'mm-nonmm-unstable' into mm-everything
git bisect bad f5d115a7b06e5661ed5218ffa9a2644c4ff1c135
# bad: [acb018d6ea0c055381fba7dddaef386ee28f8075] mm/vmalloc.c: allow vread() to read out vm_map_ram areas
git bisect bad acb018d6ea0c055381fba7dddaef386ee28f8075
# good: [1a5d9782ac969dc6e61c6786500b5160603188ea] mm/mmap: remove __vma_adjust()
git bisect good 1a5d9782ac969dc6e61c6786500b5160603188ea
# good: [4b32363697de957dcc890b6245bec3f58903639a] arm: include asm-generic/memory_model.h from page.h rather than memory.h
git bisect good 4b32363697de957dcc890b6245bec3f58903639a
# bad: [328cf3fa6682ce6a4de6f8bb8009c833dc33f3c8] mm/migrate: convert isolate_movable_page() to use folios
git bisect bad 328cf3fa6682ce6a4de6f8bb8009c833dc33f3c8
# bad: [b704c765b08cabe82adf76a4d1a74f3688eee410] mm/mempolicy: convert queue_pages_pmd() to queue_folios_pmd()
git bisect bad b704c765b08cabe82adf76a4d1a74f3688eee410
# bad: [e5734c8b0edfd2a053a5c256189586a3b1e9f63d] mm, arch: add generic implementation of pfn_valid() for FLATMEM
git bisect bad e5734c8b0edfd2a053a5c256189586a3b1e9f63d
# good: [ad8aecea034c591b9754bc5908da9719853aa7fa] mips: drop definition of pfn_valid() for DISCONTIGMEM
git bisect good ad8aecea034c591b9754bc5908da9719853aa7fa
# first bad commit: [e5734c8b0edfd2a053a5c256189586a3b1e9f63d] mm, arch: add generic implementation of pfn_valid() for FLATMEM
  
Mike Rapoport Feb. 12, 2023, 4:26 p.m. UTC | #7
Hi Guenter,

On Sun, Feb 12, 2023 at 08:13:20AM -0800, Guenter Roeck wrote:
> On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
> > From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> > 
> > Every architecture that supports FLATMEM memory model defines its own
> > version of pfn_valid() that essentially compares a pfn to max_mapnr.
> > 
> > Use mips/powerpc version implemented as static inline as a generic
> > implementation of pfn_valid() and drop its per-architecture definitions.
> > 
> 
> With this patch in the tree, sh4 and sh4eb qemu emulations no longer boot.
> Reverting this patch fixes the problem.
 
Can you please test with only partial revert for arch/sh?

> Guenter
  
Guenter Roeck Feb. 12, 2023, 5:38 p.m. UTC | #8
On 2/12/23 08:26, Mike Rapoport wrote:
> Hi Guenter,
> 
> On Sun, Feb 12, 2023 at 08:13:20AM -0800, Guenter Roeck wrote:
>> On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
>>> From: "Mike Rapoport (IBM)" <rppt@kernel.org>
>>>
>>> Every architecture that supports FLATMEM memory model defines its own
>>> version of pfn_valid() that essentially compares a pfn to max_mapnr.
>>>
>>> Use mips/powerpc version implemented as static inline as a generic
>>> implementation of pfn_valid() and drop its per-architecture definitions.
>>>
>>
>> With this patch in the tree, sh4 and sh4eb qemu emulations no longer boot.
>> Reverting this patch fixes the problem.
>   
> Can you please test with only partial revert for arch/sh?
> 

Sure, that works as well.

Guenter
  
Mike Rapoport Feb. 13, 2023, 1:26 a.m. UTC | #9
On Sun, Feb 12, 2023 at 08:13:20AM -0800, Guenter Roeck wrote:
> On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
> > From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> > 
> > Every architecture that supports FLATMEM memory model defines its own
> > version of pfn_valid() that essentially compares a pfn to max_mapnr.
> > 
> > Use mips/powerpc version implemented as static inline as a generic
> > implementation of pfn_valid() and drop its per-architecture definitions.
> > 
> 
> With this patch in the tree, sh4 and sh4eb qemu emulations no longer boot.
> Reverting this patch fixes the problem.

This should be a better fix than a revert:

diff --git a/arch/sh/mm/init.c b/arch/sh/mm/init.c
index 506784702430..bf1b54055316 100644
--- a/arch/sh/mm/init.c
+++ b/arch/sh/mm/init.c
@@ -301,6 +301,7 @@ void __init paging_init(void)
 	 */
 	max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
 	min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
+	set_max_mapnr(max_low_pfn - min_low_pfn);
 
 	nodes_clear(node_online_map);
 
 
> Guenter
> 
> ---
> # bad: [6ba8a227fd19d19779005fb66ad7562608e1df83] Add linux-next specific files for 20230210
> # good: [4ec5183ec48656cec489c49f989c508b68b518e3] Linux 6.2-rc7
> git bisect start 'HEAD' 'v6.2-rc7'
> # good: [94613f0efc69ed41f9229ef5c294db3ec37145da] Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
> git bisect good 94613f0efc69ed41f9229ef5c294db3ec37145da
> # good: [19e62c715fe70dae4582c2874ed3e66715d09af6] Merge branch 'rcu/next' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git
> git bisect good 19e62c715fe70dae4582c2874ed3e66715d09af6
> # good: [5d8b7ecef7f4a681b6e5538db59ff26c389c0ab6] Merge branch 'for-next' of https://gitlab.com/peda-linux/mux.git
> git bisect good 5d8b7ecef7f4a681b6e5538db59ff26c389c0ab6
> # good: [c349bf6ec83903b20fe570c5609b9a864a64e09c] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/krisman/unicode.git
> git bisect good c349bf6ec83903b20fe570c5609b9a864a64e09c
> # good: [5a06a9f17454df38f35672be522ff5eb9b4277d2] selftest: add testing unsharing and counting ksm zero page
> git bisect good 5a06a9f17454df38f35672be522ff5eb9b4277d2
> # bad: [f5d115a7b06e5661ed5218ffa9a2644c4ff1c135] Merge branch 'mm-nonmm-unstable' into mm-everything
> git bisect bad f5d115a7b06e5661ed5218ffa9a2644c4ff1c135
> # bad: [acb018d6ea0c055381fba7dddaef386ee28f8075] mm/vmalloc.c: allow vread() to read out vm_map_ram areas
> git bisect bad acb018d6ea0c055381fba7dddaef386ee28f8075
> # good: [1a5d9782ac969dc6e61c6786500b5160603188ea] mm/mmap: remove __vma_adjust()
> git bisect good 1a5d9782ac969dc6e61c6786500b5160603188ea
> # good: [4b32363697de957dcc890b6245bec3f58903639a] arm: include asm-generic/memory_model.h from page.h rather than memory.h
> git bisect good 4b32363697de957dcc890b6245bec3f58903639a
> # bad: [328cf3fa6682ce6a4de6f8bb8009c833dc33f3c8] mm/migrate: convert isolate_movable_page() to use folios
> git bisect bad 328cf3fa6682ce6a4de6f8bb8009c833dc33f3c8
> # bad: [b704c765b08cabe82adf76a4d1a74f3688eee410] mm/mempolicy: convert queue_pages_pmd() to queue_folios_pmd()
> git bisect bad b704c765b08cabe82adf76a4d1a74f3688eee410
> # bad: [e5734c8b0edfd2a053a5c256189586a3b1e9f63d] mm, arch: add generic implementation of pfn_valid() for FLATMEM
> git bisect bad e5734c8b0edfd2a053a5c256189586a3b1e9f63d
> # good: [ad8aecea034c591b9754bc5908da9719853aa7fa] mips: drop definition of pfn_valid() for DISCONTIGMEM
> git bisect good ad8aecea034c591b9754bc5908da9719853aa7fa
> # first bad commit: [e5734c8b0edfd2a053a5c256189586a3b1e9f63d] mm, arch: add generic implementation of pfn_valid() for FLATMEM
  
Guenter Roeck Feb. 13, 2023, 6:37 a.m. UTC | #10
On 2/12/23 17:26, Mike Rapoport wrote:
> On Sun, Feb 12, 2023 at 08:13:20AM -0800, Guenter Roeck wrote:
>> On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
>>> From: "Mike Rapoport (IBM)" <rppt@kernel.org>
>>>
>>> Every architecture that supports FLATMEM memory model defines its own
>>> version of pfn_valid() that essentially compares a pfn to max_mapnr.
>>>
>>> Use mips/powerpc version implemented as static inline as a generic
>>> implementation of pfn_valid() and drop its per-architecture definitions.
>>>
>>
>> With this patch in the tree, sh4 and sh4eb qemu emulations no longer boot.
>> Reverting this patch fixes the problem.
> 
> This should be a better fix than a revert:
> 
> diff --git a/arch/sh/mm/init.c b/arch/sh/mm/init.c
> index 506784702430..bf1b54055316 100644
> --- a/arch/sh/mm/init.c
> +++ b/arch/sh/mm/init.c
> @@ -301,6 +301,7 @@ void __init paging_init(void)
>   	 */
>   	max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
>   	min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
> +	set_max_mapnr(max_low_pfn - min_low_pfn);
>   
>   	nodes_clear(node_online_map);
>   

Confirmed, this fixes the problem for me.

Thanks,
Guenter

>   
>> Guenter
>>
>> ---
>> # bad: [6ba8a227fd19d19779005fb66ad7562608e1df83] Add linux-next specific files for 20230210
>> # good: [4ec5183ec48656cec489c49f989c508b68b518e3] Linux 6.2-rc7
>> git bisect start 'HEAD' 'v6.2-rc7'
>> # good: [94613f0efc69ed41f9229ef5c294db3ec37145da] Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git
>> git bisect good 94613f0efc69ed41f9229ef5c294db3ec37145da
>> # good: [19e62c715fe70dae4582c2874ed3e66715d09af6] Merge branch 'rcu/next' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git
>> git bisect good 19e62c715fe70dae4582c2874ed3e66715d09af6
>> # good: [5d8b7ecef7f4a681b6e5538db59ff26c389c0ab6] Merge branch 'for-next' of https://gitlab.com/peda-linux/mux.git
>> git bisect good 5d8b7ecef7f4a681b6e5538db59ff26c389c0ab6
>> # good: [c349bf6ec83903b20fe570c5609b9a864a64e09c] Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/krisman/unicode.git
>> git bisect good c349bf6ec83903b20fe570c5609b9a864a64e09c
>> # good: [5a06a9f17454df38f35672be522ff5eb9b4277d2] selftest: add testing unsharing and counting ksm zero page
>> git bisect good 5a06a9f17454df38f35672be522ff5eb9b4277d2
>> # bad: [f5d115a7b06e5661ed5218ffa9a2644c4ff1c135] Merge branch 'mm-nonmm-unstable' into mm-everything
>> git bisect bad f5d115a7b06e5661ed5218ffa9a2644c4ff1c135
>> # bad: [acb018d6ea0c055381fba7dddaef386ee28f8075] mm/vmalloc.c: allow vread() to read out vm_map_ram areas
>> git bisect bad acb018d6ea0c055381fba7dddaef386ee28f8075
>> # good: [1a5d9782ac969dc6e61c6786500b5160603188ea] mm/mmap: remove __vma_adjust()
>> git bisect good 1a5d9782ac969dc6e61c6786500b5160603188ea
>> # good: [4b32363697de957dcc890b6245bec3f58903639a] arm: include asm-generic/memory_model.h from page.h rather than memory.h
>> git bisect good 4b32363697de957dcc890b6245bec3f58903639a
>> # bad: [328cf3fa6682ce6a4de6f8bb8009c833dc33f3c8] mm/migrate: convert isolate_movable_page() to use folios
>> git bisect bad 328cf3fa6682ce6a4de6f8bb8009c833dc33f3c8
>> # bad: [b704c765b08cabe82adf76a4d1a74f3688eee410] mm/mempolicy: convert queue_pages_pmd() to queue_folios_pmd()
>> git bisect bad b704c765b08cabe82adf76a4d1a74f3688eee410
>> # bad: [e5734c8b0edfd2a053a5c256189586a3b1e9f63d] mm, arch: add generic implementation of pfn_valid() for FLATMEM
>> git bisect bad e5734c8b0edfd2a053a5c256189586a3b1e9f63d
>> # good: [ad8aecea034c591b9754bc5908da9719853aa7fa] mips: drop definition of pfn_valid() for DISCONTIGMEM
>> git bisect good ad8aecea034c591b9754bc5908da9719853aa7fa
>> # first bad commit: [e5734c8b0edfd2a053a5c256189586a3b1e9f63d] mm, arch: add generic implementation of pfn_valid() for FLATMEM
>
  
Mike Rapoport Feb. 13, 2023, 7:54 a.m. UTC | #11
Andrew, 

On Sun, Feb 12, 2023 at 10:37:15PM -0800, Guenter Roeck wrote:
> On 2/12/23 17:26, Mike Rapoport wrote:
> > On Sun, Feb 12, 2023 at 08:13:20AM -0800, Guenter Roeck wrote:
> > > On Sun, Jan 29, 2023 at 02:42:35PM +0200, Mike Rapoport wrote:
> > > > From: "Mike Rapoport (IBM)" <rppt@kernel.org>
> > > > 
> > > > Every architecture that supports FLATMEM memory model defines its own
> > > > version of pfn_valid() that essentially compares a pfn to max_mapnr.
> > > > 
> > > > Use mips/powerpc version implemented as static inline as a generic
> > > > implementation of pfn_valid() and drop its per-architecture definitions.
> > > > 
> > > 
> > > With this patch in the tree, sh4 and sh4eb qemu emulations no longer boot.
> > > Reverting this patch fixes the problem.
> > 
> > This should be a better fix than a revert:
> > 
> > diff --git a/arch/sh/mm/init.c b/arch/sh/mm/init.c
> > index 506784702430..bf1b54055316 100644
> > --- a/arch/sh/mm/init.c
> > +++ b/arch/sh/mm/init.c
> > @@ -301,6 +301,7 @@ void __init paging_init(void)
> >   	 */
> >   	max_low_pfn = max_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
> >   	min_low_pfn = __MEMORY_START >> PAGE_SHIFT;
> > +	set_max_mapnr(max_low_pfn - min_low_pfn);
> >   	nodes_clear(node_online_map);
> 
> Confirmed, this fixes the problem for me.
 
What is your preference for this and m68k fix? Fixups on top of mm-stable
or v3 of the entire series? 

> Thanks,
> Guenter
  

Patch

diff --git a/arch/alpha/include/asm/page.h b/arch/alpha/include/asm/page.h
index 8f3f5eecba28..227d32b6b75f 100644
--- a/arch/alpha/include/asm/page.h
+++ b/arch/alpha/include/asm/page.h
@@ -87,10 +87,6 @@  typedef struct page *pgtable_t;
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
 #define virt_addr_valid(kaddr)	pfn_valid((__pa(kaddr) >> PAGE_SHIFT))
 
-#ifdef CONFIG_FLATMEM
-#define pfn_valid(pfn)		((pfn) < max_mapnr)
-#endif /* CONFIG_FLATMEM */
-
 #include <asm-generic/memory_model.h>
 #include <asm-generic/getorder.h>
 
diff --git a/arch/arc/include/asm/page.h b/arch/arc/include/asm/page.h
index 9a62e1d87967..e43fe27ec54d 100644
--- a/arch/arc/include/asm/page.h
+++ b/arch/arc/include/asm/page.h
@@ -109,7 +109,6 @@  extern int pfn_valid(unsigned long pfn);
 #else /* CONFIG_HIGHMEM */
 
 #define ARCH_PFN_OFFSET		virt_to_pfn(CONFIG_LINUX_RAM_BASE)
-#define pfn_valid(pfn)		(((pfn) - ARCH_PFN_OFFSET) < max_mapnr)
 
 #endif /* CONFIG_HIGHMEM */
 
diff --git a/arch/csky/include/asm/page.h b/arch/csky/include/asm/page.h
index ed7451478b1b..b23e3006a9e0 100644
--- a/arch/csky/include/asm/page.h
+++ b/arch/csky/include/asm/page.h
@@ -39,7 +39,6 @@ 
 
 #define virt_addr_valid(kaddr)  ((void *)(kaddr) >= (void *)PAGE_OFFSET && \
 			(void *)(kaddr) < high_memory)
-#define pfn_valid(pfn)		((pfn) >= ARCH_PFN_OFFSET && ((pfn) - ARCH_PFN_OFFSET) < max_mapnr)
 
 extern void *memset(void *dest, int c, size_t l);
 extern void *memcpy(void *to, const void *from, size_t l);
diff --git a/arch/hexagon/include/asm/page.h b/arch/hexagon/include/asm/page.h
index d7d4f9fca327..9c03b9965f07 100644
--- a/arch/hexagon/include/asm/page.h
+++ b/arch/hexagon/include/asm/page.h
@@ -95,7 +95,6 @@  struct page;
 /* Default vm area behavior is non-executable.  */
 #define VM_DATA_DEFAULT_FLAGS	VM_DATA_FLAGS_NON_EXEC
 
-#define pfn_valid(pfn) ((pfn) < max_mapnr)
 #define virt_addr_valid(kaddr) pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
 
 /*  Need to not use a define for linesize; may move this to another file.  */
diff --git a/arch/ia64/include/asm/page.h b/arch/ia64/include/asm/page.h
index 1b990466d540..783eceab5df3 100644
--- a/arch/ia64/include/asm/page.h
+++ b/arch/ia64/include/asm/page.h
@@ -97,10 +97,6 @@  do {						\
 
 #include <asm-generic/memory_model.h>
 
-#ifdef CONFIG_FLATMEM
-# define pfn_valid(pfn)		((pfn) < max_mapnr)
-#endif
-
 #define page_to_phys(page)	(page_to_pfn(page) << PAGE_SHIFT)
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
 #define pfn_to_kaddr(pfn)	__va((pfn) << PAGE_SHIFT)
diff --git a/arch/loongarch/include/asm/page.h b/arch/loongarch/include/asm/page.h
index 53f284a96182..fb5338b352e6 100644
--- a/arch/loongarch/include/asm/page.h
+++ b/arch/loongarch/include/asm/page.h
@@ -82,19 +82,6 @@  typedef struct { unsigned long pgprot; } pgprot_t;
 
 #define pfn_to_kaddr(pfn)	__va((pfn) << PAGE_SHIFT)
 
-#ifdef CONFIG_FLATMEM
-
-static inline int pfn_valid(unsigned long pfn)
-{
-	/* avoid <linux/mm.h> include hell */
-	extern unsigned long max_mapnr;
-	unsigned long pfn_offset = ARCH_PFN_OFFSET;
-
-	return pfn >= pfn_offset && pfn < max_mapnr;
-}
-
-#endif
-
 #define virt_to_pfn(kaddr)	PFN_DOWN(PHYSADDR(kaddr))
 #define virt_to_page(kaddr)	pfn_to_page(virt_to_pfn(kaddr))
 
diff --git a/arch/m68k/include/asm/page_no.h b/arch/m68k/include/asm/page_no.h
index 0a8ccef777fd..2555ec57149d 100644
--- a/arch/m68k/include/asm/page_no.h
+++ b/arch/m68k/include/asm/page_no.h
@@ -26,8 +26,6 @@  extern unsigned long memory_end;
 #define virt_to_page(addr)	(mem_map + (((unsigned long)(addr)-PAGE_OFFSET) >> PAGE_SHIFT))
 #define page_to_virt(page)	__va(((((page) - mem_map) << PAGE_SHIFT) + PAGE_OFFSET))
 
-#define pfn_valid(pfn)	        ((pfn) < max_mapnr)
-
 #define	virt_addr_valid(kaddr)	(((unsigned long)(kaddr) >= PAGE_OFFSET) && \
 				((unsigned long)(kaddr) < memory_end))
 
diff --git a/arch/microblaze/include/asm/page.h b/arch/microblaze/include/asm/page.h
index 4b8b2fa78fc5..7b9861bcd458 100644
--- a/arch/microblaze/include/asm/page.h
+++ b/arch/microblaze/include/asm/page.h
@@ -112,7 +112,6 @@  extern int page_is_ram(unsigned long pfn);
 #  define page_to_phys(page)     (page_to_pfn(page) << PAGE_SHIFT)
 
 #  define ARCH_PFN_OFFSET	(memory_start >> PAGE_SHIFT)
-#  define pfn_valid(pfn)	((pfn) >= ARCH_PFN_OFFSET && (pfn) < (max_mapnr + ARCH_PFN_OFFSET))
 # endif /* __ASSEMBLY__ */
 
 #define	virt_addr_valid(vaddr)	(pfn_valid(virt_to_pfn(vaddr)))
diff --git a/arch/mips/include/asm/page.h b/arch/mips/include/asm/page.h
index 9286f11ff6ad..5978a8dfb917 100644
--- a/arch/mips/include/asm/page.h
+++ b/arch/mips/include/asm/page.h
@@ -224,19 +224,6 @@  extern phys_addr_t __phys_addr_symbol(unsigned long x);
 
 #define pfn_to_kaddr(pfn)	__va((pfn) << PAGE_SHIFT)
 
-#ifdef CONFIG_FLATMEM
-
-static inline int pfn_valid(unsigned long pfn)
-{
-	/* avoid <linux/mm.h> include hell */
-	extern unsigned long max_mapnr;
-	unsigned long pfn_offset = ARCH_PFN_OFFSET;
-
-	return pfn >= pfn_offset && pfn < max_mapnr;
-}
-
-#endif
-
 #define virt_to_pfn(kaddr)   	PFN_DOWN(virt_to_phys((void *)(kaddr)))
 #define virt_to_page(kaddr)	pfn_to_page(virt_to_pfn(kaddr))
 
diff --git a/arch/nios2/include/asm/page.h b/arch/nios2/include/asm/page.h
index 6a989819a7c1..0ae7d9ce369b 100644
--- a/arch/nios2/include/asm/page.h
+++ b/arch/nios2/include/asm/page.h
@@ -86,15 +86,6 @@  extern struct page *mem_map;
 
 # define pfn_to_kaddr(pfn)	__va((pfn) << PAGE_SHIFT)
 
-static inline bool pfn_valid(unsigned long pfn)
-{
-	/* avoid <linux/mm.h> include hell */
-	extern unsigned long max_mapnr;
-	unsigned long pfn_offset = ARCH_PFN_OFFSET;
-
-	return pfn >= pfn_offset && pfn < max_mapnr;
-}
-
 # define virt_to_page(vaddr)	pfn_to_page(PFN_DOWN(virt_to_phys(vaddr)))
 # define virt_addr_valid(vaddr)	pfn_valid(PFN_DOWN(virt_to_phys(vaddr)))
 
diff --git a/arch/openrisc/include/asm/page.h b/arch/openrisc/include/asm/page.h
index aab6e64d6db4..52b0d7e76446 100644
--- a/arch/openrisc/include/asm/page.h
+++ b/arch/openrisc/include/asm/page.h
@@ -80,8 +80,6 @@  typedef struct page *pgtable_t;
 
 #define page_to_phys(page)      ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
 
-#define pfn_valid(pfn)          ((pfn) < max_mapnr)
-
 #define virt_addr_valid(kaddr)	(pfn_valid(virt_to_pfn(kaddr)))
 
 #endif /* __ASSEMBLY__ */
diff --git a/arch/parisc/include/asm/page.h b/arch/parisc/include/asm/page.h
index 6faaaa3ebe9b..667e703c0e8f 100644
--- a/arch/parisc/include/asm/page.h
+++ b/arch/parisc/include/asm/page.h
@@ -155,10 +155,6 @@  extern int npmem_ranges;
 #define __pa(x)			((unsigned long)(x)-PAGE_OFFSET)
 #define __va(x)			((void *)((unsigned long)(x)+PAGE_OFFSET))
 
-#ifndef CONFIG_SPARSEMEM
-#define pfn_valid(pfn)		((pfn) < max_mapnr)
-#endif
-
 #ifdef CONFIG_HUGETLB_PAGE
 #define HPAGE_SHIFT		PMD_SHIFT /* fixed for transparent huge pages */
 #define HPAGE_SIZE      	((1UL) << HPAGE_SHIFT)
diff --git a/arch/powerpc/include/asm/page.h b/arch/powerpc/include/asm/page.h
index edf1dd1b0ca9..f2b6bf5687d0 100644
--- a/arch/powerpc/include/asm/page.h
+++ b/arch/powerpc/include/asm/page.h
@@ -117,15 +117,6 @@  extern long long virt_phys_offset;
 
 #ifdef CONFIG_FLATMEM
 #define ARCH_PFN_OFFSET		((unsigned long)(MEMORY_START >> PAGE_SHIFT))
-#ifndef __ASSEMBLY__
-extern unsigned long max_mapnr;
-static inline bool pfn_valid(unsigned long pfn)
-{
-	unsigned long min_pfn = ARCH_PFN_OFFSET;
-
-	return pfn >= min_pfn && pfn < max_mapnr;
-}
-#endif
 #endif
 
 #define virt_to_pfn(kaddr)	(__pa(kaddr) >> PAGE_SHIFT)
diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
index 9f432c1b5289..7fed7c431928 100644
--- a/arch/riscv/include/asm/page.h
+++ b/arch/riscv/include/asm/page.h
@@ -171,11 +171,6 @@  extern phys_addr_t __phys_addr_symbol(unsigned long x);
 
 #define sym_to_pfn(x)           __phys_to_pfn(__pa_symbol(x))
 
-#ifdef CONFIG_FLATMEM
-#define pfn_valid(pfn) \
-	(((pfn) >= ARCH_PFN_OFFSET) && (((pfn) - ARCH_PFN_OFFSET) < max_mapnr))
-#endif
-
 #endif /* __ASSEMBLY__ */
 
 #define virt_addr_valid(vaddr)	({						\
diff --git a/arch/sh/include/asm/page.h b/arch/sh/include/asm/page.h
index eca5daa43b93..09ac6c7faee0 100644
--- a/arch/sh/include/asm/page.h
+++ b/arch/sh/include/asm/page.h
@@ -169,9 +169,6 @@  typedef struct page *pgtable_t;
 #define PFN_START		(__MEMORY_START >> PAGE_SHIFT)
 #define ARCH_PFN_OFFSET		(PFN_START)
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
-#ifdef CONFIG_FLATMEM
-#define pfn_valid(pfn)		((pfn) >= min_low_pfn && (pfn) < max_low_pfn)
-#endif
 #define virt_addr_valid(kaddr)	pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
 
 #include <asm-generic/memory_model.h>
diff --git a/arch/sparc/include/asm/page_32.h b/arch/sparc/include/asm/page_32.h
index fff8861df107..6be6f683f98f 100644
--- a/arch/sparc/include/asm/page_32.h
+++ b/arch/sparc/include/asm/page_32.h
@@ -130,7 +130,6 @@  extern unsigned long pfn_base;
 #define ARCH_PFN_OFFSET		(pfn_base)
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
 
-#define pfn_valid(pfn)		(((pfn) >= (pfn_base)) && (((pfn)-(pfn_base)) < max_mapnr))
 #define virt_addr_valid(kaddr)	((((unsigned long)(kaddr)-PAGE_OFFSET)>>PAGE_SHIFT) < max_mapnr)
 
 #include <asm-generic/memory_model.h>
diff --git a/arch/um/include/asm/page.h b/arch/um/include/asm/page.h
index cdbd9653aa14..84866127d074 100644
--- a/arch/um/include/asm/page.h
+++ b/arch/um/include/asm/page.h
@@ -108,7 +108,6 @@  extern unsigned long uml_physmem;
 #define phys_to_pfn(p) ((p) >> PAGE_SHIFT)
 #define pfn_to_phys(pfn) PFN_PHYS(pfn)
 
-#define pfn_valid(pfn) ((pfn) < max_mapnr)
 #define virt_addr_valid(v) pfn_valid(phys_to_pfn(__pa(v)))
 
 #include <asm-generic/memory_model.h>
diff --git a/arch/x86/include/asm/page_32.h b/arch/x86/include/asm/page_32.h
index df42f8aa99e4..580d71aca65a 100644
--- a/arch/x86/include/asm/page_32.h
+++ b/arch/x86/include/asm/page_32.h
@@ -15,10 +15,6 @@  extern unsigned long __phys_addr(unsigned long);
 #define __phys_addr_symbol(x)	__phys_addr(x)
 #define __phys_reloc_hide(x)	RELOC_HIDE((x), 0)
 
-#ifdef CONFIG_FLATMEM
-#define pfn_valid(pfn)		((pfn) < max_mapnr)
-#endif /* CONFIG_FLATMEM */
-
 #include <linux/string.h>
 
 static inline void clear_page(void *page)
diff --git a/arch/x86/include/asm/page_64.h b/arch/x86/include/asm/page_64.h
index 198e03e59ca1..cc6b8e087192 100644
--- a/arch/x86/include/asm/page_64.h
+++ b/arch/x86/include/asm/page_64.h
@@ -39,10 +39,6 @@  extern unsigned long __phys_addr_symbol(unsigned long);
 
 #define __phys_reloc_hide(x)	(x)
 
-#ifdef CONFIG_FLATMEM
-#define pfn_valid(pfn)          ((pfn) < max_pfn)
-#endif
-
 void clear_page_orig(void *page);
 void clear_page_rep(void *page);
 void clear_page_erms(void *page);
diff --git a/arch/xtensa/include/asm/page.h b/arch/xtensa/include/asm/page.h
index 493eb7083b1a..a77d04972eb9 100644
--- a/arch/xtensa/include/asm/page.h
+++ b/arch/xtensa/include/asm/page.h
@@ -11,6 +11,8 @@ 
 #ifndef _XTENSA_PAGE_H
 #define _XTENSA_PAGE_H
 
+#include <linux/const.h>
+
 #include <asm/processor.h>
 #include <asm/types.h>
 #include <asm/cache.h>
@@ -189,8 +191,6 @@  static inline unsigned long ___pa(unsigned long va)
 #endif
 #define __va(x)	\
 	((void *)((unsigned long) (x) - PHYS_OFFSET + PAGE_OFFSET))
-#define pfn_valid(pfn) \
-	((pfn) >= ARCH_PFN_OFFSET && ((pfn) - ARCH_PFN_OFFSET) < max_mapnr)
 
 #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
 #define page_to_virt(page)	__va(page_to_pfn(page) << PAGE_SHIFT)
diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memory_model.h
index a2c8ed60233a..13d2a844d928 100644
--- a/include/asm-generic/memory_model.h
+++ b/include/asm-generic/memory_model.h
@@ -19,6 +19,18 @@ 
 #define __page_to_pfn(page)	((unsigned long)((page) - mem_map) + \
 				 ARCH_PFN_OFFSET)
 
+#ifndef pfn_valid
+static inline int pfn_valid(unsigned long pfn)
+{
+	/* avoid <linux/mm.h> include hell */
+	extern unsigned long max_mapnr;
+	unsigned long pfn_offset = ARCH_PFN_OFFSET;
+
+	return pfn >= pfn_offset && pfn < max_mapnr;
+}
+#define pfn_valid pfn_valid
+#endif
+
 #elif defined(CONFIG_SPARSEMEM_VMEMMAP)
 
 /* memmap is virtually contiguous.  */
diff --git a/include/asm-generic/page.h b/include/asm-generic/page.h
index 6fc47561814c..c0be2edeb484 100644
--- a/include/asm-generic/page.h
+++ b/include/asm-generic/page.h
@@ -84,8 +84,6 @@  extern unsigned long memory_end;
 #define page_to_phys(page)      ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
 #endif
 
-#define pfn_valid(pfn)		((pfn) >= ARCH_PFN_OFFSET && ((pfn) - ARCH_PFN_OFFSET) < max_mapnr)
-
 #define	virt_addr_valid(kaddr)	(((void *)(kaddr) >= (void *)PAGE_OFFSET) && \
 				((void *)(kaddr) < (void *)memory_end))