Skip to content

Commit

Permalink
generic: fix detect_memory_region() function
Browse files Browse the repository at this point in the history
This patch fixes the broken detect_memory_region() function on
6.6 kernel.

Signed-off-by: Shiji Yang <yangshiji66@qq.com>
  • Loading branch information
DragonBluep authored and PolynomialDivision committed Apr 5, 2024
1 parent 7753b14 commit c5dee97
Showing 1 changed file with 56 additions and 0 deletions.
@@ -0,0 +1,56 @@
From: Shiji Yang <yangshiji66@outlook.com>
Date: Wed, 13 Mar 2024 20:28:37 +0800
Subject: [PATCH] mips: kernel: fix detect_memory_region() function

1. Do not use memcmp() on unallocated memory, as the new introduced
fortify dynamic object size check[1] will report unexpected result.
2. Use a fixed pattern instead of a random function pointer as the
magic value.
3. Flip magic value and double check it.

[1] 439a1bcac648 ("fortify: Use __builtin_dynamic_object_size() when available")
Signed-off-by: Shiji Yang <yangshiji66@outlook.com>
---
arch/mips/kernel/setup.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)

--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -46,6 +46,8 @@
#include <asm/prom.h>
#include <asm/fw/fw.h>

+#define MIPS_MEM_TEST_PATTERN 0xaa5555aa
+
#ifdef CONFIG_MIPS_ELF_APPENDED_DTB
char __section(".appended_dtb") __appended_dtb[0x100000];
#endif /* CONFIG_MIPS_ELF_APPENDED_DTB */
@@ -90,7 +92,7 @@ static struct resource bss_resource = {
unsigned long __kaslr_offset __ro_after_init;
EXPORT_SYMBOL(__kaslr_offset);

-static void *detect_magic __initdata = detect_memory_region;
+static u32 detect_magic __initdata;

#ifdef CONFIG_MIPS_AUTO_PFN_OFFSET
unsigned long ARCH_PFN_OFFSET;
@@ -99,12 +101,16 @@ EXPORT_SYMBOL(ARCH_PFN_OFFSET);

void __init detect_memory_region(phys_addr_t start, phys_addr_t sz_min, phys_addr_t sz_max)
{
- void *dm = &detect_magic;
+ void *dm = (void *)KSEG1ADDR(&detect_magic);
phys_addr_t size;

for (size = sz_min; size < sz_max; size <<= 1) {
- if (!memcmp(dm, dm + size, sizeof(detect_magic)))
- break;
+ __raw_writel(MIPS_MEM_TEST_PATTERN, dm);
+ if (__raw_readl(dm) == __raw_readl(dm + size)) {
+ __raw_writel(~MIPS_MEM_TEST_PATTERN, dm);
+ if (__raw_readl(dm) == __raw_readl(dm + size))
+ break;
+ }
}

pr_debug("Memory: %lluMB of RAM detected at 0x%llx (min: %lluMB, max: %lluMB)\n",

4 comments on commit c5dee97

@guidosarducci
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DragonBluep @PolynomialDivision This appears to be written for 32-bit systems (e.g. KSEG1ADDR) and breaks building for mips64el/malta unless reverted.

Could you fix please? Note: original upstream detect_memory_region() came from @blogic

@DragonBluep
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guidosarducci Sorry for the trouble, but I do not have time and environment to debug it recently. Do you have any ath79/ramips target devices? I guess removing KSEG1ADDR() should still works.

@guidosarducci
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guidosarducci Sorry for the trouble, but I do not have time and environment to debug it recently. Do you have any ath79/ramips target devices? I guess removing KSEG1ADDR() should still works.

@DragonBluep I see the upstream submission hasn't had any comments for over a month. I suggest pinging that mailing list again for review; maybe there's an obvious fix someone can point out. Why require a physical ath79/ramips device to test? This looks like general code that could be covered by malta/be and malta/le64 for example.

@PolynomialDivision @blogic Could you please revert this given the build breakage and lack of test/debug time and any upstream review.

@guidosarducci
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included a revert in my malta 6.6 PR #15215. Feel free to pull out/use that commit.

Please sign in to comment.