diff mbox series

mm: Make copy_huge_page() always available

Message ID 20210712153207.39302-1-willy@infradead.org (mailing list archive)
State New
Headers show
Series mm: Make copy_huge_page() always available | expand

Commit Message

Matthew Wilcox July 12, 2021, 3:32 p.m. UTC
Rewrite copy_huge_page() and move it into mm/util.c so it's always
available.  Fixes an exposure of uninitialised memory on configurations
with HUGETLB and UFFD enabled and MIGRATION disabled.

Fixes: 8cc5fcbb5be8 ("mm, hugetlb: fix racy resv_huge_pages underflow on UFFDIO_COPY")
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 include/linux/migrate.h |  5 -----
 include/linux/mm.h      |  1 +
 mm/migrate.c            | 48 -----------------------------------------
 mm/util.c               | 10 +++++++++
 4 files changed, 11 insertions(+), 53 deletions(-)

Comments

Mike Kravetz July 12, 2021, 5:52 p.m. UTC | #1
On 7/12/21 8:32 AM, Matthew Wilcox (Oracle) wrote:
> Rewrite copy_huge_page() and move it into mm/util.c so it's always
> available.  Fixes an exposure of uninitialised memory on configurations
> with HUGETLB and UFFD enabled and MIGRATION disabled.
> 
> Fixes: 8cc5fcbb5be8 ("mm, hugetlb: fix racy resv_huge_pages underflow on UFFDIO_COPY")
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  include/linux/migrate.h |  5 -----
>  include/linux/mm.h      |  1 +
>  mm/migrate.c            | 48 -----------------------------------------
>  mm/util.c               | 10 +++++++++
>  4 files changed, 11 insertions(+), 53 deletions(-)

Thanks Matthew,

Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
Mina Almasry July 12, 2021, 7:32 p.m. UTC | #2
On Mon, Jul 12, 2021 at 10:52 AM Mike Kravetz <mike.kravetz@oracle.com> wrote:
>
> On 7/12/21 8:32 AM, Matthew Wilcox (Oracle) wrote:
> > Rewrite copy_huge_page() and move it into mm/util.c so it's always
> > available.  Fixes an exposure of uninitialised memory on configurations
> > with HUGETLB and UFFD enabled and MIGRATION disabled.
> >
> > Fixes: 8cc5fcbb5be8 ("mm, hugetlb: fix racy resv_huge_pages underflow on UFFDIO_COPY")
> > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>

Reviewed-By: Mina Almasry <almasrymina@google.com>

> > ---
> >  include/linux/migrate.h |  5 -----
> >  include/linux/mm.h      |  1 +
> >  mm/migrate.c            | 48 -----------------------------------------
> >  mm/util.c               | 10 +++++++++
> >  4 files changed, 11 insertions(+), 53 deletions(-)
>
> Thanks Matthew,
>
> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>
> --
> Mike Kravetz
Andrew Morton July 14, 2021, 2:12 a.m. UTC | #3
On Mon, 12 Jul 2021 16:32:07 +0100 "Matthew Wilcox (Oracle)" <willy@infradead.org> wrote:

> Rewrite copy_huge_page() and move it into mm/util.c so it's always
> available.  Fixes an exposure of uninitialised memory on configurations
> with HUGETLB and UFFD enabled and MIGRATION disabled.

Wait.  Exposing uninitialized memory is serious.  Can we please include
full info on this flaw and decide whether a -stable backport is justified? 
If not, why not, etc?
Matthew Wilcox July 14, 2021, 2:20 a.m. UTC | #4
On Tue, Jul 13, 2021 at 07:12:44PM -0700, Andrew Morton wrote:
> On Mon, 12 Jul 2021 16:32:07 +0100 "Matthew Wilcox (Oracle)" <willy@infradead.org> wrote:
> 
> > Rewrite copy_huge_page() and move it into mm/util.c so it's always
> > available.  Fixes an exposure of uninitialised memory on configurations
> > with HUGETLB and UFFD enabled and MIGRATION disabled.
> 
> Wait.  Exposing uninitialized memory is serious.  Can we please include
> full info on this flaw and decide whether a -stable backport is justified? 
> If not, why not, etc?

Well, the code was only merged this merge window, so as long as it goes
in in the next few weeks, there's no need to trouble -stable with it.
Linus Torvalds July 14, 2021, 4:09 p.m. UTC | #5
On Tue, Jul 13, 2021 at 7:21 PM Matthew Wilcox <willy@infradead.org> wrote:
>
> Well, the code was only merged this merge window, so as long as it goes
> in in the next few weeks, there's no need to trouble -stable with it.

It already went in on Monday as commit 79789db03fdd.

           Linus
diff mbox series

Patch

diff --git a/include/linux/migrate.h b/include/linux/migrate.h
index 9b7b7cd3bae9..23dadf7aeba8 100644
--- a/include/linux/migrate.h
+++ b/include/linux/migrate.h
@@ -51,7 +51,6 @@  extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 				  struct page *newpage, struct page *page);
 extern int migrate_page_move_mapping(struct address_space *mapping,
 		struct page *newpage, struct page *page, int extra_count);
-extern void copy_huge_page(struct page *dst, struct page *src);
 #else
 
 static inline void putback_movable_pages(struct list_head *l) {}
@@ -77,10 +76,6 @@  static inline int migrate_huge_page_move_mapping(struct address_space *mapping,
 {
 	return -ENOSYS;
 }
-
-static inline void copy_huge_page(struct page *dst, struct page *src)
-{
-}
 #endif /* CONFIG_MIGRATION */
 
 #ifdef CONFIG_COMPACTION
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 8f8e9d8a8489..629df6a24527 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -874,6 +874,7 @@  void __put_page(struct page *page);
 void put_pages_list(struct list_head *pages);
 
 void split_page(struct page *page, unsigned int order);
+void copy_huge_page(struct page *dst, struct page *src);
 
 /*
  * Compound pages have a destructor function.  Provide a
diff --git a/mm/migrate.c b/mm/migrate.c
index 23cbd9de030b..34a9ad3e0a4f 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -536,54 +536,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);
-	}
-}
-
-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 page to its new location
  */
diff --git a/mm/util.c b/mm/util.c
index 99c6cc77de9e..9043d03750a7 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -731,6 +731,16 @@  int __page_mapcount(struct page *page)
 }
 EXPORT_SYMBOL_GPL(__page_mapcount);
 
+void copy_huge_page(struct page *dst, struct page *src)
+{
+	unsigned i, nr = compound_nr(src);
+
+	for (i = 0; i < nr; i++) {
+		cond_resched();
+		copy_highpage(nth_page(dst, i), nth_page(src, i));
+	}
+}
+
 int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;
 int sysctl_overcommit_ratio __read_mostly = 50;
 unsigned long sysctl_overcommit_kbytes __read_mostly;