Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Song Hu" <husong@kylinos.cn>,
	hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev,
	shakeelb@google.com, muchun.song@linux.dev,
	akpm@linux-foundation.org
Cc: tj@kernel.org, tjmercier@google.com, ddrokosov@salutedevices.com,
	linux-mm@kvack.org, mkoutny@suse.com
Subject: Re: [PATCH 3/4] samples/cgroup: arm the inotify watch before reading the baseline
Date: Tue, 03 Nov 2026 12:25:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-195236@kernel.org> (raw)
In-Reply-To: <20261102122528.1498193-4-husong@kylinos.cn>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] samples/cgroup: events before read_memcg_events() are absorbed into baseline
--

commit e11bb86059068b47a263deb2a44878c61af9a6da
Author: Song Hu <husong@kylinos.cn>

samples/cgroup: arm the inotify watch before reading the baseline

This commit moves the initial read_memcg_events() call in
initialize_memcg_events() to occur after inotify_add_watch() so that
events firing during initialization are not missed before the watch is
created.

> diff --git a/samples/cgroup/memcg_event_listener.c b/samples/cgroup/memcg_event_listener.c
> index 996bf6fb117c4..c23af43bf6ccd 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;
> +	}

[Severity: Medium]
If a memcg event fires after inotify_add_watch() arms the watch in
initialize_memcg_events(), but before read_memcg_events(events, false)
reads the initial counters, won't that event be absorbed into the baseline
without its diff ever being reported?

When the event fires in that window, the kernel increments the cumulative
counter in memory.events and queues an IN_MODIFY notification on
events->inotify_fd. Then initialize_memcg_events() calls
read_memcg_events(events, false):

samples/cgroup/memcg_event_listener.c:read_memcg_events() {
    ...
	for (i = 0; i < ARRAY_SIZE(map); ++i) {
		long diff;

		if (*map[i].new > *map[i].old) {
			diff = *map[i].new - *map[i].old;

			if (show_diff)
				printf("*** %ld MEMCG %s event%s, "
				       "change counter %ld => %ld\n",
				       diff, map[i].name,
				       (diff == 1) ? "" : "s",
				       *map[i].old, *map[i].new);

			*map[i].old += diff;
			any_new_events = true;
		}
	}

	if (show_diff && !any_new_events)
		printf("*** No new untracked memcg events available\n");
    ...
}

Because show_diff is false, *map[i].old is updated to the new counter
value without printing the diff. When monitor_events() subsequently enters
poll() -> process_memcg_events() -> read_memcg_events(events, true) to
handle the queued IN_MODIFY notification, *map[i].new > *map[i].old is now
false, so no diff is reported and it instead prints:

  *** No new untracked memcg events available

Also, the commit message states that in the original order (reading the
baseline first and arming the watch afterwards), an event firing in
between "is already included in the baseline (no diff will ever be
reported) and no watch existed to queue a notification". Isn't that
description inverted? In the original order, an event firing after the
baseline read was not included in the baseline (it only lacked an inotify
notification until a subsequent event arrived), whereas with the new order
an event in the window is included in the baseline while queueing a
notification whose diff is already consumed.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261102122528.1498193-1-husong@kylinos.cn?part=3


  reply	other threads:[~2026-11-03 12:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 12:25 [PATCH 0/4] samples/cgroup: fix issues in the event listener samples Song Hu
2026-11-02 12:25 ` [PATCH 1/4] samples/cgroup: fix poll error handling in memcg_event_listener Song Hu
2026-11-02 12:25 ` [PATCH 2/4] samples/cgroup: exit cleanly when the monitored memcg is removed Song Hu
2026-11-02 12:25 ` [PATCH 3/4] samples/cgroup: arm the inotify watch before reading the baseline Song Hu
2026-11-03 12:25   ` sashiko-bot [this message]
2026-11-02 12:25 ` [PATCH 4/4] samples/cgroup: don't let dirname() clobber the monitored path Song Hu
2026-11-03 12:26   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-195236@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=ddrokosov@salutedevices.com \
    --cc=hannes@cmpxchg.org \
    --cc=husong@kylinos.cn \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeelb@google.com \
    --cc=tj@kernel.org \
    --cc=tjmercier@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox