From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.14 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796063291; b=cuWhNEdTJG4eRI602x6BZQTdJqv0bcRfjLCfslqIwnRppnIdzfNpHdkVHjxlGt2dhu7U vv0PEAauyQTB4FSjbUQPAjxrO54YevANl+N0MY620MjfWRlicjyFedV/caITbk3co/PJ9 24zsRRSrc11J2ezyTR+D6UzKFpoQ4Ywu4f1fkSiTtDY4uHrdFO7llGrOJCaCKQXD3ooZc dKHEljzlA4zzECI9Z+5Ek7a/2Bt4c6MmbriRIVyy4dXswkta6pP8xG/xwj+LTpGqNQejY V9ErS1kw0M0RvqRww5tMgsphrYFqZV9zUpb2uXvPFVyj1rR3RQ05wfNMeoUMQ4m2xGA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796063291; 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:MIME-Version:Content-Transfer-Encoding; bh=RD8N/gjx31cvnbVm4IWSQefAiwDbQKDoLIXX1Zl3WE0=; b=rudcl4eeEIjRI58uS2aWR6uLNT+s9IrDGTReHh/P+aR0bn+bTqeV7GJ0utv7SVNAajOV bLAELeKSoJjwQasBO7FLlaiznD6SWN/CFVEb2P7gQklkuSI9XzrAcDclIiW3pGTKPd0ko KosDs2ll/LDNTUOyAriir6iGnI76qX+jwHNXXUxG6MLnoc1DbmR2KoybI7BeVXJyIudzg Nhqaw902ZcLZBJZBX7jvy0K0+CrR5hPu8SgUwZmPPFhKTwaIAe+z0okXA+YhLejkekNRY yx0PyTF984EAGZ7Ro7jDRPntCA7R+wfw3mwr/CYykSCpJmkXih6J5J6AVPyFoYcYrjg== 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=cE9nNzNE; arc=none smtp.remote-ip=91.218.175.14 Received: from mta0.migadu.com (out-14.mta0.migadu.com [91.218.175.14]) (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 F2736606A31C for ; Mon, 30 Nov 2026 18:28:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F2736606A31C 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=cE9nNzNE X-Envelope-To: linux-kernel@vger.kernel.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-kernel@vger.kernel.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-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 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