From patchwork Fri Dec 30 01:12:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13083996 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 B114DC3DA79 for ; Fri, 30 Dec 2022 01:12:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 474D18E0003; Thu, 29 Dec 2022 20:12:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 424D28E0001; Thu, 29 Dec 2022 20:12:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3142C8E0003; Thu, 29 Dec 2022 20:12:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1ED168E0001 for ; Thu, 29 Dec 2022 20:12:55 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E3881801B3 for ; Fri, 30 Dec 2022 01:12:54 +0000 (UTC) X-FDA: 80297198268.06.DABEDE5 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by imf22.hostedemail.com (Postfix) with ESMTP id A90BFC000D for ; Fri, 30 Dec 2022 01:12:52 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf22.hostedemail.com: domain of yang.yang29@zte.com.cn designates 63.216.63.40 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672362773; 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=Fn8CaapX2uBzNcHLvV2QH2LQpWpDs2Djiqkkl35bf/w=; b=c1HZ4eoUYma4c3MxJmpGxGeINDUCzKftAh6nqoinMzgrn5MXFUVyPsA2WWpprL2zxWamh/ P8jpWRwnOL9wEdnqTNSgcTzJ8vN3kgmsa4X6elHj7+VINcm/VysVLaV/+lnuBa4bXpSIVV fcS/yhRTeyUIzK6TrNMuSc8hxU2VFSI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf22.hostedemail.com: domain of yang.yang29@zte.com.cn designates 63.216.63.40 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672362773; a=rsa-sha256; cv=none; b=FY97IZftamYrFIRgYw0IU9gyiOSEZiW42CqIA89V+ZPl9oH8WeRsW1pTz4xIx2V+c4co0k iUmadFlsrajWIgOQPL7Uf8xeDl+zwP6QE9IU5j+0fx3hUzindRciJmX/BxQUUjHaVt73Mh JQj/P7T8DZNb7yfy1+QFQac8qW+TlLo= Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4NjnLV0XKMz8R039; Fri, 30 Dec 2022 09:12:50 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl2.zte.com.cn with SMTP id 2BU1CiXg057579; Fri, 30 Dec 2022 09:12:44 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:12:44 +0800 (CST) Date: Fri, 30 Dec 2022 09:12:44 +0800 (CST) X-Zmail-TransId: 2b0363ae3b0c59fb2628 X-Mailer: Zmail v1.0 Message-ID: <202212300912449061763@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_1/6=5D_ksm=3A_abstract_the_function_try=5Fto?= =?utf-8?q?=5Fget=5Fold=5Frmap=5Fitem?= X-MAIL: mse-fl2.zte.com.cn 2BU1CiXg057579 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63AE3B12.000 by FangMail milter! X-FangMail-Envelope: 1672362770/4NjnLV0XKMz8R039/63AE3B12.000/10.5.228.133/[10.5.228.133]/mse-fl2.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3B12.000/4NjnLV0XKMz8R039 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: A90BFC000D X-Stat-Signature: rtptji4sxsawbafxcfzj8tfob836kpgw X-Rspam-User: X-HE-Tag: 1672362772-617569 X-HE-Meta: U2FsdGVkX1969PBi1Ihm9lw35WXxcKk+ia1n1hMHwUIZm+9iZaMW93tIN+BDnKsPorFwKCkykPHJ/HUpP60mwyyYkf8mqSiIkauSosvEh+V2gc5eA20Sf8IUJNpp/Lwx8TxDOxxkIKWgELcj8AFmcYL71er6u83nZkeNq4MtppiPS6v7HRXDYL0wMUaIxiOI9v/s5nW/z8LK9jvfs/hngpkbo8h7iX8VE7ZGnNuTw5Z5wsu6o6sxj5P1LTudtINywiPiDIl/q5FJeVzskbfm0PW+bJuvfmC2IvAHM7G9aTTwfwByach3v0rgr/WZ40kzjOTdKlN2eSHgYcFHWr779CzE7j6G7Ptrkzwo5FGC89Fk5qRZgk87GA+1wdlOg3ugJjV3dQwp+q2bVlFnd+KkHS2OS4SIXJ3WMv6Ov795vsmT5WZKjX3MBcTXAbitnfF38tlKZNbCRje0djeTQlmZP+MOBIs9i0PN+lGKeWrgEzZmCTgLcxAg+hdkq+oP+lSwtEyURK4FfqLXVlw5SgKMbLstzfolaVto/iZzBi1j2sSZYcPl2BqXs6zP2LF5aluDWljLulX3F0X6LQqw6ux1XngNqj8kdhADFajkRPdJxGvCt2rMLEXF9+JRUwG47tCE2EWp9eA80eS6DSaYWQe/tisEjEDD2LB9JWWDLv7a9yUFbF3H5XvVJt1EIDCl7MluCdXSo6k/4mjCfySU6TZNhb1NDiuuTyEgNzBZbLOvvXEwQSxuy7X/OrQVwPwkJzQ+GBItgkxgwTkZqSBFavHWo1tyRrodL5pZ8BY6aXsZ0hlj5cmxlMXYHY3uxQ3ApytgVV8d2EJUFzOEktHR7yKkye9F1Jm38LOpSN0mbdzIrgm3fSP3ZpiuazoxhPj3NRhNZr8pY5ld7to82AZYRF2BG8r3pLinyquHKCrqlIuLAGLLvK+Z9+Gz5bDCmj+JS+2m/SCXamo9huKUif11gYV f4Q== 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: From: xu xin A new function try_to_get_old_rmap_item is abstracted from get_next_rmap_item. This function will be reused by the subsequent patches about counting ksm_zero_pages. The patch improves the readability and reusability of KSM code. Signed-off-by: xu xin Cc: David Hildenbrand Cc: Claudio Imbrenda Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang --- mm/ksm.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 83e2f74ae7da..5b0a7343ff4a 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2214,23 +2214,36 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite } } -static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, - struct ksm_rmap_item **rmap_list, - unsigned long addr) +static struct ksm_rmap_item *try_to_get_old_rmap_item(unsigned long addr, + struct ksm_rmap_item **rmap_list) { - struct ksm_rmap_item *rmap_item; - while (*rmap_list) { - rmap_item = *rmap_list; + struct ksm_rmap_item *rmap_item = *rmap_list; if ((rmap_item->address & PAGE_MASK) == addr) return rmap_item; if (rmap_item->address > addr) break; *rmap_list = rmap_item->rmap_list; + /* Running here indicates it's vma has been UNMERGEABLE */ remove_rmap_item_from_tree(rmap_item); free_rmap_item(rmap_item); } + return NULL; +} + +static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, + struct ksm_rmap_item **rmap_list, + unsigned long addr) +{ + struct ksm_rmap_item *rmap_item; + + /* lookup if we have a old rmap_item matching the addr*/ + rmap_item = try_to_get_old_rmap_item(addr, rmap_list); + if (rmap_item) + return rmap_item; + + /* Need to allocate a new rmap_item */ rmap_item = alloc_rmap_item(); if (rmap_item) { /* It has already been zeroed */ From patchwork Fri Dec 30 01:13:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13083997 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 15851C3DA7A for ; Fri, 30 Dec 2022 01:14:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB5D28E0003; Thu, 29 Dec 2022 20:14:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A65938E0001; Thu, 29 Dec 2022 20:14:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 955F08E0003; Thu, 29 Dec 2022 20:14:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 87AD68E0001 for ; Thu, 29 Dec 2022 20:14:07 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5A784C0601 for ; Fri, 30 Dec 2022 01:14:07 +0000 (UTC) X-FDA: 80297201334.04.4B6B7C5 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf22.hostedemail.com (Postfix) with ESMTP id A91EDC0002 for ; Fri, 30 Dec 2022 01:14:04 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf22.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672362845; 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=40t31l01hiP9u92oShLHjW2svT5tkL5iGUmHA1NCqTU=; b=DbAFHjmDADadh2Es11PpwRt68KYZsPwHoSWf+ntsBTFMuWlFRtsbgQiFVSkCN10OH1uKEw ZCHQd/UUQnrxJubG3PhPiPFABgSNm7lf/uNpCpXXRWzfL7paMU5QWik0A3XMt+N0HeGi0T 0446xPeTYRtkzBLrx8LIIR+sXI0yJKo= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf22.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672362845; a=rsa-sha256; cv=none; b=pl9Pwk8EB/h4xi5ZwR7s6Ybz2JpxmPrmT1v97FpQZWa0C78KE1WrcBLOA8qFluRYmmkar5 +FxfB1YxLUhSZM/FQ4nJxY3ujusBJVYT3hlSonoBvigU4RKg9SmUkZRRn4I3CnF9gNDuny kykmFZY9K2376/6b5aU6AlfoW6HMlKo= Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4NjnMs3QrRz501Qf; Fri, 30 Dec 2022 09:14:01 +0800 (CST) Received: from szxlzmapp07.zte.com.cn ([10.5.230.251]) by mse-fl2.zte.com.cn with SMTP id 2BU1DuKS058130; Fri, 30 Dec 2022 09:13:56 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:13:57 +0800 (CST) Date: Fri, 30 Dec 2022 09:13:57 +0800 (CST) X-Zmail-TransId: 2b0363ae3b55650b66d5 X-Mailer: Zmail v1.0 Message-ID: <202212300913573751808@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_2/6=5D_ksm=3A_support_unsharing_zero_pages_pl?= =?utf-8?q?aced_by_KSM?= X-MAIL: mse-fl2.zte.com.cn 2BU1DuKS058130 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63AE3B59.000 by FangMail milter! X-FangMail-Envelope: 1672362841/4NjnMs3QrRz501Qf/63AE3B59.000/10.5.228.133/[10.5.228.133]/mse-fl2.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3B59.000/4NjnMs3QrRz501Qf X-Rspamd-Queue-Id: A91EDC0002 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: 8nsd168ttt6setb3qmj4uemo854quhoo X-HE-Tag: 1672362844-462952 X-HE-Meta: U2FsdGVkX1942XiPTP+47KwVKeZJ3CIvzMnuTmqztBKFkeDg+VlHG0ty/Kw+si6RrbQ5SYlWWHk58cDnNWoHDonoeho1sgNAJmKcVwFA/wdxZg+Vs/fuRNGKqRcF9EoOvru6jullj0BElzI7g1Y6aIIf4MXLcb4bQ7Qp/wEEq3CKdJf7E5P4kmUDqdfU/C2IBS1LuL9np5XTRK6b+60DPUXd0//D28xoj/2UMFctJoS6+gD0ZXTejQK+bwnO1uD8Popo98EzM9DCoi+PBoR7QvebrFWd5dEjeRgPYGkEyi+BZEgcMBbxMwGDehQ12F/p2HsA9gn/ib3qwsxKHzm/fZUC5w8026S6/rFKr2tx4F6ZWkqbv5yac5rOlJ8AOUoK/dg2THoIc6qY1h3eaxmj4CTgVEYOGXcoClUGs/QfTGEQsmTUnrx91rVLSq4hCK7ENsL3I7EBQJgje5ji30fjn7uIRXTH2iFJMBQ2v99q+kGRYiCvwGnRK9VAgna48UPfOFNYBEHZd4x1a8RCgAoqtvze0lA6Ia/DrgX8mYeXLiEVyV0YfALNYiDNXpydbrZ3/5hbejQ8Yawo02HsEC8VPAaL+mRX0VcIvI8kehZEpYLXeSPFff51cfkDdQjezL0zCGAkKpW+GpFjjbU7bfeweyYgmACJMMRYAEJ/mKODZCO0nWwVnVq+HeqtF+5PGcLtk/AemY2EPBR58KSnjFBeBDkfY2TSr4kj22KEnPeg4CjLAkyIQ6RyobMGPXKW88vLIy9nKGjqUBxoMoxUNBzS/WsH9wiYidbSGbsJGENYJxZaN1tc2e2akXhg06Xs0m8WEfI6iOFcaRX2Si9eQEgKyvSqh9MmhrdXet1JAUuOTYNeqP0G+aOwmZuivoHPQZ6c2UK85spdK7fUglDZaeT6x/6YmxoYIyB4GrWvSdcrw1c= 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: From: xu xin use_zero_pages may be very useful, not just because of cache colouring as described in doc, but also because use_zero_pages can accelerate merging empty pages when there are plenty of empty pages (full of zeros) as the time of page-by-page comparisons (unstable_tree_search_insert) is saved. But when enabling use_zero_pages, 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 introduces a dedicated flag ZERO_PAGE_FLAG to mark the rmap_items of those shared zero_pages. and guarantee that these rmap_items will be not freed during the time of zero_pages not being writing, so we can only unshare the *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 Reported-by: David Hildenbrand Cc: Claudio Imbrenda Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang --- mm/ksm.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 111 insertions(+), 30 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 5b0a7343ff4a..652c088f9786 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -214,6 +214,7 @@ struct ksm_rmap_item { #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */ #define UNSTABLE_FLAG 0x100 /* is a node of the unstable tree */ #define STABLE_FLAG 0x200 /* is listed from the stable tree */ +#define ZERO_PAGE_FLAG 0x400 /* is zero page placed by KSM */ /* The stable and unstable tree heads */ static struct rb_root one_stable_tree[1] = { RB_ROOT }; @@ -420,6 +421,11 @@ static inline bool ksm_test_exit(struct mm_struct *mm) return atomic_read(&mm->mm_users) == 0; } +enum break_ksm_pmd_entry_return_flag { + HAVE_KSM_PAGE = 1, + HAVE_ZERO_PAGE +}; + static int break_ksm_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long next, struct mm_walk *walk) { @@ -427,6 +433,7 @@ static int break_ksm_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long nex spinlock_t *ptl; pte_t *pte; int ret; + bool is_zero_page = false; if (pmd_leaf(*pmd) || !pmd_present(*pmd)) return 0; @@ -434,6 +441,8 @@ static int break_ksm_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long nex pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl); if (pte_present(*pte)) { page = vm_normal_page(walk->vma, addr, *pte); + if (!page) + is_zero_page = is_zero_pfn(pte_pfn(*pte)); } else if (!pte_none(*pte)) { swp_entry_t entry = pte_to_swp_entry(*pte); @@ -444,7 +453,14 @@ 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); + + if (page && PageKsm(page)) + ret = HAVE_KSM_PAGE; + else if (is_zero_page) + ret = HAVE_ZERO_PAGE; + else + ret = 0; + pte_unmap_unlock(pte, ptl); return ret; } @@ -466,19 +482,22 @@ static const struct mm_walk_ops break_ksm_ops = { * of the process that owns 'vma'. We also do not want to enforce * protection keys here anyway. */ -static int break_ksm(struct vm_area_struct *vma, unsigned long addr) +static int break_ksm(struct vm_area_struct *vma, unsigned long addr, + bool unshare_zero_page) { vm_fault_t ret = 0; do { - int ksm_page; + int walk_result; cond_resched(); - ksm_page = walk_page_range_vma(vma, addr, addr + 1, + walk_result = walk_page_range_vma(vma, addr, addr + 1, &break_ksm_ops, NULL); - if (WARN_ON_ONCE(ksm_page < 0)) - return ksm_page; - if (!ksm_page) + if (WARN_ON_ONCE(walk_result < 0)) + return walk_result; + if (!walk_result) + return 0; + if (walk_result == HAVE_ZERO_PAGE && !unshare_zero_page) return 0; ret = handle_mm_fault(vma, addr, FAULT_FLAG_UNSHARE | FAULT_FLAG_REMOTE, @@ -539,7 +558,7 @@ static void break_cow(struct ksm_rmap_item *rmap_item) mmap_read_lock(mm); vma = find_mergeable_vma(mm, addr); if (vma) - break_ksm(vma, addr); + break_ksm(vma, addr, false); mmap_read_unlock(mm); } @@ -764,6 +783,30 @@ static struct page *get_ksm_page(struct ksm_stable_node *stable_node, return NULL; } +/* + * Cleaning the rmap_item's ZERO_PAGE_FLAG + * This function will be called when unshare or writing on zero pages. + */ +static inline void clean_rmap_item_zero_flag(struct ksm_rmap_item *rmap_item) +{ + if (rmap_item->address & ZERO_PAGE_FLAG) + rmap_item->address &= PAGE_MASK; +} + +/* Only called when rmap_item is going to be freed */ +static inline void unshare_zero_pages(struct ksm_rmap_item *rmap_item) +{ + struct vm_area_struct *vma; + + if (rmap_item->address & ZERO_PAGE_FLAG) { + vma = vma_lookup(rmap_item->mm, rmap_item->address); + if (vma && !ksm_test_exit(rmap_item->mm)) + break_ksm(vma, rmap_item->address, true); + } + /* Put at last. */ + clean_rmap_item_zero_flag(rmap_item); +} + /* * Removing rmap_item from stable or unstable tree. * This function will clean the information from the stable/unstable tree. @@ -824,6 +867,7 @@ static void remove_trailing_rmap_items(struct ksm_rmap_item **rmap_list) struct ksm_rmap_item *rmap_item = *rmap_list; *rmap_list = rmap_item->rmap_list; remove_rmap_item_from_tree(rmap_item); + unshare_zero_pages(rmap_item); free_rmap_item(rmap_item); } } @@ -853,7 +897,7 @@ static int unmerge_ksm_pages(struct vm_area_struct *vma, if (signal_pending(current)) err = -ERESTARTSYS; else - err = break_ksm(vma, addr); + err = break_ksm(vma, addr, false); } return err; } @@ -2044,6 +2088,39 @@ static void stable_tree_append(struct ksm_rmap_item *rmap_item, rmap_item->mm->ksm_merging_pages++; } +static int try_to_merge_with_kernel_zero_page(struct ksm_rmap_item *rmap_item, + struct page *page) +{ + struct mm_struct *mm = rmap_item->mm; + int err = 0; + + /* + * It should not take ZERO_PAGE_FLAG because on one hand, + * get_next_rmap_item don't return zero pages' rmap_item. + * On the other hand, even if zero page was writen as + * anonymous page, rmap_item has been cleaned after + * stable_tree_search + */ + if (!WARN_ON_ONCE(rmap_item->address & ZERO_PAGE_FLAG)) { + struct vm_area_struct *vma; + + mmap_read_lock(mm); + vma = find_mergeable_vma(mm, rmap_item->address); + if (vma) { + err = try_to_merge_one_page(vma, page, + ZERO_PAGE(rmap_item->address)); + if (!err) + rmap_item->address |= ZERO_PAGE_FLAG; + } else { + /* If the vma is out of date, we do not need to continue. */ + err = 0; + } + mmap_read_unlock(mm); + } + + return err; +} + /* * cmp_and_merge_page - first see if page can be merged into the stable tree; * if not, compare checksum to previous and if it's the same, see if page can @@ -2055,7 +2132,6 @@ static void stable_tree_append(struct ksm_rmap_item *rmap_item, */ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_item) { - struct mm_struct *mm = rmap_item->mm; struct ksm_rmap_item *tree_rmap_item; struct page *tree_page = NULL; struct ksm_stable_node *stable_node; @@ -2092,6 +2168,7 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite } remove_rmap_item_from_tree(rmap_item); + clean_rmap_item_zero_flag(rmap_item); if (kpage) { if (PTR_ERR(kpage) == -EBUSY) @@ -2128,29 +2205,16 @@ static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rmap_ite * Same checksum as an empty page. We attempt to merge it with the * appropriate zero page if the user enabled this via sysfs. */ - if (ksm_use_zero_pages && (checksum == zero_checksum)) { - struct vm_area_struct *vma; - - mmap_read_lock(mm); - vma = find_mergeable_vma(mm, rmap_item->address); - if (vma) { - err = try_to_merge_one_page(vma, page, - ZERO_PAGE(rmap_item->address)); - } else { + if (ksm_use_zero_pages) { + if (checksum == zero_checksum) /* - * If the vma is out of date, we do not need to - * continue. + * In case of failure, the page was not really empty, so we + * need to continue. Otherwise we're done. */ - err = 0; - } - mmap_read_unlock(mm); - /* - * In case of failure, the page was not really empty, so we - * need to continue. Otherwise we're done. - */ - if (!err) - return; + if (!try_to_merge_with_kernel_zero_page(rmap_item, page)) + return; } + tree_rmap_item = unstable_tree_search_insert(rmap_item, page, &tree_page); if (tree_rmap_item) { @@ -2226,6 +2290,7 @@ static struct ksm_rmap_item *try_to_get_old_rmap_item(unsigned long addr, *rmap_list = rmap_item->rmap_list; /* Running here indicates it's vma has been UNMERGEABLE */ remove_rmap_item_from_tree(rmap_item); + unshare_zero_pages(rmap_item); free_rmap_item(rmap_item); } @@ -2350,6 +2415,22 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) } if (is_zone_device_page(*page)) goto next_page; + if (is_zero_pfn(page_to_pfn(*page))) { + /* + * To monitor ksm zero pages which becomes non-anonymous, + * we have to save each rmap_item of zero pages by + * try_to_get_old_rmap_item() walking on + * ksm_scan.rmap_list, otherwise their rmap_items will be + * freed by the next turn of get_next_rmap_item(). The + * function get_next_rmap_item() will free all "skipped" + * rmap_items because it thinks its areas as UNMERGEABLE. + */ + rmap_item = try_to_get_old_rmap_item(ksm_scan.address, + ksm_scan.rmap_list); + if (rmap_item && (rmap_item->address & ZERO_PAGE_FLAG)) + ksm_scan.rmap_list = &rmap_item->rmap_list; + goto next_page; + } if (PageAnon(*page)) { flush_anon_page(vma, *page, ksm_scan.address); flush_dcache_page(*page); From patchwork Fri Dec 30 01:15:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13083998 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 45E8BC3DA7A for ; Fri, 30 Dec 2022 01:15:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD7B88E0003; Thu, 29 Dec 2022 20:15:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C880B8E0001; Thu, 29 Dec 2022 20:15:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B29808E0003; Thu, 29 Dec 2022 20:15:23 -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 9C95D8E0001 for ; Thu, 29 Dec 2022 20:15:23 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 751B3C01A6 for ; Fri, 30 Dec 2022 01:15:23 +0000 (UTC) X-FDA: 80297204526.20.D17CD1F Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.35]) by imf15.hostedemail.com (Postfix) with ESMTP id B01FAA0002 for ; Fri, 30 Dec 2022 01:15:20 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of yang.yang29@zte.com.cn designates 63.216.63.35 as permitted sender) smtp.mailfrom=yang.yang29@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=1672362921; 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=vJ+WhRMHBEmLRP1qcZM3trDK4cV64+QE7FKeYlR0Q4I=; b=AoeYSqJ8EZ88IN7yZpJZebK2Z+FdE5isSZ8YJj4EXe6638yFJzxuQAGj+ZOLraeUU76JzI LKCtG+Fq4+IIsbDk/GeYJ39nf1za1lBput/yeZlxyvbNLBFVUosIml2HIeNBSgCzYlv9I3 M4fSLn4fhopaCZnhe5x8ujV4etGSxnU= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.hostedemail.com: domain of yang.yang29@zte.com.cn designates 63.216.63.35 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn; dmarc=pass (policy=none) header.from=zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672362921; a=rsa-sha256; cv=none; b=zHdmdk7TP1nb7M8aXqxcrwpEmQKdMmXzF1O8WDE/GT5ET3uLK5m5/dPCpsE0a5KdUIIjzY jhjhswqSaijQt0rM32mtczoHpqVQ1ZcxB3w9K6/foKScKQPa1crzMRPK3kxz77rdimAAc/ eiKAN5h/pfPwV0M+TlMrN1KhbqxjFuA= 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 mxhk.zte.com.cn (FangMail) with ESMTPS id 4NjnPK4sdDz5PkHg; Fri, 30 Dec 2022 09:15:17 +0800 (CST) Received: from szxlzmapp06.zte.com.cn ([10.5.230.252]) by mse-fl1.zte.com.cn with SMTP id 2BU1FDjd083722; Fri, 30 Dec 2022 09:15:13 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:15:14 +0800 (CST) Date: Fri, 30 Dec 2022 09:15:14 +0800 (CST) X-Zmail-TransId: 2b0363ae3ba2067bb0e7 X-Mailer: Zmail v1.0 Message-ID: <202212300915147801864@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_3/6=5D_ksm=3A_count_all_zero_pages_placed_by_?= =?utf-8?q?KSM?= X-MAIL: mse-fl1.zte.com.cn 2BU1FDjd083722 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.138.novalocal with ID 63AE3BA5.000 by FangMail milter! X-FangMail-Envelope: 1672362917/4NjnPK4sdDz5PkHg/63AE3BA5.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3BA5.000/4NjnPK4sdDz5PkHg X-Stat-Signature: zsnthxm5riyaq8xpogm1qiaq7eyxp9he X-Rspam-User: X-Rspamd-Queue-Id: B01FAA0002 X-Rspamd-Server: rspam06 X-HE-Tag: 1672362920-421305 X-HE-Meta: U2FsdGVkX18RoDUonr7cDEzU+vePaK1ygAGJtUZCA3cSjGdLWociXiAbFx5n/1RSobfZyV8BDEdUnHxl8E/QHNYc3pVtV8FzrDc7rYOGPulcdaY6JdacF4CBmxmtoDwOEl3n5OpZWibzf4tofulfDpy2KS0/dTZYdof3jSjvSarV/f4e7Ss13gCuAG1+wl3pU9XrQCoZ72VjrZmeORYMXGlfPjYYigCmpnToIZ8mCjnT1LGA5V+/0rXizGqk/TEwklQkyYUElWW6FZCwhcij4uVGr+g41XnEGDJwGv6wzGhF2eszf8MrmXyp2AHIcNgdfygoqBS/HwApf7vV3cqd26a4SBewb2LSc/yvfrY1akCNyEQtEqtH9m/cljchaybccYMPK3+R0I6siPw+29X614pxYXA17aJFztoX4B7q1bf9AVF998ElZlGHdmq7B6Pu25DltJgSAhKyZzHBZ0KnYjFvxaixC7xixhh6ymtGQJiqWpnkfc1TbkqbKOjwIdDwvwDNcBSLwbG7GHnH5tpzlGkkvZidZSPbkT7yPOALGEuhBpjERWk+CtXB13HQfqtJ9iImt2HNc2Xv2jvGG9TJeJY8+g6bZL4OcUhABwBYBW1MWDjJXcrtBKP8oXR7a/9hw9pLBmIu+ll0tN7TFTZB/c0N8Zwtf/j71S7xFPVrEiIONzR+V0HK0lbWQRb0o0+FxvcCfsx+PwJWNrOR9NpMFNSc2FKAN4dzLNHwYzDgniYnldAYXOGOECCxa/z91UJippfkDsq4sV016wsdnbA8zsVNRtpzyX56lLmIq66MuBWCVXi4XfKuZjsypS1vBIkP8Atwxjx/sIWUsYfh/Sut0v8X/aIlpSJDjLC9y/hAztzztIwbQddvaKQf7Y35MT7oJyrPWfqEUzi0GDx2AsRSBj69sMBsNHj1lRIwYKoc8K4= 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: 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 zero_pages_sharing under /sys/kernel/mm/ksm/ to show the number of all zero pages placed by KSM. Signed-off-by: xu xin Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Reviewed-by: Xiaokai Ran Reviewed-by: Yang Yang v4->v5: fix warning mm/ksm.c:3238:9: warning: no previous prototype for 'zero_pages_sharing_show' [-Wmissing-prototypes]. --- mm/ksm.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 652c088f9786..72c0722be280 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -276,6 +276,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 placed by KSM use_zero_pages */ +static unsigned long ksm_zero_pages_sharing; + #ifdef CONFIG_NUMA /* Zeroed when merging across nodes is not allowed */ static unsigned int ksm_merge_across_nodes = 1; @@ -789,8 +792,10 @@ static struct page *get_ksm_page(struct ksm_stable_node *stable_node, */ static inline void clean_rmap_item_zero_flag(struct ksm_rmap_item *rmap_item) { - if (rmap_item->address & ZERO_PAGE_FLAG) + if (rmap_item->address & ZERO_PAGE_FLAG) { + ksm_zero_pages_sharing--; rmap_item->address &= PAGE_MASK; + } } /* Only called when rmap_item is going to be freed */ @@ -2109,8 +2114,10 @@ static int try_to_merge_with_kernel_zero_page(struct ksm_rmap_item *rmap_item, if (vma) { err = try_to_merge_one_page(vma, page, ZERO_PAGE(rmap_item->address)); - if (!err) + if (!err) { rmap_item->address |= ZERO_PAGE_FLAG; + ksm_zero_pages_sharing++; + } } else { /* If the vma is out of date, we do not need to continue. */ err = 0; @@ -3228,6 +3235,13 @@ static ssize_t pages_volatile_show(struct kobject *kobj, } KSM_ATTR_RO(pages_volatile); +static ssize_t zero_pages_sharing_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%ld\n", ksm_zero_pages_sharing); +} +KSM_ATTR_RO(zero_pages_sharing); + static ssize_t stable_node_dups_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -3283,6 +3297,7 @@ static struct attribute *ksm_attrs[] = { &pages_sharing_attr.attr, &pages_unshared_attr.attr, &pages_volatile_attr.attr, + &zero_pages_sharing_attr.attr, &full_scans_attr.attr, #ifdef CONFIG_NUMA &merge_across_nodes_attr.attr, From patchwork Fri Dec 30 01:16:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13083999 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 D36F2C3DA7D for ; Fri, 30 Dec 2022 01:16:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5359A8E0003; Thu, 29 Dec 2022 20:16:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E5788E0001; Thu, 29 Dec 2022 20:16:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D4378E0003; Thu, 29 Dec 2022 20:16:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 2DE3F8E0001 for ; Thu, 29 Dec 2022 20:16:39 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D66AD1A0134 for ; Fri, 30 Dec 2022 01:16:38 +0000 (UTC) X-FDA: 80297207676.06.4C36C72 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf09.hostedemail.com (Postfix) with ESMTP id 3AA60140008 for ; Fri, 30 Dec 2022 01:16:35 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf09.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672362997; 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=lcOvOXzlofwtDdJWJO/YaawEgINJw6332MG6HHQ7ykY=; b=tiMGz+GNBK2GWVTsnNyjTtrJjso+qtHH3rlB/3ctTG594QWOhWjyS36hbIIpOklNu8SxZe P4Sfcd9K16howCdxXV1uGzQDApugPNa4yoyG0wCmX49Uqqno6ZjYOaILiWf7sr/MxNs8fu 0e4yFfVfh2QQa//HGuuk7t0++UGcMlE= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf09.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672362997; a=rsa-sha256; cv=none; b=LCEnA18Ib0IcyMr7cP2uT8OXy1XxIFQTYlHDi5CPHPfwS/ffWzzPt6aOC+/jWqoJWBs38I SP/0j3C1LTi5MmjlwkoHr7YnpzrUUI1f5SKFDVKI0afQ64J6c4aEX4gIrmsmUlscgrOkc2 PIUrfTJKwCy+3wMyE+fs94iqytG3gEc= 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 4NjnQn2W1mz501Qh; Fri, 30 Dec 2022 09:16:33 +0800 (CST) Received: from szxlzmapp06.zte.com.cn ([10.5.230.252]) by mse-fl1.zte.com.cn with SMTP id 2BU1GSAF084154; Fri, 30 Dec 2022 09:16:28 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:16:29 +0800 (CST) Date: Fri, 30 Dec 2022 09:16:29 +0800 (CST) X-Zmail-TransId: 2b0363ae3bed7a8bf824 X-Mailer: Zmail v1.0 Message-ID: <202212300916292181912@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_4/6=5D_ksm=3A_count_zero_pages_for_each_proce?= =?utf-8?q?ss?= X-MAIL: mse-fl1.zte.com.cn 2BU1GSAF084154 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63AE3BF1.000 by FangMail milter! X-FangMail-Envelope: 1672362993/4NjnQn2W1mz501Qh/63AE3BF1.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3BF1.000/4NjnQn2W1mz501Qh X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3AA60140008 X-Stat-Signature: p6dimp46n1uaumjdbif6ih64jk1ecchp X-Rspam-User: X-HE-Tag: 1672362995-3811 X-HE-Meta: U2FsdGVkX19yhcoZo8gn6Zd3Ld0bwkCAZ/lgcrUw7ZHayMt3uP4A6+zKgifurT9ygqhOE9KeN8tNDvwxjRFAMdNA7GL+3L/uKdw9N6B4fSODstcHdEjzE+23YSF9KaPgL9EL4850QBMCV8J9D67GDD4yTIYFNAZkvpigPIZ14kBKMzMGzFpZwO2RNTCb3M2sdIK7g/eZLCM2Tra4VvjRu3t+WWbX7/NIB6bCS6G936KwrX1ls1fdThpv06K02zvhP9l2w2MbG70UkoeTOX528XOCRtQ/2Fv+gN5WwuytbAIl+1yjze1pmnCm4o+/kLR+VplCSqgDkvOXd7vPFKd0pSlHznP9AkU7KmnWk0kbdJwGgsX6bmaRVmSAMiFDYUujWK++Hm7Ht4yCEuSUemOdnKTrx1fgz+axjHbXgDKqhDhZ90SGP78NuJxIo1vwmrY8Jox2qMUXpSBXJNneXqf+wuOAhE6J2HOhzZwXoUlPol/0jE6STnLRrWlDnAU891LuE8B4dveg2erv52quzBqoLKFMMpwC5dOaCAVJHa/kh8zhIU93WWtR8DHl5AA0+cBD3FbomN1A/MP/NoWahq5ApV8rH9F0fz18yZUhewSL9Qt4bhGo+c2wokRkV1GR5/UOTp7BLKVQKDN4YXwCoDsonBdbzHOfDyCBTGt5g9OyfRD7ydlwucxDrJEmqxFYQpyXsi/XBytRwaoLWnCQ/yRPZy6dRMugcoO4RV+UGeduSLvSoenrdIUKbu+Ex4/yZ4z88mMRZEmNhayny3rGzzhKN2iTlGdxrRPVzZHchdXhDtTjsAekdmvxagR/Dh276VmkMX+jWWCIDoDdMuixBi7x5spVJ1h6AzbLnrmiyPvhv/3yqTYLWVro+r4dLQAxxvCHMbbmAiQd32tFCw9UV616k2rKCrbJ1tLf2jcDDiVm+Po= 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: 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. 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 zero_pages_sharing in it. Cc: Claudio Imbrenda Cc: David Hildenbrand Cc: Xuexin Jiang Cc: Xiaokai Ran Cc: Yang Yang Signed-off-by: xu xin --- fs/proc/base.c | 1 + include/linux/mm_types.h | 7 ++++++- mm/ksm.c | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/fs/proc/base.c b/fs/proc/base.c index 9e479d7d202b..ac9ebe972be0 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -3207,6 +3207,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns, mm = get_task_mm(task); if (mm) { seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items); + seq_printf(m, "zero_pages_sharing %lu\n", mm->ksm_zero_pages_sharing); mmput(mm); } diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 4e1031626403..5c734ebc1890 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -776,7 +776,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_sharing). */ unsigned long ksm_merging_pages; /* @@ -784,6 +784,11 @@ struct mm_struct { * including merged and not merged. */ unsigned long ksm_rmap_items; + /* + * Represent how many empty pages are merged with kernel zero + * pages when enabling KSM use_zero_pages. + */ + unsigned long ksm_zero_pages_sharing; #endif #ifdef CONFIG_LRU_GEN struct { diff --git a/mm/ksm.c b/mm/ksm.c index 72c0722be280..083f5d125373 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -794,6 +794,7 @@ static inline void clean_rmap_item_zero_flag(struct ksm_rmap_item *rmap_item) { if (rmap_item->address & ZERO_PAGE_FLAG) { ksm_zero_pages_sharing--; + rmap_item->mm->ksm_zero_pages_sharing--; rmap_item->address &= PAGE_MASK; } } @@ -2117,6 +2118,7 @@ static int try_to_merge_with_kernel_zero_page(struct ksm_rmap_item *rmap_item, if (!err) { rmap_item->address |= ZERO_PAGE_FLAG; ksm_zero_pages_sharing++; + rmap_item->mm->ksm_zero_pages_sharing++; } } else { /* If the vma is out of date, we do not need to continue. */ From patchwork Fri Dec 30 01:17:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13084000 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 34B2AC3DA7A for ; Fri, 30 Dec 2022 01:17:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C60CB8E0002; Thu, 29 Dec 2022 20:17:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C11178E0001; Thu, 29 Dec 2022 20:17:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFFD28E0002; Thu, 29 Dec 2022 20:17:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A21648E0001 for ; Thu, 29 Dec 2022 20:17:38 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7A77B1601C5 for ; Fri, 30 Dec 2022 01:17:38 +0000 (UTC) X-FDA: 80297210196.03.E26B9DC Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf12.hostedemail.com (Postfix) with ESMTP id 5E0D640003 for ; Fri, 30 Dec 2022 01:17:35 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf12.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1672363056; 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=XU9FheZT57GPBRP073pAhzfqBkSRwOOpOqW3eL8idB8=; b=IdvMkk5sjNJTHTfe8gFqaEscMRROSbIKLLMi0I1VnrQuAX0LH8l+zYjXjW2WMQ5rP+7RrS 7gBLSvFtHMXQ0OlGhymOMqjof6oMB5pQ+yMIkOivpBITANNmOa2hCzMymJeaTmO6DwQtUO OZVm4CEuNZhfQZ4ef+EvhtLRufxXrw4= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf12.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672363056; a=rsa-sha256; cv=none; b=ojNFyc7bukZLRE9A9bvMyPY4Z9Ietc6Tx21ny1jtbJCK1fsmiSJdYBV2nl8FGd4qjpjhjO 7N/WmykkvO5UvqZUQ24pRzILBdQxJdZzCUcwUHBPDY05XVoh/LnMs1DOezlQfDs/YAkkJ6 vT5hunuiH9BJ83vaLypQoLWqJWFaa/8= Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4NjnRx3gb2z501Qn; Fri, 30 Dec 2022 09:17:33 +0800 (CST) Received: from szxlzmapp04.zte.com.cn ([10.5.231.166]) by mse-fl2.zte.com.cn with SMTP id 2BU1HRq8060335; Fri, 30 Dec 2022 09:17:27 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:17:28 +0800 (CST) Date: Fri, 30 Dec 2022 09:17:28 +0800 (CST) X-Zmail-TransId: 2b0363ae3c28ffffffffc3ec2cb7 X-Mailer: Zmail v1.0 Message-ID: <202212300917284911971@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_5/6=5D_ksm=3A_add_zero=5Fpages=5Fsharing_docu?= =?utf-8?q?mentation?= X-MAIL: mse-fl2.zte.com.cn 2BU1HRq8060335 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63AE3C2D.000 by FangMail milter! X-FangMail-Envelope: 1672363053/4NjnRx3gb2z501Qn/63AE3C2D.000/10.5.228.133/[10.5.228.133]/mse-fl2.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3C2D.000/4NjnRx3gb2z501Qn X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5E0D640003 X-Rspam-User: X-Stat-Signature: 3qk3q4osx6bnq85afz9czc5eyatdsar3 X-HE-Tag: 1672363055-106126 X-HE-Meta: U2FsdGVkX1/+/5tCJhFEutYvV/jIbkRFrNTOVRETJ+WGnGaLbbN8SHafHQ30Jn3bGz+EfY4ULWow1AkV8IyWDRNg+HfgCOHVhyIvYHjgViuCYDeS7SaCe8UG00WCTPqW5Vp57N/u+TZn+GKdGxik+Bj7hF+Gck3DOOIaKn9YQdxc98b8C4v7Hy5MrY4+HaHmyHIC2hpHCAZtaPApL6QafhjDHQPJJMlincUgc0Tiw3314YAtDS6D9PF5+q9fikn14GnsUSrOd6/pqBGSmjJd4CGuBOejYWdwfxnViLVeL7kX1I4DxuxgP8SaTuULeP2ymEhzKNxWVmpzIP/e38OpWwu61cHo51kcZHrGZE92n2b51iFA2kxghhulFWGoNN0IbA9daLQHWyalHS7fm9Ybax4ccE3l5pRXoaSj3t8Mn8ZT6D7/sBpQBOE6ugrfeg3Pq0rIk8kuABzsc5DM/RFKK3iQ03UTVnesj8rAoy3IywYGyK0O9e5UqfDvdBTwK45j42AOZHCF8OblS/prwZkbHEp2X1gNowgETGcgUG4cZHx7+U2Y87DM3Tnr7fByLib1uqs/3N6V7KhTxfstQscCv8BaPvGYx3QGIlPEgi8Ak0+MtMb0hlIXpzscT9xB3eyhOsf6iSMM2t40xg02sGXdS66Q3C2vMy3FxWv9GI8OwVDAetn+TlfvaFzCVHtCY+QYr8fF/jjAbfxG/t3SxDCmRYNvUXNXLnpgWh2iRTJevslZ2TgzrR0iaMftXjn/6qXJN/WKukHSOgu4tSuySQkBZFjXiLXwfgqL73axTfMO266L9dC+5s4Gqw6YcN8YYkVKYbhuyZ6+AMUNPXc9KcKASLYj8CikO6ijA/z2rumEgPl5W5Kin9MNzumL+XjGZQORmSwDHlP2B5Ib0k7gHsBQCHj9YorVJTd74DYtXpzPrHohZs1KHTkGKWaGuYNX5Gs6UvZAQ/L96eB4CFhAQ2d a9A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000062, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: xu xin When enabling use_zero_pages, pages_sharing cannot represent how much memory saved indeed. zero_pages_sharing + pages_sharing does. add the description of zero_pages_sharing. Cc: Xiaokai Ran Cc: Yang Yang Cc: Jiang Xuexin Cc: Claudio Imbrenda Cc: David Hildenbrand Signed-off-by: xu xin --- Documentation/admin-guide/mm/ksm.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Documentation/admin-guide/mm/ksm.rst b/Documentation/admin-guide/mm/ksm.rst index fb6ba2002a4b..f160f9487a90 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 +zero_pages_sharing + how many empty pages are sharing kernel zero page(s) instead of + with each other as it would happen normally. Only effective when + enabling ``use_zero_pages`` knob. + +When enabling ``use_zero_pages``, the sum of ``pages_sharing`` + +``zero_pages_sharing`` represents how much really saved by KSM. A high ratio of ``pages_sharing`` to ``pages_shared`` indicates good sharing, but a high ratio of ``pages_unshared`` to ``pages_sharing`` From patchwork Fri Dec 30 01:18:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13084001 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 2661AC3DA79 for ; Fri, 30 Dec 2022 01:18:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9C128E0002; Thu, 29 Dec 2022 20:18:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4C9D8E0001; Thu, 29 Dec 2022 20:18:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A149A8E0002; Thu, 29 Dec 2022 20:18:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 911A88E0001 for ; Thu, 29 Dec 2022 20:18:57 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 61F111401D3 for ; Fri, 30 Dec 2022 01:18:57 +0000 (UTC) X-FDA: 80297213514.06.E5C3D26 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf11.hostedemail.com (Postfix) with ESMTP id C5FF440007 for ; Fri, 30 Dec 2022 01:18:54 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; spf=pass (imf11.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@zte.com.cn; dmarc=pass (policy=none) header.from=zte.com.cn ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1672363135; a=rsa-sha256; cv=none; b=tOEsaai7ToFYskslfGepJr+p9+XrmyC8fsWUTweS2rGdlXnelyLtljR4JxMkpl1JDzm2z4 HEJQ5AdEO54GWaGSFB6EBMHnuskXnqjGIsEgrY2sLqn5gEfHlZ2EjIQgG54ZJVGvXRHq2q 7rooFzoVrZEzOnVEKqzNjrkxAh1XL84= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; spf=pass (imf11.hostedemail.com: domain of yang.yang29@zte.com.cn designates 183.62.165.209 as permitted sender) smtp.mailfrom=yang.yang29@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=1672363135; 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=qDt9E+H3ZcjHlcvgzmR4RDBbVLN6q6RAw+nqg9pWau4=; b=KCRehd4WZUsu6Je1us/XF506rlmNUwY8lbQC5UamQuvKzSwlTWAb1uRzruCwYVT8sknEGs aOKsaFrumltmmxrXpdRxizVdTwCVE69Z1SzYSqJx9ja4a031wttlzR8mpu9vQo/O/Yu2Sp IFKjY0CUxnhkc+AbS4qru4/gdnVD/fA= Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4NjnTS10zfz501Qj; Fri, 30 Dec 2022 09:18:52 +0800 (CST) Received: from szxlzmapp05.zte.com.cn ([10.5.230.85]) by mse-fl2.zte.com.cn with SMTP id 2BU1IkTi061010; Fri, 30 Dec 2022 09:18:46 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 30 Dec 2022 09:18:47 +0800 (CST) Date: Fri, 30 Dec 2022 09:18:47 +0800 (CST) X-Zmail-TransId: 2b0363ae3c77ffffffffe91c6f06 X-Mailer: Zmail v1.0 Message-ID: <202212300918477352037@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_6/6=5D_selftest=3A_add_testing_unsharing_and_?= =?utf-8?q?counting_ksm_zero?= =?utf-8?q?_page?= X-MAIL: mse-fl2.zte.com.cn 2BU1IkTi061010 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63AE3C7C.000 by FangMail milter! X-FangMail-Envelope: 1672363132/4NjnTS10zfz501Qj/63AE3C7C.000/10.5.228.133/[10.5.228.133]/mse-fl2.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3C7C.000/4NjnTS10zfz501Qj X-Rspam-User: X-Rspamd-Queue-Id: C5FF440007 X-Rspamd-Server: rspam01 X-Stat-Signature: xh4sfx5susyq911o1cyq3i1xjj633w4k X-HE-Tag: 1672363134-783814 X-HE-Meta: U2FsdGVkX19tmh5NOJc5NY2hLQ/UqCLB1Fy6gkFbBo1vjk9D04C8y/ZHkLEr8iOy3oqlCi33XLaRTaKWIWX1NYuDGeApusSQnnze85+1YjJtU6/CY743sgQ8QVucPeXT2/as3QL2NECIwEJ+E7Hzx17ARgPyVmnC3KyxTO08gXp6qjhnOVZBvd+vtAhpLuo5/rP6vNjrtJEULzZIzfucc6iZmprS0n6YEA1EUsqfpZ+G6JtJbv9K0kXg3+/KiQLFfSu/L26DDmzO2d+2N1X+1a3ZqzgPUoGQNtbeizpA4JoA2q0h3k0dDMAeLvA+GGJfBfmE9J5fwJRljwCYiQB91m8mIGo9Ybkk6eQ9NYeTmmRaKGgnq+PPRYAFvKNTfAEovBaKGfR6Nh1/U7foQBu305xk+8Q4UN4ei9UoU7+ipEVeUu0v9iZohAaq7K9mOhF9wmpOc/5iWFsW7vgiUU5J1k6OU6Y0yRR2aShltkbx7pBaGUEjms/A8wVLqhIssf9oPYffBfg7a7kmufat8210008soof/N85E77IZqo91mhcZ4TLTfJnIDOJQ4xuVa+VHk/t6iqoQ3HrqiS+HXAL5SrHvUAOywrdu3s8bS3Zshj9mC+nEQPUfCFlj7RrhYIM8S7U4+m9aFKvbXQ5iH8MzI1TjZkenEpECNLIenIhhIsFk73o9OGOVYFDJkulV/0rTFVwVBCu8GpDfSSKIrV/xj7llWrLuerjYhL/siamBP7dwt2e+janMk+1fq31MrIz/aXzsL9mFauA2TuaOEnpBKHM0tE6rIB5Ke1xrfaXlglrbp454bZKS+nzBk8YFxSYquh8SOMAdYBvnE2PNU6BORpCHeuSU5BW6qjG8tH5Wtwb1fUXei2n2UQIngnzgoo8g3gQAdzkhbSiBlFcwekFetpv5kAizHO77iqRSuPsihrg= 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: 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 page can react correctly to cow (copy on write); 2) whether the count of ksm zero page can react correctly to unmerge; 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 v4->v5: fix error of "} while (end_scans < start_scans + 20);" to "} while (end_scans < start_scans + 2);" in wait_two_full_scans(). --- tools/testing/selftests/vm/ksm_functional_tests.c | 103 +++++++++++++++++++++- 1 file changed, 99 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/vm/ksm_functional_tests.c b/tools/testing/selftests/vm/ksm_functional_tests.c index b11b7e5115dc..b792798a54c4 100644 --- a/tools/testing/selftests/vm/ksm_functional_tests.c +++ b/tools/testing/selftests/vm/ksm_functional_tests.c @@ -27,6 +27,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; @@ -57,6 +59,22 @@ static bool range_maps_duplicates(char *addr, unsigned long size) return false; } +static bool check_ksm_zero_pages_count(unsigned long zero_size) +{ + unsigned long pages_expected = zero_size / (4 * KiB); + 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 == pages_expected; +} + static long ksm_get_full_scans(void) { char buf[10]; @@ -70,15 +88,12 @@ static long ksm_get_full_scans(void) return strtol(buf, NULL, 10); } -static int ksm_merge(void) +static int wait_two_full_scans(void) { long start_scans, end_scans; - /* Wait for two full scans such that any possible merging happened. */ start_scans = ksm_get_full_scans(); if (start_scans < 0) - return start_scans; - if (write(ksm_fd, "1", 1) != 1) return -errno; do { end_scans = ksm_get_full_scans(); @@ -89,6 +104,34 @@ static int ksm_merge(void) return 0; } +static inline int ksm_merge(void) +{ + /* Wait for two full scans such that any possible merging happened. */ + if (write(ksm_fd, "1", 1) != 1) + return -errno; + return wait_two_full_scans(); +} + +static inline int make_cow(char *map, char val, unsigned long size) +{ + + memset(map, val, size); + return wait_two_full_scans(); +} + +static int unmerge_zero_page(char *start, unsigned long size) +{ + int ret; + + ret = madvise(start, size, MADV_UNMERGEABLE); + if (ret) { + ksft_test_result_fail("MADV_UNMERGEABLE failed\n"); + return ret; + } + + return wait_two_full_scans(); +} + static char *mmap_and_merge_range(char val, unsigned long size) { char *map; @@ -146,6 +189,56 @@ static void test_unmerge(void) munmap(map, size); } +static void test_unmerge_zero_pages(void) +{ + const unsigned int size = 2 * MiB; + char *map; + + ksft_print_msg("[RUN] %s\n", __func__); + + /* Confirm the interfaces*/ + ksm_zero_pages_fd = open("/sys/kernel/mm/ksm/zero_pages_sharing", O_RDONLY); + if (ksm_zero_pages_fd < 0) { + ksft_test_result_skip("open(\"/sys/kernel/mm/ksm/zero_pages_sharing\") failed\n"); + return; + } + ksm_use_zero_pages_fd = open("/sys/kernel/mm/ksm/use_zero_pages", O_RDWR); + 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); + + /* Case 1: make Writing on ksm zero pages (COW) */ + if (make_cow(map, 0xcf, size / 2)) { + ksft_test_result_fail("COW failed\n"); + goto unmap; + } + ksft_test_result(check_ksm_zero_pages_count(size / 2), + "zero page count react to cow\n"); + + /* Case 2: Call madvise(xxx, MADV_UNMERGEABLE)*/ + if (unmerge_zero_page(map + size / 2, size / 4)) { + ksft_test_result_fail("unmerge_zero_page failed\n"); + goto unmap; + } + ksft_test_result(check_ksm_zero_pages_count(size / 4), + "zero page count react to unmerge\n"); + + /*Check if ksm pages are really unmerged */ + ksft_test_result(!range_maps_duplicates(map + size / 2, size / 4), + "KSM zero pages were unmerged\n"); + +unmap: + munmap(map, size); +} + static void test_unmerge_discarded(void) { const unsigned int size = 2 * MiB; @@ -261,11 +354,13 @@ int main(int argc, char **argv) ksm_full_scans_fd = open("/sys/kernel/mm/ksm/full_scans", O_RDONLY); if (ksm_full_scans_fd < 0) ksft_exit_skip("open(\"/sys/kernel/mm/ksm/full_scans\") failed\n"); + pagemap_fd = open("/proc/self/pagemap", O_RDONLY); if (pagemap_fd < 0) ksft_exit_skip("open(\"/proc/self/pagemap\") failed\n"); test_unmerge(); + test_unmerge_zero_pages(); test_unmerge_discarded(); #ifdef __NR_userfaultfd test_unmerge_uffd_wp();