linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/rmap: optimize folio_move_anon_rmap()
@ 2024-05-12 12:35 Chen Taotao
  2024-05-13 15:37 ` David Hildenbrand
  0 siblings, 1 reply; 3+ messages in thread
From: Chen Taotao @ 2024-05-12 12:35 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel

When a folio belongs exclusively to one process after a COW event,
folio_move_anon_rmap() always moves the folio into the anon_vma
belongs only to this process.

However, if the folio already belongs to the anon_vma of the this
process, we don't need to move it again. In this case, we first
check if the folio already belongs to the anna_vma of the this
process, and only move it if it does not.

The above changes may improve the performance of vm faults in some
scenarios, because the performance loss caused by WRITE_ONCE() is
much more than the performance loss caused by add a judgment.

Signed-off-by: Chen Taotao <chentt10@chinatelecom.cn>
---
 mm/rmap.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/mm/rmap.c b/mm/rmap.c
index 3746a5531018..5720cb78162d 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1216,12 +1216,15 @@ void folio_move_anon_rmap(struct folio *folio, struct vm_area_struct *vma)
 	VM_BUG_ON_VMA(!anon_vma, vma);
 
 	anon_vma += PAGE_MAPPING_ANON;
+
 	/*
-	 * Ensure that anon_vma and the PAGE_MAPPING_ANON bit are written
-	 * simultaneously, so a concurrent reader (eg folio_referenced()'s
-	 * folio_test_anon()) will not see one without the other.
+	 * If anon_vma != folio->mapping ensure that anon_vma and the
+	 * PAGE_MAPPING_ANON bit are writtensimultaneously, so a concurrent
+	 * reader (eg folio_referenced()'s folio_test_anon()) will not see
+	 * one without the other.
 	 */
-	WRITE_ONCE(folio->mapping, anon_vma);
+	if (anon_vma != folio->mapping)
+		WRITE_ONCE(folio->mapping, anon_vma);
 }
 
 /**
-- 
2.27.0



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

* Re: [PATCH] mm/rmap: optimize folio_move_anon_rmap()
  2024-05-12 12:35 [PATCH] mm/rmap: optimize folio_move_anon_rmap() Chen Taotao
@ 2024-05-13 15:37 ` David Hildenbrand
  2024-05-14  3:25   ` Anshuman Khandual
  0 siblings, 1 reply; 3+ messages in thread
From: David Hildenbrand @ 2024-05-13 15:37 UTC (permalink / raw)
  To: Chen Taotao, akpm; +Cc: linux-mm, linux-kernel

On 12.05.24 14:35, Chen Taotao wrote:
> When a folio belongs exclusively to one process after a COW event,
> folio_move_anon_rmap() always moves the folio into the anon_vma
> belongs only to this process.
> 
> However, if the folio already belongs to the anon_vma of the this
> process, we don't need to move it again. In this case, we first
> check if the folio already belongs to the anna_vma of the this
> process, and only move it if it does not.
> 
> The above changes may improve the performance of vm faults in some
> scenarios, because the performance loss caused by WRITE_ONCE() is
> much more than the performance loss caused by add a judgment.

Please proof that by real numbers. I don't think it will make a real 
difference, and we likely don't want that change.


-- 
Cheers,

David / dhildenb



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

* Re: [PATCH] mm/rmap: optimize folio_move_anon_rmap()
  2024-05-13 15:37 ` David Hildenbrand
@ 2024-05-14  3:25   ` Anshuman Khandual
  0 siblings, 0 replies; 3+ messages in thread
From: Anshuman Khandual @ 2024-05-14  3:25 UTC (permalink / raw)
  To: David Hildenbrand, Chen Taotao, akpm; +Cc: linux-mm, linux-kernel



On 5/13/24 21:07, David Hildenbrand wrote:
> On 12.05.24 14:35, Chen Taotao wrote:
>> When a folio belongs exclusively to one process after a COW event,
>> folio_move_anon_rmap() always moves the folio into the anon_vma
>> belongs only to this process.
>>
>> However, if the folio already belongs to the anon_vma of the this
>> process, we don't need to move it again. In this case, we first
>> check if the folio already belongs to the anna_vma of the this
>> process, and only move it if it does not.
>>
>> The above changes may improve the performance of vm faults in some
>> scenarios, because the performance loss caused by WRITE_ONCE() is
>> much more than the performance loss caused by add a judgment.
> 
> Please proof that by real numbers. I don't think it will make a real difference, and we likely don't want that change.

Agreed, only scenarios when pre-condition check makes any sense is
if subsequent actions are expensive such as writing into registers
etc. But in this case both 'if' and 'WRITE_ONCE' statements are of
comparable cost, hence adding additional conditional check is only
going to increase the cost on average i.e when both gets executed.


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

end of thread, other threads:[~2024-05-14  3:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-12 12:35 [PATCH] mm/rmap: optimize folio_move_anon_rmap() Chen Taotao
2024-05-13 15:37 ` David Hildenbrand
2024-05-14  3:25   ` Anshuman Khandual

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