filemap: optimize order0 folio in filemap_map_pages
Subsystems (2)
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
@@ -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
Matthew Wilcox
maintainerQuoted 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 bynext_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
Signed-off-by Jinjiang Tu <[email protected]>--- mm/filemap.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)mm/filemap.c +9
-6@@ -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
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 bynext_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
Signed-off-by Jinjiang Tu<[email protected]>--- mm/filemap.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)mm/filemap.c +9
-6@@ -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
Matthew Wilcox
maintainerQuoted 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 bynext_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()callWe should call
folio_unlock()beforefolio_put(). Infilemap_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()?
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 bynext_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()callWe should call
folio_unlock()beforefolio_put(). Infilemap_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 beforefolio_put(). What I don't understand is why we need to delayfolio_unlock()until right beforefolio_put(). Can't we leavefolio_unlock()where it currently is and only move thefolio_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.
Matthew Wilcox
maintainerQuoted 4 lines · Jinjiang Tu
Quoted 3 lines · Matthew Wilcox
Quoted 2 lines · Jinjiang Tu
We should call
folio_unlock()beforefolio_put(). Infilemap_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 beforefolio_put(). What I don't understand is why we need to delayfolio_unlock()until right beforefolio_put(). Can't we leavefolio_unlock()where it currently is and only move thefolio_put()?In
filemap_map_order0_folio(), assuming the page is hwpoisoned, we skipset_pte_range(), the folio should be unlocked and put. If we only movefolio_put()tofilemap_map_order0_folio(), the folio is unlocked whenfilemap_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.
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()beforefolio_put(). Infilemap_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 beforefolio_put(). What I don't understand is why we need to delayfolio_unlock()until right beforefolio_put(). Can't we leavefolio_unlock()where it currently is and only move thefolio_put()?In
filemap_map_order0_folio(), assuming the page is hwpoisoned, we skipset_pte_range(), the folio should be unlocked and put. If we only movefolio_put()tofilemap_map_order0_folio(), the folio is unlocked whenfilemap_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 thefolio_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 thefolio_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
Matthew Wilcox
maintainerQuoted 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()beforefolio_put(). Infilemap_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 beforefolio_put(). What I don't understand is why we need to delayfolio_unlock()until right beforefolio_put(). Can't we leavefolio_unlock()where it currently is and only move thefolio_put()?In
filemap_map_order0_folio(), assuming the page is hwpoisoned, we skipset_pte_range(), the folio should be unlocked and put. If we only movefolio_put()tofilemap_map_order0_folio(), the folio is unlocked whenfilemap_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 thefolio_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 thefolio_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.
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()beforefolio_put(). Infilemap_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 beforefolio_put(). What I don't understand is why we need to delayfolio_unlock()until right beforefolio_put(). Can't we leavefolio_unlock()where it currently is and only move thefolio_put()?In
filemap_map_order0_folio(), assuming the page is hwpoisoned, we skipset_pte_range(), the folio should be unlocked and put. If we only movefolio_put()tofilemap_map_order0_folio(), the folio is unlocked whenfilemap_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 thefolio_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 thefolio_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
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()beforefolio_put(). Infilemap_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 beforefolio_put(). What I don't understand is why we need to delayfolio_unlock()until right before folio_put(). Can't we leavefolio_unlock()where it currently is and only move thefolio_put()?In
filemap_map_order0_folio(), assuming the page is hwpoisoned, we skipset_pte_range(), the folio should be unlocked and put. If we only movefolio_put()tofilemap_map_order0_folio(), the folio is unlocked whenfilemap_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 thefolio_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?
Matthew Wilcox
maintainerQuoted 1 line · Kefeng Wang
So, Andrew, could you help to pick this up?
NAK. Send a version that does not move the folio_unlock().
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.
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()beforefolio_put(). Infilemap_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 beforefolio_put(). What I don't understand is why we need to delayfolio_unlock()until right beforefolio_put(). Can't we leavefolio_unlock()where it currently is and only move thefolio_put()?In
filemap_map_order0_folio(), assuming the page is hwpoisoned, we skipset_pte_range(), the folio should be unlocked and put. If we only movefolio_put()tofilemap_map_order0_folio(), the folio is unlocked whenfilemap_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 thefolio_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 thefolio_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?