Skip to content

Commit 0d4c4d4

Browse files
amir73iljankara
authored andcommitted
fsnotify: optimize FMODE_NONOTIFY_PERM for the common cases
The most unlikely watched permission event is FAN_ACCESS_PERM, because at the time that it was introduced there were no evictable ignore mark, so subscribing to FAN_ACCESS_PERM would have incured a very high overhead. Yet, when we set the fmode to FMODE_NOTIFY_HSM(), we never skip trying to send FAN_ACCESS_PERM, which is almost always a waste of cycles. We got to this logic because of bundling FAN_OPEN*_PERM and FAN_ACCESS_PERM in the same category and because FAN_OPEN_PERM is a commonly used event. By open coding fsnotify_open_perm() in fsnotify_open_perm_and_set_mode(), we no longer need to regard FAN_OPEN*_PERM when calculating fmode. This leaves the case of having pre-content events and not having any other permission event in the object masks a more likely case than the other way around. Rework the fmode macros and code so that their meaning now refers only to hooks on an already open file: - FMODE_NOTIFY_NONE() skip all events - FMODE_NOTIFY_ACCESS_PERM() send all permission events including FAN_ACCESS_PERM - FMODE_NOTIFY_HSM() send pre-content permission events Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 08da98e commit 0d4c4d4

File tree

3 files changed

+53
-61
lines changed

3 files changed

+53
-61
lines changed

fs/notify/fsnotify.c

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ static bool fsnotify_event_needs_parent(struct inode *inode, __u32 mnt_mask,
199199
}
200200

201201
/* Are there any inode/mount/sb objects that watch for these events? */
202-
static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
203-
__u32 mask)
202+
static inline __u32 fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
203+
__u32 mask)
204204
{
205205
__u32 marks_mask = READ_ONCE(inode->i_fsnotify_mask) | mnt_mask |
206206
READ_ONCE(inode->i_sb->s_fsnotify_mask);
@@ -665,7 +665,7 @@ int fsnotify_open_perm_and_set_mode(struct file *file)
665665
{
666666
struct dentry *dentry = file->f_path.dentry, *parent;
667667
struct super_block *sb = dentry->d_sb;
668-
__u32 mnt_mask, p_mask;
668+
__u32 mnt_mask, p_mask = 0;
669669

670670
/* Is it a file opened by fanotify? */
671671
if (FMODE_FSNOTIFY_NONE(file->f_mode))
@@ -683,45 +683,60 @@ int fsnotify_open_perm_and_set_mode(struct file *file)
683683
}
684684

685685
/*
686-
* If there are permission event watchers but no pre-content event
687-
* watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that.
686+
* OK, there are some permission event watchers. Check if anybody is
687+
* watching for permission events on *this* file.
688688
*/
689-
if ((!d_is_dir(dentry) && !d_is_reg(dentry)) ||
690-
likely(!fsnotify_sb_has_priority_watchers(sb,
691-
FSNOTIFY_PRIO_PRE_CONTENT))) {
692-
file_set_fsnotify_mode(file, FMODE_NONOTIFY |
693-
FMODE_NONOTIFY_PERM);
694-
goto open_perm;
689+
mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
690+
p_mask = fsnotify_object_watched(d_inode(dentry), mnt_mask,
691+
ALL_FSNOTIFY_PERM_EVENTS);
692+
if (dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) {
693+
parent = dget_parent(dentry);
694+
p_mask |= fsnotify_inode_watches_children(d_inode(parent));
695+
dput(parent);
695696
}
696697

697698
/*
698-
* OK, there are some pre-content watchers. Check if anybody is
699-
* watching for pre-content events on *this* file.
699+
* Legacy FAN_ACCESS_PERM events have very high performance overhead,
700+
* so unlikely to be used in the wild. If they are used there will be
701+
* no optimizations at all.
700702
*/
701-
mnt_mask = READ_ONCE(real_mount(file->f_path.mnt)->mnt_fsnotify_mask);
702-
if (unlikely(fsnotify_object_watched(d_inode(dentry), mnt_mask,
703-
FSNOTIFY_PRE_CONTENT_EVENTS))) {
704-
/* Enable pre-content events */
703+
if (unlikely(p_mask & FS_ACCESS_PERM)) {
704+
/* Enable all permission and pre-content events */
705705
file_set_fsnotify_mode(file, 0);
706706
goto open_perm;
707707
}
708708

709-
/* Is parent watching for pre-content events on this file? */
710-
if (dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED) {
711-
parent = dget_parent(dentry);
712-
p_mask = fsnotify_inode_watches_children(d_inode(parent));
713-
dput(parent);
714-
if (p_mask & FSNOTIFY_PRE_CONTENT_EVENTS) {
715-
/* Enable pre-content events */
716-
file_set_fsnotify_mode(file, 0);
717-
goto open_perm;
718-
}
709+
/*
710+
* Pre-content events are only supported on regular files.
711+
* If there are pre-content event watchers and no permission access
712+
* watchers, set FMODE_NONOTIFY | FMODE_NONOTIFY_PERM to indicate that.
713+
* That is the common case with HSM service.
714+
*/
715+
if (d_is_reg(dentry) && (p_mask & FSNOTIFY_PRE_CONTENT_EVENTS)) {
716+
file_set_fsnotify_mode(file, FMODE_NONOTIFY |
717+
FMODE_NONOTIFY_PERM);
718+
goto open_perm;
719719
}
720-
/* Nobody watching for pre-content events from this file */
721-
file_set_fsnotify_mode(file, FMODE_NONOTIFY | FMODE_NONOTIFY_PERM);
720+
721+
/* Nobody watching permission and pre-content events on this file */
722+
file_set_fsnotify_mode(file, FMODE_NONOTIFY_PERM);
722723

723724
open_perm:
724-
return fsnotify_open_perm(file);
725+
/*
726+
* Send open perm events depending on object masks and regardless of
727+
* FMODE_NONOTIFY_PERM.
728+
*/
729+
if (file->f_flags & __FMODE_EXEC && p_mask & FS_OPEN_EXEC_PERM) {
730+
int ret = fsnotify_path(&file->f_path, FS_OPEN_EXEC_PERM);
731+
732+
if (ret)
733+
return ret;
734+
}
735+
736+
if (p_mask & FS_OPEN_PERM)
737+
return fsnotify_path(&file->f_path, FS_OPEN_PERM);
738+
739+
return 0;
725740
}
726741
#endif
727742

include/linux/fs.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,26 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
200200

201201
/*
202202
* The two FMODE_NONOTIFY* define which fsnotify events should not be generated
203-
* for a file. These are the possible values of (f->f_mode &
204-
* FMODE_FSNOTIFY_MASK) and their meaning:
203+
* for an open file. These are the possible values of
204+
* (f->f_mode & FMODE_FSNOTIFY_MASK) and their meaning:
205205
*
206206
* FMODE_NONOTIFY - suppress all (incl. non-permission) events.
207207
* FMODE_NONOTIFY_PERM - suppress permission (incl. pre-content) events.
208-
* FMODE_NONOTIFY | FMODE_NONOTIFY_PERM - suppress only pre-content events.
208+
* FMODE_NONOTIFY | FMODE_NONOTIFY_PERM - suppress only FAN_ACCESS_PERM.
209209
*/
210210
#define FMODE_FSNOTIFY_MASK \
211211
(FMODE_NONOTIFY | FMODE_NONOTIFY_PERM)
212212

213213
#define FMODE_FSNOTIFY_NONE(mode) \
214214
((mode & FMODE_FSNOTIFY_MASK) == FMODE_NONOTIFY)
215215
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
216-
#define FMODE_FSNOTIFY_PERM(mode) \
216+
#define FMODE_FSNOTIFY_HSM(mode) \
217217
((mode & FMODE_FSNOTIFY_MASK) == 0 || \
218218
(mode & FMODE_FSNOTIFY_MASK) == (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM))
219-
#define FMODE_FSNOTIFY_HSM(mode) \
219+
#define FMODE_FSNOTIFY_ACCESS_PERM(mode) \
220220
((mode & FMODE_FSNOTIFY_MASK) == 0)
221221
#else
222-
#define FMODE_FSNOTIFY_PERM(mode) 0
222+
#define FMODE_FSNOTIFY_ACCESS_PERM(mode) 0
223223
#define FMODE_FSNOTIFY_HSM(mode) 0
224224
#endif
225225

include/linux/fsnotify.h

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
147147
if (!(perm_mask & (MAY_READ | MAY_WRITE | MAY_ACCESS)))
148148
return 0;
149149

150-
if (likely(!FMODE_FSNOTIFY_PERM(file->f_mode)))
151-
return 0;
152-
153150
/*
154151
* read()/write() and other types of access generate pre-content events.
155152
*/
@@ -160,7 +157,8 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
160157
return ret;
161158
}
162159

163-
if (!(perm_mask & MAY_READ))
160+
if (!(perm_mask & MAY_READ) ||
161+
likely(!FMODE_FSNOTIFY_ACCESS_PERM(file->f_mode)))
164162
return 0;
165163

166164
/*
@@ -208,22 +206,6 @@ static inline int fsnotify_file_perm(struct file *file, int perm_mask)
208206
return fsnotify_file_area_perm(file, perm_mask, NULL, 0);
209207
}
210208

211-
/*
212-
* fsnotify_open_perm - permission hook before file open
213-
*/
214-
static inline int fsnotify_open_perm(struct file *file)
215-
{
216-
int ret;
217-
218-
if (file->f_flags & __FMODE_EXEC) {
219-
ret = fsnotify_path(&file->f_path, FS_OPEN_EXEC_PERM);
220-
if (ret)
221-
return ret;
222-
}
223-
224-
return fsnotify_path(&file->f_path, FS_OPEN_PERM);
225-
}
226-
227209
#else
228210
static inline int fsnotify_open_perm_and_set_mode(struct file *file)
229211
{
@@ -251,11 +233,6 @@ static inline int fsnotify_file_perm(struct file *file, int perm_mask)
251233
{
252234
return 0;
253235
}
254-
255-
static inline int fsnotify_open_perm(struct file *file)
256-
{
257-
return 0;
258-
}
259236
#endif
260237

261238
/*

0 commit comments

Comments
 (0)