From: David Hildenbrand <david@redhat.com>
To: Stefan Roesch <shr@devkernel.io>, kernel-team@fb.com
Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, riel@surriel.com,
linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCH v1 2/2] mm/ksm: Test case for prctl fork/exec workflow
Date: Wed, 20 Sep 2023 15:16:38 +0200 [thread overview]
Message-ID: <daf57da6-b22d-bdfb-c6f0-0ac07824ab72@redhat.com> (raw)
In-Reply-To: <20230919205158.1897353-3-shr@devkernel.io>
On 19.09.23 22:51, Stefan Roesch wrote:
> 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 <shr@devkernel.io>
> ---
> 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
>
> diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
> index 6a9fc5693145..9ab6aa402544 100644
> --- a/tools/testing/selftests/mm/Makefile
> +++ b/tools/testing/selftests/mm/Makefile
> @@ -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.
> +
> 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)
> diff --git a/tools/testing/selftests/mm/ksm_fork_exec_child.c b/tools/testing/selftests/mm/ksm_fork_exec_child.c
> new file mode 100644
> index 000000000000..298439f0d55f
> --- /dev/null
> +++ b/tools/testing/selftests/mm/ksm_fork_exec_child.c
> @@ -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.
> + exit(ksm == 1 ? 0 : 1);
> +}
> diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c
> index 901e950f9138..4dc0bb522c07 100644
> --- a/tools/testing/selftests/mm/ksm_functional_tests.c
> +++ b/tools/testing/selftests/mm/ksm_functional_tests.c
> @@ -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");
> + 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")
> + } 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");
> + 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.
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2023-09-20 13:16 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-19 20:51 [PATCH v1 0/2] mm/ksm: add fork-exec support for prctl Stefan Roesch
2023-09-19 20:51 ` [PATCH v1 1/2] mm/ksm: support fork/exec " Stefan Roesch
2023-09-20 10:00 ` Carl Klemm
2023-09-20 13:11 ` David Hildenbrand
2023-09-20 16:29 ` Stefan Roesch
2023-09-19 20:51 ` [PATCH v1 2/2] mm/ksm: Test case for prctl fork/exec workflow Stefan Roesch
2023-09-20 13:16 ` David Hildenbrand [this message]
2023-09-20 18:57 ` Stefan Roesch
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=daf57da6-b22d-bdfb-c6f0-0ac07824ab72@redhat.com \
--to=david@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=riel@surriel.com \
--cc=shr@devkernel.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox