hugetlb_cgroup: Add hugetlb_cgroup reservation counter
Subsystems (5)
PATCH v10 1/8 hugetlb_cgroup: Add hugetlb_cgroup reservation counter
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
This is patch 1 of an 8 patch series.
Problem:
Currently tasks attempting to reserve more hugetlb memory than is available get a failure at mmap/shmget time. This is thanks to Hugetlbfs Reservations [1]. However, if a task attempts to reserve hugetlb memory only more than its hugetlb_cgroup limit allows, the kernel will allow the mmap/shmget call, but will SIGBUS the task when it attempts to fault the memory in.
We have users hitting their hugetlb_cgroup limits and thus we've been looking at this failure mode. We'd like to improve this behavior such that users violating the hugetlb_cgroup limits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in. This gives the user an opportunity to fallback more gracefully to non-hugetlbfs memory for example.
The underlying problem is that today's hugetlb_cgroup accounting happens at hugetlb memory *fault* time, rather than at *reservation* time. Thus, enforcing the hugetlb_cgroup limit only happens at fault time, and the offending task gets SIGBUS'd.
Proposed Solution:
A new page counter named 'hugetlb.xMB.reservation_[limit|usage|max_usage]_in_bytes'. This counter has slightly different semantics than 'hugetlb.xMB.[limit|usage|max_usage]_in_bytes':
- While usage_in_bytes tracks all *faulted* hugetlb memory, reservation_usage_in_bytes tracks all *reserved* hugetlb memory and hugetlb memory faulted in without a prior reservation.
- If a task attempts to reserve more memory than limit_in_bytes allows, the kernel will allow it to do so. But if a task attempts to reserve more memory than reservation_limit_in_bytes, the kernel will fail this reservation.
This proposal is implemented in this patch series, with tests to verify functionality and show the usage.
Alternatives considered:
1. A new cgroup, instead of only a new page_counter attached to the existing hugetlb_cgroup. Adding a new cgroup seemed like a lot of code duplication with hugetlb_cgroup. Keeping hugetlb related page counters under hugetlb_cgroup seemed cleaner as well.
2. Instead of adding a new counter, we considered adding a sysctl that modifies the behavior of hugetlb.xMB.[limit|usage]_in_bytes, to do accounting at reservation time rather than fault time. Adding a new page_counter seems better as userspace could, if it wants, choose to enforce different cgroups
differently: one via limit_in_bytes, and another via reservation_limit_in_bytes. This could be very useful if you're transitioning how hugetlb memory is partitioned on your system one cgroup at a time, for example. Also, someone may find usage for both limit_in_bytes and reservation_limit_in_bytes concurrently, and this approach gives them the option to do so.
Testing:
- Added tests passing.
- Used libhugetlbfs for regression testing.
[1]: https://www.kernel.org/doc/html/latest/vm/hugetlbfs_reserv.html
---
Changes in v10:
- Renamed reservation_* to resv.*
--- include/linux/hugetlb.h | 4 +- mm/hugetlb_cgroup.c | 115 +++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 15 deletions(-)
include/linux/hugetlb.h +2 -2
@@ -432,8 +432,8 @@ struct hstate {unsigned int surplus_huge_pages_node[MAX_NUMNODES ];#ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ -struct cftype cgroup_files_dfl[5 ]; -struct cftype cgroup_files_legacy[5 ]; +struct cftype cgroup_files_dfl[7 ]; +struct cftype cgroup_files_legacy[9 ];#endif char name[HSTATE_NAME_LEN ]; };
mm/hugetlb_cgroup.c +102 -13
256 lines not on this page · raw message
--
2.25.0.rc1.283.g88dfdc4193-goog
PATCH v10 2/8 hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Augments hugetlb_cgroup_charge_cgroup to be able to charge hugetlb usage or hugetlb reservation counter.
Adds a new interface to uncharge a hugetlb_cgroup counter via hugetlb_cgroup_uncharge_counter.
Integrates the counter with hugetlb_cgroup, via hugetlb_cgroup_init, hugetlb_cgroup_have_usage, and hugetlb_cgroup_css_offline.
---
Changes in v10:
- Added missing VM_BUG_ON
Changes in V9:
- Fixed HUGETLB_CGROUP_MIN_ORDER.
- Minor variable name update.
- Moved some init/cleanup code from later patches in the series to this patch.
- Updated reparenting of reservation accounting.
--- include/linux/hugetlb_cgroup.h | 68 ++++++++++++++--------- mm/hugetlb.c | 19 ++++--- mm/hugetlb_cgroup.c | 99 +++++++++++++++++++++++++--------- 3 files changed, 128 insertions(+), 58 deletions(-)
include/linux/hugetlb_cgroup.h +43 -25
@@ -20,29 +20,37 @@struct hugetlb_cgroup ;/* * Minimum page order trackable by hugetlb cgroup. -* At least 3 pages are necessary for all the tracking information. +* At least 4 pages are necessary for all the tracking information. */ #define HUGETLB_CGROUP_MIN_ORDER 2#ifdef CONFIG_CGROUP_HUGETLB -static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page) +static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page, +bool reserved) {VM_BUG_ON_PAGE (!PageHuge (page), page);if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return NULL ; -return (struct hugetlb_cgroup *)page[2 ].private; +if (reserved) +return (struct hugetlb_cgroup *)page[3 ].private; +else +return (struct hugetlb_cgroup *)page[2 ].private; } -static inline -int set_hugetlb_cgroup (struct page *page,struct hugetlb_cgroup *h_cg) +static inline int set_hugetlb_cgroup (struct page *page, +struct hugetlb_cgroup *h_cg, +bool reservation) {VM_BUG_ON_PAGE (!PageHuge (page), page);if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return -1 ; - page[2 ].private = (unsigned long )h_cg; +if (reservation) + page[3 ].private = (unsigned long )h_cg; +else + page[2 ].private = (unsigned long )h_cg;return 0 ; }
@@ -52,26 +60,34 @@ static inline bool hugetlb_cgroup_disabled(void) }extern int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr); +struct hugetlb_cgroup **ptr, +bool reserved);extern void hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages,struct hugetlb_cgroup *h_cg, -struct page *page); +struct page *page,bool reserved);extern void hugetlb_cgroup_uncharge_page (int idx,unsigned long nr_pages, -struct page *page); +struct page *page,bool reserved); +extern void hugetlb_cgroup_uncharge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup *h_cg); +struct hugetlb_cgroup *h_cg, +bool reserved); +extern void hugetlb_cgroup_uncharge_counter (struct page_counter *p, +unsigned long nr_pages, +struct cgroup_subsys_state *css); +extern void hugetlb_cgroup_file_init (void )__init ;extern void hugetlb_cgroup_migrate (struct page *oldhpage,struct page *newhpage);#else -static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page) +static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page, +bool reserved) {return NULL ; } -static inline -int set_hugetlb_cgroup (struct page *page,struct hugetlb_cgroup *h_cg) +static inline int set_hugetlb_cgroup (struct page *page, +struct hugetlb_cgroup *h_cg,bool reserved) {return 0 ; }
@@ -81,28 +97,30 @@ static inline bool hugetlb_cgroup_disabled(void)return true; } -static inline int -hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +static inline int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, +struct hugetlb_cgroup **ptr, +bool reserved) {return 0 ; } -static inline void -hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages, -struct hugetlb_cgroup *h_cg, -struct page *page) +static inline void hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages, +struct hugetlb_cgroup *h_cg, +struct page *page, +bool reserved) { } -static inline void -hugetlb_cgroup_uncharge_page (int idx,unsigned long nr_pages,struct page *page) +static inline void hugetlb_cgroup_uncharge_page (int idx,unsigned long nr_pages, +struct page *page, +bool reserved) { } -static inline void -hugetlb_cgroup_uncharge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup *h_cg) +static inline void hugetlb_cgroup_uncharge_cgroup (int idx, +unsigned long nr_pages, +struct hugetlb_cgroup *h_cg, +bool reserved) { }
mm/hugetlb.c +12 -7
@@ -1068,7 +1068,8 @@ static void update_and_free_page(struct hstate *h, struct page *page)1 << PG_active |1 << PG_private | 1 << PG_writeback); } -VM_BUG_ON_PAGE (hugetlb_cgroup_from_page (page), page); +VM_BUG_ON_PAGE (hugetlb_cgroup_from_page (page, false), page); +VM_BUG_ON_PAGE (hugetlb_cgroup_from_page (page, true), page);set_compound_page_dtor (page,NULL_COMPOUND_DTOR );set_page_refcounted (page);if (hstate_is_gigantic (h)) {
@@ -1178,8 +1179,8 @@ static void __free_huge_page(struct page *page)spin_lock (&hugetlb_lock);clear_page_huge_active (page); -hugetlb_cgroup_uncharge_page (hstate_index (h), -pages_per_huge_page (h), page); +hugetlb_cgroup_uncharge_page (hstate_index (h),pages_per_huge_page (h), + page, false);if (restore_reserve) h->resv_huge_pages++;
@@ -1253,7 +1254,8 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)INIT_LIST_HEAD (&page->lru);set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );spin_lock (&hugetlb_lock); -set_hugetlb_cgroup (page,NULL ); +set_hugetlb_cgroup (page,NULL , false); +set_hugetlb_cgroup (page,NULL , true); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++;spin_unlock (&hugetlb_lock);
@@ -2039,7 +2041,8 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, gbl_chg =1 ; } - ret =hugetlb_cgroup_charge_cgroup (idx,pages_per_huge_page (h), &h_cg); + ret =hugetlb_cgroup_charge_cgroup (idx,pages_per_huge_page (h), &h_cg, + false);if (ret) goto out_subpool_put;
@@ -2063,7 +2066,8 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,list_move (&page->lru, &h->hugepage_activelist);/* Fall through */ } -hugetlb_cgroup_commit_charge (idx,pages_per_huge_page (h), h_cg, page); +hugetlb_cgroup_commit_charge (idx,pages_per_huge_page (h), h_cg, page, + false);spin_unlock (&hugetlb_lock);set_page_private (page, (unsigned long )spool);
@@ -2087,7 +2091,8 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,return page; out_uncharge_cgroup: -hugetlb_cgroup_uncharge_cgroup (idx,pages_per_huge_page (h), h_cg); +hugetlb_cgroup_uncharge_cgroup (idx,pages_per_huge_page (h), h_cg, + false); out_subpool_put:if (map_chg || avoid_reserve)hugepage_subpool_put_pages (spool,1 );
mm/hugetlb_cgroup.c +73 -26
200 lines not on this page · raw message
--
2.25.0.rc1.283.g88dfdc4193-goog
Re: [PATCH v10 2/8] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Quoted 9 lines · Mina Almasry
Augments
hugetlb_cgroup_charge_cgroupto be able to charge hugetlb usage or hugetlb reservation counter.Adds a new interface to uncharge a
hugetlb_cgroupcounter viahugetlb_cgroup_uncharge_counter.Integrates the counter with
hugetlb_cgroup, viahugetlb_cgroup_init,hugetlb_cgroup_have_usage, andhugetlb_cgroup_css_offline.
Quoted 6 lines
Signed-off-by Mina Almasry <[email protected]>---
Changes in v10:
- Added missingVM_BUG_ON
Thanks for addressing my comments.
I see that patch 8 was updated to address David's comments. I will also review patch 8 later.
I am again requesting that someone with more cgroup experience comment on this patch. I do not have enough experience to determine if the code to not reparent/zombie cgroup is correct.
For now,
while waiting for cgroup comments.
Signature · 1 line
Mike Kravetz
Re: [PATCH v10 2/8] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Quoted 18 lines · Mike Kravetz
Quoted 9 lines · Mina Almasry
Augments
hugetlb_cgroup_charge_cgroupto be able to charge hugetlb usage or hugetlb reservation counter.Adds a new interface to uncharge a
hugetlb_cgroupcounter viahugetlb_cgroup_uncharge_counter.Integrates the counter with
hugetlb_cgroup, viahugetlb_cgroup_init,hugetlb_cgroup_have_usage, andhugetlb_cgroup_css_offline.Quoted 6 lines
Signed-off-by Mina Almasry <[email protected]>---
Changes in v10:
- Added missingVM_BUG_ON
Quoted 8 lines
Thanks for addressing my comments.
I see that patch 8 was updated to address David's comments. I will also review patch 8 later.I am again requesting that someone with more cgroup experience comment on this patch. I do not have enough experience to determine if the code to not reparent/zombie cgroup is correct.
Thank you very much for your continued reviews and I'm sorry I'm having so much trouble getting anyone with cgroup experience to look deeply into this. My guess is that it's not very active feature and the patchseries is long and complex so folks are not interested. Nevertheless I'm continually poking David and Shakeel and they will hopefully continue to look into this.
FWIW, I have a test for repartenting/counting zombies and the tests at least don't uncover any problems.
Quoted 6 lines
For now,
Acked-by Mike Kravetz <[email protected]>while waiting for cgroup comments.
--
Mike Kravetz
Re: [PATCH v10 2/8] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
In reply to Mina Almasryinclude/linux/hugetlb_cgroup.h line 25
Quoted 12 lines · Mina Almasry
include/linux/hugetlb_cgroup.h +1
-1@@ -20,29 +20,37 @@struct hugetlb_cgroup ;/* * Minimum page order trackable by hugetlb cgroup. -* At least 3 pages are necessary for all the tracking information. +* At least 4 pages are necessary for all the tracking information. */ #define HUGETLB_CGROUP_MIN_ORDER 2
I always struggle with a way to document and protect these types of usages. In this case, we are using the private filed of tail pages; in thp code, we enumerate these usages separately in struct page: see "Tail pages of compound page" comment in the union. Using the private field is fine to store a pointer to the hugetlb_cgroup, but I'm wondering if we can document or protect against future patches not understanding this usage. Otherwise it's implicit beyond this comment.
Maybe an expanded comment here is the only thing that is needed because it's unique to struct hugetlb_cgroup that describes what struct page represents for the second, third, and (now) fourth tail page.
Or, we could go even more explicit and define an enum that defines the third tail page is for hugetlb_cgroup limits and fourth is for hugetlb_cgroup reservation limits.
enum {
...
H_CG_LIMIT, H_CG_RESV, MAX_H_CG_PRIVATE, };
and then do a
BUILD_BUG_ON(MAX_H_CG_PRIVATE > (1 << HUGETLB_CGROUP_MIN_ORDER));
somewhere? And then use H_CG_RESV (or a better name) as the offset from the head page in the code?
include/linux/hugetlb_cgroup.h line 30
Quoted 6 lines
include/linux/hugetlb_cgroup.h +2
-1#ifdef CONFIG_CGROUP_HUGETLB -static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page) +static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page, +bool reserved)
When implementing specs, I immediately see "bool reserved" or "int reserved:1" as reserved for future use :) Maybe simple enough to just name these formals as resv?
Otherwise looks like a simple conversion of existing functionality and extension to the new limits.
Re: [PATCH v10 2/8] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Quoted 23 lines · David Rientjes
include/linux/hugetlb_cgroup.h line 25
Quoted 12 lines · Mina Almasry
include/linux/hugetlb_cgroup.h +1
-1@@ -20,29 +20,37 @@struct hugetlb_cgroup ;/* * Minimum page order trackable by hugetlb cgroup. -* At least 3 pages are necessary for all the tracking information. +* At least 4 pages are necessary for all the tracking information. */ #define HUGETLB_CGROUP_MIN_ORDER 2I always struggle with a way to document and protect these types of usages. In this case, we are using the private filed of tail pages; in thp code, we enumerate these usages separately in struct page: see "Tail pages of compound page" comment in the union. Using the private field is fine to store a pointer to the
hugetlb_cgroup, but I'm wondering if we can document or protect against future patches not understanding this usage. Otherwise it's implicit beyond this comment.
Quoted 3 lines
Maybe an expanded comment here is the only thing that is needed because it's unique to struct
hugetlb_cgroupthat describes what struct page represents for the second, third, and (now) fourth tail page.
I think that expanding the comment may be sufficient. Let's at least document what the private field of the of the tail pages are used for WRT cgroups.
Second tail page (hpage[2]) usage cgroup
Third tail page (hpage[3]) reservation cgroup
BTW, we are just adding usage of the third tail page IIUC. The comment that 4 pages are necessary includes the head page.
Signature · 1 line
Mike Kravetz
PATCH v10 3/8 hugetlb_cgroup: add reservation accounting for private mappings
In reply to Mina AlmasryNormally the pointer to the cgroup to uncharge hangs off the struct page, and gets queried when it's time to free the page. With hugetlb_cgroup reservations, this is not possible. Because it's possible for a page to be reserved by one task and actually faulted in by another task.
The best place to put the hugetlb_cgroup pointer to uncharge for reservations is in the resv_map. But, because the resv_map has different semantics for private and shared mappings, the code patch to charge/uncharge shared and private mappings is different. This patch implements charging and uncharging for private mappings.
For private mappings, the counter to uncharge is in resv_map->reservation_counter. On initializing the resv_map this is set to NULL. On reservation of a region in private mapping, the tasks hugetlb_cgroup is charged and the hugetlb_cgroup is placed is resv_map->reservation_counter.
On hugetlb_vm_op_close, we uncharge resv_map->reservation_counter.
---
Changes in v10:
- Fixed cases where the mapping is private but cgroup accounting is disabled. Changes in V9:
- Updated for reparenting of hugetlb reservation accounting.
--- include/linux/hugetlb.h | 10 +++++++++ include/linux/hugetlb_cgroup.h | 27 ++++++++++++++++++++++++ mm/hugetlb.c | 38 +++++++++++++++++++++++++++++++++- mm/hugetlb_cgroup.c | 28 ------------------------- 4 files changed, 74 insertions(+), 29 deletions(-)
include/linux/hugetlb.h +10 -0
@@ -46,6 +46,16 @@ struct resv_map {long adds_in_progress;struct list_head region_cache;long region_cache_count; +#ifdef CONFIG_CGROUP_HUGETLB +/* +* On private mappings, the counter to uncharge reservations is stored +* here. If these fields are 0, then either the mapping is shared, or +* cgroup accounting is disabled for this resv_map. +*/ +struct page_counter *reservation_counter; +unsigned long pages_per_hpage; +struct cgroup_subsys_state *css; +#endif };extern struct resv_map *resv_map_alloc (void );void resv_map_release (struct kref *ref);
include/linux/hugetlb_cgroup.h +27 -0
@@ -25,6 +25,33 @@ struct hugetlb_cgroup;#define HUGETLB_CGROUP_MIN_ORDER 2#ifdef CONFIG_CGROUP_HUGETLB +enum hugetlb_memory_event { +HUGETLB_MAX , +HUGETLB_NR_MEMORY_EVENTS , +}; + +struct hugetlb_cgroup { +struct cgroup_subsys_state css; + +/* +* the counter to account for hugepages from hugetlb. +*/ +struct page_counter hugepage[HUGE_MAX_HSTATE ]; + +/* +* the counter to account for hugepage reservations from hugetlb. +*/ +struct page_counter reserved_hugepage[HUGE_MAX_HSTATE ]; + +atomic_long_t events[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; +atomic_long_t events_local[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; + +/* Handle for "hugetlb.events" */ +struct cgroup_file events_file[HUGE_MAX_HSTATE ]; + +/* Handle for "hugetlb.events.local" */ +struct cgroup_file events_local_file[HUGE_MAX_HSTATE ]; +};static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page,bool reserved)
mm/hugetlb.c +37 -1
@@ -666,6 +666,17 @@ struct resv_map *resv_map_alloc(void)INIT_LIST_HEAD (&resv_map->regions); resv_map->adds_in_progress =0 ; +#ifdef CONFIG_CGROUP_HUGETLB +/* +* Initialize these to 0. On shared mappings, 0's here indicate these +* fields don't do cgroup accounting. On private mappings, these will be +* re-initialized to the proper values, to indicate that hugetlb cgroup +* reservations are to be un-charged from here. +*/ + resv_map->reservation_counter =NULL ; + resv_map->pages_per_hpage =0 ; + resv_map->css =NULL ; +#endif INIT_LIST_HEAD (&resv_map->region_cache);list_add (&rg->link, &resv_map->region_cache);
@@ -3194,7 +3205,11 @@ static void hugetlb_vm_op_close(struct vm_area_struct *vma) reserve = (end - start) -region_count (resv, start, end); -kref_put (&resv->refs, resv_map_release); +#ifdef CONFIG_CGROUP_HUGETLB +hugetlb_cgroup_uncharge_counter (resv->reservation_counter, + (end - start) * resv->pages_per_hpage, + resv->css); +#endif if (reserve) {/*
@@ -3204,6 +3219,8 @@ static void hugetlb_vm_op_close(struct vm_area_struct *vma) gbl_reserve =hugepage_subpool_put_pages (spool, reserve);hugetlb_acct_memory (h, -gbl_reserve); } + +kref_put (&resv->refs, resv_map_release); }static int hugetlb_vm_op_split (struct vm_area_struct *vma,unsigned long addr)
@@ -4550,6 +4567,7 @@ int hugetlb_reserve_pages(struct inode *inode,struct hstate *h =hstate_inode (inode);struct hugepage_subpool *spool =subpool_inode (inode);struct resv_map *resv_map; +struct hugetlb_cgroup *h_cg;long gbl_reserve;/* This should never happen */
@@ -4583,12 +4601,30 @@ int hugetlb_reserve_pages(struct inode *inode, chg =region_chg (resv_map, from, to); }else { +/* Private mapping. */ resv_map =resv_map_alloc ();if (!resv_map)return -ENOMEM ; chg = to - from; +if (hugetlb_cgroup_charge_cgroup (hstate_index (h), + chg *pages_per_huge_page (h), + &h_cg, true)) { +kref_put (&resv_map->refs, resv_map_release); +return -ENOMEM ; + } + +#ifdef CONFIG_CGROUP_HUGETLB +/* +* Since this branch handles private mappings, we attach the +* counter to uncharge for this reservation off resv_map. +*/ + resv_map->reservation_counter = + &h_cg->reserved_hugepage[hstate_index (h)]; + resv_map->pages_per_hpage =pages_per_huge_page (h); +#endif +set_vma_resv_map (vma, resv_map);set_vma_resv_flags (vma,HPAGE_RESV_OWNER ); }
mm/hugetlb_cgroup.c +0 -28
@@ -23,34 +23,6 @@#include <linux/hugetlb.h> #include <linux/hugetlb_cgroup.h> -enum hugetlb_memory_event { -HUGETLB_MAX , -HUGETLB_NR_MEMORY_EVENTS , -}; - -struct hugetlb_cgroup { -struct cgroup_subsys_state css; - -/* -* the counter to account for hugepages from hugetlb. -*/ -struct page_counter hugepage[HUGE_MAX_HSTATE ]; - -/* -* the counter to account for hugepage reservations from hugetlb. -*/ -struct page_counter reserved_hugepage[HUGE_MAX_HSTATE ]; - -atomic_long_t events[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; -atomic_long_t events_local[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; - -/* Handle for "hugetlb.events" */ -struct cgroup_file events_file[HUGE_MAX_HSTATE ]; - -/* Handle for "hugetlb.events.local" */ -struct cgroup_file events_local_file[HUGE_MAX_HSTATE ]; -}; -#define MEMFILE_PRIVATE (x, val) (((x) << 16) | (val))#define MEMFILE_IDX (val) (((val) >> 16) & 0xffff)#define MEMFILE_ATTR (val) ((val) & 0xffff)
--
2.25.0.rc1.283.g88dfdc4193-goog
Re: [PATCH v10 3/8] hugetlb_cgroup: add reservation accounting for private mappings
Quoted 18 lines · Mina Almasry
Normally the pointer to the cgroup to uncharge hangs off the struct page, and gets queried when it's time to free the page. With
hugetlb_cgroupreservations, this is not possible. Because it's possible for a page to be reserved by one task and actually faulted in by another task.The best place to put the
hugetlb_cgrouppointer to uncharge for reservations is in theresv_map. But, because theresv_maphas different semantics for private and shared mappings, the code patch to charge/uncharge shared and private mappings is different. This patch implements charging and uncharging for private mappings.For private mappings, the counter to uncharge is in
resv_map->reservation_counter. On initializing theresv_mapthis is set to NULL. On reservation of a region in private mapping, the taskshugetlb_cgroupis charged and thehugetlb_cgroupis placed isresv_map->reservation_counter.
Quoted 3 lines
On
hugetlb_vm_op_close, we unchargeresv_map->reservation_counter.Signed-off-by Mina Almasry <[email protected]>
Signature · 1 line
Mike Kravetz
Re: [PATCH v10 3/8] hugetlb_cgroup: add reservation accounting for private mappings
In reply to Mina AlmasryQuoted 68 lines · Mina Almasry
include/linux/hugetlb.h +10
-0@@ -46,6 +46,16 @@ struct resv_map {long adds_in_progress;struct list_head region_cache;long region_cache_count; +#ifdef CONFIG_CGROUP_HUGETLB +/* +* On private mappings, the counter to uncharge reservations is stored +* here. If these fields are 0, then either the mapping is shared, or +* cgroup accounting is disabled for this resv_map. +*/ +struct page_counter *reservation_counter; +unsigned long pages_per_hpage; +struct cgroup_subsys_state *css; +#endif };extern struct resv_map *resv_map_alloc (void );void resv_map_release (struct kref *ref);include/linux/hugetlb_cgroup.h +27
-0@@ -25,6 +25,33 @@ struct hugetlb_cgroup;#define HUGETLB_CGROUP_MIN_ORDER 2#ifdef CONFIG_CGROUP_HUGETLB +enum hugetlb_memory_event { +HUGETLB_MAX , +HUGETLB_NR_MEMORY_EVENTS , +}; + +struct hugetlb_cgroup { +struct cgroup_subsys_state css; + +/* +* the counter to account for hugepages from hugetlb. +*/ +struct page_counter hugepage[HUGE_MAX_HSTATE ]; + +/* +* the counter to account for hugepage reservations from hugetlb. +*/ +struct page_counter reserved_hugepage[HUGE_MAX_HSTATE ]; + +atomic_long_t events[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; +atomic_long_t events_local[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; + +/* Handle for "hugetlb.events" */ +struct cgroup_file events_file[HUGE_MAX_HSTATE ]; + +/* Handle for "hugetlb.events.local" */ +struct cgroup_file events_local_file[HUGE_MAX_HSTATE ]; +};static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page,bool reserved)mm/hugetlb.c +1
-0@@ -666,6 +666,17 @@ struct resv_map *resv_map_alloc(void)INIT_LIST_HEAD (&resv_map->regions); resv_map->adds_in_progress =0 ; +#ifdef CONFIG_CGROUP_HUGETLB
mm/hugetlb.c struct resv_map *resv_map_alloc(void) · line 679
Quoted 10 lines
mm/hugetlb.c +10
-0+/* +* Initialize these to 0. On shared mappings, 0's here indicate these +* fields don't do cgroup accounting. On private mappings, these will be +* re-initialized to the proper values, to indicate that hugetlb cgroup +* reservations are to be un-charged from here. +*/ + resv_map->reservation_counter =NULL ; + resv_map->pages_per_hpage =0 ; + resv_map->css =NULL ; +#endif
Might be better to extract out a resv_map_init() that does the initialization when CONFIG_CGROUP_HUGETLB is enabled? Could be used here as well as hugetlb_reserve_pages().
Quoted 6 lines
mm/hugetlb.c +0
-0INIT_LIST_HEAD (&resv_map->region_cache);list_add (&rg->link, &resv_map->region_cache);@@ -3194,7 +3205,11 @@ static void hugetlb_vm_op_close(struct vm_area_struct *vma) reserve = (end - start) -region_count (resv, start, end);
mm/hugetlb.c static void hugetlb_vm_op_close(struct vm_area_struct *vma) · line 3215
Quoted 10 lines
mm/hugetlb.c +5
-1-kref_put (&resv->refs, resv_map_release); +#ifdef CONFIG_CGROUP_HUGETLB +hugetlb_cgroup_uncharge_counter (resv->reservation_counter, + (end - start) * resv->pages_per_hpage, + resv->css); +#endif if (reserve) {/*
Mike has given is Reviewed-by so likely not a big concern for the generic hugetlb code, but I was wondering if we can reduce the number of #ifdef's if we defined a CONFIG_CGROUP_HUGETLB helper to take the resv, end, and start? If CONFIG_CGROUP_HUGETLB is defined, it converts into the above, otherwise it's a no-op and we don't run into any compile errors because we are accessing fields that don't exist without the option.
Otherwise looks good!
Re: [PATCH v10 3/8] hugetlb_cgroup: add reservation accounting for private mappings
Quoted 100 lines · David Rientjes
Quoted 68 lines · Mina Almasry
include/linux/hugetlb.h +10
-0@@ -46,6 +46,16 @@ struct resv_map {long adds_in_progress;struct list_head region_cache;long region_cache_count; +#ifdef CONFIG_CGROUP_HUGETLB +/* +* On private mappings, the counter to uncharge reservations is stored +* here. If these fields are 0, then either the mapping is shared, or +* cgroup accounting is disabled for this resv_map. +*/ +struct page_counter *reservation_counter; +unsigned long pages_per_hpage; +struct cgroup_subsys_state *css; +#endif };extern struct resv_map *resv_map_alloc (void );void resv_map_release (struct kref *ref);include/linux/hugetlb_cgroup.h +27
-0@@ -25,6 +25,33 @@ struct hugetlb_cgroup;#define HUGETLB_CGROUP_MIN_ORDER 2#ifdef CONFIG_CGROUP_HUGETLB +enum hugetlb_memory_event { +HUGETLB_MAX , +HUGETLB_NR_MEMORY_EVENTS , +}; + +struct hugetlb_cgroup { +struct cgroup_subsys_state css; + +/* +* the counter to account for hugepages from hugetlb. +*/ +struct page_counter hugepage[HUGE_MAX_HSTATE ]; + +/* +* the counter to account for hugepage reservations from hugetlb. +*/ +struct page_counter reserved_hugepage[HUGE_MAX_HSTATE ]; + +atomic_long_t events[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; +atomic_long_t events_local[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; + +/* Handle for "hugetlb.events" */ +struct cgroup_file events_file[HUGE_MAX_HSTATE ]; + +/* Handle for "hugetlb.events.local" */ +struct cgroup_file events_local_file[HUGE_MAX_HSTATE ]; +};static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page,bool reserved)mm/hugetlb.c +1
-0@@ -666,6 +666,17 @@ struct resv_map *resv_map_alloc(void)INIT_LIST_HEAD (&resv_map->regions); resv_map->adds_in_progress =0 ; +#ifdef CONFIG_CGROUP_HUGETLB mm/hugetlb.c struct resv_map *resv_map_alloc(void) · line 679
Quoted 10 lines
mm/hugetlb.c +10
-0+/* +* Initialize these to 0. On shared mappings, 0's here indicate these +* fields don't do cgroup accounting. On private mappings, these will be +* re-initialized to the proper values, to indicate that hugetlb cgroup +* reservations are to be un-charged from here. +*/ + resv_map->reservation_counter =NULL ; + resv_map->pages_per_hpage =0 ; + resv_map->css =NULL ; +#endif Might be better to extract out a
resv_map_init()that does the initialization whenCONFIG_CGROUP_HUGETLBis enabled? Could be used here as well ashugetlb_reserve_pages().Quoted 14 lines
mm/hugetlb.c +5
-1INIT_LIST_HEAD (&resv_map->region_cache);list_add (&rg->link, &resv_map->region_cache);@@ -3194,7 +3205,11 @@ static void hugetlb_vm_op_close(struct vm_area_struct *vma) reserve = (end - start) -region_count (resv, start, end); -kref_put (&resv->refs, resv_map_release); +#ifdef CONFIG_CGROUP_HUGETLB +hugetlb_cgroup_uncharge_counter (resv->reservation_counter, + (end - start) * resv->pages_per_hpage, + resv->css); +#endif
Quoted 10 lines
mm/hugetlb.c static void hugetlb_vm_op_close(struct vm_area_struct *vma) · line 3215
Quoted 2 lines
mm/hugetlb.c +0
-0if (reserve) {/* Mike has given is Reviewed-by so likely not a big concern for the generic hugetlb code, but I was wondering if we can reduce the number of #ifdef's if we defined a
CONFIG_CGROUP_HUGETLBhelper to take the resv, end, and start? IfCONFIG_CGROUP_HUGETLBis defined, it converts into the above, otherwise it's a no-op and we don't run into any compile errors because we are accessing fields that don't exist without the option.
Yes wherever possible I refactored the code a bit to remove #ifdefs in the middle of hugetlb logic.
Quoted 3 lines
Otherwise looks good!
Acked-by David Rientjes <[email protected]>
PATCH v10 4/8 hugetlb: disable region_add file_region coalescing
In reply to Mina AlmasryA follow up patch in this series adds hugetlb cgroup uncharge info the file_region entries in resv->regions. The cgroup uncharge info may differ for different regions, so they can no longer be coalesced at region_add time. So, disable region coalescing in region_add in this patch.
Behavior change:
Say a resv_map exists like this [0->1], [2->3], and [5->6].
Then a region_chg/add call comes in region_chg/add(f=0, t=5).
Old code would generate resv->regions: [0->5], [5->6].
New code would generate resv->regions: [0->1], [1->2], [2->3], [3->5], [5->6].
Special care needs to be taken to handle the resv->adds_in_progress variable correctly. In the past, only 1 region would be added for every region_chg and region_add call. But now, each call may add multiple regions, so we can no longer increment adds_in_progress by 1 in region_chg, or decrement adds_in_progress by 1 after region_add or region_abort. Instead, region_chg calls add_reservation_in_range() to count the number of regions needed and allocates those, and that info is passed to region_add and region_abort to decrement adds_in_progress correctly.
We've also modified the assumption that region_add after region_chg never fails. region_chg now pre-allocates at least 1 region for region_add. If region_add needs more regions than region_chg has allocated for it, then it may fail.
---
Changes in v9:
- Added clarifications in the comments and addressed minor issues from code review.
Changes in v7:
- region_chg no longer allocates (t-f) / 2 file_region entries. Changes in v6:
- Fix bug in number of region_caches allocated by region_chg
--- mm/hugetlb.c | 327 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 223 insertions(+), 104 deletions(-)
mm/hugetlb.c +223 -104
@@ -245,110 +245,179 @@ struct file_region {long to; }; +/* Helper that removes a struct file_region from the resv_map cache and returns +* it for use. +*/ +static struct file_region * +get_file_region_entry_from_cache (struct resv_map *resv,long from,long to) +{ +struct file_region *nrg =NULL ; + +VM_BUG_ON (resv->region_cache_count <=0 ); + + resv->region_cache_count--; + nrg = list_first_entry(&resv->region_cache, struct file_region, link); +VM_BUG_ON (!nrg); +list_del (&nrg->link); + + nrg->from = from; + nrg->to = to; + +return nrg; +} +/* Must be called with resv->lock held. Calling this with count_only == true * will count the number of pages to be added but will not modify the linked -* list. +* list. If regions_needed != NULL and count_only == true, then regions_needed +* will indicate the number of file_regions needed in the cache to carry out to +* add the regions for this range. */ static long add_reservation_in_range (struct resv_map *resv,long f,long t, -bool count_only) +long *regions_needed,bool count_only) { -long chg =0 ; +long add =0 ;struct list_head *head = &resv->regions; +long last_accounted_offset = f;struct file_region *rg =NULL , *trg =NULL , *nrg =NULL ; -/* Locate the region we are before or in. */ -list_for_each_entry (rg, head, link) -if (f <= rg->to) -break ; - -/* Round our left edge to the current segment if it encloses us. */ -if (f > rg->from) - f = rg->from; +if (regions_needed) + *regions_needed =0 ; - chg = t - f; +/* In this loop, we essentially handle an entry for the range +* [last_accounted_offset, rg->from), at every iteration, with some +* bounds checking. +*/ +list_for_each_entry_safe (rg, trg, head, link) { +/* Skip irrelevant regions that start before our range. */ +if (rg->from < f) { +/* If this region ends after the last accounted offset, +* then we need to update last_accounted_offset. +*/ +if (rg->to > last_accounted_offset) + last_accounted_offset = rg->to; +continue ; + } -/* Check for and consume any regions we now overlap with. */ - nrg = rg; -list_for_each_entry_safe (rg, trg, rg->link.prev, link) { -if (&rg->link == head) -break ; +/* When we find a region that starts beyond our range, we've +* finished. +*/ if (rg->from > t)break ; -/* We overlap with this area, if it extends further than -* us then we must extend ourselves. Account for its -* existing reservation. +/* Add an entry for last_accounted_offset -> rg->from, and +* update last_accounted_offset. */ -if (rg->to > t) { - chg += rg->to - t; - t = rg->to; +if (rg->from > last_accounted_offset) { + add += rg->from - last_accounted_offset; +if (!count_only) { + nrg =get_file_region_entry_from_cache ( + resv, last_accounted_offset, rg->from); +list_add (&nrg->link, rg->link.prev); + }else if (regions_needed) + *regions_needed +=1 ; } - chg -= rg->to - rg->from; -if (!count_only && rg != nrg) { -list_del (&rg->link); -kfree (rg); - } + last_accounted_offset = rg->to; } -if (!count_only) { - nrg->from = f; - nrg->to = t; +/* Handle the case where our range extends beyond +* last_accounted_offset. +*/ +if (last_accounted_offset < t) { + add += t - last_accounted_offset; +if (!count_only) { + nrg =get_file_region_entry_from_cache ( + resv, last_accounted_offset, t); +list_add (&nrg->link, rg->link.prev); + }else if (regions_needed) + *regions_needed +=1 ; } -return chg; +return add; }/* * Add the huge page range represented by [f, t) to the reserve -* map. Existing regions will be expanded to accommodate the specified -* range, or a region will be taken from the cache. Sufficient regions -* must exist in the cache due to the previous call to region_chg with -* the same range. +* map. Regions will be taken from the cache to fill in this range. +* Sufficient regions should exist in the cache due to the previous +* call to region_chg with the same range, but in some cases the cache will not +* have sufficient entries due to races with other code doing region_add or +* region_del. The extra needed entries will be allocated. * -* Return the number of new huge pages added to the map. This -* number is greater than or equal to zero. +* regions_needed is the out value provided by a previous call to region_chg. +* +* Return the number of new huge pages added to the map. This number is greater +* than or equal to zero. If file_region entries needed to be allocated for +* this operation and we were not able to allocate, it ruturns -ENOMEM. +* region_add of regions of length 1 never allocate file_regions and cannot +* fail; region_chg will always allocate at least 1 entry and a region_add for +* 1 page will only require at most 1 entry. */ -static long region_add (struct resv_map *resv,long f,long t) +static long region_add (struct resv_map *resv,long f,long t, +long in_regions_needed) { -struct list_head *head = &resv->regions; -struct file_region *rg, *nrg; -long add =0 ; +long add =0 , actual_regions_needed =0 , i =0 ; +struct file_region *trg =NULL , *rg =NULL ; +struct list_head allocated_regions; + +INIT_LIST_HEAD (&allocated_regions);spin_lock (&resv->lock); -/* Locate the region we are either in or before. */ -list_for_each_entry (rg, head, link) -if (f <= rg->to) -break ; +retry: + +/* Count how many regions are actually needed to execute this add. */ +add_reservation_in_range (resv, f, t, &actual_regions_needed, true);/* -* If no region exists which can be expanded to include the -* specified range, pull a region descriptor from the cache -* and use it for this range. +* Check for sufficient descriptors in the cache to accommodate +* this add operation. Note that actual_regions_needed may be greater +* than in_regions_needed. In this case, we need to make sure that we +* allocate extra entries, such that we have enough for all the +* existing adds_in_progress, plus the excess needed for this +* operation. */ -if (&rg->link == head || t < rg->from) { -VM_BUG_ON (resv->region_cache_count <=0 ); +if (resv->region_cache_count < + resv->adds_in_progress + + (actual_regions_needed - in_regions_needed)) { +/* region_add operation of range 1 should never need to +* allocate file_region entries. +*/ +VM_BUG_ON (t - f <=1 ); - resv->region_cache_count--; - nrg = list_first_entry(&resv->region_cache, struct file_region, - link); -list_del (&nrg->link); +/* Must drop lock to allocate a new descriptor. */ +spin_unlock (&resv->lock); +for (i =0 ; i < (actual_regions_needed - in_regions_needed); + i++) { + trg =kmalloc (sizeof (*trg),GFP_KERNEL ); +if (!trg) + goto out_of_memory; +list_add (&trg->link, &allocated_regions); + } +spin_lock (&resv->lock); - nrg->from = f; - nrg->to = t; -list_add (&nrg->link, rg->link.prev); +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +list_add (&rg->link, &resv->region_cache); + resv->region_cache_count++; + } - add += t - f; - goto out_locked; + goto retry; } - add =add_reservation_in_range (resv, f, t, false); + add =add_reservation_in_range (resv, f, t,NULL , false); + + resv->adds_in_progress -= in_regions_needed; -out_locked: - resv->adds_in_progress--;spin_unlock (&resv->lock);VM_BUG_ON (add <0 );return add; + +out_of_memory: +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +kfree (rg); + } +return -ENOMEM ; }/*
@@ -358,49 +427,79 @@ static long region_add(struct resv_map *resv, long f, long t)* call to region_add that will actually modify the reserve * map to add the specified range [f, t). region_chg does * not change the number of huge pages represented by the -* map. A new file_region structure is added to the cache -* as a placeholder, so that the subsequent region_add -* call will have all the regions it needs and will not fail. +* map. A number of new file_region structures is added to the cache as a +* placeholder, for the subsequent region_add call to use. At least 1 +* file_region structure is added. +* +* out_regions_needed is the number of regions added to the +* resv->adds_in_progress. This value needs to be provided to a follow up call +* to region_add or region_abort for proper accounting. * * Returns the number of huge pages that need to be added to the existing * reservation map for the range [f, t). This number is greater or equal to * zero. -ENOMEM is returned if a new file_region structure or cache entry * is needed and can not be allocated. */ -static long region_chg (struct resv_map *resv,long f,long t) +static long region_chg (struct resv_map *resv,long f,long t, +long *out_regions_needed) { -long chg =0 ; +struct file_region *trg =NULL , *rg =NULL ; +long chg =0 , i =0 , to_allocate =0 ; +struct list_head allocated_regions; + +INIT_LIST_HEAD (&allocated_regions);spin_lock (&resv->lock); -retry_locked: - resv->adds_in_progress++; + +/* Count how many hugepages in this range are NOT respresented. */ + chg =add_reservation_in_range (resv, f, t, out_regions_needed, true); + +if (*out_regions_needed ==0 ) + *out_regions_needed =1 ; + + resv->adds_in_progress += *out_regions_needed;/* * Check for sufficient descriptors in the cache to accommodate * the number of in progress add operations. */ -if (resv->adds_in_progress > resv->region_cache_count) { -struct file_region *trg; - -VM_BUG_ON (resv->adds_in_progress - resv->region_cache_count >1 ); -/* Must drop lock to allocate a new descriptor. */ - resv->adds_in_progress--; +while (resv->region_cache_count < resv->adds_in_progress) { + to_allocate = resv->adds_in_progress - resv->region_cache_count; + +/* Must drop lock to allocate a new descriptor. Note that even +* though we drop the lock here, we do not make another call to +* add_reservation_in_range after re-acquiring the lock. +* Essentially this branch makes sure that we have enough +* descriptors in the cache as suggested by the first call to +* add_reservation_in_range. If more regions turn out to be +* required, region_add will deal with it. +*/ spin_unlock (&resv->lock); - - trg =kmalloc (sizeof (*trg),GFP_KERNEL ); -if (!trg) -return -ENOMEM ; +for (i =0 ; i < to_allocate; i++) { + trg =kmalloc (sizeof (*trg),GFP_KERNEL ); +if (!trg) + goto out_of_memory; +list_add (&trg->link, &allocated_regions); + }spin_lock (&resv->lock); -list_add (&trg->link, &resv->region_cache); - resv->region_cache_count++; - goto retry_locked; - } - chg =add_reservation_in_range (resv, f, t, true); +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +list_add (&rg->link, &resv->region_cache); + resv->region_cache_count++; + } + }spin_unlock (&resv->lock);return chg; + +out_of_memory: +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +kfree (rg); + } +return -ENOMEM ; }/*
@@ -408,17 +507,20 @@ static long region_chg(struct resv_map *resv, long f, long t)* of the resv_map keeps track of the operations in progress between * calls to region_chg and region_add. Operations are sometimes * aborted after the call to region_chg. In such cases, region_abort -* is called to decrement the adds_in_progress counter. +* is called to decrement the adds_in_progress counter. regions_needed +* is the value returned by the region_chg call, it is used to decrement +* the adds_in_progress counter. * * NOTE: The range arguments [f, t) are not needed or used in this * routine. They are kept to make reading the calling code easier as * arguments will match the associated region_chg call. */ -static void region_abort (struct resv_map *resv,long f,long t) +static void region_abort (struct resv_map *resv,long f,long t, +long regions_needed) {spin_lock (&resv->lock);VM_BUG_ON (!resv->region_cache_count); - resv->adds_in_progress--; + resv->adds_in_progress -= regions_needed;spin_unlock (&resv->lock); }
@@ -1883,6 +1985,7 @@ static long __vma_reservation_common(struct hstate *h,struct resv_map *resv;pgoff_t idx;long ret; +long dummy_out_regions_needed; resv =vma_resv_map (vma);if (!resv)
@@ -1891,20 +1994,29 @@ static long __vma_reservation_common(struct hstate *h, idx =vma_hugecache_offset (h, vma, addr);switch (mode) {case VMA_NEEDS_RESV : - ret =region_chg (resv, idx, idx +1 ); + ret =region_chg (resv, idx, idx +1 , &dummy_out_regions_needed); +/* We assume that vma_reservation_* routines always operate on +* 1 page, and that adding to resv map a 1 page entry can only +* ever require 1 region. +*/ +VM_BUG_ON (dummy_out_regions_needed !=1 );break ;case VMA_COMMIT_RESV : - ret =region_add (resv, idx, idx +1 ); + ret =region_add (resv, idx, idx +1 ,1 ); +/* region_add calls of range 1 should never fail. */ +VM_BUG_ON (ret <0 );break ;case VMA_END_RESV : -region_abort (resv, idx, idx +1 ); +region_abort (resv, idx, idx +1 ,1 ); ret =0 ;break ;case VMA_ADD_RESV : -if (vma->vm_flags &VM_MAYSHARE ) - ret =region_add (resv, idx, idx +1 ); -else { -region_abort (resv, idx, idx +1 ); +if (vma->vm_flags &VM_MAYSHARE ) { + ret =region_add (resv, idx, idx +1 ,1 ); +/* region_add calls of range 1 should never fail. */ +VM_BUG_ON (ret <0 ); + }else { +region_abort (resv, idx, idx +1 ,1 ); ret =region_del (resv, idx, idx +1 ); }break ;
@@ -4563,12 +4675,12 @@ int hugetlb_reserve_pages(struct inode *inode,struct vm_area_struct *vma, vm_flags_t vm_flags) { - long ret, chg; + long ret, chg, add = -1;struct hstate *h =hstate_inode (inode);struct hugepage_subpool *spool =subpool_inode (inode);struct resv_map *resv_map;struct hugetlb_cgroup *h_cg; -long gbl_reserve; +long gbl_reserve, regions_needed =0 ;/* This should never happen */ if (from > to) {
@@ -4598,7 +4710,7 @@ int hugetlb_reserve_pages(struct inode *inode,*/ resv_map =inode_resv_map (inode); - chg =region_chg (resv_map, from, to); + chg =region_chg (resv_map, from, to, ®ions_needed); }else {/* Private mapping. */
@@ -4668,9 +4780,14 @@ int hugetlb_reserve_pages(struct inode *inode,* else has to be done for private mappings here */ if (!vma || vma->vm_flags &VM_MAYSHARE ) { -long add =region_add (resv_map, from, to); - -if (unlikely (chg > add)) { + add =region_add (resv_map, from, to, regions_needed); + +if (unlikely (add <0 )) { +hugetlb_acct_memory (h, -gbl_reserve); +/* put back original number of pages, chg */ + (void )hugepage_subpool_put_pages (spool, chg); + goto out_err; + }else if (unlikely (chg > add)) {/* * pages in this range were added to the reserve * map between region_chg and region_add. This
@@ -4688,9 +4805,11 @@ int hugetlb_reserve_pages(struct inode *inode,return 0 ; out_err:if (!vma || vma->vm_flags &VM_MAYSHARE ) -/* Don't call region_abort if region_chg failed */ -if (chg >=0 ) -region_abort (resv_map, from, to); +/* Only call region_abort if the region_chg succeeded but the +* region_add failed or didn't run. +*/ +if (chg >=0 && add <0 ) +region_abort (resv_map, from, to, regions_needed);if (vma &&is_vma_resv_set (vma,HPAGE_RESV_OWNER ))kref_put (&resv_map->refs, resv_map_release);return ret;
--
2.25.0.rc1.283.g88dfdc4193-goog
Re: [PATCH v10 4/8] hugetlb: disable region_add file_region coalescing
Quoted 25 lines · Mina Almasry
A follow up patch in this series adds hugetlb cgroup uncharge info the
file_regionentries inresv->regions. The cgroup uncharge info may differ for different regions, so they can no longer be coalesced atregion_addtime. So, disable region coalescing inregion_addin this patch.Behavior change:
Say a
resv_mapexists like this [0->1], [2->3], and [5->6].Then a region_chg/add call comes in region_chg/add(f=0, t=5).
Old code would generate
resv->regions: [0->5], [5->6].
New code would generateresv->regions: [0->1], [1->2], [2->3], [3->5], [5->6].Special care needs to be taken to handle the
resv->adds_in_progressvariable correctly. In the past, only 1 region would be added for everyregion_chgandregion_addcall. But now, each call may add multiple regions, so we can no longer incrementadds_in_progressby 1 inregion_chg, or decrementadds_in_progressby 1 afterregion_addorregion_abort. Instead,region_chgcallsadd_reservation_in_range()to count the number of regions needed and allocates those, and that info is passed toregion_addandregion_abortto decrementadds_in_progresscorrectly.
Quoted 4 lines
We've also modified the assumption that
region_addafterregion_chgnever fails.region_chgnow pre-allocates at least 1 region forregion_add. Ifregion_addneeds more regions thanregion_chghas allocated for it, then it may fail.
Some time back we briefly discussed an optimization to coalesce file region entries if they were from the same cgroup. At the time, the thought was that such an optimization could wait. For large mappings, known users will reserve the entire area. Smaller mappings such as those in the commit log are not the common case and are mentioned mostly to illustrate what the code must handle.
However, I just remembered that for private mappings file region entries are allocated at page fault time: one per page. Since we are no longer coalescing, there will be one file region struct for each page in a private mapping. Is that correct?
I honestly do not know how common private mappings are today. But, this would cause excessive overhead for any large private mapping.
Signature · 1 line
Mike Kravetz
PATCH v10 5/8 hugetlb_cgroup: add accounting for shared mappings
In reply to Mina AlmasryFor shared mappings, the pointer to the hugetlb_cgroup to uncharge lives in the resv_map entries, in file_region->reservation_counter.
After a call to region_chg, we charge the approprate hugetlb_cgroup, and if successful, we pass on the hugetlb_cgroup info to a follow up region_add call. When a file_region entry is added to the resv_map via region_add, we put the pointer to that cgroup in file_region->reservation_counter. If charging doesn't succeed, we report the error to the caller, so that the kernel fails the reservation.
On region_del, which is when the hugetlb memory is unreserved, we also uncharge the file_region->reservation_counter.
---
Changes in v10:
- Deleted duplicated code snippet.
Changes in V9:
- Updated for hugetlb reservation repareting.
--- mm/hugetlb.c | 156 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 124 insertions(+), 32 deletions(-)
mm/hugetlb.c +124 -32
@@ -243,6 +243,16 @@ struct file_region {struct list_head link;long from;long to; +#ifdef CONFIG_CGROUP_HUGETLB +/* +* On shared mappings, each reserved region appears as a struct +* file_region in resv_map. These fields hold the info needed to +* uncharge each reservation. +*/ +struct page_counter *reservation_counter; +unsigned long pages_per_hpage; +struct cgroup_subsys_state *css; +#endif };/* Helper that removes a struct file_region from the resv_map cache and returns
@@ -266,6 +276,25 @@ get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)return nrg; } +/* Helper that records hugetlb_cgroup uncharge info. */ +static void record_hugetlb_cgroup_uncharge_info (struct hugetlb_cgroup *h_cg, +struct file_region *nrg, +struct hstate *h) +{ +#ifdef CONFIG_CGROUP_HUGETLB +if (h_cg) { + nrg->reservation_counter = + &h_cg->reserved_hugepage[hstate_index (h)]; + nrg->pages_per_hpage =pages_per_huge_page (h); + nrg->css = &h_cg->css; + }else { + nrg->reservation_counter =NULL ; + nrg->pages_per_hpage =0 ; + nrg->css =NULL ; + } +#endif +} +/* Must be called with resv->lock held. Calling this with count_only == true * will count the number of pages to be added but will not modify the linked * list. If regions_needed != NULL and count_only == true, then regions_needed
@@ -273,7 +302,9 @@ get_file_region_entry_from_cache(struct resv_map *resv, long from, long to)* add the regions for this range. */ static long add_reservation_in_range (struct resv_map *resv,long f,long t, -long *regions_needed,bool count_only) +struct hugetlb_cgroup *h_cg, +struct hstate *h,long *regions_needed, +bool count_only) {long add =0 ;struct list_head *head = &resv->regions;
@@ -312,6 +343,8 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,if (!count_only) { nrg =get_file_region_entry_from_cache ( resv, last_accounted_offset, rg->from); +record_hugetlb_cgroup_uncharge_info (h_cg, nrg, + h);list_add (&nrg->link, rg->link.prev); }else if (regions_needed) *regions_needed +=1 ;
@@ -328,11 +361,13 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,if (!count_only) { nrg =get_file_region_entry_from_cache ( resv, last_accounted_offset, t); +record_hugetlb_cgroup_uncharge_info (h_cg, nrg, h);list_add (&nrg->link, rg->link.prev); }else if (regions_needed) *regions_needed +=1 ; } +VM_BUG_ON (add <0 );return add; }
@@ -353,7 +388,8 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,* fail; region_chg will always allocate at least 1 entry and a region_add for * 1 page will only require at most 1 entry. */ -static long region_add (struct resv_map *resv,long f,long t, +static long region_add (struct hstate *h,struct hugetlb_cgroup *h_cg, +struct resv_map *resv,long f,long t,long in_regions_needed) {long add =0 , actual_regions_needed =0 , i =0 ;
@@ -366,7 +402,8 @@ static long region_add(struct resv_map *resv, long f, long t, retry:/* Count how many regions are actually needed to execute this add. */ -add_reservation_in_range (resv, f, t, &actual_regions_needed, true); +add_reservation_in_range (resv, f, t,NULL ,NULL , &actual_regions_needed, + true);/* * Check for sufficient descriptors in the cache to accommodate
@@ -404,7 +441,7 @@ static long region_add(struct resv_map *resv, long f, long t, goto retry; } - add =add_reservation_in_range (resv, f, t,NULL , false); + add =add_reservation_in_range (resv, f, t, h_cg, h,NULL , false); resv->adds_in_progress -= in_regions_needed;
@@ -452,7 +489,8 @@ static long region_chg(struct resv_map *resv, long f, long t,spin_lock (&resv->lock);/* Count how many hugepages in this range are NOT respresented. */ - chg =add_reservation_in_range (resv, f, t, out_regions_needed, true); + chg =add_reservation_in_range (resv, f, t,NULL ,NULL , + out_regions_needed, true);if (*out_regions_needed ==0 ) *out_regions_needed =1 ;
@@ -524,6 +562,29 @@ static void region_abort(struct resv_map *resv, long f, long t,spin_unlock (&resv->lock); } +static void uncharge_cgroup_if_shared_mapping (struct resv_map *resv, +struct file_region *rg, +unsigned long nr_pages) +{ +#ifdef CONFIG_CGROUP_HUGETLB +/* +* If resv->reservation_counter is NULL, then this is either a shared +* reservation, or cgroup charging is disabled on this resv_map. +* +* If the cgroup charging is disabled, then rg->reservation_counter is +* NULL and the uncharge counter call is a no-op. If the mapping is +* shared then the reserved memory is tracked in the file_struct +* entries inside of resv_map. So we need to uncharge the memory here. +*/ +if (rg->reservation_counter && rg->pages_per_hpage && nr_pages >0 && + !resv->reservation_counter) { +hugetlb_cgroup_uncharge_counter (rg->reservation_counter, + nr_pages * rg->pages_per_hpage, + rg->css); + } +#endif +} +/* * Delete the specified range [f, t) from the reserve map. If the * t parameter is LONG_MAX, this indicates that ALL regions after f
@@ -588,11 +649,22 @@ static long region_del(struct resv_map *resv, long f, long t)/* New entry for end of split region */ nrg->from = t; nrg->to = rg->to; + +#ifdef CONFIG_CGROUP_HUGETLB + nrg->reservation_counter = rg->reservation_counter; + nrg->pages_per_hpage = rg->pages_per_hpage; + nrg->css = rg->css; +css_get (rg->css); +#endif +INIT_LIST_HEAD (&nrg->link);/* Original entry is trimmed */ rg->to = f; +uncharge_cgroup_if_shared_mapping (resv, rg, + nrg->to - nrg->from); +list_add (&nrg->link, &rg->link); nrg =NULL ;break ;
@@ -600,6 +672,8 @@ static long region_del(struct resv_map *resv, long f, long t)if (f <= rg->from && t >= rg->to) {/* Remove entire region */ del += rg->to - rg->from; +uncharge_cgroup_if_shared_mapping (resv, rg, + rg->to - rg->from);list_del (&rg->link);kfree (rg);continue ;
@@ -608,14 +682,20 @@ static long region_del(struct resv_map *resv, long f, long t)if (f <= rg->from) {/* Trim beginning of region */ del += t - rg->from; rg->from = t; + +uncharge_cgroup_if_shared_mapping (resv, rg, + t - rg->from); }else {/* Trim end of region */ del += rg->to - f; rg->to = f; + +uncharge_cgroup_if_shared_mapping (resv, rg, rg->to - f); } }spin_unlock (&resv->lock);kfree (nrg); +return del; }
@@ -2002,7 +2082,7 @@ static long __vma_reservation_common(struct hstate *h,VM_BUG_ON (dummy_out_regions_needed !=1 );break ;case VMA_COMMIT_RESV : - ret =region_add (resv, idx, idx +1 ,1 ); + ret =region_add (NULL ,NULL , resv, idx, idx +1 ,1 );/* region_add calls of range 1 should never fail. */ VM_BUG_ON (ret <0 );break ;
@@ -2012,7 +2092,7 @@ static long __vma_reservation_common(struct hstate *h,break ;case VMA_ADD_RESV :if (vma->vm_flags &VM_MAYSHARE ) { - ret =region_add (resv, idx, idx +1 ,1 ); + ret =region_add (NULL ,NULL , resv, idx, idx +1 ,1 );/* region_add calls of range 1 should never fail. */ VM_BUG_ON (ret <0 ); }else {
@@ -4679,7 +4759,7 @@ int hugetlb_reserve_pages(struct inode *inode,struct hstate *h =hstate_inode (inode);struct hugepage_subpool *spool =subpool_inode (inode);struct resv_map *resv_map; -struct hugetlb_cgroup *h_cg; +struct hugetlb_cgroup *h_cg =NULL ;long gbl_reserve, regions_needed =0 ;/* This should never happen */
@@ -4720,23 +4800,6 @@ int hugetlb_reserve_pages(struct inode *inode, chg = to - from; -if (hugetlb_cgroup_charge_cgroup (hstate_index (h), - chg *pages_per_huge_page (h), - &h_cg, true)) { -kref_put (&resv_map->refs, resv_map_release); -return -ENOMEM ; - } - -#ifdef CONFIG_CGROUP_HUGETLB -/* -* Since this branch handles private mappings, we attach the -* counter to uncharge for this reservation off resv_map. -*/ - resv_map->reservation_counter = - &h_cg->reserved_hugepage[hstate_index (h)]; - resv_map->pages_per_hpage =pages_per_huge_page (h); -#endif -set_vma_resv_map (vma, resv_map);set_vma_resv_flags (vma,HPAGE_RESV_OWNER ); }
@@ -4746,6 +4809,26 @@ int hugetlb_reserve_pages(struct inode *inode, goto out_err; } + ret =hugetlb_cgroup_charge_cgroup ( +hstate_index (h), chg *pages_per_huge_page (h), &h_cg, true); + +if (ret <0 ) { + ret = -ENOMEM ; + goto out_err; + } + +#ifdef CONFIG_CGROUP_HUGETLB +if (vma && !(vma->vm_flags &VM_MAYSHARE ) && h_cg) { +/* For private mappings, the hugetlb_cgroup uncharge info hangs +* of the resv_map. +*/ + resv_map->reservation_counter = + &h_cg->reserved_hugepage[hstate_index (h)]; + resv_map->pages_per_hpage =pages_per_huge_page (h); + resv_map->css = &h_cg->css; + } +#endif +/* * There must be enough pages in the subpool for the mapping. If * the subpool has a minimum size, there may be some global
@@ -4754,7 +4837,7 @@ int hugetlb_reserve_pages(struct inode *inode, gbl_reserve =hugepage_subpool_get_pages (spool, chg);if (gbl_reserve <0 ) { ret = -ENOSPC ; - goto out_err; + goto out_uncharge_cgroup; }/*
@@ -4763,9 +4846,7 @@ int hugetlb_reserve_pages(struct inode *inode,*/ ret =hugetlb_acct_memory (h, gbl_reserve);if (ret <0 ) { -/* put back original number of pages, chg */ - (void )hugepage_subpool_put_pages (spool, chg); - goto out_err; + goto out_put_pages; }/*
@@ -4780,7 +4861,7 @@ int hugetlb_reserve_pages(struct inode *inode,* else has to be done for private mappings here */ if (!vma || vma->vm_flags &VM_MAYSHARE ) { - add =region_add (resv_map, from, to, regions_needed); + add =region_add (h, h_cg, resv_map, from, to, regions_needed);if (unlikely (add <0 )) {hugetlb_acct_memory (h, -gbl_reserve);
@@ -4797,12 +4878,23 @@ int hugetlb_reserve_pages(struct inode *inode,*/ long rsv_adjust; - rsv_adjust =hugepage_subpool_put_pages (spool, - chg - add); +hugetlb_cgroup_uncharge_cgroup ( +hstate_index (h), + (chg - add) *pages_per_huge_page (h), h_cg, + true); + + rsv_adjust = +hugepage_subpool_put_pages (spool, chg - add);hugetlb_acct_memory (h, -rsv_adjust); } }return 0 ; +out_put_pages: +/* put back original number of pages, chg */ + (void )hugepage_subpool_put_pages (spool, chg); +out_uncharge_cgroup: +hugetlb_cgroup_uncharge_cgroup ( +hstate_index (h), chg *pages_per_huge_page (h), h_cg, true); out_err:if (!vma || vma->vm_flags &VM_MAYSHARE )/* Only call region_abort if the region_chg succeeded but the
--
2.25.0.rc1.283.g88dfdc4193-goog
PATCH v10 6/8 hugetlb_cgroup: support noreserve mappings
In reply to Mina AlmasrySupport MAP_NORESERVE accounting as part of the new counter.
For each hugepage allocation, at allocation time we check if there is a reservation for this allocation or not. If there is a reservation for this allocation, then this allocation was charged at reservation time, and we don't re-account it. If there is no reserevation for this allocation, we charge the appropriate hugetlb_cgroup.
The hugetlb_cgroup to uncharge for this allocation is stored in page[3].private. We use new APIs added in an earlier patch to set this pointer.
---
Changes in v10:
- Refactored deferred_reserve check.
--- mm/hugetlb.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
mm/hugetlb.c +27 -1
@@ -1374,6 +1374,9 @@ static void __free_huge_page(struct page *page)clear_page_huge_active (page);hugetlb_cgroup_uncharge_page (hstate_index (h),pages_per_huge_page (h), page, false); +hugetlb_cgroup_uncharge_page (hstate_index (h),pages_per_huge_page (h), + page, true); +if (restore_reserve) h->resv_huge_pages++;
@@ -2207,6 +2210,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,long gbl_chg;int ret, idx;struct hugetlb_cgroup *h_cg; +bool deferred_reserve; idx =hstate_index (h);/*
@@ -2244,10 +2248,20 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, gbl_chg =1 ; } +/* If this allocation is not consuming a reservation, charge it now. +*/ + deferred_reserve = map_chg || avoid_reserve || !vma_resv_map (vma); +if (deferred_reserve) { + ret =hugetlb_cgroup_charge_cgroup (idx,pages_per_huge_page (h), + &h_cg, true); +if (ret) + goto out_subpool_put; + } + ret =hugetlb_cgroup_charge_cgroup (idx,pages_per_huge_page (h), &h_cg, false);if (ret) - goto out_subpool_put; + goto out_uncharge_cgroup_reservation;spin_lock (&hugetlb_lock);/*
@@ -2271,6 +2285,14 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, }hugetlb_cgroup_commit_charge (idx,pages_per_huge_page (h), h_cg, page, false); +/* If allocation is not consuming a reservation, also store the +* hugetlb_cgroup pointer on the page. +*/ +if (deferred_reserve) { +hugetlb_cgroup_commit_charge (idx,pages_per_huge_page (h), h_cg, + page, true); + } +spin_unlock (&hugetlb_lock);set_page_private (page, (unsigned long )spool);
@@ -2296,6 +2318,10 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, out_uncharge_cgroup:hugetlb_cgroup_uncharge_cgroup (idx,pages_per_huge_page (h), h_cg, false); +out_uncharge_cgroup_reservation: +if (deferred_reserve) +hugetlb_cgroup_uncharge_cgroup (idx,pages_per_huge_page (h), + h_cg, true); out_subpool_put:if (map_chg || avoid_reserve)hugepage_subpool_put_pages (spool,1 );
PATCH v10 7/8 hugetlb_cgroup: Add hugetlb_cgroup reservation tests
In reply to Mina AlmasryThe tests use both shared and private mapped hugetlb memory, and monitors the hugetlb usage counter as well as the hugetlb reservation counter. They test different configurations such as hugetlb memory usage via hugetlbfs, or MAP_HUGETLB, or shmget/shmat, and with and without MAP_POPULATE.
Also add test for hugetlb reservation reparenting, since this is a subtle issue.
---
Changes in v10:
- Updated tests to resv.* name changes.
Changes in v9:
- Added tests for hugetlb reparenting.
- Make tests explicitly support cgroup v1 and v2 via script argument. Changes in v6:
- Updates tests for cgroups-v2 and NORESERVE allocations.
--- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + .../selftests/vm/charge_reserved_hugetlb.sh | 557 ++++++++++++++++++ .../selftests/vm/hugetlb_reparenting_test.sh | 235 ++++++++ .../selftests/vm/write_hugetlb_memory.sh | 23 + .../testing/selftests/vm/write_to_hugetlbfs.c | 261 ++++++++ 6 files changed, 1078 insertions(+) create mode 100755 tools/testing/selftests/vm/charge_reserved_hugetlb.sh create mode 100755 tools/testing/selftests/vm/hugetlb_reparenting_test.sh create mode 100644 tools/testing/selftests/vm/write_hugetlb_memory.sh create mode 100644 tools/testing/selftests/vm/write_to_hugetlbfs.c
tools/testing/selftests/vm/.gitignore +1 -0
@@ -14,3 +14,4 @@ virtual_address_range gup_benchmark va_128TBswitch map_fixed_noreplace +write_to_hugetlbfs
tools/testing/selftests/vm/Makefile +1 -0
@@ -22,6 +22,7 @@ TEST_GEN_FILES += userfaultfd ifneq (,$(filter $(ARCH),arm64 ia64 mips64 parisc64 ppc64 riscv64 s390x sh64 sparc64 x86_64)) TEST_GEN_FILES += va_128TBswitch TEST_GEN_FILES += virtual_address_range +TEST_GEN_FILES += write_to_hugetlbfs endif TEST_PROGS := run_vmtests
tools/testing/selftests/vm/charge_reserved_hugetlb.sh new file +557 -0
557 lines not on this page · raw message
tools/testing/selftests/vm/hugetlb_reparenting_test.sh new file +235 -0
235 lines not on this page · raw message
tools/testing/selftests/vm/write_hugetlb_memory.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +set -e + +size=$1 +populate=$2 +write=$3 +cgroup=$4 +path=$5 +method=$6 +private=$7 +want_sleep=$8 +reserve=$9 + +echo "Putting task in cgroup '$cgroup'" +echo $$ > /dev/cgroup/memory/"$cgroup"/cgroup.procs + +echo "Method is $method" + +set +e +./write_to_hugetlbfs -p "$path" -s "$size" "$write" "$populate" -m "$method" \ + "$private" "$want_sleep" "$reserve"
tools/testing/selftests/vm/write_to_hugetlbfs.c new file +261 -0
261 lines not on this page · raw message
--
2.25.0.rc1.283.g88dfdc4193-goog
Re: [PATCH v10 7/8] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
Hi,
Quoted 11 lines · Mina Almasry
The tests use both shared and private mapped hugetlb memory, and monitors the hugetlb usage counter as well as the hugetlb reservation counter. They test different configurations such as hugetlb memory usage via hugetlbfs, or
MAP_HUGETLB, or shmget/shmat, and with and withoutMAP_POPULATE.Also add test for hugetlb reservation reparenting, since this is a subtle issue.
Signed-off-by Mina Almasry <[email protected]>
For powerpc64, either 16MB/16GB or 2MB/1GB huge pages are supported depending on the MMU type (Hash or Radix). I was just running these tests on a powerpc64 system with Hash MMU and ran into problems because the tests assume that the hugepage size is always 2MB. Can you determine the huge page size at runtime?
- Sandipan
Re: [PATCH v10 7/8] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
Quoted 6 lines · Sandipan Das
For powerpc64, either 16MB/16GB or 2MB/1GB huge pages are supported depending on the MMU type (Hash or Radix). I was just running these tests on a powerpc64 system with Hash MMU and ran into problems because the tests assume that the hugepage size is always 2MB. Can you determine the huge page size at runtime?
Absolutely. Let me try to reproduce this failure and it should be fixed in the next patchset version.
Re: [PATCH v10 7/8] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
In reply to Sandipan DasQuoted 9 lines · Sandipan Das
Quoted 9 lines
The tests use both shared and private mapped hugetlb memory, and monitors the hugetlb usage counter as well as the hugetlb reservation counter. They test different configurations such as hugetlb memory usage via hugetlbfs, or
MAP_HUGETLB, or shmget/shmat, and with and withoutMAP_POPULATE.Also add test for hugetlb reservation reparenting, since this is a subtle issue.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Mina Almasry <[email protected]>For powerpc64, either 16MB/16GB or 2MB/1GB huge pages are supported depending on the MMU type (Hash or Radix). I was just running these tests on a powerpc64 system with Hash MMU and ran into problems because the tests assume that the hugepage size is always 2MB. Can you determine the huge page size at runtime?
I assume this is only testing failures of the tools/testing/selftests additions that hardcode 2MB paths and not a kernel problem? In other words, you can still boot, reserve, alloc, and free hugetlb pages on ppc after this patchset without using the selftests?
Re: [PATCH v10 7/8] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
Hi David,
Quoted 8 lines · David Rientjes
Quoted 5 lines · Sandipan Das
For powerpc64, either 16MB/16GB or 2MB/1GB huge pages are supported depending on the MMU type (Hash or Radix). I was just running these tests on a powerpc64 system with Hash MMU and ran into problems because the tests assume that the hugepage size is always 2MB. Can you determine the huge page size at runtime?
Quoted 5 lines
I assume this is only testing failures of the
tools/testing/selftestsadditions that hardcode 2MB paths and not a kernel problem? In other words, you can still boot, reserve, alloc, and free hugetlb pages on ppc after this patchset without using the selftests?
Yes, its just the hardcoded paths. I didn't run into any kernel problems.
- Sandipan
Re: [PATCH v10 7/8] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
Quoted 12 lines · Sandipan Das
Hi David,
Quoted 8 lines · David Rientjes
Quoted 5 lines · Sandipan Das
For powerpc64, either 16MB/16GB or 2MB/1GB huge pages are supported depending on the MMU type (Hash or Radix). I was just running these tests on a powerpc64 system with Hash MMU and ran into problems because the tests assume that the hugepage size is always 2MB. Can you determine the huge page size at runtime?
Quoted 8 lines
Quoted 5 lines
I assume this is only testing failures of the
tools/testing/selftestsadditions that hardcode 2MB paths and not a kernel problem? In other words, you can still boot, reserve, alloc, and free hugetlb pages on ppc after this patchset without using the selftests?Yes, its just the hardcoded paths. I didn't run into any kernel problems.
Sandipan, I updated the tests to not assume 2MB page size, but I'm having trouble getting a setup with a non-2MB default size to test with. I'm uploading v11 of the series shortly, please let me know if the problem persists.
Quoted 2 lines
- Sandipan
PATCH v10 8/8 hugetlb_cgroup: Add hugetlb_cgroup reservation docs
In reply to Mina AlmasryAdd docs for how to use hugetlb_cgroup reservations, and their behavior.
---
Changes in v10:
- Clarify reparenting behavior.
- Reword benefits of reservation limits.
Changes in v6:
- Updated docs to reflect the new design based on a new counter that tracks both reservations and faults.
--- .../admin-guide/cgroup-v1/hugetlb.rst | 103 ++++++++++++++++-- 1 file changed, 92 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v1/hugetlb.rst +92 -11
@@ -2,13 +2,6 @@ HugeTLB Controller ================== -The HugeTLB controller allows to limit the HugeTLB usage per control group and -enforces the controller limit during page fault. Since HugeTLB doesn't -support page reclaim, enforcing the limit at page fault time implies that, -the application will get SIGBUS signal if it tries to access HugeTLB pages -beyond its limit. This requires the application to know beforehand how much -HugeTLB pages it would require for its use. - HugeTLB controller can be created by first mounting the cgroup filesystem. # mount -t cgroup -o hugetlb none /sys/fs/cgroup
@@ -28,10 +21,14 @@ process (bash) into it. Brief summary of control files:: - hugetlb.<hugepagesize>.limit_in_bytes # set/show limit of "hugepagesize" hugetlb usage - hugetlb.<hugepagesize>.max_usage_in_bytes # show max "hugepagesize" hugetlb usage recorded - hugetlb.<hugepagesize>.usage_in_bytes # show current usage for "hugepagesize" hugetlb - hugetlb.<hugepagesize>.failcnt # show the number of allocation failure due to HugeTLB limit + hugetlb.<hugepagesize>.resv.limit_in_bytes # set/show limit of "hugepagesize" hugetlb reservations + hugetlb.<hugepagesize>.resv.max_usage_in_bytes # show max "hugepagesize" hugetlb reservations and no-reserve faults + hugetlb.<hugepagesize>.resv.usage_in_bytes # show current reservations and no-reserve faults for "hugepagesize" hugetlb + hugetlb.<hugepagesize>.resv.failcnt # show the number of allocation failure due to HugeTLB reservation limit + hugetlb.<hugepagesize>.limit_in_bytes # set/show limit of "hugepagesize" hugetlb faults + hugetlb.<hugepagesize>.max_usage_in_bytes # show max "hugepagesize" hugetlb usage recorded + hugetlb.<hugepagesize>.usage_in_bytes # show current usage for "hugepagesize" hugetlb + hugetlb.<hugepagesize>.failcnt # show the number of allocation failure due to HugeTLB usage limit For a system supporting three hugepage sizes (64k, 32M and 1G), the control files include::
@@ -40,11 +37,95 @@ files include:: hugetlb.1GB.max_usage_in_bytes hugetlb.1GB.usage_in_bytes hugetlb.1GB.failcnt + hugetlb.1GB.resv.limit_in_bytes + hugetlb.1GB.resv.max_usage_in_bytes + hugetlb.1GB.resv.usage_in_bytes + hugetlb.1GB.resv.failcnt hugetlb.64KB.limit_in_bytes hugetlb.64KB.max_usage_in_bytes hugetlb.64KB.usage_in_bytes hugetlb.64KB.failcnt + hugetlb.64KB.resv.limit_in_bytes + hugetlb.64KB.resv.max_usage_in_bytes + hugetlb.64KB.resv.usage_in_bytes + hugetlb.64KB.resv.failcnt hugetlb.32MB.limit_in_bytes hugetlb.32MB.max_usage_in_bytes hugetlb.32MB.usage_in_bytes hugetlb.32MB.failcnt + hugetlb.32MB.resv.limit_in_bytes + hugetlb.32MB.resv.max_usage_in_bytes + hugetlb.32MB.resv.usage_in_bytes + hugetlb.32MB.resv.failcnt + + +1. Page fault accounting + +hugetlb.<hugepagesize>.limit_in_bytes +hugetlb.<hugepagesize>.max_usage_in_bytes +hugetlb.<hugepagesize>.usage_in_bytes +hugetlb.<hugepagesize>.failcnt + +The HugeTLB controller allows users to limit the HugeTLB usage (page fault) per +control group and enforces the limit during page fault. Since HugeTLB +doesn't support page reclaim, enforcing the limit at page fault time implies +that, the application will get SIGBUS signal if it tries to fault in HugeTLB +pages beyond its limit. Therefore the application needs to know exactly how many +HugeTLB pages it uses before hand, and the sysadmin needs to make sure that +there are enough available on the machine for all the users to avoid processes +getting SIGBUS. + + +2. Reservation accounting + +hugetlb.<hugepagesize>.resv.limit_in_bytes +hugetlb.<hugepagesize>.resv.max_usage_in_bytes +hugetlb.<hugepagesize>.resv.usage_in_bytes +hugetlb.<hugepagesize>.resv.failcnt + +The HugeTLB controller allows to limit the HugeTLB reservations per control +group and enforces the controller limit at reservation time and at the fault of +HugeTLB memory for which no reservation exists. Since reservation limits are +enforced at reservation time (on mmap or shget), reservation limits never causes +the application to get SIGBUS signal if the memory was reserved before hand. For +MAP_NORESERVE allocations, the reservation limit behaves the same as the fault +limit, enforcing memory usage at fault time and causing the application to +receive a SIGBUS if it's crossing its limit. + +Reservation limits are superior to page fault limits described above, since +reservation limits are enforced at reservation time (on mmap or shget), and +never causes the application to get SIGBUS signal if the memory was reserved +before hand. This allows for easier fallback to alternatives such as +non-HugeTLB memory for example. In the case of page fault accounting, it's very +hard to avoid processes getting SIGBUS since the sysadmin needs precisely know +the HugeTLB usage of all the tasks in the system and make sure there is enough +pages to satisfy all requests. Avoiding tasks getting SIGBUS on overcommited +systems is practically impossible with page fault accounting. + + +3. Caveats with shared memory + +For shared HugeTLB memory, both HugeTLB reservation and page faults are charged +to the first task that causes the memory to be reserved or faulted, and all +subsequent uses of this reserved or faulted memory is done without charging. + +Shared HugeTLB memory is only uncharged when it is unreserved or deallocated. +This is usually when the HugeTLB file is deleted, and not when the task that +caused the reservation or fault has exited. + + +4. Caveats with HugeTLB cgroup offline. + +When a HugeTLB cgroup goes offline with some reservations or faults still +charged to it, the behavior is as follows: + +- The fault charges are charged to the parent HugeTLB cgroup (reparented), +- the reservation charges remain on the offline HugeTLB cgroup. + +This means that if a HugeTLB cgroup gets offlined while there is still HugeTLB +reservations charged to it, that cgroup persists as a zombie until all HugeTLB +reservations are uncharged. HugeTLB reservations behave in this manner to match +the memory controller whose cgroups also persist as zombie until all charged +memory is uncharged. Also, the tracking of HugeTLB reservations is a bit more +complex compared to the tracking of HugeTLB faults, so it is significantly +harder to reparent reservations at offline time.
--
2.25.0.rc1.283.g88dfdc4193-goog
Quoted 10 lines · Mina Almasry
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
This is patch 1 of an 8 patch series.
Problem:
Currently tasks attempting to reserve more hugetlb memory than is available get a failure at mmap/shmget time. This is thanks to Hugetlbfs Reservations [1]. However, if a task attempts to reserve hugetlb memory only more than its
*reword*
However, if a task attempts to reserve more hugetlb memory than its
Quoted 2 lines
hugetlb_cgrouplimit allows, the kernel will allow the mmap/shmget call, but will SIGBUS the task when it attempts to fault the memory in.
*reword*
but will SIGBUS the task when it attempts to fault in the excess memory.
Quoted 13 lines
We have users hitting their
hugetlb_cgrouplimits and thus we've been looking at this failure mode. We'd like to improve this behavior such that users violating thehugetlb_cgrouplimits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in. This gives the user an opportunity to fallback more gracefully to non-hugetlbfs memory for example.The underlying problem is that today's
hugetlb_cgroupaccounting happens at hugetlb memory *fault* time, rather than at *reservation* time. Thus, enforcing thehugetlb_cgrouplimit only happens at fault time, and the offending task gets SIGBUS'd.
Quoted 4 lines
Proposed Solution:
A new page counter named 'hugetlb.xMB.reservation_[limit|usage|max_usage]_in_bytes'. This counter has slightly different semantics than
You changed the name to 'hugetlb.xMB.resv_[limit|usage|max_usage]_in_bytes' in the code, but left this description.
Also, David suggested 'rsvd' as the abbreviation to use here. I would also prefer that name to be consistent with other hugetlb interfaces.
Quoted 39 lines
'hugetlb.xMB.[limit|usage|max_usage]_in_bytes':
- While
usage_in_bytestracks all *faulted* hugetlb memory,reservation_usage_in_bytestracks all *reserved* hugetlb memory and hugetlb memory faulted in without a prior reservation.- If a task attempts to reserve more memory than
limit_in_bytesallows, the kernel will allow it to do so. But if a task attempts to reserve more memory thanreservation_limit_in_bytes, the kernel will fail this reservation.This proposal is implemented in this patch series, with tests to verify functionality and show the usage.
Alternatives considered:
1. A new cgroup, instead of only a newpage_counterattached to the existinghugetlb_cgroup. Adding a new cgroup seemed like a lot of code duplication withhugetlb_cgroup. Keeping hugetlb related page counters underhugetlb_cgroupseemed cleaner as well.2. Instead of adding a new counter, we considered adding a sysctl that modifies the behavior of hugetlb.xMB.[limit|usage]_in_bytes, to do accounting at reservation time rather than fault time. Adding a new
page_counterseems better as userspace could, if it wants, choose to enforce different cgroups
differently: one vialimit_in_bytes, and another viareservation_limit_in_bytes. This could be very useful if you're transitioning how hugetlb memory is partitioned on your system one cgroup at a time, for example. Also, someone may find usage for bothlimit_in_bytesandreservation_limit_in_bytesconcurrently, and this approach gives them the option to do so.Testing:
- Added tests passing.
- Used libhugetlbfs for regression testing.[1]: https://www.kernel.org/doc/html/latest/vm/hugetlbfs_reserv.html
Signed-off-by Mina Almasry <[email protected]>
Quoted 8 lines
---
Changes in v10:
- Renamed reservation_* to resv.*--- include/linux/hugetlb.h | 4 +- mm/hugetlb_cgroup.c | 115 +++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 15 deletions(-)
The code looks fine to me.
With the commit message and naming updates, I will add a Reviewed-by:
Please do wait a few/several days before sending a revised edition to make sure we get all feedback. I really would like to get comments from people more familiar with cgroups.
Signature · 1 line
Mike Kravetz
Quoted 25 lines · Mina Almasry
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
This is patch 1 of an 8 patch series.
Problem:
Currently tasks attempting to reserve more hugetlb memory than is available get a failure at mmap/shmget time. This is thanks to Hugetlbfs Reservations [1]. However, if a task attempts to reserve hugetlb memory only more than itshugetlb_cgrouplimit allows, the kernel will allow the mmap/shmget call, but will SIGBUS the task when it attempts to fault the memory in.We have users hitting their
hugetlb_cgrouplimits and thus we've been looking at this failure mode. We'd like to improve this behavior such that users violating thehugetlb_cgrouplimits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in. This gives the user an opportunity to fallback more gracefully to non-hugetlbfs memory for example.The underlying problem is that today's
hugetlb_cgroupaccounting happens at hugetlb memory *fault* time, rather than at *reservation* time. Thus, enforcing thehugetlb_cgrouplimit only happens at fault time, and the offending task gets SIGBUS'd.
Quoted 6 lines
Proposed Solution:
A new page counter named 'hugetlb.xMB.reservation_[limit|usage|max_usage]_in_bytes'. This counter has slightly different semantics than 'hugetlb.xMB.[limit|usage|max_usage]_in_bytes':
Changelog looks like it needs to be updated with the new resv naming.
Quoted 82 lines
- While
usage_in_bytestracks all *faulted* hugetlb memory,reservation_usage_in_bytestracks all *reserved* hugetlb memory and hugetlb memory faulted in without a prior reservation.- If a task attempts to reserve more memory than
limit_in_bytesallows, the kernel will allow it to do so. But if a task attempts to reserve more memory thanreservation_limit_in_bytes, the kernel will fail this reservation.This proposal is implemented in this patch series, with tests to verify functionality and show the usage.
Alternatives considered:
1. A new cgroup, instead of only a newpage_counterattached to the existinghugetlb_cgroup. Adding a new cgroup seemed like a lot of code duplication withhugetlb_cgroup. Keeping hugetlb related page counters underhugetlb_cgroupseemed cleaner as well.2. Instead of adding a new counter, we considered adding a sysctl that modifies the behavior of hugetlb.xMB.[limit|usage]_in_bytes, to do accounting at reservation time rather than fault time. Adding a new
page_counterseems better as userspace could, if it wants, choose to enforce different cgroups
differently: one vialimit_in_bytes, and another viareservation_limit_in_bytes. This could be very useful if you're transitioning how hugetlb memory is partitioned on your system one cgroup at a time, for example. Also, someone may find usage for bothlimit_in_bytesandreservation_limit_in_bytesconcurrently, and this approach gives them the option to do so.Testing:
- Added tests passing.
- Used libhugetlbfs for regression testing.[1]: https://www.kernel.org/doc/html/latest/vm/hugetlbfs_reserv.html
Signed-off-by Mina Almasry <[email protected]>---
Changes in v10:
- Renamed reservation_* to resv.*--- include/linux/hugetlb.h | 4 +- mm/hugetlb_cgroup.c | 115 +++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 15 deletions(-)include/linux/hugetlb.h +2
-2@@ -432,8 +432,8 @@ struct hstate {unsigned int surplus_huge_pages_node[MAX_NUMNODES ];#ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ -struct cftype cgroup_files_dfl[5 ]; -struct cftype cgroup_files_legacy[5 ]; +struct cftype cgroup_files_dfl[7 ]; +struct cftype cgroup_files_legacy[9 ];#endif char name[HSTATE_NAME_LEN ]; };mm/hugetlb_cgroup.c +6
-0@@ -36,6 +36,11 @@ struct hugetlb_cgroup {*/ struct page_counter hugepage[HUGE_MAX_HSTATE ]; +/* +* the counter to account for hugepage reservations from hugetlb. +*/ +struct page_counter reserved_hugepage[HUGE_MAX_HSTATE ]; +atomic_long_t events[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ];atomic_long_t events_local[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ];@@ -55,6 +60,14 @@ struct hugetlb_cgroup {static struct hugetlb_cgroup *root_h_cgroup__read_mostly ; +static inline struct page_counter *
mm/hugetlb_cgroup.c struct hugetlb_cgroup { · line 73
Quoted 10 lines
mm/hugetlb_cgroup.c +7
-0+hugetlb_cgroup_get_counter(struct hugetlb_cgroup *h_cg, int idx, bool reserved) +{ + if (reserved) + return &h_cg->reserved_hugepage[idx]; +return &h_cg->hugepage[idx]; +} +static inline struct hugetlb_cgroup *hugetlb_cgroup_from_css (struct cgroup_subsys_state *s) {
Small nit: hugetlb_cgroup_get_counter(), to me, implies incrementing a reference count, perhaps a better name would be in order. No strong preference.
Quoted 144 lines
mm/hugetlb_cgroup.c +49
-4@@ -295,28 +308,42 @@ void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,enum {RES_USAGE , +RES_RESERVATION_USAGE ,RES_LIMIT , +RES_RESERVATION_LIMIT ,RES_MAX_USAGE , +RES_RESERVATION_MAX_USAGE ,RES_FAILCNT , +RES_RESERVATION_FAILCNT , };static u64 hugetlb_cgroup_read_u64 (struct cgroup_subsys_state *css,struct cftype *cft) {struct page_counter *counter; +struct page_counter *reserved_counter;struct hugetlb_cgroup *h_cg =hugetlb_cgroup_from_css (css); counter = &h_cg->hugepage[MEMFILE_IDX (cft->private)]; + reserved_counter = &h_cg->reserved_hugepage[MEMFILE_IDX (cft->private)];switch (MEMFILE_ATTR (cft->private)) {case RES_USAGE :return (u64 )page_counter_read (counter) *PAGE_SIZE ; +case RES_RESERVATION_USAGE : +return (u64 )page_counter_read (reserved_counter) *PAGE_SIZE ;case RES_LIMIT :return (u64 )counter->max *PAGE_SIZE ; +case RES_RESERVATION_LIMIT : +return (u64 )reserved_counter->max *PAGE_SIZE ;case RES_MAX_USAGE :return (u64 )counter->watermark *PAGE_SIZE ; +case RES_RESERVATION_MAX_USAGE : +return (u64 )reserved_counter->watermark *PAGE_SIZE ;case RES_FAILCNT :return counter->failcnt; +case RES_RESERVATION_FAILCNT : +return reserved_counter->failcnt;default :BUG (); }@@ -338,10 +365,16 @@ static int hugetlb_cgroup_read_u64_max(struct seq_file *seq, void *v) 1 << huge_page_order(&hstates[idx]));switch (MEMFILE_ATTR (cft->private)) { +case RES_RESERVATION_USAGE : + counter = &h_cg->reserved_hugepage[idx]; +/* Fall through. */ case RES_USAGE : val = (u64 )page_counter_read (counter);seq_printf (seq,"%llu\n" , val *PAGE_SIZE );break ; +case RES_RESERVATION_LIMIT : + counter = &h_cg->reserved_hugepage[idx]; +/* Fall through. */ case RES_LIMIT : val = (u64 )counter->max;if (val == limit)@@ -365,6 +398,7 @@ static ssize_t hugetlb_cgroup_write(struct kernfs_open_file *of,int ret, idx;unsigned long nr_pages;struct hugetlb_cgroup *h_cg =hugetlb_cgroup_from_css (of_css (of)); +bool reserved = false;if (hugetlb_cgroup_is_root (h_cg))/* Can't set limit on root */ return -EINVAL ;@@ -378,9 +412,14 @@ static ssize_t hugetlb_cgroup_write(struct kernfs_open_file *of, nr_pages =round_down (nr_pages,1 <<huge_page_order (&hstates[idx]));switch (MEMFILE_ATTR (of_cft (of)->private)) { +case RES_RESERVATION_LIMIT : + reserved = true; +/* Fall through. */ case RES_LIMIT :mutex_lock (&hugetlb_limit_mutex); - ret =page_counter_set_max (&h_cg->hugepage[idx], nr_pages); + ret =page_counter_set_max (hugetlb_cgroup_get_counter (h_cg, idx, + reserved), + nr_pages);mutex_unlock (&hugetlb_limit_mutex);break ;default :@@ -406,18 +445,26 @@ static ssize_t hugetlb_cgroup_reset(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { int ret = 0; -struct page_counter *counter; +struct page_counter *counter, *reserved_counter;struct hugetlb_cgroup *h_cg =hugetlb_cgroup_from_css (of_css (of)); counter = &h_cg->hugepage[MEMFILE_IDX (of_cft (of)->private)]; + reserved_counter = + &h_cg->reserved_hugepage[MEMFILE_IDX (of_cft (of)->private)];switch (MEMFILE_ATTR (of_cft (of)->private)) {case RES_MAX_USAGE :page_counter_reset_watermark (counter);break ; +case RES_RESERVATION_MAX_USAGE : +page_counter_reset_watermark (reserved_counter); +break ;case RES_FAILCNT : counter->failcnt =0 ;break ; +case RES_RESERVATION_FAILCNT : + reserved_counter->failcnt =0 ; +break ;default : ret = -EINVAL ;break ;@@ -472,7 +519,7 @@ static void __init __hugetlb_cgroup_file_dfl_init(int idx)struct hstate *h = &hstates[idx];/* format the size */ -mem_fmt (buf,32 ,huge_page_size (h)); +mem_fmt (buf,sizeof (buf),huge_page_size (h));/* Add the limit file */ cft = &h->cgroup_files_dfl[0 ];@@ -482,15 +529,30 @@ static void __init __hugetlb_cgroup_file_dfl_init(int idx) cft->write = hugetlb_cgroup_write_dfl; cft->flags =CFTYPE_NOT_ON_ROOT ; -/* Add the current usage file */ +/* Add the reservation limit file */ cft = &h->cgroup_files_dfl[1 ]; +snprintf (cft->name,MAX_CFTYPE_NAME ,"%s.resv.max" , buf); + cft->private =MEMFILE_PRIVATE (idx,RES_RESERVATION_LIMIT ); + cft->seq_show = hugetlb_cgroup_read_u64_max; + cft->write = hugetlb_cgroup_write_dfl; + cft->flags =CFTYPE_NOT_ON_ROOT ; + +/* Add the current usage file */ + cft = &h->cgroup_files_dfl[2 ];snprintf (cft->name,MAX_CFTYPE_NAME ,"%s.current" , buf); cft->private =MEMFILE_PRIVATE (idx,RES_USAGE ); cft->seq_show = hugetlb_cgroup_read_u64_max; cft->flags =CFTYPE_NOT_ON_ROOT ; +/* Add the current reservation usage file */ + cft = &h->cgroup_files_dfl[3 ]; +snprintf (cft->name,MAX_CFTYPE_NAME ,"%s.resv.current" , buf);
mm/hugetlb_cgroup.c static void __init __hugetlb_cgroup_file_dfl_init(int idx) · line 558
Quoted 10 lines
mm/hugetlb_cgroup.c +5
-1+ cft->private =MEMFILE_PRIVATE (idx,RES_RESERVATION_USAGE ); + cft->seq_show = hugetlb_cgroup_read_u64_max; + cft->flags =CFTYPE_NOT_ON_ROOT ; +/* Add the events file */ - cft = &h->cgroup_files_dfl[2 ]; + cft = &h->cgroup_files_dfl[4 ];snprintf (cft->name,MAX_CFTYPE_NAME ,"%s.events" , buf); cft->private =MEMFILE_PRIVATE (idx,0 ); cft->seq_show = hugetlb_events_show;
Any cleanup to __hugetlb_cgroup_file_dfl_init() and __hugetlb_cgroup_file_legacy_init() that is possible would be great in a follow-up patch :)
Other than that, this looks very straight forward.
Quoted 9 lines
Quoted 6 lines
Proposed Solution:
A new page counter named 'hugetlb.xMB.reservation_[limit|usage|max_usage]_in_bytes'. This counter has slightly different semantics than 'hugetlb.xMB.[limit|usage|max_usage]_in_bytes':Changelog looks like it needs to be updated with the new resv naming.
I updated to the rsvd naming, which you suggested earlier and Mike agreed was better.
Quoted 9 lines
Quoted 4 lines
+
static inline
structhugetlb_cgroup*hugetlb_cgroup_from_css(structcgroup_subsys_state*s) {Small nit:
hugetlb_cgroup_get_counter(), to me, implies incrementing a reference count, perhaps a better name would be in order. No strong preference.
Changed
Quoted 11 lines
mm/hugetlb_cgroup.c static void __init __hugetlb_cgroup_file_dfl_init(int idx) · line 558
Quoted 6 lines
/* Add the events file */mm/hugetlb_cgroup.c +1
-1- cft = &h->cgroup_files_dfl[2 ]; + cft = &h->cgroup_files_dfl[4 ];snprintf (cft->name,MAX_CFTYPE_NAME ,"%s.events" , buf); cft->private =MEMFILE_PRIVATE (idx,0 ); cft->seq_show = hugetlb_events_show;Any cleanup to
__hugetlb_cgroup_file_dfl_init()and__hugetlb_cgroup_file_legacy_init()that is possible would be great in a follow-up patch :)
Will do!
Quoted 3 lines
Other than that, this looks very straight forward.
Acked-by David Rientjes <[email protected]>