Skip to content

Commit cd197c3

Browse files
Barry Songakpm00
authored andcommitted
mm: prohibit the last subpage from reusing the entire large folio
In a Copy-on-Write (CoW) scenario, the last subpage will reuse the entire large folio, resulting in the waste of (nr_pages - 1) pages. This wasted memory remains allocated until it is either unmapped or memory reclamation occurs. The following small program can serve as evidence of this behavior main() { #define SIZE 1024 * 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) _exit(0); memset(p, 0x12, SIZE); printf("done\n"); while(1); } For example, using a 1024KiB mTHP by: echo always > /sys/kernel/mm/transparent_hugepage/hugepages-1024kB/enabled (1) w/o the patch, it takes 2GiB, Before running the test program, / # free -m total used free shared buff/cache available Mem: 5754 84 5692 0 17 5669 Swap: 0 0 0 / # /a.out & / # done After running the test program, / # free -m total used free shared buff/cache available Mem: 5754 2149 3627 0 19 3605 Swap: 0 0 0 (2) w/ the patch, it takes 1GiB only, Before running the test program, / # free -m total used free shared buff/cache available Mem: 5754 89 5687 0 17 5664 Swap: 0 0 0 / # /a.out & / # done After running the test program, / # free -m total used free shared buff/cache available Mem: 5754 1122 4655 0 17 4632 Swap: 0 0 0 This patch migrates the last subpage to a small folio and immediately returns the large folio to the system. It benefits both memory availability and anti-fragmentation. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Barry Song <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Ryan Roberts <[email protected]> Cc: Lance Yang <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent c087a5c commit cd197c3

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

mm/memory.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,16 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio)
34983498
static bool wp_can_reuse_anon_folio(struct folio *folio,
34993499
struct vm_area_struct *vma)
35003500
{
3501+
/*
3502+
* We could currently only reuse a subpage of a large folio if no
3503+
* other subpages of the large folios are still mapped. However,
3504+
* let's just consistently not reuse subpages even if we could
3505+
* reuse in that scenario, and give back a large folio a bit
3506+
* sooner.
3507+
*/
3508+
if (folio_test_large(folio))
3509+
return false;
3510+
35013511
/*
35023512
* We have to verify under folio lock: these early checks are
35033513
* just an optimization to avoid locking the folio and freeing

0 commit comments

Comments
 (0)