From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796157329; b=vD3tjvvYEq1Hi8kqrSTcDWAb9+dp95tClWDC1UZ7ijrlpGpAx3CJMGB7TqdSeZxwWFnF pIPl8w+8aQ31dc1/epgwVxVbkxR0na0VJSLXOaK5nM8k4T+t9U4VI4KIgojZlD2PAGxSU jivQugoKHVlfjSclB5JG6/YrU8iMoA0j8Iqo53fxtHz9lC98Vd36P619Qk7nEVGkzIm4T YXEIOkhzXrDsVjO6w9yZp0Kic3FPurKAmi/lFx1k3dlFeYP9AnrTLbKJUp6mRsYHky0oF any1JUyd7fdAQN2Tv7MO7/+qw9woEO6zgzkFVf8PgoFWai5UnkD7c1pR2Tm4SRIktTA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796157329; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=2NvDflHy5Eatozczql3pPkyNv1cVzpXU3Ei1dEq9jmU=; b=Sjq9NPRH23w2JjaDV27KrYU2g6T3eZI7M8hJ6XgfIMObgP7aeU2Hg33+DJIdGDrkE7Oz rhN9W4OCDgSPlsm7VHFuByb8UOC5WRhZcphiUHAyswCtVm9RBQeBiaK6nGg7n1i0CKvHH hpNIpdSZ27wxWGa/9FnKKrbHNRv1eF6EYI9P8FPRH/jKoQ0Q1ZoihLOG1GNTbl/T7skc/ nVmXQk4MICGnmyvOcETmQRGERGRVXWKPQrxzFzIfxg7zXGo0+ARFOsZUDANbbKzbN1CZn NHlskpYLe1WUjajRhkc56ytpFrYploCOc9fM7Oa7RJHzBHamBbNlcEe2zM4q6uGKSJw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UVuzE61J; arc=none 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 193C4606A31A for ; Tue, 1 Dec 2026 20:35:29 +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=UVuzE61J Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8A3721F000C1; Tue, 1 Dec 2026 20:35:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796157328; bh=2NvDflHy5Eatozczql3pPkyNv1cVzpXU3Ei1dEq9jmU=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=UVuzE61J51BotqD2EILYuwFRzMv2Lt+8b8IOoAtmeYPfWEgy0WviAH3qTKq3oM5bg ct28gGcmMqmXND93E/842zki2s53L3pWqrUQ8ce+tRD7+T1Eif7NUWrCu/MU5Woe8S s9T6HCMoPknjNbe+CITkAPGNPLR6ZtTTHkD13b08aFRWv/tjp4hlI7tLyk7hHMVq8D dbWGXpQQ5JDKstFF4e6jGvH5c82oSbQAO/20EE6bRZrrhojQCr4gVR8blaGI1yuhiU wqUxuXJdpcMWsrxV7ITAS37uBUh3/+3np1QCxO7nRZuSsxQW5gzaWpt/Sb3Vps7Ewq wxmKSGJtGkb3Q== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v7 17/19] mm/mglru: make PG_workingset helpers work based on folio LRU refs Reply-To: sashiko-reviews@lists.linux.dev To: "Kairui Song" , 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" , =?utf-8?b?TWF0dGhldyBXaWxjb3ggKE9yYWNsZSk=?= , "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" , damon@lists.linux.dev, "Ridong Chen" , tj@kernel.org, mkoutny@suse.com In-Reply-To: <20261202-mglru-fg-v7-17-5965a96d86d0@tencent.com> References: <20261202-mglru-fg-v7-0-5965a96d86d0@tencent.com> <20261202-mglru-fg-v7-17-5965a96d86d0@tencent.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 20:35:26 +0000 X-Mailing-List: damon@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/mglru: race in madvise_cold_prep_folio() calling folio_set_workingset() -- commit e2fb2c7a825775390cc63d4b84ea016b798c9ab7 Author: Kairui Song mm/mglru: make PG_workingset helpers work based on folio LRU refs This commit replaces the page-flag generated workingset helpers with inline helpers in mm_inline.h so folio_test_workingset() checks the folio LRU refs count and folio_set_workingset() warns if called on a folio on an MGLRU generation list. > diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h > index 65bf16503b8bd..4dac237f5f57a 100644 > --- a/include/linux/mm_inline.h > +++ b/include/linux/mm_inline.h [ ... ] > @@ -559,6 +574,21 @@ static inline void folio_clear_lru_refs(struct folio= *folio) > folio_clear_referenced(folio); > } > =20 > +/* > + * For the classical LRU or isolated folios only: with MGLRU the PG_work= ingset > + * bit is part of the folio refs count maintained by folio_inc_lru_refs(= ), > + * a raw set would corrupt it. The warning below catches folios still o= n a > + * gen list, the classical paths in the switching window only see off-li= st ones. > + */ > +static __always_inline void folio_set_workingset(struct folio *folio) > +{ > + unsigned long flags; > + > + flags =3D set_mask_bits(folio_flags(folio, 0), BIT(PG_workingset), > + BIT(PG_workingset)); > + VM_WARN_ON_ONCE(lru_get_gen_flags(flags) !=3D -1); > +} > + > static __always_inline > void lruvec_add_folio(struct lruvec *lruvec, struct folio *folio) > { [Severity: Medium] Can folio_set_workingset() race with lru_gen_change_state(true) when called from madvise_cold_prep_folio(), corrupting the MGLRU refs count and triggering this warning? Unlike shrink_active_list() and workingset_refault(), which operate on isolated folios, madvise_cold_or_pageout_pte_range() calls madvise_cold_prep_folio() on a mapped on-LRU folio while holding only the page table lock: mm/madvise.c:madvise_cold_prep_folio() { if (!lru_gen_enabled()) { folio_clear_lru_refs(folio); if (folio_test_active(folio)) folio_set_workingset(folio); } else { ... } If MGLRU is enabled concurrently via /sys/kernel/mm/lru_gen/enabled, fill_evictable() can move the folio onto a generation list between folio_test_active() and folio_set_workingset(): CPU0 CPU1 madvise_cold_prep_folio(folio) !lru_gen_enabled() -> true folio_clear_lru_refs(folio) folio_test_active(folio) -> true lru_gen_change_state(true) static_branch_enable(&lru_gen_caps[= ...]) fill_evictable(lruvec) lru_gen_add_folio(lruvec, folio, = ...) /* clears PG_active, sets gen */ folio_set_workingset(folio) set_mask_bits(..., BIT(PG_workingset)) VM_WARN_ON_ONCE(gen !=3D -1) <-- warns and mutates MGLRU refs on-list Because set_mask_bits() unconditionally sets BIT(PG_workingset), it modifies bit 1 of the MGLRU refs encoding while the folio is on a generation list. With the per-folio active/inactive accounting in commit "mm/mglru: folio LRU refs based active/inactive number accounting", if the folio reached refs =3D=3D 1 before folio_set_workingset() ran, setting BIT(PG_workingset) bumps refs to 3 (LRU_REFS_ACTIVATED) without updating the active/inactive LRU size counters, causing permanent NR_ACTIVE and NR_INACTIVE counter drift. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202-mglru-fg-v= 7-0-5965a96d86d0@tencent.com?part=3D17