On 3/29/22 18:04, David Hildenbrand wrote: > ... and move the special check for pinned pages into > page_try_dup_anon_rmap() to prepare for tracking exclusive anonymous > pages via a new pageflag, clearing it only after making sure that there > are no GUP pins on the anonymous page. > > We really only care about pins on anonymous pages, because they are > prone to getting replaced in the COW handler once mapped R/O. For !anon > pages in cow-mappings (!VM_SHARED && VM_MAYWRITE) we shouldn't really > care about that, at least not that I could come up with an example. > > Let's drop the is_cow_mapping() check from page_needs_cow_for_dma(), as we > know we're dealing with anonymous pages. Also, drop the handling of > pinned pages from copy_huge_pud() and add a comment if ever supporting > anonymous pages on the PUD level. > > This is a preparation for tracking exclusivity of anonymous pages in > the rmap code, and disallowing marking a page shared (-> failing to > duplicate) if there are GUP pins on a page. > > Signed-off-by: David Hildenbrand <david@xxxxxxxxxx> Acked-by: Vlastimil Babka <vbabka@xxxxxxx> Nit: > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -825,7 +825,8 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, > */ > get_page(page); > rss[mm_counter(page)]++; > - page_dup_rmap(page, false); > + /* Cannot fail as these pages cannot get pinned. */ > + BUG_ON(page_try_dup_anon_rmap(page, false, src_vma)); Should we just call __page_dup_rmap() here? This is block for the condition is_device_private_entry(), and page_try_dup_anon_rmap() can't return -EBUSY for is_device_private_page(). > > /* > * We do not preserve soft-dirty information, because so > @@ -921,18 +922,24 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, > struct page *page; > > page = vm_normal_page(src_vma, addr, pte); > - if (page && unlikely(page_needs_cow_for_dma(src_vma, page))) { > + if (page && PageAnon(page)) { > /* > * If this page may have been pinned by the parent process, > * copy the page immediately for the child so that we'll always > * guarantee the pinned page won't be randomly replaced in the > * future. > */ > - return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, > - addr, rss, prealloc, page); > + get_page(page); > + if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { > + /* Page maybe pinned, we have to copy. */ > + put_page(page); > + return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, > + addr, rss, prealloc, page); > + } > + rss[mm_counter(page)]++; > } else if (page) { > get_page(page); > - page_dup_rmap(page, false); > + page_dup_file_rmap(page, false); > rss[mm_counter(page)]++; > } > > diff --git a/mm/migrate.c b/mm/migrate.c > index 3d60823afd2d..97de2fc17f34 100644 > --- a/mm/migrate.c > +++ b/mm/migrate.c > @@ -234,7 +234,7 @@ static bool remove_migration_pte(struct folio *folio, > if (folio_test_anon(folio)) > hugepage_add_anon_rmap(new, vma, pvmw.address); > else > - page_dup_rmap(new, true); > + page_dup_file_rmap(new, true); > set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte); > } else > #endif