* [PATCH] inotify: do not coalesce events with different rename cookies
@ 2026-10-25 23:22 Hungyu Lin
2026-10-26 9:32 ` Amir Goldstein
0 siblings, 1 reply; 2+ messages in thread
From: Hungyu Lin @ 2026-10-25 23:22 UTC (permalink / raw)
To: Jan Kara
Cc: Amir Goldstein, linux-fsdevel, linux-kernel, stable, dennylin0707
inotify compares the mask, watch descriptor and name when coalescing
adjacent unread events, but ignores their rename cookies. Distinct
renames can therefore lose an event even with IN_ALL_EVENTS.
For example, prepare two files in an unwatched staging directory and
rename each onto the same pathname in a watched destination directory
before reading the events. Only the first IN_MOVED_TO event is retained,
although the two renames have different cookies. Similarly, coalescing
distinct IN_MOVED_FROM events can discard the cookie needed to match a
subsequent IN_MOVED_TO event.
The original inotify implementation compared cookies before coalescing.
The fsnotify conversion lost this comparison. Compare sync_cookie as well
to preserve distinct rename events, while retaining coalescing for
ordinary events with zero cookies.
Fixes: 63c882a05416 ("inotify: reimplement inotify using fsnotify")
Cc: stable@vger.kernel.org
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
fs/notify/inotify/inotify_fsnotify.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
index 7c326ec2e8a8..6456d9c0e408 100644
--- a/fs/notify/inotify/inotify_fsnotify.c
+++ b/fs/notify/inotify/inotify_fsnotify.c
@@ -40,6 +40,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,
return false;
if ((old->mask == new->mask) &&
(old->wd == new->wd) &&
+ (old->sync_cookie == new->sync_cookie) &&
(old->name_len == new->name_len) &&
(!old->name_len || !strcmp(old->name, new->name)))
return true;
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] inotify: do not coalesce events with different rename cookies
2026-10-25 23:22 [PATCH] inotify: do not coalesce events with different rename cookies Hungyu Lin
@ 2026-10-26 9:32 ` Amir Goldstein
0 siblings, 0 replies; 2+ messages in thread
From: Amir Goldstein @ 2026-10-26 9:32 UTC (permalink / raw)
To: Hungyu Lin; +Cc: Jan Kara, linux-fsdevel, linux-kernel, stable
On Mon, Oct 26, 2026 at 1:23 AM Hungyu Lin <dennylin0707@gmail.com> wrote:
>
> inotify compares the mask, watch descriptor and name when coalescing
> adjacent unread events, but ignores their rename cookies. Distinct
> renames can therefore lose an event even with IN_ALL_EVENTS.
oops. Somehow I remembered that we never merge MOVE events.
Maybe that's even a simpler check. IDK.
>
> For example, prepare two files in an unwatched staging directory and
> rename each onto the same pathname in a watched destination directory
> before reading the events. Only the first IN_MOVED_TO event is retained,
> although the two renames have different cookies.
Thing is, the "wrong" rename TO cookie is meaningless without the FROM pair.
If the second source has a FROM, then the two TO won't merge.
Could you really reproduce this case?
> Similarly, coalescing
> distinct IN_MOVED_FROM events can discard the cookie needed to match a
> subsequent IN_MOVED_TO event.
THIS looks like a case that can be reproduced.
Can you provide an inotify LTP test for this case please.
>
> The original inotify implementation compared cookies before coalescing.
> The fsnotify conversion lost this comparison. Compare sync_cookie as well
> to preserve distinct rename events, while retaining coalescing for
> ordinary events with zero cookies.
>
> Fixes: 63c882a05416 ("inotify: reimplement inotify using fsnotify")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
> ---
> fs/notify/inotify/inotify_fsnotify.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/fs/notify/inotify/inotify_fsnotify.c b/fs/notify/inotify/inotify_fsnotify.c
> index 7c326ec2e8a8..6456d9c0e408 100644
> --- a/fs/notify/inotify/inotify_fsnotify.c
> +++ b/fs/notify/inotify/inotify_fsnotify.c
> @@ -40,6 +40,7 @@ static bool event_compare(struct fsnotify_event *old_fsn,
> return false;
> if ((old->mask == new->mask) &&
> (old->wd == new->wd) &&
> + (old->sync_cookie == new->sync_cookie) &&
> (old->name_len == new->name_len) &&
> (!old->name_len || !strcmp(old->name, new->name)))
> return true;
> --
> 2.43.0
>
Practically, this means that MOVED events can never be merged,
so a slightly cheaper and simpler test would be
if ((old->mask == new->mask) && (old->mask & INOTIFY_MERGEABLE_EVENTS) && ...
Thanks,
Amir.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-10-26 9:32 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-25 23:22 [PATCH] inotify: do not coalesce events with different rename cookies Hungyu Lin
2026-10-26 9:32 ` Amir Goldstein
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®