Skip to contentSkip to conversation

hugemmap: Migrate alloc-instantiate-race test from libhugetlbfs

PATCH Superseded Samir Mulani 2 participants 3 messages Aug 18 – Aug 21

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 hugemmap: Migrate alloc-instantiate-race test from libhugetlbfs

Migrate the alloc-instantiate-race.c test from libhugetlbfs [1] to LTP as hugemmap36.

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.

[1] https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/alloc-instantiate-race.c

---

v6:
- Fix copyright year, /*\ doc block, add #include "tst_test.h"
- Promote p_sync, fd_race, p_race to globals for cleanup() reachability
- Initialize fd_sync/fd_race = -1; reset to -1 after SAFE_CLOSE()
- Add p_race, fd_race, p_sync guards to cleanup()

 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/Makefile      |   2 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap42.c  | 294 ++++++++++++++++++
 4 files changed, 298 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c
runtest/hugetlb +1 -0
index 6b35c1f42..b4c9dc6cd 100644
@@ -40,6 +40,7 @@ hugemmap35 hugemmap35
 hugemmap36 hugemmap36
 hugemmap37 hugemmap37
 hugemmap38 hugemmap38
+hugemmap42 hugemmap42
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
testcases/kernel/mem/.gitignore +1 -0
index e63a6dde7..a0a877a58 100644
@@ -40,6 +40,7 @@
 /hugetlb/hugemmap/hugemmap36
 /hugetlb/hugemmap/hugemmap37
 /hugetlb/hugemmap/hugemmap38
+/hugetlb/hugemmap/hugemmap42
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
testcases/kernel/mem/hugetlb/hugemmap/Makefile +2 -0
index 6e72e7009..662b9c49d 100644
@@ -12,3 +12,5 @@ CFLAGS_no_stack_prot := $(filter-out -fstack-clash-protection, $(CFLAGS))
 
 hugemmap06: CFLAGS+=-pthread
 hugemmap34: CFLAGS=$(CFLAGS_no_stack_prot)
+hugemmap42: LDLIBS+=-lpthread
+hugemmap42: CFLAGS+=-pthread
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +294 -0

294 lines not on this page · raw message

Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Samir,

Quoted 4 lines · Samir Mulani

hugemmap: Migrate alloc-instantiate-race test from libhugetlbfs

Migrate the alloc-instantiate-race.c test from libhugetlbfs [1] to LTP as hugemmap36.

Could this be corrected to hugemmap42, which is the test added by this patch?

Quoted 1 line

+hugemmap42 hugemmap42

Could a second runtest entry exercise "-m private"? Without an option, setup() defaults to MAP_SHARED, so the new pthread path is not run by the hugetlb suite.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 57

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +1 -0
+	err = sched_setaffinity(getpid(), mask_size, cpuset);

Could this pass 0 as the pid? sched_setaffinity() applies the mask to the thread ID supplied in pid. In the MAP_PRIVATE path, getpid() identifies the thread-group leader for both pthreads, so both racers change the main thread's affinity and remain unpinned.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 225

Quoted 4 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +4 -0
+	p_sync = SAFE_MMAP(NULL, (totpages - 1) * hpage_size,
+			   PROT_READ | PROT_WRITE, MAP_SHARED, fd_sync, 0);
+
+	run_race(race_type);

Could each hugepage in p_sync be written before run_race()? mmap() without MAP_POPULATE does not fault these pages in. Consequently all free hugepages remain available to the racers instead of only the final page, and the allocation race is not exercised. The source test explicitly touches each page for this reason.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 262

Quoted 5 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +5 -0
+	if (p_sync != MAP_FAILED) {
+		unsigned long totpages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+
+		SAFE_MUNMAP(p_sync, totpages * tst_get_hugepage_size());
+	}

Could the exact length passed to mmap() be saved and reused here? The current free-page count is not the mapping length. On an abort with the current code it is one page larger, and munmap() may remove an adjacent mapping. After the pages are faulted in, a pre-existing hugepage pool can instead make it smaller and leave part of p_sync mapped.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 281

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +1 -0
+		{NULL, NULL, NULL}

Could this use the standard empty sentinel "{}"? check-hugemmap42 reports LTP-005 for this options array.

Verdict - Needs revision

---
Note:

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
author
#2 lore raw
Quoted 8 lines · On 19/08/26 1:57 am, [email protected] wrote:

Hi Samir,

Quoted 3 lines · Samir Mulani

hugemmap: Migrate alloc-instantiate-race test from libhugetlbfs Migrate the alloc-instantiate-race.c test from libhugetlbfs [1] to LTP as hugemmap36.

Could this be corrected to hugemmap42, which is the test added by this patch?

Yes, fixed the commit message. It now correctly says hugemmap42. updated in patch v7

Quoted 4 lines
Quoted 1 line

+hugemmap42 hugemmap42

Could a second runtest entry exercise "-m private"? Without an option, setup() defaults to MAP_SHARED, so the new pthread path is not run by the hugetlb suite.

Yes, added a second entry in runtest/hugetlb: updated in patch v7

Quoted 5 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 57

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +1 -0
+	err = sched_setaffinity(getpid(), mask_size, cpuset);

Could this pass 0 as the pid? sched_setaffinity() applies the mask to the thread ID supplied in pid. In the MAP_PRIVATE path, getpid() identifies the thread-group leader for both pthreads, so both racers change the main thread's affinity and remain unpinned.

Yes, fixed. Changed getpid() to 0.
With getpid(), both pthreads return the same thread-group ID, so both were pinning the main thread instead of themselves. Using 0 means "apply to the calling thread", which is correct for both the fork and pthread cases.

Quoted 9 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 225

Quoted 4 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +4 -0
+	p_sync = SAFE_MMAP(NULL, (totpages - 1) * hpage_size,
+			   PROT_READ | PROT_WRITE, MAP_SHARED, fd_sync, 0);
+
+	run_race(race_type);

Could each hugepage in p_sync be written before run_race()? mmap() without MAP_POPULATE does not fault these pages in. Consequently all free hugepages remain available to the racers instead of only the final page, and the allocation race is not exercised. The source test explicitly touches each page for this reason.

Yes, fixed. Added a loop that writes one byte to each sync page before calling run_race():

for (unsigned long i = 0; i < totpages - 1; i++) {
    volatile char *cp = (volatile char *)p_sync + i * hpage_size;
    *cp = 0;
}
Without this, mmap() does not actually take hugepages from the pool (no MAP_POPULATE), so all pages were still free when the race started and the race condition was never triggered. This was the most important fix.

Quoted 10 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 262

Quoted 5 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +5 -0
+	if (p_sync != MAP_FAILED) {
+		unsigned long totpages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+
+		SAFE_MUNMAP(p_sync, totpages * tst_get_hugepage_size());
+	}

Could the exact length passed to mmap() be saved and reused here? The current free-page count is not the mapping length. On an abort with the current code it is one page larger, and munmap() may remove an adjacent mapping. After the pages are faulted in, a pre-existing hugepage pool can instead make it smaller and leave part of p_sync mapped.

Yes, fixed. Added a global sync_mapping_size, set it at mmap() time and reused it in both run_test() and cleanup(). This avoids using SAFE_READ_MEMINFO() in cleanup() which can return a different value than what was actually mapped

Quoted 3 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c line 281

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap42.c new file +1 -0
+		{NULL, NULL, NULL}

Could this use the standard empty sentinel "{}"? check-hugemmap42 reports LTP-005 for this options array.

Yes, fixed. Changed {NULL, NULL, NULL} to {}.

Quoted 6 lines

Verdict - Needs revision

---
Note:

Quoted 6 lines

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

v7 patch will follow shortly.

Regards,
Samir

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp