From patchwork Thu Mar 28 11:10:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjiang Tu X-Patchwork-Id: 13608406 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 AE869C54E64 for ; Thu, 28 Mar 2024 11:10:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C7EA36B0096; Thu, 28 Mar 2024 07:10:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C2DEC6B0098; Thu, 28 Mar 2024 07:10:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF60B6B0099; Thu, 28 Mar 2024 07:10:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8F4A46B0098 for ; Thu, 28 Mar 2024 07:10:41 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 51C9F810EB for ; Thu, 28 Mar 2024 11:10:41 +0000 (UTC) X-FDA: 81946179882.04.DB8BADC Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf11.hostedemail.com (Postfix) with ESMTP id A989840014 for ; Thu, 28 Mar 2024 11:10:38 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; spf=pass (imf11.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711624239; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S5ooruH/cHPfkDWNWhBMEtiFounIviZyb4zr5uPuOTM=; b=J23eZ7qT2AHVf4EvbtAOzXv5EtRcy2ILBDPdJEKyA6q4gppjItM1qelXUscb5aw46Js6gy NBq4SIXDj/r6+OHMUC5vH053N/FNyUPHsneQR7PO7hYQotsjU9xALxve2MjNWevOBLNuE2 RZSv4ofDUsokdgPxsBKosQ0ddLIk3b0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711624239; a=rsa-sha256; cv=none; b=jgC4rvlgenhmY9OASnKHR9LYQBxpbjSCwUpsIjQZ9wgmOv9AJCpBLxBXdnh6miJVE8soWX os5j4ydG/DM3hJuPRhKxp+iqCnzRm37KBn1NSjAh6Z7/zjTM2V8NsQaMTESxkKP5c8Km4X mbgfNVhcCaskbOiZhHEGLTZ7dnUzZyY= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; spf=pass (imf11.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.163]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4V513Z0wJcz1h4Fr; Thu, 28 Mar 2024 19:07:54 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id 5EFCB180063; Thu, 28 Mar 2024 19:10:34 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Thu, 28 Mar 2024 19:10:33 +0800 From: Jinjiang Tu To: , , , , , , , CC: Subject: [PATCH v4 3/3] selftest/mm: ksm_functional_tests: extend test case for ksm fork/exec Date: Thu, 28 Mar 2024 19:10:10 +0800 Message-ID: <20240328111010.1502191-4-tujinjiang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240328111010.1502191-1-tujinjiang@huawei.com> References: <20240328111010.1502191-1-tujinjiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To dggpeml500011.china.huawei.com (7.185.36.84) X-Rspamd-Queue-Id: A989840014 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: rwoidiybhojux4xxbo9j778ke1pkaqw3 X-HE-Tag: 1711624238-229451 X-HE-Meta: U2FsdGVkX1+6KYIiKq4VCNnPHONN3hgC2C0gppDRkw+QOTFXj2E4XddEqTxtdqTpCl0HN27L0IFbMOPKvRULqOPH0gm/1x0w9oYumNoEorpdbeGpchyopuPRgnFxoqG+YpuKKuJnzOt1SiDS+ZotdohTizVLa7F7nehmPzF22HjqsmUO76x03f5pi711HX3JvBgClJtenyZ8KikppsLJmWGG6BkbAqedhl6M3lHbIzy99sVamDECvls1JI3cxqN3rQq1FVp5zIRA1hroX86XmQTfOzkiP2ijknclwQLTocJbc37eLW9Ramljc+9YfFZHNk4D8aGAinEgHdVzbdY7dVsl3wEZGFuGs/GgE6BBOG+gPKJIvUJStfiFQY3X1rJgDFLzodB3FW7oz7TF+qLhSiREcDLTRHP6iuF4ULHi+zaHOak8ZUrGhebGFLNecbEJwez0pLJaWg4TvY66gUSok+BAGY7t2+gwWT38Rh7rKCbnk+l8pzbTkiErli0iCGNlXbCB9hINERIpACl23OaR6AeI1qpCpBTNnLlIo+42ezLjgN5UvvrB+qBnZ3DBiDh3+shml8B6JXH6kSYnVbXBKnmxNQNBNwZyygxACXl2KuJVrgN+g9VHutbQxn+JsSzNZSHYNxoytco80i/I78ltGk9/tuc4kZbyz91sJ3U4uoid2k30HVU3NlHW1C9jSWmRnU3pQP4gP3VBB3nTSnfsYZRr5BV0TFjLlQOaInH+Ei1boALy0Ceb86y/3x0Q8BpGyW3+7DOrwbqLokIXcOjcBHS/z8xFwyBmZo2e4j2/HgPg1QzanGGgUA9Hz84cH3zodVLIFNFk4+Zmb3IvadwCL79PI5tOMnR5Z2YcCFMtoi7ND4D0o/3V1G9BWfisWWG4zpWr7NvGuWBGr7cMlQ9IVvtl6hg0IodNauaeevbho5i61L3ENlZXaZdpeIQETejNZMs+JIA1F+15WWO8Yx9 xh9DteKt WBJwjeI0bhPyaaikS+wfnod5u7yo1mLDqKytU3+RVHGRrlOk+ns1tXUHx0md8jIkH/VSgqr3TUk8FeqZ1h8ER4hBPTQ/KpJYB+L28vvyebAbXpS1EWp/cW9io7Lw7yNOu6HOwCmdYCA5Rh0yR22VPpcUi1+IKEgsRagJ/sDkZ8CCEtuApU6Ss+xQQ2tgBGDb6NPWNMtJ/9iCf6zVG79Kacpphsg== 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: List-Subscribe: List-Unsubscribe: This extends test_prctl_fork() and test_prctl_fork_exec() to make sure that deduplication really happens, instead of only testing the MMF_VM_MERGE_ANY flag is set. Suggested-by: David Hildenbrand Signed-off-by: Jinjiang Tu Reviewed-by: David Hildenbrand --- .../selftests/mm/ksm_functional_tests.c | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 5462873df84c..2d277620fad2 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -475,6 +475,36 @@ static void test_prctl(void) ksft_test_result_pass("Setting/clearing PR_SET_MEMORY_MERGE works\n"); } +static int test_child_ksm(void) +{ + const unsigned int size = 2 * MiB; + char *map; + + /* Test if KSM is enabled for the process. */ + if (prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) != 1) + return -1; + + /* Test if merge could really happen. */ + map = __mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_NONE); + if (map == MAP_MERGE_FAIL) + return -2; + else if (map == MAP_MERGE_SKIP) + return -3; + + munmap(map, size); + return 0; +} + +static void test_child_ksm_err(int status) +{ + if (status == -1) + ksft_test_result_fail("unexpected PR_GET_MEMORY_MERGE result in child\n"); + else if (status == -2) + ksft_test_result_fail("Merge in child failed\n"); + else if (status == -3) + ksft_test_result_skip("Merge in child skiped\n"); +} + /* Verify that prctl ksm flag is inherited. */ static void test_prctl_fork(void) { @@ -494,7 +524,7 @@ static void test_prctl_fork(void) child_pid = fork(); if (!child_pid) { - exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)); + exit(test_child_ksm()); } else if (child_pid < 0) { ksft_test_result_fail("fork() failed\n"); return; @@ -503,8 +533,11 @@ static void test_prctl_fork(void) if (waitpid(child_pid, &status, 0) < 0) { ksft_test_result_fail("waitpid() failed\n"); return; - } else if (WEXITSTATUS(status) != 1) { - ksft_test_result_fail("unexpected PR_GET_MEMORY_MERGE result in child\n"); + } + + status = WEXITSTATUS(status); + if (status) { + test_child_ksm_err(status); return; } @@ -516,12 +549,6 @@ 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. */ - return prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) == 1; -} - static void test_prctl_fork_exec(void) { int ret, status; @@ -554,7 +581,7 @@ static void test_prctl_fork_exec(void) if (WIFEXITED(status)) { status = WEXITSTATUS(status); if (status) { - ksft_test_result_fail("KSM not enabled\n"); + test_child_ksm_err(status); return; } } else { @@ -635,7 +662,7 @@ int main(int argc, char **argv) int err; if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) { - exit(ksm_fork_exec_child() == 1 ? 0 : 1); + exit(test_child_ksm()); } #ifdef __NR_userfaultfd