Skip to contentSkip to conversation

mm: various (urgent?) fixes

PATCH Superseded David Hildenbrand (Arm) 28 participants 249 messages Oct 26 – Oct 30 Page 2 of 2

Subsystems (28)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
author maintainer
#225 lore raw

PATCH 42/46 mm/huge_memory: split huge PUDs when adjusting VMA boundaries

In reply to David Hildenbrand (Arm)

PFN mappings such as VFIO PCI BAR mappings can use PUD leaves while allowing VMA splits at page granularity. However, vma_adjust_trans_huge() only handles PMDs.

After faulting a writable PUD-sized VFIO BAR mapping, changing its first page to read-only with mprotect() makes the PMD walk treat the PUD leaf as a PMD table. Applying MADV_DONTFORK to the first PMD-sized chunk leaves the PUD spanning two VMAs: the PMD-aligned boundary skips splitting.

Walk the page tables once at each adjusted boundary and split a huge PUD or PMD leaf as needed. Handle PUD leaves even when the boundary is PMD-aligned.

Fixes f9e54c3a2f5b7 ("vfio/pci: implement huge_fault support")
---
 mm/huge_memory.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 10 deletions(-)
mm/huge_memory.c +60 -10
index 08e58cda7d37d..d090642ac8c2b 100644
@@ -3503,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
 	__split_huge_pmd(vma, pmd, address, freeze);
 }
 
-static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
+static bool pud_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
 {
+	if (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
+		return false;
+	if (IS_ALIGNED(address, HPAGE_PUD_SIZE))
+		return false;
 	/*
-	 * If the new address isn't hpage aligned and it could previously
-	 * contain an hugepage: check if we need to split an huge pmd.
+	 * Only selected VMAs support mapping PUDs for now. Lift the
+	 * restriction once this changes.
 	 */
-	if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
-	    range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
-			 ALIGN(address, HPAGE_PMD_SIZE)))
-		split_huge_pmd_address(vma, address, false);
+	if (!vma_is_dax(vma) && !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT))
+		return false;
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE),
+			    ALIGN(address, HPAGE_PUD_SIZE));
+}
+
+static bool pmd_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
+{
+	if (IS_ALIGNED(address, HPAGE_PMD_SIZE))
+		return false;
+
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
+			    ALIGN(address, HPAGE_PMD_SIZE));
+}
+
+static void split_huge_leaf(struct vm_area_struct *vma,
+		unsigned long address)
+{
+	const bool split_pud = pud_split_required(vma, address);
+	const bool split_pmd = pmd_split_required(vma, address);
+	pgd_t *pgd;
+	p4d_t *p4d;
+	pud_t *pud;
+	pmd_t *pmd;
+
+	/*
+	 * mmap, VMA and rmap write locks protect the walk; the split helpers
+	 * take the page table locks.
+	 */
+	if (!split_pud && !split_pmd)
+		return;
+
+	pgd = pgd_offset(vma->vm_mm, address);
+	if (!pgd_present(*pgd))
+		return;
+
+	p4d = p4d_offset(pgd, address);
+	if (!p4d_present(*p4d))
+		return;
+
+	pud = pud_offset(p4d, address);
+	if (split_pud && pud_leaf(*pud))
+		__split_huge_pud(vma, pud, address);
+	if (!split_pmd || !pud_present(*pud))
+		return;
+
+	pmd = pmd_offset(pud, address);
+	__split_huge_pmd(vma, pmd, address, false);
 }
 
 void vma_adjust_trans_huge(struct vm_area_struct *vma,
@@ -3521,14 +3571,14 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma,
 			   struct vm_area_struct *next)
 {
 	/* Check if we need to split start first. */
-	split_huge_pmd_if_needed(vma, start);
+	split_huge_leaf(vma, start);
 
 	/* Check if we need to split end next. */
-	split_huge_pmd_if_needed(vma, end);
+	split_huge_leaf(vma, end);
 
 	/* If we're incrementing next->vm_start, we might need to split it. */
 	if (next)
-		split_huge_pmd_if_needed(next, end);
+		split_huge_leaf(next, end);
 }
 
 /*
Signature · 1 line
2.43.0
#226 lore raw

Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

PFN mappings such as VFIO PCI BAR mappings can use PUD leaves while allowing VMA splits at page granularity. However, vma_adjust_trans_huge() only handles PMDs.

After faulting a writable PUD-sized VFIO BAR mapping, changing its first page to read-only with mprotect() makes the PMD walk treat the PUD leaf as a PMD table. Applying MADV_DONTFORK to the first PMD-sized chunk leaves the PUD spanning two VMAs: the PMD-aligned boundary skips splitting.

Quoted 7 lines

Walk the page tables once at each adjusted boundary and split a huge PUD or PMD leaf as needed. Handle PUD leaves even when the boundary is PMD-aligned.

Fixes f9e54c3a2f5b7 ("vfio/pci: implement huge_fault support")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=42
#227 lore raw

Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

In reply to David Hildenbrand (Arm)
Quoted 82 lines · David Hildenbrand (Arm)

PFN mappings such as VFIO PCI BAR mappings can use PUD leaves while allowing VMA splits at page granularity. However, vma_adjust_trans_huge() only handles PMDs.

After faulting a writable PUD-sized VFIO BAR mapping, changing its first page to read-only with mprotect() makes the PMD walk treat the PUD leaf as a PMD table. Applying MADV_DONTFORK to the first PMD-sized chunk leaves the PUD spanning two VMAs: the PMD-aligned boundary skips splitting.

Walk the page tables once at each adjusted boundary and split a huge PUD or PMD leaf as needed. Handle PUD leaves even when the boundary is PMD-aligned.

Fixes f9e54c3a2f5b7 ("vfio/pci: implement huge_fault support")
---
 mm/huge_memory.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 10 deletions(-)
mm/huge_memory.c +44 -7
index 08e58cda7d37d..d090642ac8c2b 100644
@@ -3503,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
 	__split_huge_pmd(vma, pmd, address, freeze);
 }
 
-static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
+static bool pud_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
 {
+	if (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
+		return false;
+	if (IS_ALIGNED(address, HPAGE_PUD_SIZE))
+		return false;
 	/*
-	 * If the new address isn't hpage aligned and it could previously
-	 * contain an hugepage: check if we need to split an huge pmd.
+	 * Only selected VMAs support mapping PUDs for now. Lift the
+	 * restriction once this changes.
 	 */
-	if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
-	    range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
-			 ALIGN(address, HPAGE_PMD_SIZE)))
-		split_huge_pmd_address(vma, address, false);
+	if (!vma_is_dax(vma) && !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT))
+		return false;
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE),
+			    ALIGN(address, HPAGE_PUD_SIZE));
+}
+
+static bool pmd_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
+{
+	if (IS_ALIGNED(address, HPAGE_PMD_SIZE))
+		return false;
+
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
+			    ALIGN(address, HPAGE_PMD_SIZE));
+}
+
+static void split_huge_leaf(struct vm_area_struct *vma,
+		unsigned long address)
+{
+	const bool split_pud = pud_split_required(vma, address);
+	const bool split_pmd = pmd_split_required(vma, address);
+	pgd_t *pgd;
+	p4d_t *p4d;
+	pud_t *pud;
+	pmd_t *pmd;
+
+	/*
+	 * mmap, VMA and rmap write locks protect the walk; the split helpers
+	 * take the page table locks.
+	 */
+	if (!split_pud && !split_pmd)
+		return;
+
+	pgd = pgd_offset(vma->vm_mm, address);
+	if (!pgd_present(*pgd))
+		return;

mm/huge_memory.c void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, · line 3562

Quoted 10 lines
mm/huge_memory.c +10 -0
+
+	p4d = p4d_offset(pgd, address);
+	if (!p4d_present(*p4d))
+		return;
+
+	pud = pud_offset(p4d, address);
+	if (split_pud && pud_leaf(*pud))
+		__split_huge_pud(vma, pud, address);
+	if (!split_pmd || !pud_present(*pud))
+		return;

This looks wrong.

We should bail out here if PUD is was a leaf and wasn't split. pmd_offset() below expects page table PUD entry.

Quoted 5 lines
mm/huge_memory.c +2 -0
+	pmd = pmd_offset(pud, address);
+	__split_huge_pmd(vma, pmd, address, false);
 }
 
 void vma_adjust_trans_huge(struct vm_area_struct *vma,
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author maintainer
#228 lore raw

Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

Quoted 96 lines · Kiryl Shutsemau
Quoted 82 lines · David Hildenbrand (Arm)

PFN mappings such as VFIO PCI BAR mappings can use PUD leaves while allowing VMA splits at page granularity. However, vma_adjust_trans_huge() only handles PMDs.

After faulting a writable PUD-sized VFIO BAR mapping, changing its first page to read-only with mprotect() makes the PMD walk treat the PUD leaf as a PMD table. Applying MADV_DONTFORK to the first PMD-sized chunk leaves the PUD spanning two VMAs: the PMD-aligned boundary skips splitting.

Walk the page tables once at each adjusted boundary and split a huge PUD or PMD leaf as needed. Handle PUD leaves even when the boundary is PMD-aligned.

Fixes f9e54c3a2f5b7 ("vfio/pci: implement huge_fault support")
---
 mm/huge_memory.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 10 deletions(-)
mm/huge_memory.c +44 -7
index 08e58cda7d37d..d090642ac8c2b 100644
@@ -3503,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
 	__split_huge_pmd(vma, pmd, address, freeze);
 }
 
-static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
+static bool pud_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
 {
+	if (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
+		return false;
+	if (IS_ALIGNED(address, HPAGE_PUD_SIZE))
+		return false;
 	/*
-	 * If the new address isn't hpage aligned and it could previously
-	 * contain an hugepage: check if we need to split an huge pmd.
+	 * Only selected VMAs support mapping PUDs for now. Lift the
+	 * restriction once this changes.
 	 */
-	if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
-	    range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
-			 ALIGN(address, HPAGE_PMD_SIZE)))
-		split_huge_pmd_address(vma, address, false);
+	if (!vma_is_dax(vma) && !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT))
+		return false;
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE),
+			    ALIGN(address, HPAGE_PUD_SIZE));
+}
+
+static bool pmd_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
+{
+	if (IS_ALIGNED(address, HPAGE_PMD_SIZE))
+		return false;
+
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
+			    ALIGN(address, HPAGE_PMD_SIZE));
+}
+
+static void split_huge_leaf(struct vm_area_struct *vma,
+		unsigned long address)
+{
+	const bool split_pud = pud_split_required(vma, address);
+	const bool split_pmd = pmd_split_required(vma, address);
+	pgd_t *pgd;
+	p4d_t *p4d;
+	pud_t *pud;
+	pmd_t *pmd;
+
+	/*
+	 * mmap, VMA and rmap write locks protect the walk; the split helpers
+	 * take the page table locks.
+	 */
+	if (!split_pud && !split_pmd)
+		return;
+
+	pgd = pgd_offset(vma->vm_mm, address);
+	if (!pgd_present(*pgd))
+		return;

mm/huge_memory.c void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, · line 3562

Quoted 10 lines
mm/huge_memory.c +10 -0
+
+	p4d = p4d_offset(pgd, address);
+	if (!p4d_present(*p4d))
+		return;
+
+	pud = pud_offset(p4d, address);
+	if (split_pud && pud_leaf(*pud))
+		__split_huge_pud(vma, pud, address);
+	if (!split_pmd || !pud_present(*pud))
+		return;

This looks wrong.

Quoted 1 line

We should bail out here if PUD is was a leaf and wasn't split.

__split_huge_pud() must split a leave. Either by zapping it (what we do right now) or by populating page tables (as in Usama's PoC).

We can throw in here a

	if (WARN_ON_ONCE(!pud_leaf(pud)))
		return;

But I don't see how that would be required.

If we run into an un-splittable PUD something else is completely messed up.

(this is not hugetlb code :) )

Signature · 3 lines
Cheers,

David
#229 lore raw

Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

Quoted 97 lines · David Hildenbrand (Arm)
Quoted 96 lines · Kiryl Shutsemau
Quoted 82 lines · David Hildenbrand (Arm)

PFN mappings such as VFIO PCI BAR mappings can use PUD leaves while allowing VMA splits at page granularity. However, vma_adjust_trans_huge() only handles PMDs.

After faulting a writable PUD-sized VFIO BAR mapping, changing its first page to read-only with mprotect() makes the PMD walk treat the PUD leaf as a PMD table. Applying MADV_DONTFORK to the first PMD-sized chunk leaves the PUD spanning two VMAs: the PMD-aligned boundary skips splitting.

Walk the page tables once at each adjusted boundary and split a huge PUD or PMD leaf as needed. Handle PUD leaves even when the boundary is PMD-aligned.

Fixes f9e54c3a2f5b7 ("vfio/pci: implement huge_fault support")
---
 mm/huge_memory.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 60 insertions(+), 10 deletions(-)
mm/huge_memory.c +44 -7
index 08e58cda7d37d..d090642ac8c2b 100644
@@ -3503,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
 	__split_huge_pmd(vma, pmd, address, freeze);
 }
 
-static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
+static bool pud_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
 {
+	if (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD))
+		return false;
+	if (IS_ALIGNED(address, HPAGE_PUD_SIZE))
+		return false;
 	/*
-	 * If the new address isn't hpage aligned and it could previously
-	 * contain an hugepage: check if we need to split an huge pmd.
+	 * Only selected VMAs support mapping PUDs for now. Lift the
+	 * restriction once this changes.
 	 */
-	if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
-	    range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
-			 ALIGN(address, HPAGE_PMD_SIZE)))
-		split_huge_pmd_address(vma, address, false);
+	if (!vma_is_dax(vma) && !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT))
+		return false;
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE),
+			    ALIGN(address, HPAGE_PUD_SIZE));
+}
+
+static bool pmd_split_required(const struct vm_area_struct *vma,
+		unsigned long address)
+{
+	if (IS_ALIGNED(address, HPAGE_PMD_SIZE))
+		return false;
+
+	return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
+			    ALIGN(address, HPAGE_PMD_SIZE));
+}
+
+static void split_huge_leaf(struct vm_area_struct *vma,
+		unsigned long address)
+{
+	const bool split_pud = pud_split_required(vma, address);
+	const bool split_pmd = pmd_split_required(vma, address);
+	pgd_t *pgd;
+	p4d_t *p4d;
+	pud_t *pud;
+	pmd_t *pmd;
+
+	/*
+	 * mmap, VMA and rmap write locks protect the walk; the split helpers
+	 * take the page table locks.
+	 */
+	if (!split_pud && !split_pmd)
+		return;
+
+	pgd = pgd_offset(vma->vm_mm, address);
+	if (!pgd_present(*pgd))
+		return;

mm/huge_memory.c void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, · line 3562

Quoted 10 lines
mm/huge_memory.c +10 -0
+
+	p4d = p4d_offset(pgd, address);
+	if (!p4d_present(*p4d))
+		return;
+
+	pud = pud_offset(p4d, address);
+	if (split_pud && pud_leaf(*pud))
+		__split_huge_pud(vma, pud, address);
+	if (!split_pmd || !pud_present(*pud))
+		return;

This looks wrong.

Quoted 4 lines
Quoted 1 line

We should bail out here if PUD is was a leaf and wasn't split.

__split_huge_pud() must split a leave. Either by zapping it (what we do right now) or by populating page tables (as in Usama's PoC).

No, meant the situation when pud_leaf() is true, but split_pud is false, so __split_huge_pud() is never hit. pud_present() is true so we proceed to pmd_offset()

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author maintainer
#230 lore raw

Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

Quoted 5 lines · Kiryl Shutsemau
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 3 lines · Kiryl Shutsemau

This looks wrong.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

We should bail out here if PUD is was a leaf and wasn't split.

__split_huge_pud() must split a leave. Either by zapping it (what we do right now) or by populating page tables (as in Usama's PoC).

No, meant the situation when pud_leaf() is true, but split_pud is false, so __split_huge_pud() is never hit. pud_present() is true so we proceed to pmd_offset()

Oh, yeah, I can add a WARN_ON_ONCE() for that, it would be a bug in the software.

I was a bit careful in pud_split_required() to not harm performance of things that don't need a split because pud leaves (should be) are impossible.

Signature · 3 lines
Cheers,

David
author maintainer
#231 lore raw

Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

Quoted 11 lines · David Hildenbrand (Arm)
Quoted 8 lines · Kiryl Shutsemau
Quoted 3 lines · David Hildenbrand (Arm)

__split_huge_pud() must split a leave. Either by zapping it (what we do right now) or by populating page tables (as in Usama's PoC).

No, meant the situation when pud_leaf() is true, but split_pud is false, so __split_huge_pud() is never hit. pud_present() is true so we proceed to pmd_offset()

Oh, yeah, I can add a WARN_ON_ONCE() for that, it would be a bug in the software.

Ah, or I can just drop the split_pud check. Let me do that.

+static void split_huge_leaf(struct vm_area_struct *vma,
+ unsigned long address)
+{

+       const bool split_pud = pud_split_required(vma, address);
+       const bool split_pmd = pmd_split_required(vma, address);
+       pgd_t *pgd;
+       p4d_t *p4d;
+       pud_t *pud;
+       pmd_t *pmd;
+
+       /*
+        * mmap, VMA and rmap write locks protect the walk; the split helpers
+        * take the page table locks.
+        */

+ if (!split_pud && !split_pmd)
+ return;
+
+ pgd = pgd_offset(vma->vm_mm, address);
+ if (!pgd_present(*pgd))
+ return;
+
+ p4d = p4d_offset(pgd, address);
+ if (!p4d_present(*p4d))
+ return;
+
+ pud = pud_offset(p4d, address);
+ if (pud_leaf(*pud))
+ __split_huge_pud(vma, pud, address);
+ if (!split_pmd || !pud_present(*pud))
+ return;
+
+ pmd = pmd_offset(pud, address);
+ __split_huge_pmd(vma, pmd, address, false);
}

Signature · 3 lines
Cheers,

David
#232 lore raw

Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 5 lines · Kiryl Shutsemau
Quoted 3 lines · David Hildenbrand (Arm)

__split_huge_pud() must split a leave. Either by zapping it (what we do right now) or by populating page tables (as in Usama's PoC).

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

No, meant the situation when pud_leaf() is true, but split_pud is false, so __split_huge_pud() is never hit. pud_present() is true so we proceed to pmd_offset()

Oh, yeah, I can add a WARN_ON_ONCE() for that, it would be a bug in the software.

Ah, or I can just drop the split_pud check. Let me do that.

Yeah, looks good.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author maintainer
#233 lore raw

PATCH 43/46 mm/vma: don't leak the charge for stack growth

In reply to David Hildenbrand (Arm)

acct_stack_growth() charges stack expansion even without VM_ACCOUNT. This can happen for read-only or MAP_NORESERVE mappings created with MAP_GROWSDOWN. Unmapping these VMAs does not release the charge, leaking Committed_AS.

Only check and charge growth for VM_ACCOUNT VMAs, matching mremap(). Unaccounted growth skips the overcommit check while remaining subject to the other stack-growth limits.

---
 mm/vma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vma.c +2 -1
index acdf00346cda5..695d0be85ac86 100644
@@ -3391,7 +3391,8 @@ static int acct_stack_growth(struct vm_area_struct *vma,
 	 * Overcommit..  This must be the final test, as it will
 	 * update security statistics.
 	 */
-	if (security_vm_enough_memory_mm(mm, grow))
+	if (vma_test(vma, VMA_ACCOUNT_BIT) &&
+	    security_vm_enough_memory_mm(mm, grow))
 		return -ENOMEM;
 
 	return 0;
Signature · 1 line
2.43.0
#234 lore raw

Re: [PATCH 43/46] mm/vma: don't leak the charge for stack growth

Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

acct_stack_growth() charges stack expansion even without VM_ACCOUNT. This can happen for read-only or MAP_NORESERVE mappings created with MAP_GROWSDOWN. Unmapping these VMAs does not release the charge, leaking Committed_AS.

Only check and charge growth for VM_ACCOUNT VMAs, matching mremap(). Unaccounted growth skips the overcommit check while remaining subject to the other stack-growth limits.

Quoted 5 lines

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=43
reviewer Reviewed-by on v1 43/46
#235 lore raw

Re: [PATCH 43/46] mm/vma: don't leak the charge for stack growth

In reply to David Hildenbrand (Arm)
Quoted 9 lines · David Hildenbrand (Arm)

acct_stack_growth() charges stack expansion even without VM_ACCOUNT. This can happen for read-only or MAP_NORESERVE mappings created with MAP_GROWSDOWN. Unmapping these VMAs does not release the charge, leaking Committed_AS.

Only check and charge growth for VM_ACCOUNT VMAs, matching mremap(). Unaccounted growth skips the overcommit check while remaining subject to the other stack-growth limits.

Quoted 5 lines
Signature · 1 line
Pedro
reviewer Reviewed-by on v1 43/46
#236 lore raw

Re: [PATCH 43/46] mm/vma: don't leak the charge for stack growth

In reply to David Hildenbrand (Arm)
Quoted 9 lines · David Hildenbrand (Arm)

acct_stack_growth() charges stack expansion even without VM_ACCOUNT. This can happen for read-only or MAP_NORESERVE mappings created with MAP_GROWSDOWN. Unmapping these VMAs does not release the charge, leaking Committed_AS.

Only check and charge growth for VM_ACCOUNT VMAs, matching mremap(). Unaccounted growth skips the overcommit check while remaining subject to the other stack-growth limits.

Quoted 6 lines

---

Looks good to me,

Maybe worth having a warning when it's accounting w/o VMA_ACCOUNT_BIT or let those helpers skip when there's no VMA_ACCOUNT_BIT set...

Signature · 2 lines
Cheers,
Harry / Hyeonggon
author maintainer
#237 lore raw

Re: [PATCH 43/46] mm/vma: don't leak the charge for stack growth

Quoted 17 lines · Harry Yoo
Quoted 9 lines · David Hildenbrand (Arm)

acct_stack_growth() charges stack expansion even without VM_ACCOUNT. This can happen for read-only or MAP_NORESERVE mappings created with MAP_GROWSDOWN. Unmapping these VMAs does not release the charge, leaking Committed_AS.

Only check and charge growth for VM_ACCOUNT VMAs, matching mremap(). Unaccounted growth skips the overcommit check while remaining subject to the other stack-growth limits.

Quoted 6 lines

---

Quoted 5 lines

Looks good to me,

Maybe worth having a warning when it's accounting w/o VMA_ACCOUNT_BIT or let those helpers skip when there's no VMA_ACCOUNT_BIT set...

You mean passing the VMA instead of the MM?

Signature · 3 lines
Cheers,

David
author maintainer
#238 lore raw

PATCH 44/46 mm/cma: don't release CMA pages still in use

In reply to David Hildenbrand (Arm)

From: Rik van Riel <[email protected]>

When a driver calls dma_free_contiguous() before quiescing DMA, the page might still have a reference from the device. put_page_testzero() there returns false, WARN fires, but the code proceeds to free_contig_frozen_range() putting a live page onto buddy and clearing the bitmap. A subsequent allocation can hand the same PFN to a new owner while the original holder still references it.

A concurrent put_page() that drops the last reference between the testzero loop and free_contig_frozen_range() can double-queue the page via page->lru, corrupting buddy lists.

Avoid the corruption by not freeing a CMA region if any of the pages inside are still in use.

Make it explicit in the warning that the driver allowed a leak.

While at it, use page_ref_freeze() to make the semantics clearer and switch to pr_warn_ratelimited().

Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
[ david: use page_ref_freeze() + pr_warn_ratelimited() ]
---
 mm/cma.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/cma.c +7 -3
index a10ea37a261da..871f95c9ae9f0 100644
@@ -1020,7 +1020,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
 		 unsigned long count)
 {
 	struct cma_memrange *cmr;
-	unsigned long ret = 0;
+	unsigned long leaked = 0;
 	unsigned long i, pfn;
 
 	cmr = find_cma_memrange(cma, pages, count);
@@ -1029,9 +1029,13 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
 	pfn = page_to_pfn(pages);
 	for (i = 0; i < count; i++, pfn++)
-		ret += !put_page_testzero(pfn_to_page(pfn));
+		leaked += !page_ref_freeze(pfn_to_page(pfn), 1);
 
-	WARN(ret, "%lu pages are still in use!\n", ret);
+	if (leaked) {
+		pr_warn_ratelimited("%lu pages are still in use, not freeing CMA region!\n",
+				   leaked);
+		return true;
+	}
 
 	__cma_release_frozen(cma, cmr, pages, count);
 
Signature · 1 line
2.43.0
#239 lore raw

Re: [PATCH 44/46] mm/cma: don't release CMA pages still in use

Sashiko has reviewed this patch and found no issues.
Quoted 21 lines

From: Rik van Riel <[email protected]>

When a driver calls dma_free_contiguous() before quiescing DMA, the page might still have a reference from the device. put_page_testzero() there returns false, WARN fires, but the code proceeds to free_contig_frozen_range() putting a live page onto buddy and clearing the bitmap. A subsequent allocation can hand the same PFN to a new owner while the original holder still references it.

A concurrent put_page() that drops the last reference between the testzero loop and free_contig_frozen_range() can double-queue the page via page->lru, corrupting buddy lists.

Avoid the corruption by not freeing a CMA region if any of the pages inside are still in use.

Make it explicit in the warning that the driver allowed a leak.

While at it, use page_ref_freeze() to make the semantics clearer and switch to pr_warn_ratelimited().

Quoted 6 lines
Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
[ david: use page_ref_freeze() + pr_warn_ratelimited() ]

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=44
maintainer Acked-by on v1 44/46
#240 lore raw

Re: [PATCH 44/46] mm/cma: don't release CMA pages still in use

In reply to David Hildenbrand (Arm)
Quoted 21 lines · David Hildenbrand (Arm)

From: Rik van Riel <[email protected]>

When a driver calls dma_free_contiguous() before quiescing DMA, the page might still have a reference from the device. put_page_testzero() there returns false, WARN fires, but the code proceeds to free_contig_frozen_range() putting a live page onto buddy and clearing the bitmap. A subsequent allocation can hand the same PFN to a new owner while the original holder still references it.

A concurrent put_page() that drops the last reference between the testzero loop and free_contig_frozen_range() can double-queue the page via page->lru, corrupting buddy lists.

Avoid the corruption by not freeing a CMA region if any of the pages inside are still in use.

Make it explicit in the warning that the driver allowed a leak.

While at it, use page_ref_freeze() to make the semantics clearer and switch to pr_warn_ratelimited().

Quoted 6 lines
Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
[ david: use page_ref_freeze() + pr_warn_ratelimited() ]
Quoted 35 lines
---
 mm/cma.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
mm/cma.c +7 -4
index a10ea37a261da..871f95c9ae9f0 100644
@@ -1020,7 +1020,7 @@ bool cma_release(struct cma *cma, const struct page *pages,
 		 unsigned long count)
 {
 	struct cma_memrange *cmr;
-	unsigned long ret = 0;
+	unsigned long leaked = 0;
 	unsigned long i, pfn;
 
 	cmr = find_cma_memrange(cma, pages, count);
@@ -1029,9 +1029,13 @@ bool cma_release(struct cma *cma, const struct page *pages,
 
 	pfn = page_to_pfn(pages);
 	for (i = 0; i < count; i++, pfn++)
-		ret += !put_page_testzero(pfn_to_page(pfn));
+		leaked += !page_ref_freeze(pfn_to_page(pfn), 1);
 
-	WARN(ret, "%lu pages are still in use!\n", ret);
+	if (leaked) {
+		pr_warn_ratelimited("%lu pages are still in use, not freeing CMA region!\n",
+				   leaked);
+		return true;
+	}
 
 	__cma_release_frozen(cma, cmr, pages, count);
 
 
--
Quoted 2 lines

2.43.0

--
Cheers, Lorenzo

maintainer Acked-by on v1 44/46
#241 lore raw

Re: [PATCH 44/46] mm/cma: don't release CMA pages still in use

In reply to David Hildenbrand (Arm)
Quoted 25 lines · David Hildenbrand (Arm)

From: Rik van Riel <[email protected]>

When a driver calls dma_free_contiguous() before quiescing DMA, the page might still have a reference from the device. put_page_testzero() there returns false, WARN fires, but the code proceeds to free_contig_frozen_range() putting a live page onto buddy and clearing the bitmap. A subsequent allocation can hand the same PFN to a new owner while the original holder still references it.

A concurrent put_page() that drops the last reference between the testzero loop and free_contig_frozen_range() can double-queue the page via page->lru, corrupting buddy lists.

Avoid the corruption by not freeing a CMA region if any of the pages inside are still in use.

Make it explicit in the warning that the driver allowed a leak.

While at it, use page_ref_freeze() to make the semantics clearer and switch to pr_warn_ratelimited().

Quoted 6 lines
Fixes 9bda131c6093 ("mm: cma: add cma_alloc_frozen{_compound}()")
[ david: use page_ref_freeze() + pr_warn_ratelimited() ]

Thanks

author maintainer
#242 lore raw

PATCH 45/46 mm/memory-tiers: unregister empty tiers outside memory_tier_lock

In reply to David Hildenbrand (Arm)

When offlining a node's last memory empties its tier, clear_node_memory_tier() calls device_unregister() while holding memory_tier_lock. A concurrent sysfs read can hold a kernfs active reference and block in nodelist_show() on that mutex. device_unregister() then waits in kernfs_drain() for the read to finish and release its active reference, while the reader waits for the offlining task to release the mutex.

Return the node's former tier from clear_node_memory_tier(), making the helper more similar to set_node_memory_tier(), which also returns a memory_tier pointer. Let the caller unlink an empty tier under memory_tier_lock before rebuilding demotion targets, then unregister its device after dropping the lock.

Fixes 9832fb87834e ("mm/demotion: expose memory tier details via sysfs")
---
 mm/memory-tiers.c | 29 +++++++++++++----------------
 1 file changed, 13 insertions(+), 16 deletions(-)
mm/memory-tiers.c +13 -16
index 25e121851b586..8eda313cf6192 100644
@@ -572,21 +572,14 @@ static struct memory_tier *set_node_memory_tier(int node)
 	return memtier;
 }
 
-static void destroy_memory_tier(struct memory_tier *memtier)
+static struct memory_tier *clear_node_memory_tier(int node)
 {
-	list_del(&memtier->list);
-	device_unregister(&memtier->dev);
-}
-
-static bool clear_node_memory_tier(int node)
-{
-	bool cleared = false;
 	pg_data_t *pgdat;
 	struct memory_tier *memtier;
 
 	pgdat = NODE_DATA(node);
 	if (!pgdat)
-		return false;
+		return NULL;
 
 	/*
 	 * Make sure that anybody looking at NODE_DATA who finds
@@ -604,14 +597,10 @@ static bool clear_node_memory_tier(int node)
 		synchronize_rcu();
 		memtype = node_memory_types[node].memtype;
 		node_clear(node, memtype->nodes);
-		if (nodes_empty(memtype->nodes)) {
+		if (nodes_empty(memtype->nodes))
 			list_del_init(&memtype->tier_sibling);
-			if (list_empty(&memtier->memory_types))
-				destroy_memory_tier(memtier);
-		}
-		cleared = true;
 	}
-	return cleared;
+	return memtier;
 }
 
 static void release_memtype(struct kref *kref)
@@ -884,13 +873,21 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self,
 {
 	struct memory_tier *memtier;
 	struct node_notify *nn = _arg;
+	bool empty = false;
 
 	switch (action) {
 	case NODE_REMOVED_LAST_MEMORY:
 		mutex_lock(&memory_tier_lock);
-		if (clear_node_memory_tier(nn->nid))
+		memtier = clear_node_memory_tier(nn->nid);
+		if (memtier) {
+			empty = list_empty(&memtier->memory_types);
+			if (empty)
+				list_del(&memtier->list);
 			establish_demotion_targets();
+		}
 		mutex_unlock(&memory_tier_lock);
+		if (empty)
+			device_unregister(&memtier->dev);
 		break;
 	case NODE_ADDED_FIRST_MEMORY:
 		mutex_lock(&memory_tier_lock);
Signature · 1 line
2.43.0
#243 lore raw

Re: [PATCH 45/46] mm/memory-tiers: unregister empty tiers outside memory_tier_lock

Sashiko has reviewed this patch and found no issues.
Quoted 14 lines

When offlining a node's last memory empties its tier, clear_node_memory_tier() calls device_unregister() while holding memory_tier_lock. A concurrent sysfs read can hold a kernfs active reference and block in nodelist_show() on that mutex. device_unregister() then waits in kernfs_drain() for the read to finish and release its active reference, while the reader waits for the offlining task to release the mutex.

Return the node's former tier from clear_node_memory_tier(), making the helper more similar to set_node_memory_tier(), which also returns a memory_tier pointer. Let the caller unlink an empty tier under memory_tier_lock before rebuilding demotion targets, then unregister its device after dropping the lock.

Quoted 4 lines
Fixes 9832fb87834e ("mm/demotion: expose memory tier details via sysfs")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=45
maintainer Reviewed-by on v1 45/46
#244 lore raw

Re: [PATCH 45/46] mm/memory-tiers: unregister empty tiers outside memory_tier_lock

In reply to David Hildenbrand (Arm)
Quoted 14 lines · David Hildenbrand (Arm)

When offlining a node's last memory empties its tier, clear_node_memory_tier() calls device_unregister() while holding memory_tier_lock. A concurrent sysfs read can hold a kernfs active reference and block in nodelist_show() on that mutex. device_unregister() then waits in kernfs_drain() for the read to finish and release its active reference, while the reader waits for the offlining task to release the mutex.

Return the node's former tier from clear_node_memory_tier(), making the helper more similar to set_node_memory_tier(), which also returns a memory_tier pointer. Let the caller unlink an empty tier under memory_tier_lock before rebuilding demotion targets, then unregister its device after dropping the lock.

Quoted 4 lines
Fixes 9832fb87834e ("mm/demotion: expose memory tier details via sysfs")
Signature · 1 line
Gregory Price (Meta) <[email protected]>
author maintainer
#245 lore raw

PATCH 46/46 fs/proc/task_mmu: refuse addresses beyond 32 bits on 32bit

In reply to David Hildenbrand (Arm)

From: Josef Bacik <[email protected]>

The addresses in struct pm_scan_arg are 64 bits wide. The first thing pagemap_scan_get_args() does with them is to cast them to unsigned long. On a 32-bit kernel the upper half is dropped without a word:
PAGEMAP_SCAN of 0x100001000-0x100002000 scans, and with PM_SCAN_WP_MATCHING write-protects, 0x1000-0x2000, stores the result at the lower half of vec, and reports success.

Return -EFAULT, as for other addresses outside of the address space.

This is probably not worth CCing stable.

Fix generated by an LLM and sent by Josef Bacik in private.

Fixes 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
---
 fs/proc/task_mmu.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/proc/task_mmu.c +6 -0
index 7452b695fdaec..762b6dbb81e4a 100644
@@ -3032,6 +3032,12 @@ static int pagemap_scan_get_args(struct pm_scan_arg *arg,
 	     arg->category_anyof_mask | arg->return_mask) & ~PM_SCAN_CATEGORIES)
 		return -EINVAL;
 
+	/* Don't cut 64-bit numbers down to addresses that happen to work. */
+	if (arg->start != (unsigned long)arg->start ||
+	    arg->end != (unsigned long)arg->end ||
+	    arg->vec != (unsigned long)arg->vec)
+		return -EFAULT;
+
 	arg->start = untagged_addr((unsigned long)arg->start);
 	arg->end = untagged_addr((unsigned long)arg->end);
 	arg->vec = untagged_addr((unsigned long)arg->vec);
Signature · 1 line
2.43.0
#246 lore raw

Re: [PATCH 46/46] fs/proc/task_mmu: refuse addresses beyond 32 bits on 32bit

Sashiko has reviewed this patch and found no issues.
Quoted 11 lines

From: Josef Bacik <[email protected]>

The addresses in struct pm_scan_arg are 64 bits wide. The first thing pagemap_scan_get_args() does with them is to cast them to unsigned long. On a 32-bit kernel the upper half is dropped without a word:
PAGEMAP_SCAN of 0x100001000-0x100002000 scans, and with PM_SCAN_WP_MATCHING write-protects, 0x1000-0x2000, stores the result at the lower half of vec, and reports success.

Return -EFAULT, as for other addresses outside of the address space.

Quoted 8 lines

This is probably not worth CCing stable.

Fix generated by an LLM and sent by Josef Bacik in private.

Fixes 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=46
reviewer Reviewed-by on v1 46/46
#247 lore raw

Re: [PATCH 46/46] fs/proc/task_mmu: refuse addresses beyond 32 bits on 32bit

In reply to David Hildenbrand (Arm)
Quoted 11 lines · David Hildenbrand (Arm)

From: Josef Bacik <[email protected]>

The addresses in struct pm_scan_arg are 64 bits wide. The first thing pagemap_scan_get_args() does with them is to cast them to unsigned long. On a 32-bit kernel the upper half is dropped without a word:
PAGEMAP_SCAN of 0x100001000-0x100002000 scans, and with PM_SCAN_WP_MATCHING write-protects, 0x1000-0x2000, stores the result at the lower half of vec, and reports success.

Return -EFAULT, as for other addresses outside of the address space.

Quoted 8 lines

This is probably not worth CCing stable.

Fix generated by an LLM and sent by Josef Bacik in private.

Fixes 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Signature · 1 line
Pedro
author maintainer
#248 lore raw
In reply to David Hildenbrand (Arm)
Quoted 22 lines · David Hildenbrand (Arm)

Another set of fixes from the current batch that focuses on all core-mm things that won't be sent out separately by others. I decided to bundle these together as they are all over the place.

I have some hugetlb stuff in the works which is pretty horrible (code wise ;) ), that I will send out once ready. Plus some low-prio migrate_device oddities + two core-mm things that need more thought.

This is the biggest set of patches coming from me, so don't be scared ;)

Once more, some of the stuff is more urgent than others. No fixes are crazy big and most are easy to review.

The majority are again fixes from Anthropic that I cleaned up or reworked. While doing that, I found a bunch of other issues and tried to fix them somewhat cleanly.

I gave it at least some basic testing on x86-64; more testing will be highly appreciated.

Quoted 6 lines

This is based on akpm/mm-stable.

As with the shmem fixes, please prioritize review of these fixes, I want to get fixes for the issues in a timely manner upstream.

Thanks to everyone helping out with moving the flood of fixes forward :)

Thanks a lot everyone for the review.

I think I have tags on all except one fix:

[PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries

I'll likely send a v2 this evening/tomorrow and then try to get these things into linux-next by picking that.

Signature · 3 lines
Cheers,

David