From nobody Fri Nov 22 22:55:47 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E88B1BC9F4; Fri, 15 Nov 2024 12:40:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674445; cv=fail; b=KBX9rMr1xzapm1yZvP8Z9XlBftaNxBIngP1lzhEDcWkrOR8d/sqa0mq1WYtsfk4cpgqGqD7XMLEEaqjrDjhT+Eij625Wc1kwMYPWVWFHbn3ofpzgdxoqn0QbkKVwCLW+jsq11H9imz3Q+0l3IRWFe/eC30AOJ8PH0sMgFxdkDlI= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674445; 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=DeXqmsVzsiTQYtToWE0cY40Epojjlhj9HYKY/3E6FSVcb2U0mKfHDWfBT3h28gPX+bLMKDgMbXWCfY5iWnhVqFCiQNsitpAeWL/FYKHzfXYxjAPP0khMt+AHAwFN8V4p3WKRo2eUlCaC9y/d++pTYxmKh/SEAoEVXyMJxQ/uXnk= 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=JXXWT8eu; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=x7DvoeeV; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="JXXWT8eu"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="x7DvoeeV" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHLAN011157; Fri, 15 Nov 2024 12:40:22 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= JXXWT8euhxvgEBmCmW0g6JJ6mj6CRnmpy16oBqwWBnV4qRGwxanF+q2hx6Dyj6m2 htfOwvLGAQghZzEgqc7oIi/bmwwmfMQtTb2BLF0xxolm1BK5WZ7L/mnDT5Dx93J5 5bPWjPweNC7j/BkFwNikIsjwCey2sRFWVu8tSZ8eloT5VhWf9ZlaRQ1gs9Qwr8xw 7t16tzeyhkN+ca8JQBq6PV1X4tf0xGS6lOJZ+oTV8d1TFolevLuSbVbQEZC/Yb3I l2yiABL/zV4fb3favHDGc08OamX5RFnGjpqhjwpPvUrCHVuiUxtVT+MaoVWTMXHH KbMoh5953gf/Q/aFQO6Emw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0kc3dda-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:21 +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 4AFCdZAI022819; Fri, 15 Nov 2024 12:40:21 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2044.outbound.protection.outlook.com [104.47.55.44]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42vuw2mrm5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:20 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=UIyQig9iUHcjRaorryJbTcCWY9+F3vmQQzbH6iimWbZ/8Vxbc2aWqyWjghFDPFjZpmuy2cUNezXrKB7ytQdVVJtqkWHxhdkfrrW2myMmAlDluNXMu18FLAO0OViWs1bK9MNij3e0FI4jOf5RjquZ2OJfLovE54k+mqCi3GurtNXNIDXo8cLfsVDafMEFINgJMhDbUkMp5XVfjvWp5J4m33oYR4C/GkRmybdNkIpjLzWFgLJ7LzMWDwycWuc9XzlxkTElijrgOGNN8UlXWS3Pzq52k/QSX9149lajJG0NNT+kOcVBVfwYFcB5OTPQXNlRM4OyJm7HBERFu3qJK04bRQ== 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=c2bQ9PVYtqffzQawDBg7mHSkYa4jdT+s0vZ9gmUcJFlPIRI9Zr54kyTXSxcATkJBP1FmVKw49MMBVOmuYORdRLp9nRGRc/zNQR6QKya71CpF3MyEZ+4bX/9xUK6b1cD6waMmAZQ2uRv4AEOfTpIvLVrN8K3nEyVs9iHlS6jnHh1cb4PCMZvQpVuV9BmTJN6aGFmUg+rOp18WsnG0kvaTYBU5aH0V1/WeXfvsiREDGh87yezSs3VNTfpUqmcLycnw/z/w/esinwewKpDJPHHmhIR0aGjVtoNFqeWHyu+zKZEblFgoAbdldbi1HHG2Xt6Q4i9jU4T1GdOsgUJOsznIIQ== 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=x7DvoeeVCIwFO/tUej76OXgxl/ACnGelXRYwOKTpsbgbb5qu7zR9Bsl/dD+Yr+LnofhSYHKcp17hAPucQiV5ssIr8mnaePyFhOUTrMmlOLTyRhB1Mh7R6WbzSqtc6udrFz1OaFEtqWSqEord7ybFkRidmSUw1T4QBdeAWDNL6E0= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:40: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.8137.027; Fri, 15 Nov 2024 12:40: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 1/4] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Fri, 15 Nov 2024 12:40:07 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0389.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18f::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_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: 24c9dc38-2c61-41a2-05a8-08dd0572a925 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?2i5T+jhegnfc9Og6bvbdciDCTF16DEKb/d70nyDuGD3SnzpjIrwpRbw+inIf?= =?us-ascii?Q?c9rNBrm5xLyGSj6VgJHRWJ03w0tP8zjecQ6FVHAu9P+Z5QJIWpodJ6gDm84Z?= =?us-ascii?Q?kPA285wkDrXJXMHeb3bGh6SyTAGgCIR5ymufFEvsRrkFEP39XgE7l+NmR4Jy?= =?us-ascii?Q?wbYEloHq4izvS3GTZaDnrlHf8AXF7QaDOjIcFRkzIoMkKPKUEDsgMd7ENOlP?= =?us-ascii?Q?iXW8Nbcu8jB3H8G4EyUgyJ8kwm+WoHL6a33kgCK5rtdPtAKhBzkL52Xjt7Ff?= =?us-ascii?Q?bvxePUuozWMkhjs6K+eLBUNKqZAZvIgbd+lWKNJjQQ0lDCYP0n5nlXc4uHjQ?= =?us-ascii?Q?7+aNeELE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PJy+B/7pZ3xSo?= =?us-ascii?Q?GvfZpuJct8dz8m9ibWxl2mA9fGryz4Aqu9ZiJekcbqoRxZsnkpf0OLMx4Nkn?= =?us-ascii?Q?ZapG7DHxH4m9XAt7FGlvr6EO?= 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?nGSF2azYbFTLcjpOhafXSpQfrkzcAx0lLf37yAJbmUHfvznJXgcv+lHDETj8?= =?us-ascii?Q?knkZwX1lY0r8ddL3AMsGvxrzYpJN7a1OMvKd6BcF0kltGY9DS55+oyzPLfzX?= =?us-ascii?Q?uc3AAVyPn/A/jUFO3cxbqI+G2dpfh20UIxrB/2+GyivxiQ2mxE4BGAmuyvMm?= =?us-ascii?Q?0Gg9g0+Qfau0UTJFoBfb3Qh6Bdx0dJPii7TaLzjRK2DxG0kmLR2mI++bbkku?= =?us-ascii?Q?DYbzgJXNzG97OTT8iYXSd2kj9o8No6IKP3bOd5gq0ZnnrP4gQaSx3BEwEx/Y?= =?us-ascii?Q?MZj9KZeiyRXtzu6CnhpjOqrdruy02DYqyjmIQPVRNGeoDRjArF61zBrqtUsZ?= =?us-ascii?Q?8KcFb1GbFCSrhSsbT1ZI/6nkiZO7uqQJLwkt9Z+NDMkmpd+MK4bkMkA9+RXO?= =?us-ascii?Q?qJAL98nz5/pOnCT5FvgQ38gKtvpviIW4B1pKrLVGG5GPglSnkNwKXHZnI687?= =?us-ascii?Q?qyDsICkVvqdGSJGaXOBCKTOsQ/WERspMOXsbiIHnL117trvlo2U8m7IkZXbX?= =?us-ascii?Q?+SAvCxTXELnZ/tOjXbBoQUI6pxN9OiFteNfbfSqZHF6bd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RhC1aVj6GpGkZLbpABxy4CE+Uhyj9vmfQIZZbbn8?= =?us-ascii?Q?ztVN4NQE5ualqa3Mxh9CZWaqIwfqlAKCN3z+JjZZYn5n+WyzNwB94Gbtewc3?= =?us-ascii?Q?HK8jAU0fYq7aPGOdv4KY+WxjmdeNuUwnwjWOCjXDnQ+ak7NWDQAwD8BSHSVD?= =?us-ascii?Q?ng=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: w0uLqw8IJnUpg9jf49YCtd1fPc2nyP1LyZx7zW+lChJKt1lhN0iNY2MvSYF0wpvBcHgWFz83yJraZhiaJfm4UsZIsFRpiA4n26H8XcAB530uO6bHrv+c/nWYP2BV47KmUZEyKXy0WpFL7zfyQUHe5zbqZ30XeTW5D11DaIvlJRur+/FemYH3jztZkI/78KnGSpmrdcrAWDpL56JqLX3I776Fx4i5juVc83qhazwgIndNy0l1icW+X7TTxTb48BQ+lj0t76+o3uBJcXasJ+QPztCUr++HYCrsfAFnkr7/zDr4+ZHkHFFUsEMMhx3zWatG+bT7871I21PJr3Q2PoMxKhCsV1gIkRktYhz/MWqXlMYlNNUCcGVDCRHxBNSzNTNlOkIDtfgX6eH+FbhIXCMXHNQJtk31wkMK6CggYI3qWRBQs97XgaeXcVQu/FfVN8qNYmy55la/A2WqlyJAOYWYGDkn1BZWjG2wmGZ4CTRoSSvwk4YMYEkGOK+WduRrZtX+f1WhMBXAqG5tIkXwS1TE+jlP4g0VZR7gBm6E9BU2nWoNPFWwyU8s7WeXz6yiMzl84/nkMPuMUYwoz9Wf205zMxU5rQNgdzf0zRT3vjNkIzE= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 24c9dc38-2c61-41a2-05a8-08dd0572a925 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:40:18.1372 (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: wbFZWYuruv+f5sgYZxWztO5qH1n0oAejy4WAMKoFlbCsobGepJ2GVglKQ62eOTXWBY9taoMdOApSjm67ksr6lFKZH/lznc4zkWLXJG/vYUU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-GUID: x5k1YAtNbc21heM-ZQhUXpgV84gTzH1d X-Proofpoint-ORIG-GUID: x5k1YAtNbc21heM-ZQhUXpgV84gTzH1d 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 Fri Nov 22 22:55:47 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 2A87B1BC9E6; Fri, 15 Nov 2024 12:40:42 +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=1731674445; cv=fail; b=aN15AlFsEagEtDaPmqavj1XgapKqW/fUwbU0H1iZdzf/YmXRPogN/A1otszRO1CmBum5srFZgtP+QOTPVLitAsZ6TxeYR2+2D4+9M32+TGdNEeIXNJHuzgCNg+Vz1xdzQLoL9OYsBy+bECGovVMyY3UGC19DJqyFQ+UVTA4wzmc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674445; c=relaxed/simple; bh=sjI1WzT1XqGz+xz3HXhuHAdt/2TF7qsYE4GFZx6Qt38=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=MV6tVOSZ2Ho8ED9FwjmT09zQQCrpVeFQBD7hRxWZmUco8DJ/Hask9kpa+/DCGtkHipvKkrM3Hc1Cs6kZVrEh1D8ku6Ez4pHr+4sOrToFmSKLlRt8WxtZxoIa9XHXGTzmTzdrk1RKcai0yf/snUlE9ZkKP+rikSV53PWqR1MHOvs= 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=MmTBmgjM; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=NbhYj/7f; 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="MmTBmgjM"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="NbhYj/7f" Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHJ0M014501; Fri, 15 Nov 2024 12:40:25 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= MmTBmgjMus1Y1OHoAThRhY82a4gjQY/Vt9sU7e/6vMChl+Ep9Kv2JtQkwAhLbvRy MC3AoL7g0Aur42VakX4l9NWLeYE2FmEIhARTEOPkGJbaCv1ecjoHsZEpOv2/FRmF sxuI/IYqQPCwbiC/wejt8tpm1ZHnCkiIzjpFcMlxajIK30A2RSisbsNGEhnIYXMS Qc3GnAt0QaRj2AUSXr1M492kEAqnSHrig4ROY84RHjG10t8YimyIR19h08hQAxok 0D1bYbJqrsS+mmVMUqZVFUPnfOyEdTZG9uo7BST0T5+5/jHpffEoK4shsKrbLsFE ZitQy523nnoeip3kowCkAQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0k2b9j2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:24 +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 4AFBtqdf022740; Fri, 15 Nov 2024 12:40:23 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2048.outbound.protection.outlook.com [104.47.55.48]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42vuw2mrnj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:23 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=BAbeRHfCFCgfWA317yQlByuFeGllSYItxYNgWK4LtAqJFMevP0or43rGFuQs/mlGibIwvlSwnkfqmHdWU1NwgqSkKHDZ2YB45mUzc7zPCCJdoIIipVx2OOCdsATilH3M6SXvfVzM7KY3PdmOgCrMuMOGzXfjH7+tdTw8Uk/kqNIFIqjfv7GGLj84XrA7PWNBPSvszyMiUdtsR6ZKTUBf9eMJ5Z/JE1km7aNgLdPPUiWo37pOnfWu3MWzIS1PbQv6EbIaqk+BIVKrlOBwlgS4j+WSjij2lmV7n8loqwkACIadXvhDmkEFki5oJAbqKISfrQQeAHOmudA2njG5hVSPjw== 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=kPWr6E1P0WZkI35BzaR5dioXyk3vJYKALh16wz42bT7e4IVbaPzx+1XKHoW/msF3Zks0YQqtqqgfMxp2ZsuioKfK64mfRPGB2Zvy3ctB1lfgRw564Xz5twHNmvbNhBnqZLen6OFBPl9CdNWPciR3+R2513epPAUh4YIhz7lA58NNctzT8nM2S1lVVpEVUvxbBJeyw0cOkn1palOtw6FpgiAFVvpIpNEFgULrw/QtQqeGkmoBOx4Zbl/meHx4ENKRFoxv2ZgtnsgCILk5Au24ClQuEw3NRFpYrkyAoLb4K5jj11zn9ty7rfhvvrUGGJgds9utMd8nxBu4IGR7+Mz0wg== 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=NbhYj/7fmWqhhhmDNCMxLSV9d9U9sDwC441VI6Zv05Bb6SdSluP3bNGH/S3mD8HHAT1rkAL9zsYfffIiZZXieQOFJnZEGdYqlqqqkJkLqA6iSinJsPsgLhcY0RuGTHWWDDFZ6DcGfRuV9whum5fklHFU4awZogWArdGlKo6Z64I= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:40: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.8137.027; Fri, 15 Nov 2024 12:40: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 2/4] mm: unconditionally close VMAs on error Date: Fri, 15 Nov 2024 12:40:08 +0000 Message-ID: <58ab2716422d1ada28981b8e3b91e19ab1f5778b.1731671441.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P302CA0024.GBRP302.PROD.OUTLOOK.COM (2603:10a6:600:2c1::10) 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_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: ab7909ab-9380-449d-e51a-08dd0572aaef 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?6SN0Ini/hraWUDeQQad26sRSsRzUb32lzRlZ2v2k/rFaJDxTgdAJPwxavsGK?= =?us-ascii?Q?NDrwsExTx+5//ON50tqi7VCwAbYJTWrHMWBYu5npzfTbEV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fLtxxKlaX40DuA/aCSs5S1cWaDWDkLDnEDxIno0nY?= =?us-ascii?Q?CBHN545StIKEsRjwFNzN0LdOO0KrjFRMaG1RzS/cLLmSmErwDhyChd98qTVo?= =?us-ascii?Q?3TAwKV5R9SiMXhzLlzqHHchRrIXfFjxiXquDQjFNxAAIhT1wCy79zG6aCG0l?= =?us-ascii?Q?kLdqHqH56E4KSs/oMXDaElSGABoaeiZikf1kZEqNYKdltfw/elk9K67sVwWr?= =?us-ascii?Q?nu5mYgrCZsW5v8S/oYdjeb+4PHDYsVhzkqQyEk+tUbn2IkuoImEojLE0qgm9?= =?us-ascii?Q?zw7l4CkwykgAEB/feyOg0ju5XfDaEzBJowS+jI3AswGlqajEtA+luv5Bphay?= =?us-ascii?Q?Ys+3BsDNOgwDA5rOHHtNzjUr?= 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?H6TisW6MSazPmphUyjUJshC5R3m3atjflLCi6CYRNjGhRPWFl+0pEBSE/c7n?= =?us-ascii?Q?CoRGF8OWyiYf8dC8oukxWqQWAKoCJGuLQ+0s0+8QgJUoSY9jPZ6WiaUZqoTF?= =?us-ascii?Q?k0nl/ouxpOl/p9VTq7UkG3nx2cpTvOiyvHzybxSlCCajRlGRK0oV3fCQoR99?= =?us-ascii?Q?mXthqZkz3TnGYmWCtbtdcvnQ01KnWZouM2eLKzM9xsWpRi/EI5a0bxUtXwaW?= =?us-ascii?Q?8cljhnZRvhJtjopVdxZO3VPTli4bR6OyQ/0A1TSh+6IWlWTVtxajyDly6ghK?= =?us-ascii?Q?Q2kHNoIh01SD1BfBcaoyUbe8Z9wFLrEle7LMc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lwhgtoSVjPerEzwOtWGna2vWgnzuk02UWow3rpli+iNGONlbFONqcLk0tjpe6Z+sgQT7S1V5mIxbf7pRco2wsA17XRLEb6z3kPCXi9Zp00XKDKyarjz6HqEYYBMQn3wwXd2/s= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: ab7909ab-9380-449d-e51a-08dd0572aaef X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:40:20.9932 (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: 9kxcup4xNp8HprQBVUwJSmOYld9fmG+CMhbXfAa79IRkxgxMKZBZs8yWUOmBfGLRD8r9JqNhA9LDfEttIFn0pMNO0eySlAxHbi1aUm/AOlw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: H51HuOrTnqGSwv3BqamfB5cbYeVOQ3Nb X-Proofpoint-GUID: H51HuOrTnqGSwv3BqamfB5cbYeVOQ3Nb 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 Fri Nov 22 22:55:47 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BFCA1BDAAA; Fri, 15 Nov 2024 12:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674451; cv=fail; b=H5wwrlWWAUy2xg/3DoM0ftJnspy1TaU9Yy8Y32eDjqgBgOTFPSmLtwJTsuRKm3nWaVGtxkxaRxlBeiESY136DotTBXPpZNIdP3nODjtra4R4pjjIIaUm/XJsXPbQZl9OUGAn4jWNawyU0pCz8cbANRDNfgfjviRIyZxbBp/6Rks= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674451; c=relaxed/simple; bh=2lXFp7X7efz5VlJzZgYh66YC+AJnGOEBopeSl9HS9JI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=NSldn08Eh0PGeFTnc0mgi8tczwIdn0i10xIg/iMrRkahJbOCh749PahDCBqkRvSuJHv51Fa/J+Dx5rKdlKrKPuoajhe6rD+dlI7Q7Q+2g/v6mXr3D/Ie+CeKFfaJyCTPsb79HfAd+pTBWg0CcQZmzn4L1JM9H6Xux+9El7C/qeI= 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=kv0GjneI; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=sl8v01gy; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="kv0GjneI"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="sl8v01gy" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHPHt000682; Fri, 15 Nov 2024 12:40:29 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= kv0GjneIVniGUQxoDC6ZxAVbg9r5Q2LkjmV3bbQ875L2bIu1W7gm8524LKSrsaOk SIILfdjKHR6Nwi4A17lNUvcXOhNuLwj58lzwb8B2eRIK4UOC63l0gVk5id0v+Cu3 mZ9S0n7xqmRys6X2wMiyY5JBYK4MTENe1MjPV/5lwCpecsN8m/iV3pJKNEUXFdkx vG1b9QY3zhKeoeC9qOjGshbLJE52pW9Bv9pUZ6XUtqxR/jW497g5MCvdKQvPDDFh HwrKaMZJwU8yQ6MPHFkuCmzEqteGVo0E1JkH4bjrJsqeUwitl5M7ieuVKeaFkLUB ciknRq/gKZPddIU1tdUisQ== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0mbkdav-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:28 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFCSSLu000441; Fri, 15 Nov 2024 12:40:27 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2047.outbound.protection.outlook.com [104.47.55.47]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbjfu6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=X6ZimODuSfEe800K/OYBkUsQPylo7EwIGnT5MF6bNfUyzDT676f2MECVFmJoLOslk91TxwPMOtO/Qu6xKzeWZ6QKD04s7HU+9/QQdo/iqMck+gq0AZZY2t6MMfMenHYPUe3a0w7m7a5Jx1yXm1K8oCdjrSPk+VNvQvfB1H3OBumBQmUGBG0w2egiYIKD6sVhoTkTeYYm0Ewwr6U5qnHT9WzaL43YT/umD2u+jHuyMpx631Zgav7o41y5kQnMiQKtHQbAkqAAGpjj2k4Kloom7BO5drF4gxkJ42XsIfWYfcLvqt/2ccX8K/E4efA7apZcSDXP//zasNP9vSc8bPmzYw== 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=Bncjhu2ni+qrqE295agC5l03uepeapfcv1HbzeSJsIB9ljg6lYeL74sxb2b57FHIBD6xFCaNLSgfMJY1/SAGafqXk4BRWF342dmMBKUAkN+yPD0qpUz0b8RJhhQKhuJQAvICmu3I/REdB6WlyB/tMEeoiB3RpwHTL+CR+dW8r0e0txX1yiAdnbPUclTiBHsXaUptsqLkQ1CIR1II+758uNnOeHJV/PN2GwUoBo9nrn/0ozbKjcBFwbADtWmoj1xd85eLESWJFcjUUfZS9+Rj2Y6ftNvEje26OMnAlxHXqA6du+1eAV8ilsCGp46Q7GEPr83LH5Ko8HInELdQb8MqeA== 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=sl8v01gyXZWmbES55tHOE00Pm2QV5iQ4sOeSeoISltTlGbvFRir3+kGRTUvqS4zDpEaGJ6TfVkvzHgVidduzAs7Hz7ANuZWSXhs1v66clwFBN0glBaGAE3RbhNoo1+PjbqE9zCOqeOoT5aHZAGHkpNpli64YjsCQqk6zqWe2i7U= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:40:24 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:40:24 +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 3/4] mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling Date: Fri, 15 Nov 2024 12:40:09 +0000 Message-ID: <9d337250e80eda47ffc386482d4b5d9296a21507.1731671441.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0545.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:319::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_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: 874cb8f2-26e8-42a7-6db9-08dd0572acc2 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?1PcKDAO4NtPehkQmox6JUwypRsRSGHRHHYNC/3Ig9skKt6DdIl8gXf8c9W2l?= =?us-ascii?Q?QE31dZnC1vyvoa0+XF+EKM2Mw/ZQX5LQ0HPoz8k8dnmjXNa3c/SL35vs63+/?= =?us-ascii?Q?x9NOlXHB348IoODNAkedNFhXTEpR4fSjF421QACLNJydHnvZgxiI8ykRZ1cX?= =?us-ascii?Q?JUM0bdN1Or47NlZbIelqyDMu+yAHq7tieVIEphax3UBPm+QREQzKvZBZEi21?= =?us-ascii?Q?eBYknkTVOSzWb8p6vjiVd3o6W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Rda3yPDsCocncK06gjBLmsy6cyKt42/5DG3JkGbh0/sSA0TNLoJpu2Y2?= =?us-ascii?Q?2uXGZn5pAqX4QFddOUDin0KqlWjCT+qxAztdumEGr6OLi3+FZeDdmGEKGWQS?= =?us-ascii?Q?AoNfKt4rZBclhOiWhFXwqLLBpIcQyT3auQs2Ii0+K1CiEJe7MQ6RcNyTedTT?= =?us-ascii?Q?7evNVQAlnoi43irAkAacWd8+?= 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?n8zLWFr3fzw5AYsgc80AjoW4oaP6AVTzE40FtxRHzAnaVsjtskQw2/VXSw6c?= =?us-ascii?Q?506kXhylPHTkOEgU5iu39m0iVrrWVQ0E1QBFFGcP0L+0ovOFiSHyyjRGXmGI?= =?us-ascii?Q?7BNOtDMull+gLbz7/1Gma5JylV3py4kwX+LhixJLdgtWaIwqD68idKteZKLG?= =?us-ascii?Q?bADCUVlOJ6UbzPCdqEvZTg57/cffCOXB3XphQfi44bfRkEkm5RPhqZ6lLh3n?= =?us-ascii?Q?PyB68y4Payoi6IqaNX1u8hpGt4ITIlQTCeL8xOlE4jTOn1AB9iUIGyn86n2r?= =?us-ascii?Q?Pyv/+6ytTSsjGPubJhTnigAaW707mP52ZHrGsOqa/3nvGNbBaqAvAn5ze3Sa?= =?us-ascii?Q?kCx1RqQpqlr02FCrLk6kHr67sEslJ+Nx3LhbrKcHojIZgiEkEw6jaxvKWHfK?= =?us-ascii?Q?AcPWAesRQQ7FSVFwf3wh1EseQP2mV1YfDCBSxLFP+RleYdE0GNwpxjgLxZ0X?= =?us-ascii?Q?OZEWM40iaYKh61kfm1GYhi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= =?us-ascii?Q?+ij7tusge9rfZCIQctmXAblyBzRx/2FN9j4UWS7nULhyP/T+/JEWYfgu9M2W?= =?us-ascii?Q?yNAwsn/wcjY8572diwWbvr1goASIxRxETiHp0hg1sijT1rgCfP3RAJKcPxxR?= =?us-ascii?Q?gNen2uID/7Pm8if1vLXA3AtoqOBLsqHgwF3UcVSD3sBlvcyHMTg7VwH+RbSj?= =?us-ascii?Q?YLHmJobjl+ID+dvgGNm2s4uaobUN1CSP4McGAjC/THc3RiQyinmyelpdGOUf?= =?us-ascii?Q?8A=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: o5E0LURTrohqLPIOiX4I2xFZy65dDzAV8gHrs3F9TLDa/HzTV83DwlGG0mdKNRokuS/Hu5TX9GkQUpws2KHex6THBWqTo3ywv8BtxLe/ip0D4agw53MKxzQJoZv1d9teeuK3no+opgzbKjBqMS/d8Vf75rdeOAe+jZoepSxJ+5HojlXyX/ycQSAT8Y/fg0uAQfGJL8qTpmZAZcTzCLYnDlMSRf6FaJlgoHKBewSLrQTL/yhdLJxHdTekFYdJSdJSZmr17Fq5d6rNxGuIJUM4w4lgqN8/hMCsme43FI+E3qHHUscclctiT9oCVXlQmFCX8oij/kBHjfPJflk3cUsR0rJWAgiYssINqA1uNxlokWEDH1k5njxn+AmxUPVMRj7CHkQkGeyvP0HlqGyjiEE5PEt/hKomjqP7+6i8Vc/+YNteqwoFonh4fo8D4iOPGkgtvCTfXGRLi1rxfK6dRXZzNVPGbmPTaNP+hc+QxfnHXg1ahQUi5utAM2qMWcGxqWlGBuXmnmsFJZgQOVlnXtx/ar0aJSe1H159bG1HI/dndlkypXCPLD/Zu2rYxdr+pa6B33MYofA6mS6qLsmX0WUonlC3jSKCnkSHq8mHsGG8snQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 874cb8f2-26e8-42a7-6db9-08dd0572acc2 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:40:24.1467 (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: ieE/iCV3FW9t2//jOM0rLxnR+5JJO386IG7qBstXfSpPOJPd5/6s3fiIoJdBPH7TrP4mNMElNQEJPMwZoxKpMc55Spx/DMOZIk1wVr6e94Y= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-GUID: E5v2IPbVKiW5Rzrr45TcG1A-SeMkYXOl X-Proofpoint-ORIG-GUID: E5v2IPbVKiW5Rzrr45TcG1A-SeMkYXOl 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 Reviewed-by: Liam R. Howlett --- 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 Fri Nov 22 22:55:47 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A32F1BF80C; Fri, 15 Nov 2024 12:40:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674453; cv=fail; b=Dd6bHQAW41m0Nrp+4zHXoUW+Yl1M/uw9x1yr9Q2dlf7wUgyteut3kXx4bMEij9brVeNF2HsM0XwTxuBv1v13PIADHkGNykwzLUr9NebkIpic+LqLh7I7Z6/iDyf2mk/wi05chG2qQbuKH7TBpblI2EeO40nwkkN5DMxIpavDeCg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674453; c=relaxed/simple; bh=r1k3WtlUVa4rTK/Sle/cCczN6drEv5UqM8ZsPadNuTY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=uhQLLqev/mFYHnGmeNGwc6ZdHLMHXxnaz822Z8QzqPyPA4nd2XJezq6ZQpUONs+7mlY+ddWsjhWqrL6BQyOSIUDdjrLK0lCX3sYxDuPaWssYerWbcnJ8AsAUHy5a2E/gCQTPt0keqttkSEO7QTvF8b03yFSLeSw7+3qr1tH3YJ8= 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=bw1MasTN; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=kQ7TgRNF; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="bw1MasTN"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="kQ7TgRNF" Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAH07R021277; Fri, 15 Nov 2024 12:40:31 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=Rp4/S+tmW8/2EUOVCLf3AsFg3DSHJEhOXEcoD1rcL1I=; b= bw1MasTNWkFuZVbR3o6XxN66ieQ/YX3QDBIvvWsNaza+Ox3Ryru+De68e9uWYeb6 leAD4twaKOx+SWjdDDpE90+8BBffILVackfYgh25eElRfbdET9CPSdszQYd4IBcl CyG+KrsLaHXlqXvT4MtsSRSaSwCzGy/Ei/vJuhoLw+RdLFsfHzsIsv/K3w6cbUKr MFYzTxDCE6ctWvaURJzn48BPFTJxNA0PlANUXoVup2D22xP3SeuOZGemdHMKA98P sG9HKEFHEXzfXJzRG/gxb5ztArKZys7/nxtefJsM2HHzva/edQ7JqInpnySGaGBF r0EGhS4VJmfLrcawPwjN9Q== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42vsp4n13p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:30 +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 4AFBRFUS022725; Fri, 15 Nov 2024 12:40:30 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2040.outbound.protection.outlook.com [104.47.55.40]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42vuw2mrry-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:40:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=l2s/0hOMQUVF8gqu8v4G2siJBAmbt7kxoMnSjeuq3cKGUhmBrlKJcF/K2Hn1zl7bQYQyblcnbshrqm+xn3ukQsxrbwSFHV9pfKKmA30SCKpguOyUGLae2JIPFwP9BdIgPey5xlp4JxOycmN8DPvjgr6ebNC2Qyesy18YlWe2iV9/+Oq1b8KuZzAhHxnJx1Rx22e962LouDww3DI6D1MfNzt6bVNx+FkSqMeP2bkjoGoNWCNg0ig/Le6OxHvw4z5xsnoryvBS4m3EvPfnMF6JK0Wp50T6Ki5yu3cyqk2gViT1u6y22coSsCzAgmCxAxxAnFSBpYiB1k77iuLqQSDttA== 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=Rp4/S+tmW8/2EUOVCLf3AsFg3DSHJEhOXEcoD1rcL1I=; b=xotfYH7Sp6dp2dt6AD2NZ6ugWa8ReplCZqQAckkjvqUSB2zLWa1keNLrgFYxNQueh6L1VijiVMF4gQPiuSgDLgjNiVcmU3+3BHp4Gn8ycNR656xh/9BlBrUJzoIW8+CQs6iWPScaXYeW64Ls98/IWO9lp+OPbuOeVLJrmLSSdUZ9mX1X+Ub7Khz+y0e0KOura6Vr+L3vIy41U+73NxKfxY3spwHCf9Y+AffxCnJkEpdFQ3d6xyT/BSVKhzxf+BfsCUICvcTuGG9vYtIGUfwFRR/yIDt8D4+KpDH05qtY7+cU8atRu0S1QUIdwa9nmFuTmno/50uC7k8UpTpYJ5SspQ== 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=Rp4/S+tmW8/2EUOVCLf3AsFg3DSHJEhOXEcoD1rcL1I=; b=kQ7TgRNFWmh2CGOmBWM/rcgbip9k5ZO4a99J8ATnqQSarefgh3iY+k8A8F5L9szzh0970x4rJZx7Ghe2TmsFRhZrRwchvZ+c/ysQWLlCfRpioE2OHjjv+bNkdRTRitPiwlPPFHLY7UDKR5mqLeOUldxwE4p1clOR7TuGb4jwQuM= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:40: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.8137.027; Fri, 15 Nov 2024 12:40: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 4/4] mm: resolve faulty mmap_region() error path behaviour Date: Fri, 15 Nov 2024 12:40:10 +0000 Message-ID: <4cb9b846f0c4efcc4a2b21453eea4e4d0136efc8.1731671441.git.lorenzo.stoakes@oracle.com> 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_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: e08f1448-0deb-4c8a-89bf-08dd0572ae9a 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?68+7svkN7IVwsMmdsMGYPkSTXSEekuHO6bl/h0ycUqLV+lEfdDYdrbAJSlb3?= =?us-ascii?Q?48MJbivo6MaTM1LQj821tPoJIVqVgiHs2zELW1u7AXDQFbLDRUWVRSzUVCAq?= =?us-ascii?Q?Ol8RZuEQoWhiVDQGe1ECRvLmXCUlSu2hi8iPm3kAkBgeBOwWQi2LFJcaoKmt?= =?us-ascii?Q?lkwxXR/5tuhdi9t0Dji2hCwRpWGl9xKo1+YE5h/Unmv2YZS87rRmOlVqbxjV?= =?us-ascii?Q?OftJlCp48qy9U+dZZN6nhR6QGzjFKOzinUidjKFO3H9KEVHz3+L7/BjKyK8V?= =?us-ascii?Q?fKRnLZijd3qIuGfRqtFwxuoMVe6LrqSlq645NGvNVs2TO46L3YwKMPKVXRtp?= =?us-ascii?Q?Rm7oX61hp/QWfcmcyb23pV985F8VJ7RiqlUOWK8JP7f9nZ1sakfGBan5cXuJ?= =?us-ascii?Q?rQnq7m36+u25Bz7FwPbX4gnbSGr/2+CuHNNNtoJYOCgV7mXp43AaTTUg9pFc?= =?us-ascii?Q?ZVlDHRMH3B2vwZW7vtzQgQj9cPJJUisMtjqPP2Hvu3KCb/sAHm3OoFQCtOrl?= =?us-ascii?Q?A8wOvQPtFO/M4Q0iuSCI5+7ehfHVQY2ozRl7FvWWUilM6btVKESnpsPPEeKV?= =?us-ascii?Q?hXyD3WeNtbWdnKZvTUrXDHeQKW5AzzG2TRwU4xPRHkKE0izYQv4RVyqDK968?= =?us-ascii?Q?jeHbTyNWEt5RisWu3TpkEIgPXSElv20nZL0MtZ5ALqhAABN0jN42nDkBF8zf?= =?us-ascii?Q?GGwr9QoC6pOSfWhQaET6V8YKqGoHAUbLvyNyjRVQpZzMHx+s0kXjrBx+0fJc?= =?us-ascii?Q?d61CETrRfSrKqzc+TOQMHG9D33+katMUu9b3+Cqg5N0KvANfdKF976c84DIq?= =?us-ascii?Q?6nY8aXzdiDNilMnjqRcwshSk9/VyqoaUxSPjdD105fhgv+Rivzr6Pk1PHnXC?= =?us-ascii?Q?q5sQAfbZQ9P/PZmRSC1HwSPhafJPWq1P92ffOV8I1Avlef2LFBKah6S+tSTD?= =?us-ascii?Q?yBPHFKGSPCRnczeOU1MydH8OZ4XwNUAn6Tj65G2ZMwOC368VRujP0q7RKtgD?= =?us-ascii?Q?svL5uur+8wd/1hPqv9MoBGvjR4h6JfGdh5Yrqe1FYdRYdbcahixtcG3qOyav?= =?us-ascii?Q?GLdRMCk3Bu8RW/C9H2HnvzoXfi6SpPZ1kxsqIGAu74fYGk1RGC8OM742cShk?= =?us-ascii?Q?uI6TbJeH5EGLYnydSlIU8NqZE1dfq3FXffyW/s/N4SepKEXM2x3yL/xb5vXJ?= =?us-ascii?Q?QFdA00+RyYhv9DKXoBk9TnVT6nTToHOC1e1tphbRv2r/DaTq0Zb36xeykjps?= =?us-ascii?Q?ZMHKxcTjNYpShyz3v6r4vQaVV9BxpukIUdYO+K5t6COiCV/ellWOBzJlKFjf?= =?us-ascii?Q?9KXjl1NuxurXtT72bnA4Lnkq?= 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?O4MrEOe60ySYNRs16IM4VKIIp+3X+p4RneeSSYlgI35Z69ZO8j7bK9UEpDSd?= =?us-ascii?Q?mKNf5WoRjqYuwXWJtb9P/umkgB5iILwYz9JjXETM9T/vLo3p3WqYqf0e4UWE?= =?us-ascii?Q?cFrfgVKGun/nsWMi9oOD6en9jmpbmUA1/bf+OKhdoHdgYQAj+ERJNPYMBilr?= =?us-ascii?Q?y4OpWO4Asg7PvL0j6Zb9EVirjgifvtscc4/BMNs2Bt05T/4GsO+VjsbsOmtX?= =?us-ascii?Q?XQIzjnLxv4mEJldB3sL8Mn7t+gE2tRCmjtd98iQUx7zGfMIzbzmPPdABM9AJ?= =?us-ascii?Q?mjoc4vRUI1edn4pVB4mNkoxfhHHYEOPPLM+z6IhUeQ4YLQDOhhYVQNy3O8zM?= =?us-ascii?Q?7h/lMX+MVJmwJ0zD9OTZgKAXQu9SglOGvKFTV/Xwt34lWAjQ1zvEBDRpmcTf?= =?us-ascii?Q?NpTjB0VgMMDuHrg8JQQC7cUUhO+koAIouIB99jQGkYU6c+5MSadNZUGj4gag?= =?us-ascii?Q?KC8YFTmonKgUD4lOuHdC5IanIzEFxzKZ9WE6IbTCqDdvjbBLqP5ztCevCzgN?= =?us-ascii?Q?gMHTT0kiwlAJTa2OQGzB+pmYyLEJqsMOdPnZhYmZNok9Qo4C+N7UQgzegj6v?= =?us-ascii?Q?I6/WB3dDcNJ4u8EF1dVt6z35cgpkhynwtwbTvVNJlag8Mpttod58Ab9T3ge4?= =?us-ascii?Q?lWrjUbpMr9xTLkvpqMEzDetodRsMk1IzxZ59mGYCQNOkqPpa/XRYLYX5ah+B?= =?us-ascii?Q?PAo8RSy/YgAYdLp+Fqx0K1rJncNvlkGS8L1U06GFzu9IStEhQDi426G6gi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ntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 54DX0BpqaS3fSkEVnfivT7XgEIeJVfeDYFr6CE0xC5mErLMVSamWSeR3PWf1S+hfcSDP/FSEani2bffB+sG+z5CYhvij0+K4TnRPrh4DZEE2WyUj202BMNBDi4LqzHTKSWnEugLJ2bUzHzOmFmsLaBP39aLApMaokQ/noIegpGE3yb/xI2t+BMkBoNNL9GKf+GUS0EFsPTDjKtsEdLXRNeRi9B8LnDroL1LG09c0RuBDKwLQHa4VJ/AvAvBrtiSjIryQfPnGC5lhdNhg7xNfvAAL/ORJRp6AC8iERP901ODHXAct090oMwRK5UShfUJFa3ewiCc9ZkpmxdUKi0VntBsE8u/qFQJW6AfzVQ0oV8Decu8/2RvabTEYfSkzcIhJaY4jt3nqPD7+/z/Hh+s8aDI14rqHVBmiF0WKYWobdTIVEqxR5F/n4IqRgnVathwIYzBhWQ2fR7m+qZA2ZEF4N1ypPLJjfQ7kLS9CE70eLVVo7jKs0GaJ3TqTeIC9bYMnkWML9LhbkKqj+mwO8sGM39vN1SRx/28UyOBClOIP9dj+pzT1ukUsDUJJTPQ4IluqMD7wauvJBdkjUDnuWOyQvcPFzmw5NoSkvJ/oOx1NeOU= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e08f1448-0deb-4c8a-89bf-08dd0572ae9a X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:40:27.1288 (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: sSrmZdCLWUlCeJt/Q0SxWChwmYJ0w0pcswxmNRDl1KVh+f/z4SX3ahzSCJaFmeqcCluG2+24k+a5j4877plKc2wzsoA27W5HIR1nVf8gnCE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: 6c584fQVSkwUqMk5NVXNBP2KtXnSjkYK X-Proofpoint-GUID: 6c584fQVSkwUqMk5NVXNBP2KtXnSjkYK 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 | 103 +++++++++++++++++++++++++++++------------------------- 1 file changed, 56 insertions(+), 47 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 322677f61d30..e457169c5cce 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,7 @@ 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; + goto file_expanded; } } =20 @@ -2800,31 +2802,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 +2833,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 +2862,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