From patchwork Fri Mar 22 06:09:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjiang Tu X-Patchwork-Id: 13599649 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 384E6C54E71 for ; Fri, 22 Mar 2024 06:10:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 006FD6B0087; Fri, 22 Mar 2024 02:10:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED2AC6B0088; Fri, 22 Mar 2024 02:10:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4C646B0089; Fri, 22 Mar 2024 02:10:02 -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 C5FEF6B0087 for ; Fri, 22 Mar 2024 02:10:02 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 86079160588 for ; Fri, 22 Mar 2024 06:10:02 +0000 (UTC) X-FDA: 81923649444.18.41342D5 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf07.hostedemail.com (Postfix) with ESMTP id 7F1714000D for ; Fri, 22 Mar 2024 06:09:59 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf07.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711087800; a=rsa-sha256; cv=none; b=nQNHeyPrqOTpeSCXFGU1OyvRTWN2PgAQHFYhZqHB2skDC4zmyTLixStiooLD+UAChFFqGO tY7NPXALPDrI6h4jFMof73gJyeGnjHRW99jP13vnvusT7TaPnpds53fmuRCVfvAcCuP8Zh Z2/QkpNudwvaWzI2hZULxxSEAUCb8rY= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf07.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711087800; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ueDHausuphOTBktRKr1yyjC7vR97kx3AxoW1QG6HpqA=; b=i96ZZyvpegmU4DaoC3dcNot2H4BJ95EuUoQhelPD0/+K2rAJlgVOo51ERJA6Gz7yA/4sfP M39jh7VHqftJOdJNRJDueWAgTs99xry0o7BW2RyqbkCFw+pOsgHm/yukjxxTxqUw+PsU7J S+QYOYSw2PJ70XnS6sZJM56wiMrSPmU= Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4V1BgY1VGdzwPxs; Fri, 22 Mar 2024 14:07:21 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id D1DEB1400E4; Fri, 22 Mar 2024 14:09:55 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Fri, 22 Mar 2024 14:09:55 +0800 From: Jinjiang Tu To: , , , , , , , CC: Subject: [PATCH v2 1/2] mm/ksm: fix ksm exec support for prctl Date: Fri, 22 Mar 2024 14:09:46 +0800 Message-ID: <20240322060947.3254967-2-tujinjiang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240322060947.3254967-1-tujinjiang@huawei.com> References: <20240322060947.3254967-1-tujinjiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500011.china.huawei.com (7.185.36.84) X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 7F1714000D X-Stat-Signature: c1dzx64tgm7j3coq6ydsuiqxi5hpj5ah X-Rspam-User: X-HE-Tag: 1711087799-961070 X-HE-Meta: U2FsdGVkX18+4xc4BnxJmscwv/0oC3xhMiMK3cxLYNCOBbFMstGbdBECxN/db6ZysdjOv4/tTB6m3up0MNj6fAY/rhVE1gm5kKGBMC5gr9w5dNFcdkSEpzjCrSgxxdf6nLBnryKp+PoIdrMMoAKnatAyCyTxyyoFoT/UT/O/6PJE2wrEzuu4DlzDXTgFmAJwhT+qjtSe8wjY5rVOhjoxvTXape4XYkyH9meTwkygPQRoU32IX8SGIpCzQQI49994n1i2ue73LZiXbZByprP2hjMMJGdjZFXYJdW/J/61Urm4vBWkNcCBVZy7Garp8k1ZQsk8OGqa2g7tBvIkoHxLEOFq4HpT4a+WkdsZxueZ1b2IbqRbXa9K3O6o/IVVs/1+sv8JkYrCrr8YBhA89oWiP7ArjgMh4hKJaj3sWcuPmIukT9lvr/Xr6gNwZkzymkaOGjbmytY0PrJ8qbSNYwUVKUjugQLVHyUha2jBFGxL3CmUv2aVAGFfMt7lSI775hEx7dkKvHMo/UHitt1Uqbx2lahJRMunY8CRtXR+32VIK+qfP6F8DxLMzz4XNfDTYrDvD/e1c7x5iCWXAej0/LULVkjt64h+TqVPpLQofLpDjSMwSzmWAXLhZeqJigoI3z7e9u28Mi0CYjzFV3kZF06N+WgLcS6Ta/FaHTKWRjNSLctjzheMIUPwX9tdHjOuKeTvCRGjBIXKu2GDc7gaWBsJFCcc3RfpMxEV1zGJyLWjZDZcaVeUxVOnTYb1N0gqdwzIXZgjppPENwMa8MQZzTXtdWx23nKxjzhuiQ7ea8BPgsNnNkrsoMpxDCrzH4oFWd3gD0llHvmCW393IERMcBoYBl0vottWEG/8 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: List-Subscribe: List-Unsubscribe: commit 3c6f33b7273a ("mm/ksm: support fork/exec for prctl") inherits MMF_VM_MERGE_ANY flag when a task calls execve(). Howerver, it doesn't create the mm_slot, so ksmd will not try to scan this task. To fix it, allocate and add the mm_slot to ksm_mm_head in __bprm_mm_init() when the mm has MMF_VM_MERGE_ANY flag. Fixes: 3c6f33b7273a ("mm/ksm: support fork/exec for prctl") Signed-off-by: Jinjiang Tu --- fs/exec.c | 10 ++++++++++ include/linux/ksm.h | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index ff6f26671cfc..66202d016a0a 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -67,6 +67,7 @@ #include #include #include +#include #include #include @@ -267,6 +268,13 @@ static int __bprm_mm_init(struct linux_binprm *bprm) goto err_free; } + /* + * Need to be called with mmap write lock + * held, to avoid race with ksmd. + */ + if (ksm_execve(mm)) + goto err_ksm; + /* * Place the stack at the largest stack address the architecture * supports. Later, we'll move this to an appropriate place. We don't @@ -288,6 +296,8 @@ static int __bprm_mm_init(struct linux_binprm *bprm) bprm->p = vma->vm_end - sizeof(void *); return 0; err: + ksm_exit(mm); +err_ksm: mmap_write_unlock(mm); err_free: bprm->vma = NULL; diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 401348e9f92b..7e2b1de3996a 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -59,6 +59,14 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) return 0; } +static inline int ksm_execve(struct mm_struct *mm) +{ + if (test_bit(MMF_VM_MERGE_ANY, &mm->flags)) + return __ksm_enter(mm); + + return 0; +} + static inline void ksm_exit(struct mm_struct *mm) { if (test_bit(MMF_VM_MERGEABLE, &mm->flags)) @@ -107,6 +115,11 @@ static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) return 0; } +static inline int ksm_execve(struct mm_struct *mm) +{ + return 0; +} + static inline void ksm_exit(struct mm_struct *mm) { } From patchwork Fri Mar 22 06:09:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjiang Tu X-Patchwork-Id: 13599650 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 8B967C47DD9 for ; Fri, 22 Mar 2024 06:10:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 932916B0088; Fri, 22 Mar 2024 02:10:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B9326B0089; Fri, 22 Mar 2024 02:10:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E4F16B008A; Fri, 22 Mar 2024 02:10:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5C1DC6B0088 for ; Fri, 22 Mar 2024 02:10:03 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 35866808A3 for ; Fri, 22 Mar 2024 06:10:03 +0000 (UTC) X-FDA: 81923649486.16.CDEC022 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf09.hostedemail.com (Postfix) with ESMTP id CC33A140006 for ; Fri, 22 Mar 2024 06:09:59 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711087800; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ISPprPTwgFlA+7H9kKmGRMZFwE1OMNUfyQNJmOIsMYY=; b=2GsZqMJl1RSKToMqel34sOSJ5CYkEMpois8qXCYeFKX6IR15GCYqzZYpnkV97EydMnV20O WY8+myLBC7Q5nWrEJrIvo18Ru6QbirRGaWOHNesC2gGw5A4Ewn0qiyETS9z2EiDUv1dW9x mZ2imIC7qxvBfqyDlZ8UgxjWIVYhJ9Q= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711087800; a=rsa-sha256; cv=none; b=Jaee4WJCPZZTZ4BsLPVCRW/ANSRDMMb1KchrTkodsN1a29QNNNfujhWs4UJPyxPihwRB5Y ZauJw50g052ncRlZsrSKySLgkiEyEjLC13ZOcKWmmkG2AUhM6tuk9J+OUAUfcywAED6B0i G3dMCODfjg3Healy402SNlKQ8bSXM5Q= Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4V1BhK3XYXz1xsN9; Fri, 22 Mar 2024 14:08:01 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id 51C1C140124; Fri, 22 Mar 2024 14:09:56 +0800 (CST) Received: from huawei.com (10.175.113.32) by dggpeml500011.china.huawei.com (7.185.36.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Fri, 22 Mar 2024 14:09:55 +0800 From: Jinjiang Tu To: , , , , , , , CC: Subject: [PATCH v2 2/2] selftest/mm: ksm_functional_tests: extend test case for ksm fork/exec Date: Fri, 22 Mar 2024 14:09:47 +0800 Message-ID: <20240322060947.3254967-3-tujinjiang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240322060947.3254967-1-tujinjiang@huawei.com> References: <20240322060947.3254967-1-tujinjiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml500011.china.huawei.com (7.185.36.84) X-Rspamd-Queue-Id: CC33A140006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: yjhjauchyzjihwgt7rwdm66694gy67qi X-HE-Tag: 1711087799-286937 X-HE-Meta: U2FsdGVkX1+3+A03nY/sx/oeoVe1kMFv49EzrUTOMUgbx/GlFg6j6/vo/0B/UrdbvOoehahmcwG3ee0L/G8bu31s7lvI6FF14dHEOlO2KMxI971q4X3rGJhhS9Ffkr+ungWjP4ssTFyiZ87XXdHKTdSRvr7tf0XDto1kH8Oc1hzBnKm7sToPfYVFN6w3t4+RveqK2+qaWOXe+wiZcC9nx48hEAgnyg7+xY3C2TcTS9yAlPVlrarPKPcb5kw20rVtMYUbyR5lQxM24DxcXrLnEikuzW2FX6WGy27a+zpwxbmmnH4N6q00Fmbyr7AiiaPbgqjoxlGgKkdhpYDaspKHHtUHohl2mLj2dg3t2I5mRgAIndRWE/9g2iyG+1A3QifjDjzxd+L2MjqqqxeCzbk0cnkJSgkIUOBrsIescy2L0A6kAYKaRFGBKO+3cIG4TH+Hg9i8n1XAHV9uSTqa/5bl6eHgjWwSB7XSIb/ghVdNbwhPS2dqS8JOPnLftOA1ElDgNWVWi7C4rPcg3oOop/nWciTJoigglbHhUw4pYt9+5qTGzml5/JLO/vJ0niTAC7bn4ioxsnYSUaQJudJ9d7+eCgMyv6O4e+OFNKAMQSFh8G5qeWOP0SC1MdzpqYDDpr1OqF2i6M7m6rlDmd5ePCZzUb7pdGiz0f44nKR4zpHa4ZxRupydjLnwnELaKk2KeCgZrwvBHV5m9aYaJbBsd4VkMGvt2DTCJ+pMgwJD60nunivSlxtKG85gmOOumvXnOPv5DNlOOb02rToldaP1/oT1eJwNxKw2I6k4yoCPe6InS7KKkRTJLDiOIgpATD7vQcxF5WHrVGsozk0+pbyLjcw874RYBq++D088eJrrqlzRqaOv9jL11Qxla4Flm+xTqc+w0vZRaS3ZRr1dTRh1728DI2ouNSIXt7aC0QgDqkptjccTvQOdgT82tZ0nEWMtsxsjc0soU+ix6xB/Q3JXEwt Ec478tgw IbUBLr88qnkdLg7v+2CunVnoRXD5HEF3LtmSoY+ttkMzI2YiTZtpG7zdxbAlGOQHmm3DjxsIlptcymzEcZKnwviC6pLWCCY0lDsEPQNxDzzw0Ra/DG0bUaKk4f0ssJyhweOmIRUVN+9T8E5hTualPcdRfKvVCInCnYMxptbg/efBqXanf12jM7ojqh53Vmkx1mj3Q 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: List-Subscribe: List-Unsubscribe: This extends test_prctl_fork() and test_prctl_fork_exec() to make sure that deduplication really happens, instead of only test the MMF_VM_MERGE_ANY flag is set. Signed-off-by: Jinjiang Tu --- .../selftests/mm/ksm_functional_tests.c | 79 +++++++++++++++++-- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index d615767e396b..01999aab2e37 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -146,6 +146,54 @@ static int ksm_unmerge(void) return 0; } +static int child_test_merge(void) +{ + const unsigned int size = 2 * MiB; + char *map; + int ret = -1; + + /* Stabilize accounting by disabling KSM completely. */ + if (ksm_unmerge()) { + ksft_print_msg("Disabling (unmerging) KSM failed\n"); + return ret; + } + + if (get_my_merging_pages() > 0) { + ksft_print_msg("Still pages merged\n"); + return ret; + } + + map = mmap(NULL, size, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_ANON, -1, 0); + if (map == MAP_FAILED) { + ksft_print_msg("mmap() failed\n"); + return ret; + } + + /* Don't use THP. Ignore if THP are not around on a kernel. */ + if (madvise(map, size, MADV_NOHUGEPAGE) && errno != EINVAL) { + ksft_print_msg("MADV_NOHUGEPAGE failed\n"); + goto unmap; + } + + memset(map, 0x1c, size); + + if (ksm_merge()) { + ksft_print_msg("Running KSM failed\n"); + goto unmap; + } + + if (get_my_merging_pages() <= 0) { + ksft_print_msg("Fail to merge\n"); + goto unmap; + } + + ret = 0; +unmap: + munmap(map, size); + return ret; +} + static char *mmap_and_merge_range(char val, unsigned long size, int prot, bool use_prctl) { @@ -458,7 +506,11 @@ static void test_prctl_fork(void) child_pid = fork(); if (!child_pid) { - exit(prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)); + if (prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) != 1) + exit(-1); + if (child_test_merge() != 0) + exit(-2); + exit(0); } else if (child_pid < 0) { ksft_test_result_fail("fork() failed\n"); return; @@ -467,8 +519,14 @@ static void test_prctl_fork(void) if (waitpid(child_pid, &status, 0) < 0) { ksft_test_result_fail("waitpid() failed\n"); return; - } else if (WEXITSTATUS(status) != 1) { - ksft_test_result_fail("unexpected PR_GET_MEMORY_MERGE result in child\n"); + } + + status = WEXITSTATUS(status); + if (status != 0) { + if (status == -1) + ksft_test_result_fail("unexpected PR_GET_MEMORY_MERGE result in child\n"); + else + ksft_test_result_fail("fail to merge in child\n"); return; } @@ -483,7 +541,13 @@ static void test_prctl_fork(void) 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; + if (prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0) != 1) + return -1; + + if (child_test_merge() != 0) + return -2; + + return 0; } static void test_prctl_fork_exec(void) @@ -517,9 +581,12 @@ static void test_prctl_fork_exec(void) if (waitpid(child_pid, &status, 0) > 0) { if (WIFEXITED(status)) { status = WEXITSTATUS(status); - if (status) { + if (status == -1) { ksft_test_result_fail("KSM not enabled\n"); return; + } else if (status == -2) { + ksft_test_result_fail("fail to merge in child\n"); + return; } } else { ksft_test_result_fail("program didn't terminate normally\n"); @@ -599,7 +666,7 @@ int main(int argc, char **argv) int err; if (argc > 1 && !strcmp(argv[1], FORK_EXEC_CHILD_PRG_NAME)) { - exit(ksm_fork_exec_child() == 1 ? 0 : 1); + exit(ksm_fork_exec_child()); } #ifdef __NR_userfaultfd