Skip to content

Commit ad2fa37

Browse files
Muchun Songtorvalds
authored andcommitted
mm: hugetlb: alloc the vmemmap pages associated with each HugeTLB page
When we free a HugeTLB page to the buddy allocator, we need to allocate the vmemmap pages associated with it. However, we may not be able to allocate the vmemmap pages when the system is under memory pressure. In this case, we just refuse to free the HugeTLB page. This changes behavior in some corner cases as listed below: 1) Failing to free a huge page triggered by the user (decrease nr_pages). User needs to try again later. 2) Failing to free a surplus huge page when freed by the application. Try again later when freeing a huge page next time. 3) Failing to dissolve a free huge page on ZONE_MOVABLE via offline_pages(). This can happen when we have plenty of ZONE_MOVABLE memory, but not enough kernel memory to allocate vmemmmap pages. We may even be able to migrate huge page contents, but will not be able to dissolve the source huge page. This will prevent an offline operation and is unfortunate as memory offlining is expected to succeed on movable zones. Users that depend on memory hotplug to succeed for movable zones should carefully consider whether the memory savings gained from this feature are worth the risk of possibly not being able to offline memory in certain situations. 4) Failing to dissolve a huge page on CMA/ZONE_MOVABLE via alloc_contig_range() - once we have that handling in place. Mainly affects CMA and virtio-mem. Similar to 3). virito-mem will handle migration errors gracefully. CMA might be able to fallback on other free areas within the CMA region. Vmemmap pages are allocated from the page freeing context. In order for those allocations to be not disruptive (e.g. trigger oom killer) __GFP_NORETRY is used. hugetlb_lock is dropped for the allocation because a non sleeping allocation would be too fragile and it could fail too easily under memory pressure. GFP_ATOMIC or other modes to access memory reserves is not used because we want to prevent consuming reserves under heavy hugetlb freeing. [[email protected]: fix dissolve_free_huge_page use of tail/head page] Link: https://lkml.kernel.org/r/[email protected] [[email protected]: fix alloc_vmemmap_page_list documentation warning] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Muchun Song <[email protected]> Signed-off-by: Mike Kravetz <[email protected]> Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Reviewed-by: Mike Kravetz <[email protected]> Reviewed-by: Oscar Salvador <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: Balbir Singh <[email protected]> Cc: Barry Song <[email protected]> Cc: Bodeddula Balasubramaniam <[email protected]> Cc: Borislav Petkov <[email protected]> Cc: Chen Huang <[email protected]> Cc: Dave Hansen <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: David Rientjes <[email protected]> Cc: HORIGUCHI NAOYA <[email protected]> Cc: "H. Peter Anvin" <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Joao Martins <[email protected]> Cc: Joerg Roedel <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Miaohe Lin <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Mina Almasry <[email protected]> Cc: Oliver Neukum <[email protected]> Cc: Paul E. McKenney <[email protected]> Cc: Pawan Gupta <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Randy Dunlap <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Xiongchun Duan <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b65d4ad commit ad2fa37

File tree

9 files changed

+227
-17
lines changed

9 files changed

+227
-17
lines changed

Documentation/admin-guide/mm/hugetlbpage.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ HugePages_Surp
6060
the pool above the value in ``/proc/sys/vm/nr_hugepages``. The
6161
maximum number of surplus huge pages is controlled by
6262
``/proc/sys/vm/nr_overcommit_hugepages``.
63+
Note: When the feature of freeing unused vmemmap pages associated
64+
with each hugetlb page is enabled, the number of surplus huge pages
65+
may be temporarily larger than the maximum number of surplus huge
66+
pages when the system is under memory pressure.
6367
Hugepagesize
6468
is the default hugepage size (in Kb).
6569
Hugetlb
@@ -80,6 +84,10 @@ returned to the huge page pool when freed by a task. A user with root
8084
privileges can dynamically allocate more or free some persistent huge pages
8185
by increasing or decreasing the value of ``nr_hugepages``.
8286

87+
Note: When the feature of freeing unused vmemmap pages associated with each
88+
hugetlb page is enabled, we can fail to free the huge pages triggered by
89+
the user when ths system is under memory pressure. Please try again later.
90+
8391
Pages that are used as huge pages are reserved inside the kernel and cannot
8492
be used for other purposes. Huge pages cannot be swapped out under
8593
memory pressure.

Documentation/admin-guide/mm/memory-hotplug.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,19 @@ creates ZONE_MOVABLE as following.
357357
Unfortunately, there is no information to show which memory block belongs
358358
to ZONE_MOVABLE. This is TBD.
359359

360+
Memory offlining can fail when dissolving a free huge page on ZONE_MOVABLE
361+
and the feature of freeing unused vmemmap pages associated with each hugetlb
362+
page is enabled.
363+
364+
This can happen when we have plenty of ZONE_MOVABLE memory, but not enough
365+
kernel memory to allocate vmemmmap pages. We may even be able to migrate
366+
huge page contents, but will not be able to dissolve the source huge page.
367+
This will prevent an offline operation and is unfortunate as memory offlining
368+
is expected to succeed on movable zones. Users that depend on memory hotplug
369+
to succeed for movable zones should carefully consider whether the memory
370+
savings gained from this feature are worth the risk of possibly not being
371+
able to offline memory in certain situations.
372+
360373
.. note::
361374
Techniques that rely on long-term pinnings of memory (especially, RDMA and
362375
vfio) are fundamentally problematic with ZONE_MOVABLE and, therefore, memory

include/linux/hugetlb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,14 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
532532
* modifications require hugetlb_lock.
533533
* HPG_freed - Set when page is on the free lists.
534534
* Synchronization: hugetlb_lock held for examination and modification.
535+
* HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed.
535536
*/
536537
enum hugetlb_page_flags {
537538
HPG_restore_reserve = 0,
538539
HPG_migratable,
539540
HPG_temporary,
540541
HPG_freed,
542+
HPG_vmemmap_optimized,
541543
__NR_HPAGEFLAGS,
542544
};
543545

@@ -583,6 +585,7 @@ HPAGEFLAG(RestoreReserve, restore_reserve)
583585
HPAGEFLAG(Migratable, migratable)
584586
HPAGEFLAG(Temporary, temporary)
585587
HPAGEFLAG(Freed, freed)
588+
HPAGEFLAG(VmemmapOptimized, vmemmap_optimized)
586589

587590
#ifdef CONFIG_HUGETLB_PAGE
588591

include/linux/mm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,6 +3078,8 @@ static inline void print_vma_addr(char *prefix, unsigned long rip)
30783078

30793079
void vmemmap_remap_free(unsigned long start, unsigned long end,
30803080
unsigned long reuse);
3081+
int vmemmap_remap_alloc(unsigned long start, unsigned long end,
3082+
unsigned long reuse, gfp_t gfp_mask);
30813083

30823084
void *sparse_buffer_alloc(unsigned long size);
30833085
struct page * __populate_section_memmap(unsigned long pfn,

mm/hugetlb.c

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,39 @@ static void remove_hugetlb_page(struct hstate *h, struct page *page,
13761376
h->nr_huge_pages_node[nid]--;
13771377
}
13781378

1379+
static void add_hugetlb_page(struct hstate *h, struct page *page,
1380+
bool adjust_surplus)
1381+
{
1382+
int zeroed;
1383+
int nid = page_to_nid(page);
1384+
1385+
VM_BUG_ON_PAGE(!HPageVmemmapOptimized(page), page);
1386+
1387+
lockdep_assert_held(&hugetlb_lock);
1388+
1389+
INIT_LIST_HEAD(&page->lru);
1390+
h->nr_huge_pages++;
1391+
h->nr_huge_pages_node[nid]++;
1392+
1393+
if (adjust_surplus) {
1394+
h->surplus_huge_pages++;
1395+
h->surplus_huge_pages_node[nid]++;
1396+
}
1397+
1398+
set_compound_page_dtor(page, HUGETLB_PAGE_DTOR);
1399+
set_page_private(page, 0);
1400+
SetHPageVmemmapOptimized(page);
1401+
1402+
/*
1403+
* This page is now managed by the hugetlb allocator and has
1404+
* no users -- drop the last reference.
1405+
*/
1406+
zeroed = put_page_testzero(page);
1407+
VM_BUG_ON_PAGE(!zeroed, page);
1408+
arch_clear_hugepage_flags(page);
1409+
enqueue_huge_page(h, page);
1410+
}
1411+
13791412
static void __update_and_free_page(struct hstate *h, struct page *page)
13801413
{
13811414
int i;
@@ -1384,6 +1417,18 @@ static void __update_and_free_page(struct hstate *h, struct page *page)
13841417
if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported())
13851418
return;
13861419

1420+
if (alloc_huge_page_vmemmap(h, page)) {
1421+
spin_lock_irq(&hugetlb_lock);
1422+
/*
1423+
* If we cannot allocate vmemmap pages, just refuse to free the
1424+
* page and put the page back on the hugetlb free list and treat
1425+
* as a surplus page.
1426+
*/
1427+
add_hugetlb_page(h, page, true);
1428+
spin_unlock_irq(&hugetlb_lock);
1429+
return;
1430+
}
1431+
13871432
for (i = 0; i < pages_per_huge_page(h);
13881433
i++, subpage = mem_map_next(subpage, page, i)) {
13891434
subpage->flags &= ~(1 << PG_locked | 1 << PG_error |
@@ -1450,7 +1495,7 @@ static inline void flush_free_hpage_work(struct hstate *h)
14501495
static void update_and_free_page(struct hstate *h, struct page *page,
14511496
bool atomic)
14521497
{
1453-
if (!free_vmemmap_pages_per_hpage(h) || !atomic) {
1498+
if (!HPageVmemmapOptimized(page) || !atomic) {
14541499
__update_and_free_page(h, page);
14551500
return;
14561501
}
@@ -1806,10 +1851,14 @@ static struct page *remove_pool_huge_page(struct hstate *h,
18061851
* nothing for in-use hugepages and non-hugepages.
18071852
* This function returns values like below:
18081853
*
1809-
* -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
1810-
* (allocated or reserved.)
1811-
* 0: successfully dissolved free hugepages or the page is not a
1812-
* hugepage (considered as already dissolved)
1854+
* -ENOMEM: failed to allocate vmemmap pages to free the freed hugepages
1855+
* when the system is under memory pressure and the feature of
1856+
* freeing unused vmemmap pages associated with each hugetlb page
1857+
* is enabled.
1858+
* -EBUSY: failed to dissolved free hugepages or the hugepage is in-use
1859+
* (allocated or reserved.)
1860+
* 0: successfully dissolved free hugepages or the page is not a
1861+
* hugepage (considered as already dissolved)
18131862
*/
18141863
int dissolve_free_huge_page(struct page *page)
18151864
{
@@ -1851,19 +1900,38 @@ int dissolve_free_huge_page(struct page *page)
18511900
goto retry;
18521901
}
18531902

1854-
/*
1855-
* Move PageHWPoison flag from head page to the raw error page,
1856-
* which makes any subpages rather than the error page reusable.
1857-
*/
1858-
if (PageHWPoison(head) && page != head) {
1859-
SetPageHWPoison(page);
1860-
ClearPageHWPoison(head);
1861-
}
18621903
remove_hugetlb_page(h, head, false);
18631904
h->max_huge_pages--;
18641905
spin_unlock_irq(&hugetlb_lock);
1865-
update_and_free_page(h, head, false);
1866-
return 0;
1906+
1907+
/*
1908+
* Normally update_and_free_page will allocate required vmemmmap
1909+
* before freeing the page. update_and_free_page will fail to
1910+
* free the page if it can not allocate required vmemmap. We
1911+
* need to adjust max_huge_pages if the page is not freed.
1912+
* Attempt to allocate vmemmmap here so that we can take
1913+
* appropriate action on failure.
1914+
*/
1915+
rc = alloc_huge_page_vmemmap(h, head);
1916+
if (!rc) {
1917+
/*
1918+
* Move PageHWPoison flag from head page to the raw
1919+
* error page, which makes any subpages rather than
1920+
* the error page reusable.
1921+
*/
1922+
if (PageHWPoison(head) && page != head) {
1923+
SetPageHWPoison(page);
1924+
ClearPageHWPoison(head);
1925+
}
1926+
update_and_free_page(h, head, false);
1927+
} else {
1928+
spin_lock_irq(&hugetlb_lock);
1929+
add_hugetlb_page(h, head, false);
1930+
h->max_huge_pages++;
1931+
spin_unlock_irq(&hugetlb_lock);
1932+
}
1933+
1934+
return rc;
18671935
}
18681936
out:
18691937
spin_unlock_irq(&hugetlb_lock);

mm/hugetlb_vmemmap.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,38 @@ static inline unsigned long free_vmemmap_pages_size_per_hpage(struct hstate *h)
185185
return (unsigned long)free_vmemmap_pages_per_hpage(h) << PAGE_SHIFT;
186186
}
187187

188+
/*
189+
* Previously discarded vmemmap pages will be allocated and remapping
190+
* after this function returns zero.
191+
*/
192+
int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
193+
{
194+
int ret;
195+
unsigned long vmemmap_addr = (unsigned long)head;
196+
unsigned long vmemmap_end, vmemmap_reuse;
197+
198+
if (!HPageVmemmapOptimized(head))
199+
return 0;
200+
201+
vmemmap_addr += RESERVE_VMEMMAP_SIZE;
202+
vmemmap_end = vmemmap_addr + free_vmemmap_pages_size_per_hpage(h);
203+
vmemmap_reuse = vmemmap_addr - PAGE_SIZE;
204+
/*
205+
* The pages which the vmemmap virtual address range [@vmemmap_addr,
206+
* @vmemmap_end) are mapped to are freed to the buddy allocator, and
207+
* the range is mapped to the page which @vmemmap_reuse is mapped to.
208+
* When a HugeTLB page is freed to the buddy allocator, previously
209+
* discarded vmemmap pages must be allocated and remapping.
210+
*/
211+
ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse,
212+
GFP_KERNEL | __GFP_NORETRY | __GFP_THISNODE);
213+
214+
if (!ret)
215+
ClearHPageVmemmapOptimized(head);
216+
217+
return ret;
218+
}
219+
188220
void free_huge_page_vmemmap(struct hstate *h, struct page *head)
189221
{
190222
unsigned long vmemmap_addr = (unsigned long)head;
@@ -203,4 +235,6 @@ void free_huge_page_vmemmap(struct hstate *h, struct page *head)
203235
* which the range [@vmemmap_addr, @vmemmap_end] is mapped to.
204236
*/
205237
vmemmap_remap_free(vmemmap_addr, vmemmap_end, vmemmap_reuse);
238+
239+
SetHPageVmemmapOptimized(head);
206240
}

mm/hugetlb_vmemmap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/hugetlb.h>
1212

1313
#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP
14+
int alloc_huge_page_vmemmap(struct hstate *h, struct page *head);
1415
void free_huge_page_vmemmap(struct hstate *h, struct page *head);
1516

1617
/*
@@ -25,6 +26,11 @@ static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h)
2526
return 0;
2627
}
2728
#else
29+
static inline int alloc_huge_page_vmemmap(struct hstate *h, struct page *head)
30+
{
31+
return 0;
32+
}
33+
2834
static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head)
2935
{
3036
}

mm/migrate.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,10 @@ void migrate_page_states(struct page *newpage, struct page *page)
626626
if (PageSwapCache(page))
627627
ClearPageSwapCache(page);
628628
ClearPagePrivate(page);
629-
set_page_private(page, 0);
629+
630+
/* page->private contains hugetlb specific flags */
631+
if (!PageHuge(page))
632+
set_page_private(page, 0);
630633

631634
/*
632635
* If any waiters have accumulated on the new page then

mm/sparse-vmemmap.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
* @remap_pte: called for each lowest-level entry (PTE).
4141
* @reuse_page: the page which is reused for the tail vmemmap pages.
4242
* @reuse_addr: the virtual address of the @reuse_page page.
43-
* @vmemmap_pages: the list head of the vmemmap pages that can be freed.
43+
* @vmemmap_pages: the list head of the vmemmap pages that can be freed
44+
* or is mapped from.
4445
*/
4546
struct vmemmap_remap_walk {
4647
void (*remap_pte)(pte_t *pte, unsigned long addr,
@@ -224,6 +225,78 @@ void vmemmap_remap_free(unsigned long start, unsigned long end,
224225
free_vmemmap_page_list(&vmemmap_pages);
225226
}
226227

228+
static void vmemmap_restore_pte(pte_t *pte, unsigned long addr,
229+
struct vmemmap_remap_walk *walk)
230+
{
231+
pgprot_t pgprot = PAGE_KERNEL;
232+
struct page *page;
233+
void *to;
234+
235+
BUG_ON(pte_page(*pte) != walk->reuse_page);
236+
237+
page = list_first_entry(walk->vmemmap_pages, struct page, lru);
238+
list_del(&page->lru);
239+
to = page_to_virt(page);
240+
copy_page(to, (void *)walk->reuse_addr);
241+
242+
set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot));
243+
}
244+
245+
static int alloc_vmemmap_page_list(unsigned long start, unsigned long end,
246+
gfp_t gfp_mask, struct list_head *list)
247+
{
248+
unsigned long nr_pages = (end - start) >> PAGE_SHIFT;
249+
int nid = page_to_nid((struct page *)start);
250+
struct page *page, *next;
251+
252+
while (nr_pages--) {
253+
page = alloc_pages_node(nid, gfp_mask, 0);
254+
if (!page)
255+
goto out;
256+
list_add_tail(&page->lru, list);
257+
}
258+
259+
return 0;
260+
out:
261+
list_for_each_entry_safe(page, next, list, lru)
262+
__free_pages(page, 0);
263+
return -ENOMEM;
264+
}
265+
266+
/**
267+
* vmemmap_remap_alloc - remap the vmemmap virtual address range [@start, end)
268+
* to the page which is from the @vmemmap_pages
269+
* respectively.
270+
* @start: start address of the vmemmap virtual address range that we want
271+
* to remap.
272+
* @end: end address of the vmemmap virtual address range that we want to
273+
* remap.
274+
* @reuse: reuse address.
275+
* @gfp_mask: GFP flag for allocating vmemmap pages.
276+
*/
277+
int vmemmap_remap_alloc(unsigned long start, unsigned long end,
278+
unsigned long reuse, gfp_t gfp_mask)
279+
{
280+
LIST_HEAD(vmemmap_pages);
281+
struct vmemmap_remap_walk walk = {
282+
.remap_pte = vmemmap_restore_pte,
283+
.reuse_addr = reuse,
284+
.vmemmap_pages = &vmemmap_pages,
285+
};
286+
287+
/* See the comment in the vmemmap_remap_free(). */
288+
BUG_ON(start - reuse != PAGE_SIZE);
289+
290+
might_sleep_if(gfpflags_allow_blocking(gfp_mask));
291+
292+
if (alloc_vmemmap_page_list(start, end, gfp_mask, &vmemmap_pages))
293+
return -ENOMEM;
294+
295+
vmemmap_remap_range(reuse, end, &walk);
296+
297+
return 0;
298+
}
299+
227300
/*
228301
* Allocate a block of memory to be used to back the virtual memory map
229302
* or to back the page tables that are used to create the mapping.

0 commit comments

Comments
 (0)