Skip to contentSkip to conversation

filemap: optimize order0 folio in filemap_map_pages

PATCH Superseded Jinjiang Tu 4 participants 13 messages Aug 19, 2025 – Sep 3, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 filemap: optimize order0 folio in filemap_map_pages

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.

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

---
 mm/filemap.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
mm/filemap.c +9 -6
index 751838ef05e5..5de52deab138 100644
@@ -3693,6 +3693,8 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 	}
 
 	vmf->pte = old_ptep;
+	folio_unlock(folio);
+	folio_put(folio);
 
 	return ret;
 }
@@ -3705,7 +3707,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 +3719,19 @@ 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);
+	folio_unlock(folio);
+	return ret;
 
+out:
+	folio_unlock(folio);
+	folio_put(folio);
 	return ret;
 }
 
@@ -3783,9 +3789,6 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 			ret |= filemap_map_folio_range(vmf, folio,
 					xas.xa_index - folio->index, addr,
 					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);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 8 lines · Jinjiang Tu

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.

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

You don't explain why you move the folio_unlock() call

Quoted 63 lines
---
 mm/filemap.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
mm/filemap.c +9 -6
index 751838ef05e5..5de52deab138 100644
@@ -3693,6 +3693,8 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
 	}
 
 	vmf->pte = old_ptep;
+	folio_unlock(folio);
+	folio_put(folio);
 
 	return ret;
 }
@@ -3705,7 +3707,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 +3719,19 @@ 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);
+	folio_unlock(folio);
+	return ret;
 
+out:
+	folio_unlock(folio);
+	folio_put(folio);
 	return ret;
 }
 
@@ -3783,9 +3789,6 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
 			ret |= filemap_map_folio_range(vmf, folio,
 					xas.xa_index - folio->index, addr,
 					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);
Signature · 1 line
2.43.0
#2 lore raw
Quoted 10 lines · Matthew Wilcox
Quoted 8 lines · Jinjiang Tu

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.

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

You don't explain why you move the folio_unlock() call

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

Quoted 64 lines
Quoted 64 lines
---
  mm/filemap.c | 15 +++++++++------
  1 file changed, 9 insertions(+), 6 deletions(-)
mm/filemap.c +9 -6
index 751838ef05e5..5de52deab138 100644
@@ -3693,6 +3693,8 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,
  	}
 
  	vmf->pte = old_ptep;
+	folio_unlock(folio);
+	folio_put(folio);
 
  	return ret;
  }
@@ -3705,7 +3707,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 +3719,19 @@ 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);
+	folio_unlock(folio);
+	return ret;
 
+out:
+	folio_unlock(folio);
+	folio_put(folio);
  	return ret;
  }
 
@@ -3783,9 +3789,6 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,
  			ret |= filemap_map_folio_range(vmf, folio,
  					xas.xa_index - folio->index, addr,
  					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);
Signature · 1 line
2.43.0
maintainer
#3 lore raw
Quoted 9 lines · Jinjiang Tu
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Jinjiang Tu

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.

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

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

You don't explain why you move the folio_unlock() call

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put(). Can't we leave folio_unlock() where it currently is and only move the folio_put()?

#4 lore raw
Quoted 11 lines · Matthew Wilcox
Quoted 10 lines · Jinjiang Tu
Quoted 9 lines · Matthew Wilcox
Quoted 8 lines · Jinjiang Tu

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.

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

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

You don't explain why you move the folio_unlock() call

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put(). Can't we leave folio_unlock() where it currently is and only move the folio_put()?

In filemap_map_order0_folio(), assuming the page is hwpoisoned, we skip set_pte_range(), the folio should be unlocked and put. If we only move folio_put() to filemap_map_order0_folio(), the folio is unlocked when filemap_map_pages() doesn't hold any folio refcount.

maintainer
#5 lore raw
Quoted 4 lines · Jinjiang Tu
Quoted 3 lines · Matthew Wilcox
Quoted 2 lines · Jinjiang Tu

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

Quoted 8 lines
Quoted 4 lines

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put(). Can't we leave folio_unlock() where it currently is and only move the folio_put()?

In filemap_map_order0_folio(), assuming the page is hwpoisoned, we skip set_pte_range(), the folio should be unlocked and put. If we only move folio_put() to filemap_map_order0_folio(), the folio is unlocked when filemap_map_pages() doesn't hold any folio refcount.

Oh, I see. I misread your patch; sorry about that.

However, it is still safe to move only the folio_put() and not move the folio_unlock()! It's a little subtle, so I'll explain.

We must not free a locked folio. The page allocator has checks for this and will complain (assuming appropriate debug options are enabled). So this sequence:

	folio_put(folio);
	folio_unlock(folio);

is _generally_ unsafe because the folio_put() might be the last put of the refcount which will cause the folio to be freed. However, if we know that the folio has a refcount > 1, it's safe because the folio_put() won't free the folio. We do know that the folio has a refcount >1 because it's in the page cache, which keeps a refcount on the folio. Since we have it locked, we know that truncation will wait for the unlock to happen, and truncation will be the last one to put the refcount.

#6 lore raw
Quoted 25 lines · Matthew Wilcox
Quoted 4 lines · Jinjiang Tu
Quoted 3 lines · Matthew Wilcox
Quoted 2 lines · Jinjiang Tu

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

Quoted 8 lines
Quoted 4 lines

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put(). Can't we leave folio_unlock() where it currently is and only move the folio_put()?

In filemap_map_order0_folio(), assuming the page is hwpoisoned, we skip set_pte_range(), the folio should be unlocked and put. If we only move folio_put() to filemap_map_order0_folio(), the folio is unlocked when filemap_map_pages() doesn't hold any folio refcount.

Oh, I see. I misread your patch; sorry about that.

However, it is still safe to move only the folio_put() and not move the folio_unlock()! It's a little subtle, so I'll explain.

We must not free a locked folio. The page allocator has checks for this and will complain (assuming appropriate debug options are enabled). So this sequence:

	folio_put(folio);
	folio_unlock(folio);
Quoted 7 lines

is _generally_ unsafe because the folio_put() might be the last put of the refcount which will cause the folio to be freed. However, if we know that the folio has a refcount > 1, it's safe because the folio_put() won't free the folio. We do know that the folio has a refcount >1 because it's in the page cache, which keeps a refcount on the folio. Since we have it locked, we know that truncation will wait for the unlock to happen, and truncation will be the last one to put the refcount.

I agree that it is save, but is it worth it having that subtle detail here instead of just doing unlock+put?

IOW, what do we gain by doing it differently? :)

Signature · 3 lines
Cheers

David / dhildenb
maintainer
#7 lore raw
Quoted 34 lines · David Hildenbrand
Quoted 25 lines · Matthew Wilcox
Quoted 4 lines · Jinjiang Tu
Quoted 3 lines · Matthew Wilcox
Quoted 2 lines · Jinjiang Tu

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

Quoted 8 lines
Quoted 4 lines

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put(). Can't we leave folio_unlock() where it currently is and only move the folio_put()?

In filemap_map_order0_folio(), assuming the page is hwpoisoned, we skip set_pte_range(), the folio should be unlocked and put. If we only move folio_put() to filemap_map_order0_folio(), the folio is unlocked when filemap_map_pages() doesn't hold any folio refcount.

Oh, I see. I misread your patch; sorry about that.

However, it is still safe to move only the folio_put() and not move the folio_unlock()! It's a little subtle, so I'll explain.

We must not free a locked folio. The page allocator has checks for this and will complain (assuming appropriate debug options are enabled). So this sequence:

	folio_put(folio);
	folio_unlock(folio);
Quoted 7 lines

is _generally_ unsafe because the folio_put() might be the last put of the refcount which will cause the folio to be freed. However, if we know that the folio has a refcount > 1, it's safe because the folio_put() won't free the folio. We do know that the folio has a refcount >1 because it's in the page cache, which keeps a refcount on the folio. Since we have it locked, we know that truncation will wait for the unlock to happen, and truncation will be the last one to put the refcount.

Quoted 4 lines

I agree that it is save, but is it worth it having that subtle detail here instead of just doing unlock+put?

IOW, what do we gain by doing it differently? :)

That was in the initial mail:

Quoted 2 lines

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

Obviously the exact gains are going to depend on how good your CPU is at doing atomic inc/decs, but reducing the number of atomics is always a good thing.

#8 lore raw
Quoted 40 lines · Matthew Wilcox
Quoted 34 lines · David Hildenbrand
Quoted 25 lines · Matthew Wilcox
Quoted 4 lines · Jinjiang Tu
Quoted 3 lines · Matthew Wilcox
Quoted 2 lines · Jinjiang Tu

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

Quoted 8 lines
Quoted 4 lines

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put(). Can't we leave folio_unlock() where it currently is and only move the folio_put()?

In filemap_map_order0_folio(), assuming the page is hwpoisoned, we skip set_pte_range(), the folio should be unlocked and put. If we only move folio_put() to filemap_map_order0_folio(), the folio is unlocked when filemap_map_pages() doesn't hold any folio refcount.

Oh, I see. I misread your patch; sorry about that.

However, it is still safe to move only the folio_put() and not move the folio_unlock()! It's a little subtle, so I'll explain.

We must not free a locked folio. The page allocator has checks for this and will complain (assuming appropriate debug options are enabled). So this sequence:

	folio_put(folio);
	folio_unlock(folio);
Quoted 7 lines

is _generally_ unsafe because the folio_put() might be the last put of the refcount which will cause the folio to be freed. However, if we know that the folio has a refcount > 1, it's safe because the folio_put() won't free the folio. We do know that the folio has a refcount >1 because it's in the page cache, which keeps a refcount on the folio. Since we have it locked, we know that truncation will wait for the unlock to happen, and truncation will be the last one to put the refcount.

Quoted 4 lines

I agree that it is save, but is it worth it having that subtle detail here instead of just doing unlock+put?

IOW, what do we gain by doing it differently? :)

Quoted 8 lines

That was in the initial mail:

Quoted 2 lines

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

Obviously the exact gains are going to depend on how good your CPU is at doing atomic inc/decs, but reducing the number of atomics is always a good thing.

Ah, yes, obviously. I misread your mail, assuming you would argue for a further change.

Signature · 3 lines
Cheers

David / dhildenb
#9 lore raw
Quoted 43 lines · David Hildenbrand
Quoted 42 lines · Matthew Wilcox
Quoted 34 lines · David Hildenbrand
Quoted 25 lines · Matthew Wilcox
Quoted 4 lines · Jinjiang Tu
Quoted 3 lines · Matthew Wilcox
Quoted 2 lines · Jinjiang Tu

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

Quoted 8 lines
Quoted 4 lines

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put().  Can't we leave folio_unlock() where it currently is and only move the folio_put()?

In filemap_map_order0_folio(), assuming the page is hwpoisoned, we skip set_pte_range(), the folio should be unlocked and put. If we only move folio_put() to filemap_map_order0_folio(), the folio is unlocked when filemap_map_pages() doesn't hold any folio refcount.

Oh, I see.  I misread your patch; sorry about that.

However, it is still safe to move only the folio_put() and not move the folio_unlock()!  It's a little subtle, so I'll explain.

We must not free a locked folio.  The page allocator has checks for this and will complain (assuming appropriate debug options are enabled).  So this sequence:

    folio_put(folio);
    folio_unlock(folio);

Quoted 7 lines

is _generally_ unsafe because the folio_put() might be the last put of the refcount which will cause the folio to be freed.  However, if we know that the folio has a refcount > 1, it's safe because the folio_put() won't free the folio.  We do know that the folio has a refcount >1 because it's in the page cache, which keeps a refcount on the folio. Since we have it locked, we know that truncation will wait for the unlock to happen, and truncation will be the last one to put the refcount.

Quoted 4 lines

I agree that it is save, but is it worth it having that subtle detail here instead of just doing unlock+put?

IOW, what do we gain by doing it differently? :)

That was in the initial mail:

Quoted 9 lines
Quoted 6 lines
Quoted 2 lines

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

Obviously the exact gains are going to depend on how good your CPU is at doing atomic inc/decs, but reducing the number of atomics is always a good thing.

Ah, yes, obviously. I misread your mail, assuming you would argue for a further change.

So, Andrew, could you help to pick this up?

maintainer
#10 lore raw
Quoted 1 line · Kefeng Wang

So, Andrew, could you help to pick this up?

NAK. Send a version that does not move the folio_unlock().

#11 lore raw
Quoted 5 lines · Matthew Wilcox
Quoted 1 line · Kefeng Wang

So, Andrew, could you help to pick this up?

NAK. Send a version that does not move the folio_unlock().

OK, Jinjian, please update.

#12 lore raw
In reply to Matthew Wilcox
Quoted 24 lines · Matthew Wilcox
Quoted 11 lines · Jinjiang Tu
Quoted 7 lines · Matthew Wilcox
Quoted 2 lines · Jinjiang Tu

We should call folio_unlock() before folio_put(). In filemap_map_order0_folio(), if we doesn't set folio into pte, we should unlock and put folio.

I agree that folio_unlock() needs to be called before folio_put(). What I don't understand is why we need to delay folio_unlock() until right before folio_put(). Can't we leave folio_unlock() where it currently is and only move the folio_put()?

In filemap_map_order0_folio(), assuming the page is hwpoisoned, we skip set_pte_range(), the folio should be unlocked and put. If we only move folio_put() to filemap_map_order0_folio(), the folio is unlocked when filemap_map_pages() doesn't hold any folio refcount.

Oh, I see. I misread your patch; sorry about that.

However, it is still safe to move only the folio_put() and not move the folio_unlock()! It's a little subtle, so I'll explain.

We must not free a locked folio. The page allocator has checks for this and will complain (assuming appropriate debug options are enabled). So this sequence:

	folio_put(folio);
	folio_unlock(folio);
Quoted 7 lines

is _generally_ unsafe because the folio_put() might be the last put of the refcount which will cause the folio to be freed. However, if we know that the folio has a refcount > 1, it's safe because the folio_put() won't free the folio. We do know that the folio has a refcount >1 because it's in the page cache, which keeps a refcount on the folio. Since we have it locked, we know that truncation will wait for the unlock to happen, and truncation will be the last one to put the refcount.

Ah, I see. Thanks for explaining this.

The performance gain is from decreasing atomic operations of folio's refcount, is irrelevant to folio_unlock() call. So maybe it's no need to introduce this sublte change?