fsnotify: add pre-content hooks on mmap()

Pre-content hooks in page faults introduces potential deadlock of HSM
handler in userspace with filesystem freezing.

The requirement with pre-content event is that for every accessed file
range an event covering at least this range will be generated at least
once before the file data is accesses.

In preparation to disabling pre-content event hooks on page faults,
add pre-content hooks at mmap() variants for the entire mmaped range,
so HSM can fill content when user requests to map a portion of the file.

Note that exec() variant also calls vm_mmap_pgoff() internally to map
code sections, so pre-content hooks are also generated in this case.

Link: https://lore.kernel.org/linux-fsdevel/7ehxrhbvehlrjwvrduoxsao5k3x4aw275patsb3krkwuq573yv@o2hskrfawbnc/
Suggested-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/20250312073852.2123409-2-amir73il@gmail.com
This commit is contained in:
Amir Goldstein 2025-03-12 08:38:47 +01:00 committed by Jan Kara
parent 0fed89a961
commit 066e053fe2
2 changed files with 24 additions and 0 deletions

View file

@ -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); return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
} }
/*
* fsnotify_mmap_perm - permission hook before mmap of file range
*/
static inline int fsnotify_mmap_perm(struct file *file, int prot,
const loff_t off, size_t len)
{
/*
* mmap() generates only pre-content events.
*/
if (!file || likely(!FMODE_FSNOTIFY_HSM(file->f_mode)))
return 0;
return fsnotify_pre_content(&file->f_path, &off, len);
}
/* /*
* fsnotify_truncate_perm - permission hook before file truncate * fsnotify_truncate_perm - permission hook before file truncate
*/ */
@ -223,6 +238,12 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
return 0; return 0;
} }
static inline int fsnotify_mmap_perm(struct file *file, int prot,
const loff_t off, size_t len)
{
return 0;
}
static inline int fsnotify_truncate_perm(const struct path *path, loff_t length) static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
{ {
return 0; return 0;

View file

@ -23,6 +23,7 @@
#include <linux/processor.h> #include <linux/processor.h>
#include <linux/sizes.h> #include <linux/sizes.h>
#include <linux/compat.h> #include <linux/compat.h>
#include <linux/fsnotify.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
@ -569,6 +570,8 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
LIST_HEAD(uf); LIST_HEAD(uf);
ret = security_mmap_file(file, prot, flag); ret = security_mmap_file(file, prot, flag);
if (!ret)
ret = fsnotify_mmap_perm(file, prot, pgoff >> PAGE_SHIFT, len);
if (!ret) { if (!ret) {
if (mmap_write_lock_killable(mm)) if (mmap_write_lock_killable(mm))
return -EINTR; return -EINTR;