From patchwork Wed Oct 16 10:20:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13838123 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 1CEE9206E9D; Wed, 16 Oct 2024 10:21:02 +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=1729074064; cv=fail; b=FFvn09VsN8g7lwzrPQCHEDBbJ+EejoB67Oj1WtUGGmPBM3QHzWJFKae0JUPHM7o7TPj1Wezs5Qk6Po0p9MrhWyt1QWrRprQl0T45iHy1408gXsC+ZOWZ+YjcUBKzdNJLBkQCt7jOuYAqhYBdF/Ib1X4WmedRbkJa6Zz4sdoMMp4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729074064; c=relaxed/simple; bh=sHG+NL6SiICmhySDopX0nirslObGKD3KKnsfmat1bzs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=sRb+GTipMrYc6xEY0TFTUt9e/VMTn9KNX1G48BNBjD2YAjN2wY2aGr4piGIIziwUnvq/AnCA/NviK1K9KIAlr/2inWHf0OC7kM1K9ooSmxTPrBSFJ5+lkb4wFM++kBzxMfxeu0lhPU0VXhVFQBQszjLptGOZftKB67F/0PotjDo= 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=KxKoo3cS; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=K26Mndjd; 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="KxKoo3cS"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="K26Mndjd" Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49G9tdpR001094; Wed, 16 Oct 2024 10:20:51 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=JHJgtDsWIsDsEYWeq5BH9WNV9u/wLw2axBkIj7l9KrU=; b= KxKoo3cSivDumOMk1/twXMM6lUKRh/h5XEesTLxhYjDpjg1O+agHRvRj5NCMfm/h 9X3ln9XQHhFKJmheqE1BENB+OJw63+DnujiPfvUm1j4hjyqYuhtdAd5zVzQohwha OitD8pUC6THcuOsL9761B/PqxS0V1nfdGpyHdrl2zdVeCDGDc1rqYPeBv/wDR3vV Bvp4w0yGShPOpWli0mOW9Y1u2V+XUtBLhLOiGRNMhs7sQNnvMpqy5TtKipGH4+7w OJga0+LIJL895/Fu80rZ9OFeF8Jh93IPOsoQWr2ZvvVy3ju4PNB1ohekFXi1OOWK MquTTEBGJohGXf/9W1GaoQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 427h5ckqms-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 16 Oct 2024 10:20:50 +0000 (GMT) Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49G8hKuJ019983; Wed, 16 Oct 2024 10:20:49 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam02lp2048.outbound.protection.outlook.com [104.47.56.48]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 427fj8naje-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 16 Oct 2024 10:20:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=p+JYv9guearlTL7SVwq+v5/g2KKRCyuC9DswEnIJfWrX2OVd87hqELPX9eZgc9dJtaydMX0kGhBFyKDWakHzwUyTiAEA00Yz/EJhuxMQnC98mDpCht+YcIwnaDrMOd/3qF1t/jhK+bZ3S7ujqyOtcInXdEsoC0sWAsAyivnhI3ObxikT1kxNcFrE9diFCrnMrEn2AaD95zwSVc1qh/Cjx15QVPqWSHWCDyWD1pdYdQ3abIz+A1BMINlOwmceFZ+W7FpLg9fLRMGelTZKdr0DCaL907A4KX3cXqtvSKha3ardBZo/1tpCs3x9VkDILTpL/WTi6tXq/nSPQdH0o3gHYQ== 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=JHJgtDsWIsDsEYWeq5BH9WNV9u/wLw2axBkIj7l9KrU=; b=Vukgd+/p437s3+d3TQCYVexjCGgw/L/UJOiEkC8zKYXp6WWDq7JZxEQkOJ9dp7pt9YxcGsEx4I1TwUarbGni0Dl/58vP8CtUuYEk/fVrRQegTQItcP6TzNdtiXGMcnp3AVEUv0rUDAHscEIjcZC8/mp8Kqgqbk9fTEvJnBZjPpXiqukoR1cmTUZZEbXz9e7IlYI6ZMhe0mzCYG22a5jIa7Cd5EAMqmSdfxRnCG6DaEAd2lUv8mRyz6n7bOsgV5L1o5WLHyBIZLlD/FN/+Ea/3WjtYR5pudM24lftqCJxP8CnY1EndDoMpbbbJllISuic76qDnj778jCArsaaFJwWYQ== 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=JHJgtDsWIsDsEYWeq5BH9WNV9u/wLw2axBkIj7l9KrU=; b=K26Mndjd5pXcZSwcsIYsrE1HUSRQbNRj+AwR6HHMAPwvAG2NpEQe7bva5FQCybGVkd90VZRTCcHkWyLl1H/hLtUe7GI3KlI+11ahIzvl44NKiNX/fApkPDQoyFUtRRMpZ1n5LslnDWWOwvQ3nyNS6WhnzuLwQ0KZKJbJxYZlcAk= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by IA1PR10MB6172.namprd10.prod.outlook.com (2603:10b6:208:3a4::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.17; Wed, 16 Oct 2024 10:20: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.8069.016; Wed, 16 Oct 2024 10:20: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, Oliver Sang Subject: [PATCH v3 1/3] pidfd: extend pidfd_get_pid() and de-duplicate pid lookup Date: Wed, 16 Oct 2024 11:20:37 +0100 Message-ID: <308d25458297a859e514132dfbe5c28a338ba63d.1729073310.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0113.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:192::10) 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_|IA1PR10MB6172:EE_ X-MS-Office365-Filtering-Correlation-Id: c597b00e-a428-4853-1af1-08dcedcc331a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|10070799003|1800799024; X-Microsoft-Antispam-Message-Info: ATKJ3BIUWRe7MMAq+ZtmnrsQ0LJdLUX88zBAHoOlaZe9+5/MKTa8v4y3jWe45CSfTp+UgzuXJRp90iYubbb4mNkRt3HLXpIl/mqm2MYJzwr8mchuRcmT33QRb0fX5r/umXbSxBY1zrlNzsr0DBmgj51opIQUH4UEQwinogSQbsQwDVBR1oCS+aPKP7TgdeEfWzgSkSwaT5d6WOL9tidUGA00ONdpEDL3R06BvAbKLF55wuzGsuLqf2DsYMGOGPZgxRKYR+iISqFHqx4PAt0Azxhpr3vQaLdgww7rP2m80D+LXodclfQCEh0I64IIL1tm+T1di73174XMkMr45iWeY0UZYVTdRbySqWyXfBTDw+H88JIgeesvwNLhAwoE7Iy6hFv0q+/G+R59G5QrkET9bLxDJLf+H1GIcmvxErhsQSvwHfGSME61rBPbCxFSyix3d1l2gGPmKU7iDb5Gw3RUl5k4NSBRP/t6sNs8l/PVShyxUQC9RS+eQ9Mu8BAODomC/qd9Vba9aJRGsh8BngkmDwl8iOMYe8gjQLd01ulkwuMTfssSjTOBl0oEPmUkdE6r3tqtcMqYFKhmItANAyfxGTmjqL3BjP6VscXpVPa++dCxdPwYFONf4B+bXjyvMygO03scDbxGeY7ipj2sMIUwQFCD08MyXLInM+sfNJm0ru2WGV+9qzNSghBdcl/Zy6zPMJmV5aaw7NdTL0vtVKeVOT3Fa9lTaL5gVA2BIICCq00fF0JRpXz6TNp+RR1/6nyt5XXDMiAOO07z6r+UWLZwR53qgcj2YKAXb5Md9BDQ6VA2FFEW+kl5HqVMe2KaoL8opssMwcbJJQIv8ILPGGlHK8jX4BmRmlzWmZHEAZD1S1Tc7r5V7vptFzFi2tbGDP1vpq6DHZ6hd8RDNvXSwOQnQJ2jvueVOxJ3DGdatoUgUa0Ir0/YnwNEX0kT6gFgC9MDEYtDErsm3V4YrExuxAwG75atcRirf36LCYE9fLF+K/ZdepVanN8U/UbGSxwZk5iJI1p01UkS9uJaeZoD0zloe9PgSMFEtE9UHSqPGZZinvpMDuLIzBcM8QALNnPUkXlIChvd4AsemBUffFxvdiE1vA8w6UQB1vQsHG7GRzqztyhzicgOIpX3mL8iHKP7gCPIeO7ldzzMvdSDtHR23efMxPZlbGcP9dDdxEpXGt258eN2xWjjfgdFiRmc9bgRkhRAmihzZKYwxjkIzgb9xUDXb6HvzxxIhbCKqhq4MPfHUgxaXYvrQnQhuzTXgxdJHEljB4aI5Mx9cLP8GpRurFKjXg== 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)(366016)(10070799003)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: awlMvH82YoA5zBti/3uL6PG+/j75pS2+0DVC31Hz6VXFFhyqhlO9o55W6M+2x4S5wSMOO0E2di/y6M5HfcyQklHdK88iaFOEyK8h4XWVksi8cczKsDVLEKUt5HBHL0YVbnABnPn3+9o/ZTXnFpnTW0FC2OwkM2FLZstCalJld6Es2FTZEE0Kz26rmUZYWqr0PvvdSjgGkDTtV6HtoACzqgRfeogKHimZANKfESDUDFNdF+CxtKrh8H3GExVUCbqwzf/JeqyPtH4+hD+1YinONYlGmfFUqBQogqxIbxJ9NRwMxq8GkZXuKf2C/FOai+BP6TcwjLirIrqPwZYgUerVuupCMipBYz8f0F6pRFabDPmjfE964zCYN86jmXJzsI6eCBkXz7aqupg26E1cX3DLpF6cMUV5ofgOEDSU/ONlFP/ShOoMQTtZmKQbR5hX25gOtGKZro18bOKhM7yP6BruZWsArR94T/igValEavAvZklukvbpScG+oGDXH0hQy5edcoz6MYO0ujeA28uP0orOtCeKfhRCAy7Usn3Vk4GUw5oCEseZqf/9EwJxMmoz/WL6O5UxucLqypQDNspeUg7TEbf69ytPDeAjyMK29dpLBwIkvYhv2UYIPp9b4tjFKTS+mEg/Ah22+Cnhf4x/Yq+ekLSWUdBUeycYOwIOIaqdIcBFWrLEyCURwCff8a9TOcLxwexo8gW+rYWtEGyvRQ2fxpLp8gBRNHb4SMvlrkOT/MOu3TvI+6wlJ9t9P/jsMlIxUKt1GW9BM4dianYiLNMw+3Vg3pvthBRo+o7tsg+ok77tGa2TL2T9iXRRzin+nOX1pvaQRa2eLoYiTF8/RQNvviFLTQFJ6LpT5SSa/SCurQEpzWe3pzDeTvugOmERt9Z1wVcoddd0EpNomSCU07kYrv9BHQFXR5a0DvvN5vyuvuivOvtnyVEMHkuYLg5AiNssgaLkuDD3Qnt1S212VuPW6eLvkusB1Bn2KVr/yv2Og2/pmh8jWym4yx5w5fCzXP0T/OBKHStMr6j8nJRkQNved5agwHsp0ELF4rd5AqfoQXG/lARhWCNsb6TOxs1XX+KJBd4KKpA+DWxSs/qGT/Gv47A3Q+TEEhLHRMtWivijbptoZ2Ct32Ll2F1uTnMiF0Y5KYHIeJjTVrMGxDOW7QX5dpTjVxB9KrtdqSizI5y9fUIh4AyUpp6oUSK/IkIyQX19fQhp2y25larD6s4zP1EQZlnscul42kffNvDCl6qBigGzfOGJi6rHU1GgKLFBECDFUQPt2Mxosnh2g68W/EnNbm8AFSRxhhNKtufVeNrUrK76IyM5QTO7isFrEQ3j873E8BDNZa+q/+VwBrDc4rK0bsVSwBW8HBkaJ/R23STkklaLrMHiCuimeCk27xdrKrC9yfMD4hS5eU6i6d/5cLHOLzc8i77qkKvgjJlPAPov/3M0RK4tN7yzFr9EWL2jGXairXkN1+BWAYrtn4TT27NPKJm1qJ5vm3yWCOuYCuahCtseQaUmOnZjv00vQty/kaDx9Q+osd8FZyWiXNwdb04hnHRIHx9BHGWn9HlssCOc71aYz0bkDD1xjd//9VbqPjvvlLHuFXoJLkZ4Qm+P9S9LRYeoGRke07Z6O/35cIDp7zM9dtYepR3Fo3OQxfa7AQOJdbOXNPbB+Bb4LXupcjE9AA== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: c748uBcxPmK4+ky1qNX4TfJFKW1YgsDo54FrLxbV1v3ke13zjsazE6Y7REO37lu6FLe2aHLNAKATLR5Aj0dwc3f3jCv0Lpcp2QLB1tdVC3kcANvguX2xxHVvNFNQ13reIikzQJylcLHeTHJXc4VLKrx/ZWaWMXve/XN77DQTRW+zjiZkSb1xhHqYoFUrlhrZxtvO2g9eVSrn2DRJVzumlE0HtpEhDfFPx2feiPeuw3CmK2ygOPq5i9AaZcU5US95jLahrHrlSU/YxC25mBdRuXSYr9WyiJEsdAsNVCvea32CkJhViuObHd13+aOv0EtCMnlM51dPPY+vhoauoFmechrIAr4u4DPYQygPHZJWbPfQyfGnuc2nW0wuWbGI5q5KOwYI3KfdOuhlcp3zKXdhiCUc8a1dJXgIIeSJdQ9GC1rRfWM5jv/j8JvUIhDqk8yUCt22dCfp4gv9dT0WDCU+yXkr7mjCYyZj1HSvQAlyH6Wuywhtx7O26c46qKf0avdrK1Wd9KFli+BRsp7ypwrAZHDowjmhXvaqFU35RLEBbj3drBrITEQOAKeCAo6ilutNpg84LiSw0sL04QgyicesFt40P596y0EWf++gakFxkOc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c597b00e-a428-4853-1af1-08dcedcc331a X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2024 10:20:46.6940 (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: CHLtQlUSDWpukzs9fGPZaEAgnHtBIyWmf01s3sF4ze0KmDbvvlR0MVpYvesiHTToupa4nc+ErDSZTLWCRvcCvU1gFZ49gH48H1d15lhnuwA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB6172 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-16_08,2024-10-15_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 mlxscore=0 phishscore=0 adultscore=0 bulkscore=0 suspectscore=0 spamscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410160064 X-Proofpoint-ORIG-GUID: Zjm7jSMIl3mRK6LCXShV_4m-KTrK6dDf X-Proofpoint-GUID: Zjm7jSMIl3mRK6LCXShV_4m-KTrK6dDf 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 | 26 ++++---------------- 3 files changed, 85 insertions(+), 41 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..e64da346a90b 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) @@ -3912,6 +3901,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) @@ -3921,16 +3911,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)) @@ -3939,7 +3923,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 Wed Oct 16 10:20:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13838124 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 D665F207209; Wed, 16 Oct 2024 10:21:06 +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=1729074069; cv=fail; b=t6P6Dzd7x/LIYllRnIc8kYA67XOT6tG+aqynmXZL8ukGqZaG5J1azeXZ0ieHEQF27vRR8kRYCebO7A36EBxkh5mjXGPGxrmlEZQ+c1JbsP7PJOaED5OasHb0HiyfUdm94yG/bHesXecxItZEs0KZdMCoDEMcHZKZ6fgFE2d4FgI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729074069; c=relaxed/simple; bh=GPlOhKIanY5W96v7RGBGpX8RJziFA1dUbaLUGsBPIm4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=ZoC9/rKvvNHTdNpZFUqeqiU8t8UfDJtozNQgEeeUpogpAyHoB45ZreQbIQmMH9UNTH7UkOFrWSf0KEF7LxeWaJJVOwXhhIY7zeanHlTWdNre7vvp6jXKp0aDCy5a+b8jnwEIt6Ll3stP/80h15zvu1dIx8wGf0RcoQ/AKdOD+V8= 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=MysW88e+; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=ufN+7XIc; 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="MysW88e+"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="ufN+7XIc" 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 49G9tbo1012919; Wed, 16 Oct 2024 10:20:53 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=8nWoDMl5VOvjzc4SgroRs1FM4zRRVAwaOsTS/I20V4M=; b= MysW88e+HNI+h6lG219aPoawrP4ud9/NiRLhCqSmqoGhWBAgViBdD1g09bSYCG8V N9hdIs6eRGGyBg78umVTUjGt/QucaK7Ge85VHqmGP2K53gtPp9Z3FZBqmUy39AvD +lXjmff1AcM9260Y4JVnN1z0zGEekSW9IrgzcnhV3z1ZPI226TSTtthoDQBbfazG hXghnVLfdy0wymXfTmzHiGqa7DC3RhnpTjCBGBUj2otoM+9vUNOJTLdbLx/3Gu5G IpzIo+s1JQ/cn41qBHX+EFsRf+AHjcQZ4ug/lgTZx/A2Yez00jgK1z+Mwi5vJijH Oqffg+p0mHsIikb6N2J2eg== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 427fw2kg7m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 16 Oct 2024 10:20:53 +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 49G8w0Zh026232; Wed, 16 Oct 2024 10:20:52 GMT Received: from nam02-dm3-obe.outbound.protection.outlook.com (mail-dm3nam02lp2049.outbound.protection.outlook.com [104.47.56.49]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 427fj8n3k8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 16 Oct 2024 10:20:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TvWVO2XSdGhB9JY1xd7yX6Wf+ix1sVKJc+seT+BtTpn2ggRn2GId9FgOunB9y7USg0CQX84876iebEMFtYjnio2y+vZPUTa1qGGvzd6kXQtY/Ei2H6GrYz5jt2o/FFnri751kZVo7cWtxlKo90c/6i6DpYD8QiQPu7v3xdSBw3B8U5CinS0btIB4xnI2kBc8Nmw9ffO/4prxIgyn/n1NLykbFExfzYY/NS8Rp5/NlXTlRwx4Ts6yC+3vrhz1VEqjyF5o7UH940+b0YhJsJwVcN47fkRX8o9Xi3eJs8xLRZfkhpC7nWZqZxd9bgN2mL3EIQvolSuGPMtuWR1hWm5qpg== 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=8nWoDMl5VOvjzc4SgroRs1FM4zRRVAwaOsTS/I20V4M=; b=nsUGb8+ubeYpuitSjliwc4VmOH40V3wGpGxf2qn/gTRyyjwBqEInU/ygf/mwoUMcWOrVA6DA8iZO/boQmbFm+R/Xi62f9ieBMnanrNEsiAr4E7TKzdywJEpqpQvYfnBtYHpuq0rkV0Bm0WbPOkEjsscgexLjhNSy31Ts50Fto/c2hDxMZ9xrSrr4TNyFnI/NYGODfxh8RnNXs2CvrrF4LepiW36cTfgl3Sk4QM/gxLD5x1Tl4AGIHZlXO+LiHSpYG52sgDXMaxK8a3j//lZI0tGCtquNZRo1dD928imGP7Q8rrxPUkC9/eqJpNj69BXk/lBJpKZRaYAAeYS98vuDXg== 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=8nWoDMl5VOvjzc4SgroRs1FM4zRRVAwaOsTS/I20V4M=; b=ufN+7XIcYMaIFn/s+HRG6pZxOVu+Fz35mxo0oXNJyOQrhdF0fGf+Y5C+pjoiaYBBNDY7wCBHjng3eOAZbWpw1IIOKPLU3XNjR2QXMFJlfg71fV/PtR+mwEYflhXZiJH3HsZjUpHRXTwtxWKoKvVlDA7g2v2DWui2KPl3EFx0h6s= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by IA1PR10MB6172.namprd10.prod.outlook.com (2603:10b6:208:3a4::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.17; Wed, 16 Oct 2024 10:20:49 +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.8069.016; Wed, 16 Oct 2024 10:20:49 +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, Oliver Sang Subject: [PATCH v3 2/3] pidfd: add PIDFD_SELF_* sentinels to refer to own thread/process Date: Wed, 16 Oct 2024 11:20:38 +0100 Message-ID: <92f2d057b2861b837f37fae31ccb1d64712539ef.1729073310.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0485.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:13a::10) 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_|IA1PR10MB6172:EE_ X-MS-Office365-Filtering-Correlation-Id: db10af17-3078-4c5e-d758-08dcedcc34ad X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|10070799003|1800799024; X-Microsoft-Antispam-Message-Info: 1LZeJ3GXQ7nTjt6PnuyCuOlsb+x1l+yLFkgX8kpPjAbH+wwG7Xp/8UC+1YYCmxWHOYeDj6XLSujuhRhFHvF+PTgc5Jxky8ACOB0fCdvP2s22woDaUVsTR5jiBLhbCeX+b9bo83qPGtQi0KS7E4QSHZwT5Y2h780MHNf07I0WPRLaczjT54MHVf7V3GmgOWu4CtgJlsEIc/Wy4jec8MjHQVbNtlPO2X4GF24xRjEqPyrbLEyTbMqTAT+6w4DEHGAE7VIQGZAZVHHx+vxp2yJSijxj3I9Rp2zEL5qhy5+3wokH2u/gceerqizb4LFahZJqSHN9jUcGwzs1C+cn21yU5lZDRFqinzt6burrIE3dOu1gAmQk8bY1y9/PDCaJKpsuD6fX2R2chtxeIEzAI5BZR8dfmcKlk/B4iIlkDpAZIiOFPGyc0oOIVmnaiFMPAN37+KwDUnRGWd9imaGR6I4Ke8wHMg4uTvKv/Gn2ymU/jNOJhrQ9GkOOFBQtUizPznBKCgk8TWSdiBiAJ3ZWyxufYd72mjnqmeYUzshijViQ5cay1R45rWf0suaY/b3yZv65RZPYWr6cmj80FL1TCpetg9PT4/iLptQ2O8DMpRatDsdq2hejc1c/Itw85anrl9Skfz4Lq6dRWkJt53Co/YSqqQioe9EzRfGy1rOmkVrYcPEkxuLthLqeqz/aUd1O7mpSFMxcnHEl725X1wakBEReOxAmTllpqptJgUqKDYW1zFdmpQkKq38XiOJypKIZ4El2aPnW0ZaNEpKwbJ83uINjQcFU5sco0yNIZnbH3QtYYFouBkJ4TAfr6FAg96nFZwuaEIlEFLNhPMVzibMcWurtGGek4CsgfE/3F5KrGOlzVrRpJdB1DF3fiByVtpEAIV6ORxg8vvpdM+wuc1pc01+Nc6PmWqrLI2SxGBqYDfnxy0zqT79G5nEafip5A9b2WqBtbYT6TkM2BlnbomQYBZh0WZffHbL3NCqr5+p2eo3x2ZkGQM/vuUGfQxLcr5wGaRBF80HQaiGIlkorSPiClRqAlT3bFosb3UknubvJJj5xI3ByvsX56PnvSWX0OWeTrWbiGBdAEYHPdjoEO78c7kTaSZw4U4ICrHaoC0hglhQNvJDK1vehQGapNc9M/OrBM0HsLLkydBRoYRY8cbO+QjGmE9gUlaFMVcQfYhYjcr6ElbuPJJwMyINx67BxK6/6WYrzhTZO3jgT9j3itXdbzeYqeaDpFr39L0EnQU7zNAb8iVOZan8jYx8evdVlSW1yMZhZKB8BRDACqwLW4ASOpb83sQ== 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)(366016)(10070799003)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: EyRGdDa9RdSc4LJvWDmoA36aRU7S1sWVlrYm85z6Yw+P++oOPAERsNJskEeioWZHb9n4lls9oS5zmsMQOi4OCCLL+arnvAPHPMXVvGWZ0aglgNtPp4Ny4Fu5vavvXPEiy5zh0BAuNv19aJq5kHtdUQ78xOE0hrso509OVjtu36dKdzRdKVnZcQZ7dNt6ge5gZEX1ri8Zxp/xo2J1leT6frT7zg918lN919Z2rALAzAtaq3NA7j4tkpJcdv2CnngAfCfXDfLHY2kYD0CihbCJbKzP/ooPKVQM2Ixd6B+XH7dnwnuFqhSnZzRHKmoZEpa3EBze7/0nc/1buhUKzfY2I+Yjk4/XMA9f0TApez8U+0aHnTSbXuSnNr7rydt1mLo2A2m4q9WjIurtgszq2r2TgDGyfpZgydvEkpbw6aRgnBBBZymWKCd0FGB/v95vw2LOARGjGFft32kCJ0PEn8RV6H0lOAiBvsHwRmPd5zCywOuAqAFZ1P8MtDP6e8hpTsmNn6DEfMk19ijkdDBAH/rynz77Rg8FBYoeXufrDBSy0C5ilxxKMVyA7U9y3S3BTjYwkWd5bexnF1RkPymab2umLPX7wCEjK+tT4a8I57btIyaqtsFAy6z9CuhAv6Wz0euaauejV3n3h619BeaO1gO+7/P2kGxuKodudi+IeoO39h8S1cWJK5nrGuXYgKxkGOswM6rwN5nDhckWHp5oSiY2fVRENqCmpqCb1w1gN3xokNS1rdDO3pM4g4Cbhu4XFXPGoxjBa680TU4SLqEi15uvgU34o9tWjaftj3dcLOu1YtxIzuyb/FAL3LPMFzcowzqPah92kRs1a8LGNvCCET6KLv+BnwWyTgWm6fBXg9hy6UNuCiWmFDk33NFNRgV4k2VwSERtSJM7QG6Hpo1IPHKmL/QMRnQsmvDncJzV/juskZqkBzLlDXgaAJfDZr2ilJSmnlyu3pExQ96nCwfMr0GffPmDnz3kpdHeGqenQlJEX68WMdk2Eds8iPBSr3QHjE/nYM4rFD5ngMjag0Gm3f0Vu6WkWIQNJCt+VrK5F/Lz2eHn/mWkBOrpM3aAGJ+TITPgSpBuddjSES48Yvv+sJEDSAJGDjFC86DNAb6nt9HqoxnPR+6UkIlirJCH/+qi+DKTJqcMchwc+9U5w1RAX9goixvhkR5OQnqI+uY+mvT/mrpj0hpIvIuAll0LDhbNteJSFlw9Kw1cOwduIacNWcW5vz5uB4lpqnvLuwrA7At4akrMCGUJsw9wSalQoTGFdALo0jvmxKujE9J7RQ5b3Nt0XedG52q+RQzSBHZ0aiAXOAR4vFSuilZe321Ab8hClmGMdmlz0LmBE6GsQJ4z/wfcmTs5G+zqM3v+axN+ZUEFh5pkRoE1luzu55enSVxivCm4JmHZyRAFAcofR1ZCuJi/11l5NI4YN2mHIoN8+GzzcI6L+XFlT71X3vJmr2Hfmwxl+iMrtyPiOlZJN4lIZgb3DXiWElLZ5pOI6gfc7+w/Tj2ntp9ErtGysYPC9BnpcOgIDkgzczYxJk3N1JbswVqjzKjuOZHH+shlwxqQecAksZJ8EYKsT3aOS7RryrpClqFj+6atSeR8BYrLisFe0CuSZw+32IucMzmXaIY89/vmojknGPVtDzLqyuLmDwItlwrQAxbHRe5wFjfCEbUgFJpbhw== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: NFW9RW06UW2breua/BD1wNbj7b7yaRdUf+5ge/Fc3Eq3pTMaWZvdAMwZEQMDESBG/y+FK+tdUoNwpDAFFudmmK2JgMiMjlJFvoaxuuAqlwvY0c3ytSoPrUhKXGP5ZUXFXKkBLcF0OTHWcjnOB5j1/DRF1KCbweVL6oKTLnAB5MICu++BbP2n567Kt3jbFbUJKjrEgC+CzE4wb5lEORva7z6WQj+R92RjmrLdPprKWYMYpOkA2+iSAzXpnmhT1hxaLylI+btGYZIWpp+qAIpadcJv42b8VvmGEkPFm9FmTY+O2snh48ccGQTUINGYYn0dYOmUjLM6UwHqq2j5SbhC7iIfF0u8ptxr4gXLyy8TLXVQf0Aw1vSEFy9Mu9WGzdiuNNBee2r86OV9lS7A5YMNhqioehXEsA3nO3Jat+NesmInndf3jsx1JUp6w0oWO1+yphx+FJxOmH+Q+YZShW1l28h+p5ASJ5GCNWXzbtbI5BpI33e/BD1MfDHG77QUUe8b2UmRgfj47NyZvjld+5zlWr1MheL1q0oFIeIHCXMgUVVeXRK7XvF84SsaHgkB/AqMQPv4Vy4L20Tz6NeKVU3TD5jcEwIy+ZaNpxVAA5ICDJQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: db10af17-3078-4c5e-d758-08dcedcc34ad X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2024 10:20:49.3393 (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: vGsuK6CknPrB13Hb9yLo2clemJsGU8Bni8w69SsK/wQpCm2BT7Se3A6glzbzAe+0cBGZLDFIfUgBiMABLIkjv6Ia7/uT1gArdC/TM/TzH64= X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB6172 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-16_08,2024-10-15_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 adultscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410160064 X-Proofpoint-GUID: trbmcBb_Qmwwh1SdZsMn3avQT4-amXZx X-Proofpoint-ORIG-GUID: trbmcBb_Qmwwh1SdZsMn3avQT4-amXZx 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(-) 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 Wed Oct 16 10:20:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13838125 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 7D1CD206E87; Wed, 16 Oct 2024 10:21:08 +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=1729074070; cv=fail; b=KXtWvCvdoFj9nhFJVGstr7DE9lgceGQacxVb/dp5QqQqK2cYI8ypA3bpZCs7VOz6uQzZo5/pa2nDX2ijrBiUgsmyRMaBYpqAzzjW/6/UZlGUpTr0bIADgONmh0lOGU2SVbY7WQiJlmZZ+743z8Ap6znGtV+dr4O8/CPElPodu50= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729074070; c=relaxed/simple; bh=crBvDB53mKcdWE/b14vL7ki8GPoLhouRy/fOrmcCw90=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=PrVWzlrA6loMKEyywQnkrH6uj19mqHS7zc9fEi6iuPoRuVmQgCRDsGQju7NTvQhcxkdulYHXjaSYnK51ofODoCY7rPXow3Leo1GryASSSCgdl1+3YvnXLoPgtuLsxVGNUgxW7g7yKcDdfZkwM1y4GWvQp8xcfrZVJskA9HyS6iA= 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=Zn/X7IVk; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=Xv0TLHqu; 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="Zn/X7IVk"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="Xv0TLHqu" Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49G9tjaR022244; Wed, 16 Oct 2024 10:20:56 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=YXP4oBBbOFFH5eJ5/mE7kNI6rApLZ6pu5Gss4+TGeF8=; b= Zn/X7IVkbdv2SjFSFumlCBhE8eISKx5Ae3Hx5X0Ugz1jY/JA0n711JICv/MUV0lR JKEtrMyC8r5jBG7JV2xYvAlK/q7zVpDXrcfbMhsXlPvVeQqbstsWHiP8BQkyBBmY hV+tFFBSEn51lCSlZoktU23ztlDUW0lL0m0MoXNbZMPM/VBZN8kOlBfnQgRiNW5Y KwXThAVgkv+PYwMmbZMXniFgIbnnNbT9n658MwMgbp1+q856KbvuI7On8V/cGCqA 1aPM5Vsy3sC7HOW49v05wpxLhl0yaovncMMv0eQul6DCg477LlchizehQcxoAz6z VTH5afPCVWRwhQ7U7zrS/w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 427fhcjyuk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 16 Oct 2024 10:20:56 +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 49G9xbWS026283; Wed, 16 Oct 2024 10:20:55 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1nam02lp2040.outbound.protection.outlook.com [104.47.57.40]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 427fj8n3mv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 16 Oct 2024 10:20:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AioKK+OdMVSWIvLcnKlQIHFpxT7OFgiy8qiEvnaime1oMl5EuZyEVAWfwHpCkSQVwuL3Wwzli+1f8LgkU9yMUaQyo+FjmleVH4lbiazusotwGWC3Y9ZXA+lcuwKQVJW3+CTPXcM7SRK2l5imKxo4OvS9esiFW+fKK5/HTnacI1gbLpf42yBPox4c9EXAaJ9ucFgyy/9gN8bHmHgxarEZiy2Gufv0TxdMTI+cyBvsIHuNfdPY4zbtdH6SF27rZFTZ9nV0IanaMfP8U3etlscGGSugm6BgHBqlJ/RDu8AFNjCj2wkGfmWduDk9xKhfiH9/tchDaFkD2iXPjzUU32kbWg== 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=YXP4oBBbOFFH5eJ5/mE7kNI6rApLZ6pu5Gss4+TGeF8=; b=neBy2bjgrUnpNq1Hd0eTi1c67M/B3DhZ+2f/Z6WMsjCWZ6sxcD5LkhXzjkX9b3EG1WNM/5W6N+51CSc7d9xB83jlMmvjwwcPJ+A+XNY5Xo+UO2ooV5K++7ZnyEJMXt6Ku26Pz7nVnp86qer4QKsmaL/RaG4kVb6ni1PJdW1nWJW9ei/LOnp/nxQDWxJLrT0LCN43Iv0ZUz4/iGEsZcxUbIEVEBRLtod98LLhz1BYlfR1NeSMX+/O+KMAPoWM91K37A/M6aTxBmcvj7CUYL4u0Zv72Tih7fne41j6tEVlUhurMC+Z1ujeMzDgWFa3HvGMQcsQ/N3p135JT+IAXV3sjQ== 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=YXP4oBBbOFFH5eJ5/mE7kNI6rApLZ6pu5Gss4+TGeF8=; b=Xv0TLHquU3wWssdegp9KsRrXTgFIJBIlOJE9oimEFtSy5wcIPlsIdzZQLGX6rPsgHO59tcYdbIs5s4CvDDSeVhhXG2kzaZ3sa727FG8R3FkTAbbQJc1S2am1d43ydjr0apCvlQJLF9a3HAMExxYwT+fktDCRmQ34aAXyscTlOc8= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by IA1PR10MB6172.namprd10.prod.outlook.com (2603:10b6:208:3a4::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.17; Wed, 16 Oct 2024 10:20:52 +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.8069.016; Wed, 16 Oct 2024 10:20:52 +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, Oliver Sang Subject: [PATCH v3 3/3] selftests: pidfd: add tests for PIDFD_SELF_* Date: Wed, 16 Oct 2024 11:20:39 +0100 Message-ID: X-Mailer: git-send-email 2.46.2 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0307.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a5::31) 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_|IA1PR10MB6172:EE_ X-MS-Office365-Filtering-Correlation-Id: c5e32e5e-505e-459e-8d66-08dcedcc3669 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|10070799003|1800799024; X-Microsoft-Antispam-Message-Info: y4AIyedOtj5y2VuKxnJ/23cFJuaa4VKLD1PF/pFhBc6hcR5zcKjqDQmq/w2oquVkJeEtKShIOVe8ueAtK4+9ZB3FdxLlot6LZ7u4kGQGrh1pbvZTqUkbk3/Bpi0C/bBD61egiU/QVMEWfagnPFumTK13Y7Y0qB+8KECFbRCfamdONU8atdOOrMJu4QAG5FDYi0JLSzU0MOQ7/rQ0LEgCxhsiIKYAkGWraCE6/TjtgvRDChWzWCir7kl/zhC5hSrkujo/tjYY/RBJyZ/CUN3NAjGx19ollAtKk9AMhnI4wgeG8NBRgPIl3OD81fz8bwlDohKtryz4G1ReCXaWiM3vPC9w1yQGoT8JMn4eADOXJW6T9DsAnlkBaFr2MCfVOZ5zxn175nHKptGzGmQneRF3buYxwcw079rwxZryKGvFkUzq1n32GFzNo/4GqRJA1UFRKIxh8sIneJ79xQojdNjuymM6ObY8ig+C2R2HkYyezZOgOlVDden9Sv8AXfQx/efPwuelfpIGo4TqlmKOb++j40H6G7/PUhZHxmMziIvMqlmMZnRuf11DBV6uno6cDoeqyxhgy0xaVEzFx54DtuHSaTKAeD443T79ageoJubybdyZJtFMC+9rgi11xIvhwg1HLja3q0dslPpPGU2nRQpXYhciv8p/g/MzJIU19WjF8phP64OMQ+ks5NVwOEJO1Z1l/eiPu24Iv6M1MqPZbj9Xu3WmQMVe0pMMdztZGU1vGlDhG3PsB8om6f9I+k4K5+/yNpjEElbmAjFgbqn4pkoaTwGx6xD9huxit5nRZ34Zu0ivcWiAw96Zbs2qwAQdjjzMv7JDJOTSQFHqbSd0YF4NWD5KyWhNszYYGyU+wTpWwrYoBgnFUvmEeK4bJ2Juc9OGkAR1OKOvZ7YcnoF/xyVXeEIBIGDCxKpv1q/83AcIMjJQNYbGaHqZTA/2ez96y3O3Uk4vAuc0Bi295Tc97cW/IHzfsrg34cqmlkSz2rfYpg4p0mfcMqaNZw+BPJ++LWiqWc1qUABbCAFQ46pnIw+UCThw5rbaLQBtvO0rJkh1udNyFInMF7iyrp52cSB+NHJFDuLoCiKuAy0uvSB2dEnOMsN87VK+EPzX1o/dcZBhpPCqVKZ6QisnmcE/98Dle+Mw7hArEE1fUjekn9nnHTc4kQXVVzpfz84ol3+yBH2/RN9ClVG7HrQLfIlJw9dtDOxfHfyA3vYfhu0UjpeikJ82JKxJ6Dk698ZaCbLxm4i7fZIMvupGOhBHWo3HR2vWTft5 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)(366016)(10070799003)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dWKbAx/K5gIO8Ff3koqHPh26ggjUr5gE5oZpgKmD/setJLa/iYdCZFcXk9ON4Lm4lEiNmM+KVqwR5HK20PU6Bu2Ft3v18C7tA/iIFKLHKDRaIjTBcHhMMNQJdKBYOzFubBmHxhOQ8qYEPXDKFSbJT8vIdkXqx9n51tJcFeNBJ34bUVOofuDEb96B0OOxmqbBULm3BmZswVpHUE9KV2r/HMQEbO14gtoj1tLS3LUztqtTx4x46Kzx6OBPM1KStrui7tK0w6XopTii7VNa/XwiKneRVOKZmJpwjz4crP9GcZbdQFiV1fZjuPzTHbjVAsGALG2ww+HTxHo65ULEAZxQsMiHIpH47F20e8RWnC5e/3vUGzROuW1KmZWkoRox6lRNFUUmiEJmn4EpxXZzaPvaNvbCExpyaYYXY8d1W/LszUDyAebuTH8HS5ZbZg2Wqe4Pjcc8XjaPTfAnD1wMXNdjz6o+9nturiALSbMpySvk+MaMa29i5W9mbHyF9o8oIup4ZxlvjFG/TZ76UDvrXjhCfKjaxk1PtDxTc5p4xWuvrZJIS5I7ZfwQqP9X0Wn/ILNaXGxa6Ny800zh9j9y9QRXmGYaZ0SL6bjOB4CJaseEQe6ZY0crNrUZuVxpiQKZpEIf0NBsKxT1Xa/5mqNpbVgDrIHgRafLwbdY4LkCuEbJp/7bPpE/GQovqOXOwN87lDT98C+Ldpt4NPTArpbMQ/biNd/bNyceLEhG598x4wtuNL96BW5ZzYlAYAuTbGpl9SkrIzXmVhwDkM1cPP9SvdU1sblHZL3xUl2LSqByZ6k/EiUCizUGKgelPHfT/SVnUptkvyq0vpR+j3fIxiS1xbfEPmudU+hqorSbrSI8ISPg4aXj0/VtaXAS2WCEFC/9tvk0uSuAAvXWEQSqC1u1Wey4ZDHSOaIqPGiFqMwksIuVRoLGa18rkNx9mDTYTx8RJc38eSXCmfejIoGQeqnX6OXg3m5MblQ21BTRC2AkMY7Kg5H0gHJRHbmsuFyfThk42ixkrzP8yFT0FyeMkPBKCudcgLvJCXwREb8NHSM0SrrtgeBBfAcdbDCQMHD1KZix5nKaQ+ka4yPUVk7OqFa6OrfzFUT0C8zrLROO2tnWQDQQN4OTKfCTiDJDjlxVFSYuMzA8ojrN/Y2fdaCv8Rkn1GvhgTOcBHqj+SuIqHEtmkskTzlhoAHti6hxScMeBsjVUssIsfInzEE+yz+nw8YeiR//5mn4gp0UM0Yz72SUrjGCiymR3B4W6eYMM5s0I4Jz/wyxCD5iws0XlE+s46pm/PRoLJxcdQnLvW9n24VEB15dF2DcaY+RM9MZ4i09D7YAkW6nenM3aiMZ4X/cXj/H2OXwHkXN32v3Kh9Ctobj0YYNbsiOW5Tk4+wKKzEjmdIOcdIidYJWc1rDMyIMGjwEiDgAzwW+zWJ3oNlz4/nLFyEhesuHh9s2BInL6ZIoHHAgPhWlL+6P2pOkCSW3YZdiyiWrnbhT+KOod/ondQP0zFKrLh7mMwcgvO4p5OX9bX2M2kZRd0WILVKXf4WZioSZFsGR6Uuf0WkXolDUG2HVIc+tPtjQ+8cIUM16HPvpRbgffdQEWNcNlCnV+xB1lM++2pgjTyZ6NzQkGvktflFERpj4wTQe6T1VHdm52YVD/LIp27oU5YhwNTyj27E4JSwyWfLZWw== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: E9vm9T7nazp4H3d4xcy1tCLeTucWOG+qyQLgoK7UQZFKKjXMLFn9h0rzytY1v3ZyIwhaSSnApolgFaWauNvKTZwbWxnZtXataVI377vxYD6c0WAMlMAzhxUOjYfWugCeQMlxJBujDhn789jF5gCOKUlNK50z9Upv43R5WVGns7daikukx+/e1/s6+V8xaGF0DisthueiEsH1lz5Hpy3aAep5xVtAQFgzN6A16Q62GO/+I7shhBsl05AIX2nYHSoM+etrm8cVdhdOhReXtwTeO0rVJRAP8fnbLHsO6fWimML4B8hoh04jlb/RFJ6fQ6f0W+y4zjGsle+P3Rtmq0aeWxBFYKHU/M4RTT8g3D96LDKzqtUo0WVzlvgVdLSALHuN8P46CKyElb/itCakM/jTuVGNyYx13iCakfnvqB9kxAnd+Re9NxET4Wf5Mp/jxORybLx5tcBDLSeqOxNPMLOg8hdO9Xtp/CIw+ljGWtky1AJGFaz4IPiPXqp2OoCpggP4V4iP5CeJkoqtWswJmNnXzc57QV/Yz7N3kP++QUfki4EMMzQpLS5oRfGV8zTE3lBvpMZY6pyK8BXxEQIp4R+9rDi+CJRPA+QFXIuVMlLWhYM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c5e32e5e-505e-459e-8d66-08dcedcc3669 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2024 10:20:52.3841 (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: ID9sjXpRpZgYC8h0m9aM7xD7iWyvEDQjNg0VoFDZu3ww7ZaIlgjDjaa2bPsipiOM/AyP5kmJrI9lBjkvYfUlZuQ1A+Aa9PuQrW4ZyPvKqJk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB6172 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-16_08,2024-10-15_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 adultscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410160064 X-Proofpoint-GUID: AaFJVgT3LBv58RGyEibGWiA7zZPvlh40 X-Proofpoint-ORIG-GUID: AaFJVgT3LBv58RGyEibGWiA7zZPvlh40 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 Reviewed-by: Shuah Khan --- tools/testing/selftests/pidfd/pidfd.h | 8 + .../selftests/pidfd/pidfd_getfd_test.c | 141 ++++++++++++++++++ .../selftests/pidfd/pidfd_setns_test.c | 11 ++ tools/testing/selftests/pidfd/pidfd_test.c | 76 ++++++++-- 4 files changed, 224 insertions(+), 12 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..48d224b13c01 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,94 @@ static int child(int sk) return ret; } +static int __pidfd_self_thread_worker(unsigned long page_size) +{ + int memfd; + int newfd; + char *ptr; + int err = 0; + + /* + * Unshare our FDs so we have our own set. This means + * PIDFD_SELF_THREAD_GROUP will fal. + */ + if (unshare(CLONE_FILES) < 0) { + err = -errno; + goto exit; + } + + /* Truncate, map in and write to our memfd. */ + memfd = sys_memfd_create("test_self_child", 0); + if (memfd < 0) { + err = -errno; + goto exit; + } + + if (ftruncate(memfd, page_size)) { + err = -errno; + goto exit_close_memfd; + } + + ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, + MAP_SHARED, memfd, 0); + if (ptr == MAP_FAILED) { + err = -errno; + goto exit_close_memfd; + } + ptr[0] = 'y'; + if (munmap(ptr, page_size)) { + err = -errno; + goto exit_close_memfd; + } + + /* Get a thread-local duplicate of our memfd. */ + newfd = sys_pidfd_getfd(PIDFD_SELF_THREAD, memfd, 0); + if (newfd < 0) { + err = -errno; + goto exit_close_memfd; + } + + if (memfd == newfd) { + err = -EINVAL; + goto exit_close_fds; + } + + /* 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) { + err = -errno; + goto exit_close_fds; + } + + if (ptr[0] != 'y') { + err = -EINVAL; + goto exit_close_fds; + } + + if (munmap(ptr, page_size)) { + err = -errno; + goto exit_close_fds; + } + +exit_close_fds: + close(newfd); +exit_close_memfd: + close(memfd); +exit: + return err; +} + +static void *pidfd_self_thread_worker(void *arg) +{ + unsigned long page_size = (unsigned long)arg; + int ret; + + /* We forward any errors for the caller to handle. */ + ret = __pidfd_self_thread_worker(page_size); + return (void *)(intptr_t)ret; +} + FIXTURE(child) { /* @@ -264,6 +354,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..440447cf89ba 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; + + /* We forward any errors for the caller to handle. */ + 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 err; if (!have_pidfd_send_signal) { ksft_test_result_skip( @@ -66,25 +98,45 @@ static int test_pidfd_send_signal_simple_success(void) return 0; } + signal(SIGUSR1, set_signal_received_on_sigusr1); + + /* Try sending a signal to ourselves via /proc/self. */ pidfd = open("/proc/self", O_DIRECTORY | O_CLOEXEC); if (pidfd < 0) ksft_exit_fail_msg( "%s test: Failed to open process file descriptor\n", test_name); + err = send_signal(pidfd); + if (err) + ksft_exit_fail_msg( + "%s test: Error %d on sending pidfd signal\n", + test_name, err); + close(pidfd); - signal(SIGUSR1, set_signal_received_on_sigusr1); + /* Now try the same thing only using PIDFD_SELF_THREAD_GROUP. */ + err = send_signal(PIDFD_SELF_THREAD_GROUP); + if (err) + ksft_exit_fail_msg( + "%s test: Error %d on PIDFD_SELF_THREAD_GROUP signal\n", + test_name, err); - ret = sys_pidfd_send_signal(pidfd, SIGUSR1, NULL, 0); - close(pidfd); - if (ret < 0) - ksft_exit_fail_msg("%s test: Failed to send signal\n", + /* + * 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); + err = (int)(intptr_t)thread_res; + if (err) + ksft_exit_fail_msg( + "%s test: Error %d on PIDFD_SELF_THREAD signal\n", + test_name, err); - signal_received = 0; ksft_test_result_pass("%s test: Sent signal\n", test_name); return 0; }