linux-mips.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Serge Semin <fancer.lancer@gmail.com>
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 <fancer.lancer@gmail.com>
Subject: [PATCH 05/21] MIPS memblock: Alter initrd memory reservation method
Date: Mon, 19 Dec 2016 05:07:30 +0300	[thread overview]
Message-ID: <1482113266-13207-6-git-send-email-fancer.lancer@gmail.com> (raw)
In-Reply-To: <1482113266-13207-1-git-send-email-fancer.lancer@gmail.com>

Since memblock is used, initrd memory region can be easily
verified and reserved if looks ok. Verification method will be
useful for other reservation methods.

Signed-off-by: Serge Semin <fancer.lancer@gmail.com>
---
 arch/mips/kernel/setup.c | 157 ++++++++++++++++-------------
 1 file changed, 87 insertions(+), 70 deletions(-)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 789aafe..d2f38ac 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -82,6 +82,8 @@ static struct resource data_resource = { .name = "Kernel data", };
 
 static void *detect_magic __initdata = detect_memory_region;
 
+static phys_addr_t __initdata mips_lowmem_limit;
+
 /*
  * General method to add RAM regions to the system
  *
@@ -266,6 +268,38 @@ static int __init early_parse_mem(char *p)
 early_param("mem", early_parse_mem);
 
 /*
+ * Helper method checking whether passed lowmem region is valid
+ */
+static bool __init is_lowmem_and_valid(const char *name, phys_addr_t base,
+				       phys_addr_t size)
+{
+	phys_addr_t end = base + size;
+
+	/* Check whether region belongs to actual memory */
+	if (!memblock_is_region_memory(base, size)) {
+		pr_err("%s %08zx @ %pa is not a memory region", name,
+			(size_t)size, &base);
+		return false;
+	}
+
+	/* Check whether region belongs to low memory */
+	if (end > mips_lowmem_limit) {
+		pr_err("%s %08zx @ %pa is out of low memory", name,
+			(size_t)size, &base);
+	       return false;
+	}
+
+	/* Check whether region is free */
+	if (memblock_is_region_reserved(base, size)) {
+		pr_err("%s %08zx @ %pa overlaps in-use memory", name,
+			(size_t)size, &base);
+		return false;
+	}
+
+	return true;
+}
+
+/*
  * Manage initrd
  */
 #ifdef CONFIG_BLK_DEV_INITRD
@@ -292,47 +326,6 @@ static int __init rd_size_early(char *p)
 }
 early_param("rd_size", rd_size_early);
 
-/* it returns the next free pfn after initrd */
-static unsigned long __init init_initrd(void)
-{
-	unsigned long end;
-
-	/*
-	 * Board specific code or command line parser should have
-	 * already set up initrd_start and initrd_end. In these cases
-	 * perfom sanity checks and use them if all looks good.
-	 */
-	if (!initrd_start || initrd_end <= initrd_start)
-		goto disable;
-
-	if (initrd_start & ~PAGE_MASK) {
-		pr_err("initrd start must be page aligned\n");
-		goto disable;
-	}
-	if (initrd_start < PAGE_OFFSET) {
-		pr_err("initrd start < PAGE_OFFSET\n");
-		goto disable;
-	}
-
-	/*
-	 * Sanitize initrd addresses. For example firmware
-	 * can't guess if they need to pass them through
-	 * 64-bits values if the kernel has been built in pure
-	 * 32-bit. We need also to switch from KSEG0 to XKPHYS
-	 * addresses now, so the code can now safely use __pa().
-	 */
-	end = __pa(initrd_end);
-	initrd_end = (unsigned long)__va(end);
-	initrd_start = (unsigned long)__va(__pa(initrd_start));
-
-	ROOT_DEV = Root_RAM0;
-	return PFN_UP(end);
-disable:
-	initrd_start = 0;
-	initrd_end = 0;
-	return 0;
-}
-
 /* In some conditions (e.g. big endian bootloader with a little endian
    kernel), the initrd might appear byte swapped.  Try to detect this and
    byte swap it if needed.  */
@@ -362,26 +355,64 @@ static void __init maybe_bswap_initrd(void)
 #endif
 }
 
-static void __init finalize_initrd(void)
+/*
+ * Check and reserve memory occupied by initrd
+ */
+static void __init mips_reserve_initrd_mem(void)
 {
-	unsigned long size = initrd_end - initrd_start;
+	phys_addr_t phys_initrd_start, phys_initrd_end, phys_initrd_size;
 
-	if (size == 0) {
-		printk(KERN_INFO "Initrd not found or empty");
+	/*
+	 * Board specific code or command line parser should have already set
+	 * up initrd_start and initrd_end. In these cases perform sanity checks
+	 * and use them if all looks good.
+	 */
+	if (!initrd_start || initrd_end <= initrd_start) {
+		pr_info("No initrd found");
 		goto disable;
 	}
-	if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) {
-		printk(KERN_ERR "Initrd extends beyond end of memory");
+	if (initrd_start & ~PAGE_MASK) {
+		pr_err("Initrd start must be page aligned");
 		goto disable;
 	}
+	if (initrd_start < PAGE_OFFSET) {
+		pr_err("Initrd start < PAGE_OFFSET");
+		goto disable;
+	}
+
+	/*
+	 * Sanitize initrd addresses. For example firmware can't guess if they
+	 * need to pass them through 64-bits values if the kernel has been
+	 * built in pure 32-bit. We need also to switch from KSEG0 to XKPHYS
+	 * addresses now, so the code can now safely use __pa().
+	 */
+	phys_initrd_start = __pa(initrd_start);
+	phys_initrd_end = __pa(initrd_end);
+	phys_initrd_size = phys_initrd_end - phys_initrd_start;
 
+	/* Check whether initrd region is within available lowmem and free */
+	if (!is_lowmem_and_valid("Initrd", phys_initrd_start, phys_initrd_size))
+		goto disable;
+
+	/* Initrd may be byteswapped in Octeon */
 	maybe_bswap_initrd();
 
-	reserve_bootmem(__pa(initrd_start), size, BOOTMEM_DEFAULT);
+	/* Memory for initrd can be reserved now */
+	memblock_reserve(phys_initrd_start, phys_initrd_size);
+
+	/* Convert initrd to virtual addresses back (needed for x32 -> x64) */
+	initrd_start = (unsigned long)__va(phys_initrd_start);
+	initrd_end = (unsigned long)__va(phys_initrd_end);
+
+	/* It's OK to have initrd below actual memory start. Really? */
 	initrd_below_start_ok = 1;
 
-	pr_info("Initial ramdisk at: 0x%lx (%lu bytes)\n",
-		initrd_start, size);
+	pr_info("Initial ramdisk at: 0x%lx (%zu bytes)\n",
+		initrd_start, (size_t)phys_initrd_size);
+
+	/* Set root device to be first ram disk */
+	ROOT_DEV = Root_RAM0;
+
 	return;
 disable:
 	printk(KERN_CONT " - disabling initrd\n");
@@ -391,12 +422,7 @@ disable:
 
 #else  /* !CONFIG_BLK_DEV_INITRD */
 
-static unsigned long __init init_initrd(void)
-{
-	return 0;
-}
-
-#define finalize_initrd()	do {} while (0)
+static void __init mips_reserve_initrd_mem(void) { }
 
 #endif
 
@@ -408,8 +434,8 @@ static unsigned long __init init_initrd(void)
 
 static void __init bootmem_init(void)
 {
-	init_initrd();
-	finalize_initrd();
+	/* Check and reserve memory occupied by initrd */
+	mips_reserve_initrd_mem();
 }
 
 #else  /* !CONFIG_SGI_IP27 */
@@ -421,13 +447,9 @@ static void __init bootmem_init(void)
 	unsigned long bootmap_size;
 	int i;
 
-	/*
-	 * Sanity check any INITRD first. We don't take it into account
-	 * for bootmem setup initially, rely on the end-of-kernel-code
-	 * as our memory range starting point. Once bootmem is inited we
-	 * will reserve the area used for the initrd.
-	 */
-	init_initrd();
+	/* Check and reserve memory occupied by initrd */
+	mips_reserve_initrd_mem();
+
 	reserved_end = (unsigned long) PFN_UP(__pa_symbol(&_end));
 
 	/*
@@ -618,11 +640,6 @@ static void __init bootmem_init(void)
 #endif
 	}
 #endif
-
-	/*
-	 * Reserve initrd memory if needed.
-	 */
-	finalize_initrd();
 }
 
 #endif	/* CONFIG_SGI_IP27 */
-- 
2.6.6

  parent reply	other threads:[~2016-12-19  2:10 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-19  2:07 [PATCH 00/21] MIPS memblock: Remove bootmem code and switch to NO_BOOTMEM Serge Semin
2016-12-19  2:07 ` [PATCH 01/21] MIPS memblock: Unpin dts memblock sanity check method Serge Semin
2016-12-19  4:21   ` kbuild test robot
2016-12-19  4:21     ` kbuild test robot
2016-12-19  4:28   ` kbuild test robot
2016-12-19  4:28     ` kbuild test robot
2016-12-22 20:57   ` Rob Herring
2016-12-22 21:57     ` Serge Semin
2016-12-19  2:07 ` [PATCH 02/21] MIPS memblock: Add dts mem and reserved-mem callbacks Serge Semin
2016-12-19  4:13   ` kbuild test robot
2016-12-19  4:13     ` kbuild test robot
2016-12-19  2:07 ` [PATCH 03/21] MIPS memblock: Alter traditional add_memory_region() method Serge Semin
2016-12-19  2:07 ` [PATCH 04/21] MIPS memblock: Alter user-defined memory parameter parser Serge Semin
2017-01-23  7:55   ` Marcin Nowakowski
2017-01-23  7:55     ` Marcin Nowakowski
2016-12-19  2:07 ` Serge Semin [this message]
2016-12-19  5:08   ` [PATCH 05/21] MIPS memblock: Alter initrd memory reservation method kbuild test robot
2016-12-19  5:08     ` kbuild test robot
2016-12-19  2:07 ` [PATCH 06/21] MIPS memblock: Alter kexec-crashkernel parameters parser Serge Semin
2016-12-19  2:07 ` [PATCH 07/21] MIPS memblock: Alter elfcorehdr " Serge Semin
2016-12-19  4:09   ` kbuild test robot
2016-12-19  4:09     ` kbuild test robot
2016-12-19  2:07 ` [PATCH 08/21] MIPS memblock: Move kernel parameters parser into individual method Serge Semin
2016-12-19  2:07 ` [PATCH 09/21] MIPS memblock: Move kernel memory reservation to " Serge Semin
2016-12-19  2:07 ` [PATCH 10/21] MIPS memblock: Discard bootmem allocator initialization Serge Semin
2016-12-19  4:28   ` kbuild test robot
2016-12-19  4:28     ` kbuild test robot
2017-01-23  7:55   ` Marcin Nowakowski
2017-01-23  7:55     ` Marcin Nowakowski
2016-12-19  2:07 ` [PATCH 11/21] MIPS memblock: Add memblock sanity check method Serge Semin
2016-12-19  2:07 ` [PATCH 12/21] MIPS memblock: Add memblock print outs in debug Serge Semin
2016-12-19  2:07 ` [PATCH 13/21] MIPS memblock: Add memblock allocator initialization Serge Semin
2016-12-19  2:07 ` [PATCH 14/21] MIPS memblock: Alter IO resources initialization method Serge Semin
2016-12-19  2:07 ` [PATCH 15/21] MIPS memblock: Alter weakened MAAR " Serge Semin
2016-12-19  2:07 ` [PATCH 16/21] MIPS memblock: Alter paging " Serge Semin
2016-12-19  2:07 ` [PATCH 17/21] MIPS memblock: Alter high memory freeing method Serge Semin
2016-12-19  2:07 ` [PATCH 18/21] MIPS memblock: Slightly improve buddy allocator init method Serge Semin
2016-12-19  2:07 ` [PATCH 19/21] MIPS memblock: Add print out method of kernel virtual memory layout Serge Semin
2016-12-19 12:04   ` Matt Redfearn
2016-12-19 12:04     ` Matt Redfearn
2016-12-19 12:09     ` Serge Semin
2016-12-19 13:02     ` James Hogan
2016-12-19 13:02       ` James Hogan
2016-12-19 13:15       ` Serge Semin
2016-12-19  2:07 ` [PATCH 20/21] MIPS memblock: Add free low memory test method call Serge Semin
2016-12-19  2:07 ` [PATCH 21/21] MIPS memblock: Deactivate old bootmem allocator Serge Semin
2017-01-23  7:55 ` [PATCH 00/21] MIPS memblock: Remove bootmem code and switch to NO_BOOTMEM Marcin Nowakowski
2017-01-23  7:55   ` Marcin Nowakowski
2017-03-02  3:06   ` Florian Fainelli
2017-01-23 14:51 ` Joshua Kinard
2017-05-22  9:48 ` Marcin Nowakowski
2017-05-22  9:48   ` Marcin Nowakowski
2017-05-22 10:26   ` Serge Semin
2017-05-22 12:19     ` Marcin Nowakowski
2017-05-22 12:19       ` Marcin Nowakowski
2017-05-22 12:47     ` Joshua Kinard
2017-05-22 13:03       ` Serge Semin
2017-05-22 13:20         ` Alexander Sverdlin
2017-05-22 13:20           ` Alexander Sverdlin
2017-05-22 13:29           ` Serge Semin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1482113266-13207-6-git-send-email-fancer.lancer@gmail.com \
    --to=fancer.lancer@gmail.com \
    --cc=Sergey.Semin@t-platforms.ru \
    --cc=alexander.sverdlin@nokia.com \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=james.hogan@imgtec.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@linux-mips.org \
    --cc=matt.redfearn@imgtec.com \
    --cc=paul.burton@imgtec.com \
    --cc=rabinv@axis.com \
    --cc=ralf@linux-mips.org \
    --cc=robh+dt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).