From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796298361; b=HHlPc8zfPUB6eTkzoerTzYTY6+3W67p48/pimhgt638mNC1B85rXJN+3Tbe5jefg2hig CkDSp7rMPEBByokvgOj3+mEAso6jToxkNCsbE0p8Sg4kDbN84gVEEOhFQhZ0Bs0Tq6ilR lHKLG5wLmQahvquf/bdbsJ2451DKODhbmljYDmsb1KGap1EdB3hHyi7EoOX5j91FdwcXN +G2QO1ycX1EddZyPr9gu1Ns9qhfBwCh7iVgM51qW0GjCNOzpmlLqSko3W40EIqisuqTcS h7uBLVbDSCAlPXeDHGeX0St+ydp4Qy7MNaP/VhyHa2aMyxgos+IGn8glRepDitAnaNw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298361; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=5VCUoSbgLKX1E7QSrxwayyukwzrMmMytcfQ4SzkcMTs=; b=CObv+55/JUj8vRj8RZy8PbJTO0QmFWPSOVItDb6++4heo8bqjBd0JmoGfID9a1Y5trRw 7Hmz8pa3XpHJ7XDMKRoZ1ub3BwzI/po8LonEjdG9eeGPIZX57JPa+KpbctsxScXa1EbtC dkvAGIj3+gPrGI0wy8f0ZVghX9ukjyB+Zk2kXT6VA5FsbURZcX+j6LibEEKnrNQRRG4oh U735cUuw6BIW2QK1gpBb6EXPKu2IJgBqssOeS5SDb/AVao8pkB2yfWzjRYfRgClPU4etX 38WXSwle0Ga2oAuw4Xk3kTLruNq/dp1DOY8h+p8+1ozcVmlb8gBirRexP4A1XcAWXPg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GnPVBU0M; 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 772D8606A322; Thu, 3 Dec 2026 11:46:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=GnPVBU0M Received: by smtp.kernel.org (Postfix) with ESMTPSA id D438A1F00893; Thu, 3 Dec 2026 11:45:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796298361; bh=5VCUoSbgLKX1E7QSrxwayyukwzrMmMytcfQ4SzkcMTs=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=GnPVBU0MI318udT5vcxV1XJ8VgV7DoDXWkNCC/K+emWvBbkYIxe7KKWyaZvTZGYFj T/AGV7B0MK09Vsaz4pYrbf0Hfnc5NDp6xZMeXJ4oanRwtFIf8wZagrRapDPwvROCm7 YMRRySGQ2yvP/MjZe5RYmVTiKMgadZoZe71yw1v2EnofBnP4yF5zOkChv4LzllUs60 vJUzxnc7y7eQs3CCGvjaRCbKUVKaiQoCDWhbACKOBgJ/x1SLaeh0CEySPfrjQdRAQR UD9pZbQh7GdrqjXbuhwECFie1me50pqGb3IZ2tF5uA8jxbCdD9RX/OBlJCR21B+/NO K4auWzmQ471Zg== From: "David Hildenbrand (Arm)" Date: Thu, 03 Dec 2026 12:37:56 +0100 Subject: [PATCH v2 33/35] mm/hugetlb_cgroup: track in-use folios per cgroup X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261203-hugetlb_fixes_2-v2-33-9bbeabb555d2@kernel.org> References: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> In-Reply-To: <20261203-hugetlb_fixes_2-v2-0-9bbeabb555d2@kernel.org> To: Muchun Song , Oscar Salvador , Andrew Morton , Hongfu Li , Jinmeng Zhou , Mina Almasry , Davidlohr Bueso , Naoya Horiguchi , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Michal Hocko , Jonathan Corbet , Shuah Khan , Randy Dunlap , Vivek Kasireddy , Mel Gorman , Hugh Dickins , Baolin Wang , Dave Airlie , Jason Gunthorpe , Andy Whitcroft , Roman Gushchin , Barry Song , Andrea Arcangeli , Lorenzo Stoakes , Suren Baghdasaryan , Shakeel Butt , Pedro Falcato , Jann Horn , Guillaume Morin , Yang Shi , James Houghton , Rik van Riel , Harry Yoo , Lance Yang , Axel Rasmussen , Peter Xu , Sasha Levin , Kiryl Shutsemau , Christian Brauner , "Steven Rostedt (Google)" , Hongbo Li , Gregory Price , Zi Yan , Joshua Hahn , Rakie Kim , Ying Huang , Matthew Brost , Byungchul Park , Alistair Popple , Andi Kleen , David Rientjes , Joern Engel , "Aneesh Kumar K.V" Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Gerd Hoffmann , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Johannes Weiner , Chris Mason , Jiaqi Yan , "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7023; i=david@kernel.org; h=from:subject:message-id; bh=+HyBSH4G9pi9TuO472K9O4sHV5hUvQnSztHf6bReKQk=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLIFQ2pv5RrNaY5lkp0qdv2C+G2zfae/FE/a+qx52TrJ4 1e+B63j7yhlYRDjYJAVU2QJCr5v8PLlutpqNz5mmDmsTCBDGLg4BWAiDiIM/wNjpzh5/DlR0Bb/ THuDeOneTUqVW76FGxwKfJ+b2vCYh4WRoefQwfM9dw4yPa6dtOTImccVay731Qjs95E4Jxr4fEI jAwsA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason 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. Fixes: da1def55919f ("hugetlb/cgroup: add support for cgroup removal") Signed-off-by: Chris Mason [ david: add lock checks, add HUGETLB_CGROUP_OFFLINE_BATCH, simplify comments and description ] Signed-off-by: David Hildenbrand (Arm) --- include/linux/hugetlb_cgroup.h | 20 ++++++++++++++++++++ mm/hugetlb.c | 4 ++-- mm/hugetlb_cgroup.c | 34 ++++++++++++++++------------------ 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/include/linux/hugetlb_cgroup.h b/include/linux/hugetlb_cgroup.h index b4e0958eabf3b..1c437eb2e1c30 100644 --- a/include/linux/hugetlb_cgroup.h +++ b/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_rsvd(struct folio *folio, __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_rsvd(struct folio *folio, { } +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; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9c85fc5790f3a..1b12277cd3f0a 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7647,7 +7647,7 @@ void folio_putback_hugetlb(struct folio *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); } @@ -7723,7 +7723,7 @@ void move_hugetlb_state(struct folio *old_folio, struct folio *new_folio, */ 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); } diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c index 6921b18572666..6ee771334a3bd 100644 --- a/mm/hugetlb_cgroup.c +++ b/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 hugetlb_cgroup *h_cgroup, 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(int idx, struct hugetlb_cgroup *h_cg, { 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(int idx, struct hugetlb_cgroup *h_cg, 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 @@ static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg, 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_charge(int idx, unsigned long nr_pages, */ 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 *old_folio, struct folio *new_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 *old_folio, struct folio *new_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); } -- 2.56.1