Skip to contentSkip to conversation

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

PATCH Superseded Pavithra 3 participants 3 messages Sep 23 – Nov 2

v9 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Pavithra

author
#0 lore raw

PATCH v9 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.

---
v8 -> v9:
- Add missing #include <time.h> for time()
- Remove trailing colons from fallocate() error format strings
- Remove thread_err flag; return early on fallocate failure to avoid
log flooding; tst_res() already marks the test as failed
- Use SAFE_MUNMAP() instead of plain munmap() in thread_fault_cleanup()
- Assign free_before = nr_hpages_free to avoid redundant meminfo read
- Change TST_NEEDS to TST_REQUEST in .hugepages to avoid unnecessary
TCONF on systems with large hugepage sizes or limited memory
Link to v8: https://lore.kernel.org/ltp/[email protected]/

---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../hugetlb/hugefallocate/hugefallocate03.c   | 214 ++++++++++++++++++
 3 files changed, 216 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 +214 -0

214 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,

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

Quoted 6 lines · Pavithra

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

testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +4 -0
+static int fd = -1;
+static unsigned long max_hpages;
+static long hpage_size;
+static void *thread_fallocate(void *arg)

Add a blank line between variable declarations and function definitions.

Quoted 5 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +5 -0
+	SAFE_SIGACTION(SIGBUS, &sa, NULL);
+
+	fault_mmap_addr = SAFE_MMAP(NULL, max_hpages * hpage_size,
+			PROT_READ | PROT_WRITE, MAP_SHARED,
+			fd, 0);

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

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +10 -0
+
+	pthread_cleanup_push(thread_fault_cleanup, NULL);
+
+	while (1) {
+		tpage = ((long long)random()) % (max_hpages);
+
+		if (sigsetjmp(fault_jmp, 1) == 0) {
+			foo = *((char *)(fault_mmap_addr + (tpage * hpage_size)));
+			*((volatile char *)(fault_mmap_addr + (tpage * hpage_size))) = foo;
+		}

Save the original SIGBUS handler and restore it upon thread termination or in cleanup. If SIGBUS is raised after this thread terminates, siglongjmp() jumps to an invalid stack.

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

Quoted 8 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +8 -0
+	while (1) {
+		addr = mmap(NULL, max_hpages * hpage_size,
+				PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+		if (addr != MAP_FAILED)
+			SAFE_MUNMAP(addr, max_hpages * hpage_size);
+
+		pthread_testcancel();
+	}

Use SAFE_MMAP() instead of mmap() and drop the MAP_FAILED check.

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

Quoted 7 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +7 -0
+	nr_hpages_free = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+	max_hpages = MIN(nr_hpages_free, MAX_PAGES_TO_USE);
+	free_before = nr_hpages_free;
+	rsvd_before = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+
+	/* First preallocate file with max_hpages pages */
+	err = fallocate(fd, 0, 0, hpage_size * max_hpages);

Because .hugepages uses TST_REQUEST, nr_hpages_free and max_hpages can be 0. When max_hpages is 0, fallocate() fails with EINVAL (causing TBROK instead of TCONF), and the worker threads divide by zero. Add a check to abort with tst_brk(TCONF, ...) when max_hpages is 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
#2 lore raw

Hi!

Quoted 11 lines
Quoted 11 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +11 -0
+	SAFE_SIGACTION(SIGBUS, &sa, NULL);
+
+	fault_mmap_addr = SAFE_MMAP(NULL, max_hpages * hpage_size,
+			PROT_READ | PROT_WRITE, MAP_SHARED,
+			fd, 0);
+
+	pthread_cleanup_push(thread_fault_cleanup, NULL);
+
+	while (1) {
+		tpage = ((long long)random()) % (max_hpages);
+
Quoted 8 lines

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

Quoted 4 lines
testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate03.c new file +4 -0
+		if (sigsetjmp(fault_jmp, 1) == 0) {
+			foo = *((char *)(fault_mmap_addr + (tpage * hpage_size)));
+			*((volatile char *)(fault_mmap_addr + (tpage * hpage_size))) = foo;
+		}

Save the original SIGBUS handler and restore it upon thread termination or in cleanup. If SIGBUS is raised after this thread terminates, siglongjmp() jumps to an invalid stack.

More generally we can just remove all the thread cleanup registration and cancelation points if we just do:

static volatile int run;

...

void *pthread_func(void *)

{
	...

	while (run) {
		...
	}

	SAFE_MUNMAP(...);
	reutrn NULL;
}

The main thread can then stop all three threads by setting run = 0 and the cleanup runs without the pthread cleanup complexity.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp