From patchwork Tue Jan 4 22:23:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shakeel Butt X-Patchwork-Id: 12703895 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 EA96BC433F5 for ; Tue, 4 Jan 2022 22:24:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 602696B0072; Tue, 4 Jan 2022 17:24:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 588E86B0073; Tue, 4 Jan 2022 17:24:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 429CE6B0074; Tue, 4 Jan 2022 17:24:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0032.hostedemail.com [216.40.44.32]) by kanga.kvack.org (Postfix) with ESMTP id 2D4BA6B0072 for ; Tue, 4 Jan 2022 17:24:16 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id DA9318249980 for ; Tue, 4 Jan 2022 22:24:15 +0000 (UTC) X-FDA: 78994034070.13.9834ACB Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf16.hostedemail.com (Postfix) with ESMTP id 75474180004 for ; Tue, 4 Jan 2022 22:24:15 +0000 (UTC) Received: by mail-pj1-f73.google.com with SMTP id y13-20020a17090a134d00b001b10368d9dcso24933635pjf.7 for ; Tue, 04 Jan 2022 14:24:15 -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=7YCEEnOPj4KefNq/L+wc30dtC7OQCX+FrwepcfjVLCk=; b=i/4d5jHHzbNLD0plqavS+PSO1WxSe0NYT9QbWG0z3poJM5oXjubaAcJxaiWWreP0cR 5RiMCt5v4DQTomdHVkq3iLNdYX/yVKtHxPDMl0I2XHVIuxuFSqZGpHeGMYz88UG7O0Xu r9RMfDw8lVa4ABXKEpKplbdcb/JYd+BYaRDzoBG8Fr3Bxq/Fuv6XAleGIBpGAsP24c2L HqWZEkpkGx0zcwj6E2VFzL8t+653tIMfzyZ7+Kfvyk9YsriAmimD4EL1W9ulKiubY+wC kTTycdGDGkg1fP1SB4auPTUcB25a0h9QpzkssNErteZUcjHkYQW6SJMRWAO3Yz3vGlS4 Lpfg== 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=7YCEEnOPj4KefNq/L+wc30dtC7OQCX+FrwepcfjVLCk=; b=aDHEWhYvWDdu42kamnkjKbqkwT2mDBVefkbW9ADWw3rim3RRPk4g2nL9jK8ZIHkMTz DgrNNc9AfdzCuf6u7gCc58lXwhHq5Dyx3FNym/eqZwhDYu275edSpplpn9SyFAqGZT6W 9IR2vj9RCgNnDccTWPz+mZ5I9YB9s4c14qRzsRp1bqCcszBp4+Fh/FtaWOXWeNQwKQct mJeVJn8OPbcmfs/UFiUxMegisbUffxbEi1pKsGCHDa9vmsokGqX3XHDvAaaojMb5loyg gkzA90Ps9f6uQV0if9NODe3zfd8alzcgFP5cFOeP5nxjPYozpEMiz9OM8R07hZw/PXm0 f+wQ== X-Gm-Message-State: AOAM5319O07+ueVHqleMAI7Rahuws5sWRu3VLVZmBfaX+SVo7KtxELVa yy8r/iIhXAYKx0nyAEH+haHiDgVH29CLkw== X-Google-Smtp-Source: ABdhPJxu2rUxV1GLYPjz1MbyUkhsmMVGZu7fNO7fjMU2hGoO4haOYMv5RYTU3zN2gckKhL0PGXE0N4uc4PQ3Fg== X-Received: from shakeelb.svl.corp.google.com ([2620:15c:2cd:202:15c6:5bd2:a484:dc2e]) (user=shakeelb job=sendgmr) by 2002:a63:2c06:: with SMTP id s6mr44845822pgs.55.1641335054261; Tue, 04 Jan 2022 14:24:14 -0800 (PST) Date: Tue, 4 Jan 2022 14:23:41 -0800 Message-Id: <20220104222341.3972772-1-shakeelb@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.1.448.ga2b2bfdf31-goog Subject: [PATCH v4] 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 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 75474180004 X-Stat-Signature: s7k7ugn1tq1m8bg7tji6px8fgqs9u681 Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="i/4d5jHH"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3DsnUYQgKCAk1qjtnnukpxxpun.lxvurw36-vvt4jlt.x0p@flex--shakeelb.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3DsnUYQgKCAk1qjtnnukpxxpun.lxvurw36-vvt4jlt.x0p@flex--shakeelb.bounces.google.com X-HE-Tag: 1641335055-274220 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 Acked-by: Michal Hocko --- Changes since v3: - Removed area->page[0] checks and moved to page by page accounting as suggested by Michal Changes since v2: - remove cast, per Muchun - add area->page[0] check, per Michal Changes 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 | 13 +++++++++++-- 4 files changed, 36 insertions(+), 2 deletions(-) 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 101b22a23096..56ed6b5dd328 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..bdc7222f87d4 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -2624,12 +2625,13 @@ static void __vunmap(const void *addr, int deallocate_pages) if (deallocate_pages) { unsigned int page_order = vm_area_page_order(area); - int i; + int i, step = 1U << page_order; - for (i = 0; i < area->nr_pages; i += 1U << page_order) { + for (i = 0; i < area->nr_pages; i += step) { struct page *page = area->pages[i]; BUG_ON(!page); + mod_memcg_page_state(page, MEMCG_VMALLOC, -step); __free_pages(page, page_order); cond_resched(); } @@ -2964,6 +2966,13 @@ 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); + if (gfp_mask & __GFP_ACCOUNT) { + int i, step = 1U << page_order; + + for (i = 0; i < area->nr_pages; i += step) + mod_memcg_page_state(area->pages[i], MEMCG_VMALLOC, + step); + } /* * If not enough pages were obtained to accomplish an