]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests: memcg: Add tests for IN_DELETE_SELF and IN_IGNORED
authorT.J. Mercier <tjmercier@google.com>
Wed, 25 Feb 2026 22:34:04 +0000 (14:34 -0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 12 Mar 2026 14:51:03 +0000 (15:51 +0100)
Add two new tests that verify inotify events are sent when memcg files
or directories are removed with rmdir.

Signed-off-by: T.J. Mercier <tjmercier@google.com>
Acked-by: Tejun Heo <tj@kernel.org>
Acked-by: Amir Goldstein <amir73il@gmail.com>
Tested-by: syzbot@syzkaller.appspotmail.com
Link: https://patch.msgid.link/20260225223404.783173-4-tjmercier@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
tools/testing/selftests/cgroup/test_memcontrol.c

index 2fb096a2a9f9232b1edf5cf4b8d2e9aa672b7bd2..ea05a2524d0fc21fa781abbd165d5e2d8c1c4918 100644 (file)
@@ -10,6 +10,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <sys/inotify.h>
 #include <sys/socket.h>
 #include <sys/wait.h>
 #include <arpa/inet.h>
@@ -1643,6 +1644,115 @@ cleanup:
        return ret;
 }
 
+static int read_event(int inotify_fd, int expected_event, int expected_wd)
+{
+       struct inotify_event event;
+       ssize_t len = 0;
+
+       len = read(inotify_fd, &event, sizeof(event));
+       if (len < (ssize_t)sizeof(event))
+               return -1;
+
+       if (event.mask != expected_event || event.wd != expected_wd) {
+               fprintf(stderr,
+                       "event does not match expected values: mask %d (expected %d) wd %d (expected %d)\n",
+                       event.mask, expected_event, event.wd, expected_wd);
+               return -1;
+       }
+
+       return 0;
+}
+
+static int test_memcg_inotify_delete_file(const char *root)
+{
+       int ret = KSFT_FAIL;
+       char *memcg = NULL;
+       int fd, wd;
+
+       memcg = cg_name(root, "memcg_test_0");
+
+       if (!memcg)
+               goto cleanup;
+
+       if (cg_create(memcg))
+               goto cleanup;
+
+       fd = inotify_init1(0);
+       if (fd == -1)
+               goto cleanup;
+
+       wd = inotify_add_watch(fd, cg_control(memcg, "memory.events"), IN_DELETE_SELF);
+       if (wd == -1)
+               goto cleanup;
+
+       if (cg_destroy(memcg))
+               goto cleanup;
+       free(memcg);
+       memcg = NULL;
+
+       if (read_event(fd, IN_DELETE_SELF, wd))
+               goto cleanup;
+
+       if (read_event(fd, IN_IGNORED, wd))
+               goto cleanup;
+
+       ret = KSFT_PASS;
+
+cleanup:
+       if (fd >= 0)
+               close(fd);
+       if (memcg)
+               cg_destroy(memcg);
+       free(memcg);
+
+       return ret;
+}
+
+static int test_memcg_inotify_delete_dir(const char *root)
+{
+       int ret = KSFT_FAIL;
+       char *memcg = NULL;
+       int fd, wd;
+
+       memcg = cg_name(root, "memcg_test_0");
+
+       if (!memcg)
+               goto cleanup;
+
+       if (cg_create(memcg))
+               goto cleanup;
+
+       fd = inotify_init1(0);
+       if (fd == -1)
+               goto cleanup;
+
+       wd = inotify_add_watch(fd, memcg, IN_DELETE_SELF);
+       if (wd == -1)
+               goto cleanup;
+
+       if (cg_destroy(memcg))
+               goto cleanup;
+       free(memcg);
+       memcg = NULL;
+
+       if (read_event(fd, IN_DELETE_SELF, wd))
+               goto cleanup;
+
+       if (read_event(fd, IN_IGNORED, wd))
+               goto cleanup;
+
+       ret = KSFT_PASS;
+
+cleanup:
+       if (fd >= 0)
+               close(fd);
+       if (memcg)
+               cg_destroy(memcg);
+       free(memcg);
+
+       return ret;
+}
+
 #define T(x) { x, #x }
 struct memcg_test {
        int (*fn)(const char *root);
@@ -1662,6 +1772,8 @@ struct memcg_test {
        T(test_memcg_oom_group_leaf_events),
        T(test_memcg_oom_group_parent_events),
        T(test_memcg_oom_group_score_events),
+       T(test_memcg_inotify_delete_file),
+       T(test_memcg_inotify_delete_dir),
 };
 #undef T