Skip to contentSkip to conversation

Introduce Copy-On-Write to Page Table

PATCH Superseded Chih-En Lin 4 participants 37 messages Feb 7, 2023 – Feb 14, 2023

Subsystems (20)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Introduce Copy-On-Write to Page Table

v3 -> v4
- Add Kconfig, CONFIG_COW_PTE, since some of the architectures, e.g., s390 and powerpc32, don't support the PMD entry and PTE table operations.
- Fix unmatch type of break_cow_pte_range() in migrate_vma_collect_pmd().
- Don’t break COW PTE in folio_referenced_one().
- Fix the wrong VMA range checking in break_cow_pte_range().
- Only break COW when we modify the soft-dirty bit in clear_refs_pte_range().
- Handle do_swap_page() with COW PTE in mm/memory.c and mm/khugepaged.c.
- Change the tlb flush from flush_tlb_mm_range() (x86 specific) to tlb_flush_pmd_range().
- Handle VM_DONTCOPY with COW PTE fork.
- Fix the wrong address and invalid vma in recover_pte_range().
- Fix the infinite page fault loop in GUP routine.
In mm/gup.c:follow_pfn_pte(), instead of calling the break COW PTE handler, we return -EMLINK to let the GUP handles the page fault (call faultin_page() in __get_user_pages()).
- return not_found(pvmw) if the break COW PTE failed in page_vma_mapped_walk().
- Since COW PTE has the same result as the normal COW selftest, it probably passed the COW selftest.

# [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB)
not ok 33 No leak from parent into child
# [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) not ok 44 No leak from parent into child
# [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) not ok 55 No leak from child into parent
# [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) not ok 66 No leak from child into parent

Bail out! 4 out of 147 tests failed
# Totals: pass:143 fail:4 xfail:0 xpass:0 skip:0 error:0
See the more information about anon cow hugetlb tests:

    https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/

v3: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v2 -> v3
- Change the sysctl with PID to prctl(PR_SET_COW_PTE).
- Account all the COW PTE mapped pages in fork() instead of defer it to page fault (break COW PTE).
- If there is an unshareable mapped page (maybe pinned or private device), recover all the entries that are already handled by COW PTE fork, then copy to the new one.
- Remove COW_PTE_OWNER_EXCLUSIVE flag and handle the only case of GUP, follow_pfn_pte().
- Remove the PTE ownership since we don't need it.
- Use pte lock to protect the break COW PTE and free COW-ed PTE.
- Do TLB flushing in break COW PTE handler.
- Handle THP, KSM, madvise, mprotect, uffd and migrate device.
- Handle the replacement page of uprobe.
- Handle the clear_refs_write() of fs/proc.
- All of the benchmarks dropped since the accounting and pte lock. The benchmarks of v3 is worse than RFC v2, most of the cases are similar to the normal fork, but there still have an use case (TriforceAFL) is better than the normal fork version.

RFC v2: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v1 -> RFC v2
- Change the clone flag method to sysctl with PID.
- Change the MMF_COW_PGTABLE flag to two flags, MMF_COW_PTE and MMF_COW_PTE_READY, for the sysctl.
- Change the owner pointer to use the folio padding.
- Handle all the VMAs that cover the PTE table when doing the break COW PTE.
- Remove the self-defined refcount to use the _refcount for the page table page.
- Add the exclusive flag to let the page table only own by one task in some situations.
- Invalidate address range MMU notifier and start the write_seqcount when doing the break COW PTE.
- Handle the swap cache and swapoff.

RFC v1: https://lore.kernel.org/all/[email protected]/

---

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

DEFAULT FORK
parent child

VmRSS:   1049688 kB    1048688 kB
VmPTE:      2096 kB       2096 kB

This patch introduces copy-on-write (COW) for the PTE level page tables. COW PTE improves performance in the situation where the user needs copies of the program to run on isolated environments. Feedback-based fuzzers (e.g., AFL) and serverless/microservice frameworks are two major examples. For instance, COW PTE achieves a 1.03x throughput increase when running TriforceAFL.

After applying COW to PTE, the memory usage after forking is as follows:

COW PTE
parent child
VmRSS: 1049968 kB 2576 kB
VmPTE: 2096 kB 44 kB

The results show that this patch significantly decreases memory usage. The other number of latencies are discussed later.

Real-world application benchmarks
=================================

We run benchmarks of fuzzing and VM cloning. The experiments were done with the normal fork or the fork with COW PTE.

With AFL (LLVM mode) and SQLite, COW PTE (52.15 execs/sec) is a little bit worse than the normal fork version (53.50 execs/sec).

fork
execs_per_sec unix_time time

count    28.000000  2.800000e+01   28.000000
mean     53.496786  1.671270e+09   96.107143
std       3.625060  7.194717e+01   71.947172
min      35.350000  1.671270e+09    0.000000
25%      53.967500  1.671270e+09   33.750000
50%      54.235000  1.671270e+09   92.000000
75%      54.525000  1.671270e+09  149.250000
max      55.100000  1.671270e+09  275.000000

COW PTE
execs_per_sec unix_time time

count    34.000000  3.400000e+01   34.000000
mean     52.150000  1.671268e+09  103.323529
std       3.218271  7.507682e+01   75.076817
min      34.250000  1.671268e+09    0.000000
25%      52.500000  1.671268e+09   42.250000
50%      52.750000  1.671268e+09   94.500000
75%      52.952500  1.671268e+09  150.750000
max      53.680000  1.671268e+09  285.000000

With TriforceAFL which is for kernel fuzzing with QEMU, COW PTE (105.54 execs/sec) achieves a 1.05x throughput increase over the normal fork version (102.30 execs/sec).

fork
execs_per_sec unix_time time

count    38.000000  3.800000e+01   38.000000
mean    102.299737  1.671269e+09  156.289474
std      20.139268  8.717113e+01   87.171130
min       6.600000  1.671269e+09    0.000000
25%      95.657500  1.671269e+09   82.250000
50%     109.950000  1.671269e+09  176.500000
75%     113.972500  1.671269e+09  223.750000
max     118.790000  1.671269e+09  281.000000

COW PTE
execs_per_sec unix_time time

count    42.000000  4.200000e+01   42.000000
mean    105.540714  1.671269e+09  163.476190
std      19.443517  8.858845e+01   88.588453
min       6.200000  1.671269e+09    0.000000
25%      96.585000  1.671269e+09  123.500000
50%     113.925000  1.671269e+09  180.500000
75%     116.940000  1.671269e+09  233.500000
max     121.090000  1.671269e+09  286.000000

Microbenchmark - syscall latency
================================

We run microbenchmarks to measure the latency of a fork syscall with sizes of mapped memory ranging from 0 to 512 MB. The results show that the latency of a normal fork reaches 10 ms. The latency of a fork with COW PTE is also around 10 ms.

Microbenchmark - page fault latency
====================================

We conducted some microbenchmarks to measure page fault latency with different patterns of accesses to a 512 MB memory buffer after forking.

In the first experiment, the program accesses the entire 512 MB memory by writing to all the pages consecutively. The experiment is done with normal fork, fork with COW PTE and calculates the single access average latency. COW PTE page fault latency (0.000795 ms) and the normal fork fault latency (0.000770 ms). Here are the raw numbers:

Page fault - Access to the entire 512 MB memory

fork mean: 0.000770 ms
fork median: 0.000769 ms
fork std: 0.000010 ms

COW PTE mean: 0.000795 ms
COW PTE median: 0.000795 ms
COW PTE std: 0.000009 ms

The second experiment simulates real-world applications with sparse accesses. The program randomly accesses the memory by writing to one random page 1 million times and calculates the average access time, after that, we run both 100 times to get the averages. The result shows that COW PTE (0.000029 ms) is similar to the normal fork (0.000026 ms).

Page fault - Random access

fork mean: 0.000026 ms
fork median: 0.000025 ms
fork std: 0.000002 ms

COW PTE mean: 0.000029 ms
COW PTE median: 0.000026 ms
COW PTE std: 0.000004 ms

All the tests were run with QEMU and the kernel was built with the x86_64 default config (v3 patch set).

Summary
=======

In summary, COW PTE reduces the memory footprint of processes and improves the performance for some use cases.

This patch is based on the paper "On-demand-fork: a microsecond fork for memory-intensive and latency-sensitive applications" [1] from Purdue University.

Any comments and suggestions are welcome.

Thanks,
Chih-En Lin

---

[1] https://dl.acm.org/doi/10.1145/3447786.3456258

This patch is based on v6.2-rc7.

---

Chih-En Lin (14):
mm: Allow user to control COW PTE via prctl
mm: Add Copy-On-Write PTE to fork()
mm: Add break COW PTE fault and helper functions mm/rmap: Break COW PTE in rmap walking mm/khugepaged: Break COW PTE before scanning pte mm/ksm: Break COW PTE before modify shared PTE mm/madvise: Handle COW-ed PTE with madvise() mm/gup: Trigger break COW PTE before calling follow_pfn_pte() mm/mprotect: Break COW PTE before changing protection mm/userfaultfd: Support COW PTE
mm/migrate_device: Support COW PTE
fs/proc: Support COW PTE with clear_refs_write events/uprobes: Break COW PTE before replacing page
mm: fork: Enable COW PTE to fork system call

 fs/proc/task_mmu.c                 |   5 +
 include/linux/mm.h                 |  37 ++
 include/linux/pgtable.h            |   6 +
 include/linux/rmap.h               |   2 +
 include/linux/sched/coredump.h     |  12 +-
 include/trace/events/huge_memory.h |   1 +
 include/uapi/linux/prctl.h         |   6 +
 kernel/events/uprobes.c            |   2 +-
 kernel/fork.c                      |   7 +
 kernel/sys.c                       |  11 +
 mm/Kconfig                         |   9 +
 mm/gup.c                           |   8 +-
 mm/khugepaged.c                    |  35 +-
 mm/ksm.c                           |   4 +-
 mm/madvise.c                       |  13 +
 mm/memory.c                        | 642 ++++++++++++++++++++++++++++-
 mm/migrate.c                       |   3 +-
 mm/migrate_device.c                |   2 +
 mm/mmap.c                          |   4 +
 mm/mprotect.c                      |   9 +
 mm/mremap.c                        |   2 +
 mm/page_vma_mapped.c               |   4 +
 mm/rmap.c                          |   9 +-
 mm/swapfile.c                      |   2 +
 mm/userfaultfd.c                   |   6 +
 mm/vmscan.c                        |   3 +-
 26 files changed, 826 insertions(+), 18 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/14 mm: Allow user to control COW PTE via prctl

Add a new prctl, PR_SET_COW_PTE, to allow the user to enable COW PTE. Since it has a time gap between using the prctl to enable the COW PTE and doing the fork, we use two states (MMF_COW_PTE_READY and MMF_COW_PTE) to determine the task that wants to do COW PTE or already doing it.

The MMF_COW_PTE_READY flag marks the task to do COW PTE in the next time of fork(). During fork(), if MMF_COW_PTE_READY set, fork() will unset the flag and set the MMF_COW_PTE flag. After that, fork() might shares PTEs instead of duplicates it.

---
 include/linux/sched/coredump.h | 12 +++++++++++-
 include/uapi/linux/prctl.h     |  6 ++++++
 kernel/sys.c                   | 11 +++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)
include/linux/sched/coredump.h +11 -1
index 8270ad7ae14c..570d599ebc85 100644
@@ -83,7 +83,17 @@ static inline int get_dumpable(struct mm_struct *mm)
 #define MMF_HAS_PINNED		27	/* FOLL_PIN has run, never cleared */
 #define MMF_DISABLE_THP_MASK	(1 << MMF_DISABLE_THP)
 
+/*
+ * MMF_COW_PTE_READY: Marking the task to do COW PTE in the next time of
+ * fork(). During fork(), if MMF_COW_PTE_READY set, fork() will unset the
+ * flag and set the MMF_COW_PTE flag. After that, fork() might shares PTEs
+ * rather than duplicates it.
+ */
+#define MMF_COW_PTE_READY	29 /* Share PTE tables in next time of fork() */
+#define MMF_COW_PTE		30 /* PTE tables are shared between processes */
+#define MMF_COW_PTE_MASK	(1 << MMF_COW_PTE)
+
 #define MMF_INIT_MASK		(MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
-				 MMF_DISABLE_THP_MASK)
+				 MMF_DISABLE_THP_MASK | MMF_COW_PTE_MASK)
 
 #endif /* _LINUX_SCHED_COREDUMP_H */
include/uapi/linux/prctl.h +6 -0
index a5e06dcbba13..664a3c023019 100644
@@ -284,4 +284,10 @@ struct prctl_mm_map {
 #define PR_SET_VMA		0x53564d41
 # define PR_SET_VMA_ANON_NAME		0
 
+/*
+ * Set the prepare flag, MMF_COW_PTE_READY, to do the share (copy-on-write)
+ * page table in the next time of fork.
+ */
+#define PR_SET_COW_PTE			65
+
 #endif /* _LINUX_PRCTL_H */
kernel/sys.c +11 -0
index 88b31f096fb2..eeab3093026f 100644
@@ -2350,6 +2350,14 @@ static int prctl_set_vma(unsigned long opt, unsigned long start,
 }
 #endif /* CONFIG_ANON_VMA_NAME */
 
+static int prctl_set_cow_pte(struct mm_struct *mm)
+{
+	if (test_bit(MMF_COW_PTE, &mm->flags))
+		return -EINVAL;
+	set_bit(MMF_COW_PTE_READY, &mm->flags);
+	return 0;
+}
+
 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 		unsigned long, arg4, unsigned long, arg5)
 {
@@ -2628,6 +2636,9 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 	case PR_SET_VMA:
 		error = prctl_set_vma(arg2, arg3, arg4, arg5);
 		break;
+	case PR_SET_COW_PTE:
+		error = prctl_set_cow_pte(me->mm);
+		break;
 	default:
 		error = -EINVAL;
 		break;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/14 mm: Add Copy-On-Write PTE to fork()

In reply to Chih-En Lin

Add copy_cow_pte_range() and recover_pte_range() for copy-on-write (COW) PTE in fork system call. During COW PTE fork, when processing the shared PTE, we traverse all the entries to determine current mapped page is available to share between processes. If PTE can be shared, account those mapped pages and then share the PTE. However, once we find out the mapped page is unavailable, e.g., pinned page, we have to copy it via copy_present_page(), which means that we will fall back to default path, page table copying (copy_pte_range()). And, since we may have already processed some COW-ed PTE entries, before starting the default path, we have to recover those entries.

All the COW PTE behaviors are protected by the pte lock.
The logic of how we handle nonpresent/present pte entries and error in copy_cow_pte_range() is same as copy_pte_range(). But to keep the codes clean (e.g., avoiding condition lock), we introduce new functions instead of modifying copy_pte_range().

To track the lifetime of COW-ed PTE, introduce the refcount of PTE. We reuse the _refcount in struct page for the page table to maintain the number of process references to COW-ed PTE table. Doing the fork with COW PTE will increase the refcount. And, when someone writes to the COW-ed PTE, it will cause the write fault to break COW PTE. If the refcount of COW-ed PTE is one, the process that triggers the fault will reuse the COW-ed PTE. Otherwise, the process will decrease the refcount and duplicate it.

Since we share the PTE between the parent and child, the state of the parent's pte entries is different between COW PTE and the normal fork. COW PTE handles all the pte entries on the child side which means it will clear the dirty and access bit of the parent's pte entry.

And, since some of the architectures, e.g., s390 and powerpc32, don't support the PMD entry and PTE table operations, add a new Kconfig, COW_PTE. COW_PTE config depends on the (HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT) condition, it is same as the TRANSPARENT_HUGEPAGE config since most of the operations in COW PTE are depend on it.

---
 include/linux/mm.h |  20 +++
 mm/Kconfig         |   9 ++
 mm/memory.c        | 303 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 332 insertions(+)
include/linux/mm.h +20 -0
index 8f857163ac89..22e1e5804e96 100644
@@ -2482,6 +2482,23 @@ static inline bool ptlock_init(struct page *page) { return true; }
 static inline void ptlock_free(struct page *page) {}
 #endif /* USE_SPLIT_PTE_PTLOCKS */
 
+#ifdef CONFIG_COW_PTE
+static inline int pmd_get_pte(pmd_t *pmd)
+{
+	return page_ref_inc_return(pmd_page(*pmd));
+}
+
+static inline bool pmd_put_pte(pmd_t *pmd)
+{
+	return page_ref_add_unless(pmd_page(*pmd), -1, 1);
+}
+
+static inline int cow_pte_count(pmd_t *pmd)
+{
+	return page_count(pmd_page(*pmd));
+}
+#endif
+
 static inline void pgtable_init(void)
 {
 	ptlock_cache_init();
@@ -2494,6 +2511,9 @@ static inline bool pgtable_pte_page_ctor(struct page *page)
 		return false;
 	__SetPageTable(page);
 	inc_lruvec_page_state(page, NR_PAGETABLE);
+#ifdef CONFIG_COW_PTE
+	set_page_count(page, 1);
+#endif
 	return true;
 }
 
mm/Kconfig +9 -0
index ff7b209dec05..7dcceeb4196b 100644
@@ -822,6 +822,15 @@ config READ_ONLY_THP_FOR_FS
 
 endif # TRANSPARENT_HUGEPAGE
 
+menuconfig COW_PTE
+	bool "Copy-on-write PTE table"
+	depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT
+	help
+	  Extend the copy-on-write (COW) mechanism to the PTE table
+	  (the bottom level of the page-table hierarchy). To enable this
+	  feature, a process must set prctl(PR_SET_COW_PTE) before the
+	  fork system call.
+
 #
 # UP and nommu archs use km based percpu allocator
 #
mm/memory.c +303 -0

356 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 3/14 mm: Add break COW PTE fault and helper functions

In reply to Chih-En Lin

Add the function, handle_cow_pte_fault(), to break (unshare) COW-ed PTE with the page fault that will modify the PTE table or the mapped page resided in COW-ed PTE (i.e., write, unshared, file read fault).

When breaking COW PTE, it first checks COW-ed PTE's refcount to try to reuse it. If COW-ed PTE cannot be reused, allocates new PTE and duplicates all pte entries in COW-ed PTE. Moreover, Flush TLB when we change the write protection of PTE.

In addition, provide the helper functions, break_cow_pte{,_range}(), to let the other features (remap, THP, migration, swapfile, etc) to use.

---
 include/linux/mm.h      |  17 ++
 include/linux/pgtable.h |   6 +
 mm/memory.c             | 339 +++++++++++++++++++++++++++++++++++++++-
 mm/mmap.c               |   4 +
 mm/mremap.c             |   2 +
 mm/swapfile.c           |   2 +
 6 files changed, 363 insertions(+), 7 deletions(-)
include/linux/mm.h +17 -0
index 22e1e5804e96..369355e13936 100644
@@ -2020,6 +2020,23 @@ void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to);
 void truncate_pagecache_range(struct inode *inode, loff_t offset, loff_t end);
 int generic_error_remove_page(struct address_space *mapping, struct page *page);
 
+#ifdef CONFIG_COW_PTE
+int break_cow_pte(struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr);
+int break_cow_pte_range(struct vm_area_struct *vma, unsigned long start,
+			unsigned long end);
+#else
+static inline int break_cow_pte(struct vm_area_struct *vma,
+				pmd_t *pmd, unsigned long addr)
+{
+	return 0;
+}
+static inline int break_cow_pte_range(struct vm_area_struct *vma,
+				      unsigned long start, unsigned long end)
+{
+	return 0;
+}
+#endif
+
 #ifdef CONFIG_MMU
 extern vm_fault_t handle_mm_fault(struct vm_area_struct *vma,
 				  unsigned long address, unsigned int flags,
include/linux/pgtable.h +6 -0
index 1159b25b0542..72ff2a1cee5e 100644
@@ -1406,6 +1406,12 @@ static inline int pmd_none_or_trans_huge_or_clear_bad(pmd_t *pmd)
 	if (pmd_none(pmdval) || pmd_trans_huge(pmdval) ||
 		(IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION) && !pmd_present(pmdval)))
 		return 1;
+	/*
+	 * COW-ed PTE has write protection which can trigger pmd_bad().
+	 * To avoid this, return here if entry is write protection.
+	 */
+	if (!pmd_write(pmdval))
+		return 0;
 	if (unlikely(pmd_bad(pmdval))) {
 		pmd_clear_bad(pmd);
 		return 1;
mm/memory.c +332 -7

403 lines not on this page · raw message

mm/mmap.c +4 -0
index 425a9349e610..ca16d7abcdb6 100644
@@ -2208,6 +2208,10 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
 			return err;
 	}
 
+	err = break_cow_pte(vma, NULL, addr);
+	if (err)
+		return err;
+
 	new = vm_area_dup(vma);
 	if (!new)
 		return -ENOMEM;
mm/mremap.c +2 -0
index 930f65c315c0..3fbc45e381cc 100644
@@ -534,6 +534,8 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
 		old_pmd = get_old_pmd(vma->vm_mm, old_addr);
 		if (!old_pmd)
 			continue;
+		/* TLB flush twice time here? */
+		break_cow_pte(vma, old_pmd, old_addr);
 		new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
 		if (!new_pmd)
 			break;
mm/swapfile.c +2 -0
index 4fa440e87cd6..92e39a722100 100644
@@ -1911,6 +1911,8 @@ static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
 		next = pmd_addr_end(addr, end);
 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
 			continue;
+		if (break_cow_pte(vma, pmd, addr))
+			return -ENOMEM;
 		ret = unuse_pte_range(vma, pmd, addr, next, type);
 		if (ret)
 			return ret;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v4 4/14 mm/rmap: Break COW PTE in rmap walking

In reply to Chih-En Lin

Some of the features (unmap, migrate, device exclusive, mkclean, etc) might modify the pte entry via rmap. Add a new page vma mapped walk flag, PVMW_BREAK_COW_PTE, to indicate the rmap walking to break COW PTE.

---
 include/linux/rmap.h | 2 ++
 mm/migrate.c         | 3 ++-
 mm/page_vma_mapped.c | 4 ++++
 mm/rmap.c            | 9 +++++----
 mm/vmscan.c          | 3 ++-
 5 files changed, 15 insertions(+), 6 deletions(-)
include/linux/rmap.h +2 -0
index bd3504d11b15..d0f07e551973 100644
@@ -368,6 +368,8 @@ int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
 #define PVMW_SYNC		(1 << 0)
 /* Look for migration entries rather than present PTEs */
 #define PVMW_MIGRATION		(1 << 1)
+/* Break COW-ed PTE during walking */
+#define PVMW_BREAK_COW_PTE	(1 << 2)
 
 struct page_vma_mapped_walk {
 	unsigned long pfn;
mm/migrate.c +2 -1
index a4d3fc65085f..04376ce05aa8 100644
@@ -183,7 +183,8 @@ void putback_movable_pages(struct list_head *l)
 static bool remove_migration_pte(struct folio *folio,
 		struct vm_area_struct *vma, unsigned long addr, void *old)
 {
-	DEFINE_FOLIO_VMA_WALK(pvmw, old, vma, addr, PVMW_SYNC | PVMW_MIGRATION);
+	DEFINE_FOLIO_VMA_WALK(pvmw, old, vma, addr,
+			      PVMW_SYNC | PVMW_MIGRATION | PVMW_BREAK_COW_PTE);
 
 	while (page_vma_mapped_walk(&pvmw)) {
 		rmap_t rmap_flags = RMAP_NONE;
mm/page_vma_mapped.c +4 -0
index 93e13fc17d3c..7b35e85b9964 100644
@@ -251,6 +251,10 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
 			step_forward(pvmw, PMD_SIZE);
 			continue;
 		}
+		if (pvmw->flags & PVMW_BREAK_COW_PTE) {
+			if (break_cow_pte(vma, pvmw->pmd, pvmw->address))
+				return not_found(pvmw);
+		}
 		if (!map_pte(pvmw))
 			goto next_pte;
 this_pte:
mm/rmap.c +5 -4
index b616870a09be..bce97496b1f6 100644
@@ -1012,7 +1012,8 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw)
 static bool page_mkclean_one(struct folio *folio, struct vm_area_struct *vma,
 			     unsigned long address, void *arg)
 {
-	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, PVMW_SYNC);
+	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address,
+			      PVMW_SYNC | PVMW_BREAK_COW_PTE);
 	int *cleaned = arg;
 
 	*cleaned += page_vma_mkclean_one(&pvmw);
@@ -1463,7 +1464,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		     unsigned long address, void *arg)
 {
 	struct mm_struct *mm = vma->vm_mm;
-	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
+	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, PVMW_BREAK_COW_PTE);
 	pte_t pteval;
 	struct page *subpage;
 	bool anon_exclusive, ret = true;
@@ -1834,7 +1835,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
 		     unsigned long address, void *arg)
 {
 	struct mm_struct *mm = vma->vm_mm;
-	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
+	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, PVMW_BREAK_COW_PTE);
 	pte_t pteval;
 	struct page *subpage;
 	bool anon_exclusive, ret = true;
@@ -2187,7 +2188,7 @@ static bool page_make_device_exclusive_one(struct folio *folio,
 		struct vm_area_struct *vma, unsigned long address, void *priv)
 {
 	struct mm_struct *mm = vma->vm_mm;
-	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
+	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, PVMW_BREAK_COW_PTE);
 	struct make_exclusive_args *args = priv;
 	pte_t pteval;
 	struct page *subpage;
mm/vmscan.c +2 -1
index bf3eedf0209c..15eda32146fd 100644
@@ -1882,7 +1882,8 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 
 		/*
 		 * The folio is mapped into the page tables of one or more
-		 * processes. Try to unmap it here.
+		 * processes. Try to unmap it here. Also, since it will write
+		 * to the page tables, break COW PTE if they are.
 		 */
 		if (folio_mapped(folio)) {
 			enum ttu_flags flags = TTU_BATCH_FLUSH;
Signature · 1 line
2.34.1
#5 lore raw

PATCH v4 5/14 mm/khugepaged: Break COW PTE before scanning pte

In reply to Chih-En Lin

We should not allow THP to collapse COW-ed PTE. So, break COW PTE before collapse_pte_mapped_thp() collapse to THP. Also, break COW PTE before khugepaged_scan_pmd() scan PTE.

---
 include/trace/events/huge_memory.h |  1 +
 mm/khugepaged.c                    | 35 +++++++++++++++++++++++++++++-
 2 files changed, 35 insertions(+), 1 deletion(-)
include/trace/events/huge_memory.h +1 -0
index 3e6fb05852f9..5f2c39f61521 100644
@@ -13,6 +13,7 @@
 	EM( SCAN_PMD_NULL,		"pmd_null")			\
 	EM( SCAN_PMD_NONE,		"pmd_none")			\
 	EM( SCAN_PMD_MAPPED,		"page_pmd_mapped")		\
+	EM( SCAN_COW_PTE,		"cowed_pte")			\
 	EM( SCAN_EXCEED_NONE_PTE,	"exceed_none_pte")		\
 	EM( SCAN_EXCEED_SWAP_PTE,	"exceed_swap_pte")		\
 	EM( SCAN_EXCEED_SHARED_PTE,	"exceed_shared_pte")		\
mm/khugepaged.c +34 -1
index 90acfea40c13..1cddc20318d5 100644
@@ -31,6 +31,7 @@ enum scan_result {
 	SCAN_PMD_NULL,
 	SCAN_PMD_NONE,
 	SCAN_PMD_MAPPED,
+	SCAN_COW_PTE,
 	SCAN_EXCEED_NONE_PTE,
 	SCAN_EXCEED_SWAP_PTE,
 	SCAN_EXCEED_SHARED_PTE,
@@ -875,7 +876,7 @@ static int find_pmd_or_thp_or_none(struct mm_struct *mm,
 		return SCAN_PMD_MAPPED;
 	if (pmd_devmap(pmde))
 		return SCAN_PMD_NULL;
-	if (pmd_bad(pmde))
+	if (pmd_write(pmde) && pmd_bad(pmde))
 		return SCAN_PMD_NULL;
 	return SCAN_SUCCEED;
 }
@@ -926,6 +927,8 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
 			pte_unmap(vmf.pte);
 			continue;
 		}
+		if (break_cow_pte(vma, pmd, address))
+			return SCAN_COW_PTE;
 		ret = do_swap_page(&vmf);
 
 		/*
@@ -1038,6 +1041,9 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
 	if (result != SCAN_SUCCEED)
 		goto out_up_write;
 
+	/* We should already handled COW-ed PTE. */
+	VM_WARN_ON(test_bit(MMF_COW_PTE, &mm->flags) && !pmd_write(*pmd));
+
 	anon_vma_lock_write(vma->anon_vma);
 
 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
@@ -1148,6 +1154,13 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 
 	memset(cc->node_load, 0, sizeof(cc->node_load));
 	nodes_clear(cc->alloc_nmask);
+
+	/* Break COW PTE before we collapse the pages. */
+	if (break_cow_pte(vma, pmd, address)) {
+		result = SCAN_COW_PTE;
+		goto out;
+	}
+
 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
 	for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR;
 	     _pte++, _address += PAGE_SIZE) {
@@ -1206,6 +1219,10 @@ static int hpage_collapse_scan_pmd(struct mm_struct *mm,
 			goto out_unmap;
 		}
 
+		/*
+		 * If we only trigger the break COW PTE, the page usually
+		 * still in COW mapping, which it still be shared.
+		 */
 		if (page_mapcount(page) > 1) {
 			++shared;
 			if (cc->is_khugepaged &&
@@ -1501,6 +1518,11 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 		goto drop_hpage;
 	}
 
+	/* We shouldn't let COW-ed PTE collapse. */
+	if (break_cow_pte(vma, pmd, haddr))
+		goto drop_hpage;
+	VM_WARN_ON(test_bit(MMF_COW_PTE, &mm->flags) && !pmd_write(*pmd));
+
 	/*
 	 * We need to lock the mapping so that from here on, only GUP-fast and
 	 * hardware page walks can access the parts of the page tables that
@@ -1706,6 +1728,11 @@ static int retract_page_tables(struct address_space *mapping, pgoff_t pgoff,
 				result = SCAN_PTE_UFFD_WP;
 				goto unlock_next;
 			}
+			if (test_bit(MMF_COW_PTE, &mm->flags) &&
+			     !pmd_write(*pmd)) {
+				result = SCAN_COW_PTE;
+				goto unlock_next;
+			}
 			collapse_and_free_pmd(mm, vma, addr, pmd);
 			if (!cc->is_khugepaged && is_target)
 				result = set_huge_pmd(vma, addr, pmd, hpage);
@@ -2143,6 +2170,11 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
 	swap = 0;
 	memset(cc->node_load, 0, sizeof(cc->node_load));
 	nodes_clear(cc->alloc_nmask);
+	if (break_cow_pte(find_vma(mm, addr), NULL, addr)) {
+		result = SCAN_COW_PTE;
+		goto out;
+	}
+
 	rcu_read_lock();
 	xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) {
 		if (xas_retry(&xas, page))
@@ -2213,6 +2245,7 @@ static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
 	}
 	rcu_read_unlock();
 
+out:
 	if (result == SCAN_SUCCEED) {
 		if (cc->is_khugepaged &&
 		    present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
Signature · 1 line
2.34.1
#6 lore raw

PATCH v4 6/14 mm/ksm: Break COW PTE before modify shared PTE

In reply to Chih-En Lin

Break COW PTE before merge the page that reside in COW-ed PTE.

---
 mm/ksm.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/ksm.c +3 -1
index dd02780c387f..ce3887d3b04c 100644
@@ -1045,7 +1045,7 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page,
 			      pte_t *orig_pte)
 {
 	struct mm_struct *mm = vma->vm_mm;
-	DEFINE_PAGE_VMA_WALK(pvmw, page, vma, 0, 0);
+	DEFINE_PAGE_VMA_WALK(pvmw, page, vma, 0, PVMW_BREAK_COW_PTE);
 	int swapped;
 	int err = -EFAULT;
 	struct mmu_notifier_range range;
@@ -1163,6 +1163,8 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
 	barrier();
 	if (!pmd_present(pmde) || pmd_trans_huge(pmde))
 		goto out;
+	if (break_cow_pte(vma, pmd, addr))
+		goto out;
 
 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, mm, addr,
 				addr + PAGE_SIZE);
Signature · 1 line
2.34.1
#7 lore raw

PATCH v4 7/14 mm/madvise: Handle COW-ed PTE with madvise()

In reply to Chih-En Lin

Break COW PTE if madvise() modify the pte entry of COW-ed PTE. Following are the list of flags which need to break COW PTE. However, like MADV_HUGEPAGE and MADV_MERGEABLE, we should handle it respectively.

- MADV_DONTNEED: It calls to zap_page_range() which already be handled.
- MADV_FREE: It uses walk_page_range() with madvise_free_pte_range() to free the page by itself, so add break_cow_pte().
- MADV_REMOVE: Same as MADV_FREE, it remove the page by itself, so add

	       break_cow_pte_range().

- MADV_COLD: Similar to MAD_FREE, break COW PTE before pageout.
- MADV_POPULATE: Let GUP deal with it.

---
 mm/madvise.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
mm/madvise.c +13 -0
index b6ea204d4e23..8b815942f286 100644
@@ -428,6 +428,9 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,
 	if (pmd_trans_unstable(pmd))
 		return 0;
 #endif
+	if (break_cow_pte(vma, pmd, addr))
+		return 0;
+
 	tlb_change_page_size(tlb, PAGE_SIZE);
 	orig_pte = pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
 	flush_tlb_batched_pending(mm);
@@ -629,6 +632,10 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
 	if (pmd_trans_unstable(pmd))
 		return 0;
 
+	/* We should only allocate PTE. */
+	if (break_cow_pte(vma, pmd, addr))
+		goto next;
+
 	tlb_change_page_size(tlb, PAGE_SIZE);
 	orig_pte = pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
 	flush_tlb_batched_pending(mm);
@@ -989,6 +996,12 @@ static long madvise_remove(struct vm_area_struct *vma,
 	if ((vma->vm_flags & (VM_SHARED|VM_WRITE)) != (VM_SHARED|VM_WRITE))
 		return -EACCES;
 
+	error = break_cow_pte_range(vma, start, end);
+	if (error < 0)
+		return error;
+	else if (error > 0)
+		return -ENOMEM;
+
 	offset = (loff_t)(start - vma->vm_start)
 			+ ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
 
Signature · 1 line
2.34.1
#8 lore raw

PATCH v4 8/14 mm/gup: Trigger break COW PTE before calling follow_pfn_pte()

In reply to Chih-En Lin

In most of cases, GUP will not modify the page table, excluding follow_pfn_pte(). To deal with COW PTE, Trigger the break COW PTE fault before calling follow_pfn_pte().

---
 mm/gup.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/gup.c +7 -1
index f45a3a5be53a..e702c0800105 100644
@@ -545,7 +545,8 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
 	if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
 			 (FOLL_PIN | FOLL_GET)))
 		return ERR_PTR(-EINVAL);
-	if (unlikely(pmd_bad(*pmd)))
+	/* COW-ed PTE has write protection which can trigger pmd_bad(). */
+	if (unlikely(pmd_write(*pmd) && pmd_bad(*pmd)))
 		return no_page_table(vma, flags);
 
 	ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
@@ -588,6 +589,11 @@ static struct page *follow_page_pte(struct vm_area_struct *vma,
 		if (is_zero_pfn(pte_pfn(pte))) {
 			page = pte_page(pte);
 		} else {
+			if (test_bit(MMF_COW_PTE, &mm->flags) &&
+			    !pmd_write(*pmd)) {
+				page = ERR_PTR(-EMLINK);
+				goto out;
+			}
 			ret = follow_pfn_pte(vma, address, ptep, flags);
 			page = ERR_PTR(ret);
 			goto out;
Signature · 1 line
2.34.1
#9 lore raw

PATCH v4 9/14 mm/mprotect: Break COW PTE before changing protection

In reply to Chih-En Lin

If the PTE table is COW-ed, break it before changing the protection.

---
 mm/mprotect.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/mprotect.c +9 -0
index 61cf60015a8b..8b18cd0e5c5e 100644
@@ -103,6 +103,9 @@ static unsigned long change_pte_range(struct mmu_gather *tlb,
 	if (pmd_trans_unstable(pmd))
 		return 0;
 
+	if (break_cow_pte(vma, pmd, addr))
+		return 0;
+
 	/*
 	 * The pmd points to a regular pte so the pmd can't change
 	 * from under us even if the mmap_lock is only hold for
@@ -314,6 +317,12 @@ static inline int pmd_none_or_clear_bad_unless_trans_huge(pmd_t *pmd)
 		return 1;
 	if (pmd_trans_huge(pmdval))
 		return 0;
+	/*
+	 * If the entry point to COW-ed PTE, it's write protection bit
+	 * will cause pmd_bad().
+	 */
+	if (!pmd_write(pmdval))
+		return 0;
 	if (unlikely(pmd_bad(pmdval))) {
 		pmd_clear_bad(pmd);
 		return 1;
Signature · 1 line
2.34.1
#10 lore raw

PATCH v4 10/14 mm/userfaultfd: Support COW PTE

In reply to Chih-En Lin

If uffd fills the zeropage or installs to COW-ed PTE, break it first.

---
 mm/userfaultfd.c | 6 ++++++
 1 file changed, 6 insertions(+)
mm/userfaultfd.c +6 -0
index 0499907b6f1a..3f66aa3eb54f 100644
@@ -70,6 +70,9 @@ int mfill_atomic_install_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
 	struct inode *inode;
 	pgoff_t offset, max_off;
 
+	if (break_cow_pte(dst_vma, dst_pmd, dst_addr))
+		return -ENOMEM;
+
 	_dst_pte = mk_pte(page, dst_vma->vm_page_prot);
 	_dst_pte = pte_mkdirty(_dst_pte);
 	if (page_in_cache && !vm_shared)
@@ -229,6 +232,9 @@ static int mfill_zeropage_pte(struct mm_struct *dst_mm,
 	pgoff_t offset, max_off;
 	struct inode *inode;
 
+	if (break_cow_pte(dst_vma, dst_pmd, dst_addr))
+		return -ENOMEM;
+
 	_dst_pte = pte_mkspecial(pfn_pte(my_zero_pfn(dst_addr),
 					 dst_vma->vm_page_prot));
 	dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Signature · 1 line
2.34.1
#11 lore raw

PATCH v4 11/14 mm/migrate_device: Support COW PTE

In reply to Chih-En Lin

Break COW PTE before collecting the pages in COW-ed PTE.

---
 mm/migrate_device.c | 2 ++
 1 file changed, 2 insertions(+)
mm/migrate_device.c +2 -0
index 721b2365dbca..2930e591e8fc 100644
@@ -106,6 +106,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		}
 	}
 
+	if (break_cow_pte_range(vma, start, end))
+		return migrate_vma_collect_skip(start, end, walk);
 	if (unlikely(pmd_bad(*pmdp)))
 		return migrate_vma_collect_skip(start, end, walk);
 
Signature · 1 line
2.34.1
#12 lore raw

PATCH v4 12/14 fs/proc: Support COW PTE with clear_refs_write

In reply to Chih-En Lin

Before clearing the entry in COW-ed PTE, break COW PTE first.

---
 fs/proc/task_mmu.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/proc/task_mmu.c +5 -0
index af1c49ae11b1..94958422aede 100644
@@ -1196,6 +1196,11 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
 	if (pmd_trans_unstable(pmd))
 		return 0;
 
+	/* Only break COW when we modify the soft-dirty bit. */
+	if (cp->type == CLEAR_REFS_SOFT_DIRTY &&
+	    break_cow_pte(vma, pmd, addr))
+		return 0;
+
 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
 	for (; addr != end; pte++, addr += PAGE_SIZE) {
 		ptent = *pte;
Signature · 1 line
2.34.1
#13 lore raw

PATCH v4 13/14 events/uprobes: Break COW PTE before replacing page

In reply to Chih-En Lin

Break COW PTE if we want to replace the page which resides in COW-ed PTE.

---
 kernel/events/uprobes.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/events/uprobes.c +1 -1
index d9e357b7e17c..2956a53da01a 100644
@@ -157,7 +157,7 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
 	struct folio *old_folio = page_folio(old_page);
 	struct folio *new_folio;
 	struct mm_struct *mm = vma->vm_mm;
-	DEFINE_FOLIO_VMA_WALK(pvmw, old_folio, vma, addr, 0);
+	DEFINE_FOLIO_VMA_WALK(pvmw, old_folio, vma, addr, PVMW_BREAK_COW_PTE);
 	int err;
 	struct mmu_notifier_range range;
 
Signature · 1 line
2.34.1
#14 lore raw

PATCH v4 14/14 mm: fork: Enable COW PTE to fork system call

In reply to Chih-En Lin

This patch enables the Copy-On-Write (COW) mechanism to the PTE table in fork system call. To let the process do COW PTE fork, use prctl(PR_SET_COW_PTE), it will set the MMF_COW_PTE_READY flag to the process for enabling COW PTE during the next time of fork.

It uses the MMF_COW_PTE flag to distinguish the normal page table and the COW one. Moreover, it is difficult to distinguish whether all the page tables is out of COW state. So the MMF_COW_PTE flag won't be disabled after setup.

---
 kernel/fork.c | 7 +++++++
 1 file changed, 7 insertions(+)
kernel/fork.c +7 -0
index 9f7fe3541897..94c35c8b31b1 100644
@@ -2678,6 +2678,13 @@ pid_t kernel_clone(struct kernel_clone_args *args)
 			trace = 0;
 	}
 
+#ifdef CONFIG_COW_PTE
+	if (current->mm && test_bit(MMF_COW_PTE_READY, &current->mm->flags)) {
+		clear_bit(MMF_COW_PTE_READY, &current->mm->flags);
+		set_bit(MMF_COW_PTE, &current->mm->flags);
+	}
+#endif
+
 	p = copy_process(NULL, trace, NUMA_NO_NODE, args);
 	add_latent_entropy();
 
Signature · 1 line
2.34.1
#15 lore raw
In reply to Chih-En Lin
Quoted 80 lines · Chih-En Lin

v3 -> v4
- Add Kconfig, CONFIG_COW_PTE, since some of the architectures, e.g., s390 and powerpc32, don't support the PMD entry and PTE table operations.
- Fix unmatch type of break_cow_pte_range() in migrate_vma_collect_pmd().
- Don’t break COW PTE in folio_referenced_one().
- Fix the wrong VMA range checking in break_cow_pte_range().
- Only break COW when we modify the soft-dirty bit in clear_refs_pte_range().
- Handle do_swap_page() with COW PTE in mm/memory.c and mm/khugepaged.c.
- Change the tlb flush from flush_tlb_mm_range() (x86 specific) to tlb_flush_pmd_range().
- Handle VM_DONTCOPY with COW PTE fork.
- Fix the wrong address and invalid vma in recover_pte_range().
- Fix the infinite page fault loop in GUP routine.
In mm/gup.c:follow_pfn_pte(), instead of calling the break COW PTE handler, we return -EMLINK to let the GUP handles the page fault (call faultin_page() in __get_user_pages()).
- return not_found(pvmw) if the break COW PTE failed in page_vma_mapped_walk().
- Since COW PTE has the same result as the normal COW selftest, it probably passed the COW selftest.

# [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB)
not ok 33 No leak from parent into child
# [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) not ok 44 No leak from parent into child
# [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) not ok 55 No leak from child into parent
# [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) not ok 66 No leak from child into parent

Bail out! 4 out of 147 tests failed
# Totals: pass:143 fail:4 xfail:0 xpass:0 skip:0 error:0
See the more information about anon cow hugetlb tests:

    https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/

v3: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v2 -> v3
- Change the sysctl with PID to prctl(PR_SET_COW_PTE).
- Account all the COW PTE mapped pages in fork() instead of defer it to page fault (break COW PTE).
- If there is an unshareable mapped page (maybe pinned or private device), recover all the entries that are already handled by COW PTE fork, then copy to the new one.
- Remove COW_PTE_OWNER_EXCLUSIVE flag and handle the only case of GUP, follow_pfn_pte().
- Remove the PTE ownership since we don't need it.
- Use pte lock to protect the break COW PTE and free COW-ed PTE.
- Do TLB flushing in break COW PTE handler.
- Handle THP, KSM, madvise, mprotect, uffd and migrate device.
- Handle the replacement page of uprobe.
- Handle the clear_refs_write() of fs/proc.
- All of the benchmarks dropped since the accounting and pte lock. The benchmarks of v3 is worse than RFC v2, most of the cases are similar to the normal fork, but there still have an use case (TriforceAFL) is better than the normal fork version.

RFC v2: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v1 -> RFC v2
- Change the clone flag method to sysctl with PID.
- Change the MMF_COW_PGTABLE flag to two flags, MMF_COW_PTE and MMF_COW_PTE_READY, for the sysctl.
- Change the owner pointer to use the folio padding.
- Handle all the VMAs that cover the PTE table when doing the break COW PTE.
- Remove the self-defined refcount to use the _refcount for the page table page.
- Add the exclusive flag to let the page table only own by one task in some situations.
- Invalidate address range MMU notifier and start the write_seqcount when doing the break COW PTE.
- Handle the swap cache and swapoff.

RFC v1: https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

---

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Test program:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>

#define USEC    1000000
#define GIG     (1ul << 30)
#define NGIG    32
#define SIZE    (NGIG * GIG)
#define NPROC   16
void main() {
        int page_size = getpagesize();
        struct timeval start, end;
        long duration, i;
        char *p;

        p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (p == MAP_FAILED) {
                perror("mmap");
                exit(1);
        }
        madvise(p, SIZE, MADV_NOHUGEPAGE);

        /* Touch every page */
        for (i = 0; i < SIZE; i += page_size)
                p[i] = 0;

        gettimeofday(&start, NULL);
        for (i = 0; i < NPROC; i++) {
                int pid = fork();

                if (pid == 0) {
                        sleep(30);
                        exit(0);
                }
        }
        gettimeofday(&end, NULL);
        /* Normolize per proc and per gig */
        duration = ((end.tv_sec - start.tv_sec) * USEC
                + (end.tv_usec - start.tv_usec)) / NPROC / NGIG;
        printf("Fork latency per gigabyte: %ld.%06ld seconds\n",
                duration / USEC, duration % USEC);
}
#16 lore raw
Quoted 114 lines · Pasha Tatashin
Quoted 80 lines · Chih-En Lin

v3 -> v4
- Add Kconfig, CONFIG_COW_PTE, since some of the architectures, e.g., s390 and powerpc32, don't support the PMD entry and PTE table operations.
- Fix unmatch type of break_cow_pte_range() in migrate_vma_collect_pmd().
- Don’t break COW PTE in folio_referenced_one().
- Fix the wrong VMA range checking in break_cow_pte_range().
- Only break COW when we modify the soft-dirty bit in clear_refs_pte_range().
- Handle do_swap_page() with COW PTE in mm/memory.c and mm/khugepaged.c.
- Change the tlb flush from flush_tlb_mm_range() (x86 specific) to tlb_flush_pmd_range().
- Handle VM_DONTCOPY with COW PTE fork.
- Fix the wrong address and invalid vma in recover_pte_range().
- Fix the infinite page fault loop in GUP routine.
In mm/gup.c:follow_pfn_pte(), instead of calling the break COW PTE handler, we return -EMLINK to let the GUP handles the page fault (call faultin_page() in __get_user_pages()).
- return not_found(pvmw) if the break COW PTE failed in page_vma_mapped_walk().
- Since COW PTE has the same result as the normal COW selftest, it probably passed the COW selftest.

# [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB)
not ok 33 No leak from parent into child
# [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) not ok 44 No leak from parent into child
# [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) not ok 55 No leak from child into parent
# [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) not ok 66 No leak from child into parent

Bail out! 4 out of 147 tests failed
# Totals: pass:143 fail:4 xfail:0 xpass:0 skip:0 error:0
See the more information about anon cow hugetlb tests:

    https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/

v3: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v2 -> v3
- Change the sysctl with PID to prctl(PR_SET_COW_PTE).
- Account all the COW PTE mapped pages in fork() instead of defer it to page fault (break COW PTE).
- If there is an unshareable mapped page (maybe pinned or private device), recover all the entries that are already handled by COW PTE fork, then copy to the new one.
- Remove COW_PTE_OWNER_EXCLUSIVE flag and handle the only case of GUP, follow_pfn_pte().
- Remove the PTE ownership since we don't need it.
- Use pte lock to protect the break COW PTE and free COW-ed PTE.
- Do TLB flushing in break COW PTE handler.
- Handle THP, KSM, madvise, mprotect, uffd and migrate device.
- Handle the replacement page of uprobe.
- Handle the clear_refs_write() of fs/proc.
- All of the benchmarks dropped since the accounting and pte lock. The benchmarks of v3 is worse than RFC v2, most of the cases are similar to the normal fork, but there still have an use case (TriforceAFL) is better than the normal fork version.

RFC v2: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v1 -> RFC v2
- Change the clone flag method to sysctl with PID.
- Change the MMF_COW_PGTABLE flag to two flags, MMF_COW_PTE and MMF_COW_PTE_READY, for the sysctl.
- Change the owner pointer to use the folio padding.
- Handle all the VMAs that cover the PTE table when doing the break COW PTE.
- Remove the self-defined refcount to use the _refcount for the page table page.
- Add the exclusive flag to let the page table only own by one task in some situations.
- Invalidate address range MMU notifier and start the write_seqcount when doing the break COW PTE.
- Handle the swap cache and swapoff.

RFC v1: https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

---

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

However, it might break the existing logic of the refcount/mapcount of the page and destabilize the system.

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#me2340d963c2758a2561c39cb3baf42c478dfe548
[2] https://lore.kernel.org/linux-mm/[email protected]/T/#mbc33221f00c7cf3d71839b45fc23862a5dac3014

Quoted 43 lines

Test program:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>

#define USEC    1000000
#define GIG     (1ul << 30)
#define NGIG    32
#define SIZE    (NGIG * GIG)
#define NPROC   16
void main() {
        int page_size = getpagesize();
        struct timeval start, end;
        long duration, i;
        char *p;

        p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (p == MAP_FAILED) {
                perror("mmap");
                exit(1);
        }
        madvise(p, SIZE, MADV_NOHUGEPAGE);

        /* Touch every page */
        for (i = 0; i < SIZE; i += page_size)
                p[i] = 0;

        gettimeofday(&start, NULL);
        for (i = 0; i < NPROC; i++) {
                int pid = fork();

                if (pid == 0) {
                        sleep(30);
                        exit(0);
                }
Quoted 8 lines
        }
        gettimeofday(&end, NULL);
        /* Normolize per proc and per gig */
        duration = ((end.tv_sec - start.tv_sec) * USEC
                + (end.tv_usec - start.tv_usec)) / NPROC / NGIG;
        printf("Fork latency per gigabyte: %ld.%06ld seconds\n",
                duration / USEC, duration % USEC);
}

I'm not sure only taking the few testing is enough.
So, I rewrite your test program to run multiple times but focus on a single fork, and get the average time:
fork.log: 0.000498
odfork.log: 0.000469

Test program:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <sys/prctl.h>

#define USEC 1000000
#define GIG (1ul << 30)
#define NGIG 4
#define SIZE (NGIG * GIG)
#define NPROC 16

int main(void)

{
    unsigned int i = 0;
    unsigned long j = 0;
    int pid, page_size = getpagesize();
    struct timeval start, end;
    long duration;
    char *p;

    prctl(65, 0, 0, 0, 0);

    for (i = 0; i < NPROC; i++) {
p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (p == MAP_FAILED) {
            perror("mmap");
            exit(1);
        }
        madvise(p, SIZE, MADV_NOHUGEPAGE);
        /* Touch every page */
        for (j = 0; j < SIZE; j += page_size)
            p[j] = 0;

        gettimeofday(&start, NULL);
        pid = fork();
        switch (pid) {
        case -1:
            perror("fork");
    exit(1);
        case 0: /* child */
            return 0;
default: /* parent */
            gettimeofday(&end, NULL);
            duration = ((end.tv_sec - start.tv_sec) * USEC +
                        (end.tv_usec - start.tv_usec)) /
                       NPROC / NGIG;
            // seconds
            printf("%ld.%06ld\n", duration / USEC, duration % USEC);
            waitpid(pid, NULL, 0);
            munmap(p, SIZE);
    p = NULL;
        }
    }
}

Script:

import numpy

def calc_mean(file):
np_tmp = numpy.loadtxt(file, usecols=range(0,1)) print("{}: {:6f}".format(file, np_tmp.mean()))

calc_mean("fork.log")
calc_mean("odfork.log")

I didn't make the memory size and process number bigger because it ran on my laptop, and I can't access my server for some reason.

Thanks,
Chih-En Lin

Quoted 169 lines · Pasha Tatashin
Quoted 80 lines · Chih-En Lin

v3 -> v4
- Add Kconfig, CONFIG_COW_PTE, since some of the architectures, e.g., s390 and powerpc32, don't support the PMD entry and PTE table operations.
- Fix unmatch type of break_cow_pte_range() in migrate_vma_collect_pmd().
- Don’t break COW PTE in folio_referenced_one().
- Fix the wrong VMA range checking in break_cow_pte_range().
- Only break COW when we modify the soft-dirty bit in clear_refs_pte_range().
- Handle do_swap_page() with COW PTE in mm/memory.c and mm/khugepaged.c.
- Change the tlb flush from flush_tlb_mm_range() (x86 specific) to tlb_flush_pmd_range().
- Handle VM_DONTCOPY with COW PTE fork.
- Fix the wrong address and invalid vma in recover_pte_range().
- Fix the infinite page fault loop in GUP routine.
In mm/gup.c:follow_pfn_pte(), instead of calling the break COW PTE handler, we return -EMLINK to let the GUP handles the page fault (call faultin_page() in __get_user_pages()).
- return not_found(pvmw) if the break COW PTE failed in page_vma_mapped_walk().
- Since COW PTE has the same result as the normal COW selftest, it probably passed the COW selftest.

# [RUN] vmsplice() + unmap in child ... with hugetlb (2048 kB)
not ok 33 No leak from parent into child
# [RUN] vmsplice() + unmap in child with mprotect() optimization ... with hugetlb (2048 kB) not ok 44 No leak from parent into child
# [RUN] vmsplice() before fork(), unmap in parent after fork() ... with hugetlb (2048 kB) not ok 55 No leak from child into parent
# [RUN] vmsplice() + unmap in parent after fork() ... with hugetlb (2048 kB) not ok 66 No leak from child into parent

Bail out! 4 out of 147 tests failed
# Totals: pass:143 fail:4 xfail:0 xpass:0 skip:0 error:0
See the more information about anon cow hugetlb tests:

    https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/

v3: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v2 -> v3
- Change the sysctl with PID to prctl(PR_SET_COW_PTE).
- Account all the COW PTE mapped pages in fork() instead of defer it to page fault (break COW PTE).
- If there is an unshareable mapped page (maybe pinned or private device), recover all the entries that are already handled by COW PTE fork, then copy to the new one.
- Remove COW_PTE_OWNER_EXCLUSIVE flag and handle the only case of GUP, follow_pfn_pte().
- Remove the PTE ownership since we don't need it.
- Use pte lock to protect the break COW PTE and free COW-ed PTE.
- Do TLB flushing in break COW PTE handler.
- Handle THP, KSM, madvise, mprotect, uffd and migrate device.
- Handle the replacement page of uprobe.
- Handle the clear_refs_write() of fs/proc.
- All of the benchmarks dropped since the accounting and pte lock. The benchmarks of v3 is worse than RFC v2, most of the cases are similar to the normal fork, but there still have an use case (TriforceAFL) is better than the normal fork version.

RFC v2: https://lore.kernel.org/linux-mm/[email protected]/T/

RFC v1 -> RFC v2
- Change the clone flag method to sysctl with PID.
- Change the MMF_COW_PGTABLE flag to two flags, MMF_COW_PTE and MMF_COW_PTE_READY, for the sysctl.
- Change the owner pointer to use the folio padding.
- Handle all the VMAs that cover the PTE table when doing the break COW PTE.
- Remove the self-defined refcount to use the _refcount for the page table page.
- Add the exclusive flag to let the page table only own by one task in some situations.
- Invalidate address range MMU notifier and start the write_seqcount when doing the break COW PTE.
- Handle the swap cache and swapoff.

RFC v1: https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

---

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Test program:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>

#define USEC    1000000
#define GIG     (1ul << 30)
#define NGIG    32
#define SIZE    (NGIG * GIG)
#define NPROC   16
void main() {
        int page_size = getpagesize();
        struct timeval start, end;
        long duration, i;
        char *p;

        p = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
        if (p == MAP_FAILED) {
                perror("mmap");
                exit(1);
        }
        madvise(p, SIZE, MADV_NOHUGEPAGE);

        /* Touch every page */
        for (i = 0; i < SIZE; i += page_size)
                p[i] = 0;

        gettimeofday(&start, NULL);
        for (i = 0; i < NPROC; i++) {
                int pid = fork();

                if (pid == 0) {
                        sleep(30);
                        exit(0);
                }
        }
        gettimeofday(&end, NULL);
        /* Normolize per proc and per gig */
        duration = ((end.tv_sec - start.tv_sec) * USEC
                + (end.tv_usec - start.tv_usec)) / NPROC / NGIG;
        printf("Fork latency per gigabyte: %ld.%06ld seconds\n",
                duration / USEC, duration % USEC);
}
#17 lore raw
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Quoted 5 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Quoted 3 lines

However, it might break the existing logic of the refcount/mapcount of the page and destabilize the system.

This makes sense.

Quoted 2 lines

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#me2340d963c2758a2561c39cb3baf42c478dfe548
[2] https://lore.kernel.org/linux-mm/[email protected]/T/#mbc33221f00c7cf3d71839b45fc23862a5dac3014

#18 lore raw
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Quoted 9 lines
Quoted 5 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

Quoted 5 lines
Quoted 3 lines

However, it might break the existing logic of the refcount/mapcount of the page and destabilize the system.

This makes sense.

;)

Thanks,
Chih-En Lin

#19 lore raw
Quoted 46 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 44 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 6 lines
Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do

Sorry, it's "RFC v2 to v3".

Quoted 4 lines

the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Thanks,
Chih-En Lin

#20 lore raw
In reply to Chih-En Lin
Quoted 67 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do
the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Quoted 7 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.
(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.
(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

Quoted 6 lines
Quoted 3 lines

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

Signature · 3 lines
Thanks,

David / dhildenb
#21 lore raw
Quoted 122 lines · David Hildenbrand
Quoted 67 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do
the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Quoted 7 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.
(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.
(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

Quoted 6 lines
Quoted 3 lines

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

Quoted 8 lines

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Yes, it does sound wasteful for a pretty rare corner case that combines splitting THP in a process, and not having enough memory to allocate PTE page tables.

Quoted 4 lines

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

This sounds like a reasonable way to optimize the fork performance for processes with large RSS, which in most cases would have 2M THP mappings. When you say global pool, do you mean per machine, per cgroup, or per process?

Pasha

Quoted 7 lines

--
Thanks,

David / dhildenb

#22 lore raw
Quoted 141 lines · Pasha Tatashin
Quoted 122 lines · David Hildenbrand
Quoted 67 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do
the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Quoted 7 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.
(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.
(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

Quoted 6 lines
Quoted 3 lines

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

Quoted 8 lines

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Yes, it does sound wasteful for a pretty rare corner case that combines splitting THP in a process, and not having enough memory to allocate PTE page tables.

Quoted 4 lines

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

Quoted 4 lines

This sounds like a reasonable way to optimize the fork performance for processes with large RSS, which in most cases would have 2M THP mappings. When you say global pool, do you mean per machine, per cgroup, or per process?

Good question. I recall that the problem is that we sometimes need a new pgtable when splitting a THP, but

(a) we might be under spinlock and cannot sleep. We need an atomic allocation that might fail. For this, a pool might be helpful.

(b) we might actually be out of memory.

My gut feeling is that a global pool would be sufficient, only yo be used when we run into (a) or (b) to be able to make progress in these rare cases.

Something that would be interesting to evaluate is which THP split operations might might require some way to recover when really OOM.

For example, __split_huge_pmd() is currently not able to report a failure. I assume that we could sleep in there. And if we're not able to allocate any memory in there (with sleeping), maybe the process should be zapped either way by the OOM killer.

Signature · 3 lines
Thanks,

David / dhildenb
#23 lore raw
In reply to David Hildenbrand
Quoted 86 lines · David Hildenbrand
Quoted 67 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do
the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Quoted 7 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

Quoted 2 lines

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

I'm looking at the discussion of page table sharing in 2002 [1]. It looks like in 2002 ~ 2006, there also have some patches try to improve fork().

After that, I also saw one thread which is about another shared page table patch's benchmark. I can't find the original patch though [2]. But, I found the probably same patch in 2005 [3], it also mentioned the previous benchmark discussion:

"
For those familiar with the shared page table patch I did a couple of years ago, this patch does not implement copy-on-write page tables for private mappings. Analysis showed the cost and complexity far outweighed any potential benefit.
"

However, it might be different right now. For example, the implemetation . We have split page table lock now, so we don't have to consider the page_table_share_lock thing. Also, presently, we have different use cases (shells [2] v.s. VM cloning and fuzzing) to consider.

Nonetheless, I still think the discussion can provide some of the mind to us.

BTW, It seems like the 2002 patch [1] is different from the 2002 [2] and 2005 [3].

[1] https://lkml.iu.edu/hypermail/linux/kernel/0202.2/0102.html
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/7C49DFF721CB4E671DB260F9@%5B10.1.1.4%5D/T/#u

Quoted 6 lines

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.

Maybe I should also handle arch/ and others parts. I will keep looking at where I missed.

Quoted 3 lines

(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.

Maybe I should add WARN_ON() and skip the failed COW PTE.

Quoted 7 lines

(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

I think that holding mmap_write_lock may be enough (I added mmap_assert_write_locked() in the fault function btw). But, I might be wrong. I will look at the THP stuff to see how they work. Thanks.

Thanks for the review.

Quoted 21 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

Quoted 6 lines
Quoted 3 lines

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

Quoted 7 lines

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

When collapsing huge pages, do/can they reuse those PTEs for backup? So, we don't have to allocate the PTE or maintain the pool.

Thanks,
Chih-En Lin

#24 lore raw
Quoted 9 lines
Quoted 9 lines

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Quoted 7 lines
Quoted 4 lines

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

When collapsing huge pages, do/can they reuse those PTEs for backup? So, we don't have to allocate the PTE or maintain the pool.

It might not work for all pages, as collapsing pages might have had holes in the user page table, and there were no PTE tables. Pasha

#25 lore raw
Quoted 14 lines · Pasha Tatashin
Quoted 14 lines
Quoted 13 lines

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

Quoted 5 lines
Quoted 2 lines

When collapsing huge pages, do/can they reuse those PTEs for backup? So, we don't have to allocate the PTE or maintain the pool.

It might not work for all pages, as collapsing pages might have had holes in the user page table, and there were no PTE tables.

So if there have holes in the user page table, after we doing the collapsing and then splitting. Do those holes be filled? Assume it is, then, I think it's the reason why it's not work for all the pages.

But, after those operations, Will the user get the additional and unexpected memory (which is from the huge page filling)?

I'm a little bit confused now.

Thanks,
Chih-En Lin

#26 lore raw
Quoted 22 lines · Chih-En Lin
Quoted 14 lines · Pasha Tatashin
Quoted 14 lines
Quoted 13 lines

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

Quoted 5 lines
Quoted 2 lines

When collapsing huge pages, do/can they reuse those PTEs for backup? So, we don't have to allocate the PTE or maintain the pool.

It might not work for all pages, as collapsing pages might have had holes in the user page table, and there were no PTE tables.

Quoted 6 lines

So if there have holes in the user page table, after we doing the collapsing and then splitting. Do those holes be filled? Assume it is, then, I think it's the reason why it's not work for all the pages.

But, after those operations, Will the user get the additional and unexpected memory (which is from the huge page filling)?

Yes, more memory is going to be allocated for a process in such THP collapse case. This is similar to madvise huge pages, and touching the first byte may allocate 2M.

Pasha

#27 lore raw
Quoted 27 lines · Pasha Tatashin
Quoted 26 lines · Chih-En Lin
Quoted 14 lines · Pasha Tatashin
Quoted 14 lines
Quoted 13 lines

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

Quoted 5 lines
Quoted 2 lines

When collapsing huge pages, do/can they reuse those PTEs for backup? So, we don't have to allocate the PTE or maintain the pool.

It might not work for all pages, as collapsing pages might have had holes in the user page table, and there were no PTE tables.

So if there have holes in the user page table, after we doing the collapsing and then splitting. Do those holes be filled? Assume it is, then, I think it's the reason why it's not work for all the pages.

Quoted 6 lines
Quoted 2 lines

But, after those operations, Will the user get the additional and unexpected memory (which is from the huge page filling)?

Yes, more memory is going to be allocated for a process in such THP collapse case. This is similar to madvise huge pages, and touching the first byte may allocate 2M.

Thanks for the explanation.
Yeah, It seems like the reuse case can't work for all the pages.

Thanks,
Chih-En Lin

#28 lore raw
In reply to Chih-En Lin
Quoted 32 lines
Quoted 20 lines
Quoted 9 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

Quoted 2 lines

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

I'm looking at the discussion of page table sharing in 2002 [1]. It looks like in 2002 ~ 2006, there also have some patches try to improve fork().

After that, I also saw one thread which is about another shared page table patch's benchmark. I can't find the original patch though [2]. But, I found the probably same patch in 2005 [3], it also mentioned the previous benchmark discussion:

Quoted 6 lines

"
For those familiar with the shared page table patch I did a couple of years ago, this patch does not implement copy-on-write page tables for private mappings. Analysis showed the cost and complexity far outweighed any potential benefit.
"

Thanks for the pointer, interesting read. And my personal opinion is that part of that statement still hold true :)

Quoted 18 lines

However, it might be different right now. For example, the implemetation . We have split page table lock now, so we don't have to consider the page_table_share_lock thing. Also, presently, we have different use cases (shells [2] v.s. VM cloning and fuzzing) to consider.

Nonetheless, I still think the discussion can provide some of the mind to us.

BTW, It seems like the 2002 patch [1] is different from the 2002 [2] and 2005 [3].

[1] https://lkml.iu.edu/hypermail/linux/kernel/0202.2/0102.html
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/7C49DFF721CB4E671DB260F9@%5B10.1.1.4%5D/T/#u

Quoted 2 lines

My quick thoughts after skimming over the core parts of this series

Quoted 7 lines
Quoted 4 lines

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.

Maybe I should also handle arch/ and others parts. I will keep looking at where I missed.

One could add sanity checks when modifying a PTE while the PTE table is still marked shared ... but I guess there are some valid reasons where we might want to modify shared PTE tables (rmap).

Quoted 6 lines
Quoted 4 lines

(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.

Maybe I should add WARN_ON() and skip the failed COW PTE.

One way or the other we'll have to handle it. WARN_ON() sounds wrong for handling OOM situations (e.g., if only that cgroup is OOM).

Quoted 9 lines
Quoted 8 lines

(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 4 lines

I think that holding mmap_write_lock may be enough (I added mmap_assert_write_locked() in the fault function btw). But, I might be wrong. I will look at the THP stuff to see how they work. Thanks.

Ehm, but page faults don't hold the mmap lock writable? And so are other callers, like MADV_DONTNEED or MADV_FREE.

handle_pte_fault()->handle_pte_fault()->mmap_assert_write_locked() should bail out.

Either I am missing something or you didn't test with lockdep enabled :)

Note that there are upstream efforts to use only a VMA lock (and some people even want to perform some page faults only protected by RCU).

Signature · 3 lines
Thanks,

David / dhildenb
#29 lore raw
Quoted 39 lines · David Hildenbrand
Quoted 32 lines
Quoted 20 lines
Quoted 9 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

Quoted 2 lines

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

I'm looking at the discussion of page table sharing in 2002 [1]. It looks like in 2002 ~ 2006, there also have some patches try to improve fork().

After that, I also saw one thread which is about another shared page table patch's benchmark. I can't find the original patch though [2]. But, I found the probably same patch in 2005 [3], it also mentioned the previous benchmark discussion:

Quoted 6 lines

"
For those familiar with the shared page table patch I did a couple of years ago, this patch does not implement copy-on-write page tables for private mappings. Analysis showed the cost and complexity far outweighed any potential benefit.
"

Quoted 8 lines

Thanks for the pointer, interesting read. And my personal opinion is that part of that statement still hold true :)

Quoted 5 lines

However, it might be different right now. For example, the implemetation . We have split page table lock now, so we don't have to consider the page_table_share_lock thing. Also, presently, we have different use cases (shells [2] v.s. VM cloning and fuzzing) to consider.

Oh, and because I stumbled over it, just as an interesting pointer on QEMU devel:

"[PATCH 00/10] Retire Fork-Based Fuzzing" [1]

[1] https://lore.kernel.org/all/[email protected]/T/#u

Signature · 3 lines
Thanks,

David / dhildenb
#30 lore raw
Quoted 50 lines · David Hildenbrand
Quoted 39 lines · David Hildenbrand
Quoted 32 lines
Quoted 20 lines
Quoted 9 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

Quoted 2 lines

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

I'm looking at the discussion of page table sharing in 2002 [1]. It looks like in 2002 ~ 2006, there also have some patches try to improve fork().

After that, I also saw one thread which is about another shared page table patch's benchmark. I can't find the original patch though [2]. But, I found the probably same patch in 2005 [3], it also mentioned the previous benchmark discussion:

Quoted 6 lines

"
For those familiar with the shared page table patch I did a couple of years ago, this patch does not implement copy-on-write page tables for private mappings. Analysis showed the cost and complexity far outweighed any potential benefit.
"

Quoted 8 lines

Thanks for the pointer, interesting read. And my personal opinion is that part of that statement still hold true :)

Quoted 5 lines

However, it might be different right now. For example, the implemetation . We have split page table lock now, so we don't have to consider the page_table_share_lock thing. Also, presently, we have different use cases (shells [2] v.s. VM cloning and fuzzing) to consider.

Quoted 6 lines

Oh, and because I stumbled over it, just as an interesting pointer on QEMU devel:

"[PATCH 00/10] Retire Fork-Based Fuzzing" [1]

[1] https://lore.kernel.org/all/[email protected]/T/#u

Thanks for the information.
It's interesting.

Thanks,
Chih-En Lin

#31 lore raw
In reply to David Hildenbrand
Quoted 39 lines · David Hildenbrand
Quoted 32 lines
Quoted 20 lines
Quoted 9 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

Quoted 2 lines

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

I'm looking at the discussion of page table sharing in 2002 [1]. It looks like in 2002 ~ 2006, there also have some patches try to improve fork().

After that, I also saw one thread which is about another shared page table patch's benchmark. I can't find the original patch though [2]. But, I found the probably same patch in 2005 [3], it also mentioned the previous benchmark discussion:

Quoted 6 lines

"
For those familiar with the shared page table patch I did a couple of years ago, this patch does not implement copy-on-write page tables for private mappings. Analysis showed the cost and complexity far outweighed any potential benefit.
"

Quoted 2 lines

Thanks for the pointer, interesting read. And my personal opinion is that part of that statement still hold true :)

;)

Quoted 23 lines
Quoted 23 lines

However, it might be different right now. For example, the implemetation . We have split page table lock now, so we don't have to consider the page_table_share_lock thing. Also, presently, we have different use cases (shells [2] v.s. VM cloning and fuzzing) to consider.

Nonetheless, I still think the discussion can provide some of the mind to us.

BTW, It seems like the 2002 patch [1] is different from the 2002 [2] and 2005 [3].

[1] https://lkml.iu.edu/hypermail/linux/kernel/0202.2/0102.html
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/7C49DFF721CB4E671DB260F9@%5B10.1.1.4%5D/T/#u

Quoted 6 lines

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.

Quoted 6 lines
Quoted 2 lines

Maybe I should also handle arch/ and others parts. I will keep looking at where I missed.

One could add sanity checks when modifying a PTE while the PTE table is still marked shared ... but I guess there are some valid reasons where we might want to modify shared PTE tables (rmap).

Sounds good for adding sanity checks. I will look at this. One of the valid reasons that come to my head might be the referenced bit (rmap).

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.

Maybe I should add WARN_ON() and skip the failed COW PTE.

One way or the other we'll have to handle it. WARN_ON() sounds wrong for handling OOM situations (e.g., if only that cgroup is OOM).

Or we should do the same thing like you mentioned:
"
For example, __split_huge_pmd() is currently not able to report a failure. I assume that we could sleep in there. And if we're not able to allocate any memory in there (with sleeping), maybe the process should be zapped either way by the OOM killer.
"

But instead of zapping the process, we just skip the failed COW PTE. I don't think the user will expect their process to be killed by changing the protection.

Quoted 14 lines
Quoted 9 lines
Quoted 8 lines

(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 4 lines

I think that holding mmap_write_lock may be enough (I added mmap_assert_write_locked() in the fault function btw). But, I might be wrong. I will look at the THP stuff to see how they work. Thanks.

Quoted 7 lines

Ehm, but page faults don't hold the mmap lock writable? And so are other callers, like MADV_DONTNEED or MADV_FREE.

handle_pte_fault()->handle_pte_fault()->mmap_assert_write_locked() should bail out.

Either I am missing something or you didn't test with lockdep enabled :)

You're right. I thought I enabled the lockdep.
And, why do I have the page fault will handle the mmap lock writable in my mind. The page fault holds the mmap lock readable instead of writable. ;-)

I should check/test all the locks again.
Thanks.

Quoted 3 lines

Note that there are upstream efforts to use only a VMA lock (and some people even want to perform some page faults only protected by RCU).

I saw the discussion (https://lwn.net/Articles/906852/) before.
If the page fault handler only uses a VMA lock, handle_cow_pte_fault() might not be affected since it only takes one VMA at a time. handle_cow_pte_fault() just allocate the PTE and copy the COW mapping entries to the new one. It's alredy handle the checking and accounting in copy_cow_pte_range().

But, if we decide to skip the per-page operation during fork(). We should handle the VMA lock (or RCU) for the accounting and other stuff. It might be more complicated than before...

Thanks,
Chih-En Lin

#32 lore raw
Quoted 18 lines · Chih-En Lin
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.

Maybe I should add WARN_ON() and skip the failed COW PTE.

One way or the other we'll have to handle it. WARN_ON() sounds wrong for handling OOM situations (e.g., if only that cgroup is OOM).

Or we should do the same thing like you mentioned:
"
For example, __split_huge_pmd() is currently not able to report a failure. I assume that we could sleep in there. And if we're not able to allocate any memory in there (with sleeping), maybe the process should be zapped either way by the OOM killer.
"

Quoted 3 lines

But instead of zapping the process, we just skip the failed COW PTE. I don't think the user will expect their process to be killed by changing the protection.

The process is consuming more memory than it is capable of consuming. The process most probably would have died earlier without the PTE optimization.

But yeah, it all gets tricky ...

Quoted 23 lines
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines

(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 4 lines

I think that holding mmap_write_lock may be enough (I added mmap_assert_write_locked() in the fault function btw). But, I might be wrong. I will look at the THP stuff to see how they work. Thanks.

Quoted 7 lines

Ehm, but page faults don't hold the mmap lock writable? And so are other callers, like MADV_DONTNEED or MADV_FREE.

handle_pte_fault()->handle_pte_fault()->mmap_assert_write_locked() should bail out.

Either I am missing something or you didn't test with lockdep enabled :)

Quoted 7 lines

You're right. I thought I enabled the lockdep.
And, why do I have the page fault will handle the mmap lock writable in my mind. The page fault holds the mmap lock readable instead of writable. ;-)

I should check/test all the locks again.
Thanks.

Note that we have other ways of traversing page tables, especially, using the rmap which does not hold the mmap lock. Not sure if there are similar issues when suddenly finding no page table where there logically should be one. Or when a page table gets replaced and modified, while rmap code still walks the shared copy. Hm.

Signature · 3 lines
Thanks,

David / dhildenb
#33 lore raw
Quoted 56 lines · David Hildenbrand
Quoted 18 lines · Chih-En Lin
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.

Maybe I should add WARN_ON() and skip the failed COW PTE.

One way or the other we'll have to handle it. WARN_ON() sounds wrong for handling OOM situations (e.g., if only that cgroup is OOM).

Or we should do the same thing like you mentioned:
"
For example, __split_huge_pmd() is currently not able to report a failure. I assume that we could sleep in there. And if we're not able to allocate any memory in there (with sleeping), maybe the process should be zapped either way by the OOM killer.
"

Quoted 3 lines

But instead of zapping the process, we just skip the failed COW PTE. I don't think the user will expect their process to be killed by changing the protection.

The process is consuming more memory than it is capable of consuming. The process most probably would have died earlier without the PTE optimization.

But yeah, it all gets tricky ...

Quoted 28 lines
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines

(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 4 lines

I think that holding mmap_write_lock may be enough (I added mmap_assert_write_locked() in the fault function btw). But, I might be wrong. I will look at the THP stuff to see how they work. Thanks.

Quoted 7 lines

Ehm, but page faults don't hold the mmap lock writable? And so are other callers, like MADV_DONTNEED or MADV_FREE.

handle_pte_fault()->handle_pte_fault()->mmap_assert_write_locked() should bail out.

Either I am missing something or you didn't test with lockdep enabled :)

You're right. I thought I enabled the lockdep.
And, why do I have the page fault will handle the mmap lock writable in my mind. The page fault holds the mmap lock readable instead of writable. ;-)

Quoted 8 lines
Quoted 2 lines

I should check/test all the locks again.
Thanks.

Note that we have other ways of traversing page tables, especially, using the rmap which does not hold the mmap lock. Not sure if there are similar issues when suddenly finding no page table where there logically should be one. Or when a page table gets replaced and modified, while rmap code still walks the shared copy. Hm.

It seems like I should take carefully for the page table entry in page fault with rmap. ;)
While the rmap code walks the page table, it will hold the pt lock. So, maybe I should hold the old (shared) PTE table's lock in handle_cow_pte_fault() all the time.

Thanks,
Chih-En Lin

#34 lore raw
In reply to David Hildenbrand
Quoted 122 lines · David Hildenbrand
Quoted 67 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do
the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Quoted 7 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.
(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.
(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

Quoted 6 lines
Quoted 3 lines

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

Quoted 8 lines

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

When you say "split THP", do you mean split the compound page to base pages? IIUC the backup PTE table page is used to guarantee the PMD split (just convert pmd mapped THP to PTE-mapped but not split the compound page) succeed. You may already notice there is no return value for PMD split.

The PMD split may be called quite often, for example, MADV_DONTNEED, mbind, mlock, and even in memory reclamation context (THP swap).

Quoted 5 lines

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

It might work. But may be much more complicated than what you thought when handling multiple parallel PMD splits.

Quoted 6 lines

--
Thanks,

David / dhildenb

#35 lore raw
Quoted 129 lines · Yang Shi
Quoted 128 lines · David Hildenbrand
Quoted 67 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do
the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Quoted 7 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.
(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.
(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

Quoted 6 lines
Quoted 3 lines

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

Quoted 8 lines
Quoted 2 lines

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

When you say "split THP", do you mean split the compound page to base pages? IIUC the backup PTE table page is used to guarantee the PMD split (just convert pmd mapped THP to PTE-mapped but not split the compound page) succeed. You may already notice there is no return value for PMD split.

Yes, as I raised in my other reply.

Quoted 3 lines

The PMD split may be called quite often, for example, MADV_DONTNEED, mbind, mlock, and even in memory reclamation context (THP swap).

Yes, but with a single MADV_DONTNEED call you cannot PTE-map more than 2 THP (all other overlapped THP will get zapped). Same with most other operations.

There are corner cases, though. I recall that s390x/kvm wants to break all THP in a given VMA range. But that operation could safely fail if we can't do that.

Certainly needs some investigation, that's most probably why it hasn't been done yet.

Quoted 9 lines
Quoted 6 lines

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

It might work. But may be much more complicated than what you thought when handling multiple parallel PMD splits.

I consider the whole PTE-table linking to THPs complicated enough to eventually replace it by something differently complicated that wastes less memory ;)

Signature · 3 lines
Thanks,

David / dhildenb
#36 lore raw
Quoted 143 lines · David Hildenbrand
Quoted 129 lines · Yang Shi
Quoted 128 lines · David Hildenbrand
Quoted 67 lines · Chih-En Lin
Quoted 45 lines · Pasha Tatashin
Quoted 34 lines
Quoted 30 lines
Quoted 6 lines

Currently, copy-on-write is only used for the mapped memory; the child process still needs to copy the entire page table from the parent process during forking. The parent process might take a lot of time and memory to copy the page table when the parent has a big page table allocated. For example, the memory usage of a process after forking with 1 GB mapped memory is as follows:

For some reason, I was not able to reproduce performance improvements with a simple fork() performance measurement program. The results that I saw are the following:

Base:
Fork latency per gigabyte: 0.004416 seconds
Fork latency per gigabyte: 0.004382 seconds
Fork latency per gigabyte: 0.004442 seconds
COW kernel:
Fork latency per gigabyte: 0.004524 seconds
Fork latency per gigabyte: 0.004764 seconds
Fork latency per gigabyte: 0.004547 seconds

AMD EPYC 7B12 64-Core Processor
Base:
Fork latency per gigabyte: 0.003923 seconds
Fork latency per gigabyte: 0.003909 seconds
Fork latency per gigabyte: 0.003955 seconds
COW kernel:
Fork latency per gigabyte: 0.004221 seconds
Fork latency per gigabyte: 0.003882 seconds
Fork latency per gigabyte: 0.003854 seconds

Quoted 3 lines

Given, that page table for child is not copied, I was expecting the performance to be better with COW kernel, and also not to depend on the size of the parent.

Quoted 10 lines

Yes, the child won't duplicate the page table, but fork will still traverse all the page table entries to do the accounting. And, since this patch expends the COW to the PTE table level, it's not the mapped page (page table entry) grained anymore, so we have to guarantee that all the mapped page is available to do COW mapping in the such page table.
This kind of checking also costs some time.
As a result, since the accounting and the checking, the COW PTE fork still depends on the size of the parent so the improvement might not be significant.

Quoted 4 lines

The current version of the series does not provide any performance improvements for fork(). I would recommend removing claims from the cover letter about better fork() performance, as this may be misleading for those looking for a way to speed up forking. In my

From v3 to v4, I changed the implementation of the COW fork() part to do
the accounting and checking. At the time, I also removed most of the descriptions about the better fork() performance. Maybe it's not enough and still has some misleading. I will fix this in the next version. Thanks.

Quoted 5 lines

case, I was looking to speed up Redis OSS, which relies on fork() to create consistent snapshots for driving replicates/backups. The O(N) per-page operation causes fork() to be slow, so I was hoping that this series, which does not duplicate the VA during fork(), would make the operation much quicker.

Indeed, at first, I tried to avoid the O(N) per-page operation by deferring the accounting and the swap stuff to the page fault. But, as I mentioned, it's not suitable for the mainline.

Quoted 7 lines

Honestly, for improving the fork(), I have an idea to skip the per-page operation without breaking the logic. However, this will introduce the complicated mechanism and may has the overhead for other features. It might not be worth it. It's hard to strike a balance between the over-complicated mechanism with (probably) better performance and data consistency with the page status. So, I would focus on the safety and stable approach at first.

Yes, it is most probably possible, but complexity, robustness and maintainability have to be considered as well.

Thanks for implementing this approach (only deduplication without other optimizations) and evaluating it accordingly. It's certainly "cleaner", such that we only have to mess with unsharing and not with other accounting/pinning/mapcount thingies. But it also highlights how intrusive even this basic deduplication approach already is -- and that most benefits of the original approach requires even more complexity on top.

I am not quite sure if the benefit is worth the price (I am not to decide and I would like to hear other options).

My quick thoughts after skimming over the core parts of this series

(1) forgetting to break COW on a PTE in some pgtable walker feels quite likely (meaning that it might be fairly error-prone) and forgetting to break COW on a PTE table, accidentally modifying the shared table.
(2) break_cow_pte() can fail, which means that we can fail some operations (possibly silently halfway through) now. For example, looking at your change_pte_range() change, I suspect it's wrong.
(3) handle_cow_pte_fault() looks quite complicated and needs quite some
double-checking: we temporarily clear the PMD, to reset it afterwards. I am not sure if that is correct. For example, what stops another page fault stumbling over that pmd_none() and allocating an empty page table? Maybe there are some locking details missing or they are very subtle such that we better document them. I
recall that THP played quite some tricks to make such cases work ...

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Actually, at the RFC v1 and v2, we proposed the version of skipping those works, and we got a significant improvement. You can see the number from RFC v2 cover letter [1]:
"In short, with 512 MB mapped memory, COW PTE decreases latency by 93% for normal fork"

Quoted 6 lines
Quoted 3 lines

I suspect the 93% improvement (when the mapcount was not updated) was only for VAs with 4K pages. With 2M mappings this series did not provide any benefit is this correct?

Yes. In this case, the COW PTE performance is similar to the normal fork().

The thing with THP is, that during fork(), we always allocate a backup PTE table, to be able to PTE-map the THP whenever we have to. Otherwise we'd have to eventually fail some operations we don't want to fail -- similar to the case where break_cow_pte() could fail now due to -ENOMEM although we really don't want to fail (e.g., change_pte_range() ).

Quoted 8 lines
Quoted 2 lines

I always considered that wasteful, because in many scenarios, we'll never ever split a THP and possibly waste memory.

When you say "split THP", do you mean split the compound page to base pages? IIUC the backup PTE table page is used to guarantee the PMD split (just convert pmd mapped THP to PTE-mapped but not split the compound page) succeed. You may already notice there is no return value for PMD split.

Yes, as I raised in my other reply.

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

The PMD split may be called quite often, for example, MADV_DONTNEED, mbind, mlock, and even in memory reclamation context (THP swap).

Yes, but with a single MADV_DONTNEED call you cannot PTE-map more than 2 THP (all other overlapped THP will get zapped). Same with most other operations.

My point is there may be multiple processes calling PMD split on different THPs at the same time.

Quoted 4 lines

There are corner cases, though. I recall that s390x/kvm wants to break all THP in a given VMA range. But that operation could safely fail if we can't do that.

I'm supposed that is THP split (split the compound page), it may fail.

Quoted 11 lines

Certainly needs some investigation, that's most probably why it hasn't been done yet.

Quoted 7 lines
Quoted 6 lines

Optimizing that for THP (e.g., don't always allocate backup THP, have some global allocation backup pool for splits + refill when close-to-empty) might provide similar fork() improvements, both in speed and memory consumption when it comes to anonymous memory.

Quoted 7 lines
Quoted 2 lines

It might work. But may be much more complicated than what you thought when handling multiple parallel PMD splits.

I consider the whole PTE-table linking to THPs complicated enough to eventually replace it by something differently complicated that wastes less memory ;)

Maybe...

Quoted 6 lines

--
Thanks,

David / dhildenb