From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796405977; b=DJW896OP5SPJvgUCY+rlwtSNsJNgpRmQVz63D/PwA4j37POND2/sPM33em7Sy1C1DlsM H9V/meHqRJNUASwHcCZnyDiNq1VEwMJNWs0Q95TX3rNX++Ri/Q09vJ7sZ4ZgAJknG3pb5 /wy5ESYlkO/ihyQ4BUlihvMcrTeenueW2IludmXU/a2C7+otrPsdPLk4WwkPvQEvcI8tC XZCS7V8OrwsW7kHUopBRxjWyi22srciI9tXfg5nWpl05m5+19uryaN9aQZe2gHk0hQEVX xtClCLF1/YnncgDx+MYu96JhLzJJ8i2YTKKi3M4aT7PwuzJ3AaNhGlsW5f/sy3zYx4w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796405977; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=X0TqF7uR9BJVe6ZmyFBghKd1Ovz8mhaI6wHoayF7WUQ=; b=hHuW5oLzmYYTGUwOTfbVd5zv8aCa1fdBiuOF1iJ1Uk6v7hY0Ip9f6hNwdPPZ189c3J5V 9pspJ9mSsgjjoJsSdHCip5CSqew1mDRAU4y5vcf4633hlgxha04Svz9FJyzexPxh9CJXC BEO5GOehkqrkw52hXVtXOsN9FFpong1eOAwQ7YDtb1gXX6+LmEHvuYYHDUHZJFLyLQRfY OvMKSbcQKWEtKA8wA1Le7OdN4EeQm8lCCrVBpkuu2k+5H/8wsnKOxjR0qc3sgEnetVBKB YehrF55ALJbFOQjVi6fYDmNO0bqB0D7BRl3Yk22iqYTfuwP0x5avyjenEsIq/cnJUHQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass header.d=cmpxchg.org header.i=@cmpxchg.org header.a=rsa-sha256 header.s=google header.b="i/+0i+c3"; arc=none smtp.remote-ip=209.85.128.175 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (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 EB666606A32F for ; Fri, 4 Dec 2026 17:39:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EB666606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.a=rsa-sha256 header.s=google header.b=i/+0i+c3 Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-8b503e2a87bso12277967b3.3 for ; Fri, 04 Dec 2026 09:39:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1796405976; x=1797010776; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=X0TqF7uR9BJVe6ZmyFBghKd1Ovz8mhaI6wHoayF7WUQ=; b=i/+0i+c3P+VAtiEA4HonFQ/ibVcYxyn5G2/4XGl8k/x5Sl/l5r3rxxxhLkM0pHD9ee IeTNkSp7awv6yr2k8bfCqmlTTna6dtL4XbaLtmk4+b2zLXEEziqreVWBPmb6iReGuBKR y2GFuD4mhoCVEceIPBs13J9IoSkXa2hYFRCSFJjGAWdr8NtPX+wpFiq5VmijbN8aVY05 6s3SmDrYYr5Yg6Lq8BolzbUCL/nWUrOmEA18N0PopiSH2nZqz1QbG99BzvVe78WItsm3 aGH92dEtFQjlPKqUh/sco9BpwWAHxQNsCG/f65+j/k4TvWOGN4AD5SyZ9Dpod9r1A4yO XiwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796405976; x=1797010776; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=X0TqF7uR9BJVe6ZmyFBghKd1Ovz8mhaI6wHoayF7WUQ=; b=ahjlssf3Q9tybRjBBfos1sm4FV/0HeJc2T2CUksDp7AciXogFaebWuGKrAFsN2fwG7 q/8yAIBv6BMXsBqy0hJW86ghB5vx1ySm58WNmTDdQWRh8yPduLzK0BoIpkK39I4UlCkf 7j7/L1+lk6uzQGfRkPoMs5pUI1ok+U3qZ3XQhcLdmds9ykTapK0Rr8MPn/MUQXWyKO8O HRhinzqsVgtQ2YpMGQcWn0jTjm1ziEwJCEpVN8kB3Z97tsAgZnCp0Xit2/5JF09NSxyS EBOp4n6k9gDRC5ZIO5fevv8EU6Sbu+AjU7HasnOUAGLgRm5Hg87TR6gI8G9fNRKezWdv mzrw== X-Forwarded-Encrypted: i=1; AL+hErGoMU3A7YXHOdwCFNWhkcYUazBq3kwF+fIncFicmTGNBm3ls35BLyXN6RhJlOukoW419OyxpdtPVigs0bY=@vger.kernel.org X-Gm-Message-State: AB4q+uF0chEyZ4e0kTtolbE53zVH5rb5ge8hhTBn/cmJ894ALFQ36q/g VqW7Nu/B7U8TnaG6kA6f52v0ziE05qoFYeqOubWX04RuYIoKPx7DZFMXxmAsF5Ny3j4= X-Gm-Gg: AZFdTZfa0m5aNStXnMCj+ADjTvuDDOIQCSzH24MqGarEe0O49WHGA8qNHPhrK7WClmE 6vTToUCOIwZAQeVUHoJnbIPBPCNk9YzJnDa8QlL8dPaI9LgF1ATaI0hXAYx/UCBHRY2ehS3hEwe wDM/ZWYI+XCll2iA8ssEKWqAAgqQVcng+JYAZr1HwpAh9NBx63rRPq/i7+7B4135jFcq7AAg441 etmqfJMfxOa1Tk5qXvjEtHOkI79VBRFuG+cmPDZMVZh4vW2PvyspROoe61bm9jCYICp04qE38dc qa/Pi7NmshqZiHwfw15EqrucuN7jt2qSLKY+cLmx/g40PSCvlkipE1Y/6FBvOvXSSPpXplhbCad /gPmmX1vgyVwN9XcXvNJpkIxWGCLF+2li/YKC1yS59Cf5nfj2I/9LoBlWyWLIgs/8qEpQt6wvkM fZT/Ob9FhsSWNh2rkJBEeQC9And2MiJRVzanMD9DQdPuLvIZ/Qqfjp1SPrRNdcH68VvHwkLxPUj cYNrX4tbx9VFSztSOTjDAJShaMyXAYePcPqwJihwdrgdLnd5P6CgkbGQf6xE71qPer1ZHC1u+RW l33uEDZ5XPzPFk95SBP9XtjsiKk= X-Received: by 2002:a05:690c:c87:b0:8b4:d6de:975a with SMTP id 00721157ae682-8b6606629cfmr13186977b3.17.1796405975802; Fri, 04 Dec 2026 09:39:35 -0800 (PST) Received: from localhost ([2605:8600:200:dc6:365a:60ff:fe62:ff29]) by smtp.gmail.com with ESMTPSA id 00721157ae682-8b66086625dsm12844347b3.14.2026.12.04.09.39.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 09:39:35 -0800 (PST) From: Johannes Weiner To: Andrew Morton Cc: Chris Mason , David Hildenbrand , Michal Hocko , Roman Gushchin , Shakeel Butt , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Baolin Wang , Dave Chinner , Harry Yoo , Hugh Dickins , Kairui Song , Matthew Wilcox , Muchun Song , Qi Zheng , Qinyun Tan , Vlastimil Babka , Zi Yan Subject: [PATCH 2/2] mm: list_lru: charge per-memcg lists and XArray nodes Date: Fri, 4 Dec 2026 12:39:29 -0500 Message-ID: <20261204173929.2355221-2-hannes@cmpxchg.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204173929.2355221-1-hannes@cmpxchg.org> References: <20261204173929.2355221-1-hannes@cmpxchg.org> 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 From: Chris Mason If many child cgroups each create files on many filesystems, they can consume several times their parent's memory.max in kernel memory that is not charged to any memory cgroup. A memcg-aware list_lru needs one struct list_lru_memcg per memory cgroup, and each superblock has two such list_lrus: its dentry LRU and its inode LRU. __memcg_list_lru_alloc() allocates the struct when the cgroup first allocates an object for that list_lru, and stores it in the XArray lru->xa. The struct stays until the cgroup goes offline or the list_lru is destroyed, even if the object is freed right away. So N child cgroups that each create a file on each of M filesystems leave 2 * N * M structs behind, plus the XArray nodes that hold them. None of this memory is charged: __memcg_list_lru_alloc() masks __GFP_ACCOUNT out of its gfp mask, and lru->xa lacks XA_FLAGS_ACCOUNT. Charge the structs and the XArray nodes to the cgroup they belong to. __memcg_list_lru_alloc() makes that cgroup the active memcg for the duration and adds __GFP_ACCOUNT to its gfp mask, so the struct and the nodes, which the XArray allocates itself, are billed to it rather than to whichever task is running. This covers every list_lru, whether the object came from kmem_cache_alloc_lru() or the caller allocated the lists directly. The deferred split lists are allocated right after a large folio was charged. They now take the folio's gfp mask instead of GFP_KERNEL, so a failed charge fails the way the folio's would, and the fault falls back to smaller pages instead of invoking the OOM killer. Cc: Baolin Wang Cc: Dave Chinner Cc: Harry Yoo Cc: Hugh Dickins Cc: Kairui Song Cc: Matthew Wilcox Cc: Michal Hocko Cc: Muchun Song Cc: Qi Zheng Cc: Qinyun Tan Cc: Roman Gushchin Cc: Shakeel Butt Cc: Vlastimil Babka Cc: Zi Yan Signed-off-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) [hannes@cmpxchg.org: charge all callers, pass the folio's gfp for the deferred split lists] Signed-off-by: Johannes Weiner --- include/linux/huge_mm.h | 4 ++-- mm/huge_memory.c | 7 ++++--- mm/khugepaged.c | 2 +- mm/list_lru.c | 21 ++++++++++++++------- mm/memory.c | 2 +- mm/migrate_device.c | 2 +- mm/swap_state.c | 2 +- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index fac05120ab77..6dd1e2d30f56 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -424,7 +424,7 @@ static inline int split_huge_page(struct page *page) return split_huge_page_to_list_to_order(page, NULL, 0); } -int folio_memcg_alloc_deferred(struct folio *folio); +int folio_memcg_alloc_deferred(struct folio *folio, gfp_t gfp); void deferred_split_folio(struct folio *folio, bool partially_mapped); @@ -678,7 +678,7 @@ static inline int folio_split(struct folio *folio, unsigned int new_order, return -EINVAL; } -static inline int folio_memcg_alloc_deferred(struct folio *folio) +static inline int folio_memcg_alloc_deferred(struct folio *folio, gfp_t gfp) { return 0; } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d065b26a94df..329da86c388e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1048,11 +1048,12 @@ static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj) } #endif /* CONFIG_SYSFS */ -int folio_memcg_alloc_deferred(struct folio *folio) +int folio_memcg_alloc_deferred(struct folio *folio, gfp_t gfp) { if (mem_cgroup_disabled()) return 0; - return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, GFP_KERNEL); + /* The folio's gfp: a failed charge falls back the way the folio's does. */ + return folio_memcg_list_lru_alloc(folio, &deferred_split_lru, gfp); } static int __init thp_shrinker_init(void) @@ -1366,7 +1367,7 @@ static struct folio *vma_alloc_anon_folio_pmd(struct vm_area_struct *vma, return NULL; } - if (folio_memcg_alloc_deferred(folio)) { + if (folio_memcg_alloc_deferred(folio, gfp)) { folio_put(folio); count_vm_event(THP_FAULT_FALLBACK); count_mthp_stat(order, MTHP_STAT_ANON_FAULT_FALLBACK); diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 964f78c3856b..00e4270d5ec3 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1273,7 +1273,7 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, if (result != SCAN_SUCCEED) goto out_nolock; - if (folio_memcg_alloc_deferred(folio)) { + if (folio_memcg_alloc_deferred(folio, cc->policy.gfp)) { result = SCAN_ALLOC_HUGE_PAGE_FAIL; goto out_nolock; } diff --git a/mm/list_lru.c b/mm/list_lru.c index 5d4b660f9fe4..a00cd1f4ceec 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -481,7 +481,7 @@ static struct list_lru_memcg *memcg_init_list_lru_one(struct list_lru *lru, gfp_ static inline void memcg_init_list_lru(struct list_lru *lru, bool memcg_aware) { if (memcg_aware) - xa_init_flags(&lru->xa, XA_FLAGS_LOCK_IRQ); + xa_init_flags(&lru->xa, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT); lru->memcg_aware = memcg_aware; } @@ -591,10 +591,13 @@ static int __memcg_list_lru_alloc(struct mem_cgroup *memcg, { unsigned long flags; struct list_lru_memcg *mlru = NULL; - struct mem_cgroup *pos, *parent; + struct mem_cgroup *pos, *parent, *old_memcg; XA_STATE(xas, &lru->xa, 0); + int ret; - gfp &= GFP_RECLAIM_MASK; + /* Charge the heads and the XArray nodes to @memcg, not to current. */ + gfp = (gfp & GFP_RECLAIM_MASK) | __GFP_ACCOUNT; + old_memcg = set_active_memcg(memcg); /* * Because the list_lru can be reparented to the parent cgroup's * list_lru, we should make sure that this cgroup and all its @@ -614,8 +617,10 @@ static int __memcg_list_lru_alloc(struct mem_cgroup *memcg, if (!mlru) { mlru = memcg_init_list_lru_one(lru, gfp); - if (!mlru) - return -ENOMEM; + if (!mlru) { + ret = -ENOMEM; + goto out; + } } /* Set before the lists are published, and never changed after. */ mlru->memcg = pos; @@ -631,10 +636,12 @@ static int __memcg_list_lru_alloc(struct mem_cgroup *memcg, } while (xas_nomem(&xas, gfp)); } while (pos != memcg && !css_is_dying(&pos->css)); + ret = xas_error(&xas); if (unlikely(mlru)) kfree(mlru); - - return xas_error(&xas); +out: + set_active_memcg(old_memcg); + return ret; } int memcg_list_lru_alloc(struct mem_cgroup *memcg, struct list_lru *lru, diff --git a/mm/memory.c b/mm/memory.c index 7d09749c2676..74ee41255154 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5486,7 +5486,7 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf) folio_put(folio); goto next; } - if (order > 1 && folio_memcg_alloc_deferred(folio)) { + if (order > 1 && folio_memcg_alloc_deferred(folio, gfp)) { folio_put(folio); goto fallback; } diff --git a/mm/migrate_device.c b/mm/migrate_device.c index b4660eeb975a..f39a27e70dff 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -843,7 +843,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, goto abort; } - if (folio_memcg_alloc_deferred(folio)) { + if (folio_memcg_alloc_deferred(folio, gfp)) { count_vm_event(THP_FAULT_FALLBACK); count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_FALLBACK); ret = -ENOMEM; diff --git a/mm/swap_state.c b/mm/swap_state.c index d9e36c505398..9e0058fe3b5c 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -489,7 +489,7 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci, return ERR_PTR(-ENOMEM); } - if (order > 1 && folio_memcg_alloc_deferred(folio)) { + if (order > 1 && folio_memcg_alloc_deferred(folio, gfp)) { spin_lock(&ci->lock); __swap_cache_do_del_folio(ci, folio, entry, shadow); spin_unlock(&ci->lock); -- 2.56.1