| From: David Finkel <davidf@vimeo.com> |
| Subject: mm, memcg: cg2 memory{.swap,}.peak write tests |
| Date: Tue, 30 Jul 2024 19:13:04 -0400 |
| |
| update tests |
| |
| Link: https://lkml.kernel.org/r/20240730231304.761942-3-davidf@vimeo.com |
| Signed-off-by: David Finkel <davidf@vimeo.com> |
| Acked-by: Tejun Heo <tj@kernel.org> |
| Cc: Johannes Weiner <hannes@cmpxchg.org> |
| Cc: Jonathan Corbet <corbet@lwn.net> |
| Cc: Michal Hocko <mhocko@kernel.org> |
| Cc: Michal Koutný <mkoutny@suse.com> |
| Cc: Muchun Song <muchun.song@linux.dev> |
| Cc: Roman Gushchin <roman.gushchin@linux.dev> |
| Cc: Shakeel Butt <shakeel.butt@linux.dev> |
| Cc: Shuah Khan <shuah@kernel.org> |
| Cc: Waiman Long <longman@redhat.com> |
| Cc: Zefan Li <lizefan.x@bytedance.com> |
| Signed-off-by: Andrew Morton <akpm@linux-foundation.org> |
| --- |
| |
| tools/testing/selftests/cgroup/test_memcontrol.c | 49 +++++++++++-- |
| 1 file changed, 42 insertions(+), 7 deletions(-) |
| |
| --- a/tools/testing/selftests/cgroup/test_memcontrol.c~mm-memcg-cg2-memoryswappeak-write-tests-v7 |
| +++ a/tools/testing/selftests/cgroup/test_memcontrol.c |
| @@ -170,6 +170,7 @@ static int test_memcg_current_peak(const |
| 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 |
| */ |
| 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(cons |
| 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(cons |
| 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(cons |
| 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); |
| _ |