]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
mm, memcg: cg2 memory{.swap,}.peak write tests
authorDavid Finkel <davidf@vimeo.com>
Tue, 30 Jul 2024 23:13:04 +0000 (19:13 -0400)
committerAndrew Morton <akpm@linux-foundation.org>
Sat, 17 Aug 2024 00:52:39 +0000 (17:52 -0700)
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

index f54c1f75b6da7d594819d5088ce50fe1e651042d..16f5d74ae762edbd4540dcbe51185f38c3408120 100644 (file)
@@ -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);