mm/huge_memory: fix folio split check for anon folios in swapcache

Both uniform and non uniform split check missed the check to prevent
splitting anon folios in swapcache to non-zero order.

Splitting anon folios in swapcache to non-zero order can cause data
corruption since swapcache only support PMD order and order-0 entries. 
This can happen when one use split_huge_pages under debugfs to split
anon folios in swapcache.

In-tree callers do not perform such an illegal operation.  Only debugfs
interface could trigger it.  I will put adding a test case on my TODO
list.

Fix the check.

Link: https://lkml.kernel.org/r/20251105162910.752266-1-ziy@nvidia.com
Fixes: 58729c04cf ("mm/huge_memory: add buddy allocator like (non-uniform) folio_split()")
Signed-off-by: Zi Yan <ziy@nvidia.com>
Reported-by: "David Hildenbrand (Red Hat)" <david@kernel.org>
Closes: https://lore.kernel.org/all/dc0ecc2c-4089-484f-917f-920fdca4c898@kernel.org/
Acked-by: David Hildenbrand (Red Hat) <david@kernel.org>
Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Nico Pache <npache@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Wei Yang <richard.weiyang@gmail.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
Zi Yan 2025-11-05 11:29:10 -05:00 committed by Andrew Morton
parent 3470715e5c
commit f1d47cafe5
1 changed files with 4 additions and 2 deletions

View File

@ -3522,7 +3522,8 @@ bool non_uniform_split_supported(struct folio *folio, unsigned int new_order,
/* order-1 is not supported for anonymous THP. */ /* order-1 is not supported for anonymous THP. */
VM_WARN_ONCE(warns && new_order == 1, VM_WARN_ONCE(warns && new_order == 1,
"Cannot split to order-1 folio"); "Cannot split to order-1 folio");
return new_order != 1; if (new_order == 1)
return false;
} else if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && } else if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) &&
!mapping_large_folio_support(folio->mapping)) { !mapping_large_folio_support(folio->mapping)) {
/* /*
@ -3553,7 +3554,8 @@ bool uniform_split_supported(struct folio *folio, unsigned int new_order,
if (folio_test_anon(folio)) { if (folio_test_anon(folio)) {
VM_WARN_ONCE(warns && new_order == 1, VM_WARN_ONCE(warns && new_order == 1,
"Cannot split to order-1 folio"); "Cannot split to order-1 folio");
return new_order != 1; if (new_order == 1)
return false;
} else if (new_order) { } else if (new_order) {
if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) &&
!mapping_large_folio_support(folio->mapping)) { !mapping_large_folio_support(folio->mapping)) {