[RFC PATCH v2 26/43] arm64: Convert KVM and hugetlb size switches to if/else

Eric Curtin ericcurtin17 at gmail.com
Mon Nov 30 17:51:58 PST 2026


From: Ryan Roberts <ryan.roberts at arm.com>

With boot-time page size selection PMD_SHIFT, PUD_SHIFT, CONT_PTE_SHIFT
and the corresponding _SIZE macros are no longer compile-time constants,
so they cannot be used as case labels in a switch statement.

Convert the switches that use them to if/else chains: the vma_shift
handling in kvm_s2_resolve_vma_size(), and __hugetlb_valid_size(),
num_contig_ptes(), hugetlb_mask_last_page() and arch_make_huge_pte() in
hugetlbpage.c. The decisions made are intended to stay the same as with
the switch statements.

Signed-off-by: Ryan Roberts <ryan.roberts at arm.com>
Signed-off-by: Eric Curtin <eric.curtin at docker.com>
(cherry picked from commit 1cd569027d7d41ba43eedde8d483618ac38e2426)
---
 arch/arm64/kvm/mmu.c        | 29 +++++++++------------
 arch/arm64/mm/hugetlbpage.c | 50 ++++++++++++-------------------------
 2 files changed, 28 insertions(+), 51 deletions(-)

diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 86aa0b6fa8b5..c3a5ee486ae8 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1929,29 +1929,24 @@ static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd,
 		vma_shift = get_vma_page_shift(vma);
 	}
 
-	switch (vma_shift) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SHIFT:
-		if (fault_supports_stage2_huge_mapping(s2fd, PUD_SIZE))
-			break;
-		fallthrough;
+	if (vma_shift == PUD_SHIFT) {
+		if (!fault_supports_stage2_huge_mapping(s2fd, PUD_SIZE))
+			vma_shift = PMD_SHIFT;
+	}
 #endif
-	case CONT_PMD_SHIFT:
+	if (vma_shift == CONT_PMD_SHIFT) {
 		vma_shift = PMD_SHIFT;
-		fallthrough;
-	case PMD_SHIFT:
-		if (fault_supports_stage2_huge_mapping(s2fd, PMD_SIZE))
-			break;
-		fallthrough;
-	case CONT_PTE_SHIFT:
+	if ((vma_shift == PMD_SHIFT &&
+	     !fault_supports_stage2_huge_mapping(s2fd, PMD_SIZE)) ||
+	    vma_shift == CONT_PTE_SHIFT) {
 		vma_shift = PAGE_SHIFT;
 		s2vi->max_map_size = PAGE_SIZE;
-		fallthrough;
-	case PAGE_SHIFT:
-		break;
-	default:
-		WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
 	}
+	if (vma_shift != PUD_SHIFT &&
+	    vma_shift != PMD_SHIFT &&
+	    vma_shift != PAGE_SHIFT)
+		WARN_ONCE(1, "Unknown vma_shift %d", vma_shift);
 
 	if (s2fd->nested) {
 		unsigned long max_map_size;
diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c
index ff1f53e435e1..447e832ad512 100644
--- a/arch/arm64/mm/hugetlbpage.c
+++ b/arch/arm64/mm/hugetlbpage.c
@@ -47,16 +47,12 @@ unsigned int arch_hugetlb_cma_order(void)
 
 static bool __hugetlb_valid_size(unsigned long size)
 {
-	switch (size) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
+	if (size == PUD_SIZE)
 		return pud_sect_supported();
 #endif
-	case CONT_PMD_SIZE:
-	case PMD_SIZE:
-	case CONT_PTE_SIZE:
+	if (size == CONT_PMD_SIZE || size == PMD_SIZE || size == CONT_PTE_SIZE)
 		return true;
-	}
 
 	return false;
 }
@@ -100,16 +96,13 @@ static inline int num_contig_ptes(unsigned long size, size_t *pgsize)
 
 	*pgsize = size;
 
-	switch (size) {
-	case CONT_PMD_SIZE:
+	if (size == CONT_PMD_SIZE) {
 		*pgsize = PMD_SIZE;
 		contig_ptes = CONT_PMDS;
-		break;
-	case CONT_PTE_SIZE:
+	} else if (size == CONT_PTE_SIZE) {
 		*pgsize = PAGE_SIZE;
 		contig_ptes = CONT_PTES;
-		break;
-	default:
+	} else {
 		WARN_ON(!__hugetlb_valid_size(size));
 	}
 
@@ -321,22 +314,16 @@ unsigned long hugetlb_mask_last_page(struct hstate *h)
 {
 	unsigned long hp_size = huge_page_size(h);
 
-	switch (hp_size) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
-		if (pud_sect_supported())
-			return PGDIR_SIZE - PUD_SIZE;
-		break;
+	if (hp_size == PUD_SIZE && pud_sect_supported())
+		return PGDIR_SIZE - PUD_SIZE;
 #endif
-	case CONT_PMD_SIZE:
+	if (hp_size == CONT_PMD_SIZE)
 		return PUD_SIZE - CONT_PMD_SIZE;
-	case PMD_SIZE:
+	if (hp_size == PMD_SIZE)
 		return PUD_SIZE - PMD_SIZE;
-	case CONT_PTE_SIZE:
+	if (hp_size == CONT_PTE_SIZE)
 		return PMD_SIZE - CONT_PTE_SIZE;
-	default:
-		break;
-	}
 
 	return 0UL;
 }
@@ -345,22 +332,17 @@ pte_t arch_make_huge_pte(pte_t entry, unsigned int shift, vm_flags_t flags)
 {
 	size_t pagesize = 1UL << shift;
 
-	switch (pagesize) {
 #ifndef __PAGETABLE_PMD_FOLDED
-	case PUD_SIZE:
-		if (pud_sect_supported())
-			return pud_pte(pud_mkhuge(pte_pud(entry)));
-		break;
+	if (pagesize == PUD_SIZE && pud_sect_supported())
+		return pud_pte(pud_mkhuge(pte_pud(entry)));
 #endif
-	case CONT_PMD_SIZE:
+	if (pagesize == CONT_PMD_SIZE)
 		return pmd_pte(pmd_mkhuge(pmd_mkcont(pte_pmd(entry))));
-	case PMD_SIZE:
+	if (pagesize == PMD_SIZE)
 		return pmd_pte(pmd_mkhuge(pte_pmd(entry)));
-	case CONT_PTE_SIZE:
+	if (pagesize == CONT_PTE_SIZE)
 		return pte_mkcont(entry);
-	default:
-		break;
-	}
+
 	pr_warn("%s: unrecognized huge page size 0x%lx\n",
 		__func__, pagesize);
 	return entry;



More information about the linux-arm-kernel mailing list