From patchwork Fri Feb 10 01:15:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 5206 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp680828wrn; Thu, 9 Feb 2023 17:21:15 -0800 (PST) X-Google-Smtp-Source: AK7set/hRHrN5W3ugsZH6X8S0hw22fGVrABlOwbd9Lz09pT+X1hlHV7dlawDJMVRdZBkHQ/XZpg3 X-Received: by 2002:a17:907:7213:b0:8ac:8705:c497 with SMTP id dr19-20020a170907721300b008ac8705c497mr11756300ejc.57.1675992075695; Thu, 09 Feb 2023 17:21:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675992075; cv=none; d=google.com; s=arc-20160816; b=k3rnq7pj8evs8K4s/tff9eW1k89kUNvO36YZKIwmOF3ZS7CRIydg/DdqR4S+uLR7Zq zDw52mEIINo0lU+m3ud4kC4e8N6Toj583JFhZRbPusqTzfgumaAbdo9PZhEUGKuQ11K6 EWL5xghUAnAlmZ5H9Q1VMS//C6cmR2MzHOIfQ17GlbtZ2xrkJD7W2o5ZObkQR+1jcNsm Yw7aSZqe07lSQCf0pO21v7JP2+JCI/I58rVRDLP4AAXY2SfQEINsc5F3/FdT1tq0coPr uCSg4fVq768LslMYzmbsaeJNPOSljYKgxcnOAAVeLSMjAhN0ESFc9JwrFsiZtBg6zPk1 CE4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:mime-version:message-id:date; bh=ZRCWa5VcbtTMd93vzJE230tM5ULf6+8N5VXoD1UdbY8=; b=oBFt51C5nsEKzzHz2OmJycKoa0D5HkfzPVp78SQBTxjF/UIyeemmp1AVk8OtI1ish/ LWb+3Ab5BQ0ZM+JIwvFRL+DYnyhuhDLcgdKeMnhRUpvvmdhWhAL2ysF4S2XPDECcKRdh UkqeZV/dKz5IlF/9nWlqkufOgnhq4Qi7Q0mKKvRhrKKQ1auaja24nWafaAdIWZ1K8IO9 a6pzNvuns1k29JvI4Dm8VXr1XHr+rc6uXm/TCF4upESh/ciu8Tfuh30wNJ2yJmzS/2SL mfuO5aRqeAVaYKC+yz1Ml6Gh1OORBP4mE/dFbej3bOvYkOGArJ2TmLBwstQXrrSkun1Z YlPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id um38-20020a170907cb2600b0088eb55ed9d4si3746807ejc.689.2023.02.09.17.20.52; Thu, 09 Feb 2023 17:21:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231199AbjBJBPd (ORCPT + 99 others); Thu, 9 Feb 2023 20:15:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbjBJBPb (ORCPT ); Thu, 9 Feb 2023 20:15:31 -0500 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1481C5EA07 for ; Thu, 9 Feb 2023 17:15:29 -0800 (PST) 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 4PCbQ81jJRz501Qm; Fri, 10 Feb 2023 09:15:28 +0800 (CST) Received: from szxlzmapp01.zte.com.cn ([10.5.231.85]) by mse-fl1.zte.com.cn with SMTP id 31A1FLFN006412; Fri, 10 Feb 2023 09:15:21 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Fri, 10 Feb 2023 09:15:22 +0800 (CST) Date: Fri, 10 Feb 2023 09:15:22 +0800 (CST) X-Zmail-TransId: 2b0363e59aaafffffffff5e3bbcc X-Mailer: Zmail v1.0 Message-ID: <202302100915227721315@zte.com.cn> Mime-Version: 1.0 From: To: Cc: , , , , , , , , Subject: =?utf-8?q?=5BPATCH_v6_0/6=5D_ksm=3A_support_tracking_KSM-placed_zer?= =?utf-8?q?o-pages?= X-MAIL: mse-fl1.zte.com.cn 31A1FLFN006412 X-Fangmail-Gw-Spam-Type: 0 X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63E59AB0.000/4PCbQ81jJRz501Qm X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,SORTED_RECIPS, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757405066623354349?= X-GMAIL-MSGID: =?utf-8?q?1757405066623354349?= 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. v5->v6: 1) In [PATCH 1/6], fix some coments as Divid's suggestions [1]. 2) In [PATCH 6/6], modify the patch as Divid's suggestions [2]. [1]: https://lore.kernel.org/lkml/0e0c90a2-d12c-f965-9cce-ecd5d28c09dd@redhat.com/ [2]: https://lore.kernel.org/lkml/3704dcf0-bd0a-8ab2-7f4f-045fc7c73171@redhat.com/ 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 | 185 +++++++++++++++++----- tools/testing/selftests/vm/ksm_functional_tests.c | 96 ++++++++++- 5 files changed, 255 insertions(+), 41 deletions(-)