[LTP] [PATCH v2 1/3] syscalls/madvise03: new test for madvise(MADV_DONTNEED)

Zhao Gongyi zhaogongyi@huawei.com
Tue Oct 11 14:16:05 CEST 2022


Test cases for madvise(2) system call, verify that after a successful
MADV_DONTNEED operation, it will result in zero-fill-on-demand pages
for anonymous private mappings.

Signed-off-by: Zhao Gongyi <zhaogongyi@huawei.com>
---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/madvise/.gitignore  |  1 +
 testcases/kernel/syscalls/madvise/madvise03.c | 69 +++++++++++++++++++
 3 files changed, 71 insertions(+)
 create mode 100644 testcases/kernel/syscalls/madvise/madvise03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 51de0a614..c81764df4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -940,6 +940,7 @@ mincore04 mincore04

 madvise01 madvise01
 madvise02 madvise02
+madvise03 madvise03
 madvise05 madvise05
 madvise06 madvise06
 madvise07 madvise07
diff --git a/testcases/kernel/syscalls/madvise/.gitignore b/testcases/kernel/syscalls/madvise/.gitignore
index 002d8e5d9..f4bfdfefe 100644
--- a/testcases/kernel/syscalls/madvise/.gitignore
+++ b/testcases/kernel/syscalls/madvise/.gitignore
@@ -1,5 +1,6 @@
 /madvise01
 /madvise02
+/madvise03
 /madvise05
 /madvise06
 /madvise07
diff --git a/testcases/kernel/syscalls/madvise/madvise03.c b/testcases/kernel/syscalls/madvise/madvise03.c
new file mode 100644
index 000000000..70330060e
--- /dev/null
+++ b/testcases/kernel/syscalls/madvise/madvise03.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * Author: Zhao Gongyi <zhaogongyi@huawei.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test cases for madvise(2) system call, advise value as "MADV_MADV_DONTNEED":
+ *   After a successful MADV_DONTNEED operation, it will result in
+ *   zero-fill-on-demand pages for anonymous private mappings.
+ */
+
+#include "tst_test.h"
+
+#define MAP_SIZE (8 * 1024)
+
+static char *addr;
+
+static void run(void)
+{
+	int i;
+
+	TEST(madvise(addr, MAP_SIZE, MADV_DONTNEED));
+	if (TST_RET == -1) {
+		tst_brk(TBROK | TTERRNO, "madvise(%p, %d, 0x%x) failed",
+			addr, MAP_SIZE, MADV_DONTNEED);
+	}
+
+	for (i = 0; i < MAP_SIZE; i++) {
+		if (addr[i]) {
+			tst_res(TFAIL,
+				"There are no zero-fill-on-demand pages "
+				"for anonymous private mappings");
+			return;
+		}
+	}
+
+	if (i == MAP_SIZE) {
+		tst_res(TPASS,
+			"There are zero-fill-on-demand pages "
+			"for anonymous private mappings");
+	}
+}
+
+static void setup(void)
+{
+	addr = SAFE_MMAP(NULL, MAP_SIZE,
+			PROT_READ | PROT_WRITE,
+			MAP_PRIVATE | MAP_ANONYMOUS,
+			-1, 0);
+	memset(addr, 1, MAP_SIZE);
+}
+
+static void cleanup(void)
+{
+	if (addr)
+		SAFE_MUNMAP(addr, MAP_SIZE);
+}
+
+
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
+
--
2.17.1



More information about the ltp mailing list