From patchwork Thu Sep 21 16:47:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13394034 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 E7EC9E71094 for ; Thu, 21 Sep 2023 16:47:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 857AF6B0204; Thu, 21 Sep 2023 12:47:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7BA146B0206; Thu, 21 Sep 2023 12:47:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6A8486B0207; Thu, 21 Sep 2023 12:47:27 -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 5E4CB6B0204 for ; Thu, 21 Sep 2023 12:47:27 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 30D06C0B0B for ; Thu, 21 Sep 2023 16:47:27 +0000 (UTC) X-FDA: 81261185334.05.AF2ABA6 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf25.hostedemail.com (Postfix) with ESMTP id 8883DA0021 for ; Thu, 21 Sep 2023 16:47:25 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=neutral (imf25.hostedemail.com: 66.220.144.179 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695314845; a=rsa-sha256; cv=none; b=0fby5DSfP/9RC9YE+v6H+T0CDcyIHycJbtu3OHmL/lArxRYjBqer9jwkZxMXGiH/sZ4y71 nrkAaMZYdAT2/mZomBOzBP0/nd8sLXQEXcAIo5PF1l89XrogKViZRIuQJzT4uuK2gAGnHt nPHGyX6uyzB6qDFZH9IcU9oFBgy5qnQ= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=neutral (imf25.hostedemail.com: 66.220.144.179 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695314845; 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=bpdu/egjJzZCdO5MaTmibNJPmgXIQdgxHgSo29nLK7eGsv8vJCQ3sJ5z2lMHySi1puBexP HhEdjLVOZD/aTj+XD12d0hVuzf6Xwo8uTehjl6rj+eqlgeuho5WE9Cg9nXkC3Bwry7eriV ZVVWlyC0RP8n0mSywMLpGlcVGw8Z+v4= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 74B35C502489; Thu, 21 Sep 2023 09:47:11 -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 v3 1/2] mm/ksm: support fork/exec for prctl Date: Thu, 21 Sep 2023 09:47:08 -0700 Message-Id: <20230921164709.3627565-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230921164709.3627565-1-shr@devkernel.io> References: <20230921164709.3627565-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rsp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7Fl MrKUhtdn cF98YDYvc/EBF3C6Vs432bCrnGT2XCEUQZBgRtI5tt38gW23M3USVkOIN0icDazPFLFk68cw3wFK8LB9ULThp7yMsCFze+t6l7xcqrBmkEHsZSkNGlxiOcheoNMpx2E3/SqaujpH3TxWaZpADYzp3LIOh/LKGfvzeVs1nYCyrb/Iw15immTQPuTRF6A== 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 Thu Sep 21 16:47:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13394033 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 2D6C7E71096 for ; Thu, 21 Sep 2023 16:47:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC3556B01F9; Thu, 21 Sep 2023 12:47:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C72F16B0204; Thu, 21 Sep 2023 12:47:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B61596B0206; Thu, 21 Sep 2023 12:47:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A8FB86B01F9 for ; Thu, 21 Sep 2023 12:47:25 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 773CC40AD6 for ; Thu, 21 Sep 2023 16:47:25 +0000 (UTC) X-FDA: 81261185250.18.49953E9 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf02.hostedemail.com (Postfix) with ESMTP id C5CF980018 for ; Thu, 21 Sep 2023 16:47:23 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf02.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=1695314843; a=rsa-sha256; cv=none; b=i5q/gXEAix7DuG8laOFkvlNt5c0+RnrEcD7m8S/P8jcbc5oo74elctQLkJizShpo6t+67L ApjQNXqzIAa32JJTl3MWGm+26a3CKcoHAc0pEkgnPQIRvwjHMicFJGav3q1a3NJd2QPLk2 cF/rUP5YWNC3aV9uppKcZDRc3I8E7/c= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf02.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=1695314843; 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=jbc7a5X4ZNDpypVZXIV/QCIODowiYXixfAsK3xK0ipYeS+osHxHsikiBYW1TEBiw0yfRnV L7MLvdCvWKUu+43qCrjH+rFRRaH6eGv6gjhKOzXLy7sP32qpPzV0UdexscEu9A0sw6i4Of 5BR2RyHqEyyTMfqBGtP82xzbUNAc1CM= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 7CD33C50248A; Thu, 21 Sep 2023 09:47:11 -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 v3 2/2] mm/ksm: Test case for prctl fork/exec workflow Date: Thu, 21 Sep 2023 09:47:09 -0700 Message-Id: <20230921164709.3627565-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230921164709.3627565-1-shr@devkernel.io> References: <20230921164709.3627565-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: C5CF980018 X-Stat-Signature: 6yrhc8s46egbfucrnrgpe6ccf6io3jxp X-Rspam-User: X-HE-Tag: 1695314843-825403 X-HE-Meta: U2FsdGVkX18tAJ+0BbdATD1Q3ZzwHRItuYoTojkxwoZyxGg951xRXrTH/HruOOT+6ipHXlziYGfxU/uzgFw9sSdT9BAqk/TLr2SYqDddahKLRYqZB5Wo0cISMJwPRtDwOoO70g+GGuePAKFqoOJzpo+NJxzZ5ThShf6iLf2hNWpzCwvA1jUnfJUDTxf6B1So4prBsqS1hK6L+Td9kMWsmS/Jbrb/gad7JHM7392hElXEgQtyft4OY/doj+yddnQdQJfRUn+zE9F9J4AOy1FYLtyT0hFxq5Yj7rz6u7F8Ehl7Giskt5KNVYpypPrJQOPnmaf4vE/lyytCp7VXaWiRp3gkEODq3iKvJA+YqHdJblDnQbY84X09J0lv1bglFbhAmpmLZMRli3HPxCykyhU0zZnUnGg0pmZwR7y0Cb6RfmBOSl2S3ccxeSSkQHyv9cHdv2wr59SB1JPD84iJ9xkU+Oxnh1dfchH+nqX3kI/FbstQzuF4vqAkLW89GMNJmiT2RGdOPkWWruvKjMEQh+fl2kyOk4hxmz4HaFxZpOyLH9fgJWrmlfMY8RSaiDjnXPdQp7xaI9H5mMhWvtwieTDp2OLH+sWpMewqANp6QO2MKRQofT3QSJyE+Vpa7+W9emn7v8OtZGkoAPc793V9CtwdcXHD1iHiOmZkHKqyafActPLQkuA6OziYu+s5Z501lRHhfv+ebkduO/iWiap6rxF6B38rxRHr/CodrHjA9tZy6gLepcQEh5l3woQwVR/nGDxyJqT9V4V6HU6wTOkr9/pW7CtHubb8LZ5xcX0204BZvhrH05UlC49Dp0SVDAsmloQQ2Gi6S/VU9Bv5SE2pBTm/06FuWMXEu5X/GBaj79LmOuY1/OffqNoug7/Gv8kRCPjC1BHmFKq7CiAI9ozUeYhB2Xn8+fkAQRAfiqNH7jMbdu87LVHxKDQiMhgfmX9gc0DYQyQfqyJDjufHtJj1aMp mEKAAjhw 3fUrzATp52x5s3WHLbxAygl5TnKKCFxdwlbmiawtZtoWToDs1vS6fslDWGQ== 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();