Re: [PATCH V2 2/2] arm64/mm: Enable vmem_altmap support for vmemmap mappings

From: Anshuman Khandual
Date: Tue Mar 24 2020 - 08:03:37 EST



On 03/21/2020 01:05 AM, Robin Murphy wrote:
> On 2020-03-04 2:10 pm, Anshuman Khandual wrote:
>> Device memory ranges when getting hot added into ZONE_DEVICE, might require
>> their vmemmap mapping's backing memory to be allocated from their own range
>> instead of consuming system memory. This prevents large system memory usage
>> for potentially large device memory ranges. Device driver communicates this
>> request via vmem_altmap structure. Architecture needs to take this request
>> into account while creating and tearing down vemmmap mappings.
>>
>> This enables vmem_altmap support in vmemmap_populate() and vmemmap_free()
>> which includes vmemmap_populate_basepages() used for ARM64_16K_PAGES and
>> ARM64_64K_PAGES configs.
>>
>> Cc: Catalin Marinas <catalin.marinas@xxxxxxx>
>> Cc: Will Deacon <will@xxxxxxxxxx>
>> Cc: Mark Rutland <mark.rutland@xxxxxxx>
>> Cc: Steve Capper <steve.capper@xxxxxxx>
>> Cc: David Hildenbrand <david@xxxxxxxxxx>
>> Cc: Yu Zhao <yuzhao@xxxxxxxxxx>
>> Cc: Hsin-Yi Wang <hsinyi@xxxxxxxxxxxx>
>> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
>> Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
>> Cc: linux-arm-kernel@xxxxxxxxxxxxxxxxxxx
>> Cc: linux-kernel@xxxxxxxxxxxxxxx
>>
>> Signed-off-by: Anshuman Khandual <anshuman.khandual@xxxxxxx>
>> ---
>> Â arch/arm64/mm/mmu.c | 71 ++++++++++++++++++++++++++++++++-------------
>> Â 1 file changed, 51 insertions(+), 20 deletions(-)
>>
>> diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c
>> index 27cb95c471eb..0e0a0ecc812e 100644
>> --- a/arch/arm64/mm/mmu.c
>> +++ b/arch/arm64/mm/mmu.c
>> @@ -727,15 +727,30 @@ int kern_addr_valid(unsigned long addr)
>> Â }
>> Â Â #ifdef CONFIG_MEMORY_HOTPLUG
>> -static void free_hotplug_page_range(struct page *page, size_t size)
>> +static void free_hotplug_page_range(struct page *page, size_t size,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct vmem_altmap *altmap)
>> Â {
>> -ÂÂÂ WARN_ON(PageReserved(page));
>> -ÂÂÂ free_pages((unsigned long)page_address(page), get_order(size));
>> +ÂÂÂ if (altmap) {
>> +ÂÂÂÂÂÂÂ /*
>> +ÂÂÂÂÂÂÂÂ * Though unmap_hotplug_range() will tear down altmap based
>> +ÂÂÂÂÂÂÂÂ * vmemmap mappings at all page table levels, these mappings
>> +ÂÂÂÂÂÂÂÂ * should only have been created either at PTE or PMD level
>> +ÂÂÂÂÂÂÂÂ * with vmemmap_populate_basepages() or vmemmap_populate()
>> +ÂÂÂÂÂÂÂÂ * respectively. Unmapping requests at any other level will
>> +ÂÂÂÂÂÂÂÂ * be problematic. Drop these warnings when vmemmap mapping
>> +ÂÂÂÂÂÂÂÂ * is supported at PUD (even perhaps P4D) level.
>> +ÂÂÂÂÂÂÂÂ */
>> +ÂÂÂÂÂÂÂ WARN_ON((size != PAGE_SIZE) && (size != PMD_SIZE));
>
> Isn't that comment equally true of the regular case? AFAICS we don't call vmemmap_alloc_block_buf() with larger than PMD_SIZE either. If the warnings are useful, shouldn't they cover both cases equally? However, given that we never warned before, and the code here appears that it would work fine anyway, *are* they really useful?

Sure, this is not something exclusively applicable for altmap based
vmemmap mappings alone. Will drop it from here.

>
>> +ÂÂÂÂÂÂÂ vmem_altmap_free(altmap, size >> PAGE_SHIFT);
>> +ÂÂÂ } else {
>> +ÂÂÂÂÂÂÂ WARN_ON(PageReserved(page));
>> +ÂÂÂÂÂÂÂ free_pages((unsigned long)page_address(page), get_order(size));
>> +ÂÂÂ }
>> Â }
>> Â Â static void free_hotplug_pgtable_page(struct page *page)
>> Â {
>> -ÂÂÂ free_hotplug_page_range(page, PAGE_SIZE);
>> +ÂÂÂ free_hotplug_page_range(page, PAGE_SIZE, NULL);
>> Â }
>> Â Â static bool pgtable_range_aligned(unsigned long start, unsigned long end,
>> @@ -758,7 +773,8 @@ static bool pgtable_range_aligned(unsigned long start, unsigned long end,
>> Â }
>> Â Â static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped)
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct vmem_altmap *altmap)
>> Â {
>> ÂÂÂÂÂ pte_t *ptep, pte;
>> Â @@ -772,12 +788,14 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
>> ÂÂÂÂÂÂÂÂÂ pte_clear(&init_mm, addr, ptep);
>> ÂÂÂÂÂÂÂÂÂ flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
>> ÂÂÂÂÂÂÂÂÂ if (free_mapped)
>> -ÂÂÂÂÂÂÂÂÂÂÂ free_hotplug_page_range(pte_page(pte), PAGE_SIZE);
>> +ÂÂÂÂÂÂÂÂÂÂÂ free_hotplug_page_range(pte_page(pte),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PAGE_SIZE, altmap);
>> ÂÂÂÂÂ } while (addr += PAGE_SIZE, addr < end);
>> Â }
>> Â Â static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped)
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct vmem_altmap *altmap)
>> Â {
>> ÂÂÂÂÂ unsigned long next;
>> ÂÂÂÂÂ pmd_t *pmdp, pmd;
>> @@ -800,16 +818,17 @@ static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ if (free_mapped)
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ free_hotplug_page_range(pmd_page(pmd),
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PMD_SIZE);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PMD_SIZE, altmap);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ continue;
>> ÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂ WARN_ON(!pmd_table(pmd));
>> -ÂÂÂÂÂÂÂ unmap_hotplug_pte_range(pmdp, addr, next, free_mapped);
>> +ÂÂÂÂÂÂÂ unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
>> ÂÂÂÂÂ } while (addr = next, addr < end);
>> Â }
>> Â Â static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped)
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct vmem_altmap *altmap)
>> Â {
>> ÂÂÂÂÂ unsigned long next;
>> ÂÂÂÂÂ pud_t *pudp, pud;
>> @@ -832,16 +851,17 @@ static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ if (free_mapped)
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ free_hotplug_page_range(pud_page(pud),
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PUD_SIZE);
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ PUD_SIZE, altmap);
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ continue;
>> ÂÂÂÂÂÂÂÂÂ }
>> ÂÂÂÂÂÂÂÂÂ WARN_ON(!pud_table(pud));
>> -ÂÂÂÂÂÂÂ unmap_hotplug_pmd_range(pudp, addr, next, free_mapped);
>> +ÂÂÂÂÂÂÂ unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
>> ÂÂÂÂÂ } while (addr = next, addr < end);
>> Â }
>> Â Â static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped)
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ unsigned long end, bool free_mapped,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct vmem_altmap *altmap)
>> Â {
>> ÂÂÂÂÂ unsigned long next;
>> ÂÂÂÂÂ p4d_t *p4dp, p4d;
>> @@ -854,16 +874,24 @@ static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
>> ÂÂÂÂÂÂÂÂÂÂÂÂÂ continue;
>> Â ÂÂÂÂÂÂÂÂÂ WARN_ON(!p4d_present(p4d));
>> -ÂÂÂÂÂÂÂ unmap_hotplug_pud_range(p4dp, addr, next, free_mapped);
>> +ÂÂÂÂÂÂÂ unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
>> ÂÂÂÂÂ } while (addr = next, addr < end);
>> Â }
>> Â Â static void unmap_hotplug_range(unsigned long addr, unsigned long end,
>> -ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bool free_mapped)
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bool free_mapped, struct vmem_altmap *altmap)
>> Â {
>> ÂÂÂÂÂ unsigned long next;
>> ÂÂÂÂÂ pgd_t *pgdp, pgd;
>> Â +ÂÂÂ /*
>> +ÂÂÂÂ * vmem_altmap can only be used as backing memory in a given
>> +ÂÂÂÂ * page table mapping. In case backing memory itself is not
>> +ÂÂÂÂ * being freed, then altmap is irrelevant. Warn about this
>> +ÂÂÂÂ * inconsistency when encountered.
>> +ÂÂÂÂ */
>> +ÂÂÂ WARN_ON(!free_mapped && altmap);
>
> Personally I find that comment a bit unclear (particularly the first sentence which just seems like a confusing tautology). Is the overall point that the altmap only matters when we're unmapping and freeing vmemmap pages (such that we free them to the right allocator)? At face value it doesn't seem to warrant a warning - it's not necessary to know which allocator owns pages that we aren't freeing, but it isn't harmful either.

Probably will change the comment to something like this instead.

/*
* altmap can only be used as vmemmap mapping backing memory.
* In case the backing memory itself is not being freed, then
* altmap is just irrelevant. Warn about this inconsistency
* when encountered.
*/

altmap does decide which allocator, the backing pages will get freed
into. The primary purpose here is to just warn about this invalid
combination i.e (!free_mapped && altmap) which the function should
never be called with.

>
> That said, however, after puzzling through the code I get the distinct feeling it would be more useful if all those "free_mapped" arguments were actually named "is_vmemmap" instead. A that point, the conceptual inconsistency would be a little more obvious (and arguably might not even need commenting).

'free_mapped' was a conscious decision [1] that got added during hot
remove series V9. It avoided the name to be just vmemmap specific as
the unmapping and freeing functions are very generic in nature.

[1] https://lkml.org/lkml/2019/10/8/310

>
> All the altmap plumbing itself looks pretty mechanical and hard to disagree with :)

Okay.

>
> Robin.