Path: blob/master/tools/testing/selftests/cgroup/test_zswap.c
26285 views
// SPDX-License-Identifier: GPL-2.01#define _GNU_SOURCE23#include <linux/limits.h>4#include <unistd.h>5#include <stdio.h>6#include <signal.h>7#include <sys/sysinfo.h>8#include <string.h>9#include <sys/wait.h>10#include <sys/mman.h>1112#include "../kselftest.h"13#include "cgroup_util.h"1415static int read_int(const char *path, size_t *value)16{17FILE *file;18int ret = 0;1920file = fopen(path, "r");21if (!file)22return -1;23if (fscanf(file, "%ld", value) != 1)24ret = -1;25fclose(file);26return ret;27}2829static int set_min_free_kb(size_t value)30{31FILE *file;32int ret;3334file = fopen("/proc/sys/vm/min_free_kbytes", "w");35if (!file)36return -1;37ret = fprintf(file, "%ld\n", value);38fclose(file);39return ret;40}4142static int read_min_free_kb(size_t *value)43{44return read_int("/proc/sys/vm/min_free_kbytes", value);45}4647static int get_zswap_stored_pages(size_t *value)48{49return read_int("/sys/kernel/debug/zswap/stored_pages", value);50}5152static long get_cg_wb_count(const char *cg)53{54return cg_read_key_long(cg, "memory.stat", "zswpwb");55}5657static long get_zswpout(const char *cgroup)58{59return cg_read_key_long(cgroup, "memory.stat", "zswpout ");60}6162static int allocate_and_read_bytes(const char *cgroup, void *arg)63{64size_t size = (size_t)arg;65char *mem = (char *)malloc(size);66int ret = 0;6768if (!mem)69return -1;70for (int i = 0; i < size; i += 4095)71mem[i] = 'a';7273/* Go through the allocated memory to (z)swap in and out pages */74for (int i = 0; i < size; i += 4095) {75if (mem[i] != 'a')76ret = -1;77}7879free(mem);80return ret;81}8283static int allocate_bytes(const char *cgroup, void *arg)84{85size_t size = (size_t)arg;86char *mem = (char *)malloc(size);8788if (!mem)89return -1;90for (int i = 0; i < size; i += 4095)91mem[i] = 'a';92free(mem);93return 0;94}9596static char *setup_test_group_1M(const char *root, const char *name)97{98char *group_name = cg_name(root, name);99100if (!group_name)101return NULL;102if (cg_create(group_name))103goto fail;104if (cg_write(group_name, "memory.max", "1M")) {105cg_destroy(group_name);106goto fail;107}108return group_name;109fail:110free(group_name);111return NULL;112}113114/*115* Sanity test to check that pages are written into zswap.116*/117static int test_zswap_usage(const char *root)118{119long zswpout_before, zswpout_after;120int ret = KSFT_FAIL;121char *test_group;122123test_group = cg_name(root, "no_shrink_test");124if (!test_group)125goto out;126if (cg_create(test_group))127goto out;128if (cg_write(test_group, "memory.max", "1M"))129goto out;130131zswpout_before = get_zswpout(test_group);132if (zswpout_before < 0) {133ksft_print_msg("Failed to get zswpout\n");134goto out;135}136137/* Allocate more than memory.max to push memory into zswap */138if (cg_run(test_group, allocate_bytes, (void *)MB(4)))139goto out;140141/* Verify that pages come into zswap */142zswpout_after = get_zswpout(test_group);143if (zswpout_after <= zswpout_before) {144ksft_print_msg("zswpout does not increase after test program\n");145goto out;146}147ret = KSFT_PASS;148149out:150cg_destroy(test_group);151free(test_group);152return ret;153}154155/*156* Check that when memory.zswap.max = 0, no pages can go to the zswap pool for157* the cgroup.158*/159static int test_swapin_nozswap(const char *root)160{161int ret = KSFT_FAIL;162char *test_group;163long swap_peak, zswpout;164165test_group = cg_name(root, "no_zswap_test");166if (!test_group)167goto out;168if (cg_create(test_group))169goto out;170if (cg_write(test_group, "memory.max", "8M"))171goto out;172if (cg_write(test_group, "memory.zswap.max", "0"))173goto out;174175/* Allocate and read more than memory.max to trigger swapin */176if (cg_run(test_group, allocate_and_read_bytes, (void *)MB(32)))177goto out;178179/* Verify that pages are swapped out, but no zswap happened */180swap_peak = cg_read_long(test_group, "memory.swap.peak");181if (swap_peak < 0) {182ksft_print_msg("failed to get cgroup's swap_peak\n");183goto out;184}185186if (swap_peak < MB(24)) {187ksft_print_msg("at least 24MB of memory should be swapped out\n");188goto out;189}190191zswpout = get_zswpout(test_group);192if (zswpout < 0) {193ksft_print_msg("failed to get zswpout\n");194goto out;195}196197if (zswpout > 0) {198ksft_print_msg("zswapout > 0 when memory.zswap.max = 0\n");199goto out;200}201202ret = KSFT_PASS;203204out:205cg_destroy(test_group);206free(test_group);207return ret;208}209210/* Simple test to verify the (z)swapin code paths */211static int test_zswapin(const char *root)212{213int ret = KSFT_FAIL;214char *test_group;215long zswpin;216217test_group = cg_name(root, "zswapin_test");218if (!test_group)219goto out;220if (cg_create(test_group))221goto out;222if (cg_write(test_group, "memory.max", "8M"))223goto out;224if (cg_write(test_group, "memory.zswap.max", "max"))225goto out;226227/* Allocate and read more than memory.max to trigger (z)swap in */228if (cg_run(test_group, allocate_and_read_bytes, (void *)MB(32)))229goto out;230231zswpin = cg_read_key_long(test_group, "memory.stat", "zswpin ");232if (zswpin < 0) {233ksft_print_msg("failed to get zswpin\n");234goto out;235}236237if (zswpin < MB(24) / PAGE_SIZE) {238ksft_print_msg("at least 24MB should be brought back from zswap\n");239goto out;240}241242ret = KSFT_PASS;243244out:245cg_destroy(test_group);246free(test_group);247return ret;248}249250/*251* Attempt writeback with the following steps:252* 1. Allocate memory.253* 2. Reclaim memory equal to the amount that was allocated in step 1.254This will move it into zswap.255* 3. Save current zswap usage.256* 4. Move the memory allocated in step 1 back in from zswap.257* 5. Set zswap.max to half the amount that was recorded in step 3.258* 6. Attempt to reclaim memory equal to the amount that was allocated,259this will either trigger writeback if it's enabled, or reclamation260will fail if writeback is disabled as there isn't enough zswap space.261*/262static int attempt_writeback(const char *cgroup, void *arg)263{264long pagesize = sysconf(_SC_PAGESIZE);265size_t memsize = MB(4);266char buf[pagesize];267long zswap_usage;268bool wb_enabled = *(bool *) arg;269int ret = -1;270char *mem;271272mem = (char *)malloc(memsize);273if (!mem)274return ret;275276/*277* Fill half of each page with increasing data, and keep other278* half empty, this will result in data that is still compressible279* and ends up in zswap, with material zswap usage.280*/281for (int i = 0; i < pagesize; i++)282buf[i] = i < pagesize/2 ? (char) i : 0;283284for (int i = 0; i < memsize; i += pagesize)285memcpy(&mem[i], buf, pagesize);286287/* Try and reclaim allocated memory */288if (cg_write_numeric(cgroup, "memory.reclaim", memsize)) {289ksft_print_msg("Failed to reclaim all of the requested memory\n");290goto out;291}292293zswap_usage = cg_read_long(cgroup, "memory.zswap.current");294295/* zswpin */296for (int i = 0; i < memsize; i += pagesize) {297if (memcmp(&mem[i], buf, pagesize)) {298ksft_print_msg("invalid memory\n");299goto out;300}301}302303if (cg_write_numeric(cgroup, "memory.zswap.max", zswap_usage/2))304goto out;305306/*307* If writeback is enabled, trying to reclaim memory now will trigger a308* writeback as zswap.max is half of what was needed when reclaim ran the first time.309* If writeback is disabled, memory reclaim will fail as zswap is limited and310* it can't writeback to swap.311*/312ret = cg_write_numeric(cgroup, "memory.reclaim", memsize);313if (!wb_enabled)314ret = (ret == -EAGAIN) ? 0 : -1;315316out:317free(mem);318return ret;319}320321static int test_zswap_writeback_one(const char *cgroup, bool wb)322{323long zswpwb_before, zswpwb_after;324325zswpwb_before = get_cg_wb_count(cgroup);326if (zswpwb_before != 0) {327ksft_print_msg("zswpwb_before = %ld instead of 0\n", zswpwb_before);328return -1;329}330331if (cg_run(cgroup, attempt_writeback, (void *) &wb))332return -1;333334/* Verify that zswap writeback occurred only if writeback was enabled */335zswpwb_after = get_cg_wb_count(cgroup);336if (zswpwb_after < 0)337return -1;338339if (wb != !!zswpwb_after) {340ksft_print_msg("zswpwb_after is %ld while wb is %s\n",341zswpwb_after, wb ? "enabled" : "disabled");342return -1;343}344345return 0;346}347348/* Test to verify the zswap writeback path */349static int test_zswap_writeback(const char *root, bool wb)350{351int ret = KSFT_FAIL;352char *test_group, *test_group_child = NULL;353354if (cg_read_strcmp(root, "memory.zswap.writeback", "1"))355return KSFT_SKIP;356357test_group = cg_name(root, "zswap_writeback_test");358if (!test_group)359goto out;360if (cg_create(test_group))361goto out;362if (cg_write(test_group, "memory.zswap.writeback", wb ? "1" : "0"))363goto out;364365if (test_zswap_writeback_one(test_group, wb))366goto out;367368/* Reset memory.zswap.max to max (modified by attempt_writeback), and369* set up child cgroup, whose memory.zswap.writeback is hardcoded to 1.370* Thus, the parent's setting shall be what's in effect. */371if (cg_write(test_group, "memory.zswap.max", "max"))372goto out;373if (cg_write(test_group, "cgroup.subtree_control", "+memory"))374goto out;375376test_group_child = cg_name(test_group, "zswap_writeback_test_child");377if (!test_group_child)378goto out;379if (cg_create(test_group_child))380goto out;381if (cg_write(test_group_child, "memory.zswap.writeback", "1"))382goto out;383384if (test_zswap_writeback_one(test_group_child, wb))385goto out;386387ret = KSFT_PASS;388389out:390if (test_group_child) {391cg_destroy(test_group_child);392free(test_group_child);393}394cg_destroy(test_group);395free(test_group);396return ret;397}398399static int test_zswap_writeback_enabled(const char *root)400{401return test_zswap_writeback(root, true);402}403404static int test_zswap_writeback_disabled(const char *root)405{406return test_zswap_writeback(root, false);407}408409/*410* When trying to store a memcg page in zswap, if the memcg hits its memory411* limit in zswap, writeback should affect only the zswapped pages of that412* memcg.413*/414static int test_no_invasive_cgroup_shrink(const char *root)415{416int ret = KSFT_FAIL;417size_t control_allocation_size = MB(10);418char *control_allocation = NULL, *wb_group = NULL, *control_group = NULL;419420wb_group = setup_test_group_1M(root, "per_memcg_wb_test1");421if (!wb_group)422return KSFT_FAIL;423if (cg_write(wb_group, "memory.zswap.max", "10K"))424goto out;425control_group = setup_test_group_1M(root, "per_memcg_wb_test2");426if (!control_group)427goto out;428429/* Push some test_group2 memory into zswap */430if (cg_enter_current(control_group))431goto out;432control_allocation = malloc(control_allocation_size);433for (int i = 0; i < control_allocation_size; i += 4095)434control_allocation[i] = 'a';435if (cg_read_key_long(control_group, "memory.stat", "zswapped") < 1)436goto out;437438/* Allocate 10x memory.max to push wb_group memory into zswap and trigger wb */439if (cg_run(wb_group, allocate_bytes, (void *)MB(10)))440goto out;441442/* Verify that only zswapped memory from gwb_group has been written back */443if (get_cg_wb_count(wb_group) > 0 && get_cg_wb_count(control_group) == 0)444ret = KSFT_PASS;445out:446cg_enter_current(root);447if (control_group) {448cg_destroy(control_group);449free(control_group);450}451cg_destroy(wb_group);452free(wb_group);453if (control_allocation)454free(control_allocation);455return ret;456}457458struct no_kmem_bypass_child_args {459size_t target_alloc_bytes;460size_t child_allocated;461};462463static int no_kmem_bypass_child(const char *cgroup, void *arg)464{465struct no_kmem_bypass_child_args *values = arg;466void *allocation;467468allocation = malloc(values->target_alloc_bytes);469if (!allocation) {470values->child_allocated = true;471return -1;472}473for (long i = 0; i < values->target_alloc_bytes; i += 4095)474((char *)allocation)[i] = 'a';475values->child_allocated = true;476pause();477free(allocation);478return 0;479}480481/*482* When pages owned by a memcg are pushed to zswap by kswapd, they should be483* charged to that cgroup. This wasn't the case before commit484* cd08d80ecdac("mm: correctly charge compressed memory to its memcg").485*486* The test first allocates memory in a memcg, then raises min_free_kbytes to487* a very high value so that the allocation falls below low wm, then makes488* another allocation to trigger kswapd that should push the memcg-owned pages489* to zswap and verifies that the zswap pages are correctly charged.490*491* To be run on a VM with at most 4G of memory.492*/493static int test_no_kmem_bypass(const char *root)494{495size_t min_free_kb_high, min_free_kb_low, min_free_kb_original;496struct no_kmem_bypass_child_args *values;497size_t trigger_allocation_size;498int wait_child_iteration = 0;499long stored_pages_threshold;500struct sysinfo sys_info;501int ret = KSFT_FAIL;502int child_status;503char *test_group = NULL;504pid_t child_pid;505506/* Read sys info and compute test values accordingly */507if (sysinfo(&sys_info) != 0)508return KSFT_FAIL;509if (sys_info.totalram > 5000000000)510return KSFT_SKIP;511values = mmap(0, sizeof(struct no_kmem_bypass_child_args), PROT_READ |512PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);513if (values == MAP_FAILED)514return KSFT_FAIL;515if (read_min_free_kb(&min_free_kb_original))516return KSFT_FAIL;517min_free_kb_high = sys_info.totalram / 2000;518min_free_kb_low = sys_info.totalram / 500000;519values->target_alloc_bytes = (sys_info.totalram - min_free_kb_high * 1000) +520sys_info.totalram * 5 / 100;521stored_pages_threshold = sys_info.totalram / 5 / 4096;522trigger_allocation_size = sys_info.totalram / 20;523524/* Set up test memcg */525test_group = cg_name(root, "kmem_bypass_test");526if (!test_group)527goto out;528529/* Spawn memcg child and wait for it to allocate */530set_min_free_kb(min_free_kb_low);531if (cg_create(test_group))532goto out;533values->child_allocated = false;534child_pid = cg_run_nowait(test_group, no_kmem_bypass_child, values);535if (child_pid < 0)536goto out;537while (!values->child_allocated && wait_child_iteration++ < 10000)538usleep(1000);539540/* Try to wakeup kswapd and let it push child memory to zswap */541set_min_free_kb(min_free_kb_high);542for (int i = 0; i < 20; i++) {543size_t stored_pages;544char *trigger_allocation = malloc(trigger_allocation_size);545546if (!trigger_allocation)547break;548for (int i = 0; i < trigger_allocation_size; i += 4095)549trigger_allocation[i] = 'b';550usleep(100000);551free(trigger_allocation);552if (get_zswap_stored_pages(&stored_pages))553break;554if (stored_pages < 0)555break;556/* If memory was pushed to zswap, verify it belongs to memcg */557if (stored_pages > stored_pages_threshold) {558int zswapped = cg_read_key_long(test_group, "memory.stat", "zswapped ");559int delta = stored_pages * 4096 - zswapped;560int result_ok = delta < stored_pages * 4096 / 4;561562ret = result_ok ? KSFT_PASS : KSFT_FAIL;563break;564}565}566567kill(child_pid, SIGTERM);568waitpid(child_pid, &child_status, 0);569out:570set_min_free_kb(min_free_kb_original);571cg_destroy(test_group);572free(test_group);573return ret;574}575576#define T(x) { x, #x }577struct zswap_test {578int (*fn)(const char *root);579const char *name;580} tests[] = {581T(test_zswap_usage),582T(test_swapin_nozswap),583T(test_zswapin),584T(test_zswap_writeback_enabled),585T(test_zswap_writeback_disabled),586T(test_no_kmem_bypass),587T(test_no_invasive_cgroup_shrink),588};589#undef T590591static bool zswap_configured(void)592{593return access("/sys/module/zswap", F_OK) == 0;594}595596int main(int argc, char **argv)597{598char root[PATH_MAX];599int i, ret = EXIT_SUCCESS;600601if (cg_find_unified_root(root, sizeof(root), NULL))602ksft_exit_skip("cgroup v2 isn't mounted\n");603604if (!zswap_configured())605ksft_exit_skip("zswap isn't configured\n");606607/*608* Check that memory controller is available:609* memory is listed in cgroup.controllers610*/611if (cg_read_strstr(root, "cgroup.controllers", "memory"))612ksft_exit_skip("memory controller isn't available\n");613614if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))615if (cg_write(root, "cgroup.subtree_control", "+memory"))616ksft_exit_skip("Failed to set memory controller\n");617618for (i = 0; i < ARRAY_SIZE(tests); i++) {619switch (tests[i].fn(root)) {620case KSFT_PASS:621ksft_test_result_pass("%s\n", tests[i].name);622break;623case KSFT_SKIP:624ksft_test_result_skip("%s\n", tests[i].name);625break;626default:627ret = EXIT_FAILURE;628ksft_test_result_fail("%s\n", tests[i].name);629break;630}631}632633return ret;634}635636637