From patchwork Fri Feb 10 21:50:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13136518 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 A7FC2C64EC7 for ; Fri, 10 Feb 2023 21:58:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A3854280027; Fri, 10 Feb 2023 16:58:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A0F45280021; Fri, 10 Feb 2023 16:58:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 812EB280028; Fri, 10 Feb 2023 16:58:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 60188280021 for ; Fri, 10 Feb 2023 16:58:02 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3668CA4711 for ; Fri, 10 Feb 2023 21:58:02 +0000 (UTC) X-FDA: 80452745604.01.7853BFE 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 9B78A20003 for ; Fri, 10 Feb 2023 21:58:00 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=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; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1676066280; 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=b/a3zZcwonpyVTXPUun7YKpCdEO8aJBBt3fEU+mRk60=; b=u/5dFUpPl6nkle3KmC7wi39NaqdTUbw82n8+KPufz8FnxlVxNq1riOQEe6hVtLU1L8sErp kf/QfeN2km1oOQCMnIQ3cfz4AWgfD7aDBrruPi8Aym0Ig+dfbWFDuuOsB8FqzCvK48IYxc cygza4YPAEPsWT0mXHhLUicM1LnmmfM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=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; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676066280; a=rsa-sha256; cv=none; b=KHZLXFBlg2w/iTvvgyNQq4/9DubPFldvdQpQgR6UZr39qXNZfGvxW0cqKE3uFdihWLkToJ Mfau0WzSpr3Gs/z8DYoqv2s65Sp29aeFWGt3OIDL1krRbqMZ43aGx5R3A8FZ5fPxfT084v 6pzECJPTYhwCG1vkBIYQfGEVejAIokQ= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id B0F636BFC2ED; Fri, 10 Feb 2023 13:50:33 -0800 (PST) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, linux-mm@kvack.org, riel@surriel.com, mhocko@suse.com, david@redhat.com, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, akpm@linux-foundation.org Subject: [RFC PATCH v2 18/19] selftests/vm: add KSM fork test Date: Fri, 10 Feb 2023 13:50:22 -0800 Message-Id: <20230210215023.2740545-19-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230210215023.2740545-1-shr@devkernel.io> References: <20230210215023.2740545-1-shr@devkernel.io> MIME-Version: 1.0 X-Stat-Signature: 3w3j3iioj85sk6mq4q5mph5sgpi785ar X-Rspam-User: X-Rspamd-Queue-Id: 9B78A20003 X-Rspamd-Server: rspam06 X-HE-Tag: 1676066280-696512 X-HE-Meta: U2FsdGVkX18+UJsAkMqprNO+206haRMulcDFHs5edYeMuXlORpyKxnfE3OfWodFe20wgEY2FrsYAr91Xvaevnc/iOaayouHXloW4/xdrznOJdH0mZYMMLji24RDHG/VUz2za59DljjtIWuQB+wYoSW3/lbxYYc/aGM4nKDxyYaJOZQSImOAKKISwc5UOLFN4NRQyXvpdVz9k+9i4i3kIgc79ewqQiH1Au/Lut7lBxzhvl6gRZGj8H9OGveGq9CUsFXhk0YTjno/Qs2ICIuP33FnbpKQlQqDws4DftFZh5C6zFm3KU0PlGCkPjhmsDdhSYfOtfvj78GMKcW9GaufdYkobocRIJA+WDYCfUB/Uu/hheeSBgcb/okoZC5SHgxFA+CkxqGx9aceDVsd7F+FnZFTOChZDWzo7lLSwQFXU8JTXaC2c+SlxDc/y4+fkgyoJziHJekvGi3bCtzT6orftxKyD7wu9SN2HxTdXRJJ4dtYt8FiI64DRyhsoDPJ+YDIQaniZgi7QbD/ByVhQoEs3HNMyM2zoRkcxWTpdDIqBHDDiPNvLn6Np2VNrEuDPNZp56jblkcwV1Ls16mu4UU4urkA2UoUwE+bRJWGXSX9Hd85mcaZcPYDbT8Ditejd7ZhPS0eEhLwcBJFPlptZ65bEEk03T31f9NHbT7/aUSd3FBhWBdCytKKSfkfL8Q7e3wu7426D/HxORZ33Oo3chtoTIM7wGOIzbIj/FJbxYi0MZuXZUIKJjDDZzOQgpuYEz9JcxV/f4Sa7sRTbxvj1+Sz5e/epBdd1pw48YdqBzGa4WM9YRndwMflvP2XAv70eHnD3+tZsHRkYX4qscmlMW3LyQ+RgfNsjpTziW78YFUxNlgJSC23+03n/pvZQR68lyxQccesUjnD8LS4Qju9lKgZ5U2uUjHGf1DIXeyfDQ7rVgVcbjujjanFo7kR80dzfiHqnwX1usITYeVIC0B7wpej wUwfz3xd cs41d2p5dznIa/govorumE974dek4nc4yepqdTSB6xPE/dw4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add fork test to verify that the MMF_VM_MERGE_ANY flag is inherited by the child process. Signed-off-by: Stefan Roesch --- tools/testing/selftests/mm/ksm_tests.c | 53 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_tests.c b/tools/testing/selftests/mm/ksm_tests.c index 9667cb3b8c6a..a0a48ac43b29 100644 --- a/tools/testing/selftests/mm/ksm_tests.c +++ b/tools/testing/selftests/mm/ksm_tests.c @@ -44,6 +44,7 @@ enum ksm_merge_type { enum ksm_test_name { CHECK_KSM_MERGE, + CHECK_KSM_MERGE_FORK, CHECK_KSM_UNMERGE, CHECK_KSM_GET_MERGE_TYPE, CHECK_KSM_ZERO_PAGE_MERGE, @@ -126,7 +127,8 @@ static void print_help(void) " For this test, the size of duplicated memory area (in MiB)\n" " must be provided using -s option\n" " -C evaluate the time required to break COW of merged pages.\n" - " -G query merge mode\n\n"); + " -G query merge mode\n" + " -F evaluate that the KSM process flag is inherited\n\n"); printf(" -a: specify the access protections of pages.\n" " must be of the form [rwx].\n" @@ -325,6 +327,47 @@ static int check_ksm_merge(int merge_type, int mapping, int prot, return KSFT_FAIL; } +/* Verify that prctl ksm flag is inherited. */ +static int check_ksm_fork(void) +{ + int rc = KSFT_FAIL; + pid_t child_pid; + + if (prctl(PR_SET_MEMORY_MERGE, 1)) { + perror("prctl"); + return KSFT_FAIL; + } + + child_pid = fork(); + if (child_pid == 0) { + int is_on = prctl(PR_GET_MEMORY_MERGE, 0); + + if (!is_on) + exit(KSFT_FAIL); + + exit(KSFT_PASS); + } + + if (child_pid < 0) + goto out; + + if (waitpid(child_pid, &rc, 0) < 0) + rc = KSFT_FAIL; + + if (prctl(PR_SET_MEMORY_MERGE, 0)) { + perror("prctl"); + rc = KSFT_FAIL; + } + +out: + if (rc == KSFT_PASS) + printf("OK\n"); + else + printf("Not OK\n"); + + return rc; +} + static int check_ksm_get_merge_type(void) { if (prctl(PR_SET_MEMORY_MERGE, 1)) { @@ -760,7 +803,7 @@ int main(int argc, char *argv[]) bool merge_across_nodes = KSM_MERGE_ACROSS_NODES_DEFAULT; long size_MB = 0; - while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:t:GMUZNPCHD")) != -1) { + while ((opt = getopt(argc, argv, "ha:p:l:z:m:s:t:FGMUZNPCHD")) != -1) { switch (opt) { case 'a': prot = str_to_prot(optarg); @@ -811,6 +854,9 @@ int main(int argc, char *argv[]) merge_type = atoi(optarg); } break; + case 'F': + test_name = CHECK_KSM_MERGE_FORK; + break; case 'M': break; case 'U': @@ -867,6 +913,9 @@ int main(int argc, char *argv[]) ret = check_ksm_merge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, page_count, ksm_scan_limit_sec, page_size); break; + case CHECK_KSM_MERGE_FORK: + ret = check_ksm_fork(); + break; case CHECK_KSM_UNMERGE: ret = check_ksm_unmerge(merge_type, MAP_PRIVATE | MAP_ANONYMOUS, prot, ksm_scan_limit_sec, page_size);