[RFC PATCH v2 36/43] arm64: Convert remaining page-size switches to if/else
Eric Curtin
ericcurtin17 at gmail.com
Mon Nov 30 17:52:08 PST 2026
From: Eric Curtin <eric.curtin at docker.com>
With boot-time page size selection PMD_SIZE, PUD_SIZE, PAGE_SIZE and the
CONT_*_SIZE values are not constants, so they cannot be case labels in a
switch statement, and PAGE_SHIFT cannot be used in a preprocessor
conditional.
Convert the remaining switches on those sizes to if/else chains in
__flush_hugetlb_tlb_range(), __set_ptes_anysz(),
__ptep_get_and_clear_anysz() and __ptep_set_access_flags_anysz(). In the
nVHE hypervisor, replace the "#if PAGE_SHIFT < 16" selection of the
fixblock slot with a HAS_FIXBLOCK C expression, so that
create_fixblock() and hyp_fixblock_map()/unmap() choose at run time
between the fixblock and the fixmap. Align __bss_pgtbl to PAGE_SIZE_MAX
so that statically allocated page tables are aligned for any selectable
page size.
Signed-off-by: Eric Curtin <eric.curtin at docker.com>
(cherry picked from commit e39ee24baa88970efb2d8cc4f088715f28a6001f)
---
arch/arm64/include/asm/hugetlb.h | 15 ++++++---------
arch/arm64/include/asm/linkage.h | 2 +-
arch/arm64/include/asm/pgtable.h | 24 ++++++++----------------
arch/arm64/kvm/hyp/nvhe/mm.c | 30 ++++++++++++++----------------
arch/arm64/mm/fault.c | 12 ++++--------
5 files changed, 33 insertions(+), 50 deletions(-)
diff --git a/arch/arm64/include/asm/hugetlb.h b/arch/arm64/include/asm/hugetlb.h
index d038ff14d16c..b8487e8c9cf8 100644
--- a/arch/arm64/include/asm/hugetlb.h
+++ b/arch/arm64/include/asm/hugetlb.h
@@ -73,20 +73,17 @@ static inline void __flush_hugetlb_tlb_range(struct vm_area_struct *vma,
unsigned long stride,
tlbf_t flags)
{
- switch (stride) {
#ifndef __PAGETABLE_PMD_FOLDED
- case PUD_SIZE:
+ if (stride == PUD_SIZE) {
__flush_tlb_range(vma, start, end, PUD_SIZE, 1, flags);
- break;
+ return;
+ }
#endif
- case CONT_PMD_SIZE:
- case PMD_SIZE:
+ if (stride == CONT_PMD_SIZE || stride == PMD_SIZE) {
__flush_tlb_range(vma, start, end, PMD_SIZE, 2, flags);
- break;
- case CONT_PTE_SIZE:
+ } else if (stride == CONT_PTE_SIZE) {
__flush_tlb_range(vma, start, end, PAGE_SIZE, 3, flags);
- break;
- default:
+ } else {
__flush_tlb_range(vma, start, end, PAGE_SIZE, TLBI_TTL_UNKNOWN, flags);
}
}
diff --git a/arch/arm64/include/asm/linkage.h b/arch/arm64/include/asm/linkage.h
index d1f7a16729d2..2eb3c0f933e3 100644
--- a/arch/arm64/include/asm/linkage.h
+++ b/arch/arm64/include/asm/linkage.h
@@ -45,6 +45,6 @@
#define _THIS_IP_ ({ unsigned long __ip; asm volatile("adr %0, ." : "=r" (__ip)); __ip; })
-#define __bss_pgtbl __section(".bss..pgtbl") __aligned(PAGE_SIZE)
+#define __bss_pgtbl __section(".bss..pgtbl") __aligned(PAGE_SIZE_MAX)
#endif
diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h
index c625fe7f8452..7cdff90a3b7e 100644
--- a/arch/arm64/include/asm/pgtable.h
+++ b/arch/arm64/include/asm/pgtable.h
@@ -635,21 +635,17 @@ static inline void __set_ptes_anysz(struct mm_struct *mm, unsigned long addr,
{
unsigned long stride = pgsize >> PAGE_SHIFT;
- switch (pgsize) {
- case PAGE_SIZE:
+ if (pgsize == PAGE_SIZE) {
page_table_check_ptes_set(mm, addr, ptep, pte, nr);
- break;
- case PMD_SIZE:
+ } else if (pgsize == PMD_SIZE) {
page_table_check_pmds_set(mm, addr, (pmd_t *)ptep,
pte_pmd(pte), nr);
- break;
#ifndef __PAGETABLE_PMD_FOLDED
- case PUD_SIZE:
+ } else if (pgsize == PUD_SIZE) {
page_table_check_puds_set(mm, addr, (pud_t *)ptep,
pte_pud(pte), nr);
- break;
#endif
- default:
+ } else {
VM_WARN_ON(1);
}
@@ -1360,19 +1356,15 @@ static inline pte_t __ptep_get_and_clear_anysz(struct mm_struct *mm,
{
pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0));
- switch (pgsize) {
- case PAGE_SIZE:
+ if (pgsize == PAGE_SIZE) {
page_table_check_pte_clear(mm, address, pte);
- break;
- case PMD_SIZE:
+ } else if (pgsize == PMD_SIZE) {
page_table_check_pmd_clear(mm, address, pte_pmd(pte));
- break;
#ifndef __PAGETABLE_PMD_FOLDED
- case PUD_SIZE:
+ } else if (pgsize == PUD_SIZE) {
page_table_check_pud_clear(mm, address, pte_pud(pte));
- break;
#endif
- default:
+ } else {
VM_WARN_ON(1);
}
diff --git a/arch/arm64/kvm/hyp/nvhe/mm.c b/arch/arm64/kvm/hyp/nvhe/mm.c
index 83b74c5223c3..ede688d8a5f1 100644
--- a/arch/arm64/kvm/hyp/nvhe/mm.c
+++ b/arch/arm64/kvm/hyp/nvhe/mm.c
@@ -391,15 +391,12 @@ static int create_fixmap_slot(u64 addr, u64 cpu)
return kvm_pgtable_walk(&pkvm_pgtable, addr, PAGE_SIZE, &walker);
}
-#if PAGE_SHIFT < 16
-#define HAS_FIXBLOCK
+#define HAS_FIXBLOCK (PAGE_SHIFT < 16)
static struct hyp_fixmap_slot hyp_fixblock_slot;
static DEFINE_HYP_SPINLOCK(hyp_fixblock_lock);
-#endif
static int create_fixblock(void)
{
-#ifdef HAS_FIXBLOCK
struct kvm_pgtable_walker walker = {
.cb = __create_fixmap_slot_cb,
.flags = KVM_PGTABLE_WALK_LEAF,
@@ -409,6 +406,9 @@ static int create_fixblock(void)
phys_addr_t phys;
int ret, i;
+ if (!HAS_FIXBLOCK)
+ return 0;
+
/* Find a RAM phys address, PMD aligned */
for (i = 0; i < hyp_memblock_nr; i++) {
phys = ALIGN(hyp_memory[i].base, PMD_SIZE);
@@ -435,31 +435,29 @@ static int create_fixblock(void)
hyp_spin_unlock(&pkvm_pgd_lock);
return ret;
-#else
- return 0;
-#endif
}
void *hyp_fixblock_map(phys_addr_t phys, size_t *size)
{
-#ifdef HAS_FIXBLOCK
+ if (!HAS_FIXBLOCK) {
+ *size = PAGE_SIZE;
+ return hyp_fixmap_map(phys);
+ }
+
*size = PMD_SIZE;
hyp_spin_lock(&hyp_fixblock_lock);
return fixmap_map_slot(&hyp_fixblock_slot, phys) + offset_in_page(phys);
-#else
- *size = PAGE_SIZE;
- return hyp_fixmap_map(phys);
-#endif
}
void hyp_fixblock_unmap(void)
{
-#ifdef HAS_FIXBLOCK
+ if (!HAS_FIXBLOCK) {
+ hyp_fixmap_unmap();
+ return;
+ }
+
fixmap_clear_slot(&hyp_fixblock_slot);
hyp_spin_unlock(&hyp_fixblock_lock);
-#else
- hyp_fixmap_unmap();
-#endif
}
int hyp_create_fixmap(void)
diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c
index b22736853fcd..0880f6456b49 100644
--- a/arch/arm64/mm/fault.c
+++ b/arch/arm64/mm/fault.c
@@ -253,19 +253,15 @@ int __ptep_set_access_flags_anysz(struct vm_area_struct *vma,
* flush_tlb_fix_spurious_fault().
*/
if (dirty) {
- switch (pgsize) {
- case PAGE_SIZE:
+ if (pgsize == PAGE_SIZE) {
level = 3;
- break;
- case PMD_SIZE:
+ } else if (pgsize == PMD_SIZE) {
level = 2;
- break;
#ifndef __PAGETABLE_PMD_FOLDED
- case PUD_SIZE:
+ } else if (pgsize == PUD_SIZE) {
level = 1;
- break;
#endif
- default:
+ } else {
level = TLBI_TTL_UNKNOWN;
WARN_ON(1);
}
More information about the linux-arm-kernel
mailing list