From patchwork Wed Dec 6 12:50:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Henry Huang X-Patchwork-Id: 13481760 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 077B1C10DC1 for ; Wed, 6 Dec 2023 12:50:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5932D6B0078; Wed, 6 Dec 2023 07:50:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5433D6B007B; Wed, 6 Dec 2023 07:50:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E4356B007D; Wed, 6 Dec 2023 07:50:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 326A86B0078 for ; Wed, 6 Dec 2023 07:50:59 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 140BFA0B1A for ; Wed, 6 Dec 2023 12:50:59 +0000 (UTC) X-FDA: 81536378238.27.87A28C6 Received: from out0-208.mail.aliyun.com (out0-208.mail.aliyun.com [140.205.0.208]) by imf30.hostedemail.com (Postfix) with ESMTP id 0E1F68001B for ; Wed, 6 Dec 2023 12:50:56 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of henry.hj@antgroup.com designates 140.205.0.208 as permitted sender) smtp.mailfrom=henry.hj@antgroup.com; dmarc=pass (policy=quarantine) header.from=antgroup.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701867057; a=rsa-sha256; cv=none; b=VzcVSczcFpDeYp8TcPpc0dwLayBfq+dxnF9wlJh0rRYDTWXsHg988nnRcXbVqhKkX6cnRN kf+XvcQWmKkpQ+HbYAkxEssh2mP3L2YhTb/HVa/RyT2azmONEoIwU1ZRaym0PshGTVQNki 7TvVbkwGWGhacz9ZH3fU/t3Z5s8utPg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; spf=pass (imf30.hostedemail.com: domain of henry.hj@antgroup.com designates 140.205.0.208 as permitted sender) smtp.mailfrom=henry.hj@antgroup.com; dmarc=pass (policy=quarantine) header.from=antgroup.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701867057; 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; bh=UzqS0RTA97gZxDftsG30se71/pBKc2rueNeEBdDuQEE=; b=yMu9Jn7nOeIFlUCm/3f6dzbttQkTqcd0p7BHHz9vVuvqY6scJk/QrBeSq3cxhULS1nZTAh Fa1w9OX+GAB6trYo/1K9c+tY6h16Sxqz0y3XnlnKo0tEqyvshtbBLp8yKtl7hSlVfBpI55 rJX5SfiDFV2c1KZ6DOBH0WzkjowEjFQ= X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R211e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047198;MF=henry.hj@antgroup.com;NM=1;PH=DS;RN=6;SR=0;TI=SMTPD_---.Ve5fmIH_1701867051; Received: from localhost(mailfrom:henry.hj@antgroup.com fp:SMTPD_---.Ve5fmIH_1701867051) by smtp.aliyun-inc.com; Wed, 06 Dec 2023 20:50:51 +0800 From: "Henry Huang" To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: " =?utf-8?b?6LCI6Ym06ZSL?= " , " =?utf-8?b?5pyx?= =?utf-8?b?6L6JKOiMtuawtCk=?= " , , "Henry Huang" Subject: [RFC v2] mm: Multi-Gen LRU: fix use mm/page_idle/bitmap Date: Wed, 06 Dec 2023 20:50:07 +0800 Message-ID: <951fb7edab535cf522def4f5f2613947ed7b7d28.1701853894.git.henry.hj@antgroup.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0E1F68001B X-Stat-Signature: 3asbsg7teudhsd8qdgami4qbh4zqodnf X-HE-Tag: 1701867056-441592 X-HE-Meta: U2FsdGVkX1/BnnZq/JOqj4OD+oVmME1iUO+gjdNamdjCLLRZhDPqSi6o1FqYQDYBOLftiQw3SQP5F04jjoASs10ezy9MIOyL8yYT/brnReM2rNAeHBBudnGoBupKaMRZGbGy5Am5MVZAxBu1KLX4hECnyDymCjL9HRfVEMtEgfSDMOgJoYLsCe+DdE9GiCKdzdNPM1s5jQI9fClATg6w25NJZj+qHT14fPNIeip/JL/Av5JZFoo6/ZDjH2vUKn6jGXTuZSyi/WC1kz/8dpMXO9tlKbRbLLjiQSaVCc8YlLj2cpVVNx3aKo9EdvWddl35Pc31PcN+XPRPPcZ3qpSac4FYiIGqVIdyuVIqyCpcnzoiDvpeY4CQrp/Sqpbz5l2t0b9UaaKKvFz1QPa9A1miSt63Kp6bIMtDI33P5zkBxGKrtKGFiL5D7qyJVZuEGlXrq7+DF7sMp2Qml2Xx5YzryuVKbuDqiQprYbROHGH5Mbzea8W2UZ2KC9DmlZZ+WCWYtoyohYI1ypomTKrY210myoOR8ux5ZDNmhIpXjpCpxtGbIGywkezR59+8QkaHz+Sg4gAc7XUBSQZOsIgC2xeypzBcHJ3ekPT5hlEXmaO7/Lg6f2VnQfxaUtpoB63cxfJK/hRmSZyMhAtHXkyosXixbIlazO1hpnHeLYHfeOUkjgpK5zbJV8FHP++6MSUS9HDh+kG4075n1x7eMm5StmbspHCGYG2sw8ro8J9ALdRAC9VoFksPzSMrBFjIitVDRa3sqqVr7EiUQ/qggSLCNaIB8SKOipaSxsrfNF7pYYt+QabsLsmesytb2i1GJXVducU/+BQw11TOgQRxtg6hA/2k3EV7CcbT88X1Qdj7ymRlbuLct8veyZA4WVO996uYlBqt1qrx1PJPbsdC2EznnZPv0Sqcl9C0OewAn8ojg1JKN/Ne5co3d7ETyEY3HELMtd71iuE4uFT/r4giFUz9uNo 4JcpZ5Cf QuACWxMY5+0ChLbE9AF08t/+Zv3cW35yM3Zn7YlRCfRgJLyS+qlRPOks7gvYr9Dii3c+4fFvpfzsyRN60JaKCEhgFT3b4E/KNtlsTrVYVRq88T2Cp4qbIt7NriaJk0JbfZ/OJr7V6YQXIQZOwnrCvgA8NNyzL+ZZUuaNxI9NW9NkTWSU= 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: Multi-Gen LRU page-table walker clears pte young flag, but it doesn't clear page idle flag. When we use /sys/kernel/mm/page_idle/bitmap to check whether one page is accessed, it would tell us this page is idle, but actually this page has been accessed. For those unmapped filecache pages, page idle flag would not been cleared in folio_mark_accessed if Multi-Gen LRU is enabled. So we couln't use /sys/kernel/mm/page_idle/bitmap to check whether a filecache page is read or written. What's more, /sys/kernel/mm/page_idle/bitmap also clears pte young flag. If one page is accessed, it would set page young flag. Multi-Gen LRU page-table walker should check both page&pte young flags. how-to-reproduce-problem idle_page_track a tools to track process accessed memory during a specific time usage idle_page_track $pid $time how-it-works 1. scan process vma from /proc/$pid/maps 2. vfn --> pfn from /proc/$pid/pagemap 3. write /sys/kernel/mm/page_idle/bitmap to mark phy page idle flag and clear pte young flag 4. sleep $time 5. read /sys/kernel/mm/page_idle/bitmap to test_and_clear pte young flag and return whether phy page is accessed test ---- test program #include #include #include #include #include #include #include int main(int argc, const char *argv[]) { char *buf = NULL; char pipe_info[4096]; int n; int fd = -1; buf = malloc(1024*1024*1024UL); memset(buf, 0, 1024*1024*1024UL); fd = open("access.pipe", O_RDONLY); if (fd < 0) goto out; while (1) { n = read(fd, pipe_info, sizeof(pipe_info)); if (!n) { sleep(1); continue; } else if (n < 0) { break; } memset(buf, 0, 1024*1024*1024UL); puts("finish access"); } out: if (fd >=0) close(fd); if (buf) free(buf); return 0; } prepare: mkfifo access.pipe ./test ps -ef | grep test root 4106 3148 8 06:47 pts/0 00:00:01 ./test We use /sys/kernel/debug/lru_gen to simulate mglru page-table scan. case 1: mglru walker break page_idle ./idle_page_track 4106 60 & sleep 5; echo 1 > access.pipe sleep 5; echo '+ 8 0 6 1 1' > /sys/kernel/debug/lru_gen the output of idle_page_track is: Est(s) Ref(MB) 64.822 1.00 only found 1MB were accessed during 64.822s, but actually 1024MB were accessed. case 2: page_idle break mglru walker echo 1 > access.pipe ./idle_page_track 4106 10 echo '+ 8 0 7 1 1' > /sys/kernel/debug/lru_gen lru gen status: memcg 8 /user.slice node 0 5 772458 1065 9735 6 737435 262244 72 7 538053 1184 632 8 59404 6422 0 almost pages should be in max_seq-1 queue, but actually not. Signed-off-by: Henry Huang --- mm/swap.c | 3 +++ mm/vmscan.c | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/mm/swap.c b/mm/swap.c index cd8f0150ba3a..4bd14aabdc10 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -416,6 +416,9 @@ static void folio_inc_refs(struct folio *folio) { unsigned long new_flags, old_flags = READ_ONCE(folio->flags); + if (folio_test_idle(folio)) + folio_clear_idle(folio); + if (folio_test_unevictable(folio)) return; diff --git a/mm/vmscan.c b/mm/vmscan.c index 96abaa5a973e..c1dd9e7bc315 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3297,7 +3297,7 @@ static unsigned long get_pmd_pfn(pmd_t pmd, struct vm_area_struct *vma, unsigned #endif static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg, - struct pglist_data *pgdat, bool can_swap) + struct pglist_data *pgdat, bool can_swap, bool clear_idle) { struct folio *folio; @@ -3306,6 +3306,10 @@ static struct folio *get_pfn_folio(unsigned long pfn, struct mem_cgroup *memcg, return NULL; folio = pfn_folio(pfn); + + if (clear_idle && folio_test_idle(folio)) + folio_clear_idle(folio); + if (folio_nid(folio) != pgdat->node_id) return NULL; @@ -3355,6 +3359,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, unsigned long pfn; struct folio *folio; pte_t ptent = ptep_get(pte + i); + bool is_pte_young; total++; walk->mm_stats[MM_LEAF_TOTAL]++; @@ -3363,16 +3368,20 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, if (pfn == -1) continue; - if (!pte_young(ptent)) { - walk->mm_stats[MM_LEAF_OLD]++; + is_pte_young = !!pte_young(ptent); + folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap, is_pte_young); + if (!folio) { + if (!is_pte_young) + walk->mm_stats[MM_LEAF_OLD]++; continue; } - folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); - if (!folio) + if (!folio_test_clear_young(folio) && !is_pte_young) { + walk->mm_stats[MM_LEAF_OLD]++; continue; + } - if (!ptep_test_and_clear_young(args->vma, addr, pte + i)) + if (is_pte_young && !ptep_test_and_clear_young(args->vma, addr, pte + i)) VM_WARN_ON_ONCE(true); young++; @@ -3435,6 +3444,7 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area do { unsigned long pfn; struct folio *folio; + bool is_pmd_young; /* don't round down the first address */ addr = i ? (*first & PMD_MASK) + i * PMD_SIZE : *first; @@ -3449,11 +3459,15 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area goto next; } - folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap); + is_pmd_young = !!pmd_young(pmd[i]); + folio = get_pfn_folio(pfn, memcg, pgdat, walk->can_swap, is_pmd_young); if (!folio) goto next; - if (!pmdp_test_and_clear_young(vma, addr, pmd + i)) + if (is_pmd_young && !pmdp_test_and_clear_young(vma, addr, pmd + i)) + VM_WARN_ON_ONCE(true); + + if (!folio_test_clear_young(folio) && !is_pmd_young) goto next; walk->mm_stats[MM_LEAF_YOUNG]++; @@ -4025,19 +4039,21 @@ void lru_gen_look_around(struct page_vma_mapped_walk *pvmw) for (i = 0, addr = start; addr != end; i++, addr += PAGE_SIZE) { unsigned long pfn; pte_t ptent = ptep_get(pte + i); + bool is_pte_young; pfn = get_pte_pfn(ptent, pvmw->vma, addr); if (pfn == -1) continue; - if (!pte_young(ptent)) + is_pte_young = !!pte_young(ptent); + folio = get_pfn_folio(pfn, memcg, pgdat, can_swap, is_pte_young); + if (!folio) continue; - folio = get_pfn_folio(pfn, memcg, pgdat, can_swap); - if (!folio) + if (!folio_test_clear_young(folio) && !is_pte_young) continue; - if (!ptep_test_and_clear_young(pvmw->vma, addr, pte + i)) + if (is_pte_young && !ptep_test_and_clear_young(pvmw->vma, addr, pte + i)) VM_WARN_ON_ONCE(true); young++;