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 266BC376A08 for ; Tue, 23 Jun 2026 23:16:49 +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=1782256610; cv=none; b=fyE9CbzmIiHQR4VVWJJ8wf1+twRd0ghri5ftbuflhSldF/Lh7b27R58tJrRhY2sZoeQ66OzIGkXQjt3c53pV6IuzsW/R4lPt8HzrdXfXHlxGFRg2fi4eCV3MOAslIJx7993SCHIh3QjwPQEioNixbPpJTqa24M01goxgIqC0/UQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782256610; c=relaxed/simple; bh=O/4Lys8fj26zoMtMo9lnw6MNcR+MnxJwVDJGBHyE0Os=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qaF6+Oi74NYdcmAnmJWI7xgU1LmC42vtEaxwATEmzFSrdH0yeC31EgrrKdlp6A5PRHjWT7SZyB7IJfBGwKdid0AeSwsRp+0kcZuEbCBlgQdOKLGaxs8lZVB6ObQaEW8TgsKtjvSK6EMx3QJ5jlLGPhaPGeTcPIgfBX4htUDz6eI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I3DjDCPw; 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="I3DjDCPw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9C4D61F00A3A; Tue, 23 Jun 2026 23:16:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782256609; bh=W7uWOWQBOVdULqmStrHDCiSY4tJX+t5C/ElCnubL7dw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=I3DjDCPw0aar3s2XFJEdB/d9KjD8eZdRlx+M/EjkySttlqC9rxvQD5frbGTvQ58Z3 ib/XKy0zISc3G6I0k6DyfIDPYCnDeMXs9JrTyD/lBI5aU+zuo3dJE5IxUrWcdz8/R/ 0AI+/t2XetLyDRaT0ElKUwQbW8gy479v2PHUfrQ1uG8ajMBYIQnBGLbc0Vv1zYZY/z lwvyb8mkEyQtvDFhPpoZQhrl3C2XvY8ptrH7d40WDb/j29FG5ocncyiP5mxct6K8Cp al1iwRu9jG0gHZBFnOL/x4ELS9oKVfdkN4HnVOVPw1FDkXczZ4pUUXSifQ5wv9nhb3 ox5+mapTR2Xgg== 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 v2 1/4] mm: avoid unnecessary lru drain for wp_can_reuse_anon_folio() Date: Wed, 24 Jun 2026 07:16:32 +0800 Message-Id: <20260623231635.43086-2-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 We always unconditionally drain the LRU before retrying anon folio reuse in wp_can_reuse_anon_folio(). Instead, assume !LRU anon folios are in lru_cache, and use the refcount to avoid many unnecessary LRU drains. Acked-by: Shakeel Butt Reviewed-by: Baoquan He Signed-off-by: Barry Song (Xiaomi) --- mm/memory.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index ff338c2abe92..f6848f4234a6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4193,12 +4193,18 @@ static bool wp_can_reuse_anon_folio(struct folio *folio, */ if (folio_test_ksm(folio) || folio_ref_count(folio) > 3) return false; - if (!folio_test_lru(folio)) + if (!folio_test_lru(folio)) { + /* + * Assume folio is on lru_cache and holds a cache reference. + */ + if (folio_ref_count(folio) > 2 + folio_test_swapcache(folio)) + return false; /* * 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)) return false; if (!folio_trylock(folio)) -- 2.39.3 (Apple Git-146)