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: 142814 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5019183vqi; Thu, 21 Sep 2023 10:37:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGymdlU0/l1yheCyD8sXhY3FJg7i7WUJUgz5HMUB3ybj/x+rMAbcCKGscr6PEsmNS1Vp3bQ X-Received: by 2002:a05:6a00:14cc:b0:68f:cbd3:5b01 with SMTP id w12-20020a056a0014cc00b0068fcbd35b01mr428138pfu.13.1695317838916; Thu, 21 Sep 2023 10:37:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695317838; cv=none; d=google.com; s=arc-20160816; b=vwD0+v2TXcKD4XJL1bDvwOmTbbZEgKrQ1qAFni0mRDi+ulZVzaafiS+uCCa7HxWVn9 Nl0KypJYRgpw3+FliL6ItD8BgS00zP0H2XyjM5VHOgZ7cvZkxv/puPOA7AQKcq+ffICf HuLENK9RFsz7ctIHScgK3wdwrUJKjbJOcwLLHS8AOEWoNxkyh6sxHkdMdcymRyEXXefD CskfqVDouvN+AojX/mjUXVrE+Xo3vYJuZp+khprA7DogYM6nidCWhUtKkKQ2FIBA7y4Z sPcikyxF0MznEdu2nU45sO7rCPdob5xtYRxrvlusazEVfmsAWc+T15nlqmwGZPqS9kJJ 54vQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=2Ui9HQp2pxPJ3NIxebx6kuSf7rRkN1tIaYoms9ZvXGw=; fh=trQN94Iqca1GMQF1AkSOofuqg8pe0AKHPTTlJaXdYFw=; b=EpAG8yXxE7erhM+E72j0N3EJbFVJPoRbd/h7A8bqmusekzw0Q/zj4yLhrwT9UgX+od Tnu+IvVSlgYoJvbQxEBhjWulYcZQRNTAqruUh211Hd5BwMVpb87Sjh4mHj1hjDs7SYgB eIrneZM6APLzOtUxzW3D5tTC/bw/nQ9rL6QFIzQx1P42eYCVZLm6asOTCHrMU5woeGxQ 41vivc9K/YNndybFoDwZDpFwTKUoox4eMThNMnczgQe4bKHM4i7CShXx9aNub/Fdsc8E 6veX1+A79t+T/I/qOiw8f7KSKWEqxOn1ujcxfRsce0TxW5XyRIaHudhEJFzchEwGazfr krEQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id s72-20020a63774b000000b00577f4d85fdbsi1785225pgc.548.2023.09.21.10.37.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 10:37:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 9A9D8834F743; Thu, 21 Sep 2023 10:28:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230313AbjIUR2n (ORCPT + 29 others); Thu, 21 Sep 2023 13:28:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230249AbjIUR16 (ORCPT ); Thu, 21 Sep 2023 13:27:58 -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 4A48224853 for ; Thu, 21 Sep 2023 10:11:30 -0700 (PDT) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 21 Sep 2023 10:28:52 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777669598223424601 X-GMAIL-MSGID: 1777669598223424601 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: 142864 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5092367vqi; Thu, 21 Sep 2023 12:39:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE05osToE2/5sjw9or9UU8yMvPsG/50Fy4hNhgbpnYhhzWUUpUVd+Tq5+X6uPyfW5IgQbj6 X-Received: by 2002:a05:6a00:cd4:b0:68a:4d66:caf with SMTP id b20-20020a056a000cd400b0068a4d660cafmr6732165pfv.34.1695325156814; Thu, 21 Sep 2023 12:39:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695325156; cv=none; d=google.com; s=arc-20160816; b=shiiqZGUs7Zj4CkJPUGOjKFUCD+aBVvjuh5oun6CZohSjvXT09SfShiofjOwTkohJO OK+VADiGhj52GpA6J+azyQijhFnPMPdZDng71X7+8Fn0dwiYTeZKqeFAXcp2IBucFRDH 9h7yxSnUBBkbqVF+PbUVt3nwn83jDETe/KnEcDcQy//cuij8P1kafo0Hwr8jGRUI08oV +u9F+atdRU/2RqlZw1AZvgMKvq/ewOi3HF2iAOrtW4ZVpZw3dci57u30jIRiSRY5wm7A TPuRhEs3VqP6xZNtJ0w3BMWBemu9v1Y70mBjNwmbxIxUn16PCemXRHm9ZttZZhAynKr3 LfzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ivbfTSqiGJDKhzmVN6iFRasV8Zko5oH9CCWYtxUwt+M=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=G12x33Ru96o+cUMcQBYFUh5NkVS+El7BbmFKZtzJFuAEO3tli6dVij+FzmIi/pCxB1 /P43KlCXvqbbokXruFBq2W/63wxnoiGB4htQpCPWhdKdAtK1LIGbT9OVrsHw5WkSpztX 9NGCtaY0hxwPVUpx2q90ofcZc1P7+RXW+nzTWNr3Vui1SYL6PNOt6izEwAOPtn0Fw+2N UwvqWRqdy4SnMabTLa2hxdrh5vyYsJdWLpmTtCxbubNsOIJWOxOyIGhwwKR6huNHarqz HZCTqcRS1L9GGcaYA542GbEq1w7sY2dVcL4fF5CqOMsTN/UZk4TOKgxJtkUVZC9h0ST3 z2Zw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id z6-20020a633306000000b005740ab95917si1380603pgz.45.2023.09.21.12.39.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 12:39:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id EEBF383328DB; Thu, 21 Sep 2023 12:29:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230016AbjIUT3Q (ORCPT + 28 others); Thu, 21 Sep 2023 15:29:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230364AbjIUT2y (ORCPT ); Thu, 21 Sep 2023 15:28:54 -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 6237C3AB0 for ; Thu, 21 Sep 2023 10:09:38 -0700 (PDT) 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-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 21 Sep 2023 12:29:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777677271239184131 X-GMAIL-MSGID: 1777677271239184131 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();