diff mbox series

[v2,20/46] mm/migrate: Add folio_migrate_copy()

Message ID 20210622121551.3398730-21-willy@infradead.org (mailing list archive)
State New
Headers show
Series Folio-enabling the page cache | expand

Commit Message

Matthew Wilcox June 22, 2021, 12:15 p.m. UTC
Combine the THP, hugetlb and base page routines together into a simple
loop.  It does iterate the pages backwards, but real CPUs can prefetch
a backwards walk.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 include/linux/migrate.h |  1 +
 mm/folio-compat.c       |  6 ++++
 mm/migrate.c            | 68 ++++++++---------------------------------
 3 files changed, 19 insertions(+), 56 deletions(-)

Comments

Christoph Hellwig June 23, 2021, 8:35 a.m. UTC | #1
> +void folio_migrate_copy(struct folio *newfolio, struct folio *folio)
>  {
> +	unsigned int i = folio_nr_pages(folio) - 1;
>  
> +	copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
> +	while (i-- > 0) {
> +		cond_resched()a
> +		/* folio_page() handles discontinuities in memmap */
> +		copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
> +	}
> +

What is the advantage of copying backwards here to start with?
Matthew Wilcox June 24, 2021, 6:02 p.m. UTC | #2
On Wed, Jun 23, 2021 at 10:35:00AM +0200, Christoph Hellwig wrote:
> > +void folio_migrate_copy(struct folio *newfolio, struct folio *folio)
> >  {
> > +	unsigned int i = folio_nr_pages(folio) - 1;
> >  
> > +	copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
> > +	while (i-- > 0) {
> > +		cond_resched()a
> > +		/* folio_page() handles discontinuities in memmap */
> > +		copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
> > +	}
> > +
> 
> What is the advantage of copying backwards here to start with?

Easier to write the loop this way?  I suppose we could do it as ...

	unsigned int i, nr = folio_nr_pages(folio);

	for (i = 0; i < nr; i++) {
		/* folio_page() handles discontinuities in memmap */
		copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
		cond_resched();
	}

I'm not really bothered.  As long as we don't call folio_nr_pages() for
each iteration of the loop ... I've actually been wondering about
marking that as __pure, but I don't quite have the nerve to do it yet.
Christoph Hellwig June 28, 2021, 6:26 a.m. UTC | #3
On Thu, Jun 24, 2021 at 07:02:23PM +0100, Matthew Wilcox wrote:
> > What is the advantage of copying backwards here to start with?
> 
> Easier to write the loop this way?  I suppose we could do it as ...
> 
> 	unsigned int i, nr = folio_nr_pages(folio);
> 
> 	for (i = 0; i < nr; i++) {
> 		/* folio_page() handles discontinuities in memmap */
> 		copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
> 		cond_resched();
> 	}

I'd prefer that if there is no obvious downside.
diff mbox series

Patch

diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index 7993faffa46d..cb67692e659a 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -52,6 +52,7 @@  extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 extern int migrate_page_move_mapping(struct address_space *mapping,
 		struct page *newpage, struct page *page, int extra_count);
 void folio_migrate_flags(struct folio *newfolio, struct folio *folio);
+void folio_migrate_copy(struct folio *newfolio, struct folio *folio);
 int folio_migrate_mapping(struct address_space *mapping,
 		struct folio *newfolio, struct folio *folio, int extra_count);
 #else
diff --git a/mm/folio-compat.c b/mm/folio-compat.c
index f0ac904d396f..316c912e49e0 100644
--- a/mm/folio-compat.c
+++ b/mm/folio-compat.c
@@ -76,4 +76,10 @@  void migrate_page_states(struct page *newpage, struct page *page)
 	folio_migrate_flags(page_folio(newpage), page_folio(page));
 }
 EXPORT_SYMBOL(migrate_page_states);
+
+void migrate_page_copy(struct page *newpage, struct page *page)
+{
+	folio_migrate_copy(page_folio(newpage), page_folio(page));
+}
+EXPORT_SYMBOL(migrate_page_copy);
 #endif
diff --git a/mm/migrate.c b/mm/migrate.c
index 8b289156f4c6..bc1362f367ae 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -529,54 +529,6 @@  int migrate_huge_page_move_mapping(struct address_space *mapping,
 	return MIGRATEPAGE_SUCCESS;
 }
 
-/*
- * Gigantic pages are so large that we do not guarantee that page++ pointer
- * arithmetic will work across the entire page.  We need something more
- * specialized.
- */
-static void __copy_gigantic_page(struct page *dst, struct page *src,
-				int nr_pages)
-{
-	int i;
-	struct page *dst_base = dst;
-	struct page *src_base = src;
-
-	for (i = 0; i < nr_pages; ) {
-		cond_resched();
-		copy_highpage(dst, src);
-
-		i++;
-		dst = mem_map_next(dst, dst_base, i);
-		src = mem_map_next(src, src_base, i);
-	}
-}
-
-static void copy_huge_page(struct page *dst, struct page *src)
-{
-	int i;
-	int nr_pages;
-
-	if (PageHuge(src)) {
-		/* hugetlbfs page */
-		struct hstate *h = page_hstate(src);
-		nr_pages = pages_per_huge_page(h);
-
-		if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
-			__copy_gigantic_page(dst, src, nr_pages);
-			return;
-		}
-	} else {
-		/* thp page */
-		BUG_ON(!PageTransHuge(src));
-		nr_pages = thp_nr_pages(src);
-	}
-
-	for (i = 0; i < nr_pages; i++) {
-		cond_resched();
-		copy_highpage(dst + i, src + i);
-	}
-}
-
 /*
  * Copy the flags and some other ancillary information
  */
@@ -653,16 +605,20 @@  void folio_migrate_flags(struct folio *newfolio, struct folio *folio)
 }
 EXPORT_SYMBOL(folio_migrate_flags);
 
-void migrate_page_copy(struct page *newpage, struct page *page)
+void folio_migrate_copy(struct folio *newfolio, struct folio *folio)
 {
-	if (PageHuge(page) || PageTransHuge(page))
-		copy_huge_page(newpage, page);
-	else
-		copy_highpage(newpage, page);
+	unsigned int i = folio_nr_pages(folio) - 1;
 
-	migrate_page_states(newpage, page);
+	copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
+	while (i-- > 0) {
+		cond_resched();
+		/* folio_page() handles discontinuities in memmap */
+		copy_highpage(folio_page(newfolio, i), folio_page(folio, i));
+	}
+
+	folio_migrate_flags(newfolio, folio);
 }
-EXPORT_SYMBOL(migrate_page_copy);
+EXPORT_SYMBOL(folio_migrate_copy);
 
 /************************************************************
  *                    Migration functions
@@ -690,7 +646,7 @@  int migrate_page(struct address_space *mapping,
 		return rc;
 
 	if (mode != MIGRATE_SYNC_NO_COPY)
-		migrate_page_copy(newpage, page);
+		folio_migrate_copy(newfolio, folio);
 	else
 		folio_migrate_flags(newfolio, folio);
 	return MIGRATEPAGE_SUCCESS;