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", "zswp_wb");
58 static long get_zswpout(const char *cgroup)
60 return cg_read_key_long(cgroup, "memory.stat", "zswpout ");
63 static int allocate_bytes(const char *cgroup, void *arg)
65 size_t size = (size_t)arg;
66 char *mem = (char *)malloc(size);
70 for (int i = 0; i < size; i += 4095)
76 static char *setup_test_group_1M(const char *root, const char *name)
78 char *group_name = cg_name(root, name);
82 if (cg_create(group_name))
84 if (cg_write(group_name, "memory.max", "1M")) {
85 cg_destroy(group_name);
95 * Sanity test to check that pages are written into zswap.
97 static int test_zswap_usage(const char *root)
99 long zswpout_before, zswpout_after;
104 test_group = cg_name(root, "no_shrink_test");
107 if (cg_create(test_group))
109 if (cg_write(test_group, "memory.max", "1M"))
112 zswpout_before = get_zswpout(test_group);
113 if (zswpout_before < 0) {
114 ksft_print_msg("Failed to get zswpout\n");
118 /* Allocate more than memory.max to push memory into zswap */
119 if (cg_run(test_group, allocate_bytes, (void *)MB(4)))
122 /* Verify that pages come into zswap */
123 zswpout_after = get_zswpout(test_group);
124 if (zswpout_after <= zswpout_before) {
125 ksft_print_msg("zswpout does not increase after test program\n");
131 cg_destroy(test_group);
137 * When trying to store a memcg page in zswap, if the memcg hits its memory
138 * limit in zswap, writeback should affect only the zswapped pages of that
141 static int test_no_invasive_cgroup_shrink(const char *root)
144 size_t control_allocation_size = MB(10);
145 char *control_allocation, *wb_group = NULL, *control_group = NULL;
148 wb_group = setup_test_group_1M(root, "per_memcg_wb_test1");
151 if (cg_write(wb_group, "memory.zswap.max", "10K"))
153 control_group = setup_test_group_1M(root, "per_memcg_wb_test2");
157 /* Push some test_group2 memory into zswap */
158 if (cg_enter_current(control_group))
160 control_allocation = malloc(control_allocation_size);
161 for (int i = 0; i < control_allocation_size; i += 4095)
162 control_allocation[i] = 'a';
163 if (cg_read_key_long(control_group, "memory.stat", "zswapped") < 1)
166 /* Allocate 10x memory.max to push wb_group memory into zswap and trigger wb */
167 if (cg_run(wb_group, allocate_bytes, (void *)MB(10)))
170 /* Verify that only zswapped memory from gwb_group has been written back */
171 if (get_cg_wb_count(wb_group) > 0 && get_cg_wb_count(control_group) == 0)
174 cg_enter_current(root);
176 cg_destroy(control_group);
179 cg_destroy(wb_group);
181 if (control_allocation)
182 free(control_allocation);
186 struct no_kmem_bypass_child_args {
187 size_t target_alloc_bytes;
188 size_t child_allocated;
191 static int no_kmem_bypass_child(const char *cgroup, void *arg)
193 struct no_kmem_bypass_child_args *values = arg;
196 allocation = malloc(values->target_alloc_bytes);
198 values->child_allocated = true;
201 for (long i = 0; i < values->target_alloc_bytes; i += 4095)
202 ((char *)allocation)[i] = 'a';
203 values->child_allocated = true;
210 * When pages owned by a memcg are pushed to zswap by kswapd, they should be
211 * charged to that cgroup. This wasn't the case before commit
212 * cd08d80ecdac("mm: correctly charge compressed memory to its memcg").
214 * The test first allocates memory in a memcg, then raises min_free_kbytes to
215 * a very high value so that the allocation falls below low wm, then makes
216 * another allocation to trigger kswapd that should push the memcg-owned pages
217 * to zswap and verifies that the zswap pages are correctly charged.
219 * To be run on a VM with at most 4G of memory.
221 static int test_no_kmem_bypass(const char *root)
223 size_t min_free_kb_high, min_free_kb_low, min_free_kb_original;
224 struct no_kmem_bypass_child_args *values;
225 size_t trigger_allocation_size;
226 int wait_child_iteration = 0;
227 long stored_pages_threshold;
228 struct sysinfo sys_info;
234 /* Read sys info and compute test values accordingly */
235 if (sysinfo(&sys_info) != 0)
237 if (sys_info.totalram > 5000000000)
239 values = mmap(0, sizeof(struct no_kmem_bypass_child_args), PROT_READ |
240 PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
241 if (values == MAP_FAILED)
243 if (read_min_free_kb(&min_free_kb_original))
245 min_free_kb_high = sys_info.totalram / 2000;
246 min_free_kb_low = sys_info.totalram / 500000;
247 values->target_alloc_bytes = (sys_info.totalram - min_free_kb_high * 1000) +
248 sys_info.totalram * 5 / 100;
249 stored_pages_threshold = sys_info.totalram / 5 / 4096;
250 trigger_allocation_size = sys_info.totalram / 20;
252 /* Set up test memcg */
253 if (cg_write(root, "cgroup.subtree_control", "+memory"))
255 test_group = cg_name(root, "kmem_bypass_test");
259 /* Spawn memcg child and wait for it to allocate */
260 set_min_free_kb(min_free_kb_low);
261 if (cg_create(test_group))
263 values->child_allocated = false;
264 child_pid = cg_run_nowait(test_group, no_kmem_bypass_child, values);
267 while (!values->child_allocated && wait_child_iteration++ < 10000)
270 /* Try to wakeup kswapd and let it push child memory to zswap */
271 set_min_free_kb(min_free_kb_high);
272 for (int i = 0; i < 20; i++) {
274 char *trigger_allocation = malloc(trigger_allocation_size);
276 if (!trigger_allocation)
278 for (int i = 0; i < trigger_allocation_size; i += 4095)
279 trigger_allocation[i] = 'b';
281 free(trigger_allocation);
282 if (get_zswap_stored_pages(&stored_pages))
284 if (stored_pages < 0)
286 /* If memory was pushed to zswap, verify it belongs to memcg */
287 if (stored_pages > stored_pages_threshold) {
288 int zswapped = cg_read_key_long(test_group, "memory.stat", "zswapped ");
289 int delta = stored_pages * 4096 - zswapped;
290 int result_ok = delta < stored_pages * 4096 / 4;
292 ret = result_ok ? KSFT_PASS : KSFT_FAIL;
297 kill(child_pid, SIGTERM);
298 waitpid(child_pid, &child_status, 0);
300 set_min_free_kb(min_free_kb_original);
301 cg_destroy(test_group);
306 #define T(x) { x, #x }
308 int (*fn)(const char *root);
312 T(test_no_kmem_bypass),
313 T(test_no_invasive_cgroup_shrink),
317 static bool zswap_configured(void)
319 return access("/sys/module/zswap", F_OK) == 0;
322 int main(int argc, char **argv)
325 int i, ret = EXIT_SUCCESS;
327 if (cg_find_unified_root(root, sizeof(root)))
328 ksft_exit_skip("cgroup v2 isn't mounted\n");
330 if (!zswap_configured())
331 ksft_exit_skip("zswap isn't configured\n");
334 * Check that memory controller is available:
335 * memory is listed in cgroup.controllers
337 if (cg_read_strstr(root, "cgroup.controllers", "memory"))
338 ksft_exit_skip("memory controller isn't available\n");
340 if (cg_read_strstr(root, "cgroup.subtree_control", "memory"))
341 if (cg_write(root, "cgroup.subtree_control", "+memory"))
342 ksft_exit_skip("Failed to set memory controller\n");
344 for (i = 0; i < ARRAY_SIZE(tests); i++) {
345 switch (tests[i].fn(root)) {
347 ksft_test_result_pass("%s\n", tests[i].name);
350 ksft_test_result_skip("%s\n", tests[i].name);
354 ksft_test_result_fail("%s\n", tests[i].name);