Skip to content

Commit 1270dd8

Browse files
Alexey Dobriyantorvalds
authored andcommitted
fs/proc: calculate /proc/* and /proc/*/task/* nlink at init time
Runtime nlink calculation works but meh. I don't know how to do it at compile time, but I know how to do it at init time. Shift "2+" part into init time as a bonus. Link: http://lkml.kernel.org/r/20161122195549.GB29812@avx2 Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com> Reviewed-by: Vegard Nossum <vegard.nossum@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bac5f5d commit 1270dd8

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
* in /proc for a task before it execs a suid executable.
105105
*/
106106

107+
static u8 nlink_tid;
108+
static u8 nlink_tgid;
109+
107110
struct pid_entry {
108111
const char *name;
109112
unsigned int len;
@@ -139,13 +142,13 @@ struct pid_entry {
139142
* Count the number of hardlinks for the pid_entry table, excluding the .
140143
* and .. links.
141144
*/
142-
static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145+
static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
143146
unsigned int n)
144147
{
145148
unsigned int i;
146149
unsigned int count;
147150

148-
count = 0;
151+
count = 2;
149152
for (i = 0; i < n; ++i) {
150153
if (S_ISDIR(entries[i].mode))
151154
++count;
@@ -3068,8 +3071,7 @@ static int proc_pid_instantiate(struct inode *dir,
30683071
inode->i_fop = &proc_tgid_base_operations;
30693072
inode->i_flags|=S_IMMUTABLE;
30703073

3071-
set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3072-
ARRAY_SIZE(tgid_base_stuff)));
3074+
set_nlink(inode, nlink_tgid);
30733075

30743076
d_set_d_op(dentry, &pid_dentry_operations);
30753077

@@ -3361,8 +3363,7 @@ static int proc_task_instantiate(struct inode *dir,
33613363
inode->i_fop = &proc_tid_base_operations;
33623364
inode->i_flags|=S_IMMUTABLE;
33633365

3364-
set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3365-
ARRAY_SIZE(tid_base_stuff)));
3366+
set_nlink(inode, nlink_tid);
33663367

33673368
d_set_d_op(dentry, &pid_dentry_operations);
33683369

@@ -3552,3 +3553,9 @@ static const struct file_operations proc_task_operations = {
35523553
.iterate_shared = proc_task_readdir,
35533554
.llseek = generic_file_llseek,
35543555
};
3556+
3557+
void __init set_proc_pid_nlink(void)
3558+
{
3559+
nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3560+
nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3561+
}

‎fs/proc/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ extern const struct inode_operations proc_link_inode_operations;
211211
extern const struct inode_operations proc_pid_link_inode_operations;
212212

213213
extern void proc_init_inodecache(void);
214+
void set_proc_pid_nlink(void);
214215
extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
215216
extern int proc_fill_super(struct super_block *, void *data, int flags);
216217
extern void proc_entry_rundown(struct proc_dir_entry *);

‎fs/proc/root.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ void __init proc_root_init(void)
122122
int err;
123123

124124
proc_init_inodecache();
125+
set_proc_pid_nlink();
125126
err = register_filesystem(&proc_fs_type);
126127
if (err)
127128
return;

0 commit comments

Comments
 (0)