From patchwork Fri Nov 15 12:41:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876184 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DFAD8D6DDF7 for ; Fri, 15 Nov 2024 12:42:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7075A6B00AC; Fri, 15 Nov 2024 07:42:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B6996B00AE; Fri, 15 Nov 2024 07:42:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BB646B00AD; Fri, 15 Nov 2024 07:42:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 274AB6B00AB for ; Fri, 15 Nov 2024 07:42:27 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D588AC1661 for ; Fri, 15 Nov 2024 12:42:26 +0000 (UTC) X-FDA: 82788290676.19.BD0D73F Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf11.hostedemail.com (Postfix) with ESMTP id 3D67A40010 for ; Fri, 15 Nov 2024 12:41:27 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=cVVyG5v0; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=TumDi+h1; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf11.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674455; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/TY0TOZyddbmZHX6YK8ZiEM3V6psrC36jd9wYlgChpU=; b=LSf8S7zyShdPbLTbHo+ooTi6qbcfUiMHbWrhxgK4gSdGJKwgOmk0uZVk09Gskmxum7F4CV uADRzhe54s0hfl94txsQRv3/PWEtI18LmzQSeLIuyOjc1R0zLGhUMq3hmS73mnBQmxHLxo RNFqeNEAeNCTcVsmZpaXAHgph0KumUA= ARC-Authentication-Results: i=2; imf11.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=cVVyG5v0; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=TumDi+h1; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf11.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674455; a=rsa-sha256; cv=pass; b=Rxe7cwpeJpcWZyjAB40xn3YSJr2le0p9dOPC46LWZtvDyKPm/bUq14m+Nf+yYypt7RZa35 2eoeP9XgFAcA23P6Hwy1+EOKtKpQYfWTt5YlKw4g1ItOJxVPai8BMzVXxPe3kpulWBS9B5 6pzNok7/yC8ADASyhsFzy/x0XnVwAP8= 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: X-ClientProxiedBy: LO4P123CA0484.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:1a8::21) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) 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: XFXe3JsgzZgD4+thwQzsEC2wYrz1v7LQCok38oisZGZ3KQyn640IX76Ddyha2iLZc6i2Ievjz2sxD5VftvvIeH9sgU12FR+jcGqIv0/NolFca4nGOAJ4vmGsP5R2ua5A5CklgfVmgXm/tc6K9Zv1B6QVNFglgyYZXwN0ovI0K4DwBGbSgBIeIDNDz4yxfgxLB+bNaHeiHn1wyleHsANo5wCwxOesh0FLHhp1glzXqfDI0WR3q/H26DKvJs5FySqCw3FyQABJRMgDbghHaeV+Ui4pTgfD17YXBq6XdnSTm5T19U6A9J7KLm0fIok2E/ItI8KdEhdMrtTjnGCgHsBFjPrwDWPAwOqitu6KLJRZ8W73kNVsfNRxsh5I1f/x8g27OyotAJSqckzmZ1muCvs5R+afSPcZMzN5piiGLRdKEO+dm/goun6af5tPjtVLCLK1R45BWyp/LS7sqtA7B9UYOxD/J1cPD8gUDQw7PrIm2mHn2XfrbjoZgLzLLde2THfgkSqAyxat5j7cR+QXwYWSUlMTwDzEy7V9uxn8JqftqY8ROw4rnm+Y4xd2Qw4iuwXc3A8RJvZXUJT67682NdAhznU7X5LLxkUeegBIDzUhAwrLFLP8uzdqwImveMwAtr0IAgch6ES3WwIW6/jlttvoQ5Pe3mzJuX3eFog5Kh8BsHcIX+oUmMo7CHEjrFOJmCdVZJel4hIXU4dzqRqc+mrJ5Uvz0zOhE/q/sBeLWv5Q/FRLF6lP15nZxXynW1dcymAK5AcyT52SqlUK9UMr+1Wbet+JHUcf0EtjJJxGdvRBzd2f/AHaWzXCDCQzJ00rcLppp1Zf/1G6VvcIdotPGE4M5KC8u/5GB74tTw9VJjbJZK08JsjDJIaXPboP0pat3Q1Xc/ldxYJczbzuuxl3qJTdsGbjIOJMs5hlIy58qHqFqDuS9ek9xLIPgvjcuPKO9cZNrrYWYIJ5OEUAACXQpxfxdrrwnd2pjV32dKFU2Z2GdtiJlHYH1mIz2aU+vIwD1BMX26IFe3gIcOa139wTJTA/gd5BfBs1cWuBesuOzK1HQb0Iv/pXBhUBbAHWvWf9dhaV2sSJqqjMOv+A9bCD5GWITYoKgssKCmoRpVCLiX7Fcm/uIWkl+p6sjknLJCpTWVb17Vznpq6MpwQ+cY5jSYzUN7pp900imkvp2qdtaIBJ+9mYA/7f3UbWbYJfCaml21qLPBP0uZ7pBEtZ4uAtn8UJpw6I3SxW15HVd9udd+uxM/S9+SfPfk98ah1Qsw+o73VaqtUHGFk2+ajZT7N/xm7tfAa2yzdpbhEy8rcvqEHWA1a/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: 80QkjYeOOxA760FRSMeRgU/Dqi7pwF+uMjHoWJj0u9gpL8cwC15MKTV1VOFgP3OImneZ2+8GktotP3R9hn8yU7DywpguTBBsHMl9fJk8I1UDb9z+YuCZnNZxslTByRIwjOKRZtbDB2ZckXw1a/Tvhw0oKGx7KhkzZreAu6vRjdjvuQuZQGyNhwfobWTJnQai2sN9Z8IaG/DQVmq789LXopY6g76kHrfcv/IePCth56oqlzsWsHHh7sfbOTM2mqVkKWgHquGxT8ZRf6HiDxoe7RE+5yiXXuw1oNBlX8c984+JJ1BHtMe4nwdM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FBRnfIUD109NpVsYSOMpqH9TKMbbg/QRhh0KHebTkpMhlskIEaFeX4pSzqUpXDRNmuxYfrEcyXMrayDzRNn7jJ/t+zca8Cbd/HtCLiTw99TdWwWLfmUtURUqUE9XEpjzUwW3ggf7fsOO/++ZCYnfAbZ1u1cJmGF0EikfrMv5tFAI44fFKuMWndd88WtYUwALZ86vCvqcE0C25qxx3eZSztH2VGqXcMdYCA== 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 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 3D67A40010 X-Stat-Signature: btckcc4yt1wxhcrzcuqjhwf3euggp9cz X-HE-Tag: 1731674487-832957 X-HE-Meta: U2FsdGVkX19+Ozt0cDLYW3gROOSem7tal2ebmkk1od1oRpe4RxP/oxgT18xl0I9/TMbn2xvMEoRmV7oMvuDur/Z8bPYi6rIO4N2AVvCx/eZMeYcjsmsRVfqA10nTIVOlMG1KAKf3odG5MxGilWaPbqrIXgoSuXKtWIG6qp5Ckdsc7H/GO+Van2oF9M7XoGQOkaB/9JrkaRjbvZy6XR1ymeaXIABPmsBBxTZ9RVpvlSutntRrCRaYTL+bcuXqB5lNLVTyiAoeI+JHpj2K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VTtU2c4NaLtuXmpvCI3JSVwo+BRxNQtOJfN0HjHym7SKOCN2mavWWtNCUC3ixfK8a5Thm3M0qAsqz2sArC/21oSoBZMccCuY9frwejlxnLQfODP7k/xcqb2EEeCj6fv5tpama1Skmxzw2gUUfUG7zlmQX0NkOVby48sZS8tinJi7YYe1ZdRWLFqSqMb5Pu5c3dRa86C07urcUpyQKizkQEom/miTgbvdr2j49qvZowk+4EHJPAOscVpcq2uNA3jZpiXUbMhWi7IoybQAWrqVnSEeHq0gPVko83WFPu5XuDcgSR+0XWSeZeAWMPs76zirLhT/lahex/MrpeR+lKafap+xZLjfpGtrDxcvCE+Yf0VT/y3qyDT01CbX4XUM0EcFJjEkHPeTt5sMERF5PDtGfb+jEis6G7+Ys5MX1PdFh/vybAY2KZOIKaa94JxQ95tpvWpd1J8y55zxYwEtfM2z1nONozuTS4qwFn4L66aU/nKyfh53FaXzq4ickXJNA3+CwBbMKUPbF52JtXSdNIQrekh1LS8Ckv2iIYVuf3XIzLpNJ8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ 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.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") 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 *folio) return (void *)(mapping & ~PAGE_MAPPING_FLAGS); } +/* + * This is a file-backed mapping, and is about to be memory mapped - invoke 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 = 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 = &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, } vma->vm_file = get_file(file); - error = call_mmap(file, vma); + error = mmap_file(file, vma); if (error) goto unmap_and_free_vma; @@ -2793,7 +2793,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_iter_config(&vmi, addr, end); /* - * If vm_flags changed after call_mmap(), we should try merge + * If vm_flags changed after mmap_file(), we should try merge * vma again as we may succeed this time. */ if (unlikely(vm_flags != 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 *vma) { int ret; - ret = call_mmap(vma->vm_file, vma); + ret = mmap_file(vma->vm_file, vma); if (ret == 0) { vma->vm_region->vm_top = 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 = call_mmap(vma->vm_file, vma); + ret = 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 = -ENOSYS; From patchwork Fri Nov 15 12:41:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876185 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B7656D6DDF9 for ; Fri, 15 Nov 2024 12:42:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 272946B00AD; Fri, 15 Nov 2024 07:42:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2252C6B00AE; Fri, 15 Nov 2024 07:42:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1BC36B00AF; Fri, 15 Nov 2024 07:42:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CBC676B00AD for ; Fri, 15 Nov 2024 07:42:28 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9042B41688 for ; Fri, 15 Nov 2024 12:42:28 +0000 (UTC) X-FDA: 82788290718.08.61CE05A Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf25.hostedemail.com (Postfix) with ESMTP id 770FBA0005 for ; Fri, 15 Nov 2024 12:41:52 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=CsBQWA3M; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=CYAJPZlE; spf=pass (imf25.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674368; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1nyxFrUZ0YkzFxmLhfWFRII65PTLdN3cwHIqE3lAYMg=; b=y5HcUtfEr3DVeRfV5hqCzW3m1ar8teLkvMpM+cp/+vB8zsWD1iriF4hr9I9BTNWZ6MoGq1 CkH81BYwofa0zlgGDiTR2KaRDh9k01a8ik+xiOS0RnED94BD04BD8fGcoonzzolzOq5wmO VxMDJWekX9a6EYr9C2WTQWFd0jbJ9KA= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=CsBQWA3M; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=CYAJPZlE; spf=pass (imf25.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); dmarc=pass (policy=reject) header.from=oracle.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674368; a=rsa-sha256; cv=pass; b=qJp2wKMbJUstX/KYCZ9RBIXNachvv080bqOV/ceX4vlsnyFo+CXFXN2r5XJz4ps8GuSnNN UmrGsp9bmfURJ7y+6uWKBZ7Fkk2sU/54SYQmR9x7Lz1ZvEAqnlB8eYGa0beIyZS00ulHNw NT0/cY+A2pZksox0jnnzqZG8bIYiZAQ= 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: X-ClientProxiedBy: LO4P123CA0542.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:319::7) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) 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: lRrUcOodxZv+PzWNz8gFJZ4SM+sv+P1fws34dOBC1Hq5EiRIvp90p+lmPr3gq5oPMJSL3N6eoxEsYNN2sG2TUTWbIsGgKDrm2Tr1xvBEw/QlLHswuY0kVaMkesFX2QHT83X4G+xkP9MWTtXPfMYG9bN0Tshubt/iV5kJUfECNZloojt6DuuiDnG/rToduMcx9FV563xo/OrjiLIOIOaXvykkd6j+9YlvW+Tur5LqzxbxBWQpBeIB9cEGHd80yeqvKse0ssAdctzAU+FyyAhG5DLQ0aaGEUr2NQojF5c1g4VRGT6Sssk0e7jTrHGA4e1mDJeMsKoOxVHMiwhiR3AvwHpeI2nq3JmfyvbdyPbV2y3GrSTZTfM46Ig7dF06Ja5XdrMYdefxkDR9WdBEhliRlMmFEk4/fXLWWdl+DhYLOSaXeJ5MesputgsX0JUVwMiFHzEEGaegZKYVa3hHhtUUiQEFf4c/U9PjvurDCmTLcANruOD0o57AbotdU24AIJeFNBr5NK6aGJbkggCz8t4gMs12CJh1z+MUZFieyqYqYCyy4Zer1V+CdaKwNHtaiXp0fF/z3+rX//vaRb4gMNxfN63EGsLey3KP7LdYUefxTew80CGX5Mj5JdBEHbCAa+qbyemkL2PC9R9EUf2Z4H493XxRjsMF6rLxVAuDVLEJ2PSlsa47CGFiACVbTD5ZqP7M++885rCM8vDQkEQRLgW00bxrK9AYud46Kp7Aagkpw4kaWyDQK3wBaa/Wzlc7lhbCIdW20edGdhqjhjtLgqcuDkE5INZxIoKIq699RrbbVPul5q9hOgk7Zyjx/hzoTGTw7BFAshNWqhcl2iMv8Cgmq6E/H/vFCsuo0hyweW3tyUNtXkcS//UighlONS31I7GL8jUWM5Is92lZMhmDLa6zoPSVHTyJlOpZse55DwKBhF6SSMxCiJYkHBJHDvIdhg6H0P/0Mwdi7IWPigdnWowAdKJ5Kw498thK17IWgx/bgEMB4HyTAqXIymLs6Yak6DfzH7tc3ZmRjFSQHFkPUfYdA0I8PTGsN2QY6kWJ2NuR/HL5flgcW8KXjSOpu7HGBHHnKbR1ITN/us5PnymKeOrEyFVoPisGDDFI18FbhmJ1dFJfUk9Mzl/XW5DEGmHiBV6QTZd7sSgV0hfufL5gS3NYzRKFc5+z99VFQ+O4DthwO4sY4W6k+KDnrORAnGTsn4tqVJ2V3BeAdw8czX62Vaja+A4PWNoOXN9AM91bvKCGx4K9WwBUJn203udwgzpw2vRspHQTyoLF6C+Vmh4SuuhfXiRxl4X4Lb2mnRxZHXRSHpsIkDPhR3N/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: YaEfKXswjD2RGEk3gCnNcDzt4vOKXMY3k1BsbdmJklY1neAW3qQnydfmx4iYOkfF16ZJbtJ8/4mnQriRl28WnHCfrvWiAMDUgcXzUFIwNGbeFIPqM+vywqYimhYhNIPsk/fmGYbsLnSaQBTMIa48mC2BOgbxCESW/++4lLu4iGk4fyEfyoN2YjPucfE8WrD7E6O9DyBUJSW3L1hBBzG1MsLsCSlqxdvD2eqX2q4n98BwltfnZ/q+dDLE+mm8SHsgCFLfRgwR3TW6wnjHAIRIXdz9ZjQL17bVHOYHwdTQsKOTf8ZpmeCMhnO+YEM3CNw7gB2wF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7g+R9Y8q4Znvi/ftCBiFcmYiyoewZhRJMZTY/+0pe2H+EjV3D5H6ag6GyDE2hF+e3Tx78Fz0O5Jzz/gXmMnbbhnZPZYnT8ykgAnbik4/HwKw9+tmB2L7ZIG2cR+L2RebSekLc0gs6BEH98EcMzp2bEfOiDcIbZmkL1MJd8AQyhVIYLdzOCk/5jDOP58Dnx8IyqCrL9uXeFTTol4OivHvg== 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 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 770FBA0005 X-Stat-Signature: pd16q3ia11dgj6x35ynxkdbn4h9ud5u6 X-Rspam-User: X-HE-Tag: 1731674512-39276 X-HE-Meta: U2FsdGVkX1/nRecxeIJX5Z71f78qJ+P2/9JNOJ82oB77AG++rgL5TdGW+Ih5rS0KFQ/QVarRfAlpr2lAZ2iyEQEghNZCdPyNU0KUBajC9oFCflGmpZTYLmseaxxQnHU9RMYV+Ijb8lP1KAPTJNy4T5p8kZ1jw2FKLoVh08e/x46FdozMfsRd6eFGTT5FpbcAxTSwR5I4Yx/Ynx2/HQ/KGei74vZ4e1h+tHqtWKSkj6/ZOoq5tNCHECcSFJqmbWIybz3NGMIb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HmIb7lFNTVYAiwVHkixRajE3p4mJq/xaoa5JcnvNVdHL3MWIpcpJDDa9b7IQ0WnzVy9EFK2xwxmfJ+tW7WcSP0FIQarP+GCWUX6KxE0SJC1d5jc35H5/mUTeHuB1E0qS/3Iuy6479KQdY88orUiOFit9+fUdrFXI0u7sCiUJVnbqLYd5gmtqkTkM2WFdrXzk9DYsvN50Wlkt6L26eoF3T4fy8mR4l5wd3bFGJWVbw60iXLQfnNgOvxz4paztRNfHtCWfSbxnx8rT9N6+mFgJLS+g3SsC6ok4lRzMQUgBxA0M/2nudXDWwx9KKgiAwBV7LprpoT+37UzU2LCHfSUytcxLf77Dttu+bZZHuH4l2KLHdHU+6Z3mxhxaSU8P/uTd69nZLF5o6ZZI9MG9qz0cGcBW9O5Dll0CY+TNJPLd7P1scqjWqXlynKst8skjDJoumzu3bZaZwbNw9SxKIfFnm1nsduGr6/YRabNKyt5+kWX2VIHmerJP75V9uT6buPBoGH2ByAWzktivXhVb9hJPJqMOwN/ndKjfRl0nDYJs4ggFivVe1B41BLPhBW04JmDmvCDg3tOjzHHQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ 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.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") 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; } +/* + * If the VMA has a close hook then close it, and since closing it might leave + * it in an inconsistent state which makes the use of any hooks suspect, clear + * 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 = &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; close_and_free_vma: - if (file && vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); 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; out_vma_link: - if (new_vma->vm_ops && new_vma->vm_ops->close) - new_vma->vm_ops->close(new_vma); + vma_close(new_vma); if (new_vma->vm_file) fput(new_vma->vm_file); diff --git a/mm/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 *vma) */ 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); From patchwork Fri Nov 15 12:41:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876186 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C72EDD6DDF7 for ; Fri, 15 Nov 2024 12:42:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A8626B00AE; Fri, 15 Nov 2024 07:42:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5573A6B00AF; Fri, 15 Nov 2024 07:42:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 383B16B00B0; Fri, 15 Nov 2024 07:42:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0F00A6B00AE for ; Fri, 15 Nov 2024 07:42:32 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id BFFEBC15C8 for ; Fri, 15 Nov 2024 12:42:31 +0000 (UTC) X-FDA: 82788291852.05.06844BB Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf19.hostedemail.com (Postfix) with ESMTP id 981191A0008 for ; Fri, 15 Nov 2024 12:41:31 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=eHeNEwDy; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=tn2zave8; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf19.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674369; a=rsa-sha256; cv=pass; b=jDHd19COilKsglAP9S+kx64hbTAB84kEdWwAlLXgtviI1TjO/cvhPimpFHs3MR8AhNkfzt GWS6K7ZSIhzrTGak38B/h+uUZRuuhmvlh4vr/MD9rRAnVRggGP8ZGNzP9IiRofTj/ZEljf DthWuB8ziFCoYY2H/Cd6bQf8M3glbrw= ARC-Authentication-Results: i=2; imf19.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=eHeNEwDy; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=tn2zave8; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf19.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674369; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=g4lhvb8C2OfyVjOD4h8GKh1wFOBJ6R0iZcQUPnrOE70=; b=AXb5uPzJouP8zQUUy/Vxr2ZaFFU0tvpTiccVwOHdeIG5x0kWFCjpgufqsl6V1FxDHCyt4x Vrxb4IL8Vfxxyz+aOlC8OUkruxDqwLGbbIKAT6jV6c7A6nNFf67CQyL2pu0nIcjrAwbg7V 9sffGF+b0GLJvQuO/2Tu46wLtwdxOdc= 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: X-ClientProxiedBy: LO4P265CA0025.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2ae::10) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) 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: SVg6jZPGWE+h/y74Q112aSiDJoUx5mcdcKRWuTLOibK5X2NCndx9lrtkEdm8g2N1Axf8I8OjetHITHNXLNxuNEo6AaOrHHmwvT73uPVqEW9sVPjrnh496WfwVWjjD3H94DXy2XWbcBRUOxwXOumHXegywJm1yKGKe1J+H0gmDNeERku4jSNFzxqY1WS6F2SpyImFb4Gh4dH5tu/mMIR97pdW4IauO3+i94YgNbMyLF5tcKpITgaQrmQ4np7sCRxc9TZIMv1iaFPsS9Pri794p7c/KwU3ZbQQDwpxr3axupymxeePQUunDizUQvIq2xq+uC7DyNFXKfPcqkNYcXXIrbx9a9DBeFHFr2UbIxIg6/PqYA2I7iKj3pJ3xLr0uGfrhhNtqm3phEVWkcYyJWs7LZlHTzl+JOWrn4+ZNWWwQGXcCuUW8j72Euo4MDvZxLC3X18MgoJoMNrRA1GK2FpOuaPc46uJpLF5AwhPhnzMsKCLyshL7YC0eyy1kh51TLDtwA8rBP0URgaPB0I9TeYG8v0FHjUe1QXo/V9j9jt6sWShoPRFoJW458Z5Nh/XyxcmHC7FxfFqgK/qX3pqyy1a0v7YPSZjBX4G0nO2CCsCCPat/Fn8j28oNr9WnYjvLt4pw3JHKRZ9askoVba+CIuph7NGsHW+3w03qzbA07Omg30UnJzmzdg9fGvxKWrz6odUAKJqaVb/aE3mi/+nqU9xVDn3UvtmnxovPU3wxK/63gK/3jH0LLcgD19paCWzL8O05E7RAT0Cw2JzdVKGR2WXq+OswCcGVFO0AMueX1FhI2ceh5YazNOxCwt1ox1p07AbvNqtxHAyGnfOupWZUS6TDmQz5XCut8pk9oglz34/MHrzNwppJ7Y8L7SyYJL534hyDQ6fYmAZUrznPDge0xcOUpNoiKjLohGIeUjb1FKnbT+1fUxqLdaiUm7brQA4h9mo7pzpHT8LimPYHyyOwN1kEP3Gl7g0vluPPEdUC4cz/hGpnVLrrznKWPpt1YnKE90XUaiV6WUW3gOX2jR3zkQDmuP59y2MS7qo7dOYaj3dLD2mob79TuPUPjgueg7mxiBkHoljjqYpitsjXibSkl1CYGVU2P0lOjJeuLYZLP41Y7k/yM27/fS/oRIOBKWeSLf7R/C6YZI0A7G68lKiJqzsc2o/KCS+L+NIypNWeqbrTy79cjIDaM/iTUf4OxaMiXMBnaDicD020lXI8IkUm6kOSvzRmKMkGVzpVC7890VmbNKXn5ZsLr9tWG9s8n38kVeW5AuQye+wYku6MA3giL/9bQNkNT1DgpX8c8bVGviSZNl2OK/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)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V5wTMLBysYXlTlfiXXP5BeYidVibvMVLyjQOwMaASUQqot8YLjveDJ1Ew/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 X-Stat-Signature: rpcefaye8ckwrs9knwq1q39ae7iin6ku X-Rspamd-Queue-Id: 981191A0008 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1731674491-529217 X-HE-Meta: U2FsdGVkX18aFM42Gw2vCn/S6Dsk4HPzw5V8coYKe1f0pcmslFCfjvJGnsy5oeInssLKNae/29tSTQRaBIX2S0r7/u6IGWUNN5y28jB3mTKD3Li9oBWkr88AonULIUXk5bNjDT4/C/+bEPA04bQYew2jV55g5iBbCE9oesP2+B65K1CA6xQDfo65scDxNCnY8sdYt8B2ssad/SWz4yESRVub6mYASerFk/cPddq4RwexzALdHnfbfCZ/ILvxtWpBwJYj1k7SFQQMmuEZxGjDcBplP+uPofgE0JKrFPK0iP5FAQUsX/Fj1+B8S3L7VyI+ZgzmlXhzHjM/xEC0+u8vEnSA1/+VA5MREYI0QNPEhnWRRjx6KtSF21heWpTM96RqBHjnFMNzsz3EMd8XAAJDkIpX5/ebn+X02yLcZRrNAux5mMu0AR1fLLfJASwIgJCFMZKMAOhJcE39dgO6XE6eUMjIO3sb5N6g8aOWvt6GNMSQRTRnEmQTE44jehQRGWVJH8z45KoJ/OkWysLW5hELnxJswEhb2jUVGBLwQMPoSzB03huK01swxlarOC4tL1XbkkD4Q7XDe9d5+/uaB3RUa8NlvSKgbtqWeECpAqGlhFVFPxDGx8VSWY5lI5x2fDPPyft/xEDiFBRZZFcVdU/86gBT/7LJfj19WJs+cv6OWYPUJGYTrEZUXK7T7jdPFqgZKsDTuiVdtqMcGm4juQ5f8m0L0z2f+ZJWrb37O7YIoQhjmo4GcMfuB8GpKGyLzIEWzHD0c1MeMyO3AiemVKRujjnBBIvtEBBIsSMI26cNMcyIooCNUJrwKsJOVGU/Y3x09cL0yTwHEtLvR4FvfKDfKoH//lNMuVAbtZqX+lqZMEmlgYjv2mHkRRv7QuENUiVOdN6eHfwzC4zsZkeVXL6h/tfFjuy+9SvD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ofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ 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.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") 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_supported(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-Execute + * (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 denied. */ -static inline bool map_deny_write_exec(struct vm_area_struct *vma, unsigned long vm_flags) +static inline bool map_deny_write_exec(unsigned long old, unsigned long new) { + /* If MDWE is disabled, we have nothing to deny. */ if (!test_bit(MMF_HAS_MDWE, ¤t->mm->flags)) return false; - if ((vm_flags & VM_EXEC) && (vm_flags & VM_WRITE)) + /* If the new VMA is not executable, we have nothing to deny. */ + if (!(new & VM_EXEC)) + return false; + + /* Under MDWE we do not accept newly writably executable VMAs... */ + if (new & VM_WRITE) return true; - if (!(vma->vm_flags & VM_EXEC) && (vm_flags & VM_EXEC)) + /* ...nor previously non-executable VMAs becoming executable. */ + if (!(old & VM_EXEC)) return true; 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); } - if (map_deny_write_exec(vma, vma->vm_flags)) { + if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) { error = -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; } - if (map_deny_write_exec(vma, newflags)) { + if (map_deny_write_exec(vma->vm_flags, newflags)) { error = -EACCES; break; } From patchwork Fri Nov 15 12:41:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876187 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BE30BD6DDF8 for ; Fri, 15 Nov 2024 12:42:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 50B576B00AF; Fri, 15 Nov 2024 07:42:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4BB5B6B00B1; Fri, 15 Nov 2024 07:42:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E9586B00B2; Fri, 15 Nov 2024 07:42:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 077BF6B00AF for ; Fri, 15 Nov 2024 07:42:38 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B7C80161696 for ; Fri, 15 Nov 2024 12:42:37 +0000 (UTC) X-FDA: 82788292734.28.E3224EC Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf24.hostedemail.com (Postfix) with ESMTP id 77CAD18000D for ; Fri, 15 Nov 2024 12:42:28 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=GkttqdLn; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=DwsAraHz; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf24.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674465; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=spnAHCCRbHps3QlTZk9KoUOkxA5rCBm5i/Uk9iQm9cQ=; b=oVDf7hoa9ijrxkl62mmYmoiOJvjioDvuY4+4gd3CMQ/G8jBg0V+Ttd8pk0kCwehgruoZzS 51zuBIogKhkb8v+HAQ2q1PQFPLTnVTJMh4CCczsI8Lj7SSWBF2acFwnd5xq8WjPzQS23WJ NtxcbsxvSKFIchxhHS5x/NnUUkje/ME= ARC-Authentication-Results: i=2; imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=GkttqdLn; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=DwsAraHz; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf24.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674466; a=rsa-sha256; cv=pass; b=IKWDzSir2FO9O8LObQYW8O8aVX5DMoqXYYJL27JrE+z+eByrcJgaeDIo213UfL4uRDPGGR dNJ9Iz0vYEJ5VYadAe9J7xS/D5YQ07Bs1f60hugCnUFNIBdsMvEniXVLSU03EcefWsbOYM vN5sMC1bRo9dggt1bNm6srqLmJgxTXo= 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: X-ClientProxiedBy: LO4P123CA0036.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:151::23) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) 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: dgTI7/rkKOyZzNkmUhkYEl+nOYoBKmyWIqe6jDyxLsljNMmpMgHoMj8IlsGZCrvwmAK4PbFqAMpxWiRECfpYJG9fyuMfH9RC/PJAHZrPoeSPgIr47hWwFWF2zsd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eNxpvzPV3b2RAMgqbCDDD4+r7H3rLR2p5rha6lYsdX/mGMhWAl9h6qhRL89Qk/sYi0ODZc07P5Ot6GqosKM17xdWbRMh+0hGrLgYxjrd8H3kNEYIbylNqZ2PAr5k7dFbrpH0KUBEXn 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: 9HU+N19vEJl2+dCbk+mDd43BICK4Qp3E3uMCr5cl3kJuhVF75TXnCU160H5EPnT2pvXmVWiGAOYBEyVduadxTp9u454nFg0k4y4Zr9RgmD7Lt3ZvGGuOFw+EYx3e7I0Hk0uierJf9sgOZsRIWOme4SDqxnv4N7rn6HSCa+0vhKCmqZeeMpitMmN1Qys4XUAEnEHSSDw9ZS+YyXGCK2e+rM3Om289F8Omiuo2L3Zg1oL6Seit+emqsz+w+Dt4z0IQFp0Y02df7qrewN87A78AKFlVnI8KAAdJaLzVFV6kOkwN1SYHETx7E0pKsedFoWb9++bO3j78QVmynAeDpGm/UX556MxSeKbk7s9ChMV0vMwRmri8rvAQu38XPfTvmcieKb87gJOdcrn3VUNZQJk7vmpVZBkyxDOJSZ8gasngfGlVveZ7j9fcsRMY7PfulTYTYIveLLBwv9ImZywbJFVyK+hqe9jGQFA8gYp37BOpmtxt/zI+oQFrYF7lRI9EF7PnUuOfdccQZsi9pkEiAZSnyg1ZgSSMYC44w6OeaC6HpDAItsyGmrY1v4n3pPCBtHNpSx4sH9x7gxUHoG7odR31iJWLw/YcT3eT5sqqTzHYNasb44/JNKcVl5rpk7TjgS5cS26UZpzimU+XoXdR78JwETqMgueWoZRXq0Rt+Kak/WfNnZjKmENmRtWq6Q+1oJ9AC69e6WBvpMHDbYERVupF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DqQqOjhVnhT2r5C1NkMrnZ8DWCpS+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 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 77CAD18000D X-Stat-Signature: oo86d7qke68mqd9w6mwoigywy9t91ybp X-HE-Tag: 1731674548-157626 X-HE-Meta: U2FsdGVkX191R8eAW7Gk8GNkVwvigsxzDHqQVMN9JyVOuhLkKBR0zPUTpu28OJq8Q2JddXM7rc37dRw1HTUprDLDbn7QOPJJAqDf1/AUo72Xy2HxCwu6ycrUTaHYU4mCLbW9pIFjFjqMykf7XleXApczTA/YQUhjz4Sxjte7TzELr2qDCalHcLWpfvmWbJ/LnbT4WikySO2XK7g/0f6t10sRC2R6T89MraQbkRj4wseeVAMcGfZ/NDHJEmiYzCPgjaMl95IuBYLiYHgHC8JRNAr3Kw/YH67TpHLdg45kpG1BPhCFgO33+YvvSq/oSQq+hjhUp1rJekRBmnr1pDH/9AUuWT+Xip8xWboyF4GX4XliRq+qJsD56Nv+11HJealjQryO5z2g5vPNh1qOdEN37RDZlaULbHQmpqlb6MEX7fLKa63eZl3lUu2w31OsvfSzIemLtBoOcEWCqLWt6bzSeSA9g/l7pJFh6Eblt6AQPYvv5uhW1wiu97Lq0fiRsPe4/zxqDSCqUjAUg03WcVz9n5VWfRblz9d0cM3/7bMkeUmRu8+BT0kMI8ARPa/KtwKjrFJe8+oAr05I+Pn2ShYEPVr4VXPaLrpFZyQJfE0uesTiLAeRnU18p1yX5gbMGHSwuscCITAPpiufnhjJoASc2GU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5Bv3RGsZelYPHlV4Uk5/Onnd2+NCVFg0EVG1jzbXyyCAw7ETKrJXCEjsDUC4rkaVZ3O1qp3TT950nmEcQ/47TvRCm+C/DIMobZ0r0siU2F5jiNc+1oga0GnHrgyqVqVx8DwVHJnpiu4uNhGJPa/5ndzKNxkGqZIewxh39LmZzrZwd+UBS7YjLnIL086tekMFO98HLaQUMCCE1msLG5FYQUzHbFEZlYM0CfuS3Uc5cKLHM63j4D SWTpqhgW X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ 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.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") 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 +++++++--- 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__ #include +#include +#include #include #include @@ -21,19 +23,21 @@ static inline unsigned long arch_calc_vm_prot_bits(unsigned long prot, } #define arch_calc_vm_prot_bits(prot, pkey) arch_calc_vm_prot_bits(prot, pkey) -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; 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, flags) 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__ +#include #include /* 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 -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) { /* * The stack on parisc grows upwards, so if userspace requests memory @@ -23,6 +24,6 @@ static inline unsigned long arch_calc_vm_flag_bits(unsigned long flags) 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, flags) #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 +#include #include #include @@ -94,7 +95,7 @@ static inline void vm_unacct_memory(long pages) #endif #ifndef arch_calc_vm_flag_bits -#define arch_calc_vm_flag_bits(flags) 0 +#define arch_calc_vm_flag_bits(file, flags) 0 #endif #ifndef arch_validate_prot @@ -151,12 +152,12 @@ calc_vm_prot_bits(unsigned long prot, unsigned long pkey) * 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); } 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 long 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 |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(flags) | + vm_flags |= calc_vm_prot_bits(prot, pkey) | calc_vm_flag_bits(file, flags) | mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; 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 *file, { unsigned long vm_flags; - vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags); + vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(file, flags); 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_area_struct *vma) if (ret) return ret; - /* 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) From patchwork Fri Nov 15 12:41:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876188 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F17ACD6DDF7 for ; Fri, 15 Nov 2024 12:42:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30F916B00B2; Fri, 15 Nov 2024 07:42:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C1226B00B3; Fri, 15 Nov 2024 07:42:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 076DA6B00B4; Fri, 15 Nov 2024 07:42:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id D290F6B00B2 for ; Fri, 15 Nov 2024 07:42:39 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8EAB4ACEAA for ; Fri, 15 Nov 2024 12:42:39 +0000 (UTC) X-FDA: 82788291852.23.5A4ACAD Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf17.hostedemail.com (Postfix) with ESMTP id 4B1874000E for ; Fri, 15 Nov 2024 12:42:01 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=dVCXG0jz; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=WMPMPZVR; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf17.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1731674424; a=rsa-sha256; cv=pass; b=TYsWkyWNbvmekwzuOV55efBAGoEAN2AYmSwm+2l0LXVdsV4vRMvn21A2zu/NvlsPNioxzo ZNawsCSJrqn748cbn53mxx0QFnLNs2cgC9hvWduU6arWwkb4pIzXMteHazdycES5submU/ Pl6AYpwya6mCzF/lCzYzpJ4XO6IV6Ug= ARC-Authentication-Results: i=2; imf17.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=dVCXG0jz; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=WMPMPZVR; dmarc=pass (policy=reject) header.from=oracle.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf17.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=lorenzo.stoakes@oracle.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731674424; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0TwxBJSLcC4galjZladP3skso+2UgAA4dyxp3qrUK9g=; b=aLnkwm86qe4GulF68mianSHUQvaQdvIrVIdpUsBZVOkkWggR7opp+COhEeEBKVqUSZeRPz /ffK7dtQdUBF8HminoSylziGCWXgjgLy1XOYTemq1bSPrWISse2QpHdtOmJzafhtzZW5C4 iA2sAF132Bos1JTINRSkexXv/4Fj01c= 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: X-ClientProxiedBy: LO2P265CA0102.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:c::18) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) 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: rnM4R5DjMiLmA+papYF1WUsA7g0XnYs5natgwjGSpp/3bpoZjUYn7qiRLm1VkWIPQQXmh5fvBZfXx7SXS4cfgrwo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JSbiih/UaV1/dlzAgRu6mo0DsjVQWs6wkTX9lte2L79CuYhsYiEu2KBEnFj4Kra/3+szlsV1qzK3wbPMXVaaDMgnKqXUsa6dyNEfyc7szR8xKR3VIgr5Cz7yPShumTGyoHaR6fU7bjxgVpfULaPwwRGl75fVIcs9yimAjpltecwjS1j4akf2aa5f+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: kVTTCYSNTol9yfi3dgu25fbjFqd82rgUyQbtbipDCnWtd5FjdmM+PAlv3uuNFucKyp/comdQCHfMrC2UKCxSR1vOdQYfTaIz+wEAKIH85a07L8ILYRzNqrpiD4MUeY2oXyq3vK4+XvRxkEEWI6OSufJhjKQoLqISLrnRBobxt5Z9pCL5Vmc6jDvI16IEPsiIFYtW9bFPNm7u5z+AdLHP33oKy9JjqGR7AFk+F2ynQwEOetSZqFdougchRj62MEgl9A/ZsYIZ3wu6PXe4IpJge9N/dQuUZdACIegJOBSGhPVejSIBUORF52NhtLQiKhsZNbwRXQm/cUlDlsSw/gIbis4ommT4Ju/V8GW0aakPABj6Yao1FfC/szGNIkSo1L2IJ9YcChsR+qRyvbH55zu9q/TLBLe7UDB8dj/qDgJ2dy+27WFkTpL4DwQjdmTInQ0ODfid9PuzYcRRzmAUCeVL4i2C50ThRfREIyoW32/4wae/tgUJTd9rb8kEKIWXHdzJ73d/fwv59NPoPVeziMQb67ybHTLLbrYqb1dJt+CYmwXvoexszXXh6RPTGk+Pmwr9G2y7YFQxxbRQTwh9AjLz8MqDArWSqR8H4qAg/n7w4PPMJmR/RUWVWffDuVAtGokynktOZimvqzizcTyCn+Xgbotvlo1L1cIK8AYK7DK5B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tqXxoh/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 X-Rspamd-Queue-Id: 4B1874000E X-Stat-Signature: wmnzdww6ft37mqz1t6zykz9r96s94brw X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1731674521-294017 X-HE-Meta: U2FsdGVkX1+MfZelZecHXWke5puBHLT81494c9dL9kPWPLl2uz/0SAuW0LvQXFRY7q61LBScXJRmYtLJvMAPdzCTCJADl5jAjXrTekkLhARxPckKAQgiLqtoD1xjb1Sd9z4WZLXpXnWBP0kitNWcXd3beuAHEmaSv8BsnQQMzzF27JOADf4PVb3BxTcY7U2yckt+SPqQ2exxRj71e3iuupaoBghkeMmt9C+c7YkBGOPGyCLZvphVr3LTSoI/Ie+kkHWUeeWERyP84EKW6a7klUiETdW43NI8vY64CAbi7qm83qA5k92k7U6ky/iGKUs0V3mOrl67ZTcKeVY0fkNephwyV4sfxdofzFswJUq2ET2WGEXKe+f3N+tEuSvtcZHgcKqXXn65F91flv8Gb4cnlWEdEVQCcjp4Hqpb5AzHfn8MSgFPRfVBTGDrMu/yHk4zTpH4FTtNZOabj2b2vgNSVS1FrDdI0KmIa1YaVxAIS0b4sACdfZCL2igC+vrW3uVWZ0Izg8mPnrhnVx8Hc4n5Ds1Vjv8iB/56z+zeYd4+ZExusCHHReY9LOGB/bvBrug0+Y16Wn+k87EthfIFS/phTa1FSKaopRf1+KWlfpyajvYmg8uYkE8ebMR8zVtx0kjNbQ3NP7W0j7ketUPmzya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WR/L4Ba1IxHCU9x9soPgAQE3NlopHF87YqY4bEkSI381XmbYD+Nm+wS/SbsOVzLyj5Ti1GLK25d0rAXveUEjzCptDkPqhHbOXYQCW1/udTRLqSaQzKRhnnkN1qOYR30wFKPzEV9RsxGc75c3pSTNNGdkW6TO/fx1RKFJMTuWvk3njxN/lrEjmHXP8tKa4tq42mhY2jKcuR2hEatFZDilWfB1bTUD6f1f1vkIMALaqP wvIM3IaJ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: [ 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.1730224667.git.lorenzo.stoakes@oracle.com Fixes: deb0f6562884 ("mm/mmap: undo ->mmap() when arch_validate_flags() fails") 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 start, size_t len, return do_vmi_munmap(&vmi, mm, start, len, uf, false); } -unsigned long mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { struct mm_struct *mm = current->mm; struct vm_area_struct *vma = NULL; struct vm_area_struct *next, *prev, *merge; - pgoff_t pglen = len >> PAGE_SHIFT; + pgoff_t pglen = PHYS_PFN(len); unsigned long charged = 0; unsigned long end = addr + len; unsigned long merge_start = addr, merge_end = end; @@ -2770,25 +2770,26 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_page_prot = vm_get_page_prot(vm_flags); vma->vm_pgoff = pgoff; - if (file) { - if (vm_flags & VM_SHARED) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto free_vma; - } + if (vma_iter_prealloc(&vmi, vma)) { + error = -ENOMEM; + goto free_vma; + } + if (file) { vma->vm_file = get_file(file); error = mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; + /* Drivers cannot alter the address of the VMA. */ + WARN_ON_ONCE(addr != 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 = -EINVAL; - if (WARN_ON((addr != vma->vm_start))) - goto close_and_free_vma; + VM_WARN_ON_ONCE(vm_flags != vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); 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 = merge; /* Update vm_flags to pick up the change. */ vm_flags = vma->vm_flags; - goto unmap_writable; + goto file_expanded; } } @@ -2821,24 +2823,15 @@ unsigned long mmap_region(struct file *file, unsigned long addr, } else if (vm_flags & VM_SHARED) { error = shmem_zero_setup(vma); if (error) - goto free_vma; + goto free_iter_vma; } else { vma_set_anonymous(vma); } - if (map_deny_write_exec(vma->vm_flags, vma->vm_flags)) { - error = -EACCES; - goto close_and_free_vma; - } - - /* Allow architectures to sanity-check the vm_flags */ - error = -EINVAL; - if (!arch_validate_flags(vma->vm_flags)) - goto close_and_free_vma; - - error = -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 /* 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, unsigned long addr, */ khugepaged_enter_vma(vma, vma->vm_flags); - /* 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 = vma->vm_file; ksm_add_vma(vma); expanded: @@ -2894,33 +2884,60 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_set_page_prot(vma); - validate_mm(mm); return addr; -close_and_free_vma: - vma_close(vma); - - if (file || vma->vm_file) { -unmap_and_free_vma: - fput(vma->vm_file); - vma->vm_file = NULL; +unmap_and_free_file_vma: + fput(vma->vm_file); + vma->vm_file = NULL; - 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; } +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 = 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 = mapping_map_writable(file->f_mapping); + + if (error) + return error; + writable_file_mapping = true; + } + + ret = __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;