Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.149 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796063300; b=1dwVsUQM+MzQ7JzhyKAGAFvebTjNQKtAIGhjaSuZ4XDQZKoRh0rrXg1/DvESyFp7KmOK 1oevX+uzijPhY2IZ8rkR/hDNPfys7jh5QwrkmpXfJ+U8Ceh7WH5PtyVjWtJnL3bytMaOk E2KG+6hP6oFqjw7f/CfBAr0WvAzSJIHachNoNenW1sMFMNAnfhoDxcLheDzuRIgwlt4eM qOoEIc2cL051tH3UR3LbeJNdAXZU6phyUn2evnIXfMkZsrcifmWLfSmPzgaVUah8ebh4l EFf1IYO9IwMiJcUab4fevz5waxJr4mHrWFfFJ2PIKAZkhx0CISBCBoahcV1jUY1fFig== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796063300; 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=N00YG8Rj4eWcJIRawwpNfBVCApuQJ1sUbXidxvT8rZE=; b=C2TXFNoj8qo3nFvdO867n59cV4qyzsK8/yqhpc+EHIsK+BIEZPg1XHxk2Qj80/S/j14V +7K+46sdZkfXKsUyJJH9wVAQZh7argMUudOLMC3pIj1YdqosZzWlu/v4Bic1U/OaCzrSj thA90IpmtpHUhPBfRAyK8HyPNzESTR5slmx4Z6TftRenNVXelzDyd4Rem1yR+dpG6VT0R d1//14By4FniIenqSzNbDx+kn4dvidZLi6yerHmku27Lamyhg9aLu8Km6xHG72N2rDQkX Yg50MqHaSYXOiopPnRBHv6HGmCu8bT6+ffbAo7cj79AsnsbT63jXbmhqRt4xnNi/9EQ== 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=qumBxi+w; arc=none smtp.remote-ip=95.215.58.149 Received: from mta1.migadu.com (out-149.mta1.migadu.com [95.215.58.149]) (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 A88EB606A331 for ; Mon, 30 Nov 2026 18:28:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A88EB606A331 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=qumBxi+w X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=uno0oXq10DGeLp5ZEiS45qpoa+LylO0cqVC6pYKYuWo=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796063297; v=1; x=1796668097; b=qumBxi+weuxRZrI/KljA0fzRrwI4/kYytnzEhLF5VlF8FfSvMQRJlmoT25D0DvC/nq6hDRgA L6IeLz3pee27oUIvBx1WJAgFNI1kVKYh72cvIymKgOYZ2vvW6H8Bpa4MUgqtAOtiurBxZSmkMBJ ODl7H6BzA+IZswtcrx+ifwd0= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 9527fc6b882a848e; Mon, 30 Nov 2026 18:28:17 +0000 X-Mizu-Trace-ID: 9527fc6b882a848e 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 3/4] mm/memory: free the swap cache folio that a KSM copy leaves behind Date: Mon, 30 Nov 2026 10:27:54 -0800 Message-ID: <20261130182755.666042-4-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 When do_swap_page() finds a swap cache folio that it may not map, ksm_might_need_to_copy() gives it a copy to map instead. This happens for a KSM folio that lost its stable node or while KSM is unmerging, and for a folio that another process mapped into an unrelated anon_vma. do_swap_page() maps the copy and drops the swap count of the original, which stays in the swap cache. If that was the last reference to its slots and nothing maps it, the original is an orphan. Only LRU reclaim would free it, and with it its slots and their memcg ID. This happens to KSM pages that unrelated processes share. After the page is swapped out, the first process to fault maps the swap cache folio into its own anon_vma. If that process exits, every later fault on the slot by an unrelated process gets a copy, and the last one leaves the original behind. Free the original with folio_free_orphan_swap() while it is still locked; if it is under writeback, that queues it. The check covers all slots of the folio, because one of the sharing processes can swap the slot in as part of a large folio, and the copy takes only one page. Two unrelated processes share 4,096 KSM pages, which are swapped out. One faults them in and exits, then the other faults them in. That left 4,096 orphans before this patch and none after. The test used only small folios. Fixes: 5ad6468801d2 ("ksm: let shared pages be swappable") Signed-off-by: Shakeel Butt --- mm/memory.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index 82e1ec8e44e7..67b7564a0623 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5368,6 +5368,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) folio_unlock(folio); if (unlikely(folio != swapcache)) { + /* + * The copy is mapped and the slot's count was dropped. If + * nothing maps swapcache and its slots have no references + * left, only reclaim would remove it from the swap cache. + */ + folio_free_orphan_swap(swapcache); /* * Hold the lock to avoid the swap entry to be reused * until we take the PT lock for the pte_same() check -- 2.53.1-Meta