Skip to contentSkip to conversation

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

RFC PATCH Superseded Barry Song 2 participants 9 messages May 31, 2024 – Jun 1, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

RFC PATCH 1/1 mm: swap: reuse exclusive folio directly instead of wp page faults

From: Barry Song <[email protected]>

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!

---
 mm/memory.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +4 -3
index eef4e482c0c2..e1d2e339958e 100644
@@ -4325,9 +4325,10 @@ 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) {
+			pte = pte_mkwrite(pte_mkdirty(pte), vma);
+			if (vmf->flags & FAULT_FLAG_WRITE)
+				vmf->flags &= ~FAULT_FLAG_WRITE;
 		}
 		rmap_flags |= RMAP_EXCLUSIVE;
 	}
Signature · 1 line
2.34.1
#1 lore raw
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
  mm/memory.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+			pte = pte_mkwrite(pte_mkdirty(pte), vma);
+			if (vmf->flags & FAULT_FLAG_WRITE)
+				vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Signature · 3 lines
Cheers,

David / dhildenb
author
#2 lore raw
Quoted 94 lines · David Hildenbrand
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
  mm/memory.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+                     pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                     if (vmf->flags & FAULT_FLAG_WRITE)
+                             vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

Quoted 7 lines

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Thanks!

I assume you mean something as below?

mm/memory.c +9 -3
index eef4e482c0c2..dbf1ba8ccfd6 100644
@@ -4317,6 +4317,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
        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
         * certainly not shared either because we just allocated them without
@@ -4325,18 +4329,19 @@ 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) {
+                       if (vmf->flags & FAULT_FLAG_WRITE) {
+                               pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                               vmf->flags &= ~FAULT_FLAG_WRITE;
+                       } else if ((!vma_soft_dirty_enabled(vma) ||

pte_soft_dirty(pte))
+ && !userfaultfd_pte_wp(vma, pte)) {
+ pte = pte_mkwrite(pte, vma);
+ }

                }
                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 */
Quoted 6 lines

--
Cheers,

David / dhildenb

Thanks
Barry

#3 lore raw
Quoted 101 lines · Barry Song
Quoted 100 lines · David Hildenbrand
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
   mm/memory.c | 7 ++++---
   1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+                     pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                     if (vmf->flags & FAULT_FLAG_WRITE)
+                             vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

Quoted 5 lines
Quoted 1 line

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Thanks!

I assume you mean something as below?

It raises an important point: uffd-wp must be handled accordingly.

Quoted 21 lines
mm/memory.c +4 -1
index eef4e482c0c2..dbf1ba8ccfd6 100644
@@ -4317,6 +4317,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
         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
          * certainly not shared either because we just allocated them without
@@ -4325,18 +4329,19 @@ 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) {

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4339

Quoted 10 lines
mm/memory.c +7 -2
-                       pte = maybe_mkwrite(pte_mkdirty(pte), vma);
-                       vmf->flags &= ~FAULT_FLAG_WRITE;
+               if (vma->vm_flags & VM_WRITE) {
+                       if (vmf->flags & FAULT_FLAG_WRITE) {
+                               pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                               vmf->flags &= ~FAULT_FLAG_WRITE;
+                       } else if ((!vma_soft_dirty_enabled(vma) ||
 pte_soft_dirty(pte))
+                                   && !userfaultfd_pte_wp(vma, pte)) {
+                                       pte = pte_mkwrite(pte, vma);

Even with FAULT_FLAG_WRITE we must respect uffd-wp and *not* do a pte_mkwrite(pte). So we have to catch and handle that earlier (I could have sworn we handle that somehow).

Note that the existing

	pte = pte_mkuffd_wp(pte);

Will fix that up because it does an implicit pte_wrprotect().

So maybe what would work is

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

    !vma_soft_dirty_enabled(vma)) {
	pte = pte_mkwrite(pte);

	/* Only set the PTE dirty on write fault. */
	if (vmf->flags & FAULT_FLAG_WRITE) {
		pte = pte_mkdirty(pte);
		vmf->flags &= ~FAULT_FLAG_WRITE;
	}
}
Signature · 3 lines
Cheers,

David / dhildenb
author
#4 lore raw
Quoted 147 lines · David Hildenbrand
Quoted 101 lines · Barry Song
Quoted 100 lines · David Hildenbrand
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
   mm/memory.c | 7 ++++---
   1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+                     pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                     if (vmf->flags & FAULT_FLAG_WRITE)
+                             vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

Quoted 5 lines
Quoted 1 line

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Thanks!

I assume you mean something as below?

It raises an important point: uffd-wp must be handled accordingly.

Quoted 21 lines
mm/memory.c +4 -1
index eef4e482c0c2..dbf1ba8ccfd6 100644
@@ -4317,6 +4317,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
         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
          * certainly not shared either because we just allocated them without
@@ -4325,18 +4329,19 @@ 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) {

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4339

Quoted 10 lines
mm/memory.c +7 -2
-                       pte = maybe_mkwrite(pte_mkdirty(pte), vma);
-                       vmf->flags &= ~FAULT_FLAG_WRITE;
+               if (vma->vm_flags & VM_WRITE) {
+                       if (vmf->flags & FAULT_FLAG_WRITE) {
+                               pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                               vmf->flags &= ~FAULT_FLAG_WRITE;
+                       } else if ((!vma_soft_dirty_enabled(vma) ||
 pte_soft_dirty(pte))
+                                   && !userfaultfd_pte_wp(vma, pte)) {
+                                       pte = pte_mkwrite(pte, vma);

Even with FAULT_FLAG_WRITE we must respect uffd-wp and *not* do a pte_mkwrite(pte). So we have to catch and handle that earlier (I could have sworn we handle that somehow).

Quoted 4 lines

Note that the existing

        pte = pte_mkuffd_wp(pte);

Will fix that up because it does an implicit pte_wrprotect().

This is exactly what I have missed as I am struggling with why WRITE_FAULT blindly does mkwrite without checking userfaultfd_pte_wp().

Quoted 6 lines

So maybe what would work is

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

Quoted 10 lines
     !vma_soft_dirty_enabled(vma)) {
        pte = pte_mkwrite(pte);

        /* Only set the PTE dirty on write fault. */
        if (vmf->flags & FAULT_FLAG_WRITE) {
                pte = pte_mkdirty(pte);
                vmf->flags &= ~FAULT_FLAG_WRITE;
        }
}

looks good!

Quoted 5 lines

--
Cheers,

David / dhildenb

Thanks
Barry

author
#5 lore raw
Quoted 149 lines · Barry Song
Quoted 147 lines · David Hildenbrand
Quoted 101 lines · Barry Song
Quoted 100 lines · David Hildenbrand
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
   mm/memory.c | 7 ++++---
   1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+                     pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                     if (vmf->flags & FAULT_FLAG_WRITE)
+                             vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

Quoted 5 lines
Quoted 1 line

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Thanks!

I assume you mean something as below?

It raises an important point: uffd-wp must be handled accordingly.

Quoted 21 lines
mm/memory.c +4 -1
index eef4e482c0c2..dbf1ba8ccfd6 100644
@@ -4317,6 +4317,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
         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
          * certainly not shared either because we just allocated them without
@@ -4325,18 +4329,19 @@ 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) {

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4339

Quoted 10 lines
mm/memory.c +7 -2
-                       pte = maybe_mkwrite(pte_mkdirty(pte), vma);
-                       vmf->flags &= ~FAULT_FLAG_WRITE;
+               if (vma->vm_flags & VM_WRITE) {
+                       if (vmf->flags & FAULT_FLAG_WRITE) {
+                               pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                               vmf->flags &= ~FAULT_FLAG_WRITE;
+                       } else if ((!vma_soft_dirty_enabled(vma) ||
 pte_soft_dirty(pte))
+                                   && !userfaultfd_pte_wp(vma, pte)) {
+                                       pte = pte_mkwrite(pte, vma);

Even with FAULT_FLAG_WRITE we must respect uffd-wp and *not* do a pte_mkwrite(pte). So we have to catch and handle that earlier (I could have sworn we handle that somehow).

Quoted 22 lines
Quoted 4 lines

Note that the existing

        pte = pte_mkuffd_wp(pte);

Will fix that up because it does an implicit pte_wrprotect().

This is exactly what I have missed as I am struggling with why WRITE_FAULT blindly does mkwrite without checking userfaultfd_pte_wp().

Quoted 14 lines

So maybe what would work is

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

     !vma_soft_dirty_enabled(vma)) {
        pte = pte_mkwrite(pte);

        /* Only set the PTE dirty on write fault. */
        if (vmf->flags & FAULT_FLAG_WRITE) {
                pte = pte_mkdirty(pte);
                vmf->flags &= ~FAULT_FLAG_WRITE;
        }

WRITE_FAULT has a pte_mkdirty, so it doesn't need to check "!vma_soft_dirty_enabled(vma)"?
Maybe I thought too much, just the simple code below should work?

        if (!folio_test_ksm(folio) &&
            (exclusive || folio_ref_count(folio) == 1)) {
                if (vma->vm_flags & VM_WRITE) {
                        if (vmf->flags & FAULT_FLAG_WRITE) {
                                pte = pte_mkwrite(pte_mkdirty(pte), vma);
                                vmf->flags &= ~FAULT_FLAG_WRITE;
                        } else {
                                pte = pte_mkwrite(pte, vma);
                        }
                }
                rmap_flags |= RMAP_EXCLUSIVE;
        }

        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);

This still uses the implicit wrprotect of pte_mkuffd_wp.

Quoted 13 lines
Quoted 2 lines

}

looks good!

Quoted 5 lines

--
Cheers,

David / dhildenb

Thanks
Barry

#6 lore raw
Quoted 173 lines · Barry Song
Quoted 149 lines · Barry Song
Quoted 147 lines · David Hildenbrand
Quoted 101 lines · Barry Song
Quoted 100 lines · David Hildenbrand
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
    mm/memory.c | 7 ++++---
    1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+                     pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                     if (vmf->flags & FAULT_FLAG_WRITE)
+                             vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

Quoted 5 lines
Quoted 1 line

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Thanks!

I assume you mean something as below?

It raises an important point: uffd-wp must be handled accordingly.

Quoted 21 lines
mm/memory.c +4 -1
index eef4e482c0c2..dbf1ba8ccfd6 100644
@@ -4317,6 +4317,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
          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
           * certainly not shared either because we just allocated them without
@@ -4325,18 +4329,19 @@ 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) {

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4339

Quoted 10 lines
mm/memory.c +7 -2
-                       pte = maybe_mkwrite(pte_mkdirty(pte), vma);
-                       vmf->flags &= ~FAULT_FLAG_WRITE;
+               if (vma->vm_flags & VM_WRITE) {
+                       if (vmf->flags & FAULT_FLAG_WRITE) {
+                               pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                               vmf->flags &= ~FAULT_FLAG_WRITE;
+                       } else if ((!vma_soft_dirty_enabled(vma) ||
 pte_soft_dirty(pte))
+                                   && !userfaultfd_pte_wp(vma, pte)) {
+                                       pte = pte_mkwrite(pte, vma);

Even with FAULT_FLAG_WRITE we must respect uffd-wp and *not* do a pte_mkwrite(pte). So we have to catch and handle that earlier (I could have sworn we handle that somehow).

Quoted 22 lines
Quoted 4 lines

Note that the existing

         pte = pte_mkuffd_wp(pte);

Will fix that up because it does an implicit pte_wrprotect().

This is exactly what I have missed as I am struggling with why WRITE_FAULT blindly does mkwrite without checking userfaultfd_pte_wp().

Quoted 14 lines

So maybe what would work is

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

      !vma_soft_dirty_enabled(vma)) {
         pte = pte_mkwrite(pte);

         /* Only set the PTE dirty on write fault. */
         if (vmf->flags & FAULT_FLAG_WRITE) {
                 pte = pte_mkdirty(pte);
                 vmf->flags &= ~FAULT_FLAG_WRITE;
         }
Quoted 3 lines

WRITE_FAULT has a pte_mkdirty, so it doesn't need to check "!vma_soft_dirty_enabled(vma)"?
Maybe I thought too much, just the simple code below should work?

That would likely not handle softdirty correctly in case we end up in pte_mkwrite(pte, vma); note that pte_mksoft_dirty() will not wrprotect ...

(note that we shouldn't optimize for softdirty handling)

Quoted 19 lines
         if (!folio_test_ksm(folio) &&
             (exclusive || folio_ref_count(folio) == 1)) {
                 if (vma->vm_flags & VM_WRITE) {
                         if (vmf->flags & FAULT_FLAG_WRITE) {
                                 pte = pte_mkwrite(pte_mkdirty(pte), vma);
                                 vmf->flags &= ~FAULT_FLAG_WRITE;
                         } else {
                                 pte = pte_mkwrite(pte, vma);
                         }
                 }
                 rmap_flags |= RMAP_EXCLUSIVE;
         }

         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);
Quoted 1 line

This still uses the implicit wrprotect of pte_mkuffd_wp.

But the wrprotected->writable->wrprotected path really is confusing. I'd prefer to set these bits ahead of time instead, so we can properly rely on them -- like we do in other code.

Signature · 3 lines
Cheers,

David / dhildenb
author
#7 lore raw
Quoted 175 lines · David Hildenbrand
Quoted 173 lines · Barry Song
Quoted 171 lines · Barry Song
Quoted 147 lines · David Hildenbrand
Quoted 101 lines · Barry Song
Quoted 100 lines · David Hildenbrand
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
    mm/memory.c | 7 ++++---
    1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+                     pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                     if (vmf->flags & FAULT_FLAG_WRITE)
+                             vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

Quoted 5 lines
Quoted 1 line

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Thanks!

I assume you mean something as below?

It raises an important point: uffd-wp must be handled accordingly.

Quoted 21 lines
mm/memory.c +4 -1
index eef4e482c0c2..dbf1ba8ccfd6 100644
@@ -4317,6 +4317,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
          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
           * certainly not shared either because we just allocated them without
@@ -4325,18 +4329,19 @@ 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) {

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4339

Quoted 10 lines
mm/memory.c +7 -2
-                       pte = maybe_mkwrite(pte_mkdirty(pte), vma);
-                       vmf->flags &= ~FAULT_FLAG_WRITE;
+               if (vma->vm_flags & VM_WRITE) {
+                       if (vmf->flags & FAULT_FLAG_WRITE) {
+                               pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                               vmf->flags &= ~FAULT_FLAG_WRITE;
+                       } else if ((!vma_soft_dirty_enabled(vma) ||
 pte_soft_dirty(pte))
+                                   && !userfaultfd_pte_wp(vma, pte)) {
+                                       pte = pte_mkwrite(pte, vma);

Even with FAULT_FLAG_WRITE we must respect uffd-wp and *not* do a pte_mkwrite(pte). So we have to catch and handle that earlier (I could have sworn we handle that somehow).

Quoted 4 lines

Note that the existing

         pte = pte_mkuffd_wp(pte);

Will fix that up because it does an implicit pte_wrprotect().

This is exactly what I have missed as I am struggling with why WRITE_FAULT blindly does mkwrite without checking userfaultfd_pte_wp().

Quoted 14 lines

So maybe what would work is

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

      !vma_soft_dirty_enabled(vma)) {
         pte = pte_mkwrite(pte);

         /* Only set the PTE dirty on write fault. */
         if (vmf->flags & FAULT_FLAG_WRITE) {
                 pte = pte_mkdirty(pte);
                 vmf->flags &= ~FAULT_FLAG_WRITE;
         }
Quoted 6 lines
Quoted 3 lines

WRITE_FAULT has a pte_mkdirty, so it doesn't need to check "!vma_soft_dirty_enabled(vma)"?
Maybe I thought too much, just the simple code below should work?

That would likely not handle softdirty correctly in case we end up in pte_mkwrite(pte, vma); note that pte_mksoft_dirty() will not wrprotect ...

if SOFTDIRTY has been set, we shouldn't do wrprotect? till the dirty is cleared, we don't rely on further write fault to set soft dirty, right?

so we should better do pte_mkwrite if pte_soft_dirty(pte) == true?

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

      (!vma_soft_dirty_enabled(vma) || pte_soft_dirty(pte)))
Quoted 22 lines

(note that we shouldn't optimize for softdirty handling)

Quoted 19 lines
         if (!folio_test_ksm(folio) &&
             (exclusive || folio_ref_count(folio) == 1)) {
                 if (vma->vm_flags & VM_WRITE) {
                         if (vmf->flags & FAULT_FLAG_WRITE) {
                                 pte = pte_mkwrite(pte_mkdirty(pte), vma);
                                 vmf->flags &= ~FAULT_FLAG_WRITE;
                         } else {
                                 pte = pte_mkwrite(pte, vma);
                         }
                 }
                 rmap_flags |= RMAP_EXCLUSIVE;
         }

         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);
Quoted 5 lines
Quoted 1 line

This still uses the implicit wrprotect of pte_mkuffd_wp.

But the wrprotected->writable->wrprotected path really is confusing. I'd prefer to set these bits ahead of time instead, so we can properly rely on them -- like we do in other code.

I agree. we are setting WRITE then reverting the WRITE. It is confusing.

So in conclusion, we get the belows?

        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
         * certainly not shared either because we just allocated them without
         * exposing them to the swapcache or because the swap entry indicates
         * exclusivity.
         */
        if (!folio_test_ksm(folio) &&
            (exclusive || folio_ref_count(folio) == 1)) {
                if (vma->vm_flags & VM_WRITE && !userfaultfd_pte_wp(vma, pte)
                    && (!vma_soft_dirty_enabled(vma) || pte_soft_dirty(pte))) {
                        if (vmf->flags & FAULT_FLAG_WRITE) {
                                pte = pte_mkwrite(pte_mkdirty(pte), vma);
                                vmf->flags &= ~FAULT_FLAG_WRITE;
                        } else {
                                pte = pte_mkwrite(pte, vma);
                        }
                }
                rmap_flags |= RMAP_EXCLUSIVE;
        }
Quoted 6 lines

--
Cheers,

David / dhildenb

Thanks
Barry

#8 lore raw
Quoted 180 lines · Barry Song
Quoted 179 lines · David Hildenbrand
Quoted 176 lines · Barry Song
Quoted 149 lines · Barry Song
Quoted 147 lines · David Hildenbrand
Quoted 101 lines · Barry Song
Quoted 100 lines · David Hildenbrand
Quoted 77 lines · Barry Song

From: Barry Song <[email protected]>

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!

---
     mm/memory.c | 7 ++++---
     1 file changed, 4 insertions(+), 3 deletions(-)
mm/memory.c +0 -0
index eef4e482c0c2..e1d2e339958e 100644

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4331

Quoted 10 lines
mm/memory.c +4 -3
@@ -4325,9 +4325,10 @@ 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) {
+                     pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                     if (vmf->flags & FAULT_FLAG_WRITE)
+                             vmf->flags &= ~FAULT_FLAG_WRITE;

This implies, that even on a read fault, you would mark the pte dirty and it would have to be written back to swap if still in the swap cache and only read.

That is controversial.

What is less controversial is doing what mprotect() via change_pte_range()/can_change_pte_writable() would do: mark the PTE writable but not dirty.

Quoted 5 lines
Quoted 1 line

I suggest setting the pte only dirty if FAULT_FLAG_WRITE is set.

Thanks!

I assume you mean something as below?

It raises an important point: uffd-wp must be handled accordingly.

Quoted 21 lines
mm/memory.c +4 -1
index eef4e482c0c2..dbf1ba8ccfd6 100644
@@ -4317,6 +4317,10 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
           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
            * certainly not shared either because we just allocated them without
@@ -4325,18 +4329,19 @@ 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) {

mm/memory.c vm_fault_t do_swap_page(struct vm_fault *vmf) · line 4339

Quoted 10 lines
mm/memory.c +7 -2
-                       pte = maybe_mkwrite(pte_mkdirty(pte), vma);
-                       vmf->flags &= ~FAULT_FLAG_WRITE;
+               if (vma->vm_flags & VM_WRITE) {
+                       if (vmf->flags & FAULT_FLAG_WRITE) {
+                               pte = pte_mkwrite(pte_mkdirty(pte), vma);
+                               vmf->flags &= ~FAULT_FLAG_WRITE;
+                       } else if ((!vma_soft_dirty_enabled(vma) ||
 pte_soft_dirty(pte))
+                                   && !userfaultfd_pte_wp(vma, pte)) {
+                                       pte = pte_mkwrite(pte, vma);

Even with FAULT_FLAG_WRITE we must respect uffd-wp and *not* do a pte_mkwrite(pte). So we have to catch and handle that earlier (I could have sworn we handle that somehow).

Quoted 22 lines
Quoted 4 lines

Note that the existing

          pte = pte_mkuffd_wp(pte);

Will fix that up because it does an implicit pte_wrprotect().

This is exactly what I have missed as I am struggling with why WRITE_FAULT blindly does mkwrite without checking userfaultfd_pte_wp().

Quoted 14 lines

So maybe what would work is

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

       !vma_soft_dirty_enabled(vma)) {
          pte = pte_mkwrite(pte);

          /* Only set the PTE dirty on write fault. */
          if (vmf->flags & FAULT_FLAG_WRITE) {
                  pte = pte_mkdirty(pte);
                  vmf->flags &= ~FAULT_FLAG_WRITE;
          }

WRITE_FAULT has a pte_mkdirty, so it doesn't need to check "!vma_soft_dirty_enabled(vma)"?
Maybe I thought too much, just the simple code below should work?

Quoted 5 lines
Quoted 2 lines

That would likely not handle softdirty correctly in case we end up in pte_mkwrite(pte, vma); note that pte_mksoft_dirty() will not wrprotect ...

if SOFTDIRTY has been set, we shouldn't do wrprotect? till the dirty is cleared, we don't rely on further write fault to set soft dirty, right?

If softidrty is enabled for the VMA and the PTE is softdirty, we can (not should) map it writable.

My point is that softdirty tracking is so underused that optimizing it here is likely not required.

Quoted 38 lines

so we should better do pte_mkwrite if pte_soft_dirty(pte) == true?

  if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&
       (!vma_soft_dirty_enabled(vma) || pte_soft_dirty(pte)))
Quoted 22 lines

(note that we shouldn't optimize for softdirty handling)

Quoted 19 lines
          if (!folio_test_ksm(folio) &&
              (exclusive || folio_ref_count(folio) == 1)) {
                  if (vma->vm_flags & VM_WRITE) {
                          if (vmf->flags & FAULT_FLAG_WRITE) {
                                  pte = pte_mkwrite(pte_mkdirty(pte), vma);
                                  vmf->flags &= ~FAULT_FLAG_WRITE;
                          } else {
                                  pte = pte_mkwrite(pte, vma);
                          }
                  }
                  rmap_flags |= RMAP_EXCLUSIVE;
          }

          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);
Quoted 5 lines
Quoted 1 line

This still uses the implicit wrprotect of pte_mkuffd_wp.

But the wrprotected->writable->wrprotected path really is confusing. I'd prefer to set these bits ahead of time instead, so we can properly rely on them -- like we do in other code.

I agree. we are setting WRITE then reverting the WRITE. It is confusing.

So in conclusion, we get the belows?

Quoted 15 lines
         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
          * certainly not shared either because we just allocated them without
          * exposing them to the swapcache or because the swap entry indicates
          * exclusivity.
          */
         if (!folio_test_ksm(folio) &&
             (exclusive || folio_ref_count(folio) == 1)) {
                 if (vma->vm_flags & VM_WRITE && !userfaultfd_pte_wp(vma, pte)
                     && (!vma_soft_dirty_enabled(vma) || pte_soft_dirty(pte))) {

And here the code gets ugly. Just do

if ((vma->vm_flags & VM_WRITE) && !userfaultfd_pte_wp(vma, pte) &&

    !vma_soft_dirty_enabled(vma)) {
	...
}

and don't optimize softdirty. Or if you really want to, have a helper function like userfaultfd_pte_wp() that wraps both checks.

Quoted 7 lines
                         if (vmf->flags & FAULT_FLAG_WRITE) {
                                 pte = pte_mkwrite(pte_mkdirty(pte), vma);
                                 vmf->flags &= ~FAULT_FLAG_WRITE;
                         } else {
                                 pte = pte_mkwrite(pte, vma);
                         }
                 }

why not

pte = pte_mkwrite(pte, vma);

if (vmf->flags & FAULT_FLAG_WRITE) {
	pte = pte_mkdirty(pte);
	vmf->flags &= ~FAULT_FLAG_WRITE;
}

Conceptually, I think this should be fine.

Signature · 3 lines
Cheers,

David / dhildenb