From patchwork Wed Apr 3 08:39:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baolin Wang X-Patchwork-Id: 13615376 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 6DABDC6FD1F for ; Wed, 3 Apr 2024 08:39:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 889E96B00A3; Wed, 3 Apr 2024 04:39:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7EDF16B00A1; Wed, 3 Apr 2024 04:39:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F3B06B00A5; Wed, 3 Apr 2024 04:39:27 -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 3CE3A6B00A1 for ; Wed, 3 Apr 2024 04:39:27 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 089E74069A for ; Wed, 3 Apr 2024 08:39:27 +0000 (UTC) X-FDA: 81967571574.28.4C3B22A Received: from out30-124.freemail.mail.aliyun.com (out30-124.freemail.mail.aliyun.com [115.124.30.124]) by imf15.hostedemail.com (Postfix) with ESMTP id E96C8A0012 for ; Wed, 3 Apr 2024 08:39:24 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=BZYvZHn6; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf15.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 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=1712133565; 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=dJyQ2t09mQLZqD5haLWTwuWBgyt7BYeOyjErTFbdfvE=; b=WfG9pPAKTVJaTNsuNxoqEim9TKfIpKACsb13bMeMtlKoFWox5tClk+nY6KxHaRn81TokOr 4z2ut4wx297da8jyQb3fQVpGxSWIjUxOZiFy3Pu9Sm4TN2rTQRiVKWI9CPkpNj+HFWh0BS A1qwO0pYAwsnA4RYdHG0ynU2sbIcn3k= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=BZYvZHn6; dmarc=pass (policy=none) header.from=linux.alibaba.com; spf=pass (imf15.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.124 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712133565; a=rsa-sha256; cv=none; b=2cBaPbiQEUX9IMqO19rtHi511ic9253qt30XSLEZPobwDvLWN34m2yrl+XSD1ynbkx1YM9 DPepL2JEoWXeq06MxV3voHqoW4o6aYOjVmg+6C1TYR8jd1OD29Mw18Vbk687W+wJ4wZjiP oo7kQfs4G8KSrkZg9/W/l1PuNp4I+KM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1712133562; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=dJyQ2t09mQLZqD5haLWTwuWBgyt7BYeOyjErTFbdfvE=; b=BZYvZHn6rrg85O7VQVfoIMvoDpBP/zPXLQ+EuIVSAwA71j8jR1D6FP4LZQ/0VpN/HNLvVTrExfjq1psorB/eE/vhbPkoxJj3j2PosOGndIxOZtrF8G8eQpNmxePZT2hu22TDMsJezqdTf97qtxqRrlMDAf5SBOaQI6Nbpawl4qk= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R151e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046059;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---0W3rEr4s_1712133560; Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0W3rEr4s_1712133560) by smtp.aliyun-inc.com; Wed, 03 Apr 2024 16:39:20 +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 v3 2/2] mm: support multi-size THP numa balancing Date: Wed, 3 Apr 2024 16:39:10 +0800 Message-Id: X-Mailer: git-send-email 2.39.3 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: E96C8A0012 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ek9ceyrt6jf641deby97kmidhagikmsx X-HE-Tag: 1712133564-366911 X-HE-Meta: U2FsdGVkX1/aZ/c4Nybgri6KBlSJJds+qphuAhnVkq0tYzACXVAsAXl6PbDe9q4Kyitzsutteyg30yfdnIHUJnIPqPNPul04M765rOI3p37zSJZlzpUJI7XBFE5/DT5RNF66oRQxBb9TC631qe/0rK+TrL0dnOsI4qTud70WsCCkEUYfWO+u0NffkJESNNVnoy8tHmT9tYFesJcnN40gZOJIo6DoOKlWjFd8x519+PINHI3a6LwmhTDLE7gPfWE/Eeb/OVuginridvjYCCg0HcxNATvRsa2pUWzlJQL4S8UUss+nKH8yDXeF8Vb8Cfyez0uVsqliLG6PakfG8f4vobVKO7JNRqHx7qjjLG3OpnEBkwRf4adP1niXgHV+8Lrg7gjKUSPVXP0Vwm7TcEGXOHfyWnLErrPxpduLO4Fz0kkAxCad1NRkmwnnwshCrHl9OU1E0UaGHr/jVWEb0b3/qOrMT9W8vVx9qlHnPKacBE6cKD78tawmp1RKHk8BAFHzHtRO8/9NrVqbZ0/rljDB/YBazkdQ//cX6mew1Ghi5rGhvZIeQB9I0rMa1Dk4+9GZfQCv9qdmFst4VJpY9lKI+Tftw+f9sdApR4L4LA97vof+OiHzUYVwQ4ngbvm8Vi7NFBTEsUGDf/BzW/izZNfkKhPIhq1NvNouN/W5ea0bHiRz80reLwOaAhtxca7RdakBZe6ijqSGyG1ZwlijVsgEzEaqFGvv4dk5+VqsAMp0rLw/wTkgEJuFGM8FIVlFwImbF/VDLbg905rhwZ5FHp/jr+knixpVW1NQzSiYHjei9CxuyV2W2veSd1JQPvmU67jwvdorTomKf8usc9voIB8dEyKg6wWTtCFJEYlx5bRWLYMSNVGRwJG2wUrj4zcb5crMVMMvt+1SR0JQK2OpFgyTn+2axS00GAZGhlwyLpWMptkB/j3zve9pxIaHBiAZJm3YfDadVPDJVbj01/zsMVq laA1JDIq cwUZ0O858rT2MX7inI9RqCsmukqnS2odK2CDQoG+VRVODPA3xwJlr2yPS3mdH662UvGV9Bvy8tJQCxTkVAxXBDn6KjIKovCO7bo2/ALPn8XhkPOs+q70Dn+stwrWnbLo1DfvJfsW/zkFdguQyhJcdfIJIYfU0avmeuhxCht5VqBrbFYQh6Psrq4Yi1LcI/LYZWkBNsb2fvKFAY1rQzJFOus+Rsd409h55FNmiTQX4meVoI0z8hjwMnbsPxMfkIhnpYlzZaHsJuDoMJ80oOTjva0AJXP3oSLjMHvJYmUMxGgcyUFA1EybGg4+bvCwL6R5EsIMJ1zBCgLOVECz03y5KVcjercn/d9hVd0yKhYVkpA3vvIINl73HhpIMXB5FVW9yAaBj 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 scanning. 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 | 62 +++++++++++++++++++++++++++++++++++++++++---------- mm/mprotect.c | 3 ++- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index c30fb4b95e15..c6c216d9d95f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5055,17 +5055,51 @@ int numa_migrate_prep(struct folio *folio, struct vm_fault *vmf, } static void numa_rebuild_single_mapping(struct vm_fault *vmf, struct vm_area_struct *vma, + unsigned long fault_addr, pte_t *fault_pte, bool writable) { pte_t pte, old_pte; - old_pte = ptep_modify_prot_start(vma, vmf->address, vmf->pte); + old_pte = ptep_modify_prot_start(vma, fault_addr, fault_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); + ptep_modify_prot_commit(vma, fault_addr, fault_pte, old_pte, pte); + update_mmu_cache_range(vmf, vma, fault_addr, fault_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, bool pte_write_upgrade) +{ + 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; + unsigned long addr; + + /* Restore all PTEs' mapping of the large folio */ + for (addr = start; addr != end; start_ptep++, addr += PAGE_SIZE) { + 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; + } + + numa_rebuild_single_mapping(vmf, vma, addr, start_ptep, writable); + } } static vm_fault_t do_numa_page(struct vm_fault *vmf) @@ -5073,11 +5107,12 @@ 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; + bool pte_write_upgrade = vma_wants_manual_pte_write_upgrade(vma); 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 @@ -5099,7 +5134,7 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf) * is only valid while holding the PT lock. */ writable = pte_write(pte); - if (!writable && vma_wants_manual_pte_write_upgrade(vma) && + if (!writable && pte_write_upgrade && can_change_pte_writable(vma, vmf->address, pte)) writable = true; @@ -5107,10 +5142,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 +5161,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 +5178,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 +5199,19 @@ 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, + pte_write_upgrade); + else + numa_rebuild_single_mapping(vmf, vma, vmf->address, vmf->pte, + 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; /*