From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.144 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796063298; b=zPnfUClZ5/8dUu3Sn+h3pSshN6ebTMO6t4D/udYGpubqIytbisj8G8arPiv0Wbw6hyA4 TYSScxVkRdj3MAiO32KFUmeZJJWxhWMkRiecog8yXCPtmIb/5mDpp3D2bTSiqG4RjfWEc TlgfIITpP8rWPQDhb14jMX6kOBW9yJAzVLcp83pyX4ySpEZ/9v7J81316vOky6hAK1YUm n9Ln6cEGufWiI2qHf0uyPoa4UQxVhLH7+wPS16Z+dpD+DoxDT9rWzqfDRecqxLPVF75mG TtZPnCYvXW3Z1yGnLrydqq0ODf9WqBgDWp/FA5BEo+PALlBrxzXNc2lI+a+mk0xsw/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796063298; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=G1WpjjyvQETcBwKimi3vKBSz9VtsymBee3GKhCKOMBM=; b=SGyoJG6PeUJckgO0LUtomDt399OuU1Bt1+rThs/15hA+zbXKRNlqgzNuXVk9US2CJmHU 1LJLy5lCTpz0/L3kJ84wqfXF6huL7SA6u4s0G2KJirOh5FbAFbSDv38c+WU4ygXKx2wDL vUfXVbaXoVf1pW1AY11wKMF5AC0Qz2mjjdKDCQW6pAjXx8CtfZEMUzerumQMPqBM8vj8p e9WY4dEsvWL5w+E5uY/7d3PCxj17Z93dmxqjWj75dvjAvBpz0ClAOfuSmevk3k+UTSdgF 4YEON1o34nDVYN4Rd6pV3iN0hGuCCPMNZU67ELAc86RjvEQaMhFa7pjxTlt9UJ763MA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=MpKJL1OA; arc=none smtp.remote-ip=95.215.58.144 Received: from mta1.migadu.com (out-144.mta1.migadu.com [95.215.58.144]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2D5B606A335 for ; Mon, 30 Nov 2026 18:28:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C2D5B606A335 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, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=MpKJL1OA X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=zLoDgrNuuF7pU+j5jjk+hCfnKLmKU6FmFEPpTGYfsRU=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796063295; v=1; x=1796668095; b=MpKJL1OA8MOydnwY6JzEzMusC2Wr3CSmvoxxL+Qhg1u133ZVXdTZZodGw9abf/jSpm3rZCY5 a6TxQSztg93cvciYgKKwR+D4yqcB0dNNtxyopTPx1x5dWJFsCAwk3L0JE4lyqlxalTHE7caCo2+ 3MLGLerPTzHWrg24RDa7wC84= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id e7964cd0c768fee3; Mon, 30 Nov 2026 18:28:15 +0000 X-Mizu-Trace-ID: e7964cd0c768fee3 X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Andrew Morton , Johannes Weiner , David Hildenbrand Cc: Michal Hocko , Roman Gushchin , Muchun Song , Qi Zheng , Zi Yan , Lorenzo Stoakes , Vlastimil Babka , Kairui Song , Nhat Pham , Usama Arif , Hugh Dickins , Meta kernel team , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/4] mm: free swap cache that migration or a split leaves unmapped Date: Mon, 30 Nov 2026 10:27:53 -0800 Message-ID: <20261130182755.666042-3-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261130182755.666042-1-shakeel.butt@linux.dev> References: <20261130182755.666042-1-shakeel.butt@linux.dev> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Migration and large folio splits replace the PTEs of a folio with migration entries, and remove_migration_ptes() restores them afterwards. Zapping a migration entry neither drops a swap count nor calls free_swap_cache(). So if the process exits in between, the folio comes back unmapped. If it is in the swap cache and its slots have no references, it is an orphan that only LRU reclaim frees. This goes back to when migration switched from swap entries to migration entries. Free the swap of such a folio at the end of remove_migration_ptes(), with folio_free_orphan_swap(). Every caller holds the folio lock, and the folio is not under writeback: migration waits for writeback or gives up, and a folio under writeback is not split. So the swap is normally freed right away. The rmap walk also sees the PTEs that fork copied, so nothing can map the folio again after it, except KSM for a KSM folio, which is safe. Splits have one more gap. __folio_split() frees the swap of unmapped after-split folios, but not of the one it hands back to the caller. And a folio that was already unmapped, such as a THP swapped out whole, is split without remove_migration_ptes(). So if part of such a THP was unmapped while its other slots were still in use, the folio the caller gets back can be an orphan. Run the same helper on it in __folio_split(). It has no slot references, and no caller needs it to stay in the swap cache. In a VM with 1,200 short-lived cgroups under global memory pressure, the migration check found 0 to 12 orphans per run. A THP that was swapped out whole, then partly unmapped and split through debugfs, left an orphan before this patch and none after. Fixes: 6c5240ae7f48 ("[PATCH] Swapless page migration: modify core logic") Signed-off-by: Shakeel Butt --- mm/huge_memory.c | 10 ++++++++-- mm/migrate.c | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4fc888ddfb69..f024289930ac 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -4443,6 +4443,14 @@ static int __folio_split(struct folio *folio, unsigned int new_order, */ for (new_folio = folio; new_folio != end_folio; new_folio = next) { next = folio_next(new_folio); + /* + * Free the swap of an after-split folio that nothing maps and + * whose slots have no references, the one kept for the caller + * included: an unmapped folio is split without + * remove_migration_ptes(). + */ + if (!ret && is_swapcache) + folio_free_orphan_swap(new_folio); if (new_folio == page_folio(lock_at)) continue; @@ -4452,8 +4460,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order, * lru_lock, so we defer folio_put() on tail pages until * after the split completes. */ - if (is_swapcache && !folio_mapped(new_folio)) - folio_free_swap(new_folio); folio_unlock(new_folio); folio_put(new_folio); } diff --git a/mm/migrate.c b/mm/migrate.c index 96b66a272379..fbd69f871291 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -494,6 +494,15 @@ void remove_migration_ptes(struct folio *src, struct folio *dst, rmap_walk_locked(dst, &rwc); else rmap_walk(dst, &rwc); + + /* + * If the process exited while the folio was unmapped for migration + * or a split, no PTE points to it now: zapping a migration entry + * neither drops a swap count nor calls free_swap_cache(). Every + * caller holds the folio lock, and the folio is not under writeback, + * so this normally frees the swap right away. + */ + folio_free_orphan_swap(dst); } /* -- 2.53.1-Meta