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: 834341 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-AntiSp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OoY/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: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: 834340 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/eEG7Q1GEdn5OjshffhCCQscSKeS5r0GpQoMyBpwXAmRfCE8+jLxnpS2E2hQlAV/SidYg7EueQG/b+TpuQHk9oSxJ5kUikwyrVK6gHzI3SC9rCZ65vsmCKOz3SIZ1rbE6Bw737zEMDegz8/lwKL48qleKX8AsyH5BBeUV1rGZwcnbcmj9Ry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PM; 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/1BdAZRu5MMHZNZ6RpNyTIu44LEJ7tfNQ8NYbWE2Xvk1HInaoS3QSvbW8fpsmPrIkrbQRtbu7io9hpDt05aOZ3tQrfsxBHAkohN5rX31jp67+sS2YA+17HdtE7UmJMBaReSxm4e+uvpz9iyGkDSzlqXiNYmrtsgyZhybb4UnA29RrBkjVl64vil2rdqpZ8N/0htXOwmd0MCqPTT4BZ2mm10dYTJk4vEexVOHjYDC6yJpw1gIs0t6vs937U2PhlmNGkjasYvsm7js8JL6Bwjo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xPqPoVyVWVGbUXaLm1RAURYVHtH+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; }