From: David Finkel Date: Tue, 30 Jul 2024 23:13:04 +0000 (-0400) Subject: mm, memcg: cg2 memory{.swap,}.peak write tests X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=6a18fd890d168f7f952c1cea471059ba069bb77e;p=users%2Fjedix%2Flinux-maple.git mm, memcg: cg2 memory{.swap,}.peak write tests update tests Link: https://lkml.kernel.org/r/20240730231304.761942-3-davidf@vimeo.com Signed-off-by: David Finkel Acked-by: Tejun Heo Cc: Johannes Weiner Cc: Jonathan Corbet Cc: Michal Hocko Cc: Michal Koutný Cc: Muchun Song Cc: Roman Gushchin Cc: Shakeel Butt Cc: Shuah Khan Cc: Waiman Long Cc: Zefan Li Signed-off-by: Andrew Morton --- diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index f54c1f75b6da7..16f5d74ae762e 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -170,6 +170,7 @@ static int test_memcg_current_peak(const char *root) char *memcg; bool fd2_closed = false, fd3_closed = false, fd4_closed = false; int peak_fd = -1, peak_fd2 = -1, peak_fd3 = -1, peak_fd4 = -1; + struct stat ss; memcg = cg_name(root, "memcg_test"); if (!memcg) @@ -200,9 +201,25 @@ static int test_memcg_current_peak(const char *root) */ peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); - if (peak_fd == -1) + if (peak_fd == -1) { + if (errno == ENOENT) + ret = KSFT_SKIP; + goto cleanup; + } + + /* + * Before we try to use memory.peak's fd, try to figure out whether + * this kernel supports writing to that file in the first place. (by + * checking the writable bit on the file's st_mode) + */ + if (fstat(peak_fd, &ss)) goto cleanup; + if ((ss.st_mode & S_IWUSR) == 0) { + ret = KSFT_SKIP; + goto cleanup; + } + peak_fd2 = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); if (peak_fd2 == -1) @@ -920,6 +937,8 @@ static int test_memcg_swap_max_peak(const char *root) int ret = KSFT_FAIL; char *memcg; long max, peak; + struct stat ss; + int swap_peak_fd = -1, mem_peak_fd = -1; /* any non-empty string resets */ static const char reset_string[] = "foobarbaz"; @@ -939,13 +958,29 @@ static int test_memcg_swap_max_peak(const char *root) goto cleanup; } - int swap_peak_fd = cg_open(memcg, "memory.swap.peak", - O_RDWR | O_APPEND | O_CLOEXEC); + swap_peak_fd = cg_open(memcg, "memory.swap.peak", + O_RDWR | O_APPEND | O_CLOEXEC); - if (swap_peak_fd == -1) + if (swap_peak_fd == -1) { + if (errno == ENOENT) + ret = KSFT_SKIP; + goto cleanup; + } + + /* + * Before we try to use memory.swap.peak's fd, try to figure out + * whether this kernel supports writing to that file in the first + * place. (by checking the writable bit on the file's st_mode) + */ + if (fstat(swap_peak_fd, &ss)) goto cleanup; - int mem_peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); + if ((ss.st_mode & S_IWUSR) == 0) { + ret = KSFT_SKIP; + goto cleanup; + } + + mem_peak_fd = cg_open(memcg, "memory.peak", O_RDWR | O_APPEND | O_CLOEXEC); if (mem_peak_fd == -1) goto cleanup; @@ -1081,9 +1116,9 @@ static int test_memcg_swap_max_peak(const char *root) ret = KSFT_PASS; cleanup: - if (close(mem_peak_fd)) + if (mem_peak_fd != -1 && close(mem_peak_fd)) ret = KSFT_FAIL; - if (close(swap_peak_fd)) + if (swap_peak_fd != -1 && close(swap_peak_fd)) ret = KSFT_FAIL; cg_destroy(memcg); free(memcg);