xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/hugetlb/hugefallocate/hugefallocate01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 alignment of fallocate arguments. fallocate will take non-huge
11  * page aligned offsets and addresses.  However, operations are only
12  * performed on huge pages.  This is different that than fallocate
13  * behavior in "normal" filesystems.
14  */
15 
16 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <sys/mount.h>
19 #include <limits.h>
20 #include <sys/param.h>
21 #include <sys/types.h>
22 
23 #include "hugetlb.h"
24 #include "lapi/fallocate.h"
25 
26 #define MNTPOINT "hugetlbfs/"
27 
28 static int  fd = -1;
29 static long hpage_size;
30 
run_test(void)31 static void run_test(void)
32 {
33 	int err;
34 	unsigned long free_initial, free_after, free_after_delete;
35 
36 	fd = tst_creat_unlinked(MNTPOINT, 0);
37 
38 	free_initial = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
39 
40 	/*
41 	 * First preallocate file with just 1 byte.  Allocation sizes
42 	 * are rounded up, so we should get an entire huge page.
43 	 */
44 	err = fallocate(fd, 0, 0, 1);
45 	if (err) {
46 		if (errno == EOPNOTSUPP)
47 			tst_brk(TCONF, "Operation Not Supported");
48 		tst_res(TFAIL|TERRNO, "fallocate()");
49 		goto cleanup;
50 	}
51 
52 	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
53 	if (free_initial - free_after != 1) {
54 		tst_res(TFAIL, "fallocate 1 byte did not preallocate entire huge page");
55 		goto cleanup;
56 	}
57 
58 	/*
59 	 * Now punch a hole with just 1 byte.  On hole punch, sizes are
60 	 * rounded down. So, this operation should not create a hole.
61 	 */
62 	err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
63 			0, 1);
64 	if (err) {
65 		tst_res(TFAIL|TERRNO, "fallocate(FALLOC_FL_PUNCH_HOLE)");
66 		goto cleanup;
67 	}
68 
69 	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
70 	if (free_after == free_initial) {
71 		tst_res(TFAIL, "fallocate hole punch 1 byte free'ed a huge page");
72 		goto cleanup;
73 	}
74 
75 	/*
76 	 * Now punch a hole with of 2 * hpage_size - 1 byte.  This size
77 	 * should be rounded down to a single huge page and the hole created.
78 	 */
79 	err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
80 			0, (2 * hpage_size) - 1);
81 	if (err) {
82 		tst_res(TFAIL|TERRNO, "fallocate(FALLOC_FL_PUNCH_HOLE)");
83 		goto cleanup;
84 	}
85 
86 	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
87 	if (free_after != free_initial) {
88 		tst_res(TFAIL, "fallocate hole punch 2 * hpage_size - 1 byte did not"
89 				" free huge page");
90 		goto cleanup;
91 	}
92 
93 	/*
94 	 * Perform a preallocate operation with offset 1 and size of
95 	 * hpage_size.  The offset should be rounded down and the
96 	 * size rounded up to preallocate two huge pages.
97 	 */
98 	err = fallocate(fd, 0, 1, hpage_size);
99 	if (err) {
100 		tst_res(TFAIL, "fallocate()");
101 		goto cleanup;
102 	}
103 
104 	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
105 	if (free_initial - free_after != 2) {
106 		tst_res(TFAIL, "fallocate 1 byte offset, huge page size did not"
107 				" preallocate two huge pages");
108 		goto cleanup;
109 	}
110 
111 	/*
112 	 * The hole punch code will only delete 'whole' huge pags that are
113 	 * in the specified range.  The offset is rounded up, and (offset
114 	 * + size) is rounded down to determine the huge pages to be deleted.
115 	 * In this case, after rounding the range is (hpage_size, hpage_size).
116 	 * So, no pages should be deleted.
117 	 */
118 	err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
119 			1, hpage_size);
120 	if (err) {
121 		tst_res(TFAIL|TERRNO, "fallocate(FALLOC_FL_PUNCH_HOLE)");
122 		goto cleanup;
123 	}
124 
125 	free_after = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
126 	if (free_initial - free_after != 2) {
127 		tst_res(TFAIL, "fallocate hole punch 1 byte offset, huge page size"
128 				" incorrectly deleted a huge page");
129 		goto cleanup;
130 	}
131 
132 	/*
133 	 * To delete both huge pages, the range passed to hole punch must
134 	 * overlap the allocated pages
135 	 */
136 	err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
137 			0, 2 * hpage_size);
138 	if (err) {
139 		tst_res(TFAIL|TERRNO, "fallocate(FALLOC_FL_PUNCH_HOLE)");
140 		goto cleanup;
141 	}
142 
143 	free_after_delete = SAFE_READ_MEMINFO(MEMINFO_HPAGE_FREE);
144 	TST_EXP_EQ_LU(free_after_delete, free_initial);
145 cleanup:
146 	SAFE_CLOSE(fd);
147 }
148 
setup(void)149 static void setup(void)
150 {
151 	hpage_size = SAFE_READ_MEMINFO(MEMINFO_HPAGE_SIZE)*1024;
152 }
153 
cleanup(void)154 static void cleanup(void)
155 {
156 	if (fd > 0)
157 		SAFE_CLOSE(fd);
158 }
159 
160 static struct tst_test test = {
161 	.needs_root = 1,
162 	.mntpoint = MNTPOINT,
163 	.needs_hugetlbfs = 1,
164 	.needs_tmpdir = 1,
165 	.setup = setup,
166 	.cleanup = cleanup,
167 	.test_all = run_test,
168 	.hugepages = {2, TST_NEEDS},
169 };
170