[PATCH 3/4] samples/cgroup: arm the inotify watch before reading the baseline
From: Song Hu
Date: Mon Nov 02 2026 - 07:33:41 EST
The listener reads the initial counter state first and arms the
inotify watch afterwards, so any event firing in between is lost:
it is already included in the baseline (no diff will ever be reported)
and no watch existed to queue a notification.
Arm the watch first, then read the baseline.
Signed-off-by: Song Hu <husong@xxxxxxxxxx>
---
samples/cgroup/memcg_event_listener.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/samples/cgroup/memcg_event_listener.c b/samples/cgroup/memcg_event_listener.c
index 996bf6fb117c..c23af43bf6cc 100644
--- a/samples/cgroup/memcg_event_listener.c
+++ b/samples/cgroup/memcg_event_listener.c
@@ -293,12 +293,6 @@ static int initialize_memcg_events(struct memcg_events *events,
return ret;
}
- ret = read_memcg_events(events, /* show_diff = */false);
- if (ret) {
- warnx("Failed to read initial memcg events state (%d)", ret);
- return ret;
- }
-
events->inotify_fd = inotify_init();
if (events->inotify_fd < 0) {
warn("Failed to setup new inotify device");
@@ -313,6 +307,17 @@ static int initialize_memcg_events(struct memcg_events *events,
return -EIO;
}
+ /*
+ * Read the initial state only after the watch is armed, so no
+ * event can slip through the window between the baseline read and
+ * the watch creation.
+ */
+ ret = read_memcg_events(events, /* show_diff = */false);
+ if (ret) {
+ warnx("Failed to read initial memcg events state (%d)", ret);
+ return ret;
+ }
+
printf("Initialized MEMCG events with counters:\n");
print_memcg_counters(&events->counters);
--
2.53.0