From mboxrd@z Thu Jan 1 00:00:00 1970 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 7C0CDC433EF for ; Mon, 4 Apr 2022 15:02:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0AA8D8D0032; Mon, 4 Apr 2022 10:37:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0584A8D0002; Mon, 4 Apr 2022 10:37:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CB6D58D0032; Mon, 4 Apr 2022 10:37:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0135.hostedemail.com [216.40.44.135]) by kanga.kvack.org (Postfix) with ESMTP id B53288D0002 for ; Mon, 4 Apr 2022 10:37:26 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 75741183DBF12 for ; Mon, 4 Apr 2022 14:37:16 +0000 (UTC) X-FDA: 79319449272.23.AD3108A Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf08.hostedemail.com (Postfix) with ESMTP id B470F160024 for ; Mon, 4 Apr 2022 14:37:15 +0000 (UTC) Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 234DquEh012558; Mon, 4 Apr 2022 14:37:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=corp-2021-07-09; bh=5fkUl/JAA5bSnTZypgG88Cm57/9cAJAoKp+0d4HCV2g=; b=ac4zX5DjZsaOj/3MbuzRYlLoGJBwx8R8SkvqtvcDdwtilcWkm/WHGzerdOVnqcT8w4xi NCoLc7zdG3O/okWgybeenPR1wt26CKXlHtng2K49hYmVlrn02WxiOLPlD4p+32G8DBBQ hhKlIrcthCSQYP/2wCzGck6PuN9OAix35ZZwxIm+Znk63lpOBUf0BM7KDIkAmhM3Zqyb 2nB0kXVtJlX5oXpobiNRlR7E2Mb0uE2r9qowniYytxpA7f0+EkG5JjnmlkX/E4c/G9kh yu8/BXtjQ8UbU7xDfnMpy+NFzPETXgWJBe0vvqIfVKnKjJAR4IKz9TkK582VcgoEH3XF 3A== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com with ESMTP id 3f6cwcbbsj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 Apr 2022 14:37:14 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 234Eajoe011889; Mon, 4 Apr 2022 14:37:13 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2177.outbound.protection.outlook.com [104.47.56.177]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3f6cx2gw9p-10 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 04 Apr 2022 14:37:12 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iKTAvYwG8cDDCyAYItY4lLUK+QD0Ibnb4gEdLWTymD+okk1HtAnLug6pzMsGyDQYwiXMLORXSHUqgubkVNDbmhCWMz9cW3rgAYTfeSry3ktT9EHuzczGK9RDPx0xGqgz36sNDttZ9WKJOyDurjbDB07UxoqlU64XnFGvOJyF/JYC93MZZX9MPut8cYQlU2nqcd2lx+qauoABp53ML/5TKMCmq7FGrSWS4ds4QBLmLIfqmgkC/QidCa0JQJ1JilGBV4d1ujyZzsq951f2tuArKOPH28h+ismhOHJYiJYJBZi9HbxhIPMpKfQtJGzXI8GfzjeGz08unXdPKUxoRjMTpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; 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=5fkUl/JAA5bSnTZypgG88Cm57/9cAJAoKp+0d4HCV2g=; b=jHptoNatUfYcIK1WfRQUyn9/GSowJWGTQWuDF8+5rc/Mq6Gq8r1k4I8G/dNqj0PU2Kc+D0ruSREi9EmJbeZmyWzGpyjjbRFGTQ/6icUYEfmKNR/GGX9VPrwruBET0n+9QhyobFq3odcVG8XZ4ylvRfIs6UER/uPAY+psCuF/FaIvBwm7r4D06cGgLDjRYq+zQfiC0r05EbYRcXA9wJRrYYgGPwBKU3yQUikd45uEYre/upi5+r7EZPxanTOA5zhzc82aqFr/dcZtzrw2uykbiokjsZ0AXfw+ZhFAJnq/NZGRn0ivPKNqqqP2o/rd+BIS0TNh5ZzM+fCBU3EhqbMkEg== 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=5fkUl/JAA5bSnTZypgG88Cm57/9cAJAoKp+0d4HCV2g=; b=Mi1jg92xAkZJE4dpqttq3UyEgzqbZhbtojg1ZRcBHD5+AjpLG6fi+fwgTo46A8rGY0+/FwLfhj0CHyXS579pQNvshnjT68J2UDYF1BL5UiWwvkVrbOJ9CfWA8LGE0OLzVclIRtm1W1fDUAXqWDUJb2NVAbKewRSmMJ+lg3jv6Ro= Received: from SN6PR10MB3022.namprd10.prod.outlook.com (2603:10b6:805:d8::25) by SJ0PR10MB5742.namprd10.prod.outlook.com (2603:10b6:a03:3ed::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5123.31; Mon, 4 Apr 2022 14:37:08 +0000 Received: from SN6PR10MB3022.namprd10.prod.outlook.com ([fe80::74fd:df41:4c93:24a5]) by SN6PR10MB3022.namprd10.prod.outlook.com ([fe80::74fd:df41:4c93:24a5%6]) with mapi id 15.20.5123.031; Mon, 4 Apr 2022 14:37:08 +0000 From: Liam Howlett To: "maple-tree@lists.infradead.org" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , Andrew Morton Subject: [PATCH v7 68/70] mm: Remove the vma linked list Thread-Topic: [PATCH v7 68/70] mm: Remove the vma linked list Thread-Index: AQHYSDFNB/r/0O35IU+1hyG2trqn+A== Date: Mon, 4 Apr 2022 14:35:59 +0000 Message-ID: <20220404143501.2016403-69-Liam.Howlett@oracle.com> References: <20220404143501.2016403-1-Liam.Howlett@oracle.com> In-Reply-To: <20220404143501.2016403-1-Liam.Howlett@oracle.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.34.1 x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 440c718f-7461-4a5a-3c12-08da164898bf x-ms-traffictypediagnostic: SJ0PR10MB5742:EE_ x-microsoft-antispam-prvs: x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: fDlIXDVwSZjrB6NKs45DC/cwVwwdBSkWcpxGEDlSm39mI+/CGpNdDsYb5A310m4RKpmy/4ZXFJ3YhvbN2ZwUupL7OKeiaqpi3PsR4CSiDK6QpQ1T0KtSi39HuSQmnYrAoh/8VCq/fBaZ35w1+jEoSkrJW8v6qIQQSI8fuPGckHHhWJdzSLI4vlL+AP32qIC8n4iaO3yAmDGlk6Me+OYfZgQ2n58IFXk1ZFLipXxdgscTlMU89IvLKw3KKlUvAtWa2IstdWW8ZcgvkD/3Xa3UgbxkypQoUNktYYL8HGZZFf35Mi0ghC2VHmV7ezHzuc3+Z2OCzN8Oo3ytUQwOLW5CM0xuBzStutmz+i0PLOMW22X7NJCi4lOx+r9+3xpchwGC41D92W7dR/3r6MCoLRFfsi3FDc1QC3gG9uEHhY6Yh14lyK4DGOtKSrWQ12wK7cCNRr6KpvNsSAq+Dqh7saDvn63uq9ks77MwVNgzcCwSGdHbf6Sfdj1QluxHA6BoF6Ta8flvx3fo417jWCfA+c//b/a7c1dOKVEN5tl1bPZMyi8i7RyhRUQdk2lndY8EbJbG58hZw89ZITRlocD/keEQbBrhUdAVkQ8FFVl1dwrgWYGAxhvqT2vxWppmGY7P9pWO+4J4OMATCPrIP54KjwVAjjZTOg5kvUB9z2kwAgrzl5dlaVrMpqiqLoBCsPs1MdrQuvgmQiX7foZ+EJ1Mip38bw== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR10MB3022.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(30864003)(26005)(122000001)(2906002)(44832011)(71200400001)(83380400001)(5660300002)(64756008)(66476007)(186003)(38070700005)(8936002)(316002)(1076003)(38100700002)(36756003)(2616005)(110136005)(66556008)(66446008)(86362001)(6506007)(91956017)(6512007)(508600001)(6486002)(8676002)(66946007)(76116006)(6666004)(579004)(559001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-8859-1?Q?HSbFT+TA8ot/0wyYs2aUWFhbFLmpWI0/TlvrWgNTMagn4Ez3zsYyvUw08+?= =?iso-8859-1?Q?cmpBz3plFxkSXpmUVqusp65PnzQpIuzADU77p85SqvrIF/1EeD8wu2DmdI?= =?iso-8859-1?Q?zyPqeumQ4AtQn1pwfFiu9UgrQJFbEekA8qsouJ4MiyLVfQHrUGr9WVAZx8?= =?iso-8859-1?Q?Bfvh3XYauLxrSHKeza8N7YQzc0X4x/xzA0s0GDWMcMgqq2Ird9ZkqmLYdB?= =?iso-8859-1?Q?NpXAQ8jlyDWlwjYWxM/44frOEhNWERE7Tne4U08XVaphSCV+gwxqhgfjfX?= =?iso-8859-1?Q?0OdR9dWOUu6N2Z+q6YlbF4RzyhN9Vx6pWFuC4PFuTMN4pduNsOXo1zsE/U?= =?iso-8859-1?Q?WoJdIOTCTXqO3wTsk5gwZaVKa/+6SCWngwE/jgFhrm/Hx5CyyGb6NujS8W?= =?iso-8859-1?Q?F5QzefOxrU9bzWmbJUDML7CDQvtPl5YlybSnTTlMjvLQ4bg9EG6+NrY3q8?= =?iso-8859-1?Q?Tba2scbxVILLqrbh53YMzKfcKO5tPqpdsm9e7SCv/r1f4tuY7bOVFlSbhn?= =?iso-8859-1?Q?j/t590wc2x14aKKnxeAQIB2y9Xhb4+Ust6pbiH+nymLvXJzhxDnzDPU12k?= =?iso-8859-1?Q?+qvU6rHzbm7L/QkLGhfBjeuSgwEjjjUZ61opN2KD4TB+YrBbAH8gIJmWRf?= =?iso-8859-1?Q?nZmYzhbt3bdyJ8gc4GOlBfr5OhZa2LtgrQy3FBnB+RK17ugCz3ZI9L8szF?= =?iso-8859-1?Q?abW3POr7T5Uz/xRhBxQbEOQUK1fsjtKtFIeCNRoHXSHOIhQOUQCm8OIYYH?= =?iso-8859-1?Q?Xz6wAjUbCW7hQr+eQGjS92LKk/raMA1JcLfNQ9B1MJ6VVWOjU8JekK2x/w?= =?iso-8859-1?Q?USvmaO8MKXYqbzFK1avhKDCikRzBdiyJcsW5p7BHM70cIJBis0Y7hDuEE+?= =?iso-8859-1?Q?l1XNW4wwAej62V26j61yP8Rj07APhv+MI9X1IvhcRWYUXbWZKiT8XCcrQR?= =?iso-8859-1?Q?DWEiOJaokOCUo4bsQNnDqU0OIzSGKs0H7Z4bG8CQkcHevfn+Z4cA2rcp0h?= =?iso-8859-1?Q?h2sM1ZpR0Nin+T6rgPXBbtrFfv34Q2DsTK/qMNvwXc62MtNet30y3FJCLV?= =?iso-8859-1?Q?wJTFQLWZBAhi88gkbNSki4fOoMQZbUnLpt7tSD+xtWyNPLMJp3dPsjhPcQ?= =?iso-8859-1?Q?37RAAyY/3JAdSLb6H9d6aXPnI8LvPUtSVv53NJf9BJRoE1VQrqHe67abiH?= =?iso-8859-1?Q?W3dWJnWeaux9l3trkYysDQTnO5ShM2LverVynTDCOKVhGOHv6dzoTataDa?= =?iso-8859-1?Q?9FPqtmHNrAnseCsNtRG9yed9S8YaDYKivltcNPAlBkTLE3BE684haz55bI?= =?iso-8859-1?Q?+UBU2FgObxsKyCny+O3zpOdNZpiFnZcQIhad/fsXYUwtvB1CD50jB/9aIw?= =?iso-8859-1?Q?LDXxDglCgOv7DCPl944n88E+bCzdMlqMc3FF4LJdOvBkIsdeiMZtmlJRV6?= =?iso-8859-1?Q?DNDQ79K4PxRL3MSIav6PJypUFqfEusLzLtDr7pAnTsvZvf9tcFAJn8nwKC?= =?iso-8859-1?Q?fmXj83C6iAeJQ9zhK/zGWubF/5h4Xe7ZHtxgHnl7tlqBu8kYBP7vYCMBpw?= =?iso-8859-1?Q?NJWB//7WzoS/2UKhU0naW36x8DDsDz7MuMEbTrf7Lujl1a+ROvFaT+70Rf?= =?iso-8859-1?Q?Iv/iSbHJDwF8LgnD9JqT/GdOSIYZdCzJ7kpelqxtdDjhghraDQtxvCoArL?= =?iso-8859-1?Q?u7upZyqDorPhwP7T8ynCYTzdMUn6UhA6vyoL7H6mWgcLl2J7Bz9AmEyKzU?= =?iso-8859-1?Q?KjgGmssl/qA0f8OmQYdvBomZsJ7PCKkQbD+Hof7vxfxGSvEl6QnuK3LhhT?= =?iso-8859-1?Q?NrenERvuWuqRcsFR4VhzjHdepIPaBLI=3D?= Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SN6PR10MB3022.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 440c718f-7461-4a5a-3c12-08da164898bf X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Apr 2022 14:35:59.1269 (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: bKu7426jH+29ZJMjSRhda3tlUV3zOBH0Wk42EEVozdwQwF2ykAzOz992WjSzj6rYNdw6FwzC+ukWnr5uW3HiJQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB5742 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.425,18.0.850 definitions=2022-04-04_06:2022-03-30,2022-04-04 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 malwarescore=0 spamscore=0 phishscore=0 mlxscore=0 mlxlogscore=999 bulkscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204040083 X-Proofpoint-ORIG-GUID: 6xZjZN06zIhEyyEJLfIb8xWnnXQCOuwx X-Proofpoint-GUID: 6xZjZN06zIhEyyEJLfIb8xWnnXQCOuwx Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=ac4zX5Dj; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=Mi1jg92x; spf=none (imf08.hostedemail.com: domain of liam.howlett@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=liam.howlett@oracle.com; dmarc=pass (policy=none) header.from=oracle.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: B470F160024 X-Stat-Signature: hd9romrk4sqiudyombkhkwnxemwt3iut X-HE-Tag: 1649083035-68673 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: From: "Liam R. Howlett" Replace any vm_next use with vma_find(). Update free_pgtables(), unmap_vmas(), and zap_page_range() to use the maple tree. Use the new free_pgtables() and unmap_vmas() in do_mas_align_munmap(). At the same time, alter the loop to be more compact. Now that free_pgtables() and unmap_vmas() take a maple tree as an argument, rearrange do_mas_align_munmap() to use the new tree to hold the vmas to remove. Remove __vma_link_list() and __vma_unlink_list() as they are exclusively used to update the linked list Drop linked list update from __insert_vm_struct(). Rework validation of tree as it was depending on the linked list. Signed-off-by: Liam R. Howlett --- include/linux/mm.h | 5 +- include/linux/mm_types.h | 4 - kernel/fork.c | 15 +- mm/debug.c | 14 +- mm/internal.h | 8 +- mm/memory.c | 33 ++- mm/mmap.c | 438 +++++++++++++++++---------------------- mm/nommu.c | 5 - mm/util.c | 40 ---- 9 files changed, 230 insertions(+), 332 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 34226adf6b51..4c9f67210812 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1867,8 +1867,9 @@ void zap_vma_ptes(struct vm_area_struct *vma, unsigne= d long address, unsigned long size); void zap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size); -void unmap_vmas(struct mmu_gather *tlb, struct vm_area_struct *start_vma, - unsigned long start, unsigned long end); +void unmap_vmas(struct mmu_gather *tlb, struct maple_tree *mt, + struct vm_area_struct *start_vma, unsigned long start, + unsigned long end); =20 struct mmu_notifier_range; =20 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 43bc3d6ac074..11dc4586464d 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -398,8 +398,6 @@ struct vm_area_struct { unsigned long vm_end; /* The first byte after our end address within vm_mm. */ =20 - /* linked list of VM areas per task, sorted by address */ - struct vm_area_struct *vm_next, *vm_prev; struct mm_struct *vm_mm; /* The address space we belong to. */ =20 /* @@ -463,7 +461,6 @@ struct vm_area_struct { struct kioctx_table; struct mm_struct { struct { - struct vm_area_struct *mmap; /* list of VMAs */ struct maple_tree mm_mt; #ifdef CONFIG_MMU unsigned long (*get_unmapped_area) (struct file *filp, @@ -478,7 +475,6 @@ struct mm_struct { unsigned long mmap_compat_legacy_base; #endif unsigned long task_size; /* size of task vm space */ - unsigned long highest_vm_end; /* highest vma end address */ pgd_t * pgd; =20 #ifdef CONFIG_MEMBARRIER diff --git a/kernel/fork.c b/kernel/fork.c index fa0ec0783784..0b032f02c658 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -474,7 +474,6 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struc= t *orig) */ *new =3D data_race(*orig); INIT_LIST_HEAD(&new->anon_vma_chain); - new->vm_next =3D new->vm_prev =3D NULL; dup_anon_vma_name(orig, new); } return new; @@ -579,7 +578,7 @@ static void dup_mm_exe_file(struct mm_struct *mm, struc= t mm_struct *oldmm) static __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) { - struct vm_area_struct *mpnt, *tmp, *prev, **pprev; + struct vm_area_struct *mpnt, *tmp; int retval; unsigned long charge =3D 0; MA_STATE(old_mas, &oldmm->mm_mt, 0, 0); @@ -606,7 +605,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *= mm, mm->exec_vm =3D oldmm->exec_vm; mm->stack_vm =3D oldmm->stack_vm; =20 - pprev =3D &mm->mmap; retval =3D ksm_fork(mm, oldmm); if (retval) goto out; @@ -614,8 +612,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *= mm, if (retval) goto out; =20 - prev =3D NULL; - retval =3D mas_expected_entries(&mas, oldmm->map_count); if (retval) goto out; @@ -687,14 +683,6 @@ static __latent_entropy int dup_mmap(struct mm_struct = *mm, if (is_vm_hugetlb_page(tmp)) reset_vma_resv_huge_pages(tmp); =20 - /* - * Link in the new vma and copy the page table entries. - */ - *pprev =3D tmp; - pprev =3D &tmp->vm_next; - tmp->vm_prev =3D prev; - prev =3D tmp; - /* Link the vma into the MT */ mas.index =3D tmp->vm_start; mas.last =3D tmp->vm_end - 1; @@ -1112,7 +1100,6 @@ static void mm_init_uprobes_state(struct mm_struct *m= m) static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct = *p, struct user_namespace *user_ns) { - mm->mmap =3D NULL; mt_init_flags(&mm->mm_mt, MM_MT_FLAGS); mt_set_external_lock(&mm->mm_mt, &mm->mmap_lock); atomic_set(&mm->mm_users, 1); diff --git a/mm/debug.c b/mm/debug.c index 2d625ca0e326..0fd15ba70d16 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -139,13 +139,11 @@ EXPORT_SYMBOL(dump_page); =20 void dump_vma(const struct vm_area_struct *vma) { - pr_emerg("vma %px start %px end %px\n" - "next %px prev %px mm %px\n" + pr_emerg("vma %px start %px end %px mm %px\n" "prot %lx anon_vma %px vm_ops %px\n" "pgoff %lx file %px private_data %px\n" "flags: %#lx(%pGv)\n", - vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_next, - vma->vm_prev, vma->vm_mm, + vma, (void *)vma->vm_start, (void *)vma->vm_end, vma->vm_mm, (unsigned long)pgprot_val(vma->vm_page_prot), vma->anon_vma, vma->vm_ops, vma->vm_pgoff, vma->vm_file, vma->vm_private_data, @@ -155,11 +153,11 @@ EXPORT_SYMBOL(dump_vma); =20 void dump_mm(const struct mm_struct *mm) { - pr_emerg("mm %px mmap %px task_size %lu\n" + pr_emerg("mm %px task_size %lu\n" #ifdef CONFIG_MMU "get_unmapped_area %px\n" #endif - "mmap_base %lu mmap_legacy_base %lu highest_vm_end %lu\n" + "mmap_base %lu mmap_legacy_base %lu\n" "pgd %px mm_users %d mm_count %d pgtables_bytes %lu map_count %d\n" "hiwater_rss %lx hiwater_vm %lx total_vm %lx locked_vm %lx\n" "pinned_vm %llx data_vm %lx exec_vm %lx stack_vm %lx\n" @@ -183,11 +181,11 @@ void dump_mm(const struct mm_struct *mm) "tlb_flush_pending %d\n" "def_flags: %#lx(%pGv)\n", =20 - mm, mm->mmap, mm->task_size, + mm, mm->task_size, #ifdef CONFIG_MMU mm->get_unmapped_area, #endif - mm->mmap_base, mm->mmap_legacy_base, mm->highest_vm_end, + mm->mmap_base, mm->mmap_legacy_base, mm->pgd, atomic_read(&mm->mm_users), atomic_read(&mm->mm_count), mm_pgtables_bytes(mm), diff --git a/mm/internal.h b/mm/internal.h index 8221f290d047..b43e65c1c8dc 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -69,8 +69,9 @@ void folio_rotate_reclaimable(struct folio *folio); bool __folio_end_writeback(struct folio *folio); void deactivate_file_folio(struct folio *folio); =20 -void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *start_vm= a, - unsigned long floor, unsigned long ceiling); +void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt, + struct vm_area_struct *start_vma, unsigned long floor, + unsigned long ceiling); void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte); =20 struct zap_details; @@ -465,9 +466,6 @@ static inline int vma_mas_remove(struct vm_area_struct = *vma, struct ma_state *ma } =20 /* mm/util.c */ -void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, - struct vm_area_struct *prev); -void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma); struct anon_vma *folio_anon_vma(struct folio *folio); =20 #ifdef CONFIG_MMU diff --git a/mm/memory.c b/mm/memory.c index 76e3af9639d9..cf240d139b0d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -399,12 +399,21 @@ void free_pgd_range(struct mmu_gather *tlb, } while (pgd++, addr =3D next, addr !=3D end); } =20 -void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma, - unsigned long floor, unsigned long ceiling) +void free_pgtables(struct mmu_gather *tlb, struct maple_tree *mt, + struct vm_area_struct *vma, unsigned long floor, + unsigned long ceiling) { - while (vma) { - struct vm_area_struct *next =3D vma->vm_next; + MA_STATE(mas, mt, vma->vm_end, vma->vm_end); + + do { unsigned long addr =3D vma->vm_start; + struct vm_area_struct *next; + + /* + * Note: USER_PGTABLES_CEILING may be passed as ceiling and may + * be 0. This will underflow and is okay. + */ + next =3D mas_find(&mas, ceiling - 1); =20 /* * Hide vma from rmap and truncate_pagecache before freeing @@ -423,7 +432,7 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_ar= ea_struct *vma, while (next && next->vm_start <=3D vma->vm_end + PMD_SIZE && !is_vm_hugetlb_page(next)) { vma =3D next; - next =3D vma->vm_next; + next =3D mas_find(&mas, ceiling - 1); unlink_anon_vmas(vma); unlink_file_vma(vma); } @@ -431,7 +440,7 @@ void free_pgtables(struct mmu_gather *tlb, struct vm_ar= ea_struct *vma, floor, next ? next->vm_start : ceiling); } vma =3D next; - } + } while (vma); } =20 void pmd_install(struct mm_struct *mm, pmd_t *pmd, pgtable_t *pte) @@ -1632,17 +1641,19 @@ static void unmap_single_vma(struct mmu_gather *tlb= , * ensure that any thus-far unmapped pages are flushed before unmap_vmas() * drops the lock and schedules. */ -void unmap_vmas(struct mmu_gather *tlb, +void unmap_vmas(struct mmu_gather *tlb, struct maple_tree *mt, struct vm_area_struct *vma, unsigned long start_addr, unsigned long end_addr) { struct mmu_notifier_range range; + MA_STATE(mas, mt, vma->vm_end, vma->vm_end); =20 mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm, start_addr, end_addr); mmu_notifier_invalidate_range_start(&range); - for ( ; vma && vma->vm_start < end_addr; vma =3D vma->vm_next) + do { unmap_single_vma(tlb, vma, start_addr, end_addr, NULL); + } while ((vma =3D mas_find(&mas, end_addr - 1)) !=3D NULL); mmu_notifier_invalidate_range_end(&range); } =20 @@ -1657,8 +1668,11 @@ void unmap_vmas(struct mmu_gather *tlb, void zap_page_range(struct vm_area_struct *vma, unsigned long start, unsigned long size) { + struct maple_tree *mt =3D &vma->vm_mm->mm_mt; + unsigned long end =3D start + size; struct mmu_notifier_range range; struct mmu_gather tlb; + MA_STATE(mas, mt, vma->vm_end, vma->vm_end); =20 lru_add_drain(); mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, @@ -1666,8 +1680,9 @@ void zap_page_range(struct vm_area_struct *vma, unsig= ned long start, tlb_gather_mmu(&tlb, vma->vm_mm); update_hiwater_rss(vma->vm_mm); mmu_notifier_invalidate_range_start(&range); - for ( ; vma && vma->vm_start < range.end; vma =3D vma->vm_next) + do { unmap_single_vma(&tlb, vma, start, range.end, NULL); + } while ((vma =3D mas_find(&mas, end - 1)) !=3D NULL); mmu_notifier_invalidate_range_end(&range); tlb_finish_mmu(&tlb); } diff --git a/mm/mmap.c b/mm/mmap.c index d648b551893e..1b2916a38196 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -75,9 +75,10 @@ int mmap_rnd_compat_bits __read_mostly =3D CONFIG_ARCH_M= MAP_RND_COMPAT_BITS; static bool ignore_rlimit_data; core_param(ignore_rlimit_data, ignore_rlimit_data, bool, 0644); =20 -static void unmap_region(struct mm_struct *mm, +static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, struct vm_area_struct *vma, struct vm_area_struct *prev, - unsigned long start, unsigned long end); + struct vm_area_struct *next, unsigned long start, + unsigned long end); =20 /* description of effects of mapping type and prot in current implementati= on. * this is due to the limited x86 page protection hardware. The expected @@ -172,12 +173,10 @@ void unlink_file_vma(struct vm_area_struct *vma) } =20 /* - * Close a vm structure and free it, returning the next. + * Close a vm structure and free it. */ -static struct vm_area_struct *remove_vma(struct vm_area_struct *vma) +static void remove_vma(struct vm_area_struct *vma) { - struct vm_area_struct *next =3D vma->vm_next; - might_sleep(); if (vma->vm_ops && vma->vm_ops->close) vma->vm_ops->close(vma); @@ -185,7 +184,6 @@ static struct vm_area_struct *remove_vma(struct vm_area= _struct *vma) fput(vma->vm_file); mpol_put(vma_policy(vma)); vm_area_free(vma); - return next; } =20 /* @@ -210,8 +208,7 @@ static int do_brk_munmap(struct ma_state *mas, struct v= m_area_struct *vma, unsigned long newbrk, unsigned long oldbrk, struct list_head *uf); static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *brkvm= a, - unsigned long addr, unsigned long request, - unsigned long flags); + unsigned long addr, unsigned long request, unsigned long flags); SYSCALL_DEFINE1(brk, unsigned long, brk) { unsigned long newbrk, oldbrk, origbrk; @@ -280,7 +277,6 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) * before calling do_brk_munmap(). */ mm->brk =3D brk; - mas.last =3D oldbrk - 1; ret =3D do_brk_munmap(&mas, brkvma, newbrk, oldbrk, &uf); if (ret =3D=3D 1) { downgraded =3D true; @@ -335,42 +331,20 @@ extern void mt_dump(const struct maple_tree *mt); static void validate_mm_mt(struct mm_struct *mm) { struct maple_tree *mt =3D &mm->mm_mt; - struct vm_area_struct *vma_mt, *vma =3D mm->mmap; + struct vm_area_struct *vma_mt; =20 MA_STATE(mas, mt, 0, 0); - mas_for_each(&mas, vma_mt, ULONG_MAX) { - if (xa_is_zero(vma_mt)) - continue; - - if (!vma) - break; =20 - if ((vma !=3D vma_mt) || - (vma->vm_start !=3D vma_mt->vm_start) || - (vma->vm_end !=3D vma_mt->vm_end) || - (vma->vm_start !=3D mas.index) || - (vma->vm_end - 1 !=3D mas.last)) { + mas_for_each(&mas, vma_mt, ULONG_MAX) { + if ((vma_mt->vm_start !=3D mas.index) || + (vma_mt->vm_end - 1 !=3D mas.last)) { pr_emerg("issue in %s\n", current->comm); dump_stack(); dump_vma(vma_mt); - pr_emerg("and vm_next\n"); - dump_vma(vma->vm_next); pr_emerg("mt piv: %px %lu - %lu\n", vma_mt, mas.index, mas.last); pr_emerg("mt vma: %px %lu - %lu\n", vma_mt, vma_mt->vm_start, vma_mt->vm_end); - if (vma->vm_prev) { - pr_emerg("ll prev: %px %lu - %lu\n", - vma->vm_prev, vma->vm_prev->vm_start, - vma->vm_prev->vm_end); - } - pr_emerg("ll vma: %px %lu - %lu\n", vma, - vma->vm_start, vma->vm_end); - if (vma->vm_next) { - pr_emerg("ll next: %px %lu - %lu\n", - vma->vm_next, vma->vm_next->vm_start, - vma->vm_next->vm_end); - } =20 mt_dump(mas.tree); if (vma_mt->vm_end !=3D mas.last + 1) { @@ -387,11 +361,7 @@ static void validate_mm_mt(struct mm_struct *mm) } VM_BUG_ON_MM(vma_mt->vm_start !=3D mas.index, mm); } - VM_BUG_ON(vma !=3D vma_mt); - vma =3D vma->vm_next; - } - VM_BUG_ON(vma); mt_validate(&mm->mm_mt); } =20 @@ -399,12 +369,12 @@ static void validate_mm(struct mm_struct *mm) { int bug =3D 0; int i =3D 0; - unsigned long highest_address =3D 0; - struct vm_area_struct *vma =3D mm->mmap; + struct vm_area_struct *vma; + MA_STATE(mas, &mm->mm_mt, 0, 0); =20 validate_mm_mt(mm); =20 - while (vma) { + mas_for_each(&mas, vma, ULONG_MAX) { #ifdef CONFIG_DEBUG_VM_RB struct anon_vma *anon_vma =3D vma->anon_vma; struct anon_vma_chain *avc; @@ -416,18 +386,10 @@ static void validate_mm(struct mm_struct *mm) anon_vma_unlock_read(anon_vma); } #endif - - highest_address =3D vm_end_gap(vma); - vma =3D vma->vm_next; i++; } if (i !=3D mm->map_count) { - pr_emerg("map_count %d vm_next %d\n", mm->map_count, i); - bug =3D 1; - } - if (highest_address !=3D mm->highest_vm_end) { - pr_emerg("mm->highest_vm_end %lx, found %lx\n", - mm->highest_vm_end, highest_address); + pr_emerg("map_count %d mas_for_each %d\n", mm->map_count, i); bug =3D 1; } VM_BUG_ON_MM(bug, mm); @@ -487,29 +449,13 @@ bool range_has_overlap(struct mm_struct *mm, unsigned= long start, struct vm_area_struct *existing; =20 MA_STATE(mas, &mm->mm_mt, start, start); + rcu_read_lock(); existing =3D mas_find(&mas, end - 1); *pprev =3D mas_prev(&mas, 0); + rcu_read_unlock(); return existing ? true : false; } =20 -/* - * __vma_next() - Get the next VMA. - * @mm: The mm_struct. - * @vma: The current vma. - * - * If @vma is NULL, return the first vma in the mm. - * - * Returns: The next VMA after @vma. - */ -static inline struct vm_area_struct *__vma_next(struct mm_struct *mm, - struct vm_area_struct *vma) -{ - if (!vma) - return mm->mmap; - - return vma->vm_next; -} - static unsigned long count_vma_pages_range(struct mm_struct *mm, unsigned long addr, unsigned long end) { @@ -574,8 +520,7 @@ static void vma_store(struct mm_struct *mm, struct vm_a= rea_struct *vma) vma_mas_store(vma, &mas); } =20 -static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma, - struct vm_area_struct *prev) +static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma) { struct address_space *mapping =3D NULL; =20 @@ -585,7 +530,6 @@ static void vma_link(struct mm_struct *mm, struct vm_ar= ea_struct *vma, } =20 vma_store(mm, vma); - __vma_link_list(mm, vma, prev); __vma_link_file(vma); =20 if (mapping) @@ -599,15 +543,9 @@ static void vma_link(struct mm_struct *mm, struct vm_a= rea_struct *vma, * Helper for vma_adjust() in the split_vma insert case: insert a vma into= the * mm's list and the mm tree. It has already been inserted into the inter= val tree. */ -static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct= *vma, - unsigned long location) +static void __insert_vm_struct(struct mm_struct *mm, struct vm_area_struct= *vma) { - struct vm_area_struct *prev; - MA_STATE(mas, &mm->mm_mt, location, location); - - prev =3D mas_prev(&mas, 0); vma_store(mm, vma); - __vma_link_list(mm, vma, prev); mm->map_count++; } =20 @@ -682,15 +620,8 @@ inline int vma_expand(struct ma_state *mas, struct vm_= area_struct *vma, } =20 /* Expanding over the next vma */ - if (remove_next) { - /* Remove from mm linked list - also updates highest_vm_end */ - __vma_unlink_list(mm, next); - - if (file) - __remove_shared_vm_struct(next, file, mapping); - - } else if (!next) { - mm->highest_vm_end =3D vm_end_gap(vma); + if (remove_next && file) { + __remove_shared_vm_struct(next, file, mapping); } =20 if (anon_vma) { @@ -731,7 +662,8 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned l= ong start, struct vm_area_struct *expand) { struct mm_struct *mm =3D vma->vm_mm; - struct vm_area_struct *next =3D vma->vm_next, *orig_vma =3D vma; + struct vm_area_struct *next =3D find_vma(mm, vma->vm_end); + struct vm_area_struct *orig_vma =3D vma; struct address_space *mapping =3D NULL; struct rb_root_cached *root =3D NULL; struct anon_vma *anon_vma =3D NULL; @@ -739,7 +671,6 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned l= ong start, bool vma_changed =3D false; long adjust_next =3D 0; int remove_next =3D 0; - unsigned long ll_prev =3D vma->vm_start; /* linked list prev. */ =20 if (next && !insert) { struct vm_area_struct *exporter =3D NULL, *importer =3D NULL; @@ -773,7 +704,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned l= ong start, */ remove_next =3D 1 + (end > next->vm_end); VM_WARN_ON(remove_next =3D=3D 2 && - end !=3D next->vm_next->vm_end); + end !=3D find_vma(mm, next->vm_end)->vm_end); /* trim end to next, for case 6 first pass */ end =3D next->vm_end; } @@ -786,7 +717,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned l= ong start, * next, if the vma overlaps with it. */ if (remove_next =3D=3D 2 && !next->anon_vma) - exporter =3D next->vm_next; + exporter =3D find_vma(mm, next->vm_end); =20 } else if (end > next->vm_start) { /* @@ -881,15 +812,11 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned= long start, vma_mt_szero(mm, end, vma->vm_end); VM_WARN_ON(insert && insert->vm_end < vma->vm_end); - } else if (insert->vm_start =3D=3D end) { - ll_prev =3D vma->vm_end; } } else { vma_changed =3D true; } vma->vm_end =3D end; - if (!next) - mm->highest_vm_end =3D vm_end_gap(vma); } =20 if (vma_changed) @@ -909,17 +836,15 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned= long start, flush_dcache_mmap_unlock(mapping); } =20 - if (remove_next) { - __vma_unlink_list(mm, next); - if (file) - __remove_shared_vm_struct(next, file, mapping); + if (remove_next && file) { + __remove_shared_vm_struct(next, file, mapping); } else if (insert) { /* * split_vma has split insert from vma, and needs * us to insert it before dropping the locks * (it may either follow vma or precede it). */ - __insert_vm_struct(mm, insert, ll_prev); + __insert_vm_struct(mm, insert); } =20 if (anon_vma) { @@ -956,10 +881,10 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned= long start, /* * If "next" was removed and vma->vm_end was * expanded (up) over it, in turn - * "next->vm_prev->vm_end" changed and the - * "vma->vm_next" gap must be updated. + * "next->prev->vm_end" changed and the + * "vma->next" gap must be updated. */ - next =3D vma->vm_next; + next =3D find_vma(mm, vma->vm_end); } else { /* * For the scope of the comment "next" and @@ -977,33 +902,14 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned= long start, remove_next =3D 1; end =3D next->vm_end; goto again; - } else if (!next) { - /* - * If remove_next =3D=3D 2 we obviously can't - * reach this path. - * - * If remove_next =3D=3D 3 we can't reach this - * path because pre-swap() next is always not - * NULL. pre-swap() "next" is not being - * removed and its next->vm_end is not altered - * (and furthermore "end" already matches - * next->vm_end in remove_next =3D=3D 3). - * - * We reach this only in the remove_next =3D=3D 1 - * case if the "next" vma that was removed was - * the highest vma of the mm. However in such - * case next->vm_end =3D=3D "end" and the extended - * "vma" has vma->vm_end =3D=3D next->vm_end so - * mm->highest_vm_end doesn't need any update - * in remove_next =3D=3D 1 case. - */ - VM_WARN_ON(mm->highest_vm_end !=3D vm_end_gap(vma)); } } - if (insert && file) + if (insert && file) { uprobe_mmap(insert); + } =20 validate_mm(mm); + return 0; } =20 @@ -1163,10 +1069,10 @@ struct vm_area_struct *vma_merge(struct mm_struct *= mm, if (vm_flags & VM_SPECIAL) return NULL; =20 - next =3D __vma_next(mm, prev); + next =3D find_vma(mm, prev ? prev->vm_end : 0); area =3D next; if (area && area->vm_end =3D=3D end) /* cases 6, 7, 8 */ - next =3D next->vm_next; + next =3D find_vma(mm, next->vm_end); =20 /* verify some invariant that must be enforced by the caller */ VM_WARN_ON(prev && addr <=3D prev->vm_start); @@ -1300,18 +1206,24 @@ static struct anon_vma *reusable_anon_vma(struct vm= _area_struct *old, struct vm_ */ struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma) { + MA_STATE(mas, &vma->vm_mm->mm_mt, vma->vm_end, vma->vm_end); struct anon_vma *anon_vma =3D NULL; + struct vm_area_struct *prev, *next; =20 /* Try next first. */ - if (vma->vm_next) { - anon_vma =3D reusable_anon_vma(vma->vm_next, vma, vma->vm_next); + next =3D mas_walk(&mas); + if (next) { + anon_vma =3D reusable_anon_vma(next, vma, next); if (anon_vma) return anon_vma; } =20 + prev =3D mas_prev(&mas, 0); + VM_BUG_ON_VMA(prev !=3D vma, vma); + prev =3D mas_prev(&mas, 0); /* Try prev next. */ - if (vma->vm_prev) - anon_vma =3D reusable_anon_vma(vma->vm_prev, vma->vm_prev, vma); + if (prev) + anon_vma =3D reusable_anon_vma(prev, prev, vma); =20 /* * We might reach here with anon_vma =3D=3D NULL if we can't find @@ -2074,8 +1986,8 @@ int expand_upwards(struct vm_area_struct *vma, unsign= ed long address) if (gap_addr < address || gap_addr > TASK_SIZE) gap_addr =3D TASK_SIZE; =20 - next =3D vma->vm_next; - if (next && next->vm_start < gap_addr && vma_is_accessible(next)) { + next =3D find_vma_intersection(mm, vma->vm_end, gap_addr); + if (next && vma_is_accessible(next)) { if (!(next->vm_flags & VM_GROWSUP)) return -ENOMEM; /* Check that both stack segments have the same anon_vma? */ @@ -2121,8 +2033,6 @@ int expand_upwards(struct vm_area_struct *vma, unsign= ed long address) /* Overwrite old entry in mtree. */ vma_store(mm, vma); anon_vma_interval_tree_post_update_vma(vma); - if (!vma->vm_next) - mm->highest_vm_end =3D vm_end_gap(vma); spin_unlock(&mm->page_table_lock); =20 perf_event_mmap(vma); @@ -2141,6 +2051,7 @@ int expand_upwards(struct vm_area_struct *vma, unsign= ed long address) int expand_downwards(struct vm_area_struct *vma, unsigned long address) { struct mm_struct *mm =3D vma->vm_mm; + MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_start); struct vm_area_struct *prev; int error =3D 0; =20 @@ -2149,7 +2060,7 @@ int expand_downwards(struct vm_area_struct *vma, unsi= gned long address) return -EPERM; =20 /* Enforce stack_guard_gap */ - prev =3D vma->vm_prev; + prev =3D mas_prev(&mas, 0); /* Check that both stack segments have the same anon_vma? */ if (prev && !(prev->vm_flags & VM_GROWSDOWN) && vma_is_accessible(prev)) { @@ -2279,25 +2190,26 @@ find_extend_vma(struct mm_struct *mm, unsigned long= addr) EXPORT_SYMBOL_GPL(find_extend_vma); =20 /* - * Ok - we have the memory areas we should free on the vma list, - * so release them, and do the vma updates. + * Ok - we have the memory areas we should free on a maple tree so release= them, + * and do the vma updates. * * Called with the mm semaphore held. */ -static void remove_vma_list(struct mm_struct *mm, struct vm_area_struct *v= ma) +static inline void remove_mt(struct mm_struct *mm, struct ma_state *mas) { unsigned long nr_accounted =3D 0; + struct vm_area_struct *vma; =20 /* Update high watermark before we lower total_vm */ update_hiwater_vm(mm); - do { + mas_for_each(mas, vma, ULONG_MAX) { long nrpages =3D vma_pages(vma); =20 if (vma->vm_flags & VM_ACCOUNT) nr_accounted +=3D nrpages; vm_stat_account(mm, vma->vm_flags, -nrpages); - vma =3D remove_vma(vma); - } while (vma); + remove_vma(vma); + } vm_unacct_memory(nr_accounted); validate_mm(mm); } @@ -2307,18 +2219,18 @@ static void remove_vma_list(struct mm_struct *mm, s= truct vm_area_struct *vma) * * Called with the mm semaphore held. */ -static void unmap_region(struct mm_struct *mm, +static void unmap_region(struct mm_struct *mm, struct maple_tree *mt, struct vm_area_struct *vma, struct vm_area_struct *prev, + struct vm_area_struct *next, unsigned long start, unsigned long end) { - struct vm_area_struct *next =3D __vma_next(mm, prev); struct mmu_gather tlb; =20 lru_add_drain(); tlb_gather_mmu(&tlb, mm); update_hiwater_rss(mm); - unmap_vmas(&tlb, vma, start, end); - free_pgtables(&tlb, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, + unmap_vmas(&tlb, mt, vma, start, end); + free_pgtables(&tlb, mt, vma, prev ? prev->vm_end : FIRST_USER_ADDRESS, next ? next->vm_start : USER_PGTABLES_CEILING); tlb_finish_mmu(&tlb); } @@ -2402,24 +2314,12 @@ int split_vma(struct mm_struct *mm, struct vm_area_= struct *vma, return __split_vma(mm, vma, addr, new_below); } =20 -static inline int -unlock_range(struct vm_area_struct *start, struct vm_area_struct **tail, - unsigned long limit) +static inline void munmap_sidetree(struct vm_area_struct *vma, + struct ma_state *mas_detach) { - struct mm_struct *mm =3D start->vm_mm; - struct vm_area_struct *tmp =3D start; - int count =3D 0; - - while (tmp && tmp->vm_start < limit) { - *tail =3D tmp; - count++; - if (tmp->vm_flags & VM_LOCKED) - mm->locked_vm -=3D vma_pages(tmp); - - tmp =3D tmp->vm_next; - } - - return count; + vma_mas_store(vma, mas_detach); + if (vma->vm_flags & VM_LOCKED) + vma->vm_mm->locked_vm -=3D vma_pages(vma); } =20 /* @@ -2439,8 +2339,12 @@ do_mas_align_munmap(struct ma_state *mas, struct vm_= area_struct *vma, struct mm_struct *mm, unsigned long start, unsigned long end, struct list_head *uf, bool downgrade) { - struct vm_area_struct *prev, *last; - /* we have start < vma->vm_end */ + struct vm_area_struct *prev, *next =3D NULL; + struct maple_tree mt_detach; + int count =3D 0; + MA_STATE(mas_detach, &mt_detach, start, end - 1); + mt_init_flags(&mt_detach, MM_MT_FLAGS); + mt_set_external_lock(&mt_detach, &mm->mmap_lock); =20 mas->last =3D end - 1; /* @@ -2450,6 +2354,8 @@ do_mas_align_munmap(struct ma_state *mas, struct vm_a= rea_struct *vma, * unmapped vm_area_struct will remain in use: so lower split_vma * places tmp vma above, and higher split_vma places tmp vma below. */ + + /* Does it split the first one? */ if (start > vma->vm_start) { int error; =20 @@ -2461,34 +2367,55 @@ do_mas_align_munmap(struct ma_state *mas, struct vm= _area_struct *vma, if (end < vma->vm_end && mm->map_count >=3D sysctl_max_map_count) return -ENOMEM; =20 + /* + * mas_pause() is not needed since mas->index needs to be set + * differently than vma->vm_end anyways. + */ error =3D __split_vma(mm, vma, start, 0); if (error) return error; - prev =3D vma; - vma =3D __vma_next(mm, prev); - mas->index =3D start; - mas_reset(mas); - } else { - prev =3D vma->vm_prev; + + mas_set(mas, start); + vma =3D mas_walk(mas); } =20 - if (vma->vm_end >=3D end) - last =3D vma; - else - last =3D find_vma_intersection(mm, end - 1, end); + prev =3D mas_prev(mas, 0); + if (unlikely((!prev))) + mas_set(mas, start); =20 - /* Does it split the last one? */ - if (last && end < last->vm_end) { - int error =3D __split_vma(mm, last, end, 1); + /* + * Detach a range of VMAs from the mm. Using next as a temp variable as + * it is always overwritten. + */ + mas_for_each(mas, next, end - 1) { + /* Does it split the end? */ + if (next->vm_end > end) { + struct vm_area_struct *split; + int error; =20 - if (error) - return error; + error =3D __split_vma(mm, next, end, 1); + if (error) + return error; =20 - if (vma =3D=3D last) - vma =3D __vma_next(mm, prev); - mas_reset(mas); + mas_set(mas, end); + split =3D mas_prev(mas, 0); + munmap_sidetree(split, &mas_detach); + count++; + if (vma =3D=3D next) + vma =3D split; + break; + } + count++; + munmap_sidetree(next, &mas_detach); +#ifdef CONFIG_DEBUG_VM_MAPLE_TREE + BUG_ON(next->vm_start < start); + BUG_ON(next->vm_start > end); +#endif } =20 + if (!next) + next =3D mas_next(mas, ULONG_MAX); + if (unlikely(uf)) { /* * If userfaultfd_unmap_prep returns an error the vmas @@ -2505,35 +2432,36 @@ do_mas_align_munmap(struct ma_state *mas, struct vm= _area_struct *vma, return error; } =20 - /* - * unlock any mlock()ed ranges before detaching vmas, count the number - * of VMAs to be dropped, and return the tail entry of the affected - * area. - */ - mm->map_count -=3D unlock_range(vma, &last, end); - /* Drop removed area from the tree */ + /* Point of no return */ + mas_set_range(mas, start, end - 1); +#if defined(CONFIG_DEBUG_VM_MAPLE_TREE) + /* Make sure no VMAs are about to be lost. */ + { + MA_STATE(test, &mt_detach, start, end - 1); + struct vm_area_struct *vma_mas, *vma_test; + int test_count =3D 0; + + rcu_read_lock(); + vma_test =3D mas_find(&test, end - 1); + mas_for_each(mas, vma_mas, end - 1) { + BUG_ON(vma_mas !=3D vma_test); + test_count++; + vma_test =3D mas_next(&test, end - 1); + } + rcu_read_unlock(); + BUG_ON(count !=3D test_count); + mas_set_range(mas, start, end - 1); + } +#endif mas_store_gfp(mas, NULL, GFP_KERNEL); - - /* Detach vmas from the MM linked list */ - vma->vm_prev =3D NULL; - if (prev) - prev->vm_next =3D last->vm_next; - else - mm->mmap =3D last->vm_next; - - if (last->vm_next) { - last->vm_next->vm_prev =3D prev; - last->vm_next =3D NULL; - } else - mm->highest_vm_end =3D prev ? vm_end_gap(prev) : 0; - + mm->map_count -=3D count; /* * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or * VM_GROWSUP VMA. Such VMAs can change their size under * down_read(mmap_lock) and collide with the VMA we are about to unmap. */ if (downgrade) { - if (last && (last->vm_flags & VM_GROWSDOWN)) + if (next && (next->vm_flags & VM_GROWSDOWN)) downgrade =3D false; else if (prev && (prev->vm_flags & VM_GROWSUP)) downgrade =3D false; @@ -2541,10 +2469,12 @@ do_mas_align_munmap(struct ma_state *mas, struct vm= _area_struct *vma, mmap_write_downgrade(mm); } =20 - unmap_region(mm, vma, prev, start, end); - - /* Fix up all other VM information */ - remove_vma_list(mm, vma); + unmap_region(mm, &mt_detach, vma, prev, next, start, end); + /* Statistics and freeing VMAs */ + mas_set(&mas_detach, start); + remove_mt(mm, &mas_detach); + validate_mm(mm); + __mt_destroy(&mt_detach); =20 return downgrade ? 1 : 0; } @@ -2769,7 +2699,6 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, i_mmap_lock_write(vma->vm_file->f_mapping); =20 vma_mas_store(vma, &mas); - __vma_link_list(mm, vma, prev); mm->map_count++; if (vma->vm_file) { if (vma->vm_flags & VM_SHARED) @@ -2821,7 +2750,7 @@ unsigned long mmap_region(struct file *file, unsigned= long addr, vma->vm_file =3D NULL; =20 /* Undo any partial mapping done by a device driver. */ - unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end); + unmap_region(mm, mas.tree, vma, prev, next, vma->vm_start, vma->vm_end); charged =3D 0; if (vm_flags & VM_SHARED) mapping_unmap_writable(file->f_mapping); @@ -2910,11 +2839,12 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, st= art, unsigned long, size, goto out; =20 if (start + size > vma->vm_end) { - struct vm_area_struct *next; + VMA_ITERATOR(vmi, mm, vma->vm_end); + struct vm_area_struct *next, *prev =3D vma; =20 - for (next =3D vma->vm_next; next; next =3D next->vm_next) { + for_each_vma_range(vmi, next, start + size) { /* hole between vmas ? */ - if (next->vm_start !=3D next->vm_prev->vm_end) + if (next->vm_start !=3D prev->vm_end) goto out; =20 if (next->vm_file !=3D vma->vm_file) @@ -2923,8 +2853,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, star= t, unsigned long, size, if (next->vm_flags !=3D vma->vm_flags) goto out; =20 - if (start + size <=3D next->vm_end) - break; + prev =3D next; } =20 if (!next) @@ -2970,7 +2899,7 @@ static int do_brk_munmap(struct ma_state *mas, struct= vm_area_struct *vma, struct list_head *uf) { struct mm_struct *mm =3D vma->vm_mm; - struct vm_area_struct unmap; + struct vm_area_struct unmap, *next; unsigned long unmap_pages; int ret; =20 @@ -2987,6 +2916,7 @@ static int do_brk_munmap(struct ma_state *mas, struct= vm_area_struct *vma, ret =3D userfaultfd_unmap_prep(mm, newbrk, oldbrk, uf); if (ret) return ret; + ret =3D 1; =20 /* Change the oldbrk of vma to the newbrk of the munmap area */ @@ -3000,9 +2930,12 @@ static int do_brk_munmap(struct ma_state *mas, struc= t vm_area_struct *vma, vma_init(&unmap, mm); unmap.vm_start =3D newbrk; unmap.vm_end =3D oldbrk; + if (vma->anon_vma) + vma_set_anonymous(&unmap); if (vma_mas_remove(&unmap, mas)) goto mas_store_fail; =20 + vma->vm_end =3D newbrk; if (vma->anon_vma) { anon_vma_interval_tree_post_update_vma(vma); anon_vma_unlock_write(vma->anon_vma); @@ -3012,8 +2945,9 @@ static int do_brk_munmap(struct ma_state *mas, struct= vm_area_struct *vma, if (vma->vm_flags & VM_LOCKED) mm->locked_vm -=3D unmap_pages; =20 + next =3D mas_next(mas, ULONG_MAX); mmap_write_downgrade(mm); - unmap_region(mm, &unmap, vma, newbrk, oldbrk); + unmap_region(mm, mas->tree, &unmap, vma, next, newbrk, oldbrk); /* Statistics */ vm_stat_account(mm, vma->vm_flags, -unmap_pages); if (vma->vm_flags & VM_ACCOUNT) @@ -3024,6 +2958,7 @@ static int do_brk_munmap(struct ma_state *mas, struct= vm_area_struct *vma, return ret; =20 mas_store_fail: + mas_unlock(mas); vma->vm_end =3D oldbrk; if (vma->anon_vma) { anon_vma_interval_tree_post_update_vma(vma); @@ -3045,11 +2980,9 @@ static int do_brk_munmap(struct ma_state *mas, struc= t vm_area_struct *vma, * do some brk-specific accounting here. */ static int do_brk_flags(struct ma_state *mas, struct vm_area_struct *vma, - unsigned long addr, unsigned long len, - unsigned long flags) + unsigned long addr, unsigned long len, unsigned long flags) { struct mm_struct *mm =3D current->mm; - struct vm_area_struct *prev =3D NULL; validate_mm_mt(mm); =20 =20 @@ -3092,7 +3025,6 @@ static int do_brk_flags(struct ma_state *mas, struct = vm_area_struct *vma, khugepaged_enter_vma_merge(vma, flags); goto out; } - prev =3D vma; =20 /* create a vma struct for an anonymous mapping */ vma =3D vm_area_alloc(mm); @@ -3105,13 +3037,15 @@ static int do_brk_flags(struct ma_state *mas, struc= t vm_area_struct *vma, vma->vm_pgoff =3D addr >> PAGE_SHIFT; vma->vm_flags =3D flags; vma->vm_page_prot =3D vm_get_page_prot(flags); + if (vma->vm_file) + i_mmap_lock_write(vma->vm_file->f_mapping); vma_mas_store(vma, mas); - - if (!prev) - prev =3D mas_prev(mas, 0); - - __vma_link_list(mm, vma, prev); mm->map_count++; + if (vma->vm_file) { + __vma_link_file(vma); + i_mmap_unlock_write(vma->vm_file->f_mapping); + } + out: perf_event_mmap(vma); mm->total_vm +=3D len >> PAGE_SHIFT; @@ -3119,7 +3053,7 @@ static int do_brk_flags(struct ma_state *mas, struct = vm_area_struct *vma, if (flags & VM_LOCKED) mm->locked_vm +=3D (len >> PAGE_SHIFT); vma->vm_flags |=3D VM_SOFTDIRTY; - validate_mm_mt(mm); + validate_mm(mm); return 0; =20 vma_alloc_fail: @@ -3191,6 +3125,8 @@ void exit_mmap(struct mm_struct *mm) struct mmu_gather tlb; struct vm_area_struct *vma; unsigned long nr_accounted =3D 0; + MA_STATE(mas, &mm->mm_mt, 0, 0); + int count =3D 0; =20 /* mm's last user has gone, and its about to be pulled down */ mmu_notifier_release(mm); @@ -3215,7 +3151,7 @@ void exit_mmap(struct mm_struct *mm) mmap_write_lock(mm); arch_exit_mmap(mm); =20 - vma =3D mm->mmap; + vma =3D mas_find(&mas, ULONG_MAX); if (!vma) { /* Can happen if dup_mmap() received an OOM */ mmap_write_unlock(mm); @@ -3227,18 +3163,25 @@ void exit_mmap(struct mm_struct *mm) tlb_gather_mmu_fullmm(&tlb, mm); /* update_hiwater_rss(mm) here? but nobody should be looking */ /* Use -1 here to ensure all VMAs in the mm are unmapped */ - unmap_vmas(&tlb, vma, 0, -1); - free_pgtables(&tlb, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CEILING); + unmap_vmas(&tlb, &mm->mm_mt, vma, 0, ULONG_MAX); + free_pgtables(&tlb, &mm->mm_mt, vma, FIRST_USER_ADDRESS, USER_PGTABLES_CE= ILING); tlb_finish_mmu(&tlb); =20 - /* Walk the list again, actually closing and freeing it. */ - while (vma) { + /* + * Walk the list again, actually closing and freeing it, with preemption + * enabled, without holding any MM locks besides the unreachable + * mmap_write_lock. + */ + do { if (vma->vm_flags & VM_ACCOUNT) nr_accounted +=3D vma_pages(vma); - vma =3D remove_vma(vma); + remove_vma(vma); + count++; cond_resched(); - } - mm->mmap =3D NULL; + } while ((vma =3D mas_find(&mas, ULONG_MAX)) !=3D NULL); + + BUG_ON(count !=3D mm->map_count); + mmap_write_unlock(mm); trace_exit_mmap(mm); __mt_destroy(&mm->mm_mt); @@ -3277,7 +3220,7 @@ int insert_vm_struct(struct mm_struct *mm, struct vm_= area_struct *vma) vma->vm_pgoff =3D vma->vm_start >> PAGE_SHIFT; } =20 - vma_link(mm, vma, prev); + vma_link(mm, vma); return 0; } =20 @@ -3305,7 +3248,8 @@ struct vm_area_struct *copy_vma(struct vm_area_struct= **vmap, faulted_in_anon_vma =3D false; } =20 - if (range_has_overlap(mm, addr, addr + len, &prev)) + new_vma =3D find_vma_prev(mm, addr, &prev); + if (new_vma->vm_start < addr + len) return NULL; /* should never get here */ =20 new_vma =3D vma_merge(mm, prev, addr, addr + len, vma->vm_flags, @@ -3348,7 +3292,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct= **vmap, get_file(new_vma->vm_file); if (new_vma->vm_ops && new_vma->vm_ops->open) new_vma->vm_ops->open(new_vma); - vma_link(mm, new_vma, prev); + vma_link(mm, new_vma); *need_rmap_locks =3D false; } validate_mm_mt(mm); @@ -3649,12 +3593,13 @@ int mm_take_all_locks(struct mm_struct *mm) { struct vm_area_struct *vma; struct anon_vma_chain *avc; + MA_STATE(mas, &mm->mm_mt, 0, 0); =20 BUG_ON(mmap_read_trylock(mm)); =20 mutex_lock(&mm_all_locks_mutex); =20 - for (vma =3D mm->mmap; vma; vma =3D vma->vm_next) { + mas_for_each(&mas, vma, ULONG_MAX) { if (signal_pending(current)) goto out_unlock; if (vma->vm_file && vma->vm_file->f_mapping && @@ -3662,7 +3607,8 @@ int mm_take_all_locks(struct mm_struct *mm) vm_lock_mapping(mm, vma->vm_file->f_mapping); } =20 - for (vma =3D mm->mmap; vma; vma =3D vma->vm_next) { + mas_set(&mas, 0); + mas_for_each(&mas, vma, ULONG_MAX) { if (signal_pending(current)) goto out_unlock; if (vma->vm_file && vma->vm_file->f_mapping && @@ -3670,7 +3616,8 @@ int mm_take_all_locks(struct mm_struct *mm) vm_lock_mapping(mm, vma->vm_file->f_mapping); } =20 - for (vma =3D mm->mmap; vma; vma =3D vma->vm_next) { + mas_set(&mas, 0); + mas_for_each(&mas, vma, ULONG_MAX) { if (signal_pending(current)) goto out_unlock; if (vma->anon_vma) @@ -3729,11 +3676,12 @@ void mm_drop_all_locks(struct mm_struct *mm) { struct vm_area_struct *vma; struct anon_vma_chain *avc; + MA_STATE(mas, &mm->mm_mt, 0, 0); =20 BUG_ON(mmap_read_trylock(mm)); BUG_ON(!mutex_is_locked(&mm_all_locks_mutex)); =20 - for (vma =3D mm->mmap; vma; vma =3D vma->vm_next) { + mas_for_each(&mas, vma, ULONG_MAX) { if (vma->anon_vma) list_for_each_entry(avc, &vma->anon_vma_chain, same_vma) vm_unlock_anon_vma(avc->anon_vma); diff --git a/mm/nommu.c b/mm/nommu.c index 645d11d3a8ab..8b801f5c9ef9 100644 --- a/mm/nommu.c +++ b/mm/nommu.c @@ -551,7 +551,6 @@ static void put_nommu_region(struct vm_region *region) static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma= ) { struct address_space *mapping; - struct vm_area_struct *prev; MA_STATE(mas, &mm->mm_mt, vma->vm_start, vma->vm_end); =20 BUG_ON(!vma->vm_region); @@ -570,11 +569,8 @@ static void add_vma_to_mm(struct mm_struct *mm, struct= vm_area_struct *vma) i_mmap_unlock_write(mapping); } =20 - prev =3D mas_prev(&mas, 0); - mas_reset(&mas); /* add the VMA to the tree */ vma_mas_store(vma, &mas); - __vma_link_list(mm, vma, prev); } =20 /* @@ -599,7 +595,6 @@ static void delete_vma_from_mm(struct vm_area_struct *v= ma) =20 /* remove from the MM's tree and list */ vma_mas_remove(vma, &mas); - __vma_unlink_list(vma->vm_mm, vma); } =20 /* diff --git a/mm/util.c b/mm/util.c index 0f6faa9e0da5..07f5d398962e 100644 --- a/mm/util.c +++ b/mm/util.c @@ -271,46 +271,6 @@ void *memdup_user_nul(const void __user *src, size_t l= en) } EXPORT_SYMBOL(memdup_user_nul); =20 -void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, - struct vm_area_struct *prev) -{ - struct vm_area_struct *next; - - vma->vm_prev =3D prev; - if (prev) { - next =3D prev->vm_next; - prev->vm_next =3D vma; - } else { - next =3D mm->mmap; - mm->mmap =3D vma; - } - vma->vm_next =3D next; - if (next) - next->vm_prev =3D vma; - else - mm->highest_vm_end =3D vm_end_gap(vma); -} - -void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma) -{ - struct vm_area_struct *prev, *next; - - next =3D vma->vm_next; - prev =3D vma->vm_prev; - if (prev) - prev->vm_next =3D next; - else - mm->mmap =3D next; - if (next) { - next->vm_prev =3D prev; - } else { - if (prev) - mm->highest_vm_end =3D vm_end_gap(prev); - else - mm->highest_vm_end =3D 0; - } -} - /* Check if the vma is being used as a stack by this task */ int vma_is_stack_for_current(struct vm_area_struct *vma) { --=20 2.34.1