[LTP] [RFC PATCH 2/3] memfd_create03: make use of new .request_hugepages

Li Wang liwang@redhat.com
Thu Apr 28 15:29:48 CEST 2022


To get rid of some failures like:

  memfd_create03.c:179: TINFO: --TESTING PAGE SIZE OF CREATED FILE--
  memfd_create03.c:184: TINFO: memfd_create() succeeded
  memfd_create03.c:47: TBROK: mmap((nil),1048576,2,2,3,0) failed: ENOMEM (12)

Signed-off-by: Li Wang <liwang@redhat.com>
---
 .../syscalls/memfd_create/memfd_create03.c    | 77 ++-----------------
 1 file changed, 7 insertions(+), 70 deletions(-)

diff --git a/testcases/kernel/syscalls/memfd_create/memfd_create03.c b/testcases/kernel/syscalls/memfd_create/memfd_create03.c
index 036182f0a..400a56854 100644
--- a/testcases/kernel/syscalls/memfd_create/memfd_create03.c
+++ b/testcases/kernel/syscalls/memfd_create/memfd_create03.c
@@ -32,14 +32,6 @@
 #include <stdio.h>
 #include <errno.h>
 
-#define TOTAL_HP_PATH "/proc/sys/vm/nr_hugepages"
-#define MEMINFO_PATH "/proc/meminfo"
-#define FREE_HP "HugePages_Free:\t%ld"
-#define DEFAULT_HPS "Hugepagesize:\t%ld kB"
-
-static int hugepages_allocated;
-static long og_total_pages;
-
 static void *check_huge_mmapable(int fd, unsigned long size)
 {
 	void *mem;
@@ -88,7 +80,7 @@ static void test_def_pagesize(int fd)
 	long hps;
 	void *mem;
 
-	SAFE_FILE_LINES_SCANF(MEMINFO_PATH, DEFAULT_HPS, &hps);
+	hps = SAFE_READ_MEMINFO("Hugepagesize:");
 	hps = hps << 10;
 	unmap_size = hps / 4;
 	mem = check_huge_mmapable(fd, hps);
@@ -128,8 +120,8 @@ static void test_max_hugepages(int fd)
 	void *mem;
 	void *new_mem;
 
-	SAFE_FILE_LINES_SCANF(MEMINFO_PATH, FREE_HP, &free_pages);
-	SAFE_FILE_LINES_SCANF(MEMINFO_PATH, DEFAULT_HPS, &hps);
+	free_pages = SAFE_READ_MEMINFO("HugePages_Free:");
+	hps = SAFE_READ_MEMINFO("Hugepagesize:");
 	hps = hps << 10;
 	mem = check_huge_mmapable(fd, free_pages * hps);
 
@@ -188,68 +180,13 @@ static void memfd_huge_controller(unsigned int n)
 	SAFE_CLOSE(fd);
 }
 
-static void setup(void)
-{
-	char buf[8];
-	int fd;
-	long free_pages;
-	long total_pages;
-
-	if (access(MEMINFO_PATH, F_OK) ||
-	    access("/sys/kernel/mm/hugepages", F_OK) ||
-	    access(TOTAL_HP_PATH, F_OK))
-		tst_brk(TCONF, "Huge page is not supported");
-
-	SAFE_FILE_LINES_SCANF(MEMINFO_PATH, FREE_HP, &free_pages);
-	if (free_pages > 0)
-		return;
-
-	SAFE_FILE_LINES_SCANF(TOTAL_HP_PATH, "%ld", &og_total_pages);
-	sprintf(buf, "%ld", og_total_pages + 1);
-
-	fd = SAFE_OPEN(TOTAL_HP_PATH, O_RDWR | O_TRUNC);
-
-	if (write(fd, buf, strlen(buf)) == -1)
-		tst_brk(TCONF | TERRNO,
-			"write() fail: Hugepage allocation failed");
-
-	SAFE_CLOSE(fd);
-
-	SAFE_FILE_LINES_SCANF(TOTAL_HP_PATH, "%ld", &total_pages);
-	if (total_pages != (og_total_pages + 1))
-		tst_brk(TCONF, "Hugepage allocation failed");
-
-	hugepages_allocated = 1;
-}
-
-static void cleanup(void)
-{
-	char buf[8];
-	int fd;
-	long total_pages;
-
-	if (hugepages_allocated == 0)
-		return;
-
-	sprintf(buf, "%ld", og_total_pages);
-
-	fd = SAFE_OPEN(TOTAL_HP_PATH, O_RDWR | O_TRUNC);
-
-	if (write(fd, buf, strlen(buf)) == -1)
-		tst_brk(TCONF | TERRNO, "Clean-up failed: write() failed");
-
-	SAFE_CLOSE(fd);
-
-	SAFE_FILE_LINES_SCANF(TOTAL_HP_PATH, "%ld", &total_pages);
-	if (og_total_pages != total_pages)
-		tst_brk(TCONF, "Clean-up failed");
-}
-
 static struct tst_test test = {
-	.setup = setup,
 	.test = memfd_huge_controller,
 	.tcnt = ARRAY_SIZE(tcases),
 	.needs_root = 1,
 	.min_kver = "4.14",
-	.cleanup = cleanup,
+	.request_hugepages = (struct tst_hugepage []){
+		{1, TST_ENFORCED},
+		{}
+	},
 };
-- 
2.35.1



More information about the ltp mailing list