From patchwork Wed Mar 27 06:09:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjiang Tu X-Patchwork-Id: 13605607 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 50055CD1280 for ; Wed, 27 Mar 2024 06:10:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01BBF6B0096; Wed, 27 Mar 2024 02:09:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EC4626B009A; Wed, 27 Mar 2024 02:09:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C72206B0099; Wed, 27 Mar 2024 02:09:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B64C26B0098 for ; Wed, 27 Mar 2024 02:09:54 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7CBACA0994 for ; Wed, 27 Mar 2024 06:09:54 +0000 (UTC) X-FDA: 81941793108.06.AA709BD Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf20.hostedemail.com (Postfix) with ESMTP id DE0101C000D for ; Wed, 27 Mar 2024 06:09:51 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711519792; 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=3wm5s2+OnW88nMQQZAuHTrskP4Ow1S/ft1oHx7m30Ww=; b=Pa1VrofXAyPhFm7NNjUtbKBxjWJ0Xwd+cUK7dmIGzqSTAAFI31cXafpH+X8Nwkyfm+xcl6 5wJhzoWxIm605A7E4mJtS2BChLd9CpW/dZEdvHM/Q4owdXEqhEJEKdxt//6qnbyEIppy56 zlnw2Zibi509OSvU9eoyNLwwSRbrcs4= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf20.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711519792; a=rsa-sha256; cv=none; b=RYAGckQNVgUHe+/JYLZvNMqi3wFc/db7JVVD9jWhRBsBLb74iWpxxsN5LtyG0xTi1a6NUh k0E4qe/CHEvPQ8qTf1Sk8lwiJ2lkrC8gjykQvQHwtXWiVGoB7NQWZVM0PaNjRMAgI4p51u SG+uV5+tX87LfjDpf26f/d7DgqqKAyo= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4V4GQv736Bz1h45m; Wed, 27 Mar 2024 14:07:03 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id 307561402CA; Wed, 27 Mar 2024 14:09:43 +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; Wed, 27 Mar 2024 14:09:42 +0800 From: Jinjiang Tu To: , , , , , , , CC: Subject: [PATCH v3 3/3] selftest/mm: ksm_functional_tests: extend test case for ksm fork/exec Date: Wed, 27 Mar 2024 14:09:22 +0800 Message-ID: <20240327060922.1484395-4-tujinjiang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240327060922.1484395-1-tujinjiang@huawei.com> References: <20240327060922.1484395-1-tujinjiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500011.china.huawei.com (7.185.36.84) X-Rspam-User: X-Stat-Signature: bhmewitrg5fwt8szojtzh48ks6gfktzk X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DE0101C000D X-HE-Tag: 1711519791-461204 X-HE-Meta: U2FsdGVkX1/doTEYtnQS+Efi7rTFpMAkaeM4cif8N//74pVgyM/lXz89Wp8aebzXzJzhRF+BEbtGw+1/WloBhca0DGSlsHn2FvuoT9E1O8D43eT4HPn9Kwzwjj6d4aNG/HF0Far2c/ULEAMfERD8BL736cZlD95kRH/rMRlnUHgQ1KeUbwO1rdDXFWa1s7NHsD9cQL4SOEBXUdBfv2X7wyfkIegn/1yQP/oNQ/rBc3lZ0TxlyUh2sYIESBWxtPck07C17PR7kpdJFvwptNzNYrvpozFMdd+u7cHaXSbQoYMCWv0jVvn7AlHb7pPOeJk0PHRWR0++QwD5X5MpezSw2H6LismazWGFelJRmewAz2m0iSkbQ7tq9tbrlnt5YdnmwpYVwXtedPTAHdam8RFvbnRq1+ySZkcCZHphsYS+SspAQ/0P2CSF3Kw7FVaSwuwrZiPdGZu0nGobcQj+TBwYWXovNEiCRqLYzfCEO1AriLFyxCzpm8HfAaRq6TAF8caO2O9uZ7sQzgqok3QxK8QM3J3r8N4yBo+R4oj6m1h/Xn+QoxPFJwpHnEr8dT0fEpF3ebluWoj209Et7RubtHKWhpI8RiDwdFXNzwD8YValFvV2IoP7B/EFSFCAPGC9opPFhwFVNaTZUuFRnBUbWM/WfJvm5z53Dn7VTUeVR/pifjfdbkVUl0+ZZi/u56I+EK88C+m8dbq2+Q5NOFHsR68ItceKxXlFgKLZGGMXXIyLVGR/MLHFwCfsxmE9GY/auPStDaOJIoKuMcXECIDKNcwtKSILL53hDQT/oUWz0eZGUbDGTdkS3A9NrB37OmMaZM7OkEDGZ+n+m2eqiRfN69Q+HMj9wmehV6luRqTgVNY1XTGZuPFwTxKHNsNcffhcTDBjc1h/owzXsGHSFaM/P9ilZZpKNhHIYWEkQp9MejI6nsfOr0Ue0qaWWmVtuGQGiTkvcGkHqqsswIFk8sMSBXI Jg973gJE KwJrX8hgWRHKsC1AhDiPykSJsEEB2W1LpmxBc8KbKz1+QZxI6ZJJus7yHxRkbYahp9qsovS/SAUjwXcBrhfq+F+yG0BDHuTSzrDe6OCaW4UaigRocddonteOwzr8bk3Mk8x27I3ER6yrFJ9Yv81Q2WwzLNwTuzRKhmyPSWiXVXXJA5IvYUaHEx9CjCsLsGPdjjzzr+7rQFjreS4yXTGRmOWKK8g== 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 test the MMF_VM_MERGE_ANY flag is set. Suggested-by: David Hildenbrand Signed-off-by: Jinjiang Tu --- .../selftests/mm/ksm_functional_tests.c | 52 ++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index da58545e58e1..c56e34946096 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -491,7 +491,20 @@ static void test_prctl_fork(void) child_pid = fork(); if (!child_pid) { - exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)); + const unsigned int size = 2 * MiB; + char *map; + + if (prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) != 1) + exit(-1); + + map = mmap_and_merge_range(0xcf, size, PROT_READ | PROT_WRITE, KSM_MERGE_NONE); + if (map == MAP_MERGE_FAIL) + exit(-2); + else if (map == MAP_MERGE_SKIP) + exit(-3); + + munmap(map, size); + exit(0); } else if (child_pid < 0) { ksft_test_result_fail("fork() failed\n"); return; @@ -500,8 +513,16 @@ 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 != 0) { + 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"); return; } @@ -515,8 +536,21 @@ static void test_prctl_fork(void) static int ksm_fork_exec_child(void) { + const unsigned int size = 2 * MiB; + char *map; + /* Test if KSM is enabled for the process. */ - return prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) == 1; + if (prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) != 1) + return -1; + + 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_prctl_fork_exec(void) @@ -550,9 +584,15 @@ static void test_prctl_fork_exec(void) if (waitpid(child_pid, &status, 0) > 0) { if (WIFEXITED(status)) { status = WEXITSTATUS(status); - if (status) { + if (status == -1) { ksft_test_result_fail("KSM not enabled\n"); return; + } else if (status == -2) { + ksft_test_result_fail("Merge in child failed\n"); + return; + } else if (status == -3) { + ksft_test_result_skip("Merge in child skiped\n"); + return; } } else { ksft_test_result_fail("program didn't terminate normally\n"); @@ -632,7 +672,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(ksm_fork_exec_child()); } #ifdef __NR_userfaultfd