From patchwork Fri May 27 22:58:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Kravetz X-Patchwork-Id: 12863872 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 2ADE4C433EF for ; Fri, 27 May 2022 22:59:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0963D8D0007; Fri, 27 May 2022 18:59:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 01EC58D0002; Fri, 27 May 2022 18:59:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB0B58D0007; Fri, 27 May 2022 18:59:13 -0400 (EDT) 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 CA5F18D0002 for ; Fri, 27 May 2022 18:59:13 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A431020D1D for ; Fri, 27 May 2022 22:59:13 +0000 (UTC) X-FDA: 79513040586.11.035183F Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf03.hostedemail.com (Postfix) with ESMTP id 9768720028 for ; Fri, 27 May 2022 22:58:59 +0000 (UTC) Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24RKldMU006274; Fri, 27 May 2022 22:59:07 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=3zEOXDOnNVDX1cmb24BXpclDgrucJjzYv60skjVuz1w=; b=WMD0C1dbAFIJPBuBh/Sdm+Qi14EjYr4+N7c7cwgYnCtFZahFTipWPMlU88DhKPsoL0cZ KnSZSF6YZ24IFNCrx/ADeLr8SQmNlRe5ypSfFmc1AM8oft9GXl4MYRGadJGtl3/ZuciU hP4GIaLftcld66wKzd+H+vVZV57I6pvubE3k7QG8g6b9HCiS0gveyQw+VFbJSSthpSCS yFSUiI+xaw/Tjx4jVAgvViSdJ0/soghAQSLTMkcEj9yquYZ6npDyMoy4OYn8ejbd+A0t 3nUdtGBqslzzNwhn1np42Q8T2I89ou9qm8+alDqbli0X3/OcUa44ngktk+S0IIXi1Evb Hg== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3g93tc8cex-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 27 May 2022 22:59:06 +0000 Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 24RMoHiH009606; Fri, 27 May 2022 22:59:05 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2168.outbound.protection.outlook.com [104.47.58.168]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com with ESMTP id 3g93wys19p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 27 May 2022 22:59:05 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=drJBCzgf4OQAbPL2G5T8lzOWjquKOHWhtg6Rn0/jpMHFTE9DfOYl3hHUtKM0bX9kARIydyZ1kjrYMcL0WiV913ImkiNnCzbMPcF1yWQrShQZ+YLaq6h53VIeUlqIeD320S7vWtA0zVofOsfEBWcj5ZbAk8ws89aW5MQb5H5PzCJBNaS4V05oUi5X1U1229KdV+jFtxIZy4Yp/NO5lIy2edvvId3TMCfrc9QNIoQrC1TgAJAiGv7mXTAYD5vWHA/yCr2KWkSc1xxyl8HhPD+KoFM92Yq0nhr4wIE5VYGOBibJznvVDvPpabzaJ5noR5+GPyL6I7n5J9M981BXGFOcVg== 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=3zEOXDOnNVDX1cmb24BXpclDgrucJjzYv60skjVuz1w=; b=IwQ13nSzn9r40iYiyDLQku/McObryMt38vYOtAqz5k8gDnArESAivoPB98UEMTZgsBwdcLA611ILrtt0UGRu6XKhzTvC21e0kVlfbBfJj638GWCH5ShyQJSqhuAN5wk3GWNv68P9/zym7BJX/GWDdhQ9UjxAGiVdPdNsuW4iy9nesCutJBwbI7WQlp5ucf4ME3XpGhZ3hFWUlOYxe4yt1jT/jWvzvlZ/ecVboAEZ+V4b1I+ryBuYgbqVPNYzKEWZIL3w/5igV+8ht64FaRg51uy6vR3pz1/AkDsKTsdrPxfn8XgbY/z+FMzTKIREFTnuGBzWL6AVatcIH7OMHYHhtA== 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=3zEOXDOnNVDX1cmb24BXpclDgrucJjzYv60skjVuz1w=; b=xl0MoRvStaJtP3btr386iQCnY3nY8K+FQbx23BGO+fYKza9nE0ZWDCMnJ5DTcTXbwXpzHpQodCZ+TetaY+wUdrsD/RLVg+PeKpgyosZHKj0rRqNGXOWQkSbQFkYBwX/K8ig1dSWdn0PJZamB2XKEsn3AA9Tf1yv8b/sMx+36xJc= Received: from BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) by DS7PR10MB4831.namprd10.prod.outlook.com (2603:10b6:5:3ab::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 22:59:03 +0000 Received: from BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::2125:9bb7:bfeb:81f9]) by BY5PR10MB4196.namprd10.prod.outlook.com ([fe80::2125:9bb7:bfeb:81f9%9]) with mapi id 15.20.5293.016; Fri, 27 May 2022 22:59:03 +0000 From: Mike Kravetz To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Muchun Song , Michal Hocko , Peter Xu , Naoya Horiguchi , James Houghton , Mina Almasry , "Aneesh Kumar K . V" , Anshuman Khandual , Paul Walmsley , Christian Borntraeger , Andrew Morton , Mike Kravetz Subject: [RFC PATCH 1/3] hugetlb: skip to end of PT page mapping when pte not present Date: Fri, 27 May 2022 15:58:47 -0700 Message-Id: <20220527225849.284839-2-mike.kravetz@oracle.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220527225849.284839-1-mike.kravetz@oracle.com> References: <20220527225849.284839-1-mike.kravetz@oracle.com> X-ClientProxiedBy: MWHPR17CA0091.namprd17.prod.outlook.com (2603:10b6:300:c2::29) To BY5PR10MB4196.namprd10.prod.outlook.com (2603:10b6:a03:20d::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c51018e1-f5bf-4799-7b85-08da40347e65 X-MS-TrafficTypeDiagnostic: DS7PR10MB4831: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: DNBpEErFZEsfyJQKOQDlQFsgtURvw4ysEM9U/zLpmmEg63lwD6hXu1Z7xvaJdVWk5v5+AQTEDR2UpIyGCN4BNz9jZmpmAq5uXN4U4ey1yNAM4XJx6LclGqSF5pmQzRLGqCSax8ztI0cKUfTWnAtXr2FezE6hvlPTudb0EVJT7soAMJoQTLHihOV+hxtxLov+EbGIztHpooHlijtuAuc7bixjiZJpB7phOfqHqjFmhX/QglKmmEwG8AjgzHSwF7Jqq0TIngmkjvkhz8hh1vTWiVKt5yUleDz2BwBZWU4VrJ+xnB7sg9y2aoZ59XVv93gWGGrFgNBPCojUK/pryjebKrXg+rdQ5e73Jw7S2S18yIVxBoQfljjKLqrMC+v3rPN/2A2SpgajscRNTdbGAXukC4rxUUzHuZEo4+RKFUrbqwsomlW8TDkEyuTQLP5B+iss+dXqPNIHmmAU2zEsh6RP6ldk7ytiQn55hZDQt5PnkdN90vrB6WeejiIM7HzVf3S5qvAqtkhgp6e0JYk5g9KKFc8vC/+ibwFCTgB+Wl974/MT09eP/0kiqMEA97qqERVCh47BWfzW9BddGlYxMSoE75wfx5HBaUwIpk1iWYmxAiek3hTh/6oegX4y1M32iEuZbj2/gwesW7C2dHLU9ZfOPRlXIeEDvIFKBZFEn0BBEvJqhV58DGoM4xJr/sgOBedFN5fwz5XmjhzOmYpK+ksocw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BY5PR10MB4196.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(26005)(6506007)(6512007)(6666004)(5660300002)(2906002)(52116002)(508600001)(44832011)(8936002)(316002)(86362001)(66556008)(66476007)(66946007)(4326008)(8676002)(6486002)(54906003)(7416002)(36756003)(83380400001)(38100700002)(38350700002)(2616005)(1076003)(107886003)(186003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2lDWOWtqURVwYl4lbdgQr8szMg1ITLFvfAQ2psuUack8Z85Smj3B+Kz4Wu6GTdsr3gujDhyg+IFzCO3qaZ8gDU5qGhblekF/DDClP3+xduK9LCdJodKsdo6YERwzAWFiwiX/K4kkxVFl4XsK/BId31IMYNM65yH7tweniVM7+N/f67LQ3u7yQkZmijOArvH1mrm34XCOPlhH1v55hu4hnBYEH+fWyGXrV7tqKhrzbGFnRXamhws2gVpqBrdkUk7OfZ30pAREhhAQHLEwRlklByTJ6T20wBcoYMQTjbk7bHoVSKjRf0cV0j4VQHDcIXdIFz2+gMM1cQRW6nJMwU2vcPlVDVwLbClIxlpveb7Mh7ngPTG4qWNTEitHGvA/m4NOXZrAIGXksCkPllj0Cdn2OZv0oA41r2iRuDVwwoNP17FiI1D/pSYvYy8aw/1H7IV6UESXqyWhZwSHXfPA6bc5jEGQmdSfJ6J4vwJHw8qJGpHtdEfYMXc3V2FtEkut76emWIdyEjfdn+PaBxfQV19YkL0w5wG6Kc184oJV5opV4gmvAJzDKViSycZxSPuPgTfIRwepG+ZZktz+9YAvXWuC61LIW0hjhQ62eXjmMGhV+bRIvCUeeg7nYOwRSAQTkz8thRiI1Vysb249eGA/OPvBe+thCdGA97flMWWXmy3MGEsBiljbvkQHF0vugf5Srtczvnxxpvl7D+jZLMl82AigNRXOm88/pvluLV1Tu307OagO91brO56lEWzLjcyjKTfAAKtmCO3sU7g3vQ7bWTh0KXxLxFQEeYzLfT5ZNQS77PekH354PqvxCqBiHF8AAIrZYuADr/MJM2wEPMo8wdg9SKdk0/45pcN4iATk3gdfn2LHqJOaWp4EiQEb73XLHHf7VoYgoEM0sV631E/h0qYMdfuIwRwPHBytO16EbzcnJZalGorIYiFXBOiLqyjMknfoJUzEnRg6D7r0x4IwwnaJ+zCR12gFKq7MpKXtLq+LfU6rnCkm7CkGChQgBqJYPgIEWz/TlxMv0ZWp8jO98NMWZkGGaDxPGEpvySisJ8V4YidlpRvmG9rWfo2aKFGLat3GEK+NzFnTYLEH0e+ozb3uwwf6kSHxjf97mx6sdEOkgxrQbYTYp6k+Ph1CVnROzcyx/Kuh30nDVtyvd7ZPKpgVmoTvoO1ESwnTFbO1lcpl6p/ODRwO3ZfI6b3Uc/9vZwDeqQ6HgTu7jbfmZNvLSBLPADIAbNStccv5Mz9A1/o7Oaw3MzQUG+HnWE5Fu3oAaDpHRRrLHbigrZ8voSDZIwNobufYJ/24he4B8LTk797vlrBZk4spPOOB9mPgS2hZskCBnHGMdtrLIpxplye+qGKqfbX5Zv05O/Q4POVUsaX83zeDVNDLeRC4wheEKFgqm211VYx7WX5LXQuqIzSRNzySvwcwWIFrQyube0nOoGbAFiCg/tc6OwwvKmOkkFGwuz4iNrQzVy1qXOoQEyR585/UeIMnBfqlE61CAxnud6zZtbjq062nTn8XgAN7DT2gvQpvtw/GvHCFYzxDarcqcLDxpptIJ1k/n1bDYj7Sfui+bYQgJCItrj3g0+x/IqDVxDu9Sxx6V34v/zh+uTtT078Bi6RyYx824nv7c7VV8icfVR4y0+YnytKT4PeccaOUUYLutVaCGaRhAkRfvJ563iDHmuclXor5AVgRgzw71XwFZhcXlG4gAjKOAj4rKCu+CC8osPh1LIS0yb4aZ/7iyZacICXi3Dn8DruMJ9tPaxfPnmU= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c51018e1-f5bf-4799-7b85-08da40347e65 X-MS-Exchange-CrossTenant-AuthSource: BY5PR10MB4196.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 22:59:03.0652 (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: kpzUJwew93OyEjoQzxSxSeIwZ2I5bxBuvlgJNeWiJM13UGaY5nlNWRCFfwZUM2Jf4MrGD20s/SkXpxtZZWo6kg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR10MB4831 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486,18.0.874 definitions=2022-05-27_07:2022-05-27,2022-05-27 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 adultscore=0 mlxscore=0 phishscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2204290000 definitions=main-2205270111 X-Proofpoint-ORIG-GUID: gYnZrLLda-e_fdbSDCoMVcYP8red8kdU X-Proofpoint-GUID: gYnZrLLda-e_fdbSDCoMVcYP8red8kdU Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2021-07-09 header.b=WMD0C1db; dkim=pass header.d=oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=xl0MoRvS; dmarc=pass (policy=none) header.from=oracle.com; spf=none (imf03.hostedemail.com: domain of mike.kravetz@oracle.com has no SPF policy when checking 205.220.177.32) smtp.mailfrom=mike.kravetz@oracle.com X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9768720028 X-Stat-Signature: gbwuzkmmisk9p471kq6or4xcycr75wx5 X-HE-Tag: 1653692339-643839 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: HugeTLB address ranges are linearly scanned during fork, unmap and remap operations. If a non-present entry is encountered, the code currently continues to the next huge page aligned address. However, a non-present entry implies that the page table page for that entry is not present. Therefore, the linear scan can skip to the end of range mapped by the page table page. This can speed operations on large sparsely populated hugetlb mappings. Create a new routine hugetlb_mask_last_hp() that will return an address mask. When the mask is ORed with an address, the result will be the address of the last huge page mapped by the associated page table page. Use this mask to update addresses in routines which linearly scan hugetlb address ranges when a non-present pte is encountered. hugetlb_mask_last_hp is related to the implementation of huge_pte_offset as hugetlb_mask_last_hp is called when huge_pte_offset returns NULL. This patch only provides a complete hugetlb_mask_last_hp implementation when CONFIG_ARCH_WANT_GENERAL_HUGETLB is defined. Architectures which provide their own versions of huge_pte_offset can also provide their own version of hugetlb_mask_last_hp. Signed-off-by: Mike Kravetz Tested-by: Baolin Wang Reviewed-by: Baolin Wang --- include/linux/hugetlb.h | 1 + mm/hugetlb.c | 58 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index e4cff27d1198..25078a0ea1d8 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -194,6 +194,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long addr, unsigned long sz); pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr, unsigned long sz); +unsigned long hugetlb_mask_last_hp(struct hstate *h); int huge_pmd_unshare(struct mm_struct *mm, struct vm_area_struct *vma, unsigned long *addr, pte_t *ptep); void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 7c468ac1d069..a2db878b2255 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -4731,6 +4731,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, unsigned long npages = pages_per_huge_page(h); struct address_space *mapping = src_vma->vm_file->f_mapping; struct mmu_notifier_range range; + unsigned long last_addr_mask; int ret = 0; if (cow) { @@ -4750,11 +4751,14 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, i_mmap_lock_read(mapping); } + last_addr_mask = hugetlb_mask_last_hp(h); for (addr = src_vma->vm_start; addr < src_vma->vm_end; addr += sz) { spinlock_t *src_ptl, *dst_ptl; src_pte = huge_pte_offset(src, addr, sz); - if (!src_pte) + if (!src_pte) { + addr |= last_addr_mask; continue; + } dst_pte = huge_pte_alloc(dst, dst_vma, addr, sz); if (!dst_pte) { ret = -ENOMEM; @@ -4771,8 +4775,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, * after taking the lock below. */ dst_entry = huge_ptep_get(dst_pte); - if ((dst_pte == src_pte) || !huge_pte_none(dst_entry)) + if ((dst_pte == src_pte) || !huge_pte_none(dst_entry)) { + addr |= last_addr_mask; continue; + } dst_ptl = huge_pte_lock(h, dst, dst_pte); src_ptl = huge_pte_lockptr(h, src, src_pte); @@ -4933,6 +4939,7 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, unsigned long sz = huge_page_size(h); struct mm_struct *mm = vma->vm_mm; unsigned long old_end = old_addr + len; + unsigned long last_addr_mask; unsigned long old_addr_copy; pte_t *src_pte, *dst_pte; struct mmu_notifier_range range; @@ -4948,12 +4955,16 @@ int move_hugetlb_page_tables(struct vm_area_struct *vma, flush_cache_range(vma, range.start, range.end); mmu_notifier_invalidate_range_start(&range); + last_addr_mask = hugetlb_mask_last_hp(h); /* Prevent race with file truncation */ i_mmap_lock_write(mapping); for (; old_addr < old_end; old_addr += sz, new_addr += sz) { src_pte = huge_pte_offset(mm, old_addr, sz); - if (!src_pte) + if (!src_pte) { + old_addr |= last_addr_mask; + new_addr |= last_addr_mask; continue; + } if (huge_pte_none(huge_ptep_get(src_pte))) continue; @@ -4998,6 +5009,7 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct struct hstate *h = hstate_vma(vma); unsigned long sz = huge_page_size(h); struct mmu_notifier_range range; + unsigned long last_addr_mask; bool force_flush = false; WARN_ON(!is_vm_hugetlb_page(vma)); @@ -5018,11 +5030,14 @@ static void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct end); adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end); mmu_notifier_invalidate_range_start(&range); + last_addr_mask = hugetlb_mask_last_hp(h); address = start; for (; address < end; address += sz) { ptep = huge_pte_offset(mm, address, sz); - if (!ptep) + if (!ptep) { + address |= last_addr_mask; continue; + } ptl = huge_pte_lock(h, mm, ptep); if (huge_pmd_unshare(mm, vma, &address, ptep)) { @@ -6285,6 +6300,7 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, unsigned long pages = 0, psize = huge_page_size(h); bool shared_pmd = false; struct mmu_notifier_range range; + unsigned long last_addr_mask; bool uffd_wp = cp_flags & MM_CP_UFFD_WP; bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; @@ -6301,12 +6317,15 @@ unsigned long hugetlb_change_protection(struct vm_area_struct *vma, flush_cache_range(vma, range.start, range.end); mmu_notifier_invalidate_range_start(&range); + last_addr_mask = hugetlb_mask_last_hp(h); i_mmap_lock_write(vma->vm_file->f_mapping); for (; address < end; address += psize) { spinlock_t *ptl; ptep = huge_pte_offset(mm, address, psize); - if (!ptep) + if (!ptep) { + address |= last_addr_mask; continue; + } ptl = huge_pte_lock(h, mm, ptep); if (huge_pmd_unshare(mm, vma, &address, ptep)) { /* @@ -6857,6 +6876,35 @@ pte_t *huge_pte_offset(struct mm_struct *mm, return (pte_t *)pmd; } +/* + * Return a mask that can be used to update an address to the last huge + * page in a page table page mapping size. Used to skip non-present + * page table entries when linearly scanning address ranges. Architectures + * with unique huge page to page table relationships can define their own + * version of this routine. + */ +unsigned long hugetlb_mask_last_hp(struct hstate *h) +{ + unsigned long hp_size = huge_page_size(h); + + if (hp_size == P4D_SIZE) + return PGDIR_SIZE - P4D_SIZE; + else if (hp_size == PUD_SIZE) + return P4D_SIZE - PUD_SIZE; + else if (hp_size == PMD_SIZE) + return PUD_SIZE - PMD_SIZE; + + return ~(0); +} + +#else + +/* See description above. Architectures can provide their own version. */ +__weak unsigned long hugetlb_mask_last_hp(struct hstate *h) +{ + return ~(0); +} + #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */ /*