[REPOST PATCH v4 5/5] kernfs: add kernfs_need_inode_refresh()
From: Ian Kent
Date: Fri May 28 2021 - 02:35:00 EST
Now the kernfs_rwsem read lock is held for kernfs_refresh_inode() and
the i_lock taken to protect inode updates there can be some contention
introduced when .permission() is called with concurrent path walks in
progress.
Since .permission() is called frequently during path walks it's worth
checking if the update is actually needed before taking the lock and
performing the update.
Signed-off-by: Ian Kent <raven@xxxxxxxxxx>
---
fs/kernfs/inode.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 6728ecd81eb37..67fb1289c51dc 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -158,6 +158,30 @@ static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
inode->i_ctime = current_time(inode);
}
+static bool kernfs_need_inode_refresh(struct kernfs_node *kn,
+ struct inode *inode,
+ struct kernfs_iattrs *attrs)
+{
+ if (kernfs_type(kn) == KERNFS_DIR) {
+ if (inode->i_nlink != kn->dir.subdirs + 2)
+ return true;
+ }
+
+ if (inode->i_mode != kn->mode)
+ return true;
+
+ if (attrs) {
+ if (!timespec64_equal(&inode->i_atime, &attrs->ia_atime) ||
+ !timespec64_equal(&inode->i_mtime, &attrs->ia_mtime) ||
+ !timespec64_equal(&inode->i_ctime, &attrs->ia_ctime) ||
+ !uid_eq(inode->i_uid, attrs->ia_uid) ||
+ !gid_eq(inode->i_gid, attrs->ia_gid))
+ return true;
+ }
+
+ return false;
+}
+
static inline void set_inode_attr(struct inode *inode,
struct kernfs_iattrs *attrs)
{
@@ -172,6 +196,9 @@ static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs = kn->iattr;
+ if (!kernfs_need_inode_refresh(kn, inode, attrs))
+ return;
+
spin_lock(&inode->i_lock);
inode->i_mode = kn->mode;
if (attrs)