Skip to content

Commit fed46d1

Browse files
yosrym93kelsey-steele
authored andcommitted
selftests: cgroup: add a selftest for memory.reclaim
Add a new test for memory.reclaim that verifies that the interface correctly reclaims memory as intended, from both anon and file pages. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Yosry Ahmed <[email protected]> Acked-by: Roman Gushchin <[email protected]> Acked-by: David Rientjes <[email protected]> Cc: Chen Wandun <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Greg Thelen <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Michal Hocko <[email protected]> Cc: "Michal Koutn" <[email protected]> Cc: Shakeel Butt <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Tim Chen <[email protected]> Cc: Vaibhav Jain <[email protected]> Cc: Wei Xu <[email protected]> Cc: Yu Zhao <[email protected]> Cc: Zefan Li <[email protected]> Signed-off-by: Andrew Morton <[email protected]> [kms: backport to 5.15] Signed-off-by: Kelsey Steele <[email protected]>
1 parent 2b88c49 commit fed46d1

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

tools/testing/selftests/cgroup/test_memcontrol.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,111 @@ static int test_memcg_max(const char *root)
683683
return ret;
684684
}
685685

686+
/*
687+
* This test checks that memory.reclaim reclaims the given
688+
* amount of memory (from both anon and file, if possible).
689+
*/
690+
static int test_memcg_reclaim(const char *root)
691+
{
692+
int ret = KSFT_FAIL, fd, retries;
693+
char *memcg;
694+
long current, expected_usage, to_reclaim;
695+
char buf[64];
696+
697+
memcg = cg_name(root, "memcg_test");
698+
if (!memcg)
699+
goto cleanup;
700+
701+
if (cg_create(memcg))
702+
goto cleanup;
703+
704+
current = cg_read_long(memcg, "memory.current");
705+
if (current != 0)
706+
goto cleanup;
707+
708+
fd = get_temp_fd();
709+
if (fd < 0)
710+
goto cleanup;
711+
712+
cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd);
713+
714+
/*
715+
* If swap is enabled, try to reclaim from both anon and file, else try
716+
* to reclaim from file only.
717+
*/
718+
if (is_swap_enabled()) {
719+
cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50));
720+
expected_usage = MB(100);
721+
} else
722+
expected_usage = MB(50);
723+
724+
/*
725+
* Wait until current usage reaches the expected usage (or we run out of
726+
* retries).
727+
*/
728+
retries = 5;
729+
while (!values_close(cg_read_long(memcg, "memory.current"),
730+
expected_usage, 10)) {
731+
if (retries--) {
732+
sleep(1);
733+
continue;
734+
} else {
735+
fprintf(stderr,
736+
"failed to allocate %ld for memcg reclaim test\n",
737+
expected_usage);
738+
goto cleanup;
739+
}
740+
}
741+
742+
/*
743+
* Reclaim until current reaches 30M, this makes sure we hit both anon
744+
* and file if swap is enabled.
745+
*/
746+
retries = 5;
747+
while (true) {
748+
int err;
749+
750+
current = cg_read_long(memcg, "memory.current");
751+
to_reclaim = current - MB(30);
752+
753+
/*
754+
* We only keep looping if we get EAGAIN, which means we could
755+
* not reclaim the full amount.
756+
*/
757+
if (to_reclaim <= 0)
758+
goto cleanup;
759+
760+
761+
snprintf(buf, sizeof(buf), "%ld", to_reclaim);
762+
err = cg_write(memcg, "memory.reclaim", buf);
763+
if (!err) {
764+
/*
765+
* If writing succeeds, then the written amount should have been
766+
* fully reclaimed (and maybe more).
767+
*/
768+
current = cg_read_long(memcg, "memory.current");
769+
if (!values_close(current, MB(30), 3) && current > MB(30))
770+
goto cleanup;
771+
break;
772+
}
773+
774+
/* The kernel could not reclaim the full amount, try again. */
775+
if (err == -EAGAIN && retries--)
776+
continue;
777+
778+
/* We got an unexpected error or ran out of retries. */
779+
goto cleanup;
780+
}
781+
782+
ret = KSFT_PASS;
783+
cleanup:
784+
cg_destroy(memcg);
785+
free(memcg);
786+
close(fd);
787+
788+
return ret;
789+
}
790+
686791
static int alloc_anon_50M_check_swap(const char *cgroup, void *arg)
687792
{
688793
long mem_max = (long)arg;
@@ -1185,6 +1290,7 @@ struct memcg_test {
11851290
T(test_memcg_low),
11861291
T(test_memcg_high),
11871292
T(test_memcg_max),
1293+
T(test_memcg_reclaim),
11881294
T(test_memcg_oom_events),
11891295
T(test_memcg_swap_max),
11901296
T(test_memcg_sock),

0 commit comments

Comments
 (0)