Migrating the libhugetlbfs/testcases/alloc-instantiate-race.c test
PATCH v5 1/1 Migrating the libhugetlbfs/testcases/alloc-instantiate-race.c test
This test is designed to detect a kernel allocation race introduced with hugepage demand-faulting. The problem is that no lock is held between allocating a hugepage and instantiating it in the pagetables or page cache index. In between the two, the (huge) page is cleared, so there's substantial time. Thus two processes can race instantiating the (same) last available hugepage - one will fail on the allocation, and thus cause an OOM fault even though the page it actually wants is being instantiated by the other racing process.
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/ltp/[email protected]/
LTP Github CI link: https://github.com/linux-test-project/ltp/pull/1313
All the checks are passed
---
v4:
Addressed review comments:
- Removed unnecessary [Description] tag from comment block
- Added static keyword to global variables (child1, child2, race_type, fd_sync)
- Moved totpages and hpage_size to local scope in run_test()
- Replaced busy loop with TST_CHECKPOINT_WAIT/WAKE mechanism
- Fixed indentation in thread_racer() function
- Made check_online_cpus() function static
- Declared loop variable 'i' inside for loops using C99 style
- Removed unnecessary 'available' variable, use CPU_COUNT() directly
- Fixed indentation for tst_res() call
- Removed q_sync global variable to avoid uninitialized access
- Removed unused SYSFS_CPU_ONLINE_FMT macro
- Optimized variable scope throughout the code
- Implemented proper checkpoint synchronization pattern
- Added cleanup() function for resource cleanup
- Updated Makefile, runtest/hugetlb, and .gitignore
v5:
- Replace empty initializer {} with {NULL, NULL, NULL} to fix -Wmissing-field-initializers warning in the options array terminator.
---
--- runtest/hugetlb | 1 + testcases/kernel/mem/.gitignore | 1 + .../kernel/mem/hugetlb/hugemmap/Makefile | 2 + .../kernel/mem/hugetlb/hugemmap/hugemmap36.c | 279 ++++++++++++++++++ 4 files changed, 283 insertions(+) create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap36.c
runtest/hugetlb +1 -0
@@ -36,6 +36,7 @@ hugemmap30 hugemmap30 hugemmap31 hugemmap31 hugemmap32 hugemmap32 hugemmap34 hugemmap34 +hugemmap36 hugemmap36 hugemmap05_1 hugemmap05 -m hugemmap05_2 hugemmap05 -s hugemmap05_3 hugemmap05 -s -m
testcases/kernel/mem/.gitignore +1 -0
@@ -36,6 +36,7 @@ /hugetlb/hugemmap/hugemmap31 /hugetlb/hugemmap/hugemmap32 /hugetlb/hugemmap/hugemmap34 +/hugetlb/hugemmap/hugemmap36 /hugetlb/hugeshmat/hugeshmat01 /hugetlb/hugeshmat/hugeshmat02 /hugetlb/hugeshmat/hugeshmat03
testcases/kernel/mem/hugetlb/hugemmap/Makefile +2 -0
@@ -12,3 +12,5 @@ CFLAGS_no_stack_prot := $(filter-out -fstack-clash-protection, $(CFLAGS)) hugemmap06: CFLAGS+=-pthread hugemmap34: CFLAGS=$(CFLAGS_no_stack_prot) +hugemmap36: LDLIBS+=-lpthread +hugemmap36: CFLAGS+=-pthread
testcases/kernel/mem/hugetlb/hugemmap/hugemmap36.c new file +279 -0
279 lines not on this page · raw message
Signature · 5 lines
2.53.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Samir,
Quoted 8 lines · Samir
Migrating the
libhugetlbfs/testcases/alloc-instantiate-race.ctestSigned-off-by Samir <[email protected]>v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/ltp/[email protected]/LTP Github CI link: https://github.com/linux-test-project/ltp/pull/1313
All the checks are passed
Version history references should use the Link: tag:
The LTP Github CI link: line and All the checks are passed are free text appearing after Signed-off-by: and must be removed.
[...]
Quoted 4 lines
+/*
+ * Copyright (C) 2005-2006 IBM Corporation
+ * Author: David Gibson & Adam Litke
+ */
New file must include the year it was added. Add 2026 to the copyright range.
Quoted 2 lines
+/*
+ * This test is designed to detect a kernel allocation race introduced
The doc block must open with /*\ (not /*) for LTP's RST toolchain.
[...]
testcases/kernel/mem/hugetlb/hugemmap/hugemmap36.c line 23
Quoted 2 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap36.c new file +2
-0+#include "tst_safe_pthread.h" +#include "hugetlb.h"
Add #include "tst_test.h" explicitly, as done in the rest of this directory.
[...]
Quoted 1 line
+static int child1, child2,
race_type,fd_sync;
fd_sync zero-initializes to 0 (stdin). cleanup() checks if (fd_sync >= 0) and would close stdin if called before run_test(). Also, after run_test() calls SAFE_CLOSE(fd_sync) without resetting it to -1, cleanup() double-closes the same fd on the normal exit path. Initialize fd_sync = -1 and reset it to -1 after each close.
[...]
Pre-existing issues noticed in the surrounding code (not introduced by this patch):
- run_test(): local p_sync (MMAP) is not reachable from cleanup(); leaks if tst_brk() fires inside run_race().
- run_race(): local fd and p are not reachable from cleanup(); same concern.
---
Note:
Our agent completed the review of the patch. The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp