From nobody Tue Nov 26 21:39:50 2024 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 21194207A37; Tue, 15 Oct 2024 17:56:36 +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=1729014999; cv=fail; b=VK1ciIPyqroacbFQ0VqhU834SaOeFJYeZ1+ks2pNH1lRQGtR2uj2jX6LCsKWCoTpujqwW50UqjPz2C12CAiRqbnTxxauJq6bZ9L0VWhP9SRm54xXOAsqZzYs0lcuOGWkfFPK4iRD5YcKpZDl9GchxiOC9yxe66F0KuhY+rJ4AMo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729014999; c=relaxed/simple; bh=t1/+EbOrgsmz8cOVsebDUIsW+YFGoZ74x4xhhsTlfJY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=BGbiUxsr6SDNhj/2rpWbXHwdlDLHg/KiNOeEY8+BScRXSDNVxKgMrAusgaa28NXCa9gLeFPo7sx0piozys5ZlTjwH4t6jd+HEl0qGeJAJ/KcIj7pnqZnxoDPPM1CQuKhvWINSPJy9dvLPM64jLwVNDgncOQ9z+ajTIRHyVwzPng= 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=McVIJNPr; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=BqYIDNc/; 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="McVIJNPr"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="BqYIDNc/" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49FHtdmo011659; Tue, 15 Oct 2024 17:56:24 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=Yugvb4V1/DaU9YPaXiZJXwYIK8KD9dleMPpB9SJ6Hp8=; b= McVIJNPrsacFgsRNFbiZEW5PheaABDt/ncGhNxnG5K5s5rX6xmJ1cPuxqJWKHqpV S6hvTN4XrxlGsZmZEzuUWtuL40H/N/RhK0AJgWJ9+8rm57jega9Cz3qT32rRDiiV qIafudG6Mdh6FYURL1JMZ/8539gYhYVKT4+fdHVdOCk/TrI92j/k637s1rulYW0F AA8hZsnTInR4x9auk+2HmcdL1WaO5pRYkqq7VTp5OjNGx+KcZTUyDqK0x7Hk6hYc Ges66A7KpKJgIJR+Mgyk1VCluTAECzYZrnSEuu4x+BbwTKv2BDrfE4+d0NZTi4bR vBAr8xUE0y6ei/t1laG7kg== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 427gqt1s10-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Oct 2024 17:56:23 +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 49FH4Uno026283; Tue, 15 Oct 2024 17:56:22 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2046.outbound.protection.outlook.com [104.47.70.46]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 427fj7swjg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Oct 2024 17:56:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=E56YhpGPsn/OMck1MrUZlutbg5olQwQc2X6hpMLREi5HDNVRZ5aUxu8fI7rSVbfyp7EAYmXv8qEcLlmzfaVTH0xJKj63bEfODwQa0PYiG8+Bj5izQo/GRc1igN/i4moCrQ3G5CL5metdokADYeBIgOw0+Q05Smkm2RazYjFFTn203M/fJXUNYNmnfz9Am0M40BSdfcwayVsyuIAY9ys5sMbgodFc9Vsjl4mutXqpbwWkOJHfyop13DCK084xsYhEJOD/6xwYPYkqjO23UC+hPI/pmi0sHm6q7SKpKPxMNEiEiUNooUR+S1i90rBmSoNa9py3LSud5Pjl1ZYGTJIQAQ== 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=Yugvb4V1/DaU9YPaXiZJXwYIK8KD9dleMPpB9SJ6Hp8=; b=CkvtTRI/d8Bg4ahTWbkKTFNESQH+AA0T05d9C5poCqX5mzlCZvjMETW5fB95g8CSiNYdS+B6GXx9Pd2jyeFtHh+Atkaep2qJPO+U69bluSWgSg/h8ObbINfAa9o8uSWFOnvGmiCIh/LJC+xscxdYRRhncjq/d8mAlbzCTKEgAOaOauGX7sV3F02hkYcrnGhcXYYof9A8CTuLsdlWMNpBGC1Y/0yVSGyCaAT2cXgHN0o+ZHUYpPtST2PnCaYYZ4Ta1p8fNnweC+tOJRMd8Ufa+boMTujp5uCNIxanGGHMzJJ4jaienVCZ0X9HsC7S5y02nUVs9JI3EG8ztyHf0VOORQ== 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=Yugvb4V1/DaU9YPaXiZJXwYIK8KD9dleMPpB9SJ6Hp8=; b=BqYIDNc/8YlTMBZYwqUPDwylAN7SBkl0dtibxzY44osUDAZSEGKCI/RfPMVNx45zHO3ERDySZeDgZsZNJYjfSDJnUjFWr/HmMFqfcDK8R6AjMyg5RJQ3vOKIMAvHBaqWA4X4XtLlYE+E+EM7PDuu//TSfxdlDe7SMAtOG0c5xFc= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by PH0PR10MB4725.namprd10.prod.outlook.com (2603:10b6:510:3e::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.27; Tue, 15 Oct 2024 17:56:19 +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.020; Tue, 15 Oct 2024 17:56:19 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Linus Torvalds , Jann Horn , Liam Howlett , Vlastimil Babka , Jan Kara , Alexander Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH hotfix 6.12 1/2] fork: do not invoke uffd on fork if error occurs Date: Tue, 15 Oct 2024 18:56:05 +0100 Message-ID: X-Mailer: git-send-email 2.46.2 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0145.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:188::6) To SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR10MB5613:EE_|PH0PR10MB4725:EE_ X-MS-Office365-Filtering-Correlation-Id: b9d1b6c5-3501-45fb-dae6-08dced42ac4b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?BVPOu6txGovq+WixrfBegcbdDrzOiqvpwlJmK8NYj9tPnNyBMtHHewbBZJRq?= =?us-ascii?Q?0CXr0byulKSnvkx4S5k6DljDQQKg/oG95tqlVWxIlBrSzum1PtCc0BxYL9Tu?= =?us-ascii?Q?2el4PSDus8VRAlyM9zPnY3ZNesl1qn6ScN46EaJX/Q0EFJ8JhUD26r9wn0mb?= =?us-ascii?Q?5yztV0YKMjMI6Z7oPzzc9q/0rKT8+5jrXAmbHE3ACjccS7RTJiHRD3Ny4LEa?= =?us-ascii?Q?6UP9Hh/CxXCj0nECfc/C3NvEBbRr6J1DFezeM36JaAgWvoPPmGXhzVRIlNvY?= =?us-ascii?Q?LTE1lMguAAPditmfOl3g8mKJuRdBbB5L0bRA5G+6GJVrrLfjn5mKXhq2bfmg?= =?us-ascii?Q?UyX9V/GeMl+jOBa6W0u/lR8vFZzgv/gaIFThvbFI0KMvrDDD49vS4iFO/048?= =?us-ascii?Q?c/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wsw=3D=3D?= 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)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?vSnylwSjTB72bxXkZjOcCuSGQSxThxsQCrjrljQuSSQclVUVt0PxLPwuCaAo?= =?us-ascii?Q?ownFB5YrThnMTUUdx7Ih5hCYfMgDmYyq38VhvPJzHuxylK9JlQLt5Mol/qV/?= =?us-ascii?Q?xCTNt6Uod5Y8VgLP/NyppTPVEXhQHrabGjV6SvDkKPOnmHjzbXI+XA95JOrN?= =?us-ascii?Q?V1u7d7xqqGH5jw/lLkNIDVzwOknHFz3AIxdnOWdOxl2O5HEjGUpnBIZl+BiH?= =?us-ascii?Q?SlNJFRrctXvHWwKhSibPJ9+6A6y1mRn0MBOObDOFY6b7hirPJCGzACQHha9T?= =?us-ascii?Q?sXXCGyBMFNFFhndL+FeCEiuMM95jAReCRUNws95nX6ak4/8orYfgQiB50A2Z?= =?us-ascii?Q?91uniRoWpeUjLF7wGM0LdTfAdIKCORy4pHTltuFPAENsVzdZZMc0rGNRIeJj?= =?us-ascii?Q?E5Bq8f1k0IlgkuAOnCV7by5KtGGdsmY77AoDLb5M4cUWg7JLGWnLkjNgOHLB?= =?us-ascii?Q?c9LC9IcKy8gye+4bmi1Xuejku99dc+gJSW5RIyKTP7imGwBe6jfqCKAp9iYw?= =?us-ascii?Q?HacyWxXxnc93hFWNZVLZMshu4os6GSaHtTOy5ezt/Wm76tjPWhK6mgDP+uqv?= =?us-ascii?Q?loO6FLVUtYMku8nxtbBoOH7oKa/Xay3EoFiNV3wPNZMa391usO29XeEkdtPt?= =?us-ascii?Q?FJPxlWuUM+tHQZzjpnIl/3NmPUW7DWCO3Y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q?= =?us-ascii?Q?X+/l4q+DiMdM/g3ki4mNdgoFPFwerF3tFVEL29SEsO1KPGxWftjQYxrbRpaw?= =?us-ascii?Q?rg=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: wTm/A/4oc5OPke3Kns/Aq6f4GJ34YgoR1JxzovQvFtSnZkUirENX6bLzgRgug7jEptKcj1ud3hULrPMq3nmb+ZRY+b5bVCIbhey8uN+Vbd7/gxpoktWyKO0fdTdgNRDYmVcZYs4Xt7tyNyORpPQDm6z7K6CXnrzeh8U4QjryBN9dznPnisHg48NcXLaskJguKkJkfYAlmx4sY607rBIT8+gEwSmom3tTjnfA2u7yx1uTpVqFmYqoM0rsFdb2xXxtS32BxpVlsfJaUo0LzzTalFwIy7Lpv7Q6S/5Z/rvnF1Belgi/23qOIU3Lb1R44+G2SJOOvBz8U/xztLtLRnI8cJU6xfV6GF6G7OHrMowR4tb7JdrbJ5BwL8tlyIN0zGXjI83vsznaK7aqkHvzvYQd+zxFkU0rLvjgHbnpOZdGwhrmWB4ScyKYYzH9vLZ5fV1vLyA8MRhsCh4AzpOBMu0a8ubqcuXESbpfH53WRHHt7SmSt08QaDpGA3MsmzGyJJHsMk0lNNDUJQXO3uErSwXmEA7vtFHDntezb5PI18o4ohLHZc0sWd2vuy334goR7rTw99KGAG1BYXWCAOaHbgBv2N/JYnS9EcFl3QY2Tsqu8U8= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b9d1b6c5-3501-45fb-dae6-08dced42ac4b X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2024 17:56:19.4674 (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: AtFSLgDYCTz+BGX86lt177g7RaqJJZ1T2n/77IhH9w1eJN+J0IW0lE3u6IeOkm78oGaaUEkFZt6rvLXrPZ53W+6qY3Enyi1yfGtOrnfe/Yw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4725 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-15_13,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-2410150122 X-Proofpoint-GUID: HXnmHst5Qkx1XkMSPtEYe8ZUwKq8VBPo X-Proofpoint-ORIG-GUID: HXnmHst5Qkx1XkMSPtEYe8ZUwKq8VBPo Content-Type: text/plain; charset="utf-8" Currently on fork we expose the virtual address space of a process to userland unconditionally if uffd is registered in VMAs, regardless of whether an error arose in the fork. This is performed in dup_userfaultfd_complete() which is invoked unconditionally, and performs two duties - invoking registered handlers for the UFFD_EVENT_FORK event via dup_fctx(), and clearing down userfaultfd_fork_ctx objects established in dup_userfaultfd(). This is problematic, because the virtual address space may not yet be correctly initialised if an error arose. The change in commit d24062914837 ("fork: use __mt_dup() to duplicate maple tree in dup_mmap()") makes this more pertinent as we may be in a state where entries in the maple tree are not yet consistent. We address this by, on fork error, ensuring that we roll back state that we would otherwise expect to clean up through the event being handled by userland and perform the memory freeing duty otherwise performed by dup_userfaultfd_complete(). We do this by implementing a new function, dup_userfaultfd_fail(), which performs the same loop, only decrementing reference counts. Note that we perform mmgrab() on the parent and child mm's, however userfaultfd_ctx_put() will mmdrop() this once the reference count drops to zero, so we will avoid memory leaks correctly here. Reported-by: Jann Horn Fixes: d24062914837 ("fork: use __mt_dup() to duplicate maple tree in dup_m= map()") Reviewed-by: Jann Horn Reviewed-by: Liam R. Howlett Cc: stable@vger.kernel.org Signed-off-by: Lorenzo Stoakes --- fs/userfaultfd.c | 28 ++++++++++++++++++++++++++++ include/linux/userfaultfd_k.h | 5 +++++ kernel/fork.c | 5 ++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 68cdd89c97a3..7c0bd0b55f88 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -692,6 +692,34 @@ void dup_userfaultfd_complete(struct list_head *fcs) } } +void dup_userfaultfd_fail(struct list_head *fcs) +{ + struct userfaultfd_fork_ctx *fctx, *n; + + /* + * An error has occurred on fork, we will tear memory down, but have + * allocated memory for fctx's and raised reference counts for both the + * original and child contexts (and on the mm for each as a result). + * + * These would ordinarily be taken care of by a user handling the event, + * but we are no longer doing so, so manually clean up here. + * + * mm tear down will take care of cleaning up VMA contexts. + */ + list_for_each_entry_safe(fctx, n, fcs, list) { + struct userfaultfd_ctx *octx =3D fctx->orig; + struct userfaultfd_ctx *ctx =3D fctx->new; + + atomic_dec(&octx->mmap_changing); + VM_BUG_ON(atomic_read(&octx->mmap_changing) < 0); + userfaultfd_ctx_put(octx); + userfaultfd_ctx_put(ctx); + + list_del(&fctx->list); + kfree(fctx); + } +} + void mremap_userfaultfd_prep(struct vm_area_struct *vma, struct vm_userfaultfd_ctx *vm_ctx) { diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index 9fc6ce15c499..cb40f1a1d081 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -249,6 +249,7 @@ static inline bool vma_can_userfault(struct vm_area_str= uct *vma, extern int dup_userfaultfd(struct vm_area_struct *, struct list_head *); extern void dup_userfaultfd_complete(struct list_head *); +void dup_userfaultfd_fail(struct list_head *); extern void mremap_userfaultfd_prep(struct vm_area_struct *, struct vm_userfaultfd_ctx *); @@ -351,6 +352,10 @@ static inline void dup_userfaultfd_complete(struct lis= t_head *l) { } +static inline void dup_userfaultfd_fail(struct list_head *l) +{ +} + static inline void mremap_userfaultfd_prep(struct vm_area_struct *vma, struct vm_userfaultfd_ctx *ctx) { diff --git a/kernel/fork.c b/kernel/fork.c index 89ceb4a68af2..597b477dd491 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -775,7 +775,10 @@ static __latent_entropy int dup_mmap(struct mm_struct = *mm, mmap_write_unlock(mm); flush_tlb_mm(oldmm); mmap_write_unlock(oldmm); - dup_userfaultfd_complete(&uf); + if (!retval) + dup_userfaultfd_complete(&uf); + else + dup_userfaultfd_fail(&uf); fail_uprobe_end: uprobe_end_dup_mmap(); return retval; -- 2.46.2 From nobody Tue Nov 26 21:39:50 2024 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 8F6611FAF16; Tue, 15 Oct 2024 17:56:40 +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=1729015002; cv=fail; b=KEX9pG1HbOq0WlzrjbDLAHit0QC5pdK6wrE1/T/uV+EiJa4J3c8KcU66DTMmvvvX3AEOUfiwjXjBrvKtcACcNrkX13Ond/pmsLaoSp0HuxJ0c7Pkx71f9myP3kpchOQ1OtUjU/sdYs1kxha7TOoKnzTMT37AFH6cdBGZHV7k3nI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729015002; c=relaxed/simple; bh=kKQIVSjXtjCuVpsryGWfDBuDn5t1M97zoVQ/aMpr7yA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=YT64LzJb2f0sq+RKtS2YkqhQvAhVyVbeAUdN3uDtXfFi98f9Iq/4K7e1yCPEMP2I5gR+XSVTvbdRZBFhKJct8vj/cD9Ee9A3mEiKgNKoRwOijYFzppBgPwapQi0TCGKAwnBGDXkg+DwsJywOuZeRM5232deo/AOUl7KJhRpU0SE= 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=gEo91h5x; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=pUGOMHTL; 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="gEo91h5x"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="pUGOMHTL" 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 49FHtfub008297; Tue, 15 Oct 2024 17:56:26 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=05Vlkq3Xmq9GA1USXavexmhprJXWFzls7cqapKWzTvM=; b= gEo91h5xsF7SKSEaMc2SaLhtbljG4MvGiKRH9NK+g7Qmz4FxFxPkJNupEY+vU+38 D/oWhCw1pVwGVoCIuZJCq1qymRcYzX6Q8EAof5h9beSCdAoRdQmWgywkd11GlYuu Qa/3ZnhW0bhh2M4aYax3Ld0/ytKid+obidVc1q7AsS3bXwNsuuYXk3904b9Dvn4J KhKJlBuOpx1099I7wg108Bs5+SfhKHvfa6y2yVBR9i8v3Sm591yaWhYNseRIAGXQ tZd7mzf9d6qzSYl6iSUL70yhpJX17vzNIt10SuSR59NS17GTiUiv2Y7Hfi6vAv1G aS6U9cHVS3jsLawTNvoB7w== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 427hnt9vjp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Oct 2024 17:56:25 +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 49FHQMhr026416; Tue, 15 Oct 2024 17:56:25 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2048.outbound.protection.outlook.com [104.47.70.48]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 427fj7swmd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 15 Oct 2024 17:56:24 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=eIdtnCW4T67xzUSM3jU7NO7nNb52ndpWTU8ZJF8M/OrAxDglB44inl7UuTp/laozKS+Z8L3z32y6gScu1C/thvTJWSkC0O7Cu47rVjpz+9wzqR54ZYEw6QbDT4IpIaEyazmaNrhtNtkBWn1gCXxHMXJZuBZpjHYWx/1t2GF5GlkSRanuEMHW8jl7g77+E1rLLq9za+Rf6OR8BzuJUa2c/6Q6W1RrXYAqSGVKxuSBSznYppIZ2+E0MAMpKMkxd+39/fhiDKmIVdyn7jN3sYULLZezd73L3tew+Jann7gwa/aF+Dcakt5yNLhoz0U5cRm8E2ZDGanOzfC1Zx+oDnjdvQ== 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=05Vlkq3Xmq9GA1USXavexmhprJXWFzls7cqapKWzTvM=; b=lEbH8LYjQ7en4NxxgOAVWQHcktUq81vYmboiQWZ8wVZF6wkEHMB7aweMkLcqY5z6JK7GogG3iOA5lHQ4qFrMD+c23j0a/3D9W8hshpX91qSWnbs09LLNVVfS6Qcybz3ISwxlz1CyBqxp2Kao/DlL4kXDpptSWIfYV6Lfrxomh6pLB3hmrRScfHo0f9e9X+GfD8E/tHMJ7eBnNVM8A5zl22h/N/Oii2LAbjISCQW7YBtXU8nT0q4kLWJkcubJJLDIpxcQjXl8v+05BCOIS7FH6QXpa1iBGAo+HGP2R/gVww3xLmE5ewwBFSU65fqhJd2nZgzNHsleQDtefyZNR9HR0A== 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=05Vlkq3Xmq9GA1USXavexmhprJXWFzls7cqapKWzTvM=; b=pUGOMHTLhGi6nhg6XeCPeDGxi4sq9Gt77+E1MjZGS6vdGeHSRvEKmL+WbwPVFOh4Ro3/jMqx/3wsuoeSVauIth93+Ln4sGJIT4Su/8ST0ZavkfI+GEEUdwpFAr9YYdvAGyXdfFlab89mR/c1Oc+kCLDBXWoze5C5ZNoJahGH+E0= Received: from SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) by PH0PR10MB4725.namprd10.prod.outlook.com (2603:10b6:510:3e::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.27; Tue, 15 Oct 2024 17:56:22 +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.020; Tue, 15 Oct 2024 17:56:22 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Linus Torvalds , Jann Horn , Liam Howlett , Vlastimil Babka , Jan Kara , Alexander Viro , Christian Brauner , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH hotfix 6.12 2/2] fork: only invoke khugepaged, ksm hooks if no error Date: Tue, 15 Oct 2024 18:56:06 +0100 Message-ID: X-Mailer: git-send-email 2.46.2 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0263.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:194::16) To SJ0PR10MB5613.namprd10.prod.outlook.com (2603:10b6:a03:3d0::5) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ0PR10MB5613:EE_|PH0PR10MB4725:EE_ X-MS-Office365-Filtering-Correlation-Id: 6dd1990e-93a9-480c-dbac-08dced42adea X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|7416014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?74hlVoDjMTkUZvvELCNFxuntYqq8CJJSaXRRmuTazWAqJjlgPt+RKuPKLYZ8?= =?us-ascii?Q?JfElPFVF6SGegXubFyuGT2AmG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?us-ascii?Q?XYZgv2C9GQsH6ZmbdqvfPqjC0QTWcMiPrFT5+qEKjuQp9I1pOQJWngyrhEMz?= =?us-ascii?Q?t+s6jpBBYi/I6WUozOBxI2bYw0VTbCX6kwcw99t6ZDoi6AhrnPqoo+Aspmsl?= =?us-ascii?Q?uDq5nydX+AsKdMxzLAqi2vNJlgZV92zpC7byBURrF3ovV3PIuKaS/dYXTqkR?= =?us-ascii?Q?AVjr4O7AgoTk1vbG59Z342XWaFeI6h1zWv7w7A+0+3J/fAKEVceFmKG0bASw?= =?us-ascii?Q?Y0KXf42/uPC4TNPm5WXaicK/SnDGzGVCGiK3/Xqu7UQ+Lc1rkOQychd+VlXr?= =?us-ascii?Q?XfZDPBN/BMU04n4uuY2L7u7MJ7WAZtQaqoRH3dJktQ=3D=3D?= 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)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ubi33RJAMRLysJ1CZM6QrP08VyggS1Q3iAJaVDiUkujDuNjhE63gsspjNsMK?= =?us-ascii?Q?ulmt0KTcMCuBCDV3aaMHUvWVyCeA5Q4MVkleDgC0H+AhqqRaRYBeC2Q9qCeQ?= =?us-ascii?Q?WBGhfkFLDtOwOIqRQbDMeq8hLe9NUh/vZAd/SM+LzPaZpwa2wdVrcgHijRM/?= =?us-ascii?Q?7gP0PNTZqMwDvJeRS8PBkuhdsbH77MeTor8/b7eFuF41aP9OwDMXFoZ3YgMw?= =?us-ascii?Q?znIUpyUucCkJlViHHdqE4c657mrxioaGohOR+/wkkEqLH2Gq83mx0UlR7MOy?= =?us-ascii?Q?lzhSkvv2Dmk0BwciPQoGZEImiJnwMJs2mRE1eMJ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F/0DEpxpKzrMZijt6EaaWdOPAvxJBPoiH+n5vdV/5UPICKmOqtLYNqUP7l09MUuoSaJ00Ig5FalP1iKVqVMRQylcXMmMUqt2JXrjVVUXkPaunAHXQOMbVbOpEz8ER+/ihTL/bMJHqCnkGuI5DUKGCVPI7U= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6dd1990e-93a9-480c-dbac-08dced42adea X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB5613.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2024 17:56:22.1868 (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: d0TUINwOuHF0I0hViDqI0p4NRynnwAKeGm5XlzQe3PWpU0j1w5IGT4X3NbyNfqbM5mFApQ7nG+npNDgeQxf9jtjL2ohX3urBRPuyNTjlSpo= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR10MB4725 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-15_13,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-2410150122 X-Proofpoint-ORIG-GUID: vthpnDlnal1iTCXoT8Mmzt3v0eUEehph X-Proofpoint-GUID: vthpnDlnal1iTCXoT8Mmzt3v0eUEehph Content-Type: text/plain; charset="utf-8" There is no reason to invoke these hooks early against an mm that is in an incomplete state. The change in commit d24062914837 ("fork: use __mt_dup() to duplicate maple tree in dup_mmap()") makes this more pertinent as we may be in a state where entries in the maple tree are not yet consistent. Their placement early in dup_mmap() only appears to have been meaningful for early error checking, and since functionally it'd require a very small allocation to fail (in practice 'too small to fail') that'd only occur in the most dire circumstances, meaning the fork would fail or be OOM'd in any case. Since both khugepaged and KSM tracking are there to provide optimisations to memory performance rather than critical functionality, it doesn't really matter all that much if, under such dire memory pressure, we fail to register an mm with these. As a result, we follow the example of commit d2081b2bf819 ("mm: khugepaged: make khugepaged_enter() void function") and make ksm_fork() a void function also. We only expose the mm to these functions once we are done with them and only if no error occurred in the fork operation. Reported-by: Jann Horn Fixes: d24062914837 ("fork: use __mt_dup() to duplicate maple tree in dup_m= map()") Cc: stable@vger.kernel.org Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka Reviewed-by: Jann Horn Signed-off-by: Lorenzo Stoakes --- include/linux/ksm.h | 10 ++++------ kernel/fork.c | 7 ++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 11690dacd986..ec9c05044d4f 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -54,12 +54,11 @@ static inline long mm_ksm_zero_pages(struct mm_struct *= mm) return atomic_long_read(&mm->ksm_zero_pages); } -static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) +static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { + /* Adding mm to ksm is best effort on fork. */ if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags)) - return __ksm_enter(mm); - - return 0; + __ksm_enter(mm); } static inline int ksm_execve(struct mm_struct *mm) @@ -107,9 +106,8 @@ static inline int ksm_disable(struct mm_struct *mm) return 0; } -static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) +static inline void ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { - return 0; } static inline int ksm_execve(struct mm_struct *mm) diff --git a/kernel/fork.c b/kernel/fork.c index 597b477dd491..3bf38d260cb3 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -653,11 +653,6 @@ static __latent_entropy int dup_mmap(struct mm_struct = *mm, mm->exec_vm =3D oldmm->exec_vm; mm->stack_vm =3D oldmm->stack_vm; - retval =3D ksm_fork(mm, oldmm); - if (retval) - goto out; - khugepaged_fork(mm, oldmm); - /* Use __mt_dup() to efficiently build an identical maple tree. */ retval =3D __mt_dup(&oldmm->mm_mt, &mm->mm_mt, GFP_KERNEL); if (unlikely(retval)) @@ -760,6 +755,8 @@ static __latent_entropy int dup_mmap(struct mm_struct *= mm, vma_iter_free(&vmi); if (!retval) { mt_set_in_rcu(vmi.mas.tree); + ksm_fork(mm, oldmm); + khugepaged_fork(mm, oldmm); } else if (mpnt) { /* * The entire maple tree has already been duplicated. If the -- 2.46.2