From nobody Tue Nov 26 00:29:37 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 D64941CC148 for ; Tue, 22 Oct 2024 20:41:18 +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=1729629680; cv=fail; b=anV9/+33JPPZlcqgmoxzl3K+/DVlQPQx2y6O5mwQp1ttFt2RQ2HEGsXzf5AAlKWoTV9HCeR2Tb4DZDF78m3O+rCurBSyBeDtdvlPSFxVjQuTjJ+GglTlGGa5JDVpiz36IbNmsLOIDGKrLvECor7RnNmbWKu9yIjM+HcRcKm6VEE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629680; c=relaxed/simple; bh=bU945MpOgwlJ5cbznLicK1kzPPsy5f/H4svguhNDxOA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=cmRs41OX3qKx5utIQovRjWekwHx71e3CTBz9c4iHeQUjvqC+mq43b3+0+ZjyFKAyiXFet4+Dibd5rvbBOcY3KVa3YJ+FNCyIR6XoB2Si4U8B+W9d05IfDyeSiexcgp7uscVJ4tY4GfhC9N5jxwvpGKSWZxzAkiDriNi2dUIirVg= 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=Fm0cjR99; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=aGJLOcpg; 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="Fm0cjR99"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="aGJLOcpg" 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 49MK3Wt4025707; Tue, 22 Oct 2024 20:41:09 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=iYFdRZtxK3YWDV2kNLEF9wGq/1fKTAuij1RdPiYsVFg=; b= Fm0cjR99ocgVdxgL7Ky62AiHNGhtHszKgm6g/+O4/Y6AoVAJo/yMbTPjhB1BM9XJ mU9yvg+jh1LWd7iNd93Ti22rFi6hPrpLBbqi7/2+lpp3IBrPffCi3omRQsmCTNnQ Cl8ucla6GyvPaPgxlWGnMfkM/SYAad7P4qLUyL6k1xrn7JqlyWd0dTrV7wRdtK4n HoiQzawBeFOVVKwbPqNNMqp7vMWr/QC7o8ScfNVzCZYF/C3Ap0BgjfV8O3l29S0F +LJXOIQuJJi98T0ZP6EluassC3LvAKsmBpUsrPPsB5zKevW5eBrzvU9geVm8DoK2 9tR1pUd3ayatb8iQBRt8jw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c57qendh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:09 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49MJnVOJ012557; Tue, 22 Oct 2024 20:41:08 GMT Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam04lp2041.outbound.protection.outlook.com [104.47.73.41]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42c3780p3w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=fLFkM2c6jabIaGB9NvkgFBRD0d3iFEhkieHc8LttAf5OjAFbet7p9sMFufP6F+l/S7irILPkXLdk5rsOTCC77d6J7Dorj7eulPSEVpVz9ZpydWYFkp5gnP8j5BLIfjRtzIfjtOlJjrkXEfWJiY9ZkONiDf5pvub8ohLglfbA4lsoOohL819naTWizoz2i2ZRY4nVOF6zac6+pyJjHxqRJM39S6qGtFnjDXe82XTZcxP0Ihb8KDvOjb46F6UPKc5iPYoBWnOBYz8FsjKOKZ4pJy1q/LmoFakwDbKPhgqAwmqhlDa0xcalWOG8mt1ZNHVSQAKvi4OWpIs250nPxc4U1g== 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=iYFdRZtxK3YWDV2kNLEF9wGq/1fKTAuij1RdPiYsVFg=; b=Qnqa7rXaSeTFfEBGMjTSYj3HiJ1bpoUS0gFqGgHEkFv7tDvGi2R5S/5KO6xZNkTEr1FY2MFpitSqTZwkBIDAqJKNOb/PnTQtG/6/HBlwbrlkGqzt0hMKbOaWLHl3VNXUa9KiMwmNoPnYgOpHugKoYynV+f60CKvaNPkNtYXSUuDabcdn78TdImkoxMjaa8/QQBuPLcPYTH+bCVCuGl8hRztHKv2staS9Z9DK8RBXUlB4M5SM4Yf9/cl9EMJ8UrfcBvUzcqGLH0BGMRCEH7SOjI9fe8TLdzj5/+6+KoXBDsqkKn2Krtz5abW2XuKNm2Bo68bXGBNN81bEuXWhQGctyw== 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=iYFdRZtxK3YWDV2kNLEF9wGq/1fKTAuij1RdPiYsVFg=; b=aGJLOcpgTq4B2jVrDIdDtyIBsT9nEtQ7sz/lOEQNFtFM7OTxXIVKsFfumqj8sLRCpQkmqIs1os8qMeASdaQE7jzg0eecc8OvKZePA4y/ov2a80V8Nppd28aD4clVVTMHJ5MXN8j92TNcuhiSVt/jx0jSwk414QRSChB8IkRkVMs= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by PH7PR10MB5879.namprd10.prod.outlook.com (2603:10b6:510:130::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.16; Tue, 22 Oct 2024 20:41:06 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:06 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 1/8] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Tue, 22 Oct 2024 21:40:52 +0100 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0381.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18f::8) 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_|PH7PR10MB5879:EE_ X-MS-Office365-Filtering-Correlation-Id: bfb2990c-65ac-4234-bbf2-08dcf2d9da37 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|366016|10070799003; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?yqSyCZZK7ce9SaRWsdXgMFtZiY+ntTCawqaZ8k1AhI/LPkwAhoTsENN5ax4G?= =?us-ascii?Q?wbTGHbd0+gIRg2DViL9ORfkOZKIKqMbwXplQbWy4Epqb/T8XTRPK7swLH7RR?= =?us-ascii?Q?3Pez6DcmIkMBFgjWUT1clzGcRYhdOt9CxAyfDJq+OR6bQYQWPgGJMZ1WyCVN?= =?us-ascii?Q?3Pc+jipiQFPO5Alt+ViorTf58Pdym4F5BXmnM8VecjDAXaly2zHN3e71Bq5c?= =?us-ascii?Q?C7/PQFF1JHJSa19oBYGodbKurt3ED2XKrzvXjZU5jxPOPjoeGZeCpOsKLEHk?= =?us-ascii?Q?XSYl0cJ287E/7LL7nPWc3T8uXiKJMcFFa08kXTj5Yxq2/hO7/67jWQs+zpXS?= =?us-ascii?Q?TFvqnK3hxoxoL1MpjuWRZGeTyxk2kr+MSYQOHNe3kZSUA+gVbbUfZnxzs0Gx?= =?us-ascii?Q?bhHgMYz6Zp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EWs/QcHT?= =?us-ascii?Q?VokLrv/vJw+FOesiH3coiNcm?= 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)(376014)(366016)(10070799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?4EFOZLY82UQQUT5TcDfVvJErsCLAo+YkaRDaVebfMgcwE72YVFyCjzTeec/p?= =?us-ascii?Q?Z7odI4qCGNaQQ+33wtohHdi+ILMY/aZxYrdVdk+X3XOgXp8TMJbxIFWrRoCu?= =?us-ascii?Q?vOReAvieE5WXI7ZE0IXwmPGdpaSpAJuz9mSvc5OLb1KOfqIyANAr25LJKnf4?= =?us-ascii?Q?gUQhWhbY0X0semELLmYq6aIjfv9ryxJcF9ZbGH75lzukGitEe/0yc1UgT7Yw?= =?us-ascii?Q?QXG3fCesO7N5+j88iJPJzcglA/usVRO6oi8PI2/Sl6Uhp6M8vUWlvqXzWs8B?= =?us-ascii?Q?KwFM6Qq7zA8WWnLuWxP40VlVNh9bxYLJmK7AruqKJb1bOnrHMtY202GIPh4A?= =?us-ascii?Q?TgKqX48yIaRzb/Yo5e/bqBs5xmhlZGpIK2ksXbD1cxrqC0HR/2J2ERtbO659?= =?us-ascii?Q?ECU36QUbD1aATXEuba/aihzpcqzg5kyZ5mRfW73lTdVJrXNXLaMDVuD7GWW7?= =?us-ascii?Q?yHc+qMIqBmBPjChOOEBcjoQTr0BMmK9H1d5JaxWZ+b6GLRJbSNX4O/zZyrmf?= =?us-ascii?Q?5w1bY7+3erxhtpNc+efgo7YBCVkoYQgi8ZLQ27W8hp3J+Omj83CHi5KRrLHw?= =?us-ascii?Q?5Qzb3oi8pK2Yblx7ol05uMG7UX5r6WMMbzHu5nloT7LwScHJzfDJkRASPB9x?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e7IzGgfG5hJmgO?= =?us-ascii?Q?vt20pkBRQhFFVBtFUoLcK1+VumzNUoreGQXBDZhi+tARY8Uovf+x7O0hs/HK?= =?us-ascii?Q?ey3kaVixzirLNo0lbZuqm40JQ30vaTsKo392FccZiUiq5pUXDcWrFgFEJTMf?= =?us-ascii?Q?WDa4ww=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: OAN6fGUaUQWwmSC29JWzZxVjVStP+d3v2gctyn954aPTjI0zjBMFawxMzuoJRamCGeDaaV8gHNG+efligHCrNztbJh2LJ7vdhmkMs8ES0vRxBwn5j5sSsbJNjBiiKMr+AnPIdgTkmweQzrJE2n/VU3KTV4oaSt9J3fI1LKiWc7/vhuAMSxHNbuo86jMomOsnypv/hAq9yh3dqPg9R/COem0GMKW6Xgk4jBV2x8QZCYpPoDHPufriY34zilVI2RNgVZu55xNFBB2mSKF57oZizhz9qnYpGjdE9k4MnkcjrZh2BRpacFGJmjfagzvCXNiJXMt3rYMIEi0Ur3C6mSRpITnI1NEBi1BdphqwPtHrwGGbeJwpCvu66CKA9kOEXkArDJ7blvnBrTm2VOoUTfvrHLnIB/hyicnIpgSxdJBRkGzX3Upuqs9zpWNdKPWMcS+Zy16oePFkInJuncFu/i3biUo1vpb5YzFgd0RWQ2neIPivUo8mhY2uhblJqJLy7EuLVdbvLD1aztTHa+NTT14m3F5vKonZIhf3OIFekfhi/fhBrVuZ40ojCjG2xkVuDlURRAarrcqYQed5+UC5wJlZCogoxl0D1o7GR6QkVRONRa8= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: bfb2990c-65ac-4234-bbf2-08dcf2d9da37 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:06.4929 (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: UWrQkSa+ChaTgSVy0YwWTbU38CS8vXW6qQLoVfiv5chXrtSs1HDHG6oPpu1kBcXKo9bhNS2m0z9t2zKGOB448i4IVrlae3ta6IhmDpKOzW8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB5879 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 malwarescore=0 suspectscore=0 adultscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-ORIG-GUID: 3TfZiNXp4y8ylZEWf_jytYJC_KGEgrWo X-Proofpoint-GUID: 3TfZiNXp4y8ylZEWf_jytYJC_KGEgrWo Content-Type: text/plain; charset="utf-8" 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. Reported-by: Jann Horn Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Cc: stable Signed-off-by: Lorenzo Stoakes Reviewed-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka --- mm/internal.h | 27 +++++++++++++++++++++++++++ mm/mmap.c | 6 +++--- mm/nommu.c | 4 ++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 508f7802dd2b..af032e76dfd4 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -108,6 +108,33 @@ static inline void *folio_raw_mapping(const struct fol= io *folio) return (void *)(mapping & ~PAGE_MAPPING_FLAGS); } +/* + * 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. + */ +static inline int mmap_file(struct file *file, struct vm_area_struct *vma) +{ + 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 &vma_dummy_vm_ops; + + return err; +} + #ifdef CONFIG_MMU /* Flags for folio_pte_batch(). */ diff --git a/mm/mmap.c b/mm/mmap.c index 1ba0878bbc30..10f4ccaf491b 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1425,7 +1425,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, /* * clear PTEs while the vma is still in the tree so that rmap * cannot race with the freeing later in the truncate scenario. - * This is also needed for call_mmap(), which is why vm_ops + * This is also needed for mmap_file(), which is why vm_ops * close function is called. */ vms_clean_up_area(&vms, &mas_detach); @@ -1450,7 +1450,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, if (file) { 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; @@ -1473,7 +1473,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma_iter_config(&vmi, addr, end); /* - * 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 && vmg.prev)) { diff --git a/mm/nommu.c b/mm/nommu.c index 385b0c15add8..f9ccc02458ec 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -885,7 +885,7 @@ static int do_mmap_shared_file(struct vm_area_struct *v= ma) { int ret; - 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; @@ -918,7 +918,7 @@ static int do_mmap_private(struct vm_area_struct *vma, * happy. */ if (capabilities & NOMMU_MAP_DIRECT) { - ret =3D call_mmap(vma->vm_file, vma); + ret =3D mmap_file(vma->vm_file, vma); /* shouldn't return success if we're not sharing */ if (WARN_ON_ONCE(!is_nommu_shared_mapping(vma->vm_flags))) ret =3D -ENOSYS; -- 2.47.0 From nobody Tue Nov 26 00:29:37 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 809301CCB36 for ; Tue, 22 Oct 2024 20:41:25 +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=1729629687; cv=fail; b=X4ew1SL/f+LptPWFopGCFxW678PkSQkzLz7AxBIgXM7+5RA0AClRMdytBBRCzbKsYggaZvlfpN9s9ad/oprfRzDeUSYuB+nXlc/k02jl6OOtMtSAJd6K8bAQrM3KzlmFMljSDPPbZ6NX+ZbC81VTqARcuyQ6BO7aEwcDHsqwj9g= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629687; c=relaxed/simple; bh=Ty3qdli2iJOzZBFUaHrtKxy9P1Tb22fCbh9E03n9xLs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=aV1DatH6aSb5WwljJVMvllbXusIVkSivDyAbgfJlTYnNntonRjRN1X9m/2QJPy4wJdzTQX9d3bwlKTDRRqBfr7OsEGlT8K8Tzap62GO7bLqCcknBZVXcVeuQ2bxp3v2SbTh+9w9b7/MNUjswscp7Lm6e7gXb6dMkuERKq3vT798= 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=BZrx4mew; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=wTJ/5LRb; 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="BZrx4mew"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="wTJ/5LRb" 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 49MK3YdY013268; Tue, 22 Oct 2024 20:41:16 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=PjHEtf0t6dnYaFIMePzzq4ewJmFLT/0zq4zRJVRV9/w=; b= BZrx4mewpVIUcf7/MShtRvUhM6W6speBCmot8W2Rk7zoJh2UGPMnkW8N4H57pe1n PnYWVoAVOyDgcM+R5SQpTeWqSSgFAjspJe7Bffd1deJrxDdKt1xZ/8BEufwTlJMe S0zlro2jpBk6RinPjMo/AiXYvK6cCx0yfHUQU6hfqtBHsx5O7bU61M+LHKzut4PJ DV9C1SMQo/eAQnvKQyeSKudNXl5qkX4psXY9xiv+CWeCHUlIAYl6rkJdTU7wkFy8 hzdz5v8qY+2kTNxBF6daRmixLlDY4UaQlUMecRfpdMZvCG5Pn3y+omvCBBFodhDl yGBHTl94lvzC8s7FB4JYeA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42cqv3dsuv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:16 +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 49MJWbiV019683; Tue, 22 Oct 2024 20:41:15 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2172.outbound.protection.outlook.com [104.47.73.172]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42c37ef3fn-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ch4x8gcGhoeXmO00w/f4rd1FJi+f5FqTwJX8shzylK1q7QlwYiS/6rskbynSX5h44MNCbhd0X6GehO0DSxcEZOvauBDR0rg7O+CSWd1SHHeM5iIaw3twg/rn9qJGGsbHwYeKjmMY34yV9krHoMm1d+yFKObg9CAJx5xXgbPbygrF2oD65UIK7I6Xf3lAiXJmJE2KZnpd10gG09iksKWsk8bjJMq7ArotjSPxIJ9f8X9PWZizjYUW7EEWVEWAFqEKghLp8LgmQwk59Nrcx9somUlym3eT8Q0VqjykWYykDsZcVx2EPDgoPvv99ZLRpK4XJVVntWgjHY+5xKByXUnnXg== 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=PjHEtf0t6dnYaFIMePzzq4ewJmFLT/0zq4zRJVRV9/w=; b=EV5p5tkKOojlQQR/XsK7XE7TpZ91vZaCT/NSP11Xktu4ElwG00gPXE7VXfoJFIwK/50DhNAVBweTJfL/rS4ywziHtHQUVn2qCyH8C0WF8DQwW16QCrEYx3+KLDOlCdv3BxRs4IGwuz0hw/OE2IzxwjtyL2gZGC2m5QCHoRcxs4zQ+oS1HMdasz4yQsFRuy46A1f5TTxs1RyfVrXUCMfOu+Trjf8RCnY+/U6pYhZw1uW1IqwoG+e2VjXroV9bZynLnFKRzjDionTFvYHqC7uQU5me21G8zbO6sFh0xYj7U2BQCX87Vv25J+Rg71vXWHA8dShSsuSx5ZTGB1FzyfnV5g== 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=PjHEtf0t6dnYaFIMePzzq4ewJmFLT/0zq4zRJVRV9/w=; b=wTJ/5LRb+dP/Y2llETdj8rZBm77kKWWRg4Dw8bmBbFMmaRPkyw+uBVDQbj1Knd6HD9J3snP5rPOflqdfillGPoM7yVLtWoG+I/VKUKgLgq344kGwgI8FcD1xC3FmXDE1R3xKDd2c9Mwqgh/gvOl3zopxFUF2EAbbOYmNsuNp8Fs= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by PH7PR10MB5879.namprd10.prod.outlook.com (2603:10b6:510:130::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.16; Tue, 22 Oct 2024 20:41:12 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:12 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 2/8] mm: unconditionally close VMAs on error Date: Tue, 22 Oct 2024 21:40:53 +0100 Message-ID: <9a84bad9fdebbdb0adca2b5b43ed63afceb5bacc.1729628198.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0416.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18b::7) 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_|PH7PR10MB5879:EE_ X-MS-Office365-Filtering-Correlation-Id: b9f0d0b3-f308-42f4-5329-08dcf2d9dde2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|366016|10070799003; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?eGQrU38wwzwvye5J79bqpxCOdaQFFaRBEdscKnT+DtSl5jrczs7IJq2i9CIi?= =?us-ascii?Q?YVaGeKpEjrh08NqL+g2ZvtWkdg/qyfY8XPEgxw83d49hhnC3OUTvaubNoaPH?= =?us-ascii?Q?5o0beGzPh0NhKg61MmY0DOiXOC9qC7cKeXeNzS3Vvry34mUx33bbH0k7JSRv?= =?us-ascii?Q?fpidlufZSsacTgDy5/YyHGSLy4PEYWezDD92yrbrxvpJCFM8nCf7b4wuysW4?= =?us-ascii?Q?ARjsm/U3Dvqf1L9kPBJaayXhjJVG3UFGJEYZqYh1MJebc/Y57OfXc/9IuDRt?= =?us-ascii?Q?mDO6l3C0QiA1ETTsefSFitCKbGsgRhZm8wxoawiQxJaBEi3+dkcBlLE7S+EI?= =?us-ascii?Q?ipyJJE3OCpNZH52/FxNTNUYceqE793JDqLumRPVfjjBq0/daK7PGVvXpHlCM?= =?us-ascii?Q?y7tgK043e2M7QKE728FtV9uZqsVRi6o53hKtsFR1GxYdssexkbpaYwmy1e5H?= =?us-ascii?Q?32Z+sJWxHACER0+6JvhyQlm2nqucpVUnforkoRV3I8XsXy25iMNBWeWIhnxD?= =?us-ascii?Q?VlpAZ9zpslWA1L5ddwSf3RVQVaH1dBl+8uQJGHDoncVly03UtzoKZvOaM8sU?= =?us-ascii?Q?h07/wz7J6ugnRDMyY6Jd6kj0WEuBkpg+U2vEU4WEeY1BWXCj3nxFzTKbgPC0?= =?us-ascii?Q?D89nbA3Wu3it5C8+xeElzyVOZRr2aA10GHMyMz2nk2LZkJy9nhGeeVrX+C7T?= =?us-ascii?Q?4sb8L3TCwWH6nfNEXjqCAYU3iSCe3ssuq949iy6i78cHpJf2S3fwaWyyz5Vd?= =?us-ascii?Q?YrBps5iaDxgSFZ0aAwPjfh6x8leLVOM6HfBpaYx9HE0IGaPOZ1TvXD3smLtE?= =?us-ascii?Q?+DchjBIgP5KGnC/vsyBTWuXWaFyPBOcRD1DWrBBQ3ZrAVmJEUa5o0uxsWU9I?= =?us-ascii?Q?crfaEPdGgfOEyH3BPxq/Sv0w3bBbbAyL5tFOys38uQHRfbqXDid6YwnAlRDI?= =?us-ascii?Q?dE3cTyg7LPjRMLQP65dQJVH8Rui5d2tlvFEN+GzpT1A6WEsejyxT1fvJWmOm?= =?us-ascii?Q?wsfkjZoi3GN801v4NscAfxqiAost6+tvOBg8WgxuLpCkast1GrOxCTKykQFK?= =?us-ascii?Q?mGXZjojsw4HGkStSMWbnIE2wLkJ0yv7I1/eB87CYuqWNz/brbh+h8lBdx8Op?= =?us-ascii?Q?GxdHSZufaYMBf7MBOGrsNEVpDf+Y0MNnbtXsMgmgiR158m9+8vgLLsmVX7R2?= =?us-ascii?Q?bhH08d2QPySfZxGn4SD2OMKJnAKgtjWzPoJ/dxYUmbvO9V3XCEVK5nEEE7kH?= =?us-ascii?Q?uxyIu7kUtT0fhHmY+mUJ6crex4VdV8bdNjW4U0zjeL9KugnYENILz2cAzo1M?= =?us-ascii?Q?wEFQTa1FuTQ52SCI9FhXGXO8?= 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)(376014)(366016)(10070799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?wZhTIblHJBDgKuzUokAtWHChDA3BPO0gbqyYX3P/wEpUFkaDkWSiAPLdoCqm?= =?us-ascii?Q?FhyUIwq8M7Ll1QF16GWdhsg4ZmssJxFHB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ccn1ML0OwNCe6KZ+vRL3HeU1W+7jbbQF0APMn0HVQhH93y6u98lmjPgRX?= =?us-ascii?Q?2F38YUC4BpZQ0gBlp5bYZxYJHWOy59LXupLeIUcJFOJ17xhEqgSgdiyeH+7o?= =?us-ascii?Q?SsBw49J175uMBaI51m/gEJ/Ueh6ob3ecBohoy8TLPZX1SIvQEmObJlje1XbW?= =?us-ascii?Q?34zQAjfGgOlfhreH5BWnzQXqecraXsLa+NzgeqNji14HW1nqTYGdfSEV8mrr?= =?us-ascii?Q?RK91eUnTG4pw+LVEaJC4bdKcv+L9igVTmSYl67w317y91OQnc1+8O5LgQHUH?= =?us-ascii?Q?Lz6/b96rbU68PwZ7Zf349ZoJADuFhp1rEO8UQ5Ozs6rWOjGBHPBP+EElcEFp?= =?us-ascii?Q?hG41hOQOKxiw7LFS3hz+7MQILy5I80UuSS99Wm5LO1QvPL219T3PtX/HJK53?= =?us-ascii?Q?qsE3OSn+MdXRlfv6AK++z3AH5NNtmu7xiPW2Hj9mwkBFLExZEZO++l2Q414g?= =?us-ascii?Q?nqP0k7L/4+EufkV2uP06QxODwjNTjH80X5Y1TjeToIOmXn7B1ydq8tz+BJ2N?= =?us-ascii?Q?ptlfde4g50wVS3BWlpjw64WQO+IyBxvVVzYG+BmPMSm11anwQe4Xh9s4Oos7?= =?us-ascii?Q?KIf6Fw2ZLgK52ibEX+fOBdlNZuyZvCOjkeTJus9jKdOZmfd/RA8jHfJga5WF?= =?us-ascii?Q?Cv+RTmYTBA4V1chemUqUOzfSOl8H9L2b19UzKzQlTbNGfl4Ofar6CysjHRHU?= =?us-ascii?Q?cUdQhg=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: CB+/3eOtNrnw3NeH5cO3Y9t2vNpwc25OegDmla78hYOHI1MVtLRMk2TUnEsYamzRk/4BSlPDd07cEaJuln9Mxz9ifrq49y/fy4v8Xm4dhRZ70m3DiCpsPiRjTujriTaFckm7LkRvxc5fxdl+QqZuP6LQlOkZUkuSHP1Pv9kAqSG8uBYlN0MIOlR35bcKq773ncZZb1sH5UfCc6kA5IokeJoHFYZsi63PwIswdgaAEJMTQ9GfRUSXJsDIIKbpzUtrU0hPVPebqaiGcgZtggpDUGM536hY8ozZ5fgeSicNfcA+1LW2hmy7FY3+4+4dKdn2dxP2B767EARem5rUun/kdHALam65pRmhfuTypIsaTJCwvvvLVeJ+1KazO0HOW7PZH3o+J8I2uSr+Fo32TruXo7Qpubzp0oKZ0LY/Gqo6Bjjym0kIfTwU3uMBonQUUgh3R0L1socXisWlWfL3vkPAtFNkguJ6TtGPQef9w4yttKfI+AJliaLOZ4QLbs53GzRbBneSVgfbWeH866XaB1WhBWHddmEx37f9oxxcP/BB+/cWlxoaYl6AQ+sfy9kBN5GQJhev6ydfOTjpiRlkkK7c5IyVZMO7jy8Pvd8unW8OHsk= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b9f0d0b3-f308-42f4-5329-08dcf2d9dde2 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:12.6586 (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: KCx1MzieMdI2YlRHT4ddYf438j+3cI83iFpPEuKWPH2sib+DhX+4gvpGseuDq1g81XevfjzTKXgQIrWwzYyF7kSeH4NoRZMnTlY+dKQcTIA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR10MB5879 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 mlxlogscore=999 spamscore=0 malwarescore=0 mlxscore=0 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-ORIG-GUID: W2EFi4ZGfqTeh4WURatfCC1aiD82zXZK X-Proofpoint-GUID: W2EFi4ZGfqTeh4WURatfCC1aiD82zXZK Content-Type: text/plain; charset="utf-8" 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. Reported-by: Jann Horn Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Cc: stable Signed-off-by: Lorenzo Stoakes Reviewed-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka --- mm/internal.h | 17 +++++++++++++++++ mm/mmap.c | 5 ++--- mm/nommu.c | 3 +-- mm/vma.c | 14 +++++--------- mm/vma.h | 4 +--- 5 files changed, 26 insertions(+), 17 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index af032e76dfd4..3a45cc592fd0 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -135,6 +135,23 @@ static inline int mmap_file(struct file *file, struct = vm_area_struct *vma) return err; } +/* + * Unconditionally close the VMA if it has a close hook and prevent hooks = from + * being invoked after close. VMA hooks are mutated. + */ +static inline void vma_close(struct vm_area_struct *vma) +{ + 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 &vma_dummy_vm_ops; + } +} + #ifdef CONFIG_MMU /* Flags for folio_pte_batch(). */ diff --git a/mm/mmap.c b/mm/mmap.c index 10f4ccaf491b..d55c58e99a54 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1576,8 +1576,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, return addr; close_and_free_vma: - if (file && !vms.closed_vm_ops && vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (file || vma->vm_file) { unmap_and_free_vma: @@ -1937,7 +1936,7 @@ void exit_mmap(struct mm_struct *mm) do { if (vma->vm_flags & VM_ACCOUNT) nr_accounted +=3D vma_pages(vma); - remove_vma(vma, /* unreachable =3D */ true, /* closed =3D */ false); + remove_vma(vma, /* unreachable =3D */ true); count++; cond_resched(); vma =3D vma_next(&vmi); diff --git a/mm/nommu.c b/mm/nommu.c index f9ccc02458ec..635d028d647b 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -589,8 +589,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/vma.c b/mm/vma.c index 3c5a80876725..bb7cfa2dc282 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -323,11 +323,10 @@ static bool can_vma_merge_right(struct vma_merge_stru= ct *vmg, /* * Close a vm structure and free it. */ -void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed) +void remove_vma(struct vm_area_struct *vma, bool unreachable) { might_sleep(); - if (!closed && 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)); @@ -1115,9 +1114,7 @@ void vms_clean_up_area(struct vma_munmap_struct *vms, vms_clear_ptes(vms, mas_detach, true); mas_set(mas_detach, 0); mas_for_each(mas_detach, vma, ULONG_MAX) - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); - vms->closed_vm_ops =3D true; + vma_close(vma); } /* @@ -1160,7 +1157,7 @@ void vms_complete_munmap_vmas(struct vma_munmap_struc= t *vms, /* Remove and clean up vmas */ mas_set(mas_detach, 0); mas_for_each(mas_detach, vma, ULONG_MAX) - remove_vma(vma, /* =3D */ false, vms->closed_vm_ops); + remove_vma(vma, /* unreachable =3D */ false); vm_unacct_memory(vms->nr_accounted); validate_mm(mm); @@ -1684,8 +1681,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct= **vmap, return new_vma; out_vma_link: - if (new_vma->vm_ops && new_vma->vm_ops->close) - new_vma->vm_ops->close(new_vma); + vma_close(new_vma); if (new_vma->vm_file) fput(new_vma->vm_file); diff --git a/mm/vma.h b/mm/vma.h index 55457cb68200..75558b5e9c8c 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -42,7 +42,6 @@ struct vma_munmap_struct { int vma_count; /* Number of vmas that will be removed */ bool unlock; /* Unlock after the munmap */ bool clear_ptes; /* If there are outstanding PTE to be cle= ared */ - bool closed_vm_ops; /* call_mmap() was encountered, so vmas may be close= d */ /* 1 byte hole */ unsigned long nr_pages; /* Number of pages being removed */ unsigned long locked_vm; /* Number of locked pages */ @@ -198,7 +197,6 @@ static inline void init_vma_munmap(struct vma_munmap_st= ruct *vms, vms->unmap_start =3D FIRST_USER_ADDRESS; vms->unmap_end =3D USER_PGTABLES_CEILING; vms->clear_ptes =3D false; - vms->closed_vm_ops =3D false; } #endif @@ -269,7 +267,7 @@ int do_vmi_munmap(struct vma_iterator *vmi, struct mm_s= truct *mm, unsigned long start, size_t len, struct list_head *uf, bool unlock); -void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed); +void remove_vma(struct vm_area_struct *vma, bool unreachable); void unmap_region(struct ma_state *mas, struct vm_area_struct *vma, struct vm_area_struct *prev, struct vm_area_struct *next); -- 2.47.0 From nobody Tue Nov 26 00:29:37 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 1929D1CCEC4 for ; Tue, 22 Oct 2024 20:41:28 +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=1729629690; cv=fail; b=GJgH9/+Wpg1a/9tH2957BlmXtqVwyu8iLwaW3n/a3sjW/SQbi0OE32Mo2enN2BXpXvvavk6J6zGU1kpA75MrKOuSCxGtcPEFaXjAd5tuTviRBJYX6R7pYO4Fzvs8Z+D/CA+SjPaksVpRfMuDt18HfyWKcL4KK5AsdBqX7OmDJwc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629690; c=relaxed/simple; bh=cUmctGVR8b1UxHfrytqUMJGeG8de3oe8uKtrX8NZdBc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=EP4EspQkD4NbLXXJ20RvurfZkoM+RxqOAV8pZxwfCdEfSmlMJ4slYGDIOF2vpkF5f14mvKV4aDy92cqC/+zhI/tacQAtrST1ZBcy0kDxfDyY9TvSAjj1OQNiOVYBiPScfqXDBuWOfIZ7w/6MHD6d0K4Y22bDYmJLhJr9//RTqQE= 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=aB9G1H7O; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=u7QE0iUf; 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="aB9G1H7O"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="u7QE0iUf" 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 49MK3XG7025798; Tue, 22 Oct 2024 20:41:20 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=e34AEHAMhHEQASKV1R/2I3fLJ9/l6ehQMuhQiU5tCX8=; b= aB9G1H7OBEbDW8H01Vv8nphGJm1lsMifIVg9ELbFFvjeJ81a8wA4FEdx5jsnwgk1 PN2B1zSr5WUt4MBaMfHBvyfMbZEESNEbWofgra2aiVmBkFVMWbQOXZjygrewXmKf hK7pG53idv6m4aXl9lr+5mlgeUb1NU5FdzLLBCk+RGumJkbC4A3fvHpyAGMpMvrV aW/ZW5ZYtPA9Q2upEntenUirFdss7DxGSOiKgNw1pym+24fxkf4cHdfmshw6KEAu cEJxLmWT8ttIV0Asj/23H9VUV/vOn+18Ftps5cH/VdknCkmYJuTtgtKWc0tFn7z+ A01Yc8jPgutIWOAZlZFRYw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c57qendq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:19 +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 49MIoKGl022660; Tue, 22 Oct 2024 20:41:19 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2175.outbound.protection.outlook.com [104.47.56.175]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42c8ew34a2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:19 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=F4WPXWYz03hWe3vlZnQ65HCuHk9NWxQ8CTaH+pO9j3n1C0HKu++MpXV9qshcDWvaWwvimB+lq5Xim/0ZQ7LtSZlNbNdheF9b80yBEeQ+TtDxHU+Lq4/RSMf0a3gceMslcbXdlnJ9YHp5DiB12ZamrLNzHrc7ogfnshRaijwSYllb/Wp8gt5fY/fio1L9CCRjkT/bHYqL6nz+lAEuVH3DzwICZESWKq0ku3H+ajDou7cTlPBgni6aZut3kx+1RHzud9cvKG5bTDMRQt6ChGvauRMgCcmt7t9B10aAHcGhmboslE5WiQV+U8LJOcVkJ48KSg8QFQG2VP010F3g2JZS+g== 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=e34AEHAMhHEQASKV1R/2I3fLJ9/l6ehQMuhQiU5tCX8=; b=nutyY0oz+Qqec4+zZcn1sOz/G7tJyONM4fRgPGw2ye34LMIKHFFn+5ok5m4C2392hZ9Q0hj1F09QHxZYqv8c7r1sqVaMGAliWGRpf1IRp+TtQua0pKl0OjvQuWJJKTlx1xpMfwRIjxk6oHVCMCOyzwSR8GrXbnJ28YfCYJGKqpkUT4cM4UT1I4c6O9+bqPdMD7q1GHq84tZj02AcyhrrWwLXhlVw+LGzjbo/OG2dpT0AoZlma03hoGfqKn2belfefywVmcmrgWW7NGhx2040tUL9+TpIbkqISgg5g5rM0Z6GP1imZjuXTWPTEoZJrRL5Pz+peLdbCrjaS/N7R3feig== 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=e34AEHAMhHEQASKV1R/2I3fLJ9/l6ehQMuhQiU5tCX8=; b=u7QE0iUf4/7EINHeCJQSJvz+rMV4tPGFkFZa8coisXNVjEV2iiCyf6Mt9wY3e3nmCKc9ypXOaNtd8GQBACrOjr+UFHZnMGSkA5CfKVXuoSk0ZF0pY0bx4dz72TCxc/9fNk6A+jxkj+TfZoCVBmp1DuY2uQ2QcRDH/m93+dPACrk= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH0PR10MB7437.namprd10.prod.outlook.com (2603:10b6:610:193::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 20:41:16 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:16 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 3/8] mm: refactor map_deny_write_exec() Date: Tue, 22 Oct 2024 21:40:54 +0100 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO6P123CA0042.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:2fe::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_|CH0PR10MB7437:EE_ X-MS-Office365-Filtering-Correlation-Id: 1694ec98-4a0b-46b6-4121-08dcf2d9dff8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?iNB1fbNOqFArCB/MHYwHXYvPo4K6+0FIBNdHXU3Odw2cSMfiSPp/jvIyW4Jk?= =?us-ascii?Q?Uj+fp2OvXcubLIlxUctbDUlq/uAc0VA06WKBUGavp5HsjZlUSXGDyq5BCj2u?= =?us-ascii?Q?wEJyAEbQX2xMK3GN5+bNk9inYjajPzsLJETpP12TjWNBeOGT+pC84NxZkEC0?= =?us-ascii?Q?/0U0nUy34nik46GYb3AY1t5hERIx4e1k7nT+xQJrl+fyqwUj079K9lUM+Wze?= =?us-ascii?Q?euM/OYwyvRM0mstvSlNAEn9v+ScVKmO/QQrl7EQLt5+Vem+RopA6D8HX2Nw4?= =?us-ascii?Q?3FK9cvZ2JHkQlapM2Nn6egZxE4PDZTjf6NiRfq3CD44DNFEd5RP24bPjR5tU?= =?us-ascii?Q?1xN46BrHYniaBHXuA7viWG8JDzj0r/RYxBQmdmX1tJnS3CQmbMp6S/Ide3CP?= =?us-ascii?Q?Jggy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ZwJrpjwICgrKXDu?= =?us-ascii?Q?hXHdZAD8w545shYPiaotec8n?= 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)(10070799003)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?GAEs2oRJ9cb6A6wsNcJ/1ftn6B28ju7DBUXqGo6hzRMDIitM/5IM5SVQDTPN?= =?us-ascii?Q?RqdzrBXebKXfytU1QIlwRgUiVHbreCIYitwWm+YFatCe01Tj1baCwrnUWkLr?= =?us-ascii?Q?rQgESUOiCxmF3thGHk3/W1v/zgiXi7VInOX/vjYCo68WNnmIp97zoiuxsT+6?= =?us-ascii?Q?h7Sx9YjmhMz7UvQyE0C7JoiY0FmYIODK9CBeY4dnExFn+M+T20S4XlEGoa1c?= =?us-ascii?Q?LrQMYtms4Wn+Qvzx+AegOb3uvtqL0mGRZYVS3JMkQNGKM/Kk79/Uk7CWoghS?= =?us-ascii?Q?QME3N7Fpk5hdyXCp9D3AW3XKVZbztmco7E770cwiZsl1fbSv/9C2icK4MJe/?= =?us-ascii?Q?o/Qm2X4NmS7oJrhCjtiTpwVTKLh0GcYDwZN8aewlIM6w3BzUVsHeaDRVuXAa?= =?us-ascii?Q?E+q1MnSbKrCUtuA9ekUBeZW7aiqNId09FgxhAZHgrLJReDJfp0sWqzi1c9Kf?= =?us-ascii?Q?R9kHhewCG3y5w1ZLroZAMptaE+w66+x9PDVq/WE0G/X5DELToGArQXHvChSC?= =?us-ascii?Q?Ztm5ZGQQbr0DH8TvYpX4qFKshErrFiIiKnyxZspp15N3FcepEf1OrrWTN0Cm?= =?us-ascii?Q?qzV/H7GcZ0esfvD3zGmURu/jx9jISQRYzOkNlZlMFNs+ticNN8w6kXqwJYnp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dDjrRqkPiRO2zvd1s/JHJ?= =?us-ascii?Q?mZodbrpCL4TdJkiiDAuNo1Aksly3ghcfSjdjwXUdX7uQvD0KQDxvZtY7t0I6?= =?us-ascii?Q?IY+VEil4pKwSxTNpH1uxv0Krrs4cB+zYOikNZHWixSwN7F+A/BIvy2/e5pG9?= =?us-ascii?Q?Gz/uPQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 53v0TyjhEQo5d7mfSguNh+Ln2v8xmOyuc034sCvZWD0xbfzMJaaB5zl1dlCfKHKFsWgUFdR+70MWzhv1GYcsL8Yh6Fin37tJu2fq+8f9Sij93DrA7oi54t1SBMAfnO0YHiR2uPW5A3mz8lia9Z0iUNS7JsC1hQalFcEn8PUaQBU+yVnVoqTmZ//ZymIzlubicf1wxwVbhpiagEbMJ0MR7lqJPXtnZ4Om++E58IE14+EZxAlfQIEvzFZ5CpAAYyhLtcj8exJUFCsUm2N4GVYEV2PapTCcFcls9cJ0M9sIeQWJ7MTtjlH4gL3Nl8dL9dQgnyq22B3JcSBSNPR/fR5DBtpuX2c2Mx+QIT82Ql+LXnA2Slo6WYHefmV8LWxCqmTpCz31gXq305YLLgyHu7JzO3nRfeLMSLMpunljgSPr2t7L5GmIZOwU2AW2/amLyQDpa7BNUWgnvu39FUV5i8RGM6ZfMD/nHlwpWoMCqq2gcrMdI3MyiDNj6M365PrBKecJJ9orjksMY4eOav4y2eznM/eQ+lSQI6E9Lz5oW+BH0Pn4gsGyYY1bAKiCmHuzsVlfKZ8KX06aDDDeSInuItOitKkfuqrSPOEY6gza/+/46Oc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1694ec98-4a0b-46b6-4121-08dcf2d9dff8 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:16.2030 (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: /dIBKnR+sLk9fNlRTmE+b4te/3o03spUBU8Rd+p1qxmtdDhiLciTVDdIPAq1fXg97GrZrA/7FIfDM24n1gw0TmFb/1nTmoF1Jo50He/+SoI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7437 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 suspectscore=0 adultscore=0 mlxlogscore=979 spamscore=0 malwarescore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-ORIG-GUID: rZgmdcJBQiJr_qbbFFJtO5VJevnX5oaw X-Proofpoint-GUID: rZgmdcJBQiJr_qbbFFJtO5VJevnX5oaw Content-Type: text/plain; charset="utf-8" Refactor the map_deny_write_exec() to not unnecessarily require a VMA parameter but rather to accept VMA flags parameters, which allows us to use this function early in mmap_region() in a subsequent commit. While we're here, we refactor the function to be more readable and add some additional documentation. Reported-by: Jann Horn Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Cc: stable Signed-off-by: Lorenzo Stoakes Reviewed-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka --- include/linux/mman.h | 21 ++++++++++++++++++--- mm/mmap.c | 2 +- mm/mprotect.c | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/linux/mman.h b/include/linux/mman.h index bcb201ab7a41..d71f83b821ef 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -188,16 +188,31 @@ static inline bool arch_memory_deny_write_exec_suppor= ted(void) * * d) mmap(PROT_READ | PROT_EXEC) * mmap(PROT_READ | PROT_EXEC | PROT_BTI) + * + * This is only applicable if the user has set the Memory-Deny-Write-Execu= te + * (MDWE) protection mask for the current process. + * + * @old specifies the VMA flags the VMA originally possessed, and @new the= ones + * we propose to set. + * + * Return: false if proposed change is OK, true if not ok and should be de= nied. */ -static inline bool map_deny_write_exec(struct vm_area_struct *vma, unsign= ed long vm_flags) +static inline bool map_deny_write_exec(unsigned long old, unsigned long ne= w) { + /* If MDWE is disabled, we have nothing to deny. */ if (!test_bit(MMF_HAS_MDWE, ¤t->mm->flags)) return false; - if ((vm_flags & VM_EXEC) && (vm_flags & VM_WRITE)) + /* If the new VMA is not executable, we have nothing to deny. */ + if (!(new & VM_EXEC)) + return false; + + /* Under MDWE we absolutely do not accept writably executable... */ + if (new & VM_WRITE) return true; - if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC)) + /* ...nor newly executable VMAs. */ + if (!(old & VM_EXEC)) return true; return false; diff --git a/mm/mmap.c b/mm/mmap.c index d55c58e99a54..66edf0ebba94 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1508,7 +1508,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma_set_anonymous(vma); } - if (map_deny_write_exec(vma, vma->vm_flags)) { + if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) { error =3D -EACCES; goto close_and_free_vma; } diff --git a/mm/mprotect.c b/mm/mprotect.c index 0c5d6d06107d..6f450af3252e 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -810,7 +810,7 @@ static int do_mprotect_pkey(unsigned long start, size_t= len, break; } - if (map_deny_write_exec(vma, newflags)) { + if (map_deny_write_exec(vma->vm_flags, newflags)) { error =3D -EACCES; break; } -- 2.47.0 From nobody Tue Nov 26 00:29:37 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 13D821CDA3A for ; Tue, 22 Oct 2024 20:41:43 +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=1729629706; cv=fail; b=QYhSIzo3cPW4/KYk1bZlkJ2oWN0MGUQ9OkSo1/9QiLGBJG+MeYpBSBYEMtiEMVMpFsiEzPxM3Vq5XoRthcmCssOWhfvlWS8L4Z0jH2yOqzS/7Qqlky5ftnKL6xLjTF47b873DseywN5chSWGGPbsvEOgfT35I+hBCK8/ckikg1U= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629706; c=relaxed/simple; bh=UFR570+7lWgU2R9rqljhTWrjnz/apipw7WXakhB0wXo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=AfUF6gKB+OE3F7eCLxdrO84P37a/PpLyLpTsvrbOzXiXYGYudEX7RfBxBO7H86KURyKECeM9ZDv/vQD8Fn+u8vHVW4gbvTrdiaPm7e1T1SMKkCiwb4ij/2Bgjod1oxHiN1j7Xh1g32bVLJIPi55b1I2Os5s3Y45TalW97zS2Spg= 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=lUsgi4Ly; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=lbH5GsFw; 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="lUsgi4Ly"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="lbH5GsFw" Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49MK3Xjd022857; Tue, 22 Oct 2024 20:41:23 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=yCK8Nyvg2JGUKvD2t+3bdnTb0tis8I4rLlCnKTQX+/8=; b= lUsgi4LyKBE3aZPyUNIeryeuBekwLW1VbIC1L0M3CQ0O722OlDLyXJkvD/3KeMdm ayc6BGn4aVxPrYDLJ3nybaXJgqOOqHza9sWdBHYe0BK5cWDWm+IK3w9XT8+VmPi4 hSckP+Aaihg29akkzVe/v8w/Fe8w828H8IWXvuOcGDsTKclQ4FYfhpg9dFycpPnT TaYMHJP6sQd0zFrospyG7gC+CHVPxvwJhvjNN2yuPIPcLzt00T2tLWuKHPRzGT+Q aPwl6Z+W/I0AfLXw1SznTdd3d3e/MVcyCiyFExqEoGUbf7WiprFuVTrELobrjp7U 4Q9AHAVGOPGj8UncFJ0sTA== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c5456qu1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:22 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49MJL6AN011963; Tue, 22 Oct 2024 20:41:22 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2172.outbound.protection.outlook.com [104.47.57.172]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42c3780peu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vZGm0fVkamId52zWloPhuDTAx1oWI1wjGbs6Wg8+bjqEMCkHmy9Ym9IZb6STPx8oHnStyqxi+Whb9cDbdZgBhslxf805zOkqA8L2zxlpAyZh/DAQ7VqFoj1qTlhAtibo4pSHLCNbHZMY4TvWgnKgiY4BZGOOZFqPn/v26bT16ORhew+FvXkgrgOewiGRmaQdh0WYQqRdSeVwwfMODmWkADHjk2xRnwv0A4ZymgknKHA97TQBgDtIzBjhnmp/RjwqJUTnDZgBuSHBw55MLxg/sCdzTKghtv4u5zE4AC9YGurtRdz020ES8E/aJ2sOSqQ0YU+WiyuId2u76KpxDL/U7Q== 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=yCK8Nyvg2JGUKvD2t+3bdnTb0tis8I4rLlCnKTQX+/8=; b=artAZW/Hy/lzM8HJoFi1Bzq3qZCpdrOSFFxljl/+hvh+NBukPJvEVJEj6qrR/7bF9NWm4fEEBn0DyMm0k2yBqjZ0/W5/r9wKMwYeNqPEZU+Nm5+QW+ZojVzsJ7lj/u/iPlcVmZOiDbQM27zpgensjEwskpMkpnf2XV3LV2BURWbXxh/OBAz9j7uH7R/icHskHdGGYf9JnkXGfXRczn6o/62hr2JhC6GsO2WlK8iRvl5xoz7/4NYoThpNwkJnEw6sgKHbG8voONLtD+E8izj8MG7bi8KeCLNhrScU51HQV84dkWnylLmCksXl4F1s4xAtYK1BZsQs4X09Jh36knxiNw== 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=yCK8Nyvg2JGUKvD2t+3bdnTb0tis8I4rLlCnKTQX+/8=; b=lbH5GsFwHF9IYVdFa+1UdwIDdO025LzdQq+ljSamxoe7b87dttcFkABn8FX7oVI2z5s1Gm/oYh7nYNk2RJner3Ca+XzsNFuXVYA5QhyMStBJxpXkaPqa82yDSJwGnaHixmuLQnxf/suoE0bpd6LeGrCJmBAYUqK5YfNXC654KRs= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH0PR10MB7437.namprd10.prod.outlook.com (2603:10b6:610:193::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 20:41:19 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:19 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 4/8] mm: resolve faulty mmap_region() error path behaviour Date: Tue, 22 Oct 2024 21:40:55 +0100 Message-ID: <3bc3ef7520eed73472f7ffdce044f2e94f809b32.1729628198.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0633.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:294::20) 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_|CH0PR10MB7437:EE_ X-MS-Office365-Filtering-Correlation-Id: 6427b3a2-45ed-45c5-8092-08dcf2d9e218 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?eKQHymW4Tni8S9JfPECtWQ6HVnVEEwobYTGghi8q/EaHho15cVfQvxzRS8Dg?= =?us-ascii?Q?qXpuPqYuJAqS83A35NCQ6VJhmwda0Micy3/BuRlyMt1l3gYDV2Y0KGo2UjtM?= =?us-ascii?Q?S/pbA57cTh196L/JYzYtXXYrgJ91yeAt4pQq5rTWIlWOF6sdy3H8Bk2tRubN?= =?us-ascii?Q?5jAx5O9TmNXIuWle6gvfhAoA/z2XlCq1BWIiR8ybVW5dD/rFClyuCNGjJo3l?= =?us-ascii?Q?RFP2aQtA6oD9BPRVjxRl2Qtjsw3FGRuZlY0rk92rYT1Heg+aUwCH7UiMmRzZ?= =?us-ascii?Q?iPaRxXA1j8G8peThIAl3YGSYKLgcPjOG09m61T+tv48Hpt5UeHuUjp4gAdko?= =?us-ascii?Q?qNkQCjoRxVCg4oF769gvhono3Bp1rwc+60awg8V+ruAiIqEvBs9Hza9fA9gr?= =?us-ascii?Q?6J2h+r3ukP5TC0nY1lJD66+8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x124IFA?= =?us-ascii?Q?wZ1qAO6POhamQ2hE5oYBWzO7?= 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)(10070799003)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?KwoFQ5PGL98qQ7xrRYK1DLKI5Rt7treR0GznHboc7d+ItlODxeScuv6QXRqh?= =?us-ascii?Q?fdMm9T+cACiBgyZfzAcg0FecWM2enbEHw7hdVe5FOrTHhJvuzhWDN1sYLAQi?= =?us-ascii?Q?hXF2Rha6xLtsMccQzvJ3SSwrT9BrLFX4kwj/P/xlaRO5gZQTjMIVHkfhYlvY?= =?us-ascii?Q?NsxI9hd5FmbY3y5Qot2K8PGevowOwhDYeCpmUziy0KIHr/ctVAjkWf4EJa49?= =?us-ascii?Q?XeQIZ25EbeCHRIhvJCijBl858FcXC1wq7jHEtYAFOUiL8lWsaYh5UqOdJaQ6?= =?us-ascii?Q?Ui28mqyXLVOnNmOPpXlLBE8qcqRcDn/jIw58WWlwl+yOZy2hJgxF393sepvG?= =?us-ascii?Q?qV6D1T34EvfhoL76GiYT34FpTss9YKOTor20IewxymQ7j/x31zZhO/AxZmxf?= =?us-ascii?Q?L6DZCbpEMPFvLKSErM1fNcbshxfyqIUYbt5zAKUdp2/7Z3MEwuF6REu0z8re?= =?us-ascii?Q?EDW7DmErAfS8x0XZ9+AO9T1cdiTK3v+hRdAhxmJzSsB/wtxG6vX14l421br9?= =?us-ascii?Q?mO59kWu+ANB+3vnD75kOBG9xj2VUG0lQx8URcc/b7XomxsD6EqY6Pqt1TJLM?= =?us-ascii?Q?Nkho0VdQxjtejOPf3I2i9VZe+70r0Xz7vgd2Rpo9dR07GqWUYoMW9PBixurd?= =?us-ascii?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SH3sPlBbhxqo?= =?us-ascii?Q?Dv0GbGnbeiMrtOIyz6xiA/S/Yrph1JvsGe8OK+/VG9W/uXhGeBuvwSEf2O2v?= =?us-ascii?Q?UfT+TuicBLR2aQq6VVAYSapeQpjR6IPHx0xMwI4MIrgET08VrS1BLRDvznbz?= =?us-ascii?Q?7Ge4ow=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: BIWeL5MHZ6d6nahzchFqVJcoL4I/PIzHv5DxOVOHuz2cbpu3R4rOFk09s00qbH8FeDz+M2EGJaPKHE2plNyifpMU5eXCL2KN3iu5skqgz4IKBBlS0Gdfj4RJbEnbhH6kox1+jZOL/bda/bZSY81QTMMlTA8K4/+U92aU4UIzFYqVD1T7X/03O8FFRP5MhnkzBN3k8P313XjYW2Xx6zalbCc4LR8Yyaowel5kiMHL7owkMIotpeJF2izGrtboFUubvCPlmqBH0WgL5sTZZisadr9I5Vq0qQmW+wF8WeLHHGlOATiJ/danHlueCaD7xyK3nGQC8fkB2p8T38DKaSZiO8jN9e+yNyOy06efFMd3AHcv39NL9gEGLitHz8IXYBnyEW1BCzh59l+pEXTIPXqm36B1XTNjeLHc7NQAaH7MCocaIyiJWzcGtT2zmLrgRetnBhF4EgUks3QT0Fhf7WT7DuktX3Ojer5g0rowfX5iZztpbms4xSTsAW65aHfT+CQGmRHc8Q/zXmX3Q2WU6UWc2n3XgV1T5A2oufQSKo0CH3GFF0D+5LE7oa0VrE3qnj+G21LsmmphODJvn/xJqkuoKQg0l+zh+sgjrLA5Fdl1v9Y= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6427b3a2-45ed-45c5-8092-08dcf2d9e218 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:19.5753 (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: DDXZ79c0ZXi1o+vanivQyNg+JbbksZcexj9T5gLMvbZ7pbgZiRxM4q+Sb9xy5IKofEX0KZsTGrOq/ztkq+EhAa71rUZSr2hJCNnf1AKXuIY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7437 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 malwarescore=0 suspectscore=0 adultscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-ORIG-GUID: 5g-7dUENjQSk1UQvQiT0UM6KKFOhrXLx X-Proofpoint-GUID: 5g-7dUENjQSk1UQvQiT0UM6KKFOhrXLx Content-Type: text/plain; charset="utf-8" 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. Reported-by: Jann Horn Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Cc: stable Signed-off-by: Lorenzo Stoakes Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka --- mm/mmap.c | 120 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 66edf0ebba94..7d02b47a1895 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1361,20 +1361,18 @@ int do_munmap(struct mm_struct *mm, unsigned long s= tart, size_t len, return do_vmi_munmap(&vmi, mm, start, len, uf, false); } -unsigned long mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma =3D NULL; pgoff_t pglen =3D PHYS_PFN(len); - struct vm_area_struct *merge; unsigned long charged =3D 0; struct vma_munmap_struct vms; struct ma_state mas_detach; struct maple_tree mt_detach; unsigned long end =3D addr + len; - bool writable_file_mapping =3D false; int error; VMA_ITERATOR(vmi, mm, addr); VMG_STATE(vmg, mm, &vmi, addr, end, vm_flags, pgoff); @@ -1448,28 +1446,26 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, vm_flags_init(vma, vm_flags); vma->vm_page_prot =3D vm_get_page_prot(vm_flags); + if (vma_iter_prealloc(&vmi, vma)) { + error =3D -ENOMEM; + goto free_vma; + } + if (file) { vma->vm_file =3D get_file(file); error =3D mmap_file(file, vma); if (error) - goto unmap_and_free_vma; - - if (vma_is_shared_maywrite(vma)) { - error =3D mapping_map_writable(file->f_mapping); - if (error) - goto close_and_free_vma; - - writable_file_mapping =3D true; - } + goto unmap_and_free_file_vma; + /* Drivers cannot alter the address of the VMA. */ + WARN_ON_ONCE(addr !=3D vma->vm_start); /* - * Expansion is handled above, merging is handled below. - * Drivers should not alter the address of the VMA. + * Drivers should not permit writability when previously it was + * disallowed. */ - if (WARN_ON((addr !=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)); vma_iter_config(&vmi, addr, end); /* @@ -1477,6 +1473,8 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, * vma again as we may succeed this time. */ if (unlikely(vm_flags !=3D vma->vm_flags && vmg.prev)) { + struct vm_area_struct *merge; + vmg.flags =3D vma->vm_flags; /* If this fails, state is reset ready for a reattempt. */ merge =3D vma_merge_new_range(&vmg); @@ -1491,10 +1489,11 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, */ fput(vma->vm_file); vm_area_free(vma); + vma_iter_free(&vmi); vma =3D merge; /* Update vm_flags to pick up the change. */ vm_flags =3D vma->vm_flags; - goto unmap_writable; + goto file_expanded; } vma_iter_config(&vmi, addr, end); } @@ -1503,26 +1502,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); } - if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) { - error =3D -EACCES; - goto close_and_free_vma; - } - - /* Allow architectures to sanity-check the vm_flags */ - if (!arch_validate_flags(vma->vm_flags)) { - error =3D -EINVAL; - goto close_and_free_vma; - } - - if (vma_iter_prealloc(&vmi, vma)) { - error =3D -ENOMEM; - goto close_and_free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif /* Lock the VMA since it is modified after insertion into VMA tree */ vma_start_write(vma); @@ -1536,10 +1524,7 @@ unsigned long mmap_region(struct file *file, unsigne= d long addr, */ khugepaged_enter_vma(vma, vma->vm_flags); - /* Once vma denies write, undo our temporary denial count */ -unmap_writable: - if (writable_file_mapping) - mapping_unmap_writable(file->f_mapping); +file_expanded: file =3D vma->vm_file; ksm_add_vma(vma); expanded: @@ -1572,23 +1557,17 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, vma_set_page_prot(vma); - validate_mm(mm); return addr; -close_and_free_vma: - vma_close(vma); - - if (file || vma->vm_file) { -unmap_and_free_vma: - fput(vma->vm_file); - vma->vm_file =3D NULL; +unmap_and_free_file_vma: + fput(vma->vm_file); + vma->vm_file =3D NULL; - vma_iter_set(&vmi, vma->vm_end); - /* Undo any partial mapping done by a device driver. */ - unmap_region(&vmi.mas, vma, vmg.prev, vmg.next); - } - if (writable_file_mapping) - mapping_unmap_writable(file->f_mapping); + vma_iter_set(&vmi, vma->vm_end); + /* Undo any partial mapping done by a device driver. */ + unmap_region(&vmi.mas, vma, vmg.prev, vmg.next); +free_iter_vma: + vma_iter_free(&vmi); free_vma: vm_area_free(vma); unacct_error: @@ -1598,10 +1577,43 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, abort_munmap: vms_abort_munmap_vmas(&vms, &mas_detach); gather_failed: - validate_mm(mm); return error; } +unsigned long mmap_region(struct file *file, unsigned long addr, + unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, + struct list_head *uf) +{ + unsigned long ret; + bool writable_file_mapping =3D false; + + /* Allow architectures to sanity-check the vm_flags. */ + if (!arch_validate_flags(vm_flags)) + return -EINVAL; + + /* Check to see if MDWE is applicable. */ + if (map_deny_write_exec(vm_flags, vm_flags)) + return -EACCES; + + /* Map writable and ensure this isn't a sealed memfd. */ + if (file && is_shared_maywrite(vm_flags)) { + 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 unlock) { int ret; -- 2.47.0 From nobody Tue Nov 26 00:29:37 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 9F5111CDA13 for ; Tue, 22 Oct 2024 20:41:35 +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=1729629697; cv=fail; b=sZMFBOcS0xpn3NJ2LtMJT1kz7EfDlrWg8GM6nIxnGiu1qxz83rHtD7DJPJJiCxZq23MS0joHmErOkwpsto7qdgiruJxq9yOJ47GtKXo9oAib5SEWbS8b3VCQJaVrCrEzdmOm65OFG6WA9uXUYqHz16RmW0ucfW9X5O+Hy6KYSgc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629697; c=relaxed/simple; bh=E0qC7Ua3UXzEi3XMLrX+5853B+raXqW83k4z2/N9IIc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=uuVILRVgOxvCtQ+10N+vNUbWEzuf0FthXxcEjarkdLUQFNrabCm5/QVWC6JlLHycP4S7BtyNrvl3PPXZ/5EWUU+QoFfRbXwbL7qGl9haewUFHgsZboHyW6bK0TFStjEjhHM6rABZJHTl8pz3qQP+uGIfH9aTedG4Tt+OVGDt9gA= 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=L/wKDuUd; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=qPL8U3pU; 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="L/wKDuUd"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="qPL8U3pU" 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 49MK3XDW025813; Tue, 22 Oct 2024 20:41:27 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=IYEq217ZQe6BhqkHPhji53xMVlBJZl7d8VhT2IXVRxw=; b= L/wKDuUdbU68Zrv5TqWOnYDmQh62U8K3xVF8sGWtCmTpPCSu7cCUQFmQrVjc2HEq 26B613sSVob/uOs02fQafV07K1B19let53bHKyZhLEVsWBluf8g9qXjlFt9yGVVC 591uL8omVJ5XtTAP/4K/GfgQ+/PXAWQFJsP1Q5i/MTnZ9wpR9B6wycrSK00iKQNn 06JTee7X6i2Sz7C2i2GaUkfsfxsCFjQmX6krrs3Od24oCkZgNYVfIbPMlbryQKgg ucjwstHm/MBsb71Rq4pf3Azv0a65/Lh25Ecs0DoqQcsA8cA+ZVep9F/wAtKikJis OGFW9/bjXgBveW1aisBqdQ== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c57qene1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:26 +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 49MKDugL022727; Tue, 22 Oct 2024 20:41:26 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2174.outbound.protection.outlook.com [104.47.56.174]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42c8ew34e4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=JBo2qVHpxX5nKQ/y+q+1qlYDO0Zt31YpZhWD2n0yd9WN7PnPgwSTqgnzfSNlt+RSzWt2iZaDJCXfCFM0nZr2hmI+vpXCxcjEwRdKLaH/fxZLLTMmjLLqOuS3iqJE9VS8BtWQNUFqTSQ3VAIiCdti3q8GxSiZREsFIgBGUy4OO6unxJ1rigG7e6MCQ4+HFiIrgRj6xLihzv1m4k0Wm6/zX1bpyxGDShphqwDwY2ghvoxbOHjRHqKMsrYLzGv98n3PsZAKtDNAlD2y6O080gB+/o6kNNXfKlbgCHXxxcwRoskCFvGEG+8dXBZQ4N5+snXoanOof1vH9kctsCm/IZicIw== 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=IYEq217ZQe6BhqkHPhji53xMVlBJZl7d8VhT2IXVRxw=; b=a7pIYJ+bnksrgAu2daDJGGLZOPPHvtLfhhsC8WWLVXEyzUfawFgosmbdETfu1+PHsNDy7jWsgLu3KyhRbXt/X8O+KStw2qU/tTMbUVK9y+YQ88GHXxTZufC8fSLQUtAuzYgl8Oc2MGRgYzCphV2B6LcbkDApjQponRquUGgH9jPaL+3hWsF+MQPV+bcQ6IoN2Z3yBKf0DLn5ksM7nO+jVx3us5FnDszF6RAY2LhWBCgngucnce25oYKFhjaObFVWIBhz00ocnuYbrzdQnBhT7M4IRZkH0r4m2wCdBJSfCmG7sUrs12lgILLOSV1b2mdf3yWqXP2KjpmXzGydtG6r3g== 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=IYEq217ZQe6BhqkHPhji53xMVlBJZl7d8VhT2IXVRxw=; b=qPL8U3pUVRD02G3BLz4TZur3jkgb35Naix5D/QApbFulmzdc07oXT7QCDoHv5dZtywAnM5bECCTuYgKYZfLc48fVckS0RicwXJni7qpQWnWGBvyKPA/EG7v0vuxTOvu6Zylv6NCbmtWKaCXbU8dx36zwJcIFsc9ebUZc38n8C9E= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH0PR10MB7437.namprd10.prod.outlook.com (2603:10b6:610:193::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 20:41:23 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:23 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 5/8] tools: testing: add additional vma_internal.h stubs Date: Tue, 22 Oct 2024 21:40:56 +0100 Message-ID: <1e039f29776bbb7df5878f3ac85ef6732f450fc6.1729628198.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0657.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:316::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_|CH0PR10MB7437:EE_ X-MS-Office365-Filtering-Correlation-Id: 41598306-88f6-4e39-5526-08dcf2d9e40c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?J+OWaVWsNAc+hj8FuUai3GoEVRlfPqP3l7FfbwZ3UvNVKNJO05d+1uEwRAPh?= =?us-ascii?Q?2dh3BjxldjPvRgqiD1Nccage1a2sADzGxIhbPrzHTx31KQNgZ143BpkDXPHG?= =?us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/CON97s04gviRBVgl11YWpDvFv1yj1Ekx?= =?us-ascii?Q?z9piDX8zj6K/SZO9OpNLdRpcpP8l8VzuqPAd8aFqrUt4AtuyrNKrJwuS0QV5?= =?us-ascii?Q?+jyFX0UT8z6l2i/jWTumQ3aYxZS06EgLO2x9O3AIjEGBV5eK/oOQKgD5ajz5?= =?us-ascii?Q?HFPKyPBJadEJq17e3X5A3TU2E64b4rJJ2PwAkBS/VoYxq5/vPvnuKc/Ha2G0?= =?us-ascii?Q?yMbcMjYn3pC4w7z5SWEewH5RZpv7BpHOv17ZlnF/zvj8wI13/5Ev15ulxsil?= =?us-ascii?Q?HXYl7rYwDc9epRaFiO6o53lnu8/2acZlFdBEFb3DHywPSIp9EUvGjHJ9vCt1?= =?us-ascii?Q?6z7U210e/c3CPQOrn36vrR6r?= 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)(10070799003)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?2gE+e8nSObt/suGu0nf0Y5h2ODwQBeTfgfHRd5vMSwtR50Ids1//k9llcB9Y?= =?us-ascii?Q?bwrMAgne0AfRKrRsX5EgJapKT2k3QqSG1U8Xoel5du39TFKiVIYNEUaJm6mp?= =?us-ascii?Q?gxf9mclDD+oZaHVL1elyfEl6IHwc/0svF+JzVLiU9Bl51Hw2Vs2IGwGIzy/I?= =?us-ascii?Q?9V5atnDuQRxpCqK+35ZJD+UHR0PCKZ02/7P0mvEpQrbOx1vuPLfWJaOtY4aT?= =?us-ascii?Q?MwyokYU5Te8FvNWmIw1bKdphkh3VRndVGitr8vHA1u+ghyabpJ/fVN3YUhaR?= =?us-ascii?Q?F3h1K6uBPWN48es1+pWLm+wVeHC9MipkpIuJ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pItVftg9XvWEKW0Vt1v3Z19BKo5ax54vdts/eLQgQHi1q3mKqm83ocx?= =?us-ascii?Q?Cnn4lwJl4S5OtndZqDh8UAYzMPqmF435UJhtR9Lk5RFt6WGH4ZsQC3nx8KHk?= =?us-ascii?Q?iYXaOS2h/1jkNcPaojVJ0g9pxrLZwEBSfMQbBMHt6PS+mRkbvqKzzRpkNRhk?= =?us-ascii?Q?AUsRTI9dCVDDR8HbpVl80tUMC+3ap9tO54StUD4pJCo+BihbjOWk7tYw428F?= =?us-ascii?Q?VCP/LlojyxwSs1NG9iXqcvn+msppr0TkZO0EWpfWtbyS3x89V5Ho95qGUSBv?= =?us-ascii?Q?WRUEFtFWNO+pUHJwTGiCyraMApiD3a0q4foVm62TgDTHIb2aD72unrWuD6kj?= =?us-ascii?Q?sjqOjmg4aQEeeotQjl6XDrpb2yPQWFcOUPsCeCz0HdeSP/vbLrzx2FHlRQ7t?= =?us-ascii?Q?YScSNH9QBjdqwEiayzT5vLaKKO1hraIwkW9EhU6IPkOKStLR4BM3DupTed9y?= =?us-ascii?Q?Rhn97g=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: DJLIKZUiyiX8zxlZCPxUmJv4ewZQw0qgqP0H+vjAzUwlLjT0uDW2yhZIzKVKqmOaBmsAci2J8/aFyfqT8XYgasehWoBNI29js9KiYHJcWOIYRbZRVNSP010Bi60ObqenRR0G/R5JB2XHhqoema5eISutm9eTm4mq93sd8OJB/m5sPcwGciu0MYpGumsLkkwR21vLQaiW/MS0BAFAJp3m8acylx0zQvFt8HyMGHfqu+rUDPAQ4Teia2V2Hy9BYrqOPpu+WNOn1+4ClHN20pafFz7IgYH9iO3Vwp1hOoIyTleV682uDG4M1dTIglTpnGuD+DWEG2Huojo3dhg3iEILtkj30TFt+UCmcc57LiZwNZ/Iox1nbQJ9qJzjznj1E6yOgsdknTpxqMm2IBq4m1TLSnsi8yoxQ0DXW1F01G7P9p7738lPKvot2zi7LgxCjSLHx5cs9g126vREIJA2zjDnfpgzL4S+UaFS006q3JDoVMPVJ6ZrrorztSsbHmOr2oxUIRlJ5b3xZskkMaKsJn4NKYgP45M7SrBYAtt/bUrwcLqmHGzKqxKWtYcb2nROyLBvimRlhUwXkxkgmYbOuBZpbo/IuTXZ+M4/0Zyys3SA87o= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 41598306-88f6-4e39-5526-08dcf2d9e40c X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:22.9831 (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: 8SD7InpWsUJAjLH18KUButYk6O1qgTWTPfBUGJtAc2m3eoP+4pSJuDZlQl8Nb7EZLzjlrCOlc2ZTXlGe9+Yg6CyXPs+bsRgO1UJ0Iy16JI0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7437 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 bulkscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 spamscore=0 malwarescore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-ORIG-GUID: f9GLMFmV_wR4uE2WylsAauF2AlkWZZRf X-Proofpoint-GUID: f9GLMFmV_wR4uE2WylsAauF2AlkWZZRf Content-Type: text/plain; charset="utf-8" Add some additional vma_internal.h stubs in preparation for __mmap_region() being moved to mm/vma.c. Without these the move would result in the tests no longer compiling. Signed-off-by: Lorenzo Stoakes --- tools/testing/vma/vma_internal.h | 106 ++++++++++++++++++++++++++++++- 1 file changed, 105 insertions(+), 1 deletion(-) diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_inter= nal.h index c5b9da034511..bec50a8c846c 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -44,7 +44,9 @@ #define VM_LOCKED 0x00002000 #define VM_IO 0x00004000 #define VM_DONTEXPAND 0x00040000 +#define VM_LOCKONFAULT 0x00080000 #define VM_ACCOUNT 0x00100000 +#define VM_NORESERVE 0x00200000 #define VM_MIXEDMAP 0x10000000 #define VM_STACK VM_GROWSDOWN #define VM_SHADOW_STACK VM_NONE @@ -53,6 +55,14 @@ #define VM_ACCESS_FLAGS (VM_READ | VM_WRITE | VM_EXEC) #define VM_SPECIAL (VM_IO | VM_DONTEXPAND | VM_PFNMAP | VM_MIXEDMAP) +/* This mask represents all the VMA flag bits used by mlock */ +#define VM_LOCKED_MASK (VM_LOCKED | VM_LOCKONFAULT) + +#ifdef CONFIG_64BIT +/* VM is sealed, in vm_flags */ +#define VM_SEALED _BITUL(63) +#endif + #define FIRST_USER_ADDRESS 0UL #define USER_PGTABLES_CEILING 0UL @@ -698,8 +708,9 @@ static inline void tlb_finish_mmu(struct mmu_gather *) { } -static inline void get_file(struct file *) +static inline struct file *get_file(struct file *f) { + return f; } static inline int vma_dup_policy(struct vm_area_struct *, struct vm_area_s= truct *) @@ -920,4 +931,97 @@ static inline bool signal_pending(void *) return false; } +static inline bool is_file_hugepages(struct file *) +{ + return false; +} + +int security_vm_enough_memory_mm(struct mm_struct *, long) +{ + return true; +} + +bool may_expand_vm(struct mm_struct *, vm_flags_t, unsigned long) +{ + return true; +} + +static inline void vm_flags_init(struct vm_area_struct *vma, + vm_flags_t flags) +{ + vma->__vm_flags =3D flags; +} + +static inline void vm_flags_set(struct vm_area_struct *vma, + vm_flags_t flags) +{ + vma_start_write(vma); + vma->__vm_flags |=3D flags; +} + +static inline void vm_flags_clear(struct vm_area_struct *vma, + vm_flags_t flags) +{ + vma_start_write(vma); + vma->__vm_flags &=3D ~flags; +} + +static inline int call_mmap(struct file *, struct vm_area_struct *) +{ + return 0; +} + +static inline int shmem_zero_setup(struct vm_area_struct *) +{ + return 0; +} + +static inline void vma_set_anonymous(struct vm_area_struct *vma) +{ + vma->vm_ops =3D NULL; +} + +static inline void ksm_add_vma(struct vm_area_struct *) +{ +} + +static inline void perf_event_mmap(struct vm_area_struct *) +{ +} + +static bool vma_is_dax(struct vm_area_struct *) +{ + return false; +} + +struct vm_area_struct *get_gate_vma(struct mm_struct *) +{ + return NULL; +} + +bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_pr= ot); + +/* Update vma->vm_page_prot to reflect vma->vm_flags. */ +static inline void vma_set_page_prot(struct vm_area_struct *vma) +{ + unsigned long vm_flags =3D vma->vm_flags; + pgprot_t vm_page_prot; + + /* testing: we inline vm_pgprot_modify() to avoid clash with vma.h. */ + vm_page_prot =3D pgprot_modify(vma->vm_page_prot, vm_get_page_prot(vm_fla= gs)); + + if (vma_wants_writenotify(vma, vm_page_prot)) { + vm_flags &=3D ~VM_SHARED; + /* testing: we inline vm_pgprot_modify() to avoid clash with vma.h. */ + vm_page_prot =3D pgprot_modify(vm_page_prot, vm_get_page_prot(vm_flags)); + } + /* remove_protection_ptes reads vma->vm_page_prot without mmap_lock */ + WRITE_ONCE(vma->vm_page_prot, vm_page_prot); +} + +static inline bool arch_validate_flags(unsigned long) +{ + return true; +} + #endif /* __MM_VMA_INTERNAL_H */ -- 2.47.0 From nobody Tue Nov 26 00:29:37 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 1D3E91CC16E for ; Tue, 22 Oct 2024 20:41:39 +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=1729629702; cv=fail; b=lGjKyK3jiI4viyuVtRtCK62ldUyDlyGTx8nYOF9rFAvToEK6SBnJpmASnGDzJ73x5FRaJAWkkiUJRnsI3sN9GH5sfZopCb1dvXTFt4CwyF/PPQl/s2OYKJ7KrN0xIuzC71Ch2oM8buN0WViKKHRUvbZyFNwF86PGmtReus9DfPU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629702; c=relaxed/simple; bh=j6Bry8vwwiTHR0NSjXHQVEt2yKPbmC7N2LXNpP/nYdc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=KNxhOhCLVTArT8OwNCr0vTOerdfZVZtlfi/ITcUGErHWcTvtvHp7cQEVY5buz80HPw4aI1YdD9IaOr7BdX0SDy/zl6Y26dQXsEeW04RcfuKXozrkGlVLqOviAK/qEG4x9DrEK4J885YLKrqDi/EjFKEEbrslp3wHgPwXJNx3P/o= 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=hScche31; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=lh9nQm6b; 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="hScche31"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="lh9nQm6b" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49MK3XLj017466; Tue, 22 Oct 2024 20:41: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=6OaAYLfv3ucTeWXw9L1215jPvy4GpESA8WWmWuVuaUE=; b= hScche31GSNLLsXMjENWvE+re7clPwY4OOZJHzH7RPyymwXU28uuKkRCLx0mIuqQ /RWQnnqy13AhfQe7F58fYAvjdoP0GNLDcMb/8OF7vHKi41YMRUQ6GuJ3M3+gUh5z UqGG6mSBeXpOXTYWJXbqLqMwbZ1AU1sytwxB62lg2nVEPEEKwS6I+XcwLUFGOa7m ytLLqbP+tE5j2IgiBogkTif7P7wxpUcCQbjZ8ErIJLlBrqI91sXfBYFc260UjNKh bgvG1fZWlSCnt4hmeg6gTGVdYFuTjccIH39Ew1/iRtnSBG9lvSAp03Sz9w/fLauj Z3mir5/Hwec9O4u7MrrboQ== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42ckkqwy26-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:30 +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 49MK26bU020317; Tue, 22 Oct 2024 20:41:29 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2168.outbound.protection.outlook.com [104.47.56.168]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42c37ef3r9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:29 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=KOvA59Upmf2U2dj9/HlbjFJg74T+8e5GXr0wnjeChbAcGBK1ldfmmPwmhG0nhHiAiNIpD907sMdASMfxvhPWcUzqg/aZ7jXGwV91rJepM8Or4mbW99qLb1ZhcbFHz4aE/ITQVSYiFv0xpzPvqBLdKZpFGPUWIN4BAHY/rlXDk6U4QYFI53o45Yy5Jpb/WT2X13Bad+Ijr5rup1UNrz/ioalQbjTZdG+y+FW0dFh9PYMj+v0y/EPkNsZvgsCbEFn4Ho/0CC0xYJypY7UhcCbZtNWykjzfuDLuijlVxYBB+gFGZQSkiS4kdpAqpwcF915DFxCE7qUiD3kGRSU9zhLMTg== 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=6OaAYLfv3ucTeWXw9L1215jPvy4GpESA8WWmWuVuaUE=; b=BWYt+2seWqL0UdBjDrYj+k5/EG4/3bcvoM5BKP6Tup4xBJpNpxF52BWWpxWLwC4U3n4Is61DS7329hjnTldRQ3WtTNGRI6KdSGpUF+7mTswTrFvYsdQJArnbAFC7CcpX9YCgIe6SbNT8loeqtxmh5x6kZOpNiaCM7TxFaCZdin9kucguZSvobZPEmd5XxWGFUOJ0gVsotIf82TOOnG6PWwnux75mK4mJPagKvmlcyIqjQc1KVmnQwrChlSw8CokRVUEfmWmu2wsjC17ZG/z/Gr+ccBC9TWSE31pt6ojnM1RcpeiXLytlbF/mvl4o4DbgKmue1f/Bv2qs8V1EUu7J9w== 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=6OaAYLfv3ucTeWXw9L1215jPvy4GpESA8WWmWuVuaUE=; b=lh9nQm6b9q21rQgYYucr58ISykTs7rlpS/uUxy8CHsPoC80S2x6p2pdQl9kfZtHmjqfr/M6GG8ReXuED3jQkQ2meQhjkjPV6t9NXQVcAov79/58L25j5YRReaeR5ZkCsb5jWlgcNErltucUiA3SVEs/2WUJdEW1xDFgM2Ym9Mc4= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH0PR10MB7437.namprd10.prod.outlook.com (2603:10b6:610:193::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 20:41:26 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:26 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 6/8] mm: insolate mmap internal logic to mm/vma.c Date: Tue, 22 Oct 2024 21:40:57 +0100 Message-ID: <0b90285f68de7d091595d78422f9e48eb8600b7a.1729628198.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0661.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:316::15) 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_|CH0PR10MB7437:EE_ X-MS-Office365-Filtering-Correlation-Id: e2b3d9ab-fbcc-4cdc-9d4c-08dcf2d9e64b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?MiGhNlcxffPlXRydNIxcInTUpWa8tHtnmTt4vA1MQfmFwTVavKjz/kcUzITD?= =?us-ascii?Q?Bf/F/bZl3wk1naIGDyyADHj2vOaACS6OODmIbncFg64IChWxKKN/PNa9+9b+?= =?us-ascii?Q?8tkRhsG5TjC0mB+zw1j95WqaRMZhD1ify+pOmd9GXJCIJIbmnMQM4vLFwUML?= =?us-ascii?Q?ShbZ5kVDA0o2TylsXfcnTm+lbD6CYceqUM6bhKmE0+URv8xX+Ev6akUg5MnE?= =?us-ascii?Q?qVt5pO/B7FEraLelRJFFn4MWUI6uIn2uf+GCK7WIPutI+8/zNKzWm3sCDKNO?= =?us-ascii?Q?knt4b/74dQaX1DlvNmUigAbF51YWEr//FB+Z3GYKOduY/ACPY2v26jijKN7x?= =?us-ascii?Q?hQJW2Eq9qgi59HWktwRUz2gRbkBKNs8ww/mXRajWoNi/n6ZsQrcLC34ISpCO?= =?us-ascii?Q?TU2QY+bu63H2Oon5jn78wnWU8urrNFEQ9KPOJtQHc0hhuZx3hSmcMxobqDFN?= =?us-ascii?Q?q+/zoNL+1NrHJFpLek7Ev8IRJLi28PivIoPcYDvjWXoer63ZdJges35Tvup3?= =?us-ascii?Q?Bhm/rjo7RoIjRgpK8kj0fCrIYr98uUbJppket49Tt1XsEl6jo9yl0YMjQjtT?= =?us-ascii?Q?Bbbq6gevet3vLDbVxzFfllWyA5hwOLM87boMpQYLRgaHeQt4aV60WS8k7ooH?= =?us-ascii?Q?C6r1TxnUquKzNohExhUOkxa2WB28A8BUwmOqLTrc5PS+PQ/tcgVDePjDsD0J?= =?us-ascii?Q?n3WwC7e1APAJCRqjTsQi9CkqXD4bF+2D3/37J7eny0hrlZ83cLcv/ASsR65p?= =?us-ascii?Q?dxJEbjHovYO1zdm4VaY6a0E2VcWBoqFnNP2mUWJvUIt+ddi8QDkRs6qUgvQQ?= =?us-ascii?Q?Wo9BV2c2ipk65msAjBK6l0CZUouplRjGfok0aN9Va/vE+bZDslW7J79Y9DFA?= =?us-ascii?Q?iFw2XNurOK2fzvRVi3VpniIAVLT+JOoaypJrnsQ+h/ogesYrgdB+vpjFNspX?= =?us-ascii?Q?OGmjeXK1hi1cceVYpXqMrRHXcWhdLnGYVZZP/87cC0UV0o9Cc13V51dPrupc?= =?us-ascii?Q?4wWdSQXaHLBYic5LFEUdsp58RK0yoRJbNBaLSAhTnU7GxY4Nb5hSjAmFzcYc?= =?us-ascii?Q?QaTr0lBwzj6bvgzI9HGJzLpaJ68+oI2gC/Kn+7NsgHFPYB/BQq02dWpGtddv?= =?us-ascii?Q?k6vMBvrnX/MMpiDpEoso5gkINu+lwZa0yQCnV34NXRbXYpGzU7H1wyvxd59y?= =?us-ascii?Q?rOBlNHAda4a/qxR4LAzgps4ky1y7jrCZIKsueZ4maDKz4aJCXJT16XwwOvE4?= =?us-ascii?Q?FmbaGXlhbqfaPTuas1ldVGUxueNLSQWFKtMQ4cinRi5n+Dibc3cfBiLlM8nG?= =?us-ascii?Q?N4XmbOlqucVUhlSf4kBHrzgz?= 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)(10070799003)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?rn/wtlHiuW1SyKoh26wb47uJHyJCLBavHNQ0Ibu9PYTeGy1n1GEOnIYU4Tnp?= =?us-ascii?Q?KnYUeO/PjglCHN9u1SFT7Dwo+1Z8W/0Pzkild7s7j9+MUw3GlZj4pLSZHnbD?= =?us-ascii?Q?BIfHqXZdeXPa+3Cp8n1mbFVaBWwVRkReYeA0XbGUA51TxyCX7i7vSkwYDbOD?= =?us-ascii?Q?b+PGv5AHrMhGq6KNpHO53nCv4IfHhF0+8xkVs0KxqtMOU7AC6quLTSgRTeWT?= =?us-ascii?Q?YrHcCr9YWoIjdEemsUJ7A/dgmMLme/4tdxlsGAt4bFQlph0MdqvKjrhrFyu2?= =?us-ascii?Q?eQgHF5A/gFFrb9x7qQGzGwd/GcvDALAP71V2Mit5XL5CVv9ciB22Zg/dsYBX?= =?us-ascii?Q?UySG0UpMN273+VAYEi082cqaRC9Uzw+VD1eCXFhmlkinB8tvRghDyO+XdIcA?= =?us-ascii?Q?DrbfwDFGssJLVjiizzhLKL+TAmqBNViRzGtdp1IcHNtjR0KUJ3YmGRv+ferh?= =?us-ascii?Q?3DrxoQk1QpnSJURTfwhDQL3aBWBlvX5QY0od8HtVz4EGUrsVTieP6zdzRXNk?= =?us-ascii?Q?V5TrDolBagQuLkzLNNk93nwO/Y+SRdr068+9GCGQfWl68Or1rVE5IlD5rGeu?= =?us-ascii?Q?uAwFzbCD26tV6UEd1uhD3MgWIsxa8r0yziMzYa9fYHIHyb22ZOrtpJIFUxQs?= =?us-ascii?Q?eS0+NPyZZP74k41tArXr3TmFG8/1ZL7A+J/PozQE87gsSELG5Tcnu9xdZ/er?= =?us-ascii?Q?btKobKRSLMR0GkD58ScNv54rO018F4VUHwN38kcDJMJlR9O8scZdTMGTqcAr?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qMhpAxJMBJMgEtZ4SmqF6/RdkRn?= =?us-ascii?Q?o+69cQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: EuZ55C6AMsbmH8W9f1jWu+VWJ3b+0PqFqaHvfshQVonl/XXD0Hs17nP/SVh0JuSXcYGYUSff+kFMEuAIEMGYGxXPXGeAz6FJ8PBk3Siw5V5FsaWMAvd17CLSuBylJLCJ5aWW9M1z5Bpy4m6JQySygI25sb+qu8SL4FZDlvSkJsxfOAnjldahoTB5Js7KXQRf6KMiViBGPC/q+ZHmKLQc18ypbnW1xSRV22FJMDddCxhHP79goDcyIhjZ9HqDlFrKG7zOyhEEHibh2ANHsMeTfe16MEihW9VuLJHkKk80JqJF6l82NuoeGkpjjTeWsvPf7ryLgije7C32ef0DyF/BWjgsGa+nKFTPOp/YcUYxMkydK1p7ui/ujb6nC0mdptbnfce2lLwkYUaj3AFMkelyWbB358CGetJG8tXuQ1iUJloHv5g+PsuJqzd1b3sonb8WkzZ85y9fMZACQVDQzUlNgDKprZsRhtXGPcESZfmDzRARWPFQb5T4fgmlxmvi1KU2CBYSpcAA1OzWhLzBqdoLUCm9FfTCAzX/j6F/Hyza7qmyaojjKAE9rXuFDBr9Xv5fXjr30vbLTX9lwM0eOd4Ays3k3UGB4dwhmD88zxaUThE= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e2b3d9ab-fbcc-4cdc-9d4c-08dcf2d9e64b X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:26.7792 (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: F3NAY8d/jsBs221Kg11m0goz7qOrCtGOQISK+dA6ORobGb4TiQfiJigeLUQBoPS43BDalRYkukuR7XdX/7I4vCcuhy+0voA52jtr2drqkwc= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7437 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 mlxlogscore=999 spamscore=0 malwarescore=0 mlxscore=0 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-GUID: FfHMgnH1wUJLWAEkO_MGPTBhVvof2AfG X-Proofpoint-ORIG-GUID: FfHMgnH1wUJLWAEkO_MGPTBhVvof2AfG Content-Type: text/plain; charset="utf-8" In previous commits we effected improvements to the mmap() logic in mmap_region() and its newly introduced internal implementation function __mmap_region(). However as these changes are intended to be backported, we kept the delta as small as is possible and made as few changes as possible to the newly introduced mm/vma.* files. Take the opportunity to move this logic to mm/vma.c which not only isolates it, but also makes it available for later userland testing which can help us catch such logic errors far earlier. Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 235 --------------------------------- mm/vma.c | 324 +++++++++++++++++++++++++++++++++++++++++++++- mm/vma.h | 97 +------------- mm/vma_internal.h | 5 + 4 files changed, 330 insertions(+), 331 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 7d02b47a1895..1adf2c1cc3c0 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -577,22 +577,6 @@ SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __use= r *, arg) } #endif /* __ARCH_WANT_SYS_OLD_MMAP */ -/* - * We account for memory if it's a private writeable mapping, - * not hugepages and VM_NORESERVE wasn't set. - */ -static inline bool accountable_mapping(struct file *file, vm_flags_t vm_fl= ags) -{ - /* - * hugetlb has its own accounting separate from the core VM - * VM_HUGETLB may not be set yet so we cannot check for that flag. - */ - if (file && is_file_hugepages(file)) - return false; - - return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) =3D=3D VM_WRITE; -} - /** * unmapped_area() - Find an area between the low_limit and the high_limit= with * the correct alignment and offset, all from @info. Note: current->mm is = used @@ -1361,225 +1345,6 @@ int do_munmap(struct mm_struct *mm, unsigned long s= tart, size_t len, return do_vmi_munmap(&vmi, mm, start, len, uf, false); } -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) -{ - struct mm_struct *mm =3D current->mm; - struct vm_area_struct *vma =3D NULL; - pgoff_t pglen =3D PHYS_PFN(len); - unsigned long charged =3D 0; - struct vma_munmap_struct vms; - struct ma_state mas_detach; - struct maple_tree mt_detach; - unsigned long end =3D addr + len; - int error; - VMA_ITERATOR(vmi, mm, addr); - VMG_STATE(vmg, mm, &vmi, addr, end, vm_flags, pgoff); - - vmg.file =3D file; - /* Find the first overlapping VMA */ - vma =3D vma_find(&vmi, end); - init_vma_munmap(&vms, &vmi, vma, addr, end, uf, /* unlock =3D */ false); - if (vma) { - mt_init_flags(&mt_detach, vmi.mas.tree->ma_flags & MT_FLAGS_LOCK_MASK); - mt_on_stack(mt_detach); - mas_init(&mas_detach, &mt_detach, /* addr =3D */ 0); - /* Prepare to unmap any existing mapping in the area */ - error =3D vms_gather_munmap_vmas(&vms, &mas_detach); - if (error) - goto gather_failed; - - vmg.next =3D vms.next; - vmg.prev =3D vms.prev; - vma =3D NULL; - } else { - vmg.next =3D vma_iter_next_rewind(&vmi, &vmg.prev); - } - - /* Check against address space limit. */ - if (!may_expand_vm(mm, vm_flags, pglen - vms.nr_pages)) { - error =3D -ENOMEM; - goto abort_munmap; - } - - /* - * Private writable mapping: check memory availability - */ - if (accountable_mapping(file, vm_flags)) { - charged =3D pglen; - charged -=3D vms.nr_accounted; - if (charged) { - error =3D security_vm_enough_memory_mm(mm, charged); - if (error) - goto abort_munmap; - } - - vms.nr_accounted =3D 0; - vm_flags |=3D VM_ACCOUNT; - vmg.flags =3D vm_flags; - } - - /* - * clear PTEs while the vma is still in the tree so that rmap - * cannot race with the freeing later in the truncate scenario. - * This is also needed for mmap_file(), which is why vm_ops - * close function is called. - */ - vms_clean_up_area(&vms, &mas_detach); - vma =3D vma_merge_new_range(&vmg); - if (vma) - goto expanded; - /* - * Determine the object being mapped and call the appropriate - * specific mapper. the address has already been validated, but - * not unmapped, but the maps are removed from the list. - */ - vma =3D vm_area_alloc(mm); - if (!vma) { - error =3D -ENOMEM; - goto unacct_error; - } - - vma_iter_config(&vmi, addr, end); - vma_set_range(vma, addr, end, pgoff); - vm_flags_init(vma, vm_flags); - vma->vm_page_prot =3D vm_get_page_prot(vm_flags); - - if (vma_iter_prealloc(&vmi, vma)) { - error =3D -ENOMEM; - goto free_vma; - } - - if (file) { - vma->vm_file =3D get_file(file); - error =3D mmap_file(file, vma); - if (error) - goto unmap_and_free_file_vma; - - /* Drivers cannot alter the address of the VMA. */ - WARN_ON_ONCE(addr !=3D vma->vm_start); - /* - * 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)); - - vma_iter_config(&vmi, addr, end); - /* - * 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 && vmg.prev)) { - struct vm_area_struct *merge; - - vmg.flags =3D vma->vm_flags; - /* If this fails, state is reset ready for a reattempt. */ - merge =3D vma_merge_new_range(&vmg); - - if (merge) { - /* - * ->mmap() can change vma->vm_file and fput - * the original file. So fput the vma->vm_file - * here or we would add an extra fput for file - * and cause general protection fault - * ultimately. - */ - fput(vma->vm_file); - vm_area_free(vma); - vma_iter_free(&vmi); - vma =3D merge; - /* Update vm_flags to pick up the change. */ - vm_flags =3D vma->vm_flags; - goto file_expanded; - } - vma_iter_config(&vmi, addr, end); - } - - vm_flags =3D vma->vm_flags; - } else if (vm_flags & VM_SHARED) { - error =3D shmem_zero_setup(vma); - if (error) - goto free_iter_vma; - } else { - vma_set_anonymous(vma); - } - -#ifdef CONFIG_SPARC64 - /* TODO: Fix SPARC ADI! */ - WARN_ON_ONCE(!arch_validate_flags(vm_flags)); -#endif - - /* Lock the VMA since it is modified after insertion into VMA tree */ - vma_start_write(vma); - vma_iter_store(&vmi, vma); - mm->map_count++; - vma_link_file(vma); - - /* - * vma_merge_new_range() calls khugepaged_enter_vma() too, the below - * call covers the non-merge case. - */ - khugepaged_enter_vma(vma, vma->vm_flags); - -file_expanded: - file =3D vma->vm_file; - ksm_add_vma(vma); -expanded: - perf_event_mmap(vma); - - /* Unmap any existing mapping in the area */ - vms_complete_munmap_vmas(&vms, &mas_detach); - - vm_stat_account(mm, vm_flags, pglen); - if (vm_flags & VM_LOCKED) { - if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || - is_vm_hugetlb_page(vma) || - vma =3D=3D get_gate_vma(current->mm)) - vm_flags_clear(vma, VM_LOCKED_MASK); - else - mm->locked_vm +=3D pglen; - } - - if (file) - uprobe_mmap(vma); - - /* - * New (or expanded) vma always get soft dirty status. - * Otherwise user-space soft-dirty page tracker won't - * be able to distinguish situation when vma area unmapped, - * then new mapped in-place (which must be aimed as - * a completely new data area). - */ - vm_flags_set(vma, VM_SOFTDIRTY); - - vma_set_page_prot(vma); - - return addr; - -unmap_and_free_file_vma: - fput(vma->vm_file); - vma->vm_file =3D NULL; - - vma_iter_set(&vmi, vma->vm_end); - /* Undo any partial mapping done by a device driver. */ - unmap_region(&vmi.mas, vma, vmg.prev, vmg.next); -free_iter_vma: - vma_iter_free(&vmi); -free_vma: - vm_area_free(vma); -unacct_error: - if (charged) - vm_unacct_memory(charged); - -abort_munmap: - vms_abort_munmap_vmas(&vms, &mas_detach); -gather_failed: - return error; -} - unsigned long mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) diff --git a/mm/vma.c b/mm/vma.c index bb7cfa2dc282..7617f9d50d62 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -1103,7 +1103,7 @@ static inline void vms_clear_ptes(struct vma_munmap_s= truct *vms, vms->clear_ptes =3D false; } -void vms_clean_up_area(struct vma_munmap_struct *vms, +static void vms_clean_up_area(struct vma_munmap_struct *vms, struct ma_state *mas_detach) { struct vm_area_struct *vma; @@ -1126,7 +1126,7 @@ void vms_clean_up_area(struct vma_munmap_struct *vms, * used for the munmap() and may downgrade the lock - if requested. Every= thing * needed to be done once the vma maple tree is updated. */ -void vms_complete_munmap_vmas(struct vma_munmap_struct *vms, +static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms, struct ma_state *mas_detach) { struct vm_area_struct *vma; @@ -1167,6 +1167,23 @@ void vms_complete_munmap_vmas(struct vma_munmap_stru= ct *vms, __mt_destroy(mas_detach->tree); } +/* + * reattach_vmas() - Undo any munmap work and free resources + * @mas_detach: The maple state with the detached maple tree + * + * Reattach any detached vmas and free up the maple tree used to track the= vmas. + */ +static void reattach_vmas(struct ma_state *mas_detach) +{ + struct vm_area_struct *vma; + + mas_set(mas_detach, 0); + mas_for_each(mas_detach, vma, ULONG_MAX) + vma_mark_detached(vma, false); + + __mt_destroy(mas_detach->tree); +} + /* * vms_gather_munmap_vmas() - Put all VMAs within a range into a maple tree * for removal at a later date. Handles splitting first and last if neces= sary @@ -1177,7 +1194,7 @@ void vms_complete_munmap_vmas(struct vma_munmap_struc= t *vms, * * Return: 0 on success, error otherwise */ -int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, +static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, struct ma_state *mas_detach) { struct vm_area_struct *next =3D NULL; @@ -1315,6 +1332,39 @@ int vms_gather_munmap_vmas(struct vma_munmap_struct = *vms, return error; } +/* + * init_vma_munmap() - Initializer wrapper for vma_munmap_struct + * @vms: The vma munmap struct + * @vmi: The vma iterator + * @vma: The first vm_area_struct to munmap + * @start: The aligned start address to munmap + * @end: The aligned end address to munmap + * @uf: The userfaultfd list_head + * @unlock: Unlock after the operation. Only unlocked on success + */ +static void init_vma_munmap(struct vma_munmap_struct *vms, + struct vma_iterator *vmi, struct vm_area_struct *vma, + unsigned long start, unsigned long end, struct list_head *uf, + bool unlock) +{ + vms->vmi =3D vmi; + vms->vma =3D vma; + if (vma) { + vms->start =3D start; + vms->end =3D end; + } else { + vms->start =3D vms->end =3D 0; + } + vms->unlock =3D unlock; + vms->uf =3D uf; + vms->vma_count =3D 0; + vms->nr_pages =3D vms->locked_vm =3D vms->nr_accounted =3D 0; + vms->exec_vm =3D vms->stack_vm =3D vms->data_vm =3D 0; + vms->unmap_start =3D FIRST_USER_ADDRESS; + vms->unmap_end =3D USER_PGTABLES_CEILING; + vms->clear_ptes =3D false; +} + /* * do_vmi_align_munmap() - munmap the aligned region from @start to @end. * @vmi: The vma iterator @@ -2069,3 +2119,271 @@ void mm_drop_all_locks(struct mm_struct *mm) mutex_unlock(&mm_all_locks_mutex); } + +/* + * We account for memory if it's a private writeable mapping, + * not hugepages and VM_NORESERVE wasn't set. + */ +static bool accountable_mapping(struct file *file, vm_flags_t vm_flags) +{ + /* + * hugetlb has its own accounting separate from the core VM + * VM_HUGETLB may not be set yet so we cannot check for that flag. + */ + if (file && is_file_hugepages(file)) + return false; + + return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) =3D=3D VM_WRITE; +} + +/* + * vms_abort_munmap_vmas() - Undo as much as possible from an aborted munm= ap() + * operation. + * @vms: The vma unmap structure + * @mas_detach: The maple state with the detached maple tree + * + * Reattach any detached vmas, free up the maple tree used to track the vm= as. + * If that's not possible because the ptes are cleared (and vm_ops->closed= () may + * have been called), then a NULL is written over the vmas and the vmas are + * removed (munmap() completed). + */ +static void vms_abort_munmap_vmas(struct vma_munmap_struct *vms, + struct ma_state *mas_detach) +{ + struct ma_state *mas =3D &vms->vmi->mas; + + if (!vms->nr_pages) + return; + + if (vms->clear_ptes) + return reattach_vmas(mas_detach); + + /* + * Aborting cannot just call the vm_ops open() because they are often + * not symmetrical and state data has been lost. Resort to the old + * failure method of leaving a gap where the MAP_FIXED mapping failed. + */ + mas_set_range(mas, vms->start, vms->end - 1); + mas_store_gfp(mas, NULL, GFP_KERNEL|__GFP_NOFAIL); + /* Clean up the insertion of the unfortunate gap */ + vms_complete_munmap_vmas(vms, mas_detach); +} + +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) +{ + struct mm_struct *mm =3D current->mm; + struct vm_area_struct *vma =3D NULL; + pgoff_t pglen =3D PHYS_PFN(len); + unsigned long charged =3D 0; + struct vma_munmap_struct vms; + struct ma_state mas_detach; + struct maple_tree mt_detach; + unsigned long end =3D addr + len; + int error; + VMA_ITERATOR(vmi, mm, addr); + VMG_STATE(vmg, mm, &vmi, addr, end, vm_flags, pgoff); + + vmg.file =3D file; + /* Find the first overlapping VMA */ + vma =3D vma_find(&vmi, end); + init_vma_munmap(&vms, &vmi, vma, addr, end, uf, /* unlock =3D */ false); + if (vma) { + mt_init_flags(&mt_detach, vmi.mas.tree->ma_flags & MT_FLAGS_LOCK_MASK); + mt_on_stack(mt_detach); + mas_init(&mas_detach, &mt_detach, /* addr =3D */ 0); + /* Prepare to unmap any existing mapping in the area */ + error =3D vms_gather_munmap_vmas(&vms, &mas_detach); + if (error) + goto gather_failed; + + vmg.next =3D vms.next; + vmg.prev =3D vms.prev; + vma =3D NULL; + } else { + vmg.next =3D vma_iter_next_rewind(&vmi, &vmg.prev); + } + + /* Check against address space limit. */ + if (!may_expand_vm(mm, vm_flags, pglen - vms.nr_pages)) { + error =3D -ENOMEM; + goto abort_munmap; + } + + /* + * Private writable mapping: check memory availability + */ + if (accountable_mapping(file, vm_flags)) { + charged =3D pglen; + charged -=3D vms.nr_accounted; + if (charged) { + error =3D security_vm_enough_memory_mm(mm, charged); + if (error) + goto abort_munmap; + } + + vms.nr_accounted =3D 0; + vm_flags |=3D VM_ACCOUNT; + vmg.flags =3D vm_flags; + } + + /* + * clear PTEs while the vma is still in the tree so that rmap + * cannot race with the freeing later in the truncate scenario. + * This is also needed for mmap_file(), which is why vm_ops + * close function is called. + */ + vms_clean_up_area(&vms, &mas_detach); + vma =3D vma_merge_new_range(&vmg); + if (vma) + goto expanded; + /* + * Determine the object being mapped and call the appropriate + * specific mapper. the address has already been validated, but + * not unmapped, but the maps are removed from the list. + */ + vma =3D vm_area_alloc(mm); + if (!vma) { + error =3D -ENOMEM; + goto unacct_error; + } + + vma_iter_config(&vmi, addr, end); + vma_set_range(vma, addr, end, pgoff); + vm_flags_init(vma, vm_flags); + vma->vm_page_prot =3D vm_get_page_prot(vm_flags); + + if (vma_iter_prealloc(&vmi, vma)) { + error =3D -ENOMEM; + goto free_vma; + } + + if (file) { + vma->vm_file =3D get_file(file); + error =3D mmap_file(file, vma); + if (error) + goto unmap_and_free_file_vma; + + /* Drivers cannot alter the address of the VMA. */ + WARN_ON_ONCE(addr !=3D vma->vm_start); + /* + * 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)); + + vma_iter_config(&vmi, addr, end); + /* + * 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 && vmg.prev)) { + struct vm_area_struct *merge; + + vmg.flags =3D vma->vm_flags; + /* If this fails, state is reset ready for a reattempt. */ + merge =3D vma_merge_new_range(&vmg); + + if (merge) { + /* + * ->mmap() can change vma->vm_file and fput + * the original file. So fput the vma->vm_file + * here or we would add an extra fput for file + * and cause general protection fault + * ultimately. + */ + fput(vma->vm_file); + vm_area_free(vma); + vma_iter_free(&vmi); + vma =3D merge; + /* Update vm_flags to pick up the change. */ + vm_flags =3D vma->vm_flags; + goto file_expanded; + } + vma_iter_config(&vmi, addr, end); + } + + vm_flags =3D vma->vm_flags; + } else if (vm_flags & VM_SHARED) { + error =3D shmem_zero_setup(vma); + if (error) + goto free_iter_vma; + } else { + vma_set_anonymous(vma); + } + +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif + + /* Lock the VMA since it is modified after insertion into VMA tree */ + vma_start_write(vma); + vma_iter_store(&vmi, vma); + mm->map_count++; + vma_link_file(vma); + + /* + * vma_merge_new_range() calls khugepaged_enter_vma() too, the below + * call covers the non-merge case. + */ + khugepaged_enter_vma(vma, vma->vm_flags); + +file_expanded: + file =3D vma->vm_file; + ksm_add_vma(vma); +expanded: + perf_event_mmap(vma); + + /* Unmap any existing mapping in the area */ + vms_complete_munmap_vmas(&vms, &mas_detach); + + vm_stat_account(mm, vm_flags, pglen); + if (vm_flags & VM_LOCKED) { + if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || + is_vm_hugetlb_page(vma) || + vma =3D=3D get_gate_vma(current->mm)) + vm_flags_clear(vma, VM_LOCKED_MASK); + else + mm->locked_vm +=3D pglen; + } + + if (file) + uprobe_mmap(vma); + + /* + * New (or expanded) vma always get soft dirty status. + * Otherwise user-space soft-dirty page tracker won't + * be able to distinguish situation when vma area unmapped, + * then new mapped in-place (which must be aimed as + * a completely new data area). + */ + vm_flags_set(vma, VM_SOFTDIRTY); + + vma_set_page_prot(vma); + + return addr; + +unmap_and_free_file_vma: + fput(vma->vm_file); + vma->vm_file =3D NULL; + + vma_iter_set(&vmi, vma->vm_end); + /* Undo any partial mapping done by a device driver. */ + unmap_region(&vmi.mas, vma, vmg.prev, vmg.next); +free_iter_vma: + vma_iter_free(&vmi); +free_vma: + vm_area_free(vma); +unacct_error: + if (charged) + vm_unacct_memory(charged); + +abort_munmap: + vms_abort_munmap_vmas(&vms, &mas_detach); +gather_failed: + return error; +} diff --git a/mm/vma.h b/mm/vma.h index 75558b5e9c8c..eb15616e4e2c 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -165,99 +165,6 @@ static inline int vma_iter_store_gfp(struct vma_iterat= or *vmi, return 0; } -#ifdef CONFIG_MMU -/* - * init_vma_munmap() - Initializer wrapper for vma_munmap_struct - * @vms: The vma munmap struct - * @vmi: The vma iterator - * @vma: The first vm_area_struct to munmap - * @start: The aligned start address to munmap - * @end: The aligned end address to munmap - * @uf: The userfaultfd list_head - * @unlock: Unlock after the operation. Only unlocked on success - */ -static inline void init_vma_munmap(struct vma_munmap_struct *vms, - struct vma_iterator *vmi, struct vm_area_struct *vma, - unsigned long start, unsigned long end, struct list_head *uf, - bool unlock) -{ - vms->vmi =3D vmi; - vms->vma =3D vma; - if (vma) { - vms->start =3D start; - vms->end =3D end; - } else { - vms->start =3D vms->end =3D 0; - } - vms->unlock =3D unlock; - vms->uf =3D uf; - vms->vma_count =3D 0; - vms->nr_pages =3D vms->locked_vm =3D vms->nr_accounted =3D 0; - vms->exec_vm =3D vms->stack_vm =3D vms->data_vm =3D 0; - vms->unmap_start =3D FIRST_USER_ADDRESS; - vms->unmap_end =3D USER_PGTABLES_CEILING; - vms->clear_ptes =3D false; -} -#endif - -int vms_gather_munmap_vmas(struct vma_munmap_struct *vms, - struct ma_state *mas_detach); - -void vms_complete_munmap_vmas(struct vma_munmap_struct *vms, - struct ma_state *mas_detach); - -void vms_clean_up_area(struct vma_munmap_struct *vms, - struct ma_state *mas_detach); - -/* - * reattach_vmas() - Undo any munmap work and free resources - * @mas_detach: The maple state with the detached maple tree - * - * Reattach any detached vmas and free up the maple tree used to track the= vmas. - */ -static inline void reattach_vmas(struct ma_state *mas_detach) -{ - struct vm_area_struct *vma; - - mas_set(mas_detach, 0); - mas_for_each(mas_detach, vma, ULONG_MAX) - vma_mark_detached(vma, false); - - __mt_destroy(mas_detach->tree); -} - -/* - * vms_abort_munmap_vmas() - Undo as much as possible from an aborted munm= ap() - * operation. - * @vms: The vma unmap structure - * @mas_detach: The maple state with the detached maple tree - * - * Reattach any detached vmas, free up the maple tree used to track the vm= as. - * If that's not possible because the ptes are cleared (and vm_ops->closed= () may - * have been called), then a NULL is written over the vmas and the vmas are - * removed (munmap() completed). - */ -static inline void vms_abort_munmap_vmas(struct vma_munmap_struct *vms, - struct ma_state *mas_detach) -{ - struct ma_state *mas =3D &vms->vmi->mas; - if (!vms->nr_pages) - return; - - if (vms->clear_ptes) - return reattach_vmas(mas_detach); - - /* - * Aborting cannot just call the vm_ops open() because they are often - * not symmetrical and state data has been lost. Resort to the old - * failure method of leaving a gap where the MAP_FIXED mapping failed. - */ - mas_set_range(mas, vms->start, vms->end - 1); - mas_store_gfp(mas, NULL, GFP_KERNEL|__GFP_NOFAIL); - /* Clean up the insertion of the unfortunate gap */ - vms_complete_munmap_vmas(vms, mas_detach); -} - int do_vmi_align_munmap(struct vma_iterator *vmi, struct vm_area_struct *vma, struct mm_struct *mm, unsigned long start, @@ -336,6 +243,10 @@ bool vma_wants_writenotify(struct vm_area_struct *vma,= pgprot_t vm_page_prot); int mm_take_all_locks(struct mm_struct *mm); void mm_drop_all_locks(struct mm_struct *mm); +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); + static inline bool vma_wants_manual_pte_write_upgrade(struct vm_area_struc= t *vma) { /* diff --git a/mm/vma_internal.h b/mm/vma_internal.h index b930ab12a587..fc5f172a36bd 100644 --- a/mm/vma_internal.h +++ b/mm/vma_internal.h @@ -17,8 +17,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -32,11 +34,14 @@ #include #include #include +#include #include #include #include #include #include +#include +#include #include #include #include -- 2.47.0 From nobody Tue Nov 26 00:29:37 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 8651D1CDA27 for ; Tue, 22 Oct 2024 20:41:43 +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=1729629705; cv=fail; b=Wg4GYbSGowXk/aZCUOnN0j+6ozLiV/4NrYWri0oqM1T14X8i+o22EKREbmTtzjDZiszr9boDzuKmoEFZEFt59g5dQElMMRigziCibnk/NFNbQ7QIM4sOtY9Rw8iHFa8nPZ/rTZhdDjKcPseujvO/EvqmKhqBqYRjxVF1Mcr2BR8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629705; c=relaxed/simple; bh=u2xeLpH8MJekB5QclwF9w+6Ou4rLxnbFl2vKYOhoKR0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=j6Up4yKLO08znjUF6LGJoXubHO3cxq+9E1K1rwSW1Ek+c1u1LWc3fq+2LBb+wZ4CwNMy4Q0HKv1n0GWAnZqcTqVmPwwUEJuU4reINqh/IY2vioYYm5j9vBWvWu8G21mG0+9z8I9WEzfIwv1mSaDWcb0gmWGZcQktWqh8NeTcx7s= 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=SkM/NLbI; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=c4qV4smk; 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="SkM/NLbI"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="c4qV4smk" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49MK3Xbs017462; Tue, 22 Oct 2024 20:41:34 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=BXayzKW+JRbaXCboa+yyboq8km8gURUeWkwUdDs/fdk=; b= SkM/NLbIKZvJZytfp8LZxqN7GLkP/FhhOq5fAB87fothJ2oYfI5PeG+fUySpOcn+ thOQD4snAlfIgqie7O4qw5xPTF8LBsFMD6SeFznxRc1MVmfxDS71cqN29dBH/Pxj 60Z0AGRD4xheX94Pa+PJev+JNd1Fad9EXEhixB0sEQL6aN5EGVg2CUttGlI9XFGZ d1Z8iOO8NwDLfRAwUgLEGzF5ThbU6gLzD0pbkAIRFEhZK3PRRH6I1kaeyfzA9Zu9 p8HCC+GCPlWDYPcOHiXQRqiA++ZYXmtxeuCwbPIMZar1LY24BT0RHo3GqbypjZwn 9SfUjcLZj3fZsrNz5HUxwg== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42ckkqwy2h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:34 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49MKSeQk026202; Tue, 22 Oct 2024 20:41:33 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2174.outbound.protection.outlook.com [104.47.58.174]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42c3786y7d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gY6QxKPXFX8E+lrcmGp3tDK23TrNE/otmDQOKyLY552yvz9teqtWdUvbcdwVDNHKk/dvkYo2qhoxXOSk9QCLK6/UnYRG9RIiB8Kv+JGBNWbo4F7OsVhzUlsDAKUf4DOKKFsnX3+VSGcaLAYG1gYQdxWV6Y7TptKmHhgo/BUcIyqsaKXYoBgwQATGR8IuWBlTVK6w2gXTYWSw+n8SfjDQ4sSmYKv2EZCzyW2uP/jWyekrlcxm/PX7egWeikzV/zE3PAFcf1zKi0dDt61lzHteXBGaAZ64pvngBy4YoE0YWmrQI3LoYAClzcuZFxh2fUlZ8jihy4JtU4jXwGxuHHsQrA== 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=BXayzKW+JRbaXCboa+yyboq8km8gURUeWkwUdDs/fdk=; b=a+BcHNbMHHUL6mXiWIs1Srm/FdA+7FWvdjElPdxuNHA3TziSjzKrN3Fa4cfbtyKv2jCuL3bJLn+GEdvZmAnd8pzqvIGXfEEL/FPzhaw0NZORGqQyOS3+woa2CI2MePli16ifHCiXZs5k7TBfb02D/rakq+8KDnung1xWoIkPvW+mKA4iLzaVBKZ0EP4L96avAojJG4e1FOkuaipOGqZyKp9OSnYJoTFXryzOn/uQwT54tebmuk/IOAtQcgjpacUKblIyPVbmkk7+Z+J6g07YjswW7V/0nwtV97eAX8ixDVghdKaaGnFl9a2zbrsnQzs0P8ulgJu243XJ1mFoHspUHw== 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=BXayzKW+JRbaXCboa+yyboq8km8gURUeWkwUdDs/fdk=; b=c4qV4smkZY5UjJ1Dq6ke3R+GTmzDxI2g9LVdCNqmCATFiodiyP1kRb+dlcKdv8CJZPZ/BfIKYq66bjDTIkOkGdBvN20rPfUlFjevOy9kAvlTriHNSlDD6apPkvJoXPOW9VfqrTteAYM0Dn3ZRpcXWtPwLFe2s9oR+2Unz2hfMN0= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH0PR10MB7437.namprd10.prod.outlook.com (2603:10b6:610:193::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 20:41:30 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:30 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 7/8] mm: refactor __mmap_region() Date: Tue, 22 Oct 2024 21:40:58 +0100 Message-ID: <125b6ebddc7ae8790b8b10b47906c2d39e68f3d9.1729628198.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO2P265CA0057.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:60::21) 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_|CH0PR10MB7437:EE_ X-MS-Office365-Filtering-Correlation-Id: 7094f31e-58b4-4183-2383-08dcf2d9e890 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?obceIAltgyCD31yP6ZP/386kh/meg4pu082wVpMLK4a+nvzWl2jwZIo1OT4h?= =?us-ascii?Q?oPGLbA5jntQfX3IXhAUa8ujKvWDY6O4WmvzSXspoILLXguAeheKx3U9HgimM?= =?us-ascii?Q?N2OK1PcsSpaGVOjIix/gL2f5ML/A6Poi/LqNre6lzpbnowEdq89l99R6lvl9?= =?us-ascii?Q?0l4mu4JQ9lfk1k/pd5ba6MJ1mYKi/6C1zeA3mwcvsxcvx1ylTwoERY4Ciqez?= =?us-ascii?Q?XBuyV4IrWYOqgr2uVikHeRpN2ryCslmxK7jpufgIcNVHCAr/p8XE8840eTej?= =?us-ascii?Q?l/sHIU+lI5pcn8CBhz8YsZlXV8NO/35U6raV1/eP4LD2HfgE4mn4tZw/Pe0i?= =?us-ascii?Q?B1jykqqlAgS/gTUhNaGzZ5kjRSNmVzlhjWvZQMEPdGUe6Lx1agekykgf0nfU?= =?us-ascii?Q?yRGUw2ahRbTI75BZak8U98OK/jAUeYOD3TxKF05MTeP++Ji70WHHWqQsu217?= =?us-ascii?Q?kejwidQCEirwqRUs4okbHM5UUjhMhvueoRN+IcABwKJMHp2pKD7/8L+vapzn?= =?us-ascii?Q?mcKe6pNqUJGH309iEJKBD6exu53wAK3yBoYptvrXFo/gEhHlS/1pJhIEAl4h?= =?us-ascii?Q?Eb+kGC/8Z5HFE5C+Z9/+e7E5UD70OfU+lqdT1C6o5jASusErAfCENKyKTIOd?= =?us-ascii?Q?Lp4z120EE+7SWCC7DCTPlmr23L3rTz7fHvMe8FCDq8IIR0p+qPLORuKtsSCP?= =?us-ascii?Q?TnSIPiHMGsLrWKculmUetTt4Rkw2+U/dX/JI+PrUdSQdfFQ/Y9cutCo5wg0u?= =?us-ascii?Q?s1mS9/L0yp7zy+uv36g/6JXJn3AP8dbNG2smFw5R5FQcdE30DGr8FK5XOFlc?= =?us-ascii?Q?SbbbEhKJi6ruTbHZhOkKBk3xzLQx+gXPkUV9v/aSxsV4lehcy808cRsGhk7T?= =?us-ascii?Q?7fSzSFnXKznVrCUh6TFIslUM6gf0/K93iukxQfYL4H6Js4YqqtMYNETEzuHS?= =?us-ascii?Q?K12eKjAm3qDQV08K6KRMgM0WP0e4s9O8tlbSX1fnG8e4CzbyZnWLmUBcuw/Q?= =?us-ascii?Q?Jhw/y3zR6WP3vhwgVzJjbp60EEKFCPlXqeCEDsFzEQnIWdw+o6qzGmh1eHsp?= =?us-ascii?Q?UbN4tPhYckRvk73Ot9OTm2YpPvMB4x5F07oOV67hrHoo8ECZmmNwM+0eZs38?= =?us-ascii?Q?PTHAsP+ZwjYjPlBZcFq/1orQx0wiCvH7s5dTIVcPwb9FzM2oKG/vaH84knD6?= =?us-ascii?Q?vGyqmMmG5yzDVgRSeq4HLtLjdhIVNBdELEj47p+isxhpOn/QBRqDzY5TQXBu?= =?us-ascii?Q?F/GY7POcXPaTRwJy4JpT6H5T/FsLa0enz835I1yssIuD/W6+TehSA2fpB1o+?= =?us-ascii?Q?8hG1gS6ubh74tDp6ez4+8Awm?= 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)(10070799003)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?GHxcu/iUi/weqrkN44zOKWZcQJHOVKLFhAFChx597TwgqMq4Qh3Fb6nB02aU?= =?us-ascii?Q?pxedbbcfSbXHBb6Kr/yYLxN0kvMmRGuGxdSvt3V2AN+loWTorVlksQLsV8l6?= =?us-ascii?Q?+aGYYCRqYt/Za7VRYmEnq+yG8XCt9jhuaJKv9UItMzZodRS3b4ovmUtJQebc?= =?us-ascii?Q?TtkKkbaREFc72E/HTjPRB8kUM45M9UghRAIwCSLlBTUv7aodEW6yMLNnvfWf?= =?us-ascii?Q?PCoqFACQuFHeq/shN+qFUspskTyb16veeAq3ihJkkqMNEoMeDIywEIR5Khvr?= =?us-ascii?Q?z4el9/fuYjoEyplrtn3fct+ONzg36145GIclvGdgVp/3WZ0H+9452Xhr/oVB?= =?us-ascii?Q?KrWwTvoJ2BhwnmzY29VdBxrcSM0Wbs3NxScax6TjtIN75QLR+pDzTdeHkCVL?= =?us-ascii?Q?s1U6M20CitXrYB0EILc/R8jMlGrx2OxmGUJiMsprDukC21NotSkHLp01xTt2?= =?us-ascii?Q?YxLf8ydS8CsZ1PhLHk85Ilc4aha7va89HiBdYA6j6jk+tcM7TtM5L1aaXhXZ?= =?us-ascii?Q?IPpjl5jFM5eV3za6yRmqf8uPc9YH3ttbRQyrCgBXSx/XAyPzKB0JWVbOPoF/?= =?us-ascii?Q?/N+lsU7i9NxJodKRx3PFBUW9zXev2QTCAAhVcW+9YGeJhvTbn51xKFFbalSH?= =?us-ascii?Q?D1hQHydg13g+Ru0KKeChnQsV4nRE+k/VOMP8fdjOxkRCLdvMDzxvuqA2UWN6?= =?us-ascii?Q?o0yGGhy4nZKvFztxvEE6oTOdkgbhshLE44bazvXMV9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W7hlBjFLCNomu1tNWtah6CofdjoZaqDTBzx?= =?us-ascii?Q?b7hhTQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: fe45ZWKllBpGCqnKO/oAS9y4LbwLgSu6Flo2R7oj1K4ystQRC8UVVbRHzngTSNZaSwgjuzk/GXG4cnYvbsOjfFIMYlKstzmL19z0mkUOcuIbvc243gQkc4HBPoSkYWFz2CK6YxjLDQVLiTP/5PWyaZdOME9GF1r9lm9dmDD/Xy5WOKT8k2fN1UfnMb6EOuiLQzzuaWOIf1xsLFUh63Hi2xWN7iiHv1I/p03qfW492no3y8EyHJv+wOjFKXSP7EIiLXo6H9LHMXdWGQFJuM8gNGOdPEqkyuIcew9N+bUXiPDcytvAJ3RAAa6vog8NNFiJ+jTck44/L4HnmxCNgMewdCIWVrEkaZKeoktu8wTmGROMycHf/VLO+1Nd7Js7vK0vWmDUJnC8DnsuInjxFG0zh0Ps0d69wkLa3aw59uniwbatw+QbkSRCOT/+1tCPdlmqZpS2ME5I6CiW+t+knaN1po3TKEsqlvRJC+GFE3g6moS1pGWFD04wWfIvnVnxGFu/pJi1nFX34A4r9LpyMZK4GrVySMNMO371ze3Ok2EUtDAMYUWfJv9ZHcZMcnkhEnMsCCwhehTVF8nA9KEvKOem9Nl4L0e6gl+gTDXLmJF+r0c= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7094f31e-58b4-4183-2383-08dcf2d9e890 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:30.5614 (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: 7doej8ELU+6U+B8YxRsSOKY/LqCp26xmSGq+RjA4+2NlwMK0X0DnyjD/n8o+UjlxEiIj7wxItfcJpEToFQ4gq9qZR4j99c7BRbJQoDOr2Go= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7437 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 suspectscore=0 spamscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-GUID: XNdwUTDDwNH2hKNDT-2pUgCrDl7KjbfC X-Proofpoint-ORIG-GUID: XNdwUTDDwNH2hKNDT-2pUgCrDl7KjbfC Content-Type: text/plain; charset="utf-8" We have seen bugs and resource leaks arise from the complexity of the __mmap_region() function. This, and the generally deeply fragile error handling logic and complexity which makes understanding the function difficult make it highly desirable to refactor it into something readable. Achieve this by separating the function into smaller logical parts which are easier to understand and follow, and which importantly very significantly simplify the error handling. Note that we now call vms_abort_munmap_vmas() in more error paths than we used to, however in cases where no abort need occur, vms->nr_pages will be equal to zero and we simply exit this function without doing more than we would have done previously. Importantly, the invocation of the driver mmap hook via mmap_file() now has very simple and obvious handling (this was previously the most problematic part of the mmap() operation). Signed-off-by: Lorenzo Stoakes --- mm/vma.c | 380 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 240 insertions(+), 140 deletions(-) diff --git a/mm/vma.c b/mm/vma.c index 7617f9d50d62..a271e2b406ab 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -7,6 +7,31 @@ #include "vma_internal.h" #include "vma.h" +struct mmap_state { + struct mm_struct *mm; + struct vma_iterator *vmi; + struct vma_merge_struct *vmg; + struct list_head *uf; + + struct vma_munmap_struct vms; + struct ma_state mas_detach; + struct maple_tree mt_detach; + + unsigned long flags; + unsigned long pglen; + unsigned long charged; +}; + +#define MMAP_STATE(name, mm_, vmi_, vmg_, uf_, flags_, len_) \ + struct mmap_state name =3D { \ + .mm =3D mm_, \ + .vmi =3D vmi_, \ + .vmg =3D vmg_, \ + .uf =3D uf_, \ + .flags =3D flags_, \ + .pglen =3D PHYS_PFN(len_), \ + } + static inline bool is_mergeable_vma(struct vma_merge_struct *vmg, bool mer= ge_next) { struct vm_area_struct *vma =3D merge_next ? vmg->next : vmg->prev; @@ -2169,189 +2194,247 @@ static void vms_abort_munmap_vmas(struct vma_munm= ap_struct *vms, vms_complete_munmap_vmas(vms, mas_detach); } -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) +/* + * __mmap_prepare() - Prepare to gather any overlapping VMAs that need to = be + * unmapped once the map operation is completed, check = limits, + * account mapping and clean up any pre-existing VMAs. + * + * @map: Mapping state. + * + * Returns: 0 on success, error code otherwise. + */ +static int __mmap_prepare(struct mmap_state *map) { - struct mm_struct *mm =3D current->mm; - struct vm_area_struct *vma =3D NULL; - pgoff_t pglen =3D PHYS_PFN(len); - unsigned long charged =3D 0; - struct vma_munmap_struct vms; - struct ma_state mas_detach; - struct maple_tree mt_detach; - unsigned long end =3D addr + len; int error; - VMA_ITERATOR(vmi, mm, addr); - VMG_STATE(vmg, mm, &vmi, addr, end, vm_flags, pgoff); - - vmg.file =3D file; - /* Find the first overlapping VMA */ - vma =3D vma_find(&vmi, end); - init_vma_munmap(&vms, &vmi, vma, addr, end, uf, /* unlock =3D */ false); - if (vma) { - mt_init_flags(&mt_detach, vmi.mas.tree->ma_flags & MT_FLAGS_LOCK_MASK); - mt_on_stack(mt_detach); - mas_init(&mas_detach, &mt_detach, /* addr =3D */ 0); + struct vma_iterator *vmi =3D map->vmi; + struct vma_merge_struct *vmg =3D map->vmg; + struct vma_munmap_struct *vms =3D &map->vms; + + /* Find the first overlapping VMA and initialise unmap state. */ + vms->vma =3D vma_find(vmi, vmg->end); + init_vma_munmap(vms, vmi, vms->vma, vmg->start, vmg->end, map->uf, + /* unlock =3D */ false); + + /* OK, we have overlapping VMAs - prepare to unmap them. */ + if (vms->vma) { + mt_init_flags(&map->mt_detach, vmi->mas.tree->ma_flags & MT_FLAGS_LOCK_M= ASK); + mt_on_stack(map->mt_detach); + mas_init(&map->mas_detach, &map->mt_detach, /* addr =3D */ 0); /* Prepare to unmap any existing mapping in the area */ - error =3D vms_gather_munmap_vmas(&vms, &mas_detach); + error =3D vms_gather_munmap_vmas(vms, &map->mas_detach); if (error) - goto gather_failed; + return error; - vmg.next =3D vms.next; - vmg.prev =3D vms.prev; - vma =3D NULL; + vmg->next =3D vms->next; + vmg->prev =3D vms->prev; } else { - vmg.next =3D vma_iter_next_rewind(&vmi, &vmg.prev); + vmg->next =3D vma_iter_next_rewind(vmi, &vmg->prev); } /* Check against address space limit. */ - if (!may_expand_vm(mm, vm_flags, pglen - vms.nr_pages)) { - error =3D -ENOMEM; - goto abort_munmap; - } + if (!may_expand_vm(map->mm, map->flags, map->pglen - vms->nr_pages)) + return -ENOMEM; - /* - * Private writable mapping: check memory availability - */ - if (accountable_mapping(file, vm_flags)) { - charged =3D pglen; - charged -=3D vms.nr_accounted; - if (charged) { - error =3D security_vm_enough_memory_mm(mm, charged); + /* Private writable mapping: check memory availability. */ + if (accountable_mapping(vmg->file, map->flags)) { + map->charged =3D map->pglen; + map->charged -=3D vms->nr_accounted; + if (map->charged) { + error =3D security_vm_enough_memory_mm(map->mm, map->charged); if (error) - goto abort_munmap; + return error; } - vms.nr_accounted =3D 0; - vm_flags |=3D VM_ACCOUNT; - vmg.flags =3D vm_flags; + vms->nr_accounted =3D 0; + map->flags |=3D VM_ACCOUNT; } /* - * clear PTEs while the vma is still in the tree so that rmap + * Clear PTEs while the vma is still in the tree so that rmap * cannot race with the freeing later in the truncate scenario. * This is also needed for mmap_file(), which is why vm_ops * close function is called. */ - vms_clean_up_area(&vms, &mas_detach); - vma =3D vma_merge_new_range(&vmg); - if (vma) - goto expanded; + vms_clean_up_area(vms, &map->mas_detach); + + return 0; +} + +static int __mmap_new_file_vma(struct mmap_state *map, struct vm_area_stru= ct *vma, + struct vm_area_struct **mergep) +{ + struct vma_iterator *vmi =3D map->vmi; + struct vma_merge_struct *vmg =3D map->vmg; + int error; + + vma->vm_file =3D get_file(vmg->file); + error =3D mmap_file(vma->vm_file, vma); + if (error) { + fput(vma->vm_file); + vma->vm_file =3D NULL; + + vma_iter_set(vmi, vma->vm_end); + /* Undo any partial mapping done by a device driver. */ + unmap_region(&vmi->mas, vma, vmg->prev, vmg->next); + + return error; + } + + /* Drivers cannot alter the address of the VMA. */ + WARN_ON_ONCE(vmg->start !=3D vma->vm_start); + /* + * Drivers should not permit writability when previously it was + * disallowed. + */ + VM_WARN_ON_ONCE(map->flags !=3D vma->vm_flags && + !(map->flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + + vma_iter_config(vmi, vmg->start, vmg->end); + /* + * If flags changed after mmap_file(), we should try merge + * vma again as we may succeed this time. + */ + if (unlikely(map->flags !=3D vma->vm_flags && vmg->prev)) { + struct vm_area_struct *merge; + + vmg->flags =3D vma->vm_flags; + /* If this fails, state is reset ready for a reattempt. */ + merge =3D vma_merge_new_range(vmg); + + if (merge) { + /* + * ->mmap() can change vma->vm_file and fput + * the original file. So fput the vma->vm_file + * here or we would add an extra fput for file + * and cause general protection fault + * ultimately. + */ + fput(vma->vm_file); + vm_area_free(vma); + vma_iter_free(vmi); + *mergep =3D merge; + } else { + vma_iter_config(vmi, vmg->start, vmg->end); + } + } + + map->flags =3D vma->vm_flags; + return 0; +} + +/* + * __mmap_new_vma() - Allocate a new VMA for the region, as merging was not + * possible. + * + * An exception to this is if the mapping is file-backe= d, and + * the underlying driver changes the VMA flags, permitt= ing a + * subsequent merge of the VMA, in which case the retur= ned + * VMA is one that was merged on a second attempt. + * + * @map: Mapping state. + * @vmap: Output pointer for the new VMA. + * + * Returns: Zero on success, or an error. + */ +static int __mmap_new_vma(struct mmap_state *map, struct vm_area_struct **= vmap) +{ + struct vma_iterator *vmi =3D map->vmi; + struct vma_merge_struct *vmg =3D map->vmg; + struct vm_area_struct *merge =3D NULL; + int error =3D 0; + struct vm_area_struct *vma; + /* * Determine the object being mapped and call the appropriate * specific mapper. the address has already been validated, but * not unmapped, but the maps are removed from the list. */ - vma =3D vm_area_alloc(mm); - if (!vma) { - error =3D -ENOMEM; - goto unacct_error; - } + vma =3D vm_area_alloc(map->mm); + if (!vma) + return -ENOMEM; - vma_iter_config(&vmi, addr, end); - vma_set_range(vma, addr, end, pgoff); - vm_flags_init(vma, vm_flags); - vma->vm_page_prot =3D vm_get_page_prot(vm_flags); + vma_iter_config(vmi, vmg->start, vmg->end); + vma_set_range(vma, vmg->start, vmg->end, vmg->pgoff); + vm_flags_init(vma, map->flags); + vma->vm_page_prot =3D vm_get_page_prot(map->flags); - if (vma_iter_prealloc(&vmi, vma)) { + if (vma_iter_prealloc(vmi, vma)) { error =3D -ENOMEM; goto free_vma; } - if (file) { - vma->vm_file =3D get_file(file); - error =3D mmap_file(file, vma); - if (error) - goto unmap_and_free_file_vma; - - /* Drivers cannot alter the address of the VMA. */ - WARN_ON_ONCE(addr !=3D vma->vm_start); - /* - * 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)); - - vma_iter_config(&vmi, addr, end); - /* - * 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 && vmg.prev)) { - struct vm_area_struct *merge; - - vmg.flags =3D vma->vm_flags; - /* If this fails, state is reset ready for a reattempt. */ - merge =3D vma_merge_new_range(&vmg); - - if (merge) { - /* - * ->mmap() can change vma->vm_file and fput - * the original file. So fput the vma->vm_file - * here or we would add an extra fput for file - * and cause general protection fault - * ultimately. - */ - fput(vma->vm_file); - vm_area_free(vma); - vma_iter_free(&vmi); - vma =3D merge; - /* Update vm_flags to pick up the change. */ - vm_flags =3D vma->vm_flags; - goto file_expanded; - } - vma_iter_config(&vmi, addr, end); - } - - vm_flags =3D vma->vm_flags; - } else if (vm_flags & VM_SHARED) { + if (vmg->file) + error =3D __mmap_new_file_vma(map, vma, &merge); + else if (map->flags & VM_SHARED) error =3D shmem_zero_setup(vma); - if (error) - goto free_iter_vma; - } else { + else vma_set_anonymous(vma); - } + + if (error) + goto free_iter_vma; + + if (merge) + goto file_expanded; #ifdef CONFIG_SPARC64 /* TODO: Fix SPARC ADI! */ - WARN_ON_ONCE(!arch_validate_flags(vm_flags)); + WARN_ON_ONCE(!arch_validate_flags(map->flags)); #endif /* Lock the VMA since it is modified after insertion into VMA tree */ vma_start_write(vma); - vma_iter_store(&vmi, vma); - mm->map_count++; + vma_iter_store(vmi, vma); + map->mm->map_count++; vma_link_file(vma); /* * vma_merge_new_range() calls khugepaged_enter_vma() too, the below * call covers the non-merge case. */ - khugepaged_enter_vma(vma, vma->vm_flags); + khugepaged_enter_vma(vma, map->flags); file_expanded: - file =3D vma->vm_file; ksm_add_vma(vma); -expanded: + + *vmap =3D vma; + return 0; + +free_iter_vma: + vma_iter_free(vmi); +free_vma: + vm_area_free(vma); + return error; +} + +/* + * __mmap_complete() - Unmap any VMAs we overlap, account memory mapping + * statistics, handle locking and finalise the VMA. + * + * @map: Mapping state. + * @vma: Merged or newly allocated VMA for the mmap()'d region. + */ +static void __mmap_complete(struct mmap_state *map, struct vm_area_struct = *vma) +{ + struct mm_struct *mm =3D map->mm; + unsigned long vm_flags =3D vma->vm_flags; + perf_event_mmap(vma); - /* Unmap any existing mapping in the area */ - vms_complete_munmap_vmas(&vms, &mas_detach); + /* Unmap any existing mapping in the area. */ + vms_complete_munmap_vmas(&map->vms, &map->mas_detach); - vm_stat_account(mm, vm_flags, pglen); + vm_stat_account(mm, vma->vm_flags, map->pglen); if (vm_flags & VM_LOCKED) { if ((vm_flags & VM_SPECIAL) || vma_is_dax(vma) || is_vm_hugetlb_page(vma) || - vma =3D=3D get_gate_vma(current->mm)) + vma =3D=3D get_gate_vma(mm)) vm_flags_clear(vma, VM_LOCKED_MASK); else - mm->locked_vm +=3D pglen; + mm->locked_vm +=3D map->pglen; } - if (file) + if (vma->vm_file) uprobe_mmap(vma); /* @@ -2364,26 +2447,43 @@ unsigned long __mmap_region(struct file *file, unsi= gned long addr, vm_flags_set(vma, VM_SOFTDIRTY); vma_set_page_prot(vma); +} - return addr; +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) +{ + struct mm_struct *mm =3D current->mm; + struct vm_area_struct *vma; + int error; + VMA_ITERATOR(vmi, mm, addr); + VMG_STATE(vmg, mm, &vmi, addr, addr + len, vm_flags, pgoff); + MMAP_STATE(map, mm, &vmi, &vmg, uf, vm_flags, len); -unmap_and_free_file_vma: - fput(vma->vm_file); - vma->vm_file =3D NULL; + vmg.file =3D file; - vma_iter_set(&vmi, vma->vm_end); - /* Undo any partial mapping done by a device driver. */ - unmap_region(&vmi.mas, vma, vmg.prev, vmg.next); -free_iter_vma: - vma_iter_free(&vmi); -free_vma: - vm_area_free(vma); -unacct_error: - if (charged) - vm_unacct_memory(charged); + error =3D __mmap_prepare(&map); + if (error) + goto abort_munmap; + + /* Attempt to merge with adjacent VMAs... */ + vmg.flags =3D map.flags; + vma =3D vma_merge_new_range(&vmg); + if (!vma) { + /* ...but if we can't, allocate a new VMA. */ + error =3D __mmap_new_vma(&map, &vma); + if (error) + goto unacct_error; + } + + __mmap_complete(&map, vma); + return addr; + +unacct_error: + if (map.charged) + vm_unacct_memory(map.charged); abort_munmap: - vms_abort_munmap_vmas(&vms, &mas_detach); -gather_failed: + vms_abort_munmap_vmas(&map.vms, &map.mas_detach); return error; } -- 2.47.0 From nobody Tue Nov 26 00:29:37 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 1E5B11CDFA3 for ; Tue, 22 Oct 2024 20:41:45 +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=1729629707; cv=fail; b=kWoEbKfGNsHiBdTC8IQz16u3WpJMzUP70HxwG1O3En/njXcYOALU+WK8EWyo17qk7w3uLWIE2gORnN+/uDU1qMAh62oiaWmcMDOYwxHjvxa1hiROGNx6e8po5E7a/aFX9wcvAPZMlykMzfvOSOUDecp9wfqpGlIcSPnqND7WlIk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729629707; c=relaxed/simple; bh=eBgZN4xb41jyHIkVxVr8miWPKM5sT7f7EMsk8Iw3+/0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=D8cRtDtCW5ETQvejDhdj7igJamd0tJefOC+00rHn4I47vlKu3WCjZF/SvtXZtvqmfETQp2VklfxHKO/a8WML0jfmxqhaUliJPq5FpMMQhEDHhxqixYKoDNtTciKjVq8vCPCooKQmOPO02YvJRCt5Foj5pBcsAwC7AQNpHzlSE20= 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=RRkHlGTE; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=K1gq3NQS; 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="RRkHlGTE"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="K1gq3NQS" 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 49MK3a9U013287; Tue, 22 Oct 2024 20:41:37 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=/O91e0wD9LofGQVbbD9R68Czyy5IEr6OFCxJlloVhbE=; b= RRkHlGTEceW9skLKTCkH2+KhyGz6ZTg8oXFYJUsyjsUyu5wSeGkZd9wJnP9D1gEK T5c8Nc1/Dl6N6pNdEHPuQWLwJ2ruLQrWrRiwu3Ckjpkg1AgoAgV0myqOe/AyJOap XOmoE3AlhnI3kSjmSEDFNxO7s9Jajt1gBC2por4QgDbkMmdH2gEFtL4UivN4C/ZM sPXE26FS7CpwU0bL+spQUkbrz68JigCLh1omuECLrIyd9hReXQyXg94sGGO1mFJ9 ZzriUxGqRaMutWCP8g542X4xi3lHr2VAWGugqtqolNPtgNMRYM8EDS7ZIU9OGAZ9 u/8HAMM+cqwwbwepyl+QCA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42cqv3dsvt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:37 +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 49MKXDcN019706; Tue, 22 Oct 2024 20:41:36 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2169.outbound.protection.outlook.com [104.47.57.169]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42c37ef3v5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 22 Oct 2024 20:41:36 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=H/DpYTFyLyVZWco+dKmJRNaMlTFPjjd9WJ43j2mVvHP0zO+Gqf5H3b/T2ujrqcCQcFsNdJw2tyUynSrb7TcCb1eyLgYjgL1FflC4X5BlJuktBOvDJ2oDQaDcb2R+IdMTxcoUAWVy7AQhto1gm7W6o9pwIHI+A4NxdUPG7HwjR+QvinuccyyTNhefiD0UUnt0+ftNtckxTCT4pDUfDC8fJP5nuA994KOPSicAfZFQ1j3LsDkfvvYx/iVmlFKtwdgOYp8HgV1GKZJNuiAIg9QjJnQ+sosCxwSqMiRkR/7E9QUhITEXUBrS0TEcZmlH7U//JOGrPGVqilcQtSGjwBflmA== 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=/O91e0wD9LofGQVbbD9R68Czyy5IEr6OFCxJlloVhbE=; b=uTPvxf+m3yYwoBV6gOb1K/PFFlQO6uQca/NHHF9gprsrS2Zo7JMqfrn/9OPSeoqqoYg5xpwaw3ol5qEiqEMN4xrDxbHZoYQOQ00jhC8tnbkxc9nbE6xYMxCdJZkD4VzsqhRatycsvP6ovTeHPPch8ZZqFJQriQwrPDuU+yFqII9dKve/1+xAR3gmYLxwgT19h1USZVBdz2qv+wc2v42FhCTbCHTYBG4fYiHwrn1kU6D/qXytfR9zxUQYe8J8QrnSHROpJxJdJb96cGvJzky/Tv3Da7hhqB4YQVJTOIdfm1du/sOZoRU2Coa5uoEGfyZZEcn+/HtYXO7jmV8rSIJPDw== 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=/O91e0wD9LofGQVbbD9R68Czyy5IEr6OFCxJlloVhbE=; b=K1gq3NQS37Q0/SLRmmZlxIHc8dLbnsVMu96/8g5E7y/FEBtqGh3rU4c349Xk702+uD0RpZ/Fc3DuDS4fpP0XHoI4jxnrt8iXnRtxLDpcXckpsQxe70sxXOlYCSbSQsR4i9w3wHFlTSkdH5mIktFH7CzEiLPazW0yGkAnnRinIC8= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH0PR10MB7437.namprd10.prod.outlook.com (2603:10b6:610:193::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.28; Tue, 22 Oct 2024 20:41:33 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Tue, 22 Oct 2024 20:41:33 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu Subject: [PATCH hotfix 6.12 8/8] mm: do not attempt second merge for file-backed VMAs Date: Tue, 22 Oct 2024 21:40:59 +0100 Message-ID: <7b6bf6165080505feb5e00b313d2c84c25015e45.1729628198.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO2P265CA0215.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:9e::35) 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_|CH0PR10MB7437:EE_ X-MS-Office365-Filtering-Correlation-Id: 086c1ccf-e4b1-4bcd-c404-08dcf2d9ea93 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|376014|366016|1800799024; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?30ShBGC92MNAhvuyHDLZxFLexKIGbtrpUkBdjOeyRiSJORiu7KiSdYbkW2Mj?= =?us-ascii?Q?26b5EWDgdkdaCSWyxJSDBXgB6fI9v8MVmWzaZk2C4Ag8C/hQva+rn9/atLHe?= =?us-ascii?Q?yA+gKwArDtzMy5cNLt++5+ZMNKaqq2hk37z4j88PRwecMtj956KBDF3dNxZt?= =?us-ascii?Q?BBYPXbijvmbT9Xk/uwrtZl484BT1pl0eajRc7AJNxn4wWDKUPWUwMkWUw4wZ?= =?us-ascii?Q?IulpdCWHcjBoU4CI89rn7CJIhPfJnlP8FidIle9k+Z+D0q2lDR73NHkLF9qm?= =?us-ascii?Q?e1qXqVCA/t35CpYRWi+myt83Df2U7kuEf0JqKg3fhYyYvotkN7zfPudb6U+I?= =?us-ascii?Q?Aq6peMmQZpRp5YukZqvSYNoaMJjjBN2cIUDaoI5f6KqJZ0eVo2Wuy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efxxzjvwnNBFeFBIdp46MGYsr5iqSTkNHFyDpoUcZDjW5T/?= =?us-ascii?Q?1Vg9Jh7wqKhaKtABgGDpE+rF?= 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)(10070799003)(376014)(366016)(1800799024);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?AL1jjUIabVKfMDMQTeKS+NOLB8EOHKyOsY/NTdLvvMbwsROD47kpxFDxBNdT?= =?us-ascii?Q?vlUYDx2SeCXYalVPQM2iB90PDX5x2rScJNPsLih2tvU+N3fjwx2Bjv5Ar2v0?= =?us-ascii?Q?GZ882ENPoHObBSu/b6de/DZDQMDUN8PkiPpfUtmA/iRTsdWdts85Lq50MzwN?= =?us-ascii?Q?pgK+qLeyytLMfmlqLdT6e6d7uskl4fvknXST6BtlorK7yyzcnLCMUpl/VM+1?= =?us-ascii?Q?eX22mA5+JALeDuW7nrLay1aaRb0zWyALfVObWkBH0P3iKk2Zzp3HNoHPO4b0?= =?us-ascii?Q?QghNATT2Vio65LXte165ksHm2Ed9v0soQvQ1YHgfoggcZjg1d0G7Ndtvik4k?= =?us-ascii?Q?uipCaQVxP+X0ZPqRVMbMcqFhqnIU67SmBFahJZ+feZqplly4L7C04XPKf5LN?= =?us-ascii?Q?VNc4pxfpH0Du7hK9xRn8lGrVTWr07vTLBm9As1HVC8vteqptIS+SQmvVPAdT?= =?us-ascii?Q?DBPcyB/I5YGTULHe+zZuprawo7YdDG1sbCUzktOxL64yFdEt072EF8hMRG0Y?= =?us-ascii?Q?c5AiSON2RBOJqXGi4tJJ844iHgJ3GLp/XiPyfn1ABFbZZyulN+IrMY1q267G?= =?us-ascii?Q?2wY7e8dN5K/faXmfT/6g5Rb4Nz7msip+FV3s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k6xgNfyd6O4NaNZPcJDV8YaoQZaKRUjMA80PP8k/VdMeglTrcGFX6?= =?us-ascii?Q?JWueiZnXwsA8XX59LseYrtRu+Q7lYs30LZHqVpM0ja4tRB19G3+pOOUJmg4e?= =?us-ascii?Q?pOL8NmGCtuCU9CfIshNwexs9fAKxxAsr2XDYvopyLmYbKWkUsbWLik5jVjCd?= =?us-ascii?Q?gIvubw=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: PI4UNchyM4NbA/8ErWkLLyxGB6P9UW9HFzArjkL5yLCx5wmrqD/xhUuUpXd1FPajN480V0OL7XLTFCdtcgOe5lpXbFWQjoo246zoKuhhG2r51NrKnU4zl3QNR6oceTOhtai0NA0gZxnSQlp1qZRFlkfNcRTXlT5qGF8lZ15SvKrMdEym6JCCh8ya+y4hOgCVLMiZ6A7ys5SW4HJ4q9tNQWu5DlvaCN6+BIU7lGn+xgskFKW9bvagIaBoZfkLZmz7PsPuT/QJlmhn3/rfoGp50O1EWcpjgrlP7uvPbozxkkintupwMmT5kTD4clLVrYb2FUxxudomjRZ4H6xSuw5Y3/2xhHa1oKzHvKIBIHQh6Zn5LhhjB0UsVDF376zVdd0vunVBTEpasdzWpPHqzDgXDNbB5v0WlDuyZp1sAAobU3gHtFomRY5ULdM466CNZ5stXz7/qmUP2b03gW1Vdw0KI0uQfpvpM2rfRvm4ouol+0SQd4v7apylEuGVsb/zwNxsgzg/m6ApIhDJ4ah2dBDINGMuNBZrDjkSyRHpswcAjNY6CCi3ufHKAZkSUTEyc3s9e5Ub6KHxhx+c/+mOrjyW1+AGL8Hm4D8o+u0F5YMSAvs= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 086c1ccf-e4b1-4bcd-c404-08dcf2d9ea93 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Oct 2024 20:41:33.8002 (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: 7CaO4yRoaSo8UfW9TQwfs6gbS3SkNp/fR6bkfN/k7mVFpgkYQHnCEqZmO8A7XL7i9mN0cFhu+xtVC1gpp5LfjlhVKrJo0vsBVSFt8EId/bM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR10MB7437 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-22_21,2024-10-22_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 suspectscore=0 mlxlogscore=999 spamscore=0 malwarescore=0 mlxscore=0 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410220134 X-Proofpoint-ORIG-GUID: 0SYT3gDCiej8Hdw2SYk-M3WBSiCJQ4cQ X-Proofpoint-GUID: 0SYT3gDCiej8Hdw2SYk-M3WBSiCJQ4cQ Content-Type: text/plain; charset="utf-8" Previously, we'd always try to merge a file-backed VMA if its flags were changed by the driver. This however is rarely meaningful as typically the flags would be changed to VM_PFNMAP or other VM_SPECIAL flags which are inherently unmergable. In cases where it is meaningful (for instance DAX) it is doubtful that this optimisation is worth the effort and maintenance risk of having to unwind state and perform a merge. Since we've observed bugs and resource leaks due to complexity in this area, it is simply not acceptable to have a 'nice to have' optimisation like this complicating an already very complicated code path, so let's simply eliminate it. Signed-off-by: Lorenzo Stoakes --- mm/vma.c | 39 ++------------------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) diff --git a/mm/vma.c b/mm/vma.c index a271e2b406ab..fe1fe5099e78 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -2260,8 +2260,7 @@ static int __mmap_prepare(struct mmap_state *map) return 0; } -static int __mmap_new_file_vma(struct mmap_state *map, struct vm_area_stru= ct *vma, - struct vm_area_struct **mergep) +static int __mmap_new_file_vma(struct mmap_state *map, struct vm_area_stru= ct *vma) { struct vma_iterator *vmi =3D map->vmi; struct vma_merge_struct *vmg =3D map->vmg; @@ -2291,34 +2290,6 @@ static int __mmap_new_file_vma(struct mmap_state *ma= p, struct vm_area_struct *vm (vma->vm_flags & VM_MAYWRITE)); vma_iter_config(vmi, vmg->start, vmg->end); - /* - * If flags changed after mmap_file(), we should try merge - * vma again as we may succeed this time. - */ - if (unlikely(map->flags !=3D vma->vm_flags && vmg->prev)) { - struct vm_area_struct *merge; - - vmg->flags =3D vma->vm_flags; - /* If this fails, state is reset ready for a reattempt. */ - merge =3D vma_merge_new_range(vmg); - - if (merge) { - /* - * ->mmap() can change vma->vm_file and fput - * the original file. So fput the vma->vm_file - * here or we would add an extra fput for file - * and cause general protection fault - * ultimately. - */ - fput(vma->vm_file); - vm_area_free(vma); - vma_iter_free(vmi); - *mergep =3D merge; - } else { - vma_iter_config(vmi, vmg->start, vmg->end); - } - } - map->flags =3D vma->vm_flags; return 0; } @@ -2341,7 +2312,6 @@ static int __mmap_new_vma(struct mmap_state *map, str= uct vm_area_struct **vmap) { struct vma_iterator *vmi =3D map->vmi; struct vma_merge_struct *vmg =3D map->vmg; - struct vm_area_struct *merge =3D NULL; int error =3D 0; struct vm_area_struct *vma; @@ -2365,7 +2335,7 @@ static int __mmap_new_vma(struct mmap_state *map, str= uct vm_area_struct **vmap) } if (vmg->file) - error =3D __mmap_new_file_vma(map, vma, &merge); + error =3D __mmap_new_file_vma(map, vma); else if (map->flags & VM_SHARED) error =3D shmem_zero_setup(vma); else @@ -2374,9 +2344,6 @@ static int __mmap_new_vma(struct mmap_state *map, str= uct vm_area_struct **vmap) if (error) goto free_iter_vma; - if (merge) - goto file_expanded; - #ifdef CONFIG_SPARC64 /* TODO: Fix SPARC ADI! */ WARN_ON_ONCE(!arch_validate_flags(map->flags)); @@ -2393,8 +2360,6 @@ static int __mmap_new_vma(struct mmap_state *map, str= uct vm_area_struct **vmap) * call covers the non-merge case. */ khugepaged_enter_vma(vma, map->flags); - -file_expanded: ksm_add_vma(vma); *vmap =3D vma; -- 2.47.0