Skip to contentSkip to conversation

mm: huge pages: Misc fixes for issues found during fuzzing

PATCH Applied Will Deacon 3 participants 7 messages Jun 13, 2017 – Jun 19, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover mm: huge pages: Misc fixes for issues found during fuzzing

Hi all,

This is v2 of the patches previously posted here:

http://www.spinics.net/lists/linux-mm/msg128577.html

Changes since v1 include:

* Use smp_mb() instead of smp_mb__before_atomic() before atomic_set()
* Added acks and fixes tag

Feedback welcome,

Will

--->8

Mark Rutland (1):
mm: numa: avoid waiting on freed migrated pages

Will Deacon (2):
mm/page_ref: Ensure page_ref_unfreeze is ordered against prior
accesses
mm: migrate: Stabilise page count when migrating transparent hugepages

 include/linux/page_ref.h |  1 +
 mm/huge_memory.c         |  8 +++++++-
 mm/migrate.c             | 15 ++-------------
 3 files changed, 10 insertions(+), 14 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

PATCH v2 1/3 mm: numa: avoid waiting on freed migrated pages

From: Mark Rutland <[email protected]>

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);
        spin_unlock(vmf->ptl);
                                        ptl = pmd_lock(mm, pmd);
                                        if (page_count(page) != 2)) {
                                                /* roll back */
                                        }
                                        /* ... */
                                        mlock_migrate_page(new_page, page);
                                        /* ... */
                                        spin_unlock(ptl);
                                        put_page(page);
                                        put_page(page); // page freed here
        wait_on_page_locked(page);
        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 __migration_entry_wait().

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")
---
 mm/huge_memory.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/huge_memory.c +7 -1
index a84909cf20d3..88c6167f194d 100644
@@ -1426,8 +1426,11 @@ int do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd)
 	 */
 	if (unlikely(pmd_trans_migrating(*vmf->pmd))) {
 		page = pmd_page(*vmf->pmd);
+		if (!get_page_unless_zero(page))
+			goto out_unlock;
 		spin_unlock(vmf->ptl);
 		wait_on_page_locked(page);
+		put_page(page);
 		goto out;
 	}
 
@@ -1459,9 +1462,12 @@ int do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd)
 
 	/* 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;
 		spin_unlock(vmf->ptl);
 		wait_on_page_locked(page);
-		page_nid = -1;
+		put_page(page);
 		goto out;
 	}
 
Signature · 1 line
2.1.4
#2 lore raw

PATCH v2 2/3 mm/page_ref: Ensure page_ref_unfreeze is ordered against prior accesses

In reply to Will Deacon

page_ref_freeze and page_ref_unfreeze are designed to be used as a pair, wrapping a critical section where struct pages can be modified without having to worry about consistency for a concurrent fast-GUP.

Whilst page_ref_freeze has full barrier semantics due to its use of atomic_cmpxchg, page_ref_unfreeze is implemented using atomic_set, which doesn't provide any barrier semantics and allows the operation to be reordered with respect to page modifications in the critical section.

This patch ensures that page_ref_unfreeze is ordered after any critical section updates, by invoking smp_mb() prior to the atomic_set.

---
 include/linux/page_ref.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/page_ref.h +1 -0
index 610e13271918..1fd71733aa68 100644
@@ -174,6 +174,7 @@ static inline void page_ref_unfreeze(struct page *page, int count)
 	VM_BUG_ON_PAGE(page_count(page) != 0, page);
 	VM_BUG_ON(count == 0);
 
+	smp_mb();
 	atomic_set(&page->_refcount, count);
 	if (page_ref_tracepoint_active(__tracepoint_page_ref_unfreeze))
 		__page_ref_unfreeze(page, count);
Signature · 7 lines
2.1.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

PATCH v2 3/3 mm: migrate: Stabilise page count when migrating transparent hugepages

In reply to Will Deacon

When migrating a transparent hugepage, migrate_misplaced_transhuge_page guards itself against a concurrent fastgup of the page by checking that the page count is equal to 2 before and after installing the new pmd.

If the page count changes, then the pmd is reverted back to the original entry, however there is a small window where the new (possibly writable) pmd is installed and the underlying page could be written by userspace. Restoring the old pmd could therefore result in loss of data.

This patch fixes the problem by freezing the page count whilst updating the page tables, which protects against a concurrent fastgup without the need to restore the old pmd in the failure case (since the page count can no longer change under our feet).

---
 mm/migrate.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
mm/migrate.c +2 -13
index 89a0a1707f4c..8b21f1b1ec6e 100644
@@ -1913,7 +1913,6 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 	int page_lru = page_is_file_cache(page);
 	unsigned long mmun_start = address & HPAGE_PMD_MASK;
 	unsigned long mmun_end = mmun_start + HPAGE_PMD_SIZE;
-	pmd_t orig_entry;
 
 	/*
 	 * Rate-limit the amount of data that is being migrated to a node.
@@ -1956,8 +1955,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 	/* Recheck the target PMD */
 	mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
 	ptl = pmd_lock(mm, pmd);
-	if (unlikely(!pmd_same(*pmd, entry) || page_count(page) != 2)) {
-fail_putback:
+	if (unlikely(!pmd_same(*pmd, entry) || !page_ref_freeze(page, 2))) {
 		spin_unlock(ptl);
 		mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
 
@@ -1979,7 +1977,6 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 		goto out_unlock;
 	}
 
-	orig_entry = *pmd;
 	entry = mk_huge_pmd(new_page, vma->vm_page_prot);
 	entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
 
@@ -1996,15 +1993,7 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 	set_pmd_at(mm, mmun_start, pmd, entry);
 	update_mmu_cache_pmd(vma, address, &entry);
 
-	if (page_count(page) != 2) {
-		set_pmd_at(mm, mmun_start, pmd, orig_entry);
-		flush_pmd_tlb_range(vma, mmun_start, mmun_end);
-		mmu_notifier_invalidate_range(mm, mmun_start, mmun_end);
-		update_mmu_cache_pmd(vma, address, &entry);
-		page_remove_rmap(new_page, true);
-		goto fail_putback;
-	}
-
+	page_ref_unfreeze(page, 2);
 	mlock_migrate_page(new_page, page);
 	page_remove_rmap(page, true);
 	set_page_owner_migrate_reason(new_page, MR_NUMA_MISPLACED);
Signature · 7 lines
2.1.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v2 2/3
#3 lore raw

Re: [PATCH v2 2/3] mm/page_ref: Ensure page_ref_unfreeze is ordered against prior accesses

In reply to Will Deacon
Quoted 24 lines · Will Deacon

page_ref_freeze and page_ref_unfreeze are designed to be used as a pair, wrapping a critical section where struct pages can be modified without having to worry about consistency for a concurrent fast-GUP.

Whilst page_ref_freeze has full barrier semantics due to its use of atomic_cmpxchg, page_ref_unfreeze is implemented using atomic_set, which doesn't provide any barrier semantics and allows the operation to be reordered with respect to page modifications in the critical section.

This patch ensures that page_ref_unfreeze is ordered after any critical section updates, by invoking smp_mb() prior to the atomic_set.

---
 include/linux/page_ref.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/page_ref.h +0 -0
index 610e13271918..1fd71733aa68 100644

include/linux/page_ref.h static inline void page_ref_unfreeze(struct page *page, int count) · line 177

Quoted 4 lines
include/linux/page_ref.h +1 -0
@@ -174,6 +174,7 @@ static inline void page_ref_unfreeze(struct page *page, int count)
 	VM_BUG_ON_PAGE(page_count(page) != 0, page);
 	VM_BUG_ON(count == 0);
 
+	smp_mb();

Don't we want some comment here?

Otherwise:

Signature · 1 line
 Kirill A. Shutemov
maintainer
#5 lore raw
In reply to Will Deacon
Quoted 15 lines · On Tue, 13 Jun 2017 11:28:39 +0100 Will Deacon <[email protected]> wrote:

This is v2 of the patches previously posted here:

   http://www.spinics.net/lists/linux-mm/msg128577.html

Changes since v1 include:

* Use smp_mb() instead of smp_mb__before_atomic() before atomic_set()
* Added acks and fixes tag

Feedback welcome,

Will

--->8

Quoted 7 lines

Mark Rutland (1):
mm: numa: avoid waiting on freed migrated pages

Will Deacon (2):
mm/page_ref: Ensure page_ref_unfreeze is ordered against prior
accesses
mm: migrate: Stabilise page count when migrating transparent hugepages

I marked [1/3] for -stable backporting and held the other two for 4.13-rc1. Maybe that wasn't appropriate...

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
Quoted 20 lines · Andrew Morton
Quoted 18 lines · On Tue, 13 Jun 2017 11:28:39 +0100 Will Deacon <[email protected]> wrote:

This is v2 of the patches previously posted here:

   http://www.spinics.net/lists/linux-mm/msg128577.html

Changes since v1 include:

* Use smp_mb() instead of smp_mb__before_atomic() before atomic_set()
* Added acks and fixes tag

Feedback welcome,

Will

--->8

Mark Rutland (1):
mm: numa: avoid waiting on freed migrated pages

Quoted 7 lines
Quoted 4 lines

Will Deacon (2):
mm/page_ref: Ensure page_ref_unfreeze is ordered against prior
accesses
mm: migrate: Stabilise page count when migrating transparent hugepages

I marked [1/3] for -stable backporting and held the other two for 4.13-rc1. Maybe that wasn't appropriate...

I think that's about right. Patches 2 and 3 fix issues found by inspection, rather than something we've knowingly run into.

Thanks,

Will