Skip to contentSkip to conversation

kernfs: Use RCU for kernfs_node::name and ::parent lookup.

PATCH In review Sebastian Andrzej Siewior 3 participants 8 messages Nov 21, 2024 – Jan 16, 2025

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 kernfs: Use RCU for kernfs_node::name and ::parent lookup.

Instead of using kernfs_rename_lock to protect kernfs_node::name and ::parent from changing/ going away use RCU for its lifetime guarantees.

A node can be be renamed or moved and the observed ::name and ::parent remains valid within the RCU section. kernfs_root() may dereference kernfs_node::parent in order to obtain kernfs_root and the parent will remain around and every node points to the same kernfs_root.

This can be used to optimize kernfs_name() to perform the lookup only with RCU (without kernfs_rename_lock).

For a complete path, as it is done in kernfs_path_from_node(), the kernfs_rename_lock is still required in order to obtain a stable parent relationship while computing the relevant node depth. This must not change while the nodes are inspected in order to build the path. If the kernfs user never moves the nodes (changes the parent) then the kernfs_rename_lock is not required and the RCU guarantees are sufficient. This "restriction" can be set with KERNFS_ROOT_INVARIANT_PARENT.

I went through all ::name and ::parent users and added the required access for the lookup with a few extensions:
- rdtgroup_pseudo_lock_create() drops all locks and then uses the name later on. resctrl supports rename with different parents. Here I made a temporal copy of the name while it is used outside of the lock.
- kernfs_rename_ns() accepts NULL as new_parent. This simplifies sysfs_move_dir_ns() where it can set NULL in order to reuse the current name.
- kernfs_rename_ns() is only using kernfs_rename_lock if the parents are different. All users users use either RCU or kernfs_rwsem.
- kernfs_fop_readdir() drops kernfs_root::kernfs_rwsem while holding a reference to name and invoking dir_emit(). This has been changed and lock is held.
- kernfs_notify_workfn() access kernfs_node::name without any protection. Added kernfs_root::kernfs_rwsem for the iteration.
- kernfs_get_parent_dentry() acquires now kernfs_root::kernfs_rwsem while accessing the parent node.
- kernfs_node_dentry() acquires now kernfs_root::kernfs_rwsem while parent is accessed and the name looked up.

---
 arch/x86/kernel/cpu/resctrl/internal.h    |   5 +
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c |  14 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    |  60 ++++---
 fs/kernfs/dir.c                           | 200 ++++++++++++----------
 fs/kernfs/file.c                          |   6 +-
 fs/kernfs/kernfs-internal.h               |  24 ++-
 fs/kernfs/mount.c                         |  19 +-
 fs/kernfs/symlink.c                       |  30 ++--
 fs/sysfs/dir.c                            |   2 +-
 fs/sysfs/file.c                           |  24 ++-
 include/linux/kernfs.h                    |  11 +-
 kernel/cgroup/cgroup-v1.c                 |   2 +-
 kernel/cgroup/cgroup.c                    |  17 +-
 security/selinux/hooks.c                  |   7 +-
 14 files changed, 261 insertions(+), 160 deletions(-)
arch/x86/kernel/cpu/resctrl/internal.h +5 -0
index 955999aecfca9..cc83dbc70a8ef 100644
@@ -505,6 +505,11 @@ int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
 
 extern struct mutex rdtgroup_mutex;
 
+static inline const char *rdt_kn_get_name(const struct kernfs_node *kn)
+{
+	return rcu_dereference_check(kn->name, lockdep_is_held(&rdtgroup_mutex));
+}
+
 extern struct rdt_hw_resource rdt_resources_all[];
 extern struct rdtgroup rdtgroup_default;
 extern struct dentry *debugfs_resctrl;
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +10 -4
index 972e6b6b0481f..efe1fc5a6866f 100644
@@ -52,7 +52,8 @@ static char *pseudo_lock_devnode(const struct device *dev, umode_t *mode)
 	rdtgrp = dev_get_drvdata(dev);
 	if (mode)
 		*mode = 0600;
-	return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdtgrp->kn->name);
+	guard(mutex)(&rdtgroup_mutex);
+	return kasprintf(GFP_KERNEL, "pseudo_lock/%s", rdt_kn_get_name(rdtgrp->kn));
 }
 
 static const struct class pseudo_lock_class = {
@@ -1301,6 +1302,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 	struct task_struct *thread;
 	unsigned int new_minor;
 	struct device *dev;
+	char *kn_name __free(kfree) = NULL;
 	int ret;
 
 	ret = pseudo_lock_region_alloc(plr);
@@ -1312,6 +1314,11 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 		ret = -EINVAL;
 		goto out_region;
 	}
+	kn_name = kstrdup(rdt_kn_get_name(rdtgrp->kn), GFP_KERNEL);
+	if (!kn_name) {
+		ret = -ENOMEM;
+		goto out_cstates;
+	}
 
 	plr->thread_done = 0;
 
@@ -1360,8 +1367,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 	mutex_unlock(&rdtgroup_mutex);
 
 	if (!IS_ERR_OR_NULL(debugfs_resctrl)) {
-		plr->debugfs_dir = debugfs_create_dir(rdtgrp->kn->name,
-						      debugfs_resctrl);
+		plr->debugfs_dir = debugfs_create_dir(kn_name, debugfs_resctrl);
 		if (!IS_ERR_OR_NULL(plr->debugfs_dir))
 			debugfs_create_file("pseudo_lock_measure", 0200,
 					    plr->debugfs_dir, rdtgrp,
@@ -1370,7 +1376,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 
 	dev = device_create(&pseudo_lock_class, NULL,
 			    MKDEV(pseudo_lock_major, new_minor),
-			    rdtgrp, "%s", rdtgrp->kn->name);
+			    rdtgrp, "%s", kn_name);
 
 	mutex_lock(&rdtgroup_mutex);
 
arch/x86/kernel/cpu/resctrl/rdtgroup.c +35 -25

217 lines not on this page · raw message

fs/kernfs/dir.c +114 -86

466 lines not on this page · raw message

fs/kernfs/file.c +5 -1
index 8502ef68459b9..05f7b30283150 100644
@@ -911,9 +911,11 @@ static void kernfs_notify_workfn(struct work_struct *work)
 	/* kick fsnotify */
 
 	down_read(&root->kernfs_supers_rwsem);
+	down_read(&root->kernfs_rwsem);
 	list_for_each_entry(info, &kernfs_root(kn)->supers, node) {
 		struct kernfs_node *parent;
 		struct inode *p_inode = NULL;
+		const char *kn_name;
 		struct inode *inode;
 		struct qstr name;
 
@@ -927,7 +929,8 @@ static void kernfs_notify_workfn(struct work_struct *work)
 		if (!inode)
 			continue;
 
-		name = (struct qstr)QSTR_INIT(kn->name, strlen(kn->name));
+		kn_name = kernfs_rcu_get_name(kn);
+		name = (struct qstr)QSTR_INIT(kn_name, strlen(kn_name));
 		parent = kernfs_get_parent(kn);
 		if (parent) {
 			p_inode = ilookup(info->sb, kernfs_ino(parent));
@@ -948,6 +951,7 @@ static void kernfs_notify_workfn(struct work_struct *work)
 	}
 
 	up_read(&root->kernfs_supers_rwsem);
+	up_read(&root->kernfs_rwsem);
 	kernfs_put(kn);
 	goto repeat;
 }
fs/kernfs/kernfs-internal.h +21 -3
index b42ee6547cdc1..19a5ad269b0de 100644
@@ -64,11 +64,14 @@ struct kernfs_root {
  *
  * Return: the kernfs_root @kn belongs to.
  */
-static inline struct kernfs_root *kernfs_root(struct kernfs_node *kn)
+static inline struct kernfs_root *kernfs_root(const struct kernfs_node *kn)
 {
+	const struct kernfs_node *knp;
 	/* if parent exists, it's always a dir; otherwise, @sd is a dir */
-	if (kn->parent)
-		kn = kn->parent;
+	guard(rcu)();
+	knp = rcu_dereference(kn->parent);
+	if (knp)
+		kn = knp;
 	return kn->dir.root;
 }
 
@@ -97,6 +100,21 @@ struct kernfs_super_info {
 };
 #define kernfs_info(SB) ((struct kernfs_super_info *)(SB->s_fs_info))
 
+static inline bool kernfs_root_is_locked(const struct kernfs_node *kn)
+{
+	return lockdep_is_held(&kernfs_root(kn)->kernfs_rwsem);
+}
+
+static inline const char *kernfs_rcu_get_name(const struct kernfs_node *kn)
+{
+	return rcu_dereference_check(kn->name, kernfs_root_is_locked(kn));
+}
+
+static inline struct kernfs_node *kernfs_rcu_get_parent(const struct kernfs_node *kn)
+{
+	return rcu_dereference_check(kn->parent, kernfs_root_is_locked(kn));
+}
+
 static inline struct kernfs_node *kernfs_dentry_node(struct dentry *dentry)
 {
 	if (d_really_is_negative(dentry))
fs/kernfs/mount.c +13 -6
index 1358c21837f1a..db71faba3bb53 100644
@@ -145,8 +145,10 @@ static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
 static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
 {
 	struct kernfs_node *kn = kernfs_dentry_node(child);
+	struct kernfs_root *root = kernfs_root(kn);
 
-	return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
+	guard(rwsem_read)(&root->kernfs_rwsem);
+	return d_obtain_alias(kernfs_get_inode(child->d_sb, kernfs_rcu_get_parent(kn)));
 }
 
 static const struct export_operations kernfs_export_ops = {
@@ -186,10 +188,10 @@ static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
 		return NULL;
 	}
 
-	while (child->parent != parent) {
-		if (!child->parent)
+	while (kernfs_rcu_get_parent(child) != parent) {
+		child = kernfs_rcu_get_parent(child);
+		if (!child)
 			return NULL;
-		child = child->parent;
 	}
 
 	return child;
@@ -207,6 +209,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
 {
 	struct dentry *dentry;
 	struct kernfs_node *knparent;
+	struct kernfs_root *root;
 
 	BUG_ON(sb->s_op != &kernfs_sops);
 
@@ -216,6 +219,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
 	if (!kn->parent)
 		return dentry;
 
+	root = kernfs_root(kn);
+	guard(rwsem_read)(&root->kernfs_rwsem);
+
 	knparent = find_next_ancestor(kn, NULL);
 	if (WARN_ON(!knparent)) {
 		dput(dentry);
@@ -225,6 +231,7 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
 	do {
 		struct dentry *dtmp;
 		struct kernfs_node *kntmp;
+		const char *name;
 
 		if (kn == knparent)
 			return dentry;
@@ -233,8 +240,8 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
 			dput(dentry);
 			return ERR_PTR(-EINVAL);
 		}
-		dtmp = lookup_positive_unlocked(kntmp->name, dentry,
-					       strlen(kntmp->name));
+		name = rcu_dereference(kntmp->name);
+		dtmp = lookup_positive_unlocked(name, dentry, strlen(name));
 		dput(dentry);
 		if (IS_ERR(dtmp))
 			return dtmp;
fs/kernfs/symlink.c +16 -14
index 45371a70caa71..142cdf2af313b 100644
@@ -62,10 +62,10 @@ static int kernfs_get_target_path(struct kernfs_node *parent,
 
 	/* go up to the root, stop at the base */
 	base = parent;
-	while (base->parent) {
-		kn = target->parent;
-		while (kn->parent && base != kn)
-			kn = kn->parent;
+	while (kernfs_rcu_get_parent(base)) {
+		kn = kernfs_rcu_get_parent(target);
+		while (kernfs_rcu_get_parent(kn) && base != kn)
+			kn = kernfs_rcu_get_parent(kn);
 
 		if (base == kn)
 			break;
@@ -75,14 +75,14 @@ static int kernfs_get_target_path(struct kernfs_node *parent,
 
 		strcpy(s, "../");
 		s += 3;
-		base = base->parent;
+		base = kernfs_rcu_get_parent(base);
 	}
 
 	/* determine end of target string for reverse fillup */
 	kn = target;
-	while (kn->parent && kn != base) {
-		len += strlen(kn->name) + 1;
-		kn = kn->parent;
+	while (kernfs_rcu_get_parent(kn) && kn != base) {
+		len += strlen(kernfs_rcu_get_name(kn)) + 1;
+		kn = kernfs_rcu_get_parent(kn);
 	}
 
 	/* check limits */
@@ -94,15 +94,16 @@ static int kernfs_get_target_path(struct kernfs_node *parent,
 
 	/* reverse fillup of target string from target to base */
 	kn = target;
-	while (kn->parent && kn != base) {
-		int slen = strlen(kn->name);
+	while (kernfs_rcu_get_parent(kn) && kn != base) {
+		const char *name = kernfs_rcu_get_name(kn);
+		int slen = strlen(name);
 
 		len -= slen;
-		memcpy(s + len, kn->name, slen);
+		memcpy(s + len, name, slen);
 		if (len)
 			s[--len] = '/';
 
-		kn = kn->parent;
+		kn = kernfs_rcu_get_parent(kn);
 	}
 
 	return 0;
@@ -111,12 +112,13 @@ static int kernfs_get_target_path(struct kernfs_node *parent,
 static int kernfs_getlink(struct inode *inode, char *path)
 {
 	struct kernfs_node *kn = inode->i_private;
-	struct kernfs_node *parent = kn->parent;
+	struct kernfs_node *parent;
 	struct kernfs_node *target = kn->symlink.target_kn;
-	struct kernfs_root *root = kernfs_root(parent);
+	struct kernfs_root *root = kernfs_root(kn);
 	int error;
 
 	down_read(&root->kernfs_rwsem);
+	parent = kernfs_rcu_get_parent(kn);
 	error = kernfs_get_target_path(parent, target, path);
 	up_read(&root->kernfs_rwsem);
 
fs/sysfs/dir.c +1 -1
index 4df2afa551dc6..94e12efd92f21 100644
@@ -123,7 +123,7 @@ int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
 	new_parent = new_parent_kobj && new_parent_kobj->sd ?
 		new_parent_kobj->sd : sysfs_root_kn;
 
-	return kernfs_rename_ns(kn, new_parent, kn->name, new_ns);
+	return kernfs_rename_ns(kn, new_parent, NULL, new_ns);
 }
 
 /**
fs/sysfs/file.c +15 -9
index d1995e2d6c943..e9bfe3e80809d 100644
@@ -19,13 +19,19 @@
 
 #include "sysfs.h"
 
+static struct kobject *sysfs_file_kobj(struct kernfs_node *kn)
+{
+	guard(rcu)();
+	return rcu_dereference(kn->parent)->priv;
+}
+
 /*
  * Determine ktype->sysfs_ops for the given kernfs_node.  This function
  * must be called while holding an active reference.
  */
 static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
 {
-	struct kobject *kobj = kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(kn);
 
 	if (kn->flags & KERNFS_LOCKDEP)
 		lockdep_assert_held(kn);
@@ -40,7 +46,7 @@ static const struct sysfs_ops *sysfs_file_ops(struct kernfs_node *kn)
 static int sysfs_kf_seq_show(struct seq_file *sf, void *v)
 {
 	struct kernfs_open_file *of = sf->private;
-	struct kobject *kobj = of->kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(of->kn);
 	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
 	ssize_t count;
 	char *buf;
@@ -78,7 +84,7 @@ static ssize_t sysfs_kf_bin_read(struct kernfs_open_file *of, char *buf,
 				 size_t count, loff_t pos)
 {
 	struct bin_attribute *battr = of->kn->priv;
-	struct kobject *kobj = of->kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(of->kn);
 	loff_t size = file_inode(of->file)->i_size;
 
 	if (!count)
@@ -102,7 +108,7 @@ static ssize_t sysfs_kf_read(struct kernfs_open_file *of, char *buf,
 			     size_t count, loff_t pos)
 {
 	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
-	struct kobject *kobj = of->kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(of->kn);
 	ssize_t len;
 
 	/*
@@ -128,7 +134,7 @@ static ssize_t sysfs_kf_write(struct kernfs_open_file *of, char *buf,
 			      size_t count, loff_t pos)
 {
 	const struct sysfs_ops *ops = sysfs_file_ops(of->kn);
-	struct kobject *kobj = of->kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(of->kn);
 
 	if (!count)
 		return 0;
@@ -141,7 +147,7 @@ static ssize_t sysfs_kf_bin_write(struct kernfs_open_file *of, char *buf,
 				  size_t count, loff_t pos)
 {
 	struct bin_attribute *battr = of->kn->priv;
-	struct kobject *kobj = of->kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(of->kn);
 	loff_t size = file_inode(of->file)->i_size;
 
 	if (size) {
@@ -162,7 +168,7 @@ static int sysfs_kf_bin_mmap(struct kernfs_open_file *of,
 			     struct vm_area_struct *vma)
 {
 	struct bin_attribute *battr = of->kn->priv;
-	struct kobject *kobj = of->kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(of->kn);
 
 	return battr->mmap(of->file, kobj, battr, vma);
 }
@@ -171,7 +177,7 @@ static loff_t sysfs_kf_bin_llseek(struct kernfs_open_file *of, loff_t offset,
 				  int whence)
 {
 	struct bin_attribute *battr = of->kn->priv;
-	struct kobject *kobj = of->kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(of->kn);
 
 	if (battr->llseek)
 		return battr->llseek(of->file, kobj, battr, offset, whence);
@@ -482,7 +488,7 @@ EXPORT_SYMBOL_GPL(sysfs_break_active_protection);
  */
 void sysfs_unbreak_active_protection(struct kernfs_node *kn)
 {
-	struct kobject *kobj = kn->parent->priv;
+	struct kobject *kobj = sysfs_file_kobj(kn);
 
 	kernfs_unbreak_active_protection(kn);
 	kernfs_put(kn);
include/linux/kernfs.h +8 -3
index 87c79d076d6d7..c81947f022d6f 100644
@@ -147,6 +147,11 @@ enum kernfs_root_flag {
 	 * Support user xattrs to be written to nodes rooted at this root.
 	 */
 	KERNFS_ROOT_SUPPORT_USER_XATTR		= 0x0008,
+
+	/*
+	 * Renames must not change the parent node.
+	 */
+	KERNFS_ROOT_INVARIANT_PARENT		= 0x0010,
 };
 
 /* type-specific structures for kernfs_node union members */
@@ -199,8 +204,8 @@ struct kernfs_node {
 	 * never moved to a different parent, it is safe to access the
 	 * parent directly.
 	 */
-	struct kernfs_node	*parent;
-	const char		*name;
+	struct kernfs_node	__rcu *parent;
+	const char		__rcu *name;
 
 	struct rb_node		rb;
 
@@ -395,7 +400,7 @@ static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
 }
 
 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
-int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
+int kernfs_path_from_node(struct kernfs_node *kn_to, struct kernfs_node *kn_from,
 			  char *buf, size_t buflen);
 void pr_cont_kernfs_name(struct kernfs_node *kn);
 void pr_cont_kernfs_path(struct kernfs_node *kn);
kernel/cgroup/cgroup-v1.c +1 -1
index e28d5f0d20ed0..202e329759b12 100644
@@ -844,7 +844,7 @@ static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent
 
 	if (kernfs_type(kn) != KERNFS_DIR)
 		return -ENOTDIR;
-	if (kn->parent != new_parent)
+	if (rcu_dereference_check(kn->parent, true) != new_parent)
 		return -EIO;
 
 	/*
kernel/cgroup/cgroup.c +12 -5
index 044c7ba1cc482..d11d05a53783c 100644
@@ -633,9 +633,15 @@ int cgroup_task_count(const struct cgroup *cgrp)
 	return count;
 }
 
+static struct cgroup *cg_get_parent_priv(struct kernfs_node *kn)
+{
+	/* The parent can not be changed */
+	return rcu_dereference_check(kn->parent, true)->priv;
+}
+
 struct cgroup_subsys_state *of_css(struct kernfs_open_file *of)
 {
-	struct cgroup *cgrp = of->kn->parent->priv;
+	struct cgroup *cgrp = cg_get_parent_priv(of->kn);
 	struct cftype *cft = of_cft(of);
 
 	/*
@@ -1612,7 +1618,7 @@ void cgroup_kn_unlock(struct kernfs_node *kn)
 	if (kernfs_type(kn) == KERNFS_DIR)
 		cgrp = kn->priv;
 	else
-		cgrp = kn->parent->priv;
+		cgrp = cg_get_parent_priv(kn);
 
 	cgroup_unlock();
 
@@ -1644,7 +1650,7 @@ struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline)
 	if (kernfs_type(kn) == KERNFS_DIR)
 		cgrp = kn->priv;
 	else
-		cgrp = kn->parent->priv;
+		cgrp = cg_get_parent_priv(kn);
 
 	/*
 	 * We're gonna grab cgroup_mutex which nests outside kernfs
@@ -2118,7 +2124,8 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
 	root->kf_root = kernfs_create_root(kf_sops,
 					   KERNFS_ROOT_CREATE_DEACTIVATED |
 					   KERNFS_ROOT_SUPPORT_EXPORTOP |
-					   KERNFS_ROOT_SUPPORT_USER_XATTR,
+					   KERNFS_ROOT_SUPPORT_USER_XATTR |
+					   KERNFS_ROOT_INVARIANT_PARENT,
 					   root_cgrp);
 	if (IS_ERR(root->kf_root)) {
 		ret = PTR_ERR(root->kf_root);
@@ -4119,7 +4126,7 @@ static ssize_t cgroup_file_write(struct kernfs_open_file *of, char *buf,
 				 size_t nbytes, loff_t off)
 {
 	struct cgroup_file_ctx *ctx = of->priv;
-	struct cgroup *cgrp = of->kn->parent->priv;
+	struct cgroup *cgrp = cg_get_parent_priv(of->kn);
 	struct cftype *cft = of_cft(of);
 	struct cgroup_subsys_state *css;
 	int ret;
security/selinux/hooks.c +5 -2
index fc926d3cac6e2..ae06bc0e99d23 100644
@@ -3582,10 +3582,13 @@ static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
 		newsid = tsec->create_sid;
 	} else {
 		u16 secclass = inode_mode_to_security_class(kn->mode);
+		const char *kn_name;
 		struct qstr q;
 
-		q.name = kn->name;
-		q.hash_len = hashlen_string(kn_dir, kn->name);
+		/* kn is fresh, can't be renamed, name goes not away */
+		kn_name = rcu_dereference_check(kn->name, true);
+		q.name = kn_name;
+		q.hash_len = hashlen_string(kn_dir, kn_name);
 
 		rc = security_transition_sid(tsec->sid,
 					     parent_sid, secclass, &q,
Signature · 1 line
2.45.2
maintainer
#1 lore raw

Hello.

Quoted 11 lines · Sebastian Andrzej Siewior

- kernfs_rename_ns() is only using kernfs_rename_lock if the parents are different. All users users use either RCU or kernfs_rwsem.
- kernfs_fop_readdir() drops kernfs_root::kernfs_rwsem while holding a reference to name and invoking dir_emit(). This has been changed and lock is held.
- kernfs_notify_workfn() access kernfs_node::name without any protection. Added kernfs_root::kernfs_rwsem for the iteration.
- kernfs_get_parent_dentry() acquires now kernfs_root::kernfs_rwsem while accessing the parent node.
- kernfs_node_dentry() acquires now kernfs_root::kernfs_rwsem while parent is accessed and the name looked up.

Why is the kernfs_root::kernfs_rwsem newly R-taken? Shouldn't be RCU read section sufficient for those users?

(Perhaps it's related to second observation I have -- why there is sometimes kernfs_rcu_get_parent() whereas there are other call sites with mere rcu_dereference(kn->parent)?)

#2 lore raw
Quoted 1 line · Michal Koutný

Hello.

Hi Michal,

Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 12 lines

- kernfs_rename_ns() is only using kernfs_rename_lock if the parents are different. All users users use either RCU or kernfs_rwsem.
- kernfs_fop_readdir() drops kernfs_root::kernfs_rwsem while holding a reference to name and invoking dir_emit(). This has been changed and lock is held.
- kernfs_notify_workfn() access kernfs_node::name without any protection. Added kernfs_root::kernfs_rwsem for the iteration.
- kernfs_get_parent_dentry() acquires now kernfs_root::kernfs_rwsem while accessing the parent node.
- kernfs_node_dentry() acquires now kernfs_root::kernfs_rwsem while parent is accessed and the name looked up.

Quoted 2 lines

Why is the kernfs_root::kernfs_rwsem newly R-taken? Shouldn't be RCU read section sufficient for those users?

Those users. If I skip/ left something out, please poke.

kernfs_notify_workfn(). There is ilookup() -> wait_on_inode() which can sleep.
kernfs_get_parent_dentry(). There is kernfs_get_inode() -> iget_locked() which can sleep.
kernfs_node_dentry(). There is lookup_positive_unlocked() -> lookup_one_unlocked() -> lookup_slow() which might sleep.

Assuming the parent can't vanish in these cases, name could during the invocation. I can't keep the RCU read section open while there is a sleep within the call chain. Therefore I added the lock so the rcu_dereference.*() is quiet.

Quoted 3 lines

(Perhaps it's related to second observation I have -- why there is sometimes kernfs_rcu_get_parent() whereas there are other call sites with mere rcu_dereference(kn->parent)?)

rcu_dereference() is used where I was sure that there is always a RCU read section. I have kernfs_rcu_get_parent() when there is either a RCU read section or the kernfs_rwsem (or just the lock).

Sebastian

maintainer
#3 lore raw
Quoted 2 lines · Sebastian Andrzej Siewior

Assuming the parent can't vanish in these cases, name could during the invocation.

                                                   ^^^^^^^^^^

So those R-locks are likely missing even prior this RCUization (as pointed out by Tejun).
If I don't miss context, this would be better a separate pre-patch.

Quoted 3 lines

I can't keep the RCU read section open while there is a sleep within the call chain. Therefore I added the lock so the rcu_dereference.*() is quiet.

Ah, right.

Quoted 8 lines
Quoted 4 lines

(Perhaps it's related to second observation I have -- why there is sometimes kernfs_rcu_get_parent() whereas there are other call sites with mere rcu_dereference(kn->parent)?)

rcu_dereference() is used where I was sure that there is always a RCU read section. I have kernfs_rcu_get_parent() when there is either a RCU read section or the kernfs_rwsem (or just the lock).

I think context-less rcu_dereference_check wrapper (with a comment) could capture that semantics too.

Michal

maintainer
#4 lore raw
In reply to Sebastian Andrzej Siewior

Hello, sorry about the delay.

Generally looks good to me but I have some rcu deref accessor related comments.

Quoted 1 line · Sebastian Andrzej Siewior
arch/x86/kernel/cpu/resctrl/internal.h +0 -0

...

arch/x86/kernel/cpu/resctrl/internal.h int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp) · line 1317

Quoted 5 lines
arch/x86/kernel/cpu/resctrl/internal.h +1 -0
@@ -1312,6 +1314,11 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 		ret = -EINVAL;
 		goto out_region;
 	}
+	kn_name = kstrdup(rdt_kn_get_name(rdtgrp->kn), GFP_KERNEL);

Shouldn't this be freed somewhere?

Quoted 7 lines
arch/x86/kernel/cpu/resctrl/internal.h +4 -0
+	if (!kn_name) {
+		ret = -ENOMEM;
+		goto out_cstates;
+	}
 
 	plr->thread_done = 0;
 

...

Quoted 1 line
fs/kernfs/dir.c +0 -0

...

fs/kernfs/dir.c static void kernfs_free_rcu(struct rcu_head *rcu) · line 547

Quoted 7 lines
fs/kernfs/dir.c +2 -1
@@ -533,7 +543,8 @@ static void kernfs_free_rcu(struct rcu_head *rcu)
 {
 	struct kernfs_node *kn = container_of(rcu, struct kernfs_node, rcu);
 
-	kfree_const(kn->name);
+	/* If the whole node goes away, the name can't be used outside */
+	kfree_const(rcu_dereference_check(kn->name, true));

rcu_access_pointer()?

fs/kernfs/dir.c void kernfs_put(struct kernfs_node *kn) · line 577

Quoted 12 lines
fs/kernfs/dir.c +2 -1
@@ -557,16 +568,18 @@ void kernfs_put(struct kernfs_node *kn)
 	if (!kn || !atomic_dec_and_test(&kn->count))
 		return;
 	root = kernfs_root(kn);
+	guard(rcu)();
  repeat:
 	/*
 	 * Moving/renaming is always done while holding reference.
 	 * kn->parent won't change beneath us.
 	 */
-	parent = kn->parent;
+	parent = rcu_dereference(kn->parent);

I wonder whether it'd be better to encode the reference count rule (ie. add the condition kn->count == 0 to deref_check) in the kn->parent deref accessor. This function doesn't need RCU read lock and holding it makes it more confusing.

fs/kernfs/file.c static void kernfs_notify_workfn(struct work_struct *work) · line 914

Quoted 9 lines
fs/kernfs/file.c +1 -0
index 8502ef68459b9..05f7b30283150 100644
@@ -911,9 +911,11 @@ static void kernfs_notify_workfn(struct work_struct *work)
 	/* kick fsnotify */
 
 	down_read(&root->kernfs_supers_rwsem);
+	down_read(&root->kernfs_rwsem);

Why is this addition necessary? Hmm... was the code previously broken w.r.t. renaming? Can this be RCU?

fs/kernfs/mount.c static struct dentry *kernfs_fh_to_parent(struct super_block *sb, · line 151

Quoted 13 lines
fs/kernfs/mount.c +3 -1
index 1358c21837f1a..db71faba3bb53 100644
@@ -145,8 +145,10 @@ static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
 static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
 {
 	struct kernfs_node *kn = kernfs_dentry_node(child);
+	struct kernfs_root *root = kernfs_root(kn);
 
-	return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
+	guard(rwsem_read)(&root->kernfs_rwsem);
+	return d_obtain_alias(kernfs_get_inode(child->d_sb, kernfs_rcu_get_parent(kn)));

Ditto.

fs/kernfs/mount.c static struct kernfs_node *find_next_ancestor(struct kernfs_node *child, · line 193

Quoted 9 lines
fs/kernfs/mount.c +3 -2
@@ -186,10 +188,10 @@ static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
 		return NULL;
 	}
 
-	while (child->parent != parent) {
-		if (!child->parent)
+	while (kernfs_rcu_get_parent(child) != parent) {
+		child = kernfs_rcu_get_parent(child);
+		if (!child)

I think kernfs_rcu_get_parent() name is a bit confusing given that it allows derefing without RCU if the rwsem is locked. Maybe just kernfs_get_parent() or kernfs_parent()?

fs/kernfs/mount.c struct dentry *kernfs_node_dentry(struct kernfs_node *kn, · line 223

Quoted 6 lines
fs/kernfs/mount.c +2 -0
@@ -216,6 +219,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
 	if (!kn->parent)
 		return dentry;
 
+	root = kernfs_root(kn);
+	guard(rwsem_read)(&root->kernfs_rwsem);

Here too, it's a bit confusing that it's adding new locking. Was the code broken before? If so, it'd be clearer if the fixes were in their own patch.

fs/sysfs/file.c line 26

Quoted 13 lines
fs/sysfs/file.c +5 -0
index d1995e2d6c943..e9bfe3e80809d 100644
@@ -19,13 +19,19 @@
 
 #include "sysfs.h"
 
+static struct kobject *sysfs_file_kobj(struct kernfs_node *kn)
+{
+	guard(rcu)();
+	return rcu_dereference(kn->parent)->priv;
+}

I wonder whether it'd be better to rename kn->parent to something like kn->__parent (or maybe some other suffix) to clarify that the field is not to be deref'ed directly and kernfs_parent() helper is made available to the users. That way, users can benefit from the additional conditions in rcu_dereference_check().

kernel/cgroup/cgroup-v1.c static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent · line 848

Quoted 11 lines
kernel/cgroup/cgroup-v1.c +1 -1
index e28d5f0d20ed0..202e329759b12 100644
@@ -844,7 +844,7 @@ static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent
 
 	if (kernfs_type(kn) != KERNFS_DIR)
 		return -ENOTDIR;
-	if (kn->parent != new_parent)
+	if (rcu_dereference_check(kn->parent, true) != new_parent)
 		return -EIO;

This isn't being derefed, rcu_access_pointer()?

Quoted 6 lines
kernel/cgroup/cgroup-v1.c +0 -0
 	/*
kernel/cgroup/cgroup.c +0 -0
index 044c7ba1cc482..d11d05a53783c 100644

kernel/cgroup/cgroup.c int cgroup_task_count(const struct cgroup *cgrp) · line 640

Quoted 8 lines
kernel/cgroup/cgroup.c +5 -0
@@ -633,9 +633,15 @@ int cgroup_task_count(const struct cgroup *cgrp)
 	return count;
 }
 
+static struct cgroup *cg_get_parent_priv(struct kernfs_node *kn)
+{
+	/* The parent can not be changed */
+	return rcu_dereference_check(kn->parent, true)->priv;
+}

e.g. Here, it'd be a lot better if kernfs provided helper can be used so that deref condition check can be preserved.

Thanks.

Signature · 1 line
tejun
#5 lore raw
Quoted 1 line · Tejun Heo

Hello, sorry about the delay.

Hi,

Quoted 2 lines

Generally looks good to me but I have some rcu deref accessor related comments.

no worries, I also fell behind.

Quoted 10 lines · Sebastian Andrzej Siewior
Quoted 1 line
arch/x86/kernel/cpu/resctrl/internal.h +0 -0

...

arch/x86/kernel/cpu/resctrl/internal.h int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp) · line 1317

Quoted 5 lines
arch/x86/kernel/cpu/resctrl/internal.h +1 -0
@@ -1312,6 +1314,11 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 		ret = -EINVAL;
 		goto out_region;
 	}
+	kn_name = kstrdup(rdt_kn_get_name(rdtgrp->kn), GFP_KERNEL);

Shouldn't this be freed somewhere?

There is

         char *kn_name __free(kfree) = NULL;

at the top. This will kfree(kn_name) once it is out of scope (on return from rdtgroup_pseudo_lock_create()).

Quoted 14 lines
Quoted 7 lines
arch/x86/kernel/cpu/resctrl/internal.h +4 -0
+	if (!kn_name) {
+		ret = -ENOMEM;
+		goto out_cstates;
+	}
 
 	plr->thread_done = 0;
 

...

Quoted 1 line
fs/kernfs/dir.c +0 -0

...

Quoted 4 lines
fs/kernfs/dir.c +0 -0
@@ -533,7 +543,8 @@ static void kernfs_free_rcu(struct rcu_head *rcu)
 {
 	struct kernfs_node *kn = container_of(rcu, struct kernfs_node, rcu);
 
Quoted 5 lines

fs/kernfs/dir.c static void kernfs_free_rcu(struct rcu_head *rcu) · line 547

Quoted 3 lines
fs/kernfs/dir.c +2 -1
-	kfree_const(kn->name);
+	/* If the whole node goes away, the name can't be used outside */
+	kfree_const(rcu_dereference_check(kn->name, true));

rcu_access_pointer()?

oh, thanks.

Quoted 9 lines
Quoted 9 lines
fs/kernfs/dir.c +1 -0
@@ -557,16 +568,18 @@ void kernfs_put(struct kernfs_node *kn)
 	if (!kn || !atomic_dec_and_test(&kn->count))
 		return;
 	root = kernfs_root(kn);
+	guard(rcu)();
  repeat:
 	/*
 	 * Moving/renaming is always done while holding reference.
 	 * kn->parent won't change beneath us.
Quoted 8 lines

fs/kernfs/dir.c void kernfs_put(struct kernfs_node *kn) · line 577

Quoted 3 lines
fs/kernfs/dir.c +1 -1
 	 */
-	parent = kn->parent;
+	parent = rcu_dereference(kn->parent);

I wonder whether it'd be better to encode the reference count rule (ie. add the condition kn->count == 0 to deref_check) in the kn->parent deref accessor. This function doesn't need RCU read lock and holding it makes it more confusing.

You are saying that we don't need RCU here because if we drop the last reference then nobody can rename the node anymore and so parent can't change. That sounds right.
What about using rcu_dereference_protected() instead? Using rcu_dereference(x, !atomic_read(&kn->count)) looks odd given that we established that the counter is 0. Therefore I would suggest rcu_access_pointer() but the reference drop might qualify as "locked".

Quoted 7 lines
Quoted 7 lines
fs/kernfs/file.c +0 -0
index 8502ef68459b9..05f7b30283150 100644
@@ -911,9 +911,11 @@ static void kernfs_notify_workfn(struct work_struct *work)
 	/* kick fsnotify */
 
Quoted 5 lines

fs/kernfs/file.c static void kernfs_notify_workfn(struct work_struct *work) · line 914

Quoted 2 lines
fs/kernfs/file.c +1 -0
 	down_read(&root->kernfs_supers_rwsem);
+	down_read(&root->kernfs_rwsem);

Why is this addition necessary? Hmm... was the code previously broken w.r.t. renaming? Can this be RCU?

I *think* it was broken unless you unsure somehow that this can't be invoked on nodes which can be renamed.
The ensures that the later obtained kn_name does not freed after a rename.
This can not be RCU because ilookup() has wait_on_inode() (might sleep).

Quoted 10 lines
Quoted 10 lines
fs/kernfs/mount.c +1 -0
index 1358c21837f1a..db71faba3bb53 100644
@@ -145,8 +145,10 @@ static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
 static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
 {
 	struct kernfs_node *kn = kernfs_dentry_node(child);
+	struct kernfs_root *root = kernfs_root(kn);
 
Quoted 5 lines

fs/kernfs/mount.c static struct dentry *kernfs_fh_to_parent(struct super_block *sb, · line 151

Quoted 3 lines
fs/kernfs/mount.c +2 -1
-	return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
+	guard(rwsem_read)(&root->kernfs_rwsem);
+	return d_obtain_alias(kernfs_get_inode(child->d_sb, kernfs_rcu_get_parent(kn)));

Ditto.

kernfs_rcu_get_parent() gets you name from the kn. Can you ensure that it won't go away during a rename? If so, I would add the matching comment then.
There is d_obtain_alias() -> __d_obtain_alias() -> d_alloc_anon() which makes not possible to use RCU.

Quoted 4 lines
Quoted 4 lines
fs/kernfs/mount.c +0 -0
@@ -186,10 +188,10 @@ static struct kernfs_node *find_next_ancestor(struct kernfs_node *child,
 		return NULL;
 	}
 
Quoted 9 lines

fs/kernfs/mount.c static struct kernfs_node *find_next_ancestor(struct kernfs_node *child, · line 193

Quoted 5 lines
fs/kernfs/mount.c +3 -2
-	while (child->parent != parent) {
-		if (!child->parent)
+	while (kernfs_rcu_get_parent(child) != parent) {
+		child = kernfs_rcu_get_parent(child);
+		if (!child)

I think kernfs_rcu_get_parent() name is a bit confusing given that it allows derefing without RCU if the rwsem is locked. Maybe just kernfs_get_parent() or kernfs_parent()?

kernfs_get_parent() exists, switching to kernfs_parent() then.

Quoted 9 lines

fs/kernfs/mount.c struct dentry *kernfs_node_dentry(struct kernfs_node *kn, · line 223

Quoted 6 lines
fs/kernfs/mount.c +2 -0
@@ -216,6 +219,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
 	if (!kn->parent)
 		return dentry;
 
+	root = kernfs_root(kn);
+	guard(rwsem_read)(&root->kernfs_rwsem);

Here too, it's a bit confusing that it's adding new locking. Was the code broken before? If so, it'd be clearer if the fixes were in their own patch.

It dereferences name (later in lookup_positive_unlocked()). I don't see how it is safe against a rename without the lock.

If you agree that all three are bugs, that existed before, then I will extract it out of this patch.

Quoted 8 lines
Quoted 8 lines
fs/sysfs/file.c +0 -0
index d1995e2d6c943..e9bfe3e80809d 100644
@@ -19,13 +19,19 @@
 
 #include "sysfs.h"
 
Quoted 11 lines

fs/sysfs/file.c line 26

Quoted 5 lines
fs/sysfs/file.c +5 -0
+static struct kobject *sysfs_file_kobj(struct kernfs_node *kn)
+{
+	guard(rcu)();
+	return rcu_dereference(kn->parent)->priv;
+}

I wonder whether it'd be better to rename kn->parent to something like kn->__parent (or maybe some other suffix) to clarify that the field is not to be deref'ed directly and kernfs_parent() helper is made available to the users. That way, users can benefit from the additional conditions in rcu_dereference_check().

sparse should yell at people if they deference directly. I have no problem to rename it to __parent if you say so.

Quoted 6 lines
Quoted 6 lines
kernel/cgroup/cgroup-v1.c +0 -0
index e28d5f0d20ed0..202e329759b12 100644
@@ -844,7 +844,7 @@ static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent
 
Quoted 7 lines

kernel/cgroup/cgroup-v1.c static int cgroup1_rename(struct kernfs_node *kn, struct kernfs_node *new_parent · line 848

Quoted 5 lines
kernel/cgroup/cgroup-v1.c +1 -1
 	if (kernfs_type(kn) != KERNFS_DIR)
 		return -ENOTDIR;
-	if (kn->parent != new_parent)
+	if (rcu_dereference_check(kn->parent, true) != new_parent)
 		return -EIO;

This isn't being derefed, rcu_access_pointer()?

Ok.

Quoted 9 lines
Quoted 9 lines
kernel/cgroup/cgroup-v1.c +0 -0
 	/*
kernel/cgroup/cgroup.c +0 -0
index 044c7ba1cc482..d11d05a53783c 100644
@@ -633,9 +633,15 @@ int cgroup_task_count(const struct cgroup *cgrp)
 	return count;
 }
 
Quoted 8 lines

kernel/cgroup/cgroup.c int cgroup_task_count(const struct cgroup *cgrp) · line 640

Quoted 5 lines
kernel/cgroup/cgroup.c +5 -0
+static struct cgroup *cg_get_parent_priv(struct kernfs_node *kn)
+{
+	/* The parent can not be changed */
+	return rcu_dereference_check(kn->parent, true)->priv;
+}

e.g. Here, it'd be a lot better if kernfs provided helper can be used so that deref condition check can be preserved.

Something like

| static struct cgroup *cg_get_parent_priv(struct kernfs_node *kn)
| {
|       return rcu_dereference_check(kn->parent, kn->flags & KERNFS_ROOT_INVARIANT_PARENT)->priv;
| }

then?

Quoted 1 line

Thanks.

Sebastian

#6 lore raw
Quoted 18 lines · To Tejun Heo
Quoted 9 lines
Quoted 9 lines
fs/kernfs/dir.c +1 -0
@@ -557,16 +568,18 @@ void kernfs_put(struct kernfs_node *kn)
 	if (!kn || !atomic_dec_and_test(&kn->count))
 		return;
 	root = kernfs_root(kn);
+	guard(rcu)();
  repeat:
 	/*
 	 * Moving/renaming is always done while holding reference.
 	 * kn->parent won't change beneath us.
Quoted 8 lines

fs/kernfs/dir.c void kernfs_put(struct kernfs_node *kn) · line 577

Quoted 3 lines
fs/kernfs/dir.c +1 -1
 	 */
-	parent = kn->parent;
+	parent = rcu_dereference(kn->parent);

I wonder whether it'd be better to encode the reference count rule (ie. add the condition kn->count == 0 to deref_check) in the kn->parent deref accessor. This function doesn't need RCU read lock and holding it makes it more confusing.

Quoted 7 lines

You are saying that we don't need RCU here because if we drop the last reference then nobody can rename the node anymore and so parent can't change. That sounds right.
What about using rcu_dereference_protected() instead? Using rcu_dereference(x, !atomic_read(&kn->count)) looks odd given that we established that the counter is 0. Therefore I would suggest rcu_access_pointer() but the reference drop might qualify as "locked".

Ehm or indeed rcu_access_pointer() given that _protected() requires a second argument…

Sebastian

maintainer
#7 lore raw
In reply to Sebastian Andrzej Siewior

Hello,

Quoted 7 lines · Sebastian Andrzej Siewior
Quoted 1 line

Shouldn't this be freed somewhere?

There is

         char *kn_name __free(kfree) = NULL;

at the top. This will kfree(kn_name) once it is out of scope (on return from rdtgroup_pseudo_lock_create()).

Ah, makes sense.

...

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
fs/kernfs/dir.c +1 -0
@@ -557,16 +568,18 @@ void kernfs_put(struct kernfs_node *kn)
 	if (!kn || !atomic_dec_and_test(&kn->count))
 		return;
 	root = kernfs_root(kn);
+	guard(rcu)();
  repeat:
 	/*
 	 * Moving/renaming is always done while holding reference.
 	 * kn->parent won't change beneath us.
Quoted 8 lines

fs/kernfs/dir.c void kernfs_put(struct kernfs_node *kn) · line 577

Quoted 3 lines
fs/kernfs/dir.c +1 -1
 	 */
-	parent = kn->parent;
+	parent = rcu_dereference(kn->parent);

I wonder whether it'd be better to encode the reference count rule (ie. add the condition kn->count == 0 to deref_check) in the kn->parent deref accessor. This function doesn't need RCU read lock and holding it makes it more confusing.

Quoted 7 lines

You are saying that we don't need RCU here because if we drop the last reference then nobody can rename the node anymore and so parent can't change. That sounds right.
What about using rcu_dereference_protected() instead? Using rcu_dereference(x, !atomic_read(&kn->count)) looks odd given that we established that the counter is 0. Therefore I would suggest rcu_access_pointer() but the reference drop might qualify as "locked".

I think it's usually a better form to encode the whole access rule in a shared accessor for the field so that the deref rules for the field can be understood and enforced from the shared accessor and the shared accessor would use rcu_dereference_protected() internally.

Quoted 13 lines
Quoted 7 lines
Quoted 7 lines
fs/kernfs/file.c +0 -0
index 8502ef68459b9..05f7b30283150 100644
@@ -911,9 +911,11 @@ static void kernfs_notify_workfn(struct work_struct *work)
 	/* kick fsnotify */
 
Quoted 5 lines

fs/kernfs/file.c static void kernfs_notify_workfn(struct work_struct *work) · line 914

Quoted 2 lines
fs/kernfs/file.c +1 -0
 	down_read(&root->kernfs_supers_rwsem);
+	down_read(&root->kernfs_rwsem);

Why is this addition necessary? Hmm... was the code previously broken w.r.t. renaming? Can this be RCU?

Quoted 5 lines

I *think* it was broken unless you unsure somehow that this can't be invoked on nodes which can be renamed.
The ensures that the later obtained kn_name does not freed after a rename.
This can not be RCU because ilookup() has wait_on_inode() (might sleep).

If it was broken, let's separate it out to its own patch.

Quoted 16 lines
Quoted 10 lines
Quoted 10 lines
fs/kernfs/mount.c +1 -0
index 1358c21837f1a..db71faba3bb53 100644
@@ -145,8 +145,10 @@ static struct dentry *kernfs_fh_to_parent(struct super_block *sb,
 static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
 {
 	struct kernfs_node *kn = kernfs_dentry_node(child);
+	struct kernfs_root *root = kernfs_root(kn);
 
Quoted 5 lines

fs/kernfs/mount.c static struct dentry *kernfs_fh_to_parent(struct super_block *sb, · line 151

Quoted 3 lines
fs/kernfs/mount.c +2 -1
-	return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
+	guard(rwsem_read)(&root->kernfs_rwsem);
+	return d_obtain_alias(kernfs_get_inode(child->d_sb, kernfs_rcu_get_parent(kn)));

Ditto.

Quoted 5 lines

kernfs_rcu_get_parent() gets you name from the kn. Can you ensure that it won't go away during a rename? If so, I would add the matching comment then.
There is d_obtain_alias() -> __d_obtain_alias() -> d_alloc_anon() which makes not possible to use RCU.

If true, better to put it in its own prep patch, I think.

...

Quoted 10 lines
Quoted 9 lines

fs/kernfs/mount.c struct dentry *kernfs_node_dentry(struct kernfs_node *kn, · line 223

Quoted 6 lines
fs/kernfs/mount.c +2 -0
@@ -216,6 +219,9 @@ struct dentry *kernfs_node_dentry(struct kernfs_node *kn,
 	if (!kn->parent)
 		return dentry;
 
+	root = kernfs_root(kn);
+	guard(rwsem_read)(&root->kernfs_rwsem);

Here too, it's a bit confusing that it's adding new locking. Was the code broken before? If so, it'd be clearer if the fixes were in their own patch.

Quoted 5 lines

It dereferences name (later in lookup_positive_unlocked()). I don't see how it is safe against a rename without the lock.

If you agree that all three are bugs, that existed before, then I will extract it out of this patch.

Not sure whether they're all correct but let's separate them out.

...

Quoted 8 lines
Quoted 5 lines

I wonder whether it'd be better to rename kn->parent to something like kn->__parent (or maybe some other suffix) to clarify that the field is not to be deref'ed directly and kernfs_parent() helper is made available to the users. That way, users can benefit from the additional conditions in rcu_dereference_check().

sparse should yell at people if they deference directly. I have no problem to rename it to __parent if you say so.

Sparse isn't useless but also often ignored. Probably better to be explicit.

...

Quoted 8 lines
Quoted 2 lines

e.g. Here, it'd be a lot better if kernfs provided helper can be used so that deref condition check can be preserved.

Something like

| static struct cgroup *cg_get_parent_priv(struct kernfs_node *kn)
| {
|       return rcu_dereference_check(kn->parent, kn->flags & KERNFS_ROOT_INVARIANT_PARENT)->priv;
| }

Maybe a shorter name?

Thanks.

Signature · 1 line
tejun