[PATCH 3.16 200/233] mm: numa: avoid waiting on freed migrated pages

From: Ben Hutchings
Date: Sat Sep 09 2017 - 19:31:50 EST

3.16.48-rc1 review patch. If anyone has any objections, please let me know.


From: Mark Rutland <mark.rutland@xxxxxxx>

commit 3c226c637b69104f6b9f1c6ec5b08d7b741b3229 upstream.

In do_huge_pmd_numa_page(), we attempt to handle a migrating thp pmd by
waiting until the pmd is unlocked before we return and retry. However,
we can race with migrate_misplaced_transhuge_page():

// do_huge_pmd_numa_page // migrate_misplaced_transhuge_page()
// Holds 0 refs on page // Holds 2 refs on page

vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
/* ... */
if (pmd_trans_migrating(*vmf->pmd)) {
page = pmd_page(*vmf->pmd);
ptl = pmd_lock(mm, pmd);
if (page_count(page) != 2)) {
/* roll back */
/* ... */
mlock_migrate_page(new_page, page);
/* ... */
put_page(page); // page freed here
goto out;

This can result in the freed page having its waiters flag set
unexpectedly, which trips the PAGE_FLAGS_CHECK_AT_PREP checks in the
page alloc/free functions. This has been observed on arm64 KVM guests.

We can avoid this by having do_huge_pmd_numa_page() take a reference on
the page before dropping the pmd lock, mirroring what we do in

When we hit the race, migrate_misplaced_transhuge_page() will see the
reference and abort the migration, as it may do today in other cases.

Fixes: b8916634b77bffb2 ("mm: Prevent parallel splits during THP migration")
Link: http://lkml.kernel.org/r/1497349722-6731-2-git-send-email-will.deacon@xxxxxxx
Signed-off-by: Mark Rutland <mark.rutland@xxxxxxx>
Signed-off-by: Will Deacon <will.deacon@xxxxxxx>
Acked-by: Steve Capper <steve.capper@xxxxxxx>
Acked-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
Acked-by: Vlastimil Babka <vbabka@xxxxxxx>
Cc: Mel Gorman <mgorman@xxxxxxx>
Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Signed-off-by: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
[bwh: Backported to 3.16: adjust context]
Signed-off-by: Ben Hutchings <ben@xxxxxxxxxxxxxxx>
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1284,8 +1284,12 @@ int do_huge_pmd_numa_page(struct mm_stru
* check_same as the page may no longer be mapped.
if (unlikely(pmd_trans_migrating(*pmdp))) {
+ page = pmd_page(pmd);
+ if (!get_page_unless_zero(page))
+ goto out_unlock;
wait_migrate_huge_page(vma->anon_vma, pmdp);
+ put_page(page);
goto out;

@@ -1321,9 +1325,12 @@ int do_huge_pmd_numa_page(struct mm_stru

/* Migration could have started since the pmd_trans_migrating check */
if (!page_locked) {
+ page_nid = -1;
+ if (!get_page_unless_zero(page))
+ goto out_unlock;
- page_nid = -1;
+ put_page(page);
goto out;