From patchwork Fri Mar 29 06:56:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13610220 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 4F879CD128D for ; Fri, 29 Mar 2024 06:57:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B3646B0083; Fri, 29 Mar 2024 02:57:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EE3B6B0088; Fri, 29 Mar 2024 02:57:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DB656B0089; Fri, 29 Mar 2024 02:57:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 61DDC6B0083 for ; Fri, 29 Mar 2024 02:57:05 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2771D411A8 for ; Fri, 29 Mar 2024 06:57:05 +0000 (UTC) X-FDA: 81949169610.18.CD7ED15 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) by imf17.hostedemail.com (Postfix) with ESMTP id 130DF4000C for ; Fri, 29 Mar 2024 06:57:02 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=sW0ssREe; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf17.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.119 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711695423; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AwEXLIJvfOIlKePIj3JTyBX7Gpl2aDHZTADkmkUue0Y=; b=jKYwMuulUavof4eo/n+V0aAYY0daqy0bo+mPGuPxcYRi8HH2ZQVfLMSTYZ0C5brAdRwCHj q60rFBBvb51vmdVZYb9gDReXbT9cnEiw4/B8W3I3wHv6SZapDYtV3C6fBGvMIKWAzONO6b HjRqv/owLpvinHgpLKY0fcUIbfQXnEc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=sW0ssREe; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf17.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.119 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711695423; a=rsa-sha256; cv=none; b=p1YfYRDsWKjCQoC2EUNfGZPh1uRPVaavE15nheYQnjQycEbb24QFdZT/Cvpy+Onvr7gXqd k0Yh/drJhq5jjLmDdOMZo6I1xE1kqc1M93ZC6Muts7ra3UIEiNE59NOYoauFtD7wtoyxsY yPqUfyVBEe0cipYTytf6Xhp2ezJ3xTs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1711695419; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=AwEXLIJvfOIlKePIj3JTyBX7Gpl2aDHZTADkmkUue0Y=; b=sW0ssREev2SrPO2U1eU6UcsIUzNnzMJG2hi2FSIfo658iANI40VjAZKaQtTlujsMNAiD8kyom5VxkgUrTYNAu8Un3XIpnCDNQ4+S4De0FBgakPl8iCsD3VJx17IcJJcuhX4ziY8NtgHs7FKNLtPTZJp1G3Iwn+Sjn0wtHjzxowU= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R311e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018045176;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0W3VqFSX_1711695417; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W3VqFSX_1711695417) by smtp.aliyun-inc.com; Fri, 29 Mar 2024 14:56:58 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: david@redhat.com, mgorman@techsingularity.net, wangkefeng.wang@huawei.com, jhubbard@nvidia.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] mm: factor out the numa mapping rebuilding into a new helper Date: Fri, 29 Mar 2024 14:56:45 +0800 Message-Id: <8bc2586bdd8dbbe6d83c09b77b360ec8fcac3736.1711683069.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 130DF4000C X-Stat-Signature: eydxeq6dqy41swu7y561csfgn79cinwh X-Rspam-User: X-HE-Tag: 1711695422-882142 X-HE-Meta: U2FsdGVkX18/dGEnIGWnOOpOwGqqYncdMY+OiW47FfbadJoRKcJ4ppi3jDbF9mjmu0OKa6jeNMIUr418868It54x755MYvS6RYFT8cnI8f8MyomWMkPMzIqziYbuLuFpRviuXIiY5OVReo6QYVTZz3t58zVoZZvVBWp1jrujK+2s7UeuUIO+k181pPDlQJM4OM7Uwwu2pLI8Pktx8O6BusvpGzopjU5QMRzNfWPQqmDfIAtQ5aInRmKSSCGHiaPE0DAbKMYX5zP0RvyXpDCHskmA/DUjnw1wWTcvUOTS7aTyKWXY9QtQZD5r+akE3Ba1hKnaphm253H5WpP60Qab4Aq+Ij70OcfNR9iUxrzbqdtrTieSVZuKzIPR6Sa5YDmhpSRJvzYwdGktT3p9JN9lRNMOKGfzLiefLdlczEglQM6BtyTFAbfa0dNWzUyRG8lWK/NQN4apfapfiy1jY8FV/sxGH7HYtaV6DbzTiJ3UNyJ/zx8aSNGGMZ5RujVWlh/xuroWidzP6nEkiGMZROAIAAv/cv/NOvkarJTfdgRLLboRmAI54LDoE8jliOR4jMh8cZMgx2p3RpL9401OzPWH5o8hs+ZyssSPlXtwhqDGlTDYt5FnWbaQQ5ao7LpHy9PdjuObgX8+NKsKk4hV75Q7VdTEv5uckqjg7vBDqq3M5UW1g/1YzZOT+kOe6KaYHEv+jTSuxkOu2FdypU+E3q8VS/bN40je6xcXTZSXPYlZ1Vj9+XpENzOpXa6tmt9BhJ4N5wp2/9aUmq5GF8zhPUJHcppN4LXlKMFsYqy6YuzJznfeNRZn4/Y/4+T1S3y/ESY+nrTJ8UAK97MWkjNhnRlNnblVHCCPcH9/wexbOm0A1IAIrww+0k8uoM96gbrwuGtDeW1q8CO6BSqe5E6HoksHMWMxTOhmJqzI 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: To support large folio's numa balancing, factor out the numa mapping rebuilding into a new helper as a preparation. Signed-off-by: Baolin Wang --- mm/memory.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 62ee4a15092a..c30fb4b95e15 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5054,6 +5054,20 @@ int numa_migrate_prep(struct folio *folio, struct vm_fault *vmf, return mpol_misplaced(folio, vmf, addr); } +static void numa_rebuild_single_mapping(struct vm_fault *vmf, struct vm_area_struct *vma, + bool writable) +{ + pte_t pte, old_pte; + + old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); + pte = pte_modify(old_pte, vma->vm_page_prot); + pte = pte_mkyoung(pte); + if (writable) + pte = pte_mkwrite(pte, vma); + ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); + update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); +} + static vm_fault_t do_numa_page(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; @@ -5159,13 +5173,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) * Make it present again, depending on how arch implements * non-accessible ptes, some can allow access by kernel mode. */ - old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); - pte = pte_modify(old_pte, vma->vm_page_prot); - pte = pte_mkyoung(pte); - if (writable) - pte = pte_mkwrite(pte, vma); - ptep_modify_prot_commit(vma, vmf->address, vmf->pte, old_pte, pte); - update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); + numa_rebuild_single_mapping(vmf, vma, writable); pte_unmap_unlock(vmf->pte, vmf->ptl); goto out; } From patchwork Fri Mar 29 06:56:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13610221 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 A5831CD1288 for ; Fri, 29 Mar 2024 06:57:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DF8DF6B0088; Fri, 29 Mar 2024 02:57:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA7A16B0089; Fri, 29 Mar 2024 02:57:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C70D06B008A; Fri, 29 Mar 2024 02:57:05 -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 9736E6B0089 for ; Fri, 29 Mar 2024 02:57:05 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 58DD31C1276 for ; Fri, 29 Mar 2024 06:57:05 +0000 (UTC) X-FDA: 81949169610.12.AC06F44 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) by imf12.hostedemail.com (Postfix) with ESMTP id 16A8C40003 for ; Fri, 29 Mar 2024 06:57:02 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=loWwtaVu; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf12.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.130 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711695423; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=/4wiUWtGk2X0UuPytgN5iWC1gTs4csEIxMl7r8vW1eA=; b=VnUOf+7BAObaRQKteaPGpmF6tBAV8YXV0IjtCGayedCrbzSrWbgC7dpVU2MgaFRuBI7rVv PCglvSP6IPVMwld8K91HSnwgdQ87kEVueAdrxfj7bp2AMhWZbc102yas+RCOo9emk8ucii UDMWbXEuWf47fci9liD3tbzDLkFjgMs= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=loWwtaVu; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf12.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.130 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711695423; a=rsa-sha256; cv=none; b=EQ2CDbdxlSEd93ixYjWPAThbc+reubaySn7Cni7LcCDh0gPEp8LjHlLpcQU891pozqRCOE W7l4hFRXFW9B6rnnuFn0YQZUIFcWhws+MT2ZfOJw/1E9NbbQm58dMcQUNG7fVApTtIFAv6 VSZd3Y/YmY4U9q3uwHIvLGmMwu2FwPM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1711695420; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=/4wiUWtGk2X0UuPytgN5iWC1gTs4csEIxMl7r8vW1eA=; b=loWwtaVu5GC4an9Ns7gFNCMg1blvdDE0urH17O7jW+kfnXK3GSQzuR3MAWPlnsAptPTps7aY2zTMBE1eTlnwHlGsnq4lQPIXE4F98ExDORhEDeF9dsTfhlE/thvM6skUMuPJjQd7ihfU1vbtEDJAKuVRTGPUqoGLpxK3cKJaVcc= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R901e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046050;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0W3VqFSr_1711695418; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W3VqFSr_1711695418) by smtp.aliyun-inc.com; Fri, 29 Mar 2024 14:56:59 +0800 From: Baolin Wang To: akpm@linux-foundation.org Cc: david@redhat.com, mgorman@techsingularity.net, wangkefeng.wang@huawei.com, jhubbard@nvidia.com, ying.huang@intel.com, 21cnbao@gmail.com, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] mm: support multi-size THP numa balancing Date: Fri, 29 Mar 2024 14:56:46 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 16A8C40003 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: tjxprx5gnw5zz1k44zbxsctu4jc8aoiz X-HE-Tag: 1711695422-200296 X-HE-Meta: U2FsdGVkX1/EKGH7YNe6NND17/IFB0AFjJfLqXgwv27fRaIf4g3j3hrGBdOY7UZZelef9y0q4j4WObKZi0f36EhCnCbL6Mpn7RdYFY8pEVu2/v64fga7yrG+s3AuOQEYsPphBjecDoui+NNHJNMMhqBwh6D6Upp6+UcQO1dGS3lGOICuX5fE5wHuV1oEppFYJIdQDrOB+fx6ut6ZQkVhspVCtgJI/Q2uSpcN5Z6j0/sTt3RypglTE7Ffu6HkE8NDSDq2F/3kfVMOD35x3P/yB17enk1/IOWw1FsffZiWfFmVOR2TafPxozLiDd9is6iaKUNeAmKRhLppSbg7frc2zkuI6y/zrPVtudmyczHZGSDsoiFuYi/O+/ajNU4jruZsJwDwHD0BlGwe03jVV2qt7wZAYc6OJBstvSkq8vbJNNWbp4Ej+AVDGVnQYTi2rGGt0bM1/p4eA2rDrgrMx/OY+7GKSh64Y7foymO6C+ANiApQVaPm/ul7zsYi2qJKWbasll353wLPpGL7Bsymbxa+LITDm7x4YnbvRytUbHnk3IMMgafY7VtezIptzgsb1aj0qPhrKCvE7Eg1clQSki9NlQhPG3Lb1h0r4neYsNnXwSNMtm6U61rwRvd8IuJhrUgOZzC+1mB7NvoZLqVe0cPIEOR65mtlUC5bhrgwasWIFy0WTGKvOzg4o9ucKFjKtuiZW9NnjIppJO8IgCG++YKV6qvTzMDc5OiJpg3k2Fy+AtpT4AXDiIIj6rHMepINgtmRD9CylQqVMR+shr/ASpOgduOLKN6KCsroiiPB2084mirdbeNP8h/gPNLd4zl55zJkr7emNYTT6JgYo7c3ulYLsC4vGVQ12XNHNECQibz+a2bk1aLxK5igs7t+JbMfGaSbDGK6ONgYYikCJiC8EMTEe5nxqsBR/7ihaiqhaVaVwhg4i6vUdW0xe5dO5RmDrCNPLe5zvp9PhNM66NoCU5o 9fcabpbH RL8LYr1RKWaisK0z05e8VVGw4m+p5xfJd1jkJJBC1IvRM9JQqxhVAMGzVBnuEElqAkvirEFcB0foBzOuyod4A2KK5sf+i8lPc13Ttk+x0pJj1CtrWjhalWd099NKVgV+U9QcrcL9SjNyG9K/aUDlCCW2ADG+qBEy0oc3lan8DnArko4XkstMLOI/0O1N1nlgJCkJXBNggkGVI8z/zqQAMrr5xRl6b+KRO/w2bDtI4uXhBXcH5NTUO/o4L4BEnmMQ10/zKIbPxSDY9IIxZBQMuoOiQHdFNJZEdzDsbdrjYOrAPMDK++apcK0D7gIEikvuy+ZfQHgKwYQ1VJueSYwnbpa7SMYTqV+XF3xc3 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: Now the anonymous page allocation already supports multi-size THP (mTHP), but the numa balancing still prohibits mTHP migration even though it is an exclusive mapping, which is unreasonable. Allow scanning mTHP: Commit 859d4adc3415 ("mm: numa: do not trap faults on shared data section pages") skips shared CoW pages' NUMA page migration to avoid shared data segment migration. In addition, commit 80d47f5de5e3 ("mm: don't try to NUMA-migrate COW pages that have other uses") change to use page_count() to avoid GUP pages migration, that will also skip the mTHP numa scaning. Theoretically, we can use folio_maybe_dma_pinned() to detect the GUP issue, although there is still a GUP race, the issue seems to have been resolved by commit 80d47f5de5e3. Meanwhile, use the folio_likely_mapped_shared() to skip shared CoW pages though this is not a precise sharers count. To check if the folio is shared, ideally we want to make sure every page is mapped to the same process, but doing that seems expensive and using the estimated mapcount seems can work when running autonuma benchmark. Allow migrating mTHP: As mentioned in the previous thread[1], large folios (including THP) are more susceptible to false sharing issues among threads than 4K base page, leading to pages ping-pong back and forth during numa balancing, which is currently not easy to resolve. Therefore, as a start to support mTHP numa balancing, we can follow the PMD mapped THP's strategy, that means we can reuse the 2-stage filter in should_numa_migrate_memory() to check if the mTHP is being heavily contended among threads (through checking the CPU id and pid of the last access) to avoid false sharing at some degree. Thus, we can restore all PTE maps upon the first hint page fault of a large folio to follow the PMD mapped THP's strategy. In the future, we can continue to optimize the NUMA balancing algorithm to avoid the false sharing issue with large folios as much as possible. Performance data: Machine environment: 2 nodes, 128 cores Intel(R) Xeon(R) Platinum Base: 2024-03-25 mm-unstable branch Enable mTHP to run autonuma-benchmark mTHP:16K Base Patched numa01 numa01 224.70 143.48 numa01_THREAD_ALLOC numa01_THREAD_ALLOC 118.05 47.43 numa02 numa02 13.45 9.29 numa02_SMT numa02_SMT 14.80 7.50 mTHP:64K Base Patched numa01 numa01 216.15 114.40 numa01_THREAD_ALLOC numa01_THREAD_ALLOC 115.35 47.41 numa02 numa02 13.24 9.25 numa02_SMT numa02_SMT 14.67 7.34 mTHP:128K Base Patched numa01 numa01 205.13 144.45 numa01_THREAD_ALLOC numa01_THREAD_ALLOC 112.93 41.88 numa02 numa02 13.16 9.18 numa02_SMT numa02_SMT 14.81 7.49 [1] https://lore.kernel.org/all/20231117100745.fnpijbk4xgmals3k@techsingularity.net/ Signed-off-by: Baolin Wang Reviewed-by: "Huang, Ying" --- mm/memory.c | 57 +++++++++++++++++++++++++++++++++++++++++++-------- mm/mprotect.c | 3 ++- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index c30fb4b95e15..2aca19e4fbd8 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5068,16 +5068,56 @@ static void numa_rebuild_single_mapping(struct vm_fault *vmf, struct vm_area_str update_mmu_cache_range(vmf, vma, vmf->address, vmf->pte, 1); } +static void numa_rebuild_large_mapping(struct vm_fault *vmf, struct vm_area_struct *vma, + struct folio *folio, pte_t fault_pte, bool ignore_writable) +{ + int nr = pte_pfn(fault_pte) - folio_pfn(folio); + unsigned long start = max(vmf->address - nr * PAGE_SIZE, vma->vm_start); + unsigned long end = min(vmf->address + (folio_nr_pages(folio) - nr) * PAGE_SIZE, vma->vm_end); + pte_t *start_ptep = vmf->pte - (vmf->address - start) / PAGE_SIZE; + bool pte_write_upgrade = vma_wants_manual_pte_write_upgrade(vma); + unsigned long addr; + + /* Restore all PTEs' mapping of the large folio */ + for (addr = start; addr != end; start_ptep++, addr += PAGE_SIZE) { + pte_t pte, old_pte; + pte_t ptent = ptep_get(start_ptep); + bool writable = false; + + if (!pte_present(ptent) || !pte_protnone(ptent)) + continue; + + if (pfn_folio(pte_pfn(ptent)) != folio) + continue; + + if (!ignore_writable) { + ptent = pte_modify(ptent, vma->vm_page_prot); + writable = pte_write(ptent); + if (!writable && pte_write_upgrade && + can_change_pte_writable(vma, addr, ptent)) + writable = true; + } + + old_pte = ptep_modify_prot_start(vma, addr, start_ptep); + pte = pte_modify(old_pte, vma->vm_page_prot); + pte = pte_mkyoung(pte); + if (writable) + pte = pte_mkwrite(pte, vma); + ptep_modify_prot_commit(vma, addr, start_ptep, old_pte, pte); + update_mmu_cache_range(vmf, vma, addr, start_ptep, 1); + } +} + static vm_fault_t do_numa_page(struct vm_fault *vmf) { struct vm_area_struct *vma = vmf->vma; struct folio *folio = NULL; int nid = NUMA_NO_NODE; - bool writable = false; + bool writable = false, ignore_writable = false; int last_cpupid; int target_nid; pte_t pte, old_pte; - int flags = 0; + int flags = 0, nr_pages; /* * The pte cannot be used safely until we verify, while holding the page @@ -5107,10 +5147,6 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) if (!folio || folio_is_zone_device(folio)) goto out_map; - /* TODO: handle PTE-mapped THP */ - if (folio_test_large(folio)) - goto out_map; - /* * Avoid grouping on RO pages in general. RO pages shouldn't hurt as * much anyway since they can be in shared cache state. This misses @@ -5130,6 +5166,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) flags |= TNF_SHARED; nid = folio_nid(folio); + nr_pages = folio_nr_pages(folio); /* * For memory tiering mode, cpupid of slow memory page is used * to record page access time. So use default value. @@ -5146,6 +5183,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) } pte_unmap_unlock(vmf->pte, vmf->ptl); writable = false; + ignore_writable = true; /* Migrate to the requested node */ if (migrate_misplaced_folio(folio, vma, target_nid)) { @@ -5166,14 +5204,17 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) out: if (nid != NUMA_NO_NODE) - task_numa_fault(last_cpupid, nid, 1, flags); + task_numa_fault(last_cpupid, nid, nr_pages, flags); return 0; out_map: /* * Make it present again, depending on how arch implements * non-accessible ptes, some can allow access by kernel mode. */ - numa_rebuild_single_mapping(vmf, vma, writable); + if (folio && folio_test_large(folio)) + numa_rebuild_large_mapping(vmf, vma, folio, pte, ignore_writable); + else + numa_rebuild_single_mapping(vmf, vma, writable); pte_unmap_unlock(vmf->pte, vmf->ptl); goto out; } diff --git a/mm/mprotect.c b/mm/mprotect.c index f8a4544b4601..94878c39ee32 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -129,7 +129,8 @@ static long change_pte_range(struct mmu_gather *tlb, /* Also skip shared copy-on-write pages */ if (is_cow_mapping(vma->vm_flags) && - folio_ref_count(folio) != 1) + (folio_maybe_dma_pinned(folio) || + folio_likely_mapped_shared(folio))) continue; /*