* [PATCH 00/21] mm/hugetlb: various fixes (2)
@ 2026-11-25 22:32 David Hildenbrand (Arm)
2026-11-25 22:32 ` [PATCH 01/21] mm/hugetlb: charge the copy made at fork to the parent David Hildenbrand (Arm)
` (20 more replies)
0 siblings, 21 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:32 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason, stable
Hi,
some more hugetlb fixes, this time not as horrible and confusing stuff as
last time:
- Patch #1: a hotfix for v7.4
- Patch #2 - #18: a bunch of non-urgent fixes
- Patch #19 - #20: two related improvements
- Patch #21: an RFC for a hugetlb cgroup fix
I think patch #21 is the right thing to do, but I am not really a cgroup
expert.
Patch #19 deserves a closer look from our VMA locking experts :)
Patch #1 should go into v7.4, the rest can wait for the next merge
window.
Tested with hugetlb selftests and some targeted tests that reproduce
some of these issues.
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
Chris Mason (8):
mm/hugetlb: fix truncated copy result in UFFDIO_COPY
hugetlbfs: fix deadlock between hole punching and memory failure
hugetlbfs: recheck the vma range after the lock wait
mm/hugetlb: don't zap non-present entries when unmapping a folio
mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails
mm/hugetlb_cgroup: track in-use folios per cgroup
hugetlbfs: seal SHM_HUGETLB files against growth
mm/hugetlb: resolve protnone faults without a uffd bit
David Hildenbrand (Arm) (13):
mm/hugetlb: charge the copy made at fork to the parent
mm/hugetlb: always mark new page cache folios migratable
mm/memfd: mark new hugetlb folios migratable
mm/hugetlb: free surplus folios of other nodes on free
hugetlbfs: don't zero poisoned pages in a hole punch
mm/hugetlb: only check PMD table sharing for PMD-sized folios
mm/migrate: check hugetlb PMD sharing in move_pages()
mm/hugetlb: don't share page tables of a VMA during mprotect()
mm/hugetlb: count migration entries at fork and unmap
mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared
fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs
mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing
RFC: mm/hugetlb_cgroup: charge faults to mm->owner
Documentation/filesystems/proc.rst | 3 +-
fs/hugetlbfs/inode.c | 22 +++--
fs/proc/task_mmu.c | 9 +-
include/linux/hugetlb.h | 22 ++++-
include/linux/hugetlb_cgroup.h | 30 +++++--
include/linux/leafops.h | 11 +++
include/linux/mmap_lock.h | 1 +
mm/hugetlb.c | 174 ++++++++++++++++++++++++++-----------
mm/hugetlb_cgroup.c | 64 ++++++++------
mm/memfd.c | 1 +
mm/mempolicy.c | 2 +-
mm/migrate.c | 23 +++--
mm/mmap_lock.c | 78 +++++++++++------
mm/rmap.c | 4 +-
mm/vma.c | 2 +-
tools/testing/vma/include/stubs.h | 3 +-
16 files changed, 314 insertions(+), 135 deletions(-)
---
base-commit: c8117bff684306f5c1f959d055cc2b24dd67b1eb
change-id: 20261120-hugetlb_fixes_2-26455c584f4e
Best regards,
--
David Hildenbrand (Arm) <david@kernel.org>
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 01/21] mm/hugetlb: charge the copy made at fork to the parent
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
@ 2026-11-25 22:32 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 02/21] mm/hugetlb: fix truncated copy result in UFFDIO_COPY David Hildenbrand (Arm)
` (19 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:32 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
When copying page tables during fork(), the child is not yet assigned to
the right memcg. When copying possibly pinned anon hugetlb folios, we
must charge them to the parent's memcg, otherwise we can end up
allocating more hugetlb folios than allowed.
Let's fix it by charging the copy to the parent's mm, like we do when
copying small folios in copy_present_page(), introducing a new helper
function alloc_hugetlb_folio_for_fork().
This is a hotfix for v7.4.
Fixes: c2deb1356d82 ("mm/hugetlb: charge folios to the target mm's memcg")
Reported-by: Chris Mason <mason@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/hugetlb.c | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index d0e66fb306221..f46c85fc0cd0b 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3103,8 +3103,9 @@ typedef enum {
* allocation). New call sites should (probably) never set it to true!!
* When it's set, the allocation will bypass all vma level reservations.
*/
-struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
- unsigned long addr, bool cow_from_owner)
+static struct folio *__alloc_hugetlb_folio(struct vm_area_struct *vma,
+ unsigned long addr, bool cow_from_owner,
+ struct mm_struct *memcg_mm)
{
struct hugepage_subpool *spool = subpool_vma(vma);
struct hstate *h = hstate_vma(vma);
@@ -3187,7 +3188,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
.nodemask = nodemask,
};
- folio = hugetlb_alloc_folio(h, &mpoli, vma->vm_mm, alloc_flags);
+ folio = hugetlb_alloc_folio(h, &mpoli, memcg_mm, alloc_flags);
mpol_cond_put(mpol);
@@ -3249,6 +3250,23 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
return ERR_PTR(ret);
}
+struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,
+ unsigned long addr, bool cow_from_owner)
+{
+ return __alloc_hugetlb_folio(vma, addr, cow_from_owner, vma->vm_mm);
+}
+
+/*
+ * Allocate a folio for a copy in the child VMA during fork(), charged to the
+ * parent's mm: the child is not in its memory cgroup yet.
+ */
+static struct folio *alloc_hugetlb_folio_for_fork(struct vm_area_struct *dst_vma,
+ unsigned long addr, struct mm_struct *src_mm)
+{
+ return __alloc_hugetlb_folio(dst_vma, addr, /* cow_from_owner = */ false,
+ src_mm);
+}
+
static __init void *alloc_bootmem(struct hstate *h, int nid, bool node_exact)
{
if (hugetlb_early_cma(h))
@@ -5110,7 +5128,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
spin_unlock(src_ptl);
spin_unlock(dst_ptl);
/* Do not use reserve as it's private owned */
- new_folio = alloc_hugetlb_folio(dst_vma, addr, false);
+ new_folio = alloc_hugetlb_folio_for_fork(dst_vma, addr, src);
if (IS_ERR(new_folio)) {
folio_put(pte_folio);
ret = PTR_ERR(new_folio);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 02/21] mm/hugetlb: fix truncated copy result in UFFDIO_COPY
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
2026-11-25 22:32 ` [PATCH 01/21] mm/hugetlb: charge the copy made at fork to the parent David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 03/21] mm/hugetlb: always mark new page cache folios migratable David Hildenbrand (Arm)
` (18 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason, stable
From: Chris Mason <mason@kernel.org>
Folios that span more bytes than can fit into an unsigned integer (e.g.,
16 GiB folios on arm64) are problematic with UFFDIO_COPY: the result
from copy_folio_from_user() can be truncated, possibly indicating
success instead of an error.
We will then continue with the folio that has stale data, and make it
accessible to user space.
For example, if copy_folio_from_user() fails such that the number of
bytes not copied is a multiple of 4 GiB, we'll get an effective ret ==
0.
Let's fix it by simply testing the return value directly.
This has medium urgency, as it's an information leak.
Fixes: 8fb5debc5fcd ("userfaultfd: hugetlbfs: add hugetlb_mcopy_atomic_pte for userfaultfd support")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: cleanup code and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/hugetlb.c | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index f46c85fc0cd0b..d57964914e18a 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6568,11 +6568,8 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
goto out;
}
- ret = copy_folio_from_user(folio, (const void __user *) src_addr,
- false);
-
/* fallback to copy_from_user outside mmap_lock */
- if (unlikely(ret)) {
+ if (unlikely(copy_folio_from_user(folio, (const void __user *)src_addr, false))) {
ret = -ENOENT;
/* Free the allocated folio which may have
* consumed a reservation.
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 03/21] mm/hugetlb: always mark new page cache folios migratable
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
2026-11-25 22:32 ` [PATCH 01/21] mm/hugetlb: charge the copy made at fork to the parent David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 02/21] mm/hugetlb: fix truncated copy result in UFFDIO_COPY David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 04/21] mm/memfd: mark new hugetlb " David Hildenbrand (Arm)
` (17 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable
hugetlb_no_page() and hugetlb_mfill_atomic_pte() only set
hugetlb_migratable for a new folio once they installed the PTE.
If they back out after adding the new folio to the page cache, for
example because the PTE changed or is no longer empty, the folio will
stay in the page cache without the migratable flag set.
Later faults will find it as an existing folio and never reset the flag.
Such a folio can never be isolated for migration, which breaks memory
offlining, alloc_contig_range(), longterm pinning and memory failure
handling.
Worse, if the folio is not longterm pinnable, because it resides in
ZONE_MOVABLE or CMA, memfd_pin_folios() and the longterm GUP path retry
forever on -EAGAIN. memfd_pin_folios() cannot even be killed, which will
be fixed independently.
Let's fix it by setting hugetlb folios migratable whenever (1) a new
anon folio gets mapped; or (2) a new folio was added to the page cache,
like hugetlbfs_fallocate() already does.
This has low urgency.
Fixes: cb6acd01e2e4 ("hugetlbfs: fix races and page leaks during migration")
Cc: <stable@vger.kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/hugetlb.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index d57964914e18a..c67f9bc3fa3bc 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5953,12 +5953,11 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
{
const bool pagecache_folio = vmf->vma->vm_flags & VM_MAYSHARE;
u32 hash = hugetlb_fault_mutex_hash(mapping, vmf->pgoff);
- bool new_folio, new_anon_folio = false;
+ bool folio_locked = true, new_anon_folio = false;
struct vm_area_struct *vma = vmf->vma;
struct mm_struct *mm = vma->vm_mm;
struct hstate *h = hstate_vma(vma);
vm_fault_t ret = VM_FAULT_SIGBUS;
- bool folio_locked = true;
struct folio *folio;
pte_t new_pte;
@@ -5978,7 +5977,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
* Use page lock to guard against racing truncation
* before we get page_table_lock.
*/
- new_folio = false;
folio = filemap_lock_hugetlb_folio(h, mapping, vmf->pgoff);
if (IS_ERR(folio)) {
if (!hugetlb_pgoff_valid(mapping, vmf->pgoff, /* partial= */ false))
@@ -6048,7 +6046,6 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
}
folio_zero_user(folio, vmf->real_address);
__folio_mark_uptodate(folio);
- new_folio = true;
if (pagecache_folio) {
int err = hugetlb_add_to_page_cache(folio, mapping,
@@ -6069,6 +6066,7 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
goto out;
}
filemap_invalidate_unlock_shared(mapping);
+ folio_set_hugetlb_migratable(folio);
} else {
new_anon_folio = true;
folio_lock(folio);
@@ -6151,11 +6149,10 @@ static vm_fault_t hugetlb_no_page(struct address_space *mapping,
}
/*
- * Only set hugetlb_migratable in newly allocated pages. Existing pages
- * found in the pagecache may not have hugetlb_migratable if they have
- * been isolated for migration.
+ * We must set hugetlb folios migratable whenever (1) a new anon folio
+ * gets mapped; or (2) a new folio was added to the pagecache.
*/
- if (new_folio)
+ if (new_anon_folio)
folio_set_hugetlb_migratable(folio);
if (folio_locked)
@@ -6646,6 +6643,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
if (ret)
goto out_release_nounlock;
folio_in_pagecache = true;
+ folio_set_hugetlb_migratable(folio);
}
ptl = huge_pte_lock(h, dst_mm, dst_pte);
@@ -6694,7 +6692,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte,
/* No need to invalidate - it was non-present before */
update_mmu_cache(dst_vma, dst_addr, dst_pte);
- if (!is_continue)
+ if (!folio_in_pagecache)
folio_set_hugetlb_migratable(folio);
if (vm_shared || is_continue)
folio_unlock(folio);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 04/21] mm/memfd: mark new hugetlb folios migratable
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (2 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 03/21] mm/hugetlb: always mark new page cache folios migratable David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 05/21] mm/hugetlb: free surplus folios of other nodes on free David Hildenbrand (Arm)
` (16 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable
memfd_alloc_folio() adds a new hugetlb folio to the page cache, but
never sets it migratable, unlike hugetlb_no_page() and
hugetlbfs_fallocate().
Later faults will find it as an existing folio and never set the
migratable flag.
Such a folio can never be isolated for migration, even once it is no
longer pinned, which breaks memory offlining, alloc_contig_range() and
memory failure handling.
Let's fix it by setting the new folio migratable after adding it to the
page cache, like hugetlbfs_fallocate() already does.
This has low urgency.
Fixes: 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios")
Cc: <stable@vger.kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/memfd.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/mm/memfd.c b/mm/memfd.c
index 0e02ca69d27cb..6340e8a7c4355 100644
--- a/mm/memfd.c
+++ b/mm/memfd.c
@@ -152,6 +152,7 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
}
hugetlb_set_folio_subpool(folio, subpool_inode(inode));
+ folio_set_hugetlb_migratable(folio);
folio_unlock(folio);
filemap_invalidate_unlock_shared(memfd->f_mapping);
mutex_unlock(&hugetlb_fault_mutex_table[hash]);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 05/21] mm/hugetlb: free surplus folios of other nodes on free
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (3 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 04/21] mm/memfd: mark new hugetlb " David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 06/21] hugetlbfs: fix deadlock between hole punching and memory failure David Hildenbrand (Arm)
` (15 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable, Chris Mason
Surplus hugetlb folios are accounted per node, but reservations are
global. When free_huge_folio() frees a hugetlb folio whose node doesn't
have any surplus folios, we currently keep it as a free hugetlb folio.
However, we might then have surplus folios on other nodes that are no
longer required for the global reservations.
This can be reproduced with two NUMA nodes, nr_hugepages=1 on node 0 and
nr_overcommit_hugepages=1:
(1) Allocate the single hugetlb folio on node 0 from the pool.
(2) With MPOL_BIND to node 1, mmap(MAP_SHARED) a hugetlb memfd that
needs a single folio. This will reserve + allocate a surplus folio
on node 1.
(3) Free the folio from (1).
(4) With MPOL_BIND to node 0, fault in the folio of the memfd, which
consumes the reservation and allocates node 0's free folio, and
immediately punch a hole, freeing the folio without restoring the
reservation. Node 0 has no surplus folios, so the folio is kept.
The pool ends up with a free surplus folio on node 1 that nobody
reserved:
HugePages_Total: 2
HugePages_Free: 2
HugePages_Rsvd: 0
HugePages_Surp: 1
Let's fix it by freeing a surplus folio of another node when
free_huge_folio() keeps a folio whose node has no surplus folios, using
the same rule as for the folio's own node: the free folios cover all
reservations without it.
With this fix, the surplus folio is already freed in (3), as the two
free folios then exceed the single reservation, and we correctly end up
with a single free folio on node 0 and no surplus folios.
Note that the freed surplus folio might have been required by a task
that was bound to that node. But we don't care about that: reservations
are inherently not NUMA aware, and anybody who needs guarantees on
specific nodes has to preallocate, like QEMU does, for example.
This has low urgency.
Fixes: 7893d1d505d5 ("hugetlb: Try to grow hugetlb pool for MAP_PRIVATE mappings")
Cc: <stable@vger.kernel.org>
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/hugetlb.c | 31 +++++++++++++++++++++++++------
1 file changed, 25 insertions(+), 6 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index c67f9bc3fa3bc..0f3db932266d7 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -142,6 +142,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
unsigned long end, struct folio *folio,
zap_flags_t zap_flags);
static struct resv_map *vma_resv_map(struct vm_area_struct *vma);
+static struct folio *remove_pool_hugetlb_folio(struct hstate *h,
+ nodemask_t *nodes_allowed, bool acct_surplus);
static inline bool subpool_is_free(struct hugepage_subpool *spool)
{
@@ -1760,6 +1762,7 @@ void free_huge_folio(struct folio *folio)
struct hstate *h = folio_hstate(folio);
int nid = folio_nid(folio);
struct hugepage_subpool *spool = hugetlb_folio_subpool(folio);
+ struct folio *surplus_folio = NULL;
bool restore_reserve;
unsigned long flags;
@@ -1808,19 +1811,35 @@ void free_huge_folio(struct folio *folio)
folio_clear_hugetlb_temporary(folio);
spin_unlock_irqrestore(&hugetlb_lock, flags);
update_and_free_hugetlb_folio(h, folio, true);
+ return;
} else if (h->surplus_huge_pages_node[nid] &&
h->free_huge_pages >= h->resv_huge_pages) {
/* Free this surplus folio that is not required globally. */
remove_hugetlb_folio(h, folio, true);
spin_unlock_irqrestore(&hugetlb_lock, flags);
update_and_free_hugetlb_folio(h, folio, true);
- } else {
- mod_node_page_state(folio_pgdat(folio), NR_HUGETLB,
- -(long)pages_per_huge_page(h));
- arch_clear_hugetlb_flags(folio);
- enqueue_hugetlb_folio(h, folio);
- spin_unlock_irqrestore(&hugetlb_lock, flags);
+ return;
}
+
+ mod_node_page_state(folio_pgdat(folio), NR_HUGETLB,
+ -(long)pages_per_huge_page(h));
+ arch_clear_hugetlb_flags(folio);
+ enqueue_hugetlb_folio(h, folio);
+
+ /*
+ * This is not a surplus folio on this node, but we might have surplus
+ * folios on other nodes that are no longer required, as reservations
+ * are global and surplus folios are per node. So free another folio
+ * instead.
+ */
+ if (h->surplus_huge_pages &&
+ h->free_huge_pages > h->resv_huge_pages)
+ surplus_folio = remove_pool_hugetlb_folio(h, &node_states[N_MEMORY],
+ /* acct_surplus= */ true);
+ spin_unlock_irqrestore(&hugetlb_lock, flags);
+ /* We might be in atomic context, but freeing might need vmemmap pages. */
+ if (surplus_folio)
+ update_and_free_hugetlb_folio(h, surplus_folio, true);
}
/*
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 06/21] hugetlbfs: fix deadlock between hole punching and memory failure
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (4 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 05/21] mm/hugetlb: free surplus folios of other nodes on free David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 07/21] hugetlbfs: don't zero poisoned pages in a hole punch David Hildenbrand (Arm)
` (14 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason, stable
From: Chris Mason <mason@kernel.org>
Punching a hole that isn't aligned to the huge page size can deadlock
against memory_failure() on a mapped folio at the edge of the hole.
hugetlbfs_punch_hole() locks and zeroes the partial folios with
i_mmap_rwsem held for write, following the hugetlbfs locking order
documented in mm/rmap.c: i_mmap_rwsem before the folio lock.
However, try_memory_failure_hugetlb() locks the folio first, and then
collects the processes to kill under i_mmap_rwsem:
hugetlbfs_punch_hole() try_memory_failure_hugetlb()
====================== ============================
i_mmap_lock_write()
folio_lock()
hugetlbfs_zero_partial_page() hwpoison_user_mappings()
filemap_lock_hugetlb_folio() collect_procs_file()
waits for the folio lock i_mmap_lock_read()
waits for the writer
In fact, the documentation is outdated: hugetlbfs_punch_hole() is the
*only* one that still takes i_mmap_rwsem before the folio lock. All
other scenarios always take the folio lock before the i_mmap_rwsem.
Only memory failure can deadlock with the hole punch:
* truncation and hole punching of full folios are serialized against it
through the inode lock
* unmap_ref_private() holds the lock of an anon folio, not of a page
cache folio that the hole punch zeroes
* migration only tries to take the i_mmap_rwsem.
There is no need to zero the partial folios under i_mmap_rwsem. The lock
only protects the VMA tree and PMD table sharing, nothing about zeroing;
the inode lock and the folio lock are sufficient.
Let's fix it by only holding i_mmap_rwsem while unmapping the full
folios of the hole.
Update the documented locking order in mm/rmap.c and the function
comment above hugetlb_folio_mapping_lock_write().
hugetlb_folio_mapping_lock_write() could now take the lock
unconditionally, but we leave that for a separate change.
The order of the folio lock and i_mmap_rwsem now matches the one for
other folios, which is nice.
This has low urgency, as the race shouldn't really happen all that often
in practice (never seen).
Fixes: 68d32527d340 ("hugetlbfs: zero partial pages during fallocate hole punch")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: adjust comment and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
fs/hugetlbfs/inode.c | 6 ++----
mm/hugetlb.c | 3 +--
mm/rmap.c | 4 ++--
3 files changed, 5 insertions(+), 8 deletions(-)
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 4a92f31c88e1a..2e8672ae2fd52 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -684,8 +684,6 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
return -EPERM;
}
- i_mmap_lock_write(mapping);
-
/* If range starts before first full page, zero partial page. */
if (offset < hole_start)
hugetlbfs_zero_partial_page(h, mapping,
@@ -693,10 +691,12 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
/* Unmap users of full pages in the hole. */
if (hole_end > hole_start) {
+ i_mmap_lock_write(mapping);
if (mapping_mapped(mapping))
hugetlb_vmdelete_list(mapping,
hole_start >> PAGE_SHIFT,
hole_end >> PAGE_SHIFT, 0);
+ i_mmap_unlock_write(mapping);
}
/* If range extends beyond last full page, zero partial page. */
@@ -704,8 +704,6 @@ static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
hugetlbfs_zero_partial_page(h, mapping,
hole_end, offset + len);
- i_mmap_unlock_write(mapping);
-
/* Remove full pages from the file. */
if (hole_end > hole_start)
remove_inode_hugepages(inode, hole_start, hole_end);
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 0f3db932266d7..befa2670cb03f 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -1871,8 +1871,7 @@ void init_new_hugetlb_folio(struct folio *folio)
* Find and lock address space (mapping) in write mode.
*
* Upon entry, the folio is locked which means that folio_mapping() is
- * stable. Due to locking order, we can only trylock_write. If we can
- * not get the lock, simply return NULL to caller.
+ * stable. If we cannot get the lock immediately, we simply return NULL.
*/
struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio)
{
diff --git a/mm/rmap.c b/mm/rmap.c
index fe354fa6cc43a..f35b671d26514 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -49,8 +49,8 @@
* 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->i_mmap_rwsem (also used for hugetlb pmd sharing)
- * folio_lock
+ * folio_lock
+ * mapping->i_mmap_rwsem (also used for hugetlb pmd sharing)
*/
#include <linux/mm.h>
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 07/21] hugetlbfs: don't zero poisoned pages in a hole punch
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (5 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 06/21] hugetlbfs: fix deadlock between hole punching and memory failure David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 23:13 ` Jiaqi Yan
2026-11-25 22:33 ` [PATCH 08/21] hugetlbfs: recheck the vma range after the lock wait David Hildenbrand (Arm)
` (13 subsequent siblings)
20 siblings, 1 reply; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable, Chris Mason
Since commit 8625147cafaa ("hugetlbfs: don't delete error page from
pagecache"), a poisoned hugetlb folio stays in the page cache, and we
must not access its poisoned pages.
However, hugetlbfs_zero_partial_page() zeroes the partial folio of an
unaligned hole punch without checking for poison, which can cause
issues like machine-checks.
Let's fix it by simply not zeroing poisoned folios. We don't really care
about their content: the folio can no longer be mapped, and read() only
returns data up to the first poisoned page.
Only zeroing the pages that are not poisoned would need more thought, as
we cannot reliably identify the poisoned pages; that is future work.
Note that a folio that gets poisoned concurrently, after we checked it,
can still be zeroed, and sync'ing with memory failure handling is also
left as future work.
This has low urgency, as it requires a hole punch on a poisoned folio.
Fixes: 8625147cafaa ("hugetlbfs: don't delete error page from pagecache")
Cc: <stable@vger.kernel.org>
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
fs/hugetlbfs/inode.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 2e8672ae2fd52..5437a55d14f1c 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -656,7 +656,8 @@ static void hugetlbfs_zero_partial_page(struct hstate *h,
if (!end)
end = huge_page_size(h);
- folio_zero_segment(folio, (size_t)start, (size_t)end);
+ if (!folio_test_hwpoison(folio))
+ folio_zero_segment(folio, (size_t)start, (size_t)end);
folio_unlock(folio);
folio_put(folio);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 08/21] hugetlbfs: recheck the vma range after the lock wait
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (6 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 07/21] hugetlbfs: don't zero poisoned pages in a hole punch David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 09/21] mm/hugetlb: don't zap non-present entries when unmapping a folio David Hildenbrand (Arm)
` (12 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason, stable
From: Chris Mason <mason@kernel.org>
A hole punch or a truncation of a hugetlb file that races with a split
of a shared vma can lead to a use-after-free read of a page table, and
to an MMU notifier range with end <= start, which trips a WARN_ON_ONCE()
in KVM.
hugetlb_unmap_file_folio() computes the address of the folio in a vma
and walks the page tables to unmap the folio. As free_pgtables() unlinks
a vma from the rmap tree before it frees its page tables, i_mmap_rwsem
does not protect page tables outside of the vma, so walking them can
result in a use-after-free read.
The rmap tree walk guarantees that a found vma spans our folio. However,
when we have to fall back to locking the vma lock first and drop the
i_mmap_rwsem, we don't check whether that is still the case after we
acquired the vma lock and the i_mmap_rwsem. The following is then
possible:
hugetlb_unmap_file_folio() __split_vma(), new_below == 0
========================== =============================
hugetlb_vma_trylock_write() fails
i_mmap_unlock_write()
down_write(&vma_lock->rw_sema)
vma_prepare()
i_mmap_lock_write()
vma->vm_end = addr
vma_complete()
i_mmap_unlock_write()
i_mmap_lock_write()
vma_lock->vma is not NULL
vma_offset_start() >= vma->vm_end
vma_offset_end() == vma->vm_end
hugetlb_vma_maps_pfn(vma, v_start)
unmap_hugepage_range(vma, v_start, v_end)
Similarly, a split with new_below == 1, where the vma keeps the upper
part and vm_start and vm_pgoff move up, or shrinking the vma with
mremap() can leave the vma not covering our folio.
Let's fix it by checking whether the possibly split vma still spans our
folio. Add a VM_WARN_ON_ONCE() to highlight the problem that was
previously missed.
This has medium urgency, as this is triggerable by unprivileged users.
Fixes: 40549ba8f8e0 ("hugetlb: use new vma_lock for pmd sharing synchronization")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: adjust comment and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
fs/hugetlbfs/inode.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 5437a55d14f1c..0b96fdc3ad9d8 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -442,10 +442,11 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
i_mmap_lock_write(mapping);
vma = vma_lock->vma;
- if (!vma) {
+ if (!vma || vma_start_pgoff(vma) >= end || vma_end_pgoff(vma) <= start) {
/*
- * If lock is no longer attached to vma, then just
- * unlock, drop our reference and retry looking for
+ * If the lock is no longer attached to this vma, or if
+ * the vma changed and no longer covers the folio, then
+ * just unlock, drop our reference and retry looking for
* other vmas.
*/
up_write(&vma_lock->rw_sema);
@@ -459,6 +460,7 @@ static void hugetlb_unmap_file_folio(struct hstate *h,
*/
v_start = vma_offset_start(vma, start);
v_end = vma_offset_end(vma, end);
+ VM_WARN_ON_ONCE(v_start >= v_end);
if (hugetlb_vma_maps_pfn(vma, v_start, pfn))
unmap_hugepage_range(vma, v_start, v_end, NULL,
ZAP_FLAG_DROP_MARKER);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 09/21] mm/hugetlb: don't zap non-present entries when unmapping a folio
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (7 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 08/21] hugetlbfs: recheck the vma range after the lock wait David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 10/21] mm/hugetlb: only check PMD table sharing for PMD-sized folios David Hildenbrand (Arm)
` (11 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
From: Chris Mason <mason@kernel.org>
When the owner of a private hugetlb mapping fails to allocate a folio
for COW, unmap_ref_private() unmaps the old folio from the forked
children, and marks them with HPAGE_RESV_UNMAPPED, so they get a SIGBUS
on their next access.
hugetlb_unmap_range() is supposed to only unmap that folio, but it
clears non-present page table entries unconditionally.
A child can silently lose its COWed folio while it is getting migrated,
or a hwpoison entry or uffd poison marker, without HPAGE_RESV_UNMAPPED
getting set. Consequently, it would not SIGBUS on the next access.
Let's fix it by ignoring non-present entries when unmapping a folio. As
the folio is locked, migration entries of that folio do not apply.
This has low urgency, as it requires a failing COW allocation in the
owner racing with migration or memory failure in the child. Let's not CC
stable.
Fixes: 8c4894c6bc79 ("hwpoison, hugetlbfs: fix "bad pmd" warning in unmapping hwpoisoned hugepage")
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: adjust comment and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/hugetlb.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index befa2670cb03f..2c13349863bd8 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5457,6 +5457,14 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
* unmapped and its refcount is dropped, so just clear pte here.
*/
if (unlikely(!pte_present(pte))) {
+ /*
+ * If we have a folio provided, we only care about
+ * present page table entries (folio is locked).
+ */
+ if (folio_provided) {
+ spin_unlock(ptl);
+ continue;
+ }
/*
* If the pte was wr-protected by uffd-wp in any of the
* swap forms, meanwhile the caller does not want to
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 10/21] mm/hugetlb: only check PMD table sharing for PMD-sized folios
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (8 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 09/21] mm/hugetlb: don't zap non-present entries when unmapping a folio David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 11/21] mm/migrate: check hugetlb PMD sharing in move_pages() David Hildenbrand (Arm)
` (10 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable, Chris Mason
hugetlb_pmd_shared() reads "pt_share_count" from the ptdesc of the page
table that maps a hugetlb folio, independent of the folio size, to tell
whether that page table is shared. Only PMD tables have a valid
"pt_share_count"; for other folio sizes, such as 1 GiB folios on x86
that are mapped by a PUD table, we read whatever another user of the
union left behind.
As a result, such folios might be reported as shared in smaps and as not
exclusively mapped in pagemap, and mbind(MPOL_MF_MOVE) and
migrate_pages() might skip them.
Let's fix it by adding hugetlb_folio_maybe_mapped_shared(), which
combines folio_maybe_mapped_shared(), documented to not care about page
table sharing, with a check for PMD table sharing only for PMD-sized
folios, and using it in the affected callers.
This has low urgency.
Fixes: ca1a47cd3f5f ("mm/hugetlb: fix hugetlb_pmd_shared()")
Cc: <stable@vger.kernel.org>
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
fs/proc/task_mmu.c | 6 ++----
include/linux/hugetlb.h | 16 ++++++++++++++++
mm/mempolicy.c | 2 +-
3 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index d2a524c6ad4b9..41b280ab7b19d 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1211,8 +1211,7 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
if (folio) {
/* We treat non-present entries as "maybe shared". */
- if (!present || folio_maybe_mapped_shared(folio) ||
- hugetlb_pmd_shared(pte))
+ if (!present || hugetlb_folio_maybe_mapped_shared(folio, pte))
mss->shared_hugetlb += huge_page_size(hstate_vma(vma));
else
mss->private_hugetlb += huge_page_size(hstate_vma(vma));
@@ -2131,8 +2130,7 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
if (!folio_test_anon(folio))
flags |= PM_FILE;
- if (!folio_maybe_mapped_shared(folio) &&
- !hugetlb_pmd_shared(ptep))
+ if (!hugetlb_folio_maybe_mapped_shared(folio, ptep))
flags |= PM_MMAP_EXCLUSIVE;
if (huge_pte_uffd(pte))
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 0d5414392d870..49dad5ae57512 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -1386,6 +1386,22 @@ static inline bool hugetlb_pmd_shared(pte_t *pte)
}
#endif
+#ifdef CONFIG_HUGETLB_PAGE
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+ pte_t *ptep)
+{
+ if (folio_maybe_mapped_shared(folio))
+ return true;
+ return folio_size(folio) == PMD_SIZE && hugetlb_pmd_shared(ptep);
+}
+#else /* !CONFIG_HUGETLB_PAGE */
+static inline bool hugetlb_folio_maybe_mapped_shared(struct folio *folio,
+ pte_t *ptep)
+{
+ return true;
+}
+#endif /* CONFIG_HUGETLB_PAGE */
+
bool want_pmd_share(struct vm_area_struct *vma, unsigned long addr);
#ifndef __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 6f800fb961aec..229efcdb2c7db 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -833,7 +833,7 @@ static int queue_folios_hugetlb(pte_t *pte, unsigned long hmask,
* cannot easily detect if a folio is shared.
*/
if ((flags & MPOL_MF_MOVE_ALL) ||
- (!folio_maybe_mapped_shared(folio) && !hugetlb_pmd_shared(pte)))
+ !hugetlb_folio_maybe_mapped_shared(folio, pte))
if (!folio_isolate_hugetlb(folio, qp->pagelist))
qp->nr_failed++;
unlock:
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 11/21] mm/migrate: check hugetlb PMD sharing in move_pages()
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (9 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 10/21] mm/hugetlb: only check PMD table sharing for PMD-sized folios David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 12/21] mm/hugetlb: don't share page tables of a VMA during mprotect() David Hildenbrand (Arm)
` (9 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable, Chris Mason
Without MPOL_MF_MOVE_ALL, which requires CAP_SYS_NICE, move_pages(2)
refuses to migrate folios that might be mapped by other processes.
However, folio_maybe_mapped_shared() does not check for page table
sharing. So move_pages(2) with MPOL_MF_MOVE can migrate hugetlb
folios that other processes map.
mbind(2) and migrate_pages(2) already handle PMD table sharing since
commit 73bdf65ea748 ("migrate: hugetlb: check for hugetlb shared PMD in
node migration"), but we missed move_pages(2).
Let's fix it by using hugetlb_folio_maybe_mapped_shared() for hugetlb
folios in move_pages(2). Such folios are now reported with -EACCES in
the status array.
Rework the code to have separate checks for hugetlb vs. !hugetlb.
This has low urgency.
Fixes: e632a938d914 ("mm: migrate: add hugepage migration code to move_pages()")
Cc: <stable@vger.kernel.org>
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/migrate.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/mm/migrate.c b/mm/migrate.c
index 0430197fdc04b..194d74152e8e3 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -2279,8 +2279,9 @@ static int do_move_pages_to_node(struct list_head *pagelist, int node)
return err;
}
-static int __add_folio_for_migration(struct folio *folio, int node,
- struct list_head *pagelist, bool migrate_all)
+static int __add_folio_for_migration(struct folio *folio,
+ struct folio_walk *fw, int node, struct list_head *pagelist,
+ bool migrate_all)
{
if (is_zero_folio(folio) || is_huge_zero_folio(folio))
return -EFAULT;
@@ -2291,13 +2292,19 @@ static int __add_folio_for_migration(struct folio *folio, int node,
if (folio_nid(folio) == node)
return 0;
- if (folio_maybe_mapped_shared(folio) && !migrate_all)
- return -EACCES;
-
if (folio_test_hugetlb(folio)) {
+ /* fw->ptep aliases fw->pmdp and fw->pudp. */
+ if (!migrate_all && hugetlb_folio_maybe_mapped_shared(folio, fw->ptep))
+ return -EACCES;
if (folio_isolate_hugetlb(folio, pagelist))
return 1;
- } else if (folio_isolate_lru(folio)) {
+ return -EBUSY;
+ }
+
+ if (!migrate_all && folio_maybe_mapped_shared(folio))
+ return -EACCES;
+
+ if (folio_isolate_lru(folio)) {
list_add_tail(&folio->lru, pagelist);
node_stat_mod_folio(folio,
NR_ISOLATED_ANON + folio_is_file_lru(folio),
@@ -2332,8 +2339,8 @@ static int add_folio_for_migration(struct mm_struct *mm, const void __user *p,
if (vma && vma_migratable(vma)) {
folio = folio_walk_start(&fw, vma, addr, FW_ZEROPAGE);
if (folio) {
- err = __add_folio_for_migration(folio, node, pagelist,
- migrate_all);
+ err = __add_folio_for_migration(folio, &fw, node,
+ pagelist, migrate_all);
folio_walk_end(&fw, vma);
} else {
err = -ENOENT;
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 12/21] mm/hugetlb: don't share page tables of a VMA during mprotect()
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (10 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 11/21] mm/migrate: check hugetlb PMD sharing in move_pages() David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 13/21] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails David Hildenbrand (Arm)
` (8 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable, Chris Mason
When mprotect() of a shared hugetlb mapping races with a hugetlb fault
in another process that can share the same PMD table, we can end up with
page table entries that don't match the VMA flags, such as writable ones
in a PROT_READ mapping or PROT_NONE ones in accessible VMAs.
huge_pmd_share() takes i_mmap_rwsem and shares a PMD table if the flags
of the two VMAs match, so while that lock is held the entries in a
table have to match the flags of its VMA.
However, mprotect_fixup() stores the new flags without i_mmap_rwsem, and
only hugetlb_change_protection() takes it. A fault in between shares a
table that still has the old entries, and the walk then unshares the
table instead of changing them:
process A process C
VMA is RW, entries are RW VMA is R, PUD is empty
========================= ======================
mprotect_fixup()
VMA flags = R
huge_pmd_share()
i_mmap_lock_read()
flags match, take A's table
i_mmap_unlock_read()
hugetlb_change_protection()
i_mmap_lock_write()
huge_pmd_unshare()
clear A's PUD, return 1
skip the entries
VMA is R, entries are RW
mprotect() changes both the flags and the entries while holding the
mmap lock in write mode.
Let's fix it by only sharing the page tables of another VMA if we can
try-read-lock the mmap lock of its mm. If the trylock fails, we simply
try the next VMA, or allocate our own page table.
A trylock is fine, as we hold i_mmap_rwsem, which is usually taken under
the mmap lock. We should use the VMA lock instead, but we'll do that
separately.
This has medium urgency, as it can result in endless page fault loops if
mprotect() races with PMD table sharing and we end up with PROT_NONE
entries in accessible VMAs. If the kernel accesses such memory, for
example in copy_to_user(), the loop happens in the kernel and the task
cannot be killed.
Fixes: 39dde65c9940 ("[PATCH] shared page table for hugetlb page")
Cc: <stable@vger.kernel.org>
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/hugetlb.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 2c13349863bd8..0b0f6e17b8b06 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -7264,15 +7264,23 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
if (svma == vma)
continue;
+ /*
+ * Stabilize the svma flags, as mprotect() changes flags
+ * before adjusting page tables.
+ */
+ if (!mmap_read_trylock(svma->vm_mm))
+ continue;
+
saddr = page_table_shareable(svma, vma, addr, idx);
if (saddr) {
spte = hugetlb_walk(svma, saddr,
vma_mmu_pagesize(svma));
- if (spte) {
+ if (spte)
ptdesc_pmd_pts_inc(virt_to_ptdesc(spte));
- break;
- }
}
+ mmap_read_unlock(svma->vm_mm);
+ if (spte)
+ break;
}
if (!spte)
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 13/21] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (11 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 12/21] mm/hugetlb: don't share page tables of a VMA during mprotect() David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 14/21] mm/hugetlb: count migration entries at fork and unmap David Hildenbrand (Arm)
` (7 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason, stable
From: Chris Mason <mason@kernel.org>
If we split a VMA that has shared PMD page tables mapped, and allocating
the hugetlb VMA lock for the new VMA fails, a fault in the new VMA can
race with a hole punch and access page tables that were already freed.
__split_vma() calls hugetlb_vm_op_open() for the new VMA, which
allocates its hugetlb VMA lock in hugetlb_vma_lock_alloc(). If that
allocation fails, the new VMA is left without a lock.
A VMA that has shared PMD page tables needs the hugetlb VMA lock, since
the lock synchronizes huge_pmd_unshare() with concurrent page table
walks, including faults.
want_pmd_share() and page_table_shareable() prevent new sharing without
the lock, but a split leaves previously shared page tables in place for
the new VMA, and hugetlb_vm_op_open() can't currently return an error if
the allocation fails.
This can, for example, happen for an OOM victim. Then, nothing prevents
a hole punch from unsharing the page table of the new VMA while a fault
in the new VMA is still using it, and the last other sharer from freeing
it:
fault in the new VMA hole punch, then munmap()
==================== =========================
hugetlb_vma_lock_read()
no lock, does nothing
huge_pte_alloc()
pointer into shared table
folio_zero_user()
hugetlb_vma_trylock_write()
no lock, returns 1
huge_pmd_unshare(new VMA)
pud_clear()
last sharer frees the table
huge_pte_lock()
set_huge_pte_at()
Let's fix it by unsharing the PMD page tables of a new VMA that has no
lock. Also, exit hugetlb_split() early if the original VMA has no lock,
as it cannot have shared PMD page tables then.
This has low urgency, as it requires allocating the hugetlb VMA lock to
fail during a split, which is practically only possible for OOM victims,
and a race with a hole punch.
Fixes: 131a79b474e9 ("hugetlb: fix vma lock handling during split vma and range unmapping")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: return early if the original VMA has no lock, adjust comment
and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
include/linux/hugetlb.h | 6 ++++--
mm/hugetlb.c | 17 ++++++++++++++++-
mm/vma.c | 2 +-
tools/testing/vma/include/stubs.h | 3 ++-
4 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 49dad5ae57512..aea18895e1459 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -274,7 +274,8 @@ long hugetlb_change_protection(struct vm_area_struct *vma,
unsigned long cp_flags);
void hugetlb_unshare_all_pmds(struct vm_area_struct *vma);
void fixup_hugetlb_reservations(struct vm_area_struct *vma);
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr);
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+ unsigned long addr);
unsigned int arch_hugetlb_cma_order(void);
@@ -457,7 +458,8 @@ static inline void fixup_hugetlb_reservations(struct vm_area_struct *vma)
{
}
-static inline void hugetlb_split(struct vm_area_struct *vma, unsigned long addr) {}
+static inline void hugetlb_split(struct vm_area_struct *vma,
+ struct vm_area_struct *new, unsigned long addr) {}
#endif /* !CONFIG_HUGETLB_PAGE */
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 0b0f6e17b8b06..2bba0940a40d5 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4887,7 +4887,8 @@ static int hugetlb_vm_op_split(struct vm_area_struct *vma, unsigned long addr)
return 0;
}
-void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
+void hugetlb_split(struct vm_area_struct *vma, struct vm_area_struct *new,
+ unsigned long addr)
{
/*
* PMD sharing is only possible for PUD_SIZE-aligned address ranges
@@ -4898,8 +4899,13 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
* walks (except hardware and gup_fast()).
*/
vma_assert_write_locked(vma);
+ vma_assert_write_locked(new);
i_mmap_assert_write_locked(vma->vm_file->f_mapping);
+ /* Shared PMDs require a hugetlb VMA lock. */
+ if (!__vma_shareable_lock(vma))
+ return;
+
if (addr & ~PUD_MASK) {
unsigned long floor = addr & PUD_MASK;
unsigned long ceil = floor + PUD_SIZE;
@@ -4918,6 +4924,15 @@ void hugetlb_split(struct vm_area_struct *vma, unsigned long addr)
hugetlb_unshare_pmds(vma, floor, ceil, /* take_locks = */ false);
}
}
+
+ /*
+ * If we failed to allocate the hugetlb VMA lock, unshare everything
+ * that the new VMA covers.
+ */
+ if (!__vma_shareable_lock(new))
+ hugetlb_unshare_pmds(new, ALIGN(new->vm_start, PUD_SIZE),
+ ALIGN_DOWN(new->vm_end, PUD_SIZE),
+ /* take_locks = */ false);
}
static unsigned long hugetlb_vm_op_pagesize(struct vm_area_struct *vma)
diff --git a/mm/vma.c b/mm/vma.c
index c82e9306b11ac..183bfb8ba4703 100644
--- a/mm/vma.c
+++ b/mm/vma.c
@@ -590,7 +590,7 @@ __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
*/
vma_adjust_trans_huge(vma, vma->vm_start, addr, NULL);
if (vma_is_hugetlb(vma))
- hugetlb_split(vma, addr);
+ hugetlb_split(vma, new, addr);
if (new_below) {
vma->vm_start = addr;
diff --git a/tools/testing/vma/include/stubs.h b/tools/testing/vma/include/stubs.h
index 9f5b8e996abf6..6ddecf587d9be 100644
--- a/tools/testing/vma/include/stubs.h
+++ b/tools/testing/vma/include/stubs.h
@@ -466,7 +466,8 @@ static inline void vma_adjust_trans_huge(struct vm_area_struct *vma,
{
}
-static inline void hugetlb_split(struct vm_area_struct *, unsigned long) {}
+static inline void hugetlb_split(struct vm_area_struct *,
+ struct vm_area_struct *, unsigned long) {}
static inline bool vma_supports_mlock(const struct vm_area_struct *vma)
{
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 14/21] mm/hugetlb: count migration entries at fork and unmap
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (12 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 13/21] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 15/21] mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared David Hildenbrand (Arm)
` (6 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
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.
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 <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
include/linux/leafops.h | 11 +++++++++++
mm/hugetlb.c | 3 +++
2 files changed, 14 insertions(+)
diff --git a/include/linux/leafops.h b/include/linux/leafops.h
index 4c1476ae32343..c315a3531485e 100644
--- a/include/linux/leafops.h
+++ b/include/linux/leafops.h
@@ -587,6 +587,17 @@ static inline bool pte_is_uffd_marker(pte_t pte)
return false;
}
+/**
+ * pte_is_migration_entry() - Does this PTE entry encode a migration entry?
+ * @pte: PTE entry.
+ *
+ * Returns: true if the PTE encodes a migration entry, otherwise false.
+ */
+static inline bool pte_is_migration_entry(pte_t pte)
+{
+ return softleaf_is_migration(softleaf_from_pte(pte));
+}
+
#if defined(CONFIG_ZONE_DEVICE) && defined(CONFIG_ARCH_HAS_PMD_SOFTLEAVES)
/**
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 2bba0940a40d5..89d71e3dadd77 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5131,6 +5131,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
if (!userfaultfd_protected(dst_vma))
entry = pte_swp_clear_uffd(entry);
set_huge_pte_at(dst, addr, dst_pte, entry, sz);
+ hugetlb_count_add(npages, dst);
} else if (unlikely(pte_is_marker(entry))) {
const pte_marker marker = copy_pte_marker(softleaf, dst_vma);
@@ -5480,6 +5481,8 @@ static void hugetlb_unmap_range(struct mmu_gather *tlb,
spin_unlock(ptl);
continue;
}
+ if (pte_is_migration_entry(pte))
+ hugetlb_count_sub(pages_per_huge_page(h), mm);
/*
* If the pte was wr-protected by uffd-wp in any of the
* swap forms, meanwhile the caller does not want to
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 15/21] mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (13 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 14/21] mm/hugetlb: count migration entries at fork and unmap David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 16/21] mm/hugetlb_cgroup: track in-use folios per cgroup David Hildenbrand (Arm)
` (5 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
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.
Fixes: 5d317b2b6536 ("mm: hugetlb: proc: add HugetlbPages field to /proc/PID/status")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
Documentation/filesystems/proc.rst | 3 ++-
mm/hugetlb.c | 9 ++++++++-
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst
index bcbcb41814d96..8696359b06498 100644
--- a/Documentation/filesystems/proc.rst
+++ b/Documentation/filesystems/proc.rst
@@ -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
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 89d71e3dadd77..796f02a4ded63 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4744,8 +4744,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. */
@@ -7310,6 +7311,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));
}
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 16/21] mm/hugetlb_cgroup: track in-use folios per cgroup
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (14 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 15/21] mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 17/21] hugetlbfs: seal SHM_HUGETLB files against growth David Hildenbrand (Arm)
` (4 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
From: Chris Mason <mason@kernel.org>
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 <mason@kernel.org>
[ david: add lock checks, add HUGETLB_CGROUP_OFFLINE_BATCH, simplify
comments and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
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 796f02a4ded63..5db97f6d15595 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -7608,7 +7608,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);
}
@@ -7684,7 +7684,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
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 17/21] hugetlbfs: seal SHM_HUGETLB files against growth
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (15 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 16/21] mm/hugetlb_cgroup: track in-use folios per cgroup David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 18/21] fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs David Hildenbrand (Arm)
` (3 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
From: Chris Mason <mason@kernel.org>
The size of a SysV shared memory segment is fixed at shmget(), and
do_shmat() relies on that by mapping i_size bytes of the backing file.
However, with SHM_HUGETLB, the hugetlbfs file can be grown through an
fd obtained from /proc/<pid>/map_files with ftruncate() or fallocate().
Also, remap_file_pages() with a pgoff beyond the segment can grow it,
because hugetlbfs_file_mmap() grows the file to the end of a writable
mapping:
shmget(key, 4M, SHM_HUGETLB) i_size = 4M
addr = shmat(id, NULL, 0)
remap_file_pages(addr, 2M, 0, 6M >> PAGE_SHIFT, 0)
i_size = 8M
shmat(id, NULL, 0) maps 8M
Later calls to shmat() will map the bigger size.
Using /proc/<pid>/map_files requires CAP_CHECKPOINT_RESTORE, but
remap_file_pages() does not require any privileges, so we focus on the
latter.
Let's fix it by setting F_SEAL_GROW on the inode when creating it for a
segment. remap_file_pages() beyond the segment still succeeds, like for
shmem segments, but accessing pages there now correctly results in
SIGBUS.
This has low urgency, and likely nobody ever ran into this in practice.
Let's not CC stable.
Fixes: c8d78c1823f4 ("mm: replace remap_file_pages() syscall with emulation")
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: adjust description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
fs/hugetlbfs/inode.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 0b96fdc3ad9d8..f1a8208ffc818 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -1559,8 +1559,11 @@ struct file *hugetlb_file_setup(const char *name, size_t size,
S_IFREG | S_IRWXUGO, 0);
if (!inode)
goto out;
- if (creat_flags == HUGETLB_SHMFS_INODE)
+ if (creat_flags == HUGETLB_SHMFS_INODE) {
inode->i_flags |= S_PRIVATE;
+ /* The size of a segment is fixed at shmget(). */
+ HUGETLBFS_I(inode)->seals |= F_SEAL_GROW;
+ }
inode->i_size = size;
clear_nlink(inode);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 18/21] fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (16 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 17/21] hugetlbfs: seal SHM_HUGETLB files against growth David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 19/21] mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing David Hildenbrand (Arm)
` (2 subsequent siblings)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
stable, Chris Mason
hugetlb does not support soft-dirty tracking: since commit f96f7a40874d
("mm/hugetlb: fix hugetlb not supporting softdirty tracking"),
vma_wants_writenotify() ignores VM_SOFTDIRTY for hugetlb VMAs, and
clear_refs never clears the soft-dirty bits of hugetlb entries.
However, clear_refs still clears VM_SOFTDIRTY on hugetlb VMAs. With
CONFIG_MEM_SOFT_DIRTY, a FOLL_FORCE write to a private hugetlb mapping
without VM_WRITE can then loop in __get_user_pages() until the task is
killed, with the mmap_lock held for reading:
__get_user_pages()
retry:
follow_page_mask()
follow_huge_pmd()
can_follow_write_pmd()
pmd_needs_soft_dirty_wp() is true
returns NULL
faultin_page()
hugetlb_wp()
set_huge_ptep_maybe_writable()
no VM_WRITE: the entry stays as it is
returns 0
goto retry
Let's fix it by not clearing VM_SOFTDIRTY on hugetlb VMAs, so we always
treat hugetlb pages as soft-dirty.
This has low urgency, as the loop can be killed.
Fixes: 052ccfbcc6cd ("mm/hugetlb: support FOLL_FORCE|FOLL_WRITE")
Cc: <stable@vger.kernel.org>
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
fs/proc/task_mmu.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 41b280ab7b19d..d9ecb94f4daa5 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1801,6 +1801,9 @@ static ssize_t clear_refs_write(struct file *file, const char __user *buf,
if (type == CLEAR_REFS_SOFT_DIRTY) {
for_each_vma(vmi, vma) {
+ /* hugetlb does not support soft-dirty tracking. */
+ if (vma_is_hugetlb(vma))
+ continue;
if (!(vma->vm_flags & VM_SOFTDIRTY))
continue;
vm_flags_clear(vma, VM_SOFTDIRTY);
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 19/21] mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (17 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 18/21] fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 20/21] mm/hugetlb: resolve protnone faults without a uffd bit David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 21/21] RFC: mm/hugetlb_cgroup: charge faults to mm->owner David Hildenbrand (Arm)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm)
huge_pmd_share() currently only shares the page tables of another VMA if
it can read-lock the mmap lock of the corresponding mm. That refuses
sharing whenever that mm is write-locked or if the mmap lock is
contended.
mprotect() holds the VMA write lock while changing the flags and the
page table entries, so let's read-lock only that VMA instead. As we hold
i_mmap_rwsem, that VMA remains part of its mm and cannot go away.
Add vma_trylock_read(), a variant of vma_start_read(), that does not
require being called with the RCU read lock held, and will also not drop
it itself.
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
include/linux/mmap_lock.h | 1 +
mm/hugetlb.c | 4 +--
mm/mmap_lock.c | 78 +++++++++++++++++++++++++++++++----------------
3 files changed, 55 insertions(+), 28 deletions(-)
diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
index bada7a03ddf4b..f42444069e76c 100644
--- a/include/linux/mmap_lock.h
+++ b/include/linux/mmap_lock.h
@@ -278,6 +278,7 @@ static inline bool vma_start_read_locked(struct vm_area_struct *vma)
struct vm_area_struct *vma_start_read_unlocked(struct mm_struct *mm,
unsigned long address);
+bool vma_trylock_read(struct vm_area_struct *vma);
static inline void vma_end_read(struct vm_area_struct *vma)
{
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 5db97f6d15595..62ed8d9aeeadd 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -7287,7 +7287,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
* Stabilize the svma flags, as mprotect() changes flags
* before adjusting page tables.
*/
- if (!mmap_read_trylock(svma->vm_mm))
+ if (!vma_trylock_read(svma))
continue;
saddr = page_table_shareable(svma, vma, addr, idx);
@@ -7297,7 +7297,7 @@ pte_t *huge_pmd_share(struct mm_struct *mm, struct vm_area_struct *vma,
if (spte)
ptdesc_pmd_pts_inc(virt_to_ptdesc(spte));
}
- mmap_read_unlock(svma->vm_mm);
+ vma_end_read(svma);
if (spte)
break;
}
diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c
index c7261f980cef1..68c16281e52a5 100644
--- a/mm/mmap_lock.c
+++ b/mm/mmap_lock.c
@@ -212,26 +212,17 @@ static bool vma_seqcount_is_write_locked(unsigned int vma_seq, unsigned int mm_s
}
/*
- * Try to read-lock a vma. The function is allowed to occasionally yield false
- * locked result to avoid performance overhead, in which case we fall back to
- * using mmap_lock. The function should never yield false unlocked result.
- * False locked result is possible if vma gets reused and attached to a
- * different mm before we lock it.
- * Returns the vma on success, NULL on failure to lock and EAGAIN if vma got
- * detached.
- *
- * IMPORTANT: RCU lock must be held upon entering the function, but upon error
- * IT IS RELEASED. The caller must handle this correctly.
+ * Like vma_start_read(), but without RCU handling: the caller must make sure
+ * that the vma cannot be freed, for example by holding the RCU lock.
*/
-static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
- struct vm_area_struct *vma)
+static struct vm_area_struct *__vma_start_read(struct mm_struct *mm,
+ struct vm_area_struct *vma)
{
unsigned int vma_seq;
unsigned int mm_seq;
struct mm_struct *other_mm;
int oldcnt;
- RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
/*
* Check before locking. A race might cause false locked result.
* We can use READ_ONCE() for the mm_lock_seq here, and don't need
@@ -241,10 +232,8 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
*/
vma_seq = READ_ONCE(vma->vm_lock_seq);
mm_seq = READ_ONCE(mm->mm_lock_seq.sequence);
- if (vma_seqcount_is_write_locked(vma_seq, mm_seq)) {
- vma = NULL;
- goto err;
- }
+ if (vma_seqcount_is_write_locked(vma_seq, mm_seq))
+ return NULL;
/*
* If VM_REFCNT_EXCLUDE_READERS_FLAG is set,
@@ -257,8 +246,7 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
if (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt,
VM_REFCNT_LIMIT))) {
/* return EAGAIN if vma got detached from under us */
- vma = oldcnt ? NULL : ERR_PTR(-EAGAIN);
- goto err;
+ return oldcnt ? NULL : ERR_PTR(-EAGAIN);
}
__vma_lockdep_acquire_read(vma);
@@ -288,14 +276,9 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
mm_seq = raw_read_seqcount(&mm->mm_lock_seq);
if (unlikely(vma_seqcount_is_write_locked(vma_seq, mm_seq))) {
vma_refcount_put(vma);
- vma = NULL;
- goto err;
+ return NULL;
}
- return vma;
-err:
- rcu_read_unlock();
-
return vma;
err_unstable:
/*
@@ -309,11 +292,54 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
mmgrab(other_mm);
vma_refcount_put(vma);
mmdrop_async(other_mm);
- rcu_read_unlock();
return NULL;
}
+/**
+ * vma_start_read() - Try to read-lock a vma looked up under RCU.
+ * @mm: The mm the vma is expected to belong to.
+ * @vma: The vma to read-lock.
+ *
+ * The function is allowed to occasionally yield false locked result to avoid
+ * performance overhead, in which case we fall back to using mmap_lock. The
+ * function should never yield false unlocked result. False locked result is
+ * possible if vma gets reused and attached to a different mm before we lock it.
+ *
+ * Context: RCU lock must be held upon entering the function, but upon error
+ * IT IS RELEASED. The caller must handle this correctly.
+ *
+ * Return: The vma on success, NULL on failure to lock and ERR_PTR(-EAGAIN) if
+ * the vma got detached.
+ */
+static struct vm_area_struct *vma_start_read(struct mm_struct *mm,
+ struct vm_area_struct *vma)
+{
+ RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
+
+ vma = __vma_start_read(mm, vma);
+ if (IS_ERR_OR_NULL(vma))
+ rcu_read_unlock();
+ return vma;
+}
+
+/**
+ * vma_trylock_read() - Try to read-lock a VMA that belongs to its mm.
+ * @vma: The VMA to read-lock.
+ *
+ * Unlike vma_start_read(), this function does not deal with VMAs that might
+ * get freed and reused in another mm concurrently.
+ *
+ * Context: The caller must make sure that @vma remains part of @vma->vm_mm
+ * and cannot go away.
+ *
+ * Return: true if @vma is read-locked, false otherwise.
+ */
+bool vma_trylock_read(struct vm_area_struct *vma)
+{
+ return !IS_ERR_OR_NULL(__vma_start_read(vma->vm_mm, vma));
+}
+
/*
* Lookup and lock a VMA under RCU protection. Returned VMA is guaranteed to be
* stable and not isolated. If the VMA is not found or is being modified the
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 20/21] mm/hugetlb: resolve protnone faults without a uffd bit
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (18 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 19/21] mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 21/21] RFC: mm/hugetlb_cgroup: charge faults to mm->owner David Hildenbrand (Arm)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
From: Chris Mason <mason@kernel.org>
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.
Signed-off-by: Chris Mason <mason@kernel.org>
[ david: adjust comments and description ]
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
mm/hugetlb.c | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 62ed8d9aeeadd..438263c2ba3d9 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6355,32 +6355,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);
/*
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* [PATCH 21/21] RFC: mm/hugetlb_cgroup: charge faults to mm->owner
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
` (19 preceding siblings ...)
2026-11-25 22:33 ` [PATCH 20/21] mm/hugetlb: resolve protnone faults without a uffd bit David Hildenbrand (Arm)
@ 2026-11-25 22:33 ` David Hildenbrand (Arm)
20 siblings, 0 replies; 23+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-25 22:33 UTC (permalink / raw)
To: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li
Cc: linux-mm, Jason Gunthorpe, linux-kernel, linux-fsdevel,
Andrew Morton, linux-doc, David Hildenbrand (Arm),
Chris Mason
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 for an mm, like memcg does. 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.
Fixes: 6d76dcf40405 ("hugetlb/cgroup: add charge/uncharge routines for hugetlb cgroup")
Reported-by: Chris Mason <mason@kernel.org>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
include/linux/hugetlb_cgroup.h | 10 +++++-----
mm/hugetlb.c | 11 ++++++-----
mm/hugetlb_cgroup.c | 30 +++++++++++++++++++++++-------
3 files changed, 34 insertions(+), 17 deletions(-)
diff --git a/include/linux/hugetlb_cgroup.h b/include/linux/hugetlb_cgroup.h
index 1c437eb2e1c30..97208bb9d9639 100644
--- a/include/linux/hugetlb_cgroup.h
+++ b/include/linux/hugetlb_cgroup.h
@@ -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;
}
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 438263c2ba3d9..2b0ae201e1e60 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2305,7 +2305,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);
@@ -3000,7 +3000,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
@@ -3026,10 +3027,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;
}
@@ -7011,7 +7012,7 @@ 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), NULL, &h_cg);
if (err < 0)
goto out_err;
diff --git a/mm/hugetlb_cgroup.c b/mm/hugetlb_cgroup.c
index 6ee771334a3bd..be6a3e0f13209 100644
--- a/mm/hugetlb_cgroup.c
+++ b/mm/hugetlb_cgroup.c
@@ -281,9 +281,20 @@ 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)
+ 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 +304,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 +334,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 */
--
2.56.1
^ permalink raw reply [flat|nested] 23+ messages in thread
* Re: [PATCH 07/21] hugetlbfs: don't zero poisoned pages in a hole punch
2026-11-25 22:33 ` [PATCH 07/21] hugetlbfs: don't zero poisoned pages in a hole punch David Hildenbrand (Arm)
@ 2026-11-25 23:13 ` Jiaqi Yan
0 siblings, 0 replies; 23+ messages in thread
From: Jiaqi Yan @ 2026-11-25 23:13 UTC (permalink / raw)
To: David Hildenbrand (Arm)
Cc: Muchun Song, Oscar Salvador, Andrew Morton, Jinmeng Zhou,
Hongfu Li, Andrea Arcangeli, Mike Kravetz, Naoya Horiguchi,
Hugh Dickins, Baolin Wang, Dave Airlie, Vivek Kasireddy,
Gerd Hoffmann, Mike Rapoport, Mel Gorman, Dave McCracken,
Andy Whitcroft, Adam Litke, Lorenzo Stoakes, Rik van Riel,
Liam R. Howlett, Vlastimil Babka, Harry Yoo, Jann Horn,
Lance Yang, James Houghton, Yang Shi, Suren Baghdasaryan,
Shakeel Butt, Pedro Falcato, Gregory Price, Zi Yan, Joshua Hahn,
Rakie Kim, Ying Huang, Matthew Brost, Byungchul Park,
Alistair Popple, Wanpeng Li, Andi Kleen, Kenneth W Chen,
Hugh Dickins, Michal Hocko, David Rientjes, Joern Engel,
Jonathan Corbet, Shuah Khan, Randy Dunlap, Michal Hocko,
Aneesh Kumar K.V, KAMEZAWA Hiroyuki, Kirill A. Shutemov,
Sasha Levin, Guillaume Morin, Wanpeng Li, linux-mm,
Jason Gunthorpe, linux-kernel, linux-fsdevel, Andrew Morton,
linux-doc, stable, Chris Mason
On Wed, Nov 25, 2026 at 2:35 PM David Hildenbrand (Arm)
<david@kernel.org> wrote:
>
> Since commit 8625147cafaa ("hugetlbfs: don't delete error page from
> pagecache"), a poisoned hugetlb folio stays in the page cache, and we
> must not access its poisoned pages.
>
> However, hugetlbfs_zero_partial_page() zeroes the partial folio of an
> unaligned hole punch without checking for poison, which can cause
> issues like machine-checks.
>
> Let's fix it by simply not zeroing poisoned folios. We don't really care
> about their content: the folio can no longer be mapped, and read() only
> returns data up to the first poisoned page.
>
> Only zeroing the pages that are not poisoned would need more thought, as
> we cannot reliably identify the poisoned pages; that is future work.
>
> Note that a folio that gets poisoned concurrently, after we checked it,
> can still be zeroed, and sync'ing with memory failure handling is also
> left as future work.
>
> This has low urgency, as it requires a hole punch on a poisoned folio.
>
> Fixes: 8625147cafaa ("hugetlbfs: don't delete error page from pagecache")
> Cc: <stable@vger.kernel.org>
> Reported-by: Chris Mason <mason@kernel.org>
> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
> ---
> fs/hugetlbfs/inode.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
> index 2e8672ae2fd52..5437a55d14f1c 100644
> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -656,7 +656,8 @@ static void hugetlbfs_zero_partial_page(struct hstate *h,
> if (!end)
> end = huge_page_size(h);
>
> - folio_zero_segment(folio, (size_t)start, (size_t)end);
> + if (!folio_test_hwpoison(folio))
> + folio_zero_segment(folio, (size_t)start, (size_t)end);
Clean fix, LGTM.
Reviewed-by: Jiaqi Yan <jiaqiyan@google.com>
>
> folio_unlock(folio);
> folio_put(folio);
>
> --
> 2.56.1
>
>
^ permalink raw reply [flat|nested] 23+ messages in thread
end of thread, other threads:[~2026-11-25 23:13 UTC | newest]
Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 22:32 [PATCH 00/21] mm/hugetlb: various fixes (2) David Hildenbrand (Arm)
2026-11-25 22:32 ` [PATCH 01/21] mm/hugetlb: charge the copy made at fork to the parent David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 02/21] mm/hugetlb: fix truncated copy result in UFFDIO_COPY David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 03/21] mm/hugetlb: always mark new page cache folios migratable David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 04/21] mm/memfd: mark new hugetlb " David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 05/21] mm/hugetlb: free surplus folios of other nodes on free David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 06/21] hugetlbfs: fix deadlock between hole punching and memory failure David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 07/21] hugetlbfs: don't zero poisoned pages in a hole punch David Hildenbrand (Arm)
2026-11-25 23:13 ` Jiaqi Yan
2026-11-25 22:33 ` [PATCH 08/21] hugetlbfs: recheck the vma range after the lock wait David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 09/21] mm/hugetlb: don't zap non-present entries when unmapping a folio David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 10/21] mm/hugetlb: only check PMD table sharing for PMD-sized folios David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 11/21] mm/migrate: check hugetlb PMD sharing in move_pages() David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 12/21] mm/hugetlb: don't share page tables of a VMA during mprotect() David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 13/21] mm/hugetlb: unshare PMDs of a split VMA if allocating its lock fails David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 14/21] mm/hugetlb: count migration entries at fork and unmap David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 15/21] mm/hugetlb: stop accounting HugetlbPages once PMD tables were shared David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 16/21] mm/hugetlb_cgroup: track in-use folios per cgroup David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 17/21] hugetlbfs: seal SHM_HUGETLB files against growth David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 18/21] fs/proc/task_mmu: don't clear VM_SOFTDIRTY on hugetlb VMAs David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 19/21] mm/hugetlb: use the VMA lock to stabilize flags for PMD sharing David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 20/21] mm/hugetlb: resolve protnone faults without a uffd bit David Hildenbrand (Arm)
2026-11-25 22:33 ` [PATCH 21/21] RFC: mm/hugetlb_cgroup: charge faults to mm->owner David Hildenbrand (Arm)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®