Skip to contentSkip to conversation

hugetlb/hugefallocate: Add hugefallocate03 to stress test fallocate on hugetlbfs

PATCH Superseded Pavithra 2 participants 2 messages Sep 19

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Pavithra

author
#0 lore raw

PATCH v7 1/1 hugetlb/hugefallocate: Add hugefallocate03 to stress test fallocate on hugetlbfs

Stress test fallocate() on hugetlbfs with three concurrent threads:
one continuously punches and fills holes, one faults in hugepages, and one continuously mmaps and munmaps the same file range. Verify no hugepage leak occurs after the stress run.

---
v6 -> v7:
- Add volatile thread_err flag to propagate fallocate() failures
from thread_fallocate to the final pass/fail verdict
- Add pthread_mutex_t guards for fault_mmap_addr and mmap_munmap_addr
to fix data races between threads and their cleanup handlers
- Change .hugepages = {2, ...} to {MAX_PAGES_TO_USE, ...}
Link to v6: https://lore.kernel.org/ltp/[email protected]/

---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../hugetlb/hugefallocate/hugefallocate03.c   | 233 ++++++++++++++++++
 3 files changed, 235 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c
runtest/hugetlb +1 -0
index 621c9718a..cd1c2794e 100644
@@ -1,5 +1,6 @@
 hugefallocate01 hugefallocate01
 hugefallocate02 hugefallocate02
+hugefallocate03 hugefallocate03
 
 hugefork01 hugefork01
 hugefork02 hugefork02
testcases/kernel/mem/.gitignore +1 -0
index 9e706e1c8..3086d1ac6 100644
@@ -2,6 +2,7 @@
 /cpuset/cpuset02
 /hugetlb/hugefallocate/hugefallocate01
 /hugetlb/hugefallocate/hugefallocate02
+/hugetlb/hugefallocate/hugefallocate03
 /hugetlb/hugefork/hugefork01
 /hugetlb/hugefork/hugefork02
 /hugetlb/hugemmap/hugemmap01
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +233 -0

233 lines not on this page · raw message

Signature · 5 lines
2.55.0


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

Hi Pavithra,

Quoted 8 lines · Pavithra

hugetlb/hugefallocate: Add hugefallocate03 to stress test fallocate on hugetlbfs

+/*\
+ * Stress test fallocate. This test starts three threads.
+ * Thread one will continually punch/fill holes via falloc.
+ * Thread two will continually fault in those same pages.
+ * Thread three will continually mmap/munmap that page range.
+ */

Format syscall names using the :manpage: role (e.g. :manpage:fallocate(2)``, :manpage:mmap(2)``, :manpage:munmap(2)``) and avoid abbreviations like "falloc". Document why root privileges are required (.needs_root = 1).

testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c line 100

Quoted 9 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +9 -0
+	while (1) {
+		tpage = ((long long)random()) % (max_hpages);
+		pthread_mutex_lock(&fault_addr_lock);
+		foo = *((char *)(fault_mmap_addr + (tpage * hpage_size)));
+		*((char *)(fault_mmap_addr + (tpage * hpage_size))) = foo;
+		pthread_mutex_unlock(&fault_addr_lock);
+
+		nanosleep(&ts, NULL); /* thread cancellation point */
+	}

fault_addr_lock is not accessed by any other thread and can be removed. Cast to volatile char * when writing back to prevent the compiler from optimizing out the write. Replace nanosleep(&ts, NULL) with pthread_testcancel() for thread cancellation.

testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c line 139

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +10 -0
+	while (1) {
+		pthread_mutex_lock(&mmap_munmap_lock);
+		mmap_munmap_addr = SAFE_MMAP(NULL, max_hpages * hpage_size,
+				PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+		SAFE_MUNMAP(mmap_munmap_addr, max_hpages * hpage_size);
+		mmap_munmap_addr = NULL;
+		pthread_mutex_unlock(&mmap_munmap_lock);
+
+		nanosleep(&ts, NULL);   /* thread cancellation point */
+	}

mmap_munmap_lock is unnecessary since no other thread accesses this address; mmap_munmap_addr can be a local variable. Replace nanosleep(&ts, NULL) with pthread_testcancel().

testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c line 159

Quoted 4 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +4 -0
+	unsigned int seed = (int)getpid() * time(NULL);
+
+	srandom(seed);
+	tst_res(TINFO, "Seed = %d", seed);

seed is unsigned int; use %u instead of %d.

Quoted 5 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +5 -0
+	/* First preallocate file with max_hpages pages */
+	err = fallocate(fd, 0, 0, hpage_size * max_hpages);
+	if (err) {
+		if (errno == EOPNOTSUPP)
+			tst_brk(TCONF, "fallocate() Operation is not supported");

testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c line 179

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +10 -0
+		tst_res(TFAIL | TERRNO, "fallocate():");
+		goto windup;
+	}
+
+	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+	if (free_before - free_after != max_hpages) {
+		tst_res(TFAIL, "fallocate did not preallocate %ld huge pages",
+				max_hpages);
+		goto windup;
+	}

If fallocate or preallocation fails, branching to windup causes the test to report TPASS in the subsequent check because SAFE_CLOSE(fd) restores the free counts and thread_err remains 0. Use tst_brk(TBROK | TERRNO, ...) to abort on preallocation failure. Also use %lu instead of %ld for max_hpages.

testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c line 210

Quoted 12 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +12 -0
+windup:
+	SAFE_CLOSE(fd);
+
+	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+	rsvd_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+
+	if (thread_err)
+		tst_res(TFAIL, "fallocate() failed during stress, see above");
+	else if (free_after != free_before || rsvd_after != rsvd_before)
+		tst_res(TFAIL, "free or reserve counts incorrect after fallocate stress test");
+	else
+		tst_res(TPASS, "fallocate stress test passed");

Use TST_EXP_EQ_LU(free_after, free_before) and TST_EXP_EQ_LU(rsvd_after, rsvd_before) to report free and reserve count comparisons.

testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c line 222

Quoted 5 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +5 -0
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}

Use if (fd != -1) instead of if (fd >= 0).

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