From nobody Thu Nov 21 21:37:59 2024 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 580B21BD508; Mon, 18 Nov 2024 16:18:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946739; cv=fail; b=gHJDcHOQXVAU9pz7z5X7Qumth1WyKLzeOEuTjWXLc8b/epovbqjz2SvWwyvGyq751IFapkQ4Z/WmYp0XVZDnl4o58UcbAsBOh9dzfFViXgupOkyGmZ7I+EdS/06CkpLuhVDLyuFJw6G3AVO/dmy+VV6z0dl3Zo9meS3M7WBszBU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946739; c=relaxed/simple; bh=rMgzYDuGzwEGx9C3I4Iw+T/QnZblXTcyHcZv6bj9U+w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=UlxLZWGXekDXpW+hoN4euVfEC75xPsXjTqjjqycWH7AP/DLC6DkaAor6ajboeRyIDJvqgYkJS6uVoquFN9ftAUJf7x2yDkSv/wouvF70vo5EjpLfg+8B2O+bpCGlAVrlMJTZ5qAmj5USc0DWl5i/Yye6jHhCjTI8uQeavzdlvck= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=C9Mw7j1y; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=e1FzDHBy; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="C9Mw7j1y"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="e1FzDHBy" 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: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO6P123CA0012.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:338::18) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: 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: =?us-ascii?Q?xLJ0lMkVIz1E+3mcPoFq2sgaVaU8NyJl/TvdHQ+tCIeqGCkcvnt87puxwfIM?= =?us-ascii?Q?Rr7ZUK+du8w2Am5QAWB6lUrVlfDW/d6tuk77Z6QuGJx/yFcohOIiFKBqLSYy?= =?us-ascii?Q?qVEGEOpHaeFivLWWAO08qcLUQJ0bUjHLHaBA0ad8cGfmRy11jswpVUQQ88Yc?= =?us-ascii?Q?tdIFcme3L5AT302Y1ry277tTR/65DPB5x7hRIGr7Fvmjfgiks3VzKBLMyxX2?= =?us-ascii?Q?dS8vmeW2Tb5k/vbnoTZBn0+N9nj91WJJQ9RsMVKZca3FnOmBEKULK7wR9+gv?= =?us-ascii?Q?IgguRnO8F6TCmRNa+qIC4vwowMnGy9bb6E7eiw/yh5aqjnNxs1YXSuzILwWG?= =?us-ascii?Q?NjoSG1E5+2utuOLx697H87icFlW9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BIsAiLvqQDGOThlD23tK+Te7F9gmn4rmbPWEvxemQVHOg0s5?= =?us-ascii?Q?XIMLlMVc4uZkAp4aQvq2qBbb?= 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: =?us-ascii?Q?qwdCXglZeY0MW7NO+1Nd2YxlrlLRWK2EpSl6/HRFIQBNwwm5nNxoXY1uDI+I?= =?us-ascii?Q?eb5OUQKpujsL9DPw7aBlfL5am9Uf0nDOuZvWFiDHtFAvMd9lC+CEGdoIHsJm?= =?us-ascii?Q?qlh2Yk87tySjFbubR9bXNfefeHR0mwpRaEYSOQUVOjxiLLWfSqgIdTKAfiQp?= =?us-ascii?Q?rICVqJBcCd/Q8BgBgdhZmkvjEIJAsD+krd2BFdB+sTIf9p76Ba72ylJxFn2K?= =?us-ascii?Q?SLH0W7JUxVvxceqfR52EnhzOSPhrEoxUG24ZoWBqbtXdm7LO6DRRK1Bhqt8v?= =?us-ascii?Q?FwdhqrDqTbTsVL3ss/s8iNP1+bugFBHkg+pwqRjP+hWO28i3VD0oY/16psS/?= =?us-ascii?Q?eKf8AqXLWuaoaYVNCYF19O21s1AjFX1ldTH8OUxgrVAs0HxO4hkhSYrGJjdu?= =?us-ascii?Q?erLcWhO+NU5oyNJPsmHHTekOUP9F6Tl1qjdukR20byJQbL628SVIDBPKkpCi?= =?us-ascii?Q?+StUDr4LTcae7dzNBxl91XciT1k50z5InoLaT2z9pHNg1GBHlZEBxsN04R3t?= =?us-ascii?Q?hKHIo13wn8M/grhWVbzZx8M8zwiv5NzLDDDCE62F8A24b3LHBQKgV4CGNZm8?= =?us-ascii?Q?TfqKC/B7H88aVCO/z2g27K2UW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?Q?fLGo21ECNsC7bjT7tlSJCGhfBcy1aS3ht99lmRfU8adrq/oSI6+K0LuJotUE?= =?us-ascii?Q?ZWKiWNWTn4cJC8vDwVOUiEx4y9INu+cZfIQf/LgAm24U3s1AjgieySp5zcSK?= =?us-ascii?Q?9w=3D=3D?= 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 Content-Type: text/plain; charset="utf-8" [ 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.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") 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; =20 void page_writeback_init(void); =20 +/* + * This is a file-backed mapping, and is about to be memory mapped - invok= e 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 =3D (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, } =20 vma->vm_file =3D get_file(file); - error =3D call_mmap(file, vma); + error =3D mmap_file(file, vma); if (error) goto unmap_and_free_vma; =20 @@ -2775,7 +2775,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, mas_reset(&mas); =20 /* - * 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 !=3D 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 *v= ma) { int ret; =20 - ret =3D call_mmap(vma->vm_file, vma); + ret =3D mmap_file(vma->vm_file, vma); if (ret =3D=3D 0) { vma->vm_region->vm_top =3D 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 =3D call_mmap(vma->vm_file, vma); + ret =3D mmap_file(vma->vm_file, vma); if (ret =3D=3D 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 p= age *page2) return ret; } =20 +int mmap_file(struct file *file, struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops =3D {}; + int err =3D 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 =3D &dummy_vm_ops; + + return err; +} + #ifdef CONFIG_PRINTK /** * mem_dump_obj - Print available provenance information --=20 2.47.0 From nobody Thu Nov 21 21:37:59 2024 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23C501BD007; Mon, 18 Nov 2024 16:18:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946738; cv=fail; b=Gu5UA+Jy5aTQ732hAgXOckKyMi8w/JnnD718r/pVzlEpi7ZrZceku8lZc7KtQU4WclBjoqBcW4sixKkyCMPk3nWhJqnpjR/eKUWsaEv180RrZoCgmXWdaeYZA0JrUKR4pfmfl4l1ygU35Y5gF+NbI/AfubxEDYFjsghLdcSk1kg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946738; c=relaxed/simple; bh=sjI1WzT1XqGz+xz3HXhuHAdt/2TF7qsYE4GFZx6Qt38=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=BMePZwTt5V2qvD7e82Bo8J5RP6IpAxI0nf9n69fiZ5zWGFWFVCcapLXQ3fxLF5Grm7VzNJXoYh8CvscRR8rb3NS/QuhNj+nTwoVB1RT103IXwmtGInSaZ6uqo2WmTeByXmDe5P37pfT5NfIwddK3gTSUqck7DlsbHRRsiCy1SAg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=RxOKnzO4; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=o7NMkAh3; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="RxOKnzO4"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="o7NMkAh3" 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: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO0P265CA0011.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:355::19) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: 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: =?us-ascii?Q?7zWCD6+UaHc1dDXaCjxH/JDwvw1Xbrd/hVZVV96BmdyEQCIBLlGRhy6IiWpo?= =?us-ascii?Q?DsavrNXNMNGI14Y1W7w7A7gWLuROg1BoC+9jXgEh2p/gtYy0gg5AoUnbcsY7?= =?us-ascii?Q?1M8jKeO/MlLlZfc269deHgzOjw5WOl2QY9HehkUfh8YxQzBe9wVvgea26oki?= =?us-ascii?Q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nty1yOIvP?= =?us-ascii?Q?mWM7gBzssgHkHr8cJobOXlREgk5jMKZpdLheqQhDM74NLOMjjhfeMh8xXEKD?= =?us-ascii?Q?XbkHapxRYIVtNpoMCjwOBRbc1ET8E9s+ZqZMGhASAORVm35+kgqMmpmsfuSk?= =?us-ascii?Q?8vvFje19i6q+ysYpW0xuM/ibt2g1Q5JOPE/8PeMfui0aOvz/oGE/wBjk6KuO?= =?us-ascii?Q?nCnaLgMR8cq1jrQfrIAE+oCrY1/TIVdGOZWC7Mzalxwf/jk1ZkBnMQmWXP87?= =?us-ascii?Q?3N8cs4Kj8eKb0cF9th+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: =?us-ascii?Q?HPNvWGweDdconWe/cD2Am8iUoEb090Hm2VJh382ixZhqTQCe/q08Mwi/O5U8?= =?us-ascii?Q?ex/JSqsepG3T2z7lbI2/6xJz7SPn088Mi7VPdYzHv3GkfHYurakBQV3lR9Ss?= =?us-ascii?Q?UHClv/dpwZMeswacIwSAfPtwkvfUn7InvMfBM1/z1YWmNxExy3OjUiX318Wk?= =?us-ascii?Q?4dvrOpLFCiXEKUuAR5vEhXsHtYmgzKexRnjYiWzgMVbHHA6vc6KLEuY7cRwA?= =?us-ascii?Q?Np53Yb2CD4Q/Y9fuzlsuhdxgcSYatV9H0o8eiICy3zNHvMJa+pTenZP2YoLp?= =?us-ascii?Q?FuHSSOQLzzFhghtJtpOuQYD+VsysXXfqE9OJpP0sT+wVvJq+8+kvWROQmpqO?= =?us-ascii?Q?QviXh1gf3PviJhbbF2YL2PIwiyd/1eKEjIHb1Fv0/wFHDMsA02zkUOcyMCh1?=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6gzu6nUZ7dpNSDZ51vM+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 Content-Type: text/plain; charset="utf-8" [ 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.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") 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); =20 +/* + * If the VMA has a close hook then close it, and since closing it might l= eave + * it in an inconsistent state which makes the use of any hooks suspect, c= lear + * 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 =3D (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 =3D new->vm_end; new->vm_pgoff =3D 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; =20 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 =3D NULL; @@ -3376,8 +3373,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct= **vmap, return new_vma; =20 out_vma_link: - if (new_vma->vm_ops && new_vma->vm_ops->close) - new_vma->vm_ops->close(new_vma); + vma_close(new_vma); =20 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 *vm= a) */ 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_stru= ct *vma) return err; } =20 +void vma_close(struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops =3D {}; + + 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 =3D &dummy_vm_ops; + } +} + #ifdef CONFIG_PRINTK /** * mem_dump_obj - Print available provenance information --=20 2.47.0 From nobody Thu Nov 21 21:37:59 2024 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD47E1C07D2; Mon, 18 Nov 2024 16:19:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946742; cv=fail; b=kUpROp6h6kHGIubIMZAGAlYcOg9uxQ58xOgG5kYUt56J5RMfkNnTZXo6i76xFCk21PWOcnNL2x9r8Z66iCwDXBr+hC7bxho3GvJbJlOTMOKfy3J8MBRadJSn1Fb6142EwUzCAatC78E+ipp2hYQaGqfxen0cRV2n+aJLkqyLmWE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946742; c=relaxed/simple; bh=2lXFp7X7efz5VlJzZgYh66YC+AJnGOEBopeSl9HS9JI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=WCJ4lWR6cKP5oazn5DgatSzVtxidT4xPQ0gHge3N+/vQ2uAWaqHDiLcpJmD8OFjXUf5gnrgsZPY3GyQnGG/HGZBTrzzAuhkmP4nCfgj4vHu3Pf5Bk7c3EkaSMqIVH0Z5MKuzWU+upxDQ6vm8+jVhLivvl7hlMBAW3L4R++7doWs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=T9eoTatX; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=Mq2Ai5QQ; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="T9eoTatX"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="Mq2Ai5QQ" 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: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0044.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:152::13) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: 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: =?us-ascii?Q?a3RY11dY+5cEUnk1B2GHk1ZLueSJoq/xxZyAOgoEqYh+L/4msjI1Ew8JWu95?= =?us-ascii?Q?OhB53yUwGVErFx9vGz1rc/q9Kbhjc/0fUgKFckQz4ntBVu8rM9O6mwDlmWWJ?= =?us-ascii?Q?m2E3S6SDf1WMbpDMS4hdhTmaZnL1YVN7X5RV5G9gKUfMbTs/gvFt2sLiMuZT?= =?us-ascii?Q?KMOBQoep+2NmOic0qKrlsAETZRwQA12er/QkzNhXBMrD4aB5Be+CYMqwpq49?= =?us-ascii?Q?J4Xk77ihmK45upU8Ga6F7O1oGdGgjxD3WM0A4+wavba5S3i8RBkJ6LHad4mH?= =?us-ascii?Q?VP9ez7zYh18OkNkh6LEc2K1eY8SAB5hZB/YL1eW6bpIdH9hv+kkZOBRR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MjqgE5aZAMoKubgUVzqUdsAZ212qK2Sus8sUWK1BfCjcd3i?= =?us-ascii?Q?NlZQZLX5U209pyBRZJohH5LkQ67XV38H/WaxvSTTR1O1L+wriB0RsrT+IyJq?= =?us-ascii?Q?2HWLGgWye7KVZpBHkm0a06NR?= 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: =?us-ascii?Q?dH2z1p72GeV6DftdIJ9M/pPUkYlIzWwHAyD9Fet9RZaFzOOk7lHmc+KST6pu?= =?us-ascii?Q?a5qb977hi5OgtRUqmaZj4qYBVMjRJMkGnyI2hJYzf/8E4IK+K//c07jNiOvJ?= =?us-ascii?Q?MSJxMAZWw48DvWMknwXVJ4lh98x5xwWDTTFZldWVf4LkfIPm0bsL0TSyIw+R?= =?us-ascii?Q?HY7N3Spxyzf2Q9aum2rRxOSghj42bOfpnIYToWkO3gyV6ab8Bu+dBiZ7xT3d?= =?us-ascii?Q?UlptXRlbZMJ+CnrWXN1YL8/3VcAQfIf4DsZEYp9GzrTP0aIJrmcRzALO/D49?= =?us-ascii?Q?ClkUYeZWHRn28ai69EievT3GdWIF51TJlOonXG8yowgTOsEoOPXDBPOHSJmX?= =?us-ascii?Q?My8NBTMpJHUlZ1q7XpXlCKHfQWT8SB6D9sT6mvcAYavQgTQvnceY0pB4GXWI?= =?us-ascii?Q?mq/J9/MHEFKPcIG5nApoGpc5YiuaEuUtyEVX9qsRn6PeBMg8KTSsZtYMhAoQ?= =?us-ascii?Q?kKq8lAAFv36tmBh/jKG/v2iJaHJtm8NEXrfz1Y1VxLt8chwHBm8RTzdxMdcg?= =?us-ascii?Q?QSp3Arrytdyg8od6soVo/XbY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0YGnJr6TYoTZQfK+GDrJU2Nm1SkuxX8y1uw/C11pc+Ffl35Ez4xe?= =?us-ascii?Q?Tik77E6bSHIJbBS1q7Ux75Sz3VMBQZvI5kTvikIzf9KYa650xOVkjiuveoTD?= =?us-ascii?Q?Da7pJw7X6xGOTHvcbs4WT8ijcUufXEI0exkEkZbPdnkN00+GMz9YhtgYWk16?= =?us-ascii?Q?Kw=3D=3D?= 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 Content-Type: text/plain; charset="utf-8" [ 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.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") 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__ =20 #include +#include +#include #include #include =20 @@ -21,19 +23,21 @@ static inline unsigned long arch_calc_vm_prot_bits(unsi= gned long prot, } #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pk= ey) =20 -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; =20 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, f= lags) =20 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 =20 +#include #include #include =20 @@ -90,7 +91,7 @@ static inline void vm_unacct_memory(long pages) #endif =20 #ifndef arch_calc_vm_flag_bits -#define arch_calc_vm_flag_bits(flags) 0 +#define arch_calc_vm_flag_bits(file, flags) 0 #endif =20 #ifndef arch_validate_prot @@ -147,12 +148,12 @@ calc_vm_prot_bits(unsigned long prot, unsigned long p= key) * 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); } =20 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 lon= g 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 =3D calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | + vm_flags =3D calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(file, flag= s) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; =20 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 *fi= le, { unsigned long vm_flags; =20 - vm_flags =3D calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); + vm_flags =3D calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(file, flags); /* vm_flags |=3D mm->def_flags; */ =20 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_ar= ea_struct *vma) if (ret) return ret; =20 - /* arm64 - allow memory tagging on RAM-based files */ - vma->vm_flags |=3D VM_MTE_ALLOWED; - file_accessed(file); vma->vm_ops =3D &shmem_vm_ops; return 0; --=20 2.47.0 From nobody Thu Nov 21 21:37:59 2024 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD42D1C07CD; Mon, 18 Nov 2024 16:18:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946744; cv=fail; b=QSrX/mzCwgoqpA7wsQkcxouBMZn5N9o8DsYdWx/G1ZdzxTHRdt8m/fxUvbc5NNSXeO1IYY0fjguBuZXj2uDTFi/d6yBVu1V9AUEyUXzNuhCf3gGBmkuV1CazNXyuHXrBs8kPLFZIxG+Q4QUoVzgL/Z2U5KKlocTGBigyUMt4eZM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731946744; c=relaxed/simple; bh=SMdCHpADAZCDZ19IuwSDkdB0196oJjOK3367HSuHZsQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=gFK/R9RE5BgsTbfPQTKXN/feiRy1iTrv8PQ9j2msygAedSNlj3elA/+5zy9rv8mVzPM5QX12IGmJ7m03VcVwSf94oW6XEgmKm7riB2w7MYRtje4AhwQe8FE+BuAffNQASOvbhOw2hhCvI/fx4/L1zs6UYxVF/bPTULflUvmotBw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=NB4v6gl/; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=Rk8L9i/P; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="NB4v6gl/"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="Rk8L9i/P" 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: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO6P123CA0034.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:2fe::16) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: 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: =?us-ascii?Q?vV2subp5ZSTXCGM0ZknUUWeI7WfJT1L1hC+JgPOIAoe1oEIShHB2YVYVnkfc?= =?us-ascii?Q?OYj8f0d2VE9bKiiv556qwuDO7m5Rttorrh2eeMni27ipbx0MaxtcyGTG6Yr4?= =?us-ascii?Q?IoPfKELXUYHDDUfrqHyHMy6JvPT/cVnnSNeQb6cUctehWMnrl3foqXRsa8In?= =?us-ascii?Q?rxaYWv8u6AcgpN++bAMR4BpaGPX/dCJDDe3de7lAwSyf3tB690ogW/huhB90?= =?us-ascii?Q?0BnQgORy/sdHPeOkwv3hWllYE9juT/fGGrQwJGBqHm03cx4Ac+Oa/zB59LKG?= =?us-ascii?Q?ZTlvBymGnZa8GZSr0K8z+GU/e3uveJsnJ7/m7Qtc3Q8sY5uS2uvS9srPr0FD?= =?us-ascii?Q?oriorINQYLnEypKEZd0V0MRNsOHPdrdqlOmF/GWKpeX8WdISYpAuugl5oxQ5?= =?us-ascii?Q?iDlZ6nzD4x+UBq4EhpviCvtXjdGyLcf4Q4XUSGwa9I0RAZPSJEVc3TdAg7Qr?= =?us-ascii?Q?LFEMi246JuIIDsc2VEQwsUYmjpa6LqK1FuwXkORtnEC5hpAmQurM14CYWUh/?= =?us-ascii?Q?jdcO+FnEIy1t0JSOCdlH15WUY0embR81U66SSE37nUu/7eIJhatdVnE9CKxr?= =?us-ascii?Q?KDAJdB9HOq02aybeFbK6dXYEctMt2KXAw94VHps3epjpW6aeXnca69AcRXsf?= =?us-ascii?Q?Mgq5AueojB8BkCnOB1HvikOxKuK6vZPvhybj1w6/BAIiw7B1hd0Ldf+Ndttx?= =?us-ascii?Q?/6GeTYsmX8adUdhfCs5zluSTK0DvCWZrVZG6QxgFZ34tc20lPAA5OAgecx5b?= =?us-ascii?Q?qtrBmav8V3HqJ8SaDLRwxl3XB7chN8gn2V/zoCxMi+zHLQuTILlABcTbnUFm?= =?us-ascii?Q?Qh9x5Nx0qq8QQVkwCxkpJ2T6P5FJueNhCckydyhSRf4qFrih+r4Ox0jh8CXV?= =?us-ascii?Q?s56GB5yGNxiJWAAkIMMbVP0S/pqYRuaOn9Nz1oKub6AYOxgu1TevNloT30nn?= =?us-ascii?Q?ZUXWG7v7fQH8Hytu3YJQ3vj/GnCO1mUSEf3EGHxHUSU/ehz2HzW5AzDNzfe3?= =?us-ascii?Q?fCviKx8/qyI9seCNulXX5O15bs9qrI4e+X+7ZOxPJBs566Y6glMqlSJorVRe?= =?us-ascii?Q?hYvKZ4b/+dwDEk+gCCbGRxwhR19HBPtE3hAtOUxEpBLqk0sgD4P40dvYyRaL?= =?us-ascii?Q?+SomegsU0Iqi+Kjv14RxExV0qx/ZXLmI9F53+62Ix1yqgEA+vIcgQQvxiYX3?= =?us-ascii?Q?KZsvrWZIN4A1KVdI+ORkFbojqqYEKo0yK0+YWI7i6X/x4Hw7O9X1RynZqmoU?= =?us-ascii?Q?qt4IRNoM8qQHhVAUXOUSME1lIhFAM/ESOV/FJ2BSBReNS0laBo4ggQb7sBpl?= =?us-ascii?Q?E1QZYHz7L9RNkGM6utnk83se?= 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: =?us-ascii?Q?mQs9o+66NfqJNu9Yb7PJWJQFaXE62ns30pd3KonjEqZezIp8M1t0ryO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kvP0JcIZ+D4DfLg25DKj69HWHifgPgqgKoYuyha5B1zvqSvlsDhrRbqLYlufIMhhjdN7Ut0tcRGK9MvWUcUwvpWO3ePTgzx8tTIkj8aVFOZhDUDh0zb06s9f8+llU0ufeD1cUPhZ+pMWrnelAcY25/feFBHvZUDv1amVVlKNcS7MRjHurGuGqMX/wrQF7PXSKocO1snPRmp9zzFf+tVfL8YBrMtldjfvKzoLeIDELk52q+MoGXgx4KazXkWBodRRIoQ4Zs5bsdKTsWkHlYi2N0cMVsDm66BSNHUn+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_ueMxPh26xCT8TRDS_g92OSh0 Content-Type: text/plain; charset="utf-8" [ 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.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") 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 sta= rt, size_t len, return do_mas_munmap(&mas, mm, start, len, uf, false); } =20 -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, unsign= ed long addr, vma->vm_page_prot =3D vm_get_page_prot(vm_flags); vma->vm_pgoff =3D pgoff; =20 - if (file) { - if (vm_flags & VM_SHARED) { - error =3D mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + if (mas_preallocate(&mas, vma, GFP_KERNEL)) { + error =3D -ENOMEM; + goto free_vma; + } =20 + if (file) { vma->vm_file =3D get_file(file); error =3D 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 !=3D vma->vm_start); =20 /* - * 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 !=3D vma->vm_start))) { - error =3D -EINVAL; - goto close_and_free_vma; - } + VM_WARN_ON_ONCE(vm_flags !=3D vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + mas_reset(&mas); =20 /* @@ -2792,7 +2794,8 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma =3D merge; /* Update vm_flags to pick up the change. */ vm_flags =3D vma->vm_flags; - goto unmap_writable; + mas_destroy(&mas); + goto file_expanded; } } =20 @@ -2800,31 +2803,15 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, } else if (vm_flags & VM_SHARED) { error =3D shmem_zero_setup(vma); if (error) - goto free_vma; + goto free_iter_vma; } else { vma_set_anonymous(vma); } =20 - /* Allow architectures to sanity-check the vm_flags */ - if (!arch_validate_flags(vma->vm_flags)) { - error =3D -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 =3D -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 =20 if (vma->vm_file) i_mmap_lock_write(vma->vm_file->f_mapping); @@ -2847,10 +2834,7 @@ unsigned long mmap_region(struct file *file, unsigne= d long addr, */ khugepaged_enter_vma(vma, vma->vm_flags); =20 - /* 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 =3D vma->vm_file; expanded: perf_event_mmap(vma); @@ -2879,28 +2863,54 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, =20 vma_set_page_prot(vma); =20 - validate_mm(mm); return addr; =20 -close_and_free_vma: - vma_close(vma); -unmap_and_free_vma: +unmap_and_free_file_vma: fput(vma->vm_file); vma->vm_file =3D NULL; =20 /* 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; } =20 +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 =3D 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 =3D mapping_map_writable(file->f_mapping); + + if (error) + return error; + writable_file_mapping =3D true; + } + + ret =3D __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; --=20 2.47.0