From: Muchun Song <songmuchun@bytedance.com>
To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de,
mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com,
dave.hansen@linux.intel.com, luto@kernel.org,
peterz@infradead.org, viro@zeniv.linux.org.uk,
akpm@linux-foundation.org, paulmck@kernel.org,
mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com,
rdunlap@infradead.org, oneukum@suse.com,
anshuman.khandual@arm.com, jroedel@suse.de,
almasrymina@google.com, rientjes@google.com, willy@infradead.org,
osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com,
david@redhat.com, naoya.horiguchi@nec.com
Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-fsdevel@vger.kernel.org,
Muchun Song <songmuchun@bytedance.com>
Subject: [PATCH v10 05/11] mm/hugetlb: Allocate the vmemmap pages associated with each HugeTLB page
Date: Thu, 17 Dec 2020 20:12:57 +0800 [thread overview]
Message-ID: <20201217121303.13386-6-songmuchun@bytedance.com> (raw)
In-Reply-To: <20201217121303.13386-1-songmuchun@bytedance.com>
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@bytedance.com>
---
include/linux/mm.h | 1 +
mm/hugetlb.c | 2 ++
mm/hugetlb_vmemmap.c | 11 ++++++++
mm/hugetlb_vmemmap.h | 5 ++++
mm/sparse-vmemmap.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 91 insertions(+), 1 deletion(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 0ecad1a41190..e7d022b67ee1 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3006,6 +3006,7 @@ static inline void print_vma_addr(char *prefix, unsigned long rip)
#endif
void vmemmap_remap_free(unsigned long start, unsigned long size);
+void vmemmap_remap_alloc(unsigned long start, unsigned long size);
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 9f35f34d3195..329f473b929e 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 c4bbca270453..273816dd95b6 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -183,6 +183,17 @@ 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;
+
+ if (!free_vmemmap_pages_per_hpage(h))
+ return;
+
+ vmemmap_remap_alloc(vmemmap_addr + RESERVE_VMEMMAP_SIZE,
+ free_vmemmap_pages_size_per_hpage(h));
+}
+
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 6cf2fdfb81e9..01228065b95d 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)
@@ -207,6 +213,71 @@ void vmemmap_remap_free(unsigned long start, unsigned long size)
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;
+ int nid = page_to_nid((const void *)start);
+
+ for (addr = start; addr < end; addr += PAGE_SIZE) {
+ struct page *page;
+
+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, start + size) to the page respectively
+ * which from the @vmemmap_pages
+ * @start: start address of the vmemmap virtual address range
+ * @size: size of the vmemmap virtual address range
+ */
+void vmemmap_remap_alloc(unsigned long start, unsigned long size)
+{
+ LIST_HEAD(vmemmap_pages);
+ unsigned long end = start + size;
+
+ struct vmemmap_remap_walk walk = {
+ .remap_pte = vmemmap_restore_pte,
+ .vmemmap_pages = &vmemmap_pages,
+ };
+
+ might_sleep();
+
+ alloc_vmemmap_page_list(&vmemmap_pages, start, end);
+ vmemmap_remap_range(start, 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
next prev parent reply other threads:[~2020-12-17 12:16 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-17 12:12 [PATCH v10 00/11] Free some vmemmap pages of HugeTLB page Muchun Song
2020-12-17 12:12 ` [PATCH v10 01/11] mm/memory_hotplug: Factor out bootmem core functions to bootmem_info.c Muchun Song
2020-12-17 12:12 ` [PATCH v10 02/11] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP Muchun Song
2020-12-18 15:41 ` Oscar Salvador
2020-12-21 23:56 ` Mike Kravetz
2020-12-17 12:12 ` [PATCH v10 03/11] mm/hugetlb: Free the vmemmap pages associated with each HugeTLB page Muchun Song
2020-12-21 9:11 ` Oscar Salvador
2020-12-21 11:25 ` [External] " Muchun Song
2020-12-21 13:43 ` Oscar Salvador
2020-12-21 15:52 ` Muchun Song
2020-12-21 18:00 ` Oscar Salvador
2020-12-22 1:03 ` Mike Kravetz
2020-12-22 2:49 ` Muchun Song
2020-12-17 12:12 ` [PATCH v10 04/11] mm/hugetlb: Defer freeing of HugeTLB pages Muchun Song
2020-12-21 10:27 ` Oscar Salvador
2020-12-21 11:07 ` [External] " Muchun Song
2020-12-21 14:14 ` Oscar Salvador
2020-12-21 15:18 ` Muchun Song
2020-12-17 12:12 ` Muchun Song [this message]
2020-12-17 12:12 ` [PATCH v10 06/11] mm/hugetlb: Set the PageHWPoison to the raw error page Muchun Song
2020-12-17 12:12 ` [PATCH v10 07/11] mm/hugetlb: Flush work when dissolving hugetlb page Muchun Song
2020-12-21 10:40 ` Oscar Salvador
2020-12-21 11:07 ` [External] " Muchun Song
2020-12-17 12:13 ` [PATCH v10 08/11] mm/hugetlb: Add a kernel parameter hugetlb_free_vmemmap Muchun Song
2020-12-17 12:13 ` [PATCH v10 09/11] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate Muchun Song
2020-12-21 8:16 ` Oscar Salvador
2020-12-21 9:33 ` [External] " Muchun Song
2020-12-17 12:13 ` [PATCH v10 10/11] mm/hugetlb: Gather discrete indexes of tail page Muchun Song
2020-12-18 9:06 ` Oscar Salvador
2020-12-18 9:41 ` [External] " Muchun Song
2020-12-17 12:13 ` [PATCH v10 11/11] mm/hugetlb: Optimize the code with the help of the compiler Muchun Song
2020-12-17 12:17 ` [PATCH v10 00/11] Free some vmemmap pages of HugeTLB page David Hildenbrand
2020-12-17 14:59 ` Oscar Salvador
2020-12-17 15:52 ` [External] " Muchun Song
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201217121303.13386-6-songmuchun@bytedance.com \
--to=songmuchun@bytedance.com \
--cc=akpm@linux-foundation.org \
--cc=almasrymina@google.com \
--cc=anshuman.khandual@arm.com \
--cc=bp@alien8.de \
--cc=corbet@lwn.net \
--cc=dave.hansen@linux.intel.com \
--cc=david@redhat.com \
--cc=duanxiongchun@bytedance.com \
--cc=hpa@zytor.com \
--cc=jroedel@suse.de \
--cc=linux-doc@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=luto@kernel.org \
--cc=mchehab+huawei@kernel.org \
--cc=mhocko@suse.com \
--cc=mike.kravetz@oracle.com \
--cc=mingo@redhat.com \
--cc=naoya.horiguchi@nec.com \
--cc=oneukum@suse.com \
--cc=osalvador@suse.de \
--cc=paulmck@kernel.org \
--cc=pawan.kumar.gupta@linux.intel.com \
--cc=peterz@infradead.org \
--cc=rdunlap@infradead.org \
--cc=rientjes@google.com \
--cc=song.bao.hua@hisilicon.com \
--cc=tglx@linutronix.de \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).