diff options
author | Mateusz Guzik <mguzik@redhat.com> | 2016-08-23 10:20:38 -0400 |
---|---|---|
committer | Paul Moore <paul@paul-moore.com> | 2016-08-31 16:11:20 -0400 |
commit | cd81a9170e69e018bbaba547c1fd85a585f5697a (patch) | |
tree | b0c11dc4cc55aba04e21f8c7352b8c516124cbdb | |
parent | 43761473c254b45883a64441dd0bc85a42f3645c (diff) |
mm: introduce get_task_exe_file
For more convenient access if one has a pointer to the task.
As a minor nit take advantage of the fact that only task lock + rcu are
needed to safely grab ->exe_file. This saves mm refcount dance.
Use the helper in proc_exe_link.
Signed-off-by: Mateusz Guzik <mguzik@redhat.com>
Acked-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Acked-by: Richard Guy Briggs <rgb@redhat.com>
Cc: <stable@vger.kernel.org> # 4.3.x
Signed-off-by: Paul Moore <paul@paul-moore.com>
-rw-r--r-- | fs/proc/base.c | 7 | ||||
-rw-r--r-- | include/linux/mm.h | 1 | ||||
-rw-r--r-- | kernel/fork.c | 23 |
3 files changed, 25 insertions, 6 deletions
diff --git a/fs/proc/base.c b/fs/proc/base.c index 0d163a84082d..da8b1943ba04 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c | |||
@@ -1552,18 +1552,13 @@ static const struct file_operations proc_pid_set_comm_operations = { | |||
1552 | static int proc_exe_link(struct dentry *dentry, struct path *exe_path) | 1552 | static int proc_exe_link(struct dentry *dentry, struct path *exe_path) |
1553 | { | 1553 | { |
1554 | struct task_struct *task; | 1554 | struct task_struct *task; |
1555 | struct mm_struct *mm; | ||
1556 | struct file *exe_file; | 1555 | struct file *exe_file; |
1557 | 1556 | ||
1558 | task = get_proc_task(d_inode(dentry)); | 1557 | task = get_proc_task(d_inode(dentry)); |
1559 | if (!task) | 1558 | if (!task) |
1560 | return -ENOENT; | 1559 | return -ENOENT; |
1561 | mm = get_task_mm(task); | 1560 | exe_file = get_task_exe_file(task); |
1562 | put_task_struct(task); | 1561 | put_task_struct(task); |
1563 | if (!mm) | ||
1564 | return -ENOENT; | ||
1565 | exe_file = get_mm_exe_file(mm); | ||
1566 | mmput(mm); | ||
1567 | if (exe_file) { | 1562 | if (exe_file) { |
1568 | *exe_path = exe_file->f_path; | 1563 | *exe_path = exe_file->f_path; |
1569 | path_get(&exe_file->f_path); | 1564 | path_get(&exe_file->f_path); |
diff --git a/include/linux/mm.h b/include/linux/mm.h index 8f468e0d2534..004c73a988b7 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h | |||
@@ -1987,6 +1987,7 @@ extern void mm_drop_all_locks(struct mm_struct *mm); | |||
1987 | 1987 | ||
1988 | extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file); | 1988 | extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file); |
1989 | extern struct file *get_mm_exe_file(struct mm_struct *mm); | 1989 | extern struct file *get_mm_exe_file(struct mm_struct *mm); |
1990 | extern struct file *get_task_exe_file(struct task_struct *task); | ||
1990 | 1991 | ||
1991 | extern bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long npages); | 1992 | extern bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long npages); |
1992 | extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages); | 1993 | extern void vm_stat_account(struct mm_struct *, vm_flags_t, long npages); |
diff --git a/kernel/fork.c b/kernel/fork.c index d277e83ed3e0..42451aeb245f 100644 --- a/kernel/fork.c +++ b/kernel/fork.c | |||
@@ -774,6 +774,29 @@ struct file *get_mm_exe_file(struct mm_struct *mm) | |||
774 | EXPORT_SYMBOL(get_mm_exe_file); | 774 | EXPORT_SYMBOL(get_mm_exe_file); |
775 | 775 | ||
776 | /** | 776 | /** |
777 | * get_task_exe_file - acquire a reference to the task's executable file | ||
778 | * | ||
779 | * Returns %NULL if task's mm (if any) has no associated executable file or | ||
780 | * this is a kernel thread with borrowed mm (see the comment above get_task_mm). | ||
781 | * User must release file via fput(). | ||
782 | */ | ||
783 | struct file *get_task_exe_file(struct task_struct *task) | ||
784 | { | ||
785 | struct file *exe_file = NULL; | ||
786 | struct mm_struct *mm; | ||
787 | |||
788 | task_lock(task); | ||
789 | mm = task->mm; | ||
790 | if (mm) { | ||
791 | if (!(task->flags & PF_KTHREAD)) | ||
792 | exe_file = get_mm_exe_file(mm); | ||
793 | } | ||
794 | task_unlock(task); | ||
795 | return exe_file; | ||
796 | } | ||
797 | EXPORT_SYMBOL(get_task_exe_file); | ||
798 | |||
799 | /** | ||
777 | * get_task_mm - acquire a reference to the task's mm | 800 | * get_task_mm - acquire a reference to the task's mm |
778 | * | 801 | * |
779 | * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning | 802 | * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning |