hugetlb_cgroup: Add hugetlb_cgroup reservation counter
Subsystems (5)
PATCH v12 1/9 hugetlb_cgroup: Add hugetlb_cgroup reservation counter
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
This is patch 1 of an 9 patch series.
Problem:
Currently tasks attempting to reserve more hugetlb memory than is available get a failure at mmap/shmget time. This is thanks to Hugetlbfs Reservations [1]. However, if a task attempts to reserve more hugetlb memory than its hugetlb_cgroup limit allows, the kernel will allow the mmap/shmget call, but will SIGBUS the task when it attempts to fault in the excess memory.
We have users hitting their hugetlb_cgroup limits and thus we've been looking at this failure mode. We'd like to improve this behavior such that users violating the hugetlb_cgroup limits get an error on mmap/shmget time, rather than getting SIGBUS'd when they try to fault the excess memory in. This gives the user an opportunity to fallback more gracefully to non-hugetlbfs memory for example.
The underlying problem is that today's hugetlb_cgroup accounting happens at hugetlb memory *fault* time, rather than at *reservation* time. Thus, enforcing the hugetlb_cgroup limit only happens at fault time, and the offending task gets SIGBUS'd.
Proposed Solution:
A new page counter named 'hugetlb.xMB.rsvd.[limit|usage|max_usage]_in_bytes'. This counter has slightly different semantics than 'hugetlb.xMB.[limit|usage|max_usage]_in_bytes':
- While usage_in_bytes tracks all *faulted* hugetlb memory, rsvd.usage_in_bytes tracks all *reserved* hugetlb memory and hugetlb memory faulted in without a prior reservation.
- If a task attempts to reserve more memory than limit_in_bytes allows, the kernel will allow it to do so. But if a task attempts to reserve more memory than rsvd.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 rsvd.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 rsvd.limit_in_bytes concurrently, and this approach gives them the option to do so.
Testing:
- Added tests passing.
- Used libhugetlbfs for regression testing.
[1]: https://www.kernel.org/doc/html/latest/vm/hugetlbfs_reserv.html
---
Changes in v12:
- Minor code formatting.
Changes in v11:
- Renamed resv.* or 'reservation' or 'reserved' to rsvd.*
- Renamed hugetlb_cgroup_get_counter() to hugetlb_cgroup_counter_from_cgroup().
Changes in v10:
- Renamed reservation_* to resv.*
--- include/linux/hugetlb.h | 4 +- mm/hugetlb_cgroup.c | 115 +++++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 15 deletions(-)
include/linux/hugetlb.h +2 -2
@@ -432,8 +432,8 @@ struct hstate {unsigned int surplus_huge_pages_node[MAX_NUMNODES ];#ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ -struct cftype cgroup_files_dfl[5 ]; -struct cftype cgroup_files_legacy[5 ]; +struct cftype cgroup_files_dfl[7 ]; +struct cftype cgroup_files_legacy[9 ];#endif char name[HSTATE_NAME_LEN ]; };
mm/hugetlb_cgroup.c +102 -13
256 lines not on this page · raw message
PATCH v12 2/9 hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Augments hugetlb_cgroup_charge_cgroup to be able to charge hugetlb usage or hugetlb reservation counter.
Adds a new interface to uncharge a hugetlb_cgroup counter via hugetlb_cgroup_uncharge_counter.
Integrates the counter with hugetlb_cgroup, via hugetlb_cgroup_init, hugetlb_cgroup_have_usage, and hugetlb_cgroup_css_offline.
---
Changes in v12:
- Instead of true/false param for rsvd or non-rsvd calls, now there is:
hugetlb_cgroup_*() call for non-rsvd
hugetlb_cgroup_*_rsvd() call for rsvd
__hugetlb_cgroup_*(, bool) for both.
- Removed review tags as this patch changed quite a bit.
Changes in v11:
- Changed all 'reserved' or 'reservation' to 'rsvd' to reflect the user interface.
- Expanded comment that describes tail pages usage.
facing naming.
Changes in v10:
- Added missing VM_BUG_ON
Changes in V9:
- Fixed HUGETLB_CGROUP_MIN_ORDER.
- Minor variable name update.
- Moved some init/cleanup code from later patches in the series to this patch.
- Updated reparenting of reservation accounting.
--- include/linux/hugetlb_cgroup.h | 123 +++++++++++++++++++---- mm/hugetlb.c | 2 + mm/hugetlb_cgroup.c | 174 +++++++++++++++++++++++++++------ 3 files changed, 251 insertions(+), 48 deletions(-)
include/linux/hugetlb_cgroup.h +105 -18
@@ -20,32 +20,64 @@struct hugetlb_cgroup ;/* * Minimum page order trackable by hugetlb cgroup. -* At least 3 pages are necessary for all the tracking information. +* At least 4 pages are necessary for all the tracking information. +* The second tail page (hpage[2]) is the fault usage cgroup. +* The third tail page (hpage[3]) is the reservation usage cgroup. */ #define HUGETLB_CGROUP_MIN_ORDER 2#ifdef CONFIG_CGROUP_HUGETLB -static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page) +static inline struct hugetlb_cgroup * +__hugetlb_cgroup_from_page (struct page *page,bool rsvd) {VM_BUG_ON_PAGE (!PageHuge (page), page);if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return NULL ; -return (struct hugetlb_cgroup *)page[2 ].private; +if (rsvd) +return (struct hugetlb_cgroup *)page[3 ].private; +else +return (struct hugetlb_cgroup *)page[2 ].private; +} + +static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page) +{ +return __hugetlb_cgroup_from_page (page, false); } -static inline -int set_hugetlb_cgroup (struct page *page,struct hugetlb_cgroup *h_cg) +static inline struct hugetlb_cgroup * +hugetlb_cgroup_from_page_rsvd (struct page *page) +{ +return __hugetlb_cgroup_from_page (page, true); +} + +static inline int __set_hugetlb_cgroup (struct page *page, +struct hugetlb_cgroup *h_cg,bool rsvd) {VM_BUG_ON_PAGE (!PageHuge (page), page);if (compound_order (page) <HUGETLB_CGROUP_MIN_ORDER )return -1 ; - page[2 ].private = (unsigned long )h_cg; +if (rsvd) + page[3 ].private = (unsigned long )h_cg; +else + page[2 ].private = (unsigned long )h_cg;return 0 ; } +static inline int set_hugetlb_cgroup (struct page *page, +struct hugetlb_cgroup *h_cg) +{ +return __set_hugetlb_cgroup (page, h_cg, false); +} + +static inline int set_hugetlb_cgroup_rsvd (struct page *page, +struct hugetlb_cgroup *h_cg) +{ +return __set_hugetlb_cgroup (page, h_cg, true); +} +static inline bool hugetlb_cgroup_disabled (void ) {return !cgroup_subsys_enabled (hugetlb_cgrp_subsys);
@@ -53,13 +85,27 @@ static inline bool hugetlb_cgroup_disabled(void)extern int hugetlb_cgroup_charge_cgroup (int idx,unsigned long nr_pages,struct hugetlb_cgroup **ptr); +extern int hugetlb_cgroup_charge_cgroup_rsvd (int idx,unsigned long nr_pages, +struct hugetlb_cgroup **ptr);extern void hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages,struct hugetlb_cgroup *h_cg,struct page *page); +extern void hugetlb_cgroup_commit_charge_rsvd (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_page_rsvd (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); +extern void hugetlb_cgroup_uncharge_cgroup_rsvd (int idx,unsigned long nr_pages, +struct hugetlb_cgroup *h_cg); +extern void hugetlb_cgroup_uncharge_counter (struct page_counter *p, +unsigned long nr_pages, +struct cgroup_subsys_state *css); +extern void hugetlb_cgroup_file_init (void )__init ;extern void hugetlb_cgroup_migrate (struct page *oldhpage,struct page *newhpage);
@@ -70,8 +116,26 @@ static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page)return NULL ; } -static inline -int set_hugetlb_cgroup (struct page *page,struct hugetlb_cgroup *h_cg) +static inline struct hugetlb_cgroup * +hugetlb_cgroup_from_page_resv (struct page *page) +{ +return NULL ; +} + +static inline struct hugetlb_cgroup * +hugetlb_cgroup_from_page_rsvd (struct page *page) +{ +return NULL ; +} + +static inline int set_hugetlb_cgroup (struct page *page, +struct hugetlb_cgroup *h_cg) +{ +return 0 ; +} + +static inline int set_hugetlb_cgroup_rsvd (struct page *page, +struct hugetlb_cgroup *h_cg) {return 0 ; }
@@ -81,28 +145,51 @@ 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) {return 0 ; } -static inline void -hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages, -struct hugetlb_cgroup *h_cg, -struct page *page) +static inline int hugetlb_cgroup_charge_cgroup_rsvd (int idx, +unsigned long nr_pages, +struct hugetlb_cgroup **ptr) +{ +return 0 ; +} + +static inline void hugetlb_cgroup_commit_charge (int idx,unsigned long nr_pages, +struct hugetlb_cgroup *h_cg, +struct page *page) { }static inline void -hugetlb_cgroup_uncharge_page (int idx,unsigned long nr_pages,struct page *page) +hugetlb_cgroup_commit_charge_rsvd (int idx,unsigned long nr_pages, +struct hugetlb_cgroup *h_cg, +struct page *page) +{ +} + +static inline void hugetlb_cgroup_uncharge_page (int idx,unsigned long nr_pages, +struct page *page) +{ +} + +static inline void hugetlb_cgroup_uncharge_page_rsvd (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) +hugetlb_cgroup_uncharge_cgroup_rsvd (int idx,unsigned long nr_pages, +struct hugetlb_cgroup *h_cg) { }
mm/hugetlb.c +2 -0
@@ -1069,6 +1069,7 @@ static void update_and_free_page(struct hstate *h, struct page *page) 1 << PG_writeback); }VM_BUG_ON_PAGE (hugetlb_cgroup_from_page (page), page); +VM_BUG_ON_PAGE (hugetlb_cgroup_from_page_rsvd (page), page);set_compound_page_dtor (page,NULL_COMPOUND_DTOR );set_page_refcounted (page);if (hstate_is_gigantic (h)) {
@@ -1254,6 +1255,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)set_compound_page_dtor (page,HUGETLB_PAGE_DTOR );spin_lock (&hugetlb_lock);set_hugetlb_cgroup (page,NULL ); +set_hugetlb_cgroup_rsvd (page,NULL ); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++;spin_unlock (&hugetlb_lock);
mm/hugetlb_cgroup.c +144 -30
281 lines not on this page · raw message
--
2.25.0.225.g125e21ebc7-goog
Re: [PATCH v12 2/9] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Quoted 11 lines · Mina Almasry
Augments
hugetlb_cgroup_charge_cgroupto be able to charge hugetlb usage or hugetlb reservation counter.Adds a new interface to uncharge a
hugetlb_cgroupcounter viahugetlb_cgroup_uncharge_counter.Integrates the counter with
hugetlb_cgroup, viahugetlb_cgroup_init,hugetlb_cgroup_have_usage, andhugetlb_cgroup_css_offline.Signed-off-by Mina Almasry <[email protected]>
Thanks for the suggested changes. It will make the code easier to read and understand.
Quoted 8 lines
---
Changes in v12:
- Instead of true/false param for rsvd or non-rsvd calls, now there is:
hugetlb_cgroup_*() call for non-rsvd
hugetlb_cgroup_*_rsvd() call for rsvd
__hugetlb_cgroup_*(, bool) for both.
- Removed review tags as this patch changed quite a bit.
David Rientjes
Acked-byRe: [PATCH v12 2/9] hugetlb_cgroup: add interface for charge/uncharge hugetlb reservations
Quoted 10 lines · Mike Kravetz
Quoted 9 lines · Mina Almasry
Augments
hugetlb_cgroup_charge_cgroupto be able to charge hugetlb usage or hugetlb reservation counter.Adds a new interface to uncharge a
hugetlb_cgroupcounter viahugetlb_cgroup_uncharge_counter.Integrates the counter with
hugetlb_cgroup, viahugetlb_cgroup_init,hugetlb_cgroup_have_usage, andhugetlb_cgroup_css_offline.
Quoted 8 lines
Quoted 2 lines
Signed-off-by Mina Almasry <[email protected]>Thanks for the suggested changes. It will make the code easier to read and understand.
Acked-by Mike Kravetz <[email protected]
Agreed, thanks Mina!
PATCH v12 3/9 hugetlb_cgroup: add reservation accounting for private mappings
In reply to Mina AlmasryNormally the pointer to the cgroup to uncharge hangs off the struct page, and gets queried when it's time to free the page. With hugetlb_cgroup reservations, this is not possible. Because it's possible for a page to be reserved by one task and actually faulted in by another task.
The best place to put the hugetlb_cgroup pointer to uncharge for reservations is in the resv_map. But, because the resv_map has different semantics for private and shared mappings, the code patch to charge/uncharge shared and private mappings is different. This patch implements charging and uncharging for private mappings.
For private mappings, the counter to uncharge is in resv_map->reservation_counter. On initializing the resv_map this is set to NULL. On reservation of a region in private mapping, the tasks hugetlb_cgroup is charged and the hugetlb_cgroup is placed is resv_map->reservation_counter.
On hugetlb_vm_op_close, we uncharge resv_map->reservation_counter.
---
Changes in v12:
- Very minor interface update for readability.
Changes in v11:
- Refactored hugetlb_cgroup_uncharge_conuter a bit to eliminate unnecessary #ifdefs.
- Added resv_map_set_hugetlb_cgroup_uncharge_info() to eliminate #ifdefs in the middle of hugetlb logic.
Changes in v10:
- Fixed cases where the mapping is private but cgroup accounting is disabled.
Changes in V9:
- Updated for reparenting of hugetlb reservation accounting.
--- include/linux/hugetlb.h | 10 ++++++++ include/linux/hugetlb_cgroup.h | 39 +++++++++++++++++++++++++--- mm/hugetlb.c | 47 +++++++++++++++++++++++++++++++--- mm/hugetlb_cgroup.c | 41 +++++------------------------ 4 files changed, 97 insertions(+), 40 deletions(-)
include/linux/hugetlb.h +10 -0
@@ -46,6 +46,16 @@ struct resv_map {long adds_in_progress;struct list_head region_cache;long region_cache_count; +#ifdef CONFIG_CGROUP_HUGETLB +/* +* On private mappings, the counter to uncharge reservations is stored +* here. If these fields are 0, then either the mapping is shared, or +* cgroup accounting is disabled for this resv_map. +*/ +struct page_counter *reservation_counter; +unsigned long pages_per_hpage; +struct cgroup_subsys_state *css; +#endif };extern struct resv_map *resv_map_alloc (void );void resv_map_release (struct kref *ref);
include/linux/hugetlb_cgroup.h +36 -3
@@ -27,6 +27,33 @@ struct hugetlb_cgroup;#define HUGETLB_CGROUP_MIN_ORDER 2#ifdef CONFIG_CGROUP_HUGETLB +enum hugetlb_memory_event { +HUGETLB_MAX , +HUGETLB_NR_MEMORY_EVENTS , +}; + +struct hugetlb_cgroup { +struct cgroup_subsys_state css; + +/* +* the counter to account for hugepages from hugetlb. +*/ +struct page_counter hugepage[HUGE_MAX_HSTATE ]; + +/* +* the counter to account for hugepage reservations from hugetlb. +*/ +struct page_counter rsvd_hugepage[HUGE_MAX_HSTATE ]; + +atomic_long_t events[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; +atomic_long_t events_local[HUGE_MAX_HSTATE ][HUGETLB_NR_MEMORY_EVENTS ]; + +/* Handle for "hugetlb.events" */ +struct cgroup_file events_file[HUGE_MAX_HSTATE ]; + +/* Handle for "hugetlb.events.local" */ +struct cgroup_file events_local_file[HUGE_MAX_HSTATE ]; +};static inline struct hugetlb_cgroup *__hugetlb_cgroup_from_page (struct page *page,bool rsvd)
@@ -102,9 +129,9 @@ extern void hugetlb_cgroup_uncharge_cgroup(int idx, unsigned long nr_pages, struct hugetlb_cgroup *h_cg);extern void hugetlb_cgroup_uncharge_cgroup_rsvd (int idx,unsigned long nr_pages,struct hugetlb_cgroup *h_cg); -extern void hugetlb_cgroup_uncharge_counter (struct page_counter *p, -unsigned long nr_pages, -struct cgroup_subsys_state *css); +extern void hugetlb_cgroup_uncharge_counter (struct resv_map *resv, +unsigned long start, +unsigned long end);extern void hugetlb_cgroup_file_init (void )__init ; extern void hugetlb_cgroup_migrate(struct page *oldhpage,
@@ -193,6 +220,12 @@ hugetlb_cgroup_uncharge_cgroup_rsvd(int idx, unsigned long nr_pages, { } +static inline void hugetlb_cgroup_uncharge_counter (struct resv_map *resv, +unsigned long start, +unsigned long end) +{ +} +static inline void hugetlb_cgroup_file_init (void ) { }
mm/hugetlb.c +44 -3
89 lines not on this page · raw message
mm/hugetlb_cgroup.c +7 -34
56 lines not on this page · raw message
PATCH v12 4/9 hugetlb: disable region_add file_region coalescing
In reply to Mina AlmasryA follow up patch in this series adds hugetlb cgroup uncharge info the file_region entries in resv->regions. The cgroup uncharge info may differ for different regions, so they can no longer be coalesced at region_add time. So, disable region coalescing in region_add in this patch.
Behavior change:
Say a resv_map exists like this [0->1], [2->3], and [5->6].
Then a region_chg/add call comes in region_chg/add(f=0, t=5).
Old code would generate resv->regions: [0->5], [5->6].
New code would generate resv->regions: [0->1], [1->2], [2->3], [3->5], [5->6].
Special care needs to be taken to handle the resv->adds_in_progress variable correctly. In the past, only 1 region would be added for every region_chg and region_add call. But now, each call may add multiple regions, so we can no longer increment adds_in_progress by 1 in region_chg, or decrement adds_in_progress by 1 after region_add or region_abort. Instead, region_chg calls add_reservation_in_range() to count the number of regions needed and allocates those, and that info is passed to region_add and region_abort to decrement adds_in_progress correctly.
We've also modified the assumption that region_add after region_chg never fails. region_chg now pre-allocates at least 1 region for region_add. If region_add needs more regions than region_chg has allocated for it, then it may fail.
---
Changes in v12:
- Removed Mike's Reviewed-by until coalescing is reviewed per his request.
- Added VM_BUG_ON that was mistakingly in a follow up patch.
Changes in v9:
- Added clarifications in the comments and addressed minor issues from code review.
Changes in v7:
- region_chg no longer allocates (t-f) / 2 file_region entries.
Changes in v6:
- Fix bug in number of region_caches allocated by region_chg
--- mm/hugetlb.c | 328 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 224 insertions(+), 104 deletions(-)
mm/hugetlb.c +224 -104
@@ -245,110 +245,180 @@ struct file_region {long to; }; +/* Helper that removes a struct file_region from the resv_map cache and returns +* it for use. +*/ +static struct file_region * +get_file_region_entry_from_cache (struct resv_map *resv,long from,long to) +{ +struct file_region *nrg =NULL ; + +VM_BUG_ON (resv->region_cache_count <=0 ); + + resv->region_cache_count--; + nrg = list_first_entry(&resv->region_cache, struct file_region, link); +VM_BUG_ON (!nrg); +list_del (&nrg->link); + + nrg->from = from; + nrg->to = to; + +return nrg; +} +/* Must be called with resv->lock held. Calling this with count_only == true * will count the number of pages to be added but will not modify the linked -* list. +* list. If regions_needed != NULL and count_only == true, then regions_needed +* will indicate the number of file_regions needed in the cache to carry out to +* add the regions for this range. */ static long add_reservation_in_range (struct resv_map *resv,long f,long t, -bool count_only) +long *regions_needed,bool count_only) { -long chg =0 ; +long add =0 ;struct list_head *head = &resv->regions; +long last_accounted_offset = f;struct file_region *rg =NULL , *trg =NULL , *nrg =NULL ; -/* Locate the region we are before or in. */ -list_for_each_entry (rg, head, link) -if (f <= rg->to) -break ; - -/* Round our left edge to the current segment if it encloses us. */ -if (f > rg->from) - f = rg->from; +if (regions_needed) + *regions_needed =0 ; - chg = t - f; +/* In this loop, we essentially handle an entry for the range +* [last_accounted_offset, rg->from), at every iteration, with some +* bounds checking. +*/ +list_for_each_entry_safe (rg, trg, head, link) { +/* Skip irrelevant regions that start before our range. */ +if (rg->from < f) { +/* If this region ends after the last accounted offset, +* then we need to update last_accounted_offset. +*/ +if (rg->to > last_accounted_offset) + last_accounted_offset = rg->to; +continue ; + } -/* Check for and consume any regions we now overlap with. */ - nrg = rg; -list_for_each_entry_safe (rg, trg, rg->link.prev, link) { -if (&rg->link == head) -break ; +/* When we find a region that starts beyond our range, we've +* finished. +*/ if (rg->from > t)break ; -/* We overlap with this area, if it extends further than -* us then we must extend ourselves. Account for its -* existing reservation. +/* Add an entry for last_accounted_offset -> rg->from, and +* update last_accounted_offset. */ -if (rg->to > t) { - chg += rg->to - t; - t = rg->to; +if (rg->from > last_accounted_offset) { + add += rg->from - last_accounted_offset; +if (!count_only) { + nrg =get_file_region_entry_from_cache ( + resv, last_accounted_offset, rg->from); +list_add (&nrg->link, rg->link.prev); + }else if (regions_needed) + *regions_needed +=1 ; } - chg -= rg->to - rg->from; -if (!count_only && rg != nrg) { -list_del (&rg->link); -kfree (rg); - } + last_accounted_offset = rg->to; } -if (!count_only) { - nrg->from = f; - nrg->to = t; +/* Handle the case where our range extends beyond +* last_accounted_offset. +*/ +if (last_accounted_offset < t) { + add += t - last_accounted_offset; +if (!count_only) { + nrg =get_file_region_entry_from_cache ( + resv, last_accounted_offset, t); +list_add (&nrg->link, rg->link.prev); + }else if (regions_needed) + *regions_needed +=1 ; } -return chg; +VM_BUG_ON (add <0 ); +return add; }/* * Add the huge page range represented by [f, t) to the reserve -* map. Existing regions will be expanded to accommodate the specified -* range, or a region will be taken from the cache. Sufficient regions -* must exist in the cache due to the previous call to region_chg with -* the same range. +* map. Regions will be taken from the cache to fill in this range. +* Sufficient regions should exist in the cache due to the previous +* call to region_chg with the same range, but in some cases the cache will not +* have sufficient entries due to races with other code doing region_add or +* region_del. The extra needed entries will be allocated. +* +* 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. +* Return the number of new huge pages added to the map. This number is greater +* than or equal to zero. If file_region entries needed to be allocated for +* this operation and we were not able to allocate, it ruturns -ENOMEM. +* region_add of regions of length 1 never allocate file_regions and cannot +* fail; region_chg will always allocate at least 1 entry and a region_add for +* 1 page will only require at most 1 entry. */ -static long region_add (struct resv_map *resv,long f,long t) +static long region_add (struct resv_map *resv,long f,long t, +long in_regions_needed) { -struct list_head *head = &resv->regions; -struct file_region *rg, *nrg; -long add =0 ; +long add =0 , actual_regions_needed =0 , i =0 ; +struct file_region *trg =NULL , *rg =NULL ; +struct list_head allocated_regions; + +INIT_LIST_HEAD (&allocated_regions);spin_lock (&resv->lock); -/* Locate the region we are either in or before. */ -list_for_each_entry (rg, head, link) -if (f <= rg->to) -break ; +retry: + +/* Count how many regions are actually needed to execute this add. */ +add_reservation_in_range (resv, f, t, &actual_regions_needed, true);/* -* If no region exists which can be expanded to include the -* specified range, pull a region descriptor from the cache -* and use it for this range. +* Check for sufficient descriptors in the cache to accommodate +* this add operation. Note that actual_regions_needed may be greater +* than in_regions_needed. In this case, we need to make sure that we +* allocate extra entries, such that we have enough for all the +* existing adds_in_progress, plus the excess needed for this +* operation. */ -if (&rg->link == head || t < rg->from) { -VM_BUG_ON (resv->region_cache_count <=0 ); +if (resv->region_cache_count < + resv->adds_in_progress + + (actual_regions_needed - in_regions_needed)) { +/* region_add operation of range 1 should never need to +* allocate file_region entries. +*/ +VM_BUG_ON (t - f <=1 ); - resv->region_cache_count--; - nrg = list_first_entry(&resv->region_cache, struct file_region, - link); -list_del (&nrg->link); +/* Must drop lock to allocate a new descriptor. */ +spin_unlock (&resv->lock); +for (i =0 ; i < (actual_regions_needed - in_regions_needed); + i++) { + trg =kmalloc (sizeof (*trg),GFP_KERNEL ); +if (!trg) + goto out_of_memory; +list_add (&trg->link, &allocated_regions); + } +spin_lock (&resv->lock); - nrg->from = f; - nrg->to = t; -list_add (&nrg->link, rg->link.prev); +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +list_add (&rg->link, &resv->region_cache); + resv->region_cache_count++; + } - add += t - f; - goto out_locked; + goto retry; } - add =add_reservation_in_range (resv, f, t, false); + add =add_reservation_in_range (resv, f, t,NULL , false); + + resv->adds_in_progress -= in_regions_needed; -out_locked: - resv->adds_in_progress--;spin_unlock (&resv->lock);VM_BUG_ON (add <0 );return add; + +out_of_memory: +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +kfree (rg); + } +return -ENOMEM ; }/*
@@ -358,49 +428,79 @@ static long region_add(struct resv_map *resv, long f, long t)* call to region_add that will actually modify the reserve * map to add the specified range [f, t). region_chg does * not change the number of huge pages represented by the -* map. A new file_region structure is added to the cache -* as a placeholder, so that the subsequent region_add -* call will have all the regions it needs and will not fail. +* map. A number of new file_region structures is added to the cache as a +* placeholder, for the subsequent region_add call to use. At least 1 +* file_region structure is added. +* +* out_regions_needed is the number of regions added to the +* resv->adds_in_progress. This value needs to be provided to a follow up call +* to region_add or region_abort for proper accounting. * * Returns the number of huge pages that need to be added to the existing * reservation map for the range [f, t). This number is greater or equal to * zero. -ENOMEM is returned if a new file_region structure or cache entry * is needed and can not be allocated. */ -static long region_chg (struct resv_map *resv,long f,long t) +static long region_chg (struct resv_map *resv,long f,long t, +long *out_regions_needed) { -long chg =0 ; +struct file_region *trg =NULL , *rg =NULL ; +long chg =0 , i =0 , to_allocate =0 ; +struct list_head allocated_regions; + +INIT_LIST_HEAD (&allocated_regions);spin_lock (&resv->lock); -retry_locked: - resv->adds_in_progress++; + +/* Count how many hugepages in this range are NOT respresented. */ + chg =add_reservation_in_range (resv, f, t, out_regions_needed, true); + +if (*out_regions_needed ==0 ) + *out_regions_needed =1 ; + + resv->adds_in_progress += *out_regions_needed;/* * Check for sufficient descriptors in the cache to accommodate * the number of in progress add operations. */ -if (resv->adds_in_progress > resv->region_cache_count) { -struct file_region *trg; - -VM_BUG_ON (resv->adds_in_progress - resv->region_cache_count >1 ); -/* Must drop lock to allocate a new descriptor. */ - resv->adds_in_progress--; +while (resv->region_cache_count < resv->adds_in_progress) { + to_allocate = resv->adds_in_progress - resv->region_cache_count; + +/* Must drop lock to allocate a new descriptor. Note that even +* though we drop the lock here, we do not make another call to +* add_reservation_in_range after re-acquiring the lock. +* Essentially this branch makes sure that we have enough +* descriptors in the cache as suggested by the first call to +* add_reservation_in_range. If more regions turn out to be +* required, region_add will deal with it. +*/ spin_unlock (&resv->lock); - - trg =kmalloc (sizeof (*trg),GFP_KERNEL ); -if (!trg) -return -ENOMEM ; +for (i =0 ; i < to_allocate; i++) { + trg =kmalloc (sizeof (*trg),GFP_KERNEL ); +if (!trg) + goto out_of_memory; +list_add (&trg->link, &allocated_regions); + }spin_lock (&resv->lock); -list_add (&trg->link, &resv->region_cache); - resv->region_cache_count++; - goto retry_locked; - } - chg =add_reservation_in_range (resv, f, t, true); +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +list_add (&rg->link, &resv->region_cache); + resv->region_cache_count++; + } + }spin_unlock (&resv->lock);return chg; + +out_of_memory: +list_for_each_entry_safe (rg, trg, &allocated_regions, link) { +list_del (&rg->link); +kfree (rg); + } +return -ENOMEM ; }/*
@@ -408,17 +508,20 @@ static long region_chg(struct resv_map *resv, long f, long t)* of the resv_map keeps track of the operations in progress between * calls to region_chg and region_add. Operations are sometimes * aborted after the call to region_chg. In such cases, region_abort -* is called to decrement the adds_in_progress counter. +* is called to decrement the adds_in_progress counter. regions_needed +* is the value returned by the region_chg call, it is used to decrement +* the adds_in_progress counter. * * NOTE: The range arguments [f, t) are not needed or used in this * routine. They are kept to make reading the calling code easier as * arguments will match the associated region_chg call. */ -static void region_abort (struct resv_map *resv,long f,long t) +static void region_abort (struct resv_map *resv,long f,long t, +long regions_needed) {spin_lock (&resv->lock);VM_BUG_ON (!resv->region_cache_count); - resv->adds_in_progress--; + resv->adds_in_progress -= regions_needed;spin_unlock (&resv->lock); }
@@ -1898,6 +2001,7 @@ static long __vma_reservation_common(struct hstate *h,struct resv_map *resv;pgoff_t idx;long ret; +long dummy_out_regions_needed; resv =vma_resv_map (vma);if (!resv)
@@ -1906,20 +2010,29 @@ static long __vma_reservation_common(struct hstate *h, idx =vma_hugecache_offset (h, vma, addr);switch (mode) {case VMA_NEEDS_RESV : - ret =region_chg (resv, idx, idx +1 ); + ret =region_chg (resv, idx, idx +1 , &dummy_out_regions_needed); +/* We assume that vma_reservation_* routines always operate on +* 1 page, and that adding to resv map a 1 page entry can only +* ever require 1 region. +*/ +VM_BUG_ON (dummy_out_regions_needed !=1 );break ;case VMA_COMMIT_RESV : - ret =region_add (resv, idx, idx +1 ); + ret =region_add (resv, idx, idx +1 ,1 ); +/* region_add calls of range 1 should never fail. */ +VM_BUG_ON (ret <0 );break ;case VMA_END_RESV : -region_abort (resv, idx, idx +1 ); +region_abort (resv, idx, idx +1 ,1 ); ret =0 ;break ;case VMA_ADD_RESV : -if (vma->vm_flags &VM_MAYSHARE ) - ret =region_add (resv, idx, idx +1 ); -else { -region_abort (resv, idx, idx +1 ); +if (vma->vm_flags &VM_MAYSHARE ) { + ret =region_add (resv, idx, idx +1 ,1 ); +/* region_add calls of range 1 should never fail. */ +VM_BUG_ON (ret <0 ); + }else { +region_abort (resv, idx, idx +1 ,1 ); ret =region_del (resv, idx, idx +1 ); }break ;
@@ -4569,12 +4682,12 @@ int hugetlb_reserve_pages(struct inode *inode,struct vm_area_struct *vma, vm_flags_t vm_flags) { - long ret, chg; + long ret, chg, add = -1;struct hstate *h =hstate_inode (inode);struct hugepage_subpool *spool =subpool_inode (inode);struct resv_map *resv_map;struct hugetlb_cgroup *h_cg; -long gbl_reserve; +long gbl_reserve, regions_needed =0 ;/* This should never happen */ if (from > to) {
@@ -4604,7 +4717,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. */
@@ -4670,9 +4783,14 @@ int hugetlb_reserve_pages(struct inode *inode,* else has to be done for private mappings here */ if (!vma || vma->vm_flags &VM_MAYSHARE ) { -long add =region_add (resv_map, from, to); - -if (unlikely (chg > add)) { + add =region_add (resv_map, from, to, regions_needed); + +if (unlikely (add <0 )) { +hugetlb_acct_memory (h, -gbl_reserve); +/* put back original number of pages, chg */ + (void )hugepage_subpool_put_pages (spool, chg); + goto out_err; + }else if (unlikely (chg > add)) {/* * pages in this range were added to the reserve * map between region_chg and region_add. This
@@ -4690,9 +4808,11 @@ int hugetlb_reserve_pages(struct inode *inode,return 0 ; out_err:if (!vma || vma->vm_flags &VM_MAYSHARE ) -/* Don't call region_abort if region_chg failed */ -if (chg >=0 ) -region_abort (resv_map, from, to); +/* Only call region_abort if the region_chg succeeded but the +* region_add failed or didn't run. +*/ +if (chg >=0 && add <0 ) +region_abort (resv_map, from, to, regions_needed);if (vma &&is_vma_resv_set (vma,HPAGE_RESV_OWNER ))kref_put (&resv_map->refs, resv_map_release);return ret;
--
2.25.0.225.g125e21ebc7-goog
Re: [PATCH v12 4/9] hugetlb: disable region_add file_region coalescing
Quoted 25 lines · Mina Almasry
A follow up patch in this series adds hugetlb cgroup uncharge info the
file_regionentries inresv->regions. The cgroup uncharge info may differ for different regions, so they can no longer be coalesced atregion_addtime. So, disable region coalescing inregion_addin this patch.Behavior change:
Say a
resv_mapexists like this [0->1], [2->3], and [5->6].Then a region_chg/add call comes in region_chg/add(f=0, t=5).
Old code would generate
resv->regions: [0->5], [5->6].
New code would generateresv->regions: [0->1], [1->2], [2->3], [3->5], [5->6].Special care needs to be taken to handle the
resv->adds_in_progressvariable correctly. In the past, only 1 region would be added for everyregion_chgandregion_addcall. But now, each call may add multiple regions, so we can no longer incrementadds_in_progressby 1 inregion_chg, or decrementadds_in_progressby 1 afterregion_addorregion_abort. Instead,region_chgcallsadd_reservation_in_range()to count the number of regions needed and allocates those, and that info is passed toregion_addandregion_abortto decrementadds_in_progresscorrectly.
Quoted 7 lines
We've also modified the assumption that
region_addafterregion_chgnever fails.region_chgnow pre-allocates at least 1 region forregion_add. Ifregion_addneeds more regions thanregion_chghas allocated for it, then it may fail.Signed-off-by Mina Almasry <[email protected]>
My reason for previously asking my Reviewed-by be removed is because I did not realize the suboptimal behavior for private mappings would be addressed in a subsequent patch. That should be OK as nobody should be using just part of this series. If we need to respin, it might be worth noting this in the commit message. Something like:
With file_region coalescing disabled, private mappings will have one file_region for each page in the mapping. This suboptimal behavior will be addressed in a later patch in this series.
Quoted 6 lines
---
Changes in v12:
- Removed Mike's Reviewed-by until coalescing is reviewed per his request.
- AddedVM_BUG_ONthat was mistakingly in a follow up patch.
Re: [PATCH v12 4/9] hugetlb: disable region_add file_region coalescing
In reply to Mina AlmasryQuoted 25 lines · Mina Almasry
A follow up patch in this series adds hugetlb cgroup uncharge info the
file_regionentries inresv->regions. The cgroup uncharge info may differ for different regions, so they can no longer be coalesced atregion_addtime. So, disable region coalescing inregion_addin this patch.Behavior change:
Say a
resv_mapexists like this [0->1], [2->3], and [5->6].Then a region_chg/add call comes in region_chg/add(f=0, t=5).
Old code would generate
resv->regions: [0->5], [5->6].
New code would generateresv->regions: [0->1], [1->2], [2->3], [3->5], [5->6].Special care needs to be taken to handle the
resv->adds_in_progressvariable correctly. In the past, only 1 region would be added for everyregion_chgandregion_addcall. But now, each call may add multiple regions, so we can no longer incrementadds_in_progressby 1 inregion_chg, or decrementadds_in_progressby 1 afterregion_addorregion_abort. Instead,region_chgcallsadd_reservation_in_range()to count the number of regions needed and allocates those, and that info is passed toregion_addandregion_abortto decrementadds_in_progresscorrectly.
Quoted 6 lines
We've also modified the assumption that
region_addafterregion_chgnever fails.region_chgnow pre-allocates at least 1 region forregion_add. Ifregion_addneeds more regions thanregion_chghas allocated for it, then it may fail.Signed-off-by Mina Almasry <[email protected]>
PATCH v12 5/9 hugetlb_cgroup: add accounting for shared mappings
In reply to Mina AlmasryFor shared mappings, the pointer to the hugetlb_cgroup to uncharge lives in the resv_map entries, in file_region->reservation_counter.
After a call to region_chg, we charge the approprate hugetlb_cgroup, and if successful, we pass on the hugetlb_cgroup info to a follow up region_add call. When a file_region entry is added to the resv_map via region_add, we put the pointer to that cgroup in file_region->reservation_counter. If charging doesn't succeed, we report the error to the caller, so that the kernel fails the reservation.
On region_del, which is when the hugetlb memory is unreserved, we also uncharge the file_region->reservation_counter.
---
Changes in v12:
- Removed per-file-region pages_per_hpage field.
- Rearranged some function params.
- Fixed goto out_put_pages error condition.
Changes in v11:
- Created new function, hugetlb_cgroup_uncharge_file_region to clean up some #ifdefs.
- Moved file_region definition to hugetlb.h.
- Added copy_hugetlb_cgroup_uncharge_info function to clean up more #ifdefs in the middle of hugetlb code.
Changes in v10:
- Deleted duplicated code snippet.
Changes in V9:
- Updated for hugetlb reservation repareting.
--- include/linux/hugetlb.h | 35 ++++++++ include/linux/hugetlb_cgroup.h | 10 +++ mm/hugetlb.c | 148 +++++++++++++++++++++------------ mm/hugetlb_cgroup.c | 15 ++++ 4 files changed, 154 insertions(+), 54 deletions(-)
include/linux/hugetlb.h +35 -0
@@ -57,6 +57,41 @@ struct resv_map {struct cgroup_subsys_state *css;#endif }; + +/* +* Region tracking -- allows tracking of reservations and instantiated pages +* across the pages in a mapping. +* +* The region data structures are embedded into a resv_map and protected +* by a resv_map's lock. The set of regions within the resv_map represent +* reservations for huge pages, or huge pages that have already been +* instantiated within the map. The from and to elements are huge page +* indicies into the associated mapping. from indicates the starting index +* of the region. to represents the first index past the end of the region. +* +* For example, a file region structure with from == 0 and to == 4 represents +* four huge pages in a mapping. It is important to note that the to element +* represents the first element past the end of the region. This is used in +* arithmetic as 4(to) - 0(from) = 4 huge pages in the region. +* +* Interval notation of the form [from, to) will be used to indicate that +* the endpoint from is inclusive and to is exclusive. +*/ +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; +struct cgroup_subsys_state *css; +#endif +}; +extern struct resv_map *resv_map_alloc (void );void resv_map_release (struct kref *ref);
include/linux/hugetlb_cgroup.h +10 -0
@@ -133,11 +133,21 @@ extern void hugetlb_cgroup_uncharge_counter(struct resv_map *resv,unsigned long start, unsigned long end); +extern void hugetlb_cgroup_uncharge_file_region (struct resv_map *resv, +struct file_region *rg, +unsigned long nr_pages); +extern void hugetlb_cgroup_file_init (void )__init ;extern void hugetlb_cgroup_migrate (struct page *oldhpage,struct page *newhpage);#else +static inline void hugetlb_cgroup_uncharge_file_region (struct resv_map *resv, +struct file_region *rg, +unsigned long nr_pages) +{ +} +static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page (struct page *page) {return NULL ;
mm/hugetlb.c +94 -54
291 lines not on this page · raw message
mm/hugetlb_cgroup.c +15 -0
@@ -392,6 +392,21 @@ void hugetlb_cgroup_uncharge_counter(struct resv_map *resv, unsigned long start,css_put (resv->css); } +void hugetlb_cgroup_uncharge_file_region (struct resv_map *resv, +struct file_region *rg, +unsigned long nr_pages) +{ +if (hugetlb_cgroup_disabled () || !resv || !rg || !nr_pages) +return ; + +if (rg->reservation_counter && resv->pages_per_hpage && nr_pages >0 && + !resv->reservation_counter) { +page_counter_uncharge (rg->reservation_counter, + nr_pages * resv->pages_per_hpage); +css_put (rg->css); + } +} +enum {RES_USAGE ,RES_RSVD_USAGE ,
Re: [PATCH v12 5/9] hugetlb_cgroup: add accounting for shared mappings
Quoted 10 lines · Mina Almasry
For shared mappings, the pointer to the
hugetlb_cgroupto uncharge lives in theresv_mapentries, infile_region->reservation_counter.After a call to
region_chg, we charge the appropratehugetlb_cgroup, and if successful, we pass on thehugetlb_cgroupinfo to a follow upregion_addcall. When afile_regionentry is added to theresv_mapviaregion_add, we put the pointer to that cgroup infile_region->reservation_counter. If charging doesn't succeed, we report the error to the caller, so that the kernel fails the reservation.
Quoted 5 lines
On
region_del, which is when the hugetlb memory is unreserved, we also uncharge thefile_region->reservation_counter.Signed-off-by Mina Almasry <[email protected]>
Does this develop a dependency on hugetlb_cgroup.h in hugetlb.h? Or maybe we only need a forward declaration of struct file_region there?
Re: [PATCH v12 5/9] hugetlb_cgroup: add accounting for shared mappings
In reply to Mina AlmasryQuoted 10 lines · Mina Almasry
For shared mappings, the pointer to the
hugetlb_cgroupto uncharge lives in theresv_mapentries, infile_region->reservation_counter.After a call to
region_chg, we charge the appropratehugetlb_cgroup, and if successful, we pass on thehugetlb_cgroupinfo to a follow upregion_addcall. When afile_regionentry is added to theresv_mapviaregion_add, we put the pointer to that cgroup infile_region->reservation_counter. If charging doesn't succeed, we report the error to the caller, so that the kernel fails the reservation.
Quoted 4 lines
On
region_del, which is when the hugetlb memory is unreserved, we also uncharge thefile_region->reservation_counter.Signed-off-by Mina Almasry <[email protected]>
With addition of the build fix,
Signature · 1 line
Mike Kravetz
PATCH v12 6/9 hugetlb_cgroup: support noreserve mappings
In reply to Mina AlmasrySupport MAP_NORESERVE accounting as part of the new counter.
For each hugepage allocation, at allocation time we check if there is a reservation for this allocation or not. If there is a reservation for this allocation, then this allocation was charged at reservation time, and we don't re-account it. If there is no reserevation for this allocation, we charge the appropriate hugetlb_cgroup.
The hugetlb_cgroup to uncharge for this allocation is stored in page[3].private. We use new APIs added in an earlier patch to set this pointer.
---
Changes in v12:
- Minor rebase to new interface for readability.
Changes in v10:
- Refactored deferred_reserve check.
--- mm/hugetlb.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
mm/hugetlb.c +26 -1
@@ -1342,6 +1342,8 @@ static void __free_huge_page(struct page *page)clear_page_huge_active (page);hugetlb_cgroup_uncharge_page (hstate_index (h),pages_per_huge_page (h), page); +hugetlb_cgroup_uncharge_page_rsvd (hstate_index (h), +pages_per_huge_page (h), page);if (restore_reserve) h->resv_huge_pages++;
@@ -2175,6 +2177,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,long gbl_chg;int ret, idx;struct hugetlb_cgroup *h_cg; +bool deferred_reserve; idx =hstate_index (h);/*
@@ -2212,9 +2215,19 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, gbl_chg =1 ; } +/* If this allocation is not consuming a reservation, charge it now. +*/ + deferred_reserve = map_chg || avoid_reserve || !vma_resv_map (vma); +if (deferred_reserve) { + ret =hugetlb_cgroup_charge_cgroup_rsvd ( + idx,pages_per_huge_page (h), &h_cg); +if (ret) + goto out_subpool_put; + } + ret =hugetlb_cgroup_charge_cgroup (idx,pages_per_huge_page (h), &h_cg);if (ret) - goto out_subpool_put; + goto out_uncharge_cgroup_reservation;spin_lock (&hugetlb_lock);/*
@@ -2237,6 +2250,14 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,/* Fall through */ }hugetlb_cgroup_commit_charge (idx,pages_per_huge_page (h), h_cg, page); +/* If allocation is not consuming a reservation, also store the +* hugetlb_cgroup pointer on the page. +*/ +if (deferred_reserve) { +hugetlb_cgroup_commit_charge_rsvd (idx,pages_per_huge_page (h), + h_cg, page); + } +spin_unlock (&hugetlb_lock);set_page_private (page, (unsigned long )spool);
@@ -2261,6 +2282,10 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, out_uncharge_cgroup:hugetlb_cgroup_uncharge_cgroup (idx,pages_per_huge_page (h), h_cg); +out_uncharge_cgroup_reservation: +if (deferred_reserve) +hugetlb_cgroup_uncharge_cgroup_rsvd (idx,pages_per_huge_page (h), + h_cg); out_subpool_put:if (map_chg || avoid_reserve)hugepage_subpool_put_pages (spool,1 );
--
2.25.0.225.g125e21ebc7-goog
Re: [PATCH v12 6/9] hugetlb_cgroup: support noreserve mappings
Quoted 70 lines · Mina Almasry
Support
MAP_NORESERVEaccounting as part of the new counter.For each hugepage allocation, at allocation time we check if there is a reservation for this allocation or not. If there is a reservation for this allocation, then this allocation was charged at reservation time, and we don't re-account it. If there is no reserevation for this allocation, we charge the appropriate
hugetlb_cgroup.The
hugetlb_cgroupto uncharge for this allocation is stored in page[3].private. We use new APIs added in an earlier patch to set this pointer.Signed-off-by Mina Almasry <[email protected]>---
Changes in v12:
- Minor rebase to new interface for readability.Changes in v10:
- Refactoreddeferred_reservecheck.--- mm/hugetlb.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)mm/hugetlb.c +14
-1@@ -1342,6 +1342,8 @@ static void __free_huge_page(struct page *page)clear_page_huge_active (page);hugetlb_cgroup_uncharge_page (hstate_index (h),pages_per_huge_page (h), page); +hugetlb_cgroup_uncharge_page_rsvd (hstate_index (h), +pages_per_huge_page (h), page);if (restore_reserve) h->resv_huge_pages++;@@ -2175,6 +2177,7 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,long gbl_chg;int ret, idx;struct hugetlb_cgroup *h_cg; +bool deferred_reserve; idx =hstate_index (h);/* @@ -2212,9 +2215,19 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, gbl_chg =1 ; } +/* If this allocation is not consuming a reservation, charge it now. +*/ + deferred_reserve = map_chg || avoid_reserve || !vma_resv_map (vma); +if (deferred_reserve) { + ret =hugetlb_cgroup_charge_cgroup_rsvd ( + idx,pages_per_huge_page (h), &h_cg); +if (ret) + goto out_subpool_put; + } + ret =hugetlb_cgroup_charge_cgroup (idx,pages_per_huge_page (h), &h_cg);if (ret) - goto out_subpool_put; + goto out_uncharge_cgroup_reservation;spin_lock (&hugetlb_lock);/* @@ -2237,6 +2250,14 @@ struct page *alloc_huge_page(struct vm_area_struct *vma,/* Fall through */
mm/hugetlb.c struct page *alloc_huge_page(struct vm_area_struct *vma, · line 2260
Quoted 10 lines
mm/hugetlb.c +8
-0}hugetlb_cgroup_commit_charge (idx,pages_per_huge_page (h), h_cg, page); +/* If allocation is not consuming a reservation, also store the +* hugetlb_cgroup pointer on the page. +*/ +if (deferred_reserve) { +hugetlb_cgroup_commit_charge_rsvd (idx,pages_per_huge_page (h), + h_cg, page); + } +
This started before your new code, but those two cgroup_commit_charge calls could/should be done outside the hugetlb_lock. No need to change as it is not a big deal. Those calls only set fields in the page structs.
Signature · 1 line
Mike Kravetz
Quoted 17 lines
mm/hugetlb.c +4
-1spin_unlock (&hugetlb_lock);set_page_private (page, (unsigned long )spool);@@ -2261,6 +2282,10 @@ struct page *alloc_huge_page(struct vm_area_struct *vma, out_uncharge_cgroup:hugetlb_cgroup_uncharge_cgroup (idx,pages_per_huge_page (h), h_cg); +out_uncharge_cgroup_reservation: +if (deferred_reserve) +hugetlb_cgroup_uncharge_cgroup_rsvd (idx,pages_per_huge_page (h), + h_cg); out_subpool_put:if (map_chg || avoid_reserve)hugepage_subpool_put_pages (spool,1 ); --2.25.0.225.g125e21ebc7-goog
PATCH v12 7/9 hugetlb: support file_region coalescing again
In reply to Mina AlmasryAn earlier patch in this series disabled file_region coalescing in order to hang the hugetlb_cgroup uncharge info on the file_region entries.
This patch re-adds support for coalescing of file_region entries. Essentially everytime we add an entry, we call a recursive function that tries to coalesce the added region with the regions next to it. The worst case call depth for this function is 3: one to coalesce with the region next to it, one to coalesce to the region prev, and one to reach the base case.
This is an important performance optimization as private mappings add their entries page by page, and we could incur big performance costs for large mappings with lots of file_region entries in their resv_map.
---
Changes in v12:
- Changed logic for coalescing. Instead of checking inline to coalesce with only the region on next or prev, we now have a recursive function that takes care of coalescing in both directions.
- For testing this code I added a bunch of debug code that checks that the entries in the resv_map are coalesced appropriately. This passes with libhugetlbfs tests.
--- mm/hugetlb.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
mm/hugetlb.c +85 -0
@@ -276,6 +276,86 @@ static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,#endif } +static bool has_same_uncharge_info (struct file_region *rg, +struct file_region *org) +{ +#ifdef CONFIG_CGROUP_HUGETLB +return rg && org && + rg->reservation_counter == org->reservation_counter && + rg->css == org->css; + +#else +return true; +#endif +} + +#ifdef CONFIG_DEBUG_VM +static void dump_resv_map (struct resv_map *resv) +{ +struct list_head *head = &resv->regions; +struct file_region *rg =NULL ; + +pr_err ("--------- start print resv_map ---------\n" ); +list_for_each_entry (rg, head, link) { +pr_err ("rg->from=%ld, rg->to=%ld, rg->reservation_counter=%px, rg->css=%px\n" , + rg->from, rg->to, rg->reservation_counter, rg->css); + } +pr_err ("--------- end print resv_map ---------\n" ); +} + +/* Debug function to loop over the resv_map and make sure that coalescing is +* working. +*/ +static void check_coalesce_bug (struct resv_map *resv) +{ +struct list_head *head = &resv->regions; +struct file_region *rg =NULL , *nrg =NULL ; + +list_for_each_entry (rg, head, link) { + nrg =list_next_entry (rg, link); + +if (&nrg->link == head) +break ; + +if (nrg->reservation_counter && nrg->from == rg->to && + nrg->reservation_counter == rg->reservation_counter && + nrg->css == rg->css) { +dump_resv_map (resv); +VM_BUG_ON (true); + } + } +} +#endif + +static void coalesce_file_region (struct resv_map *resv,struct file_region *rg) +{ +struct file_region *nrg =NULL , *prg =NULL ; + + prg =list_prev_entry (rg, link); +if (&prg->link != &resv->regions && prg->to == rg->from && +has_same_uncharge_info (prg, rg)) { + prg->to = rg->to; + +list_del (&rg->link); +kfree (rg); + +coalesce_file_region (resv, prg); +return ; + } + + nrg =list_next_entry (rg, link); +if (&nrg->link != &resv->regions && nrg->from == rg->to && +has_same_uncharge_info (nrg, rg)) { + nrg->from = rg->from; + +list_del (&rg->link); +kfree (rg); + +coalesce_file_region (resv, nrg); +return ; + } +} +/* Must be called with resv->lock held. Calling this with count_only == true * will count the number of pages to be added but will not modify the linked * list. If regions_needed != NULL and count_only == true, then regions_needed
@@ -327,6 +407,7 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,record_hugetlb_cgroup_uncharge_info (h_cg, h, resv, nrg);list_add (&nrg->link, rg->link.prev); +coalesce_file_region (resv, nrg); }else if (regions_needed) *regions_needed +=1 ; }
@@ -344,11 +425,15 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, resv, last_accounted_offset, t);record_hugetlb_cgroup_uncharge_info (h_cg, h, resv, nrg);list_add (&nrg->link, rg->link.prev); +coalesce_file_region (resv, nrg); }else if (regions_needed) *regions_needed +=1 ; }VM_BUG_ON (add <0 ); +#ifdef CONFIG_DEBUG_VM +check_coalesce_bug (resv); +#endif return add; }
Re: [PATCH v12 7/9] hugetlb: support file_region coalescing again
Quoted 10 lines · Mina Almasry
An earlier patch in this series disabled
file_regioncoalescing in order to hang thehugetlb_cgroupuncharge info on thefile_regionentries.This patch re-adds support for coalescing of
file_regionentries. Essentially everytime we add an entry, we call a recursive function that tries to coalesce the added region with the regions next to it. The worst case call depth for this function is 3: one to coalesce with the region next to it, one to coalesce to the region prev, and one to reach the base case.
Quoted 5 lines
This is an important performance optimization as private mappings add their entries page by page, and we could incur big performance costs for large mappings with lots of
file_regionentries in theirresv_map.Signed-off-by Mina Almasry <[email protected]>
Re: [PATCH v12 7/9] hugetlb: support file_region coalescing again
In reply to Mina AlmasryQuoted 132 lines · Mina Almasry
An earlier patch in this series disabled
file_regioncoalescing in order to hang thehugetlb_cgroupuncharge info on thefile_regionentries.This patch re-adds support for coalescing of
file_regionentries. Essentially everytime we add an entry, we call a recursive function that tries to coalesce the added region with the regions next to it. The worst case call depth for this function is 3: one to coalesce with the region next to it, one to coalesce to the region prev, and one to reach the base case.This is an important performance optimization as private mappings add their entries page by page, and we could incur big performance costs for large mappings with lots of
file_regionentries in theirresv_map.Signed-off-by Mina Almasry <[email protected]>---
Changes in v12:
- Changed logic for coalescing. Instead of checking inline to coalesce with only the region on next or prev, we now have a recursive function that takes care of coalescing in both directions.
- For testing this code I added a bunch of debug code that checks that the entries in theresv_mapare coalesced appropriately. This passes with libhugetlbfs tests.--- mm/hugetlb.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)mm/hugetlb.c +81
-0@@ -276,6 +276,86 @@ static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,#endif } +static bool has_same_uncharge_info (struct file_region *rg, +struct file_region *org) +{ +#ifdef CONFIG_CGROUP_HUGETLB +return rg && org && + rg->reservation_counter == org->reservation_counter && + rg->css == org->css; + +#else +return true; +#endif +} + +#ifdef CONFIG_DEBUG_VM +static void dump_resv_map (struct resv_map *resv) +{ +struct list_head *head = &resv->regions; +struct file_region *rg =NULL ; + +pr_err ("--------- start print resv_map ---------\n" ); +list_for_each_entry (rg, head, link) { +pr_err ("rg->from=%ld, rg->to=%ld, rg->reservation_counter=%px, rg->css=%px\n" , + rg->from, rg->to, rg->reservation_counter, rg->css); + } +pr_err ("--------- end print resv_map ---------\n" ); +} + +/* Debug function to loop over the resv_map and make sure that coalescing is +* working. +*/ +static void check_coalesce_bug (struct resv_map *resv) +{ +struct list_head *head = &resv->regions; +struct file_region *rg =NULL , *nrg =NULL ; + +list_for_each_entry (rg, head, link) { + nrg =list_next_entry (rg, link); + +if (&nrg->link == head) +break ; + +if (nrg->reservation_counter && nrg->from == rg->to && + nrg->reservation_counter == rg->reservation_counter && + nrg->css == rg->css) { +dump_resv_map (resv); +VM_BUG_ON (true); + } + } +} +#endif + +static void coalesce_file_region (struct resv_map *resv,struct file_region *rg) +{ +struct file_region *nrg =NULL , *prg =NULL ; + + prg =list_prev_entry (rg, link); +if (&prg->link != &resv->regions && prg->to == rg->from && +has_same_uncharge_info (prg, rg)) { + prg->to = rg->to; + +list_del (&rg->link); +kfree (rg); + +coalesce_file_region (resv, prg); +return ; + } + + nrg =list_next_entry (rg, link); +if (&nrg->link != &resv->regions && nrg->from == rg->to && +has_same_uncharge_info (nrg, rg)) { + nrg->from = rg->from; + +list_del (&rg->link); +kfree (rg); + +coalesce_file_region (resv, nrg); +return ; + } +} +/* Must be called with resv->lock held. Calling this with count_only == true * will count the number of pages to be added but will not modify the linked * list. If regions_needed != NULL and count_only == true, then regions_needed @@ -327,6 +407,7 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,record_hugetlb_cgroup_uncharge_info (h_cg, h, resv, nrg);list_add (&nrg->link, rg->link.prev); +coalesce_file_region (resv, nrg); }else if (regions_needed) *regions_needed +=1 ; }@@ -344,11 +425,15 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, resv, last_accounted_offset, t);record_hugetlb_cgroup_uncharge_info (h_cg, h, resv, nrg);
mm/hugetlb.c static long add_reservation_in_range(struct resv_map *resv, long f, long t, · line 436
Quoted 10 lines
mm/hugetlb.c +4
-0list_add (&nrg->link, rg->link.prev); +coalesce_file_region (resv, nrg); }else if (regions_needed) *regions_needed +=1 ; }VM_BUG_ON (add <0 ); +#ifdef CONFIG_DEBUG_VM +check_coalesce_bug (resv); +#endif
Some distros have CONFIG_DEBUG_VM on in their default kernels. Fedora comes to mind. Yes, this means 'VM_BUG_ON()' become 'BUG_ON()'. That is somewhat accepted. I don't think we want this debug code behind CONFIG_DEBUG_VM and called each time a file region is added. It may be overkill to make it a debug option via the config system. Perhaps, just make it something like CONFIG_DEBUG_HUGETLB_RSV_REGION and let hugetlb developers turn it on if they would like?
Other than that, the code looks good.
Signature · 1 line
Mike Kravetz
Quoted 6 lines
mm/hugetlb.c +0
-1return add; } --2.25.0.225.g125e21ebc7-goog
Re: [PATCH v12 7/9] hugetlb: support file_region coalescing again
Quoted 140 lines · Mike Kravetz
Quoted 138 lines · Mina Almasry
An earlier patch in this series disabled
file_regioncoalescing in order to hang thehugetlb_cgroupuncharge info on thefile_regionentries.This patch re-adds support for coalescing of
file_regionentries. Essentially everytime we add an entry, we call a recursive function that tries to coalesce the added region with the regions next to it. The worst case call depth for this function is 3: one to coalesce with the region next to it, one to coalesce to the region prev, and one to reach the base case.This is an important performance optimization as private mappings add their entries page by page, and we could incur big performance costs for large mappings with lots of
file_regionentries in theirresv_map.Signed-off-by Mina Almasry <[email protected]>---
Changes in v12:
- Changed logic for coalescing. Instead of checking inline to coalesce with only the region on next or prev, we now have a recursive function that takes care of coalescing in both directions.
- For testing this code I added a bunch of debug code that checks that the entries in theresv_mapare coalesced appropriately. This passes with libhugetlbfs tests.--- mm/hugetlb.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)mm/hugetlb.c +82
-0@@ -276,6 +276,86 @@ static void record_hugetlb_cgroup_uncharge_info(struct hugetlb_cgroup *h_cg,#endif } +static bool has_same_uncharge_info (struct file_region *rg, +struct file_region *org) +{ +#ifdef CONFIG_CGROUP_HUGETLB +return rg && org && + rg->reservation_counter == org->reservation_counter && + rg->css == org->css; + +#else +return true; +#endif +} + +#ifdef CONFIG_DEBUG_VM +static void dump_resv_map (struct resv_map *resv) +{ +struct list_head *head = &resv->regions; +struct file_region *rg =NULL ; + +pr_err ("--------- start print resv_map ---------\n" ); +list_for_each_entry (rg, head, link) { +pr_err ("rg->from=%ld, rg->to=%ld, rg->reservation_counter=%px, rg->css=%px\n" , + rg->from, rg->to, rg->reservation_counter, rg->css); + } +pr_err ("--------- end print resv_map ---------\n" ); +} + +/* Debug function to loop over the resv_map and make sure that coalescing is +* working. +*/ +static void check_coalesce_bug (struct resv_map *resv) +{ +struct list_head *head = &resv->regions; +struct file_region *rg =NULL , *nrg =NULL ; + +list_for_each_entry (rg, head, link) { + nrg =list_next_entry (rg, link); + +if (&nrg->link == head) +break ; + +if (nrg->reservation_counter && nrg->from == rg->to && + nrg->reservation_counter == rg->reservation_counter && + nrg->css == rg->css) { +dump_resv_map (resv); +VM_BUG_ON (true); + } + } +} +#endif + +static void coalesce_file_region (struct resv_map *resv,struct file_region *rg) +{ +struct file_region *nrg =NULL , *prg =NULL ; + + prg =list_prev_entry (rg, link); +if (&prg->link != &resv->regions && prg->to == rg->from && +has_same_uncharge_info (prg, rg)) { + prg->to = rg->to; + +list_del (&rg->link); +kfree (rg); + +coalesce_file_region (resv, prg); +return ; + } + + nrg =list_next_entry (rg, link); +if (&nrg->link != &resv->regions && nrg->from == rg->to && +has_same_uncharge_info (nrg, rg)) { + nrg->from = rg->from; + +list_del (&rg->link); +kfree (rg); + +coalesce_file_region (resv, nrg); +return ; + } +} +/* Must be called with resv->lock held. Calling this with count_only == true * will count the number of pages to be added but will not modify the linked * list. If regions_needed != NULL and count_only == true, then regions_needed @@ -327,6 +407,7 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t,record_hugetlb_cgroup_uncharge_info (h_cg, h, resv, nrg);list_add (&nrg->link, rg->link.prev); +coalesce_file_region (resv, nrg); }else if (regions_needed) *regions_needed +=1 ; }@@ -344,11 +425,15 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, resv, last_accounted_offset, t);record_hugetlb_cgroup_uncharge_info (h_cg, h, resv, nrg);list_add (&nrg->link, rg->link.prev); +coalesce_file_region (resv, nrg); }else if (regions_needed) *regions_needed +=1 ; }
Quoted 13 lines
mm/hugetlb.c static long add_reservation_in_range(struct resv_map *resv, long f, long t, · line 436
Quoted 4 lines
mm/hugetlb.c +3
-0VM_BUG_ON (add <0 ); +#ifdef CONFIG_DEBUG_VM +check_coalesce_bug (resv); +#endif Some distros have
CONFIG_DEBUG_VMon in their default kernels. Fedora comes to mind. Yes, this means 'VM_BUG_ON()' become 'BUG_ON()'. That is somewhat accepted. I don't think we want this debug code behindCONFIG_DEBUG_VMand called each time a file region is added. It may be overkill to make it a debug option via the config system. Perhaps, just make it something likeCONFIG_DEBUG_HUGETLB_RSV_REGIONand let hugetlb developers turn it on if they would like?
Ah, I feel like adding a whole config to accommodate this function is overkill and will end up being dead code anyway as no-one remembers to turn it on. If it's the same to you I'll take it out of the patch and leave it as a local change for me to test with.
Quoted 10 lines
Other than that, the code looks good.
--
Mike KravetzQuoted 6 lines
mm/hugetlb.c +0
-1return add; } --2.25.0.225.g125e21ebc7-goog
PATCH 1/1 hugetlb: Remove check_coalesce_bug debug code
In reply to Mike KravetzCommit b5f16a533ce8a ("hugetlb: support file_region coalescing again") made changes to the resv_map code which are hard to test, it so added debug code guarded by CONFIG_DEBUG_VM which conducts an expensive operation that loops over the resv_map and checks it for errors.
Unfortunately, some distros have CONFIG_DEBUG_VM on in their default kernels, and we don't want this debug code behind CONFIG_DEBUG_VM and called each time a file region is added. This patch removes this debug code. I may look into making it a test or leave it for my local testing.
--- mm/hugetlb.c | 43 ------------------------------------------- 1 file changed, 43 deletions(-)
mm/hugetlb.c +0 -43
@@ -289,48 +289,6 @@ static bool has_same_uncharge_info(struct file_region *rg,#endif } -#if defined(CONFIG_DEBUG_VM ) && defined(CONFIG_CGROUP_HUGETLB ) -static void dump_resv_map (struct resv_map *resv) -{ -struct list_head *head = &resv->regions; -struct file_region *rg =NULL ; - -pr_err ("--------- start print resv_map ---------\n" ); -list_for_each_entry (rg, head, link) { -pr_err ("rg->from=%ld, rg->to=%ld, rg->reservation_counter=%px, rg->css=%px\n" , - rg->from, rg->to, rg->reservation_counter, rg->css); - } -pr_err ("--------- end print resv_map ---------\n" ); -} - -/* Debug function to loop over the resv_map and make sure that coalescing is -* working. -*/ -static void check_coalesce_bug (struct resv_map *resv) -{ -struct list_head *head = &resv->regions; -struct file_region *rg =NULL , *nrg =NULL ; - -list_for_each_entry (rg, head, link) { - nrg =list_next_entry (rg, link); - -if (&nrg->link == head) -break ; - -if (nrg->reservation_counter && nrg->from == rg->to && - nrg->reservation_counter == rg->reservation_counter && - nrg->css == rg->css) { -dump_resv_map (resv); -VM_BUG_ON (true); - } - } -} -#else -static void check_coalesce_bug (struct resv_map *resv) -{ -} -#endif -static void coalesce_file_region (struct resv_map *resv,struct file_region *rg) {struct file_region *nrg =NULL , *prg =NULL ;
@@ -435,7 +393,6 @@ static long add_reservation_in_range(struct resv_map *resv, long f, long t, }VM_BUG_ON (add <0 ); -check_coalesce_bug (resv);return add; }
--
2.25.0.265.gbab2e86ba0-goog
Re: [PATCH] hugetlb: Remove check_coalesce_bug debug code
Quoted 14 lines · Mina Almasry
Commit
b5f16a533ce8a("hugetlb: supportfile_regioncoalescing again") made changes to theresv_mapcode which are hard to test, it so added debug code guarded byCONFIG_DEBUG_VMwhich conducts an expensive operation that loops over theresv_mapand checks it for errors.Unfortunately, some distros have
CONFIG_DEBUG_VMon in their default kernels, and we don't want this debug code behindCONFIG_DEBUG_VMand called each time a file region is added. This patch removes this debug code. I may look into making it a test or leave it for my local testing.Signed-off-by Mina Almasry <[email protected]>
Quoted 8 lines
Cc David Rientjes <[email protected]>Cc Mike Kravetz <[email protected]>Cc Shakeel Butt <[email protected]>Cc Andrew Morton <[email protected]>Fixes b5f16a533ce8a ("hugetlb: support file_region coalescing again")
Thanks!
The review also applies to the original patch with the removal of this code.
Signature · 1 line
Mike Kravetz
PATCH v12 8/9 hugetlb_cgroup: Add hugetlb_cgroup reservation tests
In reply to Mina AlmasryThe tests use both shared and private mapped hugetlb memory, and monitors the hugetlb usage counter as well as the hugetlb reservation counter. They test different configurations such as hugetlb memory usage via hugetlbfs, or MAP_HUGETLB, or shmget/shmat, and with and without MAP_POPULATE.
Also add test for hugetlb reservation reparenting, since this is a subtle issue.
---
Changes in v12:
- Fixed tests on machines with non-2MB default hugepage size. Changes in v11:
- Modify test to not assume 2MB hugepage size.
- Updated resv.* to rsvd.*
Changes in v10:
- Updated tests to resv.* name changes.
Changes in v9:
- Added tests for hugetlb reparenting.
- Make tests explicitly support cgroup v1 and v2 via script argument. Changes in v6:
- Updates tests for cgroups-v2 and NORESERVE allocations.
--- tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 1 + .../selftests/vm/charge_reserved_hugetlb.sh | 575 ++++++++++++++++++ .../selftests/vm/hugetlb_reparenting_test.sh | 244 ++++++++ .../selftests/vm/write_hugetlb_memory.sh | 23 + .../testing/selftests/vm/write_to_hugetlbfs.c | 242 ++++++++ 6 files changed, 1086 insertions(+) create mode 100755 tools/testing/selftests/vm/charge_reserved_hugetlb.sh create mode 100755 tools/testing/selftests/vm/hugetlb_reparenting_test.sh create mode 100644 tools/testing/selftests/vm/write_hugetlb_memory.sh create mode 100644 tools/testing/selftests/vm/write_to_hugetlbfs.c
tools/testing/selftests/vm/.gitignore +1 -0
@@ -14,3 +14,4 @@ virtual_address_range gup_benchmark va_128TBswitch map_fixed_noreplace +write_to_hugetlbfs
tools/testing/selftests/vm/Makefile +1 -0
@@ -22,6 +22,7 @@ TEST_GEN_FILES += userfaultfd ifneq (,$(filter $(ARCH),arm64 ia64 mips64 parisc64 ppc64 riscv64 s390x sh64 sparc64 x86_64)) TEST_GEN_FILES += va_128TBswitch TEST_GEN_FILES += virtual_address_range +TEST_GEN_FILES += write_to_hugetlbfs endif TEST_PROGS := run_vmtests
tools/testing/selftests/vm/charge_reserved_hugetlb.sh new file +575 -0
575 lines not on this page · raw message
tools/testing/selftests/vm/hugetlb_reparenting_test.sh new file +244 -0
244 lines not on this page · raw message
tools/testing/selftests/vm/write_hugetlb_memory.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +set -e + +size=$1 +populate=$2 +write=$3 +cgroup=$4 +path=$5 +method=$6 +private=$7 +want_sleep=$8 +reserve=$9 + +echo "Putting task in cgroup '$cgroup'" +echo $$ > /dev/cgroup/memory/"$cgroup"/cgroup.procs + +echo "Method is $method" + +set +e +./write_to_hugetlbfs -p "$path" -s "$size" "$write" "$populate" -m "$method" \ + "$private" "$want_sleep" "$reserve"
tools/testing/selftests/vm/write_to_hugetlbfs.c new file +242 -0
242 lines not on this page · raw message
Re: [PATCH v12 8/9] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
Quoted 15 lines · Mina Almasry
The tests use both shared and private mapped hugetlb memory, and monitors the hugetlb usage counter as well as the hugetlb reservation counter. They test different configurations such as hugetlb memory usage via hugetlbfs, or
MAP_HUGETLB, or shmget/shmat, and with and withoutMAP_POPULATE.Also add test for hugetlb reservation reparenting, since this is a subtle issue.
Signed-off-by Mina Almasry <[email protected]>---
Quoted 13 lines
Changes in v12:
- Fixed tests on machines with non-2MB default hugepage size. Changes in v11:
- Modify test to not assume 2MB hugepage size.
- Updated resv.* to rsvd.*
Changes in v10:
- Updated tests to resv.* name changes.
Changes in v9:
- Added tests for hugetlb reparenting.
- Make tests explicitly support cgroup v1 and v2 via script argument. Changes in v6:
- Updates tests for cgroups-v2 and NORESERVE allocations.
For powerpc64,
Re: [PATCH v12 8/9] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
Quoted 10 lines · Sandipan Das
Quoted 6 lines · Mina Almasry
The tests use both shared and private mapped hugetlb memory, and monitors the hugetlb usage counter as well as the hugetlb reservation counter. They test different configurations such as hugetlb memory usage via hugetlbfs, or
MAP_HUGETLB, or shmget/shmat, and with and withoutMAP_POPULATE.
Quoted 5 lines
Quoted 5 lines
Also add test for hugetlb reservation reparenting, since this is a subtle issue.
Signed-off-by Mina Almasry <[email protected]>
Hi folks,
Sandipan provided a Tested-by but this is more or less the only patch in the series that is awaiting review. Can someone take a look?
Shuah, you started reviewing this months ago and I addressed the comments. Maybe you can take another look?
Thanks in advance!
Re: [PATCH v12 8/9] hugetlb_cgroup: Add hugetlb_cgroup reservation tests
In reply to Mina AlmasryQuoted 11 lines · Mina Almasry
The tests use both shared and private mapped hugetlb memory, and monitors the hugetlb usage counter as well as the hugetlb reservation counter. They test different configurations such as hugetlb memory usage via hugetlbfs, or
MAP_HUGETLB, or shmget/shmat, and with and withoutMAP_POPULATE.Also add test for hugetlb reservation reparenting, since this is a subtle issue.
Signed-off-by Mina Almasry <[email protected]>
Thanks for adding the tests. At a high level, I do not see any issues.
Signature · 1 line
Mike Kravetz
PATCH v12 9/9 hugetlb_cgroup: Add hugetlb_cgroup reservation docs
In reply to Mina AlmasryAdd docs for how to use hugetlb_cgroup reservations, and their behavior.
---
Changes in v11:
- Changed resv.* to rsvd.*
Changes in v10:
- Clarify reparenting behavior.
- Reword benefits of reservation limits.
Changes in v6:
- Updated docs to reflect the new design based on a new counter that tracks both reservations and faults.
--- .../admin-guide/cgroup-v1/hugetlb.rst | 103 ++++++++++++++++-- 1 file changed, 92 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v1/hugetlb.rst +92 -11
@@ -2,13 +2,6 @@ HugeTLB Controller ================== -The HugeTLB controller allows to limit the HugeTLB usage per control group and -enforces the controller limit during page fault. Since HugeTLB doesn't -support page reclaim, enforcing the limit at page fault time implies that, -the application will get SIGBUS signal if it tries to access HugeTLB pages -beyond its limit. This requires the application to know beforehand how much -HugeTLB pages it would require for its use. - HugeTLB controller can be created by first mounting the cgroup filesystem. # mount -t cgroup -o hugetlb none /sys/fs/cgroup
@@ -28,10 +21,14 @@ process (bash) into it. Brief summary of control files:: - hugetlb.<hugepagesize>.limit_in_bytes # set/show limit of "hugepagesize" hugetlb usage - hugetlb.<hugepagesize>.max_usage_in_bytes # show max "hugepagesize" hugetlb usage recorded - hugetlb.<hugepagesize>.usage_in_bytes # show current usage for "hugepagesize" hugetlb - hugetlb.<hugepagesize>.failcnt # show the number of allocation failure due to HugeTLB limit + hugetlb.<hugepagesize>.rsvd.limit_in_bytes # set/show limit of "hugepagesize" hugetlb reservations + hugetlb.<hugepagesize>.rsvd.max_usage_in_bytes # show max "hugepagesize" hugetlb reservations and no-reserve faults + hugetlb.<hugepagesize>.rsvd.usage_in_bytes # show current reservations and no-reserve faults for "hugepagesize" hugetlb + hugetlb.<hugepagesize>.rsvd.failcnt # show the number of allocation failure due to HugeTLB reservation limit + hugetlb.<hugepagesize>.limit_in_bytes # set/show limit of "hugepagesize" hugetlb faults + hugetlb.<hugepagesize>.max_usage_in_bytes # show max "hugepagesize" hugetlb usage recorded + hugetlb.<hugepagesize>.usage_in_bytes # show current usage for "hugepagesize" hugetlb + hugetlb.<hugepagesize>.failcnt # show the number of allocation failure due to HugeTLB usage limit For a system supporting three hugepage sizes (64k, 32M and 1G), the control files include::
@@ -40,11 +37,95 @@ files include:: hugetlb.1GB.max_usage_in_bytes hugetlb.1GB.usage_in_bytes hugetlb.1GB.failcnt + hugetlb.1GB.rsvd.limit_in_bytes + hugetlb.1GB.rsvd.max_usage_in_bytes + hugetlb.1GB.rsvd.usage_in_bytes + hugetlb.1GB.rsvd.failcnt hugetlb.64KB.limit_in_bytes hugetlb.64KB.max_usage_in_bytes hugetlb.64KB.usage_in_bytes hugetlb.64KB.failcnt + hugetlb.64KB.rsvd.limit_in_bytes + hugetlb.64KB.rsvd.max_usage_in_bytes + hugetlb.64KB.rsvd.usage_in_bytes + hugetlb.64KB.rsvd.failcnt hugetlb.32MB.limit_in_bytes hugetlb.32MB.max_usage_in_bytes hugetlb.32MB.usage_in_bytes hugetlb.32MB.failcnt + hugetlb.32MB.rsvd.limit_in_bytes + hugetlb.32MB.rsvd.max_usage_in_bytes + hugetlb.32MB.rsvd.usage_in_bytes + hugetlb.32MB.rsvd.failcnt + + +1. Page fault accounting + +hugetlb.<hugepagesize>.limit_in_bytes +hugetlb.<hugepagesize>.max_usage_in_bytes +hugetlb.<hugepagesize>.usage_in_bytes +hugetlb.<hugepagesize>.failcnt + +The HugeTLB controller allows users to limit the HugeTLB usage (page fault) per +control group and enforces the limit during page fault. Since HugeTLB +doesn't support page reclaim, enforcing the limit at page fault time implies +that, the application will get SIGBUS signal if it tries to fault in HugeTLB +pages beyond its limit. Therefore the application needs to know exactly how many +HugeTLB pages it uses before hand, and the sysadmin needs to make sure that +there are enough available on the machine for all the users to avoid processes +getting SIGBUS. + + +2. Reservation accounting + +hugetlb.<hugepagesize>.rsvd.limit_in_bytes +hugetlb.<hugepagesize>.rsvd.max_usage_in_bytes +hugetlb.<hugepagesize>.rsvd.usage_in_bytes +hugetlb.<hugepagesize>.rsvd.failcnt + +The HugeTLB controller allows to limit the HugeTLB reservations per control +group and enforces the controller limit at reservation time and at the fault of +HugeTLB memory for which no reservation exists. Since reservation limits are +enforced at reservation time (on mmap or shget), reservation limits never causes +the application to get SIGBUS signal if the memory was reserved before hand. For +MAP_NORESERVE allocations, the reservation limit behaves the same as the fault +limit, enforcing memory usage at fault time and causing the application to +receive a SIGBUS if it's crossing its limit. + +Reservation limits are superior to page fault limits described above, since +reservation limits are enforced at reservation time (on mmap or shget), and +never causes the application to get SIGBUS signal if the memory was reserved +before hand. This allows for easier fallback to alternatives such as +non-HugeTLB memory for example. In the case of page fault accounting, it's very +hard to avoid processes getting SIGBUS since the sysadmin needs precisely know +the HugeTLB usage of all the tasks in the system and make sure there is enough +pages to satisfy all requests. Avoiding tasks getting SIGBUS on overcommited +systems is practically impossible with page fault accounting. + + +3. Caveats with shared memory + +For shared HugeTLB memory, both HugeTLB reservation and page faults are charged +to the first task that causes the memory to be reserved or faulted, and all +subsequent uses of this reserved or faulted memory is done without charging. + +Shared HugeTLB memory is only uncharged when it is unreserved or deallocated. +This is usually when the HugeTLB file is deleted, and not when the task that +caused the reservation or fault has exited. + + +4. Caveats with HugeTLB cgroup offline. + +When a HugeTLB cgroup goes offline with some reservations or faults still +charged to it, the behavior is as follows: + +- The fault charges are charged to the parent HugeTLB cgroup (reparented), +- the reservation charges remain on the offline HugeTLB cgroup. + +This means that if a HugeTLB cgroup gets offlined while there is still HugeTLB +reservations charged to it, that cgroup persists as a zombie until all HugeTLB +reservations are uncharged. HugeTLB reservations behave in this manner to match +the memory controller whose cgroups also persist as zombie until all charged +memory is uncharged. Also, the tracking of HugeTLB reservations is a bit more +complex compared to the tracking of HugeTLB faults, so it is significantly +harder to reparent reservations at offline time.
--
2.25.0.225.g125e21ebc7-goog
Re: [PATCH v12 9/9] hugetlb_cgroup: Add hugetlb_cgroup reservation docs
Quoted 5 lines · Mina Almasry
Add docs for how to use
hugetlb_cgroupreservations, and their behavior.Signed-off-by Mina Almasry <[email protected]>
Hi folks,
By my count this patch and the tests are the only patches in the series awaiting review. Can you folks give it a look?
Thanks in advance!
Re: [PATCH v12 9/9] hugetlb_cgroup: Add hugetlb_cgroup reservation docs
In reply to Mina AlmasryQuoted 3 lines · Mina Almasry
Add docs for how to use
hugetlb_cgroupreservations, and their behavior.Signed-off-by Mina Almasry <[email protected]>
Thanks for updating the documentation.
Signature · 1 line
Mike Kravetz
Andrew Morton
maintainerQuoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry…
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Quoted 10 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry…
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Reverted the whole series on the top of next-20200217 fixed a crash below (I don't see anything in next-20200218 would make any differences).
[ 7933.691114][T35046] LTP: starting hugemmap06 [ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1] [ 7933.806573][T14355] LE PAGE_SIZE=64K MMU=Radix SMP NR_CPUS=256 DEBUG_PAGEALLOC NUMA PowerNV [ 7933.806594][T14355] Modules linked in: kvm_hv kvm brd ext4 crc16 mbcache jbd2 loop ip_tables x_tables xfs sd_mod bnx2x ahci mdio libahci tg3 libata libphy firmware_class dm_mirror dm_region_hash dm_log dm_mod [last unloaded: binfmt_misc] [ 7933.806651][T14355] CPU: 54 PID: 14355 Comm: hugemmap06 Tainted: G O 5.6.0-rc2-next-20200217 #1 [ 7933.806674][T14355] NIP: c00000000040d22c LR: c00000000040d210 CTR: 0000000000000000 [ 7933.806696][T14355] REGS: c0000014b71ef660 TRAP: 0700 Tainted: G O (5.6.0-rc2-next-20200217) [ 7933.806727][T14355] MSR: 900000000282b033 <SF,HV,VEC,VSX,EE,FP,ME,IR,DR,RI,LE> CR: 22022228 XER: 00000000 [ 7933.806772][T14355] CFAR: c00000000040cbec IRQMASK: 0 [ 7933.806772][T14355] GPR00: c00000000040d210 c0000014b71ef8f0 c000000001657000 0000000000000001 [ 7933.806772][T14355] GPR04: 0000000000000012 0000000000000013 0000000000000000 0000000000000000 [ 7933.806772][T14355] GPR08: 0000000000000002 0000000000000002 0000000000000001 0000000000000036 [ 7933.806772][T14355] GPR12: 0000000022022222 c000001ffffd3d00 00007fffad670000 00007fffa4bc0000 [ 7933.806772][T14355] GPR16: 0000000000000000 c000000001567178 c0000014b71efa50 0000000000000000 [ 7933.806772][T14355] GPR20: 0000000000000000 0000000000000013 0000000000000012 0000000000000001 [ 7933.806772][T14355] GPR24: c0000019f74cd270 5deadbeef0000100 5deadbeef0000122 c0000019f74cd2c0 [ 7933.806772][T14355] GPR28: 0000000000000001 c0000019f74cd268 c0000014b71ef918 0000000000000001 [ 7933.806961][T14355] NIP [c00000000040d22c] region_add+0x11c/0x3a0 [ 7933.806980][T14355] LR [c00000000040d210] region_add+0x100/0x3a0 [ 7933.807008][T14355] Call Trace: [ 7933.807024][T14355] [c0000014b71ef8f0] [c00000000040d210] region_add+0x100/0x3a0 (unreliable) [ 7933.807056][T14355] [c0000014b71ef9b0] [c00000000040e0c8] __vma_reservation_common+0x148/0x210 __vma_reservation_common at mm/hugetlb.c:2150 [ 7933.807087][T14355] [c0000014b71efa20] [c0000000004132a0] alloc_huge_page+0x350/0x830 alloc_huge_page at mm/hugetlb.c:2359 [ 7933.807100][T14355] [c0000014b71efad0] [c0000000004168f8] hugetlb_no_page+0x158/0xcb0 [ 7933.807113][T14355] [c0000014b71efc20] [c000000000417bc8] hugetlb_fault+0x678/0xb30 [ 7933.807136][T14355] [c0000014b71efcd0] [c0000000003b1de4] handle_mm_fault+0x444/0x450 [ 7933.807158][T14355] [c0000014b71efd20] [c000000000070b1c] __do_page_fault+0x2bc/0xfd0 [ 7933.807181][T14355] [c0000014b71efe20] [c00000000000aa88] handle_page_fault+0x10/0x30 [ 7933.807201][T14355] Instruction dump: [ 7933.807209][T14355] 38c00000 7ea5ab78 7ec4b378 7fa3eb78 4bfff80d e9210020 e91d0050 e95d0068 [ 7933.807232][T14355] 7d3c4850 7d294214 7faa4800 409c0238 <0b170000> 7f03c378 4858c005 60000000 [ 7933.807267][T14355] ---[ end trace 7560275de5f409f8 ]--- [ 7933.905258][T14355] [ 7934.905339][T14355] Kernel panic - not syncing: Fatal exception
Quoted 73 lines · Qian Cai
Quoted 10 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry…
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Reverted the whole series on the top of next-20200217 fixed a crash below (I don't see anything in next-20200218 would make any differences).
[ 7933.691114][T35046] LTP: starting hugemmap06 [ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1] [ 7933.806573][T14355] LE PAGE_SIZE=64K MMU=Radix SMP NR_CPUS=256 DEBUG_PAGEALLOC NUMA PowerNV [ 7933.806594][T14355] Modules linked in: kvm_hv kvm brd ext4 crc16 mbcache jbd2 loop ip_tables x_tables xfs sd_mod bnx2x ahci mdio libahci tg3 libata libphy firmware_class dm_mirror dm_region_hash dm_log dm_mod [last unloaded: binfmt_misc] [ 7933.806651][T14355] CPU: 54 PID: 14355 Comm: hugemmap06 Tainted: G O 5.6.0-rc2-next-20200217 #1 [ 7933.806674][T14355] NIP: c00000000040d22c LR: c00000000040d210 CTR: 0000000000000000 [ 7933.806696][T14355] REGS: c0000014b71ef660 TRAP: 0700 Tainted: G O (5.6.0-rc2-next-20200217) [ 7933.806727][T14355] MSR: 900000000282b033 <SF,HV,VEC,VSX,EE,FP,ME,IR,DR,RI,LE> CR: 22022228 XER: 00000000 [ 7933.806772][T14355] CFAR: c00000000040cbec IRQMASK: 0 [ 7933.806772][T14355] GPR00: c00000000040d210 c0000014b71ef8f0 c000000001657000 0000000000000001 [ 7933.806772][T14355] GPR04: 0000000000000012 0000000000000013 0000000000000000 0000000000000000 [ 7933.806772][T14355] GPR08: 0000000000000002 0000000000000002 0000000000000001 0000000000000036 [ 7933.806772][T14355] GPR12: 0000000022022222 c000001ffffd3d00 00007fffad670000 00007fffa4bc0000 [ 7933.806772][T14355] GPR16: 0000000000000000 c000000001567178 c0000014b71efa50 0000000000000000 [ 7933.806772][T14355] GPR20: 0000000000000000 0000000000000013 0000000000000012 0000000000000001 [ 7933.806772][T14355] GPR24: c0000019f74cd270 5deadbeef0000100 5deadbeef0000122 c0000019f74cd2c0 [ 7933.806772][T14355] GPR28: 0000000000000001 c0000019f74cd268 c0000014b71ef918 0000000000000001 [ 7933.806961][T14355] NIP [c00000000040d22c] region_add+0x11c/0x3a0 [ 7933.806980][T14355] LR [c00000000040d210] region_add+0x100/0x3a0 [ 7933.807008][T14355] Call Trace: [ 7933.807024][T14355] [c0000014b71ef8f0] [c00000000040d210] region_add+0x100/0x3a0 (unreliable) [ 7933.807056][T14355] [c0000014b71ef9b0] [c00000000040e0c8] __vma_reservation_common+0x148/0x210 __vma_reservation_common at mm/hugetlb.c:2150 [ 7933.807087][T14355] [c0000014b71efa20] [c0000000004132a0] alloc_huge_page+0x350/0x830 alloc_huge_page at mm/hugetlb.c:2359 [ 7933.807100][T14355] [c0000014b71efad0] [c0000000004168f8] hugetlb_no_page+0x158/0xcb0 [ 7933.807113][T14355] [c0000014b71efc20] [c000000000417bc8] hugetlb_fault+0x678/0xb30 [ 7933.807136][T14355] [c0000014b71efcd0] [c0000000003b1de4] handle_mm_fault+0x444/0x450 [ 7933.807158][T14355] [c0000014b71efd20] [c000000000070b1c] __do_page_fault+0x2bc/0xfd0 [ 7933.807181][T14355] [c0000014b71efe20] [c00000000000aa88]
handle_page_fault+0x10/0x30
Quoted 8 lines
[ 7933.807201][T14355] Instruction dump: [ 7933.807209][T14355] 38c00000 7ea5ab78 7ec4b378 7fa3eb78 4bfff80d e9210020 e91d0050 e95d0068 [ 7933.807232][T14355] 7d3c4850 7d294214 7faa4800 409c0238 <0b170000> 7f03c378 4858c005 60000000 [ 7933.807267][T14355] ---[ end trace 7560275de5f409f8 ]--- [ 7933.905258][T14355] [ 7934.905339][T14355] Kernel panic - not syncing: Fatal exception
Hi Qian,
Yes this VM_BUG_ON was added by a patch in the series ("hugetlb:
disable region_add file_region coalescing") so it's definitely related to the series. I'm taking a look at why this VM_BUG_ON fires. Can you confirm you reproduce this by running hugemmap06 from the ltp on a powerpc machine? Can I maybe have your config?
Thanks!
Quoted 83 lines · Mina Almasry
Quoted 73 lines · Qian Cai
Quoted 10 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Reverted the whole series on the top of next-20200217 fixed a crash below (I don't see anything in next-20200218 would make any differences).
[ 7933.691114][T35046] LTP: starting hugemmap06 [ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1] [ 7933.806573][T14355] LE PAGE_SIZE=64K MMU=Radix SMP NR_CPUS=256 DEBUG_PAGEALLOC NUMA PowerNV [ 7933.806594][T14355] Modules linked in: kvm_hv kvm brd ext4 crc16 mbcache jbd2 loop ip_tables x_tables xfs sd_mod bnx2x ahci mdio libahci tg3 libata libphy firmware_class dm_mirror dm_region_hash dm_log dm_mod [last unloaded: binfmt_misc] [ 7933.806651][T14355] CPU: 54 PID: 14355 Comm: hugemmap06 Tainted: G O 5.6.0-rc2-next-20200217 #1 [ 7933.806674][T14355] NIP: c00000000040d22c LR: c00000000040d210 CTR: 0000000000000000 [ 7933.806696][T14355] REGS: c0000014b71ef660 TRAP: 0700 Tainted: G O (5.6.0-rc2-next-20200217) [ 7933.806727][T14355] MSR: 900000000282b033 <SF,HV,VEC,VSX,EE,FP,ME,IR,DR,RI,LE> CR: 22022228 XER: 00000000 [ 7933.806772][T14355] CFAR: c00000000040cbec IRQMASK: 0 [ 7933.806772][T14355] GPR00: c00000000040d210 c0000014b71ef8f0 c000000001657000 0000000000000001 [ 7933.806772][T14355] GPR04: 0000000000000012 0000000000000013 0000000000000000 0000000000000000 [ 7933.806772][T14355] GPR08: 0000000000000002 0000000000000002 0000000000000001 0000000000000036 [ 7933.806772][T14355] GPR12: 0000000022022222 c000001ffffd3d00 00007fffad670000 00007fffa4bc0000 [ 7933.806772][T14355] GPR16: 0000000000000000 c000000001567178 c0000014b71efa50 0000000000000000 [ 7933.806772][T14355] GPR20: 0000000000000000 0000000000000013 0000000000000012 0000000000000001 [ 7933.806772][T14355] GPR24: c0000019f74cd270 5deadbeef0000100 5deadbeef0000122 c0000019f74cd2c0 [ 7933.806772][T14355] GPR28: 0000000000000001 c0000019f74cd268 c0000014b71ef918 0000000000000001 [ 7933.806961][T14355] NIP [c00000000040d22c] region_add+0x11c/0x3a0 [ 7933.806980][T14355] LR [c00000000040d210] region_add+0x100/0x3a0 [ 7933.807008][T14355] Call Trace: [ 7933.807024][T14355] [c0000014b71ef8f0] [c00000000040d210] region_add+0x100/0x3a0 (unreliable) [ 7933.807056][T14355] [c0000014b71ef9b0] [c00000000040e0c8] __vma_reservation_common+0x148/0x210 __vma_reservation_common at mm/hugetlb.c:2150 [ 7933.807087][T14355] [c0000014b71efa20] [c0000000004132a0] alloc_huge_page+0x350/0x830 alloc_huge_page at mm/hugetlb.c:2359 [ 7933.807100][T14355] [c0000014b71efad0] [c0000000004168f8] hugetlb_no_page+0x158/0xcb0 [ 7933.807113][T14355] [c0000014b71efc20] [c000000000417bc8] hugetlb_fault+0x678/0xb30 [ 7933.807136][T14355] [c0000014b71efcd0] [c0000000003b1de4] handle_mm_fault+0x444/0x450 [ 7933.807158][T14355] [c0000014b71efd20] [c000000000070b1c] __do_page_fault+0x2bc/0xfd0 [ 7933.807181][T14355] [c0000014b71efe20] [c00000000000aa88]
handle_page_fault+0x10/0x30Quoted 8 lines
[ 7933.807201][T14355] Instruction dump: [ 7933.807209][T14355] 38c00000 7ea5ab78 7ec4b378 7fa3eb78 4bfff80d e9210020 e91d0050 e95d0068 [ 7933.807232][T14355] 7d3c4850 7d294214 7faa4800 409c0238 <0b170000> 7f03c378 4858c005 60000000 [ 7933.807267][T14355] ---[ end trace 7560275de5f409f8 ]--- [ 7933.905258][T14355] [ 7934.905339][T14355] Kernel panic - not syncing: Fatal exception
Quoted 7 lines
Hi Qian,
Yes this
VM_BUG_ONwas added by a patch in the series ("hugetlb:
disableregion_addfile_regioncoalescing") so it's definitely related to the series. I'm taking a look at why thisVM_BUG_ONfires. Can you confirm you reproduce this by running hugemmap06 from the ltp on a powerpc machine? Can I maybe have your config?
Yes, reproduced on both powerpc and x86. Configs are in,
Quoted 9 lines · Mina Almasry
Quoted 8 lines · Qian Cai
Quoted 2 lines · Andrew Morton
On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
[ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1]
<snip>
Quoted 9 lines
Hi Qian,
Yes this
VM_BUG_ONwas added by a patch in the series ("hugetlb:
disableregion_addfile_regioncoalescing") so it's definitely related to the series. I'm taking a look at why thisVM_BUG_ONfires. Can you confirm you reproduce this by running hugemmap06 from the ltp on a powerpc machine? Can I maybe have your config?Thanks!
Hi Mina,
Looking at the region_chg code again, we do a
resv->adds_in_progress += *out_regions_needed;
and then potentially drop the lock to allocate the needed entries. Could anopther thread (only adding reservation for a single page) then come in and notice that there are not enough entries in the cache and hit the VM_BUG_ON()?
Signature · 1 line
Mike Kravetz
Quoted 24 lines · Mike Kravetz
Quoted 9 lines · Mina Almasry
Quoted 8 lines · Qian Cai
Quoted 2 lines · Andrew Morton
On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
[ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1]<snip>
Quoted 9 lines
Hi Qian,
Yes this
VM_BUG_ONwas added by a patch in the series ("hugetlb:
disableregion_addfile_regioncoalescing") so it's definitely related to the series. I'm taking a look at why thisVM_BUG_ONfires. Can you confirm you reproduce this by running hugemmap06 from the ltp on a powerpc machine? Can I maybe have your config?Thanks!
Hi Mina,
Quoted 8 lines
Looking at the
region_chgcode again, we do aresv->adds_in_progress += *out_regions_needed;and then potentially drop the lock to allocate the needed entries. Could anopther thread (only adding reservation for a single page) then come in and notice that there are not enough entries in the cache and hit the
VM_BUG_ON()?
Maybe. Also I'm thinking the code thinks actual_regions_needed >= in_regions_needed, but that doesn't seem like a guarantee. I think this call sequence with the same t->f range would violate that:
region_chg (regions_needed=1)region_chg (regions_needed=1)region_add (fills in the range)region_add (in_regions_needed = 1, actual_regions_needed = 0, so assumptions in the code break).
Luckily it seems the ltp readily reproduces this, so I'm working on reproducing it. I should have a fix soon, at least if I can reproduce it as well.
Quoted 45 lines · Mina Almasry
Quoted 24 lines · Mike Kravetz
Quoted 9 lines · Mina Almasry
Quoted 8 lines · Qian Cai
Quoted 2 lines · Andrew Morton
On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
[ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1]<snip>
Quoted 9 lines
Hi Qian,
Yes this
VM_BUG_ONwas added by a patch in the series ("hugetlb:
disableregion_addfile_regioncoalescing") so it's definitely related to the series. I'm taking a look at why thisVM_BUG_ONfires. Can you confirm you reproduce this by running hugemmap06 from the ltp on a powerpc machine? Can I maybe have your config?Thanks!
Hi Mina,
Quoted 8 lines
Looking at the
region_chgcode again, we do aresv->adds_in_progress += *out_regions_needed;and then potentially drop the lock to allocate the needed entries. Could anopther thread (only adding reservation for a single page) then come in and notice that there are not enough entries in the cache and hit the
VM_BUG_ON()?Maybe. Also I'm thinking the code thinks
actual_regions_needed>=in_regions_needed, but that doesn't seem like a guarantee. I think this call sequence with the samet->frange would violate that:
region_chg(regions_needed=1)region_chg(regions_needed=1)region_add(fills in the range)region_add(in_regions_needed= 1,actual_regions_needed= 0, so assumptions in the code break).
Quoted 3 lines
Luckily it seems the ltp readily reproduces this, so I'm working on reproducing it. I should have a fix soon, at least if I can reproduce it as well.
I had a bit of trouble reproducing this but I got it just now.
Makes sense I've never run into this even though others can readily reproduce it. I happen to run my kernels on a pretty beefy 36 core machine and in that setup things seem to execute fast and there is never a queue of pending file_region inserts into the resv_map. Once I limited qemu to only use 2 cores I ran into the issue right away. Looking into a fix now.
Quoted 50 lines · Mina Almasry
Quoted 45 lines · Mina Almasry
Quoted 24 lines · Mike Kravetz
Quoted 9 lines · Mina Almasry
Quoted 8 lines · Qian Cai
Quoted 2 lines · Andrew Morton
On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
[ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1]<snip>
Quoted 9 lines
Hi Qian,
Yes this
VM_BUG_ONwas added by a patch in the series ("hugetlb:
disableregion_addfile_regioncoalescing") so it's definitely related to the series. I'm taking a look at why thisVM_BUG_ONfires. Can you confirm you reproduce this by running hugemmap06 from the ltp on a powerpc machine? Can I maybe have your config?Thanks!
Hi Mina,
Quoted 8 lines
Looking at the
region_chgcode again, we do aresv->adds_in_progress += *out_regions_needed;and then potentially drop the lock to allocate the needed entries. Could anopther thread (only adding reservation for a single page) then come in and notice that there are not enough entries in the cache and hit the
VM_BUG_ON()?Maybe. Also I'm thinking the code thinks
actual_regions_needed>=in_regions_needed, but that doesn't seem like a guarantee. I think this call sequence with the samet->frange would violate that:
region_chg(regions_needed=1)region_chg(regions_needed=1)region_add(fills in the range)region_add(in_regions_needed= 1,actual_regions_needed= 0, so assumptions in the code break).Quoted 3 lines
Luckily it seems the ltp readily reproduces this, so I'm working on reproducing it. I should have a fix soon, at least if I can reproduce it as well.
Quoted 8 lines
I had a bit of trouble reproducing this but I got it just now.
Makes sense I've never run into this even though others can readily reproduce it. I happen to run my kernels on a pretty beefy 36 core machine and in that setup things seem to execute fast and there is never a queue of pending
file_regioninserts into theresv_map. Once I limited qemu to only use 2 cores I ran into the issue right away. Looking into a fix now.
This may not be optimal, but it resolves the issue for me. I just put it together to test the theory that the region_chg code was at fault.
Signature · 1 line
Mike Kravetz
mm/hugetlb.c +16 -3
@@ -549,6 +549,7 @@ static long region_chg(struct resv_map *resv, long f, long t,struct file_region *trg =NULL , *rg =NULL ;long chg =0 , i =0 , to_allocate =0 ;struct list_head allocated_regions; +long calc_adds_in_progress =0 ;INIT_LIST_HEAD (&allocated_regions);
@@ -561,14 +562,14 @@ static long region_chg(struct resv_map *resv, long f, long t,if (*out_regions_needed ==0 ) *out_regions_needed =1 ; - resv->adds_in_progress += *out_regions_needed; + calc_adds_in_progress = resv->adds_in_progress + *out_regions_needed;/* * Check for sufficient descriptors in the cache to accommodate * the number of in progress add operations. */ -while (resv->region_cache_count < resv->adds_in_progress) { - to_allocate = resv->adds_in_progress - resv->region_cache_count; +while (resv->region_cache_count < calc_adds_in_progress) { + to_allocate = calc_adds_in_progress - resv->region_cache_count;/* Must drop lock to allocate a new descriptor. Note that even * though we drop the lock here, we do not make another call to
@@ -593,8 +594,20 @@ static long region_chg(struct resv_map *resv, long f, long t,list_add (&rg->link, &resv->region_cache); resv->region_cache_count++; } + + chg =add_reservation_in_range (resv, f, t,NULL ,NULL , + out_regions_needed, true); + +if (*out_regions_needed ==0 ) + *out_regions_needed =1 ; + + calc_adds_in_progress = resv->adds_in_progress + + *out_regions_needed; + } + resv->adds_in_progress += *out_regions_needed; +spin_unlock (&resv->lock);return chg;
Quoted 61 lines · Mike Kravetz
Quoted 50 lines · Mina Almasry
Quoted 45 lines · Mina Almasry
Quoted 24 lines · Mike Kravetz
Quoted 9 lines · Mina Almasry
Quoted 8 lines · Qian Cai
Quoted 2 lines · Andrew Morton
On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
[ 7933.806377][T14355] ------------[ cut here ]------------ [ 7933.806541][T14355] kernel BUG at mm/hugetlb.c:490! VM_BUG_ON(t - f <= 1); [ 7933.806562][T14355] Oops: Exception in kernel mode, sig: 5 [#1]<snip>
Quoted 9 lines
Hi Qian,
Yes this
VM_BUG_ONwas added by a patch in the series ("hugetlb:
disableregion_addfile_regioncoalescing") so it's definitely related to the series. I'm taking a look at why thisVM_BUG_ONfires. Can you confirm you reproduce this by running hugemmap06 from the ltp on a powerpc machine? Can I maybe have your config?Thanks!
Hi Mina,
Quoted 8 lines
Looking at the
region_chgcode again, we do aresv->adds_in_progress += *out_regions_needed;and then potentially drop the lock to allocate the needed entries. Could anopther thread (only adding reservation for a single page) then come in and notice that there are not enough entries in the cache and hit the
VM_BUG_ON()?Maybe. Also I'm thinking the code thinks
actual_regions_needed>=in_regions_needed, but that doesn't seem like a guarantee. I think this call sequence with the samet->frange would violate that:
region_chg(regions_needed=1)region_chg(regions_needed=1)region_add(fills in the range)region_add(in_regions_needed= 1,actual_regions_needed= 0, so assumptions in the code break).Quoted 3 lines
Luckily it seems the ltp readily reproduces this, so I'm working on reproducing it. I should have a fix soon, at least if I can reproduce it as well.
Quoted 8 lines
I had a bit of trouble reproducing this but I got it just now.
Makes sense I've never run into this even though others can readily reproduce it. I happen to run my kernels on a pretty beefy 36 core machine and in that setup things seem to execute fast and there is never a queue of pending
file_regioninserts into theresv_map. Once I limited qemu to only use 2 cores I ran into the issue right away. Looking into a fix now.
Quoted 2 lines
This may not be optimal, but it resolves the issue for me. I just put it together to test the theory that the
region_chgcode was at fault.
Thanks! Just sent out a similar patch "[PATCH -next] mm/hugetlb: Fix file_region entry allocations"
Quoted 11 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Hi Andrew,
Since the patches were merged into -next there have been build fixes and test fixes and some review comments. Would you like me to submit *new* patches to address these, or would you like me to squash the fixes into my existing patch series and submit another iteration of the patch series?
Andrew Morton
maintainerQuoted 13 lines · On Wed, 19 Feb 2020 11:05:41 -0800 Mina Almasry…
Quoted 11 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry…
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Quoted 7 lines
Hi Andrew,
Since the patches were merged into -next there have been build fixes and test fixes and some review comments. Would you like me to submit *new* patches to address these, or would you like me to squash the fixes into my existing patch series and submit another iteration of the patch series?
What you did worked OK ;)
Please check the end result next time I release a kernel.
Quoted 24 lines · Andrew Morton
Quoted 13 lines · On Wed, 19 Feb 2020 11:05:41 -0800 Mina Almasry <[email protected]> wrote:
Quoted 11 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Quoted 7 lines
Hi Andrew,
Since the patches were merged into -next there have been build fixes and test fixes and some review comments. Would you like me to submit *new* patches to address these, or would you like me to squash the fixes into my existing patch series and submit another iteration of the patch series?
Quoted 3 lines
What you did worked OK ;)
Please check the end result next time I release a kernel.
Thanks Andrew! Things definitely moved along after the patchseries got into -next :D
By my count I think all my patches outside of the tests patch have been acked or reviewed. When you have a chance I have a couple of questions:
1. For the non-tests patch, anything pending on those preventing eventual submission to linus's tree?
2. For the tests patch, I only have a Tested-by from Sandipan. Is that good enough? If the worst comes to worst and I don't get a review on that patch I would rather (if possible) that 'tests' patch can be dropped while I nag folks for a review, rather than block submission of the entire patch series. I ask because it's been out for review for some time and it's the one I got least discussion on so I'm not sure I'll have a review by the time it's needed.
Thanks again!
Andrew Morton
maintainerQuoted 36 lines · On Thu, 20 Feb 2020 11:22:58 -0800 Mina Almasry <[email protected]> wrote:
Quoted 24 lines · Andrew Morton
Quoted 13 lines · On Wed, 19 Feb 2020 11:05:41 -0800 Mina Almasry <[email protected]> wrote:
Quoted 11 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry <[email protected]> wrote:
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Quoted 7 lines
Hi Andrew,
Since the patches were merged into -next there have been build fixes and test fixes and some review comments. Would you like me to submit *new* patches to address these, or would you like me to squash the fixes into my existing patch series and submit another iteration of the patch series?
Quoted 3 lines
What you did worked OK ;)
Please check the end result next time I release a kernel.
Thanks Andrew! Things definitely moved along after the patchseries got into -next :D
By my count I think all my patches outside of the tests patch have been acked or reviewed. When you have a chance I have a couple of questions:
Quoted 10 lines
1. For the non-tests patch, anything pending on those preventing eventual submission to linus's tree?
2. For the tests patch, I only have a Tested-by from Sandipan. Is that good enough? If the worst comes to worst and I don't get a review on that patch I would rather (if possible) that 'tests' patch can be dropped while I nag folks for a review, rather than block submission of the entire patch series. I ask because it's been out for review for some time and it's the one I got least discussion on so I'm not sure I'll have a review by the time it's needed.
It all looks pretty good and I expect we can get everything into 5.7-rc1, unless some issues pop up.
It's unclear to me whether
http://lkml.kernel.org/r/CAHS8izOTipknnYaKz=FdzL-7yW-Z61ck1yPnYWixyMSJuTUYLQ@mail.gmail.com
was going to result in an update?
Quoted 6 lines · Andrew Morton
It all looks pretty good and I expect we can get everything into 5.7-rc1, unless some issues pop up.
It's unclear to me whether
http://lkml.kernel.org/r/CAHS8izOTipknnYaKz=FdzL-7yW-Z61ck1yPnYWixyMSJuTUYLQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
was going to result in an update?
Yes there was,
BTW, I've been through the selftest code at a high level. Not at the level of detail Shuah may provide. I am just happy that Mina provided a relatively comprehensive test for this new functionality. Will give it an ACK shortly.
Signature · 1 line
Mike Kravetz
Quoted 5 lines · Mike Kravetz
Quoted 3 lines · Andrew Morton
It all looks pretty good and I expect we can get everything into 5.7-rc1, unless some issues pop up.
Awesome! I'll be on the lookout for issues.
Folks that reviewed (and especially Mike), thank you so much for the patient reviews!
Quoted 13 lines
Quoted 3 lines
It's unclear to me whether
http://lkml.kernel.org/r/CAHS8izOTipknnYaKz=FdzL-7yW-Z61ck1yPnYWixyMSJuTUYLQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
was going to result in an update?Yes there was,
BTW, I've been through the selftest code at a high level. Not at the level of detail Shuah may provide. I am just happy that Mina provided a relatively comprehensive test for this new functionality. Will give it an ACK shortly. --
Mike Kravetz
Quoted 24 lines · Andrew Morton
Quoted 13 lines · On Wed, 19 Feb 2020 11:05:41 -0800 Mina Almasry…
Quoted 11 lines · Andrew Morton
Quoted 3 lines · On Tue, 11 Feb 2020 13:31:20 -0800 Mina Almasry…
These counters will track hugetlb reservations rather than hugetlb memory faulted in. This patch only adds the counter, following patches add the charging and uncharging of the counter.
We're still pretty thin on review here, but as it's v12 and Mike appears to be signed up to look at this work, I'll add them to -next to help move things forward.
Quoted 7 lines
Hi Andrew,
Since the patches were merged into -next there have been build fixes and test fixes and some review comments. Would you like me to submit *new* patches to address these, or would you like me to squash the fixes into my existing patch series and submit another iteration of the patch series?
Quoted 3 lines
What you did worked OK ;)
Please check the end result next time I release a kernel.
Hey Andrew,
Thanks for taking in the patset and fixes. Only pending change in the latest -next tree is this one:
https://lore.kernel.org/linux-mm/20200219233610.13808-1-almasrymina-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org/
It's reviewed by Mike here:
https://lore.kernel.org/linux-mm/a0d7b8e1-cb43-3b43-68c3-55631f2ce199-QHcLZuEGTsvQT0dZR+AlfA@public.gmane.org/