Skip to contentSkip to conversation

folio based filemap_map_pages()

RFC PATCH Superseded Yin Fengwei 4 participants 19 messages Feb 1, 2023 – Feb 3, 2023

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover folio based filemap_map_pages()

Current filemap_map_pages() uses page granularity even when underneath folio is large folio. Making it use folio based granularity allows batched refcount, rmap and mm counter update. Which brings performance gain.

This sereis tries to bring batched refcount, rmap and mm counter for filemap_map_pages(). Testing with a micro benchmark like will-it-scale.pagefault on a 48C/96T IceLake tbox showed:
- batched rmap brings around 15% performance gain
- batched refcount brings around 2% performance gain

Patch 1 enabled the fault around for share file page write
fault. As David suggested here: [1]

Patch 2 update filemap_map_pages() to do map based on folio
granularity and batched refcount update

Patch 3,4,5 enable batched rmap and mm counter

[1] https://lore.kernel.org/linux-mm/[email protected]/

Change from v1:
- Update the struct page * parameter of *_range() to index in the folio as Matthew suggested
- Fix indentations problem as Matthew pointed out
- Add back the function comment as Matthew pointed out
- Use nr_pages than len as Matthew pointed out
- Add do_set_pte_range() as Matthew suggested
- Add function comment as Ying suggested
- Add performance test result to patch 1/2/5 commit message

Patch 1:
- Adapt commit message as Matthew suggested
- Add Reviweed-by from Matthew
Patch 3:
- Restore general logic of page_add_file_rmap_range() to make patch review easier as Matthew suggested
Patch 5:
- Add perf data collected to understand the reason of performance gain

Yin Fengwei (5):
mm: Enable fault around for shared file page fault
filemap: add function filemap_map_folio_range()
rmap: add page_add_file_rmap_range()
mm: add do_set_pte_range()
filemap: batched update mm counter,rmap when map file folio

 include/linux/mm.h   |   2 +
 include/linux/rmap.h |   2 +
 mm/filemap.c         | 119 ++++++++++++++++++++++++++++++-------------
 mm/memory.c          |  71 +++++++++++++++++++++-----
 mm/rmap.c            |  66 +++++++++++++++++++-----
 5 files changed, 199 insertions(+), 61 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

RFC PATCH v2 1/5 mm: Enable fault around for shared file page fault

The shared fault handler can also benefit from fault-around. While it is uncommon to write to MAP_SHARED files, applications that do will see a huge benefit with will-it-scale:page_fault3 (shared file write fault) improving by 375%.

---
 mm/memory.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
mm/memory.c +11 -0
index 7a04a1130ec1..51c04bb60724 100644
@@ -4546,6 +4546,17 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf)
 	struct vm_area_struct *vma = vmf->vma;
 	vm_fault_t ret, tmp;
 
+	/*
+	 * Let's call ->map_pages() first and use ->fault() as fallback
+	 * if page by the offset is not ready to be mapped (cold cache or
+	 * something).
+	 */
+	if (should_fault_around(vmf)) {
+		ret = do_fault_around(vmf);
+		if (ret)
+			return ret;
+	}
+
 	ret = __do_fault(vmf);
 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
 		return ret;
Signature · 1 line
2.30.2
#2 lore raw

Re: [RFC PATCH v2 1/5] mm: Enable fault around for shared file page fault

Quoted 20 lines · Yin Fengwei

The shared fault handler can also benefit from fault-around. While it is uncommon to write to MAP_SHARED files, applications that do will see a huge benefit with will-it-scale:page_fault3 (shared file write fault) improving by 375%.

---
 mm/memory.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
mm/memory.c +1 -0
index 7a04a1130ec1..51c04bb60724 100644
@@ -4546,6 +4546,17 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf)
 	struct vm_area_struct *vma = vmf->vma;
 	vm_fault_t ret, tmp;
 
+	/*

mm/memory.c static vm_fault_t do_shared_fault(struct vm_fault *vmf) · line 4559

Quoted 10 lines
mm/memory.c +10 -0
+	 * Let's call ->map_pages() first and use ->fault() as fallback
+	 * if page by the offset is not ready to be mapped (cold cache or
+	 * something).
+	 */
+	if (should_fault_around(vmf)) {
+		ret = do_fault_around(vmf);
+		if (ret)
+			return ret;
+	}
+

I believe it bypasses ->page_mkwrite() completely, no?

So you get a writable PTEs without notifying the filesystem. Smells like a data loss.

Quoted 7 lines
mm/memory.c +0 -0
 	ret = __do_fault(vmf);
 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
 		return ret;
Signature · 1 line
2.30.2
Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#3 lore raw

Re: [RFC PATCH v2 1/5] mm: Enable fault around for shared file page fault

Quoted 34 lines · Kirill A. Shutemov
Quoted 20 lines · Yin Fengwei

The shared fault handler can also benefit from fault-around. While it is uncommon to write to MAP_SHARED files, applications that do will see a huge benefit with will-it-scale:page_fault3 (shared file write fault) improving by 375%.

---
 mm/memory.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
mm/memory.c +1 -0
index 7a04a1130ec1..51c04bb60724 100644
@@ -4546,6 +4546,17 @@ static vm_fault_t do_shared_fault(struct vm_fault *vmf)
 	struct vm_area_struct *vma = vmf->vma;
 	vm_fault_t ret, tmp;
 
+	/*

mm/memory.c static vm_fault_t do_shared_fault(struct vm_fault *vmf) · line 4559

Quoted 10 lines
mm/memory.c +10 -0
+	 * Let's call ->map_pages() first and use ->fault() as fallback
+	 * if page by the offset is not ready to be mapped (cold cache or
+	 * something).
+	 */
+	if (should_fault_around(vmf)) {
+		ret = do_fault_around(vmf);
+		if (ret)
+			return ret;
+	}
+

I believe it bypasses ->page_mkwrite() completely, no?

Quoted 2 lines

So you get a writable PTEs without notifying the filesystem. Smells like a data loss.

Yes. You are right. This may be the reason why fault around is not enabled for shared file write fault? I will drop this patch. Thanks.

Regards
Yin, Fengwei

Quoted 9 lines
Quoted 8 lines
mm/memory.c +0 -0
 	ret = __do_fault(vmf);
 	if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE | VM_FAULT_RETRY)))
 		return ret;
Signature · 1 line
2.30.2
#4 lore raw

RFC PATCH v2 2/5 filemap: add function filemap_map_folio_range()

In reply to Yin Fengwei

filemap_map_folio_range() maps partial/full folio. Comparing to original filemap_map_pages(), it batched updates refcount and get minor performance improvement for large folio.

a self cooked will-it-scale.page_fault3 like app (change file write fault to read fault) with xfs filesystem got 2% performance gain.

---
 mm/filemap.c | 91 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 57 insertions(+), 34 deletions(-)
mm/filemap.c +57 -34
index 992554c18f1f..9cc5edd8f998 100644
@@ -3351,6 +3351,56 @@ static inline struct folio *next_map_page(struct address_space *mapping,
 				  mapping, xas, end_pgoff);
 }
 
+/*
+ * Map sub-pages range [start_page, start_page + nr_pages) of folio.
+ * start_page is gotten from start by folio_page(folio, start)
+ */
+static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
+			struct folio *folio, unsigned long start,
+			unsigned long addr, unsigned int nr_pages)
+{
+	vm_fault_t ret = 0;
+	struct vm_area_struct *vma = vmf->vma;
+	struct file *file = vma->vm_file;
+	struct page *page = folio_page(folio, start);
+	unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
+	unsigned int ref_count = 0, count = 0;
+
+	do {
+		if (PageHWPoison(page))
+			continue;
+
+		if (mmap_miss > 0)
+			mmap_miss--;
+
+		/*
+		 * NOTE: If there're PTE markers, we'll leave them to be
+		 * handled in the specific fault path, and it'll prohibit the
+		 * fault-around logic.
+		 */
+		if (!pte_none(*vmf->pte))
+			continue;
+
+		if (vmf->address == addr)
+			ret = VM_FAULT_NOPAGE;
+
+		ref_count++;
+		do_set_pte(vmf, page, addr);
+		update_mmu_cache(vma, addr, vmf->pte);
+	} while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages);
+
+	/*
+	 * Restore the vmf->pte. Otherwise, it's possible vmf->pte point
+	 * to next page table entry if the last sub page in the range is
+	 * mapped to the last entry of page table.
+	 */
+	vmf->pte -= nr_pages;
+	folio_ref_add(folio, ref_count);
+	WRITE_ONCE(file->f_ra.mmap_miss, mmap_miss);
+
+	return ret;
+}
+
 vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 			     pgoff_t start_pgoff, pgoff_t end_pgoff)
 {
@@ -3361,9 +3411,9 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 	unsigned long addr;
 	XA_STATE(xas, &mapping->i_pages, start_pgoff);
 	struct folio *folio;
-	struct page *page;
 	unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
 	vm_fault_t ret = 0;
+	int nr_pages = 0;
 
 	rcu_read_lock();
 	folio = first_map_page(mapping, &xas, end_pgoff);
@@ -3378,45 +3428,18 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 	addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) << PAGE_SHIFT);
 	vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, addr, &vmf->ptl);
 	do {
-again:
-		page = folio_file_page(folio, xas.xa_index);
-		if (PageHWPoison(page))
-			goto unlock;
-
-		if (mmap_miss > 0)
-			mmap_miss--;
+		unsigned long end;
 
 		addr += (xas.xa_index - last_pgoff) << PAGE_SHIFT;
 		vmf->pte += xas.xa_index - last_pgoff;
 		last_pgoff = xas.xa_index;
+		end = folio->index + folio_nr_pages(folio) - 1;
+		nr_pages = min(end, end_pgoff) - xas.xa_index + 1;
 
-		/*
-		 * NOTE: If there're PTE markers, we'll leave them to be
-		 * handled in the specific fault path, and it'll prohibit the
-		 * fault-around logic.
-		 */
-		if (!pte_none(*vmf->pte))
-			goto unlock;
-
-		/* We're about to handle the fault */
-		if (vmf->address == addr)
-			ret = VM_FAULT_NOPAGE;
+		ret |=	filemap_map_folio_range(vmf, folio,
+				xas.xa_index - folio->index, addr, nr_pages);
+		xas.xa_index = end;
 
-		do_set_pte(vmf, page, addr);
-		/* no need to invalidate: a not-present page won't be cached */
-		update_mmu_cache(vma, addr, vmf->pte);
-		if (folio_more_pages(folio, xas.xa_index, end_pgoff)) {
-			xas.xa_index++;
-			folio_ref_inc(folio);
-			goto again;
-		}
-		folio_unlock(folio);
-		continue;
-unlock:
-		if (folio_more_pages(folio, xas.xa_index, end_pgoff)) {
-			xas.xa_index++;
-			goto again;
-		}
 		folio_unlock(folio);
 		folio_put(folio);
 	} while ((folio = next_map_page(mapping, &xas, end_pgoff)) != NULL);
Signature · 1 line
2.30.2
#5 lore raw

RFC PATCH v2 3/5 rmap: add page_add_file_rmap_range()

In reply to Yin Fengwei

page_add_file_rmap_range() allows to add pte mapping to a specific range of file folio. Comparing to original page_add_file_rmap(), it batched updates __lruvec_stat for large folio.

---
 include/linux/rmap.h |  2 ++
 mm/rmap.c            | 66 ++++++++++++++++++++++++++++++++++----------
 2 files changed, 54 insertions(+), 14 deletions(-)
include/linux/rmap.h +2 -0
index a4570da03e58..9631a3701504 100644
@@ -198,6 +198,8 @@ void folio_add_new_anon_rmap(struct folio *, struct vm_area_struct *,
 		unsigned long address);
 void page_add_file_rmap(struct page *, struct vm_area_struct *,
 		bool compound);
+void page_add_file_rmap_range(struct folio *, unsigned long start,
+		unsigned int nr_pages, struct vm_area_struct *, bool compound);
 void page_remove_rmap(struct page *, struct vm_area_struct *,
 		bool compound);
 
mm/rmap.c +52 -14

89 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer
#6 lore raw

Re: [RFC PATCH v2 3/5] rmap: add page_add_file_rmap_range()

Quoted 4 lines · Yin Fengwei

/**
- * page_add_file_rmap - add pte mapping to a file page
- * @page: the page to add the mapping to
+ * page_add_file_rmap_range - add pte mapping to a sub page range of a folio

Quoted 8 lines

+ * @folio: The filio to add the mapping to
+ * @start: The first sub page index in folio
+ * @nr_pages: The number of sub pages from the first page
* @vma: the vm area in which the mapping is added
* @compound: charge the page as compound or small page
*
+ * The sub page range of folio is defined by
+ * [first_sub_page, first_sub_page + nr_pages)

Lose the "sub" from all of this. That's legacy thinking; pages are pages and folios are folios. "subpages" was from when we were trying to use the word "page" for both "the allocation" and "the PAGE_SIZE range of bytes".

mm/rmap.c void folio_add_new_anon_rmap(struct folio *folio, struct vm_area_struct *vma, · line 1320

Quoted 8 lines

+ *
* The caller needs to hold the pte lock.
*/

mm/rmap.c +3 -2
-void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,
-		bool compound)
+void page_add_file_rmap_range(struct folio *folio, unsigned long start,
+			unsigned int nr_pages, struct vm_area_struct *vma,
+			bool compound)

I think this function needs to be called folio_add_file_rmap()

I'd like to lose the 'compound' parameter, and base it on nr_pages == folio_nr_pages(), but that may be a step far just now.

#7 lore raw

Re: [RFC PATCH v2 3/5] rmap: add page_add_file_rmap_range()

Quoted 14 lines · Matthew Wilcox
Quoted 4 lines · Yin Fengwei

/**
- * page_add_file_rmap - add pte mapping to a file page
- * @page: the page to add the mapping to
+ * page_add_file_rmap_range - add pte mapping to a sub page range of a folio

Quoted 8 lines

+ * @folio: The filio to add the mapping to
+ * @start: The first sub page index in folio
+ * @nr_pages: The number of sub pages from the first page
* @vma: the vm area in which the mapping is added
* @compound: charge the page as compound or small page
*
+ * The sub page range of folio is defined by
+ * [first_sub_page, first_sub_page + nr_pages)

Quoted 4 lines

Lose the "sub" from all of this. That's legacy thinking; pages are pages and folios are folios. "subpages" was from when we were trying to use the word "page" for both "the allocation" and "the PAGE_SIZE range of bytes".

OK. Will remove sub in next version.

Quoted 11 lines

mm/rmap.c void folio_add_new_anon_rmap(struct folio *folio, struct vm_area_struct *vma, · line 1320

Quoted 9 lines

+ *
* The caller needs to hold the pte lock.
*/

mm/rmap.c +3 -2
-void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,
-		bool compound)
+void page_add_file_rmap_range(struct folio *folio, unsigned long start,
+			unsigned int nr_pages, struct vm_area_struct *vma,
+			bool compound)

I think this function needs to be called folio_add_file_rmap()

Yes. Maybe a followup patch after this series? Let me know if you want this change in this series.

Quoted 3 lines

I'd like to lose the 'compound' parameter, and base it on nr_pages == folio_nr_pages(), but that may be a step far just now.

Yes. I had a local change to remove if (folio_test_pmd_mappable(folio)) test (It's very close to removing 'compound'). I didn't include it in this series. I prefer a follow up patch. Let me know if you want the change in this series. Thanks.

Regards
Yin, Fengwei

Quoted 1 line
#8 lore raw

RFC PATCH v2 4/5 mm: add do_set_pte_range()

In reply to Yin Fengwei

do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio.

---
 include/linux/mm.h |  2 ++
 mm/filemap.c       |  1 -
 mm/memory.c        | 60 ++++++++++++++++++++++++++++++++++++----------
 3 files changed, 49 insertions(+), 14 deletions(-)
include/linux/mm.h +2 -0
index d6f8f41514cc..96e08fcdce24 100644
@@ -1162,6 +1162,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
 
 vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page);
 void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr);
+void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+		unsigned long start, unsigned long addr, unsigned int nr);
 
 vm_fault_t finish_fault(struct vm_fault *vmf);
 vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf);
mm/filemap.c +0 -1
index 9cc5edd8f998..95f634d11581 100644
@@ -3386,7 +3386,6 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 
 		ref_count++;
 		do_set_pte(vmf, page, addr);
-		update_mmu_cache(vma, addr, vmf->pte);
 	} while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages);
 
 	/*
mm/memory.c +47 -13

79 lines not on this page · raw message

Signature · 1 line
2.30.2
#9 lore raw

Re: [RFC PATCH v2 4/5] mm: add do_set_pte_range()

Quoted 81 lines · Yin Fengwei

do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio.

---
  include/linux/mm.h |  2 ++
  mm/filemap.c       |  1 -
  mm/memory.c        | 60 ++++++++++++++++++++++++++++++++++++----------
  3 files changed, 49 insertions(+), 14 deletions(-)
include/linux/mm.h +2 -0
index d6f8f41514cc..96e08fcdce24 100644
@@ -1162,6 +1162,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
 
  vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page);
  void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr);
+void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+		unsigned long start, unsigned long addr, unsigned int nr);
 
  vm_fault_t finish_fault(struct vm_fault *vmf);
  vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf);
mm/filemap.c +0 -1
index 9cc5edd8f998..95f634d11581 100644
@@ -3386,7 +3386,6 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 
  		ref_count++;
  		do_set_pte(vmf, page, addr);
-		update_mmu_cache(vma, addr, vmf->pte);
  	} while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages);
 
  	/*
mm/memory.c +19 -10
index 51c04bb60724..7e41142e1e4f 100644
@@ -4257,7 +4257,8 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
  }
  #endif
 
-void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
+static void do_set_pte_entry(struct vm_fault *vmf, struct page *page,
+		unsigned long addr)
  {
  	struct vm_area_struct *vma = vmf->vma;
  	bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte);
@@ -4277,16 +4278,52 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
  		entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  	if (unlikely(uffd_wp))
  		entry = pte_mkuffd_wp(entry);
-	/* copy-on-write page */
-	if (write && !(vma->vm_flags & VM_SHARED)) {
-		inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
-		page_add_new_anon_rmap(page, vma, addr);
-		lru_cache_add_inactive_or_unevictable(page, vma);
-	} else {
-		inc_mm_counter(vma->vm_mm, mm_counter_file(page));
-		page_add_file_rmap(page, vma, false);
-	}
  	set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
+
+	/* no need to invalidate: a not-present page won't be cached */
+	update_mmu_cache(vma, addr, vmf->pte);
+}
+
+void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+		unsigned long start, unsigned long addr, unsigned int nr)
+{
+	unsigned int i = 0;
+	struct page *page = folio_page(folio, start);
+	struct vm_area_struct *vma = vmf->vma;
+	bool cow = (vmf->flags & FAULT_FLAG_WRITE) &&
+			!(vma->vm_flags & VM_SHARED);
+
+	/*
+	 * file page: batched update rmap, mm counter.
+	 * copy-on-write page: batched update mm counter.

mm/memory.c void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) · line 4308

Quoted 10 lines
mm/memory.c +10 -0
+	 */
+	if (!cow) {
+		page_add_file_rmap_range(folio, start, nr, vma, false);
+		add_mm_counter(vma->vm_mm, mm_counter_file(page), nr);
+	} else
+		add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr);
+
+	do {
+		if (cow) {
+			page_add_new_anon_rmap(page, vma, addr);

This doesn't work with anon pages the way you intended in this patch.

Please leave anon pages out of the picture for now and make do_set_pte_range() only deal with !cow.

Signature · 3 lines
Thanks,

David / dhildenb
#10 lore raw

Re: [RFC PATCH v2 4/5] mm: add do_set_pte_range()

Quoted 90 lines · David Hildenbrand
Quoted 76 lines · Yin Fengwei

do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio.

---
  include/linux/mm.h |  2 ++
  mm/filemap.c       |  1 -
  mm/memory.c        | 60 ++++++++++++++++++++++++++++++++++++----------

  3 files changed, 49 insertions(+), 14 deletions(-)
include/linux/mm.h +2 -0
index d6f8f41514cc..96e08fcdce24 100644
@@ -1162,6 +1162,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
     vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page);
   void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr);
+void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+        unsigned long start, unsigned long addr, unsigned int nr);
     vm_fault_t finish_fault(struct vm_fault *vmf);
   vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf);
mm/filemap.c +0 -1
index 9cc5edd8f998..95f634d11581 100644
@@ -3386,7 +3386,6 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
             ref_count++;
           do_set_pte(vmf, page, addr);
-        update_mmu_cache(vma, addr, vmf->pte);
       } while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages);
         /*
mm/memory.c +19 -9
index 51c04bb60724..7e41142e1e4f 100644
@@ -4257,7 +4257,8 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
   }
   #endif
   -void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
+static void do_set_pte_entry(struct vm_fault *vmf, struct page *page,
+        unsigned long addr)
   {
       struct vm_area_struct *vma = vmf->vma;
       bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte);
@@ -4277,16 +4278,52 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
           entry = maybe_mkwrite(pte_mkdirty(entry), vma);
       if (unlikely(uffd_wp))
           entry = pte_mkuffd_wp(entry);
-    /* copy-on-write page */
-    if (write && !(vma->vm_flags & VM_SHARED)) {
-        inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
-        page_add_new_anon_rmap(page, vma, addr);
-        lru_cache_add_inactive_or_unevictable(page, vma);
-    } else {
-        inc_mm_counter(vma->vm_mm, mm_counter_file(page));
-        page_add_file_rmap(page, vma, false);
-    }
       set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
+
+    /* no need to invalidate: a not-present page won't be cached */
+    update_mmu_cache(vma, addr, vmf->pte);
+}
+
+void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+        unsigned long start, unsigned long addr, unsigned int nr)
+{
+    unsigned int i = 0;
+    struct page *page = folio_page(folio, start);
+    struct vm_area_struct *vma = vmf->vma;
+    bool cow = (vmf->flags & FAULT_FLAG_WRITE) &&
+            !(vma->vm_flags & VM_SHARED);
+
+    /*
+     * file page: batched update rmap, mm counter.
+     * copy-on-write page: batched update mm counter.

mm/memory.c void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) · line 4308

Quoted 10 lines
mm/memory.c +10 -0
+     */
+    if (!cow) {
+        page_add_file_rmap_range(folio, start, nr, vma, false);
+        add_mm_counter(vma->vm_mm, mm_counter_file(page), nr);
+    } else
+        add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr);
+
+    do {
+        if (cow) {
+            page_add_new_anon_rmap(page, vma, addr);

This doesn't work with anon pages the way you intended in this patch.

Quoted 1 line

Please leave anon pages out of the picture for now and make do_set_pte_range() only deal with !cow.

OK. I will move the check to do_set_pte() and only call to do_set_pte_range() when it's !cow. Thanks.

Regards
Yin, Fengwei

Quoted 1 line
maintainer
#11 lore raw

Re: [RFC PATCH v2 4/5] mm: add do_set_pte_range()

In reply to Yin Fengwei
Quoted 3 lines · Yin Fengwei

do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio.

How about something more like this? Yes, we need to define flush_icache_pages() and PTE_STRIDE.

(we could also do for (i = 0; i < nr; i++) flush_icache_page(...) but given that some architectures already implement flush_icache_range(), I think they may appreciate being given one large range to flush)

+++ b/mm/memory.c
@@ -4277,15 +4277,19 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page)
}
#endif

-void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) +void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+ unsigned int start, unsigned int nr,
+ unsigned long addr)
{

+       struct page *page = folio_page(page, start);
        struct vm_area_struct *vma = vmf->vma;
        bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte);
        bool write = vmf->flags & FAULT_FLAG_WRITE;
        bool prefault = vmf->address != addr;
        pte_t entry;

+ unsigned int i;

- flush_icache_page(vma, page);

+       flush_icache_pages(vma, page, nr);
        entry = mk_pte(page, vma->vm_page_prot);
 
        if (prefault && arch_wants_old_prefaulted_pte())
@@ -4299,14 +4303,23 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
                entry = pte_mkuffd_wp(pte_wrprotect(entry));
        /* copy-on-write page */
        if (write && !(vma->vm_flags & VM_SHARED)) {
-               inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
-               page_add_new_anon_rmap(page, vma, addr);
-               lru_cache_add_inactive_or_unevictable(page, vma);
+               add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr);
+               for (i = 0; i < nr; i++) {
+                       page_add_new_anon_rmap(page + i, vma, addr);
+                       lru_cache_add_inactive_or_unevictable(page + i, vma);
+               }
        } else {
-               inc_mm_counter(vma->vm_mm, mm_counter_file(page));
-               page_add_file_rmap(page, vma, false);
+               add_mm_counter(vma->vm_mm, mm_counter_file(page), nr);
+               folio_add_file_rmap(folio, start, n, vma);

+ }
+
+ for (i = 0; i < nr; i++) {
+ set_pte_at(vma->vm_mm, addr, vmf->pte + i, entry);
+ /* no need to invalidate: a not-present page won't be cached */
+ update_mmu_cache(vma, addr, vmf->pte + i);
+ addr += PAGE_SIZE;

+               entry += PTE_STRIDE;
        }

- set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
}

static bool vmf_pte_changed(struct vm_fault *vmf)

maintainer
#12 lore raw

Re: [RFC PATCH v2 4/5] mm: add do_set_pte_range()

Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Yin Fengwei

do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio.

How about something more like this? Yes, we need to define flush_icache_pages() and PTE_STRIDE.

Never mind about PTE_STRIDE. I forgot that pte_t isn't an integer type. Instead, we'll want each architecture to define

/* This should be right for x86 */
static inline pte_next(pte_t pte)

{
	return __pte(pte_val(pte) + PAGE_SIZE);
}

Review

Quoted 6 lines
(quoted diff) +6 -0
+       for (i = 0; i < nr; i++) {
+               set_pte_at(vma->vm_mm, addr, vmf->pte + i, entry);
+                /* no need to invalidate: a not-present page won't be cached */
+                update_mmu_cache(vma, addr, vmf->pte + i);
+               addr += PAGE_SIZE;
+               entry += PTE_STRIDE;
		entry = pte_next(entry);
#13 lore raw

Re: [RFC PATCH v2 4/5] mm: add do_set_pte_range()

In reply to Matthew Wilcox
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Yin Fengwei

do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio.

How about something more like this? Yes, we need to define flush_icache_pages() and PTE_STRIDE.

Yes. This could remove more duplicated operations here. Let me try to add flush_icache_pages() in next version.

But for pte_next(), it needs be added to handle all architectures. I suppose it will take long journey to make it. What about a followup patch for pte_next()?

Regards
Yin, Fengwei

Quoted 60 lines

(we could also do for (i = 0; i < nr; i++) flush_icache_page(...) but given that some architectures already implement flush_icache_range(), I think they may appreciate being given one large range to flush)

+++ b/mm/memory.c

(quoted diff) +6 -2
@@ -4277,15 +4277,19 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page 
 *page)
 }
 #endif
 
-void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
+void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+               unsigned int start, unsigned int nr,
+               unsigned long addr)
 {
+       struct page *page = folio_page(page, start);
        struct vm_area_struct *vma = vmf->vma;
        bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte);
        bool write = vmf->flags & FAULT_FLAG_WRITE;
        bool prefault = vmf->address != addr;
        pte_t entry;
+       unsigned int i;
 
-       flush_icache_page(vma, page);
+       flush_icache_pages(vma, page, nr);
        entry = mk_pte(page, vma->vm_page_prot);
 
        if (prefault && arch_wants_old_prefaulted_pte())
(quoted diff) +15 -6
@@ -4299,14 +4303,23 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
                entry = pte_mkuffd_wp(pte_wrprotect(entry));
        /* copy-on-write page */
        if (write && !(vma->vm_flags & VM_SHARED)) {
-               inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
-               page_add_new_anon_rmap(page, vma, addr);
-               lru_cache_add_inactive_or_unevictable(page, vma);
+               add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr);
+               for (i = 0; i < nr; i++) {
+                       page_add_new_anon_rmap(page + i, vma, addr);
+                       lru_cache_add_inactive_or_unevictable(page + i, vma);
+               }
        } else {
-               inc_mm_counter(vma->vm_mm, mm_counter_file(page));
-               page_add_file_rmap(page, vma, false);
+               add_mm_counter(vma->vm_mm, mm_counter_file(page), nr);
+               folio_add_file_rmap(folio, start, n, vma);
+       }
+
+       for (i = 0; i < nr; i++) {
+               set_pte_at(vma->vm_mm, addr, vmf->pte + i, entry);
+                /* no need to invalidate: a not-present page won't be cached */
+                update_mmu_cache(vma, addr, vmf->pte + i);
+               addr += PAGE_SIZE;
+               entry += PTE_STRIDE;
        }
-       set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
 }
 
 static bool vmf_pte_changed(struct vm_fault *vmf)
#14 lore raw

Re: [RFC PATCH v2 4/5] mm: add do_set_pte_range()

In reply to Matthew Wilcox
Quoted 11 lines · Matthew Wilcox
Quoted 3 lines · Yin Fengwei

do_set_pte_range() allows to setup page table entries for a specific range. It calls page_add_file_rmap_range() to take advantage of batched rmap update for large folio.

How about something more like this? Yes, we need to define flush_icache_pages() and PTE_STRIDE.

(we could also do for (i = 0; i < nr; i++) flush_icache_page(...) but given that some architectures already implement flush_icache_range(), I think they may appreciate being given one large range to flush)

For flush_icache_range() and flush_icache_page(), the implementation on riscv could be exception.

According to arch/riscv/include/asm/cacheflush.h
#define flush_icache_range(start, end) flush_icache_all()

There is no definition for flush_icache_page(). I suppose flush_icache_page() does nothing on riscv.

Using flush_icache_range() may not be good choice for riscv.

Regards
Yin, Fengwei

Quoted 56 lines

+++ b/mm/memory.c

(quoted diff) +6 -2
@@ -4277,15 +4277,19 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page
 *page)
  }
  #endif
 
-void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
+void do_set_pte_range(struct vm_fault *vmf, struct folio *folio,
+               unsigned int start, unsigned int nr,
+               unsigned long addr)
  {
+       struct page *page = folio_page(page, start);
         struct vm_area_struct *vma = vmf->vma;
         bool uffd_wp = pte_marker_uffd_wp(vmf->orig_pte);
         bool write = vmf->flags & FAULT_FLAG_WRITE;
         bool prefault = vmf->address != addr;
         pte_t entry;
+       unsigned int i;
 
-       flush_icache_page(vma, page);
+       flush_icache_pages(vma, page, nr);
         entry = mk_pte(page, vma->vm_page_prot);
 
         if (prefault && arch_wants_old_prefaulted_pte())
(quoted diff) +15 -6
@@ -4299,14 +4303,23 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr)
                 entry = pte_mkuffd_wp(pte_wrprotect(entry));
         /* copy-on-write page */
         if (write && !(vma->vm_flags & VM_SHARED)) {
-               inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
-               page_add_new_anon_rmap(page, vma, addr);
-               lru_cache_add_inactive_or_unevictable(page, vma);
+               add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr);
+               for (i = 0; i < nr; i++) {
+                       page_add_new_anon_rmap(page + i, vma, addr);
+                       lru_cache_add_inactive_or_unevictable(page + i, vma);
+               }
         } else {
-               inc_mm_counter(vma->vm_mm, mm_counter_file(page));
-               page_add_file_rmap(page, vma, false);
+               add_mm_counter(vma->vm_mm, mm_counter_file(page), nr);
+               folio_add_file_rmap(folio, start, n, vma);
+       }
+
+       for (i = 0; i < nr; i++) {
+               set_pte_at(vma->vm_mm, addr, vmf->pte + i, entry);
+                /* no need to invalidate: a not-present page won't be cached */
+                update_mmu_cache(vma, addr, vmf->pte + i);
+               addr += PAGE_SIZE;
+               entry += PTE_STRIDE;
         }
-       set_pte_at(vma->vm_mm, addr, vmf->pte, entry);
  }
 
  static bool vmf_pte_changed(struct vm_fault *vmf)
#15 lore raw

RFC PATCH v2 5/5 filemap: batched update mm counter,rmap when map file folio

In reply to Yin Fengwei

Use do_set_pte_range() in filemap_map_folio_range(). Which batched updates mm counter, rmap.

With a self cooked will-it-scale.page_fault3 like app (change file write fault to read fault) got 15% performance gain.

Perf data collected before/after the change:
18.73%--page_add_file_rmap

          |
           --11.60%--__mod_lruvec_page_state
                     |
                     |--7.40%--__mod_memcg_lruvec_state
                     |          |
                     |           --5.58%--cgroup_rstat_updated
                     |
                      --2.53%--__mod_lruvec_state
                                |
                                 --1.48%--__mod_node_page_state

9.93%--page_add_file_rmap_range

         |
          --2.67%--__mod_lruvec_page_state
                    |
                    |--1.95%--__mod_memcg_lruvec_state
                    |          |
                    |           --1.57%--cgroup_rstat_updated
                    |
                     --0.61%--__mod_lruvec_state
                               |
                                --0.54%--__mod_node_page_state

The running time of __mode_lruvec_page_state() is reduced a lot.

---
 mm/filemap.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)
mm/filemap.c +33 -4
index 95f634d11581..b14f077d1c55 100644
@@ -3364,11 +3364,22 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 	struct file *file = vma->vm_file;
 	struct page *page = folio_page(folio, start);
 	unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
-	unsigned int ref_count = 0, count = 0;
+	unsigned int ref_count = 0, count = 0, nr_mapped = 0;
 
 	do {
-		if (PageHWPoison(page))
+		if (PageHWPoison(page)) {
+			if (nr_mapped) {
+				vmf->pte -= nr_mapped;
+				do_set_pte_range(vmf, folio,
+						start + count - nr_mapped,
+						addr - nr_mapped * PAGE_SIZE,
+						nr_mapped);
+
+			}
+
+			nr_mapped = 0;
 			continue;
+		}
 
 		if (mmap_miss > 0)
 			mmap_miss--;
@@ -3378,16 +3389,34 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 		 * handled in the specific fault path, and it'll prohibit the
 		 * fault-around logic.
 		 */
-		if (!pte_none(*vmf->pte))
+		if (!pte_none(*vmf->pte)) {
+			if (nr_mapped) {
+				vmf->pte -= nr_mapped;
+				do_set_pte_range(vmf, folio,
+						start + count - nr_mapped,
+						addr - nr_mapped * PAGE_SIZE,
+						nr_mapped);
+
+			}
+
+			nr_mapped = 0;
+
 			continue;
+		}
 
 		if (vmf->address == addr)
 			ret = VM_FAULT_NOPAGE;
 
 		ref_count++;
-		do_set_pte(vmf, page, addr);
+		nr_mapped++;
 	} while (vmf->pte++, page++, addr += PAGE_SIZE, ++count < nr_pages);
 
+	if (nr_mapped) {
+		vmf->pte -= nr_mapped;
+		do_set_pte_range(vmf, folio, start + count - nr_mapped,
+				addr - nr_mapped * PAGE_SIZE, nr_mapped);
+	}
+
 	/*
 	 * Restore the vmf->pte. Otherwise, it's possible vmf->pte point
 	 * to next page table entry if the last sub page in the range is
Signature · 1 line
2.30.2
maintainer
#16 lore raw

Re: [RFC PATCH v2 5/5] filemap: batched update mm counter,rmap when map file folio

Quoted 5 lines · Yin Fengwei

Use do_set_pte_range() in filemap_map_folio_range(). Which batched updates mm counter, rmap.

With a self cooked will-it-scale.page_fault3 like app (change file write fault to read fault) got 15% performance gain.

I'd suggest that you create a will-it-scale.page_fault4. Anton is quite open to adding new variations of tests.

Quoted 25 lines

Perf data collected before/after the change:
18.73%--page_add_file_rmap

          |
           --11.60%--__mod_lruvec_page_state
                     |
                     |--7.40%--__mod_memcg_lruvec_state
                     |          |
                     |           --5.58%--cgroup_rstat_updated
                     |
                      --2.53%--__mod_lruvec_state
                                |
                                 --1.48%--__mod_node_page_state

9.93%--page_add_file_rmap_range

         |
          --2.67%--__mod_lruvec_page_state
                    |
                    |--1.95%--__mod_memcg_lruvec_state
                    |          |
                    |           --1.57%--cgroup_rstat_updated
                    |
                     --0.61%--__mod_lruvec_state
                               |
                                --0.54%--__mod_node_page_state
Quoted 1 line

The running time of __mode_lruvec_page_state() is reduced a lot.

Nice.

Quoted 13 lines

+++ b/mm/filemap.c

mm/filemap.c +4 -2
@@ -3364,11 +3364,22 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 	struct file *file = vma->vm_file;
 	struct page *page = folio_page(folio, start);
 	unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
-	unsigned int ref_count = 0, count = 0;
+	unsigned int ref_count = 0, count = 0, nr_mapped = 0;
 
 	do {
-		if (PageHWPoison(page))
+		if (PageHWPoison(page)) {
+			if (nr_mapped) {
+				vmf->pte -= nr_mapped;

mm/filemap.c static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, · line 3382

Quoted 10 lines
mm/filemap.c +9 -0
+				do_set_pte_range(vmf, folio,
+						start + count - nr_mapped,
+						addr - nr_mapped * PAGE_SIZE,
+						nr_mapped);
+
+			}
+
+			nr_mapped = 0;
 			continue;
+		}

Having subtracted nr_mapped from vmf->pte, we then need to add it again.

But this is all too complicated. What if we don't update vmf->pte each time around the loop? ie something like this:

	do {
		if (PageHWPoisoned(page))
			goto map;
 		if (mmap_miss > 0)
 			mmap_miss--;
		/*
		 * If there're PTE markers, we'll leave them to be handled
		 * in the specific fault path, and it'll prohibit the
		 * fault-around logic.
		 */
		if (!pte_none(vmf->pte[count]))
			goto map;
 		if (vmf->address == addr + count * PAGE_SIZE)
 			ret = VM_FAULT_NOPAGE;
		continue;
map:
		if (count > 1) {
			do_set_pte_range(vmf, folio, start, addr, count - 1);
			folio_ref_add(folio, count - 1);
		}
		start += count;
		vmf->pte += count;
		addr += count * PAGE_SIZE;
		nr_pages -= count;
		count = 0;
	} while (page++, ++count < nr_pages);

	if (count > 0) {
		do_set_pte_range(vmf, folio, start, addr, count);
		folio_ref_add(folio, count);
	} else {
		/* Make sure the PTE points to the correct page table */
		vmf->pte--;
	}
#17 lore raw

Re: [RFC PATCH v2 5/5] filemap: batched update mm counter,rmap when map file folio

Quoted 9 lines · Matthew Wilcox
Quoted 5 lines · Yin Fengwei

Use do_set_pte_range() in filemap_map_folio_range(). Which batched updates mm counter, rmap.

With a self cooked will-it-scale.page_fault3 like app (change file write fault to read fault) got 15% performance gain.

I'd suggest that you create a will-it-scale.page_fault4. Anton is quite open to adding new variations of tests.

OK. I will do this.

Quoted 56 lines
Quoted 26 lines

Perf data collected before/after the change:
18.73%--page_add_file_rmap

          |
           --11.60%--__mod_lruvec_page_state
                     |
                     |--7.40%--__mod_memcg_lruvec_state
                     |          |
                     |           --5.58%--cgroup_rstat_updated
                     |
                      --2.53%--__mod_lruvec_state
                                |
                                 --1.48%--__mod_node_page_state

9.93%--page_add_file_rmap_range

         |
          --2.67%--__mod_lruvec_page_state
                    |
                    |--1.95%--__mod_memcg_lruvec_state
                    |          |
                    |           --1.57%--cgroup_rstat_updated
                    |
                     --0.61%--__mod_lruvec_state
                               |
                                --0.54%--__mod_node_page_state
Quoted 1 line

The running time of __mode_lruvec_page_state() is reduced a lot.

Nice.

Quoted 13 lines

+++ b/mm/filemap.c

mm/filemap.c +4 -2
@@ -3364,11 +3364,22 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 	struct file *file = vma->vm_file;
 	struct page *page = folio_page(folio, start);
 	unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
-	unsigned int ref_count = 0, count = 0;
+	unsigned int ref_count = 0, count = 0, nr_mapped = 0;
 
 	do {
-		if (PageHWPoison(page))
+		if (PageHWPoison(page)) {
+			if (nr_mapped) {
+				vmf->pte -= nr_mapped;

mm/filemap.c static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, · line 3382

Quoted 10 lines
mm/filemap.c +9 -0
+				do_set_pte_range(vmf, folio,
+						start + count - nr_mapped,
+						addr - nr_mapped * PAGE_SIZE,
+						nr_mapped);
+
+			}
+
+			nr_mapped = 0;
 			continue;
+		}

Having subtracted nr_mapped from vmf->pte, we then need to add it again.

Quoted 2 lines

But this is all too complicated. What if we don't update vmf->pte each time around the loop? ie something like this:

Thanks a lot for the suggestion. I like vmf->pte[count] a lot. Will update the code accordingly in next version.

Regards
Yin, Fengwei

Quoted 35 lines
	do {
		if (PageHWPoisoned(page))
			goto map;
mm/filemap.c +0 -0
 		if (mmap_miss > 0)
 			mmap_miss--;
		/*
		 * If there're PTE markers, we'll leave them to be handled
		 * in the specific fault path, and it'll prohibit the
		 * fault-around logic.
		 */
		if (!pte_none(vmf->pte[count]))
			goto map;
(quoted diff) +0 -0
 		if (vmf->address == addr + count * PAGE_SIZE)
 			ret = VM_FAULT_NOPAGE;
		continue;
map:
		if (count > 1) {
			do_set_pte_range(vmf, folio, start, addr, count - 1);
			folio_ref_add(folio, count - 1);
		}
		start += count;
		vmf->pte += count;
		addr += count * PAGE_SIZE;
		nr_pages -= count;
		count = 0;
	} while (page++, ++count < nr_pages);

	if (count > 0) {
		do_set_pte_range(vmf, folio, start, addr, count);
		folio_ref_add(folio, count);
	} else {
		/* Make sure the PTE points to the correct page table */
		vmf->pte--;
	}
#18 lore raw

Re: [RFC PATCH v2 5/5] filemap: batched update mm counter,rmap when map file folio

In reply to Matthew Wilcox
Quoted 85 lines · Matthew Wilcox
Quoted 5 lines · Yin Fengwei

Use do_set_pte_range() in filemap_map_folio_range(). Which batched updates mm counter, rmap.

With a self cooked will-it-scale.page_fault3 like app (change file write fault to read fault) got 15% performance gain.

I'd suggest that you create a will-it-scale.page_fault4. Anton is quite open to adding new variations of tests.

Quoted 25 lines

Perf data collected before/after the change:
18.73%--page_add_file_rmap

          |
           --11.60%--__mod_lruvec_page_state
                     |
                     |--7.40%--__mod_memcg_lruvec_state
                     |          |
                     |           --5.58%--cgroup_rstat_updated
                     |
                      --2.53%--__mod_lruvec_state
                                |
                                 --1.48%--__mod_node_page_state

9.93%--page_add_file_rmap_range

         |
          --2.67%--__mod_lruvec_page_state
                    |
                    |--1.95%--__mod_memcg_lruvec_state
                    |          |
                    |           --1.57%--cgroup_rstat_updated
                    |
                     --0.61%--__mod_lruvec_state
                               |
                                --0.54%--__mod_node_page_state
Quoted 1 line

The running time of __mode_lruvec_page_state() is reduced a lot.

Nice.

Quoted 13 lines

+++ b/mm/filemap.c

mm/filemap.c +4 -2
@@ -3364,11 +3364,22 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 	struct file *file = vma->vm_file;
 	struct page *page = folio_page(folio, start);
 	unsigned int mmap_miss = READ_ONCE(file->f_ra.mmap_miss);
-	unsigned int ref_count = 0, count = 0;
+	unsigned int ref_count = 0, count = 0, nr_mapped = 0;
 
 	do {
-		if (PageHWPoison(page))
+		if (PageHWPoison(page)) {
+			if (nr_mapped) {
+				vmf->pte -= nr_mapped;

mm/filemap.c static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, · line 3382

Quoted 10 lines
mm/filemap.c +9 -0
+				do_set_pte_range(vmf, folio,
+						start + count - nr_mapped,
+						addr - nr_mapped * PAGE_SIZE,
+						nr_mapped);
+
+			}
+
+			nr_mapped = 0;
 			continue;
+		}

Having subtracted nr_mapped from vmf->pte, we then need to add it again.

But this is all too complicated. What if we don't update vmf->pte each time around the loop? ie something like this:

	do {
		if (PageHWPoisoned(page))
			goto map;
mm/filemap.c +0 -0
 		if (mmap_miss > 0)
 			mmap_miss--;
		/*
		 * If there're PTE markers, we'll leave them to be handled
		 * in the specific fault path, and it'll prohibit the
		 * fault-around logic.
		 */
		if (!pte_none(vmf->pte[count]))
			goto map;
(quoted diff) +0 -0
 		if (vmf->address == addr + count * PAGE_SIZE)
 			ret = VM_FAULT_NOPAGE;
		continue;
map:
		if (count > 1) {
Quoted 8 lines
			do_set_pte_range(vmf, folio, start, addr, count - 1);
			folio_ref_add(folio, count - 1);
		}
		start += count;
		vmf->pte += count;
		addr += count * PAGE_SIZE;
		nr_pages -= count;
		count = 0;

I found it's not easy to make this part correct. So I tried to simplify the logic here a little bit.

Regards
Yin, Fengwei

Quoted 9 lines
	} while (page++, ++count < nr_pages);

	if (count > 0) {
		do_set_pte_range(vmf, folio, start, addr, count);
		folio_ref_add(folio, count);
	} else {
		/* Make sure the PTE points to the correct page table */
		vmf->pte--;
	}