Skip to content

Commit 021ada7

Browse files
author
Al Viro
committed
procfs: switch /proc/self away from proc_dir_entry
Just have it pinned in dcache all along and let procfs ->kill_sb() drop it before kill_anon_super(). Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 0ecc833 commit 021ada7

6 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎fs/proc/base.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,7 +2794,7 @@ static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter ite
27942794
return iter;
27952795
}
27962796

2797-
#define TGID_OFFSET (FIRST_PROCESS_ENTRY)
2797+
#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
27982798

27992799
static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
28002800
struct tgid_iter iter)
@@ -2817,13 +2817,21 @@ int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
28172817
struct tgid_iter iter;
28182818
struct pid_namespace *ns;
28192819
filldir_t __filldir;
2820+
loff_t pos = filp->f_pos;
28202821

2821-
if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
2822+
if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
28222823
goto out;
28232824

2824-
ns = filp->f_dentry->d_sb->s_fs_info;
2825+
if (pos == TGID_OFFSET - 1) {
2826+
if (proc_fill_cache(filp, dirent, filldir, "self", 4,
2827+
NULL, NULL, NULL) < 0)
2828+
goto out;
2829+
iter.tgid = 0;
2830+
} else {
2831+
iter.tgid = pos - TGID_OFFSET;
2832+
}
28252833
iter.task = NULL;
2826-
iter.tgid = filp->f_pos - TGID_OFFSET;
2834+
ns = filp->f_dentry->d_sb->s_fs_info;
28272835
for (iter = next_tgid(ns, iter);
28282836
iter.task;
28292837
iter.tgid += 1, iter = next_tgid(ns, iter)) {

‎fs/proc/inode.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,5 +506,5 @@ int proc_fill_super(struct super_block *s)
506506
return -ENOMEM;
507507
}
508508

509-
return 0;
509+
return proc_setup_self(s);
510510
}

‎fs/proc/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,4 @@ int proc_setattr(struct dentry *dentry, struct iattr *attr);
205205
extern const struct inode_operations proc_ns_dir_inode_operations;
206206
extern const struct file_operations proc_ns_dir_operations;
207207

208+
extern int proc_setup_self(struct super_block *);

‎fs/proc/root.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ static void proc_kill_sb(struct super_block *sb)
137137
struct pid_namespace *ns;
138138

139139
ns = (struct pid_namespace *)sb->s_fs_info;
140+
if (ns->proc_self)
141+
dput(ns->proc_self);
140142
kill_anon_super(sb);
141143
put_pid_ns(ns);
142144
}

‎fs/proc/self.c‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#include <linux/proc_fs.h>
21
#include <linux/sched.h>
32
#include <linux/namei.h>
3+
#include <linux/pid_namespace.h>
4+
#include "internal.h"
45

56
/*
67
* /proc/self:
@@ -48,12 +49,43 @@ static const struct inode_operations proc_self_inode_operations = {
4849
.put_link = proc_self_put_link,
4950
};
5051

51-
void __init proc_self_init(void)
52+
static unsigned self_inum;
53+
54+
int proc_setup_self(struct super_block *s)
5255
{
53-
struct proc_dir_entry *proc_self_symlink;
54-
umode_t mode;
56+
struct inode *root_inode = s->s_root->d_inode;
57+
struct pid_namespace *ns = s->s_fs_info;
58+
struct dentry *self;
59+
60+
mutex_lock(&root_inode->i_mutex);
61+
self = d_alloc_name(s->s_root, "self");
62+
if (self) {
63+
struct inode *inode = new_inode_pseudo(s);
64+
if (inode) {
65+
inode->i_ino = self_inum;
66+
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
67+
inode->i_mode = S_IFLNK | S_IRWXUGO;
68+
inode->i_uid = GLOBAL_ROOT_UID;
69+
inode->i_gid = GLOBAL_ROOT_GID;
70+
inode->i_op = &proc_self_inode_operations;
71+
d_add(self, inode);
72+
} else {
73+
dput(self);
74+
self = ERR_PTR(-ENOMEM);
75+
}
76+
} else {
77+
self = ERR_PTR(-ENOMEM);
78+
}
79+
mutex_unlock(&root_inode->i_mutex);
80+
if (IS_ERR(self)) {
81+
pr_err("proc_fill_super: can't allocate /proc/self\n");
82+
return PTR_ERR(self);
83+
}
84+
ns->proc_self = self;
85+
return 0;
86+
}
5587

56-
mode = S_IFLNK | S_IRWXUGO;
57-
proc_self_symlink = proc_create("self", mode, NULL, NULL );
58-
proc_self_symlink->proc_iops = &proc_self_inode_operations;
88+
void __init proc_self_init(void)
89+
{
90+
proc_alloc_inum(&self_inum);
5991
}

‎include/linux/pid_namespace.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct pid_namespace {
2828
struct pid_namespace *parent;
2929
#ifdef CONFIG_PROC_FS
3030
struct vfsmount *proc_mnt;
31+
struct dentry *proc_self;
3132
#endif
3233
#ifdef CONFIG_BSD_PROCESS_ACCT
3334
struct bsd_acct_struct *bacct;

0 commit comments

Comments
 (0)