1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/limits.h>
8 #include <sys/sysinfo.h>
13 #include "../kselftest.h"
14 #include "cgroup_util.h"
16 static int read_int(const char *path, size_t *value)
21 file = fopen(path, "r");
24 if (fscanf(file, "%ld", value) != 1)
30 static int set_min_free_kb(size_t value)
35 file = fopen("/proc/sys/vm/min_free_kbytes", "w");
38 ret = fprintf(file, "%ld\n", value);
43 static int read_min_free_kb(size_t *value)
45 return read_int("/proc/sys/vm/min_free_kbytes", value);
48 static int get_zswap_stored_pages(size_t *value)
50 return read_int("/sys/kernel/debug/zswap/stored_pages", value);
53 static int get_cg_wb_count(const char *cg)
55 return cg_read_key_long(cg, "memory.stat", "zswpwb");
58 static long get_zswpout(const char *cgroup)
60 return cg_read_key_long(cgroup, "memory.stat", "zswpout ");
63 static int allocate_and_read_bytes(const char *cgroup, void *arg)
65 size_t size = (size_t)arg;
66 char *mem = (char *)malloc(size);
71 for (int i = 0; i < size; i += 4095)
74 /* Go through the allocated memory to (z)swap in and out pages */
75 for (int i = 0; i < size; i += 4095) {
84 static int allocate_bytes(const char *cgroup, void *arg)
86 size_t size = (size_t)arg;
87 char *mem = (char *)malloc(size);
91 for (int i = 0; i < size; i += 4095)
97 static char *setup_test_group_1M(const char *root, const char *name)
99 char *group_name = cg_name(root, name);
103 if (cg_create(group_name))
105 if (cg_write(group_name, "memory.max", "1M")) {
106 cg_destroy(group_name);
116 * Sanity test to check that pages are written into zswap.
118 static int test_zswap_usage(const char *root)
120 long zswpout_before, zswpout_after;
124 test_group = cg_name(root, "no_shrink_test");
127 if (cg_create(test_group))
129 if (cg_write(test_group, "memory.max", "1M"))
132 zswpout_before = get_zswpout(test_group);
133 if (zswpout_before < 0) {
134 ksft_print_msg("Failed to get zswpout\n");
138 /* Allocate more than memory.max to push memory into zswap */
139 if (cg_run(test_group, allocate_bytes, (void *)MB(4)))
142 /* Verify that pages come into zswap */
143 zswpout_after = get_zswpout(test_group);
144 if (zswpout_after <= zswpout_before) {
145 ksft_print_msg("zswpout does not increase after test program\n");
151 cg_destroy(test_group);
157 * Check that when memory.zswap.max = 0, no pages can go to the zswap pool for
160 static int test_swapin_nozswap(const char *root)
164 long swap_peak, zswpout;
166 test_group = cg_name(root, "no_zswap_test");
169 if (cg_create(test_group))
171 if (cg_write(test_group, "memory.max", "8M"))
173 if (cg_write(test_group, "memory.zswap.max", "0"))
176 /* Allocate and read more than memory.max to trigger swapin */
177 if (cg_run(test_group, allocate_and_read_bytes, (void *)MB(32)))
180 /* Verify that pages are swapped out, but no zswap happened */
181 swap_peak = cg_read_long(test_group, "memory.swap.peak");
183 ksft_print_msg("failed to get cgroup's swap_peak\n");
187 if (swap_peak < MB(24)) {
188 ksft_print_msg("at least 24MB of memory should be swapped out\n");
192 zswpout = get_zswpout(test_group);
194 ksft_print_msg("failed to get zswpout\n");
199 ksft_print_msg("zswapout > 0 when memory.zswap.max = 0\n");
206 cg_destroy(test_group);
211 /* Simple test to verify the (z)swapin code paths */
212 static int test_zswapin(const char *root)
218 test_group = cg_name(root, "zswapin_test");
221 if (cg_create(test_group))
223 if (cg_write(test_group, "memory.max", "8M"))
225 if (cg_write(test_group, "memory.zswap.max", "max"))
228 /* Allocate and read more than memory.max to trigger (z)swap in */
229 if (cg_run(test_group, allocate_and_read_bytes, (void *)MB(32)))
232 zswpin = cg_read_key_long(test_group, "memory.stat", "zswpin ");
234 ksft_print_msg("failed to get zswpin\n");
238 if (zswpin < MB(24) / PAGE_SIZE) {
239 ksft_print_msg("at least 24MB should be brought back from zswap\n");
246 cg_destroy(test_group);
252 * When trying to store a memcg page in zswap, if the memcg hits its memory
253 * limit in zswap, writeback should affect only the zswapped pages of that
256 static int test_no_invasive_cgroup_shrink(const char *root)
259 size_t control_allocation_size = MB(10);
260 char *control_allocation, *wb_group = NULL, *control_group = NULL;
262 wb_group = setup_test_group_1M(root, "per_memcg_wb_test1");
265 if (cg_write(wb_group, "memory.zswap.max", "10K"))
267 control_group = setup_test_group_1M(root, "per_memcg_wb_test2");
271 /* Push some test_group2 memory into zswap */
272 if (cg_enter_current(control_group))
274 control_allocation = malloc(control_allocation_size);
275 for (int i = 0; i < control_allocation_size; i += 4095)
276 control_allocation[i] = 'a';
277 if (cg_read_key_long(control_group, "memory.stat", "zswapped") < 1)
280 /* Allocate 10x memory.max to push wb_group memory into zswap and trigger wb */
281 if (cg_run(wb_group, allocate_bytes, (void *)MB(10)))
284 /* Verify that only zswapped memory from gwb_group has been written back */
285 if (get_cg_wb_count(wb_group) > 0 && get_cg_wb_count(control_group) == 0)
288 cg_enter_current(root);
290 cg_destroy(control_group);
293 cg_destroy(wb_group);
295 if (control_allocation)
296 free(control_allocation);
300 struct no_kmem_bypass_child_args {
301 size_t target_alloc_bytes;
302 size_t child_allocated;
305 static int no_kmem_bypass_child(const char *cgroup, void *arg)
307 struct no_kmem_bypass_child_args *values = arg;
310 allocation = malloc(values->target_alloc_bytes);
312 values->child_allocated = true;
315 for (long i = 0; i < values->target_alloc_bytes; i += 4095)
316 ((char *)allocation)[i] = 'a';
317 values->child_allocated = true;
324 * When pages owned by a memcg are pushed to zswap by kswapd, they should be
325 * charged to that cgroup. This wasn't the case before commit
326 * cd08d80ecdac("mm: correctly charge compressed memory to its memcg").
328 * The test first allocates memory in a memcg, then raises min_free_kbytes to
329 * a very high value so that the allocation falls below low wm, then makes
330 * another allocation to trigger kswapd that should push the memcg-owned pages
331 * to zswap and verifies that the zswap pages are correctly charged.
333 * To be run on a VM with at most 4G of memory.
335 static int test_no_kmem_bypass(const char *root)
337 size_t min_free_kb_high, min_free_kb_low, min_free_kb_original;
338 struct no_kmem_bypass_child_args *values;
339 size_t trigger_allocation_size;
340 int wait_child_iteration = 0;
341 long stored_pages_threshold;
342 struct sysinfo sys_info;
348 /* Read sys info and compute test values accordingly */
349 if (sysinfo(&sys_info) != 0)
351 if (sys_info.totalram > 5000000000)
353 values = mmap(0, sizeof(struct no_kmem_bypass_child_args), PROT_READ |
354 PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
355 if (values == MAP_FAILED)
357 if (read_min_free_kb(&min_free_kb_original))
359 min_free_kb_high = sys_info.totalram / 2000;
360 min_free_kb_low = sys_info.totalram / 500000;
361 values->target_alloc_bytes = (sys_info.totalram - min_free_kb_high * 1000) +
362 sys_info.totalram * 5 / 100;
363 stored_pages_threshold = sys_info.totalram / 5 / 4096;
364 trigger_allocation_size = sys_info.totalram / 20;
366 /* Set up test memcg */
367 if (cg_write(root, "cgroup.subtree_control", "+memory"))
369 test_group = cg_name(root, "kmem_bypass_test");
373 /* Spawn memcg child and wait for it to allocate */
374 set_min_free_kb(min_free_kb_low);
375 if (cg_create(test_group))
377 values->child_allocated = false;
378 child_pid = cg_run_nowait(test_group, no_kmem_bypass_child, values);
381 while (!values->child_allocated && wait_child_iteration++ < 10000)
384 /* Try to wakeup kswapd and let it push child memory to zswap */
385 set_min_free_kb(min_free_kb_high);
386 for (int i = 0; i < 20; i++) {
388 char *trigger_allocation = malloc(trigger_allocation_size);
390 if (!trigger_allocation)
392 for (int i = 0; i < trigger_allocation_size; i += 4095)
393 trigger_allocation[i] = 'b';
395 free(trigger_allocation);
396 if (get_zswap_stored_pages(&stored_pages))
398 if (stored_pages < 0)
400 /* If memory was pushed to zswap, verify it belongs to memcg */
401 if (stored_pages > stored_pages_threshold) {
402 int zswapped = cg_read_key_long(test_group, "memory.stat", "zswapped ");
403 int delta = stored_pages * 4096 - zswapped;
404 int result_ok = delta < stored_pages * 4096 / 4;
406 ret = result_ok ? KSFT_PASS : KSFT_FAIL;
411 kill(child_pid, SIGTERM);
412 waitpid(child_pid, &child_status, 0);
414 set_min_free_kb(min_free_kb_original);
415 cg_destroy(test_group);
420 #define T(x) { x, #x }
422 int (*fn)(const char *root);
426 T(test_swapin_nozswap),
428 T(test_no_kmem_bypass),
429 T(test_no_invasive_cgroup_shrink),
433 static bool zswap_configured(void)
435 return access("/sys/module/zswap", F_OK) == 0;
438 int main(int argc, char **argv)
441 int i, ret = EXIT_SUCCESS;
443 if (cg_find_unified_root(root, sizeof(root)))
444 ksft_exit_skip("cgroup v2 isn't mounted\n");
446 if (!zswap_configured())
447 ksft_exit_skip("zswap isn't configured\n");
450 * Check that memory controller is available:
451 * memory is listed in cgroup.controllers
453 if (cg_read_strstr(root, "cgroup.controllers", "memory"))
454 ksft_exit_skip("memory controller isn't available\n");
456 if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
457 if (cg_write(root, "cgroup.subtree_control", "+memory"))
458 ksft_exit_skip("Failed to set memory controller\n");
460 for (i = 0; i < ARRAY_SIZE(tests); i++) {
461 switch (tests[i].fn(root)) {
463 ksft_test_result_pass("%s\n", tests[i].name);
466 ksft_test_result_skip("%s\n", tests[i].name);
470 ksft_test_result_fail("%s\n", tests[i].name);