Design === folio_split() splits a large folio in the same way as buddy allocator splits a large free page for allocation. The purpose is to minimize the number of folios after the split. For example, if user wants to free the 3rd subpage in a order-9 folio, folio_split() will split the order-9 folio as: O-0, O-0, O-0, O-0, O-2, O-3, O-4, O-5, O-6, O-7, O-8 if it is anon O-1, O-0, O-0, O-2, O-3, O-4, O-5, O-6, O-7, O-9 if it is pagecache Since anon folio does not support order-1 yet. The split process is similar to existing approach: 1. Unmap all page mappings (split PMD mappings if exist); 2. Split meta data like memcg, page owner, page alloc tag; 3. Copy meta data in struct folio to sub pages, but instead of spliting the whole folio into multiple smaller ones with the same order in a shot, this approach splits the folio iteratively. Taking the example above, this approach first splits the original order-9 into two order-8, then splits left part of order-8 to two order-7 and so on; 4. Post-process split folios, like write mapping->i_pages for pagecache, adjust folio refcounts, add split folios to corresponding list; 5. Remap split folios 6. Unlock split folios. __split_unmapped_folio() and __split_folio_to_order() replace __split_huge_page() and __split_huge_page_tail() respectively. __split_unmapped_folio() uses different approaches to perform uniform split and buddy allocator like split: 1. uniform split: one single call to __split_folio_to_order() is used to uniformly split the given folio. All resulting folios are put back to the list after split. The folio containing the given page is left to caller to unlock and others are unlocked. 2. buddy allocator like split: old_order - new_order calls to __split_folio_to_order() are used to split the given folio at order N to order N-1. After each call, the target folio is changed to the one containing the page, which is given via folio_split() parameters. After each call, folios not containing the page are put back to the list. The folio containing the page is put back to the list when its order is new_order. All folios are unlocked except the first folio, which is left to caller to unlock. Patch Overview === 1. Patch 1 made file-backed THP split work in split_huge_page_test, patch 2 enabled shmem large folio split to any lower order, and patch 3 added tests for splitting file-backed THP to any lower order. They can be picked independent of this patchset. 2. Patch 4 added __split_unmapped_folio() and __split_folio_to_order() to prepare for moving to new backend split code. 3. Patch 5 moved common code in split_huge_page_to_list_to_order() to __folio_split(). 4. Patch 6 added new folio_split() and made split_huge_page_to_list_to_order() share the new __split_unmapped_folio() with folio_split(). 5. Patch 7 removed no longer used __split_huge_page() and __split_huge_page_tail(). 6. Patch 8 added a new in_folio_offset to split_huge_page debugfs for folio_split() test. 7. Patch 9 used folio_split() for truncate operation. 8. Patch 10 added folio_split() tests. Any comments and/or suggestions are welcome. Thanks. [1] https://lore.kernel.org/linux-mm/20241008223748.555845-1-ziy@xxxxxxxxxx/ [2] https://lore.kernel.org/linux-mm/20241028180932.1319265-1-ziy@xxxxxxxxxx/ [3] https://lore.kernel.org/linux-mm/20241101150357.1752726-1-ziy@xxxxxxxxxx/ [4] https://lore.kernel.org/linux-mm/e6ppwz5t4p4kvir6eqzoto4y5fmdjdxdyvxvtw43ncly4l4ogr@7ruqsay6i2h2/ [5] https://lore.kernel.org/linux-mm/20241205001839.2582020-1-ziy@xxxxxxxxxx/ [6] https://lore.kernel.org/linux-mm/20250106165513.104899-1-ziy@xxxxxxxxxx/ Zi Yan (10): selftests/mm: make file-backed THP split work by setting force option mm/huge_memory: allow split shmem large folio to any lower order selftests/mm: test splitting file-backed THP to any lower order. mm/huge_memory: add two new (not yet used) functions for folio_split() mm/huge_memory: move folio split common code to __folio_split() mm/huge_memory: add buddy allocator like folio_split() mm/huge_memory: remove the old, unused __split_huge_page() mm/huge_memory: add folio_split() to debugfs testing interface. mm/truncate: use folio_split() for truncate operation. selftests/mm: add tests for folio_split(), buddy allocator like split. include/linux/huge_mm.h | 24 + mm/huge_memory.c | 755 ++++++++++++------ mm/truncate.c | 31 +- .../selftests/mm/split_huge_page_test.c | 105 ++- 4 files changed, 633 insertions(+), 282 deletions(-) -- 2.45.2