From patchwork Thu Oct 10 18:15:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13830643 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 3BBEB19C564; Thu, 10 Oct 2024 18:16:09 +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=1728584171; cv=fail; b=jJAXIYfH1sPMPPzxkpqlBVp1MQCy9nO0SUW+rhLaUqDxGn1i9p/QSYcBiuuf9MjbcKieeil7jquTWQhXg17/jlp/AGt4uovDoyd9ulKa9yo/EXLqNS5t9iPY9PW8r9dT03j1nzk+zWEYAk4XIuHGK+ajBUa8l6f1p0mi0URGKF4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728584171; c=relaxed/simple; bh=NNU+AwdMeiPwGrUfdNmufqfA8by3JsOh/k5Bm4J9m98=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=phyUvBwVYLDtkb8CmP3ubHCPlHOdeLQJLjN0N4zqg5xt6PkDKHBd8f3H1udc10f8rP4SKjEWdsJN1PDg6icMbb8HquEJsInM1XXAnXUHiAjVOYkZgtKnczZhOum2lispKb2RXypObB/gPYayb/HgZduxl2KXEYsl1/+b7GWudxE= 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=X2UCszsE; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=SWSe4jAp; 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="X2UCszsE"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="SWSe4jAp" 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 49AHtdKY013526; Thu, 10 Oct 2024 18:15:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=DQXBsOR2E19Q2CMf9E95TjmQXv+9t0EfWPaUCkT1KJk=; b= X2UCszsEJBiPcWQAMnN1DGqOISFCrR9Uh8OrMLmn9vBEQFjMlC9zR0CYHWRKtwuL g/doAR8sq1LgS1cbbHwjFdzDBV+3nuOBqLQqdCca0d+kwePRWUeLzleF4flQRKFq 6URuZwswOd7qRn/m802mgTIzTPimczhdhdseFksm+jZBtHXDqBr7DGTnYY/xwokg aUja3F4wEtBSSrNU7CgrJATy7Y1ASwWXhzEd796E7MR7D4I2puwgRhSlXWFdG+1X chmK0RK8S+QRNgETeHTlRMVFtdKKGqcT6fnYQlKM6h5YyB35DglmuJiQuxG5GbaX dBwnyOR8l+KVOZXy8BqG0A== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42302pk8nq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2024 18:15:56 +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 49AHH1gR017491; Thu, 10 Oct 2024 18:15:54 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2043.outbound.protection.outlook.com [104.47.55.43]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 422uwag76g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2024 18:15:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pTnWEn0le439PPKyyOfWr1gV0ncysPoXdF8rlA/QsFLxDRXm5hHbHRT36ISjlx/pGFrK4OHMZ5WUO/a3dzGu+kJAsbyqNyE4K8rHibW2XowZKsS9zTVCMtnT6ZKm0or3D8pTMMSY3jZ5SKaBiyadvpqtebAUPAnidpi2CISXq8+T0qbmQkisedb8XiVCApK03KgoRmf23VPo/XPNcHj/lyXrwbrNMRwRsNgJEOHVAy/Cx0GaXj2pu6N0AFALOnrFffEvkcfCCH4lY8ydz019PzTpv6Z36Wk3oiGnamhPbqvtHWC+cpZuODaDSv6Uf5rC9isyShOCyrN/Oacfr6HLog== 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=DQXBsOR2E19Q2CMf9E95TjmQXv+9t0EfWPaUCkT1KJk=; b=FB+727kdsA08p87g3NbOpaTDQngSFbCKDC/sdvDArF3uknKT/vpRKmOS6Y/bclRKfwSCqHEbELO6A9dMUIX4Lap4lQXkuo2/eEW/uFklbb/JveLqXMUSW+Sb5q3LwWvDD+1UHOLdk9zYumWMRDp5ogGpumyZuG5Uk1NMldIv6N3SC9iCF+9crA16zupqimAeoUIl+qREHEVmNpq83BxvDbUNHWLw3O53BJzPHU4sKO4H9s+i2X78lDBDv3PSAKJfwFBo487tGRhKwQNVTCVb+rmNUCx7LLFYYrAJx9Xmoj/jY7g9nXIlBVyTC9DYOwslwohAN01WQ2mVwNHl0+Ghzw== 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=DQXBsOR2E19Q2CMf9E95TjmQXv+9t0EfWPaUCkT1KJk=; b=SWSe4jApdzVBLzdo2Bo4P0MNvX1oClT6/HfZmxA7Nell8faUSa/kWye4imRanXpLJ5NxGwAiOQry21nbuna3J+WC+s8shEtn85mjkO+rNPPFPh/LQ6oEQMvNsxGaKqp7lgr2yj7mEBSC3B9Q5unZo+ymaZqwbO8V7+yOhq1mgMc= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by SJ2PR10MB7826.namprd10.prod.outlook.com (2603:10b6:a03:56b::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.18; Thu, 10 Oct 2024 18:15:51 +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.8048.017; Thu, 10 Oct 2024 18:15:51 +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: [PATCH 1/3] pidfd: extend pidfd_get_pid() and de-duplicate pid lookup Date: Thu, 10 Oct 2024 19:15:43 +0100 Message-ID: <8e7edaf2f648fb01a71def749f17f76c0502dee1.1728578231.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0595.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:295::12) 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_|SJ2PR10MB7826:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b3ad6ea-a3d5-41eb-b55b-08dce95792d7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: pfHuJPpQyWx/1FVvKbfTWXnEkY0dJjHI4SYLSBkiDOYhoe3dHGKMQPlJwtcc5YHpS9EaPYBXHP94GsNd4WIKCYFqsP2zT06yy0ez2clZlXyqZ2J5oDARKp9g8t9dsgOV/ONNvvrOvMLzQXA1WmzxP3dT5N9srwhk+CIaiaSdvDcE1Iu25Vqy5GgSnOk3OqFnREgpHPHyZVuK7any22J2JeExYgudtOc3jpYlH9OTPGAoIH/437JeeXUGeTVuCD4yos9n/vN2nFbHYFolOxUzymBnbvylTpoQPCOllR9ur3cmn21l8l4Q57BgUn+Uk7uqHHnTUKCdmnNa87c12OApEr5n30508buAMzYY+vU8Yi0+WtlqT6N3gOgSeAZ3wzcubP2ItOayC3pcJqI1pd4vBP5U74sUCDEttfObt0IUkMA3ZFO6yykTay+5s0XGfHwsqE/91O/V+EivWS27N2UqFjQGSTN+J9NQki332Lkm/2zf5ZQnzUSLgCn0/LHPzVdC4uZOkivZN04GolHVHQ7xjMZ9h98pc3DkxYmD6ZmRj4uTpxDsNcFpDhMvi/gNjMt6PNEmf9DBO/sqE9lhV7Lg/XDBV5STJtlJBzxQUl+1z27CZVYC75UWomsaojn/jY2SppILtBJTH1WbJLKoU3f2GayYLE3/TCWNYjP0orB5a9kJXN5RhipVK1Y62zx5rcS6AasFKQP+CwqYpTnNP2+7XNrXWvSaQMTwvq90zYABYC0VG41Dznp7x9gQlGqmOFBXX9wsk7limNLtCztk1XDWpwvAeQH7ln5nijvo0fboZmdvP1H+AUdx6wwFv/2IWEYekNL3mT728TCCRsyODbOO3H0E3icdJhXwzb2+Ue+lo89wh+IvwdBnHhjJel/4wSAuJSFmQwOEeD0TfFiwVkAmis00ry77udFqW13MR/CEkSj6wpVqOhyatc73elIB+ubDyEc3qddElTzrw6uQe5uBFp11cr/YNDq8a/8hl1TkiOuuqwaB3TQcjpLCcnXkW1sSuw8D21fhFk6beaNOTMxcjeeHEmBOQdfkmK7lWxVEiJyijsPtX6s8LL4M7i+Nr9sKMcKYLZ9kkPJ3m/U6iSSTo8QCXJd7NOY0w8VdFXi1GKS7fD6/SAxbc40mzcWh8RwRVzHIYbWHDmok/vEly7JE93vFa1NswltDVO9F/BjkQe5JN72sg+DEd5RPmJtoCab2vT71A5BOLaHsKruLWBNNjTFrmcs7pZpXQ5LaqWnEQdnYTW26yoNPcVrDeVAKKbIJXTJyG8Yz+o4Re8OlWE8yxQ== 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)(376014)(7416014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiS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LOoY/3iSBQJuFjp5lAKH7Eob0iGhnWAa510lPLMrgab7XDk/Y/1QgJaoJsIobVtIH/Z2Q+EXaWAAPLnrJSIQ5nanhHS4fQR4ZfQr7s1SFC7SdveHPXJ7QaYdBP3NNnZFYcVClob4IZ1p/gOS012tR3LimZ8TGZAwOyKqjUGPwv/34hZFLbm2LlQOAtuZ4yv+Ad8ydfmbfygh0g= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b3ad6ea-a3d5-41eb-b55b-08dce95792d7 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2024 18:15:51.5494 (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: acRLvI3S5W2RYOUyqG5FzjZUy08amFpUvxj/riluyyt4SMBTYkY0Msdx0PUJhvDzzo1cYZN0nrNsUeW/iRWgMH9ga1j+btbZSIUbb+byTGU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR10MB7826 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-10_13,2024-10-10_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 malwarescore=0 suspectscore=0 mlxscore=0 phishscore=0 adultscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410100120 X-Proofpoint-ORIG-GUID: VGFbpmKXnVzGlFPQJyWe_GkShlA0HmN1 X-Proofpoint-GUID: VGFbpmKXnVzGlFPQJyWe_GkShlA0HmN1 The means by which a pid is determined from a pidfd is duplicated, with 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 refernce count). Additionally, allow the ability to open a pidfd by opening a /proc/ directory, utilised by the pidfd_send_signal() system call, providing a pidfd_get_pid_proc() helper 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 | 58 ++++++++++++++++++++++++++++++--------------- kernel/signal.c | 22 ++++------------- 3 files changed, 84 insertions(+), 38 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..25cc1c36a1b1 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -534,22 +535,46 @@ 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 file *file; struct pid *pid; + struct fd f = fdget(pidfd); - 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 +772,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 4344860ffcac..868bfa674c62 100644 --- a/kernel/signal.c +++ b/kernel/signal.c @@ -3875,17 +3875,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) @@ -3908,10 +3897,11 @@ SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig, siginfo_t __user *, info, unsigned int, flags) { int ret; - struct fd f; struct pid *pid; kernel_siginfo_t kinfo; enum pid_type type; + unsigned int f_flags; + struct fd f; /* Enforce flags be set to 0 until we add an extension. */ if (flags & ~PIDFD_SEND_SIGNAL_FLAGS) @@ -3921,12 +3911,8 @@ 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)); + pid = pidfd_to_pid_proc(pidfd, &f_flags, &f); if (IS_ERR(pid)) { ret = PTR_ERR(pid); goto err; @@ -3939,7 +3925,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 Thu Oct 10 18:15:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13830644 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 D299E1CF282; Thu, 10 Oct 2024 18:16:14 +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=1728584176; cv=fail; b=kKO/hvuzg6Bbjj/iIL75o7d6ztk6qxUjC3EDtS6UUAze4WxYkkOOksOM1EZI4ZumxlixDboXdPdKhnC5ztUIoLqLTs+wnwQf1U5VV8KtTmi1Fx40N35ilGsUZgJxRj0Pp7G7fhJy+1YsIpybq2/KpP5Bt2CKZBaC0Zuo32ErgJQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728584176; c=relaxed/simple; bh=VGMkHzBs031nVI/l8gFbxz09UsGpUu/M7LgaJA0esxs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=larffOAFcagzK7Eti1hkIH36+lVM6/V1CTDbETZd2DKUCAhFSjIUQzLootyI/i/ENFsGXOPmh5ey1FBHv6IjZaQlaGZtzxTGZ/EYd7AMaoOrHnFN2XOYDLhmiFCzDS8LCOsBSJgtS/OYxhouyrJVciS7GfEatdjr9c3D1ERHwo8= 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=nCa1iDt0; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=kACLuJiy; 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="nCa1iDt0"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="kACLuJiy" 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 49AHtdrn013529; Thu, 10 Oct 2024 18:16:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=YGsMJ89EHfHWpqvg+1cE2NG8PMFwvAfUZSszICkBT4E=; b= nCa1iDt00ID9Pk6RcM3uIjqztm7WKbGNTs1fyBCmKVL1d4DqsVq6vW/Y1MZ10gNc PpwzCi5rB/oVCy95vcRSoAi53gHZ5+ms3pCQBLKJcD2h8qmQ7iFGp8YKbHqOi5v2 pqszFuGDrcMLDtyAXh7JWJMZs1Ysoajprmjn78tf4j/b9CoTSAKKlbdISpnRaiVP pJCFcdzuGP6jcba2armVunLYH6t6GK3nRBjAWdO7FpIHbfcKPV/EFzpBuCqhXlVj MFzu2cDeWBbE1VNNJnOTMITElWCwdZ3WfAdRePU+NnYTO2M/Z1mnzehOl5Odb2qk +C/zmXsANqQ2kAQJXP7WBw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42302pk8p0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2024 18:15:59 +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 49AHG4jN017547; Thu, 10 Oct 2024 18:15:58 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2049.outbound.protection.outlook.com [104.47.58.49]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 422uwag77j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2024 18:15:58 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=SzYDtgO6EHiM3MCh2cEF+Mj10F0QwKkBWq3clH0da5o8uA5jHDBtPgWjh1ljuLAxxw4O+1ySU8Xu+JEDwp1IGKOny0JGCHoVpXwEcxfBGIzIX2Ie5fm1XTmmkS8YMjsFXm6Nm4iiejyIIx+QcHLWpyDztuQPTWBl27DLPYhHJtcIGr9neEEvbjMAwKlkezqBOk5hJQqyTgdF21jb24QPPpk2r1eBrr/BeZ93CHI51gxvRt0Gwu6VvKQr8aQUPHPeKpC+hzn/deYJ2NPqT8lWUNzC9abh4Uoz0jsW/ru9mKVvBH6a3H6MVSHaSP+DrmfOg2sv9+xb+a9G/uYTJ0UG1Q== 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=YGsMJ89EHfHWpqvg+1cE2NG8PMFwvAfUZSszICkBT4E=; b=EXtp675OxYiYK2+MvCcvhanuAdlUmuRLj+nq646/lUXM6sr3hM78r0NFrHXlnhTbHU1bQkfqOR7ZNH3nWRm+kcUrnSvGgj479FC8SXTCZz9BYnNmUu4rG9dKpQi4ua1ExV71aQ7YVslQ+V075IAY7WPZq+xk7CR+b7Z3zzkaAklJNsAZ89KKc8HXqLlFQfagmISSn9p/DEWxcqRHiMZOHmE+O2ANBRvaX16whpdxgkcIqse3z0spj02THmXdU8dQ15zYR4RsKyOet0KuKa+s0Kq1xktDwQrdX11k+NWpE9bBzY0TAp2UrLyxdTgh4w0fSaBZXtdFTwfvhVIAE091Dg== 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=YGsMJ89EHfHWpqvg+1cE2NG8PMFwvAfUZSszICkBT4E=; b=kACLuJiyJ/8bJR1+CZ7Lvur9Wbg5UmWtRUpOp7fCDxEhqy9+IKu2Z9LmI9WglqO4LttK7MjVE0ZaTVf8KWeV8uWqhvvwdU8rEGjPvsxz2JT1SNomMPWHW1pPLL8E8ZCEL7G/w342cBTK5KoOjWUBJo2UXnO2FigomAuhNIvUIFY= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by SJ2PR10MB7826.namprd10.prod.outlook.com (2603:10b6:a03:56b::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.18; Thu, 10 Oct 2024 18:15:54 +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.8048.017; Thu, 10 Oct 2024 18:15:54 +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: [PATCH 2/3] pidfd: add PIDFD_SELF* sentinels to refer to own thread/process Date: Thu, 10 Oct 2024 19:15:44 +0100 Message-ID: X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0429.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a0::33) 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_|SJ2PR10MB7826:EE_ X-MS-Office365-Filtering-Correlation-Id: 5dd2b124-fd38-4ebc-9c53-08dce9579480 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: W8qwq3le7UrSvLX0QzEHM1cAw0GuLIOSrMReslPJPAkPSrEw+XsFfOE2Vk2ssb7bvXcDWAkZmFQvASf1k66p/XL5K+gpoIWscZFRBO3sE/DUdiG+BRS156qs8yGFYOoPXfSeQjJsYUqvWvbeVZZH4XXvfMTHm1hPUHAq1i2EF5748dlznQu5WF6/HRXNrU0BnemuB9BTKR9+nOlHz6tttyzX/0oxj2DmnmvKAYPFNzvxJk9mkmezMVs7ZxV6O+JKLX904Aab10oTV+bNEJwyZTgcHq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NSPM;H:SJ0PR10MB5613.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(7416014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9QBkI1JOPkgWpBT3phKOtQmV46Dl2P6/S3TuWeiShC0gtrGiTPaihfB/uVyldJLXW0XZDITuW8ALgLev5cAHNVyEQerAjSNTBfGt+HJfWY74HDeTnK2iI0PA3SDyNGBjQxqWja0PMipv6CkaH9+e8OP3C+8RXI6v3V53laJyWKe2H0yFSgF52f5q9dRC0ePJnScVSBu3yQGzMkrKXRkq5eJRFK9cREuMAiJtGAvV5l/7AQthNvAiPFzxHONHnag+wVxIQE2VwC60b0Veu6hEceJBbLx4wrCe6RnkQFpE4GaFlaphRd87cOrJKYNGaIyr6gnY7fnNd19xxAkEwnxKK9wxe2p6Wn96zDqYbWQjGcNUelHw+yYajLkjX1/7scn58kzz4xPiMbUt/KFfrzZ8zdhVrGp/Ss0e5vBTn0x2hPcJvBEWFi6hBJIysy5cBJxgdGJdwLea8mkEU1sfSEYvZ4JK2kwhHs+MmLlXKocGg+WikSQ951KAWVQwitFihBDnZXoe48FHput3aBxkIQHg7z2QJl8cto+1pQisQoUAaoEIL9o2mjs0hzxXBGKjIhKqZRlmDQNq1MJ5pC4sw2PFe0P7rLH+Xsyj8mDjPyDhIjeAEt86nU9gQ6Rj7qrnUPqckd8htMCbBUIBjNqpi/0BknWah7Am+IeVdY/x62So9xo0/TG8pp1ULOUPTquUoYTfHj4A/yvM1YjbD6N5lDE3IJuYD09Xt/iHQTpHJQVtaz0bJ6PWsWvTQhakFU8FvQH1nDAD9wjWYrVn/f8IWpyw+Nbygc13aeHwE5eCKAQ7PqKLc9Wz3Z8LeCShEO0tysciia4qhcJZgeOzXMGERr15tLDtA+3cYt0FXzONxLnQbksoXk1GUyidzww7TekCP4gALuQpdlIFE9sUStF9B3OQi0jxaYHb1pTtJG3B/sZ9Ll9M0aN14IRJaLwCbIcwOyqiK/BrjIydJb4RAYDmhz4CLV6T9YrXOImSwSqnX9541zCwaw76PGFS70aCwTNz/V1EFK+ouxvy25kDMqblzZJCww5jFSbvCpvfT3myCk5TkcT50XGMlMecaTBr9d+RW2+7jpituxIVzqQ1hyx5bT3oEClKfijQawDC0UrNts7U5sy8nc//C7sg34vn9f7oxO0YVI7zv1KLauq/fSyrsh3JV6s+JA6jFBmDMlo6D6Soz2JWM7zkSxRdZgBJ7Lfyint0THFyDgfPHgb56aPUJR2MKKRP0fGaPnBJqETIfNbfFfyDSOadTDyyYCb3Lz5fZQm9w8S3cHMvCkfYr2KGKGGioFC95uL1GoXr2AmGFPYApVAzbCN+peFy46Ki8FeFu+D6xtgjxa1+4IcnUk2Oux1M713YllLDAeZXSpOaspCAHQ5oaCfg3vl7bjVJ04r2u37tVgX3+fBmibmmodWonbrz8eY4n8HLAKASGsgGFMkzZrILZT0mqd7NadvAob9VQaZnoaPhMukTGoLPYNIrNSThAvOSJgdphWAi50IR09+X1KajylRbUIKxtiYEexowseQgNhW1UvRjFDs6ULXU+F6jsFPsDF1BE3BWRuuXGDkHQ8zW57GeBOYLk71EofVLauWZEngDiNPyAbmXa61i4s6nwQ== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: NsBZ1Hicglu18lL2jXMniK0Jij6xlgW6bGcgXr5VF4elH4FaOULpYh0STR2jbU09vNuEV1dBHZBNlogL4q9neU8UcvkfNX8VetFNiFsPdFJRbhz5kiIIy2IFMpB/tW/2i+BVMjrAY6YH9tN5LpgrWZfjTpJb6YoIIb6VYBbjxVYmZQEN+aSbFXcpm+L2C2bdVU42k+td2yJoqWQItpwU5fnZOp3cAMy4fvwZKYqhkTTx/asnzqxBARMYsB1u0AUbAsLl2ANVkWaa0zkczmhtJw0nd3zeTcq5f9tvOdf7qVNBeNxdm++Df4gJSXDP1imnFSkEnvFVh+MSol6PnsX4R1ZDv65o8mSr02Ive5qFO9ui8iF8qXToh43NDY1tioEohUQkviUOSPLCECh2LVvAxPIi7Z7UKZIFLJT64WDwGbWoXMugFafzu6BAgWL92vKid0SleyC9siRxJ6Dn+Y8xuXXnMhJMGXFO/+wIqRWsTSX/qUvNqf8JIuTCS7burFi0RI0cHe6ymmG/MEgE1kaWcqBgwWhzegwU7m68VrJPigrGPPgiod51RuI0gBPlHhCq324L/+VuQGm07EO404Xsla7Cuzd9BytE6WY5VA5/DL8= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5dd2b124-fd38-4ebc-9c53-08dce9579480 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2024 18:15:54.5317 (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: zF1BACghZQQQOX9ERbKM9aSQzLl+yrQIy1YRrhVd6C64EXa7I1txuUgmhLJi7mZQckmoqo8Yb9FfpzGbKTuC6B6ITRalBNVyeFhjriESi5o= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR10MB7826 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-10_13,2024-10-10_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 malwarescore=0 suspectscore=0 mlxscore=0 phishscore=0 adultscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410100120 X-Proofpoint-ORIG-GUID: frTi7iFCRY5pC-kfceov9tqY0sywy46C X-Proofpoint-GUID: frTi7iFCRY5pC-kfceov9tqY0sywy46C It is useful to be able to utilise pidfd mechanisms to reference the current thread or process (from a userland point of view - thread group leader from the kernel's point of view). Therefore introduce PIDFD_SELF_THREAD to refer to the current thread, and PIDFD_SELF_THREAD_GROUP to refer to the current thread group leader. For convenience and to avoid confusion from userland's perspective we alias these: * PIDFD_SELF is an alias for PIDFD_SELF_THREAD - This is nearly always what the user will want to use, as they would find it surprising if for instance fd's were unshared()'d and they wanted to invoke pidfd_getfd() and that failed. * PIDFD_SELF_PROCESS is an alias for PIDFD_SELF_THREAD_GROUP - Most users have no concept of thread groups or what a thread group leader is, and from userland's perspective and nomenclature this is what userland considers to be a process. 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 the self case, 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 | 15 +++++++++ kernel/exit.c | 3 +- kernel/nsproxy.c | 1 + kernel/pid.c | 65 +++++++++++++++++++++++--------------- 5 files changed, 62 insertions(+), 31 deletions(-) -- 2.46.2 diff --git a/include/linux/pid.h b/include/linux/pid.h index 68b02eab7509..7c9ed1b5d16f 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_* to refer to the current + * thread or 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..f4db20d76f4b 100644 --- a/include/uapi/linux/pidfd.h +++ b/include/uapi/linux/pidfd.h @@ -29,4 +29,19 @@ #define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9) #define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10) +/* + * Special sentinel values which can be used to refer to the current thread or + * thread group leader (which from a userland perspective is the process). + */ +#define PIDFD_SELF PIDFD_SELF_THREAD +#define PIDFD_SELF_PROCESS PIDFD_SELF_THREAD_GROUP + +#define PIDFD_SELF_THREAD -100 /* Current thread. */ +#define PIDFD_SELF_THREAD_GROUP -200 /* Current thread group leader. */ + +static inline bool pidfd_is_self_sentinel(pid_t pid) +{ + return pid == PIDFD_SELF_THREAD || pid == PIDFD_SELF_THREAD_GROUP; +} + #endif /* _UAPI_LINUX_PIDFD_H */ diff --git a/kernel/exit.c b/kernel/exit.c index 619f0014c33b..3eb20f8252ee 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 && !pidfd_is_self_sentinel(upid)) return -EINVAL; pid = pidfd_get_pid(upid, &f_flags); diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c index dc952c3b05af..d239f7eeaa1f 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 25cc1c36a1b1..0f8943ecc471 100644 --- a/kernel/pid.c +++ b/kernel/pid.c @@ -539,22 +539,31 @@ struct pid *__pidfd_get_pid(unsigned int pidfd, bool pin_pid, bool allow_proc, unsigned int *flags, struct fd *fd) { - struct file *file; + struct file *file = NULL; struct pid *pid; - struct fd 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; + unsigned int f_flags = 0; + struct fd f = {}; + + if (pidfd == PIDFD_SELF_THREAD) { + pid = *task_pid_ptr(current, PIDTYPE_PID); + f_flags = PIDFD_THREAD; + } else if (pidfd == PIDFD_SELF_THREAD_GROUP) { + 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) @@ -562,18 +571,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 Thu Oct 10 18:15:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13830645 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 F2F4C1CF2A6; Thu, 10 Oct 2024 18:16:16 +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=1728584178; cv=fail; b=tXZsVfHwPYMhGI3J6KSx6k5Aquiq5I+eKGMdlBHibQipb1mJuV5I7quhPsZ4QnkDj62dbnOMC8R9kKzbOnp/sAbAgVde3jBCp8UjArC1etagNWEItE5Ztq7qahVeYx0O+KJnyw6cvdvz5LBVWJgWAJx4MnbAzI7X0mS2OlB1Zis= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728584178; c=relaxed/simple; bh=b4YeloLLv2ivS4l0WI4P7BQ4ZHCLYZ2BMz1SoAHMszg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=tTI+rO159U2p49QgrNelfVXVMEcixiPsqG3dtMj9lh9F0TLpmdhOqYWuCh4qObFQTeVUI0fOSXojqGnsdwvQLtG22ne0MMeFLxZhZl2JFAwSUIm1OQvV33gx39vGoIhLQRF0Pf39IqBaCPy6eUX1Xo8CsNAOou5+a3rzJjgfeCI= 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=bzzjo22f; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=0A7uzA93; 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="bzzjo22f"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="0A7uzA93" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49AHteBk008811; Thu, 10 Oct 2024 18:16:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=6KeT6i8tSvadI47e3vGCkdV91NduoC+jrAaJ8w45sK4=; b= bzzjo22fcrPELoKIfCRPCd8QAClNSxcm1WrwRPaXk9TW6cY5bYK8ABP1CE9xSOlf UbP99R74l2VnQ0ZbFMLPPMMz50VNogapsxy11gV0tAaplkIiqM2Srq89E1tt/fdW EzGPR5xaoIrRrzsNy/6s+84txdpUPNFA/KdxnwvaErG9q7fmzQTPkBNCbyFTh3HK JjP2YvMO/IyXcJC53YiyHGt/91sLhleBl7qViUstKuPiMdTdEYqB5zvFamN7VDlv lCZOfUgvLQpUheIBt6Cxct1E6wPbgKQi2aIKZ/V6kANJXzIA0wJJpjBu+DccaqSA KCV7etwRaNQjti756Meqwg== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42303ykdg3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2024 18:16:01 +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 49AHG4jQ017547; Thu, 10 Oct 2024 18:15:59 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2049.outbound.protection.outlook.com [104.47.58.49]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 422uwag77j-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Oct 2024 18:15:59 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=t0L80BylXFRn28bcjsHwhRu9w/9R3JXRprm73lS5BlQj2WWf5g1GKYK9Bha/ruAzuaz8GKtZwcNvfPMWNIYv5JJfM3ytN6fcKKMeW/qp/vb0MdRWs68kcQOzuTrBqQ62vb+NYD/gLQBP5c/Yef5Ac0pdVmIg/Ct1um1mGVtYT2jabx0GTlchdOwxWgDpFC0y0lTo9w+myS9MlCziMqMnQuvEnOzD4jgOsTzQ3NQOgq1i59Xyz8LIefZC+1WltXNlUKJphoESUs2Z3UqvLG5ucbfl/JryrR2mjRxJ898lAQb0ErFBUPQkwk046JYUdTwrAd8wlsUuTdtDgRm/Tx5+0g== 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=6KeT6i8tSvadI47e3vGCkdV91NduoC+jrAaJ8w45sK4=; b=EfD2tO2aiplInii5tiQSAEAd2gbs/Lh9oux5gpID01mw2w1Nw7L6H50Y1Mg+rYu4DrBYpf+RQy/9SJGH8Dqoa67GgiLYHUfF1M2E5F9rigOv7gLYKcBlcUVeJBkDcIs0bFkOVofKcqTBIpx0c6nNdXgbD43Q2hou7BansrfPi68I4sJnMMHpNvq5+BRISPHN/sWl5NZdTWflEJ8SafEvrVf4U6zz5ODao2P4aAdB6y2dawCygUE6VYNI/fGCSzNvj0/UCCU1+ROavZNvQMXdc3uQzbqEgVFHqif3sSoXyMU8zxIOOd7W708F4IHLEqMKT4xzUM6dQlz9gZTOdZ9kuw== 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=6KeT6i8tSvadI47e3vGCkdV91NduoC+jrAaJ8w45sK4=; b=0A7uzA93YAFAZhDos8CxKpDnCfBZEUyKBmN8lOVGolkYnUpd/s3L2L937v9Jfpv+FZ1Xkb8ZnCPE6HsrvOIZRZeim7Spl7J0voKuvQJF+f9LJTcF0iFBzuH1FrZHf3h7VOJVVUn/oYavWwYjABW9INNCL7kVKh4SNIueK3dH3HE= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by SJ2PR10MB7826.namprd10.prod.outlook.com (2603:10b6:a03:56b::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.18; Thu, 10 Oct 2024 18:15:57 +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.8048.017; Thu, 10 Oct 2024 18:15:57 +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: [PATCH 3/3] selftests: pidfd: add tests for PIDFD_SELF_* Date: Thu, 10 Oct 2024 19:15:45 +0100 Message-ID: <8917d809e1509c4e0bce02436a493db29e2115b3.1728578231.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0374.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18e::19) 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_|SJ2PR10MB7826:EE_ X-MS-Office365-Filtering-Correlation-Id: 3b5689e1-eebf-4793-fe98-08dce957962e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: XA4/eo42Ihn2u6uAUycXFriAdpxUBTYQR8v7t8ze+ka0cUHhn+V6ovuqmwwAubJkJfqoeMMtB6s1CK9bzbPoS3R/eEG7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wsTjpWyn2cqA7mytDUjNSkOasBuXroH/T2d611kKUdkzgFqMicRgPVxC19XXht0Wu4L7dYyclvotUk+/PFaomKLsB4qRJtk/RIZXoHr+GBLZdc1/U 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)(376014)(7416014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pZ/9TgNW4Kby/JtQAWdROsI1nfK1BIE3Etdg8qa776xMoLnmhtGtRwXhn244p86gA6yl0sLuapgS/WrNNqblR9Pom22xMUXCIdegFwqYiip4FnPh9UuwvBaQGhjuhK8SBPYhK8WrRCHyzdAg4dYD8S56zGOiOjJtd2IQ+1o8xBCrwXySFpC8tLJpV+y9V22jg2aYReH8/tAyq7Dufh2Tr1MaZ5caWjsyKM3GwTCgTqAiqiuLLVM2pbotpgc0eDf6McXxBXtS22ZPRrfXutc6AjM1nIVYgolvmk1Fen0V3hkIiWcoGipOszef48zmIyusSEqFKCtLz+pfIEu2DSiyUAd9khMTIdiWVo6xAY4kHbrcC0AqWJJAYE/cfx9HK5VR5fxDPK0vdv9/24LFtbwrr9KqwZBo2ZuLJUonCw+yxWjk+aDRag/+IAfF0tA/p6sf2OmXLNrl167yyCV4RWfzlb9yz9ATTScgT/ZwX8hwQ5IiuBjGb1hk088Ot9Q5XnN0JGXZa+3t3UUSlOahQBfG4LUax0CAXrHEQCyDylCY0QnEzZ6hlo9xv6uPlxhuFe9NyFsVsXa1DwA4sz6ncQNlgE1FTzEZ7NvQ24Pzya5/80s3kpATNSNzDkQLvpy+swzbkmz6E8/1BdAZRu5MMHZNZ6RpNyTIu44LEJ7tfNQ8NYbWE2Xvk1HInaoS3QSvbW8fpsmPrIkrbQRtbu7io9hpDt05aOZ3tQrfsxBHAk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VF3K5tHX/4h0JpBUE/TTYLJ0MkJlr84DybnX7eM5rLVv8ygtkQ7eCYWYVT4u/Rm03Sb07rhCFkUai+Goa0qek+h1IdASodF54mnwxlVCOV4SoJqhptCSSb4VwLDMmovESG3S/DzZw+nmNXFfREtKl1dMP7AZMff8eqJOLI/tfXgIm6fQZxPqPoVyVWVGbUXaLm1RAURYVHtH+Ez03vpSQKLD147Ctr2TMpkgMESP0mZIzxncTQ8tgvGOer+kQaJaQwVsECur+sK2yenPL4N3C41IY8IuxG2G5Bp1NjlY0WGvoAniUiedEJrcuVvkFMwa9MKz9PIVPW7/ShleiShDmRGTXHTEcHWGYkJA2RpVRB38ycIfQJwAC+fRxlevzZ3Lo3fewfI0JgI7+tHKayfYimR9zTW4jNgh7yqhjsOnyGzEIxn17mf0pOHud8kbxlPir24zdc91KOhX1YbCXRyrP8O9KGcxk511uqJDgu/GGn/OcpL6g8CsxVr4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b5689e1-eebf-4793-fe98-08dce957962e X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Oct 2024 18:15:57.3290 (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: 9a+212iPYmLMB6RLCo7n9NvR4Tu5FZw/bLMbSaoueVNSnxUbWZHDEFimYbKUi/t6WdKOhG4a8p5Hm730ToYMiAQcCyk7xuUpKeHYnb4yen8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR10MB7826 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-10_13,2024-10-10_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 malwarescore=0 suspectscore=0 mlxscore=0 phishscore=0 adultscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410100120 X-Proofpoint-ORIG-GUID: aUz6TG5t2AsyeymEqq-se1F8tLCe3v-S X-Proofpoint-GUID: aUz6TG5t2AsyeymEqq-se1F8tLCe3v-S Add tests to assert that PIDFD_SELF_* correctly refers to the current thread and 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 | 8 ++ .../selftests/pidfd/pidfd_getfd_test.c | 136 ++++++++++++++++++ .../selftests/pidfd/pidfd_setns_test.c | 11 ++ tools/testing/selftests/pidfd/pidfd_test.c | 67 +++++++-- 4 files changed, 213 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h index 88d6830ee004..1640b711889b 100644 --- a/tools/testing/selftests/pidfd/pidfd.h +++ b/tools/testing/selftests/pidfd/pidfd.h @@ -50,6 +50,14 @@ #define PIDFD_NONBLOCK O_NONBLOCK #endif +/* System header file may not have this available. */ +#ifndef PIDFD_SELF_THREAD +#define PIDFD_SELF_THREAD -100 +#endif +#ifndef PIDFD_SELF_THREAD_GROUP +#define PIDFD_SELF_THREAD_GROUP -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..10793fc845ed 100644 --- a/tools/testing/selftests/pidfd/pidfd_getfd_test.c +++ b/tools/testing/selftests/pidfd/pidfd_getfd_test.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -15,6 +16,7 @@ #include #include #include +#include #include #include @@ -114,6 +116,89 @@ static int child(int sk) return ret; } +static int __pidfd_self_thread_worker(unsigned long page_size) +{ + int memfd; + int newfd; + char *ptr; + int ret = 0; + + /* + * Unshare our FDs so we have our own set. This means + * PIDFD_SELF_THREAD_GROUP will fail. + */ + if (unshare(CLONE_FILES) < 0) { + ret = -errno; + goto exit; + } + + /* Truncate, map in and write to our memfd. */ + memfd = sys_memfd_create("test_self_child", 0); + if (ftruncate(memfd, page_size)) { + ret = -errno; + goto exit; + } + + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_SHARED, memfd, 0); + if (ptr == MAP_FAILED) { + ret = -errno; + goto exit; + } + ptr[0] = 'y'; + if (munmap(ptr, page_size)) { + ret = -errno; + goto exit; + } + + /* Get a thread-local duplicate of our memfd. */ + newfd = sys_pidfd_getfd(PIDFD_SELF_THREAD, memfd, 0); + if (newfd < 0) { + ret = -errno; + goto exit; + } + + if (memfd == newfd) { + ret = -EINVAL; + goto exit; + } + + /* Map in new fd and make sure that the data is as expected. */ + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_SHARED, newfd, 0); + if (ptr == MAP_FAILED) { + ret = -errno; + goto exit; + } + + if (ptr[0] != 'y') { + ret = -EINVAL; + goto exit; + } + + if (munmap(ptr, page_size)) { + ret = -errno; + goto exit; + } + +exit: + /* Cleanup. */ + close(newfd); + close(memfd); + + return ret; +} + +static void *pidfd_self_thread_worker(void *arg) +{ + unsigned long page_size = (unsigned long)arg; + int ret; + + ret = __pidfd_self_thread_worker(page_size); + + return (void *)(intptr_t)ret; +} + FIXTURE(child) { /* @@ -264,6 +349,57 @@ TEST_F(child, no_strange_EBADF) EXPECT_EQ(errno, ESRCH); } +TEST(pidfd_self) +{ + int memfd = sys_memfd_create("test_self", 0); + unsigned long page_size = sysconf(_SC_PAGESIZE); + int newfd; + char *ptr; + pthread_t thread; + void *res; + int err; + + 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_THREAD_GROUP, 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); + + /* + * Fire up the thread and assert that we can lookup the thread-specific + * PIDFD_SELF_THREAD (also aliased by PIDFD_SELF). + */ + ASSERT_EQ(pthread_create(&thread, NULL, pidfd_self_thread_worker, + (void *)page_size), 0); + ASSERT_EQ(pthread_join(thread, &res), 0); + err = (int)(intptr_t)res; + + ASSERT_EQ(err, 0); +} + #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..bbd39dc5ceb7 100644 --- a/tools/testing/selftests/pidfd/pidfd_setns_test.c +++ b/tools/testing/selftests/pidfd/pidfd_setns_test.c @@ -752,4 +752,15 @@ TEST(setns_einval) close(fd); } +TEST(setns_pidfd_self_disallowed) +{ + ASSERT_EQ(setns(PIDFD_SELF_THREAD, 0), -1); + EXPECT_EQ(errno, EBADF); + + errno = 0; + + ASSERT_EQ(setns(PIDFD_SELF_THREAD_GROUP, 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..ab5caa0368a1 100644 --- a/tools/testing/selftests/pidfd/pidfd_test.c +++ b/tools/testing/selftests/pidfd/pidfd_test.c @@ -42,12 +42,41 @@ static pid_t pidfd_clone(int flags, int *pidfd, int (*fn)(void *)) #endif } -static int signal_received; +static pthread_t signal_received; static void set_signal_received_on_sigusr1(int sig) { if (sig == SIGUSR1) - signal_received = 1; + signal_received = pthread_self(); +} + +static int send_signal(int pidfd) +{ + int ret = 0; + + if (sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0) < 0) { + ret = -EINVAL; + goto exit; + } + + if (signal_received != pthread_self()) { + ret = -EINVAL; + goto exit; + } + +exit: + signal_received = 0; + return ret; +} + +static void *send_signal_worker(void *arg) +{ + int pidfd = (int)(intptr_t)arg; + int ret; + + ret = send_signal(pidfd); + + return (void *)(intptr_t)ret; } /* @@ -56,8 +85,11 @@ static void set_signal_received_on_sigusr1(int sig) */ static int test_pidfd_send_signal_simple_success(void) { - int pidfd, ret; + int pidfd; const char *test_name = "pidfd_send_signal send SIGUSR1"; + pthread_t thread; + void *thread_res; + int res; if (!have_pidfd_send_signal) { ksft_test_result_skip( @@ -74,17 +106,34 @@ static int test_pidfd_send_signal_simple_success(void) signal(SIGUSR1, set_signal_received_on_sigusr1); - ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0); + send_signal(pidfd); close(pidfd); - if (ret < 0) - ksft_exit_fail_msg("%s test: Failed to send signal\n", + + /* Now try the same thing only using PIDFD_SELF_THREAD_GROUP. */ + res = send_signal(PIDFD_SELF_THREAD_GROUP); + if (res) + ksft_exit_fail_msg( + "%s test: Error %d on PIDFD_SELF_THREAD_GROUP signal\n", + test_name, res); + + /* + * Now try the same thing in a thread and assert thread ID is equal to + * worker thread ID. + */ + if (pthread_create(&thread, NULL, send_signal_worker, + (void *)(intptr_t)PIDFD_SELF_THREAD)) + ksft_exit_fail_msg("%s test: Failed to create thread\n", test_name); - if (signal_received != 1) - ksft_exit_fail_msg("%s test: Failed to receive signal\n", + if (pthread_join(thread, &thread_res)) + ksft_exit_fail_msg("%s test: Failed to join thread\n", test_name); + res = (int)(intptr_t)thread_res; + if (res) + ksft_exit_fail_msg( + "%s test: Error %d on PIDFD_SELF_THREAD signal\n", + test_name, res); - signal_received = 0; ksft_test_result_pass("%s test: Sent signal\n", test_name); return 0; }