From patchwork Sun Sep 13 07:00:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11772227 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 1749A139F for ; Sun, 13 Sep 2020 07:01:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A746321655 for ; Sun, 13 Sep 2020 07:01:39 +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="EQNxbcmr" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A746321655 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 8006A8E0001; Sun, 13 Sep 2020 03:01:38 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7B2F06B005A; Sun, 13 Sep 2020 03:01:38 -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 69FB68E0001; Sun, 13 Sep 2020 03:01:38 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0064.hostedemail.com [216.40.44.64]) by kanga.kvack.org (Postfix) with ESMTP id 4EEF66B0055 for ; Sun, 13 Sep 2020 03:01:38 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 01FF11803E8FA for ; Sun, 13 Sep 2020 07:01:38 +0000 (UTC) X-FDA: 77257142676.18.eggs60_070198e270fe Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin18.hostedemail.com (Postfix) with ESMTP id B49FC1018AC97 for ; Sun, 13 Sep 2020 07:01:37 +0000 (UTC) X-Spam-Summary: 1,0,0,cecc21fee67489dc,d41d8cd98f00b204,songmuchun@bytedance.com,,RULES_HIT:1:2:41:355:379:541:800:960:966:973:988:989:1260:1311:1314:1345:1437:1500:1515:1605:1730:1747:1777:1792:1801:1963:2196:2198:2199:2200:2393:2553:2559:2562:2693:2892:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3874:4050:4250:4321:4385: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:13221:13229:13869:13894:14394:21063:21080:21324:21444:21451:21627:21740:21939:21990:30005:30029:30045:30054:30064:30074:30075:30090,0,RBL:209.85.215.193:@bytedance.com:.lbl8.mailshell.net-62.14.0.100 66.201.201.201;04ygrrbo4t5oqmfq1yzmu1pxga3b3yc6bipzfk9878hrzy8371a8fjtg6361fpb.riei441r88ejrpdrwi7yybkq9kwud7t15ogup6mko5s66jtobj1zzi3z639uykt.n-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:none,Custom_rules:0:0:0,LFtime:32,LUA_S UMMARY:n X-HE-Tag: eggs60_070198e270fe X-Filterd-Recvd-Size: 10585 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Sun, 13 Sep 2020 07:01:37 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id u13so9163076pgh.1 for ; Sun, 13 Sep 2020 00:01:36 -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=DTITQjXjsPqeS3q5uQCStGzc676txDfh5hSNVhc3r6s=; b=EQNxbcmrx5LKBOZbdD8y3CWQOUtvm9Q9+0LXa4r0Mj0wtS+MstvPvKBH10e+vDijQV qtvQZ6EPHWdqk+TXzscujfYxeiG+eqJL4hXQuJvTDB1Q7noGY2y0+q/5KwhUWju9ytls VsLgAd9qO1HQVHzT2m6u0HrCBaGI8wG96AL7z7s8X6JU7rzM5lmOVAAcHiWKG8nVZuEZ u184xHmL/m/0Ptklo5uCIPA8bK5rnZKIp+LKwh0GCFkJF6u8uZsqkUaPKaFLu5ZkDzdJ FD3lsNrc8KwB7ScA80kn8z79unTGX1dZ4sJ2p3+lT9gqjjDaPUVQbFdTrswlwUWzbSmg wb4w== 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=DTITQjXjsPqeS3q5uQCStGzc676txDfh5hSNVhc3r6s=; b=jSjnJTWLtap1lqvTkNRSRm0DcR/2vhXy0j3uF9jU/ItyAgwXrNMRYoCWEZWbTB0EOW Y4nZPjq7I0g1T9ATG9tUanFO8MO7/VqsvzDiVuLJGFMjPmX5u32zEoJ6jxrb+NJBBRth 1MPFzjQXkrGxrIHTa5XrkjyXO+u9q1bYuFHsRPCi1Y7piAZbKP6XrxyWz4Bd2ErFPGOd F4U8Nvo6ER8As0/vPRP2RtJFDwQHiuusK5cVJJ0qJgvMag12p/yvQKMJl31pJ5eo3rGw Am06U4EcM4y1hsVHGTpOZy6ZBxqCWVfTAV/J/kD6cn7ITxkknmRciUNmf4BOwTgiXnZT TjoA== X-Gm-Message-State: AOAM531KdwQrYqKd7r+nOAKAIXoTVAZLvEwndff23VbPUk1cpkPXxWmo 11H9GCZoF1bEwmVo0N04r7JYBA== X-Google-Smtp-Source: ABdhPJwj4J678KdwNwTZPyPKY01PXjumQEW0bpphGuGp/1blnPWmE1TRf+I/un6mZ5cooWqy1Mz4VQ== X-Received: by 2002:a63:b20d:: with SMTP id x13mr4919769pge.136.1599980495724; Sun, 13 Sep 2020 00:01:35 -0700 (PDT) Received: from localhost.localdomain ([103.136.221.70]) by smtp.gmail.com with ESMTPSA id z84sm6877245pfc.105.2020.09.13.00.01.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 13 Sep 2020 00:01:35 -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 Cc: cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Muchun Song , kernel test robot Subject: [PATCH v3] mm: memcontrol: Add the missing numa_stat interface for cgroup v2 Date: Sun, 13 Sep 2020 15:00:10 +0800 Message-Id: <20200913070010.44053-1-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) MIME-Version: 1.0 X-Rspamd-Queue-Id: B49FC1018AC97 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 Reported-by: kernel test robot --- 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 | 107 ++++++++++++++++++++++++ 2 files changed, 179 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 6be43781ec7f..92207f0012e4 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 (eg. 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 1b403d6f5da0..2dadb1aaedaa 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6427,6 +6427,107 @@ 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 const char *memory_numa_stat_format(struct mem_cgroup *memcg) +{ + int i; + struct seq_buf s; + + /* Reserve a byte for the trailing null */ + seq_buf_init(&s, kmalloc(PAGE_SIZE, GFP_KERNEL), PAGE_SIZE - 1); + if (!s.buffer) + return NULL; + + for (i = 0; i < ARRAY_SIZE(numa_stats); i++) { + int nid; + + seq_buf_printf(&s, "%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); + size *= numa_stats[i].ratio; + seq_buf_printf(&s, " N%d=%llu", nid, size); + } + seq_buf_putc(&s, '\n'); + } + + /* The above should easily fit into one page */ + if (WARN_ON_ONCE(seq_buf_putc(&s, '\0'))) + s.buffer[PAGE_SIZE - 1] = '\0'; + + return s.buffer; +} + +static int memory_numa_stat_show(struct seq_file *m, void *v) +{ + struct mem_cgroup *memcg = mem_cgroup_from_seq(m); + const char *buf; + + buf = memory_numa_stat_format(memcg); + if (!buf) + return -ENOMEM; + seq_puts(m, buf); + kfree(buf); + return 0; +} +#endif + static int memory_oom_group_show(struct seq_file *m, void *v) { struct mem_cgroup *memcg = mem_cgroup_from_seq(m); @@ -6504,6 +6605,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,