public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages
@ 2022-04-28 13:29 Li Wang
  2022-04-28 13:29 ` [LTP] [RFC PATCH 2/3] memfd_create03: make use of new .request_hugepages Li Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Li Wang @ 2022-04-28 13:29 UTC (permalink / raw)
  To: ltp

This is to satisfy some tests which need to reserved hugepage precisely
for using, with eliminating other process side effects at the same time.

For example, if there are 'N' (N > 1) hpages reserved but all occupying.
New '.request_hugepage = 1' in another test will only save the N and do
echo 1 into hugetlbfs. That obviously may cause problems during test run.

Here, we introduce two policies to make hugepage reserve work fit for
more requirements but no need to care about details.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 doc/c-test-api.txt                    | 33 +++++++++++++++++++--------
 include/tst_hugepage.h                | 12 +++++++++-
 include/tst_test.h                    | 27 +++++++++++++++-------
 lib/newlib_tests/test20.c             | 13 +++++++----
 lib/newlib_tests/test_zero_hugepage.c |  8 +++++--
 lib/tst_hugepage.c                    | 22 ++++++++++++++----
 6 files changed, 86 insertions(+), 29 deletions(-)

diff --git a/doc/c-test-api.txt b/doc/c-test-api.txt
index 9f104ecd7..1eabdf7ca 100644
--- a/doc/c-test-api.txt
+++ b/doc/c-test-api.txt
@@ -1976,15 +1976,25 @@ For full documentation see the comments in 'include/tst_fuzzy_sync.h'.
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
 Many of the LTP tests need to use hugepage in their testing, this allows the
-test can reserve hugepages from system only via '.request_hugepages = xx'.
+test can reserve hugepages from system only via '.request_hugepages'.
 
-If set non-zero number of 'request_hugepages', test will try to reserve the
-expected number of hugepage for testing in setup phase. If system does not
-have enough hpage for using, it will try the best to reserve 80% available
-number of hpages. With success test stores the reserved hugepage number in
-'tst_hugepages'. For the system without hugetlb supporting, variable
-'tst_hugepages' will be set to 0. If the hugepage number needs to be set to 0
-on supported hugetlb system, please use '.request_hugepages = TST_NO_HUGEPAGES'.
+We simply achieved two policies for hugepage reserving:
+
+TST_FLEXIBLE:
+  If set non-zero number in .request_hugepages, ltp will try to reserve the
+  expected number of hugepage for testing in setup phase. If system does not
+  have enough memory for that, it will try the best to reserve 80% available
+  huge pages.
+
+TST_ENFORCED:
+  This is an enforced requirement for huge page reserve, ltp should strictly do
+  hpages applying and guarantee the 'HugePages_Free' no less than specified pages
+  which gives that test can use these specified numbers correctly.
+
+With success test stores the reserved hugepage number in 'tst_hugepages'. For
+system without hugetlb supporting, variable 'tst_hugepages' will be set to 0.
+If the hugepage number needs to be set to 0 on supported hugetlb system, please
+use '.request_hugepages = TST_NO_HUGEPAGES'.
 
 Also, we do cleanup and restore work for the hpages resetting automatically.
 
@@ -1996,7 +2006,7 @@ static void run(void)
 {
 	...
 
-	if (tst_hugepages == test.request_hugepages)
+	if (tst_hugepages == test.request_hugepages->number)
 		TEST(do_hpage_test);
 	else
 		...
@@ -2005,7 +2015,10 @@ static void run(void)
 
 struct tst_test test = {
 	.test_all = run,
-	.request_hugepages = 2,
+	.request_hugepages = (struct tst_hugepage []){
+		{2, TST_FLEXIBLE},
+		{}
+	},
 	...
 };
 -------------------------------------------------------------------------------
diff --git a/include/tst_hugepage.h b/include/tst_hugepage.h
index e08a2daa2..17bd4e9da 100644
--- a/include/tst_hugepage.h
+++ b/include/tst_hugepage.h
@@ -12,6 +12,16 @@
 extern char *nr_opt; /* -s num   Set the number of the been allocated hugepages */
 extern char *Hopt;   /* -H /..   Location of hugetlbfs, i.e.  -H /var/hugetlbfs */
 
+enum tst_hp_policy {
+	TST_ENFORCED,
+	TST_FLEXIBLE,
+};
+
+struct tst_hugepage {
+	const unsigned long number;
+	enum  tst_hp_policy policy;
+};
+
 /*
  * Get the default hugepage size. Returns 0 if hugepages are not supported.
  */
@@ -23,7 +33,7 @@ size_t tst_get_hugepage_size(void);
  *
  * Note: this depend on the status of system memory fragmentation.
  */
-unsigned long tst_request_hugepages(unsigned long hpages);
+unsigned long tst_request_hugepages(struct tst_hugepage *hp);
 
 /*
  * This variable is used for recording the number of hugepages which system can
diff --git a/include/tst_test.h b/include/tst_test.h
index dbe303bc8..d50d213ef 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -189,17 +189,28 @@ struct tst_test {
 	unsigned long min_mem_avail;
 
 	/*
-	 * If set non-zero number of request_hugepages, test will try to reserve the
-	 * expected number of hugepage for testing in setup phase. If system does not
-	 * have enough hpage for using, it will try the best to reserve 80% available
-	 * number of hpages. With success test stores the reserved hugepage number in
-	 * 'tst_hugepages. For the system without hugetlb supporting, variable
-	 * 'tst_hugepages' will be set to 0. If the hugepage number needs to be set to
-	 * 0 on supported hugetlb system, please use '.request_hugepages = TST_NO_HUGEPAGES'.
+	 * Two policies for hugepage reserving:
+	 *
+	 * TST_FLEXIBLE:
+	 *   If set non-zero number in .request_hugepages, ltp will try to reserve the
+	 *   expected number of hugepage for testing in setup phase. If system does not
+	 *   have enough memory for that, it will try the best to reserve 80% available
+	 *   huge pages.
+	 *
+	 * TST_ENFORCED:
+	 *   This is an enforced requirement for huge page reserve, ltp should strictly do
+	 *   hpages applying and guarantee the 'HugePages_Free' no less than specified pages
+	 *   which gives that test can use these specified numbers correctly.
+	 *
+	 *
+	 * With success test stores the reserved hugepage number in 'tst_hugepages. For
+	 * the system without hugetlb supporting, variable 'tst_hugepages' will be set to 0.
+	 * If the hugepage number needs to be set to 0 on supported hugetlb system, please
+	 * use '.request_hugepages = TST_NO_HUGEPAGES'.
 	 *
 	 * Also, we do cleanup and restore work for the hpages resetting automatically.
 	 */
-	unsigned long request_hugepages;
+	struct tst_hugepage *request_hugepages;
 
 	/*
 	 * If set to non-zero, call tst_taint_init(taint_check) during setup
diff --git a/lib/newlib_tests/test20.c b/lib/newlib_tests/test20.c
index 5c24770a1..e8e81f8f4 100644
--- a/lib/newlib_tests/test20.c
+++ b/lib/newlib_tests/test20.c
@@ -18,14 +18,16 @@ static void do_test(void) {
 	tst_res(TINFO, "tst_hugepages = %lu", tst_hugepages);
 	SAFE_FILE_PRINTF("/proc/sys/kernel/numa_balancing", "1");
 
-	hpages = test.request_hugepages;
+	hpages = test.request_hugepages->number;
 	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%lu", &val);
 	if (val != hpages)
 		tst_brk(TBROK, "nr_hugepages = %lu, but expect %lu", val, hpages);
 	else
-		tst_res(TPASS, "test .needs_hugepges");
+		tst_res(TPASS, "test .request_hugepges");
+
+	struct tst_hugepage hp = { 1000000000000, TST_FLEXIBLE };
+	hpages = tst_request_hugepages(&hp);
 
-	hpages = tst_request_hugepages(3);
 	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%lu", &val);
 	if (val != hpages)
 		tst_brk(TBROK, "nr_hugepages = %lu, but expect %lu", val, hpages);
@@ -35,7 +37,10 @@ static void do_test(void) {
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.request_hugepages = 2,
+	.request_hugepages = (struct tst_hugepage []){
+		{2, TST_ENFORCED},
+		{}
+	},
 	.save_restore = (const struct tst_path_val[]) {
 		{"!/proc/sys/kernel/numa_balancing", "0"},
 		{}
diff --git a/lib/newlib_tests/test_zero_hugepage.c b/lib/newlib_tests/test_zero_hugepage.c
index 0d85ce866..27cd196da 100644
--- a/lib/newlib_tests/test_zero_hugepage.c
+++ b/lib/newlib_tests/test_zero_hugepage.c
@@ -21,7 +21,8 @@ static void do_test(void)
 	else
 		tst_res(TPASS, "test .request_hugepages = TST_NO_HUGEPAGES");
 
-	hpages = tst_request_hugepages(3);
+	struct tst_hugepage hp = { 3, TST_FLEXIBLE };
+	hpages = tst_request_hugepages(&hp);
 	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%lu", &val);
 	if (val != hpages)
 		tst_brk(TBROK, "nr_hugepages = %lu, but expect %lu", val, hpages);
@@ -31,5 +32,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.request_hugepages = TST_NO_HUGEPAGES,
+	.request_hugepages = (struct tst_hugepage []){
+		{TST_NO_HUGEPAGES, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/lib/tst_hugepage.c b/lib/tst_hugepage.c
index a7585bc3d..06e9fb7a1 100644
--- a/lib/tst_hugepage.c
+++ b/lib/tst_hugepage.c
@@ -20,11 +20,13 @@ size_t tst_get_hugepage_size(void)
 	return SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 }
 
-unsigned long tst_request_hugepages(unsigned long hpages)
+unsigned long tst_request_hugepages(struct tst_hugepage *hp)
 {
 	unsigned long val, max_hpages;
 
 	if (access(PATH_HUGEPAGES, F_OK)) {
+		if (hp->policy == TST_ENFORCED)
+			tst_brk(TCONF, "hugetlbfs is not supported");
 		tst_hugepages = 0;
 		goto out;
 	}
@@ -32,16 +34,20 @@ unsigned long tst_request_hugepages(unsigned long hpages)
 	if (nr_opt)
 		tst_hugepages = SAFE_STRTOL(nr_opt, 1, LONG_MAX);
 	else
-		tst_hugepages = hpages;
+		tst_hugepages = hp->number;
 
-	if (hpages == TST_NO_HUGEPAGES) {
+	if (hp->number == TST_NO_HUGEPAGES) {
 		tst_hugepages = 0;
 		goto set_hugepages;
 	}
 
 	SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");
-	max_hpages = SAFE_READ_MEMINFO("MemFree:") / SAFE_READ_MEMINFO("Hugepagesize:");
+	if (hp->policy == TST_ENFORCED) {
+		tst_hugepages += SAFE_READ_MEMINFO("HugePages_Total:");
+		goto set_hugepages;
+	}
 
+	max_hpages = SAFE_READ_MEMINFO("MemFree:") / SAFE_READ_MEMINFO("Hugepagesize:");
 	if (tst_hugepages > max_hpages) {
 		tst_res(TINFO, "Requested number(%lu) of hugepages is too large, "
 				"limiting to 80%% of the max hugepage count %lu",
@@ -61,6 +67,14 @@ set_hugepages:
 				"Not enough hugepages for testing.",
 				val, tst_hugepages);
 
+	if (hp->policy == TST_ENFORCED) {
+		unsigned long free_hpages = SAFE_READ_MEMINFO("HugePages_Free:");
+		if (hp->number > free_hpages)
+			tst_brk(TCONF, "free_hpages = %lu, but expect %lu. "
+				"Not enough hugepages for testing.",
+				free_hpages, hp->number);
+	}
+
 	tst_res(TINFO, "%lu hugepage(s) reserved", tst_hugepages);
 out:
 	return tst_hugepages;
-- 
2.35.1


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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [RFC PATCH 2/3] memfd_create03: make use of new .request_hugepages
  2022-04-28 13:29 [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Li Wang
@ 2022-04-28 13:29 ` Li Wang
  2022-04-28 13:29 ` [LTP] [RFC PATCH 3/3] hugetlb: " Li Wang
  2022-06-01 12:08 ` [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Cyril Hrubis
  2 siblings, 0 replies; 5+ messages in thread
From: Li Wang @ 2022-04-28 13:29 UTC (permalink / raw)
  To: ltp

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


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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [RFC PATCH 3/3] hugetlb: make use of new .request_hugepages
  2022-04-28 13:29 [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Li Wang
  2022-04-28 13:29 ` [LTP] [RFC PATCH 2/3] memfd_create03: make use of new .request_hugepages Li Wang
@ 2022-04-28 13:29 ` Li Wang
  2022-06-01 12:08 ` [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Cyril Hrubis
  2 siblings, 0 replies; 5+ messages in thread
From: Li Wang @ 2022-04-28 13:29 UTC (permalink / raw)
  To: ltp

Signed-off-by: Li Wang <liwang@redhat.com>
---
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c   |  5 ++++-
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c   |  5 ++++-
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c   |  5 ++++-
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c   |  8 ++++----
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c   |  7 ++++---
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c |  5 ++++-
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c |  5 ++++-
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c |  5 ++++-
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c | 12 ++++++------
 testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c |  8 ++++----
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c     |  5 ++++-
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c     |  5 ++++-
 .../kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c     |  5 ++++-
 testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c |  5 ++++-
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget01.c     |  5 ++++-
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget02.c     |  5 ++++-
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget03.c     |  5 ++++-
 .../kernel/mem/hugetlb/hugeshmget/hugeshmget05.c     |  5 ++++-
 testcases/kernel/syscalls/futex/futex_wake04.c       |  8 ++++----
 testcases/kernel/syscalls/ipc/shmget/shmget02.c      |  5 ++++-
 testcases/kernel/syscalls/pkeys/pkey01.c             |  7 +++++--
 21 files changed, 87 insertions(+), 38 deletions(-)

diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c
index fcb4443f7..13b15924f 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap01.c
@@ -98,5 +98,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_hugemmap,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
index 3be68418a..c801d6ade 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
@@ -145,5 +145,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_hugemmap,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c
index 236010fe0..2104528a0 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap04.c
@@ -116,5 +116,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_hugemmap,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c
index 40d3bd8da..2d7cf87f0 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap05.c
@@ -185,9 +185,6 @@ static void setup(void)
 {
 	unsigned long hpages;
 
-	if (tst_hugepages != NR_HPAGES)
-		tst_brk(TCONF, "Not enough hugepages for testing!");
-
 	hugepagesize = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 	init_sys_sz_paths();
 
@@ -307,5 +304,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_overcommit,
-	.request_hugepages = NR_HPAGES,
+	.request_hugepages = (struct tst_hugepage []){
+		{NR_HPAGES, TST_ENFORCED},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c
index ab2ccc40b..f57470b39 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap06.c
@@ -39,8 +39,6 @@ struct mp {
 
 static void setup(void)
 {
-	if (tst_hugepages != test.request_hugepages)
-		tst_brk(TCONF, "System RAM is not enough to test.");
 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 }
 
@@ -122,7 +120,10 @@ static struct tst_test test = {
 	.needs_tmpdir = 1,
 	.test = do_mmap,
 	.setup = setup,
-	.request_hugepages = (ARSZ + 1) * LOOP,
+	.request_hugepages = (struct tst_hugepage []){
+		{(ARSZ + 1) * LOOP, TST_ENFORCED},
+		{}
+	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "f522c3ac00a4"},
 		{"linux-git", "9119a41e9091"},
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c
index 8273ede83..3258a3256 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat01.c
@@ -180,5 +180,8 @@ static struct tst_test test = {
 	.test = verify_hugeshmat,
 	.setup = setup,
 	.cleanup = cleanup,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c
index 11000a4f0..dc47769a7 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat02.c
@@ -107,5 +107,8 @@ static struct tst_test test = {
 	.test = verify_hugeshmat,
 	.setup = setup,
 	.cleanup = cleanup,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c
index 5aca7dab0..a170dd825 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat03.c
@@ -101,5 +101,8 @@ static struct tst_test test = {
 	.test_all = verify_hugeshmat,
 	.setup = setup,
 	.cleanup = cleanup,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
index 128671051..d24a3b837 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
@@ -83,9 +83,6 @@ static void setup(void)
 {
 	long hpage_size, orig_hugepages;
 
-	if (tst_hugepages == 0)
-		tst_brk(TCONF, "Not enough hugepages for testing.");
-
 	orig_hugepages = get_sys_tune("nr_hugepages");
 	SAFE_FILE_SCANF(PATH_SHMMAX, "%ld", &orig_shmmax);
 	SAFE_FILE_PRINTF(PATH_SHMMAX, "%ld", (long)SIZE);
@@ -96,8 +93,8 @@ static void setup(void)
 
 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 
-	hugepages = orig_hugepages + SIZE / hpage_size;
-	tst_request_hugepages(hugepages);
+	struct tst_hugepage hp = { orig_hugepages + SIZE / hpage_size, TST_FLEXIBLE };
+	tst_request_hugepages(&hp);
 	if (tst_hugepages != (unsigned long)hugepages)
 		tst_brk(TCONF, "No enough hugepages for testing.");
 }
@@ -121,5 +118,8 @@ static struct tst_test test = {
 	.min_mem_avail = 2048,
 	.setup = setup,
 	.cleanup = cleanup,
-	.request_hugepages = 1,
+	.request_hugepages = (struct tst_hugepage []){
+		{1, TST_ENFORCED},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c
index 7152e3363..93f85646f 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat05.c
@@ -35,9 +35,6 @@ static long hpage_size;
 
 void setup(void)
 {
-	if (tst_hugepages != test.request_hugepages)
-		tst_brk(TCONF, "Not enough hugepages for testing.");
-
 	page_size = getpagesize();
 	hpage_size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 }
@@ -91,7 +88,10 @@ static struct tst_test test = {
 	.needs_tmpdir = 1,
 	.test_all = test_hugeshmat,
 	.setup = setup,
-	.request_hugepages = N + 1,
+	.request_hugepages = (struct tst_hugepage []){
+		{N+1, TST_ENFORCED},
+		{}
+	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "091d0d55b286"},
 		{"linux-git", "af73e4d9506d"},
diff --git a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c
index 56f3a73dd..17e75f1cb 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl01.c
@@ -310,5 +310,8 @@ static struct tst_test test = {
 	.cleanup = cleanup,
 	.test = test_hugeshmctl,
 	.needs_checkpoints = 1,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c
index 8a4c8bc2d..33c61d676 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl02.c
@@ -110,5 +110,8 @@ static struct tst_test test = {
 	},
 	.setup = setup,
 	.cleanup = cleanup,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c
index f7dd43452..6c8fbb729 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmctl/hugeshmctl03.c
@@ -134,5 +134,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_hugeshmctl,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c b/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c
index 287e5990e..f5726c5f0 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmdt/hugeshmdt01.c
@@ -152,5 +152,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = hugeshmdt_test,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c
index 2a440f79d..5fd019fb6 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget01.c
@@ -78,5 +78,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_hugeshmget,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c
index 11497d150..fc25d2087 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget02.c
@@ -98,5 +98,8 @@ static struct tst_test test = {
 	.cleanup = cleanup,
 	.test = test_hugeshmget,
 	.tcnt = ARRAY_SIZE(tcases),
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c
index 72d8701f4..8c4f93a44 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget03.c
@@ -96,5 +96,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_hugeshmget,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c
index 91e30afa4..d0f9f6072 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmget/hugeshmget05.c
@@ -92,5 +92,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_hugeshmget,
-	.request_hugepages = 128,
+	.request_hugepages = (struct tst_hugepage []){
+		{128, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/syscalls/futex/futex_wake04.c b/testcases/kernel/syscalls/futex/futex_wake04.c
index 2260a3936..6c943d844 100644
--- a/testcases/kernel/syscalls/futex/futex_wake04.c
+++ b/testcases/kernel/syscalls/futex/futex_wake04.c
@@ -48,9 +48,6 @@ static struct futex_test_variants variants[] = {
 
 static void setup(void)
 {
-	if (tst_hugepages == 0)
-		tst_brk(TCONF, "No enough hugepages for testing.");
-
 	struct futex_test_variants *tv = &variants[tst_variant];
 
 	tst_res(TINFO, "Testing variant: %s", tv->desc);
@@ -135,5 +132,8 @@ static struct tst_test test = {
 	.needs_root = 1,
 	.min_kver = "2.6.32",
 	.needs_tmpdir = 1,
-	.request_hugepages = 1,
+	.request_hugepages = (struct tst_hugepage []){
+		{1, TST_ENFORCED},
+		{}
+	},
 };
diff --git a/testcases/kernel/syscalls/ipc/shmget/shmget02.c b/testcases/kernel/syscalls/ipc/shmget/shmget02.c
index 165a34456..30f829805 100644
--- a/testcases/kernel/syscalls/ipc/shmget/shmget02.c
+++ b/testcases/kernel/syscalls/ipc/shmget/shmget02.c
@@ -148,5 +148,8 @@ static struct tst_test test = {
 	.cleanup = cleanup,
 	.test = do_test,
 	.tcnt = ARRAY_SIZE(tcases),
-	.request_hugepages = TST_NO_HUGEPAGES,
+	.request_hugepages = (struct tst_hugepage []){
+		{TST_NO_HUGEPAGES, TST_FLEXIBLE},
+		{}
+	},
 };
diff --git a/testcases/kernel/syscalls/pkeys/pkey01.c b/testcases/kernel/syscalls/pkeys/pkey01.c
index 04f50924c..74eae0624 100644
--- a/testcases/kernel/syscalls/pkeys/pkey01.c
+++ b/testcases/kernel/syscalls/pkeys/pkey01.c
@@ -52,7 +52,7 @@ static void setup(void)
 
 	check_pkey_support();
 
-	if (tst_hugepages == test.request_hugepages)
+	if (tst_hugepages == test.request_hugepages->number)
 		size = SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
 	else
 		size = getpagesize();
@@ -221,5 +221,8 @@ static struct tst_test test = {
 	.forks_child = 1,
 	.test = verify_pkey,
 	.setup = setup,
-	.request_hugepages = 1,
+	.request_hugepages = (struct tst_hugepage []){
+		{1, TST_FLEXIBLE},
+		{}
+	},
 };
-- 
2.35.1


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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages
  2022-04-28 13:29 [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Li Wang
  2022-04-28 13:29 ` [LTP] [RFC PATCH 2/3] memfd_create03: make use of new .request_hugepages Li Wang
  2022-04-28 13:29 ` [LTP] [RFC PATCH 3/3] hugetlb: " Li Wang
@ 2022-06-01 12:08 ` Cyril Hrubis
  2022-06-02  4:24   ` Li Wang
  2 siblings, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2022-06-01 12:08 UTC (permalink / raw)
  To: Li Wang; +Cc: ltp

Hi!
> This is to satisfy some tests which need to reserved hugepage precisely
> for using, with eliminating other process side effects at the same time.
> 
> For example, if there are 'N' (N > 1) hpages reserved but all occupying.
> New '.request_hugepage = 1' in another test will only save the N and do
> echo 1 into hugetlbfs. That obviously may cause problems during test run.
> 
> Here, we introduce two policies to make hugepage reserve work fit for
> more requirements but no need to care about details.
> 
> Signed-off-by: Li Wang <liwang@redhat.com>
> ---
>  doc/c-test-api.txt                    | 33 +++++++++++++++++++--------
>  include/tst_hugepage.h                | 12 +++++++++-
>  include/tst_test.h                    | 27 +++++++++++++++-------
>  lib/newlib_tests/test20.c             | 13 +++++++----
>  lib/newlib_tests/test_zero_hugepage.c |  8 +++++--
>  lib/tst_hugepage.c                    | 22 ++++++++++++++----
>  6 files changed, 86 insertions(+), 29 deletions(-)
> 
> diff --git a/doc/c-test-api.txt b/doc/c-test-api.txt
> index 9f104ecd7..1eabdf7ca 100644
> --- a/doc/c-test-api.txt
> +++ b/doc/c-test-api.txt
> @@ -1976,15 +1976,25 @@ For full documentation see the comments in 'include/tst_fuzzy_sync.h'.
>  ~~~~~~~~~~~~~~~~~~~~~~~~
>  
>  Many of the LTP tests need to use hugepage in their testing, this allows the
> -test can reserve hugepages from system only via '.request_hugepages = xx'.
> +test can reserve hugepages from system only via '.request_hugepages'.
>  
> -If set non-zero number of 'request_hugepages', test will try to reserve the
> -expected number of hugepage for testing in setup phase. If system does not
> -have enough hpage for using, it will try the best to reserve 80% available
> -number of hpages. With success test stores the reserved hugepage number in
> -'tst_hugepages'. For the system without hugetlb supporting, variable
> -'tst_hugepages' will be set to 0. If the hugepage number needs to be set to 0
> -on supported hugetlb system, please use '.request_hugepages = TST_NO_HUGEPAGES'.
> +We simply achieved two policies for hugepage reserving:
> +
> +TST_FLEXIBLE:
> +  If set non-zero number in .request_hugepages, ltp will try to reserve the
> +  expected number of hugepage for testing in setup phase. If system does not
> +  have enough memory for that, it will try the best to reserve 80% available
> +  huge pages.
> +
> +TST_ENFORCED:
> +  This is an enforced requirement for huge page reserve, ltp should strictly do
> +  hpages applying and guarantee the 'HugePages_Free' no less than specified pages
> +  which gives that test can use these specified numbers correctly.

As for the naming I would probably go for TST_NEEDS and TST_REQUEST. We
use the word 'needs' in the tst_test to describe something that is
required for the test. And 'request' sounds better to me than flexible
in a sense that you can request a lot but get less than what was
requested.

> +With success test stores the reserved hugepage number in 'tst_hugepages'. For
> +system without hugetlb supporting, variable 'tst_hugepages' will be set to 0.
> +If the hugepage number needs to be set to 0 on supported hugetlb system, please
> +use '.request_hugepages = TST_NO_HUGEPAGES'.
>  
>  Also, we do cleanup and restore work for the hpages resetting automatically.
>  
> @@ -1996,7 +2006,7 @@ static void run(void)
>  {
>  	...
>  
> -	if (tst_hugepages == test.request_hugepages)
> +	if (tst_hugepages == test.request_hugepages->number)
>  		TEST(do_hpage_test);
>  	else
>  		...
> @@ -2005,7 +2015,10 @@ static void run(void)
>  
>  struct tst_test test = {
>  	.test_all = run,
> -	.request_hugepages = 2,
> +	.request_hugepages = (struct tst_hugepage []){
> +		{2, TST_FLEXIBLE},
> +		{}
> +	},
>  	...
>  };
>  -------------------------------------------------------------------------------
> diff --git a/include/tst_hugepage.h b/include/tst_hugepage.h
> index e08a2daa2..17bd4e9da 100644
> --- a/include/tst_hugepage.h
> +++ b/include/tst_hugepage.h
> @@ -12,6 +12,16 @@
>  extern char *nr_opt; /* -s num   Set the number of the been allocated hugepages */
>  extern char *Hopt;   /* -H /..   Location of hugetlbfs, i.e.  -H /var/hugetlbfs */
>  
> +enum tst_hp_policy {
> +	TST_ENFORCED,
> +	TST_FLEXIBLE,
> +};
> +
> +struct tst_hugepage {
> +	const unsigned long number;
> +	enum  tst_hp_policy policy;
> +};
> +
>  /*
>   * Get the default hugepage size. Returns 0 if hugepages are not supported.
>   */
> @@ -23,7 +33,7 @@ size_t tst_get_hugepage_size(void);
>   *
>   * Note: this depend on the status of system memory fragmentation.
>   */
> -unsigned long tst_request_hugepages(unsigned long hpages);
> +unsigned long tst_request_hugepages(struct tst_hugepage *hp);
>  
>  /*
>   * This variable is used for recording the number of hugepages which system can
> diff --git a/include/tst_test.h b/include/tst_test.h
> index dbe303bc8..d50d213ef 100644
> --- a/include/tst_test.h
> +++ b/include/tst_test.h
> @@ -189,17 +189,28 @@ struct tst_test {
>  	unsigned long min_mem_avail;
>  
>  	/*
> -	 * If set non-zero number of request_hugepages, test will try to reserve the
> -	 * expected number of hugepage for testing in setup phase. If system does not
> -	 * have enough hpage for using, it will try the best to reserve 80% available
> -	 * number of hpages. With success test stores the reserved hugepage number in
> -	 * 'tst_hugepages. For the system without hugetlb supporting, variable
> -	 * 'tst_hugepages' will be set to 0. If the hugepage number needs to be set to
> -	 * 0 on supported hugetlb system, please use '.request_hugepages = TST_NO_HUGEPAGES'.
> +	 * Two policies for hugepage reserving:
> +	 *
> +	 * TST_FLEXIBLE:
> +	 *   If set non-zero number in .request_hugepages, ltp will try to reserve the
> +	 *   expected number of hugepage for testing in setup phase. If system does not
> +	 *   have enough memory for that, it will try the best to reserve 80% available
> +	 *   huge pages.
> +	 *
> +	 * TST_ENFORCED:
> +	 *   This is an enforced requirement for huge page reserve, ltp should strictly do
> +	 *   hpages applying and guarantee the 'HugePages_Free' no less than specified pages
> +	 *   which gives that test can use these specified numbers correctly.
> +	 *
> +	 *
> +	 * With success test stores the reserved hugepage number in 'tst_hugepages. For
> +	 * the system without hugetlb supporting, variable 'tst_hugepages' will be set to 0.
> +	 * If the hugepage number needs to be set to 0 on supported hugetlb system, please
> +	 * use '.request_hugepages = TST_NO_HUGEPAGES'.
>  	 *
>  	 * Also, we do cleanup and restore work for the hpages resetting automatically.
>  	 */
> -	unsigned long request_hugepages;
> +	struct tst_hugepage *request_hugepages;

Why is this treated as an array even though we work with only single
struct tst_hugepage instance?

I guess that it should rather be just:

'struct tst_hugepage request_hugepages;'

And that it should be initialized as:

	.request_hugepages = {2, ...},

>  	/*
>  	 * If set to non-zero, call tst_taint_init(taint_check) during setup
> diff --git a/lib/newlib_tests/test20.c b/lib/newlib_tests/test20.c
> index 5c24770a1..e8e81f8f4 100644
> --- a/lib/newlib_tests/test20.c
> +++ b/lib/newlib_tests/test20.c
> @@ -18,14 +18,16 @@ static void do_test(void) {
>  	tst_res(TINFO, "tst_hugepages = %lu", tst_hugepages);
>  	SAFE_FILE_PRINTF("/proc/sys/kernel/numa_balancing", "1");
>  
> -	hpages = test.request_hugepages;
> +	hpages = test.request_hugepages->number;
>  	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%lu", &val);
>  	if (val != hpages)
>  		tst_brk(TBROK, "nr_hugepages = %lu, but expect %lu", val, hpages);
>  	else
> -		tst_res(TPASS, "test .needs_hugepges");
> +		tst_res(TPASS, "test .request_hugepges");
> +
> +	struct tst_hugepage hp = { 1000000000000, TST_FLEXIBLE };
> +	hpages = tst_request_hugepages(&hp);
>  
> -	hpages = tst_request_hugepages(3);
>  	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%lu", &val);
>  	if (val != hpages)
>  		tst_brk(TBROK, "nr_hugepages = %lu, but expect %lu", val, hpages);
> @@ -35,7 +37,10 @@ static void do_test(void) {
>  
>  static struct tst_test test = {
>  	.test_all = do_test,
> -	.request_hugepages = 2,
> +	.request_hugepages = (struct tst_hugepage []){
> +		{2, TST_ENFORCED},
> +		{}
> +	},
>  	.save_restore = (const struct tst_path_val[]) {
>  		{"!/proc/sys/kernel/numa_balancing", "0"},
>  		{}
> diff --git a/lib/newlib_tests/test_zero_hugepage.c b/lib/newlib_tests/test_zero_hugepage.c
> index 0d85ce866..27cd196da 100644
> --- a/lib/newlib_tests/test_zero_hugepage.c
> +++ b/lib/newlib_tests/test_zero_hugepage.c
> @@ -21,7 +21,8 @@ static void do_test(void)
>  	else
>  		tst_res(TPASS, "test .request_hugepages = TST_NO_HUGEPAGES");
>  
> -	hpages = tst_request_hugepages(3);
> +	struct tst_hugepage hp = { 3, TST_FLEXIBLE };
> +	hpages = tst_request_hugepages(&hp);
>  	SAFE_FILE_SCANF(PATH_NR_HPAGES, "%lu", &val);
>  	if (val != hpages)
>  		tst_brk(TBROK, "nr_hugepages = %lu, but expect %lu", val, hpages);
> @@ -31,5 +32,8 @@ static void do_test(void)
>  
>  static struct tst_test test = {
>  	.test_all = do_test,
> -	.request_hugepages = TST_NO_HUGEPAGES,
> +	.request_hugepages = (struct tst_hugepage []){
> +		{TST_NO_HUGEPAGES, TST_FLEXIBLE},
> +		{}
> +	},
>  };
> diff --git a/lib/tst_hugepage.c b/lib/tst_hugepage.c
> index a7585bc3d..06e9fb7a1 100644
> --- a/lib/tst_hugepage.c
> +++ b/lib/tst_hugepage.c
> @@ -20,11 +20,13 @@ size_t tst_get_hugepage_size(void)
>  	return SAFE_READ_MEMINFO("Hugepagesize:") * 1024;
>  }
>  
> -unsigned long tst_request_hugepages(unsigned long hpages)
> +unsigned long tst_request_hugepages(struct tst_hugepage *hp)
>  {
>  	unsigned long val, max_hpages;
>  
>  	if (access(PATH_HUGEPAGES, F_OK)) {
> +		if (hp->policy == TST_ENFORCED)
> +			tst_brk(TCONF, "hugetlbfs is not supported");
>  		tst_hugepages = 0;
>  		goto out;
>  	}
> @@ -32,16 +34,20 @@ unsigned long tst_request_hugepages(unsigned long hpages)
>  	if (nr_opt)
>  		tst_hugepages = SAFE_STRTOL(nr_opt, 1, LONG_MAX);
>  	else
> -		tst_hugepages = hpages;
> +		tst_hugepages = hp->number;
>  
> -	if (hpages == TST_NO_HUGEPAGES) {
> +	if (hp->number == TST_NO_HUGEPAGES) {
>  		tst_hugepages = 0;
>  		goto set_hugepages;
>  	}
>  
>  	SAFE_FILE_PRINTF("/proc/sys/vm/drop_caches", "3");
> -	max_hpages = SAFE_READ_MEMINFO("MemFree:") / SAFE_READ_MEMINFO("Hugepagesize:");
> +	if (hp->policy == TST_ENFORCED) {
> +		tst_hugepages += SAFE_READ_MEMINFO("HugePages_Total:");
> +		goto set_hugepages;
> +	}
>  
> +	max_hpages = SAFE_READ_MEMINFO("MemFree:") / SAFE_READ_MEMINFO("Hugepagesize:");
>  	if (tst_hugepages > max_hpages) {
>  		tst_res(TINFO, "Requested number(%lu) of hugepages is too large, "
>  				"limiting to 80%% of the max hugepage count %lu",
> @@ -61,6 +67,14 @@ set_hugepages:
>  				"Not enough hugepages for testing.",
>  				val, tst_hugepages);
>  
> +	if (hp->policy == TST_ENFORCED) {
> +		unsigned long free_hpages = SAFE_READ_MEMINFO("HugePages_Free:");
> +		if (hp->number > free_hpages)
> +			tst_brk(TCONF, "free_hpages = %lu, but expect %lu. "
> +				"Not enough hugepages for testing.",
> +				free_hpages, hp->number);
> +	}
> +
>  	tst_res(TINFO, "%lu hugepage(s) reserved", tst_hugepages);
>  out:
>  	return tst_hugepages;
> -- 
> 2.35.1
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages
  2022-06-01 12:08 ` [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Cyril Hrubis
@ 2022-06-02  4:24   ` Li Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Li Wang @ 2022-06-02  4:24 UTC (permalink / raw)
  To: Cyril Hrubis; +Cc: LTP List


[-- Attachment #1.1: Type: text/plain, Size: 1679 bytes --]

Cyril Hrubis <chrubis@suse.cz> wrote:


> +TST_FLEXIBLE:
> > +  If set non-zero number in .request_hugepages, ltp will try to reserve
> the
> > +  expected number of hugepage for testing in setup phase. If system
> does not
> > +  have enough memory for that, it will try the best to reserve 80%
> available
> > +  huge pages.
> > +
> > +TST_ENFORCED:
> > +  This is an enforced requirement for huge page reserve, ltp should
> strictly do
> > +  hpages applying and guarantee the 'HugePages_Free' no less than
> specified pages
> > +  which gives that test can use these specified numbers correctly.
>
> As for the naming I would probably go for TST_NEEDS and TST_REQUEST. We
> use the word 'needs' in the tst_test to describe something that is
> required for the test. And 'request' sounds better to me than flexible
> in a sense that you can request a lot but get less than what was
> requested.
>

Sounds better, if so, I'd also rename '.request_hugepages' to '.hugepages'
in tst_test because there have been overlaps in the tst_policy words.

 struct tst_test test = {
        .test_all = run,
-       .request_hugepages = 2,
+       .hugepages = {2, TST_REQUEST},
        ...
 };
No hugepage change to:

    .hugepages = {TST_NO_HUGEPAGES, TST_REQUEST},

API in header file change to:

-unsigned long tst_request_hugepages(unsigned long hpages);
+unsigned long tst_reserve_hugepages(struct tst_hugepage *hp);

> -     unsigned long request_hugepages;
> > +     struct tst_hugepage *request_hugepages;
>
> Why is this treated as an array even though we work with only single
> struct tst_hugepage instance?
>

Right, it no needs an array in that case.


-- 
Regards,
Li Wang

[-- Attachment #1.2: Type: text/html, Size: 3154 bytes --]

[-- Attachment #2: Type: text/plain, Size: 60 bytes --]


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-02  4:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-04-28 13:29 [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Li Wang
2022-04-28 13:29 ` [LTP] [RFC PATCH 2/3] memfd_create03: make use of new .request_hugepages Li Wang
2022-04-28 13:29 ` [LTP] [RFC PATCH 3/3] hugetlb: " Li Wang
2022-06-01 12:08 ` [LTP] [RFC PATCH 1/3] lib: extend .request_hugepages to guarantee enough pages Cyril Hrubis
2022-06-02  4:24   ` Li Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox