Skip to content

Commit 3864601

Browse files
Jiri Slabytorvalds
authored andcommitted
mm: extract exe_file handling from procfs
Setup and cleanup of mm_struct->exe_file is currently done in fs/proc/. This was because exe_file was needed only for /proc/<pid>/exe. Since we will need the exe_file functionality also for core dumps (so core name can contain full binary path), built this functionality always into the kernel. To achieve that move that out of proc FS to the kernel/ where in fact it should belong. By doing that we can make dup_mm_exe_file static. Also we can drop linux/proc_fs.h inclusion in fs/exec.c and kernel/fork.c. Signed-off-by: Jiri Slaby <jslaby@suse.cz> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 63ab25e commit 3864601

6 files changed

Lines changed: 53 additions & 82 deletions

File tree

‎fs/exec.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
#include <linux/pid_namespace.h>
4343
#include <linux/module.h>
4444
#include <linux/namei.h>
45-
#include <linux/proc_fs.h>
4645
#include <linux/mount.h>
4746
#include <linux/security.h>
4847
#include <linux/syscalls.h>

‎fs/proc/base.c‎

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,57 +1576,6 @@ static const struct file_operations proc_pid_set_comm_operations = {
15761576
.release = single_release,
15771577
};
15781578

1579-
/*
1580-
* We added or removed a vma mapping the executable. The vmas are only mapped
1581-
* during exec and are not mapped with the mmap system call.
1582-
* Callers must hold down_write() on the mm's mmap_sem for these
1583-
*/
1584-
void added_exe_file_vma(struct mm_struct *mm)
1585-
{
1586-
mm->num_exe_file_vmas++;
1587-
}
1588-
1589-
void removed_exe_file_vma(struct mm_struct *mm)
1590-
{
1591-
mm->num_exe_file_vmas--;
1592-
if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1593-
fput(mm->exe_file);
1594-
mm->exe_file = NULL;
1595-
}
1596-
1597-
}
1598-
1599-
void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1600-
{
1601-
if (new_exe_file)
1602-
get_file(new_exe_file);
1603-
if (mm->exe_file)
1604-
fput(mm->exe_file);
1605-
mm->exe_file = new_exe_file;
1606-
mm->num_exe_file_vmas = 0;
1607-
}
1608-
1609-
struct file *get_mm_exe_file(struct mm_struct *mm)
1610-
{
1611-
struct file *exe_file;
1612-
1613-
/* We need mmap_sem to protect against races with removal of
1614-
* VM_EXECUTABLE vmas */
1615-
down_read(&mm->mmap_sem);
1616-
exe_file = mm->exe_file;
1617-
if (exe_file)
1618-
get_file(exe_file);
1619-
up_read(&mm->mmap_sem);
1620-
return exe_file;
1621-
}
1622-
1623-
void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1624-
{
1625-
/* It's safe to write the exe_file pointer without exe_file_lock because
1626-
* this is called during fork when the task is not yet in /proc */
1627-
newmm->exe_file = get_mm_exe_file(oldmm);
1628-
}
1629-
16301579
static int proc_exe_link(struct inode *inode, struct path *exe_path)
16311580
{
16321581
struct task_struct *task;

‎include/linux/mm.h‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,17 +1408,11 @@ extern void exit_mmap(struct mm_struct *);
14081408
extern int mm_take_all_locks(struct mm_struct *mm);
14091409
extern void mm_drop_all_locks(struct mm_struct *mm);
14101410

1411-
#ifdef CONFIG_PROC_FS
14121411
/* From fs/proc/base.c. callers must _not_ hold the mm's exe_file_lock */
14131412
extern void added_exe_file_vma(struct mm_struct *mm);
14141413
extern void removed_exe_file_vma(struct mm_struct *mm);
1415-
#else
1416-
static inline void added_exe_file_vma(struct mm_struct *mm)
1417-
{}
1418-
1419-
static inline void removed_exe_file_vma(struct mm_struct *mm)
1420-
{}
1421-
#endif /* CONFIG_PROC_FS */
1414+
extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
1415+
extern struct file *get_mm_exe_file(struct mm_struct *mm);
14221416

14231417
extern int may_expand_vm(struct mm_struct *mm, unsigned long npages);
14241418
extern int install_special_mapping(struct mm_struct *mm,

‎include/linux/mm_types.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,9 @@ struct mm_struct {
302302
struct task_struct __rcu *owner;
303303
#endif
304304

305-
#ifdef CONFIG_PROC_FS
306305
/* store ref to file /proc/<pid>/exe symlink points to */
307306
struct file *exe_file;
308307
unsigned long num_exe_file_vmas;
309-
#endif
310308
#ifdef CONFIG_MMU_NOTIFIER
311309
struct mmu_notifier_mm *mmu_notifier_mm;
312310
#endif

‎include/linux/proc_fs.h‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ extern void proc_net_remove(struct net *net, const char *name);
173173
extern struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name,
174174
struct proc_dir_entry *parent);
175175

176-
/* While the {get|set|dup}_mm_exe_file functions are for mm_structs, they are
177-
* only needed to implement /proc/<pid>|self/exe so we define them here. */
178-
extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
179-
extern struct file *get_mm_exe_file(struct mm_struct *mm);
180-
extern void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm);
181-
182176
extern struct file *proc_ns_fget(int fd);
183177

184178
#else
@@ -230,19 +224,6 @@ static inline void pid_ns_release_proc(struct pid_namespace *ns)
230224
{
231225
}
232226

233-
static inline void set_mm_exe_file(struct mm_struct *mm,
234-
struct file *new_exe_file)
235-
{}
236-
237-
static inline struct file *get_mm_exe_file(struct mm_struct *mm)
238-
{
239-
return NULL;
240-
}
241-
242-
static inline void dup_mm_exe_file(struct mm_struct *oldmm,
243-
struct mm_struct *newmm)
244-
{}
245-
246227
static inline struct file *proc_ns_fget(int fd)
247228
{
248229
return ERR_PTR(-EINVAL);

‎kernel/fork.c‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
#include <linux/taskstats_kern.h>
6060
#include <linux/random.h>
6161
#include <linux/tty.h>
62-
#include <linux/proc_fs.h>
6362
#include <linux/blkdev.h>
6463
#include <linux/fs_struct.h>
6564
#include <linux/magic.h>
@@ -597,6 +596,57 @@ void mmput(struct mm_struct *mm)
597596
}
598597
EXPORT_SYMBOL_GPL(mmput);
599598

599+
/*
600+
* We added or removed a vma mapping the executable. The vmas are only mapped
601+
* during exec and are not mapped with the mmap system call.
602+
* Callers must hold down_write() on the mm's mmap_sem for these
603+
*/
604+
void added_exe_file_vma(struct mm_struct *mm)
605+
{
606+
mm->num_exe_file_vmas++;
607+
}
608+
609+
void removed_exe_file_vma(struct mm_struct *mm)
610+
{
611+
mm->num_exe_file_vmas--;
612+
if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
613+
fput(mm->exe_file);
614+
mm->exe_file = NULL;
615+
}
616+
617+
}
618+
619+
void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
620+
{
621+
if (new_exe_file)
622+
get_file(new_exe_file);
623+
if (mm->exe_file)
624+
fput(mm->exe_file);
625+
mm->exe_file = new_exe_file;
626+
mm->num_exe_file_vmas = 0;
627+
}
628+
629+
struct file *get_mm_exe_file(struct mm_struct *mm)
630+
{
631+
struct file *exe_file;
632+
633+
/* We need mmap_sem to protect against races with removal of
634+
* VM_EXECUTABLE vmas */
635+
down_read(&mm->mmap_sem);
636+
exe_file = mm->exe_file;
637+
if (exe_file)
638+
get_file(exe_file);
639+
up_read(&mm->mmap_sem);
640+
return exe_file;
641+
}
642+
643+
static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
644+
{
645+
/* It's safe to write the exe_file pointer without exe_file_lock because
646+
* this is called during fork when the task is not yet in /proc */
647+
newmm->exe_file = get_mm_exe_file(oldmm);
648+
}
649+
600650
/**
601651
* get_task_mm - acquire a reference to the task's mm
602652
*

0 commit comments

Comments
 (0)