Skip to content

Commit 12c9d70

Browse files
Matthew Wilcoxtorvalds
authored andcommitted
mm: fix memory leak in copy_huge_pmd()
We allocate a pgtable but do not attach it to anything if the PMD is in a DAX VMA, causing it to leak. We certainly try to not free pgtables associated with the huge zero page if the zero page is in a DAX VMA, so I think this is the right solution. This needs to be properly audited. Signed-off-by: Matthew Wilcox <[email protected]> Cc: Dan Williams <[email protected]> Acked-by: Kirill A. Shutemov <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c6400ba commit 12c9d70

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

mm/huge_memory.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,8 @@ static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
858858
return false;
859859
entry = mk_pmd(zero_page, vma->vm_page_prot);
860860
entry = pmd_mkhuge(entry);
861-
pgtable_trans_huge_deposit(mm, pmd, pgtable);
861+
if (pgtable)
862+
pgtable_trans_huge_deposit(mm, pmd, pgtable);
862863
set_pmd_at(mm, haddr, pmd, entry);
863864
atomic_long_inc(&mm->nr_ptes);
864865
return true;
@@ -1036,13 +1037,15 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10361037
spinlock_t *dst_ptl, *src_ptl;
10371038
struct page *src_page;
10381039
pmd_t pmd;
1039-
pgtable_t pgtable;
1040+
pgtable_t pgtable = NULL;
10401041
int ret;
10411042

1042-
ret = -ENOMEM;
1043-
pgtable = pte_alloc_one(dst_mm, addr);
1044-
if (unlikely(!pgtable))
1045-
goto out;
1043+
if (!vma_is_dax(vma)) {
1044+
ret = -ENOMEM;
1045+
pgtable = pte_alloc_one(dst_mm, addr);
1046+
if (unlikely(!pgtable))
1047+
goto out;
1048+
}
10461049

10471050
dst_ptl = pmd_lock(dst_mm, dst_pmd);
10481051
src_ptl = pmd_lockptr(src_mm, src_pmd);
@@ -1073,7 +1076,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
10731076
goto out_unlock;
10741077
}
10751078

1076-
if (pmd_trans_huge(pmd)) {
1079+
if (!vma_is_dax(vma)) {
10771080
/* thp accounting separate from pmd_devmap accounting */
10781081
src_page = pmd_page(pmd);
10791082
VM_BUG_ON_PAGE(!PageHead(src_page), src_page);

0 commit comments

Comments
 (0)