xref: /openbmc/linux/mm/hugetlb_vmemmap.c (revision 3ce2c24c)
1f41f2ed4SMuchun Song // SPDX-License-Identifier: GPL-2.0
2f41f2ed4SMuchun Song /*
3dff03381SMuchun Song  * HugeTLB Vmemmap Optimization (HVO)
4f41f2ed4SMuchun Song  *
5dff03381SMuchun Song  * Copyright (c) 2020, ByteDance. All rights reserved.
6f41f2ed4SMuchun Song  *
7f41f2ed4SMuchun Song  *     Author: Muchun Song <songmuchun@bytedance.com>
8f41f2ed4SMuchun Song  *
9ee65728eSMike Rapoport  * See Documentation/mm/vmemmap_dedup.rst
10f41f2ed4SMuchun Song  */
11e9fdff87SMuchun Song #define pr_fmt(fmt)	"HugeTLB: " fmt
12e9fdff87SMuchun Song 
13998a2997SMuchun Song #include <linux/pgtable.h>
14db5e8d84SVasily Gorbik #include <linux/moduleparam.h>
15998a2997SMuchun Song #include <linux/bootmem_info.h>
16998a2997SMuchun Song #include <asm/pgalloc.h>
17998a2997SMuchun Song #include <asm/tlbflush.h>
18f41f2ed4SMuchun Song #include "hugetlb_vmemmap.h"
19f41f2ed4SMuchun Song 
20998a2997SMuchun Song /**
21998a2997SMuchun Song  * struct vmemmap_remap_walk - walk vmemmap page table
22998a2997SMuchun Song  *
23998a2997SMuchun Song  * @remap_pte:		called for each lowest-level entry (PTE).
24998a2997SMuchun Song  * @nr_walked:		the number of walked pte.
25998a2997SMuchun Song  * @reuse_page:		the page which is reused for the tail vmemmap pages.
26998a2997SMuchun Song  * @reuse_addr:		the virtual address of the @reuse_page page.
27998a2997SMuchun Song  * @vmemmap_pages:	the list head of the vmemmap pages that can be freed
28998a2997SMuchun Song  *			or is mapped from.
29998a2997SMuchun Song  */
30998a2997SMuchun Song struct vmemmap_remap_walk {
31998a2997SMuchun Song 	void			(*remap_pte)(pte_t *pte, unsigned long addr,
32998a2997SMuchun Song 					     struct vmemmap_remap_walk *walk);
33998a2997SMuchun Song 	unsigned long		nr_walked;
34998a2997SMuchun Song 	struct page		*reuse_page;
35998a2997SMuchun Song 	unsigned long		reuse_addr;
36998a2997SMuchun Song 	struct list_head	*vmemmap_pages;
37998a2997SMuchun Song };
38998a2997SMuchun Song 
split_vmemmap_huge_pmd(pmd_t * pmd,unsigned long start)39*3ce2c24cSMuchun Song static int split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start)
40998a2997SMuchun Song {
41998a2997SMuchun Song 	pmd_t __pmd;
42998a2997SMuchun Song 	int i;
43998a2997SMuchun Song 	unsigned long addr = start;
44*3ce2c24cSMuchun Song 	struct page *head;
45*3ce2c24cSMuchun Song 	pte_t *pgtable;
46998a2997SMuchun Song 
47*3ce2c24cSMuchun Song 	spin_lock(&init_mm.page_table_lock);
48*3ce2c24cSMuchun Song 	head = pmd_leaf(*pmd) ? pmd_page(*pmd) : NULL;
49*3ce2c24cSMuchun Song 	spin_unlock(&init_mm.page_table_lock);
50*3ce2c24cSMuchun Song 
51*3ce2c24cSMuchun Song 	if (!head)
52*3ce2c24cSMuchun Song 		return 0;
53*3ce2c24cSMuchun Song 
54*3ce2c24cSMuchun Song 	pgtable = pte_alloc_one_kernel(&init_mm);
55998a2997SMuchun Song 	if (!pgtable)
56998a2997SMuchun Song 		return -ENOMEM;
57998a2997SMuchun Song 
58998a2997SMuchun Song 	pmd_populate_kernel(&init_mm, &__pmd, pgtable);
59998a2997SMuchun Song 
60e38f055dSMuchun Song 	for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
61998a2997SMuchun Song 		pte_t entry, *pte;
62998a2997SMuchun Song 		pgprot_t pgprot = PAGE_KERNEL;
63998a2997SMuchun Song 
64*3ce2c24cSMuchun Song 		entry = mk_pte(head + i, pgprot);
65998a2997SMuchun Song 		pte = pte_offset_kernel(&__pmd, addr);
66998a2997SMuchun Song 		set_pte_at(&init_mm, addr, pte, entry);
67998a2997SMuchun Song 	}
68998a2997SMuchun Song 
69998a2997SMuchun Song 	spin_lock(&init_mm.page_table_lock);
70998a2997SMuchun Song 	if (likely(pmd_leaf(*pmd))) {
71998a2997SMuchun Song 		/*
72998a2997SMuchun Song 		 * Higher order allocations from buddy allocator must be able to
73998a2997SMuchun Song 		 * be treated as indepdenent small pages (as they can be freed
74998a2997SMuchun Song 		 * individually).
75998a2997SMuchun Song 		 */
76*3ce2c24cSMuchun Song 		if (!PageReserved(head))
77*3ce2c24cSMuchun Song 			split_page(head, get_order(PMD_SIZE));
78998a2997SMuchun Song 
79998a2997SMuchun Song 		/* Make pte visible before pmd. See comment in pmd_install(). */
80998a2997SMuchun Song 		smp_wmb();
81998a2997SMuchun Song 		pmd_populate_kernel(&init_mm, pmd, pgtable);
82998a2997SMuchun Song 		flush_tlb_kernel_range(start, start + PMD_SIZE);
83998a2997SMuchun Song 	} else {
84998a2997SMuchun Song 		pte_free_kernel(&init_mm, pgtable);
85998a2997SMuchun Song 	}
86998a2997SMuchun Song 	spin_unlock(&init_mm.page_table_lock);
87998a2997SMuchun Song 
88998a2997SMuchun Song 	return 0;
89998a2997SMuchun Song }
90998a2997SMuchun Song 
vmemmap_pte_range(pmd_t * pmd,unsigned long addr,unsigned long end,struct vmemmap_remap_walk * walk)91998a2997SMuchun Song static void vmemmap_pte_range(pmd_t *pmd, unsigned long addr,
92998a2997SMuchun Song 			      unsigned long end,
93998a2997SMuchun Song 			      struct vmemmap_remap_walk *walk)
94998a2997SMuchun Song {
95998a2997SMuchun Song 	pte_t *pte = pte_offset_kernel(pmd, addr);
96998a2997SMuchun Song 
97998a2997SMuchun Song 	/*
98998a2997SMuchun Song 	 * The reuse_page is found 'first' in table walk before we start
99998a2997SMuchun Song 	 * remapping (which is calling @walk->remap_pte).
100998a2997SMuchun Song 	 */
101998a2997SMuchun Song 	if (!walk->reuse_page) {
102c33c7948SRyan Roberts 		walk->reuse_page = pte_page(ptep_get(pte));
103998a2997SMuchun Song 		/*
104998a2997SMuchun Song 		 * Because the reuse address is part of the range that we are
105998a2997SMuchun Song 		 * walking, skip the reuse address range.
106998a2997SMuchun Song 		 */
107998a2997SMuchun Song 		addr += PAGE_SIZE;
108998a2997SMuchun Song 		pte++;
109998a2997SMuchun Song 		walk->nr_walked++;
110998a2997SMuchun Song 	}
111998a2997SMuchun Song 
112998a2997SMuchun Song 	for (; addr != end; addr += PAGE_SIZE, pte++) {
113998a2997SMuchun Song 		walk->remap_pte(pte, addr, walk);
114998a2997SMuchun Song 		walk->nr_walked++;
115998a2997SMuchun Song 	}
116998a2997SMuchun Song }
117998a2997SMuchun Song 
vmemmap_pmd_range(pud_t * pud,unsigned long addr,unsigned long end,struct vmemmap_remap_walk * walk)118998a2997SMuchun Song static int vmemmap_pmd_range(pud_t *pud, unsigned long addr,
119998a2997SMuchun Song 			     unsigned long end,
120998a2997SMuchun Song 			     struct vmemmap_remap_walk *walk)
121998a2997SMuchun Song {
122998a2997SMuchun Song 	pmd_t *pmd;
123998a2997SMuchun Song 	unsigned long next;
124998a2997SMuchun Song 
125998a2997SMuchun Song 	pmd = pmd_offset(pud, addr);
126998a2997SMuchun Song 	do {
127998a2997SMuchun Song 		int ret;
128998a2997SMuchun Song 
129998a2997SMuchun Song 		ret = split_vmemmap_huge_pmd(pmd, addr & PMD_MASK);
130998a2997SMuchun Song 		if (ret)
131998a2997SMuchun Song 			return ret;
132998a2997SMuchun Song 
133998a2997SMuchun Song 		next = pmd_addr_end(addr, end);
134998a2997SMuchun Song 		vmemmap_pte_range(pmd, addr, next, walk);
135998a2997SMuchun Song 	} while (pmd++, addr = next, addr != end);
136998a2997SMuchun Song 
137998a2997SMuchun Song 	return 0;
138998a2997SMuchun Song }
139998a2997SMuchun Song 
vmemmap_pud_range(p4d_t * p4d,unsigned long addr,unsigned long end,struct vmemmap_remap_walk * walk)140998a2997SMuchun Song static int vmemmap_pud_range(p4d_t *p4d, unsigned long addr,
141998a2997SMuchun Song 			     unsigned long end,
142998a2997SMuchun Song 			     struct vmemmap_remap_walk *walk)
143998a2997SMuchun Song {
144998a2997SMuchun Song 	pud_t *pud;
145998a2997SMuchun Song 	unsigned long next;
146998a2997SMuchun Song 
147998a2997SMuchun Song 	pud = pud_offset(p4d, addr);
148998a2997SMuchun Song 	do {
149998a2997SMuchun Song 		int ret;
150998a2997SMuchun Song 
151998a2997SMuchun Song 		next = pud_addr_end(addr, end);
152998a2997SMuchun Song 		ret = vmemmap_pmd_range(pud, addr, next, walk);
153998a2997SMuchun Song 		if (ret)
154998a2997SMuchun Song 			return ret;
155998a2997SMuchun Song 	} while (pud++, addr = next, addr != end);
156998a2997SMuchun Song 
157998a2997SMuchun Song 	return 0;
158998a2997SMuchun Song }
159998a2997SMuchun Song 
vmemmap_p4d_range(pgd_t * pgd,unsigned long addr,unsigned long end,struct vmemmap_remap_walk * walk)160998a2997SMuchun Song static int vmemmap_p4d_range(pgd_t *pgd, unsigned long addr,
161998a2997SMuchun Song 			     unsigned long end,
162998a2997SMuchun Song 			     struct vmemmap_remap_walk *walk)
163998a2997SMuchun Song {
164998a2997SMuchun Song 	p4d_t *p4d;
165998a2997SMuchun Song 	unsigned long next;
166998a2997SMuchun Song 
167998a2997SMuchun Song 	p4d = p4d_offset(pgd, addr);
168998a2997SMuchun Song 	do {
169998a2997SMuchun Song 		int ret;
170998a2997SMuchun Song 
171998a2997SMuchun Song 		next = p4d_addr_end(addr, end);
172998a2997SMuchun Song 		ret = vmemmap_pud_range(p4d, addr, next, walk);
173998a2997SMuchun Song 		if (ret)
174998a2997SMuchun Song 			return ret;
175998a2997SMuchun Song 	} while (p4d++, addr = next, addr != end);
176998a2997SMuchun Song 
177998a2997SMuchun Song 	return 0;
178998a2997SMuchun Song }
179998a2997SMuchun Song 
vmemmap_remap_range(unsigned long start,unsigned long end,struct vmemmap_remap_walk * walk)180998a2997SMuchun Song static int vmemmap_remap_range(unsigned long start, unsigned long end,
181998a2997SMuchun Song 			       struct vmemmap_remap_walk *walk)
182998a2997SMuchun Song {
183998a2997SMuchun Song 	unsigned long addr = start;
184998a2997SMuchun Song 	unsigned long next;
185998a2997SMuchun Song 	pgd_t *pgd;
186998a2997SMuchun Song 
187998a2997SMuchun Song 	VM_BUG_ON(!PAGE_ALIGNED(start));
188998a2997SMuchun Song 	VM_BUG_ON(!PAGE_ALIGNED(end));
189998a2997SMuchun Song 
190998a2997SMuchun Song 	pgd = pgd_offset_k(addr);
191998a2997SMuchun Song 	do {
192998a2997SMuchun Song 		int ret;
193998a2997SMuchun Song 
194998a2997SMuchun Song 		next = pgd_addr_end(addr, end);
195998a2997SMuchun Song 		ret = vmemmap_p4d_range(pgd, addr, next, walk);
196998a2997SMuchun Song 		if (ret)
197998a2997SMuchun Song 			return ret;
198998a2997SMuchun Song 	} while (pgd++, addr = next, addr != end);
199998a2997SMuchun Song 
20011aad263SJoao Martins 	flush_tlb_kernel_range(start, end);
201998a2997SMuchun Song 
202998a2997SMuchun Song 	return 0;
203998a2997SMuchun Song }
204998a2997SMuchun Song 
205998a2997SMuchun Song /*
206998a2997SMuchun Song  * Free a vmemmap page. A vmemmap page can be allocated from the memblock
207998a2997SMuchun Song  * allocator or buddy allocator. If the PG_reserved flag is set, it means
208998a2997SMuchun Song  * that it allocated from the memblock allocator, just free it via the
209998a2997SMuchun Song  * free_bootmem_page(). Otherwise, use __free_page().
210998a2997SMuchun Song  */
free_vmemmap_page(struct page * page)211998a2997SMuchun Song static inline void free_vmemmap_page(struct page *page)
212998a2997SMuchun Song {
213998a2997SMuchun Song 	if (PageReserved(page))
214998a2997SMuchun Song 		free_bootmem_page(page);
215998a2997SMuchun Song 	else
216998a2997SMuchun Song 		__free_page(page);
217998a2997SMuchun Song }
218998a2997SMuchun Song 
219998a2997SMuchun Song /* Free a list of the vmemmap pages */
free_vmemmap_page_list(struct list_head * list)220998a2997SMuchun Song static void free_vmemmap_page_list(struct list_head *list)
221998a2997SMuchun Song {
222998a2997SMuchun Song 	struct page *page, *next;
223998a2997SMuchun Song 
2241cc53a04SMuchun Song 	list_for_each_entry_safe(page, next, list, lru)
225998a2997SMuchun Song 		free_vmemmap_page(page);
226998a2997SMuchun Song }
227998a2997SMuchun Song 
vmemmap_remap_pte(pte_t * pte,unsigned long addr,struct vmemmap_remap_walk * walk)228998a2997SMuchun Song static void vmemmap_remap_pte(pte_t *pte, unsigned long addr,
229998a2997SMuchun Song 			      struct vmemmap_remap_walk *walk)
230998a2997SMuchun Song {
231998a2997SMuchun Song 	/*
232998a2997SMuchun Song 	 * Remap the tail pages as read-only to catch illegal write operation
233998a2997SMuchun Song 	 * to the tail pages.
234998a2997SMuchun Song 	 */
235998a2997SMuchun Song 	pgprot_t pgprot = PAGE_KERNEL_RO;
236c33c7948SRyan Roberts 	struct page *page = pte_page(ptep_get(pte));
23711aad263SJoao Martins 	pte_t entry;
238998a2997SMuchun Song 
23911aad263SJoao Martins 	/* Remapping the head page requires r/w */
24011aad263SJoao Martins 	if (unlikely(addr == walk->reuse_addr)) {
24111aad263SJoao Martins 		pgprot = PAGE_KERNEL;
24211aad263SJoao Martins 		list_del(&walk->reuse_page->lru);
24311aad263SJoao Martins 
24411aad263SJoao Martins 		/*
24511aad263SJoao Martins 		 * Makes sure that preceding stores to the page contents from
24611aad263SJoao Martins 		 * vmemmap_remap_free() become visible before the set_pte_at()
24711aad263SJoao Martins 		 * write.
24811aad263SJoao Martins 		 */
24911aad263SJoao Martins 		smp_wmb();
25011aad263SJoao Martins 	}
25111aad263SJoao Martins 
25211aad263SJoao Martins 	entry = mk_pte(walk->reuse_page, pgprot);
253998a2997SMuchun Song 	list_add_tail(&page->lru, walk->vmemmap_pages);
254998a2997SMuchun Song 	set_pte_at(&init_mm, addr, pte, entry);
255998a2997SMuchun Song }
256998a2997SMuchun Song 
257998a2997SMuchun Song /*
258998a2997SMuchun Song  * How many struct page structs need to be reset. When we reuse the head
259998a2997SMuchun Song  * struct page, the special metadata (e.g. page->flags or page->mapping)
260998a2997SMuchun Song  * cannot copy to the tail struct page structs. The invalid value will be
2618666925cSVlastimil Babka  * checked in the free_tail_page_prepare(). In order to avoid the message
262998a2997SMuchun Song  * of "corrupted mapping in tail page". We need to reset at least 3 (one
263998a2997SMuchun Song  * head struct page struct and two tail struct page structs) struct page
264998a2997SMuchun Song  * structs.
265998a2997SMuchun Song  */
266998a2997SMuchun Song #define NR_RESET_STRUCT_PAGE		3
267998a2997SMuchun Song 
reset_struct_pages(struct page * start)268998a2997SMuchun Song static inline void reset_struct_pages(struct page *start)
269998a2997SMuchun Song {
270998a2997SMuchun Song 	struct page *from = start + NR_RESET_STRUCT_PAGE;
271998a2997SMuchun Song 
27233febb51SMuchun Song 	BUILD_BUG_ON(NR_RESET_STRUCT_PAGE * 2 > PAGE_SIZE / sizeof(struct page));
27333febb51SMuchun Song 	memcpy(start, from, sizeof(*from) * NR_RESET_STRUCT_PAGE);
274998a2997SMuchun Song }
275998a2997SMuchun Song 
vmemmap_restore_pte(pte_t * pte,unsigned long addr,struct vmemmap_remap_walk * walk)276998a2997SMuchun Song static void vmemmap_restore_pte(pte_t *pte, unsigned long addr,
277998a2997SMuchun Song 				struct vmemmap_remap_walk *walk)
278998a2997SMuchun Song {
279998a2997SMuchun Song 	pgprot_t pgprot = PAGE_KERNEL;
280998a2997SMuchun Song 	struct page *page;
281998a2997SMuchun Song 	void *to;
282998a2997SMuchun Song 
283c33c7948SRyan Roberts 	BUG_ON(pte_page(ptep_get(pte)) != walk->reuse_page);
284998a2997SMuchun Song 
285998a2997SMuchun Song 	page = list_first_entry(walk->vmemmap_pages, struct page, lru);
286998a2997SMuchun Song 	list_del(&page->lru);
287998a2997SMuchun Song 	to = page_to_virt(page);
288998a2997SMuchun Song 	copy_page(to, (void *)walk->reuse_addr);
289998a2997SMuchun Song 	reset_struct_pages(to);
290998a2997SMuchun Song 
291939de63dSMiaohe Lin 	/*
292939de63dSMiaohe Lin 	 * Makes sure that preceding stores to the page contents become visible
293939de63dSMiaohe Lin 	 * before the set_pte_at() write.
294939de63dSMiaohe Lin 	 */
295939de63dSMiaohe Lin 	smp_wmb();
296998a2997SMuchun Song 	set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot));
297998a2997SMuchun Song }
298998a2997SMuchun Song 
299998a2997SMuchun Song /**
300998a2997SMuchun Song  * vmemmap_remap_free - remap the vmemmap virtual address range [@start, @end)
301998a2997SMuchun Song  *			to the page which @reuse is mapped to, then free vmemmap
302998a2997SMuchun Song  *			which the range are mapped to.
303998a2997SMuchun Song  * @start:	start address of the vmemmap virtual address range that we want
304998a2997SMuchun Song  *		to remap.
305998a2997SMuchun Song  * @end:	end address of the vmemmap virtual address range that we want to
306998a2997SMuchun Song  *		remap.
307998a2997SMuchun Song  * @reuse:	reuse address.
308998a2997SMuchun Song  *
309998a2997SMuchun Song  * Return: %0 on success, negative error code otherwise.
310998a2997SMuchun Song  */
vmemmap_remap_free(unsigned long start,unsigned long end,unsigned long reuse)311998a2997SMuchun Song static int vmemmap_remap_free(unsigned long start, unsigned long end,
312998a2997SMuchun Song 			      unsigned long reuse)
313998a2997SMuchun Song {
314998a2997SMuchun Song 	int ret;
315998a2997SMuchun Song 	LIST_HEAD(vmemmap_pages);
316998a2997SMuchun Song 	struct vmemmap_remap_walk walk = {
317998a2997SMuchun Song 		.remap_pte	= vmemmap_remap_pte,
318998a2997SMuchun Song 		.reuse_addr	= reuse,
319998a2997SMuchun Song 		.vmemmap_pages	= &vmemmap_pages,
320998a2997SMuchun Song 	};
32111aad263SJoao Martins 	int nid = page_to_nid((struct page *)start);
32211aad263SJoao Martins 	gfp_t gfp_mask = GFP_KERNEL | __GFP_THISNODE | __GFP_NORETRY |
32311aad263SJoao Martins 			__GFP_NOWARN;
32411aad263SJoao Martins 
32511aad263SJoao Martins 	/*
32611aad263SJoao Martins 	 * Allocate a new head vmemmap page to avoid breaking a contiguous
32711aad263SJoao Martins 	 * block of struct page memory when freeing it back to page allocator
32811aad263SJoao Martins 	 * in free_vmemmap_page_list(). This will allow the likely contiguous
32911aad263SJoao Martins 	 * struct page backing memory to be kept contiguous and allowing for
33011aad263SJoao Martins 	 * more allocations of hugepages. Fallback to the currently
33111aad263SJoao Martins 	 * mapped head page in case should it fail to allocate.
33211aad263SJoao Martins 	 */
33311aad263SJoao Martins 	walk.reuse_page = alloc_pages_node(nid, gfp_mask, 0);
33411aad263SJoao Martins 	if (walk.reuse_page) {
33511aad263SJoao Martins 		copy_page(page_to_virt(walk.reuse_page),
33611aad263SJoao Martins 			  (void *)walk.reuse_addr);
33711aad263SJoao Martins 		list_add(&walk.reuse_page->lru, &vmemmap_pages);
33811aad263SJoao Martins 	}
339998a2997SMuchun Song 
340998a2997SMuchun Song 	/*
341998a2997SMuchun Song 	 * In order to make remapping routine most efficient for the huge pages,
342998a2997SMuchun Song 	 * the routine of vmemmap page table walking has the following rules
343998a2997SMuchun Song 	 * (see more details from the vmemmap_pte_range()):
344998a2997SMuchun Song 	 *
345998a2997SMuchun Song 	 * - The range [@start, @end) and the range [@reuse, @reuse + PAGE_SIZE)
346998a2997SMuchun Song 	 *   should be continuous.
347998a2997SMuchun Song 	 * - The @reuse address is part of the range [@reuse, @end) that we are
348998a2997SMuchun Song 	 *   walking which is passed to vmemmap_remap_range().
349998a2997SMuchun Song 	 * - The @reuse address is the first in the complete range.
350998a2997SMuchun Song 	 *
351998a2997SMuchun Song 	 * So we need to make sure that @start and @reuse meet the above rules.
352998a2997SMuchun Song 	 */
353998a2997SMuchun Song 	BUG_ON(start - reuse != PAGE_SIZE);
354998a2997SMuchun Song 
355998a2997SMuchun Song 	mmap_read_lock(&init_mm);
356998a2997SMuchun Song 	ret = vmemmap_remap_range(reuse, end, &walk);
357998a2997SMuchun Song 	if (ret && walk.nr_walked) {
358998a2997SMuchun Song 		end = reuse + walk.nr_walked * PAGE_SIZE;
359998a2997SMuchun Song 		/*
360998a2997SMuchun Song 		 * vmemmap_pages contains pages from the previous
361998a2997SMuchun Song 		 * vmemmap_remap_range call which failed.  These
362998a2997SMuchun Song 		 * are pages which were removed from the vmemmap.
363998a2997SMuchun Song 		 * They will be restored in the following call.
364998a2997SMuchun Song 		 */
365998a2997SMuchun Song 		walk = (struct vmemmap_remap_walk) {
366998a2997SMuchun Song 			.remap_pte	= vmemmap_restore_pte,
367998a2997SMuchun Song 			.reuse_addr	= reuse,
368998a2997SMuchun Song 			.vmemmap_pages	= &vmemmap_pages,
369998a2997SMuchun Song 		};
370998a2997SMuchun Song 
371998a2997SMuchun Song 		vmemmap_remap_range(reuse, end, &walk);
372998a2997SMuchun Song 	}
373998a2997SMuchun Song 	mmap_read_unlock(&init_mm);
374998a2997SMuchun Song 
375998a2997SMuchun Song 	free_vmemmap_page_list(&vmemmap_pages);
376998a2997SMuchun Song 
377998a2997SMuchun Song 	return ret;
378998a2997SMuchun Song }
379998a2997SMuchun Song 
alloc_vmemmap_page_list(unsigned long start,unsigned long end,struct list_head * list)380998a2997SMuchun Song static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
381eb83f652SPasha Tatashin 				   struct list_head *list)
382998a2997SMuchun Song {
383eb83f652SPasha Tatashin 	gfp_t gfp_mask = GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_THISNODE;
384998a2997SMuchun Song 	unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
385998a2997SMuchun Song 	int nid = page_to_nid((struct page *)start);
386998a2997SMuchun Song 	struct page *page, *next;
387998a2997SMuchun Song 
388998a2997SMuchun Song 	while (nr_pages--) {
389998a2997SMuchun Song 		page = alloc_pages_node(nid, gfp_mask, 0);
390998a2997SMuchun Song 		if (!page)
391998a2997SMuchun Song 			goto out;
392998a2997SMuchun Song 		list_add_tail(&page->lru, list);
393998a2997SMuchun Song 	}
394998a2997SMuchun Song 
395998a2997SMuchun Song 	return 0;
396998a2997SMuchun Song out:
397998a2997SMuchun Song 	list_for_each_entry_safe(page, next, list, lru)
398dcc1be11SLorenzo Stoakes 		__free_page(page);
399998a2997SMuchun Song 	return -ENOMEM;
400998a2997SMuchun Song }
401998a2997SMuchun Song 
402998a2997SMuchun Song /**
403998a2997SMuchun Song  * vmemmap_remap_alloc - remap the vmemmap virtual address range [@start, end)
404998a2997SMuchun Song  *			 to the page which is from the @vmemmap_pages
405998a2997SMuchun Song  *			 respectively.
406998a2997SMuchun Song  * @start:	start address of the vmemmap virtual address range that we want
407998a2997SMuchun Song  *		to remap.
408998a2997SMuchun Song  * @end:	end address of the vmemmap virtual address range that we want to
409998a2997SMuchun Song  *		remap.
410998a2997SMuchun Song  * @reuse:	reuse address.
411998a2997SMuchun Song  *
412998a2997SMuchun Song  * Return: %0 on success, negative error code otherwise.
413998a2997SMuchun Song  */
vmemmap_remap_alloc(unsigned long start,unsigned long end,unsigned long reuse)414998a2997SMuchun Song static int vmemmap_remap_alloc(unsigned long start, unsigned long end,
415eb83f652SPasha Tatashin 			       unsigned long reuse)
416998a2997SMuchun Song {
417998a2997SMuchun Song 	LIST_HEAD(vmemmap_pages);
418998a2997SMuchun Song 	struct vmemmap_remap_walk walk = {
419998a2997SMuchun Song 		.remap_pte	= vmemmap_restore_pte,
420998a2997SMuchun Song 		.reuse_addr	= reuse,
421998a2997SMuchun Song 		.vmemmap_pages	= &vmemmap_pages,
422998a2997SMuchun Song 	};
423998a2997SMuchun Song 
424998a2997SMuchun Song 	/* See the comment in the vmemmap_remap_free(). */
425998a2997SMuchun Song 	BUG_ON(start - reuse != PAGE_SIZE);
426998a2997SMuchun Song 
427eb83f652SPasha Tatashin 	if (alloc_vmemmap_page_list(start, end, &vmemmap_pages))
428998a2997SMuchun Song 		return -ENOMEM;
429998a2997SMuchun Song 
430998a2997SMuchun Song 	mmap_read_lock(&init_mm);
431998a2997SMuchun Song 	vmemmap_remap_range(reuse, end, &walk);
432998a2997SMuchun Song 	mmap_read_unlock(&init_mm);
433998a2997SMuchun Song 
434998a2997SMuchun Song 	return 0;
435998a2997SMuchun Song }
436998a2997SMuchun Song 
437cf5472e5SMuchun Song DEFINE_STATIC_KEY_FALSE(hugetlb_optimize_vmemmap_key);
438f10f1442SMuchun Song EXPORT_SYMBOL(hugetlb_optimize_vmemmap_key);
439e9fdff87SMuchun Song 
44030152245SMuchun Song static bool vmemmap_optimize_enabled = IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON);
44130152245SMuchun Song core_param(hugetlb_free_vmemmap, vmemmap_optimize_enabled, bool, 0);
442f41f2ed4SMuchun Song 
4436213834cSMuchun Song /**
4446213834cSMuchun Song  * hugetlb_vmemmap_restore - restore previously optimized (by
4456213834cSMuchun Song  *			     hugetlb_vmemmap_optimize()) vmemmap pages which
4466213834cSMuchun Song  *			     will be reallocated and remapped.
4476213834cSMuchun Song  * @h:		struct hstate.
4486213834cSMuchun Song  * @head:	the head page whose vmemmap pages will be restored.
4496213834cSMuchun Song  *
4506213834cSMuchun Song  * Return: %0 if @head's vmemmap pages have been reallocated and remapped,
4516213834cSMuchun Song  * negative error code otherwise.
452ad2fa371SMuchun Song  */
hugetlb_vmemmap_restore(const struct hstate * h,struct page * head)4536213834cSMuchun Song int hugetlb_vmemmap_restore(const struct hstate *h, struct page *head)
454ad2fa371SMuchun Song {
455ad2fa371SMuchun Song 	int ret;
4566213834cSMuchun Song 	unsigned long vmemmap_start = (unsigned long)head, vmemmap_end;
4576213834cSMuchun Song 	unsigned long vmemmap_reuse;
458ad2fa371SMuchun Song 
459ad2fa371SMuchun Song 	if (!HPageVmemmapOptimized(head))
460ad2fa371SMuchun Song 		return 0;
461ad2fa371SMuchun Song 
4626213834cSMuchun Song 	vmemmap_end	= vmemmap_start + hugetlb_vmemmap_size(h);
4636213834cSMuchun Song 	vmemmap_reuse	= vmemmap_start;
4646213834cSMuchun Song 	vmemmap_start	+= HUGETLB_VMEMMAP_RESERVE_SIZE;
4655981611dSMuchun Song 
466ad2fa371SMuchun Song 	/*
4676213834cSMuchun Song 	 * The pages which the vmemmap virtual address range [@vmemmap_start,
468ad2fa371SMuchun Song 	 * @vmemmap_end) are mapped to are freed to the buddy allocator, and
469ad2fa371SMuchun Song 	 * the range is mapped to the page which @vmemmap_reuse is mapped to.
470ad2fa371SMuchun Song 	 * When a HugeTLB page is freed to the buddy allocator, previously
471ad2fa371SMuchun Song 	 * discarded vmemmap pages must be allocated and remapping.
472ad2fa371SMuchun Song 	 */
473eb83f652SPasha Tatashin 	ret = vmemmap_remap_alloc(vmemmap_start, vmemmap_end, vmemmap_reuse);
47478f39084SMuchun Song 	if (!ret) {
475ad2fa371SMuchun Song 		ClearHPageVmemmapOptimized(head);
47678f39084SMuchun Song 		static_branch_dec(&hugetlb_optimize_vmemmap_key);
47778f39084SMuchun Song 	}
478ad2fa371SMuchun Song 
479ad2fa371SMuchun Song 	return ret;
480ad2fa371SMuchun Song }
481ad2fa371SMuchun Song 
4826213834cSMuchun Song /* Return true iff a HugeTLB whose vmemmap should and can be optimized. */
vmemmap_should_optimize(const struct hstate * h,const struct page * head)4836213834cSMuchun Song static bool vmemmap_should_optimize(const struct hstate *h, const struct page *head)
48466361095SMuchun Song {
485cf5472e5SMuchun Song 	if (!READ_ONCE(vmemmap_optimize_enabled))
4866213834cSMuchun Song 		return false;
4876213834cSMuchun Song 
4886213834cSMuchun Song 	if (!hugetlb_vmemmap_optimizable(h))
4896213834cSMuchun Song 		return false;
49066361095SMuchun Song 
49166361095SMuchun Song 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) {
49266361095SMuchun Song 		pmd_t *pmdp, pmd;
49366361095SMuchun Song 		struct page *vmemmap_page;
49466361095SMuchun Song 		unsigned long vaddr = (unsigned long)head;
49566361095SMuchun Song 
49666361095SMuchun Song 		/*
49766361095SMuchun Song 		 * Only the vmemmap page's vmemmap page can be self-hosted.
49866361095SMuchun Song 		 * Walking the page tables to find the backing page of the
49966361095SMuchun Song 		 * vmemmap page.
50066361095SMuchun Song 		 */
50166361095SMuchun Song 		pmdp = pmd_off_k(vaddr);
50266361095SMuchun Song 		/*
50366361095SMuchun Song 		 * The READ_ONCE() is used to stabilize *pmdp in a register or
50466361095SMuchun Song 		 * on the stack so that it will stop changing under the code.
50566361095SMuchun Song 		 * The only concurrent operation where it can be changed is
50666361095SMuchun Song 		 * split_vmemmap_huge_pmd() (*pmdp will be stable after this
50766361095SMuchun Song 		 * operation).
50866361095SMuchun Song 		 */
50966361095SMuchun Song 		pmd = READ_ONCE(*pmdp);
51066361095SMuchun Song 		if (pmd_leaf(pmd))
51166361095SMuchun Song 			vmemmap_page = pmd_page(pmd) + pte_index(vaddr);
51266361095SMuchun Song 		else
51366361095SMuchun Song 			vmemmap_page = pte_page(*pte_offset_kernel(pmdp, vaddr));
51466361095SMuchun Song 		/*
51566361095SMuchun Song 		 * Due to HugeTLB alignment requirements and the vmemmap pages
51666361095SMuchun Song 		 * being at the start of the hotplugged memory region in
51766361095SMuchun Song 		 * memory_hotplug.memmap_on_memory case. Checking any vmemmap
51866361095SMuchun Song 		 * page's vmemmap page if it is marked as VmemmapSelfHosted is
51966361095SMuchun Song 		 * sufficient.
52066361095SMuchun Song 		 *
52166361095SMuchun Song 		 * [                  hotplugged memory                  ]
52266361095SMuchun Song 		 * [        section        ][...][        section        ]
52366361095SMuchun Song 		 * [ vmemmap ][              usable memory               ]
52466361095SMuchun Song 		 *   ^   |     |                                        |
52566361095SMuchun Song 		 *   +---+     |                                        |
52666361095SMuchun Song 		 *     ^       |                                        |
52766361095SMuchun Song 		 *     +-------+                                        |
52866361095SMuchun Song 		 *          ^                                           |
52966361095SMuchun Song 		 *          +-------------------------------------------+
53066361095SMuchun Song 		 */
53166361095SMuchun Song 		if (PageVmemmapSelfHosted(vmemmap_page))
5326213834cSMuchun Song 			return false;
53366361095SMuchun Song 	}
53466361095SMuchun Song 
5356213834cSMuchun Song 	return true;
53666361095SMuchun Song }
53766361095SMuchun Song 
5386213834cSMuchun Song /**
5396213834cSMuchun Song  * hugetlb_vmemmap_optimize - optimize @head page's vmemmap pages.
5406213834cSMuchun Song  * @h:		struct hstate.
5416213834cSMuchun Song  * @head:	the head page whose vmemmap pages will be optimized.
5426213834cSMuchun Song  *
5436213834cSMuchun Song  * This function only tries to optimize @head's vmemmap pages and does not
5446213834cSMuchun Song  * guarantee that the optimization will succeed after it returns. The caller
5456213834cSMuchun Song  * can use HPageVmemmapOptimized(@head) to detect if @head's vmemmap pages
5466213834cSMuchun Song  * have been optimized.
5476213834cSMuchun Song  */
hugetlb_vmemmap_optimize(const struct hstate * h,struct page * head)5486213834cSMuchun Song void hugetlb_vmemmap_optimize(const struct hstate *h, struct page *head)
549f41f2ed4SMuchun Song {
5506213834cSMuchun Song 	unsigned long vmemmap_start = (unsigned long)head, vmemmap_end;
5516213834cSMuchun Song 	unsigned long vmemmap_reuse;
552f41f2ed4SMuchun Song 
5536213834cSMuchun Song 	if (!vmemmap_should_optimize(h, head))
554f41f2ed4SMuchun Song 		return;
555f41f2ed4SMuchun Song 
55678f39084SMuchun Song 	static_branch_inc(&hugetlb_optimize_vmemmap_key);
55778f39084SMuchun Song 
5586213834cSMuchun Song 	vmemmap_end	= vmemmap_start + hugetlb_vmemmap_size(h);
5596213834cSMuchun Song 	vmemmap_reuse	= vmemmap_start;
5606213834cSMuchun Song 	vmemmap_start	+= HUGETLB_VMEMMAP_RESERVE_SIZE;
561f41f2ed4SMuchun Song 
562f41f2ed4SMuchun Song 	/*
5636213834cSMuchun Song 	 * Remap the vmemmap virtual address range [@vmemmap_start, @vmemmap_end)
564f41f2ed4SMuchun Song 	 * to the page which @vmemmap_reuse is mapped to, then free the pages
5656213834cSMuchun Song 	 * which the range [@vmemmap_start, @vmemmap_end] is mapped to.
566f41f2ed4SMuchun Song 	 */
5676213834cSMuchun Song 	if (vmemmap_remap_free(vmemmap_start, vmemmap_end, vmemmap_reuse))
56878f39084SMuchun Song 		static_branch_dec(&hugetlb_optimize_vmemmap_key);
56978f39084SMuchun Song 	else
570ad2fa371SMuchun Song 		SetHPageVmemmapOptimized(head);
571f41f2ed4SMuchun Song }
57277490587SMuchun Song 
57378f39084SMuchun Song static struct ctl_table hugetlb_vmemmap_sysctls[] = {
57478f39084SMuchun Song 	{
57578f39084SMuchun Song 		.procname	= "hugetlb_optimize_vmemmap",
576cf5472e5SMuchun Song 		.data		= &vmemmap_optimize_enabled,
577f1aa2eb5SOndrej Mosnacek 		.maxlen		= sizeof(vmemmap_optimize_enabled),
57878f39084SMuchun Song 		.mode		= 0644,
579cf5472e5SMuchun Song 		.proc_handler	= proc_dobool,
58078f39084SMuchun Song 	},
58178f39084SMuchun Song 	{ }
58278f39084SMuchun Song };
58378f39084SMuchun Song 
hugetlb_vmemmap_init(void)5846213834cSMuchun Song static int __init hugetlb_vmemmap_init(void)
58578f39084SMuchun Song {
58612318566SMuchun Song 	const struct hstate *h;
58712318566SMuchun Song 
5886213834cSMuchun Song 	/* HUGETLB_VMEMMAP_RESERVE_SIZE should cover all used struct pages */
5896213834cSMuchun Song 	BUILD_BUG_ON(__NR_USED_SUBPAGE * sizeof(struct page) > HUGETLB_VMEMMAP_RESERVE_SIZE);
59078f39084SMuchun Song 
5916213834cSMuchun Song 	for_each_hstate(h) {
5926213834cSMuchun Song 		if (hugetlb_vmemmap_optimizable(h)) {
5936213834cSMuchun Song 			register_sysctl_init("vm", hugetlb_vmemmap_sysctls);
5946213834cSMuchun Song 			break;
5956213834cSMuchun Song 		}
5966213834cSMuchun Song 	}
59778f39084SMuchun Song 	return 0;
59878f39084SMuchun Song }
5996213834cSMuchun Song late_initcall(hugetlb_vmemmap_init);
600