fs: optimize chown_common by skipping unnecessary ownership changes
PATCH 1/1 fs: optimize chown_common by skipping unnecessary ownership changes
From: Jay Xu <[email protected]>
Add early return optimization to chown_common() when the requested uid/gid already matches the current inode ownership. This avoids calling notify_change() and associated filesystem operations when no actual change is needed.
The check is performed after acquiring the inode lock to ensure atomicity and uses the kernel's uid_eq()/gid_eq() functions for proper comparison.
This optimization provides several benefits:
- Reduces unnecessary filesystem metadata updates and journal writes
- Prevents redundant storage I/O when files are on persistent storage
- Improves performance for recursive chown operations that encounter files with already-correct ownership
- Avoids invoking security hooks and filesystem-specific setattr operations when no change is required
--- fs/open.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
fs/open.c +12 -0
@@ -761,6 +761,7 @@ int chown_common(const struct path *path, uid_t user, gid_t group)struct iattr newattrs;kuid_t uid;kgid_t gid; +bool needs_update = false; uid =make_kuid (current_user_ns (), user); gid =make_kgid (current_user_ns (), group);
@@ -779,6 +780,17 @@ int chown_common(const struct path *path, uid_t user, gid_t group) error =inode_lock_killable (inode);if (error)return error; + +/* Check if ownership actually needs to change */ +if ((newattrs.ia_valid &ATTR_UID ) && !uid_eq (inode->i_uid, uid)) + needs_update = true; +if ((newattrs.ia_valid &ATTR_GID ) && !gid_eq (inode->i_gid, gid)) + needs_update = true; + +if (!needs_update) { +inode_unlock (inode); +return 0 ; + }if (!S_ISDIR (inode->i_mode)) newattrs.ia_valid |=ATTR_KILL_SUID |ATTR_KILL_PRIV |setattr_should_drop_sgid (idmap, inode);
Signature · 1 line
2.34.1
Quoted 7 lines · On Thu, Nov 13, 2025 at 09:34:49AM +0800, [email protected] wrote:
From: Jay Xu <[email protected]>
Add early return optimization to
chown_common()when the requested uid/gid already matches the current inode ownership. This avoids callingnotify_change()and associated filesystem operations when no actual change is needed.
If this is useful in practice, then so is probably chmod.
Quoted 11 lines
The check is performed after acquiring the inode lock to ensure atomicity and uses the kernel's uid_eq()/gid_eq() functions for proper comparison.
This optimization provides several benefits:
- Reduces unnecessary filesystem metadata updates and journal writes
- Prevents redundant storage I/O when files are on persistent storage
- Improves performance for recursive chown operations that encounter files with already-correct ownership
- Avoids invoking security hooks and filesystem-specific setattr operations when no change is required
The last bit is a breaking change in behavior though. For example right now invoking chown 0:0 /bin as an unprivileged user fails. It will succeed with your patch.
iow you can't avoid any of the security checks.
However, if there are real workloads which chown/chmod to the current value already, perhaps it would make sense for the routine to track if anything changed and if not to avoid dirtying the inode, which still might save on I/O.
Quoted 39 lines
Signed-off-by Jay Xu <[email protected]>--- fs/open.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)fs/open.c +12
-0@@ -761,6 +761,7 @@ int chown_common(const struct path *path, uid_t user, gid_t group)struct iattr newattrs;kuid_t uid;kgid_t gid; +bool needs_update = false; uid =make_kuid (current_user_ns (), user); gid =make_kgid (current_user_ns (), group);@@ -779,6 +780,17 @@ int chown_common(const struct path *path, uid_t user, gid_t group) error =inode_lock_killable (inode);if (error)return error; + +/* Check if ownership actually needs to change */ +if ((newattrs.ia_valid &ATTR_UID ) && !uid_eq (inode->i_uid, uid)) + needs_update = true; +if ((newattrs.ia_valid &ATTR_GID ) && !gid_eq (inode->i_gid, gid)) + needs_update = true; + +if (!needs_update) { +inode_unlock (inode); +return 0 ; + }if (!S_ISDIR (inode->i_mode)) newattrs.ia_valid |=ATTR_KILL_SUID |ATTR_KILL_PRIV |setattr_should_drop_sgid (idmap, inode);Signature · 1 line
2.34.1