mm/hugetlb: various fixes (2)
Subsystems (13)
Re: [PATCH v2 31/35] mm/hugetlb: count migration entries at fork and unmap
In reply to David Hildenbrand (Arm)Quoted 13 lines · David Hildenbrand (Arm)
HugetlbPages in /proc/<pid>/status can be wrong, and even drop below zero, if we
fork()or unmap while mapped hugetlb folios are being migrated.There is an inconsistency: when installing migration entries we don't adjust the counter, treating them as if they were present. However, when copying or unmapping a migration entry, we don't adjust the counter like we would for a present entry.
Let's fix it by adjusting the counter also for migration entries in these cases. Add
pte_is_migration_entry()like we did for other softleaf types already.
Quoted 6 lines
This has low urgency and given that this has been around forever and likely nobody cares, let's not CC stable.
Fixes 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM:
Signature · 2 lines
cheers, jose a. p-a
David Hildenbrand (Arm)
maintainerPATCH v2 32/35 mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared
In reply to David Hildenbrand (Arm)Sharing hugetlb PMD tables can leave HugetlbPages in /proc/<pid>/status too low or too high, or make it underflow: when (un)mapping a folio in a shared PMD table, we only adjust mm->hugetlb_usage of the MM we process, not of the other sharers.
Accounting this precisely is not worth it for a statistic, so let's just give up: once an MM shares a PMD table, set mm->hugetlb_usage of both MMs to LONG_MIN / 2, and report negative values as 0. Later updates cannot realistically make it positive again, as PMD table sharing is 64-bit only.
Document that HugetlbPages is 0 once a process shared hugetlb PMD page tables.
This has low urgency, as it only affects a statistic. Let's not CC stable.
--- Documentation/filesystems/proc.rst | 3 ++- mm/hugetlb.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-)
Documentation/filesystems/proc.rst +2 -1
@@ -268,7 +268,8 @@ It's slow but very precise. VmPTE size of page table entries VmSwap amount of swap used by anonymous private data (shmem swap usage is not included) - HugetlbPages size of hugetlb memory portions + HugetlbPages size of hugetlb memory portions (0 once the + process shared hugetlb PMD page tables) CoreDumping process's memory is currently being dumped (killing the process may lead to a corrupted core) THP_enabled process is allowed to use THP (returns 0 when
mm/hugetlb.c +8 -1
@@ -4766,8 +4766,9 @@ void hugetlb_show_meminfo_node(int nid)void hugetlb_report_usage (struct seq_file *m,struct mm_struct *mm) { +/* Negative once the MM shared PMD tables, see huge_pmd_share(). */ seq_printf (m,"HugetlbPages:\t%8lu kB\n" , -K (atomic_long_read (&mm->hugetlb_usage))); +K (max (atomic_long_read (&mm->hugetlb_usage),0L ))); }/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
@@ -7349,6 +7350,12 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma, (pmd_t *)((unsigned long)spte & PAGE_MASK));hugetlbfs_pmd_sharing_inc (file_inode (vma->vm_file));mm_inc_nr_pmds (mm); +/* +* (Un)mapping folios with shared PMD tables would require +* updating statistics of all MMs. Instead, give up accounting. +*/ +atomic_long_set (&mm->hugetlb_usage,LONG_MIN /2 ); +atomic_long_set (&svma->vm_mm->hugetlb_usage,LONG_MIN /2 ); }else {ptdesc_pmd_pts_dec (virt_to_ptdesc (spte)); }
Signature · 1 line
2.56.1
+ mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch added to mm-unstable branch
The patch titled Subject: mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared has been added to the -mm mm-unstable branch. Its filename is…
The patch titled
Subject: mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.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: "David Hildenbrand (Arm)" <[email protected]>
Subject: mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared
Date: Thu, 03 Dec 2026 12:37:55 +0100
Sharing hugetlb PMD tables can leave HugetlbPages in /proc/<pid>/status too low or too high, or make it underflow: when (un)mapping a folio in a shared PMD table, we only adjust mm->hugetlb_usage of the MM we process, not of the other sharers.
Accounting this precisely is not worth it for a statistic, so let's just give up: once an MM shares a PMD table, set mm->hugetlb_usage of both MMs to LONG_MIN / 2, and report negative values as 0. Later updates cannot realistically make it positive again, as PMD table sharing is 64-bit only.
Document that HugetlbPages is 0 once a process shared hugetlb PMD page tables.
This has low urgency, as it only affects a statistic. Let's not CC stable.
Documentation/filesystems/proc.rst | 3 ++- mm/hugetlb.c | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-)
Documentation/filesystems/proc.rst renamed from Documentation/filesystems/proc.rst~mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared +2 -1
@@ -268,7 +268,8 @@ It's slow but very precise. VmPTE size of page table entries VmSwap amount of swap used by anonymous private data (shmem swap usage is not included) - HugetlbPages size of hugetlb memory portions + HugetlbPages size of hugetlb memory portions (0 once the + process shared hugetlb PMD page tables) CoreDumping process's memory is currently being dumped (killing the process may lead to a corrupted core) THP_enabled process is allowed to use THP (returns 0 when
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared +8 -1
@@ -4766,8 +4766,9 @@ void hugetlb_show_meminfo_node(int nid)void hugetlb_report_usage (struct seq_file *m,struct mm_struct *mm) { +/* Negative once the MM shared PMD tables, see huge_pmd_share(). */ seq_printf (m,"HugetlbPages:\t%8lu kB\n" , -K (atomic_long_read (&mm->hugetlb_usage))); +K (max (atomic_long_read (&mm->hugetlb_usage),0L ))); }/* Return the number pages of memory we physically have, in PAGE_SIZE units. */
@@ -7324,6 +7325,12 @@ pte_t *huge_pmd_share(struct mm_struct * (pmd_t *)((unsigned long)spte & PAGE_MASK));hugetlbfs_pmd_sharing_inc (file_inode (vma->vm_file));mm_inc_nr_pmds (mm); +/* +* (Un)mapping folios with shared PMD tables would require +* updating statistics of all MMs. Instead, give up accounting. +*/ +atomic_long_set (&mm->hugetlb_usage,LONG_MIN /2 ); +atomic_long_set (&svma->vm_mm->hugetlb_usage,LONG_MIN /2 ); }else {ptdesc_pmd_pts_dec (virt_to_ptdesc (spte)); }
_
Patches currently in -mm which might be from [email protected] are
mm-mprotect-dont-skip-uffd-marker-install-when-racing-with-pte-table-reclaim.patch mm-userfaultfd-prevent-poison-marker-loss.patch mm-hugetlb-compare-against-persistent-count-when-freeing-lowmem-folios.patch mm-hugetlb-preserve-reservations-when-shrinking-on-highmem.patch mm-hugetlb-account-temporary-folios-separately.patch mm-hugetlb-account-temporary-folios-separately-fix.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation-fix.patch mm-memfd-recheck-the-page-cache-before-allocating-a-hugetlb-folio.patch mm-hugetlb-count-again-if-the-reserve-map-shrank.patch mm-hugetlb-account-restored-reservations-before-freeing-folios.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush.patch mm-userfaultfd-reject-dax-mappings.patch mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.patch mm-hugetlb-restore-the-reservation-when-racing-with-another-reservation.patch mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages.patch mm-hugetlb-keep-hpage_resv_unmapped-when-forking.patch mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch mm-memfd-mark-new-hugetlb-folios-migratable.patch mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation.patch mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it.patch fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch hugetlbfs-fix-integer-overflow-when-punching-holes.patch hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate.patch hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit.patch hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation.patch mm-hugetlb-serialize-private-faults-with-truncation.patch mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison.patch hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes.patch mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas.patch mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch mm-hugetlb_cgroup-charge-faults-to-mm-owner.patch
Re: [PATCH v2 32/35] mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared
In reply to David Hildenbrand (Arm)Quoted 18 lines · David Hildenbrand (Arm)
Sharing hugetlb PMD tables can leave HugetlbPages in /proc/<pid>/status too low or too high, or make it underflow: when (un)mapping a folio in a shared PMD table, we only adjust
mm->hugetlb_usageof the MM we process, not of the other sharers.Accounting this precisely is not worth it for a statistic, so let's just give up: once an MM shares a PMD table, set
mm->hugetlb_usageof both MMs toLONG_MIN/ 2, and report negative values as 0. Later updates cannot realistically make it positive again, as PMD table sharing is 64-bit only.Document that HugetlbPages is 0 once a process shared hugetlb PMD page tables.
Quoted 6 lines
This has low urgency, as it only affects a statistic. Let's not CC stable.
Fixes 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks.
Re: [PATCH v2 32/35] mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared
In reply to David Hildenbrand (Arm)Quoted 14 lines · David Hildenbrand (Arm)
Sharing hugetlb PMD tables can leave HugetlbPages in /proc/<pid>/status too low or too high, or make it underflow: when (un)mapping a folio in a shared PMD table, we only adjust
mm->hugetlb_usageof the MM we process, not of the other sharers.Accounting this precisely is not worth it for a statistic, so let's just give up: once an MM shares a PMD table, set
mm->hugetlb_usageof both MMs toLONG_MIN/ 2, and report negative values as 0. Later updates cannot realistically make it positive again, as PMD table sharing is 64-bit only.Document that HugetlbPages is 0 once a process shared hugetlb PMD page tables.
Quoted 6 lines
This has low urgency, as it only affects a statistic. Let's not CC stable.
Fixes 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM:
Signature · 2 lines
cheers, jose a. p-a
David Hildenbrand (Arm)
maintainerPATCH v2 33/35 mm/hugetlb_cgroup: track in-use folios per cgroup
In reply to David Hildenbrand (Arm)From: Chris Mason <[email protected]>
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: add lock checks, add HUGETLB_CGROUP_OFFLINE_BATCH, simplify
comments and description ]
--- include/linux/hugetlb_cgroup.h | 20 ++++++++++++++++++++ mm/hugetlb.c | 4 ++-- mm/hugetlb_cgroup.c | 34 ++++++++++++++++------------------ 3 files changed, 38 insertions(+), 20 deletions(-)
include/linux/hugetlb_cgroup.h +20 -0
@@ -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;
mm/hugetlb.c +2 -2
@@ -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); }
mm/hugetlb_cgroup.c +16 -18
@@ -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); }
Signature · 1 line
2.56.1
+ mm-hugetlb_cgroup-track-in-use-folios-per-cgroup.patch added to mm-unstable branch
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
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 <[email protected]>
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.
include/linux/hugetlb_cgroup.h | 20 ++++++++++++++++++ mm/hugetlb.c | 4 +-- mm/hugetlb_cgroup.c | 34 ++++++++++++++----------------- 3 files changed, 38 insertions(+), 20 deletions(-)
include/linux/hugetlb_cgroup.h renamed from include/linux/hugetlb_cgroup.h~mm-hugetlb_cgroup-track-in-use-folios-per-cgroup +20 -0
@@ -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;
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb_cgroup-track-in-use-folios-per-cgroup +2 -2
@@ -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); }
mm/hugetlb_cgroup.c renamed from mm/hugetlb_cgroup.c~mm-hugetlb_cgroup-track-in-use-folios-per-cgroup +16 -18
@@ -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 hVM_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(ihugetlb_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 foliohugetlb_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 [email protected] 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
Re: [PATCH v2 33/35] mm/hugetlb_cgroup: track in-use folios per cgroup
In reply to David Hildenbrand (Arm)Quoted 17 lines · David Hildenbrand (Arm)
From: Chris Mason <[email protected]>
When offlining a hugetlb cgroup,
hugetlb_cgroup_css_offline()walks the list of all in-use hugetlb folios of each hstate withhugetlb_lockheld 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.
Quoted 8 lines
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 <[email protected]>[ david: add lock checks, add
HUGETLB_CGROUP_OFFLINE_BATCH, simplify
comments and description ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
LGTM:
Signature · 2 lines
cheers, jose a. p-a
David Hildenbrand (Arm)
maintainerPATCH v2 34/35 mm/hugetlb_cgroup: charge faults to mm->owner
In reply to David Hildenbrand (Arm)We can bypass hugetlb cgroup limits when a kernel thread faults in hugetlb pages for a user process: __hugetlb_cgroup_charge_cgroup() charges the hugetlb cgroup of current, and kernel threads usually live in the root cgroup, which has no limit.
For example, a KVM's async page fault repeats a fault of a vCPU that just failed at the limit in a kworker, which then charges the root cgroup:
vCPU thread kworker
=========== =======
__kvm_mmu_faultin_pfn()
hva_to_pfn(), FOLL_NOWAIT
hugetlb_alloc_folio()
charge fails
KVM_PFN_ERR_NEEDS_IO
kvm_setup_async_pf()
async_pf_execute()
get_user_pages_remote()
hugetlb_alloc_folio()
charges the root
Let's fix it by charging the hugetlb cgroup of mm->owner whenever we allocate or reserve for an mm, like memcg does. If we don't allocate for a specific mm, we use current->mm, again like memcg. Unlike memcg, which falls back to the root cgroup, we fail the charge if the mm has no owner (anymore), as we would otherwise bypass the limits again. As mm->owner depends on CONFIG_MEMCG, nothing changes without it.
This changes the charged hugetlb cgroup in these scenarios:
(1) Kernel threads that fault on a user mm, such as KVM async page faults, kvm_xen_set_evtchn() in the irqfd work item and iommu_sva_handle_mm(), now charge the cgroup of mm->owner instead of the root cgroup.
(2) vhost kthread workers, which join the cgroups of the task that
created them, now charge the cgroup of mm->owner.
(3) With cgroup v1, threads of a process that are in different hugetlb
cgroups now all charge the cgroup of mm->owner. (4) Processes that fault on the mm of another process, such as a userfaultfd monitor (UFFDIO_COPY) or remote GUP through ptrace, /proc/pid/mem or process_vm_writev(), now charge the cgroup of the target's mm->owner instead of their own cgroup.
... which should just be the right thing to do.
This has low urgency, as it only affects hugetlb cgroup limits. Let's not CC stable, as this might not be particularly easy to backport.
--- include/linux/hugetlb_cgroup.h | 10 +++++----- mm/hugetlb.c | 12 +++++++----- mm/hugetlb_cgroup.c | 32 +++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 17 deletions(-)
include/linux/hugetlb_cgroup.h +5 -5
@@ -139,9 +139,9 @@ static inline void resv_map_put_hugetlb_cgroup_uncharge_info( }extern int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr); +struct mm_struct *mm,struct hugetlb_cgroup **ptr);extern int hugetlb_cgroup_charge_cgroup_rsvd (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr); +struct mm_struct *mm,struct hugetlb_cgroup **ptr);extern void hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages,struct hugetlb_cgroup *h_cg,struct folio *folio);
@@ -225,14 +225,14 @@ static inline void resv_map_put_hugetlb_cgroup_uncharge_info( }static inline int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct mm_struct *mm,struct hugetlb_cgroup **ptr) {return 0 ; }static inline int hugetlb_cgroup_charge_cgroup_rsvd (int idx, -unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +unsigned long nr_pages,struct mm_struct *mm, +struct hugetlb_cgroup **ptr) {return 0 ; }
mm/hugetlb.c +7 -5
@@ -2312,7 +2312,7 @@ struct folio *alloc_hugetlb_folio_reserve(struct hstate *h, int preferred_nid,int idx =hstate_index (h);int ret; -if (hugetlb_cgroup_charge_cgroup (idx, nr_pages, &h_cg)) +if (hugetlb_cgroup_charge_cgroup (idx, nr_pages,NULL , &h_cg))return NULL ;spin_lock_irq (&hugetlb_lock);
@@ -3010,7 +3010,8 @@ void wait_for_freed_hugetlb_folios(void)* hugetlb_alloc_folio - Allocate a hugetlb folio. * @h: Hugetlb state control block. * @mpoli: Interpreted memory policy to use for allocation. -* @mm: Memory descriptor of the allocation target. +* @mm: Memory descriptor of the allocation target. With CONFIG_MEMCG, the +* hugetlb cgroup of the owner of @mm is charged. * @alloc_flags: Flags controlling the allocation behavior. * * Allocates a hugetlb folio and handles cgroup charging and global hstate
@@ -3036,10 +3037,10 @@ struct folio *hugetlb_alloc_folio(struct hstate *h,int ret;if (charge_hugetlb_cgroup_rsvd && -hugetlb_cgroup_charge_cgroup_rsvd (idx, nr_pages, &h_cg_rsvd)) +hugetlb_cgroup_charge_cgroup_rsvd (idx, nr_pages, mm, &h_cg_rsvd))return ERR_PTR (-ENOSPC ); -if (hugetlb_cgroup_charge_cgroup (idx, nr_pages, &h_cg)) { +if (hugetlb_cgroup_charge_cgroup (idx, nr_pages, mm, &h_cg)) { ret = -ENOSPC ; goto err_uncharge_hugetlb_cgroup_rsvd; }
@@ -7044,7 +7045,8 @@ long hugetlb_reserve_pages(struct inode *inode, } err =hugetlb_cgroup_charge_cgroup_rsvd (hstate_index (h), - chg *pages_per_huge_page (h), &h_cg); + chg *pages_per_huge_page (h), + vma ? vma->vm_mm :NULL , &h_cg);if (err <0 ) goto out_err;
mm/hugetlb_cgroup.c +25 -7
@@ -281,9 +281,22 @@ static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx, !hugetlb_cgroup_is_root(hugetlb)); } +static struct hugetlb_cgroup *hugetlb_cgroup_from_mm (struct mm_struct *mm) +{ +struct task_struct *task = current; + +#ifdef CONFIG_MEMCG +/* Like memcg, charge the owner of the mm we allocate for. */ +if (!mm) + mm = current->mm; +if (mm) + task =rcu_dereference (mm->owner); +#endif +return task ?hugetlb_cgroup_from_task (task) :NULL ; +} +static int __hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr, -bool rsvd) +struct mm_struct *mm,struct hugetlb_cgroup **ptr,bool rsvd) {int ret =0 ;struct page_counter *counter;
@@ -293,7 +306,12 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, goto done; again:rcu_read_lock (); - h_cg =hugetlb_cgroup_from_task (current); + h_cg =hugetlb_cgroup_from_mm (mm); +if (!h_cg) { +rcu_read_unlock (); + ret = -ENOMEM ; + goto done; + }if (!css_tryget (&h_cg->css)) {rcu_read_unlock (); goto again;
@@ -318,15 +336,15 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, }int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct mm_struct *mm,struct hugetlb_cgroup **ptr) { -return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, ptr, false); +return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, mm, ptr, false); }int hugetlb_cgroup_charge_cgroup_rsvd (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct mm_struct *mm,struct hugetlb_cgroup **ptr) { -return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, ptr, true); +return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, mm, ptr, true); }/* Should be called with hugetlb_lock held */
Signature · 1 line
2.56.1
+ mm-hugetlb_cgroup-charge-faults-to-mm-owner.patch added to mm-unstable branch
The patch titled
The patch titled
Subject: mm/hugetlb_cgroup: charge faults to mm->owner
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb_cgroup-charge-faults-to-mm-owner.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-charge-faults-to-mm-owner.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: "David Hildenbrand (Arm)" <[email protected]>
Subject: mm/hugetlb_cgroup: charge faults to mm->owner
Date: Thu, 03 Dec 2026 12:37:57 +0100
We can bypass hugetlb cgroup limits when a kernel thread faults in hugetlb pages for a user process: __hugetlb_cgroup_charge_cgroup() charges the hugetlb cgroup of current, and kernel threads usually live in the root cgroup, which has no limit.
For example, a KVM's async page fault repeats a fault of a vCPU that just failed at the limit in a kworker, which then charges the root cgroup:
vCPU thread kworker
=========== =======
__kvm_mmu_faultin_pfn()
hva_to_pfn(), FOLL_NOWAIT
hugetlb_alloc_folio()
charge fails
KVM_PFN_ERR_NEEDS_IO
kvm_setup_async_pf()
async_pf_execute()
get_user_pages_remote()
hugetlb_alloc_folio()
charges the root
Let's fix it by charging the hugetlb cgroup of mm->owner whenever we allocate or reserve for an mm, like memcg does. If we don't allocate for a specific mm, we use current->mm, again like memcg. Unlike memcg, which falls back to the root cgroup, we fail the charge if the mm has no owner (anymore), as we would otherwise bypass the limits again. As mm->owner depends on CONFIG_MEMCG, nothing changes without it.
This changes the charged hugetlb cgroup in these scenarios:
(1) Kernel threads that fault on a user mm, such as KVM async page faults, kvm_xen_set_evtchn() in the irqfd work item and iommu_sva_handle_mm(), now charge the cgroup of mm->owner instead of the root cgroup.
(2) vhost kthread workers, which join the cgroups of the task that
created them, now charge the cgroup of mm->owner.
(3) With cgroup v1, threads of a process that are in different hugetlb
cgroups now all charge the cgroup of mm->owner. (4) Processes that fault on the mm of another process, such as a userfaultfd monitor (UFFDIO_COPY) or remote GUP through ptrace, /proc/pid/mem or process_vm_writev(), now charge the cgroup of the target's mm->owner instead of their own cgroup.
... which should just be the right thing to do.
This has low urgency, as it only affects hugetlb cgroup limits. Let's not CC stable, as this might not be particularly easy to backport.
include/linux/hugetlb_cgroup.h | 10 ++++----- mm/hugetlb.c | 12 ++++++----- mm/hugetlb_cgroup.c | 32 ++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 17 deletions(-)
include/linux/hugetlb_cgroup.h renamed from include/linux/hugetlb_cgroup.h~mm-hugetlb_cgroup-charge-faults-to-mm-owner +5 -5
@@ -139,9 +139,9 @@ static inline void resv_map_put_hugetlb_ }extern int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr); +struct mm_struct *mm,struct hugetlb_cgroup **ptr);extern int hugetlb_cgroup_charge_cgroup_rsvd (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr); +struct mm_struct *mm,struct hugetlb_cgroup **ptr);extern void hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages,struct hugetlb_cgroup *h_cg,struct folio *folio);
@@ -225,14 +225,14 @@ static inline void resv_map_put_hugetlb_ }static inline int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct mm_struct *mm,struct hugetlb_cgroup **ptr) {return 0 ; }static inline int hugetlb_cgroup_charge_cgroup_rsvd (int idx, -unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +unsigned long nr_pages,struct mm_struct *mm, +struct hugetlb_cgroup **ptr) {return 0 ; }
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb_cgroup-charge-faults-to-mm-owner +7 -5
@@ -2312,7 +2312,7 @@ struct folio *alloc_hugetlb_folio_reservint idx =hstate_index (h);int ret; -if (hugetlb_cgroup_charge_cgroup (idx, nr_pages, &h_cg)) +if (hugetlb_cgroup_charge_cgroup (idx, nr_pages,NULL , &h_cg))return NULL ;spin_lock_irq (&hugetlb_lock);
@@ -3012,7 +3012,8 @@ void wait_for_freed_hugetlb_folios(void)* hugetlb_alloc_folio - Allocate a hugetlb folio. * @h: Hugetlb state control block. * @mpoli: Interpreted memory policy to use for allocation. -* @mm: Memory descriptor of the allocation target. +* @mm: Memory descriptor of the allocation target. With CONFIG_MEMCG, the +* hugetlb cgroup of the owner of @mm is charged. * @alloc_flags: Flags controlling the allocation behavior. * * Allocates a hugetlb folio and handles cgroup charging and global hstate
@@ -3038,10 +3039,10 @@ struct folio *hugetlb_alloc_folio(structint ret;if (charge_hugetlb_cgroup_rsvd && -hugetlb_cgroup_charge_cgroup_rsvd (idx, nr_pages, &h_cg_rsvd)) +hugetlb_cgroup_charge_cgroup_rsvd (idx, nr_pages, mm, &h_cg_rsvd))return ERR_PTR (-ENOSPC ); -if (hugetlb_cgroup_charge_cgroup (idx, nr_pages, &h_cg)) { +if (hugetlb_cgroup_charge_cgroup (idx, nr_pages, mm, &h_cg)) { ret = -ENOSPC ; goto err_uncharge_hugetlb_cgroup_rsvd; }
@@ -7019,7 +7020,8 @@ retry: } err =hugetlb_cgroup_charge_cgroup_rsvd (hstate_index (h), - chg *pages_per_huge_page (h), &h_cg); + chg *pages_per_huge_page (h), + vma ? vma->vm_mm :NULL , &h_cg);if (err <0 ) goto out_err;
mm/hugetlb_cgroup.c renamed from mm/hugetlb_cgroup.c~mm-hugetlb_cgroup-charge-faults-to-mm-owner +25 -7
@@ -281,9 +281,22 @@ static inline void hugetlb_event(struct !hugetlb_cgroup_is_root(hugetlb)); } +static struct hugetlb_cgroup *hugetlb_cgroup_from_mm (struct mm_struct *mm) +{ +struct task_struct *task = current; + +#ifdef CONFIG_MEMCG +/* Like memcg, charge the owner of the mm we allocate for. */ +if (!mm) + mm = current->mm; +if (mm) + task =rcu_dereference (mm->owner); +#endif +return task ?hugetlb_cgroup_from_task (task) :NULL ; +} +static int __hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr, -bool rsvd) +struct mm_struct *mm,struct hugetlb_cgroup **ptr,bool rsvd) {int ret =0 ;struct page_counter *counter;
@@ -293,7 +306,12 @@ static int __hugetlb_cgroup_charge_cgrou goto done; again:rcu_read_lock (); - h_cg =hugetlb_cgroup_from_task (current); + h_cg =hugetlb_cgroup_from_mm (mm); +if (!h_cg) { +rcu_read_unlock (); + ret = -ENOMEM ; + goto done; + }if (!css_tryget (&h_cg->css)) {rcu_read_unlock (); goto again;
@@ -318,15 +336,15 @@ done: }int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct mm_struct *mm,struct hugetlb_cgroup **ptr) { -return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, ptr, false); +return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, mm, ptr, false); }int hugetlb_cgroup_charge_cgroup_rsvd (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct mm_struct *mm,struct hugetlb_cgroup **ptr) { -return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, ptr, true); +return __hugetlb_cgroup_charge_cgroup (idx, nr_pages, mm, ptr, true); }/* Should be called with hugetlb_lock held */
_
Patches currently in -mm which might be from [email protected] are
mm-mprotect-dont-skip-uffd-marker-install-when-racing-with-pte-table-reclaim.patch mm-userfaultfd-prevent-poison-marker-loss.patch mm-hugetlb-compare-against-persistent-count-when-freeing-lowmem-folios.patch mm-hugetlb-preserve-reservations-when-shrinking-on-highmem.patch mm-hugetlb-account-temporary-folios-separately.patch mm-hugetlb-account-temporary-folios-separately-fix.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation.patch mm-hugetlb-serialize-page-cache-allocations-with-truncation-fix.patch mm-memfd-recheck-the-page-cache-before-allocating-a-hugetlb-folio.patch mm-hugetlb-count-again-if-the-reserve-map-shrank.patch mm-hugetlb-account-restored-reservations-before-freeing-folios.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush.patch mm-userfaultfd-reject-dax-mappings.patch mm-filemap-avoid-retrying-faults-on-uffd-wp-markers.patch mm-hugetlb-restore-the-reservation-if-vma_needs_reservation-fails.patch mm-hugetlb-restore-the-reservation-when-racing-with-another-reservation.patch mm-hugetlb-return-the-reservation-to-the-subpool-on-error.patch mm-hugetlb-report-reservations-that-we-actually-added-from-hugetlb_reserve_pages.patch mm-hugetlb-keep-hpage_resv_unmapped-when-forking.patch mm-hugetlb-always-mark-new-page-cache-folios-migratable.patch mm-memfd-mark-new-hugetlb-folios-migratable.patch mm-hugetlb-free-surplus-folios-of-other-nodes-on-free.patch mm-hugetlb-wait-for-deferred-frees-before-failing-an-allocation.patch mm-hugetlb_cma-reset-hugetlb_cma_size-if-the-arch-doesnt-support-it.patch fs-proc-task_mmu-dont-clear-vm_softdirty-on-hugetlb-vmas.patch hugetlbfs-fix-integer-overflow-when-punching-holes.patch hugetlbfs-dont-zero-poisoned-pages-in-a-hole-punch.patch hugetlbfs-dont-overflow-the-pseudo-vma-address-in-fallocate.patch hugetlbfs-dont-overflow-the-end-of-the-unmap-range-on-32-bit.patch hugetlbfs-dont-skip-private-vmas-when-unmapping-on-truncation.patch mm-hugetlb-serialize-private-faults-with-truncation.patch mm-hugetlb-check-eof-for-uffdio_copy-and-uffdio_poison.patch hugetlbfs-zero-all-hugetlbfs-specific-fields-of-new-inodes.patch mm-rmap-dont-fail-unmapping-hugetlb-folios-from-private-vmas.patch mm-hugetlb-only-check-pmd-table-sharing-for-pmd-sized-folios.patch mm-migrate-check-hugetlb-pmd-sharing-in-move_pages.patch mm-hugetlb-dont-share-page-tables-of-a-vma-during-mprotect.patch mm-hugetlb-count-migration-entries-at-fork-and-unmap.patch mm-hugetlb-stop-accounting-hugetlbpages-once-pmd-tables-were-shared.patch mm-hugetlb_cgroup-charge-faults-to-mm-owner.patch
Re: [PATCH v2 34/35] mm/hugetlb_cgroup: charge faults to mm->owner
In reply to David Hildenbrand (Arm)Quoted 48 lines · David Hildenbrand (Arm)
We can bypass hugetlb cgroup limits when a kernel thread faults in hugetlb pages for a user process:
__hugetlb_cgroup_charge_cgroup()charges the hugetlb cgroup of current, and kernel threads usually live in the root cgroup, which has no limit.For example, a KVM's async page fault repeats a fault of a vCPU that just failed at the limit in a kworker, which then charges the root cgroup:
vCPU thread kworker
=========== =======
__kvm_mmu_faultin_pfn()
hva_to_pfn(),FOLL_NOWAIT
hugetlb_alloc_folio()
charge fails
KVM_PFN_ERR_NEEDS_IO
kvm_setup_async_pf()
async_pf_execute()
get_user_pages_remote()
hugetlb_alloc_folio()
charges the rootLet's fix it by charging the hugetlb cgroup of
mm->ownerwhenever we allocate or reserve for an mm, like memcg does. If we don't allocate for a specific mm, we usecurrent->mm, again like memcg. Unlike memcg, which falls back to the root cgroup, we fail the charge if the mm has no owner (anymore), as we would otherwise bypass the limits again. Asmm->ownerdepends onCONFIG_MEMCG, nothing changes without it.This changes the charged hugetlb cgroup in these scenarios:
(1) Kernel threads that fault on a user mm, such as KVM async page faults,
kvm_xen_set_evtchn()in the irqfd work item andiommu_sva_handle_mm(), now charge the cgroup ofmm->ownerinstead of the root cgroup.
(2) vhost kthread workers, which join the cgroups of the task thatcreated them, now charge the cgroup of mm->owner. (3) With cgroup v1, threads of a process that are in different hugetlb cgroups now all charge the cgroup of mm->owner.(4) Processes that fault on the mm of another process, such as a userfaultfd monitor (
UFFDIO_COPY) or remote GUP through ptrace,/proc/pid/memorprocess_vm_writev(), now charge the cgroup of the target'smm->ownerinstead of their own cgroup.
Quoted 8 lines
... which should just be the right thing to do.
This has low urgency, as it only affects hugetlb cgroup limits. Let's not CC stable, as this might not be particularly easy to backport.
Fixes 6d76dcf40405 ("hugetlb/cgroup: add charge/uncharge routines for hugetlb cgroup")Reported-by Chris Mason <[email protected]>Signed-off-by David Hildenbrand (Arm) <[email protected]>
Thanks.
David Hildenbrand (Arm)
maintainerPATCH v2 35/35 mm/hugetlb: resolve protnone faults without a uffd bit
In reply to David Hildenbrand (Arm)From: Chris Mason <[email protected]>
hugetlb_fault() only restores the protection of protnone entries that have the uffd bit set, assuming that accessible VMAs cannot have other protnone entries. If such an entry existed nevertheless, any access to it would result in an endless fault loop.
We are not aware of a way to trigger this: paths that drop the uffd bit (fork(), mremap(), unregistering) restore the protection, and remove_migration_pte() only drops the uffd bit for writable migration entries, which cannot be created from protnone entries.
Let's be defensive and make any protnone entry in an accessible VMA accessible again, unless it is an RWP fault that has to be reported to user space, like do_numa_page() does for small folios.
--- mm/hugetlb.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
mm/hugetlb.c +14 -13
@@ -6383,32 +6383,33 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, }/* -* Protnone hugetlb PTEs with the uffd bit are used by -* userfaultfd RWP for access tracking. Plain PROT_NONE (without the -* marker) is not an RWP fault and is not expected on hugetlb (no -* NUMA hinting), so let normal hugetlb fault handling proceed. +* Protnone hugetlb PTEs with the uffd bit are used by userfaultfd RWP +* for access tracking. Without the bit there is nothing to report, so +* just make the PTE accessible. */ -if (pte_protnone (vmf.orig_pte) &&vma_is_accessible (vma) && -huge_pte_uffd (vmf.orig_pte)) { +if (pte_protnone (vmf.orig_pte) &&vma_is_accessible (vma)) {spinlock_t *ptl;pte_t pte;/* Sync: drop hugetlb locks before blocking in handle_userfault() */ -if (userfaultfd_rwp (vma) && !userfaultfd_rwp_async (vma)) +if (huge_pte_uffd (vmf.orig_pte) &&userfaultfd_rwp (vma) && + !userfaultfd_rwp_async (vma))return hugetlb_handle_userfault (&vmf, mapping,VM_UFFD_RWP ); ptl =huge_pte_lock (h, mm, vmf.pte); pte =huge_ptep_get (mm, vmf.address, vmf.pte); -/* -* TODO: fix all cases where uffd PTE bits remain in VMAs that -* are no longer registered with uffd. -*/ -if (pte_protnone (pte) &&huge_pte_uffd (pte)) { +if (pte_same (pte, vmf.orig_pte)) {unsigned int shift =huge_page_shift (h); pte =huge_pte_modify (pte, vma->vm_page_prot); pte =arch_make_huge_pte (pte, shift, vma->vm_flags); -/* huge_pte_modify() preserves _PAGE_UFFD; drop it on resolution */ +/* +* huge_pte_modify() preserves _PAGE_UFFD; drop it on +* resolution. +* +* TODO: fix all cases where uffd PTE bits remain in VMAs +* that are no longer registered with uffd. +*/ pte =huge_pte_clear_uffd (pte); pte =pte_mkyoung (pte);/*
Signature · 1 line
2.56.1
+ mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch added to mm-unstable branch
The patch titled Subject: mm/hugetlb: resolve protnone faults without a uffd bit has been added to the -mm mm-unstable branch. Its filename is…
The patch titled
Subject: mm/hugetlb: resolve protnone faults without a uffd bit
has been added to the -mm mm-unstable branch. Its filename is
mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit.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 <[email protected]>
Subject: mm/hugetlb: resolve protnone faults without a uffd bit
Date: Thu, 03 Dec 2026 12:37:58 +0100
hugetlb_fault() only restores the protection of protnone entries that have the uffd bit set, assuming that accessible VMAs cannot have other protnone entries. If such an entry existed nevertheless, any access to it would result in an endless fault loop.
We are not aware of a way to trigger this: paths that drop the uffd bit (fork(), mremap(), unregistering) restore the protection, and remove_migration_pte() only drops the uffd bit for writable migration entries, which cannot be created from protnone entries.
Let's be defensive and make any protnone entry in an accessible VMA accessible again, unless it is an RWP fault that has to be reported to user space, like do_numa_page() does for small folios.
mm/hugetlb.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
mm/hugetlb.c renamed from mm/hugetlb.c~mm-hugetlb-resolve-protnone-faults-without-a-uffd-bit +14 -13
@@ -6358,32 +6358,33 @@ vm_fault_t hugetlb_fault(struct mm_struc }/* -* Protnone hugetlb PTEs with the uffd bit are used by -* userfaultfd RWP for access tracking. Plain PROT_NONE (without the -* marker) is not an RWP fault and is not expected on hugetlb (no -* NUMA hinting), so let normal hugetlb fault handling proceed. +* Protnone hugetlb PTEs with the uffd bit are used by userfaultfd RWP +* for access tracking. Without the bit there is nothing to report, so +* just make the PTE accessible. */ -if (pte_protnone (vmf.orig_pte) &&vma_is_accessible (vma) && -huge_pte_uffd (vmf.orig_pte)) { +if (pte_protnone (vmf.orig_pte) &&vma_is_accessible (vma)) {spinlock_t *ptl;pte_t pte;/* Sync: drop hugetlb locks before blocking in handle_userfault() */ -if (userfaultfd_rwp (vma) && !userfaultfd_rwp_async (vma)) +if (huge_pte_uffd (vmf.orig_pte) &&userfaultfd_rwp (vma) && + !userfaultfd_rwp_async (vma))return hugetlb_handle_userfault (&vmf, mapping,VM_UFFD_RWP ); ptl =huge_pte_lock (h, mm, vmf.pte); pte =huge_ptep_get (mm, vmf.address, vmf.pte); -/* -* TODO: fix all cases where uffd PTE bits remain in VMAs that -* are no longer registered with uffd. -*/ -if (pte_protnone (pte) &&huge_pte_uffd (pte)) { +if (pte_same (pte, vmf.orig_pte)) {unsigned int shift =huge_page_shift (h); pte =huge_pte_modify (pte, vma->vm_page_prot); pte =arch_make_huge_pte (pte, shift, vma->vm_flags); -/* huge_pte_modify() preserves _PAGE_UFFD; drop it on resolution */ +/* +* huge_pte_modify() preserves _PAGE_UFFD; drop it on +* resolution. +* +* TODO: fix all cases where uffd PTE bits remain in VMAs +* that are no longer registered with uffd. +*/ pte =huge_pte_clear_uffd (pte); pte =pte_mkyoung (pte);/*
_
Patches currently in -mm which might be from [email protected] 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
Re: [PATCH v2 35/35] mm/hugetlb: resolve protnone faults without a uffd bit
In reply to David Hildenbrand (Arm)Quoted 16 lines · David Hildenbrand (Arm)
From: Chris Mason <[email protected]>
hugetlb_fault()only restores the protection of protnone entries that have the uffd bit set, assuming that accessible VMAs cannot have other protnone entries. If such an entry existed nevertheless, any access to it would result in an endless fault loop.We are not aware of a way to trigger this: paths that drop the uffd bit (
fork(),mremap(), unregistering) restore the protection, andremove_migration_pte()only drops the uffd bit for writable migration entries, which cannot be created from protnone entries.
Quoted 7 lines
Let's be defensive and make any protnone entry in an accessible VMA accessible again, unless it is an RWP fault that has to be reported to user space, like
do_numa_page()does for small folios.Signed-off-by Chris Mason <[email protected]>[ david: adjust comments and description ]Signed-off-by David Hildenbrand (Arm) <[email protected]>
THanks.
Andrew Morton
maintainerQuoted 5 lines · On Thu, 03 Dec 2026 12:37:23 +0100 "David Hildenbrand (Arm)"…
Hi,
this is v2 with a bunch more fixes, which I didn't want to send separately to not cause a mess on the list. Most of the new ones are from Sashiko reports, and some found while working on them. Chris reported two more.
updated, thanks.
Quoted 17 lines
Changes in v2:
- Collected tags (thanks!).
- Dropped "mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing".
- "mm/hugetlb_cgroup: charge faults tomm->owner"
- Dropped the RFC label.
- Fall back tocurrent->mmlike memcg, and also charge reservations to the mm of the VMA.
- Minor patch description fixups.
- Added new fixes:
- "mm/hugetlb: restore the reservation ifvma_needs_reservation()fails"
- "mm/hugetlb: restore the reservation when racing with another reservation"
- "mm/hugetlb: return the reservation to the subpool on error"
- "mm/hugetlb: report reservations that we actually added fromhugetlb_reserve_pages()"
- "mm/hugetlb: keepHPAGE_RESV_UNMAPPEDwhen forking"
- "mm/hugetlb: wait for deferred frees before failing an allocation"
- "mm/hugetlb_cma: resethugetlb_cma_sizeif the arch doesn't support it"
Quoted 8 lines
- "hugetlbfs: fix integer overflow when punching holes"
- "hugetlbfs: don't overflow the pseudo VMA address infallocate()"
- "hugetlbfs: don't overflow the end of the unmap range on 32-bit"
- "hugetlbfs: don't skip private VMAs when unmapping on truncation"
- "mm/hugetlb: serialize private faults with truncation"
- "mm/hugetlb: check EOF forUFFDIO_COPYandUFFDIO_POISON"
- "hugetlbfs: zero all hugetlbfs-specific fields of new inodes"
- "mm/rmap: don't fail unmapping hugetlb folios from private VMAs"
Here's how v2 altered mm.git:
Documentation/mm/hugetlbfs_reserv.rst | 4 fs/hugetlbfs/inode.c | 42 +++--- mm/hugetlb.c | 158 +++++++++++++++--------- mm/hugetlb_cgroup.c | 2 mm/hugetlb_cma.c | 1 mm/rmap.c | 8 - 6 files changed, 129 insertions(+), 86 deletions(-)
Documentation/mm/hugetlbfs_reserv.rst renamed from Documentation/mm/hugetlbfs_reserv.rst~b +2 -2
@@ -257,8 +257,8 @@ compensate. See the section :ref:`Reservation Map Helper Routines <resv_map_helpers>` for more information on these routines. -For CoW mappings, alloc_hugetlb_folio() calls -folio_clear_hugetlb_restore_reserve() if the reservation came from the subpool. +alloc_hugetlb_folio() calls folio_clear_hugetlb_restore_reserve() if the +reservation came from the subpool. This makes free_huge_folio() return the reservation to the subpool on error.
fs/hugetlbfs/inode.c renamed from fs/hugetlbfs/inode.c~b +22 -20
105 lines not on this page · raw message
mm/hugetlb.c renamed from mm/hugetlb.c~b +99 -59
294 lines not on this page · raw message
mm/hugetlb_cgroup.c renamed from mm/hugetlb_cgroup.c~b +2 -0
@@ -287,6 +287,8 @@ static struct hugetlb_cgroup *hugetlb_cg#ifdef CONFIG_MEMCG /* Like memcg, charge the owner of the mm we allocate for. */ +if (!mm) + mm = current->mm;if (mm) task =rcu_dereference (mm->owner);#endif
mm/hugetlb_cma.c renamed from mm/hugetlb_cma.c~b +1 -0
@@ -249,6 +249,7 @@ void __init hugetlb_cma_reserve(void) order =arch_hugetlb_cma_order ();if (!order) {pr_warn ("hugetlb_cma: the option isn't supported by current arch\n" ); + hugetlb_cma_size =0 ;return ; }
mm/rmap.c renamed from mm/rmap.c~b +3 -5
@@ -48,7 +48,7 @@* hugetlbfs PageHuge() take locks in this order: * hugetlb_fault_mutex (hugetlbfs specific page fault mutex) * vma_lock (hugetlb specific lock for pmd_sharing) -* mapping->invalidate_lock (when allocating page cache folios) +* mapping->invalidate_lock (when allocating folios) * folio_lock * mapping->i_mmap_rwsem (also used for hugetlb pmd sharing) */
@@ -2049,7 +2049,7 @@ static bool try_to_unmap_poisoned_hugetl* acquiring vma_lock BEFORE i_mmap_rwsem. We can only try lock here and * fail if unsuccessful. */ -if (!folio_test_anon (folio)) { +if (__vma_shareable_lock (vma)) {struct mmu_gather tlb;VM_WARN_ON_ONCE (!(flags &TTU_RMAP_LOCKED ));
@@ -2580,8 +2580,6 @@ static bool try_to_migrate_one(struct foPageAnonExclusive (subpage);if (folio_test_hugetlb (folio)) { -bool anon =folio_test_anon (folio); -/* * huge_pmd_unshare may unmap an entire PMD page. * There is no way of knowing exactly which PMDs may
@@ -2601,7 +2599,7 @@ static bool try_to_migrate_one(struct fo* i_mmap_rwsem. We can only try lock here and * fail if unsuccessful. */ -if (!anon) { +if (__vma_shareable_lock (vma)) {struct mmu_gather tlb;VM_BUG_ON (!(flags &TTU_RMAP_LOCKED ));
_