Re: [PATCH v2] arm: flush: check if the folio is reserved for no-mapping addresses

From: Jinjiang Tu
Date: Mon Feb 26 2024 - 02:03:17 EST


Since some abuses of pfn_valid() have been reported, I check all the use of pfn_valid(), and find some suspicious cases.

phys_mem_access_prot() defined in arch/arm/mm/mmu.c returns pgprot_noncached() when pfn_valid() returns false.
I think it’s purpose is to return pgprot_noncached() when the pfn is not in RAM, and the use of pfn_valid() is incorrect.
Notably, phys_mem_access_prot() defined in arm64 uses pfn_is_map_memory() instead of pfn_valid() since commit
873ba463914c (arm64: decouple check whether pfn is in linear map from pfn_valid()).

Similarly, virt_addr_valid() defined in arm64 uses pfn_is_map_memory() instead of pfn_valid() since commit
873ba463914c (arm64: decouple check whether pfn is in linear map from pfn_valid()), But virt_addr_valid() still
uses pfn_valid(). Besides, the implementation of x86 also uses pfn_valid().

update_mmu_cache_range() defined in arch/arm/mm/fault-armv.c checks pfn_valid() and then calls __flush_dcache_folio().
This case is similar to the case reported by Yongqiang Liu, the pfn may not be a RAM pfn, and the system will crash in
__flush_dcache_folio() due to the kernel linear mapping is not established. virt_addr_valid() is used to check whether a
vrtual address is valid linear mapping. Are these uses of pfn_valid() incorrect?

pfn_modify_allowed() defined in arch/x86/mm/mmap.c checks pfn_valid(), and the comment says it is intended to check
whether the pfn is in real memory. So the use of pfn_valid() should be incorrent. This case is only involved when the cpu
is affected by X86_BUG_L1TF.

try_ram_remap() defined in kernel/iomem.c returns the linear address when three checks are passed. One of the checks is
pfn_valid(). The only caller memremap() guarantees the pfn passed to try_ram_remap() is in RAM, but the pfn may be in
NOMAP memory regions and is not mapped in linear mapping. commit 260364d112bc (arm[64]/memremap: don't abuse
pfn_valid() to ensure presence of linear map) solves it by checking in arch_memremap_can_ram_remap(), However, if other
architectures involve this issue?

Do these suspicious case abuse pfn_valid() really? Thanks