Debian Codegraph @

tests/quota.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2007 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include <sys/types.h>
20 #include <sys/shm.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <sys/mman.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/mount.h>
32 #include <fcntl.h>
33 #include <hugetlbfs.h>
34 #include <sys/vfs.h>
35 #include "hugetests.h"
36
37 /*
38 * Test Rationale:
39 *
40 * The number of global huge pages available to a mounted hugetlbfs filesystem
41 * can be limited using a fs quota mechanism by setting the size attribute at
42 * mount time. Older kernels did not properly handle quota accounting in a
43 * number of cases (eg. for MAP_PRIVATE pages, and wrt MAP_SHARED reservation.
44 *
45 * This test replays some scenarios on a privately mounted filesystem to check
46 * for regressions in hugetlbfs quota accounting.
47 */
48
49 extern int errno;
50
51 #define BUF_SZ 1024
52
53 /* Global test configuration */
54 static long hpage_size;
55 char *mountpoint = NULL;
56
57 /* map action flags */
58 #define ACTION_COW 0x0001
59 #define ACTION_TOUCH 0x0002
60
61 /* Testlet results */
62 #define GOOD 0
63 #define BAD_SIG 1
64 #define BAD_EXIT 2
65
66 char result_str[3][10] = { "pass", "killed", "fail" };
67
68 void cleanup(void)
69 {
70 if (mountpoint && (umount(mountpoint) == 0))
71 rmdir(mountpoint);
72 }
73
74 /*
75 * Debugging function: Verify the counters in the hugetlbfs superblock that
76 * are used to implement the filesystem quotas.
77 */
78 void _verify_stat(int line, long tot, long free, long avail)
79 {
80 struct statfs s;
81 statfs(mountpoint, &s);
82
83 if (s.f_blocks != tot || s.f_bfree != free || s.f_bavail != avail)
84 FAIL("Bad quota counters at line %i: total: %li free: %li "
85 "avail: %li\n", line, s.f_blocks, s.f_bfree, s.f_bavail);
86 }
87 #define verify_stat(t, f, a) _verify_stat(__LINE__, t, f, a)
88
89 void get_quota_fs(unsigned long size, char *prog)
90 {
91 char mount_str[17];
92 char mount_opts[50];
93 int nr_written;
94
95 nr_written = snprintf(mount_opts, 20, "size=%luK", size/1024);
96
97 /*
98 * If the mount point now in use does not use the system default
99 * huge page size, specify the desired size when mounting. When
100 * the sizes do match, we avoid specifying the pagesize= option to
101 * preserve backwards compatibility with kernels that do not
102 * recognize that option.
103 */
104 if (!using_system_hpage_size(hugetlbfs_find_path()))
105 snprintf(mount_opts + nr_written, 29, ",pagesize=%lu",
106 hpage_size);
107
108 sprintf(mount_str, "/tmp/huge-XXXXXX");
109 if (!mkdtemp(mount_str))
110 FAIL("Cannot create directory for mountpoint: %s",
111 strerror(errno));
112
113 if (mount("none", mount_str, "hugetlbfs", 0, mount_opts)) {
114 perror("mount");
115 FAIL();
116 }
117 mountpoint = mount_str;
118
119 /*
120 * Set HUGETLB_PATH and then exec the test again. This will cause
121 * libhugetlbfs to use this newly created mountpoint.
122 */
123 if (setenv("HUGETLB_PATH", mount_str, 1))
124 FAIL("Cannot set HUGETLB_PATH environment variable: %s",
125 strerror(errno));
126 verbose_printf("Using %s as temporary mount point.\n", mount_str);
127
128 execlp(prog, prog, "-p", mount_str, NULL);
129 FAIL("execle failed: %s", strerror(errno));
130 }
131
132 void map(unsigned long size, int mmap_flags, int action_flags)
133 {
134 int fd;
135 char *a, *b, *c;
136
137 fd = hugetlbfs_unlinked_fd();
138 if (!fd) {
139 verbose_printf("hugetlbfs_unlinked_fd () failed\n");
140 exit(1);
141 }
142
143 a = mmap(0, size, PROT_READ|PROT_WRITE, mmap_flags, fd, 0);
144 if (a == MAP_FAILED) {
145 verbose_printf("mmap failed: %s\n", strerror(errno));
146 exit(1);
147 }
148
149
150 if (action_flags & ACTION_TOUCH)
151 for (b = a; b < a + size; b += hpage_size)
152 *(b) = 1;
153
154 if (action_flags & ACTION_COW) {
155 c = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
156 if (c == MAP_FAILED) {
157 verbose_printf("Creating COW mapping failed: %s\n", strerror(errno));
158 exit(1);
159 }
160 if ((*c) != 1) {
161 verbose_printf("Data mismatch when setting up COW");
162 exit(1);
163 }
164 (*c) = 0;
165 munmap(c, size);
166 }
167
168 munmap(a, size);
169 close(fd);
170 }
171
172 void do_unexpected_result(int line, int expected, int actual)
173 {
174 FAIL("Unexpected result on line %i: expected %s, actual %s",
175 line, result_str[expected], result_str[actual]);
176 }
177
178 void _spawn(int l, int expected_result, unsigned long size, int mmap_flags,
179 int action_flags)
180 {
181 pid_t pid;
182 int status;
183 int actual_result;
184
185 pid = fork();
186 if (pid == 0) {
187 map(size, mmap_flags, action_flags);
188 exit(0);
189 } else if (pid < 0) {
190 FAIL("fork(): %s", strerror(errno));
191 } else {
192 waitpid(pid, &status, 0);
193 if (WIFEXITED(status)) {
194 if (WEXITSTATUS(status) == 0)
195 actual_result = GOOD;
196 else
197 actual_result = BAD_EXIT;
198 } else {
199 actual_result = BAD_SIG;
200 }
201
202 if (actual_result != expected_result)
203 do_unexpected_result(l, expected_result, actual_result);
204 }
205 }
206 #define spawn(e,s,mf,af) _spawn(__LINE__, e, s, mf, af)
207
208 int main(int argc, char ** argv)
209 {
210 int private_resv;
211 int bad_priv_resv;
212
213 test_init(argc, argv);
214 hpage_size = check_hugepagesize();
215
216 if ((argc == 3) && !strcmp(argv[1], "-p"))
217 mountpoint = argv[2];
218 else
219 get_quota_fs(hpage_size, argv[0]);
220
221 check_must_be_root();
222 check_free_huge_pages(1);
223
224 private_resv = kernel_has_private_reservations();
225 if (private_resv == -1)
226 FAIL("kernel_has_private_reservations() failed\n");
227 bad_priv_resv = private_resv ? BAD_EXIT : BAD_SIG;
228
229 /*
230 * Check that unused quota is cleared when untouched mmaps are
231 * cleaned up.
232 */
233 spawn(GOOD, hpage_size, MAP_PRIVATE, 0);
234 verify_stat(1, 1, 1);
235 spawn(GOOD, hpage_size, MAP_SHARED, 0);
236 verify_stat(1, 1, 1);
237
238 /*
239 * Check that simple page instantiation works within quota limits
240 * for private and shared mappings.
241 */
242 spawn(GOOD, hpage_size, MAP_PRIVATE, ACTION_TOUCH);
243 spawn(GOOD, hpage_size, MAP_SHARED, ACTION_TOUCH);
244
245 /*
246 * Page instantiation should be refused if doing so puts the fs
247 * over quota.
248 */
249 spawn(BAD_EXIT, 2 * hpage_size, MAP_SHARED, ACTION_TOUCH);
250
251 /*
252 * If private mappings are reserved, the quota is checked up front
253 * (as is the case for shared mappings).
254 */
255 spawn(bad_priv_resv, 2 * hpage_size, MAP_PRIVATE, ACTION_TOUCH);
256
257 /*
258 * COW should not be allowed if doing so puts the fs over quota.
259 */
260 spawn(bad_priv_resv, hpage_size, MAP_SHARED, ACTION_TOUCH|ACTION_COW);
261 spawn(bad_priv_resv, hpage_size, MAP_PRIVATE, ACTION_TOUCH|ACTION_COW);
262
263 /*
264 * Make sure that operations within the quota will succeed after
265 * some failures.
266 */
267 spawn(GOOD, hpage_size, MAP_SHARED, ACTION_TOUCH);
268 spawn(GOOD, hpage_size, MAP_PRIVATE, ACTION_TOUCH);
269
270 PASS();
271 }