selftests: memcg: Add tests for IN_DELETE_SELF and IN_IGNORED

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>
This commit is contained in:
T.J. Mercier
2026-02-25 14:34:04 -08:00
committed by Greg Kroah-Hartman
parent eea5d2bb34
commit 2de27980e1

View 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