Commit f17b5e7b authored by Joel Fernandes (Google)'s avatar Joel Fernandes (Google) Committed by Stephen Rothwell

mm: speed up mremap by 20x on large regions

Android needs to mremap large regions of memory during memory management
related operations.  The mremap system call can be really slow if THP is
not enabled.  The bottleneck is move_page_tables, which is copying each
pte at a time, and can be really slow across a large map.  Turning on THP
may not be a viable option, and is not for us.  This patch speeds up the
performance for non-THP system by copying at the PMD level when possible.

The speedup is an order of magnitude on x86 (~20x).  On a 1GB mremap, the
mremap completion times drops from 3.4-3.6 milliseconds to 144-160

Total mremap time for 1GB data: 3521942 nanoseconds.
Total mremap time for 1GB data: 3449229 nanoseconds.
Total mremap time for 1GB data: 3488230 nanoseconds.

Total mremap time for 1GB data: 150279 nanoseconds.
Total mremap time for 1GB data: 144665 nanoseconds.
Total mremap time for 1GB data: 158708 nanoseconds.

If THP is enabled the optimization is mostly skipped except in certain

Link: default avatarJoel Fernandes (Google) <>
Acked-by: default avatarKirill A. Shutemov <>
Reviewed-by: default avatarWilliam Kucharski <>
Cc: Julia Lawall <>
Cc: Michal Hocko <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarStephen Rothwell <>
parent 5ed8584f
......@@ -535,6 +535,11 @@ config HAVE_IRQ_TIME_ACCOUNTING
Archs need to ensure they use a high enough resolution clock to
support irq time accounting and then call enable_sched_clock_irqtime().
Archs that select this are able to move page tables at the PMD level.
......@@ -191,6 +191,50 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
unsigned long new_addr, unsigned long old_end,
pmd_t *old_pmd, pmd_t *new_pmd)
spinlock_t *old_ptl, *new_ptl;
struct mm_struct *mm = vma->vm_mm;
pmd_t pmd;
if ((old_addr & ~PMD_MASK) || (new_addr & ~PMD_MASK)
|| old_end - old_addr < PMD_SIZE)
return false;
* The destination pmd shouldn't be established, free_pgtables()
* should have release it.
if (WARN_ON(!pmd_none(*new_pmd)))
return false;
* We don't have to worry about the ordering of src and dst
* ptlocks because exclusive mmap_sem prevents deadlock.
old_ptl = pmd_lock(vma->vm_mm, old_pmd);
new_ptl = pmd_lockptr(mm, new_pmd);
if (new_ptl != old_ptl)
spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
/* Clear the pmd */
pmd = *old_pmd;
/* Set the new pmd */
set_pmd_at(mm, new_addr, new_pmd, pmd);
flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
if (new_ptl != old_ptl)
return true;
unsigned long move_page_tables(struct vm_area_struct *vma,
unsigned long old_addr, struct vm_area_struct *new_vma,
unsigned long new_addr, unsigned long len,
......@@ -236,7 +280,25 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
split_huge_pmd(vma, old_pmd, old_addr);
if (pmd_trans_unstable(old_pmd))
} else if (extent == PMD_SIZE) {
* If the extent is PMD-sized, try to speed the move by
* moving at the PMD level if possible.
bool moved;
if (need_rmap_locks)
moved = move_normal_pmd(vma, old_addr, new_addr,
old_end, old_pmd, new_pmd);
if (need_rmap_locks)
if (moved)
if (pte_alloc(new_vma->vm_mm, new_pmd))
next = (new_addr + PMD_SIZE) & PMD_MASK;
Markdown is supported
0% or
Finish editing this message first!
Please register or to comment