1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * Copyright (C) 2015 Oracle Corporation
4 * Author: Mike Kravetz
5 */
6
7 /*\
8 * [Description]
9 *
10 * It tests basic fallocate functionality in hugetlbfs. Preallocate huge
11 * pages to a file in hugetlbfs, and then remove the pages via hole punch.
12 */
13
14 #define _GNU_SOURCE
15 #include <stdio.h>
16 #include <sys/mount.h>
17 #include <limits.h>
18 #include <sys/param.h>
19 #include <sys/types.h>
20
21 #include "hugetlb.h"
22 #include "lapi/fallocate.h"
23
24 #define MAX_PAGES_TO_USE 5
25 #define MNTPOINT "hugetlbfs/"
26
27 static int fd = -1;
28 static long hpage_size;
29
run_test(void)30 static void run_test(void)
31 {
32 int err;
33 unsigned long max_iterations;
34 unsigned long free_initial, free_after, free_end;
35
36 free_initial = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
37 max_iterations = MIN(free_initial, MAX_PAGES_TO_USE);
38
39 fd = tst_creat_unlinked(MNTPOINT, 0);
40
41 /* First preallocate file with max_iterations pages */
42 err = fallocate(fd, 0, 0, hpage_size * max_iterations);
43 if (err) {
44 if (errno == EOPNOTSUPP)
45 tst_brk(TCONF, "fallocate() Operation is not supported");
46 tst_res(TFAIL|TERRNO, "fallocate()");
47 goto cleanup;
48 }
49
50 free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
51 if (free_initial - free_after != max_iterations) {
52 tst_res(TFAIL, "fallocate did not preallocate %lu huge pages",
53 max_iterations);
54 goto cleanup;
55 }
56
57 /* Now punch a hole of the same size */
58 err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
59 0, hpage_size * max_iterations);
60 if (err) {
61 tst_res(TFAIL|TERRNO, "fallocate(FALLOC_FL_PUNCH_HOLE)");
62 goto cleanup;
63 }
64
65 free_end = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
66 TST_EXP_EQ_LU(free_end, free_initial);
67 cleanup:
68 SAFE_CLOSE(fd);
69 }
70
setup(void)71 static void setup(void)
72 {
73 hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
74 }
75
cleanup(void)76 static void cleanup(void)
77 {
78 if (fd > 0)
79 SAFE_CLOSE(fd);
80 }
81
82 static struct tst_test test = {
83 .needs_root = 1,
84 .mntpoint = MNTPOINT,
85 .needs_hugetlbfs = 1,
86 .setup = setup,
87 .cleanup = cleanup,
88 .test_all = run_test,
89 .hugepages = {3, TST_NEEDS},
90 };
91