Skip to contentSkip to conversation

arm64: mm: use fully constructed struct pages from EFI page tables

PATCH New Ard Biesheuvel 6 participants 13 messages Jul 22, 2016 – Jul 25, 2016 arm64

lore
Share & tools
lore
#0 lore raw

cover arm64: mm: use fully constructed struct pages from EFI page tables

As reported by Sudeep, the EFI memory attributes table handling may crash on an uninitialized spinlock in the struct page associated with a UEFI runtime translation table page.

This is caused by a failure to take into account the fact that generic code treats such pages differently depending on whether they are associated with init_mm or not. The EFI page tables are completely separate from both the kernel and the userland mappings, and are completely static during the lifetime of the OS, but still, the most robust solution is to simply construct these pages fully, so that generic code that compares against &init_mm can work safely and correctly.

Patch #1 is a preparatory patch to drop a reference to late_pgtable_alloc from code that no longer needs it.

Patch #2 renamed late_pgtable_alloc to pgd_table_alloc, and updates it to construct the struct page associated with the allocated page.

Ard Biesheuvel (2):
arm64: mm: make create_mapping_late() non-allocating
arm64: mm: run pgtable_page_ctor() on non-swapper translation table
pages

 arch/arm64/mm/mmu.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/2 arm64: mm: make create_mapping_late() non-allocating

The only purpose served by create_mapping_late() is to remap the already mapped .text and .rodata kernel segments with read-only permissions. Since we no longer allow block mappings to be split or merged, create_mapping_late() should not pass an allocation function pointer into __create_pgd_mapping(). So pass NULL instead.

---
 arch/arm64/mm/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/mm/mmu.c +1 -1
index a96a2413fa18..33f36cede02d 100644
@@ -312,7 +312,7 @@ static void create_mapping_late(phys_addr_t phys, unsigned long virt,
 	}
 
 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
-			     late_pgtable_alloc, !debug_pagealloc_enabled());
+			     NULL, !debug_pagealloc_enabled());
 }
 
 static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end)
Signature · 1 line
2.7.4
reviewer
#2 lore raw

PATCH 1/2 arm64: mm: make create_mapping_late() non-allocating

Hi Ard,

Quoted 16 lines · Ard Biesheuvel

The only purpose served by create_mapping_late() is to remap the already mapped .text and .rodata kernel segments with read-only permissions. Since we no longer allow block mappings to be split or merged, create_mapping_late() should not pass an allocation function pointer into __create_pgd_mapping(). So pass NULL instead.

---
 arch/arm64/mm/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/mm/mmu.c +0 -0
index a96a2413fa18..33f36cede02d 100644

arch/arm64/mm/mmu.c static void create_mapping_late(phys_addr_t phys, unsigned long virt, · line 316

Quoted 6 lines
arch/arm64/mm/mmu.c +1 -1
@@ -312,7 +312,7 @@ static void create_mapping_late(phys_addr_t phys, unsigned long virt,
 	}
 
 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
-			     late_pgtable_alloc, !debug_pagealloc_enabled());
+			     NULL, !debug_pagealloc_enabled());
 }

How about we drop the __init marker from create_mapping_noalloc(), and update the callers in mark_rodata_ro() to use that, so that we can drop create_mapping_late() entirely.

Other than the __init marker and name, create_mapping_late() and create_mapping_noalloc() would be identical after this change, and the naming of create_mapping_noalloc() does better describe what we're trying to achieve in mark_rodata_ro().

Thanks,
Mark.

#3 lore raw

PATCH 1/2 arm64: mm: make create_mapping_late() non-allocating

Quoted 33 lines · Mark Rutland

Hi Ard,

Quoted 17 lines · Ard Biesheuvel

The only purpose served by create_mapping_late() is to remap the already mapped .text and .rodata kernel segments with read-only permissions. Since we no longer allow block mappings to be split or merged, create_mapping_late() should not pass an allocation function pointer into __create_pgd_mapping(). So pass NULL instead.

---
arch/arm64/mm/mmu.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/mm/mmu.c +0 -0
index a96a2413fa18..33f36cede02d 100644

arch/arm64/mm/mmu.c static void create_mapping_late(phys_addr_t phys, unsigned long virt, · line 316

Quoted 6 lines
arch/arm64/mm/mmu.c +1 -1
@@ -312,7 +312,7 @@ static void create_mapping_late(phys_addr_t phys, unsigned long virt,
   }
 
   __create_pgd_mapping(init_mm.pgd, phys, virt, size, prot,
-                 late_pgtable_alloc, !debug_pagealloc_enabled());
+                 NULL, !debug_pagealloc_enabled());
 }

How about we drop the __init marker from create_mapping_noalloc(), and update the callers in mark_rodata_ro() to use that, so that we can drop create_mapping_late() entirely.

Quoted 5 lines

Other than the __init marker and name, create_mapping_late() and create_mapping_noalloc() would be identical after this change, and the naming of create_mapping_noalloc() does better describe what we're trying to achieve in mark_rodata_ro().

Not entirely. The _late() variant should map down to pages if the first mapping was mapped down to pages as well. The _noalloc() variant may end attempting to merge regions

reviewer
#4 lore raw

PATCH 1/2 arm64: mm: make create_mapping_late() non-allocating

Quoted 5 lines · Ard Biesheuvel
Quoted 5 lines · Mark Rutland

How about we drop the __init marker from create_mapping_noalloc(), and update the callers in mark_rodata_ro() to use that, so that we can drop create_mapping_late() entirely.

Quoted 8 lines
Quoted 4 lines

Other than the __init marker and name, create_mapping_late() and create_mapping_noalloc() would be identical after this change, and the naming of create_mapping_noalloc() does better describe what we're trying to achieve in mark_rodata_ro().

Not entirely. The _late() variant should map down to pages if the first mapping was mapped down to pages as well. The _noalloc() variant may end attempting to merge regions

Ah, I was comparing against mainline rather than the arm64 for-next/core branch with the recent rework. Sorry for the noise.

Given that, FWIW:

Mark.

#5 lore raw

PATCH 2/2 arm64: mm: run pgtable_page_ctor() on non-swapper translation table pages

In reply to Ard Biesheuvel

The kernel page table creation routines are accessible to other subsystems (e.g., EFI) via the create_pgd_mapping() entry point, which allows mappings to be created that are not covered by init_mm.

Since generic code such as apply_to_page_range() may expect translation table pages that are not associated with init_mm to be covered by fully constructed struct pages, add a call to pgtable_page_ctor() in the alloc function used by create_pgd_mapping. Since it is no longer used by create_mapping_late(), also update the name of this function to better reflect its purpose.

---
 arch/arm64/mm/mmu.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/mm/mmu.c +6 -3
index 33f36cede02d..51a558195bb9 100644
@@ -268,10 +268,11 @@ static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
 	} while (pgd++, addr = next, addr != end);
 }
 
-static phys_addr_t late_pgtable_alloc(void)
+static phys_addr_t pgd_pgtable_alloc(void)
 {
 	void *ptr = (void *)__get_free_page(PGALLOC_GFP);
-	BUG_ON(!ptr);
+	if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
+		BUG();
 
 	/* Ensure the zeroed page is visible to the page table walker */
 	dsb(ishst);
@@ -298,8 +299,10 @@ void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
 			       unsigned long virt, phys_addr_t size,
 			       pgprot_t prot, bool allow_block_mappings)
 {
+	BUG_ON(mm == &init_mm);
+
 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
-			     late_pgtable_alloc, allow_block_mappings);
+			     pgd_pgtable_alloc, allow_block_mappings);
 }
 
 static void create_mapping_late(phys_addr_t phys, unsigned long virt,
Signature · 1 line
2.7.4
#6 lore raw

PATCH 2/2 arm64: mm: run pgtable_page_ctor() on non-swapper translation table pages

Quoted 21 lines · Ard Biesheuvel

The kernel page table creation routines are accessible to other subsystems (e.g., EFI) via the create_pgd_mapping() entry point, which allows mappings to be created that are not covered by init_mm.

Since generic code such as apply_to_page_range() may expect translation table pages that are not associated with init_mm to be covered by fully constructed struct pages, add a call to pgtable_page_ctor() in the alloc function used by create_pgd_mapping. Since it is no longer used by create_mapping_late(), also update the name of this function to better reflect its purpose.

---
 arch/arm64/mm/mmu.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/mm/mmu.c +0 -0
index 33f36cede02d..51a558195bb9 100644

arch/arm64/mm/mmu.c static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, · line 274

Quoted 9 lines
arch/arm64/mm/mmu.c +2 -2
@@ -268,10 +268,11 @@ static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
 	} while (pgd++, addr = next, addr != end);
 }
 
-static phys_addr_t late_pgtable_alloc(void)
+static phys_addr_t pgd_pgtable_alloc(void)
 {
 	void *ptr = (void *)__get_free_page(PGALLOC_GFP);
-	BUG_ON(!ptr);
+	if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))

Should we free the page and return NULL when we encounter an error in pgtable_page_ctor(), like the rest of the callers ?

arch/arm64/mm/mmu.c static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, · line 275

Quoted 1 line
arch/arm64/mm/mmu.c +1 -0
+		BUG();

Otherwise, looks good to me.

Suzuki

#7 lore raw

PATCH 2/2 arm64: mm: run pgtable_page_ctor() on non-swapper translation table pages

Quoted 25 lines · Suzuki K Poulose
Quoted 24 lines · Ard Biesheuvel

The kernel page table creation routines are accessible to other subsystems (e.g., EFI) via the create_pgd_mapping() entry point, which allows mappings to be created that are not covered by init_mm.

Since generic code such as apply_to_page_range() may expect translation table pages that are not associated with init_mm to be covered by fully constructed struct pages, add a call to pgtable_page_ctor() in the alloc function used by create_pgd_mapping. Since it is no longer used by create_mapping_late(), also update the name of this function to better reflect its purpose.

---
 arch/arm64/mm/mmu.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/mm/mmu.c +0 -0
index 33f36cede02d..51a558195bb9 100644
@@ -268,10 +268,11 @@ static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
     } while (pgd++, addr = next, addr != end);
 }
 
Quoted 13 lines

arch/arm64/mm/mmu.c static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, · line 274

Quoted 6 lines
arch/arm64/mm/mmu.c +2 -2
-static phys_addr_t late_pgtable_alloc(void)
+static phys_addr_t pgd_pgtable_alloc(void)
 {
     void *ptr = (void *)__get_free_page(PGALLOC_GFP);
-    BUG_ON(!ptr);
+    if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))

Should we free the page and return NULL when we encounter an error in pgtable_page_ctor(), like the rest of the callers ?

Quoted 1 line
arch/arm64/mm/mmu.c +1 -0
+        BUG();

Just to clarify, of course with the BUG() retained.

Suzuki

maintainer
#8 lore raw

PATCH 2/2 arm64: mm: run pgtable_page_ctor() on non-swapper translation table pages

Quoted 37 lines · Suzuki K. Poulose
Quoted 36 lines · Suzuki K Poulose
Quoted 21 lines · Ard Biesheuvel

The kernel page table creation routines are accessible to other subsystems (e.g., EFI) via the create_pgd_mapping() entry point, which allows mappings to be created that are not covered by init_mm.

Since generic code such as apply_to_page_range() may expect translation table pages that are not associated with init_mm to be covered by fully constructed struct pages, add a call to pgtable_page_ctor() in the alloc function used by create_pgd_mapping. Since it is no longer used by create_mapping_late(), also update the name of this function to better reflect its purpose.

---
arch/arm64/mm/mmu.c | 9 ++++++---

1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/mm/mmu.c +0 -0
index 33f36cede02d..51a558195bb9 100644

arch/arm64/mm/mmu.c static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, · line 274

Quoted 9 lines
arch/arm64/mm/mmu.c +2 -2
@@ -268,10 +268,11 @@ static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
    } while (pgd++, addr = next, addr != end);
 }
 
-static phys_addr_t late_pgtable_alloc(void)
+static phys_addr_t pgd_pgtable_alloc(void)
 {
    void *ptr = (void *)__get_free_page(PGALLOC_GFP);
-    BUG_ON(!ptr);
+    if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))

Should we free the page and return NULL when we encounter an error in pgtable_page_ctor(), like the rest of the callers ?

Quoted 3 lines
Quoted 1 line

arch/arm64/mm/mmu.c static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, · line 275

Quoted 1 line
arch/arm64/mm/mmu.c +1 -0
+        BUG();

Just to clarify, of course with the BUG() retained.

But then, if we return, we no longer trigger BUG(). The callers of this function assume that it always succeeds or does not return.

Signature · 1 line
Catalin
#9 lore raw

PATCH 2/2 arm64: mm: run pgtable_page_ctor() on non-swapper translation table pages

Quoted 38 lines · Catalin Marinas
Quoted 37 lines · Suzuki K. Poulose
Quoted 36 lines · Suzuki K Poulose
Quoted 21 lines · Ard Biesheuvel

The kernel page table creation routines are accessible to other subsystems (e.g., EFI) via the create_pgd_mapping() entry point, which allows mappings to be created that are not covered by init_mm.

Since generic code such as apply_to_page_range() may expect translation table pages that are not associated with init_mm to be covered by fully constructed struct pages, add a call to pgtable_page_ctor() in the alloc function used by create_pgd_mapping. Since it is no longer used by create_mapping_late(), also update the name of this function to better reflect its purpose.

---
arch/arm64/mm/mmu.c | 9 ++++++---

1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/mm/mmu.c +0 -0
index 33f36cede02d..51a558195bb9 100644

arch/arm64/mm/mmu.c static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, · line 274

Quoted 9 lines
arch/arm64/mm/mmu.c +2 -2
@@ -268,10 +268,11 @@ static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
    } while (pgd++, addr = next, addr != end);
 }
 
-static phys_addr_t late_pgtable_alloc(void)
+static phys_addr_t pgd_pgtable_alloc(void)
 {
    void *ptr = (void *)__get_free_page(PGALLOC_GFP);
-    BUG_ON(!ptr);
+    if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))

Should we free the page and return NULL when we encounter an error in pgtable_page_ctor(), like the rest of the callers ?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

arch/arm64/mm/mmu.c static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys, · line 275

Quoted 1 line
arch/arm64/mm/mmu.c +1 -0
+        BUG();

Just to clarify, of course with the BUG() retained.

But then, if we return, we no longer trigger BUG(). The callers of this function assume that it always succeeds or does not return.

Bah, my bad. I somehow confused myself it with WARN behavior and thought that we could still continue after BUG(). Please ignore the noise.

Suzuki

#10 lore raw

cover arm64: mm: use fully constructed struct pages from EFI page tables

In reply to Ard Biesheuvel
Quoted 18 lines · Ard Biesheuvel

As reported by Sudeep, the EFI memory attributes table handling may crash on an uninitialized spinlock in the struct page associated with a UEFI runtime translation table page.

This is caused by a failure to take into account the fact that generic code treats such pages differently depending on whether they are associated with init_mm or not. The EFI page tables are completely separate from both the kernel and the userland mappings, and are completely static during the lifetime of the OS, but still, the most robust solution is to simply construct these pages fully, so that generic code that compares against &init_mm can work safely and correctly.

Patch #1 is a preparatory patch to drop a reference to late_pgtable_alloc from code that no longer needs it.

Patch #2 renamed late_pgtable_alloc to pgd_table_alloc, and updates it to construct the struct page associated with the allocated page.

Quoted 8 lines

Ard Biesheuvel (2):
arm64: mm: make create_mapping_late() non-allocating
arm64: mm: run pgtable_page_ctor() on non-swapper translation table
pages

 arch/arm64/mm/mmu.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
#11 lore raw

cover arm64: mm: use fully constructed struct pages from EFI page tables

In reply to Ard Biesheuvel
Quoted 12 lines · Ard Biesheuvel

As reported by Sudeep, the EFI memory attributes table handling may crash on an uninitialized spinlock in the struct page associated with a UEFI runtime translation table page.

This is caused by a failure to take into account the fact that generic code treats such pages differently depending on whether they are associated with init_mm or not. The EFI page tables are completely separate from both the kernel and the userland mappings, and are completely static during the lifetime of the OS, but still, the most robust solution is to simply construct these pages fully, so that generic code that compares against &init_mm can work safely and correctly.

Quoted 6 lines

Patch #1 is a preparatory patch to drop a reference to late_pgtable_alloc from code that no longer needs it.

Patch #2 renamed late_pgtable_alloc to pgd_table_alloc, and updates it to construct the struct page associated with the allocated page.

These patches fixes the issue I reported. You can add:

Signature · 2 lines
Regards,
Sudeep
maintainer
#12 lore raw

cover arm64: mm: use fully constructed struct pages from EFI page tables

In reply to Ard Biesheuvel
Quoted 15 lines · Ard Biesheuvel

As reported by Sudeep, the EFI memory attributes table handling may crash on an uninitialized spinlock in the struct page associated with a UEFI runtime translation table page.

This is caused by a failure to take into account the fact that generic code treats such pages differently depending on whether they are associated with init_mm or not. The EFI page tables are completely separate from both the kernel and the userland mappings, and are completely static during the lifetime of the OS, but still, the most robust solution is to simply construct these pages fully, so that generic code that compares against &init_mm can work safely and correctly.

Patch #1 is a preparatory patch to drop a reference to late_pgtable_alloc from code that no longer needs it.

Quoted 7 lines

Patch #2 renamed late_pgtable_alloc to pgd_table_alloc, and updates it to construct the struct page associated with the allocated page.

Ard Biesheuvel (2):
arm64: mm: make create_mapping_late() non-allocating
arm64: mm: run pgtable_page_ctor() on non-swapper translation table
pages

Patches applied. I'll send them with the 4.8 pull request this week.

Thanks.

Signature · 1 line
Catalin