From patchwork Tue Sep 19 20:51:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13391870 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 410E6CE79A8 for ; Tue, 19 Sep 2023 20:52:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C67D76B00D6; Tue, 19 Sep 2023 16:52:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BF0C56B00D7; Tue, 19 Sep 2023 16:52:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A91FF6B00D8; Tue, 19 Sep 2023 16:52:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9718A6B00D6 for ; Tue, 19 Sep 2023 16:52:15 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 702B51206E0 for ; Tue, 19 Sep 2023 20:52:15 +0000 (UTC) X-FDA: 81254544630.19.3F9FEC7 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf01.hostedemail.com (Postfix) with ESMTP id D06D24000D for ; Tue, 19 Sep 2023 20:52:13 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=neutral (imf01.hostedemail.com: 66.220.144.178 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=1695156734; a=rsa-sha256; cv=none; b=J5Kd3nAl0rcDvVjVfsxtS6UvYsH4Z6xok8YQzNWqexd49K1PU7VK+66pUSbqNYJ2f26zUy mHV8KI7gpSJhJfmpSGSSh9h1m2Fz+wB3nmigv3X4OkYUCA9XGN1BO5v+qYPEWfgp8qxMaO OLZ1aMYIzI8wTll275kVMMeI3WCtXlw= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=neutral (imf01.hostedemail.com: 66.220.144.178 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=1695156733; 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=3lEmbQKIShknOwu0d4x/n3fyPteL3kdNeGzeCybOtVA=; b=FDuTWAs7bKzZ95n5b2KkIE/NII+mraElOeuh8aYYN22ynPtqtFasIwMlpXNyQw8/CLL1On ocWLrzjGK9BP2D6+0tI0m2wgJcEUdlPeeMsM/5pHs7IS9vBxIE912fmXKP/KtP/5i7g+R8 nVlL3zIl0xE+ffsSNCpqpi8H6YRZpOQ= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 51999C361C1D; Tue, 19 Sep 2023 13:51:59 -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 v1 1/2] mm/ksm: support fork/exec for prctl Date: Tue, 19 Sep 2023 13:51:57 -0700 Message-Id: <20230919205158.1897353-2-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230919205158.1897353-1-shr@devkernel.io> References: <20230919205158.1897353-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-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JdcXUeexJ7PmwL zwkp1M9Z hojtF4Gzrp2fiR5kIvxZIfQ2nADwDXtf7WlTUx8NiPdz6CZd2p12PUypuE0ftZbmzrXvUuq7ymK74xOM2BT3l9o/CFQ078dA1fisjQy4B5MVAfDFvbNQBAB48Z4nPPUypWjSrJvurIUlZQJoZB/SQxASxMWxKcLzbLh7D 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 Reported-by: Carl Klemm Tested-by: Carl Klemm Reviewed-by: David Hildenbrand --- 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 Tue Sep 19 20:51:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13391871 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 A415ACE79AA for ; Tue, 19 Sep 2023 20:52:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4513E6B00D7; Tue, 19 Sep 2023 16:52:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B3F36B00D8; Tue, 19 Sep 2023 16:52:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1B82F6B00D9; Tue, 19 Sep 2023 16:52:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 005546B00D7 for ; Tue, 19 Sep 2023 16:52:16 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C837F8068C for ; Tue, 19 Sep 2023 20:52:16 +0000 (UTC) X-FDA: 81254544672.04.F0567E1 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf13.hostedemail.com (Postfix) with ESMTP id 229C920032 for ; Tue, 19 Sep 2023 20:52:14 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf13.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=1695156735; a=rsa-sha256; cv=none; b=fF5Zu3RqwXOm3E1iATNpSAiHgj7NWUrbXw0j0cXVPOnYbIsuTd11ZMWHDiyR0hqxB2RFL6 0gq2/+szmEaii00dojqd4YkkqOeQ6ELmbGMmbM+hJ1J4bhEP9hZ73UCL7HkLNHiVA4tucD EKIXxOgO+aZ5qb3+S0VbiXOD2AVKGSM= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf13.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=1695156735; 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=d0iupzJ3mlaND78YmykYesWS4YSwbA5I10u5bAOpH3Q=; b=kJj0NIb/Dw88gIgWrQDM0fDOrXMPDzKaZElPcXyeTPKRhh9W9//Tq3pnvzi2JLSC3UZ563 ZJ8hlCHW9TZC9djxoFqdVVGJLIuQ85cINtHZQkkgMGhCDVHUgH5H8EXXOA1rIauVGM/EGW Gza6CqcLIWLQ1smrLBSw0qGNw7UUn+I= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 5FCBEC361C1F; Tue, 19 Sep 2023 13:51:59 -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 v1 2/2] mm/ksm: Test case for prctl fork/exec workflow Date: Tue, 19 Sep 2023 13:51:58 -0700 Message-Id: <20230919205158.1897353-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230919205158.1897353-1-shr@devkernel.io> References: <20230919205158.1897353-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 229C920032 X-Stat-Signature: ihqnkieky6pfq9qoz8hpo7jobekzy4wh X-Rspam-User: X-HE-Tag: 1695156734-292213 X-HE-Meta: U2FsdGVkX1+fDo7KTyneh/D63uD/X4wuNRayVjJyN7bPaDNWUUVF5RhzgweynYj2dXiNxmJY3bUlJFXTFuW3uKvyWMZ8La8LkqLYmtX1obS6UFqAPrbl/aDGRSvK0WOYtQoyyiIuzkiAYsZFXAZw0C0KDiYNDrpyBZGQCe8R8ePXji0B1DEdSMv2f755eXobOZZGWaNxP8tdszAkA/B7a0hc6lxhyk3cIOHotCP61hVq/8rDaHzM7Vb72PR5wS6cgs0Fb9pm8XCe5OPxYj7+sggmWUaWXfGal95RAB4JbmIlpQH5bWwK6WtuUnAeU9kESXG73rkTPJ1LK2YPSU9QFqKIBQaCDwqPRzO84gvGqxpp2dApC/6Z0GZO98OpMSIsQW+4ifEJ66BKLjeKxtmcTpXGtBVaxAgRh9hwcUtAojpKmV9fu3Hx+/+uIQmk6kEa7lmMkqM3XfHJ7Re/orSqErcggMT+Qsg8rBWuiP0eZTIKibtTrtbW0u/7tjATzWZ8/kvITsxJ1Rjk6BZRic2SVBCqdEcGOpMVD19h+Y3G0MjFMNwG9X2ka0EQpM93+iIzTRZab34tsIlRdPTK6qeEXWqOrrCLHZHbae+5VObkwrWJgssgNbUHiIsVkUIqA2h5kHp0ny1cmeyVBZHXGgbInoyNIwTDFWMuxkNnUNuga+BXID86HNp/7apcVdtw5drSjX30INxhAgIfX+TZQnePUmab/NFBh+fXejCS95IrVBNEHA90obec9LVUow9HQ2OBQTGe4nSx/OPw+aCjdnorjK/jCBm7uzcPbynlEdrAcK0ryv02xz3Zpam/0T5+zdBBQts8MzI5LvRwiT1vPIa/hgzHrukoEgEq+Br5F5Zkd5b8Pjw1ftI5hLkAwQb6kQKOQlqrOeQbCt7tNVZxFfn6J89pJ2dqousmTCmU7FGm5CXFth68bOAajbjD6yL1OqS70sn9gfmS347Y5OpaeNF oQCI80hM ZbuxIyEK+t9eBqo0d1Mai2TfJiGZS90eRN9PF X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 --- tools/testing/selftests/mm/Makefile | 2 + .../selftests/mm/ksm_fork_exec_child.c | 9 ++++ .../selftests/mm/ksm_functional_tests.c | 50 ++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/mm/ksm_fork_exec_child.c diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 6a9fc5693145..9ab6aa402544 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -73,6 +73,8 @@ ifneq ($(ARCH),arm64) TEST_GEN_PROGS += soft-dirty endif +TEST_GEN_PROGS += ksm_fork_exec_child + ifeq ($(ARCH),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) CAN_BUILD_X86_64 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_64bit_program.c) diff --git a/tools/testing/selftests/mm/ksm_fork_exec_child.c b/tools/testing/selftests/mm/ksm_fork_exec_child.c new file mode 100644 index 000000000000..298439f0d55f --- /dev/null +++ b/tools/testing/selftests/mm/ksm_fork_exec_child.c @@ -0,0 +1,9 @@ +#include +#include + +int main() +{ + /* Test if KSM is enabled for the process. */ + int ksm = prctl(68, 0, 0, 0, 0); + exit(ksm == 1 ? 0 : 1); +} diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 901e950f9138..4dc0bb522c07 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -479,6 +479,53 @@ static void test_prctl_fork(void) ksft_test_result_pass("PR_SET_MEMORY_MERGE value is inherited\n"); } +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 *filename = "./ksm_fork_exec_child"; + char *argv_for_program[] = { filename, NULL }; + + execv(filename, argv_for_program);; + } else { + 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; + } + } + + 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,7 +583,7 @@ static void test_prot_none(void) int main(int argc, char **argv) { - unsigned int tests = 7; + unsigned int tests = 8; int err; #ifdef __NR_userfaultfd @@ -576,6 +623,7 @@ int main(int argc, char **argv) test_prctl(); test_prctl_fork(); + test_prctl_fork_exec(); test_prctl_unmerge(); err = ksft_get_fail_cnt();