Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Free some vmemmap pages of HugeTLB
4 *
5 * Copyright (c) 2020, Bytedance. All rights reserved.
6 *
7 * Author: Muchun Song <songmuchun@bytedance.com>
8 *
9 * The struct page structures (page structs) are used to describe a physical
10 * page frame. By default, there is a one-to-one mapping from a page frame to
11 * it's corresponding page struct.
12 *
13 * HugeTLB pages consist of multiple base page size pages and is supported by
14 * many architectures. See hugetlbpage.rst in the Documentation directory for
15 * more details. On the x86-64 architecture, HugeTLB pages of size 2MB and 1GB
16 * are currently supported. Since the base page size on x86 is 4KB, a 2MB
17 * HugeTLB page consists of 512 base pages and a 1GB HugeTLB page consists of
18 * 4096 base pages. For each base page, there is a corresponding page struct.
19 *
20 * Within the HugeTLB subsystem, only the first 4 page structs are used to
21 * contain unique information about a HugeTLB page. __NR_USED_SUBPAGE provides
22 * this upper limit. The only 'useful' information in the remaining page structs
23 * is the compound_head field, and this field is the same for all tail pages.
24 *
25 * By removing redundant page structs for HugeTLB pages, memory can be returned
26 * to the buddy allocator for other uses.
27 *
28 * Different architectures support different HugeTLB pages. For example, the
29 * following table is the HugeTLB page size supported by x86 and arm64
30 * architectures. Because arm64 supports 4k, 16k, and 64k base pages and
31 * supports contiguous entries, so it supports many kinds of sizes of HugeTLB
32 * page.
33 *
34 * +--------------+-----------+-----------------------------------------------+
35 * | Architecture | Page Size | HugeTLB Page Size |
36 * +--------------+-----------+-----------+-----------+-----------+-----------+
37 * | x86-64 | 4KB | 2MB | 1GB | | |
38 * +--------------+-----------+-----------+-----------+-----------+-----------+
39 * | | 4KB | 64KB | 2MB | 32MB | 1GB |
40 * | +-----------+-----------+-----------+-----------+-----------+
41 * | arm64 | 16KB | 2MB | 32MB | 1GB | |
42 * | +-----------+-----------+-----------+-----------+-----------+
43 * | | 64KB | 2MB | 512MB | 16GB | |
44 * +--------------+-----------+-----------+-----------+-----------+-----------+
45 *
46 * When the system boot up, every HugeTLB page has more than one struct page
47 * structs which size is (unit: pages):
48 *
49 * struct_size = HugeTLB_Size / PAGE_SIZE * sizeof(struct page) / PAGE_SIZE
50 *
51 * Where HugeTLB_Size is the size of the HugeTLB page. We know that the size
52 * of the HugeTLB page is always n times PAGE_SIZE. So we can get the following
53 * relationship.
54 *
55 * HugeTLB_Size = n * PAGE_SIZE
56 *
57 * Then,
58 *
59 * struct_size = n * PAGE_SIZE / PAGE_SIZE * sizeof(struct page) / PAGE_SIZE
60 * = n * sizeof(struct page) / PAGE_SIZE
61 *
62 * We can use huge mapping at the pud/pmd level for the HugeTLB page.
63 *
64 * For the HugeTLB page of the pmd level mapping, then
65 *
66 * struct_size = n * sizeof(struct page) / PAGE_SIZE
67 * = PAGE_SIZE / sizeof(pte_t) * sizeof(struct page) / PAGE_SIZE
68 * = sizeof(struct page) / sizeof(pte_t)
69 * = 64 / 8
70 * = 8 (pages)
71 *
72 * Where n is how many pte entries which one page can contains. So the value of
73 * n is (PAGE_SIZE / sizeof(pte_t)).
74 *
75 * This optimization only supports 64-bit system, so the value of sizeof(pte_t)
76 * is 8. And this optimization also applicable only when the size of struct page
77 * is a power of two. In most cases, the size of struct page is 64 bytes (e.g.
78 * x86-64 and arm64). So if we use pmd level mapping for a HugeTLB page, the
79 * size of struct page structs of it is 8 page frames which size depends on the
80 * size of the base page.
81 *
82 * For the HugeTLB page of the pud level mapping, then
83 *
84 * struct_size = PAGE_SIZE / sizeof(pmd_t) * struct_size(pmd)
85 * = PAGE_SIZE / 8 * 8 (pages)
86 * = PAGE_SIZE (pages)
87 *
88 * Where the struct_size(pmd) is the size of the struct page structs of a
89 * HugeTLB page of the pmd level mapping.
90 *
91 * E.g.: A 2MB HugeTLB page on x86_64 consists in 8 page frames while 1GB
92 * HugeTLB page consists in 4096.
93 *
94 * Next, we take the pmd level mapping of the HugeTLB page as an example to
95 * show the internal implementation of this optimization. There are 8 pages
96 * struct page structs associated with a HugeTLB page which is pmd mapped.
97 *
98 * Here is how things look before optimization.
99 *
100 * HugeTLB struct pages(8 pages) page frame(8 pages)
101 * +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+
102 * | | | 0 | -------------> | 0 |
103 * | | +-----------+ +-----------+
104 * | | | 1 | -------------> | 1 |
105 * | | +-----------+ +-----------+
106 * | | | 2 | -------------> | 2 |
107 * | | +-----------+ +-----------+
108 * | | | 3 | -------------> | 3 |
109 * | | +-----------+ +-----------+
110 * | | | 4 | -------------> | 4 |
111 * | PMD | +-----------+ +-----------+
112 * | level | | 5 | -------------> | 5 |
113 * | mapping | +-----------+ +-----------+
114 * | | | 6 | -------------> | 6 |
115 * | | +-----------+ +-----------+
116 * | | | 7 | -------------> | 7 |
117 * | | +-----------+ +-----------+
118 * | |
119 * | |
120 * | |
121 * +-----------+
122 *
123 * The value of page->compound_head is the same for all tail pages. The first
124 * page of page structs (page 0) associated with the HugeTLB page contains the 4
125 * page structs necessary to describe the HugeTLB. The only use of the remaining
126 * pages of page structs (page 1 to page 7) is to point to page->compound_head.
127 * Therefore, we can remap pages 2 to 7 to page 1. Only 2 pages of page structs
128 * will be used for each HugeTLB page. This will allow us to free the remaining
129 * 6 pages to the buddy allocator.
130 *
131 * Here is how things look after remapping.
132 *
133 * HugeTLB struct pages(8 pages) page frame(8 pages)
134 * +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+
135 * | | | 0 | -------------> | 0 |
136 * | | +-----------+ +-----------+
137 * | | | 1 | -------------> | 1 |
138 * | | +-----------+ +-----------+
139 * | | | 2 | ----------------^ ^ ^ ^ ^ ^
140 * | | +-----------+ | | | | |
141 * | | | 3 | ------------------+ | | | |
142 * | | +-----------+ | | | |
143 * | | | 4 | --------------------+ | | |
144 * | PMD | +-----------+ | | |
145 * | level | | 5 | ----------------------+ | |
146 * | mapping | +-----------+ | |
147 * | | | 6 | ------------------------+ |
148 * | | +-----------+ |
149 * | | | 7 | --------------------------+
150 * | | +-----------+
151 * | |
152 * | |
153 * | |
154 * +-----------+
155 *
156 * When a HugeTLB is freed to the buddy system, we should allocate 6 pages for
157 * vmemmap pages and restore the previous mapping relationship.
158 *
159 * For the HugeTLB page of the pud level mapping. It is similar to the former.
160 * We also can use this approach to free (PAGE_SIZE - 2) vmemmap pages.
161 *
162 * Apart from the HugeTLB page of the pmd/pud level mapping, some architectures
163 * (e.g. aarch64) provides a contiguous bit in the translation table entries
164 * that hints to the MMU to indicate that it is one of a contiguous set of
165 * entries that can be cached in a single TLB entry.
166 *
167 * The contiguous bit is used to increase the mapping size at the pmd and pte
168 * (last) level. So this type of HugeTLB page can be optimized only when its
169 * size of the struct page structs is greater than 2 pages.
170 */
171#define pr_fmt(fmt) "HugeTLB: " fmt
172
173#include "hugetlb_vmemmap.h"
174
175/*
176 * There are a lot of struct page structures associated with each HugeTLB page.
177 * For tail pages, the value of compound_head is the same. So we can reuse first
178 * page of tail page structures. We map the virtual addresses of the remaining
179 * pages of tail page structures to the first tail page struct, and then free
180 * these page frames. Therefore, we need to reserve two pages as vmemmap areas.
181 */
182#define RESERVE_VMEMMAP_NR 2U
183#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT)
184
185bool hugetlb_free_vmemmap_enabled = IS_ENABLED(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON);
186
187static int __init early_hugetlb_free_vmemmap_param(char *buf)
188{
189 /* We cannot optimize if a "struct page" crosses page boundaries. */
190 if ((!is_power_of_2(sizeof(struct page)))) {
191 pr_warn("cannot free vmemmap pages because \"struct page\" crosses page boundaries\n");
192 return 0;
193 }
194
195 if (!buf)
196 return -EINVAL;
197
198 if (!strcmp(buf, "on"))
199 hugetlb_free_vmemmap_enabled = true;
200 else if (!strcmp(buf, "off"))
201 hugetlb_free_vmemmap_enabled = false;
202 else
203 return -EINVAL;
204
205 return 0;
206}
207early_param("hugetlb_free_vmemmap", early_hugetlb_free_vmemmap_param);
208
209static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h)
210{
211 return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT;
212}
213
214/*
215 * Previously discarded vmemmap pages will be allocated and remapping
216 * after this function returns zero.
217 */
218int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
219{
220 int ret;
221 unsigned long vmemmap_addr = (unsigned long)head;
222 unsigned long vmemmap_end, vmemmap_reuse;
223
224 if (!HPageVmemmapOptimized(head))
225 return 0;
226
227 vmemmap_addr += RESERVE_VMEMMAP_SIZE;
228 vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
229 vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
230 /*
231 * The pages which the vmemmap virtual address range [@vmemmap_addr,
232 * @vmemmap_end) are mapped to are freed to the buddy allocator, and
233 * the range is mapped to the page which @vmemmap_reuse is mapped to.
234 * When a HugeTLB page is freed to the buddy allocator, previously
235 * discarded vmemmap pages must be allocated and remapping.
236 */
237 ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
238 GFP_KERNEL | __GFP_NORETRY | __GFP_THISNODE);
239
240 if (!ret)
241 ClearHPageVmemmapOptimized(head);
242
243 return ret;
244}
245
246void free_huge_page_vmemmap(struct hstate *h, struct page *head)
247{
248 unsigned long vmemmap_addr = (unsigned long)head;
249 unsigned long vmemmap_end, vmemmap_reuse;
250
251 if (!free_vmemmap_pages_per_hpage(h))
252 return;
253
254 vmemmap_addr += RESERVE_VMEMMAP_SIZE;
255 vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
256 vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
257
258 /*
259 * Remap the vmemmap virtual address range [@vmemmap_addr, @vmemmap_end)
260 * to the page which @vmemmap_reuse is mapped to, then free the pages
261 * which the range [@vmemmap_addr, @vmemmap_end] is mapped to.
262 */
263 if (!vmemmap_remap_free(vmemmap_addr, vmemmap_end, vmemmap_reuse))
264 SetHPageVmemmapOptimized(head);
265}
266
267void __init hugetlb_vmemmap_init(struct hstate *h)
268{
269 unsigned int nr_pages = pages_per_huge_page(h);
270 unsigned int vmemmap_pages;
271
272 /*
273 * There are only (RESERVE_VMEMMAP_SIZE / sizeof(struct page)) struct
274 * page structs that can be used when CONFIG_HUGETLB_PAGE_FREE_VMEMMAP,
275 * so add a BUILD_BUG_ON to catch invalid usage of the tail struct page.
276 */
277 BUILD_BUG_ON(__NR_USED_SUBPAGE >=
278 RESERVE_VMEMMAP_SIZE / sizeof(struct page));
279
280 if (!hugetlb_free_vmemmap_enabled)
281 return;
282
283 vmemmap_pages = (nr_pages * sizeof(struct page)) >> PAGE_SHIFT;
284 /*
285 * The head page and the first tail page are not to be freed to buddy
286 * allocator, the other pages will map to the first tail page, so they
287 * can be freed.
288 *
289 * Could RESERVE_VMEMMAP_NR be greater than @vmemmap_pages? It is true
290 * on some architectures (e.g. aarch64). See Documentation/arm64/
291 * hugetlbpage.rst for more details.
292 */
293 if (likely(vmemmap_pages > RESERVE_VMEMMAP_NR))
294 h->nr_free_vmemmap_pages = vmemmap_pages - RESERVE_VMEMMAP_NR;
295
296 pr_info("can free %d vmemmap pages for %s\n", h->nr_free_vmemmap_pages,
297 h->name);
298}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * HugeTLB Vmemmap Optimization (HVO)
4 *
5 * Copyright (c) 2020, ByteDance. All rights reserved.
6 *
7 * Author: Muchun Song <songmuchun@bytedance.com>
8 *
9 * See Documentation/mm/vmemmap_dedup.rst
10 */
11#define pr_fmt(fmt) "HugeTLB: " fmt
12
13#include <linux/pgtable.h>
14#include <linux/moduleparam.h>
15#include <linux/bootmem_info.h>
16#include <asm/pgalloc.h>
17#include <asm/tlbflush.h>
18#include "hugetlb_vmemmap.h"
19
20/**
21 * struct vmemmap_remap_walk - walk vmemmap page table
22 *
23 * @remap_pte: called for each lowest-level entry (PTE).
24 * @nr_walked: the number of walked pte.
25 * @reuse_page: the page which is reused for the tail vmemmap pages.
26 * @reuse_addr: the virtual address of the @reuse_page page.
27 * @vmemmap_pages: the list head of the vmemmap pages that can be freed
28 * or is mapped from.
29 */
30struct vmemmap_remap_walk {
31 void (*remap_pte)(pte_t *pte, unsigned long addr,
32 struct vmemmap_remap_walk *walk);
33 unsigned long nr_walked;
34 struct page *reuse_page;
35 unsigned long reuse_addr;
36 struct list_head *vmemmap_pages;
37};
38
39static int __split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start)
40{
41 pmd_t __pmd;
42 int i;
43 unsigned long addr = start;
44 struct page *page = pmd_page(*pmd);
45 pte_t *pgtable = pte_alloc_one_kernel(&init_mm);
46
47 if (!pgtable)
48 return -ENOMEM;
49
50 pmd_populate_kernel(&init_mm, &__pmd, pgtable);
51
52 for (i = 0; i < PTRS_PER_PTE; i++, addr += PAGE_SIZE) {
53 pte_t entry, *pte;
54 pgprot_t pgprot = PAGE_KERNEL;
55
56 entry = mk_pte(page + i, pgprot);
57 pte = pte_offset_kernel(&__pmd, addr);
58 set_pte_at(&init_mm, addr, pte, entry);
59 }
60
61 spin_lock(&init_mm.page_table_lock);
62 if (likely(pmd_leaf(*pmd))) {
63 /*
64 * Higher order allocations from buddy allocator must be able to
65 * be treated as indepdenent small pages (as they can be freed
66 * individually).
67 */
68 if (!PageReserved(page))
69 split_page(page, get_order(PMD_SIZE));
70
71 /* Make pte visible before pmd. See comment in pmd_install(). */
72 smp_wmb();
73 pmd_populate_kernel(&init_mm, pmd, pgtable);
74 flush_tlb_kernel_range(start, start + PMD_SIZE);
75 } else {
76 pte_free_kernel(&init_mm, pgtable);
77 }
78 spin_unlock(&init_mm.page_table_lock);
79
80 return 0;
81}
82
83static int split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start)
84{
85 int leaf;
86
87 spin_lock(&init_mm.page_table_lock);
88 leaf = pmd_leaf(*pmd);
89 spin_unlock(&init_mm.page_table_lock);
90
91 if (!leaf)
92 return 0;
93
94 return __split_vmemmap_huge_pmd(pmd, start);
95}
96
97static void vmemmap_pte_range(pmd_t *pmd, unsigned long addr,
98 unsigned long end,
99 struct vmemmap_remap_walk *walk)
100{
101 pte_t *pte = pte_offset_kernel(pmd, addr);
102
103 /*
104 * The reuse_page is found 'first' in table walk before we start
105 * remapping (which is calling @walk->remap_pte).
106 */
107 if (!walk->reuse_page) {
108 walk->reuse_page = pte_page(*pte);
109 /*
110 * Because the reuse address is part of the range that we are
111 * walking, skip the reuse address range.
112 */
113 addr += PAGE_SIZE;
114 pte++;
115 walk->nr_walked++;
116 }
117
118 for (; addr != end; addr += PAGE_SIZE, pte++) {
119 walk->remap_pte(pte, addr, walk);
120 walk->nr_walked++;
121 }
122}
123
124static int vmemmap_pmd_range(pud_t *pud, unsigned long addr,
125 unsigned long end,
126 struct vmemmap_remap_walk *walk)
127{
128 pmd_t *pmd;
129 unsigned long next;
130
131 pmd = pmd_offset(pud, addr);
132 do {
133 int ret;
134
135 ret = split_vmemmap_huge_pmd(pmd, addr & PMD_MASK);
136 if (ret)
137 return ret;
138
139 next = pmd_addr_end(addr, end);
140 vmemmap_pte_range(pmd, addr, next, walk);
141 } while (pmd++, addr = next, addr != end);
142
143 return 0;
144}
145
146static int vmemmap_pud_range(p4d_t *p4d, unsigned long addr,
147 unsigned long end,
148 struct vmemmap_remap_walk *walk)
149{
150 pud_t *pud;
151 unsigned long next;
152
153 pud = pud_offset(p4d, addr);
154 do {
155 int ret;
156
157 next = pud_addr_end(addr, end);
158 ret = vmemmap_pmd_range(pud, addr, next, walk);
159 if (ret)
160 return ret;
161 } while (pud++, addr = next, addr != end);
162
163 return 0;
164}
165
166static int vmemmap_p4d_range(pgd_t *pgd, unsigned long addr,
167 unsigned long end,
168 struct vmemmap_remap_walk *walk)
169{
170 p4d_t *p4d;
171 unsigned long next;
172
173 p4d = p4d_offset(pgd, addr);
174 do {
175 int ret;
176
177 next = p4d_addr_end(addr, end);
178 ret = vmemmap_pud_range(p4d, addr, next, walk);
179 if (ret)
180 return ret;
181 } while (p4d++, addr = next, addr != end);
182
183 return 0;
184}
185
186static int vmemmap_remap_range(unsigned long start, unsigned long end,
187 struct vmemmap_remap_walk *walk)
188{
189 unsigned long addr = start;
190 unsigned long next;
191 pgd_t *pgd;
192
193 VM_BUG_ON(!PAGE_ALIGNED(start));
194 VM_BUG_ON(!PAGE_ALIGNED(end));
195
196 pgd = pgd_offset_k(addr);
197 do {
198 int ret;
199
200 next = pgd_addr_end(addr, end);
201 ret = vmemmap_p4d_range(pgd, addr, next, walk);
202 if (ret)
203 return ret;
204 } while (pgd++, addr = next, addr != end);
205
206 flush_tlb_kernel_range(start, end);
207
208 return 0;
209}
210
211/*
212 * Free a vmemmap page. A vmemmap page can be allocated from the memblock
213 * allocator or buddy allocator. If the PG_reserved flag is set, it means
214 * that it allocated from the memblock allocator, just free it via the
215 * free_bootmem_page(). Otherwise, use __free_page().
216 */
217static inline void free_vmemmap_page(struct page *page)
218{
219 if (PageReserved(page))
220 free_bootmem_page(page);
221 else
222 __free_page(page);
223}
224
225/* Free a list of the vmemmap pages */
226static void free_vmemmap_page_list(struct list_head *list)
227{
228 struct page *page, *next;
229
230 list_for_each_entry_safe(page, next, list, lru)
231 free_vmemmap_page(page);
232}
233
234static void vmemmap_remap_pte(pte_t *pte, unsigned long addr,
235 struct vmemmap_remap_walk *walk)
236{
237 /*
238 * Remap the tail pages as read-only to catch illegal write operation
239 * to the tail pages.
240 */
241 pgprot_t pgprot = PAGE_KERNEL_RO;
242 struct page *page = pte_page(*pte);
243 pte_t entry;
244
245 /* Remapping the head page requires r/w */
246 if (unlikely(addr == walk->reuse_addr)) {
247 pgprot = PAGE_KERNEL;
248 list_del(&walk->reuse_page->lru);
249
250 /*
251 * Makes sure that preceding stores to the page contents from
252 * vmemmap_remap_free() become visible before the set_pte_at()
253 * write.
254 */
255 smp_wmb();
256 }
257
258 entry = mk_pte(walk->reuse_page, pgprot);
259 list_add_tail(&page->lru, walk->vmemmap_pages);
260 set_pte_at(&init_mm, addr, pte, entry);
261}
262
263/*
264 * How many struct page structs need to be reset. When we reuse the head
265 * struct page, the special metadata (e.g. page->flags or page->mapping)
266 * cannot copy to the tail struct page structs. The invalid value will be
267 * checked in the free_tail_pages_check(). In order to avoid the message
268 * of "corrupted mapping in tail page". We need to reset at least 3 (one
269 * head struct page struct and two tail struct page structs) struct page
270 * structs.
271 */
272#define NR_RESET_STRUCT_PAGE 3
273
274static inline void reset_struct_pages(struct page *start)
275{
276 struct page *from = start + NR_RESET_STRUCT_PAGE;
277
278 BUILD_BUG_ON(NR_RESET_STRUCT_PAGE * 2 > PAGE_SIZE / sizeof(struct page));
279 memcpy(start, from, sizeof(*from) * NR_RESET_STRUCT_PAGE);
280}
281
282static void vmemmap_restore_pte(pte_t *pte, unsigned long addr,
283 struct vmemmap_remap_walk *walk)
284{
285 pgprot_t pgprot = PAGE_KERNEL;
286 struct page *page;
287 void *to;
288
289 BUG_ON(pte_page(*pte) != walk->reuse_page);
290
291 page = list_first_entry(walk->vmemmap_pages, struct page, lru);
292 list_del(&page->lru);
293 to = page_to_virt(page);
294 copy_page(to, (void *)walk->reuse_addr);
295 reset_struct_pages(to);
296
297 /*
298 * Makes sure that preceding stores to the page contents become visible
299 * before the set_pte_at() write.
300 */
301 smp_wmb();
302 set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot));
303}
304
305/**
306 * vmemmap_remap_free - remap the vmemmap virtual address range [@start, @end)
307 * to the page which @reuse is mapped to, then free vmemmap
308 * which the range are mapped to.
309 * @start: start address of the vmemmap virtual address range that we want
310 * to remap.
311 * @end: end address of the vmemmap virtual address range that we want to
312 * remap.
313 * @reuse: reuse address.
314 *
315 * Return: %0 on success, negative error code otherwise.
316 */
317static int vmemmap_remap_free(unsigned long start, unsigned long end,
318 unsigned long reuse)
319{
320 int ret;
321 LIST_HEAD(vmemmap_pages);
322 struct vmemmap_remap_walk walk = {
323 .remap_pte = vmemmap_remap_pte,
324 .reuse_addr = reuse,
325 .vmemmap_pages = &vmemmap_pages,
326 };
327 int nid = page_to_nid((struct page *)start);
328 gfp_t gfp_mask = GFP_KERNEL | __GFP_THISNODE | __GFP_NORETRY |
329 __GFP_NOWARN;
330
331 /*
332 * Allocate a new head vmemmap page to avoid breaking a contiguous
333 * block of struct page memory when freeing it back to page allocator
334 * in free_vmemmap_page_list(). This will allow the likely contiguous
335 * struct page backing memory to be kept contiguous and allowing for
336 * more allocations of hugepages. Fallback to the currently
337 * mapped head page in case should it fail to allocate.
338 */
339 walk.reuse_page = alloc_pages_node(nid, gfp_mask, 0);
340 if (walk.reuse_page) {
341 copy_page(page_to_virt(walk.reuse_page),
342 (void *)walk.reuse_addr);
343 list_add(&walk.reuse_page->lru, &vmemmap_pages);
344 }
345
346 /*
347 * In order to make remapping routine most efficient for the huge pages,
348 * the routine of vmemmap page table walking has the following rules
349 * (see more details from the vmemmap_pte_range()):
350 *
351 * - The range [@start, @end) and the range [@reuse, @reuse + PAGE_SIZE)
352 * should be continuous.
353 * - The @reuse address is part of the range [@reuse, @end) that we are
354 * walking which is passed to vmemmap_remap_range().
355 * - The @reuse address is the first in the complete range.
356 *
357 * So we need to make sure that @start and @reuse meet the above rules.
358 */
359 BUG_ON(start - reuse != PAGE_SIZE);
360
361 mmap_read_lock(&init_mm);
362 ret = vmemmap_remap_range(reuse, end, &walk);
363 if (ret && walk.nr_walked) {
364 end = reuse + walk.nr_walked * PAGE_SIZE;
365 /*
366 * vmemmap_pages contains pages from the previous
367 * vmemmap_remap_range call which failed. These
368 * are pages which were removed from the vmemmap.
369 * They will be restored in the following call.
370 */
371 walk = (struct vmemmap_remap_walk) {
372 .remap_pte = vmemmap_restore_pte,
373 .reuse_addr = reuse,
374 .vmemmap_pages = &vmemmap_pages,
375 };
376
377 vmemmap_remap_range(reuse, end, &walk);
378 }
379 mmap_read_unlock(&init_mm);
380
381 free_vmemmap_page_list(&vmemmap_pages);
382
383 return ret;
384}
385
386static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
387 gfp_t gfp_mask, struct list_head *list)
388{
389 unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
390 int nid = page_to_nid((struct page *)start);
391 struct page *page, *next;
392
393 while (nr_pages--) {
394 page = alloc_pages_node(nid, gfp_mask, 0);
395 if (!page)
396 goto out;
397 list_add_tail(&page->lru, list);
398 }
399
400 return 0;
401out:
402 list_for_each_entry_safe(page, next, list, lru)
403 __free_pages(page, 0);
404 return -ENOMEM;
405}
406
407/**
408 * vmemmap_remap_alloc - remap the vmemmap virtual address range [@start, end)
409 * to the page which is from the @vmemmap_pages
410 * respectively.
411 * @start: start address of the vmemmap virtual address range that we want
412 * to remap.
413 * @end: end address of the vmemmap virtual address range that we want to
414 * remap.
415 * @reuse: reuse address.
416 * @gfp_mask: GFP flag for allocating vmemmap pages.
417 *
418 * Return: %0 on success, negative error code otherwise.
419 */
420static int vmemmap_remap_alloc(unsigned long start, unsigned long end,
421 unsigned long reuse, gfp_t gfp_mask)
422{
423 LIST_HEAD(vmemmap_pages);
424 struct vmemmap_remap_walk walk = {
425 .remap_pte = vmemmap_restore_pte,
426 .reuse_addr = reuse,
427 .vmemmap_pages = &vmemmap_pages,
428 };
429
430 /* See the comment in the vmemmap_remap_free(). */
431 BUG_ON(start - reuse != PAGE_SIZE);
432
433 if (alloc_vmemmap_page_list(start, end, gfp_mask, &vmemmap_pages))
434 return -ENOMEM;
435
436 mmap_read_lock(&init_mm);
437 vmemmap_remap_range(reuse, end, &walk);
438 mmap_read_unlock(&init_mm);
439
440 return 0;
441}
442
443DEFINE_STATIC_KEY_FALSE(hugetlb_optimize_vmemmap_key);
444EXPORT_SYMBOL(hugetlb_optimize_vmemmap_key);
445
446static bool vmemmap_optimize_enabled = IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON);
447core_param(hugetlb_free_vmemmap, vmemmap_optimize_enabled, bool, 0);
448
449/**
450 * hugetlb_vmemmap_restore - restore previously optimized (by
451 * hugetlb_vmemmap_optimize()) vmemmap pages which
452 * will be reallocated and remapped.
453 * @h: struct hstate.
454 * @head: the head page whose vmemmap pages will be restored.
455 *
456 * Return: %0 if @head's vmemmap pages have been reallocated and remapped,
457 * negative error code otherwise.
458 */
459int hugetlb_vmemmap_restore(const struct hstate *h, struct page *head)
460{
461 int ret;
462 unsigned long vmemmap_start = (unsigned long)head, vmemmap_end;
463 unsigned long vmemmap_reuse;
464
465 if (!HPageVmemmapOptimized(head))
466 return 0;
467
468 vmemmap_end = vmemmap_start + hugetlb_vmemmap_size(h);
469 vmemmap_reuse = vmemmap_start;
470 vmemmap_start += HUGETLB_VMEMMAP_RESERVE_SIZE;
471
472 /*
473 * The pages which the vmemmap virtual address range [@vmemmap_start,
474 * @vmemmap_end) are mapped to are freed to the buddy allocator, and
475 * the range is mapped to the page which @vmemmap_reuse is mapped to.
476 * When a HugeTLB page is freed to the buddy allocator, previously
477 * discarded vmemmap pages must be allocated and remapping.
478 */
479 ret = vmemmap_remap_alloc(vmemmap_start, vmemmap_end, vmemmap_reuse,
480 GFP_KERNEL | __GFP_NORETRY | __GFP_THISNODE);
481 if (!ret) {
482 ClearHPageVmemmapOptimized(head);
483 static_branch_dec(&hugetlb_optimize_vmemmap_key);
484 }
485
486 return ret;
487}
488
489/* Return true iff a HugeTLB whose vmemmap should and can be optimized. */
490static bool vmemmap_should_optimize(const struct hstate *h, const struct page *head)
491{
492 if (!READ_ONCE(vmemmap_optimize_enabled))
493 return false;
494
495 if (!hugetlb_vmemmap_optimizable(h))
496 return false;
497
498 if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) {
499 pmd_t *pmdp, pmd;
500 struct page *vmemmap_page;
501 unsigned long vaddr = (unsigned long)head;
502
503 /*
504 * Only the vmemmap page's vmemmap page can be self-hosted.
505 * Walking the page tables to find the backing page of the
506 * vmemmap page.
507 */
508 pmdp = pmd_off_k(vaddr);
509 /*
510 * The READ_ONCE() is used to stabilize *pmdp in a register or
511 * on the stack so that it will stop changing under the code.
512 * The only concurrent operation where it can be changed is
513 * split_vmemmap_huge_pmd() (*pmdp will be stable after this
514 * operation).
515 */
516 pmd = READ_ONCE(*pmdp);
517 if (pmd_leaf(pmd))
518 vmemmap_page = pmd_page(pmd) + pte_index(vaddr);
519 else
520 vmemmap_page = pte_page(*pte_offset_kernel(pmdp, vaddr));
521 /*
522 * Due to HugeTLB alignment requirements and the vmemmap pages
523 * being at the start of the hotplugged memory region in
524 * memory_hotplug.memmap_on_memory case. Checking any vmemmap
525 * page's vmemmap page if it is marked as VmemmapSelfHosted is
526 * sufficient.
527 *
528 * [ hotplugged memory ]
529 * [ section ][...][ section ]
530 * [ vmemmap ][ usable memory ]
531 * ^ | | |
532 * +---+ | |
533 * ^ | |
534 * +-------+ |
535 * ^ |
536 * +-------------------------------------------+
537 */
538 if (PageVmemmapSelfHosted(vmemmap_page))
539 return false;
540 }
541
542 return true;
543}
544
545/**
546 * hugetlb_vmemmap_optimize - optimize @head page's vmemmap pages.
547 * @h: struct hstate.
548 * @head: the head page whose vmemmap pages will be optimized.
549 *
550 * This function only tries to optimize @head's vmemmap pages and does not
551 * guarantee that the optimization will succeed after it returns. The caller
552 * can use HPageVmemmapOptimized(@head) to detect if @head's vmemmap pages
553 * have been optimized.
554 */
555void hugetlb_vmemmap_optimize(const struct hstate *h, struct page *head)
556{
557 unsigned long vmemmap_start = (unsigned long)head, vmemmap_end;
558 unsigned long vmemmap_reuse;
559
560 if (!vmemmap_should_optimize(h, head))
561 return;
562
563 static_branch_inc(&hugetlb_optimize_vmemmap_key);
564
565 vmemmap_end = vmemmap_start + hugetlb_vmemmap_size(h);
566 vmemmap_reuse = vmemmap_start;
567 vmemmap_start += HUGETLB_VMEMMAP_RESERVE_SIZE;
568
569 /*
570 * Remap the vmemmap virtual address range [@vmemmap_start, @vmemmap_end)
571 * to the page which @vmemmap_reuse is mapped to, then free the pages
572 * which the range [@vmemmap_start, @vmemmap_end] is mapped to.
573 */
574 if (vmemmap_remap_free(vmemmap_start, vmemmap_end, vmemmap_reuse))
575 static_branch_dec(&hugetlb_optimize_vmemmap_key);
576 else
577 SetHPageVmemmapOptimized(head);
578}
579
580static struct ctl_table hugetlb_vmemmap_sysctls[] = {
581 {
582 .procname = "hugetlb_optimize_vmemmap",
583 .data = &vmemmap_optimize_enabled,
584 .maxlen = sizeof(int),
585 .mode = 0644,
586 .proc_handler = proc_dobool,
587 },
588 { }
589};
590
591static int __init hugetlb_vmemmap_init(void)
592{
593 /* HUGETLB_VMEMMAP_RESERVE_SIZE should cover all used struct pages */
594 BUILD_BUG_ON(__NR_USED_SUBPAGE * sizeof(struct page) > HUGETLB_VMEMMAP_RESERVE_SIZE);
595
596 if (IS_ENABLED(CONFIG_PROC_SYSCTL)) {
597 const struct hstate *h;
598
599 for_each_hstate(h) {
600 if (hugetlb_vmemmap_optimizable(h)) {
601 register_sysctl_init("vm", hugetlb_vmemmap_sysctls);
602 break;
603 }
604 }
605 }
606 return 0;
607}
608late_initcall(hugetlb_vmemmap_init);