Commit 368a0590 authored by Aneesh Kumar K.V's avatar Aneesh Kumar K.V Committed by Andrew Morton

powerpc/book3s64/vmemmap: switch radix to use a different vmemmap handling function

This is in preparation to update radix to implement vmemmap optimization
for devdax.  Below are the rules w.r.t radix vmemmap mapping

1. First try to map things using PMD (2M)
2. With altmap if altmap cross-boundary check returns true, fall back to
   PAGE_SIZE
3. If we can't allocate PMD_SIZE backing memory for vmemmap, fallback to
   PAGE_SIZE

On removing vmemmap mapping, check if every subsection that is using the
vmemmap area is invalid.  If found to be invalid, that implies we can
safely free the vmemmap area.  We don't use the PAGE_UNUSED pattern used
by x86 because with 64K page size, we need to do the above check even at
the PAGE_SIZE granularity.

[aneesh.kumar@linux.ibm.com: fix section mismatch warning]
  Link: https://lkml.kernel.org/r/87h6pqvu5g.fsf@linux.ibm.com
[aneesh.kumar@linux.ibm.com: fix kernel build error]
  Link: https://lkml.kernel.org/r/877cqkwd20.fsf@linux.ibm.com
Link: https://lkml.kernel.org/r/20230724190759.483013-11-aneesh.kumar@linux.ibm.comSigned-off-by: default avatarAneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Joao Martins <joao.m.martins@oracle.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Nicholas Piggin <npiggin@gmail.com>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: Will Deacon <will@kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent 27af67f3
...@@ -331,6 +331,8 @@ extern int __meminit radix__vmemmap_create_mapping(unsigned long start, ...@@ -331,6 +331,8 @@ extern int __meminit radix__vmemmap_create_mapping(unsigned long start,
unsigned long phys); unsigned long phys);
int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end, int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end,
int node, struct vmem_altmap *altmap); int node, struct vmem_altmap *altmap);
void __ref radix__vmemmap_free(unsigned long start, unsigned long end,
struct vmem_altmap *altmap);
extern void radix__vmemmap_remove_mapping(unsigned long start, extern void radix__vmemmap_remove_mapping(unsigned long start,
unsigned long page_size); unsigned long page_size);
......
...@@ -157,6 +157,12 @@ static inline pgtable_t pmd_pgtable(pmd_t pmd) ...@@ -157,6 +157,12 @@ static inline pgtable_t pmd_pgtable(pmd_t pmd)
return (pgtable_t)pmd_page_vaddr(pmd); return (pgtable_t)pmd_page_vaddr(pmd);
} }
#ifdef CONFIG_PPC64
int __meminit vmemmap_populated(unsigned long vmemmap_addr, int vmemmap_map_size);
bool altmap_cross_boundary(struct vmem_altmap *altmap, unsigned long start,
unsigned long page_size);
#endif /* CONFIG_PPC64 */
#endif /* __ASSEMBLY__ */ #endif /* __ASSEMBLY__ */
#endif /* _ASM_POWERPC_PGTABLE_H */ #endif /* _ASM_POWERPC_PGTABLE_H */
...@@ -744,8 +744,58 @@ static void free_pud_table(pud_t *pud_start, p4d_t *p4d) ...@@ -744,8 +744,58 @@ static void free_pud_table(pud_t *pud_start, p4d_t *p4d)
p4d_clear(p4d); p4d_clear(p4d);
} }
static void remove_pte_table(pte_t *pte_start, unsigned long addr, #ifdef CONFIG_SPARSEMEM_VMEMMAP
unsigned long end, bool direct) static bool __meminit vmemmap_pmd_is_unused(unsigned long addr, unsigned long end)
{
unsigned long start = ALIGN_DOWN(addr, PMD_SIZE);
return !vmemmap_populated(start, PMD_SIZE);
}
static bool __meminit vmemmap_page_is_unused(unsigned long addr, unsigned long end)
{
unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
return !vmemmap_populated(start, PAGE_SIZE);
}
#endif
static void __meminit free_vmemmap_pages(struct page *page,
struct vmem_altmap *altmap,
int order)
{
unsigned int nr_pages = 1 << order;
if (altmap) {
unsigned long alt_start, alt_end;
unsigned long base_pfn = page_to_pfn(page);
/*
* with 2M vmemmap mmaping we can have things setup
* such that even though atlmap is specified we never
* used altmap.
*/
alt_start = altmap->base_pfn;
alt_end = altmap->base_pfn + altmap->reserve + altmap->free;
if (base_pfn >= alt_start && base_pfn < alt_end) {
vmem_altmap_free(altmap, nr_pages);
return;
}
}
if (PageReserved(page)) {
/* allocated from memblock */
while (nr_pages--)
free_reserved_page(page++);
} else
free_pages((unsigned long)page_address(page), order);
}
static void __meminit remove_pte_table(pte_t *pte_start, unsigned long addr,
unsigned long end, bool direct,
struct vmem_altmap *altmap)
{ {
unsigned long next, pages = 0; unsigned long next, pages = 0;
pte_t *pte; pte_t *pte;
...@@ -759,24 +809,26 @@ static void remove_pte_table(pte_t *pte_start, unsigned long addr, ...@@ -759,24 +809,26 @@ static void remove_pte_table(pte_t *pte_start, unsigned long addr,
if (!pte_present(*pte)) if (!pte_present(*pte))
continue; continue;
if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(next)) { if (PAGE_ALIGNED(addr) && PAGE_ALIGNED(next)) {
/* if (!direct)
* The vmemmap_free() and remove_section_mapping() free_vmemmap_pages(pte_page(*pte), altmap, 0);
* codepaths call us with aligned addresses. pte_clear(&init_mm, addr, pte);
*/ pages++;
WARN_ONCE(1, "%s: unaligned range\n", __func__);
continue;
} }
#ifdef CONFIG_SPARSEMEM_VMEMMAP
pte_clear(&init_mm, addr, pte); else if (!direct && vmemmap_page_is_unused(addr, next)) {
pages++; free_vmemmap_pages(pte_page(*pte), altmap, 0);
pte_clear(&init_mm, addr, pte);
}
#endif
} }
if (direct) if (direct)
update_page_count(mmu_virtual_psize, -pages); update_page_count(mmu_virtual_psize, -pages);
} }
static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr, static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
unsigned long end, bool direct) unsigned long end, bool direct,
struct vmem_altmap *altmap)
{ {
unsigned long next, pages = 0; unsigned long next, pages = 0;
pte_t *pte_base; pte_t *pte_base;
...@@ -790,18 +842,24 @@ static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr, ...@@ -790,18 +842,24 @@ static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
continue; continue;
if (pmd_is_leaf(*pmd)) { if (pmd_is_leaf(*pmd)) {
if (!IS_ALIGNED(addr, PMD_SIZE) || if (IS_ALIGNED(addr, PMD_SIZE) &&
!IS_ALIGNED(next, PMD_SIZE)) { IS_ALIGNED(next, PMD_SIZE)) {
WARN_ONCE(1, "%s: unaligned range\n", __func__); if (!direct)
continue; free_vmemmap_pages(pmd_page(*pmd), altmap, get_order(PMD_SIZE));
pte_clear(&init_mm, addr, (pte_t *)pmd);
pages++;
} }
pte_clear(&init_mm, addr, (pte_t *)pmd); #ifdef CONFIG_SPARSEMEM_VMEMMAP
pages++; else if (!direct && vmemmap_pmd_is_unused(addr, next)) {
free_vmemmap_pages(pmd_page(*pmd), altmap, get_order(PMD_SIZE));
pte_clear(&init_mm, addr, (pte_t *)pmd);
}
#endif
continue; continue;
} }
pte_base = (pte_t *)pmd_page_vaddr(*pmd); pte_base = (pte_t *)pmd_page_vaddr(*pmd);
remove_pte_table(pte_base, addr, next, direct); remove_pte_table(pte_base, addr, next, direct, altmap);
free_pte_table(pte_base, pmd); free_pte_table(pte_base, pmd);
} }
if (direct) if (direct)
...@@ -809,7 +867,8 @@ static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr, ...@@ -809,7 +867,8 @@ static void __meminit remove_pmd_table(pmd_t *pmd_start, unsigned long addr,
} }
static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr, static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr,
unsigned long end, bool direct) unsigned long end, bool direct,
struct vmem_altmap *altmap)
{ {
unsigned long next, pages = 0; unsigned long next, pages = 0;
pmd_t *pmd_base; pmd_t *pmd_base;
...@@ -834,15 +893,16 @@ static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr, ...@@ -834,15 +893,16 @@ static void __meminit remove_pud_table(pud_t *pud_start, unsigned long addr,
} }
pmd_base = pud_pgtable(*pud); pmd_base = pud_pgtable(*pud);
remove_pmd_table(pmd_base, addr, next, direct); remove_pmd_table(pmd_base, addr, next, direct, altmap);
free_pmd_table(pmd_base, pud); free_pmd_table(pmd_base, pud);
} }
if (direct) if (direct)
update_page_count(MMU_PAGE_1G, -pages); update_page_count(MMU_PAGE_1G, -pages);
} }
static void __meminit remove_pagetable(unsigned long start, unsigned long end, static void __meminit
bool direct) remove_pagetable(unsigned long start, unsigned long end, bool direct,
struct vmem_altmap *altmap)
{ {
unsigned long addr, next; unsigned long addr, next;
pud_t *pud_base; pud_t *pud_base;
...@@ -871,7 +931,7 @@ static void __meminit remove_pagetable(unsigned long start, unsigned long end, ...@@ -871,7 +931,7 @@ static void __meminit remove_pagetable(unsigned long start, unsigned long end,
} }
pud_base = p4d_pgtable(*p4d); pud_base = p4d_pgtable(*p4d);
remove_pud_table(pud_base, addr, next, direct); remove_pud_table(pud_base, addr, next, direct, altmap);
free_pud_table(pud_base, p4d); free_pud_table(pud_base, p4d);
} }
...@@ -894,7 +954,7 @@ int __meminit radix__create_section_mapping(unsigned long start, ...@@ -894,7 +954,7 @@ int __meminit radix__create_section_mapping(unsigned long start,
int __meminit radix__remove_section_mapping(unsigned long start, unsigned long end) int __meminit radix__remove_section_mapping(unsigned long start, unsigned long end)
{ {
remove_pagetable(start, end, true); remove_pagetable(start, end, true, NULL);
return 0; return 0;
} }
#endif /* CONFIG_MEMORY_HOTPLUG */ #endif /* CONFIG_MEMORY_HOTPLUG */
...@@ -926,10 +986,223 @@ int __meminit radix__vmemmap_create_mapping(unsigned long start, ...@@ -926,10 +986,223 @@ int __meminit radix__vmemmap_create_mapping(unsigned long start,
return 0; return 0;
} }
int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node,
unsigned long addr, unsigned long next)
{
int large = pmd_large(*pmdp);
if (large)
vmemmap_verify(pmdp_ptep(pmdp), node, addr, next);
return large;
}
void __meminit vmemmap_set_pmd(pmd_t *pmdp, void *p, int node,
unsigned long addr, unsigned long next)
{
pte_t entry;
pte_t *ptep = pmdp_ptep(pmdp);
VM_BUG_ON(!IS_ALIGNED(addr, PMD_SIZE));
entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
set_pte_at(&init_mm, addr, ptep, entry);
asm volatile("ptesync": : :"memory");
vmemmap_verify(ptep, node, addr, next);
}
static pte_t * __meminit radix__vmemmap_pte_populate(pmd_t *pmdp, unsigned long addr,
int node,
struct vmem_altmap *altmap,
struct page *reuse)
{
pte_t *pte = pte_offset_kernel(pmdp, addr);
if (pte_none(*pte)) {
pte_t entry;
void *p;
if (!reuse) {
/*
* make sure we don't create altmap mappings
* covering things outside the device.
*/
if (altmap && altmap_cross_boundary(altmap, addr, PAGE_SIZE))
altmap = NULL;
p = vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
if (!p && altmap)
p = vmemmap_alloc_block_buf(PAGE_SIZE, node, NULL);
if (!p)
return NULL;
} else {
/*
* When a PTE/PMD entry is freed from the init_mm
* there's a free_pages() call to this page allocated
* above. Thus this get_page() is paired with the
* put_page_testzero() on the freeing path.
* This can only called by certain ZONE_DEVICE path,
* and through vmemmap_populate_compound_pages() when
* slab is available.
*/
get_page(reuse);
p = page_to_virt(reuse);
}
VM_BUG_ON(!PAGE_ALIGNED(addr));
entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
set_pte_at(&init_mm, addr, pte, entry);
asm volatile("ptesync": : :"memory");
}
return pte;
}
static inline pud_t *vmemmap_pud_alloc(p4d_t *p4dp, int node,
unsigned long address)
{
pud_t *pud;
/* All early vmemmap mapping to keep simple do it at PAGE_SIZE */
if (unlikely(p4d_none(*p4dp))) {
if (unlikely(!slab_is_available())) {
pud = early_alloc_pgtable(PAGE_SIZE, node, 0, 0);
p4d_populate(&init_mm, p4dp, pud);
/* go to the pud_offset */
} else
return pud_alloc(&init_mm, p4dp, address);
}
return pud_offset(p4dp, address);
}
static inline pmd_t *vmemmap_pmd_alloc(pud_t *pudp, int node,
unsigned long address)
{
pmd_t *pmd;
/* All early vmemmap mapping to keep simple do it at PAGE_SIZE */
if (unlikely(pud_none(*pudp))) {
if (unlikely(!slab_is_available())) {
pmd = early_alloc_pgtable(PAGE_SIZE, node, 0, 0);
pud_populate(&init_mm, pudp, pmd);
} else
return pmd_alloc(&init_mm, pudp, address);
}
return pmd_offset(pudp, address);
}
static inline pte_t *vmemmap_pte_alloc(pmd_t *pmdp, int node,
unsigned long address)
{
pte_t *pte;
/* All early vmemmap mapping to keep simple do it at PAGE_SIZE */
if (unlikely(pmd_none(*pmdp))) {
if (unlikely(!slab_is_available())) {
pte = early_alloc_pgtable(PAGE_SIZE, node, 0, 0);
pmd_populate(&init_mm, pmdp, pte);
} else
return pte_alloc_kernel(pmdp, address);
}
return pte_offset_kernel(pmdp, address);
}
int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end, int node,
struct vmem_altmap *altmap)
{
unsigned long addr;
unsigned long next;
pgd_t *pgd;
p4d_t *p4d;
pud_t *pud;
pmd_t *pmd;
pte_t *pte;
for (addr = start; addr < end; addr = next) {
next = pmd_addr_end(addr, end);
pgd = pgd_offset_k(addr);
p4d = p4d_offset(pgd, addr);
pud = vmemmap_pud_alloc(p4d, node, addr);
if (!pud)
return -ENOMEM;
pmd = vmemmap_pmd_alloc(pud, node, addr);
if (!pmd)
return -ENOMEM;
if (pmd_none(READ_ONCE(*pmd))) {
void *p;
/*
* keep it simple by checking addr PMD_SIZE alignment
* and verifying the device boundary condition.
* For us to use a pmd mapping, both addr and pfn should
* be aligned. We skip if addr is not aligned and for
* pfn we hope we have extra area in the altmap that
* can help to find an aligned block. This can result
* in altmap block allocation failures, in which case
* we fallback to RAM for vmemmap allocation.
*/
if (altmap && (!IS_ALIGNED(addr, PMD_SIZE) ||
altmap_cross_boundary(altmap, addr, PMD_SIZE))) {
/*
* make sure we don't create altmap mappings
* covering things outside the device.
*/
goto base_mapping;
}
p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
if (p) {
vmemmap_set_pmd(pmd, p, node, addr, next);
continue;
} else if (altmap) {
/*
* A vmemmap block allocation can fail due to
* alignment requirements and we trying to align
* things aggressively there by running out of
* space. Try base mapping on failure.
*/
goto base_mapping;
}
} else if (vmemmap_check_pmd(pmd, node, addr, next)) {
/*
* If a huge mapping exist due to early call to
* vmemmap_populate, let's try to use that.
*/
continue;
}
base_mapping:
/*
* Not able allocate higher order memory to back memmap
* or we found a pointer to pte page. Allocate base page
* size vmemmap
*/
pte = vmemmap_pte_alloc(pmd, node, addr);
if (!pte)
return -ENOMEM;
pte = radix__vmemmap_pte_populate(pmd, addr, node, altmap, NULL);
if (!pte)
return -ENOMEM;
vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
next = addr + PAGE_SIZE;
}
return 0;
}
#ifdef CONFIG_MEMORY_HOTPLUG #ifdef CONFIG_MEMORY_HOTPLUG
void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size) void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned long page_size)
{ {
remove_pagetable(start, start + page_size, false); remove_pagetable(start, start + page_size, true, NULL);
}
void __ref radix__vmemmap_free(unsigned long start, unsigned long end,
struct vmem_altmap *altmap)
{
remove_pagetable(start, end, false, altmap);
} }
#endif #endif
#endif #endif
......
...@@ -92,7 +92,7 @@ static struct page * __meminit vmemmap_subsection_start(unsigned long vmemmap_ad ...@@ -92,7 +92,7 @@ static struct page * __meminit vmemmap_subsection_start(unsigned long vmemmap_ad
* a page table lookup here because with the hash translation we don't keep * a page table lookup here because with the hash translation we don't keep
* vmemmap details in linux page table. * vmemmap details in linux page table.
*/ */
static int __meminit vmemmap_populated(unsigned long vmemmap_addr, int vmemmap_map_size) int __meminit vmemmap_populated(unsigned long vmemmap_addr, int vmemmap_map_size)
{ {
struct page *start; struct page *start;
unsigned long vmemmap_end = vmemmap_addr + vmemmap_map_size; unsigned long vmemmap_end = vmemmap_addr + vmemmap_map_size;
...@@ -183,8 +183,8 @@ static __meminit int vmemmap_list_populate(unsigned long phys, ...@@ -183,8 +183,8 @@ static __meminit int vmemmap_list_populate(unsigned long phys,
return 0; return 0;
} }
static bool altmap_cross_boundary(struct vmem_altmap *altmap, unsigned long start, bool altmap_cross_boundary(struct vmem_altmap *altmap, unsigned long start,
unsigned long page_size) unsigned long page_size)
{ {
unsigned long nr_pfn = page_size / sizeof(struct page); unsigned long nr_pfn = page_size / sizeof(struct page);
unsigned long start_pfn = page_to_pfn((struct page *)start); unsigned long start_pfn = page_to_pfn((struct page *)start);
...@@ -204,6 +204,11 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, ...@@ -204,6 +204,11 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
bool altmap_alloc; bool altmap_alloc;
unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift; unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
#ifdef CONFIG_PPC_BOOK3S_64
if (radix_enabled())
return radix__vmemmap_populate(start, end, node, altmap);
#endif
/* Align to the page size of the linear mapping. */ /* Align to the page size of the linear mapping. */
start = ALIGN_DOWN(start, page_size); start = ALIGN_DOWN(start, page_size);
...@@ -303,8 +308,8 @@ static unsigned long vmemmap_list_free(unsigned long start) ...@@ -303,8 +308,8 @@ static unsigned long vmemmap_list_free(unsigned long start)
return vmem_back->phys; return vmem_back->phys;
} }
void __ref vmemmap_free(unsigned long start, unsigned long end, static void __ref __vmemmap_free(unsigned long start, unsigned long end,
struct vmem_altmap *altmap) struct vmem_altmap *altmap)
{ {
unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift; unsigned long page_size = 1 << mmu_psize_defs[mmu_vmemmap_psize].shift;
unsigned long page_order = get_order(page_size); unsigned long page_order = get_order(page_size);
...@@ -362,6 +367,17 @@ void __ref vmemmap_free(unsigned long start, unsigned long end, ...@@ -362,6 +367,17 @@ void __ref vmemmap_free(unsigned long start, unsigned long end,
vmemmap_remove_mapping(start, page_size); vmemmap_remove_mapping(start, page_size);
} }
} }
void __ref vmemmap_free(unsigned long start, unsigned long end,
struct vmem_altmap *altmap)
{
#ifdef CONFIG_PPC_BOOK3S_64
if (radix_enabled())
return radix__vmemmap_free(start, end, altmap);
#endif
return __vmemmap_free(start, end, altmap);
}
#endif #endif
void register_page_bootmem_memmap(unsigned long section_nr, void register_page_bootmem_memmap(unsigned long section_nr,
struct page *start_page, unsigned long size) struct page *start_page, unsigned long size)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment