[LTP] [PATCH v5 7/7] Hugetlb: Migrating libhugetlbfs fallocate_basic

Tarun Sahu
2022-11-20T19:17:23Z

Migrating the libhugetlbfs/testcases/fallocate_basic.c test

Test Description: It tests basic fallocate functionality in hugetlbfs.
Preallocate huge pages to a file in hugetlbfs, and then remove the pages
via hole punch.

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

diff --git a/runtest/hugetlb b/runtest/hugetlb
index ca92dfcff..ec1fc2515 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -1,4 +1,5 @@
hugefallocate01 hugefallocate01
+hugefallocate02 hugefallocate02

hugemmap01 hugemmap01
hugemmap02 hugemmap02
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index cafdb5259..c0906f3d3 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -1,5 +1,6 @@
/cpuset/cpuset01
/hugetlb/hugefallocate/hugefallocate01
+/hugetlb/hugefallocate/hugefallocate02
/hugetlb/hugemmap/hugemmap01
/hugetlb/hugemmap/hugemmap02
/hugetlb/hugemmap/hugemmap04
diff --git a/testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate02.c b/testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate02.c
new file mode 100644
index 000000000..d1e11dcbe
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate02.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright (C) 2015 Oracle Corporation
+ * Author: Mike Kravetz
+ */
+
+/*\
+ * [Description]
+ *
+ * It tests basic fallocate functionality in hugetlbfs. Preallocate huge
+ * pages to a file in hugetlbfs, and then remove the pages via hole punch.
+ *
+ */
+
+#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 MAX_PAGES_TO_USE 5
+#define MNTPOINT "hugetlbfs/"
+
+static int fd = -1;
+static long hpage_size;
+
+static void run_test(void)
+{
+ int err;
+ unsigned long max_iterations;
+ unsigned long free_initial, free_after, free_end;
+
+ free_initial = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ max_iterations = MIN(free_initial, MAX_PAGES_TO_USE);
+
+ fd = tst_creat_unlinked(MNTPOINT, 0);
+
+ /* First preallocate file with max_iterations pages */
+ err = fallocate(fd, 0, 0, hpage_size * max_iterations);
+ if (err) {
+ if (errno == EOPNOTSUPP)
+ tst_brk(TCONF, "fallocate() Operation is not supported");
+ tst_res(TFAIL|TERRNO, "fallocate()");
+ goto cleanup;
+ }
+
+ free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ if (free_initial - free_after != max_iterations) {
+ tst_res(TFAIL, "fallocate did not preallocate %lu huge pages",
+ max_iterations);
+ goto cleanup;
+ }
+
+ /* Now punch a hole of the same size */
+ err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+ 0, hpage_size * max_iterations);
+ if (err) {
+ tst_res(TFAIL|TERRNO, "fallocate(FALLOC_FL_PUNCH_HOLE)");
+ goto cleanup;
+ }
+
+ free_end = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
+ TST_EXP_EQ_LU(free_end, free_initial);
+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,
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = run_test,
+ .hugepages = {3, TST_NEEDS},