|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include <sys/mman.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <time.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include "../kselftest.h" |
| 9 | + |
| 10 | +#define KSM_SYSFS_PATH "/sys/kernel/mm/ksm/" |
| 11 | +#define KSM_FP(s) (KSM_SYSFS_PATH s) |
| 12 | +#define KSM_SCAN_LIMIT_SEC_DEFAULT 120 |
| 13 | +#define KSM_PAGE_COUNT_DEFAULT 10l |
| 14 | +#define KSM_PROT_STR_DEFAULT "rw" |
| 15 | + |
| 16 | +struct ksm_sysfs { |
| 17 | + unsigned long max_page_sharing; |
| 18 | + unsigned long merge_across_nodes; |
| 19 | + unsigned long pages_to_scan; |
| 20 | + unsigned long run; |
| 21 | + unsigned long sleep_millisecs; |
| 22 | + unsigned long stable_node_chains_prune_millisecs; |
| 23 | + unsigned long use_zero_pages; |
| 24 | +}; |
| 25 | + |
| 26 | +static int ksm_write_sysfs(const char *file_path, unsigned long val) |
| 27 | +{ |
| 28 | + FILE *f = fopen(file_path, "w"); |
| 29 | + |
| 30 | + if (!f) { |
| 31 | + fprintf(stderr, "f %s\n", file_path); |
| 32 | + perror("fopen"); |
| 33 | + return 1; |
| 34 | + } |
| 35 | + if (fprintf(f, "%lu", val) < 0) { |
| 36 | + perror("fprintf"); |
| 37 | + return 1; |
| 38 | + } |
| 39 | + fclose(f); |
| 40 | + |
| 41 | + return 0; |
| 42 | +} |
| 43 | + |
| 44 | +static int ksm_read_sysfs(const char *file_path, unsigned long *val) |
| 45 | +{ |
| 46 | + FILE *f = fopen(file_path, "r"); |
| 47 | + |
| 48 | + if (!f) { |
| 49 | + fprintf(stderr, "f %s\n", file_path); |
| 50 | + perror("fopen"); |
| 51 | + return 1; |
| 52 | + } |
| 53 | + if (fscanf(f, "%lu", val) != 1) { |
| 54 | + perror("fscanf"); |
| 55 | + return 1; |
| 56 | + } |
| 57 | + fclose(f); |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +static int str_to_prot(char *prot_str) |
| 63 | +{ |
| 64 | + int prot = 0; |
| 65 | + |
| 66 | + if ((strchr(prot_str, 'r')) != NULL) |
| 67 | + prot |= PROT_READ; |
| 68 | + if ((strchr(prot_str, 'w')) != NULL) |
| 69 | + prot |= PROT_WRITE; |
| 70 | + if ((strchr(prot_str, 'x')) != NULL) |
| 71 | + prot |= PROT_EXEC; |
| 72 | + |
| 73 | + return prot; |
| 74 | +} |
| 75 | + |
| 76 | +static void print_help(void) |
| 77 | +{ |
| 78 | + printf("usage: ksm_tests [-h] [-a prot] [-p page_count] [-l timeout]\n"); |
| 79 | + printf(" -a: specify the access protections of pages.\n" |
| 80 | + " <prot> must be of the form [rwx].\n" |
| 81 | + " Default: %s\n", KSM_PROT_STR_DEFAULT); |
| 82 | + printf(" -p: specify the number of pages to test.\n" |
| 83 | + " Default: %ld\n", KSM_PAGE_COUNT_DEFAULT); |
| 84 | + printf(" -l: limit the maximum running time (in seconds) for a test.\n" |
| 85 | + " Default: %d seconds\n", KSM_SCAN_LIMIT_SEC_DEFAULT); |
| 86 | + |
| 87 | + exit(0); |
| 88 | +} |
| 89 | + |
| 90 | +static void *allocate_memory(void *ptr, int prot, int mapping, char data, size_t map_size) |
| 91 | +{ |
| 92 | + void *map_ptr = mmap(ptr, map_size, PROT_WRITE, mapping, -1, 0); |
| 93 | + |
| 94 | + if (!map_ptr) { |
| 95 | + perror("mmap"); |
| 96 | + return NULL; |
| 97 | + } |
| 98 | + memset(map_ptr, data, map_size); |
| 99 | + if (mprotect(map_ptr, map_size, prot)) { |
| 100 | + perror("mprotect"); |
| 101 | + munmap(map_ptr, map_size); |
| 102 | + return NULL; |
| 103 | + } |
| 104 | + |
| 105 | + return map_ptr; |
| 106 | +} |
| 107 | + |
| 108 | +static int ksm_do_scan(int scan_count, struct timespec start_time, int timeout) |
| 109 | +{ |
| 110 | + struct timespec cur_time; |
| 111 | + unsigned long cur_scan, init_scan; |
| 112 | + |
| 113 | + if (ksm_read_sysfs(KSM_FP("full_scans"), &init_scan)) |
| 114 | + return 1; |
| 115 | + cur_scan = init_scan; |
| 116 | + |
| 117 | + while (cur_scan < init_scan + scan_count) { |
| 118 | + if (ksm_read_sysfs(KSM_FP("full_scans"), &cur_scan)) |
| 119 | + return 1; |
| 120 | + if (clock_gettime(CLOCK_MONOTONIC_RAW, &cur_time)) { |
| 121 | + perror("clock_gettime"); |
| 122 | + return 1; |
| 123 | + } |
| 124 | + if ((cur_time.tv_sec - start_time.tv_sec) > timeout) { |
| 125 | + printf("Scan time limit exceeded\n"); |
| 126 | + return 1; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +static int ksm_merge_pages(void *addr, size_t size, struct timespec start_time, int timeout) |
| 134 | +{ |
| 135 | + if (madvise(addr, size, MADV_MERGEABLE)) { |
| 136 | + perror("madvise"); |
| 137 | + return 1; |
| 138 | + } |
| 139 | + if (ksm_write_sysfs(KSM_FP("run"), 1)) |
| 140 | + return 1; |
| 141 | + |
| 142 | + /* Since merging occurs only after 2 scans, make sure to get at least 2 full scans */ |
| 143 | + if (ksm_do_scan(2, start_time, timeout)) |
| 144 | + return 1; |
| 145 | + |
| 146 | + return 0; |
| 147 | +} |
| 148 | + |
| 149 | +static bool assert_ksm_pages_count(long dupl_page_count) |
| 150 | +{ |
| 151 | + unsigned long max_page_sharing, pages_sharing, pages_shared; |
| 152 | + |
| 153 | + if (ksm_read_sysfs(KSM_FP("pages_shared"), &pages_shared) || |
| 154 | + ksm_read_sysfs(KSM_FP("pages_sharing"), &pages_sharing) || |
| 155 | + ksm_read_sysfs(KSM_FP("max_page_sharing"), &max_page_sharing)) |
| 156 | + return false; |
| 157 | + |
| 158 | + /* |
| 159 | + * Since there must be at least 2 pages for merging and 1 page can be |
| 160 | + * shared with the limited number of pages (max_page_sharing), sometimes |
| 161 | + * there are 'leftover' pages that cannot be merged. For example, if there |
| 162 | + * are 11 pages and max_page_sharing = 10, then only 10 pages will be |
| 163 | + * merged and the 11th page won't be affected. As a result, when the number |
| 164 | + * of duplicate pages is divided by max_page_sharing and the remainder is 1, |
| 165 | + * pages_shared and pages_sharing values will be equal between dupl_page_count |
| 166 | + * and dupl_page_count - 1. |
| 167 | + */ |
| 168 | + if (dupl_page_count % max_page_sharing == 1 || dupl_page_count % max_page_sharing == 0) { |
| 169 | + if (pages_shared == dupl_page_count / max_page_sharing && |
| 170 | + pages_sharing == pages_shared * (max_page_sharing - 1)) |
| 171 | + return true; |
| 172 | + } else { |
| 173 | + if (pages_shared == (dupl_page_count / max_page_sharing + 1) && |
| 174 | + pages_sharing == dupl_page_count - pages_shared) |
| 175 | + return true; |
| 176 | + } |
| 177 | + |
| 178 | + return false; |
| 179 | +} |
| 180 | + |
| 181 | +static int ksm_save_def(struct ksm_sysfs *ksm_sysfs) |
| 182 | +{ |
| 183 | + if (ksm_read_sysfs(KSM_FP("max_page_sharing"), &ksm_sysfs->max_page_sharing) || |
| 184 | + ksm_read_sysfs(KSM_FP("merge_across_nodes"), &ksm_sysfs->merge_across_nodes) || |
| 185 | + ksm_read_sysfs(KSM_FP("sleep_millisecs"), &ksm_sysfs->sleep_millisecs) || |
| 186 | + ksm_read_sysfs(KSM_FP("pages_to_scan"), &ksm_sysfs->pages_to_scan) || |
| 187 | + ksm_read_sysfs(KSM_FP("run"), &ksm_sysfs->run) || |
| 188 | + ksm_read_sysfs(KSM_FP("stable_node_chains_prune_millisecs"), |
| 189 | + &ksm_sysfs->stable_node_chains_prune_millisecs) || |
| 190 | + ksm_read_sysfs(KSM_FP("use_zero_pages"), &ksm_sysfs->use_zero_pages)) |
| 191 | + return 1; |
| 192 | + |
| 193 | + return 0; |
| 194 | +} |
| 195 | + |
| 196 | +static int ksm_restore(struct ksm_sysfs *ksm_sysfs) |
| 197 | +{ |
| 198 | + if (ksm_write_sysfs(KSM_FP("max_page_sharing"), ksm_sysfs->max_page_sharing) || |
| 199 | + ksm_write_sysfs(KSM_FP("merge_across_nodes"), ksm_sysfs->merge_across_nodes) || |
| 200 | + ksm_write_sysfs(KSM_FP("pages_to_scan"), ksm_sysfs->pages_to_scan) || |
| 201 | + ksm_write_sysfs(KSM_FP("run"), ksm_sysfs->run) || |
| 202 | + ksm_write_sysfs(KSM_FP("sleep_millisecs"), ksm_sysfs->sleep_millisecs) || |
| 203 | + ksm_write_sysfs(KSM_FP("stable_node_chains_prune_millisecs"), |
| 204 | + ksm_sysfs->stable_node_chains_prune_millisecs) || |
| 205 | + ksm_write_sysfs(KSM_FP("use_zero_pages"), ksm_sysfs->use_zero_pages)) |
| 206 | + return 1; |
| 207 | + |
| 208 | + return 0; |
| 209 | +} |
| 210 | + |
| 211 | +static int check_ksm_merge(int mapping, int prot, long page_count, int timeout, size_t page_size) |
| 212 | +{ |
| 213 | + void *map_ptr; |
| 214 | + struct timespec start_time; |
| 215 | + |
| 216 | + if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time)) { |
| 217 | + perror("clock_gettime"); |
| 218 | + return KSFT_FAIL; |
| 219 | + } |
| 220 | + |
| 221 | + /* fill pages with the same data and merge them */ |
| 222 | + map_ptr = allocate_memory(NULL, prot, mapping, '*', page_size * page_count); |
| 223 | + if (!map_ptr) |
| 224 | + return KSFT_FAIL; |
| 225 | + |
| 226 | + if (ksm_merge_pages(map_ptr, page_size * page_count, start_time, timeout)) |
| 227 | + goto err_out; |
| 228 | + |
| 229 | + /* verify that the right number of pages are merged */ |
| 230 | + if (assert_ksm_pages_count(page_count)) { |
| 231 | + printf("OK\n"); |
| 232 | + munmap(map_ptr, page_size * page_count); |
| 233 | + return KSFT_PASS; |
| 234 | + } |
| 235 | + |
| 236 | +err_out: |
| 237 | + printf("Not OK\n"); |
| 238 | + munmap(map_ptr, page_size * page_count); |
| 239 | + return KSFT_FAIL; |
| 240 | +} |
| 241 | + |
| 242 | +int main(int argc, char *argv[]) |
| 243 | +{ |
| 244 | + int ret, opt; |
| 245 | + int prot = 0; |
| 246 | + int ksm_scan_limit_sec = KSM_SCAN_LIMIT_SEC_DEFAULT; |
| 247 | + long page_count = KSM_PAGE_COUNT_DEFAULT; |
| 248 | + size_t page_size = sysconf(_SC_PAGESIZE); |
| 249 | + struct ksm_sysfs ksm_sysfs_old; |
| 250 | + |
| 251 | + while ((opt = getopt(argc, argv, "ha:p:l:")) != -1) { |
| 252 | + switch (opt) { |
| 253 | + case 'a': |
| 254 | + prot = str_to_prot(optarg); |
| 255 | + break; |
| 256 | + case 'p': |
| 257 | + page_count = atol(optarg); |
| 258 | + if (page_count <= 0) { |
| 259 | + printf("The number of pages must be greater than 0\n"); |
| 260 | + return KSFT_FAIL; |
| 261 | + } |
| 262 | + break; |
| 263 | + case 'l': |
| 264 | + ksm_scan_limit_sec = atoi(optarg); |
| 265 | + if (ksm_scan_limit_sec <= 0) { |
| 266 | + printf("Timeout value must be greater than 0\n"); |
| 267 | + return KSFT_FAIL; |
| 268 | + } |
| 269 | + break; |
| 270 | + case 'h': |
| 271 | + print_help(); |
| 272 | + break; |
| 273 | + default: |
| 274 | + return KSFT_FAIL; |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + if (prot == 0) |
| 279 | + prot = str_to_prot(KSM_PROT_STR_DEFAULT); |
| 280 | + |
| 281 | + if (access(KSM_SYSFS_PATH, F_OK)) { |
| 282 | + printf("Config KSM not enabled\n"); |
| 283 | + return KSFT_SKIP; |
| 284 | + } |
| 285 | + |
| 286 | + if (ksm_save_def(&ksm_sysfs_old)) { |
| 287 | + printf("Cannot save default tunables\n"); |
| 288 | + return KSFT_FAIL; |
| 289 | + } |
| 290 | + |
| 291 | + if (ksm_write_sysfs(KSM_FP("run"), 2) || |
| 292 | + ksm_write_sysfs(KSM_FP("sleep_millisecs"), 0) || |
| 293 | + ksm_write_sysfs(KSM_FP("merge_across_nodes"), 1) || |
| 294 | + ksm_write_sysfs(KSM_FP("pages_to_scan"), page_count)) |
| 295 | + return KSFT_FAIL; |
| 296 | + |
| 297 | + ret = check_ksm_merge(MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec, |
| 298 | + page_size); |
| 299 | + |
| 300 | + if (ksm_restore(&ksm_sysfs_old)) { |
| 301 | + printf("Cannot restore default tunables\n"); |
| 302 | + return KSFT_FAIL; |
| 303 | + } |
| 304 | + |
| 305 | + return ret; |
| 306 | +} |
0 commit comments