From patchwork Wed Dec 22 05:24:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12691103 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 37E0DC433F5 for ; Wed, 22 Dec 2021 05:25:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA4666B0072; Wed, 22 Dec 2021 00:25:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A54536B0073; Wed, 22 Dec 2021 00:25:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91CC36B0074; Wed, 22 Dec 2021 00:25:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0101.hostedemail.com [216.40.44.101]) by kanga.kvack.org (Postfix) with ESMTP id 837E46B0072 for ; Wed, 22 Dec 2021 00:25:19 -0500 (EST) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 3D3CA181AC9C6 for ; Wed, 22 Dec 2021 05:25:19 +0000 (UTC) X-FDA: 78944291958.08.BA45F32 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf09.hostedemail.com (Postfix) with ESMTP id 01397140028 for ; Wed, 22 Dec 2021 05:25:13 +0000 (UTC) Received: by mail-pg1-f202.google.com with SMTP id o9-20020a637309000000b0033fba59a89dso768750pgc.17 for ; Tue, 21 Dec 2021 21:25:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=V7NKgx+P49cXgQMHOCdgsm/aqsZCXHZS2DX/PC1OjLU=; b=OsycMK/PN2uUT3buihQUWnbvxxmeuNFWvw/y7LbM3119dlKMLvf9KJlgfWiNNd8XCs dIzeudPPN0sE/tqL5sBEKs7neYdm5J4iNHzuUwPrv70hjGPft9CXb02quVfA7+LPxE1J UDh7YvT9e8gWSvUkjk+OXvptZuJWRKXWN0FU1gEnq8cRQPsFdw30EAqaEvQS579QXQts 7D37mMKZ0HXJYQ5dJzb7PB2ON+aj/fGtZ9AnyWSTr3dPB8ND649Ie1CfoUyQtQKWgGyR oL5u+JRYBl8VWURRXnXEZTWnS6scb+N7uUr8ofXhg7M2I+6WT69AJ1ZtZyrMhDRJsDYx 7AzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=V7NKgx+P49cXgQMHOCdgsm/aqsZCXHZS2DX/PC1OjLU=; b=SOLZdk5xNBcxfQYOJYKmYPV946ZMlax0f/IEvIYIxJL+kPTOOjGn/LzT5e2d88cfO0 ORLHmg51FWDHdb44YaY0EEz48elii3t8+dAB3byc943WQE6THsBl8ER9qiKYL3Zl5Wwi hR8GD4entLU4GKKdR7nbk22OM+sdItBX91IqsH8X4lHtzp2+3h06aG3PxrRdiTAcQj1S gIeKquid215SNnheRIfF5ERN2MqAqM/0JQ5KEdsnOcyC3ct//qh0hFQD/axjnOjIxRZ8 GaYsY8MRrqDJa+TRyqTCzw+AXLQ+s6b3UikPItm7PSznwwYU5VUNaqSJJ/rxqOszbV5r /F5w== X-Gm-Message-State: AOAM532vkcCAJqLAlr5fXk5krlewMh3XHv8WJWvpFv2pIFTJFtDRSuXV x1xBsinq4ExWD61DyKU/3p1jVB5lFNHFFQ== X-Google-Smtp-Source: ABdhPJzLJrpINbxDAbJckAXrx2QrhzFGsUj57mjOT3Xrhi6lGorvxNt9hXxVYMQUdKNYCLqdc3/ErRK3KAiDqA== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:fbbb:6368:35d1:5ac2]) (user=shakeelb job=sendgmr) by 2002:a17:902:9b8f:b0:142:7999:67fb with SMTP id y15-20020a1709029b8f00b00142799967fbmr1256736plp.24.1640150717444; Tue, 21 Dec 2021 21:25:17 -0800 (PST) Date: Tue, 21 Dec 2021 21:24:57 -0800 Message-Id: <20211222052457.1960701-1-shakeelb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.1.307.g9b7440fafd-goog Subject: [PATCH v2] memcg: add per-memcg vmalloc stat From: Shakeel Butt To: Johannes Weiner , Michal Hocko , Roman Gushchin , Muchun Song Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Shakeel Butt Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="OsycMK/P"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf09.hostedemail.com: domain of 3vbbCYQgKCEo4tmwqqxns00sxq.o0yxuz69-yyw7mow.03s@flex--shakeelb.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3vbbCYQgKCEo4tmwqqxns00sxq.o0yxuz69-yyw7mow.03s@flex--shakeelb.bounces.google.com X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 01397140028 X-Stat-Signature: jynuexw1se9jog6ccp4uzupw9f9b4b7s X-HE-Tag: 1640150713-146578 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: The kvmalloc* allocation functions can fallback to vmalloc allocations and more often on long running machines. In addition the kernel does have __GFP_ACCOUNT kvmalloc* calls. So, often on long running machines, the memory.stat does not tell the complete picture which type of memory is charged to the memcg. So add a per-memcg vmalloc stat. Signed-off-by: Shakeel Butt Reviewed-by: Muchun Song Acked-by: Roman Gushchin --- Changelog since v1: - page_memcg() within rcu lock as suggested by Muchun. Documentation/admin-guide/cgroup-v2.rst | 3 +++ include/linux/memcontrol.h | 21 +++++++++++++++++++++ mm/memcontrol.c | 1 + mm/vmalloc.c | 5 +++++ 4 files changed, 30 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 82c8dc91b2be..5aa368d165da 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1314,6 +1314,9 @@ PAGE_SIZE multiple when read back. sock (npn) Amount of memory used in network transmission buffers + vmalloc (npn) + Amount of memory used for vmap backed memory. + shmem Amount of cached filesystem data that is swap-backed, such as tmpfs, shm segments, shared anonymous mmap()s diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index d76dad703580..b72d75141e12 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -33,6 +33,7 @@ enum memcg_stat_item { MEMCG_SWAP = NR_VM_NODE_STAT_ITEMS, MEMCG_SOCK, MEMCG_PERCPU_B, + MEMCG_VMALLOC, MEMCG_NR_STAT, }; @@ -944,6 +945,21 @@ static inline void mod_memcg_state(struct mem_cgroup *memcg, local_irq_restore(flags); } +static inline void mod_memcg_page_state(struct page *page, + int idx, int val) +{ + struct mem_cgroup *memcg; + + if (mem_cgroup_disabled()) + return; + + rcu_read_lock(); + memcg = page_memcg(page); + if (memcg) + mod_memcg_state(memcg, idx, val); + rcu_read_unlock(); +} + static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) { return READ_ONCE(memcg->vmstats.state[idx]); @@ -1399,6 +1415,11 @@ static inline void mod_memcg_state(struct mem_cgroup *memcg, { } +static inline void mod_memcg_page_state(struct page *page, + int idx, int val) +{ +} + static inline unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx) { return 0; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7ae77608847e..7027a3cc416f 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1375,6 +1375,7 @@ static const struct memory_stat memory_stats[] = { { "pagetables", NR_PAGETABLE }, { "percpu", MEMCG_PERCPU_B }, { "sock", MEMCG_SOCK }, + { "vmalloc", MEMCG_VMALLOC }, { "shmem", NR_SHMEM }, { "file_mapped", NR_FILE_MAPPED }, { "file_dirty", NR_FILE_DIRTY }, diff --git a/mm/vmalloc.c b/mm/vmalloc.c index eb6e527a6b77..af67ce4fd402 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -2626,6 +2627,9 @@ static void __vunmap(const void *addr, int deallocate_pages) unsigned int page_order = vm_area_page_order(area); int i; + mod_memcg_page_state(area->pages[0], MEMCG_VMALLOC, + -(int)area->nr_pages); + for (i = 0; i < area->nr_pages; i += 1U << page_order) { struct page *page = area->pages[i]; @@ -2964,6 +2968,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, page_order, nr_small_pages, area->pages); atomic_long_add(area->nr_pages, &nr_vmalloc_pages); + mod_memcg_page_state(area->pages[0], MEMCG_VMALLOC, area->nr_pages); /* * If not enough pages were obtained to accomplish an