Skip to contentSkip to conversation

fix proc_sys_compare() handling of in-lookup dentries

RFC PATCH In review Al Viro 3 participants 4 messages Jun 30, 2025 – Jul 1, 2025

Subsystems (4)
lore
Share & tools
lore

Al Viro

author maintainer
#0 lore raw

RFC PATCH 1/1 fix proc_sys_compare() handling of in-lookup dentries

[In #fixes, I'll send a pull request in a few days unless anybody objects]

There's one case where ->d_compare() can be called for an in-lookup dentry; usually that's nothing special from ->d_compare() point of view, but proc_sys_compare() is... unique.

The thing is, /proc/sys subdirectories can look differently for different processes. Up to and including having the same name resolve to different dentries - all of them hashed.

The way it's done is ->d_compare() refusing to admit a match unless this dentry is supposed to be visible to this caller. The information needed to discriminate between them is stored in inode; it is set during proc_sys_lookup() and until it's done d_splice_alias() we really can't tell who should that dentry be visible for.

Normally there's no negative dentries in /proc/sys; we can run into a dying dentry in RCU dcache lookup, but those can be safely rejected.

However, ->d_compare() is also called for in-lookup dentries, before they get positive - or hashed, for that matter. In case of match we will wait until dentry leaves in-lookup state and repeat ->d_compare() afterwards. In other words, the right behaviour is to treat the name match as sufficient for in-lookup dentries; if dentry is not for us, we'll see that when we recheck once proc_sys_lookup() is done with it.

Fixes d9171b934526 ("parallel lookups machinery, part 4 (and last)")

---

fs/proc/proc_sysctl.c +10 -6
index cc9d74a06ff0..b0ff2d21a3d9 100644
@@ -918,16 +918,20 @@ static int proc_sys_compare(const struct dentry *dentry,
 	struct ctl_table_header *head;
 	struct inode *inode;
 
-	/* Although proc doesn't have negative dentries, rcu-walk means
-	 * that inode here can be NULL */
-	/* AV: can it, indeed? */
-	inode = d_inode_rcu(dentry);
-	if (!inode)
-		return 1;
 	if (name->len != len)
 		return 1;
 	if (memcmp(name->name, str, len))
 		return 1;
+
+	// false positive is fine here - we'll recheck anyway
+	if (d_in_lookup(dentry))
+		return 0;
+
+	inode = d_inode_rcu(dentry);
+	// we just might have run into dentry in the middle of __dentry_kill()
+	if (!inode)
+		return 1;
+
 	head = rcu_dereference(PROC_I(inode)->sysctl);
 	return !head || !sysctl_is_seen(head);
 }
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Al Viro

[In #fixes, I'll send a pull request in a few days unless anybody objects]

There's one case where ->d_compare() can be called for an in-lookup dentry; usually that's nothing special from ->d_compare() point of view, but proc_sys_compare() is... unique.

The thing is, /proc/sys subdirectories can look differently for different processes. Up to and including having the same name resolve to different dentries - all of them hashed.

The way it's done is ->d_compare() refusing to admit a match unless this dentry is supposed to be visible to this caller. The information needed to discriminate between them is stored in inode; it is set during proc_sys_lookup() and until it's done d_splice_alias() we really can't tell who should that dentry be visible for.

Normally there's no negative dentries in /proc/sys; we can run into a dying dentry in RCU dcache lookup, but those can be safely rejected.

However, ->d_compare() is also called for in-lookup dentries, before they get positive - or hashed, for that matter. In case of match we will wait until dentry leaves in-lookup state and repeat ->d_compare() afterwards. In other words, the right behaviour is to treat the name match as sufficient for in-lookup dentries; if dentry is not for us, we'll see that when we recheck once proc_sys_lookup() is done with it.

Quoted 2 lines
Fixes d9171b934526 ("parallel lookups machinery, part 4 (and last)")
                           ^should be "neil" :-(
Quoted 1 line

Checking the name first is definitely cleaner. The fact that d_in_lookup() allows the rest to be short-circuited is neat but certainly deserves the comment.

NeilBrown

Quoted 33 lines

---

fs/proc/proc_sysctl.c +10 -6
index cc9d74a06ff0..b0ff2d21a3d9 100644
@@ -918,16 +918,20 @@ static int proc_sys_compare(const struct dentry *dentry,
 	struct ctl_table_header *head;
 	struct inode *inode;
 
-	/* Although proc doesn't have negative dentries, rcu-walk means
-	 * that inode here can be NULL */
-	/* AV: can it, indeed? */
-	inode = d_inode_rcu(dentry);
-	if (!inode)
-		return 1;
 	if (name->len != len)
 		return 1;
 	if (memcmp(name->name, str, len))
 		return 1;
+
+	// false positive is fine here - we'll recheck anyway
+	if (d_in_lookup(dentry))
+		return 0;
+
+	inode = d_inode_rcu(dentry);
+	// we just might have run into dentry in the middle of __dentry_kill()
+	if (!inode)
+		return 1;
+
 	head = rcu_dereference(PROC_I(inode)->sysctl);
 	return !head || !sysctl_is_seen(head);
 }
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Al Viro
Quoted 27 lines · Al Viro

[In #fixes, I'll send a pull request in a few days unless anybody objects]

There's one case where ->d_compare() can be called for an in-lookup dentry; usually that's nothing special from ->d_compare() point of view, but proc_sys_compare() is... unique.

The thing is, /proc/sys subdirectories can look differently for different processes. Up to and including having the same name resolve to different dentries - all of them hashed.

The way it's done is ->d_compare() refusing to admit a match unless this dentry is supposed to be visible to this caller. The information needed to discriminate between them is stored in inode; it is set during proc_sys_lookup() and until it's done d_splice_alias() we really can't tell who should that dentry be visible for.

Normally there's no negative dentries in /proc/sys; we can run into a dying dentry in RCU dcache lookup, but those can be safely rejected.

However, ->d_compare() is also called for in-lookup dentries, before they get positive - or hashed, for that matter. In case of match we will wait until dentry leaves in-lookup state and repeat ->d_compare() afterwards. In other words, the right behaviour is to treat the name match as sufficient for in-lookup dentries; if dentry is not for us, we'll see that when we recheck once proc_sys_lookup() is done with it.

Quoted 4 lines
Fixes d9171b934526 ("parallel lookups machinery, part 4 (and last)")

---

author maintainer
#2 lore raw
In reply to NeilBrown
Quoted 3 lines · NeilBrown

Checking the name first is definitely cleaner. The fact that d_in_lookup() allows the rest to be short-circuited is neat but certainly deserves the comment.

FWIW, I wonder what would be the best tree for the commit below; it's
a resurrected patch from last year, slightly updated to cover the in-lookup vs. ->d_compare(). By default it'll probably go into viro/vfs.git #work.misc...

[PATCH] documenting RCU pathwalk exposure from filesystems' POV

---

Documentation/filesystems/index.rst +2 -0
index 11a599387266..ce11c9bde4b3 100644
@@ -41,6 +41,8 @@ algorithms work.
 
    caching/index
 
+   rcu-exposure
+
    porting
 
 Filesystem support layers
Documentation/filesystems/rcu-exposure.rst new file +426 -0

426 lines not on this page · raw message