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) { }