All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests
@ 2022-12-25 15:42 Tarun Sahu
  2022-12-25 15:42 ` [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock Tarun Sahu
                   ` (12 more replies)
  0 siblings, 13 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Hi,
This patch series is in continuation to part [1], part [2] and part [3].
This series include 13 more tests taken from libhugetlbfs.

Background:
Libhugetlbfs is not being maintained actively, and some distro is dropping
support for it. There are some tests that are good for testing hugetlb
functionality in kernel. These patches include tests from libhugetlbfs.

ref:
 1. https://lore.kernel.org/all/20221104162511.28658-1-tsahu@linux.ibm.com/
 2. https://lore.kernel.org/all/20221120191533.164848-1-tsahu@linux.ibm.com/
 3. https://lore.kernel.org/all/20221220063109.279007-1-tsahu@linux.ibm.com/

Tarun Sahu (13):
  Hugetlb: Migrating libhugetlbfs mlock
  Hugetlb: Migrating libhugetlbfs mmap-cow
  Hugetlb: Migrating libhugetlbfs mmap-gettest
  Hugetlb: Migrating libhugetlbfs mprotect
  Hugetlb: Migrating libhugetlbfs mremap-fixed-huge-near-normal
  Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  Hugetlb: Migrating libhugetlbfs noresv-reserve-resv-page
  Hugetlb: Migrating libhugetlbfs noresv-regarded-as-resv
  Hugetlb: Migrating libhugetlbfs private
  Hugetlb: Migrating libhugetlbfs readahead_reserve
  Hugetlb: Migrating libhugetlbfs shared
  Hugetlb: Migrating libhugetlbfs shm-fork
  Hugetlb: Migrating libhugetlbfs mremap-expand-slice-collision

 runtest/hugetlb                               |  13 +
 testcases/kernel/mem/.gitignore               |  13 +
 .../kernel/mem/hugetlb/hugefork/hugefork02.c  | 104 ++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap20.c  |  88 +++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap21.c  | 131 ++++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap22.c  | 104 ++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap23.c  | 244 ++++++++++++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap24.c  | 212 +++++++++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap25.c  | 146 +++++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap26.c  | 131 ++++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap27.c  | 136 ++++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap28.c  |  74 ++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap29.c  | 115 +++++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap30.c  |  85 ++++++
 .../kernel/mem/hugetlb/hugemmap/hugemmap31.c  |  84 ++++++
 15 files changed, 1680 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap24.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap25.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap27.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap28.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c

-- 
2.31.1


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

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

* [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-26  4:48   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 02/13] Hugetlb: Migrating libhugetlbfs mmap-cow Tarun Sahu
                   ` (11 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/mlock.c test

Test Description: The test checks that mlocking hugetlb areas works
with all combinations of MAP_PRIVATE and MAP_SHARED with and without
MAP_LOCKED specified.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |  1 +
 testcases/kernel/mem/.gitignore               |  1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap20.c  | 88 +++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 4da1525a7..2dffa8421 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -21,6 +21,7 @@ hugemmap16 hugemmap16
 hugemmap17 hugemmap17
 hugemmap18 hugemmap18
 hugemmap19 hugemmap19
+hugemmap20 hugemmap20
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index b6b3e5ddd..dfd372892 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -20,6 +20,7 @@
 /hugetlb/hugemmap/hugemmap17
 /hugetlb/hugemmap/hugemmap18
 /hugetlb/hugemmap/hugemmap19
+/hugetlb/hugemmap/hugemmap20
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c
new file mode 100644
index 000000000..9607d58b7
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
+ * Author: David Gibson & Adam Litke
+ */
+
+/*\
+ * [Description]
+ *
+ * The test checks that mlocking hugetlb areas works with all combinations
+ * of MAP_PRIVATE and MAP_SHARED with and without MAP_LOCKED specified.
+ */
+
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+
+#include "hugetlb.h"
+
+#define MNTPOINT "hugetlbfs/"
+static int  fd = -1;
+static unsigned long hpage_size;
+
+static void test_simple_mlock(int flags, char *flags_str)
+{
+	void *p;
+	int ret;
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	p = SAFE_MMAP(0, hpage_size, PROT_READ|PROT_WRITE, flags, fd, 0);
+
+	ret = mlock(p, hpage_size);
+	if (ret) {
+		tst_res(TFAIL|TERRNO, "mlock() failed (flags %s)", flags_str);
+		goto cleanup;
+	}
+
+	ret = munlock(p, hpage_size);
+	if (ret)
+		tst_res(TFAIL|TERRNO, "munlock() failed (flags %s)", flags_str);
+	else
+		tst_res(TPASS, "mlock/munlock with %s hugetlb mmap",
+				flags_str);
+cleanup:
+	SAFE_MUNMAP(p, hpage_size);
+	SAFE_CLOSE(fd);
+}
+
+static void run_test(void)
+{
+	struct rlimit limit_info;
+
+	if (getrlimit(RLIMIT_MEMLOCK, &limit_info))
+		tst_res(TWARN|TERRNO, "Unable to read locked memory rlimit");
+	if (limit_info.rlim_cur < hpage_size)
+		tst_brk(TCONF, "Locked memory ulimit set below huge page size");
+
+	test_simple_mlock(MAP_PRIVATE, "MAP_PRIVATE");
+	test_simple_mlock(MAP_SHARED, "MAP_SHARED");
+	test_simple_mlock(MAP_PRIVATE|MAP_LOCKED, "MAP_PRIVATE|MAP_LOCKED");
+	test_simple_mlock(MAP_SHARED|MAP_LOCKED, "MAP_SHARED|MAP_LOCKED");
+
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {1, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 02/13] Hugetlb: Migrating libhugetlbfs mmap-cow
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
  2022-12-25 15:42 ` [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-26  6:57   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 03/13] Hugetlb: Migrating libhugetlbfs mmap-gettest Tarun Sahu
                   ` (10 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/mmap-cow.c test

Test Description: Tests copy-on-write semantics of large pages where a
number of threads map the same file with the MAP_PRIVATE flag. The threads
then write into their copy of the mapping and recheck the contents to
ensure they were not corrupted by the other threads.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap21.c  | 131 ++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 2dffa8421..71b4d57e4 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -22,6 +22,7 @@ hugemmap17 hugemmap17
 hugemmap18 hugemmap18
 hugemmap19 hugemmap19
 hugemmap20 hugemmap20
+hugemmap21 hugemmap21
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index dfd372892..74edfa392 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -21,6 +21,7 @@
 /hugetlb/hugemmap/hugemmap18
 /hugetlb/hugemmap/hugemmap19
 /hugetlb/hugemmap/hugemmap20
+/hugetlb/hugemmap/hugemmap21
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
new file mode 100644
index 000000000..481edb4b0
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 IBM Corporation.
+ * Author: David Gibson & Adam Litke
+ */
+
+/*\
+ * [Description]
+ *
+ * Tests copy-on-write semantics of large pages where a number of threads
+ * map the same file with the MAP_PRIVATE flag. The threads then write
+ * into their copy of the mapping and recheck the contents to ensure they
+ * were not corrupted by the other threads.
+ */
+
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/shm.h>
+#include <sys/wait.h>
+
+#include "hugetlb.h"
+
+#define BUF_SZ 256
+#define THREADS 5
+#define NR_HUGEPAGES 6
+#define MNTPOINT "hugetlbfs/"
+
+static int fd = -1;
+static long hpage_size;
+
+static void do_work(int thread, size_t size, int fd)
+{
+	char *addr;
+	size_t i;
+	char pattern = thread+65;
+
+	addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
+
+	tst_res(TINFO, "Thread %d (pid %d): Mapped at address %p",
+	       thread, getpid(), addr);
+
+	for (i = 0; i < size; i++)
+		memcpy((char *)addr+i, &pattern, 1);
+
+	if (msync(addr, size, MS_SYNC))
+		tst_brk(TBROK|TERRNO, "Thread %d (pid %d): msync() failed",
+				thread, getpid());
+
+	for (i = 0; i < size; i++)
+		if (addr[i] != pattern) {
+			tst_res(TFAIL, "thread %d (pid: %d): Corruption at %p; "
+				   "Got %c, Expected %c", thread, getpid(),
+				   &addr[i], addr[i], pattern);
+			goto cleanup;
+		}
+	tst_res(TINFO, "Thread %d (pid %d): Pattern verified",
+			thread, getpid());
+
+cleanup:
+	SAFE_MUNMAP(addr, size);
+	exit(0);
+}
+
+static void run_test(void)
+{
+	char *addr;
+	size_t size, itr;
+	int i, pid;
+	pid_t *wait_list;
+
+	wait_list = SAFE_MALLOC(THREADS * sizeof(pid_t));
+	size = (NR_HUGEPAGES / (THREADS+1)) * hpage_size;
+
+
+	/* First, mmap the file with MAP_SHARED and fill with data
+	 * If this is not done, then the fault handler will not be
+	 * called in the kernel since private mappings will be
+	 * created for the children at prefault time.
+	 */
+	addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+
+	for (itr = 0; itr < size; itr += 8)
+		memcpy(addr+itr, "deadbeef", 8);
+
+	for (i = 0; i < THREADS; i++) {
+		pid = SAFE_FORK();
+
+		if (pid == 0)
+			do_work(i, size, fd);
+
+		wait_list[i] = pid;
+	}
+	tst_reap_children();
+
+	SAFE_MUNMAP(addr, size);
+	free(wait_list);
+	tst_res(TPASS, "mmap COW working as expected.");
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+}
+
+static void cleanup(void)
+{
+	if (fd >= 1)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {NR_HUGEPAGES, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 03/13] Hugetlb: Migrating libhugetlbfs mmap-gettest
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
  2022-12-25 15:42 ` [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock Tarun Sahu
  2022-12-25 15:42 ` [LTP] [PATCH 02/13] Hugetlb: Migrating libhugetlbfs mmap-cow Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-26  7:33   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 04/13] Hugetlb: Migrating libhugetlbfs mprotect Tarun Sahu
                   ` (9 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/mmap-gettest.c test

Test Description: This baseline test validates that a mapping of a
certain size can be created, correctly.  Once created, all the pages are
filled with a pattern and rechecked to test for corruption. The mapping is
then released.  This process is repeated for a specified number of
iterations.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap22.c  | 104 ++++++++++++++++++
 3 files changed, 106 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 71b4d57e4..8e80db140 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -23,6 +23,7 @@ hugemmap18 hugemmap18
 hugemmap19 hugemmap19
 hugemmap20 hugemmap20
 hugemmap21 hugemmap21
+hugemmap22 hugemmap22
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 74edfa392..0fd01dbce 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -22,6 +22,7 @@
 /hugetlb/hugemmap/hugemmap19
 /hugetlb/hugemmap/hugemmap20
 /hugetlb/hugemmap/hugemmap21
+/hugetlb/hugemmap/hugemmap22
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
new file mode 100644
index 000000000..50addbe62
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 IBM Corporation.
+ * Author: David Gibson & Adam Litke
+ */
+
+/*\
+ * [Description]
+ *
+ * This baseline test validates that a mapping of a certain size can be
+ * created, correctly.  Once created, all the pages are filled with a
+ * pattern and rechecked to test for corruption. The mapping is then
+ * released. This process is repeated for a specified number of
+ * iterations.
+ */
+
+#include <stdio.h>
+#include <sys/mount.h>
+#include <unistd.h>
+#include <unistd.h>
+
+#include "hugetlb.h"
+
+#define ITERATIONS 10
+#define NR_HUGEPAGES 2
+#define MNTPOINT "hugetlbfs/"
+
+static unsigned long hpage_size;
+static int fd = -1;
+
+static void test_per_iteration(size_t size, int iter)
+{
+	char *m;
+	size_t i, j;
+	char pattern = 'A';
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	m = SAFE_MMAP(NULL, size, (PROT_READ|PROT_WRITE), MAP_SHARED, fd, 0);
+
+	for (i = 0; i < NR_HUGEPAGES; i++) {
+		for (j = 0; j < hpage_size; j++) {
+			if (*(m+(i*hpage_size)+j) != 0) {
+				tst_res(TFAIL, "Iter %d: Verifying the mmap area failed. "
+				     "Got %c, expected 0", iter,
+				     *(m+(i*hpage_size)+j));
+				goto cleanup;
+			}
+		}
+	}
+	for (i = 0; i < NR_HUGEPAGES; i++) {
+		pattern = 65+(i%26);
+		memset(m+(i*hpage_size), pattern, hpage_size);
+	}
+
+	for (i = 0; i < NR_HUGEPAGES; i++) {
+		pattern = 65+(i%26);
+		for (j = 0; j < hpage_size; j++) {
+			if (*(m+(i*hpage_size)+j) != pattern) {
+				tst_res(TFAIL, "Verifying the mmap area failed. "
+				     "got: %c, expected: %c",
+				     *(m+(i*hpage_size)+j), pattern);
+				goto cleanup;
+			}
+		}
+	}
+
+cleanup:
+	SAFE_MUNMAP(m, size);
+	SAFE_CLOSE(fd);
+}
+
+static void run_test(void)
+{
+	size_t size;
+	int i;
+
+	size = NR_HUGEPAGES * hpage_size;
+	for (i = 0; i < ITERATIONS; i++)
+		test_per_iteration(size, i);
+
+	tst_res(TPASS, "Successfully verified the mmap area.");
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {NR_HUGEPAGES, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 04/13] Hugetlb: Migrating libhugetlbfs mprotect
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (2 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 03/13] Hugetlb: Migrating libhugetlbfs mmap-gettest Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-27  5:48   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 05/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-huge-near-normal Tarun Sahu
                   ` (8 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/mprotect.c test

Test Description: This test uses mprotect to change protection of
hugepage mapping and perform read/write operation. It checks if the
operation results in expected behaviour as per the protection.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap23.c  | 244 ++++++++++++++++++
 3 files changed, 246 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 8e80db140..8ade3c9ec 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -24,6 +24,7 @@ hugemmap19 hugemmap19
 hugemmap20 hugemmap20
 hugemmap21 hugemmap21
 hugemmap22 hugemmap22
+hugemmap23 hugemmap23
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 0fd01dbce..ffd831f2e 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -23,6 +23,7 @@
 /hugetlb/hugemmap/hugemmap20
 /hugetlb/hugemmap/hugemmap21
 /hugetlb/hugemmap/hugemmap22
+/hugetlb/hugemmap/hugemmap23
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
new file mode 100644
index 000000000..687b192ae
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
@@ -0,0 +1,244 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 IBM Corporation.
+ * Author: David Gibson & Adam Litke
+ */
+
+/*\
+ * [Description]
+ *
+ * This test uses mprotect to change protection of hugepage mapping and
+ * perform read/write operation. It checks if the operation results in
+ * expected behaviour as per the protection.
+ */
+
+#include <stdio.h>
+#include <sys/mount.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <setjmp.h>
+#include <signal.h>
+
+#include "hugetlb.h"
+
+#ifndef barrier
+# ifdef mb
+	/* Redefining the mb() */
+#   define barrier() mb()
+# else
+#   define barrier() __asm__ __volatile__ ("" : : : "memory")
+# endif
+#endif
+
+#define MNTPOINT "hugetlbfs/"
+#define RANDOM_CONSTANT	0x1234ABCD
+
+static int  fd = -1;
+static sigjmp_buf sig_escape;
+static void *sig_expected = MAP_FAILED;
+static long hpage_size;
+
+static void sig_handler(int signum, siginfo_t *si, void *uc)
+{
+	(void)uc;
+	if (signum == SIGSEGV) {
+		tst_res(TINFO, "SIGSEGV at %p (sig_expected=%p)", si->si_addr,
+		       sig_expected);
+		if (si->si_addr == sig_expected)
+			siglongjmp(sig_escape, 1);
+		tst_res(TFAIL, "SIGSEGV somewhere unexpected");
+	} else
+		tst_res(TFAIL, "Unexpected signal %s", strsignal(signum));
+}
+
+static int test_read(void *p)
+{
+	volatile unsigned long *pl = p;
+	unsigned long x;
+
+	if (sigsetjmp(sig_escape, 1)) {
+		/* We got a SEGV */
+		sig_expected = MAP_FAILED;
+		return -1;
+	}
+
+	sig_expected = p;
+	barrier();
+	x = *pl;
+	tst_res(TINFO, "Read back %lu", x);
+	barrier();
+	sig_expected = MAP_FAILED;
+	return 0;
+}
+
+static int test_write(void *p, unsigned long val)
+{
+	volatile unsigned long *pl = p;
+	unsigned long x;
+
+	if (sigsetjmp(sig_escape, 1)) {
+		/* We got a SEGV */
+		sig_expected = MAP_FAILED;
+		return -1;
+	}
+
+	sig_expected = p;
+	barrier();
+	*pl = val;
+	x = *pl;
+	barrier();
+	sig_expected = MAP_FAILED;
+
+	return (x != val);
+}
+
+static int test_prot(void *p, int prot, char *prot_str)
+{
+	int r, w;
+
+	r = test_read(p);
+	tst_res(TINFO, "On Read: %d", r);
+	w = test_write(p, RANDOM_CONSTANT);
+	tst_res(TINFO, "On Write: %d", w);
+
+	if (prot & PROT_READ) {
+		if (r != 0) {
+			tst_res(TFAIL, "read failed on mmap(prot %s)", prot_str);
+			return -1;
+		}
+
+	} else {
+		if (r != -1) {
+			tst_res(TFAIL, "read succeeded on mmap(prot %s)", prot_str);
+			return -1;
+		}
+	}
+
+	if (prot & PROT_WRITE) {
+		switch (w) {
+		case -1:
+			tst_res(TFAIL, "write failed on mmap(prot %s)", prot_str);
+			return -1;
+		case 0:
+			break;
+		case 1:
+			tst_res(TFAIL, "write mismatch on mmap(prot %s)", prot_str);
+			return -1;
+		default:
+			tst_res(TWARN, "Bug in test");
+			return -1;
+		}
+	} else {
+		switch (w) {
+		case -1:
+			break;
+		case 0:
+			tst_res(TFAIL, "write succeeded on mmap(prot %s)", prot_str);
+			return -1;
+		case 1:
+			tst_res(TFAIL, "write mismatch on mmap(prot %s)", prot_str);
+			return -1;
+		default:
+			tst_res(TWARN, "Bug in test");
+			break;
+		}
+	}
+	return 0;
+}
+
+static int test_mprotect(int fd, char *testname,
+			  unsigned long len1, int prot1, char *prot1_str,
+			  unsigned long len2, int prot2, char *prot2_str)
+{
+	void *p;
+	int ret;
+
+	tst_res(TINFO, "Testing %s", testname);
+	tst_res(TINFO, "Mapping with prot %s", prot1_str);
+	p = SAFE_MMAP(NULL, len1, prot1, MAP_SHARED, fd, 0);
+
+	ret = test_prot(p, prot1, prot1_str);
+	if (ret)
+		goto cleanup;
+	tst_res(TINFO, "mprotect()ing to prot %s", prot2_str);
+	ret = mprotect(p, len2, prot2);
+	if (ret != 0) {
+		tst_res(TFAIL|TERRNO, "%s: mprotect(prot %s)", testname, prot2_str);
+		goto cleanup;
+	}
+
+	ret = test_prot(p, prot2, prot2_str);
+	if (ret)
+		goto cleanup;
+	if (len2 < len1)
+		ret = test_prot(p + len2, prot1, prot1_str);
+
+cleanup:
+	SAFE_MUNMAP(p, len1);
+	return ret;
+}
+
+static void run_test(void)
+{
+	void *p;
+
+	struct sigaction sa = {
+		.sa_sigaction = sig_handler,
+		.sa_flags = SA_SIGINFO,
+	};
+
+	SAFE_SIGACTION(SIGSEGV, &sa, NULL);
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	p = SAFE_MMAP(NULL, 2*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	memset(p, 0, hpage_size);
+	SAFE_MUNMAP(p, hpage_size);
+
+	if (test_mprotect(fd, "R->RW", hpage_size, PROT_READ, "PROT_READ",
+		      hpage_size, PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"))
+		goto cleanup;
+	if (test_mprotect(fd, "RW->R", hpage_size, PROT_READ|PROT_WRITE,
+		     "PROT_READ|PROT_WRITE", hpage_size, PROT_READ, "PROT_READ"))
+		goto cleanup;
+
+	if (test_mprotect(fd, "R->RW 1/2", 2*hpage_size, PROT_READ, "PROT_READ",
+		      hpage_size, PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"))
+		goto cleanup;
+	if (test_mprotect(fd, "RW->R 1/2", 2*hpage_size, PROT_READ|PROT_WRITE,
+		      "PROT_READ|PROT_WRITE", hpage_size, PROT_READ, "PROT_READ"))
+		goto cleanup;
+
+	if (test_mprotect(fd, "NONE->R", hpage_size, PROT_NONE, "PROT_NONE",
+		      hpage_size, PROT_READ, "PROT_READ"))
+		goto cleanup;
+	if (test_mprotect(fd, "NONE->RW", hpage_size, PROT_NONE, "PROT_NONE",
+		      hpage_size, PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"))
+		goto cleanup;
+
+	tst_res(TPASS, "Successfully tested mprotect with hugetlb area");
+cleanup:
+	SAFE_MUNMAP(p+hpage_size, hpage_size);
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {2, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 05/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-huge-near-normal
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (3 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 04/13] Hugetlb: Migrating libhugetlbfs mprotect Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-25 15:42 ` [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge Tarun Sahu
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/mremap-fixed-huge-near-normal.c test

Test Description: The kernel has bug for mremap() on some architecture.
mremap() can cause crashes on architectures with holes in the address
space (like ia64) and on powerpc with it's distinct page size "slices".

This test get the normal mapping address and mremap() hugepage mapping
near to this normal mapping.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap25.c  | 146 ++++++++++++++++++
 3 files changed, 148 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap25.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 8ade3c9ec..65265b0fe 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -25,6 +25,7 @@ hugemmap20 hugemmap20
 hugemmap21 hugemmap21
 hugemmap22 hugemmap22
 hugemmap23 hugemmap23
+hugemmap25 hugemmap25
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index ffd831f2e..c865a1e55 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -24,6 +24,7 @@
 /hugetlb/hugemmap/hugemmap21
 /hugetlb/hugemmap/hugemmap22
 /hugetlb/hugemmap/hugemmap23
+/hugetlb/hugemmap/hugemmap25
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap25.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap25.c
new file mode 100644
index 000000000..f8e99faf6
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap25.c
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2009 IBM Corporation.
+ * Author: David Gibson
+ */
+
+/*\
+ * [Description]
+ *
+ * The kernel has bug for mremap() on some architecture. mremap() can
+ * cause crashes on architectures with holes in the address space
+ * (like ia64) and on powerpc with it's distinct page size "slices".
+ *
+ * This test get the normal mapping address and mremap() hugepage mapping
+ * near to this normal mapping.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/types.h>
+
+#include "hugetlb.h"
+
+#define RANDOM_CONSTANT	0x1234ABCD
+#define MNTPOINT "hugetlbfs/"
+
+static int  fd = -1;
+static long hpage_size;
+
+static int do_readback(void *p, size_t size, const char *stage)
+{
+	unsigned int *q = p;
+	size_t i;
+
+	tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
+	       (unsigned long)size, stage);
+
+	for (i = 0; i < (size / sizeof(*q)); i++)
+		q[i] = RANDOM_CONSTANT ^ i;
+
+	for (i = 0; i < (size / sizeof(*q)); i++) {
+		if (q[i] != (RANDOM_CONSTANT ^ i)) {
+			tst_res(TFAIL, "Stage \"%s\": Mismatch at offset 0x%lx: 0x%x "
+					"instead of 0x%lx", stage, i, q[i], RANDOM_CONSTANT ^ i);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int do_remap(int fd, void *target)
+{
+	void *a, *b;
+	int ret;
+
+	a = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+
+	ret = do_readback(a, hpage_size, "base huge");
+	if (ret)
+		goto cleanup;
+	b = mremap(a, hpage_size, hpage_size, MREMAP_MAYMOVE | MREMAP_FIXED,
+		   target);
+
+	if (b != MAP_FAILED) {
+		ret = do_readback(b, hpage_size, "remapped");
+		a = b;
+	} else
+		tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED) disallowed");
+cleanup:
+	SAFE_MUNMAP(a, hpage_size);
+	return ret;
+}
+
+static void *map_align(size_t size, size_t align)
+{
+	unsigned long xsize = size + align - getpagesize();
+	size_t t;
+	void *p, *q;
+
+	p = SAFE_MMAP(NULL, xsize, PROT_READ|PROT_WRITE,
+		 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+
+	q = PALIGN(p, align);
+
+	t = q - p;
+	if (t)
+		SAFE_MUNMAP(p, t);
+
+	t = p + xsize - (q + size);
+	if (t)
+		SAFE_MUNMAP(q + size, t);
+
+	return q;
+}
+
+static void run_test(void)
+{
+	void *p;
+	int ret;
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	p = map_align(3*hpage_size, hpage_size);
+
+	SAFE_MUNMAP(p, hpage_size);
+	SAFE_MUNMAP(p + 2*hpage_size, hpage_size);
+
+	p = p + hpage_size;
+
+	tst_res(TINFO, "Normal mapping at %p", p);
+	ret = do_readback(p, hpage_size, "base normal page");
+	if (ret)
+		goto cleanup;
+	ret = do_remap(fd, p - hpage_size);
+	if (ret)
+		goto cleanup;
+	ret = do_remap(fd, p + hpage_size);
+	if (ret == 0)
+		tst_res(TPASS, "Successfully tested mremap hpage near normal mapping");
+cleanup:
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {3, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (4 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 05/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-huge-near-normal Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-27  6:31   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 07/13] Hugetlb: Migrating libhugetlbfs noresv-reserve-resv-page Tarun Sahu
                   ` (6 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/mremap-fixed-normal-near-huge.c test

Test Description: The kernel has bug for mremap() on some architecture.
mremap() can cause crashes on architectures with holes in the address
space (like ia64) and on powerpc with it's distinct page size "slices".

This test get the huge mapping address and mremap() normal mapping
near to this huge mapping.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap26.c  | 131 ++++++++++++++++++
 3 files changed, 133 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 65265b0fe..55185f2f7 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -26,6 +26,7 @@ hugemmap21 hugemmap21
 hugemmap22 hugemmap22
 hugemmap23 hugemmap23
 hugemmap25 hugemmap25
+hugemmap26 hugemmap26
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index c865a1e55..4886c6a5f 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -25,6 +25,7 @@
 /hugetlb/hugemmap/hugemmap22
 /hugetlb/hugemmap/hugemmap23
 /hugetlb/hugemmap/hugemmap25
+/hugetlb/hugemmap/hugemmap26
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c
new file mode 100644
index 000000000..03dd248bd
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2009 IBM Corporation.
+ * Author: David Gibson
+ */
+
+/*\
+ * [Description]
+ *
+ * Test Description: The kernel has bug for mremap() on some architecture.
+ * mremap() can cause crashes on architectures with holes in the address
+ * space (like ia64) and on powerpc with it's distinct page size "slices".
+ *
+ * This test get the huge mapping address and mremap() normal mapping
+ * near to this huge mapping.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/types.h>
+
+#include "hugetlb.h"
+
+#define RANDOM_CONSTANT	0x1234ABCD
+#define MNTPOINT "hugetlbfs/"
+
+static int  fd = -1;
+static long hpage_size, page_size;
+
+static int do_readback(void *p, size_t size, const char *stage)
+{
+	unsigned int *q = p;
+	size_t i;
+
+	tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
+	       (unsigned long)size, stage);
+
+	for (i = 0; i < (size / sizeof(*q)); i++)
+		q[i] = RANDOM_CONSTANT ^ i;
+
+	for (i = 0; i < (size / sizeof(*q)); i++) {
+		if (q[i] != (RANDOM_CONSTANT ^ i)) {
+			tst_res(TFAIL, "Stage \"%s\": Mismatch at offset 0x%lx: 0x%x "
+					"instead of 0x%lx", stage, i, q[i], RANDOM_CONSTANT ^ i);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int do_remap(void *target)
+{
+	void *a, *b;
+	int ret;
+
+	a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
+		  MAP_SHARED|MAP_ANONYMOUS, -1, 0);
+
+	ret = do_readback(a, page_size, "base normal");
+	if (ret)
+		goto cleanup;
+	b = mremap(a, page_size, page_size, MREMAP_MAYMOVE | MREMAP_FIXED,
+		   target);
+
+	if (b != MAP_FAILED) {
+		do_readback(b, page_size, "remapped");
+		a = b;
+	} else
+		tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED) disallowed");
+
+cleanup:
+	SAFE_MUNMAP(a, page_size);
+	return ret;
+}
+
+static void run_test(void)
+{
+	void *p;
+	int ret;
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	p = SAFE_MMAP(NULL, 3*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+		 fd, 0);
+
+	SAFE_MUNMAP(p, hpage_size);
+
+	SAFE_MUNMAP(p + 2*hpage_size, hpage_size);
+
+	p = p + hpage_size;
+
+	tst_res(TINFO, "Hugepage mapping at %p", p);
+
+	ret = do_readback(p, hpage_size, "base hugepage");
+	if (ret)
+		goto cleanup;
+	ret = do_remap(p - page_size);
+	if (ret)
+		goto cleanup;
+	ret = do_remap(p + hpage_size);
+	if (ret == 0)
+		tst_res(TPASS, "Successfully tested mremap normal near hpage mapping");
+cleanup:
+	SAFE_MUNMAP(p, hpage_size);
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+	page_size = getpagesize();
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {3, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 07/13] Hugetlb: Migrating libhugetlbfs noresv-reserve-resv-page
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (5 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-25 15:42 ` [LTP] [PATCH 08/13] Hugetlb: Migrating libhugetlbfs noresv-regarded-as-resv Tarun Sahu
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/noresv-preserve-resv-page.c test

Test Description: Test to preserve a reserved page against no-reserved
mapping. If all hugepages are reserved, access to no-reserved shared
mapping cause a process die, instead of stealing a hugepage which is
reserved for other process.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap27.c  | 136 ++++++++++++++++++
 3 files changed, 138 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap27.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 55185f2f7..d108542d2 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -27,6 +27,7 @@ hugemmap22 hugemmap22
 hugemmap23 hugemmap23
 hugemmap25 hugemmap25
 hugemmap26 hugemmap26
+hugemmap27 hugemmap27
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 4886c6a5f..9862414c6 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -26,6 +26,7 @@
 /hugetlb/hugemmap/hugemmap23
 /hugetlb/hugemmap/hugemmap25
 /hugetlb/hugemmap/hugemmap26
+/hugetlb/hugemmap/hugemmap27
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap27.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap27.c
new file mode 100644
index 000000000..243a8abf7
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap27.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2013 LG Electronics.
+ * Author: Joonsoo Kim
+ */
+
+/*\
+ * [Description]
+ *
+ * Test to preserve a reserved page against no-reserved mapping. If all
+ * hugepages are reserved, access to no-reserved shared mapping cause a
+ * process die, instead of stealing a hugepage which is reserved for other
+ * process.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <setjmp.h>
+#include <sys/types.h>
+
+#include "tst_test.h"
+
+#ifndef barrier
+# ifdef mb
+	/* Redefining the memory barrier */
+#   define barrier() mb()
+# else
+#   define barrier() __asm__ __volatile__ ("" : : : "memory")
+# endif
+#endif
+
+#define MNTPOINT "hugetlbfs/"
+static long hpage_size;
+static int fd1 = -1, fd2 = -1;
+static sigjmp_buf sig_escape;
+static void *sig_expected = MAP_FAILED;
+
+static void sig_handler(int signum, siginfo_t *si, void *uc)
+{
+	(void)uc;
+	if (signum == SIGBUS) {
+		if (si->si_addr == sig_expected)
+			siglongjmp(sig_escape, 1);
+		tst_res(TFAIL, "SIGBUS somewhere unexpected: %p (expected: %p)",
+				si->si_addr, sig_expected);
+	} else
+		tst_res(TFAIL, "Unexpected signal %s", strsignal(signum));
+}
+
+static int test_write(void *p)
+{
+	volatile char *pl = p;
+
+	if (sigsetjmp(sig_escape, 1)) {
+		/* We got a SIGBUS */
+		return 1;
+	}
+
+	sig_expected = p;
+	barrier();
+	*pl = 's';
+	return 0;
+}
+
+static void run_test(void)
+{
+	int nr_hugepages;
+	int surp_hugepages;
+	int ret;
+	char *p, *q;
+	struct sigaction sa = {
+		.sa_sigaction = sig_handler,
+		.sa_flags = SA_SIGINFO,
+	};
+
+	nr_hugepages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+
+	SAFE_SIGACTION(SIGBUS, &sa, NULL);
+
+	p = SAFE_MMAP(NULL, hpage_size * nr_hugepages,
+		PROT_READ | PROT_WRITE, MAP_SHARED, fd1, 0);
+
+	tst_res(TINFO, "Reserve all hugepages %d", nr_hugepages);
+
+	q = SAFE_MMAP(NULL, hpage_size,
+		PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fd2, 0);
+
+	tst_res(TINFO, "Write to %p to steal reserved page", q);
+
+	surp_hugepages = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SURP);
+	ret = test_write(q);
+	if (ret == 1) {
+		tst_res(TPASS, "Successful with SIGSEGV received");
+		goto cleanup;
+	}
+	/* Provisioning succeeded because of overcommit */
+	if (SAFE_READ_MEMINFO(MEMINFO_HPAGE_SURP) ==
+			surp_hugepages + 1) {
+		tst_res(TPASS, "Successful because of surplus pages");
+		goto cleanup;
+	}
+
+	tst_res(TFAIL, "Steal reserved page");
+cleanup:
+	SAFE_MUNMAP(p, hpage_size * nr_hugepages);
+	SAFE_MUNMAP(q, hpage_size);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+	fd1 = tst_creat_unlinked(MNTPOINT, 0);
+	fd2 = tst_creat_unlinked(MNTPOINT, 0);
+}
+
+static void cleanup(void)
+{
+	if (fd1 >= 0)
+		SAFE_CLOSE(fd1);
+	if (fd2 >= 0)
+		SAFE_CLOSE(fd2);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {2, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 08/13] Hugetlb: Migrating libhugetlbfs noresv-regarded-as-resv
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (6 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 07/13] Hugetlb: Migrating libhugetlbfs noresv-reserve-resv-page Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-27  6:47   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 09/13] Hugetlb: Migrating libhugetlbfs private Tarun Sahu
                   ` (4 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/noresv-regarded-as-resv.c test

Test Description: Test to correct handling for reserve count. If no
reserved mapping is created to reserved file region, it should be
considered as reserve mapping. Otherwise, reserve count will be
overflowed.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |  1 +
 testcases/kernel/mem/.gitignore               |  1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap28.c  | 74 +++++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap28.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index d108542d2..95afe009e 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -28,6 +28,7 @@ hugemmap23 hugemmap23
 hugemmap25 hugemmap25
 hugemmap26 hugemmap26
 hugemmap27 hugemmap27
+hugemmap28 hugemmap28
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 9862414c6..2f8ed0df0 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -27,6 +27,7 @@
 /hugetlb/hugemmap/hugemmap25
 /hugetlb/hugemmap/hugemmap26
 /hugetlb/hugemmap/hugemmap27
+/hugetlb/hugemmap/hugemmap28
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap28.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap28.c
new file mode 100644
index 000000000..1c4f49038
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap28.c
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2013 LG Electronics.
+ * Author: Joonsoo Kim
+ */
+
+/*\
+ * [Description]
+ *
+ * Test to correct handling for reserve count. If no reserved mapping is
+ * created to reserved file region, it should be considered as reserve
+ * mapping. Otherwise, reserve count will be overflowed.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <setjmp.h>
+#include <sys/types.h>
+
+#include "hugetlb.h"
+
+#define MNTPOINT "hugetlbfs/"
+static long hpage_size;
+static int fd = -1;
+
+static void run_test(void)
+{
+	unsigned long initial_resv, end_resv;
+	int fd;
+	char *p, *q;
+
+	initial_resv = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	p = SAFE_MMAP(NULL, hpage_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+
+	q = SAFE_MMAP(NULL, hpage_size,
+		PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fd, 0);
+
+	*q = 's';
+
+	SAFE_MUNMAP(p, hpage_size);
+	SAFE_MUNMAP(q, hpage_size);
+	SAFE_CLOSE(fd);
+
+	end_resv = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+
+	TST_EXP_EQ_LU(initial_resv, end_resv);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {2, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 09/13] Hugetlb: Migrating libhugetlbfs private
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (7 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 08/13] Hugetlb: Migrating libhugetlbfs noresv-regarded-as-resv Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-27  6:56   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 10/13] Hugetlb: Migrating libhugetlbfs readahead_reserve Tarun Sahu
                   ` (3 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/private.c test

Test Description: The test do mmap() with shared mapping and write.
It matches the data with private mmap() and then change it with other
data. It checks shared mapping data if data is not contaiminated by
private mapping.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap29.c  | 115 ++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 95afe009e..6ec8d1018 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -29,6 +29,7 @@ hugemmap25 hugemmap25
 hugemmap26 hugemmap26
 hugemmap27 hugemmap27
 hugemmap28 hugemmap28
+hugemmap29 hugemmap29
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 2f8ed0df0..fef0a76d6 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -28,6 +28,7 @@
 /hugetlb/hugemmap/hugemmap26
 /hugetlb/hugemmap/hugemmap27
 /hugetlb/hugemmap/hugemmap28
+/hugetlb/hugemmap/hugemmap29
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c
new file mode 100644
index 000000000..2d921e169
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 IBM Corporation.
+ * Author: David Gibson & Adam Litke
+ */
+
+/*\
+ * [Description]
+ *
+ * The test do mmap() with shared mapping and write. It matches the data
+ * with private mmap() and then change it with other data. It checks
+ * shared mapping data if data is not contaiminated by private mapping.
+ * Similiarly checks for private data if it is not contaminated by changing
+ * shared mmap data.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <setjmp.h>
+#include <sys/types.h>
+
+#include "hugetlb.h"
+
+#define C1	0x1234ABCD
+#define C2  0xFEDC9876
+
+#define MNTPOINT "hugetlbfs/"
+static unsigned long hpage_size;
+static int fd = -1;
+
+static void run_test(void)
+{
+	void *p, *q;
+	unsigned int *pl, *ql;
+	unsigned long i;
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+		 fd, 0);
+
+	pl = p;
+	for (i = 0; i < (hpage_size / sizeof(*pl)); i++)
+		pl[i] = C1 ^ i;
+
+	q = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
+		 fd, 0);
+
+	ql = q;
+	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
+		if (ql[i] != (C1 ^ i)) {
+			tst_res(TFAIL, "Mismatch at offset %lu, got: %u, expected: %lu",
+					i, ql[i], C1 ^ i);
+			goto cleanup;
+		}
+	}
+
+	for (i = 0; i < (hpage_size / sizeof(*ql)); i++)
+		ql[i] = C2 ^ i;
+
+	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
+		if (ql[i] != (C2 ^ i)) {
+			tst_res(TFAIL, "PRIVATE mismatch at offset %lu, got: %u, expected: %lu",
+					i, ql[i], C2 ^ i);
+			goto cleanup;
+		}
+	}
+
+	for (i = 0; i < (hpage_size / sizeof(*pl)); i++) {
+		if (pl[i] != (C1 ^ i)) {
+			tst_res(TFAIL, "SHARED map contaminated at offset %lu, "
+					"got: %u, expected: %lu", i, pl[i], C1 ^ i);
+			goto cleanup;
+		}
+	}
+
+	memset(p, 0, hpage_size);
+
+	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
+		if (ql[i] != (C2 ^ i)) {
+			tst_res(TFAIL, "PRIVATE map contaminated at offset %lu, "
+					"got: %u, expected: %lu", i, ql[i], C2 ^ i);
+			goto cleanup;
+		}
+	}
+	tst_res(TPASS, "Successfully tested shared/private mmaping and its data");
+cleanup:
+	SAFE_MUNMAP(p, hpage_size);
+	SAFE_MUNMAP(q, hpage_size);
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {2, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 10/13] Hugetlb: Migrating libhugetlbfs readahead_reserve
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (8 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 09/13] Hugetlb: Migrating libhugetlbfs private Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-27  7:05   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 11/13] Hugetlb: Migrating libhugetlbfs shared Tarun Sahu
                   ` (2 subsequent siblings)
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/readahead_reserve.c test

Test Description: readahead() on some kernels can cause the reservation
counter to get corrupted. The problem is that the patches are allocated
for the reservation but not faulted in at the time of allocation. The
counters do not get updated and effectively "leak". This test
identifies whether the kernel is vulnerable to the problem or not.
It's fixed in kernel by 'commit f2deae9d4e70
("Remove implementation of readpage from the hugetlbfs_aops")'.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |  1 +
 testcases/kernel/mem/.gitignore               |  1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap30.c  | 85 +++++++++++++++++++
 3 files changed, 87 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 6ec8d1018..60cca4eb7 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -30,6 +30,7 @@ hugemmap26 hugemmap26
 hugemmap27 hugemmap27
 hugemmap28 hugemmap28
 hugemmap29 hugemmap29
+hugemmap30 hugemmap30
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index fef0a76d6..bb9720452 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -29,6 +29,7 @@
 /hugetlb/hugemmap/hugemmap27
 /hugetlb/hugemmap/hugemmap28
 /hugetlb/hugemmap/hugemmap29
+/hugetlb/hugemmap/hugemmap30
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c
new file mode 100644
index 000000000..9a0709af1
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 IBM Corporation.
+ * Author: Mel Gorman
+ */
+
+/*\
+ * [Description]
+ *
+ * readahead() on some kernels can cause the reservation counter to get
+ * corrupted. The problem is that the pages are allocated for the
+ * reservation but not faulted in at the time of allocation. The
+ * counters do not get updated and effectively "leak". This test
+ * identifies whether the kernel is vulnerable to the problem or not.
+ * It's fixed in kernel by commit f2deae9d4e70.
+ */
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+#include "hugetlb.h"
+
+#define MNTPOINT "hugetlbfs/"
+static long hpage_size;
+static int fd = -1;
+
+static void run_test(void)
+{
+	void *p;
+	unsigned long initial_rsvd, map_rsvd, readahead_rsvd, end_rsvd;
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	initial_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+
+	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+		 fd, 0);
+	map_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "map_rsvd: %lu", map_rsvd);
+
+	readahead(fd, 0, hpage_size);
+	readahead_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+	tst_res(TINFO, "readahead_rsvd: %lu", readahead_rsvd);
+
+	memset(p, 1, hpage_size);
+
+	SAFE_MUNMAP(p, hpage_size);
+	SAFE_CLOSE(fd);
+	end_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
+
+	TST_EXP_EQ_LU(end_rsvd, initial_rsvd);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.tags = (struct tst_tag[]) {
+		{"linux-git", "f2deae9d4e70"},
+		{}
+	},
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {1, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 11/13] Hugetlb: Migrating libhugetlbfs shared
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (9 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 10/13] Hugetlb: Migrating libhugetlbfs readahead_reserve Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-27  7:08   ` Li Wang
  2022-12-25 15:42 ` [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork Tarun Sahu
  2022-12-25 15:42 ` [LTP] [PATCH 13/13] Hugetlb: Migrating libhugetlbfs mremap-expand-slice-collision Tarun Sahu
  12 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/shared.c test

Test Description: This test is basic shared mapping test. Two shared
mappings are created with same offset on a file. It checks if writing
to one mapping can be seen to other mapping or not?

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |  1 +
 testcases/kernel/mem/.gitignore               |  1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap31.c  | 84 +++++++++++++++++++
 3 files changed, 86 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 60cca4eb7..33fd384b4 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -31,6 +31,7 @@ hugemmap27 hugemmap27
 hugemmap28 hugemmap28
 hugemmap29 hugemmap29
 hugemmap30 hugemmap30
+hugemmap31 hugemmap31
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index bb9720452..8375389cd 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -30,6 +30,7 @@
 /hugetlb/hugemmap/hugemmap28
 /hugetlb/hugemmap/hugemmap29
 /hugetlb/hugemmap/hugemmap30
+/hugetlb/hugemmap/hugemmap31
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c
new file mode 100644
index 000000000..a09905023
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 IBM Corporation.
+ * Author: David Gibson & Adam Litke
+ */
+
+/*\
+ * [Description]
+ *
+ * This test is basic shared mapping test. Two shared mappings are created
+ * with same offset on a file. It checks if writing to one mapping can be
+ * seen to other mapping or not?
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <setjmp.h>
+#include <sys/types.h>
+
+#include "hugetlb.h"
+
+#define RANDOM_CONSTANT	0x1234ABCD
+#define MNTPOINT "hugetlbfs/"
+
+static long hpage_size;
+static int fd = -1;
+
+static void run_test(void)
+{
+	void *p, *q;
+	unsigned long *pl, *ql;
+	unsigned long i;
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+		 fd, 0);
+
+	q = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
+		 fd, 0);
+
+	pl = p;
+	for (i = 0; i < (hpage_size / sizeof(*pl)); i++)
+		pl[i] = RANDOM_CONSTANT ^ i;
+
+	ql = q;
+	for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
+		if (ql[i] != (RANDOM_CONSTANT ^ i)) {
+			tst_res(TFAIL, "Mismatch at offset %lu, Got: %lu, Expected: %lu",
+					i, ql[i], RANDOM_CONSTANT ^ i);
+			goto cleanup;
+		}
+	}
+
+	tst_res(TPASS, "Successfully tested data between two shared mappings");
+cleanup:
+	SAFE_MUNMAP(p, hpage_size);
+	SAFE_MUNMAP(q, hpage_size);
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {2, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (10 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 11/13] Hugetlb: Migrating libhugetlbfs shared Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  2022-12-28  3:15   ` Li Wang
  2023-01-03 18:23   ` Petr Vorel
  2022-12-25 15:42 ` [LTP] [PATCH 13/13] Hugetlb: Migrating libhugetlbfs mremap-expand-slice-collision Tarun Sahu
  12 siblings, 2 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Migrating the libhugetlbfs/testcases/shm-fork.c test

Test Description: Test shared memory behavior when multiple threads are
Test shared memory behavior when multiple threads are attached
to a segment.  A segment is created and then children are
spawned which attach, write, read (verify), and detach from the
shared memory segment.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugefork/hugefork02.c  | 104 ++++++++++++++++++
 3 files changed, 106 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugefork/hugefork02.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 33fd384b4..348a7dc5f 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -2,6 +2,7 @@ hugefallocate01 hugefallocate01
 hugefallocate02 hugefallocate02
 
 hugefork01 hugefork01
+hugefork02 hugefork02 -P 3 -s 5
 
 hugemmap01 hugemmap01
 hugemmap02 hugemmap02
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 8375389cd..0c1311303 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -2,6 +2,7 @@
 /hugetlb/hugefallocate/hugefallocate01
 /hugetlb/hugefallocate/hugefallocate02
 /hugetlb/hugefork/hugefork01
+/hugetlb/hugefork/hugefork02
 /hugetlb/hugemmap/hugemmap01
 /hugetlb/hugemmap/hugemmap02
 /hugetlb/hugemmap/hugemmap04
diff --git a/testcases/kernel/mem/hugetlb/hugefork/hugefork02.c b/testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
new file mode 100644
index 000000000..6596bf81f
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
+ * Author: David Gibson & Adam Litke
+ */
+
+/*\
+ * [Description]
+ *
+ * Test shared memory behavior when multiple threads are attached to a
+ * segment. A segment is created and then children are spawned which
+ * attach, write, read (verify), and detach from the shared memory segment.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <setjmp.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/shm.h>
+
+#include "tst_safe_sysv_ipc.h"
+#include "hugetlb.h"
+
+static int shmid = -1;
+
+#define NR_HUGEPAGES 5
+#define NUMPROCS 20
+#define MNTPOINT "hugetlbfs/"
+#define MAX_PROCS 200
+#define BUF_SZ 256
+
+static long hpage_size;
+
+static void do_child(int thread, unsigned long size)
+{
+	volatile char *shmaddr;
+	int j;
+	unsigned long k;
+
+	for (j = 0; j < 5; j++) {
+		shmaddr = SAFE_SHMAT(shmid, 0, SHM_RND);
+
+		for (k = 0; k < size; k++)
+			shmaddr[k] = (char) (k);
+		for (k = 0; k < size; k++)
+			if (shmaddr[k] != (char)k) {
+				tst_res(TFAIL, "Thread %d, Offset %lu mismatch", thread, k);
+				goto cleanup;
+			}
+
+		SAFE_SHMDT((const void *)shmaddr);
+	}
+cleanup:
+	exit(0);
+}
+
+static void run_test(void)
+{
+	unsigned long size;
+	int pid;
+	int i;
+
+	size = hpage_size * NR_HUGEPAGES;
+	shmid = SAFE_SHMGET(2, size, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
+
+	for (i = 0; i < NUMPROCS; i++) {
+		pid = SAFE_FORK();
+
+		if (pid == 0)
+			do_child(i, size);
+	}
+
+	tst_reap_children();
+	tst_res(TPASS, "Successfully tested shared hugetlb memory with multiple procs");
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+}
+
+static void cleanup(void)
+{
+	if (shmid >= 0)
+		SAFE_SHMCTL(shmid, IPC_RMID, NULL);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.forks_child = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {NR_HUGEPAGES, TST_NEEDS},
+};
-- 
2.31.1


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

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

* [LTP] [PATCH 13/13] Hugetlb: Migrating libhugetlbfs mremap-expand-slice-collision
  2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
                   ` (11 preceding siblings ...)
  2022-12-25 15:42 ` [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork Tarun Sahu
@ 2022-12-25 15:42 ` Tarun Sahu
  12 siblings, 0 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-25 15:42 UTC (permalink / raw)
  To: ltp; +Cc: geetika, sbhat, aneesh.kumar, vaibhav, rpalethorpe

Kernel has bug in mremap for some architecture. mremap() can cause
crashes on architectures with holes in the address space (like ia64)
and on powerpc with it's distict page size slices.

This test perform mremap() with normal and hugepages around powerpc
slice boundary.

Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap24.c  | 212 ++++++++++++++++++
 3 files changed, 214 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap24.c

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 348a7dc5f..238ce6498 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -26,6 +26,7 @@ hugemmap20 hugemmap20
 hugemmap21 hugemmap21
 hugemmap22 hugemmap22
 hugemmap23 hugemmap23
+hugemmap23 hugemmap24
 hugemmap25 hugemmap25
 hugemmap26 hugemmap26
 hugemmap27 hugemmap27
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 0c1311303..06e125d03 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -25,6 +25,7 @@
 /hugetlb/hugemmap/hugemmap21
 /hugetlb/hugemmap/hugemmap22
 /hugetlb/hugemmap/hugemmap23
+/hugetlb/hugemmap/hugemmap24
 /hugetlb/hugemmap/hugemmap25
 /hugetlb/hugemmap/hugemmap26
 /hugetlb/hugemmap/hugemmap27
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap24.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap24.c
new file mode 100644
index 000000000..7e986a471
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap24.c
@@ -0,0 +1,212 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2009 IBM Corporation.
+ * Author: David Gibson
+ */
+
+/*\
+ * [Description]
+ *
+ * Kernel has bug in mremap for some architecture. mremap() can cause
+ * crashes on architectures with holes in the address space (like ia64)
+ * and on powerpc with it's distict page size slices.
+ *
+ * This test perform mremap() with normal and hugepages around powerpc
+ * slice boundary.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <sys/param.h>
+#include <sys/types.h>
+
+#include "tst_test.h"
+
+#define RANDOM_CONSTANT	0x1234ABCD
+#define MNTPOINT "hugetlbfs/"
+
+static int  fd = -1;
+static unsigned long slice_boundary;
+static long hpage_size, page_size;
+
+static int init_slice_boundary(int fd)
+{
+	unsigned long slice_size;
+	void *p, *heap;
+	int i;
+#if defined(__LP64__) && !defined(__aarch64__)
+	/* powerpc: 1TB slices starting at 1 TB */
+	slice_boundary = 0x10000000000;
+	slice_size = 0x10000000000;
+#else
+	/* powerpc: 256MB slices up to 4GB */
+	slice_boundary = 0x00000000;
+	slice_size = 0x10000000;
+#endif
+
+	/* dummy malloc so we know where is heap */
+	heap = malloc(1);
+	free(heap);
+
+	/* Find 2 neighbour slices with couple huge pages free
+	 * around slice boundary.
+	 * 16 is the maximum number of slices (low/high)
+	 */
+	for (i = 0; i < 16-1; i++) {
+		slice_boundary += slice_size;
+		p = mmap((void *)(slice_boundary-2*hpage_size), 4*hpage_size,
+			PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0);
+		if (p == MAP_FAILED) {
+			tst_res(TINFO|TERRNO, "can't use slice_boundary: 0x%lx",
+					slice_boundary);
+		} else {
+			SAFE_MUNMAP(p, 4*hpage_size);
+			break;
+		}
+	}
+
+	if (p == MAP_FAILED) {
+		tst_res(TFAIL|TERRNO, "couldn't find 2 free neighbour slices");
+		return -1;
+	}
+	tst_res(TINFO, "using slice_boundary: 0x%lx", slice_boundary);
+	return 0;
+}
+
+static int do_readback(void *p, size_t size, const char *stage)
+{
+	unsigned int *q = p;
+	size_t i;
+
+	for (i = 0; i < (size / sizeof(*q)); i++)
+		q[i] = RANDOM_CONSTANT ^ i;
+
+	for (i = 0; i < (size / sizeof(*q)); i++) {
+		if (q[i] != (RANDOM_CONSTANT ^ i)) {
+			tst_res(TFAIL, "Stage \"%s\": Mismatch at offset 0x%lx: 0x%x instead of 0x%lx",
+			     stage, i, q[i], RANDOM_CONSTANT ^ i);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static void run_test(void)
+{
+	void *p = NULL, *q = NULL, *r;
+	long p_size, q_size;
+	int ret;
+
+	fd = tst_creat_unlinked(MNTPOINT, 0);
+	ret = init_slice_boundary(fd);
+	if (ret)
+		goto cleanup;
+
+	/* First, hugepages above, normal below */
+	tst_res(TINFO, "Testing with hpage above & normal below the slice_boundary");
+	p_size = hpage_size;
+	p = SAFE_MMAP((void *)(slice_boundary + hpage_size), p_size,
+		 PROT_READ | PROT_WRITE,
+		 MAP_SHARED | MAP_FIXED, fd, 0);
+
+	ret = do_readback(p, p_size, "huge above");
+	if (ret)
+		goto cleanup;
+
+	q_size = page_size;
+	q = SAFE_MMAP((void *)(slice_boundary - page_size), q_size,
+		 PROT_READ | PROT_WRITE,
+		 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
+
+	ret = do_readback(q, q_size, "normal below");
+	if (ret)
+		goto cleanup;
+
+	r = mremap(q, page_size, 2*page_size, 0);
+	if (r == MAP_FAILED) {
+		tst_res(TINFO, "mremap(%p, %lu, %lu, 0) disallowed",
+				q, page_size, 2*page_size);
+	} else {
+		q_size = 2*page_size;
+		if (r != q) {
+			tst_res(TFAIL, "mremap() moved without MREMAP_MAYMOVE!?");
+			ret = -1;
+		} else
+			ret = do_readback(q, 2*page_size, "normal below expanded");
+	}
+
+	SAFE_MUNMAP(p, p_size);
+	SAFE_MUNMAP(q, q_size);
+	if (ret)
+		goto cleanup_fd;
+
+	/* Next, normal pages above, huge below */
+	tst_res(TINFO, "Testing with normal above & hpage below the slice_boundary");
+	p_size = page_size;
+	p = SAFE_MMAP((void *)(slice_boundary + hpage_size), p_size,
+		 PROT_READ|PROT_WRITE,
+		 MAP_SHARED | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
+
+	ret = do_readback(p, p_size, "normal above");
+	if (ret)
+		goto cleanup;
+
+	q_size = hpage_size;
+	q = SAFE_MMAP((void *)(slice_boundary - hpage_size),
+		 q_size, PROT_READ | PROT_WRITE,
+		 MAP_SHARED | MAP_FIXED, fd, 0);
+
+	ret = do_readback(q, q_size, "huge below");
+	if (ret)
+		goto cleanup;
+
+	r = mremap(q, hpage_size, 2*hpage_size, 0);
+	if (r == MAP_FAILED) {
+		tst_res(TINFO, "mremap(%p, %lu, %lu, 0) disallowed",
+				q, hpage_size, 2*hpage_size);
+	} else {
+		q_size = 2*hpage_size;
+		if (r != q) {
+			tst_res(TFAIL, "mremap() moved without MREMAP_MAYMOVE!?");
+			ret = -1;
+		} else
+			ret = do_readback(q, 2*hpage_size, "huge below expanded");
+	}
+	if (ret)
+		goto cleanup;
+	tst_res(TPASS, "Successful");
+cleanup:
+	if (p)
+		SAFE_MUNMAP(p, p_size);
+	if (q)
+		SAFE_MUNMAP(q, q_size);
+cleanup_fd:
+	SAFE_CLOSE(fd);
+}
+
+static void setup(void)
+{
+	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
+	page_size = getpagesize();
+}
+
+static void cleanup(void)
+{
+	if (fd >= 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.mntpoint = MNTPOINT,
+	.needs_hugetlbfs = 1,
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run_test,
+	.hugepages = {4, TST_NEEDS},
+};
-- 
2.31.1


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

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

* Re: [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock
  2022-12-25 15:42 ` [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock Tarun Sahu
@ 2022-12-26  4:48   ` Li Wang
  2022-12-26  9:39     ` Tarun Sahu
  0 siblings, 1 reply; 36+ messages in thread
From: Li Wang @ 2022-12-26  4:48 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:42 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/mlock.c test
>
> Test Description: The test checks that mlocking hugetlb areas works
> with all combinations of MAP_PRIVATE and MAP_SHARED with and without
> MAP_LOCKED specified.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |  1 +
>  testcases/kernel/mem/.gitignore               |  1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap20.c  | 88 +++++++++++++++++++
>  3 files changed, 90 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 4da1525a7..2dffa8421 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -21,6 +21,7 @@ hugemmap16 hugemmap16
>  hugemmap17 hugemmap17
>  hugemmap18 hugemmap18
>  hugemmap19 hugemmap19
> +hugemmap20 hugemmap20
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index b6b3e5ddd..dfd372892 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -20,6 +20,7 @@
>  /hugetlb/hugemmap/hugemmap17
>  /hugetlb/hugemmap/hugemmap18
>  /hugetlb/hugemmap/hugemmap19
> +/hugetlb/hugemmap/hugemmap20
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c
> new file mode 100644
> index 000000000..9607d58b7
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap20.c
> @@ -0,0 +1,88 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
> + * Author: David Gibson & Adam Litke
> + */
> +
> +/*\
> + * [Description]
> + *
> + * The test checks that mlocking hugetlb areas works with all combinations
> + * of MAP_PRIVATE and MAP_SHARED with and without MAP_LOCKED specified.
> + */
> +
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <limits.h>
> +#include <sys/param.h>
> +#include <sys/types.h>
> +#include <sys/resource.h>
> +
> +#include "hugetlb.h"
> +
> +#define MNTPOINT "hugetlbfs/"
> +static int  fd = -1;
> +static unsigned long hpage_size;
> +
> +static void test_simple_mlock(int flags, char *flags_str)
> +{
> +       void *p;
> +       int ret;
> +
> +       fd = tst_creat_unlinked(MNTPOINT, 0);
> +       p = SAFE_MMAP(0, hpage_size, PROT_READ|PROT_WRITE, flags, fd, 0);
> +
> +       ret = mlock(p, hpage_size);
> +       if (ret) {
> +               tst_res(TFAIL|TERRNO, "mlock() failed (flags %s)",
> flags_str);
> +               goto cleanup;
> +       }
> +
> +       ret = munlock(p, hpage_size);
> +       if (ret)
> +               tst_res(TFAIL|TERRNO, "munlock() failed (flags %s)",
> flags_str);
> +       else
> +               tst_res(TPASS, "mlock/munlock with %s hugetlb mmap",
> +                               flags_str);
> +cleanup:
> +       SAFE_MUNMAP(p, hpage_size);
> +       SAFE_CLOSE(fd);
> +}
> +
> +static void run_test(void)
> +{
>



> +       struct rlimit limit_info;
> +
> +       if (getrlimit(RLIMIT_MEMLOCK, &limit_info))
> +               tst_res(TWARN|TERRNO, "Unable to read locked memory
> rlimit");
> +       if (limit_info.rlim_cur < hpage_size)
> +               tst_brk(TCONF, "Locked memory ulimit set below huge page
> size");
>

These lines are better for moving into setup() phase. And, I'd propose
printing the value of 'limit_info.rlim_cur' and 'hpage_size' when TCONF.

The default value of max-locked-memory is smaller than hpage_size on
both RHEL8 and 9, which means this test will TCONF and skip running.
I'm hesitating should we temporally cancel the limitations and make
this test can really perform then restore that value to the original,
is this change make sense? WDYT?



> +
> +       test_simple_mlock(MAP_PRIVATE, "MAP_PRIVATE");
> +       test_simple_mlock(MAP_SHARED, "MAP_SHARED");
> +       test_simple_mlock(MAP_PRIVATE|MAP_LOCKED,
> "MAP_PRIVATE|MAP_LOCKED");
> +       test_simple_mlock(MAP_SHARED|MAP_LOCKED, "MAP_SHARED|MAP_LOCKED");
>

If we define an additional function like flags_to_str(int flags) for
converting
the flag into a string, which will be more simple for reading.

static char *flags_to_str(int flags)
{
       ...
}

static void test_simple_mlock(int flags)
{
        char *flags_str = flags_to_str(flags);
        ...
}



> +
> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> +}
> +
> +static void cleanup(void)
> +{
> +       if (fd >= 0)
> +               SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {1, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 02/13] Hugetlb: Migrating libhugetlbfs mmap-cow
  2022-12-25 15:42 ` [LTP] [PATCH 02/13] Hugetlb: Migrating libhugetlbfs mmap-cow Tarun Sahu
@ 2022-12-26  6:57   ` Li Wang
  2022-12-26  7:35     ` Tarun Sahu
  0 siblings, 1 reply; 36+ messages in thread
From: Li Wang @ 2022-12-26  6:57 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:42 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/mmap-cow.c test
>
> Test Description: Tests copy-on-write semantics of large pages where a
> number of threads map the same file with the MAP_PRIVATE flag. The threads
> then write into their copy of the mapping and recheck the contents to
> ensure they were not corrupted by the other threads.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |   1 +
>  testcases/kernel/mem/.gitignore               |   1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap21.c  | 131 ++++++++++++++++++
>  3 files changed, 133 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 2dffa8421..71b4d57e4 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -22,6 +22,7 @@ hugemmap17 hugemmap17
>  hugemmap18 hugemmap18
>  hugemmap19 hugemmap19
>  hugemmap20 hugemmap20
> +hugemmap21 hugemmap21
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index dfd372892..74edfa392 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -21,6 +21,7 @@
>  /hugetlb/hugemmap/hugemmap18
>  /hugetlb/hugemmap/hugemmap19
>  /hugetlb/hugemmap/hugemmap20
> +/hugetlb/hugemmap/hugemmap21
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
> new file mode 100644
> index 000000000..481edb4b0
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
> @@ -0,0 +1,131 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 IBM Corporation.
> + * Author: David Gibson & Adam Litke
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Tests copy-on-write semantics of large pages where a number of threads
> + * map the same file with the MAP_PRIVATE flag. The threads then write
> + * into their copy of the mapping and recheck the contents to ensure they
> + * were not corrupted by the other threads.
> + */
> +
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <limits.h>
> +#include <sys/param.h>
> +#include <sys/types.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/stat.h>
> +#include <fcntl.h>
> +#include <unistd.h>
> +#include <sys/shm.h>
> +#include <sys/wait.h>
> +
> +#include "hugetlb.h"
> +
> +#define BUF_SZ 256
> +#define THREADS 5
> +#define NR_HUGEPAGES 6
> +#define MNTPOINT "hugetlbfs/"
> +
> +static int fd = -1;
> +static long hpage_size;
> +
> +static void do_work(int thread, size_t size, int fd)
> +{
> +       char *addr;
> +       size_t i;
> +       char pattern = thread+65;
> +
> +       addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
> fd, 0);
> +
> +       tst_res(TINFO, "Thread %d (pid %d): Mapped at address %p",
> +              thread, getpid(), addr);
> +
> +       for (i = 0; i < size; i++)
> +               memcpy((char *)addr+i, &pattern, 1);
> +
> +       if (msync(addr, size, MS_SYNC))
> +               tst_brk(TBROK|TERRNO, "Thread %d (pid %d): msync() failed",
> +                               thread, getpid());
> +
> +       for (i = 0; i < size; i++)
> +               if (addr[i] != pattern) {
> +                       tst_res(TFAIL, "thread %d (pid: %d): Corruption at
> %p; "
> +                                  "Got %c, Expected %c", thread, getpid(),
> +                                  &addr[i], addr[i], pattern);
> +                       goto cleanup;
> +               }
> +       tst_res(TINFO, "Thread %d (pid %d): Pattern verified",
> +                       thread, getpid());
>

Maybe combining the address output with the content of patterns is better?

i.e.

--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
@@ -45,9 +45,6 @@ static void do_work(int thread, size_t size, int fd)

        addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd,
0);

-       tst_res(TINFO, "Thread %d (pid %d): Mapped at address %p",
-              thread, getpid(), addr);
-
        for (i = 0; i < size; i++)
                memcpy((char *)addr+i, &pattern, 1);

@@ -62,8 +59,8 @@ static void do_work(int thread, size_t size, int fd)
                                   &addr[i], addr[i], pattern);
                        goto cleanup;
                }
-       tst_res(TINFO, "Thread %d (pid %d): Pattern verified",
-                       thread, getpid());
+       tst_res(TINFO, "Thread %d (pid %d): Pattern %c verified at address
%p",
+                       thread, getpid(), pattern, addr);

 cleanup:
        SAFE_MUNMAP(addr, size);



> +
> +cleanup:
> +       SAFE_MUNMAP(addr, size);
> +       exit(0);
> +}
> +
> +static void run_test(void)
> +{
> +       char *addr;
> +       size_t size, itr;
> +       int i, pid;
> +       pid_t *wait_list;
> +
> +       wait_list = SAFE_MALLOC(THREADS * sizeof(pid_t));
> +       size = (NR_HUGEPAGES / (THREADS+1)) * hpage_size;
> +
> +
> +       /* First, mmap the file with MAP_SHARED and fill with data
> +        * If this is not done, then the fault handler will not be
> +        * called in the kernel since private mappings will be
> +        * created for the children at prefault time.
> +        */
> +       addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
> 0);
> +
> +       for (itr = 0; itr < size; itr += 8)
> +               memcpy(addr+itr, "deadbeef", 8);
> +
> +       for (i = 0; i < THREADS; i++) {
> +               pid = SAFE_FORK();
> +
> +               if (pid == 0)
> +                       do_work(i, size, fd);
> +
> +               wait_list[i] = pid;
> +       }
> +       tst_reap_children();
> +
> +       SAFE_MUNMAP(addr, size);
> +       free(wait_list);
> +       tst_res(TPASS, "mmap COW working as expected.");
> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
>

We do have a dedicated function named tst_get_hugepage_size();
could be used for getting huge page size.

The rest part looks good to me.

Reviewed-by: Li Wang <liwang@redhat.com>



> +       fd = tst_creat_unlinked(MNTPOINT, 0);
> +}
> +
> +static void cleanup(void)
> +{
> +       if (fd >= 1)
> +               SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .forks_child = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {NR_HUGEPAGES, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 03/13] Hugetlb: Migrating libhugetlbfs mmap-gettest
  2022-12-25 15:42 ` [LTP] [PATCH 03/13] Hugetlb: Migrating libhugetlbfs mmap-gettest Tarun Sahu
@ 2022-12-26  7:33   ` Li Wang
  2022-12-26  9:41     ` Tarun Sahu
  0 siblings, 1 reply; 36+ messages in thread
From: Li Wang @ 2022-12-26  7:33 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:42 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/mmap-gettest.c test
>
> Test Description: This baseline test validates that a mapping of a
> certain size can be created, correctly.  Once created, all the pages are
> filled with a pattern and rechecked to test for corruption. The mapping is
> then released.  This process is repeated for a specified number of
> iterations.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
>

Reviewed-by: Li Wang <liwang@redhat.com>

> ---
>  runtest/hugetlb                               |   1 +
>  testcases/kernel/mem/.gitignore               |   1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap22.c  | 104 ++++++++++++++++++
>  3 files changed, 106 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 71b4d57e4..8e80db140 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -23,6 +23,7 @@ hugemmap18 hugemmap18
>  hugemmap19 hugemmap19
>  hugemmap20 hugemmap20
>  hugemmap21 hugemmap21
> +hugemmap22 hugemmap22
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index 74edfa392..0fd01dbce 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -22,6 +22,7 @@
>  /hugetlb/hugemmap/hugemmap19
>  /hugetlb/hugemmap/hugemmap20
>  /hugetlb/hugemmap/hugemmap21
> +/hugetlb/hugemmap/hugemmap22
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
> new file mode 100644
> index 000000000..50addbe62
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
> @@ -0,0 +1,104 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 IBM Corporation.
> + * Author: David Gibson & Adam Litke
> + */
> +
> +/*\
> + * [Description]
> + *
> + * This baseline test validates that a mapping of a certain size can be
> + * created, correctly.  Once created, all the pages are filled with a
> + * pattern and rechecked to test for corruption. The mapping is then
> + * released. This process is repeated for a specified number of
> + * iterations.
> + */
> +
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <unistd.h>
> +#include <unistd.h>
> +
> +#include "hugetlb.h"
> +
> +#define ITERATIONS 10
>

I don't think we need too many iterations, it will cost too
much time and easily timed out on a slower machine,
so at most cycling up two times should be enough.



> +#define NR_HUGEPAGES 2
> +#define MNTPOINT "hugetlbfs/"
> +
> +static unsigned long hpage_size;
> +static int fd = -1;
> +
> +static void test_per_iteration(size_t size, int iter)
> +{
> +       char *m;
> +       size_t i, j;
> +       char pattern = 'A';
> +
> +       fd = tst_creat_unlinked(MNTPOINT, 0);
> +       m = SAFE_MMAP(NULL, size, (PROT_READ|PROT_WRITE), MAP_SHARED, fd,
> 0);
> +
> +       for (i = 0; i < NR_HUGEPAGES; i++) {
> +               for (j = 0; j < hpage_size; j++) {
> +                       if (*(m+(i*hpage_size)+j) != 0) {
> +                               tst_res(TFAIL, "Iter %d: Verifying the
> mmap area failed. "
> +                                    "Got %c, expected 0", iter,
> +                                    *(m+(i*hpage_size)+j));
> +                               goto cleanup;
> +                       }
> +               }
> +       }
> +       for (i = 0; i < NR_HUGEPAGES; i++) {
> +               pattern = 65+(i%26);
> +               memset(m+(i*hpage_size), pattern, hpage_size);
> +       }
> +
> +       for (i = 0; i < NR_HUGEPAGES; i++) {
> +               pattern = 65+(i%26);
> +               for (j = 0; j < hpage_size; j++) {
> +                       if (*(m+(i*hpage_size)+j) != pattern) {
> +                               tst_res(TFAIL, "Verifying the mmap area
> failed. "
> +                                    "got: %c, expected: %c",
> +                                    *(m+(i*hpage_size)+j), pattern);
> +                               goto cleanup;
> +                       }
> +               }
> +       }
> +
> +cleanup:
> +       SAFE_MUNMAP(m, size);
> +       SAFE_CLOSE(fd);
> +}
> +
> +static void run_test(void)
> +{
> +       size_t size;
> +       int i;
> +
> +       size = NR_HUGEPAGES * hpage_size;
> +       for (i = 0; i < ITERATIONS; i++)
> +               test_per_iteration(size, i);
> +
> +       tst_res(TPASS, "Successfully verified the mmap area.");
> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
>

hpage_size = tst_get_hugepage_size();

+}
> +
> +static void cleanup(void)
> +{
> +       if (fd >= 0)
> +               SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {NR_HUGEPAGES, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 02/13] Hugetlb: Migrating libhugetlbfs mmap-cow
  2022-12-26  6:57   ` Li Wang
@ 2022-12-26  7:35     ` Tarun Sahu
  0 siblings, 0 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-26  7:35 UTC (permalink / raw)
  To: Li Wang; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi Li,
Thanks for reviewing the patch.
Please find my comments inline.

--skip
> > +static void do_work(int thread, size_t size, int fd)
> > +{
> > +       char *addr;
> > +       size_t i;
> > +       char pattern = thread+65;
> > +
> > +       addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
> > fd, 0);
> > +
> > +       tst_res(TINFO, "Thread %d (pid %d): Mapped at address %p",
> > +              thread, getpid(), addr);
> > +
> > +       for (i = 0; i < size; i++)
> > +               memcpy((char *)addr+i, &pattern, 1);
> > +
> > +       if (msync(addr, size, MS_SYNC))
> > +               tst_brk(TBROK|TERRNO, "Thread %d (pid %d): msync() failed",
> > +                               thread, getpid());
> > +
> > +       for (i = 0; i < size; i++)
> > +               if (addr[i] != pattern) {
> > +                       tst_res(TFAIL, "thread %d (pid: %d): Corruption at
> > %p; "
> > +                                  "Got %c, Expected %c", thread, getpid(),
> > +                                  &addr[i], addr[i], pattern);
> > +                       goto cleanup;
> > +               }
> > +       tst_res(TINFO, "Thread %d (pid %d): Pattern verified",
> > +                       thread, getpid());
> >
> 
> Maybe combining the address output with the content of patterns is better?
> 
> i.e.
> 
> --- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap21.c
> @@ -45,9 +45,6 @@ static void do_work(int thread, size_t size, int fd)
> 
>         addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd,
> 0);
> 
> -       tst_res(TINFO, "Thread %d (pid %d): Mapped at address %p",
> -              thread, getpid(), addr);
> -
>         for (i = 0; i < size; i++)
>                 memcpy((char *)addr+i, &pattern, 1);
> 
> @@ -62,8 +59,8 @@ static void do_work(int thread, size_t size, int fd)
>                                    &addr[i], addr[i], pattern);
>                         goto cleanup;
>                 }
> -       tst_res(TINFO, "Thread %d (pid %d): Pattern verified",
> -                       thread, getpid());
> +       tst_res(TINFO, "Thread %d (pid %d): Pattern %c verified at address
> %p",
> +                       thread, getpid(), pattern, addr);
> 
>  cleanup:
>         SAFE_MUNMAP(addr, size);
> 
In case of failure, this verified comment will not get printed.
In case of success, I dont think it matters to show the content of pattern.
> 
> 
> > +
> > +cleanup:
> > +       SAFE_MUNMAP(addr, size);
> > +       exit(0);
> > +}
> > +
> > +static void run_test(void)
> > +{
> > +       char *addr;
> > +       size_t size, itr;
> > +       int i, pid;
> > +       pid_t *wait_list;
> > +
> > +       wait_list = SAFE_MALLOC(THREADS * sizeof(pid_t));
> > +       size = (NR_HUGEPAGES / (THREADS+1)) * hpage_size;
> > +
> > +
> > +       /* First, mmap the file with MAP_SHARED and fill with data
> > +        * If this is not done, then the fault handler will not be
> > +        * called in the kernel since private mappings will be
> > +        * created for the children at prefault time.
> > +        */
> > +       addr = SAFE_MMAP(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
> > 0);
> > +
> > +       for (itr = 0; itr < size; itr += 8)
> > +               memcpy(addr+itr, "deadbeef", 8);
> > +
> > +       for (i = 0; i < THREADS; i++) {
> > +               pid = SAFE_FORK();
> > +
> > +               if (pid == 0)
> > +                       do_work(i, size, fd);
> > +
> > +               wait_list[i] = pid;
> > +       }
> > +       tst_reap_children();
> > +
> > +       SAFE_MUNMAP(addr, size);
> > +       free(wait_list);
> > +       tst_res(TPASS, "mmap COW working as expected.");
> > +}
> > +
> > +static void setup(void)
> > +{
> > +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> >
> 
> We do have a dedicated function named tst_get_hugepage_size();
> could be used for getting huge page size.
Oh right! I could have used it in all other previous tests too.
Thanks for pointing this, Will take care of it in next revision.
> 
> The rest part looks good to me.
> 
> Reviewed-by: Li Wang <liwang@redhat.com>
> 
> 
> 
> > +       fd = tst_creat_unlinked(MNTPOINT, 0);
> > +}
> > +
> > +static void cleanup(void)
> > +{
> > +       if (fd >= 1)
> > +               SAFE_CLOSE(fd);
> > +}
> > +
> > +static struct tst_test test = {
> > +       .needs_root = 1,
> > +       .mntpoint = MNTPOINT,
> > +       .needs_hugetlbfs = 1,
> > +       .needs_tmpdir = 1,
> > +       .forks_child = 1,
> > +       .setup = setup,
> > +       .cleanup = cleanup,
> > +       .test_all = run_test,
> > +       .hugepages = {NR_HUGEPAGES, TST_NEEDS},
> > +};
> > --
> > 2.31.1
> >
> >
> 
> -- 
> Regards,
> Li Wang
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

* Re: [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock
  2022-12-26  4:48   ` Li Wang
@ 2022-12-26  9:39     ` Tarun Sahu
  2022-12-26  9:48       ` Li Wang
  0 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-26  9:39 UTC (permalink / raw)
  To: Li Wang; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi Li,
Thanks for reviewing the patch.
Please find my comments inline.

--skip
On Dec 26 2022, Li Wang wrote:
> On Sun, Dec 25, 2022 at 11:42 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:
> > +static void run_test(void)
> > +{
> >
> 
> 
> 
> > +       struct rlimit limit_info;
> > +
> > +       if (getrlimit(RLIMIT_MEMLOCK, &limit_info))
> > +               tst_res(TWARN|TERRNO, "Unable to read locked memory
> > rlimit");
> > +       if (limit_info.rlim_cur < hpage_size)
> > +               tst_brk(TCONF, "Locked memory ulimit set below huge page
> > size");
> >
> 
> These lines are better for moving into setup() phase. And, I'd propose
> printing the value of 'limit_info.rlim_cur' and 'hpage_size' when TCONF.
> 
> The default value of max-locked-memory is smaller than hpage_size on
> both RHEL8 and 9, which means this test will TCONF and skip running.
> I'm hesitating should we temporally cancel the limitations and make
> this test can really perform then restore that value to the original,
> is this change make sense? WDYT?
> 
Yeah, Incase if limit is smaller than expected, we can change it temporarily
to run the test. Will update in next revision. Also will change,
getrlimit/setrlimit function to SAFE_*.


> 
> 
> > +
> > +       test_simple_mlock(MAP_PRIVATE, "MAP_PRIVATE");
> > +       test_simple_mlock(MAP_SHARED, "MAP_SHARED");
> > +       test_simple_mlock(MAP_PRIVATE|MAP_LOCKED,
> > "MAP_PRIVATE|MAP_LOCKED");
> > +       test_simple_mlock(MAP_SHARED|MAP_LOCKED, "MAP_SHARED|MAP_LOCKED");
> >
> 
> If we define an additional function like flags_to_str(int flags) for
> converting
> the flag into a string, which will be more simple for reading.
> 
> static char *flags_to_str(int flags)
> {
>        ...
> }
> 
> static void test_simple_mlock(int flags)
> {
>         char *flags_str = flags_to_str(flags);
>         ...
> }
> 
It was not used so often and only some specific flags are used. I think
if there will be more general use case being some more mmap flags used in
application, then it would be good to change this to function.

Another way is to define a macro like

#define FLAGS_STR(flag) #flag

and pass it like: test_simple_mlock(flag, FLAGS_STR(flag));

But I thought, it is like passing the string itself.

what do you think?

> 
> 
> > +
> > +}
> > +
> > +static void setup(void)
> > +{
> > +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> > +}
> > +
> > +static void cleanup(void)
> > +{
> > +       if (fd >= 0)
> > +               SAFE_CLOSE(fd);
> > +}
> > +
> > +static struct tst_test test = {
> > +       .needs_root = 1,
> > +       .mntpoint = MNTPOINT,
> > +       .needs_hugetlbfs = 1,
> > +       .needs_tmpdir = 1,
> > +       .setup = setup,
> > +       .cleanup = cleanup,
> > +       .test_all = run_test,
> > +       .hugepages = {1, TST_NEEDS},
> > +};
> > --
> > 2.31.1
> >
> >
> 
> -- 
> Regards,
> Li Wang
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

* Re: [LTP] [PATCH 03/13] Hugetlb: Migrating libhugetlbfs mmap-gettest
  2022-12-26  7:33   ` Li Wang
@ 2022-12-26  9:41     ` Tarun Sahu
  0 siblings, 0 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-26  9:41 UTC (permalink / raw)
  To: Li Wang; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi Li,
Thanks for reviewing it.
Will take care of suggestions in next revision.

Thanks
Tarun

On Dec 26 2022, Li Wang wrote:
> On Sun, Dec 25, 2022 at 11:42 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:
> 
> > Migrating the libhugetlbfs/testcases/mmap-gettest.c test
> >
> > Test Description: This baseline test validates that a mapping of a
> > certain size can be created, correctly.  Once created, all the pages are
> > filled with a pattern and rechecked to test for corruption. The mapping is
> > then released.  This process is repeated for a specified number of
> > iterations.
> >
> > Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> >
> 
> Reviewed-by: Li Wang <liwang@redhat.com>
> 
> > ---
> >  runtest/hugetlb                               |   1 +
> >  testcases/kernel/mem/.gitignore               |   1 +
> >  .../kernel/mem/hugetlb/hugemmap/hugemmap22.c  | 104 ++++++++++++++++++
> >  3 files changed, 106 insertions(+)
> >  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
> >
> > diff --git a/runtest/hugetlb b/runtest/hugetlb
> > index 71b4d57e4..8e80db140 100644
> > --- a/runtest/hugetlb
> > +++ b/runtest/hugetlb
> > @@ -23,6 +23,7 @@ hugemmap18 hugemmap18
> >  hugemmap19 hugemmap19
> >  hugemmap20 hugemmap20
> >  hugemmap21 hugemmap21
> > +hugemmap22 hugemmap22
> >  hugemmap05_1 hugemmap05 -m
> >  hugemmap05_2 hugemmap05 -s
> >  hugemmap05_3 hugemmap05 -s -m
> > diff --git a/testcases/kernel/mem/.gitignore
> > b/testcases/kernel/mem/.gitignore
> > index 74edfa392..0fd01dbce 100644
> > --- a/testcases/kernel/mem/.gitignore
> > +++ b/testcases/kernel/mem/.gitignore
> > @@ -22,6 +22,7 @@
> >  /hugetlb/hugemmap/hugemmap19
> >  /hugetlb/hugemmap/hugemmap20
> >  /hugetlb/hugemmap/hugemmap21
> > +/hugetlb/hugemmap/hugemmap22
> >  /hugetlb/hugeshmat/hugeshmat01
> >  /hugetlb/hugeshmat/hugeshmat02
> >  /hugetlb/hugeshmat/hugeshmat03
> > diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
> > b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
> > new file mode 100644
> > index 000000000..50addbe62
> > --- /dev/null
> > +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap22.c
> > @@ -0,0 +1,104 @@
> > +// SPDX-License-Identifier: LGPL-2.1-or-later
> > +/*
> > + * Copyright (C) 2005-2006 IBM Corporation.
> > + * Author: David Gibson & Adam Litke
> > + */
> > +
> > +/*\
> > + * [Description]
> > + *
> > + * This baseline test validates that a mapping of a certain size can be
> > + * created, correctly.  Once created, all the pages are filled with a
> > + * pattern and rechecked to test for corruption. The mapping is then
> > + * released. This process is repeated for a specified number of
> > + * iterations.
> > + */
> > +
> > +#include <stdio.h>
> > +#include <sys/mount.h>
> > +#include <unistd.h>
> > +#include <unistd.h>
> > +
> > +#include "hugetlb.h"
> > +
> > +#define ITERATIONS 10
> >
> 
> I don't think we need too many iterations, it will cost too
> much time and easily timed out on a slower machine,
> so at most cycling up two times should be enough.
> 
> 
> 
> > +#define NR_HUGEPAGES 2
> > +#define MNTPOINT "hugetlbfs/"
> > +
> > +static unsigned long hpage_size;
> > +static int fd = -1;
> > +
> > +static void test_per_iteration(size_t size, int iter)
> > +{
> > +       char *m;
> > +       size_t i, j;
> > +       char pattern = 'A';
> > +
> > +       fd = tst_creat_unlinked(MNTPOINT, 0);
> > +       m = SAFE_MMAP(NULL, size, (PROT_READ|PROT_WRITE), MAP_SHARED, fd,
> > 0);
> > +
> > +       for (i = 0; i < NR_HUGEPAGES; i++) {
> > +               for (j = 0; j < hpage_size; j++) {
> > +                       if (*(m+(i*hpage_size)+j) != 0) {
> > +                               tst_res(TFAIL, "Iter %d: Verifying the
> > mmap area failed. "
> > +                                    "Got %c, expected 0", iter,
> > +                                    *(m+(i*hpage_size)+j));
> > +                               goto cleanup;
> > +                       }
> > +               }
> > +       }
> > +       for (i = 0; i < NR_HUGEPAGES; i++) {
> > +               pattern = 65+(i%26);
> > +               memset(m+(i*hpage_size), pattern, hpage_size);
> > +       }
> > +
> > +       for (i = 0; i < NR_HUGEPAGES; i++) {
> > +               pattern = 65+(i%26);
> > +               for (j = 0; j < hpage_size; j++) {
> > +                       if (*(m+(i*hpage_size)+j) != pattern) {
> > +                               tst_res(TFAIL, "Verifying the mmap area
> > failed. "
> > +                                    "got: %c, expected: %c",
> > +                                    *(m+(i*hpage_size)+j), pattern);
> > +                               goto cleanup;
> > +                       }
> > +               }
> > +       }
> > +
> > +cleanup:
> > +       SAFE_MUNMAP(m, size);
> > +       SAFE_CLOSE(fd);
> > +}
> > +
> > +static void run_test(void)
> > +{
> > +       size_t size;
> > +       int i;
> > +
> > +       size = NR_HUGEPAGES * hpage_size;
> > +       for (i = 0; i < ITERATIONS; i++)
> > +               test_per_iteration(size, i);
> > +
> > +       tst_res(TPASS, "Successfully verified the mmap area.");
> > +}
> > +
> > +static void setup(void)
> > +{
> > +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> >
> 
> hpage_size = tst_get_hugepage_size();
> 
> +}
> > +
> > +static void cleanup(void)
> > +{
> > +       if (fd >= 0)
> > +               SAFE_CLOSE(fd);
> > +}
> > +
> > +static struct tst_test test = {
> > +       .needs_root = 1,
> > +       .mntpoint = MNTPOINT,
> > +       .needs_hugetlbfs = 1,
> > +       .needs_tmpdir = 1,
> > +       .setup = setup,
> > +       .cleanup = cleanup,
> > +       .test_all = run_test,
> > +       .hugepages = {NR_HUGEPAGES, TST_NEEDS},
> > +};
> > --
> > 2.31.1
> >
> >
> 
> -- 
> Regards,
> Li Wang
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

* Re: [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock
  2022-12-26  9:39     ` Tarun Sahu
@ 2022-12-26  9:48       ` Li Wang
  0 siblings, 0 replies; 36+ messages in thread
From: Li Wang @ 2022-12-26  9:48 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Mon, Dec 26, 2022 at 5:39 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Hi Li,
> Thanks for reviewing the patch.
> Please find my comments inline.
>
> --skip
> On Dec 26 2022, Li Wang wrote:
> > On Sun, Dec 25, 2022 at 11:42 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:
> > > +static void run_test(void)
> > > +{
> > >
> >
> >
> >
> > > +       struct rlimit limit_info;
> > > +
> > > +       if (getrlimit(RLIMIT_MEMLOCK, &limit_info))
> > > +               tst_res(TWARN|TERRNO, "Unable to read locked memory
> > > rlimit");
> > > +       if (limit_info.rlim_cur < hpage_size)
> > > +               tst_brk(TCONF, "Locked memory ulimit set below huge
> page
> > > size");
> > >
> >
> > These lines are better for moving into setup() phase. And, I'd propose
> > printing the value of 'limit_info.rlim_cur' and 'hpage_size' when TCONF.
> >
> > The default value of max-locked-memory is smaller than hpage_size on
> > both RHEL8 and 9, which means this test will TCONF and skip running.
> > I'm hesitating should we temporally cancel the limitations and make
> > this test can really perform then restore that value to the original,
> > is this change make sense? WDYT?
> >
> Yeah, Incase if limit is smaller than expected, we can change it
> temporarily
> to run the test. Will update in next revision. Also will change,
> getrlimit/setrlimit function to SAFE_*.
>
>
> >
> >
> > > +
> > > +       test_simple_mlock(MAP_PRIVATE, "MAP_PRIVATE");
> > > +       test_simple_mlock(MAP_SHARED, "MAP_SHARED");
> > > +       test_simple_mlock(MAP_PRIVATE|MAP_LOCKED,
> > > "MAP_PRIVATE|MAP_LOCKED");
> > > +       test_simple_mlock(MAP_SHARED|MAP_LOCKED,
> "MAP_SHARED|MAP_LOCKED");
> > >
> >
> > If we define an additional function like flags_to_str(int flags) for
> > converting
> > the flag into a string, which will be more simple for reading.
> >
> > static char *flags_to_str(int flags)
> > {
> >        ...
> > }
> >
> > static void test_simple_mlock(int flags)
> > {
> >         char *flags_str = flags_to_str(flags);
> >         ...
> > }
> >
> It was not used so often and only some specific flags are used. I think
> if there will be more general use case being some more mmap flags used in
> application, then it would be good to change this to function.
>
> Another way is to define a macro like
>
> #define FLAGS_STR(flag) #flag
>
> and pass it like: test_simple_mlock(flag, FLAGS_STR(flag));
>
> But I thought, it is like passing the string itself.
>
> what do you think?
>

Yes sure, and I think of we have TST_TO_STR() in LTP as well.

$ git grep TST_TO_STR
tst_common.h:#define TST_TO_STR_(s) #s
tst_common.h:#define TST_TO_STR(s) TST_TO_STR_(s)

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 04/13] Hugetlb: Migrating libhugetlbfs mprotect
  2022-12-25 15:42 ` [LTP] [PATCH 04/13] Hugetlb: Migrating libhugetlbfs mprotect Tarun Sahu
@ 2022-12-27  5:48   ` Li Wang
  2022-12-27 16:00     ` Tarun Sahu
  0 siblings, 1 reply; 36+ messages in thread
From: Li Wang @ 2022-12-27  5:48 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi Tarun,

This test itself is no problem but looks a bit messy in code organization.
At least we need to do some refactored work to improve the readability and
reduce the (long-term) maintenance workload.


Tarun Sahu <tsahu@linux.ibm.com> wrote:

Migrating the libhugetlbfs/testcases/mprotect.c test
>
> Test Description: This test uses mprotect to change protection of
> hugepage mapping and perform read/write operation. It checks if the
> operation results in expected behaviour as per the protection.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |   1 +
>  testcases/kernel/mem/.gitignore               |   1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap23.c  | 244 ++++++++++++++++++
>  3 files changed, 246 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 8e80db140..8ade3c9ec 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -24,6 +24,7 @@ hugemmap19 hugemmap19
>  hugemmap20 hugemmap20
>  hugemmap21 hugemmap21
>  hugemmap22 hugemmap22
> +hugemmap23 hugemmap23
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index 0fd01dbce..ffd831f2e 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -23,6 +23,7 @@
>  /hugetlb/hugemmap/hugemmap20
>  /hugetlb/hugemmap/hugemmap21
>  /hugetlb/hugemmap/hugemmap22
> +/hugetlb/hugemmap/hugemmap23
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
> new file mode 100644
> index 000000000..687b192ae
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
> @@ -0,0 +1,244 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 IBM Corporation.
> + * Author: David Gibson & Adam Litke
> + */
> +
> +/*\
> + * [Description]
> + *
> + * This test uses mprotect to change protection of hugepage mapping and
> + * perform read/write operation. It checks if the operation results in
> + * expected behaviour as per the protection.
> + */
> +
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <unistd.h>
> +#include <sys/mman.h>
> +#include <setjmp.h>
> +#include <signal.h>
> +
> +#include "hugetlb.h"
> +
> +#ifndef barrier
> +# ifdef mb
> +       /* Redefining the mb() */
> +#   define barrier() mb()
> +# else
> +#   define barrier() __asm__ __volatile__ ("" : : : "memory")
> +# endif
> +#endif
> +
> +#define MNTPOINT "hugetlbfs/"
> +#define RANDOM_CONSTANT        0x1234ABCD
>

Avoid mixing tab and spaces in the macro definition.



> +
> +static int  fd = -1;
>

^ one space is enough.



> +static sigjmp_buf sig_escape;
> +static void *sig_expected = MAP_FAILED;
> +static long hpage_size;
> +
> +static void sig_handler(int signum, siginfo_t *si, void *uc)
> +{
> +       (void)uc;
> +       if (signum == SIGSEGV) {
> +               tst_res(TINFO, "SIGSEGV at %p (sig_expected=%p)",
> si->si_addr,
> +                      sig_expected);
> +               if (si->si_addr == sig_expected)
> +                       siglongjmp(sig_escape, 1);
> +               tst_res(TFAIL, "SIGSEGV somewhere unexpected");
> +       } else
> +               tst_res(TFAIL, "Unexpected signal %s", strsignal(signum));
> +}
> +
> +static int test_read(void *p)
> +{
> +       volatile unsigned long *pl = p;
> +       unsigned long x;
> +
> +       if (sigsetjmp(sig_escape, 1)) {
> +               /* We got a SEGV */
> +               sig_expected = MAP_FAILED;
> +               return -1;
> +       }
> +
> +       sig_expected = p;
> +       barrier();
> +       x = *pl;
> +       tst_res(TINFO, "Read back %lu", x);
> +       barrier();
> +       sig_expected = MAP_FAILED;
> +       return 0;
> +}
> +
> +static int test_write(void *p, unsigned long val)
> +{
> +       volatile unsigned long *pl = p;
> +       unsigned long x;
> +
> +       if (sigsetjmp(sig_escape, 1)) {
> +               /* We got a SEGV */
> +               sig_expected = MAP_FAILED;
> +               return -1;
> +       }
> +
> +       sig_expected = p;
> +       barrier();
> +       *pl = val;
> +       x = *pl;
> +       barrier();
> +       sig_expected = MAP_FAILED;
> +
> +       return (x != val);
> +}
> +
> +static int test_prot(void *p, int prot, char *prot_str)
> +{
> +       int r, w;
> +
> +       r = test_read(p);
> +       tst_res(TINFO, "On Read: %d", r);
> +       w = test_write(p, RANDOM_CONSTANT);
> +       tst_res(TINFO, "On Write: %d", w);
> +
> +       if (prot & PROT_READ) {
> +               if (r != 0) {
> +                       tst_res(TFAIL, "read failed on mmap(prot %s)",
> prot_str);
> +                       return -1;
> +               }
> +
> +       } else {
> +               if (r != -1) {
> +                       tst_res(TFAIL, "read succeeded on mmap(prot %s)",
> prot_str);
> +                       return -1;
> +               }
> +       }
> +
> +       if (prot & PROT_WRITE) {
> +               switch (w) {
> +               case -1:
> +                       tst_res(TFAIL, "write failed on mmap(prot %s)",
> prot_str);
> +                       return -1;
> +               case 0:
> +                       break;
> +               case 1:
> +                       tst_res(TFAIL, "write mismatch on mmap(prot %s)",
> prot_str);
> +                       return -1;
> +               default:
> +                       tst_res(TWARN, "Bug in test");
> +                       return -1;
> +               }
> +       } else {
> +               switch (w) {
> +               case -1:
> +                       break;
> +               case 0:
> +                       tst_res(TFAIL, "write succeeded on mmap(prot %s)",
> prot_str);
> +                       return -1;
> +               case 1:
> +                       tst_res(TFAIL, "write mismatch on mmap(prot %s)",
> prot_str);
> +                       return -1;
> +               default:
> +                       tst_res(TWARN, "Bug in test");
> +                       break;
> +               }
> +       }
> +       return 0;
> +}
> +
> +static int test_mprotect(int fd, char *testname,
> +                         unsigned long len1, int prot1, char *prot1_str,
> +                         unsigned long len2, int prot2, char *prot2_str)
> +{
> +       void *p;
> +       int ret;
> +
> +       tst_res(TINFO, "Testing %s", testname);
> +       tst_res(TINFO, "Mapping with prot %s", prot1_str);
> +       p = SAFE_MMAP(NULL, len1, prot1, MAP_SHARED, fd, 0);
> +
> +       ret = test_prot(p, prot1, prot1_str);
> +       if (ret)
> +               goto cleanup;
> +       tst_res(TINFO, "mprotect()ing to prot %s", prot2_str);
> +       ret = mprotect(p, len2, prot2);
> +       if (ret != 0) {
> +               tst_res(TFAIL|TERRNO, "%s: mprotect(prot %s)", testname,
> prot2_str);
> +               goto cleanup;
> +       }
> +
> +       ret = test_prot(p, prot2, prot2_str);
> +       if (ret)
> +               goto cleanup;
> +       if (len2 < len1)
> +               ret = test_prot(p + len2, prot1, prot1_str);
> +
> +cleanup:
> +       SAFE_MUNMAP(p, len1);
> +       return ret;
> +}
> +
> +static void run_test(void)
> +{
> +       void *p;
> +
>


> +       struct sigaction sa = {
> +               .sa_sigaction = sig_handler,
> +               .sa_flags = SA_SIGINFO,
> +       };
> +
> +       SAFE_SIGACTION(SIGSEGV, &sa, NULL);
> +
> +       fd = tst_creat_unlinked(MNTPOINT, 0);
> +       p = SAFE_MMAP(NULL, 2*hpage_size, PROT_READ|PROT_WRITE,
> MAP_SHARED, fd, 0);
> +       memset(p, 0, hpage_size);
> +       SAFE_MUNMAP(p, hpage_size);
>

I guess moving those preparation work into setup() is better.


> +
> +       if (test_mprotect(fd, "R->RW", hpage_size, PROT_READ, "PROT_READ",
> +                     hpage_size, PROT_READ|PROT_WRITE,
> "PROT_READ|PROT_WRITE"))
> +               goto cleanup;
> +       if (test_mprotect(fd, "RW->R", hpage_size, PROT_READ|PROT_WRITE,
> +                    "PROT_READ|PROT_WRITE", hpage_size, PROT_READ,
> "PROT_READ"))
> +               goto cleanup;
> +
> +       if (test_mprotect(fd, "R->RW 1/2", 2*hpage_size, PROT_READ,
> "PROT_READ",
> +                     hpage_size, PROT_READ|PROT_WRITE,
> "PROT_READ|PROT_WRITE"))
> +               goto cleanup;
> +       if (test_mprotect(fd, "RW->R 1/2", 2*hpage_size,
> PROT_READ|PROT_WRITE,
> +                     "PROT_READ|PROT_WRITE", hpage_size, PROT_READ,
> "PROT_READ"))
> +               goto cleanup;
> +
> +       if (test_mprotect(fd, "NONE->R", hpage_size, PROT_NONE,
> "PROT_NONE",
> +                     hpage_size, PROT_READ, "PROT_READ"))
> +               goto cleanup;
> +       if (test_mprotect(fd, "NONE->RW", hpage_size, PROT_NONE,
> "PROT_NONE",
> +                     hpage_size, PROT_READ|PROT_WRITE,
> "PROT_READ|PROT_WRITE"))
> +               goto cleanup;
>

Can we reorg those test options just into one structure for reducing
the duplicated code? Something maybe like:

static struct mprotect_param {
        char *tname;
        int pg_num;
        int prot1;
        char *prot1_str;
        int prot2;
        char *prot2_str;
} mprotect_params[] = {
        {"R->RW", 1,
                PROT_READ, "PROT_READ",
                PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"},

        {"RW->R", 1,
                PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE",
                PROT_READ, "PROT_READ"},

        {"R->RW 1/2", 2,
                PROT_READ, "PROT_READ",
                PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"},

        {"RW->R 1/2", 2,
                PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE",
                PROT_READ, "PROT_READ"},

        {"NONE->R", 1,
                PROT_NONE, "PROT_NONE",
                PROT_READ, "PROT_READ"},

        {"NONE->RW", 1,
                PROT_NONE, "PROT_NONE",
                PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"},
};

static void run_test(unsigned int i)
{
        struct mprotect_param *mpt = &mprotect_params[i];

        if (test_mprotect(fd, mpt->tname,
                        hpage_size * mpt->pg_num, mpt->prot1,
mpt->prot1_str,
                        hpage_size, mpt->prot2, mpt->prot2_str))
                return;

        tst_res(TPASS, "Successfully tested mprotect with hugetlb area\n");
}

static void setup(void)
{
        struct sigaction sa = {
                .sa_sigaction = sig_handler,
                .sa_flags = SA_SIGINFO,
        };

        SAFE_SIGACTION(SIGSEGV, &sa, NULL);

        hpage_size = tst_get_hugepage_size();

        fd = tst_creat_unlinked(MNTPOINT, 0);
        p = SAFE_MMAP(NULL, 2*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
fd, 0);

        memset(p, 0, hpage_size);
        SAFE_MUNMAP(p, hpage_size);
}

static void cleanup(void)
{
        SAFE_MUNMAP(p+hpage_size, hpage_size);

        if (fd >= 0)
                SAFE_CLOSE(fd);
}

static struct tst_test test = {
        .tcnt = ARRAY_SIZE(mprotect_params),
        .needs_root = 1,
        .mntpoint = MNTPOINT,
        .needs_hugetlbfs = 1,
        .needs_tmpdir = 1,
        .setup = setup,
        .cleanup = cleanup,
        .test = run_test,
        .hugepages = {2, TST_NEEDS},
};



> +
> +       tst_res(TPASS, "Successfully tested mprotect with hugetlb area");
>



> +cleanup:
> +       SAFE_MUNMAP(p+hpage_size, hpage_size);
> +       SAFE_CLOSE(fd);
>

we can move this into cleanup() function as well.



> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> +}
> +
> +static void cleanup(void)
> +{
> +       if (fd >= 0)
> +               SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {2, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  2022-12-25 15:42 ` [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge Tarun Sahu
@ 2022-12-27  6:31   ` Li Wang
  2022-12-27 18:21     ` Tarun Sahu
  0 siblings, 1 reply; 36+ messages in thread
From: Li Wang @ 2022-12-27  6:31 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:42 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/mremap-fixed-normal-near-huge.c test
>
> Test Description: The kernel has bug for mremap() on some architecture.
> mremap() can cause crashes on architectures with holes in the address
> space (like ia64) and on powerpc with it's distinct page size "slices".
>
> This test get the huge mapping address and mremap() normal mapping
> near to this huge mapping.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |   1 +
>  testcases/kernel/mem/.gitignore               |   1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap26.c  | 131 ++++++++++++++++++
>  3 files changed, 133 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 65265b0fe..55185f2f7 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -26,6 +26,7 @@ hugemmap21 hugemmap21
>  hugemmap22 hugemmap22
>  hugemmap23 hugemmap23
>  hugemmap25 hugemmap25
> +hugemmap26 hugemmap26
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index c865a1e55..4886c6a5f 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -25,6 +25,7 @@
>  /hugetlb/hugemmap/hugemmap22
>  /hugetlb/hugemmap/hugemmap23
>  /hugetlb/hugemmap/hugemmap25
> +/hugetlb/hugemmap/hugemmap26
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c
> new file mode 100644
> index 000000000..03dd248bd
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap26.c
> @@ -0,0 +1,131 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2009 IBM Corporation.
> + * Author: David Gibson
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test Description: The kernel has bug for mremap() on some architecture.
> + * mremap() can cause crashes on architectures with holes in the address
> + * space (like ia64) and on powerpc with it's distinct page size "slices".
> + *
> + * This test get the huge mapping address and mremap() normal mapping
> + * near to this huge mapping.
> + */
> +
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <limits.h>
> +#include <sys/param.h>
> +#include <sys/types.h>
> +
> +#include "hugetlb.h"
> +
> +#define RANDOM_CONSTANT        0x1234ABCD
> +#define MNTPOINT "hugetlbfs/"
> +
> +static int  fd = -1;
> +static long hpage_size, page_size;
> +
> +static int do_readback(void *p, size_t size, const char *stage)
> +{
> +       unsigned int *q = p;
> +       size_t i;
> +
> +       tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
> +              (unsigned long)size, stage);
> +
> +       for (i = 0; i < (size / sizeof(*q)); i++)
> +               q[i] = RANDOM_CONSTANT ^ i;
> +
> +       for (i = 0; i < (size / sizeof(*q)); i++) {
> +               if (q[i] != (RANDOM_CONSTANT ^ i)) {
> +                       tst_res(TFAIL, "Stage \"%s\": Mismatch at offset
> 0x%lx: 0x%x "
> +                                       "instead of 0x%lx", stage, i,
> q[i], RANDOM_CONSTANT ^ i);
> +                       return -1;
> +               }
> +       }
> +       return 0;
> +}
> +
> +static int do_remap(void *target)
> +{
> +       void *a, *b;
> +       int ret;
> +
> +       a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
> +                 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
> +
> +       ret = do_readback(a, page_size, "base normal");
> +       if (ret)
> +               goto cleanup;
> +       b = mremap(a, page_size, page_size, MREMAP_MAYMOVE | MREMAP_FIXED,
> +                  target);
> +
> +       if (b != MAP_FAILED) {
> +               do_readback(b, page_size, "remapped");
> +               a = b;
> +       } else
> +               tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED) disallowed");
> +
> +cleanup:
> +       SAFE_MUNMAP(a, page_size);
> +       return ret;
> +}
>

Those two functions do_readback() and do_remap() are
copy&past from hugemmap2[4|5].c, what about extracting
them into a common header file(tst_hugetlb.h or mem.h) for
easy reusing?

And I also noticed other tests (hugemmap27) using the same
macro mem barrier(), it'd be great to define it in a header file.


-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 08/13] Hugetlb: Migrating libhugetlbfs noresv-regarded-as-resv
  2022-12-25 15:42 ` [LTP] [PATCH 08/13] Hugetlb: Migrating libhugetlbfs noresv-regarded-as-resv Tarun Sahu
@ 2022-12-27  6:47   ` Li Wang
  0 siblings, 0 replies; 36+ messages in thread
From: Li Wang @ 2022-12-27  6:47 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Tarun Sahu <tsahu@linux.ibm.com> wrote:

Migrating the libhugetlbfs/testcases/noresv-regarded-as-resv.c test
>
> Test Description: Test to correct handling for reserve count. If no
> reserved mapping is created to reserved file region, it should be
> considered as reserve mapping. Otherwise, reserve count will be
> overflowed.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
>

Reviewed-by: Li Wang <liwang@redhat.com>


-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 09/13] Hugetlb: Migrating libhugetlbfs private
  2022-12-25 15:42 ` [LTP] [PATCH 09/13] Hugetlb: Migrating libhugetlbfs private Tarun Sahu
@ 2022-12-27  6:56   ` Li Wang
  0 siblings, 0 replies; 36+ messages in thread
From: Li Wang @ 2022-12-27  6:56 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:43 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/private.c test
>
> Test Description: The test do mmap() with shared mapping and write.
> It matches the data with private mmap() and then change it with other
> data. It checks shared mapping data if data is not contaiminated by
> private mapping.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |   1 +
>  testcases/kernel/mem/.gitignore               |   1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap29.c  | 115 ++++++++++++++++++
>  3 files changed, 117 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 95afe009e..6ec8d1018 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -29,6 +29,7 @@ hugemmap25 hugemmap25
>  hugemmap26 hugemmap26
>  hugemmap27 hugemmap27
>  hugemmap28 hugemmap28
> +hugemmap29 hugemmap29
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index 2f8ed0df0..fef0a76d6 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -28,6 +28,7 @@
>  /hugetlb/hugemmap/hugemmap26
>  /hugetlb/hugemmap/hugemmap27
>  /hugetlb/hugemmap/hugemmap28
> +/hugetlb/hugemmap/hugemmap29
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c
> new file mode 100644
> index 000000000..2d921e169
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap29.c
> @@ -0,0 +1,115 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 IBM Corporation.
> + * Author: David Gibson & Adam Litke
> + */
> +
> +/*\
> + * [Description]
> + *
> + * The test do mmap() with shared mapping and write. It matches the data
> + * with private mmap() and then change it with other data. It checks
> + * shared mapping data if data is not contaiminated by private mapping.
> + * Similiarly checks for private data if it is not contaminated by
> changing
> + * shared mmap data.
> + */
> +
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <limits.h>
> +#include <sys/param.h>
> +#include <setjmp.h>
> +#include <sys/types.h>
> +
> +#include "hugetlb.h"
> +
> +#define C1     0x1234ABCD
> +#define C2  0xFEDC9876
>

Dont mix tabs and spaces ^

Reviewed-by: Li Wang <liwang@redhat.com>



> +
> +#define MNTPOINT "hugetlbfs/"
> +static unsigned long hpage_size;
> +static int fd = -1;
> +
> +static void run_test(void)
> +{
> +       void *p, *q;
> +       unsigned int *pl, *ql;
> +       unsigned long i;
> +
> +       fd = tst_creat_unlinked(MNTPOINT, 0);
> +       p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
> +                fd, 0);
> +
> +       pl = p;
> +       for (i = 0; i < (hpage_size / sizeof(*pl)); i++)
> +               pl[i] = C1 ^ i;
> +
> +       q = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
> +                fd, 0);
> +
> +       ql = q;
> +       for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
> +               if (ql[i] != (C1 ^ i)) {
> +                       tst_res(TFAIL, "Mismatch at offset %lu, got: %u,
> expected: %lu",
> +                                       i, ql[i], C1 ^ i);
> +                       goto cleanup;
> +               }
> +       }
> +
> +       for (i = 0; i < (hpage_size / sizeof(*ql)); i++)
> +               ql[i] = C2 ^ i;
> +
> +       for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
> +               if (ql[i] != (C2 ^ i)) {
> +                       tst_res(TFAIL, "PRIVATE mismatch at offset %lu,
> got: %u, expected: %lu",
> +                                       i, ql[i], C2 ^ i);
> +                       goto cleanup;
> +               }
> +       }
> +
> +       for (i = 0; i < (hpage_size / sizeof(*pl)); i++) {
> +               if (pl[i] != (C1 ^ i)) {
> +                       tst_res(TFAIL, "SHARED map contaminated at offset
> %lu, "
> +                                       "got: %u, expected: %lu", i,
> pl[i], C1 ^ i);
> +                       goto cleanup;
> +               }
> +       }
> +
> +       memset(p, 0, hpage_size);
> +
> +       for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
> +               if (ql[i] != (C2 ^ i)) {
> +                       tst_res(TFAIL, "PRIVATE map contaminated at offset
> %lu, "
> +                                       "got: %u, expected: %lu", i,
> ql[i], C2 ^ i);
> +                       goto cleanup;
> +               }
> +       }
> +       tst_res(TPASS, "Successfully tested shared/private mmaping and its
> data");
> +cleanup:
> +       SAFE_MUNMAP(p, hpage_size);
> +       SAFE_MUNMAP(q, hpage_size);
> +       SAFE_CLOSE(fd);
> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> +}
> +
> +static void cleanup(void)
> +{
> +       if (fd >= 0)
> +               SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {2, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 10/13] Hugetlb: Migrating libhugetlbfs readahead_reserve
  2022-12-25 15:42 ` [LTP] [PATCH 10/13] Hugetlb: Migrating libhugetlbfs readahead_reserve Tarun Sahu
@ 2022-12-27  7:05   ` Li Wang
  0 siblings, 0 replies; 36+ messages in thread
From: Li Wang @ 2022-12-27  7:05 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:43 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/readahead_reserve.c test
>
> Test Description: readahead() on some kernels can cause the reservation
> counter to get corrupted. The problem is that the patches are allocated
> for the reservation but not faulted in at the time of allocation. The
> counters do not get updated and effectively "leak". This test
> identifies whether the kernel is vulnerable to the problem or not.
> It's fixed in kernel by 'commit f2deae9d4e70
> ("Remove implementation of readpage from the hugetlbfs_aops")'.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |  1 +
>  testcases/kernel/mem/.gitignore               |  1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap30.c  | 85 +++++++++++++++++++
>  3 files changed, 87 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 6ec8d1018..60cca4eb7 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -30,6 +30,7 @@ hugemmap26 hugemmap26
>  hugemmap27 hugemmap27
>  hugemmap28 hugemmap28
>  hugemmap29 hugemmap29
> +hugemmap30 hugemmap30
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index fef0a76d6..bb9720452 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -29,6 +29,7 @@
>  /hugetlb/hugemmap/hugemmap27
>  /hugetlb/hugemmap/hugemmap28
>  /hugetlb/hugemmap/hugemmap29
> +/hugetlb/hugemmap/hugemmap30
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c
> new file mode 100644
> index 000000000..9a0709af1
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap30.c
> @@ -0,0 +1,85 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 IBM Corporation.
> + * Author: Mel Gorman
> + */
> +
> +/*\
> + * [Description]
> + *
> + * readahead() on some kernels can cause the reservation counter to get
> + * corrupted. The problem is that the pages are allocated for the
> + * reservation but not faulted in at the time of allocation. The
> + * counters do not get updated and effectively "leak". This test
> + * identifies whether the kernel is vulnerable to the problem or not.
> + * It's fixed in kernel by commit f2deae9d4e70.
> + */
> +
> +#define _GNU_SOURCE
>


> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/mount.h>
> +#include <limits.h>
> +#include <sys/param.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/types.h>
>

I just found these header files are not needed, this test
still compiles fine without including them. The possible
reason is that we already indirectly include them via hugetlb.h.

This comment also works for other tests.

Reviewed-by: Li Wang <liwang@redhat.com>



> +
> +#include "hugetlb.h"
> +
> +#define MNTPOINT "hugetlbfs/"
> +static long hpage_size;
> +static int fd = -1;
> +
> +static void run_test(void)
> +{
> +       void *p;
> +       unsigned long initial_rsvd, map_rsvd, readahead_rsvd, end_rsvd;
> +
> +       fd = tst_creat_unlinked(MNTPOINT, 0);
> +       initial_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
> +
> +       p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
> +                fd, 0);
> +       map_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
> +       tst_res(TINFO, "map_rsvd: %lu", map_rsvd);
> +
> +       readahead(fd, 0, hpage_size);
> +       readahead_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
> +       tst_res(TINFO, "readahead_rsvd: %lu", readahead_rsvd);
> +
> +       memset(p, 1, hpage_size);
> +
> +       SAFE_MUNMAP(p, hpage_size);
> +       SAFE_CLOSE(fd);
> +       end_rsvd = SAFE_READ_MEMINFO(MEMINFO_HPAGE_RSVD);
> +
> +       TST_EXP_EQ_LU(end_rsvd, initial_rsvd);
> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> +}
> +
> +static void cleanup(void)
> +{
> +       if (fd >= 0)
> +               SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +       .tags = (struct tst_tag[]) {
> +               {"linux-git", "f2deae9d4e70"},
> +               {}
> +       },
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {1, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 11/13] Hugetlb: Migrating libhugetlbfs shared
  2022-12-25 15:42 ` [LTP] [PATCH 11/13] Hugetlb: Migrating libhugetlbfs shared Tarun Sahu
@ 2022-12-27  7:08   ` Li Wang
  0 siblings, 0 replies; 36+ messages in thread
From: Li Wang @ 2022-12-27  7:08 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:43 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/shared.c test
>
> Test Description: This test is basic shared mapping test. Two shared
> mappings are created with same offset on a file. It checks if writing
> to one mapping can be seen to other mapping or not?
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
>

Reviewed-by: Li Wang <liwang@redhat.com>

> ---
>  runtest/hugetlb                               |  1 +
>  testcases/kernel/mem/.gitignore               |  1 +
>  .../kernel/mem/hugetlb/hugemmap/hugemmap31.c  | 84 +++++++++++++++++++
>  3 files changed, 86 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 60cca4eb7..33fd384b4 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -31,6 +31,7 @@ hugemmap27 hugemmap27
>  hugemmap28 hugemmap28
>  hugemmap29 hugemmap29
>  hugemmap30 hugemmap30
> +hugemmap31 hugemmap31
>  hugemmap05_1 hugemmap05 -m
>  hugemmap05_2 hugemmap05 -s
>  hugemmap05_3 hugemmap05 -s -m
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index bb9720452..8375389cd 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -30,6 +30,7 @@
>  /hugetlb/hugemmap/hugemmap28
>  /hugetlb/hugemmap/hugemmap29
>  /hugetlb/hugemmap/hugemmap30
> +/hugetlb/hugemmap/hugemmap31
>  /hugetlb/hugeshmat/hugeshmat01
>  /hugetlb/hugeshmat/hugeshmat02
>  /hugetlb/hugeshmat/hugeshmat03
> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c
> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c
> new file mode 100644
> index 000000000..a09905023
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap31.c
> @@ -0,0 +1,84 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 IBM Corporation.
> + * Author: David Gibson & Adam Litke
> + */
> +
> +/*\
> + * [Description]
> + *
> + * This test is basic shared mapping test. Two shared mappings are created
> + * with same offset on a file. It checks if writing to one mapping can be
> + * seen to other mapping or not?
> + */
> +
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <limits.h>
> +#include <sys/param.h>
> +#include <setjmp.h>
> +#include <sys/types.h>
> +
> +#include "hugetlb.h"
> +
> +#define RANDOM_CONSTANT        0x1234ABCD
> +#define MNTPOINT "hugetlbfs/"
> +
> +static long hpage_size;
> +static int fd = -1;
> +
> +static void run_test(void)
> +{
> +       void *p, *q;
> +       unsigned long *pl, *ql;
> +       unsigned long i;
> +
> +       fd = tst_creat_unlinked(MNTPOINT, 0);
> +       p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
> +                fd, 0);
> +
> +       q = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
> +                fd, 0);
> +
> +       pl = p;
> +       for (i = 0; i < (hpage_size / sizeof(*pl)); i++)
> +               pl[i] = RANDOM_CONSTANT ^ i;
> +
> +       ql = q;
> +       for (i = 0; i < (hpage_size / sizeof(*ql)); i++) {
> +               if (ql[i] != (RANDOM_CONSTANT ^ i)) {
> +                       tst_res(TFAIL, "Mismatch at offset %lu, Got: %lu,
> Expected: %lu",
> +                                       i, ql[i], RANDOM_CONSTANT ^ i);
> +                       goto cleanup;
> +               }
> +       }
> +
> +       tst_res(TPASS, "Successfully tested data between two shared
> mappings");
> +cleanup:
> +       SAFE_MUNMAP(p, hpage_size);
> +       SAFE_MUNMAP(q, hpage_size);
> +       SAFE_CLOSE(fd);
> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> +}
> +
> +static void cleanup(void)
> +{
> +       if (fd >= 0)
> +               SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {2, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 04/13] Hugetlb: Migrating libhugetlbfs mprotect
  2022-12-27  5:48   ` Li Wang
@ 2022-12-27 16:00     ` Tarun Sahu
  0 siblings, 0 replies; 36+ messages in thread
From: Tarun Sahu @ 2022-12-27 16:00 UTC (permalink / raw)
  To: Li Wang; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi Li,
Thanks for reviewing this patch.
I will rework on this.

Li Wang <liwang@redhat.com> writes:

> Hi Tarun,
>
> This test itself is no problem but looks a bit messy in code organization.
> At least we need to do some refactored work to improve the readability and
> reduce the (long-term) maintenance workload.
>
>
> Tarun Sahu <tsahu@linux.ibm.com> wrote:
>
> Migrating the libhugetlbfs/testcases/mprotect.c test
>>
>> Test Description: This test uses mprotect to change protection of
>> hugepage mapping and perform read/write operation. It checks if the
>> operation results in expected behaviour as per the protection.
>>
>> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
>> ---
>>  runtest/hugetlb                               |   1 +
>>  testcases/kernel/mem/.gitignore               |   1 +
>>  .../kernel/mem/hugetlb/hugemmap/hugemmap23.c  | 244 ++++++++++++++++++
>>  3 files changed, 246 insertions(+)
>>  create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
>>
>> diff --git a/runtest/hugetlb b/runtest/hugetlb
>> index 8e80db140..8ade3c9ec 100644
>> --- a/runtest/hugetlb
>> +++ b/runtest/hugetlb
>> @@ -24,6 +24,7 @@ hugemmap19 hugemmap19
>>  hugemmap20 hugemmap20
>>  hugemmap21 hugemmap21
>>  hugemmap22 hugemmap22
>> +hugemmap23 hugemmap23
>>  hugemmap05_1 hugemmap05 -m
>>  hugemmap05_2 hugemmap05 -s
>>  hugemmap05_3 hugemmap05 -s -m
>> diff --git a/testcases/kernel/mem/.gitignore
>> b/testcases/kernel/mem/.gitignore
>> index 0fd01dbce..ffd831f2e 100644
>> --- a/testcases/kernel/mem/.gitignore
>> +++ b/testcases/kernel/mem/.gitignore
>> @@ -23,6 +23,7 @@
>>  /hugetlb/hugemmap/hugemmap20
>>  /hugetlb/hugemmap/hugemmap21
>>  /hugetlb/hugemmap/hugemmap22
>> +/hugetlb/hugemmap/hugemmap23
>>  /hugetlb/hugeshmat/hugeshmat01
>>  /hugetlb/hugeshmat/hugeshmat02
>>  /hugetlb/hugeshmat/hugeshmat03
>> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
>> b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
>> new file mode 100644
>> index 000000000..687b192ae
>> --- /dev/null
>> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap23.c
>> @@ -0,0 +1,244 @@
>> +// SPDX-License-Identifier: LGPL-2.1-or-later
>> +/*
>> + * Copyright (C) 2005-2006 IBM Corporation.
>> + * Author: David Gibson & Adam Litke
>> + */
>> +
>> +/*\
>> + * [Description]
>> + *
>> + * This test uses mprotect to change protection of hugepage mapping and
>> + * perform read/write operation. It checks if the operation results in
>> + * expected behaviour as per the protection.
>> + */
>> +
>> +#include <stdio.h>
>> +#include <sys/mount.h>
>> +#include <unistd.h>
>> +#include <sys/mman.h>
>> +#include <setjmp.h>
>> +#include <signal.h>
>> +
>> +#include "hugetlb.h"
>> +
>> +#ifndef barrier
>> +# ifdef mb
>> +       /* Redefining the mb() */
>> +#   define barrier() mb()
>> +# else
>> +#   define barrier() __asm__ __volatile__ ("" : : : "memory")
>> +# endif
>> +#endif
>> +
>> +#define MNTPOINT "hugetlbfs/"
>> +#define RANDOM_CONSTANT        0x1234ABCD
>>
>
> Avoid mixing tab and spaces in the macro definition.
>
>
>
>> +
>> +static int  fd = -1;
>>
>
> ^ one space is enough.
>
>
>
>> +static sigjmp_buf sig_escape;
>> +static void *sig_expected = MAP_FAILED;
>> +static long hpage_size;
>> +
>> +static void sig_handler(int signum, siginfo_t *si, void *uc)
>> +{
>> +       (void)uc;
>> +       if (signum == SIGSEGV) {
>> +               tst_res(TINFO, "SIGSEGV at %p (sig_expected=%p)",
>> si->si_addr,
>> +                      sig_expected);
>> +               if (si->si_addr == sig_expected)
>> +                       siglongjmp(sig_escape, 1);
>> +               tst_res(TFAIL, "SIGSEGV somewhere unexpected");
>> +       } else
>> +               tst_res(TFAIL, "Unexpected signal %s", strsignal(signum));
>> +}
>> +
>> +static int test_read(void *p)
>> +{
>> +       volatile unsigned long *pl = p;
>> +       unsigned long x;
>> +
>> +       if (sigsetjmp(sig_escape, 1)) {
>> +               /* We got a SEGV */
>> +               sig_expected = MAP_FAILED;
>> +               return -1;
>> +       }
>> +
>> +       sig_expected = p;
>> +       barrier();
>> +       x = *pl;
>> +       tst_res(TINFO, "Read back %lu", x);
>> +       barrier();
>> +       sig_expected = MAP_FAILED;
>> +       return 0;
>> +}
>> +
>> +static int test_write(void *p, unsigned long val)
>> +{
>> +       volatile unsigned long *pl = p;
>> +       unsigned long x;
>> +
>> +       if (sigsetjmp(sig_escape, 1)) {
>> +               /* We got a SEGV */
>> +               sig_expected = MAP_FAILED;
>> +               return -1;
>> +       }
>> +
>> +       sig_expected = p;
>> +       barrier();
>> +       *pl = val;
>> +       x = *pl;
>> +       barrier();
>> +       sig_expected = MAP_FAILED;
>> +
>> +       return (x != val);
>> +}
>> +
>> +static int test_prot(void *p, int prot, char *prot_str)
>> +{
>> +       int r, w;
>> +
>> +       r = test_read(p);
>> +       tst_res(TINFO, "On Read: %d", r);
>> +       w = test_write(p, RANDOM_CONSTANT);
>> +       tst_res(TINFO, "On Write: %d", w);
>> +
>> +       if (prot & PROT_READ) {
>> +               if (r != 0) {
>> +                       tst_res(TFAIL, "read failed on mmap(prot %s)",
>> prot_str);
>> +                       return -1;
>> +               }
>> +
>> +       } else {
>> +               if (r != -1) {
>> +                       tst_res(TFAIL, "read succeeded on mmap(prot %s)",
>> prot_str);
>> +                       return -1;
>> +               }
>> +       }
>> +
>> +       if (prot & PROT_WRITE) {
>> +               switch (w) {
>> +               case -1:
>> +                       tst_res(TFAIL, "write failed on mmap(prot %s)",
>> prot_str);
>> +                       return -1;
>> +               case 0:
>> +                       break;
>> +               case 1:
>> +                       tst_res(TFAIL, "write mismatch on mmap(prot %s)",
>> prot_str);
>> +                       return -1;
>> +               default:
>> +                       tst_res(TWARN, "Bug in test");
>> +                       return -1;
>> +               }
>> +       } else {
>> +               switch (w) {
>> +               case -1:
>> +                       break;
>> +               case 0:
>> +                       tst_res(TFAIL, "write succeeded on mmap(prot %s)",
>> prot_str);
>> +                       return -1;
>> +               case 1:
>> +                       tst_res(TFAIL, "write mismatch on mmap(prot %s)",
>> prot_str);
>> +                       return -1;
>> +               default:
>> +                       tst_res(TWARN, "Bug in test");
>> +                       break;
>> +               }
>> +       }
>> +       return 0;
>> +}
>> +
>> +static int test_mprotect(int fd, char *testname,
>> +                         unsigned long len1, int prot1, char *prot1_str,
>> +                         unsigned long len2, int prot2, char *prot2_str)
>> +{
>> +       void *p;
>> +       int ret;
>> +
>> +       tst_res(TINFO, "Testing %s", testname);
>> +       tst_res(TINFO, "Mapping with prot %s", prot1_str);
>> +       p = SAFE_MMAP(NULL, len1, prot1, MAP_SHARED, fd, 0);
>> +
>> +       ret = test_prot(p, prot1, prot1_str);
>> +       if (ret)
>> +               goto cleanup;
>> +       tst_res(TINFO, "mprotect()ing to prot %s", prot2_str);
>> +       ret = mprotect(p, len2, prot2);
>> +       if (ret != 0) {
>> +               tst_res(TFAIL|TERRNO, "%s: mprotect(prot %s)", testname,
>> prot2_str);
>> +               goto cleanup;
>> +       }
>> +
>> +       ret = test_prot(p, prot2, prot2_str);
>> +       if (ret)
>> +               goto cleanup;
>> +       if (len2 < len1)
>> +               ret = test_prot(p + len2, prot1, prot1_str);
>> +
>> +cleanup:
>> +       SAFE_MUNMAP(p, len1);
>> +       return ret;
>> +}
>> +
>> +static void run_test(void)
>> +{
>> +       void *p;
>> +
>>
>
>
>> +       struct sigaction sa = {
>> +               .sa_sigaction = sig_handler,
>> +               .sa_flags = SA_SIGINFO,
>> +       };
>> +
>> +       SAFE_SIGACTION(SIGSEGV, &sa, NULL);
>> +
>> +       fd = tst_creat_unlinked(MNTPOINT, 0);
>> +       p = SAFE_MMAP(NULL, 2*hpage_size, PROT_READ|PROT_WRITE,
>> MAP_SHARED, fd, 0);
>> +       memset(p, 0, hpage_size);
>> +       SAFE_MUNMAP(p, hpage_size);
>>
>
> I guess moving those preparation work into setup() is better.
>
>
>> +
>> +       if (test_mprotect(fd, "R->RW", hpage_size, PROT_READ, "PROT_READ",
>> +                     hpage_size, PROT_READ|PROT_WRITE,
>> "PROT_READ|PROT_WRITE"))
>> +               goto cleanup;
>> +       if (test_mprotect(fd, "RW->R", hpage_size, PROT_READ|PROT_WRITE,
>> +                    "PROT_READ|PROT_WRITE", hpage_size, PROT_READ,
>> "PROT_READ"))
>> +               goto cleanup;
>> +
>> +       if (test_mprotect(fd, "R->RW 1/2", 2*hpage_size, PROT_READ,
>> "PROT_READ",
>> +                     hpage_size, PROT_READ|PROT_WRITE,
>> "PROT_READ|PROT_WRITE"))
>> +               goto cleanup;
>> +       if (test_mprotect(fd, "RW->R 1/2", 2*hpage_size,
>> PROT_READ|PROT_WRITE,
>> +                     "PROT_READ|PROT_WRITE", hpage_size, PROT_READ,
>> "PROT_READ"))
>> +               goto cleanup;
>> +
>> +       if (test_mprotect(fd, "NONE->R", hpage_size, PROT_NONE,
>> "PROT_NONE",
>> +                     hpage_size, PROT_READ, "PROT_READ"))
>> +               goto cleanup;
>> +       if (test_mprotect(fd, "NONE->RW", hpage_size, PROT_NONE,
>> "PROT_NONE",
>> +                     hpage_size, PROT_READ|PROT_WRITE,
>> "PROT_READ|PROT_WRITE"))
>> +               goto cleanup;
>>
>
> Can we reorg those test options just into one structure for reducing
> the duplicated code? Something maybe like:
>
> static struct mprotect_param {
>         char *tname;
>         int pg_num;
>         int prot1;
>         char *prot1_str;
>         int prot2;
>         char *prot2_str;
> } mprotect_params[] = {
>         {"R->RW", 1,
>                 PROT_READ, "PROT_READ",
>                 PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"},
>
>         {"RW->R", 1,
>                 PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE",
>                 PROT_READ, "PROT_READ"},
>
>         {"R->RW 1/2", 2,
>                 PROT_READ, "PROT_READ",
>                 PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"},
>
>         {"RW->R 1/2", 2,
>                 PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE",
>                 PROT_READ, "PROT_READ"},
>
>         {"NONE->R", 1,
>                 PROT_NONE, "PROT_NONE",
>                 PROT_READ, "PROT_READ"},
>
>         {"NONE->RW", 1,
>                 PROT_NONE, "PROT_NONE",
>                 PROT_READ|PROT_WRITE, "PROT_READ|PROT_WRITE"},
> };
>
> static void run_test(unsigned int i)
> {
>         struct mprotect_param *mpt = &mprotect_params[i];
>
>         if (test_mprotect(fd, mpt->tname,
>                         hpage_size * mpt->pg_num, mpt->prot1,
> mpt->prot1_str,
>                         hpage_size, mpt->prot2, mpt->prot2_str))
>                 return;
>
>         tst_res(TPASS, "Successfully tested mprotect with hugetlb area\n");
> }
>
> static void setup(void)
> {
>         struct sigaction sa = {
>                 .sa_sigaction = sig_handler,
>                 .sa_flags = SA_SIGINFO,
>         };
>
>         SAFE_SIGACTION(SIGSEGV, &sa, NULL);
>
>         hpage_size = tst_get_hugepage_size();
>
>         fd = tst_creat_unlinked(MNTPOINT, 0);
>         p = SAFE_MMAP(NULL, 2*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
> fd, 0);
>
>         memset(p, 0, hpage_size);
>         SAFE_MUNMAP(p, hpage_size);
> }
>
> static void cleanup(void)
> {
>         SAFE_MUNMAP(p+hpage_size, hpage_size);
>
>         if (fd >= 0)
>                 SAFE_CLOSE(fd);
> }
>
> static struct tst_test test = {
>         .tcnt = ARRAY_SIZE(mprotect_params),
>         .needs_root = 1,
>         .mntpoint = MNTPOINT,
>         .needs_hugetlbfs = 1,
>         .needs_tmpdir = 1,
>         .setup = setup,
>         .cleanup = cleanup,
>         .test = run_test,
>         .hugepages = {2, TST_NEEDS},
> };
>
>
>
>> +
>> +       tst_res(TPASS, "Successfully tested mprotect with hugetlb area");
>>
>
>
>
>> +cleanup:
>> +       SAFE_MUNMAP(p+hpage_size, hpage_size);
>> +       SAFE_CLOSE(fd);
>>
>
> we can move this into cleanup() function as well.
>
>
>
>> +}
>> +
>> +static void setup(void)
>> +{
>> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
>> +}
>> +
>> +static void cleanup(void)
>> +{
>> +       if (fd >= 0)
>> +               SAFE_CLOSE(fd);
>> +}
>> +
>> +static struct tst_test test = {
>> +       .needs_root = 1,
>> +       .mntpoint = MNTPOINT,
>> +       .needs_hugetlbfs = 1,
>> +       .needs_tmpdir = 1,
>> +       .setup = setup,
>> +       .cleanup = cleanup,
>> +       .test_all = run_test,
>> +       .hugepages = {2, TST_NEEDS},
>> +};
>> --
>> 2.31.1
>>
>>
>
> -- 
> Regards,
> Li Wang
>
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

* Re: [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  2022-12-27  6:31   ` Li Wang
@ 2022-12-27 18:21     ` Tarun Sahu
  2022-12-29 19:06       ` Tarun Sahu
  0 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-27 18:21 UTC (permalink / raw)
  To: Li Wang; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi Li,
Thanks for reviewing the patch.
I will update it in next revision.

--skip
>> +static int do_readback(void *p, size_t size, const char *stage)
>> +{
>> +       unsigned int *q = p;
>> +       size_t i;
>> +
>> +       tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
>> +              (unsigned long)size, stage);
>> +
>> +       for (i = 0; i < (size / sizeof(*q)); i++)
>> +               q[i] = RANDOM_CONSTANT ^ i;
>> +
>> +       for (i = 0; i < (size / sizeof(*q)); i++) {
>> +               if (q[i] != (RANDOM_CONSTANT ^ i)) {
>> +                       tst_res(TFAIL, "Stage \"%s\": Mismatch at offset
>> 0x%lx: 0x%x "
>> +                                       "instead of 0x%lx", stage, i,
>> q[i], RANDOM_CONSTANT ^ i);
>> +                       return -1;
>> +               }
>> +       }
>> +       return 0;
>> +}
>> +
>> +static int do_remap(void *target)
>> +{
>> +       void *a, *b;
>> +       int ret;
>> +
>> +       a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
>> +                 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
>> +
>> +       ret = do_readback(a, page_size, "base normal");
>> +       if (ret)
>> +               goto cleanup;
>> +       b = mremap(a, page_size, page_size, MREMAP_MAYMOVE | MREMAP_FIXED,
>> +                  target);
>> +
>> +       if (b != MAP_FAILED) {
>> +               do_readback(b, page_size, "remapped");
>> +               a = b;
>> +       } else
>> +               tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED) disallowed");
>> +
>> +cleanup:
>> +       SAFE_MUNMAP(a, page_size);
>> +       return ret;
>> +}
>>
>
> Those two functions do_readback() and do_remap() are
> copy&past from hugemmap2[4|5].c, what about extracting
> them into a common header file(tst_hugetlb.h or mem.h) for
> easy reusing?
>
ok, I think, hugetlb.h/.c will be better place to keep them.

> And I also noticed other tests (hugemmap27) using the same
> macro mem barrier(), it'd be great to define it in a header file.
>
ok.
>
> -- 
> Regards,
> Li Wang
>
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

* Re: [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork
  2022-12-25 15:42 ` [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork Tarun Sahu
@ 2022-12-28  3:15   ` Li Wang
  2023-01-03 18:23   ` Petr Vorel
  1 sibling, 0 replies; 36+ messages in thread
From: Li Wang @ 2022-12-28  3:15 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sun, Dec 25, 2022 at 11:43 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Migrating the libhugetlbfs/testcases/shm-fork.c test
>
> Test Description: Test shared memory behavior when multiple threads are
> Test shared memory behavior when multiple threads are attached
> to a segment.  A segment is created and then children are
> spawned which attach, write, read (verify), and detach from the
> shared memory segment.
>
> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |   1 +
>  testcases/kernel/mem/.gitignore               |   1 +
>  .../kernel/mem/hugetlb/hugefork/hugefork02.c  | 104 ++++++++++++++++++
>  3 files changed, 106 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
>
> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 33fd384b4..348a7dc5f 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -2,6 +2,7 @@ hugefallocate01 hugefallocate01
>  hugefallocate02 hugefallocate02
>
>  hugefork01 hugefork01
> +hugefork02 hugefork02 -P 3 -s 5
>
>  hugemmap01 hugemmap01
>  hugemmap02 hugemmap02
> diff --git a/testcases/kernel/mem/.gitignore
> b/testcases/kernel/mem/.gitignore
> index 8375389cd..0c1311303 100644
> --- a/testcases/kernel/mem/.gitignore
> +++ b/testcases/kernel/mem/.gitignore
> @@ -2,6 +2,7 @@
>  /hugetlb/hugefallocate/hugefallocate01
>  /hugetlb/hugefallocate/hugefallocate02
>  /hugetlb/hugefork/hugefork01
> +/hugetlb/hugefork/hugefork02
>  /hugetlb/hugemmap/hugemmap01
>  /hugetlb/hugemmap/hugemmap02
>  /hugetlb/hugemmap/hugemmap04
> diff --git a/testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
> b/testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
> new file mode 100644
> index 000000000..6596bf81f
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
> @@ -0,0 +1,104 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
> + * Author: David Gibson & Adam Litke
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Test shared memory behavior when multiple threads are attached to a
> + * segment. A segment is created and then children are spawned which
> + * attach, write, read (verify), and detach from the shared memory
> segment.
> + */
> +
> +#define _GNU_SOURCE
> +#include <stdio.h>
> +#include <sys/mount.h>
> +#include <limits.h>
> +#include <sys/param.h>
> +#include <setjmp.h>
> +#include <sys/types.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/shm.h>
> +
> +#include "tst_safe_sysv_ipc.h"
> +#include "hugetlb.h"
> +
> +static int shmid = -1;
> +
> +#define NR_HUGEPAGES 5
>



> +#define NUMPROCS 20
>

Defining a fixed number to fork children might not be wise.

A sanity way is to detect the available NCPUS and create
corresponding children (or twice the amount), this will benefit
small machines (e.g. KVM guest with only single CPU).
For a system with a larger number of CPUs, just choose 20 as
the default will be OK.

LTP has tst_ncpus_available() function for getting available NCPUS.



> +#define MNTPOINT "hugetlbfs/"
>


> +#define MAX_PROCS 200
> +#define BUF_SZ 256
>

I don't see any places using these two macros.


+
> +static long hpage_size;
> +
> +static void do_child(int thread, unsigned long size)
> +{
> +       volatile char *shmaddr;
> +       int j;
> +       unsigned long k;
> +
> +       for (j = 0; j < 5; j++) {
> +               shmaddr = SAFE_SHMAT(shmid, 0, SHM_RND);
> +
> +               for (k = 0; k < size; k++)
> +                       shmaddr[k] = (char) (k);
> +               for (k = 0; k < size; k++)
> +                       if (shmaddr[k] != (char)k) {
> +                               tst_res(TFAIL, "Thread %d, Offset %lu
> mismatch", thread, k);
> +                               goto cleanup;
> +                       }
> +
> +               SAFE_SHMDT((const void *)shmaddr);
> +       }
> +cleanup:
> +       exit(0);
> +}
> +
> +static void run_test(void)
> +{
> +       unsigned long size;
> +       int pid;
> +       int i;
> +
> +       size = hpage_size * NR_HUGEPAGES;
> +       shmid = SAFE_SHMGET(2, size, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
> +
> +       for (i = 0; i < NUMPROCS; i++) {
> +               pid = SAFE_FORK();
> +
> +               if (pid == 0)
> +                       do_child(i, size);
> +       }
> +
> +       tst_reap_children();
> +       tst_res(TPASS, "Successfully tested shared hugetlb memory with
> multiple procs");
> +}
> +
> +static void setup(void)
> +{
> +       hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
> +}
> +
> +static void cleanup(void)
> +{
> +       if (shmid >= 0)
> +               SAFE_SHMCTL(shmid, IPC_RMID, NULL);
> +}
> +
> +static struct tst_test test = {
> +       .needs_root = 1,
> +       .mntpoint = MNTPOINT,
> +       .needs_hugetlbfs = 1,
> +       .needs_tmpdir = 1,
> +       .forks_child = 1,
> +       .setup = setup,
> +       .cleanup = cleanup,
> +       .test_all = run_test,
> +       .hugepages = {NR_HUGEPAGES, TST_NEEDS},
> +};
> --
> 2.31.1
>
>

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  2022-12-27 18:21     ` Tarun Sahu
@ 2022-12-29 19:06       ` Tarun Sahu
  2022-12-30  3:49         ` Li Wang
  0 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-29 19:06 UTC (permalink / raw)
  To: Li Wang; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi,


Tarun Sahu <tsahu@linux.ibm.com> writes:

> Hi Li,
> Thanks for reviewing the patch.
> I will update it in next revision.
>
> --skip
>>> +static int do_readback(void *p, size_t size, const char *stage)
>>> +{
>>> +       unsigned int *q = p;
>>> +       size_t i;
>>> +
>>> +       tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
>>> +              (unsigned long)size, stage);
>>> +
>>> +       for (i = 0; i < (size / sizeof(*q)); i++)
>>> +               q[i] = RANDOM_CONSTANT ^ i;
>>> +
>>> +       for (i = 0; i < (size / sizeof(*q)); i++) {
>>> +               if (q[i] != (RANDOM_CONSTANT ^ i)) {
>>> +                       tst_res(TFAIL, "Stage \"%s\": Mismatch at offset
>>> 0x%lx: 0x%x "
>>> +                                       "instead of 0x%lx", stage, i,
>>> q[i], RANDOM_CONSTANT ^ i);
>>> +                       return -1;
>>> +               }
>>> +       }
>>> +       return 0;
>>> +}
>>> +
>>> +static int do_remap(void *target)
>>> +{
>>> +       void *a, *b;
>>> +       int ret;
>>> +
>>> +       a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
>>> +                 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
>>> +
>>> +       ret = do_readback(a, page_size, "base normal");
>>> +       if (ret)
>>> +               goto cleanup;
>>> +       b = mremap(a, page_size, page_size, MREMAP_MAYMOVE | MREMAP_FIXED,
>>> +                  target);
>>> +
>>> +       if (b != MAP_FAILED) {
>>> +               do_readback(b, page_size, "remapped");
>>> +               a = b;
>>> +       } else
>>> +               tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED) disallowed");
>>> +
>>> +cleanup:
>>> +       SAFE_MUNMAP(a, page_size);
>>> +       return ret;
>>> +}
>>>
>>
>> Those two functions do_readback() and do_remap() are
>> copy&past from hugemmap2[4|5].c, what about extracting
>> them into a common header file(tst_hugetlb.h or mem.h) for
>> easy reusing?
>>
> ok, I think, hugetlb.h/.c will be better place to keep them.
>
These two functions are very specific to tests (specially the do_remap).
Also, they use values that are defined only inside the test.
Though do_readback is more general which can be put in hugetlb.h/c.

WDYT?
>> And I also noticed other tests (hugemmap27) using the same
>> macro mem barrier(), it'd be great to define it in a header file.
>>
> ok.
>>
>> -- 
>> Regards,
>> Li Wang
>>
>> -- 
>> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

* Re: [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  2022-12-29 19:06       ` Tarun Sahu
@ 2022-12-30  3:49         ` Li Wang
  2022-12-31  5:08           ` Tarun Sahu
  0 siblings, 1 reply; 36+ messages in thread
From: Li Wang @ 2022-12-30  3:49 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Fri, Dec 30, 2022 at 3:06 AM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Hi,
>
>
> Tarun Sahu <tsahu@linux.ibm.com> writes:
>
> > Hi Li,
> > Thanks for reviewing the patch.
> > I will update it in next revision.
> >
> > --skip
> >>> +static int do_readback(void *p, size_t size, const char *stage)
> >>> +{
> >>> +       unsigned int *q = p;
> >>> +       size_t i;
> >>> +
> >>> +       tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
> >>> +              (unsigned long)size, stage);
> >>> +
> >>> +       for (i = 0; i < (size / sizeof(*q)); i++)
> >>> +               q[i] = RANDOM_CONSTANT ^ i;
> >>> +
> >>> +       for (i = 0; i < (size / sizeof(*q)); i++) {
> >>> +               if (q[i] != (RANDOM_CONSTANT ^ i)) {
> >>> +                       tst_res(TFAIL, "Stage \"%s\": Mismatch at
> offset
> >>> 0x%lx: 0x%x "
> >>> +                                       "instead of 0x%lx", stage, i,
> >>> q[i], RANDOM_CONSTANT ^ i);
> >>> +                       return -1;
> >>> +               }
> >>> +       }
> >>> +       return 0;
> >>> +}
> >>> +
> >>> +static int do_remap(void *target)
> >>> +{
> >>> +       void *a, *b;
> >>> +       int ret;
> >>> +
> >>> +       a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
> >>> +                 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
> >>> +
> >>> +       ret = do_readback(a, page_size, "base normal");
> >>> +       if (ret)
> >>> +               goto cleanup;
> >>> +       b = mremap(a, page_size, page_size, MREMAP_MAYMOVE |
> MREMAP_FIXED,
> >>> +                  target);
> >>> +
> >>> +       if (b != MAP_FAILED) {
> >>> +               do_readback(b, page_size, "remapped");
> >>> +               a = b;
> >>> +       } else
> >>> +               tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED)
> disallowed");
> >>> +
> >>> +cleanup:
> >>> +       SAFE_MUNMAP(a, page_size);
> >>> +       return ret;
> >>> +}
> >>>
> >>
> >> Those two functions do_readback() and do_remap() are
> >> copy&past from hugemmap2[4|5].c, what about extracting
> >> them into a common header file(tst_hugetlb.h or mem.h) for
> >> easy reusing?
> >>
> > ok, I think, hugetlb.h/.c will be better place to keep them.
> >
> These two functions are very specific to tests (specially the do_remap).
> Also, they use values that are defined only inside the test.
> Though do_readback is more general which can be put in hugetlb.h/c.
>
> WDYT?
>

Ah yes, I didn't realize the difference.
Or can we pass the specific arguments based on the variable part?
e.g.

    static int do_remap(int fd, size_t page_size; int prot, void *target);


-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  2022-12-30  3:49         ` Li Wang
@ 2022-12-31  5:08           ` Tarun Sahu
  2023-01-03  3:06             ` Li Wang
  0 siblings, 1 reply; 36+ messages in thread
From: Tarun Sahu @ 2022-12-31  5:08 UTC (permalink / raw)
  To: Li Wang; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi,

Li Wang <liwang@redhat.com> writes:

> On Fri, Dec 30, 2022 at 3:06 AM Tarun Sahu <tsahu@linux.ibm.com> wrote:
>
>> Hi,
>>
>>
>> Tarun Sahu <tsahu@linux.ibm.com> writes:
>>
>> > Hi Li,
>> > Thanks for reviewing the patch.
>> > I will update it in next revision.
>> >
>> > --skip
>> >>> +static int do_readback(void *p, size_t size, const char *stage)
>> >>> +{
>> >>> +       unsigned int *q = p;
>> >>> +       size_t i;
>> >>> +
>> >>> +       tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
>> >>> +              (unsigned long)size, stage);
>> >>> +
>> >>> +       for (i = 0; i < (size / sizeof(*q)); i++)
>> >>> +               q[i] = RANDOM_CONSTANT ^ i;
>> >>> +
>> >>> +       for (i = 0; i < (size / sizeof(*q)); i++) {
>> >>> +               if (q[i] != (RANDOM_CONSTANT ^ i)) {
>> >>> +                       tst_res(TFAIL, "Stage \"%s\": Mismatch at
>> offset
>> >>> 0x%lx: 0x%x "
>> >>> +                                       "instead of 0x%lx", stage, i,
>> >>> q[i], RANDOM_CONSTANT ^ i);
>> >>> +                       return -1;
>> >>> +               }
>> >>> +       }
>> >>> +       return 0;
>> >>> +}
>> >>> +
>> >>> +static int do_remap(void *target)
>> >>> +{
>> >>> +       void *a, *b;
>> >>> +       int ret;
>> >>> +
>> >>> +       a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
>> >>> +                 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
>> >>> +
>> >>> +       ret = do_readback(a, page_size, "base normal");
>> >>> +       if (ret)
>> >>> +               goto cleanup;
>> >>> +       b = mremap(a, page_size, page_size, MREMAP_MAYMOVE |
>> MREMAP_FIXED,
>> >>> +                  target);
>> >>> +
>> >>> +       if (b != MAP_FAILED) {
>> >>> +               do_readback(b, page_size, "remapped");
>> >>> +               a = b;
>> >>> +       } else
>> >>> +               tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED)
>> disallowed");
>> >>> +
>> >>> +cleanup:
>> >>> +       SAFE_MUNMAP(a, page_size);
>> >>> +       return ret;
>> >>> +}
>> >>>
>> >>
>> >> Those two functions do_readback() and do_remap() are
>> >> copy&past from hugemmap2[4|5].c, what about extracting
>> >> them into a common header file(tst_hugetlb.h or mem.h) for
>> >> easy reusing?
>> >>
>> > ok, I think, hugetlb.h/.c will be better place to keep them.
>> >
>> These two functions are very specific to tests (specially the do_remap).
>> Also, they use values that are defined only inside the test.
>> Though do_readback is more general which can be put in hugetlb.h/c.
>>
>> WDYT?
>>
>
> Ah yes, I didn't realize the difference.
> Or can we pass the specific arguments based on the variable part?
> e.g.
>
>     static int do_remap(int fd, size_t page_size; int prot, void *target);
>
This can be done this way, but What concerned me is mremap function
itself. it is defined in sys/mmap under _GNU_SOURCE. I am not sure
whether it will be ok to use _GNU_SOURCE specific function in
lib/hugetlb.c/h file.

please confirm if it is ok to define _GNU_SOURCE in hugetlb.c/h.
>
> -- 
> Regards,
> Li Wang
>
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

* Re: [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge
  2022-12-31  5:08           ` Tarun Sahu
@ 2023-01-03  3:06             ` Li Wang
  0 siblings, 0 replies; 36+ messages in thread
From: Li Wang @ 2023-01-03  3:06 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

On Sat, Dec 31, 2022 at 1:08 PM Tarun Sahu <tsahu@linux.ibm.com> wrote:

> Hi,
>
> Li Wang <liwang@redhat.com> writes:
>
> > On Fri, Dec 30, 2022 at 3:06 AM Tarun Sahu <tsahu@linux.ibm.com> wrote:
> >
> >> Hi,
> >>
> >>
> >> Tarun Sahu <tsahu@linux.ibm.com> writes:
> >>
> >> > Hi Li,
> >> > Thanks for reviewing the patch.
> >> > I will update it in next revision.
> >> >
> >> > --skip
> >> >>> +static int do_readback(void *p, size_t size, const char *stage)
> >> >>> +{
> >> >>> +       unsigned int *q = p;
> >> >>> +       size_t i;
> >> >>> +
> >> >>> +       tst_res(TINFO, "%s(%p, 0x%lx, \"%s\")", __func__, p,
> >> >>> +              (unsigned long)size, stage);
> >> >>> +
> >> >>> +       for (i = 0; i < (size / sizeof(*q)); i++)
> >> >>> +               q[i] = RANDOM_CONSTANT ^ i;
> >> >>> +
> >> >>> +       for (i = 0; i < (size / sizeof(*q)); i++) {
> >> >>> +               if (q[i] != (RANDOM_CONSTANT ^ i)) {
> >> >>> +                       tst_res(TFAIL, "Stage \"%s\": Mismatch at
> >> offset
> >> >>> 0x%lx: 0x%x "
> >> >>> +                                       "instead of 0x%lx", stage,
> i,
> >> >>> q[i], RANDOM_CONSTANT ^ i);
> >> >>> +                       return -1;
> >> >>> +               }
> >> >>> +       }
> >> >>> +       return 0;
> >> >>> +}
> >> >>> +
> >> >>> +static int do_remap(void *target)
> >> >>> +{
> >> >>> +       void *a, *b;
> >> >>> +       int ret;
> >> >>> +
> >> >>> +       a = SAFE_MMAP(NULL, page_size, PROT_READ|PROT_WRITE,
> >> >>> +                 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
> >> >>> +
> >> >>> +       ret = do_readback(a, page_size, "base normal");
> >> >>> +       if (ret)
> >> >>> +               goto cleanup;
> >> >>> +       b = mremap(a, page_size, page_size, MREMAP_MAYMOVE |
> >> MREMAP_FIXED,
> >> >>> +                  target);
> >> >>> +
> >> >>> +       if (b != MAP_FAILED) {
> >> >>> +               do_readback(b, page_size, "remapped");
> >> >>> +               a = b;
> >> >>> +       } else
> >> >>> +               tst_res(TINFO|TERRNO, "mremap(MAYMOVE|FIXED)
> >> disallowed");
> >> >>> +
> >> >>> +cleanup:
> >> >>> +       SAFE_MUNMAP(a, page_size);
> >> >>> +       return ret;
> >> >>> +}
> >> >>>
> >> >>
> >> >> Those two functions do_readback() and do_remap() are
> >> >> copy&past from hugemmap2[4|5].c, what about extracting
> >> >> them into a common header file(tst_hugetlb.h or mem.h) for
> >> >> easy reusing?
> >> >>
> >> > ok, I think, hugetlb.h/.c will be better place to keep them.
> >> >
> >> These two functions are very specific to tests (specially the do_remap).
> >> Also, they use values that are defined only inside the test.
> >> Though do_readback is more general which can be put in hugetlb.h/c.
> >>
> >> WDYT?
> >>
> >
> > Ah yes, I didn't realize the difference.
> > Or can we pass the specific arguments based on the variable part?
> > e.g.
> >
> >     static int do_remap(int fd, size_t page_size; int prot, void
> *target);
> >
> This can be done this way, but What concerned me is mremap function
> itself. it is defined in sys/mmap under _GNU_SOURCE. I am not sure
> whether it will be ok to use _GNU_SOURCE specific function in
> lib/hugetlb.c/h file.
>

Good catch.

>
> please confirm if it is ok to define _GNU_SOURCE in hugetlb.c/h.
>

Well, I guess we should avoid doing that because it will bring
more uncertain factors to other tests that include this header file.

You could make an informed trade-off about review comments
according to actual conditions.

-- 
Regards,
Li Wang

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

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

* Re: [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork
  2022-12-25 15:42 ` [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork Tarun Sahu
  2022-12-28  3:15   ` Li Wang
@ 2023-01-03 18:23   ` Petr Vorel
  2023-01-04 17:59     ` Tarun Sahu
  1 sibling, 1 reply; 36+ messages in thread
From: Petr Vorel @ 2023-01-03 18:23 UTC (permalink / raw)
  To: Tarun Sahu; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Hi,

> Migrating the libhugetlbfs/testcases/shm-fork.c test
NOTE: if you're talking about libhugetlbfs [1], the test path is
tests/shm-fork.c (not testcases).


> Test Description: Test shared memory behavior when multiple threads are
> Test shared memory behavior when multiple threads are attached
> to a segment.  A segment is created and then children are
> spawned which attach, write, read (verify), and detach from the
> shared memory segment.

> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
> ---
>  runtest/hugetlb                               |   1 +
>  testcases/kernel/mem/.gitignore               |   1 +
>  .../kernel/mem/hugetlb/hugefork/hugefork02.c  | 104 ++++++++++++++++++
>  3 files changed, 106 insertions(+)
>  create mode 100644 testcases/kernel/mem/hugetlb/hugefork/hugefork02.c

> diff --git a/runtest/hugetlb b/runtest/hugetlb
> index 33fd384b4..348a7dc5f 100644
> --- a/runtest/hugetlb
> +++ b/runtest/hugetlb
> @@ -2,6 +2,7 @@ hugefallocate01 hugefallocate01
>  hugefallocate02 hugefallocate02

>  hugefork01 hugefork01
> +hugefork02 hugefork02 -P 3 -s 5

You're not define tst_option [2] (you don't even have any code which would hold
these options), therefore passing any of these 2 options will quit the test:
hugefork02: invalid option -- 'P'

Please fix it in next version.

Kind regards,
Petr

[1] https://github.com/libhugetlbfs/libhugetlbfs
[2] https://github.com/linux-test-project/ltp/wiki/C-Test-API#15-test-specific-command-line-options

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

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

* Re: [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork
  2023-01-03 18:23   ` Petr Vorel
@ 2023-01-04 17:59     ` Tarun Sahu
  0 siblings, 0 replies; 36+ messages in thread
From: Tarun Sahu @ 2023-01-04 17:59 UTC (permalink / raw)
  To: Petr Vorel; +Cc: sbhat, aneesh.kumar, geetika, vaibhav, rpalethorpe, ltp

Petr Vorel <pvorel@suse.cz> writes:

Hi Petr,

Thanks for reviewing it.

> Hi,
>
>> Migrating the libhugetlbfs/testcases/shm-fork.c test
> NOTE: if you're talking about libhugetlbfs [1], the test path is
> tests/shm-fork.c (not testcases).
Ohh Right. Thanks for pointing this out.
I checked, I did the same in all the past testcases, will take
care of it.

>
>
>> Test Description: Test shared memory behavior when multiple threads are
>> Test shared memory behavior when multiple threads are attached
>> to a segment.  A segment is created and then children are
>> spawned which attach, write, read (verify), and detach from the
>> shared memory segment.


>> Signed-off-by: Tarun Sahu <tsahu@linux.ibm.com>
>> ---
>>  runtest/hugetlb                               |   1 +
>>  testcases/kernel/mem/.gitignore               |   1 +
>>  .../kernel/mem/hugetlb/hugefork/hugefork02.c  | 104 ++++++++++++++++++
>>  3 files changed, 106 insertions(+)
>>  create mode 100644 testcases/kernel/mem/hugetlb/hugefork/hugefork02.c
>
>> diff --git a/runtest/hugetlb b/runtest/hugetlb
>> index 33fd384b4..348a7dc5f 100644
>> --- a/runtest/hugetlb
>> +++ b/runtest/hugetlb
>> @@ -2,6 +2,7 @@ hugefallocate01 hugefallocate01
>>  hugefallocate02 hugefallocate02
>
>>  hugefork01 hugefork01
>> +hugefork02 hugefork02 -P 3 -s 5
>
> You're not define tst_option [2] (you don't even have any code which would hold
> these options), therefore passing any of these 2 options will quit the test:
> hugefork02: invalid option -- 'P'
>
> Please fix it in next version.
>
Fixed it in next revision.
please check here: https://lore.kernel.org/all/20230104122224.369467-1-tsahu@linux.ibm.com/
> Kind regards,
> Petr
>
> [1] https://github.com/libhugetlbfs/libhugetlbfs
> [2] https://github.com/linux-test-project/ltp/wiki/C-Test-API#15-test-specific-command-line-options
>
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

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

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

end of thread, other threads:[~2023-01-04 17:59 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-25 15:42 [LTP] [PATCH 0/13][PART 4] Hugetlb:Migrating the libhugetlbfs tests Tarun Sahu
2022-12-25 15:42 ` [LTP] [PATCH 01/13] Hugetlb: Migrating libhugetlbfs mlock Tarun Sahu
2022-12-26  4:48   ` Li Wang
2022-12-26  9:39     ` Tarun Sahu
2022-12-26  9:48       ` Li Wang
2022-12-25 15:42 ` [LTP] [PATCH 02/13] Hugetlb: Migrating libhugetlbfs mmap-cow Tarun Sahu
2022-12-26  6:57   ` Li Wang
2022-12-26  7:35     ` Tarun Sahu
2022-12-25 15:42 ` [LTP] [PATCH 03/13] Hugetlb: Migrating libhugetlbfs mmap-gettest Tarun Sahu
2022-12-26  7:33   ` Li Wang
2022-12-26  9:41     ` Tarun Sahu
2022-12-25 15:42 ` [LTP] [PATCH 04/13] Hugetlb: Migrating libhugetlbfs mprotect Tarun Sahu
2022-12-27  5:48   ` Li Wang
2022-12-27 16:00     ` Tarun Sahu
2022-12-25 15:42 ` [LTP] [PATCH 05/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-huge-near-normal Tarun Sahu
2022-12-25 15:42 ` [LTP] [PATCH 06/13] Hugetlb: Migrating libhugetlbfs mremap-fixed-normal-near-huge Tarun Sahu
2022-12-27  6:31   ` Li Wang
2022-12-27 18:21     ` Tarun Sahu
2022-12-29 19:06       ` Tarun Sahu
2022-12-30  3:49         ` Li Wang
2022-12-31  5:08           ` Tarun Sahu
2023-01-03  3:06             ` Li Wang
2022-12-25 15:42 ` [LTP] [PATCH 07/13] Hugetlb: Migrating libhugetlbfs noresv-reserve-resv-page Tarun Sahu
2022-12-25 15:42 ` [LTP] [PATCH 08/13] Hugetlb: Migrating libhugetlbfs noresv-regarded-as-resv Tarun Sahu
2022-12-27  6:47   ` Li Wang
2022-12-25 15:42 ` [LTP] [PATCH 09/13] Hugetlb: Migrating libhugetlbfs private Tarun Sahu
2022-12-27  6:56   ` Li Wang
2022-12-25 15:42 ` [LTP] [PATCH 10/13] Hugetlb: Migrating libhugetlbfs readahead_reserve Tarun Sahu
2022-12-27  7:05   ` Li Wang
2022-12-25 15:42 ` [LTP] [PATCH 11/13] Hugetlb: Migrating libhugetlbfs shared Tarun Sahu
2022-12-27  7:08   ` Li Wang
2022-12-25 15:42 ` [LTP] [PATCH 12/13] Hugetlb: Migrating libhugetlbfs shm-fork Tarun Sahu
2022-12-28  3:15   ` Li Wang
2023-01-03 18:23   ` Petr Vorel
2023-01-04 17:59     ` Tarun Sahu
2022-12-25 15:42 ` [LTP] [PATCH 13/13] Hugetlb: Migrating libhugetlbfs mremap-expand-slice-collision Tarun Sahu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.