Re: [PATCH] inotify: do not coalesce events with different rename cookies
From: Jan Kara
Date: Tue Nov 03 2026 - 06:28:19 EST
On Mon 26-10-26 11:32:10, Amir Goldstein wrote:
> On Mon, Oct 26, 2026 at 1:23 AM Hungyu Lin <dennylin0707@xxxxxxxxx> 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?
So I don't think this is something that practically matters (you have to
really try to screw up youself to trigger this). Anyway, I think comparing
the cookie is a sensible thing to do so I'm inclined to merge this. Any
objections Amir?
Honza
>
> > 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@xxxxxxxxxxxxxxx
> > Signed-off-by: Hungyu Lin <dennylin0707@xxxxxxxxx>
> > ---
> > 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.
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR