Skip to contentSkip to conversation

fsnotify: fix unlink performance regression

PATCH Superseded Amir Goldstein 4 participants 21 messages May 5, 2019 – May 13, 2019

Subsystems (2)
lore
Share & tools
lore

Amir Goldstein

author reviewer
#0 lore raw

PATCH 1/1 fsnotify: fix unlink performance regression

__fsnotify_parent() has an optimization in place to avoid unneeded take_dentry_name_snapshot(). When fsnotify_nameremove() was changed not to call __fsnotify_parent(), we left out the optimization. Kernel test robot reported a 5% performance regression in concurrent unlink() workload.

---

Jan,

The linked 5.1-rc1 performance regression report came with bad timing. Not sure if Linus is planning an rc8. If not, you will probably not see this before the 5.1 release and we shall have to queue it for 5.2 and backport to stable 5.1.

I crafted the patch so it applies cleanly both to master and Al's for-next branch (there are some fsnotify changes in work.dcache).

Thanks,
Amir.

 include/linux/fsnotify.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/linux/fsnotify.h +7 -0
index 09587e2860b5..2272c8c2023c 100644
@@ -175,12 +175,19 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 		mask |= FS_ISDIR;
 
 	parent = dget_parent(dentry);
+	/* Avoid unneeded take_dentry_name_snapshot() */
+	if (!(d_inode(parent)->i_fsnotify_mask & FS_DELETE) &&
+	    !(dentry->d_sb->s_fsnotify_mask & FS_DELETE))
+		goto out_dput;
+
 	take_dentry_name_snapshot(&name, dentry);
 
 	fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
 		 name.name, 0);
 
 	release_dentry_name_snapshot(&name);
+
+out_dput:
 	dput(parent);
 }
 
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 15 lines · Amir Goldstein

__fsnotify_parent() has an optimization in place to avoid unneeded take_dentry_name_snapshot(). When fsnotify_nameremove() was changed not to call __fsnotify_parent(), we left out the optimization. Kernel test robot reported a 5% performance regression in concurrent unlink() workload.

---

Jan,

Quoted 7 lines

The linked 5.1-rc1 performance regression report came with bad timing. Not sure if Linus is planning an rc8. If not, you will probably not see this before the 5.1 release and we shall have to queue it for 5.2 and backport to stable 5.1.

I crafted the patch so it applies cleanly both to master and Al's for-next branch (there are some fsnotify changes in work.dcache).

Charming... What about rename() and matching regressions there?

author reviewer
#2 lore raw
Quoted 22 lines · Al Viro
Quoted 20 lines · Amir Goldstein

__fsnotify_parent() has an optimization in place to avoid unneeded take_dentry_name_snapshot(). When fsnotify_nameremove() was changed not to call __fsnotify_parent(), we left out the optimization. Kernel test robot reported a 5% performance regression in concurrent unlink() workload.

---

Jan,

The linked 5.1-rc1 performance regression report came with bad timing. Not sure if Linus is planning an rc8. If not, you will probably not see this before the 5.1 release and we shall have to queue it for 5.2 and backport to stable 5.1.

Quoted 4 lines
Quoted 2 lines

I crafted the patch so it applies cleanly both to master and Al's for-next branch (there are some fsnotify changes in work.dcache).

Charming... What about rename() and matching regressions there?

rename() and create() do not take_dentry_name_snapshot(), because they are called with parent inode lock held.
I have made an analysis of callers to d_delete() and found that all callers either hold parent inode lock or name is stable for another reason:
https://lore.kernel.org/linux-fsdevel/[email protected]/

But Jan preferred to keep take_dentry_name_snapshot() to be safe. I think the right thing to do is assert that parent inode is locked or no rename op in d_delete() and take the lock in ceph/ocfs2 to conform to the standard.

If that sounds good to you, I will follow up with a patch for next and then remove take_dentry_name_snapshot() from fsnotify_nameremove() hook.

Thanks,
Amir.

maintainer
#3 lore raw
Quoted 8 lines · Amir Goldstein

I have made an analysis of callers to d_delete() and found that all callers either hold parent inode lock or name is stable for another reason:
https://lore.kernel.org/linux-fsdevel/20190104090357.GD22409(a)quack2.suse.cz/

But Jan preferred to keep take_dentry_name_snapshot() to be safe. I think the right thing to do is assert that parent inode is locked or no rename op in d_delete() and take the lock in ceph/ocfs2 to conform to the standard.

Any messing with the locking in ceph_fill_trace() would have to come with very detailed proof of correctness, convincingly stable wrt future changes in ceph...

author reviewer
#4 lore raw
Quoted 7 lines · Al Viro
Quoted 4 lines · Amir Goldstein

I have made an analysis of callers to d_delete() and found that all callers either hold parent inode lock or name is stable for another reason:
https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 8 lines
Quoted 4 lines

But Jan preferred to keep take_dentry_name_snapshot() to be safe. I think the right thing to do is assert that parent inode is locked or no rename op in d_delete() and take the lock in ceph/ocfs2 to conform to the standard.

Any messing with the locking in ceph_fill_trace() would have to come with very detailed proof of correctness, convincingly stable wrt future changes in ceph...

Yeh... Is there any other way to assert that d_name is stable in d_delete()? My original rule was:
WARN_ON_ONCE(!inode_is_locked(dir) && dir->i_op->rename &&

      !(dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE));

In the hope that declaring FS_RENAME_DOES_D_MOVE means fs knows what it is doing...

Thanks,
Amir.

author reviewer
#5 lore raw
In reply to Al Viro
Quoted 7 lines · Al Viro
Quoted 4 lines · Amir Goldstein

I have made an analysis of callers to d_delete() and found that all callers either hold parent inode lock or name is stable for another reason:
https://lore.kernel.org/linux-fsdevel/[email protected]/

Quoted 8 lines
Quoted 4 lines

But Jan preferred to keep take_dentry_name_snapshot() to be safe. I think the right thing to do is assert that parent inode is locked or no rename op in d_delete() and take the lock in ceph/ocfs2 to conform to the standard.

Any messing with the locking in ceph_fill_trace() would have to come with very detailed proof of correctness, convincingly stable wrt future changes in ceph...

OK. What do you have to say about this statement?

Because fsnotify_nameremove() is called from d_delete() with negative or unhashed dentry, d_move() is not expected on this dentry, so it is safe to use d_parent/d_name without take_dentry_name_snapshot().

I assume it is not correct, but cannot figure out why. Under what circumstances is d_move() expected to move an unhashed dentry and hash it?

If this is not expected then wouldn't we be better off with:
@@ -2774,9 +2774,9 @@ static void __d_move(struct dentry *dentry, struct dentry *target,

        /* unhash both */
-       if (!d_unhashed(dentry))
+       if (!WARN_ON(d_unhashed(dentry)))
                ___d_drop(dentry);
-       if (!d_unhashed(target))
+       if (!WARN_ON(d_unhashed(target)))
                ___d_drop(target);

And then we can get rid of take_dentry_name_snapshot() in fsnotify_nameremove() and leave an assertion instead:

+ /* Proof of stability of d_parent and d_name */
+ WARN_ON_ONCE(d_inode(dentry) && !d_unhashed(dentry));
+

My other thought is why is fsnotify_nameremove() in d_delete() and not in vfs_unlink()/vfs_rmdir() under parent inode lock like the rest of the fsnotify_create/fsnotify_move hooks?

In what case would we need the fsnotify event that is not coming from vfs_unlink()/vfs_rmdir()?

Thanks,
Amir.

maintainer
#6 lore raw
Quoted 9 lines · Amir Goldstein

OK. What do you have to say about this statement?

Because fsnotify_nameremove() is called from d_delete() with negative or unhashed dentry, d_move() is not expected on this dentry, so it is safe to use d_parent/d_name without take_dentry_name_snapshot().

I assume it is not correct, but cannot figure out why. Under what circumstances is d_move() expected to move an unhashed dentry and hash it?

For starters, d_splice_alias() picking an exising alias for given directory inode.

Quoted 6 lines

My other thought is why is fsnotify_nameremove() in d_delete() and not in vfs_unlink()/vfs_rmdir() under parent inode lock like the rest of the fsnotify_create/fsnotify_move hooks?

In what case would we need the fsnotify event that is not coming from vfs_unlink()/vfs_rmdir()?

*snort*

You can thank those who whine about notifications on sysfs/devpts/whatnot. Go talk to them if you wish, but don't ask me to translate what you'll get into something coherent - I'd never been able to.

author reviewer
#7 lore raw
Quoted 8 lines · Al Viro
Quoted 6 lines · Amir Goldstein

OK. What do you have to say about this statement?

Because fsnotify_nameremove() is called from d_delete() with negative or unhashed dentry, d_move() is not expected on this dentry, so it is safe to use d_parent/d_name without take_dentry_name_snapshot().

Quoted 6 lines
Quoted 3 lines

I assume it is not correct, but cannot figure out why. Under what circumstances is d_move() expected to move an unhashed dentry and hash it?

For starters, d_splice_alias() picking an exising alias for given directory inode.

Ok. But seeing that we are already in d_delete() said directory is already IS_DEADDIR, so that can be added to the assertion that proves the stability of d_name.
Are there any other cases? weird filesystems?

Quoted 5 lines
Quoted 5 lines

My other thought is why is fsnotify_nameremove() in d_delete() and not in vfs_unlink()/vfs_rmdir() under parent inode lock like the rest of the fsnotify_create/fsnotify_move hooks?

Quoted 8 lines
Quoted 2 lines

In what case would we need the fsnotify event that is not coming from vfs_unlink()/vfs_rmdir()?

*snort*

You can thank those who whine about notifications on sysfs/devpts/whatnot. Go talk to them if you wish, but don't ask me to translate what you'll get into something coherent - I'd never been able to.

I see. So all of those fs that are interested in notifications already have fsnotify_create()/fsnotify_move() calls in them.
There are only 5 of them: binderfs, debugfs, devpts, tracefs, sunrpc. It would be easier and less convoluted to also add the fsnotify_nameremove() explicit calls in those fs.
With those fs either d_name is inherently stable or (debugfs) locks on parent are taken properly.

But d_delete() effectively provides something else. If provides "remote server change notifications" for clustered/networking filesystems when server invalidates the local dentry. This is not a feature that is guarantied by fsnotify.
notifications will be delivered randomly based on existence of local entry in dcache. Only networking fs that provide proper remote notifications (cifs is interested in doing that) should really be calling the fsnofity hooks.

Of course, we remove the random remote notifications from clustered/network fs, there is bound to be someone unhappy. Urgh! out of those fs, only ocfs2 calls fsnotify_create(). If it is deemed important, can add fsnotify_nameremove()
to ocfs2 as well.

Dare we make this change and see if people shout?
It's easy to add the random remote fsnotify_nameremove() calls per request for a filesystems that want it.

Thanks,
Amir.

#8 lore raw
In reply to Amir Goldstein
I love your patch! Yet something to improve:

Hi Amir,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.1-rc7 next-20190503]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Amir-Goldstein/fsnotify-fix-unlink-performance-regression/20190505-233115
config: riscv-tinyconfig (attached as .config)
compiler: riscv64-linux-gcc (GCC) 8.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=8.1.0 make.cross ARCH=riscv

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from fs///attr.c:15:
include/linux/fsnotify.h: In function 'fsnotify_nameremove':

>> include/linux/fsnotify.h:179:23: error: 'struct inode' has no member named 'i_fsnotify_mask'
     if (!(d_inode(parent)->i_fsnotify_mask & FS_DELETE) &&
                          ^~
>> include/linux/fsnotify.h:180:20: error: 'struct super_block' has no member named 's_fsnotify_mask'
         !(dentry->d_sb->s_fsnotify_mask & FS_DELETE))
                       ^~

vim +179 include/linux/fsnotify.h

153
154 /*
155 * fsnotify_nameremove - a filename was removed from a directory
156 *
157 * This is mostly called under parent vfs inode lock so name and
158 * dentry->d_parent should be stable. However there are some corner cases where
159 * inode lock is not held. So to be on the safe side and be reselient to future
160 * callers and out of tree users of d_delete(), we do not assume that d_parent
161 * and d_name are stable and we use dget_parent() and
162 * take_dentry_name_snapshot() to grab stable references.
163 */
164 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
165 {
166 struct dentry *parent;
167 struct name_snapshot name;
168 __u32 mask = FS_DELETE;
169
170 /* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
171 if (IS_ROOT(dentry))
172 return;
173
174 if (isdir)
175 mask |= FS_ISDIR;
176
177 parent = dget_parent(dentry);
178 /* Avoid unneeded take_dentry_name_snapshot() */

 > 179		if (!(d_inode(parent)->i_fsnotify_mask & FS_DELETE) &&
 > 180		    !(dentry->d_sb->s_fsnotify_mask & FS_DELETE))

181 goto out_dput;
182
183 take_dentry_name_snapshot(&name, dentry);
184
185 fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
186 name.name, 0);
187
188 release_dentry_name_snapshot(&name);
189
190 out_dput:
191 dput(parent);
192 }
193

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

author reviewer
#9 lore raw
Quoted 23 lines · kbuild test robot

Hi Amir,

I love your patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.1-rc7 next-20190503]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Amir-Goldstein/fsnotify-fix-unlink-performance-regression/20190505-233115
config: riscv-tinyconfig (attached as .config)
compiler: riscv64-linux-gcc (GCC) 8.1.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=8.1.0 make.cross ARCH=riscv

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

Quoted 9 lines

In file included from fs///attr.c:15:
include/linux/fsnotify.h: In function 'fsnotify_nameremove':

Quoted 1 line
Quoted 1 line

include/linux/fsnotify.h:179:23: error: 'struct inode' has no member named 'i_fsnotify_mask'

     if (!(d_inode(parent)->i_fsnotify_mask & FS_DELETE) &&
                          ^~
Quoted 1 line
Quoted 1 line

include/linux/fsnotify.h:180:20: error: 'struct super_block' has no member named 's_fsnotify_mask'

         !(dentry->d_sb->s_fsnotify_mask & FS_DELETE))
                       ^~

Crap! forgot these wrappers are not NOOPed without CONFIG_FSNOTIFY. It is so annoying to fix bugs in code that should not exist.

In d_delete() at this point, dentry is either negative or inode->i_nlink which accounts for this name should be decremented.
If d_move() was possible on this dentry, bad things would happen.

I really wish I could just drop this take_dentry_name_snapshot() and leave the WARN_ON() I suggested instead...
For now will just send an unbroken patch.

Thanks,
Amir.

author reviewer
#10 lore raw

PATCH v2 1/1 fsnotify: fix unlink performance regression

__fsnotify_parent() has an optimization in place to avoid unneeded take_dentry_name_snapshot(). When fsnotify_nameremove() was changed not to call __fsnotify_parent(), we left out the optimization. Kernel test robot reported a 5% performance regression in concurrent unlink() workload.

Modify __fsnotify_parent() so that it can be called also to report directory modififcation events and use it from fsnotify_nameremove().

---

Jan,

A nicer approach reusing __fsnotify_parent() instead of copying code from it.

This version does not apply cleanly to Al's for-next branch (there are some fsnotify changes in work.dcache). The conflict is trivial and resolved on my fsnotify branch [1].

Thanks,
Amir.

Changes since v1:
- Fix build without CONFIG_FSNOTIFY
- Use __fsnotify_parent() for reporting FS_DELETE

[1] https://github.com/amir73il/linux/commits/fsnotify

 fs/notify/fsnotify.c     | 22 +++++++++++-----------
 include/linux/fsnotify.h | 15 +++------------
 2 files changed, 14 insertions(+), 23 deletions(-)
fs/notify/fsnotify.c +11 -11
index df06f3da166c..265b726d6e8d 100644
@@ -151,31 +151,31 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)
 	spin_unlock(&inode->i_lock);
 }
 
-/* Notify this dentry's parent about a child's events. */
+/*
+ * Notify this dentry's parent about an event and make sure that name is stable.
+ * Events "on child" are only reported if parent is watching.
+ * Directory modification events are also reported if super block is watching.
+ */
 int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask)
 {
 	struct dentry *parent;
 	struct inode *p_inode;
 	int ret = 0;
+	bool on_child = (mask & FS_EVENT_ON_CHILD);
+	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
 
-	if (!dentry)
-		dentry = path->dentry;
-
-	if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
+	if (on_child && !(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
 		return 0;
 
 	parent = dget_parent(dentry);
 	p_inode = parent->d_inode;
 
-	if (unlikely(!fsnotify_inode_watches_children(p_inode))) {
+	if (on_child && unlikely(!fsnotify_inode_watches_children(p_inode))) {
 		__fsnotify_update_child_dentry_flags(p_inode);
-	} else if (p_inode->i_fsnotify_mask & mask & ALL_FSNOTIFY_EVENTS) {
+	} else if ((p_inode->i_fsnotify_mask & test_mask) ||
+		   (!on_child && (dentry->d_sb->s_fsnotify_mask & test_mask))) {
 		struct name_snapshot name;
 
-		/* we are notifying a parent so come up with the new mask which
-		 * specifies these are events which came from a child. */
-		mask |= FS_EVENT_ON_CHILD;
-
 		take_dentry_name_snapshot(&name, dentry);
 		if (path)
 			ret = fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
include/linux/fsnotify.h +3 -12
index 09587e2860b5..8641bf9a1eda 100644
@@ -37,7 +37,7 @@ static inline int fsnotify_parent(const struct path *path,
 	if (!dentry)
 		dentry = path->dentry;
 
-	return __fsnotify_parent(path, dentry, mask);
+	return __fsnotify_parent(path, dentry, mask | FS_EVENT_ON_CHILD);
 }
 
 /*
@@ -158,13 +158,11 @@ static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
  * dentry->d_parent should be stable. However there are some corner cases where
  * inode lock is not held. So to be on the safe side and be reselient to future
  * callers and out of tree users of d_delete(), we do not assume that d_parent
- * and d_name are stable and we use dget_parent() and
+ * and d_name are stable and we use __fsnotify_parent() to
  * take_dentry_name_snapshot() to grab stable references.
  */
 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 {
-	struct dentry *parent;
-	struct name_snapshot name;
 	__u32 mask = FS_DELETE;
 
 	/* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
@@ -174,14 +172,7 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 	if (isdir)
 		mask |= FS_ISDIR;
 
-	parent = dget_parent(dentry);
-	take_dentry_name_snapshot(&name, dentry);
-
-	fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
-		 name.name, 0);
-
-	release_dentry_name_snapshot(&name);
-	dput(parent);
+	__fsnotify_parent(NULL, dentry, mask);
 }
 
 /*
Signature · 1 line
2.17.1
maintainer
#11 lore raw
Quoted 16 lines · Amir Goldstein

__fsnotify_parent() has an optimization in place to avoid unneeded take_dentry_name_snapshot(). When fsnotify_nameremove() was changed not to call __fsnotify_parent(), we left out the optimization. Kernel test robot reported a 5% performance regression in concurrent unlink() workload.

Modify __fsnotify_parent() so that it can be called also to report directory modififcation events and use it from fsnotify_nameremove().

---

Quoted 8 lines

Jan,

A nicer approach reusing __fsnotify_parent() instead of copying code from it.

This version does not apply cleanly to Al's for-next branch (there are some fsnotify changes in work.dcache). The conflict is trivial and resolved on my fsnotify branch [1].

Hum, let me check if I understand the situation right. We've changed fsnotify_nameremove() to not use fsnotify_parent() as we don't want to report FS_EVENT_ON_CHILD with it anymore. We should use fsnotify_dirent() as for all other directory event notification handlers but that's problematic due to different locking context and possible instability of parent.

Honestly I don't like the patch below much. How we are notifying parent without sending FS_EVENT_ON_CHILD and modify behavior based on that flag just looks subtle. So I'd rather move the fsnotify call to vfs_unlink(), vfs_rmdir(), simple_unlink(), simple_rmdir(), and then those few callers of d_delete() that remain as you suggest elsewhere in this thread. And then we get more consistent context for fsnotify_nameremove() and could just use fsnotify_dirent().

Honza

Quoted 94 lines
fs/notify/fsnotify.c +11 -11
index df06f3da166c..265b726d6e8d 100644
@@ -151,31 +151,31 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)
 	spin_unlock(&inode->i_lock);
 }
 
-/* Notify this dentry's parent about a child's events. */
+/*
+ * Notify this dentry's parent about an event and make sure that name is stable.
+ * Events "on child" are only reported if parent is watching.
+ * Directory modification events are also reported if super block is watching.
+ */
 int __fsnotify_parent(const struct path *path, struct dentry *dentry, __u32 mask)
 {
 	struct dentry *parent;
 	struct inode *p_inode;
 	int ret = 0;
+	bool on_child = (mask & FS_EVENT_ON_CHILD);
+	__u32 test_mask = (mask & ALL_FSNOTIFY_EVENTS);
 
-	if (!dentry)
-		dentry = path->dentry;
-
-	if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
+	if (on_child && !(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
 		return 0;
 
 	parent = dget_parent(dentry);
 	p_inode = parent->d_inode;
 
-	if (unlikely(!fsnotify_inode_watches_children(p_inode))) {
+	if (on_child && unlikely(!fsnotify_inode_watches_children(p_inode))) {
 		__fsnotify_update_child_dentry_flags(p_inode);
-	} else if (p_inode->i_fsnotify_mask & mask & ALL_FSNOTIFY_EVENTS) {
+	} else if ((p_inode->i_fsnotify_mask & test_mask) ||
+		   (!on_child && (dentry->d_sb->s_fsnotify_mask & test_mask))) {
 		struct name_snapshot name;
 
-		/* we are notifying a parent so come up with the new mask which
-		 * specifies these are events which came from a child. */
-		mask |= FS_EVENT_ON_CHILD;
-
 		take_dentry_name_snapshot(&name, dentry);
 		if (path)
 			ret = fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
include/linux/fsnotify.h +3 -12
index 09587e2860b5..8641bf9a1eda 100644
@@ -37,7 +37,7 @@ static inline int fsnotify_parent(const struct path *path,
 	if (!dentry)
 		dentry = path->dentry;
 
-	return __fsnotify_parent(path, dentry, mask);
+	return __fsnotify_parent(path, dentry, mask | FS_EVENT_ON_CHILD);
 }
 
 /*
@@ -158,13 +158,11 @@ static inline void fsnotify_vfsmount_delete(struct vfsmount *mnt)
  * dentry->d_parent should be stable. However there are some corner cases where
  * inode lock is not held. So to be on the safe side and be reselient to future
  * callers and out of tree users of d_delete(), we do not assume that d_parent
- * and d_name are stable and we use dget_parent() and
+ * and d_name are stable and we use __fsnotify_parent() to
  * take_dentry_name_snapshot() to grab stable references.
  */
 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 {
-	struct dentry *parent;
-	struct name_snapshot name;
 	__u32 mask = FS_DELETE;
 
 	/* d_delete() of pseudo inode? (e.g. __ns_get_path() playing tricks) */
@@ -174,14 +172,7 @@ static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
 	if (isdir)
 		mask |= FS_ISDIR;
 
-	parent = dget_parent(dentry);
-	take_dentry_name_snapshot(&name, dentry);
-
-	fsnotify(d_inode(parent), mask, d_inode(dentry), FSNOTIFY_EVENT_INODE,
-		 name.name, 0);
-
-	release_dentry_name_snapshot(&name);
-	dput(parent);
+	__fsnotify_parent(NULL, dentry, mask);
 }
 
 /*
Signature · 1 line
2.17.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#12 lore raw
Quoted 27 lines · Jan Kara
Quoted 16 lines · Amir Goldstein

__fsnotify_parent() has an optimization in place to avoid unneeded take_dentry_name_snapshot(). When fsnotify_nameremove() was changed not to call __fsnotify_parent(), we left out the optimization. Kernel test robot reported a 5% performance regression in concurrent unlink() workload.

Modify __fsnotify_parent() so that it can be called also to report directory modififcation events and use it from fsnotify_nameremove().

---

Quoted 8 lines

Jan,

A nicer approach reusing __fsnotify_parent() instead of copying code from it.

This version does not apply cleanly to Al's for-next branch (there are some fsnotify changes in work.dcache). The conflict is trivial and resolved on my fsnotify branch [1].

Quoted 7 lines

Hum, let me check if I understand the situation right. We've changed fsnotify_nameremove() to not use fsnotify_parent() as we don't want to report FS_EVENT_ON_CHILD with it anymore. We should use fsnotify_dirent() as for all other directory event notification handlers but that's problematic due to different locking context and possible instability of parent.

Yes. Not only do we not want to report FS_EVENT_ON_CHILD with FS_DELETE, but we also need to report it to dir inode and to fs sb regardless of DCACHE_FSNOTIFY_PARENT_WATCHED.

Quoted 3 lines

Honestly I don't like the patch below much. How we are notifying parent without sending FS_EVENT_ON_CHILD and modify behavior based on that flag just looks subtle.

I see, although please note that reporting FS_EVENT_ON_CHILD is strongly related to the "modified behavior", because unless this an a report of event on_child, DCACHE_FSNOTIFY_PARENT_WATCHED is not relevant.

Quoted 6 lines

So I'd rather move the fsnotify call to vfs_unlink(), vfs_rmdir(), simple_unlink(), simple_rmdir(), and then those few callers of d_delete() that remain as you suggest elsewhere in this thread. And then we get more consistent context for fsnotify_nameremove() and could just use fsnotify_dirent().

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

The change of cleansing d_delete() from fsnotify_nameremove() requires more research and is anyway not stable tree material - if not for the level of complexity, then because all the users of FS_DELETE from pseudo and clustered filesystems need more time to find regressions (we do not have test coverage for those in LTP).

Thanks,
Amir.

author reviewer
#13 lore raw
Quoted 65 lines · Amir Goldstein
Quoted 27 lines · Jan Kara
Quoted 16 lines · Amir Goldstein

__fsnotify_parent() has an optimization in place to avoid unneeded take_dentry_name_snapshot(). When fsnotify_nameremove() was changed not to call __fsnotify_parent(), we left out the optimization. Kernel test robot reported a 5% performance regression in concurrent unlink() workload.

Modify __fsnotify_parent() so that it can be called also to report directory modififcation events and use it from fsnotify_nameremove().

---

Quoted 8 lines

Jan,

A nicer approach reusing __fsnotify_parent() instead of copying code from it.

This version does not apply cleanly to Al's for-next branch (there are some fsnotify changes in work.dcache). The conflict is trivial and resolved on my fsnotify branch [1].

Quoted 7 lines

Hum, let me check if I understand the situation right. We've changed fsnotify_nameremove() to not use fsnotify_parent() as we don't want to report FS_EVENT_ON_CHILD with it anymore. We should use fsnotify_dirent() as for all other directory event notification handlers but that's problematic due to different locking context and possible instability of parent.

Yes. Not only do we not want to report FS_EVENT_ON_CHILD with FS_DELETE, but we also need to report it to dir inode and to fs sb regardless of DCACHE_FSNOTIFY_PARENT_WATCHED.

Quoted 3 lines

Honestly I don't like the patch below much. How we are notifying parent without sending FS_EVENT_ON_CHILD and modify behavior based on that flag just looks subtle.

I see, although please note that reporting FS_EVENT_ON_CHILD is strongly related to the "modified behavior", because unless this an a report of event on_child, DCACHE_FSNOTIFY_PARENT_WATCHED is not relevant.

Quoted 6 lines

So I'd rather move the fsnotify call to vfs_unlink(), vfs_rmdir(), simple_unlink(), simple_rmdir(), and then those few callers of d_delete() that remain as you suggest elsewhere in this thread. And then we get more consistent context for fsnotify_nameremove() and could just use fsnotify_dirent().

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

Quoted 6 lines

The change of cleansing d_delete() from fsnotify_nameremove() requires more research and is anyway not stable tree material - if not for the level of complexity, then because all the users of FS_DELETE from pseudo and clustered filesystems need more time to find regressions (we do not have test coverage for those in LTP).

Something like this:
https://github.com/amir73il/linux/commits/fsnotify_nameremove

Only partially tested. Obviously haven't tested all callers.

Thanks,
Amir.

maintainer
#14 lore raw
Quoted 16 lines · Amir Goldstein
Quoted 8 lines · Amir Goldstein

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

Quoted 6 lines

The change of cleansing d_delete() from fsnotify_nameremove() requires more research and is anyway not stable tree material - if not for the level of complexity, then because all the users of FS_DELETE from pseudo and clustered filesystems need more time to find regressions (we do not have test coverage for those in LTP).

Quoted 4 lines

Something like this:
https://github.com/amir73il/linux/commits/fsnotify_nameremove

Only partially tested. Obviously haven't tested all callers.

Not quite. I'd add the fsnotify_nameremove() call also to simple_rmdir() and simple_unlink(). That takes care of:
arch/s390/hypfs/inode.c, drivers/infiniband/hw/qib/qib_fs.c, fs/configfs/dir.c, fs/debugfs/inode.c, fs/tracefs/inode.c, net/sunrpc/rpc_pipe.c

So you're left only with:
drivers/usb/gadget/function/f_fs.c, fs/btrfs/ioctl.c, fs/devpts/inode.c, fs/reiserfs/xattr.c

Out of these drivers/usb/gadget/function/f_fs.c and fs/reiserfs/xattr.c actually also don't want the notifications to be generated. They don't generate events on file creation AFAICS and at least in case of reiserfs I know that xattrs are handled in "hidden" system files so notification does not make any sense. So we are left with exactly *two* places that need explicit fsnotify_nameremove() call. Since both these callers already take care of calling fsnotify_create() I think that having explicit fsnotify_nameremove() calls there is clearer than the current state.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#15 lore raw
Quoted 21 lines · Jan Kara
Quoted 19 lines · Amir Goldstein
Quoted 8 lines · Amir Goldstein

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

Quoted 6 lines

The change of cleansing d_delete() from fsnotify_nameremove() requires more research and is anyway not stable tree material - if not for the level of complexity, then because all the users of FS_DELETE from pseudo and clustered filesystems need more time to find regressions (we do not have test coverage for those in LTP).

Something like this:
https://github.com/amir73il/linux/commits/fsnotify_nameremove

Quoted 8 lines
Quoted 1 line

Only partially tested. Obviously haven't tested all callers.

Not quite. I'd add the fsnotify_nameremove() call also to simple_rmdir() and simple_unlink(). That takes care of:
arch/s390/hypfs/inode.c, drivers/infiniband/hw/qib/qib_fs.c, fs/configfs/dir.c, fs/debugfs/inode.c, fs/tracefs/inode.c, net/sunrpc/rpc_pipe.c

simple_unlink() is used as i_op->unlink() implementation of simple filesystems, such as: fs/pstore/inode.c fs/ramfs/inode.c fs/ocfs2/dlmfs/dlmfs.c fs/hugetlbfs/inode.c kernel/bpf/inode.c

If we place fsnotify hook in the filesystem implementation and not in vfs_unlink(), what will cover normal fs? If we do place fsnotify hook in vfs_unlink(), then we have a double call to hook.

The places we add explicit fsnofity hooks should only be call sites that do not originate from vfs_unlink/vfs_rmdir.

Quoted 9 lines

So you're left only with:
drivers/usb/gadget/function/f_fs.c, fs/btrfs/ioctl.c, fs/devpts/inode.c, fs/reiserfs/xattr.c

Out of these drivers/usb/gadget/function/f_fs.c and fs/reiserfs/xattr.c actually also don't want the notifications to be generated. They don't generate events on file creation AFAICS and at least in case of reiserfs I know that xattrs are handled in "hidden" system files so notification does not make any sense. So we are left with exactly *two* places that need

OK. good to know.

Quoted 4 lines

explicit fsnotify_nameremove() call. Since both these callers already take care of calling fsnotify_create() I think that having explicit fsnotify_nameremove() calls there is clearer than the current state.

I though so too, but then I did not feel comfortable with "regressing" delete notifications on many filesystems that did seem plausible for having notification users like configfs, even though they do not have create notification, so I decided to do the safer option of converting all plausible callers of d_delete() that abide to the parent/name stable constrains to use the d_delete_and_notify() wrapper.

For readers that did not follow my link, those are the call site that were converted to opt-in for d_delete_and_notify() in the linked branch:
arch/s390/hypfs/inode.c
drivers/infiniband/hw/qib/qib_fs.c drivers/usb/gadget/function/f_fs.c
fs/btrfs/ioctl.c
fs/configfs/dir.c
fs/debugfs/inode.c
fs/devpts/inode.c
fs/reiserfs/xattr.c
fs/tracefs/inode.c
net/sunrpc/rpc_pipe.c

In addition, nfs and afs no longer need to call fsnotify hook explcitly on completion of silly rename (delayed unlink).

Thanks,
Amir.

maintainer
#16 lore raw
Quoted 34 lines · Amir Goldstein
Quoted 20 lines · Jan Kara
Quoted 19 lines · Amir Goldstein
Quoted 8 lines · Amir Goldstein

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

Quoted 6 lines

The change of cleansing d_delete() from fsnotify_nameremove() requires more research and is anyway not stable tree material - if not for the level of complexity, then because all the users of FS_DELETE from pseudo and clustered filesystems need more time to find regressions (we do not have test coverage for those in LTP).

Something like this:
https://github.com/amir73il/linux/commits/fsnotify_nameremove

Quoted 8 lines
Quoted 1 line

Only partially tested. Obviously haven't tested all callers.

Not quite. I'd add the fsnotify_nameremove() call also to simple_rmdir() and simple_unlink(). That takes care of:
arch/s390/hypfs/inode.c, drivers/infiniband/hw/qib/qib_fs.c, fs/configfs/dir.c, fs/debugfs/inode.c, fs/tracefs/inode.c, net/sunrpc/rpc_pipe.c

simple_unlink() is used as i_op->unlink() implementation of simple filesystems, such as: fs/pstore/inode.c fs/ramfs/inode.c fs/ocfs2/dlmfs/dlmfs.c fs/hugetlbfs/inode.c kernel/bpf/inode.c

Quoted 6 lines

If we place fsnotify hook in the filesystem implementation and not in vfs_unlink(), what will cover normal fs? If we do place fsnotify hook in vfs_unlink(), then we have a double call to hook.

The places we add explicit fsnofity hooks should only be call sites that do not originate from vfs_unlink/vfs_rmdir.

Hum, right. I didn't realize simple_unlink() gets also called through vfs_unlink() for some filesystems. But then I'd rather create variants simple_unlink_notify() and simple_rmdir_notify() than messing with d_delete(). As I just think that fsnotify call in d_delete() happens at a wrong layer. d_delete() is about dcache management, not really about filesystem name removal which is what we want to notify about.

Quoted 17 lines
Quoted 9 lines

So you're left only with:
drivers/usb/gadget/function/f_fs.c, fs/btrfs/ioctl.c, fs/devpts/inode.c, fs/reiserfs/xattr.c

Out of these drivers/usb/gadget/function/f_fs.c and fs/reiserfs/xattr.c actually also don't want the notifications to be generated. They don't generate events on file creation AFAICS and at least in case of reiserfs I know that xattrs are handled in "hidden" system files so notification does not make any sense. So we are left with exactly *two* places that need

OK. good to know.

Quoted 4 lines

explicit fsnotify_nameremove() call. Since both these callers already take care of calling fsnotify_create() I think that having explicit fsnotify_nameremove() calls there is clearer than the current state.

Quoted 6 lines

I though so too, but then I did not feel comfortable with "regressing" delete notifications on many filesystems that did seem plausible for having notification users like configfs, even though they do not have create notification, so I decided to do the safer option of converting all plausible callers of d_delete() that abide to the parent/name stable constrains to use the d_delete_and_notify() wrapper.

As I said above, I don't want to regress anyone either. But I just think that d_delete() is a wrong function to call fsnotify hook from (as much as it is convenient) and that's what's causing all these problems.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#17 lore raw
Quoted 43 lines · Jan Kara
Quoted 34 lines · Amir Goldstein
Quoted 20 lines · Jan Kara
Quoted 19 lines · Amir Goldstein
Quoted 8 lines · Amir Goldstein

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

Quoted 6 lines

The change of cleansing d_delete() from fsnotify_nameremove() requires more research and is anyway not stable tree material - if not for the level of complexity, then because all the users of FS_DELETE from pseudo and clustered filesystems need more time to find regressions (we do not have test coverage for those in LTP).

Something like this:
https://github.com/amir73il/linux/commits/fsnotify_nameremove

Quoted 8 lines
Quoted 1 line

Only partially tested. Obviously haven't tested all callers.

Not quite. I'd add the fsnotify_nameremove() call also to simple_rmdir() and simple_unlink(). That takes care of:
arch/s390/hypfs/inode.c, drivers/infiniband/hw/qib/qib_fs.c, fs/configfs/dir.c, fs/debugfs/inode.c, fs/tracefs/inode.c, net/sunrpc/rpc_pipe.c

simple_unlink() is used as i_op->unlink() implementation of simple filesystems, such as: fs/pstore/inode.c fs/ramfs/inode.c fs/ocfs2/dlmfs/dlmfs.c fs/hugetlbfs/inode.c kernel/bpf/inode.c

Quoted 6 lines

If we place fsnotify hook in the filesystem implementation and not in vfs_unlink(), what will cover normal fs? If we do place fsnotify hook in vfs_unlink(), then we have a double call to hook.

The places we add explicit fsnofity hooks should only be call sites that do not originate from vfs_unlink/vfs_rmdir.

Quoted 7 lines

Hum, right. I didn't realize simple_unlink() gets also called through vfs_unlink() for some filesystems. But then I'd rather create variants simple_unlink_notify() and simple_rmdir_notify() than messing with d_delete(). As I just think that fsnotify call in d_delete() happens at a wrong layer. d_delete() is about dcache management, not really about filesystem name removal which is what we want to notify about.

Agreed.
I'll follow up with this solution, hopefully after the stable regression fix is already merged.

Thanks,
Amir.

maintainer
#18 lore raw
In reply to Amir Goldstein
Quoted 8 lines · Amir Goldstein
Quoted 6 lines · Jan Kara

So I'd rather move the fsnotify call to vfs_unlink(), vfs_rmdir(), simple_unlink(), simple_rmdir(), and then those few callers of d_delete() that remain as you suggest elsewhere in this thread. And then we get more consistent context for fsnotify_nameremove() and could just use fsnotify_dirent().

Quoted 7 lines

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

OK, fair enough. I'll go with v1 + build fix for current merge window + stable as it's local to fsnotify_nameremove().

Quoted 5 lines

The change of cleansing d_delete() from fsnotify_nameremove() requires more research and is anyway not stable tree material - if not for the level of complexity, then because all the users of FS_DELETE from pseudo and clustered filesystems need more time to find regressions (we do not have test coverage for those in LTP).

Agreed.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#19 lore raw
Quoted 18 lines · Jan Kara
Quoted 8 lines · Amir Goldstein
Quoted 6 lines · Jan Kara

So I'd rather move the fsnotify call to vfs_unlink(), vfs_rmdir(), simple_unlink(), simple_rmdir(), and then those few callers of d_delete() that remain as you suggest elsewhere in this thread. And then we get more consistent context for fsnotify_nameremove() and could just use fsnotify_dirent().

Quoted 7 lines

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

Quoted 2 lines

OK, fair enough. I'll go with v1 + build fix for current merge window + stable as it's local to fsnotify_nameremove().

Please note that the patch on your fsnotify branch conflicts with fsnotify_nameremove() changes in master:
230c6402b1b3 ovl_lookup_real_one(): don't bother with strlen() 25b229dff4ff fsnotify(): switch to passing const struct qstr * for file_name

Thanks,
Amir.

maintainer
#20 lore raw
Quoted 19 lines · Amir Goldstein
Quoted 18 lines · Jan Kara
Quoted 15 lines · Amir Goldstein
Quoted 6 lines · Jan Kara

So I'd rather move the fsnotify call to vfs_unlink(), vfs_rmdir(), simple_unlink(), simple_rmdir(), and then those few callers of d_delete() that remain as you suggest elsewhere in this thread. And then we get more consistent context for fsnotify_nameremove() and could just use fsnotify_dirent().

Yes, I much prefer this solution myself and I will follow up with it, but it would not be honest to suggest said solution as a stable fix to the performance regression that was introduced in v5.1. I think is it better if you choose between lesser evil:
v1 with ifdef CONFIG_FSNOTIFY to fix build issue
v2 as subtle as it is
OR another obviously safe stable fix that you can think of

Quoted 7 lines
Quoted 2 lines

OK, fair enough. I'll go with v1 + build fix for current merge window + stable as it's local to fsnotify_nameremove().

Please note that the patch on your fsnotify branch conflicts with fsnotify_nameremove() changes in master:
230c6402b1b3 ovl_lookup_real_one(): don't bother with strlen() 25b229dff4ff fsnotify(): switch to passing const struct qstr * for file_name

Thanks for the heads up! The conflict is easy enough to resolve but I've notified Linus about it in my pull request. Hum, which reminds me that I've forgotten to pull the latest fix in fsnotify branch into my for_next branch and so I didn't get a notification about the conflict myself. Too late to fix that now I guess...

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR