Skip to contentSkip to conversation

proc: only bump parent nlink when registering directories

PATCH Superseded Krzysztof Wilczyński 2 participants 3 messages Jun 12 – Jun 13

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 proc: only bump parent nlink when registering directories

proc_register() increments the parent directory's link count for every entry it registers, while remove_proc_entry() and remove_proc_subtree() decrement it only when the removed entry is a directory. Regular files thus inflate the parent's count while they exist, and leak one link permanently on every create and remove cycle.

For example, /proc/bus/pci/00 with twenty-two device files and no subdirectories reports nlink 24 instead of 2, and SR-IOV VF enable and disable cycles, each creating and removing the VF config space entries under /proc/bus/pci/<bus>, inflate the link count of that directory without bound.

Before commit e06689bf5701 ("proc: change ->nlink under proc_subdir_lock"), the increment lived in proc_mkdir_data() and proc_create_mount_point(), and was therefore applied only to directories. Moving it into proc_register() to bring it under proc_subdir_lock dropped the S_ISDIR check.

Thus, increment the parent link count only for directories, matching the decrements in remove_proc_entry() and remove_proc_subtree().

Fixes e06689bf5701 ("proc: change ->nlink under proc_subdir_lock")
---
 fs/proc/generic.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/proc/generic.c +2 -1
index 8bb81e58c9d8..3ac825c79313 100644
@@ -404,7 +404,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
 		write_unlock(&proc_subdir_lock);
 		goto out_free_inum;
 	}
-	dir->nlink++;
+	if (S_ISDIR(dp->mode))
+		dir->nlink++;
 	write_unlock(&proc_subdir_lock);
 
 	return dp;
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Krzysztof Wilczyński

proc_register() increments the parent directory's link count for every entry it registers, while remove_proc_entry() and remove_proc_subtree() decrement it only when the removed entry is a directory. Regular files thus inflate the parent's count while they exist, and leak one link permanently on every create and remove cycle.

For example, /proc/bus/pci/00 with twenty-two device files and no subdirectories reports nlink 24 instead of 2, and SR-IOV VF enable and disable cycles, each creating and removing the VF config space entries under /proc/bus/pci/<bus>, inflate the link count of that directory without bound.

Before commit e06689bf5701 ("proc: change ->nlink under proc_subdir_lock"), the increment lived in proc_mkdir_data() and proc_create_mount_point(), and was therefore applied only to directories. Moving it into proc_register() to bring it under proc_subdir_lock dropped the S_ISDIR check.

Thus, increment the parent link count only for directories, matching the decrements in remove_proc_entry() and remove_proc_subtree().

Fixes e06689bf5701 ("proc: change ->nlink under proc_subdir_lock")

fs/proc/generic.c struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, · line 409

Quoted 7 lines
fs/proc/generic.c +2 -1
@@ -404,7 +404,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
 		write_unlock(&proc_subdir_lock);
 		goto out_free_inum;
 	}
-	dir->nlink++;
+	if (S_ISDIR(dp->mode))
+		dir->nlink++;
 	write_unlock(&proc_subdir_lock);

nlink accounting should be folded into pde_subdir_insert/pde_erase probably but as minimal patch it's OK.

#2 lore raw

Hello,

Quoted 5 lines
Quoted 5 lines
fs/proc/generic.c +0 -0
@@ -404,7 +404,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
 		write_unlock(&proc_subdir_lock);
 		goto out_free_inum;
Quoted 8 lines

fs/proc/generic.c struct proc_dir_entry *proc_register(struct proc_dir_entry *dir, · line 409

Quoted 5 lines
fs/proc/generic.c +2 -1
 	}
-	dir->nlink++;
+	if (S_ISDIR(dp->mode))
+		dir->nlink++;
 	write_unlock(&proc_subdir_lock);

nlink accounting should be folded into pde_subdir_insert/pde_erase probably but as minimal patch it's OK.

I will follow-up with a v2, then. Thank you for the review!

All the best,

Krzysztof