[PATCH v5 16/17] mm: fork: Enable COW PTE to fork system call

From: Chih-En Lin
Date: Fri Apr 14 2023 - 10:28:02 EST


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.

Signed-off-by: Chih-En Lin <shiyn.lin@xxxxxxxxx>
---
kernel/fork.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/kernel/fork.c b/kernel/fork.c
index 0c92f224c68c..8452d5c4eb5e 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -2679,6 +2679,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();

--
2.34.1