From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-183.mta0.migadu.com (out-183.mta0.migadu.com [91.218.175.183]) (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 44A0340D584 for ; Sat, 27 Jun 2026 02:44:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.183 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782528262; cv=none; b=YU6evutIzq4K5i0+BUPGvWRdtjflHHAMG6CaWhJggADneoDtwZwhLI9MnrG5MkRCZP8zPGRtLw1ZIZlUuh6+wnVvgxYqvR2NCxwKoUxZ3ID2Bn4yv8vTFXeW+tBrFXL4Tcn1V3b9Fb7PaixU1HHnByY7vIx+nxk+TjWLHE9iULs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782528262; c=relaxed/simple; bh=/KBju8Rl2CBRd0ZmaPoEB48yojyNZZg24CxiGMwy+GI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=nV8u2I5yoB6nQvnsrftReqX2a0uZhxHkmmSYbNe2LwnzWZH2EiYrRkcBr6qSj2y557NRXETwmcJz7J+9HKIiudnVZd8k2E+6tC7fNQ3X6we6WoPG+fpchTPZ1fAyWmKkUovKo/+ZQCZ/0UrzSsn0ApUKIJGIGZN27RT4M1WaDn4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=EUiBNUdO; arc=none smtp.client-ip=91.218.175.183 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) header.d=linux.dev header.i=@linux.dev header.b="EUiBNUdO" Date: Fri, 26 Jun 2026 19:44:03 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1782528256; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=q4KPW4hH3D8r4gcyvgnmYX50L3+RdAFR9wgu/2J4vTs=; b=EUiBNUdOMI15HmF9eakHwkBkurQNCxW1mvBDj8rMqv/u9hizN1qwgLP1xY9GlHCuA4zeot VbIaVwqElV+XXVL5jQIF1B6Ih9fnXSpt2hCh4Q1+a80CTJI7s5n6pH2csjeGEkwMDAgxGl C5cnRiYOqGLUCT21eTcvNo6D8FZZw6Q= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Shakeel Butt To: "David Hildenbrand (Arm)" Cc: Barry Song , akpm@linux-foundation.org, linux-mm@kvack.org, baoquan.he@linux.dev, chrisl@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, shikemeng@huaweicloud.com, surenb@google.com, usama.arif@linux.dev, vbabka@kernel.org, youngjun.park@lge.com Subject: Re: [PATCH v2 1/4] mm: avoid unnecessary lru drain for wp_can_reuse_anon_folio() Message-ID: References: <20260623231635.43086-1-baohua@kernel.org> <20260623231635.43086-2-baohua@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Migadu-Flow: FLOW_OUT On Fri, Jun 26, 2026 at 06:25:48PM +0200, David Hildenbrand (Arm) wrote: > On 6/24/26 23:04, Barry Song wrote: > > On Wed, Jun 24, 2026 at 11:02 PM David Hildenbrand (Arm) > > wrote: > >> > >> On 6/24/26 01:16, Barry Song (Xiaomi) wrote: > >>> 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; > >> > >> I'm not keen on making this function even uglier, so no, not like that. > >> > >> We have the earlier "folio_ref_count(folio) > 3" check. > >> > >> In which scenarios can you trigger this such that we would care? > >> > >> If the answer is "I don't know" there is no reason for a change. > > > > As I replied to Shakeel in the v1 discussion [1], this can avoid a > > large number of drains, both during Ubuntu boot and under normal > > workloads: > > > > "I booted the system into Ubuntu, and after > > is this with this patch only? > > > > > boot completed I observed: > > > > wp_reuse_skipped_drain: 5542 > > do_swap_skipped_drain: 0 > > > > Then I built the kernel in a 1GB memcg using zRAM swap, and observed: > > > > wp_reuse_skipped_drain: 25017 > > do_swap_skipped_drain: 43595 > > This is all data that belongs into this patch description, not hidden > somewhere on the internet :) I asked the same i.e. to include this information in the commit message [1]. [1] https://lore.kernel.org/linux-mm/ajK4N2zK3sPZFQuf@linux.dev/ > > > And why do we care about local draining? This is not a drain-all. Let me take a stab at it. Local draining can potentially make the lru cache's batching ineffective. The whole reason for lru caches is to batch the lru operations to keep the lru lock contention low but if we keep draining prematurely we will continue to make lru lock contention worse. This is particularly bad for workloads which chrun a lot of LRU pages through allocation, free and/or reclaim.