Skip to contentSkip to conversation

armv8: mmu: don't panic when unmapping a page which isn't mapped

PATCH In review Casey Connolly 2 participants 4 messages Jun 12 – Jun 23 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 armv8: mmu: don't panic when unmapping a page which isn't mapped

If a caller tries to unmap a single page which is already part of an unmapped block, set_one_region() will panic since there isn't a valid block or table to contain the page. Detect this case explicitly and return since no action needs to be taken.

---
Depends on my previous series which fixes unmapping regions

https://lore.kernel.org/u-boot/[email protected]/

---
 arch/arm/cpu/armv8/cache_v8.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/cpu/armv8/cache_v8.c +5 -0
index 6c85022556ad..472887b5d99b 100644
@@ -1003,8 +1003,13 @@ static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
 	int levelshift = level2shift(level);
 	u64 levelsize = 1ULL << levelshift;
 	u64 *pte = find_pte(start, level);
 
+	/* Trying to unmap a region which is part of an already unmapped block, nothing to do */
+	if (pte_type(pte) == PTE_TYPE_FAULT && attrs == PTE_TYPE_FAULT && size < levelsize) {
+		return size;
+	}
+
 	/* Can we can just modify the current level block/page? */
 	if (is_aligned(start, size, levelsize)) {
 		if (attrs == PTE_TYPE_FAULT) {
 			if (pte_type(pte) == PTE_TYPE_TABLE && level < 3)
Signature · 1 line
2.53.0
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Casey Connolly

If a caller tries to unmap a single page which is already part of an unmapped block, set_one_region() will panic since there isn't a valid block or table to contain the page. Detect this case explicitly and return since no action needs to be taken.

Quoted 6 lines

---
Depends on my previous series which fixes unmapping regions

https://lore.kernel.org/u-boot/[email protected]/
---

Quoted 24 lines
 arch/arm/cpu/armv8/cache_v8.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/cpu/armv8/cache_v8.c +5 -1
index 6c85022556ad..472887b5d99b 100644
@@ -1003,8 +1003,13 @@ static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
        int levelshift = level2shift(level);
        u64 levelsize = 1ULL << levelshift;
        u64 *pte = find_pte(start, level);
 
+       /* Trying to unmap a region which is part of an already unmapped block, nothing to do */
+       if (pte_type(pte) == PTE_TYPE_FAULT && attrs == PTE_TYPE_FAULT && size < levelsize) {
+               return size;
+       }
+
        /* Can we can just modify the current level block/page? */
        if (is_aligned(start, size, levelsize)) {
                if (attrs == PTE_TYPE_FAULT) {
                        if (pte_type(pte) == PTE_TYPE_TABLE && level < 3)
--

2.53.0

#2 lore raw
Quoted 29 lines · Ilias Apalodimas
Quoted 6 lines · Casey Connolly

If a caller tries to unmap a single page which is already part of an unmapped block, set_one_region() will panic since there isn't a valid block or table to contain the page. Detect this case explicitly and return since no action needs to be taken.

Quoted 6 lines

---
Depends on my previous series which fixes unmapping regions

https://lore.kernel.org/u-boot/[email protected]/
---

Quoted 12 lines
 arch/arm/cpu/armv8/cache_v8.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/cpu/armv8/cache_v8.c +0 -0
index 6c85022556ad..472887b5d99b 100644
@@ -1003,8 +1003,13 @@ static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
        int levelshift = level2shift(level);
        u64 levelsize = 1ULL << levelshift;
        u64 *pte = find_pte(start, level);
 
Quoted 4 lines

arch/arm/cpu/armv8/cache_v8.c static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level) · line 1010

Quoted 4 lines
arch/arm/cpu/armv8/cache_v8.c +4 -0
+       /* Trying to unmap a region which is part of an already unmapped block, nothing to do */
+       if (pte_type(pte) == PTE_TYPE_FAULT && attrs == PTE_TYPE_FAULT && size < levelsize) {
+               return size;
+       }

On a second thought, you should check the size of the region as well. What if the user starts from an unmapped region with size X but requests to unmap a size Y with Y > X?

Thanks
/Ilias

Quoted 8 lines
Quoted 8 lines
arch/arm/cpu/armv8/cache_v8.c +1 -1
+
        /* Can we can just modify the current level block/page? */
        if (is_aligned(start, size, levelsize)) {
                if (attrs == PTE_TYPE_FAULT) {
                        if (pte_type(pte) == PTE_TYPE_TABLE && level < 3)
--

2.53.0

#3 lore raw
Quoted 32 lines · Ilias Apalodimas
Quoted 29 lines · Ilias Apalodimas
Quoted 6 lines · Casey Connolly

If a caller tries to unmap a single page which is already part of an unmapped block, set_one_region() will panic since there isn't a valid block or table to contain the page. Detect this case explicitly and return since no action needs to be taken.

Quoted 6 lines

---
Depends on my previous series which fixes unmapping regions

https://lore.kernel.org/u-boot/[email protected]/
---

Quoted 12 lines
 arch/arm/cpu/armv8/cache_v8.c | 5 +++++
 1 file changed, 5 insertions(+)
arch/arm/cpu/armv8/cache_v8.c +0 -0
index 6c85022556ad..472887b5d99b 100644
@@ -1003,8 +1003,13 @@ static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
        int levelshift = level2shift(level);
        u64 levelsize = 1ULL << levelshift;
        u64 *pte = find_pte(start, level);
 
Quoted 8 lines
Quoted 4 lines

arch/arm/cpu/armv8/cache_v8.c static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level) · line 1010

Quoted 4 lines
arch/arm/cpu/armv8/cache_v8.c +4 -0
+       /* Trying to unmap a region which is part of an already unmapped block, nothing to do */
+       if (pte_type(pte) == PTE_TYPE_FAULT && attrs == PTE_TYPE_FAULT && size < levelsize) {
+               return size;
+       }

On a second thought, you should check the size of the region as well. What if the user starts from an unmapped region with size X but requests to unmap a size Y with Y > X?

Just to make the response a bit clearer.
Add these on the qemu board_late_init() mmu_change_region_attr_nobreak(0x40200000, 0x200000, PTE_TYPE_FAULT); mmu_change_region_attr_nobreak(0x40200000, 0x2000, PTE_TYPE_FAULT); mmu_change_region_attr_nobreak(0x40300000, 0x101000, PTE_TYPE_FAULT);

meminfo says:
[0x0000013ffea000] | Table | |

         |
    [0x00000040000000 - 0x00000040200000]   |  Block | RWX        |
Normal        | Inner-shareable
    [0x00000040200000 - 0x00000040400000]   |  Fault | RWX        |
Normal        | Inner-shareable
    [0x00000040400000 - 0x00000080000000]   |  Block | RWX        |

Normal | Inner-shareable

Cheers
/Ilias

Quoted 11 lines

Thanks
/Ilias

Quoted 8 lines
Quoted 8 lines
arch/arm/cpu/armv8/cache_v8.c +1 -1
+
        /* Can we can just modify the current level block/page? */
        if (is_aligned(start, size, levelsize)) {
                if (attrs == PTE_TYPE_FAULT) {
                        if (pte_type(pte) == PTE_TYPE_TABLE && level < 3)
--

2.53.0