Skip to content

Commit 158863e

Browse files
uarif1akpm00
authored andcommitted
selftests: cgroup: add tests to verify the zswap writeback path
Attempt writeback with the below steps and check using memory.stat.zswpwb if zswap writeback occurred: 1. Allocate memory. 2. Reclaim memory equal to the amount that was allocated in step 1. This will move it into zswap. 3. Save current zswap usage. 4. Move the memory allocated in step 1 back in from zswap. 5. Set zswap.max to half the amount that was recorded in step 3. 6. Attempt to reclaim memory equal to the amount that was allocated, this will either trigger writeback if it's enabled, or reclamation will fail if writeback is disabled as there isn't enough zswap space. Link: https://lkml.kernel.org/r/20240508171359.1545744-1-usamaarif642@gmail.com Signed-off-by: Usama Arif <usamaarif642@gmail.com> Suggested-by: Nhat Pham <nphamcs@gmail.com> Acked-by: Yosry Ahmed <yosryahmed@google.com> Acked-by: Nhat Pham <nphamcs@gmail.com> Cc: Chengming Zhou <chengming.zhou@linux.dev> Cc: Johannes Weiner <hannes@cmpxchg.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent a8248bb commit 158863e

File tree

1 file changed

+129
-1
lines changed

1 file changed

+129
-1
lines changed

tools/testing/selftests/cgroup/test_zswap.c

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static int get_zswap_stored_pages(size_t *value)
5050
return read_int("/sys/kernel/debug/zswap/stored_pages", value);
5151
}
5252

53-
static int get_cg_wb_count(const char *cg)
53+
static long get_cg_wb_count(const char *cg)
5454
{
5555
return cg_read_key_long(cg, "memory.stat", "zswpwb");
5656
}
@@ -248,6 +248,132 @@ static int test_zswapin(const char *root)
248248
return ret;
249249
}
250250

251+
/*
252+
* Attempt writeback with the following steps:
253+
* 1. Allocate memory.
254+
* 2. Reclaim memory equal to the amount that was allocated in step 1.
255+
This will move it into zswap.
256+
* 3. Save current zswap usage.
257+
* 4. Move the memory allocated in step 1 back in from zswap.
258+
* 5. Set zswap.max to half the amount that was recorded in step 3.
259+
* 6. Attempt to reclaim memory equal to the amount that was allocated,
260+
this will either trigger writeback if it's enabled, or reclamation
261+
will fail if writeback is disabled as there isn't enough zswap space.
262+
*/
263+
static int attempt_writeback(const char *cgroup, void *arg)
264+
{
265+
long pagesize = sysconf(_SC_PAGESIZE);
266+
char *test_group = arg;
267+
size_t memsize = MB(4);
268+
char buf[pagesize];
269+
long zswap_usage;
270+
bool wb_enabled;
271+
int ret = -1;
272+
char *mem;
273+
274+
wb_enabled = cg_read_long(test_group, "memory.zswap.writeback");
275+
mem = (char *)malloc(memsize);
276+
if (!mem)
277+
return ret;
278+
279+
/*
280+
* Fill half of each page with increasing data, and keep other
281+
* half empty, this will result in data that is still compressible
282+
* and ends up in zswap, with material zswap usage.
283+
*/
284+
for (int i = 0; i < pagesize; i++)
285+
buf[i] = i < pagesize/2 ? (char) i : 0;
286+
287+
for (int i = 0; i < memsize; i += pagesize)
288+
memcpy(&mem[i], buf, pagesize);
289+
290+
/* Try and reclaim allocated memory */
291+
if (cg_write_numeric(test_group, "memory.reclaim", memsize)) {
292+
ksft_print_msg("Failed to reclaim all of the requested memory\n");
293+
goto out;
294+
}
295+
296+
zswap_usage = cg_read_long(test_group, "memory.zswap.current");
297+
298+
/* zswpin */
299+
for (int i = 0; i < memsize; i += pagesize) {
300+
if (memcmp(&mem[i], buf, pagesize)) {
301+
ksft_print_msg("invalid memory\n");
302+
goto out;
303+
}
304+
}
305+
306+
if (cg_write_numeric(test_group, "memory.zswap.max", zswap_usage/2))
307+
goto out;
308+
309+
/*
310+
* If writeback is enabled, trying to reclaim memory now will trigger a
311+
* writeback as zswap.max is half of what was needed when reclaim ran the first time.
312+
* If writeback is disabled, memory reclaim will fail as zswap is limited and
313+
* it can't writeback to swap.
314+
*/
315+
ret = cg_write_numeric(test_group, "memory.reclaim", memsize);
316+
if (!wb_enabled)
317+
ret = (ret == -EAGAIN) ? 0 : -1;
318+
319+
out:
320+
free(mem);
321+
return ret;
322+
}
323+
324+
/* Test to verify the zswap writeback path */
325+
static int test_zswap_writeback(const char *root, bool wb)
326+
{
327+
long zswpwb_before, zswpwb_after;
328+
int ret = KSFT_FAIL;
329+
char *test_group;
330+
331+
test_group = cg_name(root, "zswap_writeback_test");
332+
if (!test_group)
333+
goto out;
334+
if (cg_create(test_group))
335+
goto out;
336+
if (cg_write(test_group, "memory.zswap.writeback", wb ? "1" : "0"))
337+
goto out;
338+
339+
zswpwb_before = get_cg_wb_count(test_group);
340+
if (zswpwb_before != 0) {
341+
ksft_print_msg("zswpwb_before = %ld instead of 0\n", zswpwb_before);
342+
goto out;
343+
}
344+
345+
if (cg_run(test_group, attempt_writeback, (void *) test_group))
346+
goto out;
347+
348+
/* Verify that zswap writeback occurred only if writeback was enabled */
349+
zswpwb_after = get_cg_wb_count(test_group);
350+
if (zswpwb_after < 0)
351+
goto out;
352+
353+
if (wb != !!zswpwb_after) {
354+
ksft_print_msg("zswpwb_after is %ld while wb is %s",
355+
zswpwb_after, wb ? "enabled" : "disabled");
356+
goto out;
357+
}
358+
359+
ret = KSFT_PASS;
360+
361+
out:
362+
cg_destroy(test_group);
363+
free(test_group);
364+
return ret;
365+
}
366+
367+
static int test_zswap_writeback_enabled(const char *root)
368+
{
369+
return test_zswap_writeback(root, true);
370+
}
371+
372+
static int test_zswap_writeback_disabled(const char *root)
373+
{
374+
return test_zswap_writeback(root, false);
375+
}
376+
251377
/*
252378
* When trying to store a memcg page in zswap, if the memcg hits its memory
253379
* limit in zswap, writeback should affect only the zswapped pages of that
@@ -423,6 +549,8 @@ struct zswap_test {
423549
T(test_zswap_usage),
424550
T(test_swapin_nozswap),
425551
T(test_zswapin),
552+
T(test_zswap_writeback_enabled),
553+
T(test_zswap_writeback_disabled),
426554
T(test_no_kmem_bypass),
427555
T(test_no_invasive_cgroup_shrink),
428556
};

0 commit comments

Comments
 (0)