Skip to contentSkip to conversation

selftests/mm: hugetlb_madv_vs_map: add underflow test

PATCH Superseded Guillaume Morin 3 participants 3 messages Sep 4 – Sep 10

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 selftests/mm: hugetlb_madv_vs_map: add underflow test

Add a test that checks for underflows when a parent unmaps the page first. Also check that when the child exits the reserve count is correct.

---
v4: address Mike's comments
v3: address Mike's comments
v2: refactor tests into 2 functions

 .../testing/selftests/mm/hugepage_settings.c  |   9 ++
 .../testing/selftests/mm/hugepage_settings.h  |   1 +
 .../selftests/mm/hugetlb_madv_vs_map.c        | 145 +++++++++++++++---
 3 files changed, 134 insertions(+), 21 deletions(-)
tools/testing/selftests/mm/hugepage_settings.c +9 -0
index d7917dce3aba..584054736ce9 100644
@@ -449,6 +449,15 @@ unsigned long hugetlb_free_pages(unsigned long size)
 	return read_num(path);
 }
 
+unsigned long hugetlb_nr_resv_pages(unsigned long size)
+{
+	char path[PATH_MAX];
+
+	hugetlb_sysfs_path(path, sizeof(path), size, "resv_hugepages");
+
+	return read_num(path);
+}
+
 static bool __hugetlb_setup(unsigned long size, unsigned long nr)
 {
 	unsigned long free = hugetlb_free_pages(size);
tools/testing/selftests/mm/hugepage_settings.h +1 -0
index 726c73c43c05..548e9d288d1d 100644
@@ -98,6 +98,7 @@ unsigned long default_huge_page_size(void);
 unsigned long hugetlb_nr_pages(unsigned long size);
 void hugetlb_set_nr_pages(unsigned long size, unsigned long nr);
 unsigned long hugetlb_free_pages(unsigned long size);
+unsigned long hugetlb_nr_resv_pages(unsigned long size);
 
 static inline void hugetlb_save_settings(void)
 {
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +124 -21

181 lines not on this page · raw message

Signature · 5 lines
2.39.1


-- 
Guillaume Morin <[email protected]>
reviewer Reviewed-by on v4 1/1
#1 lore raw
Quoted 8 lines · Guillaume Morin

Add a test that checks for underflows when a parent unmaps the page first. Also check that when the child exits the reserve count is correct.

With some small nits below fixed

tools/testing/selftests/mm/hugetlb_madv_vs_map.c int main(void) · line 101

Quoted 11 lines

---
v4: address Mike's comments
v3: address Mike's comments
v2: refactor tests into 2 functions

tools/testing/selftests/mm/hugetlb_madv_vs_map.c +1 -1
@@ -100,7 +98,7 @@ int main(void)
 				-1, 0);
 
 		if ((unsigned long)huge_ptr == -1)
-			ksft_exit_fail_msg("Failed to allocate huge page\n");
+			ksft_exit_fail_perror("Failed to allocate huge page\n");

Nit: _perror() does not need "\n".

Quoted 13 lines
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +7 -0
 		pthread_create(&thread1, NULL, madv, NULL);
 		pthread_create(&thread2, NULL, touch, NULL);
@@ -120,5 +118,110 @@ int main(void)
 	}
 
 	ksft_test_result_pass("No unexpected huge page allocations\n");
+}
+
+/*  We create a child process, then unmap the page in the parent while the child
+ *  waits and verify that there is no underflow of the reserved count. We also
+ *  verify that after the child exits, the reserved count is properly restored.
+ */
+void test_underflow(void)

tools/testing/selftests/mm/hugetlb_madv_vs_map.c int main(void) · line 137

Quoted 10 lines
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +10 -0
+{
+	pid_t pid;
+	int pipe_fds[2];
+	unsigned long nr_reserved = 0;
+
+	huge_ptr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
+			MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
+
+	if ((unsigned long)huge_ptr == -1)
+		ksft_exit_fail_perror("Failed to allocate huge page\n");

Ditto.

Quoted 6 lines
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +6 -0
+
+	nr_reserved = hugetlb_nr_resv_pages(default_huge_page_size());
+	if (nr_reserved != 1)
+		ksft_exit_fail_msg("Unexpected number of reserved pages: %lu, expected 1\n",
+				   nr_reserved);
+
Signature · 2 lines
Sincerely yours,
Mike.
Reviewed-by on v4 1/1
#2 lore raw
In reply to Guillaume Morin
Quoted 6 lines · Guillaume Morin

Add a test that checks for underflows when a parent unmaps the page first. Also check that when the child exits the reserve count is correct.

it seems you are missing a > at the end of your email.

Quoted 2 lines
Cc Breno Leitao <[email protected]>
Cc Mike Rapoport <[email protected]>
Quoted 15 lines
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +0 -7
index f94549efcc6f..d89b2c27c2ae 100644
@@ -3,25 +3,16 @@
  * A test case that must run on a system with one and only one huge page available.
  *	# echo 1 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
  *
- * During setup, the test allocates the only available page, and starts three threads:
- *  - thread1:
- *	* madvise(MADV_DONTNEED) on the allocated huge page
- *  - thread 2:
- *	* Write to the allocated huge page
- *  - thread 3:
- *	* Try to allocated an extra huge page (which must not available)

tools/testing/selftests/mm/hugetlb_madv_vs_map.c line 10

Quoted 10 lines
tools/testing/selftests/mm/hugetlb_madv_vs_map.c +2 -5
- *
- *  The test fails if thread3 is able to allocate a page.
- *
- *  Touching the first page after thread3's allocation will raise a SIGBUS
- *
  *  Author: Breno Leitao <[email protected]>
  */
+#include <limits.h>
 #include <pthread.h>
+#include <signal.h>

nit: Are limits.h and signal.h ever used by yor new code?