From patchwork Mon May 22 10:49:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 97253 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1353188vqo; Mon, 22 May 2023 03:52:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5yOdyJsea/1WU/Z6vl405g2n6IJW+HQCOWPnJkujFclnztvUMCBLcLfAGTdCIZVmuJJtDa X-Received: by 2002:a05:6a20:7fa8:b0:101:9344:bf82 with SMTP id d40-20020a056a207fa800b001019344bf82mr11507507pzj.15.1684752741185; Mon, 22 May 2023 03:52:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684752741; cv=none; d=google.com; s=arc-20160816; b=KRZ0EDVSuz7soAoJtaVbIwMqcNatEUWQLrHTEXAWfc152dzwVS8hILOFKE+t6tXoj5 T4lR3ELXUbpb0Udm+XByh5EyGern0mw6rfjyZPprYNPeFVg6ER8fXdimAXVBwOSeopvj ArOLA3kiAE9zV/pSxFEkMcmc9EVJ0IEQL80UrmhVXaaZksBWv1qMIwUcn5KjD5LR6H92 em4kv2VsVyHEJCOUizKz0Tswx9K/tGzZE3zEX4zYAnSo0uSnRMODlO1aHzobnqYgthxI R/Xu9d7PgnagDV5hr68/mAHDqe3d1bSt7ZltJIIkiHdFhwxEXdza4B0xiKXfCyZVAvAg i0QQ== 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=26JJ0jludqg/KlrhhNatsJle7K7stopN854w36nWFVw=; b=gTR3x+kv9xdXiIXQo6mJVaeUbrKh+oEwRlwjHf6+3KHKLCtLvV+nakHE+rYC7ecKa0 xdF7Q1UtbHRAxA58/QpbvcTuj9eufTZEV1xab/Cm3ftK/TO5PjbwA8+9PGCOhmxSTyHF HOkT+vjRjMnPKNcUbACdu5YYNykYws2OQSCk+8/2VoCiup4mqG0IwCL39UrbupR9aOzn Q5NJSjD2VLYZ9mpoJm7QF8lQFJaQzYcqcgKaF3cWeONeXOQEVUDOxWrracWKU/iwHBLx P50aJ0VJp7xtMG0lNxirPP5c021bqf/K9R/e/tWZxZXzD07pQ47geP4NQzUtTrMygS7u 7Agg== 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 j29-20020a637a5d000000b00534869d75d0si427677pgn.767.2023.05.22.03.52.06; Mon, 22 May 2023 03:52:21 -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 S231983AbjEVKtQ (ORCPT + 99 others); Mon, 22 May 2023 06:49:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230188AbjEVKtO (ORCPT ); Mon, 22 May 2023 06:49:14 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD538DC for ; Mon, 22 May 2023 03:49:13 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id 5EA9FE0CF6; Mon, 22 May 2023 18:49:11 +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 v8 1/6] ksm: support unsharing KSM-placed zero pages Date: Mon, 22 May 2023 18:49:08 +0800 Message-Id: <20230522104908.3999-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305221842587200002@zte.com.cn> References: <202305221842587200002@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?1766591290141465149?= X-GMAIL-MSGID: =?utf-8?q?1766591290141465149?= 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. 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 --- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 899a314bc487..7989200cdbb7 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -26,6 +26,9 @@ int ksm_disable(struct mm_struct *mm); int __ksm_enter(struct mm_struct *mm); void __ksm_exit(struct mm_struct *mm); +/* use pte_mkdirty to track a KSM-placed zero page */ +#define set_pte_ksm_zero(pte) pte_mkdirty(pte_mkspecial(pte)) +#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 +98,9 @@ static inline void ksm_exit(struct mm_struct *mm) { } +#define set_pte_ksm_zero(pte) pte_mkspecial(pte) +#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..9962f5962afd 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,7 +1221,7 @@ 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), + newpte = set_pte_ksm_zero(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot)); /* * We're replacing an anonymous page with a zero page, which is From patchwork Mon May 22 10:52:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 97314 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1375092vqo; Mon, 22 May 2023 04:31:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7vkcxFNoGOvEoTVK555k3Ei6RaUNjjkxc0m/qJeAqvWQS8tFIuoi7SXB3UvK/cvIfviU/q X-Received: by 2002:a17:90a:128d:b0:253:971b:dd1e with SMTP id g13-20020a17090a128d00b00253971bdd1emr7902285pja.0.1684755079640; Mon, 22 May 2023 04:31:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684755079; cv=none; d=google.com; s=arc-20160816; b=PDPy92J1Mdn+L4F7NJdQneT/cPri/+ncj1Tp2MLsIELGWOvIMpsQVw35t1eN9XxylR mY/pXtjXK35vn+Oh3X+laTRc2jP0dGEHjZS1yX7q/pi7PV+yesNTLQHvCgceqgrUFvy5 +ocfmBfh6y8a01XjizA5jW+M2Wj8tfiD960e2/Jaq/uH/+uWV7rnSuVdWimPB5JrI6MM pseEE4yP5zZHudLjDlEAn4PU/uB7XaP/IRW/5l/3FJoqNHK2kJU+Bq3VviL0BmED5dF3 EM6KG19AfYSe7NKQe4rdxtusWZO0hPmLzDjG9ByQUEU5dEfuxU35ik6nkFQXBNYM/rLP c21Q== 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=Np5W5XnTkIMJucrHSGIVNpjJ6wauC6JIFYT6llnkbcg=; b=axnoTd2LaT6tHWPDlmh29oIPTaYqgD9jNKYmYNJ89BxIb1H8VDJEE1yiayfKnTSwUw x/8SWXNnjOwSBqT7ZaPoomf0D3xn8FaPIUbkw1MZMDais3Nr4xTwWTLwzBhIGA8SUwj0 hUIC3CvXSF8zMK4UBOCIbgTmNazkBMZSLiApQ8jpaa/w2OLWhnfIIl8LTYdZYwLWLppC vhnSDowN1X4NpGi1fEU+BrjZDiGUZPsgXwxa4P8RxX+6FJ106Tm3hUrRNkZEGWeNTZrT 66VYsiin9G2S9HjkVtnaLwUMbqN25i/mFTP4hY2YIFvUs8zZ5QVNUyOEZbeULbspXIqF GanA== 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 o23-20020a17090ac71700b0024df9227b1asi6689976pjt.167.2023.05.22.04.31.01; Mon, 22 May 2023 04:31:19 -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 S233155AbjEVK7F (ORCPT + 99 others); Mon, 22 May 2023 06:59:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232982AbjEVK6j (ORCPT ); Mon, 22 May 2023 06:58:39 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4CF02698 for ; Mon, 22 May 2023 03:53:57 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id 28CEDE1EE3; Mon, 22 May 2023 18:52:31 +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 v8 2/6] ksm: count all zero pages placed by KSM Date: Mon, 22 May 2023 18:52:29 +0800 Message-Id: <20230522105229.4066-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305221842587200002@zte.com.cn> References: <202305221842587200002@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?1766593741969390472?= X-GMAIL-MSGID: =?utf-8?q?1766593741969390472?= 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. v7->v8: Handle the case when khugepaged replaces a shared zeropage by a THP. Signed-off-by: xu xin Suggested-by: David Hildenbrand Cc: Claudio Imbrenda Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang --- include/linux/ksm.h | 17 +++++++++++++++++ mm/khugepaged.c | 3 +++ mm/ksm.c | 12 ++++++++++++ mm/memory.c | 7 ++++++- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index 7989200cdbb7..1adcae0205e3 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -29,6 +29,16 @@ void __ksm_exit(struct mm_struct *mm); /* use pte_mkdirty to track a KSM-placed zero page */ #define set_pte_ksm_zero(pte) pte_mkdirty(pte_mkspecial(pte)) #define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte)) +extern unsigned long ksm_zero_pages; +static inline void inc_ksm_zero_pages(void) +{ + ksm_zero_pages++; +} + +static inline void dec_ksm_zero_pages(void) +{ + ksm_zero_pages--; +} static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) { @@ -100,6 +110,13 @@ static inline void ksm_exit(struct mm_struct *mm) #define set_pte_ksm_zero(pte) pte_mkspecial(pte) #define is_ksm_zero_pte(pte) 0 +static inline void inc_ksm_zero_pages(void) +{ +} + +static inline void dec_ksm_zero_pages(void) +{ +} #ifdef CONFIG_MEMORY_FAILURE static inline void collect_procs_ksm(struct page *page, diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 6b9d39d65b73..ba0d077b6951 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -711,6 +712,8 @@ static void __collapse_huge_page_copy_succeeded(pte_t *pte, spin_lock(ptl); ptep_clear(vma->vm_mm, address, _pte); spin_unlock(ptl); + if (is_ksm_zero_pte(pteval)) + dec_ksm_zero_pages(); } } else { src_page = pte_page(pteval); diff --git a/mm/ksm.c b/mm/ksm.c index 9962f5962afd..2ca7e8860faa 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; @@ -1223,6 +1226,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, } else { newpte = set_pte_ksm_zero(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot)); + inc_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 @@ -3350,6 +3354,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) { @@ -3417,6 +3428,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..058b416adf24 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1415,8 +1415,11 @@ 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)) { + if (is_ksm_zero_pte(ptent)) + dec_ksm_zero_pages(); continue; + } delay_rmap = 0; if (!PageAnon(page)) { @@ -3120,6 +3123,8 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) inc_mm_counter(mm, MM_ANONPAGES); } } else { + if (is_ksm_zero_pte(vmf->orig_pte)) + dec_ksm_zero_pages(); inc_mm_counter(mm, MM_ANONPAGES); } flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); From patchwork Mon May 22 10:53:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 97326 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1384620vqo; Mon, 22 May 2023 04:49:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5mtpFj1Lc8cG8KL7IBh4GAGYj30FPuxeMkiEKRjTX8XWspbvjBh4pyuVgsURM6WAe4tMbM X-Received: by 2002:a05:6a00:1707:b0:649:93a7:571b with SMTP id h7-20020a056a00170700b0064993a7571bmr15917598pfc.13.1684756174791; Mon, 22 May 2023 04:49:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684756174; cv=none; d=google.com; s=arc-20160816; b=rUKmyN3Rx2XCAARn9iz7trN9zyaM3XmHAOnDwXKyn6ZSPp6KPQR7y98Et+Jn00KqBm MJmGFxASQxRskea8KzxfXWzrimbCGSwvlTmwcZMDpXAGfgi9WwzSA8JAckM1wnj4dfQW MK/bPymmfTtXwJwXlIb9MOugHgJoTncyCwJoDn9q7eG8K3efCpTFY7iXejYsqqiMEvbS NQTujSOJGtZk+RGR4xKLEWMJplR4paMYJYkdcnHViMJxE6hnHuGXq6oQ4pNkTPds6d3x GI+eMqV38jHfnUEo9/KbeD35dtuCEerGJdpl1uUFO5lZlse/YeA7Xnqaq/8ub5QNrLXr 3f1A== 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=5BUahojILp+mcIgmpDxO+NgaP5hskex7e5hEvZaqTUk=; b=jf6GzWCCmcPPjfpmG9FXUkTmjr65/pJkgi/W7XNluHZ8ZeRrSVP+4/sed8NCHFBqQs kqs0bGYaAldfAuKAGpdzuF8Gb69WYJhGvaoBjfz4n8ggCNLeNqBw/FBK6EhA9UP+eU3R Qy6sTW8D8RsJoyJ483dTbVS0bR7fcwhEOguu32+Yl+I5ryubdE9LuR/Q997Olk1YZi/k eKDdYCRXdM+cUSoT8ALvE1FCPiSwaEztDxiM9Sqa+xSsLT466CejJt2+fm5xMg/PgguL VvKjZdbTYezuYFqcMj3s15cCXGXPsVAhmZBRAJypZpgi2oIKZQR74+bGbDWDObTJ0YD5 lWIg== 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 w22-20020a63af16000000b00530b3b98fc5si2196434pge.417.2023.05.22.04.49.20; Mon, 22 May 2023 04:49: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 S233356AbjEVLSU (ORCPT + 99 others); Mon, 22 May 2023 07:18:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232989AbjEVLQx (ORCPT ); Mon, 22 May 2023 07:16:53 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDE4D269F for ; Mon, 22 May 2023 04:11:08 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id 6B89DE1EE9; Mon, 22 May 2023 18:53:06 +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 v8 3/6] ksm: add ksm zero pages for each process Date: Mon, 22 May 2023 18:53:05 +0800 Message-Id: <20230522105305.4126-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305221842587200002@zte.com.cn> References: <202305221842587200002@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?1766594890624704147?= X-GMAIL-MSGID: =?utf-8?q?1766594890624704147?= 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 --- fs/proc/base.c | 1 + include/linux/ksm.h | 10 ++++++---- include/linux/mm_types.h | 9 +++++++-- mm/khugepaged.c | 2 +- mm/ksm.c | 2 +- mm/memory.c | 4 ++-- 6 files changed, 18 insertions(+), 10 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 1adcae0205e3..ca29e95481b0 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -30,14 +30,16 @@ void __ksm_exit(struct mm_struct *mm); #define set_pte_ksm_zero(pte) pte_mkdirty(pte_mkspecial(pte)) #define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte)) extern unsigned long ksm_zero_pages; -static inline void inc_ksm_zero_pages(void) +static inline void inc_ksm_zero_pages(struct mm_struct *mm) { ksm_zero_pages++; + mm->ksm_zero_pages++; } -static inline void dec_ksm_zero_pages(void) +static inline void dec_ksm_zero_pages(struct mm_struct *mm) { ksm_zero_pages--; + mm->ksm_zero_pages--; } static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm) @@ -110,11 +112,11 @@ static inline void ksm_exit(struct mm_struct *mm) #define set_pte_ksm_zero(pte) pte_mkspecial(pte) #define is_ksm_zero_pte(pte) 0 -static inline void inc_ksm_zero_pages(void) +static inline void inc_ksm_zero_pages(struct mm_struct *mm) { } -static inline void dec_ksm_zero_pages(void) +static inline void dec_ksm_zero_pages(struct mm_struct *mm) { } 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 ba0d077b6951..5cd6ac70261e 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -713,7 +713,7 @@ static void __collapse_huge_page_copy_succeeded(pte_t *pte, ptep_clear(vma->vm_mm, address, _pte); spin_unlock(ptl); if (is_ksm_zero_pte(pteval)) - dec_ksm_zero_pages(); + dec_ksm_zero_pages(vma->vm_mm); } } else { src_page = pte_page(pteval); diff --git a/mm/ksm.c b/mm/ksm.c index 2ca7e8860faa..4e510f5c5938 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1226,7 +1226,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, } else { newpte = set_pte_ksm_zero(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot)); - inc_ksm_zero_pages(); + inc_ksm_zero_pages(mm); /* * 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 058b416adf24..2603dad833d0 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1417,7 +1417,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, ptent); if (unlikely(!page)) { if (is_ksm_zero_pte(ptent)) - dec_ksm_zero_pages(); + dec_ksm_zero_pages(mm); continue; } @@ -3124,7 +3124,7 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) } } else { if (is_ksm_zero_pte(vmf->orig_pte)) - dec_ksm_zero_pages(); + dec_ksm_zero_pages(mm); inc_mm_counter(mm, MM_ANONPAGES); } flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); From patchwork Mon May 22 10:53:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 97310 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1373768vqo; Mon, 22 May 2023 04:29:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7hO4GD3UUloEXc87VYliAUMkBktGqE3Zreg6iXvWIfKhkFWFBXpr+li0jrpHqQ9cFDNxYG X-Received: by 2002:a17:90b:2386:b0:255:338d:4652 with SMTP id mr6-20020a17090b238600b00255338d4652mr6170080pjb.33.1684754945397; Mon, 22 May 2023 04:29:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684754945; cv=none; d=google.com; s=arc-20160816; b=kWZzRLGhYQECB8HTcFP/HPCcq10mDs1dEmCdZQG6TeW9uEav6jqwblc8pNHR9g+FbN dvQDYWtxhjyWVLQfHGKsM+VeVqoyihZjMY9A2UDT3dR37F51Xr50rEZuhllnmumrTgf8 zQw2zUv1I6X4pL89f7QEr0/L9nB3dyjPSkO4pg5gEgCk7tYBdvpcsVc/EBbxoDNL6wxm q2M7Q3Pr0KkGELbmYr3foA96mQzclNVB+RzRIiaTGhPdZXvuyDfutHChb8Lxt355xyIH sGnabs63UMw7srWwUybpj24j6qRpmN7Grfmi+2lzMexYHsGVXyMlEhzd+d3gBGo9jwT6 ECMA== 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=/K1nT1hMAPCy1sS9ZSi+ba8NlO1a9AM53K+NxXVc4Aw=; b=hjEHNegYrF47UjVj92C0Dj5Ss/TRvkLQvNAXaPr/1USTixzw/eEKEH3AaCI70hnk8U mhX+OclulP3O9GfYSVmdgTZ2Mmc/jm2Fb/CIii3dGuMktveWBZ8uzp1VNZD9VL2leMU5 Tvs0uHw4lJ3AUGj28KA9D/TCuw3Kvp0HdFx3sJkJDH6LAFhfjdl2D5w7rxIBds7bEvqt GtYvJsiIYQH31CcnuTrAPEvWMMl6fnzVFRDxudPXSfGTxTjAocj2Bf6eepIqGTofHinO TGDtZF8OLcsW9AA81MZhJ/9jJz+8q4L4s6YVJM1W9JYJVC8yppB6SNEb7o344Yx7NkO4 w9Ug== 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 o23-20020a17090ac71700b0024df9227b1asi6689976pjt.167.2023.05.22.04.28.53; Mon, 22 May 2023 04:29:05 -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 S233363AbjEVLSX (ORCPT + 99 others); Mon, 22 May 2023 07:18:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231883AbjEVLQe (ORCPT ); Mon, 22 May 2023 07:16:34 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9CB9269E for ; Mon, 22 May 2023 04:11:08 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id 664B9E1EF3; Mon, 22 May 2023 18:53:36 +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 v8 4/6] ksm: add documentation for ksm zero pages Date: Mon, 22 May 2023 18:53:35 +0800 Message-Id: <20230522105335.4176-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305221842587200002@zte.com.cn> References: <202305221842587200002@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?1766593601530456007?= X-GMAIL-MSGID: =?utf-8?q?1766593601530456007?= From: xu xin Add the description of ksm_zero_pages. When use_zero_pages is enabled, pages_sharing cannot represent how much memory saved actually by KSM, but the sum of ksm_zero_pages + pages_sharing does. Signed-off-by: xu xin Cc: Xiaokai Ran Cc: Yang Yang Cc: Jiang Xuexin Cc: Claudio Imbrenda Cc: David Hildenbrand --- Documentation/admin-guide/mm/ksm.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index 7626392fe82c..019dc40a0d3c 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -173,6 +173,14 @@ 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 empty pages are sharing the kernel zero page(s) instead + of other user pages as it would happen normally. Only meaningful + when ``use_zero_pages`` is/was enabled. + +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`` From patchwork Mon May 22 10:54:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 97297 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1366625vqo; Mon, 22 May 2023 04:15:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6hrNgFalFWvVLV2zVEhmm444q6csFjvCdyJ4xi3atEU0bSZ/S9dbAfvFMaGdHranQboLjq X-Received: by 2002:a05:6a20:9f92:b0:106:c9b7:c93d with SMTP id mm18-20020a056a209f9200b00106c9b7c93dmr9650696pzb.19.1684754137917; Mon, 22 May 2023 04:15:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684754137; cv=none; d=google.com; s=arc-20160816; b=Wkid6LLOtC40+dVpWwHwkaniBZQysmP8/5KzhUkC4MqfTHjwcEKRRJbwN+7JnEJIXm XfcBKcaSKHwXgNJmArLwyo9CR8VqTb2mUTQGXzhjwb26OW/JFH1TOINEN2wl30+f5DNw gEmvUjkuc5p8qhTPZ9b3zz3Fk+9WoNqmG7fonn4iRr5+Z8hRmfM9+Nugrd+6MsToBYKX mfscx8kYa17gzoYf0IkG4o13lVW6iqwm7Uh9rhcmPyfUorulu4zoFhVIWO7B3TusERan V5dHUohTzrUjSE17jTHCfs5qq95MCsWTApN90BkUCxd1H3dkSJjunEE6pIHbSkbbePD9 QsWw== 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=E5v3OA97PzwcyWc2mCPAWgEmQ1PwlOzYoY5ZvYxibKk=; b=oRrQqWVCczo9yrRgRvgiiozgaj3fpjtWEerWGwzFNrz2PH4yU+6g7vSe5omkP/fiuT H54zcRewPoLGir1QIOhFMEtD1E4wOZ+CGoRYXbFw5l1uH576d2tTsZRF6mh3oU+ldvq+ Cb/1NqDhGO9f00Wpv0CdVQ4gAbO6uuVgwaCFLrzfxfabGchyb80mMKBhvTwVVgVXCI/7 ruKnzxHzHDhlhX6sZ39O+IMNxSPd6X37GXob1mTR8KSkkvEAO3p1jOBF1CmDr8dqFqz2 0PKQyvx7xiKNnEGyOw/TdDc2zMgMAvjplY/heaKQ9eXiDgpzXzPqbAV4ktlZxR87ciGb lhoQ== 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 ep5-20020a17090ae64500b002534791ea59si425373pjb.28.2023.05.22.04.15.25; Mon, 22 May 2023 04:15:37 -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 S231232AbjEVLLq (ORCPT + 99 others); Mon, 22 May 2023 07:11:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231429AbjEVLLO (ORCPT ); Mon, 22 May 2023 07:11:14 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 882A4358C for ; Mon, 22 May 2023 04:06:15 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id ECF0DE1EF6; Mon, 22 May 2023 18:54:03 +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 v8 5/6] ksm: update the calculation of KSM profit Date: Mon, 22 May 2023 18:54:02 +0800 Message-Id: <20230522105402.4225-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305221842587200002@zte.com.cn> References: <202305221842587200002@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?1766592754968886300?= X-GMAIL-MSGID: =?utf-8?q?1766592754968886300?= 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 --- Documentation/admin-guide/mm/ksm.rst | 18 +++++++++++------- mm/ksm.c | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index 019dc40a0d3c..dde7c152f0ae 100644 --- a/Documentation/admin-guide/mm/ksm.rst +++ b/Documentation/admin-guide/mm/ksm.rst @@ -204,21 +204,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 alos 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 4e510f5c5938..d23a240c2519 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -3085,7 +3085,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 */ From patchwork Mon May 22 10:54:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 97317 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp1375846vqo; Mon, 22 May 2023 04:32:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6HJXpo9rFvA7HbF2LWlYzPybx4JUdfO39PiOuJO2+z2iSil9/9k5A0oOpXNirxI4yhIdtG X-Received: by 2002:a17:902:db03:b0:1ae:6135:a050 with SMTP id m3-20020a170902db0300b001ae6135a050mr13889056plx.19.1684755150056; Mon, 22 May 2023 04:32:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684755150; cv=none; d=google.com; s=arc-20160816; b=QXnHmxHmzZGIirEkj82JwN9TQydyFSm2Xr2cCRrhRidbDybgxdueXUr56yaVjNvM1l sDEtr9Yu+D5x0O5by7pifblm4I0WElQLa9PRZPJXQz/xexM4My8mrj3BQ7ahtYEmIpHe Vzz/tNzRpsKsaz8MpU0Q3Qe8Kz8I2p6VdUWaOfEax2PvaR6dLdUsfKYZ1xpi6hgdsBgL b8KZCKYhCKcJC9Nmiibwt6UR1tbQafNymv5Si4H0A18hUdiF9hXWjz5zdsYV2XOOfqDa BNIYcwJ9DdHB+QjG+NWb2Ro8Xv3N/s+5Qi9wFx7g6cHYRlur61d6sxFpzMYbSAiY6pkU w3nw== 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=HO7hXsNwRL0hi9CylIyMGCj0jCuwz8Wd8Mosybdcu8Y=; b=Yvua7vFMbxk7eaq+5ou6p5dciv+0PRhZyA4gM9Ir+jiJ1LHfWH+HwQhC9arF6tbZCN c57h0jw2JdIPFU89blgKEK36X/YPp6TGznsRo6LnrJp/jxQ19EuF2f9moIHtLYK8MItU pV3vVmMi7mRB7aBv9Z5FTkTdIrYDgHaHLkMukegPibRel16zkoMiCkSIutZ9XzgXWuiK f/ze2skT9e8fWTouT97z1VI4Wh6Aa09kk2nkUipo2V+GjCYW4rWMvWrBqmXzjdg5RaAn H8JmFwZe2CGaJrDul2Jl1dbzTIZk6ZXX5jOkRSkIa1TDunGSdzp5yYor0/nZPdpy5eRm GO+Q== 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 ju11-20020a170903428b00b001a50dcd10c2si1286307plb.247.2023.05.22.04.32.17; Mon, 22 May 2023 04:32:30 -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 S233137AbjEVLSS (ORCPT + 99 others); Mon, 22 May 2023 07:18:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233255AbjEVLQb (ORCPT ); Mon, 22 May 2023 07:16:31 -0400 Received: from ubuntu20 (unknown [193.203.214.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95C482690 for ; Mon, 22 May 2023 04:11:07 -0700 (PDT) Received: by ubuntu20 (Postfix, from userid 1003) id 23C14E1ED8; Mon, 22 May 2023 18:54:35 +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 v8 6/6] selftest: add a testcase of ksm zero pages Date: Mon, 22 May 2023 18:54:33 +0800 Message-Id: <20230522105433.4277-1-yang.yang29@zte.com.cn> X-Mailer: git-send-email 2.25.1 In-Reply-To: <202305221842587200002@zte.com.cn> References: <202305221842587200002@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?1766593816088244475?= X-GMAIL-MSGID: =?utf-8?q?1766593816088244475?= 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 | 75 +++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tools/testing/selftests/mm/ksm_functional_tests.c b/tools/testing/selftests/mm/ksm_functional_tests.c index 26853badae70..9b7fb94ed64f 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,21 @@ 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; + + 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 +176,61 @@ static void test_unmerge(void) munmap(map, size); } +static inline unsigned long expected_ksm_pages(unsigned long mergeable_size) +{ + return mergeable_size / pagesize; +} + +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__); + + /* Confirm the interfaces*/ + 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 = expected_ksm_pages(size); + ksft_test_result(pages_expected == get_ksm_zero_pages(), + "The count zero_page_sharing was updated after merging\n"); + + /* 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 = expected_ksm_pages(size / 2); + ksft_test_result(pages_expected == get_ksm_zero_pages(), + "The count zero_page_sharing was updated after unmerging\n"); + + /* 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; @@ -379,8 +451,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();