hugetlb memcg accounting
Subsystems (8)
v4 cover hugetlb memcg accounting
Changelog:
v4:
* Add another prep patch to clean up memory controller migration logic.
* Fix an issue in hugetlb folio migration where the new folio is not properly charged (patch 3) (reported by Mike Kravetz) (suggested by Johannes Weiner).
v3:
* Add a prep patch at the start of the series to extend the memory controller interface with new helper functions for hugetlb accounting.
* Do not account hugetlb memory for memcontroller in cgroup v1 (patch 2) (suggested by Johannes Weiner).
* Change the gfp flag passed to mem cgroup charging (patch 2) (suggested by Michal Hocko).
* Add caveats to cgroup admin guide and commit changelog (patch 2) (suggested by Michal Hocko).
v2:
* Add a cgroup mount option to enable/disable the new hugetlb memcg accounting behavior (patch 1) (suggested by Johannes Weiner).
* Add a couple more ksft_print_msg() on error to aid debugging when the selftest fails. (patch 2)
Currently, hugetlb memory usage is not acounted for in the memory controller, which could lead to memory overprotection for cgroups with hugetlb-backed memory. This has been observed in our production system.
For instance, here is one of our usecases: suppose there are two 32G containers. The machine is booted with hugetlb_cma=6G, and each container may or may not use up to 3 gigantic page, depending on the workload within it. The rest is anon, cache, slab, etc. We can set the hugetlb cgroup limit of each cgroup to 3G to enforce hugetlb fairness. But it is very difficult to configure memory.max to keep overall consumption, including anon, cache, slab etc. fair.
What we have had to resort to is to constantly poll hugetlb usage and readjust memory.max. Similar procedure is done to other memory limits (memory.low for e.g). However, this is rather cumbersome and buggy. Furthermore, when there is a delay in memory limits correction, (for e.g when hugetlb usage changes within consecutive runs of the userspace agent), the system could be in an over/underprotected state.
This patch series rectifies this issue by charging the memcg when the hugetlb folio is allocated, and uncharging when the folio is freed. In addition, a new selftest is added to demonstrate and verify this new behavior.
Nhat Pham (4):
memcontrol: add helpers for hugetlb memcg accounting
memcontrol: only transfer the memcg data for migration
hugetlb: memcg: account hugetlb-backed memory in memory controller
selftests: add a selftest to verify hugetlb usage in memcg
Documentation/admin-guide/cgroup-v2.rst | 29 +++ MAINTAINERS | 2 + include/linux/cgroup-defs.h | 5 + include/linux/memcontrol.h | 37 +++ kernel/cgroup/cgroup.c | 15 +- mm/filemap.c | 2 +- mm/hugetlb.c | 35 ++- mm/memcontrol.c | 139 +++++++++-- mm/migrate.c | 3 +- tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + .../selftests/cgroup/test_hugetlb_memcg.c | 234 ++++++++++++++++++ 12 files changed, 478 insertions(+), 26 deletions(-) create mode 100644 tools/testing/selftests/cgroup/test_hugetlb_memcg.c
Signature · 1 line
2.34.1
PATCH v4 1/4 memcontrol: add helpers for hugetlb memcg accounting
This patch exposes charge committing and cancelling as parts of the memory controller interface. These functionalities are useful when the try_charge() and commit_charge() stages have to be separated by other actions in between (which can fail). One such example is the new hugetlb accounting behavior in the following patch.
The patch also adds a helper function to obtain a reference to the current task's memcg.
--- include/linux/memcontrol.h | 21 ++++++++++++++ mm/memcontrol.c | 59 ++++++++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 12 deletions(-)
include/linux/memcontrol.h +21 -0
@@ -653,6 +653,8 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target,page_counter_read (&memcg->memory); } +void mem_cgroup_commit_charge (struct folio *folio,struct mem_cgroup *memcg); +int __mem_cgroup_charge (struct folio *folio,struct mm_struct *mm,gfp_t gfp);/**
@@ -704,6 +706,8 @@ static inline void mem_cgroup_uncharge_list(struct list_head *page_list)__mem_cgroup_uncharge_list (page_list); } +void mem_cgroup_cancel_charge (struct mem_cgroup *memcg,unsigned int nr_pages); +void mem_cgroup_migrate (struct folio *old,struct folio *new);/**
@@ -760,6 +764,8 @@ struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);struct mem_cgroup *get_mem_cgroup_from_mm (struct mm_struct *mm); +struct mem_cgroup *get_mem_cgroup_from_current (void ); +struct lruvec *folio_lruvec_lock (struct folio *folio);struct lruvec *folio_lruvec_lock_irq (struct folio *folio); struct lruvec *folio_lruvec_lock_irqsave(struct folio *folio,
@@ -1245,6 +1251,11 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target,return false; } +static inline void mem_cgroup_commit_charge (struct folio *folio, +struct mem_cgroup *memcg) +{ +} +static inline int mem_cgroup_charge (struct folio *folio,struct mm_struct *mm,gfp_t gfp) {
@@ -1269,6 +1280,11 @@ static inline void mem_cgroup_uncharge_list(struct list_head *page_list) { } +static inline void mem_cgroup_cancel_charge (struct mem_cgroup *memcg, +unsigned int nr_pages) +{ +} +static inline void mem_cgroup_migrate (struct folio *old,struct folio *new) { }
@@ -1306,6 +1322,11 @@ static inline struct mem_cgroup *get_mem_cgroup_from_mm(struct mm_struct *mm)return NULL ; } +static inline struct mem_cgroup *get_mem_cgroup_from_current (void ) +{ +return NULL ; +} +static inline struct mem_cgroup *mem_cgroup_from_css (struct cgroup_subsys_state *css) {
mm/memcontrol.c +47 -12
100 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 2/4 memcontrol: only transfer the memcg data for migration
In reply to Nhat PhamFor most migration use cases, only transfer the memcg data from the old folio to the new folio, and clear the old folio's memcg data. No charging and uncharging will be done.
This shaves off some work on the migration path, and avoids the temporary double charging of a folio during its migration.
The only exception is replace_page_cache_folio(), which will use the old mem_cgroup_migrate() (now renamed to mem_cgroup_replace_folio). In that context, the isolation of the old page isn't quite as thorough as with migration, so we cannot use our new implementation directly.
This patch is the result of the following discussion on the new hugetlb memcg accounting behavior:
https://lore.kernel.org/lkml/20231003171329.GB314430@monkey/
--- include/linux/memcontrol.h | 7 +++++++ mm/filemap.c | 2 +- mm/memcontrol.c | 40 +++++++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 4 deletions(-)
include/linux/memcontrol.h +7 -0
@@ -708,6 +708,8 @@ static inline void mem_cgroup_uncharge_list(struct list_head *page_list)void mem_cgroup_cancel_charge (struct mem_cgroup *memcg,unsigned int nr_pages); +void mem_cgroup_replace_folio (struct folio *old,struct folio *new); +void mem_cgroup_migrate (struct folio *old,struct folio *new);/**
@@ -1285,6 +1287,11 @@ static inline void mem_cgroup_cancel_charge(struct mem_cgroup *memcg, { } +static inline void mem_cgroup_replace_folio (struct folio *old, +struct folio *new) +{ +} +static inline void mem_cgroup_migrate (struct folio *old,struct folio *new) { }
mm/filemap.c +1 -1
@@ -819,7 +819,7 @@ void replace_page_cache_folio(struct folio *old, struct folio *new) new->mapping = mapping; new->index = offset; -mem_cgroup_migrate (old, new); +mem_cgroup_replace_folio (old, new);xas_lock_irq (&xas);xas_store (&xas, new);
mm/memcontrol.c +37 -3
@@ -7281,16 +7281,17 @@ void __mem_cgroup_uncharge_list(struct list_head *page_list) }/** -* mem_cgroup_migrate - Charge a folio's replacement. +* mem_cgroup_replace_folio - Charge a folio's replacement. * @old: Currently circulating folio. * @new: Replacement folio. * * Charge @new as a replacement folio for @old. @old will -* be uncharged upon free. +* be uncharged upon free. This is only used by the page cache +* (in replace_page_cache_folio()). * * Both folios must be locked, @new->mapping must be set up. */ -void mem_cgroup_migrate (struct folio *old,struct folio *new) +void mem_cgroup_replace_folio (struct folio *old,struct folio *new) {struct mem_cgroup *memcg;long nr_pages =folio_nr_pages (new);
@@ -7329,6 +7330,39 @@ void mem_cgroup_migrate(struct folio *old, struct folio *new)local_irq_restore (flags); } +/** +* mem_cgroup_migrate - Transfer the memcg data from the old to the new folio. +* @old: Currently circulating folio. +* @new: Replacement folio. +* +* Transfer the memcg data from the old folio to the new folio for migration. +* The old folio's data info will be cleared. Note that the memory counters +* will remain unchanged throughout the process. +* +* Both folios must be locked, @new->mapping must be set up. +*/ +void mem_cgroup_migrate (struct folio *old,struct folio *new) +{ +struct mem_cgroup *memcg; + +VM_BUG_ON_FOLIO (!folio_test_locked (old), old); +VM_BUG_ON_FOLIO (!folio_test_locked (new), new); +VM_BUG_ON_FOLIO (folio_test_anon (old) !=folio_test_anon (new), new); +VM_BUG_ON_FOLIO (folio_nr_pages (old) !=folio_nr_pages (new), new); + +if (mem_cgroup_disabled ()) +return ; + + memcg =folio_memcg (old); +VM_WARN_ON_ONCE_FOLIO (!memcg, old); +if (!memcg) +return ; + +/* Transfer the charge and the css ref */ +commit_charge (new, memcg); + old->memcg_data =0 ; +} +DEFINE_STATIC_KEY_FALSE (memcg_sockets_enabled_key);EXPORT_SYMBOL (memcg_sockets_enabled_key);
Signature · 1 line
2.34.1
PATCH v4 3/4 hugetlb: memcg: account hugetlb-backed memory in memory controller
In reply to Nhat PhamCurrently, hugetlb memory usage is not acounted for in the memory controller, which could lead to memory overprotection for cgroups with hugetlb-backed memory. This has been observed in our production system.
For instance, here is one of our usecases: suppose there are two 32G containers. The machine is booted with hugetlb_cma=6G, and each container may or may not use up to 3 gigantic page, depending on the workload within it. The rest is anon, cache, slab, etc. We can set the hugetlb cgroup limit of each cgroup to 3G to enforce hugetlb fairness. But it is very difficult to configure memory.max to keep overall consumption, including anon, cache, slab etc. fair.
What we have had to resort to is to constantly poll hugetlb usage and readjust memory.max. Similar procedure is done to other memory limits (memory.low for e.g). However, this is rather cumbersome and buggy. Furthermore, when there is a delay in memory limits correction, (for e.g when hugetlb usage changes within consecutive runs of the userspace agent), the system could be in an over/underprotected state.
This patch rectifies this issue by charging the memcg when the hugetlb folio is utilized, and uncharging when the folio is freed (analogous to the hugetlb controller). Note that we do not charge when the folio is allocated to the hugetlb pool, because at this point it is not owned by any memcg.
Some caveats to consider:
* This feature is only available on cgroup v2.
* There is no hugetlb pool management involved in the memory controller. As stated above, hugetlb folios are only charged towards the memory controller when it is used. Host overcommit management has to consider it when configuring hard limits.
* Failure to charge towards the memcg results in SIGBUS. This could happen even if the hugetlb pool still has pages (but the cgroup limit is hit and reclaim attempt fails).
* When this feature is enabled, hugetlb pages contribute to memory reclaim protection. low, min limits tuning must take into account hugetlb memory.
* Hugetlb pages utilized while this option is not selected will not be tracked by the memory controller (even if cgroup v2 is remounted later on).
--- Documentation/admin-guide/cgroup-v2.rst | 29 +++++++++++++++++ include/linux/cgroup-defs.h | 5 +++ include/linux/memcontrol.h | 9 ++++++ kernel/cgroup/cgroup.c | 15 ++++++++- mm/hugetlb.c | 35 ++++++++++++++++----- mm/memcontrol.c | 42 ++++++++++++++++++++++++- mm/migrate.c | 3 +- 7 files changed, 127 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +29 -0
@@ -210,6 +210,35 @@ cgroup v2 currently supports the following mount options. relying on the original semantics (e.g. specifying bogusly high 'bypass' protection values at higher tree levels). + memory_hugetlb_accounting + Count HugeTLB memory usage towards the cgroup's overall + memory usage for the memory controller (for the purpose of + statistics reporting and memory protetion). This is a new + behavior that could regress existing setups, so it must be + explicitly opted in with this mount option. + + A few caveats to keep in mind: + + * There is no HugeTLB pool management involved in the memory + controller. The pre-allocated pool does not belong to anyone. + Specifically, when a new HugeTLB folio is allocated to + the pool, it is not accounted for from the perspective of the + memory controller. It is only charged to a cgroup when it is + actually used (for e.g at page fault time). Host memory + overcommit management has to consider this when configuring + hard limits. In general, HugeTLB pool management should be + done via other mechanisms (such as the HugeTLB controller). + * Failure to charge a HugeTLB folio to the memory controller + results in SIGBUS. This could happen even if the HugeTLB pool + still has pages available (but the cgroup limit is hit and + reclaim attempt fails). + * Charging HugeTLB memory towards the memory controller affects + memory protection and reclaim dynamics. Any userspace tuning + (of low, min limits for e.g) needs to take this into account. + * HugeTLB pages utilized while this option is not selected + will not be tracked by the memory controller (even if cgroup + v2 is remounted later on). + Organizing Processes and Threads --------------------------------
include/linux/cgroup-defs.h +5 -0
@@ -115,6 +115,11 @@ enum {* Enable recursive subtree protection */ CGRP_ROOT_MEMORY_RECURSIVE_PROT = (1 <<18 ), + +/* +* Enable hugetlb accounting for the memory controller. +*/ + CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING = (1 << 19), };/* cftype->flags */
include/linux/memcontrol.h +9 -0
@@ -679,6 +679,9 @@ static inline int mem_cgroup_charge(struct folio *folio, struct mm_struct *mm,return __mem_cgroup_charge (folio, mm, gfp); } +int mem_cgroup_hugetlb_try_charge (struct mem_cgroup *memcg,gfp_t gfp, +long nr_pages); +int mem_cgroup_swapin_charge_folio (struct folio *folio,struct mm_struct *mm,gfp_t gfp,swp_entry_t entry);void mem_cgroup_swapin_uncharge_swap (swp_entry_t entry);
@@ -1264,6 +1267,12 @@ static inline int mem_cgroup_charge(struct folio *folio,return 0 ; } +static inline int mem_cgroup_hugetlb_try_charge (struct mem_cgroup *memcg, +gfp_t gfp,long nr_pages) +{ +return 0 ; +} +static inline int mem_cgroup_swapin_charge_folio (struct folio *folio,struct mm_struct *mm,gfp_t gfp,swp_entry_t entry) {
kernel/cgroup/cgroup.c +14 -1
@@ -1902,6 +1902,7 @@ enum cgroup2_param { Opt_favordynmods, Opt_memory_localevents, Opt_memory_recursiveprot, + Opt_memory_hugetlb_accounting, nr__cgroup2_params };
@@ -1910,6 +1911,7 @@ static const struct fs_parameter_spec cgroup2_fs_parameters[] = {fsparam_flag ("favordynmods" , Opt_favordynmods),fsparam_flag ("memory_localevents" , Opt_memory_localevents),fsparam_flag ("memory_recursiveprot" , Opt_memory_recursiveprot), + fsparam_flag("memory_hugetlb_accounting", Opt_memory_hugetlb_accounting), {} };
@@ -1936,6 +1938,9 @@ static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *paramcase Opt_memory_recursiveprot: ctx->flags |=CGRP_ROOT_MEMORY_RECURSIVE_PROT ;return 0 ; +case Opt_memory_hugetlb_accounting: + ctx->flags |=CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING ; +return 0 ; }return -EINVAL ; }
@@ -1960,6 +1965,11 @@ static void apply_cgroup_root_flags(unsigned int root_flags) cgrp_dfl_root.flags |=CGRP_ROOT_MEMORY_RECURSIVE_PROT ; else cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_RECURSIVE_PROT; + +if (root_flags &CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING ) + cgrp_dfl_root.flags |=CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING ; +else + cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING ; } }
@@ -1973,6 +1983,8 @@ static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_rootseq_puts (seq,",memory_localevents" );if (cgrp_dfl_root.flags &CGRP_ROOT_MEMORY_RECURSIVE_PROT )seq_puts (seq,",memory_recursiveprot" ); +if (cgrp_dfl_root.flags &CGRP_ROOT_MEMORY_HUGETLB_ACCOUNTING ) +seq_puts (seq,",memory_hugetlb_accounting" );return 0 ; }
@@ -7050,7 +7062,8 @@ static ssize_t features_show(struct kobject *kobj, struct kobj_attribute *attr,"nsdelegate\n" "favordynmods\n" "memory_localevents\n" - "memory_recursiveprot\n"); +"memory_recursiveprot\n" + "memory_hugetlb_accounting\n"); }static struct kobj_attribute cgroup_features_attr =__ATTR_RO (features);
mm/hugetlb.c +28 -7
@@ -1902,6 +1902,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); +mem_cgroup_uncharge (folio);if (restore_reserve) h->resv_huge_pages++;
@@ -3009,11 +3010,20 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,struct hugepage_subpool *spool =subpool_vma (vma);struct hstate *h =hstate_vma (vma);struct folio *folio; -long map_chg, map_commit; +long map_chg, map_commit, nr_pages =pages_per_huge_page (h);long gbl_chg; -int ret, idx; +int memcg_charge_ret, ret, idx;struct hugetlb_cgroup *h_cg =NULL ; +struct mem_cgroup *memcg;bool deferred_reserve; +gfp_t gfp =htlb_alloc_mask (h) | __GFP_RETRY_MAYFAIL; + + memcg =get_mem_cgroup_from_current (); + memcg_charge_ret =mem_cgroup_hugetlb_try_charge (memcg, gfp, nr_pages); +if (memcg_charge_ret == -ENOMEM ) { +mem_cgroup_put (memcg); +return ERR_PTR (-ENOMEM ); + } idx =hstate_index (h);/*
@@ -3022,8 +3032,12 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,* code of zero indicates a reservation exists (no change). */ map_chg = gbl_chg =vma_needs_reservation (h, vma, addr); -if (map_chg <0 ) +if (map_chg <0 ) { +if (!memcg_charge_ret) +mem_cgroup_cancel_charge (memcg, nr_pages); +mem_cgroup_put (memcg);return ERR_PTR (-ENOMEM ); + }/* * Processes that did not create the mapping will have no
@@ -3034,10 +3048,8 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,*/ if (map_chg || avoid_reserve) { gbl_chg =hugepage_subpool_get_pages (spool,1 ); -if (gbl_chg <0 ) { -vma_end_reservation (h, vma, addr); -return ERR_PTR (-ENOSPC ); - } +if (gbl_chg <0 ) + goto out_end_reservation;/* * Even though there was no reservation in the region/reserve
@@ -3119,6 +3131,11 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma,hugetlb_cgroup_uncharge_folio_rsvd (hstate_index (h),pages_per_huge_page (h), folio); } + +if (!memcg_charge_ret) +mem_cgroup_commit_charge (folio, memcg); +mem_cgroup_put (memcg); +return folio; out_uncharge_cgroup:
@@ -3130,7 +3147,11 @@ struct folio *alloc_hugetlb_folio(struct vm_area_struct *vma, out_subpool_put:if (map_chg || avoid_reserve)hugepage_subpool_put_pages (spool,1 ); +out_end_reservation:vma_end_reservation (h, vma, addr); +if (!memcg_charge_ret) +mem_cgroup_cancel_charge (memcg, nr_pages); +mem_cgroup_put (memcg);return ERR_PTR (-ENOSPC ); }
mm/memcontrol.c +41 -1
54 lines not on this page · raw message
mm/migrate.c +1 -2
@@ -633,8 +633,7 @@ void folio_migrate_flags(struct folio *newfolio, struct folio *folio)folio_copy_owner (newfolio, folio); -if (!folio_test_hugetlb (folio)) -mem_cgroup_migrate (folio, newfolio); +mem_cgroup_migrate (folio, newfolio); }EXPORT_SYMBOL (folio_migrate_flags);
Signature · 1 line
2.34.1
PATCH v4 4/4 selftests: add a selftest to verify hugetlb usage in memcg
In reply to Nhat PhamThis patch add a new kselftest to demonstrate and verify the new hugetlb memcg accounting behavior.
--- MAINTAINERS | 2 + tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + .../selftests/cgroup/test_hugetlb_memcg.c | 234 ++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_hugetlb_memcg.c
MAINTAINERS +2 -0
@@ -5269,6 +5269,7 @@ S: Maintained F: mm/memcontrol.c F: mm/swap_cgroup.c F: tools/testing/selftests/cgroup/memcg_protection.m +F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c F: tools/testing/selftests/cgroup/test_kmem.c F: tools/testing/selftests/cgroup/test_memcontrol.c
@@ -9652,6 +9653,7 @@ F: include/linux/hugetlb.h F: mm/hugetlb.c F: mm/hugetlb_vmemmap.c F: mm/hugetlb_vmemmap.h +F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c HVA ST MEDIA DRIVER M: Jean-Christophe Trotin <[email protected]>
tools/testing/selftests/cgroup/.gitignore +1 -0
@@ -7,4 +7,5 @@ test_kill test_cpu test_cpuset test_zswap +test_hugetlb_memcg wait_inotify
tools/testing/selftests/cgroup/Makefile +2 -0
@@ -14,6 +14,7 @@ TEST_GEN_PROGS += test_kill TEST_GEN_PROGS += test_cpu TEST_GEN_PROGS += test_cpuset TEST_GEN_PROGS += test_zswap +TEST_GEN_PROGS += test_hugetlb_memcg LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
@@ -27,3 +28,4 @@ $(OUTPUT)/test_kill: cgroup_util.c $(OUTPUT)/test_cpu: cgroup_util.c $(OUTPUT)/test_cpuset: cgroup_util.c $(OUTPUT)/test_zswap: cgroup_util.c +$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c
tools/testing/selftests/cgroup/test_hugetlb_memcg.c new file +234 -0
234 lines not on this page · raw message
Signature · 1 line
2.34.1