Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1762357AbcLSC1A (ORCPT ); Sun, 18 Dec 2016 21:27:00 -0500 Received: from mail-lf0-f66.google.com ([209.85.215.66]:36448 "EHLO mail-lf0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752156AbcLSCH4 (ORCPT ); Sun, 18 Dec 2016 21:07:56 -0500 From: Serge Semin To: ralf@linux-mips.org, paul.burton@imgtec.com, rabinv@axis.com, matt.redfearn@imgtec.com, james.hogan@imgtec.com, alexander.sverdlin@nokia.com, robh+dt@kernel.org, frowand.list@gmail.com Cc: Sergey.Semin@t-platforms.ru, linux-mips@linux-mips.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Serge Semin Subject: [PATCH 01/21] MIPS memblock: Unpin dts memblock sanity check method Date: Mon, 19 Dec 2016 05:07:26 +0300 Message-Id: <1482113266-13207-2-git-send-email-fancer.lancer@gmail.com> X-Mailer: git-send-email 2.6.6 In-Reply-To: <1482113266-13207-1-git-send-email-fancer.lancer@gmail.com> References: <1482113266-13207-1-git-send-email-fancer.lancer@gmail.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3390 Lines: 108 It's necessary to check whether retrieved from dts memory regions fits to page alignment and limits restrictions. Sometimes it is necessary to perform the same checks, but ito add the memory regions into a different subsystem. MIPS is going to be that case. Signed-off-by: Serge Semin --- drivers/of/fdt.c | 47 +++++++++++++++++++++++--------- include/linux/of_fdt.h | 1 + 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index 1f98156..1ee958f 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -983,44 +983,65 @@ int __init early_init_dt_scan_chosen(unsigned long node, const char *uname, #define MAX_MEMBLOCK_ADDR ((phys_addr_t)~0) #endif -void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) +int __init sanity_check_dt_memory(phys_addr_t *out_base, + phys_addr_t *out_size) { + phys_addr_t base = *out_base, size = *out_size; const u64 phys_offset = MIN_MEMBLOCK_ADDR; if (!PAGE_ALIGNED(base)) { if (size < PAGE_SIZE - (base & ~PAGE_MASK)) { - pr_warn("Ignoring memory block 0x%llx - 0x%llx\n", + pr_err("Memblock 0x%llx - 0x%llx isn't page aligned\n", base, base + size); - return; + return -EINVAL; } + pr_warn("Memblock 0x%llx - 0x%llx shifted to ", + base, base + size); size -= PAGE_SIZE - (base & ~PAGE_MASK); base = PAGE_ALIGN(base); + pr_cont("0x%llx - 0x%llx\n", base, base + size); } size &= PAGE_MASK; if (base > MAX_MEMBLOCK_ADDR) { - pr_warning("Ignoring memory block 0x%llx - 0x%llx\n", - base, base + size); - return; + pr_err("Memblock 0x%llx - 0x%llx exceeds max address\n", + base, base + size); + return -EINVAL; } if (base + size - 1 > MAX_MEMBLOCK_ADDR) { - pr_warning("Ignoring memory range 0x%llx - 0x%llx\n", - ((u64)MAX_MEMBLOCK_ADDR) + 1, base + size); + pr_warn("Memblock 0x%llx - 0x%llx truncated to ", + base, base + size); size = MAX_MEMBLOCK_ADDR - base + 1; + pr_cont("0x%llx - 0x%llx\n", base, base + size); } if (base + size < phys_offset) { - pr_warning("Ignoring memory block 0x%llx - 0x%llx\n", - base, base + size); - return; + pr_err("Memblock 0x%llx - 0x%llx is below phys offset\n", + base, base + size); + return -EINVAL; } + if (base < phys_offset) { - pr_warning("Ignoring memory range 0x%llx - 0x%llx\n", - base, phys_offset); + pr_warn("Memblock 0x%llx - 0x%llx truncated to ", + base, base + size); size -= phys_offset - base; base = phys_offset; + pr_cont("0x%llx - 0x%llx\n", base, base + size); } + + /* Set the output base address and size */ + *out_base = base; + *out_size = size; + + return 0; +} + +void __init __weak early_init_dt_add_memory_arch(u64 base, u64 size) +{ + if (sanity_check_dt_memory(&base, &size)) + return; + memblock_add(base, size); } diff --git a/include/linux/of_fdt.h b/include/linux/of_fdt.h index df9ef38..ddf93c5 100644 --- a/include/linux/of_fdt.h +++ b/include/linux/of_fdt.h @@ -84,6 +84,7 @@ extern const void *of_flat_dt_match_machine(const void *default_match, const void * (*get_next_compat)(const char * const**)); /* Other Prototypes */ +extern int sanity_check_dt_memory(phys_addr_t *base, phys_addr_t *size); extern void unflatten_device_tree(void); extern void unflatten_and_copy_device_tree(void); extern void early_init_devtree(void *); -- 2.6.6