From patchwork Fri Aug 11 16:28:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13351126 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 B1D48C001B0 for ; Fri, 11 Aug 2023 16:28:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B1216B0078; Fri, 11 Aug 2023 12:28:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 461606B007B; Fri, 11 Aug 2023 12:28:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 350886B007D; Fri, 11 Aug 2023 12:28:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 26D996B0078 for ; Fri, 11 Aug 2023 12:28:22 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id DCAE1A0682 for ; Fri, 11 Aug 2023 16:28:21 +0000 (UTC) X-FDA: 81112356402.10.8C500C8 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by imf25.hostedemail.com (Postfix) with ESMTP id 02B67A0010 for ; Fri, 11 Aug 2023 16:28:19 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=neutral (imf25.hostedemail.com: 66.220.144.179 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=1691771300; 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=NdKa6UdtqrZpOMMOh/nJA+hAWi6NPydLoCg/ottJ1KY=; b=PutsJtyDhq98TNhAtxyaSTLnnCw0hdfSmNZrKR4zAmw1LlORbT+LHjrCHPFh0L9z20URGD vG56hQEHPI9rNHE4Y8r6dVJlQ52Ex1LjktdkOKIlEoCTPbo4qBok3VFuHq9ldeFU3ukAIz eS4d2w1UCD34ki2vU8dYPZKjLiV3DdU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=neutral (imf25.hostedemail.com: 66.220.144.179 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=1691771300; a=rsa-sha256; cv=none; b=5KtyGjGyth1kxoS0hA1apmbnf8Ex1fVje3I3ctEegsMNaVhCjaHwx/KMyw4Fs65v7E1IOs jTAyW0hks/h0wbBFSxld01pYnwmifwsy7PwCQNPL+9LAC2BLDk7rvbJ5hpzp3oh0J91t5Q XLUUvE3QlmOMt3Xoya/j7O5IJ/TRPXU= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id DC108A076222; Fri, 11 Aug 2023 09:28:06 -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 v2] proc/ksm: add ksm stats to /proc/pid/smaps Date: Fri, 11 Aug 2023 09:28:03 -0700 Message-Id: <20230811162803.1361989-1-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Rspamd-Queue-Id: 02B67A0010 X-Rspam-User: X-Stat-Signature: ytoj4smtaxeeh5r3pomh1sfq8u5j4iq1 X-Rspamd-Server: rspam01 X-HE-Tag: 1691771299-545113 X-HE-Meta: U2FsdGVkX18C5/GbjglsOBWov1D90xPrdB0ggplh90hEAoOET/xe86FK+bUoiQ/ImmSx9+2NqpFCQTLZ1Prk0XAl7FwwqclEPytVXWuSVGQQqQCFLgCY7sMqie7PVV+0EgYBN305wkEP4WbISjbTnUGpMrFeVaCrpcOsCTzccuRBwJarI7kU4UYgiauw8lxlYO55fZI6Hti5/MqoXw+swROmBKKlOEf941BYEfgzMI05BrA5BX9mB02QlqPxAgzp+5TAvy+A2guyWchgVUL0ER/65wLs1OgNL4Cc286qWL1TXI1pAoMz5TRHom+pWiQIEhKJvOMw/I71gVEuLiQ7Faf3eyY67X0nvkc3XYRU7kSrulUk5HpA7uOSeEW7uu/Yh3JaMbV7nQboh6JA4NXWdjkBC8CpoPxC+NAn3tcy5otgZZ8TZ8rhpAEav48S3iuqAK2W9XMHfePmnQ8QjzS+K1Xx13ZduzfDYkTV9HZjd0ksK9jDIxpATxBbW5stzptpy2DeDIPalOMnN6ZbSxzZechmUhua6yRFi28N9T3sxZc3KUdmby+z0r2ThyTxVNEjJKFLXUrY+hv/f1/GWY7bWC6kBU3x7Q/adRcwNOo2er5jNRdxhJatv6L9VOV06Ra+L7TIRspYZ8MuUcyHkfmfkzlFPCwGrD859cqLQw0lukDosvgiy6jdq1s0NXlgob7Uy7hvdv2+jSrRATx1SeWQa/0DBqw1cDrMpaqieux5Z6hP4/plSGUjz9thilLAf45xk+kBVCJD0R8x0B2us9CC9GG+GuOPMfIDrK7KQkloQj9SJKWLQ6qHXd9PNpRtPXvoMNSOax6iJuSlXmo05hXakq51m1AMPm2GIZ3GgRa8vcICYRuzXxnTA5gSaMfLAKPNLLRgYjBrpFw2jqG4l1ht6o1DBSBZhuogrUmBPp+LIp/gZv6QaMbu9N5f9yzmoQbEkFvqo6QFwMeqT3gZ3g5 u4t2xRTK DwT2YnDVnlHvs+54= 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. 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 | 3 +++ fs/proc/task_mmu.c | 5 +++++ 2 files changed, 8 insertions(+) base-commit: f4a280e5bb4a764a75d3215b61bc0f02b4c26417 diff --git a/Documentation/filesystems/proc.rst b/Documentation/filesystems/proc.rst index 7897a7dafcbc..4ef3c0bbf16a 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,8 @@ 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. + "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..f591c750ffda 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -396,6 +396,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; @@ -452,6 +453,9 @@ static void smaps_account(struct mem_size_stats *mss, struct page *page, mss->lazyfree += size; } + if (PageKsm(page)) + mss->ksm += size; + mss->resident += size; /* Accumulate the size in pages that have been accessed. */ if (young || page_is_young(page) || PageReferenced(page)) @@ -822,6 +826,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);