[PATCH v2 27/46] mm: rmap: update try_to_{migrate,unmap} to handle mapcount for HGM

From: James Houghton
Date: Fri Feb 17 2023 - 19:30:59 EST


Make use of the new pvmw->pte_order field to determine the size of the
PTE we're unmapping/migrating.

Signed-off-by: James Houghton <jthoughton@xxxxxxxxxx>

diff --git a/mm/migrate.c b/mm/migrate.c
index 9b4a7e75f6e6..616afcc40fdc 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -247,7 +247,7 @@ static bool remove_migration_pte(struct folio *folio,

#ifdef CONFIG_HUGETLB_PAGE
if (folio_test_hugetlb(folio)) {
- unsigned int shift = huge_page_shift(hstate_vma(vma));
+ unsigned int shift = pvmw.pte_order + PAGE_SHIFT;

pte = arch_make_huge_pte(pte, shift, vma->vm_flags);
if (folio_test_anon(folio))
diff --git a/mm/rmap.c b/mm/rmap.c
index c010d0af3a82..0a019ae32f04 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1609,7 +1609,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
if (PageHWPoison(subpage) && !(flags & TTU_IGNORE_HWPOISON)) {
pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
if (folio_test_hugetlb(folio)) {
- hugetlb_count_sub(folio_nr_pages(folio), mm);
+ hugetlb_count_sub(1UL << pvmw.pte_order, mm);
set_huge_pte_at(mm, address, pvmw.pte, pteval);
} else {
dec_mm_counter(mm, mm_counter(&folio->page));
@@ -1757,7 +1757,13 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
*
* See Documentation/mm/mmu_notifier.rst
*/
- page_remove_rmap(subpage, vma, folio_test_hugetlb(folio));
+ if (folio_test_hugetlb(folio))
+ hugetlb_remove_rmap(subpage,
+ pvmw.pte_order + PAGE_SHIFT,
+ hstate_vma(vma), vma);
+ else
+ page_remove_rmap(subpage, vma, false);
+
if (vma->vm_flags & VM_LOCKED)
mlock_drain_local();
folio_put(folio);
@@ -2020,7 +2026,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
} else if (PageHWPoison(subpage)) {
pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
if (folio_test_hugetlb(folio)) {
- hugetlb_count_sub(folio_nr_pages(folio), mm);
+ hugetlb_count_sub(1L << pvmw.pte_order, mm);
set_huge_pte_at(mm, address, pvmw.pte, pteval);
} else {
dec_mm_counter(mm, mm_counter(&folio->page));
@@ -2112,7 +2118,12 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
*
* See Documentation/mm/mmu_notifier.rst
*/
- page_remove_rmap(subpage, vma, folio_test_hugetlb(folio));
+ if (folio_test_hugetlb(folio))
+ hugetlb_remove_rmap(subpage,
+ pvmw.pte_order + PAGE_SHIFT,
+ hstate_vma(vma), vma);
+ else
+ page_remove_rmap(subpage, vma, false);
if (vma->vm_flags & VM_LOCKED)
mlock_drain_local();
folio_put(folio);
@@ -2196,6 +2207,8 @@ static bool page_make_device_exclusive_one(struct folio *folio,
args->owner);
mmu_notifier_invalidate_range_start(&range);

+ VM_BUG_ON_FOLIO(folio_test_hugetlb(folio), folio);
+
while (page_vma_mapped_walk(&pvmw)) {
/* Unexpected PMD-mapped THP? */
VM_BUG_ON_FOLIO(!pvmw.pte, folio);
--
2.39.2.637.g21b0678d19-goog