From patchwork Mon Nov 18 16:17:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13878822 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 C4BA3D44009 for ; Mon, 18 Nov 2024 16:18:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C49B6B0082; Mon, 18 Nov 2024 11:18:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4742B6B0083; Mon, 18 Nov 2024 11:18:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 252076B0085; Mon, 18 Nov 2024 11:18:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 00ADC6B0082 for ; Mon, 18 Nov 2024 11:18:52 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8FC9EC031A for ; Mon, 18 Nov 2024 16:18:52 +0000 (UTC) X-FDA: 82799721984.01.393CF08 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf07.hostedemail.com (Postfix) with ESMTP id DB53E40022 for ; Mon, 18 Nov 2024 16:17:39 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=C9Mw7j1y; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=e1FzDHBy; spf=pass (imf07.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.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=1731946549; 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=n677DzD/vofqdaHKQpPiCq3teUv8/eVlhrcSr5BSnVM=; b=lRlx0WtinLPiO5LUnwW5ATgP75jdcDa5xDvFuZzyNrh86yjIuzoGgHXzVm8HS5QjD+1dcs lMSbE9McxghnJuc2FSzAFtdaJAN+uiXGFV83UM6nwohInq/GrtHrE6Cqkc76o6eOLJMfqu s+6UMS44Ie9J7FfSwRCFl9fF94M6zx4= ARC-Authentication-Results: i=2; imf07.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=C9Mw7j1y; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=e1FzDHBy; spf=pass (imf07.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.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-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731946549; a=rsa-sha256; cv=pass; b=vJdFrAOZy+jUthDnhyKmdLweq+U1R0RnQ2jRgU3qY4AlEYq6c+GVlycs/t1tEjVMp/rJc7 nFEE+rFwxwb0FI7xv2Cz0orD0RgASiRki5SawlPioYLG7EL9uTXkj+PNyg0nRJCgTPiHQK kPBwkOwYJBrgdatlyQDrRdfpfbVF02g= 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 4AI8R2ub011100; Mon, 18 Nov 2024 16:18:34 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=n677DzD/vofqdaHKQpPiCq3teUv8/eVlhrcSr5BSnVM=; b= C9Mw7j1y4KtZ3qckz3L2nDH5GF7OUQw7njWsmGRKwmfE0vDlC36kRaKvqsQKlPue ckEdFkLcC+AjfPbsNBFw3QA6zVfltn2Vsp7MozjM2YyQDVsa4W0W/kD2GvP7/zZQ f1TpxHAm+ZQUNpY3xhatSVF1cddPxCiNOBNjixjlNd6PBigMjhnUO3tlMnLl8yIx e/A97sqmQCj7QHrzOb8X9TelctkPXRXAe4wmLKEGCK7Ma5L01rjdC3VX4DAcxs7G RyN4y+MpqlHSD9Kjrz0uqsCswmi+6l/+0ME5XXdEFy1qXmv3q+SLnVxStEM6HbOp VMKd43tWWDJLsELDQjPwBg== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42xhtc341d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:33 +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 4AIFPi6R039269; Mon, 18 Nov 2024 16:18:32 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2172.outbound.protection.outlook.com [104.47.58.172]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42xhu78bdx-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:31 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=l6Lkp3Phv2GNO1HWDoSqAkSi+feWCIJk4sosigJGjvO2sFzg9HR1/vYsewBW38Td1GhBCATJwE38W+7w9P5dlAaUrkZrH8NC7qP3KvmQ+8HSQ/YdhVUUNRjenm5yixQPQIIsjiicJm8ym8jVSEgk1S34EWGDcWQhe6bpxsjByr3WM4UCV0pXvhjtFAWqdYBuoA+EiwzIgj68Zm0p7N+j1dlbST26hFvAbv/xKl3uy8OnkonZiCW0xaDzSbhVbimRApikD/5EJJt7DJGq9QcH9H0BAygy4CRfJ4osoIOHI7XH/3MUjYfdLKxJrpyBLs9R5Uui1iiqWVOnzb0XiYhpTQ== 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=n677DzD/vofqdaHKQpPiCq3teUv8/eVlhrcSr5BSnVM=; b=daYf5txv0qw2+KObYNtBXUISXjolKfg4dEqrxhnMhW7mI9uolysBfqN7umclxxMtzp98/C+Cd0036HlFPXCqZ5W0eDcFXAHMJDMbyhTLvZ2OOGT9N6s8NCIWRIf5WzJDi0RG7OLKh8AyI/sgVLP5Jn/qZjj3BYbwom9WvpzSgawLwVueBUmPqFBKnq4ftAVbjfRN6hUSiUfDFEzx0sNLScGT3PZ4kMJIYsAsfEp1ZXsaxn66KaKPZgUAe68JH8FsgJFPes9w0ICbvtaS19CUxRdMOtdGtEBJzTHuCHhwXyHE56ipZWUPcrwoXaXkZ8+mbVY72hJm9jWYAaoh18REeA== 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=n677DzD/vofqdaHKQpPiCq3teUv8/eVlhrcSr5BSnVM=; b=e1FzDHBy98dxMyeMfON+V8NSVZ/GSY5GMzitf1KxasSB7VXwK9F6KowQFemA/w+WV/itDESfwVJGcN4yIRp9umDtoC/0P/pSy8e6hqw/3zzaBMtMDvSuPSspIdBKPIxPaus6xGV2vQ1F2Q2jmre2h1ll52NmGrdNkUgg7fpDYAQ= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SJ2PR10MB7619.namprd10.prod.outlook.com (2603:10b6:a03:549::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Mon, 18 Nov 2024 16:18:15 +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.8158.021; Mon, 18 Nov 2024 16:18:15 +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 6.1.y v2 1/4] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Mon, 18 Nov 2024 16:17:25 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO6P123CA0012.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:338::18) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SJ2PR10MB7619:EE_ X-MS-Office365-Filtering-Correlation-Id: 73313726-e6d1-4e96-20f5-08dd07ec9af0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: xLJ0lMkVIz1E+3mcPoFq2sgaVaU8NyJl/TvdHQ+tCIeqGCkcvnt87puxwfIMRr7ZUK+du8w2Am5QAWB6lUrVlfDW/d6tuk77Z6QuGJx/yFcohOIiFKBqLSYyqVEGEOpHaeFivLWWAO08qcLUQJ0bUjHLHaBA0ad8cGfmRy11jswpVUQQ88YctdIFcme3L5AT302Y1ry277tTR/65DPB5x7hRIGr7Fvmjfgiks3VzKBLMyxX2dS8vmeW2Tb5k/vbnoTZBn0+N9nj91WJJQ9RsMVKZca3FnOmBEKULK7wR9+gvIgguRnO8F6TCmRNa+qIC4vwowMnGy9bb6E7eiw/yh5aqjnNxs1YXSuzILwWGNjoSG1E5+2utuOLx697H87icFlW9PJhwRHbb1nR/mpjQiLDwr0FNd/hjlyLpsssKqW7WUGNKZbFIe8fDniYsQFcvqdG/BcgEqIQ9aPogvs8qmPvZB2ZlAdM75+iadzbvRJ57hnAOFFV19C1W5itqyNL2s6rh8ccEjmhkVKGJMlqCZqUZdTLj9enXekd2PTgaV8EiSSS4/foeXOXS7fKopXTqWyaepzi6Dy0g2gfoUdd/CnLWI6YIJTVi5kZimnreL84bnxA0Af/JzA/BREVchHGI7bb1mVkshY9MkXkcqxJALTcd2vZ2kHsTGxSaWtplCgU/YJc0C20ORViYeXkFQuPNtZE8k5fBuh0zk+57NBhn5fqRMZV2Q3EQ1pRD/NEHzIOrIVLn1Qwi3NtuNWBcRai62vdD/utPTzaZG6CjiAQP8639YEF2Q7swttML4rvNMz7DZ/T1kqgPfy7k2o1OP2K+Ojb1Myr/62AipSiJmfj21F12TEkhHGflIkv47qujrPkAKRa+r128/bvnl/R6+R4zGP5awhxHL5BWN6oTK3QA5lHTx1X6Al42FLhROOm2CdoUjzvgQn0gpr21NqP51hb9FJWtczITPgmi0sfRE7gxcCFiYq4pp5xkkZbbUvvk92BhpqZWJFS221/4sJgvveERHXJ7OrgfWfZ6jbCTvSJai93QVQZyoD007nMvinbtdxyPoMSg7SDtVFKgLUJ8te1b3oluA6fkh5fopFElaHChdvAFqPAowLVUldxrGX8yAqt2OoLYvZuKlpXXDGAtlkOIAuZiRfFRVqQ4ugNicdomicXQ5LBHzfGyzHMgYPuQBTJS7gIONZtKwBqgK+limXAKREuJMJACyqk929JyyC47M4qtEsbkx6w3RD7ZzTsV8W2KLbhZKtcJK6dDBIsAiLvqQDGOThlD23tK+Te7F9gmn4rmbPWEvxemQVHOg0s5XIMLlMVc4uZkAp4aQvq2qBbb 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)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qwdCXglZeY0MW7NO+1Nd2YxlrlLRWK2EpSl6/HRFIQBNwwm5nNxoXY1uDI+Ieb5OUQKpujsL9DPw7aBlfL5am9Uf0nDOuZvWFiDHtFAvMd9lC+CEGdoIHsJmqlh2Yk87tySjFbubR9bXNfefeHR0mwpRaEYSOQUVOjxiLLWfSqgIdTKAfiQprICVqJBcCd/Q8BgBgdhZmkvjEIJAsD+krd2BFdB+sTIf9p76Ba72ylJxFn2KSLH0W7JUxVvxceqfR52EnhzOSPhrEoxUG24ZoWBqbtXdm7LO6DRRK1Bhqt8vFwdhqrDqTbTsVL3ss/s8iNP1+bugFBHkg+pwqRjP+hWO28i3VD0oY/16psS/eKf8AqXLWuaoa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VGDsaAea0FTFFl15tjxFRCvVXuUhixl13ZRvhl6ofLGo21ECNsC7bjT7tlSJCGhfBcy1aS3ht99lmRfU8adrq/oSI6+K0LuJotUEZWKiWNWTn4cJC8vDwVOUiEx4y9INu+cZfIQf/LgAm24U3s1AjgieySp5zcSK9w== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: C9PvO/plObP3PHclyxX4YjwjHhUMr+npa9MCysNO5F3ZTSa9z5LQhANNUN74ykqiVaZYLMLHJSv1mVA6DFhvv5mINtvpEdTyc9Gvq0dOc67BmZS3y4FdVXborE+3p/FEQA4LN2zh/5tUDII1pwOgp23HGrJA1uqAioZ58e8qVzGrsxjozYwRyezJb195hX6T/ghf3bIV5gAK8lE07wucMuGzbHgdoFbBl8GQWHTjiZpA6I0AnQ5a1bgpMxpQ4qVcgQgTosDxdmYguogPW5acPtG0iTJ2quThszR/V9/Mql/yFEepp3Asyty7NvB26Tmx3j+Ucm4bRUJ/mb/brb0wO5a3tGnhtPLN0hJ03/W4bh8K/724ejUjAEK12CJl3S4PyCnWM2pSNRAnOo0z/34tOWd1MOeMtoDoh+B08oU5sazussp2q1RiUr/En3Nj56fDJ0qspcLhkTxm4tofXLyNq3ngPpeF6yklhPdl52mF0/nGHTSwwtUu2cY2xv6siNQvo4vn34HmNqJuAy8jz19Z9Xt5Gp0Mj/G+UD/45KYTL68kxN8h+wdG+nzHUFI6iF/+SbGjb1gqGdzBVuPCCvbGUB6cgPep3BSncCrxdxGpUAo= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 73313726-e6d1-4e96-20f5-08dd07ec9af0 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Nov 2024 16:18:15.2244 (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: 0sdrATx/udyTeQ2j+bXhdn/r62RaKTdzucerfkxJyuXmBFuGO1ET3J+Tkiw3jg9az0K/Eq8bqbATo7HQknuffHuwHjSW63cf46sCs5cTqys= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR10MB7619 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-18_12,2024-11-18_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 spamscore=0 phishscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411180135 X-Proofpoint-GUID: qGUQBavZK78VVyz307kCXBkpWgCsEw4l X-Proofpoint-ORIG-GUID: qGUQBavZK78VVyz307kCXBkpWgCsEw4l X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: DB53E40022 X-Stat-Signature: o5u6buyan1egqpcomkbq5jwqrhmjpqrp X-Rspam-User: X-HE-Tag: 1731946659-609971 X-HE-Meta: U2FsdGVkX18I8dIPv0xusm0JjmArrcNzM779/R0jRvXHEJshQm1uN3XdFYIozo0u+gwsNRksl9pL6fTq517Pf3DPzxMmdLKNdUqiLdD0vUjvqmQIBEE5wSCZs96/C7S/Hl2I6unC4DufOBJ9Q0NmXidwknl8OglS+E1x8oMO7i0CJOoTCEZgWlEQQAccw3PXfiDmnlqG/ffSn0owGDNB1ONMBt16n5ZeLWlmdoakm9Yy8s5mlJIKXOBGgDaFfd63lJ2EP5QOdmYmd+C/i8I7B+c3TruTQPi9UkAfeg0p5Pc3aLIVki2poKZJ62Pp2g8u19Xv9G5qLxMxmiDbRM7qBhCq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b7blq6y+gjkjYQt7IYytjq8LCYX0gE7H3hb29roVp9F+0e906XNkTXzf0ms136lLYmWacId5Xjxg8Yfwid8m/F74LQ/w6Gqp5jyyZlwlrTyE0g7N/NTTSJ5Muig6oZAvgdiLkQH60lSJLYwlvzAww8+GBoVxEg84aLyxAshE8Ss3JxP9n/1eKq1zCRGPo7HUhe3hGHca8zqdFiVHZ41nrEAF7p5neioz92M0Cl7IQ7HOMc10WE1j28KE2hF49k058iJrcm7tN5ruehTTVtMgcIQgCZxmj43y9L009T9RcvEVJpBGH6yBQM8ISySCMwQckH7pLoZlalQTNxkil/1w+KMrX6w5mLF6fp9l2HIMF5FVaaXmA6mdlwWJygNlyK7+ylWg8dfAm69UfxsBuk+c/UK+07mhL9Uq1V1UpO8a/2aqDmum0fXSvWQq4AN9Puvd8CcIUbpYmFSIUfGKzLJry05CYMZVyXiekaEV3rAf4FgIyzOiL96V2w99cYVOD6quHd8r1YUNkLwSKI= 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 a50bc08337d2..85ac9c6a1393 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -52,6 +52,18 @@ struct folio_batch; 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); + static inline void *folio_raw_mapping(struct folio *folio) { unsigned long mapping = (unsigned long)folio->mapping; diff --git a/mm/mmap.c b/mm/mmap.c index c0f9575493de..bf2f1ca87bef 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2760,7 +2760,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; @@ -2775,7 +2775,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, mas_reset(&mas); /* - * If vm_flags changed after call_mmap(), we should try merge + * 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 8e8fe491d914..f09e798a4416 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -939,7 +939,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; @@ -970,7 +970,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 94fff247831b..15f1970da665 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1103,6 +1103,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 Mon Nov 18 16:17:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13878823 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 9FE84D49207 for ; Mon, 18 Nov 2024 16:18:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 255D56B0083; Mon, 18 Nov 2024 11:18:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B75C6B0088; Mon, 18 Nov 2024 11:18:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E11B36B0089; Mon, 18 Nov 2024 11:18:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BD6256B0083 for ; Mon, 18 Nov 2024 11:18:53 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3D40E1602FA for ; Mon, 18 Nov 2024 16:18:52 +0000 (UTC) X-FDA: 82799721060.20.E838CD5 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf26.hostedemail.com (Postfix) with ESMTP id 8629D140023 for ; Mon, 18 Nov 2024 16:18:11 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=RxOKnzO4; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=o7NMkAh3; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf26.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=1731946663; a=rsa-sha256; cv=pass; b=YogNQlIgNrOfYSwZ/KryZyKP9JIglEDsAfOhHCQ6k3QUdkXJ/A1FkbjpRiXIMcyt42RR3G KsefSOwlI00XZP2LhCVRa+f1UMZruu9jKw9lGn+l6INLVvjumy2mYA/ndXCjB0WflNxsdr sA4thJsT1rinIPBSauJmGY0twfrxYXg= ARC-Authentication-Results: i=2; imf26.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=RxOKnzO4; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=o7NMkAh3; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf26.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=1731946663; 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=OxWMYoiN4etCWEyz5nY0Duc+cphdupWfvPon5pvG3ks=; b=XwHV9vmB3lr930bFi9l9K9jGoAoamvvcB5v20NnR55DdHjmU2DN+Kr2pMdiSjbbAg3M0J6 8p7UpJ3huiZZ1Y6/RM1Mvl2rxX4rDfPteKW/zvwscW02kGL0fsNvGGDcAYX5qMeTmCwvyl yu1PpRZGD856b6Ox+/PR+yAEdjel/TM= 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 4AI8QXZX032645; Mon, 18 Nov 2024 16:18:34 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=OxWMYoiN4etCWEyz5nY0Duc+cphdupWfvPon5pvG3ks=; b= RxOKnzO4cNgq8fEj4WhXU2d6X9MJmgpQKqQePnp0jyZ+AEXrVgV9fRStYO8eDjOx BOWXaS/C0qCMTlxZO+P4DGqW7/GWlxjvVaurvNvo7lB4ka5Ag9jIswYssMBkb94k UhL/oYW0SMm3OkxBkzUZy0/YAbQpoOSU012ai4hoN2ddunFc1Jsvbgy8gUiHzTww EJCS1UUKdbCPEcEbWaqHN/2ezOarwigumoPCl0UwgQv3lca6WgANsH5qOgE2f7+K zIdLW1IAw4dVniMSqAzbwhZBnHTuRQAhWaqBeiZUjtak6bMIbtn+R6pbqpqKWiok n+7djNborFkpNmsr31zNsQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42xk0sk2md-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:34 +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 4AIFPi6S039269; Mon, 18 Nov 2024 16:18:33 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2172.outbound.protection.outlook.com [104.47.58.172]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42xhu78bdx-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=r/eYt9oA4A979OouXyh6k1OmeaOJC8fEUEess0uo6CrOAot2aDgORyokE0AR7hnXumR6zkAdFLE/8M0fJxkYCsLZwiLo5hHRtikqgKhIVMsA/F18TPXPUkyrfVJze+V6dpk75qQcymGdxiFTntNuGpvTqbcDH0PtJaHckntJBDEwpprVPBF+ivKci3Ugge28IJi7olMLtKc60oPYQbt41I3zkS8LabDKhPzBoK+Lbp9Ui6cMZY38+lQgij1wev1mQnvM4UK9IqkGbHiei6Yv4+AzIWg4tBfl36nwUoaLIu/6jzsSS+qBQDts8lHkDYwhKfR3b0nO7q5emiG2p3CqEA== 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=OxWMYoiN4etCWEyz5nY0Duc+cphdupWfvPon5pvG3ks=; b=CKTEl0NQ6jLfPYFQmlqWeT9SsCl/nZ3kZGWCGRXhiNl9ZFTson93TxeN6KHWVq30y7ckx5wOOLLDMelyHo+2Ho4RNdu5UEeS4Mr0OuwXaRUcc0yfoMHUQzAVNMo3+dxYKLu8oyZGlW3GIuPY3JoEfUcE4Uosg/Q+uAo3H6vYlYJ4Sd2HabSkPIcKoU3wUuay9qknRbUUhRCuSpYm0TLwt5cpSv73Y+RrxwmhARDyrDTtHxkrSBr2NjhW30OZ8rAvNKPbDkdKbJcUwPYQwDhmnNoehLFxXPhozjCgW3mwI/1qDr1xIk8A0s2vubvYYs4uPLUAnNJDfJg8E/abxQlBTQ== 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=OxWMYoiN4etCWEyz5nY0Duc+cphdupWfvPon5pvG3ks=; b=o7NMkAh3wb8L3t8FT/HsQr8poWe9WdIoX7dzNkq+W7H46U77aSsE/Oyy0KBIvyAl0LTmuv4SzXge74USpbH93qoK0W9hWfl9a5Ne5f0yXWWXQ8Ex6iLktvvcZ9U7aoM7Vfjg1RZ7xnKeBmZgLMwjOYn9DoVg8OrblrO7CEgVKrw= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SJ2PR10MB7619.namprd10.prod.outlook.com (2603:10b6:a03:549::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Mon, 18 Nov 2024 16:18:18 +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.8158.021; Mon, 18 Nov 2024 16:18:18 +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 6.1.y v2 2/4] mm: unconditionally close VMAs on error Date: Mon, 18 Nov 2024 16:17:26 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO0P265CA0011.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:355::19) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SJ2PR10MB7619:EE_ X-MS-Office365-Filtering-Correlation-Id: c4b2c6d4-44b8-4cfc-ffd7-08dd07ec9ceb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: 7zWCD6+UaHc1dDXaCjxH/JDwvw1Xbrd/hVZVV96BmdyEQCIBLlGRhy6IiWpoDsavrNXNMNGI14Y1W7w7A7gWLuROg1BoC+9jXgEh2p/gtYy0gg5AoUnbcsY71M8jKeO/MlLlZfc269deHgzOjw5WOl2QY9HehkUfh8YxQzBe9wVvgea26okiDCztX7C4Co7zEwlBmzVoANCmeTR5oyaCaY/Q02BgICOOWAWJkWevk8GVzgQ3xu8rGko0Wb4ri1HcuT+tCrk+FajpU9MFDy+FQNvNF8Jb4A5/b425G+0oOdcrRY5ws7ZOuhWREbS/csadlgdzun7HQwQqyDUVXQaH+ExoEuffoS7/vC0DzLacUx33nAmjyyktEAxG/K0CEYHkh5LIfflsUuAeOhdE7633/ZZkrXAzHh66aU2HCBehUJUQEzoBCDwE8GFWZN/NrnqhW4QO/oInt+Bvte3ffk5vOnolm2ucwDX0IhkLWvmGF3qxujnokPVf8Olzi0k558tudnqbHAIAHpF/C/gCRQWBPwnMAvGAw19ztBwQPIan3vR6BQFf0qVRv4Z9fEg3jhoHRO7YHbDS5WQWLehIV0aawEskAC8iWi3RRZpXkwrypaHRwlIEsVsuvvwKgUDmF5DqdvHEKndknxM0letcJM3/Sh0BM1DT6FRepsnFQu9YFbGFV6L39cD1jc09BLvvzVjyJd/9HrBpZIw4pE2X+YNzf36yfpM/Fo7qvltDluAvdf+hiQ3u37gqrj4QHYK8xLu28t+Iqlu4GhsgpK3bxDWRRXrYKBpV0V6iFAF4RboE3g2/BV3kvlFmSe79KfBFmFzjM7v+yhuRcnX9NXZ0GLInJMynxBPMslIWbsksS9ohOK+qpIz2wb93oeveFms0ufyCcKQemX97PjpXIdJsjMgMCkcDLQfjaNzszwkB3EJcnmKRJw79DFoPL47XY17jE0QnUfCXe2CWwzK9iRREV4NlcoK0NGMULU3MPYcdtoNZLMlbDUxHK+PIYu4n18mSwuYl/D9ZPM0jh9R814lBp7P0lCsfMN1fp3ZN7Onhi00clUwQhy/nty1yOIvPmWM7gBzssgHkHr8cJobOXlREgk5jMKZpdLheqQhDM74NLOMjjhfeMh8xXEKDXbkHapxRYIVtNpoMCjwOBRbc1ET8E9s+ZqZMGhASAORVm35+kgqMmpmsfuSk8vvFje19i6q+ysYpW0xuM/ibt2g1Q5JOPE/8PeMfui0aOvz/oGE/wBjk6KuOnCnaLgMR8cq1jrQfrIAE+oCrY1/TIVdGOZWC7Mzalxwf/jk1ZkBnMQmWXP873N8cs4Kj8eKb0cF9th+iRvFS 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)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HPNvWGweDdconWe/cD2Am8iUoEb090Hm2VJh382ixZhqTQCe/q08Mwi/O5U8ex/JSqsepG3T2z7lbI2/6xJz7SPn088Mi7VPdYzHv3GkfHYurakBQV3lR9SsUHClv/dpwZMeswacIwSAfPtwkvfUn7InvMfBM1/z1YWmNxExy3OjUiX318Wk4dvrOpLFCiXEKUuAR5vEhXsHtYmgzKexRnjYiWzgMV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xHuPT3YVlaX8GhV+ISTpXZp1oyGlMzWs+EKMhA3RPBYQyAC1SRYAbuBzGFucolkshU+uWIR1wCERKJjLdPf8Wznk8aiPGS3dnSOLoxu0VCzrWDvKG+xVhnX8uINnkTX0V7gRTEvueqpxTYnAjKLoTCAnjfmCR3ePTg+oX02/NCfLqAgjNONBbD2ggLM3Sn0kmeyS9n0Y/pQLzy8B20YIT4hjhpeipBpTbZ6bvoG2/bgqtSuP8p/+FPvap0HFpum+OzT9KodEnOy9VL7hGJkMe4Lmz96QAn4k7Z2pX52gBYzvHswL64+O2s6nybMQBfaB3Q== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 3gLIGuE9OvP9/BruG+eWWYIwTkt0QIoOPzX88B7BNAJU2/xZLLyVGAx1nkBbAW8Kgmsr9AtO6AJOIBqaEdtRutkPpUh47yzZZwQMkHWT/+ZjEvM36ON3T/ja7yqD5oirTWBUp5EPbRMb8FCSu5i0Y5nKXuhN0XyLU7s6n+FhLJXBiArgS1Wo4SqUS2t/POM65TkhL/Eel+noUSNn0PPHCNKGh+i22sAPSscXthcBZd1hG+AIEdwB4TKxKHkq7Lb+Qf2GqpqtaEfKdZ019wz/OwuRB2xsKxdLtl0yU9fSazLTwyaYC0j7duFEAQvco5h/4qP6Wf/lmOgfp+UoHGOYWxGYu57wWqYtNE15E3Zru9i5o7k2L6RGidyZrniynou5AfLyfzCYpAMhqDQVcrcadvlDlJp/N0w8JPg4Hd1Oe+dOAxFLO4s+nbN8BflST3oL1/nGriK7QX/iV2AWQFXGjcdJX/DDbpm45dqdUKH0XOyyyvRi/SBjHkwsxrHZG05Fma6QRre614QdwxasPpWDXLtx2Y7kxrv/AqRvq3MTpOCVSBYiYvUgrh+HKLowNBlWSg6gzu6nUZ7dpNSDZ51vM+Ll2A7Xg1KZL4bV3XHZpRM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c4b2c6d4-44b8-4cfc-ffd7-08dd07ec9ceb X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Nov 2024 16:18:18.3789 (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: sdDMH4QSpRrevIbgFXNn0/34dl3kTuMyl2W1qGSK+MGXCbfB2HjrbqFoHufA4aj5Tp7+pibeHAIRmz7pT03BuN0zqFbhPOvfETxP5aNJe28= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR10MB7619 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-18_12,2024-11-18_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 spamscore=0 phishscore=0 adultscore=0 mlxlogscore=999 bulkscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411180135 X-Proofpoint-ORIG-GUID: uJBBSe8JciJ7_GG4USGnHHdOOdYjG_tM X-Proofpoint-GUID: uJBBSe8JciJ7_GG4USGnHHdOOdYjG_tM X-Rspam-User: X-Rspamd-Queue-Id: 8629D140023 X-Rspamd-Server: rspam01 X-Stat-Signature: tqe1czq9wa7uirkdgtmiz6ai9pm7u5h8 X-HE-Tag: 1731946691-616269 X-HE-Meta: U2FsdGVkX19Aid0F2ACGLSw/cKE0aJUAk7IsSmnAmtlsdTyQ2be9fBOtgzCqaIjSH5rs565/JGWkhuTRrF5Nqs8+cKIm3RpGLv3ixuspOYD7k6IEoGWclT6C3//xjgi9SvCjYWMCNLIgV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L8E7267ooe2D36PALSsdcX/Tudf2SX8C0p4DlUMZ/xZMC6oU60dc8JZIwftLC8kOD00DM6L3sDPuqMI9DHRLS4VqhD1O2NePOjM8Pdqb251Z8efc27zcIdxps1UzmkDuCW6mm/c+1cll/LoQvblUBQF8yqDaJMlz4eGSuff82mBAjXiSJ5nRy5+jl4HIfUx01+src6FId4siBZdxrOF0KQS/OHKSiJkaR8elgd8rKc8ioBOejjLLjnksB0Y60sxcIWRzAqrws8G+juxk1lN+N3O+SyI723toqXRFwgSuVyJsRj3uQuzKEWwlm7kteRElQFyAgspeD4CBcOxbMUfv4KfYY1oDU1bB2TxzSZTPLPte1rX8pnMjp5IEfikcBY3UUEwGKM+KO7R56KUixlk4Jk6Bu4bGJaTooQkCe19ChDTeijKgc9LDseAOEYTC4jQPm1cqRcGaMIFglkEyvEyYwiendNbw8pD+8DNlB0zaoQPPwTy8zuOzSq6X+bAt+G9KGKldhvP7mGk6Z0JPP9VyeTHfYw0o6ZvCl02RfHeEU3I+IsuX14746EjXaOYH+oxPTGk4juKRvHTVHHj0TW3ESBwEov+IEFoo8OEF5fQwhF+TQbihUsTeiyPSHBxFUw7iV7rdvi95l1XRFVeIxaORIxTU8Hd0fWE9g02UhgrXt+BaQXXfXoq0SLIXnxyX3MirioAuIA= 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 | 12 ++++-------- mm/nommu.c | 3 +-- mm/util.c | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 85ac9c6a1393..16a4a9aece30 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -64,6 +64,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); + static inline void *folio_raw_mapping(struct folio *folio) { unsigned long mapping = (unsigned long)folio->mapping; diff --git a/mm/mmap.c b/mm/mmap.c index bf2f1ca87bef..4bfec4df51c2 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -136,8 +136,7 @@ void unlink_file_vma(struct vm_area_struct *vma) static void remove_vma(struct vm_area_struct *vma) { 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)); @@ -2388,8 +2387,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, new->vm_start = new->vm_end; new->vm_pgoff = 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); @@ -2885,8 +2883,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; @@ -3376,8 +3373,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, return new_vma; out_vma_link: - if (new_vma->vm_ops && new_vma->vm_ops->close) - new_vma->vm_ops->close(new_vma); + vma_close(new_vma); if (new_vma->vm_file) fput(new_vma->vm_file); diff --git a/mm/nommu.c b/mm/nommu.c index f09e798a4416..e0428fa57526 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -650,8 +650,7 @@ static int 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 15f1970da665..d3a2877c176f 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1121,6 +1121,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 Mon Nov 18 16:17:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13878825 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 34438D44009 for ; Mon, 18 Nov 2024 16:19:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7FD026B008A; Mon, 18 Nov 2024 11:18:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 70C3C6B008C; Mon, 18 Nov 2024 11:18:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 511AB6B0092; Mon, 18 Nov 2024 11:18:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2198F6B008C for ; Mon, 18 Nov 2024 11:18:57 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D4FE31C5478 for ; Mon, 18 Nov 2024 16:18:56 +0000 (UTC) X-FDA: 82799721270.20.DCB9619 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf25.hostedemail.com (Postfix) with ESMTP id E7FCCA0003 for ; Mon, 18 Nov 2024 16:18:17 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=T9eoTatX; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=Mq2Ai5QQ; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf25.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=1731946667; a=rsa-sha256; cv=pass; b=ClQiyUeoeOMcamEb2yH4WKQMZJoHg84OX0NGuo6UG72Qn3qLEakTckEZR9yWC8jtDMy2S9 CeVM/FVufyfrlwZqFHK1GptPtqXpRovSlkfN0Qui0AOFpBmV9F8bDx9wd5iCrwW5X7KCnm ZgldxnIeAJirQbv1QeSg/Li/yE3LNcE= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=T9eoTatX; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=Mq2Ai5QQ; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf25.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=1731946667; 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=DQOybQXxm8QK69H+ekYsnrRu/djaUo/ByvscuyUxD20=; b=pnQ/CjgTyo5uCyAs7LmbV9qthzFKcLvNlaqTALfhb0k4/COfuIqMgV+PHetl3GM9lrmqL0 xfOQ4NdSEvZ8VI2kEiC/0kgaEtkFu4Yz0UMKY3zeLlaFD9P0T9TFgULjbyZoC98WVPiTNW wNoBjLBc/X+5RANlcX2AkCkmpi33ZzI= 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 4AI8QZaD009482; Mon, 18 Nov 2024 16:18:43 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=DQOybQXxm8QK69H+ekYsnrRu/djaUo/ByvscuyUxD20=; b= T9eoTatXzPU7SqlbkX0UOJbybDURIDq8nYa634fCa4wg5mLq/SHVgHqE63bgvVYr RF/1StqRGgyg5as7+xCVTWYhydeYt3HwM6rgBPxtCySipc/b/aGzd4PW3/j6ZKpR McBYTJureTeayOym2f0vsqMZloilZ8ClOWlqoMGRPjXqStPG6bqxQ3e1iOCTPcBO ARcgrao8gJnADYYWcKsLVngwLkg8VZq9bZRCl4gMLmLicH/fMuE4zrPShU7H9fue vmUDhqMCqLt7IXcJs6A4Ziod23Y70ABkWvr2WdRdK1S6k2jOPGvly7ZijIH2RgOa Dnp5qh8qKjrZi4Y1Q5QYvw== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42xhtc3425-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:42 +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 4AIG1G5k009102; Mon, 18 Nov 2024 16:18:38 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2045.outbound.protection.outlook.com [104.47.70.45]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42xhu7gxfh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:38 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=a9kGyHRfaA05D4JMCzfZfqhSZGXtsJBSNj6hp9i4pl0mHOfihSiyc1Pb4T9nz4BsydxzJGGdIu+5bpJUnssC5Ch8ARSwGpbOcJs2ml44yb8FlFSgkHzAlhQdIEQ9kPpue8BWDum0ckxTBAMNpRN6E+J/e/T+OhOckBGmz6xlYrPNxlFQ4+wFBZi3EyY9sPnt8x9iZR8N1PKMUao1HvGMKJPNB6RcU01xOiFqVBYFVCLvN3Lu30QReKbpacCEQ4kiAWUh7WNMNIqXzitRCpGoe2hNRMjucO8+InrzgWJDpE1ry+GftLFkW7EGsCGb5OCa0LhWDvUtDeg6CeyjU1VisA== 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=DQOybQXxm8QK69H+ekYsnrRu/djaUo/ByvscuyUxD20=; b=LzZuJGd0vT13990tUQNhRLIZkUyAclpavRltVcgLBOZlc6KGjY0CPypsOZZ94dagq+ZpGyZ56361yy/4gmcZOZIbuYIFI/pEzZR6qCxyBw1YTEdJ6oFN6Aj7CNz+kuG+Twjs+DhS2GfajhztR7kl/oPr8kSzAGs6UTbmqZFVSexAv8UzJoBMrUdpR5tWvEn6dghebmk21Rqnby3ipF4QxqkEf+dvOrLnBPsHGbJYHqVZUTFDP5gKQDvBsCOuZ5kUv+vYbIfKP+bErUdf2GzZgMJJLlms8ikW5cAOKIBSlS7GNlz8JGfRATKxrx6D+ylniphNIkv+Xvh0lsYDXesAOA== 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=DQOybQXxm8QK69H+ekYsnrRu/djaUo/ByvscuyUxD20=; b=Mq2Ai5QQ5d380hUSEmB19YxNwwe3CwSHYhhRTbRtmhhIhLedELbYPmke4PYDZuG02x+ibuGMZ/Boz7j63GKFkbCtO2rK27FKx1R9PW1L8uP+IHKvah8o90unqJuZH+s22pW3WrnlERqElzQODfqg9ZWk2RTw+hg9SsvGS3V4Yk0= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by IA0PR10MB6819.namprd10.prod.outlook.com (2603:10b6:208:438::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.23; Mon, 18 Nov 2024 16:18:21 +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.8158.021; Mon, 18 Nov 2024 16:18:21 +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 6.1.y v2 3/4] mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling Date: Mon, 18 Nov 2024 16:17:27 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO4P123CA0044.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:152::13) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|IA0PR10MB6819:EE_ X-MS-Office365-Filtering-Correlation-Id: 72dc83b8-cb10-429e-c549-08dd07ec9ebc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|1800799024|366016; X-Microsoft-Antispam-Message-Info: a3RY11dY+5cEUnk1B2GHk1ZLueSJoq/xxZyAOgoEqYh+L/4msjI1Ew8JWu95OhB53yUwGVErFx9vGz1rc/q9Kbhjc/0fUgKFckQz4ntBVu8rM9O6mwDlmWWJm2E3S6SDf1WMbpDMS4hdhTmaZnL1YVN7X5RV5G9gKUfMbTs/gvFt2sLiMuZTKMOBQoep+2NmOic0qKrlsAETZRwQA12er/QkzNhXBMrD4aB5Be+CYMqwpq49J4Xk77ihmK45upU8Ga6F7O1oGdGgjxD3WM0A4+wavba5S3i8RBkJ6LHad4mHVP9ez7zYh18OkNkh6LEc2K1eY8SAB5hZB/YL1eW6bpIdH9hv+kkZOBRRcC6U0WD3lAZA2JkLMNB/+D87Z+0rFp1m+/sefIq90Sq68gR78t839f0imZFMGsWOXx1o3xxDbyVUOVxy5J68/wdDziYoT7/HUa9lXgP/HCZQkyvN8TGiShDFGRqPHREwcPnvEZS4Jriv4POV2SAHKLhnyyehBaqxHQHr6w6VrcxRbFdTmEWWsaGzhEuzaNxyopodqAe/ukiPZ6fDGXB88it3aavtR7XiX1hDJEVFDKPrlZfpBuogyQkr0rkcaOBzypjcJKd6DQW1215WIxNAVF2w+DCHJxbWis+ZuI2Pe9qqyDBRsnzHyKSrW7AoSvnxT/0W7IJYNDtJR8DC9xy11fNavcg9mJdqE80K0eNz0/JXFdnoVHwjiqJyQ0F00EaE/7qkkfGJgKuayIXjSx+v4GoKGST5AfO0sIhefINXdyUrqbViKGgYL0Vtqsro0VfPNspVBnvCsElGt3hhr9PCeKxTSRJbdYTx4PfzKtn+m2h4Ym6tp486bNG8Z/0hJxiFJ9Ttl5vDWfhyEuAjRKmQwG/Tz9LiFxS0YFGEgwTv8B1Dtsfr0/RI/7q4OeIeVPJV+ZzQhmM7KVLWktinyWicJxsb+dzGaq/LynWx1y08WB8nbsERE2e9xEOVK9jSlcYdfkb/Y6Dbf3quSMqu76crhxENJn0gfoRBjYmxSiNr5CTnEb2xiYL4ZNGCbQ5lkFBZIQ0Qz5Mvqf0GjuxDdk4BVo6qJiXmiAabL/qZfQ3Gnb8ILwlYPkXOaFHpgZstU0iFDptxnQesNo+LmZpM8DhGwZqg5bNg0aEpwVbYsMr/vZtgRoL3X90XvRfMe7H0/MaZugYoDFF0nPT+iC4u935JtIVm+MjqgE5aZAMoKubgUVzqUdsAZ212qK2Sus8sUWK1BfCjcd3iNlZQZLX5U209pyBRZJohH5LkQ67XV38H/WaxvSTTR1O1L+wriB0RsrT+IyJq2HWLGgWye7KVZpBHkm0a06NR 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)(7416014)(376014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dH2z1p72GeV6DftdIJ9M/pPUkYlIzWwHAyD9Fet9RZaFzOOk7lHmc+KST6pua5qb977hi5OgtRUqmaZj4qYBVMjRJMkGnyI2hJYzf/8E4IK+K//c07jNiOvJMSJxMAZWw48DvWMknwXVJ4lh98x5xwWDTTFZldWVf4LkfIPm0bsL0TSyIw+RHY7N3Spxyzf2Q9aum2rRxOSghj42bOfpnIYToWkO3gyV6ab8Bu+dBiZ7xT3dUlptXRlbZMJ+CnrWXN1YL8/3VcAQfIf4DsZEYp9GzrTP0aIJrmcRzALO/D49ClkUYeZWHRn28ai69EievT3GdWIF51TJlOonXG8yowgTOsEoOPXDBPOHSJmXMy8NBTMpJHUlZ1q7XpXlCKHfQWT8SB6D9sT6mvcAYavQgTQvnceY0pB4GXWImq/J9/MHEFKPcIG5nApoGpc5YiuaEuUtyEVX9qsRn6PeBMg8KTSsZtYMhAoQk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GOTHvcbs4WT8ijcUufXEI0exkEkZbPdnkN00+GMz9YhtgYWk16Kw== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: maork1lP6r2+Azhgdt9Mbfu8BpkFLp4dbRKQT3sKFeZzQZMWsSLFPPed26+w3cbOT9wqSwwXkK5I39S4nAdDy3hG/G9UnhE18Ak4DY7UGDmY0fJrcjvwbYs8D/kOQx9BVWiIthK0r8PmWYQiFpQCwIhLiLHTw9kogqOU23YT/1LRmKa0ndETAyhw/1lVnXjhg8Qa7+UbOnxq04xZ97LWq/8AFPDBSS4w3jEx1Rgw5H4CoI5OBQ9SteRCjyoaL9aR8it3ph0+gpQM838oRnN4lN4I3n7gddhX3po0XVV0yfi4M3pv609lUoULxWTvL6BTjSuEOVr4yBWC3rbAFdyRusPFkp3oofTyav0ISTpEPHHN6s+JYk68z4Os1KYz4vJkZJaXg/VpG7nQoVWo0HHwoyEfXqCBZGZm8roLm4/EAZKSSR6K6M3J/2I987Py0S0LS1VJwjSx1pH/SzODdpUA0R2iR3mzug2YWcCdDWZFfd5nTLhC5A4U08i4dWwPOaVzior9qqts2Ees1QCQwWSWvuvelu796Af8EcCDo/OVZRO3q03e9Wcjqta85iZEz4JrJykiTYvVL23cLWchleHlOHoDRIirYF3JNTSQK4Slb/Y= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 72dc83b8-cb10-429e-c549-08dd07ec9ebc X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Nov 2024 16:18:21.5963 (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: xoSnbu77s0kUz83tC92MSss03UNsQYyQqWq7jjWYTyy1m60mmLMt5h0PQd7//uvpgHWcXDkrd4YxDNUz3Yg3Kj03tZXXfU7hRqVjNF/L/PE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR10MB6819 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-18_12,2024-11-18_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 suspectscore=0 adultscore=0 bulkscore=0 mlxscore=0 malwarescore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411180135 X-Proofpoint-GUID: M-Q7TO7tHsn5W1cYUrbQ9m-98e7SGbHF X-Proofpoint-ORIG-GUID: M-Q7TO7tHsn5W1cYUrbQ9m-98e7SGbHF X-Rspam-User: X-Rspamd-Queue-Id: E7FCCA0003 X-Rspamd-Server: rspam11 X-Stat-Signature: kzxstw6znu9mfu8or8rjea7wpx6a6tia X-HE-Tag: 1731946697-97996 X-HE-Meta: U2FsdGVkX19kVol8yFw107z8xJg1Kyll2WLui8Q95Q+27aeb2NdIllTniX6kn9F6eHNbTtlDY4T2rhEs9nTvkO4+GSr71WWXELcueIdZGqIomlQuEJMG4wTBx6xjBaDGtsDDg/7cRi00cdbG3csD3/9E4VH2vLM/0kzn8AIy9ZFxt6SBGa4gueV1u/Io0HGR2UhN3ksU/hEcnfS0CGvwzUSB5s6ZgFymSnxk4zMkR0CFylNOjJ7wcog2IGCSJjzgWOWGsmmarzbpycf2e3sSdUwCpbxx8RZiR5wtXuUiLkHJqgybmLNhYX8bnIUMQGT0ss7V0TvCOX0FemSOd6oVTG0xVR0sqk5ddUlCBIKjFtk1MkKGMvN1AAMTn8LdoCKYVQwjgc33y7LGbMn2kL1Cg3JJqmbRv3pQWxiwjLApyLs6IjGjLfkKj3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ia89+CJjrVqaEpC4B3255LePV1vJS8YLEhQkE2kOV1hTHbzLAUiuEZlI6xHIcHN35v3bJmeCu7EThuewhCkHc+Ni+39MlRkXqfBMWi63mlIHe5q0tG6LUgksARW1Rzp0QHEBVD2+J8jTOoXYfr6Slloeq47ynS1xTWK7DE9wrQtQOuCOYkom0vcc1X6d6eysUi9dl7k5GgkeGfGpXq+lEhEsimXbHcrBcYLA5qhoIgYrVHS8+t2zrX8VA8gWZrW5utOlJ+kZjxFuO6khTjeKxB0HkeKvDXidYlXaNvUjhXG95LF373eSJ2TOLBDL1dXMLnsRPmJK5oQlAbPAEEl+LoftvHuZrJ7G9a5Ax59hvgei0yTjxzmEFUdtA8g1VGq73oOubiXQxokqz8E84/Jja57H3el7iAVqW0sGcwumjYByKXnRqxwLjrSAdsz1+S9KRu9B+ +nyR3Z6W 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 5966ee4a6154..ef35c52aabd6 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 bool arch_validate_prot(unsigned long prot, unsigned long addr __always_unused) diff --git a/include/linux/mman.h b/include/linux/mman.h index 58b3abd457a3..21ea08b919d9 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_validate_prot @@ -147,12 +148,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 4bfec4df51c2..322677f61d30 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1316,7 +1316,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 e0428fa57526..859ba6bdeb9c 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -903,7 +903,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 0e1fbc53717d..d1a33f66cc7f 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2308,9 +2308,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; return 0; From patchwork Mon Nov 18 16:17:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13878824 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 721ACD49207 for ; Mon, 18 Nov 2024 16:18:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16C666B0089; Mon, 18 Nov 2024 11:18:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0A7406B008A; Mon, 18 Nov 2024 11:18:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D038B6B008C; Mon, 18 Nov 2024 11:18:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9FABF6B0089 for ; Mon, 18 Nov 2024 11:18:56 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4E8691202CC for ; Mon, 18 Nov 2024 16:18:56 +0000 (UTC) X-FDA: 82799723538.05.B457193 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf06.hostedemail.com (Postfix) with ESMTP id 03FE118000B for ; Mon, 18 Nov 2024 16:18:17 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="NB4v6gl/"; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b="Rk8L9i/P"; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf06.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-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731946490; 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=MRrVHGDRLughqVlx4AwYJKYY2Ke4n4qbuAKF0PLzItY=; b=j1PcedSQ4Dn7cmYUe592Z6pfVkMc0EhsXrFRb6W+TLddhGayW3MtMC1zFqgJzwYVBJAH/S N2knmAmc8naG1+v3fw5LfkKZi34Irq3331OQ/f0rh50+v3z1rM5Yz9lPGGcyC/2+pH2+Wu DKYuukBxyAsuqDNCrWfSrxhLYU5dCNs= ARC-Authentication-Results: i=2; imf06.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="NB4v6gl/"; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b="Rk8L9i/P"; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf06.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-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731946490; a=rsa-sha256; cv=pass; b=aBVMVbNaxsFZccNOYgs5XW8fjYQwyykEMv5IF7c6e0Vidf6c2x3xyfL5WtIIPKEYvF4Won PNSEMMbDgSPgl7rEDT2MkIuMnosYdqV49kA1Pf2vPV4WwvkFGf2PpaneBe9qDGhT9bB3sT WVmOP0DPlEpLwLzSWUERhU6mjnBPIME= 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 4AIGGoai032669; Mon, 18 Nov 2024 16:18:41 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=MRrVHGDRLughqVlx4AwYJKYY2Ke4n4qbuAKF0PLzItY=; b= NB4v6gl/5FbjBYm8DjknXrLGi19T3bwjAug/yVuXq1+GHeikqDYjDOm3glO2yCEg 98154/IOItpmPj7lGNBWMK7B3c4oly3X61d0IxpQctMnHH2F7bCnnrOSZwlmPE3b P3CygkzZK/X2M9+46JJAbF4pO7z1kFnpsFTno+S7WHq4gV3r3rDwjKM0aUpmjZ1z 1QLmSlW4i8RmJZZL2EQ6QNgBNFqxaNxH9WyfJEH9KwV1naHH65UiSfyxYjMpkP89 q5LgK+wQETnnDKXeEokaihL2ikZIWDIkm+Dnc6a6reQ4dE6CM+i6yX+NPjgIAIDJ 5dODssgmKqb8a34azwQ4iw== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42xk0sk2nj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:40 +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 4AIG1G5m009102; Mon, 18 Nov 2024 16:18:39 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2045.outbound.protection.outlook.com [104.47.70.45]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42xhu7gxfh-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 18 Nov 2024 16:18:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cnykYleQF+PZvdoGuZaTx1JSX4mq1pd8c1B+HcxOzYyc+p+mHRcq1+8KYte2VAAbs8ziwkEE/pXIBRc4qo5guteFCmazfOiYY5WO4OULF+uVFymtZPWRQbJvqirOxX4XfMgbYa8J7evj+fHbUn5vHwjGQkJdc/QBfzLTrj5WwHbnm04KP2JacV3RL5KRhFzvrtKhN/ubRKvL9wQouPzG92ZCok19KZYDJcEiVCRgNmHUI61jAyodMAb0R68o0LwxRxraB3P3k8MVs0UgYwN6C9rnUr6qvodtPhEcQHGy4xF3MnyT+xUp14XeWG83MkxeX0xDoSDbCTrnFfHACcD9SA== 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=MRrVHGDRLughqVlx4AwYJKYY2Ke4n4qbuAKF0PLzItY=; b=QpmZbdsA4vGKz1/JoWoRTtW5SfOydIINF36ltMy59F8rdHCQT9txqsJnmYe3CIDZMkYlA26mrqXuOIXPrsI1/RpFz+Njfwg0JuwtSRkQ8EFTOMiVGgt1aybERyVH179rSaK1vEPYH4YY2g0nzVs0gvKxaEBvTMvuI3pOV7uue+TNylFoWZcQybRrHoSZHSd/Vr8vzdhKLYU/ap2a4dOhJAywzEt41UsJLgYGyaMwUueA0ErnYMCFzIACX29oPXDwxSh4yPHFAIgcPigJIgfwy3Nf06qzYfxBL71mC5/nC8hg5ricyILfa6TIAOwjqtExg4dlLOuz2yS2i1KobyT2kQ== 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=MRrVHGDRLughqVlx4AwYJKYY2Ke4n4qbuAKF0PLzItY=; b=Rk8L9i/PawLwM0ab1iB2T6sIlRyIttdH6locWvTOaCvGD3mOPPKCstWlHEhHA7SmpoWfaLj7xAnBSv2s3KyDnZXaIBUN3gO1/Q6I6p64kGzR7dJkB6D9Z/VDzBsQOhtDbbMha9NIP1u+6XxaFTjtMwv6fXE8j1GNWgdXg8CVPZs= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by IA0PR10MB6819.namprd10.prod.outlook.com (2603:10b6:208:438::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.23; Mon, 18 Nov 2024 16:18:27 +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.8158.021; Mon, 18 Nov 2024 16:18:27 +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 6.1.y v2 4/4] mm: resolve faulty mmap_region() error path behaviour Date: Mon, 18 Nov 2024 16:17:28 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO6P123CA0034.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:2fe::16) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|IA0PR10MB6819:EE_ X-MS-Office365-Filtering-Correlation-Id: 5b3d2cfd-8951-4881-3972-08dd07eca07a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|1800799024|366016; X-Microsoft-Antispam-Message-Info: vV2subp5ZSTXCGM0ZknUUWeI7WfJT1L1hC+JgPOIAoe1oEIShHB2YVYVnkfcOYj8f0d2VE9bKiiv556qwuDO7m5Rttorrh2eeMni27ipbx0MaxtcyGTG6Yr4IoPfKELXUYHDDUfrqHyHMy6JvPT/cVnnSNeQb6cUctehWMnrl3foqXRsa8InrxaYWv8u6AcgpN++bAMR4BpaGPX/dCJDDe3de7lAwSyf3tB690ogW/huhB900BnQgORy/sdHPeOkwv3hWllYE9juT/fGGrQwJGBqHm03cx4Ac+Oa/zB59LKGZTlvBymGnZa8GZSr0K8z+GU/e3uveJsnJ7/m7Qtc3Q8sY5uS2uvS9srPr0FDoriorINQYLnEypKEZd0V0MRNsO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7416014)(376014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: mQs9o+66NfqJNu9Yb7PJWJQFaXE62ns30pd3KonjEqZezIp8M1t0ryO60PNkraX7ZUS93yDXhdYbaERnJXVjIZguQha0ogoLjcjgKkZCaNRse0VGDn3GGL776/mIQwp8kRWPKPOs0KvSezCCq/FCpTWs55KOoxy1gPMQP9nveH+9qeihSad7Ctko/NnRb2RbwBKga2aNHWesB+SaDkZInPI9gbbt7G0vp3a+xJ+p2FxmPhyodgYoBHfSun96kOLugiHkTe6xacmlj3+RtHdSJyu5jJvyZvkn8llR98+dO0C2ZErZ/D1X+S+ePrsuIkG0ZdcuXisZzxjjkoy+aoVHpx8g96lxVaK0hXA7E/8yMIbeCTncmlWf61ZwTJu3l+OitPRnsvJFburb/wofj3AlJXl+XyfFRayCZJrZ2olbAm3WzSGARbKNcZX4dZsm2wnR+/ip+l7jEyy7udsA0/RrRMLB6hm4wl1jQj95NlOQnTL4Caudu4JnZ1cPIpNwGtH1l6KHA+TL26BVyhrZU3IyV/NKOCSe6u9BksC3TEW//2mAyDyFlPklXq2zmnKMHUdN2xdtY0sA0qxgwlIy+9vBNsU82zG+Zyxq9DgnrYDN8BlaPV9vsOxO0gn38AGrqANpVYnKx05MVbL05EZTQcbs0qT/+cu3eplBwBfxTvcLiuX63Gun+h0pwIFWJ54okb/h7eRWaQz0wNZelzkzE6sdBpar/AE84nICRDEW6eve54/38q873gmKHWe/mNIUbiiaAVtUwZTroBqDCiHbjDm5z4Zp3YLO8vvkRvvLFz08PfsFodTNwTDyiKr3CKZz9q18R08gypo7TzYl+Stk9YHzjAjgOtNoCHuDYU5CT03v8ESqmLHwUwDBK707UGUaI4FJUMRmNJYVCo0lV5x9wOD+awqpOmXmzz6bVnoZXjxcsO8y3whRKDB8f5cNY9/S9lY+wjpwtJuMVRGzO/uP1OCq1VKSj5WRKBvPlRybCZ/M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GXgx4KazXkWBodRRIoQ4Zs5bsdKTsWkHlYi2N0cMVsDm66BSNHUn+YAwptFwWlKDaixilh3CQzjglgZVwmcX3vgK47kAfgd9ZsaswxDn+d0uabH/+MJkF713HErbObgywhh7lt5z6dGl2/iqqSH3FF4SUBMizILNGaea8M6vVYq3/IBqmkLaXcJEJ9MrpsqCKvrCSQwcsxChC/ohIOATzfj0j30OewGYUYsD9JPxPlb6mOtjX4VixVnk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5b3d2cfd-8951-4881-3972-08dd07eca07a X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Nov 2024 16:18:24.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: WSRO9MZcbgSJBm/udHr7q46plGDBbNRdH6g7/V5Xh7AcG0cI/B/Hc/g5mheEVGPxMezRvSJRkvg0ZP7ySac92Ac+oAOcJFd1CpLKv7Nz71c= X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR10MB6819 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-18_12,2024-11-18_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 suspectscore=0 adultscore=0 bulkscore=0 mlxscore=0 malwarescore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411180135 X-Proofpoint-ORIG-GUID: FBo21Tb_ueMxPh26xCT8TRDS_g92OSh0 X-Proofpoint-GUID: FBo21Tb_ueMxP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ity=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 | 104 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 47 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 322677f61d30..9a9933ede542 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2652,7 +2652,7 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, return do_mas_munmap(&mas, mm, start, len, uf, false); } -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) { @@ -2750,26 +2750,28 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_page_prot = vm_get_page_prot(vm_flags); vma->vm_pgoff = pgoff; - if (file) { - if (vm_flags & VM_SHARED) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + if (mas_preallocate(&mas, vma, GFP_KERNEL)) { + error = -ENOMEM; + goto free_vma; + } + if (file) { vma->vm_file = get_file(file); error = mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; + + /* Drivers cannot alter the address of the VMA. */ + WARN_ON_ONCE(addr != vma->vm_start); /* - * Expansion is handled above, merging is handled below. - * Drivers should not alter the address of the VMA. + * Drivers should not permit writability when previously it was + * disallowed. */ - if (WARN_ON((addr != vma->vm_start))) { - error = -EINVAL; - goto close_and_free_vma; - } + VM_WARN_ON_ONCE(vm_flags != vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + mas_reset(&mas); /* @@ -2792,7 +2794,8 @@ 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; + mas_destroy(&mas); + goto file_expanded; } } @@ -2800,31 +2803,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr, } else if (vm_flags & VM_SHARED) { error = shmem_zero_setup(vma); if (error) - goto free_vma; + goto free_iter_vma; } else { 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 if (vma->vm_file) - goto unmap_and_free_vma; - else - goto free_vma; - } - - if (mas_preallocate(&mas, vma, GFP_KERNEL)) { - error = -ENOMEM; - if (file) - goto close_and_free_vma; - else if (vma->vm_file) - goto unmap_and_free_vma; - else - goto free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif if (vma->vm_file) i_mmap_lock_write(vma->vm_file->f_mapping); @@ -2847,10 +2834,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, */ khugepaged_enter_vma(vma, vma->vm_flags); - /* 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; expanded: perf_event_mmap(vma); @@ -2879,28 +2863,54 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_set_page_prot(vma); - validate_mm(mm); 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, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end); - if (file && (vm_flags & VM_SHARED)) - mapping_unmap_writable(file->f_mapping); +free_iter_vma: + mas_destroy(&mas); free_vma: vm_area_free(vma); unacct_error: if (charged) vm_unacct_memory(charged); - validate_mm(mm); return error; } +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;