Fix some cache flush bugs
Subsystems (7)
v5 cover Fix some cache flush bugs
This series focus on fixing cache maintenance.
v5:
- Collect Reviewed-by from Mike.
- Fix mcopy_atomic_pte() and __mcopy_atomic() (Mike).
- Fix shmem_mfill_atomic_pte().
v4:
- Replace folio_copy() with copy_user_huge_page().
- Update commit message for patch 2.
v3:
- Collect Reviewed-by tag from Zi Yan.
- Fix hugetlb cache maintenance.
v2:
- Collect Reviewed-by tag from Zi Yan.
- Using a for loop instead of the folio variant for backportability.
Muchun Song (7):
mm: thp: fix wrong cache flush in remove_migration_pmd()
mm: fix missing cache flush for all tail pages of compound page
mm: hugetlb: fix missing cache flush in copy_huge_page_from_user()
mm: hugetlb: fix missing cache flush in hugetlb_mcopy_atomic_pte()
mm: shmem: fix missing cache flush in shmem_mfill_atomic_pte()
mm: userfaultfd: fix missing cache flush in mcopy_atomic_pte() and
__mcopy_atomic()
mm: replace multiple dcache flush with flush_dcache_folio()
mm/huge_memory.c | 3 ++- mm/hugetlb.c | 3 ++- mm/memory.c | 2 ++ mm/migrate.c | 3 +-- mm/shmem.c | 4 +++- mm/userfaultfd.c | 3 +++ 6 files changed, 13 insertions(+), 5 deletions(-)
Signature · 1 line
2.11.0
PATCH v5 1/7 mm: thp: fix wrong cache flush in remove_migration_pmd()
The flush_cache_range() is supposed to be justified only if the page is already placed in process page table, and that is done right after flush_cache_range(). So using this interface is wrong. And there is no need to invalite cache since it was non-present before in remove_migration_pmd(). So just to remove it.
--- mm/huge_memory.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/huge_memory.c +2 -1
@@ -3207,7 +3207,6 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)if (pmd_swp_uffd_wp (*pvmw->pmd)) pmde =pmd_wrprotect (pmd_mkuffd_wp (pmde)); -flush_cache_range (vma, mmun_start, mmun_start +HPAGE_PMD_SIZE );if (PageAnon (new))page_add_anon_rmap (new, vma, mmun_start, true);else
@@ -3215,6 +3214,8 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)set_pmd_at (mm, mmun_start, pvmw->pmd, pmde);if ((vma->vm_flags &VM_LOCKED ) && !PageDoubleMap (new))mlock_vma_page (new); + +/* No need to invalidate - it was non-present before */ update_mmu_cache_pmd(vma, address, pvmw->pmd); }#endif
Signature · 1 line
2.11.0
PATCH v5 2/7 mm: fix missing cache flush for all tail pages of compound page
In reply to Muchun SongThe D-cache maintenance inside move_to_new_page() only consider one page, there is still D-cache maintenance issue for tail pages of compound page (e.g. THP or HugeTLB). THP migration is only enabled on x86_64, ARM64 and powerpc, while powerpc and arm64 need to maintain the consistency between I-Cache and D-Cache, which depends on flush_dcache_page() to maintain the consistency between I-Cache and D-Cache. But there is no issues on arm64 and powerpc since they already considers the compound page cache flushing in their icache flush function. HugeTLB migration is enabled on arm, arm64, mips, parisc, powerpc, riscv, s390 and sh, while arm has handled the compound page cache flush in flush_dcache_page(), but most others do not. In theory, the issue exists on many architectures. Fix this by not using flush_dcache_folio() since it is not backportable.
--- mm/migrate.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
mm/migrate.c +5 -2
@@ -933,9 +933,12 @@ static int move_to_new_page(struct page *newpage, struct page *page,if (!PageMappingFlags (page)) page->mapping =NULL ; -if (likely (!is_zone_device_page (newpage))) -flush_dcache_page (newpage); +if (likely (!is_zone_device_page (newpage))) { +int i, nr =compound_nr (newpage); +for (i =0 ; i < nr; i++) +flush_dcache_page (newpage + i); + } } out:return rc;
Signature · 1 line
2.11.0
PATCH v5 3/7 mm: hugetlb: fix missing cache flush in copy_huge_page_from_user()
In reply to Muchun SongThe userfaultfd calls copy_huge_page_from_user() which does not do any cache flushing for the target page. Then the target page will be mapped to the user space with a different address (user address), which might have an alias issue with the kernel address used to copy the data from the user to. Fix this issue by flushing dcache in copy_huge_page_from_user().
--- mm/memory.c | 2 ++ 1 file changed, 2 insertions(+)
mm/memory.c +2 -0
@@ -5405,6 +5405,8 @@ long copy_huge_page_from_user(struct page *dst_page,if (rc)break ; +flush_dcache_page (subpage); +cond_resched (); }return ret_val;
Signature · 1 line
2.11.0
PATCH v5 4/7 mm: hugetlb: fix missing cache flush in hugetlb_mcopy_atomic_pte()
In reply to Muchun Songfolio_copy() will copy the data from one page to the target page, then the target page will be mapped to the user space address, which might have an alias issue with the kernel address used to copy the data from the page to. There are 2 ways to fix this issue.
1) insert flush_dcache_page() after folio_copy().
2) replace folio_copy() with copy_user_huge_page() which already considers the cache maintenance.
We chose 2) way to fix the issue since architectures can optimize this situation. It is also make backports easier.
--- mm/hugetlb.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/hugetlb.c +2 -1
@@ -5818,7 +5818,8 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm, *pagep =NULL ; goto out; } -folio_copy (page_folio (page),page_folio (*pagep)); +copy_user_huge_page (page, *pagep, dst_addr, dst_vma, +pages_per_huge_page (h));put_page (*pagep); *pagep =NULL ; }
Signature · 1 line
2.11.0
PATCH v5 5/7 mm: shmem: fix missing cache flush in shmem_mfill_atomic_pte()
In reply to Muchun SongThe userfaultfd calls shmem_mfill_atomic_pte() which does not do any cache flushing for the target page. Then the target page will be mapped to the user space with a different address (user address), which might have an alias issue with the kernel address used to copy the data from the user to. Insert flush_dcache_page() in non-zero-page case. And replace clear_highpage() with clear_user_highpage() which already considers the cache maintenance.
--- mm/shmem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/shmem.c +3 -1
@@ -2371,8 +2371,10 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,/* don't free the page */ goto out_unacct_blocks; } + +flush_dcache_page (page); }else {/* ZEROPAGE */ -clear_highpage (page); +clear_user_highpage (page, dst_addr); } }else { page = *pagep;
Signature · 1 line
2.11.0
Re: [PATCH v5 5/7] mm: shmem: fix missing cache flush in shmem_mfill_atomic_pte()
Quoted 8 lines · Muchun Song
The userfaultfd calls
shmem_mfill_atomic_pte()which does not do any cache flushing for the target page. Then the target page will be mapped to the user space with a different address (user address), which might have an alias issue with the kernel address used to copy the data from the user to. Insertflush_dcache_page()in non-zero-page case. And replaceclear_highpage()withclear_user_highpage()which already considers the cache maintenance.
Quoted 6 lines
Fixes 8d1039634206 ("userfaultfd: shmem: add shmem_mfill_zeropage_pte for userfaultfd support")Fixes 4c27fe4c4c84 ("userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support")Signed-off-by Muchun Song <[email protected]>--- mm/shmem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Thanks,
It might have been better to combine this and the next patch. When looking at this, I noted the 'fallback to copy_from_user outside mmap_lock' case needs to be addressed as well. It is in the next patch. No need to change.
Signature · 1 line
Mike Kravetz
Quoted 17 lines
mm/shmem.c +3
-1@@ -2371,8 +2371,10 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,/* don't free the page */ goto out_unacct_blocks; } + +flush_dcache_page (page); }else {/* ZEROPAGE */ -clear_highpage (page); +clear_user_highpage (page, dst_addr); } }else { page = *pagep;
Signature · 1 line
Mike Kravetz
Re: [PATCH v5 5/7] mm: shmem: fix missing cache flush in shmem_mfill_atomic_pte()
Quoted 17 lines · Mike Kravetz
Quoted 8 lines · Muchun Song
The userfaultfd calls
shmem_mfill_atomic_pte()which does not do any cache flushing for the target page. Then the target page will be mapped to the user space with a different address (user address), which might have an alias issue with the kernel address used to copy the data from the user to. Insertflush_dcache_page()in non-zero-page case. And replaceclear_highpage()withclear_user_highpage()which already considers the cache maintenance.Quoted 6 lines
Fixes 8d1039634206 ("userfaultfd: shmem: add shmem_mfill_zeropage_pte for userfaultfd support")Fixes 4c27fe4c4c84 ("userfaultfd: shmem: add shmem_mcopy_atomic_pte for userfaultfd support")Signed-off-by Muchun Song <[email protected]>--- mm/shmem.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Quoted 5 lines
Thanks,
It might have been better to combine this and the next patch. When looking at this, I noted the 'fallback to
copy_from_useroutsidemmap_lock' case needs to be addressed as well. It is in the next patch. No need to change.
I separate those changes into 2 patches since the fixed patch is different. This patch is fixing linux 4.13 and later, while next patch is fixing linux 4.2 and later. Maybe it is hard to backport if combining those two patches.
Quoted 2 lines
Reviewed-by Mike Kravetz <[email protected]>
Thanks Mike.
PATCH v5 6/7 mm: userfaultfd: fix missing cache flush in mcopy_atomic_pte() and __mcopy_atomic()
In reply to Muchun SongThe userfaultfd calls mcopy_atomic_pte() and __mcopy_atomic() which do not do any cache flushing for the target page. Then the target page will be mapped to the user space with a different address (user address), which might have an alias issue with the kernel address used to copy the data from the user to. Fix this by insert flush_dcache_page() after copy_from_user() succeeds.
--- mm/userfaultfd.c | 3 +++ 1 file changed, 3 insertions(+)
mm/userfaultfd.c +3 -0
@@ -150,6 +150,8 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm,/* don't free the page */ goto out; } + +flush_dcache_page (page); }else { page = *pagep; *pagep =NULL ;
@@ -625,6 +627,7 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, err = -EFAULT ; goto out; } +flush_dcache_page (page); goto retry; }else BUG_ON (page);
Signature · 1 line
2.11.0
Re: [PATCH v5 6/7] mm: userfaultfd: fix missing cache flush in mcopy_atomic_pte() and __mcopy_atomic()
Quoted 7 lines · Muchun Song
The userfaultfd calls
mcopy_atomic_pte()and__mcopy_atomic()which do not do any cache flushing for the target page. Then the target page will be mapped to the user space with a different address (user address), which might have an alias issue with the kernel address used to copy the data from the user to. Fix this by insertflush_dcache_page()aftercopy_from_user()succeeds.
Quoted 6 lines
Fixes b6ebaedb4cb1 ("userfaultfd: avoid mmap_sem read recursion in mcopy_atomic")Fixes c1a4de99fada ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation")Signed-off-by Muchun Song <[email protected]>--- mm/userfaultfd.c | 3 +++ 1 file changed, 3 insertions(+)
Signature · 1 line
Mike Kravetz
Quoted 22 lines
mm/userfaultfd.c +3
-0@@ -150,6 +150,8 @@ static int mcopy_atomic_pte(struct mm_struct *dst_mm,/* don't free the page */ goto out; } + +flush_dcache_page (page); }else { page = *pagep; *pagep =NULL ;@@ -625,6 +627,7 @@ static __always_inline ssize_t __mcopy_atomic(struct mm_struct *dst_mm, err = -EFAULT ; goto out; } +flush_dcache_page (page); goto retry; }else BUG_ON (page);
PATCH v5 7/7 mm: replace multiple dcache flush with flush_dcache_folio()
In reply to Muchun SongSimplify the code by using flush_dcache_folio().
--- mm/migrate.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
mm/migrate.c +2 -6
@@ -933,12 +933,8 @@ static int move_to_new_page(struct page *newpage, struct page *page,if (!PageMappingFlags (page)) page->mapping =NULL ; -if (likely (!is_zone_device_page (newpage))) { -int i, nr =compound_nr (newpage); - -for (i =0 ; i < nr; i++) -flush_dcache_page (newpage + i); - } +if (likely (!is_zone_device_page (newpage))) +flush_dcache_folio (page_folio (newpage)); } out:return rc;
Signature · 1 line
2.11.0