PowerPC radix has a PMD-sized vmemmap mapping that can cover more than
one sparsemem section, so its HVO populate path handled preexisting
ordinary PMD mappings while populating an HVO-capable range.

Now that the generic sparse-vmemmap helper supports that case, switch the
PowerPC HVO path to vmemmap_populate_hugepages() and remove the duplicate
local page-table walk.

Signed-off-by: Muchun Song <[email protected]>
---
 arch/powerpc/mm/book3s64/radix_pgtable.c | 150 +++--------------------
 1 file changed, 16 insertions(+), 134 deletions(-)

diff --git a/arch/powerpc/mm/book3s64/radix_pgtable.c 
b/arch/powerpc/mm/book3s64/radix_pgtable.c
index 6558fc74483f..4dc1e0940e7b 100644
--- a/arch/powerpc/mm/book3s64/radix_pgtable.c
+++ b/arch/powerpc/mm/book3s64/radix_pgtable.c
@@ -980,8 +980,7 @@ int __meminit radix__vmemmap_create_mapping(unsigned long 
start,
 
 static pte_t * __meminit radix__vmemmap_pte_populate(pmd_t *pmdp, unsigned 
long addr,
                                                     int node,
-                                                    struct vmem_altmap *altmap,
-                                                    struct page *reuse)
+                                                    struct vmem_altmap *altmap)
 {
        pte_t *pte = pte_offset_kernel(pmdp, addr);
 
@@ -989,44 +988,22 @@ static pte_t * __meminit 
radix__vmemmap_pte_populate(pmd_t *pmdp, unsigned long
                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;
-                       pr_debug("PAGE_SIZE vmemmap mapping\n");
-               } 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 try_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.
-                        *
-                        * Use try_get_page() to prevent the shared page 
refcount
-                        * from overflowing.
-                        */
-                       if (!try_get_page(reuse))
-                               return NULL;
-                       p = page_to_virt(reuse);
-                       pr_debug("Tail page reuse vmemmap mapping\n");
-               }
+               /*
+                * 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;
+               pr_debug("PAGE_SIZE vmemmap mapping\n");
 
                VM_BUG_ON(!PAGE_ALIGNED(addr));
                entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
-               /* Map the shared tail page as read-only to catch illegal 
writes. */
-               if (reuse)
-                       entry = pte_wrprotect(entry);
                set_pte_at(&init_mm, addr, pte, entry);
        }
        return pte;
@@ -1081,10 +1058,6 @@ static inline pte_t *vmemmap_pte_alloc(pmd_t *pmdp, int 
node,
        return pte_offset_kernel(pmdp, address);
 }
 
-static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
-                                                    unsigned long start,
-                                                    unsigned long end, int 
node);
-
 int __meminit radix__vmemmap_populate(unsigned long start, unsigned long end, 
int node,
                                      struct vmem_altmap *altmap)
 {
@@ -1098,7 +1071,7 @@ int __meminit radix__vmemmap_populate(unsigned long 
start, unsigned long end, in
        unsigned long pfn = page_to_pfn((struct page *)start);
 
        if (vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)))
-               return vmemmap_populate_compound_pages(pfn, start, end, node);
+               return vmemmap_populate_hugepages(start, end, node, NULL);
        /*
         * If altmap is present, Make sure we align the start vmemmap addr
         * to PAGE_SIZE so that we calculate the correct start_pfn in
@@ -1184,7 +1157,7 @@ int __meminit radix__vmemmap_populate(unsigned long 
start, unsigned long end, in
                if (!pte)
                        return -ENOMEM;
 
-               pte = radix__vmemmap_pte_populate(pmd, addr, node, altmap, 
NULL);
+               pte = radix__vmemmap_pte_populate(pmd, addr, node, altmap);
                if (!pte)
                        return -ENOMEM;
 
@@ -1194,97 +1167,6 @@ int __meminit radix__vmemmap_populate(unsigned long 
start, unsigned long end, in
        return 0;
 }
 
-static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
-                                                    unsigned long start,
-                                                    unsigned long end, int 
node)
-{
-       /*
-        * we want to map things as base page size mapping so that
-        * we can save space in vmemmap. We could have huge mapping
-        * covering out both edges.
-        */
-       unsigned long addr;
-       unsigned long next;
-       pgd_t *pgd;
-       p4d_t *p4d;
-       pud_t *pud;
-       pmd_t *pmd;
-       pte_t *pte;
-       struct page *tail_page;
-       unsigned int order = pfn_to_section_compound_order(start_pfn);
-
-       tail_page = vmemmap_shared_tail_page(order, device_zone(node));
-       if (!tail_page)
-               return -ENOMEM;
-
-       for (addr = start; addr < end; addr = next) {
-
-               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_leaf(READ_ONCE(*pmd))) {
-                       unsigned long addr_pfn = page_to_pfn((struct page 
*)addr);
-                       struct mem_section *ms = __pfn_to_section(addr_pfn);
-
-                       /*
-                        * Existing PMD mappings can cover the edges of a valid
-                        * HVO populate range. They are ordinary vmemmap 
mappings,
-                        * so the current section must not be treated as 
HVO-backed
-                        * when its struct pages are initialized later.
-                        */
-                       section_set_compound_order(ms, 0);
-                       next = pmd_addr_end(addr, end);
-                       continue;
-               }
-               pte = vmemmap_pte_alloc(pmd, node, addr);
-               if (!pte)
-                       return -ENOMEM;
-               if (!pte_none(*pte)) {
-                       /*
-                        * This could be because we already have a compound
-                        * page whose retained vmemmap page was mapped and
-                        * this request fall in those pages.
-                        */
-                       next = addr + PAGE_SIZE;
-                       continue;
-               } else {
-                       unsigned long nr_pages = 1UL << order;
-                       unsigned long addr_pfn = page_to_pfn((struct page 
*)addr);
-                       unsigned long pfn_offset = addr_pfn - 
ALIGN_DOWN(addr_pfn, nr_pages);
-
-                       /*
-                        * if the address is aligned to huge page size it is the
-                        * head mapping.
-                        */
-                       if (pfn_offset == 0) {
-                               /* Populate the head page vmemmap page */
-                               pte = radix__vmemmap_pte_populate(pmd, addr, 
node, NULL, NULL);
-                               if (!pte)
-                                       return -ENOMEM;
-                               vmemmap_verify(pte, node, addr, addr + 
PAGE_SIZE);
-
-                               next = addr + PAGE_SIZE;
-                               continue;
-                       }
-
-                       pte = radix__vmemmap_pte_populate(pmd, addr, node, 
NULL, tail_page);
-                       if (!pte)
-                               return -ENOMEM;
-                       vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
-
-                       next = addr + PAGE_SIZE;
-                       continue;
-               }
-       }
-       return 0;
-}
-
 
 #ifdef CONFIG_MEMORY_HOTPLUG
 void __meminit radix__vmemmap_remove_mapping(unsigned long start, unsigned 
long page_size)
-- 
2.54.0


Reply via email to