From nobody Fri Nov 22 21:36:10 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D8921BC07A; Fri, 15 Nov 2024 12:37:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674258; cv=fail; b=LrNeTLRFTZIGLNS3I8rGqURzJ2njN/TZBZaNnZ/cQafYBW1QLdorvEqLbrnq7qhg3yOIGC+nRV2gxdcIQJreMK3NQo3eCxQo7ZpAS9L44011SQX95s2oahPOHLv+6EuXoBvFN0/aUqgRkSzdEnoDExn4FbrZ99pFZIIRujrzgkQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674258; c=relaxed/simple; bh=3eBHKuaFDX5a3pUiw815zR3ygDwr3z/dKY+rzPBiC0I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=S7vxLe1oH/lNOFuQhgrI4YhwFyinqYWWF4z0yApN+5rJz1XhvqFrPTwehRXSTSTfpq9HrQYeQvk9IPO73wkGQeUQ3oYzHmsaw4KNqDJxj28Z72InCxOBenLqR8W74s2JZ1SKkR8CqN+mMi3Lz+XQJZ5EwMLO56lJYLfdkfMaQ2w= 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=QH9ietNe; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=lvhEGq1R; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="QH9ietNe"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="lvhEGq1R" Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHHHN030318; Fri, 15 Nov 2024 12:37:13 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=lcqVyGR2Ie4RX5B2PFQeOoDGKUd4H35f3scPpB+0CAw=; b= QH9ietNeyYDtg8505G2LIz8MqSlrqw+Yl9437iMoV7gSq4kWT3jHOtOnurJ9AnTk rYyb2/EXEiBv7fsXfPC5RtMP2Uasz+Unp7BENqrhQMx0UKT8U/c0yh8hEPYrjgIK crqg+tQu2MW7eXBrYzC4yS1D9WunSpi07Y+DoDegJPo5LcvsiK8adQuK2g8jR0/f EigdgirQQXoPnb2uHGCsRFRMvMOnHnCSA0OHpNdk5V+vSK6k1lm8uVTeDYlOAiFP QZVK9QP0VuiIxuteG/hod1fCYAI6Xf19V8EkERiQsNjxjwgN+W5GhRrEGpKxRj7T HsmjhEo7C5+vAQjYpdjSBA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0n539t2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:13 +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 4AFAVLFL000376; Fri, 15 Nov 2024 12:37:12 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2171.outbound.protection.outlook.com [104.47.58.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbjcxt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gaqEsaUZBgsLeUMM5LkOvfUL1X0V5CxFVXUqOlD9L2zokhyuDlZ8FyF1tPGD/3odXltrQnmeQuznkxvEIQ/DM+nIJTawBnN10oSjrS25q+kZYV/7nNQXZhpVrgHGumXFm6Rtw8YjALQq+t2uob0sZh2SxSnK/U7o9I6p9ZwxjogTGhoRqKNNTRwjncOZ4nZb95RjcHSLKoqSHO+zHY9cKEQeP+nJoCdgNG6YxJq1ogtA9abCVgNU6Cas+ShfLo7jW/qJDKtD2Eyu1dgo35+y1npRPdJ/OUVRB7L/XnZj+3xh/1a1tSY/7fVxAjWM/3TV4y+mnKnUJQOlDJgQ2a86Ww== 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=lcqVyGR2Ie4RX5B2PFQeOoDGKUd4H35f3scPpB+0CAw=; b=pvZxGwJg3TZq/y2VySBIpk/zglY9RFm3CCQR08FZvMrEKJJ+2sexqOJ3BiY6W0GxmxV+2nJ2TwBEJvdsXFEBRZ+UYnTpNuBM65ZJR6Mre6Z/IOgiqnfGvkYcSLfNp+9WbuVGv7WzyXcKJBXyMAdlW4td3V6fHqrKetXjVeOyrp3+hpPhegw+ejQvgKVTgYoqOrl/k46vrnvHQZjC6c4v7Usz1Dbq1Nv2AGcizJ0N7CcP99QvhzKj5+Yg4zl+CLj7DJ0go6vXJPzqYFEUekwCi2Pw4h2Vjq3r93tDEdIZ1RbPKopkyoTu0Fnmhczfzw3rpqKgZWE8Ac1qoW2a3tvWKw== 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=lcqVyGR2Ie4RX5B2PFQeOoDGKUd4H35f3scPpB+0CAw=; b=lvhEGq1Rk+Rj9yoEYJwb4LiCbFo9ohkp6p0a6Fj4STZF6jVSIeq34OmEtBpEd4Iqe5ssTNo6fI5blkTEKx24Enz38m63yE5fvWjXYbYEfNZaJZYvUdoWrQSBTOXKJvq1JthBfDhYes5FUVcqeWWh3hW3ZpVZft1DjIPe/1p0oBk= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:37:04 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:37:04 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.10.y 1/4] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Fri, 15 Nov 2024 12:36:51 +0000 Message-ID: <3a4ff9ebcc085e0074711ae883fa5ea11e6d7a8e.1731670097.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P265CA0165.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:312::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_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 59ca1d38-1a74-4ed7-0ba4-08dd057235e0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?JF9VHHAz8zQdXGfBOn6oXH598n+rDaDmCesiJTAtYy3e+0jhAFqwFhCZ88Fn?= =?us-ascii?Q?7UAm0ZRlM0YQSS6g6UeiRjDE0Z0Wj73HT2hlDofwBNmZLNm9uSsqRSU9G7RO?= =?us-ascii?Q?WvWpG4kkJpSNIdIlOy3gEiLoOeAW/uJeTlb4nplZxyn9jEG0H0oxED1hiN5B?= =?us-ascii?Q?amvtUJ7O0GQFfOTkqhuRuqDzwvic4J0D8P3lMvf/CItQBXSwJtIil75uPELR?= =?us-ascii?Q?cMxZ/5SLT/xiiub/+Jb869o240Ww15uh31DqJk4q9iB5J6mvVVgqQdny9BoJ?= =?us-ascii?Q?eOI5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GtDQELWeD4hX7d?= =?us-ascii?Q?ENnRcJAdzn/Y78mM9Tt2gYRKz9UuROaGNoloxc3IIUF+kE697jDfUsBsw69q?= =?us-ascii?Q?t3GkB2J9hIEbHQZjj5RvsPkRKXFM9dCLu3Da/FuPBn39mtO+Cbzd5ACTFiCN?= =?us-ascii?Q?lCV062n7iNKrz6Os7/ECvQLy?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?fqwdnypkYd50oxRvFhzJ2zeMXVjDISfDZbHeRsp9RQUaICyi0rDngFNUGq++?= =?us-ascii?Q?826373vjzQ9FTP9ELLNGLnjyYu7FqXyVbN/07TrxKUeqCuxtjdxZdVpKo+kk?= =?us-ascii?Q?ouTSo2TXIsF/XhVB6AZARZ7DldTSEAKpkYCU08cslNmrh79W0M59BI3gPCQ8?= =?us-ascii?Q?k1yUCx3VPx/VH95WYsMXG7+3i7be84k+WBqup4nxKT/hkn2+blXWnsH22E2U?= =?us-ascii?Q?7ozsG4wNN3I2TXR+w1xo3d/dcTngiZ1a9I+NCew9GhHYw9NgXzCvvn59wc6P?= =?us-ascii?Q?FWQE52fPgpGTNOTfeu6syazbuZi/nJaa3s58Rvdkvrbf1LHQIdqJT8nq25ey?= =?us-ascii?Q?EBixqES8hH1Lc1R9PmGWgwtUJ5lPm4uTCk83R36MPqw3hRd2AIHytcRVySAa?= =?us-ascii?Q?sXrg8VaQGLM1gv3azXBtugL426cVCPes3FtlNT5MVcf5jlrBKDqyZ90FuUoi?= =?us-ascii?Q?3MSqqPd3ICUrI5ZAmC5CPvFnoyvbNLK8W8Sq+PGP/sbYICtUC7q7SMJtJtYF?= =?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w/VN83FthRQ+vdFCQKO?= =?us-ascii?Q?NerTKoGrGLL0BKOFM1ezJxbQRjvjGvFmeG4qbGEvTGDH3pl+XzrBAIG2yOZ3?= =?us-ascii?Q?0XFtHqZWuoDjEb8Cf3bvsCkkoFaLzj70m/1pMjIj93FYVAwr59kqIvxuUpNn?= =?us-ascii?Q?PuLHOVbEPg439jUcTifUkDs99zeBik8zGXguS/Tpa6b09PjyAOzNRorQwrBa?= =?us-ascii?Q?kQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 6YVoWqV6rPXkYHEFm/efmy8Zs6xf/bkVX1x3Env5Sgx4u8Tyzb+g4CW5mgODz9wEmLTIN27SpWHzaIWYZLoMtTXAKtkUxErjxZxTNYOByIoP+/jy7RNFZ0/UNdPna9CU0NERHnwvAIlzGwipmdvFvUn8oU5PRrAbPirTY15MiXjykotTy+Xupzd1jZjOC+yJ+Lux966+qX7Dh6cxdY3kWNyzLINW0b8ecQzKNv5beB0ycpwCX91z80K1lL/XWRiQHFMEklrs4FqU3oKXj+56vcg0U5dpQbDhjA3LRAyLWD/Iq7DnZ+yr8Z7aLxu8Bshqh045K38tkT+UO6RH/H2ZqEWBNcSI0Y462Ewrtp1hnWjBcu+YT48n6lr8Qz/ul8DtNx/xz3nDZxJW+PjLuVXVXwps+WtXOfJQb3F2SkreKLG+Lpg0bFKpyLFEnX27lvV3bmFBNHAq1ySjGW0fh7ep9etOwup45U8SzmCrPNP2kEpTZy4jb+ypHb9aQiWKFBArnC4vXevS9DVnIylKnJB51Yuu8I9dwB4ce1Pj0JcFkBRrFgX0AzBhwT9qU8NkKbCxRUjBdkW99yHWBexrNCSVwuCaOj6OC69z8qs5BzzImUA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 59ca1d38-1a74-4ed7-0ba4-08dd057235e0 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:37:04.7861 (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: RfCQAmEElMFrFzd+lKA2DQ1yCaIanAMWmiI/0Jp1PtWx7rVWGNM+wrXpBhdCvV6qT9Nhf9OLHtsj/lAdOSNpED/UDHTGKT9dFZ5KCxFDBtU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: dNe-1qRMSXA8Xw9ueu8mH48DW-0ogJiM X-Proofpoint-GUID: dNe-1qRMSXA8Xw9ueu8mH48DW-0ogJiM Content-Type: text/plain; charset="utf-8" [ Upstream commit 3dd6ed34ce1f2356a77fb88edafb5ec96784e3cf ] Patch series "fix error handling in mmap_region() and refactor (hotfixes)", v4. mmap_region() is somewhat terrifying, with spaghetti-like control flow and numerous means by which issues can arise and incomplete state, memory leaks and other unpleasantness can occur. A large amount of the complexity arises from trying to handle errors late in the process of mapping a VMA, which forms the basis of recently observed issues with resource leaks and observable inconsistent state. This series goes to great lengths to simplify how mmap_region() works and to avoid unwinding errors late on in the process of setting up the VMA for the new mapping, and equally avoids such operations occurring while the VMA is in an inconsistent state. The patches in this series comprise the minimal changes required to resolve existing issues in mmap_region() error handling, in order that they can be hotfixed and backported. There is additionally a follow up series which goes further, separated out from the v1 series and sent and updated separately. This patch (of 5): After an attempted mmap() fails, we are no longer in a situation where we can safely interact with VMA hooks. This is currently not enforced, meaning that we need complicated handling to ensure we do not incorrectly call these hooks. We can avoid the whole issue by treating the VMA as suspect the moment that the file->f_ops->mmap() function reports an error by replacing whatever VMA operations were installed with a dummy empty set of VMA operations. We do so through a new helper function internal to mm - mmap_file() - which is both more logically named than the existing call_mmap() function and correctly isolates handling of the vm_op reassignment to mm. All the existing invocations of call_mmap() outside of mm are ultimately nested within the call_mmap() from mm, which we now replace. It is therefore safe to leave call_mmap() in place as a convenience function (and to avoid churn). The invokers are: ovl_file_operations -> mmap -> ovl_mmap() -> backing_file_mmap() coda_file_operations -> mmap -> coda_file_mmap() shm_file_operations -> shm_mmap() shm_file_operations_huge -> shm_mmap() dma_buf_fops -> dma_buf_mmap_internal -> i915_dmabuf_ops -> i915_gem_dmabuf_mmap() None of these callers interact with vm_ops or mappings in a problematic way on error, quickly exiting out. Link: https://lkml.kernel.org/r/cover.1730224667.git.lorenzo.stoakes@oracle= .com Link: https://lkml.kernel.org/r/d41fd763496fd0048a962f3fd9407dc72dd4fd86.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Signed-off-by: Lorenzo Stoakes Reported-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka Reviewed-by: Jann Horn Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- mm/internal.h | 12 ++++++++++++ mm/mmap.c | 4 ++-- mm/nommu.c | 4 ++-- mm/util.c | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 840b8a330b9a..e47f112a63d3 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -34,6 +34,18 @@ =20 void page_writeback_init(void); =20 +/* + * This is a file-backed mapping, and is about to be memory mapped - invok= e its + * mmap hook and safely handle error conditions. On error, VMA hooks will = be + * mutated. + * + * @file: File which backs the mapping. + * @vma: VMA which we are mapping. + * + * Returns: 0 if success, error otherwise. + */ +int mmap_file(struct file *file, struct vm_area_struct *vma); + vm_fault_t do_swap_page(struct vm_fault *vmf); =20 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vm= a, diff --git a/mm/mmap.c b/mm/mmap.c index 33ebda8385b9..f4eac5a95d64 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1808,7 +1808,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, * new file must not have been exposed to user-space, yet. */ vma->vm_file =3D get_file(file); - error =3D call_mmap(file, vma); + error =3D mmap_file(file, vma); if (error) goto unmap_and_free_vma; =20 @@ -1823,7 +1823,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, =20 addr =3D vma->vm_start; =20 - /* If vm_flags changed after call_mmap(), we should try merge vma again + /* If vm_flags changed after mmap_file(), we should try merge vma again * as we may succeed this time. */ if (unlikely(vm_flags !=3D vma->vm_flags && prev)) { diff --git a/mm/nommu.c b/mm/nommu.c index 0faf39b32cdb..fdacc3d119c3 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -955,7 +955,7 @@ static int do_mmap_shared_file(struct vm_area_struct *v= ma) { int ret; =20 - ret =3D call_mmap(vma->vm_file, vma); + ret =3D mmap_file(vma->vm_file, vma); if (ret =3D=3D 0) { vma->vm_region->vm_top =3D vma->vm_region->vm_end; return 0; @@ -986,7 +986,7 @@ static int do_mmap_private(struct vm_area_struct *vma, * - VM_MAYSHARE will be set if it may attempt to share */ if (capabilities & NOMMU_MAP_DIRECT) { - ret =3D call_mmap(vma->vm_file, vma); + ret =3D mmap_file(vma->vm_file, vma); if (ret =3D=3D 0) { /* shouldn't return success if we're not sharing */ BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); diff --git a/mm/util.c b/mm/util.c index ad8f8c482d14..8e5bd2c9f4b4 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1073,3 +1073,21 @@ int __weak memcmp_pages(struct page *page1, struct p= age *page2) kunmap_atomic(addr1); return ret; } + +int mmap_file(struct file *file, struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops =3D {}; + int err =3D call_mmap(file, vma); + + if (likely(!err)) + return 0; + + /* + * OK, we tried to call the file hook for mmap(), but an error + * arose. The mapping is in an inconsistent state and we most not invoke + * any further hooks on it. + */ + vma->vm_ops =3D &dummy_vm_ops; + + return err; +} --=20 2.47.0 From nobody Fri Nov 22 21:36:10 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3B0BF1BC9E6; Fri, 15 Nov 2024 12:37:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674259; cv=fail; b=utPnSyZR60i8NdeGx0q94aH6pmnVCf1gV3F7/EA9mtel7LCOGJ/RnPVpDg+ICahxdjTKiY0QkqDq4ACpFHWUlmyt3L0MgFxILTcG0NVaL33fAXpkwM57zQ/B9Hu2BZOEWb/4Jjhv9jEAcss9/27q8jGE4mbzd2/k2wy96N5DJgA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674259; c=relaxed/simple; bh=PwJOnIwrgbk/R3MCXw3oz9MB6l+a9N1JKHv6iKh+Mgo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=oKZL7zaoJm5F+0uz6NgkBi6MV++qQxkSDag73RbuswTWPGwPtLhn/5oD0GmOHObnzWbL5gyOaa+zndq8VdK1TOkvSiOy1gBVhwVgzCalE+Ss78G2pLqCe5pvkzko598dQ3qAvMzt2NmlLr+p0KWN9/m25q+h9sVeAg5jzL0wckc= 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=GutEOMx6; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=Gw1PDfXA; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="GutEOMx6"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="Gw1PDfXA" Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAH4VP030282; Fri, 15 Nov 2024 12:37:15 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=0YO6r84z6d4rGgD1e8mQW4WUPkS0navErPTnR081+LE=; b= GutEOMx6MiSjMMA8ufsOVfF9BfPzNWusbZrMb/+3dF19/UrbEWV72tGoNfAGlzV4 QcMONCgpXzpEBipFjNO0y80sax/JxtJ1c1Bpu5s22ACH0fQTtp78RpXrxZmdgi0s huvhyC8a2rrzvHO8S/reAyU8e7wfYOggFrueQvzdf0HS1nh5uFbriu28dFxFWas/ cfi3jtx/Ll0N54mPRjt5hAvi0tDuuvaXpZ3dEHVCEUS1LR4f9b400t14I/pwulEU +gWsXht2JxTb5d9yjji/zI8fZtsDpPHDDmZ3nxiPWf5NdxBULCiE9vQWhHgdMIec f47IGbPoFw4J8rBkGmI+Gg== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0n539t3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:14 +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 4AFAVLFM000376; Fri, 15 Nov 2024 12:37:12 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2171.outbound.protection.outlook.com [104.47.58.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbjcxt-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=oV83LeQve76m8QAG/X8OqOiBRb8FJLeOuYXKq4JcMfNgDJtPXlf7CENWaXxZYK9a5fd2v9vyuoLlRR0CR5N+np8mQvjjKe2P1y4cz1Rdph6BVZya21KNQM7wP1Y4AkcEsW/x8Vi2pse/vtooZDpNETZd53cTg4hRkKQ0X51wmmBSpDsfRSvF8VbvXQQHElHfVnaewYcKQKMx0EZWIkiNi6I+a+4V8xwERj4iSGYBbmKwhz+ubjtw9JvSVV1lnli/BbOxH2EzcD9eQLkhi3GCVcGOVoQnirKrA0McpL/VRjfVILtpQCof23rQ/Zbk6DkAlvWOeVYohU/eHl6Wq6/Z9g== 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=0YO6r84z6d4rGgD1e8mQW4WUPkS0navErPTnR081+LE=; b=KZ6f1+AUGGlbM4FL5slrDDEcsVbq5eDYyfTz77AGlGyVQf+yPs5Wsm/2JzT7gRyRnBpj7MdJbYw6sxgYaDcDeNA99SXACIVgvq1tmY/ZowjJps0ajWzVlRzpy5sf633xs7sgTVq5nmqO+njoYGJMvtA3Qhv01s1NAVh6JIam5liq5Iai8rhPjsEAxJGCnuJ9WycOM2anmm3XPxmtBRkFzWNIhfMg20/LcWKZiieLdzF4UnGZdURziQABo+jKreEgPRuL30RYO9E4L2waOxyW4P8J/V6riboaWlCQn0YnzOYaIY8ejaZH8YTn8P+RtUFkSRfsCR/Mh3aRSHC8FVywdA== 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=0YO6r84z6d4rGgD1e8mQW4WUPkS0navErPTnR081+LE=; b=Gw1PDfXAa5WVCgPjLJ70KiTvE3FOn2WyIDCvoZcZuuIuv5ZU0MwHYuOg/F0hBJCo1uD2WWgTsNYiAKDIGb8ntwzBF6/HfAZPuNLUNU2WndZE68DWWcEuG2t0b+mMcgDZU8CqAHfpHpXSvafj5gPjXYnCEjdbZ1diRGQDYV6YeT4= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:37:07 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:37:07 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.10.y 2/4] mm: unconditionally close VMAs on error Date: Fri, 15 Nov 2024 12:36:52 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO2P265CA0450.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:e::30) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 779c2df2-6419-4dde-1372-08dd057237aa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?2Wa/Pjh+MfjDfwQUMMLyVsHbUhLz1pOUhXQbgVN+SSqMREUJloiSRwkqCN/B?= =?us-ascii?Q?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b/FWKtNOY?= =?us-ascii?Q?bunsu5n1s6DoXoHU+HZb+dvJnOLxKHRLs8eOeGV99lD7891lTOjW/Y7Y3Bzz?= =?us-ascii?Q?NdKw5G3/K28bOW1umen6eoE9WrEvKH+YOlpBkFe2wrJoy3QK+ez0kko8uLJ+?= =?us-ascii?Q?DhCA/31Pa8/he4fC23PWw6FZ/V3jFmTvEMxAdKiiHvPWKb66eiJI1jim7Ns1?= =?us-ascii?Q?23Z7Gfv5hDwF4RGz3Sz8wTZTM8RAWTf8b4l9lvrpOTeAF79GCF5B9M5e3k9E?= =?us-ascii?Q?kMr87gEdopLIbmGto85tnWJS/whqXXcZnBgv9shlqMGtZKGy6Nxu+uoO35Bh?= =?us-ascii?Q?lGMHplc/V5Zx7/zAST+O9OH39zb5EELrrbBLB+hmilkB8FOCWP1BSj5i0ifo?= =?us-ascii?Q?47JqLyqZHyQWNsfCg1YS2Bg4?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?OkMk3R2T1ciZYzP7MRyZrra1fwbNAxZOS/TsABQyxgApC0F8v7MvQ2YC9Bmv?= =?us-ascii?Q?EPerRDuahe6h/hLJtnxv3I/eykC6OPDFEORQcOrrIi5MfEtI2Ujt5ESA2Msw?= =?us-ascii?Q?uhw0oUmhE+SHtJARiBFg6L0dv1tV9iw1672uldjcgMJwEfMLPoD1TPenWQFZ?= =?us-ascii?Q?CNdavcV/xyZx1qINjrwKrOpeIg+4V98fjbLqnelgtm0Rctk14d+9oHe9w+n2?= =?us-ascii?Q?7mIUUpThm2ZF7uyVD5K5KnnrQOFGcZFC7Wb80F4WikkTE/Fv52SC0lDRk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A/kMegjZWtOm1xk8ZHHq49uENn6lHUOtu8viO7OlJhc6Q?= =?us-ascii?Q?BOd5ahoMIEYitcp3koMgb7Ae3ii96jCo3OmDFdjIpze1AJgWx8OY4LiewD2g?= =?us-ascii?Q?2HtuAgv2PBisI/agy8K5BjB7+8U7QZvPvqEYFykvhu/q8/4yP1+86Snkhx0d?= =?us-ascii?Q?bARqOxnfCy7xsjBufjVmUpMk7hFNqdU1LHtZJunMlodX5adGD0jSet1IJrt5?= =?us-ascii?Q?MCI2nd5BOPSOm8hH+gPCn+G06V7SRDpQTrygJvwe34EeWmv5WoWsT7qZNaqh?= =?us-ascii?Q?FoKccbyQexHLLa62uo2MpirHlIpjTK/Fb/xhiuTJjwgXcNx8zhTO0fFa3Wgr?= =?us-ascii?Q?eqQ/siIBPO1EQXtgsI2Ljib3ZsgotPuwx4Wiqyag0LG2cOLZbLLgFxs1Z/KK?= =?us-ascii?Q?BTslsErcKiD7JOHOOf3iK4JVWIJIdDn43/naNVuhzqgDbis9tsvQfwX168ua?= =?us-ascii?Q?9g=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ogS0zrO7RWzStu6SGHNdXfKIXJto3uPVBzujEK7pPuKCMR6K9qL1nmzSGlARWGy4xAVYELnQ+5inZ59aHP2fRmhl4my2xFVE8r63ZoZfv1ythU9+CJxEL0+qEkgya4MHQyYb5sI4oIyMaA+yjWwYGw6lQmiUbbCCULn7L5hxNoxakd/fn+cQQPRhynSuYVfynQtglWH0YDjN/ppALJECs4D+zoyg1cU69TdNuv03aYEix+XQZtqPIXawxUp+hJXlhMNPqpG3BGYHRUguA9gZNqh3PT+WF8lxlrcKyDIRwk+ak1MGVnciKyYCOeDds2qJz457w7RlvciTU3Qu1OgKpN6alDyH3KccusnRst+skpswRZsNA+Ns5aZpyHXBepUQ01hJBIqr5D/fVZUTgoi5/0cenwW+ozZ5gRwJ7eo8DzdlrP2xMekMqtazDAtSGUrciOhBD1npcOM6KvqIZfMEkKp5UZ0/ZN6pd6YC766WnhHv2lFVPa1gjSNEZUlXSnI3DpA2IRxCIeIaGnEFALta4Kd2s7MrKRXpqqix1f9LqSITtB3hS4del9jaJwHhMu3oqo9IdDxw2z1L8lS0O+uf6hrFjlEIB4yZlq/U3hMYZ0Q= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 779c2df2-6419-4dde-1372-08dd057237aa X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:37:07.7142 (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: bA3sNcqbSpzyFa67M1I6eC/KDnN72Ds6ansCWnXMejIEbj7Xatk6Cof7ey1UxHNwX6wTFu6ROatf714V4242tOF1FbS/dvWcHGYSfY+eLbw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: 8N0rPsEK10oZH9mlXBsUAiJLJpU5ohVg X-Proofpoint-GUID: 8N0rPsEK10oZH9mlXBsUAiJLJpU5ohVg Content-Type: text/plain; charset="utf-8" [ Upstream commit 4080ef1579b2413435413988d14ac8c68e4d42c8 ] Incorrect invocation of VMA callbacks when the VMA is no longer in a consistent state is bug prone and risky to perform. With regards to the important vm_ops->close() callback We have gone to great lengths to try to track whether or not we ought to close VMAs. Rather than doing so and risking making a mistake somewhere, instead unconditionally close and reset vma->vm_ops to an empty dummy operations set with a NULL .close operator. We introduce a new function to do so - vma_close() - and simplify existing vms logic which tracked whether we needed to close or not. This simplifies the logic, avoids incorrect double-calling of the .close() callback and allows us to update error paths to simply call vma_close() unconditionally - making VMA closure idempotent. Link: https://lkml.kernel.org/r/28e89dda96f68c505cb6f8e9fc9b57c3e9f74b42.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Signed-off-by: Lorenzo Stoakes Reported-by: Jann Horn Reviewed-by: Vlastimil Babka Reviewed-by: Liam R. Howlett Reviewed-by: Jann Horn Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- mm/internal.h | 7 +++++++ mm/mmap.c | 9 +++------ mm/nommu.c | 3 +-- mm/util.c | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index e47f112a63d3..df2b1156ef65 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -46,6 +46,13 @@ void page_writeback_init(void); */ int mmap_file(struct file *file, struct vm_area_struct *vma); =20 +/* + * If the VMA has a close hook then close it, and since closing it might l= eave + * it in an inconsistent state which makes the use of any hooks suspect, c= lear + * them down by installing dummy empty hooks. + */ +void vma_close(struct vm_area_struct *vma); + vm_fault_t do_swap_page(struct vm_fault *vmf); =20 void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vm= a, diff --git a/mm/mmap.c b/mm/mmap.c index f4eac5a95d64..ac1517a96066 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -176,8 +176,7 @@ static struct vm_area_struct *remove_vma(struct vm_area= _struct *vma) struct vm_area_struct *next =3D vma->vm_next; =20 might_sleep(); - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (vma->vm_file) fput(vma->vm_file); mpol_put(vma_policy(vma)); @@ -1901,8 +1900,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, return addr; =20 close_and_free_vma: - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); unmap_and_free_vma: vma->vm_file =3D NULL; fput(file); @@ -2788,8 +2786,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_= struct *vma, return 0; =20 /* Clean everything up if vma_adjust failed. */ - if (new->vm_ops && new->vm_ops->close) - new->vm_ops->close(new); + vma_close(new); if (new->vm_file) fput(new->vm_file); unlink_anon_vmas(new); diff --git a/mm/nommu.c b/mm/nommu.c index fdacc3d119c3..f46a883e93e4 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -662,8 +662,7 @@ static void delete_vma_from_mm(struct vm_area_struct *v= ma) */ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma) { - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (vma->vm_file) fput(vma->vm_file); put_nommu_region(vma->vm_region); diff --git a/mm/util.c b/mm/util.c index 8e5bd2c9f4b4..9e0c86555adf 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1091,3 +1091,18 @@ int mmap_file(struct file *file, struct vm_area_stru= ct *vma) =20 return err; } + +void vma_close(struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops =3D {}; + + if (vma->vm_ops && vma->vm_ops->close) { + vma->vm_ops->close(vma); + + /* + * The mapping is in an inconsistent state, and no further hooks + * may be invoked upon it. + */ + vma->vm_ops =3D &dummy_vm_ops; + } +} --=20 2.47.0 From nobody Fri Nov 22 21:36:10 2024 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83D191BCA05; Fri, 15 Nov 2024 12:37:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674259; cv=fail; b=FG5/bRSI/+i0z8hm78E+hZY/66ZZqQZ95YGZhaieLGYZ3x1esF4ZKNLYB/OM6zlp0YEIOfdfOLO8Dc0KTNHpHlvbabbynS2HGXSMDvf6ADqu6amYFe/p6XpLpruU0RqXNa8Cfni9uFVihGrn2b4IekUtULolOl5aP/wPA9rx33E= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674259; c=relaxed/simple; bh=dLV+ChrKahd+il9BZ4slIU0Fi8dDBMuXCzjeHyp369c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=IqMURi5Zs9fAuB1fCMeB0H0KvYcAY61TISOUdtLsE0fuMDa+vAitaoZLQxWtA67VignyQ4TFDQP2Ha0iJwlQ8i/cTN4qFtfMmvzjgRZvGumbec0AopFnIlgb8sDSwhQwQ0Vv31VyVT2mCd/ZhObNV6u9wmn12cmJM5M3V15tfvU= 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=UPVYpGwC; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=cLkFFZoa; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="UPVYpGwC"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="cLkFFZoa" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHJUF011152; Fri, 15 Nov 2024 12:37:15 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=XUrGdEfJG3BcCfoToxKpYOf/vzU5H6f7Of1TxrY7+mM=; b= UPVYpGwCbClS0OVbuxqyGCTOUMuZ2z5CO+JgszRmB+scbSmNC6qeQXkeWBhh3p/d GuI3sWHGj0eU0Imqqiy+mLPPWKCYnVbjAw54/ts2l5LkCRKTAdJJGtHodojb4vHp RV3ufkxy8oiON1KRI5awisIWWaMMto7kDYyYORhT3tX0SXhuCwRYODeGi7iXSHJw vxE+Oc6jUqGKkoyK9raphD9s1OeM6M0OGV9WyEDHyxWaJUJWlXNo9t4e1gLcTw3i mW965anxVoe0hZl6+NX8mFokxnq+0QjEGiLTlXy2GUsMA7c1pzXLvQxxyhCwSEzt 3Rau1iVYNO9t68psWnf2gg== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0kc3da0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:14 +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 4AFAVLFN000376; Fri, 15 Nov 2024 12:37:13 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2171.outbound.protection.outlook.com [104.47.58.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbjcxt-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:13 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=QNAagK2KFCZfl57sBWnkH3o+6IYk/PGOw1dVM7YY+dy+JnStSLBLhvKdE5hnJMOH2eqvSl2TbBEUd5Fyi7kTvJhTkylWCizNEuKRuuevSMzgkNW9DK/p1+/WCwGZzKOV6OPXdWBh2Ny/lA9+MgSmsUf3iWdwHO5UYSDG1QdCZFeYHJgNOeILSyI0/uCY2YvzpIII3yr7gvx6QsypIhfTlzP894GDyYDi8w9TLkP60+dVCo+2BfNk4IZH1hi9OZ8btT7iMbh8OYypNXZ15sSH5w0pNWLBqUC+gdLDDfMuICJCscZIlbsIMeIhj5Q/FpZzf0klEfSQvgm1qjpOf8zHQg== 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=XUrGdEfJG3BcCfoToxKpYOf/vzU5H6f7Of1TxrY7+mM=; b=YQ/aORvKrF7wKiRnXn8ocxuvpzZ/LVsAYjr36qwZSeBCbvTGo4bSdjEn7xIUZgoOloqFH8/9m6K1f1glClbmt5vY4fLazFjhi1msbMi9ttQtlo4DccH+llGYI3HeZNVu4y58YsA75w34PzSx7TLIMQYM2O9XgMVd1VHVL8Vyg/hTYjJUk6TwJZJ9H5i7P6DFJT42IHeShcTp6siBKRsH3/uo8J0fqZGiVvKvmtKCaDlHURDmeZ1zmrhdFW28ff/qG0KrU3rA/tLqr2hDXvmjwPS9OtAZFP2jf0pw27JtXFDTqUWPWvYhChqG/SKz0x4TWuqUBHxQ7j4Rj0j+P7Askw== 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=XUrGdEfJG3BcCfoToxKpYOf/vzU5H6f7Of1TxrY7+mM=; b=cLkFFZoaTlqbevTvdR12QSu+8lu0Ei+rbVRkBXC2xhldwO5VdLSCTr8QhFLiDFi308EnE8lvd8RCxtUjA4qkTVKS0bEr5AWkBhWip0fK8fZtwUM6Lu9bH9Rd5LbOtADh5FjeDCnom4ljmZFGsamSjq6qp0LXqmBOKazoFK5V4XY= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:37:10 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:37:10 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.10.y 3/4] mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling Date: Fri, 15 Nov 2024 12:36:53 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P265CA0295.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:38f::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_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: d70344e2-0cb1-4f86-f937-08dd05723979 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?P74XDdDEy3ry/gUR80M551O5lmuUJgzACc/xcuS8iRyr/xv45xKuLCX9xsEV?= =?us-ascii?Q?fxBAAJsBUuUd0QX9r5aHU1U/S3hbAw/n5yGDCq4+zZM7QOi/WPm1J36xufXp?= =?us-ascii?Q?LyxgQjsW6KUzKGfbhbzW2u5JnrM+cP2FYnb4b5iZ6A2kAtiNkIKo0GxB2xjr?= =?us-ascii?Q?7XYVCC7TyUkmrv0i6HYNnjFAVhxqhCwcA3rb0SJ6oIFKGrVOId6wRNNjaoQP?= =?us-ascii?Q?gtldqYa4FvcUhMXN3dSE4Gad7jeLdjV+YLZ6SHvWq3oOl5pdIrZbn5zbvdsi?= =?us-ascii?Q?/Q2UXvHdl0NXpGxpKYd5EM6tA4eLWSzb6965kFLO7U6MI2wUAOaVh3RGeum1?= =?us-ascii?Q?tkeEoHGnxdPepEAEuZpY4Z82Gwg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?us-ascii?Q?Sx4aYcncLk7Pd57EMu+Bl7hTHuOk4tlUqHgCgvTKY9LklhvCFCWSM3f+HK4Y?= =?us-ascii?Q?6Zm72kwI0zAw1rtFe/TwYaG6?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?gQJh6tCjN9ZP3VC3CnQ8Nl0ghzLbuHNRkL5RUYR4PVelYj8fe4tqc3xBDI9s?= =?us-ascii?Q?Q3ngTo191+JtaNOoiEpK5PJ7t9w5wD7oRucbdg6+7Q9KL/aoSPAPz/8z1IIs?= =?us-ascii?Q?TxrKF9AU4gV/4QzvQXt08HU0+tHa5idGsE7+a/nMfkBgKB2kF1XKff0a9VxS?= =?us-ascii?Q?xGckQPz/cFB9TpRHACh+VjMzU3hsIHWPsNIPGO3F+OQn2xucko8d9ZrC+Nsx?= =?us-ascii?Q?JT3as8yhG2t/UHwCyVnJwH7DoAYv15Rt91C1sWlfuesQTc2LVxaSFg1Gi8D4?= =?us-ascii?Q?7kH7ZSO+g2mNQxWhZTGnaw/uhpouKapnI3iw7NO3QqMEfKK4EDFvEGBha1bQ?= =?us-ascii?Q?Lt6WIBBxzxV/yX0cPdqvTb7D94lIBgBCilvRBWxy9pyWsjs4FLn2gTNCphaT?= =?us-ascii?Q?gz1D0wCZp6QQlpFwz2IrZ0hzpuN8BZQ93CVOzuJBMoGu87t9gK/i3rqN/MLw?= =?us-ascii?Q?DBBcxv4s4jsta9i4Ep0ouefCmKsviiuMkyH6AXSvoyECYZjGqE36fa0Ou1OJ?= =?us-ascii?Q?XOaCyDC6rk7wifHKkw0Z2rUkcx53eGhfcwUlAfrKZXiHmwOQ1t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fZgcJvnazv64c6iEkbDzz5zkedJXlrpz9t5yyaLH1BSnCiVO5F?= =?us-ascii?Q?6cx72+bB4+HhkxqhCODqZWPDmn5ynBc6ny2hlpBb4obPTWCMx2BszpgCGZ/m?= =?us-ascii?Q?7066UwveLSFidg10zs7rKbTh+NRvV+jmoDTc8ar8zRvCOQCQfAo/LD5sd+KL?= =?us-ascii?Q?mg=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: X8a2wp6/ngbNTBksLQchZHTZ5BiD8OQ3Rgn1V8oniHWYdmOpQetxydl9b4qzExy56TLsH2isgLB0KyIDodgCa68cAwfAXSmySaXhVnvpG+AeAW5BPrduGJHMFDyybxLVJTK5L5iBa4zMSCXxQP9b2HMMBO47HkuN1/TsxU+tK3HxGAAt0wj0KFneeL9onsLu6z/SRz94dAJXPuxkn0uyuiMXOFKtN32q8DFArY1yv1YqkhYbYB/1kqRucfzVbueeEXeIatgT1lh/oA6/2VyjxO0Jne52+q8WgxXcd+yFbWTQqMWb7/K9BGewC6dLfTBtEnyEOizpdxjghG5m+0zO94VuNs5w0WnwcUpn7QMQsCYT9gvIRJc7qTqjupEa9jYO4RlGAOMKsBLAmYnetdd5Dz4bFojVYyhNSuSS4qhXhGS1HIbdV8Td7B2ROR/+kRzxaI+cBSqDToptNs9Ve+QewJOPJmR0yRoBIx8jz1jSvYq5UKwhlo6yNpyTAJIdN0wWXxYVGLNXubuj7NSeG9LZx+9SwSzXD+7ZPegomRmWgoH+kkmz7mpEONtLKNhyvrd6C3T2w/PKU8NNIVL3ZJEYJyHAvA1vJJBq2Ds0EE7B7zw= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d70344e2-0cb1-4f86-f937-08dd05723979 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:37:10.7523 (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: 74EYEFmxzYXp9Hs8WmdcFOZCh+wYoxGgzO3dQojW7lreDWBYkrOoSBr9s5ckwuWuJJsds7ju9q7pUsjf4xWyd3m+8xFmoiU1BbvDkQSkCzI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-GUID: mt6B6F85uTrRtoEODQDSqS4jDd8SV8xH X-Proofpoint-ORIG-GUID: mt6B6F85uTrRtoEODQDSqS4jDd8SV8xH Content-Type: text/plain; charset="utf-8" [ Upstream commit 5baf8b037debf4ec60108ccfeccb8636d1dbad81 ] Currently MTE is permitted in two circumstances (desiring to use MTE having been specified by the VM_MTE flag) - where MAP_ANONYMOUS is specified, as checked by arch_calc_vm_flag_bits() and actualised by setting the VM_MTE_ALLOWED flag, or if the file backing the mapping is shmem, in which case we set VM_MTE_ALLOWED in shmem_mmap() when the mmap hook is activated in mmap_region(). The function that checks that, if VM_MTE is set, VM_MTE_ALLOWED is also set is the arm64 implementation of arch_validate_flags(). Unfortunately, we intend to refactor mmap_region() to perform this check earlier, meaning that in the case of a shmem backing we will not have invoked shmem_mmap() yet, causing the mapping to fail spuriously. It is inappropriate to set this architecture-specific flag in general mm code anyway, so a sensible resolution of this issue is to instead move the check somewhere else. We resolve this by setting VM_MTE_ALLOWED much earlier in do_mmap(), via the arch_calc_vm_flag_bits() call. This is an appropriate place to do this as we already check for the MAP_ANONYMOUS case here, and the shmem file case is simply a variant of the same idea - we permit RAM-backed memory. This requires a modification to the arch_calc_vm_flag_bits() signature to pass in a pointer to the struct file associated with the mapping, however this is not too egregious as this is only used by two architectures anyway - arm64 and parisc. So this patch performs this adjustment and removes the unnecessary assignment of VM_MTE_ALLOWED in shmem_mmap(). [akpm@linux-foundation.org: fix whitespace, per Catalin] Link: https://lkml.kernel.org/r/ec251b20ba1964fb64cf1607d2ad80c47f3873df.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Signed-off-by: Lorenzo Stoakes Suggested-by: Catalin Marinas Reported-by: Jann Horn Reviewed-by: Catalin Marinas Reviewed-by: Vlastimil Babka Cc: Andreas Larsson Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Liam R. Howlett Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- arch/arm64/include/asm/mman.h | 10 +++++++--- include/linux/mman.h | 7 ++++--- mm/mmap.c | 2 +- mm/nommu.c | 2 +- mm/shmem.c | 3 --- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h index e3e28f7daf62..56bc2e4e81a6 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -3,6 +3,8 @@ #define __ASM_MMAN_H__ =20 #include +#include +#include #include #include =20 @@ -21,19 +23,21 @@ static inline unsigned long arch_calc_vm_prot_bits(unsi= gned long prot, } #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pk= ey) =20 -static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags) +static inline unsigned long arch_calc_vm_flag_bits(struct file *file, + unsigned long flags) { /* * Only allow MTE on anonymous mappings as these are guaranteed to be * backed by tags-capable memory. The vm_flags may be overridden by a * filesystem supporting MTE (RAM-based). */ - if (system_supports_mte() && (flags & MAP_ANONYMOUS)) + if (system_supports_mte() && + ((flags & MAP_ANONYMOUS) || shmem_file(file))) return VM_MTE_ALLOWED; =20 return 0; } -#define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags) +#define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, f= lags) =20 static inline pgprot_t arch_vm_get_page_prot(unsigned long vm_flags) { diff --git a/include/linux/mman.h b/include/linux/mman.h index 629cefc4ecba..5994365ccf18 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -2,6 +2,7 @@ #ifndef _LINUX_MMAN_H #define _LINUX_MMAN_H =20 +#include #include #include =20 @@ -87,7 +88,7 @@ static inline void vm_unacct_memory(long pages) #endif =20 #ifndef arch_calc_vm_flag_bits -#define arch_calc_vm_flag_bits(flags) 0 +#define arch_calc_vm_flag_bits(file, flags) 0 #endif =20 #ifndef arch_vm_get_page_prot @@ -148,13 +149,13 @@ calc_vm_prot_bits(unsigned long prot, unsigned long p= key) * Combine the mmap "flags" argument into "vm_flags" used internally. */ static inline unsigned long -calc_vm_flag_bits(unsigned long flags) +calc_vm_flag_bits(struct file *file, unsigned long flags) { return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) | _calc_vm_trans(flags, MAP_DENYWRITE, VM_DENYWRITE ) | _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) | _calc_vm_trans(flags, MAP_SYNC, VM_SYNC ) | - arch_calc_vm_flag_bits(flags); + arch_calc_vm_flag_bits(file, flags); } =20 unsigned long vm_commit_limit(void); diff --git a/mm/mmap.c b/mm/mmap.c index ac1517a96066..c30ebe82ebdb 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1468,7 +1468,7 @@ unsigned long do_mmap(struct file *file, unsigned lon= g addr, * to. we assume access permissions have been handled by the open * of the memory object, so we don't do any here. */ - vm_flags =3D calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | + vm_flags =3D calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(file, flag= s) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; =20 if (flags & MAP_LOCKED) diff --git a/mm/nommu.c b/mm/nommu.c index f46a883e93e4..015d291e1830 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -919,7 +919,7 @@ static unsigned long determine_vm_flags(struct file *fi= le, { unsigned long vm_flags; =20 - vm_flags =3D calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); + vm_flags =3D calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(file, flags); /* vm_flags |=3D mm->def_flags; */ =20 if (!(capabilities & NOMMU_MAP_DIRECT)) { diff --git a/mm/shmem.c b/mm/shmem.c index 8239a0beb01c..4e7d2d54dae4 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2269,9 +2269,6 @@ static int shmem_mmap(struct file *file, struct vm_ar= ea_struct *vma) if (ret) return ret; =20 - /* arm64 - allow memory tagging on RAM-based files */ - vma->vm_flags |=3D VM_MTE_ALLOWED; - file_accessed(file); vma->vm_ops =3D &shmem_vm_ops; if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && --=20 2.47.0 From nobody Fri Nov 22 21:36:10 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 3BCAF1BDABE; Fri, 15 Nov 2024 12:37:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674265; cv=fail; b=GAorbNBcAtJYus34DKAKC/UCOxISVsIet9SY6ME/mnN7+gAccU6OzLqLELWn1uzJRPBFSe1zPcqxCbXGMLQgbgdiZ9FGX+B9OIXxi3e3sACFXPWvz4PnVX6ppSNzjgaNVvIgpQapbYidNrx0XNLAUGt6MBwr4sCGu+aYziBaxR4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674265; c=relaxed/simple; bh=QcHeEphsStiaSFUqI6D2rzYW7B/9r+g4Gz5kJlYJ+oM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=BMiyKkWWKlUliNXLK82ju7iexhmGeH1qyv9YLSp3Rr+vQVKAsSLJkGIXSuHEUFhI6Kl5sVflYijWkeQexZJAn5eMs6ICjzguub+MVqi4E2fmZsfBfvFyN2gBqApGd4U8MH63GT4H02qE9hX8gjj5S4y8ytA1spppMN0QIAtyTsI= 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=NqPYSABS; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=FEasZOxu; 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="NqPYSABS"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="FEasZOxu" 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 4AFAH1Um025789; Fri, 15 Nov 2024 12:37:18 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=HLLzt7YUAlhV3V4zmpe3bPdi4KwZJIGMsntE4y/q8EY=; b= NqPYSABSui2LA03XGgtSmfPBg8En1blAJWgeKxzuM/4QDJkv8f+U8UGncxJ1volI eSmE9cnMXPXKPEKUahLhqaQsTWijBSNHzBKUC/hpy5yq5I2laYvgdb5Rm0knaaL6 MOgM+jYAwVtGOgF7EdgEySFORFGtL0HMP506xCbbgkZo6RzCaT1gR3SrSvni0NY3 wHuMpDUKYx+zh8pWJcRS4YJbMTlEB8zRjd98wmrNASXfccrnfh2SjOK3YrL3SSC/ x/jF/oPE0vmhue86DDGuqexnEuh83+2AC9AVpvhB4+eROTYxahw8y28U8KNnyV3m E03du9qvrS5DClJlKsaUuw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0heu6c9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:17 +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 4AFBXWnd001273; Fri, 15 Nov 2024 12:37:17 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2046.outbound.protection.outlook.com [104.47.66.46]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ch2s3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gYt819jmfsGGlKuBUSotgXm+BvAPflHJsKu2o0N2HBC6X/k/GLxppqw0xZT+8w1ltDXX0oo53kJ+c1izfRezluaAGHEM9/PlrR/IQJllDepGmDNa66gUkknhVLgn031rkY/h8BOgQam0TlXabmNELKTa6EUM86+OCTGTfmx9+MpAOvS+09Cdzw1oERF9a2SVzD0U23kxjTnpMwtrjz5mMuAkL38VVxuzc6S6A0Ei/bqoQexSWHJZBcDnfZWpKqy6ZP04iwXTRXaSbPsgVEvqrEAvi35HckLlnTncknkQURUiRnwMxNJsHYoWMSBtV+q+u2mOsb49gBMUAnAnZKfWAw== 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=HLLzt7YUAlhV3V4zmpe3bPdi4KwZJIGMsntE4y/q8EY=; b=Zl/MGtYaDHxidCQh94Y9vw9GGfddfe76elTVNo917SY0tR1vdgDUo6bu02GdscYiL9GgNL5RxT8tEcydswLFkhi8sWf+z7KXJTE0pfJBdOL7MloX2TpoOFnnH/XIxr1MXz5sjeaRpLLc8qcJ8iZxtOXBYsEotIMUiqK+X2YIJxeyJa2PtoJ5WceB/fOoo0fuQxEwsAbp+vya5378PZ6eY4tKiox+nvgGPk+d72sds9Od2G5W4R0CSDG5OdRaDbTplKOYCzX9HcVDFtU2Zi9sjU0s/esKcr2wXxCmUjVRn5lT1EPAxplNSsAOxeHbtTqWiXQG5rr5I4ZrhL2oT3ueAw== 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=HLLzt7YUAlhV3V4zmpe3bPdi4KwZJIGMsntE4y/q8EY=; b=FEasZOxuV9a+yAKUVKMybysnQR0rQb4Huk6/dJGGpo9cZvQo+7K2bcrrA1q9RyCWK3jHc67Zb2aV/WMa9Wo9EjGUCNaVSD6LCzgnANYzIidMQLkg4PA7w9xABaPTeIm1i5u0rJq/QCw7i1WqdgS/yBaIBPKi3hCqBFJmalxELqM= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:37:14 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:37:13 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.10.y 4/4] mm: resolve faulty mmap_region() error path behaviour Date: Fri, 15 Nov 2024 12:36:54 +0000 Message-ID: <99f72d6dc52835126ca6d2e79732d397f6bfa20b.1731670097.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO2P265CA0438.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:e::18) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 1d98819e-3e35-4b34-3281-08dd05723b3c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?u2wHipCiP+4lY6bqNN99FcZaSPHSR+9u+cPZW3Nz85oSVOpDsLNxvJDnuk75?= =?us-ascii?Q?tRmj+EWz/HaWtHElzZLMa6cGz9xGDrIUm09uCh+wUvQ868pfd2LTm1Z0JRaC?= =?us-ascii?Q?2xBakBc4Oz37so7kKnC4pG+6zHrZXsdXCeYv6B39yavxJlncCXT2m77C5hOJ?= =?us-ascii?Q?A18JvYlm3npG6RP3Mi+ywtealp5ynyBErodW54SAMii0IIc31zdAyhzymX/c?= =?us-ascii?Q?21VI2JDA7YqraRd0bhwE8MUKvat5UC3G811wEuM6m0zpjjdbGfCYss45ZCpm?= =?us-ascii?Q?y9QnMybwvijv748j7QbUvHf3cnTlRfWgzmp1WBxj1cYVUypDod8X1jO5axDf?= =?us-ascii?Q?ABim0SMyAxjtMxE7tXH3jyLDiWR+dTTrIUKyoZNirpME4qcBIe10idm9hf3G?= =?us-ascii?Q?Vw0HZWdsdR0u38ZbpV0v4Af76XnlYpN5OPJmPmw83zTtUTB2apsIq5HKKAuR?= =?us-ascii?Q?1r9N97ivksaA3GFlKxptsg5afdmW6pPOHCXpJDBTKk75q8O0eGm/hht78pyv?= =?us-ascii?Q?ScPq+PeRrKmHOeMA1Cp5uoI1WpwWw9Zll9SyCa9rtJwCPHcL7aYRDAxBrtbc?= =?us-ascii?Q?eSIodGEVg1fmx3RsEs+nZKW+JXujYEM/oZqc8vsaWs3xt9XQeg7UKbzj7i4K?= =?us-ascii?Q?8y1rZ2wjwpzcV/HM4vMykKSTGKc05p1seJXgVY+6xBV5vUd5sYoA9W9M8Its?= =?us-ascii?Q?dVudU1+l+/yETbxEaczax6T/dYobqaNnPh/K7s2UgcsZ/vUxr8Zvb2VO9Io+?= =?us-ascii?Q?xWaONcJmyYHZxWhDzHT/J4FJ/qDxM7mcIFzdSNQfx1CRp7o79pBJCiuFlGjz?= =?us-ascii?Q?sSypWFXwE+poe03qDKJteWYJAcw1KfqRt0ZEZpp5cvTZJzERTq+8VVgORYz4?= =?us-ascii?Q?tPh9VQ3TbS/fZzkeZ4Az/CvUU02Wuh+BLiDstZZUkpHxHus72lKhjPD7nFWG?= =?us-ascii?Q?X0tDM8D97MBMjbTHImxOrnhO/uYu5NT/yrgwOGO427BQgfdcwS+gGaaGGkdg?= =?us-ascii?Q?MDA8LLXiAd64pFmONf4dtZjbsMlva6wzct2Zme+f2dORZWUmRSwrNqG11kZ0?= =?us-ascii?Q?TWNNyrQB+ODGMh4jxYJtzkNHHNCoyORUcBFKZBQFFcLAYLyepQi1tORmYyh6?= =?us-ascii?Q?I+b84kGMumCLho4tnoTPhMtAtLOdT8EQkk3uYSFwL6dQ6C0mMRDSkb531+HJ?= =?us-ascii?Q?kyR0K9ajbgyYJC4atC5ZkGvfWxe2fB+tq3c7+QvdsG16PhWEnzyr5ppkvTjC?= =?us-ascii?Q?N0dUjXgAJEyhCvV6XYY3x1YWzKe2yGo4pTQzarRdHhNPRFg6Zyh3E3IrKl8O?= =?us-ascii?Q?b8apnyWlM6xbm8YCnYfApAiz?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?oVhyVI+gmDkwdbfqCZXRwIWdMhjJurDfT5glQSJLHdACZ/xaV35Zr6WoVgdd?= =?us-ascii?Q?ZovXock5SeztTzQhxMxhW3KTXnGoplVweFNq2zfzEc/nzcRNIhv0l82KDd21?= =?us-ascii?Q?FvzqpudblF8rQvhZoTtkuSxd8IS5qj+TpFxHPQiopiuh5XqmCR16ZTpx74QI?= =?us-ascii?Q?lBA84BKdW2ag5DB5FN2r8k2D4agCcrzEXkcHoWFUWWF6RW7Dw9OZfKmXRhei?= =?us-ascii?Q?REzIfwSgvJUn2oN3iKPslQoedkwwnqL960SFTwmN5jB4cduzqaSL5WHuELU6?= =?us-ascii?Q?53bgYsmTY2GQzzj9hlIgUB8jik1PntgcP7PaF7SrJqz6S0KPSPTanNr9+Xb3?= =?us-ascii?Q?u4ihW6N0TOLGCu+TWyxJjUM25PultvU1DUm19VZqZ+uBb21Upel8P2+VuJU4?= =?us-ascii?Q?OcdMA2CS6vhsRO5ctUTe6xgo7muPCbNP9MKHFsKq6gb8UGa7olCUPXgOUs3v?= =?us-ascii?Q?/uX1GGb4hP/gr+QNF2M2UKkrJwEzh8wO98gKh9W3PJXd6MYINia4Cnpj3N8u?= =?us-ascii?Q?GUpeaTXZVcT6To8CYPhFgberepwhPE6bME5eQVhz2LwmnKKq0btqP5p5s8i4?= =?us-ascii?Q?BeksOAHww4QYAFfQatlY/avYbZE37OvnWPw7QZMmtFctGh2/P2cSwo59Yp5y?= =?us-ascii?Q?WMYMHwRRgbvuk8Bz5ttYJv5Hpuj8CB0Sb2XrBRxB6TWeDJawQQG2EenDcbpV?= =?us-ascii?Q?g80IT8JhTquy85OvcibFkkZe1w6jzgz20/O5qJavLRX4CB7RTbJ02dL3WgiM?= =?us-ascii?Q?9wDZzgtf6+9BHw+TBWyEkSawrmtkkdjtEpL5G00tL5lO9rcbl4q4C9Lp4xdu?= =?us-ascii?Q?4n213tRcO0/Nq888Nau+3udx4jkgLIDXv7goLTyT8ie7vvKKtKp/g4Xk0myo?= =?us-ascii?Q?aF8+zC9WswZJJsYVKylg83kvnCBQUqVi0RN5x2hdZuMHN9mtNA6bTFrUKcKW?= =?us-ascii?Q?8HRx76/WUUtw7WXi9P58edwtCoCKc5EFYinVinWdYvxJ2aFe+QwxyDnZgIcW?= =?us-ascii?Q?dxdnNOwGn02z3eUWbWlzK39b7lsrDWbVXHbrtSU5wFdaq6y2DcIx4gOxg6P6?= =?us-ascii?Q?KXz9OKIcEST3/jeEarmM03/Xk+kaJfzgZ9lo6TbFh1HJBeo/RpVffTaKvtll?= =?us-ascii?Q?d/TSJhujjsR50wPY/J//knF85UG4IB9UaD4Rh4c5mVYepFfQNDkA8nu3UxGA?= =?us-ascii?Q?BlIZRtZM5KzoZvd+/OFRtfMFTjVYKHZirfgOZp3rAn6R57ZNX709+7h0dhgG?= =?us-ascii?Q?eWUXhiH/T83aZ4TzH1Whm8D2MnAcJwC+dk3lqKA1anDnNV17Q17+87YyGlwF?= =?us-ascii?Q?Fj9G/n9wJ5y7phxhhTP78ox5gOwhMo/QG6MSdddU2aA7Ch4/v5+vAEiziIaw?= =?us-ascii?Q?rBC1JEQmWqEXsu0N0C4x9sJLk4kH2IfDIm2cNGPNBhZt/lpM3dUxwzRTF+e7?= =?us-ascii?Q?q/fk1yZfiP0pJXlm2yM/zMaf0S5TIUyc3Muvr9Z3KigZkR5b9QktCVAYo75r?= =?us-ascii?Q?RvT6T3g2BFKKcoDdQ8BMwmX4r8XfpbGAYPY1+D23hFRTOu4NSoIQ6bUILo7/?= =?us-ascii?Q?rNwn80yYd/mpmTuhNamkLQfa5IF3PMRWaE4i5dm2CLuX1Yp7Mie/LESQHNYZ?= =?us-ascii?Q?eA=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 3znoPqeMNJKbVpFw+7nxQkAZX33HE4rzsGwPK4bqG/o/75meRWALU62ulbytqJwDxK2hLGBltOAyhJ0nKnh/9M2CZBYKhClVj97W/TQcUbe9qIKYo4IYuSTljBdJ8K3i+9qpbH+v60pYg4nKebxw8AIGrPriF/EmJdxIp6Zlgw1uuon3JYCEtCrbwYMmyBi6Ueb+ukeTNBBKtjwr6eCTAXyYot1KIbeuC3lIZfLDjCmbbWlFuJFhYNydz0c2je1w4fbcVLhogzDVAbDe9MH3xovGne7tCeacbM+sFOyV60BqE7hWdXIaew4frxEqHcex0BSGeiOYjleDogmjgdmrVWrpSyKDENhOCJ8tNYerWIMKMH+6faflLW22SbElOyCm5f7zSQ87286d7UZX6aV+jviQtdwWcB2Z/17VdjWeaNFBsXqXZUR9VkjMd1Co0OeaEVqYBwyNS3Ci0CB7+8Cfrr8v7z+y68nWeWio6/0UO3V8U5NHbse4OOmtLLwKhOIbxmJocc+frkXm/KqVveXEVUZ+CivONQN23hVYLDP5tfUDY2NF6zjTJhwzMzxbtmK3jQLnK+rLfdlLJ0NH1WwrlEFiu+40WQnBEwJ5ih8BjHA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1d98819e-3e35-4b34-3281-08dd05723b3c X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:37:13.7093 (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: DY3bTqAhRD5JTDOpBSLLyc5X9uFi/1nW9+K04S4DhAOgnBjqLXsM2CVLP/iYJa+ZyZQzsNy2ie+d6BD7oonhTSEx42TFBuFA2AL1C1Mm1vg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=945 malwarescore=0 adultscore=0 phishscore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: Vc0Y4RYwVnXxdBo-ske0bG6ydEnz75Oa X-Proofpoint-GUID: Vc0Y4RYwVnXxdBo-ske0bG6ydEnz75Oa Content-Type: text/plain; charset="utf-8" [ Upstream commit 5de195060b2e251a835f622759550e6202167641 ] The mmap_region() function is somewhat terrifying, with spaghetti-like control flow and numerous means by which issues can arise and incomplete state, memory leaks and other unpleasantness can occur. A large amount of the complexity arises from trying to handle errors late in the process of mapping a VMA, which forms the basis of recently observed issues with resource leaks and observable inconsistent state. Taking advantage of previous patches in this series we move a number of checks earlier in the code, simplifying things by moving the core of the logic into a static internal function __mmap_region(). Doing this allows us to perform a number of checks up front before we do any real work, and allows us to unwind the writable unmap check unconditionally as required and to perform a CONFIG_DEBUG_VM_MAPLE_TREE validation unconditionally also. We move a number of things here: 1. We preallocate memory for the iterator before we call the file-backed memory hook, allowing us to exit early and avoid having to perform complicated and error-prone close/free logic. We carefully free iterator state on both success and error paths. 2. The enclosing mmap_region() function handles the mapping_map_writable() logic early. Previously the logic had the mapping_map_writable() at the point of mapping a newly allocated file-backed VMA, and a matching mapping_unmap_writable() on success and error paths. We now do this unconditionally if this is a file-backed, shared writable mapping. If a driver changes the flags to eliminate VM_MAYWRITE, however doing so does not invalidate the seal check we just performed, and we in any case always decrement the counter in the wrapper. We perform a debug assert to ensure a driver does not attempt to do the opposite. 3. We also move arch_validate_flags() up into the mmap_region() function. This is only relevant on arm64 and sparc64, and the check is only meaningful for SPARC with ADI enabled. We explicitly add a warning for this arch if a driver invalidates this check, though the code ought eventually to be fixed to eliminate the need for this. With all of these measures in place, we no longer need to explicitly close the VMA on error paths, as we place all checks which might fail prior to a call to any driver mmap hook. This eliminates an entire class of errors, makes the code easier to reason about and more robust. Link: https://lkml.kernel.org/r/6e0becb36d2f5472053ac5d544c0edfe9b899e25.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Signed-off-by: Lorenzo Stoakes Reported-by: Jann Horn Reviewed-by: Liam R. Howlett Reviewed-by: Vlastimil Babka Tested-by: Mark Brown Cc: Andreas Larsson Cc: Catalin Marinas Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Linus Torvalds Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 69 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index c30ebe82ebdb..9f76625a1743 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1726,7 +1726,7 @@ static inline int accountable_mapping(struct file *fi= le, vm_flags_t vm_flags) return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) =3D=3D VM_WRITE; } =20 -unsigned long mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { @@ -1795,11 +1795,6 @@ unsigned long mmap_region(struct file *file, unsigne= d long addr, if (error) goto free_vma; } - if (vm_flags & VM_SHARED) { - error =3D mapping_map_writable(file->f_mapping); - if (error) - goto allow_write_and_free_vma; - } =20 /* ->mmap() can change vma->vm_file, but must guarantee that * vma_link() below can deny write-access if VM_DENYWRITE is set @@ -1809,7 +1804,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma->vm_file =3D get_file(file); error =3D mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; =20 /* Can addr have changed?? * @@ -1820,6 +1815,14 @@ unsigned long mmap_region(struct file *file, unsigne= d long addr, */ WARN_ON_ONCE(addr !=3D vma->vm_start); =20 + /* + * Drivers should not permit writability when previously it was + * disallowed. + */ + VM_WARN_ON_ONCE(vm_flags !=3D vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + addr =3D vma->vm_start; =20 /* If vm_flags changed after mmap_file(), we should try merge vma again @@ -1851,21 +1854,14 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, vma_set_anonymous(vma); } =20 - /* Allow architectures to sanity-check the vm_flags */ - if (!arch_validate_flags(vma->vm_flags)) { - error =3D -EINVAL; - if (file) - goto close_and_free_vma; - else - goto free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif =20 vma_link(mm, vma, prev, rb_link, rb_parent); - /* Once vma denies write, undo our temporary denial count */ if (file) { unmap_writable: - if (vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); if (vm_flags & VM_DENYWRITE) allow_write_access(file); } @@ -1899,17 +1895,12 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, =20 return addr; =20 -close_and_free_vma: - vma_close(vma); -unmap_and_free_vma: +unmap_and_free_file_vma: vma->vm_file =3D NULL; fput(file); =20 /* Undo any partial mapping done by a device driver. */ unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); - if (vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); -allow_write_and_free_vma: if (vm_flags & VM_DENYWRITE) allow_write_access(file); free_vma: @@ -2931,6 +2922,36 @@ int do_munmap(struct mm_struct *mm, unsigned long st= art, size_t len, return __do_munmap(mm, start, len, uf, false); } =20 +unsigned long mmap_region(struct file *file, unsigned long addr, + unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, + struct list_head *uf) +{ + unsigned long ret; + bool writable_file_mapping =3D false; + + /* Allow architectures to sanity-check the vm_flags. */ + if (!arch_validate_flags(vm_flags)) + return -EINVAL; + + /* Map writable and ensure this isn't a sealed memfd. */ + if (file && (vm_flags & VM_SHARED)) { + int error =3D mapping_map_writable(file->f_mapping); + + if (error) + return error; + writable_file_mapping =3D true; + } + + ret =3D __mmap_region(file, addr, len, vm_flags, pgoff, uf); + + /* Clear our write mapping regardless of error. */ + if (writable_file_mapping) + mapping_unmap_writable(file->f_mapping); + + validate_mm(current->mm); + return ret; +} + static int __vm_munmap(unsigned long start, size_t len, bool downgrade) { int ret; --=20 2.47.0