Skip to content
Snippets Groups Projects
  1. Jan 14, 2025
    • Amir Goldstein's avatar
      fsnotify: fix sending inotify event with unexpected filename · 6e8f784f
      Amir Goldstein authored and Frieder Schrempf's avatar Frieder Schrempf committed
      
      commit aa52c54da40d9eee3ba87c05cdcb0cd07c04fa13 upstream.
      
      We got a report that adding a fanotify filsystem watch prevents tail -f
      from receiving events.
      
      Reproducer:
      
      1. Create 3 windows / login sessions. Become root in each session.
      2. Choose a mounted filesystem that is pretty quiet; I picked /boot.
      3. In the first window, run: fsnotifywait -S -m /boot
      4. In the second window, run: echo data >> /boot/foo
      5. In the third window, run: tail -f /boot/foo
      6. Go back to the second window and run: echo more data >> /boot/foo
      7. Observe that the tail command doesn't show the new data.
      8. In the first window, hit control-C to interrupt fsnotifywait.
      9. In the second window, run: echo still more data >> /boot/foo
      10. Observe that the tail command in the third window has now printed
      the missing data.
      
      When stracing tail, we observed that when fanotify filesystem mark is
      set, tail does get the inotify event, but the event is receieved with
      the filename:
      
      read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\20\0\0\0foo\0\0\0\0\0\0\0\0\0\0\0\0\0",
      50) = 32
      
      This is unexpected, because tail is watching the file itself and not its
      parent and is inconsistent with the inotify event received by tail when
      fanotify filesystem mark is not set:
      
      read(4, "\1\0\0\0\2\0\0\0\0\0\0\0\0\0\0\0", 50) = 16
      
      The inteference between different fsnotify groups was caused by the fact
      that the mark on the sb requires the filename, so the filename is passed
      to fsnotify().  Later on, fsnotify_handle_event() tries to take care of
      not passing the filename to groups (such as inotify) that are interested
      in the filename only when the parent is watching.
      
      But the logic was incorrect for the case that no group is watching the
      parent, some groups are watching the sb and some watching the inode.
      
      Reported-by: default avatarMiklos Szeredi <miklos@szeredi.hu>
      Fixes: 7372e79c ("fanotify: fix logic of reporting name info with watched parent")
      Cc: stable@vger.kernel.org # 5.10+
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      6e8f784f
  2. Sep 17, 2024
    • Amir Goldstein's avatar
      fsnotify: clear PARENT_WATCHED flags lazily · 6527d13a
      Amir Goldstein authored
      
      [ Upstream commit 172e422f ]
      
      In some setups directories can have many (usually negative) dentries.
      Hence __fsnotify_update_child_dentry_flags() function can take a
      significant amount of time. Since the bulk of this function happens
      under inode->i_lock this causes a significant contention on the lock
      when we remove the watch from the directory as the
      __fsnotify_update_child_dentry_flags() call from fsnotify_recalc_mask()
      races with __fsnotify_update_child_dentry_flags() calls from
      __fsnotify_parent() happening on children. This can lead upto softlockup
      reports reported by users.
      
      Fix the problem by calling fsnotify_update_children_dentry_flags() to
      set PARENT_WATCHED flags only when parent starts watching children.
      
      When parent stops watching children, clear false positive PARENT_WATCHED
      flags lazily in __fsnotify_parent() for each accessed child.
      
      Suggested-by: default avatarJan Kara <jack@suse.cz>
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarStephen Brennan <stephen.s.brennan@oracle.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
      6527d13a
  3. Aug 17, 2023
  4. May 17, 2023
  5. Sep 26, 2022
  6. Sep 09, 2022
  7. Sep 01, 2022
  8. Jul 26, 2022
  9. Jul 01, 2022
    • Amir Goldstein's avatar
      fanotify: introduce FAN_MARK_IGNORE · e252f2ed
      Amir Goldstein authored
      This flag is a new way to configure ignore mask which allows adding and
      removing the event flags FAN_ONDIR and FAN_EVENT_ON_CHILD in ignore mask.
      
      The legacy FAN_MARK_IGNORED_MASK flag would always ignore events on
      directories and would ignore events on children depending on whether
      the FAN_EVENT_ON_CHILD flag was set in the (non ignored) mask.
      
      FAN_MARK_IGNORE can be used to ignore events on children without setting
      FAN_EVENT_ON_CHILD in the mark's mask and will not ignore events on
      directories unconditionally, only when FAN_ONDIR is set in ignore mask.
      
      The new behavior is non-downgradable.  After calling fanotify_mark() with
      FAN_MARK_IGNORE once, calling fanotify_mark() with FAN_MARK_IGNORED_MASK
      on the same object will return EEXIST error.
      
      Setting the event flags with FAN_MARK_IGNORE on a non-dir inode mark
      has no meaning and will return ENOTDIR error.
      
      The meaning of FAN_MARK_IGNORED_SURV_MODIFY is preserved with the new
      FAN_MARK_IGNORE flag, but with a few semantic differences:
      
      1. FAN_MARK_IGNORED_SURV_MODIFY is required for filesystem and mount
         marks and on an inode mark on a directory. Omitting this flag
         will return EINVAL or EISDIR error.
      
      2. An ignore mask on a non-directory inode that survives modify could
         never be downgraded to an ignore mask that does not survive modify.
         With new FAN_MARK_IGNORE semantics we make that rule explicit -
         trying to update a surviving ignore mask without the flag
         FAN_MARK_IGNORED_SURV_MODIFY will return EEXIST error.
      
      The conveniene macro FAN_MARK_IGNORE_SURV is added for
      (FAN_MARK_IGNORE | FAN_MARK_IGNORED_SURV_MODIFY), because the
      common case should use short constant names.
      
      Link: https://lore.kernel.org/r/20220629144210.2983229-4-amir73il@gmail.com
      
      
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      e252f2ed
    • Amir Goldstein's avatar
      fanotify: cleanups for fanotify_mark() input validations · 8afd7215
      Amir Goldstein authored
      Create helper fanotify_may_update_existing_mark() for checking for
      conflicts between existing mark flags and fanotify_mark() flags.
      
      Use variable mark_cmd to make the checks for mark command bits
      cleaner.
      
      Link: https://lore.kernel.org/r/20220629144210.2983229-3-amir73il@gmail.com
      
      
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      8afd7215
    • Amir Goldstein's avatar
      fanotify: prepare for setting event flags in ignore mask · 31a371e4
      Amir Goldstein authored
      Setting flags FAN_ONDIR FAN_EVENT_ON_CHILD in ignore mask has no effect.
      The FAN_EVENT_ON_CHILD flag in mask implicitly applies to ignore mask and
      ignore mask is always implicitly applied to events on directories.
      
      Define a mark flag that replaces this legacy behavior with logic of
      applying the ignore mask according to event flags in ignore mask.
      
      Implement the new logic to prepare for supporting an ignore mask that
      ignores events on children and ignore mask that does not ignore events
      on directories.
      
      To emphasize the change in terminology, also rename ignored_mask mark
      member to ignore_mask and use accessors to get only the effective
      ignored events or the ignored events and flags.
      
      This change in terminology finally aligns with the "ignore mask"
      language in man pages and in most of the comments.
      
      Link: https://lore.kernel.org/r/20220629144210.2983229-2-amir73il@gmail.com
      
      
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      31a371e4
    • Oliver Ford's avatar
      fs: inotify: Fix typo in inotify comment · c05787b4
      Oliver Ford authored
      
      Correct spelling in comment.
      
      Signed-off-by: default avatarOliver Ford <ojford@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      Link: https://lore.kernel.org/r/20220518145959.41-1-ojford@gmail.com
      c05787b4
  10. Jun 28, 2022
  11. May 23, 2022
  12. May 18, 2022
  13. May 09, 2022
    • Amir Goldstein's avatar
      fanotify: do not allow setting dirent events in mask of non-dir · ceaf69f8
      Amir Goldstein authored
      Dirent events (create/delete/move) are only reported on watched
      directory inodes, but in fanotify as well as in legacy inotify, it was
      always allowed to set them on non-dir inode, which does not result in
      any meaningful outcome.
      
      Until kernel v5.17, dirent events in fanotify also differed from events
      "on child" (e.g. FAN_OPEN) in the information provided in the event.
      For example, FAN_OPEN could be set in the mask of a non-dir or the mask
      of its parent and event would report the fid of the child regardless of
      the marked object.
      By contrast, FAN_DELETE is not reported if the child is marked and the
      child fid was not reported in the events.
      
      Since kernel v5.17, with fanotify group flag FAN_REPORT_TARGET_FID, the
      fid of the child is reported with dirent events, like events "on child",
      which may create confusion for users expecting the same behavior as
      events "on child" when setting events in the mask on a child.
      
      The desired semantics of setting dirent events in the mask of a child
      are not clear, so for now, deny this action for a group initialized
      with flag FAN_REPORT_TARGET_FID and for the new event FAN_RENAME.
      We may relax this restriction in the future if we decide on the
      semantics and implement them.
      
      Fixes: d61fd650 ("fanotify: introduce group flag FAN_REPORT_TARGET_FID")
      Fixes: 8cc3b1cc ("fanotify: wire up FAN_RENAME event")
      Link: https://lore.kernel.org/linux-fsdevel/20220505133057.zm5t6vumc4xdcnsg@quack3.lan/
      
      
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      Link: https://lore.kernel.org/r/20220507080028.219826-1-amir73il@gmail.com
      ceaf69f8
  14. Apr 25, 2022
  15. Mar 14, 2022
  16. Feb 24, 2022
    • Amir Goldstein's avatar
      fsnotify: optimize FS_MODIFY events with no ignored masks · 04e317ba
      Amir Goldstein authored
      fsnotify() treats FS_MODIFY events specially - it does not skip them
      even if the FS_MODIFY event does not apear in the object's fsnotify
      mask.  This is because send_to_group() checks if FS_MODIFY needs to
      clear ignored mask of marks.
      
      The common case is that an object does not have any mark with ignored
      mask and in particular, that it does not have a mark with ignored mask
      and without the FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY flag.
      
      Set FS_MODIFY in object's fsnotify mask during fsnotify_recalc_mask()
      if object has a mark with an ignored mask and without the
      FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY flag and remove the special
      treatment of FS_MODIFY in fsnotify(), so that FS_MODIFY events could
      be optimized in the common case.
      
      Call fsnotify_recalc_mask() from fanotify after adding or removing an
      ignored mask from a mark without FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
      or when adding the FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY flag to a mark
      with ignored mask (the flag cannot be removed by fanotify uapi).
      
      Performance results for doing 10000000 write(2)s to tmpfs:
      
      				vanilla		patched
      without notification mark	25.486+-1.054	24.965+-0.244
      with notification mark		30.111+-0.139	26.891+-1.355
      
      So we can see the overhead of notification subsystem has been
      drastically reduced.
      
      Link: https://lore.kernel.org/r/20220223151438.790268-3-amir73il@gmail.com
      
      
      Suggested-by: default avatarJan Kara <jack@suse.cz>
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      04e317ba
    • Amir Goldstein's avatar
      fsnotify: fix merge with parent's ignored mask · 4f0b903d
      Amir Goldstein authored
      fsnotify_parent() does not consider the parent's mark at all unless
      the parent inode shows interest in events on children and in the
      specific event.
      
      So unless parent added an event to both its mark mask and ignored mask,
      the event will not be ignored.
      
      Fix this by declaring the interest of an object in an event when the
      event is in either a mark mask or ignored mask.
      
      Link: https://lore.kernel.org/r/20220223151438.790268-2-amir73il@gmail.com
      
      
      Signed-off-by: default avatarAmir Goldstein <amir73il@gmail.com>
      Signed-off-by: default avatarJan Kara <jack@suse.cz>
      4f0b903d
  17. Feb 01, 2022
  18. Jan 22, 2022
    • Xiaoming Ni's avatar
      inotify: simplify subdirectory registration with register_sysctl() · 7b9ad122
      Xiaoming Ni authored
      There is no need to user boiler plate code to specify a set of base
      directories we're going to stuff sysctls under.  Simplify this by using
      register_sysctl() and specifying the directory path directly.
      
      Move inotify_user sysctl to inotify_user.c while at it to remove clutter
      from kernel/sysctl.c.
      
      [mcgrof@kernel.org: remember to register fanotify_table]
        Link: https://lkml.kernel.org/r/YZ5A6iWLb0h3N3RC@bombadil.infradead.org
      [mcgrof@kernel.org: update commit log to reflect new path we decided to take]
      
      Link: https://lkml.kernel.org/r/20211123202422.819032-7-mcgrof@kernel.org
      
      
      Signed-off-by: default avatarXiaoming Ni <nixiaoming@huawei.com>
      Signed-off-by: default avatarLuis Chamberlain <mcgrof@kernel.org>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Amir Goldstein <amir73il@gmail.com>
      Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
      Cc: Antti Palosaari <crope@iki.fi>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
      Cc: Benjamin LaHaise <bcrl@kvack.org>
      Cc: Clemens Ladisch <clemens@ladisch.de>
      Cc: David Airlie <airlied@linux.ie>
      Cc: "Eric W. Biederman" <ebiederm@xmission.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Iurii Zaikin <yzaikin@google.com>
      Cc: Jani Nikula <jani.nikula@linux.intel.com>
      Cc: Jan Kara <jack@suse.cz>
      Cc: Joel Becker <jlbec@evilplan.org>
      Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
      Cc: Joseph Qi <joseph.qi@linux.alibaba.com>
      Cc: Julia Lawall <julia.lawall@inria.fr>
      Cc: Kees Cook <keescook@chromium.org>
      Cc: Lukas Middendorf <kernel@tuxforce.de>
      Cc: Mark Fasheh <mark@fasheh.com>
      Cc: Paul Turner <pjt@google.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Petr Mladek <pmladek@suse.com>
      Cc: Phillip Potter <phil@philpotter.co.uk>
      Cc: Qing Wang <wangqing@vivo.com>
      Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
      Cc: Sebastian Reichel <sre@kernel.org>
      Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
      Cc: Stephen Kitt <steve@sk2.org>
      Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
      Cc: Douglas Gilbert <dgilbert@interlog.com>
      Cc: James E.J. Bottomley <jejb@linux.ibm.com>
      Cc: Jani Nikula <jani.nikula@intel.com>
      Cc: John Ogness <john.ogness@linutronix.de>
      Cc: Martin K. Petersen <martin.petersen@oracle.com>
      Cc: "Rafael J. Wysocki" <rafael@kernel.org>
      Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: "Theodore Ts'o" <tytso@mit.edu>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      7b9ad122
    • Xiaoming Ni's avatar
      dnotify: move dnotify sysctl to dnotify.c · 49a4de75
      Xiaoming Ni authored
      The kernel/sysctl.c is a kitchen sink where everyone leaves their dirty
      dishes, this makes it very difficult to maintain.
      
      To help with this maintenance let's start by moving sysctls to places
      where they actually belong.  The proc sysctl maintainers do not want to
      know what sysctl knobs you wish to add for your own piece of code, we
      just care about the core logic.
      
      So move dnotify sysctls to dnotify.c and use the new
      register_sysctl_init() to register the sysctl interface.
      
      [mcgrof@kernel.org: adjust the commit log to justify the move]
      
      Link: https://lkml.kernel.org/r/20211123202347.818157-10-mcgrof@kernel.org
      
      
      Signed-off-by: default avatarXiaoming Ni <nixiaoming@huawei.com>
      Signed-off-by: default avatarLuis Chamberlain <mcgrof@kernel.org>
      Acked-by: default avatarJan Kara <jack@suse.cz>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Cc: Amir Goldstein <amir73il@gmail.com>
      Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
      Cc: Benjamin LaHaise <bcrl@kvack.org>
      Cc: "Eric W. Biederman" <ebiederm@xmission.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Iurii Zaikin <yzaikin@google.com>
      Cc: Kees Cook <keescook@chromium.org>
      Cc: Paul Turner <pjt@google.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Petr Mladek <pmladek@suse.com>
      Cc: Qing Wang <wangqing@vivo.com>
      Cc: Sebastian Reichel <sre@kernel.org>
      Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
      Cc: Stephen Kitt <steve@sk2.org>
      Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
      Cc: Antti Palosaari <crope@iki.fi>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
      Cc: Clemens Ladisch <clemens@ladisch.de>
      Cc: David Airlie <airlied@linux.ie>
      Cc: Jani Nikula <jani.nikula@linux.intel.com>
      Cc: Joel Becker <jlbec@evilplan.org>
      Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
      Cc: Joseph Qi <joseph.qi@linux.alibaba.com>
      Cc: Julia Lawall <julia.lawall@inria.fr>
      Cc: Lukas Middendorf <kernel@tuxforce.de>
      Cc: Mark Fasheh <mark@fasheh.com>
      Cc: Phillip Potter <phil@philpotter.co.uk>
      Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
      Cc: Douglas Gilbert <dgilbert@interlog.com>
      Cc: James E.J. Bottomley <jejb@linux.ibm.com>
      Cc: Jani Nikula <jani.nikula@intel.com>
      Cc: John Ogness <john.ogness@linutronix.de>
      Cc: Martin K. Petersen <martin.petersen@oracle.com>
      Cc: "Rafael J. Wysocki" <rafael@kernel.org>
      Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
      Cc: Suren Baghdasaryan <surenb@google.com>
      Cc: "Theodore Ts'o" <tytso@mit.edu>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      49a4de75
  19. Jan 20, 2022
  20. Dec 15, 2021
Loading