Skip to content

Commit

Permalink
mm: Allow user to control COW PTE via prctl
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Chih-En Lin <shiyn.lin@gmail.com>
  • Loading branch information
linD026 authored and intel-lab-lkp committed Dec 20, 2022
1 parent b7b275e commit fd7a502
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion include/linux/sched/coredump.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
6 changes: 6 additions & 0 deletions include/uapi/linux/prctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
11 changes: 11 additions & 0 deletions kernel/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -2348,6 +2348,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)
{
Expand Down Expand Up @@ -2626,6 +2634,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;
Expand Down

0 comments on commit fd7a502

Please sign in to comment.