From patchwork Mon Dec 26 02:01:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13081558 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 61F7DC46467 for ; Mon, 26 Dec 2022 02:01:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3BEB900004; Sun, 25 Dec 2022 21:01:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CEC56900002; Sun, 25 Dec 2022 21:01:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB44B900004; Sun, 25 Dec 2022 21:01:25 -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 AA313900002 for ; Sun, 25 Dec 2022 21:01:25 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 1F4D11204E9 for ; Mon, 26 Dec 2022 02:01:25 +0000 (UTC) X-FDA: 80282805330.14.9E33B33 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by imf12.hostedemail.com (Postfix) with ESMTP id DF9F840002 for ; Mon, 26 Dec 2022 02:01:22 +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 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=1672020083; 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=28cza/u2aRQSvUsL28YQc+oAGxUZMdxK6+x65cyme5E=; b=S0ZZSFRSlre+YIET++OjQTOBvvsIktOCpDWbtpU51XvGp4Y1WvjtR/bG4oB2YSbijSagxp iX3IJVMXEPlEI2E7pgXCuY9Tm/cdLuig1l1e9aBVHEmq+2b43+GEYAaaWBg6YXyVsjrVIG VmC4IGeks/QAt9/IRvCfZwRMSiXSMO0= 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 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=1672020083; a=rsa-sha256; cv=none; b=n6R+FadCi62TKxWKbjbECYKr2YOVkLJejVTZyG90VOlI6ftBaoeqRBLZoHT/5jlvV1Jq0V pn2kpUX+/FTbARQ9KZ61jsguYXmeAAN7PzMsw+O9MMUYzOXH4+YHuo7+h8mi747icdtV3I P9RLrnVPTm/p7c50OehstIYW8AHQPoc= 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 4NgLcH2ljtz8R041; Mon, 26 Dec 2022 10:01:19 +0800 (CST) Received: from szxlzmapp05.zte.com.cn ([10.5.230.85]) by mse-fl1.zte.com.cn with SMTP id 2BQ21E0a052063; Mon, 26 Dec 2022 10:01:14 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Mon, 26 Dec 2022 10:01:16 +0800 (CST) Date: Mon, 26 Dec 2022 10:01:16 +0800 (CST) X-Zmail-TransId: 2b0363a9006c2e76458f X-Mailer: Zmail v1.0 Message-ID: <202212261001160129980@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v4_1/6=5D_ksm=3A_abstract_the_function_try=5Fto?= =?utf-8?q?=5Fget=5Fold=5Frmap=5Fitem?= X-MAIL: mse-fl1.zte.com.cn 2BQ21E0a052063 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63A9006F.000 by FangMail milter! X-FangMail-Envelope: 1672020079/4NgLcH2ljtz8R041/63A9006F.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63A9006F.000/4NgLcH2ljtz8R041 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: DF9F840002 X-Stat-Signature: w65tdnsyyqhpdo7s8gupjwxemxeswkup X-HE-Tag: 1672020082-721363 X-HE-Meta: U2FsdGVkX1/1hhuUs17mp2F58VqTahD7bWVIPAJ6kz4vMwmrjn5BAgKJt0Rw1npTTWCGRWm5h3l4WB+CLhkftPnOp+uoIrRjktTXuXlslTdCFiuVqZwZOLYGh4FPc/3hF3y/0WUJ7B2cn9DmlutPnXrBEbzeBwFdPkrvNoMJ2Ss0ScdXstj+xQR9/Fp6nXv1iA7RtCLvepdp59ZIx0aDqNnVSS25GkJ6gz4ltmkvztDJjoeUkm+OkI67epUfnQOzdsnHG6ThTAgycjuXS3ASYXBZzZsvPc4QJEsBHeeVP8OVHfJXNxMkp309HWa1VCgCejQHghEvDh9QkBsSalywVtm4En85D971uVrueR8M66V3kDyKTZ+kex4iTdAodTVHaIaw7ovx1q8XP4hOfrsYApglD2ZkN8QqbWJt0TgOPZmYmpdno7lMK2LW+CUOeLmyKV3DggA9CRsULgflmOe9yNdBtCTf4etDQNYjJoCQOcILXSSbdcmV2VLFkXl23slGr5MGZgCWlpD1P8ySZX3W5nLDpqdBrFdSlALsquAmJV2oF+WDqhItB3wjB4K+2acWLgpktSIuFSiBdWLejAv7fQL1jqbNKdCS6QfS5ZP22KvlFYNtfDed/2ueHED/nPWALVPOQEZF5ePHZ3LzQRYwJTM7m1azlbdZ8sPCP/3xQf28uclsEArdglG1gWv0PU/T1NPn9AbJ5iH8h+1ScY3G/93lz9lL7NxjkNL6akJlJY139E0loGf0nLnJOE0leN/G+3DiCThWEDSonAyOBSuKgUiV+a5DKS9oVEEiMd/X1MA/oy1tTlZapAEhvabDwMRCiAz8z+j/4dsEMYck2/Yf6bitP1B+/wLAO3z9a9+G3yZcuiSUhUSN1vdi1zyrIbAM88dYxzqB4iVLPISj0THoQJQCLPWpsxdJdAl0Vx2XgAZxX2PDS4RnXGFR/2n9dOj7N3Jm6y0oM5fxmrVypNL K8Q== 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 dd02780c387f..8d6336ef0750 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 Mon Dec 26 02:02:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13081559 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 3AAFDC4167B for ; Mon, 26 Dec 2022 02:03:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACA6D940007; Sun, 25 Dec 2022 21:03:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A7A5B900002; Sun, 25 Dec 2022 21:03:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91A7B940007; Sun, 25 Dec 2022 21:03:05 -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 830EB900002 for ; Sun, 25 Dec 2022 21:03:05 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 53AFD1604C6 for ; Mon, 26 Dec 2022 02:03:05 +0000 (UTC) X-FDA: 80282809530.03.478F99E Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.35]) by imf03.hostedemail.com (Postfix) with ESMTP id AEC1520014 for ; Mon, 26 Dec 2022 02:03:02 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; spf=pass (imf03.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=1672020183; a=rsa-sha256; cv=none; b=Kzz/dT9g/fyKK+GqiiPEoFhcYjWzQgZAtLnCIChEHQvpqpLpbuw2DOWNDCpmFj9n9myDM8 AepocpzXZNs0KVDglGr5dbjlKGLHXixQas37Dno9/g0gmG824HFIrCnoOOF6buEee2x2mL aUJmGIydZuajh+oEz4xGTbWVB89NE/4= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; spf=pass (imf03.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=1672020183; 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=1L+ZJzIOXe9Ah4jra1HtPnRJmquPWiYS64cRlChQkqg=; b=QFup63orJ1R+ewDYtzrxM3k/vle+jbwrZCO53JQ00u5IjPpSfEGYoMXAOaJkakacKwkPtm z9Thm5a2e1VfQMx9qg9JVKPOhB7HDxBUP2fKzyWEcf8VznEY62dsD5IAa+krhI32Gshjbj voXAhVIeefmJpkyXrQfnua4uLIsWcGo= 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 4NgLfC6mS5z4xVnH; Mon, 26 Dec 2022 10:02:59 +0800 (CST) Received: from szxlzmapp05.zte.com.cn ([10.5.230.85]) by mse-fl2.zte.com.cn with SMTP id 2BQ22re0032448; Mon, 26 Dec 2022 10:02:53 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Mon, 26 Dec 2022 10:02:54 +0800 (CST) Date: Mon, 26 Dec 2022 10:02:54 +0800 (CST) X-Zmail-TransId: 2b0363a900ce6636a905 X-Mailer: Zmail v1.0 Message-ID: <202212261002549090051@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v4_2/6=5D_ksm=3A_support_unsharing_zero_pages_pl?= =?utf-8?q?aced_by_KSM?= X-MAIL: mse-fl2.zte.com.cn 2BQ22re0032448 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.138.novalocal with ID 63A900D3.000 by FangMail milter! X-FangMail-Envelope: 1672020179/4NgLfC6mS5z4xVnH/63A900D3.000/10.5.228.133/[10.5.228.133]/mse-fl2.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63A900D3.000/4NgLfC6mS5z4xVnH X-Rspam-User: X-Rspamd-Queue-Id: AEC1520014 X-Rspamd-Server: rspam01 X-Stat-Signature: 871okmbzszrwjszono86qwuigyubjk1y X-HE-Tag: 1672020182-428774 X-HE-Meta: U2FsdGVkX19dY6FgKJwJGDFiekLb6/lvxg6L4459dTdKk1pitr+s5kXbnTONrGuCiij5RHvA+SV6PV0gMKlAD5M9pnVoPVTSdDW79YDTurIL1PnVjoS+7cqiVhhVOMxsEUAuVda5hVSjkB2gy6ilkMHNqXHdE3Aado7AOKZhpjPc9ZWZBUIc2m5P3FrO8VrzSFZgcs0NPHFsWgKwBBGz9R8TEkgTfgZA+/50LUVo2A0KLfQDMPTtUclbxLs3AFvQfuWY2X+Hwziv0DyxUJj9VT4/tEz3FhTDAJ3D7ru8093SSs6ymHYCZIXjRsVOzVegZbecu60RPB+gu9DVxvo6dGh8s8Vz3QDY8EgkTQ5jy3gfb3rHFc5dZwW8l8YWulhG5RA9PxZVLJbKs+WutklIfolz7VaHCn3uT8aLVFlHOVeVH5kCQjbXCHEoMJuV6l/+OPagMhO6J6/UE06RtokOLUhwrg6NBDGsAyVu1o5h10oIVBCQhGqrgBVo9rxem2PuV3tyz9b+0h3RknvsLJ/3ldB4BQhKvuev4+aW5jHwVlmsvSWzmQ0PVoXsJAfjxZ8XErCyMPhSceAKAbFEW8CoPD7Ko+DoiT93e4zCaSJLvEWGTNX3f1mDzEGNfNIbSyk1mToqfkxobWCSMPcMVpyRzSvbFtqCOaM7E6VTM/ZM4kB9Z6YRARYzajvH905wvpBifFXWz9PgCcZahsQfiijhJ0JaEV5bHmyRnJDze3+FRPF6PvgjW6MiDs3EaIw3abjou1lH3pSyJwDtHsvFtPY09/rgxu4H0+ltAupFp4ZKBR6PonkFa8XRt7cVpkzutsLMFZ5jVQ2DLDrRF9rADrjqXLRUvCx0YFzcaX4wE0pHBrSukiJGjO66tucP0PD+lQoOdL2dgczyECLVwJ0txZIA6dvQilJNx86NpJagqOJbZpU= 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 8d6336ef0750..b7fc7d86d11a 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 Mon Dec 26 02:04:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13081560 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 180F9C46467 for ; Mon, 26 Dec 2022 02:04:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8583B900003; Sun, 25 Dec 2022 21:04:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 80868900002; Sun, 25 Dec 2022 21:04:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6F885900003; Sun, 25 Dec 2022 21:04:41 -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 5D8BB900002 for ; Sun, 25 Dec 2022 21:04:41 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 29D254034B for ; Mon, 26 Dec 2022 02:04:41 +0000 (UTC) X-FDA: 80282813562.05.5D2347C Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf01.hostedemail.com (Postfix) with ESMTP id C60614000A for ; Mon, 26 Dec 2022 02:04:37 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf01.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=1672020278; 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=RRcYd0BIbv1l2hMdePPfbOWbARE2cwJ/mBK4CfFjrwg=; b=qaP/apuDRvo4jNvJgI8oND3t4W08go13EpFiNhymyGjGvdlhPN8/lsq7O6X3SAs+2B91My Rc/CiNrfdgQV1DTt1zMcsn4GftOCFFet/spY7DUgcBZ1RQHDI/RiOCIGRVjNaEMXipkPFt 1RaqxpA8TzXM0ode7gUVw3/VtSV6QLU= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf01.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=1672020278; a=rsa-sha256; cv=none; b=CBf6iHD9TXuNqS2X7GILednOVuBuIgltNvCKUEXwmXVuu2tEyBF0/Bxth8x/249+KKF+jW 2a8f5O1l1onAGP0KL/oY0/gUbaA7yki6gFj8NsXhj6Jp9iDmrQNVg17x2VCsPmiJzBIqFa ALFfDwFa02MgIMq7WOBn2cJYFGts5Ic= 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 4NgLh22Mcvz501Td; Mon, 26 Dec 2022 10:04:34 +0800 (CST) Received: from szxlzmapp07.zte.com.cn ([10.5.230.251]) by mse-fl1.zte.com.cn with SMTP id 2BQ24PHa055013; Mon, 26 Dec 2022 10:04:25 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Mon, 26 Dec 2022 10:04:26 +0800 (CST) Date: Mon, 26 Dec 2022 10:04:26 +0800 (CST) X-Zmail-TransId: 2b0363a9012a68f7092b X-Mailer: Zmail v1.0 Message-ID: <202212261004269680139@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v4_3/6=5D_ksm=3A_count_all_zero_pages_placed_by_?= =?utf-8?q?KSM?= X-MAIL: mse-fl1.zte.com.cn 2BQ24PHa055013 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63A90132.000 by FangMail milter! X-FangMail-Envelope: 1672020274/4NgLh22Mcvz501Td/63A90132.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63A90132.000/4NgLh22Mcvz501Td X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: C60614000A X-Rspam-User: X-Stat-Signature: pzszxdn9gnfs9jiuozeuftmn99ox36jw X-HE-Tag: 1672020277-326470 X-HE-Meta: U2FsdGVkX19xyF6GpEfUhA/jGU2+074vpNVIP1J+3yMjx+MrZGBxu8wLDwiSn4vS3SfoZNtlWGRyu67AOxYmHTWiqF5GvWohsafq+j+vkRv0XKBc1Oo9wX/NTlfZ93u3I6L8mtMNu+DPjyN7eMe6mxW7IRnSb6T7pzpu8SvGFsHA3spHbTaejcRPTEc5v2UBMcY4ljNX8hCcCIPgtVToOLdBTsR+i8xo/xArQ6rPukWO+1J4WqHOcT1zL3OWLHFrsyWB9LA68doVBFoggT9/PIeD8opUsgKNfu2tnB3yfSBnhVfKQpa+63YJ4SyKElkL5nd+WrtsP7oM9ubjJ6/ekClwRwlmwxoOh0O5YTEfB/nyQlvqI/GAH+Rq9yA9Jpa3XR2NpRNmTGfnPHb0blBAEjm8MGgBKaL/CKYoQE8yanpvfFKl4UnrlkyX+AkIk94EFW4T2nCvYnIqZ7MQN+e2kiQoEN/o8w+TGsYZTbPY6lp4QYLlvZno+3TtBH3uTzOehh7VJ74CPZGOSEnzmE8FzOXPTpEpF6QCsLpg8Zy6Zj6/L3v4pGMKilLa+YmKbfzFuAiHWsqPNR6ZCkYiftihccO4QXylA+PC0Lp+g4Dj2HkHx079mRT2XzX/+bqVNyKn44nWaO8EFbkPh8AZTekX4v09u1HPcXMyntUvKc7JC4GP+yMkd6vo2afaV2+pXyQ44CU+T8cy0cBouHqDHnR2IktM0AiTPGvDxjIe/LF8iJB0GUTSmW5dv4CEsJuvMv7AELfW1zfNVjSK85LH30L0qc+A4nir12voaOpdescPkQVEZiCGNLbCqVDpSO9Hjostyfb+bjETfQN+AJQSLAJrrtR8m6q2zR3/k/cGOtYo45aMD2HjE6Iyfn7vXUGrcZ4Fy/j6MnncymstfzhSS7g05klERDvCgF3DHVo+PVCc0zo= 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 --- mm/ksm.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index b7fc7d86d11a..e02cc89512cd 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; @@ -3224,6 +3231,13 @@ static ssize_t pages_volatile_show(struct kobject *kobj, } KSM_ATTR_RO(pages_volatile); +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) { @@ -3279,6 +3293,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 Mon Dec 26 02:05:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13081561 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 17D25C4167B for ; Mon, 26 Dec 2022 02:06:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98EE4940008; Sun, 25 Dec 2022 21:06:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 93F48900002; Sun, 25 Dec 2022 21:06:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82EC2940008; Sun, 25 Dec 2022 21:06:09 -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 73878900002 for ; Sun, 25 Dec 2022 21:06:09 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 3EA411C4472 for ; Mon, 26 Dec 2022 02:06:09 +0000 (UTC) X-FDA: 80282817258.22.8C4A668 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf22.hostedemail.com (Postfix) with ESMTP id 9F81DC000C for ; Mon, 26 Dec 2022 02:06:06 +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=1672020367; 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=0e98/MKwqcuwIVV5LBEnxmtT1/kAVs7NTPIMztvTxQ8=; b=XhDwEA89hn2dfABawZkObi+CloTjw2rW2umpXwZw+dSyM3Jkr0XXGA8zmIZat3Jm+36b72 KHb/UI3NZOL5MPWffxb9jLY3LBOFgstay5El7RkfjBSvFbB8u8aq4zGEppTiijIafvceTw u2d0g98NmAJfDxYXKAxHrif3e/ZooHE= 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=1672020367; a=rsa-sha256; cv=none; b=cOqz3tkscNql6EkQG0xjoEvNpeg7th920Bzeg9u2EYE4VnMGt1NjDokGpbu1HDp+HybS1+ GxxLskfQIDssiWpmtdT44ZJ9Avvicf1/b6saE0nZ8xGGEj3XgXnU2bGrQkazlfCIqJsFip ID0X1Q+N02AsPBNZezYWKZRchPIS17E= 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 4NgLjl6hXqz4xpyB; Mon, 26 Dec 2022 10:06:03 +0800 (CST) Received: from szxlzmapp03.zte.com.cn ([10.5.231.207]) by mse-fl1.zte.com.cn with SMTP id 2BQ25poD056061; Mon, 26 Dec 2022 10:05:51 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Mon, 26 Dec 2022 10:05:52 +0800 (CST) Date: Mon, 26 Dec 2022 10:05:52 +0800 (CST) X-Zmail-TransId: 2b0363a90180ffffffff8da76b70 X-Mailer: Zmail v1.0 Message-ID: <202212261005526160207@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v4_4/6=5D_ksm=3A_count_zero_pages_for_each_proce?= =?utf-8?q?ss?= X-MAIL: mse-fl1.zte.com.cn 2BQ25poD056061 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63A9018B.002 by FangMail milter! X-FangMail-Envelope: 1672020363/4NgLjl6hXqz4xpyB/63A9018B.002/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63A9018B.002/4NgLjl6hXqz4xpyB X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9F81DC000C X-Stat-Signature: sk1myq1d9uwohcnsbs4wwxd7aomw9zdo X-HE-Tag: 1672020366-141372 X-HE-Meta: U2FsdGVkX1/tc9BIKiPuaU/Jxr0ov+6leu3ILAhNTshoVozeVYmPlyXuB6Bx20lHXfEIxA2D37oY44muwCAUoC/cKZFHx/9EYFRSyOMa/rT6aen/aDi18Qep7KMkK/yBPcEFzNRbjJ1VF3dXFQxpbCqOsCfUw1/373jD4NS4WUUZvGCIGIGmDHe2fYCbAiObq70l2dg/SQQShQo6U0kSqYny1LGEtsZwLr4UrHBqM4+HDbIl/Q2edC/61tluaZQh916KH6kYDCnNFMjMGekzqK8y0iTcaFMr9HW3xrVEjRc/oiRKdUVYi3XVR/LkEdbx0jATAxaVN3H7ll6PL84gK3W8Q2I9Z487J8XKN778yMRBu8BM8wrsPWq/mRmnb0locnsq3HFUajJi2gidcUPCQ3gj0UV2SuFncY3oNNSdfd5p8sMsS3CrY+T72fNnrqbsYHUgXrcKqGgwRb4LraHsJvLtD5ak3O9shChQ8dOXYV0xP5CrkiLGs1+HCfCf5HlEaMvZfHLXn/buCvk402yhxeEMiTUo0OqErZknBBE2r8D1rwkVMburkZ/AlJPDU66eHwFGzd+WG6weO7ZGV0lE+VfQ5l2i6A1P4nngiAYi/JrCmuJq8tt1Yx9iYnVcTOKgzl8PS4bx0LTwfTEEdQCYnT0re+f710LzIJ46oA5LL/CSS9jsf+78n0vP6IiDc2BKUso5PktN93TJv0j6T/NJcgTeL8a9mHYk2eE8zdaKbgeB7CnS/ZBvoNALChynfZlwy9QP/ZaMpo3AX56RtKsm2VATjUnxMycoh78VM+XBfuZWe645lJuI5iVxuIVb+HQP/AQJ2g0I2xiRMMqTI7zfmp6G8TkNmJ6FOCF/HdYK1mcykkCUhm82G56udKEHfN5Ki83G1eTC6WuXEx805PqtzvK58oYMIl9rLWmBYxdoPt8= 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 3b8475007734..9081cfe89d6f 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 e02cc89512cd..6eb85de7b69f 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 Mon Dec 26 02:07:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13081562 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 882A1C4167B for ; Mon, 26 Dec 2022 02:07:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 17882900004; Sun, 25 Dec 2022 21:07:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DAC9900002; Sun, 25 Dec 2022 21:07:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9811900004; Sun, 25 Dec 2022 21:07:32 -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 CD8BB900002 for ; Sun, 25 Dec 2022 21:07:32 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AA6E416021D for ; Mon, 26 Dec 2022 02:07:32 +0000 (UTC) X-FDA: 80282820744.26.1D767C9 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by imf27.hostedemail.com (Postfix) with ESMTP id 349C940008 for ; Mon, 26 Dec 2022 02:07:29 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf27.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=1672020450; 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=mwhAxcSI5lDMmsy0O+sDkb6+d9TGs8sPPVdmZJ/BhtSa7fUOaEdwDjAoF6eXiXIxIPkAHp rv8fr0z3hdahdxFSxUBAogPHKm8gUdWI3ONcdp+iKQdaZ6ojOdrVhF8t3lpUqcBoYGatSH 9jS5JvMLVD8C+dXUEDNp6RFSzSkP2po= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf27.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=1672020450; a=rsa-sha256; cv=none; b=VWvCyD+e7plugy2UcalJkUnfYcPuogg0uAE170l5idmzWg59UceJGhDOY6e3d42CGH0+Zh GIEriv0cZ3WO0Q/T/pNlWuXiBsw+IWnIl7bvgIL0NLLhACjMl/CL2rlzcEAMaBgxpSPW/0 d1bnvMd0tJu7PEvGDaZx3ZL0USAUJQs= 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 4NgLlM1bT9z4xyCV; Mon, 26 Dec 2022 10:07:27 +0800 (CST) Received: from szxlzmapp07.zte.com.cn ([10.5.230.251]) by mse-fl1.zte.com.cn with SMTP id 2BQ27BEr057135; Mon, 26 Dec 2022 10:07:11 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Mon, 26 Dec 2022 10:07:12 +0800 (CST) Date: Mon, 26 Dec 2022 10:07:12 +0800 (CST) X-Zmail-TransId: 2b0363a901d0ffffffffd587cfe8 X-Mailer: Zmail v1.0 Message-ID: <202212261007128590286@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v4_5/6=5D_ksm=3A_add_zero=5Fpages=5Fsharing_docu?= =?utf-8?q?mentation?= X-MAIL: mse-fl1.zte.com.cn 2BQ27BEr057135 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.251.13.novalocal with ID 63A901DF.000 by FangMail milter! X-FangMail-Envelope: 1672020447/4NgLlM1bT9z4xyCV/63A901DF.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63A901DF.000/4NgLlM1bT9z4xyCV X-Rspamd-Queue-Id: 349C940008 X-Stat-Signature: whq3escs3eh3ncha4zxjjijc1zmwbbgb X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1672020449-583171 X-HE-Meta: U2FsdGVkX18CzUuzIMcZy5XlsrgAgBQWPNr8t3JustvUL7YjujEN5u/w22UV3ELUkE7TI3qyfwvTfWlNgXTH0Qx+OTOitFHZ63QEW5KqgH4YSMwwJdoRaMAAFIXcg5ZRXwGH2gStjDdb/tvjNRXAgDg21hxX8wRj2Ehdp+jqCWf3g8frMjDByEj3jUJo5pOgaaPRZJm3GKvzcwnAo4MFa6W0hqtL7XD9HdrJX3vnpvSTIIzcRe57jAkl7VaYKgKeVdY7hWsHvECcX0svpevUV7C2+OrCHvHrA4KfxltUv3bSGYrsoKzrceQgN1Po4cqvHpeqRr0Oev+l7BFsMmr63oXZF2mJq1Ylmd6VRo/l3jn6HAPT08W2ddhKSAJN+5LYIV5JolQH0H8EBFh2QlrHuqe8Zy/Hk75yfC2FnSkXRP/gID2WxJh3up3t51tBN/GBCkFJTMBPor3AYnMnoQyn6CohoShlpfL3OfflRCMBInTjY2aKvGAFQdgsIqydHYvExBnwqtABVNa0B2omHwTl1C2GmEoaEjZC01kSLzszWXXyX1287MFICXrBmWnT3eEyru1RktwOJ5DGtJIXQSDsIlF2twQw4W65Q7yUiBrSD/pw6oHM6O3XWHTln5+j15c2BINRs6ST6xIxyxDSBdckzEhR2WDXoCdxPztS66S99x9FNmqUOo9HUq1eOtOCpRETJ71XWikWxxNw3B3/Fi59aX6bin7FDHbowE71JtbUl9tZ93zbij+kC+N5/ohh/bjK9VxT0xobdq7AKTBLO9oyasth2FTKDguzolINfsZoI0aCTyi/+2CJOG5ICBPjUgEK2myvXURCGg2e2jogewronYvFgZHIEXP1xne3mJ6AziNuWJD3EDjgDlwCs7Lkz2bcyNZcE1ln7irXaTxOrl9EppqyvNqwxmqlDZzieWkEXmUwP1uTJG1H6rGLzCx5tUzqF39KjrP2ALhG6ua+2Mw wu4mJ0f5 4Attc 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 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 Mon Dec 26 02:08:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13081563 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 0A5AAC4167B for ; Mon, 26 Dec 2022 02:09:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6809B940007; Sun, 25 Dec 2022 21:09:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 60905900002; Sun, 25 Dec 2022 21:09:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4843B940007; Sun, 25 Dec 2022 21:09:00 -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 35F67900002 for ; Sun, 25 Dec 2022 21:09:00 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C07461C568D for ; Mon, 26 Dec 2022 02:08:59 +0000 (UTC) X-FDA: 80282824398.02.E88699D Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by imf03.hostedemail.com (Postfix) with ESMTP id 1508D20002 for ; Mon, 26 Dec 2022 02:08:56 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf03.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=1672020538; 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=0QnV7mNMdH+8mcywUek8xu+xgd+KDTRWIfQMsoObDuU=; b=lpxjA0tXGyZyo+cuS11oS5tyKGywTO9gk1EKeLkzELI8GQ8CQfOzHiakFjzVXy8eoj9LyA r+9UP3ZRvfIBJP8cGvTIAlJbVEPt3JnvzrMqV8liN26bD7j/qlpfkR+Vg2A25hVz0GEhCU onSHcYhKJv8KkdfYVMtxRMhkoYdZf+g= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf03.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=1672020538; a=rsa-sha256; cv=none; b=6GTdu3d5eBV8bH12IMhqwVIWdz7JJ0RP5d1UoJTdgFShihMh8lTKU0QENNriC9JrRNrDgi XA+UO7r5JmIv297iXSyclMV8nzEJBchs44UK9EWgv+9C8O3dHU6AYjuZqgfJbNRN6VpovB VLVOKS1mKCVElxDE6cT7tQhpwKj54/o= 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 4NgLn06Lxpz8R041; Mon, 26 Dec 2022 10:08:52 +0800 (CST) Received: from szxlzmapp07.zte.com.cn ([10.5.230.251]) by mse-fl1.zte.com.cn with SMTP id 2BQ28cmB058499; Mon, 26 Dec 2022 10:08:38 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Mon, 26 Dec 2022 10:08:39 +0800 (CST) Date: Mon, 26 Dec 2022 10:08:39 +0800 (CST) X-Zmail-TransId: 2b0363a90227ffffffff9d783c60 X-Mailer: Zmail v1.0 Message-ID: <202212261008399750363@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v4_6/6=5D_selftest=3A_add_testing_unsharing_and_?= =?utf-8?q?counting_ksm_zero?= =?utf-8?q?_pages?= X-MAIL: mse-fl1.zte.com.cn 2BQ28cmB058499 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63A90234.000 by FangMail milter! X-FangMail-Envelope: 1672020532/4NgLn06Lxpz8R041/63A90234.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63A90234.000/4NgLn06Lxpz8R041 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 1508D20002 X-Stat-Signature: w7e59poyn1ufx5jx14kih1ujtud4nfta X-HE-Tag: 1672020536-929066 X-HE-Meta: U2FsdGVkX19xDDA+Ul6jxfUJm8KT9AXHmh25hlx7eXp1gsLq7PX23URVGl+mL2S1OjVLiI9IvLNLcvrgkhHtpbt1TMa14Ruc+DV8MFUAxQqvko7+lSor1ZhbZ1qasvGiXMIkzJBIOlp4ipAZ+7urT3jXFXYTGiSjw2yUVbIueCqVtmLWa/+7DxHUITkQDYKtR+pz4dY0VzxR3BXbwj+tuH+R/GDfbrtMqTvRCq0a9Aodmzbl8S4nWO2nlVDQ40f97mlzolBiBFLuU/9gqaosBa1ReLRSgrbWlt4QTfxzdJ0BeIRq0kqOQbPwltRNpvIWhF48sWWHCvKwBbkcF+EtqhCWRPAJ1ZpMfhHWHqIcQHRnKoLJxbM6h+XLS0D8Y82ml7H42Lmm5G4N5xt+GT/dJV2mJMF+Pea4ujialR5wqUbhvPk+dqMoPgUk/ObGnjfJQ72Ip1ParMsOreqUfo8VRycCoED03VtorbkVRqzySbMeDLzCd/ElXV/TjBvqdTpVMhcLl2gUJfAer46yi73TUHQh0EmUuMUIVN+GkgU4G6dpFrpOq0NJDI6r8BAkZCh2+iEHgo56NTu+esud0OjD2cCKhO1ZkqOaY5UV/KoLl/jFjnTTd4FZ1yUhMab0UU5xUTAqXgOtRI7CPvLYdPoHU8pSBshHe3fMlnolVlIs5wHmwkdNwZJQKF/0v0QiKnmU6iHw5Khtt/H8Ghn52C2xJc3388whlSQnVDuyTUvmrCX3ekWxFpluooNEbzUKOQO/wQqjX1NJRC61/xzvprKzUY5Fi/ONxpKbyMLAbEd4NGk8WChm4G2sidcVtIicnod5XTkLLSOwqWC0Kp/+9PyIDOmHq8rRHmQJ/GTHsFHenKz5gb6T00ZdHsoukAn9SSYE+WrPbgv/jwUbZuAmzvli4QtQue/5g9QcijQReA/hNcY= 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 --- tools/testing/selftests/vm/ksm_functional_tests.c | 105 ++++++++++++++++++++-- 1 file changed, 100 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/vm/ksm_functional_tests.c b/tools/testing/selftests/vm/ksm_functional_tests.c index b11b7e5115dc..2c50c14d42db 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,25 +88,50 @@ 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(); if (end_scans < 0) return end_scans; - } while (end_scans < start_scans + 2); + } while (end_scans < start_scans + 20); 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();