hugetlb_cgroup: Add hugetlb_cgroup reservation limits
Subsystems (5)
v5 cover hugetlb_cgroup: Add hugetlb_cgroup reservation limits
Patch series implements hugetlb_cgroup reservation usage and limits, which track hugetlb reservations rather than hugetlb memory faulted in. Details of the approach is 1/7.
Changes in v5:
- Moved the bulk of the description to the first patch in the series.
- Clang formatted the entire series.
- Split off 'hugetlb: remove duplicated code' and 'hugetlb: region_chg provides only cache entry' into their own patch series.
- Added comments to HUGETLB_RES enum.
- Fixed bug in 'hugetlb: disable region_add file_region coalescing' calculating the wrong number of regions_needed in some cases.
- Changed sleeps in test to proper conditions.
- Misc fixes in test based on shuah@ review.
Changes in v4:
- Split up 'hugetlb_cgroup: add accounting for shared mappings' into 4 patches for better isolation and context on the indvidual changes:
- hugetlb_cgroup: add accounting for shared mappings
- hugetlb: disable region_add file_region coalescing
- hugetlb: remove duplicated code
- hugetlb: region_chg provides only cache entry
- Fixed resv->adds_in_progress accounting.
- Retained behavior that region_add never fails, in earlier patchsets region_add could return failure.
- Fixed libhugetlbfs failure.
- Minor fix to the added tests that was preventing them from running on some environments.
Changes in v3:
- Addressed comments of Hillf Danton:
- Added docs.
- cgroup_files now uses enum.
- Various readability improvements.
- Addressed comments of Mike Kravetz.
- region_* functions no longer coalesce file_region entries in the resv_map.
- region_add() and region_chg() refactored to make them much easier to understand and remove duplicated code so this patch doesn't add too much complexity.
- Refactored common functionality into helpers.
Changes in v2:
- Split the patch into a 5 patch series.
- Fixed patch subject.
Mina Almasry (7):
hugetlb_cgroup: Add hugetlb_cgroup reservation counter
hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
hugetlb_cgroup: add reservation accounting for private mappings
hugetlb: disable region_add file_region coalescing
hugetlb_cgroup: add accounting for shared mappings
hugetlb_cgroup: Add hugetlb_cgroup reservation tests
hugetlb_cgroup: Add hugetlb_cgroup reservation docs
.../admin-guide/cgroup-v1/hugetlb.rst | 85 +++- include/linux/hugetlb.h | 31 +- include/linux/hugetlb_cgroup.h | 33 +- mm/hugetlb.c | 423 +++++++++++----- mm/hugetlb_cgroup.c | 190 ++++++-- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + .../selftests/vm/charge_reserved_hugetlb.sh | 461 ++++++++++++++++++ .../selftests/vm/write_hugetlb_memory.sh | 22 + .../testing/selftests/vm/write_to_hugetlbfs.c | 250 ++++++++++ 10 files changed, 1306 insertions(+), 191 deletions(-) create mode 100755 tools/testing/selftests/vm/charge_reserved_hugetlb.sh create mode 100644 tools/testing/selftests/vm/write_hugetlb_memory.sh create mode 100644 tools/testing/selftests/vm/write_to_hugetlbfs.c
--
2.23.0.351.gc4317032e6-goog
PATCH v5 1/7 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.
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.
- If a task attempts to reserve more memory than limit_in_bytes allows, the kernel will allow it to do so. But if a task attempts to reserve more memory than reservation_limit_in_bytes, the kernel will fail this reservation.
This proposal is implemented in this patch series, with tests to verify functionality and show the usage.
Alternatives considered:
1. A new cgroup, instead of only a new page_counter attached to the existing hugetlb_cgroup. Adding a new cgroup seemed like a lot of code duplication with hugetlb_cgroup. Keeping hugetlb related page counters under hugetlb_cgroup seemed cleaner as well.
2. Instead of adding a new counter, we considered adding a sysctl that modifies the behavior of hugetlb.xMB.[limit|usage]_in_bytes, to do accounting at reservation time rather than fault time. Adding a new page_counter seems better as userspace could, if it wants, choose to enforce different cgroups
differently: one via limit_in_bytes, and another via reservation_limit_in_bytes. This could be very useful if you're transitioning how hugetlb memory is partitioned on your system one cgroup at a time, for example. Also, someone may find usage for both limit_in_bytes and reservation_limit_in_bytes concurrently, and this approach gives them the option to do so.
Caveats:
1. This support is implemented for cgroups-v1. I have not tried hugetlb_cgroups with cgroups v2, and AFAICT it's not supported yet. This is largely because we use cgroups-v1 for now. If required, I can add hugetlb_cgroup support to cgroups v2 in this patch or
a follow up.
2. Most complicated bit of this patch I believe is: where to store the pointer to the hugetlb_cgroup to uncharge at unreservation time? Normally the cgroup pointers hang off the struct page. But, with hugetlb_cgroup reservations, one task can reserve a specific page and another task may fault it in (I believe), so storing the pointer in struct page is not appropriate. Proposed approach here is to store the pointer in the resv_map. See patch for details.
Testing:
- Added tests passing.
- libhugetlbfs tests mostly passing, but some tests have trouble with and without this patch series. Seems environment issue rather than code:
- Overall results:
********** TEST SUMMARY
* 2M
* 32-bit 64-bit
* Total testcases: 84 0
* Skipped: 0 0
* PASS: 66 0
* FAIL: 14 0
* Killed by signal: 0 0
* Bad configuration: 4 0
* Expected FAIL: 0 0
* Unexpected PASS: 0 0
* Test not present: 0 0
* Strange test result: 0 0
**********
- Failing tests:
- elflink_rw_and_share_test("linkhuge_rw") segfaults with and without this patch series.
- LD_PRELOAD=libhugetlbfs.so HUGETLB_MORECORE=yes malloc (2M: 32):
FAIL Address is not hugepage
- LD_PRELOAD=libhugetlbfs.so HUGETLB_RESTRICT_EXE=unknown:malloc HUGETLB_MORECORE=yes malloc (2M: 32):
FAIL Address is not hugepage
- LD_PRELOAD=libhugetlbfs.so HUGETLB_MORECORE=yes malloc_manysmall (2M: 32):
FAIL Address is not hugepage
- GLIBC_TUNABLES=glibc.malloc.tcache_count=0 LD_PRELOAD=libhugetlbfs.so HUGETLB_MORECORE=yes heapshrink (2M: 32):
FAIL Heap not on hugepages
- GLIBC_TUNABLES=glibc.malloc.tcache_count=0 LD_PRELOAD=libhugetlbfs.so libheapshrink.so HUGETLB_MORECORE=yes heapshrink (2M: 32):
FAIL Heap not on hugepages
- HUGETLB_ELFMAP=RW linkhuge_rw (2M: 32): FAIL small_data is not hugepage
- HUGETLB_ELFMAP=RW HUGETLB_MINIMAL_COPY=no linkhuge_rw (2M: 32):
FAIL small_data is not hugepage
- alloc-instantiate-race shared (2M: 32):
Bad configuration: sched_setaffinity(cpu1): Invalid argument -
FAIL Child 1 killed by signal Killed
- shmoverride_linked (2M: 32):
FAIL shmget failed size 2097152 from line 176: Invalid argument
- HUGETLB_SHM=yes shmoverride_linked (2M: 32):
FAIL shmget failed size 2097152 from line 176: Invalid argument
- shmoverride_linked_static (2M: 32):
FAIL shmget failed size 2097152 from line 176: Invalid argument
- HUGETLB_SHM=yes shmoverride_linked_static (2M: 32):
FAIL shmget failed size 2097152 from line 176: Invalid argument
- LD_PRELOAD=libhugetlbfs.so shmoverride_unlinked (2M: 32):
FAIL shmget failed size 2097152 from line 176: Invalid argument
- LD_PRELOAD=libhugetlbfs.so HUGETLB_SHM=yes shmoverride_unlinked (2M: 32):
FAIL shmget failed size 2097152 from line 176: Invalid argument
[1]: https://www.kernel.org/doc/html/latest/vm/hugetlbfs_reserv.html
--- include/linux/hugetlb.h | 23 ++++++++- mm/hugetlb_cgroup.c | 111 ++++++++++++++++++++++++++++++---------- 2 files changed, 107 insertions(+), 27 deletions(-)
include/linux/hugetlb.h +22 -1
@@ -320,6 +320,27 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,#ifdef CONFIG_HUGETLB_PAGE +enum { +/* Tracks hugetlb memory faulted in. */ +HUGETLB_RES_USAGE , +/* Tracks hugetlb memory reserved. */ +HUGETLB_RES_RESERVATION_USAGE , +/* Limit for hugetlb memory faulted in. */ +HUGETLB_RES_LIMIT , +/* Limit for hugetlb memory reserved. */ +HUGETLB_RES_RESERVATION_LIMIT , +/* Max usage for hugetlb memory faulted in. */ +HUGETLB_RES_MAX_USAGE , +/* Max usage for hugetlb memory reserved. */ +HUGETLB_RES_RESERVATION_MAX_USAGE , +/* Faulted memory accounting fail count. */ +HUGETLB_RES_FAILCNT , +/* Reserved memory accounting fail count. */ +HUGETLB_RES_RESERVATION_FAILCNT , +HUGETLB_RES_NULL , +HUGETLB_RES_MAX , +}; +#define HSTATE_NAME_LEN 32/* Defines one hugetlb page size */ struct hstate {
@@ -340,7 +361,7 @@ struct hstate {unsigned int surplus_huge_pages_node[MAX_NUMNODES ];#ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ -struct cftype cgroup_files[5 ]; +struct cftype cgroup_files[HUGETLB_RES_MAX ];#endif char name[HSTATE_NAME_LEN ]; };
mm/hugetlb_cgroup.c +85 -26
197 lines not on this page · raw message
--
2.23.0.351.gc4317032e6-goog
PATCH v5 2/7 hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
In reply to Mina AlmasryAugments 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.
--- include/linux/hugetlb_cgroup.h | 22 ++++++---- mm/hugetlb.c | 6 ++- mm/hugetlb_cgroup.c | 77 ++++++++++++++++++++++++++++------ 3 files changed, 83 insertions(+), 22 deletions(-)
include/linux/hugetlb_cgroup.h +14 -8
@@ -52,14 +52,19 @@ 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);extern void hugetlb_cgroup_uncharge_page (int idx,unsigned long nr_pages,struct page *page);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); +extern void hugetlb_cgroup_file_init (void )__init ;extern void hugetlb_cgroup_migrate (struct page *oldhpage,struct page *newhpage);
@@ -81,9 +86,9 @@ 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 ; }
@@ -100,9 +105,10 @@ hugetlb_cgroup_uncharge_page(int idx, unsigned long nr_pages, struct page *page) { } -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 +4 -2
@@ -2032,7 +2032,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;
@@ -2080,7 +2081,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 +65 -12
138 lines not on this page · raw message
--
2.23.0.351.gc4317032e6-goog
PATCH v5 3/7 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.
--- include/linux/hugetlb.h | 8 +++++++ include/linux/hugetlb_cgroup.h | 11 +++++++++ mm/hugetlb.c | 44 +++++++++++++++++++++++++++++++++- mm/hugetlb_cgroup.c | 12 ---------- 4 files changed, 62 insertions(+), 13 deletions(-)
include/linux/hugetlb.h +8 -0
@@ -46,6 +46,14 @@ 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; +#endif };extern struct resv_map *resv_map_alloc (void );void resv_map_release (struct kref *ref);
include/linux/hugetlb_cgroup.h +11 -0
@@ -25,6 +25,17 @@ struct hugetlb_cgroup;#define HUGETLB_CGROUP_MIN_ORDER 2#ifdef CONFIG_CGROUP_HUGETLB +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 ]; +};static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page) {
mm/hugetlb.c +43 -1
80 lines not on this page · raw message
mm/hugetlb_cgroup.c +0 -12
@@ -19,18 +19,6 @@#include <linux/hugetlb.h> #include <linux/hugetlb_cgroup.h> -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 ]; -}; -#define MEMFILE_PRIVATE (x, val) (((x) << 16) | (val))#define MEMFILE_IDX (val) (((val) >> 16) & 0xffff)#define MEMFILE_ATTR (val) ((val) & 0xffff)
--
2.23.0.351.gc4317032e6-goog
PATCH v5 4/7 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.
--- mm/hugetlb.c | 273 +++++++++++++++++++++++++++++---------------------- 1 file changed, 158 insertions(+), 115 deletions(-)
mm/hugetlb.c +158 -115
@@ -244,6 +244,12 @@ 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); +/* 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.
@@ -251,51 +257,61 @@ struct file_region {static long add_reservation_in_range (struct resv_map *resv,long f,long t,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; - - 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); + } } - 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); + } + last_accounted_offset = t; } -return chg; +return add; }/*
@@ -305,46 +321,24 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,* must exist in the cache due to the previous call to region_chg with * the same range. * +* 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. */ -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 regions_needed) { -struct list_head *head = &resv->regions; -struct file_region *rg, *nrg;long add =0 ;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 ; -/* -* 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. -*/ -if (&rg->link == head || t < rg->from) { -VM_BUG_ON (resv->region_cache_count <=0 ); - - resv->region_cache_count--; - nrg = list_first_entry(&resv->region_cache, struct file_region, - link); -list_del (&nrg->link); - - nrg->from = f; - nrg->to = t; -list_add (&nrg->link, rg->link.prev); - - add += t - f; - goto out_locked; - } +VM_BUG_ON (resv->region_cache_count < regions_needed); add =add_reservation_in_range (resv, f, t, false); + resv->adds_in_progress -= regions_needed; -out_locked: - resv->adds_in_progress--;spin_unlock (&resv->lock);VM_BUG_ON (add <0 );return add;
@@ -361,42 +355,55 @@ static long region_add(struct resv_map *resv, long f, long t)* as a placeholder, so that the subsequent region_add * call will have all the regions it needs and will not fail. * +* out_regions_needed is the number of regions added to the +* resv->region_cache_count. 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) { +struct file_region *trg =NULL ;long chg =0 ; +/* Allocate the maximum number of regions we're going to need for this +* reservation. The maximum number of regions we're going to need is +* (t - f) / 2 + 1, which corresponds to a region with alternating +* reserved and unreserved pages. +*/ + *out_regions_needed = (t - f) /2 +1 ; +retry: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, true);/* * 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 ); +if (resv->region_cache_count < *out_regions_needed) {/* Must drop lock to allocate a new descriptor. */ - resv->adds_in_progress--;spin_unlock (&resv->lock); - trg =kmalloc (sizeof (*trg),GFP_KERNEL ); -if (!trg) -return -ENOMEM ; +while (resv->region_cache_count < *out_regions_needed +1 ) { + trg =kmalloc (sizeof (*trg),GFP_KERNEL ); +if (!trg) +return -ENOMEM ; -spin_lock (&resv->lock); -list_add (&trg->link, &resv->region_cache); - resv->region_cache_count++; - goto retry_locked; +spin_lock (&resv->lock); +list_add (&trg->link, &resv->region_cache); + resv->region_cache_count++; +spin_unlock (&resv->lock); + } + goto retry; } - chg =add_reservation_in_range (resv, f, t, true); + resv->adds_in_progress += *out_regions_needed;spin_unlock (&resv->lock);return chg;
@@ -407,17 +414,19 @@ 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); }
@@ -1866,9 +1875,10 @@ enum vma_resv_mode {VMA_END_RESV , VMA_ADD_RESV, }; -static long __vma_reservation_common (struct hstate *h, -struct vm_area_struct *vma,unsigned long addr, -enum vma_resv_mode mode) +static long +__vma_reservation_common (struct hstate *h,struct vm_area_struct *vma, +unsigned long addr,enum vma_resv_mode mode, +long *out_regions_needed,long in_regions_needed) {struct resv_map *resv;pgoff_t idx;
@@ -1881,20 +1891,24 @@ 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 ); +VM_BUG_ON (!out_regions_needed); + ret =region_chg (resv, idx, idx +1 , out_regions_needed);break ;case VMA_COMMIT_RESV : - ret =region_add (resv, idx, idx +1 ); +VM_BUG_ON (in_regions_needed ==-1 ); + ret =region_add (resv, idx, idx +1 , in_regions_needed);break ;case VMA_END_RESV : -region_abort (resv, idx, idx +1 ); +VM_BUG_ON (in_regions_needed ==-1 ); +region_abort (resv, idx, idx +1 , in_regions_needed); ret =0 ;break ;case VMA_ADD_RESV : +VM_BUG_ON (in_regions_needed ==-1 );if (vma->vm_flags &VM_MAYSHARE ) - ret =region_add (resv, idx, idx +1 ); + ret =region_add (resv, idx, idx +1 , in_regions_needed);else { -region_abort (resv, idx, idx +1 ); +region_abort (resv, idx, idx +1 , in_regions_needed); ret =region_del (resv, idx, idx +1 ); }break ;
@@ -1927,28 +1941,32 @@ static long __vma_reservation_common(struct hstate *h,return ret <0 ? ret :0 ; } -static long vma_needs_reservation (struct hstate *h, -struct vm_area_struct *vma,unsigned long addr) +static long vma_needs_reservation (struct hstate *h,struct vm_area_struct *vma, +unsigned long addr,long *out_regions_needed) { -return __vma_reservation_common (h, vma, addr,VMA_NEEDS_RESV ); +return __vma_reservation_common (h, vma, addr,VMA_NEEDS_RESV , + out_regions_needed,-1 ); } -static long vma_commit_reservation (struct hstate *h, -struct vm_area_struct *vma,unsigned long addr) +static long vma_commit_reservation (struct hstate *h,struct vm_area_struct *vma, +unsigned long addr,long regions_needed) { -return __vma_reservation_common (h, vma, addr,VMA_COMMIT_RESV ); +return __vma_reservation_common (h, vma, addr,VMA_COMMIT_RESV ,NULL , + regions_needed); } -static void vma_end_reservation (struct hstate *h, -struct vm_area_struct *vma,unsigned long addr) +static void vma_end_reservation (struct hstate *h,struct vm_area_struct *vma, +unsigned long addr,long regions_needed) { - (void )__vma_reservation_common (h, vma, addr,VMA_END_RESV ); + (void )__vma_reservation_common (h, vma, addr,VMA_END_RESV ,NULL , + regions_needed); } -static long vma_add_reservation (struct hstate *h, -struct vm_area_struct *vma,unsigned long addr) +static long vma_add_reservation (struct hstate *h,struct vm_area_struct *vma, +unsigned long addr,long regions_needed) { -return __vma_reservation_common (h, vma, addr,VMA_ADD_RESV ); +return __vma_reservation_common (h, vma, addr,VMA_ADD_RESV ,NULL , + regions_needed); }/*
@@ -1966,8 +1984,10 @@ static void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, unsigned long address, struct page *page) { + long regions_needed = 0;if (unlikely (PagePrivate (page))) { - long rc = vma_needs_reservation(h, vma, address); +long rc = +vma_needs_reservation (h, vma, address, ®ions_needed);if (unlikely (rc <0 )) {/*
@@ -1983,7 +2003,8 @@ static void restore_reserve_on_error(struct hstate *h,*/ ClearPagePrivate (page); }else if (rc) { - rc =vma_add_reservation (h, vma, address); + rc =vma_add_reservation (h, vma, address, + regions_needed);if (unlikely (rc <0 ))/* * See above comment about rare out of
@@ -1991,7 +2012,7 @@ static void restore_reserve_on_error(struct hstate *h,*/ ClearPagePrivate (page); }else -vma_end_reservation (h, vma, address); +vma_end_reservation (h, vma, address, regions_needed); } }
@@ -2005,6 +2026,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,long gbl_chg;int ret, idx;struct hugetlb_cgroup *h_cg; +long regions_needed =0 ; idx =hstate_index (h);/*
@@ -2012,7 +2034,8 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,* has a reservation for the page to be allocated. A return * code of zero indicates a reservation exists (no change). */ - map_chg = gbl_chg =vma_needs_reservation (h, vma, addr); + map_chg = gbl_chg = +vma_needs_reservation (h, vma, addr, ®ions_needed);if (map_chg <0 )return ERR_PTR (-ENOMEM );
@@ -2026,7 +2049,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,if (map_chg || avoid_reserve) { gbl_chg =hugepage_subpool_get_pages (spool,1 );if (gbl_chg <0 ) { -vma_end_reservation (h, vma, addr); +vma_end_reservation (h, vma, addr, regions_needed);return ERR_PTR (-ENOSPC ); }
@@ -2072,7 +2095,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,set_page_private (page, (unsigned long )spool); - map_commit =vma_commit_reservation (h, vma, addr); + map_commit =vma_commit_reservation (h, vma, addr, regions_needed);if (unlikely (map_chg > map_commit)) {/* * The page was added to the reservation map between
@@ -2096,7 +2119,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, out_subpool_put:if (map_chg || avoid_reserve)hugepage_subpool_put_pages (spool,1 ); -vma_end_reservation (h, vma, addr); +vma_end_reservation (h, vma, addr, regions_needed);return ERR_PTR (-ENOSPC ); }
@@ -3780,6 +3803,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,spinlock_t *ptl;unsigned long haddr = address &huge_page_mask (h);bool new_page = false; +long regions_needed =0 ;/* * Currently, we are forced to kill the process in the event the
@@ -3897,12 +3921,12 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,* the spinlock. */ if ((flags &FAULT_FLAG_WRITE ) && !(vma->vm_flags &VM_SHARED )) { -if (vma_needs_reservation (h, vma, haddr) <0 ) { +if (vma_needs_reservation (h, vma, haddr, ®ions_needed) <0 ) { ret =VM_FAULT_OOM ; goto backout_unlocked; }/* Just decrements count, does not deallocate */ -vma_end_reservation (h, vma, haddr); +vma_end_reservation (h, vma, haddr, regions_needed); } ptl =huge_pte_lock (h, mm, ptep);
@@ -3992,6 +4016,7 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,struct address_space *mapping;int need_wait_lock =0 ;unsigned long haddr = address &huge_page_mask (h); +long regions_needed =0 ; ptep =huge_pte_offset (mm, haddr,huge_page_size (h));if (ptep) {
@@ -4046,12 +4071,12 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,* consumed. */ if ((flags &FAULT_FLAG_WRITE ) && !huge_pte_write (entry)) { -if (vma_needs_reservation (h, vma, haddr) <0 ) { +if (vma_needs_reservation (h, vma, haddr, ®ions_needed) <0 ) { ret =VM_FAULT_OOM ; goto out_mutex; }/* Just decrements count, does not deallocate */ -vma_end_reservation (h, vma, haddr); +vma_end_reservation (h, vma, haddr, regions_needed);if (!(vma->vm_flags &VM_MAYSHARE )) pagecache_page = hugetlbfs_pagecache_page(h,
@@ -4514,7 +4539,7 @@ int hugetlb_reserve_pages(struct 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) {
@@ -4544,7 +4569,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. */
@@ -4614,7 +4639,7 @@ 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); +long add =region_add (resv_map, from, to, regions_needed);if (unlikely (chg > add)) {/*
@@ -4636,7 +4661,7 @@ int hugetlb_reserve_pages(struct inode *inode,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); +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;
@@ -5060,3 +5085,21 @@ void move_hugetlb_state(struct page *oldpage, struct page *newpage, int reason)spin_unlock (&hugetlb_lock); } } + +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; +}
--
2.23.0.351.gc4317032e6-goog
Re: [PATCH v5 4/7] hugetlb: disable region_add file_region coalescing
Quoted 36 lines · Mina Almasry
A follow up patch in this series adds hugetlb cgroup uncharge info the
file_regionentries inresv->regions. The cgroup uncharge info may differ for different regions, so they can no longer be coalesced atregion_addtime. So, disable region coalescing inregion_addin this patch.Behavior change:
Say a
resv_mapexists like this [0->1], [2->3], and [5->6].Then a region_chg/add call comes in region_chg/add(f=0, t=5).
Old code would generate
resv->regions: [0->5], [5->6].
New code would generateresv->regions: [0->1], [1->2], [2->3], [3->5], [5->6].Special care needs to be taken to handle the
resv->adds_in_progressvariable correctly. In the past, only 1 region would be added for everyregion_chgandregion_addcall. But now, each call may add multiple regions, so we can no longer incrementadds_in_progressby 1 inregion_chg, or decrementadds_in_progressby 1 afterregion_addorregion_abort. Instead,region_chgcallsadd_reservation_in_range()to count the number of regions needed and allocates those, and that info is passed toregion_addandregion_abortto decrementadds_in_progresscorrectly.Signed-off-by Mina Almasry <[email protected]>--- mm/hugetlb.c | 273 +++++++++++++++++++++++++++++---------------------- 1 file changed, 158 insertions(+), 115 deletions(-)mm/hugetlb.c +0
-0
mm/hugetlb.c struct file_region { · line 252
Quoted 9 lines
mm/hugetlb.c +6
-0@@ -244,6 +244,12 @@ 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); +
Instead of the forward declaration, just put the function here.
mm/hugetlb.c static long add_reservation_in_range(struct resv_map *resv, long f, long t,
Quoted 97 lines
mm/hugetlb.c +41
-31/* 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. @@ -251,51 +257,61 @@ struct file_region {static long add_reservation_in_range (struct resv_map *resv,long f,long t,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; - - 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); + } } - 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); + } + last_accounted_offset = t; } -return chg; +return add; }/* @@ -305,46 +321,24 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,
The start of this comment block says,
/*
* 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.
We are no longer expanding existing regions. Correct?
As an optimization, I guess we could coalesce/combine reion entries as long as they are for the same cgroup. However, it may not be worth the effort.
Quoted 37 lines
mm/hugetlb.c +5
-21* must exist in the cache due to the previous call to region_chg with * the same range. * +* 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. */ -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 regions_needed) { -struct list_head *head = &resv->regions; -struct file_region *rg, *nrg;long add =0 ;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 ; -/* -* 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. -*/ -if (&rg->link == head || t < rg->from) { -VM_BUG_ON (resv->region_cache_count <=0 ); - - resv->region_cache_count--; - nrg = list_first_entry(&resv->region_cache, struct file_region, - link); -list_del (&nrg->link); - - nrg->from = f;
mm/hugetlb.c static long add_reservation_in_range(struct resv_map *resv, long f, long t, · line 340
Quoted 10 lines
mm/hugetlb.c +2
-6- nrg->to = t; -list_add (&nrg->link, rg->link.prev); - - add += t - f; - goto out_locked; - } +VM_BUG_ON (resv->region_cache_count < regions_needed); add =add_reservation_in_range (resv, f, t, false); + resv->adds_in_progress -= regions_needed;
Consider this example,
- region_chg(1,2)
adds_in_progress = 1
cache entries 1
- region_chg(3,4)
adds_in_progress = 2
cache entries 2
- region_chg(5,6)
adds_in_progress = 3
cache entries 3
At this point, no region descriptors are in the map because only region_chg has been called.
- region_chg(0,6)
adds_in_progress = 4
cache entries 4
Is that correct so far?
Then the following sequence happens,
- region_add(1,2)
adds_in_progress = 3
cache entries 3
- region_add(3,4)
adds_in_progress = 2
cache entries 2
- region_add(5,6)
adds_in_progress = 1
cache entries 1
list of region descriptors is:
[1->2] [3->4] [5->6]
- region_add(0,6)
This is going to require 3 cache entries but only one is in the cache. I think we are going to BUG in get_file_region_entry_from_cache() the second time it is called from add_reservation_in_range().
I stopped looking at the code here as things will need to change if this is a real issue.
Signature · 1 line
Mike Kravetz
Re: [PATCH v5 4/7] hugetlb: disable region_add file_region coalescing
Quoted 244 lines · Mike Kravetz
Quoted 36 lines · Mina Almasry
A follow up patch in this series adds hugetlb cgroup uncharge info the
file_regionentries inresv->regions. The cgroup uncharge info may differ for different regions, so they can no longer be coalesced atregion_addtime. So, disable region coalescing inregion_addin this patch.Behavior change:
Say a
resv_mapexists like this [0->1], [2->3], and [5->6].Then a region_chg/add call comes in region_chg/add(f=0, t=5).
Old code would generate
resv->regions: [0->5], [5->6].
New code would generateresv->regions: [0->1], [1->2], [2->3], [3->5], [5->6].Special care needs to be taken to handle the
resv->adds_in_progressvariable correctly. In the past, only 1 region would be added for everyregion_chgandregion_addcall. But now, each call may add multiple regions, so we can no longer incrementadds_in_progressby 1 inregion_chg, or decrementadds_in_progressby 1 afterregion_addorregion_abort. Instead,region_chgcallsadd_reservation_in_range()to count the number of regions needed and allocates those, and that info is passed toregion_addandregion_abortto decrementadds_in_progresscorrectly.Signed-off-by Mina Almasry <[email protected]>--- mm/hugetlb.c | 273 +++++++++++++++++++++++++++++---------------------- 1 file changed, 158 insertions(+), 115 deletions(-)mm/hugetlb.c +0
-0mm/hugetlb.c struct file_region { · line 252
Quoted 9 lines
mm/hugetlb.c +6
-0@@ -244,6 +244,12 @@ 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); +Instead of the forward declaration, just put the function here.
mm/hugetlb.c static long add_reservation_in_range(struct resv_map *resv, long f, long t,
Quoted 97 lines
mm/hugetlb.c +41
-31/* 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. @@ -251,51 +257,61 @@ struct file_region {static long add_reservation_in_range (struct resv_map *resv,long f,long t,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; - - 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); + } } - 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); + } + last_accounted_offset = t; } -return chg; +return add; }/* @@ -305,46 +321,24 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,The start of this comment block says,
/*
* 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.We are no longer expanding existing regions. Correct?
As an optimization, I guess we could coalesce/combine reion entries as long as they are for the same cgroup. However, it may not be worth the effort.Quoted 37 lines
mm/hugetlb.c +5
-21* must exist in the cache due to the previous call to region_chg with * the same range. * +* 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. */ -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 regions_needed) { -struct list_head *head = &resv->regions; -struct file_region *rg, *nrg;long add =0 ;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 ; -/* -* 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. -*/ -if (&rg->link == head || t < rg->from) { -VM_BUG_ON (resv->region_cache_count <=0 ); - - resv->region_cache_count--; - nrg = list_first_entry(&resv->region_cache, struct file_region, - link); -list_del (&nrg->link); - - nrg->from = f;mm/hugetlb.c static long add_reservation_in_range(struct resv_map *resv, long f, long t, · line 340
Quoted 10 lines
mm/hugetlb.c +2
-6- nrg->to = t; -list_add (&nrg->link, rg->link.prev); - - add += t - f; - goto out_locked; - } +VM_BUG_ON (resv->region_cache_count < regions_needed); add =add_reservation_in_range (resv, f, t, false); + resv->adds_in_progress -= regions_needed;Consider this example,
-
region_chg(1,2)
adds_in_progress= 1
cache entries 1
-region_chg(3,4)
adds_in_progress= 2
cache entries 2
-region_chg(5,6)
adds_in_progress= 3
cache entries 3At this point, no region descriptors are in the map because only
region_chghas been called.-
region_chg(0,6)
adds_in_progress= 4
cache entries 4Is that correct so far?
Then the following sequence happens,
-
region_add(1,2)
adds_in_progress= 3
cache entries 3
-region_add(3,4)
adds_in_progress= 2
cache entries 2
-region_add(5,6)
adds_in_progress= 1
cache entries 1list of region descriptors is:
[1->2] [3->4] [5->6]
Quoted 7 lines
-
region_add(0,6)
This is going to require 3 cache entries but only one is in the cache. I think we are going to BUG inget_file_region_entry_from_cache()the second time it is called fromadd_reservation_in_range().I stopped looking at the code here as things will need to change if this is a real issue.
You're right. I had been assuming that some higher level sync causes to ban a sequence of region_chg calls without a region_add or region_abort to resolve each region_chg call, but seems that is not the case. I'll fix and upload another version of the patch.
Quoted 2 lines
--
Mike Kravetz
PATCH v5 5/7 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.
--- mm/hugetlb.c | 126 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 105 insertions(+), 21 deletions(-)
mm/hugetlb.c +105 -21
@@ -242,6 +242,15 @@ 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; +#endif };/* Helper that removes a struct file_region from the resv_map cache and returns
@@ -250,12 +259,30 @@ struct file_region {static struct file_region *get_file_region_entry_from_cache (struct resv_map *resv,long from,long to); +/* 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); + }else { + nrg->reservation_counter =NULL ; + nrg->pages_per_hpage =0 ; + } +#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. */ static long add_reservation_in_range (struct resv_map *resv,long f,long t, -bool count_only) +struct hugetlb_cgroup *h_cg, +struct hstate *h,bool count_only) {long add =0 ;struct list_head *head = &resv->regions;
@@ -291,6 +318,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); } }
@@ -306,11 +335,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); } last_accounted_offset = t; } +VM_BUG_ON (add <0 );return add; }
@@ -327,7 +358,8 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,* Return the number of new huge pages added to the map. This * number is greater than or equal to zero. */ -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 regions_needed) {long add =0 ;
@@ -336,7 +368,7 @@ static long region_add(struct resv_map *resv, long f, long t,VM_BUG_ON (resv->region_cache_count < regions_needed); - add =add_reservation_in_range (resv, f, t, false); + add =add_reservation_in_range (resv, f, t, h_cg, h, false); resv->adds_in_progress -= regions_needed;spin_unlock (&resv->lock);
@@ -380,7 +412,7 @@ 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, true); + chg =add_reservation_in_range (resv, f, t,NULL ,NULL , true);/* * Check for sufficient descriptors in the cache to accommodate
@@ -430,6 +462,24 @@ 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); + } +#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
@@ -499,6 +549,9 @@ static long region_del(struct resv_map *resv, long f, long t)/* 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 ;
@@ -506,6 +559,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 ;
@@ -514,14 +569,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; }
@@ -1896,7 +1957,8 @@ __vma_reservation_common(struct hstate *h, struct vm_area_struct *vma,break ;case VMA_COMMIT_RESV :VM_BUG_ON (in_regions_needed ==-1 ); - ret =region_add (resv, idx, idx +1 , in_regions_needed); + ret =region_add (NULL ,NULL , resv, idx, idx +1 , + in_regions_needed);break ;case VMA_END_RESV :VM_BUG_ON (in_regions_needed ==-1 );
@@ -1906,7 +1968,8 @@ __vma_reservation_common(struct hstate *h, struct vm_area_struct *vma,case VMA_ADD_RESV :VM_BUG_ON (in_regions_needed ==-1 );if (vma->vm_flags &VM_MAYSHARE ) - ret =region_add (resv, idx, idx +1 , in_regions_needed); + ret =region_add (NULL ,NULL , resv, idx, idx +1 , + in_regions_needed);else {region_abort (resv, idx, idx +1 , in_regions_needed); ret =region_del (resv, idx, idx +1 );
@@ -4538,7 +4601,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 */
@@ -4579,13 +4642,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
@@ -4605,6 +4661,14 @@ 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; + } +/* * There must be enough pages in the subpool for the mapping. If * the subpool has a minimum size, there may be some global
@@ -4613,7 +4677,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; }/*
@@ -4622,9 +4686,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; }/*
@@ -4639,7 +4701,8 @@ 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, regions_needed); +long add = +region_add (h, h_cg, resv_map, from, to, regions_needed);if (unlikely (chg > add)) {/*
@@ -4651,12 +4714,33 @@ 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); +#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 )/* Don't call region_abort if region_chg failed */
--
2.23.0.351.gc4317032e6-goog
PATCH v5 6/7 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.
--- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + .../selftests/vm/charge_reserved_hugetlb.sh | 461 ++++++++++++++++++ .../selftests/vm/write_hugetlb_memory.sh | 22 + .../testing/selftests/vm/write_to_hugetlbfs.c | 250 ++++++++++ 5 files changed, 735 insertions(+) create mode 100755 tools/testing/selftests/vm/charge_reserved_hugetlb.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
@@ -18,6 +18,7 @@ TEST_GEN_FILES += transhuge-stress TEST_GEN_FILES += userfaultfd TEST_GEN_FILES += va_128TBswitch TEST_GEN_FILES += virtual_address_range +TEST_GEN_FILES += write_to_hugetlbfs TEST_PROGS := run_vmtests
tools/testing/selftests/vm/charge_reserved_hugetlb.sh new file +461 -0
461 lines not on this page · raw message
tools/testing/selftests/vm/write_hugetlb_memory.sh new file +22 -0
@@ -0,0 +1,22 @@ +#!/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 + +echo "Putting task in cgroup '$cgroup'" +echo $$ > /dev/cgroup/memory/"$cgroup"/tasks + +echo "Method is $method" + +set +e +./write_to_hugetlbfs -p "$path" -s "$size" "$write" "$populate" -m "$method" \ + "$private" "$want_sleep"
tools/testing/selftests/vm/write_to_hugetlbfs.c new file +250 -0
250 lines not on this page · raw message
--
2.23.0.351.gc4317032e6-goog
PATCH v5 7/7 hugetlb_cgroup: Add hugetlb_cgroup reservation docs
In reply to Mina AlmasryAdd docs for how to use hugetlb_cgroup reservations, and their behavior.
--- .../admin-guide/cgroup-v1/hugetlb.rst | 85 ++++++++++++++++--- 1 file changed, 74 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v1/hugetlb.rst +74 -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 recorded + hugetlb.<hugepagesize>.reservation_usage_in_bytes # show current reservations 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,77 @@ 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. Reservation limits +are superior to Page fault limits (see section 2), since Reservation limits are +enforced at reservation time, and never causes the application to get SIGBUS +signal. Instead, if the application is violating its limits, then it gets an +error on reservation time, i.e. the mmap or shmget return an error. + + +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 + +a. Charging and uncharging: + +For shared hugetlb memory, both hugetlb reservation and usage (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. + +b. Interaction between reservation limit and fault limit. + +Generally, it's not recommended to set both of the reservation limit and fault +limit in a cgroup. For private memory, the fault usage cannot exceed the +reservation usage, so if you set both, one of those limits will be useless. +Still, there is no kernel enforcement on setting both at the same time. + +For shared memory, a cgroup's fault usage may be greater than its reservation +usage, so some care needs to be taken. Consider this example: + +- Task A reserves 4 pages in a shared hugetlbfs file. Cgroup A will get + 4 reservations charged to it and no faults charged to it. +- Task B reserves and faults the same 4 pages as Task A. Cgroup B will get no + reservation charge, but will get charged 4 faulted pages. If Cgroup B's limit + is less than 4, then Task B will get a SIGBUS. + +For the above scenario, it's not recommended for the userspace to set both +reservation limits and fault limits, but it is still allowed to in case it sees +some use for it, such as safe gradual transitioning from one to the other.
--
2.23.0.351.gc4317032e6-goog
Quoted 3 lines · Mina Almasry
Patch series implements
hugetlb_cgroupreservation usage and limits, which track hugetlb reservations rather than hugetlb memory faulted in. Details of the approach is 1/7.
Thanks for your continued efforts Mina.
One thing that has bothered me with this approach from the beginning is that hugetlb reservations are related to, but somewhat distinct from hugetlb allocations. The original (existing) huegtlb cgroup implementation does not take reservations into account. This is an issue you are trying to address by adding a cgroup support for hugetlb reservations. However, this new reservation cgroup ignores hugetlb allocations at fault time.
I 'think' the whole purpose of any hugetlb cgroup is to manage the allocation of hugetlb pages. Both the existing cgroup code and the reservation approach have what I think are some serious flaws. Consider a system with 100 hugetlb pages available. A sysadmin, has two groups A and B and wants to limit hugetlb usage to 50 pages each.
With the existing implementation, a task in group A could create a mmap of 100 pages in size and reserve all 100 pages. Since the pages are 'reserved', nobody in group B can allocate ANY huge pages. This is true even though no pages have been allocated in A (or B).
With the reservation implementation, a task in group A could use MAP_NORESERVE and allocate all 100 pages without taking any reservations.
As mentioned in your documentation, it would be possible to use both the existing (allocation) and new reservation cgroups together. Perhaps if both are setup for the 50/50 split things would work a little better.
However, instead of creating a new reservation crgoup how about adding reservation support to the existing allocation cgroup support. One could even argue that a reservation is an allocation as it sets aside huge pages that can only be used for a specific purpose. Here is something that may work.
Starting with the existing allocation cgroup.
- When hugetlb pages are reserved, the cgroup of the task making the reservations is charged. Tracking for the charged cgroup is done in the reservation map in the same way proposed by this patch set.
- At page fault time,
- If a reservation already exists for that specific area do not charge the faulting task. No tracking in page, just the reservation map.
- If no reservation exists, charge the group of the faulting task. Tracking of this information is in the page itself as implemented today.
- When the hugetlb object is removed, compare the reservation map with any allocated pages. If cgroup tracking information exists in page, uncharge that group. Otherwise, unharge the group (if any) in the reservation map.
One of the advantages of a separate reservation cgroup is that the existing code is unmodified. Combining the two provides a more complete/accurate solution IMO. But, it has the potential to break existing users.
I really would like to get feedback from anyone that knows how the existing hugetlb cgroup controller may be used today. Comments from Aneesh would be very welcome to know if reservations were considered in development of the existing code.
Signature · 1 line
Mike Kravetz
Quoted 8 lines · Mike Kravetz
Quoted 3 lines · Mina Almasry
Patch series implements
hugetlb_cgroupreservation usage and limits, which track hugetlb reservations rather than hugetlb memory faulted in. Details of the approach is 1/7.Thanks for your continued efforts Mina.
And thanks for your reviews so far.
Quoted 48 lines
One thing that has bothered me with this approach from the beginning is that hugetlb reservations are related to, but somewhat distinct from hugetlb allocations. The original (existing) huegtlb cgroup implementation does not take reservations into account. This is an issue you are trying to address by adding a cgroup support for hugetlb reservations. However, this new reservation cgroup ignores hugetlb allocations at fault time.
I 'think' the whole purpose of any hugetlb cgroup is to manage the allocation of hugetlb pages. Both the existing cgroup code and the reservation approach have what I think are some serious flaws. Consider a system with 100 hugetlb pages available. A sysadmin, has two groups A and B and wants to limit hugetlb usage to 50 pages each.
With the existing implementation, a task in group A could create a mmap of 100 pages in size and reserve all 100 pages. Since the pages are 'reserved', nobody in group B can allocate ANY huge pages. This is true even though no pages have been allocated in A (or B).
With the reservation implementation, a task in group A could use
MAP_NORESERVEand allocate all 100 pages without taking any reservations.As mentioned in your documentation, it would be possible to use both the existing (allocation) and new reservation cgroups together. Perhaps if both are setup for the 50/50 split things would work a little better.
However, instead of creating a new reservation crgoup how about adding reservation support to the existing allocation cgroup support. One could even argue that a reservation is an allocation as it sets aside huge pages that can only be used for a specific purpose. Here is something that may work.
Starting with the existing allocation cgroup.
- When hugetlb pages are reserved, the cgroup of the task making the reservations is charged. Tracking for the charged cgroup is done in the reservation map in the same way proposed by this patch set.
- At page fault time,
- If a reservation already exists for that specific area do not charge the faulting task. No tracking in page, just the reservation map.
- If no reservation exists, charge the group of the faulting task. Tracking of this information is in the page itself as implemented today.
- When the hugetlb object is removed, compare the reservation map with any allocated pages. If cgroup tracking information exists in page, uncharge that group. Otherwise, unharge the group (if any) in the reservation map.One of the advantages of a separate reservation cgroup is that the existing code is unmodified. Combining the two provides a more complete/accurate solution IMO. But, it has the potential to break existing users.
Quoted 5 lines
I really would like to get feedback from anyone that knows how the existing hugetlb cgroup controller may be used today. Comments from Aneesh would be very welcome to know if reservations were considered in development of the existing code.
--
FWIW, I'm aware of the interaction with NORESERVE and my thoughts are:
AFAICT, the 2 counter approach we have here is strictly superior to the 1 upgraded counter approach. Consider these points:
- From what I can tell so far, everything you can do with the 1 counter approach, you can do with the two counter approach by setting both limit_in_bytes and reservation_limit_in_bytes to the limit value. That will limit both reservations and at fault allocations.
- The 2 counter approach preserves existing usage of hugetlb cgroups, so no need to muck around with reverting the feature some time from now because of broken users. No existing users of hugetlb cgroups need to worry about the effect of this on their usage.
- Users that use hugetlb memory strictly through reservations can use only reservation_limit_in_bytes and enjoy cgroup limits that never SIGBUS the application. This is our usage for example.
- The 2 counter approach provides more info to the sysadmin. The sysadmin knows exactly how much reserved bytes there are via reservation_usage_in_bytes, and how much actually in use bytes there are via usage_in_bytes. They can even detect NORESERVE usage if usage_in_bytes > reservation_usage_in_bytes. failcnt shows failed reservations *and* failed allocations at fault, etc. All around better debuggability when things go wrong. I think this is particularly troubling for the 1 upgraded counter approach. That counter's usage_in_bytes doesn't tell you if the usage came from reservations or allocations at fault time.
- Honestly, I think the 2 counter approach is easier to document and understand by the userspace? 1 counter that vaguely tracks both the reservations and usage and decides whether or not to charge at fault time seems hard to understand what really happened after something goes wrong. 1 counter that tracks reservations and 1 counter that tracks actual usage seem much simpler to digest, and provide better visibility to what the cgroup is doing as I mentioned above.
I think it may be better if I keep the 2 counter approach but thoroughly document the interaction between the existing counters and NORESERVE. What do you think?
FWIW, it may be prudent to consider deprecating MAP_NORESERVE, if that's an option. I'm not sure what that benefit that provides applications, and on the other hand it makes it hard for the kernel to guarantee the hugetlb memory is available to the application that requested it, and makes it harder for the cgroups to police hugetlb usage without SIGBUSing something. But that may be a discussion for another proposal.
Quoted 1 line
Mike Kravetz
Quoted 104 lines · Mina Almasry
Quoted 8 lines · Mike Kravetz
Quoted 3 lines · Mina Almasry
Patch series implements
hugetlb_cgroupreservation usage and limits, which track hugetlb reservations rather than hugetlb memory faulted in. Details of the approach is 1/7.Thanks for your continued efforts Mina.
And thanks for your reviews so far.
Quoted 48 lines
One thing that has bothered me with this approach from the beginning is that hugetlb reservations are related to, but somewhat distinct from hugetlb allocations. The original (existing) huegtlb cgroup implementation does not take reservations into account. This is an issue you are trying to address by adding a cgroup support for hugetlb reservations. However, this new reservation cgroup ignores hugetlb allocations at fault time.
I 'think' the whole purpose of any hugetlb cgroup is to manage the allocation of hugetlb pages. Both the existing cgroup code and the reservation approach have what I think are some serious flaws. Consider a system with 100 hugetlb pages available. A sysadmin, has two groups A and B and wants to limit hugetlb usage to 50 pages each.
With the existing implementation, a task in group A could create a mmap of 100 pages in size and reserve all 100 pages. Since the pages are 'reserved', nobody in group B can allocate ANY huge pages. This is true even though no pages have been allocated in A (or B).
With the reservation implementation, a task in group A could use
MAP_NORESERVEand allocate all 100 pages without taking any reservations.As mentioned in your documentation, it would be possible to use both the existing (allocation) and new reservation cgroups together. Perhaps if both are setup for the 50/50 split things would work a little better.
However, instead of creating a new reservation crgoup how about adding reservation support to the existing allocation cgroup support. One could even argue that a reservation is an allocation as it sets aside huge pages that can only be used for a specific purpose. Here is something that may work.
Starting with the existing allocation cgroup.
- When hugetlb pages are reserved, the cgroup of the task making the reservations is charged. Tracking for the charged cgroup is done in the reservation map in the same way proposed by this patch set.
- At page fault time,
- If a reservation already exists for that specific area do not charge the faulting task. No tracking in page, just the reservation map.
- If no reservation exists, charge the group of the faulting task. Tracking of this information is in the page itself as implemented today.
- When the hugetlb object is removed, compare the reservation map with any allocated pages. If cgroup tracking information exists in page, uncharge that group. Otherwise, unharge the group (if any) in the reservation map.One of the advantages of a separate reservation cgroup is that the existing code is unmodified. Combining the two provides a more complete/accurate solution IMO. But, it has the potential to break existing users.
Quoted 5 lines
I really would like to get feedback from anyone that knows how the existing hugetlb cgroup controller may be used today. Comments from Aneesh would be very welcome to know if reservations were considered in development of the existing code.
--FWIW, I'm aware of the interaction with NORESERVE and my thoughts are:
AFAICT, the 2 counter approach we have here is strictly superior to the 1 upgraded counter approach. Consider these points:
- From what I can tell so far, everything you can do with the 1 counter approach, you can do with the two counter approach by setting both
limit_in_bytesandreservation_limit_in_bytesto the limit value. That will limit both reservations and at fault allocations.- The 2 counter approach preserves existing usage of hugetlb cgroups, so no need to muck around with reverting the feature some time from now because of broken users. No existing users of hugetlb cgroups need to worry about the effect of this on their usage.
- Users that use hugetlb memory strictly through reservations can use only
reservation_limit_in_bytesand enjoy cgroup limits that never SIGBUS the application. This is our usage for example.- The 2 counter approach provides more info to the sysadmin. The sysadmin knows exactly how much reserved bytes there are via
reservation_usage_in_bytes, and how much actually in use bytes there are viausage_in_bytes. They can even detect NORESERVE usage ifusage_in_bytes>reservation_usage_in_bytes. failcnt shows failed reservations *and* failed allocations at fault, etc. All around better debuggability when things go wrong. I think this is particularly troubling for the 1 upgraded counter approach. That counter'susage_in_bytesdoesn't tell you if the usage came from reservations or allocations at fault time.- Honestly, I think the 2 counter approach is easier to document and understand by the userspace? 1 counter that vaguely tracks both the reservations and usage and decides whether or not to charge at fault time seems hard to understand what really happened after something goes wrong. 1 counter that tracks reservations and 1 counter that tracks actual usage seem much simpler to digest, and provide better visibility to what the cgroup is doing as I mentioned above.
Quoted 3 lines
I think it may be better if I keep the 2 counter approach but thoroughly document the interaction between the existing counters and NORESERVE. What do you think?
I personally prefer the one counter approach only for the reason that it exposes less information about hugetlb reservations. I was not around for the introduction of hugetlb reservations, but I have fixed several issues having to do with reservations. IMO, reservations should be hidden from users as much as possible. Others may disagree.
I really hope that Aneesh will comment. He added the existing hugetlb cgroup code. I was not involved in that effort, but it looks like there might have been some thought given to reservations in early versions of that code. It would be interesting to get his perspective.
Changes included in patch 4 (disable region_add file_region coalescing) would be needed in a one counter approach as well, so I do plan to review those changes.
Signature · 1 line
Mike Kravetz
Quoted 116 lines · Mike Kravetz
Quoted 104 lines · Mina Almasry
Quoted 8 lines · Mike Kravetz
Quoted 3 lines · Mina Almasry
Patch series implements
hugetlb_cgroupreservation usage and limits, which track hugetlb reservations rather than hugetlb memory faulted in. Details of the approach is 1/7.Thanks for your continued efforts Mina.
And thanks for your reviews so far.
Quoted 48 lines
One thing that has bothered me with this approach from the beginning is that hugetlb reservations are related to, but somewhat distinct from hugetlb allocations. The original (existing) huegtlb cgroup implementation does not take reservations into account. This is an issue you are trying to address by adding a cgroup support for hugetlb reservations. However, this new reservation cgroup ignores hugetlb allocations at fault time.
I 'think' the whole purpose of any hugetlb cgroup is to manage the allocation of hugetlb pages. Both the existing cgroup code and the reservation approach have what I think are some serious flaws. Consider a system with 100 hugetlb pages available. A sysadmin, has two groups A and B and wants to limit hugetlb usage to 50 pages each.
With the existing implementation, a task in group A could create a mmap of 100 pages in size and reserve all 100 pages. Since the pages are 'reserved', nobody in group B can allocate ANY huge pages. This is true even though no pages have been allocated in A (or B).
With the reservation implementation, a task in group A could use
MAP_NORESERVEand allocate all 100 pages without taking any reservations.As mentioned in your documentation, it would be possible to use both the existing (allocation) and new reservation cgroups together. Perhaps if both are setup for the 50/50 split things would work a little better.
However, instead of creating a new reservation crgoup how about adding reservation support to the existing allocation cgroup support. One could even argue that a reservation is an allocation as it sets aside huge pages that can only be used for a specific purpose. Here is something that may work.
Starting with the existing allocation cgroup.
- When hugetlb pages are reserved, the cgroup of the task making the reservations is charged. Tracking for the charged cgroup is done in the reservation map in the same way proposed by this patch set.
- At page fault time,
- If a reservation already exists for that specific area do not charge the faulting task. No tracking in page, just the reservation map.
- If no reservation exists, charge the group of the faulting task. Tracking of this information is in the page itself as implemented today.
- When the hugetlb object is removed, compare the reservation map with any allocated pages. If cgroup tracking information exists in page, uncharge that group. Otherwise, unharge the group (if any) in the reservation map.One of the advantages of a separate reservation cgroup is that the existing code is unmodified. Combining the two provides a more complete/accurate solution IMO. But, it has the potential to break existing users.
Quoted 5 lines
I really would like to get feedback from anyone that knows how the existing hugetlb cgroup controller may be used today. Comments from Aneesh would be very welcome to know if reservations were considered in development of the existing code.
--FWIW, I'm aware of the interaction with NORESERVE and my thoughts are:
AFAICT, the 2 counter approach we have here is strictly superior to the 1 upgraded counter approach. Consider these points:
- From what I can tell so far, everything you can do with the 1 counter approach, you can do with the two counter approach by setting both
limit_in_bytesandreservation_limit_in_bytesto the limit value. That will limit both reservations and at fault allocations.- The 2 counter approach preserves existing usage of hugetlb cgroups, so no need to muck around with reverting the feature some time from now because of broken users. No existing users of hugetlb cgroups need to worry about the effect of this on their usage.
- Users that use hugetlb memory strictly through reservations can use only
reservation_limit_in_bytesand enjoy cgroup limits that never SIGBUS the application. This is our usage for example.- The 2 counter approach provides more info to the sysadmin. The sysadmin knows exactly how much reserved bytes there are via
reservation_usage_in_bytes, and how much actually in use bytes there are viausage_in_bytes. They can even detect NORESERVE usage ifusage_in_bytes>reservation_usage_in_bytes. failcnt shows failed reservations *and* failed allocations at fault, etc. All around better debuggability when things go wrong. I think this is particularly troubling for the 1 upgraded counter approach. That counter'susage_in_bytesdoesn't tell you if the usage came from reservations or allocations at fault time.- Honestly, I think the 2 counter approach is easier to document and understand by the userspace? 1 counter that vaguely tracks both the reservations and usage and decides whether or not to charge at fault time seems hard to understand what really happened after something goes wrong. 1 counter that tracks reservations and 1 counter that tracks actual usage seem much simpler to digest, and provide better visibility to what the cgroup is doing as I mentioned above.
Quoted 3 lines
I think it may be better if I keep the 2 counter approach but thoroughly document the interaction between the existing counters and NORESERVE. What do you think?
I personally prefer the one counter approach only for the reason that it exposes less information about hugetlb reservations. I was not around for the introduction of hugetlb reservations, but I have fixed several issues having to do with reservations. IMO, reservations should be hidden from users as much as possible. Others may disagree.
Quoted 8 lines
I really hope that Aneesh will comment. He added the existing hugetlb cgroup code. I was not involved in that effort, but it looks like there might have been some thought given to reservations in early versions of that code. It would be interesting to get his perspective.
Changes included in patch 4 (disable
region_addfile_regioncoalescing) would be needed in a one counter approach as well, so I do plan to review those changes.
OK, FWIW, the 1 counter approach should be sufficient for us, so I'm not really opposed. David, maybe chime in if you see a problem here? From the perspective of hiding reservations from the user as much as possible, it is an improvement.
I'm only wary about changing the behavior of the current and having that regress applications. I'm hoping you and Aneesh can shed light on this.
Quoted 2 lines
--
Mike Kravetz
Quoted 15 lines · Mina Almasry
Quoted 6 lines
I personally prefer the one counter approach only for the reason that it exposes less information about hugetlb reservations. I was not around for the introduction of hugetlb reservations, but I have fixed several issues having to do with reservations. IMO, reservations should be hidden from users as much as possible. Others may disagree.
Quoted 8 lines
I really hope that Aneesh will comment. He added the existing hugetlb cgroup code. I was not involved in that effort, but it looks like there might have been some thought given to reservations in early versions of that code. It would be interesting to get his perspective.
Changes included in patch 4 (disable
region_addfile_regioncoalescing) would be needed in a one counter approach as well, so I do plan to review those changes.
Quoted 9 lines
OK, FWIW, the 1 counter approach should be sufficient for us, so I'm not really opposed. David, maybe chime in if you see a problem here? From the perspective of hiding reservations from the user as much as possible, it is an improvement.
I'm only wary about changing the behavior of the current and having that regress applications. I'm hoping you and Aneesh can shed light on this.
I think neither Aneesh nor myself are going to be able to provide a complete answer on the use of hugetlb cgroup today, anybody could be using it without our knowledge and that opens up the possibility that combining the limits would adversely affect a real system configuration.
If that is a possibility, I think we need to do some due diligence and try to deprecate allocation limits if possible. One of the benefits to separate limits is that we can make reasonable steps to deprecating the actual allocation limits, if possible: we could add warnings about the deprecation of allocation limits and see if anybody complains.
That could take the form of two separate limits or a tunable in the root hugetlb cgroup that defines whether the limits are for allocation or reservation.
Combining them in the first pass seems to be very risky and could cause pain for users that will not detect this during an rc cycle and will report the issue only when their distro gets it. Then we are left with no alternative other than stable backports and the separation of the limits anyway.
Quoted 27 lines · David Rientjes
Quoted 15 lines · Mina Almasry
Quoted 6 lines
I personally prefer the one counter approach only for the reason that it exposes less information about hugetlb reservations. I was not around for the introduction of hugetlb reservations, but I have fixed several issues having to do with reservations. IMO, reservations should be hidden from users as much as possible. Others may disagree.
Quoted 8 lines
I really hope that Aneesh will comment. He added the existing hugetlb cgroup code. I was not involved in that effort, but it looks like there might have been some thought given to reservations in early versions of that code. It would be interesting to get his perspective.
Changes included in patch 4 (disable
region_addfile_regioncoalescing) would be needed in a one counter approach as well, so I do plan to review those changes.Quoted 9 lines
OK, FWIW, the 1 counter approach should be sufficient for us, so I'm not really opposed. David, maybe chime in if you see a problem here? From the perspective of hiding reservations from the user as much as possible, it is an improvement.
I'm only wary about changing the behavior of the current and having that regress applications. I'm hoping you and Aneesh can shed light on this.
Quoted 4 lines
I think neither Aneesh nor myself are going to be able to provide a complete answer on the use of hugetlb cgroup today, anybody could be using it without our knowledge and that opens up the possibility that combining the limits would adversely affect a real system configuration.
I agree that nobody can provide complete information on hugetlb cgroup usage today. My interest was in anything Aneesh could remember about development of the current cgroup code. It 'appears' that the idea of including reservations or mmap ranges was considered or at least discussed. But, those discussions happened more than 7 years old and my searches are not providing a complete picture. My hope was that Aneesh may remember those discussions.
Quoted 10 lines
If that is a possibility, I think we need to do some due diligence and try to deprecate allocation limits if possible. One of the benefits to separate limits is that we can make reasonable steps to deprecating the actual allocation limits, if possible: we could add warnings about the deprecation of allocation limits and see if anybody complains.
That could take the form of two separate limits or a tunable in the root hugetlb cgroup that defines whether the limits are for allocation or reservation.
Quoted 5 lines
Combining them in the first pass seems to be very risky and could cause pain for users that will not detect this during an rc cycle and will report the issue only when their distro gets it. Then we are left with no alternative other than stable backports and the separation of the limits anyway.
I agree that changing behavior of the existing controller is too risky. Such a change is likely to break someone. The more I think about it, the best way forward will be to retain the existing controller and create a new controller that satisfies the new use cases. The question remains as to what that new controller will be. Does it control reservations only? Is it a combination of reservations and allocations? If a combined controller will work for new use cases, that would be my preference. Of course, I have not prototyped such a controller so there may be issues when we get into the details. For a reservation only or combined controller, the region_* changes proposed by Mina would be used.
Signature · 1 line
Mike Kravetz
Quoted 51 lines · Mike Kravetz
Quoted 27 lines · David Rientjes
Quoted 15 lines · Mina Almasry
Quoted 6 lines
I personally prefer the one counter approach only for the reason that it exposes less information about hugetlb reservations. I was not around for the introduction of hugetlb reservations, but I have fixed several issues having to do with reservations. IMO, reservations should be hidden from users as much as possible. Others may disagree.
Quoted 8 lines
I really hope that Aneesh will comment. He added the existing hugetlb cgroup code. I was not involved in that effort, but it looks like there might have been some thought given to reservations in early versions of that code. It would be interesting to get his perspective.
Changes included in patch 4 (disable
region_addfile_regioncoalescing) would be needed in a one counter approach as well, so I do plan to review those changes.Quoted 9 lines
OK, FWIW, the 1 counter approach should be sufficient for us, so I'm not really opposed. David, maybe chime in if you see a problem here? From the perspective of hiding reservations from the user as much as possible, it is an improvement.
I'm only wary about changing the behavior of the current and having that regress applications. I'm hoping you and Aneesh can shed light on this.
Quoted 4 lines
I think neither Aneesh nor myself are going to be able to provide a complete answer on the use of hugetlb cgroup today, anybody could be using it without our knowledge and that opens up the possibility that combining the limits would adversely affect a real system configuration.
I agree that nobody can provide complete information on hugetlb cgroup usage today. My interest was in anything Aneesh could remember about development of the current cgroup code. It 'appears' that the idea of including reservations or mmap ranges was considered or at least discussed. But, those discussions happened more than 7 years old and my searches are not providing a complete picture. My hope was that Aneesh may remember those discussions.
Quoted 10 lines
If that is a possibility, I think we need to do some due diligence and try to deprecate allocation limits if possible. One of the benefits to separate limits is that we can make reasonable steps to deprecating the actual allocation limits, if possible: we could add warnings about the deprecation of allocation limits and see if anybody complains.
That could take the form of two separate limits or a tunable in the root hugetlb cgroup that defines whether the limits are for allocation or reservation.
Quoted 8 lines
Quoted 5 lines
Combining them in the first pass seems to be very risky and could cause pain for users that will not detect this during an rc cycle and will report the issue only when their distro gets it. Then we are left with no alternative other than stable backports and the separation of the limits anyway.
I agree that changing behavior of the existing controller is too risky. Such a change is likely to break someone.
I'm glad we're converging on keeping the existing behavior unchanged.
Quoted 3 lines
The more I think about it, the
best way forward will be to retain the existing controller and create a new controller that satisfies the new use cases.
My guess is that a new controller needs to support cgroups-v2, which is fine. But can a new controller also support v1? Or is there a requirement that new controllers support *only* v2? I need whatever solution here to work on v1. Added Tejun to hopefully comment on this.
Quoted 7 lines
The question remains as
to what that new controller will be. Does it control reservations only? Is it a combination of reservations and allocations? If a combined controller will work for new use cases, that would be my preference. Of course, I have not prototyped such a controller so there may be issues when we get into the details. For a reservation only or combined controller, the region_* changes proposed by Mina would be used.
Provided we keep the existing controller untouched, should the new controller track:
1. only reservations, or
2. both reservations and allocations for which no reservations exist (such as the MAP_NORESERVE case)?
I like the 'both' approach. Seems to me a counter like that would work automatically regardless of whether the application is allocating hugetlb memory with NORESERVE or not. NORESERVE allocations cannot cut into reserved hugetlb pages, correct? If so, then applications that allocate with NORESERVE will get sigbused when they hit their limit, and applications that allocate without NORESERVE may get an error at mmap time but will always be within their limits while they access the mmap'd memory, correct? So the 'both' counter seems like a one size fits all.
I think the only sticking point left is whether an added controller can support both cgroup-v2 and cgroup-v1. If I could get confirmation on that I'll provide a patchset.
Quoted 2 lines
--
Mike Kravetz
Quoted 11 lines · Mina Almasry
Provided we keep the existing controller untouched, should the new controller track:
1. only reservations, or
2. both reservations and allocations for which no reservations exist (such as theMAP_NORESERVEcase)?I like the 'both' approach. Seems to me a counter like that would work automatically regardless of whether the application is allocating hugetlb memory with NORESERVE or not. NORESERVE allocations cannot cut into reserved hugetlb pages, correct?
Correct. One other easy way to allocate huge pages without reserves (that I know is used today) is via the fallocate system call.
Quoted 5 lines
If so, then applications that
allocate with NORESERVE will get sigbused when they hit their limit, and applications that allocate without NORESERVE may get an error at mmap time but will always be within their limits while they access the mmap'd memory, correct?
Correct. At page allocation time we can easily check to see if a reservation exists and not charge. For any specific page within a hugetlbfs file, a charge would happen at mmap time or allocation time.
One exception (that I can think of) to this mmap(RESERVE) will not cause a SIGBUS rule is in the case of hole punch. If someone punches a hole in a file, not only do they remove pages associated with the file but the reservation information as well. Therefore, a subsequent fault will be the same as an allocation without reservation.
I 'think' the code to remove/truncate a file will work corrctly as it is today, but I need to think about this some more.
Quoted 6 lines
mmap'd memory, correct? So the 'both' counter seems like a one size fits all.
I think the only sticking point left is whether an added controller can support both cgroup-v2 and cgroup-v1. If I could get confirmation on that I'll provide a patchset.
Sorry, but I can not provide cgroup expertise.
Signature · 1 line
Mike Kravetz
Quoted 27 lines · Mike Kravetz
Quoted 11 lines · Mina Almasry
Provided we keep the existing controller untouched, should the new controller track:
1. only reservations, or
2. both reservations and allocations for which no reservations exist (such as theMAP_NORESERVEcase)?I like the 'both' approach. Seems to me a counter like that would work automatically regardless of whether the application is allocating hugetlb memory with NORESERVE or not. NORESERVE allocations cannot cut into reserved hugetlb pages, correct?
Correct. One other easy way to allocate huge pages without reserves (that I know is used today) is via the fallocate system call.
Quoted 5 lines
If so, then applications that
allocate with NORESERVE will get sigbused when they hit their limit, and applications that allocate without NORESERVE may get an error at mmap time but will always be within their limits while they access the mmap'd memory, correct?Correct. At page allocation time we can easily check to see if a reservation exists and not charge. For any specific page within a hugetlbfs file, a charge would happen at mmap time or allocation time.
Quoted 6 lines
One exception (that I can think of) to this mmap(RESERVE) will not cause a SIGBUS rule is in the case of hole punch. If someone punches a hole in a file, not only do they remove pages associated with the file but the reservation information as well. Therefore, a subsequent fault will be the same as an allocation without reservation.
I don't think it causes a sigbus. This is the scenario, right:
1. Make cgroup with limit X bytes.
2. Task in cgroup mmaps a file with X bytes, causing the cgroup to get charged
3. A hole of size Y is punched in the file, causing the cgroup to get uncharged Y bytes.
4. The task faults in memory from the hole, getting charged up to Y bytes again. But they will be still within their limits.
IIUC userspace only gets sigbus'd if the limit is lowered between steps 3 and 4, and it's ok if it gets sigbus'd there in my opinion.
Quoted 13 lines
I 'think' the code to remove/truncate a file will work corrctly as it is today, but I need to think about this some more.
Quoted 6 lines
mmap'd memory, correct? So the 'both' counter seems like a one size fits all.
I think the only sticking point left is whether an added controller can support both cgroup-v2 and cgroup-v1. If I could get confirmation on that I'll provide a patchset.
Sorry, but I can not provide cgroup expertise.
--
Mike Kravetz
Quoted 44 lines · Mina Almasry
Quoted 27 lines · Mike Kravetz
Quoted 11 lines · Mina Almasry
Provided we keep the existing controller untouched, should the new controller track:
1. only reservations, or
2. both reservations and allocations for which no reservations exist (such as theMAP_NORESERVEcase)?I like the 'both' approach. Seems to me a counter like that would work automatically regardless of whether the application is allocating hugetlb memory with NORESERVE or not. NORESERVE allocations cannot cut into reserved hugetlb pages, correct?
Correct. One other easy way to allocate huge pages without reserves (that I know is used today) is via the fallocate system call.
Quoted 5 lines
If so, then applications that
allocate with NORESERVE will get sigbused when they hit their limit, and applications that allocate without NORESERVE may get an error at mmap time but will always be within their limits while they access the mmap'd memory, correct?Correct. At page allocation time we can easily check to see if a reservation exists and not charge. For any specific page within a hugetlbfs file, a charge would happen at mmap time or allocation time.
Quoted 6 lines
One exception (that I can think of) to this mmap(RESERVE) will not cause a SIGBUS rule is in the case of hole punch. If someone punches a hole in a file, not only do they remove pages associated with the file but the reservation information as well. Therefore, a subsequent fault will be the same as an allocation without reservation.
I don't think it causes a sigbus. This is the scenario, right:
1. Make cgroup with limit X bytes.
2. Task in cgroup mmaps a file with X bytes, causing the cgroup to get charged
3. A hole of size Y is punched in the file, causing the cgroup to get uncharged Y bytes.
4. The task faults in memory from the hole, getting charged up to Y bytes again. But they will be still within their limits.
Quoted 2 lines
IIUC userspace only gets sigbus'd if the limit is lowered between steps 3 and 4, and it's ok if it gets sigbus'd there in my opinion.
You are correct. That was my mistake. I was still thinking of behavior for a reservation only cgroup model. It would behave as you describe above (no SIGBUS) for a combined reservation/allocate model.
Signature · 1 line
Mike Kravetz
Michal Koutný
maintainerHi.
Quoted 4 lines · Mina Almasry
My guess is that a new controller needs to support cgroups-v2, which is fine. But can a new controller also support v1? Or is there a requirement that new controllers support *only* v2? I need whatever solution here to work on v1.
Here is my view of important criteria:
1) stability, v1 APIs and semantics should not be changed,
2) futureproofness, v1 uses should be convertible to v2 uses,
3) maintainability, the less (similar) code the better.
And here is my braindump of some approaches:
A) new controller, v2 only
- 1) ok
- 2) may be ok
- 3) separate v1 and v2 implementations
- exclusion must be ensured on hybrid hierarchies
B) new controller, version oblivious (see e.g. pid)
- 1) sort of ok
- 2) partially ok
- 3) two v1 implementations
- exclusion must be ensured even on pure v1 hierarchies
C) extending the existing controller, w/out v2 counterpart
- 1) ok with workarounds (new option switching behavior)
- 2) not ok
- 3) likely OK
D) extending the existing controller, with v2 counterpart
- 1) ok with workarounds (new option switching behavior, see cpuset)
- 2) may be ok
- 3) likely OK
AFAIU, the current patchset is variation of C). Personally, I think something like D) could work, I'm not convinced about A) and B) based on the breakdown above. But it may induce some other ideas.
My two cents,
Michal
Quoted 39 lines · Mike Kravetz
Quoted 3 lines · Mina Almasry
Patch series implements
hugetlb_cgroupreservation usage and limits, which track hugetlb reservations rather than hugetlb memory faulted in. Details of the approach is 1/7.Thanks for your continued efforts Mina.
One thing that has bothered me with this approach from the beginning is that hugetlb reservations are related to, but somewhat distinct from hugetlb allocations. The original (existing) huegtlb cgroup implementation does not take reservations into account. This is an issue you are trying to address by adding a cgroup support for hugetlb reservations. However, this new reservation cgroup ignores hugetlb allocations at fault time.
I 'think' the whole purpose of any hugetlb cgroup is to manage the allocation of hugetlb pages. Both the existing cgroup code and the reservation approach have what I think are some serious flaws. Consider a system with 100 hugetlb pages available. A sysadmin, has two groups A and B and wants to limit hugetlb usage to 50 pages each.
With the existing implementation, a task in group A could create a mmap of 100 pages in size and reserve all 100 pages. Since the pages are 'reserved', nobody in group B can allocate ANY huge pages. This is true even though no pages have been allocated in A (or B).
With the reservation implementation, a task in group A could use
MAP_NORESERVEand allocate all 100 pages without taking any reservations.As mentioned in your documentation, it would be possible to use both the existing (allocation) and new reservation cgroups together. Perhaps if both are setup for the 50/50 split things would work a little better.
However, instead of creating a new reservation crgoup how about adding reservation support to the existing allocation cgroup support. One could even argue that a reservation is an allocation as it sets aside huge pages that can only be used for a specific purpose. Here is something that may work.
Quoted 13 lines
Starting with the existing allocation cgroup.
- When hugetlb pages are reserved, the cgroup of the task making the reservations is charged. Tracking for the charged cgroup is done in the reservation map in the same way proposed by this patch set.
- At page fault time,
- If a reservation already exists for that specific area do not charge the faulting task. No tracking in page, just the reservation map.
- If no reservation exists, charge the group of the faulting task. Tracking of this information is in the page itself as implemented today.
- When the hugetlb object is removed, compare the reservation map with any allocated pages. If cgroup tracking information exists in page, uncharge that group. Otherwise, unharge the group (if any) in the reservation map.
Sorry for the late response here. I've been prototyping the suggestions from this conversation:
1. Supporting cgroup-v2 on the current controller seems trivial. Basically just specifying the dfl files seems to do it, and my tests on top of cgroup-v2 don't see any problems so far at least. In light of this I'm not sure it's best to create a new controller per say. Seems like it would duplicate a lot of code with the current controller, so I've tentatively just stuck to the plan in my current patchset, a new counter on the existing controller.
2. I've been working on transitioning the new counter to the behavior Mike specified in the email I'm responding to. So far I have a flow that works for shared mappings but not private mappings:
- On reservation, charge the new counter and store the info in the resv_map. The counter gets uncharged when the resv_map entry gets removed (works fine).
- On alloc_huge_page(), check if there is a reservation for the page being allocated. If not, charge the new counter and store the information in resv_map. The counter still gets uncharged when the resv_map entry gets removed.
The above works for all shared mappings and reserved private mappings, but I' having trouble supporting private NORESERVE mappings. Charging can work the same as for shared mappings: charge the new counter on reservation and on allocations that do not have a reservation. But the question still comes up: where to store the counter to uncharge this page? I thought of a couple of things that don't seem to work:
1. I thought of putting the counter in resv_map->reservation_counter, so that it gets uncharged on vm_op_close. But, private NORESERVE mappings don't even have a resv_map allocated for them.
2. I thought of detecting on free_huge_page that the page being freed belonged to a private NORESERVE mapping, and uncharging the hugetlb_cgroup in the page itself, but free_huge_page only gets a struct page* and I can't seem to find a way to detect that that the page comes from a private NORESERVE mapping from only the struct page*.
Mike, note your suggestion above to check if the page hugetlb_cgroup is null doesn't work if we want to keep the current counter working the same: the page will always have a hugetlb_cgroup that points that contains the old counter. Any ideas how to apply this new counter behavior to a private NORESERVE mappings? Is there maybe a flag I can set on the pages at allocation time that I can read on free time to know whether to uncharge the hugetlb_cgroup or not?
Quoted 96 lines · Mina Almasry
Quoted 39 lines · Mike Kravetz
Quoted 3 lines · Mina Almasry
Patch series implements
hugetlb_cgroupreservation usage and limits, which track hugetlb reservations rather than hugetlb memory faulted in. Details of the approach is 1/7.Thanks for your continued efforts Mina.
One thing that has bothered me with this approach from the beginning is that hugetlb reservations are related to, but somewhat distinct from hugetlb allocations. The original (existing) huegtlb cgroup implementation does not take reservations into account. This is an issue you are trying to address by adding a cgroup support for hugetlb reservations. However, this new reservation cgroup ignores hugetlb allocations at fault time.
I 'think' the whole purpose of any hugetlb cgroup is to manage the allocation of hugetlb pages. Both the existing cgroup code and the reservation approach have what I think are some serious flaws. Consider a system with 100 hugetlb pages available. A sysadmin, has two groups A and B and wants to limit hugetlb usage to 50 pages each.
With the existing implementation, a task in group A could create a mmap of 100 pages in size and reserve all 100 pages. Since the pages are 'reserved', nobody in group B can allocate ANY huge pages. This is true even though no pages have been allocated in A (or B).
With the reservation implementation, a task in group A could use
MAP_NORESERVEand allocate all 100 pages without taking any reservations.As mentioned in your documentation, it would be possible to use both the existing (allocation) and new reservation cgroups together. Perhaps if both are setup for the 50/50 split things would work a little better.
However, instead of creating a new reservation crgoup how about adding reservation support to the existing allocation cgroup support. One could even argue that a reservation is an allocation as it sets aside huge pages that can only be used for a specific purpose. Here is something that may work.
Quoted 13 lines
Starting with the existing allocation cgroup.
- When hugetlb pages are reserved, the cgroup of the task making the reservations is charged. Tracking for the charged cgroup is done in the reservation map in the same way proposed by this patch set.
- At page fault time,
- If a reservation already exists for that specific area do not charge the faulting task. No tracking in page, just the reservation map.
- If no reservation exists, charge the group of the faulting task. Tracking of this information is in the page itself as implemented today.
- When the hugetlb object is removed, compare the reservation map with any allocated pages. If cgroup tracking information exists in page, uncharge that group. Otherwise, unharge the group (if any) in the reservation map.Sorry for the late response here. I've been prototyping the suggestions from this conversation:
1. Supporting cgroup-v2 on the current controller seems trivial. Basically just specifying the dfl files seems to do it, and my tests on top of cgroup-v2 don't see any problems so far at least. In light of this I'm not sure it's best to create a new controller per say. Seems like it would duplicate a lot of code with the current controller, so I've tentatively just stuck to the plan in my current patchset, a new counter on the existing controller.
2. I've been working on transitioning the new counter to the behavior Mike specified in the email I'm responding to. So far I have a flow that works for shared mappings but not private mappings:
- On reservation, charge the new counter and store the info in the
resv_map. The counter gets uncharged when theresv_mapentry gets removed (works fine).
- Onalloc_huge_page(), check if there is a reservation for the page being allocated. If not, charge the new counter and store the information inresv_map. The counter still gets uncharged when theresv_mapentry gets removed.The above works for all shared mappings and reserved private mappings, but I' having trouble supporting private NORESERVE mappings. Charging can work the same as for shared mappings: charge the new counter on reservation and on allocations that do not have a reservation. But the question still comes up: where to store the counter to uncharge this page? I thought of a couple of things that don't seem to work:
1. I thought of putting the counter in
resv_map->reservation_counter, so that it gets uncharged onvm_op_close. But, private NORESERVE mappings don't even have aresv_mapallocated for them.2. I thought of detecting on
free_huge_pagethat the page being freed belonged to a private NORESERVE mapping, and uncharging thehugetlb_cgroupin the page itself, butfree_huge_pageonly gets a struct page* and I can't seem to find a way to detect that that the page comes from a private NORESERVE mapping from only the struct page*.
Quoted 7 lines
Mike, note your suggestion above to check if the page
hugetlb_cgroupis null doesn't work if we want to keep the current counter working the same: the page will always have ahugetlb_cgroupthat points that contains the old counter. Any ideas how to apply this new counter behavior to a private NORESERVE mappings? Is there maybe a flag I can set on the pages at allocation time that I can read on free time to know whether to uncharge thehugetlb_cgroupor not?
Reading the code and asking around a bit, it seems the pointer to the hugetlb_cgroup is in page[2].private. Is it reasonable to use page[3].private to store the hugetlb_cgroup to uncharge for the new counter and increment HUGETLB_CGROUP_MIN_ORDER to 3? I think that would solve my problem. When allocating a private NORESERVE page, set page[3].private to the hugetlb_cgroup to uncharge, then on free_huge_page, check page[3].private, if it is non-NULL, uncharge the new counter on it.
Quoted 14 lines · Mina Almasry
Quoted 5 lines · Mina Almasry
Quoted 2 lines · Mike Kravetz
On 9/19/19 3:24 PM, Mina Almasry wrote:
Quoted 7 lines
Mike, note your suggestion above to check if the page
hugetlb_cgroupis null doesn't work if we want to keep the current counter working the same: the page will always have ahugetlb_cgroupthat points that contains the old counter. Any ideas how to apply this new counter behavior to a private NORESERVE mappings? Is there maybe a flag I can set on the pages at allocation time that I can read on free time to know whether to uncharge thehugetlb_cgroupor not?
Quoted 8 lines
Reading the code and asking around a bit, it seems the pointer to the
hugetlb_cgroupis in page[2].private. Is it reasonable to use page[3].private to store thehugetlb_cgroupto uncharge for the new counter and incrementHUGETLB_CGROUP_MIN_ORDERto 3? I think that would solve my problem. When allocating a private NORESERVE page, set page[3].private to thehugetlb_cgroupto uncharge, then onfree_huge_page, check page[3].private, if it is non-NULL, uncharge the new counter on it.
Sorry for not responding sooner. This approach should work, and it looks like you have a v6 of the series. I'll take a look.
Signature · 1 line
Mike Kravetz
Quoted 25 lines · Mike Kravetz
Quoted 14 lines · Mina Almasry
Quoted 5 lines · Mina Almasry
Quoted 2 lines · Mike Kravetz
On 9/19/19 3:24 PM, Mina Almasry wrote:
Quoted 7 lines
Mike, note your suggestion above to check if the page
hugetlb_cgroupis null doesn't work if we want to keep the current counter working the same: the page will always have ahugetlb_cgroupthat points that contains the old counter. Any ideas how to apply this new counter behavior to a private NORESERVE mappings? Is there maybe a flag I can set on the pages at allocation time that I can read on free time to know whether to uncharge thehugetlb_cgroupor not?Quoted 8 lines
Reading the code and asking around a bit, it seems the pointer to the
hugetlb_cgroupis in page[2].private. Is it reasonable to use page[3].private to store thehugetlb_cgroupto uncharge for the new counter and incrementHUGETLB_CGROUP_MIN_ORDERto 3? I think that would solve my problem. When allocating a private NORESERVE page, set page[3].private to thehugetlb_cgroupto uncharge, then onfree_huge_page, check page[3].private, if it is non-NULL, uncharge the new counter on it.
Quoted 3 lines
Sorry for not responding sooner. This approach should work, and it looks like you have a v6 of the series. I'll take a look.
Great! Thanks! That's the approach I went with in v6.
Quoted 2 lines
--
Mike Kravetz