From patchwork Fri Jan 10 09:40:34 2025 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: 13934114 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 2D9B0E77188 for ; Fri, 10 Jan 2025 09:40:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DBC18D0003; Fri, 10 Jan 2025 04:40:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 98BCC8D0002; Fri, 10 Jan 2025 04:40:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 853FA8D0003; Fri, 10 Jan 2025 04:40:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 591A78D0002 for ; Fri, 10 Jan 2025 04:40:49 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 218054460B for ; Fri, 10 Jan 2025 09:40:48 +0000 (UTC) X-FDA: 82991047776.13.7FD3E42 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf26.hostedemail.com (Postfix) with ESMTP id 32162140010 for ; Fri, 10 Jan 2025 09:40:44 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; spf=pass (imf26.hostedemail.com: domain of xu.xin16@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=xu.xin16@zte.com.cn; dmarc=pass (policy=none) header.from=zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736502046; 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=5GF92Kng9ekTxSZn3dDJZ/OryN1PUn4dWYxzRNa33KU=; b=Z29qp0lFqVYsAGsRsD47MGrMoz5MNldk2NasN7PcHNbzP04OSWUVldT5g5+2+HJVyj8HWa 4s+Qu+01TJPSvz/DVjRa82YFwxnRLUtbFiS305BphEngz4mzW+/4Nk7JI5rZxmoRBUSsOt 38iwBRPVBEvTR20bDP9bIObztB5U/GU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736502046; a=rsa-sha256; cv=none; b=W0aahJbBgXA/qS5xjzP+YevxjGc4pOffz6lWtRp8pjuRaMDqVubPDxaErArjCyUPH2ibnM gHJWqTd1blasFMq+veNpsoSLvvzK/YEWSBsPmD3RZYzyNX7MGMnxdZtc+utX060WMFUCVY eF7cYOiRmocDAsfzgcCbD4uMXAbLE/E= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; spf=pass (imf26.hostedemail.com: domain of xu.xin16@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=xu.xin16@zte.com.cn; dmarc=pass (policy=none) header.from=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4YTxTx0N7Rz501bN; Fri, 10 Jan 2025 17:40:37 +0800 (CST) Received: from xaxapp02.zte.com.cn ([10.88.97.241]) by mse-fl1.zte.com.cn with SMTP id 50A9eVaD045689; Fri, 10 Jan 2025 17:40:31 +0800 (+08) (envelope-from xu.xin16@zte.com.cn) Received: from mapi (xaxapp04[null]) by mapi (Zmail) with MAPI id mid32; Fri, 10 Jan 2025 17:40:34 +0800 (CST) Date: Fri, 10 Jan 2025 17:40:34 +0800 (CST) X-Zmail-TransId: 2afb6780eb12ffffffff881-70433 X-Mailer: Zmail v1.0 Message-ID: <20250110174034304QOb8eDoqtFkp3_t8mqnqc@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , Subject: =?utf-8?q?=5BPATCH_v5=5D_ksm=3A_add_ksm_involvement_information_for?= =?utf-8?q?_each_process?= X-MAIL: mse-fl1.zte.com.cn 50A9eVaD045689 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6780EB15.000/4YTxTx0N7Rz501bN X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 32162140010 X-Stat-Signature: dg1wag8mdjmz8qami8mh5zwwxjaem7d3 X-Rspam-User: X-HE-Tag: 1736502044-381512 X-HE-Meta: U2FsdGVkX1+NfWp0YceiWij+qt7cey+NF7rrewFSFkZn4r2RQwT4WTWXybX4lLMdGdNGy3leIFkenPC7RDHJ4T3RTa0Lw3n4+qLQC2BQ1Tu+SQSZ3b+SrOjRaXPGW7bEkx3tAWOF/ljUjoDSiWcNoem6L9zmXrg6gWrFFK+6DQROKOEgRISBCTYCFDgooL/XkgthJd1M8FN6lenqnxctLqXEdHAVHLEWKSB8mwJZKNP3I3tfWLyFrUcw1ZRK1UYwXXjuBudczVHMPyQ/MygazNxmlt32DD2RCGsracWmWf4peIrFMaRY+td4BBbwDfdtDtPe7xmue1jmiLPEh4IiDukY0YIC4FnP+qyr7q0DB6buOWZDM16OdeHPw/s6sdvhUeIYfe+is8awesLvOPvejgBfonVoQE5U08ZXbP4UxRd/eInqOdPTRSxnBuiRKFOcLVpc3tbbi52b9Xw5NXNEXojN5BhDz8qXcgceCbYiviCMUjp5n8bhJsCQBUup8BPMgrnYKFOpXND5D3jPnqeKtfgOHI3USCmyBAd19JD3N13VTS5kpDvR1l7efosuTxj8uoll2D9no9uHa5LM1Syh1NzOgQ+aVcbu5Xb0d05/SNFV1rGil9A/9Tb1eOQCRwK2KkOWwuxCFtqJWHRRyOezBvphjXyUm74mWqIYbhfFIZFmFhPCse8ha9TlhTtSxcyGhBg7GYFzw/5u7uOynVXKyCxsF17hPrBtZFTQzROcPM6x2CHAOmImmI+WZUwg7Dz0c+nXJLdrUBBdgy7D2gzcerT8+Jl4RcT+gvvt+oLXLn0dLdQrQGDho+XOoyf76IL1bEtmMmsyq7PDBKFp4bc1x2v16g8YTniijEa+oqSS1va+h51ywwSx22LDWI17RdiHUzsRFX28GvXWeRiRVgEnOfQigzyiJTMsELR4hJdtKZxPrRmLVUjRHiiTtMChXiBjW47K+V/1spzHJ4IMbFM I6ZypLLt vxO62tpgl/FhaHnrANFy0KDqWnspDLUEBdPiiq9rg8SkZZkcKNz+MQv4lWRX8iCVTrzJf8UNbCqrA8QB7Y7LBGsck26YVoEM9HRz8rut++RXiq7fp4Ur6poolrCh0DqmzLZONOnFXaL/PYKauOaZU+IfpeuTEqyrqyVRDr87+MKe3tXVo3VtmR+sWh3GujULycwrBu/TgWUFlaDMRVYepP0LaeUVQRSMvRqyLOYEP3fNyu1c+BCJW7ziDd8Ir5dbNE0+1xAxn/AhBNCgr2GtSw8VLKg== 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_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. ksm_mergeable: yes/no whether any VMAs of the process'mm are currently applicable to KSM. Purpose ======= These two items are just to improve the observability of KSM at process level, so that users can know if a certain process has enable KSM. For example, if without these two items, when we look at /proc//ksm_stat and there's no merging pages found, We are not sure whether it is because KSM was not enabled or because KSM did not successfully merge any pages. Althrough "mg" in /proc//smaps indicate VM_MERGEABLE, it's opaque and not very obvious for non professionals. Signed-off-by: xu xin Cc: Wang Yaxin --- Changelog v4 -> v5: 1. Update the documentation. 2. Correct a comment sentence and add purpose statment in commit message. --- Documentation/filesystems/proc.rst | 66 ++++++++++++++++++++++++++++++++++++++ fs/proc/base.c | 11 +++++++ include/linux/ksm.h | 1 + mm/ksm.c | 19 +++++++++++ 4 files changed, 97 insertions(+) diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index 6a882c57a7e7..916f83203de0 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -48,6 +48,7 @@ fixes/update part 1.1 Stefani Seibold June 9 2009 3.11 /proc//patch_state - Livepatch patch operation state 3.12 /proc//arch_status - Task architecture specific information 3.13 /proc//fd - List of symlinks to open files + 3.14 /proc//fd for fast access. ------------------------------------------------------- +3.14 /proc//ksm_merging_pages shows. + +ksm_process_profit +^^^^^^^^^^^^^^^^^^ + +The profit that KSM brings (Saved bytes). KSM can save memory by merging +identical pages, but also can consume additional memory, because it needs +to generate a number of rmap_items to save each scanned page's brief rmap +information. Some of these pages may be merged, but some may not be abled +to be merged after being checked several times, which are unprofitable +memory consumed. + +ksm_merge_any +^^^^^^^^^^^^^ + +It specifies whether the process'mm is added by prctl() into the candidate list +of KSM or not, and KSM scanning is fully enabled at process level. + +ksm_mergeable +^^^^^^^^^^^^^ + +It specifies whether any VMAs of the process'mm are currently applicable to KSM. + +More information about KSM can be found at Documentation/admin-guide/mm/ksm.rst. + Chapter 4: Configuring procfs ============================= 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..be2eb1778225 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 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 -