From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796156388; b=vqtDw4XkUE4NrvR09t4gkMzun9OQbHU1qsmGX8o2LyAB2ykuBVc7ZT/jVKZr0AxvWy+A UzdLJUcvl2aFAFEMXzsQhiX7FkNu6+RDW9Mh/qYsFKTJO5ha4tZhmJvnaqvP9deIffXnv 6Rp6DiiM9UthBuviT3+Hjki47H+1g36aBP+hc8sR4/p12X95WdtGZZtHFS30Tj61CD458 uSFN1TSZ9kc+tGTp9kglTyL1UYUso8dVjTwwgwe1ln1lgMqhZ8V2ufoF0O0Mrkk+TDaPT wFSFEoLiFUfxMB9By6+R3ccRVNL7LfopYiHNBs1AMcb1ab3sxiSebgsjk122Y3Uj1Tg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796156388; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=kOjccge26rcBtMheu3COJ+A8gBAHHLlutX8+XkDJRlQ=; b=rqLOLqoKfhYo9cQMSNK1ebxeO+J98K4h53+Vn6IKLmPx0luV5Bh+89GX/sARGnnyKydY 3QNh6fciC1z1ATFU4p1GJgzVsTz0dlRmN8z5CACbx9hkpfIgNIqUvSu4uNB8umZSiAVbl ziUkHZouaBku9LUPVUqaJ2Zou1Ndl2t3gSOMDFAvG1Q9S9D0OXYawxdZz79stJI+o+wtI O/NxMQw3vmjA07YOuj3aLUErUZqOSpWwVc8ZMKp7apM54kAOxCA7jycAGGZmapOvj+j9D 3Qrj9HHJgnApXETIfEwZzYBol3vKCMzMWu54jINL4HQJ5Y+wRvTGvNXheleog952GeQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Q2yMUhZR; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23035606A6B4; Tue, 1 Dec 2026 20:19:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Q2yMUhZR Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796156387; b=OFIe9x+N6ykZdp1Q5Sblx5osCF44p9ZJWL0rZAzwYDDq2EEMNxzIyDchfYuzfgKTDi7Q kLlTRL2/Y2S6DwbSQecHijEjkjnyAeZxZ/w0G45kFBdxBwHIX2ym8EAn+/1zAR8PMYVWK QXctguoZSXcRCFt0SNib6lHGUj9xy+x7K3ikPqS7T5xlEFom4nwRJMSiVJ0v7VRRSzOjH /UANwp8hbLItZ+Z+uB3Okkt/SY21ssIeLNc75OtRvEzH564cf4HNzLo43sn6/zrU0T4re urlc/vIiEZl457o9pZs5rUHXEyY77UH74T61s7Vu1qZP5Q/GpTERGNWFwOLlgMH66GQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796156387; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=kOjccge26rcBtMheu3COJ+A8gBAHHLlutX8+XkDJRlQ=; b=MQWWebiMylm/+QZfFHHo0OGQ1tnsgrGtTo1n65HtI93/zupGkdMGbCNM8Y7eovbk13mk Pn0PS+KnKsYVaeXiZqMZKjievRXoaTtsYL69LZC+XfJwl9ZQRkivluL3SMyC/OxwhF3A2 at6/Wr5gMymvDHxinOmNhUFKic09/2qMWAux9/hg13FlDF2h5sBFrg/rKjnzg6fte2Uj5 lpw+Irpj7RoD+B8FC28gUt3BiCKEXBpBh8bqnKNftyjj9avL6c3G2AxTES3gDABUfQi6b nk3qumLwzBIOfJcHZfmF+1ooozPLWm8U7uW1XRKBpAO+wH+0x/FVRsJ/ZxU0oovwwow== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 894AF1F0092A; Tue, 1 Dec 2026 20:19:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796156387; bh=kOjccge26rcBtMheu3COJ+A8gBAHHLlutX8+XkDJRlQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=Q2yMUhZRcZIITaDnyFCI3V8Pw6qDQur02tHxCtCJxThLlFd4nzM5R3g8omJLIczrL ZDYSTSi1tDklMKC3fQlsr98m8ccL/6e5G/WMLIrLdgce9nS5pYQYvsUboJL3opXx2y 6IULdT0c7ZF9ISsL3tW+yndUZTtykExDZK5mUGWyUw5wQrWdLe+7dLd2JDMXtZ7FhH K9W+IzoSbBk3WvKeLbD8bBa2aENTLa0FaqpJNHg0OxpBiaYkA9s81Tyw9lxuZzMLSz HoU+CFzW5rWOMt9MIiuW0HFIyVlF5DFYoOCmfDzv03L2e+SUK4QiB6tStJqss5cZ/x I0ObCg7aIP/HA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id EBB8E4F0BF; Tue, 1 Dec 2026 20:19:21 +0000 (UTC) From: Kairui Song via B4 Relay Date: Wed, 02 Dec 2026 04:18:51 +0800 Subject: [PATCH v7 11/19] mm/mglru, smaps: convert to LRU refs based operations for MGLRU 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" Message-Id: <20261202-mglru-fg-v7-11-5965a96d86d0@tencent.com> References: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> In-Reply-To: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> To: linux-mm@kvack.org Cc: Andrew Morton , Johannes Weiner , Muchun Song , Qi Zheng , Ying Huang , Chris Li , Baoquan He , Nico Pache , Usama Arif , Michal Hocko , Roman Gushchin , Shakeel Butt , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Vlastimil Babka , Suren Baghdasaryan , Nhat Pham , Youngjun Park , Zi Yan , Gregory Price , "Matthew Wilcox (Oracle)" , Baolin Wang , Ryan Roberts , Dev Jain , Lance Yang , Hugh Dickins , SJ Park , David Rientjes , Yu Zhao , Vernon Yang , Zicheng Wang , Tal Zussman , Kairui Song , Xiang Liu , Ehab Ababneh , Zhao Zhenzhuo , Lian Wang , Joanne Koong , Kunwu Chan , Hui Zhu , Maxim Muzafarov , Tianyi Chen , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, damon@lists.linux.dev, linux-doc@vger.kernel.org, Kairui Song , Ridong Chen X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796156357; l=4223; i=kasong@tencent.com; s=kasong-sign-tencent; h=from:subject:message-id; bh=ncJn+owZT6B+rHb6Ri0WytLZu7qbTAsFNOaDBndj/b4=; b=sZv/ugL2gIgnTuAhB3XPzQGBhnuvegO3g9l4T9LkSZ1LcJvT4hLfNuS1ybdyJCek/Q0wJt7w4 Jhf9ZAo6kjdBzu5i5ljdv7ej22tD7FGZAbrtOxyyEurALYe//pIO2Im X-Developer-Key: i=kasong@tencent.com; a=ed25519; pk=kCdoBuwrYph+KrkJnrr7Sm1pwwhGDdZKcKrqiK8Y1mI= X-Endpoint-Received: by B4 Relay for kasong@tencent.com/kasong-sign-tencent with auth_id=562 X-Original-From: Kairui Song Reply-To: kasong@tencent.com Content-Transfer-Encoding: 7bit From: Kairui Song PG_referenced is only the lowest bit of the folio LRU refs count (LRU_REFS_FLAGS) now, and MGLRU never kept it stable: promotion used to clear it, and now it toggles as the count grows. But with the LRU referenced count as a stable API now, we can use two helpers to provide in-kernel users a stable and sane reading of the referenced status of an LRU managed folio. Add folio_test_lru_refs() and folio_clear_lru_refs(), which cover the PG_young page-idle hint and use the refs count when MGLRU is enabled and PG_referenced otherwise, and switch the smaps paths over to them. Convert smaps as the first user of them. MGLRU now reports every folio with a nonzero count, so the reading no longer flickers and clearing no longer results in an ambiguous status. The classical LRU case is unchanged, and its internals keep using the raw PG_referenced helpers. Reviewed-by: Kunwu Chan Tested-by: Andrew Morton Acked-by: Zi Yan Signed-off-by: Kairui Song --- fs/proc/task_mmu.c | 8 +++----- include/linux/mm_inline.h | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 0d1c941790b0..44a347bc65d2 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -890,7 +890,7 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page, mss->resident += size; /* Accumulate the size in pages that have been accessed. */ - if (young || folio_test_young(folio) || folio_test_referenced(folio)) + if (young || folio_test_lru_refs(folio)) mss->referenced += size; /* @@ -1693,8 +1693,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, /* Clear accessed and referenced bits. */ pmdp_test_and_clear_young(vma, addr, pmd); - folio_test_clear_young(folio); - folio_clear_referenced(folio); + folio_clear_lru_refs(folio); out: spin_unlock(ptl); return 0; @@ -1722,8 +1721,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr, /* Clear accessed and referenced bits. */ ptep_test_and_clear_young(vma, addr, pte); - folio_test_clear_young(folio); - folio_clear_referenced(folio); + folio_clear_lru_refs(folio); } pte_unmap_unlock(pte - 1, ptl); cond_resched(); diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index dde3fc6e7c0c..d3f0c29ac340 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -9,6 +9,7 @@ #include #include #include +#include /** * folio_is_file_lru - Should the folio be on a file LRU or anon LRU? @@ -524,6 +525,40 @@ static __always_inline void folio_inc_lru_refs_fast(struct folio *folio) } while (!try_cmpxchg(folio_flags(folio, 0), &old_flags, new_flags)); } +/** + * folio_test_lru_refs - Test if an LRU managed folio has been referenced. + * @folio: The folio to test. + * + * Returns true if the MGLRU refs count is nonzero, or PG_referenced is set, + * which is also correct for the non-MGLRU case, or if PG_young is set. Kept + * as a separate helper, to avoid splattering the lru_gen_enabled() static key. + */ +static inline bool folio_test_lru_refs(const struct folio *folio) +{ + if (folio_test_young(folio)) + return true; + if (lru_gen_enabled()) + return folio_lru_refs(folio) > 0; + return folio_test_referenced(folio); +} + +/** + * folio_clear_lru_refs - Clear the referenced state of an LRU managed folio. + * @folio: The folio to clear. + * + * Reset the MGLRU refs count to zero, or clear PG_referenced, leaving + * PG_workingset alone for the non-MGLRU case, and clear PG_young. Kept as a + * separate helper just like folio_test_lru_refs(). + */ +static inline void folio_clear_lru_refs(struct folio *folio) +{ + folio_test_clear_young(folio); + if (lru_gen_enabled()) + folio_set_lru_refs(folio, 0); + else + folio_clear_referenced(folio); +} + static __always_inline void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) { -- 2.56.1