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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
// SPDX-License-Identifier: GPL-2.0-only
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdint.h>
#include <fcntl.h>
#include <assert.h>
#include <linux/mman.h>
#include <sys/mman.h>
#include "../kselftest.h"
#include "thp_settings.h"
#include "uffd-common.h"
static int pagemap_fd;
static size_t pagesize;
static int nr_pagesizes = 1;
static int nr_thpsizes;
static size_t thpsizes[20];
static int nr_hugetlbsizes;
static size_t hugetlbsizes[10];
static int sz2ord(size_t size)
{
return __builtin_ctzll(size / pagesize);
}
static int detect_thp_sizes(size_t sizes[], int max)
{
int count = 0;
unsigned long orders;
size_t kb;
int i;
/* thp not supported at all. */
if (!read_pmd_pagesize())
return 0;
orders = thp_supported_orders();
for (i = 0; orders && count < max; i++) {
if (!(orders & (1UL << i)))
continue;
orders &= ~(1UL << i);
kb = (pagesize >> 10) << i;
sizes[count++] = kb * 1024;
ksft_print_msg("[INFO] detected THP size: %zu KiB\n", kb);
}
return count;
}
static void *mmap_aligned(size_t size, int prot, int flags)
{
size_t mmap_size = size * 2;
char *mmap_mem, *mem;
mmap_mem = mmap(NULL, mmap_size, prot, flags, -1, 0);
if (mmap_mem == MAP_FAILED)
return mmap_mem;
mem = (char *)(((uintptr_t)mmap_mem + size - 1) & ~(size - 1));
munmap(mmap_mem, mem - mmap_mem);
munmap(mem + size, mmap_mem + mmap_size - mem - size);
return mem;
}
static void *alloc_one_folio(size_t size, bool private, bool hugetlb)
{
bool thp = !hugetlb && size > pagesize;
int flags = MAP_ANONYMOUS;
int prot = PROT_READ | PROT_WRITE;
char *mem, *addr;
assert((size & (size - 1)) == 0);
if (private)
flags |= MAP_PRIVATE;
else
flags |= MAP_SHARED;
/*
* For THP, we must explicitly enable the THP size, allocate twice the
* required space then manually align.
*/
if (thp) {
struct thp_settings settings = *thp_current_settings();
if (private)
settings.hugepages[sz2ord(size)].enabled = THP_ALWAYS;
else
settings.shmem_hugepages[sz2ord(size)].enabled = SHMEM_ALWAYS;
thp_push_settings(&settings);
mem = mmap_aligned(size, prot, flags);
} else {
if (hugetlb) {
flags |= MAP_HUGETLB;
flags |= __builtin_ctzll(size) << MAP_HUGE_SHIFT;
}
mem = mmap(NULL, size, prot, flags, -1, 0);
}
if (mem == MAP_FAILED) {
mem = NULL;
goto out;
}
assert(((uintptr_t)mem & (size - 1)) == 0);
/*
* Populate the folio by writing the first byte and check that all pages
* are populated. Finally set the whole thing to non-zero data to avoid
* kernel from mapping it back to the zero page.
*/
mem[0] = 1;
for (addr = mem; addr < mem + size; addr += pagesize) {
if (!pagemap_is_populated(pagemap_fd, addr)) {
munmap(mem, size);
mem = NULL;
goto out;
}
}
memset(mem, 1, size);
out:
if (thp)
thp_pop_settings();
return mem;
}
static bool check_uffd_wp_state(void *mem, size_t size, bool expect)
{
uint64_t pte;
void *addr;
for (addr = mem; addr < mem + size; addr += pagesize) {
pte = pagemap_get_entry(pagemap_fd, addr);
if (!!(pte & PM_UFFD_WP) != expect) {
ksft_test_result_fail("uffd-wp not %s for pte %lu!\n",
expect ? "set" : "clear",
(addr - mem) / pagesize);
return false;
}
}
return true;
}
static bool range_is_swapped(void *addr, size_t size)
{
for (; size; addr += pagesize, size -= pagesize)
if (!pagemap_is_swapped(pagemap_fd, addr))
return false;
return true;
}
static void test_one_folio(size_t size, bool private, bool swapout, bool hugetlb)
{
struct uffdio_writeprotect wp_prms;
uint64_t features = 0;
void *addr = NULL;
void *mem = NULL;
assert(!(hugetlb && swapout));
ksft_print_msg("[RUN] %s(size=%zu, private=%s, swapout=%s, hugetlb=%s)\n",
__func__,
size,
private ? "true" : "false",
swapout ? "true" : "false",
hugetlb ? "true" : "false");
/* Allocate a folio of required size and type. */
mem = alloc_one_folio(size, private, hugetlb);
if (!mem) {
ksft_test_result_fail("alloc_one_folio() failed\n");
goto out;
}
/* Register range for uffd-wp. */
if (userfaultfd_open(&features)) {
if (errno == ENOENT)
ksft_test_result_skip("userfaultfd not available\n");
else
ksft_test_result_fail("userfaultfd_open() failed\n");
goto out;
}
if (uffd_register(uffd, mem, size, false, true, false)) {
ksft_test_result_fail("uffd_register() failed\n");
goto out;
}
wp_prms.mode = UFFDIO_WRITEPROTECT_MODE_WP;
wp_prms.range.start = (uintptr_t)mem;
wp_prms.range.len = size;
if (ioctl(uffd, UFFDIO_WRITEPROTECT, &wp_prms)) {
ksft_test_result_fail("ioctl(UFFDIO_WRITEPROTECT) failed\n");
goto out;
}
if (swapout) {
madvise(mem, size, MADV_PAGEOUT);
if (!range_is_swapped(mem, size)) {
ksft_test_result_skip("MADV_PAGEOUT did not work, is swap enabled?\n");
goto out;
}
}
/* Check that uffd-wp is set for all PTEs in range. */
if (!check_uffd_wp_state(mem, size, true))
goto out;
/*
* Move the mapping to a new, aligned location. Since
* UFFD_FEATURE_EVENT_REMAP is not set, we expect the uffd-wp bit for
* each PTE to be cleared in the new mapping.
*/
addr = mmap_aligned(size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS);
if (addr == MAP_FAILED) {
ksft_test_result_fail("mmap_aligned() failed\n");
goto out;
}
if (mremap(mem, size, size, MREMAP_FIXED | MREMAP_MAYMOVE, addr) == MAP_FAILED) {
ksft_test_result_fail("mremap() failed\n");
munmap(addr, size);
goto out;
}
mem = addr;
/* Check that uffd-wp is cleared for all PTEs in range. */
if (!check_uffd_wp_state(mem, size, false))
goto out;
ksft_test_result_pass("%s(size=%zu, private=%s, swapout=%s, hugetlb=%s)\n",
__func__,
size,
private ? "true" : "false",
swapout ? "true" : "false",
hugetlb ? "true" : "false");
out:
if (mem)
munmap(mem, size);
if (uffd >= 0) {
close(uffd);
uffd = -1;
}
}
struct testcase {
size_t *sizes;
int *nr_sizes;
bool private;
bool swapout;
bool hugetlb;
};
static const struct testcase testcases[] = {
/* base pages. */
{
.sizes = &pagesize,
.nr_sizes = &nr_pagesizes,
.private = false,
.swapout = false,
.hugetlb = false,
},
{
.sizes = &pagesize,
.nr_sizes = &nr_pagesizes,
.private = true,
.swapout = false,
.hugetlb = false,
},
{
.sizes = &pagesize,
.nr_sizes = &nr_pagesizes,
.private = false,
.swapout = true,
.hugetlb = false,
},
{
.sizes = &pagesize,
.nr_sizes = &nr_pagesizes,
.private = true,
.swapout = true,
.hugetlb = false,
},
/* thp. */
{
.sizes = thpsizes,
.nr_sizes = &nr_thpsizes,
.private = false,
.swapout = false,
.hugetlb = false,
},
{
.sizes = thpsizes,
.nr_sizes = &nr_thpsizes,
.private = true,
.swapout = false,
.hugetlb = false,
},
{
.sizes = thpsizes,
.nr_sizes = &nr_thpsizes,
.private = false,
.swapout = true,
.hugetlb = false,
},
{
.sizes = thpsizes,
.nr_sizes = &nr_thpsizes,
.private = true,
.swapout = true,
.hugetlb = false,
},
/* hugetlb. */
{
.sizes = hugetlbsizes,
.nr_sizes = &nr_hugetlbsizes,
.private = false,
.swapout = false,
.hugetlb = true,
},
{
.sizes = hugetlbsizes,
.nr_sizes = &nr_hugetlbsizes,
.private = true,
.swapout = false,
.hugetlb = true,
},
};
int main(int argc, char **argv)
{
struct thp_settings settings;
int i, j, plan = 0;
pagesize = getpagesize();
nr_thpsizes = detect_thp_sizes(thpsizes, ARRAY_SIZE(thpsizes));
nr_hugetlbsizes = detect_hugetlb_page_sizes(hugetlbsizes,
ARRAY_SIZE(hugetlbsizes));
/* If THP is supported, save THP settings and initially disable THP. */
if (nr_thpsizes) {
thp_save_settings();
thp_read_settings(&settings);
for (i = 0; i < NR_ORDERS; i++) {
settings.hugepages[i].enabled = THP_NEVER;
settings.shmem_hugepages[i].enabled = SHMEM_NEVER;
}
thp_push_settings(&settings);
}
for (i = 0; i < ARRAY_SIZE(testcases); i++)
plan += *testcases[i].nr_sizes;
ksft_set_plan(plan);
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
if (pagemap_fd < 0)
ksft_exit_fail_msg("opening pagemap failed\n");
for (i = 0; i < ARRAY_SIZE(testcases); i++) {
const struct testcase *tc = &testcases[i];
for (j = 0; j < *tc->nr_sizes; j++)
test_one_folio(tc->sizes[j], tc->private, tc->swapout,
tc->hugetlb);
}
/* If THP is supported, restore original THP settings. */
if (nr_thpsizes)
thp_restore_settings();
i = ksft_get_fail_cnt();
if (i)
ksft_exit_fail_msg("%d out of %d tests failed\n",
i, ksft_test_num());
ksft_exit_pass();
}
|