Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | // SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE #include <stdlib.h> #include <stdio.h> #include <string.h> #include <linux/magic.h> #include <sys/mman.h> #include <sys/statfs.h> #include <errno.h> #include <stdbool.h> #include "../kselftest.h" #define PREFIX " ... " #define ERROR_PREFIX " !!! " #define MAX_WRITE_READ_CHUNK_SIZE (getpagesize() * 16) #define MAX(a, b) (((a) > (b)) ? (a) : (b)) enum test_status { TEST_PASSED = 0, TEST_FAILED = 1, TEST_SKIPPED = 2, }; static char *status_to_str(enum test_status status) { switch (status) { case TEST_PASSED: return "TEST_PASSED"; case TEST_FAILED: return "TEST_FAILED"; case TEST_SKIPPED: return "TEST_SKIPPED"; default: return "TEST_???"; } } static int setup_filemap(char *filemap, size_t len, size_t wr_chunk_size) { char iter = 0; for (size_t offset = 0; offset < len; offset += wr_chunk_size) { iter++; memset(filemap + offset, iter, wr_chunk_size); } return 0; } static bool verify_chunk(char *buf, size_t len, char val) { size_t i; for (i = 0; i < len; ++i) { if (buf[i] != val) { printf(PREFIX ERROR_PREFIX "check fail: buf[%lu] = %u != %u\n", i, buf[i], val); return false; } } return true; } static bool seek_read_hugepage_filemap(int fd, size_t len, size_t wr_chunk_size, off_t offset, size_t expected) { char buf[MAX_WRITE_READ_CHUNK_SIZE]; ssize_t ret_count = 0; ssize_t total_ret_count = 0; char val = offset / wr_chunk_size + offset % wr_chunk_size; printf(PREFIX PREFIX "init val=%u with offset=0x%lx\n", val, offset); printf(PREFIX PREFIX "expect to read 0x%lx bytes of data in total\n", expected); if (lseek(fd, offset, SEEK_SET) < 0) { perror(PREFIX ERROR_PREFIX "seek failed"); return false; } while (offset + total_ret_count < len) { ret_count = read(fd, buf, wr_chunk_size); if (ret_count == 0) { printf(PREFIX PREFIX "read reach end of the file\n"); break; } else if (ret_count < 0) { perror(PREFIX ERROR_PREFIX "read failed"); break; } ++val; if (!verify_chunk(buf, ret_count, val)) return false; total_ret_count += ret_count; } printf(PREFIX PREFIX "actually read 0x%lx bytes of data in total\n", total_ret_count); return total_ret_count == expected; } static bool read_hugepage_filemap(int fd, size_t len, size_t wr_chunk_size, size_t expected) { char buf[MAX_WRITE_READ_CHUNK_SIZE]; ssize_t ret_count = 0; ssize_t total_ret_count = 0; char val = 0; printf(PREFIX PREFIX "expect to read 0x%lx bytes of data in total\n", expected); while (total_ret_count < len) { ret_count = read(fd, buf, wr_chunk_size); if (ret_count == 0) { printf(PREFIX PREFIX "read reach end of the file\n"); break; } else if (ret_count < 0) { perror(PREFIX ERROR_PREFIX "read failed"); break; } ++val; if (!verify_chunk(buf, ret_count, val)) return false; total_ret_count += ret_count; } printf(PREFIX PREFIX "actually read 0x%lx bytes of data in total\n", total_ret_count); return total_ret_count == expected; } static enum test_status test_hugetlb_read(int fd, size_t len, size_t wr_chunk_size) { enum test_status status = TEST_SKIPPED; char *filemap = NULL; if (ftruncate(fd, len) < 0) { perror(PREFIX ERROR_PREFIX "ftruncate failed"); return status; } filemap = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, 0); if (filemap == MAP_FAILED) { perror(PREFIX ERROR_PREFIX "mmap for primary mapping failed"); goto done; } setup_filemap(filemap, len, wr_chunk_size); status = TEST_FAILED; if (read_hugepage_filemap(fd, len, wr_chunk_size, len)) status = TEST_PASSED; munmap(filemap, len); done: if (ftruncate(fd, 0) < 0) { perror(PREFIX ERROR_PREFIX "ftruncate back to 0 failed"); status = TEST_FAILED; } return status; } static enum test_status test_hugetlb_read_hwpoison(int fd, size_t len, size_t wr_chunk_size, bool skip_hwpoison_page) { enum test_status status = TEST_SKIPPED; char *filemap = NULL; char *hwp_addr = NULL; const unsigned long pagesize = getpagesize(); if (ftruncate(fd, len) < 0) { perror(PREFIX ERROR_PREFIX "ftruncate failed"); return status; } filemap = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, 0); if (filemap == MAP_FAILED) { perror(PREFIX ERROR_PREFIX "mmap for primary mapping failed"); goto done; } setup_filemap(filemap, len, wr_chunk_size); status = TEST_FAILED; /* * Poisoned hugetlb page layout (assume hugepagesize=2MB): * |<---------------------- 1MB ---------------------->| * |<---- healthy page ---->|<---- HWPOISON page ----->| * |<------------------- (1MB - 8KB) ----------------->| */ hwp_addr = filemap + len / 2 + pagesize; if (madvise(hwp_addr, pagesize, MADV_HWPOISON) < 0) { perror(PREFIX ERROR_PREFIX "MADV_HWPOISON failed"); goto unmap; } if (!skip_hwpoison_page) { /* * Userspace should be able to read (1MB + 1 page) from * the beginning of the HWPOISONed hugepage. */ if (read_hugepage_filemap(fd, len, wr_chunk_size, len / 2 + pagesize)) status = TEST_PASSED; } else { /* * Userspace should be able to read (1MB - 2 pages) from * HWPOISONed hugepage. */ if (seek_read_hugepage_filemap(fd, len, wr_chunk_size, len / 2 + MAX(2 * pagesize, wr_chunk_size), len / 2 - MAX(2 * pagesize, wr_chunk_size))) status = TEST_PASSED; } unmap: munmap(filemap, len); done: if (ftruncate(fd, 0) < 0) { perror(PREFIX ERROR_PREFIX "ftruncate back to 0 failed"); status = TEST_FAILED; } return status; } static int create_hugetlbfs_file(struct statfs *file_stat) { int fd; fd = memfd_create("hugetlb_tmp", MFD_HUGETLB); if (fd < 0) { perror(PREFIX ERROR_PREFIX "could not open hugetlbfs file"); return -1; } memset(file_stat, 0, sizeof(*file_stat)); if (fstatfs(fd, file_stat)) { perror(PREFIX ERROR_PREFIX "fstatfs failed"); goto close; } if (file_stat->f_type != HUGETLBFS_MAGIC) { printf(PREFIX ERROR_PREFIX "not hugetlbfs file\n"); goto close; } return fd; close: close(fd); return -1; } int main(void) { int fd; struct statfs file_stat; enum test_status status; /* Test read() in different granularity. */ size_t wr_chunk_sizes[] = { getpagesize() / 2, getpagesize(), getpagesize() * 2, getpagesize() * 4 }; size_t i; for (i = 0; i < ARRAY_SIZE(wr_chunk_sizes); ++i) { printf("Write/read chunk size=0x%lx\n", wr_chunk_sizes[i]); fd = create_hugetlbfs_file(&file_stat); if (fd < 0) goto create_failure; printf(PREFIX "HugeTLB read regression test...\n"); status = test_hugetlb_read(fd, file_stat.f_bsize, wr_chunk_sizes[i]); printf(PREFIX "HugeTLB read regression test...%s\n", status_to_str(status)); close(fd); if (status == TEST_FAILED) return -1; fd = create_hugetlbfs_file(&file_stat); if (fd < 0) goto create_failure; printf(PREFIX "HugeTLB read HWPOISON test...\n"); status = test_hugetlb_read_hwpoison(fd, file_stat.f_bsize, wr_chunk_sizes[i], false); printf(PREFIX "HugeTLB read HWPOISON test...%s\n", status_to_str(status)); close(fd); if (status == TEST_FAILED) return -1; fd = create_hugetlbfs_file(&file_stat); if (fd < 0) goto create_failure; printf(PREFIX "HugeTLB seek then read HWPOISON test...\n"); status = test_hugetlb_read_hwpoison(fd, file_stat.f_bsize, wr_chunk_sizes[i], true); printf(PREFIX "HugeTLB seek then read HWPOISON test...%s\n", status_to_str(status)); close(fd); if (status == TEST_FAILED) return -1; } return 0; create_failure: printf(ERROR_PREFIX "Abort test: failed to create hugetlbfs file\n"); return -1; } |