From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F2AA606A31C for ; Mon, 30 Nov 2026 18:28:36 +0000 (UTC) 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 Received: by kanga.kvack.org (Postfix) id B89C06B05B6; Mon, 30 Nov 2026 13:28:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B613A6B05B7; Mon, 30 Nov 2026 13:28:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A29986B05B8; Mon, 30 Nov 2026 13:28:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 3A19E6B05B6 for ; Mon, 30 Nov 2026 13:28:24 -0500 (EST) Received: from smtpin24.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CD0AEA04D3 for ; Mon, 30 Nov 2026 18:28:23 +0000 (UTC) X-FDA: 85492620486.24.7C9BEAC Received: from mta1.migadu.com (out-156.mta1.migadu.com [95.215.58.156]) by imf03.hostedemail.com (Postfix) with ESMTP id 6E7FA20009 for ; Mon, 30 Nov 2026 18:28:21 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BDIaeAqa; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.156 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796063302; b=wzklS+dDIw32aZPFhwipzFnC8z6+DyY7BjFxajENHLn9+PaqBFhzHV21K8xBoJmeGtNlbB 46JP7pXPMnhbTv8GzPakfwbDOKpHGgfHvdFUV5Ema8m2BeWKLaRc1z9hF8TD9abUNHVwM1 Nusj7yThmwXVU7XFeEeHB2zSmY5r+mU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796063302; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IP0K/zOGY2Fk+qvhAH1plQY2/Mf3KJLXdj1fWUATcqQ=; b=ThhoakUkNK3P3C6BmxS2SBXTrNnGwySqAH2yIPk5ApqLtP0N0sNU4fYOmiJbNhS0J0/OjI APJ1561dEBgGfoKIV7e2TEsqdObibcM5kqrLeN+pEilkHDB7UPZhuXOHcuS51oh04Xtbxg wZED65eVn+oA5f8iwmw8h9ySTsKQjww= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BDIaeAqa; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.156 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.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-mm@kvack.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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6E7FA20009 X-Rspam-User: X-Stat-Signature: epoabzebxdfrwkxzke91bdxuxdchbnbm X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796063301-732581 X-HE-Meta: U2FsdGVkX1/shtNSOo5rXQpWmTfLlBib1LH7wh3mRAYaa1V2gPo/1LbYDvvPtb672LlbvqVMwYnmdkevI8zOoItYtkMkSFfnGpHyH/btF19fT8idZUhBVW9Xt0ctLBd4mqdSLlApStIw7x6yCgeQK9QDje3+gbdi0IRdcOt2HEldfAFA43YOPUqaOF7B0O1mj3dFnkm8I6ti4oB5dNINk7b9WEW4sQdnsCbXnp9TCEvPVwdCsbmhFdpq4Dmkw/7bAhFYxO3y4uYgLY3qlkdTgNmno2nq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l6K7MTGjKF96ZAvZ9m3WwGpNUE+7wlTSvrF6OdnifFRWhqR31nzlwSTVXg5WcMJkGB+GdPpbm Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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