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

arm64: decouple check whether pfn is in linear map from pfn_valid()

The intended semantics of pfn_valid() is to verify whether there is a
struct page for the pfn in question and nothing else.

Yet, on arm64 it is used to distinguish memory areas that are mapped in
the linear map vs those that require ioremap() to access them.

Introduce a dedicated pfn_is_map_memory() wrapper for
memblock_is_map_memory() to perform such check and use it where
appropriate.

Using a wrapper allows to avoid cyclic include dependencies.

While here also update style of pfn_valid() so that both pfn_valid() and
pfn_is_map_memory() declarations will be consistent.

Link: https://lkml.kernel.org/r/20210511100550.28178-4-rppt@kernel.org
Signed-off-by: Mike Rapoport <rppt@linux.ibm.com>
Acked-by: David Hildenbrand <david@redhat.com>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Anshuman Khandual <anshuman.khandual@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Mike Rapoport and committed by
Linus Torvalds
873ba463 9092d4f7

+19 -6
+1 -1
arch/arm64/include/asm/memory.h
··· 369 369 370 370 #define virt_addr_valid(addr) ({ \ 371 371 __typeof__(addr) __addr = __tag_reset(addr); \ 372 - __is_lm_address(__addr) && pfn_valid(virt_to_pfn(__addr)); \ 372 + __is_lm_address(__addr) && pfn_is_map_memory(virt_to_pfn(__addr)); \ 373 373 }) 374 374 375 375 void dump_mem_limit(void);
+2 -1
arch/arm64/include/asm/page.h
··· 37 37 38 38 typedef struct page *pgtable_t; 39 39 40 - extern int pfn_valid(unsigned long); 40 + int pfn_valid(unsigned long pfn); 41 + int pfn_is_map_memory(unsigned long pfn); 41 42 42 43 #include <asm/memory.h> 43 44
+1 -1
arch/arm64/kvm/mmu.c
··· 85 85 86 86 static bool kvm_is_device_pfn(unsigned long pfn) 87 87 { 88 - return !pfn_valid(pfn); 88 + return !pfn_is_map_memory(pfn); 89 89 } 90 90 91 91 static void *stage2_memcache_zalloc_page(void *arg)
+12
arch/arm64/mm/init.c
··· 256 256 } 257 257 EXPORT_SYMBOL(pfn_valid); 258 258 259 + int pfn_is_map_memory(unsigned long pfn) 260 + { 261 + phys_addr_t addr = PFN_PHYS(pfn); 262 + 263 + /* avoid false positives for bogus PFNs, see comment in pfn_valid() */ 264 + if (PHYS_PFN(addr) != pfn) 265 + return 0; 266 + 267 + return memblock_is_map_memory(addr); 268 + } 269 + EXPORT_SYMBOL(pfn_is_map_memory); 270 + 259 271 static phys_addr_t memory_limit = PHYS_ADDR_MAX; 260 272 261 273 /*
+2 -2
arch/arm64/mm/ioremap.c
··· 43 43 /* 44 44 * Don't allow RAM to be mapped. 45 45 */ 46 - if (WARN_ON(pfn_valid(__phys_to_pfn(phys_addr)))) 46 + if (WARN_ON(pfn_is_map_memory(__phys_to_pfn(phys_addr)))) 47 47 return NULL; 48 48 49 49 area = get_vm_area_caller(size, VM_IOREMAP, caller); ··· 84 84 void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size) 85 85 { 86 86 /* For normal memory we already have a cacheable mapping. */ 87 - if (pfn_valid(__phys_to_pfn(phys_addr))) 87 + if (pfn_is_map_memory(__phys_to_pfn(phys_addr))) 88 88 return (void __iomem *)__phys_to_virt(phys_addr); 89 89 90 90 return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
+1 -1
arch/arm64/mm/mmu.c
··· 82 82 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 83 83 unsigned long size, pgprot_t vma_prot) 84 84 { 85 - if (!pfn_valid(pfn)) 85 + if (!pfn_is_map_memory(pfn)) 86 86 return pgprot_noncached(vma_prot); 87 87 else if (file->f_flags & O_SYNC) 88 88 return pgprot_writecombine(vma_prot);