mm/ksm: add fork-exec support for prctl
Subsystems (3)
cover mm/ksm: add fork-exec support for prctl
A process can enable KSM with the prctl system call. When the process is forked the KSM flag is inherited by the child process. However if the process is executing an exec system call directly after the fork, the KSM setting is cleared. This patch series addresses this problem.
1) Change the mask in coredump.h for execing a new process
2) Add a new test case in ksm_functional_tests
Stefan Roesch (2):
mm/ksm: support fork/exec for prctl
mm/ksm: Test case for prctl fork/exec workflow
include/linux/sched/coredump.h | 7 ++- tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/ksm_fork_exec_child.c | 9 ++++ .../selftests/mm/ksm_functional_tests.c | 50 ++++++++++++++++++- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/mm/ksm_fork_exec_child.c
base-commit: 15bcc9730fcd7526a3b92eff105d6701767a53bb
Signature · 1 line
2.39.3
PATCH 1/2 mm/ksm: support fork/exec for prctl
A process can enable KSM with the prctl system call. When the process is forked the KSM flag is inherited by the child process. However if the process is executing an exec system call directly after the fork, the KSM setting is cleared. This patch addresses this problem.
--- include/linux/sched/coredump.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/sched/coredump.h +5 -2
@@ -87,8 +87,11 @@ static inline int get_dumpable(struct mm_struct *mm)#define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) +#define MMF_VM_MERGE_ANY 29 +#define MMF_VM_MERGE_ANY_MASK (1 << MMF_VM_MERGE_ANY) +#define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ - MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK) + MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ + MMF_VM_MERGE_ANY_MASK) -#define MMF_VM_MERGE_ANY 29#endif /* _LINUX_SCHED_COREDUMP_H */
Signature · 1 line
2.39.3
Re: [PATCH v1 1/2] mm/ksm: support fork/exec for prctl
Quoted 27 lines · Stefan Roesch
A process can enable KSM with the prctl system call. When the process is
forked the KSM flag is inherited by the child process. However if the process is executing an exec system call directly after the fork, the KSM setting is cleared. This patch addresses this problem.Signed-off-by Stefan Roesch <[email protected]>Reported-by Carl Klemm <[email protected]>---
include/linux/sched/coredump.h | 7 +++++--1 file changed, 5 insertions(+), 2 deletions(-)include/linux/sched/coredump.h +0
-0@@ -87,8 +87,11 @@ static inline int get_dumpable(struct mm_struct *mm)
#define MMF_DISABLE_THP_MASK (1 <<MMF_DISABLE_THP)include/linux/sched/coredump.h +3
-0+#define MMF_VM_MERGE_ANY 29 +#define MMF_VM_MERGE_ANY_MASK (1 << MMF_VM_MERGE_ANY) +#define MMF_INIT_MASK (MMF_DUMPABLE_MASK |
MMF_DUMP_FILTER_MASK|\
Quoted 8 lines
-
MMF_DISABLE_THP_MASK|MMF_HAS_MDWE_MASK)
+MMF_DISABLE_THP_MASK|MMF_HAS_MDWE_MASK|\
+MMF_VM_MERGE_ANY_MASK)
-#define MMF_VM_MERGE_ANY 29
#endif /*_LINUX_SCHED_COREDUMP_H*/
I can confirm that this patch works as intended for my usecase, you can have my:
Re: [PATCH v1 1/2] mm/ksm: support fork/exec for prctl
In reply to Stefan RoeschQuoted 19 lines · Stefan Roesch
A process can enable KSM with the prctl system call. When the process is forked the KSM flag is inherited by the child process. However if the process is executing an exec system call directly after the fork, the KSM setting is cleared. This patch addresses this problem.
Signed-off-by Stefan Roesch <[email protected]>Reported-by Carl Klemm <[email protected]>--- include/linux/sched/coredump.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)include/linux/sched/coredump.h +0
-0@@ -87,8 +87,11 @@ static inline int get_dumpable(struct mm_struct *mm)#define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP)
include/linux/sched/coredump.h static inline int get_dumpable(struct mm_struct *mm) · line 97
Quoted 10 lines
include/linux/sched/coredump.h +5
-2+#define MMF_VM_MERGE_ANY 29 +#define MMF_VM_MERGE_ANY_MASK (1 << MMF_VM_MERGE_ANY) +#define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ - MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK) + MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ + MMF_VM_MERGE_ANY_MASK) -#define MMF_VM_MERGE_ANY 29#endif /* _LINUX_SCHED_COREDUMP_H */
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 1/2] mm/ksm: support fork/exec for prctl
Quoted 19 lines · David Hildenbrand
Quoted 18 lines · Stefan Roesch
A process can enable KSM with the prctl system call. When the process is forked the KSM flag is inherited by the child process. However if the process is executing an exec system call directly after the fork, the KSM setting is cleared. This patch addresses this problem.
Signed-off-by Stefan Roesch <[email protected]>Reported-by Carl Klemm <[email protected]>--- include/linux/sched/coredump.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)include/linux/sched/coredump.h +2
-0@@ -87,8 +87,11 @@ static inline int get_dumpable(struct mm_struct *mm)#define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) +#define MMF_VM_MERGE_ANY 29 +#define MMF_VM_MERGE_ANY_MASK (1 << MMF_VM_MERGE_ANY) +
Quoted 9 lines
include/linux/sched/coredump.h static inline int get_dumpable(struct mm_struct *mm) · line 97
Quoted 6 lines
include/linux/sched/coredump.h +2
-1#define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ - MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK) + MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ + MMF_VM_MERGE_ANY_MASK) -#define MMF_VM_MERGE_ANY 29#endif /* _LINUX_SCHED_COREDUMP_H */ Fixes tag?
Added fixes tag
Quoted 1 line
Reviewed-by David Hildenbrand <[email protected]>
PATCH 2/2 mm/ksm: Test case for prctl fork/exec workflow
In reply to Stefan RoeschThis adds a new test case to the ksm functional tests to make sure that the KSM setting is inherited by the child process when doing a fork/exec.
--- tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/ksm_fork_exec_child.c | 9 ++++ .../selftests/mm/ksm_functional_tests.c | 50 ++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/mm/ksm_fork_exec_child.c
tools/testing/selftests/mm/Makefile +2 -0
@@ -73,6 +73,8 @@ ifneq ($(ARCH),arm64) TEST_GEN_PROGS += soft-dirty endif +TEST_GEN_PROGS += ksm_fork_exec_child + ifeq ($(ARCH),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_64bit_program.c)
tools/testing/selftests/mm/ksm_fork_exec_child.c new file +9 -0
@@ -0,0 +1,9 @@ +#include <sys/prctl.h> +#include <stdlib.h> + +int main () +{ +/* Test if KSM is enabled for the process. */ +int ksm =prctl (68 ,0 ,0 ,0 ,0 ); +exit (ksm ==1 ?0 :1 ); +}
tools/testing/selftests/mm/ksm_functional_tests.c +49 -1
68 lines not on this page · raw message
Signature · 1 line
2.39.3
Re: [PATCH v1 2/2] mm/ksm: Test case for prctl fork/exec workflow
Quoted 17 lines · Stefan Roesch
This adds a new test case to the ksm functional tests to make sure that the KSM setting is inherited by the child process when doing a fork/exec.
Signed-off-by Stefan Roesch <[email protected]>--- tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/ksm_fork_exec_child.c | 9 ++++ .../selftests/mm/ksm_functional_tests.c | 50 ++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/mm/ksm_fork_exec_child.ctools/testing/selftests/mm/Makefile +0
-0
tools/testing/selftests/mm/Makefile ifneq ($(ARCH),arm64) · line 76
Quoted 4 lines
tools/testing/selftests/mm/Makefile +1
-0@@ -73,6 +73,8 @@ ifneq ($(ARCH),arm64) TEST_GEN_PROGS += soft-dirty endif +TEST_GEN_PROGS += ksm_fork_exec_child
It's not a test itself, so it shouldn't be run when running all tests. See below.
Quoted 10 lines
tools/testing/selftests/mm/Makefile +1
-0+ ifeq ($(ARCH),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_64bit_program.c)tools/testing/selftests/mm/ksm_fork_exec_child.c new file +0
-0
tools/testing/selftests/mm/ksm_fork_exec_child.c line 7
Quoted 7 lines
tools/testing/selftests/mm/ksm_fork_exec_child.c new file +7
-0@@ -0,0 +1,9 @@ +#include <sys/prctl.h> +#include <stdlib.h> + +int main () +{ +/* Test if KSM is enabled for the process. */ +int ksm =prctl (68 ,0 ,0 ,0 ,0 );
Can we use the define from a header? (PR_SET_MEMORY_MERGE)
I was wondering if we could simply exec() ourself (same binary), but pass a special cmdline argument. Then you don't have to build a separate binary.
Just special-case in main() on that argument and perform this check.
Quoted 25 lines
tools/testing/selftests/mm/ksm_fork_exec_child.c new file +2
-0+exit (ksm ==1 ?0 :1 ); +}tools/testing/selftests/mm/ksm_functional_tests.c +15
-0@@ -479,6 +479,53 @@ static void test_prctl_fork(void)ksft_test_result_pass ("PR_SET_MEMORY_MERGE value is inherited\n" ); } +static void test_prctl_fork_exec (void ) +{ +int ret, status; +pid_t child_pid; + +ksft_print_msg ("[RUN] %s\n" , __func__); + + ret =prctl (PR_SET_MEMORY_MERGE ,1 ,0 ,0 ,0 ); +if (ret <0 && errno ==EINVAL ) { +ksft_test_result_skip ("PR_SET_MEMORY_MERGE not supported\n" ); +return ; + }else if (ret) { +ksft_test_result_fail ("PR_SET_MEMORY_MERGE=1 failed\n" ); +return ; + }
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 506
Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +10
-0+ + child_pid =fork (); +if (child_pid ==-1 ) { +ksft_test_result_skip ("fork() failed\n" ); +return ; + }else if (child_pid ==0 ) { +char *filename ="./ksm_fork_exec_child" ; +char *argv_for_program[] = { filename,NULL }; + +execv (filename, argv_for_program);;
s/;;/;/
Add a return; so you can simplify the code below (no need for the "else")
Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +10
-0+ }else { +if (waitpid (child_pid, &status,0 ) >0 ) { +if (WIFEXITED (status)) { + status =WEXITSTATUS (status); +if (status) { +ksft_test_result_fail ("KSM not enabled\n" ); +return ; + } + + }else {
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 526
Quoted 10 lines
tools/testing/selftests/mm/ksm_functional_tests.c +10
-0+ksft_test_result_fail ("program didn't terminate normally\n" ); +return ; + } + }else { +ksft_test_result_fail ("waitpid() failed\n" ); +return ; + } + } + +ksft_test_result_pass ("PR_SET_MEMORY_MERGE value is inherited\n" );
It's probably the cleanest to disable PR_SET_MEMORY_MERGE again when returning form this function, so the other tests have a clean slate.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 2/2] mm/ksm: Test case for prctl fork/exec workflow
Quoted 35 lines · David Hildenbrand
Quoted 16 lines · Stefan Roesch
This adds a new test case to the ksm functional tests to make sure that the KSM setting is inherited by the child process when doing a fork/exec.
Signed-off-by Stefan Roesch <[email protected]>--- tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/ksm_fork_exec_child.c | 9 ++++ .../selftests/mm/ksm_functional_tests.c | 50 ++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-)create mode 100644
tools/testing/selftests/mm/ksm_fork_exec_child.ctools/testing/selftests/mm/Makefile +0
-0tools/testing/selftests/mm/Makefile ifneq ($(ARCH),arm64) · line 75
Quoted 3 lines
tools/testing/selftests/mm/Makefile +0
-0@@ -73,6 +73,8 @@ ifneq ($(ARCH),arm64) TEST_GEN_PROGS += soft-dirty endif +TEST_GEN_PROGS += ksm_fork_exec_childIt's not a test itself, so it shouldn't be run when running all tests. See below.
Quoted 11 lines
tools/testing/selftests/mm/Makefile +1
-0+ ifeq ($(ARCH),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_64bit_program.c)tools/testing/selftests/mm/ksm_fork_exec_child.c new file +1
-0@@ -0,0 +1,9 @@ +#include <sys/prctl.h>
Quoted 9 lines
tools/testing/selftests/mm/ksm_fork_exec_child.c line 7
Quoted 6 lines
tools/testing/selftests/mm/ksm_fork_exec_child.c new file +6
-0+#include <stdlib.h> + +int main () +{ +/* Test if KSM is enabled for the process. */ +int ksm =prctl (68 ,0 ,0 ,0 ,0 );Can we use the define from a header? (
PR_SET_MEMORY_MERGE)
I changed it.
Quoted 3 lines
I was wondering if we could simply
exec()ourself (same binary), but pass a special cmdline argument. Then you don't have to build a separate binary.
I'm execing the same executable with an additional parameter.
Quoted 2 lines
Just special-case in
main()on that argument and perform this check.
I added a check in main and invoke a new function for the child.
Quoted 28 lines
Quoted 28 lines
tools/testing/selftests/mm/ksm_fork_exec_child.c new file +2
-0+exit (ksm ==1 ?0 :1 ); +}tools/testing/selftests/mm/ksm_functional_tests.c +18
-0@@ -479,6 +479,53 @@ static void test_prctl_fork(void)ksft_test_result_pass ("PR_SET_MEMORY_MERGE value is inherited\n" ); } +static void test_prctl_fork_exec(void) +{ +int ret, status; +pid_t child_pid; + +ksft_print_msg ("[RUN] %s\n" , __func__); + + ret =prctl (PR_SET_MEMORY_MERGE ,1 ,0 ,0 ,0 ); +if (ret <0 && errno ==EINVAL ) { +ksft_test_result_skip ("PR_SET_MEMORY_MERGE not supported\n" ); +return ; + }else if (ret) { +ksft_test_result_fail ("PR_SET_MEMORY_MERGE=1 failed\n" ); +return ; + } + + child_pid =fork (); +if (child_pid ==-1 ) { +ksft_test_result_skip ("fork() failed\n" );
Quoted 9 lines
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 506
Quoted 6 lines
tools/testing/selftests/mm/ksm_functional_tests.c +6
-0+return ; + }else if (child_pid ==0 ) { +char *filename ="./ksm_fork_exec_child" ; +char *argv_for_program[] = { filename,NULL }; + +execv (filename, argv_for_program);;s/;;/;/
Fixed.
Quoted 2 lines
Add a return; so you can simplify the code below (no need for the "else")
Done.
Quoted 15 lines
Quoted 15 lines
tools/testing/selftests/mm/ksm_functional_tests.c +15
-0+ }else { +if (waitpid (child_pid, &status,0 ) >0 ) { +if (WIFEXITED (status)) { + status =WEXITSTATUS (status); +if (status) { +ksft_test_result_fail ("KSM not enabled\n" ); +return ; + } + + }else { +ksft_test_result_fail ("program didn't terminate normally\n" ); +return ; + } + }else { +ksft_test_result_fail ("waitpid() failed\n" );
Quoted 8 lines
tools/testing/selftests/mm/ksm_functional_tests.c static void test_prctl_fork(void) · line 526
Quoted 5 lines
tools/testing/selftests/mm/ksm_functional_tests.c +5
-0+return ; + } + } + +ksft_test_result_pass ("PR_SET_MEMORY_MERGE value is inherited\n" );It's probably the cleanest to disable
PR_SET_MEMORY_MERGEagain when returning form this function, so the other tests have a clean slate.
In the next version it is disabled at the end.