Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.30 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796063302; b=hFx724YBFUH04teccygiX6Ds0e4YYt8kAEzG4U1gWUYC1ORPDum6swrnsMyQlS5DqleI UfRlMKulIzPAY4y0WBr9HVZ/0cGPZwSB3ToNcvAR9qY7wfqyt0ur7o3XTzSVH38RuBGCT i3kUvF/O0kjxr7r2xCg96/sr4d9WGzQ8q8UFo28MFsJgHezjK8fXFc0Z87IMds1xCfVNv 6ks3m5+i2l3Qrf0QqrYlGgzJ57+p4dsf9VoKpFgPpyd1TJ426g9D1QePNqdj8YUBpbtOc vHrKLccHiFZwPZ0fSRyslWqn3MWDrQyv9X9Zk00OFQX/UnjYxHIuuAuHJgsbNuQbi9w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796063302; 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=IP0K/zOGY2Fk+qvhAH1plQY2/Mf3KJLXdj1fWUATcqQ=; b=yqr9eRShf4hlw6T7lcEx5znoQusHC4NhnbLWfvQPZ7GT9A8b7fDrG1b7PRZFus0xIMFm bnVZGIZXZtjvIREaSNueLl6WiYBU3nX7po3Ux9ZtUs9J7Y0LmaAlz8XCiBJpjjTty1IOF E1JxdNKaXGzXV0cXQG56dTE0eoDIsWfD2NFmy/KN7NvpJCPUsR84h9bQknioTtg10vZHZ UquLMdfZGPN9x+Itjl/7uJO7Js9oRiOrNKYIVr3Qk+QfBZ9HSuz/LtoL5TGCNybc8iyYq cM3/ztqPd/spTlyrpRRzMEeVMSfJIbu1GhBNbYvrZlF9xrtq+mrNceA/VNf5idLvKmg== 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=BDIaeAqa; arc=none smtp.remote-ip=91.218.175.30 Received: from mta0.migadu.com (out-30.mta0.migadu.com [91.218.175.30]) (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 8CDA3606A32E for ; Mon, 30 Nov 2026 18:28:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8CDA3606A32E 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=BDIaeAqa X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=MVAGnbl0kLKRk721yl+Wr+H6hHjumYFUsKoeFc+QOEw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796063299; v=1; x=1796668099; b=BDIaeAqaWKRqMGXvOLjtKT9UntQHt9cAmYs2ElN3OpcaMzf4a3TkP7ApJiIF+LLigD2eJqcl p32++403INh9rceSvVhPHcp/1e4JZjL7uuKqTO+6qDbhxATCdcdy98K2yJlQt8EPQnfW7lCme6H FybiGorJmIXWNMBoptBryJBo= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id fa9d83d3bf3e968a; Mon, 30 Nov 2026 18:28:19 +0000 X-Mizu-Trace-ID: fa9d83d3bf3e968a 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 4/4] mm/vmscan: don't give swap to an anon folio that nothing maps Date: Mon, 30 Nov 2026 10:27:55 -0800 Message-ID: <20261130182755.666042-5-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 a process exits while reclaim has some of its folios isolated, the folios lose their mappings, but reclaim's reference keeps them alive. shrink_folio_list() still gives such a folio swap. If something else also holds a reference, such as the exiting task's TLB gather, pageout() returns PAGE_KEEP. The folio then goes back to the LRU in the swap cache, unmapped and with no slot references. No swap count ever dropped, so nothing queues it. It is an orphan from the start, and keeps its slots and memcg ID until reclaim comes back, which may not happen below a cgroup protected by memory.low. In a VM that runs short-lived cgroups under global memory pressure, 53 of the 64 orphans left after three runs got their swap this way, 51 of them from kswapd. An unmapped anon folio that is not in the swap cache has no page table or shmem entry pointing to it, so don't give it swap. It is freed when the last reference goes away, often reclaim's own. A KSM stable node holds no reference, but KSM can map a KSM folio again; a later pass swaps it out then. The check runs right before the swap allocation, after the split of a partially mapped folio, so it also catches an unmapped first page of such a folio. An unmapped folio that is still held elsewhere is kept or activated as before; only the swap allocation is gone. The last mapping can also go away after that check, while reclaim holds the folio lock, so the exiting task's free_swap_cache() can't free the folio. So check again after the unmap step: if the folio got swap in this pass and is now an orphan, free its slots with folio_free_orphan_swap() and keep it. shrink_list() in Linux 2.6.12-rc2 already allocated swap before checking page_mapped(), so there is no Fixes tag. Signed-off-by: Shakeel Butt --- mm/vmscan.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/mm/vmscan.c b/mm/vmscan.c index 99cb209797b4..ddc1584d06ed 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1128,6 +1128,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, struct folio *folio; enum folio_references references = FOLIOREF_RECLAIM; bool dirty, writeback; + bool new_swap = false; unsigned int nr_pages; cond_resched(); @@ -1331,6 +1332,19 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, split_folio_to_list(folio, folio_list)) goto activate_locked; } + /* + * If nothing maps the folio, its last mapping went + * away, usually because its process exited, and no page + * table or shmem entry refers to it. It is freed when + * the last reference goes away. Giving it swap would + * only leave it in the swap cache if something else + * still holds a reference. KSM can map a KSM folio + * again; a later pass swaps it out then. This comes + * after the split above, so it also skips an unmapped + * first page of a partially mapped folio. + */ + if (!folio_mapped(folio)) + goto keep_locked; ret = folio_alloc_swap(folio); if (ret) { int __maybe_unused order = folio_order(folio); @@ -1366,6 +1380,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, * flag for the folio solves the problem. */ folio_mark_dirty(folio); + new_swap = true; } /* @@ -1419,6 +1434,17 @@ static unsigned int shrink_folio_list(struct list_head *folio_list, } } + /* + * If the folio got swap above but is an orphan now, its last + * mapping went away in the meantime, before try_to_unmap() or + * while it ran. No swap count will drop for it, and the + * unmapping task's free_swap_cache() found it either not yet + * in the swap cache or locked by us; it may have queued it. + * Free the slots and keep the folio, as above. + */ + if (new_swap && folio_free_orphan_swap(folio)) + goto keep_locked; + /* * Folio is unmapped now so it cannot be newly pinned anymore. * No point in trying to reclaim folio if it is pinned. -- 2.53.1-Meta