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

Chih-En Lin
2023-02-07T03:55:31Z

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@gmail.com>
---
kernel/fork.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/kernel/fork.c b/kernel/fork.c
index 9f7fe3541897..94c35c8b31b1 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -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();

--
2.34.1