proc: only bump parent nlink when registering directories
Subsystems (2)
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().
--- fs/proc/generic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
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);return dp;
Signature · 1 line
2.54.0
Quoted 28 lines · Krzysztof Wilczyński
proc_register()increments the parent directory's link count for every entry it registers, whileremove_proc_entry()andremove_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/00with 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 underproc_subdir_lock"), the increment lived inproc_mkdir_data()andproc_create_mount_point(), and was therefore applied only to directories. Moving it intoproc_register()to bring it underproc_subdir_lockdropped theS_ISDIRcheck.Thus, increment the parent link count only for directories, matching the decrements in
remove_proc_entry()andremove_proc_subtree().Fixes e06689bf5701 ("proc: change ->nlink under proc_subdir_lock")Cc [email protected] # v5.5+Signed-off-by Krzysztof Wilczyński <[email protected]>
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.
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