From nobody Sun Dec 14 05:48:54 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AB45C04FEC for ; Wed, 20 Sep 2023 19:01:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229870AbjITTBq (ORCPT ); Wed, 20 Sep 2023 15:01:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229813AbjITTBm (ORCPT ); Wed, 20 Sep 2023 15:01:42 -0400 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D56DBCF for ; Wed, 20 Sep 2023 12:01:35 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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) =20 #define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) =20 +#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) =20 -#define MMF_VM_MERGE_ANY 29 #endif /* _LINUX_SCHED_COREDUMP_H */ --=20 2.39.3 From nobody Sun Dec 14 05:48:54 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71FCDC04FEB for ; Wed, 20 Sep 2023 19:01:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229879AbjITTBs (ORCPT ); Wed, 20 Sep 2023 15:01:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229832AbjITTBm (ORCPT ); Wed, 20 Sep 2023 15:01:42 -0400 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6F99C2 for ; Wed, 20 Sep 2023 12:01:35 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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/test= ing/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 @@ =20 #define KiB 1024u #define MiB (1024 * KiB) +#define FORK_EXEC_CHILD_PRG_NAME "ksm_fork_exec_child" =20 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"); } =20 +static int ksm_fork_exec_child(void) +{ + /* Test if KSM is enabled for the process. */ + int ksm =3D prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0); + return ksm =3D=3D 1; +} + +static void test_prctl_fork_exec(void) +{ + int ret, status; + pid_t child_pid; + + ksft_print_msg("[RUN] %s\n", __func__); + + ret =3D prctl(PR_SET_MEMORY_MERGE, 1, 0, 0, 0); + if (ret < 0 && errno =3D=3D EINVAL) { + ksft_test_result_skip("PR_SET_MEMORY_MERGE not supported\n"); + return; + } else if (ret) { + ksft_test_result_fail("PR_SET_MEMORY_MERGE=3D1 failed\n"); + return; + } + + child_pid =3D fork(); + if (child_pid =3D=3D -1) { + ksft_test_result_skip("fork() failed\n"); + return; + } else if (child_pid =3D=3D 0) { + char *prg_name =3D "./ksm_functional_tests"; + char *argv_for_program[] =3D { 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 =3D 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=3D0 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 =3D 2 * MiB; @@ -536,9 +596,13 @@ static void test_prot_none(void) =20 int main(int argc, char **argv) { - unsigned int tests =3D 7; + unsigned int tests =3D 8; int err; =20 + if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) { + exit(ksm_fork_exec_child() =3D=3D 1 ? 0 : 1); + } + #ifdef __NR_userfaultfd tests++; #endif @@ -576,6 +640,7 @@ int main(int argc, char **argv) =20 test_prctl(); test_prctl_fork(); + test_prctl_fork_exec(); test_prctl_unmerge(); =20 err =3D ksft_get_fail_cnt(); --=20 2.39.3