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 20E15606A32D for ; Mon, 30 Nov 2026 18:28:17 +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=cE9nNzNE Received: by kanga.kvack.org (Postfix) id CC3056B05B0; Mon, 30 Nov 2026 13:28:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C72576B05B1; Mon, 30 Nov 2026 13:28:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B8AC36B05B2; Mon, 30 Nov 2026 13:28:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8D5466B05B0 for ; Mon, 30 Nov 2026 13:28:15 -0500 (EST) Received: from smtpin07.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 135D640BC2 for ; Mon, 30 Nov 2026 18:28:15 +0000 (UTC) X-FDA: 85492620150.07.D7B3070 Received: from mta1.migadu.com (out-134.mta1.migadu.com [95.215.58.134]) by imf15.hostedemail.com (Postfix) with ESMTP id C6FDCA001A for ; Mon, 30 Nov 2026 18:28:12 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=cE9nNzNE; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.134 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=1796063293; b=2M1PUaW6xb3zZtgXJZW+8QPMAGlt0NdqQO9QE4fOs9gJoVAZVVntAV7iSbwoz20g/2LPeO ayx+Dy37Z/zm5juwOWgVq/E5ZQlaoYOXTX+kPC4G+5ss2ixVN3jcG4rB62gkAlMJN7Pw7c wWcLjZm4sG4cn9rrpyMVsbf+ohW4cPU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796063293; 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:references:dkim-signature; bh=RD8N/gjx31cvnbVm4IWSQefAiwDbQKDoLIXX1Zl3WE0=; b=ue1YUXo4FIn5ZEza6ayPpMFYmWgUCf2eiVmmQhlD+moHl0b6diCVOiSu8lijCjgBVAJ+Yl HLhKEbtpl0CDmi5sn/L7G11l8D4ErmS9e4nooKJsukuf6r5YUGIXanqWq3kyGsjMtmVjEI TAZSFv/UE4sQmQTRAOTbTFo3E3yGZxU= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=cE9nNzNE; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of shakeel.butt@linux.dev designates 95.215.58.134 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=OTN0Cu9dgX0AvSkhJxiA25yUrxehZzZ6V8nXdQt2dgg=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796063288; v=1; x=1796668088; b=cE9nNzNEpI7TQkSPCQsL23jieSqcXO1iVBST3fjX465dheycyTcd+e8EtwMq5R8J5M3OLcnE ylo0kTPWqGSts1fTgAO3kIj+2FVuVPS5DNEbg/z5tKD4A9ilmV8He+vS8/zoG1Ne97b6AmjK8P2 18nJ4iFsTtTRUApF5iWgwx8E= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id c94c18f1825b4fb6; Mon, 30 Nov 2026 18:28:07 +0000 X-Mizu-Trace-ID: c94c18f1825b4fb6 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 0/4] mm, memcg: stop orphaned swap cache from using up memcg IDs Date: Mon, 30 Nov 2026 10:27:51 -0800 Message-ID: <20261130182755.666042-1-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: C6FDCA001A X-Stat-Signature: 36sso1ffkuq9joj83qbwa6kc4mwhw7ja X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,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: 1796063292-184062 X-HE-Meta: U2FsdGVkX18GTc72nWd0dy2bcw2uzqjj7Zr+s2T0eP4j8twQnyGKfQGCr15h2Xk3I9ha8ugxr/yKlTBYyK7Fc37RV0UPVOSNIA/QM9V3z0VR6dVlSRrW4axt51CV4Rdq++owVZEH4ruhBtlWYufYov9EMDS4sv+AXWj08dv4sFJlaVrW5p67oHWyUsq7AXbcR+7yZSextKHUWNqX4JYBAbdi1HNqOgg3Tc39L4jaZ32nzphQiAvTS8COFlZV/sFF7BNSGE7uqjvopF0uuQAc/ZcCVqwu/1+zAQTDEYul+gLXfhG0kqQcBPeIr6UhnPVSl9Ewtf7WRumfPAsG5ya+hOBeh9jW65lZMXMXx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N/li4LP2ivQAB0MpqDRkl1REA== 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 On Meta hosts that run many short-lived cgroups, about 13,000 new ones per hour, creating a memory cgroup started to fail with -EBUSY. All 65,535 memcg IDs were in use, nearly all by deleted cgroups, so systemd could not start units and container setup failed. drgn showed what held the IDs: - a 7.1 host: 58,431 IDs in use; 57,803 of the 57,806 dead ones held only by orphaned swap cache, 262K slots (1 GB) - a 6.16 host: 22,777 IDs in use; all 22,284 dead ones held only by orphaned swap cache, 1M slots (4 GB) An orphan is a swap cache folio that nothing maps and whose swap slots have no references. It can never be swapped back in, but it keeps its memory and its slots until LRU reclaim frees it. Each slot records the ID of its memcg, and the ID stays allocated while any slot records it, even after the cgroup is deleted. On these hosts, reclaim did not free the orphans for days. memory.low protects the parent cgroups, and since LRU reparenting (f1cf8d2f36dc) the deleted cgroups no longer hold the folios, so reclaim of those cgroups does not find them either. That makes orphans pile up faster on 7.1 than on 6.16, but both leak. On 7.1, the leak still shows up in nr_dying_subsys_memory in cgroup.stat, because a deleted memcg stays around while its ID is held. Since commit 8290f45995f5 ("memcg: move memcg private ID refcount to objcg") in 7.4, the memcg is freed and only its ID stays, so that count can be 0 while the IDs run out. Then the only sign the kernel gives is the warning from "memcg: warn when memory cgroup IDs run out", which is already in mm-unstable. The kernel normally removes such a folio from the swap cache when the last reference to its slots, or its last mapping, goes away. Each patch fixes a case where that does not happen: - Patch 1: the folio is locked or under writeback at that moment. Retry from a delayed work, and sweep all swap clusters if the retry queue overflows. - Patch 2: the process exits while the folio is unmapped for migration or a split, or a split hands back an orphan. - Patch 3: do_swap_page() maps a KSM copy and drops the last swap reference of the original. - Patch 4: reclaim gives swap to a folio whose process has already exited. This was the largest source in a VM test. Swap slots can also outlive their cgroup for reasons that are not bugs, for example a tmpfs file left behind. This series does not change that. The series is based on mm-everything-2026-11-30-07-23, which already has two related patches: "memcg: free the private ID of a memcg that never comes online" [1] and "memcg: warn when memory cgroup IDs run out" [2]. Testing in a VM with 8 CPUs, 4 GB of memory and 8 GB of swap: - 1,200 short-lived cgroups, 8 at a time, that each swap under memory.high and exit, under global memory pressure and with memory.low on their parent. Dead memcg IDs left at the end: without the series with it v7.4-rc3 132, 27, 21, 39, 0 in 5 runs 15, 108 mm-everything-2026-11-30 51, 17 0 in 5 runs Four of the runs with the series used KASAN, lockdep and DEBUG_VM. A KCSAN run reported nothing in the new code. In traced runs without patches 3 and 4, 53 of the 64 orphans left got their swap from reclaim, 51 of them from kswapd, after their process had exited. - A 1 GB process killed three times while thousands of its folios were under slow swap writeback: the retry queue overflowed, the sweeps freed every orphan, and a sweep took about 63 ms with 3 GB of swap in use. - Two processes sharing 4,096 KSM pages: without patch 3, 4,096 orphans and the ID of their dead cgroup stay; with it, none. - A THP swapped out whole, then partly unmapped and split through debugfs: without patch 2 one orphan stays; with it, none. - swapoff and swapon in a loop under KASAN and lockdep: no reports. Each patch builds with W=1, and the series also builds with CONFIG_SWAP=n, CONFIG_MEMCG=n and for i386. [1] https://lore.kernel.org/all/20261124000439.967403-1-shakeel.butt@linux.dev/ [2] https://lore.kernel.org/all/20261124020800.2313930-1-shakeel.butt@linux.dev/ Shakeel Shakeel Butt (4): mm/swap: free orphaned swap cache instead of leaving it to reclaim mm: free swap cache that migration or a split leaves unmapped mm/memory: free the swap cache folio that a KSM copy leaves behind mm/vmscan: don't give swap to an anon folio that nothing maps mm/huge_memory.c | 10 +- mm/memory.c | 6 ++ mm/migrate.c | 9 ++ mm/swap.h | 17 +++ mm/swap_state.c | 12 ++- mm/swapfile.c | 263 ++++++++++++++++++++++++++++++++++++++++++++++- mm/vmscan.c | 26 +++++ 7 files changed, 334 insertions(+), 9 deletions(-) base-commit: f05a21e03b2fa69a4fc3130e148349985d62ff8e -- 2.53.1-Meta