From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A380D1C68F for ; Tue, 23 Jun 2026 23:17:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782256621; cv=none; b=dBL8kxgamdzozAUOvIoqO5S3fk3NIfZ3e+PB9njhc1dyX6hkZPdLfJWL3FNmiiTbLUxjOijaZ+ZRBF6LVcOsFwUc1hIxMafbTxWQ/1fEYAPj0djCx5PDT8KmL1cWSYXcsQODJK1I9Tu7LJ5bDVSyg/ZOfl0bI5RpsRQhy2VGABE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782256621; c=relaxed/simple; bh=T3nVhyp24eaDfbxEfQyaUTmYoGoF52SKCvSLUtz65O8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SR+lVVcLHoFe05jSRexwNKQkIC5qCDd6ML1Es7EtPKgrEtuTyE9NYvFu3oXWj7jQwEcwg7YD1pltUfzsEEQd0tOWwveBzu++r6jGyD/kIs3V1C7YZDPx2g7sOg+5yIQVLzQH2trZy7PUw2UU8ite9BREsVyASByherE9guiJwOs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VBxP9Nbr; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VBxP9Nbr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF1891F00A3D; Tue, 23 Jun 2026 23:16:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782256620; bh=wfS2cq98HLmpUobLI+w2iB53VWLqU5OG3MSQtlkIjFo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=VBxP9NbrpfckDpEk2swimjW/OsC7MAhf/P0x5VGDHqen1jj3IVmCGvAH60i5FkDq7 z/8e0g0PYDyIhwe8jw4BtvcMQQF41Sc2vdXH15miWTlGng97Au3n0SjTQPDyF5rXDY Fb673rEDbIlq4KuqgbkAmTzpXFhHWohHQo7rh9SRL1LalbacniFG80OBSAKFURo87P Qtp0Tmr8RONXDwZb28MGk2fnylWa6YegUIgJcR/OM8Cf+FKniCIwMqJcl8vazUgvSO jLYbtf+BElr8zN4yYgbooVA2TXFMoxZVJMpdWEUV5Wi6+ZYN5kIA7M9Oa9HqXUjyth 8J/yQbK9oPFXg== From: "Barry Song (Xiaomi)" To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baoquan.he@linux.dev, chrisl@kernel.org, david@kernel.org, jp.kobryn@linux.dev, kasong@tencent.com, liam@infradead.org, linux-kernel@vger.kernel.org, ljs@kernel.org, mhocko@suse.com, nphamcs@gmail.com, rppt@kernel.org, shakeel.butt@linux.dev, shikemeng@huaweicloud.com, surenb@google.com, usama.arif@linux.dev, vbabka@kernel.org, youngjun.park@lge.com, "Barry Song (Xiaomi)" , Kairui Song Subject: [PATCH v2 4/4] mm: try to free swapcache for non-LRU folios Date: Wed, 24 Jun 2026 07:16:35 +0800 Message-Id: <20260623231635.43086-5-baohua@kernel.org> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260623231635.43086-1-baohua@kernel.org> References: <20260623231635.43086-1-baohua@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Originally, we unconditionally called lru_add_drain() for write swap-in page faults. This might drop the reference held by the per-CPU LRU cache if the folio happened to reside there. However, there was no guarantee that the folio was actually cached on the current CPU. Now that lru_add_drain() has been removed, we have lost one opportunity to drop a reference held by the LRU cache. We could instead incorporate that possibility into the condition evaluated by should_try_to_free_swap(). Suggested-by: Kairui Song Signed-off-by: Barry Song (Xiaomi) --- mm/memory.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index 2983a6baf474..14577c67c61a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5087,8 +5087,11 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) * Remove the swap entry and conditionally try to free up the swapcache. * Do it after mapping, so raced page faults will likely see the folio * in swap cache and wait on the folio lock. + * Assume non-LRU folios may be queued in the LRU cache, which contributes + * an additional reference to the folio. */ - if (should_try_to_free_swap(si, folio, vma, nr_pages, vmf->flags)) + if (should_try_to_free_swap(si, folio, vma, nr_pages + + !folio_test_lru(folio), vmf->flags)) folio_free_swap(folio); folio_unlock(folio); -- 2.39.3 (Apple Git-146)