Skip to contentSkip to conversation

mm: shmem: limit forced anonymous shmem huge orders to MAX_PAGECACHE_ORDER

PATCH Applied Jingyi Sun 2 participants 3 messages Nov 18 – Nov 19

Subsystems (3)
lore
Share & tools
lore

Jingyi Sun

author
#0 lore raw

PATCH v3 1/1 mm: shmem: limit forced anonymous shmem huge orders to MAX_PAGECACHE_ORDER

When the top-level shmem_enabled knob is set to "force", shmem_allowable_huge_orders() returns huge_shmem_orders_inherit for anonymous shmem without masking it with THP_ORDERS_ALL_FILE_DEFAULT, unlike the other return path at the end of the same function.

huge_shmem_orders_inherit defaults to BIT(HPAGE_PMD_ORDER). On arm64 with 64K base pages HPAGE_PMD_ORDER is 13, while MAX_PAGECACHE_ORDER is 11 because of the xarray split limit. With shmem_enabled=force, an anonymous MAP_SHARED mapping can therefore get a 512M (order-13) folio in the page cache. Splitting it later, e.g. on swap-out from shmem_writeout(), triggers

  WARNING: lib/xarray.c:1051 at xas_split_alloc

and the split fails, so the folio can never be swapped out.

This is the same problem that commit 9fd154ba926b ("mm/shmem: disable PMD-sized page cache if needed") and commit b66b1b71d7ff ("mm: shmem:
avoid allocating huge pages larger than MAX_PAGECACHE_ORDER for shmem") fixed for the other paths; the force branch was left out. Mask it with THP_ORDERS_ALL_FILE_DEFAULT as well.

Reproduced on v7.3-rc2 in QEMU (arm64, 64K pages, -cpu cortex-a72, 4G RAM, zram swap): with shmem_enabled=force, a PMD-aligned MAP_SHARED|MAP_ANONYMOUS mapping of one PMD size got a single PMD-mapped folio (ShmemPmdMapped: 524288 kB) and MADV_PAGEOUT triggered the warning above in 3/3 runs. With this change: 0/3. shmem_enabled=always and 4K pages did not show the warning. The code is unchanged in v7.4-rc3 and in mm-unstable, but the test has not been re-run on those trees.

The issue was found with a research static analyzer for cross-architecture dependency bugs and confirmed with the QEMU reproducer described above.

Fixes e7a2ab7b3bb5 ("mm: shmem: add mTHP support for anonymous shmem")
Cc Luiz Capitulino <[email protected]>

---
Changes in v3:
- In the force branch, set mask and jump to the common return at the end of the function instead of masking in place (David). No functional change from v2; kept Baolin's Reviewed-by.
- Re-ran the reproducer with this version on v7.3-rc2: 0/3 with the change, 3/3 without.

Changes in v2:
- Dropped the Assisted-by tag (Baolin).
- Collected Reviewed-by from Baolin.
- Dropped the note about the overlap with Luiz's mTHP series; Luiz confirmed his series will land after this patch.

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 mm/shmem.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/shmem.c +5 -2
index 10cef89b00b0..6d6b4a7e024a 100644
@@ -2113,8 +2113,10 @@ unsigned long shmem_allowable_huge_orders(struct inode *inode,
 	 * Only allow inherit orders if the top-level value is 'force', which
 	 * means non-PMD sized THP can not override 'huge' mount option now.
 	 */
-	if (shmem_huge == SHMEM_HUGE_FORCE)
-		return READ_ONCE(huge_shmem_orders_inherit);
+	if (shmem_huge == SHMEM_HUGE_FORCE) {
+		mask = READ_ONCE(huge_shmem_orders_inherit);
+		goto out;
+	}
 
 	/* Allow mTHP that will be fully within i_size. */
 	mask |= shmem_get_orders_within_size(inode, within_size_orders, index, 0);
@@ -2125,6 +2127,7 @@ unsigned long shmem_allowable_huge_orders(struct inode *inode,
 	if (global_orders > 0)
 		mask |= READ_ONCE(huge_shmem_orders_inherit);
 
+out:
 	return THP_ORDERS_ALL_FILE_DEFAULT & mask;
 }
 

base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4

Signature · 1 line
2.43.0
Reviewed-by on v3 1/1
#1 lore raw
Quoted 35 lines · Jingyi Sun

When the top-level shmem_enabled knob is set to "force", shmem_allowable_huge_orders() returns huge_shmem_orders_inherit for anonymous shmem without masking it with THP_ORDERS_ALL_FILE_DEFAULT, unlike the other return path at the end of the same function.

huge_shmem_orders_inherit defaults to BIT(HPAGE_PMD_ORDER). On arm64 with 64K base pages HPAGE_PMD_ORDER is 13, while MAX_PAGECACHE_ORDER is 11 because of the xarray split limit. With shmem_enabled=force, an anonymous MAP_SHARED mapping can therefore get a 512M (order-13) folio in the page cache. Splitting it later, e.g. on swap-out from shmem_writeout(), triggers

  WARNING: lib/xarray.c:1051 at xas_split_alloc

and the split fails, so the folio can never be swapped out.

This is the same problem that commit 9fd154ba926b ("mm/shmem: disable PMD-sized page cache if needed") and commit b66b1b71d7ff ("mm: shmem:
avoid allocating huge pages larger than MAX_PAGECACHE_ORDER for shmem") fixed for the other paths; the force branch was left out. Mask it with THP_ORDERS_ALL_FILE_DEFAULT as well.

Reproduced on v7.3-rc2 in QEMU (arm64, 64K pages, -cpu cortex-a72, 4G RAM, zram swap): with shmem_enabled=force, a PMD-aligned MAP_SHARED|MAP_ANONYMOUS mapping of one PMD size got a single PMD-mapped folio (ShmemPmdMapped: 524288 kB) and MADV_PAGEOUT triggered the warning above in 3/3 runs. With this change: 0/3. shmem_enabled=always and 4K pages did not show the warning. The code is unchanged in v7.4-rc3 and in mm-unstable, but the test has not been re-run on those trees.

The issue was found with a research static analyzer for cross-architecture dependency bugs and confirmed with the QEMU reproducer described above.

Quoted 6 lines
Fixes e7a2ab7b3bb5 ("mm: shmem: add mTHP support for anonymous shmem")
Cc Luiz Capitulino <[email protected]>

---

I'll queue this later.

Signature · 3 lines
Cheers,

David
#2 lore raw
In reply to Jingyi Sun
Quoted 5 lines · Jingyi Sun

When the top-level shmem_enabled knob is set to "force", shmem_allowable_huge_orders() returns huge_shmem_orders_inherit for anonymous shmem without masking it with THP_ORDERS_ALL_FILE_DEFAULT, unlike the other return path at the end of the same function.

Quoted 8 lines

huge_shmem_orders_inherit defaults to BIT(HPAGE_PMD_ORDER). On arm64 with 64K base pages HPAGE_PMD_ORDER is 13, while MAX_PAGECACHE_ORDER is 11 because of the xarray split limit. With shmem_enabled=force, an anonymous MAP_SHARED mapping can therefore get a 512M (order-13) folio in the page cache. Splitting it later, e.g. on swap-out from shmem_writeout(), triggers

[...]

Applied, thanks!

tree: mm/core.git
branch: shmem-7.5.misc

Applied patches will appear soon in mm-next, followed by linux-next.

Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.

It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.

[1/1] mm: shmem: limit forced anonymous shmem huge orders to MAX_PAGECACHE_ORDER

      https://git.kernel.org/mm/core/c/a0dc4b77
Signature · 3 lines
Cheers,

David