Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

arm64: mm: Represent physical memory with phys_addr_t and resource_size_t

This is a type-correctness cleanup to MMU/boot code that replaces
several instances of void * and u64 with phys_addr_t (to represent
addresses) and resource_size_t (to represent sizes) to emphasize that
the code in question concerns physical memory specifically.

The rationale for this change is to improve clarity and readability in
a few modules that handle both types (physical and virtual) of address
and differentiation is essential.

I have left u64 in cases where the address may be either physical or
virtual, where the address is exclusively virtual but used in heavy
pointer arithmetic, and in cases I may have overlooked. I do not
necessarily consider u64 the ideal type in those situations, but it
avoids breaking existing semantics in this cleanup.

This patch provably has no effect at runtime: I have verified that
.text of vmlinux is identical after this change.

Signed-off-by: Sam Edwards <CFSworks@gmail.com>
Signed-off-by: Will Deacon <will@kernel.org>

authored by

Sam Edwards and committed by
Will Deacon
b868fff5 c56aa9a6

+42 -36
+13 -13
arch/arm64/kernel/pi/map_kernel.c
··· 18 18 19 19 extern const u8 __eh_frame_start[], __eh_frame_end[]; 20 20 21 - extern void idmap_cpu_replace_ttbr1(void *pgdir); 21 + extern void idmap_cpu_replace_ttbr1(phys_addr_t pgdir); 22 22 23 - static void __init map_segment(pgd_t *pg_dir, u64 *pgd, u64 va_offset, 23 + static void __init map_segment(pgd_t *pg_dir, phys_addr_t *pgd, u64 va_offset, 24 24 void *start, void *end, pgprot_t prot, 25 25 bool may_use_cont, int root_level) 26 26 { ··· 40 40 { 41 41 bool enable_scs = IS_ENABLED(CONFIG_UNWIND_PATCH_PAC_INTO_SCS); 42 42 bool twopass = IS_ENABLED(CONFIG_RELOCATABLE); 43 - u64 pgdp = (u64)init_pg_dir + PAGE_SIZE; 43 + phys_addr_t pgdp = (phys_addr_t)init_pg_dir + PAGE_SIZE; 44 44 pgprot_t text_prot = PAGE_KERNEL_ROX; 45 45 pgprot_t data_prot = PAGE_KERNEL; 46 46 pgprot_t prot; ··· 90 90 true, root_level); 91 91 dsb(ishst); 92 92 93 - idmap_cpu_replace_ttbr1(init_pg_dir); 93 + idmap_cpu_replace_ttbr1((phys_addr_t)init_pg_dir); 94 94 95 95 if (twopass) { 96 96 if (IS_ENABLED(CONFIG_RELOCATABLE)) ··· 129 129 /* Copy the root page table to its final location */ 130 130 memcpy((void *)swapper_pg_dir + va_offset, init_pg_dir, PAGE_SIZE); 131 131 dsb(ishst); 132 - idmap_cpu_replace_ttbr1(swapper_pg_dir); 132 + idmap_cpu_replace_ttbr1((phys_addr_t)swapper_pg_dir); 133 133 } 134 134 135 - static void noinline __section(".idmap.text") set_ttbr0_for_lpa2(u64 ttbr) 135 + static void noinline __section(".idmap.text") set_ttbr0_for_lpa2(phys_addr_t ttbr) 136 136 { 137 137 u64 sctlr = read_sysreg(sctlr_el1); 138 138 u64 tcr = read_sysreg(tcr_el1) | TCR_DS; ··· 172 172 */ 173 173 create_init_idmap(init_pg_dir, mask); 174 174 dsb(ishst); 175 - set_ttbr0_for_lpa2((u64)init_pg_dir); 175 + set_ttbr0_for_lpa2((phys_addr_t)init_pg_dir); 176 176 177 177 /* 178 178 * Recreate the initial ID map with the same granularity as before. ··· 185 185 dsb(ishst); 186 186 187 187 /* switch back to the updated initial ID map */ 188 - set_ttbr0_for_lpa2((u64)init_idmap_pg_dir); 188 + set_ttbr0_for_lpa2((phys_addr_t)init_idmap_pg_dir); 189 189 190 190 /* wipe the temporary ID map from memory */ 191 191 memset(init_pg_dir, 0, (char *)init_pg_end - (char *)init_pg_dir); 192 192 } 193 193 194 - static void *__init map_fdt(u64 fdt) 194 + static void *__init map_fdt(phys_addr_t fdt) 195 195 { 196 196 static u8 ptes[INIT_IDMAP_FDT_SIZE] __initdata __aligned(PAGE_SIZE); 197 - u64 efdt = fdt + MAX_FDT_SIZE; 198 - u64 ptep = (u64)ptes; 197 + phys_addr_t efdt = fdt + MAX_FDT_SIZE; 198 + phys_addr_t ptep = (phys_addr_t)ptes; /* We're idmapped when called */ 199 199 200 200 /* 201 201 * Map up to MAX_FDT_SIZE bytes, but avoid overlap with ··· 232 232 return true; 233 233 } 234 234 235 - asmlinkage void __init early_map_kernel(u64 boot_status, void *fdt) 235 + asmlinkage void __init early_map_kernel(u64 boot_status, phys_addr_t fdt) 236 236 { 237 237 static char const chosen_str[] __initconst = "/chosen"; 238 238 u64 va_base, pa_base = (u64)&_text; ··· 240 240 int root_level = 4 - CONFIG_PGTABLE_LEVELS; 241 241 int va_bits = VA_BITS; 242 242 int chosen; 243 - void *fdt_mapped = map_fdt((u64)fdt); 243 + void *fdt_mapped = map_fdt(fdt); 244 244 245 245 /* Clear BSS and the initial page tables */ 246 246 memset(__bss_start, 0, (char *)init_pg_end - (char *)__bss_start);
+12 -8
arch/arm64/kernel/pi/map_range.c
··· 26 26 * @va_offset: Offset between a physical page and its current mapping 27 27 * in the VA space 28 28 */ 29 - void __init map_range(u64 *pte, u64 start, u64 end, u64 pa, pgprot_t prot, 30 - int level, pte_t *tbl, bool may_use_cont, u64 va_offset) 29 + void __init map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa, 30 + pgprot_t prot, int level, pte_t *tbl, bool may_use_cont, 31 + u64 va_offset) 31 32 { 32 33 u64 cmask = (level == 3) ? CONT_PTE_SIZE - 1 : U64_MAX; 33 34 ptdesc_t protval = pgprot_val(prot) & ~PTE_TYPE_MASK; ··· 88 87 } 89 88 } 90 89 91 - asmlinkage u64 __init create_init_idmap(pgd_t *pg_dir, ptdesc_t clrmask) 90 + asmlinkage phys_addr_t __init create_init_idmap(pgd_t *pg_dir, ptdesc_t clrmask) 92 91 { 93 - u64 ptep = (u64)pg_dir + PAGE_SIZE; 92 + phys_addr_t ptep = (phys_addr_t)pg_dir + PAGE_SIZE; /* MMU is off */ 94 93 pgprot_t text_prot = PAGE_KERNEL_ROX; 95 94 pgprot_t data_prot = PAGE_KERNEL; 96 95 97 96 pgprot_val(text_prot) &= ~clrmask; 98 97 pgprot_val(data_prot) &= ~clrmask; 99 98 100 - map_range(&ptep, (u64)_stext, (u64)__initdata_begin, (u64)_stext, 101 - text_prot, IDMAP_ROOT_LEVEL, (pte_t *)pg_dir, false, 0); 102 - map_range(&ptep, (u64)__initdata_begin, (u64)_end, (u64)__initdata_begin, 103 - data_prot, IDMAP_ROOT_LEVEL, (pte_t *)pg_dir, false, 0); 99 + /* MMU is off; pointer casts to phys_addr_t are safe */ 100 + map_range(&ptep, (u64)_stext, (u64)__initdata_begin, 101 + (phys_addr_t)_stext, text_prot, IDMAP_ROOT_LEVEL, 102 + (pte_t *)pg_dir, false, 0); 103 + map_range(&ptep, (u64)__initdata_begin, (u64)_end, 104 + (phys_addr_t)__initdata_begin, data_prot, IDMAP_ROOT_LEVEL, 105 + (pte_t *)pg_dir, false, 0); 104 106 105 107 return ptep; 106 108 }
+5 -4
arch/arm64/kernel/pi/pi.h
··· 29 29 void relocate_kernel(u64 offset); 30 30 int scs_patch(const u8 eh_frame[], int size); 31 31 32 - void map_range(u64 *pgd, u64 start, u64 end, u64 pa, pgprot_t prot, 33 - int level, pte_t *tbl, bool may_use_cont, u64 va_offset); 32 + void map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa, 33 + pgprot_t prot, int level, pte_t *tbl, bool may_use_cont, 34 + u64 va_offset); 34 35 35 - asmlinkage void early_map_kernel(u64 boot_status, void *fdt); 36 + asmlinkage void early_map_kernel(u64 boot_status, phys_addr_t fdt); 36 37 37 - asmlinkage u64 create_init_idmap(pgd_t *pgd, ptdesc_t clrmask); 38 + asmlinkage phys_addr_t create_init_idmap(pgd_t *pgd, ptdesc_t clrmask);
+3 -3
arch/arm64/mm/init.c
··· 243 243 */ 244 244 if (memory_limit != PHYS_ADDR_MAX) { 245 245 memblock_mem_limit_remove_map(memory_limit); 246 - memblock_add(__pa_symbol(_text), (u64)(_end - _text)); 246 + memblock_add(__pa_symbol(_text), (resource_size_t)(_end - _text)); 247 247 } 248 248 249 249 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { ··· 252 252 * initrd to become inaccessible via the linear mapping. 253 253 * Otherwise, this is a no-op 254 254 */ 255 - u64 base = phys_initrd_start & PAGE_MASK; 256 - u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base; 255 + phys_addr_t base = phys_initrd_start & PAGE_MASK; 256 + resource_size_t size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base; 257 257 258 258 /* 259 259 * We can only add back the initrd memory if we don't end up
+9 -8
arch/arm64/mm/mmu.c
··· 794 794 declare_vma(&vmlinux_seg[4], _data, _end, 0); 795 795 } 796 796 797 - void __pi_map_range(u64 *pgd, u64 start, u64 end, u64 pa, pgprot_t prot, 798 - int level, pte_t *tbl, bool may_use_cont, u64 va_offset); 797 + void __pi_map_range(phys_addr_t *pte, u64 start, u64 end, phys_addr_t pa, 798 + pgprot_t prot, int level, pte_t *tbl, bool may_use_cont, 799 + u64 va_offset); 799 800 800 801 static u8 idmap_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init, 801 802 kpti_ptes[IDMAP_LEVELS - 1][PAGE_SIZE] __aligned(PAGE_SIZE) __ro_after_init; 802 803 803 804 static void __init create_idmap(void) 804 805 { 805 - u64 start = __pa_symbol(__idmap_text_start); 806 - u64 end = __pa_symbol(__idmap_text_end); 807 - u64 ptep = __pa_symbol(idmap_ptes); 806 + phys_addr_t start = __pa_symbol(__idmap_text_start); 807 + phys_addr_t end = __pa_symbol(__idmap_text_end); 808 + phys_addr_t ptep = __pa_symbol(idmap_ptes); 808 809 809 810 __pi_map_range(&ptep, start, end, start, PAGE_KERNEL_ROX, 810 811 IDMAP_ROOT_LEVEL, (pte_t *)idmap_pg_dir, false, ··· 813 812 814 813 if (IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0) && !arm64_use_ng_mappings) { 815 814 extern u32 __idmap_kpti_flag; 816 - u64 pa = __pa_symbol(&__idmap_kpti_flag); 815 + phys_addr_t pa = __pa_symbol(&__idmap_kpti_flag); 817 816 818 817 /* 819 818 * The KPTI G-to-nG conversion code needs a read-write mapping ··· 1332 1331 struct range arch_get_mappable_range(void) 1333 1332 { 1334 1333 struct range mhp_range; 1335 - u64 start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1336 - u64 end_linear_pa = __pa(PAGE_END - 1); 1334 + phys_addr_t start_linear_pa = __pa(_PAGE_OFFSET(vabits_actual)); 1335 + phys_addr_t end_linear_pa = __pa(PAGE_END - 1); 1337 1336 1338 1337 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 1339 1338 /*