Skip to contentSkip to conversation

mm/migrate_device: fix cache flush when replacing huge zero PMD

PATCH Applied Hui Su 5 participants 9 messages Aug 17

Subsystems (2)
lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH 1/1 mm/migrate_device: fix cache flush when replacing huge zero PMD

migrate_vma_insert_huge_pmd_page() calls flush_cache_page() before replacing an existing huge zero PMD. However, the third argument to flush_cache_page() is a PFN, while addr + HPAGE_PMD_SIZE is an end virtual address.

More importantly, the mapping being invalidated is PMD-sized rather than PAGE_SIZE-sized. Flush the whole PMD range with flush_cache_range(), matching other huge PMD invalidation paths.

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/migrate_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/migrate_device.c +1 -1
index 908d2d4ec43a..098c04c1b124 100644
@@ -872,7 +872,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 
 	if (flush) {
 		pte_free(vma->vm_mm, pgtable);
-		flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
+		flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE);
 		pmdp_invalidate(vma, addr, pmdp);
 	} else {
 		pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Hui Su

migrate_vma_insert_huge_pmd_page() calls flush_cache_page() before replacing an existing huge zero PMD. However, the third argument to flush_cache_page() is a PFN, while addr + HPAGE_PMD_SIZE is an end virtual address.

More importantly, the mapping being invalidated is PMD-sized rather than PAGE_SIZE-sized. Flush the whole PMD range with flush_cache_range(), matching other huge PMD invalidation paths.

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/migrate_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/migrate_device.c +0 -0
index 908d2d4ec43a..098c04c1b124 100644

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 878

Quoted 8 lines
mm/migrate_device.c +1 -1
@@ -872,7 +872,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 
 	if (flush) {
 		pte_free(vma->vm_mm, pgtable);
-		flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
+		flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE);
 		pmdp_invalidate(vma, addr, pmdp);
 	} else {
 		pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
maintainer
#2 lore raw
Quoted 22 lines · On Mon, 17 Aug 2026 17:35:53 +1000 Balbir Singh <[email protected]> wrote:
Quoted 21 lines · Hui Su

migrate_vma_insert_huge_pmd_page() calls flush_cache_page() before replacing an existing huge zero PMD. However, the third argument to flush_cache_page() is a PFN, while addr + HPAGE_PMD_SIZE is an end virtual address.

More importantly, the mapping being invalidated is PMD-sized rather than PAGE_SIZE-sized. Flush the whole PMD range with flush_cache_range(), matching other huge PMD invalidation paths.

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/migrate_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/migrate_device.c +0 -0
index 908d2d4ec43a..098c04c1b124 100644
@@ -872,7 +872,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 
Quoted 9 lines

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 878

Quoted 7 lines
mm/migrate_device.c +1 -1
 	if (flush) {
 		pte_free(vma->vm_mm, pgtable);
-		flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
+		flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE);
 		pmdp_invalidate(vma, addr, pmdp);
 	} else {
 		pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);

doh. It's a shame this actually compiled...

Can we add some speculation about the userspace-visible effects of the bug?

I'm assuming we should backport the fix?

author
#3 lore raw
Quoted 34 lines · On Mon, 17 Aug 2026 17:35:53 +1000 Balbir Singh <[email protected]> wrote:
Quoted 23 lines · Hui Su
Quoted 21 lines

migrate_vma_insert_huge_pmd_page() calls flush_cache_page() before replacing an existing huge zero PMD. However, the third argument to flush_cache_page() is a PFN, while addr + HPAGE_PMD_SIZE is an end virtual address.

More importantly, the mapping being invalidated is PMD-sized rather than PAGE_SIZE-sized. Flush the whole PMD range with flush_cache_range(), matching other huge PMD invalidation paths.

Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/migrate_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/migrate_device.c +0 -0
index 908d2d4ec43a..098c04c1b124 100644
@@ -872,7 +872,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 
Quoted 9 lines

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 878

Quoted 7 lines
mm/migrate_device.c +1 -1
 	if (flush) {
 		pte_free(vma->vm_mm, pgtable);
-		flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
+		flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE);
 		pmdp_invalidate(vma, addr, pmdp);
 	} else {
 		pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
Quoted 6 lines

doh. It's a shame this actually compiled...

Can we add some speculation about the userspace-visible effects of the bug?

I'm assuming we should backport the fix?

Hi,

I took a closer look at this, there is no userspace-visible effect today.

The architectures that currently enable ARCH_ENABLE_THP_MIGRATION use no-op implementations of flush_cache_page()/flush_cache_range(). 32-bit ARM has non-trivial implementations, but does not enable ARCH_ENABLE_THP_MIGRATION.

So this appears to be a latent API misuse rather than a currently observable bug, and I don't think a stable backport is necessary.

Should I resend a v2 clarifying the userspace-visible effect in the changelog?

Thanks,
Hui

maintainer
#4 lore raw
Quoted 25 lines · On Tue, 18 Aug 2026 00:36:46 +0800 Hui Su <[email protected]> wrote:
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
mm/migrate_device.c +0 -0
@@ -872,7 +872,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 
Quoted 9 lines

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 878

Quoted 7 lines
mm/migrate_device.c +1 -1
 	if (flush) {
 		pte_free(vma->vm_mm, pgtable);
-		flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
+		flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE);
 		pmdp_invalidate(vma, addr, pmdp);
 	} else {
 		pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
Quoted 6 lines

doh. It's a shame this actually compiled...

Can we add some speculation about the userspace-visible effects of the bug?

I'm assuming we should backport the fix?

Hi,

I took a closer look at this, there is no userspace-visible effect today.

Quoted 7 lines

The architectures that currently enable ARCH_ENABLE_THP_MIGRATION use no-op implementations of flush_cache_page()/flush_cache_range(). 32-bit ARM has non-trivial implementations, but does not enable ARCH_ENABLE_THP_MIGRATION.

So this appears to be a latent API misuse rather than a currently observable bug, and I don't think a stable backport is necessary.

OK, thanks for checking.

Quoted 2 lines

Should I resend a v2 clarifying the userspace-visible effect in the changelog?

Yes please, after 7.3-rc1.

#5 lore raw
Quoted 35 lines · Andrew Morton
Quoted 25 lines · On Tue, 18 Aug 2026 00:36:46 +0800 Hui Su <[email protected]> wrote:
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
mm/migrate_device.c +0 -0
@@ -872,7 +872,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 
Quoted 9 lines

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 878

Quoted 7 lines
mm/migrate_device.c +1 -1
 	if (flush) {
 		pte_free(vma->vm_mm, pgtable);
-		flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
+		flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE);
 		pmdp_invalidate(vma, addr, pmdp);
 	} else {
 		pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
Quoted 6 lines

doh. It's a shame this actually compiled...

Can we add some speculation about the userspace-visible effects of the bug?

I'm assuming we should backport the fix?

Hi,

I took a closer look at this, there is no userspace-visible effect today.

Quoted 7 lines

The architectures that currently enable ARCH_ENABLE_THP_MIGRATION use no-op implementations of flush_cache_page()/flush_cache_range(). 32-bit ARM has non-trivial implementations, but does not enable ARCH_ENABLE_THP_MIGRATION.

So this appears to be a latent API misuse rather than a currently observable bug, and I don't think a stable backport is necessary.

Quoted 6 lines

OK, thanks for checking.

Quoted 2 lines

Should I resend a v2 clarifying the userspace-visible effect in the changelog?

Yes please, after 7.3-rc1.

I have been running some tests at my end, I have some new ones, nothing so far exposes this. Thanks for checking Hui!

Balbir

maintainer
#6 lore raw
In reply to Andrew Morton
Quoted 12 lines · On Mon, 17 Aug 2026 14:34:15 -0700 Andrew Morton <[email protected]>…
Quoted 11 lines

Hi,

I took a closer look at this, there is no userspace-visible effect today.

The architectures that currently enable ARCH_ENABLE_THP_MIGRATION use no-op implementations of flush_cache_page()/flush_cache_range(). 32-bit ARM has non-trivial implementations, but does not enable ARCH_ENABLE_THP_MIGRATION.

So this appears to be a latent API misuse rather than a currently observable bug, and I don't think a stable backport is necessary.

Quoted 6 lines

OK, thanks for checking.

Quoted 2 lines

Should I resend a v2 clarifying the userspace-visible effect in the changelog?

Yes please, after 7.3-rc1.

Actually, I updated the changelog and added it to next week's queue-for-Linus.

reviewer Reviewed-by on v1 1/1
#7 lore raw
In reply to Hui Su
Quoted 9 lines · Hui Su

migrate_vma_insert_huge_pmd_page() calls flush_cache_page() before replacing an existing huge zero PMD. However, the third argument to flush_cache_page() is a PFN, while addr + HPAGE_PMD_SIZE is an end virtual address.

More importantly, the mapping being invalidated is PMD-sized rather than PAGE_SIZE-sized. Flush the whole PMD range with flush_cache_range(), matching other huge PMD invalidation paths.

Quoted 6 lines
Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/migrate_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Thank you for fixing it.

Signature · 2 lines
Best Regards,
Yan, Zi
maintainer Acked-by
#8 lore raw
Quoted 17 lines · Zi Yan
Quoted 9 lines · Hui Su

migrate_vma_insert_huge_pmd_page() calls flush_cache_page() before replacing an existing huge zero PMD. However, the third argument to flush_cache_page() is a PFN, while addr + HPAGE_PMD_SIZE is an end virtual address.

More importantly, the mapping being invalidated is PMD-sized rather than PAGE_SIZE-sized. Flush the whole PMD range with flush_cache_range(), matching other huge PMD invalidation paths.

Quoted 6 lines
Fixes a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
---
 mm/migrate_device.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 3 lines

Thank you for fixing it.

I do wonder whether there should be a PMD helper.

Signature · 3 lines
Cheers,

David