From patchwork Mon Jan 23 17:37:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13112654 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 B5FDFC05027 for ; Mon, 23 Jan 2023 17:38:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B9416B009A; Mon, 23 Jan 2023 12:38:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6F40B6B0099; Mon, 23 Jan 2023 12:38:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 595116B009A; Mon, 23 Jan 2023 12:38:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 42C4E6B0098 for ; Mon, 23 Jan 2023 12:38:47 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 194DCC179E for ; Mon, 23 Jan 2023 17:38:47 +0000 (UTC) X-FDA: 80386773894.10.C72FC6C Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf11.hostedemail.com (Postfix) with ESMTP id 71B2A4000B for ; Mon, 23 Jan 2023 17:38:45 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; spf=neutral (imf11.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=1674495525; 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=3bAJwifag69+je0WBSnlVAQ9+kANBDi2Pdxva4sytJM=; b=TQGxRxP9O/Rw3SQ1JC96vTipyfsqw4kaeUivewOVOIspbfVY5fs330BXVtGNSJc21ASCgQ uKbGYouunSi7OxiRij3B3BlLkOT6i3oW/Uym3NvhgITXy1R+XJASoUVIvoQBwcv0mLGXMX b43qGAvrUpgXg2Wy4p6z1axqfcsZDto= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; spf=neutral (imf11.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=1674495525; a=rsa-sha256; cv=none; b=P5zLA7ImVhcPJ6gTgXtZVNDJtLcgRWeJQd8kLXtcm0HFIb0wUqeFvcZ0xk+7jvBw1Z4r6C vYD4J5a8q3bg7ywzX76G+12se6+oFhjZ1mHQlRYeh2JSR5OX8vjIk3dQbkOfzCA4EDNvek I39SLm/UZRWuQABEqqlorM0lAJg/IOM= Received: by dev0134.prn3.facebook.com (Postfix, from userid 425415) id 8CE0B5616BF9; Mon, 23 Jan 2023 09:37:56 -0800 (PST) From: Stefan Roesch To: linux-mm@kvack.org Cc: shr@devkernel.io, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org Subject: [RESEND RFC PATCH v1 19/20] selftests/vm: add KSM fork test Date: Mon, 23 Jan 2023 09:37:47 -0800 Message-Id: <20230123173748.1734238-20-shr@devkernel.io> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230123173748.1734238-1-shr@devkernel.io> References: <20230123173748.1734238-1-shr@devkernel.io> MIME-Version: 1.0 X-Stat-Signature: x5d1dm5388o6h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nTg9hd2 2AnQrcHC AXdrdsUMZB+KXaoNik2pslAtrpR19CN4Z05Omzf9BUTWf4B7nsuTHd1pKBw== 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/vm/ksm_tests.c | 53 +++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vm/ksm_tests.c b/tools/testing/selftests/vm/ksm_tests.c index 9667cb3b8c6a..a0a48ac43b29 100644 --- a/tools/testing/selftests/vm/ksm_tests.c +++ b/tools/testing/selftests/vm/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);