mm/migrate_device: fix pgtable leak in migrate_vma_insert_huge_pmd_page
Subsystems (2)
PATCH v2 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.
Add a pte_free() call in the unlock_abort error path to release the pgtable before returning.Also included before goto abort in the folio check path.
---
Changes in v2:
- Added pte_free() before goto abort in the folio_is_zone_device()
check path. The lock is not taken at this point so goto unlock_abort would be incorrect here.
- v1 only fixed the unlock_abort path, this version fixes both
leak locations.
mm/migrate_device.c | 2 ++ 1 file changed, 2 insertions(+)
mm/migrate_device.c +2 -0
@@ -840,6 +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)) { +pte_free (vma->vm_mm, pgtable); goto abort; } entry =folio_mk_pmd (folio, vma->vm_page_prot);
@@ -893,6 +894,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl); +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
Quoted 10 lines · Sunny Patel
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.Add a
pte_free()call in theunlock_aborterror path to release the pgtable before returning.Also included before goto abort in the folio check path.Signed-off-by Sunny Patel <[email protected]>---
pages")
Cc: <[email protected]>
Sashiko moans about a lot of unrelated stuff, one of them fixed by your other fix, and one of them to be cleaned up by your other patch.
There is another thing about migrate_vma_split_folio() and temporary split_folio() failures which I think we can ignore.
Signature · 3 lines
Cheers, David
Huang, Ying
reviewerQuoted 25 lines · Sunny Patel
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.Add a
pte_free()call in theunlock_aborterror path to release the pgtable before returning.Also included before goto abort in the folio check path.Signed-off-by Sunny Patel <[email protected]>---
Changes in v2:
- Addedpte_free()before goto abort in thefolio_is_zone_device()
check path. The lock is not taken at this point so gotounlock_abortwould be incorrect here.
- v1 only fixed theunlock_abortpath, this version fixes both
leak locations.mm/migrate_device.c | 2 ++ 1 file changed, 2 insertions(+)mm/migrate_device.c +0
-0
mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 844
Quoted 5 lines
mm/migrate_device.c +1
-0@@ -840,6 +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)) { +pte_free (vma->vm_mm, pgtable); goto abort;
goto free_abort?
mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 896
Quoted 6 lines
mm/migrate_device.c +0
-0} entry =folio_mk_pmd (folio, vma->vm_page_prot);@@ -893,6 +894,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, unlock_abort:spin_unlock (ptl);
free_abort:
mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 900
Quoted 4 lines
mm/migrate_device.c +1
-0+pte_free (vma->vm_mm, pgtable); abort:for (i =0 ; i <HPAGE_PMD_NR ; i++) src[i] &= ~MIGRATE_PFN_MIGRATE ;
Although I prefer guard based memory freeing, if we stick with the original error handling, I suggest following existing style more closely.
---
Best Regards,
Huang, Ying
…
Quoted 3 lines
Add a
pte_free()call in theunlock_aborterror path to release the pgtable before returning.Also included before goto abort in the folio check path.
See also:
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.0#n145
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.0#n34
Regards,
Markus