[LTP] [PATCH v4 3/4] syscalls/madvise12: new test for madvise(MADV_REMOVE)

Zhao Gongyi zhaogongyi@huawei.com
Thu Oct 13 15:47:27 CEST 2022


Check that after a successful madvise(2) MADV_REMOVE operation, subsequent
accesses in the specified address range will see bytes containing zero.

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

diff --git a/runtest/syscalls b/runtest/syscalls
index 296af9f9d..0697b31ab 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -949,6 +949,7 @@ madvise08 madvise08
 madvise09 madvise09
 madvise10 madvise10
 madvise11 madvise11
+madvise12 madvise12

 newuname01 newuname01

diff --git a/testcases/kernel/syscalls/madvise/.gitignore b/testcases/kernel/syscalls/madvise/.gitignore
index ffd8823d1..dc82c82bd 100644
--- a/testcases/kernel/syscalls/madvise/.gitignore
+++ b/testcases/kernel/syscalls/madvise/.gitignore
@@ -9,3 +9,4 @@
 /madvise09
 /madvise10
 /madvise11
+/madvise12
diff --git a/testcases/kernel/syscalls/madvise/madvise12.c b/testcases/kernel/syscalls/madvise/madvise12.c
new file mode 100644
index 000000000..7c22e464d
--- /dev/null
+++ b/testcases/kernel/syscalls/madvise/madvise12.c
@@ -0,0 +1,85 @@
+// 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]
+ *
+ * Check that after a successful madvise(2) MADV_REMOVE operation, subsequent
+ * accesses in the specified address range will see bytes containing zero.
+ */
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include "tst_test.h"
+
+#define MAP_SIZE (8 * 1024)
+#define FNAME "madvise_remove"
+#define MOUNT_POINT "mntpoint"
+
+static char *addr;
+static int fd;
+
+static void run(void)
+{
+	TEST(madvise(addr, MAP_SIZE, MADV_REMOVE));
+	if (TST_RET == -1) {
+		if (TST_ERR == EOPNOTSUPP)
+			tst_brk(TCONF, "'MADV_REMOVE' not supported?");
+		else {
+			tst_res(TFAIL | TTERRNO, "madvise(%p, %d, 0x%x) failed",
+				addr, MAP_SIZE, MADV_REMOVE);
+			return;
+		}
+	}
+
+	for (int i = 0; i < MAP_SIZE; i++) {
+		if (addr[0]) {
+			tst_res(TFAIL,
+				"The content of mapping memory is not removed");
+			return;
+		}
+	}
+
+	tst_res(TPASS, "The content of mapping memory is removed");
+}
+
+static void setup(void)
+{
+	fd = SAFE_OPEN(FNAME, O_CREAT | O_RDWR, 0777);
+	TEST(fallocate(fd, 0, 0, MAP_SIZE));
+	if (TST_RET) {
+		if (TST_ERR == ENOSYS || TST_ERR == EOPNOTSUPP)
+			tst_brk(TCONF, "fallocate not support");
+		else
+			tst_brk(TBROK | TERRNO, "fallocate failed");
+	}
+
+	addr = SAFE_MMAP(NULL, MAP_SIZE,
+			PROT_READ | PROT_WRITE,
+			MAP_SHARED,
+			fd, 0);
+	memset(addr, 1, MAP_SIZE);
+}
+
+static void cleanup(void)
+{
+	SAFE_CLOSE(fd);
+	SAFE_UNLINK(FNAME);
+	if (addr)
+		SAFE_MUNMAP(addr, MAP_SIZE);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.needs_root = 1,
+	.min_kver = "2.6.16",
+	.setup = setup,
+	.cleanup = cleanup,
+	.all_filesystems = 1,
+	.mount_device = 1,
+	.mntpoint = MOUNT_POINT,
+};
+
--
2.17.1



More information about the ltp mailing list