mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] fs: optimize chown_common by skipping unnecessary ownership changes
@ 2025-11-13  1:34 jayxu1990
  2025-11-13  2:08 ` Mateusz Guzik
  0 siblings, 1 reply; 2+ messages in thread
From: jayxu1990 @ 2025-11-13  1:34 UTC (permalink / raw)
  To: viro, brauner
  Cc: jack, linux-fsdevel, linux-kernel, jayxu1990, rdlee.upstream, avnerkhan

From: Jay Xu <jayxu1990@gmail.com>

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

Signed-off-by: Jay Xu <jayxu1990@gmail.com>
---
 fs/open.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/fs/open.c b/fs/open.c
index 3d64372ecc67..82bde70c6c08 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -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);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-11-13  2:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-13  1:34 [PATCH] fs: optimize chown_common by skipping unnecessary ownership changes jayxu1990
2025-11-13  2:08 ` Mateusz Guzik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®