Skip to content

Commit 093bbe2

Browse files
Anshuman Khandualwilldeacon
authored andcommitted
arm64/mm: Reorganize pfn_valid()
There are multiple instances of pfn_to_section_nr() and __pfn_to_section() when CONFIG_SPARSEMEM is enabled. This can be optimized if memory section is fetched earlier. This replaces the open coded PFN and ADDR conversion with PFN_PHYS() and PHYS_PFN() helpers. While there, also add a comment. This does not cause any functional change. Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will@kernel.org> Cc: Ard Biesheuvel <ardb@kernel.org> Cc: linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org Reviewed-by: David Hildenbrand <david@redhat.com> Signed-off-by: Anshuman Khandual <anshuman.khandual@arm.com> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Link: https://lore.kernel.org/r/1614921898-4099-3-git-send-email-anshuman.khandual@arm.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent eeb0753 commit 093bbe2

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

‎arch/arm64/mm/init.c‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,26 @@ static void __init zone_sizes_init(unsigned long min, unsigned long max)
219219

220220
int pfn_valid(unsigned long pfn)
221221
{
222-
phys_addr_t addr = pfn << PAGE_SHIFT;
222+
phys_addr_t addr = PFN_PHYS(pfn);
223223

224-
if ((addr >> PAGE_SHIFT) != pfn)
224+
/*
225+
* Ensure the upper PAGE_SHIFT bits are clear in the
226+
* pfn. Else it might lead to false positives when
227+
* some of the upper bits are set, but the lower bits
228+
* match a valid pfn.
229+
*/
230+
if (PHYS_PFN(addr) != pfn)
225231
return 0;
226232

227233
#ifdef CONFIG_SPARSEMEM
234+
{
235+
struct mem_section *ms;
236+
228237
if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
229238
return 0;
230239

231-
if (!valid_section(__pfn_to_section(pfn)))
240+
ms = __pfn_to_section(pfn);
241+
if (!valid_section(ms))
232242
return 0;
233243

234244
/*
@@ -240,8 +250,9 @@ int pfn_valid(unsigned long pfn)
240250
* memory sections covering all of hotplug memory including
241251
* both normal and ZONE_DEVICE based.
242252
*/
243-
if (!early_section(__pfn_to_section(pfn)))
244-
return pfn_section_valid(__pfn_to_section(pfn), pfn);
253+
if (!early_section(ms))
254+
return pfn_section_valid(ms, pfn);
255+
}
245256
#endif
246257
return memblock_is_map_memory(addr);
247258
}

0 commit comments

Comments
 (0)