From patchwork Tue Sep 15 05:58:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11775601 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A140F618 for ; Tue, 15 Sep 2020 05:59:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0E3392080C for ; Tue, 15 Sep 2020 05:59:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="Kmp43hyA" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0E3392080C Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9A21B900007; Tue, 15 Sep 2020 01:59:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 9554C900002; Tue, 15 Sep 2020 01:59:56 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 842AD900007; Tue, 15 Sep 2020 01:59:56 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0123.hostedemail.com [216.40.44.123]) by kanga.kvack.org (Postfix) with ESMTP id 6D290900002 for ; Tue, 15 Sep 2020 01:59:56 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 2DD65180AD815 for ; Tue, 15 Sep 2020 05:59:56 +0000 (UTC) X-FDA: 77264244792.27.ants27_300b25a2710e Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin27.hostedemail.com (Postfix) with ESMTP id 0022B3D669 for ; Tue, 15 Sep 2020 05:59:55 +0000 (UTC) X-Spam-Summary: 1,0,0,690902e8d390e465,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:1:2:41:355:379:541:800:960:968:973:988:989:1260:1311:1314:1345:1437:1500:1515:1605:1730:1747:1777:1792:1801:1963:2198:2199:2393:2553:2559:2562:2693:2892:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3874:4050:4250:4605:5007:6119:6261:6653:7903:8603:9010:9040:10004:11026:11473:11658:11914:12043:12048:12219:12291:12296:12297:12438:12517:12519:12555:12895:13869:13894:14394:21063:21080:21324:21444:21451:21611:21627:21740:21939:21990:30005:30029:30045:30054:30074:30075:30090,0,RBL:209.85.214.193:@bytedance.com:.lbl8.mailshell.net-66.100.201.201 62.2.0.100;04yf5h5qh1zq47sn75pnpj8wuwwoeopq71cwp6wp7ymrgekrw59yje93on9qffb.xybot4hkfkb71kd6wi7yybkq9x9ozq6k5xwup6mko5s66f87i9txz9jetu54p6z.s-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: ants27_300b25a2710e X-Filterd-Recvd-Size: 10187 Received: from mail-pl1-f193.google.com (mail-pl1-f193.google.com [209.85.214.193]) by imf30.hostedemail.com (Postfix) with ESMTP for ; Tue, 15 Sep 2020 05:59:55 +0000 (UTC) Received: by mail-pl1-f193.google.com with SMTP id e4so768102pln.10 for ; Mon, 14 Sep 2020 22:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=gFDf/CtGOuA4yHmXsalFNqBAU9vWFrb7HtZgVcDaWLM=; b=Kmp43hyAocL+y8Gi71UVrt9eZ6Aid1KIhzkcPEZIT9WKJJTJ1ZUhgWKxungq3gMN6T uYegHsECB5NgkmN28c+6oZselCdqg1M8EpotGjdLARK2NoYQ1rOxoFwv6jmwM4CPgfGL JJse09B+nZVKsK7yV/zW80ZWoiRyzZfzYnN54qNSdYSskbkvQDHmyegpcezqIv8mWb+p HIq5oZTWqCA+snkukbJo60Z5N5FxCBrmAHRVMLoy9r7QIcJ60E8Gpt8Yd7E++SOSGdrX luZJPBzyrXPyDpvnSPFyJdFVgvVWuVvEkVA15Z+7F85jcbRTZb95DSLjHxh4IDYTbbPe i+2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=gFDf/CtGOuA4yHmXsalFNqBAU9vWFrb7HtZgVcDaWLM=; b=DF2BcUW6MSTaWg6PiFmKhyzV4fZRiyt1FkDglbHq4mDHLXfSK5YlokjG1r/Sti5o8i QkXI1gw3RSn0y3OK2naT0aXKKtr31ng/OSkKojgc5h9o77ro3skZhtQ2HAkVxIllAP8R CHQwbI4HbCGR6GMNenFhVCQpTDkuD91U0A1KtpZgUFSnm1feQlUv/IhChmk/7uaHEfRA RUt6tWEQIZ5UvWR+D6PM3c689z3NBjOHQa8uqCZ8Cx15epQ+KGRm4CNfizzqjTCeemQB vdHy70Wp2VCNow1/MtNrRsqn98AzWlbB7KH3yTWo7yF+wgyUGu6cRp8cV5Wah8ylkBOP z0Fw== X-Gm-Message-State: AOAM530cf/rXh9VW5o2tVRQCgfeqApC/1H7//igUgkTLD25enlGl0nFp x1SBTzhtsfo4Gu0vuZ2ZPHTYtQ== X-Google-Smtp-Source: ABdhPJxDA2E3SfO/j1kH3y8s0o5yMhX3MJKfRGBKKnDr9f8L/5G9xncXE1kuBELZp+104qB+5+cTTw== X-Received: by 2002:a17:90a:5282:: with SMTP id w2mr2757843pjh.77.1600149594263; Mon, 14 Sep 2020 22:59:54 -0700 (PDT) Received: from localhost.bytedance.net ([103.136.220.66]) by smtp.gmail.com with ESMTPSA id a13sm9942198pgq.41.2020.09.14.22.59.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 14 Sep 2020 22:59:53 -0700 (PDT) From: Muchun Song To: tj@kernel.org, lizefan@huawei.com, hannes@cmpxchg.org, corbet@lwn.net, mhocko@kernel.org, vdavydov.dev@gmail.com, akpm@linux-foundation.org, shakeelb@google.com, guro@fb.com, rdunlap@infradead.org Cc: cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Muchun Song Subject: [PATCH v4] mm: memcontrol: Add the missing numa_stat interface for cgroup v2 Date: Tue, 15 Sep 2020 13:58:25 +0800 Message-Id: <20200915055825.5279-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Queue-Id: 0022B3D669 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 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: In the cgroup v1, we have a numa_stat interface. This is useful for providing visibility into the numa locality information within an memcg since the pages are allowed to be allocated from any physical node. One of the use cases is evaluating application performance by combining this information with the application's CPU allocation. But the cgroup v2 does not. So this patch adds the missing information. Signed-off-by: Muchun Song Suggested-by: Shakeel Butt Reviewed-by: Shakeel Butt --- changelog in v4: 1. Fix some document problems pointed out by Randy Dunlap. 2. Remove memory_numa_stat_format() suggested by Shakeel Butt. changelog in v3: 1. Fix compiler error on powerpc architecture reported by kernel test robot. 2. Fix a typo from "anno" to "anon". changelog in v2: 1. Add memory.numa_stat interface in cgroup v2. Documentation/admin-guide/cgroup-v2.rst | 72 +++++++++++++++++++++ mm/memcontrol.c | 86 +++++++++++++++++++++++++ 2 files changed, 158 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 6be43781ec7f..bcb7b202e88d 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1368,6 +1368,78 @@ PAGE_SIZE multiple when read back. collapsing an existing range of pages. This counter is not present when CONFIG_TRANSPARENT_HUGEPAGE is not set. + memory.numa_stat + A read-only flat-keyed file which exists on non-root cgroups. + + This breaks down the cgroup's memory footprint into different + types of memory, type-specific details, and other information + per node on the state of the memory management system. + + This is useful for providing visibility into the NUMA locality + information within an memcg since the pages are allowed to be + allocated from any physical node. One of the use cases is evaluating + application performance by combining this information with the + application's CPU allocation. + + All memory amounts are in bytes. + + The output format of memory.numa_stat is:: + + type N0= N1= ... + + The entries are ordered to be human readable, and new entries + can show up in the middle. Don't rely on items remaining in a + fixed position; use the keys to look up specific values! + + anon + Amount of memory per node used in anonymous mappings such + as brk(), sbrk(), and mmap(MAP_ANONYMOUS). + + file + Amount of memory per node used to cache filesystem data, + including tmpfs and shared memory. + + kernel_stack + Amount of memory per node allocated to kernel stacks. + + shmem + Amount of cached filesystem data per node that is swap-backed, + such as tmpfs, shm segments, shared anonymous mmap()s. + + file_mapped + Amount of cached filesystem data per node mapped with mmap(). + + file_dirty + Amount of cached filesystem data per node that was modified but + not yet written back to disk. + + file_writeback + Amount of cached filesystem data per node that was modified and + is currently being written back to disk. + + anon_thp + Amount of memory per node used in anonymous mappings backed by + transparent hugepages. + + inactive_anon, active_anon, inactive_file, active_file, unevictable + Amount of memory, swap-backed and filesystem-backed, + per node on the internal memory management lists used + by the page reclaim algorithm. + + As these represent internal list state (e.g. shmem pages are on + anon memory management lists), inactive_foo + active_foo may not + be equal to the value for the foo counter, since the foo counter + is type-based, not list-based. + + slab_reclaimable + Amount of memory per node used for storing in-kernel data + structures which might be reclaimed, such as dentries and + inodes. + + slab_unreclaimable + Amount of memory per node used for storing in-kernel data + structures which cannot be reclaimed on memory pressure. + memory.swap.current A read-only single value file which exists on non-root cgroups. diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 75cd1a1e66c8..ff919ef3b57b 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6425,6 +6425,86 @@ static int memory_stat_show(struct seq_file *m, void *v) return 0; } +#ifdef CONFIG_NUMA +struct numa_stat { + const char *name; + unsigned int ratio; + enum node_stat_item idx; +}; + +static struct numa_stat numa_stats[] = { + { "anon", PAGE_SIZE, NR_ANON_MAPPED }, + { "file", PAGE_SIZE, NR_FILE_PAGES }, + { "kernel_stack", 1024, NR_KERNEL_STACK_KB }, + { "shmem", PAGE_SIZE, NR_SHMEM }, + { "file_mapped", PAGE_SIZE, NR_FILE_MAPPED }, + { "file_dirty", PAGE_SIZE, NR_FILE_DIRTY }, + { "file_writeback", PAGE_SIZE, NR_WRITEBACK }, +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + /* + * The ratio will be initialized in numa_stats_init(). Because + * on some architectures, the macro of HPAGE_PMD_SIZE is not + * constant(e.g. powerpc). + */ + { "anon_thp", 0, NR_ANON_THPS }, +#endif + { "inactive_anon", PAGE_SIZE, NR_INACTIVE_ANON }, + { "active_anon", PAGE_SIZE, NR_ACTIVE_ANON }, + { "inactive_file", PAGE_SIZE, NR_INACTIVE_FILE }, + { "active_file", PAGE_SIZE, NR_ACTIVE_FILE }, + { "unevictable", PAGE_SIZE, NR_UNEVICTABLE }, + { "slab_reclaimable", 1, NR_SLAB_RECLAIMABLE_B }, + { "slab_unreclaimable", 1, NR_SLAB_UNRECLAIMABLE_B }, +}; + +static int __init numa_stats_init(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(numa_stats); i++) { +#ifdef CONFIG_TRANSPARENT_HUGEPAGE + if (numa_stats[i].idx == NR_ANON_THPS) + numa_stats[i].ratio = HPAGE_PMD_SIZE; +#endif + } + + return 0; +} +pure_initcall(numa_stats_init); + +static unsigned long memcg_node_page_state(struct mem_cgroup *memcg, + unsigned int nid, + enum node_stat_item idx) +{ + VM_BUG_ON(nid >= nr_node_ids); + return lruvec_page_state(mem_cgroup_lruvec(memcg, NODE_DATA(nid)), idx); +} + +static int memory_numa_stat_show(struct seq_file *m, void *v) +{ + int i; + struct mem_cgroup *memcg = mem_cgroup_from_seq(m); + + for (i = 0; i < ARRAY_SIZE(numa_stats); i++) { + int nid; + + seq_printf(m, "%s", numa_stats[i].name); + for_each_node_state(nid, N_MEMORY) { + u64 size; + + size = memcg_node_page_state(memcg, nid, + numa_stats[i].idx); + VM_WARN_ON_ONCE(!numa_stats[i].ratio); + size *= numa_stats[i].ratio; + seq_printf(m, " N%d=%llu", nid, size); + } + seq_putc(m, '\n'); + } + + return 0; +} +#endif + static int memory_oom_group_show(struct seq_file *m, void *v) { struct mem_cgroup *memcg = mem_cgroup_from_seq(m); @@ -6502,6 +6582,12 @@ static struct cftype memory_files[] = { .name = "stat", .seq_show = memory_stat_show, }, +#ifdef CONFIG_NUMA + { + .name = "numa_stat", + .seq_show = memory_numa_stat_show, + }, +#endif { .name = "oom.group", .flags = CFTYPE_NOT_ON_ROOT | CFTYPE_NS_DELEGATABLE,