linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: <Conor.Dooley@microchip.com>
To: <linux-riscv@lists.infradead.org>
Cc: <linux-kernel@vger.kernel.org>, <devicetree@vger.kernel.org>,
	<frowand.list@gmail.com>, <robh+dt@kernel.org>,
	<mick@ics.forth.gr>, <paul.walmsley@sifive.com>,
	<palmer@dabbelt.com>, <aou@eecs.berkeley.edu>,
	<Valentina.FernandezAlanis@microchip.com>,
	<Daire.McNamara@microchip.com>
Subject: RISC-V reserved memory problems
Date: Tue, 16 Aug 2022 20:41:05 +0000	[thread overview]
Message-ID: <f8e67f82-103d-156c-deb0-d6d6e2756f5e@microchip.com> (raw)
In-Reply-To: <8e10bf15-9fa9-fe90-1656-35bf3e87e7f8@microchip.com>

[-- Attachment #1: Type: text/plain, Size: 5825 bytes --]

Hey all,
We've run into a bit of a problem with reserved memory on PolarFire, or
more accurately a pair of problems that seem to have opposite fixes.

The first of these problems is triggered when trying to implement a
remoteproc driver. To get the reserved memory buffer, remoteproc
does an of_reserved_mem_lookup(), something like:

	np = of_parse_phandle(pdev->of_node, "memory-region", 0);
	if (!np)
		return -EINVAL;

	rmem = of_reserved_mem_lookup(np);
	if (!rmem)
		return -EINVAL;

of_reserved_mem_lookup() then uses reserved_mem[i].name to try and find
a match - but this was triggering kernel panics for us. We did some
debugging and found that the name string's pointer was pointing to an
address in the 0x4000_0000 range. The minimum reproduction for this
crash is attached - it hacks in some print_reserved_mem()s into
setup_vm_final() around a tlb flush so you can see the before/after.
(You'll need a reserved memory node in your dts to replicate)

The output is like so, with the same crash as in the remoteproc driver:

[    0.000000] Linux version 6.0.0-rc1-00001-g0d9d6953d834 (conor@wendy) (riscv64-unknown-linux-gnu-gcc (g5964b5cd727) 11.1.0, GNU ld (GNU Binutils) 2.37) #1 SMP Tue Aug 16 13:42:09 IST 2022
[    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] before flush
[    0.000000] OF: reserved mem: debug name is fabricbuf@ae000000
[    0.000000] after flush
[    0.000000] Unable to handle kernel paging request at virtual address 00000000401c31ac
[    0.000000] Oops [#1]
[    0.000000] Modules linked in:
[    0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 6.0.0-rc1-00001-g0d9d6953d834 #1
[    0.000000] Hardware name: Microchip PolarFire-SoC Icicle Kit (DT)
[    0.000000] epc : string+0x4a/0xea
[    0.000000]  ra : vsnprintf+0x1e4/0x336
[    0.000000] epc : ffffffff80335ea0 ra : ffffffff80338936 sp : ffffffff81203be0
[    0.000000]  gp : ffffffff812e0a98 tp : ffffffff8120de40 t0 : 0000000000000000
[    0.000000]  t1 : ffffffff81203e28 t2 : 7265736572203a46 s0 : ffffffff81203c20
[    0.000000]  s1 : ffffffff81203e28 a0 : ffffffff81203d22 a1 : 0000000000000000
[    0.000000]  a2 : ffffffff81203d08 a3 : 0000000081203d21 a4 : ffffffffffffffff
[    0.000000]  a5 : 00000000401c31ac a6 : ffff0a00ffffff04 a7 : ffffffffffffffff
[    0.000000]  s2 : ffffffff81203d08 s3 : ffffffff81203d00 s4 : 0000000000000008
[    0.000000]  s5 : ffffffff000000ff s6 : 0000000000ffffff s7 : 00000000ffffff00
[    0.000000]  s8 : ffffffff80d9821a s9 : ffffffff81203d22 s10: 0000000000000002
[    0.000000]  s11: ffffffff80d9821c t3 : ffffffff812f3617 t4 : ffffffff812f3617
[    0.000000]  t5 : ffffffff812f3618 t6 : ffffffff81203d08
[    0.000000] status: 0000000200000100 badaddr: 00000000401c31ac cause: 000000000000000d
[    0.000000] [<ffffffff80338936>] vsnprintf+0x1e4/0x336
[    0.000000] [<ffffffff80055ae2>] vprintk_store+0xf6/0x344
[    0.000000] [<ffffffff80055d86>] vprintk_emit+0x56/0x192
[    0.000000] [<ffffffff80055ed8>] vprintk_default+0x16/0x1e
[    0.000000] [<ffffffff800563d2>] vprintk+0x72/0x80
[    0.000000] [<ffffffff806813b2>] _printk+0x36/0x50
[    0.000000] [<ffffffff8068af48>] print_reserved_mem+0x1c/0x24
[    0.000000] [<ffffffff808057ec>] paging_init+0x528/0x5bc
[    0.000000] [<ffffffff808031ae>] setup_arch+0xd0/0x592
[    0.000000] [<ffffffff8080070e>] start_kernel+0x82/0x73c
[    0.000000] ---[ end trace 0000000000000000 ]---
[    0.000000] Kernel panic - not syncing: Attempted to kill the idle task!
[    0.000000] ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]---

We traced this back to early_init_fdt_scan_reserved_mem() in
setup_bootmem() - moving it later back up the boot sequence to
after the dt has been remapped etc has fixed the problem for us.

The least movement to get it working is attached, and also pushed
here: git.kernel.org/conor/c/1735589baefc

The second problem is a bit more complicated to explain - but we
found the solution conflicted with the remoteproc fix as we had
to move early_init_fdt_scan_reserved_mem() _earlier_ in the boot
process to solve this one.

We want to have a node in our devicetree that contains some memory
that is non-cached & marked as reserved-memory. Maybe we have just
missed something, but from what we've seen:
- the really early setup looks at the dtb, picks the highest bit
   of memory and puts the dtb etc there so it can start using it
- early_init_fdt_scan_reserved_mem() is then called, which figures
   out if memory is reserved or not.

Unfortunately, the highest bit of memory is the non-cached bit so
everything falls over, but we can avoid this by moving the call to
early_init_fdt_scan_reserved_mem() above the dtb memblock alloc that
takes place right before it in setup_bootmem().

Obviously, both of these changes are moving the function call in
opposite directions and we can only really do one of them. We are not
sure if what we are doing with the non-cached reserved-memory section
is just not permitted & cannot work - or if this is something that
was overlooked for RISC-V specifically and works for other archs.

It does seem like the first issue is a real bug, and I am happy to
submit the patch for that whenever - but having two problems with
opposite fixes seemed as if there was something else lurking that we
just don't have enough understanding to detect.

Any help would be great!

Thanks,
Conor.




[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: fix_reserved_mem.patch --]
[-- Type: text/x-patch; name="fix_reserved_mem.patch", Size: 1840 bytes --]

From 1735589baefc264486f142947012e50710955744 Mon Sep 17 00:00:00 2001
From: Conor Dooley <conor.dooley@microchip.com>
Date: Thu, 14 Jul 2022 10:39:20 +0100
Subject: riscv: fix reserved memory setup

Currently, RISC-V sets up reserved memory using the "early" copy of the
device tree. As a result, when trying to get a reserved memory region
using of_reserved_mem_lookup(), the pointer to reserved memory regions
is using the early, pre-virtual-memory address which causes a kernel
panic.

Move early_init_fdt_scan_reserved_mem() further along the boot seqeunce
so that the names are using the correct virtual memory addresses.

Reported-by: Valentina Fernandez <valentina.fernandezalanis@microchip.com>
Fixes: 922b0375fc93 ("riscv: Fix memblock reservation for device tree blob")
Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
---
 arch/riscv/kernel/setup.c | 1 +
 arch/riscv/mm/init.c      | 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/kernel/setup.c b/arch/riscv/kernel/setup.c
index 95ef6e2bf45c0..62ece4fbc92ad 100644
--- a/arch/riscv/kernel/setup.c
+++ b/arch/riscv/kernel/setup.c
@@ -283,6 +283,7 @@ void __init setup_arch(char **cmdline_p)
 	else
 		pr_err("No DTB found in kernel mappings\n");
 #endif
+	early_init_fdt_scan_reserved_mem();
 	misc_mem_init();
 
 	init_resources();
diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index b56a0a75533fe..50a1b6edd4918 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -262,7 +262,6 @@ static void __init setup_bootmem(void)
 			memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
 	}
 
-	early_init_fdt_scan_reserved_mem();
 	dma_contiguous_reserve(dma32_phys_limit);
 	if (IS_ENABLED(CONFIG_64BIT))
 		hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
-- 
cgit 



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: debug_reserved_memory.patch --]
[-- Type: text/x-patch; name="debug_reserved_memory.patch", Size: 2463 bytes --]

From 14447dc618a3007bf17dd27c03f7fec095efbc6f Mon Sep 17 00:00:00 2001
From: Valentina Fernandez <valentina.fernandezalanis@microchip.com>
Date: Wed, 13 Jul 2022 10:56:47 +0100
Subject: [PATCH] Debug tlb_flush with reserved memory

---
 arch/riscv/boot/dts/microchip/Makefile        |   1 +
 .../microchip/mpfs-icicle-kit-context-a.dts   | 203 ++++++++++++++++++
 arch/riscv/mm/init.c                          |   9 +-
 drivers/of/of_reserved_mem.c                  |   6 +
 include/linux/of_reserved_mem.h               |   2 +
 5 files changed, 220 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/boot/dts/microchip/mpfs-icicle-kit-context-a.dts

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index d466ec670e1f..5458519c3eb4 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -20,7 +20,7 @@
 #include <linux/dma-map-ops.h>
 #include <linux/crash_dump.h>
 #include <linux/hugetlb.h>
-
+#include <linux/of_reserved_mem.h>
 #include <asm/fixmap.h>
 #include <asm/tlbflush.h>
 #include <asm/sections.h>
@@ -1112,8 +1112,15 @@ static void __init setup_vm_final(void)
 
 	/* Move to swapper page table */
 	csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
+
+	pr_err("before flush\n");
+	print_reserved_mem();
+
 	local_flush_tlb_all();
 
+	pr_err("after flush\n");
+	print_reserved_mem();
+
 	pt_ops_set_late();
 }
 #else
diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 75caa6f5d36f..6aaebb05730d 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -445,3 +445,9 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
 	return NULL;
 }
 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
+
+void print_reserved_mem(void)
+{
+	pr_err("debug name is %s\n", reserved_mem[0].name);
+}
+EXPORT_SYMBOL_GPL(print_reserved_mem);
\ No newline at end of file
diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h
index 4de2a24cadc9..1fe504af3944 100644
--- a/include/linux/of_reserved_mem.h
+++ b/include/linux/of_reserved_mem.h
@@ -40,6 +40,8 @@ int of_reserved_mem_device_init_by_name(struct device *dev,
 void of_reserved_mem_device_release(struct device *dev);
 
 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np);
+
+void print_reserved_mem(void);
 #else
 
 #define RESERVEDMEM_OF_DECLARE(name, compat, init)			\
-- 
2.25.1



[-- Attachment #4: Type: text/plain, Size: 161 bytes --]

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

       reply	other threads:[~2022-08-17 15:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <8e10bf15-9fa9-fe90-1656-35bf3e87e7f8@microchip.com>
2022-08-16 20:41 ` Conor.Dooley [this message]
2022-08-18 14:32   ` RISC-V reserved memory problems Heinrich Schuchardt
2022-08-23  9:36     ` Conor.Dooley
2022-11-03 11:46   ` Evgenii Shatokhin
2022-11-03 12:07     ` Conor Dooley

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=f8e67f82-103d-156c-deb0-d6d6e2756f5e@microchip.com \
    --to=conor.dooley@microchip.com \
    --cc=Daire.McNamara@microchip.com \
    --cc=Valentina.FernandezAlanis@microchip.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=devicetree@vger.kernel.org \
    --cc=frowand.list@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mick@ics.forth.gr \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --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).