From patchwork Tue Sep 19 20:51:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13391871 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A415ACE79AA for ; Tue, 19 Sep 2023 20:52:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4513E6B00D7; Tue, 19 Sep 2023 16:52:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B3F36B00D8; Tue, 19 Sep 2023 16:52:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B82F6B00D9; Tue, 19 Sep 2023 16:52:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 005546B00D7 for ; Tue, 19 Sep 2023 16:52:16 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C837F8068C for ; Tue, 19 Sep 2023 20:52:16 +0000 (UTC) X-FDA: 81254544672.04.F0567E1 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf13.hostedemail.com (Postfix) with ESMTP id 229C920032 for ; Tue, 19 Sep 2023 20:52:14 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695156735; a=rsa-sha256; cv=none; b=fF5Zu3RqwXOm3E1iATNpSAiHgj7NWUrbXw0j0cXVPOnYbIsuTd11ZMWHDiyR0hqxB2RFL6 0gq2/+szmEaii00dojqd4YkkqOeQ6ELmbGMmbM+hJ1J4bhEP9hZ73UCL7HkLNHiVA4tucD EKIXxOgO+aZ5qb3+S0VbiXOD2AVKGSM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695156735; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d0iupzJ3mlaND78YmykYesWS4YSwbA5I10u5bAOpH3Q=; b=kJj0NIb/Dw88gIgWrQDM0fDOrXMPDzKaZElPcXyeTPKRhh9W9//Tq3pnvzi2JLSC3UZ563 ZJ8hlCHW9TZC9djxoFqdVVGJLIuQ85cINtHZQkkgMGhCDVHUgH5H8EXXOA1rIauVGM/EGW Gza6CqcLIWLQ1smrLBSw0qGNw7UUn+I= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 5FCBEC361C1F; Tue, 19 Sep 2023 13:51:59 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 2/2] mm/ksm: Test case for prctl fork/exec workflow Date: Tue, 19 Sep 2023 13:51:58 -0700 Message-Id: <20230919205158.1897353-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230919205158.1897353-1-shr@devkernel.io> References: <20230919205158.1897353-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam08 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QCI80hM ZbuxIyEK+t9eBqo0d1Mai2TfJiGZS90eRN9PF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- 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 + 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 +#include + +int main() +{ + /* Test if KSM is enabled for the process. */ + int ksm = prctl(68, 0, 0, 0, 0); + 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);; + } 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"); +} + static void test_prctl_unmerge(void) { const unsigned int size = 2 * MiB; @@ -536,7 +583,7 @@ static void test_prot_none(void) int main(int argc, char **argv) { - unsigned int tests = 7; + unsigned int tests = 8; int err; #ifdef __NR_userfaultfd @@ -576,6 +623,7 @@ int main(int argc, char **argv) test_prctl(); test_prctl_fork(); + test_prctl_fork_exec(); test_prctl_unmerge(); err = ksft_get_fail_cnt();