From: Josef Bacik migrate_vma_collect_pmd() drops the page table lock to split a large folio which it finds in the middle of a page table, and then starts over at the "again" label. The entries for [start, addr) are in migrate->src[] and migrate->dst[] by then. While the page table lock is dropped the page table can go away, in which case the restarted walk invokes migrate_vma_collect_huge_pmd(). However this is passed start rather than addr, so [start, addr) is collected a second time. This can result in migrate->npages exceeding the number of pages the caller asked for, and therefore in writes past the end of the migrate->src[] and migrate->dst[] arrays. Fix this by only handing over what has not yet been collected, page-by-page, and flush the TLB for those entries which have been previously replaced, similar to the other returns in the function. Fixes: 022a12deda53 ("mm/migrate_device: handle partially mapped folios during collection") Cc: Signed-off-by: Josef Bacik [ ljs: cleanup commit message ] Signed-off-by: Lorenzo Stoakes (ARM) --- mm/migrate_device.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 9e5dab8e1b77..28ed706109da 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -257,13 +257,17 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pte_t *ptep; again: + /* [start, addr) is collected if we get here after splitting a folio */ if (pmd_trans_huge(*pmdp) || !pmd_present(*pmdp)) { - int ret = migrate_vma_collect_huge_pmd(pmdp, start, end, walk, fault_folio); + int ret = migrate_vma_collect_huge_pmd(pmdp, addr, end, walk, fault_folio); if (ret == -EAGAIN) goto again; - if (ret == 0) + if (ret == 0) { + if (unmapped) + flush_tlb_range(walk->vma, start, end); return 0; + } } ptep = pte_offset_map_lock(mm, pmdp, start, &ptl); -- 2.56.0