[v3 22/24] selftests/mm: add partial_mlock test

Usama Arif
2026-03-27T02:16:19Z

Add test for partial mlock on THP which exercises walk_page_range()
with a subset of the THP. This should trigger a PMD split since
mlock operates at page granularity.

Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
.../testing/selftests/mm/thp_pmd_split_test.c | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/tools/testing/selftests/mm/thp_pmd_split_test.c b/tools/testing/selftests/mm/thp_pmd_split_test.c
index 4944a5a516da9..3c9f05457efec 100644
--- a/tools/testing/selftests/mm/thp_pmd_split_test.c
+++ b/tools/testing/selftests/mm/thp_pmd_split_test.c
@@ -177,4 +177,30 @@ TEST_F(thp_pmd_split, partial_mprotect)
self->split_pmd_failed_before);
}

+/*
+ * Partial mlock triggering split (walk_page_range)
+ *
+ * Tests mlock on a partial THP region which should trigger a PMD split.
+ */
+TEST_F(thp_pmd_split, partial_mlock)
+{
+ int ret;
+
+ ret = allocate_thp(self->aligned, self->pmdsize);
+ if (ret)
+ SKIP(return, "Failed to allocate THP");
+
+ /* Partial mlock - should trigger PMD split */
+ ret = mlock((char *)self->aligned + self->pagesize, self->pagesize);
+ if (ret && errno == ENOMEM)
+ SKIP(return, "mlock failed with ENOMEM (resource limit)");
+ ASSERT_EQ(ret, 0);
+
+ /* Cleanup */
+ munlock((char *)self->aligned + self->pagesize, self->pagesize);
+
+ log_and_check_pmd_split(_metadata, self->split_pmd_before,
+ self->split_pmd_failed_before);
+}
+
TEST_HARNESS_MAIN
--
2.52.0