From patchwork Tue Aug 22 18:05:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13361282 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 E62DAEE4993 for ; Tue, 22 Aug 2023 18:06:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75131280057; Tue, 22 Aug 2023 14:06:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6DA22280056; Tue, 22 Aug 2023 14:06:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57AF9280057; Tue, 22 Aug 2023 14:06:02 -0400 (EDT) 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 431C7280056 for ; Tue, 22 Aug 2023 14:06:02 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 063291C9365 for ; Tue, 22 Aug 2023 18:06:02 +0000 (UTC) X-FDA: 81152519364.29.D28181F Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf13.hostedemail.com (Postfix) with ESMTP id 49D4720019 for ; Tue, 22 Aug 2023 18:06:00 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692727560; a=rsa-sha256; cv=none; b=29JZgocBsTQ0dXF88D7kRZmy8W+caoO1KCunN0A0FLIykCrkheFRNZr7CNUNaRnrkx1cgI YUkSPJ93wqJQ/MdJDLsYx9qFxz+IiZELMdZBK7w0kdKFUoIFGCDtBRiyWtACxmJOD/b+8D /Clj/3jPC1+7KYLbaX1l5072pdLGG9I= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=neutral (imf13.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1692727560; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=a3cfI3u8igSCLqtS1ByRMhcywb8vAKEdPaFBENqDSFs=; b=I7C2p4NfrI+lo59sNBQ4wyfr0B8HaInua3iKr/SLfG2t8JAd/BCzP8jNkAehPOqA9b63aL PofYolHZgld1ZfhhRaXePegAuPBBb5MFCGXxpqCf8sXW05KgrVLf6tUwE+3XXrGCaUTEiV 1+8zmls5kwOzgKZungIjcSST0jqrnrw= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id D6D9DA9FA88E; Tue, 22 Aug 2023 11:05:42 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, linux-fsdevel@vger.kernel.org, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4] proc/ksm: add ksm stats to /proc/pid/smaps Date: Tue, 22 Aug 2023 11:05:39 -0700 Message-Id: <20230822180539.1424843-1-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 49D4720019 X-Stat-Signature: tm1kjgsff87rippmaf5xkbs14mkphx75 X-HE-Tag: 1692727560-496523 X-HE-Meta: U2FsdGVkX19GPQW8BUD/y6GfHLGx4IRtG9bw+KGKHsO/+oolJp0fygp8FIoW1TJPePSVHc017uiMzJHMQp/hkT9aIYapl9eB912NIIDcp03z+cfstilGxbetVKIJp5XeGALGL5kdCiduLT1AbW1cO1UX2kfIu/yh+j8j53lcbMb3poQ7HWrLzlidjWtAUyu3oy7FBPWSO6KCWxxswuyd2VD4y92/R2DJNO1nSLyMW84QkyYLLuXet7bUBxQ4zxaIMYJFHZsk8OZcZZFlKQI5CpfMQjPDDPAE9XbS71D44Hb+AbS5u/49Vcl/NAgytTQycijSbKfzBKx1q3Y08QdzUC0SfEqrF7GPUNUUDlOX8h4qMpy9YClXxouwPVGKX0SvPQ+KBl/kt9nQO1yPpP9UMnwujuVF5sk1u2qnKuouAz8wFGBjv7ij6HtlT95sW4W2hUxKeSEsSQUIdCna5aqs6xntQjpqrLON9QR176QfvgKi5YJBVomacjpoRNIflQk5SxjXgxfRBODraXn07bJQKGKwGD6eiyUvhu0dmruiSeICKuFqDQWPnaIiKkdP/orjSdTqjU9q6EGm+uS556iyVX7DgHzQKw73sNNT5+0aoOY9Z36RPc8DnIF0fas9LCFXFv+D1w0QGBDqI7l/pFoVD9y41PuEritWm0uYjcUE3J/1uk64X5nD5Zp9OV8pERmIkuP8bqm0eywnXvRMbwHCkCmcoc4B0Cjn5LWpiJP0fUVtz3W0/TFPeboi//CPXGnmwDOTB+re07BLB0//B9o9WwhDFh5H9hrElmc9rKPZAaaoNeFqtoXZWbt0GA/VcEoxcB727RuMHKO431IQTYuY+h+oz3S0LMBeJ+gv5lj93pgpb3FF9U/2Jm/T/ZHJzIZlYxUS3ozE96DkGJb3j542zMAyPHy40yuvYaTyQXSiE9+IfUP2BN43RBs26gNO533JCUV1UHTcYB0jf0oe0Su FB8T52hd pTfCh4O70ChTPjLzE9xjqLrxRAYklGc9fEoyL 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: With madvise and prctl KSM can be enabled for different VMA's. Once it is enabled we can query how effective KSM is overall. However we cannot easily query if an individual VMA benefits from KSM. This commit adds a KSM section to the /prod//smaps file. It reports how many of the pages are KSM pages. The returned value for KSM is independent of the use of the shared zeropage. Here is a typical output: 7f420a000000-7f421a000000 rw-p 00000000 00:00 0 Size: 262144 kB KernelPageSize: 4 kB MMUPageSize: 4 kB Rss: 51212 kB Pss: 8276 kB Shared_Clean: 172 kB Shared_Dirty: 42996 kB Private_Clean: 196 kB Private_Dirty: 7848 kB Referenced: 15388 kB Anonymous: 51212 kB KSM: 41376 kB LazyFree: 0 kB AnonHugePages: 0 kB ShmemPmdMapped: 0 kB FilePmdMapped: 0 kB Shared_Hugetlb: 0 kB Private_Hugetlb: 0 kB Swap: 202016 kB SwapPss: 3882 kB Locked: 0 kB THPeligible: 0 ProtectionKey: 0 ksm_state: 0 ksm_skip_base: 0 ksm_skip_count: 0 VmFlags: rd wr mr mw me nr mg anon This information also helps with the following workflow: - First enable KSM for all the VMA's of a process with prctl. - Then analyze with the above smaps report which VMA's benefit the most - Change the application (if possible) to add the corresponding madvise calls for the VMA's that benefit the most Signed-off-by: Stefan Roesch --- Documentation/filesystems/proc.rst | 4 ++++ fs/proc/task_mmu.c | 16 +++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) base-commit: f4a280e5bb4a764a75d3215b61bc0f02b4c26417 diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index 7897a7dafcbc..d5bdfd59f5b0 100644 --- a/Documentation/filesystems/proc.rst +++ b/Documentation/filesystems/proc.rst @@ -461,6 +461,7 @@ Memory Area, or VMA) there is a series of lines such as the following:: Private_Dirty: 0 kB Referenced: 892 kB Anonymous: 0 kB + KSM: 0 kB LazyFree: 0 kB AnonHugePages: 0 kB ShmemPmdMapped: 0 kB @@ -501,6 +502,9 @@ accessed. a mapping associated with a file may contain anonymous pages: when MAP_PRIVATE and a page is modified, the file page is replaced by a private anonymous copy. +"KSM" shows the amount of anonymous memory that has been de-duplicated. The +value is independent of the use of shared zeropage. + "LazyFree" shows the amount of memory which is marked by madvise(MADV_FREE). The memory isn't freed immediately with madvise(). It's freed in memory pressure if the memory is clean. Please note that the printed value might diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 51315133cdc2..4532caa8011c 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -396,6 +397,7 @@ struct mem_size_stats { unsigned long swap; unsigned long shared_hugetlb; unsigned long private_hugetlb; + unsigned long ksm; u64 pss; u64 pss_anon; u64 pss_file; @@ -435,9 +437,9 @@ static void smaps_page_accumulate(struct mem_size_stats *mss, } } -static void smaps_account(struct mem_size_stats *mss, struct page *page, - bool compound, bool young, bool dirty, bool locked, - bool migration) +static void smaps_account(struct mem_size_stats *mss, pte_t *pte, + struct page *page, bool compound, bool young, bool dirty, + bool locked, bool migration) { int i, nr = compound ? compound_nr(page) : 1; unsigned long size = nr * PAGE_SIZE; @@ -452,6 +454,9 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page, mss->lazyfree += size; } + if (PageKsm(page) && (!pte || !is_ksm_zero_pte(*pte))) + mss->ksm += size; + mss->resident += size; /* Accumulate the size in pages that have been accessed. */ if (young || page_is_young(page) || PageReferenced(page)) @@ -557,7 +562,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr, if (!page) return; - smaps_account(mss, page, false, young, dirty, locked, migration); + smaps_account(mss, pte, page, false, young, dirty, locked, migration); } #ifdef CONFIG_TRANSPARENT_HUGEPAGE @@ -591,7 +596,7 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr, else mss->file_thp += HPAGE_PMD_SIZE; - smaps_account(mss, page, true, pmd_young(*pmd), pmd_dirty(*pmd), + smaps_account(mss, NULL, page, true, pmd_young(*pmd), pmd_dirty(*pmd), locked, migration); } #else @@ -822,6 +827,7 @@ static void __show_smap(struct seq_file *m, const struct mem_size_stats *mss, SEQ_PUT_DEC(" kB\nPrivate_Dirty: ", mss->private_dirty); SEQ_PUT_DEC(" kB\nReferenced: ", mss->referenced); SEQ_PUT_DEC(" kB\nAnonymous: ", mss->anonymous); + SEQ_PUT_DEC(" kB\nKSM: ", mss->ksm); SEQ_PUT_DEC(" kB\nLazyFree: ", mss->lazyfree); SEQ_PUT_DEC(" kB\nAnonHugePages: ", mss->anonymous_thp); SEQ_PUT_DEC(" kB\nShmemPmdMapped: ", mss->shmem_thp);