From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) (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 8E5E517A2FC for ; Tue, 7 Jul 2026 12:55:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783428938; cv=none; b=JWD/4oKVQaKaUy6qiL8oJET3AT2Ivv7dBGDltd9R6z+yGsLADLuL5P7N8ngd5CR/eUPTmjVA6M/Ro24Gs/WCctDhM7mm1GWQGUJC7ADg99mo4LFeQwk9qsARwdq8H+1TwYx0kbV4G9QsSBwVpD1tCyJQm7KXrEiYk800GJeJP24= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783428938; c=relaxed/simple; bh=2zBjFdHpqYDMkxfqnWcFYhXz9afUWiFztTnSeIO5yPU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VVRpyNYIKul0H4gHOmHSHIkKGeh2XmRRQI8JU48s09TdagohS2d/ZXm9UQJxJ7EKQuG3kU2cJ9UGnU6Y5sGgO/K/jnWmfhTPesq26omaOaBjXMQwjjQaez+yE54GBl0RSVWhXfag3hrPgDBarotPau0RodJdbpDePK12YU6qxRY= 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=E6rFw4FQ; arc=none smtp.client-ip=95.215.58.182 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="E6rFw4FQ" Date: Tue, 7 Jul 2026 20:55:18 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1783428933; 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: in-reply-to:in-reply-to:references:references; bh=RocPjRVmhKRX2Gf0pkjPFphj9HwTbw1IGurD0TN9Dxs=; b=E6rFw4FQWw2qdm0dZDuOHLoC/Hs5JB3u6KObSs3b55vka5YGoXTuSiNs71dL24GPbp971w N3+3xzREEyYKKSyC4O2ACyNYf1JZw9UyRQSU+uafXAnYOzRnqJwqRYO5U2DswgIZJA58pj 6YEib/ysaWyxBDeMJr4qYNpnMIgBrYU= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Baoquan He To: "Barry Song (Xiaomi)" Cc: akpm@linux-foundation.org, linux-mm@kvack.org, 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 Subject: Re: [PATCH v3 1/4] mm: avoid unnecessary lru drain for wp_can_reuse_anon_folio() Message-ID: References: <20260701235955.36126-1-baohua@kernel.org> <20260701235955.36126-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=us-ascii Content-Disposition: inline In-Reply-To: <20260701235955.36126-2-baohua@kernel.org> X-Migadu-Flow: FLOW_OUT On 07/02/26 at 07:59am, Barry Song (Xiaomi) wrote: > 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(-) Rechecked, LGTM, Reviewed-by: Baoquan He > > 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) >