* [PATCH v9 00/19] fanotify: add pre-content hooks
@ 2024-11-21 11:21 Jan Kara
2024-11-21 11:22 ` [PATCH 01/19] fs: get rid of __FMODE_NONOTIFY kludge Jan Kara
` (19 more replies)
0 siblings, 20 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:21 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
I'm posting here the series I'm currently carrying in my tree [1]. The changes
from v8 Josef posted are not huge but big enough that I think it's worth a
repost. Unless somebody speaks up, the plan is to merge into fsnotify branch
after the merge window closes.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git/log/?h=fsnotify_hsm
v8:
https://lore.kernel.org/all/cover.1731684329.git.josef@toxicpanda.com
v7:
https://lore.kernel.org/linux-fsdevel/cover.1731433903.git.josef@toxicpanda.com/
v6:
https://lore.kernel.org/linux-fsdevel/cover.1731355931.git.josef@toxicpanda.com/
v5:
https://lore.kernel.org/linux-fsdevel/cover.1725481503.git.josef@toxicpanda.com/
v4:
https://lore.kernel.org/linux-fsdevel/cover.1723670362.git.josef@toxicpanda.com/
v3:
https://lore.kernel.org/linux-fsdevel/cover.1723228772.git.josef@toxicpanda.com/
v2:
https://lore.kernel.org/linux-fsdevel/cover.1723144881.git.josef@toxicpanda.com/
v1:
https://lore.kernel.org/linux-fsdevel/cover.1721931241.git.josef@toxicpanda.com/
v8->v9:
- fix DAX fault handling for ext4 & xfs
- rework FMODE_ constants a bit to keep FMODE_NONOTIFY a single bit
- move file_set_fsnotify_mode() out of line as it's quite big
- fold fsnotify_file_object_watched() into the single caller
- use explicit f_mode checks instead of fsnotify_file_has_pre_content_watches()
- fix compilation breakage with CONFIG_NOMMU
- fixed up some changelogs
v7->v8:
- A bunch of work from Amir to cleanup the fast path for the common case of no
watches, which cascades through the rest of th series to update the helpers
and the hooks to use the new helpers.
- A patch from Al to get rid of the __FMODE_NONOTIFY flag and cleanup the usage
there, thanks Al!
v6->v7:
- As per Linus's suggestion, Amir added the file flag FMODE_NOTIFY_PERM that
will be set at open time if the file has permission related watches (this is
the original malware style permission watches and the new precontent watches).
All of the VFS hooks and the page fault hooks use this flag to determine if
they should generate a notification to allow for a much cheaper check in the
common case.
v5->v6:
- Linus had problems with this and rejected Jan's PR
(https://lore.kernel.org/linux-fsdevel/20240923110348.tbwihs42dxxltabc@quack3/
),
so I'm respinning this series to address his concerns. Hopefully this is more
acceptable.
- Change the page fault hooks to happen only in the case where we have to add a
page, not where there exists pages already.
- Amir added a hook to truncate.
- We made the flag per SB instead of per fstype, Amir wanted this because of
some potential issues with other file system specific work he's doing.
- Dropped the bcachefs patch, there were some concerns that we were doing
something wrong, and it's not a huge deal to not have this feature for now.
- Unfortunately the xfs write fault path still has to do the page fault hook
before we know if we have a page or not, this is because of the locking that's
done before we get to the part where we know if we have a page already or not,
so that's the path that is still the same from last iteration.
- I've re-validated this series with btrfs, xfs, and ext4 to make sure I didn't
break anything.
v4->v5:
- Cleaned up the various "I'll fix it on commit" notes that Jan made since I had
to respin the series anyway.
- Renamed the filemap pagefault helper for fsnotify per Christians suggestion.
- Added a FS_ALLOW_HSM flag per Jan's comments, based on Amir's rough sketch.
- Added a patch to disable btrfs defrag on pre-content watched files.
- Added a patch to turn on FS_ALLOW_HSM for all the file systems that I tested.
- Added two fstests (which will be posted separately) to validate everything,
re-validated the series with btrfs, xfs, ext4, and bcachefs to make sure I
didn't break anything.
v3->v4:
- Trying to send a final verson Friday at 5pm before you go on vacation is a
recipe for silly mistakes, fixed the xfs handling yet again, per Christoph's
review.
- Reworked the file system helper so it's handling of fpin was a little less
silly, per Chinner's suggestion.
- Updated the return values to not or in VM_FAULT_RETRY, as we have a comment
in filemap_fault that says if VM_FAULT_ERROR is set we won't have
VM_FAULT_RETRY set.
v2->v3:
- Fix the pagefault path to do MAY_ACCESS instead, updated the perm handler to
emit PRE_ACCESS in this case, so we can avoid the extraneous perm event as per
Amir's suggestion.
- Reworked the exported helper so the per-filesystem changes are much smaller,
per Amir's suggestion.
- Fixed the screwup for DAX writes per Chinner's suggestion.
- Added Christian's reviewed-by's where appropriate.
v1->v2:
- reworked the page fault logic based on Jan's suggestion and turned it into a
helper.
- Added 3 patches per-fs where we need to call the fsnotify helper from their
->fault handlers.
- Disabled readahead in the case that there's a pre-content watch in place.
- Disabled huge faults when there's a pre-content watch in place (entirely
because it's untested, theoretically it should be straightforward to do).
- Updated the command numbers.
- Addressed the random spelling/grammer mistakes that Jan pointed out.
- Addressed the other random nits from Jan.
--- Original email ---
Hello,
These are the patches for the bare bones pre-content fanotify support. The
majority of this work is Amir's, my contribution to this has solely been around
adding the page fault hooks, testing and validating everything. I'm sending it
because Amir is traveling a bunch, and I touched it last so I'm going to take
all the hate and he can take all the credit.
There is a PoC that I've been using to validate this work, you can find the git
repo here
https://github.com/josefbacik/remote-fetch
This consists of 3 different tools.
1. populate. This just creates all the stub files in the directory from the
source directory. Just run ./populate ~/linux ~/hsm-linux and it'll
recursively create all of the stub files and directories.
2. remote-fetch. This is the actual PoC, you just point it at the source and
destination directory and then you can do whatever. ./remote-fetch ~/linux
~/hsm-linux.
3. mmap-validate. This was to validate the pagefault thing, this is likely what
will be turned into the selftest with remote-fetch. It creates a file and
then you can validate the file matches the right pattern with both normal
reads and mmap. Normally I do something like
./mmap-validate create ~/src/foo
./populate ~/src ~/dst
./rmeote-fetch ~/src ~/dst
./mmap-validate validate ~/dst/foo
I did a bunch of testing, I also got some performance numbers. I copied a
kernel tree, and then did remote-fetch, and then make -j4
Normal
real 9m49.709s
user 28m11.372s
sys 4m57.304s
HSM
real 10m6.454s
user 29m10.517s
sys 5m2.617s
So ~17 seconds more to build with HSM. I then did a make mrproper on both trees
to see the size
[root@fedora ~]# du -hs /src/linux
1.6G /src/linux
[root@fedora ~]# du -hs dst
125M dst
This mirrors the sort of savings we've seen in production.
Meta has had these patches (minus the page fault patch) deployed in production
for almost a year with our own utility for doing on-demand package fetching.
The savings from this has been pretty significant.
The page-fault hooks are necessary for the last thing we need, which is
on-demand range fetching of executables. Some of our binaries are several gigs
large, having the ability to remote fetch them on demand is a huge win for us
not only with space savings, but with startup time of containers.
There will be tests for this going into LTP once we're satisfied with the
patches and they're on their way upstream. Thanks,
Josef
Al Viro (1):
fs: get rid of __FMODE_NONOTIFY kludge
Amir Goldstein (11):
fsnotify: opt-in for permission events at file open time
fsnotify: check if file is actually being watched for pre-content events on open
fanotify: don't skip extra event info if no info_mode is set
fanotify: rename a misnamed constant
fanotify: reserve event bit of deprecated FAN_DIR_MODIFY
fsnotify: introduce pre-content permission events
fsnotify: pass optional file access range in pre-content event
fsnotify: generate pre-content permission event on truncate
fanotify: introduce FAN_PRE_ACCESS permission event
fanotify: report file range info with pre-content events
fanotify: allow to set errno in FAN_DENY permission response
Jan Kara (1):
ext4: add pre-content fsnotify hook for DAX faults
Josef Bacik (6):
fanotify: disable readahead if we have pre-content watches
mm: don't allow huge faults for files with pre content watches
fsnotify: generate pre-content permission event on page fault
xfs: add pre-content fsnotify hook for DAX faults
btrfs: disable defrag on pre-content watched files
fs: enable pre-content events on supported file systems
fs/btrfs/ioctl.c | 9 +++
fs/btrfs/super.c | 2 +-
fs/ext4/file.c | 3 +
fs/ext4/super.c | 3 +
fs/fcntl.c | 4 +-
fs/notify/fanotify/fanotify.c | 31 +++++++--
fs/notify/fanotify/fanotify.h | 15 ++++
fs/notify/fanotify/fanotify_user.c | 137 +++++++++++++++++++++++++++++--------
fs/notify/fsnotify.c | 83 +++++++++++++++++++++-
fs/open.c | 62 +++++++++++++----
fs/xfs/xfs_file.c | 14 ++++
fs/xfs/xfs_super.c | 2 +-
include/linux/fanotify.h | 18 +++--
include/linux/fs.h | 44 +++++++++---
include/linux/fsnotify.h | 78 ++++++++++++++++-----
include/linux/fsnotify_backend.h | 53 +++++++++++++-
include/linux/mm.h | 1 +
include/uapi/asm-generic/fcntl.h | 1 -
include/uapi/linux/fanotify.h | 18 +++++
mm/filemap.c | 90 ++++++++++++++++++++++++
mm/memory.c | 22 ++++++
mm/nommu.c | 7 ++
mm/readahead.c | 14 ++++
security/selinux/hooks.c | 3 +-
24 files changed, 624 insertions(+), 90 deletions(-)
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 01/19] fs: get rid of __FMODE_NONOTIFY kludge
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 02/19] fsnotify: opt-in for permission events at file open time Jan Kara
` (18 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Al Viro, Jan Kara
From: Al Viro <viro@zeniv.linux.org.uk>
All it takes to get rid of the __FMODE_NONOTIFY kludge is switching
fanotify from anon_inode_getfd() to anon_inode_getfile_fmode() and adding
a dentry_open_nonotify() helper to be used by fanotify on the other path.
That's it - no more weird shit in OPEN_FMODE(), etc.
Suggested-by: Al Viro <viro@zeniv.linux.org.uk>
Link: https://lore.kernel.org/linux-fsdevel/20241113043003.GH3387508@ZenIV/
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/d1231137e7b661a382459e79a764259509a4115d.1731684329.git.josef@toxicpanda.com
---
fs/fcntl.c | 4 ++--
fs/notify/fanotify/fanotify_user.c | 25 ++++++++++++++++---------
fs/open.c | 23 +++++++++++++++++++----
include/linux/fs.h | 6 +++---
include/uapi/asm-generic/fcntl.h | 1 -
5 files changed, 40 insertions(+), 19 deletions(-)
diff --git a/fs/fcntl.c b/fs/fcntl.c
index 22dd9dcce7ec..ebd1c82bfb6b 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -1161,10 +1161,10 @@ static int __init fcntl_init(void)
* Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
* is defined as O_NONBLOCK on some platforms and not on others.
*/
- BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
+ BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ !=
HWEIGHT32(
(VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) |
- __FMODE_EXEC | __FMODE_NONOTIFY));
+ __FMODE_EXEC));
fasync_cache = kmem_cache_create("fasync_cache",
sizeof(struct fasync_struct), 0,
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 8e2d43fc6f7c..83ee591744e9 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -101,8 +101,7 @@ static void __init fanotify_sysctls_init(void)
*
* Internal and external open flags are stored together in field f_flags of
* struct file. Only external open flags shall be allowed in event_f_flags.
- * Internal flags like FMODE_NONOTIFY, FMODE_EXEC, FMODE_NOCMTIME shall be
- * excluded.
+ * Internal flags like FMODE_EXEC shall be excluded.
*/
#define FANOTIFY_INIT_ALL_EVENT_F_BITS ( \
O_ACCMODE | O_APPEND | O_NONBLOCK | \
@@ -259,12 +258,11 @@ static int create_fd(struct fsnotify_group *group, const struct path *path,
return client_fd;
/*
- * we need a new file handle for the userspace program so it can read even if it was
- * originally opened O_WRONLY.
+ * We provide an fd for the userspace program, so it could access the
+ * file without generating fanotify events itself.
*/
- new_file = dentry_open(path,
- group->fanotify_data.f_flags | __FMODE_NONOTIFY,
- current_cred());
+ new_file = dentry_open_nonotify(path, group->fanotify_data.f_flags,
+ current_cred());
if (IS_ERR(new_file)) {
put_unused_fd(client_fd);
client_fd = PTR_ERR(new_file);
@@ -1415,6 +1413,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
unsigned int fid_mode = flags & FANOTIFY_FID_BITS;
unsigned int class = flags & FANOTIFY_CLASS_BITS;
unsigned int internal_flags = 0;
+ struct file *file;
pr_debug("%s: flags=%x event_f_flags=%x\n",
__func__, flags, event_f_flags);
@@ -1483,7 +1482,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
(!(fid_mode & FAN_REPORT_NAME) || !(fid_mode & FAN_REPORT_FID)))
return -EINVAL;
- f_flags = O_RDWR | __FMODE_NONOTIFY;
+ f_flags = O_RDWR;
if (flags & FAN_CLOEXEC)
f_flags |= O_CLOEXEC;
if (flags & FAN_NONBLOCK)
@@ -1561,10 +1560,18 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
goto out_destroy_group;
}
- fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
+ fd = get_unused_fd_flags(f_flags);
if (fd < 0)
goto out_destroy_group;
+ file = anon_inode_getfile_fmode("[fanotify]", &fanotify_fops, group,
+ f_flags, FMODE_NONOTIFY);
+ if (IS_ERR(file)) {
+ fd = PTR_ERR(file);
+ put_unused_fd(fd);
+ goto out_destroy_group;
+ }
+ fd_install(fd, file);
return fd;
out_destroy_group:
diff --git a/fs/open.c b/fs/open.c
index 6c4950f19cfb..480c3798da2a 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -1122,6 +1122,23 @@ struct file *dentry_open(const struct path *path, int flags,
}
EXPORT_SYMBOL(dentry_open);
+struct file *dentry_open_nonotify(const struct path *path, int flags,
+ const struct cred *cred)
+{
+ struct file *f = alloc_empty_file(flags, cred);
+ if (!IS_ERR(f)) {
+ int error;
+
+ f->f_mode |= FMODE_NONOTIFY;
+ error = vfs_open(path, f);
+ if (error) {
+ fput(f);
+ f = ERR_PTR(error);
+ }
+ }
+ return f;
+}
+
/**
* dentry_create - Create and open a file
* @path: path to create
@@ -1219,7 +1236,7 @@ inline struct open_how build_open_how(int flags, umode_t mode)
inline int build_open_flags(const struct open_how *how, struct open_flags *op)
{
u64 flags = how->flags;
- u64 strip = __FMODE_NONOTIFY | O_CLOEXEC;
+ u64 strip = O_CLOEXEC;
int lookup_flags = 0;
int acc_mode = ACC_MODE(flags);
@@ -1227,9 +1244,7 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)
"struct open_flags doesn't yet handle flags > 32 bits");
/*
- * Strip flags that either shouldn't be set by userspace like
- * FMODE_NONOTIFY or that aren't relevant in determining struct
- * open_flags like O_CLOEXEC.
+ * Strip flags that aren't relevant in determining struct open_flags.
*/
flags &= ~strip;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 3559446279c1..24598d707578 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2728,6 +2728,8 @@ static inline struct file *file_open_root_mnt(struct vfsmount *mnt,
}
struct file *dentry_open(const struct path *path, int flags,
const struct cred *creds);
+struct file *dentry_open_nonotify(const struct path *path, int flags,
+ const struct cred *cred);
struct file *dentry_create(const struct path *path, int flags, umode_t mode,
const struct cred *cred);
struct path *backing_file_user_path(struct file *f);
@@ -3625,11 +3627,9 @@ struct ctl_table;
int __init list_bdev_fs_names(char *buf, size_t size);
#define __FMODE_EXEC ((__force int) FMODE_EXEC)
-#define __FMODE_NONOTIFY ((__force int) FMODE_NONOTIFY)
#define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
-#define OPEN_FMODE(flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE) | \
- (flag & __FMODE_NONOTIFY)))
+#define OPEN_FMODE(flag) ((__force fmode_t)((flag + 1) & O_ACCMODE))
static inline bool is_sxid(umode_t mode)
{
diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h
index 80f37a0d40d7..613475285643 100644
--- a/include/uapi/asm-generic/fcntl.h
+++ b/include/uapi/asm-generic/fcntl.h
@@ -6,7 +6,6 @@
/*
* FMODE_EXEC is 0x20
- * FMODE_NONOTIFY is 0x4000000
* These cannot be used by userspace O_* until internal and external open
* flags are split.
* -Eric Paris
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 02/19] fsnotify: opt-in for permission events at file open time
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
2024-11-21 11:22 ` [PATCH 01/19] fs: get rid of __FMODE_NONOTIFY kludge Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open Jan Kara
` (17 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
Legacy inotify/fanotify listeners can add watches for events on inode,
parent or mount and expect to get events (e.g. FS_MODIFY) on files that
were already open at the time of setting up the watches.
fanotify permission events are typically used by Anti-malware sofware,
that is watching the entire mount and it is not common to have more that
one Anti-malware engine installed on a system.
To reduce the overhead of the fsnotify_file_perm() hooks on every file
access, relax the semantics of the legacy FAN_ACCESS_PERM event to generate
events only if there were *any* permission event listeners on the
filesystem at the time that the file was opened.
The new semantic is implemented by extending the FMODE_NONOTIFY bit into
two FMODE_NONOTIFY_* bits, that are used to store a mode for which of the
events types to report.
This is going to apply to the new fanotify pre-content events in order
to reduce the cost of the new pre-content event vfs hooks.
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://lore.kernel.org/linux-fsdevel/CAHk-=wj8L=mtcRTi=NECHMGfZQgXOp_uix1YVh04fEmrKaMnXA@mail.gmail.com/
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/5ea5f8e283d1edb55aa79c35187bfe344056af14.1731684329.git.josef@toxicpanda.com
---
fs/notify/fsnotify.c | 38 ++++++++++++++++++++++++++++++++++++++
fs/open.c | 8 +++++++-
include/linux/fs.h | 37 ++++++++++++++++++++++++++++++++-----
include/linux/fsnotify.h | 39 +++++++++++++++++++++++----------------
4 files changed, 100 insertions(+), 22 deletions(-)
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index f976949d2634..569ec356e4ce 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -623,6 +623,44 @@ int fsnotify(__u32 mask, const void *data, int data_type, struct inode *dir,
}
EXPORT_SYMBOL_GPL(fsnotify);
+#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
+/*
+ * At open time we check fsnotify_sb_has_priority_watchers() and set the
+ * FMODE_NONOTIFY_ mode bits accordignly.
+ * Later, fsnotify permission hooks do not check if there are permission event
+ * watches, but that there were permission event watches at open time.
+ */
+void file_set_fsnotify_mode(struct file *file)
+{
+ struct super_block *sb = file->f_path.dentry->d_sb;
+
+ /* Is it a file opened by fanotify? */
+ if (FMODE_FSNOTIFY_NONE(file->f_mode))
+ return;
+
+ /*
+ * Permission events is a super set of pre-content events, so if there
+ * are no permission event watchers, there are also no pre-content event
+ * watchers and this is implied from the single FMODE_NONOTIFY_PERM bit.
+ */
+ if (likely(!fsnotify_sb_has_priority_watchers(sb,
+ FSNOTIFY_PRIO_CONTENT))) {
+ file->f_mode |= FMODE_NONOTIFY_PERM;
+ return;
+ }
+
+ /*
+ * If there are permission event watchers but no pre-content event
+ * watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that.
+ */
+ if (likely(!fsnotify_sb_has_priority_watchers(sb,
+ FSNOTIFY_PRIO_PRE_CONTENT))) {
+ file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
+ return;
+ }
+}
+#endif
+
static __init int fsnotify_init(void)
{
int ret;
diff --git a/fs/open.c b/fs/open.c
index 480c3798da2a..9b1fa59d52bf 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -918,7 +918,7 @@ static int do_dentry_open(struct file *f,
f->f_sb_err = file_sample_sb_err(f);
if (unlikely(f->f_flags & O_PATH)) {
- f->f_mode = FMODE_PATH | FMODE_OPENED;
+ f->f_mode = FMODE_PATH | FMODE_OPENED | FMODE_NONOTIFY;
f->f_op = &empty_fops;
return 0;
}
@@ -946,6 +946,12 @@ static int do_dentry_open(struct file *f,
if (error)
goto cleanup_all;
+ /*
+ * Set FMODE_NONOTIFY_* bits according to existing permission watches.
+ * If FMODE_NONOTIFY was already set for an fanotify fd, this doesn't
+ * change anything.
+ */
+ file_set_fsnotify_mode(f);
error = fsnotify_open_perm(f);
if (error)
goto cleanup_all;
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 24598d707578..6a170c2c5326 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -171,13 +171,20 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
#define FMODE_NOREUSE ((__force fmode_t)(1 << 23))
-/* FMODE_* bit 24 */
-
/* File is embedded in backing_file object */
-#define FMODE_BACKING ((__force fmode_t)(1 << 25))
+#define FMODE_BACKING ((__force fmode_t)(1 << 24))
+
+/*
+ * Together with FMODE_NONOTIFY_PERM defines which fsnotify events shouldn't be
+ * generated (see below)
+ */
+#define FMODE_NONOTIFY ((__force fmode_t)(1 << 25))
-/* File was opened by fanotify and shouldn't generate fanotify events */
-#define FMODE_NONOTIFY ((__force fmode_t)(1 << 26))
+/*
+ * Together with FMODE_NONOTIFY defines which fsnotify events shouldn't be
+ * generated (see below)
+ */
+#define FMODE_NONOTIFY_PERM ((__force fmode_t)(1 << 26))
/* File is capable of returning -EAGAIN if I/O will block */
#define FMODE_NOWAIT ((__force fmode_t)(1 << 27))
@@ -188,6 +195,26 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
/* File does not contribute to nr_files count */
#define FMODE_NOACCOUNT ((__force fmode_t)(1 << 29))
+/*
+ * The two FMODE_NONOTIFY* define which fsnotify events should not be generated
+ * for a file. These are the possible values of (f->f_mode &
+ * FMODE_FSNOTIFY_MASK) and their meaning:
+ *
+ * FMODE_NONOTIFY - suppress all (incl. non-permission) events.
+ * FMODE_NONOTIFY_PERM - suppress permission (incl. pre-content) events.
+ * FMODE_NONOTIFY | FMODE_NONOTIFY_PERM - suppress only pre-content events.
+ */
+#define FMODE_FSNOTIFY_MASK \
+ (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM)
+
+#define FMODE_FSNOTIFY_NONE(mode) \
+ ((mode & FMODE_FSNOTIFY_MASK) == FMODE_NONOTIFY)
+#define FMODE_FSNOTIFY_PERM(mode) \
+ ((mode & FMODE_FSNOTIFY_MASK) == 0 || \
+ (mode & FMODE_FSNOTIFY_MASK) == (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM))
+#define FMODE_FSNOTIFY_HSM(mode) \
+ ((mode & FMODE_FSNOTIFY_MASK) == 0)
+
/*
* Attribute flags. These should be or-ed together to figure out what
* has been changed!
diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h
index 278620e063ab..8d1849137a96 100644
--- a/include/linux/fsnotify.h
+++ b/include/linux/fsnotify.h
@@ -108,38 +108,35 @@ static inline void fsnotify_dentry(struct dentry *dentry, __u32 mask)
fsnotify_parent(dentry, mask, dentry, FSNOTIFY_EVENT_DENTRY);
}
-static inline int fsnotify_file(struct file *file, __u32 mask)
+static inline int fsnotify_path(const struct path *path, __u32 mask)
{
- const struct path *path;
+ return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
+}
+static inline int fsnotify_file(struct file *file, __u32 mask)
+{
/*
* FMODE_NONOTIFY are fds generated by fanotify itself which should not
* generate new events. We also don't want to generate events for
* FMODE_PATH fds (involves open & close events) as they are just
* handle creation / destruction events and not "real" file events.
*/
- if (file->f_mode & (FMODE_NONOTIFY | FMODE_PATH))
+ if (FMODE_FSNOTIFY_NONE(file->f_mode))
return 0;
- path = &file->f_path;
- /* Permission events require group prio >= FSNOTIFY_PRIO_CONTENT */
- if (mask & ALL_FSNOTIFY_PERM_EVENTS &&
- !fsnotify_sb_has_priority_watchers(path->dentry->d_sb,
- FSNOTIFY_PRIO_CONTENT))
- return 0;
-
- return fsnotify_parent(path->dentry, mask, path, FSNOTIFY_EVENT_PATH);
+ return fsnotify_path(&file->f_path, mask);
}
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
+
+void file_set_fsnotify_mode(struct file *file);
+
/*
* fsnotify_file_area_perm - permission hook before access to file range
*/
static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
const loff_t *ppos, size_t count)
{
- __u32 fsnotify_mask = FS_ACCESS_PERM;
-
/*
* filesystem may be modified in the context of permission events
* (e.g. by HSM filling a file on access), so sb freeze protection
@@ -150,7 +147,10 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
if (!(perm_mask & MAY_READ))
return 0;
- return fsnotify_file(file, fsnotify_mask);
+ if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode)))
+ return 0;
+
+ return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
}
/*
@@ -168,16 +168,23 @@ static inline int fsnotify_open_perm(struct file *file)
{
int ret;
+ if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode)))
+ return 0;
+
if (file->f_flags & __FMODE_EXEC) {
- ret = fsnotify_file(file, FS_OPEN_EXEC_PERM);
+ ret = fsnotify_path(&file->f_path, FS_OPEN_EXEC_PERM);
if (ret)
return ret;
}
- return fsnotify_file(file, FS_OPEN_PERM);
+ return fsnotify_path(&file->f_path, FS_OPEN_PERM);
}
#else
+static inline void file_set_fsnotify_mode(struct file *file)
+{
+}
+
static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
const loff_t *ppos, size_t count)
{
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
2024-11-21 11:22 ` [PATCH 01/19] fs: get rid of __FMODE_NONOTIFY kludge Jan Kara
2024-11-21 11:22 ` [PATCH 02/19] fsnotify: opt-in for permission events at file open time Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 16:01 ` Amir Goldstein
2024-11-21 11:22 ` [PATCH 04/19] fanotify: don't skip extra event info if no info_mode is set Jan Kara
` (16 subsequent siblings)
19 siblings, 1 reply; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
So far, we set FMODE_NONOTIFY_ flags at open time if we know that there
are no permission event watchers at all on the filesystem, but lack of
FMODE_NONOTIFY_ flags does not mean that the file is actually watched.
For pre-content events, it is possible to optimize things so that we
don't bother trying to send pre-content events if file was not watched
(through sb, mnt, parent or inode itself) on open. Set FMODE_NONOTIFY_
flags according to that.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/2ddcc9f8d1fde48d085318a6b5a889289d8871d8.1731684329.git.josef@toxicpanda.com
---
fs/notify/fsnotify.c | 27 +++++++++++++++++++++++++--
include/linux/fsnotify_backend.h | 3 +++
2 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index 569ec356e4ce..dd1dffd89fd6 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -193,7 +193,7 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,
return mask & marks_mask;
}
-/* Are there any inode/mount/sb objects that are interested in this event? */
+/* Are there any inode/mount/sb objects that watch for these events? */
static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
__u32 mask)
{
@@ -632,7 +632,9 @@ EXPORT_SYMBOL_GPL(fsnotify);
*/
void file_set_fsnotify_mode(struct file *file)
{
- struct super_block *sb = file->f_path.dentry->d_sb;
+ struct dentry *dentry = file->f_path.dentry, *parent;
+ struct super_block *sb = dentry->d_sb;
+ __u32 mnt_mask, p_mask;
/* Is it a file opened by fanotify? */
if (FMODE_FSNOTIFY_NONE(file->f_mode))
@@ -658,6 +660,27 @@ void file_set_fsnotify_mode(struct file *file)
file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
return;
}
+
+ /*
+ * OK, there are some pre-content watchers. Check if anybody can be
+ * watching for pre-content events on *this* file.
+ */
+ mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
+ if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) &&
+ !fsnotify_object_watched(d_inode(dentry), mnt_mask,
+ FSNOTIFY_PRE_CONTENT_EVENTS))) {
+ file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
+ return;
+ }
+
+ /* Even parent is not watching for pre-content events on this file? */
+ parent = dget_parent(dentry);
+ p_mask = fsnotify_inode_watches_children(d_inode(parent));
+ dput(parent);
+ if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
+ file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
+ return;
+ }
}
#endif
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index 3ecf7768e577..9c105244815d 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -77,6 +77,9 @@
*/
#define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
+/* Pre-content events can be used to fill file content */
+#define FSNOTIFY_PRE_CONTENT_EVENTS 0
+
#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
FS_OPEN_EXEC_PERM)
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 04/19] fanotify: don't skip extra event info if no info_mode is set
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (2 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 05/19] fanotify: rename a misnamed constant Jan Kara
` (15 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
Previously we would only include optional information if you requested
it via an FAN_ flag at fanotify_init time (FAN_REPORT_FID for example).
However this isn't necessary as the event length is encoded in the
metadata, and if the user doesn't want to consume the information they
don't have to. With the PRE_ACCESS events we will always generate range
information, so drop this check in order to allow this extra
information to be exported without needing to have another flag.
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/afcbc4e4139dee076ef1757918b037d3b48c3edb.1731684329.git.josef@toxicpanda.com
---
fs/notify/fanotify/fanotify_user.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 83ee591744e9..8dcd46fe6964 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -159,9 +159,6 @@ static size_t fanotify_event_len(unsigned int info_mode,
int fh_len;
int dot_len = 0;
- if (!info_mode)
- return event_len;
-
if (fanotify_is_error_event(event->mask))
event_len += FANOTIFY_ERROR_INFO_LEN;
@@ -755,12 +752,10 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
buf += FAN_EVENT_METADATA_LEN;
count -= FAN_EVENT_METADATA_LEN;
- if (info_mode) {
- ret = copy_info_records_to_user(event, info, info_mode, pidfd,
- buf, count);
- if (ret < 0)
- goto out_close_fd;
- }
+ ret = copy_info_records_to_user(event, info, info_mode, pidfd,
+ buf, count);
+ if (ret < 0)
+ goto out_close_fd;
if (f)
fd_install(fd, f);
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 05/19] fanotify: rename a misnamed constant
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (3 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 04/19] fanotify: don't skip extra event info if no info_mode is set Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 06/19] fanotify: reserve event bit of deprecated FAN_DIR_MODIFY Jan Kara
` (14 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
FANOTIFY_PIDFD_INFO_HDR_LEN is not the length of the header.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/8776ab90fe538225aeb561c560296bafd16b97c4.1731684329.git.josef@toxicpanda.com
---
fs/notify/fanotify/fanotify_user.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 8dcd46fe6964..8d8afa09560b 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -118,7 +118,7 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init;
#define FANOTIFY_EVENT_ALIGN 4
#define FANOTIFY_FID_INFO_HDR_LEN \
(sizeof(struct fanotify_event_info_fid) + sizeof(struct file_handle))
-#define FANOTIFY_PIDFD_INFO_HDR_LEN \
+#define FANOTIFY_PIDFD_INFO_LEN \
sizeof(struct fanotify_event_info_pidfd)
#define FANOTIFY_ERROR_INFO_LEN \
(sizeof(struct fanotify_event_info_error))
@@ -173,14 +173,14 @@ static size_t fanotify_event_len(unsigned int info_mode,
dot_len = 1;
}
- if (info_mode & FAN_REPORT_PIDFD)
- event_len += FANOTIFY_PIDFD_INFO_HDR_LEN;
-
if (fanotify_event_has_object_fh(event)) {
fh_len = fanotify_event_object_fh_len(event);
event_len += fanotify_fid_info_len(fh_len, dot_len);
}
+ if (info_mode & FAN_REPORT_PIDFD)
+ event_len += FANOTIFY_PIDFD_INFO_LEN;
+
return event_len;
}
@@ -502,7 +502,7 @@ static int copy_pidfd_info_to_user(int pidfd,
size_t count)
{
struct fanotify_event_info_pidfd info = { };
- size_t info_len = FANOTIFY_PIDFD_INFO_HDR_LEN;
+ size_t info_len = FANOTIFY_PIDFD_INFO_LEN;
if (WARN_ON_ONCE(info_len > count))
return -EFAULT;
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 06/19] fanotify: reserve event bit of deprecated FAN_DIR_MODIFY
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (4 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 05/19] fanotify: rename a misnamed constant Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 07/19] fsnotify: introduce pre-content permission events Jan Kara
` (13 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
Avoid reusing it, because we would like to reserve it for future
FAN_PATH_MODIFY pre-content event.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/632d9f80428e2e7a6b6a8ccc2925d87c92bbb518.1731684329.git.josef@toxicpanda.com
---
include/linux/fsnotify_backend.h | 1 +
include/uapi/linux/fanotify.h | 1 +
2 files changed, 2 insertions(+)
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index 9c105244815d..c38762b62bf1 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -55,6 +55,7 @@
#define FS_OPEN_PERM 0x00010000 /* open event in an permission hook */
#define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */
#define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */
+/* #define FS_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */
/*
* Set on inode mark that cares about things that happen to its children.
diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h
index 34f221d3a1b9..79072b6894f2 100644
--- a/include/uapi/linux/fanotify.h
+++ b/include/uapi/linux/fanotify.h
@@ -25,6 +25,7 @@
#define FAN_OPEN_PERM 0x00010000 /* File open in perm check */
#define FAN_ACCESS_PERM 0x00020000 /* File accessed in perm check */
#define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */
+/* #define FAN_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */
#define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 07/19] fsnotify: introduce pre-content permission events
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (5 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 06/19] fanotify: reserve event bit of deprecated FAN_DIR_MODIFY Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 08/19] fsnotify: pass optional file access range in pre-content event Jan Kara
` (12 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
The new FS_PRE_ACCESS permission event is similar to FS_ACCESS_PERM,
but it meant for a different use case of filling file content before
access to a file range, so it has slightly different semantics.
Generate FS_PRE_ACCESS/FS_ACCESS_PERM as two seperate events, so content
scanners could inspect the content filled by pre-content event handler.
Unlike FS_ACCESS_PERM, FS_PRE_ACCESS is also called before a file is
modified by syscalls as write() and fallocate().
FS_ACCESS_PERM is reported also on blockdev and pipes, but the new
pre-content events are only reported for regular files and dirs.
The pre-content events are meant to be used by hierarchical storage
managers that want to fill the content of files on first access.
There are some specific requirements from filesystems that could
be used with pre-content events, so add a flag for fs to opt-in
for pre-content events explicitly before they can be used.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/b934c5e3af205abc4e0e4709f6486815937ddfdf.1731684329.git.josef@toxicpanda.com
---
fs/notify/fsnotify.c | 2 +-
include/linux/fs.h | 1 +
include/linux/fsnotify.h | 19 ++++++++++++++++++-
include/linux/fsnotify_backend.h | 11 ++++++++---
security/selinux/hooks.c | 3 ++-
5 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index dd1dffd89fd6..d61f6bc679f1 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -688,7 +688,7 @@ static __init int fsnotify_init(void)
{
int ret;
- BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 23);
+ BUILD_BUG_ON(HWEIGHT32(ALL_FSNOTIFY_BITS) != 24);
ret = init_srcu_struct(&fsnotify_mark_srcu);
if (ret)
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 6a170c2c5326..d9bccec6cc4d 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -1257,6 +1257,7 @@ extern int send_sigurg(struct file *file);
#define SB_I_RETIRED 0x00000800 /* superblock shouldn't be reused */
#define SB_I_NOUMASK 0x00001000 /* VFS does not apply umask */
#define SB_I_NOIDMAP 0x00002000 /* No idmapped mounts on this superblock */
+#define SB_I_ALLOW_HSM 0x00004000 /* Allow HSM events on this superblock */
/* Possible states of 'frozen' field */
enum {
diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h
index 8d1849137a96..d91aa064f0e4 100644
--- a/include/linux/fsnotify.h
+++ b/include/linux/fsnotify.h
@@ -144,12 +144,29 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
*/
lockdep_assert_once(file_write_not_started(file));
- if (!(perm_mask & MAY_READ))
+ if (!(perm_mask & (MAY_READ | MAY_WRITE | MAY_ACCESS)))
return 0;
if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode)))
return 0;
+ /*
+ * read()/write() and other types of access generate pre-content events.
+ */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
+ int ret = fsnotify_path(&file->f_path, FS_PRE_ACCESS);
+
+ if (ret)
+ return ret;
+ }
+
+ if (!(perm_mask & MAY_READ))
+ return 0;
+
+ /*
+ * read() also generates the legacy FS_ACCESS_PERM event, so content
+ * scanners can inspect the content filled by pre-content event.
+ */
return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
}
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index c38762b62bf1..9bda354b5538 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -57,6 +57,8 @@
#define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */
/* #define FS_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */
+#define FS_PRE_ACCESS 0x00100000 /* Pre-content access hook */
+
/*
* Set on inode mark that cares about things that happen to its children.
* Always set for dnotify and inotify.
@@ -78,11 +80,14 @@
*/
#define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
+/* Content events can be used to inspect file content */
+#define FSNOTIFY_CONTENT_PERM_EVENTS (FS_OPEN_PERM | FS_OPEN_EXEC_PERM | \
+ FS_ACCESS_PERM)
/* Pre-content events can be used to fill file content */
-#define FSNOTIFY_PRE_CONTENT_EVENTS 0
+#define FSNOTIFY_PRE_CONTENT_EVENTS (FS_PRE_ACCESS)
-#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
- FS_OPEN_EXEC_PERM)
+#define ALL_FSNOTIFY_PERM_EVENTS (FSNOTIFY_CONTENT_PERM_EVENTS | \
+ FSNOTIFY_PRE_CONTENT_EVENTS)
/*
* This is a list of all events that may get sent to a parent that is watching
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index fc926d3cac6e..c6f38705c715 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -3404,7 +3404,8 @@ static int selinux_path_notify(const struct path *path, u64 mask,
perm |= FILE__WATCH_WITH_PERM;
/* watches on read-like events need the file:watch_reads permission */
- if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
+ if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_PRE_ACCESS |
+ FS_CLOSE_NOWRITE))
perm |= FILE__WATCH_READS;
return path_has_perm(current_cred(), path, perm);
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 08/19] fsnotify: pass optional file access range in pre-content event
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (6 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 07/19] fsnotify: introduce pre-content permission events Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 09/19] fsnotify: generate pre-content permission event on truncate Jan Kara
` (11 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
We would like to add file range information to pre-content events.
Pass a struct file_range with offset and length to event handler
along with pre-content permission event.
The offset and length are aligned to page size, but we may need to
align them to minimum folio size for filesystems with large block size.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/88eddee301231d814aede27fb4d5b41ae37c9702.1731684329.git.josef@toxicpanda.com
---
fs/notify/fanotify/fanotify.c | 11 +++++++--
fs/notify/fanotify/fanotify.h | 2 ++
fs/notify/fsnotify.c | 18 ++++++++++++++
include/linux/fsnotify.h | 4 ++--
include/linux/fsnotify_backend.h | 40 ++++++++++++++++++++++++++++++++
5 files changed, 71 insertions(+), 4 deletions(-)
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index 224bccaab4cc..c1e4ae221093 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -549,9 +549,13 @@ static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
return &pevent->fae;
}
-static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
+static struct fanotify_event *fanotify_alloc_perm_event(const void *data,
+ int data_type,
gfp_t gfp)
{
+ const struct path *path = fsnotify_data_path(data, data_type);
+ const struct file_range *range =
+ fsnotify_data_file_range(data, data_type);
struct fanotify_perm_event *pevent;
pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
@@ -565,6 +569,9 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
pevent->hdr.len = 0;
pevent->state = FAN_EVENT_INIT;
pevent->path = *path;
+ /* NULL ppos means no range info */
+ pevent->ppos = range ? &range->pos : NULL;
+ pevent->count = range ? range->count : 0;
path_get(path);
return &pevent->fae;
@@ -802,7 +809,7 @@ static struct fanotify_event *fanotify_alloc_event(
old_memcg = set_active_memcg(group->memcg);
if (fanotify_is_perm_event(mask)) {
- event = fanotify_alloc_perm_event(path, gfp);
+ event = fanotify_alloc_perm_event(data, data_type, gfp);
} else if (fanotify_is_error_event(mask)) {
event = fanotify_alloc_error_event(group, fsid, data,
data_type, &hash);
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index e5ab33cae6a7..93598b7d5952 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -425,6 +425,8 @@ FANOTIFY_PE(struct fanotify_event *event)
struct fanotify_perm_event {
struct fanotify_event fae;
struct path path;
+ const loff_t *ppos; /* optional file range info */
+ size_t count;
u32 response; /* userspace answer to the event */
unsigned short state; /* state of the event */
int fd; /* fd we passed to userspace for this event */
diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
index d61f6bc679f1..e8625b61100a 100644
--- a/fs/notify/fsnotify.c
+++ b/fs/notify/fsnotify.c
@@ -203,6 +203,24 @@ static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
return mask & marks_mask & ALL_FSNOTIFY_EVENTS;
}
+/* Report pre-content event with optional range info */
+int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
+ size_t count)
+{
+ struct file_range range;
+
+ /* Report page aligned range only when pos is known */
+ if (!ppos)
+ return fsnotify_path(path, FS_PRE_ACCESS);
+
+ range.path = path;
+ range.pos = PAGE_ALIGN_DOWN(*ppos);
+ range.count = PAGE_ALIGN(*ppos + count) - range.pos;
+
+ return fsnotify_parent(path->dentry, FS_PRE_ACCESS, &range,
+ FSNOTIFY_EVENT_FILE_RANGE);
+}
+
/*
* Notify this dentry's parent about a child's events with child name info
* if parent is watching or if inode/sb/mount are interested in events with
diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h
index d91aa064f0e4..87044acf8e79 100644
--- a/include/linux/fsnotify.h
+++ b/include/linux/fsnotify.h
@@ -154,7 +154,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
* read()/write() and other types of access generate pre-content events.
*/
if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
- int ret = fsnotify_path(&file->f_path, FS_PRE_ACCESS);
+ int ret = fsnotify_pre_content(&file->f_path, ppos, count);
if (ret)
return ret;
@@ -171,7 +171,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
}
/*
- * fsnotify_file_perm - permission hook before file access
+ * fsnotify_file_perm - permission hook before file access (unknown range)
*/
static inline int fsnotify_file_perm(struct file *file, int perm_mask)
{
diff --git a/include/linux/fsnotify_backend.h b/include/linux/fsnotify_backend.h
index 9bda354b5538..0d24a21a8e60 100644
--- a/include/linux/fsnotify_backend.h
+++ b/include/linux/fsnotify_backend.h
@@ -294,6 +294,7 @@ static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
/* When calling fsnotify tell it if the data is a path or inode */
enum fsnotify_data_type {
FSNOTIFY_EVENT_NONE,
+ FSNOTIFY_EVENT_FILE_RANGE,
FSNOTIFY_EVENT_PATH,
FSNOTIFY_EVENT_INODE,
FSNOTIFY_EVENT_DENTRY,
@@ -306,6 +307,17 @@ struct fs_error_report {
struct super_block *sb;
};
+struct file_range {
+ const struct path *path;
+ loff_t pos;
+ size_t count;
+};
+
+static inline const struct path *file_range_path(const struct file_range *range)
+{
+ return range->path;
+}
+
static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
{
switch (data_type) {
@@ -315,6 +327,8 @@ static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
return d_inode(data);
case FSNOTIFY_EVENT_PATH:
return d_inode(((const struct path *)data)->dentry);
+ case FSNOTIFY_EVENT_FILE_RANGE:
+ return d_inode(file_range_path(data)->dentry);
case FSNOTIFY_EVENT_ERROR:
return ((struct fs_error_report *)data)->inode;
default:
@@ -330,6 +344,8 @@ static inline struct dentry *fsnotify_data_dentry(const void *data, int data_typ
return (struct dentry *)data;
case FSNOTIFY_EVENT_PATH:
return ((const struct path *)data)->dentry;
+ case FSNOTIFY_EVENT_FILE_RANGE:
+ return file_range_path(data)->dentry;
default:
return NULL;
}
@@ -341,6 +357,8 @@ static inline const struct path *fsnotify_data_path(const void *data,
switch (data_type) {
case FSNOTIFY_EVENT_PATH:
return data;
+ case FSNOTIFY_EVENT_FILE_RANGE:
+ return file_range_path(data);
default:
return NULL;
}
@@ -356,6 +374,8 @@ static inline struct super_block *fsnotify_data_sb(const void *data,
return ((struct dentry *)data)->d_sb;
case FSNOTIFY_EVENT_PATH:
return ((const struct path *)data)->dentry->d_sb;
+ case FSNOTIFY_EVENT_FILE_RANGE:
+ return file_range_path(data)->dentry->d_sb;
case FSNOTIFY_EVENT_ERROR:
return ((struct fs_error_report *) data)->sb;
default:
@@ -375,6 +395,18 @@ static inline struct fs_error_report *fsnotify_data_error_report(
}
}
+static inline const struct file_range *fsnotify_data_file_range(
+ const void *data,
+ int data_type)
+{
+ switch (data_type) {
+ case FSNOTIFY_EVENT_FILE_RANGE:
+ return (struct file_range *)data;
+ default:
+ return NULL;
+ }
+}
+
/*
* Index to merged marks iterator array that correlates to a type of watch.
* The type of watched object can be deduced from the iterator type, but not
@@ -863,9 +895,17 @@ static inline void fsnotify_init_event(struct fsnotify_event *event)
{
INIT_LIST_HEAD(&event->list);
}
+int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
+ size_t count);
#else
+static inline int fsnotify_pre_content(const struct path *path,
+ const loff_t *ppos, size_t count)
+{
+ return 0;
+}
+
static inline int fsnotify(__u32 mask, const void *data, int data_type,
struct inode *dir, const struct qstr *name,
struct inode *inode, u32 cookie)
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 09/19] fsnotify: generate pre-content permission event on truncate
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (7 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 08/19] fsnotify: pass optional file access range in pre-content event Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 10/19] fanotify: introduce FAN_PRE_ACCESS permission event Jan Kara
` (10 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
Generate FS_PRE_ACCESS event before truncate, without sb_writers held.
Move the security hooks also before sb_start_write() to conform with
other security hooks (e.g. in write, fallocate).
The event will have a range info of the page surrounding the new size
to provide an opportunity to fill the conetnt at the end of file before
truncating to non-page aligned size.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/23af8201db6ac2efdea94f09ab067d81ba5de7a7.1731684329.git.josef@toxicpanda.com
---
fs/open.c | 31 +++++++++++++++++++++----------
include/linux/fsnotify.h | 20 ++++++++++++++++++++
2 files changed, 41 insertions(+), 10 deletions(-)
diff --git a/fs/open.c b/fs/open.c
index 9b1fa59d52bf..f7bd176545f5 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -81,14 +81,18 @@ long vfs_truncate(const struct path *path, loff_t length)
if (!S_ISREG(inode->i_mode))
return -EINVAL;
- error = mnt_want_write(path->mnt);
- if (error)
- goto out;
-
idmap = mnt_idmap(path->mnt);
error = inode_permission(idmap, inode, MAY_WRITE);
if (error)
- goto mnt_drop_write_and_out;
+ return error;
+
+ error = fsnotify_truncate_perm(path, length);
+ if (error)
+ return error;
+
+ error = mnt_want_write(path->mnt);
+ if (error)
+ return error;
error = -EPERM;
if (IS_APPEND(inode))
@@ -114,7 +118,7 @@ long vfs_truncate(const struct path *path, loff_t length)
put_write_access(inode);
mnt_drop_write_and_out:
mnt_drop_write(path->mnt);
-out:
+
return error;
}
EXPORT_SYMBOL_GPL(vfs_truncate);
@@ -175,11 +179,18 @@ long do_ftruncate(struct file *file, loff_t length, int small)
/* Check IS_APPEND on real upper inode */
if (IS_APPEND(file_inode(file)))
return -EPERM;
- sb_start_write(inode->i_sb);
+
error = security_file_truncate(file);
- if (!error)
- error = do_truncate(file_mnt_idmap(file), dentry, length,
- ATTR_MTIME | ATTR_CTIME, file);
+ if (error)
+ return error;
+
+ error = fsnotify_truncate_perm(&file->f_path, length);
+ if (error)
+ return error;
+
+ sb_start_write(inode->i_sb);
+ error = do_truncate(file_mnt_idmap(file), dentry, length,
+ ATTR_MTIME | ATTR_CTIME, file);
sb_end_write(inode->i_sb);
return error;
diff --git a/include/linux/fsnotify.h b/include/linux/fsnotify.h
index 87044acf8e79..1a9ef8f6784d 100644
--- a/include/linux/fsnotify.h
+++ b/include/linux/fsnotify.h
@@ -170,6 +170,21 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
}
+/*
+ * fsnotify_truncate_perm - permission hook before file truncate
+ */
+static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
+{
+ struct inode *inode = d_inode(path->dentry);
+
+ if (!(inode->i_sb->s_iflags & SB_I_ALLOW_HSM) ||
+ !fsnotify_sb_has_priority_watchers(inode->i_sb,
+ FSNOTIFY_PRIO_PRE_CONTENT))
+ return 0;
+
+ return fsnotify_pre_content(path, &length, 0);
+}
+
/*
* fsnotify_file_perm - permission hook before file access (unknown range)
*/
@@ -208,6 +223,11 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
return 0;
}
+static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
+{
+ return 0;
+}
+
static inline int fsnotify_file_perm(struct file *file, int perm_mask)
{
return 0;
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 10/19] fanotify: introduce FAN_PRE_ACCESS permission event
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (8 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 09/19] fsnotify: generate pre-content permission event on truncate Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 11/19] fanotify: report file range info with pre-content events Jan Kara
` (9 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
Similar to FAN_ACCESS_PERM permission event, but it is only allowed with
class FAN_CLASS_PRE_CONTENT and only allowed on regular files and dirs.
Unlike FAN_ACCESS_PERM, it is safe to write to the file being accessed
in the context of the event handler.
This pre-content event is meant to be used by hierarchical storage
managers that want to fill the content of files on first read access.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/b80986f8d5b860acea2c9a73c0acd93587be5fe4.1731684329.git.josef@toxicpanda.com
---
fs/notify/fanotify/fanotify.c | 3 ++-
fs/notify/fanotify/fanotify_user.c | 22 +++++++++++++++++++---
include/linux/fanotify.h | 14 ++++++++++----
include/uapi/linux/fanotify.h | 2 ++
4 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index c1e4ae221093..5e05410ddb9f 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -917,8 +917,9 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,
BUILD_BUG_ON(FAN_OPEN_EXEC_PERM != FS_OPEN_EXEC_PERM);
BUILD_BUG_ON(FAN_FS_ERROR != FS_ERROR);
BUILD_BUG_ON(FAN_RENAME != FS_RENAME);
+ BUILD_BUG_ON(FAN_PRE_ACCESS != FS_PRE_ACCESS);
- BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 21);
+ BUILD_BUG_ON(HWEIGHT32(ALL_FANOTIFY_EVENT_BITS) != 22);
mask = fanotify_group_event_mask(group, iter_info, &match_mask,
mask, data, data_type, dir);
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 8d8afa09560b..b4dfc8f0d037 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -1646,11 +1646,23 @@ static int fanotify_events_supported(struct fsnotify_group *group,
unsigned int flags)
{
unsigned int mark_type = flags & FANOTIFY_MARK_TYPE_BITS;
+ bool is_dir = d_is_dir(path->dentry);
/* Strict validation of events in non-dir inode mask with v5.17+ APIs */
bool strict_dir_events = FAN_GROUP_FLAG(group, FAN_REPORT_TARGET_FID) ||
(mask & FAN_RENAME) ||
(flags & FAN_MARK_IGNORE);
+ /*
+ * Filesystems need to opt-into pre-content evnets (a.k.a HSM)
+ * and they are only supported on regular files and directories.
+ */
+ if (mask & FANOTIFY_PRE_CONTENT_EVENTS) {
+ if (!(path->mnt->mnt_sb->s_iflags & SB_I_ALLOW_HSM))
+ return -EOPNOTSUPP;
+ if (!is_dir && !d_is_reg(path->dentry))
+ return -EINVAL;
+ }
+
/*
* Some filesystems such as 'proc' acquire unusual locks when opening
* files. For them fanotify permission events have high chances of
@@ -1683,7 +1695,7 @@ static int fanotify_events_supported(struct fsnotify_group *group,
* but because we always allowed it, error only when using new APIs.
*/
if (strict_dir_events && mark_type == FAN_MARK_INODE &&
- !d_is_dir(path->dentry) && (mask & FANOTIFY_DIRONLY_EVENT_BITS))
+ !is_dir && (mask & FANOTIFY_DIRONLY_EVENT_BITS))
return -ENOTDIR;
return 0;
@@ -1787,11 +1799,15 @@ static int do_fanotify_mark(int fanotify_fd, unsigned int flags, __u64 mask,
goto fput_and_out;
/*
- * Permission events require minimum priority FAN_CLASS_CONTENT.
+ * Permission events are not allowed for FAN_CLASS_NOTIF.
+ * Pre-content permission events are not allowed for FAN_CLASS_CONTENT.
*/
ret = -EINVAL;
if (mask & FANOTIFY_PERM_EVENTS &&
- group->priority < FSNOTIFY_PRIO_CONTENT)
+ group->priority == FSNOTIFY_PRIO_NORMAL)
+ goto fput_and_out;
+ else if (mask & FANOTIFY_PRE_CONTENT_EVENTS &&
+ group->priority == FSNOTIFY_PRIO_CONTENT)
goto fput_and_out;
if (mask & FAN_FS_ERROR &&
diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h
index 89ff45bd6f01..c747af064d2c 100644
--- a/include/linux/fanotify.h
+++ b/include/linux/fanotify.h
@@ -89,6 +89,16 @@
#define FANOTIFY_DIRENT_EVENTS (FAN_MOVE | FAN_CREATE | FAN_DELETE | \
FAN_RENAME)
+/* Content events can be used to inspect file content */
+#define FANOTIFY_CONTENT_PERM_EVENTS (FAN_OPEN_PERM | FAN_OPEN_EXEC_PERM | \
+ FAN_ACCESS_PERM)
+/* Pre-content events can be used to fill file content */
+#define FANOTIFY_PRE_CONTENT_EVENTS (FAN_PRE_ACCESS)
+
+/* Events that require a permission response from user */
+#define FANOTIFY_PERM_EVENTS (FANOTIFY_CONTENT_PERM_EVENTS | \
+ FANOTIFY_PRE_CONTENT_EVENTS)
+
/* Events that can be reported with event->fd */
#define FANOTIFY_FD_EVENTS (FANOTIFY_PATH_EVENTS | FANOTIFY_PERM_EVENTS)
@@ -104,10 +114,6 @@
FANOTIFY_INODE_EVENTS | \
FANOTIFY_ERROR_EVENTS)
-/* Events that require a permission response from user */
-#define FANOTIFY_PERM_EVENTS (FAN_OPEN_PERM | FAN_ACCESS_PERM | \
- FAN_OPEN_EXEC_PERM)
-
/* Extra flags that may be reported with event or control handling of events */
#define FANOTIFY_EVENT_FLAGS (FAN_EVENT_ON_CHILD | FAN_ONDIR)
diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h
index 79072b6894f2..7596168c80eb 100644
--- a/include/uapi/linux/fanotify.h
+++ b/include/uapi/linux/fanotify.h
@@ -27,6 +27,8 @@
#define FAN_OPEN_EXEC_PERM 0x00040000 /* File open/exec in perm check */
/* #define FAN_DIR_MODIFY 0x00080000 */ /* Deprecated (reserved) */
+#define FAN_PRE_ACCESS 0x00100000 /* Pre-content access hook */
+
#define FAN_EVENT_ON_CHILD 0x08000000 /* Interested in child events */
#define FAN_RENAME 0x10000000 /* File was renamed */
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 11/19] fanotify: report file range info with pre-content events
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (9 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 10/19] fanotify: introduce FAN_PRE_ACCESS permission event Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 12/19] fanotify: allow to set errno in FAN_DENY permission response Jan Kara
` (8 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
With group class FAN_CLASS_PRE_CONTENT, report offset and length info
along with FAN_PRE_ACCESS pre-content events.
This information is meant to be used by hierarchical storage managers
that want to fill partial content of files on first access to range.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/b90a9e6c809dd3cad5684da90f23ea93ec6ce8c8.1731684329.git.josef@toxicpanda.com
---
fs/notify/fanotify/fanotify.h | 8 +++++++
fs/notify/fanotify/fanotify_user.c | 38 ++++++++++++++++++++++++++++++
include/uapi/linux/fanotify.h | 8 +++++++
3 files changed, 54 insertions(+)
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index 93598b7d5952..7f06355afa1f 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -448,6 +448,14 @@ static inline bool fanotify_is_perm_event(u32 mask)
mask & FANOTIFY_PERM_EVENTS;
}
+static inline bool fanotify_event_has_access_range(struct fanotify_event *event)
+{
+ if (!(event->mask & FANOTIFY_PRE_CONTENT_EVENTS))
+ return false;
+
+ return FANOTIFY_PERM(event)->ppos;
+}
+
static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
{
return container_of(fse, struct fanotify_event, fse);
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index b4dfc8f0d037..61e0f67169e4 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -122,6 +122,8 @@ struct kmem_cache *fanotify_perm_event_cachep __ro_after_init;
sizeof(struct fanotify_event_info_pidfd)
#define FANOTIFY_ERROR_INFO_LEN \
(sizeof(struct fanotify_event_info_error))
+#define FANOTIFY_RANGE_INFO_LEN \
+ (sizeof(struct fanotify_event_info_range))
static int fanotify_fid_info_len(int fh_len, int name_len)
{
@@ -181,6 +183,9 @@ static size_t fanotify_event_len(unsigned int info_mode,
if (info_mode & FAN_REPORT_PIDFD)
event_len += FANOTIFY_PIDFD_INFO_LEN;
+ if (fanotify_event_has_access_range(event))
+ event_len += FANOTIFY_RANGE_INFO_LEN;
+
return event_len;
}
@@ -517,6 +522,30 @@ static int copy_pidfd_info_to_user(int pidfd,
return info_len;
}
+static size_t copy_range_info_to_user(struct fanotify_event *event,
+ char __user *buf, int count)
+{
+ struct fanotify_perm_event *pevent = FANOTIFY_PERM(event);
+ struct fanotify_event_info_range info = { };
+ size_t info_len = FANOTIFY_RANGE_INFO_LEN;
+
+ if (WARN_ON_ONCE(info_len > count))
+ return -EFAULT;
+
+ if (WARN_ON_ONCE(!pevent->ppos))
+ return -EINVAL;
+
+ info.hdr.info_type = FAN_EVENT_INFO_TYPE_RANGE;
+ info.hdr.len = info_len;
+ info.offset = *(pevent->ppos);
+ info.count = pevent->count;
+
+ if (copy_to_user(buf, &info, info_len))
+ return -EFAULT;
+
+ return info_len;
+}
+
static int copy_info_records_to_user(struct fanotify_event *event,
struct fanotify_info *info,
unsigned int info_mode, int pidfd,
@@ -638,6 +667,15 @@ static int copy_info_records_to_user(struct fanotify_event *event,
total_bytes += ret;
}
+ if (fanotify_event_has_access_range(event)) {
+ ret = copy_range_info_to_user(event, buf, count);
+ if (ret < 0)
+ return ret;
+ buf += ret;
+ count -= ret;
+ total_bytes += ret;
+ }
+
return total_bytes;
}
diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h
index 7596168c80eb..0636a9c85dd0 100644
--- a/include/uapi/linux/fanotify.h
+++ b/include/uapi/linux/fanotify.h
@@ -146,6 +146,7 @@ struct fanotify_event_metadata {
#define FAN_EVENT_INFO_TYPE_DFID 3
#define FAN_EVENT_INFO_TYPE_PIDFD 4
#define FAN_EVENT_INFO_TYPE_ERROR 5
+#define FAN_EVENT_INFO_TYPE_RANGE 6
/* Special info types for FAN_RENAME */
#define FAN_EVENT_INFO_TYPE_OLD_DFID_NAME 10
@@ -192,6 +193,13 @@ struct fanotify_event_info_error {
__u32 error_count;
};
+struct fanotify_event_info_range {
+ struct fanotify_event_info_header hdr;
+ __u32 pad;
+ __u64 offset;
+ __u64 count;
+};
+
/*
* User space may need to record additional information about its decision.
* The extra information type records what kind of information is included.
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 12/19] fanotify: allow to set errno in FAN_DENY permission response
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (10 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 11/19] fanotify: report file range info with pre-content events Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 13/19] fanotify: disable readahead if we have pre-content watches Jan Kara
` (7 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Amir Goldstein <amir73il@gmail.com>
With FAN_DENY response, user trying to perform the filesystem operation
gets an error with errno set to EPERM.
It is useful for hierarchical storage management (HSM) service to be able
to deny access for reasons more diverse than EPERM, for example EAGAIN,
if HSM could retry the operation later.
Allow fanotify groups with priority FAN_CLASSS_PRE_CONTENT to responsd
to permission events with the response value FAN_DENY_ERRNO(errno),
instead of FAN_DENY to return a custom error.
Limit custom error values to errors expected on read(2)/write(2) and
open(2) of regular files. This list could be extended in the future.
Userspace can test for legitimate values of FAN_DENY_ERRNO(errno) by
writing a response to an fanotify group fd with a value of FAN_NOFD in
the fd field of the response.
The change in fanotify_response is backward compatible, because errno is
written in the high 8 bits of the 32bit response field and old kernels
reject respose value with high bits set.
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/1e5fb6af84b69ca96b5c849fa5f10bdf4d1dc414.1731684329.git.josef@toxicpanda.com
---
fs/notify/fanotify/fanotify.c | 17 +++++++++++++----
fs/notify/fanotify/fanotify.h | 5 +++++
fs/notify/fanotify/fanotify_user.c | 29 +++++++++++++++++++++++++++--
include/linux/fanotify.h | 4 +++-
include/uapi/linux/fanotify.h | 7 +++++++
5 files changed, 55 insertions(+), 7 deletions(-)
diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c
index 5e05410ddb9f..6ebe95e5bbdd 100644
--- a/fs/notify/fanotify/fanotify.c
+++ b/fs/notify/fanotify/fanotify.c
@@ -224,7 +224,7 @@ static int fanotify_get_response(struct fsnotify_group *group,
struct fanotify_perm_event *event,
struct fsnotify_iter_info *iter_info)
{
- int ret;
+ int ret, errno;
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
@@ -263,14 +263,23 @@ static int fanotify_get_response(struct fsnotify_group *group,
ret = 0;
break;
case FAN_DENY:
+ /* Check custom errno from pre-content events */
+ errno = fanotify_get_response_errno(event->response);
+ if (errno) {
+ ret = -errno;
+ break;
+ }
+ fallthrough;
default:
ret = -EPERM;
}
/* Check if the response should be audited */
- if (event->response & FAN_AUDIT)
- audit_fanotify(event->response & ~FAN_AUDIT,
- &event->audit_rule);
+ if (event->response & FAN_AUDIT) {
+ u32 response = event->response &
+ (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS);
+ audit_fanotify(response & ~FAN_AUDIT, &event->audit_rule);
+ }
pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
group, event, ret);
diff --git a/fs/notify/fanotify/fanotify.h b/fs/notify/fanotify/fanotify.h
index 7f06355afa1f..c12cbc270539 100644
--- a/fs/notify/fanotify/fanotify.h
+++ b/fs/notify/fanotify/fanotify.h
@@ -528,3 +528,8 @@ static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark)
return mflags;
}
+
+static inline u32 fanotify_get_response_errno(int res)
+{
+ return (res >> FAN_ERRNO_SHIFT) & FAN_ERRNO_MASK;
+}
diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c
index 61e0f67169e4..0919ea735f4a 100644
--- a/fs/notify/fanotify/fanotify_user.c
+++ b/fs/notify/fanotify/fanotify_user.c
@@ -328,11 +328,12 @@ static int process_access_response(struct fsnotify_group *group,
struct fanotify_perm_event *event;
int fd = response_struct->fd;
u32 response = response_struct->response;
+ int errno = fanotify_get_response_errno(response);
int ret = info_len;
struct fanotify_response_info_audit_rule friar;
- pr_debug("%s: group=%p fd=%d response=%u buf=%p size=%zu\n", __func__,
- group, fd, response, info, info_len);
+ pr_debug("%s: group=%p fd=%d response=%x errno=%d buf=%p size=%zu\n",
+ __func__, group, fd, response, errno, info, info_len);
/*
* make sure the response is valid, if invalid we do nothing and either
* userspace can send a valid response or we will clean it up after the
@@ -343,7 +344,31 @@ static int process_access_response(struct fsnotify_group *group,
switch (response & FANOTIFY_RESPONSE_ACCESS) {
case FAN_ALLOW:
+ if (errno)
+ return -EINVAL;
+ break;
case FAN_DENY:
+ /* Custom errno is supported only for pre-content groups */
+ if (errno && group->priority != FSNOTIFY_PRIO_PRE_CONTENT)
+ return -EINVAL;
+
+ /*
+ * Limit errno to values expected on open(2)/read(2)/write(2)
+ * of regular files.
+ */
+ switch (errno) {
+ case 0:
+ case EIO:
+ case EPERM:
+ case EBUSY:
+ case ETXTBSY:
+ case EAGAIN:
+ case ENOSPC:
+ case EDQUOT:
+ break;
+ default:
+ return -EINVAL;
+ }
break;
default:
return -EINVAL;
diff --git a/include/linux/fanotify.h b/include/linux/fanotify.h
index c747af064d2c..78f660ebc318 100644
--- a/include/linux/fanotify.h
+++ b/include/linux/fanotify.h
@@ -132,7 +132,9 @@
/* These masks check for invalid bits in permission responses. */
#define FANOTIFY_RESPONSE_ACCESS (FAN_ALLOW | FAN_DENY)
#define FANOTIFY_RESPONSE_FLAGS (FAN_AUDIT | FAN_INFO)
-#define FANOTIFY_RESPONSE_VALID_MASK (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS)
+#define FANOTIFY_RESPONSE_VALID_MASK \
+ (FANOTIFY_RESPONSE_ACCESS | FANOTIFY_RESPONSE_FLAGS | \
+ (FAN_ERRNO_MASK << FAN_ERRNO_SHIFT))
/* Do not use these old uapi constants internally */
#undef FAN_ALL_CLASS_BITS
diff --git a/include/uapi/linux/fanotify.h b/include/uapi/linux/fanotify.h
index 0636a9c85dd0..bd8167979707 100644
--- a/include/uapi/linux/fanotify.h
+++ b/include/uapi/linux/fanotify.h
@@ -235,6 +235,13 @@ struct fanotify_response_info_audit_rule {
/* Legit userspace responses to a _PERM event */
#define FAN_ALLOW 0x01
#define FAN_DENY 0x02
+/* errno other than EPERM can specified in upper byte of deny response */
+#define FAN_ERRNO_BITS 8
+#define FAN_ERRNO_SHIFT (32 - FAN_ERRNO_BITS)
+#define FAN_ERRNO_MASK ((1 << FAN_ERRNO_BITS) - 1)
+#define FAN_DENY_ERRNO(err) \
+ (FAN_DENY | ((((__u32)(err)) & FAN_ERRNO_MASK) << FAN_ERRNO_SHIFT))
+
#define FAN_AUDIT 0x10 /* Bitmask to create audit record for result */
#define FAN_INFO 0x20 /* Bitmask to indicate additional information */
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 13/19] fanotify: disable readahead if we have pre-content watches
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (11 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 12/19] fanotify: allow to set errno in FAN_DENY permission response Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 14/19] mm: don't allow huge faults for files with pre content watches Jan Kara
` (6 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Josef Bacik <josef@toxicpanda.com>
With page faults we can trigger readahead on the file, and then
subsequent faults can find these pages and insert them into the file
without emitting an fanotify event. To avoid this case, disable
readahead if we have pre-content watches on the file. This way we are
guaranteed to get an event for every range we attempt to access on a
pre-content watched file.
Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/70a54e859f555e54bc7a47b32fe5aca92b085615.1731684329.git.josef@toxicpanda.com
---
mm/filemap.c | 12 ++++++++++++
mm/readahead.c | 14 ++++++++++++++
2 files changed, 26 insertions(+)
diff --git a/mm/filemap.c b/mm/filemap.c
index 36d22968be9a..98f15dccff89 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3149,6 +3149,14 @@ static struct file *do_sync_mmap_readahead(struct vm_fault *vmf)
unsigned long vm_flags = vmf->vma->vm_flags;
unsigned int mmap_miss;
+ /*
+ * If we have pre-content watches we need to disable readahead to make
+ * sure that we don't populate our mapping with 0 filled pages that we
+ * never emitted an event for.
+ */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode)))
+ return fpin;
+
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
/* Use the readahead code, even if readahead is disabled */
if ((vm_flags & VM_HUGEPAGE) && HPAGE_PMD_ORDER <= MAX_PAGECACHE_ORDER) {
@@ -3217,6 +3225,10 @@ static struct file *do_async_mmap_readahead(struct vm_fault *vmf,
struct file *fpin = NULL;
unsigned int mmap_miss;
+ /* See comment in do_sync_mmap_readahead. */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode)))
+ return fpin;
+
/* If we don't want any read-ahead, don't bother */
if (vmf->vma->vm_flags & VM_RAND_READ || !ra->ra_pages)
return fpin;
diff --git a/mm/readahead.c b/mm/readahead.c
index 3dc6c7a128dd..e482f9f2e159 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -128,6 +128,7 @@
#include <linux/blk-cgroup.h>
#include <linux/fadvise.h>
#include <linux/sched/mm.h>
+#include <linux/fsnotify.h>
#include "internal.h"
@@ -544,6 +545,15 @@ void page_cache_sync_ra(struct readahead_control *ractl,
unsigned long max_pages, contig_count;
pgoff_t prev_index, miss;
+ /*
+ * If we have pre-content watches we need to disable readahead to make
+ * sure that we don't find 0 filled pages in cache that we never emitted
+ * events for. Filesystems supporting HSM must make sure to not call
+ * this function with ractl->file unset for files handled by HSM.
+ */
+ if (ractl->file && unlikely(FMODE_FSNOTIFY_HSM(ractl->file->f_mode)))
+ return;
+
/*
* Even if readahead is disabled, issue this request as readahead
* as we'll need it to satisfy the requested range. The forced
@@ -622,6 +632,10 @@ void page_cache_async_ra(struct readahead_control *ractl,
if (!ra->ra_pages)
return;
+ /* See the comment in page_cache_sync_ra. */
+ if (ractl->file && unlikely(FMODE_FSNOTIFY_HSM(ractl->file->f_mode)))
+ return;
+
/*
* Same bit is used for PG_readahead and PG_reclaim.
*/
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 14/19] mm: don't allow huge faults for files with pre content watches
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (12 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 13/19] fanotify: disable readahead if we have pre-content watches Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 15/19] fsnotify: generate pre-content permission event on page fault Jan Kara
` (5 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Josef Bacik <josef@toxicpanda.com>
There's nothing stopping us from supporting this, we could simply pass
the order into the helper and emit the proper length. However currently
there's no tests to validate this works properly, so disable it until
there's a desire to support this along with the appropriate tests.
Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/9035b82cff08a3801cef3d06bbf2778b2e5a4dba.1731684329.git.josef@toxicpanda.com
---
mm/memory.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/mm/memory.c b/mm/memory.c
index 2366578015ad..111ba20a22e7 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -78,6 +78,7 @@
#include <linux/ptrace.h>
#include <linux/vmalloc.h>
#include <linux/sched/sysctl.h>
+#include <linux/fsnotify.h>
#include <trace/events/kmem.h>
@@ -5622,8 +5623,17 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
{
struct vm_area_struct *vma = vmf->vma;
+ struct file *file = vma->vm_file;
if (vma_is_anonymous(vma))
return do_huge_pmd_anonymous_page(vmf);
+ /*
+ * Currently we just emit PAGE_SIZE for our fault events, so don't allow
+ * a huge fault if we have a pre content watch on this file. This would
+ * be trivial to support, but there would need to be tests to ensure
+ * this works properly and those don't exist currently.
+ */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode)))
+ return VM_FAULT_FALLBACK;
if (vma->vm_ops->huge_fault)
return vma->vm_ops->huge_fault(vmf, PMD_ORDER);
return VM_FAULT_FALLBACK;
@@ -5633,6 +5643,7 @@ static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)
static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf)
{
struct vm_area_struct *vma = vmf->vma;
+ struct file *file = vma->vm_file;
const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
vm_fault_t ret;
@@ -5647,6 +5658,9 @@ static inline vm_fault_t wp_huge_pmd(struct vm_fault *vmf)
}
if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) {
+ /* See comment in create_huge_pmd. */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode)))
+ goto split;
if (vma->vm_ops->huge_fault) {
ret = vma->vm_ops->huge_fault(vmf, PMD_ORDER);
if (!(ret & VM_FAULT_FALLBACK))
@@ -5666,9 +5680,13 @@ static vm_fault_t create_huge_pud(struct vm_fault *vmf)
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
struct vm_area_struct *vma = vmf->vma;
+ struct file *file = vma->vm_file;
/* No support for anonymous transparent PUD pages yet */
if (vma_is_anonymous(vma))
return VM_FAULT_FALLBACK;
+ /* See comment in create_huge_pmd. */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode)))
+ return VM_FAULT_FALLBACK;
if (vma->vm_ops->huge_fault)
return vma->vm_ops->huge_fault(vmf, PUD_ORDER);
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
@@ -5680,12 +5698,16 @@ static vm_fault_t wp_huge_pud(struct vm_fault *vmf, pud_t orig_pud)
#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && \
defined(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)
struct vm_area_struct *vma = vmf->vma;
+ struct file *file = vma->vm_file;
vm_fault_t ret;
/* No support for anonymous transparent PUD pages yet */
if (vma_is_anonymous(vma))
goto split;
if (vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) {
+ /* See comment in create_huge_pmd. */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode)))
+ goto split;
if (vma->vm_ops->huge_fault) {
ret = vma->vm_ops->huge_fault(vmf, PUD_ORDER);
if (!(ret & VM_FAULT_FALLBACK))
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 15/19] fsnotify: generate pre-content permission event on page fault
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (13 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 14/19] mm: don't allow huge faults for files with pre content watches Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 16/19] xfs: add pre-content fsnotify hook for DAX faults Jan Kara
` (4 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Josef Bacik <josef@toxicpanda.com>
FS_PRE_ACCESS will be generated on page fault depending on the faulting
method. This pre-content event is meant to be used by hierarchical storage
managers that want to fill in the file content on first read access.
Export a simple helper that file systems that have their own ->fault()
will use, and have a more complicated helper to be do fancy things in
filemap_fault.
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/aa56c50ce81b1fd18d7f5d71dd2dfced5eba9687.1731684329.git.josef@toxicpanda.com
---
include/linux/mm.h | 1 +
mm/filemap.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++
mm/nommu.c | 7 +++++
3 files changed, 86 insertions(+)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index ecf63d2b0582..e9077ab16972 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3405,6 +3405,7 @@ extern vm_fault_t filemap_fault(struct vm_fault *vmf);
extern vm_fault_t filemap_map_pages(struct vm_fault *vmf,
pgoff_t start_pgoff, pgoff_t end_pgoff);
extern vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf);
+extern vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf);
extern unsigned long stack_guard_gap;
/* Generic expand stack which grows the stack according to GROWS{UP,DOWN} */
diff --git a/mm/filemap.c b/mm/filemap.c
index 98f15dccff89..0648bb568259 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -47,6 +47,7 @@
#include <linux/splice.h>
#include <linux/rcupdate_wait.h>
#include <linux/sched/mm.h>
+#include <linux/fsnotify.h>
#include <asm/pgalloc.h>
#include <asm/tlbflush.h>
#include "internal.h"
@@ -3287,6 +3288,52 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
return ret;
}
+/**
+ * filemap_fsnotify_fault - maybe emit a pre-content event.
+ * @vmf: struct vm_fault containing details of the fault.
+ * @folio: the folio we're faulting in.
+ *
+ * If we have a pre-content watch on this file we will emit an event for this
+ * range. If we return anything the fault caller should return immediately, we
+ * will return VM_FAULT_RETRY if we had to emit an event, which will trigger the
+ * fault again and then the fault handler will run the second time through.
+ *
+ * This is meant to be called with the folio that we will be filling in to make
+ * sure the event is emitted for the correct range.
+ *
+ * Return: a bitwise-OR of %VM_FAULT_ codes, 0 if nothing happened.
+ */
+vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf)
+{
+ struct file *fpin = NULL;
+ int mask = (vmf->flags & FAULT_FLAG_WRITE) ? MAY_WRITE : MAY_ACCESS;
+ loff_t pos = vmf->pgoff >> PAGE_SHIFT;
+ size_t count = PAGE_SIZE;
+ vm_fault_t ret;
+
+ /*
+ * We already did this and now we're retrying with everything locked,
+ * don't emit the event and continue.
+ */
+ if (vmf->flags & FAULT_FLAG_TRIED)
+ return 0;
+
+ /* No watches, we're done. */
+ if (likely(!FMODE_FSNOTIFY_HSM(vmf->vma->vm_file->f_mode)))
+ return 0;
+
+ fpin = maybe_unlock_mmap_for_io(vmf, fpin);
+ if (!fpin)
+ return VM_FAULT_SIGBUS;
+
+ ret = fsnotify_file_area_perm(fpin, mask, &pos, count);
+ fput(fpin);
+ if (ret)
+ return VM_FAULT_SIGBUS;
+ return VM_FAULT_RETRY;
+}
+EXPORT_SYMBOL_GPL(filemap_fsnotify_fault);
+
/**
* filemap_fault - read in file data for page fault handling
* @vmf: struct vm_fault containing details of the fault
@@ -3390,6 +3437,37 @@ vm_fault_t filemap_fault(struct vm_fault *vmf)
* or because readahead was otherwise unable to retrieve it.
*/
if (unlikely(!folio_test_uptodate(folio))) {
+ /*
+ * If this is a precontent file we have can now emit an event to
+ * try and populate the folio.
+ */
+ if (!(vmf->flags & FAULT_FLAG_TRIED) &&
+ unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
+ loff_t pos = folio_pos(folio);
+ size_t count = folio_size(folio);
+
+ /* We're NOWAIT, we have to retry. */
+ if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT) {
+ folio_unlock(folio);
+ goto out_retry;
+ }
+
+ if (mapping_locked)
+ filemap_invalidate_unlock_shared(mapping);
+ mapping_locked = false;
+
+ folio_unlock(folio);
+ fpin = maybe_unlock_mmap_for_io(vmf, fpin);
+ if (!fpin)
+ goto out_retry;
+
+ error = fsnotify_file_area_perm(fpin, MAY_ACCESS, &pos,
+ count);
+ if (error)
+ ret = VM_FAULT_SIGBUS;
+ goto out_retry;
+ }
+
/*
* If the invalidate lock is not held, the folio was in cache
* and uptodate and now it is not. Strange but possible since we
diff --git a/mm/nommu.c b/mm/nommu.c
index 385b0c15add8..a8789faa49ee 100644
--- a/mm/nommu.c
+++ b/mm/nommu.c
@@ -1614,6 +1614,13 @@ int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
}
EXPORT_SYMBOL(remap_vmalloc_range);
+vm_fault_t filemap_fsnotify_fault(struct vm_fault *vmf)
+{
+ BUG();
+ return 0;
+}
+EXPORT_SYMBOL_GPL(filemap_fsnotify_fault);
+
vm_fault_t filemap_fault(struct vm_fault *vmf)
{
BUG();
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 16/19] xfs: add pre-content fsnotify hook for DAX faults
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (14 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 15/19] fsnotify: generate pre-content permission event on page fault Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 17/19] btrfs: disable defrag on pre-content watched files Jan Kara
` (3 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Josef Bacik <josef@toxicpanda.com>
xfs has it's own handling for DAX faults, so we need to add the
pre-content fsnotify hook for this case. Other faults go through
filemap_fault so they're handled properly there.
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/9eccdf59a65b72f0a1a5e2f2b9bff8eda2d4f2d9.1731684329.git.josef@toxicpanda.com
---
fs/xfs/xfs_file.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/fs/xfs/xfs_file.c b/fs/xfs/xfs_file.c
index 412b1d71b52b..8b1bfb149b2c 100644
--- a/fs/xfs/xfs_file.c
+++ b/fs/xfs/xfs_file.c
@@ -1395,6 +1395,9 @@ xfs_dax_read_fault(
struct xfs_inode *ip = XFS_I(file_inode(vmf->vma->vm_file));
vm_fault_t ret;
+ ret = filemap_fsnotify_fault(vmf);
+ if (unlikely(ret))
+ return ret;
xfs_ilock(ip, XFS_MMAPLOCK_SHARED);
ret = xfs_dax_fault_locked(vmf, order, false);
xfs_iunlock(ip, XFS_MMAPLOCK_SHARED);
@@ -1412,6 +1415,17 @@ xfs_write_fault(
unsigned int lock_mode = XFS_MMAPLOCK_SHARED;
vm_fault_t ret;
+ /*
+ * Usually we get here from ->page_mkwrite callback but in case of DAX
+ * we will get here also for ordinary write fault. Handle HSM
+ * notifications for that case.
+ */
+ if (IS_DAX(inode)) {
+ ret = filemap_fsnotify_fault(vmf);
+ if (unlikely(ret))
+ return ret;
+ }
+
sb_start_pagefault(inode->i_sb);
file_update_time(vmf->vma->vm_file);
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 17/19] btrfs: disable defrag on pre-content watched files
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (15 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 16/19] xfs: add pre-content fsnotify hook for DAX faults Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 18/19] ext4: add pre-content fsnotify hook for DAX faults Jan Kara
` (2 subsequent siblings)
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Josef Bacik <josef@toxicpanda.com>
We queue up inodes to be defrag'ed asynchronously, which means we do not
have their original file for readahead. This means that the code to
skip readahead on pre-content watched files will not run, and we could
potentially read in empty pages.
Handle this corner case by disabling defrag on files that are currently
being watched for pre-content events.
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/4cc5bcea13db7904174353d08e85157356282a59.1731684329.git.josef@toxicpanda.com
---
fs/btrfs/ioctl.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 226c91fe31a7..a908f75458f0 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2638,6 +2638,15 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
goto out;
}
+ /*
+ * Don't allow defrag on pre-content watched files, as it could
+ * populate the page cache with 0's via readahead.
+ */
+ if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
+ ret = -EINVAL;
+ goto out;
+ }
+
if (argp) {
if (copy_from_user(&range, argp, sizeof(range))) {
ret = -EFAULT;
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 18/19] ext4: add pre-content fsnotify hook for DAX faults
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (16 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 17/19] btrfs: disable defrag on pre-content watched files Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 11:22 ` [PATCH 19/19] fs: enable pre-content events on supported file systems Jan Kara
2024-11-21 16:06 ` [PATCH v9 00/19] fanotify: add pre-content hooks Amir Goldstein
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
ext4 has its own handling for DAX faults. Add the pre-content fsnotify
hook for this case.
Signed-off-by: Jan Kara <jack@suse.cz>
---
fs/ext4/file.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index f14aed14b9cf..07946bf67944 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -737,6 +737,9 @@ static vm_fault_t ext4_dax_huge_fault(struct vm_fault *vmf, unsigned int order)
return VM_FAULT_SIGBUS;
}
} else {
+ result = filemap_fsnotify_fault(vmf);
+ if (unlikely(result))
+ return result;
filemap_invalidate_lock_shared(mapping);
}
result = dax_iomap_fault(vmf, order, &pfn, &error, &ext4_iomap_ops);
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 19/19] fs: enable pre-content events on supported file systems
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (17 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 18/19] ext4: add pre-content fsnotify hook for DAX faults Jan Kara
@ 2024-11-21 11:22 ` Jan Kara
2024-11-21 16:06 ` [PATCH v9 00/19] fanotify: add pre-content hooks Amir Goldstein
19 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-21 11:22 UTC (permalink / raw)
To: linux-fsdevel
Cc: Amir Goldstein, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm, Jan Kara
From: Josef Bacik <josef@toxicpanda.com>
Now that all the code has been added for pre-content events, and the
various file systems that need the page fault hooks for fsnotify have
been updated, add SB_I_ALLOW_HSM to the supported file systems.
Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/46960dcb2725fa0317895ed66a8409ba1c306a82.1731684329.git.josef@toxicpanda.com
---
fs/btrfs/super.c | 2 +-
fs/ext4/super.c | 3 +++
fs/xfs/xfs_super.c | 2 +-
3 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 98fa0f382480..c211e5aa82d8 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -954,7 +954,7 @@ static int btrfs_fill_super(struct super_block *sb,
#endif
sb->s_xattr = btrfs_xattr_handlers;
sb->s_time_gran = 1;
- sb->s_iflags |= SB_I_CGROUPWB;
+ sb->s_iflags |= SB_I_CGROUPWB | SB_I_ALLOW_HSM;
err = super_setup_bdi(sb);
if (err) {
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 16a4ce704460..733d71dac09e 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -5266,6 +5266,9 @@ static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
/* i_version is always enabled now */
sb->s_flags |= SB_I_VERSION;
+ /* HSM events are allowed by default. */
+ sb->s_iflags |= SB_I_ALLOW_HSM;
+
err = ext4_check_feature_compatibility(sb, es, silent);
if (err)
goto failed_mount;
diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c
index fbb3a1594c0d..b6cd52f2289d 100644
--- a/fs/xfs/xfs_super.c
+++ b/fs/xfs/xfs_super.c
@@ -1713,7 +1713,7 @@ xfs_fs_fill_super(
sb->s_time_max = XFS_LEGACY_TIME_MAX;
}
trace_xfs_inode_timestamp_range(mp, sb->s_time_min, sb->s_time_max);
- sb->s_iflags |= SB_I_CGROUPWB;
+ sb->s_iflags |= SB_I_CGROUPWB | SB_I_ALLOW_HSM;
set_posix_acl_flag(sb);
--
2.35.3
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open
2024-11-21 11:22 ` [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open Jan Kara
@ 2024-11-21 16:01 ` Amir Goldstein
2024-11-22 10:58 ` Jan Kara
0 siblings, 1 reply; 23+ messages in thread
From: Amir Goldstein @ 2024-11-21 16:01 UTC (permalink / raw)
To: Jan Kara
Cc: linux-fsdevel, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm
On Thu, Nov 21, 2024 at 12:22 PM Jan Kara <jack@suse.cz> wrote:
>
> From: Amir Goldstein <amir73il@gmail.com>
>
> So far, we set FMODE_NONOTIFY_ flags at open time if we know that there
> are no permission event watchers at all on the filesystem, but lack of
> FMODE_NONOTIFY_ flags does not mean that the file is actually watched.
>
> For pre-content events, it is possible to optimize things so that we
> don't bother trying to send pre-content events if file was not watched
> (through sb, mnt, parent or inode itself) on open. Set FMODE_NONOTIFY_
> flags according to that.
>
> Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> Signed-off-by: Jan Kara <jack@suse.cz>
> Link: https://patch.msgid.link/2ddcc9f8d1fde48d085318a6b5a889289d8871d8.1731684329.git.josef@toxicpanda.com
> ---
> fs/notify/fsnotify.c | 27 +++++++++++++++++++++++++--
> include/linux/fsnotify_backend.h | 3 +++
> 2 files changed, 28 insertions(+), 2 deletions(-)
>
> diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
> index 569ec356e4ce..dd1dffd89fd6 100644
> --- a/fs/notify/fsnotify.c
> +++ b/fs/notify/fsnotify.c
> @@ -193,7 +193,7 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,
> return mask & marks_mask;
> }
>
> -/* Are there any inode/mount/sb objects that are interested in this event? */
> +/* Are there any inode/mount/sb objects that watch for these events? */
> static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
> __u32 mask)
> {
> @@ -632,7 +632,9 @@ EXPORT_SYMBOL_GPL(fsnotify);
> */
> void file_set_fsnotify_mode(struct file *file)
> {
> - struct super_block *sb = file->f_path.dentry->d_sb;
> + struct dentry *dentry = file->f_path.dentry, *parent;
> + struct super_block *sb = dentry->d_sb;
> + __u32 mnt_mask, p_mask;
>
> /* Is it a file opened by fanotify? */
> if (FMODE_FSNOTIFY_NONE(file->f_mode))
> @@ -658,6 +660,27 @@ void file_set_fsnotify_mode(struct file *file)
> file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> return;
> }
This was lost in translation:
@@ -672,8 +672,10 @@ void file_set_fsnotify_mode(struct file *file)
/*
* If there are permission event watchers but no pre-content event
* watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that.
+ * Pre-content events are only reported for regular files and dirs.
*/
- if (likely(!fsnotify_sb_has_priority_watchers(sb,
+ if ((!d_is_dir(dentry) && !d_is_reg(dentry)) ||
+ likely(!fsnotify_sb_has_priority_watchers(sb,
FSNOTIFY_PRIO_PRE_CONTENT))) {
file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
return;
> +
> + /*
> + * OK, there are some pre-content watchers. Check if anybody can be
> + * watching for pre-content events on *this* file.
> + */
> + mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
> + if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) &&
> + !fsnotify_object_watched(d_inode(dentry), mnt_mask,
> + FSNOTIFY_PRE_CONTENT_EVENTS))) {
> + file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> + return;
> + }
> +
> + /* Even parent is not watching for pre-content events on this file? */
> + parent = dget_parent(dentry);
> + p_mask = fsnotify_inode_watches_children(d_inode(parent));
> + dput(parent);
> + if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
> + file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> + return;
> + }
inlining broke the logic and your branch fails the new PRE_ACCESS
test cases of fanotify03 LTP test (now pushed to branch fan_hsm).
Specifically in the test case that fails, parent is not watching and
inode is watching pre-content and your code gets to the p_mask
test and marks this file as no-pre-content watchers.
This passes the test:
@@ -684,17 +686,18 @@ void file_set_fsnotify_mode(struct file *file)
* watching for pre-content events on *this* file.
*/
mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
- if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) &&
- !fsnotify_object_watched(d_inode(dentry), mnt_mask,
- FSNOTIFY_PRE_CONTENT_EVENTS))) {
- file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
+ if (unlikely(fsnotify_object_watched(d_inode(dentry), mnt_mask,
+ FSNOTIFY_PRE_CONTENT_EVENTS))) {
return;
}
/* Even parent is not watching for pre-content events on this file? */
- parent = dget_parent(dentry);
- p_mask = fsnotify_inode_watches_children(d_inode(parent));
- dput(parent);
+ p_mask = 0;
+ if (unlikely(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) {
+ parent = dget_parent(dentry);
+ p_mask = fsnotify_inode_watches_children(d_inode(parent));
+ dput(parent);
+ }
if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
return;
Thanks,
Amir.
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH v9 00/19] fanotify: add pre-content hooks
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
` (18 preceding siblings ...)
2024-11-21 11:22 ` [PATCH 19/19] fs: enable pre-content events on supported file systems Jan Kara
@ 2024-11-21 16:06 ` Amir Goldstein
19 siblings, 0 replies; 23+ messages in thread
From: Amir Goldstein @ 2024-11-21 16:06 UTC (permalink / raw)
To: Jan Kara
Cc: linux-fsdevel, Josef Bacik, brauner, Linus Torvalds, Al Viro,
linux-xfs, linux-btrfs, linux-ext4, linux-mm
On Thu, Nov 21, 2024 at 12:22 PM Jan Kara <jack@suse.cz> wrote:
>
> I'm posting here the series I'm currently carrying in my tree [1]. The changes
> from v8 Josef posted are not huge but big enough that I think it's worth a
> repost. Unless somebody speaks up, the plan is to merge into fsnotify branch
> after the merge window closes.
>
> [1] https://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs.git/log/?h=fsnotify_hsm
>
> v8:
> https://lore.kernel.org/all/cover.1731684329.git.josef@toxicpanda.com
> v7:
> https://lore.kernel.org/linux-fsdevel/cover.1731433903.git.josef@toxicpanda.com/
> v6:
> https://lore.kernel.org/linux-fsdevel/cover.1731355931.git.josef@toxicpanda.com/
> v5:
> https://lore.kernel.org/linux-fsdevel/cover.1725481503.git.josef@toxicpanda.com/
> v4:
> https://lore.kernel.org/linux-fsdevel/cover.1723670362.git.josef@toxicpanda.com/
> v3:
> https://lore.kernel.org/linux-fsdevel/cover.1723228772.git.josef@toxicpanda.com/
> v2:
> https://lore.kernel.org/linux-fsdevel/cover.1723144881.git.josef@toxicpanda.com/
> v1:
> https://lore.kernel.org/linux-fsdevel/cover.1721931241.git.josef@toxicpanda.com/
>
> v8->v9:
> - fix DAX fault handling for ext4 & xfs
> - rework FMODE_ constants a bit to keep FMODE_NONOTIFY a single bit
> - move file_set_fsnotify_mode() out of line as it's quite big
> - fold fsnotify_file_object_watched() into the single caller
I tested this with my new test cases on LTP branch fan_hsm.
One test broke, I posted a suggested fix for patch 3/19.
> - use explicit f_mode checks instead of fsnotify_file_has_pre_content_watches()
> - fix compilation breakage with CONFIG_NOMMU
> - fixed up some changelogs
>
Other than patch 3, all looks good to me.
Thanks,
Amir.
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open
2024-11-21 16:01 ` Amir Goldstein
@ 2024-11-22 10:58 ` Jan Kara
0 siblings, 0 replies; 23+ messages in thread
From: Jan Kara @ 2024-11-22 10:58 UTC (permalink / raw)
To: Amir Goldstein
Cc: Jan Kara, linux-fsdevel, Josef Bacik, brauner, Linus Torvalds,
Al Viro, linux-xfs, linux-btrfs, linux-ext4, linux-mm
On Thu 21-11-24 17:01:13, Amir Goldstein wrote:
> On Thu, Nov 21, 2024 at 12:22 PM Jan Kara <jack@suse.cz> wrote:
> >
> > From: Amir Goldstein <amir73il@gmail.com>
> >
> > So far, we set FMODE_NONOTIFY_ flags at open time if we know that there
> > are no permission event watchers at all on the filesystem, but lack of
> > FMODE_NONOTIFY_ flags does not mean that the file is actually watched.
> >
> > For pre-content events, it is possible to optimize things so that we
> > don't bother trying to send pre-content events if file was not watched
> > (through sb, mnt, parent or inode itself) on open. Set FMODE_NONOTIFY_
> > flags according to that.
> >
> > Signed-off-by: Amir Goldstein <amir73il@gmail.com>
> > Signed-off-by: Jan Kara <jack@suse.cz>
> > Link: https://patch.msgid.link/2ddcc9f8d1fde48d085318a6b5a889289d8871d8.1731684329.git.josef@toxicpanda.com
> > ---
> > fs/notify/fsnotify.c | 27 +++++++++++++++++++++++++--
> > include/linux/fsnotify_backend.h | 3 +++
> > 2 files changed, 28 insertions(+), 2 deletions(-)
> >
> > diff --git a/fs/notify/fsnotify.c b/fs/notify/fsnotify.c
> > index 569ec356e4ce..dd1dffd89fd6 100644
> > --- a/fs/notify/fsnotify.c
> > +++ b/fs/notify/fsnotify.c
> > @@ -193,7 +193,7 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,
> > return mask & marks_mask;
> > }
> >
> > -/* Are there any inode/mount/sb objects that are interested in this event? */
> > +/* Are there any inode/mount/sb objects that watch for these events? */
> > static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
> > __u32 mask)
> > {
> > @@ -632,7 +632,9 @@ EXPORT_SYMBOL_GPL(fsnotify);
> > */
> > void file_set_fsnotify_mode(struct file *file)
> > {
> > - struct super_block *sb = file->f_path.dentry->d_sb;
> > + struct dentry *dentry = file->f_path.dentry, *parent;
> > + struct super_block *sb = dentry->d_sb;
> > + __u32 mnt_mask, p_mask;
> >
> > /* Is it a file opened by fanotify? */
> > if (FMODE_FSNOTIFY_NONE(file->f_mode))
> > @@ -658,6 +660,27 @@ void file_set_fsnotify_mode(struct file *file)
> > file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> > return;
> > }
>
> This was lost in translation:
>
> @@ -672,8 +672,10 @@ void file_set_fsnotify_mode(struct file *file)
> /*
> * If there are permission event watchers but no pre-content event
> * watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that.
> + * Pre-content events are only reported for regular files and dirs.
> */
> - if (likely(!fsnotify_sb_has_priority_watchers(sb,
> + if ((!d_is_dir(dentry) && !d_is_reg(dentry)) ||
> + likely(!fsnotify_sb_has_priority_watchers(sb,
> FSNOTIFY_PRIO_PRE_CONTENT))) {
> file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> return;
Right. Specifically the (!d_is_dir(dentry) && !d_is_reg(dentry)) got lost,
likely when I moved stuff between different commits and then was resolving
patch conflicts. I'll add it back. Thanks for finding this!
> > +
> > + /*
> > + * OK, there are some pre-content watchers. Check if anybody can be
> > + * watching for pre-content events on *this* file.
> > + */
> > + mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
> > + if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) &&
> > + !fsnotify_object_watched(d_inode(dentry), mnt_mask,
> > + FSNOTIFY_PRE_CONTENT_EVENTS))) {
> > + file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> > + return;
> > + }
> > +
> > + /* Even parent is not watching for pre-content events on this file? */
> > + parent = dget_parent(dentry);
> > + p_mask = fsnotify_inode_watches_children(d_inode(parent));
> > + dput(parent);
> > + if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
> > + file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> > + return;
> > + }
>
> inlining broke the logic and your branch fails the new PRE_ACCESS
> test cases of fanotify03 LTP test (now pushed to branch fan_hsm).
>
> Specifically in the test case that fails, parent is not watching and
> inode is watching pre-content and your code gets to the p_mask
> test and marks this file as no-pre-content watchers.
>
> This passes the test:
>
> @@ -684,17 +686,18 @@ void file_set_fsnotify_mode(struct file *file)
> * watching for pre-content events on *this* file.
> */
> mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
> - if (likely(!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) &&
> - !fsnotify_object_watched(d_inode(dentry), mnt_mask,
> - FSNOTIFY_PRE_CONTENT_EVENTS))) {
> - file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> + if (unlikely(fsnotify_object_watched(d_inode(dentry), mnt_mask,
> + FSNOTIFY_PRE_CONTENT_EVENTS))) {
> return;
> }
>
> /* Even parent is not watching for pre-content events on this file? */
> - parent = dget_parent(dentry);
> - p_mask = fsnotify_inode_watches_children(d_inode(parent));
> - dput(parent);
> + p_mask = 0;
> + if (unlikely(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED)) {
> + parent = dget_parent(dentry);
> + p_mask = fsnotify_inode_watches_children(d_inode(parent));
> + dput(parent);
> + }
> if (!(p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
> file->f_mode |= FMODE_NONOTIFY | FMODE_NONOTIFY_PERM;
> return;
Right. I've fixed that up. Thanks!
LTP from your fan_hsm branch is now passing so I've re-pushed the current
state to my fsnotify_hsm branch.
Honza
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 23+ messages in thread
end of thread, other threads:[~2024-11-22 10:58 UTC | newest]
Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-21 11:21 [PATCH v9 00/19] fanotify: add pre-content hooks Jan Kara
2024-11-21 11:22 ` [PATCH 01/19] fs: get rid of __FMODE_NONOTIFY kludge Jan Kara
2024-11-21 11:22 ` [PATCH 02/19] fsnotify: opt-in for permission events at file open time Jan Kara
2024-11-21 11:22 ` [PATCH 03/19] fsnotify: check if file is actually being watched for pre-content events on open Jan Kara
2024-11-21 16:01 ` Amir Goldstein
2024-11-22 10:58 ` Jan Kara
2024-11-21 11:22 ` [PATCH 04/19] fanotify: don't skip extra event info if no info_mode is set Jan Kara
2024-11-21 11:22 ` [PATCH 05/19] fanotify: rename a misnamed constant Jan Kara
2024-11-21 11:22 ` [PATCH 06/19] fanotify: reserve event bit of deprecated FAN_DIR_MODIFY Jan Kara
2024-11-21 11:22 ` [PATCH 07/19] fsnotify: introduce pre-content permission events Jan Kara
2024-11-21 11:22 ` [PATCH 08/19] fsnotify: pass optional file access range in pre-content event Jan Kara
2024-11-21 11:22 ` [PATCH 09/19] fsnotify: generate pre-content permission event on truncate Jan Kara
2024-11-21 11:22 ` [PATCH 10/19] fanotify: introduce FAN_PRE_ACCESS permission event Jan Kara
2024-11-21 11:22 ` [PATCH 11/19] fanotify: report file range info with pre-content events Jan Kara
2024-11-21 11:22 ` [PATCH 12/19] fanotify: allow to set errno in FAN_DENY permission response Jan Kara
2024-11-21 11:22 ` [PATCH 13/19] fanotify: disable readahead if we have pre-content watches Jan Kara
2024-11-21 11:22 ` [PATCH 14/19] mm: don't allow huge faults for files with pre content watches Jan Kara
2024-11-21 11:22 ` [PATCH 15/19] fsnotify: generate pre-content permission event on page fault Jan Kara
2024-11-21 11:22 ` [PATCH 16/19] xfs: add pre-content fsnotify hook for DAX faults Jan Kara
2024-11-21 11:22 ` [PATCH 17/19] btrfs: disable defrag on pre-content watched files Jan Kara
2024-11-21 11:22 ` [PATCH 18/19] ext4: add pre-content fsnotify hook for DAX faults Jan Kara
2024-11-21 11:22 ` [PATCH 19/19] fs: enable pre-content events on supported file systems Jan Kara
2024-11-21 16:06 ` [PATCH v9 00/19] fanotify: add pre-content hooks Amir Goldstein
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox