Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * Tests for mremap w/ MREMAP_DONTUNMAP.
0005  *
0006  * Copyright 2020, Brian Geffon <bgeffon@google.com>
0007  */
0008 #define _GNU_SOURCE
0009 #include <sys/mman.h>
0010 #include <errno.h>
0011 #include <stdio.h>
0012 #include <stdlib.h>
0013 #include <string.h>
0014 #include <unistd.h>
0015 
0016 #include "../kselftest.h"
0017 
0018 #ifndef MREMAP_DONTUNMAP
0019 #define MREMAP_DONTUNMAP 4
0020 #endif
0021 
0022 unsigned long page_size;
0023 char *page_buffer;
0024 
0025 static void dump_maps(void)
0026 {
0027     char cmd[32];
0028 
0029     snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps", getpid());
0030     system(cmd);
0031 }
0032 
0033 #define BUG_ON(condition, description)                        \
0034     do {                                      \
0035         if (condition) {                          \
0036             fprintf(stderr, "[FAIL]\t%s():%d\t%s:%s\n", __func__, \
0037                 __LINE__, (description), strerror(errno));    \
0038             dump_maps();                      \
0039             exit(1);                          \
0040         }                                 \
0041     } while (0)
0042 
0043 // Try a simple operation for to "test" for kernel support this prevents
0044 // reporting tests as failed when it's run on an older kernel.
0045 static int kernel_support_for_mremap_dontunmap()
0046 {
0047     int ret = 0;
0048     unsigned long num_pages = 1;
0049     void *source_mapping = mmap(NULL, num_pages * page_size, PROT_NONE,
0050                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0051     BUG_ON(source_mapping == MAP_FAILED, "mmap");
0052 
0053     // This simple remap should only fail if MREMAP_DONTUNMAP isn't
0054     // supported.
0055     void *dest_mapping =
0056         mremap(source_mapping, num_pages * page_size, num_pages * page_size,
0057            MREMAP_DONTUNMAP | MREMAP_MAYMOVE, 0);
0058     if (dest_mapping == MAP_FAILED) {
0059         ret = errno;
0060     } else {
0061         BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
0062                "unable to unmap destination mapping");
0063     }
0064 
0065     BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
0066            "unable to unmap source mapping");
0067     return ret;
0068 }
0069 
0070 // This helper will just validate that an entire mapping contains the expected
0071 // byte.
0072 static int check_region_contains_byte(void *addr, unsigned long size, char byte)
0073 {
0074     BUG_ON(size & (page_size - 1),
0075            "check_region_contains_byte expects page multiples");
0076     BUG_ON((unsigned long)addr & (page_size - 1),
0077            "check_region_contains_byte expects page alignment");
0078 
0079     memset(page_buffer, byte, page_size);
0080 
0081     unsigned long num_pages = size / page_size;
0082     unsigned long i;
0083 
0084     // Compare each page checking that it contains our expected byte.
0085     for (i = 0; i < num_pages; ++i) {
0086         int ret =
0087             memcmp(addr + (i * page_size), page_buffer, page_size);
0088         if (ret) {
0089             return ret;
0090         }
0091     }
0092 
0093     return 0;
0094 }
0095 
0096 // this test validates that MREMAP_DONTUNMAP moves the pagetables while leaving
0097 // the source mapping mapped.
0098 static void mremap_dontunmap_simple()
0099 {
0100     unsigned long num_pages = 5;
0101 
0102     void *source_mapping =
0103         mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
0104          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0105     BUG_ON(source_mapping == MAP_FAILED, "mmap");
0106 
0107     memset(source_mapping, 'a', num_pages * page_size);
0108 
0109     // Try to just move the whole mapping anywhere (not fixed).
0110     void *dest_mapping =
0111         mremap(source_mapping, num_pages * page_size, num_pages * page_size,
0112            MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
0113     BUG_ON(dest_mapping == MAP_FAILED, "mremap");
0114 
0115     // Validate that the pages have been moved, we know they were moved if
0116     // the dest_mapping contains a's.
0117     BUG_ON(check_region_contains_byte
0118            (dest_mapping, num_pages * page_size, 'a') != 0,
0119            "pages did not migrate");
0120     BUG_ON(check_region_contains_byte
0121            (source_mapping, num_pages * page_size, 0) != 0,
0122            "source should have no ptes");
0123 
0124     BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
0125            "unable to unmap destination mapping");
0126     BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
0127            "unable to unmap source mapping");
0128 }
0129 
0130 // This test validates that MREMAP_DONTUNMAP on a shared mapping works as expected.
0131 static void mremap_dontunmap_simple_shmem()
0132 {
0133     unsigned long num_pages = 5;
0134 
0135     int mem_fd = memfd_create("memfd", MFD_CLOEXEC);
0136     BUG_ON(mem_fd < 0, "memfd_create");
0137 
0138     BUG_ON(ftruncate(mem_fd, num_pages * page_size) < 0,
0139             "ftruncate");
0140 
0141     void *source_mapping =
0142         mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
0143          MAP_FILE | MAP_SHARED, mem_fd, 0);
0144     BUG_ON(source_mapping == MAP_FAILED, "mmap");
0145 
0146     BUG_ON(close(mem_fd) < 0, "close");
0147 
0148     memset(source_mapping, 'a', num_pages * page_size);
0149 
0150     // Try to just move the whole mapping anywhere (not fixed).
0151     void *dest_mapping =
0152         mremap(source_mapping, num_pages * page_size, num_pages * page_size,
0153            MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
0154     if (dest_mapping == MAP_FAILED && errno == EINVAL) {
0155         // Old kernel which doesn't support MREMAP_DONTUNMAP on shmem.
0156         BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
0157             "unable to unmap source mapping");
0158         return;
0159     }
0160 
0161     BUG_ON(dest_mapping == MAP_FAILED, "mremap");
0162 
0163     // Validate that the pages have been moved, we know they were moved if
0164     // the dest_mapping contains a's.
0165     BUG_ON(check_region_contains_byte
0166            (dest_mapping, num_pages * page_size, 'a') != 0,
0167            "pages did not migrate");
0168 
0169     // Because the region is backed by shmem, we will actually see the same
0170     // memory at the source location still.
0171     BUG_ON(check_region_contains_byte
0172            (source_mapping, num_pages * page_size, 'a') != 0,
0173            "source should have no ptes");
0174 
0175     BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
0176            "unable to unmap destination mapping");
0177     BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
0178            "unable to unmap source mapping");
0179 }
0180 
0181 // This test validates MREMAP_DONTUNMAP will move page tables to a specific
0182 // destination using MREMAP_FIXED, also while validating that the source
0183 // remains intact.
0184 static void mremap_dontunmap_simple_fixed()
0185 {
0186     unsigned long num_pages = 5;
0187 
0188     // Since we want to guarantee that we can remap to a point, we will
0189     // create a mapping up front.
0190     void *dest_mapping =
0191         mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
0192          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0193     BUG_ON(dest_mapping == MAP_FAILED, "mmap");
0194     memset(dest_mapping, 'X', num_pages * page_size);
0195 
0196     void *source_mapping =
0197         mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
0198          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0199     BUG_ON(source_mapping == MAP_FAILED, "mmap");
0200     memset(source_mapping, 'a', num_pages * page_size);
0201 
0202     void *remapped_mapping =
0203         mremap(source_mapping, num_pages * page_size, num_pages * page_size,
0204            MREMAP_FIXED | MREMAP_DONTUNMAP | MREMAP_MAYMOVE,
0205            dest_mapping);
0206     BUG_ON(remapped_mapping == MAP_FAILED, "mremap");
0207     BUG_ON(remapped_mapping != dest_mapping,
0208            "mremap should have placed the remapped mapping at dest_mapping");
0209 
0210     // The dest mapping will have been unmap by mremap so we expect the Xs
0211     // to be gone and replaced with a's.
0212     BUG_ON(check_region_contains_byte
0213            (dest_mapping, num_pages * page_size, 'a') != 0,
0214            "pages did not migrate");
0215 
0216     // And the source mapping will have had its ptes dropped.
0217     BUG_ON(check_region_contains_byte
0218            (source_mapping, num_pages * page_size, 0) != 0,
0219            "source should have no ptes");
0220 
0221     BUG_ON(munmap(dest_mapping, num_pages * page_size) == -1,
0222            "unable to unmap destination mapping");
0223     BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
0224            "unable to unmap source mapping");
0225 }
0226 
0227 // This test validates that we can MREMAP_DONTUNMAP for a portion of an
0228 // existing mapping.
0229 static void mremap_dontunmap_partial_mapping()
0230 {
0231     /*
0232      *  source mapping:
0233      *  --------------
0234      *  | aaaaaaaaaa |
0235      *  --------------
0236      *  to become:
0237      *  --------------
0238      *  | aaaaa00000 |
0239      *  --------------
0240      *  With the destination mapping containing 5 pages of As.
0241      *  ---------
0242      *  | aaaaa |
0243      *  ---------
0244      */
0245     unsigned long num_pages = 10;
0246     void *source_mapping =
0247         mmap(NULL, num_pages * page_size, PROT_READ | PROT_WRITE,
0248          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0249     BUG_ON(source_mapping == MAP_FAILED, "mmap");
0250     memset(source_mapping, 'a', num_pages * page_size);
0251 
0252     // We will grab the last 5 pages of the source and move them.
0253     void *dest_mapping =
0254         mremap(source_mapping + (5 * page_size), 5 * page_size,
0255            5 * page_size,
0256            MREMAP_DONTUNMAP | MREMAP_MAYMOVE, NULL);
0257     BUG_ON(dest_mapping == MAP_FAILED, "mremap");
0258 
0259     // We expect the first 5 pages of the source to contain a's and the
0260     // final 5 pages to contain zeros.
0261     BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 'a') !=
0262            0, "first 5 pages of source should have original pages");
0263     BUG_ON(check_region_contains_byte
0264            (source_mapping + (5 * page_size), 5 * page_size, 0) != 0,
0265            "final 5 pages of source should have no ptes");
0266 
0267     // Finally we expect the destination to have 5 pages worth of a's.
0268     BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') !=
0269            0, "dest mapping should contain ptes from the source");
0270 
0271     BUG_ON(munmap(dest_mapping, 5 * page_size) == -1,
0272            "unable to unmap destination mapping");
0273     BUG_ON(munmap(source_mapping, num_pages * page_size) == -1,
0274            "unable to unmap source mapping");
0275 }
0276 
0277 // This test validates that we can remap over only a portion of a mapping.
0278 static void mremap_dontunmap_partial_mapping_overwrite(void)
0279 {
0280     /*
0281      *  source mapping:
0282      *  ---------
0283      *  |aaaaa|
0284      *  ---------
0285      *  dest mapping initially:
0286      *  -----------
0287      *  |XXXXXXXXXX|
0288      *  ------------
0289      *  Source to become:
0290      *  ---------
0291      *  |00000|
0292      *  ---------
0293      *  With the destination mapping containing 5 pages of As.
0294      *  ------------
0295      *  |aaaaaXXXXX|
0296      *  ------------
0297      */
0298     void *source_mapping =
0299         mmap(NULL, 5 * page_size, PROT_READ | PROT_WRITE,
0300          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0301     BUG_ON(source_mapping == MAP_FAILED, "mmap");
0302     memset(source_mapping, 'a', 5 * page_size);
0303 
0304     void *dest_mapping =
0305         mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE,
0306          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0307     BUG_ON(dest_mapping == MAP_FAILED, "mmap");
0308     memset(dest_mapping, 'X', 10 * page_size);
0309 
0310     // We will grab the last 5 pages of the source and move them.
0311     void *remapped_mapping =
0312         mremap(source_mapping, 5 * page_size,
0313            5 * page_size,
0314            MREMAP_DONTUNMAP | MREMAP_MAYMOVE | MREMAP_FIXED, dest_mapping);
0315     BUG_ON(dest_mapping == MAP_FAILED, "mremap");
0316     BUG_ON(dest_mapping != remapped_mapping, "expected to remap to dest_mapping");
0317 
0318     BUG_ON(check_region_contains_byte(source_mapping, 5 * page_size, 0) !=
0319            0, "first 5 pages of source should have no ptes");
0320 
0321     // Finally we expect the destination to have 5 pages worth of a's.
0322     BUG_ON(check_region_contains_byte(dest_mapping, 5 * page_size, 'a') != 0,
0323             "dest mapping should contain ptes from the source");
0324 
0325     // Finally the last 5 pages shouldn't have been touched.
0326     BUG_ON(check_region_contains_byte(dest_mapping + (5 * page_size),
0327                 5 * page_size, 'X') != 0,
0328             "dest mapping should have retained the last 5 pages");
0329 
0330     BUG_ON(munmap(dest_mapping, 10 * page_size) == -1,
0331            "unable to unmap destination mapping");
0332     BUG_ON(munmap(source_mapping, 5 * page_size) == -1,
0333            "unable to unmap source mapping");
0334 }
0335 
0336 int main(void)
0337 {
0338     page_size = sysconf(_SC_PAGE_SIZE);
0339 
0340     // test for kernel support for MREMAP_DONTUNMAP skipping the test if
0341     // not.
0342     if (kernel_support_for_mremap_dontunmap() != 0) {
0343         printf("No kernel support for MREMAP_DONTUNMAP\n");
0344         return KSFT_SKIP;
0345     }
0346 
0347     // Keep a page sized buffer around for when we need it.
0348     page_buffer =
0349         mmap(NULL, page_size, PROT_READ | PROT_WRITE,
0350          MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0351     BUG_ON(page_buffer == MAP_FAILED, "unable to mmap a page.");
0352 
0353     mremap_dontunmap_simple();
0354     mremap_dontunmap_simple_shmem();
0355     mremap_dontunmap_simple_fixed();
0356     mremap_dontunmap_partial_mapping();
0357     mremap_dontunmap_partial_mapping_overwrite();
0358 
0359     BUG_ON(munmap(page_buffer, page_size) == -1,
0360            "unable to unmap page buffer");
0361 
0362     printf("OK\n");
0363     return 0;
0364 }