Skip to contentSkip to conversation

More THP fixes

PATCH In review Matthew Wilcox (Oracle) 2 participants 11 messages Oct 26, 2020 – Oct 27, 2020

Subsystems (11)
Memory management VFS MM core MGLRU +7 more subsystems
lore
Share & tools
lore

Matthew Wilcox (Oracle)

author maintainer
#0 lore raw

cover More THP fixes

I'm not sure there's a common thread to this set of THP patches other than I think they're pretty uncontroversial. Maybe I'm wrong.

Matthew Wilcox (Oracle) (8):
mm: Support THPs in zero_user_segments mm/page-flags: Allow accessing PageError on tail pages
mm: Return head pages from grab_cache_page_write_begin
mm: Replace prep_transhuge_page with thp_prep mm/truncate: Make invalidate_inode_pages2_range work with THPs mm/truncate: Fix invalidate_complete_page2 for THPs mm/vmscan: Free non-shmem THPs without splitting them
mm: Fix READ_ONLY_THP warning

Zi Yan (1):
mm: Fix THP size assumption in mem_cgroup_split_huge_fixup

 include/linux/highmem.h    | 19 +++++++++---
 include/linux/huge_mm.h    |  7 +++--
 include/linux/page-flags.h |  3 +-
 include/linux/pagemap.h    |  4 +--
 mm/filemap.c               | 15 ++++++---
 mm/highmem.c               | 62 ++++++++++++++++++++++++++++++++++++--
 mm/huge_memory.c           | 12 +++++---
 mm/internal.h              |  1 +
 mm/khugepaged.c            | 12 ++------
 mm/memcontrol.c            |  2 +-
 mm/mempolicy.c             | 15 +++------
 mm/migrate.c               | 15 +++------
 mm/page-writeback.c        |  2 +-
 mm/shmem.c                 |  9 +++---
 mm/truncate.c              | 25 ++++++---------
 mm/vmscan.c                |  4 +--
 16 files changed, 132 insertions(+), 75 deletions(-)
Signature · 1 line
2.28.0
author maintainer
#1 lore raw

PATCH 1/9 mm: Support THPs in zero_user_segments

We can only kmap() one subpage of a THP at a time, so loop over all relevant subpages, skipping ones which don't need to be zeroed. This is too large to inline when THPs are enabled and we actually need highmem, so put it in highmem.c.

---
 include/linux/highmem.h | 19 ++++++++++---
 mm/highmem.c            | 62 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 75 insertions(+), 6 deletions(-)
include/linux/highmem.h +15 -4
index 14e6202ce47f..8e21fe82b3a3 100644
@@ -284,13 +284,22 @@ static inline void clear_highpage(struct page *page)
 	kunmap_atomic(kaddr);
 }
 
+/*
+ * If we pass in a base or tail page, we can zero up to PAGE_SIZE.
+ * If we pass in a head page, we can zero up to the size of the compound page.
+ */
+#if defined(CONFIG_HIGHMEM) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
+void zero_user_segments(struct page *page, unsigned start1, unsigned end1,
+		unsigned start2, unsigned end2);
+#else /* !HIGHMEM || !TRANSPARENT_HUGEPAGE */
 static inline void zero_user_segments(struct page *page,
-	unsigned start1, unsigned end1,
-	unsigned start2, unsigned end2)
+		unsigned start1, unsigned end1,
+		unsigned start2, unsigned end2)
 {
 	void *kaddr = kmap_atomic(page);
+	unsigned int i;
 
-	BUG_ON(end1 > PAGE_SIZE || end2 > PAGE_SIZE);
+	BUG_ON(end1 > page_size(page) || end2 > page_size(page));
 
 	if (end1 > start1)
 		memset(kaddr + start1, 0, end1 - start1);
@@ -299,8 +308,10 @@ static inline void zero_user_segments(struct page *page,
 		memset(kaddr + start2, 0, end2 - start2);
 
 	kunmap_atomic(kaddr);
-	flush_dcache_page(page);
+	for (i = 0; i < compound_nr(page); i++)
+		flush_dcache_page(page + i);
 }
+#endif /* !HIGHMEM || !TRANSPARENT_HUGEPAGE */
 
 static inline void zero_user_segment(struct page *page,
 	unsigned start, unsigned end)
mm/highmem.c +60 -2

69 lines not on this page · raw message

Signature · 1 line
2.28.0
author maintainer
#2 lore raw

PATCH 2/9 mm/page-flags: Allow accessing PageError on tail pages

In reply to Matthew Wilcox (Oracle)

We track whether a page has had a read error for the entire THP, just like we track Uptodate and Dirty. This lets, eg, generic_file_buffered_read() continue to work on the appropriate subpage of the THP without modification.

---
 include/linux/page-flags.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/page-flags.h +2 -1
index 4f6ba9379112..eb3a9796de8e 100644
@@ -328,7 +328,8 @@ static inline int TestClearPage##uname(struct page *page) { return 0; }
 
 __PAGEFLAG(Locked, locked, PF_NO_TAIL)
 PAGEFLAG(Waiters, waiters, PF_ONLY_HEAD) __CLEARPAGEFLAG(Waiters, waiters, PF_ONLY_HEAD)
-PAGEFLAG(Error, error, PF_NO_TAIL) TESTCLEARFLAG(Error, error, PF_NO_TAIL)
+PAGEFLAG(Error, error, PF_HEAD)
+	TESTCLEARFLAG(Error, error, PF_HEAD)
 PAGEFLAG(Referenced, referenced, PF_HEAD)
 	TESTCLEARFLAG(Referenced, referenced, PF_HEAD)
 	__SETPAGEFLAG(Referenced, referenced, PF_HEAD)
Signature · 1 line
2.28.0
author maintainer
#3 lore raw

PATCH 3/9 mm: Return head pages from grab_cache_page_write_begin

In reply to Matthew Wilcox (Oracle)

This function is only called from filesystems on their own mapping, so no caller will be surprised by getting back a head page when they were expecting a tail page. This lets us remove a call to thp_head() in wait_for_stable_page().

---
 mm/filemap.c        | 12 ++++++++++--
 mm/page-writeback.c |  2 +-
 2 files changed, 11 insertions(+), 3 deletions(-)
mm/filemap.c +10 -2
index 2214a2c48dd1..62bc6affeb70 100644
@@ -3327,15 +3327,23 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from)
 }
 EXPORT_SYMBOL(generic_file_direct_write);
 
-/*
+/**
+ * grab_cache_page_write_begin - Find or create a page for buffered writes.
+ * @mapping: The address space we're writing to.
+ * @index: The index we're writing to.
+ * @flags: %AOP_FLAG_NOFS to prevent memory reclaim calling the filesystem.
+ *
  * Find or create a page at the given pagecache position. Return the locked
  * page. This function is specifically for buffered writes.
+ *
+ * Return: The head page found in the cache, or NULL if no page could be
+ * created (due to lack of memory).
  */
 struct page *grab_cache_page_write_begin(struct address_space *mapping,
 					pgoff_t index, unsigned flags)
 {
 	struct page *page;
-	int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT;
+	int fgp_flags = FGP_LOCK|FGP_WRITE|FGP_CREAT|FGP_HEAD;
 
 	if (flags & AOP_FLAG_NOFS)
 		fgp_flags |= FGP_NOFS;
mm/page-writeback.c +1 -1
index 7709f0e223f5..3671568d433f 100644
@@ -2849,7 +2849,7 @@ EXPORT_SYMBOL_GPL(wait_on_page_writeback);
  */
 void wait_for_stable_page(struct page *page)
 {
-	page = thp_head(page);
+	VM_BUG_ON_PGFLAGS(PageTail(page), page);
 	if (page->mapping->host->i_sb->s_iflags & SB_I_STABLE_WRITES)
 		wait_on_page_writeback(page);
 }
Signature · 1 line
2.28.0
author maintainer
#4 lore raw

PATCH 4/9 mm: Replace prep_transhuge_page with thp_prep

In reply to Matthew Wilcox (Oracle)

Since this is a THP function, move it into the thp_* function namespace.

By permitting NULL or order-0 pages as an argument, and returning the argument, callers can write:

	return thp_prep(alloc_pages(...));

instead of assigning the result to a temporary variable and conditionally passing that to prep_transhuge_page(). It'd be even nicer to have a thp_alloc() function, but there are a lot of different ways that THPs get allocated, and replicating the alloc_pages() family of APIs is a bit too much verbosity.

---
 include/linux/huge_mm.h |  7 +++++--
 mm/huge_memory.c        | 12 ++++++++----
 mm/khugepaged.c         | 12 +++---------
 mm/mempolicy.c          | 15 ++++-----------
 mm/migrate.c            | 15 ++++-----------
 mm/shmem.c              |  9 ++++-----
 6 files changed, 28 insertions(+), 42 deletions(-)
include/linux/huge_mm.h +5 -2
index 0365aa97f8e7..c2ecb6036ad8 100644
@@ -183,7 +183,7 @@ extern unsigned long thp_get_unmapped_area(struct file *filp,
 		unsigned long addr, unsigned long len, unsigned long pgoff,
 		unsigned long flags);
 
-extern void prep_transhuge_page(struct page *page);
+extern struct page *thp_prep(struct page *page);
 extern void free_transhuge_page(struct page *page);
 bool is_transparent_hugepage(struct page *page);
 
@@ -375,7 +375,10 @@ static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
 	return false;
 }
 
-static inline void prep_transhuge_page(struct page *page) {}
+static inline struct page *thp_prep(struct page *page)
+{
+	return page;
+}
 
 static inline bool is_transparent_hugepage(struct page *page)
 {
mm/huge_memory.c +8 -4
index 9474dbc150ed..4448b9cb4327 100644
@@ -487,15 +487,20 @@ static inline struct deferred_split *get_deferred_split_queue(struct page *page)
 }
 #endif
 
-void prep_transhuge_page(struct page *page)
+struct page *thp_prep(struct page *page)
 {
+	if (!page || compound_order(page) == 0)
+		return page;
 	/*
-	 * we use page->mapping and page->indexlru in second tail page
+	 * we use page->mapping and page->index in second tail page
 	 * as list_head: assuming THP order >= 2
 	 */
+	BUG_ON(compound_order(page) == 1);
 
 	INIT_LIST_HEAD(page_deferred_list(page));
 	set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
+
+	return page;
 }
 
 bool is_transparent_hugepage(struct page *page)
@@ -745,12 +750,11 @@ vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
 		return ret;
 	}
 	gfp = alloc_hugepage_direct_gfpmask(vma);
-	page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
+	page = thp_prep(alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER));
 	if (unlikely(!page)) {
 		count_vm_event(THP_FAULT_FALLBACK);
 		return VM_FAULT_FALLBACK;
 	}
-	prep_transhuge_page(page);
 	return __do_huge_pmd_anonymous_page(vmf, page, gfp);
 }
 
mm/khugepaged.c +3 -9
index 4e3dff13eb70..3b09c7e4ae3a 100644
@@ -867,14 +867,13 @@ khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
 {
 	VM_BUG_ON_PAGE(*hpage, *hpage);
 
-	*hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
+	*hpage = thp_prep(__alloc_pages_node(node, gfp, HPAGE_PMD_ORDER));
 	if (unlikely(!*hpage)) {
 		count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
 		*hpage = ERR_PTR(-ENOMEM);
 		return NULL;
 	}
 
-	prep_transhuge_page(*hpage);
 	count_vm_event(THP_COLLAPSE_ALLOC);
 	return *hpage;
 }
@@ -886,13 +885,8 @@ static int khugepaged_find_target_node(void)
 
 static inline struct page *alloc_khugepaged_hugepage(void)
 {
-	struct page *page;
-
-	page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
-			   HPAGE_PMD_ORDER);
-	if (page)
-		prep_transhuge_page(page);
-	return page;
+	return thp_prep(alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
+			   HPAGE_PMD_ORDER));
 }
 
 static struct page *khugepaged_alloc_hugepage(bool *wait)
mm/mempolicy.c +4 -11
index 3fde772ef5ef..e97cee53c0b1 100644
@@ -1226,19 +1226,12 @@ static struct page *new_page(struct page *page, unsigned long start)
 		vma = vma->vm_next;
 	}
 
-	if (PageHuge(page)) {
+	if (PageHuge(page))
 		return alloc_huge_page_vma(page_hstate(compound_head(page)),
 				vma, address);
-	} else if (PageTransHuge(page)) {
-		struct page *thp;
-
-		thp = alloc_hugepage_vma(GFP_TRANSHUGE, vma, address,
-					 HPAGE_PMD_ORDER);
-		if (!thp)
-			return NULL;
-		prep_transhuge_page(thp);
-		return thp;
-	}
+	if (PageTransHuge(page))
+		return thp_prep(alloc_hugepage_vma(GFP_TRANSHUGE, vma,
+				address, thp_order(page)));
 	/*
 	 * if !vma, alloc_page_vma() will use task or system default policy
 	 */
mm/migrate.c +4 -11
index 5ca5842df5db..262c91038c41 100644
@@ -1538,7 +1538,6 @@ struct page *alloc_migration_target(struct page *page, unsigned long private)
 	struct migration_target_control *mtc;
 	gfp_t gfp_mask;
 	unsigned int order = 0;
-	struct page *new_page = NULL;
 	int nid;
 	int zidx;
 
@@ -1568,12 +1567,8 @@ struct page *alloc_migration_target(struct page *page, unsigned long private)
 	if (is_highmem_idx(zidx) || zidx == ZONE_MOVABLE)
 		gfp_mask |= __GFP_HIGHMEM;
 
-	new_page = __alloc_pages_nodemask(gfp_mask, order, nid, mtc->nmask);
-
-	if (new_page && PageTransHuge(new_page))
-		prep_transhuge_page(new_page);
-
-	return new_page;
+	return thp_prep(__alloc_pages_nodemask(gfp_mask, order, nid,
+			mtc->nmask));
 }
 
 #ifdef CONFIG_NUMA
@@ -2134,12 +2129,10 @@ int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 	int page_lru = page_is_file_lru(page);
 	unsigned long start = address & HPAGE_PMD_MASK;
 
-	new_page = alloc_pages_node(node,
-		(GFP_TRANSHUGE_LIGHT | __GFP_THISNODE),
-		HPAGE_PMD_ORDER);
+	new_page = thp_prep(alloc_pages_node(node,
+			GFP_TRANSHUGE_LIGHT | __GFP_THISNODE, HPAGE_PMD_ORDER));
 	if (!new_page)
 		goto out_fail;
-	prep_transhuge_page(new_page);
 
 	isolated = numamigrate_isolate_page(pgdat, page);
 	if (!isolated) {
mm/shmem.c +4 -5
index 0cce132457f1..c10f8ecf85ce 100644
@@ -1486,12 +1486,11 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp,
 		return NULL;
 
 	shmem_pseudo_vma_init(&pvma, info, hindex);
-	page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
-			HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
+	gfp |= __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN;
+	page = thp_prep(alloc_pages_vma(gfp, HPAGE_PMD_ORDER, &pvma, 0,
+			numa_node_id(), true));
 	shmem_pseudo_vma_destroy(&pvma);
-	if (page)
-		prep_transhuge_page(page);
-	else
+	if (!page)
 		count_vm_event(THP_FILE_FALLBACK);
 	return page;
 }
Signature · 1 line
2.28.0
author maintainer
#5 lore raw

PATCH 5/9 mm/truncate: Make invalidate_inode_pages2_range work with THPs

In reply to Matthew Wilcox (Oracle)

If we're going to unmap a THP, we have to be sure to unmap the entire page, not just the part of it which lies after the search index.

---
 mm/truncate.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)
mm/truncate.c +8 -12
index 27cf411ae51f..30653b2717d3 100644
@@ -652,6 +652,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
 	while (find_get_entries(mapping, index, end, &pvec, indices)) {
 		for (i = 0; i < pagevec_count(&pvec); i++) {
 			struct page *page = pvec.pages[i];
+			pgoff_t count;
 
 			/* We rely upon deletion not changing page->index */
 			index = indices[i];
@@ -664,27 +665,22 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
 			}
 
 			lock_page(page);
-			WARN_ON(page_to_index(page) != index);
 			if (page->mapping != mapping) {
 				unlock_page(page);
 				continue;
 			}
 			wait_on_page_writeback(page);
+			count = thp_nr_pages(page);
+			index = page->index + count - 1;
 			if (page_mapped(page)) {
 				if (!did_range_unmap) {
-					/*
-					 * Zap the rest of the file in one hit.
-					 */
-					unmap_mapping_pages(mapping, index,
-						(1 + end - index), false);
+					/* Zap the rest of the file */
+					count = max(count,
+							end - page->index + 1);
 					did_range_unmap = 1;
-				} else {
-					/*
-					 * Just zap this page
-					 */
-					unmap_mapping_pages(mapping, index,
-								1, false);
 				}
+				unmap_mapping_pages(mapping, page->index,
+						count, false);
 			}
 			BUG_ON(page_mapped(page));
 			ret2 = do_launder_page(mapping, page);
Signature · 1 line
2.28.0
author maintainer
#6 lore raw

PATCH 6/9 mm/truncate: Fix invalidate_complete_page2 for THPs

In reply to Matthew Wilcox (Oracle)

invalidate_complete_page2() currently open-codes page_cache_free_page(), except for the part where it handles THP. Rather than adding that, call page_cache_free_page() from invalidate_complete_page2().

---
 mm/filemap.c  | 3 +--
 mm/internal.h | 1 +
 mm/truncate.c | 5 +----
 3 files changed, 3 insertions(+), 6 deletions(-)
mm/filemap.c +1 -2
index 62bc6affeb70..00de12d42bc4 100644
@@ -228,8 +228,7 @@ void __delete_from_page_cache(struct page *page, void *shadow)
 	page_cache_delete(mapping, page, shadow);
 }
 
-static void page_cache_free_page(struct address_space *mapping,
-				struct page *page)
+void page_cache_free_page(struct address_space *mapping, struct page *page)
 {
 	void (*freepage)(struct page *);
 
mm/internal.h +1 -0
index 5aca7d7bc57c..1391e3239547 100644
@@ -623,4 +623,5 @@ struct migration_target_control {
 };
 
 bool truncate_inode_partial_page(struct page *page, loff_t start, loff_t end);
+void page_cache_free_page(struct address_space *mapping, struct page *page);
 #endif	/* __MM_INTERNAL_H */
mm/truncate.c +1 -4
index 30653b2717d3..bed24857d1d2 100644
@@ -603,10 +603,7 @@ invalidate_complete_page2(struct address_space *mapping, struct page *page)
 	__delete_from_page_cache(page, NULL);
 	xa_unlock_irqrestore(&mapping->i_pages, flags);
 
-	if (mapping->a_ops->freepage)
-		mapping->a_ops->freepage(page);
-
-	put_page(page);	/* pagecache ref */
+	page_cache_free_page(mapping, page);
 	return 1;
 failed:
 	xa_unlock_irqrestore(&mapping->i_pages, flags);
Signature · 1 line
2.28.0
author maintainer
#7 lore raw

PATCH 7/9 mm/vmscan: Free non-shmem THPs without splitting them

In reply to Matthew Wilcox (Oracle)

We have to allocate memory in order to split a file-backed page, so it's not a good idea to split them. It also doesn't work for XFS because pages have an extra reference count from page_has_private() and split_huge_page() expects that reference to have already been removed. Unfortunately, we still have to split shmem THPs because we can't handle swapping out an entire THP yet.

---
 mm/vmscan.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/vmscan.c +2 -2
index 1b8f0e059767..8e60ae2fabd1 100644
@@ -1274,8 +1274,8 @@ static unsigned int shrink_page_list(struct list_head *page_list,
 				/* Adding to swap updated mapping */
 				mapping = page_mapping(page);
 			}
-		} else if (unlikely(PageTransHuge(page))) {
-			/* Split file THP */
+		} else if (PageSwapBacked(page) && PageTransHuge(page)) {
+			/* Split shmem THP */
 			if (split_huge_page_to_list(page, page_list))
 				goto keep_locked;
 		}
Signature · 1 line
2.28.0
author maintainer
#8 lore raw

PATCH 8/9 mm: Fix THP size assumption in mem_cgroup_split_huge_fixup

In reply to Matthew Wilcox (Oracle)

From: Zi Yan <[email protected]>

Ask the page what size it is instead of assuming PMD size.

---
 mm/memcontrol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/memcontrol.c +1 -1
index 3a24e3b619f5..e7824c4dab25 100644
@@ -3280,7 +3280,7 @@ void mem_cgroup_split_huge_fixup(struct page *head)
 	if (mem_cgroup_disabled())
 		return;
 
-	for (i = 1; i < HPAGE_PMD_NR; i++) {
+	for (i = 1; i < thp_nr_pages(head); i++) {
 		css_get(&memcg->css);
 		head[i].mem_cgroup = memcg;
 	}
Signature · 1 line
2.28.0
author maintainer
#9 lore raw

PATCH 9/9 mm: Fix READ_ONLY_THP warning

In reply to Matthew Wilcox (Oracle)

These counters only exist if CONFIG_READ_ONLY_THP_FOR_FS is defined, but we should not warn if the filesystem natively supports THPs.

---
 include/linux/pagemap.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/pagemap.h +2 -2
index 86143d36d028..2c736f8ae324 100644
@@ -146,7 +146,7 @@ static inline void filemap_nr_thps_inc(struct address_space *mapping)
 	if (!mapping_thp_support(mapping))
 		atomic_inc(&mapping->nr_thps);
 #else
-	WARN_ON_ONCE(1);
+	WARN_ON_ONCE(!mapping_thp_support(mapping));
 #endif
 }
 
@@ -156,7 +156,7 @@ static inline void filemap_nr_thps_dec(struct address_space *mapping)
 	if (!mapping_thp_support(mapping))
 		atomic_dec(&mapping->nr_thps);
 #else
-	WARN_ON_ONCE(1);
+	WARN_ON_ONCE(!mapping_thp_support(mapping));
 #endif
 }
 
Signature · 1 line
2.28.0
reviewer Reviewed-by on the v1 series
#10 lore raw
In reply to Matthew Wilcox (Oracle)
Quoted 12 lines · Matthew Wilcox (Oracle)

I'm not sure there's a common thread to this set of THP patches other than I think they're pretty uncontroversial. Maybe I'm wrong.

Matthew Wilcox (Oracle) (8):
mm: Support THPs in zero_user_segments mm/page-flags: Allow accessing PageError on tail pages
mm: Return head pages from grab_cache_page_write_begin
mm: Replace prep_transhuge_page with thp_prep mm/truncate: Make invalidate_inode_pages2_range work with THPs mm/truncate: Fix invalidate_complete_page2 for THPs mm/vmscan: Free non-shmem THPs without splitting them
mm: Fix READ_ONLY_THP warning

They look good to me. Thanks.

—
Best Regards,
Yan Zi