[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20201222142440.28930-6-songmuchun@bytedance.com>
Date: Tue, 22 Dec 2020 22:24:34 +0800
From: Muchun Song <songmuchun@...edance.com>
To: corbet@....net, mike.kravetz@...cle.com, tglx@...utronix.de,
mingo@...hat.com, bp@...en8.de, x86@...nel.org, hpa@...or.com,
dave.hansen@...ux.intel.com, luto@...nel.org, peterz@...radead.org,
viro@...iv.linux.org.uk, akpm@...ux-foundation.org,
paulmck@...nel.org, mchehab+huawei@...nel.org,
pawan.kumar.gupta@...ux.intel.com, rdunlap@...radead.org,
oneukum@...e.com, anshuman.khandual@....com, jroedel@...e.de,
almasrymina@...gle.com, rientjes@...gle.com, willy@...radead.org,
osalvador@...e.de, mhocko@...e.com, song.bao.hua@...ilicon.com,
david@...hat.com, naoya.horiguchi@....com
Cc: duanxiongchun@...edance.com, linux-doc@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-mm@...ck.org,
linux-fsdevel@...r.kernel.org,
Muchun Song <songmuchun@...edance.com>
Subject: [PATCH v11 05/11] mm/hugetlb: Allocate the vmemmap pages associated with each HugeTLB page
When we free a HugeTLB page to the buddy allocator, we should allocate the
vmemmap pages associated with it. We can do that in the __free_hugepage()
before freeing it to buddy.
Signed-off-by: Muchun Song <songmuchun@...edance.com>
---
include/linux/mm.h | 2 ++
mm/hugetlb.c | 2 ++
mm/hugetlb_vmemmap.c | 15 +++++++++++
mm/hugetlb_vmemmap.h | 5 ++++
mm/sparse-vmemmap.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 99 insertions(+), 1 deletion(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index f928994ed273..16b55d13b0ab 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3007,6 +3007,8 @@ static inline void print_vma_addr(char *prefix, unsigned long rip)
void vmemmap_remap_free(unsigned long start, unsigned long end,
unsigned long reuse);
+void vmemmap_remap_alloc(unsigned long start, unsigned long end,
+ unsigned long reuse);
void *sparse_buffer_alloc(unsigned long size);
struct page * __populate_section_memmap(unsigned long pfn,
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index fc45a900acf1..ab6d2eabfea8 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1365,6 +1365,8 @@ static void __free_hugepage(struct hstate *h, struct page *page)
{
int i;
+ alloc_huge_page_vmemmap(h, page);
+
for (i = 0; i < pages_per_huge_page(h); i++) {
page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
1 << PG_referenced | 1 << PG_dirty |
diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 19f1898aaede..6108ae80314f 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -183,6 +183,21 @@ static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h)
return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT;
}
+void alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
+{
+ unsigned long vmemmap_addr = (unsigned long)head;
+ unsigned long vmemmap_end, vmemmap_reuse;
+
+ if (!free_vmemmap_pages_per_hpage(h))
+ return;
+
+ vmemmap_addr += RESERVE_VMEMMAP_SIZE;
+ vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
+ vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
+
+ vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse);
+}
+
void free_huge_page_vmemmap(struct hstate *h, struct page *head)
{
unsigned long vmemmap_addr = (unsigned long)head;
diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h
index 01f8637adbe0..b2c8d2f11d48 100644
--- a/mm/hugetlb_vmemmap.h
+++ b/mm/hugetlb_vmemmap.h
@@ -11,6 +11,7 @@
#include <linux/hugetlb.h>
#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP
+void alloc_huge_page_vmemmap(struct hstate *h, struct page *head);
void free_huge_page_vmemmap(struct hstate *h, struct page *head);
/*
@@ -25,6 +26,10 @@ static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h)
return 0;
}
#else
+static inline void alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
+{
+}
+
static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head)
{
}
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 0e9c49a028b4..ed4702d5d664 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -29,6 +29,7 @@
#include <linux/sched.h>
#include <linux/pgtable.h>
#include <linux/bootmem_info.h>
+#include <linux/delay.h>
#include <asm/dma.h>
#include <asm/pgalloc.h>
@@ -40,7 +41,8 @@
* @remap_pte: called for each non-empty PTE (lowest-level) entry.
* @reuse_page: the page which is reused for the tail vmemmap pages.
* @reuse_addr: the virtual address of the @reuse_page page.
- * @vmemmap_pages: the list head of the vmemmap pages that can be freed.
+ * @vmemmap_pages: the list head of the vmemmap pages that can be freed
+ * or is mapped from.
*/
struct vmemmap_remap_walk {
void (*remap_pte)(pte_t *pte, unsigned long addr,
@@ -50,6 +52,10 @@ struct vmemmap_remap_walk {
struct list_head *vmemmap_pages;
};
+/* The gfp mask of allocating vmemmap page */
+#define GFP_VMEMMAP_PAGE \
+ (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN | __GFP_THISNODE)
+
static void vmemmap_pte_range(pmd_t *pmd, unsigned long addr,
unsigned long end,
struct vmemmap_remap_walk *walk)
@@ -212,6 +218,74 @@ void vmemmap_remap_free(unsigned long start, unsigned long end,
free_vmemmap_page_list(&vmemmap_pages);
}
+static void vmemmap_restore_pte(pte_t *pte, unsigned long addr,
+ struct vmemmap_remap_walk *walk)
+{
+ pgprot_t pgprot = PAGE_KERNEL;
+ struct page *page;
+ void *to;
+
+ BUG_ON(pte_page(*pte) != walk->reuse_page);
+
+ page = list_first_entry(walk->vmemmap_pages, struct page, lru);
+ list_del(&page->lru);
+ to = page_to_virt(page);
+ copy_page(to, (void *)walk->reuse_addr);
+
+ set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot));
+}
+
+static void alloc_vmemmap_page_list(struct list_head *list,
+ unsigned long start, unsigned long end)
+{
+ unsigned long addr;
+
+ for (addr = start; addr < end; addr += PAGE_SIZE) {
+ struct page *page;
+ int nid = page_to_nid((const void *)addr);
+
+retry:
+ page = alloc_pages_node(nid, GFP_VMEMMAP_PAGE, 0);
+ if (unlikely(!page)) {
+ msleep(100);
+ /*
+ * We should retry infinitely, because we cannot
+ * handle allocation failures. Once we allocate
+ * vmemmap pages successfully, then we can free
+ * a HugeTLB page.
+ */
+ goto retry;
+ }
+ list_add_tail(&page->lru, list);
+ }
+}
+
+/**
+ * vmemmap_remap_alloc - remap the vmemmap virtual address range [@start, end)
+ * to the page which is from the @vmemmap_pages
+ * respectively.
+ * @start: start address of the vmemmap virtual address range.
+ * @end: end address of the vmemmap virtual address range.
+ * @reuse: reuse address.
+ */
+void vmemmap_remap_alloc(unsigned long start, unsigned long end,
+ unsigned long reuse)
+{
+ LIST_HEAD(vmemmap_pages);
+ struct vmemmap_remap_walk walk = {
+ .remap_pte = vmemmap_restore_pte,
+ .reuse_addr = reuse,
+ .vmemmap_pages = &vmemmap_pages,
+ };
+
+ might_sleep();
+
+ BUG_ON(start != reuse + PAGE_SIZE);
+
+ alloc_vmemmap_page_list(&vmemmap_pages, start, end);
+ vmemmap_remap_range(reuse, end, &walk);
+}
+
/*
* Allocate a block of memory to be used to back the virtual memory map
* or to back the page tables that are used to create the mapping.
--
2.11.0
Powered by blists - more mailing lists