From nobody Fri Nov 22 21:55:18 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 0F6271BCA0A; Fri, 15 Nov 2024 12:42:31 +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=1731674553; cv=fail; b=dR4WeBoJYw6GCQnCCf5PDMfqaml7qB/Kc/phssaYta/CbKKuKkVIPlagdNIqI4wtnc1bvvnd/Cj87PkuKofnLfh4kOYdx+ZJlENO7NN5URg3E9XTg8fg/K/+aXwtUuFVvhxegrEtZXagxkQJRWYTAzgglsLLkL+/Tgqgg10zaqo= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674553; c=relaxed/simple; bh=oRkang9dK7aFEYbjyqK93kVrmfU9W5dKd4grjVRuW3U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=UjGulkbv5Nr/AumqaaRxCrp6+R6pmSjpFhELFQFaxuoUzP7LJiOysjngiX39Kvu5T7FKoymDzSZ3j9L5C1cnYlKw3MOWcO+B1ctsXbhGoq/q3RG+zCbadZSegV/qL+L65lzd6XdR6JduuSlwoEHdZZCBICOGnLfjjTRAwbS/voI= 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=cVVyG5v0; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=TumDi+h1; 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="cVVyG5v0"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="TumDi+h1" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHHMs000667; Fri, 15 Nov 2024 12:42: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=/TY0TOZyddbmZHX6YK8ZiEM3V6psrC36jd9wYlgChpU=; b= cVVyG5v0gJ2EY7YwZ8dW/C8G3C2B4pDPBl4Ex7KiNZF/0fptAAtS90yX+mViy1ve 4HpYk/tC7BirNYFvtRj+1H1cxWwy2yqs1NJ0V4iXuGkuQl7+kkOmIFMZR4SnnRPg VyqE3HcvNg9SF+TzChuar7PTqEmQcrpXRupgFS0rWvFMHpaVOqtgKESkaWrykTaa T4sdxcRjglRWDkQxhVnpzurM1ArvY1c8up3e7ZbmZGN2LjbfP1z7/+AKJYk4DWO5 +WBwpo9JNhAF2PxGHAAhr1dVNCXjf5ofvj/pQsBVvy6pMbczdipndu40vh0CQSqp YUP/lbFJCAsOHtqQvl/1gw== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0mbkddp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:09 +0000 (GMT) Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFCMWdZ035940; Fri, 15 Nov 2024 12:42:08 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2168.outbound.protection.outlook.com [104.47.59.168]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6c5604-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=LU9dnhzkLRD6B9BnF++wNik/dfdD942l/nES/eMjUNSPp/4OEAiD92yLvfkAcyylzEAX6Qvvsooz4Ep7vesuAIWOKw6hRq+cmOxaVvMWjb//ztcgowFVaKreNdCSG6mXo+FKAGihnMcJ+BBnmntyjWOax1Uf8RITIdR5Urlcb3sdD3vYkJkm530jGHtPJN00lzXpNe5I389NheKGD0XWniVJQ2FyY/CeFUTfn3FOISX2aDhCBD/2ea4FIl+QoVqIbcz6MxFpVF9eg47xYfXM2C9FiOjpLmhRIwpZ9uGV7ciSdfG+DBzpJVmUf3OGUesaqUVbTVVy9D7jAYTqi/DwKg== 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=/TY0TOZyddbmZHX6YK8ZiEM3V6psrC36jd9wYlgChpU=; b=wHQJqnuU8P2OFHSF6ucbO9jH12EUKEzFlQyQqur73cL36yHkvCUXNRi8MAfYh7nRiSZH2dD5tfSKQ+rwCg+R2f+uLwyi6j/e55qPeRdojD8ad2psQoYjkoYNJQktrXGIdTYmI5wYNkl1kGLN4m/A75bzrbTsoXCdXLHPjo5ISG7iGiVXj2AQsEwIn1syeti0zdeX4jej/aBNpVU9QOAjkBVOaanTUR/jOWYc9ChBJBC5sMojDnuTLmOcWAj9riaGDcKE//CTXJAMONpogz//RwPlNl/sjZnpMmvg7lj3UQPWWKys8j75analewwLeLYJTsOXj6PHC7J7zaRCsRda3w== 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=/TY0TOZyddbmZHX6YK8ZiEM3V6psrC36jd9wYlgChpU=; b=TumDi+h1C+woNv+vL9/JQI0UN0feHiQbeGJ6uqVClfrO/pgEtsWvCUpp5ydOqptluo6NTRl92E/Ggtw0YHmdI6FN+tpnrtO4qQGNAXDSgSVlc0leAQJXfrhFkKfOBtkqnQ5jpxbEo8Id4XVTl+lkTumDbEaKFPtxeNt7ECKlLb4= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:42:05 +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:42:05 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 6.6.y 1/5] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Fri, 15 Nov 2024 12:41:54 +0000 Message-ID: <33d70849ec62ba738ca2f8db58fe24076d5282bf.1731672733.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0484.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a8::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_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: b28debd0-1fbc-490f-792c-08dd0572e936 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?XFXe3JsgzZgD4+thwQzsEC2wYrz1v7LQCok38oisZGZ3KQyn640IX76Ddyha?= =?us-ascii?Q?2iLZc6i2Ievjz2sxD5VftvvIeH9sgU12FR+jcGqIv0/NolFca4nGOAJ4vmGs?= =?us-ascii?Q?P5R2ua5A5CklgfVmgXm/tc6K9Zv1B6QVNFglgyYZXwN0ovI0K4DwBGbSgBIe?= =?us-ascii?Q?IDNDz4yxfgxLB+bNaHeiHn1wyleHsANo5wCwxOesh0FLHhp1glzXqfDI0WR3?= =?us-ascii?Q?q/H26DKvJs5FySqCw3FyQABJRMgDbghHaeV+Ui4pTgfD17YXBq6XdnSTm5T1?= =?us-ascii?Q?9U6A9J7KLm0fIok2E/ItI8KdEhdMrtTjnGCgHsBFjPrwDWPAwOqitu6KLJRZ?= =?us-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uAtn8UJpw6I3SxW15HVd9udd+uxM/S9?= =?us-ascii?Q?+SfPfk98ah1Qsw+o73VaqtUHGFk2+ajZT7N/xm7tfAa2yzdpbhEy8rcvqEHW?= =?us-ascii?Q?A1a/WOMCAd90eSk+nKwh9rGd?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?80QkjYeOOxA760FRSMeRgU/Dqi7pwF+uMjHoWJj0u9gpL8cwC15MKTV1VOFg?= =?us-ascii?Q?P3OImneZ2+8GktotP3R9hn8yU7DywpguTBBsHMl9fJk8I1UDb9z+YuCZnNZx?= =?us-ascii?Q?slTByRIwjOKRZtbDB2ZckXw1a/Tvhw0oKGx7KhkzZreAu6vRjdjvuQuZQGyN?= =?us-ascii?Q?hwfobWTJnQai2sN9Z8IaG/DQVmq789LXopY6g76kHrfcv/IePCth56oqlzsW?= =?us-ascii?Q?sHHh7sfbOTM2mqVkKWgHquGxT8ZRf6HiDxoe7RE+5yiXXuw1oNBlX8c984+J?= =?us-ascii?Q?J1BHtMe4nwdMz413dkTCpvB9BbWeHXeEdBZf9CoIB08Qf3c7qM2sgzvYACf1?= =?us-ascii?Q?uObC+dmvHoN1iLj41qRN47nwSxFYY54CC0q0hDLmaerak8NPHm7j5hnLXEac?= =?us-ascii?Q?keD21ejPgh7EV/MoN2v3qhR+RVheO8iY8jF+EtAGBYTt1GPx7okJgiPtx1bD?= =?us-ascii?Q?hjJD9Clb/MmUcZCsmZ4j2GrteAEELGg7dgWg8uUa8AOrz6614p1hJVfxpqsl?= =?us-ascii?Q?unvoea+RV3H+5rNhQkolU4t7IiP/xdDPr7ZjXK+SoFXNADpMH0+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NmuxYfrEcyXMrayDzRNn7jJ/t+zca8Cbd/HtCLiTw?= =?us-ascii?Q?99TdWwWLfmUtURUqUE9XEpjzUwW3ggf7fsOO/++ZCYnfAbZ1u1cJmGF0Eikf?= =?us-ascii?Q?rMv5tFAI44fFKuMWndd88WtYUwALZ86vCvqcE0C25qxx3eZSztH2VGqXcMdY?= =?us-ascii?Q?CA=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: cLtvnuqUb3MnFHXmWFwtftKyn/LavRmj6+h6883pIC+qCqxmOibmNV4QBvRPkOqENqk65kntgwh8FY7pPx2uWUr9SQAIeO3HkSXDWflOAqYNp2L8vxqGTevyaYDulj4DC0ykPYbQpsEegKwiO2Cywxbn71hbLH1DgdOt/R+S7yuPHIncxoY25bT8i4h4TbB14jQG+jj2eJF/iVXCcU+DrhCXjqMIRfKOzevrLf0nVyaqzgLsLPCPDTEVx/XQ/lvDgnrdrj7PnPu+NhYI0LYiECJL0R0mxqdwU+fIAqD6QGA+CCc14Uag3WBoz+lLbLny9sKQ2F4hgExAYltihS6tQrxIAR9T+kp4f47+B5YJFYAbOymm/0Qx8tA2hpOW5ppU4raCXQ5FYKHt5SBD7nlH106VfH0/HE7ILG1kb7C6PGZCyqHJ8CdG8V0oZkIYvxSPotX3dPPU4DnFjrUALvO3J6TtavoHAAnswMTyL4xGnOnVCr9OmkM3qj2FPdgRzpB+ob9fVQZip5TkB8R6xg4qZnfnbRGVImauNhHTRKDLRvWfjBGU753F0Y47R3GbUe/c+22YN3iV4GeR4QO7ygedBRKlWap1I7262zsyz4REekQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: b28debd0-1fbc-490f-792c-08dd0572e936 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:42:05.5959 (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: DdHPzNCMvp5/xseSJGdYYjDRwD1QpIFPBRv29GoOPFDbXeWxVVwW/v0k2EV21JrSkV6xkgW2zKSpCbQv3if+dQegrXhQcOTDQmLLOTlu6fM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 mlxscore=0 bulkscore=0 suspectscore=0 phishscore=0 mlxlogscore=999 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-GUID: wb0EMk5x6GuXQj52zmaE5p0uDruBuhAi X-Proofpoint-ORIG-GUID: wb0EMk5x6GuXQj52zmaE5p0uDruBuhAi 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 | 27 +++++++++++++++++++++++++++ mm/mmap.c | 4 ++-- mm/nommu.c | 4 ++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index ef8d787a510c..d52d6b57dafb 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -83,6 +83,33 @@ static inline void *folio_raw_mapping(struct folio *foli= o) return (void *)(mapping & ~PAGE_MAPPING_FLAGS); } =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. + */ +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; +} + void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, int nr_throttled); static inline void acct_reclaim_writeback(struct folio *folio) diff --git a/mm/mmap.c b/mm/mmap.c index 6530e9cac458..8a055bae6bdb 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2779,7 +2779,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, } =20 vma->vm_file =3D get_file(file); - error =3D call_mmap(file, vma); + error =3D mmap_file(file, vma); if (error) goto unmap_and_free_vma; =20 @@ -2793,7 +2793,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, =20 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 && prev)) { diff --git a/mm/nommu.c b/mm/nommu.c index 7f9e9e5a0e12..e976c62264c9 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -896,7 +896,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; @@ -929,7 +929,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; --=20 2.47.0 From nobody Fri Nov 22 21:55:18 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 61E671BCA11; Fri, 15 Nov 2024 12:42:32 +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=1731674554; cv=fail; b=axrlaMwom4YiMGYiXddaIIDLuxYKlvBQrhMI1g2YYSp7HmFk8V2EuIacxH/gAdei1ebi59f0A88zJpLNCHaq7va1FTnnCbNuouOYYAG44AKNj2pbbCkut0lf2OwzvRYQbreJ8MD8Tb8jPGDH/ogYWs9Sj0vK4p5LrEtn9tRgjG4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674554; c=relaxed/simple; bh=1OkhP7Ux47jb+hySixOUM9xE0HSsT87vSJwugCA4j6Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=cSGEGZqhOrAcFCJbVhzUgXsmyKuE/I31bQqcJNcCtrSpDwno4WhjyJ4wff0p5I6kds/Uhn/Ub7oPwnrrOkRXTQYw1iabRMAu5B2VLpDItVQBd127eZzgFnU/anJLYqgSA1u6KCONbBtwCX3IUnnMhBgdH2p9KDEdiKcVEdl1Dzo= 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=CsBQWA3M; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=CYAJPZlE; 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="CsBQWA3M"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="CYAJPZlE" 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 4AFAGwPc010628; Fri, 15 Nov 2024 12:42: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=1nyxFrUZ0YkzFxmLhfWFRII65PTLdN3cwHIqE3lAYMg=; b= CsBQWA3Ma7UarW6iwsP5Jv/ZBQZN2ZxlxJCsMixNhM/mjz3bpikeRlYkcSLAjtok HOlMBCxFwvqYnih0ePBOXfdKaWjz2LXzpISCuwYfJWXXPvCAxNl7lIHT8jzW2GZQ rjpoy5+RQNWWbWuOfdNw3lC7uO1oWQlJ+lnXgrpweN/ler5wxOcy1RYFAS1pErBA 01j+i37CXLY7YpJ8fmcOA+H0/SBvF01sRdBlHm1a3tztVK1QfKyRmET7+M5hPxhk 7hTQFdkhM2mmSDr9sVFNIos5bBLJq+En5W6w7ITgC+F19j4bQEQ0z0tofcIHxobO r5JdN9/kcGmJe0C+uT9wjg== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0nwubhk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:12 +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 4AFCYhtt001151; Fri, 15 Nov 2024 12:42:12 GMT Received: from nam12-dm6-obe.outbound.protection.outlook.com (mail-dm6nam12lp2173.outbound.protection.outlook.com [104.47.59.173]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ch7gx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=D4vqstwCrrW15bIiuIb4vZbiIdInkknm5MV3R6P2wruUqB+N/7c1X+Und9EAphNlmKXPNoRL55xvN25w4rKhuvmpwvIRTpgUA546wnfe44OIEcHuL9Rkhnf9kmh5vgA60817tlPOL9XeUSqjOXsEjxAE9KQ9gzAE+u93o7zvaiQy1x0GICyk52cDkn9Xm6YiwK+UZ+AbF7AF0qC9oyzf6Vv4sdjp32zLz62D6VSZFTWfxJiSRrb4c+A3SadgfLyk8cqsK1YLcQrrkpzzgQeZ0Jpl2MD62wDlmERgtf2qx3hIDBV9pi/aiAAgKh0Bsuk/wa0ArY9CYIpyTz7fJOTiDQ== 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=1nyxFrUZ0YkzFxmLhfWFRII65PTLdN3cwHIqE3lAYMg=; b=StsOVzgmJix9XaUtGHmsaW6Nv9TWDr4W3s9u4lzh2fQEYvucDViKTIwhENWlOVU9pYEGt+gkQwehg1P4dDLeXHVD6grumIpcduU8ROAvKoeds/sdTpmpX4A7fJERUPQRLEHaksiUf7Wq0Pcq+l8o9BapzZCjfq/CF05LESpp+BWkUtQJAFvr4nElVMLHNAWtRkBY2Fg9zwyJygKzeQCy21Ml4WOewZpJvngs6rk3hwUMNt77nwoX7UeYP4Lt8JblbBgsxYsz/TdyurCa4hvWK/iIY5KMYaJLMzBraW4gqW+6x+UiqfgDpDbe9jNJeA7mHM2oQnQ/KvA/sKntRgq4tA== 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=1nyxFrUZ0YkzFxmLhfWFRII65PTLdN3cwHIqE3lAYMg=; b=CYAJPZlEAltJSdpajx9CZsX9f0PIY0ff6pv3paQu2YQ7yq0e1gJT19fMr+gMzGC+x2mJNpRN7j5yNBerRPjwkWYUW7V3/CclTKK2cBTnpVidMDbYuMS4Ya89880wEfJwgJHfjulETFzuzyHPPtAX2zlh818UCUNGAdVHg55Jl/0= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:42:09 +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:42:09 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 6.6.y 2/5] mm: unconditionally close VMAs on error Date: Fri, 15 Nov 2024 12:41:55 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0542.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:319::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_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: 22084be9-5b69-4cef-8443-08dd0572eb80 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?lRrUcOodxZv+PzWNz8gFJZ4SM+sv+P1fws34dOBC1Hq5EiRIvp90p+lmPr3g?= =?us-ascii?Q?q5oPMJSL3N6eoxEsYNN2sG2TUTWbIsGgKDrm2Tr1xvBEw/QlLHswuY0kVaMk?= =?us-ascii?Q?esFX2QHT83X4G+xkP9MWTtXPfMYG9bN0Tshubt/iV5kJUfECNZloojt6Duui?= =?us-ascii?Q?DnG/rToduMcx9FV563xo/OrjiLIOIOaXvykkd6j+9YlvW+Tur5LqzxbxBWQp?= =?us-ascii?Q?BeIB9cEGHd80yeqvKse0ssAdctzAU+FyyAhG5DLQ0aaGEUr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V0hfufL5gS3NYzRKFc5+z99VFQ+O4DthwO4sY4W6k?= =?us-ascii?Q?+KDnrORAnGTsn4tqVJ2V3BeAdw8czX62Vaja+A4PWNoOXN9AM91bvKCGx4K9?= =?us-ascii?Q?WwBUJn203udwgzpw2vRspHQTyoLF6C+Vmh4SuuhfXiRxl4X4Lb2mnRxZHXRS?= =?us-ascii?Q?HpsIkDPhR3N/cJuvtgdcusZN?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?YaEfKXswjD2RGEk3gCnNcDzt4vOKXMY3k1BsbdmJklY1neAW3qQnydfmx4iY?= =?us-ascii?Q?OkfF16ZJbtJ8/4mnQriRl28WnHCfrvWiAMDUgcXzUFIwNGbeFIPqM+vywqYi?= =?us-ascii?Q?mhYhNIPsk/fmGYbsLnSaQBTMIa48mC2BOgbxCESW/++4lLu4iGk4fyEfyoN2?= =?us-ascii?Q?YjPucfE8WrD7E6O9DyBUJSW3L1hBBzG1MsLsCSlqxdvD2eqX2q4n98Bwltfn?= =?us-ascii?Q?Z/q+dDLE+mm8SHsgCFLfRgwR3TW6wnjHAIRIXdz9ZjQL17bVHOYHwdTQsKOT?= =?us-ascii?Q?f8ZpmeCMhnO+YEM3CNw7gB2wF7jyFRLfvZKQyHSHOnsMeR4AuC6akr4er1qs?= =?us-ascii?Q?uqLxEy8H0pNeuu3wkS6PD4+dE5jOf52DYLB9gZwiAQVj4o5Rc26r4l9ek03f?= =?us-ascii?Q?O3VNFGz+rwjpw1wTPweV55L0Q+1NjlrgRYQX2u54R+B9QssCmHokDq0BF3Sv?= =?us-ascii?Q?zN3sfSe9yqlcDw33NSAvl8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/AWylN4pDa5k8aDFIDmiYL5OX8UmT7g+R9Y8q4Znvi/ftCBiFcmYiyoewZhR?= =?us-ascii?Q?JMZTY/+0pe2H+EjV3D5H6ag6GyDE2hF+e3Tx78Fz0O5Jzz/gXmMnbbhnZPZY?= =?us-ascii?Q?nT8ykgAnbik4/HwKw9+tmB2L7ZIG2cR+L2RebSekLc0gs6BEH98EcMzp2bEf?= =?us-ascii?Q?OiDcIbZmkL1MJd8AQyhVIYLdzOCk/5jDOP58Dnx8IyqCrL9uXeFTTol4OivH?= =?us-ascii?Q?vg=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: T7+3a6cmWOKNH57/k0HH+UQtJYCjwtLmuxPPdCJHAS4eznAkFJIoVQ9tNbpjmLWA6VfSjBrFxpEHlGJPJrnPaUC8y7oDRtLexHhlvlM3Kcj4lAgOyoW+3jXKWv8od9/DfjImdTLfd5uoNj8zkA1IkilfUTALD8F/o/gis8QC60WQx05jAZLelHsXuf0umQeuAy13JQQjyVE8HycMQABybHtsuSnpdUTNvQquMEPlL+dgcrMOLiICgfpRPu9z2pLwMLlqXwbYM2XSCNXUJgUOTQC6dbzIt2C5+65rnauQXaXrjTJW+IqOa95NujcQ9/APZxaitn6kqm/R0JHkNWxy/mR+FdrkP14chy96w+4CmiBEWxcyd2hUiTpEl6FRDI8kd4DZLwZAWO8GEdgi0BYEKwfb39NIOdODiyyl4lnFY5afXWXvuSOwCIAhLwJ2VGdeTBHdWLHP/RtJddqS5as9DPn1JBhZOIQNBeXX67Ksk/QP3VdVoRgIuydVWte7Nsl2S+J8gDHGNeR+SGv6Uc64fFIWyHD72oC5muAVSYSkGN/x7ciRpn2kNUlgGQmvAIQPYVIc8X7rgHUBWBpwIOty9rheX22aJywSoRxJ+Ix6j5E= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 22084be9-5b69-4cef-8443-08dd0572eb80 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:42:09.4841 (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: 6mLh82sip7T2QZQa6mVW7UtcokBSdJJnLgmpygxQSEAO7kZN3qE2AZ54S4s8zO6PnCn6bMRKwviYzzJCuID6OElagN6T2DKovlyTLRQ1qaQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 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-GUID: W4wR622cEeIvVS-WNAbRnXMvdlzSsIeD X-Proofpoint-ORIG-GUID: W4wR622cEeIvVS-WNAbRnXMvdlzSsIeD 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 | 18 ++++++++++++++++++ mm/mmap.c | 9 +++------ mm/nommu.c | 3 +-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index d52d6b57dafb..36c6693f4ebf 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -110,6 +110,24 @@ static inline int mmap_file(struct file *file, struct = vm_area_struct *vma) return err; } =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. + */ +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; + } +} + void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, int nr_throttled); static inline void acct_reclaim_writeback(struct folio *folio) diff --git a/mm/mmap.c b/mm/mmap.c index 8a055bae6bdb..9fefd13640d1 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -137,8 +137,7 @@ void unlink_file_vma(struct vm_area_struct *vma) static void remove_vma(struct vm_area_struct *vma, bool unreachable) { 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)); @@ -2899,8 +2898,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, return addr; =20 close_and_free_vma: - if (file && vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); =20 if (file || vma->vm_file) { unmap_and_free_vma: @@ -3392,8 +3390,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct= **vmap, return new_vma; =20 out_vma_link: - if (new_vma->vm_ops && new_vma->vm_ops->close) - new_vma->vm_ops->close(new_vma); + vma_close(new_vma); =20 if (new_vma->vm_file) fput(new_vma->vm_file); diff --git a/mm/nommu.c b/mm/nommu.c index e976c62264c9..8bc339050e6d 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -600,8 +600,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); --=20 2.47.0 From nobody Fri Nov 22 21:55:18 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 958081BD4E4; Fri, 15 Nov 2024 12:42: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=1731674557; cv=fail; b=ujUXT/5vD+PBCP7LBI+JnSvWwObtMwcdc6g3jkIcFbzbI6nG2HjG8wua2FS6rdpkJCrLYY/oWBc2WyFXQaVUtGL6HrrmEAnPWVMpg8Cf5qOi7X5WtnUEPZ5rJQoVsupGSF/yydT2XhbAUnw/lEzSFN0tg+540O85So9xkiC6PFY= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674557; c=relaxed/simple; bh=akgC8a+gK+lqML2mKGEIjvEIm5paQeFwTdGgIxkQk6U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=YefGKGOIU1gFTVsSw5QPHuc6HeEJkUldDva6Gti6dSKoaz5rZ0KmAaJfpeHhMG4J7Z9plmJzNtjHw9jiv1H3VFRqDHi4ZiK97qQSQJlUMkCCJJ+i81A891XToJW0+opluMecDMLVewcMMKQrK8hBik9qE4X6CJmcgqFm6uItC0U= 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=eHeNEwDy; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=tn2zave8; 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="eHeNEwDy"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="tn2zave8" 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 4AFAHGhv026024; Fri, 15 Nov 2024 12:42:17 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=g4lhvb8C2OfyVjOD4h8GKh1wFOBJ6R0iZcQUPnrOE70=; b= eHeNEwDyHA1FnAh+7g30wMpTzIVgdYl86Meq4pYUnKNVffC/gAxRaTiA0/0VwogP hHHR3ancXaWDs1b49m4r0BqabSz9BbkSu6rhYQIV0OFeTwn/JTbSoJE8EW8e/9+m lh19PUFllyDikXIOFNoKKg1+aN5FCd+doT9FTXIEeGtSQ0Ew3n/idmKj7qAoDNIs I0MYUs6l/Y00WzDi+v2UdrCjk8BgrL4WVAuFeo9uUfDYfexnfDVIh12INiwa74Q7 Gy3TW3I6BDF0Im4IM6E/PlZEZMDEFr6OAwN5Ra1j7fwqB5p7dcuInnB71d6o6sX7 JyGM5SF9vT8JL8m6y7iwCQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0heu6hv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:16 +0000 (GMT) Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFCXkje035901; Fri, 15 Nov 2024 12:42:16 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2041.outbound.protection.outlook.com [104.47.66.41]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6c564r-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:15 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WKM5cc4BHUimTlsO2Vistlhqcvx1h8AgDf904X/v7HW1Hrbzl72b42TlMUPFuB5MaaFo1N+j0sUsAjzkNNHFidtVPc/GE8KX1panlfoySf/wRqxaf/A/1kdRmOXwFIt30egLQlqXIB1TzwY4z1w6rjsN7nBmgcK1EDJzD8CMG9CCU25enlH9mHhQJFRO3RL6dUhR5j4TJFVFk2L0n54T9O7e8JeA5Y7lxEsCe+pMNXW+pAZ7+ELxIOj6pjrJxrKLHmpWe1EoAXEHJonuJpVFy0ujK3cLQ7rz+yTFp+Nysz6Zmw7uEAOr3fg9xNjmv9lI+Q/zEwa+bH3pEqwcwlWSog== 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=g4lhvb8C2OfyVjOD4h8GKh1wFOBJ6R0iZcQUPnrOE70=; b=KonPrnKZ/9ZWQs0AVCfjkC8pb/gFoZYQxvbxv4lpNK30SC5Mdd+DUJZUEgiJap2C4eyymPcum3sO+5gnWTXhrxSvXaolMs+6jqVjrU2qSykwdLnwp/ph1JsWwvSHi85IT9yTCFEeRDNJIOI3Au/3LcLOHCRo0RR9hAF40RoQixogheq/w4i1YTBU9yDX/0gm/c+KOQ5XwQ9oXsZsOVSK98sotVByVDYR+/wyxEay1vkTgcuAMs47oJfd3RML4G58TWfUK01adNPBkBegwsKsPw9xiqxg4NCyMojyvd1fHFUfvjtU0yfkOAQoenzRH2FCA/GWkHY0TpRkLch+112dGQ== 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=g4lhvb8C2OfyVjOD4h8GKh1wFOBJ6R0iZcQUPnrOE70=; b=tn2zave8udvICg63kGbBz5vOGKx7lVN23lfumr1EgXHi9utGkrp65CpByt5ednavMT8B1jbGNn2ngn/xuFq6xewuY9+V0iKfYt6cgzlK118jJ4rvaOZZDDMGl52wUuPEauXd1G23PfGH5ivyYJTDGhqEuNMbiL+nfEGqHr4MoiI= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:42:13 +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:42: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 6.6.y 3/5] mm: refactor map_deny_write_exec() Date: Fri, 15 Nov 2024 12:41:56 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P265CA0025.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2ae::10) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: 3907286c-3672-44f6-e2cd-08dd0572ed94 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?SVg6jZPGWE+h/y74Q112aSiDJoUx5mcdcKRWuTLOibK5X2NCndx9lrtkEdm8?= =?us-ascii?Q?g2N1Axf8I8OjetHITHNXLNxuNEo6AaOrHHmwvT73uPVqEW9sVPjrnh496Wfw?= =?us-ascii?Q?VWjjD3H94DXy2XWbcBRUOxwXOumHXegywJm1yKGKe1J+H0gmDNeERku4jSNF?= =?us-ascii?Q?zxqY1WS6F2SpyImFb4Gh4dH5tu/mMIR97pdW4IauO3+i94YgNbMyLF5tcKpI?= =?us-ascii?Q?TgaQrmQ4np7sCRxc9TZIMv1iaFPsS9Pri794p7c/KwU3ZbQQDwpxr3axupym?= =?us-ascii?Q?xeePQUunDizUQvIq2xq+uC7DyNFXKfPcqkNYcXXIrbx9a9DBeFHFr2UbIxIg?= =?us-ascii?Q?6/PqYA2I7iKj3pJ3xLr0uGfrhhNtqm3phEVWkcYyJWs7LZlHTzl+JOWrn4+Z?= =?us-ascii?Q?NWWwQGXcCuUW8j72Euo4MDvZxLC3X18MgoJoMNrRA1GK2FpOuaPc46uJpLF5?= =?us-ascii?Q?AwhPhnzMsKCLyshL7YC0eyy1kh51TLDtwA8rBP0URgaPB0I9TeYG8v0FHjUe?= =?us-ascii?Q?1QXo/V9j9jt6sWShoPRFoJW458Z5Nh/XyxcmHC7FxfFqgK/qX3pqyy1a0v7Y?= =?us-ascii?Q?PSZjBX4G0nO2CCsCCPat/Fn8j28oNr9WnYjvLt4pw3JHKRZ9askoVba+CIup?= =?us-ascii?Q?h7NGsHW+3w03qzbA07Omg30UnJzmzdg9fGvxKWrz6odUAKJqaVb/aE3mi/+n?= =?us-ascii?Q?qU9xVDn3UvtmnxovPU3wxK/63gK/3jH0LLcgD19paCWzL8O05E7RAT0Cw2Jz?= =?us-ascii?Q?dVKGR2WXq+OswCcGVFO0AMueX1FhI2ceh5YazNOxCwt1ox1p07AbvNqtxHAy?= =?us-ascii?Q?GnfOupWZUS6TDmQz5XCut8pk9oglz34/MHrzNwppJ7Y8L7SyYJL534hyDQ6f?= =?us-ascii?Q?YmAZUrznPDge0xcOUpNoiKjLohGIeUjb1FKnbT+1fUxqLdaiUm7brQA4h9mo?= =?us-ascii?Q?7pzpHT8LimPYHyyOwN1kEP3Gl7g0vluPPEdUC4cz/hGpnVLrrznKWPpt1YnK?= =?us-ascii?Q?E90XUaiV6WUW3gOX2jR3zkQDmuP59y2MS7qo7dOYaj3dLD2mob79TuPUPjgu?= =?us-ascii?Q?eg7mxiBkHoljjqYpitsjXibSkl1CYGVU2P0lOjJeuLYZLP41Y7k/yM27/fS/?= =?us-ascii?Q?oRIOBKWeSLf7R/C6YZI0A7G68lKiJqzsc2o/KCS+L+NIypNWeqbrTy79cjID?= =?us-ascii?Q?aM/iTUf4OxaMiXMBnaDicD020lXI8IkUm6kOSvzRmKMkGVzpVC7890VmbNKX?= =?us-ascii?Q?n5ZsLr9tWG9s8n38kVeW5AuQye+wYku6MA3giL/9bQNkNT1DgpX8c8bVGviS?= =?us-ascii?Q?ZNl2OK/VrB818E9f8ROFAAT7?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?04qYkFAIkscjJsrbWSRYQBOZ+ou+38D1jCxxM0m+VCbdxmBnvfFpPSmJEUw6?= =?us-ascii?Q?0m9Z1XnyNTMwJtoYLo8QAfaMsQerIKLSqx+3F8Pxn4KWwmrHExCobNv2gQhM?= =?us-ascii?Q?2cWYAyMCOOnwhzedN80TVKEPugtEdNqm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ascii?Q?R1Kf27aQrQQLDp60dwtFbOHdaeZhB0uTCIeaCQgJRbNkIGaNqoOKNAHP+mWH?= =?us-ascii?Q?rMs6zzLDZu7Ks0vIvSav7R9UbVL/shmBGRNYQFLpusXZReTMTLDYLlRvlbWB?= =?us-ascii?Q?B1vZ2Y7UIp7nBnlbN0eiFoChncsMdddrGhrqe8Uqv87rniTCGUxttFJQnjFT?= =?us-ascii?Q?8dWVmCb3d1nDyiK363IRKU7aFkwS3awmkhwwgi3wm3lCuYi8HfEvzHBtskGn?= =?us-ascii?Q?drzBGNmo+Rw9wqLdatQOmVeTdGCIhgDXyCPAI4Q17PPhs3rVhIz1vBcmqCmD?= =?us-ascii?Q?p4W6nVA8iBwBL6+AYs6xwRsIoFE8AwdJOJoa3sVe/ZYfldZIb2qU6PZR2Ahs?= =?us-ascii?Q?wYCUIF9KMiBdMqO2+h3f67DNY8fcr9hi1QUT3Se59TaFVC8EPp4jjHEbxEfn?= =?us-ascii?Q?KTssW+hcCHBC2p5cvZ2wJ1QdM8CwLVAPOZsOP7cRRPaOos2O/2okJWTWe+ol?= =?us-ascii?Q?GfWIdIrUiu+5RWNDNPPs/nbEViAU8l+6Hl3gUYRHjP6UP+opgcbEncx9m8Vf?= =?us-ascii?Q?tuls3Ul5ESKw5a/dD0YmqXEqBacKxNpPVwAAWuKAv6dQR+a6iVNQtf1704Xx?= =?us-ascii?Q?9g=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: rNkA7EFuysRf/8MsF3LgfNc/yhctiIdQ+qdU3ndOUlJsHEGgDvQjBa1X1KfamlZezCshfZLkZO/XRAwTfPDEbVa+gsL6GuK9bVExFEk3PWP1J5lk94pXsVn3gXt7kTZJbkTEpXsewRYB5wwkYEbCShHxOu1Fgop9ISMF4t88ApHFNJ8ccvLpytW6CV1FHdCnE3iCzjER2ig9CVvFWXjKXM8DjTDG54kvMV5XNkmGI71uuD+2aRdeNQBdybypX4PIP90CPbahR3SpHJb5tci68EDBomjBwiUU1CKZWHRs8bwIv/Fc9pSgDiEtBznsY6MmN+MmXyOVT/fsG6LzRCCYU/LmvE+sAWuOEOliYRkaEV5wTMLBysYXlTlfiXXP5BeYidVibvMVLyjQOwMaASUQqot8YLjveDJ1Ew/2mI5fTnQykfRsa0Qt2pkOGkzEyFc6vtFeU4dmeUbLwNx+lxHaDB334RYZv00qBORPpTg4mpxDWPUDdkNwjoHmy3xWq7oP2zvQTQZYO13zYL0mrATcUt2qGx+QBWdjuwX9CgzWTtyV+ZSj6CRa+Ak7W1W2Rs6Uh2+jXdpUOXrPP8A59k56pTHguuLcsKrsZNr3yc95fWM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3907286c-3672-44f6-e2cd-08dd0572ed94 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:42:12.9396 (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: W1eetwGaqOXKQ+UyOlevgfW3NYlK212Zys3177vU/jbfs7Fhv1ZaV4poBOqN9AEUgqc9f2+wTmKP4+MoPuiMTRtw0VqGExsLYdUeKrNObcU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 mlxscore=0 bulkscore=0 suspectscore=0 phishscore=0 mlxlogscore=999 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: DsbnIAcLJnyFsgQ79_RAcNtzr-Zl73-N X-Proofpoint-GUID: DsbnIAcLJnyFsgQ79_RAcNtzr-Zl73-N Content-Type: text/plain; charset="utf-8" [ Upstream commit 0fb4a7ad270b3b209e510eb9dc5b07bf02b7edaf ] 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. Link: https://lkml.kernel.org/r/6be8bb59cd7c68006ebb006eb9d8dc27104b1f70.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 --- 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 db4741007bef..651705c2bf47 100644 --- a/include/linux/mman.h +++ b/include/linux/mman.h @@ -187,16 +187,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; =20 - 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 do not accept newly writably executable VMAs... */ + if (new & VM_WRITE) return true; =20 - if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC)) + /* ...nor previously non-executable VMAs becoming executable. */ + if (!(old & VM_EXEC)) return true; =20 return false; diff --git a/mm/mmap.c b/mm/mmap.c index 9fefd13640d1..d71ac65563b2 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2826,7 +2826,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma_set_anonymous(vma); } =20 - 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 b94fbb45d5c7..7e870a8c9402 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -791,7 +791,7 @@ static int do_mprotect_pkey(unsigned long start, size_t= len, break; } =20 - if (map_deny_write_exec(vma, newflags)) { + if (map_deny_write_exec(vma->vm_flags, newflags)) { error =3D -EACCES; break; } --=20 2.47.0 From nobody Fri Nov 22 21:55:18 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 B6B571C07C5; Fri, 15 Nov 2024 12:42:42 +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=1731674564; cv=fail; b=L16IjFw4u7sM3vabAhJtQaa5uYN37rnc/C/qiCxhYRj2yVIm+Jz2DzU8BUE72oChSIFQsa9l0sjL9kXlZVxlTvjn5hCzlgwa3H0b2zwm090kw+QqnT1ZB+lc4x8pH+4jwIxAlEt9c520WPc7bp+4oItYjdQyY4cJwErdPwmZIRQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674564; c=relaxed/simple; bh=3h/WOvLvl97KLmx18nzqvwvy1syzRiyUvzNQgOCw0cE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=Jq4YlzKkMWRcrmTBjPwnBUjW85+fOA4gEvYmR6uXY4MDOK362INct5HMlOHtDlU4zUWtStqgtTYbp2mw9PjkGcRpv4Dc5TGTdJTjH4jLMdkn7nhdvTfTCMRADIY89TJQP8t1UNX3KALy+bcCcQuqhXCTrGZaYyONV/0A2R0rJ1A= 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=GkttqdLn; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=DwsAraHz; 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="GkttqdLn"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="DwsAraHz" Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHBL1021442; Fri, 15 Nov 2024 12:42:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=spnAHCCRbHps3QlTZk9KoUOkxA5rCBm5i/Uk9iQm9cQ=; b= GkttqdLn0EKqwLEZII9F2CN9sId8EnPNvpKOkk8EnAJCMLwr61/npX0nRK++zoaU 2S0V04UcrmbevrGRG8q7oOQFkD7xZFbVSG5HJ156XHipRu53bojL4k+kPlMhoGYV g9bdkiD9R85GZNfFUQkyCJ7Tuh+IAP4FxkucuKuVAAkubUZ1Ee56sFPBLBwT2xBt 3aOjUb3oFJ6FfoVvX1KkIlgONvwBUxKIbbXLNk4bg2lPJvkTu76Dl2RPLzucPotR SKu7O4JU4hMwAG+JjxW9fNBzZx4S3kIzvRACgydWF2dbl5y1mH1oIFIkm97jDazF YYUoOqJ6/vd+vdu24b1s8A== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42vsp4n16n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:21 +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 4AFAe6R0005765; Fri, 15 Nov 2024 12:42:20 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2045.outbound.protection.outlook.com [104.47.66.45]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ckve9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:20 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=maS2x0Y6LiBP3PpOkYDiKfVOdDhx2XAjdzS5LiD8xETMFxMdkceirb8eu9mQp5yB8GxmDi3VmpleqFHKFqozX9MXmyzJaJvteJufsfZZpLaUJDIVqfZfSFf1zIOLA2rYsLJB7NkZWpSMgdZ3eH+JSE3+RkaOMXO8DGNoUghoDYwkDt3R79vJ4SWOvmzhdfLwNMlKIncIRC44pCdThz42EbMRwO54DY1OlLI29Wa1eK6qrQZA0FtOWGmW4DBTf1bBvXX6b7c4fv7cv0kpo4Wpq5dOjweCIJhATsYf+XU8U9l47ftUD5syaz/v4oECkQB3I9q805R+xMhzJ+NUbhYELg== 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=spnAHCCRbHps3QlTZk9KoUOkxA5rCBm5i/Uk9iQm9cQ=; b=NuN24qKlNFPM6WLF+ZXYivVvwOjJmFCBrCPp9MO5cku04Kff7aZFvFVIxBwrfSMjIK28lJdhXrxRf9GIA4FQBjIwiKUb21gKsj45Gphb741lL/8nMFXBu9x1/UlQPn2lHrVGZHpPHwjxKzD7Uzt8QwM+OCFxF5Q9id2p+hr7iKqeLkykgqOybhglVsH/XTQ6JBw+zlv+/c1ESIxshumrBFfYUOyo9E/CNbxPXL6IPVj5fd+2RidRhCf6fHT+Jq1oiVGm6FzCa+B31+Dcoivz719FuQtmYTd8HPK555KsYLJKp7eyYJ2ujQvAWT4ZEnVXHGo+aoZw7sys43+X5SzbIg== 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=spnAHCCRbHps3QlTZk9KoUOkxA5rCBm5i/Uk9iQm9cQ=; b=DwsAraHzf/gzgipfJw0qWPlV5x8dDOxs0hew64hF0ZtJI1hvyEt5aOO4eMCBbJJn4CYqLLhFVVaWX95jPji64853ZzGvMm7WGa9d6EMIsyMbElOPY/7SOdaIEXjYvzE6a0iWjNwBD4Noc5PL5QzOLAbQaWm8VYVMsjqBLgHWno0= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:42: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%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:42:16 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 6.6.y 4/5] mm: refactor arch_calc_vm_flag_bits() and arm64 MTE handling Date: Fri, 15 Nov 2024 12:41:57 +0000 Message-ID: <7c0218d03fd2119025d8cbc1b814639cf09314e0.1731672733.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0036.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:151::23) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: 0312004c-47cb-49d6-acfd-08dd0572ef9e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?dgTI7/rkKOyZzNkmUhkYEl+nOYoBKmyWIqe6jDyxLsljNMmpMgHoMj8IlsGZ?= =?us-ascii?Q?CrvwmAK4PbFqAMpxWiRECfpYJG9fyuMfH9RC/PJAHZrPoeSPgIr47hWwFWF2?= =?us-ascii?Q?zsdH16IIdDzd81Z8gLZ59e6kZJY8PO8A1/8RLRmDTQx0iQGYWiJMCIn5bDTO?= =?us-ascii?Q?ttUN8ExyqkJEQMPtlXhkRgV2TjIBSYoMNRupuen/ScaxAmpMEFCO/n2mV6pl?= =?us-ascii?Q?fmPrwYbQt926w27XBSLB7IU1CUhJ6li0lyBtdgK40SDHhh/WBtLzLK/12Oxb?= =?us-ascii?Q?gwpm3stB3HZezuzXLuGM8FFKP61Kp2Pnm8kw/OzgWfAM1sTwRUxuUwTNB1dq?= =?us-ascii?Q?+pgT8qp/BB7b/E9Q0MYwKYBM8a5OPFXBAO85HaZgc1WIVt8aaHd6UNRKhRdP?= =?us-ascii?Q?opRTZje5SwdQiLYpXGE8GMmkreTDReBsUESbHmwD/ouY1N+/NrKBILDDotte?= =?us-ascii?Q?BMShGz6qmDOT+MkCZFA7Omd1UCV93eV5HYYRrXVfixV8USXz7LzHH4KIijFd?= =?us-ascii?Q?Ol3OlCQE+JPYoli3sjx06Bq5Ir6TDx2Nu+1XfnSB+fTALVMQ1SHCT/Qp/1hj?= =?us-ascii?Q?NIzyRGe4WImkAeO3JkwG6voE76FwXENEMUmDRVgjd0Us3gJWzE3hKe4j1/qD?= =?us-ascii?Q?h4pENe0dTZKXVBv57CfqPul2GjrjC9CBXmIDkYBgzTKklibw9HYFoohP1ZAV?= =?us-ascii?Q?Yp9JzAct890esX0zwT3GVnXb37Ac4umUXuCkah5KiYwPTwZePOg33mEgM1Bk?= =?us-ascii?Q?cAiUl9on090JmLbdzJyLP0rmgxOgPh3reD/jNdqXxSwI5xXtYOeb3fYTEuzv?= =?us-ascii?Q?tjyUgkfL5crZvBsiIc9rDQBJjqHSN3jOkAqOxsqr2wQlGwmWWTsWM6sn5/26?= =?us-ascii?Q?OgWIF7RllskFPnA/nahAgCSKcMAPOArCCtL4dpklEIuL9i713RtWtRJTwE9x?= =?us-ascii?Q?7PSo9rQiqF5Up6re2vct6O/DDn9zFa01lwWW7Ry5VFRFF72B37Sx5rY5AWB4?= =?us-ascii?Q?bKrePmRiqjWE6COhbe4jAIDjbqlOb6Ji+JJYLGIyUO53RwIIlMJgXtWYzAGN?= =?us-ascii?Q?+AGVnE4xJ2rSWe/k9u3nAzDVVYJ56PN9K9zqzVcveGt5w2M30S3kj5hDkk1E?= =?us-ascii?Q?qPmlu3ZLMkUCqTsYAmXijVYgvTcVKv2a2IvzukoSU/qrXiG9GoW1LkEtoUsm?= =?us-ascii?Q?RkozUzeNxpvzPV3b2RAMgqbCDDD4+r7H3rLR2p5rha6lYsdX/mGMhWAl9h6q?= =?us-ascii?Q?hRL89Qk/sYi0ODZc07P5Ot6GqosKM17xdWbRMh+0hGrLgYxjrd8H3kNEYIby?= =?us-ascii?Q?lNqZ2PAr5k7dFbrpH0KUBEXn?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?9HU+N19vEJl2+dCbk+mDd43BICK4Qp3E3uMCr5cl3kJuhVF75TXnCU160H5E?= =?us-ascii?Q?PnT2pvXmVWiGAOYBEyVduadxTp9u454nFg0k4y4Zr9RgmD7Lt3ZvGGuOFw+E?= =?us-ascii?Q?Yx3e7I0Hk0uierJf9sgOZsRIWOme4SDqxnv4N7rn6HSCa+0vhKCmqZeeMpit?= =?us-ascii?Q?MmN1Qys4XUAEnEHSSDw9ZS+YyXGCK2e+rM3Om289F8Omiuo2L3Zg1oL6Seit?= =?us-ascii?Q?+emqsz+w+Dt4z0IQFp0Y02df7qrewN87A78AKFlVnI8KAAdJaLzVFV6kOkwN?= =?us-ascii?Q?1SYHETx7E0pKsedFoWb9++bO3j78QVmynAeDpGm/UX556MxSeKbk7s9ChMV0?= =?us-ascii?Q?vMwRmri8rvAQu38XPfTvmcieKb87gJOdcrn3VUNZQJk7vmpVZBkyxDOJSZ8g?= =?us-ascii?Q?asngfGlVveZ7j9fcsRMY7PfulTYTYIveLLBwv9ImZywbJFVyK+hqe9jGQFA8?= =?us-ascii?Q?gYp37BOpmtxt/zI+oQFrYF7lRI9EF7PnUuOfdccQZsi9pkEiAZSnyg1ZgSSM?= =?us-ascii?Q?YC44w6OeaC6HpDAItsyGmrY1v4n3pPCBtHNpSx4sH9x7gxUHoG7odR31iJWL?= =?us-ascii?Q?w/YcT3eT5sqqTzHYNasb44/JNKcVl5rpk7TjgS5cS26UZpzimU+XoXdR78Jw?= =?us-ascii?Q?ETqMgueWoZRXq0Rt+Kak/WfNnZjKmENmRtWq6Q+1oJ9AC69e6WBvpMHDbYER?= =?us-ascii?Q?VupF+0SH3Dofj8k8aSOG656ejtURFrcSxfLfQiv7kxpJ+IR63lBFNNpDAyFv?= =?us-ascii?Q?ggsd3HpAJ6BnCr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S-Exchange-AntiSpam-ExternalHop-MessageData-0: TpCaOwC9/y5j8RpCCO35u3LRuiDG/P+NnLEy2Yqb1uMCcuq72l772GIq2coPkIgYMqo/27k5xKHcw7WBGHzVl1Px9aNn0dmoUKAmS6YDqQqOjhVnhT2r5C1NkMrnZ8DWCpS+21KS2tyKMR8mjTv1aQd0+Y6siA2Dvl49dan+ZDcwgGhKgFniKvbiKrCOwN3BBl+L5SlXCFqMBYZre4bYnGobKOU5QctKjVGJZWx5aCC8AkOHXBGDyVFk4s1MbZ0jEzNsY0u9nu0laI31c2Ddtq7R4e0YU+S0L1KElQpUQLArn2BVnPmZDAbVAtFKMZh+kN1YeALY4a91gjbDdQFtu5bJx6/q/YG1qFWyGHPsM4CnX+Yia4GPdNYB2QP1RVCTi/+TnP+cUWL7UKvwzsiZ2qi+RNCUnmptBQRaHiiMRDcy0S+Gnl1M+ZnCdyPOcGWK8mIVz/e5IiquzSHMh3D2Al3W54lQu7l3iZNKs1809xW6hYH4he96ReTMyNBXortzioZQshfJTJ5ICQ6kX5uFhfIdICSxKyt64mcHffaNu0ZifQqqpRkSy3dkxysvEtGce3RYIv6wR0tJAPo+hgd0H6WmuWk7MiLgr/S/gg9TgPI= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0312004c-47cb-49d6-acfd-08dd0572ef9e 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:42:16.3598 (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: IMHfufEIIX2jE2jUTH++hC9ba0U2h3Kw9wiQ4EOagKiF2G0bDEHTDO2o+hWMQSed6gZOpG1jg7eF+4lsNlDLMa7nXsxOLdV38cB+SXz5pnY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: RVwjD-P-eMj8lzbvHWkEcg-Xd9D5K93J X-Proofpoint-GUID: RVwjD-P-eMj8lzbvHWkEcg-Xd9D5K93J Content-Type: text/plain; charset="utf-8" [ Upstream commit 5baf8b037debf4ec60108ccfeccb8636d1dbad81 ] Currently MTE is permitted in two circumstances (desiring to use MTE having been specified by the VM_MTE flag) - where MAP_ANONYMOUS is specified, as checked by arch_calc_vm_flag_bits() and actualised by setting the VM_MTE_ALLOWED flag, or if the file backing the mapping is shmem, in which case we set VM_MTE_ALLOWED in shmem_mmap() when the mmap hook is activated in mmap_region(). The function that checks that, if VM_MTE is set, VM_MTE_ALLOWED is also set is the arm64 implementation of arch_validate_flags(). Unfortunately, we intend to refactor mmap_region() to perform this check earlier, meaning that in the case of a shmem backing we will not have invoked shmem_mmap() yet, causing the mapping to fail spuriously. It is inappropriate to set this architecture-specific flag in general mm code anyway, so a sensible resolution of this issue is to instead move the check somewhere else. We resolve this by setting VM_MTE_ALLOWED much earlier in do_mmap(), via the arch_calc_vm_flag_bits() call. This is an appropriate place to do this as we already check for the MAP_ANONYMOUS case here, and the shmem file case is simply a variant of the same idea - we permit RAM-backed memory. This requires a modification to the arch_calc_vm_flag_bits() signature to pass in a pointer to the struct file associated with the mapping, however this is not too egregious as this is only used by two architectures anyway - arm64 and parisc. So this patch performs this adjustment and removes the unnecessary assignment of VM_MTE_ALLOWED in shmem_mmap(). [akpm@linux-foundation.org: fix whitespace, per Catalin] Link: https://lkml.kernel.org/r/ec251b20ba1964fb64cf1607d2ad80c47f3873df.17= 30224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fai= ls") Signed-off-by: Lorenzo Stoakes Suggested-by: Catalin Marinas Reported-by: Jann Horn Reviewed-by: Catalin Marinas Reviewed-by: Vlastimil Babka Cc: Andreas Larsson Cc: David S. Miller Cc: Helge Deller Cc: James E.J. Bottomley Cc: Liam R. Howlett Cc: Linus Torvalds Cc: Mark Brown Cc: Peter Xu Cc: Will Deacon Cc: Signed-off-by: Andrew Morton Signed-off-by: Lorenzo Stoakes Reviewed-by: Liam R. Howlett --- arch/arm64/include/asm/mman.h | 10 +++++++--- arch/parisc/include/asm/mman.h | 5 +++-- include/linux/mman.h | 7 ++++--- mm/mmap.c | 2 +- mm/nommu.c | 2 +- mm/shmem.c | 3 --- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/arch/arm64/include/asm/mman.h b/arch/arm64/include/asm/mman.h index 5966ee4a6154..ef35c52aabd6 100644 --- a/arch/arm64/include/asm/mman.h +++ b/arch/arm64/include/asm/mman.h @@ -3,6 +3,8 @@ #define __ASM_MMAN_H__ =20 #include +#include +#include #include #include =20 @@ -21,19 +23,21 @@ static inline unsigned long arch_calc_vm_prot_bits(unsi= gned long prot, } #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pk= ey) =20 -static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags) +static inline unsigned long arch_calc_vm_flag_bits(struct file *file, + unsigned long flags) { /* * Only allow MTE on anonymous mappings as these are guaranteed to be * backed by tags-capable memory. The vm_flags may be overridden by a * filesystem supporting MTE (RAM-based). */ - if (system_supports_mte() && (flags & MAP_ANONYMOUS)) + if (system_supports_mte() && + ((flags & MAP_ANONYMOUS) || shmem_file(file))) return VM_MTE_ALLOWED; =20 return 0; } -#define arch_calc_vm_flag_bits(flags) arch_calc_vm_flag_bits(flags) +#define arch_calc_vm_flag_bits(file, flags) arch_calc_vm_flag_bits(file, f= lags) =20 static inline bool arch_validate_prot(unsigned long prot, unsigned long addr __always_unused) diff --git a/arch/parisc/include/asm/mman.h b/arch/parisc/include/asm/mman.h index 89b6beeda0b8..663f587dc789 100644 --- a/arch/parisc/include/asm/mman.h +++ b/arch/parisc/include/asm/mman.h @@ -2,6 +2,7 @@ #ifndef __ASM_MMAN_H__ #define __ASM_MMAN_H__ =20 +#include #include =20 /* PARISC cannot allow mdwe as it needs writable stacks */ @@ -11,7 +12,7 @@ static inline bool arch_memory_deny_write_exec_supported(= void) } #define arch_memory_deny_write_exec_supported arch_memory_deny_write_exec_= supported =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, unsi= gned long flags) { /* * The stack on parisc grows upwards, so if userspace requests memory @@ -23,6 +24,6 @@ static inline unsigned long arch_calc_vm_flag_bits(unsign= ed long flags) =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 #endif /* __ASM_MMAN_H__ */ diff --git a/include/linux/mman.h b/include/linux/mman.h index 651705c2bf47..b2e2677ea156 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 @@ -94,7 +95,7 @@ static inline void vm_unacct_memory(long pages) #endif =20 #ifndef arch_calc_vm_flag_bits -#define arch_calc_vm_flag_bits(flags) 0 +#define arch_calc_vm_flag_bits(file, flags) 0 #endif =20 #ifndef arch_validate_prot @@ -151,12 +152,12 @@ calc_vm_prot_bits(unsigned long prot, unsigned long p= key) * Combine the mmap "flags" argument into "vm_flags" used internally. */ static inline unsigned long -calc_vm_flag_bits(unsigned long flags) +calc_vm_flag_bits(struct file *file, unsigned long flags) { return _calc_vm_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN ) | _calc_vm_trans(flags, MAP_LOCKED, VM_LOCKED ) | _calc_vm_trans(flags, MAP_SYNC, VM_SYNC ) | - arch_calc_vm_flag_bits(flags); + arch_calc_vm_flag_bits(file, flags); } =20 unsigned long vm_commit_limit(void); diff --git a/mm/mmap.c b/mm/mmap.c index d71ac65563b2..fca3429da2fe 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1273,7 +1273,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, fla= gs) | 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 8bc339050e6d..7d37b734e66b 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -853,7 +853,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); =20 if (!file) { /* diff --git a/mm/shmem.c b/mm/shmem.c index 5d076022da24..78c061517a72 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2402,9 +2402,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 */ - vm_flags_set(vma, VM_MTE_ALLOWED); - file_accessed(file); /* This is anonymous shared memory if it is unlinked at the time of mmap = */ if (inode->i_nlink) --=20 2.47.0 From nobody Fri Nov 22 21:55:18 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 40F8A1BC064; Fri, 15 Nov 2024 12:42: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=1731674565; cv=fail; b=E3X3SF7JCyZNnNGuZ86YLBO56GtevOZwc2mUbdL8iz5XqCk4zN09LCzRw/RyFEwfZ/Pu3XzNL92hKSHuxwhEWjGsFfEJrgfGtyvYpeFuF7LnWixkvTV0lOn9mUJL5TysOjAOaPYNZ6bXnclaQPj8HRthZmcSn0CUcwiyf9Kk4Z4= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731674565; c=relaxed/simple; bh=uLQYpBq1iO+Mf2/e4Id0no0b8V9EgNxCwufQfycSqrY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=hErf1gyXS5RigjdRToGbbmWG7cyImSIDzFvmaBpvbO3neSQdf0NSCRxCGofLAwValdo6md0ddr2xBuVTYXBDaHti1071Rz389FLrnSNScKJKuxxriw1rKe3YY9mD6Jms3i6OGZmkFbmciRISxl7snlW6xPsyLUbgSQzpNtLp8QI= 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=dVCXG0jz; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=WMPMPZVR; 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="dVCXG0jz"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="WMPMPZVR" 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 4AFAH0Gr014175; Fri, 15 Nov 2024 12:42: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=0TwxBJSLcC4galjZladP3skso+2UgAA4dyxp3qrUK9g=; b= dVCXG0jzMDL0qrPPbJBQYZSch02bxa61nvtDqVwb1R065qeNGUlS5dyDb7FwC3px V77ov7Ja6Ukx/HVEp45Xd/5Py/EIOMf80z6Z6c0MDmZ685CY6qxMDwXJ6Pppk8HA JLkLFrrZsNEGmxyeYgCX+EdvdaZ3pOdCgV5WE+VpCuSs72MIL60vkO1A5Bhzxcrk a9VoFjV+4+KSQh8LIoEaFwYtRpseEQWPGk+jl8zgo4h1s6DHPAKTafBRh1PXZKql KbThgQnpfuZUdzeRLV1XkK7164KJ3CHdAQ+ynD9im+Ng5ixFXmhwbf1/FjreR9V6 K50CloRg6B+fTIyQjc8YAQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0k5k5yj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:23 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFBtqeb022740; Fri, 15 Nov 2024 12:42:22 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2047.outbound.protection.outlook.com [104.47.66.47]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42vuw2mtnv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:42:22 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=t52z7c01UOjJ5bJ/324plUaSmuFrQNdInzxtI8jUAVRzv3hcaGPqJC089Q3me3SvF+2tYao/9seGFdzYCsQCHmYVrNke69e0Eazy5ne4xT9PSCYm1zlJcDBFCU4J1PJeej+LlIESb9MYY5obHB9aPoi1icepcuemZ3N3tXs71DyN6/g+eIh1ijjBcYAV4zYkYcF8s/LIZfPhlyzWBEyaVahmQUqI30R1/O9k/1gPcDtUmNamBQpBHuZyZHTMeX6fCkz+QNgPFGOMON+sQYte7yJ8jOf7lHijDNGuKKAdo7G2OlULBVodwPMHrJNwC+gXsKdgEQ7epmsXhl9QMOStug== 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=0TwxBJSLcC4galjZladP3skso+2UgAA4dyxp3qrUK9g=; b=OWxZYKGpN/CDYuE/kGLCM4RECBkkbeyoM0zm8Kgpy8SRODSp8PWmy9M1iMAgoH2VAryKBz95BY5lDa8yjfJD7DutLnuaxfZvZoBoMz6otOcpluk/Eaw6JT2abfieIdP/9KMVeyahg2z3OhV+kO9YpilN7isaYp0+8rB4zBuyaj653ZMlo23oeUFYLwys5TWu+qgAaLI/5UJFosYwyVJAqD1txhJJHHT/8f90rHy0xXq8iGKSSpnkQE/LbHebtcY9b4J4ZLbtvWsr7ZUwtny//jdCxJguMHQ9T6qeiEfbUzSGtMPq1QNB3PYzGnATCd6+i4y1gLMJJM1bWNG2xgGT4g== 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=0TwxBJSLcC4galjZladP3skso+2UgAA4dyxp3qrUK9g=; b=WMPMPZVRMbgbO9pCwuNHV1QAtBblWk1x4KxscvLJgI7hAPOT9E8Lzou+ghMGGDfwrp4SJkUKKM2aQXibirFjUYXBBIc+UBveeMduunrxnSAMkX3RuFKrWBABs/DFxpw3yr/DY0dhXbmhKNnU3yd0vwqBBZThdmWNnE68LMQavi4= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DM6PR10MB4201.namprd10.prod.outlook.com (2603:10b6:5:216::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.20; Fri, 15 Nov 2024 12:42: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%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:42:19 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 6.6.y 5/5] mm: resolve faulty mmap_region() error path behaviour Date: Fri, 15 Nov 2024 12:41:58 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO2P265CA0102.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:c::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_|DM6PR10MB4201:EE_ X-MS-Office365-Filtering-Correlation-Id: d4067e71-2fa9-4653-36b3-08dd0572f166 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|7416014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?rnM4R5DjMiLmA+papYF1WUsA7g0XnYs5natgwjGSpp/3bpoZjUYn7qiRLm1V?= =?us-ascii?Q?kWIPQQXmh5fvBZfXx7SXS4cfgrwo0+ojM5MFpjDIkqDATouCTliRZwAIVdhv?= =?us-ascii?Q?RbaECLcbq+aTb+aERfsrCxq6H13GS9HuJ2WRgddcrRm5CQXa7WDQt/ihxDD1?= =?us-ascii?Q?A9VJ/4f9QOw1+PdG7BI2OPve08jCUgWGjoA7hNNaOB6Notbb9mmaoJViaTRZ?= =?us-ascii?Q?GkzYgdPrp1KKqca4llrZOWAmdlBkUVf6Iv3EaDctluuMyUCWer+OwbQx8RFo?= =?us-ascii?Q?wauTrg0VusWC6KvZiXULy5Srgsn0chOLL/cVn1GsnQeknTYUcX+z7vz/Sfv7?= =?us-ascii?Q?xi2+mShzegwNlDtN6jvHQr2wZXx5PsrLw2bGt0/9Yn7jlalDLlwZ8MOZe/lD?= =?us-ascii?Q?s35YACtKvT5hQA13FapobGtmJRo+Z2Fny4VE/0vtvPWMw2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q?tecwjS1j4akf2aa5f+j9vTMY?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(7416014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?kVTTCYSNTol9yfi3dgu25fbjFqd82rgUyQbtbipDCnWtd5FjdmM+PAlv3uuN?= =?us-ascii?Q?FucKyp/comdQCHfMrC2UKCxSR1vOdQYfTaIz+wEAKIH85a07L8ILYRzNqrpi?= =?us-ascii?Q?D4MUeY2oXyq3vK4+XvRxkEEWI6OSufJhjKQoLqISLrnRBobxt5Z9pCL5Vmc6?= =?us-ascii?Q?jDvI16IEPsiIFYtW9bFPNm7u5z+AdLHP33oKy9JjqGR7AFk+F2ynQwEOetSZ?= =?us-ascii?Q?qFdougchRj62MEgl9A/ZsYIZ3wu6PXe4IpJge9N/dQuUZdACIegJOBSGhPVe?= =?us-ascii?Q?jSIBUORF52NhtLQiKhsZNbwRXQm/cUlDlsSw/gIbis4ommT4Ju/V8GW0aakP?= =?us-ascii?Q?ABj6Yao1FfC/szGNIkSo1L2IJ9YcChsR+qRyvbH55zu9q/TLBLe7UDB8dj/q?= =?us-ascii?Q?DgJ2dy+27WFkTpL4DwQjdmTInQ0ODfid9PuzYcRRzmAUCeVL4i2C50ThRfRE?= =?us-ascii?Q?IyoW32/4wae/tgUJTd9rb8kEKIWXHdzJ73d/fwv59NPoPVeziMQb67ybHTLL?= =?us-ascii?Q?brYqb1dJt+CYmwXvoexszXXh6RPTGk+Pmwr9G2y7YFQxxbRQTwh9AjLz8MqD?= =?us-ascii?Q?ArWSqR8H4qAg/n7w4PPMJmR/RUWVWffDuVAtGokynktOZimvqzizcTyCn+Xg?= =?us-ascii?Q?botvlo1L1cIK8AYK7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us-ascii?Q?V6rYI4oIqO8pv+6h2yObqrMa/rLf+EMRGHcpH58usMkebPLqZLOsxgEHanCH?= =?us-ascii?Q?Ug=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 6u5CO3oMB+f63mcFWyXXQro31n8bQS5+W2dZsPOlhtqJbIfUhkeiORF0F/maFHJEi7tqXxoh/AeHjdol8RCMUi25B3Sc80LL1wO1MPQG4YLFODeZCpxj6oqNixwmeVGMvunbX3VHNzJuC/cdoQskCa4u4Aw5OtL02h17fDkqgcVds8OTpqiTofH0RgYhSeY2UMZJvegfa2ObMnoAwJj2YS4czz3ybxVSKAq5gT69DEpkrH1RqVQzb7xAxa6D2yel80TJd7wxK5XsPdY+I2wux+nsX/NYLrFuABa2grXMKgP2PCYl82rwAo5nIVNnRAnXeUWktoENgeArBhMV/O7UQT6n51iXymnbGoAAswn2VwZPNiFwBcQm9I53yOtjQXqkSywhNdm/ioy9N4y8UoQkmSSwSM2uGuD5T0UTq9zkgptzKno6gWVmpFh1dK8rIWaeqx3x3ARpxZeNqasQlmjrPmi7yUOsmyfgURffgGSQUHLNXqk7cigQ18+eASi2bSXajyW7aDYCSXLXrmJ97MVd75varfQVLRo+0u+prucAoqF43Q05FW+F1kfxfoTWIRLMBe3lnJYDFXvl2WW4NFOMH7Kj7RySK7wqkKrddQIEiAA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: d4067e71-2fa9-4653-36b3-08dd0572f166 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:42:19.2130 (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: IXbzhdledHIS3VEXMI1wuELdsBBlJFf+oLZB8L9I+dsnNT+m6hSXMLBrxj+OU5vGO61FkmjDAnoruXVIh2qqYSWz+GUnCnQ2VCiEbMptJgs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR10MB4201 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 spamscore=0 adultscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: tmQ4f_OegXEagwmH2a8LtWiayz_lGjGB X-Proofpoint-GUID: tmQ4f_OegXEagwmH2a8LtWiayz_lGjGB 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 | 115 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 49 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index fca3429da2fe..e4dfeaef668a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2666,14 +2666,14 @@ int do_munmap(struct mm_struct *mm, unsigned long s= tart, size_t len, return do_vmi_munmap(&vmi, mm, start, len, uf, false); } =20 -unsigned long mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { struct mm_struct *mm =3D current->mm; struct vm_area_struct *vma =3D NULL; struct vm_area_struct *next, *prev, *merge; - pgoff_t pglen =3D len >> PAGE_SHIFT; + pgoff_t pglen =3D PHYS_PFN(len); unsigned long charged =3D 0; unsigned long end =3D addr + len; unsigned long merge_start =3D addr, merge_end =3D end; @@ -2770,25 +2770,26 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, vma->vm_page_prot =3D vm_get_page_prot(vm_flags); vma->vm_pgoff =3D pgoff; =20 - if (file) { - if (vm_flags & VM_SHARED) { - error =3D mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + if (vma_iter_prealloc(&vmi, vma)) { + error =3D -ENOMEM; + goto free_vma; + } =20 + if (file) { vma->vm_file =3D get_file(file); error =3D mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; =20 + /* 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. */ - error =3D -EINVAL; - if (WARN_ON((addr !=3D vma->vm_start))) - goto close_and_free_vma; + VM_WARN_ON_ONCE(vm_flags !=3D vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); =20 vma_iter_config(&vmi, addr, end); /* @@ -2800,6 +2801,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma->vm_end, vma->vm_flags, NULL, vma->vm_file, vma->vm_pgoff, NULL, NULL_VM_UFFD_CTX, NULL); + if (merge) { /* * ->mmap() can change vma->vm_file and fput @@ -2813,7 +2815,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma =3D merge; /* Update vm_flags to pick up the change. */ vm_flags =3D vma->vm_flags; - goto unmap_writable; + goto file_expanded; } } =20 @@ -2821,24 +2823,15 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, } else if (vm_flags & VM_SHARED) { error =3D shmem_zero_setup(vma); if (error) - goto free_vma; + goto free_iter_vma; } else { vma_set_anonymous(vma); } =20 - 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 */ - error =3D -EINVAL; - if (!arch_validate_flags(vma->vm_flags)) - goto close_and_free_vma; - - error =3D -ENOMEM; - if (vma_iter_prealloc(&vmi, vma)) - goto close_and_free_vma; +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif =20 /* Lock the VMA since it is modified after insertion into VMA tree */ vma_start_write(vma); @@ -2861,10 +2854,7 @@ unsigned long mmap_region(struct file *file, unsigne= d long addr, */ khugepaged_enter_vma(vma, vma->vm_flags); =20 - /* Once vma denies write, undo our temporary denial count */ -unmap_writable: - if (file && vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); +file_expanded: file =3D vma->vm_file; ksm_add_vma(vma); expanded: @@ -2894,33 +2884,60 @@ unsigned long mmap_region(struct file *file, unsign= ed long addr, =20 vma_set_page_prot(vma); =20 - validate_mm(mm); return addr; =20 -close_and_free_vma: - vma_close(vma); - - 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; =20 - vma_iter_set(&vmi, vma->vm_end); - /* Undo any partial mapping done by a device driver. */ - unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start, - vma->vm_end, vma->vm_end, true); - } - if (file && (vm_flags & VM_SHARED)) - mapping_unmap_writable(file->f_mapping); + vma_iter_set(&vmi, vma->vm_end); + /* Undo any partial mapping done by a device driver. */ + unmap_region(mm, &vmi.mas, vma, prev, next, vma->vm_start, + vma->vm_end, vma->vm_end, true); +free_iter_vma: + vma_iter_free(&vmi); free_vma: vm_area_free(vma); unacct_error: if (charged) vm_unacct_memory(charged); - validate_mm(mm); return error; } =20 +unsigned long mmap_region(struct file *file, unsigned long addr, + unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, + struct list_head *uf) +{ + unsigned long ret; + bool writable_file_mapping =3D false; + + /* Check to see if MDWE is applicable. */ + if (map_deny_write_exec(vm_flags, vm_flags)) + return -EACCES; + + /* 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 unlock) { int ret; --=20 2.47.0