| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
extern int errno; |
| 50 |
|
| 51 |
#define BUF_SZ 1024 |
| 52 |
|
| 53 |
|
| 54 |
static long hpage_size; |
| 55 |
char *mountpoint = NULL; |
| 56 |
|
| 57 |
|
| 58 |
#define ACTION_COW 0x0001 |
| 59 |
#define ACTION_TOUCH 0x0002 |
| 60 |
|
| 61 |
|
| 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 |
|
| 76 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 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 |
|
| 121 |
|
| 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 |
|
| 231 |
|
| 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 |
|
| 240 |
|
| 241 |
|
| 242 |
spawn(GOOD, hpage_size, MAP_PRIVATE, ACTION_TOUCH); |
| 243 |
spawn(GOOD, hpage_size, MAP_SHARED, ACTION_TOUCH); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
spawn(BAD_EXIT, 2 * hpage_size, MAP_SHARED, ACTION_TOUCH); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
spawn(bad_priv_resv, 2 * hpage_size, MAP_PRIVATE, ACTION_TOUCH); |
| 256 |
|
| 257 |
|
| 258 |
|
| 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 |
|
| 265 |
|
| 266 |
|
| 267 |
spawn(GOOD, hpage_size, MAP_SHARED, ACTION_TOUCH); |
| 268 |
spawn(GOOD, hpage_size, MAP_PRIVATE, ACTION_TOUCH); |
| 269 |
|
| 270 |
PASS(); |
| 271 |
} |