[PATCH 1/2] powerpc/mm: Check memblock_add against MAX_PHYSMEM_BITS range

2018-06-21 Thread Aneesh Kumar K.V
With SPARSEMEM config enabled, we make sure that we don't add sections beyond
MAX_PHYSMEM_BITS range. This results in not building vmemmap mapping for
range beyond max range. But our memblock layer looks the device tree and create
mapping for the full memory range. Prevent this by checking against
MAX_PHSYSMEM_BITS when doing memblock_add.

We don't do similar check for memeblock_reserve_range. If reserve range is 
beyond
MAX_PHYSMEM_BITS we expect that to be configured with 'nomap'. Any other
reserved range should come from existing memblock ranges which we already
filtered while adding.

This avoids crash as below when running on a system with system ram config above
MAX_PHSYSMEM_BITS

 Unable to handle kernel paging request for data at address 0xc00a00100440
 Faulting instruction address: 0xc1034118
 cpu 0x0: Vector: 300 (Data Access) at [c124fb30]
 pc: c1034118: __free_pages_bootmem+0xc0/0x1c0
 lr: c103b258: free_all_bootmem+0x19c/0x22c
 sp: c124fdb0
msr: 92001033
dar: c00a00100440
  dsisr: 4000
   current = 0xc120dd00
   paca= 0xc1f6^I irqmask: 0x03^I irq_happened: 0x01
 pid   = 0, comm = swapper
 [c124fe20] c103b258 free_all_bootmem+0x19c/0x22c
 [c124fee0] c1010a68 mem_init+0x3c/0x5c
 [c124ff00] c100401c start_kernel+0x298/0x5e4
 [c124ff90] c000b57c start_here_common+0x1c/0x520

Signed-off-by: Aneesh Kumar K.V 
---
 arch/powerpc/kernel/prom.c | 32 +---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/arch/powerpc/kernel/prom.c b/arch/powerpc/kernel/prom.c
index 05e7fb47a7a4..8f32f14ba508 100644
--- a/arch/powerpc/kernel/prom.c
+++ b/arch/powerpc/kernel/prom.c
@@ -440,6 +440,29 @@ static int __init early_init_dt_scan_chosen_ppc(unsigned 
long node,
return 1;
 }
 
+/*
+ * Compare the range against max mem limit and update
+ * size if it cross the limit.
+ */
+
+#ifdef CONFIG_SPARSEMEM
+static bool validate_mem_limit(u64 base, u64 *size)
+{
+   u64 max_mem = 1UL << (MAX_PHYSMEM_BITS);
+
+   if (base >= max_mem)
+   return false;
+   if ((base + *size) > max_mem)
+   *size = max_mem - base;
+   return true;
+}
+#else
+static bool validate_mem_limit(u64 base, u64 *size)
+{
+   return true;
+}
+#endif
+
 #ifdef CONFIG_PPC_PSERIES
 /*
  * Interpret the ibm dynamic reconfiguration memory LMBs.
@@ -494,7 +517,8 @@ static void __init early_init_drmem_lmb(struct drmem_lmb 
*lmb,
}
 
DBG("Adding: %llx -> %llx\n", base, size);
-   memblock_add(base, size);
+   if (validate_mem_limit(base, &size))
+   memblock_add(base, size);
} while (--rngs);
 }
 #endif /* CONFIG_PPC_PSERIES */
@@ -548,8 +572,10 @@ void __init early_init_dt_add_memory_arch(u64 base, u64 
size)
}
 
/* Add the chunk to the MEMBLOCK list */
-   if (add_mem_to_memblock)
-   memblock_add(base, size);
+   if (add_mem_to_memblock) {
+   if (validate_mem_limit(base, &size))
+   memblock_add(base, size);
+   }
 }
 
 static void __init early_reserve_mem_dt(void)
-- 
2.17.1



Re: [1/2] powerpc/mm: Check memblock_add against MAX_PHYSMEM_BITS range

2018-07-24 Thread Michael Ellerman
On Thu, 2018-06-21 at 08:31:57 UTC, "Aneesh Kumar K.V" wrote:
> With SPARSEMEM config enabled, we make sure that we don't add sections beyond
> MAX_PHYSMEM_BITS range. This results in not building vmemmap mapping for
> range beyond max range. But our memblock layer looks the device tree and 
> create
> mapping for the full memory range. Prevent this by checking against
> MAX_PHSYSMEM_BITS when doing memblock_add.
> 
> We don't do similar check for memeblock_reserve_range. If reserve range is 
> beyond
> MAX_PHYSMEM_BITS we expect that to be configured with 'nomap'. Any other
> reserved range should come from existing memblock ranges which we already
> filtered while adding.
> 
> This avoids crash as below when running on a system with system ram config 
> above
> MAX_PHSYSMEM_BITS
> 
>  Unable to handle kernel paging request for data at address 0xc00a00100440
>  Faulting instruction address: 0xc1034118
>  cpu 0x0: Vector: 300 (Data Access) at [c124fb30]
>  pc: c1034118: __free_pages_bootmem+0xc0/0x1c0
>  lr: c103b258: free_all_bootmem+0x19c/0x22c
>  sp: c124fdb0
> msr: 92001033
> dar: c00a00100440
>   dsisr: 4000
>current = 0xc120dd00
>paca= 0xc1f6^I irqmask: 0x03^I irq_happened: 0x01
>  pid   = 0, comm = swapper
>  [c124fe20] c103b258 free_all_bootmem+0x19c/0x22c
>  [c124fee0] c1010a68 mem_init+0x3c/0x5c
>  [c124ff00] c100401c start_kernel+0x298/0x5e4
>  [c124ff90] c000b57c start_here_common+0x1c/0x520
> 
> Signed-off-by: Aneesh Kumar K.V 

Series applied to powerpc next, thanks.

https://git.kernel.org/powerpc/c/6aba0c84ec474534bbae3675e95464

cheers