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 22D0237C0E6 for ; Tue, 18 Aug 2026 22:59:30 +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=1787093972; cv=none; b=s5bz9ApjxgQfmfQiQu27myFRQw9v1AdHB1z/94O44KWbdWO5iahc5KKoFN95XretaMtzF80AplelI36Bb8wE+mEy1PsSo5TPnfk2WmSHUxdtE1AN850878evnC/+7ugUfHmtduOFx56IxOxJ7HWsqi0ihZbTjC3DYwt4jt+h85Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787093972; c=relaxed/simple; bh=9dNKqqkF3YTCYZlCRCOG6L1lSYyF2lX7+PgR52Vwogc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=euw7ha4b075gGsj7r6n3yB62xWNqqjBZsrmbIYnXJVyhTr1e6BG3EmBEfpRYQDXMyokWnbceIUmq5HWfg+NHWykoTYJWkbjbuyRVbJ53MTEA1GzFQWdElIgrxiwDSiJMBH8BR/f7qAjm319yegb0TF7sV70VpLD3+6gs+pGTvac= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FHZPBPs2; 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="FHZPBPs2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B1621F00A3E; Tue, 18 Aug 2026 22:59:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1787093970; bh=B1x0THuwlQHV75X2L7TyoBUXAkoOlg2uj72r2UXh84o=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=FHZPBPs2ZsXHZEEPdxNKFFIHv6g+omPTLAwrOnfk72DZKY4Rvl+/XwHk4K9ZBE0K3 bu10OqjJC2CTYjB+W+qX+mpQEQSwwn6KIViALPI91C+W4hUADJ+7LiM709GGQRBC0G h340onceStKr9OqWRfQtIyxXWApPymKH1q9HxBoDZg1Qt/M+F/qxUfi/hgMTuJfonE t0ycSOZ71tG/jTdwzkS56JNJEfp9SIB7qezvxJIuY64jIXCLdXN9LOOXhM38MhrKNp 5Evhw3EXpr3vP2BtRHRxIT1FLeeHt/OCp8lvaUybSRv7qUdHVNEtchkfVKv9wC2WB5 bNuoBEuGb+Rww== From: "Barry Song (Xiaomi)" To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baolin.wang@linux.alibaba.com, david@kernel.org, dev.jain@arm.com, lance.yang@linux.dev, liam@infradead.org, linux-kernel@vger.kernel.org, ljs@kernel.org, mhocko@suse.com, npache@redhat.com, rppt@kernel.org, ryan.roberts@arm.com, surenb@google.com, vbabka@kernel.org, ziy@nvidia.com, hughd@google.com, ackerleytng@google.com, usama.arif@linux.dev, joannelkoong@gmail.com, hannes@cmpxchg.org, "Barry Song (Xiaomi)" Subject: [RFC PATCH v3 3/4] mm: drain LRU cache if necessary for splitting large folios Date: Wed, 19 Aug 2026 06:59:03 +0800 Message-Id: <20260818225904.55236-4-baohua@kernel.org> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20260818225904.55236-1-baohua@kernel.org> References: <20260818225904.55236-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 Smaller large folios might now be present in the LRU cache. Use David's new lru_cache_drain_for_folio() helper to drain the LRU cache before splitting a folio, ensuring that the folio can be split successfully. Also, we only perform the drain when it may actually help, assuming that the lru_cache holds an extra reference. Signed-off-by: Barry Song (Xiaomi) --- mm/huge_memory.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ced400f72d43..263ef9b6949d 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4201,6 +4201,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order, if (shmem_mapping(mapping)) end = shmem_fallocend(mapping->host, end); } + if (folio_ref_count(folio) == folio_expected_ref_count(folio) + 1 + + folio_may_be_lru_cached(folio)) + lru_cache_drain_for_folio(folio, 1, NULL); /* * Racy check if we can split the page, before unmap_folio() will @@ -4325,6 +4328,9 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order) VM_WARN_ON_ONCE_FOLIO(!folio_test_large(folio), folio); VM_WARN_ON_ONCE_FOLIO(!folio_test_anon(folio), folio); + if (folio_ref_count(folio) == folio_expected_ref_count(folio) + 1 + + folio_may_be_lru_cached(folio)) + lru_cache_drain_for_folio(folio, 1, NULL); if (folio_expected_ref_count(folio) != folio_ref_count(folio) - 1) return -EAGAIN; @@ -4805,6 +4811,10 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, goto next; total++; + + if (folio_ref_count(folio) == folio_expected_ref_count(folio) + + folio_may_be_lru_cached(folio)) + lru_cache_drain_for_folio(folio, 0, NULL); /* * For folios with private, split_huge_page_to_list_to_order() * will try to drop it before split and then check if the folio -- 2.34.1