From patchwork Wed Sep 20 19:01:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13393220 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 C4077C04FEB for ; Wed, 20 Sep 2023 19:01:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 375B66B0196; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 325336B0197; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1ED4A6B0198; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0E1996B0196 for ; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C89D280DB2 for ; Wed, 20 Sep 2023 19:01:39 +0000 (UTC) X-FDA: 81257894718.09.9FA874E Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf07.hostedemail.com (Postfix) with ESMTP id 0759E40036 for ; Wed, 20 Sep 2023 19:01:37 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.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=1695236498; 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=KIinxHgd3W5A5KZig7FsyZOy3C3CzjLyJqwrfF5qVT8=; b=vlE158Dd8LbOG4d9M1OMnS6iDTdEewEhhWAyGUg1FcvD+1++UwpE58XqoVLqPt+nYQWKav 0lomrcnwm6KJiz1leGDP+hSxcZ2LMuUDqX8vJPUGGAshPmWrOkBxpkUXXPRcQuYEsrZydK ONvdPGJddIx5lqCZAu7i+mNgiXo/s/Q= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.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=1695236498; a=rsa-sha256; cv=none; b=lKdQOdo2xikbZpmpuZRwq0DVoRpkxGWdGa30RtJnAncMK0KAS0pZOI/qUSrsIIUEy/v3to OUTW+J/WfXXiCXFFA2D8Y+fQGVKMknqW9xldEtyEEMkAC32ZHK+2e0aq0jqgE74fVwnJua vCI4u18/zAli3FESxu8d9Ct1nHlwdbk= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 3BE9EC4363F8; Wed, 20 Sep 2023 12:01:22 -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 v2 2/2] mm/ksm: Test case for prctl fork/exec workflow Date: Wed, 20 Sep 2023 12:01:17 -0700 Message-Id: <20230920190117.784151-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230920190117.784151-1-shr@devkernel.io> References: <20230920190117.784151-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0759E40036 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ddomjswku1zs96me5ircyfkfsdbu473n X-HE-Tag: 1695236497-548102 X-HE-Meta: U2FsdGVkX1+BD3X26iw0JJVs/x42OzKoadCoVuju8MEppTxk0XX23cAtKB63plvAL1AW1OmySGe3X5ObXp6i0bj1xgvElPMdIgwlhYeq5AQO3oADTD4aVxrxsHoISfTUSHf9rWCq6WWFn6sQU4hQygh1yjEfvzL5ho639TWja70g9lXuP85JPf4NZGlMcU2pi4AtznYjR/pEvPgKg+CJpXxcof4CfgOgW+D5Yg7GNXwa/O8y0LcXWfnjsh4rWatiJuYdDxmY25skwx1h7ZVXpieKq4wBzwYj5BfW0OvPhdWsR/74b2V1omG6GEFhMU1M9VLFNFktljOPm87cch8yfS4adYBS5RAwfdnLeQqUAAPRqSeotZuwPpUa+Y8oBlxuKgtg0oGx2Sg1U/sM97c3YBl3G/UCFk81cDrH46zFmOXT5OwtRQgYdoVj7e0fDr4zZJAutrbpxdU50M34hkTiFI1fdckMxriD5VYyFSuA9fJl4LLYRuFY4Hdae6sV124rWw/pq7UpjT2jPYUKwBg4rhvDu5bZSvtLyPqd4C/SsYCOONyCkWa0IQOmWyw/K+zNyNURHien7sQd9x46N8/72jZTiqDDeaUdKdlUEzGidGpY6+PpVovYgpR+1fq0BgAGEYSAsx59ujQrNiNqW42JWhCo9x7rJ97yv9X0m0rJID2a+42wmobHD/gTDPXRH9txc3cQAT41b2DfEOFm9icGFsaPMDqe7PQ7bXrGbfuAVE32Ugff+JpKitMfTCkLC1y2jmqe8cNKnNHTGgzs5DlVDwjM7wOfwvATYy7IPaZpznrzhnO87GUNgD06ohq+w2xD8/wtqb9bOdeaovSgB++BVemi5IXzjsD7VZqJUvDgAJjNllOyADUE3u2F0uEr4ypThL4uYObddw3DA1OATURGWlIXH0golmymnMkpb+neljLpUenB5+iezlXQYEGMnTnznFFDTqMLYE54Ntj+1a1 b2i1Iyy/ GwiGJ2BJrEs7k4liBQ2c5wjLsd1dvBL9eRpsj X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, 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 Reviewed-by: David Hildenbrand --- .../selftests/mm/ksm_functional_tests.c | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 901e950f9138..40b86c9caf3a 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -26,6 +26,7 @@ #define KiB 1024u #define MiB (1024 * KiB) +#define FORK_EXEC_CHILD_PRG_NAME "ksm_fork_exec_child" static int mem_fd; static int ksm_fd; @@ -479,6 +480,65 @@ static void test_prctl_fork(void) ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } +static int ksm_fork_exec_child(void) +{ + /* Test if KSM is enabled for the process. */ + int ksm = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0); + return ksm == 1; +} + +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 *prg_name = "./ksm_functional_tests"; + char *argv_for_program[] = { prg_name, FORK_EXEC_CHILD_PRG_NAME }; + + execv(prg_name, argv_for_program); + return; + } + + 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; + } + + if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=0 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,9 +596,13 @@ static void test_prot_none(void) int main(int argc, char **argv) { - unsigned int tests = 7; + unsigned int tests = 8; int err; + if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) { + exit(ksm_fork_exec_child() == 1 ? 0 : 1); + } + #ifdef __NR_userfaultfd tests++; #endif @@ -576,6 +640,7 @@ int main(int argc, char **argv) test_prctl(); test_prctl_fork(); + test_prctl_fork_exec(); test_prctl_unmerge(); err = ksft_get_fail_cnt();