From patchwork Mon Sep 30 09:22:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13815709 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14DBC1885A6; Mon, 30 Sep 2024 09:22:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688179; cv=fail; b=Yeu156f0olamBBkzx9Ja3DxABf4UeOb9YFjldFMDBY8+qlPXaTl9vq0/e2g/9dVBKIGg6M0Pu+4iKK7oM+oG7s721Y7PyfKB3KDnW45KyTruh7LlZ1zdtPwN1ccgpMhTzP1TJzADEVqT29lXE/xtjDdNe/KT4U73YrBgbCKezDo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688179; c=relaxed/simple; bh=ltbOeDMfe1OWnc1jc+IhBX+gUlA9XLkrovG0G6EkBNM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Jcdsp4DMUuZH+WWdXO4copQiA5whDb6vDoqUO2QcfCMnj46ZWS/JluNa/bSQAwKO5h4iwF5NlpiXPGdK7cfnLNxEb51z+J5+y0csJjzzNzAlWiZO2owHHrjmCpJqilela3JT9IOq7CdAZicZhtZXLqSV18o7E56sDTqYDD7EwL0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=onxbaVbR; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=l8tPOHLl; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="onxbaVbR"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="l8tPOHLl" Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48U2thcU009535; Mon, 30 Sep 2024 09:22:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :content-transfer-encoding:content-type:mime-version; s= corp-2023-11-20; bh=4el6H1mAR3r73/0GbUniY3qR2QbvpTqKt5q05n7V0bA=; b= onxbaVbRxMIRzYSnjIh/YKoV+R/EzICK95kTEA54NbcDq0Oeb/CkyCWANoG4ygsE GGWR0zvaiw3UTG1xAIB6ywE4s1DdNgtoMTbn7SBUEJmIWYxreM9IJzoWuF77k8y2 GUbITgTQIEYYcj3hJv9HOhmWKgi7wOyvXKaAOxiEa7SOh/dx69iGgiZ7ak08wxo8 d5s7VojZQyi4xFyhSiXd6z0ae6/PiYpAbHDHLLX1ddE4hHd5KjkzFy/Q/5pyicup jhsbwb4cE1K/PqU4BH7Zja0ovpHnJeo+itqxjpbRS861EAEG/rCf9afWjG2hNwQY +fA5R9+T2WkPe4+McfR61w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41x8k32ut2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Sep 2024 09:22:46 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 48U8XHtN017481; Mon, 30 Sep 2024 09:22:45 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2049.outbound.protection.outlook.com [104.47.55.49]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 41x885stpw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Sep 2024 09:22:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Nl/Sx2m/9FxUrZ3fsBPQPpVJTB3Z9Ufdk/wYNOMtbQp2BShatbOLIKNcbV2YuUdCxrs/mlYAZWacHP+0M0UXUfBkioQKuX7yGfVBMhzPF3lPyNFJnt3QOhts7JGooSfM8xe8L5FIunMvd5v6DNaK9s9gXEDlzVbM3lkK1vuSrxaXFF8w8n1MCuAq8tv1H9IS56huHQrO+5Jd/pPTUVX2w+KJ/JRXDi3eJ8ubJY20j85bjusUJDLqjAffeDfe+wns5jO3xRvUKTpUZD/mbZqyS9e+jVsvnNoxgEK9w4Smq3SU/FIbkStNcKCY6xMPAQIO3kqFGM5xK7GEf4kWf4h3GA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4el6H1mAR3r73/0GbUniY3qR2QbvpTqKt5q05n7V0bA=; b=YXSgWxaHNHfkzYqfm9kFckuxSja+2RZs6Kyj6cyR4CfaralxHWfZ+iIjrjFxcGwYf0UI41N1IuO7Sqmy071OXic93IhimoE3xUiYPnLJmWiZ0J1v2i7tTwKwaht+BGXrj76vtBj27/SUJ1vlfR0N5VlKMn5wJkomqbDmuE7khcQIV5vUk/L8RJ5UNyZww/pjuuQD47BBnkDk0MsXk+Pf20OIFgbTzNNrF4AD8bgh6CW2PIh2GYQ6PR7zzOcEKL3+2yVyG/3iiytafEHszXYcXNZdlN/B/cv08ktKSlFIIDB9EMw1NFVC1/cpb0/Pr67vkRyEboanzko0mxevD3UhZw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4el6H1mAR3r73/0GbUniY3qR2QbvpTqKt5q05n7V0bA=; b=l8tPOHLllgH0u7DzqFS47Xu72ybZs9HIWAj/EpJ18a1Mqc3h5Iv4Mt0JfJVPAM84mPho2j3Nsx60euvPqE/jOVVfkZq/eiFTHQmjEd+akhUVuu05b8kP4jq8ZUlHxzxO42sIDlrAi3aYu6W80FxduRQnqt0RkfVORBWdTyrl3eA= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by SA6PR10MB8134.namprd10.prod.outlook.com (2603:10b6:806:443::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.15; Mon, 30 Sep 2024 09:22:43 +0000 Received: from SJ0PR10MB5613.namprd10.prod.outlook.com ([fe80::4239:cf6f:9caa:940e]) by SJ0PR10MB5613.namprd10.prod.outlook.com ([fe80::4239:cf6f:9caa:940e%5]) with mapi id 15.20.8026.014; Mon, 30 Sep 2024 09:22:43 +0000 From: Lorenzo Stoakes To: Christian Brauner Cc: Shuah Khan , "Liam R . Howlett" , Suren Baghdasaryan , Vlastimil Babka , pedro.falcato@gmail.com, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/3] pidfd: refactor pidfd_get_pid/to_pid() and de-duplicate pid lookup Date: Mon, 30 Sep 2024 10:22:28 +0100 Message-ID: <32bb395f982cd7017af6251d27eae53c31b58acb.1727644404.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0321.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a4::21) To SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR10MB5613:EE_|SA6PR10MB8134:EE_ X-MS-Office365-Filtering-Correlation-Id: ce481eee-740b-44e8-0693-08dce1317022 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: SweSOk8n+QD3n4uFy612TuRFDhZprteZ0LnJprmXmMxRP+5jP4UuFWnd4BopM4m50jLK/kGJre0Qf+7KBfeIANgGtAOotGdRxUgOjA3ITf3HwwFZJFqURIalJnFDizmcSVVvzYV2/uGma/0V41XEPQQF1bE20ANy3eWvF8b2QGo61M/J41MpKnDuukfxbLyFJveunVvSGKChy8J+ssA8yrbcR+FcpP9cE84yqdcIB/QUVKzGtbxC4Z3PXUI/bN8QPLMLzYLD0cvGdVrNHVkQvdJ4cY5ZAm9mt5r37uRws0BAjjIBy8Mjd+9Djmu3SMiEB+81aHf/LJrmWTB/Df9dUQ/xmalBERarFAbO/CZWrjxCnBNGfb9qqj1l1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613.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gCJ+dqhYyCVysPH8MTHPDdSj59QdgshtQoclQLdKUhVFIwukTScAaf2SkR8YHenaFE5u0HXzt5DGWvScsS4UlU7VRR45cBuiye6lbPOSVSEUPfzGycnma0siCp6WE6Z5QOYFuvXwPmwUrU6Cta6PUSD1PbYUTZlwTNQgCIL4Z+/Nf8MSPSr2M3wi1w/wFAx9KtJns3z2vDLDefcblYPwt63miIMrnlXeaeoB7vwhsxplx33AI3BkjBhbS6xts44GRHV05Be9lL7sMoeGZMjrwuR3+Fw2NbiCSbSGQVfXuVqyIhckFhWLF5LouBSHoMgFA5+a7qOIKowGMBxa5Sh15tSJfqLA+jF3smxXJVz7s5mrSgMproon4CSBSgrH8fAwebFUzS+iDmurxWcf3BJC32zEpNmrWLh+/5NC/wAU8J+mMr2AC311YK0Zi52oBkdx9kHwBiERwenYr5rFMKLTt/H8urJW6mUMgWkRPlVaZW0cC8jwhTU4YCKZ8kaVe9hm9M6iFV6I9WDf174+jmq3VgUbPg6tNvm7TrYv6QND+UWjE82eC1FP2R82YzwOUJOTzcSVT1fMp/ABbTJPAnHL4Lbi9Q3MlM4GoiXgiG2nmuge7FUb7ReOHZ/op0rnAsie2x+YgLDD4QSUIqIRksVvPoZANY9JlJf8ZW1fL3XnYer/XqqGjEDHXvS5eci6/P/t8Q5CGefCPmEBCyugNA7AySqd3pHj7nGX7BeXzvqT0rSiCPrqUc4tqpXc5JZ2pVKkpywakFwrzpg+l6gbA7p/ezG+anUnV8ILKx0BLxidYlzrMG88IHqQ4m4O+QmVd5amkyqWtyuVBSdpRW8XLN+ZYYnoQa4mnLuTvA2db6ZU7ikmAka+EIqumGQ4RQqDYn7Ift6Pn3xPlGYvlReZvVG5k1GFBdezkNGKdbCrCu05DR94T/Sgy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3h4sbmwLfChyw70gbiGzXnFpbQzYupdOOLMprI1Y+J3IIjh+EeTPfEoWS2Gy74Ke1zAnOAcMKtOlSUMqfGde0wrrCRmmWsPlB048S0DpujiMMySm2ZwjlyiMuO4khuIWEz3wWMwEYbOI2a8OwhrJt7x9mLH7M39eTUcS75qbqukpgzCoS3TWSSNsaGuM0MNf2Eh+2qh+RTJzDz3PN+QrpJnqiyRstJf85Rs/pwTKCOkd37XbbyHVkAedNo9V8WEEuR5pi6zES+qAfqmhFMNyEG+8nFk6RqhEFoo9xg= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce481eee-740b-44e8-0693-08dce1317022 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Sep 2024 09:22:43.2859 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: yLuNhIwOYk8Yrgki6RitzlQpYFHqiNcsD9TRewxDT2eV5vgGOn/ul5Rwmzt+sus1wQXCti3rPVDT5yZuqO8/rvp4SaJH9V6dqSbsErrnSis= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8134 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-30_08,2024-09-27_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 spamscore=0 suspectscore=0 bulkscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2408220000 definitions=main-2409300067 X-Proofpoint-GUID: AEJHMyBTC0vEllORlS1n2JE3dqa0axBK X-Proofpoint-ORIG-GUID: AEJHMyBTC0vEllORlS1n2JE3dqa0axBK The means by which a pid is determined from a pidfd is duplicated, some callers holding a reference to the (pid)fd, and others explicitly pinning the pid. Introduce __pidfd_get_pid() which abstracts both approaches and provide optional output parameters for file->f_flags and the fd (the latter of which, if provided, prevents the function from decrementing the fd's reference count). We add a wrapper function pidfd_get_pid() which performs the same functionality as the original, only deferring to __pidfd_get_pid() for the heavy lifting. Additionally, abstract the ability to open a pidfd by opening a /proc/ directory (used by the pidfd_send_signal() system call), providing a pidfd_get_pid_proc() wrapper function to do so. Doing this allows us to eliminate open-coded pidfd pid lookup and to consistently handle this in one place. This lays the groundwork for a subsequent patch which adds a new sentinel pidfd to explicitly reference the current process (i.e. thread group leader) without the need for a pidfd. Signed-off-by: Lorenzo Stoakes --- include/linux/pid.h | 42 +++++++++++++++++++++++++++++++++- kernel/pid.c | 55 +++++++++++++++++++++++++++++++-------------- kernel/signal.c | 26 +++++---------------- 3 files changed, 84 insertions(+), 39 deletions(-) diff --git a/include/linux/pid.h b/include/linux/pid.h index a3aad9b4074c..68b02eab7509 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -2,6 +2,7 @@ #ifndef _LINUX_PID_H #define _LINUX_PID_H +#include #include #include #include @@ -72,8 +73,47 @@ extern struct pid init_struct_pid; struct file; + +/** + * __pidfd_get_pid() - Retrieve a pid associated with the specified pidfd. + * + * @pidfd: The pidfd whose pid we want, or the fd of a /proc/ file if + * @alloc_proc is also set. + * @pin_pid: If set, then the reference counter of the returned pid is + * incremented. If not set, then @fd should be provided to pin the + * pidfd. + * @allow_proc: If set, then an fd of a /proc/ file can be passed instead + * of a pidfd, and this will be used to determine the pid. + * @flags: Output variable, if non-NULL, then the file->f_flags of the + * pidfd will be set here. + * @fd: Output variable, if non-NULL, then the pidfd reference will + * remain elevated and the caller will need to decrement it + * themselves. + * + * Returns: If successful, the pid associated with the pidfd, otherwise an + * error. + */ +struct pid *__pidfd_get_pid(unsigned int pidfd, bool pin_pid, + bool allow_proc, unsigned int *flags, + struct fd *fd); + +static inline struct pid *pidfd_get_pid(unsigned int pidfd, unsigned int *flags) +{ + return __pidfd_get_pid(pidfd, /* pin_pid = */ true, + /* allow_proc = */ false, + flags, /* fd = */ NULL); +} + +static inline struct pid *pidfd_to_pid_proc(unsigned int pidfd, + unsigned int *flags, + struct fd *fd) +{ + return __pidfd_get_pid(pidfd, /* pin_pid = */ false, + /* allow_proc = */ true, + flags, fd); +} + struct pid *pidfd_pid(const struct file *file); -struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags); struct task_struct *pidfd_get_task(int pidfd, unsigned int *flags); int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret); void do_notify_pidfd(struct task_struct *task); diff --git a/kernel/pid.c b/kernel/pid.c index 2715afb77eab..26e2581210c4 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -534,22 +535,47 @@ struct pid *find_ge_pid(int nr, struct pid_namespace *ns) } EXPORT_SYMBOL_GPL(find_ge_pid); -struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) +struct pid *__pidfd_get_pid(unsigned int pidfd, bool pin_pid, + bool allow_proc, unsigned int *flags, + struct fd *fd) { struct fd f; struct pid *pid; + struct file *file; f = fdget(fd); - if (!fd_file(f)) + file = fd_file(f); + if (!file) return ERR_PTR(-EBADF); - pid = pidfd_pid(fd_file(f)); - if (!IS_ERR(pid)) { - get_pid(pid); - *flags = fd_file(f)->f_flags; + pid = pidfd_pid(file); + /* If we allow opening a pidfd via /proc/, do so. */ + if (IS_ERR(pid) && allow_proc) + pid = tgid_pidfd_to_pid(file); + + if (IS_ERR(pid)) { + fdput(f); + return pid; } - fdput(f); + if (pin_pid) + get_pid(pid); + else + WARN_ON_ONCE(!fd); /* Nothing to keep pid/pidfd around? */ + + if (flags) + *flags = file->f_flags; + + /* + * If the user provides an fd output then it will handle decrementing + * its reference counter. + */ + if (fd) + *fd = f; + else + /* Otherwise we release it. */ + fdput(f); + return pid; } @@ -747,23 +773,18 @@ SYSCALL_DEFINE3(pidfd_getfd, int, pidfd, int, fd, unsigned int, flags) { struct pid *pid; - struct fd f; int ret; /* flags is currently unused - make sure it's unset */ if (flags) return -EINVAL; - f = fdget(pidfd); - if (!fd_file(f)) - return -EBADF; - - pid = pidfd_pid(fd_file(f)); + pid = pidfd_get_pid(pidfd, NULL); if (IS_ERR(pid)) - ret = PTR_ERR(pid); - else - ret = pidfd_getfd(pid, fd); + return PTR_ERR(pid); - fdput(f); + ret = pidfd_getfd(pid, fd); + + put_pid(pid); return ret; } diff --git a/kernel/signal.c b/kernel/signal.c index 6e57036f947f..abbb1931deba 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -3894,17 +3894,6 @@ static int copy_siginfo_from_user_any(kernel_siginfo_t *kinfo, return copy_siginfo_from_user(kinfo, info); } -static struct pid *pidfd_to_pid(const struct file *file) -{ - struct pid *pid; - - pid = pidfd_pid(file); - if (!IS_ERR(pid)) - return pid; - - return tgid_pidfd_to_pid(file); -} - #define PIDFD_SEND_SIGNAL_FLAGS \ (PIDFD_SIGNAL_THREAD | PIDFD_SIGNAL_THREAD_GROUP | \ PIDFD_SIGNAL_PROCESS_GROUP) @@ -3931,6 +3920,7 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, struct pid *pid; kernel_siginfo_t kinfo; enum pid_type type; + unsigned int f_flags; /* Enforce flags be set to 0 until we add an extension. */ if (flags & ~PIDFD_SEND_SIGNAL_FLAGS) @@ -3940,16 +3930,10 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, if (hweight32(flags & PIDFD_SEND_SIGNAL_FLAGS) > 1) return -EINVAL; - f = fdget(pidfd); - if (!fd_file(f)) - return -EBADF; - /* Is this a pidfd? */ - pid = pidfd_to_pid(fd_file(f)); - if (IS_ERR(pid)) { - ret = PTR_ERR(pid); - goto err; - } + pid = pidfd_to_pid_proc(pidfd, &f_flags, &f); + if (IS_ERR(pid)) + return PTR_ERR(pid); ret = -EINVAL; if (!access_pidfd_pidns(pid)) @@ -3958,7 +3942,7 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, switch (flags) { case 0: /* Infer scope from the type of pidfd. */ - if (fd_file(f)->f_flags & PIDFD_THREAD) + if (f_flags & PIDFD_THREAD) type = PIDTYPE_PID; else type = PIDTYPE_TGID; From patchwork Mon Sep 30 09:22:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13815710 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0465D188739; Mon, 30 Sep 2024 09:23:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688184; cv=fail; b=gGwruJ5dAK7KJD/6d/ZCqMlhZ5erFgsjKSlJjfHXmt3Uhq2hnoGKOoXDNxAixG0FIiTpq7+WXksyZ+sgQi59M4rGJBFivhVpUN9QVdWo6162gj1Oe3PqXutTpcrFusyNSQfj50q0duK+pCvJkT23cXIGBAXnubFmdyf+Sffena4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688184; c=relaxed/simple; bh=7fwbGByVMqnoEagp2Br5ei9A5v0aAERUCSVcJthsAZk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=XAw4gRmAdSEQqME9taQL+haA1fIq5BsTDlSEe5K9Ffbc8P3qMqWxQgezg6MWJlKLZIYD1VgH2jsiGDs06+oJyCKJRUvlaqDBKJYY6kpfD+lK2u083/lcWeseI0Xtdpdw5i8djvtIusOpFYZGFI3Tm2OZapUb2PxFLetVPdp5QNE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=REb04Jve; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=SHExCieI; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="REb04Jve"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="SHExCieI" Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48U2uIuQ007117; Mon, 30 Sep 2024 09:22:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :content-transfer-encoding:content-type:mime-version; s= corp-2023-11-20; bh=LxIwM5Kze2ba8NdY6DiW5Q+trqcF92YGIvuODcHkpVQ=; b= REb04JveKwlwnsChJNlJgp/MufFtd7/+ki1BOKHh3dUK11MQUwQ5VO268zGT5IoM uvuShKwKD9kUq6kP3efwghWpSiyusoykn9Ijf4O3ImlNUmBR7UsgwO7PxRbTPVR9 ptW+fAVj5PvhFGXLeosdQ1clr6Zf0Ul9h8ARwiKrtuw12+pP25WUIZTsNBfgoccY udy87k8XIKzjRN8xOl2tTsR+ASGFlub3ewMdxHi9LB9Y8NKQNPus0EjZU7oqcv4/ DXYi1BW3PRt/9JGdp5EdYeYd9/xQwln+E4+p37PrYuwTFYFDoW5T8WK3pJkbcBD9 x6dPvZ9Svg/cJpfGBjrKoQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41x9p9jrgu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Sep 2024 09:22:50 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 48U8HwFu012532; Mon, 30 Sep 2024 09:22:49 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2045.outbound.protection.outlook.com [104.47.55.45]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 41x885hj7s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Sep 2024 09:22:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=aM/raC7iCI4P1nz+v0ATq2uExqy9de0U332LxZfjbgwtXTZtwJMSmzeeGwOSfdoLC6Jw9+EkQzitPYYhXshwq1/gVJAK6iJ8TN12u+qiQKpz7muGmhH6bcPKnFeqx4u1lVJLARJcxyKDCY9GQd+lvBm1LwBv2g6fOACPde+Fy28O4eQ7vz6W9JTS0lAXSVYWgtIcIbt8I+pl+jQ1/UdLDn+d4OTIt+6u//wPTvF88ScCp9srQQ9rYTewhgIuJ6/TN0/3bpQYH9K/smWNxeuoR+MAQ0OMjBqfpDP0cCpmDSrR1MrKFLeTSiLF+O5AE7IkWd4aSaCRXGN5+6WmfMrpHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=LxIwM5Kze2ba8NdY6DiW5Q+trqcF92YGIvuODcHkpVQ=; b=e6C+ZrqkZM5XpF1pGyuXwfhRUb0tHQXgXMQIjIvBSwn/Xa8766aTZXQ1I0Q04Tmnqyf4P40hUK9vUDC9YA82Xa+rfqmIvaitJuw7MNe5B51HN7V6HF66ugpkfG4R9Yo/mAmeaEAb+9iz1Bf/6pmtEY9kil0CRBBIBq7o9FTWJf/RvqvAVvGajiRryy8DZgJix5deVtDPj5yPcT3721qpvoVEAkZOgjA6568ATC2PyqLb6eMV6wExOToV+GfUdgDoZ6cDaenB4Iy0n3UQhBbDovyLZXBlQD5a+6AhwgKMYLK7ECT6XpjRRBWdQLcVz2o25zbG7AthBXOESZIt8IS33A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=LxIwM5Kze2ba8NdY6DiW5Q+trqcF92YGIvuODcHkpVQ=; b=SHExCieI+ZYEpupbXCnZ1zG8KQlMiZ2PY50x2dZD98L+pnUgDpef9U4N/rcAJ7ayP2Bu2kySx4psb0p0S3XYZoQIcQy87sne3Yu9gIB2TdfpE3wBeIix17ybBGBdQn9tbG6CDtC5meOR5sxhmb0y9QaPK9q9fBhl5eTaVHVLlRo= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by SA6PR10MB8134.namprd10.prod.outlook.com (2603:10b6:806:443::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.15; Mon, 30 Sep 2024 09:22:46 +0000 Received: from SJ0PR10MB5613.namprd10.prod.outlook.com ([fe80::4239:cf6f:9caa:940e]) by SJ0PR10MB5613.namprd10.prod.outlook.com ([fe80::4239:cf6f:9caa:940e%5]) with mapi id 15.20.8026.014; Mon, 30 Sep 2024 09:22:46 +0000 From: Lorenzo Stoakes To: Christian Brauner Cc: Shuah Khan , "Liam R . Howlett" , Suren Baghdasaryan , Vlastimil Babka , pedro.falcato@gmail.com, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/3] pidfd: add PIDFD_SELF sentinel to refer to own process Date: Mon, 30 Sep 2024 10:22:29 +0100 Message-ID: <94c6ecc5c659b54f62fd7930dd9026a7ebf8e53b.1727644404.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0544.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:319::9) To SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR10MB5613:EE_|SA6PR10MB8134:EE_ X-MS-Office365-Filtering-Correlation-Id: a0a5d1a1-dac3-45c7-ccb1-08dce131723f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: thuhJbQrUhdUPvtpypibyeQHQ+4hbkKBASu28CWcfeYuiVSeKm44lvrbrdanBlEiWZCV3fc38i2dDcg/7AKFYKep54nJyWppnwFvLwC8fgJAtKRPQUMfgV9IZ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mxOlRSpakopBiOCFoML6TrBOq/HLChroHxrr3adaNl58/UqJvSABGFvDgtJ/En4dOiv2NmHPEPOeq5f4S/MhjULJ2V9VJzIi3kdfa60WLIpQgPibjdnhOG/VBiLCxMjKEn0I+VZfbURayg05tLkYkXg0i/ri1NfFA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR10MB5613.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +ga0hg48hbSWkwI/Y163CPMVZySp3BJNxU9tPGRtGFNNvHpI10tyGxEuP2szISOzIg4FnctOUrlIxYSNv7A7U/UiBZn7soLx4pPKuu3pTBkYnEov7jWSxpRNs6su88bKrONzkEARrej9GuGiVovRP58ywahglZdqCZJVYaekWhcd8sakVDozKVFtctMwZHLSUAtIkTC/8LOxL9uYHS7fwG/YT5kB80/woZvntg9t2jb0RydNFo3GEa8Qf1EyRGnAte2UvYMM67scRBhObhufdtRphWVZw3FU1xSBVqX1koLa/G+yyA4vWVanwJ7jP9rChj/gsuYbhmwvHrw4udgoA1Ne3OwWQ8oOFHLrbAXmFqf0s2wouxtvCKwh7MQ9zsGVqIGvaK05mCpdE9uUVG9qzhE6ZY7rVPIn6dt9FT77aRSmi9krHvy9vPLW+/Zxdwlzup8AKBBL8kqnvOp2tdqrhm8P93uTBH4VkMee/yPCyJlYampIa0lI0T1Lks87Tt6Ft31QVIFVz3KiSrCQIrNf8i2NWf7plLQj56hQcYrIhQRJHwQoPUd5Gy7QS5qWeKqgPL9Ik9eX4gsPRBOLMqSfpu30nbKGkLF9h7GExjtF5Smtg4dMlyAktBTwH3wzh/0rlgHu+nsPXHuJ7jJAyV6unXFN++xy5QYmUOegXy+Vg7riOjXnShXZoyDx7jy280P7rhMd+Koa0as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DyMt13G2y13rj2U4UzGkCHrvM74LQ7+IfoOVVNcwziEOq0eHy2CPI9grI44JZrhPIXOV016eFBnqpG69uNtastzYrLzES82lLhFz/bFWPtSyIheDYhKZVHPxp09hXqq1M9nNSC63fl5UJCFzU3bl7haaw9tC0JKS7itzEvsx5aFZg81gMkSiP2yoMZrzbs80Lk2gGL1kdBmAj0GoZvX4nXOQCdmyUHD/hmfgGTiqUg1WGw8rC3xtcHYgkfBPjT1/t1mAUi0l3nXf8/xL2BVI+TvuZmSLZsPdYvdwsV7NZ5SS72nwLjnSRhz9dTlqWCN3fZ/yl7UMldS688zamOU3yQa+cCabsGfH0XLMMBUejYACcHZqTe1P0HKKC4bfS5MyUE3/lxybmTqDbGaYprLPclkm1XWymyS8kaQRRiY0stKzUNty+cAGr5cEfW2mecPRsPF6i7KOqOoTZmHSTRd3vp/EMD1EXW7cQmZy/IKnNV3cncK4eCXCX8MizfxP804cGrNasBrYdxafoo= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a0a5d1a1-dac3-45c7-ccb1-08dce131723f X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Sep 2024 09:22:46.7075 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: TI6jjyFtNygk+ZNrzpHm5nVsY9mHRRH6F5zCiWFS/nJlGisrjScO7i9BLvzcVR8dcCZjmAC7/wZ2zMLblT1uucLeFoS+zjW4z8Hot9a8D3Q= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8134 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-30_08,2024-09-27_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 malwarescore=0 mlxscore=0 spamscore=0 suspectscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2408220000 definitions=main-2409300067 X-Proofpoint-ORIG-GUID: HmTzf8z58VrX79xxr4B79-DVZ-sh1a8z X-Proofpoint-GUID: HmTzf8z58VrX79xxr4B79-DVZ-sh1a8z Add a PIDFD_SELF special sentinel value which can be passed as a pidfd to indicate that the current process is to be targeted (as this is a process from the userland perspective, this means from the kernel's perspective the current thread group leader is to be targeted). Due to the refactoring of the central __pidfd_get_pid() function we can implement this functionality centrally, providing the use of this sentinel in most functionality which utilises pidfd's. We need to explicitly adjust kernel_waitid_prepare() to permit this (though it wouldn't really make sense to use this there, we provide the ability for consistency). We explicitly disallow use of this in setns(), which would otherwise have required explicit custom handling, as it doesn't make sense to set the current calling thread to join the namespace of itself. As the callers of pidfd_get_pid() expect an increased reference count on the pid we do so in this case even for the current thread leader, reducing churn and avoiding any breakage from existing logic which decrements this reference count. In the pidfd_send_signal() system call, we can continue to fdput() the struct fd output by pidfs_to_pid_proc() even if PIDFD_SELF is specified, as this will be empty and the invocation will be a no-op. This change implicitly provides PIDFD_SELF support in the waitid(P_PIDFS, ...), process_madvise(), process_mrelease(), pidfd_send_signal(), and pidfd_getfd() system calls. Things such as polling a pidfs and general fd operations are not supported, this strictly provides the sentinel for APIs which explicitly accept a pidfd. Signed-off-by: Lorenzo Stoakes --- include/linux/pid.h | 9 +++--- include/uapi/linux/pidfd.h | 3 ++ kernel/exit.c | 3 +- kernel/nsproxy.c | 1 + kernel/pid.c | 63 ++++++++++++++++++++++---------------- 5 files changed, 47 insertions(+), 32 deletions(-) diff --git a/include/linux/pid.h b/include/linux/pid.h index 68b02eab7509..e95dd7b81ae2 100644 --- a/include/linux/pid.h +++ b/include/linux/pid.h @@ -77,18 +77,19 @@ struct file; /** * __pidfd_get_pid() - Retrieve a pid associated with the specified pidfd. * - * @pidfd: The pidfd whose pid we want, or the fd of a /proc/ file if - * @alloc_proc is also set. + * @pidfd: The pidfd whose pid we want, the fd of a /proc/ file if + * @alloc_proc is also set, or PIDFD_SELF if the pid we require + * is the thread group leader. * @pin_pid: If set, then the reference counter of the returned pid is * incremented. If not set, then @fd should be provided to pin the * pidfd. * @allow_proc: If set, then an fd of a /proc/ file can be passed instead * of a pidfd, and this will be used to determine the pid. * @flags: Output variable, if non-NULL, then the file->f_flags of the - * pidfd will be set here. + * pidfd will be set here. If PIDFD_SELF set, this is zero. * @fd: Output variable, if non-NULL, then the pidfd reference will * remain elevated and the caller will need to decrement it - * themselves. + * themselves. If PIDFD_SELF set, this is empty. * * Returns: If successful, the pid associated with the pidfd, otherwise an * error. diff --git a/include/uapi/linux/pidfd.h b/include/uapi/linux/pidfd.h index 565fc0629fff..0bff5276b51d 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -29,4 +29,7 @@ #define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9) #define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10) +#define PIDFD_SELF -200 /* Special sentinel value which can be passed as a pidfd + * to refer to the current thread group leader. */ + #endif /* _UAPI_LINUX_PIDFD_H */ diff --git a/kernel/exit.c b/kernel/exit.c index 619f0014c33b..fc10eb2b180c 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -71,6 +71,7 @@ #include #include +#include #include #include @@ -1739,7 +1740,7 @@ int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t upid, break; case P_PIDFD: type = PIDTYPE_PID; - if (upid < 0) + if (upid < 0 && upid != PIDFD_SELF) return -EINVAL; pid = pidfd_get_pid(upid, &f_flags); diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c index dc952c3b05af..aee86fcaf670 100644 --- a/kernel/nsproxy.c +++ b/kernel/nsproxy.c @@ -550,6 +550,7 @@ SYSCALL_DEFINE2(setns, int, fd, int, flags) struct nsset nsset = {}; int err = 0; + /* If fd is PIDFD_SELF, implicitly fail here, as invalid. */ if (!fd_file(f)) return -EBADF; diff --git a/kernel/pid.c b/kernel/pid.c index 26e2581210c4..9c037441afb8 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -539,23 +539,28 @@ struct pid *__pidfd_get_pid(unsigned int pidfd, bool pin_pid, bool allow_proc, unsigned int *flags, struct fd *fd) { - struct fd f; struct pid *pid; - struct file *file; - - f = fdget(fd); - file = fd_file(f); - if (!file) - return ERR_PTR(-EBADF); - - pid = pidfd_pid(file); - /* If we allow opening a pidfd via /proc/, do so. */ - if (IS_ERR(pid) && allow_proc) - pid = tgid_pidfd_to_pid(file); - - if (IS_ERR(pid)) { - fdput(f); - return pid; + struct fd f = {}; + struct file *file = NULL; + unsigned int f_flags = 0; + + if (pidfd == PIDFD_SELF) { + pid = *task_pid_ptr(current, PIDTYPE_TGID); + } else { + f = fdget(pidfd); + file = fd_file(f); + if (!file) + return ERR_PTR(-EBADF); + + pid = pidfd_pid(file); + /* If we allow opening a pidfd via /proc/, do so. */ + if (IS_ERR(pid) && allow_proc) + pid = tgid_pidfd_to_pid(file); + + if (IS_ERR(pid)) { + fdput(f); + return pid; + } } if (pin_pid) @@ -563,18 +568,22 @@ struct pid *__pidfd_get_pid(unsigned int pidfd, bool pin_pid, else WARN_ON_ONCE(!fd); /* Nothing to keep pid/pidfd around? */ - if (flags) - *flags = file->f_flags; + if (file) { + f_flags = file->f_flags; - /* - * If the user provides an fd output then it will handle decrementing - * its reference counter. - */ - if (fd) - *fd = f; - else - /* Otherwise we release it. */ - fdput(f); + /* + * If the user provides an fd output then it will handle decrementing + * its reference counter. + */ + if (fd) + *fd = f; + else + /* Otherwise we release it. */ + fdput(f); + } + + if (flags) + *flags = f_flags; return pid; } From patchwork Mon Sep 30 09:22:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13815711 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3756188007; Mon, 30 Sep 2024 09:23:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688187; cv=fail; b=n7gzsqbkA+rEVbEf4yZH5CXyRtd2ywUkS6bKd+dslNXpU0vYs2q1zM21KydkgPqVcilyWLduvKxxcPAJzRoR+tXOfkPlkem0BQDxm84L/cqpGmRPd4vfnHJ9oJMIUP4/wKt/7w6YzUYQ/tpGT1C97xeLrkkt/KAMuAA09a3TiZk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727688187; c=relaxed/simple; bh=MII/7zPyndC+HuFHW/rwxPxvph7oEY2jKp40Wls50f0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=BjjPX/K4f8/ciWnIPEYj14G88GZP6wHeWTHnu49H3q4+jpXJcqB3pP8K9NcczkFszHOKPGBYgC+jX9O0YZbKbWOLLfwT7j7Rrs93JzxXZ53eC2MMatSmEg/FdzRvwQ4e0bB+EWnwDKlgIcx/pijR01kiG0rtKBaDiB+u7gS38GQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=Y5EaH20o; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=Mks+zvzN; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="Y5EaH20o"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="Mks+zvzN" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48U2tsUQ014360; Mon, 30 Sep 2024 09:22:54 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :content-transfer-encoding:content-type:mime-version; s= corp-2023-11-20; bh=uy94UiJFBw073KsMcbu3zT/X9YDvCxHNCZOFkRHDofQ=; b= Y5EaH20oOMJfnWFMv/azEi7SIku0JZT63iRt+3JFIb1M03tqzU/rx4RHJ8BIw5/v mHBN/6VY1zvX2fbVLHG+m4UMVdDs6LuQus7DmvuHde5kjgAkptQhclpJCZTWtUpB Hd1xxS9hyShSX8kp+pJiD46N4ZyvkSGknvHKwKRL/y4iYycQ90/s65W2ZCGVQ8wb IVAkB8OyMYnbVN4kaBVXSKctt6hh1jKH4EUwqgJJ/7P44WRcn4aM4ISFA3auDn+Z Vs1tettVPsGwcolXUqilJ0czpaG6RF9jg1m639dI3HhfnktaFXJVT08JmS0attDg M0ym2g1F0myrAPlXh8D3UQ== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 41x8qb2tu3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Sep 2024 09:22:54 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 48U7EUO6038677; Mon, 30 Sep 2024 09:22:52 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2046.outbound.protection.outlook.com [104.47.55.46]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 41x885t06h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Sep 2024 09:22:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=OC258i8IiXuaStkK19/nFGiloCv+S8r+9JrNJo6Ivi3YHIyEAs4c+e7GESJKvtEH7zuXPGg6l6anh5S6ui7n+hwTx/vZ/8LWRwBoEboT0DOvfsirJmbR9FExckcqntRPh74X7KAJa2AeLNEbxJCImqVE2myCx1W/DM2sRfxDHirJEX9N5vYCC2r+4vgj7jHR3CQjJJH8/91hS+BZcJNC/c4kXzHdHxcHdSbZ9Dj/kNtZi8CBjN+kI3msfdIHX2ww5iR1+cth/uYWGcpCH0vW3BJXsfdK80FEoJlV9rukv1WvWdkTvpXQgZp5L8YQFqaFFYXa4ZUcTqUQjJmQMWJ6CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=uy94UiJFBw073KsMcbu3zT/X9YDvCxHNCZOFkRHDofQ=; b=eXQXhx4tLt0Ia/bVzfLS7AyiGOqw/pbaU9YCDCekL0rs4mwml0cIG7Wrwp+eICgc92P2lXL3OiGWMRzEvDhtle8d6YUxeItIhVJhwGN9+LpeJJ5yvBC2jGfNXnqTFROaiUGeKrnOvORJlYAr+rjbH5iH6BMGKESFKCKGP5JT5k88CTNy7nPSfEcuGSNeSSc5UntBpEQC+RPHTPrtGNtldY+F1OIDsShcLfQjpNmworobguGdR7jIuCpnMbvpF/SQNNko+liUt3B4PCkU2luq+/w1io6lDJeHOX8Q2OktLlYr5LW/OZs9HnP38rhosKWm4N7lrM3+B29hextOdiGrBw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uy94UiJFBw073KsMcbu3zT/X9YDvCxHNCZOFkRHDofQ=; b=Mks+zvzNWE28uBxbK6rdaDk8SPtYlm1MtqQpTgVgY75LzsgzR36hY0vuuCY/bRUMnjVI4+tGHz6NqAKVahNN6HIxNlrj6uG1uBN9bI/I9uUxFnxCt3iUsc0fXf0mYsDDBIIjqDPxX+JnEjoBnKnfROutMRIvNG2mZ/roSjpr8Rc= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by SA6PR10MB8134.namprd10.prod.outlook.com (2603:10b6:806:443::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8026.15; Mon, 30 Sep 2024 09:22:50 +0000 Received: from SJ0PR10MB5613.namprd10.prod.outlook.com ([fe80::4239:cf6f:9caa:940e]) by SJ0PR10MB5613.namprd10.prod.outlook.com ([fe80::4239:cf6f:9caa:940e%5]) with mapi id 15.20.8026.014; Mon, 30 Sep 2024 09:22:50 +0000 From: Lorenzo Stoakes To: Christian Brauner Cc: Shuah Khan , "Liam R . Howlett" , Suren Baghdasaryan , Vlastimil Babka , pedro.falcato@gmail.com, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 3/3] selftests: pidfd: add tests for PIDFD_SELF Date: Mon, 30 Sep 2024 10:22:30 +0100 Message-ID: <12dc5760f45f8c90f57be43e6f29f1ded473053a.1727644404.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0016.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:62::28) To SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR10MB5613:EE_|SA6PR10MB8134:EE_ X-MS-Office365-Filtering-Correlation-Id: 77556be3-efde-4e9d-ab3c-08dce1317450 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: oi2ounY953a2r7Wnsy0h4tCssbH9vf7c0NKrrVunLtXv71mgXHrINa8r2LFqVWCaSGjvfLoFFQ+4uda9OA8GP5HHvcu8jfAatsZvXMswTWPJ8EVe6qfcwypCNSxhiGlOphRhbPFjdTY51oeP/PXdUWiRY99L/cDm7iRTKXugjPP+58sYXPfQ3E6sKz42nvQDTR/Fhhxjvymy4iMkWxrf9kSikgpqHLuP62r+hSkp9/0lG8zRqLmzSmDLl8bt/rm2O8Q1jpIyZmnrwx+hdxjJXlwoR1sNF8/wcFHWChC+LcstafeRwiqa3HlAb1fvBm4HY/j22VkMHGfLWWDJPVRwzZFo8x5Ky4+RKXPU3AcwF8tn9GrNMbkqQUDuzfZ5tgK2ALMtW9igVFQZFIc4GM49GZ5SLSHtgxMMQMgs765b7QINzY3ZgTHXK86MblXQhWVml+AZQQDNsRg4DBibdPopsO6MUrn2Oo3/N8yuuVJrd2TUsZwzgUN8Oa8DsUkYCcGj4vGrxKF7s7U20obzrmYU4/MiTPuir4wNckXtbdA7YdXeb59/bvyObqNrys5vhBOQKg7wX+ic6C3w4HGZL0hSl2ZjEPDYZbev/5Cy4J2XJsvEsRk3JqqvNRN3msFV3Mf9zdKjAOrhhZ/An0Od6dxB1lsjR1He+8K8NzZjXWsltvVbEyRjj8ObWkVZVwdLB6PJ4emLEh1px0YIQb9nxWX+EglmZFgNEBzWwNGuDzZsl4d14HW/8WeoSXEbNm2VEDV/XbjDeTGcj3dL5uGmVnYTBMoT+z0L0YC6fDmEwgeZK25cXt4W0+GWg2d4JosqdbRwmDHg+XHCAQwrL380fFvkZJI02DOg+lXrVxglaC5wKm+jmX4OLi0U55yx3yv/norFq5rbdxe/MKP1LMnZaoMP6q5qDTTRC1sFxnsZtdY6tYV2qqYzBGmkdj/u2SG+bu5AdvNs/MMZdVrUR0Rd0FeqDKXddwPQgqDs8ab14sZ0taE1rKIfEWRLYmBaiXrT46yexo5NR1PCH8Y2BNotjlz2/f7vBMUGUhkSARN8lO0GU2mztrbTFo04RM/puQrYSzfndlthLho+LHzmdhxdeEERZApkZf8zyPBpWhEpC1758eCCkjystm6hbdCOnJobvH71ThZYbiEThpmhhehFNeXqhqQf+9zo07iH5l4yQG0QUgKK5vDkoKeSFZl5GfwTljSX3aR9stMSmnr4nhmk5f8ogcjJm7aGwy2QWRS9HRTSQqp8JhbU4ddzTf8E4sBJUIqYAy8JO7pxr2FSjy22RpnS7k6ceo+mJslgQBlj48igg39fNXfqGP2v/leRE2xXXi91/jLEPzQcVpvlMdwJG8DAOA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR10MB5613.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7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NZmXf38jbw9bFvwV2x6G0Si+wdUAKjl74QrJq31O1PmN5uGDavi4+JHO684zJnu5h2H0ECcTX4bHcSD8o4608+PGypK7Cx9DkWZf/gRfffNZJ45jtWk9lAIA7pDHLec3Xa4t5fbWHHUPyUVEt8DSqm6qzARsD9vDk4fnF9tqyYHGYzK13eISjDuXKb6uWuzQsQ/qjZ3UMqlUZ6thABMFkmEpbel2NSZ/4fNk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 77556be3-efde-4e9d-ab3c-08dce1317450 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Sep 2024 09:22:50.3036 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: LPCVmmnEeo7v6/hkbY72dPTb2XbTnwRLeq32XWwOQzprMq9nfCBDgIXJeTlR+l3e8qU37Mcvg+rDmkcW5wQt0hgLpIDQq/zGHEP+RzGe974= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8134 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-30_08,2024-09-27_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 phishscore=0 malwarescore=0 adultscore=0 bulkscore=0 mlxscore=0 suspectscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2408220000 definitions=main-2409300067 X-Proofpoint-GUID: wD8FIO5tIN44wvCvXVbxiKRrjX7M3mvl X-Proofpoint-ORIG-GUID: wD8FIO5tIN44wvCvXVbxiKRrjX7M3mvl Add tests to assert that PIDFD_SELF correctly refers to the current process. This is only practically meaningful to pidfd_send_signal() and pidfd_getfd(), but also explicitly test that we disallow this feature for setns() where it would make no sense. We cannot reasonably wait on ourself using waitid(P_PIDFD, ...) so while in theory PIDFD_SELF would work here, we'd be left blocked if we tried it. We defer testing of mm-specific functionality which uses pidfd, namely process_madvise() and process_mrelease() to mm testing (though note the latter can not be sensibly tested as it would require the testing process to be dying). Signed-off-by: Lorenzo Stoakes --- tools/testing/selftests/pidfd/pidfd.h | 5 +++ .../selftests/pidfd/pidfd_getfd_test.c | 38 +++++++++++++++++++ .../selftests/pidfd/pidfd_setns_test.c | 6 +++ tools/testing/selftests/pidfd/pidfd_test.c | 13 +++++++ 4 files changed, 62 insertions(+) diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h index 88d6830ee004..099ee7178193 100644 --- a/tools/testing/selftests/pidfd/pidfd.h +++ b/tools/testing/selftests/pidfd/pidfd.h @@ -50,6 +50,11 @@ #define PIDFD_NONBLOCK O_NONBLOCK #endif +/* System header file may not have this available. */ +#ifndef PIDFD_SELF +#define PIDFD_SELF -200 +#endif + /* * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c * That means, when it wraps around any pid < 300 will be skipped. diff --git a/tools/testing/selftests/pidfd/pidfd_getfd_test.c b/tools/testing/selftests/pidfd/pidfd_getfd_test.c index cd51d547b751..cdf375fd61b2 100644 --- a/tools/testing/selftests/pidfd/pidfd_getfd_test.c +++ b/tools/testing/selftests/pidfd/pidfd_getfd_test.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -264,6 +265,43 @@ TEST_F(child, no_strange_EBADF) EXPECT_EQ(errno, ESRCH); } +TEST(pidfd_self) +{ + int memfd = sys_memfd_create("test_self", 0); + long page_size = sysconf(_SC_PAGESIZE); + int newfd; + char *ptr; + + ASSERT_GE(memfd, 0); + ASSERT_EQ(ftruncate(memfd, page_size), 0); + + /* + * Map so we can assert that the duplicated fd references the same + * memory. + */ + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_SHARED, memfd, 0); + ASSERT_NE(ptr, MAP_FAILED); + ptr[0] = 'x'; + ASSERT_EQ(munmap(ptr, page_size), 0); + + /* Now get a duplicate of our memfd. */ + newfd = sys_pidfd_getfd(PIDFD_SELF, memfd, 0); + ASSERT_GE(newfd, 0); + ASSERT_NE(memfd, newfd); + + /* Now map duplicate fd and make sure it references the same memory. */ + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_SHARED, newfd, 0); + ASSERT_NE(ptr, MAP_FAILED); + ASSERT_EQ(ptr[0], 'x'); + ASSERT_EQ(munmap(ptr, page_size), 0); + + /* Cleanup. */ + close(memfd); + close(newfd); +} + #if __NR_pidfd_getfd == -1 int main(void) { diff --git a/tools/testing/selftests/pidfd/pidfd_setns_test.c b/tools/testing/selftests/pidfd/pidfd_setns_test.c index 7c2a4349170a..8e1510744aaa 100644 --- a/tools/testing/selftests/pidfd/pidfd_setns_test.c +++ b/tools/testing/selftests/pidfd/pidfd_setns_test.c @@ -752,4 +752,10 @@ TEST(setns_einval) close(fd); } +TEST(setns_pidfd_self_disallowed) +{ + ASSERT_EQ(setns(PIDFD_SELF, 0), -1); + EXPECT_EQ(errno, EBADF); +} + TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/pidfd/pidfd_test.c b/tools/testing/selftests/pidfd/pidfd_test.c index 9faa686f90e4..18802b657352 100644 --- a/tools/testing/selftests/pidfd/pidfd_test.c +++ b/tools/testing/selftests/pidfd/pidfd_test.c @@ -85,6 +85,19 @@ static int test_pidfd_send_signal_simple_success(void) test_name); signal_received = 0; + + /* Now try the same thing only using PIDFD_SELF. */ + ret = sys_pidfd_send_signal(PIDFD_SELF, SIGUSR1, NULL, 0); + if (ret < 0) + ksft_exit_fail_msg("%s test: Failed to send PIDFD_SELF signal\n", + test_name); + + if (signal_received != 1) + ksft_exit_fail_msg("%s test: Failed to receive PIDFD_SELF signal\n", + test_name); + + signal_received = 0; + ksft_test_result_pass("%s test: Sent signal\n", test_name); return 0; }