From patchwork Wed May 24 05:57:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 98308 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2629234vqo; Tue, 23 May 2023 23:17:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7BFbjYTT6eNhNV/Qc6gLywBknKWFYhRqjKcvuCYRAHN7UdpOjDVzhzxSAWfRwoTaaqG0ov X-Received: by 2002:a17:902:d4d2:b0:1af:b5af:367b with SMTP id o18-20020a170902d4d200b001afb5af367bmr7175365plg.29.1684909043117; Tue, 23 May 2023 23:17:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684909043; cv=none; d=google.com; s=arc-20160816; b=UehIppS0KSyb2ERkOrzSel/TvXGvrpN6hJSeQ+udfIMvls2HYhnA/7mn54QTiq9usD jtkwsGEVNcaY6qZKYFPAeXmoCMIO205vTYb22qawqMDXB2+NcDom4i9jAIVpL3zyfchH zsFrq9w6021IErIW4FZIvNrvqoDGvbsDwlDuf3L+5ePCcH+Z3cZoVfM5zMxZ3HZLMErj U0pKygZd8DFCVLJVGxscXDmLv4XZPqfEu+f0eQXoUQrroFlrFndwt2Ws8nC2sdH1uKOQ JXvbE9DI73ITMHXxqtbLWpRFXxGAyaT3dPS3s0cmDZaLznUNEyUEivwzgHDhbrI1xD/0 c+fQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=7JI6wONS1ZOPxIh+CUvBMbRR/tf4apgOKD5okXa96g0=; b=fcqS9WlSVDSjsO6Hkz/JoeGugmQG3Ph6A13zS97AJ8efDYKQA8LYg0y4za35g8hsgK 5yq5c22u+1hvVjzAAGXqP2SI8GxuQVM/Ap3UZEEr3/I2UtWLs62ldinSS8prNfdgIFzd R7QMTiyhxYnQKSBkuiruQ3zdiFcwcQRGcB7nadlFtBhIGUsUN9pIPPKXCB+6vby3a9Q6 79Ipa7+a8qophjpoM5SA0uMzM9z3mPCB2uMlxkbEwVIvP9R2QN5/ENYpzLxwV3PTqNnX kysq88oF/fIwykK3WfAVRouA1SQLWNcEd0Q0rSSJVEqQ9g5Q+nrHFybOfwIvjEDMAns/ /qjQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bk13-20020a056a02028d00b0053ef1b5a506si2367025pgb.275.2023.05.23.23.17.07; Tue, 23 May 2023 23:17:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239552AbjEXF5f (ORCPT + 99 others); Wed, 24 May 2023 01:57:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239548AbjEXF5d (ORCPT ); Wed, 24 May 2023 01:57:33 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D104198 for ; Tue, 23 May 2023 22:57:30 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id A86C3E1EDF; Wed, 24 May 2023 13:57:13 +0800 (CST) From: Yang Yang To: akpm@linux-foundation.org, david@redhat.com Cc: yang.yang29@zte.com.cn, imbrenda@linux.ibm.com, jiang.xuexin@zte.com.cn, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ran.xiaokai@zte.com.cn, xu.xin.sc@gmail.com, xu.xin16@zte.com.cn Subject: [PATCH v9 1/5] ksm: support unsharing KSM-placed zero pages Date: Wed, 24 May 2023 13:57:11 +0800 Message-Id: <20230524055711.20387-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305241351365661923@zte.com.cn> References: <202305241351365661923@zte.com.cn> MIME-Version: 1.0 X-Spam-Status: No, score=3.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,FSL_HELO_NON_FQDN_1, HEADER_FROM_DIFFERENT_DOMAINS,HELO_NO_DOMAIN,NO_DNS_FOR_FROM, RCVD_IN_PBL,RDNS_NONE,SPF_SOFTFAIL,SPOOFED_FREEMAIL_NO_RDNS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: *** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766755184379528976?= X-GMAIL-MSGID: =?utf-8?q?1766755184379528976?= From: xu xin When use_zero_pages of ksm is enabled, madvise(addr, len, MADV_UNMERGEABLE) and other ways (like write 2 to /sys/kernel/mm/ksm/run) to trigger unsharing will *not* actually unshare the shared zeropage as placed by KSM (which is against the MADV_UNMERGEABLE documentation). As these KSM-placed zero pages are out of the control of KSM, the related counts of ksm pages don't expose how many zero pages are placed by KSM (these special zero pages are different from those initially mapped zero pages, because the zero pages mapped to MADV_UNMERGEABLE areas are expected to be a complete and unshared page). To not blindly unshare all shared zero_pages in applicable VMAs, the patch use pte_mkdirty (related with architecture) to mark KSM-placed zero pages. Thus, MADV_UNMERGEABLE will only unshare those KSM-placed zero pages. In addition, we'll reuse this mechanism to reliably identify KSM-placed ZeroPages to properly account for them (e.g., calculating the KSM profit that includes zeropages) in the latter patches. The patch will not degrade the performance of use_zero_pages as it doesn't change the way of merging empty pages in use_zero_pages's feature. Signed-off-by: xu xin Suggested-by: David Hildenbrand Cc: Claudio Imbrenda Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang Reviewed-by: David Hildenbrand --- include/linux/ksm.h | 8 ++++++++ mm/ksm.c | 11 ++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 899a314bc487..4fd5f4a50bac 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -26,6 +26,12 @@ int ksm_disable(struct mm_struct *mm); int __ksm_enter(struct mm_struct *mm); void __ksm_exit(struct mm_struct *mm); +/* + * To identify zeropages that were mapped by KSM, we reuse the dirty bit + * in the PTE. If the PTE is dirty, the zeropage was mapped by KSM when + * deduplicating memory. + */ +#define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte)) static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { @@ -95,6 +101,8 @@ static inline void ksm_exit(struct mm_struct *mm) { } +#define is_ksm_zero_pte(pte) 0 + #ifdef CONFIG_MEMORY_FAILURE static inline void collect_procs_ksm(struct page *page, struct list_head *to_kill, int force_early) diff --git a/mm/ksm.c b/mm/ksm.c index 0156bded3a66..f31c789406b1 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -447,7 +447,8 @@ static int break_ksm_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long nex if (is_migration_entry(entry)) page = pfn_swap_entry_to_page(entry); } - ret = page && PageKsm(page); + /* return 1 if the page is an normal ksm page or KSM-placed zero page */ + ret = (page && PageKsm(page)) || is_ksm_zero_pte(*pte); pte_unmap_unlock(pte, ptl); return ret; } @@ -1220,8 +1221,12 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, page_add_anon_rmap(kpage, vma, addr, RMAP_NONE); newpte = mk_pte(kpage, vma->vm_page_prot); } else { - newpte = pte_mkspecial(pfn_pte(page_to_pfn(kpage), - vma->vm_page_prot)); + /* + * Use pte_mkdirty to mark the zero page mapped by KSM, and then + * we can easily track all KSM-placed zero pages by checking if + * the dirty bit in zero page's PTE is set. + */ + newpte = pte_mkdirty(pte_mkspecial(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot))); /* * We're replacing an anonymous page with a zero page, which is * not anonymous. We need to do proper accounting otherwise we From patchwork Wed May 24 05:57:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 98314 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2633149vqo; Tue, 23 May 2023 23:28:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4ET7Qjtx42lpOOprGidFUvN1ev26o8UryNNpCzGelECg5CuuwEf/txPDuTczHTUMy4ykeE X-Received: by 2002:a17:903:1111:b0:1a2:8c7e:f310 with SMTP id n17-20020a170903111100b001a28c7ef310mr19767871plh.35.1684909681552; Tue, 23 May 2023 23:28:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684909681; cv=none; d=google.com; s=arc-20160816; b=QDsohKf6HwWX724RVsYr/ZI7mOYANB3yu5/Lvmpmwal6dhpiUIOhBuWfx6A5rdZrR5 wbWNK4972kyrZlgtyDoHpK06ly2tY4SX2lZnUz3ZsiGnQfNVr5r4Kq/D07qtBmJtl/4m 8sJicJOvDALzWx+Rlqdfop2zEnMy6VU4XR4Ek+R+aB/l8iLUEHgiogQizNmojyCu6wyp ooz+vDknrvhkQTVEOYTHkyxmNJVFuVombMm8SDd/Ci+KdhLX30X0+am+votm4ESu6WKp Lf+duJedu1kbhsmAeIvH6/YuKkoJh+aZTZNJW/EH1KuAarhJQFmIASSAzBPsymIWcAex yUdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=1L7B17ljpy0hISZ5buaBCwmdgaj11tB8hVWcEZQ2938=; b=g1kFY5ePZpg2hQOzFqFgQN2AfewZ5L6x8/on9GdbHuz4IvOK4X71jVfyNUMcDbN0kq 9Lfm8uRYNzbhgmAT+GuYb/ks/BDSGroXOMpxMwdXJB7LpgSX9Kr3IjHKj9kMm3FFtBK3 uEpaTNJDAKJIAcbpvKtwDkrALzY1brmR58YzmTj2HDbWrlcEFjKjVqP0ue9t4g6PiVJf yNWZcibBaiHHLRfEPsfdCpqUByq65vkrw2AsONXf4IKVOEeAc3UXAVWjB+EfuD6HbhO1 7XgVviyeDx1p/9SSoD9c9vhHKOIYSBNbfc2kFyQEkQ4UEu+G+WIL4l3mu6AaTs8p3W73 TNfg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r22-20020a170902be1600b001ab23ced2d2si363922pls.613.2023.05.23.23.27.46; Tue, 23 May 2023 23:28:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239560AbjEXF6P (ORCPT + 99 others); Wed, 24 May 2023 01:58:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233941AbjEXF6N (ORCPT ); Wed, 24 May 2023 01:58:13 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB4EC1B1 for ; Tue, 23 May 2023 22:58:00 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id 81B28E1EE2; Wed, 24 May 2023 13:57:54 +0800 (CST) From: Yang Yang To: akpm@linux-foundation.org, david@redhat.com Cc: yang.yang29@zte.com.cn, imbrenda@linux.ibm.com, jiang.xuexin@zte.com.cn, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ran.xiaokai@zte.com.cn, xu.xin.sc@gmail.com, xu.xin16@zte.com.cn Subject: [PATCH v9 2/5] ksm: count all zero pages placed by KSM Date: Wed, 24 May 2023 13:57:52 +0800 Message-Id: <20230524055752.20449-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305241351365661923@zte.com.cn> References: <202305241351365661923@zte.com.cn> MIME-Version: 1.0 X-Spam-Status: No, score=3.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,FSL_HELO_NON_FQDN_1, HEADER_FROM_DIFFERENT_DOMAINS,HELO_NO_DOMAIN,NO_DNS_FOR_FROM, RCVD_IN_PBL,RDNS_NONE,SPF_SOFTFAIL,SPOOFED_FREEMAIL_NO_RDNS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: *** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766755853833312231?= X-GMAIL-MSGID: =?utf-8?q?1766755853833312231?= From: xu xin As pages_sharing and pages_shared don't include the number of zero pages merged by KSM, we cannot know how many pages are zero pages placed by KSM when enabling use_zero_pages, which leads to KSM not being transparent with all actual merged pages by KSM. In the early days of use_zero_pages, zero-pages was unable to get unshared by the ways like MADV_UNMERGEABLE so it's hard to count how many times one of those zeropages was then unmerged. But now, unsharing KSM-placed zero page accurately has been achieved, so we can easily count both how many times a page full of zeroes was merged with zero-page and how many times one of those pages was then unmerged. and so, it helps to estimate memory demands when each and every shared page could get unshared. So we add ksm_zero_pages under /sys/kernel/mm/ksm/ to show the number of all zero pages placed by KSM. Meanwhile, we update the Documentation. Signed-off-by: xu xin Suggested-by: David Hildenbrand Cc: Claudio Imbrenda Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang Reviewed-by: David Hildenbrand --- Documentation/admin-guide/mm/ksm.rst | 7 +++++++ include/linux/ksm.h | 12 ++++++++++++ mm/khugepaged.c | 2 ++ mm/ksm.c | 12 ++++++++++++ mm/memory.c | 5 ++++- 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index 7626392fe82c..6cc919dbfd55 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -173,6 +173,13 @@ stable_node_chains the number of KSM pages that hit the ``max_page_sharing`` limit stable_node_dups number of duplicated KSM pages +ksm_zero_pages + how many zero pages that are still mapped into processes were mapped by + KSM when deduplicating. + +When ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` + +``ksm_zero_pages`` represents the actual number of pages saved by KSM. +if ``use_zero_pages`` has never been enabled, ``ksm_zero_pages`` is 0. A high ratio of ``pages_sharing`` to ``pages_shared`` indicates good sharing, but a high ratio of ``pages_unshared`` to ``pages_sharing`` diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 4fd5f4a50bac..f2d98c53cfec 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -33,6 +33,14 @@ void __ksm_exit(struct mm_struct *mm); */ #define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte)) +extern unsigned long ksm_zero_pages; + +static inline void ksm_notify_unmap_zero_page(pte_t pte) +{ + if (is_ksm_zero_pte(pte)) + ksm_zero_pages--; +} + static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { int ret; @@ -103,6 +111,10 @@ static inline void ksm_exit(struct mm_struct *mm) #define is_ksm_zero_pte(pte) 0 +static inline void ksm_notify_unmap_zero_page(pte_t pte) +{ +} + #ifdef CONFIG_MEMORY_FAILURE static inline void collect_procs_ksm(struct page *page, struct list_head *to_kill, int force_early) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 6b9d39d65b73..e417a928ef8d 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -711,6 +712,7 @@ static void __collapse_huge_page_copy_succeeded(pte_t *pte, spin_lock(ptl); ptep_clear(vma->vm_mm, address, _pte); spin_unlock(ptl); + ksm_notify_unmap_zero_page(pteval); } } else { src_page = pte_page(pteval); diff --git a/mm/ksm.c b/mm/ksm.c index f31c789406b1..d3ed90159322 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -278,6 +278,9 @@ static unsigned int zero_checksum __read_mostly; /* Whether to merge empty (zeroed) pages with actual zero pages */ static bool ksm_use_zero_pages __read_mostly; +/* The number of zero pages which is placed by KSM */ +unsigned long ksm_zero_pages; + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -1227,6 +1230,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, * the dirty bit in zero page's PTE is set. */ newpte = pte_mkdirty(pte_mkspecial(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot))); + ksm_zero_pages++; /* * We're replacing an anonymous page with a zero page, which is * not anonymous. We need to do proper accounting otherwise we @@ -3354,6 +3358,13 @@ static ssize_t pages_volatile_show(struct kobject *kobj, } KSM_ATTR_RO(pages_volatile); +static ssize_t ksm_zero_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%ld\n", ksm_zero_pages); +} +KSM_ATTR_RO(ksm_zero_pages); + static ssize_t general_profit_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3421,6 +3432,7 @@ static struct attribute *ksm_attrs[] = { &pages_sharing_attr.attr, &pages_unshared_attr.attr, &pages_volatile_attr.attr, + &ksm_zero_pages_attr.attr, &full_scans_attr.attr, #ifdef CONFIG_NUMA &merge_across_nodes_attr.attr, diff --git a/mm/memory.c b/mm/memory.c index 8358f3b853f2..09c31160af4e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1415,8 +1415,10 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, tlb_remove_tlb_entry(tlb, pte, addr); zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); - if (unlikely(!page)) + if (unlikely(!page)) { + ksm_notify_unmap_zero_page(ptent); continue; + } delay_rmap = 0; if (!PageAnon(page)) { @@ -3120,6 +3122,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) inc_mm_counter(mm, MM_ANONPAGES); } } else { + ksm_notify_unmap_zero_page(vmf->orig_pte); inc_mm_counter(mm, MM_ANONPAGES); } flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); From patchwork Wed May 24 05:58:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 98311 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2629623vqo; Tue, 23 May 2023 23:18:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6f7L3nn+BDzQ9CUmdFjvkg7XPurU/iRWHenKT30jCAdNBUgk77VZicW6hXSSP+hgZxXPAW X-Received: by 2002:a17:90a:8597:b0:24d:f159:d28b with SMTP id m23-20020a17090a859700b0024df159d28bmr15716024pjn.47.1684909102984; Tue, 23 May 2023 23:18:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684909102; cv=none; d=google.com; s=arc-20160816; b=NPO4dsmqNg8w0Wa/Qa/yw40U1bxlK+eVTLwRHwur+7JgvAglH+uEM7P4bkfYelSDHn mIplBUpxkBCz47wVDOXGy08kCf1yZsdP4217Qv1IYcdY76W0iZhK2GMRVcvkRtxqVZJL RoFp3FwTrJAkoxhP3YRVP9FrhhDxYklx52p5tHkZAb4njfCDbDonNvRfuRmSH3Kq3+8w 5W92krJhRZ9WQJdT0qoxNquOHYoGXoveGd6rNMfvgj1jqJThM+upJvfh+vuIQC9pOetd AASiuo8/aM5uKylDSonSdDTjTi0MMD6hHAVbDcEdEjIvBuWgjMdidcFJ6uVBFmqvQ4LW nCKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=S/4R4SuMutEbIKn60YR2+yw3qp0qpoqjLzcR9NS/kyw=; b=XuIcaag7Xp5/qZ7igOsW3sLn9b6cVb3zPOWdSVJCuP8AORoZF+V7JqmyxtNH54ufTd /x7ui5TO8dIiumByznSSwgF6dzy3IUnCrLYBUe7EALx44ECNVxJZrj/2JIyvaOcO/hb7 lYjdUQBPqcPPhL6XShba/ECGnC+dk0sfAtNWhIB5VuDZA3vAL+IEiXkTBk287zoF6ERJ Vdr4ZTLgvTekkXghJNequnZ7vqz+O4IzuISebT8J3sj9BuBeUaYx4L9RUtQVdDj4kxbJ AR0zMXY0d8HIng34HzGYD1qCzdP7kyOBt/dduOKU30iVAqT0cwSCSnSREQGgXVWm4O9W 9w6Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r5-20020a17090aa08500b0024bc03cfa95si682041pjp.156.2023.05.23.23.18.08; Tue, 23 May 2023 23:18:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239557AbjEXF6V (ORCPT + 99 others); Wed, 24 May 2023 01:58:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239563AbjEXF6T (ORCPT ); Wed, 24 May 2023 01:58:19 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DEE1132 for ; Tue, 23 May 2023 22:58:14 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id ECE64E1EE6; Wed, 24 May 2023 13:58:02 +0800 (CST) From: Yang Yang To: akpm@linux-foundation.org, david@redhat.com Cc: yang.yang29@zte.com.cn, imbrenda@linux.ibm.com, jiang.xuexin@zte.com.cn, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ran.xiaokai@zte.com.cn, xu.xin.sc@gmail.com, xu.xin16@zte.com.cn Subject: [PATCH v9 3/5] ksm: add ksm zero pages for each process Date: Wed, 24 May 2023 13:58:00 +0800 Message-Id: <20230524055800.20498-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305241351365661923@zte.com.cn> References: <202305241351365661923@zte.com.cn> MIME-Version: 1.0 X-Spam-Status: No, score=3.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,FSL_HELO_NON_FQDN_1, HEADER_FROM_DIFFERENT_DOMAINS,HELO_NO_DOMAIN,NO_DNS_FOR_FROM, RCVD_IN_PBL,RDNS_NONE,SPF_SOFTFAIL,SPOOFED_FREEMAIL_NO_RDNS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: *** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766755247355502764?= X-GMAIL-MSGID: =?utf-8?q?1766755247355502764?= From: xu xin As the number of ksm zero pages is not included in ksm_merging_pages per process when enabling use_zero_pages, it's unclear of how many actual pages are merged by KSM. To let users accurately estimate their memory demands when unsharing KSM zero-pages, it's necessary to show KSM zero- pages per process. In addition, it help users to know the actual KSM profit because KSM-placed zero pages are also benefit from KSM. since unsharing zero pages placed by KSM accurately is achieved, then tracking empty pages merging and unmerging is not a difficult thing any longer. Since we already have /proc//ksm_stat, just add the information of 'ksm_zero_pages' in it. Signed-off-by: xu xin Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Cc: Xiaokai Ran Cc: Yang Yang Reviewed-by: David Hildenbrand --- fs/proc/base.c | 1 + include/linux/ksm.h | 6 ++++-- include/linux/mm_types.h | 9 +++++++-- mm/khugepaged.c | 2 +- mm/ksm.c | 1 + mm/memory.c | 4 ++-- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index 05452c3b9872..e407a34a46e8 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3209,6 +3209,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items); 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_zero_pages %lu\n", mm->ksm_zero_pages); mmput(mm); } diff --git a/include/linux/ksm.h b/include/linux/ksm.h index f2d98c53cfec..0e6f04ac3f2b 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -35,10 +35,12 @@ void __ksm_exit(struct mm_struct *mm); extern unsigned long ksm_zero_pages; -static inline void ksm_notify_unmap_zero_page(pte_t pte) +static inline void ksm_notify_unmap_zero_page(struct mm_struct *mm, pte_t pte) { - if (is_ksm_zero_pte(pte)) + if (is_ksm_zero_pte(pte)) { ksm_zero_pages--; + mm->ksm_zero_pages--; + } } static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 306a3d1a0fa6..14f781509812 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -777,7 +777,7 @@ struct mm_struct { #ifdef CONFIG_KSM /* * Represent how many pages of this process are involved in KSM - * merging. + * merging (not including ksm_zero_pages). */ unsigned long ksm_merging_pages; /* @@ -785,7 +785,12 @@ struct mm_struct { * including merged and not merged. */ unsigned long ksm_rmap_items; -#endif + /* + * Represent how many empty pages are merged with kernel zero + * pages when enabling KSM use_zero_pages. + */ + unsigned long ksm_zero_pages; +#endif /* CONFIG_KSM */ #ifdef CONFIG_LRU_GEN struct { /* this mm_struct is on lru_gen_mm_list */ diff --git a/mm/khugepaged.c b/mm/khugepaged.c index e417a928ef8d..85dc2e922f64 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -712,7 +712,7 @@ static void __collapse_huge_page_copy_succeeded(pte_t *pte, spin_lock(ptl); ptep_clear(vma->vm_mm, address, _pte); spin_unlock(ptl); - ksm_notify_unmap_zero_page(pteval); + ksm_notify_unmap_zero_page(vma->vm_mm, pteval); } } else { src_page = pte_page(pteval); diff --git a/mm/ksm.c b/mm/ksm.c index d3ed90159322..07a6fe7d7c99 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1231,6 +1231,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, */ newpte = pte_mkdirty(pte_mkspecial(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot))); ksm_zero_pages++; + mm->ksm_zero_pages++; /* * We're replacing an anonymous page with a zero page, which is * not anonymous. We need to do proper accounting otherwise we diff --git a/mm/memory.c b/mm/memory.c index 09c31160af4e..26bb291c1c72 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1416,7 +1416,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); if (unlikely(!page)) { - ksm_notify_unmap_zero_page(ptent); + ksm_notify_unmap_zero_page(mm, ptent); continue; } @@ -3122,7 +3122,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) inc_mm_counter(mm, MM_ANONPAGES); } } else { - ksm_notify_unmap_zero_page(vmf->orig_pte); + ksm_notify_unmap_zero_page(mm, vmf->orig_pte); inc_mm_counter(mm, MM_ANONPAGES); } flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); From patchwork Wed May 24 05:58:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 98304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2624372vqo; Tue, 23 May 2023 23:05:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5gpgOzmVK9RkIl76OFkvfb/zRf98XJvJsVIACtNh9Ld774QTNO0BpTjIya4ugcduDGdyZ1 X-Received: by 2002:a17:90a:c294:b0:253:572f:79b1 with SMTP id f20-20020a17090ac29400b00253572f79b1mr17154938pjt.28.1684908334269; Tue, 23 May 2023 23:05:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684908334; cv=none; d=google.com; s=arc-20160816; b=BmmT4V2tVsC/xkDMAG2EiSW/Ln6J6hW5nu40QpEFN9TQUwN8ftuym8tZoHtWTBJtKX uzKVfTuQYlbJrLdlWsYvLizhZ1wI3ANSUwMmuEuhSr+t/diFFLgRLjFEHS46ZEctND0l /fKtc/empCfw9F0sml6IZXzgOiQKFJjkIuiOxCzmjgXDz9qqQ5WzsDwLl4omnGtIc1Nm WWFe4nO5gLk9p5p6n/8mf2YNn9d5G2aSu/1o09ROm4dbzbbWe3/ZprjjRLL8qCbl7URR O9DU5lcC/aoqWMIy8mTLk34u6E284YhUmxpb7SnQt4B9wYoyXQxctcqeZPdlW0v7Qv5Q 2APA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=VcL2Q4JTspmfkQxQd44Bdj2lnGJZupasdfeynoYHwIs=; b=gSWyHTGUoSccD5NKoOdG4Tju6jF1X8gx5JMpTGa3jPJAImr1jlA4w0KRyyetcVjxBl dwKJ8tx2UnhDKLnWwFCsVnVT8JkY2l8AhaWIuNK9Us1G+GFHeeu6TlUCmzURAlqJoUXP 0lC9ULP0eXeASw5X0WcDh2Dft41VH6IgXz8S0wApy31WxZLCnP8tBP3nHHAtat1aEMEi DbiNisX5Q/Ja5oIoxodf+hnTaedkdwxO96u4wJQL0SFYYv3s8yEqQj166zHP+KiSivL7 M5ivEhr7p1+JpJ7GWElTvEChircBG4WqukeuuQHXLI+CeEeqUoJ7fcRqZHeGJ4057Lvz QFIg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oa13-20020a17090b1bcd00b0024df18639f4si766353pjb.72.2023.05.23.23.05.21; Tue, 23 May 2023 23:05:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239570AbjEXF7J (ORCPT + 99 others); Wed, 24 May 2023 01:59:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239521AbjEXF7H (ORCPT ); Wed, 24 May 2023 01:59:07 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13DEF18B for ; Tue, 23 May 2023 22:58:54 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id CE8E2E1EEA; Wed, 24 May 2023 13:58:52 +0800 (CST) From: Yang Yang To: akpm@linux-foundation.org, david@redhat.com Cc: yang.yang29@zte.com.cn, imbrenda@linux.ibm.com, jiang.xuexin@zte.com.cn, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ran.xiaokai@zte.com.cn, xu.xin.sc@gmail.com, xu.xin16@zte.com.cn, shr@devkernel.io Subject: [PATCH v9 4/5] ksm: consider KSM-placed zeropages when calculating KSM profit Date: Wed, 24 May 2023 13:58:51 +0800 Message-Id: <20230524055851.20565-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305241351365661923@zte.com.cn> References: <202305241351365661923@zte.com.cn> MIME-Version: 1.0 X-Spam-Status: No, score=3.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,FSL_HELO_NON_FQDN_1, HEADER_FROM_DIFFERENT_DOMAINS,HELO_NO_DOMAIN,NO_DNS_FOR_FROM, RCVD_IN_PBL,RDNS_NONE,SPF_SOFTFAIL,SPOOFED_FREEMAIL_NO_RDNS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: *** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766754441785541402?= X-GMAIL-MSGID: =?utf-8?q?1766754441785541402?= From: xu xin When use_zero_pages is enabled, the calculation of ksm profit is not correct because ksm zero pages is not counted in. So update the calculation of KSM profit including the documentation. Signed-off-by: xu xin Cc: Xiaokai Ran Cc: Yang Yang Cc: Jiang Xuexin Cc: Claudio Imbrenda Cc: David Hildenbrand Acked-by: David Hildenbrand --- Documentation/admin-guide/mm/ksm.rst | 18 +++++++++++------- mm/ksm.c | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index 6cc919dbfd55..5c5be7bd84b8 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -203,21 +203,25 @@ several times, which are unprofitable memory consumed. 1) How to determine whether KSM save memory or consume memory in system-wide range? Here is a simple approximate calculation for reference:: - general_profit =~ pages_sharing * sizeof(page) - (all_rmap_items) * + general_profit =~ ksm_saved_pages * sizeof(page) - (all_rmap_items) * sizeof(rmap_item); - where all_rmap_items can be easily obtained by summing ``pages_sharing``, - ``pages_shared``, ``pages_unshared`` and ``pages_volatile``. + where ksm_saved_pages equals to the sum of ``pages_sharing`` + + ``ksm_zero_pages`` of the system, and all_rmap_items can be easily + obtained by summing ``pages_sharing``, ``pages_shared``, ``pages_unshared`` + and ``pages_volatile``. 2) The KSM profit inner a single process can be similarly obtained by the following approximate calculation:: - process_profit =~ ksm_merging_pages * sizeof(page) - + process_profit =~ ksm_saved_pages * sizeof(page) - ksm_rmap_items * sizeof(rmap_item). - where ksm_merging_pages is shown under the directory ``/proc//``, - and ksm_rmap_items is shown in ``/proc//ksm_stat``. The process profit - is also shown in ``/proc//ksm_stat`` as ksm_process_profit. + where ksm_saved_pages equals to the sum of ``ksm_merging_pages`` and + ``ksm_zero_pages``, both of which are shown under the directory + ``/proc//ksm_stat``, and ksm_rmap_items is also shown in + ``/proc//ksm_stat``. The process profit is also shown in + ``/proc//ksm_stat`` as ksm_process_profit. From the perspective of application, a high ratio of ``ksm_rmap_items`` to ``ksm_merging_pages`` means a bad madvise-applied policy, so developers or diff --git a/mm/ksm.c b/mm/ksm.c index 07a6fe7d7c99..1c7d0e215c47 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3090,7 +3090,7 @@ static void wait_while_offlining(void) #ifdef CONFIG_PROC_FS long ksm_process_profit(struct mm_struct *mm) { - return mm->ksm_merging_pages * PAGE_SIZE - + return (long)(mm->ksm_merging_pages + mm->ksm_zero_pages) * PAGE_SIZE - mm->ksm_rmap_items * sizeof(struct ksm_rmap_item); } #endif /* CONFIG_PROC_FS */ @@ -3371,7 +3371,7 @@ static ssize_t general_profit_show(struct kobject *kobj, { long general_profit; - general_profit = ksm_pages_sharing * PAGE_SIZE - + general_profit = (ksm_pages_sharing + ksm_zero_pages) * PAGE_SIZE - ksm_rmap_items * sizeof(struct ksm_rmap_item); return sysfs_emit(buf, "%ld\n", general_profit); From patchwork Wed May 24 05:59:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 98307 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2628610vqo; Tue, 23 May 2023 23:15:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4O+JmNt8Mk2hVWqFIJeVE1mWTIEpHydPUiN7XnPky3+YAmoH0furZXbEKeBiolzmrsa4gJ X-Received: by 2002:a05:6a00:1488:b0:64d:2ea4:937a with SMTP id v8-20020a056a00148800b0064d2ea4937amr2402748pfu.7.1684908956926; Tue, 23 May 2023 23:15:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684908956; cv=none; d=google.com; s=arc-20160816; b=Fvi7d3Za7CLGImYfdyi+TFnM97KiI3dFwF6mlhTC5ibJtQD2zj53tbg/oPJgnPXfOJ Zx5ADXLJ+BrHqJOTyz28UQMw6t/sG/j4y8ck5kkHALZrMLAlPgvBk8eRp4aiRrTx19FL DJ/d/iwRyuGwwm8NNbhhbveNFHcW9Oh+7+UIBOh56BeYvZsndqZtQZxvsoeZ5q92TyBu ti0jKXMSV2TLdwAsTmhRMTdgTn+HRFqG3n8NyLYuEouUOj0KumKTfssSZeznEizEZJn6 MjGsJ7F8vN9EmWXBsaYNU/TjArTewVKiojBft4b1+ZRErnwT6ANug9m/SrPaARRJXyhO Y0DQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=kQ3F2IN05Ahw0on18BQUCeYv7ka8KT7Jpd6G6tu5hYw=; b=CiV2YD8/3M2Cu/D99zVwuvjuOFtBh4LMVb3GLFrq3mVByS7t+p5G1+5k2rjQdRovHV Ne++0TBXqeM7BKIqg24EzsSafj2TPnBP1HovlWgq1FSnKPY11wZg9F6L0UUS0LymEoBb 8W85145wFIaQXJaUmuMW/VAANKOZcQ7sgkOZPMEtv+R+VPJPG7v0729c59+BRIlHb4jx GFpjTyUjXLTXr8R3rz6AHuzss6ssrseB5Al3zRsiNdrDRpseEliF4PJibzcs3E21tJYE C/KfJP5A9adY3scmSc7z9wrm4EMTLywOGt64NhejLSLA9uAIYzmSAdPAz5h9zcM5oyoO gAPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l65-20020a622544000000b0064d3a475274si7680808pfl.75.2023.05.23.23.15.42; Tue, 23 May 2023 23:15:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239580AbjEXF7M (ORCPT + 99 others); Wed, 24 May 2023 01:59:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239568AbjEXF7L (ORCPT ); Wed, 24 May 2023 01:59:11 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6D7E19D for ; Tue, 23 May 2023 22:59:08 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id 8967BE1EE8; Wed, 24 May 2023 13:59:07 +0800 (CST) From: Yang Yang To: akpm@linux-foundation.org, david@redhat.com Cc: yang.yang29@zte.com.cn, imbrenda@linux.ibm.com, jiang.xuexin@zte.com.cn, linux-kernel@vger.kernel.org, linux-mm@kvack.org, ran.xiaokai@zte.com.cn, xu.xin.sc@gmail.com, xu.xin16@zte.com.cn Subject: [PATCH v9 5/5] selftest: add a testcase of ksm zero pages Date: Wed, 24 May 2023 13:59:06 +0800 Message-Id: <20230524055906.20614-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305241351365661923@zte.com.cn> References: <202305241351365661923@zte.com.cn> MIME-Version: 1.0 X-Spam-Status: No, score=3.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,FSL_HELO_NON_FQDN_1, HEADER_FROM_DIFFERENT_DOMAINS,HELO_NO_DOMAIN,NO_DNS_FOR_FROM, RCVD_IN_PBL,RDNS_NONE,SPF_SOFTFAIL,SPOOFED_FREEMAIL_NO_RDNS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: *** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766755094157199515?= X-GMAIL-MSGID: =?utf-8?q?1766755094157199515?= From: xu xin Add a function test_unmerge_zero_page() to test the functionality on unsharing and counting ksm-placed zero pages and counting of this patch series. test_unmerge_zero_page() actually contains three subjct test objects: (1) whether the count of ksm zero pages can update correctly after merging; (2) whether the count of ksm zero pages can update correctly after unmerging; (3) whether ksm zero pages are really unmerged. Signed-off-by: xu xin Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang --- tools/testing/selftests/mm/ksm_functional_tests.c | 78 ++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 26853badae70..ea060c683c80 100644 --- a/tools/testing/selftests/mm/ksm_functional_tests.c +++ b/tools/testing/selftests/mm/ksm_functional_tests.c @@ -29,6 +29,8 @@ static int ksm_fd; static int ksm_full_scans_fd; +static int ksm_zero_pages_fd; +static int ksm_use_zero_pages_fd; static int pagemap_fd; static size_t pagesize; @@ -59,6 +61,24 @@ static bool range_maps_duplicates(char *addr, unsigned long size) return false; } +static long get_ksm_zero_pages(void) +{ + char buf[20]; + ssize_t read_size; + unsigned long ksm_zero_pages; + + if (!ksm_zero_pages_fd) + return 0; + + read_size = pread(ksm_zero_pages_fd, buf, sizeof(buf) - 1, 0); + if (read_size < 0) + return -errno; + buf[read_size] = 0; + ksm_zero_pages = strtol(buf, NULL, 10); + + return ksm_zero_pages; +} + static long ksm_get_full_scans(void) { char buf[10]; @@ -159,6 +179,59 @@ static void test_unmerge(void) munmap(map, size); } +static void test_unmerge_zero_pages(void) +{ + const unsigned int size = 2 * MiB; + char *map; + unsigned long pages_expected; + + ksft_print_msg("[RUN] %s\n", __func__); + + if (ksm_zero_pages_fd < 0) { + ksft_test_result_skip("open(\"/sys/kernel/mm/ksm/ksm_zero_pages\") failed\n"); + return; + } + if (ksm_use_zero_pages_fd < 0) { + ksft_test_result_skip("open \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n"); + return; + } + if (write(ksm_use_zero_pages_fd, "1", 1) != 1) { + ksft_test_result_skip("write \"/sys/kernel/mm/ksm/use_zero_pages\" failed\n"); + return; + } + + /* Mmap zero pages */ + map = mmap_and_merge_range(0x00, size, false); + if (map == MAP_FAILED) + return; + + /* Check if ksm_zero_pages can be update correctly after merging */ + pages_expected = size / pagesize; + if (pages_expected != get_ksm_zero_pages()) { + ksft_test_result_fail("'ksm_zero_pages' updated after merging\n"); + goto unmap; + } + + /* try to unmerge half of the region */ + if (madvise(map, size / 2, MADV_UNMERGEABLE)) { + ksft_test_result_fail("MADV_UNMERGEABLE failed\n"); + goto unmap; + } + + /* Check if ksm_zero_pages can be update correctly after unmerging */ + pages_expected /= 2; + if (pages_expected != get_ksm_zero_pages()) { + ksft_test_result_fail("'ksm_zero_pages' updated after unmerging\n"); + goto unmap; + } + + /* Check if ksm zero pages are really unmerged */ + ksft_test_result(!range_maps_duplicates(map, size / 2), + "KSM zero pages were unmerged\n"); +unmap: + munmap(map, size); +} + static void test_unmerge_discarded(void) { const unsigned int size = 2 * MiB; @@ -358,7 +431,7 @@ static void test_prctl_unmerge(void) int main(int argc, char **argv) { - unsigned int tests = 5; + unsigned int tests = 6; int err; #ifdef __NR_userfaultfd @@ -379,8 +452,11 @@ int main(int argc, char **argv) pagemap_fd = open("/proc/self/pagemap", O_RDONLY); if (pagemap_fd < 0) ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n"); + ksm_zero_pages_fd = open("/sys/kernel/mm/ksm/ksm_zero_pages", O_RDONLY); + ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR); test_unmerge(); + test_unmerge_zero_pages(); test_unmerge_discarded(); #ifdef __NR_userfaultfd test_unmerge_uffd_wp();