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 D3BBC37756A for ; Thu, 2 Jul 2026 00:00:06 +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=1782950407; cv=none; b=R8CJxrCoaG0DM6pygayLkGdC1LshrOb2y1O+KZtKC80pUJ+LfevHUKYUhYbi3UaKsTHsznw0XTSpOukcMUFiU/HZ42XxqscFTr0m/OgJzlQ0TLWRFNUl6GKl0QY1FfjPcwLGc8gZD7BfhN1aRsGwcMh2zlKf7Kqm9bVTnlAEXxU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782950407; c=relaxed/simple; bh=jhE3ffYHx9lz2pubZKFR7Jbw6qyx92R2hzsfYj0mWRc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZuMmgm5qy0kMrn1QsvFbNlLK4UR5OgKPVJDqz/KG/Ya5A01NQQbB5j7/VgxKEXWnJZXIMUNxf57ZyDjIwG09Av+3VUMQptG+SUsfMenZP5al1oo7PmRAdKMHZ2Stx6grxl+iMgI+mamaNOmSLBHk9HzxTCNNW/u0Kxrt04aonhY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=h03HHPGv; 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="h03HHPGv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 646051F00A3A; Thu, 2 Jul 2026 00:00:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782950406; bh=EUmZCIeSXjjFquF3kdezm47YdK5Oit4OnLrV5UYTfAA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=h03HHPGve7bQuVc3Ho95s14S9BPXhSq1RKv+ZsmG0JbQRrMrTMLbgdYsrmRAjocUW 9deVZQuAVaUPG4JfLFMkrwrOk693c6mUnubgnaxz+TTMkUoFcJUrHdjsm/5xAFc0oR /YkZMBTK6MeIuUPEwdvQyJk7nMTsy0X0/uQk22u1tF6omD5rwpJm9xR8xRtdG3ibra /dGjUmzSKm3cKPFXKdeg1OFO15DcdSaCg/2lKJH2Pgd4eY8oH/nsrcOFf+PpnoP3/P +SoeONAmmj/LcKTU5SNmM6lwvP5n/zAkJPk6DVI1jIV5nvLNuiUzh/twT67XgEZT1u xxgXMyMd5kXFw== 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)" Subject: [PATCH v3 1/4] mm: avoid unnecessary lru drain for wp_can_reuse_anon_folio() Date: Thu, 2 Jul 2026 07:59:52 +0800 Message-Id: <20260701235955.36126-2-baohua@kernel.org> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260701235955.36126-1-baohua@kernel.org> References: <20260701235955.36126-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 There is a case where `folio_ref_count(folio) == 3` and `!folio_test_swapcache(folio)`. In that case, both `folio_ref_count(folio) > 3` and `folio_ref_count(folio) > 1 + folio_test_swapcache(folio)` evaluate false, causing an unnecessary local LRU drain. During an Ubuntu boot, I observed over 5,000 redundant local LRU drains. For a kernel build with a minimal configuration, I observed more than 20,000 redundant drains. Fix this by checking against: `1 + in_swapcache + in_lrucache` instead of hardcoding `folio_ref_count(folio) > 3`. Suggested-by: David Hildenbrand (Arm) Reviewed-by: Kairui Song Acked-by: Shakeel Butt Reviewed-by: Baoquan He Signed-off-by: Barry Song (Xiaomi) --- mm/memory.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index ff338c2abe92..87da78eb1abd 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4181,6 +4181,9 @@ static bool __wp_can_reuse_large_anon_folio(struct folio *folio, static bool wp_can_reuse_anon_folio(struct folio *folio, struct vm_area_struct *vma) { + const bool in_lru_cache = !folio_test_lru(folio); + const bool in_swapcache = folio_test_swapcache(folio); + if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && folio_test_large(folio)) return __wp_can_reuse_large_anon_folio(folio, vma); @@ -4191,15 +4194,16 @@ static bool wp_can_reuse_anon_folio(struct folio *folio, * * KSM doesn't necessarily raise the folio refcount. */ - if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) + if (folio_test_ksm(folio) || + folio_ref_count(folio) > 1 + in_lru_cache + in_swapcache) return false; - if (!folio_test_lru(folio)) + if (in_lru_cache) /* * We cannot easily detect+handle references from * remote LRU caches or references to LRU folios. */ lru_add_drain(); - if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio)) + if (folio_ref_count(folio) > 1 + in_swapcache) return false; if (!folio_trylock(folio)) return false; -- 2.39.3 (Apple Git-146)