Skip to content

Commit 925d1c4

Browse files
Matt Helsleytorvalds
authored andcommitted
procfs task exe symlink
The kernel implements readlink of /proc/pid/exe by getting the file from the first executable VMA. Then the path to the file is reconstructed and reported as the result. Because of the VMA walk the code is slightly different on nommu systems. This patch avoids separate /proc/pid/exe code on nommu systems. Instead of walking the VMAs to find the first executable file-backed VMA we store a reference to the exec'd file in the mm_struct. That reference would prevent the filesystem holding the executable file from being unmounted even after unmapping the VMAs. So we track the number of VM_EXECUTABLE VMAs and drop the new reference when the last one is unmapped. This avoids pinning the mounted filesystem. [akpm@linux-foundation.org: improve comments] [yamamoto@valinux.co.jp: fix dup_mmap] Signed-off-by: Matt Helsley <matthltc@us.ibm.com> Cc: Oleg Nesterov <oleg@tv-sign.ru> Cc: David Howells <dhowells@redhat.com> Cc:"Eric W. Biederman" <ebiederm@xmission.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Hugh Dickins <hugh@veritas.com> Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e93b4ea commit 925d1c4

12 files changed

Lines changed: 157 additions & 81 deletions

File tree

‎fs/binfmt_flat.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ static int load_flat_file(struct linux_binprm * bprm,
531531
DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
532532

533533
down_write(&current->mm->mmap_sem);
534-
textpos = do_mmap(bprm->file, 0, text_len, PROT_READ|PROT_EXEC, MAP_PRIVATE, 0);
534+
textpos = do_mmap(bprm->file, 0, text_len, PROT_READ|PROT_EXEC,
535+
MAP_PRIVATE|MAP_EXECUTABLE, 0);
535536
up_write(&current->mm->mmap_sem);
536537
if (!textpos || textpos >= (unsigned long) -4096) {
537538
if (!textpos)

‎fs/exec.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,8 @@ int flush_old_exec(struct linux_binprm * bprm)
964964
if (retval)
965965
goto out;
966966

967+
set_mm_exe_file(bprm->mm, bprm->file);
968+
967969
/*
968970
* Release all of the old mmap stuff
969971
*/

‎fs/proc/base.c‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,81 @@ static const struct file_operations proc_pid_sched_operations = {
11811181

11821182
#endif
11831183

1184+
/*
1185+
* We added or removed a vma mapping the executable. The vmas are only mapped
1186+
* during exec and are not mapped with the mmap system call.
1187+
* Callers must hold down_write() on the mm's mmap_sem for these
1188+
*/
1189+
void added_exe_file_vma(struct mm_struct *mm)
1190+
{
1191+
mm->num_exe_file_vmas++;
1192+
}
1193+
1194+
void removed_exe_file_vma(struct mm_struct *mm)
1195+
{
1196+
mm->num_exe_file_vmas--;
1197+
if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1198+
fput(mm->exe_file);
1199+
mm->exe_file = NULL;
1200+
}
1201+
1202+
}
1203+
1204+
void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1205+
{
1206+
if (new_exe_file)
1207+
get_file(new_exe_file);
1208+
if (mm->exe_file)
1209+
fput(mm->exe_file);
1210+
mm->exe_file = new_exe_file;
1211+
mm->num_exe_file_vmas = 0;
1212+
}
1213+
1214+
struct file *get_mm_exe_file(struct mm_struct *mm)
1215+
{
1216+
struct file *exe_file;
1217+
1218+
/* We need mmap_sem to protect against races with removal of
1219+
* VM_EXECUTABLE vmas */
1220+
down_read(&mm->mmap_sem);
1221+
exe_file = mm->exe_file;
1222+
if (exe_file)
1223+
get_file(exe_file);
1224+
up_read(&mm->mmap_sem);
1225+
return exe_file;
1226+
}
1227+
1228+
void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1229+
{
1230+
/* It's safe to write the exe_file pointer without exe_file_lock because
1231+
* this is called during fork when the task is not yet in /proc */
1232+
newmm->exe_file = get_mm_exe_file(oldmm);
1233+
}
1234+
1235+
static int proc_exe_link(struct inode *inode, struct path *exe_path)
1236+
{
1237+
struct task_struct *task;
1238+
struct mm_struct *mm;
1239+
struct file *exe_file;
1240+
1241+
task = get_proc_task(inode);
1242+
if (!task)
1243+
return -ENOENT;
1244+
mm = get_task_mm(task);
1245+
put_task_struct(task);
1246+
if (!mm)
1247+
return -ENOENT;
1248+
exe_file = get_mm_exe_file(mm);
1249+
mmput(mm);
1250+
if (exe_file) {
1251+
*exe_path = exe_file->f_path;
1252+
path_get(&exe_file->f_path);
1253+
fput(exe_file);
1254+
return 0;
1255+
} else
1256+
return -ENOENT;
1257+
}
1258+
11841259
static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
11851260
{
11861261
struct inode *inode = dentry->d_inode;

‎fs/proc/internal.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ extern int maps_protect;
4848

4949
extern void create_seq_entry(char *name, mode_t mode,
5050
const struct file_operations *f);
51-
extern int proc_exe_link(struct inode *, struct path *);
5251
extern int proc_tid_stat(struct seq_file *m, struct pid_namespace *ns,
5352
struct pid *pid, struct task_struct *task);
5453
extern int proc_tgid_stat(struct seq_file *m, struct pid_namespace *ns,

‎fs/proc/task_mmu.c‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,6 @@ int task_statm(struct mm_struct *mm, int *shared, int *text,
7575
return mm->total_vm;
7676
}
7777

78-
int proc_exe_link(struct inode *inode, struct path *path)
79-
{
80-
struct vm_area_struct * vma;
81-
int result = -ENOENT;
82-
struct task_struct *task = get_proc_task(inode);
83-
struct mm_struct * mm = NULL;
84-
85-
if (task) {
86-
mm = get_task_mm(task);
87-
put_task_struct(task);
88-
}
89-
if (!mm)
90-
goto out;
91-
down_read(&mm->mmap_sem);
92-
93-
vma = mm->mmap;
94-
while (vma) {
95-
if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file)
96-
break;
97-
vma = vma->vm_next;
98-
}
99-
100-
if (vma) {
101-
*path = vma->vm_file->f_path;
102-
path_get(&vma->vm_file->f_path);
103-
result = 0;
104-
}
105-
106-
up_read(&mm->mmap_sem);
107-
mmput(mm);
108-
out:
109-
return result;
110-
}
111-
11278
static void pad_len_spaces(struct seq_file *m, int len)
11379
{
11480
len = 25 + sizeof(void*) * 6 - len;

‎fs/proc/task_nommu.c‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,6 @@ int task_statm(struct mm_struct *mm, int *shared, int *text,
103103
return size;
104104
}
105105

106-
int proc_exe_link(struct inode *inode, struct path *path)
107-
{
108-
struct vm_list_struct *vml;
109-
struct vm_area_struct *vma;
110-
struct task_struct *task = get_proc_task(inode);
111-
struct mm_struct *mm = get_task_mm(task);
112-
int result = -ENOENT;
113-
114-
if (!mm)
115-
goto out;
116-
down_read(&mm->mmap_sem);
117-
118-
vml = mm->context.vmlist;
119-
vma = NULL;
120-
while (vml) {
121-
if ((vml->vma->vm_flags & VM_EXECUTABLE) && vml->vma->vm_file) {
122-
vma = vml->vma;
123-
break;
124-
}
125-
vml = vml->next;
126-
}
127-
128-
if (vma) {
129-
*path = vma->vm_file->f_path;
130-
path_get(&vma->vm_file->f_path);
131-
result = 0;
132-
}
133-
134-
up_read(&mm->mmap_sem);
135-
mmput(mm);
136-
out:
137-
return result;
138-
}
139-
140106
/*
141107
* display mapping lines for a particular process's /proc/pid/maps
142108
*/

‎include/linux/mm.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,19 @@ extern void unlink_file_vma(struct vm_area_struct *);
10661066
extern struct vm_area_struct *copy_vma(struct vm_area_struct **,
10671067
unsigned long addr, unsigned long len, pgoff_t pgoff);
10681068
extern void exit_mmap(struct mm_struct *);
1069+
1070+
#ifdef CONFIG_PROC_FS
1071+
/* From fs/proc/base.c. callers must _not_ hold the mm's exe_file_lock */
1072+
extern void added_exe_file_vma(struct mm_struct *mm);
1073+
extern void removed_exe_file_vma(struct mm_struct *mm);
1074+
#else
1075+
static inline void added_exe_file_vma(struct mm_struct *mm)
1076+
{}
1077+
1078+
static inline void removed_exe_file_vma(struct mm_struct *mm)
1079+
{}
1080+
#endif /* CONFIG_PROC_FS */
1081+
10691082
extern int may_expand_vm(struct mm_struct *mm, unsigned long npages);
10701083
extern int install_special_mapping(struct mm_struct *mm,
10711084
unsigned long addr, unsigned long len,

‎include/linux/mm_types.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ struct mm_struct {
229229
struct task_struct *owner; /* The thread group leader that */
230230
/* owns the mm_struct. */
231231
#endif
232+
233+
#ifdef CONFIG_PROC_FS
234+
/* store ref to file /proc/<pid>/exe symlink points to */
235+
struct file *exe_file;
236+
unsigned long num_exe_file_vmas;
237+
#endif
232238
};
233239

234240
#endif /* _LINUX_MM_TYPES_H */

‎include/linux/proc_fs.h‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
struct net;
1111
struct completion;
12-
1312
/*
1413
* The proc filesystem constants/structures
1514
*/
@@ -206,6 +205,12 @@ extern void proc_net_remove(struct net *net, const char *name);
206205
extern struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
207206
struct proc_dir_entry *parent);
208207

208+
/* While the {get|set|dup}_mm_exe_file functions are for mm_structs, they are
209+
* only needed to implement /proc/<pid>|self/exe so we define them here. */
210+
extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
211+
extern struct file *get_mm_exe_file(struct mm_struct *mm);
212+
extern void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm);
213+
209214
#else
210215

211216
#define proc_root_driver NULL
@@ -255,6 +260,19 @@ static inline void pid_ns_release_proc(struct pid_namespace *ns)
255260
{
256261
}
257262

263+
static inline void set_mm_exe_file(struct mm_struct *mm,
264+
struct file *new_exe_file)
265+
{}
266+
267+
static inline struct file *get_mm_exe_file(struct mm_struct *mm)
268+
{
269+
return NULL;
270+
}
271+
272+
static inline void dup_mm_exe_file(struct mm_struct *oldmm,
273+
struct mm_struct *newmm)
274+
{}
275+
258276
#endif /* CONFIG_PROC_FS */
259277

260278
#if !defined(CONFIG_PROC_KCORE)

‎kernel/fork.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ void mmput(struct mm_struct *mm)
431431
if (atomic_dec_and_test(&mm->mm_users)) {
432432
exit_aio(mm);
433433
exit_mmap(mm);
434+
set_mm_exe_file(mm, NULL);
434435
if (!list_empty(&mm->mmlist)) {
435436
spin_lock(&mmlist_lock);
436437
list_del(&mm->mmlist);
@@ -543,6 +544,8 @@ struct mm_struct *dup_mm(struct task_struct *tsk)
543544
if (init_new_context(tsk, mm))
544545
goto fail_nocontext;
545546

547+
dup_mm_exe_file(oldmm, mm);
548+
546549
err = dup_mmap(mm, oldmm);
547550
if (err)
548551
goto free_pt;

0 commit comments

Comments
 (0)