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 239D73D9DD2 for ; Thu, 11 Jun 2026 10:51:38 +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=1781175100; cv=none; b=lPien785IhbxsccO5AegGh+3T6QU8BroFMor6Gaq+DTD1OexofrAnrTQADbokgoVMSRh/0fLLDDtdQyo4KJql5jkAnGOnTAK88ZPfIWlNp+rROI5dSUhmGguyeZHObXfa0cZD1CR+Ll7ZPTqq3WnkLd4HmQbtxK+CGs18YNLt1A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781175100; c=relaxed/simple; bh=Rcz73RcEa/taWWR17slGRepv+bS74F1mFaIFfDNfpJQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ksGjuzMivgt5Gxr72FyKI+9sHrZ0aX9OJi1ykYjONT7zndjSG3dpAfET6PK1yFsmwl1+Whp5vDhrDBFnCAOVDjsun/kpSEvO04Wqb8DVInqTERBiEi7Naty81IfdUrqhX7yZovHPA5MGygI8lHSF4h0bbMdYpFfzJFcWPZcdvQA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hTqlktC2; 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="hTqlktC2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DEA51F00898; Thu, 11 Jun 2026 10:51:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1781175098; bh=D0s7UpgjBDSfz2QS9ezb75mzFmzbkNnsRo0hYTKP+0g=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=hTqlktC234qrJRVePFIoWyyOzNDxH6ZTuZLG7rdmABAX1n782qVJpbjakKbenKhKX 549+aODTDis3NO49HEySttn/vHay0/ntSMxzqivp66k+UA4qObUl3j+HolO5scsx7m NFXn9hT27+9qR/sI0CctpnMkSWzmyhJ2ElRkZ5bpSnSpVhcgwaqhWSVz7W08NHVUlJ zwstbHtvUS61ctJcgc22F5O4xAZvSA8NrKOlhqT1bFZZ0PL/GLqb6e04S54teFPaTl dyZ/2ELnovdWX5EI2JLhHxsCde7LPPtV3q9kqWpIuJKAgmEID/cmLnjmFOeZhI3BlN M1jsPxHMd7BDQ== From: "Barry Song (Xiaomi)" To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, david@kernel.org, ljs@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, surenb@google.com, mhocko@suse.com, chrisl@kernel.org, kasong@tencent.com, shikemeng@huaweicloud.com, nphamcs@gmail.com, baoquan.he@linux.dev, youngjun.park@lge.com, jp.kobryn@linux.dev, usama.arif@linux.dev, shakeel.butt@linux.dev, "Barry Song (Xiaomi)" Subject: [RFC PATCH 1/3] mm: avoid unnecessary lru drain for wp_can_reuse_anon_folio() Date: Thu, 11 Jun 2026 18:51:22 +0800 Message-Id: <20260611105124.98668-2-baohua@kernel.org> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260611105124.98668-1-baohua@kernel.org> References: <20260611105124.98668-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. 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 56be920c56d7..487a34377a7b 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)