From patchwork Fri Dec 30 01:11:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13083995 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 4CA4FC3DA79 for ; Fri, 30 Dec 2022 01:11:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A42968E0002; Thu, 29 Dec 2022 20:11:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9F2448E0001; Thu, 29 Dec 2022 20:11:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E2058E0002; Thu, 29 Dec 2022 20:11:45 -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 7F1B18E0001 for ; Thu, 29 Dec 2022 20:11:45 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 51BBF1401DA for ; Fri, 30 Dec 2022 01:11:45 +0000 (UTC) X-FDA: 80297195370.09.86D09E2 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by imf19.hostedemail.com (Postfix) with ESMTP id E618F1A000F for ; Fri, 30 Dec 2022 01:11:41 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf19.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=1672362703; 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=c/wh2POJ+J+EwSvGAi/4q4fm1dKsarKtBYeUG5A5a+A=; b=YGExgOHdD7p5TPlzj1akWkY5kHqVm94fBmFNvVzKbUhh7nV9Dk70a77SBMzA+FWtvn/wIS 7kEQLUNhdvj0ctaGbuRp2PA7bR6zcxBpNQ5uiqf/oKbESrPASGSMY23T2R3sZ2RSGi9L0e 9OHhRMoR/0sCwZGK4YYWZlz5UkFGCSg= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=zte.com.cn; spf=pass (imf19.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=1672362703; a=rsa-sha256; cv=none; b=Yw7rvGkJiQBfGfcOWTJWynCxBKFeM3406fMtZWKdaw94qABeX3vVtxnJJ+LtrmweipPUiT GqENr8NSG1PsU3J4nAf3rkAZlkdibinQ8HdmKEmZiGaNgv4h/008/LqZl+UJ6dr0CzNozq 9X6P85qTckSdgmLkDgoJ21u82HCNlM4= 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 4NjnK42wzVz8QrkZ; Fri, 30 Dec 2022 09:11:36 +0800 (CST) Received: from szxlzmapp04.zte.com.cn ([10.5.231.166]) by mse-fl1.zte.com.cn with SMTP id 2BU1BVr1080628; Fri, 30 Dec 2022 09:11:31 +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:11:32 +0800 (CST) Date: Fri, 30 Dec 2022 09:11:32 +0800 (CST) X-Zmail-TransId: 2b0363ae3ac4ffffffffd3caddca X-Mailer: Zmail v1.0 Message-ID: <202212300911327101708@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v5_0/6=5D_ksm=3A_support_tracking_KSM-placed_zer?= =?utf-8?q?o-pages?= X-MAIL: mse-fl1.zte.com.cn 2BU1BVr1080628 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63AE3AC8.000 by FangMail milter! X-FangMail-Envelope: 1672362696/4NjnK42wzVz8QrkZ/63AE3AC8.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63AE3AC8.000/4NjnK42wzVz8QrkZ X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E618F1A000F X-Rspam-User: X-Stat-Signature: bc5a45bzi5ybq3dunos4himq1bfnd3t1 X-HE-Tag: 1672362701-38311 X-HE-Meta: U2FsdGVkX1+9I8uMvNDza2R26VQTbYboJHWFgNHNEASgmu57Hbf41jHSBNpVmfXo+9CG4dyljyFrVcvspQ4uNe6+0yQN9W+LCrqSbBzC/8UW9oWOBjzOe0fa59LFWjayN8ukG+euMCaNrkINSvJKtyBhB/x8AoBT2hORduEW4rsgpwisoyRwNPKrf722r9TLT7yDFe2K7TdqNcS0K+Ixj/Nr/uwKsJOkpXpQA9C8iwsACHM1IY+oq+b/6V9Q0WNh/NqUAIZro2RyFFvWsKAVmkjH0L0kkFgTDjmaqZxeYiJvOMTIH/BDwjw0ovGZ9uZMN6kTxfdgaw13jvU3zuKOlGaIzioZvun1pHKEjKCzFg7gA6ibOHRz5nR9EjFXqe8KtL+BYKQQJ1KmPgSN+L/h/U9LFwiUYBwPsSVo414f/SAQJbKirrK6L+YxauLGhI/S3cgK76QWwvg6kl9YHTds8BaLMqn4yhuyGKPIJm1S5YOyPWwgSPZCRGY052sT8fVQm6WwVFwmsHKTJlQ/HeBTHcr+HBq/dDEqBXn9m0d6yl1SNcPdYl+XaNKnjAwxKeG0j1lWuvfAxjFUlNfHHH+6CMo6xTlRgpG8LCp5h/EKbkJGzStCyFvMBGpoa7a8R1Kk2GUvZicDcyqXt5E+fslgD73EIdSisa5tcBQl3o3/ZV059UOcXZcJlNysUR8KM/7dxHCCs60CNLcgpIZJZ716eu7KC648kQQRFthM3JUVym3NmFogAMCLVwZqH71ncDiuQR1Hw4pTnI+oUaV3jAJhjWbBjlCBKgrJ930rCg9oy5PpMTFkUUEeju8+4ZEMhGV8bPl8Ks2D/ZouvV1QBZfe+sZtvk41okguSpPvonmbvdGgSZ2WxcI0nrbYKdGzsrNQIOoOP1KvQJG72KY/AmyXwGm7HpxSvWN61Tcjtl5Aeho= 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 The core idea of this patch set is to enable users to perceive the number of any pages merged by KSM, regardless of whether use_zero_page switch has been turned on, so that users can know how much free memory increase is really due to their madvise(MERGEABLE) actions. But the problem is, when enabling use_zero_pages, all empty pages will be merged with kernel zero pages instead of with each other as use_zero_pages is disabled, and then these zero-pages are no longer monitored by KSM. The motivations for me to do this contains three points: 1) MADV_UNMERGEABLE and other ways to trigger unsharing will *not* unshare the shared zeropage as placed by KSM (which is against the MADV_UNMERGEABLE documentation at least); see the link: https://lore.kernel.org/lkml/4a3daba6-18f9-d252-697c-197f65578c44@redhat.com/ 2) We cannot know how many pages are zero pages placed by KSM when enabling use_zero_pages, which hides the critical information about how much actual memory are really saved by KSM. Knowing how many ksm-placed zero pages are helpful for user to use the policy of madvise (MERGEABLE) better because they can see the actual profit brought by KSM. 3) The zero pages placed-by KSM are different from those initial empty page (filled with zeros) which are never touched by applications. The former is active-merged by KSM while the later have never consume actual memory. use_zero_pages is useful, not only 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. So we hope to implement the support for ksm zero page tracking without affecting the feature of use_zero_pages. Zero pages may be the most common merged pages in actual environment(not only VM but also including other application like containers). Enabling use_zero_pages in the environment with plenty of empty pages(full of zeros) will be very useful. Users and app developer can also benefit from knowing the proportion of zero pages in all merged pages to optimize applications. With the patch series, we can both unshare zero-pages(KSM-placed) accurately and count ksm zero pages with enabling use_zero_pages. v4->v5: --- 1) fix warning: mm/ksm.c:3238:9: warning: no previous prototype for 'zero_pages_sharing_show' [-Wmissing-prototypes]. In [PATCH 3/6] (ksm: count all zero pages placed by KSM), declare the function ssize_t zero_pages_sharing_show(struct kobject *kobj...) as 'static'. 2) In [PATCH 6/6],fix error of "} while (end_scans < start_scans + 20);" to "} while (end_scans < start_scans + 2);" in wait_two_full_scans(). --- v3->v4: 1) The patch series are readjusted to adapt to these recent changes of break_ksm() from David Hildenbrand's commits: https://lore.kernel.org/all/20221021101141.84170-9-david@redhat.com/T/#u 2) Some changes of patch itself: In [patch 2/6], add a check of mm exiting in unshare_zero_pages in case of unsharing the zero pages whose process is exiting; form a new function clean_rmap_item_zero_flag(), and add it after stable_tree_search() to fix; In [patch 3/6], all decreasing actions of zero pages count are put in clean_rmap_item_zero_flag(), which is more accurate. 3) Add a selftest of unsharing and counting ksm-placed zero pages. --- v2->v3: 1) Add more descriptive information in cover letter. 2) In [patch 2/5], add more commit log for explaining reasons. 3) In [patch 2/5], fix misuse of break_ksm() in unmerge_ksm_pages(): break_ksm(vma, addr, NULL) -> break_ksm(vma, addr, false); --- v1->v2: [patch 4/5] fix build warning, mm/ksm.c:550, misleading indentation; statement 'rmap_item->mm->ksm_zero_pages_sharing--;' is not part of the previous 'if'. *** BLURB HERE *** xu xin (6): ksm: abstract the function try_to_get_old_rmap_item ksm: support unsharing zero pages placed by KSM ksm: count all zero pages placed by KSM ksm: count zero pages for each process ksm: add zero_pages_sharing documentation selftest: add testing unsharing and counting ksm zero page Documentation/admin-guide/mm/ksm.rst | 7 + fs/proc/base.c | 1 + include/linux/mm_types.h | 7 +- mm/ksm.c | 183 +++++++++++++++++----- tools/testing/selftests/vm/ksm_functional_tests.c | 105 ++++++++++++- 5 files changed, 261 insertions(+), 42 deletions(-)