Skip to content

Commit 066e053

Browse files
amir73iljankara
authored andcommitted
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
1 parent 0fed89a commit 066e053

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

include/linux/fsnotify.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
170170
return fsnotify_path(&file->f_path, FS_ACCESS_PERM);
171171
}
172172

173+
/*
174+
* fsnotify_mmap_perm - permission hook before mmap of file range
175+
*/
176+
static inline int fsnotify_mmap_perm(struct file *file, int prot,
177+
const loff_t off, size_t len)
178+
{
179+
/*
180+
* mmap() generates only pre-content events.
181+
*/
182+
if (!file || likely(!FMODE_FSNOTIFY_HSM(file->f_mode)))
183+
return 0;
184+
185+
return fsnotify_pre_content(&file->f_path, &off, len);
186+
}
187+
173188
/*
174189
* fsnotify_truncate_perm - permission hook before file truncate
175190
*/
@@ -223,6 +238,12 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
223238
return 0;
224239
}
225240

241+
static inline int fsnotify_mmap_perm(struct file *file, int prot,
242+
const loff_t off, size_t len)
243+
{
244+
return 0;
245+
}
246+
226247
static inline int fsnotify_truncate_perm(const struct path *path, loff_t length)
227248
{
228249
return 0;

mm/util.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/processor.h>
2424
#include <linux/sizes.h>
2525
#include <linux/compat.h>
26+
#include <linux/fsnotify.h>
2627

2728
#include <linux/uaccess.h>
2829

@@ -569,6 +570,8 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
569570
LIST_HEAD(uf);
570571

571572
ret = security_mmap_file(file, prot, flag);
573+
if (!ret)
574+
ret = fsnotify_mmap_perm(file, prot, pgoff >> PAGE_SHIFT, len);
572575
if (!ret) {
573576
if (mmap_write_lock_killable(mm))
574577
return -EINTR;

0 commit comments

Comments
 (0)