hugetlb_cgroup: Add hugetlb_cgroup reservation counter
Subsystems (5)
PATCH v9 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 allocate 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 allocate 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 developers interested in using hugetlb_cgroups, and they have expressed dissatisfaction regarding this behavior. We'd like to improve this behavior such that tasks 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.
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]_in_bytes. This counter has slightly different semantics than hugetlb.xMB.[limit|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. We also added cgroup-v2 support to hugetlb_cgroup so that the new use cases can be extended to v2.
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
--- include/linux/hugetlb.h | 4 +- mm/hugetlb_cgroup.c | 116 +++++++++++++++++++++++++++++++++++----- 2 files changed, 106 insertions(+), 14 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 +104 -12
251 lines not on this page · raw message
--
2.24.1.735.g03f4e72817-goog
PATCH v9 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 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 | 98 +++++++++++++++++++++++++--------- 3 files changed, 127 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
@@ -1067,7 +1067,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)) {
@@ -1177,8 +1178,8 @@ 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++;
@@ -1204,7 +1205,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);
@@ -1990,7 +1992,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;
@@ -2014,7 +2017,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);
@@ -2038,7 +2042,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 +72 -26
199 lines not on this page · raw message
--
2.24.1.735.g03f4e72817-goog
Re: [PATCH v9 2/8] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Quoted 86 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.Signed-off-by Mina Almasry <[email protected]>---
Changes in V9:
- FixedHUGETLB_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 | 98 +++++++++++++++++++++++++--------- 3 files changed, 127 insertions(+), 58 deletions(-)include/linux/hugetlb_cgroup.h +17
-9@@ -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);
include/linux/hugetlb_cgroup.h static inline bool hugetlb_cgroup_disabled(void) · line 77
Quoted 10 lines
include/linux/hugetlb_cgroup.h +8
-1+ 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); +
Do we need a corresponding stub for the !CONFIG_CGROUP_HUGETLB case?
Quoted 77 lines
include/linux/hugetlb_cgroup.h +18
-15extern 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 +2
-1@@ -1067,7 +1067,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)) {
mm/hugetlb.c void free_huge_page(struct page *page) · line 1182
Quoted 7 lines
mm/hugetlb.c +2
-2@@ -1177,8 +1178,8 @@ 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);
hugetlb code has been restructured a bit and this now resides in the function __free_huge_page().
Quoted 88 lines
mm/hugetlb.c +8
-4if (restore_reserve) h->resv_huge_pages++;@@ -1204,7 +1205,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);@@ -1990,7 +1992,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;@@ -2014,7 +2017,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);@@ -2038,7 +2042,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 +19
-6@@ -96,8 +96,12 @@ static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg)int idx;for (idx =0 ; idx < hugetlb_max_hstate; idx++) { -if (page_counter_read (&h_cg->hugepage[idx])) +if (page_counter_read ( +hugetlb_cgroup_get_counter (h_cg, idx, true)) || +page_counter_read ( +hugetlb_cgroup_get_counter (h_cg, idx, false))) {return true; + } }return false; }@@ -108,18 +112,32 @@ static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup,int idx;for (idx =0 ; idx <HUGE_MAX_HSTATE ; idx++) { -struct page_counter *counter = &h_cgroup->hugepage[idx]; -struct page_counter *parent =NULL ; +struct page_counter *fault_parent =NULL ; +struct page_counter *reserved_parent =NULL ;unsigned long limit;int ret; -if (parent_h_cgroup) - parent = &parent_h_cgroup->hugepage[idx]; -page_counter_init (counter, parent); +if (parent_h_cgroup) { + fault_parent =hugetlb_cgroup_get_counter ( + parent_h_cgroup, idx, false); + reserved_parent =hugetlb_cgroup_get_counter ( + parent_h_cgroup, idx, true); + } +page_counter_init (hugetlb_cgroup_get_counter (h_cgroup, idx, + false), + fault_parent); +page_counter_init (hugetlb_cgroup_get_counter (h_cgroup, idx, + true), + reserved_parent);
mm/hugetlb_cgroup.c static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup, · line 141
Quoted 10 lines
mm/hugetlb_cgroup.c +6
-1limit =round_down (PAGE_COUNTER_MAX ,1 <<huge_page_order (&hstates[idx])); - ret =page_counter_set_max (counter, limit); + + ret =page_counter_set_max ( +hugetlb_cgroup_get_counter (h_cgroup, idx, false), + limit); + ret =page_counter_set_max ( +hugetlb_cgroup_get_counter (h_cgroup, idx, true), limit);VM_BUG_ON (ret);
The second page_counter_set_max() call overwrites ret before the check in VM_BUG_ON().
Quoted 45 lines
mm/hugetlb_cgroup.c +5
-6} }@@ -149,7 +167,6 @@ static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css)kfree (h_cgroup); } -/* * Should be called with hugetlb_lock held. * Since we are holding hugetlb_lock, pages cannot get moved from @@ -165,7 +182,7 @@ static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,struct hugetlb_cgroup *page_hcg;struct hugetlb_cgroup *parent =parent_hugetlb_cgroup (h_cg); - page_hcg =hugetlb_cgroup_from_page (page); + page_hcg =hugetlb_cgroup_from_page (page, false);/* * We can have pages in active list without any cgroup * ie, hugepage with less than 3 pages. We can safely @@ -184,7 +201,7 @@ static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,/* Take the pages off the local counter */ page_counter_cancel (counter, nr_pages); -set_hugetlb_cgroup (page, parent); +set_hugetlb_cgroup (page, parent, false); out:return ; }@@ -227,7 +244,7 @@ static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx, }int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct hugetlb_cgroup **ptr,bool reserved) {int ret =0 ;struct page_counter *counter;@@ -250,13 +267,20 @@ int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, }rcu_read_unlock (); -if (!page_counter_try_charge (&h_cg->hugepage[idx], nr_pages, - &counter)) { + if (!page_counter_try_charge(hugetlb_cgroup_get_counter(h_cg, idx, + reserved),
mm/hugetlb_cgroup.c int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, · line 280
Quoted 10 lines
mm/hugetlb_cgroup.c +5
-1+ nr_pages, &counter)) { ret = -ENOMEM ;hugetlb_event (hugetlb_cgroup_from_counter (counter, idx), idx,HUGETLB_MAX ); +css_put (&h_cg->css); + goto done; } -css_put (&h_cg->css); +/* Reservations take a reference to the css because they do not get +* reparented.
I'm hoping someone with more cgroup knowledge can comment on this and any consequences of not reparenting reservations. We previously talked about why reparenting would be very difficult/expensive. I understand why you are nopt doing it. Just do not fully understand what needs to be done from the cgroup side.
Several of the following modifications deal with this. --
Mike kravetz
Quoted 108 lines
mm/hugetlb_cgroup.c +37
-13+*/ +if (!reserved) +css_put (&h_cg->css); done: *ptr = h_cg;return ret;@@ -265,12 +289,12 @@ int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,/* Should be called with hugetlb_lock held */ void hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages,struct hugetlb_cgroup *h_cg, -struct page *page) +struct page *page,bool reserved) {if (hugetlb_cgroup_disabled () || !h_cg)return ; -set_hugetlb_cgroup (page, h_cg); +set_hugetlb_cgroup (page, h_cg, reserved);return ; }@@ -278,23 +302,29 @@ void hugetlb_cgroup_commit_charge(int idx, unsigned long nr_pages,* Should be called with hugetlb_lock held */ void hugetlb_cgroup_uncharge_page (int idx,unsigned long nr_pages, -struct page *page) +struct page *page,bool reserved) {struct hugetlb_cgroup *h_cg;if (hugetlb_cgroup_disabled ())return ;lockdep_assert_held (&hugetlb_lock); - h_cg =hugetlb_cgroup_from_page (page); + h_cg =hugetlb_cgroup_from_page (page, reserved);if (unlikely (!h_cg))return ; -set_hugetlb_cgroup (page,NULL ); -page_counter_uncharge (&h_cg->hugepage[idx], nr_pages); +set_hugetlb_cgroup (page,NULL , reserved); + +page_counter_uncharge (hugetlb_cgroup_get_counter (h_cg, idx, reserved), + nr_pages); + +if (reserved) +css_put (&h_cg->css); +return ; }void hugetlb_cgroup_uncharge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup *h_cg) +struct hugetlb_cgroup *h_cg,bool reserved) {if (hugetlb_cgroup_disabled () || !h_cg)return ;@@ -302,8 +332,22 @@ void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages,if (huge_page_order (&hstates[idx]) <HUGETLB_CGROUP_MIN_ORDER )return ; -page_counter_uncharge (&h_cg->hugepage[idx], nr_pages); -return ; +page_counter_uncharge (hugetlb_cgroup_get_counter (h_cg, idx, reserved), + nr_pages); + +if (reserved) +css_put (&h_cg->css); +} + +void hugetlb_cgroup_uncharge_counter (struct page_counter *p, +unsigned long nr_pages, +struct cgroup_subsys_state *css) +{ +if (hugetlb_cgroup_disabled () || !p || !css) +return ; + +page_counter_uncharge (p, nr_pages); +css_put (css); }enum {@@ -678,6 +722,7 @@ void __init hugetlb_cgroup_file_init(void)void hugetlb_cgroup_migrate (struct page *oldhpage,struct page *newhpage) {struct hugetlb_cgroup *h_cg; +struct hugetlb_cgroup *h_cg_reservation;struct hstate *h =page_hstate (oldhpage);if (hugetlb_cgroup_disabled ())@@ -685,11 +730,12 @@ void hugetlb_cgroup_migrate(struct page *oldhpage, struct page *newhpage)VM_BUG_ON_PAGE (!PageHuge (oldhpage), oldhpage);spin_lock (&hugetlb_lock); - h_cg =hugetlb_cgroup_from_page (oldhpage); -set_hugetlb_cgroup (oldhpage,NULL ); + h_cg =hugetlb_cgroup_from_page (oldhpage, false); + h_cg_reservation =hugetlb_cgroup_from_page (oldhpage, true); +set_hugetlb_cgroup (oldhpage,NULL , false);/* move the h_cg details to new cgroup */ -set_hugetlb_cgroup (newhpage, h_cg); +set_hugetlb_cgroup (newhpage, h_cg_reservation, true);list_move (&newhpage->lru, &h->hugepage_activelist);spin_unlock (&hugetlb_lock);return ; --2.24.1.735.g03f4e72817-goog
Re: [PATCH v9 2/8] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Quoted 112 lines · Mike Kravetz
Quoted 45 lines
mm/hugetlb_cgroup.c +19
-6@@ -96,8 +96,12 @@ static inline bool hugetlb_cgroup_have_usage(struct hugetlb_cgroup *h_cg)int idx;for (idx =0 ; idx < hugetlb_max_hstate; idx++) { -if (page_counter_read (&h_cg->hugepage[idx])) +if (page_counter_read ( +hugetlb_cgroup_get_counter (h_cg, idx, true)) || +page_counter_read ( +hugetlb_cgroup_get_counter (h_cg, idx, false))) {return true; + } }return false; }@@ -108,18 +112,32 @@ static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup,int idx;for (idx =0 ; idx <HUGE_MAX_HSTATE ; idx++) { -struct page_counter *counter = &h_cgroup->hugepage[idx]; -struct page_counter *parent =NULL ; +struct page_counter *fault_parent =NULL ; +struct page_counter *reserved_parent =NULL ;unsigned long limit;int ret; -if (parent_h_cgroup) - parent = &parent_h_cgroup->hugepage[idx]; -page_counter_init (counter, parent); +if (parent_h_cgroup) { + fault_parent =hugetlb_cgroup_get_counter ( + parent_h_cgroup, idx, false); + reserved_parent =hugetlb_cgroup_get_counter ( + parent_h_cgroup, idx, true); + } +page_counter_init (hugetlb_cgroup_get_counter (h_cgroup, idx, + false), + fault_parent); +page_counter_init (hugetlb_cgroup_get_counter (h_cgroup, idx, + true), + reserved_parent);mm/hugetlb_cgroup.c static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup, · line 141
Quoted 10 lines
mm/hugetlb_cgroup.c +6
-1limit =round_down (PAGE_COUNTER_MAX ,1 <<huge_page_order (&hstates[idx])); - ret =page_counter_set_max (counter, limit); + + ret =page_counter_set_max ( +hugetlb_cgroup_get_counter (h_cgroup, idx, false), + limit); + ret =page_counter_set_max ( +hugetlb_cgroup_get_counter (h_cgroup, idx, true), limit);VM_BUG_ON (ret);The second
page_counter_set_max()call overwrites ret before the check inVM_BUG_ON().Quoted 53 lines
mm/hugetlb_cgroup.c +8
-7} }@@ -149,7 +167,6 @@ static void hugetlb_cgroup_css_free(struct cgroup_subsys_state *css)kfree (h_cgroup); } -/* * Should be called with hugetlb_lock held. * Since we are holding hugetlb_lock, pages cannot get moved from @@ -165,7 +182,7 @@ static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,struct hugetlb_cgroup *page_hcg;struct hugetlb_cgroup *parent =parent_hugetlb_cgroup (h_cg); - page_hcg =hugetlb_cgroup_from_page (page); + page_hcg =hugetlb_cgroup_from_page (page, false);/* * We can have pages in active list without any cgroup * ie, hugepage with less than 3 pages. We can safely @@ -184,7 +201,7 @@ static void hugetlb_cgroup_move_parent(int idx, struct hugetlb_cgroup *h_cg,/* Take the pages off the local counter */ page_counter_cancel (counter, nr_pages); -set_hugetlb_cgroup (page, parent); +set_hugetlb_cgroup (page, parent, false); out:return ; }@@ -227,7 +244,7 @@ static inline void hugetlb_event(struct hugetlb_cgroup *hugetlb, int idx, }int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages, -struct hugetlb_cgroup **ptr) +struct hugetlb_cgroup **ptr,bool reserved) {int ret =0 ;struct page_counter *counter;@@ -250,13 +267,20 @@ int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, }rcu_read_unlock (); -if (!page_counter_try_charge (&h_cg->hugepage[idx], nr_pages, - &counter)) { +if (!page_counter_try_charge (hugetlb_cgroup_get_counter (h_cg, idx, + reserved), + nr_pages, &counter)) { ret = -ENOMEM ;hugetlb_event (hugetlb_cgroup_from_counter (counter, idx), idx,HUGETLB_MAX ); +css_put (&h_cg->css); + goto done; } -css_put (&h_cg->css);
Quoted 9 lines
mm/hugetlb_cgroup.c int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages, · line 280
Quoted 2 lines
mm/hugetlb_cgroup.c +2
-0+/* Reservations take a reference to the css because they do not get +* reparented. I'm hoping someone with more cgroup knowledge can comment on this and any consequences of not reparenting reservations. We previously talked about why reparenting would be very difficult/expensive. I understand why you are nopt doing it. Just do not fully understand what needs to be done from the cgroup side.
I don't see any description of how hugetlb_cgroup currently acts wrt reparenting in the last patch in the series and how this is the same or different for reservations. I think the discussion that is referenced here is probably lost in some previous posting of the series. I think it's particularly useful information that the end user will need to know about for its handling so it would benefit from some documentation in the last patch.
Re: [PATCH v9 2/8] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
In reply to Mike KravetzQuoted 7 lines · Mike Kravetz
include/linux/hugetlb_cgroup.h static inline bool hugetlb_cgroup_disabled(void) · line 77
Quoted 4 lines
include/linux/hugetlb_cgroup.h +4
-0+extern void hugetlb_cgroup_uncharge_counter (struct page_counter *p, +unsigned long nr_pages, +struct cgroup_subsys_state *css); +Do we need a corresponding stub for the !CONFIG_CGROUP_HUGETLB case?
Not necessary AFAICT because the code is only called from code guarded by CONFIG_CGROUP_HUGETLB.
Addressing comments from you and David in v10 shortly. Thanks for the review!
PATCH v9 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 V9:
- Updated for reparenting of hugetlb reservation accounting.
--- include/linux/hugetlb.h | 9 +++++++ include/linux/hugetlb_cgroup.h | 27 +++++++++++++++++++ mm/hugetlb.c | 47 +++++++++++++++++++++++++++++++++- mm/hugetlb_cgroup.c | 28 -------------------- 4 files changed, 82 insertions(+), 29 deletions(-)
include/linux/hugetlb.h +9 -0
@@ -46,6 +46,15 @@ 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 the mapping is shared. +*/ +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 +46 -1
83 lines not on this page · raw message
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.24.1.735.g03f4e72817-goog
Re: [PATCH v9 3/8] hugetlb_cgroup: add reservation accounting for private mappings
Quoted 40 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.On
hugetlb_vm_op_close, we unchargeresv_map->reservation_counter.Signed-off-by Mina Almasry <[email protected]>Acked-by Hillf Danton <[email protected]>---
Changes in V9:
- Updated for reparenting of hugetlb reservation accounting.--- include/linux/hugetlb.h | 9 +++++++ include/linux/hugetlb_cgroup.h | 27 +++++++++++++++++++ mm/hugetlb.c | 47 +++++++++++++++++++++++++++++++++- mm/hugetlb_cgroup.c | 28 -------------------- 4 files changed, 82 insertions(+), 29 deletions(-)include/linux/hugetlb.h +0
-0
include/linux/hugetlb.h struct resv_map { · line 52
Quoted 7 lines
include/linux/hugetlb.h +4
-0@@ -46,6 +46,15 @@ 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 the mapping is shared.
Will *reservation_counter ALWAYS be non-NULL for private mappings?
Quoted 72 lines
include/linux/hugetlb.h +5
-0+*/ +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 +11
-0@@ -665,6 +665,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);@@ -3145,7 +3156,20 @@ 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 3167
Quoted 10 lines
mm/hugetlb.c +9
-1-kref_put (&resv->refs, resv_map_release); +#ifdef CONFIG_CGROUP_HUGETLB +/* +* Since we check for HPAGE_RESV_OWNER above, this must a private +* mapping, and these values should be none-zero, and should point to +* the hugetlb_cgroup counter to uncharge for this reservation. +*/ +WARN_ON (!resv->reservation_counter); +WARN_ON (!resv->pages_per_hpage); +WARN_ON (!resv->css);
I was once again wondering if these were always non-NULL for private mappings. It seems that reservation_counter (h_gc) would be NULL in these cases from these early checks in hugetlb_cgroup_charge_cgroup().
int hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,
struct hugetlb_cgroup **ptr, bool reserved)
{
int ret = 0;
struct page_counter *counter;
struct hugetlb_cgroup *h_cg = NULL;
if (hugetlb_cgroup_disabled())
goto done;
/*
* We don't charge any cgroup if the compound page have less
* than 3 pages.
*/
if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER)
goto done;
...It seems like the following hugetlb_cgroup_uncharge_counter() guards against reservation_counter being NULL (for some of the same reasons).
Quoted 33 lines
mm/hugetlb.c +9
-0+ +hugetlb_cgroup_uncharge_counter (resv->reservation_counter, + (end - start) * resv->pages_per_hpage, + resv->css); +#endif if (reserve) {/* @@ -3155,6 +3179,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)@@ -4501,6 +4527,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 */ @@ -4534,12 +4561,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 ;
mm/hugetlb.c int hugetlb_reserve_pages(struct inode *inode, · line 4577
Quoted 10 lines
mm/hugetlb.c +7
-0chg = 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 ; + } +
Shouldn't this code be in the #ifdef CONFIG_CGROUP_HUGETLB block?
Signature · 1 line
Mike Kravetz
Quoted 13 lines
mm/hugetlb.c +10
-0+#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 ); }
Re: [PATCH v9 3/8] hugetlb_cgroup: add reservation accounting for private mappings
Quoted 89 lines · Mike Kravetz
include/linux/hugetlb.h struct resv_map { · line 52
Quoted 12 lines
include/linux/hugetlb.h +4
-0@@ -46,6 +46,15 @@ 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 the mapping is shared. Will
*reservation_counterALWAYS be non-NULL for private mappings?Quoted 72 lines
include/linux/hugetlb.h +5
-0+*/ +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 +11
-0@@ -665,6 +665,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);@@ -3145,7 +3156,20 @@ static void hugetlb_vm_op_close(struct vm_area_struct *vma) reserve = (end - start) -region_count (resv, start, end);
Quoted 15 lines
mm/hugetlb.c static void hugetlb_vm_op_close(struct vm_area_struct *vma) · line 3167
Quoted 10 lines
mm/hugetlb.c +9
-1-kref_put (&resv->refs, resv_map_release); +#ifdef CONFIG_CGROUP_HUGETLB +/* +* Since we check for HPAGE_RESV_OWNER above, this must a private +* mapping, and these values should be none-zero, and should point to +* the hugetlb_cgroup counter to uncharge for this reservation. +*/ +WARN_ON (!resv->reservation_counter); +WARN_ON (!resv->pages_per_hpage); +WARN_ON (!resv->css);I was once again wondering if these were always non-NULL for private mappings. It seems that
reservation_counter(h_gc) would be NULL in these cases from these early checks inhugetlb_cgroup_charge_cgroup().
You are right. I'm fixing in v10 the code and comments to account for h_cg potentially being NULL, but I'm having trouble testing. Looking at the code, I'm a bit confused by the checks. Seems to me hugetlb_cgroup_disabled() is the same as #ifdef CONFIG_CGROUP_HUGETLB; I can't find a way to enable the Kconfig but have that return false unless I hack the code. Also seems to me checking huge_page_order is just super definsive; I skimmed the hugepage sizes allowed code and I can't find an arch that allows you to configure hugetlb page size to < 2^HUGETLB_CGROUP_MIN_ORDER pages. So in reality these will never fire, IIUC.
Quoted 56 lines
int hugetlb_cgroup_charge_cgroup(int idx, unsigned long
nr_pages,
structhugetlb_cgroup**ptr, bool reserved){ int ret = 0; struct page_counter *counter; struct hugetlb_cgroup *h_cg = NULL; if (hugetlb_cgroup_disabled()) goto done; /* * We don't charge any cgroup if the compound page have less * than 3 pages. */ if (huge_page_order(&hstates[idx]) < HUGETLB_CGROUP_MIN_ORDER) goto done; ...It seems like the following
hugetlb_cgroup_uncharge_counter()guards againstreservation_counterbeing NULL (for some of the same reasons).Quoted 36 lines
mm/hugetlb.c +9
-0+ +hugetlb_cgroup_uncharge_counter (resv->reservation_counter, + (end - start) * resv->pages_per_hpage, + resv->css); +#endif if (reserve) {/* @@ -3155,6 +3179,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)@@ -4501,6 +4527,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 */ @@ -4534,12 +4561,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;
Quoted 9 lines
mm/hugetlb.c int hugetlb_reserve_pages(struct inode *inode, · line 4577
Quoted 7 lines
mm/hugetlb.c +7
-0+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 ; + } +Shouldn't this code be in the #ifdef
CONFIG_CGROUP_HUGETLBblock?
Not necessary AFAICT, hugetlb_cgroup_charge_cgroup stub returns 0 (no-op).
Re: [PATCH v9 3/8] hugetlb_cgroup: add reservation accounting for private mappings
Quoted 16 lines · Mina Almasry
Quoted 10 lines · Mike Kravetz
mm/hugetlb.c static void hugetlb_vm_op_close(struct vm_area_struct *vma) · line 3167
Quoted 9 lines
mm/hugetlb.c +9
-0+#ifdef CONFIG_CGROUP_HUGETLB +/* +* Since we check for HPAGE_RESV_OWNER above, this must a private +* mapping, and these values should be none-zero, and should point to +* the hugetlb_cgroup counter to uncharge for this reservation. +*/ +WARN_ON (!resv->reservation_counter); +WARN_ON (!resv->pages_per_hpage); +WARN_ON (!resv->css);Quoted 4 lines
I was once again wondering if these were always non-NULL for private mappings. It seems that
reservation_counter(h_gc) would be NULL in these cases from these early checks inhugetlb_cgroup_charge_cgroup().
Quoted 6 lines
You are right. I'm fixing in v10 the code and comments to account for
h_cgpotentially being NULL, but I'm having trouble testing. Looking at the code, I'm a bit confused by the checks. Seems to mehugetlb_cgroup_disabled()is the same as #ifdefCONFIG_CGROUP_HUGETLB; I can't find a way to enable theKconfigbut have that return false unless I hack the code.
What about the boot options?
cgroup_disable=
cgroup_no_v1=
Signature · 1 line
Mike Kravetz
Re: [PATCH v9 3/8] hugetlb_cgroup: add reservation accounting for private mappings
Quoted 25 lines · Mike Kravetz
Quoted 16 lines · Mina Almasry
Quoted 10 lines · Mike Kravetz
mm/hugetlb.c static void hugetlb_vm_op_close(struct vm_area_struct *vma) · line 3167
Quoted 9 lines
mm/hugetlb.c +9
-0+#ifdef CONFIG_CGROUP_HUGETLB +/* +* Since we check for HPAGE_RESV_OWNER above, this must a private +* mapping, and these values should be none-zero, and should point to +* the hugetlb_cgroup counter to uncharge for this reservation. +*/ +WARN_ON (!resv->reservation_counter); +WARN_ON (!resv->pages_per_hpage); +WARN_ON (!resv->css);Quoted 4 lines
I was once again wondering if these were always non-NULL for private mappings. It seems that
reservation_counter(h_gc) would be NULL in these cases from these early checks inhugetlb_cgroup_charge_cgroup().Quoted 6 lines
You are right. I'm fixing in v10 the code and comments to account for
h_cgpotentially being NULL, but I'm having trouble testing. Looking at the code, I'm a bit confused by the checks. Seems to mehugetlb_cgroup_disabled()is the same as #ifdefCONFIG_CGROUP_HUGETLB; I can't find a way to enable theKconfigbut have that return false unless I hack the code.
Quoted 4 lines
What about the boot options?
cgroup_disable=
cgroup_no_v1=
Thanks, cgroup_disable=hugetlb does it. I ran the the libhugetlbfs tests with patchset v10 and it passed, so it seems the latest version of the patch should be fine. Of course my hugetlb cgroup tests fail outright when hugetlb cgroups are disabled so those don't say anything useful.
PATCH v9 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
@@ -244,110 +244,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 ; }/*
@@ -357,49 +426,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 ; }/*
@@ -407,17 +506,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); }
@@ -1834,6 +1936,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)
@@ -1842,20 +1945,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 ;
@@ -4523,12 +4635,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) {
@@ -4558,7 +4670,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. */
@@ -4628,9 +4740,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
@@ -4648,9 +4765,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.24.1.735.g03f4e72817-goog
Re: [PATCH v9 4/8] hugetlb: disable region_add file_region coalescing
Quoted 1 line · Mina Almasry
A follow up patch in this series adds hugetlb cgroup uncharge info the
*nit*
A follow up patch in this series adds hugetlb cgroup uncharge info to the
Quoted 24 lines
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 7 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.Signed-off-by Mina Almasry <[email protected]>Reviewed-by Mike Kravetz <[email protected]>
Thanks for the updated comments. Still,
Signature · 1 line
Mike Kravetz
Re: [PATCH v9 4/8] hugetlb: disable region_add file_region coalescing
Quoted 35 lines · Mike Kravetz
Quoted 1 line · Mina Almasry
A follow up patch in this series adds hugetlb cgroup uncharge info the
*nit*
A follow up patch in this series adds hugetlb cgroup uncharge info to theQuoted 29 lines
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.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.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Mina Almasry <[email protected]>Reviewed-by Mike Kravetz <[email protected]>Thanks for the updated comments. Still,
Reviewed-by Mike Kravetz <[email protected]>
Sorry, that comment was directed at V10 of the patch.
Signature · 1 line
Mike Kravetz
PATCH v9 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 V9:
- Updated for hugetlb reservation repareting.
--- mm/hugetlb.c | 161 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 129 insertions(+), 32 deletions(-)
mm/hugetlb.c +129 -32
@@ -242,6 +242,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
@@ -265,6 +275,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
@@ -272,7 +301,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;
@@ -311,6 +342,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 ;
@@ -327,11 +360,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; }
@@ -352,7 +387,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 ;
@@ -365,7 +401,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
@@ -403,7 +440,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;
@@ -451,7 +488,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 ;
@@ -523,6 +561,25 @@ 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 shared +* reservation, and 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
@@ -587,11 +644,20 @@ 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; + + nrg->reservation_counter = rg->reservation_counter; + nrg->pages_per_hpage = rg->pages_per_hpage; + nrg->css = rg->css; +css_get (rg->css); +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 ;
@@ -599,6 +665,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 ;
@@ -607,14 +675,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; }
@@ -1953,7 +2027,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 ;
@@ -1963,7 +2037,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 {
@@ -4639,7 +4713,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 */
@@ -4680,23 +4754,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 ); }
@@ -4706,6 +4763,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 )) { +/* 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
@@ -4714,7 +4791,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; }/*
@@ -4723,9 +4800,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; }/*
@@ -4740,7 +4815,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);
@@ -4757,12 +4832,34 @@ 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); } + }else { +#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); + resv_map->css = &h_cg->css; +#endif }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.24.1.735.g03f4e72817-goog
PATCH v9 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.
--- mm/hugetlb.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-)
mm/hugetlb.c +25 -1
@@ -1367,6 +1367,9 @@ 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++;
@@ -2189,10 +2192,19 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, gbl_chg =1 ; } +/* If this allocation is not consuming a reservation, charge it now. +*/ +if (map_chg || avoid_reserve || !vma_resv_map (vma)) { + 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);/*
@@ -2216,6 +2228,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 (!vma_resv_map (vma) || map_chg || avoid_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);
@@ -2241,6 +2261,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 (map_chg || avoid_reserve || !vma_resv_map (vma)) +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 );
--
2.24.1.735.g03f4e72817-goog
Re: [PATCH v9 6/8] hugetlb_cgroup: support noreserve mappings
Quoted 15 lines · Mina Almasry
mm/hugetlb.c +3
-0@@ -1367,6 +1367,9 @@ 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++;
mm/hugetlb.c struct page *alloc_huge_page(struct vm_area_struct *vma, · line 2197
Quoted 6 lines
mm/hugetlb.c +3
-0@@ -2189,10 +2192,19 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, gbl_chg =1 ; } +/* If this allocation is not consuming a reservation, charge it now. +*/ +if (map_chg || avoid_reserve || !vma_resv_map (vma)) {
These conditions are checked three times in this function, maybe better set a bool on the stack and use it throughout the function to guarantee it remains consistent?
bool deferred_reserve = !vma_resv_map(vma) || map_chg || avoid_reserve;
Quoted 43 lines
mm/hugetlb.c +19
-2+ 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);/* @@ -2216,6 +2228,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 (!vma_resv_map (vma) || map_chg || avoid_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);@@ -2241,6 +2261,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 (map_chg || avoid_reserve || !vma_resv_map (vma)) +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 ); --2.24.1.735.g03f4e72817-goog
PATCH v9 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 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.24.1.735.g03f4e72817-goog
PATCH v9 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 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 | 64 +++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v1/hugetlb.rst +53 -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>.reservation_limit_in_bytes # set/show limit of "hugepagesize" hugetlb reservations + hugetlb.<hugepagesize>.reservation_max_usage_in_bytes # show max "hugepagesize" hugetlb reservations and no-reserve faults. + hugetlb.<hugepagesize>.reservation_usage_in_bytes # show current reservations and no-reserve faults for "hugepagesize" hugetlb + hugetlb.<hugepagesize>.reservation_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,56 @@ files include:: hugetlb.1GB.max_usage_in_bytes hugetlb.1GB.usage_in_bytes hugetlb.1GB.failcnt + hugetlb.1GB.reservation_limit_in_bytes + hugetlb.1GB.reservation_max_usage_in_bytes + hugetlb.1GB.reservation_usage_in_bytes + hugetlb.1GB.reservation_failcnt hugetlb.64KB.limit_in_bytes hugetlb.64KB.max_usage_in_bytes hugetlb.64KB.usage_in_bytes hugetlb.64KB.failcnt + hugetlb.64KB.reservation_limit_in_bytes + hugetlb.64KB.reservation_max_usage_in_bytes + hugetlb.64KB.reservation_usage_in_bytes + hugetlb.64KB.reservation_failcnt hugetlb.32MB.limit_in_bytes hugetlb.32MB.max_usage_in_bytes hugetlb.32MB.usage_in_bytes hugetlb.32MB.failcnt + hugetlb.32MB.reservation_limit_in_bytes + hugetlb.32MB.reservation_max_usage_in_bytes + hugetlb.32MB.reservation_usage_in_bytes + hugetlb.32MB.reservation_failcnt + + +1. Reservation limits + +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. Reservation limits +are superior to Page fault limits (see section 2), 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. 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. + +2. Page fault limits + +The HugeTLB controller allows to limit the HugeTLB usage (page fault) 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. + + +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 hugetlbfs file is deleted, and not when the task that +caused the reservation or fault has exited.
--
2.24.1.735.g03f4e72817-goog
Re: [PATCH v9 8/8] hugetlb_cgroup: Add hugetlb_cgroup reservation docs
Quoted 25 lines · Mina Almasry
Documentation/admin-guide/cgroup-v1/hugetlb.rst +0
-10@@ -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
Documentation/admin-guide/cgroup-v1/hugetlb.rst process (bash) into it. · line 32
Quoted 10 lines
Documentation/admin-guide/cgroup-v1/hugetlb.rst +8
-1- hugetlb.<hugepagesize>.failcnt # show the number of allocation failure due to HugeTLB limit + hugetlb.<hugepagesize>.reservation_limit_in_bytes # set/show limit of "hugepagesize" hugetlb reservations + hugetlb.<hugepagesize>.reservation_max_usage_in_bytes # show max "hugepagesize" hugetlb reservations and no-reserve faults. + hugetlb.<hugepagesize>.reservation_usage_in_bytes # show current reservations and no-reserve faults for "hugepagesize" hugetlb + hugetlb.<hugepagesize>.reservation_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
I assume these are better named hugetlb.<hugepagesize>.reservation.* rather than reservation_*, or perhaps shortened to resv.*, so for example hugetlb.<hugepagesize>.resv.limit_in_bytes.
Quoted 19 lines
Documentation/admin-guide/cgroup-v1/hugetlb.rst +8
-0For a system supporting three hugepage sizes (64k, 32M and 1G), the control files include::@@ -40,11 +37,56 @@ files include:: hugetlb.1GB.max_usage_in_bytes hugetlb.1GB.usage_in_bytes hugetlb.1GB.failcnt + hugetlb.1GB.reservation_limit_in_bytes + hugetlb.1GB.reservation_max_usage_in_bytes + hugetlb.1GB.reservation_usage_in_bytes + hugetlb.1GB.reservation_failcnt hugetlb.64KB.limit_in_bytes hugetlb.64KB.max_usage_in_bytes hugetlb.64KB.usage_in_bytes hugetlb.64KB.failcnt + hugetlb.64KB.reservation_limit_in_bytes + hugetlb.64KB.reservation_max_usage_in_bytes + hugetlb.64KB.reservation_usage_in_bytes + hugetlb.64KB.reservation_failcnt hugetlb.32MB.limit_in_bytes
Documentation/admin-guide/cgroup-v1/hugetlb.rst files include:: · line 62
Quoted 10 lines
Documentation/admin-guide/cgroup-v1/hugetlb.rst +7
-0hugetlb.32MB.max_usage_in_bytes hugetlb.32MB.usage_in_bytes hugetlb.32MB.failcnt + hugetlb.32MB.reservation_limit_in_bytes + hugetlb.32MB.reservation_max_usage_in_bytes + hugetlb.32MB.reservation_usage_in_bytes + hugetlb.32MB.reservation_failcnt + + +1. Reservation limits
Should probably be described after the page fault limits since those are the canonical limits that already exist and the "reservation_.*" equivalents are supplementary.
Documentation/admin-guide/cgroup-v1/hugetlb.rst files include:: · line 73
Quoted 11 lines
Documentation/admin-guide/cgroup-v1/hugetlb.rst +11
-0+ +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. Reservation limits +are superior to Page fault limits (see section 2), 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. 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. +
When saying that reservation limits are superior to page fault limits, it might be helpful to expand on the downsides of page fault limits. The existing documentation calls out that the application needs to know its expected usage; it does not call attention to the fact that several different applications may be accessing an overcommited system-wide pool of hugetlb memory. So it might be possible that the application understands its own usage but it may not understand how that is orchestrated with other applications on the same system accessing a shared pool of hugetlb pages.
But yes, I think MAP_FAILED and allow for fallback or freeing of hugetlb memory is far superior to SIGBUS :)
Quoted 9 lines
Documentation/admin-guide/cgroup-v1/hugetlb.rst +9
-0+2. Page fault limits + +The HugeTLB controller allows to limit the HugeTLB usage (page fault) 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. +
Documentation/admin-guide/cgroup-v1/hugetlb.rst files include:: · line 92
Quoted 10 lines
Documentation/admin-guide/cgroup-v1/hugetlb.rst +10
-0+ +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 hugetlbfs file is deleted, and not when the task that +caused the reservation or fault has exited.
Discussion of reparenting?
Andrew Morton
maintainerQuoted 33 lines · On Tue, 17 Dec 2019 15:16:08 -0800 Mina Almasry <[email protected]> wrote:
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 allocate 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 allocate 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 developers interested in using
hugetlb_cgroups, and they have expressed dissatisfaction regarding this behavior. We'd like to improve this behavior such that tasks violating thehugetlb_cgrouplimits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in.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.Proposed Solution:
A new page counter named hugetlb.xMB.reservation_[limit|usage]_in_bytes. This counter has slightly different semantics than hugetlb.xMB.[limit|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.
Quoted 8 lines
- 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. We also added cgroup-v2 support to
hugetlb_cgroupso that the new use cases can be extended to v2.
This would make
http://lkml.kernel.org/r/[email protected]
obsolete?
Quoted 17 lines
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.
Quoted 1 line · Andrew Morton
On Tue, 17 Dec 2019 15:16:08 -0800 Mina Almasry <[email protected]> wrote:
<snip>
Quoted 7 lines
Quoted 3 lines
This proposal is implemented in this patch series, with tests to verify functionality and show the usage. We also added cgroup-v2 support to
hugetlb_cgroupso that the new use cases can be extended to v2.This would make
http://lkml.kernel.org/r/[email protected]
obsolete?
I haven't started looking at this series yet. However, since Mina was involved in the discussion of that patch (hugetlb controller for cgroups v2) my assumption is that this patch would simply build on that v2 support? Seems like the above patch would be a prereq for this series.
Mina, are those assumptions correct and perhaps this is an old/obsolete comment? Does this series apply 'on top' of the above patch? That patch is already in Andrew's tree.
Signature · 1 line
Mike Kravetz
Quoted 12 lines · Mike Kravetz
Quoted 1 line · Andrew Morton
On Tue, 17 Dec 2019 15:16:08 -0800 Mina Almasry <[email protected]> wrote:
<snip>
Quoted 7 lines
Quoted 3 lines
This proposal is implemented in this patch series, with tests to verify functionality and show the usage. We also added cgroup-v2 support to
hugetlb_cgroupso that the new use cases can be extended to v2.This would make
http://lkml.kernel.org/r/[email protected]
obsolete?
Quoted 8 lines
I haven't started looking at this series yet. However, since Mina was involved in the discussion of that patch (hugetlb controller for cgroups v2) my assumption is that this patch would simply build on that v2 support? Seems like the above patch would be a prereq for this series.
Mina, are those assumptions correct and perhaps this is an old/obsolete comment? Does this series apply 'on top' of the above patch? That patch is already in Andrew's tree.
Gah, I just forgot to update this commit message. This patch series is indeed on top of Giuseppe's v2 support and doesn't conflict or duplicate that functionality. Sorry for the confusion. I'll fix the commit message in the next iteration.
Quoted 2 lines
--
Mike Kravetz
Quoted 24 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 allocate 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 allocate 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 developers interested in using
hugetlb_cgroups, and they have expressed dissatisfaction regarding this behavior. We'd like to improve this behavior such that tasks violating thehugetlb_cgrouplimits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in.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 8 lines
Proposed Solution:
A new page counter named hugetlb.xMB.reservation_[limit|usage]_in_bytes. This counter has slightly different semantics than hugetlb.xMB.[limit|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.
To me, this implies that 'faults without reservations' could cause reservation usage to exceed reservation limit? Or, does the faulting process get a SIGBUS because of the reservation limit even though it is not using reservations?
We shall see in subsequent patches.
Quoted 9 lines
- 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. We also added cgroup-v2 support to
hugetlb_cgroupso that the new use cases can be extended to v2.
As previously discussed, cgroup-v2 support for hugetlb_cgroup will exist before this patch series.
Quoted 18 lines
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.
Quoted 8 lines
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]>Acked-by Hillf Danton <[email protected]>
I think the ACK by Hillf happened some time back. You may want to check to see if it still applies.
Quoted 5 lines
--- include/linux/hugetlb.h | 4 +- mm/hugetlb_cgroup.c | 116 +++++++++++++++++++++++++++++++++++----- 2 files changed, 106 insertions(+), 14 deletions(-)
Only one minor nit in the code.
You made this cleanup,
@@ -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];But did not make the same cleanup in __hugetlb_cgroup_file_legacy_init()
Signature · 1 line
Mike Kravetz
Quoted 35 lines · Mike Kravetz
Quoted 24 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 allocate 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 allocate 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 developers interested in using
hugetlb_cgroups, and they have expressed dissatisfaction regarding this behavior. We'd like to improve this behavior such that tasks violating thehugetlb_cgrouplimits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in.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 8 lines
Proposed Solution:
A new page counter named hugetlb.xMB.reservation_[limit|usage]_in_bytes. This counter has slightly different semantics than hugetlb.xMB.[limit|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.
Quoted 7 lines
To me, this implies that 'faults without reservations' could cause reservation usage to exceed reservation limit? Or, does the faulting process get a SIGBUS because of the reservation limit even though it is not using reservations?
We shall see in subsequent patches.
The design we went with based on previous discussions is as follows:
hugetlb pages faulted without a prior reservation get accounted at fault time, rather than reservation time, and if the fault causes the counter to cross the limit, the charge fails, hence the fault fails, hence the process gets sigbus'd.
This means that one counter I'm adding here can cover both use cases:
if the userspace uses MAP_NORESERVE, then their memory is accounted at fault time and they may get sigbus'd. If the userspace does *not* use MAP_NORESERVE, then their memory is charged at reservation (mmap) time and should be completely immune to runtime sigbus, unless they do something really weird like reserve a bunch of memory, punch a hole in the reserved memory, lower their limit, then fault in memory from the hole.
Quoted 6 lines
Quoted 6 lines
- 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.
Quoted 7 lines
Quoted 3 lines
This proposal is implemented in this patch series, with tests to verify functionality and show the usage. We also added cgroup-v2 support to
hugetlb_cgroupso that the new use cases can be extended to v2.As previously discussed, cgroup-v2 support for
hugetlb_cgroupwill exist before this patch series.
Yes, this will be fixed in the next iteration.
Quoted 22 lines
Quoted 22 lines
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.
Quoted 8 lines
Quoted 4 lines
[1]: https://www.kernel.org/doc/html/latest/vm/hugetlbfs_reserv.html
Signed-off-by Mina Almasry <[email protected]>Acked-by Hillf Danton <[email protected]>I think the ACK by Hillf happened some time back. You may want to check to see if it still applies.
Yes, will remove the ack in the next iteration. I'll re-add it if Hilf reviews again.
Quoted 19 lines
Quoted 5 lines
--- include/linux/hugetlb.h | 4 +- mm/hugetlb_cgroup.c | 116 +++++++++++++++++++++++++++++++++++----- 2 files changed, 106 insertions(+), 14 deletions(-)Only one minor nit in the code.
You made this cleanup,
mm/hugetlb_cgroup.c +1
-1@@ -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 ];
Quoted 1 line
But did not make the same cleanup in
__hugetlb_cgroup_file_legacy_init()
Will be fixed in the next iteration.
Quoted 2 lines
--
Mike Kravetz
Quoted 12 lines · Mina Almasry
Quoted 11 lines · Mike Kravetz
Quoted 3 lines · Mina Almasry
- 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.To me, this implies that 'faults without reservations' could cause reservation usage to exceed reservation limit? Or, does the faulting process get a SIGBUS because of the reservation limit even though it is not using reservations?
Quoted 8 lines
Quoted 2 lines
We shall see in subsequent patches.
The design we went with based on previous discussions is as follows:
hugetlb pages faulted without a prior reservation get accounted at fault time, rather than reservation time, and if the fault causes the counter to cross the limit, the charge fails, hence the fault fails, hence the process gets sigbus'd.
Ok, sorry I did not recall the design discussion.
Quoted 3 lines
This means that one counter I'm adding here can cover both use cases:
if the userspace usesMAP_NORESERVE, then their memory is accounted at fault time and they may get sigbus'd.
Let's make sure this is clearly documented. Someone could be surprised if their application not using reserves gets a SIGBUS because there is a reserve limit.
Signature · 1 line
Mike Kravetz
Quoted 10 lines
Quoted 7 lines
Quoted 1 line · Mina Almasry
The design we went with based on previous discussions is as follows:
hugetlb pages faulted without a prior reservation get accounted at fault time, rather than reservation time, and if the fault causes the counter to cross the limit, the charge fails, hence the fault fails, hence the process gets sigbus'd.Ok, sorry I did not recall the design discussion.
No worries! It has indeed been a while since that discussion.
Quoted 7 lines
Quoted 3 lines
This means that one counter I'm adding here can cover both use cases:
if the userspace usesMAP_NORESERVE, then their memory is accounted at fault time and they may get sigbus'd.Let's make sure this is clearly documented. Someone could be surprised if their application not using reserves gets a SIGBUS because there is a reserve limit.
I have some stuff on that already in the docs patch, but I'll beef that section up to ensure there is no confusion.
Quoted 4 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.
Quoted 9 lines
This is patch 1 of an 8 patch series.
Problem:
Currently tasks attempting to allocate 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 allocate 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.
I think it's subtle, but the use of the word "allocate" instead of using "reserve" might be confusing here. Might want to reword it.
Quoted 6 lines
We have developers interested in using
hugetlb_cgroups, and they have expressed dissatisfaction regarding this behavior. We'd like to improve this behavior such that tasks violating thehugetlb_cgrouplimits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in.
I'm not sure the developers are interested in being restricted by hugetlb_cgroups :) I think users get constrained by hugetlb_cgroup so the developers are interested in the failure more: do we want to SIGBUS at fault and not be allowed an opportunity to influence that (today) or do we want to fallback to non-hugetlbfs memory and just keep going (tomorrow, after your patchset).
Quoted 10 lines
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.Proposed Solution:
A new page counter named hugetlb.xMB.reservation_[limit|usage]_in_bytes. This counter has slightly different semantics than hugetlb.xMB.[limit|usage]_in_bytes:
Is there a max_usage_in_bytes equivalent? It's a page_counter so I assume it's easy to support.
I'll defer the naming to Mike here, "rsvd" seems to be the hugetlb way of saying "reserved".
Quoted 38 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. We also added cgroup-v2 support to
hugetlb_cgroupso that the new use cases can be extended to v2.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]>Acked-by Hillf Danton <[email protected]>