Skip to content

Commit 79789db

Browse files
Matthew Wilcox (Oracle)torvalds
authored andcommitted
mm: Make copy_huge_page() always available
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: 8cc5fcb ("mm, hugetlb: fix racy resv_huge_pages underflow on UFFDIO_COPY") Signed-off-by: Matthew Wilcox (Oracle) <[email protected]> Reviewed-by: Mike Kravetz <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent efdb672 commit 79789db

File tree

4 files changed

+11
-53
lines changed

4 files changed

+11
-53
lines changed

include/linux/migrate.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ extern int migrate_huge_page_move_mapping(struct address_space *mapping,
5151
struct page *newpage, struct page *page);
5252
extern int migrate_page_move_mapping(struct address_space *mapping,
5353
struct page *newpage, struct page *page, int extra_count);
54-
extern void copy_huge_page(struct page *dst, struct page *src);
5554
#else
5655

5756
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,
7776
{
7877
return -ENOSYS;
7978
}
80-
81-
static inline void copy_huge_page(struct page *dst, struct page *src)
82-
{
83-
}
8479
#endif /* CONFIG_MIGRATION */
8580

8681
#ifdef CONFIG_COMPACTION

include/linux/mm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ void __put_page(struct page *page);
906906
void put_pages_list(struct list_head *pages);
907907

908908
void split_page(struct page *page, unsigned int order);
909+
void copy_huge_page(struct page *dst, struct page *src);
909910

910911
/*
911912
* Compound pages have a destructor function. Provide a

mm/migrate.c

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -536,54 +536,6 @@ int migrate_huge_page_move_mapping(struct address_space *mapping,
536536
return MIGRATEPAGE_SUCCESS;
537537
}
538538

539-
/*
540-
* Gigantic pages are so large that we do not guarantee that page++ pointer
541-
* arithmetic will work across the entire page. We need something more
542-
* specialized.
543-
*/
544-
static void __copy_gigantic_page(struct page *dst, struct page *src,
545-
int nr_pages)
546-
{
547-
int i;
548-
struct page *dst_base = dst;
549-
struct page *src_base = src;
550-
551-
for (i = 0; i < nr_pages; ) {
552-
cond_resched();
553-
copy_highpage(dst, src);
554-
555-
i++;
556-
dst = mem_map_next(dst, dst_base, i);
557-
src = mem_map_next(src, src_base, i);
558-
}
559-
}
560-
561-
void copy_huge_page(struct page *dst, struct page *src)
562-
{
563-
int i;
564-
int nr_pages;
565-
566-
if (PageHuge(src)) {
567-
/* hugetlbfs page */
568-
struct hstate *h = page_hstate(src);
569-
nr_pages = pages_per_huge_page(h);
570-
571-
if (unlikely(nr_pages > MAX_ORDER_NR_PAGES)) {
572-
__copy_gigantic_page(dst, src, nr_pages);
573-
return;
574-
}
575-
} else {
576-
/* thp page */
577-
BUG_ON(!PageTransHuge(src));
578-
nr_pages = thp_nr_pages(src);
579-
}
580-
581-
for (i = 0; i < nr_pages; i++) {
582-
cond_resched();
583-
copy_highpage(dst + i, src + i);
584-
}
585-
}
586-
587539
/*
588540
* Copy the page to its new location
589541
*/

mm/util.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,16 @@ int __page_mapcount(struct page *page)
731731
}
732732
EXPORT_SYMBOL_GPL(__page_mapcount);
733733

734+
void copy_huge_page(struct page *dst, struct page *src)
735+
{
736+
unsigned i, nr = compound_nr(src);
737+
738+
for (i = 0; i < nr; i++) {
739+
cond_resched();
740+
copy_highpage(nth_page(dst, i), nth_page(src, i));
741+
}
742+
}
743+
734744
int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;
735745
int sysctl_overcommit_ratio __read_mostly = 50;
736746
unsigned long sysctl_overcommit_kbytes __read_mostly;

0 commit comments

Comments
 (0)