From nobody Fri Nov 22 22:52:05 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 A2E111BC07E; Fri, 15 Nov 2024 12:38: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=1731674344; cv=fail; b=JdJAHIxwSSZdZ2O0TzuG0y/KVsRYASappbMc4I4ndPBMSBuEsgzlnTppri/0cPo5ma/xdF5gSNiRSa7o8dWd916o3lPCHYGp68OR3jZLPZrfm0D+dVAMmAx8IfIFK51/yNizhQKj4TCTNJgyl5kUUoLLRLTWykGPIiytRDnomKM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674344; c=relaxed/simple; bh=wWWRmAPYqFNpFH1xWAQSId5YVH7Jnmaw3Uc53rAPzvc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=OPrrNOwpFM2Co5xOvEMrxEbKcpFs80Oc2MqspGI7CxcVOSPGAL1JrEb+G5HS6dtnEStNmfk/h+Fmh6fqAi26ogpi3rXGOIj8LrYXAlkPsG+HaaKIygJvA3oUizBvqlA0M4t8EyC2ZVcy5PjqKrdal/dAJvGXDuUu7Ur/3eCtlhQ= 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=WdS7vL8L; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=h1QGwClw; 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="WdS7vL8L"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="h1QGwClw" Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAH04M014153; Fri, 15 Nov 2024 12:38:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=lN/v8jAJa6+T87vYqjFs43JvP4ZvGI7KJfkCLwPdhVo=; b= WdS7vL8LpfijFgPkKN0TpoKwBcFDOaK2VU8+py82NU6LNiOtFFi7qP734UxMH2sM +DOooQ3XUl7pjNFB094+OyJUMLt1TPO5U2yCv+RAlWIh72BsvnAEjUxEymKBA8Cf lRjcmo1cPLiwyYFvc521JOb/BmKbKaI8WwNtrBc7yrA5ZM1UtMlrgwrEONIvHlWP gDBss1HUgGb/8xlneldmrJ0c84ZMCPxZj+ZDWB6j4AGq5dSUnRbgUpdPVE149lUq hAWy3mMm4es2CFkq5h5u/MRUpuuDkYWqcu9YOiVbfO6hCOcW/BK2lBBSYQSU1mmD tVtALIuphRir6s7AwLp0zw== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0k5k5td-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:40 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFCYFXU005704; Fri, 15 Nov 2024 12:38:39 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2176.outbound.protection.outlook.com [104.47.57.176]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ckrhd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:39 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=IgWI0U+OQPFs7uyBIlLwxrFZOnSseSckxhEJm6HtaRGHeDRVyJAeo7RvdVrLZrdsxEWoU6jWgnLVj1GXeJn5UcXoSuI35Vc7wUw6Bv+iJ69TqqrH2wu4qhDJltrlget833o6c8JHddaAz/HOCypE4674SY7LEWYysuXCgxJIPeMIm1giTogctaBJXS/DBBUp+nkTr9T+vnPsRF0FTnR8PsNHvfZ3xjamuUvfeRvN9jadySvHKnqXKxYK11rRJ3ee7bJt0ef6D3yWcjbtILNssrw+VIScByecwFJJaNMr+67qZJG5frUrD4Z4R5bO5eENavsdz9XFFRJpQiXDvx/5AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=lN/v8jAJa6+T87vYqjFs43JvP4ZvGI7KJfkCLwPdhVo=; b=ilGwea3wHXX7wAoNivuoIsn94r6gOY/DmXucCX5TM5AtK/NPXxq9GkCLPF+Xsyz0j0K0NiabpQ9b67iKxmyIv615ebNBq/ve8BYt9FFfThbJG9i/FuP4BRpTEI0jQVfvqcoDJP/+Y+XEVDVR1J6bch/RkzujLN8mxxjGC/TdUSsL+M90fn5+7z8jJcCuxpA81awOUnDVsIVckX4TUwcksB+l51BeLSTSgKfHnEiNW9VRaD1WAyjp7+RwBcvLThnbFRATytG1J1pZ/kOHy8DR1AAxnCIZ/UlD0vaNzTILm+SNeYmLxfkt1DxDpHbh3Hm6DAhKe3Cg8qIbj7SRFWY4eg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lN/v8jAJa6+T87vYqjFs43JvP4ZvGI7KJfkCLwPdhVo=; b=h1QGwClwwZnPxlXOx1WvxAaPzEgxFEmo1ZXvO390ieBxHflw63inGRYEdBta3e3kRyfTFrmztRCvTQytIpHipkcMhNgw1bI3IxqSfIvSwkl8bCHInoV8eTkXrnbtbO8j+H3McBGTpT4hWxmxR8wkkwVAsYik0YVRy345CsBIoYI= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:37 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:37 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 1/4] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Fri, 15 Nov 2024 12:38:13 +0000 Message-ID: <2eb0f68593bf50f79cd346269fefe596cf0a6801.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO2P265CA0037.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:61::25) 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_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 675d25c0-f6ee-43c9-c212-08dd05726cdf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?BIkdN3OjDLNKNa/xPLz7K3qR7Rqi/83HSDUIWFxg3YUq/HLUuuR2K+3Znly1?= =?us-ascii?Q?N71mmSzGEMBBWq5momcK/p4wJLappmAUCSDuhQZjKKDVxRWD8230Z46yk7iZ?= =?us-ascii?Q?cFpYb1Hs8iMhWm4gd1y/8Z13NbLmqthGhzFL6G9AjkX1rWEL6KAlmQcGqArt?= =?us-ascii?Q?ZUuBeiPm/ZCntUntAYyW4i91ufKeWlkOBxIHQjyjKTU4qGfrflDGAHnZgumv?= =?us-ascii?Q?Eb51zM6rNHjLHt45rKk7UdM7tlr7D/1AQHuYq5UuUv/cgN2oHrnVvDK8B6mU?= =?us-ascii?Q?8fmXzGiPWZCo+jWhT7B6IR+L39TxlbyuTCCOCKgUUBcnpQ23VThw10+o47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gJId9lbQ8//p2QbmHAklIFpme9BkRw6fjl?= =?us-ascii?Q?WPHB9H97kHrjmkRQPIuf2rRNnV0h7VQp7d4iR5xCkDrqMlCr1z2987Jogink?= =?us-ascii?Q?JIp0HfEdi9gP50JCqRygTSjB?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?P6/IOF4+K0Jnnams6Tcn6/xZ9ybPykiwyHP7Yetf90/AibV5xJeKTcIMefJJ?= =?us-ascii?Q?kD/O21MtNXvhTs6hUTrzgOHuJ/ptPj9I26hPEYnTa9Jctqd7+gpjgWf5etdt?= =?us-ascii?Q?DQ7WFzuZiWosYO6sPS4eg7X8eeTUjU5vxIcl7Ju++45lBmKIa4qQ9LO/CCA2?= =?us-ascii?Q?LV3FsmRLScQUvYes24itl4Jx9WQc6yVWj+FGurqZdroSzgleN2haI3TbR8Mc?= =?us-ascii?Q?qd5YtW47KDu8QBURjjZALQeiq9UKccvoovp5dW+XQmznkhjT4mrfT8TVrgS2?= =?us-ascii?Q?6YqQfvVJKqtQazDWPvqc0Zt8zlgs2J0azfgGE9C+T6hpscoRJOFl9wvt4bLg?= =?us-ascii?Q?NgStHiOyWpRsMyhWJvQu1m1CKFMUJnDoKA+M7fU52W2cZF+AK1zbsHnpqCoH?= =?us-ascii?Q?6mBQ2jQK00ApHtaaKKEYQjM5GLpIpwLBsqRElBvx443a48NRYFm6pvCCz2mX?= =?us-ascii?Q?hJS9ZAPajiGFCjHhLM/IV9ZmPr9STlC7IapA7bvtpn31UDidqCpafdq9Hkal?= =?us-ascii?Q?2/21x5bS64qDXBBLewlHvXLOWD5u3bcdLwODVK/W60A2f/8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CBlNS1acWcB1ug28gphqGwIQoMcTylf1kBeURXFgm4mc4k/dQm5?= =?us-ascii?Q?07uq57SodAeBnRdmsADs56g3lWjwej+7eHVml61UecI8qOTSx2bnayAXVhjl?= =?us-ascii?Q?51uIPdDDYdL+se2B/7Wyr4FtjdL4rtiFikx6tcI7tiXWAqBebwpxjj4PAA8F?= =?us-ascii?Q?sA=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 4Ct3uqx/dOe24ehSuqGakrWUVplW2Qybhk4XkLN6Y+Qeu7GzT+7lPe7+fiR6Vz0oSb3Jge09zx1H6DX0ExEOdVvSVcSnwgplJkxG0sR64r+sips3tXXblB4T2eaf681rMmpGWZpj7yWdpyKkulQrM1RChDKJQFSkH91tYgIQhdt/2j96MD9Pg5eGwIHpfR038krV24MOUSsu+YR2Wv0XR7z6SLs7MYPdSG5qJ3FuxPcqOgTjdAercJp57IvmE7vJfdBvfF1mjyk80Snahf7vNs6cmTVz+qW8EDwojTAgagl43JCr9LrrhfQy/KIjecWdybFWKmY6l2ZAHDNYwDg20Ifo5FfjTwtzHWXWOnYfjUClYpuHsaGqBzmYrz7LLdtrlU3SIupAc4+RzWfHgfaHlKpDPTrzA4qOemS+4qmWWPKH0qLGk0AvelxP+wSecgbg1LCGvkfjCIrjM42AXXVbJGjDrIUJdHJGJZTaE2Q593DUBuqgEe4NiasZSDjOzBnpy9+7jS8Bj9itx+SbeMhsD2vvJahtSXnm2h9KtUi0WIuCQwN+4gsw8aBvSPSjI/ZIyLRkMVW4VbsKz5wVgbujkFRCwkXCiPr/99HRTfxTJmU= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 675d25c0-f6ee-43c9-c212-08dd05726cdf X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:36.9689 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: wn8gEn56I/1ow84ctn5UXB2XnJv1OVyOJxXzqGP3K8J9msacuZ7L2aO6fAxv9fJtustCh/rd8oAeQF3QrosM/bRibZSmrNoVqc8RkHw50Mw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: cO9S6qe0LT4Un3PmImzeVCGMjSnaU9P3 X-Proofpoint-GUID: cO9S6qe0LT4Un3PmImzeVCGMjSnaU9P3 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 cd444aa7a10a..4670e97eb694 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -34,6 +34,18 @@ =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); + vm_fault_t do_swap_page(struct vm_fault *vmf); =20 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vm= a, diff --git a/mm/mmap.c b/mm/mmap.c index a0a4eadc8779..11d023eab949 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1788,7 +1788,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, } =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 @@ -1803,7 +1803,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, =20 addr =3D vma->vm_start; =20 - /* If vm_flags changed after call_mmap(), we should try merge vma again + /* If vm_flags changed after mmap_file(), we should try merge vma again * as we may succeed this time. */ if (unlikely(vm_flags !=3D vma->vm_flags && prev)) { diff --git a/mm/nommu.c b/mm/nommu.c index 02d2427b8f9e..2515c98d4be1 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -942,7 +942,7 @@ static int do_mmap_shared_file(struct vm_area_struct *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; @@ -973,7 +973,7 @@ static int do_mmap_private(struct vm_area_struct *vma, * - VM_MAYSHARE will be set if it may attempt to share */ if (capabilities & NOMMU_MAP_DIRECT) { - ret =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 973021847e69..f55d7be982de 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1086,6 +1086,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:52:05 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 27DD41BE251; Fri, 15 Nov 2024 12:39:06 +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=1731674347; cv=fail; b=Ajs/0KD87VjabkU+aT3s903AvadkgDUZzVFVBzm/FRH02XlPztYyCpE9+2avY7CAKGsgbIfxhIFZKo1eAa79eis8JjoIbB4b/sPoLicKNlA59Ly1PVnM4jDHlPLqre9gZC4z2dimGwuD253qZ3mSqhZkc2DUPqIlSYk/OEXlSls= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674347; c=relaxed/simple; bh=Kti3uNG7aFoGpzHXxfC7eO87Vzr4kHUrcn7f4Sa/hNM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=hHhvuPs6MqFQ70R2fKeJ5b6QB2sLCu7f53ocn7hCyDLd41ck/41SJO8Me4H/l+pkR4sDAXk1QJNSt1toyRXrvKlsJMkd8+BfPYC2aN+P0EtkiggGQfa5rxtysaemiCFcnrgCEjDpLmCQOXncjqEJPayWSzGX9K+GDbHS92npDGc= 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=X/DDeoNm; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=A6MdOwnY; 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="X/DDeoNm"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="A6MdOwnY" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHJXb000670; Fri, 15 Nov 2024 12:38:44 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=2hzJyGd7ND7ALeuDMST/ER3oYgX4pWOPUyctcPhuIyI=; b= X/DDeoNmCV/yVvjp+KNdjE4YqXBAdXO/F4h3u9GT7jNtMDnVFFAzF+en9m7zT7KS RYLXKd3EQjxsafCTFY4DO5Yu7jGn45UunTbGnGsEHP6rVdlbrSpTrFblWIQjmMNM giTceEUgK95vTYePN9hpAgpP1TMtvWGDV6Lt61yDdtfPwqF/IGWt1tBbOHnrwmLm VNQgo6n2/cM4nDIqAmIEDXClG/VhJJHgnO/gg3ozdPS0/jWWoCg3d0SdxfMrv3no Ec74smptQ7DXc0+wK3sDh21ZsDURQlNm1F3ZnB0Scd0/se0Mr2FMtP48uCgpFbRI 3gncWvrWmbZOdjZrESkmIA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0mbkd8a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:43 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFCSLMg005652; Fri, 15 Nov 2024 12:38:42 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2177.outbound.protection.outlook.com [104.47.59.177]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ckrk2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:42 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=i7jJplQw2gPo/LTrXjn/aTsnSlZ77TjLpaDn5g1Suoebb+idack3BygnEbtc8Jq0PDoK/lY19YYtIHUubwHyqxTJk5fRR+9+M6YpEgS5wkZkMZ/1eCUQQ69+0B4iU3L6kzEEZp13b1IhqaYyKV3f89xRXBGioMd4Jyi/cYHspK0kKEtG3gu84DZqc/9u4SJqqmJxsP24IdIaz+HU0D6IJnUk+wvWW1SU1U4fZDzUZmjD1/zuopMWQzg1bEZ/Wlcz/XPcULNtjwdDItilgl9iHemrkMUff+VNV5cpQB6ycq7zPcFAyGRMrGsVr0igGyYlK9wzkPi5X4rFkZn+0aZz8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=2hzJyGd7ND7ALeuDMST/ER3oYgX4pWOPUyctcPhuIyI=; b=cgQgaU55ZrRKhinXW7HhxrtLUu1ENynQyVjzxcJr+W2hpIAS0ajHGv+LN/UuN6Ojp5ELZ0gwwi/wc6L1fpmiTmlgGCzfP+/1lvLoCizGcNUj9prMjaxrfWHKqEX6tikokPslEWFUUufYYBRdvlELE5T9dh4Iycad0lje9eL1dwgqJPyi5/WMGH7PcNmR3HYXJ3NewyEaOoBN4qnUd/wNgLrSElW5M6KRBeGttvI4OdyhSYt1Bi4XDMWvnCaR3FEo6y9Tx+UWohDUKJgX034KNOrrMs6PZ7FTifcJSSWAum0CAm2IEN3gneScUXVn3QInDPOIs+r9kBxetuE1UkQZ3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2hzJyGd7ND7ALeuDMST/ER3oYgX4pWOPUyctcPhuIyI=; b=A6MdOwnYAuiThhEskYA5OwVyfz+FCAhviu9pQwyPuLMbVxF2/YQf1C3G7dy8lIz1hNIsJmJq6BbrgPH7IHGa6w4A4kMPVS9ekVz8hJsS1IGXfD/Hz5AyVhl5/uhASbSFfVUkgU9AUH3OnGzRWuflPUj7JG5CbhUzqLbtIpXbK1A= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:40 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:39 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 2/4] mm: unconditionally close VMAs on error Date: Fri, 15 Nov 2024 12:38:14 +0000 Message-ID: <9ccad1c5a53af878459f32ae3efaa3d12d33e4e2.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P265CA0178.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:311::9) 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_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: d2a9324d-82a8-4efe-9053-08dd05726e97 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?4n8N14UzQVWNUv+8o3on9hFj5b1tH/5rVdModFiOHYUYtSlUptbWla9ewpXH?= =?us-ascii?Q?oMnzlxfdW9L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= =?us-ascii?Q?kp8w5ZAIhRchB3Ee3JMJR/gMrnq9HcI3IAoujLI7OKFGN0gimFKPLRdbl0DF?= =?us-ascii?Q?t/sNPdkr1x1IHdz9RpFJwywHyE/k3ZKXyYm6g2jyI9k4zOuQ/LPRkPj99qAR?= =?us-ascii?Q?HDTOwSIT8tAIgBQumKTexIhdAx+SMYqzKdk2JRKsuSifUroxnH1eAP4mbSfI?= =?us-ascii?Q?iD4+Jcq/r5FxSEORVXtFBfEF5fgMAE1fYMSjzEZllTfDPvrHhB7xXk4bIp3H?= =?us-ascii?Q?deQ5fUaJIr5/QZue8fv0X+8P0qGGvgx1zao8SVGvbkupNUEW673vBmivbymb?= =?us-ascii?Q?EAiFp5UWw1IX/k/MGAzAguLh/VHskpjBLXNNcnW17vg71JPuC9+Ba5SPYbjD?= =?us-ascii?Q?mj4BfmqgaZHsJShBZse9+aQi?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?sYXyxdEAs2iN+EmJxpjuA95uTTsVIbNrXUHbSsX9ENtGfP8YUwTTjIPgKKON?= =?us-ascii?Q?Fo4ltDa3rrvPQ0GxyRy9RBAXSY6Y/3mjAahYMvZjbgZ2j9TVW1nTMcs+Bfdy?= =?us-ascii?Q?h8spKKJ3Oo9O9tq61+zF3EpZBxLuwX1q/7YNQAVocrov1cZlGR3mpOzSdmYx?= =?us-ascii?Q?ObD8g5tyBd4wPhA0W5Gbtf1yVl5eE66WOAiWOC3YWVSFzGXaC2G0x9GifBI/?= =?us-ascii?Q?A/RL3ARIu0JSzfOoLoz96XoOZwvKLkS3QDzTTP1wfuSPkfvuJYipAQeIKRqp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VsDpCFwqkp4SOD?= =?us-ascii?Q?q+unX6vRqZ893X7/HYoqV1bmT+pDOf851ETRSLi/YSc8JYE6SUp82ib2nZ8f?= =?us-ascii?Q?YYUwpqJimeVKRIFQQB+Dbci+18n7F59fiXpLGAbbqrCsKpq/JNqgyuEar3w6?= =?us-ascii?Q?pZ5y5OOdWOpUu63mjyqzVlrMLmWCAFC+gdTKykeT+pwrAIV09ZguHYVZTKM7?= =?us-ascii?Q?hKJYzXkINlKxTmMxYrWOCpkwM/7weU5RE3tEj5CoF+wvWLMIUuI4yIj+fGso?= =?us-ascii?Q?hBVlDGjgfqXy9uT12oDlA1riJlXIBUfsrZONosxfbd7xn3/T50sK4IPMMQLD?= =?us-ascii?Q?Tmdyz1t2xhievrr9h7Q05jniY9rWPCdGzcGJgD6z7erDAsVjVY9DN0ydGFnH?= =?us-ascii?Q?KaM3OfMOfwu2Wvm/v2nL0R2eNb7Da0xulbb2KQghb3MHhcPCIbu7B9i7XYrR?= =?us-ascii?Q?CQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: Mt0exN33bw7ylLY582OgbW0wBTh+AIleWkjgU4YJymM+v3oFg/7rXp+T+wtymA6b3kYBkJZH2uW6AEJhBRhncOBxAPJNVgaP75F6ZpKDpluozIJItyIU0Zq4C/i3/P6ug8viLdaQvig5GSIcZObusHVlIY45Vmdkmwxs7e0cCzx3Fz1/YinaZNvhvpMRRRqW7cRE1Uumy4tZwX8zy4VUoQ2La2jV1t5oHaQrAyrwl7St06wAT3F3cP1dNeT5NvdU8F57KSxQgb3cL4GISpOhaVs9h2SE2fbw45OKZFqPJ19aqfbHGoOkaQhpyPzaVVjYWREVRuBwopyQCr9G/bCVTx1NK7f/NYTtJwrb4rqHlrF2cUihGYB7L/53oUH2tjdFbMZoH5v7pkv3rwSOyL9yAk59hO+jlmiWViY7hd/be87xUpljBEDp7Hc/jmq0c6H2sPla8V6mFR9oXgcNotvQ34mTZBXEZO+VAc64ddL0Pxo24O6FIWl4uxD7MJit9hHH7mxAvzkoRAeMqp8PL7Jg56GVEG10qexvwO05N3kMr17EQZTSLjXdVBm+3LRztOg8cKOHZZ/EOdwlyplgQtBc9JRlTrumnI9AcQWZAHCDWLM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d2a9324d-82a8-4efe-9053-08dd05726e97 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:39.8749 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: NLbdxDrRFMM9Mw5kbFee0vE79mQlHWQJ0sBAWoeFouRxffKPSwtVEBZmgEirIlW6rd8jZ25LAR8UBfGMfkqJv2iY6XY/JBvlUrixPUusMe4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-GUID: wh8wQ_Hw_9pJaiqJIOVj1fBgzh5hjuFV X-Proofpoint-ORIG-GUID: wh8wQ_Hw_9pJaiqJIOVj1fBgzh5hjuFV 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 | 9 +++------ mm/nommu.c | 3 +-- mm/util.c | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 4670e97eb694..34b3a16aa01f 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -46,6 +46,13 @@ void page_writeback_init(void); */ int mmap_file(struct file *file, struct vm_area_struct *vma); =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); + vm_fault_t do_swap_page(struct vm_fault *vmf); =20 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vm= a, diff --git a/mm/mmap.c b/mm/mmap.c index 11d023eab949..d19fdcf2aa26 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -180,8 +180,7 @@ static struct vm_area_struct *remove_vma(struct vm_area= _struct *vma) struct vm_area_struct *next =3D vma->vm_next; =20 might_sleep(); - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (vma->vm_file) fput(vma->vm_file); mpol_put(vma_policy(vma)); @@ -1877,8 +1876,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, return addr; =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; @@ -2762,8 +2760,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_= struct *vma, return 0; =20 /* Clean everything up if vma_adjust failed. */ - if (new->vm_ops && new->vm_ops->close) - new->vm_ops->close(new); + vma_close(new); if (new->vm_file) fput(new->vm_file); unlink_anon_vmas(new); diff --git a/mm/nommu.c b/mm/nommu.c index 2515c98d4be1..084dd593913e 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -652,8 +652,7 @@ static void delete_vma_from_mm(struct vm_area_struct *v= ma) */ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) { - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (vma->vm_file) fput(vma->vm_file); put_nommu_region(vma->vm_region); diff --git a/mm/util.c b/mm/util.c index f55d7be982de..af6c9bce8314 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1104,6 +1104,21 @@ int mmap_file(struct file *file, struct vm_area_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:52:05 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 7F6F81BCA1B; Fri, 15 Nov 2024 12:39:07 +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=1731674349; cv=fail; b=rjehO6Q0u2OZQHN08Te/BwNjKcfogqG6fOl6v9mW64l95xPXiS5pUwxURMnPFZItgT32cE0gLvyZB5hKC3PRyM3VepUys1nCjfpwGoATVZBtvIkTr38YA4I0jp8t/DMNGpPWrKyYAqSqH7M0g3acMz3Ou3uEkQabW0XtsuFyBXs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674349; c=relaxed/simple; bh=9yoifrh9O/uj5LEtXOMU/Jj+kHY61eVyJaDqZ5fOcgY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=mhEBEoTL1pdNC26qMqPz0pVxQXC1lB/SXjlBBYboy1MxXZTrwTMlZVa4IrU9aHLPHILZKsVbdjHVrcG+iL5Pz1tcum/CdBJaxquE6znDMoHhGjZucj9YncorYM6vYlMlYFJkR74vMutwT0BzDDdthW9dnUium8JVzu5lrEWWmjc= 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=mezBQECR; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=at8foYZX; 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="mezBQECR"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="at8foYZX" Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAH4VW030282; Fri, 15 Nov 2024 12:38:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=Fm96MfUl2UnxcHP9gVllVpK7rAL3yVbYJSzoyNNjI8o=; b= mezBQECRGc2IwiJCFxEJ3gs6yyC070s0LHbrgrv32qA/w7b5Uyo6fSGxlRiVsisD C26vTakYcNwsTF1/ptBIfGdT5kmBgtIaaN8coicqW3Jig4SJApDov2Y5Jh1tzrlj d/bBMrffOdb3Q5yuajai9k1QRuTcA27AaJFuDD3KrCe90KF2N76/66fjkbZVtbeh FhddNXpmfZwWaKs/iAnzyYyybt6oObaVOt9wosxNN3wM9PX5EAczcutlgrNB3/1E CC8sypgmE7i6c2HAI97v7IFo6Nr8YcbSxFdhlCEkAgKYWmH8xrK8rSHYxvJr4JsG H8V5LLNH4GfFLaJIH6bAog== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0n539ue-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:47 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFC2HOA000547; Fri, 15 Nov 2024 12:38:45 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2171.outbound.protection.outlook.com [104.47.59.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbje9e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cyWv9Z/4X0b1nBEHdoi/4im0T0FtZq04uir+uH8RZVlhlhf+EyFYxlnZr3mRXZzP04N5l/2/NEIIU8OY5gbEg7DBlmlDi0RfVAET3cmeCICUHmLSVe35CCNCQFC/bneac0YOzNjDDGWH1ovVmlBFnxG/srxi15UgNNN4DpwUzk+8q+POoHFpdwuDsrDlayA6tb4sL3BchA+u1PALcLeTzFWN2c4mjU6jJQom0PzCatjzPDfiDl0wNIPgXG27nlVd0UaRioKh8BYk0NFa3Dqm+oA9t0YI8MYAYIVuqwmD2+JNCI3K+zMmVqaYcssXwOLHGaioSI9Q6HhFz9NwqshrCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Fm96MfUl2UnxcHP9gVllVpK7rAL3yVbYJSzoyNNjI8o=; b=sWugTZyAefYBxYN2Rb6Po4ZC6Ir8XsgN93XKPn00WHA+fTBB+krUestFBCZXvLJBJu4HbcsJ+wkBFnGhC1W2Ll6bynDiP8A7McNWol17LDsMG1zlYXaEAshOVI/PYCYzwAFuoL8e9fDETrHbjUSNhGBexSnNrlm1cOtwU2sxOgG+WHqfX1888E830lzelpFCjBro63hWQdJW168XQzhz7/tdSKvQxwSQnv5pZ9mKmibcKwkeZjtzawkQxPLhpuh5ynP4hI+VvxrlpPA9U8l7luOcjNqQs6Q+60QksMwCmFddYQT3Nui+i7bJIw9OvwNoznFhzqpQvzovIJOvC0/mag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Fm96MfUl2UnxcHP9gVllVpK7rAL3yVbYJSzoyNNjI8o=; b=at8foYZXJQVFfroYASm4HwgTwQGqfwd90AD6j07+Yyb/aLYWMPDUMPMIAfZFe1XzWQKQ9Mz0WOQvLPVxvwGWB88Xh2a7YYaPSSurv6emflcWLIrdUczBqP9KQhE60Op6uLZzjUu55R1lTGhRHDOmZnlmLFUxfUXfE2GdP0KibZQ= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:43 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:42 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 3/4] mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling Date: Fri, 15 Nov 2024 12:38:15 +0000 Message-ID: <6dc9d6d69584a326d7de66da9cbaade9ec64e4fd.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P302CA0044.GBRP302.PROD.OUTLOOK.COM (2603:10a6:600:317::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_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 0e4bea6f-0847-4cb0-b893-08dd0572705d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?fBbvKZXUTYonY97A1LWW9SsZwLZGzPocv3MNM8gBlDCWGCvCTaEn5nIw3cg1?= =?us-ascii?Q?lng0aT3qznKTIySaP830FQNvuMRdtD/zBSVw31ZIKIITkkTQpW9Sr3luKOFO?= =?us-ascii?Q?ygeij/ocOFTk0ol1TevyamaNDmVKHYNSGEw32RerIm+66ejj0uGqrLo5f8Tj?= =?us-ascii?Q?pwiNfGZvmlqRxfVjyejvbX/Ux0RiW1vPc/U8c3KpDMDRadSp3fpASKDFHw+0?= =?us-ascii?Q?iKY6vsi2pxH+TYMXhHIzqyuwHT17/bNsrwEpPWLfX45en2cb9d4gU5Nhf0gg?= =?us-ascii?Q?sXDCuhdzpW3r7sVDT/2GvaNyfxkSB+3CWuIgMv2sh/0MhIH4NvWA4UrV45Ys?= =?us-ascii?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D0OOfV?= =?us-ascii?Q?2ABIw683qyoGdVtzTNKD1gJ6VAkG4IANDCEdnUKugfwq8Aj+KK8z84yEuY61?= =?us-ascii?Q?oX+YsJ5PcVI31i7pyb+gZk2I?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ho4h2/o0sU97GKjb/n8/X/12eetAKml0oaIbqL/pTEbV/tJvmbGKiPhRAz0R?= =?us-ascii?Q?ycSlVZ2COtReoqxXPQqtq12cNShrrlmfksdghfWSgNQBJ8qu7OuArVvVBsv0?= =?us-ascii?Q?KzMz3b5tyNM1hBCH9yHcxK9JfUV2rHC6g5GGmpcUL08EeyX3NVYskV3PD+ul?= =?us-ascii?Q?xfEPtM2RPlNrwIM3c3pjYWE0cgjAndUCNW36mh9Q8g1x5dO5cKgEtYNUJ91b?= =?us-ascii?Q?aU+hpdJEr4Xblsxh9OGgpxLOc+N1JuwlJeYaTel8EEm5c/PlgJpeaQu3DfF0?= =?us-ascii?Q?BEgkJS9ixo6xa8vON1h4rGyrp+ILqFSfvJhhTijf/aBVPZWk5WIsWc266HKJ?= =?us-ascii?Q?K8SBaD2GmXVcuP5jbzHUJy/7qF4/Uw8XCBde8UCBxl2drTuoI4CxiEdTGg2E?= =?us-ascii?Q?Pilj0gGt6IXex2FJbtbEXKsVFqS4veELctgw0tGMQaBNobO86y2okBXn0QMt?= =?us-ascii?Q?FdA7BhMunlCOyKL2sH9iXATTHuZ7l9lDNeqxG5b3hjvjL+xs/Up/hKF6dGru?= =?us-ascii?Q?hfExkau5QpeIVmOltoN07elKpnUmV78FKM0VWKUiNag7BVlX1DSMU1XNq+EF?= =?us-ascii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Io/6ig9jGYukNFswRJ?= =?us-ascii?Q?n21THooh/plvMFPgCPyhIMZhB2PY5p1wNKv2UODCDay0BfmkPvwkXkOlBIMY?= =?us-ascii?Q?BuhXpCIyVPrrMy0vLJejnETG+9YYK08OgLXaz+wrHFucKl/6GGLP3r6OhyEi?= =?us-ascii?Q?CQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 284QLRzBHg60yERX2fvxnARsLLdrXxeEJxvxdBsAQd5h5JphhtAlxFamsXC657prpGRaMznEs0tNi2A0GJw6v0IrWKa7rdrkSI461NgnU41Sm2ZtyFyuq4FVg/VFlbmcO/HOffIbgBiYXlQU+qM7Ud9PAVmu8VSCgxOBxOReIwNOd58J8SqyJsiYgkPM0YQ6+MTHUFGgRK5gGhS89JNGX57fjZ2jEuw/IWL4U6Bsd1EvptVjHSR0xhC3sTkZG09n49B/JXlPXlOy4huDcAIbO2q/wgU20M/DLFSUOXXBf/Auqw4rpIWYKYSvVBAPpd4ioSQV9x393D+HmlKpAKRzE+UoUUiXzFnZsrFNkdirylw+Tiv/rF6T7DQgAb/OIr3OIeeKcO0BqlCwndfzLfQyrsrJIIM8f5Ad0j76ybJgmB2nXPP4Rptl6iFem6qTOTuLfA9FlhemBfz+0SND7Jq5+txkP9ZV/wcbccOuaumK3vkF6hcY8dxHbT1fNR4kgZ7UcUbu0rD5IM2DSikrtbFL9Lm7CXvjsAZVOyR3GHBXnecQDpvbrQk0Z5h+OqJSIAkI2cXcbbO+sPpW8D1q5kbS9Oe1Ryij3kabR+JtxV4uoVk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0e4bea6f-0847-4cb0-b893-08dd0572705d X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:42.8544 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: bHqER+Lh+PzR/BThzsrOcp0b9sZJaX25HZWadk+nxRB9/jUdWqd8ppE5MDb+kitB7EKx2AscrwFnUgf1oDH8k8dYK+qUHkacvD4+ZpJnhqM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: VpUPWNeS2cLght2ZiN1sqJPy2YAHzn9L X-Proofpoint-GUID: VpUPWNeS2cLght2ZiN1sqJPy2YAHzn9L 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 e3e28f7daf62..56bc2e4e81a6 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -3,6 +3,8 @@ #define __ASM_MMAN_H__ =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 pgprot_t arch_vm_get_page_prot(unsigned long vm_flags) { diff --git a/include/linux/mman.h b/include/linux/mman.h index b66e91b8176c..c5569219de01 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -2,6 +2,7 @@ #ifndef _LINUX_MMAN_H #define _LINUX_MMAN_H =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_vm_get_page_prot @@ -151,12 +152,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 d19fdcf2aa26..a766b1c1af32 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1466,7 +1466,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 084dd593913e..8b5c95528b00 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -906,7 +906,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 cdb169348ba9..81da2c3debdf 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2268,9 +2268,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; if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && --=20 2.47.0 From nobody Fri Nov 22 22:52:05 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 B0BA11BC07E; Fri, 15 Nov 2024 12:39:11 +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=1731674353; cv=fail; b=Uh2EGOamCTvVRiwDYSYtCXrrjKgfwPod/7FnXNrUdofi2xefOLTfBWc3BA8/GOgfDSm+WFTZAb6ESwR/KKbrB+R5KN2wWxb71gX9hvevQvCQedi0j7ND/uClUB98mdRLAQe+HDRdy1dWMCpfE1krGYmKSG6r+8GfQMKm7sri+4o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674353; c=relaxed/simple; bh=o/2flovik/z0yUaXkz0y5sobawqIjNYQ0dcvGKorQfU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=uHFNYvTM0oyohzMEKpka98biTFFAp1huUd7OYKuHdjju+i5MdPSewKJDWeib2I+fTNw6un20LfDIuFuhTNLXd4QRN5ptTyryTrBovb0i0fBvpeBOojtm49sPmn4q6L+AxrUeL+M8diNkjbTaiQZ4EKaX7d6fNT05+R2LHnHY7sE= 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=dGCFj9Ea; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=HICE8gzk; 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="dGCFj9Ea"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="HICE8gzk" Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHAGc014379; Fri, 15 Nov 2024 12:38:50 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=wSSFsDJKNEnL2Ec93aXMbwQXwjo0SGvX0HMoMi4GdeI=; b= dGCFj9Eanr0yGHt4VxkQ1iqtT3dIzpkMsusb/gegyjtrY0SihgFmGZdcq5QMvTeF Iit7Az8zeRttXJ3prCSufVcONJe2kvNJdjyGWkj3X59jf8iS9/waPcn10lfVod0b iuXaMKwe+UCF13Ab98TOU3yKwiW/OH+0ApEpRMWWhqdNOfcQbQmO1B5rnOpbSoan dsWJYc7PeZsat4PIRK1jkeao3cZiaMGqXtkqGj2pSHhQuUt0YMwWnKlabZTJYg4A nOle1mKZUhC0VNgRXOLU9GynpYolx3S/oL9Ycru72yD4k7yV1mpm0mpoXV4h37vw A/1fnfUgmrA++pxsinYvSw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0k2b9fx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:50 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFBQQPT022877; Fri, 15 Nov 2024 12:38:49 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2177.outbound.protection.outlook.com [104.47.59.177]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42vuw2mq3t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:38:49 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=D3I9u4HA281oswvX/PdmZYHAHKKTjqzKNFizhRgDBQRA8xooPfMC2FejuKsLs9tjmpT2IxXdBPUGJZY+nK6vOFqgc9C5z6WeToRnjnCeRRmGhiAqHkDYpCrvle/98Pp0KfqZr7wyBIWYwgkmcHCuYjH3uBG3ulsHh/T5ustqfQ9GSFg1orpU2lgMKbdAo4RF2xUwoYPPGO9K0OIg1R8y2gee9/DQGd/p0qmQWP8cxkafW8XSoM0vBxthR9wSjqyJKaDBXRpK5GOMKUQdP0rxTJc7XiTCDW1C31B2iPCwoOt5inQGAhjHc86vfly7ib5Dfm9nyZ2nDkCsB5fZN6+RoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wSSFsDJKNEnL2Ec93aXMbwQXwjo0SGvX0HMoMi4GdeI=; b=LMhnpdTmUyZkCJrofxGZaPCC2Vu2oLMizYmUeIzCW1Z3zBsBf3PloP++NiKYqTlhJA8oSGrL6HuR4EcIeuHrCzVlZKp6NiQ3VU70zLy2hIQVyb8Brij+rtsYyWUH4oSwTEnVDXLLQ3YcU4hEU43icm9IVlV45JIMeniGU0N8Q8ylwMxUf93tZn3aqJdIRzAhtFZPOJhNVIXKb3rHFF828OKTqzzsYqiZinm/Cvnnj34mJNpggB28K7bSow7nw8TTc6mEyMSjXJkpjr/gRxlizyqTZIeFnIC7cIdWa+LXoK1drN6gymG/rOjB5z3exTS9Fegp4n7X3AcOInAyIV78eQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wSSFsDJKNEnL2Ec93aXMbwQXwjo0SGvX0HMoMi4GdeI=; b=HICE8gzkLcl71zT+fEiEuL4FbBqoowOyaLUiPyG5iAueKOErowPqVSGGsB072mHZak9tMbl8KNPUgAjoI6d615os6VGOfWMGKu62xoSq2yj1/f/voShDFe8okC0J3qZ4CHOQP4x3gtnVnaw9MUPtIIAENpAu16RtU6FL3YGjdYs= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:38:46 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:38:46 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.15.y 4/4] mm: resolve faulty mmap_region() error path behaviour Date: Fri, 15 Nov 2024 12:38:16 +0000 Message-ID: <03d88781b023104684f07a920a3e3b205461e419.1731667436.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P265CA0183.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:311::6) 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_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: f8fa5182-2064-4ac1-c58d-08dd0572727a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?lFQTBR+AWM5L8MbdO77BT1RAuyq6E0TAe/eOr7XYtA2sO5GYVbMBblAI0UbC?= =?us-ascii?Q?pOtFDnfpljZOTBU1B0Sxo31Yzih9er2GmA2Z7+sbFNnPoJwZMMiiv6VSqfIe?= =?us-ascii?Q?udKh4LrbsjjpbOGNx5ym04JQp5qAc66jJHhRa1OIgakCoBbIWbyo/A295mAI?= =?us-ascii?Q?RjrY3RgyGOwBEnw5eOzgGH7KIZFKQFjoXJ8nP4bT5QjNktO/af5CpJBMU78E?= =?us-ascii?Q?DdiUTmZYza9Ker6rwZFePuC3NMmq/Ipd1jASFP8zjgs0NQSnKznnK+eNcxhE?= =?us-ascii?Q?mhfrMgdLiQMm275g2RCDk6ohanTdhj69XDRIy9OLaQmOpq1nF8arxyZwMeKa?= =?us-ascii?Q?pvjbt1TTqn/H98fWGgmScIvT+LwDHolrbox+q8j6LdjT3UIe1E6qvkmDqSdP?= =?us-ascii?Q?aiA52IsOYWMvDtPAcgKAf38Rt+Jen6oeVM+OoKinQBEw5+xtZIj1WZExQWoR?= =?us-ascii?Q?5So5858ZvOOfLbeuzDIBcfHbb1IxPYduEJp0g3v9nAmK1NxTKRqTbK4oykKE?= =?us-ascii?Q?rpWhmii237WiymWlAVibiJCE8b3EPUsOl+HVsvhxXVEp/eLHFH9kRAshsrlR?= =?us-ascii?Q?Yv/G6anOlRooIxxiaxaFAB6re+rfwOOr6K8psPsH1jn6oIycWE6vB9eFSisk?= =?us-ascii?Q?HyQYReg4QLGW3986gJmgxvTghMi6LGT0iV3BfAgh66XUKZwqrARCn2vcRIGX?= =?us-ascii?Q?pcA5xTnTa1fGel0TshaJ9DTruOsrNlMg1gwRFYXTWlmJTPFz/XMMBKGkU16I?= =?us-ascii?Q?sLc5C2AUGrtPO4RCPt+04AevmVKx+Sp0UZWr1GCAQJseTI+XJSHB8x+RfUGu?= =?us-ascii?Q?Ysf3Zhe3NTsDZZYf3BA+uzzoo/o/EZLgA+UkEukNpvK/FwDv9irfv2ngwiMF?= =?us-ascii?Q?xPPFVv6l7Jhm6aQ+NhrmwkCpS9SLTig9H7j5X4JzsqalJJwFmYRq9RG0KwWF?= =?us-ascii?Q?W8BXK/FyDnql6DgP/0HgAfYrsSchczIslcgagw8tIfrky4Su32E2KjhdjrnD?= =?us-ascii?Q?bJs5Ou60xNxdUTtH+nI5oaDlXMZbAWXeZW4IaCVgboHGXg8JSycb6WgCnYbs?= =?us-ascii?Q?G2pZKYlKdvuIN9Qxf7uawmeR/A0ay2LRzeeqPAb8ymOyw6RqKg/d2ukJzaEM?= =?us-ascii?Q?994VJrCJLdY77JnHdl/Cg7xW+PztgyVQaWWWxCqu8DZjZO2pocwJ2doop8xD?= =?us-ascii?Q?+BqEiGykTL0pGliGu9iAo+61KjUxKXNUSQOKJZuiChX/fvNdHEd+/e5qJ+H3?= =?us-ascii?Q?s9VZ1uUb7DgkqbKHpC0iJxXYFu97Q/MK+i+pel3YnsrU97nimmVABFNzasOa?= =?us-ascii?Q?+CBVVTY/DTmV9R1vh6JAPg+v?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?J3uisKQgjNeybK7ds6dmgipOZznr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G2lBiZmxLqqf+wwXv236NuxcCzIK70a+Ymff/sGPUJ3yMgJ/N++Uu1liepo?= =?us-ascii?Q?iy0l8Eht4hkLVijrgqPRrmEfQAZ289ZEa/UtHTLRBTVAykpCfBzNTJUGRwEA?= =?us-ascii?Q?WBOd78qk8+n8pU4xhf9XoeI3TaScwLXNfkn0ZJGk94AWnebTCNMMIMmLmd0P?= =?us-ascii?Q?A6gqDPKI3Un5q/oF43pWA4KxK/nQqoTMI/4CsO3TXP24XTnsKRFm3JEQtVly?= =?us-ascii?Q?dDoP0/6Ym+ss68Vhjtuzcf449IXi8I5ZNJwrp9JkUub/Piq7Si7trcVAee8p?= =?us-ascii?Q?y2f2kFvoJulf/tLczMIA2hufSnOyk2HjGZjuY5fVXp467XASs+z3jVX5IBJn?= =?us-ascii?Q?CJcR0I93LbhbbA5Ur6cc96w+5Sj5DjusxOTHoKVc0dOpzB78hC/x2gI9YLdh?= =?us-ascii?Q?E0rnlfxa8+4uDm/1cvZgSekP5JzVS2e1umlbxsQIO7hmED3wnLZAftpbUWvu?= =?us-ascii?Q?O34mGiTbZJYkmgoW9bmEW9SLY6q/gjLuhD2UBBkiA4f8uC/MsrHacZEPGG9s?= =?us-ascii?Q?4FYcg5ZbRflaQOllBU+edDWDhmjX7VaUlb01WGzUSCru+qNgLkwpw0KCujJL?= =?us-ascii?Q?2ArRXgURRxVIor3Bku/2FjIlPNCdouQ+22pCYTzSYAkBiZ2sFO5Sqi96wR16?= =?us-ascii?Q?zv9UJJyH7nfIJde6O/S/xUlrDOaRukI4CrkF/KxzEuToY6J7On0GpK5+qE16?= =?us-ascii?Q?wA=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: W4UkWlZ0k57EQha/BFUsp8l8s9ResBKQi5KTXK52w5B1XaPxvJhIHHBVvRSmzpYzQSRUQSm6htMKXgz53cPwQXZuqhu+6TDqPfzpnKKkzJOveIIMAf/wivEcDh2Mo3EzpGsbIlPlrMLnae0vmx9MOwCi71DVyokG3fGP6hbMHjjkF4KaG+adwmtpdG99wZFR4pGJV3tie55GUrDUCjWuXpXY9Uh2T3m4iRxtFblpnVrOdkYLIoqh1+B3+HG8tQnrqCNeOJanO2oQKnrwXn+nmWjXzpDMoUQKqz46wDElEQicja+eeozCcux5CQqvf5pI+4bacTjPWvv23NRSRum40LBj64RP4H7Wdt+Is22Dr1gXSPSPCxbPVrquKd3JA2d+LpvjVCmAhYktTJ5gBaJS5hdmk1jNTot1l46ldBWYEeeESpX2XVVc0zMlqNnAaIIU0Ip6sno+v+T88QxMve/s8g4JARzmllhjMHcy3WeqOQk0p44kzcFm4RuWQhSbF0Y6qnspy859o4Du+lz+xK1N1O8HRed+SH5TBH9l/DMRTwRKgSbYbexiVySiwHjLfPJ0yB6zFvDXxjJ7bCdHValx7lQVpR7oWq4kHVO9svNATM4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f8fa5182-2064-4ac1-c58d-08dd0572727a X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:38:46.3757 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: vBxiwkzia9itGhMMAXhDwnUT4S8YP+kgwA/m4+xq8tLjaF8zba6dtJwefKqPpgQKGe7o8SLpGdrmWSuhB7wTT8ojaV5NCqQebzPAnS9hOMs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: w87bDAH_fk5vjFbiXSmyj2LX9qoRCoGr X-Proofpoint-GUID: w87bDAH_fk5vjFbiXSmyj2LX9qoRCoGr 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 | 73 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index a766b1c1af32..f8a2f15fc5a2 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1716,7 +1716,7 @@ static inline int accountable_mapping(struct file *fi= le, vm_flags_t vm_flags) return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) =3D=3D VM_WRITE; } =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) { @@ -1780,16 +1780,10 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, 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; - } - 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; =20 /* Can addr have changed?? * @@ -1800,6 +1794,14 @@ unsigned long mmap_region(struct file *file, unsigne= d long addr, */ WARN_ON_ONCE(addr !=3D vma->vm_start); =20 + /* + * Drivers should not permit writability when previously it was + * disallowed. + */ + VM_WARN_ON_ONCE(vm_flags !=3D vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + addr =3D vma->vm_start; =20 /* If vm_flags changed after mmap_file(), we should try merge vma again @@ -1818,7 +1820,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma =3D merge; /* Update vm_flags to pick up the change. */ vm_flags =3D vma->vm_flags; - goto unmap_writable; + goto file_expanded; } } =20 @@ -1831,20 +1833,13 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, 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 - goto free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif =20 vma_link(mm, vma, prev, rb_link, rb_parent); - /* Once vma denies write, undo our temporary denial count */ -unmap_writable: - if (file && vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); +file_expanded: file =3D vma->vm_file; out: perf_event_mmap(vma); @@ -1875,16 +1870,12 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, =20 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, vma, prev, vma->vm_start, vma->vm_end); - if (vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); free_vma: vm_area_free(vma); unacct_error: @@ -2907,6 +2898,36 @@ int do_munmap(struct mm_struct *mm, unsigned long st= art, size_t len, return __do_munmap(mm, start, len, uf, false); } =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