From patchwork Wed Mar 27 06:09:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinjiang Tu X-Patchwork-Id: 13605606 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 0B10AC47DD9 for ; Wed, 27 Mar 2024 06:09:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D37E76B0095; Wed, 27 Mar 2024 02:09:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEE356B0096; Wed, 27 Mar 2024 02:09:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBEB96B0095; Wed, 27 Mar 2024 02:09:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id ABBB76B0095 for ; Wed, 27 Mar 2024 02:09:54 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4EC7614062E for ; Wed, 27 Mar 2024 06:09:54 +0000 (UTC) X-FDA: 81941793108.21.D1B0D02 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf15.hostedemail.com (Postfix) with ESMTP id 1E490A0005 for ; Wed, 27 Mar 2024 06:09:51 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711519792; a=rsa-sha256; cv=none; b=zVtKtQTaYjwDjpgb4k6/IPRdyh2Cjb94r2rfmg4tb1X54hKxCga0Pa4nkYUG0nMpKQZfwn rK6GfQH4JdJa1G/96Y8cKf+kmqYNS7H0ubqrtVt2OdM1gNw4hkhGrilZ6Yp8bKxr6Ih2vh hGkKpRIkWF0weerRb+ykPlM9X4vP43Y= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of tujinjiang@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=tujinjiang@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711519792; 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=VSYd4pTxe45tA5Bx2we1UFSyrEfDYpNqJnIAcY3hM+s=; b=mPZ//A0m3ZHINZLzhmEQIxsTZ9ds8HpLMN1vNuhZB1w6wvica2aOkyzKBZV8xIGFqY1dMd NcIkaD1+v6YCb1+RlpF/YP39BHOvrGILwr9Y1CX1R7KfqTBl+FM4iZuOaLRvAPGC0fbbmk rlyaPRLi1QUoK+2/rVMe5uRoPbtmWJw= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4V4GRd73Rhz1xsc4; Wed, 27 Mar 2024 14:07:41 +0800 (CST) Received: from dggpeml500011.china.huawei.com (unknown [7.185.36.84]) by mail.maildlp.com (Postfix) with ESMTPS id 40A751402CA; Wed, 27 Mar 2024 14:09:42 +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; Wed, 27 Mar 2024 14:09:41 +0800 From: Jinjiang Tu To: , , , , , , , CC: Subject: [PATCH v3 1/3] mm/ksm: fix ksm exec support for prctl Date: Wed, 27 Mar 2024 14:09:20 +0800 Message-ID: <20240327060922.1484395-2-tujinjiang@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240327060922.1484395-1-tujinjiang@huawei.com> References: <20240327060922.1484395-1-tujinjiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpeml500011.china.huawei.com (7.185.36.84) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1E490A0005 X-Stat-Signature: i1hjmodmmjairpe7t6r41o6hpbkjobic X-HE-Tag: 1711519791-581099 X-HE-Meta: U2FsdGVkX1+m6ANCjDswk5TL71o104kXdjf0kplQwcEz0lJK6HvXZkMQvTnJ4IjIpm7Q9Q16/5jkm0R3hS1R2OovaZvz9pN39zjI8KbUB45FT2NRjL2q9pZ68oN7UforgcAH7zBVwBgSiIIB+viYUsphhbhjGkcFA+/s+m8SOVb6cuRf7R75jiepRbt4kvJz/PRzzKMGU3TXdcr9AHF4CfwN1cOfT2fimh8MnLfoW/ANqUgmLfRGDm5FHQbkpChaSSwBfuIVOVWIMF/MtY5torlmdCdSNwRIWydZnBqqKsm2w04kAQRRKGtNyeZm/WR1ji6K4AX1TuScj7URIig3e6Epdz7bL2nbUYY+EvdX6qd6zjSlf0zrREYm0YSkfVKwQXLSvoXxCcHQsjbPeGYuqlHBTzHIcTvqrSYpZ31KDU6rMX+bIVkataaq3ctSfuFgAxHRjePCRh4BRrTV5G794YFpTxdnWIQ2T1D/JPv5mgw327beeOjyfv8/8RK6R52vAnrcyox06F3n3xNHpH9Mneu8ktGx8QlJMMDy6paHomDQryju9ZmdbwiT1hQaIQENe4nuD2vtilIo7I4bg5HuV7hLMeTXMQOw3Mnixx2z4sA3ZU86yglmF9vFXHxQobd+lvIJniP3zkXX38s6FEttjBC2FVPbRYI/JM5cgJ/hnX5k5s4wdy+FIydTRjcp73vehqj7dMrhHAF9BR1PPrMWbG+1F5too+ee141KFCG+czvmWpHrwIpEior120y63pVjGFR1aVaIiJHr1Xz3WGOb6bHsWiB9X6dEu9adXDnThF9NoYk+IOirhQdEwk+0uuJiqloNHO5vFlrI1WLcGWi1vJooxnV2v31nMUZlBO15cjKzO97ycWr70GtMh+J/skdXB2nrb7VYLug+x6zmzXDTegXRETpDedyibnMWLWyHW6DbQ8I6oE9Yv4Zl5dcOFx1x9JPYjKQwMnIvmHWJqcD B3OP/TEW Ypcl8kLjQti+/7rPWrMTg3Q8jPRyD9QIEsVQvB5AtigRcXsW8ylRLcIxx21CozJGIqyN0mPYGDNfOBbSHrVGdafpyqUNbb6GpR8M11jkr9Pfuw3YKRv38LlWJuPg+eucL6iejkYBdwrxOgdPrt4lMIaK/0QTb0K2AusXUVxECUDuVzPJ6yjJsMpONnQ== 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 Reviewed-by: David Hildenbrand --- fs/exec.c | 11 +++++++++++ include/linux/ksm.h | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/fs/exec.c b/fs/exec.c index ff6f26671cfc..c2890d32232e 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -67,6 +67,7 @@ #include #include #include +#include #include #include @@ -267,6 +268,14 @@ 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. + */ + err = ksm_execve(mm); + if (err) + 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 +297,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) { }