From: jayxu1990@gmail.com
To: viro@zeniv.linux.org.uk, brauner@kernel.org
Cc: jack@suse.cz, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org, jayxu1990@gmail.com,
rdlee.upstream@gmail.com, avnerkhan@utexas.edu
Subject: [PATCH] fs: optimize chown_common by skipping unnecessary ownership changes
Date: Thu, 13 Nov 2025 09:34:49 +0800 [thread overview]
Message-ID: <20251113013449.3874650-1-jayxu1990@gmail.com> (raw)
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
next reply other threads:[~2025-11-13 1:36 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-13 1:34 jayxu1990 [this message]
2025-11-13 2:08 ` Mateusz Guzik
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251113013449.3874650-1-jayxu1990@gmail.com \
--to=jayxu1990@gmail.com \
--cc=avnerkhan@utexas.edu \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rdlee.upstream@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®