Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp903651rdb; Tue, 19 Sep 2023 13:53:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHK5MMUw/0nlGFZwnwYCfwFQz2swZpWezcG+0hSaSSym4na0kbOPJiO0+3sg3P+SxPq+Ik0 X-Received: by 2002:a05:6358:7e46:b0:13f:e3eb:53a6 with SMTP id p6-20020a0563587e4600b0013fe3eb53a6mr649061rwm.30.1695156835728; Tue, 19 Sep 2023 13:53:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695156835; cv=none; d=google.com; s=arc-20160816; b=ybj/Zu7g5rdvs2n2miUSpbticTIT2WAqHWhm2bpg3LZpobokNKcS93yDFJTUHuYTnB FzBMpQbGbYqu5PxD6Bm+LGQRUv+Q2TaMMe6LmhxG7a6+AWkV2ed2y4SVE6Mtp7wCBmY2 cH1v255jydhG7eB4o8bQWh288Y0cMjQu9ZgmdGeyeMJeXrvUZ3bMQ7lq1qzTEkODQa75 ZjlYjnYWHLBCySIBHngybyN3pmZ0w/avBVsmaJBlZoVkC2exDURRRiDo6Nn3JhZWu3Y6 oVVMmr6lBlvAp2MDzPbsXV/EcHcamNZ8txCNBrn81fMfO3JyY5SM2aCKSKnv9g8OPWB5 Jctg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=d0iupzJ3mlaND78YmykYesWS4YSwbA5I10u5bAOpH3Q=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=fds/z/kge/EAemLQl9ry+ciCNTk27fZi3SoX3uVFOfbvcAVlFzPlTEk/7vNgQ1JJuO Nu7Vg4bBslxvHW3y4iT1l3I8JewzKZo/JinBqI+4At7jHn5s6uazhqVwecuzHJrmURnh SS5V39aH0EtMDFfo1dksENZvIuCnHcw2CIM5HcRycoyeeRTl8TCJwySb1YhoarFMoVEs pP7Ez19z5HJwtzJFv2JJNgU8nxk9LruVHW1KiGr0SWclYrLAq4gcp5cPTbrs/jdFCsLc 0XTJjMETWBiUMQOI7GPqyEMEoK+z7ryEjAq99z0cSNsbj+Q1ZiWMiVP1Y3SMCYUwQgLy 9ErA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id 6-20020a630a06000000b00577f67a0614si10065340pgk.879.2023.09.19.13.53.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 13:53:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id A9E6A825B510; Tue, 19 Sep 2023 13:52:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233273AbjISUw0 (ORCPT + 99 others); Tue, 19 Sep 2023 16:52:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233222AbjISUwV (ORCPT ); Tue, 19 Sep 2023 16:52:21 -0400 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01CD4B3 for ; Tue, 19 Sep 2023 13:52:15 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 19 Sep 2023 13:52:48 -0700 (PDT) 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/selftest= s/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 +=3D soft-dirty endif =20 +TEST_GEN_PROGS +=3D ksm_fork_exec_child + ifeq ($(ARCH),x86_64) CAN_BUILD_I386 :=3D $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_= 32bit_program.c -m32) CAN_BUILD_X86_64 :=3D $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivia= l_64bit_program.c) diff --git a/tools/testing/selftests/mm/ksm_fork_exec_child.c b/tools/tes= ting/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 =3D prctl(68, 0, 0, 0, 0); + exit(ksm =3D=3D 1 ? 0 : 1); +} diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/te= sting/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"); } =20 +static void test_prctl_fork_exec(void) +{ + int ret, status; + pid_t child_pid; + + ksft_print_msg("[RUN] %s\n", __func__); + + ret =3D prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); + if (ret < 0 && errno =3D=3D EINVAL) { + ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); + return; + } else if (ret) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=3D1 failed\n"); + return; + } + + child_pid =3D fork(); + if (child_pid =3D=3D -1) { + ksft_test_result_skip("fork() failed\n"); + return; + } else if (child_pid =3D=3D 0) { + char *filename =3D "./ksm_fork_exec_child"; + char *argv_for_program[] =3D { filename, NULL }; + + execv(filename, argv_for_program);; + } else { + if (waitpid(child_pid, &status, 0) > 0) { + if (WIFEXITED(status)) { + status =3D 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 =3D 2 * MiB; @@ -536,7 +583,7 @@ static void test_prot_none(void) =20 int main(int argc, char **argv) { - unsigned int tests =3D 7; + unsigned int tests =3D 8; int err; =20 #ifdef __NR_userfaultfd @@ -576,6 +623,7 @@ int main(int argc, char **argv) =20 test_prctl(); test_prctl_fork(); + test_prctl_fork_exec(); test_prctl_unmerge(); =20 err =3D ksft_get_fail_cnt(); --=20 2.39.3