A compound pagemap is a dev_pagemap with @align > PAGE_SIZE and it
means that pages are mapped at a given huge page alignment and utilize
uses compound pages as opposed to order-0 pages.

To minimize struct page overhead we take advantage of the fact that
most tail pages look the same (except the first two). We allocate a
separate page for the vmemmap area which contains the head page and
separate for the next 64 pages. The rest of the subsections then reuse
this tail vmemmap page to initialize the rest of the tail pages.

Sections are arch-dependent (e.g. on x86 it's 64M, 128M or 512M) and
when initializing compound pagemap with big enough @align (e.g. 1G
PUD) it  will cross various sections. To be able to reuse tail pages
across sections belonging to the same gigantic page we fetch the
@range being mapped (nr_ranges + 1).  If the section being mapped is
not offset 0 of the @align, then lookup the PFN of the struct page
address that preceeds it and use that to populate the entire
section.

On compound pagemaps with 2M align, this lets mechanism saves 6 pages
out of the 8 necessary PFNs necessary to set the subsection's 512
struct pages being mapped. On a 1G compound pagemap it saves
4094 pages.

Altmap isn't supported yet, given various restrictions in altmap pfn
allocator, thus fallback to the already in use vmemmap_populate().

Signed-off-by: Joao Martins <joao.m.mart...@oracle.com>
---
 include/linux/mm.h  |   2 +-
 mm/memremap.c       |   1 +
 mm/sparse-vmemmap.c | 139 ++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 131 insertions(+), 11 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 61474602c2b1..49d717ae40ae 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3040,7 +3040,7 @@ p4d_t *vmemmap_p4d_populate(pgd_t *pgd, unsigned long 
addr, int node);
 pud_t *vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node);
 pmd_t *vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node);
 pte_t *vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
-                           struct vmem_altmap *altmap);
+                           struct vmem_altmap *altmap, void *block);
 void *vmemmap_alloc_block(unsigned long size, int node);
 struct vmem_altmap;
 void *vmemmap_alloc_block_buf(unsigned long size, int node,
diff --git a/mm/memremap.c b/mm/memremap.c
index d160853670c4..2e6bc0b1ff00 100644
--- a/mm/memremap.c
+++ b/mm/memremap.c
@@ -345,6 +345,7 @@ void *memremap_pages(struct dev_pagemap *pgmap, int nid)
 {
        struct mhp_params params = {
                .altmap = pgmap_altmap(pgmap),
+               .pgmap = pgmap,
                .pgprot = PAGE_KERNEL,
        };
        const int nr_range = pgmap->nr_range;
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 8814876edcfa..f57c5eada099 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -141,16 +141,20 @@ void __meminit vmemmap_verify(pte_t *pte, int node,
 }
 
 pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int 
node,
-                                      struct vmem_altmap *altmap)
+                                      struct vmem_altmap *altmap, void *block)
 {
        pte_t *pte = pte_offset_kernel(pmd, addr);
        if (pte_none(*pte)) {
                pte_t entry;
-               void *p;
-
-               p = vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
-               if (!p)
-                       return NULL;
+               void *p = block;
+
+               if (!block) {
+                       p = vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
+                       if (!p)
+                               return NULL;
+               } else if (!altmap) {
+                       get_page(virt_to_page(block));
+               }
                entry = pfn_pte(__pa(p) >> PAGE_SHIFT, PAGE_KERNEL);
                set_pte_at(&init_mm, addr, pte, entry);
        }
@@ -217,7 +221,8 @@ pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, 
int node)
 }
 
 static int __meminit vmemmap_populate_address(unsigned long addr, int node,
-                                             struct vmem_altmap *altmap)
+                                             struct vmem_altmap *altmap,
+                                             void *page, void **ptr)
 {
        pgd_t *pgd;
        p4d_t *p4d;
@@ -237,10 +242,14 @@ static int __meminit vmemmap_populate_address(unsigned 
long addr, int node,
        pmd = vmemmap_pmd_populate(pud, addr, node);
        if (!pmd)
                return -ENOMEM;
-       pte = vmemmap_pte_populate(pmd, addr, node, altmap);
+       pte = vmemmap_pte_populate(pmd, addr, node, altmap, page);
        if (!pte)
                return -ENOMEM;
        vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
+
+       if (ptr)
+               *ptr = __va(__pfn_to_phys(pte_pfn(*pte)));
+       return 0;
 }
 
 int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long 
end,
@@ -249,7 +258,110 @@ int __meminit vmemmap_populate_basepages(unsigned long 
start, unsigned long end,
        unsigned long addr = start;
 
        for (; addr < end; addr += PAGE_SIZE) {
-               if (vmemmap_populate_address(addr, node, altmap))
+               if (vmemmap_populate_address(addr, node, altmap, NULL, NULL))
+                       return -ENOMEM;
+       }
+
+       return 0;
+}
+
+static int __meminit vmemmap_populate_range(unsigned long start,
+                                           unsigned long end,
+                                           int node, void *page)
+{
+       unsigned long addr = start;
+
+       for (; addr < end; addr += PAGE_SIZE) {
+               if (vmemmap_populate_address(addr, node, NULL, page, NULL))
+                       return -ENOMEM;
+       }
+
+       return 0;
+}
+
+static inline int __meminit vmemmap_populate_page(unsigned long addr, int node,
+                                                 void **ptr)
+{
+       return vmemmap_populate_address(addr, node, NULL, NULL, ptr);
+}
+
+static pte_t * __meminit vmemmap_lookup_address(unsigned long addr)
+{
+       pgd_t *pgd;
+       p4d_t *p4d;
+       pud_t *pud;
+       pmd_t *pmd;
+       pte_t *pte;
+
+       pgd = pgd_offset_k(addr);
+       if (pgd_none(*pgd))
+               return NULL;
+
+       p4d = p4d_offset(pgd, addr);
+       if (p4d_none(*p4d))
+               return NULL;
+
+       pud = pud_offset(p4d, addr);
+       if (pud_none(*pud))
+               return NULL;
+
+       pmd = pmd_offset(pud, addr);
+       if (pmd_none(*pmd))
+               return NULL;
+
+       pte = pte_offset_kernel(pmd, addr);
+       if (pte_none(*pte))
+               return NULL;
+
+       return pte;
+}
+
+static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
+                                                    unsigned long start,
+                                                    unsigned long end, int 
node,
+                                                    struct dev_pagemap *pgmap)
+{
+       unsigned long offset, size, addr;
+
+       /*
+        * For compound pages bigger than section size (e.g. 1G) fill the rest
+        * of sections as tail pages.
+        *
+        * Note that memremap_pages() resets @nr_range value and will increment
+        * it after each range successful onlining. Thus the value or @nr_range
+        * at section memmap populate corresponds to the in-progress range
+        * being onlined that we care about.
+        */
+       offset = PFN_PHYS(start_pfn) - pgmap->ranges[pgmap->nr_range].start;
+       if (!IS_ALIGNED(offset, pgmap_align(pgmap)) &&
+           pgmap_align(pgmap) > SUBSECTION_SIZE) {
+               pte_t *ptep = vmemmap_lookup_address(start - PAGE_SIZE);
+
+               if (!ptep)
+                       return -ENOMEM;
+
+               return vmemmap_populate_range(start, end, node,
+                                             page_to_virt(pte_page(*ptep)));
+       }
+
+       size = min(end - start, pgmap_pfn_align(pgmap) * sizeof(struct page));
+       for (addr = start; addr < end; addr += size) {
+               unsigned long next = addr, last = addr + size;
+               void *block;
+
+               /* Populate the head page vmemmap page */
+               if (vmemmap_populate_page(addr, node, NULL))
+                       return -ENOMEM;
+
+               /* Populate the tail pages vmemmap page */
+               block = NULL;
+               next = addr + PAGE_SIZE;
+               if (vmemmap_populate_page(next, node, &block))
+                       return -ENOMEM;
+
+               /* Reuse the previous page for the rest of tail pages */
+               next += PAGE_SIZE;
+               if (vmemmap_populate_range(next, last, node, block))
                        return -ENOMEM;
        }
 
@@ -262,12 +374,19 @@ struct page * __meminit 
__populate_section_memmap(unsigned long pfn,
 {
        unsigned long start = (unsigned long) pfn_to_page(pfn);
        unsigned long end = start + nr_pages * sizeof(struct page);
+       unsigned int align = pgmap_align(pgmap);
+       int r;
 
        if (WARN_ON_ONCE(!IS_ALIGNED(pfn, PAGES_PER_SUBSECTION) ||
                !IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION)))
                return NULL;
 
-       if (vmemmap_populate(start, end, nid, altmap))
+       if (align > PAGE_SIZE && !altmap)
+               r = vmemmap_populate_compound_pages(pfn, start, end, nid, 
pgmap);
+       else
+               r = vmemmap_populate(start, end, nid, altmap);
+
+       if (r < 0)
                return NULL;
 
        return pfn_to_page(pfn);
-- 
2.17.1
_______________________________________________
Linux-nvdimm mailing list -- linux-nvdimm@lists.01.org
To unsubscribe send an email to linux-nvdimm-le...@lists.01.org

Reply via email to