memcg memory accounting in vmalloc is broken
Subsystems (4)
vmalloc was switched to __alloc_pages_bulk but it does not account the memory to memcg.
Is it known issue perhaps?
thank you,
Vasily Averin
Michal Hocko
reviewerQuoted 3 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerCc Mel and Uladzislau
Quoted 10 lines · Michal Hocko
Quoted 3 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
Signature · 2 lines
Michal Hocko SUSE Labs
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 10 lines · Michal Hocko
Cc Mel and Uladzislau
Quoted 7 lines · Michal Hocko
Quoted 3 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
I use following patch as a quick fix,
it helps though it is far from ideal and can be optimized.
mm/page_alloc.c +6 -0
@@ -5290,6 +5290,12 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, page =__rmqueue_pcplist (zone,0 , ac.migratetype, alloc_flags, pcp, pcp_list); + +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT) && page && +unlikely (__memcg_kmem_charge_page (page, gfp,0 ) !=0 )) { +__free_pages (page,0 ); + page =NULL ; + }if (unlikely (!page)) {/* Try and get at least one page */ if (!nr_populated)
Signature · 1 line
2.31.1
Michal Hocko
reviewerQuoted 34 lines · Vasily Averin
Quoted 10 lines · Michal Hocko
Cc Mel and Uladzislau
Quoted 7 lines · Michal Hocko
Quoted 3 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
I use following patch as a quick fix,
it helps though it is far from ideal and can be optimized.mm/page_alloc.c +6
-0@@ -5290,6 +5290,12 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, page =__rmqueue_pcplist (zone,0 , ac.migratetype, alloc_flags, pcp, pcp_list); + +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT) && page && +unlikely (__memcg_kmem_charge_page (page, gfp,0 ) !=0 )) { +__free_pages (page,0 ); + page =NULL ; + }if (unlikely (!page)) {/* Try and get at least one page */ if (!nr_populated)Signature · 1 line
2.31.1
Yes, this makes sense to me.
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 7 lines · Vasily Averin
Quoted 6 lines · Michal Hocko
Cc Mel and Uladzislau
Quoted 3 lines · Michal Hocko
Quoted 2 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
I use following patch as a quick fix,
it helps though it is far from ideal and can be optimized.
Thanks Vasily.
This papers over the problem but it could certainly be optimized. At minimum;
1. Test (memcg_kmem_enabled() && (gfp & __GFP_ACCOUNT)) in the function preamble and store the result in a bool
2. Avoid the temptation to batch the accounting because if the accounting fails, there is no information on how many pages could be allocated before the limits were hit. I guess you could pre-charge the pages and uncharging the number of pages that failed to be allocated but it should be a separate patch.
3. If an allocation fails due to memcg accounting, break
out of the loop because all remaining bulk allocations are also likely to fail.
As it's not vmalloc's fault, I would suggest the patch have
and
Cc: <[email protected]>
Note the Cc should just be in the patch and not mailed directly to stable@ as it'll simply trigger a form letter about the patch having to be merged to mainline first.
Signature · 2 lines
Mel Gorman SUSE Labs
Quoted 16 lines · Mel Gorman
Quoted 7 lines · Vasily Averin
Quoted 6 lines · Michal Hocko
Cc Mel and Uladzislau
Quoted 3 lines · Michal Hocko
Quoted 2 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
I use following patch as a quick fix,
it helps though it is far from ideal and can be optimized.
Quoted 7 lines
Thanks Vasily.
This papers over the problem but it could certainly be optimized. At minimum;
1. Test (
memcg_kmem_enabled()&& (gfp &__GFP_ACCOUNT)) in the function preamble and store the result in a bool
memcg_kmem_enabled() is a static key based check so caching that defeats its purpose and changes it to a normal branch. That leaves gfp & __GFP_ACCOUNT, which may perhaps still benefit from such caching.
Quoted 19 lines
2. Avoid the temptation to batch the accounting because if the accounting fails, there is no information on how many pages could be allocated before the limits were hit. I guess you could pre-charge the pages and uncharging the number of pages that failed to be allocated but it should be a separate patch.
3. If an allocation fails due to memcg accounting, break
out of the loop because all remaining bulk allocations are also likely to fail.As it's not vmalloc's fault, I would suggest the patch have
Fixes 387ba26fb1cb ("mm/page_alloc: add a bulk page allocator")and
Cc: <[email protected]>Note the Cc should just be in the patch and not mailed directly to stable@ as it'll simply trigger a form letter about the patch having to be merged to mainline first.
Quoted 20 lines · Vasily Averin
Quoted 10 lines · Michal Hocko
Cc Mel and Uladzislau
Quoted 7 lines · Michal Hocko
Quoted 3 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
I use following patch as a quick fix,
it helps though it is far from ideal and can be optimized.mm/page_alloc.c +0
-0
mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5296
Quoted 7 lines
mm/page_alloc.c +4
-0@@ -5290,6 +5290,12 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, page =__rmqueue_pcplist (zone,0 , ac.migratetype, alloc_flags, pcp, pcp_list); + +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT) && page && +unlikely (__memcg_kmem_charge_page (page, gfp,0 ) !=0 )) { +__free_pages (page,0 );
It's too early for this here, we didn't go through prep_new_page() yet, minimally the post_alloc_hook() -> set_page_refcounted() part, required for put_page_testzero() in __free_pages() to work properly, and probably other stuff.
Either do the full prep+free or a minimal reversion of __rmqueue_pcplist - something to just put the page back to pcplist. Probably the former so we don't introduce subtle errors.
Quoted 6 lines
mm/page_alloc.c +2
-0+ page =NULL ; + }if (unlikely (!page)) {/* Try and get at least one page */ if (!nr_populated)
Michal Hocko
reviewerQuoted 22 lines · Vlastimil Babka
Quoted 21 lines · Vasily Averin
Quoted 10 lines · Michal Hocko
Cc Mel and Uladzislau
Quoted 7 lines · Michal Hocko
Quoted 3 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
I use following patch as a quick fix,
it helps though it is far from ideal and can be optimized.mm/page_alloc.c +0
-0@@ -5290,6 +5290,12 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,
Quoted 14 lines
mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5296
Quoted 6 lines
mm/page_alloc.c +4
-0page =__rmqueue_pcplist (zone,0 , ac.migratetype, alloc_flags, pcp, pcp_list); + +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT) && page && +unlikely (__memcg_kmem_charge_page (page, gfp,0 ) !=0 )) { +__free_pages (page,0 );It's too early for this here, we didn't go through
prep_new_page()yet, minimally thepost_alloc_hook()->set_page_refcounted()part, required forput_page_testzero()in__free_pages()to work properly, and probably other stuff.
Either do the full prep+free or a minimal reversion of__rmqueue_pcplist- something to just put the page back to pcplist. Probably the former so we don't introduce subtle errors.
Very well spotted!
Signature · 2 lines
Michal Hocko SUSE Labs
Quoted 7 lines · Vasily Averin
Quoted 6 lines · Michal Hocko
Cc Mel and Uladzislau
Quoted 3 lines · Michal Hocko
Quoted 2 lines · Vasily Averin
vmalloc was switched to
__alloc_pages_bulkbut it does not account the memory to memcg.
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Is it known issue perhaps?
No, I think this was just overlooked. Definitely doesn't look intentional to me.
I use following patch as a quick fix,
it helps though it is far from ideal and can be optimized.
Dear all,
thanks a lot for your comments and suggestions!
Unfortunately current patch version does not work properly. We cannot charge each page in this cycle, with taken pagesets spinlock, lockdep reports about possible deadlock.
Trying to use pre-charge, as suggested by Mel Gorman.
Quoted 18 lines
mm/page_alloc.c +6
-0@@ -5290,6 +5290,12 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, page =__rmqueue_pcplist (zone,0 , ac.migratetype, alloc_flags, pcp, pcp_list); + +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT) && page && +unlikely (__memcg_kmem_charge_page (page, gfp,0 ) !=0 )) { +__free_pages (page,0 ); + page =NULL ; + }if (unlikely (!page)) {/* Try and get at least one page */ if (!nr_populated)
PATCH 1/1 memcg: enable memory accounting in __alloc_pages_bulk
Enable memory accounting for bulk page allocator.
--- mm/page_alloc.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-)
mm/page_alloc.c +62 -2
@@ -5172,6 +5172,55 @@ static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order,return true; } +#ifdef CONFIG_MEMCG_KMEM +static bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +struct obj_cgroup *objcg =NULL ; + +if (!memcg_kmem_enabled () || !(gfp & __GFP_ACCOUNT)) +return true; + + objcg =get_obj_cgroup_from_current (); + +if (objcg &&obj_cgroup_charge (objcg, gfp, nr_pages <<PAGE_SHIFT )) { +obj_cgroup_put (objcg); +return false; + } +obj_cgroup_get_many (objcg, nr_pages); + *objcgp = objcg; +return true; +} + +static void memcg_bulk_charge_hook (struct obj_cgroup *objcg, +struct page *page) +{ + page->memcg_data = (unsigned long )objcg |MEMCG_DATA_KMEM ; +} + +static void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +obj_cgroup_uncharge (objcg, nr_pages <<PAGE_SHIFT ); +percpu_ref_put_many (&objcg->refcnt, nr_pages +1 ); +} +#else +static bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +return true; +} + +static void memcg_bulk_charge_hook (struct obj_cgroup *objcgp, +struct page *page) +{ +} + +static void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +} +#endif /* * __alloc_pages_bulk - Allocate a number of order-0 pages to a list or array * @gfp: GFP flags for the allocation
@@ -5207,6 +5256,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,gfp_t alloc_gfp;unsigned int alloc_flags =ALLOC_WMARK_LOW ;int nr_populated =0 , nr_account =0 ; +unsigned int nr_pre_charge =0 ; +struct obj_cgroup *objcg =NULL ;/* * Skip populated array elements to determine if any pages need
@@ -5275,6 +5326,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,if (unlikely (!zone)) goto failed; + nr_pre_charge = nr_pages - nr_populated; +if (!memcg_bulk_pre_charge_hook (&objcg, gfp, nr_pre_charge)) + goto failed; +/* Attempt the batch allocation */ local_lock_irqsave (&pagesets.lock, flags); pcp =this_cpu_ptr (zone->per_cpu_pageset);
@@ -5287,9 +5342,9 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_populated++;continue ; } - page =__rmqueue_pcplist (zone,0 , ac.migratetype, alloc_flags, pcp, pcp_list); +if (unlikely (!page)) {/* Try and get at least one page */ if (!nr_populated)
@@ -5297,6 +5352,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,break ; } nr_account++; +if (objcg) +memcg_bulk_charge_hook (objcg, page);prep_new_page (page,0 , gfp,0 );if (page_list)
@@ -5310,13 +5367,16 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account); +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge - nr_account); out:return nr_populated; failed_irq:local_unlock_irqrestore (&pagesets.lock, flags); - +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge); failed: page =__alloc_pages (gfp,0 , preferred_nid, nodemask);if (page) {
Signature · 1 line
2.31.1
Re: [PATCH memcg] memcg: enable memory accounting in __alloc_pages_bulk
+Roman
Quoted 15 lines · Vasily Averin
Enable memory accounting for bulk page allocator.
Fixes 387ba26fb1cb ("mm/page_alloc: add a bulk page allocator")Cc <[email protected]>Signed-off-by Vasily Averin <[email protected]>--- mm/page_alloc.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-)mm/page_alloc.c +0
-0
mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5174
Quoted 3 lines
mm/page_alloc.c +0
-0@@ -5172,6 +5172,55 @@ static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order,return true; }
Please move the following memcg functions to memcontrol.[h|c] files.
mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5186
Quoted 12 lines
mm/page_alloc.c +12
-0+#ifdef CONFIG_MEMCG_KMEM +static bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +struct obj_cgroup *objcg =NULL ; + +if (!memcg_kmem_enabled () || !(gfp & __GFP_ACCOUNT)) +return true; + + objcg =get_obj_cgroup_from_current (); + +if (objcg &&obj_cgroup_charge (objcg, gfp, nr_pages <<PAGE_SHIFT )) {
Please use obj_cgroup_charge_pages() when you move this code to memcontrol.c
Quoted 9 lines
mm/page_alloc.c +9
-0+obj_cgroup_put (objcg); +return false; + } +obj_cgroup_get_many (objcg, nr_pages); + *objcgp = objcg; +return true; +} + +static void memcg_bulk_charge_hook(struct obj_cgroup *objcg,
mm/page_alloc.c static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, · line 5205
Quoted 10 lines
mm/page_alloc.c +10
-0+ struct page *page) +{ + page->memcg_data = (unsigned long )objcg |MEMCG_DATA_KMEM ; +} + +static void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +obj_cgroup_uncharge (objcg, nr_pages <<PAGE_SHIFT ); +percpu_ref_put_many (&objcg->refcnt, nr_pages +1 );
Introduce the obj_cgroup_put_many() and you don't need to keep the extra ref from the pre hook i.e. put the ref in the pre hook.
Quoted 53 lines
mm/page_alloc.c +25
-1+} +#else +static bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +return true; +} + +static void memcg_bulk_charge_hook (struct obj_cgroup *objcgp, +struct page *page) +{ +} + +static void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +} +#endif /* * __alloc_pages_bulk - Allocate a number of order-0 pages to a list or array * @gfp: GFP flags for the allocation @@ -5207,6 +5256,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,gfp_t alloc_gfp;unsigned int alloc_flags =ALLOC_WMARK_LOW ;int nr_populated =0 , nr_account =0 ; +unsigned int nr_pre_charge =0 ; +struct obj_cgroup *objcg =NULL ;/* * Skip populated array elements to determine if any pages need @@ -5275,6 +5326,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,if (unlikely (!zone)) goto failed; + nr_pre_charge = nr_pages - nr_populated; +if (!memcg_bulk_pre_charge_hook (&objcg, gfp, nr_pre_charge)) + goto failed; +/* Attempt the batch allocation */ local_lock_irqsave (&pagesets.lock, flags); pcp =this_cpu_ptr (zone->per_cpu_pageset);@@ -5287,9 +5342,9 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_populated++;continue ; } - page =__rmqueue_pcplist (zone,0 , ac.migratetype, alloc_flags, pcp, pcp_list); +if (unlikely (!page)) {/* Try and get at least one page */ if (!nr_populated)
mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5356
Quoted 5 lines
mm/page_alloc.c +2
-0@@ -5297,6 +5352,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,break ; } nr_account++; +if (objcg) +memcg_bulk_charge_hook (objcg, page);
Logically this above should be after prep_new_page().
Quoted 24 lines
mm/page_alloc.c +4
-2prep_new_page (page,0 , gfp,0 );if (page_list)@@ -5310,13 +5367,16 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account); +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge - nr_account); out:return nr_populated; failed_irq:local_unlock_irqrestore (&pagesets.lock, flags); - +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge); failed: page =__alloc_pages (gfp,0 , preferred_nid, nodemask);if (page) { --2.31.1
PATCH v2 1/1 memcg: enable memory accounting in __alloc_pages_bulk
Enable memory accounting for bulk page allocator.
---
v2: modified according to Shakeel Butt's remarks
--- include/linux/memcontrol.h | 11 +++++++++ mm/memcontrol.c | 48 +++++++++++++++++++++++++++++++++++++- mm/page_alloc.c | 14 ++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +11 -0
@@ -810,6 +810,12 @@ static inline void obj_cgroup_put(struct obj_cgroup *objcg)percpu_ref_put (&objcg->refcnt); } +static inline void obj_cgroup_put_many (struct obj_cgroup *objcg, +unsigned long nr) +{ +percpu_ref_put_many (&objcg->refcnt, nr); +} +static inline void mem_cgroup_put (struct mem_cgroup *memcg) {if (memcg)
@@ -1746,4 +1752,9 @@ static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages); +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page); +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages);#endif /* _LINUX_MEMCONTROL_H */
mm/memcontrol.c +47 -1
54 lines not on this page · raw message
mm/page_alloc.c +13 -1
@@ -5207,6 +5207,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,gfp_t alloc_gfp;unsigned int alloc_flags =ALLOC_WMARK_LOW ;int nr_populated =0 , nr_account =0 ; +unsigned int nr_pre_charge =0 ; +struct obj_cgroup *objcg =NULL ;/* * Skip populated array elements to determine if any pages need
@@ -5275,6 +5277,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,if (unlikely (!zone)) goto failed; + nr_pre_charge = nr_pages - nr_populated; +if (!memcg_bulk_pre_charge_hook (&objcg, gfp, nr_pre_charge)) + goto failed; +/* Attempt the batch allocation */ local_lock_irqsave (&pagesets.lock, flags); pcp =this_cpu_ptr (zone->per_cpu_pageset);
@@ -5299,6 +5305,9 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_account++;prep_new_page (page,0 , gfp,0 ); +if (objcg) +memcg_bulk_charge_hook (objcg, page); +if (page_list)list_add (&page->lru, page_list);else
@@ -5310,13 +5319,16 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account); +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge - nr_account); out:return nr_populated; failed_irq:local_unlock_irqrestore (&pagesets.lock, flags); - +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge); failed: page =__alloc_pages (gfp,0 , preferred_nid, nodemask);if (page) {
Signature · 1 line
2.31.1
Re: [PATCH mm v2] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 58 lines · Vasily Averin
Enable memory accounting for bulk page allocator.
Fixes 387ba26fb1cb ("mm/page_alloc: add a bulk page allocator")Cc <[email protected]>Signed-off-by Vasily Averin <[email protected]>---
v2: modified according to Shakeel Butt's remarks--- include/linux/memcontrol.h | 11 +++++++++ mm/memcontrol.c | 48 +++++++++++++++++++++++++++++++++++++- mm/page_alloc.c | 14 ++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-)include/linux/memcontrol.h +11
-0@@ -810,6 +810,12 @@ static inline void obj_cgroup_put(struct obj_cgroup *objcg)percpu_ref_put (&objcg->refcnt); } +static inline void obj_cgroup_put_many (struct obj_cgroup *objcg, +unsigned long nr) +{ +percpu_ref_put_many (&objcg->refcnt, nr); +} +static inline void mem_cgroup_put (struct mem_cgroup *memcg) {if (memcg)@@ -1746,4 +1752,9 @@ static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages); +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page); +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages);#endif /* _LINUX_MEMCONTROL_H */ mm/memcontrol.c +9
-1@@ -3239,7 +3239,53 @@ void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)refill_obj_stock (objcg, size, true); } -#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +struct obj_cgroup *objcg =NULL ; + +if (!memcg_kmem_enabled () || !(gfp & __GFP_ACCOUNT)) +return true; + + objcg =get_obj_cgroup_from_current ();
mm/memcontrol.c void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) · line 3260
Quoted 10 lines
mm/memcontrol.c +10
-0+ +if (objcg &&obj_cgroup_charge_pages (objcg, gfp, nr_pages)) { +obj_cgroup_put (objcg); +return false; + } +obj_cgroup_get_many (objcg, nr_pages -1 ); + *objcgp = objcg; +return true; +} +
This is probably a stupid question but why is it necessary to get many references instead of taking one reference here and dropping it in memcg_bulk_post_charge_hook?
mm/memcontrol.c void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) · line 3271
Quoted 11 lines
mm/memcontrol.c +11
-0+void memcg_bulk_charge_hook (struct obj_cgroup *objcg,struct page *page) +{ + page->memcg_data = (unsigned long )objcg |MEMCG_DATA_KMEM ; +} + +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +obj_cgroup_uncharge_pages (objcg, nr_pages); +obj_cgroup_put_many (objcg, nr_pages); +}
And are you sure obj_cgroup_uncharge_pages should be called here? I thought the pages get uncharged on free.
Quoted 75 lines
mm/memcontrol.c +17
-0+#else /* !CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +return true; +} + +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page) +{ +} + +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +} +#endif +/* * Because page_memcg(head) is not set on tails, set it now. mm/page_alloc.c +13
-1@@ -5207,6 +5207,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,gfp_t alloc_gfp;unsigned int alloc_flags =ALLOC_WMARK_LOW ;int nr_populated =0 , nr_account =0 ; +unsigned int nr_pre_charge =0 ; +struct obj_cgroup *objcg =NULL ;/* * Skip populated array elements to determine if any pages need @@ -5275,6 +5277,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,if (unlikely (!zone)) goto failed; + nr_pre_charge = nr_pages - nr_populated; +if (!memcg_bulk_pre_charge_hook (&objcg, gfp, nr_pre_charge)) + goto failed; +/* Attempt the batch allocation */ local_lock_irqsave (&pagesets.lock, flags); pcp =this_cpu_ptr (zone->per_cpu_pageset);@@ -5299,6 +5305,9 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_account++;prep_new_page (page,0 , gfp,0 ); +if (objcg) +memcg_bulk_charge_hook (objcg, page); +if (page_list)list_add (&page->lru, page_list);else @@ -5310,13 +5319,16 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account); +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge - nr_account); out:return nr_populated; failed_irq:local_unlock_irqrestore (&pagesets.lock, flags); - +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge); failed: page =__alloc_pages (gfp,0 , preferred_nid, nodemask);if (page) {Signature · 1 line
2.31.1
Signature · 2 lines
Mel Gorman SUSE Labs
Re: [PATCH mm v2] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 65 lines · Mel Gorman
Quoted 64 lines · Vasily Averin
Enable memory accounting for bulk page allocator.
Fixes 387ba26fb1cb ("mm/page_alloc: add a bulk page allocator")Cc <[email protected]>Signed-off-by Vasily Averin <[email protected]>---
v2: modified according to Shakeel Butt's remarks--- include/linux/memcontrol.h | 11 +++++++++ mm/memcontrol.c | 48 +++++++++++++++++++++++++++++++++++++- mm/page_alloc.c | 14 ++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-)include/linux/memcontrol.h +11
-0@@ -810,6 +810,12 @@ static inline void obj_cgroup_put(struct obj_cgroup *objcg)percpu_ref_put (&objcg->refcnt); } +static inline void obj_cgroup_put_many (struct obj_cgroup *objcg, +unsigned long nr) +{ +percpu_ref_put_many (&objcg->refcnt, nr); +} +static inline void mem_cgroup_put (struct mem_cgroup *memcg) {if (memcg)@@ -1746,4 +1752,9 @@ static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages); +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page); +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages);#endif /* _LINUX_MEMCONTROL_H */ mm/memcontrol.c +15
-1@@ -3239,7 +3239,53 @@ void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)refill_obj_stock (objcg, size, true); } -#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +struct obj_cgroup *objcg =NULL ; + +if (!memcg_kmem_enabled () || !(gfp & __GFP_ACCOUNT)) +return true; + + objcg =get_obj_cgroup_from_current (); + +if (objcg &&obj_cgroup_charge_pages (objcg, gfp, nr_pages)) { +obj_cgroup_put (objcg); +return false; + } +obj_cgroup_get_many (objcg, nr_pages -1 );
Quoted 8 lines
mm/memcontrol.c void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) · line 3260
Quoted 4 lines
mm/memcontrol.c +4
-0+ *objcgp = objcg; +return true; +} +This is probably a stupid question but why is it necessary to get many references instead of taking one reference here and dropping it in
memcg_bulk_post_charge_hook?
Each allocated page keeps the referenece and releases it on free.
Quoted 6 lines
Quoted 6 lines
mm/memcontrol.c +6
-0+void memcg_bulk_charge_hook (struct obj_cgroup *objcg,struct page *page) +{ + page->memcg_data = (unsigned long )objcg |MEMCG_DATA_KMEM ; +} + +void memcg_bulk_post_charge_hook(struct obj_cgroup *objcg,
Quoted 8 lines
mm/memcontrol.c void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) · line 3271
Quoted 5 lines
mm/memcontrol.c +5
-0+ unsigned int nr_pages) +{ +obj_cgroup_uncharge_pages (objcg, nr_pages); +obj_cgroup_put_many (objcg, nr_pages); +}And are you sure
obj_cgroup_uncharge_pagesshould be called here? I thought the pages get uncharged on free.
Here we decrement counters for non-allocated but pre-charged pages. If all pre-chared pages are allocated we release noting. However in this case we will be called with nr_pages = 0, and I think this time it makes sense to add this check. I will do it in next patch version.
Quoted 76 lines
Quoted 75 lines
mm/memcontrol.c +17
-0+#else /* !CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +return true; +} + +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page) +{ +} + +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +} +#endif +/* * Because page_memcg(head) is not set on tails, set it now. mm/page_alloc.c +13
-1@@ -5207,6 +5207,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,gfp_t alloc_gfp;unsigned int alloc_flags =ALLOC_WMARK_LOW ;int nr_populated =0 , nr_account =0 ; +unsigned int nr_pre_charge =0 ; +struct obj_cgroup *objcg =NULL ;/* * Skip populated array elements to determine if any pages need @@ -5275,6 +5277,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,if (unlikely (!zone)) goto failed; + nr_pre_charge = nr_pages - nr_populated; +if (!memcg_bulk_pre_charge_hook (&objcg, gfp, nr_pre_charge)) + goto failed; +/* Attempt the batch allocation */ local_lock_irqsave (&pagesets.lock, flags); pcp =this_cpu_ptr (zone->per_cpu_pageset);@@ -5299,6 +5305,9 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_account++;prep_new_page (page,0 , gfp,0 ); +if (objcg) +memcg_bulk_charge_hook (objcg, page); +if (page_list)list_add (&page->lru, page_list);else @@ -5310,13 +5319,16 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account); +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge - nr_account); out:return nr_populated; failed_irq:local_unlock_irqrestore (&pagesets.lock, flags); - +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge); failed: page =__alloc_pages (gfp,0 , preferred_nid, nodemask);if (page) {Signature · 1 line
2.31.1
PATCH v3 1/1 memcg: enable memory accounting in __alloc_pages_bulk
Enable memory accounting for bulk page allocator.
---
v3: added comments,
removed call of post charge hook for nr_pages = 0
v2: modified according to Shakeel Butt's remarks
--- include/linux/memcontrol.h | 11 +++++++++ mm/memcontrol.c | 48 +++++++++++++++++++++++++++++++++++++- mm/page_alloc.c | 21 ++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +11 -0
@@ -810,6 +810,12 @@ static inline void obj_cgroup_put(struct obj_cgroup *objcg)percpu_ref_put (&objcg->refcnt); } +static inline void obj_cgroup_put_many (struct obj_cgroup *objcg, +unsigned long nr) +{ +percpu_ref_put_many (&objcg->refcnt, nr); +} +static inline void mem_cgroup_put (struct mem_cgroup *memcg) {if (memcg)
@@ -1746,4 +1752,9 @@ static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages); +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page); +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages);#endif /* _LINUX_MEMCONTROL_H */
mm/memcontrol.c +47 -1
54 lines not on this page · raw message
mm/page_alloc.c +20 -1
@@ -5207,6 +5207,8 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,gfp_t alloc_gfp;unsigned int alloc_flags =ALLOC_WMARK_LOW ;int nr_populated =0 , nr_account =0 ; +unsigned int nr_pre_charge =0 ; +struct obj_cgroup *objcg =NULL ;/* * Skip populated array elements to determine if any pages need
@@ -5275,6 +5277,11 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,if (unlikely (!zone)) goto failed; + nr_pre_charge = nr_pages - nr_populated; +/* pre-charge memory and take refcounts for each allocated page */ +if (!memcg_bulk_pre_charge_hook (&objcg, gfp, nr_pre_charge)) + goto failed; +/* Attempt the batch allocation */ local_lock_irqsave (&pagesets.lock, flags); pcp =this_cpu_ptr (zone->per_cpu_pageset);
@@ -5299,6 +5306,9 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, nr_account++;prep_new_page (page,0 , gfp,0 ); +if (objcg) +memcg_bulk_charge_hook (objcg, page); +if (page_list)list_add (&page->lru, page_list);else
@@ -5310,13 +5320,22 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account); +/* +* Typically nr_pre_charge == nr_account, +* otherwise a few extra pages was pre-charged, +* and must be uncharged now. +*/ +if (objcg && nr_pre_charge - nr_account) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge - nr_account); out:return nr_populated; failed_irq:local_unlock_irqrestore (&pagesets.lock, flags); - +/* uncharge memory and decrement refcounts for all pre-charged pages */ +if (objcg) +memcg_bulk_post_charge_hook (objcg, nr_pre_charge); failed: page =__alloc_pages (gfp,0 , preferred_nid, nodemask);if (page) {
Signature · 1 line
2.31.1
Re: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 48 lines · Vasily Averin
Enable memory accounting for bulk page allocator.
Fixes 387ba26fb1cb ("mm/page_alloc: add a bulk page allocator")Cc <[email protected]>Signed-off-by Vasily Averin <[email protected]>---
v3: added comments,
removed call of post charge hook fornr_pages= 0
v2: modified according to Shakeel Butt's remarks--- include/linux/memcontrol.h | 11 +++++++++ mm/memcontrol.c | 48 +++++++++++++++++++++++++++++++++++++- mm/page_alloc.c | 21 ++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-)include/linux/memcontrol.h +11
-0@@ -810,6 +810,12 @@ static inline void obj_cgroup_put(struct obj_cgroup *objcg)percpu_ref_put (&objcg->refcnt); } +static inline void obj_cgroup_put_many (struct obj_cgroup *objcg, +unsigned long nr) +{ +percpu_ref_put_many (&objcg->refcnt, nr); +} +static inline void mem_cgroup_put (struct mem_cgroup *memcg) {if (memcg)@@ -1746,4 +1752,9 @@ static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages); +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page); +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages);#endif /* _LINUX_MEMCONTROL_H */ mm/memcontrol.c +0
-0
mm/memcontrol.c void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) · line 3245
Quoted 8 lines
mm/memcontrol.c +4
-1@@ -3239,7 +3239,53 @@ void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size)refill_obj_stock (objcg, size, true); } -#endif /* CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +struct obj_cgroup *objcg =NULL ;
No need to explicitly set it to NULL.
mm/memcontrol.c void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) · line 3255
Quoted 10 lines
mm/memcontrol.c +10
-0+ +if (!memcg_kmem_enabled () || !(gfp & __GFP_ACCOUNT)) +return true; + + objcg =get_obj_cgroup_from_current (); + +if (objcg &&obj_cgroup_charge_pages (objcg, gfp, nr_pages)) { +obj_cgroup_put (objcg); +return false; + }
get_obj_cgroup_from_current() can return NULL, so you would need to return true early for that condition.
Quoted 6 lines
mm/memcontrol.c +6
-0+obj_cgroup_get_many (objcg, nr_pages -1 ); + *objcgp = objcg; +return true; +} + +void memcg_bulk_charge_hook (struct obj_cgroup *objcg,struct page *page)
mm/memcontrol.c void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) · line 3271
Quoted 10 lines
mm/memcontrol.c +10
-0+{ + page->memcg_data = (unsigned long )objcg |MEMCG_DATA_KMEM ; +} + +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +obj_cgroup_uncharge_pages (objcg, nr_pages); +obj_cgroup_put_many (objcg, nr_pages); +}
You can keep the following '#else' code block in the header file.
Quoted 17 lines
mm/memcontrol.c +17
-0+#else /* !CONFIG_MEMCG_KMEM */ +bool memcg_bulk_pre_charge_hook (struct obj_cgroup **objcgp,gfp_t gfp, +unsigned int nr_pages) +{ +return true; +} + +void memcg_bulk_charge_hook (struct obj_cgroup *objcgp,struct page *page) +{ +} + +void memcg_bulk_post_charge_hook (struct obj_cgroup *objcg, +unsigned int nr_pages) +{ +} +#endif +
Re: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
In reply to Vasily AverinQuoted 5 lines · Vasily Averin
Enable memory accounting for bulk page allocator.
Fixes 387ba26fb1cb ("mm/page_alloc: add a bulk page allocator")Cc <[email protected]>Signed-off-by Vasily Averin <[email protected]>
Signature · 2 lines
Mel Gorman SUSE Labs
Michal Hocko
reviewerRe: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
In reply to Vasily AverinQuoted 1 line · Vasily Averin
Enable memory accounting for bulk page allocator.
ENOCHANGELOG
And I have to say I am not very happy about the solution. It adds a very tricky code where it splits different charging steps apart.
Would it be just too inefficient to charge page-by-page once all pages are already taken away from the pcp lists? This bulk should be small so this shouldn't really cause massive problems. I mean something like
mm/page_alloc.c +4 -1
@@ -5308,6 +5308,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,local_unlock_irqrestore (&pagesets.lock, flags); +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT)) { +/* charge pages here */ + } +__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account); --
Michal Hocko
SUSE Labs
Re: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 18 lines · Michal Hocko
Quoted 1 line · Vasily Averin
Enable memory accounting for bulk page allocator.
ENOCHANGELOG
And I have to say I am not very happy about the solution. It adds a very tricky code where it splits different charging steps apart.
Would it be just too inefficient to charge page-by-page once all pages are already taken away from the pcp lists? This bulk should be small so this shouldn't really cause massive problems. I mean something like
mm/page_alloc.c +0
-0
mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5313
Quoted 6 lines
mm/page_alloc.c +3
-0@@ -5308,6 +5308,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,local_unlock_irqrestore (&pagesets.lock, flags); +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT)) { +/* charge pages here */ + }
It is not that simple because __alloc_pages_bulk only allocate pages for empty slots in the page_array provided by the caller.
The failure handling for post charging would be more complicated.
Michal Hocko
reviewerRe: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 22 lines · Shakeel Butt
Quoted 21 lines · Michal Hocko
Quoted 1 line · Vasily Averin
Enable memory accounting for bulk page allocator.
ENOCHANGELOG
And I have to say I am not very happy about the solution. It adds a very tricky code where it splits different charging steps apart.
Would it be just too inefficient to charge page-by-page once all pages are already taken away from the pcp lists? This bulk should be small so this shouldn't really cause massive problems. I mean something like
mm/page_alloc.c +0
-0@@ -5308,6 +5308,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,local_unlock_irqrestore (&pagesets.lock, flags);
Quoted 8 lines
mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5313
Quoted 3 lines
mm/page_alloc.c +3
-0+if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT)) { +/* charge pages here */ + }It is not that simple because
__alloc_pages_bulkonly allocate pages for empty slots in thepage_arrayprovided by the caller.The failure handling for post charging would be more complicated.
If this is really that complicated (I haven't tried) then it would be much more simple to completely skip the bulk allocator for __GFP_ACCOUNT rather than add a tricky code. The bulk allocator is meant to be used for ultra hot paths and memcg charging along with the reclaim doesn't really fit into that model anyway. Or are there any actual users who really need bulk allocator optimization and also need memcg accounting?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 31 lines · Michal Hocko
Quoted 29 lines · Shakeel Butt
Quoted 18 lines · Michal Hocko
Quoted 1 line · Vasily Averin
Enable memory accounting for bulk page allocator.
ENOCHANGELOG
And I have to say I am not very happy about the solution. It adds a very tricky code where it splits different charging steps apart.
Would it be just too inefficient to charge page-by-page once all pages are already taken away from the pcp lists? This bulk should be small so this shouldn't really cause massive problems. I mean something like
mm/page_alloc.c +0
-0mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5313
Quoted 6 lines
mm/page_alloc.c +3
-0@@ -5308,6 +5308,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,local_unlock_irqrestore (&pagesets.lock, flags); +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT)) { +/* charge pages here */ + }It is not that simple because
__alloc_pages_bulkonly allocate pages for empty slots in thepage_arrayprovided by the caller.
Quoted 8 lines
Quoted 1 line
The failure handling for post charging would be more complicated.
If this is really that complicated (I haven't tried) then it would be much more simple to completely skip the bulk allocator for
__GFP_ACCOUNTrather than add a tricky code. The bulk allocator is meant to be used for ultra hot paths and memcg charging along with the reclaim doesn't really fit into that model anyway. Or are there any actual users who really need bulk allocator optimization and also need memcg accounting?
Bulk allocator is being used for vmalloc and we have several kvmalloc() with __GFP_ACCOUNT allocations.
It seems like Vasily has some ideas, so let's wait for his next version.
Michal Hocko
reviewerRe: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 41 lines · Shakeel Butt
Quoted 31 lines · Michal Hocko
Quoted 29 lines · Shakeel Butt
Quoted 18 lines · Michal Hocko
Quoted 1 line · Vasily Averin
Enable memory accounting for bulk page allocator.
ENOCHANGELOG
And I have to say I am not very happy about the solution. It adds a very tricky code where it splits different charging steps apart.
Would it be just too inefficient to charge page-by-page once all pages are already taken away from the pcp lists? This bulk should be small so this shouldn't really cause massive problems. I mean something like
mm/page_alloc.c +0
-0mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5313
Quoted 6 lines
mm/page_alloc.c +3
-0@@ -5308,6 +5308,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,local_unlock_irqrestore (&pagesets.lock, flags); +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT)) { +/* charge pages here */ + }It is not that simple because
__alloc_pages_bulkonly allocate pages for empty slots in thepage_arrayprovided by the caller.Quoted 8 lines
Quoted 1 line
The failure handling for post charging would be more complicated.
If this is really that complicated (I haven't tried) then it would be much more simple to completely skip the bulk allocator for
__GFP_ACCOUNTrather than add a tricky code. The bulk allocator is meant to be used for ultra hot paths and memcg charging along with the reclaim doesn't really fit into that model anyway. Or are there any actual users who really need bulk allocator optimization and also need memcg accounting?
Quoted 2 lines
Bulk allocator is being used for vmalloc and we have several
kvmalloc()with__GFP_ACCOUNTallocations.
Do we really need to use bulk allocator for these allocations? Bulk allocator is an bypass of the page allocator for performance reason and I can see why that can be useful but considering that the charging path can imply some heavy lifting is all the code churn to make bulk allocator memcg aware really worth it? Why cannot we simply skip over bulk allocator for __GFP_ACCOUNT. That would be a trivial fix.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 1 line · Michal Hocko
[...]
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
If this is really that complicated (I haven't tried) then it would be much more simple to completely skip the bulk allocator for
__GFP_ACCOUNTrather than add a tricky code. The bulk allocator is meant to be used for ultra hot paths and memcg charging along with the reclaim doesn't really fit into that model anyway. Or are there any actual users who really need bulk allocator optimization and also need memcg accounting?Bulk allocator is being used for vmalloc and we have several
kvmalloc()with__GFP_ACCOUNTallocations.
Quoted 7 lines
Do we really need to use bulk allocator for these allocations? Bulk allocator is an bypass of the page allocator for performance reason and I can see why that can be useful but considering that the charging path can imply some heavy lifting is all the code churn to make bulk allocator memcg aware really worth it? Why cannot we simply skip over bulk allocator for
__GFP_ACCOUNT. That would be a trivial fix. --
Actually that might be the simplest solution and I agree to skip bulk allocator for __GFP_ACCOUNT allocations.
Re: [PATCH mm v3] memcg: enable memory accounting in __alloc_pages_bulk
In reply to Michal HockoQuoted 17 lines · Michal Hocko
Quoted 1 line · Vasily Averin
Enable memory accounting for bulk page allocator.
ENOCHANGELOG
And I have to say I am not very happy about the solution. It adds a very tricky code where it splits different charging steps apart.
Would it be just too inefficient to charge page-by-page once all pages are already taken away from the pcp lists? This bulk should be small so this shouldn't really cause massive problems. I mean something like
mm/page_alloc.c +0
-0
mm/page_alloc.c unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, · line 5317
Quoted 10 lines
mm/page_alloc.c +4
-0@@ -5308,6 +5308,10 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,local_unlock_irqrestore (&pagesets.lock, flags); +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT)) { +/* charge pages here */ + } +__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account);
In general it looks like we can do it.
We can traverse via filled page_array or page_list.
For page_array we need to check is the page already accounted
(incoming array can contain some pages already, both in the beginning and in middle) For each taken page we can try to charge it.
If it was charges successfully -- we will process next page in list/array. When charge fails we need to remove rest of pages from list/array and somehow release them. At present I do not understand how to do it correctly -- perhaps just call free_page() ? Finally, we'll need to adjust nr_account and nr_populated properly.
I'll try to implement this tomorrow.
Thank you,
Vasily Averin
PATCH v5 1/1 memcg: enable memory accounting in __alloc_pages_bulk
In reply to Vasily AverinBulk page allocator is used in vmalloc where it can be called with __GFP_ACCOUNT and must charge allocated pages into memory cgroup.
---
v5: remove pre-charge,
use post-allocation per-page charges according to Michal Hocko's remarks
v4: updated according to Shakeel Butt's remarks,
fixed wrong location of hooks declaration in memcontrol.h
v3: added comments,
removed call of post charge hook for nr_pages = 0
v2: modified according to Shakeel Butt's remarks
--- include/linux/memcontrol.h | 9 +++++++ mm/memcontrol.c | 50 ++++++++++++++++++++++++++++++++++++++ mm/page_alloc.c | 12 +++++++-- 3 files changed, 69 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +9 -0
@@ -1692,6 +1692,9 @@ static inline int memcg_cache_id(struct mem_cgroup *memcg)struct mem_cgroup *mem_cgroup_from_obj (void *p); +int memcg_charge_bulk_pages (gfp_t gfp,int nr_pages, +struct list_head *page_list, +struct page **page_array);#else static inline bool mem_cgroup_kmem_disabled (void ) {
@@ -1744,6 +1747,12 @@ static inline struct mem_cgroup *mem_cgroup_from_obj(void *p)return NULL ; } +int memcg_charge_bulk_pages (gfp_t gfp,int nr_pages, +struct list_head *page_list, +struct page **page_array) +{ +return 0 ; +}#endif /* CONFIG_MEMCG_KMEM */ #endif /* _LINUX_MEMCONTROL_H */
mm/memcontrol.c +50 -0
56 lines not on this page · raw message
mm/page_alloc.c +10 -2
@@ -5203,10 +5203,11 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,struct zoneref *z;struct per_cpu_pages *pcp;struct list_head *pcp_list; +LIST_HEAD (tpl);struct alloc_context ac;gfp_t alloc_gfp;unsigned int alloc_flags =ALLOC_WMARK_LOW ; -int nr_populated =0 , nr_account =0 ; +int nr_populated =0 , nr_account =0 , nr_freed =0 ;/* * Skip populated array elements to determine if any pages need
@@ -5300,7 +5301,7 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,prep_new_page (page,0 , gfp,0 );if (page_list) -list_add (&page->lru, page_list); +list_add (&page->lru, &tpl);else page_array[nr_populated] = page; nr_populated++;
@@ -5308,6 +5309,13 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,local_unlock_irqrestore (&pagesets.lock, flags); +if (memcg_kmem_enabled () && (gfp & __GFP_ACCOUNT) && nr_account) + nr_freed =memcg_charge_bulk_pages (gfp, nr_populated, + page_list ? &tpl :NULL , + page_array); + nr_account -= nr_freed; + nr_populated -= nr_freed; +list_splice (&tpl, page_list);__count_zid_vm_events (PGALLOC ,zone_idx (zone), nr_account);zone_statistics (ac.preferred_zoneref->zone, zone, nr_account);
Signature · 1 line
2.31.1
Andrew Morton
maintainerRe: [PATCH mm v5] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 2 lines · On Thu, 14 Oct 2021 11:02:57 +0300 Vasily Averin…
Bulk page allocator is used in vmalloc where it can be called with
__GFP_ACCOUNTand must charge allocated pages into memory cgroup.
Is this problem serious enough to justify -stable backporting? Some words which explaining reasoning for the backport would be helpful.
This patch makes Shakeel's "memcg: page_alloc: skip bulk allocator for __GFP_ACCOUNT" unnecessary. Which should we use?
Re: [PATCH mm v5] memcg: enable memory accounting in __alloc_pages_bulk
Quoted 10 lines · Andrew Morton
Quoted 2 lines · On Thu, 14 Oct 2021 11:02:57 +0300 Vasily Averin <[email protected]> wrote:
Bulk page allocator is used in vmalloc where it can be called with
__GFP_ACCOUNTand must charge allocated pages into memory cgroup.Is this problem serious enough to justify -stable backporting? Some words which explaining reasoning for the backport would be helpful.
This patch makes Shakeel's "memcg:
page_alloc: skip bulk allocator for__GFP_ACCOUNT" unnecessary. Which should we use?
Please use Shakeel's patch.
My patch at least requires review, so at present it should be delayed. I've submitted it because it may be useful later.
Moreover Currently it have minor issue, function in !MEMCG_KMEM branch in of memcontrol.h should be declare as static inline.
Thank you,
Vasily Averin