From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794190599; b=2eBImD0ODTrtA0JniM3r1gA09lpaQR0s7zogCrhypCSVbp1W4oZaOpV2UJwY9DKqW+zr 1sOBxQSCdoPr9man6cRsI/FWXr5I+0Dm76vOd3FieupfzKy6ZCSWfOk1IoeCTPSF9uni5 IrGFzUsLrdyiQ926dJ8b51ZtRVLwfA3UbYxokM9jlmz0GfylOn9IjH5Gad9JVbgv2shYt TQ23WMpLHVjuP6GgsMLuuMjzHjPXlPButePqprteat/EYouRKU7v1vSiiJjgy8HLBmZuw tnep2jnmXjME0aEh+F41Dvv0D4KmAVdFmKWKstbfhKY4viT78LM2NONzM1FCpZ52eRA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794190599; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=RweQvyw4NIgsuj4dNi7nK06pAit76GUtp9g8kGTqMg4=; b=x1YaslgB/SQX/sXrIV8E1eSDxKGHWUT6z9rw08YSBSm+iPl/lubuipewJlhQXGCMMVTa BkmOOp5+wbEoTbLSwMlYhV+4CjeuemI6bgVr0OcR5gLfqgRXI/vWGbY7ivVr01Y+4F5FA xZoUcCRgIpQzfaQJet+OIvxDTwxXL3SDHSJqwi+zDXaREXT7YylkU8PM01mLDDp7+o6so KgplYck/7ff1i8FCICVbWgfGlwVhgUUYwjKs8lXUApXseLz/96fLNVCSwMLdVsx6CLX+3 gbr0QK4EcegyCE/fx9ijf7ded6ClBVWe3vHcfsVlQqpAXjgzzqC+ZI/7cmz7UxfHMbg== 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=NFDf4TDr; arc=none smtp.remote-ip=91.218.175.200 Received: from mta0.migadu.com (out-200.mta0.migadu.com [91.218.175.200]) (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 036CC606A320 for ; Mon, 9 Nov 2026 02:16:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 036CC606A320 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=NFDf4TDr X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=0vqFp7/7WWiwnx+Zs/qlGOXEjprolacOHKPupS8KY24=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1794190597; v=1; x=1794795397; b=NFDf4TDrErNLl4AlkmuT4JnT5cGbRlHkE7spyQDd057yfU25J2+yZEjD/pm9Q9Re9XIoGFJ7 /dhV8eQb6xW4bTlmN/pH0jzJXtYuPNcQ/Yq+uvEM3TWG+1SZ+g/8h5GDLtpTIkAl28LRRZ9v7u/ 1fo5dm8vfWbISM5pXq+nR9+8= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta11.migadu.com with ESMTPS id 3ba9fe655f2ef269; Mon, 09 Nov 2026 02:16:36 +0000 X-Mizu-Trace-ID: 3ba9fe655f2ef269 X-Migadu-Flow: FLOW_OUT From: Hui Zhu To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Yu Zhao , Kairui Song , Barry Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hui Zhu , stable@vger.kernel.org Subject: [PATCH v3] mm/mglru: fix a no-op lru_gen_clear_refs() call in lru_lazyfree() Date: Mon, 9 Nov 2026 10:16:11 +0800 Message-ID: <20261109021611.22247-1-hui.zhu@linux.dev> X-Mailer: git-send-email 2.43.0 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-Transfer-Encoding: 8bit From: Hui Zhu lru_lazyfree() calls lru_gen_clear_refs() after lruvec_del_folio(), but lru_gen_del_folio() has already cleared LRU_GEN_MASK, so folio_lru_gen() returns -1 and lru_gen_clear_refs() exits at its "gen < 0" check without clearing the refs or PG_workingset. A MADV_FREE folio therefore keeps the state it had before madvise(2), even though madvise_free_pte_range() has already cleared the accessed and dirty bits of its PTEs. lru_gen_folio_seq() places it one generation younger, and PG_workingset puts it in the last tier, so sort_folio() promotes and protects it instead of evicting it and charges it to lrugen->protected[], which read_ctrl_pos() uses to balance anon against file reclaim. __folio_inc_gen() clears the refs but not PG_workingset, so this repeats every time the folio reaches the oldest generation. If the folio is redirtied first, ttu_anon_lazyfree_folio() keeps it and lru_gen_eviction() packs the stale state into the shadow entry, so a later refault is promoted to the youngest generation. Call lru_gen_clear_refs() while the folio is still on the generation list, as deactivate_file_folio() and folio_deactivate() already do. Its return value only says whether the folio is in the oldest generation, which lru_lazyfree() does not need because it re-adds the folio below. Keep folio_clear_referenced() unconditional: the classic LRU needs it, and it also covers the folios that lru_gen_clear_refs() skips. Fixes: cc8ec7be78ff ("mm/mglru: optimize deactivation") Cc: stable@vger.kernel.org Suggested-by: Barry Song Signed-off-by: Hui Zhu Reviewed-by: Barry Song --- Changelog: v3: Add Cc: stable@vger.kernel.org and pick up Barry's Reviewed-by. No code change compared to v2. Per the thread discussion, the __lru_gen_clear_refs() refactor will be sent as a separate cleanup after this fix lands. v2: Call lru_gen_clear_refs() before lruvec_del_folio() instead of open-coding the flag clears; drop the lru_gen_enabled() branch and the comment (Barry Song). mm/folio.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/mm/folio.c b/mm/folio.c index 35e242b48870b..ea3adb6a89d75 100644 --- a/mm/folio.c +++ b/mm/folio.c @@ -597,12 +597,10 @@ static void lru_lazyfree(struct lruvec *lruvec, struct folio *folio) folio_test_swapcache(folio) || folio_test_unevictable(folio)) return; + lru_gen_clear_refs(folio); lruvec_del_folio(lruvec, folio); folio_clear_active(folio); - if (lru_gen_enabled()) - lru_gen_clear_refs(folio); - else - folio_clear_referenced(folio); + folio_clear_referenced(folio); /* * Lazyfree folios are clean anonymous folios. They have * the swapbacked flag cleared, to distinguish them from normal -- 2.43.0