From: Shakeel Butt <shakeel.butt@linux.dev>
To: Christian Brauner <brauner@kernel.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Tejun Heo <tj@kernel.org>, Jori Koolstra <jkoolstra@xs4all.nl>,
Ian Kent <raven@themaw.net>,
"T . J . Mercier" <tjmercier@google.com>,
Meta kernel team <kernel-team@meta.com>,
linux-fsdevel@vger.kernel.org, driver-core@lists.linux.dev,
linux-kernel@vger.kernel.org
Subject: [PATCH 2/6] kernfs: don't copy times and link count into the inode on refresh
Date: Wed, 18 Nov 2026 14:01:49 -0800 [thread overview]
Message-ID: <20261118220154.2965621-3-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261118220154.2965621-1-shakeel.butt@linux.dev>
Every permission check and getattr copies a node's times, if it has
iattrs, and a directory's link count into the shared inode. Since the
previous patch, getattr reports both from the node, and permission
checks use only the mode, owner and group. The only other users of a
kernfs inode's times are the mmap write fault and the atime update,
which just update the inode's copy. The inode's link count matters
only to the IN_DELETE_SELF check, which needs 0 once __kernfs_remove()
clears it.
Copy only the mode, owner and group, which also takes work off every
permission check. Set a directory's link count when its inode is set
up. Every caller that sets up a directory inode holds kernfs_rwsem,
and __kernfs_remove() holds it for writing while it clears the count,
so the two cannot race. Assert that, and note in the kernfs_get_inode()
comment that it is not for directories; its one user outside kernfs,
cgroup_may_write(), gets a file.
getattr fell back to the inode's link count for a directory being
removed. That count is no longer kept up to date, so report 0 instead:
KERNFS_REMOVING is never cleared, and lookups already fail.
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
fs/kernfs/inode.c | 32 ++++++++++++++++++--------------
1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 3616161a56cb..2d8da8b4e7da 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -157,27 +157,23 @@ static inline void set_inode_attr(struct inode *inode,
{
inode->i_uid = attrs->ia_uid;
inode->i_gid = attrs->ia_gid;
- inode_set_atime_to_ts(inode, attrs->ia_atime);
- inode_set_mtime_to_ts(inode, attrs->ia_mtime);
- inode_set_ctime_to_ts(inode, attrs->ia_ctime);
}
/*
- * Get the link count of a directory: its subdirectories plus two.
- * Return false for other nodes and for a directory being removed.
+ * Get a directory's link count: its subdirectories plus two, or 0 once
+ * its removal has started. Return false for other nodes.
*/
static bool kernfs_dir_nlink(struct kernfs_node *kn, unsigned int *nlink)
{
- if (kernfs_type(kn) != KERNFS_DIR || (kn->flags & KERNFS_REMOVING))
+ if (kernfs_type(kn) != KERNFS_DIR)
return false;
- *nlink = kn->dir.subdirs + 2;
+ *nlink = (kn->flags & KERNFS_REMOVING) ? 0 : kn->dir.subdirs + 2;
return true;
}
static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
{
struct kernfs_iattrs *attrs;
- unsigned int nlink;
inode->i_mode = kn->mode;
attrs = kernfs_iattrs_noalloc(kn);
@@ -187,9 +183,6 @@ static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
* persistent copy in kernfs_node.
*/
set_inode_attr(inode, attrs);
-
- if (kernfs_dir_nlink(kn, &nlink))
- set_nlink(inode, nlink);
}
int kernfs_iop_getattr(const struct mnt_idmap *idmap,
@@ -207,8 +200,8 @@ int kernfs_iop_getattr(const struct mnt_idmap *idmap,
generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
/*
- * Other refreshes may store older times or link counts in the
- * inode at the same time. Report the values from the node.
+ * The inode's times and link count are not kept up to date.
+ * Report them from the node.
*/
attrs = kernfs_iattrs_noalloc(kn);
if (attrs) {
@@ -225,6 +218,8 @@ int kernfs_iop_getattr(const struct mnt_idmap *idmap,
static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
{
+ unsigned int nlink;
+
kernfs_get(kn);
inode->i_private = kn;
inode->i_mapping->a_ops = &ram_aops;
@@ -238,6 +233,14 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
/* initialize inode according to type */
switch (kernfs_type(kn)) {
case KERNFS_DIR:
+ /*
+ * The link count is set only here; getattr reports it from
+ * the node. Holding kernfs_rwsem keeps this from racing
+ * with __kernfs_remove() clearing it.
+ */
+ lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
+ if (kernfs_dir_nlink(kn, &nlink))
+ set_nlink(inode, nlink);
inode->i_op = &kernfs_dir_iops;
inode->i_fop = &kernfs_dir_fops;
if (kn->flags & KERNFS_EMPTY_DIR)
@@ -267,7 +270,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
* locked.
*
* Locking:
- * Kernel thread context (may sleep).
+ * Kernel thread context (may sleep). Not for directories: kernfs
+ * sets up their inodes itself, with kernfs_rwsem held.
*
* Return:
* Pointer to allocated inode on success, %NULL on failure.
--
2.53.1-Meta
next prev parent reply other threads:[~2026-11-18 22:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 22:01 [PATCH 0/6] kernfs: stop taking kernfs_iattr_rwsem for writing in mkdir/rmdir Shakeel Butt
2026-11-18 22:01 ` [PATCH 1/6] kernfs: report times and link count from the node in getattr Shakeel Butt
2026-11-18 22:01 ` Shakeel Butt [this message]
2026-11-18 22:01 ` [PATCH 3/6] kernfs: don't take kernfs_iattr_rwsem for the directory counters Shakeel Butt
2026-11-18 22:01 ` [PATCH 4/6] kernfs: don't take kernfs_iattr_rwsem to publish KERNFS_REMOVING Shakeel Butt
2026-11-18 22:01 ` [PATCH 5/6] kernfs: update a directory's times without kernfs_iattr_rwsem Shakeel Butt
2026-11-18 22:01 ` [PATCH 6/6] kernfs: don't take kernfs_iattr_rwsem in __kernfs_remove() Shakeel Butt
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=20261118220154.2965621-3-shakeel.butt@linux.dev \
--to=shakeel.butt@linux.dev \
--cc=brauner@kernel.org \
--cc=driver-core@lists.linux.dev \
--cc=gregkh@linuxfoundation.org \
--cc=jkoolstra@xs4all.nl \
--cc=kernel-team@meta.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=raven@themaw.net \
--cc=tj@kernel.org \
--cc=tjmercier@google.com \
/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