Skip to content

Commit ce83d21

Browse files
aarcange-rhtorvalds
authored andcommitted
thp: allocate memory in khugepaged outside of mmap_sem write mode
This tries to be more friendly to filesystem in userland, with userland backends that allocate memory in the I/O paths and that could deadlock if khugepaged holds the mmap_sem write mode of the userland backend while allocating memory. Memory allocation may wait for writeback I/O completion from the daemon that may be blocked in the mmap_sem read mode if a page fault happens and the daemon wasn't using mlock for the memory required for the I/O submission and completion. Signed-off-by: Andrea Arcangeli <aarcange@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0bbbc0b commit ce83d21

1 file changed

Lines changed: 34 additions & 22 deletions

File tree

‎mm/huge_memory.c‎

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,9 +1664,9 @@ static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
16641664

16651665
static void collapse_huge_page(struct mm_struct *mm,
16661666
unsigned long address,
1667-
struct page **hpage)
1667+
struct page **hpage,
1668+
struct vm_area_struct *vma)
16681669
{
1669-
struct vm_area_struct *vma;
16701670
pgd_t *pgd;
16711671
pud_t *pud;
16721672
pmd_t *pmd, _pmd;
@@ -1680,9 +1680,34 @@ static void collapse_huge_page(struct mm_struct *mm,
16801680
VM_BUG_ON(address & ~HPAGE_PMD_MASK);
16811681
#ifndef CONFIG_NUMA
16821682
VM_BUG_ON(!*hpage);
1683+
new_page = *hpage;
16831684
#else
16841685
VM_BUG_ON(*hpage);
1686+
/*
1687+
* Allocate the page while the vma is still valid and under
1688+
* the mmap_sem read mode so there is no memory allocation
1689+
* later when we take the mmap_sem in write mode. This is more
1690+
* friendly behavior (OTOH it may actually hide bugs) to
1691+
* filesystems in userland with daemons allocating memory in
1692+
* the userland I/O paths. Allocating memory with the
1693+
* mmap_sem in read mode is good idea also to allow greater
1694+
* scalability.
1695+
*/
1696+
new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address);
1697+
if (unlikely(!new_page)) {
1698+
up_read(&mm->mmap_sem);
1699+
*hpage = ERR_PTR(-ENOMEM);
1700+
return;
1701+
}
16851702
#endif
1703+
if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1704+
up_read(&mm->mmap_sem);
1705+
put_page(new_page);
1706+
return;
1707+
}
1708+
1709+
/* after allocating the hugepage upgrade to mmap_sem write mode */
1710+
up_read(&mm->mmap_sem);
16861711

16871712
/*
16881713
* Prevent all access to pagetables with the exception of
@@ -1720,18 +1745,6 @@ static void collapse_huge_page(struct mm_struct *mm,
17201745
if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
17211746
goto out;
17221747

1723-
#ifndef CONFIG_NUMA
1724-
new_page = *hpage;
1725-
#else
1726-
new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address);
1727-
if (unlikely(!new_page)) {
1728-
*hpage = ERR_PTR(-ENOMEM);
1729-
goto out;
1730-
}
1731-
#endif
1732-
if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
1733-
goto out_put_page;
1734-
17351748
anon_vma_lock(vma->anon_vma);
17361749

17371750
pte = pte_offset_map(pmd, address);
@@ -1759,7 +1772,7 @@ static void collapse_huge_page(struct mm_struct *mm,
17591772
spin_unlock(&mm->page_table_lock);
17601773
anon_vma_unlock(vma->anon_vma);
17611774
mem_cgroup_uncharge_page(new_page);
1762-
goto out_put_page;
1775+
goto out;
17631776
}
17641777

17651778
/*
@@ -1798,15 +1811,15 @@ static void collapse_huge_page(struct mm_struct *mm,
17981811
*hpage = NULL;
17991812
#endif
18001813
khugepaged_pages_collapsed++;
1801-
out:
1814+
out_up_write:
18021815
up_write(&mm->mmap_sem);
18031816
return;
18041817

1805-
out_put_page:
1818+
out:
18061819
#ifdef CONFIG_NUMA
18071820
put_page(new_page);
18081821
#endif
1809-
goto out;
1822+
goto out_up_write;
18101823
}
18111824

18121825
static int khugepaged_scan_pmd(struct mm_struct *mm,
@@ -1865,10 +1878,9 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
18651878
ret = 1;
18661879
out_unmap:
18671880
pte_unmap_unlock(pte, ptl);
1868-
if (ret) {
1869-
up_read(&mm->mmap_sem);
1870-
collapse_huge_page(mm, address, hpage);
1871-
}
1881+
if (ret)
1882+
/* collapse_huge_page will return with the mmap_sem released */
1883+
collapse_huge_page(mm, address, hpage, vma);
18721884
out:
18731885
return ret;
18741886
}

0 commit comments

Comments
 (0)