summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Rapoport <rppt@linux.vnet.ibm.com>2018-02-06 18:40:17 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2018-02-06 21:32:46 -0500
commit2ee0826085d1c0281cb60c1f4bc3e0c27efeedc3 (patch)
treebb2dc8915be7e4f0fea7438edf380d84b896f80a
parenteab216e9cc636c24cec06bdf57a80f4c26b34493 (diff)
pids: introduce find_get_task_by_vpid() helper
There are several functions that do find_task_by_vpid() followed by get_task_struct(). We can use a helper function instead. Link: http://lkml.kernel.org/r/1509602027-11337-1-git-send-email-rppt@linux.vnet.ibm.com Signed-off-by: Mike Rapoport <rppt@linux.vnet.ibm.com> Acked-by: Oleg Nesterov <oleg@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--arch/ia64/kernel/perfmon.c13
-rw-r--r--include/linux/sched.h5
-rw-r--r--kernel/futex.c20
-rw-r--r--kernel/pid.c13
-rw-r--r--kernel/ptrace.c27
-rw-r--r--kernel/taskstats.c6
-rw-r--r--mm/process_vm_access.c6
-rw-r--r--security/yama/yama_lsm.c11
8 files changed, 33 insertions, 68 deletions
diff --git a/arch/ia64/kernel/perfmon.c b/arch/ia64/kernel/perfmon.c
index c44f002e8f6b..858602494096 100644
--- a/arch/ia64/kernel/perfmon.c
+++ b/arch/ia64/kernel/perfmon.c
@@ -2610,17 +2610,10 @@ pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
2610 if (pid < 2) return -EPERM; 2610 if (pid < 2) return -EPERM;
2611 2611
2612 if (pid != task_pid_vnr(current)) { 2612 if (pid != task_pid_vnr(current)) {
2613
2614 read_lock(&tasklist_lock);
2615
2616 p = find_task_by_vpid(pid);
2617
2618 /* make sure task cannot go away while we operate on it */ 2613 /* make sure task cannot go away while we operate on it */
2619 if (p) get_task_struct(p); 2614 p = find_get_task_by_vpid(pid);
2620 2615 if (!p)
2621 read_unlock(&tasklist_lock); 2616 return -ESRCH;
2622
2623 if (p == NULL) return -ESRCH;
2624 } 2617 }
2625 2618
2626 ret = pfm_task_incompatible(ctx, p); 2619 ret = pfm_task_incompatible(ctx, p);
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 166144c04ef6..ce5a27304b03 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -1489,6 +1489,11 @@ static inline struct thread_info *task_thread_info(struct task_struct *task)
1489extern struct task_struct *find_task_by_vpid(pid_t nr); 1489extern struct task_struct *find_task_by_vpid(pid_t nr);
1490extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns); 1490extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns);
1491 1491
1492/*
1493 * find a task by its virtual pid and get the task struct
1494 */
1495extern struct task_struct *find_get_task_by_vpid(pid_t nr);
1496
1492extern int wake_up_state(struct task_struct *tsk, unsigned int state); 1497extern int wake_up_state(struct task_struct *tsk, unsigned int state);
1493extern int wake_up_process(struct task_struct *tsk); 1498extern int wake_up_process(struct task_struct *tsk);
1494extern void wake_up_new_task(struct task_struct *tsk); 1499extern void wake_up_new_task(struct task_struct *tsk);
diff --git a/kernel/futex.c b/kernel/futex.c
index 7f719d110908..1f450e092c74 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -862,24 +862,6 @@ static void put_pi_state(struct futex_pi_state *pi_state)
862 } 862 }
863} 863}
864 864
865/*
866 * Look up the task based on what TID userspace gave us.
867 * We dont trust it.
868 */
869static struct task_struct *futex_find_get_task(pid_t pid)
870{
871 struct task_struct *p;
872
873 rcu_read_lock();
874 p = find_task_by_vpid(pid);
875 if (p)
876 get_task_struct(p);
877
878 rcu_read_unlock();
879
880 return p;
881}
882
883#ifdef CONFIG_FUTEX_PI 865#ifdef CONFIG_FUTEX_PI
884 866
885/* 867/*
@@ -1183,7 +1165,7 @@ static int attach_to_pi_owner(u32 uval, union futex_key *key,
1183 */ 1165 */
1184 if (!pid) 1166 if (!pid)
1185 return -ESRCH; 1167 return -ESRCH;
1186 p = futex_find_get_task(pid); 1168 p = find_get_task_by_vpid(pid);
1187 if (!p) 1169 if (!p)
1188 return -ESRCH; 1170 return -ESRCH;
1189 1171
diff --git a/kernel/pid.c b/kernel/pid.c
index 5d30c87e3c42..ed6c343fe50d 100644
--- a/kernel/pid.c
+++ b/kernel/pid.c
@@ -343,6 +343,19 @@ struct task_struct *find_task_by_vpid(pid_t vnr)
343 return find_task_by_pid_ns(vnr, task_active_pid_ns(current)); 343 return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
344} 344}
345 345
346struct task_struct *find_get_task_by_vpid(pid_t nr)
347{
348 struct task_struct *task;
349
350 rcu_read_lock();
351 task = find_task_by_vpid(nr);
352 if (task)
353 get_task_struct(task);
354 rcu_read_unlock();
355
356 return task;
357}
358
346struct pid *get_task_pid(struct task_struct *task, enum pid_type type) 359struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
347{ 360{
348 struct pid *pid; 361 struct pid *pid;
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index 5e1d713c8e61..21fec73d45d4 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -1103,21 +1103,6 @@ int ptrace_request(struct task_struct *child, long request,
1103 return ret; 1103 return ret;
1104} 1104}
1105 1105
1106static struct task_struct *ptrace_get_task_struct(pid_t pid)
1107{
1108 struct task_struct *child;
1109
1110 rcu_read_lock();
1111 child = find_task_by_vpid(pid);
1112 if (child)
1113 get_task_struct(child);
1114 rcu_read_unlock();
1115
1116 if (!child)
1117 return ERR_PTR(-ESRCH);
1118 return child;
1119}
1120
1121#ifndef arch_ptrace_attach 1106#ifndef arch_ptrace_attach
1122#define arch_ptrace_attach(child) do { } while (0) 1107#define arch_ptrace_attach(child) do { } while (0)
1123#endif 1108#endif
@@ -1135,9 +1120,9 @@ SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
1135 goto out; 1120 goto out;
1136 } 1121 }
1137 1122
1138 child = ptrace_get_task_struct(pid); 1123 child = find_get_task_by_vpid(pid);
1139 if (IS_ERR(child)) { 1124 if (!child) {
1140 ret = PTR_ERR(child); 1125 ret = -ESRCH;
1141 goto out; 1126 goto out;
1142 } 1127 }
1143 1128
@@ -1281,9 +1266,9 @@ COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
1281 goto out; 1266 goto out;
1282 } 1267 }
1283 1268
1284 child = ptrace_get_task_struct(pid); 1269 child = find_get_task_by_vpid(pid);
1285 if (IS_ERR(child)) { 1270 if (!child) {
1286 ret = PTR_ERR(child); 1271 ret = -ESRCH;
1287 goto out; 1272 goto out;
1288 } 1273 }
1289 1274
diff --git a/kernel/taskstats.c b/kernel/taskstats.c
index 4559e914452b..4e62a4a8fa91 100644
--- a/kernel/taskstats.c
+++ b/kernel/taskstats.c
@@ -194,11 +194,7 @@ static int fill_stats_for_pid(pid_t pid, struct taskstats *stats)
194{ 194{
195 struct task_struct *tsk; 195 struct task_struct *tsk;
196 196
197 rcu_read_lock(); 197 tsk = find_get_task_by_vpid(pid);
198 tsk = find_task_by_vpid(pid);
199 if (tsk)
200 get_task_struct(tsk);
201 rcu_read_unlock();
202 if (!tsk) 198 if (!tsk)
203 return -ESRCH; 199 return -ESRCH;
204 fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats); 200 fill_stats(current_user_ns(), task_active_pid_ns(current), tsk, stats);
diff --git a/mm/process_vm_access.c b/mm/process_vm_access.c
index 8973cd231ece..16424b9ae424 100644
--- a/mm/process_vm_access.c
+++ b/mm/process_vm_access.c
@@ -197,11 +197,7 @@ static ssize_t process_vm_rw_core(pid_t pid, struct iov_iter *iter,
197 } 197 }
198 198
199 /* Get process information */ 199 /* Get process information */
200 rcu_read_lock(); 200 task = find_get_task_by_vpid(pid);
201 task = find_task_by_vpid(pid);
202 if (task)
203 get_task_struct(task);
204 rcu_read_unlock();
205 if (!task) { 201 if (!task) {
206 rc = -ESRCH; 202 rc = -ESRCH;
207 goto free_proc_pages; 203 goto free_proc_pages;
diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
index 8298e094f4f7..ffda91a4a1aa 100644
--- a/security/yama/yama_lsm.c
+++ b/security/yama/yama_lsm.c
@@ -250,15 +250,10 @@ int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
250 } else { 250 } else {
251 struct task_struct *tracer; 251 struct task_struct *tracer;
252 252
253 rcu_read_lock(); 253 tracer = find_get_task_by_vpid(arg2);
254 tracer = find_task_by_vpid(arg2); 254 if (!tracer) {
255 if (tracer)
256 get_task_struct(tracer);
257 else
258 rc = -EINVAL; 255 rc = -EINVAL;
259 rcu_read_unlock(); 256 } else {
260
261 if (tracer) {
262 rc = yama_ptracer_add(tracer, myself); 257 rc = yama_ptracer_add(tracer, myself);
263 put_task_struct(tracer); 258 put_task_struct(tracer);
264 } 259 }