From patchwork Wed Sep 20 19:01:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13393221 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 88975C04FEA for ; Wed, 20 Sep 2023 19:01:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 01BC66B0197; Wed, 20 Sep 2023 15:01:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E98AD6B0198; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D10046B0199; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C1BE36B0197 for ; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 30905A0B1E for ; Wed, 20 Sep 2023 19:01:40 +0000 (UTC) X-FDA: 81257894760.06.58A3AB1 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 0336040004 for ; Wed, 20 Sep 2023 19:01:37 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695236498; 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=2Ui9HQp2pxPJ3NIxebx6kuSf7rRkN1tIaYoms9ZvXGw=; b=zDMnVkJ395bMMgNnWMMCV3Ohuwpcna8KJO1NY/PB2sxZ37jLz71FpV55miJJrlHTjdhk7W I5kiBv8Z/G65SP5LS5rePwPaQRbRvFQP+JYmS8I1gKMkD/rbPYXn0/uFez+qcQKzc84elX x4zakRgmenAjGyXmUWVfGwYOAZVUotc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695236498; a=rsa-sha256; cv=none; b=idW5rVjXga5KPAgw6wZHFAPbCbCLuw3EvbxaARLdsn/JsAQvQl5MJ4Bzyqlhc1zlJyqvbw VOGVPro4vMi/Qp/qAummhWhBeDa55QbOcYdZZA5iALutLnPrLNelnRCNny003kRKv2gUQw Jlywch06n5nq4nnrUButerihDx7Gwnc= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 33BFBC4363F6; Wed, 20 Sep 2023 12:01:22 -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, Carl Klemm Subject: [PATCH v2 1/2] mm/ksm: support fork/exec for prctl Date: Wed, 20 Sep 2023 12:01:16 -0700 Message-Id: <20230920190117.784151-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230920190117.784151-1-shr@devkernel.io> References: <20230920190117.784151-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-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JWgenGiJ 1kjrvyZIZgyITzATHblTWX0751IBZ4sKIlpvZe1RPw3O2j2wl6YCHzM5rdo8+7HtrjSp9uSEtFAW2d/OqQi+xiMu0fQaLPs6Em4I3yV0/Vspqmnc+RdQTRYW7hu1HShwnKQoc/Ef0HfusuzQjpOf2jySQynxybSDSpiccLCbcEQ5ht0aPpfTiJaKbUnQRVZreH+eg 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: A process can enable KSM with the prctl system call. When the process is forked the KSM flag is inherited by the child process. However if the process is executing an exec system call directly after the fork, the KSM setting is cleared. This patch addresses this problem. Signed-off-by: Stefan Roesch Fixes: d7597f59d1d3 ("mm: add new api to enable ksm per process") Reviewed-by: David Hildenbrand Reported-by: Carl Klemm Tested-by: Carl Klemm --- include/linux/sched/coredump.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index 0ee96ea7a0e9..205aa9917394 100644 --- a/include/linux/sched/coredump.h +++ b/include/linux/sched/coredump.h @@ -87,8 +87,11 @@ static inline int get_dumpable(struct mm_struct *mm) #define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) +#define MMF_VM_MERGE_ANY 29 +#define MMF_VM_MERGE_ANY_MASK (1 << MMF_VM_MERGE_ANY) + #define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ - MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK) + MMF_DISABLE_THP_MASK | MMF_HAS_MDWE_MASK |\ + MMF_VM_MERGE_ANY_MASK) -#define MMF_VM_MERGE_ANY 29 #endif /* _LINUX_SCHED_COREDUMP_H */ From patchwork Wed Sep 20 19:01:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13393220 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 C4077C04FEB for ; Wed, 20 Sep 2023 19:01:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 375B66B0196; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 325336B0197; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1ED4A6B0198; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0E1996B0196 for ; Wed, 20 Sep 2023 15:01:40 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C89D280DB2 for ; Wed, 20 Sep 2023 19:01:39 +0000 (UTC) X-FDA: 81257894718.09.9FA874E Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf07.hostedemail.com (Postfix) with ESMTP id 0759E40036 for ; Wed, 20 Sep 2023 19:01:37 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695236498; 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=KIinxHgd3W5A5KZig7FsyZOy3C3CzjLyJqwrfF5qVT8=; b=vlE158Dd8LbOG4d9M1OMnS6iDTdEewEhhWAyGUg1FcvD+1++UwpE58XqoVLqPt+nYQWKav 0lomrcnwm6KJiz1leGDP+hSxcZ2LMuUDqX8vJPUGGAshPmWrOkBxpkUXXPRcQuYEsrZydK ONvdPGJddIx5lqCZAu7i+mNgiXo/s/Q= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695236498; a=rsa-sha256; cv=none; b=lKdQOdo2xikbZpmpuZRwq0DVoRpkxGWdGa30RtJnAncMK0KAS0pZOI/qUSrsIIUEy/v3to OUTW+J/WfXXiCXFFA2D8Y+fQGVKMknqW9xldEtyEEMkAC32ZHK+2e0aq0jqgE74fVwnJua vCI4u18/zAli3FESxu8d9Ct1nHlwdbk= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 3BE9EC4363F8; Wed, 20 Sep 2023 12:01:22 -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 v2 2/2] mm/ksm: Test case for prctl fork/exec workflow Date: Wed, 20 Sep 2023 12:01:17 -0700 Message-Id: <20230920190117.784151-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230920190117.784151-1-shr@devkernel.io> References: <20230920190117.784151-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0759E40036 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ddomjswku1zs96me5ircyfkfsdbu473n X-HE-Tag: 1695236497-548102 X-HE-Meta: U2FsdGVkX1+BD3X26iw0JJVs/x42OzKoadCoVuju8MEppTxk0XX23cAtKB63plvAL1AW1OmySGe3X5ObXp6i0bj1xgvElPMdIgwlhYeq5AQO3oADTD4aVxrxsHoISfTUSHf9rWCq6WWFn6sQU4hQygh1yjEfvzL5ho639TWja70g9lXuP85JPf4NZGlMcU2pi4AtznYjR/pEvPgKg+CJpXxcof4CfgOgW+D5Yg7GNXwa/O8y0LcXWfnjsh4rWatiJuYdDxmY25skwx1h7ZVXpieKq4wBzwYj5BfW0OvPhdWsR/74b2V1omG6GEFhMU1M9VLFNFktljOPm87cch8yfS4adYBS5RAwfdnLeQqUAAPRqSeotZuwPpUa+Y8oBlxuKgtg0oGx2Sg1U/sM97c3YBl3G/UCFk81cDrH46zFmOXT5OwtRQgYdoVj7e0fDr4zZJAutrbpxdU50M34hkTiFI1fdckMxriD5VYyFSuA9fJl4LLYRuFY4Hdae6sV124rWw/pq7UpjT2jPYUKwBg4rhvDu5bZSvtLyPqd4C/SsYCOONyCkWa0IQOmWyw/K+zNyNURHien7sQd9x46N8/72jZTiqDDeaUdKdlUEzGidGpY6+PpVovYgpR+1fq0BgAGEYSAsx59ujQrNiNqW42JWhCo9x7rJ97yv9X0m0rJID2a+42wmobHD/gTDPXRH9txc3cQAT41b2DfEOFm9icGFsaPMDqe7PQ7bXrGbfuAVE32Ugff+JpKitMfTCkLC1y2jmqe8cNKnNHTGgzs5DlVDwjM7wOfwvATYy7IPaZpznrzhnO87GUNgD06ohq+w2xD8/wtqb9bOdeaovSgB++BVemi5IXzjsD7VZqJUvDgAJjNllOyADUE3u2F0uEr4ypThL4uYObddw3DA1OATURGWlIXH0golmymnMkpb+neljLpUenB5+iezlXQYEGMnTnznFFDTqMLYE54Ntj+1a1 b2i1Iyy/ GwiGJ2BJrEs7k4liBQ2c5wjLsd1dvBL9eRpsj X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 901e950f9138..40b86c9caf3a 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -26,6 +26,7 @@ #define KiB 1024u #define MiB (1024 * KiB) +#define FORK_EXEC_CHILD_PRG_NAME "ksm_fork_exec_child" static int mem_fd; static int ksm_fd; @@ -479,6 +480,65 @@ 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. */ + int ksm = prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0); + return ksm == 1; +} + +static void test_prctl_fork_exec(void) +{ + int ret, status; + pid_t child_pid; + + ksft_print_msg("[RUN] %s\n", __func__); + + ret = prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); + if (ret < 0 && errno == EINVAL) { + ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); + return; + } else if (ret) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=1 failed\n"); + return; + } + + child_pid = fork(); + if (child_pid == -1) { + ksft_test_result_skip("fork() failed\n"); + return; + } else if (child_pid == 0) { + char *prg_name = "./ksm_functional_tests"; + char *argv_for_program[] = { 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 = 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=0 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 = 2 * MiB; @@ -536,9 +596,13 @@ static void test_prot_none(void) int main(int argc, char **argv) { - unsigned int tests = 7; + unsigned int tests = 8; int err; + if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) { + exit(ksm_fork_exec_child() == 1 ? 0 : 1); + } + #ifdef __NR_userfaultfd tests++; #endif @@ -576,6 +640,7 @@ int main(int argc, char **argv) test_prctl(); test_prctl_fork(); + test_prctl_fork_exec(); test_prctl_unmerge(); err = ksft_get_fail_cnt();