From patchwork Fri Nov 1 20:44:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joshua Hahn X-Patchwork-Id: 13859750 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 12E63E6F08C for ; Fri, 1 Nov 2024 20:44:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 426476B009D; Fri, 1 Nov 2024 16:44:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D6BE6B009E; Fri, 1 Nov 2024 16:44:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29DDE6B009F; Fri, 1 Nov 2024 16:44:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0BB016B009D for ; Fri, 1 Nov 2024 16:44:07 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A7DC21404A0 for ; Fri, 1 Nov 2024 20:44:06 +0000 (UTC) X-FDA: 82738702998.28.7A8E5BA Received: from mail-yb1-f173.google.com (mail-yb1-f173.google.com [209.85.219.173]) by imf14.hostedemail.com (Postfix) with ESMTP id 0888B100015 for ; Fri, 1 Nov 2024 20:43:32 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HwTa6AWe; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of joshua.hahnjy@gmail.com designates 209.85.219.173 as permitted sender) smtp.mailfrom=joshua.hahnjy@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730493681; a=rsa-sha256; cv=none; b=E4MLZEPz/H3P14GrByBJ3m0SN7lJr7PFADho8+JhvefQWsNBWBp/1CaPBtzePlKctjhchi EOHLXXfff3TAYQCDtBVlhACv6SpHdZOaH2xrS6DPt09x5GkMGdU+rMYndWKodqEAeAt6Z3 4ihlvXEf64SxB5w/TaYqVBoOg7BIe/g= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HwTa6AWe; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf14.hostedemail.com: domain of joshua.hahnjy@gmail.com designates 209.85.219.173 as permitted sender) smtp.mailfrom=joshua.hahnjy@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730493681; 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:dkim-signature; bh=tCM+fKsGzk5AZDxT4A2dewBEVdVNNm2ySi8AOsEK/l0=; b=8gi7tlN1XMC5fA7rxNgEEOLmqCc8OymM/0TWkbQ0NplQ8Kl6OdxLsEIRt46+FZQVm1NtL1 kuoG8TWg5pf6T49wOecoO2ZUWMSCseTygbpqRtXwu0JXgQuGkgvtPjLSfSF8MtVTUx7S8F zMeb8cPg09YRHjBQ0Y6Khjd3bcYhhXE= Received: by mail-yb1-f173.google.com with SMTP id 3f1490d57ef6-e291f1d659aso2221382276.3 for ; Fri, 01 Nov 2024 13:44:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730493844; x=1731098644; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=tCM+fKsGzk5AZDxT4A2dewBEVdVNNm2ySi8AOsEK/l0=; b=HwTa6AWe5qoQtXSQ+CLr5etc7kJJ2iL8X9ZHp8aa05clQPrt3++8PLTpW1GMkn6rFu lc7jWdzU9eyswf4PTMsBP92wanBiVvN/VlaH+5sjlYe6iprFSl2tReC1mWeM3DZRuGbk yAWh/XSMSye4uGnVXUXNWeEgtm3oslcPomDqjr9QxVpOdFt0hW7pL1/4bMowjaHH6/nN GhsY939HPf+wEDkXw49zJj0ERqQlSEn5E/85GqWpRhYAkDzGq1mIIi46uVrGJsOaobeO BUeIj8mP0jEq1TtibntXouf3u7nYM7VOB+WPhIIClRaAUwBNR0BjjpdrHLwD+3jDag78 9Y6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730493844; x=1731098644; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tCM+fKsGzk5AZDxT4A2dewBEVdVNNm2ySi8AOsEK/l0=; b=qYV/601MsJxUyhvQlNL9XTb/vbRU7CMOrOthRSfdvVjL3ch75CMqmvKoMlSK0yjCoM XsoAHc7BphP3nDikmJVI+6euNdpJEsq2f0VemvdikIZfZZfyKOx9lQB3ApSCTP74TL0I LPov8jic6wdyrnSmJGdgdMZd5zYjP7nS/Bggx0XChTiBxRuhCb2WKw7aE0XDEyN1ffTK kypwAWSx9PthFO6NDjGysXy8MiaUJbeF53zNg/2fH2pO1lfz80aVm3Q7EASUonaaf5VZ iJrywz4uwEyJwewkBTGu0DwaOJlDnk0+QOPxZqaQiM3AtRIZmViQjTa6iSeNetBAUwRz XOww== X-Forwarded-Encrypted: i=1; AJvYcCWW1Fiye2+Hpy4yl63nFzztx+BXDUYt7q68NycIlaWc39HrIptohic8etVzMXEuw0c+Sgaswd+4oA==@kvack.org X-Gm-Message-State: AOJu0YwE5XtjB7fpS3rfFNgpP+rdbZP4CWKtYytHMAhXdFY86pCMfbSz y9sYGE92624in/usAQXgywiihs1/znA/N8K367RykTFUoFRNiewv X-Google-Smtp-Source: AGHT+IEkuzO5zxQKkn/65x1uoWoqfWH/QbZaag/6vrSeC1yuyVpynp5j/emFlwaPWuvpnu0ognHsMA== X-Received: by 2002:a05:6902:b0e:b0:e30:cbde:1252 with SMTP id 3f1490d57ef6-e330266ec61mr3601095276.36.1730493843734; Fri, 01 Nov 2024 13:44:03 -0700 (PDT) Received: from localhost (fwdproxy-frc-022.fbsv.net. [2a03:2880:21ff:16::face:b00c]) by smtp.gmail.com with ESMTPSA id 3f1490d57ef6-e30e8a61797sm871280276.12.2024.11.01.13.44.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Nov 2024 13:44:03 -0700 (PDT) From: Joshua Hahn To: akpm@linux-foundation.org, hannes@cmpxchg.org Cc: nphamcs@gmail.com, shakeel.butt@linux.dev, roman.gushchin@linux.dev, muchun.song@linux.dev, chris@chrisdown.name, tj@kernel.org, lizefan.x@bytedance.com, mkoutny@suse.com, corbet@lwn.net, lnyng@meta.com, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH v4 1/1] memcg/hugetlb: Add hugeTLB counters to memcg Date: Fri, 1 Nov 2024 13:44:02 -0700 Message-ID: <20241101204402.1885383-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Stat-Signature: 5p3zdbrfoa3iqbzwrnxu988pwx7kzwao X-Rspamd-Queue-Id: 0888B100015 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1730493812-665572 X-HE-Meta: U2FsdGVkX19B18F/n1U2XBeoVQUqgypa8vR1Qao7Lh7t5MXGtLcU3BrTX9Asu0X1PUdNAWbHxlJKRcTs8zemyNAawQkJL+qO/9YPV8g6gGRVaHRLvD406Y0Ka7J1CySdEz83QXqXi2UzLA89RZQ/ROr4fd3LVOW6HAsNNpEJB9zDy9No+TWH1oAj8V8TzPAMC0yKHarvMp5f6SHKh6deIy1SJTQKoR67U31IRJkMu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lj6O9WmK3Pfi0peoqrHJwLWQdGoPlk3PqS+NbEi8RHQ5lBFbm1uqVXi5RkcmP5/jWMcV75FW1bqiF8qkcLRKm9TVqOO8Re1ZdoRaMA6jbfKFjmGq+ZZq2V1x7xfAoOTPWBy1zJKmzbXhyWSj5qDAjiHQ4NJhGcUEG1+egOR6lv9t4byOWbgGYPHaPwbFlsQ4vRCBHgO3TrKK0dtKG9LECCUxmNuAfvMpPK95y8/oG07X 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: List-Subscribe: List-Unsubscribe: This patch introduces a new counter to memory.stat that tracks hugeTLB usage, only if hugeTLB accounting is done to memory.current. This feature is enabled the same way hugeTLB accounting is enabled, via the memory_hugetlb_accounting mount flag for cgroupsv2. 1. Why is this patch necessary? Currently, memcg hugeTLB accounting is an opt-in feature [1] that adds hugeTLB usage to memory.current. However, the metric is not reported in memory.stat. Given that users often interpret memory.stat as a breakdown of the value reported in memory.current, the disparity between the two reports can be confusing. This patch solves this problem by including the metric in memory.stat as well, but only if it is also reported in memory.current (it would also be confusing if the value was reported in memory.stat, but not in memory.current) Aside from the consistency between the two files, we also see benefits in observability. Userspace might be interested in the hugeTLB footprint of cgroups for many reasons. For instance, system admins might want to verify that hugeTLB usage is distributed as expected across tasks: i.e. memory-intensive tasks are using more hugeTLB pages than tasks that don't consume a lot of memory, or are seen to fault frequently. Note that this is separate from wanting to inspect the distribution for limiting purposes (in which case, hugeTLB controller makes more sense). 2. We already have a hugeTLB controller. Why not use that? It is true that hugeTLB tracks the exact value that we want. In fact, by enabling the hugeTLB controller, we get all of the observability benefits that I mentioned above, and users can check the total hugeTLB usage, verify if it is distributed as expected, etc. With this said, there are 2 problems: (a) They are still not reported in memory.stat, which means the disparity between the memcg reports are still there. (b) We cannot reasonably expect users to enable the hugeTLB controller just for the sake of hugeTLB usage reporting, especially since they don't have any use for hugeTLB usage enforcing [2]. 3. Implementation Details: In the alloc / free hugetlb functions, we call lruvec_stat_mod_folio regardless of whether memcg accounts hugetlb. mem_cgroup_commit_charge which is called from alloc_hugetlb_folio will set memcg for the folio only if the CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING cgroup mount option is used, so lruvec_stat_mod_folio accounts per-memcg hugetlb counters only if the feature is enabled. Regardless of whether memcg accounts for hugetlb, the newly added global counter is updated and shown in /proc/vmstat. The global counter is added because vmstats is the preferred framework for cgroup stats. It makes stat items consistent between global and cgroups. It also provides a per-node breakdown, which is useful. Because it does not use cgroup-specific hooks, we also keep generic MM code separate from memcg code. [1] https://lore.kernel.org/all/20231006184629.155543-1-nphamcs@gmail.com/ [2] Of course, we can't make a new patch for every feature that can be duplicated. However, since the existing solution of enabling the hugeTLB controller is an imperfect solution that still leaves a discrepancy between memory.stat and memory.curent, I think that it is reasonable to isolate the feature in this case. Suggested-by: Nhat Pham Suggested-by: Shakeel Butt Suggested-by: Johannes Weiner Acked-by: Shakeel Butt Acked-by: Johannes Weiner Acked-by: Chris Down Acked-by: Michal Hocko Reviewed-by: Roman Gushchin Reviewed-by: Nhat Pham Signed-off-by: Joshua Hahn --- Changelog v4: * Added {acked,suggested,reviewed}-by to the list * Added an extra section detailing why having no checks for mount options or configs is ok (handled by lruvec_stat_mod_folio). Also includes justifications for adding a global counter v3: * Removed check for whether CGRP_ROOT_HUGETLB_ACCOUNTING is on, since this check is already handled by lruvec_stat_mod (and doing the check in hugetlb.c actually breaks the build if MEMCG is not enabled. * Because there is now only one check for the flags, I've opted to do all of the cleanup in a separate patch series. * Added hugetlb information in cgroup-v2.rst * Added Suggested-by: Shakeel Butt v2: * Enables the feature only if memcg accounts for hugeTLB usage * Moves the counter from memcg_stat_item to node_stat_item * Expands on motivation & justification in commitlog * Added Suggested-by: Nhat Pham Documentation/admin-guide/cgroup-v2.rst | 5 +++++ include/linux/mmzone.h | 3 +++ mm/hugetlb.c | 2 ++ mm/memcontrol.c | 11 +++++++++++ mm/vmstat.c | 3 +++ 5 files changed, 24 insertions(+) diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst index 69af2173555f..bd7e81c2aa2b 100644 --- a/Documentation/admin-guide/cgroup-v2.rst +++ b/Documentation/admin-guide/cgroup-v2.rst @@ -1646,6 +1646,11 @@ The following nested keys are defined. pgdemote_khugepaged Number of pages demoted by khugepaged. + hugetlb + Amount of memory used by hugetlb pages. This metric only shows + up if hugetlb usage is accounted for in memory.current (i.e. + cgroup is mounted with the memory_hugetlb_accounting option). + memory.numa_stat A read-only nested-keyed file which exists on non-root cgroups. diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 17506e4a2835..972795ae5946 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -220,6 +220,9 @@ enum node_stat_item { PGDEMOTE_KSWAPD, PGDEMOTE_DIRECT, PGDEMOTE_KHUGEPAGED, +#ifdef CONFIG_HUGETLB_PAGE + NR_HUGETLB, +#endif NR_VM_NODE_STAT_ITEMS }; diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 190fa05635f4..fbb10e52d7ea 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1925,6 +1925,7 @@ void free_huge_folio(struct folio *folio) pages_per_huge_page(h), folio); hugetlb_cgroup_uncharge_folio_rsvd(hstate_index(h), pages_per_huge_page(h), folio); + lruvec_stat_mod_folio(folio, NR_HUGETLB, -pages_per_huge_page(h)); mem_cgroup_uncharge(folio); if (restore_reserve) h->resv_huge_pages++; @@ -3093,6 +3094,7 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, if (!memcg_charge_ret) mem_cgroup_commit_charge(folio, memcg); + lruvec_stat_mod_folio(folio, NR_HUGETLB, pages_per_huge_page(h)); mem_cgroup_put(memcg); return folio; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7845c64a2c57..5444d0e7bb64 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -310,6 +310,9 @@ static const unsigned int memcg_node_stat_items[] = { PGDEMOTE_KSWAPD, PGDEMOTE_DIRECT, PGDEMOTE_KHUGEPAGED, +#ifdef CONFIG_HUGETLB_PAGE + NR_HUGETLB, +#endif }; static const unsigned int memcg_stat_items[] = { @@ -1346,6 +1349,9 @@ static const struct memory_stat memory_stats[] = { { "unevictable", NR_UNEVICTABLE }, { "slab_reclaimable", NR_SLAB_RECLAIMABLE_B }, { "slab_unreclaimable", NR_SLAB_UNRECLAIMABLE_B }, +#ifdef CONFIG_HUGETLB_PAGE + { "hugetlb", NR_HUGETLB }, +#endif /* The memory events */ { "workingset_refault_anon", WORKINGSET_REFAULT_ANON }, @@ -1441,6 +1447,11 @@ static void memcg_stat_format(struct mem_cgroup *memcg, struct seq_buf *s) for (i = 0; i < ARRAY_SIZE(memory_stats); i++) { u64 size; +#ifdef CONFIG_HUGETLB_PAGE + if (unlikely(memory_stats[i].idx == NR_HUGETLB) && + !(cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING)) + continue; +#endif size = memcg_page_state_output(memcg, memory_stats[i].idx); seq_buf_printf(s, "%s %llu\n", memory_stats[i].name, size); diff --git a/mm/vmstat.c b/mm/vmstat.c index b5a4cea423e1..871566b04b79 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1273,6 +1273,9 @@ const char * const vmstat_text[] = { "pgdemote_kswapd", "pgdemote_direct", "pgdemote_khugepaged", +#ifdef CONFIG_HUGETLB_PAGE + "nr_hugetlb", +#endif /* system-wide enum vm_stat_item counters */ "nr_dirty_threshold", "nr_dirty_background_threshold",