Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796325268; b=g42PHEHZ0bQhawtOwFL84aB0II8bIJvUKVzlpJ3uE7vcfE/n52kQwBG22WGTazj3mDcY D2ko0ewvswANyThb6sCVy9iz/WWpgCt3v8d8B+MXQvSLFULzchXK5j4O0GuZHW88hTdgu e1e6t42P69nXvPQazs4XvTnv7gfTS0QjcrBzlqWtBYgK8uhqiooV32DxVc+/+T8YsuOWq IMIu4FnVyCYbGCxDxKb08/qK/FM7ATXq4QyUmaRXdUZklol1GgTrBwpHCCmGEaHsbaMzy l7Zf7PWDyumoGneFS+xzyXUkXZXBoUmpcTrYgPuGQs/Zh2wFa0+EPgvV8vYswrtAweQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796325268; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=NqpOz9O6kAnjDj/4d5XUTsuqeWAUVH/hxt+TGpd9HgU=; b=B1i2oSanT5DVs2AqawblvZQLN3dzLjZ+Nja0bKp/GDQ2Zy3Hfl3IV7FOQLr33V2QUB5i AdLCgxK1rwMUjM3BOdi6uFNb7spq0pWZGQ+TkO/17+w3ngKpYOXygtg2sfyBv9EAn6JYW T19kxCmxIdGsZW0Rrvtiw38AZkMlYzl/drnL2yiW2VWccKwKvdwzI+XmqEk4WEEhyuxK+ fGhKxqlZ+jlomjHOPlC1T8VxRoUdix3HK4DtqkLSOuQVhpnJhjIvhsltoqUe7Q5Lksz9G 4rkBvzrk5bzkhlwhy3ou8QIxXUUh5Qyi9BcDKl35GKP5HzLcKRfF3wY6tsZ5ttdVgMA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=IK8fwAmV; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EF98606A32C for ; Thu, 3 Dec 2026 19:14:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=IK8fwAmV Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBDA21F000C2; Thu, 3 Dec 2026 19:14:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796325268; bh=NqpOz9O6kAnjDj/4d5XUTsuqeWAUVH/hxt+TGpd9HgU=; h=Date:To:From:In-Reply-To:References:Subject; b=IK8fwAmVy/XKjyHcqLZfkZL67MjRXhQ69aB2L+s3O2BEd2Hh0DcQajK0cO7PxE9sj Nyf5+F22CvLJLJEHWa8Dhb1Ksq5AZxaohHflLsnOrvax+yzE6j0XrZMSRnTrVcvGBN SFuMQKC4ILm9OEWBqEe5qMXiNUvV9qIZjW2dmmkc= Date: Thu, 03 Dec 2026 11:14:27 -0800 To: mm-commits@vger.kernel.org,mason@kernel.org,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261203-hugetlb_fixes_2-v2-33-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-33-9bbeabb555d2@kernel.org> Subject: + mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch added to mm-unstable branch Message-Id: <20261203191427.DBDA21F000C2@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: mm/hugetlb_cgroup: track in-use folios per cgroup has been added to the -mm mm-unstable branch. Its filename is mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Chris Mason Subject: mm/hugetlb_cgroup: track in-use folios per cgroup Date: Thu, 03 Dec 2026 12:37:56 +0100 When offlining a hugetlb cgroup, hugetlb_cgroup_css_offline() walks the list of all in-use hugetlb folios of each hstate with hugetlb_lock held and interrupts disabled, to reparent the folios charged to the dying cgroup. With many in-use hugetlb folios, that can keep interrupts disabled for a long time on every hugetlb cgroup removal, even for cgroups that never charged any hugetlb folios. Let's fix it by keeping in-use folios that are charged to a hugetlb cgroup on per-cgroup lists, so offlining only has to walk the folios of the dying cgroup. Process in batches to not keep interrupts disabled for too long when a single cgroup has many folios. Use batches of 32, like reclaim does with SWAP_CLUSTER_MAX. This has low urgency, as it only matters with a lot of hugetlb folios; let's not CC stable. [david@kernel.org: add lock checks, add HUGETLB_CGROUP_OFFLINE_BATCH, simplify comments and description] Link: https://patch.msgid.link/20261203-hugetlb_fixes_2-v2-33-9bbeabb555d2@kernel.org Fixes: da1def55919f ("hugetlb/cgroup: add support for cgroup removal") Signed-off-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Andrew Morton Cc: Muchun Song Cc: Oscar Salvador Cc: Hongfu Li Cc: Jinmeng Zhou Cc: Mina Almasry Cc: Davidlohr Bueso Cc: Naoya Horiguchi Cc: Liam R. Howlett Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Michal Hocko Cc: Jonathan Corbet Cc: Shuah Khan Cc: Randy Dunlap Cc: Vivek Kasireddy Cc: Mel Gorman Cc: Hugh Dickins Cc: Baolin Wang Cc: Dave Airlie Cc: Jason Gunthorpe Cc: Andy Whitcroft Cc: Roman Gushchin Cc: Barry Song Cc: Andrea Arcangeli Cc: Lorenzo Stoakes Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Pedro Falcato Cc: Jann Horn Cc: Guillaume Morin Cc: Yang Shi Cc: James Houghton Cc: Rik van Riel Cc: Harry Yoo Cc: Lance Yang Cc: Axel Rasmussen Cc: Peter Xu Cc: Sasha Levin Cc: Kiryl Shutsemau Cc: Christian Brauner Cc: Steven Rostedt Cc: Hongbo Li Cc: Gregory Price Cc: Zi Yan Cc: Joshua Hahn Cc: Rakie Kim Cc: Ying Huang Cc: Matthew Brost Cc: Byungchul Park Cc: Alistair Popple Cc: Andi Kleen Cc: David Rientjes Cc: Joern Engel Cc: Aneesh Kumar K.V Cc: Gerd Hoffmann Cc: Johannes Weiner Cc: Jiaqi Yan include/linux/hugetlb_cgroup.h | 20 ++++++++++++++++++ mm/hugetlb.c | 4 +-- mm/hugetlb_cgroup.c | 34 ++++++++++++++----------------- 3 files changed, 38 insertions(+), 20 deletions(-) --- a/include/linux/hugetlb_cgroup.h~mm-hugetlb_cgroup-track-in-use-folios-per-cgroup +++ a/include/linux/hugetlb_cgroup.h @@ -54,6 +54,9 @@ struct hugetlb_cgroup { /* Handle for "hugetlb.events.local" */ struct cgroup_file events_local_file[HUGE_MAX_HSTATE]; + /* In-use folios whose usage is charged here, under hugetlb_lock. */ + struct list_head activelist[HUGE_MAX_HSTATE]; + struct hugetlb_cgroup_per_node *nodeinfo[]; }; @@ -100,6 +103,17 @@ static inline void set_hugetlb_cgroup_rs __set_hugetlb_cgroup(folio, h_cg, true); } +static inline struct list_head *hugetlb_folio_activelist(struct folio *folio) +{ + struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_folio(folio); + struct hstate *h = folio_hstate(folio); + + lockdep_assert_held(&hugetlb_lock); + if (!h_cg) + return &h->hugepage_activelist; + return &h_cg->activelist[hstate_index(h)]; +} + static inline bool hugetlb_cgroup_disabled(void) { return !cgroup_subsys_enabled(hugetlb_cgrp_subsys); @@ -185,6 +199,12 @@ static inline void set_hugetlb_cgroup_rs { } +static inline struct list_head *hugetlb_folio_activelist(struct folio *folio) +{ + lockdep_assert_held(&hugetlb_lock); + return &folio_hstate(folio)->hugepage_activelist; +} + static inline bool hugetlb_cgroup_disabled(void) { return true; --- a/mm/hugetlb.c~mm-hugetlb_cgroup-track-in-use-folios-per-cgroup +++ a/mm/hugetlb.c @@ -7622,7 +7622,7 @@ void folio_putback_hugetlb(struct folio { spin_lock_irq(&hugetlb_lock); folio_set_hugetlb_migratable(folio); - list_move_tail(&folio->lru, &(folio_hstate(folio))->hugepage_activelist); + list_move_tail(&folio->lru, hugetlb_folio_activelist(folio)); spin_unlock_irq(&hugetlb_lock); folio_put(folio); } @@ -7698,7 +7698,7 @@ void move_hugetlb_state(struct folio *ol */ spin_lock_irq(&hugetlb_lock); folio_set_hugetlb_migratable(new_folio); - list_move_tail(&new_folio->lru, &(folio_hstate(new_folio))->hugepage_activelist); + list_move_tail(&new_folio->lru, hugetlb_folio_activelist(new_folio)); spin_unlock_irq(&hugetlb_lock); } --- a/mm/hugetlb_cgroup.c~mm-hugetlb_cgroup-track-in-use-folios-per-cgroup +++ a/mm/hugetlb_cgroup.c @@ -28,6 +28,8 @@ #define DFL_TMPL_SIZE ARRAY_SIZE(hugetlb_dfl_tmpl) #define LEGACY_TMPL_SIZE ARRAY_SIZE(hugetlb_legacy_tmpl) +#define HUGETLB_CGROUP_OFFLINE_BATCH 32 + static struct hugetlb_cgroup *root_h_cgroup __read_mostly; static struct cftype *dfl_files; static struct cftype *legacy_files; @@ -123,6 +125,8 @@ static void hugetlb_cgroup_init(struct h VM_WARN_ON_ONCE(ret); ret = page_counter_set_max(rsvd, limit); VM_WARN_ON_ONCE(ret); + + INIT_LIST_HEAD(&h_cgroup->activelist[idx]); } } @@ -219,18 +223,8 @@ static void hugetlb_cgroup_move_parent(i { unsigned int nr_pages; struct page_counter *counter; - struct hugetlb_cgroup *hcg; struct hugetlb_cgroup *parent = parent_hugetlb_cgroup(h_cg); - hcg = hugetlb_cgroup_from_folio(folio); - /* - * We can have pages in active list without any cgroup - * ie, hugepage with less than 3 pages. We can safely - * ignore those pages. - */ - if (!hcg || hcg != h_cg) - goto out; - nr_pages = folio_nr_pages(folio); if (!parent) { parent = root_h_cgroup; @@ -244,8 +238,7 @@ static void hugetlb_cgroup_move_parent(i hugetlb_cgroup_move_usage(h_cg, parent, folio, folio); set_hugetlb_cgroup(folio, parent); -out: - return; + list_move(&folio->lru, hugetlb_folio_activelist(folio)); } /* @@ -255,15 +248,20 @@ out: static void hugetlb_cgroup_css_offline(struct cgroup_subsys_state *css) { struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_css(css); + struct folio *folio, *next; struct hstate *h; - struct folio *folio; do { for_each_hstate(h) { - spin_lock_irq(&hugetlb_lock); - list_for_each_entry(folio, &h->hugepage_activelist, lru) - hugetlb_cgroup_move_parent(hstate_index(h), h_cg, folio); + int idx = hstate_index(h); + unsigned int nr = 0; + spin_lock_irq(&hugetlb_lock); + list_for_each_entry_safe(folio, next, &h_cg->activelist[idx], lru) { + hugetlb_cgroup_move_parent(idx, h_cg, folio); + if (++nr == HUGETLB_CGROUP_OFFLINE_BATCH) + break; + } spin_unlock_irq(&hugetlb_lock); } cond_resched(); @@ -367,6 +365,7 @@ static void __hugetlb_cgroup_commit_char */ WRITE_ONCE(h_cg->nodeinfo[folio_nid(folio)]->usage[idx], usage + nr_pages); + list_move(&folio->lru, hugetlb_folio_activelist(folio)); } out: if (!rsvd && charged_h_cg) @@ -940,7 +939,6 @@ void hugetlb_cgroup_migrate(struct folio { struct hugetlb_cgroup *h_cg; struct hugetlb_cgroup *h_cg_rsvd; - struct hstate *h = folio_hstate(old_folio); if (hugetlb_cgroup_disabled()) return; @@ -957,7 +955,7 @@ void hugetlb_cgroup_migrate(struct folio hugetlb_cgroup_move_usage(h_cg, h_cg, old_folio, new_folio); - list_move(&new_folio->lru, &h->hugepage_activelist); + list_move(&new_folio->lru, hugetlb_folio_activelist(new_folio)); spin_unlock_irq(&hugetlb_lock); } _ Patches currently in -mm which might be from mason@kernel.org are mm-swap-fix-large-swap-in-across-swap-file-extents.patch hugetlbfs-do-not-grow-a-sealed-file-in-mmap.patch mm-hugetlb-keep-surplus-folios-that-are-reserved.patch mm-hugetlb-fix-reservation-leak-on-min_size=-mounts.patch mm-cachestat-count-each-page-only-once.patch mm-cachestat-restart-the-walk-after-each-entry.patch percpu-queue-the-balance-work-on-an-unbound-workqueue.patch mm-vma-fix-uncharge-after-failed-stack-relocation.patch mm-mremap-apply-the-mmap-offset-limits-on-expansion.patch mm-mremap-respect-guard-gaps-on-in-place-expansion.patch mm-vma-make-brk-mappings-non-executable-under-mdwe.patch kfence-use-a-nofault-store-to-zero-the-guard-page.patch mm-filemap-update-the-folio-order-range-atomically.patch mm-userfaultfd-account-memory-allocated-for-unmap-events.patch mm-userfaultfd-drop-fork-context-reference-outside-event_wqhlock.patch mm-madvise-dont-declare-oom-for-madv_willneed-swapin.patch mm-mprotect-fix-unpaired-notifier-start-at-address-0.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault.patch mm-mglru-count-all-generations-as-evictable.patch mm-mglru-test-pg_lru-before-taking-a-folio-reference.patch mm-mglru-report-root-reclaim-to-memorypressure_level.patch mm-hugetlb-fix-truncated-copy-result-in-uffdio_copy.patch hugetlbfs-fix-deadlock-between-hole-punching-and-memory-failure.patch hugetlbfs-recheck-the-vma-range-after-the-lock-wait.patch mm-hugetlb-dont-zap-non-present-entries-when-unmapping-a-folio.patch hugetlbfs-seal-shm_hugetlb-files-against-growth.patch mm-hugetlb-unshare-pmds-of-a-split-vma-if-allocating-its-lock-fails.patch mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch