From patchwork Fri Nov 15 12:36:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876166 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 08230D6DDF7 for ; Fri, 15 Nov 2024 12:37:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 72B176B008C; Fri, 15 Nov 2024 07:37:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D8036B0092; Fri, 15 Nov 2024 07:37:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DC586B0093; Fri, 15 Nov 2024 07:37:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 293B26B008C for ; Fri, 15 Nov 2024 07:37:31 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8FFAD121456 for ; Fri, 15 Nov 2024 12:37:30 +0000 (UTC) X-FDA: 82788277740.09.E79D912 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf01.hostedemail.com (Postfix) with ESMTP id D838D4001E for ; Fri, 15 Nov 2024 12:36:51 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=QH9ietNe; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=lvhEGq1R; spf=pass (imf01.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.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=1731674070; 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=lcqVyGR2Ie4RX5B2PFQeOoDGKUd4H35f3scPpB+0CAw=; b=TZjVWBKpodNDeR2uWVPXsfCtoLx4vZgow/T8LhRNtobPXMltG0Ghm+01zQl02LIhrCZw8t caAqPBIQAepGNTDy+sYbP2XS43w15zjSFdLa+Hu2P1htz1dOTiWVyu15Ak5/hAcaUgRdAd l9P8WJAujlKeDOy3/aUF+iVV8Db25K4= ARC-Authentication-Results: i=2; imf01.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=QH9ietNe; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=lvhEGq1R; spf=pass (imf01.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.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=1731674070; a=rsa-sha256; cv=pass; b=H6kt+CuC5gSxsxJPUMLilwPe9LxZMOqOCGxkt6++ozxNh1Ly1v2jufqUmghrRLDCaR6PDN Gs62lH8wyskzwtGqna3CpKN08yp2Jx0C/03QqzT6o6BUbXU8mEGfIUo7AL/+H05Ib05ZSo hXGeThpOlfzF7SfZ2jYclQn5uHJKFMI= Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAHHHN030318; Fri, 15 Nov 2024 12:37:13 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=lcqVyGR2Ie4RX5B2PFQeOoDGKUd4H35f3scPpB+0CAw=; b= QH9ietNeyYDtg8505G2LIz8MqSlrqw+Yl9437iMoV7gSq4kWT3jHOtOnurJ9AnTk rYyb2/EXEiBv7fsXfPC5RtMP2Uasz+Unp7BENqrhQMx0UKT8U/c0yh8hEPYrjgIK crqg+tQu2MW7eXBrYzC4yS1D9WunSpi07Y+DoDegJPo5LcvsiK8adQuK2g8jR0/f EigdgirQQXoPnb2uHGCsRFRMvMOnHnCSA0OHpNdk5V+vSK6k1lm8uVTeDYlOAiFP QZVK9QP0VuiIxuteG/hod1fCYAI6Xf19V8EkERiQsNjxjwgN+W5GhRrEGpKxRj7T HsmjhEo7C5+vAQjYpdjSBA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0n539t2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:13 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAVLFL000376; Fri, 15 Nov 2024 12:37:12 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2171.outbound.protection.outlook.com [104.47.58.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbjcxt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gaqEsaUZBgsLeUMM5LkOvfUL1X0V5CxFVXUqOlD9L2zokhyuDlZ8FyF1tPGD/3odXltrQnmeQuznkxvEIQ/DM+nIJTawBnN10oSjrS25q+kZYV/7nNQXZhpVrgHGumXFm6Rtw8YjALQq+t2uob0sZh2SxSnK/U7o9I6p9ZwxjogTGhoRqKNNTRwjncOZ4nZb95RjcHSLKoqSHO+zHY9cKEQeP+nJoCdgNG6YxJq1ogtA9abCVgNU6Cas+ShfLo7jW/qJDKtD2Eyu1dgo35+y1npRPdJ/OUVRB7L/XnZj+3xh/1a1tSY/7fVxAjWM/3TV4y+mnKnUJQOlDJgQ2a86Ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=lcqVyGR2Ie4RX5B2PFQeOoDGKUd4H35f3scPpB+0CAw=; b=pvZxGwJg3TZq/y2VySBIpk/zglY9RFm3CCQR08FZvMrEKJJ+2sexqOJ3BiY6W0GxmxV+2nJ2TwBEJvdsXFEBRZ+UYnTpNuBM65ZJR6Mre6Z/IOgiqnfGvkYcSLfNp+9WbuVGv7WzyXcKJBXyMAdlW4td3V6fHqrKetXjVeOyrp3+hpPhegw+ejQvgKVTgYoqOrl/k46vrnvHQZjC6c4v7Usz1Dbq1Nv2AGcizJ0N7CcP99QvhzKj5+Yg4zl+CLj7DJ0go6vXJPzqYFEUekwCi2Pw4h2Vjq3r93tDEdIZ1RbPKopkyoTu0Fnmhczfzw3rpqKgZWE8Ac1qoW2a3tvWKw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lcqVyGR2Ie4RX5B2PFQeOoDGKUd4H35f3scPpB+0CAw=; b=lvhEGq1Rk+Rj9yoEYJwb4LiCbFo9ohkp6p0a6Fj4STZF6jVSIeq34OmEtBpEd4Iqe5ssTNo6fI5blkTEKx24Enz38m63yE5fvWjXYbYEfNZaJZYvUdoWrQSBTOXKJvq1JthBfDhYes5FUVcqeWWh3hW3ZpVZft1DjIPe/1p0oBk= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:37:04 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:37:04 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.10.y 1/4] mm: avoid unsafe VMA hook invocation when error arises on mmap hook Date: Fri, 15 Nov 2024 12:36:51 +0000 Message-ID: <3a4ff9ebcc085e0074711ae883fa5ea11e6d7a8e.1731670097.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0165.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:312::8) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 59ca1d38-1a74-4ed7-0ba4-08dd057235e0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: JF9VHHAz8zQdXGfBOn6oXH598n+rDaDmCesiJTAtYy3e+0jhAFqwFhCZ88Fn7UAm0ZRlM0YQSS6g6UeiRjDE0Z0Wj73HT2hlDofwBNmZLNm9uSsqRSU9G7ROWvWpG4kkJpSNIdIlOy3gEiLoOeAW/uJeTlb4nplZxyn9jEG0H0oxED1hiN5BamvtUJ7O0GQFfOTkqhuRuqDzwvic4J0D8P3lMvf/CItQBXSwJtIil75uPELRcMxZ/5SLT/xiiub/+Jb869o240Ww15uh31DqJk4q9iB5J6mvVVgqQdny9BoJeOI5kjqobgYB+ZbIaMv73CzmfcST5vtnJGgSVkq4+dqhaTq06D8DjP8q+6NZTR56/nNfPPuYwRBmXxC7ZSufv7gHe/fDykOLPsuxO0cqCYGLVvapo57rabGylo6HoQ+GwERA7iHeRnxTGFB+KsiSecJGXMt76QoOObM3J8ZMcTYGQKCwFcfGkcq/4Xwo1J33othgTuTlpDMq8M1VaLXwtiar1lxTUEmi+j8NpThdcSnxOBS45HtiaIExguuQcu1w3ialy2D5UEigaXQjtnBwoqkcWBvJJjIWNjf6jsCK+gE5fktssQe7onJ+ZVwmAqItHTnHYRIPF8YkEKxOeCid0aARG1uMyRuqC3ytnJQ54+wttjZJ9bcK75DsQHFQlWRrXhx0Y6hr6AvkpVXZIZvpHiUFBFAK41ZDuizRzljD2nJxz22hjY53NASb7SaSE+pNkyEYi3ZYhpbv90vw0nDn9dQnDOkMWMvrN0iFED9/WHunXOm9TD48dTtHtNVYLUAK4fNDVl2cqjrmK7w+NiDuH+vzyDG52QVy3RO6qWb62ZJnaFPspQ7E52bpXSNwGx77LI/qF/V3UuvLQR727tI/Lpf11RAhmPMXF/J7oIlEs4YhJ2BA4NFtZC7IjGQMBy0H5gcN7NPdoxWLaCdVh78gJQG69aNcvravigv85m+rvlzlYtR1B10aX8J0GV8QNfNmFVPCmaAcU2hgNZsxiRmvtGSgeJtlVq4gxD1DsMdrc0xJ0u09IHTZVXOlH7YO+/ci8ysN7bj94Yg9wJwegq42CNxS9OI2PIJ4tAfW1avzTRIXY9uGjxhtrYDZk+c0pHB3XuVyICyiPxQPTL0MwOalfzIEYUXfdYg3nhkJ6XFh95IEdp2g4Z7r3mGtDQELWeD4hX7dENnRcJAdzn/Y78mM9Tt2gYRKz9UuROaGNoloxc3IIUF+kE697jDfUsBsw69qt3GkB2J9hIEbHQZjj5RvsPkRKXFM9dCLu3Da/FuPBn39mtO+Cbzd5ACTFiCNlCV062n7iNKrz6Os7/ECvQLy X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fqwdnypkYd50oxRvFhzJ2zeMXVjDISfDZbHeRsp9RQUaICyi0rDngFNUGq++826373vjzQ9FTP9ELLNGLnjyYu7FqXyVbN/07TrxKUeqCuxtjdxZdVpKo+kkouTSo2TXIsF/XhVB6AZARZ7DldTSEAKpkYCU08cslNmrh79W0M59BI3gPCQ8k1yUCx3VPx/VH95WYsMXG7+3i7be84k+WBqup4nxKT/hkn2+blXWnsH22E2U7ozsG4wNN3I2TXR+w1xo3d/dcTngiZ1a9I+NCew9GhHYw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YS32WTu4inyPQyU3sliOZ20a2jcS5uKckqj460vEY7XhGMVSrwydAWw/VN83FthRQ+vdFCQKONerTKoGrGLL0BKOFM1ezJxbQRjvjGvFmeG4qbGEvTGDH3pl+XzrBAIG2yOZ30XFtHqZWuoDjEb8Cf3bvsCkkoFaLzj70m/1pMjIj93FYVAwr59kqIvxuUpNnPuLHOVbEPg439jUcTifUkDs99zeBik8zGXguS/Tpa6b09PjyAOzNRorQwrBakQ== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 6YVoWqV6rPXkYHEFm/efmy8Zs6xf/bkVX1x3Env5Sgx4u8Tyzb+g4CW5mgODz9wEmLTIN27SpWHzaIWYZLoMtTXAKtkUxErjxZxTNYOByIoP+/jy7RNFZ0/UNdPna9CU0NERHnwvAIlzGwipmdvFvUn8oU5PRrAbPirTY15MiXjykotTy+Xupzd1jZjOC+yJ+Lux966+qX7Dh6cxdY3kWNyzLINW0b8ecQzKNv5beB0ycpwCX91z80K1lL/XWRiQHFMEklrs4FqU3oKXj+56vcg0U5dpQbDhjA3LRAyLWD/Iq7DnZ+yr8Z7aLxu8Bshqh045K38tkT+UO6RH/H2ZqEWBNcSI0Y462Ewrtp1hnWjBcu+YT48n6lr8Qz/ul8DtNx/xz3nDZxJW+PjLuVXVXwps+WtXOfJQb3F2SkreKLG+Lpg0bFKpyLFEnX27lvV3bmFBNHAq1ySjGW0fh7ep9etOwup45U8SzmCrPNP2kEpTZy4jb+ypHb9aQiWKFBArnC4vXevS9DVnIylKnJB51Yuu8I9dwB4ce1Pj0JcFkBRrFgX0AzBhwT9qU8NkKbCxRUjBdkW99yHWBexrNCSVwuCaOj6OC69z8qs5BzzImUA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 59ca1d38-1a74-4ed7-0ba4-08dd057235e0 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:37:04.7861 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: RfCQAmEElMFrFzd+lKA2DQ1yCaIanAMWmiI/0Jp1PtWx7rVWGNM+wrXpBhdCvV6qT9Nhf9OLHtsj/lAdOSNpED/UDHTGKT9dFZ5KCxFDBtU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: dNe-1qRMSXA8Xw9ueu8mH48DW-0ogJiM X-Proofpoint-GUID: dNe-1qRMSXA8Xw9ueu8mH48DW-0ogJiM X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D838D4001E X-Stat-Signature: oexssmbrtykw6esjcimitym54ka6o6xk X-Rspam-User: X-HE-Tag: 1731674211-550343 X-HE-Meta: U2FsdGVkX1+pzZdXkPfaCamufCO9xIfMKy1J0v9lrRqg68fMI9f0slZfWpqERo98+ibwZqFzGYuLQkr0zUyUf1RW/mXNTwPo6behuyPjW18YvozFiFNGwDnhMVQp7irET2qfikupj4QK54iXDI3//KSE+hvyoo9YBLUrQaoAWYYuLHgr/IdA4FkIXneJle+v1Ltgss8WA+jzN8/liOu0CWzljYqboByDrIjaOWz86HsvLBhZ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fdLWMe18tommD8mtj1UTBd51JlPQGXq2uekPAwwnfmVhUlWBbxTiAzQ7TgRkpZYM++bvpk8Rpzfqn+1/Wa2Hrj5jyVJyqlI/8ym0rW9MkYTrAjWnxezGMyllUyiHDMe0tBD5T3ZAPAU9ewS8Zf0VPO6yk6nodcD+sgYCAOOUv3qUNOTJ2i4EjRueI0wExY5Rciw/CJXirfYjlYFsaCH41Y/9wal49x5jQGuaiNDIlbqvLw2E2fcvIL+LVDUASEWNNHUh13TuYz4s5jijJCBtAVOHSSpckn5fT4i0f4bKcCJf097McHZ52YYNemDw3dcEBvr4Ao2vCLTgVrbLlCBBPdJqgXBReJv9hG18HggThBJAU34Lk9DK8XPeZgqCOa70pLKnCdvRqXKU+AQPiZXOmX9jfDhDpRI13zcLZLXhUCZ+V9i3/qnByNvWmNPhikNFZlgntaNkR37Ysir+Mez00YDrEg3BjeC6so98Xm60O412vuU/SRAhFvBG/bgSRYshM0BkGN8ZTeGXNxHDUHfneobxV37VXbUiR8JmXry+Dc+F/09ETL1+YlqLQ6PSl+kYkwzFcAVnKFSmLHOcsHT+mAAKC8jV+O+fe2JNS5NMK69MM0jgl7Yaqn3nFDYuc1sitkPQgfro0l3YO5SaD G0PdPF55 u2hxLQhBaXryPhk3Cpn0iLHOackmOEObOPeJ24wfmhs= 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 | 12 ++++++++++++ mm/mmap.c | 4 ++-- mm/nommu.c | 4 ++-- mm/util.c | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 840b8a330b9a..e47f112a63d3 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -34,6 +34,18 @@ void page_writeback_init(void); +/* + * 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. + */ +int mmap_file(struct file *file, struct vm_area_struct *vma); + vm_fault_t do_swap_page(struct vm_fault *vmf); void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma, diff --git a/mm/mmap.c b/mm/mmap.c index 33ebda8385b9..f4eac5a95d64 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1808,7 +1808,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, * new file must not have been exposed to user-space, yet. */ vma->vm_file = get_file(file); - error = call_mmap(file, vma); + error = mmap_file(file, vma); if (error) goto unmap_and_free_vma; @@ -1823,7 +1823,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, addr = vma->vm_start; - /* If vm_flags changed after call_mmap(), we should try merge vma again + /* If vm_flags changed after mmap_file(), we should try merge vma again * as we may succeed this time. */ if (unlikely(vm_flags != vma->vm_flags && prev)) { diff --git a/mm/nommu.c b/mm/nommu.c index 0faf39b32cdb..fdacc3d119c3 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -955,7 +955,7 @@ static int do_mmap_shared_file(struct vm_area_struct *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; @@ -986,7 +986,7 @@ static int do_mmap_private(struct vm_area_struct *vma, * - VM_MAYSHARE will be set if it may attempt to share */ if (capabilities & NOMMU_MAP_DIRECT) { - ret = call_mmap(vma->vm_file, vma); + ret = mmap_file(vma->vm_file, vma); if (ret == 0) { /* shouldn't return success if we're not sharing */ BUG_ON(!(vma->vm_flags & VM_MAYSHARE)); diff --git a/mm/util.c b/mm/util.c index ad8f8c482d14..8e5bd2c9f4b4 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1073,3 +1073,21 @@ int __weak memcmp_pages(struct page *page1, struct page *page2) kunmap_atomic(addr1); return ret; } + +int mmap_file(struct file *file, struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops = {}; + 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 = &dummy_vm_ops; + + return err; +} From patchwork Fri Nov 15 12:36:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13876167 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 8ADB4D6DDF3 for ; Fri, 15 Nov 2024 12:37:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 10ED66B0092; Fri, 15 Nov 2024 07:37:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E6136B0093; Fri, 15 Nov 2024 07:37:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D43126B0096; Fri, 15 Nov 2024 07:37:32 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A99B06B0092 for ; Fri, 15 Nov 2024 07:37:32 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5ACFB1A15F7 for ; Fri, 15 Nov 2024 12:37:32 +0000 (UTC) X-FDA: 82788278622.25.1263858 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf28.hostedemail.com (Postfix) with ESMTP id BF629C000A for ; Fri, 15 Nov 2024 12:36:42 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=GutEOMx6; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=Gw1PDfXA; spf=pass (imf28.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.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=1731674072; 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=0YO6r84z6d4rGgD1e8mQW4WUPkS0navErPTnR081+LE=; b=Q+OFIrO5xjlxlKb59mA3x5AsgYHL7gqlWoKxp25Jba556qps1IZVilHdMAKN3BgNRoxIlE p645ruvPOa2hOpRHUrZLVnIqL587ZJcoAD1YL+tNifTV6NMnvLgN6Lvpuxg93ZPWRqNKIi V5+4GrBnkbDo/3RMaXBlYwu6OurS2Bc= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=GutEOMx6; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=Gw1PDfXA; spf=pass (imf28.hostedemail.com: domain of lorenzo.stoakes@oracle.com designates 205.220.165.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=1731674072; a=rsa-sha256; cv=pass; b=EipZZxm5XTREn3e3grbvGF2FV9Tar0KRrJK6FEKtf7vfNJweIdnrg1r0N/tQYWHUri/VIg fAxcz9QbWxsJmYSGBONKYAmR7xLL1V6/DXAxLO2C9RHEcImQ4BGKe56prcqANmuZi6HW5W nyvESYseTLD6YMszAkG/vle6+KwkbHk= Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAH4VP030282; Fri, 15 Nov 2024 12:37:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=0YO6r84z6d4rGgD1e8mQW4WUPkS0navErPTnR081+LE=; b= GutEOMx6MiSjMMA8ufsOVfF9BfPzNWusbZrMb/+3dF19/UrbEWV72tGoNfAGlzV4 QcMONCgpXzpEBipFjNO0y80sax/JxtJ1c1Bpu5s22ACH0fQTtp78RpXrxZmdgi0s huvhyC8a2rrzvHO8S/reAyU8e7wfYOggFrueQvzdf0HS1nh5uFbriu28dFxFWas/ cfi3jtx/Ll0N54mPRjt5hAvi0tDuuvaXpZ3dEHVCEUS1LR4f9b400t14I/pwulEU +gWsXht2JxTb5d9yjji/zI8fZtsDpPHDDmZ3nxiPWf5NdxBULCiE9vQWhHgdMIec f47IGbPoFw4J8rBkGmI+Gg== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0n539t3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:14 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAVLFM000376; Fri, 15 Nov 2024 12:37:12 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2171.outbound.protection.outlook.com [104.47.58.171]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42tbpbjcxt-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=oV83LeQve76m8QAG/X8OqOiBRb8FJLeOuYXKq4JcMfNgDJtPXlf7CENWaXxZYK9a5fd2v9vyuoLlRR0CR5N+np8mQvjjKe2P1y4cz1Rdph6BVZya21KNQM7wP1Y4AkcEsW/x8Vi2pse/vtooZDpNETZd53cTg4hRkKQ0X51wmmBSpDsfRSvF8VbvXQQHElHfVnaewYcKQKMx0EZWIkiNi6I+a+4V8xwERj4iSGYBbmKwhz+ubjtw9JvSVV1lnli/BbOxH2EzcD9eQLkhi3GCVcGOVoQnirKrA0McpL/VRjfVILtpQCof23rQ/Zbk6DkAlvWOeVYohU/eHl6Wq6/Z9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=0YO6r84z6d4rGgD1e8mQW4WUPkS0navErPTnR081+LE=; b=KZ6f1+AUGGlbM4FL5slrDDEcsVbq5eDYyfTz77AGlGyVQf+yPs5Wsm/2JzT7gRyRnBpj7MdJbYw6sxgYaDcDeNA99SXACIVgvq1tmY/ZowjJps0ajWzVlRzpy5sf633xs7sgTVq5nmqO+njoYGJMvtA3Qhv01s1NAVh6JIam5liq5Iai8rhPjsEAxJGCnuJ9WycOM2anmm3XPxmtBRkFzWNIhfMg20/LcWKZiieLdzF4UnGZdURziQABo+jKreEgPRuL30RYO9E4L2waOxyW4P8J/V6riboaWlCQn0YnzOYaIY8ejaZH8YTn8P+RtUFkSRfsCR/Mh3aRSHC8FVywdA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0YO6r84z6d4rGgD1e8mQW4WUPkS0navErPTnR081+LE=; b=Gw1PDfXAa5WVCgPjLJ70KiTvE3FOn2WyIDCvoZcZuuIuv5ZU0MwHYuOg/F0hBJCo1uD2WWgTsNYiAKDIGb8ntwzBF6/HfAZPuNLUNU2WndZE68DWWcEuG2t0b+mMcgDZU8CqAHfpHpXSvafj5gPjXYnCEjdbZ1diRGQDYV6YeT4= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:37:07 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:37:07 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.10.y 2/4] mm: unconditionally close VMAs on error Date: Fri, 15 Nov 2024 12:36:52 +0000 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0450.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:e::30) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 779c2df2-6419-4dde-1372-08dd057237aa X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: 2Wa/Pjh+MfjDfwQUMMLyVsHbUhLz1pOUhXQbgVN+SSqMREUJloiSRwkqCN/BJzAAM3ctpIZsPSbw6ws1MAGqC5tXbV/DdUKFQ22t5bYcjcRWJzveva7EUrLAWMj3Q3qjquSrR+4hCz2Oj7TYVdX6wEffF6dtsI+IN8ciA1ZjVozsZTndQYrvD58gNmtXYXuyKPqxucsmQMExiwM2jG9/uSJabrH+NKIYc8LP6m2o7M5nJI7ydxRJo5mv07hFF3ltDZTPo+1dggunW8ypKv32W7j9GLiSsKgtH+hdf0xQmxSyg+9e6leDc15MuDzZPx4/rpO1lSqvW9HmSL4qkjwbbrw8oclgaYXRNxnzFXIx/ttMIBHTFI71glff1COqeOwyUGA9wBooFZsVwjr4+hwor5bpnh6HTQ4oaRmaEK7IcctPaMPPif0j9Lb++kjFshu2vqCcpf+vl86pIs8I88YrEmBBibOZ/NHLbwwdVB0MK6fqzAPTMFFnfrdBWycc6edbQjKN3vfXUWssGd6NjCySQlgLxnGb9TpIuuKoJNp7PLOTCBdwCt/RNXMIxMeORZuoZwUgXcmlrWE0FC2AKTwzlt4nl58fvUFlUkdSJ4MjHTr2IFIez/mD+XGgl02XroNOVSCgY+tfW+4iTp6PA5/n1wQm+lPZdxXEbo2/+k2LXsLD565CK/8YY1eZsB23ZdyPK9uevVrCPvL6QDt5ETmAWYcI/Nr1C7P/rMSp1VD7XroFTy7704lzsWZv9PX74f5vX4QP60IbRf6XktNQOh7hcTjoM4kYmH5VF4SzSfeH8iTxrXB9+5by3JloVOLpuiTq5+4V/NKC79AowvZj4UKtwMcZOB7tDz9FudtlmnG8ButZ2g+A81s+JggWefyoEzuuFO5JmHmVY+v3WQF+iXY+yf/83uGqiz4OfTpSUHFP0kshFFTBG/a+/Cjb/FWKtNOYbunsu5n1s6DoXoHU+HZb+dvJnOLxKHRLs8eOeGV99lD7891lTOjW/Y7Y3BzzNdKw5G3/K28bOW1umen6eoE9WrEvKH+YOlpBkFe2wrJoy3QK+ez0kko8uLJ+DhCA/31Pa8/he4fC23PWw6FZ/V3jFmTvEMxAdKiiHvPWKb66eiJI1jim7Ns123Z7Gfv5hDwF4RGz3Sz8wTZTM8RAWTf8b4l9lvrpOTeAF79GCF5B9M5e3k9EkMr87gEdopLIbmGto85tnWJS/whqXXcZnBgv9shlqMGtZKGy6Nxu+uoO35BhlGMHplc/V5Zx7/zAST+O9OH39zb5EELrrbBLB+hmilkB8FOCWP1BSj5i0ifo47JqLyqZHyQWNsfCg1YS2Bg4 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OkMk3R2T1ciZYzP7MR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ylYsAP2GlNEkJGni0SaGp3wJ1uEPVbEzDIAdXBux2dg5S+3YpgI89s5PHlglo3j2dTXA/kMegjZWtOm1xk8ZHHq49uENn6lHUOtu8viO7OlJhc6QBOd5ahoMIEYitcp3koMgb7Ae3ii96jCo3OmDFdjIpze1AJgWx8OY4LiewD2g2HtuAgv2PBisI/agy8K5BjB7+8U7QZvPvqEYFykvhu/q8/4yP1+86Snkhx0dbARqOxnfCy7xsjBufjVmUpMk7hFNqdU1LHtZJunMlodX5adGD0jSet1IJrt5MCI2nd5BOPSOm8hH+gPCn+G06V7SRDpQTrygJvwe34EeWmv5WoWsT7qZNaqhFoKccbyQexHLLa62uo2MpirHlIpjTK/Fb/xhiuTJjwgXcNx8zhTO0fFa3WgreqQ/siIBPO1EQXtgsI2Ljib3ZsgotPuwx4Wiqyag0LG2cOLZbLLgFxs1Z/KKBTslsErcKiD7JOHOOf3iK4JVWIJIdDn43/naNVuhzqgDbis9tsvQfwX168ua9g== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ogS0zrO7RWzStu6SGHNdXfKIXJto3uPVBzujEK7pPuKCMR6K9qL1nmzSGlARWGy4xAVYELnQ+5inZ59aHP2fRmhl4my2xFVE8r63ZoZfv1ythU9+CJxEL0+qEkgya4MHQyYb5sI4oIyMaA+yjWwYGw6lQmiUbbCCULn7L5hxNoxakd/fn+cQQPRhynSuYVfynQtglWH0YDjN/ppALJECs4D+zoyg1cU69TdNuv03aYEix+XQZtqPIXawxUp+hJXlhMNPqpG3BGYHRUguA9gZNqh3PT+WF8lxlrcKyDIRwk+ak1MGVnciKyYCOeDds2qJz457w7RlvciTU3Qu1OgKpN6alDyH3KccusnRst+skpswRZsNA+Ns5aZpyHXBepUQ01hJBIqr5D/fVZUTgoi5/0cenwW+ozZ5gRwJ7eo8DzdlrP2xMekMqtazDAtSGUrciOhBD1npcOM6KvqIZfMEkKp5UZ0/ZN6pd6YC766WnhHv2lFVPa1gjSNEZUlXSnI3DpA2IRxCIeIaGnEFALta4Kd2s7MrKRXpqqix1f9LqSITtB3hS4del9jaJwHhMu3oqo9IdDxw2z1L8lS0O+uf6hrFjlEIB4yZlq/U3hMYZ0Q= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 779c2df2-6419-4dde-1372-08dd057237aa X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:37:07.7142 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: bA3sNcqbSpzyFa67M1I6eC/KDnN72Ds6ansCWnXMejIEbj7Xatk6Cof7ey1UxHNwX6wTFu6ROatf714V4242tOF1FbS/dvWcHGYSfY+eLbw= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 mlxscore=0 malwarescore=0 mlxlogscore=999 spamscore=0 phishscore=0 adultscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: 8N0rPsEK10oZH9mlXBsUAiJLJpU5ohVg X-Proofpoint-GUID: 8N0rPsEK10oZH9mlXBsUAiJLJpU5ohVg X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: BF629C000A X-Stat-Signature: 4rh3tdx4hx16j6my6za1sq118g3zmf68 X-Rspam-User: X-H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ug/CmvNpsA3iRTP5jh6rzfqT6sAgFxXCUDG7QD3R6ieGWw+eaWuiJFR3+0xjKvL+XdnbQMiTWmO4B4FXBVoOrSAMHFpYyYCcHySsu0ZLLZNtCuZ9UcFae7sb5XvvXGdcM94A6TnHb45Fe0rBnaLls6qhuwqahsD9U1b4lBTCMxJu9M0fRPjgn0dUkVrFJafGIxpu+IpQ7dR+KoJ7dHBqBeova5DpJdTakJCuldMKkLfj73B50Tl5Ipu0NPwPojWfODg36bHGuLYuTs1nttNPedxrE4NjkwufWrMLTzmptpTl0vysI85wOOLoFy2+oEUmd7PODyDYiiovjM/JvOvtX0ZCXRXLBqzXxwH7+dMwAbDh8FArtwIzrwlHK9bfLxfEnrSTNEaTpVoPOUYMtdwPVsWDrjo/3MalksD2Y68rzbZBlPwO1f42Ssi7Xh9qMEYK0KFgkckFvNwMelBM9KFphhqH6E16DeafnBlpc4S5ZqzPgY76YvY2BVQn+qlx8G76wP5UZn0Bj15DadZ1MJ1x0VjX5lepfJIJdJYAxA9dbkXN31qN8rVBuj8JvxhrSXydTnLyWY7hVcY12NynMQalP2kqMSrhUqDCGUUUlPaoCAlADmZ4XTz0kRp2nEaggE2vao8wN4OHObRspdT9mmwHlQJH2YCdNiNCsqZu536pzQmFAoUFdwV56hDFYMBhLY8bjDcDpcdvF9+sl7E+7bxElO582B0+iOmuLoW30mOJQONDjqeK36y5XY5hEJSFFu4HX5BS9riPRV3+s3xUOFaEjV92szKLoryO2gCfEP63P2RjRfxBrCwJLh2ffCNBfiKbLAtUr/XPYDOUqjl7dY4AEas4qoisP+4YZfKyV/wv+uV1rEJpugx/mbkOVFuqdRK37cjqnCks+ 30YnnuL/ 92T85XeKRKQ/YVr+0R/YABa+fVOQ6mWljK/tubx+/gQ= 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 | 7 +++++++ mm/mmap.c | 9 +++------ mm/nommu.c | 3 +-- mm/util.c | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index e47f112a63d3..df2b1156ef65 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -46,6 +46,13 @@ void page_writeback_init(void); */ int mmap_file(struct file *file, struct vm_area_struct *vma); +/* + * 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. + */ +void vma_close(struct vm_area_struct *vma); + vm_fault_t do_swap_page(struct vm_fault *vmf); void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vma, diff --git a/mm/mmap.c b/mm/mmap.c index f4eac5a95d64..ac1517a96066 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -176,8 +176,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) struct vm_area_struct *next = vma->vm_next; might_sleep(); - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); if (vma->vm_file) fput(vma->vm_file); mpol_put(vma_policy(vma)); @@ -1901,8 +1900,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, return addr; close_and_free_vma: - if (vma->vm_ops && vma->vm_ops->close) - vma->vm_ops->close(vma); + vma_close(vma); unmap_and_free_vma: vma->vm_file = NULL; fput(file); @@ -2788,8 +2786,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma, return 0; /* Clean everything up if vma_adjust failed. */ - if (new->vm_ops && new->vm_ops->close) - new->vm_ops->close(new); + vma_close(new); if (new->vm_file) fput(new->vm_file); unlink_anon_vmas(new); diff --git a/mm/nommu.c b/mm/nommu.c index fdacc3d119c3..f46a883e93e4 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -662,8 +662,7 @@ static void delete_vma_from_mm(struct vm_area_struct *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); diff --git a/mm/util.c b/mm/util.c index 8e5bd2c9f4b4..9e0c86555adf 100644 --- a/mm/util.c +++ b/mm/util.c @@ -1091,3 +1091,18 @@ int mmap_file(struct file *file, struct vm_area_struct *vma) return err; } + +void vma_close(struct vm_area_struct *vma) +{ + static const struct vm_operations_struct dummy_vm_ops = {}; + + 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 = &dummy_vm_ops; + } +} From patchwork Fri Nov 15 12:36: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: 13876168 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 616A5D6DDF7 for ; Fri, 15 Nov 2024 12:37:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2F056B0095; Fri, 15 Nov 2024 07:37:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D90D96B0096; Fri, 15 Nov 2024 07:37:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B22AC6B0098; Fri, 15 Nov 2024 07:37:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 890F76B0095 for ; Fri, 15 Nov 2024 07:37:35 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4DE7EA16EB for ; Fri, 15 Nov 2024 12:37:35 +0000 (UTC) X-FDA: 82788277278.17.815C76F Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf25.hostedemail.com (Postfix) with ESMTP id 2C034A0005 for ; Fri, 15 Nov 2024 12:36:59 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=NqPYSABS; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=FEasZOxu; 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=1731674075; 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=HLLzt7YUAlhV3V4zmpe3bPdi4KwZJIGMsntE4y/q8EY=; b=I+XFpue7FWEvpC8vT7/eCDiEVooEvgeNkp3VAsxi6RGq+3vZr63jxfSBQGKeIYJBs+eOej oJrw95tFyh+vsb/qe6V/OgWO79wGx4X3JakyghSJUQwbCrK8Vz1cdZJf+k2EWqi0vHnPAF cJML7JpxrH0YLJ3O/UXoRfMDj3Ss2x4= ARC-Authentication-Results: i=2; imf25.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=NqPYSABS; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=FEasZOxu; 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=1731674075; a=rsa-sha256; cv=pass; b=77rNGBNfZenRUS546fRCoKnYElfZRsZjRh+rMrdQqbo1PGZvrCaKh+CQV4X4FL75beZd58 BE1cYpaFe214gCCgPOtcSrGOxp7fn6b8ppqJV4V+4SISvj1I8QflfDeNfCPiXRtQxIrhdp NiJxIqARH4/VvSygjgp4QfXD6/QP/B4= Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFAH1Um025789; Fri, 15 Nov 2024 12:37:18 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=HLLzt7YUAlhV3V4zmpe3bPdi4KwZJIGMsntE4y/q8EY=; b= NqPYSABSui2LA03XGgtSmfPBg8En1blAJWgeKxzuM/4QDJkv8f+U8UGncxJ1volI eSmE9cnMXPXKPEKUahLhqaQsTWijBSNHzBKUC/hpy5yq5I2laYvgdb5Rm0knaaL6 MOgM+jYAwVtGOgF7EdgEySFORFGtL0HMP506xCbbgkZo6RzCaT1gR3SrSvni0NY3 wHuMpDUKYx+zh8pWJcRS4YJbMTlEB8zRjd98wmrNASXfccrnfh2SjOK3YrL3SSC/ x/jF/oPE0vmhue86DDGuqexnEuh83+2AC9AVpvhB4+eROTYxahw8y28U8KNnyV3m E03du9qvrS5DClJlKsaUuw== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0heu6c9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:17 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4AFBXWnd001273; Fri, 15 Nov 2024 12:37:17 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2046.outbound.protection.outlook.com [104.47.66.46]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6ch2s3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 15 Nov 2024 12:37:17 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=gYt819jmfsGGlKuBUSotgXm+BvAPflHJsKu2o0N2HBC6X/k/GLxppqw0xZT+8w1ltDXX0oo53kJ+c1izfRezluaAGHEM9/PlrR/IQJllDepGmDNa66gUkknhVLgn031rkY/h8BOgQam0TlXabmNELKTa6EUM86+OCTGTfmx9+MpAOvS+09Cdzw1oERF9a2SVzD0U23kxjTnpMwtrjz5mMuAkL38VVxuzc6S6A0Ei/bqoQexSWHJZBcDnfZWpKqy6ZP04iwXTRXaSbPsgVEvqrEAvi35HckLlnTncknkQURUiRnwMxNJsHYoWMSBtV+q+u2mOsb49gBMUAnAnZKfWAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HLLzt7YUAlhV3V4zmpe3bPdi4KwZJIGMsntE4y/q8EY=; b=Zl/MGtYaDHxidCQh94Y9vw9GGfddfe76elTVNo917SY0tR1vdgDUo6bu02GdscYiL9GgNL5RxT8tEcydswLFkhi8sWf+z7KXJTE0pfJBdOL7MloX2TpoOFnnH/XIxr1MXz5sjeaRpLLc8qcJ8iZxtOXBYsEotIMUiqK+X2YIJxeyJa2PtoJ5WceB/fOoo0fuQxEwsAbp+vya5378PZ6eY4tKiox+nvgGPk+d72sds9Od2G5W4R0CSDG5OdRaDbTplKOYCzX9HcVDFtU2Zi9sjU0s/esKcr2wXxCmUjVRn5lT1EPAxplNSsAOxeHbtTqWiXQG5rr5I4ZrhL2oT3ueAw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HLLzt7YUAlhV3V4zmpe3bPdi4KwZJIGMsntE4y/q8EY=; b=FEasZOxuV9a+yAKUVKMybysnQR0rQb4Huk6/dJGGpo9cZvQo+7K2bcrrA1q9RyCWK3jHc67Zb2aV/WMa9Wo9EjGUCNaVSD6LCzgnANYzIidMQLkg4PA7w9xABaPTeIm1i5u0rJq/QCw7i1WqdgS/yBaIBPKi3hCqBFJmalxELqM= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SA6PR10MB8136.namprd10.prod.outlook.com (2603:10b6:806:438::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8158.18; Fri, 15 Nov 2024 12:37:14 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8137.027; Fri, 15 Nov 2024 12:37:13 +0000 From: Lorenzo Stoakes To: stable@vger.kernel.org Cc: Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Jann Horn , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , Peter Xu , Catalin Marinas , Will Deacon , Mark Brown , "David S . Miller" , Andreas Larsson , "James E . J . Bottomley" , Helge Deller Subject: [PATCH 5.10.y 4/4] mm: resolve faulty mmap_region() error path behaviour Date: Fri, 15 Nov 2024 12:36:54 +0000 Message-ID: <99f72d6dc52835126ca6d2e79732d397f6bfa20b.1731670097.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0438.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:e::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_|SA6PR10MB8136:EE_ X-MS-Office365-Filtering-Correlation-Id: 1d98819e-3e35-4b34-3281-08dd05723b3c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014; X-Microsoft-Antispam-Message-Info: u2wHipCiP+4lY6bqNN99FcZaSPHSR+9u+cPZW3Nz85oSVOpDsLNxvJDnuk75tRmj+EWz/HaWtHElzZLMa6cGz9xGDrIUm09uCh+wUvQ868pfd2LTm1Z0JRaC2xBakBc4Oz37so7kKnC4pG+6zHrZXsdXCeYv6B39yavxJlncCXT2m77C5hOJA18JvYlm3npG6RP3Mi+ywtealp5ynyBErodW54SAMii0IIc31zdAyhzymX/c21VI2JDA7YqraRd0bhwE8MUKvat5UC3G811wEuM6m0zpjjdbGfCYss45ZCpmy9QnMybwvijv748j7QbUvHf3cnTlRfWgzmp1WBxj1cYVUypDod8X1jO5axDfABim0SMyAxjtMxE7tXH3jyLDiWR+dTTrIUKyoZNirpME4qcBIe10idm9hf3GVw0HZWdsdR0u38ZbpV0v4Af76XnlYpN5OPJmPmw83zTtUTB2apsIq5HKKAuR1r9N97ivksaA3GFlKxptsg5afdmW6pPOHCXpJDBTKk75q8O0eGm/hht78pyvScPq+PeRrKmHOeMA1Cp5uoI1WpwWw9Zll9SyCa9rtJwCPHcL7aYRDAxBrtbceSIodGEVg1fmx3RsEs+nZKW+JXujYEM/oZqc8vsaWs3xt9XQeg7UKbzj7i4K8y1rZ2wjwpzcV/HM4vMykKSTGKc05p1seJXgVY+6xBV5vUd5sYoA9W9M8ItsdVudU1+l+/yETbxEaczax6T/dYobqaNnPh/K7s2UgcsZ/vUxr8Zvb2VO9Io+xWaONcJmyYHZxWhDzHT/J4FJ/qDxM7mcIFzdSNQfx1CRp7o79pBJCiuFlGjzsSypWFXwE+poe03qDKJteWYJAcw1KfqRt0ZEZpp5cvTZJzERTq+8VVgORYz4tPh9VQ3TbS/fZzkeZ4Az/CvUU02Wuh+BLiDstZZUkpHxHus72lKhjPD7nFWGX0tDM8D97MBMjbTHImxOrnhO/uYu5NT/yrgwOGO427BQgfdcwS+gGaaGGkdgMDA8LLXiAd64pFmONf4dtZjbsMlva6wzct2Zme+f2dORZWUmRSwrNqG11kZ0TWNNyrQB+ODGMh4jxYJtzkNHHNCoyORUcBFKZBQFFcLAYLyepQi1tORmYyh6I+b84kGMumCLho4tnoTPhMtAtLOdT8EQkk3uYSFwL6dQ6C0mMRDSkb531+HJkyR0K9ajbgyYJC4atC5ZkGvfWxe2fB+tq3c7+QvdsG16PhWEnzyr5ppkvTjCN0dUjXgAJEyhCvV6XYY3x1YWzKe2yGo4pTQzarRdHhNPRFg6Zyh3E3IrKl8Ob8apnyWlM6xbm8YCnYfApAiz X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR10MB3366.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oVhyVI+gmDkwdbfqCZXRwIWdMhjJurDfT5glQSJLHdACZ/xaV35Zr6WoVgddZovXock5SeztTzQhxMxhW3KTXnGoplVweFNq2zfzEc/nzcRNIhv0l82KDd21FvzqpudblF8rQvhZoTtkuSxd8IS5qj+TpFxHPQiopiuh5XqmCR16ZTpx74QIlBA84BKdW2ag5DB5FN2r8k2D4agCcrzEXkcHoWFUWWF6RW7Dw9OZfKmXRheiREzIfwSgvJUn2oN3iKPslQoedkwwnqL960SFTwmN5jB4cduzqaSL5WHuELU653bgYsmTY2GQzzj9hlIgUB8jik1PntgcP7PaF7SrJqz6S0KPSPTanNr9+Xb3u4ihW6N0TOLGCu+TWyxJjUM25PultvU1DUm19VZqZ+uBb21Upel8P2+VuJU4OcdMA2CS6vhsRO5ctU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8BMwmX4r8XfpbGAYPY1+D23hFRTOu4NSoIQ6bUILo7/rNwn80yYd/mpmTuhNamkLQfa5IF3PMRWaE4i5dm2CLuX1Yp7Mie/LESQHNYZeA== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 3znoPqeMNJKbVpFw+7nxQkAZX33HE4rzsGwPK4bqG/o/75meRWALU62ulbytqJwDxK2hLGBltOAyhJ0nKnh/9M2CZBYKhClVj97W/TQcUbe9qIKYo4IYuSTljBdJ8K3i+9qpbH+v60pYg4nKebxw8AIGrPriF/EmJdxIp6Zlgw1uuon3JYCEtCrbwYMmyBi6Ueb+ukeTNBBKtjwr6eCTAXyYot1KIbeuC3lIZfLDjCmbbWlFuJFhYNydz0c2je1w4fbcVLhogzDVAbDe9MH3xovGne7tCeacbM+sFOyV60BqE7hWdXIaew4frxEqHcex0BSGeiOYjleDogmjgdmrVWrpSyKDENhOCJ8tNYerWIMKMH+6faflLW22SbElOyCm5f7zSQ87286d7UZX6aV+jviQtdwWcB2Z/17VdjWeaNFBsXqXZUR9VkjMd1Co0OeaEVqYBwyNS3Ci0CB7+8Cfrr8v7z+y68nWeWio6/0UO3V8U5NHbse4OOmtLLwKhOIbxmJocc+frkXm/KqVveXEVUZ+CivONQN23hVYLDP5tfUDY2NF6zjTJhwzMzxbtmK3jQLnK+rLfdlLJ0NH1WwrlEFiu+40WQnBEwJ5ih8BjHA= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1d98819e-3e35-4b34-3281-08dd05723b3c X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Nov 2024 12:37:13.7093 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DY3bTqAhRD5JTDOpBSLLyc5X9uFi/1nW9+K04S4DhAOgnBjqLXsM2CVLP/iYJa+ZyZQzsNy2ie+d6BD7oonhTSEx42TFBuFA2AL1C1Mm1vg= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA6PR10MB8136 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-14_05,2024-11-14_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=945 malwarescore=0 adultscore=0 phishscore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411150108 X-Proofpoint-ORIG-GUID: Vc0Y4RYwVnXxdBo-ske0bG6ydEnz75Oa X-Proofpoint-GUID: Vc0Y4RYwVnXxdBo-ske0bG6ydEnz75Oa X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 2C034A0005 X-Stat-Signature: pnan4kxscp5ttrkzxddr1txu566j9jwu X-Rspam-User: X-HE-Tag: 1731674219-207953 X-HE-Meta: U2FsdGVkX18FwbEaphaq4r015YPEkyOp3owNX/wvQC5QCjpDI/88eX4VNHcdjAeIoSVhVg0QlTspqa0IwtKZCMhQeDWaw/ToBh4+0cz/DzhD2JzrmKHiiEWH5hL7oAgWtGx4x97lgWuL97RFttwGje3Baf7IC4YAqNTqNPk48Z7klA/NNePmxmXbRZb4v4o+urvldpzlK7efwSfPaC1wPeLBhJjB41KbyEatDFfcnbqevlMCTU9tUcfVzkypGbwJpAzr2BYxHkdcFx5UbBzJ136UlzIm6HBo4DLIymg1RhAfnYQ8HgG+sGhEHq8dGouinvjVcsETVcifg5k7+eOtBqv4BC7cKZicJigESAGd5pPX7mL8z3e99U1lgXOvu4YBxgkB4hPt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nMT1qo7JWKL5MWxFKZVKcEI3ZvZGXX1fwSToYbtlV8hMBoLvF9v6ivPVAa7AGLSEUOXlAHox4WAVtSkNIHM+z5uPEGz3q9An8ufF12pbFaSQNkk7veCHOH2Ebwu9/EqOgaG5iEl1n1BG/ZefmhSW9qADAcgjrLjvgvU/F3HfIl7s5o9I7L4aUcntt+2zTAkUFXtgIORB6NeouT4zKFZlX82iyO2024bXdjylrGU5FNfx+rEcPh3lERi8jJzJBocDjjL90BqgdmHPGKBC/WmdnOGmEH/FZuyAykoI0GquUuB5wnL8Tygj7OfztmwATZYK9LBwz5ZAzSGerp0+fUzt/GUU7E3uEsoDXYnG7p7EW5S0IncEy2alxlOUaCunk/stez0bB5flfINZ+H+ieK/Cd+aeDFMU3Z+d2DRcgjxj2RgnX9APtu/YZJI+INbcoOL/ERyXJIEfX67aneBOb6nHwlu9MTtbyINkK+Gy14rDP1JQ3BSarTWMonu9Vpaj1EjX0Z1 3BPzubjx 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 | 69 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index c30ebe82ebdb..9f76625a1743 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1726,7 +1726,7 @@ static inline int accountable_mapping(struct file *file, vm_flags_t vm_flags) return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; } -unsigned long mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { @@ -1795,11 +1795,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr, if (error) goto free_vma; } - if (vm_flags & VM_SHARED) { - error = mapping_map_writable(file->f_mapping); - if (error) - goto allow_write_and_free_vma; - } /* ->mmap() can change vma->vm_file, but must guarantee that * vma_link() below can deny write-access if VM_DENYWRITE is set @@ -1809,7 +1804,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma->vm_file = get_file(file); error = mmap_file(file, vma); if (error) - goto unmap_and_free_vma; + goto unmap_and_free_file_vma; /* Can addr have changed?? * @@ -1820,6 +1815,14 @@ unsigned long mmap_region(struct file *file, unsigned long addr, */ WARN_ON_ONCE(addr != vma->vm_start); + /* + * Drivers should not permit writability when previously it was + * disallowed. + */ + VM_WARN_ON_ONCE(vm_flags != vma->vm_flags && + !(vm_flags & VM_MAYWRITE) && + (vma->vm_flags & VM_MAYWRITE)); + addr = vma->vm_start; /* If vm_flags changed after mmap_file(), we should try merge vma again @@ -1851,21 +1854,14 @@ unsigned long mmap_region(struct file *file, unsigned long addr, vma_set_anonymous(vma); } - /* Allow architectures to sanity-check the vm_flags */ - if (!arch_validate_flags(vma->vm_flags)) { - error = -EINVAL; - if (file) - goto close_and_free_vma; - else - goto free_vma; - } +#ifdef CONFIG_SPARC64 + /* TODO: Fix SPARC ADI! */ + WARN_ON_ONCE(!arch_validate_flags(vm_flags)); +#endif vma_link(mm, vma, prev, rb_link, rb_parent); - /* Once vma denies write, undo our temporary denial count */ if (file) { unmap_writable: - if (vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); if (vm_flags & VM_DENYWRITE) allow_write_access(file); } @@ -1899,17 +1895,12 @@ unsigned long mmap_region(struct file *file, unsigned long addr, return addr; -close_and_free_vma: - vma_close(vma); -unmap_and_free_vma: +unmap_and_free_file_vma: vma->vm_file = NULL; fput(file); /* Undo any partial mapping done by a device driver. */ unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); - if (vm_flags & VM_SHARED) - mapping_unmap_writable(file->f_mapping); -allow_write_and_free_vma: if (vm_flags & VM_DENYWRITE) allow_write_access(file); free_vma: @@ -2931,6 +2922,36 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, return __do_munmap(mm, start, len, uf, false); } +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; + + /* 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 downgrade) { int ret;