Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2982529rdb; Fri, 22 Sep 2023 14:13:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHv7Wyz15zII76/rxAstNgo6D3sGgWloui5YT2Z44RaIesIHG4I1NwGL0acEFfEwoDYFZb2 X-Received: by 2002:a17:90a:7ac8:b0:276:79b6:8bd6 with SMTP id b8-20020a17090a7ac800b0027679b68bd6mr844488pjl.15.1695417195629; Fri, 22 Sep 2023 14:13:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695417195; cv=none; d=google.com; s=arc-20160816; b=LqrnVe4s4u9IWHZeNnkOC3eGJOncaXQ1T7fjxFMipbuYcIHuJi9vyZKrKsKMuRaE+A tafN1Ue6l7tbwCbpYl0M8ik0TKfvsoUnEWEFP9rjt8S7UC5MB12qmgfJMMHFlkx1f0J/ CGnlxOhj+t8Nk/7Tyettjr4iSDMd488kxT1h2vsUuBl7Ox1NRCPSs5l6JsXvoTdW1FQ1 Ypg8zZobv1ACnSY/y00ywsbLlCacVuFZfWpZLkBaPREbOTDohz901x6b+yS/tyFEofuD DVHB/y/n/X0LS+Gl9aDDPs6uHtlU4QmGHmhYgilpGKFWIZusZDGHF/qGoZ4lvhjilGsu 4LdA== 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=ivbfTSqiGJDKhzmVN6iFRasV8Zko5oH9CCWYtxUwt+M=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=lQAqMjvx6ya5moLmqKpH7XkU/Vj9UhPa0YJkHQWT60RKDR1m01kt1r3RdHvVv+Ywgx gsT4BOc9IzodNQk88NieOLb7ly8X2v0Md/1xiazf00/knc/9KY+LrusNXZoLzzkJCwOE 0OAHjhM0v1Zcj9jQwj/JHRfsaZ+xYZBY0agtmu39kfqHz1ME79115fyrYTcc0oun7buO TdndnhEC7FEKjRiLW/0FfLnOVJfY+1C2ir+vpjcv1efeg6RSzCVPOB9MzBRok3gp9Ygz zAiN2BSTBBZDOqUBrETgsq5q63JSPg6+tubC58V9UjIgSlPE+rLcbCevUScqurHD0TyJ 0N5g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id ce14-20020a17090aff0e00b00263dcd605a9si6613138pjb.25.2023.09.22.14.13.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 14:13:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id 0BB6C81F3470; Fri, 22 Sep 2023 14:12:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229887AbjIVVMQ (ORCPT + 99 others); Fri, 22 Sep 2023 17:12:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229798AbjIVVMK (ORCPT ); Fri, 22 Sep 2023 17:12:10 -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 CEF8BE8 for ; Fri, 22 Sep 2023 14:12:03 -0700 (PDT) Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 9BCBFC60DC4E; Fri, 22 Sep 2023 14:11:50 -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 v4 2/2] mm/ksm: Test case for prctl fork/exec workflow Date: Fri, 22 Sep 2023 14:11:41 -0700 Message-Id: <20230922211141.320789-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230922211141.320789-1-shr@devkernel.io> References: <20230922211141.320789-1-shr@devkernel.io> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Fri, 22 Sep 2023 14:12:29 -0700 (PDT) X-Spam-Level: ** 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 | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/te= sting/selftests/mm/ksm_functional_tests.c index 901e950f9138..fbff0dd09191 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -26,6 +26,7 @@ =20 #define KiB 1024u #define MiB (1024 * KiB) +#define FORK_EXEC_CHILD_PRG_NAME "ksm_fork_exec_child" =20 static int mem_fd; static int ksm_fd; @@ -479,6 +480,64 @@ static void test_prctl_fork(void) ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } =20 +static int ksm_fork_exec_child(void) +{ + /* Test if KSM is enabled for the process. */ + return prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) =3D=3D 1; +} + +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 *prg_name =3D "./ksm_functional_tests"; + char *argv_for_program[] =3D { 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 =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; + } + + if (prctl(PR_SET_MEMORY_MERGE, 0, 0, 0, 0)) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=3D0 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,9 +595,13 @@ 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 + if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) { + exit(ksm_fork_exec_child() =3D=3D 1 ? 0 : 1); + } + #ifdef __NR_userfaultfd tests++; #endif @@ -576,6 +639,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