[kvm-unit-tests PATCH v1 14/18] arm/arm64: Use pgd_alloc() to allocate mmu_idmap

Shaoqin Huang
2023-11-30T09:08:00Z

Until commit 031755dbfefb ("arm: enable vmalloc"), the idmap was allocated
using pgd_alloc(). After that commit, all the page table allocator
functions were switched to using the page allocator, but pgd_alloc() was
left unchanged and became unused, with the idmap now being allocated with
alloc_page().

For arm64, the pgd table size varies based on the page size, which is
configured by the user. For arm, it will always contain 4 entries (it
translates bits 31:30 of the input address). To keep things simple and
consistent with the other functions and across both architectures, modify
pgd_alloc() to use alloc_page() instead of memalign like the rest of the
page table allocator functions and use it to allocate the idmap.

Note that when the idmap is created, alloc_ops->memalign is
memalign_pages(), which allocates memory with page granularity. Which means
that the existing code also allocated a full page, so the total memory used
is not increased by using alloc_page().

Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
Signed-off-by: Alexandru Elisei <alexandru.elisei@arm.com>
---
lib/arm/asm/pgtable.h | 4 ++--
lib/arm/mmu.c | 4 ++--
lib/arm64/asm/pgtable.h | 4 ++--
3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/lib/arm/asm/pgtable.h b/lib/arm/asm/pgtable.h
index d7c73906..a35f4296 100644
--- a/lib/arm/asm/pgtable.h
+++ b/lib/arm/asm/pgtable.h
@@ -43,8 +43,8 @@
#define pgd_free(pgd) free(pgd)
static inline pgd_t *pgd_alloc(void)
{
- pgd_t *pgd = memalign(L1_CACHE_BYTES, PTRS_PER_PGD * sizeof(pgd_t));
- memset(pgd, 0, PTRS_PER_PGD * sizeof(pgd_t));
+ assert(PTRS_PER_PGD * sizeof(pgd_t) <= PAGE_SIZE);
+ pgd_t *pgd = alloc_page();
return pgd;
}

diff --git a/lib/arm/mmu.c b/lib/arm/mmu.c
index 2f4ec815..70c5333c 100644
--- a/lib/arm/mmu.c
+++ b/lib/arm/mmu.c
@@ -217,7 +217,7 @@ void *setup_mmu(phys_addr_t phys_end, void *unused)
#endif

if (!mmu_idmap)
- mmu_idmap = alloc_page();
+ mmu_idmap = pgd_alloc();

for (r = mem_regions; r->end; ++r) {
if (r->flags & MR_F_IO) {
@@ -253,7 +253,7 @@ void __iomem *__ioremap(phys_addr_t phys_addr, size_t size)
pgtable = current_thread_info()->pgtable;
} else {
if (!mmu_idmap)
- mmu_idmap = alloc_page();
+ mmu_idmap = pgd_alloc();
pgtable = mmu_idmap;
}

diff --git a/lib/arm64/asm/pgtable.h b/lib/arm64/asm/pgtable.h
index bfb8a993..06357920 100644
--- a/lib/arm64/asm/pgtable.h
+++ b/lib/arm64/asm/pgtable.h
@@ -49,8 +49,8 @@
#define pgd_free(pgd) free(pgd)
static inline pgd_t *pgd_alloc(void)
{
- pgd_t *pgd = memalign(PAGE_SIZE, PTRS_PER_PGD * sizeof(pgd_t));
- memset(pgd, 0, PTRS_PER_PGD * sizeof(pgd_t));
+ assert(PTRS_PER_PGD * sizeof(pgd_t) <= PAGE_SIZE);
+ pgd_t *pgd = alloc_page();
return pgd;
}

--
2.40.1