linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] filemap: optimize order0 folio in filemap_map_pages
@ 2025-09-03  8:42 Jinjiang Tu
  2025-09-03  9:16 ` David Hildenbrand
  0 siblings, 1 reply; 6+ messages in thread
From: Jinjiang Tu @ 2025-09-03  8:42 UTC (permalink / raw)
  To: willy, akpm, david, linux-mm; +Cc: wangkefeng.wang, tujinjiang

There are two meaningless folio refcount update for order0 folio in
filemap_map_pages(). First, filemap_map_order0_folio() adds folio refcount
after the folio is mapped to pte. And then, filemap_map_pages() drops a
refcount grabbed by next_uptodate_folio(). We could remain the refcount
unchanged in this case.

As Matthew metenioned in [1], it is safe to call folio_unlock() before
calling folio_put() here, because the folio is in page cache with refcount
held, and truncation will wait for the unlock.

With this patch, we can get 8% performance gain for lmbench testcase
'lat_pagefault -P 1 file', the size of file is 512M.

[1]: https://lore.kernel.org/all/aKcU-fzxeW3xT5Wv@casper.infradead.org/

Signed-off-by: Jinjiang Tu <tujinjiang@huawei.com>
---

v2:
 * Don't move folio_unlock(), suggested by Matthew.

 mm/filemap.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/mm/filemap.c b/mm/filemap.c
index 751838ef05e5..3da8bf8b93ec 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -3693,6 +3693,7 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 	}
 
 	vmf->pte = old_ptep;
+	folio_put(folio);
 
 	return ret;
 }
@@ -3705,7 +3706,7 @@ static vm_fault_t filemap_map_order0_folio(struct vm_fault *vmf,
 	struct page *page = &folio->page;
 
 	if (PageHWPoison(page))
-		return ret;
+		goto out;
 
 	/* See comment of filemap_map_folio_range() */
 	if (!folio_test_workingset(folio))
@@ -3717,15 +3718,17 @@ static vm_fault_t filemap_map_order0_folio(struct vm_fault *vmf,
 	 * the fault-around logic.
 	 */
 	if (!pte_none(ptep_get(vmf->pte)))
-		return ret;
+		goto out;
 
 	if (vmf->address == addr)
 		ret = VM_FAULT_NOPAGE;
 
 	set_pte_range(vmf, folio, page, 1, addr);
 	(*rss)++;
-	folio_ref_inc(folio);
+	return ret;
 
+out:
+	folio_put(folio);
 	return ret;
 }
 
@@ -3785,7 +3788,6 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 					nr_pages, &rss, &mmap_miss);
 
 		folio_unlock(folio);
-		folio_put(folio);
 	} while ((folio = next_uptodate_folio(&xas, mapping, end_pgoff)) != NULL);
 	add_mm_counter(vma->vm_mm, folio_type, rss);
 	pte_unmap_unlock(vmf->pte, vmf->ptl);
-- 
2.43.0



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-09-04 12:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-03  8:42 [PATCH v2] filemap: optimize order0 folio in filemap_map_pages Jinjiang Tu
2025-09-03  9:16 ` David Hildenbrand
2025-09-04  1:05   ` Jinjiang Tu
2025-09-04  1:06     ` Jinjiang Tu
2025-09-04  6:20       ` David Hildenbrand
2025-09-04 12:12         ` Jinjiang Tu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox