From patchwork Fri Nov 15 12:38:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876174 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0E6DD6DDF3 for ; Fri, 15 Nov 2024 12:38:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 778246B009B; Fri, 15 Nov 2024 07:38:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D8C66B009C; Fri, 15 Nov 2024 07:38:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 469E56B009D; Fri, 15 Nov 2024 07:38:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 203046B009B for ; Fri, 15 Nov 2024 07:38:56 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CD8031C6193 for ; Fri, 15 Nov 2024 12:38:55 +0000 (UTC) X-FDA: 82788281520.07.BB6DF24 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf16.hostedemail.com (Postfix) with ESMTP id 569A318000B for ; Fri, 15 Nov 2024 12:38:07 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=WdS7vL8L; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=h1QGwClw; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf16.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674268; a=rsa-sha256; cv=pass; b=lbhgeKfMsAWRCSxjf4JRQaeuXAQ4aHCiiAhWUf0mjWePaRJncXe+Wy8u8ifxmkenDxaAo3 kDHHUgVcbUSRtdyGyUS7BPTSgVUXI2R0gH98XD84HxQZP+oGtIFsH8RWmkvNbxgkvxWf+R y47HrNiKc1Y4dw1AO+jLY4dfXoTDLr4= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=WdS7vL8L; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=h1QGwClw; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf16.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674268; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=lN/v8jAJa6+T87vYqjFs43JvP4ZvGI7KJfkCLwPdhVo=; b=wpTQSkZaAhCfQW8sUIXF0lR3Es1fYyWLp9FpW8/7weS8a9JIiy3zk1/E4IB+af14O2QTsy HZqNVQQ0XAvP9Jp1G9KbBBtKn29OS5OPyhj4GehJOcOD0go+Opgu3uMz2Tu7Zl8it/rY4t Y2uu4dkLF7UFsoi/Ji6SK4gs0RTEXYQ= 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 4AFAH04M014153; Fri, 15 Nov 2024 12:38:40 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=lN/v8jAJa6+T87vYqjFs43JvP4ZvGI7KJfkCLwPdhVo=; b= WdS7vL8LpfijFgPkKN0TpoKwBcFDOaK2VU8+py82NU6LNiOtFFi7qP734UxMH2sM +DOooQ3XUl7pjNFB094+OyJUMLt1TPO5U2yCv+RAlWIh72BsvnAEjUxEymKBA8Cf lRjcmo1cPLiwyYFvc521JOb/BmKbKaI8WwNtrBc7yrA5ZM1UtMlrgwrEONIvHlWP gDBss1HUgGb/8xlneldmrJ0c84ZMCPxZj+ZDWB6j4AGq5dSUnRbgUpdPVE149lUq hAWy3mMm4es2CFkq5h5u/MRUpuuDkYWqcu9YOiVbfO6hCOcW/BK2lBBSYQSU1mmD tVtALIuphRir6s7AwLp0zw== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0k5k5td-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:40 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFCYFXU005704; Fri, 15 Nov 2024 12:38:39 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2176.outbound.protection.outlook.com [104.47.57.176]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ckrhd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IgWI0U+OQPFs7uyBIlLwxrFZOnSseSckxhEJm6HtaRGHeDRVyJAeo7RvdVrLZrdsxEWoU6jWgnLVj1GXeJn5UcXoSuI35Vc7wUw6Bv+iJ69TqqrH2wu4qhDJltrlget833o6c8JHddaAz/HOCypE4674SY7LEWYysuXCgxJIPeMIm1giTogctaBJXS/DBBUp+nkTr9T+vnPsRF0FTnR8PsNHvfZ3xjamuUvfeRvN9jadySvHKnqXKxYK11rRJ3ee7bJt0ef6D3yWcjbtILNssrw+VIScByecwFJJaNMr+67qZJG5frUrD4Z4R5bO5eENavsdz9XFFRJpQiXDvx/5AQ== 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=lN/v8jAJa6+T87vYqjFs43JvP4ZvGI7KJfkCLwPdhVo=; b=ilGwea3wHXX7wAoNivuoIsn94r6gOY/DmXucCX5TM5AtK/NPXxq9GkCLPF+Xsyz0j0K0NiabpQ9b67iKxmyIv615ebNBq/ve8BYt9FFfThbJG9i/FuP4BRpTEI0jQVfvqcoDJP/+Y+XEVDVR1J6bch/RkzujLN8mxxjGC/TdUSsL+M90fn5+7z8jJcCuxpA81awOUnDVsIVckX4TUwcksB+l51BeLSTSgKfHnEiNW9VRaD1WAyjp7+RwBcvLThnbFRATytG1J1pZ/kOHy8DR1AAxnCIZ/UlD0vaNzTILm+SNeYmLxfkt1DxDpHbh3Hm6DAhKe3Cg8qIbj7SRFWY4eg== 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=lN/v8jAJa6+T87vYqjFs43JvP4ZvGI7KJfkCLwPdhVo=; b=h1QGwClwwZnPxlXOx1WvxAaPzEgxFEmo1ZXvO390ieBxHflw63inGRYEdBta3e3kRyfTFrmztRCvTQytIpHipkcMhNgw1bI3IxqSfIvSwkl8bCHInoV8eTkXrnbtbO8j+H3McBGTpT4hWxmxR8wkkwVAsYik0YVRy345CsBIoYI= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:37 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:37 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 1/4] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Fri, 15 Nov 2024 12:38:13 +0000 Message-ID: <2eb0f68593bf50f79cd346269fefe596cf0a6801.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0037.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:61::25) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 675d25c0-f6ee-43c9-c212-08dd05726cdf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: BIkdN3OjDLNKNa/xPLz7K3qR7Rqi/83HSDUIWFxg3YUq/HLUuuR2K+3Znly1N71mmSzGEMBBWq5momcK/p4wJLappmAUCSDuhQZjKKDVxRWD8230Z46yk7iZcFpYb1Hs8iMhWm4gd1y/8Z13NbLmqthGhzFL6G9AjkX1rWEL6KAlmQcGqArtZUuBeiPm/ZCntUntAYyW4i91ufKeWlkOBxIHQjyjKTU4qGfrflDGAHnZgumvEb51zM6rNHjLHt45rKk7UdM7tlr7D/1AQHuYq5UuUv/cgN2oHrnVvDK8B6mU8fmXzGiPWZCo+jWhT7B6IR+L39TxlbyuTCCOCKgUUBcnpQ23VThw10+o47RffyvghuwWuZYkGLMdsX2ifaZJv81SFLPNMJrV81RT84K3qii8/n/yIP+TMR+MpkNc/GVeb68KMSEmW73W/owd0A3IKbIxEUJ/nhKNJlOHYQ8NSYiEelCc6lAswiGzVb5liRMQ/dt0t37ksvFkjtnYxfFLJwRyLe0HPpDvNDIJRGdjoz2+4c6nCxgqShzrWVidcCpgJI/2vyICiaXyE6foi5PQWIKqlz+EBF/KMahW9jhUnfLFC+gxqHDLk/BkDL48UhkRhWfYu1jypuJ+nfvYSvhYSknl4BZYVq6U8RzdUlwQjwUmJX+EMgRq797GtY7bRrYCtZj7QsR5EOQlsm9ohnXwS3UgZUT1GmRvZhUHftr0X1R5BA14sb90ceizG86Ja7NszNRdqyYZC3Y5dssBU4CGnZ3tk6FaRzjgVbL49I6zDB/PHKmrAfI+9hoGTnz1kxrZDJtJkilHILm7u1SMS5ms8Mk5G3N2Sbfrb53d+qEzo6L3dRT0RYqeOMzXnPgf39TEXV23ofsnGbENnmQreveeTxPQiy95kw8sl8xgyMhL+/46pRwnKjp9pnVYB+3Cm0aL24PvS4ykUo9wL1QDY4qHE3ONKWPf5nskZ933hbXuDFGK0vCYP5HrZxUOGtn7QD/9Ekgfz746ThW3Z48DVVQtsBZVJTTyzKTXOS4DTQ/TFlAj5lFmTDIgEWSsFn2xNefgUE9Y5AHw/G8pc7v4VFr+n7Uy/zveSpoqrYzzLkhbTJZxB1/dCgKeil59WqbnUhmXPvtnM4bfjw660wX/hyzmLvOWcDfgrze+5RLQ7teJPpZGj/43ste+yvNqPfazajqNU/9gDv7ymy8tx8NDddp9AEDybwYiiIgJId9lbQ8//p2QbmHAklIFpme9BkRw6fjlWPHB9H97kHrjmkRQPIuf2rRNnV0h7VQp7d4iR5xCkDrqMlCr1z2987JoginkJIp0HfEdi9gP50JCqRygTSjB X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: P6/IOF4+K0Jnnams6Tcn6/xZ9ybPykiwyHP7Yetf90/AibV5xJeKTcIMefJJkD/O21MtNXvhTs6hUTrzgOHuJ/ptPj9I26hPEYnTa9Jctqd7+gpjgWf5etdtDQ7WFzuZiWosYO6sPS4eg7X8eeTUjU5vxIcl7Ju++45lBmKIa4qQ9LO/CCA2LV3FsmRLScQUvYes24itl4Jx9WQc6yVWj+FGurqZdroSzgleN2haI3TbR8Mcqd5YtW47KDu8QBURjjZALQeiq9UKccvoovp5dW+XQmznkhjT4mrfT8TVrgS26YqQfvVJKqtQazDWPvqc0Z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5dn2TL1nFhZSKZl/mqs0JdTi2q0NP3APVvFG5+rfxSyDRfjLgvCBlNS1acWcB1ug28gphqGwIQoMcTylf1kBeURXFgm4mc4k/dQm507uq57SodAeBnRdmsADs56g3lWjwej+7eHVml61UecI8qOTSx2bnayAXVhjl51uIPdDDYdL+se2B/7Wyr4FtjdL4rtiFikx6tcI7tiXWAqBebwpxjj4PAA8FsA== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 4Ct3uqx/dOe24ehSuqGakrWUVplW2Qybhk4XkLN6Y+Qeu7GzT+7lPe7+fiR6Vz0oSb3Jge09zx1H6DX0ExEOdVvSVcSnwgplJkxG0sR64r+sips3tXXblB4T2eaf681rMmpGWZpj7yWdpyKkulQrM1RChDKJQFSkH91tYgIQhdt/2j96MD9Pg5eGwIHpfR038krV24MOUSsu+YR2Wv0XR7z6SLs7MYPdSG5qJ3FuxPcqOgTjdAercJp57IvmE7vJfdBvfF1mjyk80Snahf7vNs6cmTVz+qW8EDwojTAgagl43JCr9LrrhfQy/KIjecWdybFWKmY6l2ZAHDNYwDg20Ifo5FfjTwtzHWXWOnYfjUClYpuHsaGqBzmYrz7LLdtrlU3SIupAc4+RzWfHgfaHlKpDPTrzA4qOemS+4qmWWPKH0qLGk0AvelxP+wSecgbg1LCGvkfjCIrjM42AXXVbJGjDrIUJdHJGJZTaE2Q593DUBuqgEe4NiasZSDjOzBnpy9+7jS8Bj9itx+SbeMhsD2vvJahtSXnm2h9KtUi0WIuCQwN+4gsw8aBvSPSjI/ZIyLRkMVW4VbsKz5wVgbujkFRCwkXCiPr/99HRTfxTJmU= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 675d25c0-f6ee-43c9-c212-08dd05726cdf X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:36.9689 (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: wn8gEn56I/1ow84ctn5UXB2XnJv1OVyOJxXzqGP3K8J9msacuZ7L2aO6fAxv9fJtustCh/rd8oAeQF3QrosM/bRibZSmrNoVqc8RkHw50Mw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: cO9S6qe0LT4Un3PmImzeVCGMjSnaU9P3 X-Proofpoint-GUID: cO9S6qe0LT4Un3PmImzeVCGMjSnaU9P3 X-Rspam-User: X-Rspamd-Queue-Id: 569A318000B X-Rspamd-Server: rspam01 X-Stat-Signature: cujcuo96xoenq3oy71z6g8g9sh39wump X-HE-Tag: 1731674287-431272 X-HE-Meta: U2FsdGVkX18tQeJ2GvH+2/lAzsZzcZejzOz7oXmGxiRYCYTYdBxcaAN8++fOha2Tb6LqXfbrCFluww4xhFTQgC70wd5O/5vMzeKY88s2sbcGRbER+qEFa6oKLdx+L2n281iII5/S/tuAWs9OFb02rJVAOfxZzUgDPZZU7WdGTlbPE/asGk9rzv0BWMU6VTI+JdZZDg2jRxW6Tz7SI48gpM+7tPty7S5wCMrj/p3/JkmTm8C+OVjA10x+haXhUzMXxKCzOl2RFXP2JswcR0BkqPH/JU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aRGip18ZcrYCzVwuxpg2JOoBwzzjYG86KZdB86t2fM8KoZJdqy675+0v27vT3GiaC5IKl9ecopXewmH+nAory/A/+RJL2HOpXPR78yqrSSAoEv8cZo0QWMafCqDYkaxYJ7Xjdhfkr4hWozjcRtP8DpctPitmp2coiBAx5CnAFASkGi9HdR4mTEdbeZ4OvAOwUPcz3zGDEV4+ciF5vqeFFPGLlZzW7P+qNmTJ1BI+/OXpVkEdntl0rFeRcSCP6sNS9b1w588fO5IRTmJAtKU4XijKoBL95MT18G8w23J0vMZPFnbKFo/lpSw06K4NjRlSUfqCWW9h8E5T4TphkyL46/TbjBXJDiHUP6TY55DBm+bh5me2jEa1zDq1BJxWVe/6n71gUKTEqiA3NqSGCGCo5RnbVrvsWglsKhiMb5fbsOSTsZS+DiDVacFUVTuf+2nGIaI1I8XGM66ry37sKtiQPI/3DMGwhADakCYbeIhAPZ05Ct2TQHhvaCqr5aaHc1MfpXf+r5X4uTMhidMeP+lYxUOus3Xo35UzDet/rhsvIdOyLWyl27GGs2Q3vAUkT0irqBeEBQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ Upstream commit 3dd6ed34ce1f2356a77fb88edafb5ec96784e3cf ] Patch series "fix error handling in mmap_region() and refactor (hotfixes)", v4. mmap_region() is somewhat terrifying, with spaghetti-like control flow and numerous means by which issues can arise and incomplete state, memory leaks and other unpleasantness can occur. A large amount of the complexity arises from trying to handle errors late in the process of mapping a VMA, which forms the basis of recently observed issues with resource leaks and observable inconsistent state. This series goes to great lengths to simplify how mmap_region() works and to avoid unwinding errors late on in the process of setting up the VMA for the new mapping, and equally avoids such operations occurring while the VMA is in an inconsistent state. The patches in this series comprise the minimal changes required to resolve existing issues in mmap_region() error handling, in order that they can be hotfixed and backported. There is additionally a follow up series which goes further, separated out from the v1 series and sent and updated separately. This patch (of 5): After an attempted mmap() fails, we are no longer in a situation where we can safely interact with VMA hooks. This is currently not enforced, meaning that we need complicated handling to ensure we do not incorrectly call these hooks. We can avoid the whole issue by treating the VMA as suspect the moment that the file->f_ops->mmap() function reports an error by replacing whatever VMA operations were installed with a dummy empty set of VMA operations. We do so through a new helper function internal to mm - mmap_file() - which is both more logically named than the existing call_mmap() function and correctly isolates handling of the vm_op reassignment to mm. All the existing invocations of call_mmap() outside of mm are ultimately nested within the call_mmap() from mm, which we now replace. It is therefore safe to leave call_mmap() in place as a convenience function (and to avoid churn). The invokers are: ovl_file_operations -> mmap -> ovl_mmap() -> backing_file_mmap() coda_file_operations -> mmap -> coda_file_mmap() shm_file_operations -> shm_mmap() shm_file_operations_huge -> shm_mmap() dma_buf_fops -> dma_buf_mmap_internal -> i915_dmabuf_ops -> i915_gem_dmabuf_mmap() None of these callers interact with vm_ops or mappings in a problematic way on error, quickly exiting out. Link: https://lkml.kernel.org/r/cover.1730224667.git.lorenzo.stoakes@oracle.com Link: https://lkml.kernel.org/r/d41fd763496fd0048a962f3fd9407dc72dd4fd86.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") Signed-off-by: Lorenzo Stoakes Reported-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka Reviewed-by: Jann Horn Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- mm/internal.h | 12 ++++++++++++ mm/mmap.c | 4 ++-- mm/nommu.c | 4 ++-- mm/util.c | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index cd444aa7a10a..4670e97eb694 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -34,6 +34,18 @@ void page_writeback_init(void); +/* + * This is a file-backed mapping, and is about to be memory mapped - invoke its + * mmap hook and safely handle error conditions. On error, VMA hooks will be + * mutated. + * + * @file: File which backs the mapping. + * @vma: VMA which we are mapping. + * + * Returns: 0 if success, error otherwise. + */ +int mmap_file(struct file *file, struct vm_area_struct *vma); + vm_fault_t do_swap_page(struct vm_fault *vmf); void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma, diff --git a/mm/mmap.c b/mm/mmap.c index a0a4eadc8779..11d023eab949 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1788,7 +1788,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, } vma->vm_file = get_file(file); - error = call_mmap(file, vma); + error = mmap_file(file, vma); if (error) goto unmap_and_free_vma; @@ -1803,7 +1803,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, addr = vma->vm_start; - /* If vm_flags changed after call_mmap(), we should try merge vma again + /* If vm_flags changed after mmap_file(), we should try merge vma again * as we may succeed this time. */ if (unlikely(vm_flags != vma->vm_flags && prev)) { diff --git a/mm/nommu.c b/mm/nommu.c index 02d2427b8f9e..2515c98d4be1 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -942,7 +942,7 @@ static int do_mmap_shared_file(struct vm_area_struct *vma) { int ret; - ret = call_mmap(vma->vm_file, vma); + ret = mmap_file(vma->vm_file, vma); if (ret == 0) { vma->vm_region->vm_top = vma->vm_region->vm_end; return 0; @@ -973,7 +973,7 @@ static int do_mmap_private(struct vm_area_struct *vma, * - VM_MAYSHARE will be set if it may attempt to share */ if (capabilities & NOMMU_MAP_DIRECT) { - ret = call_mmap(vma->vm_file, vma); + ret = mmap_file(vma->vm_file, vma); if (ret == 0) { /* shouldn't return success if we're not sharing */ BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); diff --git a/mm/util.c b/mm/util.c index 973021847e69..f55d7be982de 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1086,6 +1086,24 @@ int __weak memcmp_pages(struct page *page1, struct page *page2) return ret; } +int mmap_file(struct file *file, struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops = {}; + int err = call_mmap(file, vma); + + if (likely(!err)) + return 0; + + /* + * OK, we tried to call the file hook for mmap(), but an error + * arose. The mapping is in an inconsistent state and we most not invoke + * any further hooks on it. + */ + vma->vm_ops = &dummy_vm_ops; + + return err; +} + #ifdef CONFIG_PRINTK /** * mem_dump_obj - Print available provenance information From patchwork Fri Nov 15 12:38:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876175 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1059FD6DDF3 for ; Fri, 15 Nov 2024 12:39:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FFFD6B009D; Fri, 15 Nov 2024 07:39:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8AE616B009E; Fri, 15 Nov 2024 07:39:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B3276B009F; Fri, 15 Nov 2024 07:39:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 46EF16B009D for ; Fri, 15 Nov 2024 07:39:01 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 115ABA143E for ; Fri, 15 Nov 2024 12:39:01 +0000 (UTC) X-FDA: 82788282318.25.9D59553 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf16.hostedemail.com (Postfix) with ESMTP id 7F4CF180015 for ; Fri, 15 Nov 2024 12:38:12 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="X/DDeoNm"; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=A6MdOwnY; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf16.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674280; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2hzJyGd7ND7ALeuDMST/ER3oYgX4pWOPUyctcPhuIyI=; b=zxv4SrHsMP6rEEN/0nUSLqx2mvMS0kr/vpqxzeJU6MgD5I3bFNWarSAPXgxS3wI1X+4sJz AhW+cP+aMUIMD/2KOOQ5yIWvniEf6+h0zs8Ld1y3i7nKQUduA+roM7Rq5mstazaxbA6jNi LMZbQk54jDRArQ27oQiL30aZOeabi/0= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674280; a=rsa-sha256; cv=pass; b=34S4tpgeaOqh4Tt3h+Hj0TPcWSiIlF9eANmyqzCI6MDGQ8Lb1Lw5Zs10T+PXzxWc4Jfy0Z p2l7D9BUx0QDeteYYupWjN0pK8wtwMIL59mxS8Hfo5Rb0rrvnOcNYANnw/bfIqZfu9FHcX zwr7Kmq1xPZTjQvZW1fIonVvOG/q3XA= ARC-Authentication-Results: i=2; imf16.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="X/DDeoNm"; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=A6MdOwnY; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf16.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com 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 4AFAHJXb000670; Fri, 15 Nov 2024 12:38:44 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=2hzJyGd7ND7ALeuDMST/ER3oYgX4pWOPUyctcPhuIyI=; b= X/DDeoNmCV/yVvjp+KNdjE4YqXBAdXO/F4h3u9GT7jNtMDnVFFAzF+en9m7zT7KS RYLXKd3EQjxsafCTFY4DO5Yu7jGn45UunTbGnGsEHP6rVdlbrSpTrFblWIQjmMNM giTceEUgK95vTYePN9hpAgpP1TMtvWGDV6Lt61yDdtfPwqF/IGWt1tBbOHnrwmLm VNQgo6n2/cM4nDIqAmIEDXClG/VhJJHgnO/gg3ozdPS0/jWWoCg3d0SdxfMrv3no Ec74smptQ7DXc0+wK3sDh21ZsDURQlNm1F3ZnB0Scd0/se0Mr2FMtP48uCgpFbRI 3gncWvrWmbZOdjZrESkmIA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0mbkd8a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:43 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFCSLMg005652; Fri, 15 Nov 2024 12:38:42 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2177.outbound.protection.outlook.com [104.47.59.177]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ckrk2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=i7jJplQw2gPo/LTrXjn/aTsnSlZ77TjLpaDn5g1Suoebb+idack3BygnEbtc8Jq0PDoK/lY19YYtIHUubwHyqxTJk5fRR+9+M6YpEgS5wkZkMZ/1eCUQQ69+0B4iU3L6kzEEZp13b1IhqaYyKV3f89xRXBGioMd4Jyi/cYHspK0kKEtG3gu84DZqc/9u4SJqqmJxsP24IdIaz+HU0D6IJnUk+wvWW1SU1U4fZDzUZmjD1/zuopMWQzg1bEZ/Wlcz/XPcULNtjwdDItilgl9iHemrkMUff+VNV5cpQB6ycq7zPcFAyGRMrGsVr0igGyYlK9wzkPi5X4rFkZn+0aZz8w== 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=2hzJyGd7ND7ALeuDMST/ER3oYgX4pWOPUyctcPhuIyI=; b=cgQgaU55ZrRKhinXW7HhxrtLUu1ENynQyVjzxcJr+W2hpIAS0ajHGv+LN/UuN6Ojp5ELZ0gwwi/wc6L1fpmiTmlgGCzfP+/1lvLoCizGcNUj9prMjaxrfWHKqEX6tikokPslEWFUUufYYBRdvlELE5T9dh4Iycad0lje9eL1dwgqJPyi5/WMGH7PcNmR3HYXJ3NewyEaOoBN4qnUd/wNgLrSElW5M6KRBeGttvI4OdyhSYt1Bi4XDMWvnCaR3FEo6y9Tx+UWohDUKJgX034KNOrrMs6PZ7FTifcJSSWAum0CAm2IEN3gneScUXVn3QInDPOIs+r9kBxetuE1UkQZ3g== 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=2hzJyGd7ND7ALeuDMST/ER3oYgX4pWOPUyctcPhuIyI=; b=A6MdOwnYAuiThhEskYA5OwVyfz+FCAhviu9pQwyPuLMbVxF2/YQf1C3G7dy8lIz1hNIsJmJq6BbrgPH7IHGa6w4A4kMPVS9ekVz8hJsS1IGXfD/Hz5AyVhl5/uhASbSFfVUkgU9AUH3OnGzRWuflPUj7JG5CbhUzqLbtIpXbK1A= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:40 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:39 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 2/4] mm: unconditionally close VMAs on error Date: Fri, 15 Nov 2024 12:38:14 +0000 Message-ID: <9ccad1c5a53af878459f32ae3efaa3d12d33e4e2.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0178.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:311::9) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: d2a9324d-82a8-4efe-9053-08dd05726e97 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: 4n8N14UzQVWNUv+8o3on9hFj5b1tH/5rVdModFiOHYUYtSlUptbWla9ewpXHoMnzlxfdW9LYmrVlg+DVO3nwvBwTzk6CfipNhtIBleFAcoNZA1VhA7p1B4lXFZJuXgwg6yY7MOAQA0myYdizowlIYPaZlGGNY9KYLfgfWVx307PSo7E2N9DQs0GUk095Cgr9lkIJcOmyOGE9FmoQLVZ/A3brtDouHBjCl7JNEoVnzX3KQhUIDtEbG2oFmZrrZkSeN43L5s3XR5N2bs31NsaJdFBym7p4nz4Pkx01ZZkU8dCk4upoWrNFcFhLzQObwtsOwzrGdSH+9H7GHmow7JJvKtD5B/UAoxWQIgX81R8Rbj6ByUYEdz/sw6wimuO+UE3Q+9+VPuthdnZWa7040xmscrIe31P7b+CehFuTEcf78hKrVEaOV7Cb1Jo/b14igcG3TTjujAQBiocMexFqye7lhNtpY93x5ORDDRwMAL0WllZAQywr2OvOyJGJcCTkabDfP17628DgA9WAJa/zFrTVNioVaKgzc7oYlhCRRKOUdFb/ztTrgrkG8IFMPn1D4Ky2PSQdkhGQKEsDu6eXkcJMKjLLOZJ2LxfAtfdB85Pb2kzr0vP5UEhAKBJrvKESPnoumT5MBEZPQz56Lc1/0RPEK8NUXIJz8aFs25xoCgoD9ntcTUpeM806F/OdHRGs6N6FHms2F/VwqGD2YpXSikBnKLKBJTJSPPyIEzyk5fMKzXQJUZhvq7CT/raunVp3hceNCrmWLXK+29DKTD9hZPHNS+uE1Vg1knSejI6FKZcfqI1Fm7q7o3GqyY7xm64Uj7O5gg79XMNyDRpyh7tXqsMepIyx6YuIwkaZQDeKIgWl32fm4UhyJHpbNU2zgD2zIdGs4mSdeYpQoghexRuOeaIdE7XYxasLXI6+RstLLNLy0G/zjEQKvGypyiU3R7PDF0eDkp8w5ZAIhRchB3Ee3JMJR/gMrnq9HcI3IAoujLI7OKFGN0gimFKPLRdbl0DFt/sNPdkr1x1IHdz9RpFJwywHyE/k3ZKXyYm6g2jyI9k4zOuQ/LPRkPj99qARHDTOwSIT8tAIgBQumKTexIhdAx+SMYqzKdk2JRKsuSifUroxnH1eAP4mbSfIiD4+Jcq/r5FxSEORVXtFBfEF5fgMAE1fYMSjzEZllTfDPvrHhB7xXk4bIp3HdeQ5fUaJIr5/QZue8fv0X+8P0qGGvgx1zao8SVGvbkupNUEW673vBmivbymbEAiFp5UWw1IX/k/MGAzAguLh/VHskpjBLXNNcnW17vg71JPuC9+Ba5SPYbjDmj4BfmqgaZHsJShBZse9+aQi X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sYXyxdEAs2iN+EmJxpjuA95uTTsVIbNrXUHbSsX9ENtGfP8YUwTTjIPgKKONFo4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56GVEG10qexvwO05N3kMr17EQZTSLjXdVBm+3LRztOg8cKOHZZ/EOdwlyplgQtBc9JRlTrumnI9AcQWZAHCDWLM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d2a9324d-82a8-4efe-9053-08dd05726e97 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:39.8749 (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: NLbdxDrRFMM9Mw5kbFee0vE79mQlHWQJ0sBAWoeFouRxffKPSwtVEBZmgEirIlW6rd8jZ25LAR8UBfGMfkqJv2iY6XY/JBvlUrixPUusMe4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-GUID: wh8wQ_Hw_9pJaiqJIOVj1fBgzh5hjuFV X-Proofpoint-ORIG-GUID: wh8wQ_Hw_9pJaiqJIOVj1fBgzh5hjuFV X-Stat-Signature: 4s7pwrk539h73yga5ihbqhmjokbxdtxu X-Rspam-User: X-Rspamd-Queue-Id: 7F4CF180015 X-Rspamd-Server: rspam02 X-HE-Tag: 1731674292-288156 X-HE-Meta: U2FsdGVkX19Ygmr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X6cD7tYSeUolvYPZ2SZDYGc/J7FRXfRTH0USQ4YnwmpWNfIB7gM+ZPEAOSo5vdjzPCOnUmCgPdRybiIrByY4hOVFCJCiopiiObYPqXkbilL+cb6Var2WRUI/b8uOHNvy+MoUA8l0GVINqhztcHbwAcGL+jO+rO7P0S9qtdwrWvhE3PgEmWnfT+ITv+KJoV5ebYTox4K5oYbBUR0Idj7XLABoeTAaN4D9W1SD2kOgLi/HmVtGO8vVdhQpFCIuRZRvzUm2PThWVHmM7u6+CHs9yK0JYQBAkmK+8rQOKvrzBpcKy+49tm38lNVvmy6IxO5IZ6ExHCeFRg2jh+l13lFJCn1co4pFWYrTbMbUlX3rvNt12yzBJzNCWM4dXbyd4zbw4Z6AS61Qo+mDPq+TKyD37y4BXOaVZg9pRgemFLhV8UZcUK7rJU5G9Gm1Be/D+DrJwnVkGS5yCCvlasRy6NHKbmtR1rtqrTbp53TQbAIefW33402zY25tmKQAZBKIFmth0TWTdWLHAYA2GVm69MC9v7wqs0gKdQ5vIOuFkfbPl7EK7HBt7/rnOrsGepbuWIj05ZNxOBajJlzGt6e1XA2wtRgnbJLVxLPedvWttggJxDcJ7VQqcb66D/R/StUpEu5H+1EoqE8fYp45kEPv0yPhjukhA+IL+hf3GdRHWxIjwD6ZbrIjGqDYiv7i9ZghJfbQovLxCwsDQ04VSz/n7yd0rd7KxD5ZfN0Jq5dEUUmM1YfWow2ZQyu6GpXDt35RnZMZ3mngE+HFRQrxhz/RLDw/vHitWmV145OIzCLWTL32GaQhTsJCdjfMPpGCnl55MhqM37IuBzDPW5+LsRiaZMa0t1zw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ Upstream commit 4080ef1579b2413435413988d14ac8c68e4d42c8 ] Incorrect invocation of VMA callbacks when the VMA is no longer in a consistent state is bug prone and risky to perform. With regards to the important vm_ops->close() callback We have gone to great lengths to try to track whether or not we ought to close VMAs. Rather than doing so and risking making a mistake somewhere, instead unconditionally close and reset vma->vm_ops to an empty dummy operations set with a NULL .close operator. We introduce a new function to do so - vma_close() - and simplify existing vms logic which tracked whether we needed to close or not. This simplifies the logic, avoids incorrect double-calling of the .close() callback and allows us to update error paths to simply call vma_close() unconditionally - making VMA closure idempotent. Link: https://lkml.kernel.org/r/28e89dda96f68c505cb6f8e9fc9b57c3e9f74b42.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") Signed-off-by: Lorenzo Stoakes Reported-by: Jann Horn Reviewed-by: Vlastimil Babka Reviewed-by: Liam R. Howlett Reviewed-by: Jann Horn Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- mm/internal.h | 7 +++++++ mm/mmap.c | 9 +++------ mm/nommu.c | 3 +-- mm/util.c | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 4670e97eb694..34b3a16aa01f 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -46,6 +46,13 @@ void page_writeback_init(void); */ int mmap_file(struct file *file, struct vm_area_struct *vma); +/* + * If the VMA has a close hook then close it, and since closing it might leave + * it in an inconsistent state which makes the use of any hooks suspect, clear + * them down by installing dummy empty hooks. + */ +void vma_close(struct vm_area_struct *vma); + vm_fault_t do_swap_page(struct vm_fault *vmf); void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma, diff --git a/mm/mmap.c b/mm/mmap.c index 11d023eab949..d19fdcf2aa26 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -180,8 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) struct vm_area_struct *next = vma->vm_next; might_sleep(); - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (vma->vm_file) fput(vma->vm_file); mpol_put(vma_policy(vma)); @@ -1877,8 +1876,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, return addr; close_and_free_vma: - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); unmap_and_free_vma: fput(vma->vm_file); vma->vm_file = NULL; @@ -2762,8 +2760,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, return 0; /* Clean everything up if vma_adjust failed. */ - if (new->vm_ops && new->vm_ops->close) - new->vm_ops->close(new); + vma_close(new); if (new->vm_file) fput(new->vm_file); unlink_anon_vmas(new); diff --git a/mm/nommu.c b/mm/nommu.c index 2515c98d4be1..084dd593913e 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -652,8 +652,7 @@ static void delete_vma_from_mm(struct vm_area_struct *vma) */ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) { - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (vma->vm_file) fput(vma->vm_file); put_nommu_region(vma->vm_region); diff --git a/mm/util.c b/mm/util.c index f55d7be982de..af6c9bce8314 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1104,6 +1104,21 @@ int mmap_file(struct file *file, struct vm_area_struct *vma) return err; } +void vma_close(struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops = {}; + + if (vma->vm_ops && vma->vm_ops->close) { + vma->vm_ops->close(vma); + + /* + * The mapping is in an inconsistent state, and no further hooks + * may be invoked upon it. + */ + vma->vm_ops = &dummy_vm_ops; + } +} + #ifdef CONFIG_PRINTK /** * mem_dump_obj - Print available provenance information From patchwork Fri Nov 15 12:38:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876176 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C2AE8D6DDF8 for ; Fri, 15 Nov 2024 12:39:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DF946B009E; Fri, 15 Nov 2024 07:39:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4906C6B009F; Fri, 15 Nov 2024 07:39:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 294E06B00A0; Fri, 15 Nov 2024 07:39:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 028ED6B009E for ; Fri, 15 Nov 2024 07:39:02 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A3F321C4A83 for ; Fri, 15 Nov 2024 12:39:02 +0000 (UTC) X-FDA: 82788282738.26.28EDDA5 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf19.hostedemail.com (Postfix) with ESMTP id 6B3911A0006 for ; Fri, 15 Nov 2024 12:38:02 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=mezBQECR; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=at8foYZX; spf=pass (imf19.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674250; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Fm96MfUl2UnxcHP9gVllVpK7rAL3yVbYJSzoyNNjI8o=; b=N40CrvVgkysRVWPAC3HUBOdtAK/Z8qU7fkLcP9ablqLZgqAUKSXuvZ4xZ5X/06lfg+OZ7t MqtMt9Q2u0I164g630VBLF5NiKFi+V9LMj1E536FlgljrDIK0mjiXAkW+4Mj9xYdvBZbWJ fEmevj6RWwrCFSg7h76TUPVv4nMTjz0= ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674250; a=rsa-sha256; cv=pass; b=V0Y6yu47/EyOytgTMDYq5gj++r2kSw8mXP/p3CT1aHhCPJUtB0lDxfDGuBA9oSnOaonWVP vEvFnZtzdLlUPg8rj0g/d4tRH7d338g/C7F0/UorbMqRnaInjQLsK7GAuJd8tV/tRDQC0t CzET3tqRRiBiGrhTq9cgB579wBxTAQc= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=mezBQECR; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=at8foYZX; spf=pass (imf19.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); dmarc=pass (policy=reject) header.from=oracle.com 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 4AFAH4VW030282; Fri, 15 Nov 2024 12:38:47 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=Fm96MfUl2UnxcHP9gVllVpK7rAL3yVbYJSzoyNNjI8o=; b= mezBQECRGc2IwiJCFxEJ3gs6yyC070s0LHbrgrv32qA/w7b5Uyo6fSGxlRiVsisD C26vTakYcNwsTF1/ptBIfGdT5kmBgtIaaN8coicqW3Jig4SJApDov2Y5Jh1tzrlj d/bBMrffOdb3Q5yuajai9k1QRuTcA27AaJFuDD3KrCe90KF2N76/66fjkbZVtbeh FhddNXpmfZwWaKs/iAnzyYyybt6oObaVOt9wosxNN3wM9PX5EAczcutlgrNB3/1E CC8sypgmE7i6c2HAI97v7IFo6Nr8YcbSxFdhlCEkAgKYWmH8xrK8rSHYxvJr4JsG H8V5LLNH4GfFLaJIH6bAog== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0n539ue-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:47 +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 4AFC2HOA000547; Fri, 15 Nov 2024 12:38:45 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2171.outbound.protection.outlook.com [104.47.59.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbje9e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cyWv9Z/4X0b1nBEHdoi/4im0T0FtZq04uir+uH8RZVlhlhf+EyFYxlnZr3mRXZzP04N5l/2/NEIIU8OY5gbEg7DBlmlDi0RfVAET3cmeCICUHmLSVe35CCNCQFC/bneac0YOzNjDDGWH1ovVmlBFnxG/srxi15UgNNN4DpwUzk+8q+POoHFpdwuDsrDlayA6tb4sL3BchA+u1PALcLeTzFWN2c4mjU6jJQom0PzCatjzPDfiDl0wNIPgXG27nlVd0UaRioKh8BYk0NFa3Dqm+oA9t0YI8MYAYIVuqwmD2+JNCI3K+zMmVqaYcssXwOLHGaioSI9Q6HhFz9NwqshrCA== 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=Fm96MfUl2UnxcHP9gVllVpK7rAL3yVbYJSzoyNNjI8o=; b=sWugTZyAefYBxYN2Rb6Po4ZC6Ir8XsgN93XKPn00WHA+fTBB+krUestFBCZXvLJBJu4HbcsJ+wkBFnGhC1W2Ll6bynDiP8A7McNWol17LDsMG1zlYXaEAshOVI/PYCYzwAFuoL8e9fDETrHbjUSNhGBexSnNrlm1cOtwU2sxOgG+WHqfX1888E830lzelpFCjBro63hWQdJW168XQzhz7/tdSKvQxwSQnv5pZ9mKmibcKwkeZjtzawkQxPLhpuh5ynP4hI+VvxrlpPA9U8l7luOcjNqQs6Q+60QksMwCmFddYQT3Nui+i7bJIw9OvwNoznFhzqpQvzovIJOvC0/mag== 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=Fm96MfUl2UnxcHP9gVllVpK7rAL3yVbYJSzoyNNjI8o=; b=at8foYZXJQVFfroYASm4HwgTwQGqfwd90AD6j07+Yyb/aLYWMPDUMPMIAfZFe1XzWQKQ9Mz0WOQvLPVxvwGWB88Xh2a7YYaPSSurv6emflcWLIrdUczBqP9KQhE60Op6uLZzjUu55R1lTGhRHDOmZnlmLFUxfUXfE2GdP0KibZQ= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:43 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:42 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 3/4] mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling Date: Fri, 15 Nov 2024 12:38:15 +0000 Message-ID: <6dc9d6d69584a326d7de66da9cbaade9ec64e4fd.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO4P302CA0044.GBRP302.PROD.OUTLOOK.COM (2603:10a6:600:317::10) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 0e4bea6f-0847-4cb0-b893-08dd0572705d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: fBbvKZXUTYonY97A1LWW9SsZwLZGzPocv3MNM8gBlDCWGCvCTaEn5nIw3cg1lng0aT3qznKTIySaP830FQNvuMRdtD/zBSVw31ZIKIITkkTQpW9Sr3luKOFOygeij/ocOFTk0ol1TevyamaNDmVKHYNSGEw32RerIm+66ejj0uGqrLo5f8TjpwiNfGZvmlqRxfVjyejvbX/Ux0RiW1vPc/U8c3KpDMDRadSp3fpASKDFHw+0iKY6vsi2pxH+TYMXhHIzqyuwHT17/bNsrwEpPWLfX45en2cb9d4gU5Nhf0ggsXDCuhdzpW3r7sVDT/2GvaNyfxkSB+3CWuIgMv2sh/0MhIH4NvWA4UrV45Ysp7VyqQolaiOuXGrIKp9GhggnOPb9QcfUMuphwEg+oqOFwa4yI5Ea6zd2iCPGoEbXRfCf87fcBzAQhK4uRRwt6TxWCZV5OfrXGI+JLdnPY3zULhN+IZVQNfR3EJeG0nmHWvW6o98oF0RPxCCMKWFE5Yexlqxn3sIsghiEsLBet0wcQabGZ/cKFRwTkloQj7P9j78q8yoAkOmNusQnY6viqw+thpmwtFY5bioYCYQI8T99jxOT6LkMR05jLx7LyFhqp51R7fzWpF86W7SuEm5CVPhOXpxBO5UW06H6gYAzBe5xzxypor0cOCAyMZf9wuHD+ysQh9v4N5j92irKc33pwiHE/2ulF0BXroTMcaMId02lYd/BWDtwOh/avo4nYHoCMvVfCZeO74birtUqs5nKhblHy4n++IrkzGL1IOkpdiKVKTdEZWZvkafNLG0DY8P1BrQxDW6Q1H1UMNOso6ainy64VWnFtOsSxDAnC15tlqqswsxD6Zg7j8Bje1uO+Sf6rfkXB8mkRsn3WpmKv1dKVRUrpMgXBImLmitKNS1jPc3noPa4aWW4QvQWqZgM4+/QB78Dj7jhx7kaes1S1CQJs7OFW4gvzSC+ovSjfxvRMgQ052B1hvSbZzkk827Ufy3mTcXdyCqEB/bQ9TLviiZPDdaNqjrFcdZFPPn/3++TVHgIR088xbabGTAv8Trcq5/1Hczg3IgiKEfC/rAmmISKl5NGPqDl6veZtgnz+UWnuYU8VYUhDSvMEPCTnc+XbTKQhM5viMM4K3H9JqWMlLxltsOJFu+AUdtMstr/UXct8OiKlKHyBmbd/HZVJg7JDRFkA0FbDREYSSlzyFh5ixJFywze44sRRN+yTMp3q4zRfkgagGqb7QYGLSNGRE4Tu0D0OOfV2ABIw683qyoGdVtzTNKD1gJ6VAkG4IANDCEdnUKugfwq8Aj+KK8z84yEuY61oX+YsJ5PcVI31i7pyb+gZk2I X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ho4h2/o0sU97GKjb/n8/X/12eetAKml0oaIbqL/pTEbV/tJvmbGKiPhRAz0RycSlVZ2COtReoqxXPQqtq12cNShrrlmfksdghfWSgNQBJ8qu7OuArVvVBsv0KzMz3b5tyNM1hBCH9yHcxK9JfUV2rHC6g5GGmpcUL08EeyX3NVYskV3PD+ulxfEPtM2RPlNrwIM3c3pjYWE0cgjAndUCNW36mh9Q8g1x5dO5cKgEtYNUJ91baU+hpdJEr4Xblsxh9OGgpxLOc+N1JuwlJeYaTel8EEm5c/PlgJpeaQu3DfF0BEgkJS9ixo6xa8vON1h4rGyrp+ILqFSfvJhhTijf/aBVPZWk5WIsWc266HKJK8SBaD2GmXVcuP5jbzHUJy/7qF4/Uw8XCBde8UCBxl2drTuoI4CxiEdTGg2EPilj0gGt6IXex2FJbtbEXKsVFqS4veELctgw0tG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PvwkXkOlBIMYBuhXpCIyVPrrMy0vLJejnETG+9YYK08OgLXaz+wrHFucKl/6GGLP3r6OhyEiCQ== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 284QLRzBHg60yERX2fvxnARsLLdrXxeEJxvxdBsAQd5h5JphhtAlxFamsXC657prpGRaMznEs0tNi2A0GJw6v0IrWKa7rdrkSI461NgnU41Sm2ZtyFyuq4FVg/VFlbmcO/HOffIbgBiYXlQU+qM7Ud9PAVmu8VSCgxOBxOReIwNOd58J8SqyJsiYgkPM0YQ6+MTHUFGgRK5gGhS89JNGX57fjZ2jEuw/IWL4U6Bsd1EvptVjHSR0xhC3sTkZG09n49B/JXlPXlOy4huDcAIbO2q/wgU20M/DLFSUOXXBf/Auqw4rpIWYKYSvVBAPpd4ioSQV9x393D+HmlKpAKRzE+UoUUiXzFnZsrFNkdirylw+Tiv/rF6T7DQgAb/OIr3OIeeKcO0BqlCwndfzLfQyrsrJIIM8f5Ad0j76ybJgmB2nXPP4Rptl6iFem6qTOTuLfA9FlhemBfz+0SND7Jq5+txkP9ZV/wcbccOuaumK3vkF6hcY8dxHbT1fNR4kgZ7UcUbu0rD5IM2DSikrtbFL9Lm7CXvjsAZVOyR3GHBXnecQDpvbrQk0Z5h+OqJSIAkI2cXcbbO+sPpW8D1q5kbS9Oe1Ryij3kabR+JtxV4uoVk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0e4bea6f-0847-4cb0-b893-08dd0572705d X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:42.8544 (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: bHqER+Lh+PzR/BThzsrOcp0b9sZJaX25HZWadk+nxRB9/jUdWqd8ppE5MDb+kitB7EKx2AscrwFnUgf1oDH8k8dYK+qUHkacvD4+ZpJnhqM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: VpUPWNeS2cLght2ZiN1sqJPy2YAHzn9L X-Proofpoint-GUID: VpUPWNeS2cLght2ZiN1sqJPy2YAHzn9L X-Rspamd-Server: rspam10 X-Stat-Signature: 1mrc6d94cs1oh73p8k6q7p9ziubsjpyc X-Rspamd-Queue-Id: 6B3911A0006 X-Rspam-User: X-HE-Tag: 1731674282-503626 X-HE-Meta: U2FsdGVkX1+DfyeXyjjNIzGryt9HSAatealexb2LVFFOAbmDSKJLSNy/9Gt7vb+7S6/HcSYuwsB6BV4UZxRmRDiT+XDsd/702C+cPiaA810IP3G/zmGWH6X3aeX68KmTkVOBW9rXw9KiVRLKIipopa7TF3Z3k/1q0NzSSMPP/qi1KqnqjgvkUgd5OgS+f1Y3khpEedTeriQAVtuLELGJquD2LA/HOy/s4TRmnFQpjMirxacfoJMtQqxLRZvY0S12hOWHv7qFJAZ6WZu1dXVjSHthz3LKO6BgnkkR4sm+5PnPVCC+XSe3o1adD9tQ1TBJgQx5LG7QdvWIF+jYXqXpCv7pCnpYPo1CXRP5dlChsS33yrqzDXC62/Eiyz1nNES0nH/DagzCNCrkSYFnuuK+v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96eIN+0FqtLBUqlQ2dTRln6h4BvkQ7TOkdivF6yycJLV+ceEobVkmGiB0yMsH6VVQ4JppsPXpLULkKSiS/rOiMK27NVpnJVYZsctiDBAWtBBP7p1WSXVAWcCIohwZCdmPrIirIzS8XSIoWrLX/vctNlNOlTEb55rwLFErxpxx4bTGIVWEpuKBSFenRNwcYeqUnh02UFWYN+koqTkc0M0zehsbw0dkZ7VjdH0daXZxc8IaCrLPhHDUrECupIyO/o8wLPydoAuK7Ng8z324TZQQcARLW1YQ9aXg0V51VtwERETnlli8SutHS1A6elQtd8aZaqwDzRcVpIvmDcpnF2bB2ZEnmAinngE+z5+pGHX2mrtp/HGYDTjcCFIJcZ8oPIo+hGDSPil6HyXc8lC6xf5lUzWAV+x4RmvhjCpHT0xeFk17vE6K76n2ZSzpjHu12wyFnwKm7c+H/JmEYNpMC/LJalNOqR1IGx2ac2 CDeF+m4k X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ Upstream commit 5baf8b037debf4ec60108ccfeccb8636d1dbad81 ] Currently MTE is permitted in two circumstances (desiring to use MTE having been specified by the VM_MTE flag) - where MAP_ANONYMOUS is specified, as checked by arch_calc_vm_flag_bits() and actualised by setting the VM_MTE_ALLOWED flag, or if the file backing the mapping is shmem, in which case we set VM_MTE_ALLOWED in shmem_mmap() when the mmap hook is activated in mmap_region(). The function that checks that, if VM_MTE is set, VM_MTE_ALLOWED is also set is the arm64 implementation of arch_validate_flags(). Unfortunately, we intend to refactor mmap_region() to perform this check earlier, meaning that in the case of a shmem backing we will not have invoked shmem_mmap() yet, causing the mapping to fail spuriously. It is inappropriate to set this architecture-specific flag in general mm code anyway, so a sensible resolution of this issue is to instead move the check somewhere else. We resolve this by setting VM_MTE_ALLOWED much earlier in do_mmap(), via the arch_calc_vm_flag_bits() call. This is an appropriate place to do this as we already check for the MAP_ANONYMOUS case here, and the shmem file case is simply a variant of the same idea - we permit RAM-backed memory. This requires a modification to the arch_calc_vm_flag_bits() signature to pass in a pointer to the struct file associated with the mapping, however this is not too egregious as this is only used by two architectures anyway - arm64 and parisc. So this patch performs this adjustment and removes the unnecessary assignment of VM_MTE_ALLOWED in shmem_mmap(). [akpm@linux-foundation.org: fix whitespace, per Catalin] Link: https://lkml.kernel.org/r/ec251b20ba1964fb64cf1607d2ad80c47f3873df.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") Signed-off-by: Lorenzo Stoakes Suggested-by: Catalin Marinas Reported-by: Jann Horn Reviewed-by: Catalin Marinas Reviewed-by: Vlastimil Babka Cc: Andreas Larsson Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Liam R. Howlett Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- arch/arm64/include/asm/mman.h | 10 +++++++--- include/linux/mman.h | 7 ++++--- mm/mmap.c | 2 +- mm/nommu.c | 2 +- mm/shmem.c | 3 --- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h index e3e28f7daf62..56bc2e4e81a6 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -3,6 +3,8 @@ #define __ASM_MMAN_H__ #include +#include +#include #include #include @@ -21,19 +23,21 @@ static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, } #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey) -static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags) +static inline unsigned long arch_calc_vm_flag_bits(struct file *file, + unsigned long flags) { /* * Only allow MTE on anonymous mappings as these are guaranteed to be * backed by tags-capable memory. The vm_flags may be overridden by a * filesystem supporting MTE (RAM-based). */ - if (system_supports_mte() && (flags & MAP_ANONYMOUS)) + if (system_supports_mte() && + ((flags & MAP_ANONYMOUS) || shmem_file(file))) return VM_MTE_ALLOWED; return 0; } -#define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags) +#define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, flags) static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags) { diff --git a/include/linux/mman.h b/include/linux/mman.h index b66e91b8176c..c5569219de01 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -2,6 +2,7 @@ #ifndef _LINUX_MMAN_H #define _LINUX_MMAN_H +#include #include #include @@ -90,7 +91,7 @@ static inline void vm_unacct_memory(long pages) #endif #ifndef arch_calc_vm_flag_bits -#define arch_calc_vm_flag_bits(flags) 0 +#define arch_calc_vm_flag_bits(file, flags) 0 #endif #ifndef arch_vm_get_page_prot @@ -151,12 +152,12 @@ calc_vm_prot_bits(unsigned long prot, unsigned long pkey) * Combine the mmap "flags" argument into "vm_flags" used internally. */ static inline unsigned long -calc_vm_flag_bits(unsigned long flags) +calc_vm_flag_bits(struct file *file, unsigned long flags) { return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) | _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) | _calc_vm_trans(flags, MAP_SYNC, VM_SYNC ) | - arch_calc_vm_flag_bits(flags); + arch_calc_vm_flag_bits(file, flags); } unsigned long vm_commit_limit(void); diff --git a/mm/mmap.c b/mm/mmap.c index d19fdcf2aa26..a766b1c1af32 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1466,7 +1466,7 @@ unsigned long do_mmap(struct file *file, unsigned long addr, * to. we assume access permissions have been handled by the open * of the memory object, so we don't do any here. */ - vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | + vm_flags = calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(file, flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; if (flags & MAP_LOCKED) diff --git a/mm/nommu.c b/mm/nommu.c index 084dd593913e..8b5c95528b00 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -906,7 +906,7 @@ static unsigned long determine_vm_flags(struct file *file, { unsigned long vm_flags; - vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); + vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(file, flags); /* vm_flags |= mm->def_flags; */ if (!(capabilities & NOMMU_MAP_DIRECT)) { diff --git a/mm/shmem.c b/mm/shmem.c index cdb169348ba9..81da2c3debdf 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2268,9 +2268,6 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma) if (ret) return ret; - /* arm64 - allow memory tagging on RAM-based files */ - vma->vm_flags |= VM_MTE_ALLOWED; - file_accessed(file); vma->vm_ops = &shmem_vm_ops; if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && From patchwork Fri Nov 15 12:38:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876177 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3B3DFD6DDF3 for ; Fri, 15 Nov 2024 12:39:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0C7B6B00A1; Fri, 15 Nov 2024 07:39:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B96356B00A2; Fri, 15 Nov 2024 07:39:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99A386B00A3; Fri, 15 Nov 2024 07:39:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 755996B00A1 for ; Fri, 15 Nov 2024 07:39:08 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 254CC14163E for ; Fri, 15 Nov 2024 12:39:08 +0000 (UTC) X-FDA: 82788282990.26.6B3BB0A Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf19.hostedemail.com (Postfix) with ESMTP id 9727F1A0014 for ; Fri, 15 Nov 2024 12:38:07 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=dGCFj9Ea; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=HICE8gzk; spf=pass (imf19.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674148; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wSSFsDJKNEnL2Ec93aXMbwQXwjo0SGvX0HMoMi4GdeI=; b=3M47V6FA2/rE+rwHfvjXUyDI4DM+skW35t7d2YjzL8EXGkM4sfRvCCTiGm/X0QnhQRd4Ys vzQ2s1PpxkBFgVDg+UFqrvp5caCA62biA7XOouhuftpQz1Zu4da1LBrc62urAW5w4LleJU erJMSNyai31I3cv+qykgjcVNxmt4cms= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=dGCFj9Ea; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=HICE8gzk; spf=pass (imf19.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1") ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674148; a=rsa-sha256; cv=pass; b=g0HUXh3n9A4uTtR6E1VvzH//cvPeMvH4nrx2PvE/eE3oSP0Wwg1aV2RU5XzgTGEHoydoex aSPw8lCZdZ64dwBDBDpfTZsLB33Un8ShJGd7LNxa6zCjoy687Eegc//GLmo4O0B2lVIWfv HzFq7NwCXXhWYURkypTKznTPn4meQ4s= 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 4AFAHAGc014379; Fri, 15 Nov 2024 12:38:50 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=wSSFsDJKNEnL2Ec93aXMbwQXwjo0SGvX0HMoMi4GdeI=; b= dGCFj9Eanr0yGHt4VxkQ1iqtT3dIzpkMsusb/gegyjtrY0SihgFmGZdcq5QMvTeF Iit7Az8zeRttXJ3prCSufVcONJe2kvNJdjyGWkj3X59jf8iS9/waPcn10lfVod0b iuXaMKwe+UCF13Ab98TOU3yKwiW/OH+0ApEpRMWWhqdNOfcQbQmO1B5rnOpbSoan dsWJYc7PeZsat4PIRK1jkeao3cZiaMGqXtkqGj2pSHhQuUt0YMwWnKlabZTJYg4A nOle1mKZUhC0VNgRXOLU9GynpYolx3S/oL9Ycru72yD4k7yV1mpm0mpoXV4h37vw A/1fnfUgmrA++pxsinYvSw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0k2b9fx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:50 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFBQQPT022877; Fri, 15 Nov 2024 12:38:49 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2177.outbound.protection.outlook.com [104.47.59.177]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42vuw2mq3t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=D3I9u4HA281oswvX/PdmZYHAHKKTjqzKNFizhRgDBQRA8xooPfMC2FejuKsLs9tjmpT2IxXdBPUGJZY+nK6vOFqgc9C5z6WeToRnjnCeRRmGhiAqHkDYpCrvle/98Pp0KfqZr7wyBIWYwgkmcHCuYjH3uBG3ulsHh/T5ustqfQ9GSFg1orpU2lgMKbdAo4RF2xUwoYPPGO9K0OIg1R8y2gee9/DQGd/p0qmQWP8cxkafW8XSoM0vBxthR9wSjqyJKaDBXRpK5GOMKUQdP0rxTJc7XiTCDW1C31B2iPCwoOt5inQGAhjHc86vfly7ib5Dfm9nyZ2nDkCsB5fZN6+RoA== 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=wSSFsDJKNEnL2Ec93aXMbwQXwjo0SGvX0HMoMi4GdeI=; b=LMhnpdTmUyZkCJrofxGZaPCC2Vu2oLMizYmUeIzCW1Z3zBsBf3PloP++NiKYqTlhJA8oSGrL6HuR4EcIeuHrCzVlZKp6NiQ3VU70zLy2hIQVyb8Brij+rtsYyWUH4oSwTEnVDXLLQ3YcU4hEU43icm9IVlV45JIMeniGU0N8Q8ylwMxUf93tZn3aqJdIRzAhtFZPOJhNVIXKb3rHFF828OKTqzzsYqiZinm/Cvnnj34mJNpggB28K7bSow7nw8TTc6mEyMSjXJkpjr/gRxlizyqTZIeFnIC7cIdWa+LXoK1drN6gymG/rOjB5z3exTS9Fegp4n7X3AcOInAyIV78eQ== 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=wSSFsDJKNEnL2Ec93aXMbwQXwjo0SGvX0HMoMi4GdeI=; b=HICE8gzkLcl71zT+fEiEuL4FbBqoowOyaLUiPyG5iAueKOErowPqVSGGsB072mHZak9tMbl8KNPUgAjoI6d615os6VGOfWMGKu62xoSq2yj1/f/voShDFe8okC0J3qZ4CHOQP4x3gtnVnaw9MUPtIIAENpAu16RtU6FL3YGjdYs= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:46 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:46 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 4/4] mm: resolve faulty mmap_region() error path behaviour Date: Fri, 15 Nov 2024 12:38:16 +0000 Message-ID: <03d88781b023104684f07a920a3e3b205461e419.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:311::6) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: f8fa5182-2064-4ac1-c58d-08dd0572727a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: lFQTBR+AWM5L8MbdO77BT1RAuyq6E0TAe/eOr7XYtA2sO5GYVbMBblAI0UbCpOtFDnfpljZOTBU1B0Sxo31Yzih9er2GmA2Z7+sbFNnPoJwZMMiiv6VSqfIeudKh4LrbsjjpbOGNx5ym04JQp5qAc66jJHhRa1OIgakCoBbIWbyo/A295mAIRjrY3RgyGOwBEnw5eOzgGH7KIZFKQFjoXJ8nP4bT5QjNktO/af5CpJBMU78EDdiUTmZYza9Ker6rwZFePuC3NMmq/Ipd1jASF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255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: J3uisKQgjNeybK7ds6dmgipOZznrAQoUoejotMjp4pz2yyyTbPcfcv3OB4E4FDoBD4+wv2JevNAZLXAGuCm7a3JK1XtE6P3RJbv/sdi75KHlPd/dlwxOZXT7iGd0qWWjpyIvGztptdNOTp+LgTFYtA7DNuhpK3px8nwHtQmfMY7a274A9HPGEjj48X3z8YNVUhEgJEc4XjaBA1iFTjqkRwLqLZ1IH32QjNEkjAWNPvlx+r2UIDmzjCa0XSqF5m1H9ajtVV/1kMzsoBMOsZy+IdZOwmNyX2w1JcztdP2LDZlN26hqvjYy6r/VgMR6A4HRZkY29RT5tgaYnOzuqgIodfT79Bu/y3rY2cBBpfAjDdwT7/aO3PPywFMqxqJbc9hn3dRbJVPi2/JlGhyw1yUwLJdsXDctlUtXYnnz3DjtBywLj3J61ou+hWfQ/WWTWRGY+xb2kffCsBCexgtoxddcneJ3NR3h/nO7DEZf6ROMtZJM6GNygXUlfxdv9H5DScPVXN7NqDK5IDpkh4GGNPTZcz9T7acrrXJ8qSINkdB5EcLsflY8F331iznOX0k5AUMm0lPd/jfy1w2IuqyDL26gBzXwfQkVLOtywPe4p6I8eHBnMB3Unx5bkiiGMX8sK2TjCYiYlKfiqgu1fziA88D5UHXOWsf2a7/B6X2vQCavjdVpqeCq5Bzlagu8/av/NC4+jSbzdyMnlwUIapksS/sF6ArgpmjyyqzTzue+GEbHK973oIH5D/+B/vis7GkBtMPwCFq7YmLR/jxZP6q2xtLlA6VgV/gXe0bw6QKIkRWYFdAa5ItcGXiGzRG0U8MH1lajOZD+UKupua19HKLflBjlwK2CnAojDabnbI5OEYv4lXcSy2ADv3MFLJIrH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WjXzpDMoUQKqz46wDElEQicja+eeozCcux5CQqvf5pI+4bacTjPWvv23NRSRum40LBj64RP4H7Wdt+Is22Dr1gXSPSPCxbPVrquKd3JA2d+LpvjVCmAhYktTJ5gBaJS5hdmk1jNTot1l46ldBWYEeeESpX2XVVc0zMlqNnAaIIU0Ip6sno+v+T88QxMve/s8g4JARzmllhjMHcy3WeqOQk0p44kzcFm4RuWQhSbF0Y6qnspy859o4Du+lz+xK1N1O8HRed+SH5TBH9l/DMRTwRKgSbYbexiVySiwHjLfPJ0yB6zFvDXxjJ7bCdHValx7lQVpR7oWq4kHVO9svNATM4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f8fa5182-2064-4ac1-c58d-08dd0572727a X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:46.3757 (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: vBxiwkzia9itGhMMAXhDwnUT4S8YP+kgwA/m4+xq8tLjaF8zba6dtJwefKqPpgQKGe7o8SLpGdrmWSuhB7wTT8ojaV5NCqQebzPAnS9hOMs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: w87bDAH_fk5vjFbiXSmyj2LX9qoRCoGr X-Proofpoint-GUID: w87bDAH_fk5vjFbiXSmyj2LX9qoRCoGr X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9727F1A0014 X-Stat-Signature: dy57najdxu9whjqt8pspmx16kshrntxb X-Rspam-User: X-HE-Tag: 1731674287-727761 X-HE-Meta: U2FsdGVkX18dHrxTtIrXkQqW0vD6DAXNSkPpKJP5wsIfJfa3i8lEziOpCztORXpuKZsBQaOKHLwq96Jfnu2nYWi1cM7s6XTSpryRJhpldkCTAUXbmVaUFY8ppv7WkqRF+zy4eVkxxGV19gFK+7BEI8HvXxIVegLHQYBtjikBVy2fWVMuVXlU+a1CO/if1oaX/yxlkqlshCu0SiNCTq+M+vuQvxic/mQTAk4HOQa1yewfteIlbNPKl5FdsGKvrCgVfgpXoGGdCnm0A9lvd7+RNx9J7DxkvB2XGbgb3y0PuNhBvAW2vfxMpw8jvhrrKbqViD4JzR/lNxjgMImYB50udaOcDLyLUbWepCZYFplGmRHxYOPaCXL7e2fqNG85VOwwEtnwoPeLmj5KmrhlSsGCrIop1gLS34+ZCpmTaW4zHGOl8Fh1L71wgQFWwDPW6LVlEfJseCugFaYp+3tcPXC3QvBtGZLNQ327RBvpndLYdGOqAQ67z9JgGSw9JTrWDOllMyAJMYYwwbw5jkxCE1zEzmgAxmwjMSfu+X4TSMAYIsDQ3Aiaozduw7+kllWE7l5aWuY3Mt2gD0rXgQp008jAQ9PloroCn829z/UFA0bDJdqxPjPOFz3qdruSOG29hDtj2xf5pz3xtce6nQNrHZZJoxV4zND3I0fFkk6DIcxb/As/QNheTPJzy4bftHaVtovUcu58oT/p4ULP232y9QlFPyhj7E7cqq67TFFDdcMUlMrrJHzqV4PDIL2QndQYvNPiUKQgQBdqSA0bC0pllTZWxv8qwEUEp8xu1lCO+9CLNbuHCVd0co9ROkxqVG/saADg6fWLYiwh632pSpluMez1Sw5iwQpDPpXY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z9cVaBpAxFF+tNTLLuiGMtOcVzQKlJlMCYfTq+9 xKyoX6RC X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ Upstream commit 5de195060b2e251a835f622759550e6202167641 ] The mmap_region() function is somewhat terrifying, with spaghetti-like control flow and numerous means by which issues can arise and incomplete state, memory leaks and other unpleasantness can occur. A large amount of the complexity arises from trying to handle errors late in the process of mapping a VMA, which forms the basis of recently observed issues with resource leaks and observable inconsistent state. Taking advantage of previous patches in this series we move a number of checks earlier in the code, simplifying things by moving the core of the logic into a static internal function __mmap_region(). Doing this allows us to perform a number of checks up front before we do any real work, and allows us to unwind the writable unmap check unconditionally as required and to perform a CONFIG_DEBUG_VM_MAPLE_TREE validation unconditionally also. We move a number of things here: 1. We preallocate memory for the iterator before we call the file-backed memory hook, allowing us to exit early and avoid having to perform complicated and error-prone close/free logic. We carefully free iterator state on both success and error paths. 2. The enclosing mmap_region() function handles the mapping_map_writable() logic early. Previously the logic had the mapping_map_writable() at the point of mapping a newly allocated file-backed VMA, and a matching mapping_unmap_writable() on success and error paths. We now do this unconditionally if this is a file-backed, shared writable mapping. If a driver changes the flags to eliminate VM_MAYWRITE, however doing so does not invalidate the seal check we just performed, and we in any case always decrement the counter in the wrapper. We perform a debug assert to ensure a driver does not attempt to do the opposite. 3. We also move arch_validate_flags() up into the mmap_region() function. This is only relevant on arm64 and sparc64, and the check is only meaningful for SPARC with ADI enabled. We explicitly add a warning for this arch if a driver invalidates this check, though the code ought eventually to be fixed to eliminate the need for this. With all of these measures in place, we no longer need to explicitly close the VMA on error paths, as we place all checks which might fail prior to a call to any driver mmap hook. This eliminates an entire class of errors, makes the code easier to reason about and more robust. Link: https://lkml.kernel.org/r/6e0becb36d2f5472053ac5d544c0edfe9b899e25.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") Signed-off-by: Lorenzo Stoakes Reported-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka Tested-by: Mark Brown Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 73 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index a766b1c1af32..f8a2f15fc5a2 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1716,7 +1716,7 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; } -unsigned long mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { @@ -1780,16 +1780,10 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_pgoff = pgoff; if (file) { - if (vm_flags & VM_SHARED) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } - vma->vm_file = get_file(file); error = mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; /* Can addr have changed?? * @@ -1800,6 +1794,14 @@ unsigned long mmap_region(struct file *file, unsigned long addr, */ WARN_ON_ONCE(addr != vma->vm_start); + /* + * Drivers should not permit writability when previously it was + * disallowed. + */ + VM_WARN_ON_ONCE(vm_flags != vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + addr = vma->vm_start; /* If vm_flags changed after mmap_file(), we should try merge vma again @@ -1818,7 +1820,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma = merge; /* Update vm_flags to pick up the change. */ vm_flags = vma->vm_flags; - goto unmap_writable; + goto file_expanded; } } @@ -1831,20 +1833,13 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_set_anonymous(vma); } - /* Allow architectures to sanity-check the vm_flags */ - if (!arch_validate_flags(vma->vm_flags)) { - error = -EINVAL; - if (file) - goto close_and_free_vma; - else - goto free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif vma_link(mm, vma, prev, rb_link, rb_parent); - /* Once vma denies write, undo our temporary denial count */ -unmap_writable: - if (file && vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); +file_expanded: file = vma->vm_file; out: perf_event_mmap(vma); @@ -1875,16 +1870,12 @@ unsigned long mmap_region(struct file *file, unsigned long addr, return addr; -close_and_free_vma: - vma_close(vma); -unmap_and_free_vma: +unmap_and_free_file_vma: fput(vma->vm_file); vma->vm_file = NULL; /* Undo any partial mapping done by a device driver. */ unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); - if (vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); unacct_error: @@ -2907,6 +2898,36 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, return __do_munmap(mm, start, len, uf, false); } +unsigned long mmap_region(struct file *file, unsigned long addr, + unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, + struct list_head *uf) +{ + unsigned long ret; + bool writable_file_mapping = false; + + /* Allow architectures to sanity-check the vm_flags. */ + if (!arch_validate_flags(vm_flags)) + return -EINVAL; + + /* Map writable and ensure this isn't a sealed memfd. */ + if (file && (vm_flags & VM_SHARED)) { + int error = mapping_map_writable(file->f_mapping); + + if (error) + return error; + writable_file_mapping = true; + } + + ret = __mmap_region(file, addr, len, vm_flags, pgoff, uf); + + /* Clear our write mapping regardless of error. */ + if (writable_file_mapping) + mapping_unmap_writable(file->f_mapping); + + validate_mm(current->mm); + return ret; +} + static int __vm_munmap(unsigned long start, size_t len, bool downgrade) { int ret;