mm/migrate_device: fix pgtable leak in migrate_vma_insert_huge_pmd_page
Subsystems (2)
PATCH v3 1/1 mm/migrate_device: fix pgtable leak in migrate_vma_insert_huge_pmd_page
When migrate_vma_insert_huge_pmd_page() jumps to unlock_abort due to a PMD check failure, the pgtable allocated earlier via pte_alloc_one() is never freed, causing a memory leak.
Added free_abort label to release the pgtable in error path.
---
Changes in v3:
- Added free_abort label as suggested to release the pgtable in error path.
- v2 had the pte_free() call in both error paths, this version has a single goto target for both paths to avoid code duplication.
mm/migrate_device.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/migrate_device.c +3 -1
@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )
@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;
Signature · 1 line
2.43.0
Andrew Morton
maintainerQuoted 20 lines · On Fri, 1 May 2026 17:21:16 +0530 Sunny Patel <[email protected]>…
When
migrate_vma_insert_huge_pmd_page()jumps tounlock_abortdue to a PMD check failure, the pgtable allocated earlier viapte_alloc_one()is never freed, causing a memory leak.Added
free_abortlabel to release the pgtable in error path....
mm/migrate_device.c +1
-1@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )
mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 900
Quoted 8 lines
mm/migrate_device.c +2
-0@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;
Yikes, we leak that page on several error paths.
Thanks, I'll retain David's ack from the v2 patch.
Balbir, please review?
David Hildenbrand (Arm)
maintainerQuoted 33 lines · Andrew Morton
Quoted 20 lines · On Fri, 1 May 2026 17:21:16 +0530 Sunny Patel <[email protected]>…
When
migrate_vma_insert_huge_pmd_page()jumps tounlock_abortdue to a PMD check failure, the pgtable allocated earlier viapte_alloc_one()is never freed, causing a memory leak.Added
free_abortlabel to release the pgtable in error path....
mm/migrate_device.c +1
-1@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 900
Quoted 8 lines
mm/migrate_device.c +2
-0@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;Yikes, we leak that page on several error paths.
Quoted 1 line
Thanks, I'll retain David's ack from the v2 patch.
Yes. If we want to avoid more labels, we could do something like:
mm/migrate_device.c +14 -12
@@ -795,8 +795,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,struct folio *folio =page_folio (page);int ret;vm_fault_t csa_ret; -spinlock_t *ptl; -pgtable_t pgtable; +spinlock_t *ptl =NULL ; +pgtable_t pgtable =NULL ;pmd_t entry;bool flush = false;unsigned long i;
@@ -818,14 +818,14 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,count_vm_event (THP_FAULT_FALLBACK );count_mthp_stat (HPAGE_PMD_ORDER ,MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE ); ret = -ENOMEM ; - goto abort; + goto error; }__folio_mark_uptodate (folio); pgtable =pte_alloc_one (vma->vm_mm);if (unlikely (!pgtable)) - goto abort; + goto error;if (folio_is_device_private (folio)) {swp_entry_t swp_entry;
@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto error; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )
@@ -850,21 +850,21 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, ptl =pmd_lock (vma->vm_mm, pmdp); csa_ret =check_stable_address_space (vma->vm_mm);if (csa_ret) - goto unlock_abort; + goto error;/* * Check for userfaultfd but do not deliver the fault. Instead, * just back off. */ if (userfaultfd_missing (vma)) - goto unlock_abort; + goto error;if (!pmd_none (*pmdp)) {if (!is_huge_zero_pmd (*pmdp)) - goto unlock_abort; + goto error; flush = true; }else if (!pmd_none (*pmdp)) - goto unlock_abort; + goto error;add_mm_counter (vma->vm_mm,MM_ANONPAGES ,HPAGE_PMD_NR );folio_add_new_anon_rmap (folio, vma, addr,RMAP_EXCLUSIVE );
@@ -891,9 +891,11 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,return 0 ; -unlock_abort: -spin_unlock (ptl); -abort: +error: +if (ptl) +spin_unlock (ptl); +if (pgtable) +pte_free (vma->vm_mm, pgtable);for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;return 0 ;
Signature · 3 lines
Cheers, David
Balbir Singh
Reviewed-byQuoted 112 lines · David Hildenbrand (Arm)
Quoted 33 lines · Andrew Morton
Quoted 20 lines · On Fri, 1 May 2026 17:21:16 +0530 Sunny Patel <[email protected]>…
When
migrate_vma_insert_huge_pmd_page()jumps tounlock_abortdue to a PMD check failure, the pgtable allocated earlier viapte_alloc_one()is never freed, causing a memory leak.Added
free_abortlabel to release the pgtable in error path....
mm/migrate_device.c +1
-1@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 900
Quoted 8 lines
mm/migrate_device.c +2
-0@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;Yikes, we leak that page on several error paths.
Quoted 1 line
Thanks, I'll retain David's ack from the v2 patch.
Yes. If we want to avoid more labels, we could do something like:
mm/migrate_device.c +9
-10@@ -795,8 +795,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,struct folio *folio =page_folio (page);int ret;vm_fault_t csa_ret; -spinlock_t *ptl; -pgtable_t pgtable; +spinlock_t *ptl =NULL ; +pgtable_t pgtable =NULL ;pmd_t entry;bool flush = false;unsigned long i;@@ -818,14 +818,14 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,count_vm_event (THP_FAULT_FALLBACK );count_mthp_stat (HPAGE_PMD_ORDER ,MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE ); ret = -ENOMEM ; - goto abort; + goto error; }__folio_mark_uptodate (folio); pgtable =pte_alloc_one (vma->vm_mm);if (unlikely (!pgtable)) - goto abort; + goto error;if (folio_is_device_private (folio)) {swp_entry_t swp_entry;@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto error; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )@@ -850,21 +850,21 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, ptl =pmd_lock (vma->vm_mm, pmdp); csa_ret =check_stable_address_space (vma->vm_mm);if (csa_ret) - goto unlock_abort; + goto error;/* * Check for userfaultfd but do not deliver the fault. Instead, * just back off. */ if (userfaultfd_missing (vma)) - goto unlock_abort; + goto error;if (!pmd_none (*pmdp)) {if (!is_huge_zero_pmd (*pmdp)) - goto unlock_abort; + goto error; flush = true; }else if (!pmd_none (*pmdp)) - goto unlock_abort; + goto error;add_mm_counter (vma->vm_mm,MM_ANONPAGES ,HPAGE_PMD_NR );folio_add_new_anon_rmap (folio, vma, addr,RMAP_EXCLUSIVE );@@ -891,9 +891,11 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,return 0 ; -unlock_abort:
mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 901
Quoted 10 lines
mm/migrate_device.c +5
-2-spin_unlock (ptl); -abort: +error: +if (ptl) +spin_unlock (ptl); +if (pgtable) +pte_free (vma->vm_mm, pgtable);for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;return 0 ;
The changes look good!
David Hildenbrand (Arm)
maintainerQuoted 17 lines
Quoted 4 lines
mm/migrate_device.c +0
-1-unlock_abort:mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 901
Quoted 10 lines
mm/migrate_device.c +5
-2-spin_unlock (ptl); -abort: +error: +if (ptl) +spin_unlock (ptl); +if (pgtable) +pte_free (vma->vm_mm, pgtable);for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;return 0 ;The changes look good!
Quoted 1 line
Reviewed-by Balbir Singh <[email protected]>
Do we prefer that or what we have in v3?
In case we prefer what I posted, it would be great if Sunny could send a v4!
Signature · 3 lines
Cheers, David
Quoted 110 lines · David Hildenbrand (Arm)
Quoted 33 lines · Andrew Morton
Quoted 20 lines · On Fri, 1 May 2026 17:21:16 +0530 Sunny Patel <[email protected]>…
When
migrate_vma_insert_huge_pmd_page()jumps tounlock_abortdue to a PMD check failure, the pgtable allocated earlier viapte_alloc_one()is never freed, causing a memory leak.Added
free_abortlabel to release the pgtable in error path....
mm/migrate_device.c +1
-1@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 900
Quoted 8 lines
mm/migrate_device.c +2
-0@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;Yikes, we leak that page on several error paths.
Quoted 1 line
Thanks, I'll retain David's ack from the v2 patch.
Yes. If we want to avoid more labels, we could do something like:
mm/migrate_device.c +9
-10@@ -795,8 +795,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,struct folio *folio =page_folio (page);int ret;vm_fault_t csa_ret; -spinlock_t *ptl; -pgtable_t pgtable; +spinlock_t *ptl =NULL ; +pgtable_t pgtable =NULL ;pmd_t entry;bool flush = false;unsigned long i;@@ -818,14 +818,14 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,count_vm_event (THP_FAULT_FALLBACK );count_mthp_stat (HPAGE_PMD_ORDER ,MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE ); ret = -ENOMEM ; - goto abort; + goto error; }__folio_mark_uptodate (folio); pgtable =pte_alloc_one (vma->vm_mm);if (unlikely (!pgtable)) - goto abort; + goto error;if (folio_is_device_private (folio)) {swp_entry_t swp_entry;@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto error; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )@@ -850,21 +850,21 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, ptl =pmd_lock (vma->vm_mm, pmdp); csa_ret =check_stable_address_space (vma->vm_mm);if (csa_ret) - goto unlock_abort; + goto error;/* * Check for userfaultfd but do not deliver the fault. Instead, * just back off. */ if (userfaultfd_missing (vma)) - goto unlock_abort; + goto error;if (!pmd_none (*pmdp)) {if (!is_huge_zero_pmd (*pmdp)) - goto unlock_abort; + goto error; flush = true; }else if (!pmd_none (*pmdp)) - goto unlock_abort; + goto error;add_mm_counter (vma->vm_mm,MM_ANONPAGES ,HPAGE_PMD_NR );folio_add_new_anon_rmap (folio, vma, addr,RMAP_EXCLUSIVE );@@ -891,9 +891,11 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,return 0 ; -unlock_abort:
mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 901
Quoted 10 lines
mm/migrate_device.c +5
-2-spin_unlock (ptl); -abort: +error: +if (ptl) +spin_unlock (ptl); +if (pgtable) +pte_free (vma->vm_mm, pgtable);for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;return 0 ;
Both look good to me, feel free to add my
in the future versions.
---
Best Regards,
Huang, Ying
Quoted 33 lines · Andrew Morton
Quoted 20 lines · On Fri, 1 May 2026 17:21:16 +0530 Sunny Patel <[email protected]>…
When
migrate_vma_insert_huge_pmd_page()jumps tounlock_abortdue to a PMD check failure, the pgtable allocated earlier viapte_alloc_one()is never freed, causing a memory leak.Added
free_abortlabel to release the pgtable in error path....
mm/migrate_device.c +1
-1@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 900
Quoted 8 lines
mm/migrate_device.c +2
-0@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;Yikes, we leak that page on several error paths.
Quoted 3 lines
Thanks, I'll retain David's ack from the v2 patch.
Balbir, please review?
The issue can be reached if we try to migrate a non private/coherent folio (I assume DAX/P2PDMA) pages. Even migrate_vma_insert_page() has a similar pattern. Creating the pud/pmd/pte structure there is unnecessary (but not allocations occur, depends on pmd_none() for example), do we want to clean up all of it (need to think through)?
I'll re-review and send fixes as needed. Looks like David sent some fixes out, will review those as well.
Balbir
Quoted 39 lines · Balbir Singh
Quoted 33 lines · Andrew Morton
Quoted 20 lines · On Fri, 1 May 2026 17:21:16 +0530 Sunny Patel <[email protected]>…
When
migrate_vma_insert_huge_pmd_page()jumps tounlock_abortdue to a PMD check failure, the pgtable allocated earlier viapte_alloc_one()is never freed, causing a memory leak.Added
free_abortlabel to release the pgtable in error path....
mm/migrate_device.c +1
-1@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 900
Quoted 8 lines
mm/migrate_device.c +2
-0@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;Yikes, we leak that page on several error paths.
Quoted 3 lines
Thanks, I'll retain David's ack from the v2 patch.
Balbir, please review?
Quoted 8 lines
The issue can be reached if we try to migrate a non private/coherent folio (I assume DAX/P2PDMA) pages. Even
migrate_vma_insert_page()has a similar pattern. Creating the pud/pmd/pte structure there is unnecessary (but not allocations occur, depends onpmd_none()for example), do we want to clean up all of it (need to think through)?I'll re-review and send fixes as needed. Looks like David sent some fixes out, will review those as well.
I checked migrate_vma_insert_page() is no affected.
Balbir
Quoted 6 lines · On Sat, 2 May 2026 10:59:33 +1000 Balbir Singh <[email protected]> wrote:
Quoted 6 lines
The issue can be reached if we try to migrate a non private/coherent folio (I assume DAX/P2PDMA) pages. Even
migrate_vma_insert_page()has a similar pattern. Creating the pud/pmd/pte structure there is unnecessary (but not allocations occur, depends onpmd_none()for example), do we want to clean up all of it (need to think through)?
Quoted 6 lines
Quoted 3 lines
I'll re-review and send fixes as needed. Looks like David sent some fixes out, will review those as well.
I checked
migrate_vma_insert_page()is no affected.
Thanks.
What to do here. We have a cc:stable hotfix which is adequately tested and seems to have passed review. And a cleanup patch from David.
I'm inclined to upstream this patch as-is and leave the cleanup on David's TODO list (one more can't make any difference ;))
?
From: Sunny Patel <[email protected]>
Subject: mm/migrate_device: fix pgtable leak in migrate_vma_insert_huge_pmd_page
Date: Fri, 1 May 2026 17:21:16 +0530
When migrate_vma_insert_huge_pmd_page() jumps to unlock_abort due to a PMD check failure, the pgtable allocated earlier via pte_alloc_one() is never freed, causing a memory leak.
Added free_abort label to release the pgtable in error path.
---
mm/migrate_device.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/migrate_device.c renamed from mm/migrate_device.c~mm-migrate_device-fix-pgtable-leak-in-migrate_vma_insert_huge_pmd_page +3 -1
@@ -840,7 +840,7 @@ static int migrate_vma_insert_huge_pmd_p }else {if (folio_is_zone_device (folio) && !folio_is_device_coherent (folio)) { - goto abort; + goto free_abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);if (vma->vm_flags &VM_WRITE )
@@ -893,6 +893,8 @@ static int migrate_vma_insert_huge_pmd_p unlock_abort:spin_unlock (ptl); +free_abort: +pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;
_
David Hildenbrand (Arm)
maintainerQuoted 15 lines · Andrew Morton
Quoted 6 lines · On Sat, 2 May 2026 10:59:33 +1000 Balbir Singh <[email protected]> wrote:
Quoted 6 lines
The issue can be reached if we try to migrate a non private/coherent folio (I assume DAX/P2PDMA) pages. Even
migrate_vma_insert_page()has a similar pattern. Creating the pud/pmd/pte structure there is unnecessary (but not allocations occur, depends onpmd_none()for example), do we want to clean up all of it (need to think through)?Quoted 6 lines
Quoted 3 lines
I'll re-review and send fixes as needed. Looks like David sent some fixes out, will review those as well.
I checked
migrate_vma_insert_page()is no affected.
Quoted 7 lines
Thanks.
What to do here. We have a cc:stable hotfix which is adequately tested and seems to have passed review. And a cleanup patch from David.
I'm inclined to upstream this patch as-is and leave the cleanup on David's TODO list (one more can't make any difference ;))
No strong opinion from my side.
Signature · 3 lines
Cheers, David