From patchwork Tue Mar 26 11:51:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13603987 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 D2442CD1280 for ; Tue, 26 Mar 2024 11:51:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3198D6B0088; Tue, 26 Mar 2024 07:51:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 254E06B0092; Tue, 26 Mar 2024 07:51:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F21FF6B008A; Tue, 26 Mar 2024 07:51:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DF7026B0087 for ; Tue, 26 Mar 2024 07:51:43 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AC1031C0AC3 for ; Tue, 26 Mar 2024 11:51:43 +0000 (UTC) X-FDA: 81939025686.22.CF0A88A Received: from out30-132.freemail.mail.aliyun.com (out30-132.freemail.mail.aliyun.com [115.124.30.132]) by imf13.hostedemail.com (Postfix) with ESMTP id 9741120015 for ; Tue, 26 Mar 2024 11:51:41 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=GspjoSx2; spf=pass (imf13.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711453902; 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=qq9URdAuWFaYVRUE0/jOGxRLImW74nNlrBDDLc2W6Zw=; b=3kjo5MtY/EUyTL1zX3WC7DpA93ITsgixgboWm/omEbI85kr9hvNYK2a1u7NphAWQK40j27 HJQRiagq97aAqL0lW0oxPSLc9hjSWmWNQnQkF4OyqDMG3XeDZZB9OG3filz6g1OOGD/SXA 7gA+HkL7uZG4y7FdQ8JHIGVEaiBIXrI= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=GspjoSx2; spf=pass (imf13.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.132 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711453902; a=rsa-sha256; cv=none; b=490emzFzw7DP8UZn+xTh3VFl4e1SpWCF/CD/mikS5KhQWKCtJAqWWvjh0sDC1nDrmMwGvg JJZPSAnz0euPjcwZoDmanw7B79o91J5ZsvzEh0da6KA/ZghCalSJbL2fRHVsX0sr1nx+3/ niGQxxAT8h3dNsfXdM1iIWgLm+e8gIQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1711453898; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=qq9URdAuWFaYVRUE0/jOGxRLImW74nNlrBDDLc2W6Zw=; b=GspjoSx2TwkVGl4xcC0eqyHWdI+XcMvFFX95hfoGV1tJLPg1P7n81fIBF8MW/yH/su/CGzvNuqIU1ijBT6P9+vlSq5JYRiiSFb9B5bv9ABY74DU8HAIEbnrY4+D+1E/ixmF1QhsxExDcCko1BRpLrn0Cy2Bn24l/rOHir9Jn9AE= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R431e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046051;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0W3LKqyp_1711453897; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W3LKqyp_1711453897) by smtp.aliyun-inc.com; Tue, 26 Mar 2024 19:51:37 +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 2/2] mm: support multi-size THP numa balancing Date: Tue, 26 Mar 2024 19:51:25 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 9741120015 X-Rspam-User: X-Stat-Signature: tpyzn9yiph8811353ky5p7cqug7z4xwm X-Rspamd-Server: rspam01 X-HE-Tag: 1711453901-535457 X-HE-Meta: U2FsdGVkX1+6ZtWR2f94iweddjZZ6t8YSESXShXvPqQAhBRB2TogVg4x7LeY1a2+U++KEuHaP/HNlAL401hwIzW6a4L3iPL0Hxb2egUOchCIH0UUA4vR8O5Cq3AUAV4hi8PkCLFSietchB0I8vSuTLYsgbogojfRDQuPmrO3e1uBzMR243F6uMcr0hBj+8LubLP00duQeq0yNPSkXQ1MVopniz9E+re/qqL8NlrR3HfYbmJUdZ9NvPIAKWHr+kpKn9fxzZcn/eQA2RHYPkkaLDAwZbv8j4xlfDR2PfBEBGh1K4hUxzssJINxRarb7O+B7pGoJ4BJAITTx1qhW95brVEzPBJon/atQgD+xsAGnkhhdZCOJ6ZETTbXixQa1P84pspAl21sXP3XlZXVqS8QGAtuyA65s0mxxKOAZLEX9bw/kwTHogS5VHvGDOqev4UjC+74qlBjFKOYbdRZzI/v/xAaByD6o/EPI3q9/k9pesxmBzhhhHTZSLMf7rnDYemoUwAb0/r5lZ2rBeeHCj5KYZCwPJpRA2g36jHDC2k22DK7b5siz0KuZVfgTmDUo3daC3Tuq0BTCtVoaHUqlPSkgSFlLMDkULJBOrNYjiFmaSmTohhr7/nZAvO63WdmoDcYzcV9VMAzwnxeDC0KyVHpfX4eVzNUoAadO8d4JBWTATV28pbsQR6QP5KhqNBEyH4zd75aK9gZm3iwe4Vue0qZkUrHpdWewf96ElibxmKYhBS6tcLOPpV/6BSYSUOLkAEu7X8zX+QizqPgoiuSJ9iz91P6UqOoxNwl107By8HjT99OZSAYUs7KQ8Qh21raNH8UofdxOPmFnV2ltTmjcqj+fYATuZijNiw2k4CE7Ww/vXL8ORQX4Awv7vTCgZfH+ZOdWiyzvZvVGSQ6FxuIY6RjGqlETGK4PdAr 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 137.23 numa01_THREAD_ALLOC numa01_THREAD_ALLOC 118.05 50.57 numa02 numa02 13.45 9.30 numa02_SMT numa02_SMT 14.80 7.43 mTHP:64K Base Patched numa01 numa01 216.15 135.20 numa01_THREAD_ALLOC numa01_THREAD_ALLOC 115.35 46.93 numa02 numa02 13.24 9.24 numa02_SMT numa02_SMT 14.67 7.31 mTHP:128K Base Patched numa01 numa01 205.13 140.41 numa01_THREAD_ALLOC numa01_THREAD_ALLOC 112.93 44.78 numa02 numa02 13.16 9.19 numa02_SMT numa02_SMT 14.81 7.39 [1] https://lore.kernel.org/all/20231117100745.fnpijbk4xgmals3k@techsingularity.net/ Signed-off-by: Baolin Wang --- mm/memory.c | 56 +++++++++++++++++++++++++++++++++++++++++++-------- mm/mprotect.c | 3 ++- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index c30fb4b95e15..36191a9c799c 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5068,16 +5068,55 @@ 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(start + folio_nr_pages(folio) * 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 (vm_normal_folio(vma, addr, ptent) != folio) + continue; + + if (!ignore_writable) { + writable = pte_write(pte); + if (!writable && pte_write_upgrade && + can_change_pte_writable(vma, addr, pte)) + 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 +5146,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 +5165,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 +5182,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 +5203,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; /*