From patchwork Fri Sep 22 21:11:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13396392 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 DB651CE7A8A for ; Fri, 22 Sep 2023 21:12:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B3DBD6B0302; Fri, 22 Sep 2023 17:12:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A9DA06B0300; Fri, 22 Sep 2023 17:12:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 93EF56B0302; Fri, 22 Sep 2023 17:12:07 -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 7E0B16B02FB for ; Fri, 22 Sep 2023 17:12:07 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4E2CD41182 for ; Fri, 22 Sep 2023 21:12:07 +0000 (UTC) X-FDA: 81265481094.25.A99AB88 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf26.hostedemail.com (Postfix) with ESMTP id A9EE4140006 for ; Fri, 22 Sep 2023 21:12:05 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf26.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=1695417125; 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=7iTDGizWpIb8FZfB8TU7wmmmnqqKOWotVSPof36YeIg=; b=LdkogKFzejR6U0Mmnh1l7SD8nFgqjFGjLa1NvXuQfhE1AwJtusF4hN2HQjxeH1MWdVpOwo vbX6TTEpnNpXc/dWWgr1GudvA7W2kifw0xq0lQ5x/efHQhPCVJv5InOsMlgABpzUgAHPi/ 3xTG8ud4ldGHitaQMw0ERbqFykvo9hk= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf26.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=1695417125; a=rsa-sha256; cv=none; b=tdX7FTTQHKNeUoufyjYPG/VToj/f1OwNKff6+JGhFkujt7R1Q8JxIihAxmgTG92f7ft2aU yxRDn6jnFOcCxmU+b7NRaqrcuIUDj6W5lyMXK1oCHg/MxL8fdUJExPW5adU1iuIENMBGAf QMTfB5LdFV46BRuoQuS9RLHKBAvWF14= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 7D7E9C60DC4C; Fri, 22 Sep 2023 14:11:49 -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 v4 1/2] mm/ksm: support fork/exec for prctl Date: Fri, 22 Sep 2023 14:11:40 -0700 Message-Id: <20230922211141.320789-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230922211141.320789-1-shr@devkernel.io> References: <20230922211141.320789-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: kxpyj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lUZeAo iijC++n0wRAR6eoIV1lnOByZQga5Oir6hP3GRFtJJG05g2uDbnCkSs//PNwGo7Q6zzUalqdg6RbAoXw+OQupxbMn0oyn998NAyrIkKXVWNnCVX+Igz/dLSRLamiRrdgmkPPqMAhAyeqrvwLgE7iCrgj1BtBPMkDrIklDacqmoUWnBsqtUTCZg9NT4btHV3EhOLbxi 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: Today we have two ways to enable KSM: 1) madvise system call This allows to enable KSM for a memory region for a long time. 2) prctl system call This is a recent addition to enable KSM for the complete process. In addition when a process is forked, the KSM setting is inherited. This change only affects the second case. One of the use cases for (2) was to support the ability to enable KSM for cgroups. This allows systemd to enable KSM for the seed process. By enabling it in the seed process all child processes inherit the setting. This works correctly when the process is forked. However it doesn't support fork/exec workflow. From the previous cover letter: .... Use case 3: With the madvise call sharing opportunities are only enabled for the current process: it is a workload-local decision. A considerable number of sharing opportunities may exist across multiple workloads or jobs (if they are part of the same security domain). Only a higler level entity like a job scheduler or container can know for certain if its running one or more instances of a job. That job scheduler however doesn't have the necessary internal workload knowledge to make targeted madvise calls. .... In addition it can also be a bit surprising that fork keeps the KSM setting and fork/exec does not. 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 Fri Sep 22 21:11:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13396393 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 01866CE7A86 for ; Fri, 22 Sep 2023 21:12:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC1246B02F9; Fri, 22 Sep 2023 17:12:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C71836B02FB; Fri, 22 Sep 2023 17:12:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5FA36B0300; Fri, 22 Sep 2023 17:12:05 -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 A7CCF6B02FB for ; Fri, 22 Sep 2023 17:12:05 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7C5128116B for ; Fri, 22 Sep 2023 21:12:05 +0000 (UTC) X-FDA: 81265481010.10.86500BE Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf10.hostedemail.com (Postfix) with ESMTP id BBB3DC0002 for ; Fri, 22 Sep 2023 21:12:03 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf10.hostedemail.com: 66.220.144.179 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=1695417123; 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=ivbfTSqiGJDKhzmVN6iFRasV8Zko5oH9CCWYtxUwt+M=; b=6FGjtOxdrmPI4STBCucHvG6u+TI197WFsTxKlQzbU+QVVzkqo5kkMQuNqpPvtGf9LAZ3Xi mdHrKGK+JvEe+cANazLNSWn32MEEuRXMm37kDmam5Bn0pOYGY7KgMZEdLh9I305cVqA7lT OEY9mAdp+f8AUVXrJk4SxaMaMHBYFuo= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf10.hostedemail.com: 66.220.144.179 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=1695417123; a=rsa-sha256; cv=none; b=32i4az3wX/bfbx5X0PDLcLOTIaU9FSANwy5DcxOdJ9YQauFM9aR7n5NyNR/w0c14GWZ4dF Fep1rtOoXkgsn8SOv20NQZOg8aMn07mbUFj7n1jagMt2jb6lEiLPbEUfAsNyNZSFhDYD8+ 9yjKGUhn6OzgGkXx8p7WldDzXViO1Pc= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 9BCBFC60DC4E; Fri, 22 Sep 2023 14:11:50 -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 v4 2/2] mm/ksm: Test case for prctl fork/exec workflow Date: Fri, 22 Sep 2023 14:11:41 -0700 Message-Id: <20230922211141.320789-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230922211141.320789-1-shr@devkernel.io> References: <20230922211141.320789-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: BBB3DC0002 X-Stat-Signature: kd7bkq7uwa57xbq8oqs3e5pdjefmxko9 X-HE-Tag: 1695417123-536836 X-HE-Meta: U2FsdGVkX1/jbSVoIEKCE0zGJ4OiExYf5TRBSnxCG70aPDV70GSD5wE0pg1yTWxVENeJ6YL3qmQYm1GiyBTY8VRu51GHGSk3dLHG3lnKreEXRB5Z5OMlFXeTeh1HGH7ywOmMVzIGnqvF947gFcin6H1SM8wnR78CdyDGvmLimlEnPWT8NwNOoeIm9BfexWasX4dRhND+PGon+2uoIf8u9xt1AXRkVPBQqpOsuBMiBMYVN9zasXUoNAtTRbWrnZYOZkGDRP8zyLibl0RzbHjeOfWnXmLCWwDMoRWgLebdU147rvdB3TnzDSbMRcHBDKByQtrkX365xJj7zwK8s6GiFNr4vB3cGjBmMGZhVYYB3PDDBJCGC3bIh3r6WY2FrCIsjH6E1YDaMR1aEC0O8Kz35bgfuGSo4K1vJb60KjgvT32dxRquId04ErM+TbAbutgG4rUKSkk1ouwGgWQYGjU61A9s4tGlXDSMdzTe2Oz3h5A2skjfaahQKAxIryQ+KJ66xdByA0fAHRCLBpW9LogQ2bsOewX+YdwQZ92zfsoEL03viSjxirNqK9xIPYbZayZcV2VNBW4bmOAwyL+bEr/qY5MijgufhlYW0d9jCtBSjx6u5/sxjPcv3B79LnvBg3MMAvyu7lzegGiO551xTCeF5TE15tNqZA3pRLhuHQIqthOpUhWihFnc6wYg5Vsn0N85roSyc9u26q7UO1Pf70OCaBq1t3TGu07pJwkQxnK1fhXYyTdPaJYZr2/1QT4WaDdqgTi+qBYvTog2uqZhZgDNS+fHX3xVfZttNLjxauRb8WHwjTTCkS9nCsSa0deuC9Ow3goKuE+vbb4R7o0a0V+zAMbVlMnRkyOGXuN3Ft6nzWX5Z96IYV4lg+ctG8EbMTqVGcvCbsVDIxB9jm952TQRs/0bVqlkK9U0ZeArxY71oTAGzOYjOX1MSzVPUECQl4w9qIzQmriFdyqlv/Nwuak wXB4aOZn K08FPKibVBWOud2MN43CJMlL+ogqoT5WIGB/K6gdoxFzMZQ8= 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: 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 | 66 ++++++++++++++++++- 1 file changed, 65 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..fbff0dd09191 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,64 @@ 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; + 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 +595,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 +639,7 @@ int main(int argc, char **argv) test_prctl(); test_prctl_fork(); + test_prctl_fork_exec(); test_prctl_unmerge(); err = ksft_get_fail_cnt();