From patchwork Tue Dec 3 11:26:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: xu.xin16@zte.com.cn X-Patchwork-Id: 13892248 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 9B8AFE64A81 for ; Tue, 3 Dec 2024 11:26:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED7A26B007B; Tue, 3 Dec 2024 06:26:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E607B6B0083; Tue, 3 Dec 2024 06:26:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D021C6B0089; Tue, 3 Dec 2024 06:26:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id AE4CF6B007B for ; Tue, 3 Dec 2024 06:26:51 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 62F5AAE399 for ; Tue, 3 Dec 2024 11:26:51 +0000 (UTC) X-FDA: 82853420244.22.45673CF Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by imf07.hostedemail.com (Postfix) with ESMTP id 614934000C for ; Tue, 3 Dec 2024 11:26:35 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf07.hostedemail.com: domain of xu.xin16@zte.com.cn designates 63.216.63.40 as permitted sender) smtp.mailfrom=xu.xin16@zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733225196; 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:in-reply-to: references; bh=bsTViP2382ZzD1j1wGXr8eNqwz4iFFKi7qB21LTeDgQ=; b=fYjThGCMeBTNYewgJfxQTwTEtJ2sVQ1w2ZvuE8wXrcN4sOXsv2/b3pHq2IlOVj2tNywya/ 3Nes1F8Pbytn5k+jcJ7Cah4N+w2LEbu+ozAX2NojHGDzRlZ5FyyGbmttO9K4lD7Kl22t64 gUHBkqM4jZHYWl5SSCDECgw9Fra+G5w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733225196; a=rsa-sha256; cv=none; b=VMYomOpxUDqwdc/DmQTaVL7JUriTv5zDkmV9K5V0ZK4xiPqZpj4pjflhOdqpNyes/cvLB1 bS5TXrNPELC4lC9JMMMFRgbX5eYvWKM9Rng2h2EMcDq1R6xqqx+N/N6qhRI28dOJnj0V4f Pfy58BKXQUteceit+HuAQMowHLEWWHs= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf07.hostedemail.com: domain of xu.xin16@zte.com.cn designates 63.216.63.40 as permitted sender) smtp.mailfrom=xu.xin16@zte.com.cn Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4Y2ddp0qMKz8R040; Tue, 3 Dec 2024 19:26:38 +0800 (CST) Received: from xaxapp01.zte.com.cn ([10.88.99.176]) by mse-fl2.zte.com.cn with SMTP id 4B3BQUho000945; Tue, 3 Dec 2024 19:26:31 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp04[null]) by mapi (Zmail) with MAPI id mid32; Tue, 3 Dec 2024 19:26:33 +0800 (CST) Date: Tue, 3 Dec 2024 19:26:33 +0800 (CST) X-Zmail-TransId: 2afb674eeae972d-c1081 X-Mailer: Zmail v1.0 Message-ID: <20241203192633836RVHhkoK1Amnqjt84D4Ryd@zte.com.cn> Mime-Version: 1.0 From: To: , Cc: , , , Subject: =?utf-8?q?=5BPATCH_linux-next_v4=5D_ksm=3A_add_ksm_involvement_info?= =?utf-8?q?rmation_for_each_process?= X-MAIL: mse-fl2.zte.com.cn 4B3BQUho000945 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 674EEAEE.000/4Y2ddp0qMKz8R040 X-Rspamd-Queue-Id: 614934000C X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: nxwaf6ke3ww3cw9irr9g9fiyjf9ckrba X-HE-Tag: 1733225195-770226 X-HE-Meta: U2FsdGVkX1+sb4c0Mbl8K7taKZph1ijjMkAMslUa9hDfl9TO9r1ViDRoim1h98py8hXRvjE5gEi0TAcQF4kpCe+ccmj6Np7ekIy0Bm4PtmEqomAhSywo0Quef12WDqHs6vREHRMnHT7iaZcr8LpBBEhQzawoQtSgmEj6s/x81fTldRXBZPVueU0vTZ30Uy22T5JhYgh3gXT22JNJB34vfu8OJ5WUIgpPCvJRCiJxeELLxxFkgGsuieoj0EB37OimRt8kkAWZgPHfZpmQIGz2oipVGVHAuXXmkG+iurdu3LY+bEPPumP0+p4IaVqLcD2cQiH7LSmDrrk1WVpZVlZq4fEk5GMfWL/0v7dNz883VJyNsdicceMx6G4DN5C/E6bd89sEFIaBRkZj0Yd9XgDZe5eI9lLJIPXP4DE2fStkAy+ttcBuAhRy6mvoa6XrVaNyqeZ/Fu150EzppunX0k2Vt1K9p6vowYkE0UmMWrj3Exv4CKXGu2InFeOk/RcIQ3pnnea11fe3e5+fvzEqz3hUuZmwC3rMb9E2zAwu6itjsEcH7gr3Lkg0VonPW2x2bO1B92ed4yQ63GZ32yrGidXQVH+B+wgmvRR+NHJY57yxnZLaAo2StDAJuMV+kIffZ+1XRbJSDbj1JbFQqMRiIcLJtBjVbjI39b7dYDuRVjU4hsW09WzbK6i1qR0Jn1C/82xjvHtBSGOqqN9tszEpmQeF2wvc97mz5UrjXOxRos2s2BiTwFirq7yhrKaizdIl+rh3JBjUyNfe/JMlUbM7GV2KnxQJt1i+yQqqdstoupnzyRJXhmjZt1hL16MwxRBofCPUuqjLKntQf0Gv/KzKn5Iaiu/0GjMs7oXI7CHTuMF3iRZLbvDnnJmbC2yiH1LqWxIw0Ll5ZUenzKHuQ9DT06dtiMdgeaiEugXuQ1T8UopR2jeD4pmB7CY/KUfCscVItnF5i3IJIwtTTE1rt1y7D53 6sf2kE71 fibQt3HkhHHro3n6MPap8iwUZzJK0ljUSgUD1NZ3IToRiE+ncnjS8ZsIF7QVkqUjCLjoToOY0c814UADYvdkwFhP1kaV3IZuUPryaLatkdJQkSgobo1Z6QYBIcHYffHxfNyKWQSVv0mYPXVGlQ6U0YTFTrqmvwEb8y3rrKnxuGT6nY81F2GnLUpwEwEz8idAQEFQ+/YI9m5y94s26I+o6BLlX1livsJulyHFa+JcZ5l40SLLqVLFcYzUSGYOQQbj8fzn65CdlYI8uFY0= 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: From: xu xin In /proc//ksm_stat, Add two extra ksm involvement items including KSM_mergeable and KSM_merge_any. It helps administrators to better know the system's KSM behavior at process level. KSM_mergeable: yes/no whether any VMAs of the process'mm are currently applicable to KSM. KSM_merge_any: yes/no whether the process'mm is added by prctl() into the candidate list of KSM or not, and fully enabled at process level. Changelog ========= v3 -> v4: 1. Keep the name of ksm items consistent in /proc/pid/ksm_stat. * KSM_mergeable -> ksm_mergeable * KSM_merge_any -> ksm_merge_any 2. Hold the read lock of mmap while calling ksm_process_mergeable() Suggested-by: https://lore.kernel.org/all/cec0ed06-b5d0-45aa-ad2b-eaca6dd7bacb@redhat.com/ v2 -> v3: Update the KSM_mergeable getting method: loop up if any vma is mergeable to KSM. https://lore.kernel.org/all/bc0e1cdd-2d9d-437c-8fc9-4df0e13c48c0@redhat.com/ v1 -> v2: replace the internal flag names with straightforward strings. * MMF_VM_MERGEABLE -> KSM_mergeable * MMF_VM_MERGE_ANY -> KSM_merge_any Signed-off-by: xu xin Cc: Wang Yaxin --- fs/proc/base.c | 11 +++++++++++ include/linux/ksm.h | 1 + mm/ksm.c | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index 0edf14a9840e..a50b222a5917 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3269,6 +3269,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task) { struct mm_struct *mm; + int ret = 0; mm = get_task_mm(task); if (mm) { @@ -3276,6 +3277,16 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm)); seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages); seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm)); + seq_printf(m, "ksm_merge_any: %s\n", + test_bit(MMF_VM_MERGE_ANY, &mm->flags) ? "yes" : "no"); + ret = mmap_read_lock_killable(mm); + if (ret) { + mmput(mm); + return ret; + } + seq_printf(m, "ksm_mergeable: %s\n", + ksm_process_mergeable(mm) ? "yes" : "no"); + mmap_read_unlock(mm); mmput(mm); } diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 6a53ac4885bb..d73095b5cd96 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -93,6 +93,7 @@ void folio_migrate_ksm(struct folio *newfolio, struct folio *folio); void collect_procs_ksm(const struct folio *folio, const struct page *page, struct list_head *to_kill, int force_early); long ksm_process_profit(struct mm_struct *); +bool ksm_process_mergeable(struct mm_struct *mm); #else /* !CONFIG_KSM */ diff --git a/mm/ksm.c b/mm/ksm.c index 7ac59cde626c..e87af149d5ee 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3263,6 +3263,25 @@ static void wait_while_offlining(void) #endif /* CONFIG_MEMORY_HOTREMOVE */ #ifdef CONFIG_PROC_FS +/* + * The process is mergeable only if any VMA (and which) is currently + * applicable to KSM. + * + * The mmap lock must be held in read mode. + */ +bool ksm_process_mergeable(struct mm_struct *mm) +{ + struct vm_area_struct *vma; + + mmap_assert_locked(mm); + VMA_ITERATOR(vmi, mm, 0); + for_each_vma(vmi, vma) + if (vma->vm_flags & VM_MERGEABLE) + return true; + + return false; +} + long ksm_process_profit(struct mm_struct *mm) { return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PAGE_SIZE -