From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.209 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796557171; b=mv2/pmpoWp6U14D7XZGXGtnpDFzFXJmf6avw5kXeTw7FHpmApLAHwQSlw4L3GUVQDNdC vUkf9bT85PRRd36Af3bFXlQ/j22u9W8Rwh1z2cnIRiyqzjGkFqr09tzU3AP9NyVOB0WVu Mue9tQJXYGJTbLuDIszQiGxC3Ptqr+UmdA1fzxyKt25oEDHpn89JeeFIGdMy0RUQtNtdu nHEm/qmRRGnsyejz8A00B+YH+jVzOq7po08STivU+dij/jrUWmhX/AbyB0dEST1EIO4rR 5dnTaZuDj3/TpsYxs9HOSLw9VRCL8pnPwSWko+5HQpVpjw2TbV3lKE3LNjqYa3iM4dw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796557171; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer; bh=/XgrNherqBcrdmGrrzhT0qcPd9K58FB+Z6L6wSVOYrA=; b=AN54PyECs3ZlCwpGnLbijf9k3irIDtSxm1PuLd3s8c51t08OFuvlOopYU+8NuwyQkXe6 KdbWJyNOFyAKikJLQUn+YoVHYyz13kZmdRO67jcE7JAW6uj8w1/AdygtYg2qzWzPiTQff qSq7OW3qCybHKe5rbAUVGUBz5n6z2a5ZYTCytL1Zxu6e5m8B6Ei4NXetyEL+Wjg+5qtql g+RsEWr2NdlO3a3hnV6tvHjVSiONat2CCfV60F3gNuU3/oc+lpjACsc3MKWC9Jqj+opHH qwN7mOSKeBCgJ6rAOxm/82jVNBR2m7ZPcg7b9NalgvlUSc+bJsz3xOix/KGIRpwj7Iw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=DtkBSzS6; arc=none smtp.remote-ip=91.218.175.209 Received: from mta0.migadu.com (out-209.mta0.migadu.com [91.218.175.209]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A7F87606A683 for ; Sun, 6 Dec 2026 11:39:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A7F87606A683 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=DtkBSzS6 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=7EKFTD7TGP6MOhPo8XLzswKHm7hwxul7GJi593tyZz4=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796557167; v=1; x=1797161967; b=DtkBSzS6A7l2HEzxahBqRbGXg0OQFXWzJOpISfHXledEMxZvSiKrzd3Au2ptkr3YF2UhZiaV yErVCQhOXgbch+ktTMNEOfuZKrsowQZ2UnuLCDvPymnu9Fdx/JbF4dKq/MyNnXkgExUF82+B+Md lm4tkxOCtTOAFOwPug3cMq8k= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 658ef1bb67717916; Sun, 06 Dec 2026 11:39:27 +0000 X-Mizu-Trace-ID: 658ef1bb67717916 X-Migadu-Flow: FLOW_OUT From: Qiliang Yuan Date: Sun, 06 Dec 2026 06:38:41 -0500 Subject: [PATCH 4/8] mm/mempolicy: consult secondary MMUs when sampling accessed bits X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261206-feat-numab-series-v1-4-2af21ad24718@linux.dev> References: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev> In-Reply-To: <20261206-feat-numab-series-v1-0-2af21ad24718@linux.dev> To: Peter Zijlstra , Ingo Molnar , Juri Lelli , Vincent Guittot , Mel Gorman , K Prateek Nayak , David Hildenbrand , Andrew Morton Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Valentin Schneider , Zi Yan , Gregory Price , Ying Huang , Kefeng Wang , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qiliang Yuan X-Mailer: b4 0.13.0 Accesses made through a secondary MMU do not set the accessed bit of the primary page table. A KVM guest reading its hugetlb backed memory sets the bit in the stage-2 or nested page table only. Sampling the primary PTEs of the VMM therefore finds that memory idle, apart from the bits left behind once when it was populated, and the vCPU threads get no lasting placement signal from it. Test the accessed bit of secondary MMUs too, with mmu_notifier_test_young(), and count the page as accessed if either is set. mmu_notifier_test_young() takes one address, so for a huge mapping only its first base page is tested, and a secondary MMU that maps it with smaller entries is undersampled. Testing is enough to tell where the guest memory lives, and clearing the bit with mmu_notifier_clear_young() instead is costly when the L1 memory itself is not backed by huge pages: with such an L1, copy rounds of about 285 ms in an L2 guest with 6 GiB of hugetlb memory took 406 to 841 ms every 2.5 to 4.8 s, and none did with testing alone. With huge pages behind the L1, both give the same placement and copy times: 133 against 134 ms with the whole series in the test below. An L2 guest whose 6 GiB of hugetlb memory are split between node 0 and node 4 of the L1 VM used for the previous patches copies within 2 GiB of the node 4 part, its vCPU started on node 0; 3 rounds of 2 runs of 60 s alternating with the kernel without this patch, median copy time over the second half of each run. Without the patch the accessed bits left by populating the guest memory are found once, on both nodes alike, and the vCPU never stays on node 4. With it the vCPU moves there after 20 to 27 s in every run and stays: before after change runs ending on node 4 0/6 6/6 copy 2 GiB (ms) 205 (203-207) 134 (132-134) -35% Signed-off-by: Qiliang Yuan --- mm/mempolicy.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index e69a4b38b136e..f4bb2a980072e 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -961,6 +961,7 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr, pte_t *start_pte, *pte, ptent; struct folio *folio; spinlock_t *ptl; + bool young; ptl = pmd_trans_huge_lock(pmd, vma); if (ptl) { @@ -968,10 +969,13 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr, folio = pmd_present(pmdval) ? vm_normal_folio_pmd(vma, addr, pmdval) : NULL; - if (numa_sample_folio(sw, folio) && - pmdp_test_and_clear_young(vma, addr, pmd)) { - sw->node_pages[folio_nid(folio)] += HPAGE_PMD_NR; - sw->nr_young += HPAGE_PMD_NR; + if (numa_sample_folio(sw, folio)) { + young = pmdp_test_and_clear_young(vma, addr, pmd); + young |= mmu_notifier_test_young(walk->mm, addr); + if (young) { + sw->node_pages[folio_nid(folio)] += HPAGE_PMD_NR; + sw->nr_young += HPAGE_PMD_NR; + } } spin_unlock(ptl); return 0; @@ -991,7 +995,9 @@ static int numa_sample_pmd_entry(pmd_t *pmd, unsigned long addr, if (!numa_sample_folio(sw, folio)) continue; - if (ptep_test_and_clear_young(vma, addr, pte)) { + young = ptep_test_and_clear_young(vma, addr, pte); + young |= mmu_notifier_test_young(walk->mm, addr); + if (young) { sw->node_pages[folio_nid(folio)]++; sw->nr_young++; } @@ -1011,17 +1017,26 @@ static int numa_sample_hugetlb_entry(pte_t *pte, unsigned long hmask, struct numa_sample_walk *sw = walk->private; unsigned long psize = huge_page_size(h); spinlock_t *ptl; + bool young; pte_t entry; int nid; ptl = huge_pte_lock(h, walk->mm, pte); entry = huge_ptep_get(walk->mm, addr, pte); - if (pte_present(entry) && pte_young(entry)) { + if (pte_present(entry)) { nid = folio_nid(pfn_folio(pte_pfn(entry))); - entry = huge_ptep_get_and_clear(walk->mm, addr, pte, psize); - set_huge_pte_at(walk->mm, addr, pte, pte_mkold(entry), psize); - sw->node_pages[nid] += pages_per_huge_page(h); - sw->nr_young += pages_per_huge_page(h); + young = pte_young(entry); + if (young) { + entry = huge_ptep_get_and_clear(walk->mm, addr, pte, + psize); + set_huge_pte_at(walk->mm, addr, pte, pte_mkold(entry), + psize); + } + young |= mmu_notifier_test_young(walk->mm, addr); + if (young) { + sw->node_pages[nid] += pages_per_huge_page(h); + sw->nr_young += pages_per_huge_page(h); + } } spin_unlock(ptl); @@ -1046,8 +1061,11 @@ static const struct mm_walk_ops numa_sample_ops = { * @node_pages: Per-node counters, indexed by node id. * @all: Sample every folio instead of pinned folios only. * - * Test and clear the accessed bit of hugetlb pages and of pinned folios and - * add the pages found accessed to @node_pages for the node they reside on. + * Test and clear the accessed bit of hugetlb pages and of pinned folios, test + * the accessed bit kept by secondary MMUs, and add the pages found accessed to + * @node_pages for the node they reside on. mmu_notifier_test_young() takes a + * single address, so a secondary MMU is asked about the first base page of a + * huge mapping only; one that maps it with smaller entries is undersampled. * * Return: The number of pages found accessed. */ -- 2.43.0