diff mbox series

[v2] mm: swap: reuse exclusive folio directly instead of wp page faults

Message ID 20240602004502.26895-1-21cnbao@gmail.com (mailing list archive)
State New
Headers show
Series [v2] mm: swap: reuse exclusive folio directly instead of wp page faults | expand

Commit Message

Barry Song June 2, 2024, 12:45 a.m. UTC
From: Barry Song <v-songbaohua@oppo.com>

After swapping out, we perform a swap-in operation. If we first read
and then write, we encounter a major fault in do_swap_page for reading,
along with additional minor faults in do_wp_page for writing. However,
the latter appears to be unnecessary and inefficient. Instead, we can
directly reuse in do_swap_page and completely eliminate the need for
do_wp_page.

This patch achieves that optimization specifically for exclusive folios.
The following microbenchmark demonstrates the significant reduction in
minor faults.

 #define DATA_SIZE (2UL * 1024 * 1024)
 #define PAGE_SIZE (4UL * 1024)

 static void *read_write_data(char *addr)
 {
         char tmp;

         for (int i = 0; i < DATA_SIZE; i += PAGE_SIZE) {
                 tmp = *(volatile char *)(addr + i);
                 *(volatile char *)(addr + i) = tmp;
         }
 }

 int main(int argc, char **argv)
 {
         struct rusage ru;

         char *addr = mmap(NULL, DATA_SIZE, PROT_READ | PROT_WRITE,
                         MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
         memset(addr, 0x11, DATA_SIZE);

         do {
                 long old_ru_minflt, old_ru_majflt;
                 long new_ru_minflt, new_ru_majflt;

                 madvise(addr, DATA_SIZE, MADV_PAGEOUT);

                 getrusage(RUSAGE_SELF, &ru);
                 old_ru_minflt = ru.ru_minflt;
                 old_ru_majflt = ru.ru_majflt;

                 read_write_data(addr);
                 getrusage(RUSAGE_SELF, &ru);
                 new_ru_minflt = ru.ru_minflt;
                 new_ru_majflt = ru.ru_majflt;

                 printf("minor faults:%ld major faults:%ld\n",
                         new_ru_minflt - old_ru_minflt,
                         new_ru_majflt - old_ru_majflt);
         } while(0);

         return 0;
 }

w/o patch,
/ # ~/a.out
minor faults:512 major faults:512

w/ patch,
/ # ~/a.out
minor faults:0 major faults:512

Minor faults decrease to 0!

Signed-off-by: Barry Song <v-songbaohua@oppo.com>
---
 -v2:
 * don't set the dirty flag for read fault, per David;
 * make write-protect of uffd_wp clear and remove confusion(
   it used to be "wrprotected->writable->wrprotected"), per
   David;
 Thank you for reviewing, David.

 -v1:
 https://lore.kernel.org/linux-mm/20240531104819.140218-1-21cnbao@gmail.com/

 mm/memory.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

Comments

David Hildenbrand June 3, 2024, 9:01 a.m. UTC | #1
On 02.06.24 02:45, Barry Song wrote:
> From: Barry Song <v-songbaohua@oppo.com>
> 
> After swapping out, we perform a swap-in operation. If we first read
> and then write, we encounter a major fault in do_swap_page for reading,
> along with additional minor faults in do_wp_page for writing. However,
> the latter appears to be unnecessary and inefficient. Instead, we can
> directly reuse in do_swap_page and completely eliminate the need for
> do_wp_page.
> 
> This patch achieves that optimization specifically for exclusive folios.
> The following microbenchmark demonstrates the significant reduction in
> minor faults.
> 
>   #define DATA_SIZE (2UL * 1024 * 1024)
>   #define PAGE_SIZE (4UL * 1024)
> 
>   static void *read_write_data(char *addr)
>   {
>           char tmp;
> 
>           for (int i = 0; i < DATA_SIZE; i += PAGE_SIZE) {
>                   tmp = *(volatile char *)(addr + i);
>                   *(volatile char *)(addr + i) = tmp;
>           }
>   }
> 
>   int main(int argc, char **argv)
>   {
>           struct rusage ru;
> 
>           char *addr = mmap(NULL, DATA_SIZE, PROT_READ | PROT_WRITE,
>                           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
>           memset(addr, 0x11, DATA_SIZE);
> 
>           do {
>                   long old_ru_minflt, old_ru_majflt;
>                   long new_ru_minflt, new_ru_majflt;
> 
>                   madvise(addr, DATA_SIZE, MADV_PAGEOUT);
> 
>                   getrusage(RUSAGE_SELF, &ru);
>                   old_ru_minflt = ru.ru_minflt;
>                   old_ru_majflt = ru.ru_majflt;
> 
>                   read_write_data(addr);
>                   getrusage(RUSAGE_SELF, &ru);
>                   new_ru_minflt = ru.ru_minflt;
>                   new_ru_majflt = ru.ru_majflt;
> 
>                   printf("minor faults:%ld major faults:%ld\n",
>                           new_ru_minflt - old_ru_minflt,
>                           new_ru_majflt - old_ru_majflt);
>           } while(0);
> 
>           return 0;
>   }
> 
> w/o patch,
> / # ~/a.out
> minor faults:512 major faults:512
> 
> w/ patch,
> / # ~/a.out
> minor faults:0 major faults:512
> 
> Minor faults decrease to 0!
> 
> Signed-off-by: Barry Song <v-songbaohua@oppo.com>
> ---
>   -v2:
>   * don't set the dirty flag for read fault, per David;
>   * make write-protect of uffd_wp clear and remove confusion(
>     it used to be "wrprotected->writable->wrprotected"), per
>     David;
>   Thank you for reviewing, David.
> 
>   -v1:
>   https://lore.kernel.org/linux-mm/20240531104819.140218-1-21cnbao@gmail.com/
> 
>   mm/memory.c | 18 +++++++++++-------
>   1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/mm/memory.c b/mm/memory.c
> index eef4e482c0c2..9696c7397b85 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4316,6 +4316,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
>   	add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages);
>   	add_mm_counter(vma->vm_mm, MM_SWAPENTS, -nr_pages);
>   	pte = mk_pte(page, vma->vm_page_prot);
> +	if (pte_swp_soft_dirty(vmf->orig_pte))
> +		pte = pte_mksoft_dirty(pte);
> +	if (pte_swp_uffd_wp(vmf->orig_pte))
> +		pte = pte_mkuffd_wp(pte);
>   
>   	/*
>   	 * Same logic as in do_wp_page(); however, optimize for pages that are
> @@ -4325,18 +4329,18 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
>   	 */
>   	if (!folio_test_ksm(folio) &&
>   	    (exclusive || folio_ref_count(folio) == 1)) {
> -		if (vmf->flags & FAULT_FLAG_WRITE) {
> -			pte = maybe_mkwrite(pte_mkdirty(pte), vma);
> -			vmf->flags &= ~FAULT_FLAG_WRITE;
> +		if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&
> +		    !vma_soft_dirty_enabled(vma)) {
> +			pte = pte_mkwrite(pte, vma);
> +			if (vmf->flags & FAULT_FLAG_WRITE) {
> +				pte = pte_mkdirty(pte);
> +				vmf->flags &= ~FAULT_FLAG_WRITE;
> +			}
>   		}
>   		rmap_flags |= RMAP_EXCLUSIVE;
>   	}
>   	folio_ref_add(folio, nr_pages - 1);
>   	flush_icache_pages(vma, page, nr_pages);
> -	if (pte_swp_soft_dirty(vmf->orig_pte))
> -		pte = pte_mksoft_dirty(pte);
> -	if (pte_swp_uffd_wp(vmf->orig_pte))
> -		pte = pte_mkuffd_wp(pte);
>   	vmf->orig_pte = pte_advance_pfn(pte, page_idx);
>   
>   	/* ksm created a completely new copy */

Thanks, LGTM!

Acked-by: David Hildenbrand <david@redhat.com>

Maybe we really want (likely with better naming)

soft_dirty_pte(vma, pte)
soft_dirty_pte(vma, pmd)

that wrap the VMA+pte check, like we have for uffd-wp.

$ git grep vma_soft_dirty | grep -E "(pmd)|(pte)"
mm/gup.c:       if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd))
mm/gup.c:       if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))
mm/huge_memory.c:       if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd))
mm/mprotect.c:  if (vma_soft_dirty_enabled(vma) && !pte_soft_dirty(pte))

... then we could simply use the one here as well.
diff mbox series

Patch

diff --git a/mm/memory.c b/mm/memory.c
index eef4e482c0c2..9696c7397b85 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4316,6 +4316,10 @@  vm_fault_t do_swap_page(struct vm_fault *vmf)
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, nr_pages);
 	add_mm_counter(vma->vm_mm, MM_SWAPENTS, -nr_pages);
 	pte = mk_pte(page, vma->vm_page_prot);
+	if (pte_swp_soft_dirty(vmf->orig_pte))
+		pte = pte_mksoft_dirty(pte);
+	if (pte_swp_uffd_wp(vmf->orig_pte))
+		pte = pte_mkuffd_wp(pte);
 
 	/*
 	 * Same logic as in do_wp_page(); however, optimize for pages that are
@@ -4325,18 +4329,18 @@  vm_fault_t do_swap_page(struct vm_fault *vmf)
 	 */
 	if (!folio_test_ksm(folio) &&
 	    (exclusive || folio_ref_count(folio) == 1)) {
-		if (vmf->flags & FAULT_FLAG_WRITE) {
-			pte = maybe_mkwrite(pte_mkdirty(pte), vma);
-			vmf->flags &= ~FAULT_FLAG_WRITE;
+		if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&
+		    !vma_soft_dirty_enabled(vma)) {
+			pte = pte_mkwrite(pte, vma);
+			if (vmf->flags & FAULT_FLAG_WRITE) {
+				pte = pte_mkdirty(pte);
+				vmf->flags &= ~FAULT_FLAG_WRITE;
+			}
 		}
 		rmap_flags |= RMAP_EXCLUSIVE;
 	}
 	folio_ref_add(folio, nr_pages - 1);
 	flush_icache_pages(vma, page, nr_pages);
-	if (pte_swp_soft_dirty(vmf->orig_pte))
-		pte = pte_mksoft_dirty(pte);
-	if (pte_swp_uffd_wp(vmf->orig_pte))
-		pte = pte_mkuffd_wp(pte);
 	vmf->orig_pte = pte_advance_pfn(pte, page_idx);
 
 	/* ksm created a completely new copy */