Received: by 10.223.164.221 with SMTP id h29csp1709304wrb; Wed, 1 Nov 2017 22:54:50 -0700 (PDT) X-Google-Smtp-Source: ABhQp+Rs8CT6STZ4Zy+1egzQtroVYu5GELNKX9LyI46fxvtYqkQc88RlCD4rJ0xMkz399Myhw5T+ X-Received: by 10.84.149.102 with SMTP id b35mr2056407plh.331.1509602090829; Wed, 01 Nov 2017 22:54:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509602090; cv=none; d=google.com; s=arc-20160816; b=PKYBNolgaC6WrtEGFDN1wGXsntOOvZrLFyqnrFB/2dbfCzS7z/1B346gcfWaADWdT1 wUfqCQdzLLf/YV3Vl4Faag62RcG/pbAU46AL1Gm1C+uKmD+hHy1NrAj7KzlLNEWNT6BY DToroQLL7XXMn3CXbt9ZKuWWonEFAH9BiqgKc2Bo2FSphcBJEAX6glzpwQFoxx7FFk3O Tk6ldjgXlTFYv5NjjnteTBQRWvv7UENnBxp7NMGXiVDRvDbM/GnxutWVJCMYutJAYKKc 1sWvxWLgnAlxAsuOG8sHEyj3B7qpjmZCCym26c12NcL8teOBMoz6cPhE1SqHUzXCdlGn Z80Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=UenUf9y2jztM320cbfV0vhGoN17LNZw5s0W2WPeFL+0=; b=mMGVUUb/0njmxdgkmw3i6OTk1uaWXLY+ur/a38Z6sp/4CEvqt/olHaQkf0qsAJpUSJ vUM7RN9UTfY3fcMa5hFrXBYkbT6CwhcTp+lJ31Fp/7ckI0WUE90hEJzDczl3ImYhOBum ImsM7Eon7NKcHro7F9jsbrQCANgK1ZGy5syZmXU7zghB5fvZn3pmt5mt1XCdqcAdAZDY C3oYKQ7AF2LF7n+oe3as55Cts2a6U8sqDKR83m+nO09ZpMvbFM6J5+fX2SjNtSxyfQU6 YrbBNrlyeun3bal+FK00gjDRtT4mu4eP+KPqXcdzVW7sUG6Z7u/9IhXAtZmCtlBwOIIi skSw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t23si2745383pgo.645.2017.11.01.22.54.36; Wed, 01 Nov 2017 22:54:50 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751460AbdKBFyB (ORCPT + 99 others); Thu, 2 Nov 2017 01:54:01 -0400 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:60412 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750836AbdKBFx7 (ORCPT ); Thu, 2 Nov 2017 01:53:59 -0400 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id vA25rtZh064612 for ; Thu, 2 Nov 2017 01:53:59 -0400 Received: from e06smtp13.uk.ibm.com (e06smtp13.uk.ibm.com [195.75.94.109]) by mx0b-001b2d01.pphosted.com with ESMTP id 2dyq923aka-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Thu, 02 Nov 2017 01:53:58 -0400 Received: from localhost by e06smtp13.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 2 Nov 2017 05:53:56 -0000 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp13.uk.ibm.com (192.168.101.143) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 2 Nov 2017 05:53:52 -0000 Received: from d06av22.portsmouth.uk.ibm.com (d06av22.portsmouth.uk.ibm.com [9.149.105.58]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id vA25rpEH14090382; Thu, 2 Nov 2017 05:53:51 GMT Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 217DA4C1D8; Thu, 2 Nov 2017 05:49:20 +0000 (GMT) Received: from d06av22.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 484644C1D7; Thu, 2 Nov 2017 05:49:18 +0000 (GMT) Received: from rapoport-lnx (unknown [9.148.8.198]) by d06av22.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Thu, 2 Nov 2017 05:49:18 +0000 (GMT) Received: by rapoport-lnx (sSMTP sendmail emulation); Thu, 02 Nov 2017 07:53:49 +0200 From: Mike Rapoport To: Andrew Morton Cc: Ingo Molnar , Peter Zijlstra , Thomas Gleixner , Darren Hart , Oleg Nesterov , Balbir Singh , Tony Luck , Kees Cook , linux-mm , lkml , Mike Rapoport Subject: [PATCH v3] pids: introduce find_get_task_by_vpid helper Date: Thu, 2 Nov 2017 07:53:47 +0200 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 x-cbid: 17110205-0012-0000-0000-000005889AE1 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17110205-0013-0000-0000-000019032C89 Message-Id: <1509602027-11337-1-git-send-email-rppt@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-11-02_01:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1707230000 definitions=main-1711020079 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org There are several functions that do find_task_by_vpid() followed by get_task_struct(). We can use a helper function instead. Signed-off-by: Mike Rapoport Acked-by: Oleg Nesterov --- v3: update arch/ia64/kernel/perfmon.c and security/yama/yama_lsm.c that were missing in the previous versions v2: remove futex_find_get_task() and ptrace_get_task_struct() as Oleg suggested arch/ia64/kernel/perfmon.c | 13 +++---------- include/linux/sched.h | 5 +++++ kernel/futex.c | 20 +------------------- kernel/pid.c | 13 +++++++++++++ kernel/ptrace.c | 27 ++++++--------------------- kernel/taskstats.c | 6 +----- mm/process_vm_access.c | 6 +----- security/yama/yama_lsm.c | 11 +++-------- 8 files changed, 33 insertions(+), 68 deletions(-) diff --git a/arch/ia64/kernel/perfmon.c b/arch/ia64/kernel/perfmon.c index 09f86ebfcc7b..496b3f87ffab 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) if (pid < 2) return -EPERM; if (pid != task_pid_vnr(current)) { - - read_lock(&tasklist_lock); - - p = find_task_by_vpid(pid); - /* make sure task cannot go away while we operate on it */ - if (p) get_task_struct(p); - - read_unlock(&tasklist_lock); - - if (p == NULL) return -ESRCH; + p = find_get_task_by_vpid(pid); + if (!p) + return -ESRCH; } ret = pfm_task_incompatible(ctx, p); diff --git a/include/linux/sched.h b/include/linux/sched.h index 26a7df4e558c..4c3af5255fcf 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -1484,6 +1484,11 @@ static inline struct thread_info *task_thread_info(struct task_struct *task) extern struct task_struct *find_task_by_vpid(pid_t nr); extern struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns); +/* + * find a task by its virtual pid and get the task struct + */ +extern struct task_struct *find_get_task_by_vpid(pid_t nr); + extern int wake_up_state(struct task_struct *tsk, unsigned int state); extern int wake_up_process(struct task_struct *tsk); extern void wake_up_new_task(struct task_struct *tsk); diff --git a/kernel/futex.c b/kernel/futex.c index 0518a0bfc746..e2a160549a0c 100644 --- a/kernel/futex.c +++ b/kernel/futex.c @@ -862,24 +862,6 @@ static void put_pi_state(struct futex_pi_state *pi_state) } } -/* - * Look up the task based on what TID userspace gave us. - * We dont trust it. - */ -static struct task_struct *futex_find_get_task(pid_t pid) -{ - struct task_struct *p; - - rcu_read_lock(); - p = find_task_by_vpid(pid); - if (p) - get_task_struct(p); - - rcu_read_unlock(); - - return p; -} - #ifdef CONFIG_FUTEX_PI /* @@ -1166,7 +1148,7 @@ static int attach_to_pi_owner(u32 uval, union futex_key *key, */ if (!pid) return -ESRCH; - p = futex_find_get_task(pid); + p = find_get_task_by_vpid(pid); if (!p) return -ESRCH; diff --git a/kernel/pid.c b/kernel/pid.c index 020dedbdf066..ead086b0ef8e 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -462,6 +462,19 @@ struct task_struct *find_task_by_vpid(pid_t vnr) return find_task_by_pid_ns(vnr, task_active_pid_ns(current)); } +struct task_struct *find_get_task_by_vpid(pid_t nr) +{ + struct task_struct *task; + + rcu_read_lock(); + task = find_task_by_vpid(nr); + if (task) + get_task_struct(task); + rcu_read_unlock(); + + return task; +} + struct pid *get_task_pid(struct task_struct *task, enum pid_type type) { struct pid *pid; diff --git a/kernel/ptrace.c b/kernel/ptrace.c index 84b1367935e4..6f3de14313f5 100644 --- a/kernel/ptrace.c +++ b/kernel/ptrace.c @@ -1099,21 +1099,6 @@ int ptrace_request(struct task_struct *child, long request, return ret; } -static struct task_struct *ptrace_get_task_struct(pid_t pid) -{ - struct task_struct *child; - - rcu_read_lock(); - child = find_task_by_vpid(pid); - if (child) - get_task_struct(child); - rcu_read_unlock(); - - if (!child) - return ERR_PTR(-ESRCH); - return child; -} - #ifndef arch_ptrace_attach #define arch_ptrace_attach(child) do { } while (0) #endif @@ -1131,9 +1116,9 @@ SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr, goto out; } - child = ptrace_get_task_struct(pid); - if (IS_ERR(child)) { - ret = PTR_ERR(child); + child = find_get_task_by_vpid(pid); + if (!child) { + ret = -ESRCH; goto out; } @@ -1278,9 +1263,9 @@ COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid, goto out; } - child = ptrace_get_task_struct(pid); - if (IS_ERR(child)) { - ret = PTR_ERR(child); + child = find_get_task_by_vpid(pid); + if (!child) { + ret = -ESRCH; goto out; } 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) { struct task_struct *tsk; - rcu_read_lock(); - tsk = find_task_by_vpid(pid); - if (tsk) - get_task_struct(tsk); - rcu_read_unlock(); + tsk = find_get_task_by_vpid(pid); if (!tsk) return -ESRCH; 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, } /* Get process information */ - rcu_read_lock(); - task = find_task_by_vpid(pid); - if (task) - get_task_struct(task); - rcu_read_unlock(); + task = find_get_task_by_vpid(pid); if (!task) { rc = -ESRCH; 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, } else { struct task_struct *tracer; - rcu_read_lock(); - tracer = find_task_by_vpid(arg2); - if (tracer) - get_task_struct(tracer); - else + tracer = find_get_task_by_vpid(arg2); + if (!tracer) { rc = -EINVAL; - rcu_read_unlock(); - - if (tracer) { + } else { rc = yama_ptracer_add(tracer, myself); put_task_struct(tracer); } -- 2.7.4 From 1582915677951599532@xxx Thu Nov 02 01:27:07 +0000 2017 X-GM-THRID: 1582915677951599532 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread