Skip to contentSkip to conversation

arm64: mm: Fix pfn_is_map_memory() misreporting mapped memory

PATCH In review Vladimir Murzin 4 participants 14 messages Sep 8 – Sep 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: mm: Fix pfn_is_map_memory() misreporting mapped memory

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory")
Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +2 -1
index fbf215ecc7d0..5da80e1e1727 100644
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 31 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory")
Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 177

Quoted 7 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }

Yuck, this is really grotty :(

Can we at least predicate the extra memblock_is_region_memory() check on having a page-size > 4k?

Will

#2 lore raw
Quoted 35 lines · Will Deacon
Quoted 34 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory")
Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
Quoted 9 lines

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 177

Quoted 4 lines
arch/arm64/mm/init.c +2 -1
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }

Yuck, this is really grotty :(

Can we at least predicate the extra memblock_is_region_memory() check on having a page-size > 4k?

The same can happen with 4K page size and subpage no-map and I have not seen anything prohibiting subpage no-map ...

Cheers
Vladimir

Quoted 2 lines

Will

maintainer
#3 lore raw
Quoted 42 lines · Vladimir Murzin
Quoted 41 lines · Will Deacon
Quoted 31 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory")
Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 177

Quoted 7 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }

Yuck, this is really grotty :(

Quoted 6 lines
Quoted 3 lines

Can we at least predicate the extra memblock_is_region_memory() check on having a page-size > 4k?

The same can happen with 4K page size and subpage no-map and I have not seen anything prohibiting subpage no-map ...

Ah yes, I made a similar observation during the review of the offending commit. Oh well. Let's see what others think before we merge this.

Will

#4 lore raw
In reply to Vladimir Murzin

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Note that I recently removed the frivolous use of MEMBLOCK_NOMAP in __map_mem() so the remaining calls should mostly be true reservations originating from the boot environment.

maintainer
#5 lore raw
Quoted 59 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

mm/memblock.c line 1126

Quoted 8 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }

Makes sense to me, just lacks kernel-doc update ;-)

Quoted 3 lines

Note that I recently removed the frivolous use of MEMBLOCK_NOMAP in __map_mem() so the remaining calls should mostly be true reservations originating from the boot environment.

There is still riscv :)

Signature · 2 lines
Sincerely yours,
Mike.
#6 lore raw
In reply to Ard Biesheuvel
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 7 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

mm/memblock.c line 1128

Quoted 10 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Thanks
Vladimir

Quoted 8 lines

Note that I recently removed the frivolous use of MEMBLOCK_NOMAP in __map_mem() so the remaining calls should mostly be true reservations originating from the boot environment.

#7 lore raw
Quoted 59 lines · Vladimir Murzin
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 4 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

Quoted 16 lines

mm/memblock.c line 1128

Quoted 13 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

Yes.

Quoted 3 lines

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Probably not, but there are some corner cases to consider before we go down this route:
- what happens when marking a range no-map where the outward rounding would exceed the limits of the existing memblock memory range?
- what happens when removing a non-page aligned region from memblock that intersects with a (page aligned) no-map region?

maintainer
#8 lore raw
Quoted 79 lines · Ard Biesheuvel
Quoted 59 lines · Vladimir Murzin
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 4 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

Quoted 16 lines

mm/memblock.c line 1128

Quoted 13 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

Yes.

Quoted 8 lines
Quoted 3 lines

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Probably not, but there are some corner cases to consider before we go down this route:
- what happens when marking a range no-map where the outward rounding would exceed the limits of the existing memblock memory range?

AFAIR it's fine, memblock_isolate will clamp the update. Didn't check the code though.

Quoted 2 lines

- what happens when removing a non-page aligned region from memblock that intersects with a (page aligned) no-map region?

It will make the nomap region non-page aligned.

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#9 lore raw
In reply to Ard Biesheuvel
Quoted 85 lines · Ard Biesheuvel
Quoted 59 lines · Vladimir Murzin
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 4 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

Quoted 16 lines

mm/memblock.c line 1128

Quoted 13 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

Yes.

Quoted 3 lines

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Quoted 6 lines

Probably not, but there are some corner cases to consider before we go down this route:
- what happens when marking a range no-map where the outward rounding would exceed the limits of the existing memblock memory range?
- what happens when removing a non-page aligned region from memblock that intersects with a (page aligned) no-map region?

Another thing is that maybe we should force memblock.memory only to page aligned ranges. x86 uses memblock_trim_memory() for that since, like, forever.

Signature · 2 lines
Sincerely yours,
Mike.
#10 lore raw
Quoted 93 lines · Mike Rapoport
Quoted 85 lines · Ard Biesheuvel
Quoted 59 lines · Vladimir Murzin
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 4 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

Quoted 16 lines

mm/memblock.c line 1128

Quoted 13 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

Yes.

Quoted 3 lines

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Quoted 6 lines

Probably not, but there are some corner cases to consider before we go down this route:
- what happens when marking a range no-map where the outward rounding would exceed the limits of the existing memblock memory range?
- what happens when removing a non-page aligned region from memblock that intersects with a (page aligned) no-map region?

Quoted 4 lines

Another thing is that maybe we should force memblock.memory only to page aligned ranges. x86 uses memblock_trim_memory() for that since, like, forever.

Interesting. It would make sense to do the same on arm64.

But if a NOMAP region ends in the middle of a page, that code will trim on both sides, and throw away the shared page entirely. So all flag manipulation occurring beforehand should be rounded outward (not just NOMAP, although I'm not sure if there are others that may get set on non-page granular regions)

maintainer
#11 lore raw
Quoted 99 lines · Ard Biesheuvel
Quoted 93 lines · Mike Rapoport
Quoted 85 lines · Ard Biesheuvel
Quoted 59 lines · Vladimir Murzin
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 4 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

Quoted 16 lines

mm/memblock.c line 1128

Quoted 13 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

Yes.

Quoted 3 lines

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Quoted 6 lines

Probably not, but there are some corner cases to consider before we go down this route:
- what happens when marking a range no-map where the outward rounding would exceed the limits of the existing memblock memory range?
- what happens when removing a non-page aligned region from memblock that intersects with a (page aligned) no-map region?

Quoted 4 lines

Another thing is that maybe we should force memblock.memory only to page aligned ranges. x86 uses memblock_trim_memory() for that since, like, forever.

Quoted 4 lines

Interesting. It would make sense to do the same on arm64.

But if a NOMAP region ends in the middle of a page, that code will trim on both sides, and throw away the shared page entirely.

Honestly, I think the whole sub-page NOMAP is firmware's problem. While rounding up in _mark_nomap() makes sense because we cannot "not map" a sub-page range, I would add a big fat WARN_ON("FIX YOUR FIRMWARE") if the rounding actually happens.

As for the trimming on arm64, I believe that's relevant, because again, we cannot really work with sub-page ranges in memblock.memory and they are anyway discarded by for_each_mem_pfn_range() that's used all over mm initialization.

Quoted 3 lines

So all flag manipulation occurring beforehand should be rounded outward (not just NOMAP, although I'm not sure if there are others that may get set on non-page granular regions)

Virtually anything, but I don't think any existing caller does that.

Signature · 2 lines
Sincerely yours,
Mike.
#12 lore raw
Quoted 100 lines · Mike Rapoport
Quoted 99 lines · Ard Biesheuvel
Quoted 97 lines · Mike Rapoport
Quoted 85 lines · Ard Biesheuvel
Quoted 59 lines · Vladimir Murzin
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 4 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

Quoted 16 lines

mm/memblock.c line 1128

Quoted 13 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

Yes.

Quoted 3 lines

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Quoted 6 lines

Probably not, but there are some corner cases to consider before we go down this route:
- what happens when marking a range no-map where the outward rounding would exceed the limits of the existing memblock memory range?
- what happens when removing a non-page aligned region from memblock that intersects with a (page aligned) no-map region?

Another thing is that maybe we should force memblock.memory only to page aligned ranges. x86 uses memblock_trim_memory() for that since, like, forever.

Quoted 6 lines
Quoted 4 lines

Interesting. It would make sense to do the same on arm64.

But if a NOMAP region ends in the middle of a page, that code will trim on both sides, and throw away the shared page entirely.

Honestly, I think the whole sub-page NOMAP is firmware's problem.

Not entirely. The firmware does not know whether the OS will use 4k pages or 64k pages, and rounding up all reservations to 64k just in case might be wasteful.

So I think it is reasonable to require 4k alignment for NOMAP regions, and the OS should decide whether to round outward or not. It does mean that the firmware should avoid treating the misaligned pieces at the edges as memory where it can place things like initrd etc.

Quoted 5 lines

While
rounding up in _mark_nomap() makes sense because we cannot "not map" a sub-page range, I would add a big fat WARN_ON("FIX YOUR FIRMWARE") if the rounding actually happens.

I don't think that is very productive for 4k -> 64k rounding.

Quoted 5 lines

As for the trimming on arm64, I believe that's relevant, because again, we cannot really work with sub-page ranges in memblock.memory and they are anyway discarded by for_each_mem_pfn_range() that's used all over mm initialization.

Yeah I think the rounding is needed in any case, but I don't think it is the firmware's job to mark unrelated adjacent memory as no-map only because the OS might decide to use a coarser granule.

maintainer
#13 lore raw
Quoted 103 lines · Ard Biesheuvel
Quoted 102 lines · Mike Rapoport
Quoted 101 lines · Ard Biesheuvel
Quoted 93 lines · Mike Rapoport
Quoted 85 lines · Ard Biesheuvel
Quoted 59 lines · Vladimir Murzin
Quoted 47 lines · Ard Biesheuvel

(cc Mike)

Quoted 32 lines · Vladimir Murzin

Commit 7ace06a01efa ("arm64: mm: fix accidental linear mapping of no-map reserved memory") removed sub-page no-map regions from the linear mapping. However, pfn_is_map_memory() can still report that such a region is mapped.

For instance, with 64K pages, say we have

normal: ...–0xa2007fff
no-map: 0xa2008000–0xa200ffff
normal: 0xa2010000–...

The range 0xa2000000–0xa200ffff is not linearly mapped, but pfn_is_map_memory(__phys_to_pfn(0xa2008000)) aligns the address to page granularity and passes 0xa2000000 to memblock_is_map_memory(). That address belongs to the preceding normal memblock region, so the no-map region is ignored and the function returns true.

Fix that by checking if entire page is subset of memory block.

Fixes 7ace06a01efa ("arm64: mm: fix accidental linear mapping of

no-map reserved memory")

Assisted-by LLM
---
 arch/arm64/mm/init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/mm/init.c +0 -0
index fbf215ecc7d0..5da80e1e1727 100644

arch/arm64/mm/init.c int pfn_is_map_memory(unsigned long pfn) · line 179

Quoted 9 lines
arch/arm64/mm/init.c +2 -1
@@ -172,7 +172,8 @@ int pfn_is_map_memory(unsigned long pfn)
 	if (PHYS_PFN(addr) != pfn)
 		return 0;
 
-	return memblock_is_map_memory(addr);
+	return memblock_is_region_memory(addr, PAGE_SIZE) &&
+		memblock_is_map_memory(addr);
 }
 EXPORT_SYMBOL(pfn_is_map_memory);
 

memblock_is_region_memory() only tells you whether the range in question is covered by a single entry in memblock.memory.regions[].

Quoted 4 lines

memblock has other flags, which may or may not be set on sub-page regions too. So I don't think this is the right solution in the general case, even if it fixes your example.

Ack.

Quoted 4 lines

Given that the no-map attribute fundamentally only applies to page granular regions, it would make sense to round those outwards whenever they are created.

Quoted 16 lines

mm/memblock.c line 1128

Quoted 13 lines
mm/memblock.c +4 -0
@@ -1119,6 +1119,10 @@
 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
 {
+       phys_addr_t end = PAGE_ALIGN(base + size);
+
+       base &= PAGE_MASK;
+       size = end - base;
        return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
 }
 
 

Should the same be applied to memblock_clear_nomap()?

Yes.

Quoted 3 lines

This is indeed much nicer way to fix the problem! Now, when no-map is rounded outwards, do we still need 7ace06a01efa?

Quoted 6 lines

Probably not, but there are some corner cases to consider before we go down this route:
- what happens when marking a range no-map where the outward rounding would exceed the limits of the existing memblock memory range?
- what happens when removing a non-page aligned region from memblock that intersects with a (page aligned) no-map region?

Quoted 4 lines

Another thing is that maybe we should force memblock.memory only to page aligned ranges. x86 uses memblock_trim_memory() for that since, like, forever.

Interesting. It would make sense to do the same on arm64.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

But if a NOMAP region ends in the middle of a page, that code will trim on both sides, and throw away the shared page entirely.

Honestly, I think the whole sub-page NOMAP is firmware's problem.

Not entirely. The firmware does not know whether the OS will use 4k pages or 64k pages, and rounding up all reservations to 64k just in case might be wasteful.

I keep forgetting about 64k pages :)

Quoted 5 lines

So I think it is reasonable to require 4k alignment for NOMAP regions, and the OS should decide whether to round outward or not. It does mean that the firmware should avoid treating the misaligned pieces at the edges as memory where it can place things like initrd etc.

Quoted 7 lines
Quoted 5 lines

While
rounding up in _mark_nomap() makes sense because we cannot "not map" a sub-page range, I would add a big fat WARN_ON("FIX YOUR FIRMWARE") if the rounding actually happens.

I don't think that is very productive for 4k -> 64k rounding.

I agree that the strict requirement should be for 4k.
Or without loss of generality "minimal supported base page size" :)

Quoted 9 lines
Quoted 5 lines

As for the trimming on arm64, I believe that's relevant, because again, we cannot really work with sub-page ranges in memblock.memory and they are anyway discarded by for_each_mem_pfn_range() that's used all over mm initialization.

Yeah I think the rounding is needed in any case, but I don't think it is the firmware's job to mark unrelated adjacent memory as no-map only because the OS might decide to use a coarser granule.

I afraid this will create weird edge cases where firmware mixes parts of a 64k pages for no-map and other things at 4k granularity.

Signature · 2 lines
Sincerely yours,
Mike.